Skip to content

fix(hooks): report the PostToolUse tool_response.output truncation - #39029

Open
aweirdmaan wants to merge 1 commit into
kirodotdev:mainfrom
aweirdmaan:fix/posttooluse-truncation-marker
Open

aweirdmaan wants to merge 1 commit into
kirodotdev:mainfrom
aweirdmaan:fix/posttooluse-truncation-marker

Conversation

@aweirdmaan

@aweirdmaan aweirdmaan commented Nov 18, 2026 •

Copy link
Copy Markdown

Problem / Motivation

Goal: A PostToolUse hook can tell, from its own input, whether tool_response.output was cut — it no longer has to guess from an exact 2000-character length.

tool_response.output is silently cut to its first 2000 characters at every PostToolUse call site, with nothing in the payload saying a cut happened — no truncated flag, 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 PostToolUse hook author who parses tool_response.output is 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

What changed (motivation → approach → change)

Observed symptom: a cut tool_response.output with no signal that a cut happened, at 4 independent call sites that each apply the same bare text[:2000] slice.

Approach: rather than patch each site ad hoc, add one small shared helper, bounded_hook_output() (new module src/kiro_crew/hook_output.py), that returns {"output": text[:limit], "truncated": len(text) > limit, "output_length": len(text)}. Considered homing it in kiro_crew.hooks (rejected — its "owners read these globals" pattern couples it tightly to the hook engine, not a bounding concern) and kiro_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 to bounded_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 under PostToolUse in docs/reference/kiro-cli/hooks.md.

Backwards compatibility

Compatible: this only adds two new sibling keys (truncated, output_length) alongside the existing output key in the tool_response dict a hook receives. A hook that reads only output (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 output contents 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.

$ python3 prove.py --base upstream/main
INCONCLUSIVE: the mutated run only errored at collection, so no assertion observed the bug
  inconclusive: all production hunks

Automated tests added/updated:

  • New test/test_hook_output.py — direct unit tests of bounded_hook_output() (cap value, long/short/exact-boundary/empty text, custom limit).
  • New test/test_messaging_tool_hooks_post_tool_truncation.py and test/test_dashboard_post_tool_truncation.py — drive the dashboard and messaging PostToolUse sites end-to-end for a 7901-char and a 1500-char output; neither site had existing truncation coverage.
  • Extended test/test_acp_client_tool_hooks.py and test/test_subagent_run_loop.py with truncated/output_length assertions alongside their pre-existing pinned 2000-character length checks (those length pins are untouched).
  • Updated 2 other pre-existing tests (test_acp_client_more_coverage_observers.py, test_kas_spec_hooks_channel_turn.py) whose exact tool_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 —

$ python3 -c "from kiro_crew.hook_output import bounded_hook_output"
ModuleNotFoundError: No module named 'kiro_crew.hook_output'

After (head cbb347b324):

$ python3 -c "from kiro_crew.hook_output import bounded_hook_output as b; print(b('x'*7901)['truncated'], b('x'*7901)['output_length'], len(b('x'*7901)['output'])); print(b('short text')['truncated'])"
True 7901 2000
False

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 the PostToolUse hook 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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

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>
@aweirdmaan
aweirdmaan requested review from a team and KiroCrewCaptain as code owners November 18, 2026 07:28
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostToolUse hooks get tool_response.output cut to 2000 characters with no marker, and the hooks reference states no limit

1 participant