@cogitator-ai/self-modifying 17.0.17 → 18.0.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.
Files changed (114) hide show
  1. package/README.md +112 -48
  2. package/dist/architecture-evolution/capability-analyzer.d.ts.map +1 -1
  3. package/dist/architecture-evolution/capability-analyzer.js +44 -40
  4. package/dist/architecture-evolution/capability-analyzer.js.map +1 -1
  5. package/dist/architecture-evolution/evolution-strategy.d.ts.map +1 -1
  6. package/dist/architecture-evolution/evolution-strategy.js +4 -2
  7. package/dist/architecture-evolution/evolution-strategy.js.map +1 -1
  8. package/dist/architecture-evolution/index.d.ts +2 -2
  9. package/dist/architecture-evolution/index.d.ts.map +1 -1
  10. package/dist/architecture-evolution/index.js +1 -1
  11. package/dist/architecture-evolution/index.js.map +1 -1
  12. package/dist/architecture-evolution/parameter-optimizer.d.ts +5 -4
  13. package/dist/architecture-evolution/parameter-optimizer.d.ts.map +1 -1
  14. package/dist/architecture-evolution/parameter-optimizer.js +62 -34
  15. package/dist/architecture-evolution/parameter-optimizer.js.map +1 -1
  16. package/dist/architecture-evolution/prompts.d.ts +21 -2
  17. package/dist/architecture-evolution/prompts.d.ts.map +1 -1
  18. package/dist/architecture-evolution/prompts.js +189 -37
  19. package/dist/architecture-evolution/prompts.js.map +1 -1
  20. package/dist/constraints/modification-validator.d.ts +1 -0
  21. package/dist/constraints/modification-validator.d.ts.map +1 -1
  22. package/dist/constraints/modification-validator.js +45 -18
  23. package/dist/constraints/modification-validator.js.map +1 -1
  24. package/dist/constraints/rollback-manager.d.ts +1 -0
  25. package/dist/constraints/rollback-manager.d.ts.map +1 -1
  26. package/dist/constraints/rollback-manager.js +25 -1
  27. package/dist/constraints/rollback-manager.js.map +1 -1
  28. package/dist/events/event-emitter.d.ts +0 -1
  29. package/dist/events/event-emitter.d.ts.map +1 -1
  30. package/dist/events/event-emitter.js +6 -10
  31. package/dist/events/event-emitter.js.map +1 -1
  32. package/dist/index.d.ts +4 -4
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +3 -3
  35. package/dist/index.js.map +1 -1
  36. package/dist/meta-reasoning/meta-reasoner.d.ts +4 -1
  37. package/dist/meta-reasoning/meta-reasoner.d.ts.map +1 -1
  38. package/dist/meta-reasoning/meta-reasoner.js +120 -43
  39. package/dist/meta-reasoning/meta-reasoner.js.map +1 -1
  40. package/dist/meta-reasoning/observation-collector.d.ts.map +1 -1
  41. package/dist/meta-reasoning/observation-collector.js +6 -2
  42. package/dist/meta-reasoning/observation-collector.js.map +1 -1
  43. package/dist/meta-reasoning/prompts.d.ts +10 -14
  44. package/dist/meta-reasoning/prompts.d.ts.map +1 -1
  45. package/dist/meta-reasoning/prompts.js +108 -33
  46. package/dist/meta-reasoning/prompts.js.map +1 -1
  47. package/dist/meta-reasoning/strategy-selector.d.ts.map +1 -1
  48. package/dist/meta-reasoning/strategy-selector.js +6 -0
  49. package/dist/meta-reasoning/strategy-selector.js.map +1 -1
  50. package/dist/self-modifying-agent.d.ts +88 -15
  51. package/dist/self-modifying-agent.d.ts.map +1 -1
  52. package/dist/self-modifying-agent.js +583 -376
  53. package/dist/self-modifying-agent.js.map +1 -1
  54. package/dist/tool-generation/gap-analyzer.d.ts +0 -1
  55. package/dist/tool-generation/gap-analyzer.d.ts.map +1 -1
  56. package/dist/tool-generation/gap-analyzer.js +16 -15
  57. package/dist/tool-generation/gap-analyzer.js.map +1 -1
  58. package/dist/tool-generation/generated-tool-store.d.ts.map +1 -1
  59. package/dist/tool-generation/generated-tool-store.js +90 -10
  60. package/dist/tool-generation/generated-tool-store.js.map +1 -1
  61. package/dist/tool-generation/index.d.ts +3 -3
  62. package/dist/tool-generation/index.d.ts.map +1 -1
  63. package/dist/tool-generation/index.js +2 -2
  64. package/dist/tool-generation/index.js.map +1 -1
  65. package/dist/tool-generation/prompts.d.ts +4 -3
  66. package/dist/tool-generation/prompts.d.ts.map +1 -1
  67. package/dist/tool-generation/prompts.js +96 -18
  68. package/dist/tool-generation/prompts.js.map +1 -1
  69. package/dist/tool-generation/tool-generator.d.ts +6 -6
  70. package/dist/tool-generation/tool-generator.d.ts.map +1 -1
  71. package/dist/tool-generation/tool-generator.js +88 -23
  72. package/dist/tool-generation/tool-generator.js.map +1 -1
  73. package/dist/tool-generation/tool-sandbox.d.ts +10 -11
  74. package/dist/tool-generation/tool-sandbox.d.ts.map +1 -1
  75. package/dist/tool-generation/tool-sandbox.js +346 -156
  76. package/dist/tool-generation/tool-sandbox.js.map +1 -1
  77. package/dist/tool-generation/tool-validator.d.ts +4 -8
  78. package/dist/tool-generation/tool-validator.d.ts.map +1 -1
  79. package/dist/tool-generation/tool-validator.js +74 -28
  80. package/dist/tool-generation/tool-validator.js.map +1 -1
  81. package/dist/utils/index.d.ts.map +1 -1
  82. package/dist/utils/index.js +30 -5
  83. package/dist/utils/index.js.map +1 -1
  84. package/dist/utils/llm-helper.d.ts.map +1 -1
  85. package/dist/utils/llm-helper.js +1 -0
  86. package/dist/utils/llm-helper.js.map +1 -1
  87. package/package.json +3 -12
  88. package/src/__tests__/component-regressions.test.ts +599 -0
  89. package/src/__tests__/meta-reasoning.test.ts +5 -1
  90. package/src/__tests__/sandbox-security.test.ts +186 -0
  91. package/src/__tests__/self-modifying-agent.test.ts +405 -11
  92. package/src/architecture-evolution/capability-analyzer.ts +63 -44
  93. package/src/architecture-evolution/evolution-strategy.ts +5 -2
  94. package/src/architecture-evolution/index.ts +2 -0
  95. package/src/architecture-evolution/parameter-optimizer.ts +82 -40
  96. package/src/architecture-evolution/prompts.ts +224 -42
  97. package/src/constraints/modification-validator.ts +48 -21
  98. package/src/constraints/rollback-manager.ts +20 -1
  99. package/src/events/event-emitter.ts +6 -15
  100. package/src/index.ts +14 -0
  101. package/src/meta-reasoning/meta-reasoner.ts +124 -52
  102. package/src/meta-reasoning/observation-collector.ts +6 -2
  103. package/src/meta-reasoning/prompts.ts +131 -56
  104. package/src/meta-reasoning/strategy-selector.ts +6 -0
  105. package/src/self-modifying-agent.ts +797 -413
  106. package/src/tool-generation/gap-analyzer.ts +21 -19
  107. package/src/tool-generation/generated-tool-store.ts +97 -10
  108. package/src/tool-generation/index.ts +13 -3
  109. package/src/tool-generation/prompts.ts +120 -18
  110. package/src/tool-generation/tool-generator.ts +120 -26
  111. package/src/tool-generation/tool-sandbox.ts +408 -206
  112. package/src/tool-generation/tool-validator.ts +90 -37
  113. package/src/utils/index.ts +29 -5
  114. package/src/utils/llm-helper.ts +1 -0
@@ -1,29 +1,41 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { DEFAULT_META_REASONING_CONFIG } from '@cogitator-ai/types';
1
3
  import { SelfModifyingEventEmitter } from './events/index.js';
2
4
  import { GapAnalyzer, ToolGenerator, InMemoryGeneratedToolStore } from './tool-generation/index.js';
3
5
  import { MetaReasoner } from './meta-reasoning/index.js';
4
- import { ParameterOptimizer, CapabilityAnalyzer } from './architecture-evolution/index.js';
6
+ import { ParameterOptimizer } from './architecture-evolution/index.js';
5
7
  import { ModificationValidator, RollbackManager, DEFAULT_SAFETY_CONSTRAINTS, DEFAULT_CAPABILITY_CONSTRAINTS, DEFAULT_RESOURCE_CONSTRAINTS, } from './constraints/index.js';
8
+ const DEFAULT_TOOL_TIMEOUT_MS = 30_000;
9
+ function resolveModelName(model, provider) {
10
+ if (!model)
11
+ return 'default';
12
+ const prefix = `${provider}/`;
13
+ return model.startsWith(prefix) ? model.slice(prefix.length) : model;
14
+ }
15
+ const DEFAULT_MAX_TOOL_ROUNDS = 10;
6
16
  export class SelfModifyingAgent {
7
17
  agent;
8
18
  llm;
19
+ modelName;
9
20
  config;
10
21
  emitter = new SelfModifyingEventEmitter();
22
+ listenerWrappers = new Map();
11
23
  gapAnalyzer;
12
24
  toolGenerator;
13
25
  toolStore;
14
26
  metaReasoner;
15
27
  parameterOptimizer;
16
- capabilityAnalyzer;
17
28
  modificationValidator;
18
29
  rollbackManager;
19
30
  currentContext = null;
20
- isInitialized = false;
31
+ runQueue = Promise.resolve();
21
32
  constructor(options) {
22
33
  this.agent = options.agent;
23
34
  this.llm = options.llm;
35
+ this.modelName = resolveModelName(options.agent.model, options.llm.provider);
24
36
  this.config = this.mergeConfig(options.config);
25
37
  const toolGenConfig = this.config.toolGeneration;
26
- const agentModel = this.agent.model;
38
+ const agentModel = this.modelName;
27
39
  this.gapAnalyzer = new GapAnalyzer({ llm: this.llm, config: toolGenConfig, model: agentModel });
28
40
  this.toolGenerator = new ToolGenerator({
29
41
  llm: this.llm,
@@ -33,32 +45,28 @@ export class SelfModifyingAgent {
33
45
  this.toolStore = new InMemoryGeneratedToolStore();
34
46
  this.metaReasoner = new MetaReasoner({
35
47
  llm: this.llm,
36
- model: agentModel ?? 'default',
48
+ model: agentModel,
37
49
  config: this.config.metaReasoning,
38
50
  });
39
- const baseArchConfig = {
40
- model: agentModel ?? 'default',
41
- temperature: 0.7,
42
- maxTokens: 4096,
43
- toolStrategy: 'sequential',
44
- reflectionDepth: 1,
45
- };
46
51
  this.parameterOptimizer = new ParameterOptimizer({
47
52
  llm: this.llm,
48
53
  config: this.config.architectureEvolution,
49
- baseConfig: baseArchConfig,
50
- model: agentModel,
51
- });
52
- this.capabilityAnalyzer = new CapabilityAnalyzer({
53
- llm: this.llm,
54
- enableLLMAnalysis: true,
54
+ baseConfig: this.createBaseArchitectureConfig(),
55
55
  model: agentModel,
56
+ availableModels: options.availableModels,
56
57
  });
57
58
  this.modificationValidator = new ModificationValidator({
58
59
  constraints: {
59
- safety: DEFAULT_SAFETY_CONSTRAINTS,
60
- capability: DEFAULT_CAPABILITY_CONSTRAINTS,
61
- resource: DEFAULT_RESOURCE_CONSTRAINTS,
60
+ safety: [...DEFAULT_SAFETY_CONSTRAINTS, ...(options.modificationConstraints?.safety ?? [])],
61
+ capability: [
62
+ ...DEFAULT_CAPABILITY_CONSTRAINTS,
63
+ ...(options.modificationConstraints?.capability ?? []),
64
+ ],
65
+ resource: [
66
+ ...DEFAULT_RESOURCE_CONSTRAINTS,
67
+ ...(options.modificationConstraints?.resource ?? []),
68
+ ],
69
+ custom: options.modificationConstraints?.custom,
62
70
  },
63
71
  });
64
72
  this.rollbackManager = new RollbackManager({
@@ -66,126 +74,37 @@ export class SelfModifyingAgent {
66
74
  });
67
75
  }
68
76
  on(event, handler) {
69
- this.emitter.on(event, handler);
77
+ let wrappers = this.listenerWrappers.get(event);
78
+ if (!wrappers) {
79
+ wrappers = new Map();
80
+ this.listenerWrappers.set(event, wrappers);
81
+ }
82
+ const existing = wrappers.get(handler);
83
+ if (existing)
84
+ this.emitter.off(event, existing);
85
+ const wrapped = (e) => handler(e);
86
+ wrappers.set(handler, wrapped);
87
+ this.emitter.on(event, wrapped);
88
+ return () => this.off(event, handler);
70
89
  }
71
90
  off(event, handler) {
72
- this.emitter.off(event, handler);
91
+ const wrappers = this.listenerWrappers.get(event);
92
+ const wrapped = wrappers?.get(handler);
93
+ if (!wrappers || !wrapped)
94
+ return;
95
+ this.emitter.off(event, wrapped);
96
+ wrappers.delete(handler);
73
97
  }
74
- async run(input) {
75
- await this.ensureInitialized();
76
- const runId = `run_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
77
- const tools = this.getAvailableTools();
78
- const baseConfig = {
79
- model: 'default',
80
- temperature: 0.7,
81
- maxTokens: 4096,
82
- toolStrategy: 'sequential',
83
- reflectionDepth: 1,
84
- };
85
- this.currentContext = {
86
- runId,
87
- input,
88
- startTime: Date.now(),
89
- tools: [...tools],
90
- currentConfig: baseConfig,
91
- generatedTools: [],
92
- observations: [],
93
- adaptations: [],
94
- checkpoints: [],
95
- };
96
- void this.emitter.emit({
97
- type: 'run_started',
98
- runId,
99
- timestamp: new Date(),
100
- data: { input },
101
- });
102
- try {
103
- if (this.config.architectureEvolution.enabled) {
104
- await this.optimizeArchitecture(input);
105
- }
106
- if (this.config.toolGeneration.enabled && this.config.toolGeneration.autoGenerate) {
107
- await this.analyzeAndGenerateTools(input);
108
- }
109
- const modeConfig = this.metaReasoner.initializeRun(runId);
110
- void this.emitter.emit({
111
- type: 'strategy_changed',
112
- runId,
113
- timestamp: new Date(),
114
- data: {
115
- previousMode: 'analytical',
116
- newMode: modeConfig.mode,
117
- reason: 'Initial mode selection',
118
- },
119
- });
120
- const output = await this.executeWithMetaReasoning(input, runId);
121
- const result = {
122
- output,
123
- toolsGenerated: [...this.currentContext.generatedTools],
124
- adaptationsMade: [...this.currentContext.adaptations],
125
- finalConfig: { ...this.currentContext.currentConfig },
126
- };
127
- void this.emitter.emit({
128
- type: 'run_completed',
129
- runId,
130
- timestamp: new Date(),
131
- data: {
132
- success: true,
133
- toolsGenerated: result.toolsGenerated.length,
134
- adaptationsMade: result.adaptationsMade.length,
135
- },
136
- });
137
- return result;
138
- }
139
- catch (error) {
140
- void this.emitter.emit({
141
- type: 'run_completed',
142
- runId,
143
- timestamp: new Date(),
144
- data: {
145
- success: false,
146
- error: error instanceof Error ? error.message : String(error),
147
- },
148
- });
149
- throw error;
150
- }
151
- finally {
152
- this.metaReasoner.cleanupRun(runId);
153
- this.currentContext = null;
154
- }
98
+ run(input) {
99
+ const result = this.runQueue.then(() => this.executeRun(input));
100
+ this.runQueue = result.catch(() => undefined);
101
+ return result;
155
102
  }
156
103
  async generateTool(gap) {
157
104
  if (!this.config.toolGeneration.enabled) {
158
105
  return null;
159
106
  }
160
- const tools = this.getAvailableTools();
161
- const result = await this.toolGenerator.generate(gap, tools);
162
- if (result.success && result.tool) {
163
- await this.toolStore.save(result.tool);
164
- void this.emitter.emit({
165
- type: 'tool_generation_completed',
166
- runId: this.currentContext?.runId || 'manual',
167
- timestamp: new Date(),
168
- data: {
169
- toolId: result.tool.id,
170
- name: result.tool.name,
171
- success: true,
172
- iterations: result.iterations,
173
- },
174
- });
175
- return result.tool;
176
- }
177
- void this.emitter.emit({
178
- type: 'tool_generation_completed',
179
- runId: this.currentContext?.runId || 'manual',
180
- timestamp: new Date(),
181
- data: {
182
- toolId: '',
183
- name: gap.suggestedToolName,
184
- success: false,
185
- error: result.error,
186
- },
187
- });
188
- return null;
107
+ return this.generateAndRegisterTool(gap, this.currentContext?.tools ?? this.getAgentTools());
189
108
  }
190
109
  async recordToolUsage(toolId, success, executionTime) {
191
110
  await this.toolStore.recordUsage({
@@ -199,48 +118,131 @@ export class SelfModifyingAgent {
199
118
  return this.toolStore.list({ status: 'active' });
200
119
  }
201
120
  async createCheckpoint() {
202
- if (!this.currentContext)
121
+ const ctx = this.currentContext;
122
+ if (!ctx)
203
123
  return null;
204
124
  const agentConfig = {
205
125
  name: this.agent.name || 'agent',
206
- model: this.currentContext.currentConfig.model,
126
+ model: ctx.currentConfig.model,
207
127
  instructions: this.agent.instructions || '',
208
- temperature: this.currentContext.currentConfig.temperature,
209
- maxTokens: this.currentContext.currentConfig.maxTokens,
128
+ temperature: ctx.currentConfig.temperature,
129
+ maxTokens: ctx.currentConfig.maxTokens,
210
130
  };
211
- const checkpoint = await this.rollbackManager.createCheckpoint(this.agent.name || 'agent', agentConfig, this.currentContext.tools, []);
212
- this.currentContext.checkpoints.push(checkpoint);
213
- void this.emitter.emit({
214
- type: 'checkpoint_created',
215
- runId: this.currentContext.runId,
216
- timestamp: new Date(),
217
- data: { checkpointId: checkpoint.id },
218
- });
131
+ const checkpoint = await this.rollbackManager.createCheckpoint(this.agent.name || 'agent', agentConfig, ctx.tools, [...ctx.appliedModifications]);
132
+ ctx.checkpoints.push(checkpoint);
133
+ this.emit('checkpoint_created', ctx.runId, { checkpointId: checkpoint.id });
219
134
  return checkpoint;
220
135
  }
221
- async rollbackToCheckpoint(checkpointId) {
136
+ async rollbackToCheckpoint(checkpointId, reason = 'Manual rollback') {
137
+ const checkpoint = await this.rollbackManager.getCheckpoint(checkpointId);
222
138
  const restored = await this.rollbackManager.rollbackTo(checkpointId);
223
- if (!restored)
139
+ if (!checkpoint || !restored)
224
140
  return false;
225
- if (this.currentContext) {
226
- this.currentContext.tools = restored.tools;
141
+ const ctx = this.currentContext;
142
+ if (ctx) {
143
+ ctx.tools = restored.tools;
144
+ ctx.currentConfig = {
145
+ ...ctx.currentConfig,
146
+ model: restored.agentConfig.model ?? ctx.currentConfig.model,
147
+ temperature: restored.agentConfig.temperature ?? ctx.currentConfig.temperature,
148
+ maxTokens: restored.agentConfig.maxTokens ?? ctx.currentConfig.maxTokens,
149
+ };
150
+ ctx.appliedModifications = [...checkpoint.modifications];
151
+ const restoredNames = new Set(restored.tools.map((t) => t.name));
152
+ const discarded = ctx.generatedTools.filter((t) => !restoredNames.has(t.name));
153
+ for (const tool of discarded) {
154
+ await this.toolStore.deprecate(tool.id, `Rolled back to checkpoint ${checkpointId}`);
155
+ }
156
+ ctx.generatedTools = ctx.generatedTools.filter((t) => restoredNames.has(t.name));
227
157
  }
158
+ this.emit('rollback_performed', ctx?.runId ?? 'manual', { checkpointId, reason });
159
+ return true;
160
+ }
161
+ async executeRun(input) {
162
+ const runId = `run_${nanoid(12)}`;
163
+ const selfModification = this.config.enabled;
164
+ const tools = selfModification ? await this.loadAvailableTools() : this.getAgentTools();
165
+ const ctx = {
166
+ runId,
167
+ input,
168
+ startTime: Date.now(),
169
+ tools,
170
+ currentConfig: this.createBaseArchitectureConfig(),
171
+ generatedTools: [],
172
+ observations: [],
173
+ adaptations: [],
174
+ checkpoints: [],
175
+ modificationsCount: 0,
176
+ appliedModifications: [],
177
+ tokensUsed: 0,
178
+ };
179
+ this.currentContext = ctx;
180
+ this.emit('run_started', runId, { input });
181
+ let optimization = null;
182
+ try {
183
+ if (selfModification &&
184
+ this.config.constraints.enabled &&
185
+ this.config.constraints.autoRollback) {
186
+ await this.createCheckpoint();
187
+ }
188
+ if (selfModification && this.config.architectureEvolution.enabled) {
189
+ optimization = await this.optimizeArchitecture(ctx);
190
+ }
191
+ if (selfModification &&
192
+ this.config.toolGeneration.enabled &&
193
+ this.config.toolGeneration.autoGenerate) {
194
+ await this.analyzeAndGenerateTools(ctx);
195
+ }
196
+ const execution = await this.executeWithMetaReasoning(ctx, selfModification);
197
+ await this.recordArchitectureOutcome(ctx, optimization, true, execution.confidence);
198
+ const result = {
199
+ output: execution.output,
200
+ toolsGenerated: [...ctx.generatedTools],
201
+ adaptationsMade: [...ctx.adaptations],
202
+ finalConfig: { ...ctx.currentConfig },
203
+ };
204
+ this.emit('run_completed', runId, {
205
+ success: true,
206
+ toolsGenerated: result.toolsGenerated.length,
207
+ adaptationsMade: result.adaptationsMade.length,
208
+ });
209
+ return result;
210
+ }
211
+ catch (error) {
212
+ await this.recordArchitectureOutcome(ctx, optimization, false, 0).catch(() => undefined);
213
+ if (selfModification &&
214
+ this.config.constraints.enabled &&
215
+ this.config.constraints.autoRollback) {
216
+ await this.autoRollback(ctx, 'Run failed');
217
+ }
218
+ this.emit('run_completed', runId, {
219
+ success: false,
220
+ error: error instanceof Error ? error.message : String(error),
221
+ });
222
+ throw error;
223
+ }
224
+ finally {
225
+ this.metaReasoner.cleanupRun(runId);
226
+ this.currentContext = null;
227
+ }
228
+ }
229
+ emit(type, runId, data) {
228
230
  void this.emitter.emit({
229
- type: 'rollback_performed',
230
- runId: this.currentContext?.runId || 'manual',
231
+ type,
232
+ runId,
233
+ agentId: this.agent.id,
231
234
  timestamp: new Date(),
232
- data: {
233
- checkpointId,
234
- reason: 'Manual rollback',
235
- },
235
+ data,
236
236
  });
237
- return true;
238
237
  }
239
- async ensureInitialized() {
240
- if (this.isInitialized)
241
- return;
242
- await this.toolStore.list({ status: 'active' });
243
- this.isInitialized = true;
238
+ createBaseArchitectureConfig() {
239
+ return {
240
+ model: this.modelName,
241
+ temperature: this.agent.config?.temperature ?? 0.7,
242
+ maxTokens: this.agent.config?.maxTokens ?? 4096,
243
+ toolStrategy: 'sequential',
244
+ reflectionDepth: 0,
245
+ };
244
246
  }
245
247
  mergeConfig(partial) {
246
248
  const defaults = {
@@ -260,40 +262,7 @@ export class SelfModifyingAgent {
260
262
  isolationLevel: 'strict',
261
263
  },
262
264
  },
263
- metaReasoning: {
264
- enabled: true,
265
- defaultMode: 'analytical',
266
- allowedModes: [
267
- 'analytical',
268
- 'creative',
269
- 'systematic',
270
- 'intuitive',
271
- 'reflective',
272
- 'exploratory',
273
- ],
274
- modeProfiles: {
275
- analytical: { mode: 'analytical', temperature: 0.3, depth: 3 },
276
- creative: { mode: 'creative', temperature: 0.9, depth: 2 },
277
- systematic: { mode: 'systematic', temperature: 0.2, depth: 4 },
278
- intuitive: { mode: 'intuitive', temperature: 0.6, depth: 1 },
279
- reflective: { mode: 'reflective', temperature: 0.4, depth: 3 },
280
- exploratory: { mode: 'exploratory', temperature: 0.7, depth: 2 },
281
- },
282
- maxMetaAssessments: 5,
283
- maxAdaptations: 3,
284
- metaAssessmentCooldown: 10000,
285
- adaptationCooldown: 15000,
286
- triggers: ['on_failure', 'on_low_confidence', 'periodic'],
287
- triggerAfterIterations: 3,
288
- triggerOnConfidenceDrop: 0.3,
289
- triggerOnProgressStall: 2,
290
- tokenBudget: 2000,
291
- maxMetaTokens: 1000,
292
- minConfidenceToAdapt: 0.6,
293
- enableRollback: true,
294
- rollbackWindow: 30000,
295
- rollbackOnDecline: true,
296
- },
265
+ metaReasoning: { ...DEFAULT_META_REASONING_CONFIG },
297
266
  architectureEvolution: {
298
267
  enabled: true,
299
268
  strategy: { type: 'ucb', explorationConstant: 2 },
@@ -311,14 +280,19 @@ export class SelfModifyingAgent {
311
280
  };
312
281
  if (!partial)
313
282
  return defaults;
283
+ const defaultSandbox = defaults.toolGeneration.sandboxConfig ?? {
284
+ enabled: true,
285
+ maxExecutionTime: 5000,
286
+ maxMemory: 50 * 1024 * 1024,
287
+ allowedModules: [],
288
+ isolationLevel: 'strict',
289
+ };
314
290
  return {
315
291
  enabled: partial.enabled ?? defaults.enabled,
316
292
  toolGeneration: {
317
293
  ...defaults.toolGeneration,
318
294
  ...partial.toolGeneration,
319
- sandboxConfig: partial.toolGeneration?.sandboxConfig
320
- ? { ...defaults.toolGeneration.sandboxConfig, ...partial.toolGeneration.sandboxConfig }
321
- : defaults.toolGeneration.sandboxConfig,
295
+ sandboxConfig: { ...defaultSandbox, ...partial.toolGeneration?.sandboxConfig },
322
296
  },
323
297
  metaReasoning: {
324
298
  ...defaults.metaReasoning,
@@ -331,241 +305,474 @@ export class SelfModifyingAgent {
331
305
  architectureEvolution: {
332
306
  ...defaults.architectureEvolution,
333
307
  ...partial.architectureEvolution,
334
- strategy: partial.architectureEvolution?.strategy
335
- ? {
336
- ...defaults.architectureEvolution.strategy,
337
- ...partial.architectureEvolution.strategy,
338
- }
339
- : defaults.architectureEvolution.strategy,
308
+ strategy: {
309
+ ...defaults.architectureEvolution.strategy,
310
+ ...partial.architectureEvolution?.strategy,
311
+ },
340
312
  },
341
313
  constraints: { ...defaults.constraints, ...partial.constraints },
342
314
  };
343
315
  }
344
- getAvailableTools() {
345
- return this.agent.tools ?? [];
316
+ getAgentTools() {
317
+ return [...(this.agent.tools ?? [])];
346
318
  }
347
- async optimizeArchitecture(input) {
348
- if (!this.currentContext)
349
- return;
319
+ async loadAvailableTools() {
320
+ const tools = this.getAgentTools();
321
+ const names = new Set(tools.map((t) => t.name));
322
+ const stored = await this.toolStore.list({ status: 'active' });
323
+ for (const generated of stored) {
324
+ if (names.has(generated.name))
325
+ continue;
326
+ names.add(generated.name);
327
+ tools.push(this.toolGenerator.createExecutableTool(generated));
328
+ }
329
+ return tools;
330
+ }
331
+ async optimizeArchitecture(ctx) {
350
332
  try {
351
- const result = await this.parameterOptimizer.optimize(input);
352
- if (result.shouldAdopt && result.confidence > 0.6) {
353
- const validation = await this.modificationValidator.validate({
354
- type: 'config_change',
355
- target: 'architecture',
356
- changes: result.recommendedConfig,
357
- reason: result.reasoning,
358
- });
359
- if (validation.valid) {
360
- this.currentContext.currentConfig = result.recommendedConfig;
361
- void this.emitter.emit({
362
- type: 'architecture_evolved',
363
- runId: this.currentContext.runId,
364
- timestamp: new Date(),
365
- data: {
366
- candidateId: result.candidate?.id,
367
- changes: result.recommendedConfig,
368
- metrics: result.metrics,
369
- },
370
- });
371
- }
333
+ const result = await this.parameterOptimizer.optimize(ctx.input);
334
+ const candidate = result.candidate;
335
+ if (!candidate)
336
+ return null;
337
+ const isNoop = Object.keys(candidate.config).length === 0;
338
+ if (isNoop) {
339
+ return { result, candidateId: candidate.id };
340
+ }
341
+ if (!result.shouldAdopt || !this.canApplyModification(ctx)) {
342
+ return { result, candidateId: 'baseline' };
372
343
  }
344
+ const valid = await this.validateModification(ctx, {
345
+ type: 'config_change',
346
+ target: 'architecture',
347
+ changes: result.recommendedConfig,
348
+ reason: result.reasoning,
349
+ payload: { candidateId: candidate.id, changedKeys: Object.keys(candidate.config) },
350
+ });
351
+ if (!valid) {
352
+ return { result, candidateId: 'baseline' };
353
+ }
354
+ ctx.currentConfig = { ...result.recommendedConfig };
355
+ this.recordModification(ctx, 'config_change', {
356
+ candidateId: candidate.id,
357
+ config: result.recommendedConfig,
358
+ });
359
+ this.emit('architecture_evolved', ctx.runId, {
360
+ success: true,
361
+ candidateId: candidate.id,
362
+ changes: result.recommendedConfig,
363
+ metrics: result.metrics,
364
+ });
365
+ return { result, candidateId: candidate.id };
373
366
  }
374
367
  catch (error) {
375
- void this.emitter.emit({
376
- type: 'architecture_evolved',
377
- runId: this.currentContext.runId,
378
- timestamp: new Date(),
379
- data: { error: error instanceof Error ? error.message : String(error) },
368
+ this.emit('architecture_evolved', ctx.runId, {
369
+ success: false,
370
+ error: error instanceof Error ? error.message : String(error),
380
371
  });
372
+ return null;
381
373
  }
382
374
  }
383
- async analyzeAndGenerateTools(input) {
384
- if (!this.currentContext)
375
+ async recordArchitectureOutcome(ctx, optimization, success, quality) {
376
+ if (!optimization)
385
377
  return;
378
+ await this.parameterOptimizer.recordOutcome(optimization.candidateId, optimization.result.taskProfile, {
379
+ successRate: success ? 1 : 0,
380
+ latency: Date.now() - ctx.startTime,
381
+ tokenUsage: ctx.tokensUsed,
382
+ qualityScore: quality,
383
+ });
384
+ }
385
+ async analyzeAndGenerateTools(ctx) {
386
386
  try {
387
- const analysis = await this.gapAnalyzer.analyze(input, this.currentContext.tools);
387
+ const analysis = await this.gapAnalyzer.analyze(ctx.input, ctx.tools);
388
388
  for (const gap of analysis.gaps) {
389
- if (gap.confidence >= this.config.toolGeneration.minConfidenceForGeneration) {
390
- void this.emitter.emit({
391
- type: 'tool_generation_started',
392
- runId: this.currentContext.runId,
393
- timestamp: new Date(),
394
- data: { gap },
395
- });
396
- const tool = await this.generateTool(gap);
397
- if (tool) {
398
- this.currentContext.generatedTools.push(tool);
399
- const executableTool = this.toolGenerator.createExecutableTool(tool);
400
- this.currentContext.tools.push(executableTool);
401
- }
402
- }
389
+ if (ctx.generatedTools.length >= this.config.toolGeneration.maxToolsPerSession)
390
+ break;
391
+ if (!this.canApplyModification(ctx))
392
+ break;
393
+ if (gap.confidence < this.config.toolGeneration.minConfidenceForGeneration)
394
+ continue;
395
+ const tool = await this.generateAndRegisterTool(gap, ctx.tools);
396
+ if (!tool)
397
+ continue;
398
+ ctx.generatedTools.push(tool);
399
+ ctx.tools.push(this.toolGenerator.createExecutableTool(tool));
400
+ this.recordModification(ctx, 'tool_generation', { toolId: tool.id, name: tool.name });
403
401
  }
404
402
  }
405
403
  catch (error) {
406
- void this.emitter.emit({
407
- type: 'tool_generation_completed',
408
- runId: this.currentContext.runId,
409
- timestamp: new Date(),
410
- data: {
411
- toolId: '',
412
- name: '',
413
- success: false,
414
- error: error instanceof Error ? error.message : String(error),
415
- },
404
+ this.emit('tool_generation_completed', ctx.runId, {
405
+ toolId: '',
406
+ name: '',
407
+ success: false,
408
+ error: error instanceof Error ? error.message : String(error),
416
409
  });
417
410
  }
418
411
  }
419
- async executeWithMetaReasoning(input, runId) {
420
- if (!this.currentContext) {
421
- throw new Error('No active run context');
412
+ async generateAndRegisterTool(gap, existingTools) {
413
+ const ctx = this.currentContext;
414
+ const runId = ctx?.runId ?? 'manual';
415
+ this.emit('tool_generation_started', runId, { gap });
416
+ const result = await this.toolGenerator.generate(gap, existingTools);
417
+ const generated = result.tool;
418
+ if (!result.success || !generated) {
419
+ this.emit('tool_generation_completed', runId, {
420
+ toolId: generated?.id ?? '',
421
+ name: generated?.name ?? gap.suggestedToolName,
422
+ success: false,
423
+ iterations: result.iterations,
424
+ error: result.error,
425
+ });
426
+ return null;
427
+ }
428
+ const rejection = await this.checkGeneratedTool(generated, existingTools);
429
+ if (rejection) {
430
+ this.emit('tool_generation_completed', runId, {
431
+ toolId: generated.id,
432
+ name: generated.name,
433
+ success: false,
434
+ iterations: result.iterations,
435
+ error: rejection,
436
+ });
437
+ return null;
438
+ }
439
+ const activeTool = { ...generated, status: 'active' };
440
+ await this.toolStore.save(activeTool);
441
+ const saved = (await this.toolStore.get(activeTool.id)) ?? activeTool;
442
+ this.emit('tool_generation_completed', runId, {
443
+ toolId: saved.id,
444
+ name: saved.name,
445
+ success: true,
446
+ iterations: result.iterations,
447
+ });
448
+ return saved;
449
+ }
450
+ async checkGeneratedTool(tool, existingTools) {
451
+ if (existingTools.some((t) => t.name === tool.name)) {
452
+ return `A tool named "${tool.name}" already exists`;
453
+ }
454
+ const linesOfCode = tool.implementation.split('\n').length;
455
+ const valid = await this.validateModification(this.currentContext, {
456
+ type: 'tool_generation',
457
+ target: tool.name,
458
+ changes: { name: tool.name, implementation: tool.implementation },
459
+ reason: `Generated tool for capability gap: ${tool.name}`,
460
+ payload: {
461
+ sandboxExecution: this.config.toolGeneration.sandboxConfig?.enabled ?? false,
462
+ linesOfCode,
463
+ complexity: linesOfCode,
464
+ modificationDepth: 1,
465
+ },
466
+ });
467
+ return valid ? null : 'Rejected by modification constraints';
468
+ }
469
+ async executeWithMetaReasoning(ctx, selfModification) {
470
+ const runId = ctx.runId;
471
+ const metaEnabled = selfModification && this.config.metaReasoning.enabled;
472
+ const maxAttempts = metaEnabled ? Math.max(1, this.config.metaReasoning.maxAdaptations + 1) : 1;
473
+ const contextAdditions = [];
474
+ let modeAdapted = false;
475
+ let pendingAdaptation = null;
476
+ if (metaEnabled) {
477
+ const modeConfig = this.metaReasoner.initializeRun(runId);
478
+ this.emit('strategy_changed', runId, {
479
+ previousMode: this.config.metaReasoning.defaultMode,
480
+ newMode: modeConfig.mode,
481
+ reason: 'Initial mode selection',
482
+ });
422
483
  }
423
- let output = '';
424
- let iteration = 0;
425
- const maxIterations = 10;
426
- while (iteration < maxIterations) {
427
- iteration++;
428
- const currentMode = this.metaReasoner.getCurrentMode(runId);
484
+ let step = null;
485
+ let confidence = 0;
486
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
487
+ const temperature = modeAdapted
488
+ ? this.metaReasoner.getCurrentConfig(runId).temperature
489
+ : ctx.currentConfig.temperature;
490
+ step = await this.executeAgentStep(ctx, { temperature, contextAdditions });
491
+ ctx.tokensUsed += step.tokensUsed;
492
+ confidence = this.stepConfidence(step);
493
+ if (!metaEnabled)
494
+ break;
495
+ this.metaReasoner.recordConfidence(runId, confidence);
496
+ if (pendingAdaptation) {
497
+ this.metaReasoner.recordOutcome(runId, pendingAdaptation.adaptation.id, {
498
+ improved: step.complete || confidence > pendingAdaptation.confidence,
499
+ progressDelta: step.complete ? 1 : 0,
500
+ confidenceDelta: confidence - pendingAdaptation.confidence,
501
+ });
502
+ pendingAdaptation = null;
503
+ }
504
+ if (step.complete || attempt === maxAttempts)
505
+ break;
429
506
  const observation = this.metaReasoner.observe({
430
507
  runId,
431
- iteration,
432
- goal: input,
433
- currentMode,
434
- tokensUsed: Math.round(output.length * 1.3),
435
- timeElapsed: Date.now() - this.currentContext.startTime,
436
- iterationsRemaining: maxIterations - iteration,
437
- budgetRemaining: 10000 - Math.round(output.length * 1.3),
508
+ iteration: attempt,
509
+ goal: ctx.input,
510
+ currentMode: this.metaReasoner.getCurrentMode(runId),
511
+ tokensUsed: ctx.tokensUsed,
512
+ timeElapsed: Date.now() - ctx.startTime,
513
+ iterationsRemaining: maxAttempts - attempt,
514
+ budgetRemaining: (maxAttempts - attempt) / maxAttempts,
438
515
  }, []);
439
- this.currentContext.observations.push(observation);
440
- if (this.metaReasoner.shouldTrigger(runId, 'periodic', {
441
- iteration,
442
- confidence: this.estimateConfidence(output),
443
- progressDelta: output.length > 0 ? 0.1 : 0,
444
- stagnationCount: 0,
445
- })) {
446
- const assessment = await this.metaReasoner.assess(observation);
447
- void this.emitter.emit({
448
- type: 'meta_assessment',
449
- runId,
450
- timestamp: new Date(),
451
- data: {
452
- observation,
453
- assessment,
454
- },
455
- });
456
- if (assessment.requiresAdaptation) {
457
- const adaptation = await this.metaReasoner.adapt(runId, assessment);
458
- if (adaptation) {
459
- this.currentContext.adaptations.push(adaptation);
460
- void this.emitter.emit({
461
- type: 'strategy_changed',
462
- runId,
463
- timestamp: new Date(),
464
- data: {
465
- previousMode: adaptation.before?.mode || 'analytical',
466
- newMode: adaptation.after?.mode || 'analytical',
467
- reason: adaptation.type,
468
- },
469
- });
470
- }
471
- }
472
- }
473
- output = await this.executeAgentStep(input);
474
- if (this.isTaskComplete(output)) {
516
+ ctx.observations.push(observation);
517
+ if (!this.selectTrigger(runId, attempt, confidence, observation))
518
+ continue;
519
+ const assessment = await this.metaReasoner.assess(observation);
520
+ this.emit('meta_assessment', runId, { observation, assessment });
521
+ if (!assessment.requiresAdaptation)
522
+ continue;
523
+ if (assessment.recommendation.action === 'abort')
475
524
  break;
525
+ const adaptation = await this.metaReasoner.adapt(runId, assessment);
526
+ if (!adaptation)
527
+ continue;
528
+ ctx.adaptations.push(adaptation);
529
+ pendingAdaptation = { adaptation, confidence };
530
+ if (adaptation.type === 'context_injection' && assessment.recommendation.contextAddition) {
531
+ contextAdditions.push(assessment.recommendation.contextAddition);
476
532
  }
533
+ else {
534
+ modeAdapted = true;
535
+ }
536
+ this.emit('strategy_changed', runId, {
537
+ previousMode: adaptation.previousMode ?? this.metaReasoner.getCurrentMode(runId),
538
+ newMode: adaptation.newMode ?? this.metaReasoner.getCurrentMode(runId),
539
+ reason: adaptation.reason ?? adaptation.type,
540
+ });
477
541
  }
478
- return output;
542
+ return { output: step?.content ?? '', confidence };
479
543
  }
480
- async executeAgentStep(input) {
481
- const tools = this.currentContext?.tools ?? [];
482
- const toolSchemas = this.buildToolSchemas(tools);
483
- const toolMap = new Map();
484
- for (const t of tools) {
485
- toolMap.set(t.name, t);
544
+ selectTrigger(runId, iteration, confidence, observation) {
545
+ const context = {
546
+ iteration,
547
+ confidence,
548
+ progressDelta: observation.progressDelta,
549
+ stagnationCount: observation.stagnationCount,
550
+ };
551
+ const candidates = [
552
+ 'on_failure',
553
+ 'on_low_confidence',
554
+ 'on_stagnation',
555
+ 'iteration_complete',
556
+ 'periodic',
557
+ ];
558
+ return candidates.find((t) => this.metaReasoner.shouldTrigger(runId, t, context)) ?? null;
559
+ }
560
+ async executeAgentStep(ctx, options) {
561
+ const toolMap = new Map(ctx.tools.map((t) => [t.name, t]));
562
+ const toolSchemas = this.buildToolSchemas(ctx.tools);
563
+ const generatedIds = new Map(ctx.generatedTools.map((t) => [t.name, t.id]));
564
+ for (const stored of await this.toolStore.list({ status: 'active' })) {
565
+ if (!generatedIds.has(stored.name))
566
+ generatedIds.set(stored.name, stored.id);
486
567
  }
487
- const model = this.currentContext?.currentConfig.model ?? this.agent.model ?? 'default';
568
+ const model = ctx.currentConfig.model;
569
+ const maxTokens = ctx.currentConfig.maxTokens;
488
570
  const messages = [
489
571
  ...(this.agent.instructions
490
572
  ? [{ role: 'system', content: this.agent.instructions }]
491
573
  : []),
492
- { role: 'user', content: input },
574
+ ...options.contextAdditions.map((content) => ({ role: 'system', content })),
575
+ { role: 'user', content: ctx.input },
493
576
  ];
494
- const maxToolRounds = 10;
577
+ let tokensUsed = 0;
578
+ let toolCalls = 0;
579
+ let toolErrors = 0;
580
+ let response = null;
581
+ const maxToolRounds = this.agent.config?.maxIterations ?? DEFAULT_MAX_TOOL_ROUNDS;
495
582
  for (let round = 0; round < maxToolRounds; round++) {
496
- const response = await this.llm.chat({
583
+ response = await this.llm.chat({
497
584
  model,
498
585
  messages,
499
586
  tools: toolSchemas.length > 0 ? toolSchemas : undefined,
500
- temperature: this.currentContext?.currentConfig.temperature,
501
- maxTokens: this.currentContext?.currentConfig.maxTokens,
587
+ temperature: options.temperature,
588
+ maxTokens,
502
589
  });
590
+ tokensUsed += response.usage?.totalTokens ?? 0;
503
591
  if (response.finishReason !== 'tool_calls' || !response.toolCalls?.length) {
504
- return response.content;
592
+ break;
505
593
  }
506
- messages.push({
594
+ const assistantMessage = {
507
595
  role: 'assistant',
508
596
  content: response.content,
509
597
  toolCalls: response.toolCalls,
510
- });
511
- for (const toolCall of response.toolCalls) {
512
- const t = toolMap.get(toolCall.name);
513
- let result;
514
- if (!t) {
515
- result = { error: `Tool "${toolCall.name}" not found` };
516
- }
517
- else {
518
- try {
519
- const ctx = {
520
- agentId: this.agent.name || 'self-modifying',
521
- runId: this.currentContext?.runId || 'unknown',
522
- signal: AbortSignal.timeout(30_000),
523
- };
524
- result = await t.execute(toolCall.arguments, ctx);
525
- }
526
- catch (err) {
527
- result = { error: err instanceof Error ? err.message : String(err) };
528
- }
529
- }
598
+ };
599
+ messages.push(assistantMessage);
600
+ const outcomes = await this.executeToolCalls(ctx, response.toolCalls, toolMap, generatedIds);
601
+ for (const outcome of outcomes) {
602
+ toolCalls++;
603
+ if (outcome.failed)
604
+ toolErrors++;
530
605
  messages.push({
531
606
  role: 'tool',
532
- content: typeof result === 'string' ? result : JSON.stringify(result),
533
- toolCallId: toolCall.id,
534
- name: toolCall.name,
607
+ content: outcome.content,
608
+ toolCallId: outcome.call.id,
609
+ name: outcome.call.name,
535
610
  });
536
611
  }
612
+ response = null;
537
613
  }
538
- const finalResponse = await this.llm.chat({
539
- model,
540
- messages,
541
- temperature: this.currentContext?.currentConfig.temperature,
542
- maxTokens: this.currentContext?.currentConfig.maxTokens,
543
- });
544
- return finalResponse.content;
614
+ if (!response) {
615
+ response = await this.llm.chat({
616
+ model,
617
+ messages,
618
+ temperature: options.temperature,
619
+ maxTokens,
620
+ });
621
+ tokensUsed += response.usage?.totalTokens ?? 0;
622
+ }
623
+ let content = response.content;
624
+ let finishReason = response.finishReason;
625
+ for (let depth = 0; depth < ctx.currentConfig.reflectionDepth && content.trim(); depth++) {
626
+ const reflection = await this.llm.chat({
627
+ model,
628
+ messages: [
629
+ ...messages,
630
+ { role: 'assistant', content },
631
+ {
632
+ role: 'user',
633
+ content: 'Review your previous answer for mistakes or omissions. Reply with the final, corrected answer only. If it is already correct, repeat it unchanged.',
634
+ },
635
+ ],
636
+ temperature: options.temperature,
637
+ maxTokens,
638
+ });
639
+ tokensUsed += reflection.usage?.totalTokens ?? 0;
640
+ if (!reflection.content.trim() || reflection.finishReason === 'error')
641
+ break;
642
+ content = reflection.content;
643
+ finishReason = reflection.finishReason;
644
+ }
645
+ return {
646
+ content,
647
+ finishReason,
648
+ tokensUsed,
649
+ toolCalls,
650
+ toolErrors,
651
+ complete: content.trim().length > 0 && finishReason !== 'length' && finishReason !== 'error',
652
+ };
545
653
  }
546
- buildToolSchemas(tools) {
547
- return tools.map((t) => {
548
- if (typeof t.toJSON === 'function') {
549
- return t.toJSON();
654
+ async executeToolCalls(ctx, calls, toolMap, generatedIds) {
655
+ const strategy = ctx.currentConfig.toolStrategy;
656
+ const parallel = strategy === 'parallel' ||
657
+ (strategy === 'adaptive' && new Set(calls.map((c) => c.name)).size === calls.length);
658
+ if (parallel) {
659
+ return Promise.all(calls.map((call) => this.executeToolCall(ctx, call, toolMap, generatedIds)));
660
+ }
661
+ const outcomes = [];
662
+ for (const call of calls) {
663
+ outcomes.push(await this.executeToolCall(ctx, call, toolMap, generatedIds));
664
+ }
665
+ return outcomes;
666
+ }
667
+ async executeToolCall(ctx, call, toolMap, generatedIds) {
668
+ const t = toolMap.get(call.name);
669
+ const startedAt = Date.now();
670
+ let result;
671
+ let error;
672
+ if (!t) {
673
+ error = `Tool "${call.name}" not found`;
674
+ }
675
+ else {
676
+ const parsed = t.parameters.safeParse(call.arguments);
677
+ if (!parsed.success) {
678
+ error = `Invalid arguments for "${call.name}": ${parsed.error.issues
679
+ .map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`)
680
+ .join('; ')}`;
681
+ }
682
+ else {
683
+ try {
684
+ const toolContext = {
685
+ agentId: this.agent.id ?? this.agent.name ?? 'self-modifying',
686
+ runId: ctx.runId,
687
+ signal: AbortSignal.timeout(t.timeout ?? DEFAULT_TOOL_TIMEOUT_MS),
688
+ };
689
+ result = await t.execute(parsed.data, toolContext);
690
+ }
691
+ catch (err) {
692
+ error = err instanceof Error ? err.message : String(err);
693
+ }
550
694
  }
551
- return {
695
+ }
696
+ const duration = Date.now() - startedAt;
697
+ const generatedId = generatedIds.get(call.name);
698
+ if (generatedId) {
699
+ await this.toolStore.recordUsage({
700
+ toolId: generatedId,
701
+ timestamp: new Date(),
702
+ success: error === undefined,
703
+ executionTime: duration,
704
+ error,
705
+ });
706
+ }
707
+ if (this.config.enabled && this.config.metaReasoning.enabled) {
708
+ this.metaReasoner.recordAction(ctx.runId, {
709
+ type: 'tool_call',
710
+ toolName: call.name,
711
+ input: call.arguments,
712
+ output: error === undefined ? result : undefined,
713
+ error,
714
+ duration,
715
+ timestamp: startedAt,
716
+ });
717
+ }
718
+ if (error !== undefined) {
719
+ return { call, content: JSON.stringify({ error }), failed: true };
720
+ }
721
+ return {
722
+ call,
723
+ content: typeof result === 'string' ? result : (JSON.stringify(result) ?? 'null'),
724
+ failed: false,
725
+ };
726
+ }
727
+ buildToolSchemas(tools) {
728
+ return tools.map((t) => typeof t.toJSON === 'function'
729
+ ? t.toJSON()
730
+ : {
552
731
  name: t.name,
553
732
  description: t.description,
554
733
  parameters: { type: 'object', properties: {} },
555
- };
556
- });
734
+ });
557
735
  }
558
- estimateConfidence(output) {
559
- if (!output)
560
- return 0.3;
561
- if (output.length < 50)
562
- return 0.4;
563
- if (output.length < 200)
564
- return 0.6;
565
- return 0.8;
736
+ stepConfidence(step) {
737
+ if (!step.complete)
738
+ return 0.2;
739
+ if (step.toolCalls > 0 && step.toolErrors > 0) {
740
+ return step.toolErrors >= step.toolCalls ? 0.4 : 0.6;
741
+ }
742
+ return 0.9;
566
743
  }
567
- isTaskComplete(_output) {
568
- return true;
744
+ async validateModification(ctx, request) {
745
+ if (!this.config.constraints.enabled)
746
+ return true;
747
+ const validation = await this.modificationValidator.validate(request);
748
+ if (validation.rollbackRequired && this.config.constraints.autoRollback && ctx) {
749
+ await this.autoRollback(ctx, `Critical constraint violation: ${request.type}`);
750
+ }
751
+ return validation.valid;
752
+ }
753
+ canApplyModification(ctx) {
754
+ if (!this.config.constraints.enabled)
755
+ return true;
756
+ return ctx.modificationsCount < this.config.constraints.maxModificationsPerRun;
757
+ }
758
+ recordModification(ctx, type, data) {
759
+ ctx.modificationsCount++;
760
+ ctx.appliedModifications.push({
761
+ id: `mod_${nanoid(8)}`,
762
+ type,
763
+ appliedAt: new Date(),
764
+ data,
765
+ });
766
+ }
767
+ async autoRollback(ctx, reason) {
768
+ const now = Date.now();
769
+ const window = this.config.constraints.rollbackWindow;
770
+ const target = ctx.checkpoints
771
+ .filter((c) => now - c.timestamp.getTime() <= window)
772
+ .sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime())[0];
773
+ if (target) {
774
+ await this.rollbackToCheckpoint(target.id, reason);
775
+ }
569
776
  }
570
777
  }
571
778
  //# sourceMappingURL=self-modifying-agent.js.map