fix(cancel): Properly cancel prompts#542
Draft
CSRessel wants to merge 4 commits intoagentclientprotocol:mainfrom
Draft
fix(cancel): Properly cancel prompts#542CSRessel wants to merge 4 commits intoagentclientprotocol:mainfrom
CSRessel wants to merge 4 commits intoagentclientprotocol:mainfrom
Conversation
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>
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.
No description provided.