fix(core): clear tool output and attachments on compaction prune#14693
Closed
bil9148 wants to merge 1 commit intoanomalyco:devfrom
Closed
fix(core): clear tool output and attachments on compaction prune#14693bil9148 wants to merge 1 commit intoanomalyco:devfrom
bil9148 wants to merge 1 commit intoanomalyco:devfrom
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.
Contributor
|
The following comment was made by an LLM, it may be inaccurate: I found potentially related PRs:
|
Author
|
Closing in favor of #7049, which addresses the same root cause (compacted tool outputs and attachments not being cleared during pruning) and includes tests. |
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.
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