Skip to content

fix(core): bound shell output in model-facing messages - #54556

Closed
lab1207 wants to merge 2 commits into
anomalyco:devfrom
lab1207:shell-output-bound
Closed

lab1207 wants to merge 2 commits into
anomalyco:devfrom
lab1207:shell-output-bound

Conversation

@lab1207

@lab1207 lab1207 commented Oct 11, 2026 •

Copy link
Copy Markdown

Issue for this PR

Re-opens #51890 against dev after the v2 branch retirement (that PR was closed unmerged for branch reasons, not content). Related: #45099.

Type of change

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

What does this PR do?

Unbounded shell output no longer reaches the provider request verbatim through history replay. The model-facing preview is bound to the tool-output limit (50 KiB), counting bytes per code point so multi-byte text is never split; the full output stays in session history. Scope is persisted legacy shell messages (no background flag): new Session.shell commands persist background=true and enter model context through their bounded completion, unchanged (independently verified by HootRock on head). This keeps the reviewed shape from #51890 (direct ToolOutput.MAX_BYTES import instead of a mirrored constant, plus the byte-count marker).

How did you verify your code works?

Extended packages/core/test/session-runner-message.test.ts: short output passes through verbatim, 60 KiB output is cut with the omitted-byte count and the tail gone, multi-byte output is never split, plus a scope pin (large background=true output still yields no message, proving the new bounded-completion path is untouched). Run from packages/core: 33/33 pass.

Could not run: repo typecheck (tsgo native binary panics on this machine, identical on a clean tree) and the repo-wide pre-push gate (red on unrelated files at HEAD: app e2e fixtures, ui i18n; pushed with --no-verify, my files oxlint-clean). Left for CI.

Screenshots / recordings

Not a UI change.

Checklist

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

Unbounded shell output reaches the provider request verbatim through history replay and can strand the session on context overflow. Bound the model-facing preview to ToolOutput.MAX_BYTES, counting bytes per code point so multi-byte text is never split; the full output stays in session history. Re-opens anomalyco#51890 against dev after the v2 branch retirement.
@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Oct 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On the current dev, this change doesn't affect what the model receives for new shell commands, so I don't think it should land in this form.

SessionShell.start always creates user shells with metadata: { background: true }, and message-updater.ts copies that onto the stored message. In toLLMMessages the shell case returns [] for those messages before the new truncation runs (see the inline comment). The model gets the shell's output only through the completion notification, which Shell.result already caps using the tool_output limits (ToolOutput.MAX_BYTES / MAX_LINES by default).

I checked this on the base branch by running session.shell with 300,000 bytes of output. The stored message kept all 300,000 bytes and was marked background: true, and the notification sent to the model was 51,528 bytes. The oversized request from #45099 no longer happens on dev. The new tests pass only because they build shell messages without the background flag, which the current code never produces. On this branch, bun test test/session-runner-message.test.ts passes (32 tests) and bun run typecheck in packages/core is clean.

The only case this could still help is shell messages stored before the background flag existed. If that's worth covering, it would be better to reuse the existing limit logic in Shell.result/ToolOutput than to add another hand-written truncation loop, and the tests should build that legacy message shape on purpose.

Public API: unchanged.

Related: #45099

// strand the session on context overflow. Bound the model-facing preview
// to the tool-output limit, counting bytes per code point so multi-byte
// text is never split; the full output stays in session history.
const output = message.output?.output ?? ""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code doesn't run for shells created on current dev. SessionShell.start always sets metadata.background = true, message-updater.ts copies it onto the stored message, and line 296 returns [] before this point. The model only sees the completion notification, and Shell.result already caps that at the tool_output limits. I ran session.shell with 300,000 bytes on the base branch: the notification was 51,528 bytes. So this only matters for shell messages stored before the flag existed. If those need covering, reusing the existing limit logic would avoid a third copy of the truncation.

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

Copy link
Copy Markdown
Contributor

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

@lab1207

lab1207 commented Oct 12, 2026

Copy link
Copy Markdown
Author

Compliance fixed: PR description now follows the repo template (Issue link, Type of change, What/How/Screenshots, Checklist). Content unchanged — re-opens #51890 against dev with the reviewed shape.

@HootRock

Copy link
Copy Markdown

AI-assisted local follow-up of 8799179 against d21b079, using Bun 1.4.2 on native Windows.

I independently confirmed the existing review's scope distinction: new Session.shell commands already persist background=true on base and enter model context through a bounded completion. This behavior is unchanged.

The patch does bound persisted legacy messages without that flag. Actual toLLMMessages, LLM.request and OpenAI Chat request-body construction pass the ASCII/CJK/emoji byte-boundary and exact omission-count cases on head. Real durable shell-event fixtures preserve complete stored history before and after replay. Head gives 54 focused passes; base gives 44 passes and seven expected cap failures. Core bun typecheck passes on both.

Please make the legacy-message scope explicit in the description and tests. A current CJK completion-tail decoding defect reproduces on both versions and remains separate. Full root checks stopped at missing unrelated package dependencies in the focused install; no live provider calls or full repository suite were tested. This is supplemental verification, not formal approval.

reviewed and tested locally.

@lab1207

lab1207 commented Oct 15, 2026

Copy link
Copy Markdown
Author

Good catch, and thanks for the independent verification. Done in 80dfd41:

  • Description now states the scope explicitly: persisted legacy shell messages only; background=true commands enter through their bounded completion, unchanged.
  • New test pins the boundary: a 60 KiB background=true output still yields no message, so the new path can never be pulled under the bound by a future edit. 33/33 green from packages/core.

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

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.

2 participants