Uh oh!
There was an error while loading. Please reload this page.
fix(core): clear tool output and attachments on compaction prune - #14693
fix(core): clear tool output and attachments on compaction prune#14693bil9148 wants to merge 1 commit into
Conversation
prune() sets time.compacted but leaves the full output string and attachments in the part. Since toModelMessages already replaces compacted output with '[Old tool result content cleared]', the stored data is never used after compaction but still gets loaded from disk into memory on every prompt loop iteration. Clear both fields when pruning so they are not reloaded on subsequent iterations, reducing memory pressure in long sessions.
The following comment was made by an LLM, it may be inaccurate: I found potentially related PRs:
|
bil9148
commented
Feb 22, 2026
Closing in favor of #7049, which addresses the same root cause (compacted tool outputs and attachments not being cleared during pruning) and includes tests. |
Issue for this PR
Closes#5700, #3013
Related: #9385, #9151, #7046, #5363, #13796, #12255, #10248, #9156, #9140, #10913
Type of change
What does this PR do?
SessionCompaction.prune()marks old tool outputs as compacted by settingpart.state.time.compacted = Date.now(), but it never clears the actualoutputstring orattachmentsarray from the part. SincetoModelMessagesalready replaces compacted output with"[Old tool result content cleared]"(message-v2.ts:620), the stored data is dead weight after compaction.The problem is that every prompt loop iteration reloads all parts from storage into memory. In a long session with many tool calls (file reads, grep results, bash output), these compacted-but-still-full parts accumulate significant data that gets loaded into RAM repeatedly for no reason.
This clears both
outputandattachmentson the part when pruning, so subsequent reloads don't bring dead data back into memory.How did you verify your code works?
I monitored RSS memory of the bun process over multiple long sessions using
/proc/<pid>/statusand/proc/<pid>/smaps. Before the fix, a session would climb to 14-22GB RSS over the course of extended use. After the fix, memory growth is noticeably slower because compacted parts no longer contribute to the reload size.Also verified that
toModelMessagesalready guards compacted parts at message-v2.ts:620-621 — it checkspart.state.time.compactedand substitutes"[Old tool result content cleared]"regardless of what's inoutput, so clearing the field has no functional impact on the messages sent to the API.Checklist