@hicaru/pi-rlm 0.1.9 → 0.2.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.
@@ -36,6 +36,36 @@ function promptCapTokensK(maxPromptChars: number): number {
36
36
  return Math.round(maxPromptChars / 4_000);
37
37
  }
38
38
 
39
+ /**
40
+ * Deterministic retrieval over `context` (headless + native).
41
+ *
42
+ * The paper's trajectories retrieve with hand-written regex (App. E.1); frontier models do that
43
+ * well, small ones guess keywords badly, and the first decomposition disproportionately decides
44
+ * the outcome (§5, Fig. 4a). These cost no tokens and no sub-calls.
45
+ */
46
+ const RETRIEVAL_GLOSSARY_LINES: readonly string[] = Object.freeze([
47
+ "- `search(query: str, k=10, path_glob=None)`: BM25 ranking over `context`. Returns",
48
+ " [{path, line, score, snippet}] — POINTERS, not bodies. **Start here.** It is free:",
49
+ " no sub-LLM call, no tokens. Use it before you guess at filenames or write regex.",
50
+ "- `grep_context(pattern, k=50, path_glob=None, before=0, after=0) -> dict`: regex over",
51
+ " `context`. Returns {hits: [{path, line, text}], counts: {path: n}, total, truncated} —",
52
+ " `counts` is complete even when `hits` is capped, so a wide pattern reports its shape",
53
+ " instead of flooding you. Use for exact lexical needles; use `search` for meaning.",
54
+ "- `outline(path) -> str`: definition/heading skeleton of one file with line numbers.",
55
+ " Orient in ~200 chars instead of printing 20K. Matches exact path, then suffix, then glob.",
56
+ ]);
57
+
58
+ /** One-line delegation helpers — orchestrating must be cheaper than solving. */
59
+ const DELEGATION_GLOSSARY_LINES: readonly string[] = Object.freeze([
60
+ "- `map_files(files, prompt, model=None) -> dict[path, str]`: ask `prompt` of every file and",
61
+ " get back {path: answer}. Accepts context entries or paths, packs them into cap-sized",
62
+ " batched sub-calls, and splits oversized files automatically. **This is the default way to",
63
+ " read many files** — prefer it over hand-rolling a chunk loop.",
64
+ "- `llm_map_reduce(items, map_prompt, reduce_prompt, model=None) -> str`: map over items in",
65
+ " one batch, then reduce the partial answers with a single call. The paper's canonical",
66
+ " strategy (query per chunk → aggregate the buffers) as one call.",
67
+ ]);
68
+
39
69
  /** Shared glossary entry for the chunked-query helper (headless + native). */
40
70
  const CHUNKED_GLOSSARY_LINES: readonly string[] = Object.freeze([
41
71
  "- `llm_query_chunked(text: str, prompt: str, model=None) -> list[str]`: auto-splits `text` into",
@@ -69,6 +99,66 @@ const CHUNKED_GLOSSARY_LINE_NATIVE =
69
99
  const LARGE_FILE_RULE_NATIVE =
70
100
  "- Files >1MB or gitignored are NOT in `context`: open() + parse deterministically in Python is fine; ANY semantic reading of the raw text goes through llm_query_chunked. Never print >2K chars raw.";
71
101
 
102
+ /**
103
+ * The decomposition doctrine, ported from the RLM paper's Appendix C.3 `<env_tips>` and
104
+ * retargeted from competition math to repository analysis.
105
+ *
106
+ * This block is the single highest-leverage prompt intervention the paper reports: +69.5% on
107
+ * LongCoT-mini over the same RLM without it (Table 2). Plain RLM prompting alone actually
108
+ * *regressed* two of the five categories; the doctrine is what fixed them. Its purpose is to
109
+ * counter under-delegation — the model doing the work itself in the REPL instead of fanning out.
110
+ *
111
+ * Note the counterweight: `orchestratorAddendum` carries the anti-OVER-recursion batching rule.
112
+ * The paper is explicit (App. B) that one prompt does not port across models and that both
113
+ * guardrails are needed; keep them both.
114
+ */
115
+ const ENV_TIPS = [
116
+ "## Decomposition doctrine",
117
+ "",
118
+ "**Orchestrate; don't solve.** A single chain of thought over a large repository drifts —",
119
+ "you lose partials and compound mistakes. Your sub-LLMs are competent readers: given a",
120
+ "self-contained prompt and the text, they will extract, locate, classify, and summarize",
121
+ "reliably. Trust them; don't do their reading yourself.",
122
+ "",
123
+ "Your job: (1) find the relevant slice with `search` / `grep_context` / `outline`,",
124
+ "(2) delegate all semantic reading to `map_files` / `llm_query_batched` / `llm_map_reduce`,",
125
+ "(3) memoize every result you will reuse in `answers`, (4) sanity-check an answer before",
126
+ "another step depends on it, (5) assemble the final answer from `answers` by lookup.",
127
+ "Your own compute is: pointers, dict lookups, string formatting, and decisions.",
128
+ "",
129
+ "### The only state that matters",
130
+ "`answers` and `plan` are dicts that persist across every turn and survive snapshots.",
131
+ "**If a value isn't in `answers`, it doesn't exist.** Do not trust a number from your own",
132
+ "earlier reasoning or from truncated stdout — context drifts. Memoize everything you reuse.",
133
+ "",
134
+ "### Shape of a run",
135
+ "1. Probe: `print(len(context))`, `search(<the user's question>)`. Do not print file bodies.",
136
+ "2. Plan: write the sub-questions into `plan`; each must be answerable from a named slice.",
137
+ "3. Fan out: one `map_files` / `llm_query_batched` per independent group, not one call per",
138
+ " file. Store results into `answers` keyed by path or sub-question.",
139
+ "4. Assemble: build the answer from `answers`. Delegate the aggregation too if it is large.",
140
+ "",
141
+ "### Red flags — you are off track",
142
+ "- Printing file bodies to read them yourself → stop, delegate to `map_files`.",
143
+ "- Writing regex to *infer meaning* (naming conventions, intent, correctness) → that is a",
144
+ " sub-LLM job. Regex is for exact lexical needles only.",
145
+ "- Two turns in with zero sub-LLM calls on an analysis task → you are solving it yourself.",
146
+ "- About to reuse a value that is not in `answers` → re-derive it and store it.",
147
+ "- One sub-call per file over dozens of files → batch them; fat prompts in small batches win.",
148
+ ].join("\n");
149
+
150
+ /** Native-mode variant of the doctrine — same rules, sized for the native prompt budget. */
151
+ const ENV_TIPS_CONDENSED = [
152
+ "### Decomposition doctrine (paper App. C.3 — worth +69.5% there)",
153
+ "Orchestrate; don't solve. Loop: `search`/`grep_context`/`outline` to find the slice →",
154
+ "`map_files` / `llm_query_batched` to read it → memoize into `answers` → assemble by lookup.",
155
+ "`answers` and `plan` persist across turns and snapshots: **if a value isn't in `answers`, it",
156
+ "doesn't exist** — never reuse a number from your own earlier reasoning or truncated stdout.",
157
+ "Red flags: printing file bodies to read them; regex used to infer meaning rather than match",
158
+ "a literal; two turns into an analysis with zero sub-LLM calls; one sub-call per file instead",
159
+ "of one batch. Exception — AUTHORING is not reading: you write every edit body yourself.",
160
+ ].join("\n");
161
+
72
162
  function howToRunCode(): string {
73
163
  return [
74
164
  "To run Python, write a fenced ```repl``` block. The REPL **persists** across turns. Only",
@@ -100,24 +190,23 @@ function replGlossary(
100
190
  " bodies into your own output.",
101
191
  CONTEXT_EXCLUSION_NOTE,
102
192
  "",
103
- " Chunking example:",
193
+ " Worked example — find the slice, then delegate it:",
104
194
  " ```python",
105
- " chunk_size = 5",
106
- " for i in range(0, len(context), chunk_size):",
107
- " batch = context[i:i+chunk_size]",
108
- " results = llm_query_batched([",
109
- " f\"Analyze {f['path']} ({f['tokens']} tok):\\n{f['content']}\"",
110
- " for f in batch",
111
- " ])",
195
+ ' hits = search("where is the retry/backoff policy configured?", k=8)',
196
+ " paths = sorted({h['path'] for h in hits})",
197
+ ' answers.update(map_files(paths, "Describe any retry/backoff policy in this file, with line numbers. Say NONE if absent."))',
198
+ " print({p: a[:80] for p, a in answers.items()})",
112
199
  " ```",
113
200
  );
114
201
  }
202
+ lines.push(...RETRIEVAL_GLOSSARY_LINES);
115
203
  lines.push(
116
204
  "- `llm_query(prompt: str, model=None) -> str`: a single sub-LLM completion. Use for extraction,",
117
205
  " summarization, or Q&A over a chunk of text.",
118
206
  "- `llm_query_batched(prompts: list[str], model=None) -> list[str]`: run several sub-LLM calls",
119
207
  " concurrently; output order matches input order.",
120
208
  ...CHUNKED_GLOSSARY_LINES,
209
+ ...DELEGATION_GLOSSARY_LINES,
121
210
  );
122
211
  if (askUserQuestion) {
123
212
  lines.push(
@@ -189,6 +278,8 @@ function replGlossary(
189
278
  );
190
279
  }
191
280
  lines.push(
281
+ "- `answers` / `plan`: two dicts that persist across turns and snapshots. Memoize every",
282
+ " verified result in `answers` — see the decomposition doctrine below.",
192
283
  "- `SHOW_VARS() -> str`: list every variable currently in the REPL.",
193
284
  '- `answer`: a dict initialized to {"content": "", "ready": False}. To submit your final answer,',
194
285
  ' set `answer["content"]` to the answer text and `answer["ready"] = True`.',
@@ -244,7 +335,9 @@ export function buildRlmSystemPrompt(meta: PromptMeta, opts: SystemPromptOptions
244
335
  "Start by probing `context` (print a few lines, count items). Then build up an answer to the query.",
245
336
  ];
246
337
  if (opts.orchestrator ?? true) {
247
- parts.push("", orchestratorAddendum(maxPromptChars));
338
+ // Two counterweights, both required (paper App. B): the addendum bounds OVER-recursion
339
+ // (batching/cost), ENV_TIPS bounds UNDER-recursion (solving it yourself).
340
+ parts.push("", orchestratorAddendum(maxPromptChars), "", ENV_TIPS);
248
341
  }
249
342
  if (kind === "files") {
250
343
  parts.push("", LARGE_FILE_RULE_LINES.join("\n"));
@@ -263,61 +356,55 @@ function nativeReplGlossary(): string {
263
356
  "",
264
357
  "### REPL Environment",
265
358
  "- `context`: list[dict] — every file in the repository. Each dict: `path` (str), `content` (str), `tokens` (int).",
359
+ "",
360
+ "Retrieval — free (no sub-LLM call, no tokens). **Start here, before guessing filenames:**",
361
+ "- `search(query, k=10, path_glob=None) -> [{path, line, score, snippet}]` — BM25 over `context`. Returns pointers, not bodies.",
362
+ "- `grep_context(pattern, k=50, path_glob=None, before=0, after=0) -> {hits, counts, total, truncated}` — regex; `counts` stays complete when `hits` is capped. Lexical needles only.",
363
+ "- `outline(path) -> str` — definition/heading skeleton with line numbers. Orient in ~200 chars instead of printing 20K.",
364
+ "",
365
+ "Delegation — everything semantic goes through these:",
366
+ "- `map_files(files, prompt, model=None) -> {path: answer}` — ask `prompt` of many files; batches and splits oversized files for you. **The default way to read many files.**",
367
+ "- `llm_map_reduce(items, map_prompt, reduce_prompt, model=None) -> str` — map in one batch, then reduce with one call.",
266
368
  "- `llm_query(prompt, model=None) -> str` — one-shot sub-LLM. Use for extraction, summarization, Q&A over a chunk.",
267
369
  "- `llm_query_batched(prompts, model=None) -> list[str]` — concurrent sub-LLM calls; output order matches input order.",
268
370
  CHUNKED_GLOSSARY_LINE_NATIVE,
269
371
  "- `rlm_query(prompt, model=None) -> str` — recursive RLM with its own REPL for complex sub-tasks needing iterative reasoning. Prefer llm_query — rlm_query is slower and costlier.",
270
372
  "- `rlm_query_batched(prompts, model=None) -> list[str]` — concurrent recursive RLM calls.",
271
373
  "",
374
+ "",
375
+ "- `answers` / `plan` — dicts persisted across every repl() call and snapshot. Your memo.",
272
376
  "- `todo(action, **kwargs) -> str` — manage a task list. Actions: create, update, list, get, delete, clear. Statuses: pending → in_progress → completed.",
273
377
  "- `load_library(source) -> dict`: append external dir/file/git tree into `context` under `lib/<id>/…`. Return is metadata only — always use `context`.",
274
378
  "- `SHOW_VARS() -> str` — list all variables currently in the REPL.",
275
379
  "- `answer`: dict `{\"content\": \"\", \"ready\": False}`. Setting `answer[\"ready\"] = True` delivers it to the user; do not restate it.",
276
380
  "",
277
- "### Orchestrator Pattern",
278
- "You are an **orchestrator, not a solver**. After probing `context`, decompose the task into sub-LLM / REPL steps,",
279
- "then execute one step at a time, printing samples of each result to verify before moving on.",
381
+ ENV_TIPS_CONDENSED,
280
382
  "",
281
- "Push every long-context operation (reading, summarizing, classifying, answering sub-questions) into",
282
- "`llm_query` / `llm_query_batched` — never dump raw file bodies into your own output. Aggregate small",
283
- "results back in Python. Use Python string operations (`in`, `re.search`) over `context` for quick lookups.",
284
- "",
285
- "### Chunking Strategy",
383
+ "### Worked pattern",
286
384
  "```python",
287
- "chunk_size = 10",
288
- "for i in range(0, len(context), chunk_size):",
289
- " batch = context[i:i+chunk_size]",
290
- " results = llm_query_batched([",
291
- " f\"Analyze {f['path']} ({f['tokens']} tok):\\n{f['content']}\"",
292
- " for f in batch",
293
- " ])",
294
- " # aggregate results into a buffer",
385
+ 'hits = search("where is retry/backoff configured?", k=8)',
386
+ "paths = sorted({h['path'] for h in hits})",
387
+ 'answers.update(map_files(paths, "Describe any retry/backoff policy here, with line numbers. Say NONE if absent."))',
388
+ "print({p: a[:80] for p, a in answers.items()})",
295
389
  "```",
296
- `- Keep sub-prompts under ${DEFAULT_PROMPT_CAP.toLocaleString()} characters (≈${promptCapTokensK(DEFAULT_PROMPT_CAP)}K tokens); batch ~20 prompts per call. Fat prompts in small batches > thousands of tiny prompts.`,
390
+ `- Sub-prompts cap at ${DEFAULT_PROMPT_CAP.toLocaleString()} chars (≈${promptCapTokensK(DEFAULT_PROMPT_CAP)}K tokens); ~20 prompts per batch. Fat prompts in small batches > thousands of tiny prompts.`,
297
391
  "",
298
392
  "### Choosing Between Tools",
299
393
  "| Tool | When |",
300
394
  "|------|------|",
301
- "| `repl({code})` | Need to chunk/delegate `context` to sub-LLMs; need Python scripting; need REPL state across calls |",
302
- "| `zebra-mcp` | Semantic search over the codebase |",
303
- "| `edit` | Change an existing file. Compose oldText/newText yourself; exact match required |",
304
- "| `write` | Create a new file |",
305
- "| `llm_query` (inside repl) | Extract, summarize, or classify a chunk of text |",
306
- "| `rlm_query` (inside repl) | Complex sub-task needing iterative reasoning with its own REPL |",
307
- "| `todo` (inside repl) | Track multi-step progress visibly to the user |",
308
- "",
309
- "### Workflow",
310
- "1. **Plan**: Create todos for the multi-step analysis. Probe `context` — print length, inspect a few entries.",
311
- "2. **Chunk & Delegate**: Slice `context` into batches, delegate each batch to sub-LLMs via `llm_query_batched`.",
312
- "3. **Aggregate**: Collect results in Python, pass aggregated results to a final `llm_query` or produce the answer directly.",
313
- "4. **Finalize**: For file changes, call the native `edit` / `write` tools directly — you",
314
- " author the change, and Pi validates the anchor and renders the diff. For analysis tasks,",
315
- " write a normal message.",
395
+ "| `repl({code})` | ALL repository reading, search, and analysis; Python scripting; state across calls |",
396
+ "| `edit` / `write` | Change or create a file. Compose oldText/newText yourself; exact match required |",
397
+ "| `search` / `grep_context` / `outline` (in repl) | Locate the relevant slice free, do this first |",
398
+ "| `zebra-mcp` | Semantic/embedding search when lexical `search` misses the concept |",
399
+ "| `map_files` / `llm_query_batched` (in repl) | Read/extract/classify that slice |",
400
+ "| `rlm_query` (in repl) | Sub-task needing its own iterative reasoning and REPL |",
401
+ "| `todo` (in repl) | Track multi-step progress visibly to the user |",
316
402
  "",
317
403
  "### Task-Specific Patterns",
318
404
  LARGE_FILE_RULE_NATIVE,
319
- "- Architecture/code review: chunk relevant files and delegate summaries or review to `llm_query_batched`.",
320
- "- Bug investigation: use Python string/regex search over `context`; delegate matching files for analysis.",
405
+ "- Architecture/code review: `search` for the subsystem, then `map_files` the hits.",
406
+ "- Bug investigation: `grep_context` for the literal symbol/message, then `map_files` the matching files.",
407
+ "- Finalizing: for file changes call `edit`/`write` directly so Pi validates the anchor and renders the diff; for analysis, write a normal message.",
321
408
  "- If sub-LLM credits are exhausted, report partial results and stop — do not bypass REPL restrictions.",
322
409
  "",
323
410
  "Reserve your own tokens for high-level decisions: what to ask next, how to combine sub-LLM outputs, when to finalize.",
@@ -336,9 +423,10 @@ export function buildNativeSystemPrompt(): string {
336
423
  "- `read`/`grep` are blocked; bash readers (cat/sed/head/tail/awk/rg) are blocked; bash output is hard-capped at 4K chars.",
337
424
  "- repl() stdout returned to you is hard-capped at 4K chars — printing file bodies is USELESS; the text will not reach you.",
338
425
  "",
339
- "DELEGATION RULE: if a step needs MEANING from more than ~4K chars of text, that reading MUST",
340
- "be an llm_query / llm_query_batched / llm_query_chunked call (rlm_query for iterative",
341
- "sub-tasks). Deterministic Python (search, count, slice, json, re) over `context` is free and",
426
+ "LOCATE-THEN-DELEGATE: `search(query)` / `grep_context(pattern)` / `outline(path)` cost nothing",
427
+ " run them FIRST to find the relevant slice. Then, if a step needs MEANING from more than ~4K",
428
+ "chars, that reading MUST be a map_files / llm_query / llm_query_batched / llm_query_chunked",
429
+ "call (rlm_query for iterative sub-tasks). Deterministic Python over `context` is free and",
342
430
  "preferred for lookups. Semantic reading is always delegated.",
343
431
  "",
344
432
  "All file content is pre-loaded in the REPL `context` variable. Use ONLY `repl({code})`.",
@@ -355,8 +443,10 @@ export function buildNativeSystemPrompt(): string {
355
443
  }
356
444
 
357
445
  /** Soft cap on the static native prompt. Leaves headroom for per-turn context injection
358
- * without bloating the root model's system prompt. Exceeded → phase-guards.ts fails. */
359
- export const NATIVE_PROMPT_BUDGET = 6_000;
446
+ * without bloating the root model's system prompt. Exceeded → phase-guards.ts fails.
447
+ * Raised from 6K when the retrieval glossary and the condensed decomposition doctrine
448
+ * landed; both buy far more than they cost (paper Table 2, Fig. 4a). */
449
+ export const NATIVE_PROMPT_BUDGET = 7_500;
360
450
 
361
451
  /** Exported for tests — prompt length without context metadata (which is injected separately). */
362
452
  export const NATIVE_PROMPT_STATIC = buildNativeSystemPrompt();
@@ -365,10 +455,11 @@ export const NATIVE_PROMPT_STATIC = buildNativeSystemPrompt();
365
455
  export const NATIVE_TURN_REMINDER = [
366
456
  "[RLM orchestrator contract — enforced by the runtime, not optional:",
367
457
  "repl() stdout to you is hard-capped at 4K chars; read/grep and bash readers are blocked.",
368
- "Any SEMANTIC reading of file/text content MUST go through llm_query / llm_query_batched /",
369
- "llm_query_chunked (rlm_query for iterative sub-tasks). Deterministic Python (search, count,",
370
- "slice, json) is free. AUTHORING IS NOT READING: you write every edit body yourself and apply",
371
- "it with the native `edit` / `write` tools never delegate code you will ship to a sub-LLM.",
458
+ "LOCATE FIRST with search() / grep_context() / outline() they cost nothing. Any SEMANTIC",
459
+ "reading MUST then go through map_files / llm_query / llm_query_batched / llm_query_chunked",
460
+ "(rlm_query for iterative sub-tasks). Memoize what you reuse in `answers`; a value not in",
461
+ "`answers` does not exist. AUTHORING IS NOT READING: you write every edit body yourself and",
462
+ "apply it with the native `edit` / `write` tools — never delegate code you will ship.",
372
463
  "Keep your own output to decisions, authored edits, and aggregation.]",
373
464
  ].join("\n");
374
465
 
@@ -7,12 +7,8 @@ export function buildTurnPrompt(
7
7
  iteration: number,
8
8
  maxIterations: number,
9
9
  gateMessage?: string,
10
- phaseGuidanceText?: string,
11
10
  ): string {
12
- const parts: string[] = [];
13
- if (phaseGuidanceText) parts.push(phaseGuidanceText);
14
- if (gateMessage) parts.push(gateMessage);
15
- const prefix = parts.length > 0 ? `${parts.join("\n\n")}\n\n` : "";
11
+ const prefix = gateMessage ? `${gateMessage}\n\n` : "";
16
12
  const body = `Turn ${iteration + 1}/${maxIterations}:`;
17
13
  if (iteration === 0) {
18
14
  return (
@@ -52,9 +52,6 @@ export interface WorkerResponse {
52
52
  readonly var_names?: readonly string[];
53
53
  // load_context:
54
54
  readonly index?: number;
55
- // snapshot/restore:
56
- readonly skipped?: readonly string[];
57
- readonly restored?: readonly string[];
58
55
  }
59
56
 
60
57
  /** Kinds of sub-LLM interrupt the worker can raise mid-exec. */
@@ -88,10 +85,6 @@ export interface AskAnswer {
88
85
  readonly custom?: string;
89
86
  }
90
87
 
91
- export interface AskUserQuestionReply {
92
- readonly answers: readonly AskAnswer[];
93
- }
94
-
95
88
  interface InterruptBase {
96
89
  readonly rid: string;
97
90
  readonly depth: number;
@@ -81,17 +81,17 @@ export class SandboxManager {
81
81
  }
82
82
 
83
83
  /**
84
- * Execute code in the sandbox. Serializes concurrent calls via a promise queue
85
- * (second call waits for first to complete, no interleaving). On failure,
86
- * nullifies the sandbox so the next call recreates it (death-recreate).
84
+ * Execute code in the sandbox with no per-invocation setup. Serializes concurrent calls via
85
+ * a promise queue (second call waits for the first, no interleaving). On failure the sandbox
86
+ * is nullified so the next call recreates it (death-recreate).
87
87
  */
88
88
  async exec(code: string): Promise<ReplResult> {
89
89
  return this.execQueued(code);
90
90
  }
91
91
 
92
92
  /**
93
- * Execute code after running setup inside the serialized execution slot.
94
- * Use this for per-invocation handler state that must match the active REPL run.
93
+ * Execute code after running `setup` inside the serialized execution slot, so per-invocation
94
+ * handler state (emitter, limits, depth) always matches the active REPL run.
95
95
  */
96
96
  async execWithSetup(code: string, setup: () => void): Promise<ReplResult> {
97
97
  return this.execQueued(code, setup);
@@ -24,7 +24,7 @@ import {
24
24
  type WorkerRequest,
25
25
  type WorkerResponse,
26
26
  } from "./protocol.ts";
27
- import { formatError } from "../util/errors.ts";
27
+ import { errorMessage, formatError } from "../util/errors.ts";
28
28
 
29
29
  /** Result of a host-side library pack requested by `load_library`. */
30
30
  export interface LibraryLoadResult {
@@ -76,6 +76,7 @@ export interface SandboxOptions {
76
76
  }
77
77
 
78
78
  const WORKER_PATH = join(dirname(fileURLToPath(import.meta.url)), "worker.py");
79
+ const STDERR_TAIL_CHARS = 8_192;
79
80
  const TODO_PROTO_KEYS = new Set(["type", "rid", "depth", "action"]);
80
81
 
81
82
  // The sandbox runs untrusted model-authored code; it must never inherit provider secrets.
@@ -124,7 +125,23 @@ export class PythonSandbox {
124
125
  private readonly handlers: SubLlmHandlers;
125
126
  private readonly requestTimeoutMs: number;
126
127
  private readonly initTimeoutMs: number;
127
- private stderr = "";
128
+ /** Bounded stderr tail (chunks, newest last) — avoids rebuilding the buffer per chunk. */
129
+ private readonly stderrTail: string[] = [];
130
+ private stderrLen = 0;
131
+
132
+ /** Bounded tail of everything written to stderr, oldest chunks already dropped. */
133
+ private get stderr(): string {
134
+ return this.stderrTail.join("");
135
+ }
136
+
137
+ /** Record a diagnostic on the same bounded tail as real worker stderr. */
138
+ private appendStderr(text: string): void {
139
+ this.stderrTail.push(text);
140
+ this.stderrLen += text.length;
141
+ while (this.stderrLen > STDERR_TAIL_CHARS && this.stderrTail.length > 1) {
142
+ this.stderrLen -= (this.stderrTail.shift() ?? "").length;
143
+ }
144
+ }
128
145
  private disposed = false;
129
146
  private ready: Promise<void>;
130
147
 
@@ -153,9 +170,7 @@ export class PythonSandbox {
153
170
  this.proc.stdout.setEncoding("utf8");
154
171
  this.proc.stdout.on("data", (chunk: string) => this.onData(chunk));
155
172
  this.proc.stderr.setEncoding("utf8");
156
- this.proc.stderr.on("data", (chunk: string) => {
157
- this.stderr = (this.stderr + chunk).slice(-8192);
158
- });
173
+ this.proc.stderr.on("data", (chunk: string) => this.appendStderr(chunk));
159
174
  this.proc.on("error", (err: NodeJS.ErrnoException) => {
160
175
  const hint = err.code === "ENOENT" ? ` ('${python}' not found — is Python installed and on PATH?)` : "";
161
176
  this.failAll(new Error(`failed to start sandbox${hint}: ${err.message}`));
@@ -189,12 +204,12 @@ export class PythonSandbox {
189
204
  return sandbox;
190
205
  }
191
206
 
192
- async loadContext(payload: unknown, index?: number): Promise<number> {
207
+ async loadContext(payload: unknown): Promise<number> {
193
208
  const isJson = typeof payload !== "string";
194
209
  let path: string | undefined;
195
210
  try {
196
211
  path = await this.writeContextFile(payload, isJson);
197
- const res = await this.request({ type: "load_context", path, index, json: isJson });
212
+ const res = await this.request({ type: "load_context", path, json: isJson });
198
213
  if (!res.ok) throw new Error(res.error ?? "load_context failed");
199
214
  return res.index ?? 0;
200
215
  } finally {
@@ -308,15 +323,6 @@ export class PythonSandbox {
308
323
  }
309
324
  }
310
325
 
311
- /**
312
- * Refresh the parent-side request watchdog for every pending request.
313
- * Used during long mid-exec work that does not
314
- * produce additional worker interrupts on this sandbox.
315
- */
316
- refreshWatchdog(): void {
317
- this.touchPending();
318
- }
319
-
320
326
  private send(msg: ParentMessage): void {
321
327
  this.proc.stdin.write(`${JSON.stringify(msg)}\n`);
322
328
  }
@@ -331,11 +337,11 @@ export class PythonSandbox {
331
337
  try {
332
338
  const message = JSON.parse(line) as unknown;
333
339
  if (isWorkerMessage(message)) this.dispatch(message);
334
- else this.stderr = `${this.stderr}\n[protocol] skipped invalid stdout message: ${line.slice(0, 200)}`.slice(-8192);
340
+ else this.appendStderr(`\n[protocol] skipped invalid stdout message: ${line.slice(0, 200)}`);
335
341
  } catch {
336
342
  // Non-JSON line on the protocol stream — likely a subprocess writing to fd 1.
337
343
  // Skip it so a rogue write doesn't kill the pump, but retain a breadcrumb for watchdog errors.
338
- this.stderr = `${this.stderr}\n[protocol] skipped non-JSON stdout line: ${line.slice(0, 200)}`.slice(-8192);
344
+ this.appendStderr(`\n[protocol] skipped non-JSON stdout line: ${line.slice(0, 200)}`);
339
345
  }
340
346
  }
341
347
  }
@@ -419,7 +425,7 @@ export class PythonSandbox {
419
425
  }
420
426
  }
421
427
  } catch (err) {
422
- this.reply(msg.rid, { error: err instanceof Error ? err.message : String(err) });
428
+ this.reply(msg.rid, { error: errorMessage(err) });
423
429
  }
424
430
  }
425
431