agents icon indicating copy to clipboard operation
agents copied to clipboard

Think: cancelSubmission() doesn't prevent a waiting submission's messages from being applied when its turn-queue slot arrives

Open ivoelbert opened this issue 5 months ago • 1 comments

Describe the bug

cancelSubmission() cannot fully un-send a durable submission that is waiting behind an active turn.

When the submission drain claims a submission it marks it running and calls _runProgrammaticMessagesTurn, whose closure then blocks on the turn queue until the active turn finishes. If cancelSubmission() runs during that wait it marks the row aborted and fires the abort controller — but the already-enqueued closure appends the submission's messages unconditionally when its slot arrives: the external abort signal is only linked via _aborts.linkExternal(...) after the append loop, and the only pre-append guard is the turn-queue generation check, which a cancelled submission doesn't trip.

Result: the cancelled submission's user message is deposited into the transcript (and broadcast to connected clients) as a message nothing ever answers — the inference loop aborts immediately after.

To Reproduce

  1. Start an ordinary turn (e.g. saveMessages) whose model response streams for a while.
  2. submitMessages(...) — the drain claims it (pending → running); its turn closure queues behind the active turn.
  3. cancelSubmission(id) while it waits → the row is aborted.
  4. Let the active turn finish → the cancelled closure's slot arrives → the message is appended anyway.

Real-world context: we hit this behind a "queue a follow-up while the agent is working" composer (queued chips backed by durable submissions; ✕ = cancelSubmission). Cancelled messages reappeared as orphan user messages after the active turn finished. We currently carry a local workaround (wrapping the internal append to drop cancelled message ids) that we'd love to retire.

Expected behavior

A submission cancelled before its messages were applied never touches the transcript — it terminalizes as aborted with nothing persisted.

Version:

@cloudflare/think 0.8.7 / 0.8.8 (reproduced); the append path is unchanged on main as of 2026-06-10.

Additional context

We have a working fix + failing-then-passing regression test on a fork, in case it's useful to cherry-pick: https://github.com/ivoelbert/agents/tree/fix/think-cancelled-submission-slot-append

Shape of the fix: _runProgrammaticMessagesTurn accepts an internal shouldApplyMessages?: () => boolean re-checked at slot time before any append; the submission path passes a re-read of the submission row (status === "running") and abandons the turn (status: "aborted", nothing persisted) otherwise. Deliberately scoped to submissions — saveMessages() with a pre-aborted signal keeps its documented persist-user-message behavior (the existing test pinning that still passes). Full submissions.test.ts 43/43 and the abort-semantics tests 9/9 pass with the change; the new regression test fails on main. Changeset included on the branch.

(We read the README's note that external PRs aren't being accepted right now — happy for this branch to just serve as a reference.)

ivoelbert avatar Jun 10 '26 20:06 ivoelbert

watching

rwdaigle avatar Jun 10 '26 22:06 rwdaigle