Skip to content

fix(react): a stop pressed at any point in a turn stops it (FIX-1956) - #3269

Merged
jhoffner merged 6 commits into
mainfrom
fix/FIX-1956-early-stop
Oct 13, 2026
Merged

jhoffner merged 6 commits into
mainfrom
fix/FIX-1956-early-stop

Conversation

@jhoffner

@jhoffner jhoffner commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Jake · project thread

The problem

A person presses Stop early in a turn. Sometimes the page does not show the turn as stopped, or the turn continues. The Kitchen Sink e2e test "a stop before Claude Code names a conversation reads as stopped" fails about 3 times in 20 on main.

The issue said that abortRequest returns early because the request id is not set yet. The repro shows a different cause for the e2e failure. In that test, the abort reaches the server (204), and the server stops the run. The failure is in the view:

  1. abortRequest closes the stream and adds its own stop record to items.
  2. The page reads the session snapshot again when the turn stops streaming.
  3. That read can land before the engine writes its own stop item. The snapshot then removes the hook's stop record, and latestRequest still says in_progress. So the turn reads as running, and no notice shows.

The early-stop gap that the issue names is also real, in two other places:

  • The stop comes while the turn's POST is still out. The abort can find no request on the server (404). When the POST answers, the hook attaches its stream, and the turn continues.
  • The turn was resumed on mount (a reload during a turn). The mount resume did not set the active request id, so abortRequest returned early and did nothing.

What this does

useSession now keeps a set of the requests that the person stopped.

  • Snapshot reads. The hook keeps its own stop record across snapshot reads until a snapshot holds the server's stop record. Then the hook removes its record, so the view shows one stop record and not two. If the abort gets a 409 (the turn finished before the stop), the hook does not keep the record, and the server's result stands.
  • A stop during the POST. When the POST answers, the hook sends the abort again, because now the server has the request. The hook cancels the response body and does not attach the stream.
  • The mount resume. The mount resume sets the active request id, so a stop reaches the request.

All three changes are in packages/react/src/hooks/useSession.ts. The client and the server abort contract do not change. The client package has no way to cancel a send that is still out. This fix does not need one, because the abort route stops the request once the request exists.

What's asked of you

Nothing here is yours to decide. This is a bug fix, so this PR is the gate.

Parts worth reviewing closely

  • applySnapshot: the loop that puts the stop record back. It checks the unfiltered nextSnapshot.items for the server's stop item (detail.code === "system.request_aborted").
  • sendAction: the early return after the POST answers for a stopped request. It returns an in_progress response and attaches no stream.
  • abortRequest: only a 409 removes the request from the set. A 404 keeps it, because a stop during the POST can get a 404 before the request exists.

Linear: FIX-1956 · no spec (bug, direct route) · merges after review

How to review this — altitude, what's in scope, what's deliberately unsettled

How to review this. This is a bug fix on the direct route: there is no spec and no
spec PR, because the hard part was the diagnosis and it happened in code. This PR is the
only gate
, so unlike a spec-backed change, the approach is in scope — nothing here was
signed off upstream.

Most valuable here: the diagnosis chain — does the repro actually reproduce the reported
failure, is the named cause the real one, does the fix address that cause rather than the
symptom, and does the regression test fail without the fix? Then the second path (the legacy
shape, the null boundary, the concurrent case, the cancel path).

In scope to challenge: where the guard was placed, any behaviour change a user could
notice, and whether this should have been a spec'd feature instead of a fix.

Engineering calls — what was decided while building
  • The fix is in the hook, not in Kitchen Sink. The page reads the stop item, which is correct. The hook removed its own record of a stop that it had sent. Any consumer that reads a snapshot after a stop has the same problem.
  • The hook does not keep the stream open until the server's aborted event. That design also works, but it changes when isStreaming goes false after a stop, and the hook needs a timeout for a 202 stop that runs in another process. Keeping the stop record changes less.
  • dismissRequest does not change. It refreshes the snapshot and latestRequest itself, and it can target a request that the server does not have (404). Keeping its record could show a stop that did not happen.
  • The set clears on a session change, together with the other request refs.
Everything else — verification

Unit test, red on main: packages/react/test/useSession-stop.test.ts. The first three tests fail without the fix:

× a stop pressed while the turn's POST is still out stops the turn once the server has it
  → expected "spy" to be called 2 times, but got 1 times
× a stop pressed on a turn picked up on mount stops it
  → expected "spy" to be called with arguments: [ 'req_running' ]
× a stop mid-stream still reads as stopped after a snapshot taken before the server recorded it, without doubling once it has
  → expected [] to have a length of 1 but got +0
✓ a stop that lost the race to the turn's end does not keep claiming the turn was stopped
Tests  3 failed | 1 passed (4)

With the fix, all 4 pass.

Kitchen Sink e2e, claude-code-mode.spec.ts -g "a stop before Claude Code" --repeat-each=20 --retries=0, on a production test-mode build:

Build Parallel workers --workers=1
main 17/20 passed 17/20 passed
this branch 20/20 passed 20/20 passed

The full e2e suite passed once on this branch: 32 passed. The Codex twin test that the issue names is not on main, so this PR does not run it.

Local note: the sandbox cannot download the Chromium build that Playwright 1.59 wants. The runs used the installed chromium_headless_shell-1194 through a scratch config that was not committed. The full Chromium build in this sandbox also requests /favicon.ico and logs a 404, which the consoleErrors fixture catches. The headless shell does not do this.

Other checks: pnpm typecheck, pnpm knip:ci, and node scripts/check-retired-terms.mjs pass. The react tests (356), client tests (174), and kitchen-sink unit tests pass.

Changeset: .changeset/stop-early-in-a-turn.md, patch for @flow-state-dev/react, names FIX-1956.

🤖 Generated with Claude Code

https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv


Generated by Claude Code


Note

Medium Risk
Changes core session streaming and abort timing in useSession, which affects all consumers’ stop UX and race handling; behavior is well covered by new tests but regressions could still show up in e2e stop flows.

Overview
useSession().abortRequest now reliably stops a turn and keeps the UI showing “stopped” across POST-in-flight, mount auto-resume, and snapshot refresh races (FIX-1956).

The hook tracks stopped request IDs in stoppedRequestsRef, re-applies local system.request_aborted status items in applySnapshot until the server snapshot includes the same stop, and uses serverRefusedStop (up to two abortRequest calls on HTTP 409) so a finished turn does not keep a synthetic stop. sendAction re-aborts and skips attaching SSE when the POST returns after an early stop; auto-resume sets activeRequestIdRef so stop works on reload mid-turn.

Adds packages/react/test/useSession-stop.test.ts and a patch changeset for @flow-state-dev/react. Shift Manager’s project-workstreams test updates scripted coordinator tooling from handOff to dispatchTask.

Reviewed by Cursor Bugbot for commit 5f70c3e. Configure here.

useSession.abortRequest lost a stop in three places:

- mid-stream: it closed the stream and showed its own stop record, but
  the next snapshot read (Kitchen Sink reads one as the turn stops
  streaming) could land before the engine wrote its stop item, erased
  the hook's record, and left the latest request reading in_progress,
  so the turn read as running. This is the Kitchen Sink e2e flake.
- while the turn's POST was out: the abort could reach no request yet,
  and the POST's stream was attached when it answered, so the turn ran on.
- on a turn resumed on mount: the hook held no request id, so the stop
  returned early.

The hook now remembers the requests the person stopped. It keeps its
own stop record across snapshot reads until one holds the server's, and
drops it on a 409 (the turn finished first). A stop during the POST is
sent again once the POST answers, and the turn is not attached. The
mount resume sets the active request id.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv
@vercel

vercel Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Actions Updated
flow-state-dev-docs Ignored Ignored Oct 13, 2026 11:18pm UTC
flow-state-dev-kitchen-sink Ignored Ignored Oct 13, 2026 11:18pm UTC
flow-state-dev-ui Ignored Ignored Preview Oct 13, 2026 11:18pm UTC

Request Review

@linear-code

linear-code Bot commented Oct 13, 2026

Copy link
Copy Markdown

FIX-1956

@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 9d6b4beb-0ff4-4440-a251-11483841c6e8)

@jhoffner
jhoffner marked this pull request as ready for review October 13, 2026 22:39
@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 56f5cf0e-f17b-49e0-bfef-318f35fba12c)

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/simplify review (FIX-1956)

Parallel read-only passes (code quality, performance, reuse) on the scoped diff. No local code changes from this automation — feedback only.

Verdict on approach

The three failure modes (stop while POST is in flight, stop on mount-resumed turn, stop erased by an early snapshot) are real. Given how useSession already works — optimistic items, held reconciliation in applySnapshot, and client-owned ids until the server snapshot agrees — a stoppedRequestsRef latch plus re-upsert of the synthetic stop item is a proportionate fix, not an over-engineered parallel state machine. Performance on the new applySnapshot loop is negligible (small S, infrequent path vs sort/network).

Simpler architectures considered (likely not worth this PR):

Alternative Why it is heavier or worse here
Server-only stop visibility Flicker / “still streaming” until snapshot; needs engine ordering guarantees
One boolean “user stopped” Does not cover POST-vs-stream timing, mount resume, or per-request history
Push stop code into client transport Still need react-side optimistic item + snapshot race handling for UX
Third-party package No off-the-shelf fit for FSD item/snapshot merge semantics

Optional follow-up hygiene (not blocking the core design): centralize system.request_aborted (+ a small status-item predicate) in @flow-state-dev/contracts so engine, kitchen-sink, and react stop duplicating literals; keep abortItemIdOf in react (server ids are random).

Requested change (correctness)

On 409, you correctly drop the request from stoppedRequestsRef, but injectSyntheticAbortItem still runs and abortRequest does not call refreshSnapshot() (unlike dismissRequest). That can leave a phantom “Request was stopped” on a turn that already finished until some later refresh. The new test only asserts after refresh(). Consider skipping synthetic inject on 409 (and/or mirroring dismiss’s immediate refresh) and asserting zero stop items immediately after abortRequest in that test.

Nits / optional (your call)

  • sendAction after stop-during-POST still returns status: "in_progress" while the hook does not attach the stream — fine if callers only look at isStreaming / items; worth a one-line hook doc if anything reads the return value.
  • dismissRequest vs abortRequest: only the latter uses stoppedRequestsRef; if dismiss is another stop entry point, snapshot races may differ — unify later if both need the same latch.
  • Trim a few restate-the-code comments (activeRequestIdRef mount line, duplicate “until snapshot” prose).
  • Test mock duplication with useSession-continueRequest.test.ts matches repo convention; shared hoisted mock helper is optional when a third file appears.

What looks good

  • Symmetry with optimisticIdOf / abortItemIdOf.
  • Setting activeRequestIdRef when attaching on mount so stop reaches resumed turns.
  • Post-POST branch: cancel body, second abort, no stream attach.
  • Four behavioral tests map cleanly to the bugs + 409 race.
  • Changeset is accurate.

Happy to approve after the 409 + synthetic-stop interaction is tightened (small diff).

Open in Web View Automation 

Sent by Cursor Automation: Review For Best Approach

Comment thread packages/react/src/hooks/useSession.ts Resolved
Comment thread packages/react/src/hooks/useSession.ts Resolved
Comment thread packages/react/src/hooks/useSession.ts Resolved
When the server answers a stop with 409 the turn already finished, so
abortRequest no longer injects the local stop item it would otherwise
show until the next read.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv
@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: fb9b2cc7-cf8e-4701-9213-198753bc622e)

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Snob.

The stop set, the snapshot put-back, the second abort, and the mount assignment are the contract. refused is the 409 skip for the synthetic item; leave it.

Two things around that are extra.

sendAction builds the same in-progress response twice: the stop that landed during the POST, and the POST that comes back as an event stream. One accepted value covers both. The 202 path still returns the server body.

Comments that say the same thing as the next lines: the STOP_CODE line, the snapshot-loop note, the three-line POST note (one line stays: the first stop may have reached nothing), the test header's three moments, and the test comments that preview the next assertion. The line "Nothing claims a stop, not even before the next read." stays.

Net −34 against 45ad26b. The four stop tests passed, including the immediate 409 assertion. React typecheck passed.

POC, does not merge: #3273

Fold that subtraction into this PR, then close the POC unmerged.

Left as recorded: the fix stays in the hook, the stream is not held open for the aborted event, dismissRequest is unchanged, the set clears on a session change, only a 409 drops the id, a refused stop adds no item, the stopped POST returns in progress and attaches no stream, and the snapshot check reads unfiltered nextSnapshot.items. abortItemIdOf keeps the comment optimisticIdOf already has. The set's note, the mount line, and the 409 note stay. The changeset stays one sentence.

View PR

Open in Web View Automation 

Sent by Cursor Automation: Code Snob

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv

Copy link
Copy Markdown
Collaborator Author

On the Code Snob review: I took the single accepted response from #3273 in 787cf98, and all 356 react tests pass. I kept the comments it cut. The test header's three moments and the snapshot-loop note say why each case exists, and the code can't tell you that (CLAUDE.md §5 and BP-007). The rest are one-liners, so cutting them buys little. I'm leaving #3273 for its owner to close.


Generated by Claude Code

@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 8514c22a-b982-4918-a5be-adddd0f9c3f2)

Copy link
Copy Markdown
Collaborator Author

Test (1/3) failed at 787cf98 in one test: shift-manager's project-workstreams.test.tsx ("refuses research under the EM ... opens it under the general lead") timed out waiting for the ask in the general lead's session. Run locally on this same head, the file passes three times out of three (12/12 each time), so I've re-run the failed job once. If it fails again I'll treat it as real and dig into it.


Generated by Claude Code

@jhoffner

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T23:10:42.816734Z 787cf98 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 787cf98ab9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/react/src/hooks/useSession.ts Resolved
Comment thread packages/react/src/hooks/useSession.ts Resolved

Copy link
Copy Markdown
Collaborator Author

Update: Test (1/3) failed again on the re-run, but the failure isn't this PR's. Shift-manager's project-workstreams.test.tsx ("... opens it under the general lead") times out on main itself at 57a87d8: three runs out of three locally, with the same "timed out waiting for the ask in the general lead's session". It came in with the recent general-lead merges (#3232 FIX-1920, then #3263). This PR only touches packages/react/src/hooks/useSession.ts. No fix exists yet, and I've flagged it for whoever owns that work. Once main is fixed, I'll merge it in here and re-run.


Generated by Claude Code

…fter the POST (FIX-1956)

A 409 to a stop means either the request finished or it was resumed
before the stop reached it. The hook now sends the stop once more and
treats only a second 409 as finished. A stop pressed during the POST
that the finished turn refuses drops the view's stop record and reads
the finished turn, instead of claiming a stop forever.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv
@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 89baf638-6d28-4073-925c-f52b94b08385)

The same one-line change as #3281; it no-ops once main carries it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017pp7wCd87NW4BH7XVuF9Gv

Copy link
Copy Markdown
Collaborator Author

I merged main and ported #3281's one-line test fix (handOff → dispatchTask) in 5f70c3e. Locally the Shift Manager file now passes 13/13 and react passes 358/358. The port no-ops once #3281 lands on main.


Generated by Claude Code

@cursor

cursor Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: d2743ec1-7b98-487a-9204-49a4a1e1d16d)

@jhoffner
jhoffner merged commit c6e71d8 into main Oct 13, 2026
19 of 20 checks passed
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.

2 participants