Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /** * V3 Lifecycle Manager * Decomposed from orchestrator.ts - Agent spawn/terminate * ~150 lines (target achieved) */ import type { IAgent, IAgentConfig, IAgentLifecycleManager, IAgentPool, AgentStatus, } from '../interfaces/agent.interface.js'; import type { IEventBus } from '../interfaces/event.interface.js'; import { SystemEventTypes } from '../interfaces/event.interface.js'; /** * Agent pool implementation */ export class AgentPool implements IAgentPool { private agents = new Map<string, IAgent>(); add(agent: IAgent): void { this.agents.set(agent.id, agent); } remove(agentId: string): boolean { return this.agents.delete(agentId); } get(agentId: string): IAgent | undefined { return this.agents.get(agentId); } getAll(): IAgent[] { return Array.from(this.agents.values()); } getByStatus(status: AgentStatus): IAgent[] { return this.getAll().filter(agent => agent.status === status); } getByType(type: string): IAgent[] { return this.getAll().filter(agent => agent.type === type); } getAvailable(): IAgent[] { return this.getAll().filter( agent => (agent.status === 'active' || agent.status === 'idle') && agent.currentTaskCount < agent.config.maxConcurrentTasks, ); } size(): number { return this.agents.size; } hasCapacity(maxSize: number): boolean { return this.agents.size < maxSize; } clear(): void { this.agents.clear(); } } /** * Lifecycle manager configuration */ export interface LifecycleManagerConfig { maxConcurrentAgents: number; spawnTimeout: number; terminateTimeout: number; maxSpawnRetries: number; } /** * Lifecycle manager implementation */ export class LifecycleManager implements IAgentLifecycleManager { private pool: IAgentPool; constructor( private eventBus: IEventBus, private config: LifecycleManagerConfig, pool?: IAgentPool, ) { this.pool = pool ?? new AgentPool(); } async spawn(config: IAgentConfig): Promise<IAgent> { // Validate capacity if (!this.pool.hasCapacity(this.config.maxConcurrentAgents)) { throw new Error('Maximum concurrent agents reached'); } // Validate agent doesn't already exist if (this.pool.get(config.id)) { throw new Error(`Agent with ID ${config.id} already exists`); } const agent: IAgent = { id: config.id, name: config.name, type: config.type, config, createdAt: new Date(), status: 'spawning', currentTaskCount: 0, lastActivity: new Date(), metrics: { tasksCompleted: 0, tasksFailed: 0, avgTaskDuration: 0, errorCount: 0, uptime: 0, }, }; // Add to pool this.pool.add(agent); // Mark as active agent.status = 'active'; this.eventBus.emit(SystemEventTypes.AGENT_SPAWNED, { agentId: agent.id, profile: config, sessionId: undefined, }); return agent; } async spawnBatch(configs: IAgentConfig[]): Promise<Map<string, IAgent>> { const results = new Map<string, IAgent>(); // Check total capacity if (this.pool.size() + configs.length > this.config.maxConcurrentAgents) { throw new Error('Batch would exceed maximum concurrent agents'); } // Spawn in parallel const spawnPromises = configs.map(async config => { try { const agent = await this.spawn(config); return { id: config.id, agent, error: null }; } catch (error) { return { id: config.id, agent: null, error }; } }); const settled = await Promise.allSettled(spawnPromises); for (const result of settled) { if (result.status === 'fulfilled' && result.value.agent) { results.set(result.value.id, result.value.agent); } } return results; } async terminate(agentId: string, reason?: string): Promise<void> { const agent = this.pool.get(agentId); if (!agent) { throw new Error(`Agent not found: ${agentId}`); } agent.status = 'terminated'; // Remove from pool this.pool.remove(agentId); this.eventBus.emit(SystemEventTypes.AGENT_TERMINATED, { agentId, reason: reason ?? 'User requested', }); } async terminateAll(reason?: string): Promise<void> { const agents = this.pool.getAll(); await Promise.allSettled( agents.map(agent => this.terminate(agent.id, reason)), ); } async restart(agentId: string): Promise<IAgent> { const agent = this.pool.get(agentId); if (!agent) { throw new Error(`Agent not found: ${agentId}`); } const config = agent.config; await this.terminate(agentId, 'Restart requested'); return this.spawn(config); } async updateConfig(agentId: string, config: Partial<IAgentConfig>): Promise<void> { const agent = this.pool.get(agentId); if (!agent) { throw new Error(`Agent not found: ${agentId}`); } Object.assign(agent.config, config); } getAgent(agentId: string): IAgent | undefined { return this.pool.get(agentId); } getAllAgents(): IAgent[] { return this.pool.getAll(); } getActiveCount(): number { return this.pool.getByStatus('active').length + this.pool.getByStatus('idle').length; } async checkHealth(agentId: string): Promise<IAgent['health']> { const agent = this.pool.get(agentId); if (!agent) { throw new Error(`Agent not found: ${agentId}`); } // Simple health check based on metrics const errorRate = agent.metrics ? agent.metrics.errorCount / Math.max(1, agent.metrics.tasksCompleted + agent.metrics.tasksFailed) : 0; const health: IAgent['health'] = { status: errorRate > 0.5 ? 'unhealthy' : errorRate > 0.2 ? 'degraded' : 'healthy', lastCheck: new Date(), issues: [], }; if (errorRate > 0.2) { health.issues?.push(`High error rate: ${(errorRate * 100).toFixed(1)}%`); } agent.health = health; if (health.status !== 'healthy') { this.eventBus.emit(SystemEventTypes.AGENT_HEALTH_CHANGED, { agentId, previousStatus: agent.status, currentStatus: agent.status, issues: health.issues, }); } return health; } /** * Get agent pool */ getPool(): IAgentPool { return this.pool; } } |