Repository navigation
fix(acp): cancel parked approvals when their turn ends - #14
Open
bhawesh-khandelwal wants to merge 1 commit into
Open
bhawesh-khandelwal wants to merge 1 commit into
bhawesh-khandelwal wants to merge 1 commit into
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An agent permission request becomes an
ApprovalRequiredevent 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:cancelledresponse,ApprovalRequiredwith no matchingApprovalResolved.The sibling
codexandclaudedrivers 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
ApprovalRequiredis followed by exactly oneApprovalResolved, and no parked approval outlives its turn:no_responsetimer, andEndAgentTurn) decrement the turn count and, at zero, cancel every parked request unders.mu: each is settledcancelledand itsApprovalResolvedemitted before the lock is released, so a request parking with a reused id cannot emit a newApprovalRequiredbefore the prior resolution is out.parkemitsApprovalRequiredunder 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.Resolvesettles and emits its resolution under the lock too, for the same ordering guarantee.dropPendingremoves 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 laterResolveerrors), and thecancelledoutcome 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:
session/loadparks beforeOpenreturns the session, so the caller cannot resolve it until load completes (pre-existing, re-exposed here);Close/Kill/exit) cancels without emittingApprovalResolved, by the existing terminal-session contract;#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