Repository navigation
Description
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)
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