fix(#288): make sub-agent body truncation actually free memory (V8 slice trap + toolUseResult miss)#321
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why #320 didn't work
#320 added body truncation to
SubAgentWatcher, but a fresh--ownerssnapshot showedSubAgentWatcherstill 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 —
clampStringused.slice(), which frees nothingvalue.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 assliced string.Proof:
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 truncatedClaude writes the full Read/tool output to the top-level
entry.toolUseResultfield (toolUseResult.file.content,stdout/stderr) — in addition to themessage.contenttool_result block.truncateEntryBodiesonly walkedmessage.content/attachment/content, so the actual megabytes (held byObject.contentundertoolUseResult.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 — catchestoolUseResult,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.contentclamped 270,000 → 8,238 chars withtype/tool_use_id/filePathall intact.Safety / invisibility
clampStringis a no-op for strings under the 8KB cap, so structural fields (type/uuid/role/name/id/tool_use_id/timestamps) are untouched, andbuildSubAgentStateonly 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
SubAgentWatchershould fall from ~278MB → single-digit MB.🤖 Generated with Claude Code