fix(ai-client): drop queued resume when run is superseded #2181
main fromrefs/pull/2181/head·merged by tombeckenham View on GitHubA ChatClient resume that waits for the current stream still starts after reload() replaces that pause. The queued action now checks the run id and does not send the old resume.
🎯 Changes#
The client drops a queued resume when reload() replaces the paused run. No doc page describes this race, so docs/ stays as it is. The patch changeset is .changeset/drop-stale-queued-resume.md.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. A user whose interrupt resume failed, and who then hits Retry, can still approve the old card while the new run streams. When that run pauses, the old resume is sent and refused.
Cause. resumeInterruptsUnsafeForGeneration keeps the original target inside queuePostStreamAction. observeInterruptState then points lastResume at the new run. drainPostStreamActions sends the old target anyway.
Fix. The queued action resolves false and returns when lastResume.runId is not the target run id.
Possible alternatives#
- Clear interrupts in
reload(). The old card cannot be approved during Retry. This PR does not do that, because it changes the Retry UI. The run-id check is the smaller fix.
Testing#
Commands run. An agent-written Vitest repro on clean main (16026d91a) failed: 4 connects, and the fourth resume was the old card. The same repro on the fix tree passed: 3 connects, the new interrupt stayed, and interruptErrors was empty. pnpm test:pr was not run. The changeset commit does not change runtime code.
Manual test.
- On
main, queue a resume whilereload()is held open, then let that reload pause on a new interrupt. A fourth connect carries the oldparentRunIdand the old interrupt id. - On this branch, run
pnpm exec nx build @tanstack/ai, thenpnpm exec vitest run --config packages/ai-client/vite.config.ts packages/ai-client/tests/chat-client-resume.test.ts -t "drops a queued resume". The test passes. Only the new interrupt stays pending.
How this PR makes testing easy. packages/ai-client/tests/chat-client-resume.test.ts covers the race. The test name is drops a queued resume when a newer run replaces the paused interrupt.
Linked issues#
Closes #2178
Risk / rollback#
Risk is low. The same-run resume path still starts. Revert the PR to restore the old send.
Summary by CodeRabbit#
- Bug Fixes
- A queued resume no longer starts an extra connection if a reload replaces the paused interrupt before the resume proceeds.
- The newer interrupt remains active while the reload completes, and approving the outdated interrupt does not continue the replaced run.
- This prevents an outdated resume from interfering with the latest streamed response.
tombeckenham approved these changes
Thanks for the fix. I checked the queued-resume guard against a clean-main repro: the stale connect is gone, the new interrupt stays, and the patch changeset is on the branch.