Skip to content
Source
Sign inSign up
TanStack/ai

ai-client: a resume queued while streaming is sent after a newer interrupt pause replaced its run #2178

Send to my agent
Openaayushbtw(GitHub profile, opens in a new tab) opened this issue 0 comments

ai-client: a resume queued while streaming is sent after a newer interrupt pause replaced its run

Version

@tanstack/ai-client 1.1.0

What happens

resumeInterruptsUnsafeForGeneration defers a resume while isLoading is true, through queuePostStreamAction. The queued action keeps the target (thread and run id) it was created with. If the stream that is running ends on a new interrupt pause, observeInterruptState sets lastResume to the new run and interruptManager.hydrate() replaces the interrupts. The queued action is not touched. When the stream settles, drainPostStreamActions() sends it anyway: a request with parentRunId set to the superseded run and resume holding the old batch.

The server refuses it (for example Missing resume entry for pending interrupt ..., since the pending interrupt is now the new run's), and the client reports a failed stream.

How a user reaches it

  1. A run pauses on a tool approval (run 1). The user approves. The resume run (run 2) starts streaming.
  2. The resume stream is cut (an idle proxy timeout, a network drop) and the reconnect fails. The batch fails, and the run 1 approval card is shown again.
  3. The user hits Retry (reload()), which starts run 3 for the same user message. While run 3 streams, the run 1 card is still answerable.
  4. The user approves the run 1 card. isLoading is true, so the resume is queued.
  5. Run 3 ends on its own approval pause (a new interrupt id).
  6. The queued resume for run 1 is sent and refused. The user sees an error, though run 3's card is now on screen.

Minimal repro

With a ChatClient over a stubbed fetch that returns SSE:

  1. First POST: a run that ends RUN_FINISHED with outcome: { type: "interrupt" } carrying one tool approval.
  2. Approve it. Second POST (the resume): send only RUN_STARTED, then close. Answer the reconnect (Last-Event-ID) with an empty stream so the batch fails.
  3. Call client.reload(). Hold its response open. While isLoading is true, call resolveInterrupt(true) on the card getInterrupts() still returns.
  4. Answer the held request with a new run that ends on an interrupt pause with a different approval id.
  5. Await reload(). Captured POSTs include a fourth request with parentRunId equal to the first run's id and resume: [{ interruptId: <first approval> }].

Expected: no fourth request. Only the new pause's interrupt is pending.

Suggested fix

In the queued action, drop the resume when the run it answers is no longer the paused one:

this.queuePostStreamAction(async () => {
  if (this.lastResume?.runId !== target.runId) {
    resolve(false)
    return
  }
  // ...existing resume
})

An approval answered while its own run's stream is still open keeps working, since lastResume.runId is still that run when the stream ends. hydrate() already clears the manager's activeSubmissionOperation, so the false from the dropped submission does not mark the new batch as failed.

A related question: should reload() clear the pending interrupts of the run it replaces, so the old card cannot be answered while the new run streams? With that, step 4 above could not happen at all.

  1. tanstack-source added labelsbug

  2. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.