Repository navigation
fix(hooks): stop the post-tool-use wire bound from collapsing escape-heavy outputs - #9
Merged
Merged
Conversation
…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 {}.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
breakdiscarded every remaining field. Reproduced with the pre-fix code: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.tspattern) and now mirrors the server'sboundValueinsrc/functions/observe.ts:boundStringalgorithm) — the delivered form always fits its budget.stdoutcannot crowd outfile_path/exit_code. Field order changes in transit only; the server re-bounds and re-orders anyway.break→continuein the entry loops) — long primitives or sub-marker budgets no longer discard later fittable entries.Built copies in
dist/hooks/andplugin/scripts/regenerate vianpm run build.Verification
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)plugin/scripts/post-tool-use.mjswith 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