GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: clear tool output and attachments when pruning to prevent memory leak by hendem · Pull Request #7049 · anomalyco/opencode

Conversation

@hendem

@hendem hendem commented Jan 6, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Fixes #3013

Summary

Clear tool output and attachments when compaction prunes old tool results, actually freeing memory instead of just flagging.

Evidence

In a real session working on this repo:

Metric Value
OpenCode process RAM 16.4 GB
Tool parts on disk 34,887 files
Session duration ~3 hours

Sample tool output sizes from this session:

  • webfetch of docs page: 10 KB
  • read of source file: 5-50 KB
  • git diff / gh pr diff: 10-100 KB
  • bash command outputs: 1-50 KB

All of these outputs stay in memory even after compaction marks them "old".

The Problem

In SessionCompaction.prune(), when tool outputs are pruned:

// BEFORE: Only sets a flag, output data stays in memory
part.state.time.compacted = Date.now()
await Session.updatePart(part)

The compacted timestamp is used by toModelMessage() to replace output with placeholder text like "(Old tool result content cleared)" - but the actual data never gets freed.

Over a long session:

  • 34,887 tool calls × average output size = hundreds of MB to GBs retained
  • Memory never decreases, even after compaction runs
  • Eventually Bun runs out of memory and crashes

The Fix

// AFTER: Actually clear the data
part.state.time.compacted = Date.now()
part.state.output = ""              // Free the output string
part.state.attachments = undefined  // Free any attachments
await Session.updatePart(part)

Now when compaction runs, the memory is actually freed. The placeholder text is already shown to the LLM (that logic exists), we just were not clearing the source data.

Testing

Existing compaction tests pass. Added test verifying output/attachments are cleared after prune.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Copilot AI review requested due to automatic review settings January 6, 2026 05:49
Copilot started reviewing on behalf of hendem January 6, 2026 05:49
View session
@github-actions

github-actions Bot commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

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

No duplicate PRs found

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Copilot AI reviewed Jan 6, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a memory leak where compacted tool outputs and their attachments were retained indefinitely in storage. When the pruning mechanism marks old tool outputs as compacted, the fix now clears both the output string and attachments array to free memory.

Key changes:

  • Clears tool output and attachments during compaction to prevent unbounded memory growth
  • Adds comprehensive test coverage for the pruning behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
packages/opencode/src/session/compaction.ts Sets output to empty string and attachments to undefined when marking tool parts as compacted, with explanatory comment
packages/opencode/test/session/compaction.test.ts Adds two new test cases: one verifying output/attachments are cleared during pruning, and one testing that pruning respects the disabled configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/opencode/test/session/compaction.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/opencode/test/session/compaction.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/opencode/test/session/compaction.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/opencode/test/session/compaction.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/opencode/test/session/compaction.test.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hendem

hendem commented Jan 6, 2026

Copy link
Copy Markdown
Author

Addressed review comment about test not verifying behavior when pruning is disabled (commit 488173440).

The test now:

  1. Creates a tool part with large output (200,000 chars) and an attachment (similar to the first test)
  2. Creates additional user messages to get past turn protection
  3. Calls prune() with pruning disabled via config
  4. Verifies that:
    • output.length remains 200,000 (unchanged)
    • attachments.length remains 1 (unchanged)
    • time.compacted is undefined (not set)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hendem hendem mentioned this pull request Jan 7, 2026
fix: clean up pending permissions when session is deleted #7154
Closed
@hendem
hendem requested a review from Copilot January 7, 2026 03:54
Copilot started reviewing on behalf of hendem January 7, 2026 03:55
View session
Copilot AI reviewed Jan 7, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@kevoconnell

kevoconnell commented Jan 7, 2026

Copy link
Copy Markdown

this would be really helpful

hendem reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jan 7, 2026
feat: implement smart pruning #7215
Closed
@hendem
hendem requested a review from Copilot January 7, 2026 15:45
Copilot started reviewing on behalf of hendem January 7, 2026 15:46
View session
Copilot AI reviewed Jan 7, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the needs:issue label Jan 9, 2026
@github-actions

github-actions Bot commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot removed the needs:issue label Jan 9, 2026
@hendem hendem mentioned this pull request Jan 9, 2026
fix: force kill MCP server processes on dispose to prevent orphan processes #7424
Closed
@fwang
fwang force-pushed the dev branch from 4da2998 to a618fbe Compare January 9, 2026 06:32
@fwang
fwang requested a review from adamdotdevin as a code owner January 9, 2026 06:32
Mark Henderson and others added 7 commits January 10, 2026 14:14
Mark Henderson @fwang
fix: clear tool output and attachments when pruning to prevent memory…
1b92c8f
… leak

When compaction prunes old tool outputs, only the compacted timestamp
was being set - the actual output string and attachments array remained
in storage indefinitely. This caused storage files to grow unbounded
with large tool outputs (file contents, base64 images/PDFs, etc.).

Now prune() clears both output and attachments when marking parts as
compacted. The toModelMessage function already replaces compacted
outputs with placeholder text, so this is safe.

Fixes part of anomalyco#4315
Mark Henderson @fwang
address PR review comments: remove unused variables and add assertion…
718df08
…s to pruning disabled test
@fwang
wip: zen
b302472
@fwang
Revert "wip: zen"
1b3aec7
This reverts commit b302472.
@fwang
wip: black
62d4b51
@fwang
wip: black
d6fca76
@actions-user @fwang
chore: generate
141c413
@fwang
fwang force-pushed the fix/memory-leak-compaction-cleanup branch from 9d089b6 to 141c413 Compare January 10, 2026 19:15
hendem added 2 commits January 11, 2026 00:51
@hendem
Merge branch 'dev' into fix/memory-leak-compaction-cleanup
9f0287b
@hendem
Merge branch 'dev' into fix/memory-leak-compaction-cleanup
4cbc49a
@github-actions github-actions Bot mentioned this pull request Jan 16, 2026
feat(bash): stream large output to tmpfile with filtering and GC #8953
Closed
@Th0rgal

Th0rgal commented Jan 17, 2026

Copy link
Copy Markdown

Server Mode Diagnostic Data

Running OpenCode v1.1.23 as a systemd daemon (opencode serve) on a 64GB Linux server. Here is data that may help with this PR.

Memory Growth (from systemd logs)

Runtime Memory Peak Swap
2.5h 263 MB 0
4.5h 699 MB 0
4.75h 56 GB 8.8 GB
2.25h 20.9 GB 0

Memory grows over time and eventually causes the process to become unresponsive.

Storage Accumulation

storage/part/     141MB (10,820 directories)
storage/message/   19MB (728 directories)
tool-output/       21MB (15 files)

The 10,820 part directories correlate with the unfree-d tool outputs described in this PR.

Timeout Pattern

When memory gets high, HTTP requests to /session/{id}/message stop responding. Downstream clients see 10-minute timeouts:

17:22:29 WARN Retry attempt=2 session=ses_xxx
17:32:30 WARN Retry attempt=3 (10 min gap = timeout)
17:42:31 ERROR elapsed_secs=1801 Failed after retries

Environment

  • OpenCode v1.1.23, server mode (opencode serve --port 4096)
  • Ubuntu Linux, systemd service
  • Multiple concurrent sessions over extended periods
  • Heavy tool usage (grep, read, bash)

Happy to provide more data if useful.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jan 20, 2026
fix: Prevent O(n²) memory usage in command output #9693
Closed
@thdxr
thdxr force-pushed the dev branch from cbab81f to 2d3c7a0 Compare January 30, 2026 04:50
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 00637c0 to 71e0ba2 Compare January 30, 2026 14:32
@thdxr
thdxr force-pushed the dev branch 3 times, most recently from f1ae801 to 08fa7f7 Compare January 30, 2026 14:37
@github-actions github-actions Bot mentioned this pull request Feb 13, 2026
fix: resolve multiple memory leaks causing unbounded RAM growth ( Now passing: && Fixed failing tests for windows ) #13514
Closed
@github-actions github-actions Bot mentioned this pull request Feb 22, 2026
fix(core): clear tool output and attachments on compaction prune #14693
Closed
3 of 6 tasks
@kryptobaseddev

kryptobaseddev commented Feb 23, 2026

Copy link
Copy Markdown

can the PR be reviewed along with this one, it was fixing similar issues from a different persepctive: #14650

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Feb 28, 2026
fix: consolidate memory leak fixes from #14650 and #8953 #15435
Closed
3 of 6 tasks
@guazi04 guazi04 mentioned this pull request Mar 5, 2026
[FEATURE]: Session Lifecycle Management — unified storage reclamation, auto-archival, and child session cleanup #16101
Closed
1 task done
@github-actions github-actions Bot mentioned this pull request Mar 5, 2026
fix: clear tool output and attachments during compaction prune #16162
Closed
vbuccigrossi pushed a commit to vbuccigrossi/opencode that referenced this pull request Mar 7, 2026
EBrown @claude
feat: port remaining Tier 1+2 upstream fixes and features
63e7373
Tier 1 bug fixes:
- Fix O(n²) bash output concatenation with StreamingOutput class (anomalyco#9693)
- Fix memory leaks in Bus.once, Format, Plugin, ShareNext, Bootstrap (anomalyco#13514)
- Fix FileTime race condition using actual file mtime instead of JS clock
- Free memory on compaction prune: clear output/attachments/metadata (anomalyco#7049)
- Throttle reasoning-delta storage writes to 50ms intervals (anomalyco#11328)
- Handle SIGHUP/SIGTERM to prevent orphaned processes (anomalyco#12718)
- Add process.once("close") handler for bash tool reliability

Tier 2 features:
- Support 1M context window for Anthropic models via beta header (anomalyco#14375)
- Input-only token counting for compaction with limit.input models
- MCP lazy loading: on-demand tool discovery via mcp_search tool (anomalyco#8771)
- MCP servers listed in system prompt when lazy mode enabled
- StreamingOutput: output_filter regex for build diagnostics
- LSP server cleanup callback for temp directory removal
- Extract formatSize utility from uninstall to shared util/format
- GitHub CI: fix Bus subscription leak in session event handler

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@binarydoubling binarydoubling mentioned this pull request Mar 9, 2026
Multiple memory leaks cause unbounded RAM growth during extended TUI usage #16697
Closed
@github-actions github-actions Bot mentioned this pull request Mar 9, 2026
fix(session): clear compacted tool output #16785
Closed
3 of 6 tasks
@binarydoubling binarydoubling mentioned this pull request Mar 9, 2026
fix: resolve multiple memory leaks causing unbounded growth #16695
Closed
3 of 6 tasks
@github-actions github-actions Bot mentioned this pull request Apr 7, 2026
fix: bash tool memory leak #21321
Closed
3 tasks done
@opencode-agent
opencode-agent Bot force-pushed the dev branch from 2f2ed11 to 9f201d6 Compare April 16, 2026 21:55
@github-actions

github-actions Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot closed this Apr 25, 2026
@ChangeHow ChangeHow mentioned this pull request Sep 25, 2026
serve: RSS grows with activity and remains elevated while idle #51340
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@adamdotdevin adamdotdevin Awaiting requested review from adamdotdevin

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uses a huge amount of memory

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@hendem @kevoconnell @Th0rgal @kryptobaseddev @fwang @actions-user
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.