Repository navigation
fix(agents): enforce PreToolUse deny on subagent/task-runner paths - #7843
aniruddhaadak80 wants to merge 2 commits into
Conversation
Audit note — part of this has already landed; the rest has notExtends the PreToolUse script-hook deny gate from the dashboard chat path to the two autonomous execution paths. hooks.py gains ScriptHookResult.has_verdict/verdict (exit 0 = allow, 2 = deny), module helpers pretooluse_should_block / _hook_fail_open / _pretooluse_block_reason / _should_block_results, and a new per-hook ScriptHook.fail_open field (default False = fail closed) wired through from_dict, validate_hook_fields, ScriptHookStore.create and .update. dashboard/chat_runner.py's inner _fire() is refactored onto the shared predicate plus per-hook fail_open. subagent_manager/run.py and task_executor.py stop calling the informational fire_tool_hooks on EVENT_TOOL_CALL and instead run HookManager.on_tool_call governance (POLICY ∩ PROFILE) plus a fail-closed PreToolUse hook_store.fire gate before execution, and also add a PreToolUse hook gate on the EVENT_PERMISSION_REQUEST path, logging outcome=hook_blocked / hook_error to SEL instead of hardcoded auto_approved. 4 files, +444/-82, no tests and no docs touched. Partly covered by 7422 (MERGED_PR, coverage PARTIAL)#7422 (landed as ac700e0 on 2026-09-01) already put the fail-closed non-verdict rule on ONE of the three call sites — the dashboard chat gate in dashboard/chat_runner.py's inner _fire(). #7843's chat_runner hunk deletes exactly the block #7422 added (the 'Fail closed. A PreToolUse hook has a two-valued contract' comment and the
Checked against
|
Open PR relationship auditThis is a consolidated, point-in-time code-level audit note. It compares complete merge-base diffs and current/merged code; it does not treat a shared topic as duplication or partial coverage as completion. Relationship findings
No PR, Issue, label, branch, or review state was changed by the relationship-note portion of this audit. |
3ce8acd to
0ae08d2
Compare
0ae08d2 to
112a466
Compare
112a466 to
adbbcbb
Compare
adbbcbb to
810b98f
Compare
|
Rebased onto main in 810b98f (conflict was import drift in subagent_manager/run.py: kept both the upstream persistence imports and the runtime hooks import). CI re-running. Remaining from the audit: tests for the new autonomous gates + fail_open field, and the coverage gap - tracking as follow-up work. |
First Principles Review (Fable 5, fork) — 🔴 BLOCKPremise-level review of First-Principles-Verdict: BLOCK The new permission-path gate re-implements Not justified as shipped
What this change shipsInventory (10 items) — 5 justifiedIntent: make the operator's PreToolUse deny hooks actually reject tools on autonomous subagent/task-runner runs — a FIX (new tests fail on base), though the description text covers only CI fixups.
BlockersDuplicate gate on the subagent/task-runner permission paths. Grepped [FIRST-PRINCIPLES-REVIEWED] cb47aba Description read[DESCRIPTION-READ] 16bc172b74849d9289f4f6f9f969331962205dfdc76dd5540a69d6ba6eb76f8b That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Fable 5, fork) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS New permission gate runs parallel to the existing Watch
[DESIGN-REVIEWED] cb47aba Description read[DESCRIPTION-READ] d69907643d8187d4f2ea974234359c6f32c8467193d731c68a094a291768ea56 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
f56389c to
836de03
Compare
|
CI diagnosis on head 836de03 (run 35210278946 — no fix pushed, nothing in these failures comes from this PR): Backend Tests (3.12, 3) went 27949 passed / 1 failed; the single failure is test_pod_windows_run.py::test_public_up_prepares_home_only_after_start_reservation[False], which dies on Linux with WindowsTaskError (Task Scheduler backend is win32-only). That file carries no OS markers and was last touched upstream by #10841 — unrelated to this seam change (all hook-deny/subagent tests pass). It needs a windows_only marker (or equivalent per testing-conventions) from whoever owns that file. Coverage Gate fail is a pure cascade (backend-test=failure, failing closed) — not a coverage number. GPT 5.6 Review fail is an advisory verdict; please tell me if you want anything changed for it. PR Readiness blockers are the fork approval-gated workflows, same as before. |
|
CI diagnosis update on the single Backend Tests (3.12, 3) failure - this is an upstream test-isolation defect, not caused by this PR diff (which touches only chat_runner/hooks/subagent/task_executor/test_subagent). Chain: test_pod_windows_run.py::test_public_up_prepares_home_only_after_start_reservation[False] drives the public up verb; runtime.require_backend() reads runtime.IS_WINDOWS (mocked True in the model fixture); win_backend.require_backend() reads windows.IS_WINDOWS (never mocked) and raises WindowsTaskError at src/kiro_crew/pod/windows.py:178, translated to PodError at runtime.py:1031. IS_WINDOWS is defined in platform_compat and imported into both namespaces, so patching only rt.* is exactly the silent-no-op shape documented in testing-conventions (Patch the defining module, not a re-export). Repair belongs in a separate upstream PR (one-change-per-PR; unrelated to this diff): the model fixture needs monkeypatch.setattr(win, IS_WINDOWS, True), plus a stub for the later schtasks probe stages (schtasks_bin/_PROBE_OK) since Linux CI has no schtasks.exe. Happy to open that as a follow-up if useful. Coverage Gate / PR Readiness fails are cascade from this one red; GPT review is advisory. |
…kirodotdev#9605) `bind_component_globals` rebuilds every `*_impl` on the manager component types with `FunctionType(code, vars(subagent))`. The function keeps its own code but runs on `subagent.py`'s globals. So an import at the top of the module that defines it does nothing for it. The name has to exist in `subagent.py`. Nothing checked that. A missing name raises `NameError` only when its line runs. Several of these implementations load a global inside a `try:` whose `except Exception` denies, so the miss looks like a gate that refuses everything instead of an error. kirodotdev#7843 hit this twice in five days. This adds a test that walks the bytecode of every rebound `*_impl`, collects each global it loads (nested bodies included), and asserts the name resolves in `subagent.py` or in builtins. Bytecode, not `co_names`: `co_names` also holds attribute names, which would report dozens of names that are not globals. Current `main` is clean, so this is a regression test, not a code change. The docstring on `bind_component_globals` now states the consequence a reader has to know before adding an import to one of those modules. Fixes kirodotdev#9534
iamwhatever
left a comment
There was a problem hiding this comment.
Review of 836de035b4 — needs changes (review-only, nothing pushed)
Read-only review against origin/main @ b83100d136, with tests and mutations run in a throwaway worktree at this head. The shape and file set are right; seven things block it as it stands.
1. The task-runner half is inert on the path it targets. set_global_hook_store is called only from dashboard/server.py and dashboard/handlers/hooks.py. cli_server.py builds TaskRunner with a HookManager but never constructs a ScriptHookStore, so get_global_hook_store() returns None there and the new if _store is not None: gate skips silently. The dashboard treats a missing store on PreToolUse as BLOCK ("BLOCKED:system:hook store not initialized"), so this is also a parity gap, not only a coverage one.
2. The gated events carry no tool input. AcpEvent.shell_command's docstring in acp/types.py states that on permission_request events the params are resolved into tool_input and raw_tool_params is NOT set. Both new gates pass tool_input=event.raw_tool_params. An argument-sensitive deny hook therefore fires with empty input on exactly the event class the PR gates, and allows what the operator wrote the rule to stop.
3. Audit-after-wire breaks an invariant the repo pins with its own test file. test/test_deny_audit_first.py pins "the SEL audit write must precede any wire I/O for that decision", enforced by a source scan bounded to chat_runner. The two new denial branches audit after client.reject_tool (task_executor) and after SubagentManager._reject_and_log (subagent), outside that scan's reach.
4. Duplicate audit rows on the subagent path. _reject_and_log already writes outcome="denied", error="hook_blocked"; the new branch then writes a second row with outcome="hook_blocked". One decision, two rows — and only the second carries subagent_id.
5. The new imports in run.py are dead. _run_inner_impl executes on kiro_crew.subagent's globals (its own comment says so), so the module-level from kiro_crew.hooks import … and from kiro_crew.sel import sel in run.py are inert; commit 2's _COMPONENT_GLOBAL_BINDINGS entries are the live mechanism. Verified by deleting both lines — the new test still passes.
6. Tests cover one arm of three, and not the risk arm. Measured mutations at this head: _should_block_results returning (False, "") fails TestSubagentPreToolUseHookGate (good). _should_block_results returning (True, "…") — deny every tool on every gating event — leaves 1104 tests green. So there is no allow control, no timeout case, no task-runner test, and the new exit-0-with-stderr warning in chat_runner is unpinned. The unpinned arm is the one that breaks operators: a hook that exits nonzero by accident.
7. Stale — the PR's own regression pin no longer passes on main. mergeable: CONFLICTING. A local rebase onto b83100d136 produces one 2-line conflict in run.py's TYPE_CHECKING import list, trivially resolved; after that TestSubagentPreToolUseHookGate fails with ValueError: memory_unavailable: no captured execution context (run.py:948) because main added a precondition the test's SubagentInfo double does not satisfy. The production hunks themselves rebase clean.
Checked and fine
Baseline at this head: test_subagent.py, test_fire_tool_hooks.py, test_hooks.py → 322 passed. The only red CI test is test_pod_windows_run.py::test_public_up_prepares_home_only_after_start_reservation[False], unrelated to hooks; Coverage Gate is red only because backend-test was red. Leaving the auto-approved EVENT_TOOL_CALL branch informational is correct and matches the dashboard — the tool is already running there — so the comment-only change on that branch is the right call.
Two corrections to the ticket's premise (Mesh-3030)
The main-session path already enforces deny: chat_runner._fire blocks on exit 2 and fail-closed on any non-verdict, and _reject_hook_blocked / _reject_hook_error reject and audit. That landed in #7422. A grep for the class name ScriptHookResult misses that consumer because it reads r.blocked / r.exit_code duck-typed.
So the remaining gap is narrower than "subagent hooks never deny": it is the missing script-hook consultation on the permission-request branch of subagent_manager/run.py and task_executor.py. That part is genuinely absent from main and this PR is not superseded.
What would clear this
A rebase, three tests (exit 0 allows, timeout denies, task-runner gate denies), tool_input derived the way the dashboard derives it, the audit moved before the wire write on both new branches, the duplicate SEL row dropped, and a decision on store-absent behaviour — block like the dashboard, or a comment establishing it is unreachable.
836de03 to
cfcc61b
Compare
|
Addressed the review in
Validation: |
|
Rebased onto current Validation: |
Rebased onto upstream main 972675c as a single commit. Two files drifted against main and are resolved by intent rather than by discarding either side: - subagent_manager/run.py — main added the sel and eardown_capture imports into the existing import block; both are kept alongside this branch's hook-gating changes. - docs/system-specs/modules/security.md — main added the paragraph on the two ACP transports repeating the identity-free security floor in �pprove_tool; this branch added the paragraph on task-runner and subagent permission requests passing through the global ScriptHookStore PreToolUse gate. Both paragraphs are kept; the spec now documents the two layers together rather than one replacing the other.
Rebased onto upstream main 972675c as a single commit. Two files drifted against main and are resolved by intent rather than by discarding either side: - subagent_manager/run.py — main added the sel and eardown_capture imports into the existing import block; both are kept alongside this branch's hook-gating changes. - docs/system-specs/modules/security.md — main added the paragraph on the two ACP transports repeating the identity-free security floor in ^Gpprove_tool; this branch added the paragraph on task-runner and subagent permission requests passing through the global ScriptHookStore PreToolUse gate. Both paragraphs are kept; the spec now documents the two layers together rather than one replacing the other. Squashes two CI-gate fixes so this PR stays within the 1-2 commit budget: the black ratchet baseline prunes two entries this branch made black-clean, and the duplicate sel import in subagent_manager/run.py is removed (flake8 F811). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Open-PR triage (2026-10-05): PARTIALLY_COVERED. Needs a REBASE and has one fail-open blockerI checked head What main already covers
The PR is therefore not superseded, and I'm not closing it. Blocker: fails open, a regression against main (raised by the Opus bot and still present at this head)
Other issues still open at this head
Overlap: no other open PR implements this gate. #12643 (open PR) edits the same Recommendation: rebase onto the |
bolichen97
left a comment
There was a problem hiding this comment.
[P0] kirocrew run registers a fail-soft hook store, turning the strict saved-hooks gate fail-open
Class: Security guard turned fail-open (saved PreToolUse hooks).
Mechanism: _run_task now registers a plain ScriptHookStore(), whose load is fail-soft. Because a store is registered, permission_pre_tool_block no longer falls back to the strict persisted_hook_store() read, so an unreadable or malformed hooks.json loads no deny hooks and the call is allowed. On main the same case blocks.
User impact: Under kirocrew run, an operator's deny hooks silently stop applying when hooks.json cannot be read.
Location: src/kiro_crew/cli_server.py:2708 @ cb47abab89fd213e7cda62ea079e5e85a4edb96c
Minimum fix: Drop set_global_hook_store(ScriptHookStore()) from _run_task so the runner keeps the strict read, or register the store built by persisted_hook_store() and fail the run closed if it raises.
How to verify: With a temp KIROCREW_HOME holding a malformed hooks.json, run _run_task's store setup, then assert permission_pre_tool_block(get_global_hook_store(), ...) returns a block reason. It returns None on this head.
Exploit details are left out on purpose. They are available to maintainers through the repo's private security reporting.
Independently re-verified on head cb47abab89 with a focused local reproduction.
|
|
||
| # Opt-out state is sourced from the keystone denied_commands.json, not | ||
| # config.json's hooks section (the agent cannot write the keystone file). | ||
| set_global_hook_store(ScriptHookStore()) |
There was a problem hiding this comment.
[P0] kirocrew run registers a fail-soft hook store, turning the strict saved-hooks gate fail-open — see the review summary for mechanism, fix and test.
|
Thanks for this fix, @aniruddhaadak80! Main now enforces the agent spec's PreToolUse hooks on both the subagent path (src/kiro_crew/subagent_manager/run.py) and the task-runner path (src/kiro_crew/task_executor.py), consolidated in #17220 (commit 8e06ee3). So we're closing this as superseded. If you see a path that still skips the deny, please reopen or file an issue. |
CI fixes for the gates this branch tripped:
Backend Lint & Type Check— the black ratchet gate requires baselined files that have become black-clean to be pruned, so this branch reformattedtest/test_fire_tool_hooks.pyandtest/test_taskrunner_autoapprove.pyand left their entries behind. Both entries are removed from.github/black-baseline.txt.Lint (flake8)—F811 redefinition of unused selinsubagent_manager/run.py: thefrom kiro_crew.sel import selline this branch added shadowed theselthat already arrives through the..subagentimport block. The redundant line is gone;selstill resolves from..subagent, so behaviour is unchanged.PR Hygiene— the two fixes above are squashed into this one commit so the PR stays inside the 1-2 commit budget.Both fixes were verified locally against the pinned toolchain before pushing:
black 26.3.1 --line-length 100reports the two test files clean, andflake8 7.1.0 --select=F811is clear on the edited module. The remaining red checks on this PR are not from this diff —Resolve desktop matrixandPublish readiness signalfail onAPI rate limit exceeded for installation, and the runner queue currently has ~1000 runs waiting.