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 — feat(bash): stream large output to tmpfile with filtering and GC by cgwalters · Pull Request #8953 · anomalyco/opencode

Conversation

@cgwalters

@cgwalters cgwalters commented Jan 16, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

What does this PR do?

Stream command output directly to a temp file when it exceeds 50KB,
avoiding memory bloat for commands with huge output. Inspired by
aaif-goose/goose#2817.

New feature:

  • output_filter param: pass a regex (e.g. "^(warning|error):") to
    capture only matching lines inline while full output goes to file

The agent is informed that Grep and Read can be used to analyze the
temp files.

UI now shows filtered output stats (e.g. "Filtered: 7 matches from
2.1 KB (1.9 KB omitted)") so users can see how much output was
filtered and the total size.

Assisted-by: OpenCode (Opus 4.5)

How did you verify your code works?

Unit tests, and also by trying it out directly. (Though I only lightly tested it interactively)

Sorry, something went wrong.

Uh oh!

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

cbaxley reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@github-actions github-actions Bot added the needs:issue label Jan 16, 2026
@github-actions

github-actions Bot commented Jan 16, 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 commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

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

Potential Duplicate Found:

PR #6048 - "feat('bash'): Bash tool output saves the stdout/stderr to a file if it exceeds the limit"
#6048

This PR appears to be directly related to the current PR #8953. Both address:

  • Saving command output to a file when it exceeds a size limit
  • Preventing memory bloat from large command outputs
  • File-based output streaming

There's also a tangentially related PR:

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

This PR addresses memory management in tool output, which relates to the memory optimization goal of the current PR.

The most likely duplicate is PR #6048, which implements very similar functionality. You may want to check if it was closed, merged, or if the current PR #8953 supersedes it with additional features (like the output_filter parameter and improved GC strategy).

All reactions

Sorry, something went wrong.

Uh oh!

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

@cgwalters cgwalters mentioned this pull request Jan 16, 2026
feat('bash'): Bash tool output saves the stdout/stderr to a file if it exceeds the limit. #6048
Closed
@github-actions github-actions Bot mentioned this pull request Jan 16, 2026
fix(desktop): Stream bash output + strip-asni #8961
Merged
@cgwalters
cgwalters marked this pull request as draft January 16, 2026 20:53
@cgwalters
cgwalters force-pushed the bash-auto-redirect branch from 3f26c22 to 59b6d76 Compare January 16, 2026 21:01
@cgwalters
cgwalters marked this pull request as ready for review January 16, 2026 21:02
@cgwalters
cgwalters force-pushed the bash-auto-redirect branch from 59b6d76 to 0967da8 Compare January 16, 2026 21:26
@github-actions github-actions Bot mentioned this pull request Jan 20, 2026
fix: Prevent O(n²) memory usage in command output #9693
Closed
@cgwalters
refactor: move formatSize to shared utility
f96d269
Assisted-by: OpenCode (Claude Sonnet 4)
@cgwalters
cgwalters force-pushed the bash-auto-redirect branch from 0967da8 to 5c8edb9 Compare January 23, 2026 14:01
cgwalters added 2 commits January 23, 2026 09:12
@cgwalters
fix(bash): avoid O(n²) memory growth for large command output
f5c7f2c
Previously, output was accumulated via `output += chunk.toString()` which
creates a new string for every chunk, causing O(n²) memory usage. For
commands producing megabytes of output, this caused catastrophic memory
consumption.

Add StreamingOutput class that:
- Accumulates output in memory up to 50KB threshold
- Spills to a temp file when threshold exceeded
- Tracks bytes incrementally to avoid scanning

Fixes memory exhaustion on commands like `find /` or large builds.

Assisted-by: OpenCode (Claude Sonnet 4)
@cgwalters
feat(bash): add output_filter parameter for build command filtering
667ed6b
Add output_filter parameter that captures regex-matching lines inline
while streaming full output to file. Useful for build commands where only
warnings/errors matter.

When a filter is provided:
- Full output still streams to temp file (for later inspection)
- Only matching lines are returned inline to the LLM
- Filter stats (matchCount, filteredBytes) are tracked incrementally

Example usage:
  output_filter: "^(warning|error|WARN|ERROR):"

This avoids the LLM having to grep through large build outputs to find
the relevant diagnostics.

Assisted-by: OpenCode (Claude Sonnet 4)
@cgwalters
cgwalters force-pushed the bash-auto-redirect branch from 5c8edb9 to 667ed6b Compare January 23, 2026 14:14
@cgwalters

cgwalters commented Jan 23, 2026

Copy link
Copy Markdown
Author

I'll be very clear in this that I have not written much Typescript and this was basically 100% output from Opus 4.5, but I did (as I do with all code targeted for important projects) review it at least superficially and I tested this a good bit.

I've now also rolled in the changes from #9693 but this ensures that the O(n^2) problem is fixed for both the inline ! verb and the bash tool.

There's a separate commit for the filtering which is more of a feature than a bugfix - and I think this is somewhat novel in that while many tools similar to OpenCode offer a bash/shell tool, this "feature" one is one I only tried to add to goose.

One thing I notice related to this though is at least Opus 4.5 habitually does e.g. make | tail -100 and such - it's been trained to avoid large output, but IMO this filtering functionality is a lot better because it ensures it can still access all of the data if it needs to.

All reactions

Sorry, something went wrong.

Uh oh!

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

@cgwalters

cgwalters commented Jan 23, 2026

Copy link
Copy Markdown
Author

While it's not new, I really dislike how opencode writes command output into $HOME by default...personally what I think would be better at least on Unix derivatives is having unlinked (i.e. anonymous) tempfiles so they reliably get GC'd when the process exits among other things. We'd just need to teach e.g. the grep tool to operate on an in-memory file descriptor too.

All reactions

Sorry, something went wrong.

Uh oh!

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

@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
tda1017 pushed a commit to tda1017/opencode that referenced this pull request Feb 28, 2026
@cgwalters root
fix: stream large bash output to tmpfile to prevent O(n²) memory growth
2e0f232
Stream command output directly to a temp file when it exceeds threshold,
avoiding memory bloat for commands with huge output. Adds output_filter
param for regex-based line filtering.

Original work from PR anomalyco#8953 by @cgwalters
Co-authored-by: Colin Walters <[email protected]>
@tda1017 tda1017 mentioned this pull request Feb 28, 2026
fix: consolidate memory leak fixes from #14650 and #8953 #15435
Closed
3 of 6 tasks
@tda1017

tda1017 commented Feb 28, 2026

Copy link
Copy Markdown

Hi @cgwalters, I've been experiencing memory issues with large command outputs and found your streaming fix here. Since this PR has been open for a while, I created a consolidated PR (#15435) that includes your work along with @kryptobaseddev's memory leak fixes from #14650.

I've properly attributed your work with Co-authored-by tags and explicit credit in the PR description. Hope this helps get these important fixes merged faster. Thank you for the great solution!

All reactions

Sorry, something went wrong.

Uh oh!

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

@tda1017 tda1017 mentioned this pull request Feb 28, 2026
fix: resolve memory leak issues across multiple subsystems #14650
Closed
3 tasks done
@github-actions github-actions Bot mentioned this pull request Mar 2, 2026
fix: prevent memory leaks from SSE streams, LSP, Bus, and process cleanup #15646
Closed
3 of 6 tasks
@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 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 30, 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 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

needs:issue

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@cgwalters @tda1017
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.