dsh-awesome-optimizer 1.7.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 +23 -22
- package/README.zh.md +23 -22
- package/package.json +9 -20
- package/scripts/benchmark.mjs +53 -2
- package/src/convergence.js +2 -2
- package/src/custom-prompt.js +4 -2
- 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
- package/src/runtime-policy.js +19 -15
|
@@ -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();
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
py_worker.py - Zero-dependency persistent Python worker for DSH Agent.
|
|
5
|
+
Maintains a stateful global scope across execution frames over stdio line-delimited JSON.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
import io
|
|
10
|
+
import os
|
|
11
|
+
import ast
|
|
12
|
+
import json
|
|
13
|
+
import time
|
|
14
|
+
import traceback
|
|
15
|
+
|
|
16
|
+
# Ensure strict UTF-8 stdio streams with line buffering
|
|
17
|
+
if hasattr(sys.stdin, "reconfigure"):
|
|
18
|
+
sys.stdin.reconfigure(encoding="utf-8", errors="replace")
|
|
19
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
20
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
21
|
+
if hasattr(sys.stderr, "reconfigure"):
|
|
22
|
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
23
|
+
|
|
24
|
+
# Keep handles to original standard streams for JSON protocol frames
|
|
25
|
+
REAL_STDIN = sys.__stdin__
|
|
26
|
+
REAL_STDOUT = sys.__stdout__
|
|
27
|
+
|
|
28
|
+
def create_fresh_scope():
|
|
29
|
+
return {
|
|
30
|
+
"__name__": "__main__",
|
|
31
|
+
"__doc__": "DSH Persistent Python Kernel",
|
|
32
|
+
"__builtins__": __builtins__,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
GLOBAL_SCOPE = create_fresh_scope()
|
|
36
|
+
|
|
37
|
+
def execute_code(code, scope):
|
|
38
|
+
"""
|
|
39
|
+
Execute python code.
|
|
40
|
+
If the last AST node is an expression, evaluate it to capture its return value.
|
|
41
|
+
Otherwise execute as statements.
|
|
42
|
+
"""
|
|
43
|
+
code_stripped = code.strip()
|
|
44
|
+
if not code_stripped:
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
parsed = ast.parse(code, filename="<eval_kernel>", mode="exec")
|
|
48
|
+
if not parsed.body:
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
# Check if the last statement is an expression
|
|
52
|
+
last_stmt = parsed.body[-1]
|
|
53
|
+
if isinstance(last_stmt, ast.Expr):
|
|
54
|
+
# If there are preceding statements, execute them first
|
|
55
|
+
if len(parsed.body) > 1:
|
|
56
|
+
exec_mod = ast.Module(body=parsed.body[:-1], type_ignores=[])
|
|
57
|
+
exec(compile(exec_mod, filename="<eval_kernel>", mode="exec"), scope)
|
|
58
|
+
|
|
59
|
+
# Evaluate the trailing expression
|
|
60
|
+
eval_expr = ast.Expression(body=last_stmt.value)
|
|
61
|
+
return eval(compile(eval_expr, filename="<eval_kernel>", mode="eval"), scope)
|
|
62
|
+
else:
|
|
63
|
+
# All statements, execute directly
|
|
64
|
+
exec(compile(parsed, filename="<eval_kernel>", mode="exec"), scope)
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
def serialize_result(result):
|
|
68
|
+
if result is None:
|
|
69
|
+
return None, None
|
|
70
|
+
result_repr = repr(result)
|
|
71
|
+
try:
|
|
72
|
+
# Check if directly JSON-serializable
|
|
73
|
+
json.dumps(result)
|
|
74
|
+
return result, result_repr
|
|
75
|
+
except (TypeError, OverflowError):
|
|
76
|
+
# Fall back to string representation
|
|
77
|
+
return result_repr, result_repr
|
|
78
|
+
|
|
79
|
+
def main():
|
|
80
|
+
global GLOBAL_SCOPE
|
|
81
|
+
|
|
82
|
+
# Signal ready frame on startup
|
|
83
|
+
ready_frame = json.dumps({"status": "ready", "pid": os.getpid()}, ensure_ascii=False) + "\n"
|
|
84
|
+
REAL_STDOUT.buffer.write(ready_frame.encode("utf-8"))
|
|
85
|
+
REAL_STDOUT.buffer.flush()
|
|
86
|
+
|
|
87
|
+
for line in REAL_STDIN:
|
|
88
|
+
line_str = line.strip()
|
|
89
|
+
if not line_str:
|
|
90
|
+
continue
|
|
91
|
+
|
|
92
|
+
try:
|
|
93
|
+
req = json.loads(line_str)
|
|
94
|
+
except Exception as e:
|
|
95
|
+
err_frame = json.dumps({
|
|
96
|
+
"id": None,
|
|
97
|
+
"success": False,
|
|
98
|
+
"error": f"Invalid JSON payload: {str(e)}",
|
|
99
|
+
"stdout": "",
|
|
100
|
+
"stderr": "",
|
|
101
|
+
"executionTimeMs": 0
|
|
102
|
+
}, ensure_ascii=False) + "\n"
|
|
103
|
+
REAL_STDOUT.buffer.write(err_frame.encode("utf-8"))
|
|
104
|
+
REAL_STDOUT.buffer.flush()
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
req_id = req.get("id")
|
|
108
|
+
action = req.get("action", "eval")
|
|
109
|
+
code = req.get("code", "")
|
|
110
|
+
should_reset = bool(req.get("reset", False))
|
|
111
|
+
|
|
112
|
+
if action == "ping":
|
|
113
|
+
resp = {
|
|
114
|
+
"id": req_id,
|
|
115
|
+
"success": True,
|
|
116
|
+
"status": "pong",
|
|
117
|
+
"pid": os.getpid()
|
|
118
|
+
}
|
|
119
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
120
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
121
|
+
REAL_STDOUT.buffer.flush()
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
if action == "reset" or should_reset:
|
|
125
|
+
GLOBAL_SCOPE = create_fresh_scope()
|
|
126
|
+
if action == "reset":
|
|
127
|
+
resp = {
|
|
128
|
+
"id": req_id,
|
|
129
|
+
"success": True,
|
|
130
|
+
"stdout": "",
|
|
131
|
+
"stderr": "",
|
|
132
|
+
"result": "Kernel namespace reset successfully.",
|
|
133
|
+
"executionTimeMs": 0
|
|
134
|
+
}
|
|
135
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
136
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
137
|
+
REAL_STDOUT.buffer.flush()
|
|
138
|
+
continue
|
|
139
|
+
|
|
140
|
+
captured_stdout = io.StringIO()
|
|
141
|
+
captured_stderr = io.StringIO()
|
|
142
|
+
old_stdout = sys.stdout
|
|
143
|
+
old_stderr = sys.stderr
|
|
144
|
+
sys.stdout = captured_stdout
|
|
145
|
+
sys.stderr = captured_stderr
|
|
146
|
+
|
|
147
|
+
start_time = time.perf_counter()
|
|
148
|
+
success = True
|
|
149
|
+
error_msg = None
|
|
150
|
+
result_val = None
|
|
151
|
+
result_repr = None
|
|
152
|
+
|
|
153
|
+
try:
|
|
154
|
+
raw_result = execute_code(code, GLOBAL_SCOPE)
|
|
155
|
+
result_val, result_repr = serialize_result(raw_result)
|
|
156
|
+
except Exception as ex:
|
|
157
|
+
success = False
|
|
158
|
+
error_msg = str(ex)
|
|
159
|
+
# Write traceback into captured_stderr
|
|
160
|
+
traceback.print_exc(file=captured_stderr)
|
|
161
|
+
finally:
|
|
162
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 3)
|
|
163
|
+
sys.stdout = old_stdout
|
|
164
|
+
sys.stderr = old_stderr
|
|
165
|
+
|
|
166
|
+
resp = {
|
|
167
|
+
"id": req_id,
|
|
168
|
+
"success": success,
|
|
169
|
+
"stdout": captured_stdout.getvalue(),
|
|
170
|
+
"stderr": captured_stderr.getvalue(),
|
|
171
|
+
"result": result_val,
|
|
172
|
+
"resultRepr": result_repr,
|
|
173
|
+
"error": error_msg,
|
|
174
|
+
"executionTimeMs": elapsed_ms
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
178
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
179
|
+
REAL_STDOUT.buffer.flush()
|
|
180
|
+
|
|
181
|
+
if __name__ == "__main__":
|
|
182
|
+
main()
|
package/src/runtime-policy.js
CHANGED
|
@@ -371,23 +371,27 @@ export class RuntimePolicyManager {
|
|
|
371
371
|
};
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
// Guard against dangerous
|
|
374
|
+
// Guard against dangerous heavy concurrent or publishing command chaining on Windows
|
|
375
375
|
if (this.config.disallowChainedNetworkCommands) {
|
|
376
376
|
const hasCompoundOperator = /(&&|;)/.test(cmd);
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
if (
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
377
|
+
if (hasCompoundOperator) {
|
|
378
|
+
// Strip quoted string literals to prevent false positives in commit messages / echo args
|
|
379
|
+
const unquoted = cmd.replace(/"[^"\\]*(?:\\.[^"\\]*)*"/g, '""').replace(/'[^'\\]*(?:\\.[^'\\]*)*'/g, "''");
|
|
380
|
+
const hasBlockedCompoundAction = /(?:^|[\s;&|])(?:npm\s+(?:test|t|run\s+build|publish)|vitest)\b/i.test(unquoted);
|
|
381
|
+
if (hasBlockedCompoundAction) {
|
|
382
|
+
state.shellPayloadBlocks++;
|
|
383
|
+
state.policyDenials++;
|
|
384
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
385
|
+
return {
|
|
386
|
+
kind: 'deny',
|
|
387
|
+
reason: JSON.stringify({
|
|
388
|
+
code: 'CHAINED_HEAVY_COMMAND',
|
|
389
|
+
reason: "Heavy tasks ('npm test', 'npm run build', 'vitest', 'npm publish') must run standalone to prevent Windows deadlocks.",
|
|
390
|
+
nextAction: "Execute heavy tasks standalone, then chain lightweight commands via '&&'.",
|
|
391
|
+
retryable: false
|
|
392
|
+
})
|
|
393
|
+
};
|
|
394
|
+
}
|
|
391
395
|
}
|
|
392
396
|
}
|
|
393
397
|
}
|