Skip to content

fix(hooks): stop the post-tool-use wire bound from collapsing escape-heavy outputs - #9

Merged
codeasier merged 1 commit into
v0.9.29-patchfrom
fix/hook-truncation
Sep 28, 2026
Merged

codeasier merged 1 commit into
v0.9.29-patchfrom
fix/hook-truncation

Conversation

@codeasier

Copy link
Copy Markdown
Owner

Fork-local fix for the reproduced hook truncation regression (risk-list item 1). Not an upstream port.

The regression

The hook's 32k structure-preserving wire bound sliced strings by raw character count. JSON escaping (quotes, backslashes, control characters) can double or triple the serialized form, so a "truncated" string could still exceed its budget. The object accounting's cost check then rejected the field, and the loop's break discarded every remaining field. Reproduced with the pre-fix code:

input : { stdout: '"'.repeat(60000), file_path: '/very/important/path.ts', exit_code: 0 }
output: {}          # every field lost — file_path included

Quote-heavy stdout is exactly what serialized-JSON tool outputs look like, so this fired on real captures.

The fix

Truncation moves to src/hooks/truncate.ts (importable, sdk-guard.ts pattern) and now mirrors the server's boundValue in src/functions/observe.ts:

  1. Strings cut by binary search on serialized length (the server's boundString algorithm) — the delivered form always fits its budget.
  2. Object fields taken smallest-first (decorate-sort-undecorate by serialized size) — a huge stdout cannot crowd out file_path/exit_code. Field order changes in transit only; the server re-bounds and re-orders anyway.
  3. Unfittable entries skipped, not fatal (break → continue in the entry loops) — long primitives or sub-marker budgets no longer discard later fittable entries.

Built copies in dist/hooks/ and plugin/scripts/ regenerate via npm run build.

Verification

  • New test/post-tool-use-truncation.test.ts: 17 tests (serialized-length bound, smallest-field survival, skip-not-stop, passthrough, nested budgets)
  • npm test: 1861 passed, 1 skipped (baseline 1844 + 17)
  • npx tsc --noEmit: identical error set to the post-fix(cli): make fresh installs portable and persistent #8 baseline (31 normalized lines, zero new)
  • End-to-end against the built plugin/scripts/post-tool-use.mjs with a mock REST sink: 60k-quote stdout payload delivers {exit_code, file_path, stdout: truncated} at 31999/32000 serialized chars — the previous build delivered {}
  • npm run build: clean; plugin script verified to contain the binary search + sort + continues

…heavy outputs

The hook's 32k structure-preserving bound sliced strings by raw character
count. JSON escaping (quotes, backslashes, control characters) can double
or triple the serialized form, so a 'truncated' string could still exceed
its budget; the object accounting's cost check then rejected it and the
loop's break discarded every remaining field. Tool outputs like
{stdout: <quote-heavy json>, file_path, exit_code} arrived as {} — losing
exactly the small fields the bound exists to keep.

The truncation moves to src/hooks/truncate.ts and now mirrors the
server's boundValue:

- strings are cut by binary search on serialized length (boundString),
  so the delivered form always fits its budget;
- object fields are taken smallest-first, so a huge stdout cannot crowd
  out file_path / exit_code when the budget runs out;
- a single unfittable entry (long primitives, sub-marker budgets) is
  skipped instead of discarding the rest (break -> continue).

Field order of over-budget objects changes in transit only; the server
re-bounds and re-orders anyway. The built copies in dist/hooks/ and
plugin/scripts/ are regenerated by npm run build.

Verified end to end against the built plugin/scripts/post-tool-use.mjs
with a mock REST sink: a 60k-quote stdout payload now delivers
{exit_code, file_path, stdout: truncated} at 31999/32000 chars where the
previous build delivered {}.
@codeasier
codeasier merged commit 26308d1 into v0.9.29-patch Sep 28, 2026
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