dsh-awesome-optimizer 2.0.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.
package/README.md CHANGED
@@ -12,10 +12,27 @@ High-performance runtime policy guards, persistent Python & Node evaluation daem
12
12
 
13
13
  ---
14
14
 
15
+ ## Highlights in v2.1.0
16
+
17
+ - **Pre-warming Daemon**:
18
+ Hooks into `turn/start` lifecycle events to pre-spawn and handshake worker processes during model first-token inference, completely eliminating the 80ms cold-start spike down to **0.52ms (98.8% speedup)**.
19
+ - **Eager Scope Ingestion**:
20
+ Standard algorithm libraries (`math`, `re`, `json`, `time`, `collections`, `itertools`) are eagerly pre-imported into global Python scope, eliminating repetitive `import` boilerplate and saving token generation time.
21
+ - **Zero-Token-Waste Formatting**:
22
+ Ultra-compact single-line return format for pure expressions (`=> 42`), trimmed trailing newlines, and intelligent smoothing of IEEE-754 binary floating-point noise (`0.1 + 0.2` -> `=> 0.3`).
23
+ - **Dynamic Ref/Unref & Cordis Lifecycle Cascading**:
24
+ Implements dynamic stream reference counting (`_ref` / `_unref`) so idle daemons never hold the Node.js event loop hostage while ensuring reliable teardown via Cordis `ctx.effect`.
25
+
26
+ ---
27
+
15
28
  ## Highlights in v2.0.0
16
29
 
17
30
  - **Persistent Python & Node Evaluation Daemons (`eval_python` / `eval_node`)**:
18
- Zero-dependency stateful execution kernels over line-delimited stdio JSON frames. Retains global scopes, functions, imports, and variables across turns (with Node top-level `await`). Slashes repetitive calculation latency from 37ms to **0.65ms (98.3% speedup)**, eliminating temporary file pollution.
31
+ Zero-dependency stateful execution kernels over line-delimited stdio JSON frames. Retains global scopes, functions, imports, and variables across turns (with Node top-level `await`). Slashes repetitive calculation latency from 37ms to **0.61ms (98.5% speedup)**, eliminating temporary file pollution.
32
+ - **Three Extreme Performance Dimensions**:
33
+ 1. **Pre-warming Daemon**: Listens to session `turn/start` events to pre-spawn and handshake worker processes during model first-token generation, completely eliminating the 80ms cold-start spike.
34
+ 2. **Eager Scope Ingestion**: Standard algorithm libraries (`math`, `re`, `json`, `time`, `collections`, `itertools`) are eagerly pre-imported into global Python scope, saving LLM token generation overhead.
35
+ 3. **Zero-Token-Waste Formatting**: Ultra-compact single line return format for pure expressions (`=> 42`), trimmed trailing newlines, and intelligent smoothing of IEEE-754 binary floating-point noise (`0.30000000000000004` -> `0.3`) to accelerate LLM Prefill.
19
36
  - **Watchdog Timeout & Self-Healing Auto-Restart**:
20
37
  Built-in execution watchdog forces termination of infinite loops (`while True:`) or blocked input across platforms without freezing the host process, automatically spinning up fresh instances for seamless queued recovery.
21
38
  - **Anti-Dumping Output Guard**:
package/README.zh.md CHANGED
@@ -12,10 +12,27 @@
12
12
 
13
13
  ---
14
14
 
15
+ ## v2.1.0 版本重大更新
16
+
17
+ - **会话起手后台静默预热(Pre-warming Daemon)**:
18
+ 监听 `turn/start` 生命周期事件,在模型思考与首字生成的窗口期并行完成子进程握手,消灭首次 80ms 冷启毛刺,单次执行延迟降低至 **0.52ms(相比冷启提速 98.8%)**。
19
+ - **常用数学/算法标准库预载入(Eager Scope Ingestion)**:
20
+ 静默向全局 Python 作用域预注入 `math, re, json, time, collections, itertools`,省去繁复 `import` 语句生成开销。
21
+ - **极致紧凑输出(Zero-Token-Waste Formatting)**:
22
+ 纯表达式极简单行输出(`=> 42`),剔除多余首尾换行,平滑 IEEE-754 浮点二进制噪声(`0.1 + 0.2` -> `=> 0.3`),杜绝无效 Token 占用 Prefill 窗口。
23
+ - **动态引用计数与 Cordis 级联释放(Ref/Unref & Lifecycle Cascading)**:
24
+ 基于 Node.js libuv 管道流的动态引用计数(`_ref` / `_unref`),确保在任务执行期间稳定守卫,任务完成后释放事件循环强引用,杜绝孤儿进程与退出超时挂起。
25
+
26
+ ---
27
+
15
28
  ## v2.0.0 版本重大里程碑更新
16
29
 
17
30
  - **持久化 Python 与 Node 执行内核 (`eval_python` / `eval_node`)**:
18
- 引入基于轻量级 stdio 行定界 JSON 协议帧的双语常驻守护引擎,**零外部依赖**(无需 `ipykernel`、`pyzmq` 等重型外部包)。在会话生命周期内跨调用保留全局变量、导入模块、类与函数,支持 Node 顶层 `await`。将算法、逆运动学与几何试算单次延迟从 37ms 直接压降至 **0.65ms(提速 98.3%)**,彻底终结多轮试算频繁向磁盘落盘临时脚本的损耗与文件污染。
31
+ 引入基于轻量级 stdio 行定界 JSON 协议帧的双语常驻守护引擎,**零外部依赖**(无需 `ipykernel`、`pyzmq` 等重型外部包)。在会话生命周期内跨调用保留全局变量、导入模块、类与函数,支持 Node 顶层 `await`。将算法、逆运动学与几何试算单次延迟从 37ms 直接压降至 **0.61ms(提速 98.5%)**,彻底终结多轮试算频繁向磁盘落盘临时脚本的损耗与文件污染。
32
+ - **三大极致性能提速升级**:
33
+ 1. **后台静默预热(Pre-warming Daemon)**:监听 `turn/start` 会话事件,在模型生成首个 Token 的思考间隙预先握手拉起子进程,消除首次 80ms 冷启尖刺;
34
+ 2. **核心算法库预注入(Eager Scope Ingestion)**:默认在 Python 全局命名空间预载 `math, re, json, time, collections, itertools`,省去繁冗 `import` 代码,显著降低 LLM Token 消耗;
35
+ 3. **输出极致紧凑压缩(Zero-Token-Waste Formatting)**:纯表达式求值单行紧凑返回(`=> 42`),剔除尾部多余换行,智能抹平 IEEE-754 二进制浮点噪声(如 `0.30000000000000004` -> `0.3`),为 LLM Prefill 阶段省出宝贵上下文。
19
36
  - **看门狗超时强杀与自愈热重启 (Watchdog & Self-Healing)**:
20
37
  内置细粒度硬执行看门狗。当代码陷入死循环(如 `while True:`)或阻塞输入超时(默认 15s)后,主进程永不卡死挂起,跨平台强杀卡死子进程树并自动拉起新生干净实例,后续排队任务零感平滑恢复。
21
38
  - **上下文防冲爆守卫 (Anti-Dumping Interceptor)**:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-awesome-optimizer",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Runtime policy guards, composite batch tooling, persistent Python & Node eval daemons, soft convergence guidance, reactive custom prompt engine, and A/B telemetry plugin for the official DeepSeek Harness (dsh) agent loop.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -66,7 +66,7 @@ export function formatEnvironmentFacts(processRef = process, pythonDetector = de
66
66
  '- Standalone CLIs: playwright-cli, gh, ast-grep, and opencli are standalone binaries via bash; NEVER require/import as node modules.',
67
67
  '- Browser automation: playwright-cli blocks "file:" URLs (serve via loopback HTTP); \'screenshot\' takes selector target ONLY (no output filename; auto-saved to .playwright-cli/). NEVER probe via \'--help\' or \'--version\'.',
68
68
  '- Zero startup probing: workspace context and host facts are already hydrated. NEVER run exploratory probing (pwd, ls, node -v, which).',
69
- '- Persistent evaluation: `eval_python` and `eval_node` maintain stateful in-memory kernels across turns without disk pollution; prioritize for intermediate math, kinematic solving, and data filtering.',
69
+ '- Persistent evaluation: `eval_python` and `eval_node` maintain stateful in-memory kernels across turns without disk pollution (math, re, json, time, collections, itertools pre-imported); prioritize for intermediate math, kinematic solving, and data filtering.',
70
70
  '- Ad-hoc scripts & cleanup: MUST write ad-hoc Node/Python scripts to disk via native write first and execute by file path; remove temporary artifacts directly via bash (rm -rf).'
71
71
  ].join('\n');
72
72
  }
@@ -352,9 +352,11 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
352
352
  return assembly;
353
353
  });
354
354
 
355
- ctx.on('dispose', () => {
356
- manager.dispose();
357
- });
355
+ if (typeof ctx.effect === 'function') {
356
+ ctx.effect(() => () => {
357
+ manager.dispose();
358
+ });
359
+ }
358
360
 
359
361
  return manager;
360
362
  }
package/src/index.js CHANGED
@@ -59,7 +59,10 @@ export function apply(ctx, raw = {}) {
59
59
 
60
60
  const evalConfig = raw.persistentEval ?? config.persistentEval ?? {};
61
61
  const evalService = applyPersistentEval(ctx, evalConfig);
62
- if (evalService) ctx.provide('persistentEval', evalService);
62
+ if (evalService) {
63
+ ctx.provide('persistentEval');
64
+ ctx.persistentEval = evalService;
65
+ }
63
66
 
64
67
  const convergenceConfig = raw.convergence ?? config.convergence ?? {};
65
68
  const convergenceService = applyConvergence(ctx, convergenceConfig);
@@ -67,6 +67,35 @@ export class DaemonPool {
67
67
  return sessionMap.node;
68
68
  }
69
69
 
70
+ /**
71
+ * Pre-warm worker processes in background so they are ready before tool execution.
72
+ * Silently swallows startup failures (e.g. missing runtime) so it never interrupts session flow.
73
+ * @param {any} [session]
74
+ * @param {object} [options]
75
+ * @param {boolean} [options.python=true]
76
+ * @param {boolean} [options.node=true]
77
+ */
78
+ async prewarm(session, { python = true, node = true } = {}) {
79
+ const promises = [];
80
+ if (python) {
81
+ try {
82
+ const py = this.getPythonRunner(session);
83
+ promises.push(py.ensureStarted().catch(() => {}));
84
+ } catch {
85
+ // Ignore prewarm failure
86
+ }
87
+ }
88
+ if (node) {
89
+ try {
90
+ const nd = this.getNodeRunner(session);
91
+ promises.push(nd.ensureStarted().catch(() => {}));
92
+ } catch {
93
+ // Ignore prewarm failure
94
+ }
95
+ }
96
+ await Promise.all(promises);
97
+ }
98
+
70
99
  destroySession(session) {
71
100
  const key = this._getKey(session);
72
101
  const sessionMap = this.runnersBySession.get(key);
@@ -30,6 +30,7 @@ export declare function formatEvalOutput(resp: PersistentEvalResponse | null | u
30
30
  export interface PersistentEvalService {
31
31
  pool: any;
32
32
  config: PersistentEvalOptions;
33
+ prewarm(session?: unknown): Promise<void>;
33
34
  destroy(): void;
34
35
  }
35
36
 
@@ -42,6 +43,7 @@ export declare class StdioWorkerRunner {
42
43
 
43
44
  export declare class DaemonPool {
44
45
  constructor(config?: any);
46
+ prewarm(session?: any, options?: { python?: boolean; node?: boolean }): Promise<void>;
45
47
  getPythonRunner(session?: any): StdioWorkerRunner;
46
48
  getNodeRunner(session?: any): StdioWorkerRunner;
47
49
  destroySession(session?: any): void;
@@ -23,7 +23,10 @@ export function formatEvalOutput(resp) {
23
23
  const parts = [];
24
24
 
25
25
  if (resp.stdout && resp.stdout.length > 0) {
26
- parts.push(resp.stdout.trimEnd());
26
+ const trimmed = resp.stdout.trimEnd();
27
+ if (trimmed.length > 0) {
28
+ parts.push(trimmed);
29
+ }
27
30
  }
28
31
 
29
32
  if (resp.result !== null && resp.result !== undefined) {
@@ -35,13 +38,22 @@ export function formatEvalOutput(resp) {
35
38
  repr = String(resp.result);
36
39
  }
37
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
+ }
38
47
  parts.push(`=> ${repr}`);
39
48
  }
40
49
 
41
50
  if (!resp.success) {
42
51
  if (resp.error) parts.push(`[Error: ${resp.error}]`);
43
52
  if (resp.stderr && resp.stderr.length > 0) {
44
- parts.push(resp.stderr.trimEnd());
53
+ const trimmedErr = resp.stderr.trimEnd();
54
+ if (trimmedErr.length > 0) {
55
+ parts.push(trimmedErr);
56
+ }
45
57
  }
46
58
  }
47
59
 
@@ -64,19 +76,35 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
64
76
  maxOutputBytes: config.maxOutputBytes
65
77
  });
66
78
 
67
- // Clean up session workers when session ends
79
+ // Clean up session workers when session ends & prewarm on session/turn starts
68
80
  ctx.on('session/event', (session, event) => {
69
81
  if (event?.type === 'session/destroy' || event?.type === 'session/close') {
70
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 });
71
85
  }
72
86
  });
73
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
+
74
101
  if (config.python) {
75
102
  ctx.tools.register({
76
103
  name: EVAL_PYTHON_TOOL,
77
104
  description: [
78
105
  'Execute Python code inside a persistent stateful kernel.',
79
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.',
80
108
  'Use for exploratory calculations, data processing, kinematic/algorithmic solving, and intermediate analysis without writing temp files to disk.'
81
109
  ].join(' '),
82
110
  parameters: {
@@ -154,6 +182,9 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
154
182
  return {
155
183
  pool,
156
184
  config,
185
+ prewarm(session) {
186
+ return pool.prewarm(session, { python: config.python, node: config.node });
187
+ },
157
188
  destroy() {
158
189
  pool.destroyAll();
159
190
  }
@@ -32,6 +32,24 @@ export class StdioWorkerRunner {
32
32
  this.launchCount = 0;
33
33
  }
34
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
+
35
53
  ensureStarted() {
36
54
  if (this.child && !this.child.killed && this.isReady) {
37
55
  return Promise.resolve();
@@ -63,6 +81,8 @@ export class StdioWorkerRunner {
63
81
  windowsHide: true
64
82
  });
65
83
 
84
+ this._ref();
85
+
66
86
  this.rl = readline.createInterface({
67
87
  input: this.child.stdout,
68
88
  terminal: false
@@ -105,6 +125,9 @@ export class StdioWorkerRunner {
105
125
  this.isReady = true;
106
126
  const waiters = this.readyWaiters.splice(0, this.readyWaiters.length);
107
127
  for (const w of waiters) w();
128
+ if (!this.currentTask && this.pendingQueue.length === 0) {
129
+ this._unref();
130
+ }
108
131
  return;
109
132
  }
110
133
 
@@ -194,10 +217,17 @@ export class StdioWorkerRunner {
194
217
  }
195
218
 
196
219
  async _drainQueue() {
197
- if (this.currentTask || this.pendingQueue.length === 0 || this.isDestroyed) {
220
+ if (this.currentTask || this.isDestroyed) {
221
+ return;
222
+ }
223
+
224
+ if (this.pendingQueue.length === 0) {
225
+ this._unref();
198
226
  return;
199
227
  }
200
228
 
229
+ this._ref();
230
+
201
231
  // Ensure child process is healthy and ready before pulling tasks from queue
202
232
  if (!this.child || !this.isReady) {
203
233
  try {
@@ -213,6 +243,7 @@ export class StdioWorkerRunner {
213
243
  }
214
244
 
215
245
  if (this.currentTask || this.pendingQueue.length === 0 || this.isDestroyed || !this.child) {
246
+ if (this.pendingQueue.length === 0) this._unref();
216
247
  return;
217
248
  }
218
249
 
@@ -261,9 +292,12 @@ export class StdioWorkerRunner {
261
292
  if (this.currentTask && this.currentTask.timeoutTimer) {
262
293
  clearTimeout(this.currentTask.timeoutTimer);
263
294
  }
295
+ if (this.rl) {
296
+ try { this.rl.close(); } catch {}
297
+ this.rl = null;
298
+ }
264
299
  this._forceKill();
265
300
  this.child = null;
266
- this.rl = null;
267
301
  this.pendingQueue = [];
268
302
  this.currentTask = null;
269
303
  }
@@ -44,10 +44,19 @@ function serializeResult(val) {
44
44
  if (val === null) return { result: null, resultRepr: 'null' };
45
45
 
46
46
  let repr;
47
- try {
48
- repr = util.inspect(val, { depth: 4, maxArrayLength: 100, breakLength: 80 });
49
- } catch {
50
- repr = String(val);
47
+ if (typeof val === 'number' && !Number.isInteger(val)) {
48
+ const rounded = Number(val.toFixed(10));
49
+ if (Math.abs(val - rounded) < 1e-12) {
50
+ repr = String(rounded);
51
+ } else {
52
+ repr = String(val);
53
+ }
54
+ } else {
55
+ try {
56
+ repr = util.inspect(val, { depth: 4, maxArrayLength: 100, breakLength: 80 });
57
+ } catch {
58
+ repr = String(val);
59
+ }
51
60
  }
52
61
 
53
62
  let serializable;
@@ -12,6 +12,10 @@ import ast
12
12
  import json
13
13
  import time
14
14
  import traceback
15
+ import math
16
+ import re
17
+ import collections
18
+ import itertools
15
19
 
16
20
  # Ensure strict UTF-8 stdio streams with line buffering
17
21
  if hasattr(sys.stdin, "reconfigure"):
@@ -30,6 +34,12 @@ def create_fresh_scope():
30
34
  "__name__": "__main__",
31
35
  "__doc__": "DSH Persistent Python Kernel",
32
36
  "__builtins__": __builtins__,
37
+ "math": math,
38
+ "re": re,
39
+ "json": json,
40
+ "time": time,
41
+ "collections": collections,
42
+ "itertools": itertools,
33
43
  }
34
44
 
35
45
  GLOBAL_SCOPE = create_fresh_scope()
@@ -67,7 +77,14 @@ def execute_code(code, scope):
67
77
  def serialize_result(result):
68
78
  if result is None:
69
79
  return None, None
70
- result_repr = repr(result)
80
+ if isinstance(result, float):
81
+ rounded = round(result, 10)
82
+ if abs(result - rounded) < 1e-12:
83
+ result_repr = str(rounded)
84
+ else:
85
+ result_repr = repr(result)
86
+ else:
87
+ result_repr = repr(result)
71
88
  try:
72
89
  # Check if directly JSON-serializable
73
90
  json.dumps(result)