Skip to content

fix(core): clear tool output and attachments on compaction prune#14693

Closed
bil9148 wants to merge 1 commit intoanomalyco:devfrom
bil9148:fix/clear-compacted-output
Closed

fix(core): clear tool output and attachments on compaction prune#14693
bil9148 wants to merge 1 commit intoanomalyco:devfrom
bil9148:fix/clear-compacted-output

Conversation

@bil9148
Copy link

@bil9148 bil9148 commented Feb 22, 2026

Issue for this PR

Closes #5700, #3013

Related: #9385, #9151, #7046, #5363, #13796, #12255, #10248, #9156, #9140, #10913

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

SessionCompaction.prune() marks old tool outputs as compacted by setting part.state.time.compacted = Date.now(), but it never clears the actual output string or attachments array from the part. Since toModelMessages already 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 output and attachments on 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>/status and /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 toModelMessages already guards compacted parts at message-v2.ts:620-621 — it checks part.state.time.compacted and substitutes "[Old tool result content cleared]" regardless of what's in output, so clearing the field has no functional impact on the messages sent to the API.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

I found potentially related PRs:

⚠️ Potential Duplicates:

  1. PR fix: clear tool output and attachments when pruning to prevent memory leak #7049: "fix: clear tool output and attachments when pruning to prevent memory leak"

  2. PR fix: resolve memory leak issues across multiple subsystems #14650: "fix: resolve memory leak issues across multiple subsystems"

Recommendation: Check PR #7049 first, as it appears to be directly addressing the same bug fix. It may have already been merged or closed, making PR #14693 potentially redundant.

@bil9148
Copy link
Author

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.

@bil9148 bil9148 closed this Feb 22, 2026
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.

Too high memory usage

1 participant