Skip to content

fix(core): show line counts when tool output is truncated - #51200

Merged
rekram1-node merged 1 commit into
v2from
truncation-line-counts
Sep 24, 2026
Merged

rekram1-node merged 1 commit into
v2from
truncation-line-counts

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Issue for this PR

N/A

Type of change

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

What does this PR do?

When shell output or another tool's result is cut, the note now says which lines are shown and how many there are in total.

Before After
Shell (keeps the end) [full output saved to <file>] [showing lines 6432-8431 of 8431; full output saved to <file>]
Other tools (keep the start) ... 6431 lines truncated; full content saved to <file> ... [showing lines 1-2000 of 8431; full output saved to <file>]

The line numbers match read's offset, so the agent knows exactly where the missing part starts in the saved file. The shell note used to give no size at all. The other note gave a line count or a byte count depending on which limit was hit; it now always gives lines.

Cost. Shell output is only read back from its last 50 KB, so the total can't come from the capture. Instead, newlines are counted as each chunk is written to the output file, so building the note never rescans the file. Counting took about 17 ms per 100 MB of output in a quick benchmark. The other tools' results are already in memory, so their count is free.

Edge cases:

  • When the shell cut lands in the middle of a line, the range starts at that partial line's number.
  • When another tool's result is a single line over the byte limit (e.g. minified JSON), no lines fit, and the note reads [showing 0 lines of 1; full output saved to <file>].

read, grep and glob are unchanged. read already says where to continue, and grep/glob stop searching at the limit, so their totals aren't known.

How did you verify your code works?

  • Ran real commands through the shell tool in a throwaway test:
    • seq 1 8431 gives lines 6432-8431 of 8431, and the first line shown is 6432.
    • 1,000 lines of 100 characters, cut by the byte limit, gives lines 494-1000 of 1000, and the partial first line is line 494.
    • A single 200 KB line gives lines 1-1 of 1.
  • Ran ToolOutput.truncate on 8,431 short lines (lines 1-2000 of 8431, last line shown is line 2000) and on a single 60 KB line (0 lines of 1).
  • Updated the four existing assertions in test/tool-output.test.ts for the new note, and renamed the byte-limit test to match.
  • bun run check passes. In packages/core, test/tool-output.test.ts, test/tool-shell.test.ts, test/session-shell.test.ts and test/shell.test.ts give 104 pass, 6 fail. The same 6 bash-approval tests fail locally on v2 without this change.

Screenshots / recordings

N/A

Checklist

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

@rekram1-node
rekram1-node merged commit 8118690 into v2 Sep 24, 2026
14 of 15 checks passed
@rekram1-node
rekram1-node deleted the truncation-line-counts branch September 24, 2026 19:50
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