withPersistence stores the durability batch hint on a stopped or failed assistant message
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
Issue này chưa được đánh giá.
Mô tả
TanStack AI version
@tanstack/ai 0.69.0, @tanstack/ai-persistence 0.9.0 (the same code is on main at 85c2956)
Framework/Library version
Node 26.3.1, server only (no UI framework involved)
Describe the bug and the steps to reproduce it
With snapshotStreaming: true, withPersistence keeps a metadata.tanstack.batch: true key on the stored assistant message when a run is stopped or fails after a middleware called ctx.emitCustomEvent(name, value, { batch: true }).
This is about the message store (stores.messages, which withPersistence writes and reconstructChat reads). It is not about the durability log, the delivery stream that toServerSentEventsResponse({ durability }) appends to. They are separate stores.
The batch hint is an in-process flag for the durability producer. #1355 says it "is stripped before the log and the wire", and stripDurabilityBatchHint does that in toServerSentEventsResponse and in the strip-to-spec middleware. That covers the durability log and the wire. flush() in stream-to-response.ts strips each chunk before durability.append and before forwarding it. The emitted CUSTOM chunk also passes through the middleware chain, though, before any of that. In withPersistence, onChunk merges chunk.metadata of every parent chunk into the streaming snapshot metadata (packages/ai-persistence/src/middleware.ts, the block under "Keep the call metadata (source, response id) for the snapshot rows."). The hint goes in with it. Every later snapshot row carries it. On Stop or on an error, updateStreamingRows(ctx, 'aborted' | 'error') writes the merged metadata to the final stored row, so the hint stays in storage and comes back from reconstructChat. A run that finishes normally is clean, because the final save replaces the snapshot rows.
Steps:
- Install
@tanstack/[email protected]and@tanstack/[email protected]. - Save the script below as
repro.mjsin an ESM package and runnode repro.mjs.
repro.mjs:
import { chat, defineChatMiddleware } from '@tanstack/ai'
import { memoryPersistence, withPersistence } from '@tanstack/ai-persistence'
const controller = new AbortController()
const adapter = {
kind: 'text',
name: 'fixed',
model: 'test-model',
'~types': {},
async *chatStream() {
const t = Date.now()
yield { type: 'RUN_STARTED', threadId: 'thread-1', runId: 'run-1', timestamp: t }
yield { type: 'TEXT_MESSAGE_START', messageId: 'msg-1', role: 'assistant', timestamp: t }
yield { type: 'TEXT_MESSAGE_CONTENT', messageId: 'msg-1', delta: 'partial', timestamp: t }
controller.abort() // the user presses Stop
},
}
const progress = defineChatMiddleware({
name: 'progress',
onChunk(ctx, chunk) {
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
ctx.emitCustomEvent('progress', { step: 1 }, { batch: true })
}
},
})
const persistence = memoryPersistence()
const stream = chat({
adapter,
messages: [{ role: 'user', content: 'hi' }],
threadId: 'thread-1',
runId: 'run-1',
abortController: controller,
middleware: [
withPersistence(persistence, { snapshotStreaming: true, snapshotIntervalMs: 0 }),
progress,
],
})
for await (const _ of stream) void _
const stored = await persistence.stores.messages.loadThread('thread-1')
console.log(JSON.stringify(stored.at(-1).metadata))
Expected
{"tanstack":{"source":{"provider":"fixed","api":"text","model":"test-model"},"stopReason":"aborted"}}
Actual
{"tanstack":{"source":{"provider":"fixed","api":"text","model":"test-model"},"batch":true,"stopReason":"aborted"}}
If the adapter throws instead of the abort, the row ends with "batch":true,"stopReason":"error". A snapshot row read in the middle of the run (after a second TEXT_MESSAGE_CONTENT) is {"tanstack":{"source":{...},"batch":true}}.
I found nothing that reads tanstack.batch on a stored message, so the visible effect is a stray internal key in the message store and in the hydrated messages. Any store that validates message metadata will see an unknown key. The hint is meant to stay in process, as the durability log and the wire already show, but the message store keeps it.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
Inline above. Plain Node script, no network, no model.
Screenshots or Videos (Optional)
None.
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 3.2k
- Fork
- 365
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 249
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/ai
-
withPersistence does not save activity rows when a run is stopped or failsCó thể đã có người làm @tombeckenham đã nhận 2 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#1804 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Activity events that a chat middleware emits are streamed but never recorded or persistedCó thể đã có người làm @AlemTuzlak đã nhận 2 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#1803 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Discriminate status-dependent record and option types (RunRecord, SessionIndexEntry, InputState, durable-only options)Có thể đã có người làm @jherr đã nhận 2 ngày trước. Đang mởenhancement waiting-on: maintainer
TanStack/ai#1799 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ai-harness: an input with no principal runs with the owner session's principalCó thể đã có người làm @tombeckenham đã nhận 3 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#1797 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
-
ai-harness: a forwarded command completes with no result, also when the owner rejects itCó thể đã có người làm @tombeckenham đã nhận 3 ngày trước. Đang mởwaiting-on: maintainer
TanStack/ai#1796 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
Object parameters lose their structure when resources is enabledCó thể đã có người làm @asts-top đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 2 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
jaegertracing/jaeger-ui#4618 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
pending close triaging
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
anomalyco/opencode#56765 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area:plugins enhancement platform:vscode
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
anthropics/claude-code#103166 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Types: FlashListProps imports ViewabilityConfig from react-native, removed in RN 0.87+Có thể đã có người làm @efegokdemir đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Shopify/flash-list#2540 ·