Repository navigation
fix(hooks): report the PostToolUse tool_response.output truncation - #39029
Open
aweirdmaan wants to merge 1 commit into
Open
aweirdmaan wants to merge 1 commit into
aweirdmaan wants to merge 1 commit into
Conversation
tool_response.output is silently cut to 2000 characters at every PostToolUse call site (ACP, dashboard, subagent, messaging) with nothing in the payload indicating a cut happened. A hook parsing a long result (JSON, line counts, file summaries) silently gets wrong answers with no way to tell a short result from a truncated one. Adds a shared bounded_hook_output() helper that keeps the 2000-char bound in place but reports it via two new sibling keys, truncated and output_length, at all four sites. Redaction still runs on the full text before any cut. Documents the cap under PostToolUse in docs/reference/kiro-cli/hooks.md. Fixes kirodotdev#38593 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This branch has not been deployed
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.
Problem / Motivation
Goal: A
PostToolUsehook can tell, from its own input, whethertool_response.outputwas cut — it no longer has to guess from an exact 2000-character length.tool_response.outputis silently cut to its first 2000 characters at everyPostToolUsecall site, with nothing in the payload saying a cut happened — notruncatedflag, no original length. A hook parsing a long result (JSON, a line count, a file summary) works on short results and silently gets wrong answers on long ones: a 7901-character JSON tool result reaches the hook as 2000 characters of broken JSON with no sign anything is missing.Why it matters
Every
PostToolUsehook author who parsestool_response.outputis exposed to this today, across all four surfaces that fire the event (ACP, dashboard, subagent, messaging). The only workaround available today is guessing that exactly 2000 characters means a cut, or re-reading the session transcript (the workaround #28610 already documents hook authors resorting to).Not a goal
tool_input/tool_call_id) or fix: redact full text before bounding at core-path slice sites (#7390) #7424 (redaction ordering) — related context cited in the issue, not re-opened here.subagent_manager/run_turn.pytruncates before redacting while the other three sites redact before truncating — noted below, left as-is.What changed (motivation → approach → change)
Observed symptom: a cut
tool_response.outputwith no signal that a cut happened, at 4 independent call sites that each apply the same baretext[:2000]slice.Approach: rather than patch each site ad hoc, add one small shared helper,
bounded_hook_output()(new modulesrc/kiro_crew/hook_output.py), that returns{"output": text[:limit], "truncated": len(text) > limit, "output_length": len(text)}. Considered homing it inkiro_crew.hooks(rejected — its "owners read these globals" pattern couples it tightly to the hook engine, not a bounding concern) andkiro_crew.security(rejected — redaction is a different concern, and redaction must still run on the full text before this bound is applied). A new dependency-free leaf module lets all four call sites (ACP, dashboard, subagent, messaging) import it without adding an edge to either larger module.Change: replaced the bare
[:2000]/[:_POST_TOOL_OUTPUT_CHARS]slice with a call tobounded_hook_output()at all four sites (acp/client_tool_events.py,dashboard/chat_turn/tool_results.py,subagent_manager/run_turn.py,messaging/tool_hooks.py), and documented the cap and the two new keys underPostToolUseindocs/reference/kiro-cli/hooks.md.Backwards compatibility
Compatible: this only adds two new sibling keys (
truncated,output_length) alongside the existingoutputkey in thetool_responsedict a hook receives. A hook that reads onlyoutput(every hook written against today's contract) is unaffected; a hook that ignores unknown keys keeps working unchanged.Removes nothing: no field, type, or behavior is taken away — the 2000-character bound, redaction order, and existing
outputcontents are all unchanged.Tests
Proof: not provable -- other: the fix is itself a new helper module; reverting that module (not a logic tweak inside existing code) breaks test collection for the new unit tests that import it directly, rather than producing an assertion failure, so prove.py's revert-and-rerun technique is INCONCLUSIVE rather than PROVEN or NOT_PROVEN by its own design (see tool output below). Manually confirmed the before/after behavior instead — see Real-machine verification.
Automated tests added/updated:
test/test_hook_output.py— direct unit tests ofbounded_hook_output()(cap value, long/short/exact-boundary/empty text, customlimit).test/test_messaging_tool_hooks_post_tool_truncation.pyandtest/test_dashboard_post_tool_truncation.py— drive the dashboard and messagingPostToolUsesites end-to-end for a 7901-char and a 1500-char output; neither site had existing truncation coverage.test/test_acp_client_tool_hooks.pyandtest/test_subagent_run_loop.pywithtruncated/output_lengthassertions alongside their pre-existing pinned 2000-character length checks (those length pins are untouched).test_acp_client_more_coverage_observers.py,test_kas_spec_hooks_channel_turn.py) whose exacttool_response == {"output": ...}dict-equality assertions would otherwise regress from the shape change — a necessary consequence of the new keys, not scope creep.Manual verification
N/A — unit coverage sufficient (all 4 call sites and the helper itself are covered by the tests above).
Real-machine verification
Machine: own computer (macOS, arm64).
Before (base
5ec3b81677, upstream/main): the capability does not exist at all —After (head
cbb347b324):Full test suite on head:
pytest test/test_acp_client_tool_hooks.py test/test_subagent_run_loop.py test/test_hook_output.py test/test_messaging_tool_hooks_post_tool_truncation.py test/test_dashboard_post_tool_truncation.py test/test_acp_client_more_coverage_observers.py test/test_kas_spec_hooks_channel_turn.py -q→ 143 passed.Cleanup: own computer, no instance to terminate.
Sibling sweep
Swept:
grep -rn '\[:2000\]' src/for other PostToolUse-style hook-output truncation missing a cut marker.Sibling: #38616 -- same defect ("the same 2000-character cut of hook output, on the other hook path"), intentionally out of scope for this fix, tracked separately.
Sibling: other
[:2000]sites found (task_reporter.py,dashboard/chat_utils_slots.py,slack/interactions.py,taskrunner_parts/run_reporting.py,apps/job_sdk.py, etc.) -- not affected, 8+ chars -- these bound internal summaries/notifications/error strings, not thePostToolUsehook contract this issue is about.Pattern harvest
Not generalizable: this is a one-off missing-signal gap in a hand-written payload dict at 4 call sites, not a reusable static-analysis pattern (there's no generic "truncation without a marker" shape a linter could flag across the codebase without also flagging the many legitimate internal
[:2000]summaries found in the sibling sweep above).Related Issues
Fixes #38593
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributed under the project's Apache-2.0 license.