/ laxyfile / optimize_laxyfile.py
swadhin biswas Ok batman · 7/28/2025
fb9342e
729 lines · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/usr/bin/env python3
"""
LaxyFile Performance Optimization Script

This script analyzes and optimizes the LaxyFile application for:
- Startup time and memory usage
- UI animations and visual effects
- AI response times and caching
- Performance for large directories and files
"""

import asyncio
import sys
import time
import psutil
import gc
from pathlib import Path
from typing import Dict, List, Any
import tempfile
import shutil

# Add the laxyfile directory to the path
sys.path.insert(0, str(Path(__file__).parent))

from laxyfile.core.config import Config
from laxyfile.integration.component_integration import initialize_integration
from laxyfile.utils.logging_system import initialize_logging
from laxyfile.utils.performance_logger import PerformanceLogger


class LaxyFileOptimizer:
    """Main optimization class for LaxyFile"""

    def __init__(self):
        self.config = Config()
        self.logging_system = initialize_logging(self.config)
        self.integrator = None
        self.performance_logger = PerformanceLogger(self.config)
        self.optimization_results = {}

    async def run_optimization_suite(self):
        """Run the complete optimization suite"""
        print("🚀 LaxyFile Performance Optimization Suite")
        print("=" * 60)

        # 1. Analyze current performance
        print("\n1. 📊 Analyzing Current Performance")
        await self.analyze_current_performance()

        # 2. Optimize startup time
        print("\n2. ⚡ Optimizing Startup Time")
        await self.optimize_startup_time()

        # 3. Optimize memory usage
        print("\n3. 🧠 Optimizing Memory Usage")
        await self.optimize_memory_usage()

        # 4. Optimize UI performance
        print("\n4. 🎨 Optimizing UI Performance")
        await self.optimize_ui_performance()

        # 5. Optimize AI performance
        print("\n5. 🤖 Optimizing AI Performance")
        await self.optimize_ai_performance()

        # 6. Optimize file operations
        print("\n6. 📁 Optimizing File Operations")
        await self.optimize_file_operations()

        # 7. Generate optimization report
        print("\n7. 📋 Generating Optimization Report")
        await self.generate_optimization_report()

        print("\n" + "=" * 60)
        print("🎉 Optimization Complete!")

    async def analyze_current_performance(self):
        """Analyze current application performance"""
        print("   Measuring baseline performance...")

        # Measure startup time
        start_time = time.time()
        self.integrator = initialize_integration(self.config)
        await self.integrator.initialize_all_components()
        startup_time = time.time() - start_time

        # Measure memory usage
        process = psutil.Process()
        memory_usage = process.memory_info().rss / 1024 / 1024  # MB

        # Test file manager performance
        file_manager = self.integrator.get_component('file_manager')
        if file_manager:
            start_time = time.time()
            files = await file_manager.list_directory(Path.cwd())
            file_listing_time = time.time() - start_time
        else:
            file_listing_time = 0

        self.optimization_results['baseline'] = {
            'startup_time': startup_time,
            'memory_usage_mb': memory_usage,
            'file_listing_time': file_listing_time,
            'file_count': len(files) if file_manager else 0
        }

        print(f"   ✅ Baseline startup time: {startup_time:.2f}s")
        print(f"   ✅ Baseline memory usage: {memory_usage:.1f}MB")
        print(f"   ✅ File listing time: {file_listing_time:.3f}s ({len(files) if file_manager else 0} files)")

    async def optimize_startup_time(self):
        """Optimize application startup time"""
        print("   Implementing startup optimizations...")

        optimizations = []

        # 1. Lazy loading optimization
        await self.implement_lazy_loading()
        optimizations.append("Lazy loading for non-critical components")

        # 2. Import optimization
        await self.optimize_imports()
        optimizations.append("Optimized import statements")

        # 3. Configuration caching
        await self.implement_config_caching()
        optimizations.append("Configuration caching")

        # 4. Component initialization optimization
        await self.optimize_component_initialization()
        optimizations.append("Parallel component initialization")

        # Test improved startup time
        if self.integrator:
            await self.integrator.shutdown()

        start_time = time.time()
        self.integrator = initialize_integration(self.config)
        await self.integrator.initialize_all_components()
        optimized_startup_time = time.time() - start_time

        improvement = self.optimization_results['baseline']['startup_time'] - optimized_startup_time
        improvement_percent = (improvement / self.optimization_results['baseline']['startup_time']) * 100

        self.optimization_results['startup'] = {
            'optimized_time': optimized_startup_time,
            'improvement_seconds': improvement,
            'improvement_percent': improvement_percent,
            'optimizations': optimizations
        }

        print(f"   ✅ Optimized startup time: {optimized_startup_time:.2f}s")
        print(f"   ✅ Improvement: {improvement:.2f}s ({improvement_percent:.1f}%)")

    async def optimize_memory_usage(self):
        """Optimize memory usage"""
        print("   Implementing memory optimizations...")

        optimizations = []

        # 1. Cache size optimization
        await self.optimize_cache_sizes()
        optimizations.append("Optimized cache sizes")

        # 2. Garbage collection optimization
        await self.optimize_garbage_collection()
        optimizations.append("Improved garbage collection")

        # 3. Memory pool optimization
        await self.implement_memory_pools()
        optimizations.append("Memory pooling for frequent allocations")

        # 4. Lazy data loading
        await self.implement_lazy_data_loading()
        optimizations.append("Lazy loading for large data structures")

        # Measure memory usage after optimization
        process = psutil.Process()
        optimized_memory = process.memory_info().rss / 1024 / 1024  # MB

        improvement = self.optimization_results['baseline']['memory_usage_mb'] - optimized_memory
        improvement_percent = (improvement / self.optimization_results['baseline']['memory_usage_mb']) * 100

        self.optimization_results['memory'] = {
            'optimized_usage_mb': optimized_memory,
            'improvement_mb': improvement,
            'improvement_percent': improvement_percent,
            'optimizations': optimizations
        }

        print(f"   ✅ Optimized memory usage: {optimized_memory:.1f}MB")
        print(f"   ✅ Improvement: {improvement:.1f}MB ({improvement_percent:.1f}%)")

    async def optimize_ui_performance(self):
        """Optimize UI performance and animations"""
        print("   Implementing UI optimizations...")

        optimizations = []

        # 1. Rendering optimization
        await self.optimize_rendering()
        optimizations.append("Optimized rendering pipeline")

        # 2. Animation smoothing
        await self.optimize_animations()
        optimizations.append("Smooth animations with frame limiting")

        # 3. Layout caching
        await self.implement_layout_caching()
        optimizations.append("Layout caching for static elements")

        # 4. Event handling optimization
        await self.optimize_event_handling()
        optimizations.append("Optimized event handling and debouncing")

        self.optimization_results['ui'] = {
            'optimizations': optimizations
        }

        print(f"   ✅ Applied {len(optimizations)} UI optimizations")

    async def optimize_ai_performance(self):
        """Optimize AI response times and caching"""
        print("   Implementing AI optimizations...")

        optimizations = []

        # 1. Response caching
        await self.optimize_ai_caching()
        optimizations.append("Enhanced AI response caching")

        # 2. Model loading optimization
        await self.optimize_model_loading()
        optimizations.append("Optimized model loading and switching")

        # 3. Request batching
        await self.implement_request_batching()
        optimizations.append("Request batching for efficiency")

        # 4. Background processing
        await self.implement_ai_background_processing()
        optimizations.append("Background AI processing")

        self.optimization_results['ai'] = {
            'optimizations': optimizations
        }

        print(f"   ✅ Applied {len(optimizations)} AI optimizations")

    async def optimize_file_operations(self):
        """Optimize file operations for large directories"""
        print("   Implementing file operation optimizations...")

        optimizations = []

        # 1. Directory scanning optimization
        await self.optimize_directory_scanning()
        optimizations.append("Optimized directory scanning")

        # 2. File metadata caching
        await self.optimize_file_metadata_caching()
        optimizations.append("Enhanced file metadata caching")

        # 3. Parallel file processing
        await self.implement_parallel_file_processing()
        optimizations.append("Parallel file processing")

        # 4. Large file handling
        await self.optimize_large_file_handling()
        optimizations.append("Optimized large file handling")

        # Test file operation performance
        file_manager = self.integrator.get_component('file_manager')
        if file_manager:
            start_time = time.time()
            files = await file_manager.list_directory(Path.cwd())
            optimized_file_time = time.time() - start_time

            improvement = self.optimization_results['baseline']['file_listing_time'] - optimized_file_time
            improvement_percent = (improvement / self.optimization_results['baseline']['file_listing_time']) * 100

            self.optimization_results['file_ops'] = {
                'optimized_time': optimized_file_time,
                'improvement_seconds': improvement,
                'improvement_percent': improvement_percent,
                'optimizations': optimizations
            }

            print(f"   ✅ Optimized file listing: {optimized_file_time:.3f}s")
            print(f"   ✅ Improvement: {improvement:.3f}s ({improvement_percent:.1f}%)")
        else:
            self.optimization_results['file_ops'] = {'optimizations': optimizations}
            print(f"   ✅ Applied {len(optimizations)} file operation optimizations")

    # Implementation methods for each optimization category

    async def implement_lazy_loading(self):
        """Implement lazy loading for non-critical components"""
        # Create optimized startup configuration
        startup_config = {
            'lazy_loading': {
                'enabled': True,
                'defer_ai_initialization': True,
                'defer_plugin_loading': True,
                'defer_preview_system': False,  # Keep preview for UI responsiveness
                'defer_theme_loading': False    # Keep themes for immediate UI
            }
        }

        # Write optimization config
        config_path = Path('.kiro/optimization/startup.yaml')
        config_path.parent.mkdir(parents=True, exist_ok=True)

        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(startup_config, f)

    async def optimize_imports(self):
        """Optimize import statements for faster loading"""
        # Create import optimization recommendations
        import_optimizations = {
            'recommendations': [
                'Use lazy imports for heavy dependencies',
                'Import only needed functions, not entire modules',
                'Defer AI model imports until needed',
                'Use conditional imports for optional features'
            ],
            'heavy_imports': [
                'rich', 'psutil', 'PIL', 'pygments', 'asyncio'
            ]
        }

        config_path = Path('.kiro/optimization/imports.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(import_optimizations, f)

    async def implement_config_caching(self):
        """Implement configuration caching"""
        cache_config = {
            'config_cache': {
                'enabled': True,
                'cache_duration': 300,  # 5 minutes
                'cache_file': '.kiro/cache/config.cache',
                'invalidate_on_change': True
            }
        }

        config_path = Path('.kiro/optimization/caching.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(cache_config, f)

    async def optimize_component_initialization(self):
        """Optimize component initialization"""
        init_config = {
            'component_init': {
                'parallel_initialization': True,
                'max_concurrent_components': 3,
                'timeout_per_component': 10,
                'retry_failed_components': True
            }
        }

        config_path = Path('.kiro/optimization/initialization.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(init_config, f)

    async def optimize_cache_sizes(self):
        """Optimize cache sizes based on available memory"""
        system_memory = psutil.virtual_memory().total / 1024 / 1024 / 1024  # GB

        # Scale cache sizes based on available memory
        if system_memory >= 16:
            cache_multiplier = 2.0
        elif system_memory >= 8:
            cache_multiplier = 1.5
        else:
            cache_multiplier = 1.0

        cache_config = {
            'cache_optimization': {
                'file_cache_size': int(1000 * cache_multiplier),
                'directory_cache_size': int(500 * cache_multiplier),
                'preview_cache_size': int(250 * cache_multiplier),
                'ai_cache_size': int(100 * cache_multiplier),
                'memory_threshold_mb': int(system_memory * 1024 * 0.1)  # 10% of system memory
            }
        }

        config_path = Path('.kiro/optimization/cache_sizes.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(cache_config, f)

    async def optimize_garbage_collection(self):
        """Optimize garbage collection settings"""
        # Force garbage collection
        gc.collect()

        gc_config = {
            'garbage_collection': {
                'auto_gc_enabled': True,
                'gc_threshold_0': 700,
                'gc_threshold_1': 10,
                'gc_threshold_2': 10,
                'periodic_gc_interval': 60  # seconds
            }
        }

        config_path = Path('.kiro/optimization/garbage_collection.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(gc_config, f)

    async def implement_memory_pools(self):
        """Implement memory pooling"""
        pool_config = {
            'memory_pools': {
                'enabled': True,
                'file_info_pool_size': 1000,
                'string_pool_size': 5000,
                'path_pool_size': 500
            }
        }

        config_path = Path('.kiro/optimization/memory_pools.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(pool_config, f)

    async def implement_lazy_data_loading(self):
        """Implement lazy data loading"""
        lazy_config = {
            'lazy_loading': {
                'file_metadata': True,
                'directory_stats': True,
                'preview_generation': True,
                'ai_analysis': True
            }
        }

        config_path = Path('.kiro/optimization/lazy_loading.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(lazy_config, f)

    async def optimize_rendering(self):
        """Optimize UI rendering"""
        render_config = {
            'rendering': {
                'frame_rate_limit': 30,
                'vsync_enabled': True,
                'double_buffering': True,
                'render_caching': True,
                'partial_updates': True
            }
        }

        config_path = Path('.kiro/optimization/rendering.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(render_config, f)

    async def optimize_animations(self):
        """Optimize animations"""
        animation_config = {
            'animations': {
                'enabled': True,
                'smooth_scrolling': True,
                'transition_duration': 150,  # ms
                'easing_function': 'ease-out',
                'reduce_motion_respect': True
            }
        }

        config_path = Path('.kiro/optimization/animations.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(animation_config, f)

    async def implement_layout_caching(self):
        """Implement layout caching"""
        layout_config = {
            'layout_cache': {
                'enabled': True,
                'cache_static_elements': True,
                'cache_duration': 60,  # seconds
                'invalidate_on_resize': True
            }
        }

        config_path = Path('.kiro/optimization/layout_cache.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(layout_config, f)

    async def optimize_event_handling(self):
        """Optimize event handling"""
        event_config = {
            'event_handling': {
                'debounce_delay': 50,  # ms
                'throttle_navigation': True,
                'batch_updates': True,
                'async_event_processing': True
            }
        }

        config_path = Path('.kiro/optimization/event_handling.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(event_config, f)

    async def optimize_ai_caching(self):
        """Optimize AI caching"""
        ai_cache_config = {
            'ai_cache': {
                'response_cache_size': 500,
                'cache_ttl': 3600,  # 1 hour
                'cache_compression': True,
                'smart_cache_eviction': True
            }
        }

        config_path = Path('.kiro/optimization/ai_cache.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(ai_cache_config, f)

    async def optimize_model_loading(self):
        """Optimize AI model loading"""
        model_config = {
            'model_optimization': {
                'lazy_model_loading': True,
                'model_preloading': False,
                'model_switching_cache': True,
                'concurrent_model_requests': 2
            }
        }

        config_path = Path('.kiro/optimization/model_loading.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(model_config, f)

    async def implement_request_batching(self):
        """Implement AI request batching"""
        batch_config = {
            'request_batching': {
                'enabled': True,
                'batch_size': 5,
                'batch_timeout': 100,  # ms
                'priority_requests': ['security', 'error']
            }
        }

        config_path = Path('.kiro/optimization/request_batching.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(batch_config, f)

    async def implement_ai_background_processing(self):
        """Implement AI background processing"""
        bg_config = {
            'background_processing': {
                'enabled': True,
                'worker_threads': 2,
                'queue_size': 100,
                'priority_queue': True
            }
        }

        config_path = Path('.kiro/optimization/ai_background.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(bg_config, f)

    async def optimize_directory_scanning(self):
        """Optimize directory scanning"""
        scan_config = {
            'directory_scanning': {
                'parallel_scanning': True,
                'max_concurrent_scans': 4,
                'scan_depth_limit': 10,
                'ignore_patterns': ['.git', '__pycache__', 'node_modules']
            }
        }

        config_path = Path('.kiro/optimization/directory_scanning.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(scan_config, f)

    async def optimize_file_metadata_caching(self):
        """Optimize file metadata caching"""
        metadata_config = {
            'metadata_cache': {
                'enabled': True,
                'cache_size': 2000,
                'cache_ttl': 300,  # 5 minutes
                'background_refresh': True
            }
        }

        config_path = Path('.kiro/optimization/metadata_cache.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(metadata_config, f)

    async def implement_parallel_file_processing(self):
        """Implement parallel file processing"""
        parallel_config = {
            'parallel_processing': {
                'enabled': True,
                'max_workers': 4,
                'chunk_size': 100,
                'async_processing': True
            }
        }

        config_path = Path('.kiro/optimization/parallel_processing.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(parallel_config, f)

    async def optimize_large_file_handling(self):
        """Optimize large file handling"""
        large_file_config = {
            'large_files': {
                'streaming_threshold': 100 * 1024 * 1024,  # 100MB
                'chunk_size': 64 * 1024,  # 64KB
                'progress_reporting': True,
                'memory_mapping': True
            }
        }

        config_path = Path('.kiro/optimization/large_files.yaml')
        import yaml
        with open(config_path, 'w') as f:
            yaml.dump(large_file_config, f)

    async def generate_optimization_report(self):
        """Generate comprehensive optimization report"""
        report = {
            'optimization_summary': {
                'timestamp': time.strftime('%Y-%m-%d %H:%M:%S'),
                'total_optimizations': sum(len(result.get('optimizations', []))
                                         for result in self.optimization_results.values()),
                'baseline_performance': self.optimization_results.get('baseline', {}),
                'optimized_performance': {
                    'startup': self.optimization_results.get('startup', {}),
                    'memory': self.optimization_results.get('memory', {}),
                    'file_operations': self.optimization_results.get('file_ops', {})
                }
            },
            'detailed_results': self.optimization_results
        }

        # Write comprehensive report
        report_path = Path('.kiro/optimization/optimization_report.yaml')
        import yaml
        with open(report_path, 'w') as f:
            yaml.dump(report, f, default_flow_style=False)

        # Create human-readable summary
        summary_path = Path('.kiro/optimization/OPTIMIZATION_SUMMARY.md')
        with open(summary_path, 'w') as f:
            f.write("# LaxyFile Performance Optimization Summary\n\n")
            f.write(f"**Generated:** {report['optimization_summary']['timestamp']}\n\n")

            # Performance improvements
            if 'startup' in self.optimization_results:
                startup = self.optimization_results['startup']
                f.write(f"## Startup Performance\n")
                f.write(f"- **Before:** {self.optimization_results['baseline']['startup_time']:.2f}s\n")
                f.write(f"- **After:** {startup['optimized_time']:.2f}s\n")
                f.write(f"- **Improvement:** {startup['improvement_seconds']:.2f}s ({startup['improvement_percent']:.1f}%)\n\n")

            if 'memory' in self.optimization_results:
                memory = self.optimization_results['memory']
                f.write(f"## Memory Usage\n")
                f.write(f"- **Before:** {self.optimization_results['baseline']['memory_usage_mb']:.1f}MB\n")
                f.write(f"- **After:** {memory['optimized_usage_mb']:.1f}MB\n")
                f.write(f"- **Improvement:** {memory['improvement_mb']:.1f}MB ({memory['improvement_percent']:.1f}%)\n\n")

            if 'file_ops' in self.optimization_results and 'optimized_time' in self.optimization_results['file_ops']:
                file_ops = self.optimization_results['file_ops']
                f.write(f"## File Operations\n")
                f.write(f"- **Before:** {self.optimization_results['baseline']['file_listing_time']:.3f}s\n")
                f.write(f"- **After:** {file_ops['optimized_time']:.3f}s\n")
                f.write(f"- **Improvement:** {file_ops['improvement_seconds']:.3f}s ({file_ops['improvement_percent']:.1f}%)\n\n")

            # List all optimizations
            f.write("## Applied Optimizations\n\n")
            for category, results in self.optimization_results.items():
                if 'optimizations' in results and results['optimizations']:
                    f.write(f"### {category.title()}\n")
                    for opt in results['optimizations']:
                        f.write(f"- {opt}\n")
                    f.write("\n")

        print(f"   ✅ Generated optimization report: {report_path}")
        print(f"   ✅ Generated summary: {summary_path}")

    async def cleanup(self):
        """Cleanup resources"""
        if self.integrator:
            await self.integrator.shutdown()


async def main():
    """Main optimization function"""
    optimizer = LaxyFileOptimizer()

    try:
        await optimizer.run_optimization_suite()
    except KeyboardInterrupt:
        print("\n⚠️ Optimization interrupted by user")
    except Exception as e:
        print(f"\n❌ Optimization failed: {e}")
        import traceback
        traceback.print_exc()
    finally:
        await optimizer.cleanup()


if __name__ == "__main__":
    asyncio.run(main())