dsh-awesome-optimizer 1.8.0 → 2.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 +16 -15
- package/README.zh.md +16 -15
- package/package.json +6 -2
- package/scripts/benchmark.mjs +53 -2
- package/src/custom-prompt.js +1 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +15 -1
- package/src/persistent-eval/daemon-pool.js +91 -0
- package/src/persistent-eval/index.d.ts +54 -0
- package/src/persistent-eval/index.js +161 -0
- package/src/persistent-eval/stdio-runner.js +270 -0
- package/src/persistent-eval/workers/node_worker.mjs +238 -0
- package/src/persistent-eval/workers/py_worker.py +182 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
import { DaemonPool } from './daemon-pool.js';
|
|
3
|
+
|
|
4
|
+
export { DaemonPool, PY_WORKER_SCRIPT, NODE_WORKER_SCRIPT } from './daemon-pool.js';
|
|
5
|
+
export { StdioWorkerRunner } from './stdio-runner.js';
|
|
6
|
+
export const EVAL_PYTHON_TOOL = 'eval_python';
|
|
7
|
+
export const EVAL_NODE_TOOL = 'eval_node';
|
|
8
|
+
|
|
9
|
+
export const PersistentEvalConfig = z.object({
|
|
10
|
+
enabled: z.boolean().default(true),
|
|
11
|
+
python: z.boolean().default(true),
|
|
12
|
+
node: z.boolean().default(true),
|
|
13
|
+
timeoutMs: z.number().step(1).min(50).max(120000).default(15000),
|
|
14
|
+
maxOutputBytes: z.number().step(1).min(64).max(65536).default(8192)
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Render friendly and compact text representation of execution output for agent response.
|
|
19
|
+
*/
|
|
20
|
+
export function formatEvalOutput(resp) {
|
|
21
|
+
if (!resp) return 'No response returned from kernel.';
|
|
22
|
+
|
|
23
|
+
const parts = [];
|
|
24
|
+
|
|
25
|
+
if (resp.stdout && resp.stdout.length > 0) {
|
|
26
|
+
parts.push(resp.stdout.trimEnd());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (resp.result !== null && resp.result !== undefined) {
|
|
30
|
+
let repr = resp.resultRepr;
|
|
31
|
+
if (!repr) {
|
|
32
|
+
try {
|
|
33
|
+
repr = typeof resp.result === 'object' ? JSON.stringify(resp.result) : String(resp.result);
|
|
34
|
+
} catch {
|
|
35
|
+
repr = String(resp.result);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
parts.push(`=> ${repr}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!resp.success) {
|
|
42
|
+
if (resp.error) parts.push(`[Error: ${resp.error}]`);
|
|
43
|
+
if (resp.stderr && resp.stderr.length > 0) {
|
|
44
|
+
parts.push(resp.stderr.trimEnd());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (parts.length === 0) {
|
|
49
|
+
return resp.success ? '<Executed successfully with no output>' : '<Execution failed with no output>';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return parts.join('\n');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Mount stateful persistent Python and Node evaluation tools around the DSH agent loop.
|
|
57
|
+
*/
|
|
58
|
+
export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
59
|
+
const config = PersistentEvalConfig(rawConfig);
|
|
60
|
+
if (!config.enabled) return null;
|
|
61
|
+
|
|
62
|
+
const pool = new DaemonPool({
|
|
63
|
+
timeoutMs: config.timeoutMs,
|
|
64
|
+
maxOutputBytes: config.maxOutputBytes
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Clean up session workers when session ends
|
|
68
|
+
ctx.on('session/event', (session, event) => {
|
|
69
|
+
if (event?.type === 'session/destroy' || event?.type === 'session/close') {
|
|
70
|
+
pool.destroySession(session);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (config.python) {
|
|
75
|
+
ctx.tools.register({
|
|
76
|
+
name: EVAL_PYTHON_TOOL,
|
|
77
|
+
description: [
|
|
78
|
+
'Execute Python code inside a persistent stateful kernel.',
|
|
79
|
+
'Variables, functions, imports, and state persist across calls within the session.',
|
|
80
|
+
'Use for exploratory calculations, data processing, kinematic/algorithmic solving, and intermediate analysis without writing temp files to disk.'
|
|
81
|
+
].join(' '),
|
|
82
|
+
parameters: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
additionalProperties: false,
|
|
85
|
+
required: ['code'],
|
|
86
|
+
properties: {
|
|
87
|
+
code: {
|
|
88
|
+
type: 'string',
|
|
89
|
+
description: 'Python code to execute or expression to evaluate.'
|
|
90
|
+
},
|
|
91
|
+
reset: {
|
|
92
|
+
type: 'boolean',
|
|
93
|
+
description: 'Reset kernel state before execution. Defaults to false.'
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
output: {
|
|
98
|
+
schema: { type: 'string' },
|
|
99
|
+
render: (_args, value) => [{ type: 'text', text: value }]
|
|
100
|
+
},
|
|
101
|
+
isConcurrencySafe: () => false,
|
|
102
|
+
async execute(args, exec) {
|
|
103
|
+
exec?.signal?.throwIfAborted();
|
|
104
|
+
const runner = pool.getPythonRunner(exec?.session);
|
|
105
|
+
const code = args?.code || '';
|
|
106
|
+
const reset = Boolean(args?.reset);
|
|
107
|
+
|
|
108
|
+
const response = await runner.execute(code, { reset, timeoutMs: config.timeoutMs });
|
|
109
|
+
return formatEvalOutput(response);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (config.node) {
|
|
115
|
+
ctx.tools.register({
|
|
116
|
+
name: EVAL_NODE_TOOL,
|
|
117
|
+
description: [
|
|
118
|
+
'Execute JavaScript code inside a persistent stateful Node.js VM context.',
|
|
119
|
+
'Variables, functions, and state persist across calls within the session. Supports top-level await.',
|
|
120
|
+
'Use for rapid expression evaluation, AST exploration, and data transformations without writing temp files to disk.'
|
|
121
|
+
].join(' '),
|
|
122
|
+
parameters: {
|
|
123
|
+
type: 'object',
|
|
124
|
+
additionalProperties: false,
|
|
125
|
+
required: ['code'],
|
|
126
|
+
properties: {
|
|
127
|
+
code: {
|
|
128
|
+
type: 'string',
|
|
129
|
+
description: 'JavaScript code to execute. Can be expressions, statements, or async code.'
|
|
130
|
+
},
|
|
131
|
+
reset: {
|
|
132
|
+
type: 'boolean',
|
|
133
|
+
description: 'Reset VM context before execution. Defaults to false.'
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
output: {
|
|
138
|
+
schema: { type: 'string' },
|
|
139
|
+
render: (_args, value) => [{ type: 'text', text: value }]
|
|
140
|
+
},
|
|
141
|
+
isConcurrencySafe: () => false,
|
|
142
|
+
async execute(args, exec) {
|
|
143
|
+
exec?.signal?.throwIfAborted();
|
|
144
|
+
const runner = pool.getNodeRunner(exec?.session);
|
|
145
|
+
const code = args?.code || '';
|
|
146
|
+
const reset = Boolean(args?.reset);
|
|
147
|
+
|
|
148
|
+
const response = await runner.execute(code, { reset, timeoutMs: config.timeoutMs });
|
|
149
|
+
return formatEvalOutput(response);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
pool,
|
|
156
|
+
config,
|
|
157
|
+
destroy() {
|
|
158
|
+
pool.destroyAll();
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import readline from 'node:readline';
|
|
3
|
+
import process from 'node:process';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Manages a single stateful worker process with stdio line-delimited JSON RPC,
|
|
7
|
+
* watchdog timeout self-healing, and output anti-dumping guard.
|
|
8
|
+
*/
|
|
9
|
+
export class StdioWorkerRunner {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {string} options.command - e.g. 'python' or 'node'
|
|
13
|
+
* @param {string[]} options.args - CLI arguments
|
|
14
|
+
* @param {number} [options.timeoutMs=15000] - Hard execution timeout before killing
|
|
15
|
+
* @param {number} [options.maxOutputBytes=8192] - Max stdout/stderr allowed before truncation
|
|
16
|
+
* @param {object} [options.env={}] - Additional environment variables
|
|
17
|
+
*/
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.command = options.command;
|
|
20
|
+
this.args = options.args || [];
|
|
21
|
+
this.timeoutMs = options.timeoutMs ?? 15000;
|
|
22
|
+
this.maxOutputBytes = options.maxOutputBytes ?? 8192;
|
|
23
|
+
this.env = options.env || {};
|
|
24
|
+
|
|
25
|
+
this.child = null;
|
|
26
|
+
this.rl = null;
|
|
27
|
+
this.isReady = false;
|
|
28
|
+
this.isDestroyed = false;
|
|
29
|
+
this.currentTask = null;
|
|
30
|
+
this.pendingQueue = [];
|
|
31
|
+
this.readyWaiters = [];
|
|
32
|
+
this.launchCount = 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
ensureStarted() {
|
|
36
|
+
if (this.child && !this.child.killed && this.isReady) {
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
if (this.child && !this.child.killed && !this.isReady) {
|
|
40
|
+
return new Promise(resolve => this.readyWaiters.push(resolve));
|
|
41
|
+
}
|
|
42
|
+
return this._spawnProcess();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_spawnProcess() {
|
|
46
|
+
this.launchCount++;
|
|
47
|
+
this.isReady = false;
|
|
48
|
+
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
this.readyWaiters.push(resolve);
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const mergedEnv = {
|
|
54
|
+
...process.env,
|
|
55
|
+
PYTHONUNBUFFERED: '1',
|
|
56
|
+
PYTHONIOENCODING: 'utf-8',
|
|
57
|
+
...this.env
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
this.child = spawn(this.command, this.args, {
|
|
61
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
62
|
+
env: mergedEnv,
|
|
63
|
+
windowsHide: true
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
this.rl = readline.createInterface({
|
|
67
|
+
input: this.child.stdout,
|
|
68
|
+
terminal: false
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
this.rl.on('line', line => this._handleStdoutLine(line));
|
|
72
|
+
|
|
73
|
+
this.child.stderr.on('data', chunk => {
|
|
74
|
+
// Worker-level unhandled stderr logging (not during task execution)
|
|
75
|
+
if (this.currentTask) {
|
|
76
|
+
this.currentTask.stderrChunk = (this.currentTask.stderrChunk || '') + chunk.toString('utf-8');
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
this.child.on('error', err => {
|
|
81
|
+
this._handleProcessFailure(err);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
this.child.on('exit', (code, signal) => {
|
|
85
|
+
this._handleProcessExit(code, signal);
|
|
86
|
+
});
|
|
87
|
+
} catch (err) {
|
|
88
|
+
reject(err);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_handleStdoutLine(line) {
|
|
94
|
+
const trimmed = line.trim();
|
|
95
|
+
if (!trimmed) return;
|
|
96
|
+
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(trimmed);
|
|
100
|
+
} catch {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (parsed.status === 'ready' && !this.isReady) {
|
|
105
|
+
this.isReady = true;
|
|
106
|
+
const waiters = this.readyWaiters.splice(0, this.readyWaiters.length);
|
|
107
|
+
for (const w of waiters) w();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (this.currentTask && parsed.id === this.currentTask.id) {
|
|
112
|
+
const task = this.currentTask;
|
|
113
|
+
this.currentTask = null;
|
|
114
|
+
if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
|
|
115
|
+
|
|
116
|
+
this._truncateAndResolve(task, parsed);
|
|
117
|
+
this._drainQueue();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_truncateAndResolve(task, response) {
|
|
122
|
+
let stdout = response.stdout || '';
|
|
123
|
+
let stderr = (response.stderr || '') + (task.stderrChunk || '');
|
|
124
|
+
|
|
125
|
+
const limit = this.maxOutputBytes;
|
|
126
|
+
if (stdout.length > limit) {
|
|
127
|
+
stdout = stdout.slice(0, limit) + `\n\n[Output truncated: stdout exceeded ${limit} bytes. If full payload is required, write it to a file directly.]`;
|
|
128
|
+
}
|
|
129
|
+
if (stderr.length > limit) {
|
|
130
|
+
stderr = stderr.slice(0, limit) + `\n\n[Output truncated: stderr exceeded ${limit} bytes.]`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
response.stdout = stdout;
|
|
134
|
+
response.stderr = stderr;
|
|
135
|
+
task.resolve(response);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_handleProcessFailure(err) {
|
|
139
|
+
if (this.currentTask) {
|
|
140
|
+
const task = this.currentTask;
|
|
141
|
+
this.currentTask = null;
|
|
142
|
+
if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
|
|
143
|
+
task.reject(err);
|
|
144
|
+
}
|
|
145
|
+
this._drainQueue();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
_handleProcessExit(code, signal) {
|
|
149
|
+
this.isReady = false;
|
|
150
|
+
this.child = null;
|
|
151
|
+
this.rl = null;
|
|
152
|
+
|
|
153
|
+
if (this.currentTask) {
|
|
154
|
+
const task = this.currentTask;
|
|
155
|
+
this.currentTask = null;
|
|
156
|
+
if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
|
|
157
|
+
|
|
158
|
+
task.resolve({
|
|
159
|
+
id: task.id,
|
|
160
|
+
success: false,
|
|
161
|
+
error: task.killedByWatchdog
|
|
162
|
+
? `Execution timed out after ${this.timeoutMs}ms. Worker process was terminated and will be restarted cleanly.`
|
|
163
|
+
: `Worker process exited unexpectedly (code: ${code}, signal: ${signal}).`,
|
|
164
|
+
stdout: '',
|
|
165
|
+
stderr: task.stderrChunk || '',
|
|
166
|
+
executionTimeMs: this.timeoutMs
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this._drainQueue();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async execute(code, options = {}) {
|
|
174
|
+
if (this.isDestroyed) {
|
|
175
|
+
throw new Error('Worker runner has been destroyed.');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
await this.ensureStarted();
|
|
179
|
+
|
|
180
|
+
const id = `req-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
181
|
+
const payload = {
|
|
182
|
+
id,
|
|
183
|
+
action: options.action || 'eval',
|
|
184
|
+
code: code || '',
|
|
185
|
+
reset: Boolean(options.reset)
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
return new Promise((resolve, reject) => {
|
|
189
|
+
this.pendingQueue.push({ id, payload, resolve, reject, options });
|
|
190
|
+
if (!this.currentTask) {
|
|
191
|
+
this._drainQueue();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async _drainQueue() {
|
|
197
|
+
if (this.currentTask || this.pendingQueue.length === 0 || this.isDestroyed) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Ensure child process is healthy and ready before pulling tasks from queue
|
|
202
|
+
if (!this.child || !this.isReady) {
|
|
203
|
+
try {
|
|
204
|
+
await this.ensureStarted();
|
|
205
|
+
} catch (err) {
|
|
206
|
+
const task = this.pendingQueue.shift();
|
|
207
|
+
if (task) {
|
|
208
|
+
task.reject(err);
|
|
209
|
+
}
|
|
210
|
+
this._drainQueue();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (this.currentTask || this.pendingQueue.length === 0 || this.isDestroyed || !this.child) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const task = this.pendingQueue.shift();
|
|
220
|
+
this.currentTask = task;
|
|
221
|
+
|
|
222
|
+
// Armed watchdog timer
|
|
223
|
+
const effectiveTimeout = task.options?.timeoutMs ?? this.timeoutMs;
|
|
224
|
+
task.timeoutTimer = setTimeout(() => {
|
|
225
|
+
task.killedByWatchdog = true;
|
|
226
|
+
this._forceKill();
|
|
227
|
+
}, effectiveTimeout);
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
const line = JSON.stringify(task.payload) + '\n';
|
|
231
|
+
this.child.stdin.write(line, 'utf-8');
|
|
232
|
+
} catch (err) {
|
|
233
|
+
if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
|
|
234
|
+
this.currentTask = null;
|
|
235
|
+
task.reject(err);
|
|
236
|
+
this._drainQueue();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
_forceKill() {
|
|
241
|
+
if (!this.child) return;
|
|
242
|
+
const pid = this.child.pid;
|
|
243
|
+
try {
|
|
244
|
+
if (process.platform === 'win32' && pid) {
|
|
245
|
+
// Force terminate process tree on Windows to prevent orphan python processes
|
|
246
|
+
spawn('taskkill', ['/pid', String(pid), '/T', '/F'], { windowsHide: true });
|
|
247
|
+
} else {
|
|
248
|
+
this.child.kill('SIGKILL');
|
|
249
|
+
}
|
|
250
|
+
} catch {
|
|
251
|
+
try {
|
|
252
|
+
this.child.kill('SIGKILL');
|
|
253
|
+
} catch {
|
|
254
|
+
// Ignored
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
destroy() {
|
|
260
|
+
this.isDestroyed = true;
|
|
261
|
+
if (this.currentTask && this.currentTask.timeoutTimer) {
|
|
262
|
+
clearTimeout(this.currentTask.timeoutTimer);
|
|
263
|
+
}
|
|
264
|
+
this._forceKill();
|
|
265
|
+
this.child = null;
|
|
266
|
+
this.rl = null;
|
|
267
|
+
this.pendingQueue = [];
|
|
268
|
+
this.currentTask = null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* node_worker.mjs - Zero-dependency persistent Node.js worker for DSH Agent.
|
|
4
|
+
* Maintains a stateful VM context across execution frames over stdio line-delimited JSON.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import readline from 'node:readline';
|
|
8
|
+
import vm from 'node:vm';
|
|
9
|
+
import util from 'node:util';
|
|
10
|
+
import process from 'node:process';
|
|
11
|
+
|
|
12
|
+
function createFreshContext() {
|
|
13
|
+
const sandbox = {
|
|
14
|
+
setTimeout,
|
|
15
|
+
clearTimeout,
|
|
16
|
+
setInterval,
|
|
17
|
+
clearInterval,
|
|
18
|
+
setImmediate,
|
|
19
|
+
clearImmediate,
|
|
20
|
+
Buffer,
|
|
21
|
+
URL,
|
|
22
|
+
URLSearchParams,
|
|
23
|
+
TextEncoder,
|
|
24
|
+
TextDecoder,
|
|
25
|
+
process: {
|
|
26
|
+
env: { ...process.env },
|
|
27
|
+
cwd: () => process.cwd(),
|
|
28
|
+
version: process.version,
|
|
29
|
+
platform: process.platform,
|
|
30
|
+
arch: process.arch
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const context = vm.createContext(sandbox);
|
|
35
|
+
context.global = context;
|
|
36
|
+
context.globalThis = context;
|
|
37
|
+
return context;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let CURRENT_CONTEXT = createFreshContext();
|
|
41
|
+
|
|
42
|
+
function serializeResult(val) {
|
|
43
|
+
if (val === undefined) return { result: null, resultRepr: 'undefined' };
|
|
44
|
+
if (val === null) return { result: null, resultRepr: 'null' };
|
|
45
|
+
|
|
46
|
+
let repr;
|
|
47
|
+
try {
|
|
48
|
+
repr = util.inspect(val, { depth: 4, maxArrayLength: 100, breakLength: 80 });
|
|
49
|
+
} catch {
|
|
50
|
+
repr = String(val);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let serializable;
|
|
54
|
+
try {
|
|
55
|
+
serializable = JSON.parse(JSON.stringify(val));
|
|
56
|
+
} catch {
|
|
57
|
+
serializable = repr;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { result: serializable, resultRepr: repr };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function wrapAsyncCode(code) {
|
|
64
|
+
const trimmed = code.trim();
|
|
65
|
+
if (/\breturn\s+/.test(trimmed)) {
|
|
66
|
+
return `(async () => {\n${trimmed}\n})()`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const lines = trimmed.split('\n');
|
|
70
|
+
let lastIndex = -1;
|
|
71
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
72
|
+
if (lines[i].trim().length > 0) {
|
|
73
|
+
lastIndex = i;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (lastIndex === -1) {
|
|
79
|
+
return `(async () => {})()`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const lastLine = lines[lastIndex].trim();
|
|
83
|
+
const statementKeywords = /^(const|let|var|function|class|import|export|throw|return|if|else|while|for|switch|case|default|try|catch|finally)\b/;
|
|
84
|
+
|
|
85
|
+
if (!statementKeywords.test(lastLine)) {
|
|
86
|
+
const expr = lastLine.endsWith(';') ? lastLine.slice(0, -1) : lastLine;
|
|
87
|
+
lines[lastIndex] = `return (${expr});`;
|
|
88
|
+
return `(async () => {\n${lines.join('\n')}\n})()`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return `(async () => {\n${trimmed}\n})()`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function executeInContext(code, context) {
|
|
95
|
+
const trimmed = code.trim();
|
|
96
|
+
if (!trimmed) return undefined;
|
|
97
|
+
|
|
98
|
+
// If code appears to use top-level await, wrap in async IIFE
|
|
99
|
+
const hasTopLevelAwait = /\bawait\s+/.test(trimmed);
|
|
100
|
+
|
|
101
|
+
if (hasTopLevelAwait) {
|
|
102
|
+
const wrappedCode = wrapAsyncCode(trimmed);
|
|
103
|
+
const script = new vm.Script(wrappedCode, { filename: '<eval_kernel>' });
|
|
104
|
+
const promise = script.runInContext(context, { breakOnSigint: true });
|
|
105
|
+
return await promise;
|
|
106
|
+
} else {
|
|
107
|
+
// Standard execution: natively returns the value of the last evaluated expression
|
|
108
|
+
const script = new vm.Script(trimmed, { filename: '<eval_kernel>' });
|
|
109
|
+
return script.runInContext(context, { breakOnSigint: true });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function sendFrame(payload) {
|
|
114
|
+
let line;
|
|
115
|
+
try {
|
|
116
|
+
line = JSON.stringify(payload) + '\n';
|
|
117
|
+
} catch (err) {
|
|
118
|
+
line = JSON.stringify({
|
|
119
|
+
id: payload.id,
|
|
120
|
+
success: false,
|
|
121
|
+
error: `Failed to serialize response frame: ${err.message}`,
|
|
122
|
+
stdout: payload.stdout || '',
|
|
123
|
+
stderr: payload.stderr || '',
|
|
124
|
+
executionTimeMs: payload.executionTimeMs || 0
|
|
125
|
+
}) + '\n';
|
|
126
|
+
}
|
|
127
|
+
process.stdout.write(line, 'utf-8');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function main() {
|
|
131
|
+
// Send ready frame on startup
|
|
132
|
+
sendFrame({ status: 'ready', pid: process.pid });
|
|
133
|
+
|
|
134
|
+
const rl = readline.createInterface({
|
|
135
|
+
input: process.stdin,
|
|
136
|
+
output: process.stdout,
|
|
137
|
+
terminal: false
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
rl.on('line', async rawLine => {
|
|
141
|
+
const line = rawLine.trim();
|
|
142
|
+
if (!line) return;
|
|
143
|
+
|
|
144
|
+
let req;
|
|
145
|
+
try {
|
|
146
|
+
req = JSON.parse(line);
|
|
147
|
+
} catch (e) {
|
|
148
|
+
sendFrame({
|
|
149
|
+
id: null,
|
|
150
|
+
success: false,
|
|
151
|
+
error: `Invalid JSON payload: ${e.message}`,
|
|
152
|
+
stdout: '',
|
|
153
|
+
stderr: '',
|
|
154
|
+
executionTimeMs: 0
|
|
155
|
+
});
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const reqId = req.id;
|
|
160
|
+
const action = req.action || 'eval';
|
|
161
|
+
const code = req.code || '';
|
|
162
|
+
const shouldReset = Boolean(req.reset);
|
|
163
|
+
|
|
164
|
+
if (action === 'ping') {
|
|
165
|
+
sendFrame({
|
|
166
|
+
id: reqId,
|
|
167
|
+
success: true,
|
|
168
|
+
status: 'pong',
|
|
169
|
+
pid: process.pid
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (action === 'reset' || shouldReset) {
|
|
175
|
+
CURRENT_CONTEXT = createFreshContext();
|
|
176
|
+
if (action === 'reset') {
|
|
177
|
+
sendFrame({
|
|
178
|
+
id: reqId,
|
|
179
|
+
success: true,
|
|
180
|
+
stdout: '',
|
|
181
|
+
stderr: '',
|
|
182
|
+
result: 'Node VM context reset successfully.',
|
|
183
|
+
executionTimeMs: 0
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
let stdoutBuf = '';
|
|
190
|
+
let stderrBuf = '';
|
|
191
|
+
|
|
192
|
+
// Intercept console inside context
|
|
193
|
+
const customConsole = {
|
|
194
|
+
log: (...args) => {
|
|
195
|
+
stdoutBuf += util.format(...args) + '\n';
|
|
196
|
+
},
|
|
197
|
+
info: (...args) => {
|
|
198
|
+
stdoutBuf += util.format(...args) + '\n';
|
|
199
|
+
},
|
|
200
|
+
warn: (...args) => {
|
|
201
|
+
stderrBuf += util.format(...args) + '\n';
|
|
202
|
+
},
|
|
203
|
+
error: (...args) => {
|
|
204
|
+
stderrBuf += util.format(...args) + '\n';
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
CURRENT_CONTEXT.console = customConsole;
|
|
208
|
+
|
|
209
|
+
const startTime = performance.now();
|
|
210
|
+
let success = true;
|
|
211
|
+
let errorMsg = null;
|
|
212
|
+
let rawResult;
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
rawResult = await executeInContext(code, CURRENT_CONTEXT);
|
|
216
|
+
} catch (err) {
|
|
217
|
+
success = false;
|
|
218
|
+
errorMsg = err instanceof Error ? err.stack || err.message : String(err);
|
|
219
|
+
stderrBuf += errorMsg + '\n';
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const elapsedMs = Number((performance.now() - startTime).toFixed(3));
|
|
223
|
+
const { result, resultRepr } = serializeResult(rawResult);
|
|
224
|
+
|
|
225
|
+
sendFrame({
|
|
226
|
+
id: reqId,
|
|
227
|
+
success,
|
|
228
|
+
stdout: stdoutBuf,
|
|
229
|
+
stderr: stderrBuf,
|
|
230
|
+
result: success ? result : null,
|
|
231
|
+
resultRepr: success ? resultRepr : null,
|
|
232
|
+
error: success ? null : errorMsg,
|
|
233
|
+
executionTimeMs: elapsedMs
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
main();
|