Skip to content

fix(acp): cancel parked approvals when their turn ends - #14

Open
bhawesh-khandelwal wants to merge 1 commit into
inference-sh:mainfrom
bhawesh-khandelwal:fix/acp-approval-lifecycle
Open

bhawesh-khandelwal wants to merge 1 commit into
inference-sh:mainfrom
bhawesh-khandelwal:fix/acp-approval-lifecycle

Conversation

@bhawesh-khandelwal

Copy link
Copy Markdown

Problem

An agent permission request becomes an ApprovalRequired event and parks a goroutine until a human answers (Resolve), the context ends, or the session closes. On interrupt or turn-end the driver never cancelled a still-parked request:

  • the agent received no cancelled response,
  • the parked goroutine leaked,
  • and the caller was left holding an ApprovalRequired with no matching ApprovalResolved.

The sibling codex and claude drivers already settle outstanding approvals at turn end; ACP was the one that did not.

Change

Cancel parked approvals when the turn that owns them ends, and hold the invariant every ApprovalRequired is followed by exactly one ApprovalResolved, and no parked approval outlives its turn:

  • The three turn-end paths (prompt completion, the no_response timer, and EndAgentTurn) decrement the turn count and, at zero, cancel every parked request under s.mu: each is settled cancelled and its ApprovalResolved emitted before the lock is released, so a request parking with a reused id cannot emit a new ApprovalRequired before the prior resolution is out.
  • park emits ApprovalRequired under that same lock, chooses a collision-free id so two requests sharing a tool-call id (or the session fallback) stay distinct, and cancels itself without emitting when it is an agent permission arriving with no turn in flight (an orphan of a turn that already ended). Requests raised during a load, questions, and extension approvals are exempt - they have no turn to wait for and are answered or torn down instead.
  • Resolve settles and emits its resolution under the lock too, for the same ordering guarantee.
  • dropPending removes a request only if it is still the one registered under that id, so a request ending on its own context cannot delete a later request that reused the id.

Tests

go test -race ./driver/ passes. New:

  • TestInterruptCancelsAParkedApproval - an interrupt cancels the parked approval: the resolution is emitted, the request is gone (a later Resolve errors), and the cancelled outcome reaches the agent over the wire (the fake agent echoes the outcome it received).
  • TestTwoApprovalsSharingAToolCallIDStayDistinct - two permission requests sharing one tool-call id stay distinct (call_danger, call_danger#1) and each resolves independently. (Verified non-vacuous: it fails if the second request overwrites the first.)

Known limitations (tracked in #13)

Documented rather than addressed here, so this change stays scoped to the interrupt/turn-end leak and the lifecycle races it exposed:

  • a permission raised during session/load parks before Open returns the session, so the caller cannot resolve it until load completes (pre-existing, re-exposed here);
  • teardown (Close/Kill/exit) cancels without emitting ApprovalResolved, by the existing terminal-session contract;
  • the live-turn check is not strict per-originating-turn ownership;
  • a suffixed collision id does not join tool-lifecycle events.

#13 proposes the coherent followup (a per-request cancellable context that subsumes most of these) and raises one open policy decision: what should happen to a permission raised mid-resume.

🤖 Generated with Claude Code

An agent permission request becomes an ApprovalRequired event and parks a
goroutine until a human answers, the context ends, or the session closes.
On interrupt or turn-end the driver never cancelled a still-parked
request: the agent got no cancelled response, the goroutine leaked, and
the caller was left holding an ApprovalRequired with no matching
ApprovalResolved. The sibling codex and claude drivers already settle
this at turn end; ACP did not.

Cancel parked approvals when the last turn ends, and keep every
ApprovalRequired paired with exactly one ApprovalResolved:

- The three turn-end paths (prompt completion, the no_response timer, and
  EndAgentTurn) decrement the turn count and, at zero, cancel every parked
  request under s.mu: each is settled cancelled and its ApprovalResolved
  emitted before the lock is released, so a request parking with a reused
  id cannot emit a new ApprovalRequired before the prior resolution is out.
- park emits ApprovalRequired under the same lock, chooses a collision-free
  id so two requests sharing a tool-call id (or the session fallback) stay
  distinct, and cancels itself without emitting when it is an agent
  permission arriving with no turn in flight (an orphan of a turn that
  already ended). Requests raised during a load, questions, and extension
  approvals are exempt: they have no turn to wait for.
- Resolve settles and emits its resolution under the lock too, for the
  same ordering guarantee.
- dropPending removes a request only if it is still the one registered
  under that id, so a request ending on its own context cannot delete a
  later request that reused the id.

Tests cover an interrupt cancelling a parked approval (the resolution is
emitted, the request is gone, and the cancelled outcome reaches the agent
over the wire) and two approvals sharing one tool-call id staying distinct
and each resolvable.

Known limitations are tracked as a followup: a permission raised during
session/load parks before Open returns the session; teardown cancels
without emitting ApprovalResolved; the live-turn check is not strict
per-originating-turn ownership; and a suffixed collision id does not join
tool-lifecycle events.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.

1 participant