Skip to content

fix(subagent): refuse tool requests once a stop is in progress - #18955

Closed
javenciu wants to merge 2 commits into
kirodotdev:mainfrom
javenciu:fix/no-tool-approval-after-stop
Closed

javenciu wants to merge 2 commits into
kirodotdev:mainfrom
javenciu:fix/no-tool-approval-after-stop

Conversation

@javenciu

@javenciu javenciu commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A sub-agent that the user has stopped never gets another tool approved, even when its session teardown fails.

A Stop on a running sub-agent tears its session down first and cancels the run task after that (subagent_manager/terminal.py, the reset in _force_reap, then _cancel_task_intentionally). When the run is waiting on a person for a tool approval through the dashboard, ApprovalCoordinator.request turns that cancel into a denial, so the run loop resumes and reads its next event.

When the teardown works, that read hits the dead stream and the run ends. The trigger needs two failures together: the session reset hangs past _RESET_TIMEOUT or raises, AND the SIGKILL fallback after it also fails to stop the process. Then the stream is still live. The backend's next permission request went through the normal ladder, and a grant that answers without a card (slot trust, --approval yolo or reads, auto_approve_sources, parent_policy=auto, a hook grant) approved a tool after the user pressed Stop.

Observed in-process with the real run loop, the real stop and the real approval coordinator, with fakes for the backend, the reset and the kill. The order in every run was: Stop, reset, kill failed, cancel, the approval returns denied, tool A rejected, the backend sends tool B, B is granted, approve_tool is sent for B.

Why it matters

A tool that runs after a Stop is the worst outcome a stop can have: the user pressed Stop to prevent exactly that. The likelihood is low, because it needs both a failed session reset and a failed kill.

Not a goal

  • Changing the cancel swallow in ApprovalCoordinator.request (except (asyncio.TimeoutError, asyncio.CancelledError): return False), or its question twin. That behaviour is documented as intended ("Request interactive approval and deny on timeout or cancellation", DashboardState.request_approval). Re-raising there would change that documented contract for every caller of DashboardState.request_approval, not only sub-agent runs; this fix closes the hole at the one place that approves tools for a sub-agent run.
  • Making the session reset or the kill fallback more reliable. They are what failed, and they keep their own records and audits.
  • The session-sharing stop path (the shared provider's shutdown()). _force_reap sets _reap_started before either branch, so the guard applies there too, but that path was not driven separately.
  • Tools a backend runs without asking for permission. The host cannot gate those, and the teardown is what ends them.

What changed (motivation → approach → change)

  • Symptom: a tool request read after a Stop was settled by the ordinary ladder, so a grant could approve it.
  • Root cause: the permission branch in subagent_manager/run.py never checked whether a stop was in progress. The stop's own ordering (teardown before cancel) was the only thing that kept the request from being read, and that ordering does not hold when the teardown fails. A check made once, at the top of the branch, is not enough either: the branch awaits the diagnostics write and the subagent_tool event, and the ladder awaits the spec hooks, the gate and a person, so a stop can land after the check and before the approval is sent.
  • Change: tool_permission.Policy gains stopping, a live predicate. settle reads it before its first stage (tool_permission.py:410) and before it asks a person (tool_permission.py:437), and _allow reads it at the commit point of every approval (tool_permission.py:521), with no await between that read and wire.allow. A grant, the gate's hit and a person's yes all reach the wire only through _allow, so once the predicate holds the request is answered by tool_permission.bail(..., "stopped"): the SEL row is written with error: "stopped" and the request is rejected bare on the wire. The run passes its predicate (_reap_started, user_stopped or reaped; subagent_manager/run.py:279, wired at subagent_manager/run.py:2079) and also reads it when it reads a request (subagent_manager/run.py:3258), before any floor, grant or person is consulted. "stopped" joins BailReason and the sub-agent refusal rows, so a Stop is recorded as a stop, not as a turn limit or a person's no.
  • The transport's own awaits come after _allow's read: approve_tool still awaits the security floor's thread hop and the stdin write lock before it writes the allow. _allow hands the predicate to the wire (tool_permission.py:524), and AcpWire passes it as withdraw_if to a provider type that declares approval_withdrawable (tool_permission.py:553): AcpProvider and AcpSessionProvider, while any other provider keeps the base approval_withdrawable of False (providers/base.py:119). Once the floor's thread hop returns, both transports read withdraw_if again (acp/session_handle.py:2520, acp/client.py:9979), and once more under the write lock right before the frame is written (acp/session_handle.py:2547, acp/client.py:10001). The shared runtime reads it as withdrawal_check, ahead of its own liveness check (acp/runtime.py:5447), and keeps a caller's before_write, where a timed grant's deadline goes, after that check (acp/runtime.py:5450): a runtime that died while the answer waited for the lock raises AcpRuntimeDead, so the chat path takes the session-reset recovery instead of asking a person about an ended grant. AcpClient has no liveness check under its lock and reads the withdrawal in its before_write, ahead of the deadline. Once it holds, no byte of the allow is written, the request's records are put back, and permission_floor.ApprovalWithdrawn makes _allow bail the request as stopped (tool_permission.py:525), answered once with the agent's own reject option.
  • The stop bail ends the run, as the two limit bails do: the loop breaks after the bail (subagent_manager/run.py:3267) and after a settle the ladder bailed as stopped (subagent_manager/run.py:3356), and the stream's own ending arms record the stop (with _reap_started set, the reap's ending: subagent_manager/run.py:3621). A person's no that a stop's cancel caused reads on, so a stream the stop tore down still ends at its next read, as before.
  • docs/system-specs/modules/subagent.md and docs/system-specs/common/injected-messages.md list the new bail beside the two limit bails, and the first says where the stop is read and what ends the run; docs/system-specs/modules/acp-client.md documents withdraw_if.

Backwards compatibility

Compatible: a run with no stop in progress takes exactly the path it takes today; the new branch runs only for a run that is already being stopped, and it answers each request through the existing bail. An approval without withdraw_if reaches the transport exactly as before: each layer passes the keyword on only when it is given.
Removes nothing: the change adds one refusal rung, one optional Policy field (stopping, default None, so the task runner's ladder takes its path unchanged) and the reads of it, the optional keywords withdraw_if (approve_tool on both providers and both transports) and withdrawal_check (AcpRuntime.send_response and its protocol), the class attribute approval_withdrawable (default False) and permission_floor.ApprovalWithdrawn; no key, row, rung, path or public function is removed or renamed.

Tests

In short: Red and green, pytest -n 0, 3 of 3 serial runs each. This round, rebased on origin/main ac8a96a: the new runtime ordering test fails 3 of 3 on the previous head a712e6f (died raises GrantEnded) and passes 3 of 3 here; 20 more -n 0 passes and 10 -n 4 passes of its file, no failure. The new transport test on this PR's previous head 671151e: 4 failed, 2 passed (the no-stop controls); each failure is an allow_once frame written after the Stop. The PR's two stop test files copied onto origin/main 10350b7, run with the deny-notice suite: 26 failed, 190 passed. On this branch the three changed test files: 217 passed. Then the 425 other test files that import a changed module (test/test_acp_client.py left out: it builds paths under the real home directory), run with -n 16: 24,737 passed, 66 skipped, 19 failed. The 19 are in test/test_acp_spawn_offload.py, test/test_auto_improvement_members.py and test/test_native_skill_projection.py, and the same 19 fail on origin/main 10350b7 with the same command.

  • test/test_subagent_stop_refuses_permission.py, new, driving the real SubagentManager run loop, cancel, _force_reap and ApprovalCoordinator.request against a fake backend:
    • test_a_stop_whose_teardown_fails_approves_no_further_tool[hangs] and [raises]: the reset hangs or raises and the kill fallback fails. B is rejected, no grant is consulted, no approve_tool is sent after the Stop, B's SEL row is denied / stopped, and the run ends right after B is rejected: request C is never read. On main approve_tool:102 and approve_tool:103 are sent after the Stop; with this PR's first commit alone, C was still read.
    • test_a_stop_landing_before_the_answer_bails_the_request, 13 cases: what would approve B (parent_policy=auto, the gate's auto-approve, a person's yes) times the await the Stop lands in (the stream, the diagnostics write, the subagent_tool event, the gate, the person's wait), with the reset held so the stop stays in progress. B ends in the audited stopped bail, no person is asked when the stop landed before the ask, and C is never read. On main all 13 send approve_tool:102 after the Stop; with this PR's first commit alone, 10 do, and the 3 stream cases read C.
    • test_a_stop_whose_teardown_works_ends_the_run_before_the_next_request and test_a_request_with_no_stop_is_settled_as_before: controls; both pass on main.
  • test/test_tool_permission.py: test_a_stop_read_at_entry_bails_before_any_stage, and test_a_stop_landing_in_any_stage_bails_the_request_not_an_approval[floor|gate|grant|person], where each policy would approve and the stop lands while the named stage runs. test_every_subagent_bail_is_audited_and_bare still covers the rung through BailReason.
  • test/test_tool_permission.py: test_a_stop_landing_in_the_transport_awaits_writes_no_approval[shared|dedicated-floor|write_lock|none] drives settle through AcpWire and the real providers and transports (AcpSessionProvider over AcpSessionHandle and AcpRuntime; AcpProvider over AcpClient) into a fake backend's stdin. A grant approves, and the Stop lands on the floor's worker thread or while the approval waits for the write lock behind another frame. The one frame written is the agent's reject_once, the SEL row is stopped, no approval row is written, and after the floor only the reject takes the lock. The two none cases are controls: one allow_once.
  • test/test_transport_permission_floor.py: test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait[died|alive|died-and-withdrawn] holds a real AcpRuntime's write lock, lets a timed grant end while the approval waits for it, and in died marks the runtime dead in the same wait (_mark_dead, as a sibling's stall does). died raises AcpRuntimeDead, alive raises GrantEnded, died-and-withdrawn raises ApprovalWithdrawn, and nothing is written in any case. On this PR's previous head a712e6f, died fails 3 of 3 with GrantEnded: 52; on this head all three pass 3 of 3. Moving before_write back ahead of the liveness check fails only died; moving the withdrawal after it fails only died-and-withdrawn.
  • test/test_eval_subagent_deny_notice.py: the run's ladder-entry count stays {"settle": 1, "bail": 3}.
  • Each new read is load-bearing: with any one removed (the settle entry read, the read before a person is asked, the _allow commit read, the run's break after a stopped settle), 7, 2, 6 and 10 of the tests above fail. In the transports, with the read after the floor removed, the read under the lock, or the put-back of the records, 2, 2 and 2 of the transport cases fail.

The new file's autouse _pinned_data_home pins KIROCREW_HOME and KIROCREW_WORKSPACE through the isolation floor's own _floor_monkeypatch, and every wait in it is bounded by the file's _SETTLE_SECS.

E       AssertionError: ['STOP', 'reset', 'kill_failed', 'cancel', 'approval_returned:101:False', 'reject_tool:101', ...]
E       assert not ['approve_tool:102', 'approve_tool:103']
E       assert 'approve_tool:102' not in ['STOP', 'reset', 'approve_tool:102', 'backend_sends:103', 'approve_tool:103', 'kill_failed']

mypy (the nine changed source files), flake8, isort, black (no changed file is baselined), scripts/check_black_formatting.py, scripts/check_subprocess_encoding.py, COMMENT_HISTORY_BASE_REF=origin/main scripts/check_comment_history.py, ./scripts/docs-lint.sh and test/test_wait_discipline_ratchet.py (39 passed, on this head over origin/main 10350b7): all exit 0.

This round, on origin/main ac8a96a, -n 0 in one process: the PR's test files, test/test_transport_permission_floor.py, the six test/test_acp_runtime*.py files and the three test/test_chat_runner*.py files that cover approvals, 1,503 passed. The 69 test files that name acp/runtime.py, acp/session_handle.py or chat_runner.py (-n 8): 8,824 passed, 2 skipped. The wait-discipline, flake-pattern, pid-reader, tmpdir-helper and dir-fd ratchets: 127 passed. mypy on the two changed source files, flake8, isort, black, scripts/check_agent_sdk_boundary.py, scripts/check_black_formatting.py, scripts/check_subprocess_encoding.py, COMMENT_HISTORY_BASE_REF=ac8a96a242 scripts/check_comment_history.py and ./scripts/docs-lint.sh: all exit 0.

Manual verification

N/A: the trigger needs a session reset and a kill that both fail, which the unit tests drive with fakes; no real process is signalled.

Related Issues

None found. Searched open and merged PRs and issues for "approval CancelledError", "stop approval subagent tool", "swallowed cancel approval" and "tool after stop".

Pattern harvest

Rule candidate: review-prompt
Pattern: a stop that relies on its own ordering (tear down, then cancel) to keep a loop from acting, with no check of the stop flag at the point where the loop grants or executes work.

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

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 10, 2026 22:11
@javenciu
javenciu requested a review from bolichen97 October 10, 2026 22:11
@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 Oct 10, 2026
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision 3877d64d29644b6ba00c5aaba128375827e14b59 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of 3877d64d29644b6ba00c5aaba128375827e14b59 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The transport-level withdrawal (withdraw_if, ApprovalWithdrawn, approval_withdrawable, withdrawal_check) adds permanent surface across six files. It closes a millisecond window inside a defect the author already calls doubly improbable.

Not justified as shipped

  • Item 3 — oversized: the description says the defect "needs both a failed session reset and a failed kill". This item narrows it further, to a stop that lands during the floor's thread hop or the write-lock wait, after the commit-point read in _allow (if _stopping(policy): return await bail(...)). For that it adds a kwarg on 4 approve_tool methods, on send_response and AcpRuntimeProtocol, plus a new exception, a class attribute and two _restore_withdrawn helpers. All of it serves 1 consumer: AcpWire, fed only by subagent_manager/run.py (Policy( grep shows 2 constructions, and only run.py passes stopping).
    Subtraction: delete withdraw_if/withdrawal_check, ApprovalWithdrawn, raise_if_withdrawn, approval_withdrawable and both _restore_withdrawn helpers. That leaves the run-loop bail and the ladder's three live reads.
    Clears when: a human accepts the cross-transport surface for this window, or the transport half is removed.

What this change ships

Inventory (5 items) — 4 justified

Intent: once a user stops a sub-agent, it never gets another tool approved, even when teardown fails. FIX.

  1. A stopped sub-agent's next tool request is refused and the run ends — justified
  2. The permission ladder re-checks the stop at entry, before asking a person, and at approval commit — justified
  3. ACP transports drop an approval if the stop lands during floor hop or write-lock wait — oversized
  4. The audit/SEL records a new stopped refusal reason — justified
  5. Spec docs list the stop bail and document withdraw_if — justified

[FIRST-PRINCIPLES-REVIEWED] 3877d64

Description read

[DESCRIPTION-READ] a2439ce67f58641c5b8cdb571852ba71af88cf1b87f4d8668cfd021ec92bcb2c

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 3877d64d29644b6ba00c5aaba128375827e14b59 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check before merge: the session-sharing stop path (shutdown() on a shared provider) was never driven end to end, so confirm _reap_started is set before that path's awaits.

Suggestions

  • The withdraw_if / withdrawal_check / approval_withdrawable plumbing touches two providers, two transports and the runtime protocol to cover a window of a few milliseconds (the floor's thread hop and the write-lock wait). The run-loop check plus the check in _allow already close the hole the description reports. Splitting the transport half into its own PR would let it be judged on its own value and keep this fix small enough to revert cleanly.

[DESIGN-REVIEWED] 3877d64

Description read

[DESCRIPTION-READ] 8dda611e99a919f9ba57923f334616f872588cb6607465c25e1b624b81342a93

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 3877d64d29644b6ba00c5aaba128375827e14b59 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 3877d64

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed 3877d64d29644b6ba00c5aaba128375827e14b59 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 3877d64

@javenciu
javenciu force-pushed the fix/no-tool-approval-after-stop branch from f1b91bb to 2008a13 Compare October 11, 2026 10:00
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 11, 2026
@javenciu
javenciu force-pushed the fix/no-tool-approval-after-stop branch from 2008a13 to 503e3d0 Compare October 12, 2026 10:55
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu
javenciu force-pushed the fix/no-tool-approval-after-stop branch from 503e3d0 to e84bd3e Compare October 12, 2026 14:49
@javenciu

Copy link
Copy Markdown
Contributor Author

Amended the PR's one commit, in test/test_subagent_stop_refuses_permission.py only: the 10 s settle ceiling is now 30 s and the two 10 ms poll loops wait with async_wait_until, the form the repo's wait-discipline ratchet (test/test_wait_discipline_ratchet.py) accepts.
The file counted 3 short fixed waits against a baseline of 0 and counts 0 again; the ratchet passes on the head merged with main (c4c51aa), and the file passes 3 of 3 runs (4 tests each).

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@javenciu
javenciu force-pushed the fix/no-tool-approval-after-stop branch from e84bd3e to 671151e Compare October 12, 2026 21:15
@javenciu

Copy link
Copy Markdown
Contributor Author

671151e answers each point; the branch is rebased onto main 65df088 and is still two commits.

GPT: Stop still permits approval of an already-dequeued request (span 3b96d9d7798e). Fixed at the commit point, for every approver. Policy.stopping is a live predicate the run passes (subagent_manager/run.py:2012). settle reads it before its first stage (tool_permission.py:402) and before it asks a person (tool_permission.py:429), and _allow reads it at the commit point of every approval (tool_permission.py:511), with no await between that read and wire.allow. A grant (parent_policy=auto), the gate's hit and a person's yes reach the wire only through _allow. A stop that landed in any earlier await (the diagnostics write, the subagent_tool event, the spec hooks, the gate, a person's wait) therefore ends in the audited stopped bail. The transport floor in approve_tool runs after that commit, and it can only turn an approval into a rejection. Test: test_a_stop_landing_before_the_answer_bails_the_request covers 13 cases: auto, gate or person, crossed with the Stop landing in the stream, the diagnostics write, the tool event, the gate or the person's wait. The reset is held so the stop stays in progress. On main all 13 send approve_tool:102 after the Stop. On the previous head 10 do, and the 3 stream cases read the next request. On this head B is rejected with a denied / stopped row in all 13, 3 of 3 runs. The ladder's reads are also pinned at its interface (test_a_stop_read_at_entry_bails_before_any_stage, test_a_stop_landing_in_any_stage_bails_the_request_not_an_approval[floor|gate|grant|person]). Removing any one of the three ladder reads, or the run's new break after a stopped settle, fails 2 to 10 of these tests.

Design: a stopped run with a live stream keeps running (span 56a8325fbe5c). The stop bail now ends the run, as the limit bails do. The loop breaks after it (subagent_manager/run.py:3193) and after a settle the ladder bailed as stopped (subagent_manager/run.py:3275). The stream's ending arms then record the stop (with _reap_started set, the reap's ending: subagent_manager/run.py:3527). Test: test_a_stop_whose_teardown_fails_approves_no_further_tool[hangs|raises] now sends a third request C. The run ends right after B is rejected, and C is never read. On the previous head C was read. The spec paragraph in docs/system-specs/modules/subagent.md now says what ends the run.

Design suggestion: one check where the swallowed cancel resumes the loop (span a8e0f52a2122). Taken, where it applies: the run checks right after the approval returns (after settle), and the ladder checks inside. There is no check at the top of every event. The approval wait is the only await in the loop that turns a cancel into a denial; elsewhere a cancel ends the run as CancelledError, and text and tool-call events approve nothing. A person's no caused by a stop's cancel still reads on, so a stream the stop tore down ends at its next read, as before (test_a_stop_whose_teardown_works_ends_the_run_before_the_next_request).

First principles: the loop continues after the bail. Same change as the Design point above: the loop now breaks.

CI. The two backend shards that failed on the previous head failed in test/test_provider_spawn_off_loop_18172.py::TestBoundsAndCleanupArePreserved::test_cancellation_during_the_spawn_still_reaps (fixed on main by #19358) and in the test/test_pid_reader_ratchet.py::test_every_needle_finds_a_site timeout (fixed on main by #19430). Both fixes are in the new base.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 12, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 13, 2026
@javenciu
javenciu enabled auto-merge (squash) October 13, 2026 04:19

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR makes a sub-agent run that is being stopped refuse every further tool request, even when its session teardown and the kill fallback both fail. It does this with a live Policy.stopping predicate that the run loop, settle, the _allow commit point and, through withdraw_if, both ACP transports all read. The run-loop and ladder reads fix the root cause, and I could not get a grant, a gate hit or a person's yes past them: there is no await between _allow's read and wire.allow, and the only Wire implementations (AcpWire and the test fake) accept the new argument. One regression remains, and it is on the existing chat path, not on sub-agents. In src/kiro_crew/acp/runtime.py:5448, checks() now runs the caller's before_write before _refuse_write_if_dead(). For a timed grant, that caller check is grant_check, so an expired grant on a runtime that died during the lock wait now raises GrantEnded instead of AcpRuntimeDead. dashboard/chat_runner.py:14589 then sets approval_sent = None and shows a person an approval card for a dead runtime, so session-reset recovery waits a whole approval window. This is the Opus lane's advisory finding on a712e6f, and no disposition has answered it. Suggested fix: run only the withdrawal ahead of the liveness check. For example, in session_handle.approve_tool, pass the runtime a before_write that runs raise_if_withdrawn and leaves grant_check until after _refuse_write_if_dead, or split the hook into pre-liveness and post-liveness parts, and add a test that a dead runtime plus an expired grant still raises AcpRuntimeDead. Please also post a disposition for the First Principles CONCERNS item (the transport-level withdraw_if layer). — crew-pr-reviewer (deep re-check)

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 13, 2026
A Stop tears the sub-agent's session down first and cancels the run
task after that. The dashboard approval wait turns that cancel into a
denial, so the run loop resumes and reads its next event. When the
session reset hangs or raises and the kill fallback also fails, the
stream is still live, and the next permission request went through the
normal ladder: a grant such as slot trust or --approval yolo approved
a tool after the user pressed Stop.

The permission branch now bails every request as "stopped" once
_reap_started, user_stopped or reaped is set, before any grant or
person is consulted. The bail is the ladder's own: the SEL row is
written with error "stopped" and the request is rejected bare on the
wire. "stopped" joins BailReason and the sub-agent refusal rows, and
the run's ladder-entry ratchet counts the new bail.

The cancel swallow in ApprovalCoordinator.request is unchanged; it is
documented as intended (deny on timeout or cancellation).
The stop check at the top of the permission branch ran once, before the
branch's own awaits (the diagnostics write and the subagent_tool event)
and before the ladder's (the spec hooks, the gate, a person's wait). A
Stop landing in any of them, while its session reset was still hanging,
let the request reach the wire as an approval: parent_policy=auto, the
gate's auto-approve or a person's yes sent approve_tool after the Stop.

Policy gains a live stopping predicate. settle reads it before its first
stage and before it asks a person, and _allow reads it at the commit
point of every approval. Once it holds, the request goes through the
same audited stopped bail. The run passes its predicate (_reap_started,
user_stopped or reaped).

_allow also hands the predicate to the wire, because approve_tool still
awaits the security floor's thread hop and the stdin write lock before
it writes the allow. AcpWire passes it as withdraw_if to a provider type
that declares approval_withdrawable (AcpProvider, AcpSessionProvider),
and both transports read it after the floor and under the write lock,
right before the frame is written: AcpClient through its before_write
check, the shared runtime through a withdrawal_check it runs ahead of
its own liveness check. Once it holds, no byte is written, the request's
records are put back, and ApprovalWithdrawn makes _allow bail the
request as stopped: audited, and answered once with the agent's reject.
A timed grant's deadline stays in the runtime's before_write, which runs
after the liveness check, so a runtime that died during the lock wait
still raises AcpRuntimeDead rather than GrantEnded.

The stop bail now ends the run, as the limit bails do: the loop breaks
instead of continuing, after the bail at the top of the branch and after
a settle the ladder bailed as stopped, and the stream's own ending arms
record the stop. A person's no that a stop's cancel caused still reads
on, so a stream the stop tore down ends at its next read, as before.
@javenciu
javenciu force-pushed the fix/no-tool-approval-after-stop branch from a712e6f to 3877d64 Compare October 13, 2026 18:48
@javenciu

Copy link
Copy Markdown
Contributor Author

Thanks for the deep re-check. The new head is 3877d64, rebased onto origin/main ac8a96a with no conflict, still two commits.

  1. The regression. checks() in AcpRuntime._write_response_bounded ran the caller's before_write ahead of _refuse_write_if_dead(), so on a timed grant grant_check raised GrantEnded for a runtime that died during the lock wait. I took your second shape and split the hook. send_response keeps before_write after the liveness check, as on main, and gains withdrawal_check, which is read ahead of it (src/kiro_crew/acp/runtime.py:5443-5451, forwarded at :5682, protocol at src/kiro_crew/acp/session_handle.py:1058). approve_tool passes the withdrawal as withdrawal_check and the grant's guard as before_write (src/kiro_crew/acp/session_handle.py:2547, :2549). The order under the lock is now withdrawal, liveness, deadline. chat_runner.py is unchanged: a dead runtime reaches it as AcpRuntimeDead again, not as the GrantEnded it catches (src/kiro_crew/dashboard/chat_runner.py:14763). AcpClient has no liveness check under its lock, so its single before_write (withdrawal, then deadline) is unchanged.
  2. The test. test/test_transport_permission_floor.py::test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait[died|alive|died-and-withdrawn] holds a real runtime's write lock, lets the grant end while the approval waits, and in died marks the runtime dead in the same wait. A dead runtime raises AcpRuntimeDead, a live one GrantEnded, and a withdrawn approval on a dead runtime ApprovalWithdrawn. On a712e6f died fails 3 of 3 (GrantEnded: 52); on 3877d64 all three pass 3 of 3, plus 20 -n 0 and 10 -n 4 runs with no failure. Moving before_write back ahead of the liveness check fails only died; moving the withdrawal after it fails only died-and-withdrawn.
  3. First Principles. The transport-level withdraw_if layer (span bcc96c0ead6d) has its own disposition, rebutted: the lock wait it covers is bounded by write progress, not by elapsed time (src/kiro_crew/acp/transport_framing.py:112-124). Item 4 (the reorder) and the Design punchline have their own dispositions, fixed in 3877d64. The Opus advisory finding names the same line and is answered by point 1.

The body's transport bullet, the Removes nothing line and the Tests section are updated for the split; Goal, Why it matters and Not a goal are unchanged.

1 similar comment
@javenciu

Copy link
Copy Markdown
Contributor Author

Thanks for the deep re-check. The new head is 3877d64, rebased onto origin/main ac8a96a with no conflict, still two commits.

  1. The regression. checks() in AcpRuntime._write_response_bounded ran the caller's before_write ahead of _refuse_write_if_dead(), so on a timed grant grant_check raised GrantEnded for a runtime that died during the lock wait. I took your second shape and split the hook. send_response keeps before_write after the liveness check, as on main, and gains withdrawal_check, which is read ahead of it (src/kiro_crew/acp/runtime.py:5443-5451, forwarded at :5682, protocol at src/kiro_crew/acp/session_handle.py:1058). approve_tool passes the withdrawal as withdrawal_check and the grant's guard as before_write (src/kiro_crew/acp/session_handle.py:2547, :2549). The order under the lock is now withdrawal, liveness, deadline. chat_runner.py is unchanged: a dead runtime reaches it as AcpRuntimeDead again, not as the GrantEnded it catches (src/kiro_crew/dashboard/chat_runner.py:14763). AcpClient has no liveness check under its lock, so its single before_write (withdrawal, then deadline) is unchanged.
  2. The test. test/test_transport_permission_floor.py::test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait[died|alive|died-and-withdrawn] holds a real runtime's write lock, lets the grant end while the approval waits, and in died marks the runtime dead in the same wait. A dead runtime raises AcpRuntimeDead, a live one GrantEnded, and a withdrawn approval on a dead runtime ApprovalWithdrawn. On a712e6f died fails 3 of 3 (GrantEnded: 52); on 3877d64 all three pass 3 of 3, plus 20 -n 0 and 10 -n 4 runs with no failure. Moving before_write back ahead of the liveness check fails only died; moving the withdrawal after it fails only died-and-withdrawn.
  3. First Principles. The transport-level withdraw_if layer (span bcc96c0ead6d) has its own disposition, rebutted: the lock wait it covers is bounded by write progress, not by elapsed time (src/kiro_crew/acp/transport_framing.py:112-124). Item 4 (the reorder) and the Design punchline have their own dispositions, fixed in 3877d64. The Opus advisory finding names the same line and is answered by point 1.

The body's transport bullet, the Removes nothing line and the Tests section are updated for the split; Goal, Why it matters and Not a goal are unchanged.

@javenciu

Copy link
Copy Markdown
Contributor Author

span=bcc96c0ead6d

rebutted: the transport's window is not milliseconds. The allow waits for the shared runtime's write lock behind whatever frame another session is writing, and that wait is bounded by the reader's progress, not by elapsed time: a healthy backend draining a co-tenant's multi-MB prompt frame keeps it alive as long as each 5 s window removes 4 KiB (src/kiro_crew/acp/transport_framing.py:112-124 and :133-142; 900 s where the level cannot show progress, :152).
A Stop pressed during that wait is pressed before any byte of the allow exists, while the request is still pending, so it can still be answered with the agent's reject; after the write it cannot. That is the line this layer draws, and only the transport sees the moment the allow is written. The ladder's reads decide whether to approve; the transport's reads (src/kiro_crew/acp/session_handle.py:2520 after the floor's thread hop and :2547 under the lock, src/kiro_crew/acp/client.py:9979 and :10001) decide whether an approval the user has since stopped is written.
The layer answers the GPT lane's block on head 671151e (the transport awaits after _allow's read). On that head, before the layer existed, test_tool_permission.py::test_a_stop_landing_in_the_transport_awaits_writes_no_approval failed its floor and write_lock cases on both transports, 4 of 6, each writing allow_once after the Stop. Its write_lock case holds the lock the way a sibling's frame does.
Item 4 of the same verdict, the reorder that rode along, is fixed in 3877d64: only the withdrawal is read ahead of the runtime's liveness check.

@javenciu

Copy link
Copy Markdown
Contributor Author

fixed in 3877d64: an expired grant on a shared runtime that died during the lock wait raises AcpRuntimeDead again and recovers. Only the withdrawal is read ahead of the liveness check (src/kiro_crew/acp/runtime.py:5447); the grant's deadline is read after it (:5450).
test/test_transport_permission_floor.py::test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait pins all three orders; [died] fails 3 of 3 on a712e6f and passes 3 of 3 on 3877d64.

@javenciu

Copy link
Copy Markdown
Contributor Author

span=47d4f3e83df1

fixed in 3877d64: the runtime reads only the withdrawal ahead of its liveness check (withdrawal_check, src/kiro_crew/acp/runtime.py:5447); a caller's before_write, where a timed grant's deadline goes, runs after _refuse_write_if_dead again, as on main (src/kiro_crew/acp/runtime.py:5449-5451).
test/test_transport_permission_floor.py::test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait: [died] fails 3 of 3 on a712e6f (GrantEnded) and passes 3 of 3 on 3877d64.

@javenciu

Copy link
Copy Markdown
Contributor Author

span=705140069842

fixed in 3877d64: checks() reads the withdrawal, then _refuse_write_if_dead(), then the caller's before_write (src/kiro_crew/acp/runtime.py:5443-5451); approve_tool passes the withdrawal as withdrawal_check and the grant's guard as before_write (src/kiro_crew/acp/session_handle.py:2547, :2549), so a runtime that died during the lock wait raises AcpRuntimeDead and chat_runner takes the recovery.
test/test_transport_permission_floor.py::test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait[died|alive|died-and-withdrawn]: red 3 of 3 on a712e6f ([died] raises GrantEnded), green 3 of 3 on 3877d64.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 13, 2026
@javenciu
javenciu requested a review from iamwhatever October 13, 2026 20:49
@iamwhatever

Copy link
Copy Markdown
Contributor

This makes a sub-agent run that is being stopped refuse every further tool request, even when its teardown and kill fallback both fail: a live Policy.stopping predicate is read by the run loop, at settle entry, before asking a person and at _allow's commit point, and the ACP transports read it again after the floor hop and under the write lock. Our change request on a712e6f is met: AcpRuntime._write_response_bounded now runs only withdrawal_check ahead of _refuse_write_if_dead() and the caller's before_write (the grant deadline) after it (src/kiro_crew/acp/runtime.py:5443-5451, src/kiro_crew/acp/session_handle.py:2547-2549), so a dead runtime with an expired grant raises AcpRuntimeDead again, and test_what_the_shared_runtime_reports_when_the_grant_ends_during_the_lock_wait[died] pins it. I tried to get an approval past a stop through a grant, the gate hit and a person's yes, and through the floor hop and lock wait on both AcpClient and the session handle; every path goes through _allow with no await before the read, a withdrawn approval writes nothing and puts the request's records back, and since the change only adds refusals it cannot widen access. Checks are green. The one blocker is the hard rule on AI review: the First Principles lane re-posted CONCERNS on the current head 3877d64 at 19:41Z, after your 18:48Z rebuttal, for the same item 3 (the transport-level withdraw_if / withdrawal_check / ApprovalWithdrawn / approval_withdrawable layer, span bcc96c0ead6d), and your disposition is scoped to head=a712e6f, so nothing answers it on this head. Please re-post that rebuttal as <!-- ai-review-disposition target=first-principles head=3877d64d29644b6ba00c5aaba128375827e14b59 -->; since the lane says it clears only when a human accepts the cross-transport surface, the question for a maintainer is whether to accept that layer here or split it into its own PR as Design suggests. — crew-pr-reviewer (deep re-check)

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 14, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you @javenciu for finding this and for a careful fix with thorough tests.

Main split acp/client.py into owner modules after this PR was written, so it no longer merged cleanly. To land your work, I carried your two commits (your authorship kept) onto current main in a same-repo PR: #20743. The only port change: the AcpClient.approve_tool part now lives in acp/client_permissions.py. It also opens issue #20734 for the bug.

#20743 is green with readiness passed, so I'm closing this one in its favour. Your branch is left as it is. Thanks again!

auto-merge was automatically disabled October 15, 2026 11:36

Pull request was closed

@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 15, 2026
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) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants