@@ -262,6 +262,15 @@ BRAVE_API_KEY=sk-brave-...
262262# Optional: cache TTL (days), summarization model
263263EDU_CACHE_TTL_DAYS=7
264264EDU_SUMMARY_MODEL=google/gemini-2.5-flash
265+
266+ # V9 Automatic Cleanup Configuration
267+ # Cleanup delay in seconds (default: 600 = 10 minutes)
268+ # Allows time for users to download reports before cleanup
269+ V9_CLEANUP_DELAY=600
270+
271+ # Enable/disable automatic cleanup (default: true)
272+ # Set to false to disable cleanup in development
273+ V9_CLEANUP_ENABLED=true
265274```
266275
267276Expected Impact
@@ -582,6 +591,172 @@ await orchestrator.orchestrate(repoPath, 'pr');
582591- ** Quick Reference** : ` src/two-branch/docs/dependency_check/QUICK_REFERENCE.md `
583592- ** Test Script** : ` test-dependency-check-fix.ts `
584593
594+ ## 🧹 V9 Automatic Cleanup Service (October 2025)
595+
596+ ### ✅ PRODUCTION READY: Automated Cleanup After Analysis
597+
598+ ** Status:** Integrated into V9IntegratedAnalyzer - Automatic cleanup with configurable delay - ** PRODUCTION READY**
599+
600+ ** What It Does:**
601+ The V9CleanupService automatically cleans up all analysis artifacts after report delivery, ensuring no data persists unnecessarily on servers.
602+
603+ ### How It Works
604+
605+ 1 . ** Integrated into V9IntegratedAnalyzer** : Cleanup service is initialized in the V9 engine constructor
606+ 2 . ** Non-Blocking Background Execution** : Uses ` scheduleCleanup() ` for delayed cleanup that doesn't block API responses
607+ 3 . ** Configurable Delay** : Default 10-minute delay allows users time to download reports and files
608+ 4 . ** Comprehensive Cleanup** : Removes repositories, reports, IDE fix files, and temp directories
609+
610+ ### Configuration (via Environment Variables)
611+
612+ ** Production defaults (optimized for cloud deployment):**
613+
614+ ``` bash
615+ # Cleanup delay in seconds (default: 600 = 10 minutes)
616+ # Allows time for users to download reports before cleanup
617+ V9_CLEANUP_DELAY=600
618+
619+ # Enable/disable automatic cleanup (default: true)
620+ # Set to false to disable cleanup in development
621+ V9_CLEANUP_ENABLED=true
622+ ```
623+
624+ ** For different deployment scenarios:**
625+
626+ ``` bash
627+ # Development (keep files longer for debugging)
628+ V9_CLEANUP_DELAY=3600 # 1 hour
629+ V9_CLEANUP_ENABLED=false # Disable for manual cleanup
630+
631+ # Staging (moderate delay)
632+ V9_CLEANUP_DELAY=900 # 15 minutes
633+ V9_CLEANUP_ENABLED=true
634+
635+ # Production (recommended)
636+ V9_CLEANUP_DELAY=600 # 10 minutes (optimal balance)
637+ V9_CLEANUP_ENABLED=true
638+ ```
639+
640+ ### What Gets Cleaned Up
641+
642+ - ** Repository Files** : Cloned git repositories (via V9RepositoryManager)
643+ - ** Report Artifacts** : Markdown reports (` .md ` ), JSON manifests (` .json ` )
644+ - ** Temp Directories** : Temporary working directories created during analysis
645+ - ** Redis Cache** : Already cleared immediately (not part of delayed cleanup)
646+
647+ ** Note:** CodeQual uses a manifest-based approach (not direct IDE integration). Users download the manifest file, open their preferred IDE, and apply fixes manually. The manifest contains issue locations and metadata for guidance.
648+
649+ ### Integration Points
650+
651+ ** File:** ` packages/agents/src/two-branch/analyzers/v9-integrated-analyzer.ts `
652+
653+ ``` typescript
654+ // Cleanup service initialized in constructor (lines 82-95)
655+ this .cleanupService = new V9CleanupService ({
656+ cleanupAfterDelivery: cleanupEnabled ,
657+ cleanupDelaySeconds: cleanupDelay ,
658+ keepSuccessfulReports: false ,
659+ maxReportAge: 3600
660+ });
661+
662+ // Cleanup scheduled in finally block (lines 219-229)
663+ this .cleanupService .scheduleCleanup (workspace , {
664+ repository: ` /tmp/v9-repos/${workspace } ` ,
665+ outputDir: process .cwd () + ` /test-outputs/${workspace } ` ,
666+ tempDir: ` /tmp/v9-temp-${workspace } `
667+ });
668+ ```
669+
670+ ### Benefits
671+
672+ - ✅ ** No Manual Intervention** : Cleanup happens automatically
673+ - ✅ ** Non-Blocking** : API returns immediately, cleanup runs in background
674+ - ✅ ** User-Friendly** : 10-minute delay allows downloads before cleanup
675+ - ✅ ** Configurable** : Easy to adjust delay and enable/disable per environment
676+ - ✅ ** Comprehensive** : Cleans all analysis artifacts, not just reports
677+ - ✅ ** Production Safe** : Validated paths, safe deletion with proper checks
678+
679+ ### Common Configurations
680+
681+ | Environment | Delay | Enabled | Use Case |
682+ | ------------| -------| ---------| ----------|
683+ | Development | 3600s (1h) | false | Manual debugging, keep all files |
684+ | Staging | 900s (15m) | true | Testing with moderate cleanup |
685+ | Production | 600s (10m) | true | ** RECOMMENDED** - Optimal balance |
686+ | CI/CD | 300s (5m) | true | Fast cleanup for automated tests |
687+
688+ ### Documentation
689+
690+ - ** Service Implementation** : ` src/two-branch/services/v9-cleanup-service.ts `
691+ - ** Integration Point** : ` src/two-branch/analyzers/v9-integrated-analyzer.ts ` (lines 82-95, 219-231)
692+ - ** Environment Template** : See "Environment Flags" section above
693+
694+ ### ⚠️ High-Scale Considerations (1000+ Concurrent Analyses)
695+
696+ ** Current Architecture (setTimeout-based):**
697+ - ✅ Suitable for: 1-100 concurrent analyses
698+ - ✅ Zero infrastructure overhead
699+ - ⚠️ Memory considerations at very high scale
700+
701+ ** For Production Scale > 1000 concurrent:**
702+
703+ If you anticipate more than 1000 concurrent analyses, consider upgrading to a queue-based system:
704+
705+ ** Option 1: Job Queue (Recommended)**
706+ ``` typescript
707+ // Use Bull, BullMQ, or similar
708+ import Bull from ' bull' ;
709+
710+ const cleanupQueue = new Bull (' v9-cleanup' , process .env .REDIS_URL );
711+
712+ // In finally block:
713+ cleanupQueue .add (' cleanup' , { workspace }, {
714+ delay: cleanupDelay * 1000 ,
715+ removeOnComplete: true
716+ });
717+
718+ // Processor (separate worker):
719+ cleanupQueue .process (' cleanup' , async (job ) => {
720+ await cleanupService .cleanupAfterReport (job .data .workspace , targets );
721+ });
722+ ```
723+
724+ ** Benefits:**
725+ - ✅ Persistent across restarts
726+ - ✅ Better memory management
727+ - ✅ Monitoring and retry logic
728+ - ✅ Distributed processing
729+
730+ ** Option 2: Cron-Based Cleanup**
731+ ``` typescript
732+ // Periodic sweep of old directories
733+ import cron from ' node-cron' ;
734+
735+ cron .schedule (' */5 * * * *' , async () => {
736+ const oldWorkspaces = await findWorkspacesOlderThan (cleanupDelay );
737+ for (const workspace of oldWorkspaces ) {
738+ await cleanupService .cleanupAfterReport (workspace , targets );
739+ }
740+ });
741+ ```
742+
743+ ** Option 3: External Cleanup Service**
744+ ``` typescript
745+ // Separate microservice for cleanup
746+ // API call instead of local setTimeout
747+ await fetch (' http://cleanup-service/schedule' , {
748+ method: ' POST' ,
749+ body: JSON .stringify ({ workspace , delay: cleanupDelay })
750+ });
751+ ```
752+
753+ ** When to upgrade:**
754+ - Monitor memory usage with 100+ concurrent analyses
755+ - If Node.js process grows > 2GB, consider queue-based cleanup
756+ - If timers exceed 10,000 active, migrate to queue system
757+
758+ ** Note:** For most deployments (< 1000 concurrent), the current setTimeout-based approach is optimal.
759+
585760## 📁 V9 System Architecture
586761
587762### Core Files You Must Know
0 commit comments