fix(extension): make tool cancellation cooperative#18
Open
NianJiuZst wants to merge 5 commits into
Open
Conversation
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.
What problem this solves
The extension previously implemented cancellation by racing every tool handler against a promise that rejected as soon as an AbortSignal fired. That made the RPC appear cancelled immediately, but it did not stop the handler that lost the race.
For read-only work this wasted CDP work and could corrupt later state: a cancelled snapshot could finish after a newer snapshot and replace the session RefStore with stale element ids. For mutating work the impact was more visible. Cancelling
tool.session_startwhilechrome.windows.createwas pending caused the daemon to discard its reservation, but the unresolved handler later created and registered an Agent Window that the daemon did not know about.Several tab handlers already contained abort checks and compensation logic, but the dispatcher did not pass their AbortSignal. The result was a cancellation protocol that acknowledged cancellation without establishing that the underlying browser operation had stopped or rolled back.
How this fixes it
The dispatcher no longer uses
Promise.raceto manufacture an early cancelled response. It still acknowledges the separatecancelrequest immediately, but the original tool RPC now waits for its handler to observe the signal and finish any necessary compensation before sending its final response.Cancellation is wired through the previously disconnected paths:
session_startreceives the AbortSignal;SessionManager.startnow treats Agent Window creation as a transactional operation. If cancellation arrives afterwindows.create, or if active-tab initialization fails, it removes the incomplete window and does not register the session. The session handler maps this AbortError to the structuredcancelledresult.Snapshot checks the signal before replacing RefStore contents. A cancelled older snapshot can therefore finish its underlying CDP request without overwriting refs produced by later work.
Cancellation boundary
Some Chrome APIs are irreversible after dispatch, such as removing a tab that Chrome has already accepted. The fix does not pretend those operations can be rolled back. Instead, it guarantees that BrowserSkill does not send the original RPC response while a handler is still running invisibly. Handlers with a reversible side effect, such as creating a tab or Agent Window, perform compensation before returning cancelled.
User impact
Cancelling a slow command no longer leaves hidden Agent Windows or silently mutates the element-ref state after the CLI has moved on. The cancel control remains responsive because its own acknowledgement is immediate, while the cancelled tool's final lifecycle is now truthful and deterministic.
The change also fixes a non-cancellation leak: if Agent Window active-tab setup fails after window creation, the partial window is removed.
Validation
corepack pnpm --filter @browser-skill/extension testcorepack pnpm --filter @browser-skill/extension compilecorepack pnpm ext:buildnode --test scripts/*.test.mjscorepack pnpm exec stylelint "**/*.css"git diff --checkRegression tests verify that:
CI Baseline Compatibility
This branch also carries the one-line Biome 2.4 formatter output for
apps/extension/vitest.config.ts. The same formatting mismatch currently fails the Frontend job onupstream/main; this minimal compatibility commit is intentionally separate from the functional fix and lets this PR run the repository CI suite to completion.CI Reliability Follow-up
GitHub Actions exposed a pre-existing test-only port allocation race: integration helpers probed an ephemeral port, released it, and then asked the daemon to bind the same number, allowing another process to claim it in between. The resulting
Address already in usefailure was unrelated to the functional changes. The PR now lets each daemon bind port0directly and reads the assigned address from its handle, removing the TOCTOU window across all affected integration helpers.