@cogitator-ai/self-modifying 17.0.18 → 19.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 +114 -50
- 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 +11 -18
- 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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-generator.js","sourceRoot":"","sources":["../../src/tool-generation/tool-generator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"tool-generator.js","sourceRoot":"","sources":["../../src/tool-generation/tool-generator.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAgB,MAAM,KAAK,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAwB,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EACL,6BAA6B,EAC7B,yBAAyB,EACzB,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,WAAW,CAAC;AAoBnB,MAAM,OAAO,aAAa;IACP,GAAG,CAAa;IAChB,MAAM,CAA2B;IACjC,KAAK,CAAS;IACd,SAAS,CAAgB;IACzB,OAAO,CAAc;IAEtC,YAAY,OAA6B;QACvC,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,SAAS,CAAC;QACxC,IAAI,CAAC,SAAS,GAAG,IAAI,aAAa,CAAC;YACjC,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,KAAK,EAA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@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { ToolSandboxConfig, ToolSandboxResult, GeneratedTool } from '@cogitator-ai/types';
|
|
2
2
|
export declare const DEFAULT_SANDBOX_CONFIG: ToolSandboxConfig;
|
|
3
|
+
export interface SandboxTestCase {
|
|
4
|
+
input: unknown;
|
|
5
|
+
expectedOutput?: unknown;
|
|
6
|
+
shouldThrow?: boolean;
|
|
7
|
+
allowThrow?: boolean;
|
|
8
|
+
}
|
|
3
9
|
export declare class ToolSandbox {
|
|
4
10
|
private readonly config;
|
|
5
11
|
constructor(config?: Partial<ToolSandboxConfig>);
|
|
6
12
|
execute(tool: GeneratedTool, params: unknown): Promise<ToolSandboxResult>;
|
|
7
|
-
testWithCases(tool: GeneratedTool, testCases:
|
|
8
|
-
input: unknown;
|
|
9
|
-
expectedOutput?: unknown;
|
|
10
|
-
shouldThrow?: boolean;
|
|
11
|
-
}>): Promise<{
|
|
13
|
+
testWithCases(tool: GeneratedTool, testCases: SandboxTestCase[]): Promise<{
|
|
12
14
|
passed: number;
|
|
13
15
|
failed: number;
|
|
14
16
|
results: Array<{
|
|
@@ -19,9 +21,10 @@ export declare class ToolSandbox {
|
|
|
19
21
|
executionTime: number;
|
|
20
22
|
}>;
|
|
21
23
|
}>;
|
|
24
|
+
private run;
|
|
22
25
|
private validateImplementation;
|
|
23
26
|
private executeInWorker;
|
|
24
27
|
private executeUnsandboxed;
|
|
25
28
|
}
|
|
26
|
-
export declare function deepEqual(a: unknown, b: unknown
|
|
29
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
27
30
|
//# sourceMappingURL=tool-sandbox.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-sandbox.d.ts","sourceRoot":"","sources":["../../src/tool-generation/tool-sandbox.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAE/F,eAAO,MAAM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
|
|
6
6
|
allowedModules: [],
|
|
7
7
|
isolationLevel: 'strict',
|
|
8
8
|
};
|
|
9
|
-
const WORKER_SOURCE =
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
'const context = vm.createContext(sandbox);',
|
|
29
|
-
'try {',
|
|
30
|
-
' const wrapped = "(async function sandboxedExecution() {\\n" +',
|
|
31
|
-
' code + "\\n" +',
|
|
32
|
-
' "if (typeof execute === \\"function\\") { return await execute(params); }\\n" +',
|
|
33
|
-
' "throw new Error(\\"Implementation must define an execute function\\");\\n" +',
|
|
34
|
-
' "})()";',
|
|
35
|
-
' const promise = vm.runInContext(wrapped, context);',
|
|
36
|
-
' Promise.resolve(promise).then(',
|
|
37
|
-
' (result) => { parentPort.postMessage({ success: true, result, logs }); },',
|
|
38
|
-
' (err) => { parentPort.postMessage({ success: false, error: err instanceof Error ? err.message : String(err), logs }); }',
|
|
39
|
-
' );',
|
|
40
|
-
'} catch (err) {',
|
|
41
|
-
' parentPort.postMessage({ success: false, error: err instanceof Error ? err.message : String(err), logs });',
|
|
42
|
-
'}',
|
|
9
|
+
const WORKER_SOURCE = String.raw `
|
|
10
|
+
const { parentPort, workerData } = require('node:worker_threads');
|
|
11
|
+
const vm = require('node:vm');
|
|
12
|
+
const { code, paramsJson, timeout } = workerData;
|
|
13
|
+
|
|
14
|
+
const BOOTSTRAP = [
|
|
15
|
+
'(function () {',
|
|
16
|
+
' "use strict";',
|
|
17
|
+
' const logs = [];',
|
|
18
|
+
' const format = (args) => args.map((v) => {',
|
|
19
|
+
' if (typeof v === "string") return v;',
|
|
20
|
+
' try { const s = JSON.stringify(v); return s === undefined ? String(v) : s; } catch (e) { return String(v); }',
|
|
21
|
+
' }).join(" ");',
|
|
22
|
+
' const logger = (level) => (...args) => { if (logs.length < 1000) logs.push("[" + level + "] " + format(args)); };',
|
|
23
|
+
' Object.defineProperty(globalThis, "console", { value: Object.freeze({',
|
|
24
|
+
' log: logger("LOG"), info: logger("INFO"), debug: logger("DEBUG"), warn: logger("WARN"), error: logger("ERROR"),',
|
|
25
|
+
' }) });',
|
|
26
|
+
' Object.defineProperty(globalThis, "__sandboxState", { value: { status: "pending", payload: "", hasValue: false, logs } });',
|
|
27
|
+
'})();',
|
|
43
28
|
].join('\n');
|
|
29
|
+
|
|
30
|
+
const runner = [
|
|
31
|
+
'(async function sandboxedExecution(params) {',
|
|
32
|
+
code,
|
|
33
|
+
' if (typeof execute !== "function") { throw new Error("Implementation must define an execute function"); }',
|
|
34
|
+
' return await execute(params);',
|
|
35
|
+
'})(JSON.parse(' + JSON.stringify(paramsJson) + ')).then(',
|
|
36
|
+
' (value) => {',
|
|
37
|
+
' const state = globalThis.__sandboxState;',
|
|
38
|
+
' try {',
|
|
39
|
+
' const json = JSON.stringify(value);',
|
|
40
|
+
' state.hasValue = json !== undefined;',
|
|
41
|
+
' state.payload = json === undefined ? "" : json;',
|
|
42
|
+
' state.status = "fulfilled";',
|
|
43
|
+
' } catch (e) {',
|
|
44
|
+
' state.payload = "Tool returned a non-serializable value: " + (e && e.message);',
|
|
45
|
+
' state.status = "failed";',
|
|
46
|
+
' }',
|
|
47
|
+
' },',
|
|
48
|
+
' (err) => {',
|
|
49
|
+
' const state = globalThis.__sandboxState;',
|
|
50
|
+
' state.payload = err && typeof err.message === "string" ? err.message : String(err);',
|
|
51
|
+
' state.status = "rejected";',
|
|
52
|
+
' }',
|
|
53
|
+
');',
|
|
54
|
+
].join('\n');
|
|
55
|
+
|
|
56
|
+
const asString = (value) => (typeof value === 'string' ? value : '');
|
|
57
|
+
const readState = (context, expression) => {
|
|
58
|
+
try {
|
|
59
|
+
return vm.runInContext(expression, context);
|
|
60
|
+
} catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const readLogs = (context) => {
|
|
65
|
+
const raw = readState(context, 'JSON.stringify(__sandboxState.logs)');
|
|
66
|
+
if (typeof raw !== 'string') return [];
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(raw);
|
|
69
|
+
return Array.isArray(parsed) ? parsed.filter((l) => typeof l === 'string') : [];
|
|
70
|
+
} catch {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const context = vm.createContext(Object.create(null), {
|
|
76
|
+
codeGeneration: { strings: false, wasm: false },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
let started = false;
|
|
80
|
+
try {
|
|
81
|
+
vm.runInContext(BOOTSTRAP, context);
|
|
82
|
+
vm.runInContext(runner, context, { timeout });
|
|
83
|
+
started = true;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
parentPort.postMessage({
|
|
86
|
+
success: false,
|
|
87
|
+
thrown: true,
|
|
88
|
+
error: err && typeof err.message === 'string' ? err.message : String(err),
|
|
89
|
+
logs: readLogs(context),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (started) setImmediate(() => {
|
|
94
|
+
const status = asString(readState(context, '__sandboxState.status'));
|
|
95
|
+
const payload = asString(readState(context, '__sandboxState.payload'));
|
|
96
|
+
const logs = readLogs(context);
|
|
97
|
+
|
|
98
|
+
if (status === 'fulfilled') {
|
|
99
|
+
let result;
|
|
100
|
+
if (readState(context, '__sandboxState.hasValue') === true) {
|
|
101
|
+
try {
|
|
102
|
+
result = JSON.parse(payload);
|
|
103
|
+
} catch {
|
|
104
|
+
parentPort.postMessage({ success: false, thrown: false, error: 'Tool returned an unreadable value', logs });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
parentPort.postMessage({ success: true, result, logs });
|
|
109
|
+
} else if (status === 'rejected') {
|
|
110
|
+
parentPort.postMessage({ success: false, thrown: true, error: payload, logs });
|
|
111
|
+
} else if (status === 'failed') {
|
|
112
|
+
parentPort.postMessage({ success: false, thrown: false, error: payload, logs });
|
|
113
|
+
} else {
|
|
114
|
+
parentPort.postMessage({
|
|
115
|
+
success: false,
|
|
116
|
+
thrown: false,
|
|
117
|
+
error: 'Tool execution never settled: execute() returned a promise that cannot resolve inside the sandbox',
|
|
118
|
+
logs,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
`;
|
|
44
123
|
export class ToolSandbox {
|
|
45
124
|
config;
|
|
46
125
|
constructor(config = {}) {
|
|
47
126
|
this.config = { ...DEFAULT_SANDBOX_CONFIG, ...config };
|
|
48
127
|
}
|
|
49
128
|
async execute(tool, params) {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
this.validateImplementation(tool.implementation);
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
return {
|
|
56
|
-
success: false,
|
|
57
|
-
error: error instanceof Error ? error.message : String(error),
|
|
58
|
-
executionTime: Date.now() - startTime,
|
|
59
|
-
memoryUsed: 0,
|
|
60
|
-
logs: [],
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
if (!this.config.enabled) {
|
|
64
|
-
return this.executeUnsandboxed(tool, params, startTime);
|
|
65
|
-
}
|
|
66
|
-
return this.executeInWorker(tool.implementation, params, startTime);
|
|
129
|
+
const run = await this.run(tool, params);
|
|
130
|
+
return run.result;
|
|
67
131
|
}
|
|
68
132
|
async testWithCases(tool, testCases) {
|
|
69
133
|
const results = [];
|
|
70
134
|
for (const testCase of testCases) {
|
|
71
|
-
const execResult = await this.
|
|
72
|
-
let passed
|
|
135
|
+
const { result: execResult, thrownByTool } = await this.run(tool, testCase.input);
|
|
136
|
+
let passed;
|
|
73
137
|
if (testCase.shouldThrow) {
|
|
74
|
-
passed = !execResult.success;
|
|
138
|
+
passed = !execResult.success && thrownByTool;
|
|
75
139
|
}
|
|
76
140
|
else if (testCase.expectedOutput !== undefined) {
|
|
77
141
|
passed = execResult.success && deepEqual(execResult.result, testCase.expectedOutput);
|
|
78
142
|
}
|
|
143
|
+
else if (testCase.allowThrow) {
|
|
144
|
+
passed = execResult.success || thrownByTool;
|
|
145
|
+
}
|
|
79
146
|
else {
|
|
80
147
|
passed = execResult.success;
|
|
81
148
|
}
|
|
@@ -93,6 +160,32 @@ export class ToolSandbox {
|
|
|
93
160
|
results,
|
|
94
161
|
};
|
|
95
162
|
}
|
|
163
|
+
async run(tool, params) {
|
|
164
|
+
const startTime = Date.now();
|
|
165
|
+
try {
|
|
166
|
+
this.validateImplementation(tool.implementation);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
return {
|
|
170
|
+
result: failure(error instanceof Error ? error.message : String(error), startTime),
|
|
171
|
+
thrownByTool: false,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
let paramsJson;
|
|
175
|
+
try {
|
|
176
|
+
paramsJson = JSON.stringify(params ?? null);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
return {
|
|
180
|
+
result: failure(`Parameters must be JSON-serializable: ${error instanceof Error ? error.message : String(error)}`, startTime),
|
|
181
|
+
thrownByTool: false,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (!this.config.enabled) {
|
|
185
|
+
return this.executeUnsandboxed(tool, JSON.parse(paramsJson), startTime);
|
|
186
|
+
}
|
|
187
|
+
return this.executeInWorker(tool.implementation, paramsJson, startTime);
|
|
188
|
+
}
|
|
96
189
|
validateImplementation(code) {
|
|
97
190
|
const forbidden = [
|
|
98
191
|
{ pattern: /\beval\s*\(/, label: 'eval()' },
|
|
@@ -153,28 +246,28 @@ export class ToolSandbox {
|
|
|
153
246
|
throw new Error(`Implementation too large: ${lines} lines (max 200)`);
|
|
154
247
|
}
|
|
155
248
|
}
|
|
156
|
-
|
|
157
|
-
let serializableParams;
|
|
158
|
-
try {
|
|
159
|
-
serializableParams = JSON.parse(JSON.stringify(params ?? null));
|
|
160
|
-
}
|
|
161
|
-
catch {
|
|
162
|
-
serializableParams = null;
|
|
163
|
-
}
|
|
249
|
+
executeInWorker(code, paramsJson, startTime) {
|
|
164
250
|
return new Promise((resolve) => {
|
|
165
251
|
let settled = false;
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
252
|
+
let worker;
|
|
253
|
+
const settle = (result, thrownByTool = false) => {
|
|
254
|
+
if (settled)
|
|
255
|
+
return;
|
|
256
|
+
settled = true;
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
if (worker)
|
|
259
|
+
void worker.terminate();
|
|
260
|
+
resolve({ result, thrownByTool });
|
|
171
261
|
};
|
|
262
|
+
const timer = setTimeout(() => {
|
|
263
|
+
settle(failure(`Execution timeout: exceeded ${this.config.maxExecutionTime}ms`, startTime));
|
|
264
|
+
}, this.config.maxExecutionTime);
|
|
172
265
|
const maxMemoryMb = Math.max(4, Math.ceil(this.config.maxMemory / (1024 * 1024)));
|
|
173
|
-
let worker;
|
|
174
266
|
try {
|
|
175
267
|
worker = new Worker(WORKER_SOURCE, {
|
|
176
268
|
eval: true,
|
|
177
|
-
|
|
269
|
+
env: {},
|
|
270
|
+
workerData: { code, paramsJson, timeout: this.config.maxExecutionTime },
|
|
178
271
|
resourceLimits: {
|
|
179
272
|
maxOldGenerationSizeMb: maxMemoryMb,
|
|
180
273
|
maxYoungGenerationSizeMb: Math.max(2, Math.ceil(maxMemoryMb / 4)),
|
|
@@ -182,63 +275,38 @@ export class ToolSandbox {
|
|
|
182
275
|
});
|
|
183
276
|
}
|
|
184
277
|
catch (error) {
|
|
185
|
-
settle({
|
|
186
|
-
success: false,
|
|
187
|
-
error: `Failed to create sandbox worker: ${error instanceof Error ? error.message : String(error)}`,
|
|
188
|
-
executionTime: Date.now() - startTime,
|
|
189
|
-
memoryUsed: 0,
|
|
190
|
-
logs: [],
|
|
191
|
-
});
|
|
278
|
+
settle(failure(`Failed to create sandbox worker: ${error instanceof Error ? error.message : String(error)}`, startTime));
|
|
192
279
|
return;
|
|
193
280
|
}
|
|
194
|
-
const timer = setTimeout(() => {
|
|
195
|
-
void worker.terminate();
|
|
196
|
-
settle({
|
|
197
|
-
success: false,
|
|
198
|
-
error: `Execution timeout: exceeded ${this.config.maxExecutionTime}ms`,
|
|
199
|
-
executionTime: Date.now() - startTime,
|
|
200
|
-
memoryUsed: 0,
|
|
201
|
-
logs: [],
|
|
202
|
-
});
|
|
203
|
-
}, this.config.maxExecutionTime);
|
|
204
281
|
worker.on('message', (msg) => {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
success: msg.success,
|
|
208
|
-
result: msg.result,
|
|
209
|
-
error: msg.error,
|
|
210
|
-
executionTime: Date.now() - startTime,
|
|
211
|
-
memoryUsed: estimateMemoryUsage(msg.result),
|
|
212
|
-
logs: msg.logs ?? [],
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
worker.on('error', (error) => {
|
|
216
|
-
clearTimeout(timer);
|
|
217
|
-
settle({
|
|
218
|
-
success: false,
|
|
219
|
-
error: `Sandbox worker error: ${error.message}`,
|
|
220
|
-
executionTime: Date.now() - startTime,
|
|
221
|
-
memoryUsed: 0,
|
|
222
|
-
logs: [],
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
worker.on('exit', (exitCode) => {
|
|
226
|
-
clearTimeout(timer);
|
|
227
|
-
if (exitCode !== 0) {
|
|
282
|
+
const logs = msg.logs ?? [];
|
|
283
|
+
if (msg.success) {
|
|
228
284
|
settle({
|
|
229
|
-
success:
|
|
230
|
-
|
|
231
|
-
? `Execution exceeded memory limit (${this.config.maxMemory} bytes)`
|
|
232
|
-
: `Sandbox worker exited with code ${exitCode}`,
|
|
285
|
+
success: true,
|
|
286
|
+
result: msg.result,
|
|
233
287
|
executionTime: Date.now() - startTime,
|
|
234
|
-
memoryUsed:
|
|
235
|
-
logs
|
|
288
|
+
memoryUsed: estimateMemoryUsage(msg.result),
|
|
289
|
+
logs,
|
|
236
290
|
});
|
|
237
291
|
}
|
|
292
|
+
else {
|
|
293
|
+
settle({ ...failure(msg.error ?? 'Tool execution failed', startTime), logs }, msg.thrown);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
worker.on('error', (error) => {
|
|
297
|
+
settle(failure(error.code === 'ERR_WORKER_OUT_OF_MEMORY'
|
|
298
|
+
? `Execution exceeded memory limit (${this.config.maxMemory} bytes)`
|
|
299
|
+
: `Sandbox worker error: ${error.message}`, startTime));
|
|
300
|
+
});
|
|
301
|
+
worker.on('exit', (exitCode) => {
|
|
302
|
+
settle(failure(exitCode === 134 || exitCode === 137
|
|
303
|
+
? `Execution exceeded memory limit (${this.config.maxMemory} bytes)`
|
|
304
|
+
: `Sandbox worker exited with code ${exitCode} before reporting a result`, startTime));
|
|
238
305
|
});
|
|
239
306
|
});
|
|
240
307
|
}
|
|
241
308
|
async executeUnsandboxed(tool, params, startTime) {
|
|
309
|
+
let timer;
|
|
242
310
|
try {
|
|
243
311
|
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
244
312
|
const factory = new Function(`
|
|
@@ -247,29 +315,56 @@ export class ToolSandbox {
|
|
|
247
315
|
return execute;
|
|
248
316
|
`);
|
|
249
317
|
const execute = factory();
|
|
318
|
+
if (typeof execute !== 'function') {
|
|
319
|
+
return {
|
|
320
|
+
result: failure('Implementation must define an execute function', startTime),
|
|
321
|
+
thrownByTool: false,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
const timeout = new Promise((_, reject) => {
|
|
325
|
+
timer = setTimeout(() => reject(new SandboxTimeoutError(this.config.maxExecutionTime)), this.config.maxExecutionTime);
|
|
326
|
+
});
|
|
250
327
|
const result = await Promise.race([
|
|
251
328
|
execute(params),
|
|
252
|
-
|
|
329
|
+
timeout,
|
|
253
330
|
]);
|
|
254
331
|
return {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
332
|
+
result: {
|
|
333
|
+
success: true,
|
|
334
|
+
result,
|
|
335
|
+
executionTime: Date.now() - startTime,
|
|
336
|
+
memoryUsed: estimateMemoryUsage(result),
|
|
337
|
+
logs: [],
|
|
338
|
+
},
|
|
339
|
+
thrownByTool: false,
|
|
260
340
|
};
|
|
261
341
|
}
|
|
262
342
|
catch (error) {
|
|
263
343
|
return {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
executionTime: Date.now() - startTime,
|
|
267
|
-
memoryUsed: 0,
|
|
268
|
-
logs: [],
|
|
344
|
+
result: failure(error instanceof Error ? error.message : String(error), startTime),
|
|
345
|
+
thrownByTool: !(error instanceof SandboxTimeoutError),
|
|
269
346
|
};
|
|
270
347
|
}
|
|
348
|
+
finally {
|
|
349
|
+
clearTimeout(timer);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
class SandboxTimeoutError extends Error {
|
|
354
|
+
constructor(limit) {
|
|
355
|
+
super(`Execution timeout: exceeded ${limit}ms`);
|
|
356
|
+
this.name = 'SandboxTimeoutError';
|
|
271
357
|
}
|
|
272
358
|
}
|
|
359
|
+
function failure(error, startTime) {
|
|
360
|
+
return {
|
|
361
|
+
success: false,
|
|
362
|
+
error,
|
|
363
|
+
executionTime: Date.now() - startTime,
|
|
364
|
+
memoryUsed: 0,
|
|
365
|
+
logs: [],
|
|
366
|
+
};
|
|
367
|
+
}
|
|
273
368
|
function estimateMemoryUsage(value) {
|
|
274
369
|
try {
|
|
275
370
|
const str = JSON.stringify(value);
|
|
@@ -279,55 +374,60 @@ function estimateMemoryUsage(value) {
|
|
|
279
374
|
return 0;
|
|
280
375
|
}
|
|
281
376
|
}
|
|
282
|
-
export function deepEqual(a, b
|
|
377
|
+
export function deepEqual(a, b) {
|
|
378
|
+
return deepEqualInner(a, b, new Map());
|
|
379
|
+
}
|
|
380
|
+
function deepEqualInner(a, b, visited) {
|
|
283
381
|
if (Object.is(a, b))
|
|
284
382
|
return true;
|
|
285
|
-
if (typeof a !== typeof b)
|
|
383
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null)
|
|
286
384
|
return false;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
385
|
+
const pairs = visited.get(a);
|
|
386
|
+
if (pairs?.has(b))
|
|
387
|
+
return true;
|
|
388
|
+
if (pairs)
|
|
389
|
+
pairs.add(b);
|
|
390
|
+
else
|
|
391
|
+
visited.set(a, new Set([b]));
|
|
392
|
+
if (a instanceof Date || b instanceof Date) {
|
|
393
|
+
return a instanceof Date && b instanceof Date && a.getTime() === b.getTime();
|
|
291
394
|
}
|
|
292
|
-
if (a instanceof RegExp
|
|
293
|
-
return a.source === b.source && a.flags === b.flags;
|
|
395
|
+
if (a instanceof RegExp || b instanceof RegExp) {
|
|
396
|
+
return (a instanceof RegExp && b instanceof RegExp && a.source === b.source && a.flags === b.flags);
|
|
294
397
|
}
|
|
295
|
-
if (a instanceof Map
|
|
296
|
-
if (a.size !== b.size)
|
|
398
|
+
if (a instanceof Map || b instanceof Map) {
|
|
399
|
+
if (!(a instanceof Map && b instanceof Map) || a.size !== b.size)
|
|
297
400
|
return false;
|
|
298
401
|
for (const [key, val] of a) {
|
|
299
|
-
if (!b.has(key) || !
|
|
402
|
+
if (!b.has(key) || !deepEqualInner(val, b.get(key), visited))
|
|
300
403
|
return false;
|
|
301
404
|
}
|
|
302
405
|
return true;
|
|
303
406
|
}
|
|
304
|
-
if (a instanceof Set
|
|
305
|
-
if (a.size !== b.size)
|
|
407
|
+
if (a instanceof Set || b instanceof Set) {
|
|
408
|
+
if (!(a instanceof Set && b instanceof Set) || a.size !== b.size)
|
|
306
409
|
return false;
|
|
410
|
+
const unmatched = [...b];
|
|
307
411
|
for (const val of a) {
|
|
308
|
-
|
|
412
|
+
const idx = unmatched.findIndex((candidate) => deepEqualInner(val, candidate, visited));
|
|
413
|
+
if (idx === -1)
|
|
309
414
|
return false;
|
|
415
|
+
unmatched.splice(idx, 1);
|
|
310
416
|
}
|
|
311
417
|
return true;
|
|
312
418
|
}
|
|
313
|
-
if (Array.isArray(a)
|
|
314
|
-
if (a.length !== b.length)
|
|
419
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
420
|
+
if (!(Array.isArray(a) && Array.isArray(b)) || a.length !== b.length)
|
|
315
421
|
return false;
|
|
316
|
-
return a.every((val, i) =>
|
|
422
|
+
return a.every((val, i) => deepEqualInner(val, b[i], visited));
|
|
317
423
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const keysB = Object.keys(bObj).filter((k) => bObj[k] !== undefined);
|
|
327
|
-
if (keysA.length !== keysB.length)
|
|
328
|
-
return false;
|
|
329
|
-
return keysA.every((key) => deepEqual(aObj[key], bObj[key], seen));
|
|
330
|
-
}
|
|
331
|
-
return false;
|
|
424
|
+
const aObj = a;
|
|
425
|
+
const bObj = b;
|
|
426
|
+
const keysA = Object.keys(aObj).filter((k) => aObj[k] !== undefined);
|
|
427
|
+
const keysB = Object.keys(bObj).filter((k) => bObj[k] !== undefined);
|
|
428
|
+
if (keysA.length !== keysB.length)
|
|
429
|
+
return false;
|
|
430
|
+
return keysA.every((key) => Object.prototype.hasOwnProperty.call(bObj, key) &&
|
|
431
|
+
deepEqualInner(aObj[key], bObj[key], visited));
|
|
332
432
|
}
|
|
333
433
|
//# sourceMappingURL=tool-sandbox.js.map
|