@cogitator-ai/self-modifying 17.0.18 → 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 (84) 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 +38 -37
  4. package/dist/architecture-evolution/capability-analyzer.js.map +1 -1
  5. package/dist/architecture-evolution/index.d.ts +2 -2
  6. package/dist/architecture-evolution/index.d.ts.map +1 -1
  7. package/dist/architecture-evolution/index.js +1 -1
  8. package/dist/architecture-evolution/index.js.map +1 -1
  9. package/dist/architecture-evolution/parameter-optimizer.d.ts +5 -4
  10. package/dist/architecture-evolution/parameter-optimizer.d.ts.map +1 -1
  11. package/dist/architecture-evolution/parameter-optimizer.js +36 -31
  12. package/dist/architecture-evolution/parameter-optimizer.js.map +1 -1
  13. package/dist/architecture-evolution/prompts.d.ts +21 -2
  14. package/dist/architecture-evolution/prompts.d.ts.map +1 -1
  15. package/dist/architecture-evolution/prompts.js +150 -36
  16. package/dist/architecture-evolution/prompts.js.map +1 -1
  17. package/dist/constraints/modification-validator.d.ts +1 -0
  18. package/dist/constraints/modification-validator.d.ts.map +1 -1
  19. package/dist/constraints/modification-validator.js +30 -14
  20. package/dist/constraints/modification-validator.js.map +1 -1
  21. package/dist/index.d.ts +4 -4
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -3
  24. package/dist/index.js.map +1 -1
  25. package/dist/meta-reasoning/meta-reasoner.d.ts +3 -0
  26. package/dist/meta-reasoning/meta-reasoner.d.ts.map +1 -1
  27. package/dist/meta-reasoning/meta-reasoner.js +101 -33
  28. package/dist/meta-reasoning/meta-reasoner.js.map +1 -1
  29. package/dist/meta-reasoning/prompts.d.ts +10 -14
  30. package/dist/meta-reasoning/prompts.d.ts.map +1 -1
  31. package/dist/meta-reasoning/prompts.js +109 -1
  32. package/dist/meta-reasoning/prompts.js.map +1 -1
  33. package/dist/self-modifying-agent.d.ts +83 -17
  34. package/dist/self-modifying-agent.d.ts.map +1 -1
  35. package/dist/self-modifying-agent.js +557 -444
  36. package/dist/self-modifying-agent.js.map +1 -1
  37. package/dist/tool-generation/gap-analyzer.d.ts +0 -1
  38. package/dist/tool-generation/gap-analyzer.d.ts.map +1 -1
  39. package/dist/tool-generation/gap-analyzer.js +14 -14
  40. package/dist/tool-generation/gap-analyzer.js.map +1 -1
  41. package/dist/tool-generation/index.d.ts +3 -3
  42. package/dist/tool-generation/index.d.ts.map +1 -1
  43. package/dist/tool-generation/index.js +2 -2
  44. package/dist/tool-generation/index.js.map +1 -1
  45. package/dist/tool-generation/prompts.d.ts +1 -0
  46. package/dist/tool-generation/prompts.d.ts.map +1 -1
  47. package/dist/tool-generation/prompts.js +33 -8
  48. package/dist/tool-generation/prompts.js.map +1 -1
  49. package/dist/tool-generation/tool-generator.d.ts +5 -5
  50. package/dist/tool-generation/tool-generator.d.ts.map +1 -1
  51. package/dist/tool-generation/tool-generator.js +41 -28
  52. package/dist/tool-generation/tool-generator.js.map +1 -1
  53. package/dist/tool-generation/tool-sandbox.d.ts +9 -6
  54. package/dist/tool-generation/tool-sandbox.d.ts.map +1 -1
  55. package/dist/tool-generation/tool-sandbox.js +257 -157
  56. package/dist/tool-generation/tool-sandbox.js.map +1 -1
  57. package/dist/tool-generation/tool-validator.d.ts +3 -8
  58. package/dist/tool-generation/tool-validator.d.ts.map +1 -1
  59. package/dist/tool-generation/tool-validator.js +53 -54
  60. package/dist/tool-generation/tool-validator.js.map +1 -1
  61. package/dist/utils/llm-helper.d.ts.map +1 -1
  62. package/dist/utils/llm-helper.js +1 -0
  63. package/dist/utils/llm-helper.js.map +1 -1
  64. package/package.json +3 -12
  65. package/src/__tests__/component-regressions.test.ts +599 -0
  66. package/src/__tests__/meta-reasoning.test.ts +5 -1
  67. package/src/__tests__/sandbox-security.test.ts +186 -0
  68. package/src/__tests__/self-modifying-agent.test.ts +363 -1
  69. package/src/architecture-evolution/capability-analyzer.ts +56 -40
  70. package/src/architecture-evolution/index.ts +2 -0
  71. package/src/architecture-evolution/parameter-optimizer.ts +52 -34
  72. package/src/architecture-evolution/prompts.ts +179 -41
  73. package/src/constraints/modification-validator.ts +30 -14
  74. package/src/index.ts +14 -0
  75. package/src/meta-reasoning/meta-reasoner.ts +105 -40
  76. package/src/meta-reasoning/prompts.ts +129 -15
  77. package/src/self-modifying-agent.ts +764 -489
  78. package/src/tool-generation/gap-analyzer.ts +19 -18
  79. package/src/tool-generation/index.ts +13 -3
  80. package/src/tool-generation/prompts.ts +41 -8
  81. package/src/tool-generation/tool-generator.ts +59 -33
  82. package/src/tool-generation/tool-sandbox.ts +306 -175
  83. package/src/tool-generation/tool-validator.ts +66 -63
  84. package/src/utils/llm-helper.ts +1 -0
@@ -4,8 +4,9 @@ import type {
4
4
  LLMBackend,
5
5
  ToolSelfGenerationConfig,
6
6
  } from '@cogitator-ai/types';
7
- import { ToolSandbox } from './tool-sandbox';
7
+ import { ToolSandbox, type SandboxTestCase } from './tool-sandbox';
8
8
  import { buildToolValidationPrompt, parseValidationResponse } from './prompts';
9
+ import { llmChat } from '../utils/llm-helper';
9
10
 
10
11
  export interface ToolValidatorOptions {
11
12
  llm?: LLMBackend;
@@ -13,7 +14,14 @@ export interface ToolValidatorOptions {
13
14
  model?: string;
14
15
  }
15
16
 
16
- interface ValidationRule {
17
+ interface JsonPropertySchema {
18
+ type?: string;
19
+ default?: unknown;
20
+ enum?: unknown[];
21
+ examples?: unknown[];
22
+ }
23
+
24
+ export interface ValidationRule {
17
25
  id: string;
18
26
  name: string;
19
27
  severity: 'error' | 'warning' | 'info';
@@ -109,9 +117,9 @@ const STATIC_VALIDATION_RULES: ValidationRule[] = [
109
117
  severity: 'error',
110
118
  check: (code) => {
111
119
  if (code.includes('child_process')) return 'Uses child_process';
112
- if (/\bexec\s*\(/.test(code)) return 'Uses exec()';
113
- if (/\bspawn\s*\(/.test(code)) return 'Uses spawn()';
114
- if (/\bexecSync\s*\(/.test(code)) return 'Uses execSync()';
120
+ if (/(?<![.\w$])exec\s*\(/.test(code)) return 'Uses exec()';
121
+ if (/\bspawn(?:Sync)?\s*\(/.test(code)) return 'Uses spawn()';
122
+ if (/\bexec(?:Sync|File|FileSync)\s*\(/.test(code)) return 'Uses execSync()/execFile()';
115
123
  return null;
116
124
  },
117
125
  },
@@ -155,7 +163,7 @@ export class ToolValidator {
155
163
 
156
164
  async validate(
157
165
  tool: GeneratedTool,
158
- testCases?: Array<{ input: unknown; expectedOutput?: unknown; shouldThrow?: boolean }>
166
+ testCases?: SandboxTestCase[]
159
167
  ): Promise<ToolValidationResult> {
160
168
  const securityIssues: string[] = [];
161
169
  const logicIssues: string[] = [];
@@ -244,7 +252,7 @@ export class ToolValidator {
244
252
  if (result) {
245
253
  if (rule.severity === 'error') {
246
254
  errors.push(`[${rule.id}] ${result}`);
247
- } else if (rule.severity === 'warning') {
255
+ } else {
248
256
  warnings.push(`[${rule.id}] ${result}`);
249
257
  }
250
258
  }
@@ -255,7 +263,7 @@ export class ToolValidator {
255
263
 
256
264
  private async runLLMValidation(
257
265
  tool: GeneratedTool,
258
- testCases: Array<{ input: unknown; expectedOutput?: unknown; shouldThrow?: boolean }>
266
+ testCases: SandboxTestCase[]
259
267
  ): Promise<ToolValidationResult | null> {
260
268
  if (!this.llm) return null;
261
269
 
@@ -268,11 +276,14 @@ export class ToolValidator {
268
276
  ? `Should return ${JSON.stringify(tc.expectedOutput)}`
269
277
  : tc.shouldThrow
270
278
  ? 'Should throw an error'
271
- : 'Should execute successfully',
279
+ : tc.allowThrow
280
+ ? 'Should either return a result or throw a descriptive error'
281
+ : 'Should execute successfully',
272
282
  }))
273
283
  );
274
284
 
275
- const response = await this.callLLM(
285
+ const content = await llmChat(
286
+ this.llm,
276
287
  [
277
288
  {
278
289
  role: 'system',
@@ -282,70 +293,73 @@ Be thorough but practical - focus on real issues.`,
282
293
  },
283
294
  { role: 'user', content: prompt },
284
295
  ],
285
- 0.2
296
+ { model: this.model, temperature: 0.2 }
286
297
  );
287
298
 
288
- return response ? parseValidationResponse(response.content) : null;
299
+ return parseValidationResponse(content);
289
300
  } catch {
290
301
  return null;
291
302
  }
292
303
  }
293
304
 
294
- private generateBasicTestCases(
295
- tool: GeneratedTool
296
- ): Array<{ input: unknown; expectedOutput?: unknown; shouldThrow?: boolean }> {
297
- const testCases: Array<{ input: unknown; expectedOutput?: unknown; shouldThrow?: boolean }> =
298
- [];
305
+ private generateBasicTestCases(tool: GeneratedTool): SandboxTestCase[] {
299
306
  const params = tool.parameters;
300
307
 
301
- if (params.type === 'object' && params.properties) {
302
- const properties = params.properties as Record<string, { type?: string; default?: unknown }>;
303
- const required = Array.isArray(params.required)
304
- ? new Set(params.required as string[])
305
- : new Set<string>();
308
+ if (params.type !== 'object' || !params.properties || typeof params.properties !== 'object') {
309
+ return [{ input: {} }];
310
+ }
306
311
 
307
- const validInput: Record<string, unknown> = {};
308
- for (const [key, schema] of Object.entries(properties)) {
309
- validInput[key] = this.generateSampleValue(schema.type, schema.default);
310
- }
311
- testCases.push({ input: validInput });
312
+ const properties = params.properties as Record<string, JsonPropertySchema>;
313
+ const required = new Set(
314
+ Array.isArray(params.required)
315
+ ? params.required.filter((r): r is string => typeof r === 'string')
316
+ : []
317
+ );
318
+ const entries = Object.entries(properties);
319
+ const testCases: SandboxTestCase[] = [];
312
320
 
313
- const emptyInput: Record<string, unknown> = {};
314
- for (const [key, schema] of Object.entries(properties)) {
315
- if (!required.has(key)) {
316
- emptyInput[key] = this.generateSampleValue(schema.type, schema.default);
321
+ const validInput: Record<string, unknown> = {};
322
+ for (const [key, schema] of entries) {
323
+ validInput[key] = this.generateSampleValue(schema);
324
+ }
325
+ testCases.push({ input: validInput });
326
+
327
+ const hasOptional = entries.some(([key]) => !required.has(key));
328
+ if (required.size > 0 && hasOptional) {
329
+ const requiredOnlyInput: Record<string, unknown> = {};
330
+ for (const [key, schema] of entries) {
331
+ if (required.has(key)) {
332
+ requiredOnlyInput[key] = this.generateSampleValue(schema);
317
333
  }
318
334
  }
319
- if (Object.keys(emptyInput).length > 0) {
320
- testCases.push({ input: emptyInput });
321
- }
335
+ testCases.push({ input: requiredOnlyInput });
336
+ }
322
337
 
323
- const edgeInput: Record<string, unknown> = {};
324
- for (const [key, schema] of Object.entries(properties)) {
325
- edgeInput[key] = this.generateEdgeValue(schema.type);
326
- }
327
- testCases.push({ input: edgeInput });
328
-
329
- if (required.size > 0) {
330
- const missingRequired: Record<string, unknown> = {};
331
- for (const [key, schema] of Object.entries(properties)) {
332
- if (!required.has(key)) {
333
- missingRequired[key] = this.generateSampleValue(schema.type, schema.default);
334
- }
338
+ const edgeInput: Record<string, unknown> = {};
339
+ for (const [key, schema] of entries) {
340
+ edgeInput[key] = this.generateEdgeValue(schema.type);
341
+ }
342
+ testCases.push({ input: edgeInput, allowThrow: true });
343
+
344
+ if (required.size > 0) {
345
+ const missingRequired: Record<string, unknown> = {};
346
+ for (const [key, schema] of entries) {
347
+ if (!required.has(key)) {
348
+ missingRequired[key] = this.generateSampleValue(schema);
335
349
  }
336
- testCases.push({ input: missingRequired, shouldThrow: true });
337
350
  }
338
- } else {
339
- testCases.push({ input: {} });
351
+ testCases.push({ input: missingRequired, shouldThrow: true });
340
352
  }
341
353
 
342
354
  return testCases;
343
355
  }
344
356
 
345
- private generateSampleValue(type?: string, defaultValue?: unknown): unknown {
346
- if (defaultValue !== undefined) return defaultValue;
357
+ private generateSampleValue(schema: JsonPropertySchema): unknown {
358
+ if (schema.default !== undefined) return schema.default;
359
+ if (Array.isArray(schema.examples) && schema.examples.length > 0) return schema.examples[0];
360
+ if (Array.isArray(schema.enum) && schema.enum.length > 0) return schema.enum[0];
347
361
 
348
- switch (type) {
362
+ switch (schema.type) {
349
363
  case 'string':
350
364
  return 'test';
351
365
  case 'number':
@@ -403,15 +417,4 @@ Be thorough but practical - focus on real issues.`,
403
417
 
404
418
  return Math.max(0, Math.min(1, score));
405
419
  }
406
-
407
- private async callLLM(
408
- messages: Array<{ role: 'system' | 'user' | 'assistant'; content: string }>,
409
- temperature: number
410
- ) {
411
- if (!this.llm) return null;
412
- if (this.llm.complete) {
413
- return this.llm.complete({ messages, temperature });
414
- }
415
- return this.llm.chat({ model: this.model, messages, temperature });
416
- }
417
420
  }
@@ -8,6 +8,7 @@ export async function llmChat(
8
8
  if (llm.complete) {
9
9
  const response = await llm.complete({
10
10
  messages,
11
+ model: options?.model,
11
12
  temperature: options?.temperature,
12
13
  maxTokens: options?.maxTokens,
13
14
  });