@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/system.ts
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Baseline Autonomy System — Layer 6
|
|
3
|
+
*
|
|
4
|
+
* Autonomous AI operations layer of the Baseline Protocol providing
|
|
5
|
+
* autonomous agents framework, trust & safety governance, flag system
|
|
6
|
+
* & resolution, and challenge system integration.
|
|
7
|
+
*
|
|
8
|
+
* @license Apache-2.0
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { randomUUID } from 'node:crypto';
|
|
12
|
+
import { EventEmitter } from 'node:events';
|
|
13
|
+
|
|
14
|
+
// ─── Types ──────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
export type SafetyLevel = 'safe' | 'warning' | 'critical' | 'high' | 'medium';
|
|
17
|
+
export type AgentStatus = 'idle' | 'initializing' | 'ready' | 'executing' | 'error' | 'suspended' | 'shutting_down' | 'shutdown';
|
|
18
|
+
export type TaskType = 'data_processing' | 'decision_making' | 'system_operation' | 'communication';
|
|
19
|
+
|
|
20
|
+
export interface AgentConfig {
|
|
21
|
+
id?: string;
|
|
22
|
+
name?: string;
|
|
23
|
+
type?: string;
|
|
24
|
+
capabilities?: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface AgentTask {
|
|
28
|
+
id: string;
|
|
29
|
+
type: TaskType;
|
|
30
|
+
description: string;
|
|
31
|
+
startTime: number;
|
|
32
|
+
trustRequirement?: number;
|
|
33
|
+
requiresElevatedPermissions?: boolean;
|
|
34
|
+
involvesSensitiveData?: boolean;
|
|
35
|
+
dataType?: string;
|
|
36
|
+
potentialSystemImpact?: 'low' | 'medium' | 'high';
|
|
37
|
+
dataSize?: number;
|
|
38
|
+
options?: DecisionOption[];
|
|
39
|
+
operation?: string;
|
|
40
|
+
system?: string;
|
|
41
|
+
message?: string;
|
|
42
|
+
recipient?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface DecisionOption {
|
|
46
|
+
risk?: 'low' | 'medium' | 'high';
|
|
47
|
+
efficiency?: 'low' | 'medium' | 'high';
|
|
48
|
+
cost?: 'low' | 'medium' | 'high';
|
|
49
|
+
impact?: 'low' | 'medium' | 'high';
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface TaskResult {
|
|
54
|
+
taskId: string;
|
|
55
|
+
type: string;
|
|
56
|
+
status: string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface PerformanceMetrics {
|
|
61
|
+
tasksCompleted: number;
|
|
62
|
+
tasksFailed: number;
|
|
63
|
+
averageResponseTime: number;
|
|
64
|
+
trustScore: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface LearningModel {
|
|
68
|
+
decisionHistory: Array<{ task: AgentTask; result: TaskResult; timestamp: number }>;
|
|
69
|
+
performanceHistory: Array<{
|
|
70
|
+
taskId: string;
|
|
71
|
+
success: boolean;
|
|
72
|
+
responseTime: number;
|
|
73
|
+
trustScore: number;
|
|
74
|
+
}>;
|
|
75
|
+
adaptationRate: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ViolationFlag {
|
|
79
|
+
id: string;
|
|
80
|
+
type: string;
|
|
81
|
+
severity: string;
|
|
82
|
+
taskId: string;
|
|
83
|
+
agentId: string;
|
|
84
|
+
description: string;
|
|
85
|
+
timestamp: number;
|
|
86
|
+
status: 'open' | 'resolved' | 'requires_manual_resolution';
|
|
87
|
+
resolvedAt?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface SafetyAlert {
|
|
91
|
+
agentId: string;
|
|
92
|
+
level: string;
|
|
93
|
+
message: string;
|
|
94
|
+
timestamp: number;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface SafetyProtocol {
|
|
98
|
+
id: string;
|
|
99
|
+
name: string;
|
|
100
|
+
description: string;
|
|
101
|
+
rules: string[];
|
|
102
|
+
enforcement: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface AgentType {
|
|
106
|
+
id: string;
|
|
107
|
+
name: string;
|
|
108
|
+
description: string;
|
|
109
|
+
capabilities: string[];
|
|
110
|
+
trustRequirement: number;
|
|
111
|
+
safetyLevel: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface AgentStatusReport {
|
|
115
|
+
id: string;
|
|
116
|
+
name: string;
|
|
117
|
+
type: string;
|
|
118
|
+
status: AgentStatus;
|
|
119
|
+
trustScore: number;
|
|
120
|
+
safetyLevel: SafetyLevel;
|
|
121
|
+
currentTask: AgentTask | null;
|
|
122
|
+
performanceMetrics: PerformanceMetrics;
|
|
123
|
+
capabilities: string[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface SystemStatus {
|
|
127
|
+
systemId: string;
|
|
128
|
+
version: string;
|
|
129
|
+
status: string;
|
|
130
|
+
agentCount: number;
|
|
131
|
+
agentTypes: number;
|
|
132
|
+
safetyProtocols: number;
|
|
133
|
+
flagCount: number;
|
|
134
|
+
challengeSystem: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ─── Autonomous Agent ───────────────────────────────────────────────
|
|
138
|
+
|
|
139
|
+
export class AutonomousAgent extends EventEmitter {
|
|
140
|
+
readonly id: string;
|
|
141
|
+
readonly name: string;
|
|
142
|
+
readonly type: string;
|
|
143
|
+
capabilities: string[];
|
|
144
|
+
status: AgentStatus = 'idle';
|
|
145
|
+
taskQueue: AgentTask[] = [];
|
|
146
|
+
currentTask: AgentTask | null = null;
|
|
147
|
+
trustScore = 100;
|
|
148
|
+
safetyLevel: SafetyLevel = 'high';
|
|
149
|
+
learningRate = 0.1;
|
|
150
|
+
performanceMetrics: PerformanceMetrics = {
|
|
151
|
+
tasksCompleted: 0,
|
|
152
|
+
tasksFailed: 0,
|
|
153
|
+
averageResponseTime: 0,
|
|
154
|
+
trustScore: 100,
|
|
155
|
+
};
|
|
156
|
+
learningModel: LearningModel = {
|
|
157
|
+
decisionHistory: [],
|
|
158
|
+
performanceHistory: [],
|
|
159
|
+
adaptationRate: 0.1,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
private monitoringInterval: ReturnType<typeof setInterval> | null = null;
|
|
163
|
+
|
|
164
|
+
constructor(config: AgentConfig = {}) {
|
|
165
|
+
super();
|
|
166
|
+
this.id = config.id || randomUUID();
|
|
167
|
+
this.name = config.name || `Agent_${this.id.slice(0, 8)}`;
|
|
168
|
+
this.type = config.type || 'general';
|
|
169
|
+
this.capabilities = config.capabilities || [];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async initialize(): Promise<void> {
|
|
173
|
+
try {
|
|
174
|
+
this.status = 'initializing';
|
|
175
|
+
await this.loadCapabilities();
|
|
176
|
+
this.learningModel.adaptationRate = this.learningRate;
|
|
177
|
+
this.startMonitoring();
|
|
178
|
+
this.status = 'ready';
|
|
179
|
+
} catch (error) {
|
|
180
|
+
this.status = 'error';
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async loadCapabilities(): Promise<void> {
|
|
186
|
+
if (this.capabilities.length === 0) {
|
|
187
|
+
this.capabilities = ['task_execution', 'decision_making', 'learning'];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private startMonitoring(): void {
|
|
192
|
+
this.monitoringInterval = setInterval(() => {
|
|
193
|
+
this.updatePerformanceMetrics();
|
|
194
|
+
this.checkSafetyProtocols();
|
|
195
|
+
}, 30000);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async executeTask(task: AgentTask): Promise<TaskResult> {
|
|
199
|
+
try {
|
|
200
|
+
this.status = 'executing';
|
|
201
|
+
this.currentTask = task;
|
|
202
|
+
|
|
203
|
+
await this.validateTaskSafety(task);
|
|
204
|
+
const result = await this.performTask(task);
|
|
205
|
+
this.updateTaskMetrics(task);
|
|
206
|
+
await this.learnFromExecution(task, result);
|
|
207
|
+
|
|
208
|
+
this.status = 'ready';
|
|
209
|
+
this.currentTask = null;
|
|
210
|
+
return result;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
this.status = 'error';
|
|
213
|
+
this.currentTask = null;
|
|
214
|
+
await this.handleTaskError(task, error as Error);
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private async validateTaskSafety(task: AgentTask): Promise<void> {
|
|
220
|
+
const safetyCheck = await this.runSafetyCheck(task);
|
|
221
|
+
|
|
222
|
+
if (!safetyCheck.passed) {
|
|
223
|
+
throw new Error(`Safety violation detected: ${safetyCheck.violation}`);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (task.trustRequirement && this.trustScore < task.trustRequirement) {
|
|
227
|
+
throw new Error(`Insufficient trust score: ${this.trustScore} < ${task.trustRequirement}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private async runSafetyCheck(
|
|
232
|
+
task: AgentTask,
|
|
233
|
+
): Promise<{ passed: boolean; violation?: string; details?: string }> {
|
|
234
|
+
const safetyRules = [
|
|
235
|
+
'no_harmful_actions',
|
|
236
|
+
'no_unauthorized_access',
|
|
237
|
+
'no_data_breach',
|
|
238
|
+
'no_system_damage',
|
|
239
|
+
];
|
|
240
|
+
|
|
241
|
+
for (const rule of safetyRules) {
|
|
242
|
+
const ruleCheck = this.checkSafetyRule(rule, task);
|
|
243
|
+
if (!ruleCheck.passed) {
|
|
244
|
+
return { passed: false, violation: rule, details: ruleCheck.details };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { passed: true };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private checkSafetyRule(
|
|
252
|
+
rule: string,
|
|
253
|
+
task: AgentTask,
|
|
254
|
+
): { passed: boolean; details?: string } {
|
|
255
|
+
switch (rule) {
|
|
256
|
+
case 'no_harmful_actions': {
|
|
257
|
+
const harmfulKeywords = ['delete', 'destroy', 'harm', 'damage', 'corrupt'];
|
|
258
|
+
const desc = task.description.toLowerCase();
|
|
259
|
+
for (const keyword of harmfulKeywords) {
|
|
260
|
+
if (desc.includes(keyword)) {
|
|
261
|
+
return { passed: false, details: `Task contains potentially harmful keyword: ${keyword}` };
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return { passed: true };
|
|
265
|
+
}
|
|
266
|
+
case 'no_unauthorized_access':
|
|
267
|
+
if (task.requiresElevatedPermissions && !this.capabilities.includes('elevated_permissions')) {
|
|
268
|
+
return { passed: false, details: 'Task requires elevated permissions not available to agent' };
|
|
269
|
+
}
|
|
270
|
+
return { passed: true };
|
|
271
|
+
case 'no_data_breach':
|
|
272
|
+
if (task.involvesSensitiveData && !this.capabilities.includes(`data_access_${task.dataType}`)) {
|
|
273
|
+
return { passed: false, details: 'Task involves sensitive data without proper permissions' };
|
|
274
|
+
}
|
|
275
|
+
return { passed: true };
|
|
276
|
+
case 'no_system_damage':
|
|
277
|
+
if (task.potentialSystemImpact === 'high' && this.trustScore < 80) {
|
|
278
|
+
return { passed: false, details: 'High system impact task requires higher trust score' };
|
|
279
|
+
}
|
|
280
|
+
return { passed: true };
|
|
281
|
+
default:
|
|
282
|
+
return { passed: true };
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private async performTask(task: AgentTask): Promise<TaskResult> {
|
|
287
|
+
switch (task.type) {
|
|
288
|
+
case 'data_processing':
|
|
289
|
+
return { taskId: task.id, type: task.type, status: 'completed', dataProcessed: task.dataSize || 0, result: 'Data processed successfully' };
|
|
290
|
+
case 'decision_making': {
|
|
291
|
+
const decision = this.evaluateOptions(task.options || []);
|
|
292
|
+
return {
|
|
293
|
+
taskId: task.id,
|
|
294
|
+
type: task.type,
|
|
295
|
+
status: 'completed',
|
|
296
|
+
decision,
|
|
297
|
+
confidence: this.calculateConfidence(),
|
|
298
|
+
reasoning: `Decision based on risk/efficiency/cost/impact evaluation. Trust: ${this.trustScore}, experience: ${this.performanceMetrics.tasksCompleted} tasks.`,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
case 'system_operation':
|
|
302
|
+
return { taskId: task.id, type: task.type, status: 'completed', operation: task.operation, systemAffected: task.system, result: 'System operation completed' };
|
|
303
|
+
case 'communication':
|
|
304
|
+
return { taskId: task.id, type: task.type, status: 'completed', message: task.message, recipient: task.recipient, result: 'Communication sent' };
|
|
305
|
+
default:
|
|
306
|
+
throw new Error(`Unknown task type: ${task.type}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private evaluateOptions(options: DecisionOption[]): DecisionOption | string {
|
|
311
|
+
if (!options || options.length === 0) return 'no_decision';
|
|
312
|
+
|
|
313
|
+
let bestOption = options[0];
|
|
314
|
+
let bestScore = 0;
|
|
315
|
+
|
|
316
|
+
for (const option of options) {
|
|
317
|
+
const score = this.calculateOptionScore(option);
|
|
318
|
+
if (score > bestScore) {
|
|
319
|
+
bestScore = score;
|
|
320
|
+
bestOption = option;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return bestOption;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private calculateOptionScore(option: DecisionOption): number {
|
|
328
|
+
let score = 0;
|
|
329
|
+
const riskScores: Record<string, number> = { low: 30, medium: 20, high: 10 };
|
|
330
|
+
const efficiencyScores: Record<string, number> = { high: 25, medium: 15, low: 5 };
|
|
331
|
+
const costScores: Record<string, number> = { low: 25, medium: 15, high: 5 };
|
|
332
|
+
const impactScores: Record<string, number> = { high: 20, medium: 10, low: 5 };
|
|
333
|
+
|
|
334
|
+
if (option.risk) score += riskScores[option.risk] || 0;
|
|
335
|
+
if (option.efficiency) score += efficiencyScores[option.efficiency] || 0;
|
|
336
|
+
if (option.cost) score += costScores[option.cost] || 0;
|
|
337
|
+
if (option.impact) score += impactScores[option.impact] || 0;
|
|
338
|
+
|
|
339
|
+
return score;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private calculateConfidence(): number {
|
|
343
|
+
const baseConfidence = 70;
|
|
344
|
+
const experienceBonus = Math.min(this.performanceMetrics.tasksCompleted * 0.5, 20);
|
|
345
|
+
const trustBonus = Math.min(this.trustScore * 0.1, 10);
|
|
346
|
+
return Math.min(baseConfidence + experienceBonus + trustBonus, 100);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private updateTaskMetrics(task: AgentTask): void {
|
|
350
|
+
this.performanceMetrics.tasksCompleted++;
|
|
351
|
+
const responseTime = Date.now() - task.startTime;
|
|
352
|
+
const currentAvg = this.performanceMetrics.averageResponseTime;
|
|
353
|
+
const totalTasks = this.performanceMetrics.tasksCompleted;
|
|
354
|
+
this.performanceMetrics.averageResponseTime =
|
|
355
|
+
(currentAvg * (totalTasks - 1) + responseTime) / totalTasks;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
private async learnFromExecution(task: AgentTask, result: TaskResult): Promise<void> {
|
|
359
|
+
this.learningModel.decisionHistory.push({ task, result, timestamp: Date.now() });
|
|
360
|
+
this.learningModel.performanceHistory.push({
|
|
361
|
+
taskId: task.id,
|
|
362
|
+
success: result.status === 'completed',
|
|
363
|
+
responseTime: Date.now() - task.startTime,
|
|
364
|
+
trustScore: this.trustScore,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
if (result.status === 'completed') {
|
|
368
|
+
this.learningRate = Math.min(this.learningRate * 1.01, 0.5);
|
|
369
|
+
} else {
|
|
370
|
+
this.learningRate = Math.max(this.learningRate * 0.99, 0.01);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
private async handleTaskError(task: AgentTask, error: Error): Promise<void> {
|
|
375
|
+
this.trustScore = Math.max(this.trustScore - 5, 0);
|
|
376
|
+
this.performanceMetrics.tasksFailed++;
|
|
377
|
+
|
|
378
|
+
if (error.message.includes('Safety violation')) {
|
|
379
|
+
const flag: ViolationFlag = {
|
|
380
|
+
id: randomUUID(),
|
|
381
|
+
type: 'safety_violation',
|
|
382
|
+
severity: 'high',
|
|
383
|
+
taskId: task.id,
|
|
384
|
+
agentId: this.id,
|
|
385
|
+
description: error.message,
|
|
386
|
+
timestamp: Date.now(),
|
|
387
|
+
status: 'open',
|
|
388
|
+
};
|
|
389
|
+
this.emit('violation_flag', flag);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private updatePerformanceMetrics(): void {
|
|
394
|
+
const total = this.performanceMetrics.tasksCompleted + this.performanceMetrics.tasksFailed;
|
|
395
|
+
if (total === 0) return;
|
|
396
|
+
|
|
397
|
+
const successRate = this.performanceMetrics.tasksCompleted / total;
|
|
398
|
+
if (successRate > 0.9) {
|
|
399
|
+
this.trustScore = Math.min(this.trustScore + 1, 100);
|
|
400
|
+
} else if (successRate < 0.7) {
|
|
401
|
+
this.trustScore = Math.max(this.trustScore - 1, 0);
|
|
402
|
+
}
|
|
403
|
+
this.performanceMetrics.trustScore = this.trustScore;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private checkSafetyProtocols(): void {
|
|
407
|
+
if (this.trustScore < 20) {
|
|
408
|
+
this.safetyLevel = 'critical';
|
|
409
|
+
this.emit('safety_alert', {
|
|
410
|
+
agentId: this.id,
|
|
411
|
+
level: 'critical',
|
|
412
|
+
message: 'Agent trust score critically low',
|
|
413
|
+
timestamp: Date.now(),
|
|
414
|
+
} as SafetyAlert);
|
|
415
|
+
} else if (this.trustScore < 50) {
|
|
416
|
+
this.safetyLevel = 'warning';
|
|
417
|
+
} else {
|
|
418
|
+
this.safetyLevel = 'safe';
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
getStatus(): AgentStatusReport {
|
|
423
|
+
return {
|
|
424
|
+
id: this.id,
|
|
425
|
+
name: this.name,
|
|
426
|
+
type: this.type,
|
|
427
|
+
status: this.status,
|
|
428
|
+
trustScore: this.trustScore,
|
|
429
|
+
safetyLevel: this.safetyLevel,
|
|
430
|
+
currentTask: this.currentTask,
|
|
431
|
+
performanceMetrics: this.performanceMetrics,
|
|
432
|
+
capabilities: this.capabilities,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async shutdown(): Promise<void> {
|
|
437
|
+
this.status = 'shutting_down';
|
|
438
|
+
if (this.monitoringInterval) {
|
|
439
|
+
clearInterval(this.monitoringInterval);
|
|
440
|
+
this.monitoringInterval = null;
|
|
441
|
+
}
|
|
442
|
+
if (this.currentTask) {
|
|
443
|
+
await this.handleTaskError(this.currentTask, new Error('Agent shutdown'));
|
|
444
|
+
}
|
|
445
|
+
this.status = 'shutdown';
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// ─── Baseline Autonomy System ───────────────────────────────────────
|
|
450
|
+
|
|
451
|
+
export class BaselineAutonomySystem extends EventEmitter {
|
|
452
|
+
readonly systemId: string;
|
|
453
|
+
readonly version = '1.0.0';
|
|
454
|
+
status = 'initializing';
|
|
455
|
+
private agents = new Map<string, AutonomousAgent>();
|
|
456
|
+
private agentTypes = new Map<string, AgentType>();
|
|
457
|
+
private safetyProtocols = new Map<string, SafetyProtocol>();
|
|
458
|
+
private flagSystem = new Map<string, ViolationFlag[]>();
|
|
459
|
+
|
|
460
|
+
constructor(config: { systemId?: string } = {}) {
|
|
461
|
+
super();
|
|
462
|
+
this.systemId = config.systemId || 'baseline-autonomy-system';
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async initialize(): Promise<void> {
|
|
466
|
+
try {
|
|
467
|
+
await this.initializeSafetyProtocols();
|
|
468
|
+
await this.initializeAgentTypes();
|
|
469
|
+
this.initializeFlagSystem();
|
|
470
|
+
this.status = 'operational';
|
|
471
|
+
this.emit('system_ready');
|
|
472
|
+
} catch (error) {
|
|
473
|
+
this.status = 'error';
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
private async initializeSafetyProtocols(): Promise<void> {
|
|
479
|
+
const protocols: SafetyProtocol[] = [
|
|
480
|
+
{
|
|
481
|
+
id: 'no_harmful_actions',
|
|
482
|
+
name: 'No Harmful Actions',
|
|
483
|
+
description: 'Prevent any actions that could cause harm',
|
|
484
|
+
rules: ['no_physical_harm', 'no_emotional_harm', 'no_system_harm'],
|
|
485
|
+
enforcement: 'strict',
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
id: 'data_privacy',
|
|
489
|
+
name: 'Data Privacy Protection',
|
|
490
|
+
description: 'Protect user data and privacy',
|
|
491
|
+
rules: ['no_unauthorized_access', 'no_data_breach', 'encryption_required'],
|
|
492
|
+
enforcement: 'strict',
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
id: 'system_integrity',
|
|
496
|
+
name: 'System Integrity',
|
|
497
|
+
description: 'Maintain system stability and security',
|
|
498
|
+
rules: ['no_unauthorized_changes', 'backup_before_modifications', 'rollback_capability'],
|
|
499
|
+
enforcement: 'strict',
|
|
500
|
+
},
|
|
501
|
+
];
|
|
502
|
+
|
|
503
|
+
for (const protocol of protocols) {
|
|
504
|
+
this.safetyProtocols.set(protocol.id, protocol);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private async initializeAgentTypes(): Promise<void> {
|
|
509
|
+
const types: AgentType[] = [
|
|
510
|
+
{
|
|
511
|
+
id: 'general',
|
|
512
|
+
name: 'General Purpose Agent',
|
|
513
|
+
description: 'General purpose autonomous agent',
|
|
514
|
+
capabilities: ['task_execution', 'decision_making', 'learning'],
|
|
515
|
+
trustRequirement: 50,
|
|
516
|
+
safetyLevel: 'medium',
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
id: 'data_processor',
|
|
520
|
+
name: 'Data Processing Agent',
|
|
521
|
+
description: 'Specialized in data processing tasks',
|
|
522
|
+
capabilities: ['data_processing', 'data_analysis', 'data_validation'],
|
|
523
|
+
trustRequirement: 60,
|
|
524
|
+
safetyLevel: 'high',
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
id: 'decision_maker',
|
|
528
|
+
name: 'Decision Making Agent',
|
|
529
|
+
description: 'Specialized in complex decision making',
|
|
530
|
+
capabilities: ['decision_making', 'risk_assessment', 'option_evaluation'],
|
|
531
|
+
trustRequirement: 80,
|
|
532
|
+
safetyLevel: 'high',
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
id: 'system_operator',
|
|
536
|
+
name: 'System Operation Agent',
|
|
537
|
+
description: 'Specialized in system operations',
|
|
538
|
+
capabilities: ['system_operation', 'system_monitoring', 'system_maintenance'],
|
|
539
|
+
trustRequirement: 90,
|
|
540
|
+
safetyLevel: 'critical',
|
|
541
|
+
},
|
|
542
|
+
];
|
|
543
|
+
|
|
544
|
+
for (const type of types) {
|
|
545
|
+
this.agentTypes.set(type.id, type);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
private initializeFlagSystem(): void {
|
|
550
|
+
const categories = [
|
|
551
|
+
'safety_violation',
|
|
552
|
+
'trust_violation',
|
|
553
|
+
'performance_issue',
|
|
554
|
+
'system_error',
|
|
555
|
+
'security_breach',
|
|
556
|
+
];
|
|
557
|
+
for (const category of categories) {
|
|
558
|
+
this.flagSystem.set(category, []);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async createAgent(config: AgentConfig): Promise<AutonomousAgent> {
|
|
563
|
+
this.validateAgentConfig(config);
|
|
564
|
+
const agent = new AutonomousAgent(config);
|
|
565
|
+
await agent.initialize();
|
|
566
|
+
this.agents.set(agent.id, agent);
|
|
567
|
+
this.setupAgentEventListeners(agent);
|
|
568
|
+
this.emit('agent_created', agent);
|
|
569
|
+
return agent;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
private validateAgentConfig(config: AgentConfig): void {
|
|
573
|
+
if (!config.type) throw new Error('Agent type is required');
|
|
574
|
+
if (!this.agentTypes.has(config.type)) throw new Error(`Unknown agent type: ${config.type}`);
|
|
575
|
+
if (!config.name || config.name.trim() === '') throw new Error('Agent name is required');
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
private setupAgentEventListeners(agent: AutonomousAgent): void {
|
|
579
|
+
agent.on('violation_flag', (flag: ViolationFlag) => this.handleViolationFlag(flag));
|
|
580
|
+
agent.on('safety_alert', (alert: SafetyAlert) => this.handleSafetyAlert(alert));
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private async handleViolationFlag(flag: ViolationFlag): Promise<void> {
|
|
584
|
+
const category = flag.type;
|
|
585
|
+
if (!this.flagSystem.has(category)) {
|
|
586
|
+
this.flagSystem.set(category, []);
|
|
587
|
+
}
|
|
588
|
+
this.flagSystem.get(category)!.push(flag);
|
|
589
|
+
this.emit('flag_generated', flag);
|
|
590
|
+
await this.attemptFlagResolution(flag);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
private async handleSafetyAlert(alert: SafetyAlert): Promise<void> {
|
|
594
|
+
this.emit('safety_alert', alert);
|
|
595
|
+
|
|
596
|
+
if (alert.level === 'critical') {
|
|
597
|
+
const agent = this.agents.get(alert.agentId);
|
|
598
|
+
if (agent) {
|
|
599
|
+
agent.status = 'suspended';
|
|
600
|
+
this.emit('agent_suspended', agent);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
private async attemptFlagResolution(flag: ViolationFlag): Promise<void> {
|
|
606
|
+
try {
|
|
607
|
+
if (flag.type === 'safety_violation') {
|
|
608
|
+
const agent = this.agents.get(flag.agentId);
|
|
609
|
+
if (agent) agent.safetyLevel = 'high';
|
|
610
|
+
} else if (flag.type === 'trust_violation') {
|
|
611
|
+
const agent = this.agents.get(flag.agentId);
|
|
612
|
+
if (agent) agent.trustScore = Math.max(agent.trustScore - 10, 0);
|
|
613
|
+
}
|
|
614
|
+
flag.status = 'resolved';
|
|
615
|
+
flag.resolvedAt = Date.now();
|
|
616
|
+
} catch {
|
|
617
|
+
flag.status = 'requires_manual_resolution';
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
getSystemStatus(): SystemStatus {
|
|
622
|
+
let flagCount = 0;
|
|
623
|
+
for (const [, flags] of this.flagSystem) {
|
|
624
|
+
flagCount += flags.length;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
return {
|
|
628
|
+
systemId: this.systemId,
|
|
629
|
+
version: this.version,
|
|
630
|
+
status: this.status,
|
|
631
|
+
agentCount: this.agents.size,
|
|
632
|
+
agentTypes: this.agentTypes.size,
|
|
633
|
+
safetyProtocols: this.safetyProtocols.size,
|
|
634
|
+
flagCount,
|
|
635
|
+
challengeSystem: 'inactive',
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
getAllAgents(): AgentStatusReport[] {
|
|
640
|
+
return Array.from(this.agents.values()).map((agent) => agent.getStatus());
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
getAgent(agentId: string): AgentStatusReport | null {
|
|
644
|
+
const agent = this.agents.get(agentId);
|
|
645
|
+
return agent ? agent.getStatus() : null;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
async shutdown(): Promise<void> {
|
|
649
|
+
this.status = 'shutting_down';
|
|
650
|
+
const shutdownPromises = Array.from(this.agents.values()).map((agent) => agent.shutdown());
|
|
651
|
+
await Promise.all(shutdownPromises);
|
|
652
|
+
this.status = 'shutdown';
|
|
653
|
+
}
|
|
654
|
+
}
|