Skip to content

fix(acp): clear pending permission requests when a turn ends - #3672

Merged
kdcokenny merged 4 commits into
generalaction:mainfrom
arsalankhan786777:fix-3623-clear-permission-on-turn-end
Dec 5, 2026
Merged

kdcokenny merged 4 commits into
generalaction:mainfrom
arsalankhan786777:fix-3623-clear-permission-on-turn-end

Conversation

@arsalankhan786777

@arsalankhan786777 arsalankhan786777 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

The ACP session machine cleared pending permission requests on Stop and on process close, but not when whoever was waiting for the answer was done, so a dead request kept the permission band up, the task at "needs action" and the chat busy.

The session cell now records who owns each request, and the machine cancels it (the agent gets a cancelled outcome) when nobody can be waiting for it:

  • The conversation's own session: cancelled when the turn or replay ends.
  • A child (subagent) session: stays answerable after the parent turn ends while an agent still runs; cancelled when that child reports idle, or its agent completes or fails (also when the event names only the launching call, or the end of a replay settles it).
  • The conversation's own session while a background agent without its own session runs: either one may have asked, so it is kept past a turn end, and cancelled when the last agent is done (also during the agent's own turn). An agent counts as having its own session once it is announced or sends on it, also when the transcript shows it as its launch tool call.
  • Any owner: an idle session (no turn, no agent turn, no running agent) or a closed one takes no request, and none stays pending in it. Stop and process close cancel everything.

Behaviour changes to check:

  • The agent's own (unprompted) turn no longer ends on the 250 ms quiet timer while any request is unanswered, a child's included. The chat stays busy, Stop stays available and queued prompts wait. The quiet period starts over once the request is answered or cancelled.
  • A resumed child that asks before its running update is taken only while something else is active. In a fully idle conversation it gets cancelled.
  • A request that was really the parent's, made during its unprompted turn while a background agent without a session ran, is cancelled when the last agent is done.

Known limits (from reading the code, not tested): a request on the conversation's session made between a launch call and the child's announcement stays while that child runs; a nested child that its parent child shows only as a tool call is not counted as running, so its request is cancelled if nothing else runs.

ACP state machine change (high-risk area). No wire schema change: the owner stays inside the runtime.

Related issues

Fixes #3623

Testing

  • New tests: machine.test.ts, cell.test.ts and permission-ownership.test.ts (through AcpRuntime and the session router). With the sources of d9c79a0d90, 10 tests fail (4 in permission-ownership.test.ts, 3 exhaustive model runs, 3 SessionCell property runs); all pass now.
  • Model check: T7 ("no permission request outlives agent activity") is no longer it.fails. The model sends requests of the conversation and of child sessions, also while idle and during a replay, and the SessionCell property test checks T7 too.
  • T6 ("an agent turn that holds the queue has something that ends it") now also accepts a pending request that somebody may still wait for, since only its answer or Stop may end that turn. An unattributed request with no background agent left does not count.
  • The exhaustive model test has a 30 s timeout: its state space grew and it timed out once at 5 s in a parallel run.
  • Two existing broker tests in cell.test.ts now start a turn before asking, because an idle session cancels the request.
  • Last commit: 8 new tests through AcpRuntime in permission-ownership.test.ts fail without its source change and pass with it.
  • npx vitest run src/runtimes/acp in packages/core (52 files, 730 pass, 1 skipped)
  • Desktop node project tests matching acp conversation (46 files, 347 pass; run before the last commit). The main-db ones do not run here (better-sqlite3 is not installed); browser tests not run.
  • npx tsgo --noEmit -p tsconfig.json in packages/core
  • oxlint and oxfmt --check on the changed files, tooling/oxlint/scripts/check-allowlists.mjs
  • The whole packages/core suite was not run for the last commit. On an earlier head it did not pass on my Windows machine (97 files fail, none under runtimes/acp; among the errors are a missing better-sqlite3 and "Path must be Windows drive or UNC absolute"); I did not compare that run with main.
  • I did not run the app by hand, or against a real agent.

Written with Claude Code (AI); tested locally with the commands above.

Checklist
  • I kept this PR small and focused
  • I ran a self-review before opening this PR (Claude Code reviewed the diff; no human review yet)
  • I ran the relevant local checks or explained why not
  • I updated docs when behavior or setup changed (none needed: no user docs describe this)
  • I added or updated tests when behavior changed, or explained why not
  • I only added comments where the logic is not obvious
  • I used Conventional Commits for commit
    messages and, when possible, the PR title

🤖 Generated with Claude Code


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

The session machine drained pending permission requests on Stop and on
process close, but not on TurnEnded. A request still unanswered when the
turn ended stayed on screen, kept the task as "needs action" and kept the
chat busy.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; the previous finding is fixed and no new actionable issues were found.

Summary

Tracks who owns each pending permission and cancels requests when their activity ends.

  • Permission requests end with the turn or child that owns them.
  • Unanswered permission requests keep an unprompted agent turn active.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Request[Permission request] --> Idle{Conversation idle or closed?}
  Idle -->|Yes| Cancel[Reply cancelled]
  Idle -->|No| Pending[Keep request with its owner]
  Pending --> Answer[User answers]
  Pending --> End{Owner activity ends?}
  End -->|Parent turn ends; child still runs| Pending
  End -->|Child ends or no activity remains| Cancel
  Pending --> Stop[Stop or process close]
  Stop --> Cancel
Loading

Reviews (3) · Last reviewed commit: "fix(acp): cancel a child's requests howe..." · Reviewed by Greptile

Copy link
Copy Markdown
Member

Thanks for picking up #3623. I reviewed 33a5885e96 against the real session runtime.

What works: for a request that arrives before the parent turn ends (normal end, Stop, a turn error, a quick hand-off to a queued turn), the request is cleared, the agent is answered with a cancelled outcome through the permission broker, and a user answer racing with it returns invalid_state without throwing. Gates and the ACP suites pass.

One regression, which blocks merging: TurnEnded now cancels every pending permission in the conversation. Requests from a background subagent are routed into the parent conversation's pendingPermissions without recording which session owns them (SessionRouter.onPermissionRequest), so when the parent turn ends normally, a still-running background subagent's request is answered cancelled and its prompt disappears. On main it stays, and the user can approve it. The clear needs to be limited to requests owned by the turn that ended, which means recording the owner when the request is routed.

The issue is only partly fixed: a stale request can still be left behind on four other paths, and the model test's invariant T7 is still marked it.fails:

  • a request that arrives after TurnEnded while the session is idle is still accepted (PermissionRequested in machine.ts);
  • AgentActivity(false), AgentsChanged(0) and ReplayEnded do not clear requests whose owner is gone.

A counterexample from the model: sessionReady → idleAgentMessage → permissionRequest(r0) → quiesceTimer. If the fix is complete, T7 should pass without it.fails.

A turn end no longer cancels the requests of child sessions, which stay
answerable until the child goes idle. Requests that arrive in an idle
session, or outlive a replay or sessionless background agents, are
cancelled. The agent's own turn stays open while it waits for an answer.

Fixes generalaction#3623

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@arsalankhan786777

Copy link
Copy Markdown
Contributor Author

Thanks, you were right on both points. Fixed in a new commit.

Regression: each request now records its owner when it is routed in (the conversation's own session, or the child session that sent it). A parent turn end no longer cancels a child's request; it stays answerable until that child reports idle (or its agent completes or fails). Stop and process close still cancel everything.

The other four paths: a request arriving in an idle session is answered cancelled; ReplayEnded and AgentsChanged(0) clear requests with no owner left. T7 now passes without it.fails, with requests also modelled while idle and during replay.

One choice for you: for AgentActivity(false) I did not cancel the request. The quiet timer fires 250 ms after the agent stops talking, which is also what happens while it waits for permission, so cancelling there would dismiss live prompts. Instead the agent's own turn stays open until the request is answered (or Stop). Queued prompts wait during that time.

Limits:

  • If a background agent without its own session is running, a request on the conversation's own session can't be attributed, so it is kept until neither a turn nor a background agent is left.
  • A child's request is accepted even if that child last reported idle (a resumed child can ask first). One that is really stale there stays until the child reports again, Stop, or process close.

Verified: ACP suites (718 pass, 1 skipped), tsgo, oxlint, oxfmt --check, allowlist check. The new tests include the background subagent case through AcpRuntime and the router; 10 new tests fail on 33a5885e96 and pass now.

Not verified: the running app or a real agent. The whole packages/core suite does not pass on my Windows machine (97 files fail, none under runtimes/acp); I did not compare that with main.

(Written with Claude Code, AI.)

Copy link
Copy Markdown
Member

Thanks for the quick turnaround. I re-reviewed d9c79a0d90 against the real SessionCell and AcpRuntime.

Direction: yes. Recording an owner per request is the right model. A child's request now survives the parent's turn end, replay is handled, and T7 passes without it.fails. On your question: keeping the agent's own turn open while it waits for an answer is the right choice; cancelling on the quiet timer would dismiss live prompts.

Three cases still go wrong. All three are reproduced, not read from the code.

  1. A child started by a tool call makes the parent's own request survive its turn. Sequence: the parent launches a background child through a subagent tool call (agentId: child-1, background: true) and the child announces subagent_session(child-1, running). The transcript keeps the launch tool-call row, so transcript.subagentRow('child-1') is null. permissionOwner (cell.ts:746-755) then tags a request on the parent's own session as unattributed. When the parent turn ends while child-1 runs, that request stays on screen, and answering it sends selected to a turn that is over. A child that only announces subagent_session, with no launch tool call, works.

  2. A late child request in an idle session cannot be cleared. Sequence: child-1 reports idle, the parent turn ends, then requestPermission({ sessionId: 'child-1' }) arrives. machine.ts:397 exempts child-owned requests from the idle check, so it is accepted with backgroundAgentCount: 0 and canCancel: false. Stop returns ok() and does nothing. The request stays until the process closes, which breaks T7.

  3. A background agent without its own session, started while idle, can lock the session. Sequence: in ready, a subagent update (background: true, in_progress) opens the agent turn; a request arrives on the conversation's session and is tagged unattributed; the 250 ms timer exits without re-arming because the turn is held; then subagent_update(completed) fires AgentsChanged(0). machine.ts:350 computes idle as false because agentTurnActive is still true. Result: the request stays pending, agentTurnActive stays true with no timer, and a queued prompt is never sent.

On the model:

  • machine.model.ts never generates a child-owned request, so it cannot find case 2.
  • machine.model.ts:447 adds && !holdsAgentTurn(m) to T6-agent-turn-ends, which hides case 3. Please restore T6 and let the model find the sequence.

Tests and gates on this head pass (ACP suite 720/720, desktop ACP and conversations 342/342, format, lint, typecheck). The new tests fail on main and on the previous head for the right reasons.

If you want to take another round, these three plus the two model points are the whole list. If you would rather hand it over, say so and I will finish it on your branch.

- A launched child that has its own session no longer counts as a possible asker of
  requests on the conversation's session.
- An idle session takes no request, a child's included, and a turn or replay that ends
  with no agent left cancels what is pending. An agent turn now lasts while any request
  is unanswered.
- When the last background agent is done, an unattributed request is cancelled even if
  the agent's own turn is open, and the quiet period starts over.
- The model generates child requests and checks that an open agent turn always has
  something that can end it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@arsalankhan786777

Copy link
Copy Markdown
Contributor Author

Thanks for the reproductions. All three are fixed in a new commit.

  1. Child started by a tool call: "has no session of its own" is now decided by whether that child was announced (or sent) on its own session, not by whether the transcript has a subagent row. The parent's request is tagged turn and cancelled when the turn ends.

  2. Late child request in an idle session: one rule for every owner: an idle session (ready, no agent turn, no running agent) takes no request, and a turn or replay that ends with no agent left cancels what is pending. A resumed child that asks before its running update is still taken while a turn, an agent turn or another agent is active. I preferred this to letting Stop clear it, which would still leave a request outside any activity (T7). So that such a request is not left behind either, the agent's own turn now lasts while any request is unanswered, a child's included.

  3. Locked session: when the last background agent is done, an unattributed request is cancelled even if the agent's own turn is open. Whenever a request is answered or cancelled, the quiet period starts over, so the turn ends and the queue continues.

Model: it now generates child requests (and the end of the child that asked). On the sources of d9c79a0d90 it finds case 2 (T7) and case 3 (T6) by itself.

T6: the holdsAgentTurn exemption is gone, but the literal old form cannot hold: sessionReady → idleAgentMessage → prompt → permissionRequest → quiesceTimer is a live prompt, and only its answer (or Stop) may end that turn. Re-arming the timer while held would satisfy the old wording without ending anything, and would hide case 3. The condition is now: an agent turn holding the queue needs an armed quiet timer, or a pending request that somebody may still wait for. An unattributed request with no background agent left does not count. This is written in the model itself, not taken from the machine.

Trade-offs:

  • A request that was really the parent's, made during its own unprompted turn while a background agent without a session ran, is cancelled when the last agent is done. It cannot be told apart from case 3.
  • A resumed child that asks before running while the whole conversation is idle gets cancelled. The same happens if it was taken during a turn that ends before anything reports running.
  • From reading the code, not tested: a request on the conversation's session made between the launch call and the child's announcement is still unattributed, so it stays while that child runs. A nested child that its parent child shows only as a tool call is not counted as running, so its request is cancelled if nothing else runs.

Verified: packages/core ACP tests (52 files, 722 pass, 1 skipped), desktop node tests matching acp conversation (46 files, 347 pass), tsgo in packages/core, oxlint and oxfmt --check on the changed files, allowlist check. With the sources of d9c79a0d90, 10 of the tests fail (4 through AcpRuntime, 3 exhaustive model runs, 3 SessionCell property runs); all pass now. I raised the timeout of the exhaustive model test to 30 s: the larger state space timed out once at 5 s in a parallel run.

Not verified: the running app or a real agent, desktop database tests (better-sqlite3 is not installed here) and browser tests, and the whole packages/core suite.

(Written with Claude Code, AI.)

Copy link
Copy Markdown
Member

@greptile review

A child's request that outlived the parent turn was left pending when its
agent ended through a `subagent` event, through an event naming only the
launching call, or because the end of a replay settled it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Member

@greptile review

@kdcokenny
kdcokenny merged commit 64045e0 into generalaction:main Dec 5, 2026
14 checks passed

Copy link
Copy Markdown
Member

Merged. Thanks for staying with this through four rounds; the per-request owner model is a real improvement over the first version.

What the final checks showed on 08b2452bbd:

  • Code review: the two-subagent sequence fails on the previous commit and passes on this one. No probe made the before/after comparison cancel a request that was still live. Per-event cost is unchanged on a 3,000-row transcript.
  • Real app, real Claude Code chat: Allow, Reject, queued requests, Stop while a request is open, a queued follow-up after Stop, a background subagent's request outliving the parent turn, two background subagents with one stopped, and quit and reopen all behaved correctly. The chat was never left busy or with an unsent prompt.
  • On main, the two-subagent case reproduces the bug: the stopped subagent's request stays on screen, keeps the chat busy, and blocks the other subagent's next request.

Two things for a follow-up, if you want one; neither blocked the merge:

  • The model tests (machine.model.test.ts, cell.property.test.ts) still pass with the last commit's cell.ts change reverted. Only the example tests in permission-ownership.test.ts guard that case.
  • The before/after comparison inside push / pushSubagent can be removed without any test failing. Either it is not needed or a test is missing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: A permission request that arrives as a turn ends stays on screen and keeps the chat marked busy

2 participants