dsh-awesome-optimizer 1.8.0 → 2.1.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.
@@ -0,0 +1,192 @@
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
+ const trimmed = resp.stdout.trimEnd();
27
+ if (trimmed.length > 0) {
28
+ parts.push(trimmed);
29
+ }
30
+ }
31
+
32
+ if (resp.result !== null && resp.result !== undefined) {
33
+ let repr = resp.resultRepr;
34
+ if (!repr) {
35
+ try {
36
+ repr = typeof resp.result === 'object' ? JSON.stringify(resp.result) : String(resp.result);
37
+ } catch {
38
+ repr = String(resp.result);
39
+ }
40
+ }
41
+ if (typeof resp.result === 'number' && !Number.isInteger(resp.result)) {
42
+ const rounded = Number(resp.result.toFixed(10));
43
+ if (Math.abs(resp.result - rounded) < 1e-12) {
44
+ repr = String(rounded);
45
+ }
46
+ }
47
+ parts.push(`=> ${repr}`);
48
+ }
49
+
50
+ if (!resp.success) {
51
+ if (resp.error) parts.push(`[Error: ${resp.error}]`);
52
+ if (resp.stderr && resp.stderr.length > 0) {
53
+ const trimmedErr = resp.stderr.trimEnd();
54
+ if (trimmedErr.length > 0) {
55
+ parts.push(trimmedErr);
56
+ }
57
+ }
58
+ }
59
+
60
+ if (parts.length === 0) {
61
+ return resp.success ? '<Executed successfully with no output>' : '<Execution failed with no output>';
62
+ }
63
+
64
+ return parts.join('\n');
65
+ }
66
+
67
+ /**
68
+ * Mount stateful persistent Python and Node evaluation tools around the DSH agent loop.
69
+ */
70
+ export function applyPersistentEval(ctx, rawConfig = {}) {
71
+ const config = PersistentEvalConfig(rawConfig);
72
+ if (!config.enabled) return null;
73
+
74
+ const pool = new DaemonPool({
75
+ timeoutMs: config.timeoutMs,
76
+ maxOutputBytes: config.maxOutputBytes
77
+ });
78
+
79
+ // Clean up session workers when session ends & prewarm on session/turn starts
80
+ ctx.on('session/event', (session, event) => {
81
+ if (event?.type === 'session/destroy' || event?.type === 'session/close') {
82
+ pool.destroySession(session);
83
+ } else if (event?.type === 'turn/start' || event?.type === 'session/start' || event?.type === 'session/create') {
84
+ pool.prewarm(session, { python: config.python, node: config.node });
85
+ }
86
+ });
87
+
88
+ // Pre-warm on turn/start event so worker processes are already hot during model inference
89
+ ctx.on('turn/start', (turn) => {
90
+ const session = turn?.session;
91
+ pool.prewarm(session, { python: config.python, node: config.node });
92
+ });
93
+
94
+ // Clean up all daemon workers when plugin context is disposed
95
+ if (typeof ctx.effect === 'function') {
96
+ ctx.effect(() => () => {
97
+ pool.destroyAll();
98
+ });
99
+ }
100
+
101
+ if (config.python) {
102
+ ctx.tools.register({
103
+ name: EVAL_PYTHON_TOOL,
104
+ description: [
105
+ 'Execute Python code inside a persistent stateful kernel.',
106
+ 'Variables, functions, imports, and state persist across calls within the session.',
107
+ 'Standard modules (math, re, json, time, collections, itertools) are eagerly pre-imported in scope.',
108
+ 'Use for exploratory calculations, data processing, kinematic/algorithmic solving, and intermediate analysis without writing temp files to disk.'
109
+ ].join(' '),
110
+ parameters: {
111
+ type: 'object',
112
+ additionalProperties: false,
113
+ required: ['code'],
114
+ properties: {
115
+ code: {
116
+ type: 'string',
117
+ description: 'Python code to execute or expression to evaluate.'
118
+ },
119
+ reset: {
120
+ type: 'boolean',
121
+ description: 'Reset kernel state before execution. Defaults to false.'
122
+ }
123
+ }
124
+ },
125
+ output: {
126
+ schema: { type: 'string' },
127
+ render: (_args, value) => [{ type: 'text', text: value }]
128
+ },
129
+ isConcurrencySafe: () => false,
130
+ async execute(args, exec) {
131
+ exec?.signal?.throwIfAborted();
132
+ const runner = pool.getPythonRunner(exec?.session);
133
+ const code = args?.code || '';
134
+ const reset = Boolean(args?.reset);
135
+
136
+ const response = await runner.execute(code, { reset, timeoutMs: config.timeoutMs });
137
+ return formatEvalOutput(response);
138
+ }
139
+ });
140
+ }
141
+
142
+ if (config.node) {
143
+ ctx.tools.register({
144
+ name: EVAL_NODE_TOOL,
145
+ description: [
146
+ 'Execute JavaScript code inside a persistent stateful Node.js VM context.',
147
+ 'Variables, functions, and state persist across calls within the session. Supports top-level await.',
148
+ 'Use for rapid expression evaluation, AST exploration, and data transformations without writing temp files to disk.'
149
+ ].join(' '),
150
+ parameters: {
151
+ type: 'object',
152
+ additionalProperties: false,
153
+ required: ['code'],
154
+ properties: {
155
+ code: {
156
+ type: 'string',
157
+ description: 'JavaScript code to execute. Can be expressions, statements, or async code.'
158
+ },
159
+ reset: {
160
+ type: 'boolean',
161
+ description: 'Reset VM context before execution. Defaults to false.'
162
+ }
163
+ }
164
+ },
165
+ output: {
166
+ schema: { type: 'string' },
167
+ render: (_args, value) => [{ type: 'text', text: value }]
168
+ },
169
+ isConcurrencySafe: () => false,
170
+ async execute(args, exec) {
171
+ exec?.signal?.throwIfAborted();
172
+ const runner = pool.getNodeRunner(exec?.session);
173
+ const code = args?.code || '';
174
+ const reset = Boolean(args?.reset);
175
+
176
+ const response = await runner.execute(code, { reset, timeoutMs: config.timeoutMs });
177
+ return formatEvalOutput(response);
178
+ }
179
+ });
180
+ }
181
+
182
+ return {
183
+ pool,
184
+ config,
185
+ prewarm(session) {
186
+ return pool.prewarm(session, { python: config.python, node: config.node });
187
+ },
188
+ destroy() {
189
+ pool.destroyAll();
190
+ }
191
+ };
192
+ }
@@ -0,0 +1,304 @@
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
+ _ref() {
36
+ try {
37
+ if (this.child?.ref) this.child.ref();
38
+ if (this.child?.stdin?.ref) this.child.stdin.ref();
39
+ if (this.child?.stdout?.ref) this.child.stdout.ref();
40
+ if (this.child?.stderr?.ref) this.child.stderr.ref();
41
+ } catch {}
42
+ }
43
+
44
+ _unref() {
45
+ try {
46
+ if (this.child?.unref) this.child.unref();
47
+ if (this.child?.stdin?.unref) this.child.stdin.unref();
48
+ if (this.child?.stdout?.unref) this.child.stdout.unref();
49
+ if (this.child?.stderr?.unref) this.child.stderr.unref();
50
+ } catch {}
51
+ }
52
+
53
+ ensureStarted() {
54
+ if (this.child && !this.child.killed && this.isReady) {
55
+ return Promise.resolve();
56
+ }
57
+ if (this.child && !this.child.killed && !this.isReady) {
58
+ return new Promise(resolve => this.readyWaiters.push(resolve));
59
+ }
60
+ return this._spawnProcess();
61
+ }
62
+
63
+ _spawnProcess() {
64
+ this.launchCount++;
65
+ this.isReady = false;
66
+
67
+ return new Promise((resolve, reject) => {
68
+ this.readyWaiters.push(resolve);
69
+
70
+ try {
71
+ const mergedEnv = {
72
+ ...process.env,
73
+ PYTHONUNBUFFERED: '1',
74
+ PYTHONIOENCODING: 'utf-8',
75
+ ...this.env
76
+ };
77
+
78
+ this.child = spawn(this.command, this.args, {
79
+ stdio: ['pipe', 'pipe', 'pipe'],
80
+ env: mergedEnv,
81
+ windowsHide: true
82
+ });
83
+
84
+ this._ref();
85
+
86
+ this.rl = readline.createInterface({
87
+ input: this.child.stdout,
88
+ terminal: false
89
+ });
90
+
91
+ this.rl.on('line', line => this._handleStdoutLine(line));
92
+
93
+ this.child.stderr.on('data', chunk => {
94
+ // Worker-level unhandled stderr logging (not during task execution)
95
+ if (this.currentTask) {
96
+ this.currentTask.stderrChunk = (this.currentTask.stderrChunk || '') + chunk.toString('utf-8');
97
+ }
98
+ });
99
+
100
+ this.child.on('error', err => {
101
+ this._handleProcessFailure(err);
102
+ });
103
+
104
+ this.child.on('exit', (code, signal) => {
105
+ this._handleProcessExit(code, signal);
106
+ });
107
+ } catch (err) {
108
+ reject(err);
109
+ }
110
+ });
111
+ }
112
+
113
+ _handleStdoutLine(line) {
114
+ const trimmed = line.trim();
115
+ if (!trimmed) return;
116
+
117
+ let parsed;
118
+ try {
119
+ parsed = JSON.parse(trimmed);
120
+ } catch {
121
+ return;
122
+ }
123
+
124
+ if (parsed.status === 'ready' && !this.isReady) {
125
+ this.isReady = true;
126
+ const waiters = this.readyWaiters.splice(0, this.readyWaiters.length);
127
+ for (const w of waiters) w();
128
+ if (!this.currentTask && this.pendingQueue.length === 0) {
129
+ this._unref();
130
+ }
131
+ return;
132
+ }
133
+
134
+ if (this.currentTask && parsed.id === this.currentTask.id) {
135
+ const task = this.currentTask;
136
+ this.currentTask = null;
137
+ if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
138
+
139
+ this._truncateAndResolve(task, parsed);
140
+ this._drainQueue();
141
+ }
142
+ }
143
+
144
+ _truncateAndResolve(task, response) {
145
+ let stdout = response.stdout || '';
146
+ let stderr = (response.stderr || '') + (task.stderrChunk || '');
147
+
148
+ const limit = this.maxOutputBytes;
149
+ if (stdout.length > limit) {
150
+ stdout = stdout.slice(0, limit) + `\n\n[Output truncated: stdout exceeded ${limit} bytes. If full payload is required, write it to a file directly.]`;
151
+ }
152
+ if (stderr.length > limit) {
153
+ stderr = stderr.slice(0, limit) + `\n\n[Output truncated: stderr exceeded ${limit} bytes.]`;
154
+ }
155
+
156
+ response.stdout = stdout;
157
+ response.stderr = stderr;
158
+ task.resolve(response);
159
+ }
160
+
161
+ _handleProcessFailure(err) {
162
+ if (this.currentTask) {
163
+ const task = this.currentTask;
164
+ this.currentTask = null;
165
+ if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
166
+ task.reject(err);
167
+ }
168
+ this._drainQueue();
169
+ }
170
+
171
+ _handleProcessExit(code, signal) {
172
+ this.isReady = false;
173
+ this.child = null;
174
+ this.rl = null;
175
+
176
+ if (this.currentTask) {
177
+ const task = this.currentTask;
178
+ this.currentTask = null;
179
+ if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
180
+
181
+ task.resolve({
182
+ id: task.id,
183
+ success: false,
184
+ error: task.killedByWatchdog
185
+ ? `Execution timed out after ${this.timeoutMs}ms. Worker process was terminated and will be restarted cleanly.`
186
+ : `Worker process exited unexpectedly (code: ${code}, signal: ${signal}).`,
187
+ stdout: '',
188
+ stderr: task.stderrChunk || '',
189
+ executionTimeMs: this.timeoutMs
190
+ });
191
+ }
192
+
193
+ this._drainQueue();
194
+ }
195
+
196
+ async execute(code, options = {}) {
197
+ if (this.isDestroyed) {
198
+ throw new Error('Worker runner has been destroyed.');
199
+ }
200
+
201
+ await this.ensureStarted();
202
+
203
+ const id = `req-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
204
+ const payload = {
205
+ id,
206
+ action: options.action || 'eval',
207
+ code: code || '',
208
+ reset: Boolean(options.reset)
209
+ };
210
+
211
+ return new Promise((resolve, reject) => {
212
+ this.pendingQueue.push({ id, payload, resolve, reject, options });
213
+ if (!this.currentTask) {
214
+ this._drainQueue();
215
+ }
216
+ });
217
+ }
218
+
219
+ async _drainQueue() {
220
+ if (this.currentTask || this.isDestroyed) {
221
+ return;
222
+ }
223
+
224
+ if (this.pendingQueue.length === 0) {
225
+ this._unref();
226
+ return;
227
+ }
228
+
229
+ this._ref();
230
+
231
+ // Ensure child process is healthy and ready before pulling tasks from queue
232
+ if (!this.child || !this.isReady) {
233
+ try {
234
+ await this.ensureStarted();
235
+ } catch (err) {
236
+ const task = this.pendingQueue.shift();
237
+ if (task) {
238
+ task.reject(err);
239
+ }
240
+ this._drainQueue();
241
+ return;
242
+ }
243
+ }
244
+
245
+ if (this.currentTask || this.pendingQueue.length === 0 || this.isDestroyed || !this.child) {
246
+ if (this.pendingQueue.length === 0) this._unref();
247
+ return;
248
+ }
249
+
250
+ const task = this.pendingQueue.shift();
251
+ this.currentTask = task;
252
+
253
+ // Armed watchdog timer
254
+ const effectiveTimeout = task.options?.timeoutMs ?? this.timeoutMs;
255
+ task.timeoutTimer = setTimeout(() => {
256
+ task.killedByWatchdog = true;
257
+ this._forceKill();
258
+ }, effectiveTimeout);
259
+
260
+ try {
261
+ const line = JSON.stringify(task.payload) + '\n';
262
+ this.child.stdin.write(line, 'utf-8');
263
+ } catch (err) {
264
+ if (task.timeoutTimer) clearTimeout(task.timeoutTimer);
265
+ this.currentTask = null;
266
+ task.reject(err);
267
+ this._drainQueue();
268
+ }
269
+ }
270
+
271
+ _forceKill() {
272
+ if (!this.child) return;
273
+ const pid = this.child.pid;
274
+ try {
275
+ if (process.platform === 'win32' && pid) {
276
+ // Force terminate process tree on Windows to prevent orphan python processes
277
+ spawn('taskkill', ['/pid', String(pid), '/T', '/F'], { windowsHide: true });
278
+ } else {
279
+ this.child.kill('SIGKILL');
280
+ }
281
+ } catch {
282
+ try {
283
+ this.child.kill('SIGKILL');
284
+ } catch {
285
+ // Ignored
286
+ }
287
+ }
288
+ }
289
+
290
+ destroy() {
291
+ this.isDestroyed = true;
292
+ if (this.currentTask && this.currentTask.timeoutTimer) {
293
+ clearTimeout(this.currentTask.timeoutTimer);
294
+ }
295
+ if (this.rl) {
296
+ try { this.rl.close(); } catch {}
297
+ this.rl = null;
298
+ }
299
+ this._forceKill();
300
+ this.child = null;
301
+ this.pendingQueue = [];
302
+ this.currentTask = null;
303
+ }
304
+ }