@baselineos/autonomy 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +14 -0
- package/.turbo/turbo-test.log +14 -0
- package/LICENSE +17 -0
- package/README.md +19 -0
- package/dist/index.d.ts +539 -0
- package/dist/index.js +2540 -0
- package/package.json +34 -0
- package/src/__tests__/smoke.test.ts +24 -0
- package/src/index.ts +62 -0
- package/src/integration.ts +600 -0
- package/src/lifecycle.ts +1323 -0
- package/src/mastra-engine.ts +1097 -0
- package/src/system.ts +654 -0
- package/tsconfig.json +9 -0
package/src/lifecycle.ts
ADDED
|
@@ -0,0 +1,1323 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Types & Interfaces
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
export interface LifecycleStage {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
order: number;
|
|
13
|
+
required: boolean;
|
|
14
|
+
autoTransition: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface StageHistoryEntry {
|
|
18
|
+
stage: string;
|
|
19
|
+
timestamp: number;
|
|
20
|
+
status: string;
|
|
21
|
+
error?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AgentDeployment {
|
|
25
|
+
agentId: string;
|
|
26
|
+
agentName: string;
|
|
27
|
+
currentStage: string;
|
|
28
|
+
stageHistory: StageHistoryEntry[];
|
|
29
|
+
startTime: number;
|
|
30
|
+
status: string;
|
|
31
|
+
metrics: {
|
|
32
|
+
stageTransitions: number;
|
|
33
|
+
totalTime: number;
|
|
34
|
+
errors: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ScalingPolicy {
|
|
39
|
+
id: string;
|
|
40
|
+
name: string;
|
|
41
|
+
description: string;
|
|
42
|
+
triggers: string[];
|
|
43
|
+
actions: string[];
|
|
44
|
+
thresholds: Record<string, number>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RetirementPolicy {
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
description: string;
|
|
51
|
+
criteria: string[];
|
|
52
|
+
thresholds: Record<string, number>;
|
|
53
|
+
gracePeriod: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface HealthMonitor {
|
|
57
|
+
id: string;
|
|
58
|
+
name: string;
|
|
59
|
+
metrics: string[];
|
|
60
|
+
interval: number;
|
|
61
|
+
thresholds: Record<string, number | string>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ThresholdViolation {
|
|
65
|
+
metric: string;
|
|
66
|
+
value: number;
|
|
67
|
+
threshold: number | string;
|
|
68
|
+
severity: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface LifecycleStatus {
|
|
72
|
+
agentId: string;
|
|
73
|
+
agentName: string;
|
|
74
|
+
currentStage: string;
|
|
75
|
+
status: string;
|
|
76
|
+
stageHistory: StageHistoryEntry[];
|
|
77
|
+
metrics: AgentDeployment['metrics'];
|
|
78
|
+
uptime: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface Alert {
|
|
82
|
+
id: string;
|
|
83
|
+
type: string;
|
|
84
|
+
severity: string;
|
|
85
|
+
message: string;
|
|
86
|
+
agentId: string;
|
|
87
|
+
timestamp: number;
|
|
88
|
+
violations: ThresholdViolation[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// AgentLifecycleManager
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
export class AgentLifecycleManager extends EventEmitter {
|
|
96
|
+
private autonomySystem: unknown;
|
|
97
|
+
private lifecycleStages: Map<string, LifecycleStage>;
|
|
98
|
+
private agentDeployments: Map<string, AgentDeployment>;
|
|
99
|
+
private scalingPolicies: Map<string, ScalingPolicy>;
|
|
100
|
+
private retirementPolicies: Map<string, RetirementPolicy>;
|
|
101
|
+
private healthMonitors: Map<string, HealthMonitor>;
|
|
102
|
+
|
|
103
|
+
constructor(autonomySystem: unknown) {
|
|
104
|
+
super();
|
|
105
|
+
this.autonomySystem = autonomySystem;
|
|
106
|
+
this.lifecycleStages = new Map();
|
|
107
|
+
this.agentDeployments = new Map();
|
|
108
|
+
this.scalingPolicies = new Map();
|
|
109
|
+
this.retirementPolicies = new Map();
|
|
110
|
+
this.healthMonitors = new Map();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// -----------------------------------------------------------------------
|
|
114
|
+
// Initialization
|
|
115
|
+
// -----------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
async initialize(): Promise<void> {
|
|
118
|
+
console.log('[AgentLifecycleManager] Initializing agent lifecycle management...');
|
|
119
|
+
|
|
120
|
+
this.initializeLifecycleStages();
|
|
121
|
+
this.initializeScalingPolicies();
|
|
122
|
+
this.initializeRetirementPolicies();
|
|
123
|
+
this.initializeHealthMonitoring();
|
|
124
|
+
|
|
125
|
+
console.log('[AgentLifecycleManager] Lifecycle management initialized');
|
|
126
|
+
console.log(`[AgentLifecycleManager] Lifecycle stages: ${this.lifecycleStages.size}`);
|
|
127
|
+
console.log(`[AgentLifecycleManager] Scaling policies: ${this.scalingPolicies.size}`);
|
|
128
|
+
console.log(`[AgentLifecycleManager] Retirement policies: ${this.retirementPolicies.size}`);
|
|
129
|
+
console.log(`[AgentLifecycleManager] Health monitors: ${this.healthMonitors.size}`);
|
|
130
|
+
|
|
131
|
+
this.emit('initialized', {
|
|
132
|
+
stages: this.lifecycleStages.size,
|
|
133
|
+
scalingPolicies: this.scalingPolicies.size,
|
|
134
|
+
retirementPolicies: this.retirementPolicies.size,
|
|
135
|
+
healthMonitors: this.healthMonitors.size,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// -----------------------------------------------------------------------
|
|
140
|
+
// Lifecycle Stages (8)
|
|
141
|
+
// -----------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
private initializeLifecycleStages(): void {
|
|
144
|
+
const stages: LifecycleStage[] = [
|
|
145
|
+
{
|
|
146
|
+
id: 'creation',
|
|
147
|
+
name: 'Creation',
|
|
148
|
+
description: 'Agent is being created and configured',
|
|
149
|
+
order: 1,
|
|
150
|
+
required: true,
|
|
151
|
+
autoTransition: true,
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
id: 'validation',
|
|
155
|
+
name: 'Validation',
|
|
156
|
+
description: 'Agent configuration and capabilities are validated',
|
|
157
|
+
order: 2,
|
|
158
|
+
required: true,
|
|
159
|
+
autoTransition: true,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
id: 'deployment',
|
|
163
|
+
name: 'Deployment',
|
|
164
|
+
description: 'Agent is deployed to the target environment',
|
|
165
|
+
order: 3,
|
|
166
|
+
required: true,
|
|
167
|
+
autoTransition: true,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
id: 'activation',
|
|
171
|
+
name: 'Activation',
|
|
172
|
+
description: 'Agent is activated and begins operation',
|
|
173
|
+
order: 4,
|
|
174
|
+
required: true,
|
|
175
|
+
autoTransition: true,
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'monitoring',
|
|
179
|
+
name: 'Monitoring',
|
|
180
|
+
description: 'Agent is actively monitored during operation',
|
|
181
|
+
order: 5,
|
|
182
|
+
required: true,
|
|
183
|
+
autoTransition: false,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
id: 'scaling',
|
|
187
|
+
name: 'Scaling',
|
|
188
|
+
description: 'Agent resources are scaled based on demand',
|
|
189
|
+
order: 6,
|
|
190
|
+
required: false,
|
|
191
|
+
autoTransition: false,
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
id: 'maintenance',
|
|
195
|
+
name: 'Maintenance',
|
|
196
|
+
description: 'Agent undergoes maintenance and updates',
|
|
197
|
+
order: 7,
|
|
198
|
+
required: false,
|
|
199
|
+
autoTransition: false,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'retirement',
|
|
203
|
+
name: 'Retirement',
|
|
204
|
+
description: 'Agent is gracefully retired from service',
|
|
205
|
+
order: 8,
|
|
206
|
+
required: false,
|
|
207
|
+
autoTransition: false,
|
|
208
|
+
},
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
for (const stage of stages) {
|
|
212
|
+
this.lifecycleStages.set(stage.id, stage);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// -----------------------------------------------------------------------
|
|
217
|
+
// Scaling Policies (3)
|
|
218
|
+
// -----------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
private initializeScalingPolicies(): void {
|
|
221
|
+
const policies: ScalingPolicy[] = [
|
|
222
|
+
{
|
|
223
|
+
id: 'cpu-scaling',
|
|
224
|
+
name: 'CPU-Based Scaling',
|
|
225
|
+
description: 'Scale based on CPU utilization thresholds',
|
|
226
|
+
triggers: ['cpu_utilization_high', 'cpu_utilization_sustained'],
|
|
227
|
+
actions: ['scale_up', 'add_replicas'],
|
|
228
|
+
thresholds: {
|
|
229
|
+
scaleUpThreshold: 80,
|
|
230
|
+
scaleDownThreshold: 30,
|
|
231
|
+
cooldownPeriod: 300,
|
|
232
|
+
maxReplicas: 10,
|
|
233
|
+
minReplicas: 1,
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: 'memory-scaling',
|
|
238
|
+
name: 'Memory-Based Scaling',
|
|
239
|
+
description: 'Scale based on memory utilization thresholds',
|
|
240
|
+
triggers: ['memory_utilization_high', 'memory_pressure'],
|
|
241
|
+
actions: ['scale_up', 'optimize_memory', 'add_replicas'],
|
|
242
|
+
thresholds: {
|
|
243
|
+
scaleUpThreshold: 75,
|
|
244
|
+
scaleDownThreshold: 25,
|
|
245
|
+
cooldownPeriod: 600,
|
|
246
|
+
maxReplicas: 8,
|
|
247
|
+
minReplicas: 1,
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
id: 'request-scaling',
|
|
252
|
+
name: 'Request-Based Scaling',
|
|
253
|
+
description: 'Scale based on incoming request volume',
|
|
254
|
+
triggers: ['request_rate_high', 'queue_depth_high'],
|
|
255
|
+
actions: ['scale_up', 'add_replicas', 'enable_caching'],
|
|
256
|
+
thresholds: {
|
|
257
|
+
requestsPerSecondHigh: 1000,
|
|
258
|
+
requestsPerSecondLow: 100,
|
|
259
|
+
queueDepthThreshold: 50,
|
|
260
|
+
maxReplicas: 20,
|
|
261
|
+
minReplicas: 2,
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
];
|
|
265
|
+
|
|
266
|
+
for (const policy of policies) {
|
|
267
|
+
this.scalingPolicies.set(policy.id, policy);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// -----------------------------------------------------------------------
|
|
272
|
+
// Retirement Policies (3)
|
|
273
|
+
// -----------------------------------------------------------------------
|
|
274
|
+
|
|
275
|
+
private initializeRetirementPolicies(): void {
|
|
276
|
+
const policies: RetirementPolicy[] = [
|
|
277
|
+
{
|
|
278
|
+
id: 'performance-retirement',
|
|
279
|
+
name: 'Performance-Based Retirement',
|
|
280
|
+
description: 'Retire agents that consistently underperform',
|
|
281
|
+
criteria: ['low_success_rate', 'high_error_rate', 'slow_response_time'],
|
|
282
|
+
thresholds: {
|
|
283
|
+
minSuccessRate: 0.7,
|
|
284
|
+
maxErrorRate: 0.3,
|
|
285
|
+
maxResponseTime: 5000,
|
|
286
|
+
evaluationPeriod: 86400,
|
|
287
|
+
},
|
|
288
|
+
gracePeriod: 3600,
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
id: 'age-retirement',
|
|
292
|
+
name: 'Age-Based Retirement',
|
|
293
|
+
description: 'Retire agents that have exceeded their maximum operational age',
|
|
294
|
+
criteria: ['max_age_exceeded', 'deprecation_schedule'],
|
|
295
|
+
thresholds: {
|
|
296
|
+
maxAgeDays: 90,
|
|
297
|
+
warningAgeDays: 75,
|
|
298
|
+
evaluationPeriod: 86400,
|
|
299
|
+
},
|
|
300
|
+
gracePeriod: 86400,
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
id: 'resource-retirement',
|
|
304
|
+
name: 'Resource-Based Retirement',
|
|
305
|
+
description: 'Retire agents consuming excessive resources without proportional output',
|
|
306
|
+
criteria: ['high_resource_usage', 'low_throughput', 'cost_inefficiency'],
|
|
307
|
+
thresholds: {
|
|
308
|
+
maxCpuUsage: 90,
|
|
309
|
+
maxMemoryUsage: 85,
|
|
310
|
+
minThroughput: 10,
|
|
311
|
+
costEfficiencyThreshold: 0.5,
|
|
312
|
+
evaluationPeriod: 43200,
|
|
313
|
+
},
|
|
314
|
+
gracePeriod: 7200,
|
|
315
|
+
},
|
|
316
|
+
];
|
|
317
|
+
|
|
318
|
+
for (const policy of policies) {
|
|
319
|
+
this.retirementPolicies.set(policy.id, policy);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// -----------------------------------------------------------------------
|
|
324
|
+
// Health Monitoring (3 monitors)
|
|
325
|
+
// -----------------------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
private initializeHealthMonitoring(): void {
|
|
328
|
+
const monitors: HealthMonitor[] = [
|
|
329
|
+
{
|
|
330
|
+
id: 'system-health',
|
|
331
|
+
name: 'System Health Monitor',
|
|
332
|
+
metrics: ['cpu_usage', 'memory_usage', 'disk_usage', 'network_latency'],
|
|
333
|
+
interval: 30000,
|
|
334
|
+
thresholds: {
|
|
335
|
+
cpu_usage: 85,
|
|
336
|
+
memory_usage: 80,
|
|
337
|
+
disk_usage: 90,
|
|
338
|
+
network_latency: 1000,
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
id: 'performance-health',
|
|
343
|
+
name: 'Performance Health Monitor',
|
|
344
|
+
metrics: ['response_time', 'throughput', 'error_rate', 'success_rate'],
|
|
345
|
+
interval: 60000,
|
|
346
|
+
thresholds: {
|
|
347
|
+
response_time: 3000,
|
|
348
|
+
throughput: 50,
|
|
349
|
+
error_rate: 0.1,
|
|
350
|
+
success_rate: 0.9,
|
|
351
|
+
},
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
id: 'safety-health',
|
|
355
|
+
name: 'Safety Health Monitor',
|
|
356
|
+
metrics: ['trust_score', 'safety_violations', 'autonomy_level', 'compliance_score'],
|
|
357
|
+
interval: 15000,
|
|
358
|
+
thresholds: {
|
|
359
|
+
trust_score: 0.7,
|
|
360
|
+
safety_violations: 0,
|
|
361
|
+
autonomy_level: 'supervised',
|
|
362
|
+
compliance_score: 0.95,
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
];
|
|
366
|
+
|
|
367
|
+
for (const monitor of monitors) {
|
|
368
|
+
this.healthMonitors.set(monitor.id, monitor);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// -----------------------------------------------------------------------
|
|
373
|
+
// Agent creation & deployment
|
|
374
|
+
// -----------------------------------------------------------------------
|
|
375
|
+
|
|
376
|
+
async createAndDeployAgent(
|
|
377
|
+
agentName: string,
|
|
378
|
+
agentConfig: Record<string, unknown> = {}
|
|
379
|
+
): Promise<AgentDeployment> {
|
|
380
|
+
console.log(`[AgentLifecycleManager] Creating and deploying agent: ${agentName}`);
|
|
381
|
+
|
|
382
|
+
const agentId = randomUUID();
|
|
383
|
+
|
|
384
|
+
const deployment: AgentDeployment = {
|
|
385
|
+
agentId,
|
|
386
|
+
agentName,
|
|
387
|
+
currentStage: 'creation',
|
|
388
|
+
stageHistory: [],
|
|
389
|
+
startTime: Date.now(),
|
|
390
|
+
status: 'initializing',
|
|
391
|
+
metrics: {
|
|
392
|
+
stageTransitions: 0,
|
|
393
|
+
totalTime: 0,
|
|
394
|
+
errors: 0,
|
|
395
|
+
},
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
this.agentDeployments.set(agentId, deployment);
|
|
399
|
+
|
|
400
|
+
this.emit('agent-created', { agentId, agentName, config: agentConfig });
|
|
401
|
+
|
|
402
|
+
await this.initializeAgentLifecycle(agentId, agentConfig);
|
|
403
|
+
|
|
404
|
+
return deployment;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private async initializeAgentLifecycle(
|
|
408
|
+
agentId: string,
|
|
409
|
+
agentConfig: Record<string, unknown>
|
|
410
|
+
): Promise<void> {
|
|
411
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
412
|
+
if (!deployment) {
|
|
413
|
+
throw new Error(`Deployment not found for agent: ${agentId}`);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
console.log(`[AgentLifecycleManager] Starting lifecycle for agent: ${deployment.agentName}`);
|
|
417
|
+
|
|
418
|
+
deployment.status = 'running';
|
|
419
|
+
await this.executeLifecycleStages(agentId, agentConfig);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// -----------------------------------------------------------------------
|
|
423
|
+
// Execute lifecycle stages
|
|
424
|
+
// -----------------------------------------------------------------------
|
|
425
|
+
|
|
426
|
+
private async executeLifecycleStages(
|
|
427
|
+
agentId: string,
|
|
428
|
+
agentConfig: Record<string, unknown>
|
|
429
|
+
): Promise<void> {
|
|
430
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
431
|
+
if (!deployment) return;
|
|
432
|
+
|
|
433
|
+
const sortedStages = Array.from(this.lifecycleStages.values()).sort(
|
|
434
|
+
(a, b) => a.order - b.order
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
for (const stage of sortedStages) {
|
|
438
|
+
if (!stage.required && !stage.autoTransition) {
|
|
439
|
+
console.log(
|
|
440
|
+
`[AgentLifecycleManager] Skipping optional stage: ${stage.name} (will be triggered on-demand)`
|
|
441
|
+
);
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
try {
|
|
446
|
+
await this.executeStage(agentId, stage, agentConfig);
|
|
447
|
+
} catch (error: unknown) {
|
|
448
|
+
const errMsg = error instanceof Error ? error.message : String(error);
|
|
449
|
+
console.error(
|
|
450
|
+
`[AgentLifecycleManager] Error in stage ${stage.name}: ${errMsg}`
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
deployment.metrics.errors += 1;
|
|
454
|
+
deployment.stageHistory.push({
|
|
455
|
+
stage: stage.id,
|
|
456
|
+
timestamp: Date.now(),
|
|
457
|
+
status: 'failed',
|
|
458
|
+
error: errMsg,
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
if (stage.required) {
|
|
462
|
+
deployment.status = 'failed';
|
|
463
|
+
this.emit('lifecycle-failed', { agentId, stage: stage.id, error: errMsg });
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
deployment.status = 'active';
|
|
470
|
+
deployment.metrics.totalTime = Date.now() - deployment.startTime;
|
|
471
|
+
|
|
472
|
+
console.log(
|
|
473
|
+
`[AgentLifecycleManager] Agent ${deployment.agentName} lifecycle complete - status: active`
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
this.emit('lifecycle-complete', {
|
|
477
|
+
agentId,
|
|
478
|
+
agentName: deployment.agentName,
|
|
479
|
+
totalTime: deployment.metrics.totalTime,
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// -----------------------------------------------------------------------
|
|
484
|
+
// Execute individual stage (switch)
|
|
485
|
+
// -----------------------------------------------------------------------
|
|
486
|
+
|
|
487
|
+
private async executeStage(
|
|
488
|
+
agentId: string,
|
|
489
|
+
stage: LifecycleStage,
|
|
490
|
+
agentConfig: Record<string, unknown>
|
|
491
|
+
): Promise<void> {
|
|
492
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
493
|
+
if (!deployment) return;
|
|
494
|
+
|
|
495
|
+
console.log(
|
|
496
|
+
`[AgentLifecycleManager] Executing stage: ${stage.name} for agent: ${deployment.agentName}`
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
deployment.currentStage = stage.id;
|
|
500
|
+
deployment.metrics.stageTransitions += 1;
|
|
501
|
+
|
|
502
|
+
const startTime = Date.now();
|
|
503
|
+
|
|
504
|
+
switch (stage.id) {
|
|
505
|
+
case 'creation':
|
|
506
|
+
await this.executeCreationStage(agentId, agentConfig);
|
|
507
|
+
break;
|
|
508
|
+
case 'validation':
|
|
509
|
+
await this.executeValidationStage(agentId, agentConfig);
|
|
510
|
+
break;
|
|
511
|
+
case 'deployment':
|
|
512
|
+
await this.executeDeploymentStage(agentId, agentConfig);
|
|
513
|
+
break;
|
|
514
|
+
case 'activation':
|
|
515
|
+
await this.executeActivationStage(agentId, agentConfig);
|
|
516
|
+
break;
|
|
517
|
+
case 'monitoring':
|
|
518
|
+
await this.executeMonitoringStage(agentId);
|
|
519
|
+
break;
|
|
520
|
+
case 'scaling':
|
|
521
|
+
await this.executeScalingStage(agentId);
|
|
522
|
+
break;
|
|
523
|
+
case 'maintenance':
|
|
524
|
+
await this.executeMaintenanceStage(agentId);
|
|
525
|
+
break;
|
|
526
|
+
case 'retirement':
|
|
527
|
+
await this.executeRetirementStage(agentId);
|
|
528
|
+
break;
|
|
529
|
+
default:
|
|
530
|
+
console.log(`[AgentLifecycleManager] Unknown stage: ${stage.id}`);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const elapsed = Date.now() - startTime;
|
|
534
|
+
|
|
535
|
+
deployment.stageHistory.push({
|
|
536
|
+
stage: stage.id,
|
|
537
|
+
timestamp: Date.now(),
|
|
538
|
+
status: 'completed',
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
this.emit('stage-completed', {
|
|
542
|
+
agentId,
|
|
543
|
+
stage: stage.id,
|
|
544
|
+
duration: elapsed,
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// -----------------------------------------------------------------------
|
|
549
|
+
// Stage implementations
|
|
550
|
+
// -----------------------------------------------------------------------
|
|
551
|
+
|
|
552
|
+
private async executeCreationStage(
|
|
553
|
+
agentId: string,
|
|
554
|
+
agentConfig: Record<string, unknown>
|
|
555
|
+
): Promise<void> {
|
|
556
|
+
console.log(`[AgentLifecycleManager] [Creation] Initializing agent resources...`);
|
|
557
|
+
|
|
558
|
+
// Allocate resources
|
|
559
|
+
await this.delay(100);
|
|
560
|
+
console.log('[AgentLifecycleManager] [Creation] Resources allocated');
|
|
561
|
+
|
|
562
|
+
// Configure agent capabilities
|
|
563
|
+
await this.delay(50);
|
|
564
|
+
console.log('[AgentLifecycleManager] [Creation] Capabilities configured');
|
|
565
|
+
|
|
566
|
+
// Set up communication channels
|
|
567
|
+
await this.delay(50);
|
|
568
|
+
console.log('[AgentLifecycleManager] [Creation] Communication channels established');
|
|
569
|
+
|
|
570
|
+
// Initialize memory stores
|
|
571
|
+
await this.delay(50);
|
|
572
|
+
console.log('[AgentLifecycleManager] [Creation] Memory stores initialized');
|
|
573
|
+
|
|
574
|
+
this.emit('creation-complete', { agentId, config: agentConfig });
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private async executeValidationStage(
|
|
578
|
+
agentId: string,
|
|
579
|
+
_agentConfig: Record<string, unknown>
|
|
580
|
+
): Promise<void> {
|
|
581
|
+
console.log('[AgentLifecycleManager] [Validation] Validating agent configuration...');
|
|
582
|
+
|
|
583
|
+
// Validate configuration
|
|
584
|
+
await this.delay(100);
|
|
585
|
+
const configValid = true;
|
|
586
|
+
console.log(
|
|
587
|
+
`[AgentLifecycleManager] [Validation] Configuration valid: ${configValid}`
|
|
588
|
+
);
|
|
589
|
+
|
|
590
|
+
// Validate capabilities
|
|
591
|
+
await this.delay(50);
|
|
592
|
+
const capabilitiesValid = true;
|
|
593
|
+
console.log(
|
|
594
|
+
`[AgentLifecycleManager] [Validation] Capabilities valid: ${capabilitiesValid}`
|
|
595
|
+
);
|
|
596
|
+
|
|
597
|
+
// Run safety checks
|
|
598
|
+
await this.delay(100);
|
|
599
|
+
const safetyPassed = true;
|
|
600
|
+
console.log(
|
|
601
|
+
`[AgentLifecycleManager] [Validation] Safety checks passed: ${safetyPassed}`
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
// Validate resource requirements
|
|
605
|
+
await this.delay(50);
|
|
606
|
+
const resourcesAvailable = true;
|
|
607
|
+
console.log(
|
|
608
|
+
`[AgentLifecycleManager] [Validation] Resources available: ${resourcesAvailable}`
|
|
609
|
+
);
|
|
610
|
+
|
|
611
|
+
if (!configValid || !capabilitiesValid || !safetyPassed || !resourcesAvailable) {
|
|
612
|
+
throw new Error('Agent validation failed');
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
this.emit('validation-complete', { agentId, results: { configValid, capabilitiesValid, safetyPassed, resourcesAvailable } });
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
private async executeDeploymentStage(
|
|
619
|
+
agentId: string,
|
|
620
|
+
agentConfig: Record<string, unknown>
|
|
621
|
+
): Promise<void> {
|
|
622
|
+
console.log('[AgentLifecycleManager] [Deployment] Deploying agent to target environment...');
|
|
623
|
+
|
|
624
|
+
// Prepare deployment environment
|
|
625
|
+
await this.delay(100);
|
|
626
|
+
console.log('[AgentLifecycleManager] [Deployment] Environment prepared');
|
|
627
|
+
|
|
628
|
+
// Deploy agent components
|
|
629
|
+
await this.delay(200);
|
|
630
|
+
console.log('[AgentLifecycleManager] [Deployment] Components deployed');
|
|
631
|
+
|
|
632
|
+
// Configure networking
|
|
633
|
+
await this.delay(100);
|
|
634
|
+
console.log('[AgentLifecycleManager] [Deployment] Networking configured');
|
|
635
|
+
|
|
636
|
+
// Run deployment verification
|
|
637
|
+
await this.delay(100);
|
|
638
|
+
console.log('[AgentLifecycleManager] [Deployment] Deployment verified');
|
|
639
|
+
|
|
640
|
+
// Simulate deployment
|
|
641
|
+
await this.simulateDeployment(agentId, agentConfig);
|
|
642
|
+
|
|
643
|
+
this.emit('deployment-complete', { agentId });
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private async executeActivationStage(
|
|
647
|
+
agentId: string,
|
|
648
|
+
_agentConfig: Record<string, unknown>
|
|
649
|
+
): Promise<void> {
|
|
650
|
+
console.log('[AgentLifecycleManager] [Activation] Activating agent...');
|
|
651
|
+
|
|
652
|
+
// Start agent processes
|
|
653
|
+
await this.delay(100);
|
|
654
|
+
console.log('[AgentLifecycleManager] [Activation] Agent processes started');
|
|
655
|
+
|
|
656
|
+
// Enable monitoring
|
|
657
|
+
await this.delay(50);
|
|
658
|
+
console.log('[AgentLifecycleManager] [Activation] Monitoring enabled');
|
|
659
|
+
|
|
660
|
+
// Begin health checks
|
|
661
|
+
await this.delay(50);
|
|
662
|
+
console.log('[AgentLifecycleManager] [Activation] Health checks active');
|
|
663
|
+
|
|
664
|
+
// Set agent to active state
|
|
665
|
+
await this.delay(50);
|
|
666
|
+
console.log('[AgentLifecycleManager] [Activation] Agent is now active');
|
|
667
|
+
|
|
668
|
+
// Start autonomous operations
|
|
669
|
+
await this.startAutonomousOperations(agentId);
|
|
670
|
+
|
|
671
|
+
this.emit('activation-complete', { agentId });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
private async executeMonitoringStage(agentId: string): Promise<void> {
|
|
675
|
+
console.log('[AgentLifecycleManager] [Monitoring] Starting comprehensive monitoring...');
|
|
676
|
+
|
|
677
|
+
// Start all health monitors
|
|
678
|
+
await this.startComprehensiveMonitoring(agentId);
|
|
679
|
+
|
|
680
|
+
console.log('[AgentLifecycleManager] [Monitoring] Comprehensive monitoring active');
|
|
681
|
+
|
|
682
|
+
this.emit('monitoring-active', { agentId });
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
private async executeScalingStage(agentId: string): Promise<void> {
|
|
686
|
+
console.log('[AgentLifecycleManager] [Scaling] Evaluating scaling needs...');
|
|
687
|
+
|
|
688
|
+
const scalingNeeded = await this.checkScalingNeeded(agentId);
|
|
689
|
+
|
|
690
|
+
if (scalingNeeded) {
|
|
691
|
+
console.log('[AgentLifecycleManager] [Scaling] Scaling action required');
|
|
692
|
+
await this.executeScaling(agentId);
|
|
693
|
+
} else {
|
|
694
|
+
console.log('[AgentLifecycleManager] [Scaling] No scaling action needed');
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
this.emit('scaling-evaluated', { agentId, scalingNeeded });
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
private async executeMaintenanceStage(agentId: string): Promise<void> {
|
|
701
|
+
console.log('[AgentLifecycleManager] [Maintenance] Starting maintenance cycle...');
|
|
702
|
+
|
|
703
|
+
const maintenanceNeeded = await this.checkMaintenanceNeeded(agentId);
|
|
704
|
+
|
|
705
|
+
if (maintenanceNeeded) {
|
|
706
|
+
console.log('[AgentLifecycleManager] [Maintenance] Performing maintenance...');
|
|
707
|
+
await this.executeMaintenance(agentId);
|
|
708
|
+
} else {
|
|
709
|
+
console.log('[AgentLifecycleManager] [Maintenance] No maintenance required');
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
this.emit('maintenance-complete', { agentId, maintenancePerformed: maintenanceNeeded });
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
private async executeRetirementStage(agentId: string): Promise<void> {
|
|
716
|
+
console.log('[AgentLifecycleManager] [Retirement] Evaluating retirement criteria...');
|
|
717
|
+
|
|
718
|
+
const retirementNeeded = await this.checkRetirementNeeded(agentId);
|
|
719
|
+
|
|
720
|
+
if (retirementNeeded) {
|
|
721
|
+
console.log('[AgentLifecycleManager] [Retirement] Initiating graceful retirement...');
|
|
722
|
+
await this.executeRetirement(agentId);
|
|
723
|
+
} else {
|
|
724
|
+
console.log('[AgentLifecycleManager] [Retirement] Agent does not meet retirement criteria');
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
this.emit('retirement-evaluated', { agentId, retirementNeeded });
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// -----------------------------------------------------------------------
|
|
731
|
+
// Deployment simulation
|
|
732
|
+
// -----------------------------------------------------------------------
|
|
733
|
+
|
|
734
|
+
private async simulateDeployment(
|
|
735
|
+
agentId: string,
|
|
736
|
+
_agentConfig: Record<string, unknown>
|
|
737
|
+
): Promise<void> {
|
|
738
|
+
console.log(`[AgentLifecycleManager] [Deployment] Simulating deployment for agent: ${agentId}`);
|
|
739
|
+
|
|
740
|
+
// Simulate resource provisioning
|
|
741
|
+
await this.delay(150);
|
|
742
|
+
console.log('[AgentLifecycleManager] [Deployment] Resources provisioned');
|
|
743
|
+
|
|
744
|
+
// Simulate container creation
|
|
745
|
+
await this.delay(100);
|
|
746
|
+
console.log('[AgentLifecycleManager] [Deployment] Container created');
|
|
747
|
+
|
|
748
|
+
// Simulate service registration
|
|
749
|
+
await this.delay(50);
|
|
750
|
+
console.log('[AgentLifecycleManager] [Deployment] Service registered');
|
|
751
|
+
|
|
752
|
+
// Simulate health check endpoint setup
|
|
753
|
+
await this.delay(50);
|
|
754
|
+
console.log('[AgentLifecycleManager] [Deployment] Health check endpoint ready');
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// -----------------------------------------------------------------------
|
|
758
|
+
// Autonomous operations
|
|
759
|
+
// -----------------------------------------------------------------------
|
|
760
|
+
|
|
761
|
+
private async startAutonomousOperations(agentId: string): Promise<void> {
|
|
762
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
763
|
+
if (!deployment) return;
|
|
764
|
+
|
|
765
|
+
console.log(
|
|
766
|
+
`[AgentLifecycleManager] Starting autonomous operations for: ${deployment.agentName}`
|
|
767
|
+
);
|
|
768
|
+
|
|
769
|
+
// Initialize autonomous decision loop
|
|
770
|
+
await this.delay(100);
|
|
771
|
+
console.log('[AgentLifecycleManager] Autonomous decision loop initialized');
|
|
772
|
+
|
|
773
|
+
// Enable self-monitoring
|
|
774
|
+
await this.delay(50);
|
|
775
|
+
console.log('[AgentLifecycleManager] Self-monitoring enabled');
|
|
776
|
+
|
|
777
|
+
// Start adaptive learning
|
|
778
|
+
await this.delay(50);
|
|
779
|
+
console.log('[AgentLifecycleManager] Adaptive learning started');
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// -----------------------------------------------------------------------
|
|
783
|
+
// Comprehensive Monitoring
|
|
784
|
+
// -----------------------------------------------------------------------
|
|
785
|
+
|
|
786
|
+
private async startComprehensiveMonitoring(agentId: string): Promise<void> {
|
|
787
|
+
console.log(
|
|
788
|
+
`[AgentLifecycleManager] Starting comprehensive monitoring for agent: ${agentId}`
|
|
789
|
+
);
|
|
790
|
+
|
|
791
|
+
for (const [, monitor] of this.healthMonitors) {
|
|
792
|
+
await this.startHealthMonitor(agentId, monitor);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
private async startHealthMonitor(
|
|
797
|
+
agentId: string,
|
|
798
|
+
monitor: HealthMonitor
|
|
799
|
+
): Promise<void> {
|
|
800
|
+
console.log(
|
|
801
|
+
`[AgentLifecycleManager] Starting health monitor: ${monitor.name} (interval: ${monitor.interval}ms)`
|
|
802
|
+
);
|
|
803
|
+
|
|
804
|
+
// Collect initial metrics
|
|
805
|
+
const metrics = await this.collectMetrics(agentId, monitor);
|
|
806
|
+
|
|
807
|
+
// Check for threshold violations
|
|
808
|
+
const violations = this.checkThresholdViolations(metrics, monitor);
|
|
809
|
+
|
|
810
|
+
if (violations.length > 0) {
|
|
811
|
+
await this.handleThresholdViolations(agentId, violations, monitor);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
console.log(
|
|
815
|
+
`[AgentLifecycleManager] Health monitor ${monitor.name} started - ${monitor.metrics.length} metrics tracked`
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
private async collectMetrics(
|
|
820
|
+
agentId: string,
|
|
821
|
+
monitor: HealthMonitor
|
|
822
|
+
): Promise<Record<string, number>> {
|
|
823
|
+
const metrics: Record<string, number> = {};
|
|
824
|
+
|
|
825
|
+
for (const metric of monitor.metrics) {
|
|
826
|
+
// Simulate metric collection with reasonable random values
|
|
827
|
+
switch (metric) {
|
|
828
|
+
case 'cpu_usage':
|
|
829
|
+
metrics[metric] = Math.random() * 100;
|
|
830
|
+
break;
|
|
831
|
+
case 'memory_usage':
|
|
832
|
+
metrics[metric] = Math.random() * 100;
|
|
833
|
+
break;
|
|
834
|
+
case 'disk_usage':
|
|
835
|
+
metrics[metric] = Math.random() * 100;
|
|
836
|
+
break;
|
|
837
|
+
case 'network_latency':
|
|
838
|
+
metrics[metric] = Math.random() * 2000;
|
|
839
|
+
break;
|
|
840
|
+
case 'response_time':
|
|
841
|
+
metrics[metric] = Math.random() * 5000;
|
|
842
|
+
break;
|
|
843
|
+
case 'throughput':
|
|
844
|
+
metrics[metric] = Math.random() * 200;
|
|
845
|
+
break;
|
|
846
|
+
case 'error_rate':
|
|
847
|
+
metrics[metric] = Math.random() * 0.2;
|
|
848
|
+
break;
|
|
849
|
+
case 'success_rate':
|
|
850
|
+
metrics[metric] = 0.8 + Math.random() * 0.2;
|
|
851
|
+
break;
|
|
852
|
+
case 'trust_score':
|
|
853
|
+
metrics[metric] = 0.7 + Math.random() * 0.3;
|
|
854
|
+
break;
|
|
855
|
+
case 'safety_violations':
|
|
856
|
+
metrics[metric] = Math.random() < 0.95 ? 0 : Math.floor(Math.random() * 3);
|
|
857
|
+
break;
|
|
858
|
+
case 'autonomy_level':
|
|
859
|
+
metrics[metric] = Math.random();
|
|
860
|
+
break;
|
|
861
|
+
case 'compliance_score':
|
|
862
|
+
metrics[metric] = 0.9 + Math.random() * 0.1;
|
|
863
|
+
break;
|
|
864
|
+
default:
|
|
865
|
+
metrics[metric] = Math.random() * 100;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
return metrics;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
private checkThresholdViolations(
|
|
873
|
+
metrics: Record<string, number>,
|
|
874
|
+
monitor: HealthMonitor
|
|
875
|
+
): ThresholdViolation[] {
|
|
876
|
+
const violations: ThresholdViolation[] = [];
|
|
877
|
+
|
|
878
|
+
for (const [metric, value] of Object.entries(metrics)) {
|
|
879
|
+
const threshold = monitor.thresholds[metric];
|
|
880
|
+
if (threshold === undefined) continue;
|
|
881
|
+
|
|
882
|
+
let violated = false;
|
|
883
|
+
|
|
884
|
+
if (typeof threshold === 'number') {
|
|
885
|
+
// For rates/scores where higher is better, violation is when value is below threshold
|
|
886
|
+
if (
|
|
887
|
+
metric === 'success_rate' ||
|
|
888
|
+
metric === 'trust_score' ||
|
|
889
|
+
metric === 'compliance_score' ||
|
|
890
|
+
metric === 'throughput'
|
|
891
|
+
) {
|
|
892
|
+
violated = value < threshold;
|
|
893
|
+
} else {
|
|
894
|
+
// For usage/latency/error metrics, violation is when value exceeds threshold
|
|
895
|
+
violated = value > threshold;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
if (violated) {
|
|
900
|
+
violations.push({
|
|
901
|
+
metric,
|
|
902
|
+
value,
|
|
903
|
+
threshold,
|
|
904
|
+
severity: this.calculateViolationSeverity(metric, value, threshold),
|
|
905
|
+
});
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
return violations;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
private calculateViolationSeverity(
|
|
913
|
+
metric: string,
|
|
914
|
+
value: number,
|
|
915
|
+
threshold: number | string
|
|
916
|
+
): string {
|
|
917
|
+
if (typeof threshold === 'string') return 'low';
|
|
918
|
+
|
|
919
|
+
const ratio = value / threshold;
|
|
920
|
+
|
|
921
|
+
// For metrics where exceeding the threshold is bad
|
|
922
|
+
if (
|
|
923
|
+
metric === 'success_rate' ||
|
|
924
|
+
metric === 'trust_score' ||
|
|
925
|
+
metric === 'compliance_score' ||
|
|
926
|
+
metric === 'throughput'
|
|
927
|
+
) {
|
|
928
|
+
// Value is below threshold - lower ratio means worse
|
|
929
|
+
if (ratio < 0.5) return 'critical';
|
|
930
|
+
if (ratio < 0.75) return 'high';
|
|
931
|
+
if (ratio < 0.9) return 'medium';
|
|
932
|
+
return 'low';
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// For metrics where exceeding the threshold is bad (cpu, memory, errors, latency)
|
|
936
|
+
if (ratio > 2.0) return 'critical';
|
|
937
|
+
if (ratio > 1.5) return 'high';
|
|
938
|
+
if (ratio > 1.2) return 'medium';
|
|
939
|
+
return 'low';
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
private async handleThresholdViolations(
|
|
943
|
+
agentId: string,
|
|
944
|
+
violations: ThresholdViolation[],
|
|
945
|
+
_monitor: HealthMonitor
|
|
946
|
+
): Promise<void> {
|
|
947
|
+
console.log(
|
|
948
|
+
`[AgentLifecycleManager] Handling ${violations.length} threshold violation(s) for agent: ${agentId}`
|
|
949
|
+
);
|
|
950
|
+
|
|
951
|
+
for (const violation of violations) {
|
|
952
|
+
console.log(
|
|
953
|
+
`[AgentLifecycleManager] Violation: ${violation.metric} = ${typeof violation.value === 'number' ? violation.value.toFixed(2) : violation.value} (threshold: ${violation.threshold}, severity: ${violation.severity})`
|
|
954
|
+
);
|
|
955
|
+
|
|
956
|
+
switch (violation.severity) {
|
|
957
|
+
case 'critical':
|
|
958
|
+
await this.handleCriticalViolation(agentId, violation);
|
|
959
|
+
break;
|
|
960
|
+
case 'high':
|
|
961
|
+
await this.handleHighViolation(agentId, violation);
|
|
962
|
+
break;
|
|
963
|
+
case 'medium':
|
|
964
|
+
case 'low':
|
|
965
|
+
await this.handleLowViolation(agentId, violation);
|
|
966
|
+
break;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
private async handleCriticalViolation(
|
|
972
|
+
agentId: string,
|
|
973
|
+
violation: ThresholdViolation
|
|
974
|
+
): Promise<void> {
|
|
975
|
+
console.log(
|
|
976
|
+
`[AgentLifecycleManager] CRITICAL violation for agent ${agentId}: ${violation.metric}`
|
|
977
|
+
);
|
|
978
|
+
|
|
979
|
+
// Generate critical alert
|
|
980
|
+
this.generateAlert('critical', agentId, [violation]);
|
|
981
|
+
|
|
982
|
+
// Increase monitoring frequency
|
|
983
|
+
this.increaseMonitoringFrequency(agentId);
|
|
984
|
+
|
|
985
|
+
// Consider emergency scaling or retirement
|
|
986
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
987
|
+
if (deployment) {
|
|
988
|
+
deployment.metrics.errors += 1;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
this.emit('critical-violation', { agentId, violation });
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
private async handleHighViolation(
|
|
995
|
+
agentId: string,
|
|
996
|
+
violation: ThresholdViolation
|
|
997
|
+
): Promise<void> {
|
|
998
|
+
console.log(
|
|
999
|
+
`[AgentLifecycleManager] HIGH violation for agent ${agentId}: ${violation.metric}`
|
|
1000
|
+
);
|
|
1001
|
+
|
|
1002
|
+
// Generate high-severity alert
|
|
1003
|
+
this.generateAlert('high', agentId, [violation]);
|
|
1004
|
+
|
|
1005
|
+
// Increase monitoring frequency
|
|
1006
|
+
this.increaseMonitoringFrequency(agentId);
|
|
1007
|
+
|
|
1008
|
+
this.emit('high-violation', { agentId, violation });
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
private async handleLowViolation(
|
|
1012
|
+
agentId: string,
|
|
1013
|
+
violation: ThresholdViolation
|
|
1014
|
+
): Promise<void> {
|
|
1015
|
+
console.log(
|
|
1016
|
+
`[AgentLifecycleManager] LOW/MEDIUM violation for agent ${agentId}: ${violation.metric}`
|
|
1017
|
+
);
|
|
1018
|
+
|
|
1019
|
+
// Generate informational alert
|
|
1020
|
+
this.generateAlert('info', agentId, [violation]);
|
|
1021
|
+
|
|
1022
|
+
this.emit('low-violation', { agentId, violation });
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
private generateAlert(
|
|
1026
|
+
severity: string,
|
|
1027
|
+
agentId: string,
|
|
1028
|
+
violations: ThresholdViolation[]
|
|
1029
|
+
): Alert {
|
|
1030
|
+
const alert: Alert = {
|
|
1031
|
+
id: randomUUID(),
|
|
1032
|
+
type: 'threshold-violation',
|
|
1033
|
+
severity,
|
|
1034
|
+
message: `Agent ${agentId} has ${violations.length} threshold violation(s)`,
|
|
1035
|
+
agentId,
|
|
1036
|
+
timestamp: Date.now(),
|
|
1037
|
+
violations,
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
console.log(
|
|
1041
|
+
`[AgentLifecycleManager] Alert generated: [${severity.toUpperCase()}] ${alert.message}`
|
|
1042
|
+
);
|
|
1043
|
+
|
|
1044
|
+
this.emit('alert', alert);
|
|
1045
|
+
|
|
1046
|
+
return alert;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
private increaseMonitoringFrequency(agentId: string): void {
|
|
1050
|
+
console.log(
|
|
1051
|
+
`[AgentLifecycleManager] Increasing monitoring frequency for agent: ${agentId}`
|
|
1052
|
+
);
|
|
1053
|
+
|
|
1054
|
+
// Halve the interval for all monitors (increase frequency)
|
|
1055
|
+
for (const [, monitor] of this.healthMonitors) {
|
|
1056
|
+
const originalInterval = monitor.interval;
|
|
1057
|
+
monitor.interval = Math.max(5000, Math.floor(monitor.interval / 2));
|
|
1058
|
+
if (monitor.interval !== originalInterval) {
|
|
1059
|
+
console.log(
|
|
1060
|
+
`[AgentLifecycleManager] ${monitor.name}: ${originalInterval}ms -> ${monitor.interval}ms`
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// -----------------------------------------------------------------------
|
|
1067
|
+
// Scaling
|
|
1068
|
+
// -----------------------------------------------------------------------
|
|
1069
|
+
|
|
1070
|
+
private async checkScalingNeeded(agentId: string): Promise<boolean> {
|
|
1071
|
+
console.log(`[AgentLifecycleManager] Checking scaling needs for agent: ${agentId}`);
|
|
1072
|
+
|
|
1073
|
+
for (const [, policy] of this.scalingPolicies) {
|
|
1074
|
+
const needed = await this.evaluateScalingPolicy(agentId, policy);
|
|
1075
|
+
if (needed) {
|
|
1076
|
+
console.log(
|
|
1077
|
+
`[AgentLifecycleManager] Scaling triggered by policy: ${policy.name}`
|
|
1078
|
+
);
|
|
1079
|
+
return true;
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
return false;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
private async evaluateScalingPolicy(
|
|
1087
|
+
agentId: string,
|
|
1088
|
+
policy: ScalingPolicy
|
|
1089
|
+
): Promise<boolean> {
|
|
1090
|
+
// Simulate policy evaluation with random metrics
|
|
1091
|
+
const simulatedMetric = Math.random() * 100;
|
|
1092
|
+
|
|
1093
|
+
switch (policy.id) {
|
|
1094
|
+
case 'cpu-scaling':
|
|
1095
|
+
return simulatedMetric > policy.thresholds.scaleUpThreshold;
|
|
1096
|
+
case 'memory-scaling':
|
|
1097
|
+
return simulatedMetric > policy.thresholds.scaleUpThreshold;
|
|
1098
|
+
case 'request-scaling':
|
|
1099
|
+
return simulatedMetric * 20 > policy.thresholds.requestsPerSecondHigh;
|
|
1100
|
+
default:
|
|
1101
|
+
return false;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
private async executeScaling(agentId: string): Promise<void> {
|
|
1106
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1107
|
+
if (!deployment) return;
|
|
1108
|
+
|
|
1109
|
+
console.log(`[AgentLifecycleManager] Executing scaling for agent: ${deployment.agentName}`);
|
|
1110
|
+
|
|
1111
|
+
// Simulate scaling process
|
|
1112
|
+
await this.delay(200);
|
|
1113
|
+
console.log('[AgentLifecycleManager] [Scaling] Additional resources allocated');
|
|
1114
|
+
|
|
1115
|
+
await this.delay(100);
|
|
1116
|
+
console.log('[AgentLifecycleManager] [Scaling] New replicas deployed');
|
|
1117
|
+
|
|
1118
|
+
await this.delay(100);
|
|
1119
|
+
console.log('[AgentLifecycleManager] [Scaling] Load balancer updated');
|
|
1120
|
+
|
|
1121
|
+
await this.delay(50);
|
|
1122
|
+
console.log('[AgentLifecycleManager] [Scaling] Scaling complete');
|
|
1123
|
+
|
|
1124
|
+
this.emit('scaling-complete', { agentId, agentName: deployment.agentName });
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
// -----------------------------------------------------------------------
|
|
1128
|
+
// Maintenance
|
|
1129
|
+
// -----------------------------------------------------------------------
|
|
1130
|
+
|
|
1131
|
+
private async checkMaintenanceNeeded(agentId: string): Promise<boolean> {
|
|
1132
|
+
console.log(`[AgentLifecycleManager] Checking maintenance needs for agent: ${agentId}`);
|
|
1133
|
+
|
|
1134
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1135
|
+
if (!deployment) return false;
|
|
1136
|
+
|
|
1137
|
+
// Check uptime-based maintenance
|
|
1138
|
+
const uptime = Date.now() - deployment.startTime;
|
|
1139
|
+
const maintenanceInterval = 24 * 60 * 60 * 1000; // 24 hours
|
|
1140
|
+
|
|
1141
|
+
if (uptime > maintenanceInterval) {
|
|
1142
|
+
console.log('[AgentLifecycleManager] Maintenance needed: uptime threshold exceeded');
|
|
1143
|
+
return true;
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// Check error-based maintenance
|
|
1147
|
+
if (deployment.metrics.errors > 10) {
|
|
1148
|
+
console.log('[AgentLifecycleManager] Maintenance needed: error threshold exceeded');
|
|
1149
|
+
return true;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
return false;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
private async executeMaintenance(agentId: string): Promise<void> {
|
|
1156
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1157
|
+
if (!deployment) return;
|
|
1158
|
+
|
|
1159
|
+
console.log(
|
|
1160
|
+
`[AgentLifecycleManager] Executing maintenance for agent: ${deployment.agentName}`
|
|
1161
|
+
);
|
|
1162
|
+
|
|
1163
|
+
// Pause agent operations
|
|
1164
|
+
await this.delay(100);
|
|
1165
|
+
console.log('[AgentLifecycleManager] [Maintenance] Agent operations paused');
|
|
1166
|
+
|
|
1167
|
+
// Clear caches
|
|
1168
|
+
await this.delay(50);
|
|
1169
|
+
console.log('[AgentLifecycleManager] [Maintenance] Caches cleared');
|
|
1170
|
+
|
|
1171
|
+
// Optimize memory
|
|
1172
|
+
await this.delay(100);
|
|
1173
|
+
console.log('[AgentLifecycleManager] [Maintenance] Memory optimized');
|
|
1174
|
+
|
|
1175
|
+
// Update configurations
|
|
1176
|
+
await this.delay(50);
|
|
1177
|
+
console.log('[AgentLifecycleManager] [Maintenance] Configurations updated');
|
|
1178
|
+
|
|
1179
|
+
// Run diagnostics
|
|
1180
|
+
await this.delay(100);
|
|
1181
|
+
console.log('[AgentLifecycleManager] [Maintenance] Diagnostics complete');
|
|
1182
|
+
|
|
1183
|
+
// Resume operations
|
|
1184
|
+
await this.delay(50);
|
|
1185
|
+
console.log('[AgentLifecycleManager] [Maintenance] Agent operations resumed');
|
|
1186
|
+
|
|
1187
|
+
// Reset error count after maintenance
|
|
1188
|
+
deployment.metrics.errors = 0;
|
|
1189
|
+
|
|
1190
|
+
this.emit('maintenance-executed', { agentId, agentName: deployment.agentName });
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// -----------------------------------------------------------------------
|
|
1194
|
+
// Retirement
|
|
1195
|
+
// -----------------------------------------------------------------------
|
|
1196
|
+
|
|
1197
|
+
private async checkRetirementNeeded(agentId: string): Promise<boolean> {
|
|
1198
|
+
console.log(`[AgentLifecycleManager] Checking retirement criteria for agent: ${agentId}`);
|
|
1199
|
+
|
|
1200
|
+
for (const [, policy] of this.retirementPolicies) {
|
|
1201
|
+
const shouldRetire = await this.evaluateRetirementPolicy(agentId, policy);
|
|
1202
|
+
if (shouldRetire) {
|
|
1203
|
+
console.log(
|
|
1204
|
+
`[AgentLifecycleManager] Retirement triggered by policy: ${policy.name}`
|
|
1205
|
+
);
|
|
1206
|
+
return true;
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
return false;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
private async evaluateRetirementPolicy(
|
|
1214
|
+
agentId: string,
|
|
1215
|
+
policy: RetirementPolicy
|
|
1216
|
+
): Promise<boolean> {
|
|
1217
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1218
|
+
if (!deployment) return false;
|
|
1219
|
+
|
|
1220
|
+
switch (policy.id) {
|
|
1221
|
+
case 'performance-retirement': {
|
|
1222
|
+
const errorRate = deployment.metrics.errors / Math.max(deployment.metrics.stageTransitions, 1);
|
|
1223
|
+
return errorRate > (policy.thresholds.maxErrorRate ?? 0.3);
|
|
1224
|
+
}
|
|
1225
|
+
case 'age-retirement': {
|
|
1226
|
+
const ageDays = (Date.now() - deployment.startTime) / (1000 * 60 * 60 * 24);
|
|
1227
|
+
return ageDays > (policy.thresholds.maxAgeDays ?? 90);
|
|
1228
|
+
}
|
|
1229
|
+
case 'resource-retirement': {
|
|
1230
|
+
// Simulate resource check
|
|
1231
|
+
const simulatedCpu = Math.random() * 100;
|
|
1232
|
+
return simulatedCpu > (policy.thresholds.maxCpuUsage ?? 90);
|
|
1233
|
+
}
|
|
1234
|
+
default:
|
|
1235
|
+
return false;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
private async executeRetirement(agentId: string): Promise<void> {
|
|
1240
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1241
|
+
if (!deployment) return;
|
|
1242
|
+
|
|
1243
|
+
console.log(
|
|
1244
|
+
`[AgentLifecycleManager] Executing retirement for agent: ${deployment.agentName}`
|
|
1245
|
+
);
|
|
1246
|
+
|
|
1247
|
+
// Begin graceful shutdown
|
|
1248
|
+
await this.delay(100);
|
|
1249
|
+
console.log('[AgentLifecycleManager] [Retirement] Graceful shutdown initiated');
|
|
1250
|
+
|
|
1251
|
+
// Drain active connections
|
|
1252
|
+
await this.delay(200);
|
|
1253
|
+
console.log('[AgentLifecycleManager] [Retirement] Active connections drained');
|
|
1254
|
+
|
|
1255
|
+
// Save state and knowledge
|
|
1256
|
+
await this.delay(100);
|
|
1257
|
+
console.log('[AgentLifecycleManager] [Retirement] State and knowledge saved');
|
|
1258
|
+
|
|
1259
|
+
// Deregister services
|
|
1260
|
+
await this.delay(50);
|
|
1261
|
+
console.log('[AgentLifecycleManager] [Retirement] Services deregistered');
|
|
1262
|
+
|
|
1263
|
+
// Release resources
|
|
1264
|
+
await this.delay(100);
|
|
1265
|
+
console.log('[AgentLifecycleManager] [Retirement] Resources released');
|
|
1266
|
+
|
|
1267
|
+
// Update status
|
|
1268
|
+
deployment.status = 'retired';
|
|
1269
|
+
deployment.currentStage = 'retirement';
|
|
1270
|
+
deployment.metrics.totalTime = Date.now() - deployment.startTime;
|
|
1271
|
+
|
|
1272
|
+
console.log(
|
|
1273
|
+
`[AgentLifecycleManager] Agent ${deployment.agentName} has been retired after ${deployment.metrics.totalTime}ms`
|
|
1274
|
+
);
|
|
1275
|
+
|
|
1276
|
+
this.emit('agent-retired', {
|
|
1277
|
+
agentId,
|
|
1278
|
+
agentName: deployment.agentName,
|
|
1279
|
+
totalTime: deployment.metrics.totalTime,
|
|
1280
|
+
metrics: deployment.metrics,
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// -----------------------------------------------------------------------
|
|
1285
|
+
// Status
|
|
1286
|
+
// -----------------------------------------------------------------------
|
|
1287
|
+
|
|
1288
|
+
getLifecycleStatus(agentId: string): LifecycleStatus | null {
|
|
1289
|
+
const deployment = this.agentDeployments.get(agentId);
|
|
1290
|
+
if (!deployment) return null;
|
|
1291
|
+
|
|
1292
|
+
return {
|
|
1293
|
+
agentId: deployment.agentId,
|
|
1294
|
+
agentName: deployment.agentName,
|
|
1295
|
+
currentStage: deployment.currentStage,
|
|
1296
|
+
status: deployment.status,
|
|
1297
|
+
stageHistory: deployment.stageHistory,
|
|
1298
|
+
metrics: deployment.metrics,
|
|
1299
|
+
uptime: Date.now() - deployment.startTime,
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
getAllLifecycleStatuses(): LifecycleStatus[] {
|
|
1304
|
+
const statuses: LifecycleStatus[] = [];
|
|
1305
|
+
|
|
1306
|
+
for (const [agentId] of this.agentDeployments) {
|
|
1307
|
+
const status = this.getLifecycleStatus(agentId);
|
|
1308
|
+
if (status) {
|
|
1309
|
+
statuses.push(status);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
return statuses;
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// -----------------------------------------------------------------------
|
|
1317
|
+
// Utility
|
|
1318
|
+
// -----------------------------------------------------------------------
|
|
1319
|
+
|
|
1320
|
+
private delay(ms: number): Promise<void> {
|
|
1321
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1322
|
+
}
|
|
1323
|
+
}
|