Skip to content

fix(opencode): batch shell output updates - #37653

Closed
flowluap wants to merge 1 commit into
anomalyco:devfrom
flowluap:shell-output-batching
Closed

flowluap wants to merge 1 commit into
anomalyco:devfrom
flowluap:shell-output-batching

Conversation

@flowluap

@flowluap flowluap commented Jul 18, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #30001

Related to #35543. This PR also coalesces metadata updates and uses a bounded trailing-output drain.

Type of change

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

What does this PR do?

The shell tool currently awaits ctx.metadata for every decoded output chunk. For chatty commands, pipe chunking therefore controls durable update frequency, and process exit can win before trailing pipe output is consumed.

This change consumes output immediately, publishes the newest cumulative preview at most every 100 ms, then allows 100 ms for trailing output and performs a final flush before returning. The cumulative metadata.output contract is unchanged.

How did you verify your code works?

  • bun test test/tool/shell.test.ts: 26 pass
  • bun typecheck
  • 80 paced writes: 2.7x-3.7x fewer metadata updates and 8%-23% lower runtime depending on simulated metadata sink cost
  • Patched output was complete in 18/18 runs; baseline lost final output in 11/12 delayed-sink runs

Screenshots / recordings

N/A

Checklist

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

@github-actions

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.

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jul 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

Based on my search, I found several related PRs that touch on shell output handling:

Potentially Related PRs:

  1. fix(app): show shell output while a command runs #37097 - fix(app): show shell output while a command runs

    • Related to shell output visibility during execution, which is connected to your metadata update batching logic
  2. fix: normalize carriage-return shell output #36684 - fix: normalize carriage-return shell output

    • Addresses shell output formatting, potentially affected by your chunking changes
  3. fix(shell): drain stdout before reading output to avoid "(no output)" on exit 0 #35543 - fix(shell): drain stdout before reading output to avoid "(no output)" on exit 0

    • Directly relevant: addresses the exact problem you're solving (trailing output being lost before the final read)

The most closely related is #35543, which also handles the edge case of output loss at process exit. Your PR extends this solution with batching and timing controls to prevent output loss in high-volume scenarios.

No exact duplicates found, but #35543 may need consideration for interaction with your changes.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jul 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

@kvyb

kvyb commented Aug 31, 2026

Copy link
Copy Markdown

Independent evidence now supports reopening this. In a 25.4 GB database, bash progress accounted for 851,118 updates and 9.68 GB of encoded event payload. In ten fresh-process synthetic trials, the exact baseline produced 30 metadata callbacks versus a median of 4 with 100 ms coalescing.

I found this PR only after my duplicate #46287 was auto-closed for contribution-format violations. I will not resubmit the duplicate. This PR was closed by automated age/reaction cleanup rather than a code-review rejection; would a maintainer consider reopening it?

@opgrsankkar

Copy link
Copy Markdown

Confirming the issue on the current stable Homebrew CLI, OpenCode 1.18.25 on macOS arm64. This also appears to contradict the claim above that event persistence is now off for local CLI use. OPENCODE_EXPERIMENTAL and OPENCODE_EXPERIMENTAL_EVENT_SYSTEM were both unset in the shell and launchd environment.

Read-only, aggregate-only diagnostics; no event contents, session IDs, titles, or paths included:

opencode.db:              11,155,890,176 bytes
sessions:                 638
messages:                 10,596
current parts:            52,754
events:                   439,143

message.part.updated.1:   401,369 rows / 10,106,478,202 encoded bytes
message.updated.1:         28,157 rows /     15,570,814 encoded bytes
session.updated.1:          9,069 rows /      6,227,156 encoded bytes
session.created.1:            548 rows /        334,676 encoded bytes

largest 6 aggregates:     9,375,253,883 bytes, 92.6% of all event payload bytes
page_size:                 4096
page_count:                2,723,606
freelist_count:            0
auto_vacuum:               0
integrity_check:           ok

So this instance is overwhelmingly the part-level variant: message.part.updated.1 is 99.8% of encoded event payload bytes. The normal message/part projections are much smaller, and freelist_count=0 confirms that VACUUM alone cannot recover this space.

The six-aggregate concentration also means age-based session deletion is a poor primary fix: a few output-heavy sessions can generate most of the growth quickly. The durable stream needs bounded/coalesced intermediate part updates or supported projection-aware event compaction. A built-in cleanup command for already affected databases would still be valuable.

The installed 1.18.25 source path I checked still calls events.publish(SessionV1.Event.PartUpdated, ...) from Session.updatePart, and the core event service persists durable definitions. If persistence is expected to be opt-in/off for the stable local CLI, there may be a release-path or runtime wiring difference worth checking.

Please consider re-opening this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ShellTool can return before the output stream drains

3 participants