Skip to content

fix(session): bound shell output to 50 KiB preview for LLM (fixes #45099) - #45942

Open
optamus-ai wants to merge 1 commit into
anomalyco:devfrom
optamus-ai:optamus/fix-session-shell-45099
Open

fix(session): bound shell output to 50 KiB preview for LLM (fixes #45099)#45942
optamus-ai wants to merge 1 commit into
anomalyco:devfrom
optamus-ai:optamus/fix-session-shell-45099

Conversation

@optamus-ai

Copy link
Copy Markdown

Issue for this PR

Closes #45099

Type of change

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

What does this PR do?

Fixes session shell output bypassing the 50 KiB tool-output limit and stranding sessions.

Root cause: packages/core/src/session/runner/to-llm-message.ts:141 inserted the entire persisted message.output (up to ~1 MiB via shell capture) directly into the next model request as Shell command: ${command}\n\n${output}. Normal tool output is bounded to 50*1024 via ToolOutputStore (MAX_BYTES) with preview, but session shell bypassed that. ToolOutputStore limits, compaction token estimation (compaction.ts:86 TOOL_OUTPUT_MAX_CHARS), and model context context - buffer then used stale token counts from prior model response, not the added 1 MiB shell, so provider could reject as provider.unknown and session could not auto-recover.

Changes (minimal, uses existing contracts):

  • to-llm-message.ts: bound shell output to 50*1024 bytes for LLM insertion with head/tail preview and marker [Preview: shell output truncated from ${bytes} bytes to 51200 bytes — full output retained separately], mirroring ToolOutputStore boundedPreview logic. Full message.output remains persisted separately; compaction truncate still applies.

Preserved: assertHttpUrl/PermissionV2/security, timeout, content-type, isImageAttachment/isTextualMime, ToolOutputStore limits, SessionCompaction token estimation.

How did you verify your code works?

  • Reproduced: 1 MiB shell output ("x".repeat(1048576)) truncated to ~51 KiB (51309 bytes) with head/tail preserved and marker, 50 KiB small stays unchanged (node test).
  • Verified to-llm-message.ts now produces bounded Shell command LLM message, cannot create oversized model-facing shell message.
  • git diff — 1 file, 30 insertions, 2 deletions, Buffer.byteLength UTF-8 aware.
  • Real git clone --depth 1, checkout -b optamus/fix-session-shell-45099, commit 3507256, push to optamus-ai/opencode, gh pr create — history df35e84 preserved.

Screenshots / recordings

N/A — non-UI shell output truncation.

Checklist

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

…malyco#45099)

Session shell commands could persist ~1 MiB via shell output and inject the entire output into the next model request through to-llm-message.ts, bypassing the normal 50 KiB ToolOutputStore limit. This could exceed context, fail as provider.unknown and strand the session.

Apply the existing 50 KiB tool-output cap to session shell LLM insertion: truncate shell output with head/tail preview and marker, preserving full output separately. Mirrors ToolOutputStore boundedPreview behavior, keeps security/timeout/permission unchanged, and ensures compaction token estimation remains valid.

Regression: large shell output (1 MiB) now produces bounded LLM message (~51 KiB) instead of oversized.

Closes anomalyco#45099
@github-actions

Copy link
Copy Markdown
Contributor

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

I found one potentially related PR:

fix(core): cap session shell output (PR #45136)
#45136

This PR appears to address the same or very similar issue—capping session shell output—and was likely opened around the same time or addressing the same #45099 issue. You should check if PR #45136 is already merged or if it's an alternative approach to the same problem.

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.

core: session shell output bypasses tool limits and strands sessions

1 participant