Skip to content

fix(#288): make sub-agent body truncation actually free memory (V8 slice trap + toolUseResult miss)#321

Merged
Juliusolsson05 merged 1 commit into
mainfrom
fix/288-clamp-slice-and-tooluseresult
Jun 21, 2026
Merged

fix(#288): make sub-agent body truncation actually free memory (V8 slice trap + toolUseResult miss)#321
Juliusolsson05 merged 1 commit into
mainfrom
fix/288-clamp-slice-and-tooluseresult

Conversation

@Juliusolsson05

Copy link
Copy Markdown
Owner

Why #320 didn't work

#320 added body truncation to SubAgentWatcher, but a fresh --owners snapshot showed SubAgentWatcher still retaining ~278MB / 89% of the main-process heap — unchanged. Investigation (dominator tree + path-to-root + standalone proofs) found two real bugs, both missed by the agents and the prior review:

Bug 1 — clampString used .slice(), which frees nothing

value.slice(0, cap) returns a V8 SlicedString that retains the entire parent string. So every "truncated" body kept its full original alive behind the slice — truncation freed zero memory. The heap even showed 56MB of Read blobs as sliced string.

Proof:

clamp 2000×200KB via slice   → 194 MB pinned
clamp 2000×200KB via flat-copy → 19 MB

Fix: round-trip the prefix through a Buffer (utf16le, byte-exact) so the result is a fresh flat string and the parent becomes GC-eligible.

Bug 2 — the bulk lives in toolUseResult, which we never truncated

Claude writes the full Read/tool output to the top-level entry.toolUseResult field (toolUseResult.file.content, stdout/stderr) — in addition to the message.content tool_result block. truncateEntryBodies only walked message.content/attachment/content, so the actual megabytes (held by Object.content under toolUseResult.file) sailed through untouched. That's why only 5 truncation markers existed for ~1,952 un-truncated Read blobs.

Fix: replace the hand-maintained field-by-field walk with a single recursive clampDeep(entry, cap, 12) over the whole entry — catches toolUseResult, message.content (string OR array), tool_use.input, attachments, and any future shape. No more whack-a-mole.

Proof: on a realistic entry, toolUseResult.file.content clamped 270,000 → 8,238 chars with type/tool_use_id/filePath all intact.

Safety / invisibility

clampString is a no-op for strings under the 8KB cap, so structural fields (type/uuid/role/name/id/tool_use_id/timestamps) are untouched, and buildSubAgentState only renders ≤80-char headlines — so clamping at 8KB can never change a rendered value. Durable full bodies stay on disk. Net −57 lines (simpler than #320's field list). Typechecks clean.

Verify after merge + rebuild

node --max-old-space-size=12288 scripts/analyze-heapsnapshot.mjs <new-snap> --owners

SubAgentWatcher should fall from ~278MB → single-digit MB.

🤖 Generated with Claude Code

…eal bugs)

#320 truncated sub-agent entry bodies but the heap did not move. A dominator
analysis + path-to-root on a post-#320 snapshot showed SubAgentWatcher still
retaining ~278MB / 89% of the main-process heap. Two defects, both proven:

1) clampString used `value.slice(0, cap)`. In V8 that returns a SlicedString
   that RETAINS the entire parent string, so truncation freed zero memory.
   Proof: clamping 2000×200KB strings via slice left ~194MB pinned; via a
   forced Buffer flat-copy, ~19MB. Fixed clampString to round-trip the prefix
   through a Buffer (utf16le, exact) so the original body becomes GC-eligible.

2) truncateEntryBodies walked only message.content / attachment / content, but
   Claude writes the FULL Read/tool output to the TOP-LEVEL `toolUseResult`
   field (toolUseResult.file.content, stdout/stderr) — where the bytes actually
   lived. The targeted walk never touched it. Replaced the hand-maintained
   field list with a single recursive `clampDeep(entry, cap, 12)` over the
   whole entry, so toolUseResult, message.content (string OR array), tool_use
   input, attachments, and any future shape are all caught. clampString is a
   no-op under the cap, so structural fields (type/id/role/name/tool_use_id/
   timestamps) are untouched and buildSubAgentState's ≤80-char headlines are
   unchanged. Net -57 lines.

Both verified with standalone proofs (slice-frees, toolUseResult clamped
270000→8238 with structure intact). Real before/after needs a fresh snapshot
after rebuild: `analyze-heapsnapshot.mjs <snap> --owners` — SubAgentWatcher
should fall from ~278MB to single-digit MB.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant