Skip to content

Adapter-emitted RUN_ERROR can bypass onError and leave persisted runs active #1171

Closed

Description

@kolaworld

TanStack AI version

0.46.0

Framework/Library version

@tanstack/ai-persistence: 0.2.0

Describe the bug and the steps to reproduce it

When a text adapter emits RUN_ERROR during an ordinary chat run without structured output, the chat engine stops processing but can return before calling the middleware terminal lifecycle.

handleRunErrorEvent() always sets earlyTermination, but records a finalizationError only when structured output is configured: index.ts#L1626-L1643.

The next agent-loop check returns immediately when earlyTermination is set: index.ts#L1075-L1078.

That return bypasses the later terminal-hook block that would call onError: index.ts#L1140-L1169.

This is observable without persistence: middleware receives neither onError nor another terminal callback for the failed run.

With withPersistence, the effect becomes durable because RunStore is marked failed only from the middleware's onError hook: middleware.ts#L1765-L1767.

The client receives RUN_ERROR, while the corresponding stored run can remain in its active status indefinitely.

Expected behavior

Every adapter-emitted RUN_ERROR should terminate the current chat invocation through onError exactly once, regardless of whether structured output is configured.

The original RUN_ERROR should remain visible to the stream consumer. Middleware should receive the same error and code, onFinish should not run, and withPersistence should mark the run failed.

Provider-specific recovery may prevent a recoverable error from becoming RUN_ERROR, but it must not be required for the core terminal lifecycle to remain correct.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://stackblitz.com/edit/vitejs-vite-srnby8ol?file=package.json,index.html,src%2Fmain.ts

Do you intend to try to help solve this bug with your own PR?

Yes, I am also opening a PR that solves the problem along side this issue

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Activity

  1. added
    waiting-on: maintainer
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting-on: maintainer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions