@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.
- package/README.md +112 -48
- package/dist/architecture-evolution/capability-analyzer.d.ts.map +1 -1
- package/dist/architecture-evolution/capability-analyzer.js +38 -37
- package/dist/architecture-evolution/capability-analyzer.js.map +1 -1
- package/dist/architecture-evolution/index.d.ts +2 -2
- package/dist/architecture-evolution/index.d.ts.map +1 -1
- package/dist/architecture-evolution/index.js +1 -1
- package/dist/architecture-evolution/index.js.map +1 -1
- package/dist/architecture-evolution/parameter-optimizer.d.ts +5 -4
- package/dist/architecture-evolution/parameter-optimizer.d.ts.map +1 -1
- package/dist/architecture-evolution/parameter-optimizer.js +36 -31
- package/dist/architecture-evolution/parameter-optimizer.js.map +1 -1
- package/dist/architecture-evolution/prompts.d.ts +21 -2
- package/dist/architecture-evolution/prompts.d.ts.map +1 -1
- package/dist/architecture-evolution/prompts.js +150 -36
- package/dist/architecture-evolution/prompts.js.map +1 -1
- package/dist/constraints/modification-validator.d.ts +1 -0
- package/dist/constraints/modification-validator.d.ts.map +1 -1
- package/dist/constraints/modification-validator.js +30 -14
- package/dist/constraints/modification-validator.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/meta-reasoning/meta-reasoner.d.ts +3 -0
- package/dist/meta-reasoning/meta-reasoner.d.ts.map +1 -1
- package/dist/meta-reasoning/meta-reasoner.js +101 -33
- package/dist/meta-reasoning/meta-reasoner.js.map +1 -1
- package/dist/meta-reasoning/prompts.d.ts +10 -14
- package/dist/meta-reasoning/prompts.d.ts.map +1 -1
- package/dist/meta-reasoning/prompts.js +109 -1
- package/dist/meta-reasoning/prompts.js.map +1 -1
- package/dist/self-modifying-agent.d.ts +83 -17
- package/dist/self-modifying-agent.d.ts.map +1 -1
- package/dist/self-modifying-agent.js +557 -444
- package/dist/self-modifying-agent.js.map +1 -1
- package/dist/tool-generation/gap-analyzer.d.ts +0 -1
- package/dist/tool-generation/gap-analyzer.d.ts.map +1 -1
- package/dist/tool-generation/gap-analyzer.js +14 -14
- package/dist/tool-generation/gap-analyzer.js.map +1 -1
- package/dist/tool-generation/index.d.ts +3 -3
- package/dist/tool-generation/index.d.ts.map +1 -1
- package/dist/tool-generation/index.js +2 -2
- package/dist/tool-generation/index.js.map +1 -1
- package/dist/tool-generation/prompts.d.ts +1 -0
- package/dist/tool-generation/prompts.d.ts.map +1 -1
- package/dist/tool-generation/prompts.js +33 -8
- package/dist/tool-generation/prompts.js.map +1 -1
- package/dist/tool-generation/tool-generator.d.ts +5 -5
- package/dist/tool-generation/tool-generator.d.ts.map +1 -1
- package/dist/tool-generation/tool-generator.js +41 -28
- package/dist/tool-generation/tool-generator.js.map +1 -1
- package/dist/tool-generation/tool-sandbox.d.ts +9 -6
- package/dist/tool-generation/tool-sandbox.d.ts.map +1 -1
- package/dist/tool-generation/tool-sandbox.js +257 -157
- package/dist/tool-generation/tool-sandbox.js.map +1 -1
- package/dist/tool-generation/tool-validator.d.ts +3 -8
- package/dist/tool-generation/tool-validator.d.ts.map +1 -1
- package/dist/tool-generation/tool-validator.js +53 -54
- package/dist/tool-generation/tool-validator.js.map +1 -1
- package/dist/utils/llm-helper.d.ts.map +1 -1
- package/dist/utils/llm-helper.js +1 -0
- package/dist/utils/llm-helper.js.map +1 -1
- package/package.json +3 -12
- package/src/__tests__/component-regressions.test.ts +599 -0
- package/src/__tests__/meta-reasoning.test.ts +5 -1
- package/src/__tests__/sandbox-security.test.ts +186 -0
- package/src/__tests__/self-modifying-agent.test.ts +363 -1
- package/src/architecture-evolution/capability-analyzer.ts +56 -40
- package/src/architecture-evolution/index.ts +2 -0
- package/src/architecture-evolution/parameter-optimizer.ts +52 -34
- package/src/architecture-evolution/prompts.ts +179 -41
- package/src/constraints/modification-validator.ts +30 -14
- package/src/index.ts +14 -0
- package/src/meta-reasoning/meta-reasoner.ts +105 -40
- package/src/meta-reasoning/prompts.ts +129 -15
- package/src/self-modifying-agent.ts +764 -489
- package/src/tool-generation/gap-analyzer.ts +19 -18
- package/src/tool-generation/index.ts +13 -3
- package/src/tool-generation/prompts.ts +41 -8
- package/src/tool-generation/tool-generator.ts +59 -33
- package/src/tool-generation/tool-sandbox.ts +306 -175
- package/src/tool-generation/tool-validator.ts +66 -63
- 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
|
|
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 (
|
|
113
|
-
if (/\bspawn
|
|
114
|
-
if (/\
|
|
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?:
|
|
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
|
|
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:
|
|
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
|
-
:
|
|
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
|
|
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
|
|
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
|
|
302
|
-
|
|
303
|
-
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
335
|
+
testCases.push({ input: requiredOnlyInput });
|
|
336
|
+
}
|
|
322
337
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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
|
-
|
|
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(
|
|
346
|
-
if (
|
|
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
|
}
|