Skip to content

fix(hooks): give a subagent's PreToolUse hook an edit call's params - #32219

Open
famanson wants to merge 6 commits into
kirodotdev:mainfrom
famanson:fix/spec-hook-edit-tool-input
Open

famanson wants to merge 6 commits into
kirodotdev:mainfrom
famanson:fix/spec-hook-edit-tool-input

Conversation

@famanson

@famanson famanson commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A subagent's or task-runner run's PreToolUse spec hook gets an edit call's arguments as tool_input, so a hook that checks the edit's file path can allow it.

On kas, every file edit by a delegated agent whose spec has a PreToolUse hook reaches the hook with no tool_input key at all. A guard that checks the edit's path cannot see one, so it refuses every edit. The model is told "The user rejected this tool call." Shell calls on the same path are unaffected.

Observed on 0.12.0 with a crew agent spec carrying preToolUse: [{matcher: "*", command: "python3 -I guard.py --role test-writer"}]. The hook's own stderr from a str_replace on a test file:

guard: this fs_write call names no file path (it has nothing), so it is refused.

security_events.jsonl records it as outcome: denied, error: hook_deny, reason: spec_hook.

Why it matters

A spec hook that guards writes by path is the normal way to scope a delegated agent ("the test-writer may only write tests"). With this bug, every such agent on kas can run shell commands but cannot edit a single file, and the refusal it reports misleads both the model and the user.

Not a goal

  • The dashboard chat turn's Hooks-page PreToolUse fire (dashboard/chat_turn/permission_prompt.py, tool_approval.py). It parses event.tool_input the same way and likely has the same gap for edits; it is left for a follow-up so this PR stays on the reported path.
  • Changing what the approval row renders. The diff stays the row's rendering.

What changed (motivation → approach → change)

  • Symptom: the hook's stdin has no tool_input for an edit call.
  • Root cause: SpecHooks.refuse (tool_permission.py) hands permission_pre_tool_block the event's tool_input, which is the call's rendering for the approval row. For an edit call, session_update_parser.py replaces that rendering with a unified diff (input_str = diff_str, since fix(chat): show a diff on kiro-cli v3 replace edit rows #26589). permission_pre_tool_block then does json.loads on the diff, the parse fails silently, tool_input stays None, and script_hooks.fire leaves the key out of the payload.
  • Change:
    • New pre_tool_hook_input(event_tool_input, event_tool_params) in hook_runtime/hook_dispatch.py. A rendering that parses as a JSON object is used as before. Otherwise the call's structured params (AcpEvent.raw_tool_params) are used, with every string key and leaf credential-redacted (security.redact), matching the redaction the rendering already had.
    • permission_pre_tool_block takes a keyword-only event_tool_params=None and uses the helper.
    • SpecHooks.refuse passes event.raw_tool_params.
    • The kiro_crew.hooks facade re-exports the helper.

Backwards compatibility

Compatible: event_tool_params is a new keyword-only argument defaulting to None, and with it absent or with a JSON-object rendering the hook gets exactly what it got before.

Removes nothing: the change only adds a tool_input where the payload previously had none.

Tests

  • test_the_gate_gives_an_edit_hook_the_calls_params_not_its_diff: a diff rendering plus params gives the hook the params.
  • test_the_gate_keeps_a_json_rendering_over_the_params: a JSON rendering still wins.
  • test_the_gate_redacts_the_params_it_falls_back_to: a credential in the params is redacted, and the path stays readable.
  • test_the_gate_with_neither_json_nor_params_sends_no_tool_input: unchanged behaviour when nothing is readable.
  • test_tool_permission.py: the two pinned pre_tool call-arg tests now expect event_tool_params.
  • test_hooks_composition_contract.py: _BASE_SHAPE_DIGEST re-pinned for the new keyword, with a comment line like the earlier re-pins.

Proof: PROVEN test_the_gate_gives_an_edit_hook_the_calls_params_not_its_diff, test_the_gate_redacts_the_params_it_falls_back_to (both fail with KeyError: 'tool_input' when the gate ignores the params; the other two pass either way, as guards). Done by hand with the params fallback disabled, not with prove.py.

Ran locally: test_kas_spec_hook_gaps.py, test_kas_spec_hook_gaps_win_retry.py, test_tool_permission.py, test_hooks_composition_contract.py, test_crew_fired_spec_hooks.py, test_taskrunner_deny_notice.py, test_run_bindings.py, test_mirror_spec_hooks.py, test_script_hooks.py, test_subagent_refused_turn_resume.py: 805 passed. black --check and flake8 clean on the changed files.

Manual verification

Not yet done end to end. The live symptom and hook stderr above are from 0.12.0. I have not run a patched gateway to confirm that raw_tool_params is populated on a real kas edit permission request. The raw_params_cache comment in session_update_parser.py says the refinement's rawInput is cached for exactly this event. A maintainer check on a live kas subagent edit would close this.

A subagent or task-runner permission request hands its PreToolUse hooks
the event's rendered tool_input. For an edit call that rendering is a
unified diff for the approval row, not JSON, so the hook's stdin carried
no tool_input at all and a guard keyed on the edit's path refused every
edit.

When the rendering is not a JSON object, the gate now passes the call's
structured params (AcpEvent.raw_tool_params), credential-redacted like
the rendering they replace. A JSON rendering is used as before.
@famanson
famanson requested review from a team and KiroCrewCaptain as code owners November 7, 2026 08:26
@famanson
famanson requested a review from tim-finnigan November 7, 2026 08:26
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Nov 7, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

This PR and #32830 make the same fix. Each one builds a PreToolUse hook's tool_input from the call's structured params, not from the parsed rendering. Both edit permission_pre_tool_block in hook_runtime/hook_dispatch.py and pass event.raw_tool_params from SpecHooks.refuse in tool_permission.py. Both also re-export the helper from hooks.py and re-pin _BASE_SHAPE_DIGEST. #32830 also covers the other hook callers, so the two PRs conflict on the same seam.

Please keep one of #32219 and #32830 and close the other, carrying over anything only this one has, for example the kas edit case in test_kas_spec_hook_gaps.py.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 8, 2026
…tool-input

# Conflicts:
#	src/kiro_crew/hooks.py
#	test/test_hooks_composition_contract.py
@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Nov 8, 2026
@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Nov 9, 2026
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 12, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: Approve

Why: the decider estimates a 12% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes.

This fixes a PreToolUse spec hook getting no tool_input for an edit call. The edit's tool_input is the diff shown in the approval row, so it fails JSON parsing. Now, when that string isn't a JSON object, the gate falls back to the call's structured params (raw_tool_params), with every key and string value credential-redacted. The subagent and task-runner paths both reach the gate through SpecHooks.refuse, and both forward the new keyword with *a, **kw. The events on these paths are AcpEvents, which always have raw_tool_params. The hooks can only deny, and the params come from the same sources as the existing rendering (tool-call cache, then the frame's inline copy), so no trust boundary is weakened. The redaction matches the rendering's. The new tests fail without the change, and the pinned call-argument tests cover the SpecHooks wiring. CI ran no test, lint or build jobs because the fork's workflows were awaiting maintainer approval; the only failing check (Sensitive Change Review) failed for a missing reasoning template. The author reports 805 local tests passing.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit f4d1498b8c · 112 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@iamwhatever

Copy link
Copy Markdown
Contributor

Stale-PR cleanup note: this PR and #32830 fix the same seam (a PreToolUse hook's tool_input built from raw_tool_params), as flagged in the earlier Captain comment. #32830 also covers the other hook callers and is still active, so a maintainer should pick one to keep. Both are fork PRs that touch sensitive paths, so the kept one also needs a maintainer takeover on a branch of this repo to pass Sensitive Change Review. Suggestion: keep #32830 and carry over this PR's kas edit regression test (test/test_kas_spec_hook_gaps.py). Thanks @famanson!

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.

3 participants