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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Lazy Loading Service for AI Agents
Optimizes memory usage and startup time by loading agents on-demand
"""

import asyncio
import importlib
import inspect
from typing import Dict, Type, Optional, Any, List, Callable
from datetime import datetime, timedelta
import weakref

from src.core import get_logger
from src.agents.deodoro import BaseAgent
from src.core.exceptions import AgentExecutionError

logger = get_logger(__name__)


class AgentMetadata:
    """Metadata for lazy-loaded agents."""
    
    def __init__(
        self,
        name: str,
        module_path: str,
        class_name: str,
        description: str,
        capabilities: List[str],
        priority: int = 0,
        preload: bool = False
    ):
        self.name = name
        self.module_path = module_path
        self.class_name = class_name
        self.description = description
        self.capabilities = capabilities
        self.priority = priority
        self.preload = preload
        self.loaded_class: Optional[Type[BaseAgent]] = None
        self.last_used: Optional[datetime] = None
        self.usage_count: int = 0
        self.load_time_ms: Optional[float] = None


class AgentLazyLoader:
    """
    Lazy loading manager for AI agents.
    
    Features:
    - On-demand agent loading
    - Memory-efficient agent management
    - Automatic unloading of unused agents
    - Preloading for critical agents
    - Usage tracking and statistics
    """
    
    def __init__(
        self,
        unload_after_minutes: int = 15,
        max_loaded_agents: int = 10
    ):
        """
        Initialize lazy loader.
        
        Args:
            unload_after_minutes: Minutes of inactivity before unloading
            max_loaded_agents: Maximum number of loaded agents in memory
        """
        self.unload_after_minutes = unload_after_minutes
        self.max_loaded_agents = max_loaded_agents
        
        # Agent registry
        self._registry: Dict[str, AgentMetadata] = {}
        
        # Weak references to track agent instances
        self._instances: weakref.WeakValueDictionary = weakref.WeakValueDictionary()
        
        # Loading statistics
        self._stats = {
            "total_loads": 0,
            "cache_hits": 0,
            "cache_misses": 0,
            "total_unloads": 0,
            "avg_load_time_ms": 0.0
        }
        
        # Cleanup task
        self._cleanup_task: Optional[asyncio.Task] = None
        self._running = False
        
        # Initialize with default agents
        self._register_default_agents()
    
    def _register_default_agents(self):
        """Register all available agents."""
        # Core agents - high priority, preload
        self.register_agent(
            name="Zumbi",
            module_path="src.agents.zumbi",
            class_name="ZumbiAgent",
            description="Anomaly detection investigator",
            capabilities=["anomaly_detection", "fraud_analysis", "pattern_recognition"],
            priority=10,
            preload=True
        )
        
        self.register_agent(
            name="Anita",
            module_path="src.agents.anita",
            class_name="AnitaAgent",
            description="Pattern analysis specialist",
            capabilities=["pattern_analysis", "trend_detection", "correlation_analysis"],
            priority=10,
            preload=True
        )
        
        self.register_agent(
            name="Tiradentes",
            module_path="src.agents.tiradentes",
            class_name="TiradentesAgent",
            description="Natural language report generation",
            capabilities=["report_generation", "summarization", "natural_language"],
            priority=10,
            preload=True
        )
        
        # Extended agents - lower priority, lazy load
        self.register_agent(
            name="MariaCurie",
            module_path="src.agents.legacy.mariacurie",
            class_name="MariaCurieAgent",
            description="Scientific research specialist",
            capabilities=["research", "data_analysis", "methodology"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Drummond",
            module_path="src.agents.legacy.drummond",
            class_name="DrummondAgent",
            description="Communication and writing specialist",
            capabilities=["writing", "communication", "poetry"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="JoseBonifacio",
            module_path="src.agents.bonifacio",
            class_name="BonifacioAgent",
            description="Policy and governance analyst",
            capabilities=["policy_analysis", "governance", "regulation"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="MariaQuiteria",
            module_path="src.agents.maria_quiteria",
            class_name="MariaQuiteriaAgent",
            description="Security auditor and system integrity guardian",
            capabilities=["security_audit", "threat_detection", "compliance"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Dandara",
            module_path="src.agents.dandara",
            class_name="DandaraAgent",
            description="Social justice and equity monitoring specialist",
            capabilities=["social_equity", "inclusion_policies", "justice_analysis"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Machado",
            module_path="src.agents.machado",
            class_name="MachadoAgent",
            description="Government document analysis and text processing",
            capabilities=["text_analysis", "document_processing", "nlp"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Obaluaie",
            module_path="src.agents.obaluaie",
            class_name="CorruptionDetectorAgent",
            description="Corruption detection and systemic pattern analysis",
            capabilities=["corruption_detection", "fraud_detection", "systemic_analysis"],
            priority=8,
            preload=False
        )
        
        self.register_agent(
            name="OscarNiemeyer",
            module_path="src.agents.oscar_niemeyer",
            class_name="OscarNiemeyerAgent",
            description="Data aggregation and visualization metadata specialist",
            capabilities=["data_aggregation", "visualization", "multidimensional_analysis"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Lampiao",
            module_path="src.agents.lampiao",
            class_name="LampiaoAgent",
            description="Regional analysis and geographic data specialist",
            capabilities=["regional_analysis", "spatial_statistics", "inequality_measurement"],
            priority=5,
            preload=False
        )
        
        self.register_agent(
            name="Oxossi",
            module_path="src.agents.oxossi",
            class_name="OxossiAgent",
            description="Fraud detection and tracking specialist with precision hunting capabilities",
            capabilities=["fraud_detection", "pattern_recognition", "financial_forensics", "evidence_tracking"],
            priority=9,
            preload=False
        )
    
    def register_agent(
        self,
        name: str,
        module_path: str,
        class_name: str,
        description: str,
        capabilities: List[str],
        priority: int = 0,
        preload: bool = False
    ):
        """Register an agent for lazy loading."""
        metadata = AgentMetadata(
            name=name,
            module_path=module_path,
            class_name=class_name,
            description=description,
            capabilities=capabilities,
            priority=priority,
            preload=preload
        )
        
        self._registry[name] = metadata
        logger.info(
            f"Registered agent {name}",
            module=module_path,
            class_name=class_name,
            preload=preload
        )
    
    async def start(self):
        """Start the lazy loader and preload critical agents."""
        self._running = True
        self._cleanup_task = asyncio.create_task(self._cleanup_loop())
        
        # Preload high-priority agents
        await self._preload_agents()
        
        logger.info("Agent lazy loader started")
    
    async def stop(self):
        """Stop the lazy loader and cleanup resources."""
        self._running = False
        
        if self._cleanup_task:
            self._cleanup_task.cancel()
            try:
                await self._cleanup_task
            except asyncio.CancelledError:
                pass
        
        # Cleanup all loaded agents
        for metadata in self._registry.values():
            if metadata.loaded_class:
                await self._unload_agent(metadata)
        
        logger.info("Agent lazy loader stopped")
    
    async def _preload_agents(self):
        """Preload high-priority agents."""
        preload_agents = [
            (metadata.priority, name, metadata)
            for name, metadata in self._registry.items()
            if metadata.preload
        ]
        
        # Sort by priority (descending)
        preload_agents.sort(key=lambda x: x[0], reverse=True)
        
        for _, name, metadata in preload_agents:
            try:
                await self._load_agent(metadata)
                logger.info(f"Preloaded agent {name}")
            except Exception as e:
                logger.error(f"Failed to preload agent {name}: {e}")
    
    async def get_agent_class(self, name: str) -> Type[BaseAgent]:
        """
        Get agent class, loading if necessary.
        
        Args:
            name: Agent name
            
        Returns:
            Agent class
            
        Raises:
            AgentExecutionError: If agent not found or load fails
        """
        if name not in self._registry:
            raise AgentExecutionError(
                f"Agent '{name}' not registered",
                details={"available_agents": list(self._registry.keys())}
            )
        
        metadata = self._registry[name]
        
        # Return cached class if available
        if metadata.loaded_class:
            metadata.last_used = datetime.now()
            metadata.usage_count += 1
            self._stats["cache_hits"] += 1
            return metadata.loaded_class
        
        # Load agent class
        self._stats["cache_misses"] += 1
        await self._load_agent(metadata)
        
        # Check if we need to unload other agents
        await self._check_memory_pressure()
        
        return metadata.loaded_class
    
    async def create_agent(self, name: str, **kwargs) -> BaseAgent:
        """
        Create an agent instance.
        
        Args:
            name: Agent name
            **kwargs: Agent initialization arguments
            
        Returns:
            Agent instance
        """
        agent_class = await self.get_agent_class(name)
        
        # Create instance
        instance = agent_class(**kwargs)
        
        # Track instance
        self._instances[f"{name}_{id(instance)}"] = instance
        
        # Initialize if needed
        if hasattr(instance, 'initialize'):
            await instance.initialize()
        
        return instance
    
    async def _load_agent(self, metadata: AgentMetadata):
        """Load an agent class."""
        start_time = asyncio.get_event_loop().time()
        
        try:
            # Import module
            module = importlib.import_module(metadata.module_path)
            
            # Get class
            agent_class = getattr(module, metadata.class_name)
            
            # Verify it's a valid agent
            if not issubclass(agent_class, BaseAgent):
                raise ValueError(f"{metadata.class_name} is not a BaseAgent subclass")
            
            metadata.loaded_class = agent_class
            metadata.last_used = datetime.now()
            metadata.usage_count += 1
            
            # Calculate load time
            load_time = (asyncio.get_event_loop().time() - start_time) * 1000
            metadata.load_time_ms = load_time
            
            # Update statistics
            self._stats["total_loads"] += 1
            total_loads = self._stats["total_loads"]
            avg_load_time = self._stats["avg_load_time_ms"]
            self._stats["avg_load_time_ms"] = (
                (avg_load_time * (total_loads - 1) + load_time) / total_loads
            )
            
            logger.info(
                f"Loaded agent {metadata.name}",
                load_time_ms=round(load_time, 2),
                module=metadata.module_path
            )
            
        except Exception as e:
            logger.error(
                f"Failed to load agent {metadata.name}",
                error=str(e),
                module=metadata.module_path
            )
            raise AgentExecutionError(
                f"Failed to load agent '{metadata.name}'",
                details={"error": str(e), "module": metadata.module_path}
            )
    
    async def _unload_agent(self, metadata: AgentMetadata):
        """Unload an agent from memory."""
        if not metadata.loaded_class:
            return
        
        # Check if any instances are still active
        active_instances = [
            key for key in self._instances
            if key.startswith(f"{metadata.name}_")
        ]
        
        if active_instances:
            logger.debug(
                f"Cannot unload agent {metadata.name}, {len(active_instances)} instances active"
            )
            return
        
        # Unload the module
        try:
            # Remove class reference
            metadata.loaded_class = None
            
            # Try to remove from sys.modules
            import sys
            if metadata.module_path in sys.modules:
                del sys.modules[metadata.module_path]
            
            self._stats["total_unloads"] += 1
            
            logger.info(f"Unloaded agent {metadata.name}")
            
        except Exception as e:
            logger.error(f"Error unloading agent {metadata.name}: {e}")
    
    async def _check_memory_pressure(self):
        """Check if we need to unload agents to free memory."""
        loaded_agents = [
            (metadata.last_used, metadata)
            for metadata in self._registry.values()
            if metadata.loaded_class
        ]
        
        # If under limit, no action needed
        if len(loaded_agents) <= self.max_loaded_agents:
            return
        
        # Sort by last used (oldest first)
        loaded_agents.sort(key=lambda x: x[0] or datetime.min)
        
        # Unload oldest agents
        to_unload = len(loaded_agents) - self.max_loaded_agents
        for _, metadata in loaded_agents[:to_unload]:
            # Skip preloaded agents
            if metadata.preload:
                continue
            
            await self._unload_agent(metadata)
    
    async def _cleanup_loop(self):
        """Background task to unload unused agents."""
        while self._running:
            try:
                await asyncio.sleep(60)  # Check every minute
                await self._cleanup_unused_agents()
            except asyncio.CancelledError:
                break
            except Exception as e:
                logger.error(f"Error in cleanup loop: {e}")
    
    async def _cleanup_unused_agents(self):
        """Unload agents that haven't been used recently."""
        cutoff_time = datetime.now() - timedelta(minutes=self.unload_after_minutes)
        
        for metadata in self._registry.values():
            # Skip if not loaded or preloaded
            if not metadata.loaded_class or metadata.preload:
                continue
            
            # Check last used time
            if metadata.last_used and metadata.last_used < cutoff_time:
                await self._unload_agent(metadata)
    
    def get_available_agents(self) -> List[Dict[str, Any]]:
        """Get list of available agents."""
        agents = []
        
        for name, metadata in self._registry.items():
            agents.append({
                "name": name,
                "description": metadata.description,
                "capabilities": metadata.capabilities,
                "loaded": metadata.loaded_class is not None,
                "usage_count": metadata.usage_count,
                "last_used": metadata.last_used.isoformat() if metadata.last_used else None,
                "load_time_ms": metadata.load_time_ms,
                "priority": metadata.priority,
                "preload": metadata.preload
            })
        
        # Sort by priority and usage
        agents.sort(key=lambda x: (-x["priority"], -x["usage_count"]))
        
        return agents
    
    def get_stats(self) -> Dict[str, Any]:
        """Get lazy loader statistics."""
        loaded_count = sum(
            1 for m in self._registry.values()
            if m.loaded_class
        )
        
        return {
            "total_agents": len(self._registry),
            "loaded_agents": loaded_count,
            "active_instances": len(self._instances),
            "statistics": self._stats,
            "memory_usage": {
                "max_loaded_agents": self.max_loaded_agents,
                "unload_after_minutes": self.unload_after_minutes
            }
        }


# Global lazy loader instance
agent_lazy_loader = AgentLazyLoader()


async def get_agent_lazy_loader() -> AgentLazyLoader:
    """Get the global agent lazy loader instance."""
    if not agent_lazy_loader._running:
        await agent_lazy_loader.start()
    return agent_lazy_loader