dsh-awesome-optimizer 3.3.0 → 3.5.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
@@ -5,20 +5,20 @@ English | [中文](README.zh.md)
5
5
  [![npm version](https://img.shields.io/npm/v/dsh-awesome-optimizer.svg?color=blue)](https://www.npmjs.com/package/dsh-awesome-optimizer)
6
6
  [![license](https://img.shields.io/github/license/Jaylor-Wang/dsh-awesome-optimizer.svg)](LICENSE)
7
7
  [![GitHub release](https://img.shields.io/github/v/release/Jaylor-Wang/dsh-awesome-optimizer.svg)](https://github.com/Jaylor-Wang/dsh-awesome-optimizer/releases)
8
- [![Tests](https://img.shields.io/badge/tests-160%20passed-brightgreen.svg)](test/)
8
+ [![Tests](https://img.shields.io/badge/tests-167%20passed-brightgreen.svg)](test/)
9
9
  [![TypeScript](https://img.shields.io/badge/TypeScript-Strict%20Types-blue.svg)](src/)
10
10
 
11
11
  High-performance runtime policy guards, persistent Python & Node evaluation daemons, PTC-backed composite batch tooling, standalone refined system prompt engine, and objective A/B turn telemetry plugin for the official [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (`@deepseek-ai/dsh-agent-loop`).
12
12
 
13
13
  ---
14
14
 
15
- ## Highlights in v3.3.0
15
+ ## Highlights in v3.5.0
16
16
 
17
- - **Opt-in HTTP cache diagnostics**: [Capture request hashes and upstream usage](cache-diagnostics.md) for OpenAI-compatible `/chat/completions` to compare message prefixes and configuration across turns. Missing cache counters remain distinct from explicit zero; prompts, responses, image bytes, and credentials are never logged.
18
- - **Bounded, isolated diagnostics**: Asynchronous logging has file and queue limits, flushes on disposal, and stops with one warning on failure. Initialization failures preserve telemetry; response URL, redirect/type metadata, and clone metadata are preserved.
19
- - **Accurate prefix telemetry**: Detects message replacement, in-place mutation, and changes across turns without stale object-identity digest caching.
20
- - **Stricter batch preflight**: Rejects empty or unchanged replacement strings before dispatching edits, keeping atomic preflight consistent with native edit validation.
21
- - **Verification**: 160 tests passed; public declaration type checking passed. No new package dependencies.
17
+ - **Sandbox-authoritative file inspection**: Edit recovery and batch preflight never fall back to local disk when the host filesystem service denies, fails, or cancels a read.
18
+ - **Byte-bounded persistent workers**: Python and Node output limits are enforced in UTF-8 bytes, and external stderr accumulation is bounded.
19
+ - **Unified worker lifecycle**: Session disposal, restart, and plugin shutdown clean up persistent workers consistently and prevent unintended worker recreation.
20
+ - **Concurrent session coverage**: Tests cover isolated multi-session execution, cancellation, unexpected worker termination, and disposal races.
21
+ - **Verification**: 167 tests passed; public declaration type checking passed. No new package dependencies.
22
22
 
23
23
  ---
24
24
 
@@ -27,7 +27,7 @@ High-performance runtime policy guards, persistent Python & Node evaluation daem
27
27
  ### 1. Refined System Prompt & File-Driven Hot-Reload (`customPrompt`)
28
28
 
29
29
  - **Standalone Mode (`placement: "replace-all"`)**:
30
- Hooks into the DSH `system-prompt/assemble` waterfall to strip all official verbose sections (`harness:*`, `deployment:*`, `tool:*`, `deliverable:*`, `web:*`), preserving convergence guidance companion sections while presenting your refined `SYSTEM.md` as the standalone authority.
30
+ Hooks into the DSH `system-prompt/assemble` waterfall to strip all official verbose sections (`harness:*`, `deployment:*`, `tool:*`, `deliverable:*`, `web:*`), preserving convergence guidance companion sections while presenting your refined `SYSTEM.md` as the standalone authority. This intentionally abandons those official prompt sections; add any instructions you still need to `SYSTEM.md`.
31
31
  - **Dual-Layer Markdown Convention**:
32
32
  Automatically discovers global `~/.dsh/SYSTEM.md` (or `$DSH_HOME/SYSTEM.md`) and project workspace `.dsh/SYSTEM.md` (or `SYSTEM.md`), cleanly separating developer system defaults from repo-specific instructions.
33
33
  - **Millisecond Hot-Reload**:
package/README.zh.md CHANGED
@@ -5,20 +5,20 @@
5
5
  [![npm version](https://img.shields.io/npm/v/dsh-awesome-optimizer.svg?color=blue)](https://www.npmjs.com/package/dsh-awesome-optimizer)
6
6
  [![license](https://img.shields.io/github/license/Jaylor-Wang/dsh-awesome-optimizer.svg)](LICENSE)
7
7
  [![GitHub release](https://img.shields.io/github/v/release/Jaylor-Wang/dsh-awesome-optimizer.svg)](https://github.com/Jaylor-Wang/dsh-awesome-optimizer/releases)
8
- [![Tests](https://img.shields.io/badge/tests-160%20passed-brightgreen.svg)](test/)
8
+ [![Tests](https://img.shields.io/badge/tests-167%20passed-brightgreen.svg)](test/)
9
9
  [![TypeScript](https://img.shields.io/badge/TypeScript-Strict%20Types-blue.svg)](src/)
10
10
 
11
11
  围绕官方 [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (`@deepseek-ai/dsh-agent-loop`) 打造的高性能运行时策略防护、持久化 Python/Node 执行内核、PTC 复合批处理工具、全新独立系统提示词接管与客观 A/B 轮次度量插件。
12
12
 
13
13
  ---
14
14
 
15
- ## v3.3.0 HTTP 缓存诊断与编辑预检修复
15
+ ## v3.5.0 Worker 字节限制与生命周期加固
16
16
 
17
- - **可选 HTTP 缓存诊断**:为 OpenAI 兼容的 `/chat/completions` [记录请求哈希与上游原始 usage](cache-diagnostics.md),跨轮比较消息前缀和配置。缓存字段缺失与显式零值分别保留;不记录提示词、回复、图片字节或凭据。
18
- - **诊断边界与故障隔离**:异步日志限制文件大小和队列容量,释放时等待写入完成,失败时仅警告一次并停止诊断。初始化失败不影响 telemetry;保留响应 URL、重定向、类型及克隆元数据。
19
- - **准确的前缀检测**:重新计算消息摘要,识别消息替换、原地修改和跨轮变化,消除按对象身份缓存摘要造成的盲区。
20
- - **严格的批量编辑预检**:派发编辑前拒绝空匹配串及新旧内容相同的替换,使 atomic 预检与原生编辑校验保持一致。
21
- - **验证**:160 项测试通过,公开声明类型检查通过,无新增包依赖。
17
+ - **文件服务遵循沙箱权限**:Edit recovery 与批量编辑预检在宿主文件服务拒绝、读取失败或取消时,不会回退到本地磁盘。
18
+ - **Worker 输出按字节限制**:Python 与 Node 的输出上限按 UTF-8 字节执行;外部 stderr 累积也有明确内存上限。
19
+ - **统一 Worker 生命周期**:session disposal、显式 session 重启及插件关闭时一致清理持久 Worker,并防止意外重建。
20
+ - **并发与异常场景覆盖**:测试涵盖多 session 隔离并发、取消、Worker 意外退出和 disposal 竞态。
21
+ - **验证**:167 项测试通过,公开声明类型检查通过,无新增包依赖。
22
22
 
23
23
  ---
24
24
 
@@ -27,7 +27,7 @@
27
27
  ### 1. 系统提示词精简与文件驱动热重载 (`customPrompt`)
28
28
 
29
29
  - **独立接管模式 (`placement: "replace-all"`)**:
30
- 在 DSH 官方 `system-prompt/assemble` 组装瀑布流中,精确剥除官方 verbose 冗余章节(包括 `harness:*`、`deployment:*`、`tool:*`、`deliverable:*`、`web:*` 等),并保留会话收敛指导等协同章节,将系统提示词基线精简至约 380 tokens 的轻量核心指令。
30
+ 在 DSH 官方 `system-prompt/assemble` 组装瀑布流中,精确剥除官方 verbose 冗余章节(包括 `harness:*`、`deployment:*`、`tool:*`、`deliverable:*`、`web:*` 等),并保留会话收敛指导等协同章节,将系统提示词基线精简至约 380 tokens 的轻量核心指令。此模式会主动放弃这些官方 prompt sections;仍需保留的指令应自行补入 `SYSTEM.md`。
31
31
  - **双层 Markdown 规约**:
32
32
  自动寻找全局配置 `~/.dsh/SYSTEM.md`(或 `$DSH_HOME/SYSTEM.md`)与项目级工作区配置 `.dsh/SYSTEM.md`(或 `SYSTEM.md`)。权责清晰,与面向角色的 `AGENTS.md` 解耦。
33
33
  - **毫秒级防抖热重载(Hot-Reload)**:
package/cordis.patch.yml CHANGED
@@ -52,3 +52,7 @@
52
52
  telemetry:
53
53
  enabled: true
54
54
  trackStreams: true
55
+ # Use the host compaction backend to bound long-session context.
56
+ contextCompaction:
57
+ enabled: true
58
+ recoverOverflow: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-awesome-optimizer",
3
- "version": "3.3.0",
3
+ "version": "3.5.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",
@@ -30,6 +30,10 @@
30
30
  "types": "./src/read-guard.d.ts",
31
31
  "default": "./src/read-guard.js"
32
32
  },
33
+ "./context-compaction": {
34
+ "types": "./src/context-compaction.d.ts",
35
+ "default": "./src/context-compaction.js"
36
+ },
33
37
  "./composite-tools": {
34
38
  "types": "./src/composite-tools.d.ts",
35
39
  "default": "./src/composite-tools.js"
@@ -147,12 +147,15 @@ function countOccurrences(haystack, needle) {
147
147
 
148
148
  async function readFileForPreflight(filePath, cwd, fsService, signal) {
149
149
  if (fsService) {
150
+ // A service failure is authoritative; never bypass sandbox or cancellation through local disk.
150
151
  if (typeof fsService.readFile === 'function') {
151
152
  try {
152
- const content = await fsService.readFile(filePath, { cwd, signal });
153
- if (typeof content === 'string') return { ok: true, content };
153
+ const content = await fsService.readFile(filePath, { cwd, signal, encoding: 'utf8' });
154
+ return typeof content === 'string' ? { ok: true, content } : null;
154
155
  } catch (err) {
155
- if (err?.code === 'ENOENT') return { ok: false, missing: true };
156
+ return err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND'
157
+ ? { ok: false, missing: true }
158
+ : null;
156
159
  }
157
160
  }
158
161
  if (typeof fsService.readText === 'function') {
@@ -161,31 +164,23 @@ async function readFileForPreflight(filePath, cwd, fsService, signal) {
161
164
  ? await fsService.resolve(filePath, { cwd, signal })
162
165
  : filePath;
163
166
  const content = await fsService.readText(target, signal);
164
- if (typeof content === 'string') return { ok: true, content };
167
+ return typeof content === 'string' ? { ok: true, content } : null;
165
168
  } catch (err) {
166
- if (err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND') return { ok: false, missing: true };
169
+ return err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND'
170
+ ? { ok: false, missing: true }
171
+ : null;
167
172
  }
168
173
  }
174
+ return null;
169
175
  }
170
176
 
177
+ // Standalone hosts without an fs service may use local preflight inspection.
171
178
  try {
172
179
  const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
173
180
  const content = await fs.readFile(resolved, 'utf8');
174
181
  return { ok: true, content };
175
182
  } catch (err) {
176
- if (err?.code === 'ENOENT') {
177
- if (fsService && typeof fsService.resolve === 'function' && typeof fsService.stat === 'function') {
178
- try {
179
- const target = await fsService.resolve(filePath, { cwd, signal });
180
- const stat = await fsService.stat(target, signal);
181
- if (stat) return null;
182
- } catch {
183
- // ignore
184
- }
185
- }
186
- return { ok: false, missing: true };
187
- }
188
- return null;
183
+ return err?.code === 'ENOENT' ? { ok: false, missing: true } : null;
189
184
  }
190
185
  }
191
186
 
@@ -0,0 +1,34 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type z from '@deepseek-ai/schemastery';
3
+
4
+ export interface ContextCompactionOptions {
5
+ /** Enable the optimizer-owned bridge to the host compaction service. Default: true. */
6
+ enabled?: boolean;
7
+ /** Compact and retry provider-confirmed context-window overflows. Default: true. */
8
+ recoverOverflow?: boolean;
9
+ }
10
+
11
+ export interface ContextCompactionStats {
12
+ pressureAttempts: number;
13
+ pressureCompactions: number;
14
+ overflowAttempts: number;
15
+ overflowCompactions: number;
16
+ failures: number;
17
+ }
18
+
19
+ export interface ContextCompactionService {
20
+ getStats(): ContextCompactionStats;
21
+ }
22
+
23
+ export declare const CONTEXT_WINDOW_EXCEEDED_CODE: 'CONTEXT_WINDOW_EXCEEDED';
24
+ export declare const ContextCompactionConfig: z<ContextCompactionOptions>;
25
+ export declare function applyContextCompaction(
26
+ ctx: Context,
27
+ rawConfig?: ContextCompactionOptions
28
+ ): ContextCompactionService | null;
29
+ export declare const name: string;
30
+ export declare const inject: string[];
31
+ export declare const Config: z<ContextCompactionOptions>;
32
+ export declare function apply(ctx: Context, config?: ContextCompactionOptions): void;
33
+ declare const plugin: { name: typeof name; inject: typeof inject; Config: typeof Config; apply: typeof apply };
34
+ export default plugin;
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Opt into the host compaction seam without changing the official agent loop.
3
+ * This keeps long sessions below provider context pressure when the selected
4
+ * compaction backend is present, even if its own automatic listeners are off.
5
+ */
6
+ import z from '@deepseek-ai/schemastery';
7
+
8
+ export const CONTEXT_WINDOW_EXCEEDED_CODE = 'CONTEXT_WINDOW_EXCEEDED';
9
+
10
+ export const ContextCompactionConfig = z.object({
11
+ enabled: z.boolean().default(true),
12
+ recoverOverflow: z.boolean().default(true)
13
+ });
14
+
15
+ function compactService(ctx) {
16
+ try {
17
+ const service = ctx.get('compaction');
18
+ return service && typeof service.compactIfNeeded === 'function' ? service : null;
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+
24
+ function log(ctx, level, message) {
25
+ try { ctx.logger?.[level]?.(`[optimizer-context-compaction] ${message}`); } catch {}
26
+ }
27
+
28
+ export function applyContextCompaction(ctx, rawConfig = {}) {
29
+ const config = ContextCompactionConfig(rawConfig);
30
+ if (!config.enabled) return null;
31
+
32
+ let disposed = false;
33
+ let pressureAttempts = 0;
34
+ let pressureCompactions = 0;
35
+ let overflowAttempts = 0;
36
+ let overflowCompactions = 0;
37
+ let failures = 0;
38
+
39
+ ctx.on('dispose', () => { disposed = true; });
40
+
41
+ ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
42
+ if (!disposed && !signal?.aborted) {
43
+ const compaction = compactService(ctx);
44
+ if (compaction) {
45
+ pressureAttempts++;
46
+ try {
47
+ const result = await compaction.compactIfNeeded(agent, 'pressure', signal);
48
+ if (result) pressureCompactions++;
49
+ } catch (error) {
50
+ if (!signal?.aborted) {
51
+ failures++;
52
+ log(ctx, 'warn', `pressure check failed: ${error instanceof Error ? error.message : String(error)}`);
53
+ }
54
+ }
55
+ }
56
+ }
57
+ return next();
58
+ });
59
+
60
+ if (config.recoverOverflow) {
61
+ ctx.on('agent/request-error', async ({ agent, failure, signal }, next) => {
62
+ if (
63
+ disposed || signal?.aborted || failure?.code !== CONTEXT_WINDOW_EXCEEDED_CODE
64
+ ) return next();
65
+
66
+ const compaction = compactService(ctx);
67
+ if (!compaction) return next();
68
+
69
+ const generation = agent.session?.surface?.replaceGeneration;
70
+ overflowAttempts++;
71
+ try {
72
+ const result = await compaction.compactIfNeeded(agent, 'context-overflow', signal);
73
+ const advanced = Number.isFinite(generation)
74
+ && agent.session?.surface?.replaceGeneration > generation;
75
+ if (result || advanced) overflowCompactions++;
76
+ if (!signal?.aborted && advanced) return { kind: 'retry' };
77
+ } catch (error) {
78
+ if (!signal?.aborted) {
79
+ failures++;
80
+ log(ctx, 'warn', `overflow recovery failed: ${error instanceof Error ? error.message : String(error)}`);
81
+ if (Number.isFinite(generation) && agent.session?.surface?.replaceGeneration > generation) {
82
+ overflowCompactions++;
83
+ return { kind: 'retry' };
84
+ }
85
+ }
86
+ }
87
+ return next();
88
+ });
89
+ }
90
+
91
+ return {
92
+ getStats() {
93
+ return {
94
+ pressureAttempts,
95
+ pressureCompactions,
96
+ overflowAttempts,
97
+ overflowCompactions,
98
+ failures
99
+ };
100
+ }
101
+ };
102
+ }
103
+
104
+ export const name = 'agent-optimizer-context-compaction';
105
+ export const inject = ['agent'];
106
+ export const Config = ContextCompactionConfig;
107
+ export function apply(ctx, config = {}) {
108
+ const service = applyContextCompaction(ctx, config);
109
+ if (service) ctx.provide('contextCompaction', service);
110
+ }
111
+ export default { name, inject, Config, apply };
@@ -312,8 +312,8 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
312
312
 
313
313
  const manager = new CustomPromptManager(config);
314
314
 
315
- // If placement is 'suffix' or 'section', register section directly with systemPrompt service
316
- if ((config.placement === 'suffix' || config.placement === 'section') && ctx.systemPrompt?.section) {
315
+ // For suffix placement, register the section directly with the systemPrompt service
316
+ if (config.placement === 'suffix' && ctx.systemPrompt?.section) {
317
317
  ctx.systemPrompt.section({
318
318
  name: config.sectionName,
319
319
  order: config.sectionOrder,
@@ -325,7 +325,7 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
325
325
  });
326
326
  }
327
327
 
328
- // Also hook into waterfall 'system-prompt/assemble' for prefix or replace-persona-suffix
328
+ // Hook into the waterfall for both supported placement modes.
329
329
  ctx.on('system-prompt/assemble', (assembly, context) => {
330
330
  const wsRoot = resolveWorkspaceRoot(ctx, context);
331
331
  const customText = manager.getEffectivePrompt(wsRoot);
@@ -386,6 +386,7 @@ async function readTargetForRecovery(filePath, cwd, fsService, signal, maxInspec
386
386
  if (!filePath || typeof filePath !== 'string') return null;
387
387
 
388
388
  if (fsService) {
389
+ // A service failure is authoritative; never bypass sandbox or cancellation through local disk.
389
390
  if (typeof fsService.readFile === 'function') {
390
391
  try {
391
392
  const content = await fsService.readFile(filePath, { cwd, signal, encoding: 'utf8' });
@@ -404,11 +405,13 @@ async function readTargetForRecovery(filePath, cwd, fsService, signal, maxInspec
404
405
  if (typeof content !== 'string') return null;
405
406
  return Buffer.byteLength(content, 'utf8') <= maxInspectBytes ? content : null;
406
407
  } catch {
407
- // Fall through to disk
408
+ return null;
408
409
  }
409
410
  }
411
+ return null;
410
412
  }
411
413
 
414
+ // Standalone hosts without an fs service may use a bounded local inspection.
412
415
  try {
413
416
  const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
414
417
  const stat = await fs.stat(resolved);
package/src/index.d.ts CHANGED
@@ -9,6 +9,7 @@ import type { PersistentEvalOptions, PersistentEvalService } from './persistent-
9
9
  import type { ConvergenceOptions } from './convergence.d.ts';
10
10
  import type { CustomPromptOptions } from './custom-prompt.d.ts';
11
11
  import type { TelemetryOptions } from './telemetry.d.ts';
12
+ import type { ContextCompactionOptions, ContextCompactionService } from './context-compaction.d.ts';
12
13
 
13
14
  export interface AgentOptimizerConfig {
14
15
  enabled?: boolean;
@@ -24,6 +25,7 @@ export interface AgentOptimizerConfig {
24
25
  convergence?: ConvergenceOptions;
25
26
  customPrompt?: CustomPromptOptions;
26
27
  telemetry?: TelemetryOptions;
28
+ contextCompaction?: ContextCompactionOptions;
27
29
  }
28
30
 
29
31
  declare module '@deepseek-ai/dsh-llm' {
@@ -122,6 +124,12 @@ export {
122
124
  type TurnTelemetrySummary,
123
125
  type TurnComparisonDelta
124
126
  } from './telemetry.d.ts';
127
+ export {
128
+ applyContextCompaction,
129
+ ContextCompactionConfig,
130
+ type ContextCompactionOptions,
131
+ type ContextCompactionService
132
+ } from './context-compaction.d.ts';
125
133
  export {
126
134
  EVAL_PYTHON_TOOL,
127
135
  EVAL_NODE_TOOL,
package/src/index.js CHANGED
@@ -10,6 +10,7 @@ import { applyPersistentEval, PersistentEvalConfig, EVAL_PYTHON_TOOL, EVAL_NODE_
10
10
  import { applyConvergence, ConvergenceConfig, SOURCE as CONVERGENCE_SOURCE, GUIDANCE, REMINDER } from './convergence.js';
11
11
  import { applyCustomPrompt, CustomPromptConfig, CustomPromptManager, SECTION_NAME as CUSTOM_PROMPT_SECTION, ENVIRONMENT_FACTS_SECTION, formatEnvironmentFacts, detectPythonEnv, resetPythonCacheForTesting } from './custom-prompt.js';
12
12
  import { applyTelemetry, TelemetryConfig, AgentTelemetryManager, compareTurnSummaries } from './telemetry.js';
13
+ import { applyContextCompaction, ContextCompactionConfig } from './context-compaction.js';
13
14
 
14
15
  import { lintPrompt, fixPrompt, lintPromptFile, RULE_IDS as PROMPT_LINT_RULES } from './prompt-linter.js';
15
16
 
@@ -29,7 +30,8 @@ export const Config = z.object({
29
30
  persistentEval: PersistentEvalConfig.default({}),
30
31
  convergence: ConvergenceConfig.default({}),
31
32
  customPrompt: CustomPromptConfig.default({}),
32
- telemetry: TelemetryConfig.default({})
33
+ telemetry: TelemetryConfig.default({}),
34
+ contextCompaction: ContextCompactionConfig.default({})
33
35
  });
34
36
 
35
37
  /**
@@ -86,6 +88,10 @@ export function apply(ctx, raw = {}) {
86
88
  const telemetryConfig = raw.telemetry ?? config.telemetry ?? {};
87
89
  const telemetryManager = applyTelemetry(ctx, telemetryConfig);
88
90
  if (telemetryManager) ctx.provide('agentTelemetry', telemetryManager);
91
+
92
+ const compactionConfig = raw.contextCompaction ?? config.contextCompaction ?? {};
93
+ const compactionManager = applyContextCompaction(ctx, compactionConfig);
94
+ if (compactionManager) ctx.provide('contextCompaction', compactionManager);
89
95
  }
90
96
 
91
97
  export {
@@ -134,7 +140,9 @@ export {
134
140
  lintPromptFile,
135
141
  PROMPT_LINT_RULES,
136
142
  GUIDANCE,
137
- REMINDER
143
+ REMINDER,
144
+ applyContextCompaction,
145
+ ContextCompactionConfig
138
146
  };
139
147
 
140
148
  export default { name, inject, Config, apply };
@@ -20,6 +20,8 @@ export class DaemonPool {
20
20
  constructor(config = {}) {
21
21
  this.config = config;
22
22
  this.runnersBySession = new Map();
23
+ this.disposedKeys = new Set();
24
+ this.isDestroyed = false;
23
25
  }
24
26
 
25
27
  _getKey(session) {
@@ -27,8 +29,20 @@ export class DaemonPool {
27
29
  return session.id || session;
28
30
  }
29
31
 
32
+ _assertSessionActive(key) {
33
+ if (this.isDestroyed) throw new Error('Daemon pool has been destroyed.');
34
+ if (this.disposedKeys.has(key)) throw new Error('Session workers have been destroyed.');
35
+ }
36
+
37
+ reviveSession(session) {
38
+ if (this.isDestroyed) return false;
39
+ this.disposedKeys.delete(this._getKey(session));
40
+ return true;
41
+ }
42
+
30
43
  getPythonRunner(session) {
31
44
  const key = this._getKey(session);
45
+ this._assertSessionActive(key);
32
46
  let sessionMap = this.runnersBySession.get(key);
33
47
  if (!sessionMap) {
34
48
  sessionMap = {};
@@ -50,6 +64,7 @@ export class DaemonPool {
50
64
 
51
65
  getNodeRunner(session) {
52
66
  const key = this._getKey(session);
67
+ this._assertSessionActive(key);
53
68
  let sessionMap = this.runnersBySession.get(key);
54
69
  if (!sessionMap) {
55
70
  sessionMap = {};
@@ -77,6 +92,8 @@ export class DaemonPool {
77
92
  * @param {boolean} [options.node=true]
78
93
  */
79
94
  async prewarm(session, { python = true, node = true } = {}) {
95
+ const key = this._getKey(session);
96
+ if (this.isDestroyed || this.disposedKeys.has(key)) return;
80
97
  const promises = [];
81
98
  if (python) {
82
99
  try {
@@ -99,6 +116,7 @@ export class DaemonPool {
99
116
 
100
117
  destroySession(session) {
101
118
  const key = this._getKey(session);
119
+ this.disposedKeys.add(key);
102
120
  const sessionMap = this.runnersBySession.get(key);
103
121
  if (!sessionMap) return;
104
122
 
@@ -112,9 +130,12 @@ export class DaemonPool {
112
130
  }
113
131
 
114
132
  destroyAll() {
133
+ if (this.isDestroyed) return;
134
+ this.isDestroyed = true;
115
135
  for (const [key, sessionMap] of this.runnersBySession.entries()) {
116
136
  if (sessionMap.python) sessionMap.python.destroy();
117
137
  if (sessionMap.node) sessionMap.node.destroy();
138
+ this.disposedKeys.add(key);
118
139
  }
119
140
  this.runnersBySession.clear();
120
141
  }
@@ -79,6 +79,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
79
79
  });
80
80
 
81
81
  const workingDirectories = new Map();
82
+ let disposed = false;
82
83
  const sessionKey = session => session?.id || session || '__global__';
83
84
  function getWorkingDirectory(session) {
84
85
  const key = sessionKey(session);
@@ -94,28 +95,32 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
94
95
  pool.destroySession(session);
95
96
  }
96
97
  function destroy() {
98
+ if (disposed) return;
99
+ disposed = true;
97
100
  workingDirectories.clear();
98
101
  pool.destroyAll();
99
102
  }
103
+ function prewarmSession(session) {
104
+ if (disposed) return;
105
+ void pool.prewarm(session, { python: config.python, node: config.node });
106
+ }
100
107
 
101
- // Clean up session workers when session ends & prewarm on session/turn starts
108
+ // Clean up session workers when sessions end. The official session/event stream
109
+ // is the single lifecycle source; avoid duplicate turn/start prewarm listeners.
102
110
  ctx.on('session/event', (session, event) => {
103
- if (event?.type === 'session/destroy' || event?.type === 'session/close') {
111
+ if (event?.type === 'session/destroy' || event?.type === 'session/close' || event?.type === 'session/disposed') {
104
112
  destroySession(session);
113
+ } else if (event?.type === 'session/create' || event?.type === 'session/start') {
114
+ pool.reviveSession(session);
115
+ prewarmSession(session);
105
116
  } else if (event?.type === 'working-directory/change') {
106
117
  workingDirectories.set(sessionKey(session), event.data.cwd);
107
- } else if (event?.type === 'turn/start' || event?.type === 'session/start' || event?.type === 'session/create') {
108
- pool.prewarm(session, { python: config.python, node: config.node });
118
+ } else if (event?.type === 'turn/start') {
119
+ prewarmSession(session);
109
120
  }
110
121
  });
111
122
 
112
123
  ctx.on('session/disposed', destroySession);
113
-
114
- // Pre-warm on turn/start event so worker processes are already hot during model inference
115
- ctx.on('turn/start', (turn) => {
116
- const session = turn?.agent?.session ?? turn?.session;
117
- pool.prewarm(session, { python: config.python, node: config.node });
118
- });
119
124
  // Clean up all daemon workers when plugin context is disposed
120
125
  if (typeof ctx.effect === 'function') {
121
126
  ctx.effect(() => destroy);
@@ -152,6 +157,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
152
157
  },
153
158
  isConcurrencySafe: () => false,
154
159
  async execute(args, exec) {
160
+ if (disposed) throw new Error('Persistent eval service has been disposed.');
155
161
  exec?.signal?.throwIfAborted();
156
162
  const session = exec?.agent?.session ?? exec?.session;
157
163
  const runner = pool.getPythonRunner(session);
@@ -199,6 +205,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
199
205
  },
200
206
  isConcurrencySafe: () => false,
201
207
  async execute(args, exec) {
208
+ if (disposed) throw new Error('Persistent eval service has been disposed.');
202
209
  exec?.signal?.throwIfAborted();
203
210
  const session = exec?.agent?.session ?? exec?.session;
204
211
  const runner = pool.getNodeRunner(session);
@@ -220,6 +227,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
220
227
  pool,
221
228
  config,
222
229
  prewarm(session) {
230
+ if (disposed) return Promise.resolve();
223
231
  return pool.prewarm(session, { python: config.python, node: config.node });
224
232
  },
225
233
  destroy
@@ -1,6 +1,38 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import readline from 'node:readline';
3
3
  import process from 'node:process';
4
+
5
+ const OUTPUT_TRUNCATION_SUFFIX = '\n\n[Output truncated: output exceeded the configured byte limit.]';
6
+
7
+ function byteLength(value) {
8
+ return Buffer.byteLength(value, 'utf8');
9
+ }
10
+
11
+ function truncateUtf8(value, maxBytes, suffix = OUTPUT_TRUNCATION_SUFFIX) {
12
+ const text = typeof value === 'string' ? value : String(value ?? '');
13
+ if (byteLength(text) <= maxBytes) return text;
14
+ if (maxBytes <= 0) return '';
15
+ let effectiveSuffix = suffix;
16
+ if (byteLength(effectiveSuffix) >= maxBytes) effectiveSuffix = '\n[Output truncated]';
17
+ const suffixBytes = byteLength(effectiveSuffix);
18
+ const source = Buffer.from(text, 'utf8');
19
+ if (suffixBytes >= maxBytes) {
20
+ let end = Math.min(maxBytes, source.length);
21
+ while (end > 0 && (source[end] & 0xc0) === 0x80) end--;
22
+ return source.subarray(0, end).toString('utf8');
23
+ }
24
+ const contentBytes = maxBytes - suffixBytes;
25
+ let end = Math.min(contentBytes, source.length);
26
+ while (end > 0 && (source[end] & 0xc0) === 0x80) end--;
27
+ return source.subarray(0, end).toString('utf8') + effectiveSuffix;
28
+ }
29
+
30
+ function appendUtf8Bounded(current, chunk, maxBytes) {
31
+ const text = typeof chunk === 'string' ? chunk : String(chunk ?? '');
32
+ const remaining = Math.max(0, maxBytes - byteLength(current));
33
+ if (!remaining) return current;
34
+ return current + truncateUtf8(text, remaining, '');
35
+ }
4
36
  /**
5
37
  * Convert an aborted signal's reason into an Error suitable for rejection
6
38
  * and DSH cancellation classification.
@@ -123,7 +155,11 @@ export class StdioWorkerRunner {
123
155
  if (this.child !== child) return;
124
156
  // Worker-level unhandled stderr logging (not during task execution)
125
157
  if (this.currentTask) {
126
- this.currentTask.stderrChunk = (this.currentTask.stderrChunk || '') + chunk.toString('utf-8');
158
+ const before = this.currentTask.stderrChunk || '';
159
+ this.currentTask.stderrChunk = appendUtf8Bounded(before, chunk, this.maxOutputBytes);
160
+ if (byteLength(String(chunk)) > Math.max(0, this.maxOutputBytes - byteLength(before))) {
161
+ this.currentTask.stderrTruncated = true;
162
+ }
127
163
  }
128
164
  });
129
165
 
@@ -187,19 +223,14 @@ export class StdioWorkerRunner {
187
223
  }
188
224
 
189
225
  _truncateAndResolve(task, response) {
190
- let stdout = response.stdout || '';
191
- let stderr = (response.stderr || '') + (task.stderrChunk || '');
192
-
193
226
  const limit = this.maxOutputBytes;
194
- if (stdout.length > limit) {
195
- stdout = stdout.slice(0, limit) + `\n\n[Output truncated: stdout exceeded ${limit} bytes. If full payload is required, write it to a file directly.]`;
196
- }
197
- if (stderr.length > limit) {
198
- stderr = stderr.slice(0, limit) + `\n\n[Output truncated: stderr exceeded ${limit} bytes.]`;
199
- }
200
-
201
- response.stdout = stdout;
202
- response.stderr = stderr;
227
+ response.stdout = truncateUtf8(response.stdout || '', limit,
228
+ `\n\n[Output truncated: stdout exceeded ${limit} bytes. If full payload is required, write it to a file directly.]`);
229
+ const stderrText = `${response.stderr || ''}${task.stderrChunk || ''}`;
230
+ const stderrSuffix = `\n\n[Output truncated: stderr exceeded ${limit} bytes.]`;
231
+ response.stderr = task.stderrTruncated
232
+ ? truncateUtf8(`${stderrText}${stderrSuffix}`, limit, stderrSuffix)
233
+ : truncateUtf8(stderrText, limit, stderrSuffix);
203
234
  task.resolve(response);
204
235
  }
205
236
 
@@ -342,7 +373,8 @@ export class StdioWorkerRunner {
342
373
  action: options.action || 'eval',
343
374
  cwd: options.cwd,
344
375
  code: code || '',
345
- reset: Boolean(options.reset)
376
+ reset: Boolean(options.reset),
377
+ maxOutputBytes: this.maxOutputBytes
346
378
  };
347
379
 
348
380
  return new Promise((resolve, reject) => {
@@ -132,6 +132,37 @@ async function executeInContext(code, context) {
132
132
  return syncScript.runInContext(context, { breakOnSigint: true });
133
133
  }
134
134
 
135
+ function byteLength(value) {
136
+ return Buffer.byteLength(value, 'utf8');
137
+ }
138
+
139
+ function createBoundedCapture(maxBytes, label) {
140
+ const suffix = `\n\n[Output truncated: ${label} exceeded ${maxBytes} bytes.]`;
141
+ let value = '';
142
+ let truncated = false;
143
+ return {
144
+ write(chunk) {
145
+ if (truncated) return;
146
+ const text = typeof chunk === 'string' ? chunk : String(chunk ?? '');
147
+ if (byteLength(value) + byteLength(text) <= maxBytes) {
148
+ value += text;
149
+ return;
150
+ }
151
+ const effectiveSuffix = byteLength(suffix) >= maxBytes ? '\n[Output truncated]' : suffix;
152
+ const suffixBytes = byteLength(effectiveSuffix);
153
+ const available = Math.max(0, maxBytes - byteLength(value) - suffixBytes);
154
+ const bytes = Buffer.from(text, 'utf8');
155
+ let end = Math.min(available, bytes.length);
156
+ while (end > 0 && (bytes[end] & 0xc0) === 0x80) end--;
157
+ value += bytes.subarray(0, end).toString('utf8');
158
+ if (byteLength(value) + suffixBytes <= maxBytes) value += effectiveSuffix;
159
+ else value = Buffer.from(value, 'utf8').subarray(0, maxBytes).toString('utf8');
160
+ truncated = true;
161
+ },
162
+ get value() { return value; }
163
+ };
164
+ }
165
+
135
166
  function sendFrame(payload) {
136
167
  let line;
137
168
  try {
@@ -193,23 +224,18 @@ function main() {
193
224
  return;
194
225
  }
195
226
 
196
- let stdoutBuf = '';
197
- let stderrBuf = '';
227
+ const maxOutputBytes = Number.isInteger(req.maxOutputBytes) && req.maxOutputBytes >= 0
228
+ ? req.maxOutputBytes
229
+ : 8192;
230
+ const stdoutCapture = createBoundedCapture(maxOutputBytes, 'stdout');
231
+ const stderrCapture = createBoundedCapture(maxOutputBytes, 'stderr');
198
232
 
199
- // Intercept console inside context
233
+ // Intercept console inside context with byte-bounded UTF-8 buffers.
200
234
  const customConsole = {
201
- log: (...args) => {
202
- stdoutBuf += util.format(...args) + '\n';
203
- },
204
- info: (...args) => {
205
- stdoutBuf += util.format(...args) + '\n';
206
- },
207
- warn: (...args) => {
208
- stderrBuf += util.format(...args) + '\n';
209
- },
210
- error: (...args) => {
211
- stderrBuf += util.format(...args) + '\n';
212
- }
235
+ log: (...args) => stdoutCapture.write(util.format(...args) + '\n'),
236
+ info: (...args) => stdoutCapture.write(util.format(...args) + '\n'),
237
+ warn: (...args) => stderrCapture.write(util.format(...args) + '\n'),
238
+ error: (...args) => stderrCapture.write(util.format(...args) + '\n')
213
239
  };
214
240
  CURRENT_CONTEXT.console = customConsole;
215
241
 
@@ -236,7 +262,7 @@ function main() {
236
262
  } catch (err) {
237
263
  success = false;
238
264
  errorMsg = err instanceof Error ? err.stack || err.message : String(err);
239
- stderrBuf += errorMsg + '\n';
265
+ stderrCapture.write(errorMsg + '\n');
240
266
  }
241
267
 
242
268
  const elapsedMs = Number((performance.now() - startTime).toFixed(3));
@@ -245,8 +271,8 @@ function main() {
245
271
  sendFrame({
246
272
  id: reqId,
247
273
  success,
248
- stdout: stdoutBuf,
249
- stderr: stderrBuf,
274
+ stdout: stdoutCapture.value,
275
+ stderr: stderrCapture.value,
250
276
  result: success ? result : null,
251
277
  resultRepr: success ? resultRepr : null,
252
278
  error: success ? null : errorMsg,
@@ -101,6 +101,45 @@ def serialize_result(result):
101
101
  # Fall back to string representation
102
102
  return result_repr, result_repr
103
103
 
104
+ class BoundedTextCapture(io.TextIOBase):
105
+ """Capture UTF-8 output without retaining more than the configured byte limit."""
106
+ def __init__(self, max_bytes, label):
107
+ self.max_bytes = max(0, int(max_bytes))
108
+ self.label = label
109
+ self.value = ''
110
+ self.truncated = False
111
+
112
+ def write(self, text):
113
+ if self.truncated:
114
+ return len(text)
115
+ text = str(text)
116
+ candidate = self.value + text
117
+ if len(candidate.encode('utf-8')) <= self.max_bytes:
118
+ self.value = candidate
119
+ return len(text)
120
+ suffix = f"\n\n[Output truncated: {self.label} exceeded {self.max_bytes} bytes.]"
121
+ if len(suffix.encode('utf-8')) >= self.max_bytes:
122
+ suffix = "\n[Output truncated]"
123
+ suffix_bytes = len(suffix.encode('utf-8'))
124
+ available = max(0, self.max_bytes - len(self.value.encode('utf-8')) - suffix_bytes)
125
+ encoded = text.encode('utf-8')[:available]
126
+ while encoded and (encoded[-1] & 0xc0) == 0x80:
127
+ encoded = encoded[:-1]
128
+ self.value += encoded.decode('utf-8', errors='ignore')
129
+ if len((self.value + suffix).encode('utf-8')) <= self.max_bytes:
130
+ self.value += suffix
131
+ else:
132
+ encoded_value = self.value.encode('utf-8')[:self.max_bytes]
133
+ while encoded_value and (encoded_value[-1] & 0xc0) == 0x80:
134
+ encoded_value = encoded_value[:-1]
135
+ self.value = encoded_value.decode('utf-8', errors='ignore')
136
+ self.truncated = True
137
+ return len(text)
138
+
139
+ def getvalue(self):
140
+ return self.value
141
+
142
+
104
143
  def print_user_traceback(ex, stream):
105
144
  """Hide worker frames without discarding user frames or exception metadata."""
106
145
  diagnostic = traceback.TracebackException.from_exception(ex)
@@ -167,8 +206,11 @@ def main():
167
206
  REAL_STDOUT.buffer.flush()
168
207
  continue
169
208
 
170
- captured_stdout = io.StringIO()
171
- captured_stderr = io.StringIO()
209
+ max_output_bytes = req.get("maxOutputBytes", 8192)
210
+ if not isinstance(max_output_bytes, int) or max_output_bytes < 0:
211
+ max_output_bytes = 8192
212
+ captured_stdout = BoundedTextCapture(max_output_bytes, "stdout")
213
+ captured_stderr = BoundedTextCapture(max_output_bytes, "stderr")
172
214
  old_stdout = sys.stdout
173
215
  old_stderr = sys.stderr
174
216
  sys.stdout = captured_stdout