Skip to content

fix(core): keep the start and end of long shell output - #67159

Merged
rekram1-node merged 1 commit into
devfrom
shell-output-cut
Nov 30, 2026
Merged

rekram1-node merged 1 commit into
devfrom
shell-output-cut

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Closes #

Type of change

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

What does this PR do?

When shell output is over tool_output (2,000 lines / 50 KiB by default), the shell tool kept only the end and put the note at the bottom. Anything at the start was lost, and the result looked like complete output. Models missed rg matches and files in listings that were in the cut start, and scripts that edited a cat result and wrote it back deleted the top of the file.

It now keeps the start and the end, half of each limit each, with the note where the cut is. Normal mode and Code Mode get the same text.

 $ seq 1 3000
-1001
-…
-3000
-
-[showing lines 1001-3000 of 3000; full output saved to …/sh_….out]
+1
+…
+1000
+
+[lines 1001-2000 of 3000 cut (10 KB); full output saved to …/sh_….out]
+
+2001
+…
+3000

How the cut works:

fits both limits        → whole output, as before
otherwise
  start = first half of the byte limit, then at most half of max_lines lines
  end   = last half of the byte limit, then at most half of max_lines lines
  each side is cut after a line break,
    unless that would drop more than half of it (one huge line): then it keeps part of the line
  note = lines a-b of N cut (size), or "part of …" when a line is only partly shown
  • The cut is worked out on raw bytes. Output up to the byte limit is read once; longer output only at its two ends (25 KiB + 3 bytes each by default), so memory use stays bounded however long the output is, like today.
  • Cuts never split a UTF-8 character or a \r\n; they reuse splitCharacter, which Shell.output already uses. A running command's unfinished last character is left out. TextBoundary.utf8Tail had no other users and is removed.
  • The special case for a single cut-off line, and the line-start bookkeeping it needed (in the spawner and a backwards scan of daemon files), is gone: the note now reports how much was cut instead of how long the cut line was.
  • Line numbers still come from the newline count kept while streaming. When a running daemon command's file is too big to count (over 16 MiB), the note gives only the size cut.

Only the shell changes. Other tools keep the start through ToolOutput.truncate as before.

How did you verify your code works?

  • End-to-end A/B in full Code Mode (dev 0fd4df1ca vs this PR, 6 smaller models, fresh project per run). On a task where the needed match is on a 208 KB line, dev's tail-only cut drops that line entirely; this PR shows it with the note: 144/180 vs 166/180 correct (+12 pts, 95% CI +5 to +19, Fisher p = 0.001; gpt-6-luna 17/30 vs 30/30). A multi-file task was 90/90 in both. No corrupted files or timeouts in either. Models re-ran shell commands about half as often after a cut (299 vs 142 calls).

  • Updated the truncation assertions in test/tool-shell.test.ts and test/shell-daemon.test.ts (whole lines at both ends, a line ending right at the cut, blank and CRLF lines next to the note, one long line, UTF-8 characters at both cuts, line limits, unknown line count).

  • A separate review ran a fuzzer outside the repo (about 400,000 cases: exited and running commands, incomplete trailing UTF-8, max_lines 1-10, max_bytes 1-100 and the default, binary bytes, CRLF, blank lines) checking line numbers, cut size, overlap and UTF-8 against ground truth: no failures.

  • bun test for every shell and codemode test file in packages/core: 3608 pass. 4 fail the same way on dev on this machine (system bash 3.2 and temp-path tests).

  • bun run typecheck in packages/core, bunx tsc -p tsconfig.build.json --noEmit in packages/server, packages/sdk and packages/simulation.

Screenshots / recordings

N/A

Checklist

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

Added by the opencode bot (AI-generated).

Related: #62718

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

This works as described. On the base, a 3,000-line output came back as lines 1001–3000 only. With this PR it comes back as lines 1–1000, the [lines 1001-2000 of 3000 cut (5 KB); …] note, then lines 2001–3000. The shell, daemon and shell-result tests pass, except one that fails the same way on the base. tsc -b in packages/core and the text-boundary test pass too.

I checked the cut logic for overlap between the two ends, odd limits, empty ends, one huge line, ends that start mid-line and an unknown line count, and found no problems. Memory use stays bounded because each end is read on its own.

Public API: unchanged. Only the wording of the shell truncation note changes.

One small inaccuracy in the note is flagged inline.

Related: #62718

Comment thread packages/core/src/shell.ts Outdated
const tailWindow = yield* readRange(info.file, Math.max(0, tailStart - 1), size - Math.max(0, tailStart - 1))
const lineStart = tailStart === 0 || tailWindow[0] === 10
const end = TextBoundary.utf8Tail(tailWindow, size - tailStart)
const head = firstLines(

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.

When the start ends exactly on a line break (headWindow[headSize] === 10), the last line of head is complete but has no trailing \n. So firstLines either drops it (if it is over half the window) or keeps it, and the note then says "part of" a line that is fully shown. The extra byte is already read here, so treating it as a line end (or passing a flag to firstLines) would make the note exact.

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

The new commit fixes the earlier point: a line that ends exactly where the start is cut is now kept whole, and the note no longer calls it "part of" a line. The shell, daemon and shell-result tests pass, except one that also fails on the base. tsc -b in packages/core is clean. Nothing new to raise. Public API: unchanged.

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

The earlier point about a line ending right at the cut is still fixed after the byte-based rewrite: startEnd keeps the line break just past the limit when it does not overlap the end.

I checked the new cut logic by fuzzing startEnd/endStart (300k random outputs with CRLF, multi-byte characters and small limits) for overlap, the byte and line limits and UTF-8 boundaries: no failures. The shell, daemon and shell-result tests pass, except one that also fails on the base. Type checks for packages/core and packages/util are clean.

Public API: unchanged. Removing TextBoundary.utf8Head/utf8Tail from @opencode/util only affects internal code, and nothing else used them.

Nothing new to raise.

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

The new commit moves the cut logic into keepEnds and cutNote without changing the output. I compared the two helpers with the inline code they replace and found the same calculations. The shell, daemon and shell-result tests pass, except one that also fails on the base, and tsc -b in packages/core is clean. Public API: unchanged. Nothing new to raise.

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

The rebase onto the shared output-store code (shell/capture.ts) keeps the cut logic I reviewed and fuzzed earlier. Local, daemon and remote shells now all get the start-and-end cut. The output file is still created in the same cases as on dev. lineStarts is removed from the internal OutputStore type, and nothing outside packages/core used it. The shell, daemon, remote and shell-result tests pass, except one that also fails on the base, and tsc -b in packages/core is clean. Public API: unchanged. Nothing new to raise.

@rekram1-node
rekram1-node enabled auto-merge (squash) November 30, 2026 05:56

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

This push only rebases onto a newer dev: git range-diff shows the pull request's commit is identical to the one I last reviewed, and the new dev commits don't touch the shell code or its tests. The shell, daemon, remote and shell-result tests pass, except one that also fails on the base, and tsc -b in packages/core is clean. Public API: unchanged. Nothing new to raise.

@rekram1-node
rekram1-node merged commit 5b287da into dev Nov 30, 2026
12 checks passed
@rekram1-node
rekram1-node deleted the shell-output-cut branch November 30, 2026 05:58
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.

1 participant