Skip to content

fix: truncate LLM-facing command output head+tail instead of head-only - #777

Merged
will-lamerton merged 2 commits into
Nano-Collective:mainfrom
MsfPablo:fix/bash-output-truncation
Aug 9, 2026
Merged

will-lamerton merged 2 commits into
Nano-Collective:mainfrom
MsfPablo:fix/bash-output-truncation

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #764. execute-bash.tsx and custom-tools/handler.ts both truncated long output for the model by keeping only the first TRUNCATION_OUTPUT_LIMIT (2000) characters. Compiler and test-runner output puts the actionable part at the end — tsc prints its error list then the count, AVA prints passing output then the failure summary, most build tools print the exit digest last — so head-only truncation throws away exactly what the model needs from a pnpm run test:types or pnpm run test:ava run. It then has to re-run with a narrower command to recover information already in hand.

Added a shared helper, source/utils/truncate-output.ts, that keeps both the head and the tail within the same byte budget (40/60 split, tail-weighted since that's where the actionable content usually is), with a marker showing how many characters were elided in between. Used it at both LLM-facing call sites:

  • formatBashResultForLLM in execute-bash.tsx
  • truncate() in custom-tools/handler.ts

Left bash-progress.tsx's head-only truncation alone — per the issue, that one only feeds a display-only token-count estimate for the live progress UI, not something the model sees.

Test plan

  • pnpm run test:format, test:lint, test:types — all clean
  • New source/utils/truncate-output.spec.ts — 5 tests covering the under/at/over-limit cases, that both head and tail are kept, and the 40/60 split
  • pnpm run test:ava source/tools/execute-bash.spec.tsx source/custom-tools/handler.spec.ts — 67 tests pass unmodified, including the existing truncation-length and truncation-marker assertions

execute-bash.tsx and custom-tools/handler.ts both truncated long output
by keeping the first TRUNCATION_OUTPUT_LIMIT characters and discarding
the rest. Compiler and test-runner output puts the actionable part at
the end (tsc's error list, AVA's failure summary, most tools' exit
digest), so head-only truncation throws away exactly what the model
needs and forces an extra round-trip with a narrower command to
recover it.

Added a shared truncateHeadAndTail() helper (source/utils/truncate-output.ts)
that keeps both ends within the same byte budget, tail-weighted (40/60
head/tail split) since that's where the actionable content usually is,
with an elision marker showing how much was cut. Used it in both call
sites. Left bash-progress.tsx's head-only estimate alone, since that
one only feeds a display-only token-count stat, not the model.

Fixes Nano-Collective#764

@akramcodez akramcodez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM great work @MsfPablo

@luisangelrod luisangelrod left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice extraction and the tail-weighted split matches the failure-output use case well. One contract detail is worth clarifying before this helper is reused by the global-cap work in #769: limit currently limits only the retained source characters, not the returned string. For a 10,000-character input with limit = 2_000, the result is 2,052 characters after adding the marker.

That is consistent with the two old call sites (they also appended a marker beyond TRUNCATION_OUTPUT_LIMIT), so I do not see it as a blocker for this PR. But the generic helper name/signature makes a hard output ceiling easy to assume. I would either document that limit is the retained-content budget, or make the marker count toward the limit and add a result.length <= limit test. The latter would make this helper safe to reuse for #769 without quietly exceeding the configured cap.

I also traced both call sites and verified the 40/60 calculation directly. I could not complete the repository test command locally because the current checkout's frozen install reports a pre-existing patchedDependencies/lockfile configuration mismatch, so this feedback is based on the diff plus a direct execution of the helper logic rather than claiming a full suite run.

@will-lamerton

Copy link
Copy Markdown
Member

Nice work @MsfPablo :)

@will-lamerton
will-lamerton merged commit d35b2fa into Nano-Collective:main Aug 9, 2026
1 check passed
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.

Bash output truncation is head-only, discarding the tail where build and test errors live

4 participants