Skip to content

fix(cancel): Properly cancel prompts#542

Draft
CSRessel wants to merge 4 commits intoagentclientprotocol:mainfrom
CSRessel:fix/cancel-stale-idle-drain
Draft

fix(cancel): Properly cancel prompts#542
CSRessel wants to merge 4 commits intoagentclientprotocol:mainfrom
CSRessel:fix/cancel-stale-idle-drain

Conversation

@CSRessel
Copy link
Copy Markdown

No description provided.

CSRessel and others added 4 commits April 11, 2026 23:19
When cancel() returns before the first prompt's loop has consumed any
messages, a second prompt() call resets session.cancelled to false
(line 497). The first prompt then sees cancelled=false, processes the
interrupted turn's messages as a normal end_turn, and returns the wrong
stopReason. The second prompt is silently queued and either inherits
the stale turn or gets a 0-token response.

The race: cancel() → interrupt() resolves → second prompt() resets
cancelled=false → first prompt's next() returns → cancelled is false.

Test documents the expected behaviour (first prompt returns "cancelled")
and fails with the current code (returns "end_turn").
🤖 Generated with [Nori](https://noriagentic.com)

Co-Authored-By: Nori <contact@tilework.tech>
Replace the session-wide `cancelled` boolean with a monotonically
increasing `cancelGeneration` counter. Each prompt() captures the
counter at entry; cancel() increments it. The prompt loop checks
whether the counter has advanced past its snapshot, so a later
prompt() call can never clobber the cancellation signal of an
earlier in-flight prompt.

This fixes the race where:
  1. cancel() sets cancelled=true, interrupt() resolves
  2. A second prompt() arrives and resets cancelled=false
  3. The first prompt's loop sees cancelled=false and returns
     "end_turn" instead of "cancelled"
🤖 Generated with [Nori](https://noriagentic.com)

Co-Authored-By: Nori <contact@tilework.tech>
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