feat: add AI persistence and generation resumability #785
Summary#
- Adds generation artifact persistence on top of
withPersistence, including input/output artifact refs, blob-backed media storage,generation:artifactsevents, and custom extraction/name hooks. - Adds generation run identity, replay, request parsing helpers, and lightweight client resume snapshots for generation hooks.
- Wires resumable generation APIs across React, Solid, Vue, Svelte, and Angular, with docs for resumable generations, artifacts, Cloudflare/R2, and hook API fields.
Validation#
pnpm test:docsgit diff --check HEAD- Focused typechecks for
@tanstack/ai,@tanstack/ai-client,@tanstack/ai-persistence,@tanstack/ai-event-client, React, Solid, Vue, Svelte, and Angular packages - Focused generation/persistence/hook test suites for core, ai-client, ai-persistence, React, Solid, Vue, Svelte, and Angular
Full pnpm test:pr was not run per request; validation was scoped to the changed packages and docs.
- 1
🚀 Changeset Version Preview#
14 package(s) bumped directly, 36 bumped as dependents.
🟥 Major bumps#
Package Version Reason @tanstack/ai-angular0.2.4 → 1.0.0 Changeset @tanstack/ai-persistence0.0.0 → 1.0.0 Changeset @tanstack/ai-persistence-cloudflare0.0.0 → 1.0.0 Changeset @tanstack/ai-persistence-drizzle0.0.0 → 1.0.0 Changeset @tanstack/ai-persistence-prisma0.0.0 → 1.0.0 Changeset @tanstack/ai-preact0.10.4 → 1.0.0 Changeset @tanstack/ai-react0.17.0 → 1.0.0 Changeset @tanstack/ai-sandbox0.2.3 → 1.0.0 Changeset @tanstack/ai-solid0.14.4 → 1.0.0 Changeset @tanstack/ai-svelte0.14.4 → 1.0.0 Changeset @tanstack/ai-vue0.14.4 → 1.0.0 Changeset @tanstack/ai-acp0.2.2 → 1.0.0 Dependent @tanstack/ai-anthropic0.16.2 → 1.0.0 Dependent @tanstack/ai-bedrock0.1.3 → 1.0.0 Dependent @tanstack/ai-claude-code0.2.2 → 1.0.0 Dependent @tanstack/ai-code-mode0.3.7 → 1.0.0 Dependent @tanstack/ai-code-mode-skills0.3.10 → 1.0.0 Dependent @tanstack/ai-codex0.2.2 → 1.0.0 Dependent @tanstack/ai-elevenlabs0.2.33 → 1.0.0 Dependent @tanstack/ai-fal0.9.11 → 1.0.0 Dependent @tanstack/ai-gemini0.20.0 → 1.0.0 Dependent @tanstack/ai-grok0.14.8 → 1.0.0 Dependent @tanstack/ai-grok-build0.2.2 → 1.0.0 Dependent @tanstack/ai-groq0.5.2 → 1.0.0 Dependent @tanstack/ai-isolate-node0.1.46 → 1.0.0 Dependent @tanstack/ai-isolate-quickjs0.1.46 → 1.0.0 Dependent @tanstack/ai-mistral0.2.2 → 1.0.0 Dependent @tanstack/ai-ollama0.8.15 → 1.0.0 Dependent @tanstack/ai-openai0.17.0 → 1.0.0 Dependent @tanstack/ai-opencode0.2.2 → 1.0.0 Dependent @tanstack/ai-openrouter0.15.9 → 1.0.0 Dependent @tanstack/ai-react-ui0.8.14 → 1.0.0 Dependent @tanstack/ai-sandbox-cloudflare0.2.3 → 1.0.0 Dependent @tanstack/ai-sandbox-daytona0.2.0 → 1.0.0 Dependent @tanstack/ai-sandbox-docker0.2.0 → 1.0.0 Dependent @tanstack/ai-sandbox-local-process0.2.0 → 1.0.0 Dependent @tanstack/ai-sandbox-sprites0.2.1 → 1.0.0 Dependent @tanstack/ai-sandbox-vercel0.2.0 → 1.0.0 Dependent @tanstack/ai-solid-ui0.7.13 → 1.0.0 Dependent @tanstack/openai-base0.9.8 → 1.0.0 Dependent 🟨 Minor bumps#
Package Version Reason @tanstack/ai0.41.0 → 0.42.0 Changeset @tanstack/ai-client0.21.0 → 0.22.0 Changeset 🟩 Patch bumps#
Package Version Reason @tanstack/ai-event-client0.6.8 → 0.6.9 Changeset @tanstack/ai-devtools-core0.4.23 → 0.4.24 Dependent @tanstack/ai-isolate-cloudflare0.2.37 → 0.2.38 Dependent @tanstack/ai-mcp0.2.4 → 0.2.5 Dependent @tanstack/ai-vue-ui0.2.32 → 0.2.33 Dependent @tanstack/preact-ai-devtools0.1.66 → 0.1.67 Dependent @tanstack/react-ai-devtools0.2.66 → 0.2.67 Dependent @tanstack/solid-ai-devtools0.2.66 → 0.2.67 Dependent View your CI Pipeline Execution ↗ for commit 090f86d2c12fe63221e2f787985e8877cac1a1b8
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 2m 10s View ↗
☁️ Nx Cloud last updated this comment at
2026-07-17 06:58:31UTCReview the following changes in direct dependencies. Learn more about Socket for GitHub.
Diff Package Supply Chain
SecurityVulnerability Quality Maintenance License prisma@6.19.3 fake-indexeddb@6.2.5 @prisma/client@6.19.3 @ag-ui/core@0.0.57 drizzle-kit@0.31.10 @tanstack/ai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@785@tanstack/ai-acp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@785@tanstack/ai-angular
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@785@tanstack/ai-anthropic
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@785@tanstack/ai-bedrock
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@785@tanstack/ai-claude-code
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@785@tanstack/ai-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@785@tanstack/ai-code-mode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@785@tanstack/ai-code-mode-skills
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-skills@785@tanstack/ai-codex
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@785@tanstack/ai-devtools-core
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@785@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@785@tanstack/ai-event-client
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@785@tanstack/ai-fal
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@785@tanstack/ai-gemini
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@785@tanstack/ai-grok
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@785@tanstack/ai-grok-build
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@785@tanstack/ai-groq
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@785@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@785@tanstack/ai-isolate-node
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@785@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@785@tanstack/ai-mcp
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@785@tanstack/ai-mistral
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@785@tanstack/ai-ollama
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@785@tanstack/ai-openai
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@785@tanstack/ai-opencode
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@785@tanstack/ai-openrouter
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@785@tanstack/ai-persistence
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence@785@tanstack/ai-persistence-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence-cloudflare@785@tanstack/ai-persistence-drizzle
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence-drizzle@785@tanstack/ai-persistence-prisma
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-persistence-prisma@785@tanstack/ai-preact
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@785@tanstack/ai-react
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@785@tanstack/ai-react-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@785@tanstack/ai-sandbox
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@785@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@785@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@785@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@785@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@785@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@785@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@785@tanstack/ai-solid
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@785@tanstack/ai-solid-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@785@tanstack/ai-svelte
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@785@tanstack/ai-utils
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@785@tanstack/ai-vue
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@785@tanstack/ai-vue-ui
npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@785@tanstack/openai-base
npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@785@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@785@tanstack/react-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@785@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@785commit:
090f86dAwesome to see that this is in progress, it was one of the final bits that prevented it from feeling completely batteries included!
AlemTuzlak changed the title
Actionable comments posted: 10
Note
Due to the large number of review comments, Critical, Major severity comments were prioritized as inline comments.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
packages/ai/src/activities/chat/index.ts (1)
1164-1191: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy liftDefer tool-call terminals before middleware side effects run.
runOnChunk()runs beforeshouldDeferToolCallRunFinished(), so persistence/devtools middleware can record the originalRUN_FINISHEDeven whendiscardDeferredToolCallRunFinishedChunks()later hides it and an interrupt terminal is emitted instead. That makes replayable event history diverge from the public stream.Proposed direction
- const outputChunks = await this.middlewareRunner.runOnChunk( - this.middlewareCtx, - chunk, - ) // When a streaming structured-output finalization step will run after // the agent loop, suppress the agent-loop's RUN_STARTED/RUN_FINISHED // here — the finalization step emits the single outer lifecycle pair // that reaches the consumer. // // Native combined mode does NOT issue a second adapter stream — the // agent loop's lifecycle IS the outer pair the consumer sees. const suppressAgentLifecycle = !!this.finalStructuredOutput && this.finalStructuredOutput.yieldChunks && this.finalStructuredOutput.nativeCombined !== true + + if (!suppressAgentLifecycle && this.shouldDeferToolCallRunFinished(chunk)) { + this.deferredToolCallRunFinishedChunks.push(chunk) + continue + } + + const outputChunks = await this.middlewareRunner.runOnChunk( + this.middlewareCtx, + chunk, + )- private *flushDeferredToolCallRunFinishedChunks(): Generator<StreamChunk> { - for (const chunk of this.deferredToolCallRunFinishedChunks) { - this.logger.output(`type=${chunk.type}`, { chunk }) - yield chunk - this.middlewareCtx.chunkIndex++ - } + private async *flushDeferredToolCallRunFinishedChunks(): AsyncGenerator<StreamChunk> { + const chunks = this.deferredToolCallRunFinishedChunks this.deferredToolCallRunFinishedChunks = [] + for (const chunk of chunks) { + yield* this.pipeThroughMiddleware(chunk) + } }Also applies to: 1457-1475, 1620-1633, 1659-1680
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/activities/chat/index.ts` around lines 1164 - 1191, The chunk-processing flow is recording tool-call terminal events in middleware before they are marked for deferral, which can leave persisted/devtools history out of sync with the public stream. In the chat event handling loops around runOnChunk(), check shouldDeferToolCallRunFinished() before calling middlewareRunner.runOnChunk() for RUN_FINISHED-like chunks, and route deferred terminals into deferredToolCallRunFinishedChunks first so discardDeferredToolCallRunFinishedChunks() can safely suppress them later. Apply the same ordering wherever these chunk loops appear to keep replayable history and emitted lifecycle events aligned.packages/ai-react/src/use-chat.ts (1)
257-341: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winSame stale-client race as the Preact binding.
sendMessage/append/reload/clear/addToolApprovalResponse/resume/resumeInterruptscallsyncResumeState(client)from closures bound to whateverclientwas active when the callback was created. Ifclientis recreated (e.g.,idprop changes) while one of these is in-flight, the stale closure'sfinallycan overwriteresumeState/pendingInterruptswith data from the disposed client — the same hazard theactiveClientRef.current !== instanceguard on the otherChatClientcallbacks (Lines 123-182) is designed to prevent.🛠️ Proposed fix
- const syncResumeState = useCallback((target: ChatClient | null) => { - if (!target) return - setResumeState(target.getResumeState()) - setPendingInterrupts(target.getPendingInterrupts()) - }, []) + const syncResumeState = useCallback((target: ChatClient | null) => { + if (!target || activeClientRef.current !== target) return + setResumeState(target.getResumeState()) + setPendingInterrupts(target.getPendingInterrupts()) + }, [])🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-react/src/use-chat.ts` around lines 257 - 341, The callbacks in use-chat.ts can still sync state from a stale ChatClient after the active client changes, causing old resume data to overwrite the current instance. Add the same active-client guard used in the ChatClient callbacks (compare against a current ref before calling syncResumeState) to sendMessage, append, reload, clear, addToolApprovalResponse, resume, and resumeInterrupts so only the live client can update resumeState and pendingInterrupts.packages/ai-preact/src/use-chat.ts (1)
250-333: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winStale-client race when syncing resume state after async operations.
sendMessage/append/reload/clear/addToolApprovalResponse/resume/resumeInterruptscallsyncResumeState(client)whereclientis the closure-captured memoized instance. IfclientIdchanges (client recreated) while one of these is in flight, the old closure still resolves and overwritesresumeState/pendingInterruptswith data from the now-inactive client — the exact scenario theactiveClientRef.current !== instanceguard on the otherChatClientcallbacks (Lines 121-180) exists to prevent.🛠️ Proposed fix: sync against the currently-active client
- const syncResumeState = useCallback((target: ChatClient | null) => { - if (!target) return - setResumeState(target.getResumeState()) - setPendingInterrupts(target.getPendingInterrupts()) - }, []) + const syncResumeState = useCallback((target: ChatClient | null) => { + if (!target || activeClientRef.current !== target) return + setResumeState(target.getResumeState()) + setPendingInterrupts(target.getPendingInterrupts()) + }, [])🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-preact/src/use-chat.ts` around lines 250 - 333, The async callbacks in useChat currently call syncResumeState with the closure-captured client, which can let an old client overwrite resumeState or pendingInterrupts after clientId changes. Update sendMessage, append, reload, clear, addToolApprovalResponse, resume, and resumeInterrupts to sync only if the resolved client is still the active one, following the same activeClientRef.current !== instance pattern used by ChatClient callbacks. Use the current client instance/guard in useChat and keep syncResumeState tied to the latest active client before mutating resume-related state.🟡 Minor comments (19)
packages/ai/package.json-88-88 (1)
88-88: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win@ag-ui/core0.0.57 changesEventTypeTHINKING_*events are deprecated in favor ofREASONING_*, so theEventTypeconsumers inpackages/ai/src/types.tsandpackages/ai/src/activities/summarize/chat-stream-summarize.tsneed a compatibility pass before this bump lands.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/package.json` at line 88, The `@ag-ui/core` bump introduces an EventType rename, so the EventType consumers need to stay compatible with both THINKING_* and REASONING_* variants. Update the handling in packages/ai/src/types.ts and packages/ai/src/activities/summarize/chat-stream-summarize.ts to accept/map the new REASONING_* events while preserving support for existing THINKING_* cases, using the existing EventType-related symbols in those modules.docs/architecture/approval-flow-processing.md-224-234 (1)
224-234: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winAdd a language tag to the pseudocode fence.
The block at Line 226 is untyped, which trips markdownlint
MD040. Mark it astextso the docs build stays clean.🛠️ Proposed fix
-``` +```text handleRunFinished(): 1. updateToolCallApproval(messages, messageId, "tc-1", "appr-1") → Sets part.state = "approval-requested" → Sets part.approval = { id: "appr-1", needsApproval: true } 2. records the pending interrupt on the client 3. emitMessagesChange() 4. fires onApprovalRequest({ toolCallId, toolName, input, approvalId })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@docs/architecture/approval-flow-processing.md` around lines 224 - 234, Add a language tag to the pseudocode code fence in the approval-flow-processing documentation so the markdownlint MD040 warning is avoided. Update the untyped fenced block that contains the handleRunFinished pseudocode to use a text fence, keeping the existing content unchanged and ensuring the docs build remains clean.Source: Linters/SAST tools
packages/ai/skills/ai-core/tool-calling/SKILL.md-333-345 (1)
333-345: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winHandle every pending interrupt, not just the first one.
pendingInterrupts[0]leaves any additional waits unresolved, so a thread with multiple approvals can stay blocked. Either iterate the whole array or state explicitly that this sample only handles a single interrupt.🛠️ Proposed fix
-await resumeInterrupts([ - { - interruptId: pendingInterrupts[0].id, - status: 'resolved', - payload: { approved: true }, - }, -]) +await resumeInterrupts( + pendingInterrupts.map((interrupt) => ({ + interruptId: interrupt.id, + status: 'resolved', + payload: { approved: true }, + })), +)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/skills/ai-core/tool-calling/SKILL.md` around lines 333 - 345, The interrupt-resume example in SKILL.md only uses pendingInterrupts[0], which leaves additional pending waits unresolved. Update the sample around resumeInterrupts to either iterate over the full pendingInterrupts array and resume each item, or clearly label it as a single-interrupt example; reference pendingInterrupts and resumeInterrupts so readers know the intended handling for multiple approvals.packages/ai/tests/resume-seam.test.ts-167-174 (1)
167-174: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix
require-yieldlint failure on the no-op replay generator.Static analysis flags this generator for never yielding. Since it always throws, either add an eslint-disable comment or restructure to avoid the generator syntax.
🔧 Suggested fix
const source: ResumeSource = { hasRun: async () => false, + // eslint-disable-next-line require-yield -- intentional: always throws before yielding replay: async function* () { throw new Error('replay should not be called') }, getStatus: async () => null, }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/tests/resume-seam.test.ts` around lines 167 - 174, The no-op replay implementation in resume-seam.test.ts triggers the require-yield lint rule because the async generator in the ResumeSource.replay stub never yields and only throws. Fix this by either adding the appropriate eslint-disable comment for require-yield at the replay stub, or by restructuring the test double so it no longer uses generator syntax while still satisfying ResumeSource.replay and preserving the “replay should not be called” behavior.Source: Linters/SAST tools
packages/ai/tests/resume-seam.test.ts-7-7 (1)
7-7: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import ordering/sorting per ESLint.
./test-utilsshould be imported before the type import of../src/resume, andcreateMockAdaptershould be alphabetically sorted within the import.🔧 Suggested fix
-import { ev, createMockAdapter, collectChunks, getDeltas } from './test-utils' +import { collectChunks, createMockAdapter, ev, getDeltas } from './test-utils'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/tests/resume-seam.test.ts` at line 7, The import ordering in the test file is violating ESLint’s sort rules: move the `./test-utils` import ahead of the `../src/resume` type import, and reorder the named imports so `createMockAdapter` is alphabetically placed within the `ev`, `createMockAdapter`, `collectChunks`, `getDeltas` list. Use the existing import statements in `resume-seam.test.ts` to adjust the order only, keeping the same symbols and source paths.Source: Linters/SAST tools
packages/ai/tests/chat.test.ts-571-577 (1)
571-577: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winAssert the tool result exists before checking ordering.
findIndex()returns-1when noTOOL_CALL_RESULTis emitted, and any realrunFinishedIndexwill still be greater than-1, so these tests can pass while missing the server-tool result entirely.Proposed fix
const toolResultIndex = chunks.findIndex( (c) => c.type === 'TOOL_CALL_RESULT' && 'content' in c && (c as any).content, ) + expect(toolResultIndex).toBeGreaterThanOrEqual(0) const runFinished = expectSingleRunFinished(chunks) const runFinishedIndex = chunks.indexOf(runFinished) expect(runFinishedIndex).toBeGreaterThan(toolResultIndex)Also applies to: 659-665
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/tests/chat.test.ts` around lines 571 - 577, The ordering assertion in the chat tests can pass even when no TOOL_CALL_RESULT chunk exists because chunks.findIndex() returns -1, so update the checks around expectSingleRunFinished and the tool result lookup to first assert that a valid tool result was emitted, then verify runFinishedIndex comes after it. Apply the same fix in both affected test cases in chat.test.ts, using the TOOL_CALL_RESULT lookup and runFinishedIndex comparison to make the intent explicit.packages/ai/tests/chat.test.ts-494-496 (1)
494-496: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winRemove the unnecessary stream cast.
ESLint flags Line 495 as an unnecessary type assertion;
collectChunksalready accepts the stream type here.Proposed fix
- const chunks = await collectChunks( - stream as unknown as AsyncIterable<StreamChunk>, - ) + const chunks = await collectChunks(stream)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/tests/chat.test.ts` around lines 494 - 496, The stream passed into collectChunks in chat.test.ts is being cast unnecessarily, and ESLint flags it as a redundant type assertion. Remove the cast at the collectChunks call site and pass the stream directly, since the existing stream type already matches what collectChunks accepts. Use the collectChunks invocation in the test as the target for the cleanup.Source: Linters/SAST tools
packages/ai/tests/stream-processor.test.ts-8-14 (1)
8-14: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order (ESLint
sort-imports).collectChunksis not alphabetically ordered relative tocreateMockAdapter.🔧 Suggested fix
import { - createMockAdapter, - collectChunks, - serverTool, clientTool, + collectChunks, + createMockAdapter, ev as chatEv, + serverTool, } from './test-utils'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/tests/stream-processor.test.ts` around lines 8 - 14, The import list in the test file is not sorted alphabetically, causing the ESLint sort-imports warning. Reorder the named imports in the existing test-utils import so they are alphabetized, keeping createMockAdapter, collectChunks, serverTool, clientTool, and ev as chatEv in the correct order. Use the import declaration in stream-processor.test.ts as the target location.Source: Linters/SAST tools
packages/ai/src/activities/chat/stream/processor.ts-1310-1339 (1)
1310-1339: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winPrefer the tool call's owner message over "active" message when resolving approval target.
resolvedMessageIdpicksgetActiveAssistantMessageId()first and only falls back totoolCallToMessage.get(toolCallId). Elsewhere in this same file, theui-resourceCUSTOM handler does the reverse and explains why: the tool call's owner message is authoritative to avoid misattaching state to the wrong message in multi-message sessions. If aRUN_FINISHEDinterrupts batch contains tool calls owned by a message other than the currently "active" one,updateToolCallApprovalwill patch the wrong message. TheonApprovalRequestcallback itself still carries the correcttoolCallId/approvalId, so the blast radius is limited to the UI-rendered state.🔧 Suggested fix
- const resolvedMessageId = - this.getActiveAssistantMessageId() ?? - this.toolCallToMessage.get(toolCallId) + const resolvedMessageId = + this.toolCallToMessage.get(toolCallId) ?? + this.getActiveAssistantMessageId()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/activities/chat/stream/processor.ts` around lines 1310 - 1339, The approval target resolution in handleInterrupts currently prefers getActiveAssistantMessageId() before the tool call’s owning message, which can attach approval state to the wrong assistant message in multi-message sessions. Update the resolvedMessageId logic in handleInterrupts to prioritize this.toolCallToMessage.get(toolCallId) first and only fall back to getActiveAssistantMessageId() if no owner mapping exists, matching the authoritative ownership behavior used elsewhere in the file. Keep the rest of updateToolCallApproval and emitMessagesChange flow unchanged.packages/ai-client/tests/connection-adapters.test.ts-5-13 (1)
5-13: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order per ESLint
sort-imports.fetcherToConnectionAdapteris listed beforefetchServerSentEvents, but the rule sorts case-sensitively (uppercaseS< lowercasee), sofetchServerSentEventsshould come first.🔧 Reorder imports
import { - fetcherToConnectionAdapter, fetchServerSentEvents, + fetcherToConnectionAdapter, normalizeConnectionAdapter, rpcStream, stream, } from '../src/connection-adapters'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-client/tests/connection-adapters.test.ts` around lines 5 - 13, The imports in connection-adapters.test.ts are not in ESLint sort-imports order. Reorder the named imports from ../src/connection-adapters so fetchServerSentEvents comes before fetcherToConnectionAdapter, keeping normalizeConnectionAdapter, rpcStream, and stream in the correct case-sensitive order. Preserve the existing import grouping and make no other changes.Source: Linters/SAST tools
packages/ai-solid/src/use-chat.ts-269-282 (1)
269-282: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winresume/resumeInterruptsskip the resync-on-error safeguard used elsewhere in this file.sendMessage,append, andreloadwere just wrapped intry/finallyspecifically sosyncResumeState()still runs when the underlying call throws.resumeandresumeInterrupts— the two methods most likely to encounter a failed reconnect/replay — don't get the same treatment, so a rejectedclient().resume(...)/resumeInterrupts(...)call leavesresumeState/pendingInterruptsstale.♻️ Proposed fix
const resume = async (state?: ChatResumeState) => { - const result = await client().resume(state) - syncResumeState() - return result + try { + return await client().resume(state) + } finally { + syncResumeState() + } } const resumeInterrupts = async ( resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState, ) => { - const result = await client().resumeInterrupts(resumeItems, state) - syncResumeState() - return result + try { + return await client().resumeInterrupts(resumeItems, state) + } finally { + syncResumeState() + } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-solid/src/use-chat.ts` around lines 269 - 282, The resume flow in use-chat is missing the same resync-on-error protection already applied to sendMessage, append, and reload. Update resume and resumeInterrupts to use try/finally around client().resume(...) and client().resumeInterrupts(...) so syncResumeState() always runs, even when the client call rejects. This keeps resumeState and pendingInterrupts in sync after failed reconnect/replay attempts.testing/e2e/src/components/ChatUI.tsx-53-54 (1)
53-54: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winBlock image auto-send while an interrupt is pending.
The new guard only covers
handleSubmit; the file input path bypasses it and callsonSendMessageWithImagedirectly. IfshowImageInputandhasPendingInterruptare both true, users can still submit new input.Proposed fix
{showImageInput && ( <input type="file" accept="image/*" data-testid="image-attachment-input" + disabled={hasPendingInterrupt} className="text-xs text-gray-400" onChange={(e) => { + if (hasPendingInterrupt) { + e.currentTarget.value = '' + return + } const file = e.target.files?.[0]Also applies to: 235-241
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@testing/e2e/src/components/ChatUI.tsx` around lines 53 - 54, The interrupt guard in handleSubmit only blocks text submit, but the image upload path still bypasses it and can call onSendMessageWithImage while hasPendingInterrupt is true. Add the same pending-interrupt check to the file input/image send flow in ChatUI.tsx, reusing the existing guard logic around showImageInput and the image handler so both submission paths are blocked consistently.packages/ai-vue/src/use-chat.ts-270-282 (1)
270-282: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winSync resume refs on failed resume attempts too.
Line 271 and Line 280 only call
syncResumeState()after a successful await, unlikesendMessage/append/reload. Ifclient.resume()rejects, Vue state can keep staleresumeState/pendingInterrupts.Proposed fix
const resume = async (state?: ChatResumeState) => { - const result = await client.resume(state) - syncResumeState() - return result + try { + return await client.resume(state) + } finally { + syncResumeState() + } } const resumeInterrupts = async ( resumeItems: Array<RunAgentResumeItem>, state?: ChatResumeState, ) => { - const result = await client.resumeInterrupts(resumeItems, state) - syncResumeState() - return result + try { + return await client.resumeInterrupts(resumeItems, state) + } finally { + syncResumeState() + } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-vue/src/use-chat.ts` around lines 270 - 282, The `resume` and `resumeInterrupts` helpers in `use-chat.ts` only call `syncResumeState()` after a successful `await`, so stale `resumeState`/`pendingInterrupts` can remain when `client.resume()` or `client.resumeInterrupts()` throws. Update these methods to mirror the `sendMessage`/`append`/`reload` pattern by ensuring `syncResumeState()` runs even on failures, ideally by wrapping the client call in a `try/finally` so the Vue refs are always refreshed.testing/e2e/src/routes/api.chat.ts-9-13 (1)
9-13: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winSplit and reorder the persistence import to satisfy lint.
ESLint reports
import/order,sort-imports, andimport/consistent-type-specifier-styleon this import.Proposed fix
-import type { StreamChunk } from '`@tanstack/ai`' -import { - memoryPersistence, - withPersistence, - type AIPersistence, -} from '`@tanstack/ai-persistence`' +import { memoryPersistence, withPersistence } from '`@tanstack/ai-persistence`' +import type { StreamChunk } from '`@tanstack/ai`' +import type { AIPersistence } from '`@tanstack/ai-persistence`'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@testing/e2e/src/routes/api.chat.ts` around lines 9 - 13, The persistence import in api.chat.ts is tripping import/order, sort-imports, and import/consistent-type-specifier-style, so split the combined import from `@tanstack/ai-persistence` into separate value and type imports and order the specifiers consistently. Update the import near memoryPersistence, withPersistence, and AIPersistence so the value imports are grouped and the type-only import uses the preferred type-specifier style.Source: Linters/SAST tools
packages/ai-persistence/tests/memory.test.ts-4-11 (1)
4-11: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winMerge duplicate
'../src/types'imports.ESLint (
import/no-duplicates) flags separate import statements from the same module.🔧 Suggested fix
import { EventType } from '`@tanstack/ai`' import { memoryPersistence } from '../src/memory' import { + AppendConflictError, defineAIPersistence, defineChatPersistence, validatePersistenceFeatures, } from '../src/types' -import { AppendConflictError } from '../src/types' import type { StreamChunk } from '`@tanstack/ai`' import type { AIPersistence, ChatPersistence } from '../src/types'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/tests/memory.test.ts` around lines 4 - 11, Merge the duplicate imports from '../src/types' in memory.test.ts to satisfy import/no-duplicates. Combine the named imports and the type-only imports into a single import statement where possible, keeping the symbols defineAIPersistence, defineChatPersistence, validatePersistenceFeatures, AppendConflictError, AIPersistence, and ChatPersistence together under the same module reference.Source: Linters/SAST tools
packages/ai-persistence/src/middleware.ts-419-440 (1)
419-440: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick winonErrorcan overwrite an already-interruptedrun with'failed'.onFinish(Line 420) explicitly guardsif (runState.get(ctx)?.interrupted) returnbefore marking a run'completed', butonErrorhas no equivalent guard — if an error is thrown afteronChunkalready persisted the interrupt outcome and setrunState.interrupted = true(Lines 388-414),onErrorwould still overwrite the run's terminal status to'failed', losing the'interrupted'state that resume logic depends on.🛠️ Suggested guard
async onError(ctx: ChatMiddlewareContext, info) { + if (runState.get(ctx)?.interrupted) return await runs?.update(ctx.runId, { status: 'failed', finishedAt: Date.now(), error: info.error instanceof Error ? info.error.message : String(info.error), }) },🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/middleware.ts` around lines 419 - 440, The onError handler in middleware.ts can overwrite an already interrupted run by updating it to failed after onChunk has marked runState.interrupted. Add the same interrupted-state check used in onFinish inside onError, keyed off runState.get(ctx)?.interrupted, so interrupted runs keep their terminal status and only non-interrupted errors call runs.update with status: 'failed'. Refer to onError, onFinish, and runState.get(ctx) when applying the guard.packages/ai-persistence/tests/interrupts.test.ts-1-8 (1)
1-8: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order (ESLint
import/order).🔧 Proposed fix
import { describe, expect, it, vi } from 'vitest' import { EventType, chat } from '`@tanstack/ai`' -import type { AnyTextAdapter, StreamChunk, Tool } from '`@tanstack/ai`' import { createApprovalController } from '../src/approval-controller' import { memoryPersistence } from '../src/memory' import { withPersistence } from '../src/middleware' import { defineAIPersistence, validatePersistenceFeatures } from '../src/types' +import type { AnyTextAdapter, StreamChunk, Tool } from '`@tanstack/ai`'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/tests/interrupts.test.ts` around lines 1 - 8, The imports in the test module are out of order and violate ESLint import/order. Reorganize the top-level imports in interrupts.test.ts so they follow the project’s import grouping and sorting rules, keeping related imports from `@tanstack/ai` together and placing type-only imports in the correct position relative to value imports. Use the existing import block near describe, EventType, createApprovalController, and defineAIPersistence as the location to fix.Source: Linters/SAST tools
packages/ai-persistence/tests/history.test.ts-1-5 (1)
1-5: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order (ESLint
import/order).🔧 Proposed fix
import { describe, expect, it } from 'vitest' import { EventType } from '`@tanstack/ai`' -import type { StreamChunk } from '`@tanstack/ai`' import { memoryPersistence } from '../src/memory' import { loadRunHistory } from '../src/history' +import type { StreamChunk } from '`@tanstack/ai`'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/tests/history.test.ts` around lines 1 - 5, The import block in history.test.ts violates the import/order rule; reorder the existing imports so external package imports are grouped and sorted consistently before the relative imports, while keeping the current symbols describe, expect, it, EventType, StreamChunk, memoryPersistence, and loadRunHistory intact. Adjust only the order of the import statements to satisfy ESLint without changing any imported names or behavior.Source: Linters/SAST tools
packages/ai-persistence-prisma/tests/prisma.test.ts-2-5 (1)
2-5: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order per ESLint.
Static analysis flags
node:sqliteimport ordering beforevitest, and../src/indexbefore the@tanstack/aitype import.🔧 Proposed fix
-import { describe, expect, it } from 'vitest' -import { DatabaseSync } from 'node:sqlite' -import { EventType } from '`@tanstack/ai`' -import type { StreamChunk } from '`@tanstack/ai`' -import { prismaPersistence } from '../src/index' -import type { PrismaRawClient } from '../src/index' +import { DatabaseSync } from 'node:sqlite' +import { describe, expect, it } from 'vitest' +import { EventType } from '`@tanstack/ai`' +import type { StreamChunk } from '`@tanstack/ai`' +import { prismaPersistence } from '../src/index' +import type { PrismaRawClient } from '../src/index'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-prisma/tests/prisma.test.ts` around lines 2 - 5, The imports in prisma.test.ts are out of ESLint order. Reorder the top-level imports so built-in modules like DatabaseSync from node:sqlite come after the test framework import, and keep the `@tanstack/ai` value import before the `@tanstack/ai` type-only import, with prismaPersistence from ../src/index placed in the correct group according to the existing import-order rule.Source: Linters/SAST tools
🧹 Nitpick comments (15)
packages/ai/src/types.ts (1)
954-970: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDoc gap: cursor+resume interaction not documented.
Per
resume-seam.test.ts, when bothcursorandresumeare supplied, replay is skipped and the adapter runs normally (interrupt-resume takes precedence over durable replay). This important interaction isn't captured in thecursordoc comment here, which only describes the plain replay case.📝 Suggested doc addition
/** * Resume cursor for persistence-backed runs. When provided alongside `runId`, * and a resume source has been provided by middleware (e.g. `withPersistence`), * the engine replays persisted events after this cursor instead of running the * adapter fresh. Opaque string; generated/consumed by `@tanstack/ai-persistence`. * Ignored (a no-op) when no resume source is present — so a normal run is * unaffected. See {`@link` StreamChunk.cursor}. + * + * When `resume` entries are also provided, replay is skipped in favor of + * running the adapter with those interrupt resolutions. */🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/types.ts` around lines 954 - 970, Update the doc comments in types.ts for the cursor and resume fields to document their interaction: when both are present, resume handling takes precedence and cursor-based persisted replay is skipped, so the adapter runs normally. Keep the explanation near the existing cursor and resume definitions, and reference the runtime behavior implied by resume-seam.test.ts so future readers understand that cursor is only used for durable replay when no resume payload is supplied.packages/ai/src/activities/chat/stream/processor.ts (1)
1317-1318: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueInterrupts without
toolCallIdare silently dropped.Per the AG-UI spec
toolCallIdis optional onInterrupt, so future/non-tool interrupt reasons could legitimately omit it. Right now they're silently skipped with no observability, unlike theui-resourcehandler whichconsole.warns on an unresolvable target.Confirm whether non-tool-call interrupt reasons are expected to reach this path in the current design; if so, consider logging instead of silently continuing.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/activities/chat/stream/processor.ts` around lines 1317 - 1318, Interrupts with no toolCallId are being skipped silently in the interrupt handling path, which hides valid non-tool interrupts. Update the interrupt processing in processor.ts around the toolCallId check to confirm whether these interrupts should be handled here; if they can arrive, add an explicit warning/log in this branch instead of just continuing, similar to the ui-resource handler’s observability. Use the interrupt loop and toolCallId guard as the key symbols to locate the change.packages/ai-client/src/chat-client.ts (1)
586-619: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicated terminal-clearing condition risks future desync.
The 5-flag boolean (
isRunlessSessionError || isTrackedRunTerminal || isPendingInterruptRunTerminal || isCurrentRunTerminal || isCurrentStreamTerminal) is computed twice — once to nulllastResume, once to clearpendingInterrupts/pendingInterruptRunId/pendingInterruptResumeItems. These two pieces of state must always change together (per the guard insendMessage/appendthat checks both), so duplicating the condition creates a latent risk: a future edit to one copy without the other would silently desynchronize resume state and pending interrupts.♻️ Combine into a single condition
const isCurrentStreamTerminal = this.isLoading && chunk.type === 'RUN_FINISHED' && !runId - if ( - isRunlessSessionError || - isTrackedRunTerminal || - isPendingInterruptRunTerminal || - isCurrentRunTerminal || - isCurrentStreamTerminal - ) { - this.lastResume = null - } - if ( - isRunlessSessionError || - isTrackedRunTerminal || - isPendingInterruptRunTerminal || - isCurrentRunTerminal || - isCurrentStreamTerminal - ) { - this.pendingInterrupts = [] - this.pendingInterruptRunId = null - this.pendingInterruptResumeItems.clear() - } + const shouldClearResumeState = + isRunlessSessionError || + isTrackedRunTerminal || + isPendingInterruptRunTerminal || + isCurrentRunTerminal || + isCurrentStreamTerminal + if (shouldClearResumeState) { + this.lastResume = null + this.pendingInterrupts = [] + this.pendingInterruptRunId = null + this.pendingInterruptResumeItems.clear() + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-client/src/chat-client.ts` around lines 586 - 619, The terminal-state check in chat-client’s resume handling is duplicated, which can let `lastResume` and the pending interrupt state drift apart if one copy changes later. In the chunk-processing logic around `isRunlessSessionError`, `isTrackedRunTerminal`, `isPendingInterruptRunTerminal`, `isCurrentRunTerminal`, and `isCurrentStreamTerminal`, compute the combined boolean once and reuse it for both the `lastResume` reset and the `pendingInterrupts`/`pendingInterruptRunId`/`pendingInterruptResumeItems` cleanup so the state transitions stay in sync.packages/ai-react/src/use-chat.ts (1)
322-329: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedundant
?? undefined.client.resume(state ?? undefined)—stateis alreadyChatResumeState | undefined, so the coalescing is a no-op.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-react/src/use-chat.ts` around lines 322 - 329, The `resume` callback in `useChat` has a redundant `?? undefined` when calling `client.resume`, since `state` is already nullable/optional. Update the `resume` function to pass `state` directly to `client.resume` and keep the rest of the `useCallback` logic unchanged.packages/ai-persistence/src/types.ts (1)
194-216: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winTighten
featureRequirementsvalue type to avoid silent typos.Array<string>loses compile-time checking against actual store keys, so a typo in a required-store name would silently make a feature permanently "missing" instead of failing to compile.♻️ Suggested typing fix
-const featureRequirements: Record<PersistenceFeature, Array<string>> = { +const featureRequirements: Record< + PersistenceFeature, + Array<keyof AIPersistence['stores']> +> = { messages: ['messages'], 'durable-replay': ['runs', 'publicEvents'], interrupts: ['runs', 'publicEvents', 'interrupts'], 'internal-events': ['internalEvents'], metadata: ['metadata'], locks: ['locks'], artifacts: ['artifacts'], }Then
persistence.stores[store]at line 211 no longer needs theascast.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/types.ts` around lines 194 - 216, The `featureRequirements` map is too loosely typed, so typos in required store names can slip through and later force the cast in `validatePersistenceFeatures`. Tighten the value type in `featureRequirements` to use the actual `AIPersistence['stores']` keys (or a derived key union) instead of `Array<string>`, then update `validatePersistenceFeatures` so the `missingStores` check can index `persistence.stores` directly without the `as` cast. Refer to `featureRequirements` and `validatePersistenceFeatures` when making the typing change.packages/ai-persistence/src/middleware.ts (1)
152-183: 📐 Maintainability & Code Quality | 🔵 TrivialInterrupt "kind"/"reason" detection relies on hardcoded magic strings.
'approval','approval_required','client_tool','client_tool_input'are duplicated literal strings that presumably mirror constants defined in the core custom-events catalog (@tanstack/ai). If those canonical values drift, this mapping silently stops matching pending interrupts to the correct resume bucket. Consider importing/sharing the canonical constants instead of re-declaring string literals here.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/middleware.ts` around lines 152 - 183, The interrupt classification in resumeToolStateFromPending uses duplicated magic strings for approval/client-tool detection, which should be replaced with the shared canonical event values. Update resumeToolStateFromPending to reference the existing constants or exported symbols from the core custom-events catalog instead of hardcoded literals, and make sure interruptKind, reason, and the approval/client_tool matching logic all use those shared identifiers consistently.packages/ai-persistence/src/memory.ts (1)
220-223: 🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick wincreate()isn't idempotent against overwriting existing interrupts.createunconditionally overwrites any existing record forinterruptId. If it's ever called twice for the same id (e.g. a redelivered chunk afterresolve/cancelalready ran), an already-resolved interrupt would silently revert topending, losingresolvedAt/response.🛡️ Proposed fix to guard against overwrite
create(record: Omit<InterruptRecord, 'resolvedAt'>): Promise<void> { - this.interrupts.set(record.interruptId, { ...record }) + if (!this.interrupts.has(record.interruptId)) { + this.interrupts.set(record.interruptId, { ...record }) + } return Promise.resolve() }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/memory.ts` around lines 220 - 223, The MemoryInterruptStore create() method is overwriting existing interrupt entries for the same interruptId, which can reset resolved records back to pending. Update create() to be idempotent by checking this.interrupts before writing and skipping or preserving an existing record, keeping any resolvedAt/response state intact. Use the create(), resolve(), and cancel() methods in MemoryInterruptStore to ensure repeated calls for the same interruptId do not clobber already stored data.packages/ai-persistence-sql/src/index.ts (1)
27-29: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueLegacyEventLogtype not exported despite being part of the publicSqlPersistenceshape.SqlPersistence.events(seesql-persistence.ts) is typedLegacyEventLog, but this barrel only exportsSqlPersistence/SqlPersistenceOptions, notLegacyEventLogitself. Consumers can still use.eventsstructurally, but can't name/import the type directly.♻️ Proposed fix
export { createSqlPersistence } from './sql-persistence' export type { SqlPersistence, SqlPersistenceOptions } from './sql-persistence' +export type { LegacyEventLog } from './stores'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sql/src/index.ts` around lines 27 - 29, The barrel export in createSqlPersistence currently exposes SqlPersistence and SqlPersistenceOptions but omits LegacyEventLog, even though SqlPersistence.events uses that type in sql-persistence.ts. Update the index.ts re-export so LegacyEventLog is exported alongside the existing types, keeping the public API aligned with the SqlPersistence shape and allowing consumers to import the type directly.packages/ai-persistence-sql/src/stores.ts (1)
87-106: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAvoid
thisbinding inside the returned store object.createOrResumecallsthis.get(...), relying on the method being invoked with the store object as receiver. Every other store in this file uses closures instead ofthis. If this method is ever destructured or reassigned (const { createOrResume } = runStore), it will throw at runtime.♻️ Proposed fix — use a closure instead of `this`
export function createRunStore(driver: SqlDriver): RunStore { const p = (i: number) => param(driver.dialect, i) + async function get(runId: string) { + const rows = await driver.query( + `SELECT * FROM runs WHERE run_id = ${p(1)}`, + [runId], + ) + const row = rows[0] + return row ? mapRun(row) : null + } return { async createOrResume(input) { - const existing = await this.get(input.runId) + const existing = await get(input.runId) ... }, async update(runId, patch) { ... }, - async get(runId) { - const rows = await driver.query( - `SELECT * FROM runs WHERE run_id = ${p(1)}`, - [runId], - ) - const row = rows[0] - return row ? mapRun(row) : null - }, + get, } }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sql/src/stores.ts` around lines 87 - 106, The createOrResume method in createRunStore relies on this.get, which breaks if the method is destructured or called without the store object as receiver. Replace the this-based lookup with a closure over the returned store’s get implementation, following the pattern used by the other store helpers in stores.ts, so createOrResume always calls the correct getter regardless of invocation style.packages/ai-persistence-sql/tests/sql-persistence.test.ts (1)
1-11: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winFix import order flagged by ESLint.
import type { StreamChunk } from '@tanstack/ai'should come after the./sqlite-driverimport per theimport/orderrule.🔧 Proposed fix
import { describe, expect, it } from 'vitest' import { EventType } from '`@tanstack/ai`' import { AppendConflictError, createApprovalController, } from '`@tanstack/ai-persistence`' -import type { StreamChunk } from '`@tanstack/ai`' import { createSqlPersistence } from '../src/sql-persistence' import { migrate } from '../src/migrations' import { createTestSqliteDriver } from './sqlite-driver' +import type { StreamChunk } from '`@tanstack/ai`' import type { SqlDriver } from '../src/driver'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sql/tests/sql-persistence.test.ts` around lines 1 - 11, The import order in the sql-persistence test file is violating the ESLint import/order rule because the type-only StreamChunk import from `@tanstack/ai` is placed before the local ./sqlite-driver import. Reorder the imports in sql-persistence.test.ts so that the type import for StreamChunk comes after the relative test helper import, keeping the existing group ordering consistent with the rest of the file.Source: Linters/SAST tools
packages/ai-persistence-sql/src/migrations.ts (1)
52-61: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winConsider adding indexes for
interruptsthread/run lookups.interruptsis queried bythread_id(list/listPending/decisionsForThread) andrun_id(listByRun/listPendingByRun) instores.ts, but the table only has a PK oninterrupt_id. These will full-scan as the table grows.⚡ Proposed fix
`CREATE TABLE IF NOT EXISTS interrupts ( interrupt_id TEXT PRIMARY KEY, run_id TEXT NOT NULL, thread_id TEXT NOT NULL, status TEXT NOT NULL, requested_at ${ts} NOT NULL, resolved_at ${ts}, payload TEXT NOT NULL, response TEXT )`, + `CREATE INDEX IF NOT EXISTS interrupts_thread_id_idx ON interrupts (thread_id, requested_at)`, + `CREATE INDEX IF NOT EXISTS interrupts_run_id_idx ON interrupts (run_id, requested_at)`,🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sql/src/migrations.ts` around lines 52 - 61, The interrupts table creation in migrations.ts only defines a primary key on interrupt_id, but stores.ts queries it by thread_id and run_id in list/listPending/decisionsForThread and listByRun/listPendingByRun, so add indexes for those lookup columns in the CREATE TABLE migration (or equivalent follow-up migration) to avoid full table scans. Use the interrupts schema definition in migrations.ts as the place to add the new indexes so the common thread/run access paths are optimized.packages/ai-persistence-cloudflare/src/index.ts (1)
64-77: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUnnecessary type assertion.
createSqlPersistence(...)already returns a value structurally assignable toCloudflarePersistence(an intersection with an optionallocksfield), so theas CloudflarePersistencecast on Line 70 is redundant per ESLint.♻️ Proposed fix
const persistence = createSqlPersistence(createD1Driver(opts.d1), { mode: opts.mode, migrate: opts.migrate, - }) as CloudflarePersistence + })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 64 - 77, The `cloudflarePersistence` factory is using a redundant `as CloudflarePersistence` cast on the result of `createSqlPersistence`, which ESLint flags as unnecessary. Remove the type assertion and keep the returned value structurally typed, then preserve the existing `opts.durableObjects` logic that assigns `locks` via `createDurableObjectLockStore` and updates `persistence.stores.locks`/`persistence.locks`.Source: Linters/SAST tools
packages/ai-persistence-cloudflare/tests/d1.test.ts (1)
3-3: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMinor import-order nits.
ESLint flags
node:sqlite(Line 3) and../src/index(Line 6) ordering relative to neighboring imports.Also applies to: 6-6
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts` at line 3, The import ordering in the D1 test file is flagged by ESLint: the `DatabaseSync` import from `node:sqlite` and the `../src/index` import need to be reordered to match the surrounding import sort rules. Update the import block in the test module so these symbols are placed in the correct order relative to the neighboring imports, keeping the existing imports unchanged otherwise.Source: Linters/SAST tools
packages/ai-persistence-sqlite/tests/sqlite.test.ts (1)
3-3: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueMinor static-analysis nits: import order and unnecessary assertion.
ESLint flags the
@tanstack/aitype import ordering relative to../src/index(Line 3) and an unnecessary non-null assertion onp.events!at Line 61 (thecreateResumeSourcecall already narrows to a non-nullable type here).Also applies to: 61-61
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sqlite/tests/sqlite.test.ts` at line 3, The test file has two lint issues: the `StreamChunk` type import is out of order relative to the local `../src/index` import, and `p.events!` in the `createResumeSource` test uses an unnecessary non-null assertion. Update the import ordering in `sqlite.test.ts` to match the project’s import sort rules, and remove the `!` from `p.events` in the `createResumeSource` call since the type is already narrowed there.Source: Linters/SAST tools
packages/ai-persistence-drizzle/package.json (1)
40-44: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valuepeerDependenciesuseworkspace:^instead ofworkspace:*.As per coding guidelines, "Use the
workspace:*protocol for internal package dependencies in package.json," but the peerDependencies here useworkspace:^. This is a common, defensible pattern for peer ranges (compatibility statement vs. pinned local link), so verify this is the intended repo-wide convention for peerDependencies specifically before changing it.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-drizzle/package.json` around lines 40 - 44, The peerDependencies block in this package.json uses workspace:^, which may conflict with the repo’s internal dependency convention. Check the package manager’s intended pattern for peerDependencies and, if the repo expects the same internal protocol everywhere, update the `@tanstack/ai`, `@tanstack/ai-persistence`, and `@tanstack/ai-persistence-sql` peer ranges in peerDependencies accordingly while keeping the package manifest consistent with the rest of the workspace.Source: Coding guidelines
packages/ai-persistence-cloudflare/src/index.ts lines 84 to 99OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +79,21 @@79+/**80+ * Distributed lock over a Durable Object namespace. The companion DO class must81+ * implement a `fetch` that serializes by holding the single-threaded DO; this82+ * acquires by calling the DO keyed by the lock name. COMPILE-VERIFIED ONLY.83+ */84+export function createDurableObjectLockStore(85+ ns: DurableObjectNamespace,86+): LockStore {87+ return {88+ async withLock<T>(key: string, fn: () => Promise<T>): Promise<T> {89+ const stub = ns.get(ns.idFromName(key))90+ // Acquire: the DO returns 200 once it holds the turn for this key.91+ await stub.fetch('https://lock/acquire')92+ try {93+ return await fn()94+ } finally {95+ await stub.fetch('https://lock/release')96+ }97+ },98+ }99+}🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Lock acquisition result is never checked — a failed/rejected acquire silently proceeds as if the lock was held.
stub.fetch('https://lock/acquire')'s response is discarded (Line 91); if the DO returns a non-2xx status (contention, error, etc.) instead of granting the lock,withLockstill callsfn()and laterrelease, defeating the mutual-exclusion guarantee this store exists to provide. There's also no timeout on eitherfetchcall, so a hung DO could block the caller indefinitely (an external-call hazard on request threads).Since this is documented as "COMPILE-VERIFIED ONLY" (not runtime-tested here), this bug won't surface until actually deployed against a real Durable Object.
🔒 Proposed fix: validate the acquire response and add a timeout
async withLock<T>(key: string, fn: () => Promise<T>): Promise<T> { const stub = ns.get(ns.idFromName(key)) // Acquire: the DO returns 200 once it holds the turn for this key. - await stub.fetch('https://lock/acquire') + const acquireRes = await stub.fetch('https://lock/acquire', { + signal: AbortSignal.timeout(ACQUIRE_TIMEOUT_MS), + }) + if (!acquireRes.ok) { + throw new Error( + `Failed to acquire lock "${key}": ${acquireRes.status}`, + ) + } try { return await fn() } finally { await stub.fetch('https://lock/release') } },🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 84 - 99, The lock acquisition in createDurableObjectLockStore.withLock ignores the response from stub.fetch('https://lock/acquire'), so a failed acquire can still run fn() and break mutual exclusion. Update withLock to check the acquire response status from the Durable Object and only proceed when it indicates success, otherwise throw or retry appropriately; keep the release in finally. Also add timeout handling around both acquire and release fetch calls so a hung Durable Object cannot block callers indefinitely.✅ Addressed in commits 253a2eb to c864d51
@@ -0,0 +42,21 @@42+}43+44+function sqliteDriver(client: SqliteClient): SqlDriver {45+ const driver: SqlDriver = {46+ dialect: 'sqlite',47+ exec(sql, params = []) {48+ client.prepare(sql).run(...params)49+ return Promise.resolve()50+ },51+ query<T extends SqlRow = SqlRow>(52+ sql: string,53+ params: ReadonlyArray<unknown> = [],54+ ) {55+ return Promise.resolve(client.prepare(sql).all(...params) as Array<T>)56+ },57+ transaction(fn) {58+ return fn(driver)59+ },60+ }61+ return driver62+}🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Compare with the sibling raw sqlite adapter's transaction implementation for consistency fd -HI 'index.ts' packages/ai-persistence-sqlite/src | xargs cat -nRepository: TanStack/ai
Length of output: 2031
🌐 Web query:
better-sqlite3 transactions synchronous BEGIN COMMIT ROLLBACK💡 Result:
In better-sqlite3, transactions are managed primarily through the.transaction method, which is designed to be synchronous to match SQLite's serialized nature [1][2][3]. ### Using the.transaction Method The recommended way to manage transactions is to use the.transaction higher-order function. This method creates a new function that automatically wraps your code in a transaction [1][2]. - BEGIN: When the created function is invoked, the library automatically issues a BEGIN statement [1]. - COMMIT: If the function returns normally, the transaction is automatically committed [1][2]. - ROLLBACK: If the function throws an exception, the transaction is automatically rolled back [1][2]. You can also use different transaction modes by calling.deferred,.immediate, or.exclusive on the transaction function [1]. ### Synchronous Nature and Async Caveats Better-sqlite3 is strictly synchronous. It does not support asynchronous functions within the.transaction wrapper [1][2]. - Why: SQLite serializes all statements and transactions. Keeping a transaction open across event loop ticks in Node.js can block other operations and leads to unpredictable behavior [4][5][6]. - Restriction: Passing an async function to.transaction will cause the library to throw a TypeError (e.g., "Transaction function cannot return a promise") to prevent dangerous, silent failures [6]. ### Manual Transaction Management While the.transaction method is preferred, you can manage transactions manually using prepared statements for BEGIN, COMMIT, and ROLLBACK [1]. - Note: If you choose to manage transactions manually, you should not mix them with the.transaction method [1]. - Automatic Rollbacks: Be aware that SQLite may automatically roll back transactions in certain scenarios (such as specific errors or trigger conditions) [1][2]. If an automatic rollback occurs, attempting to manually execute a ROLLBACK statement may cause an error [7]. When catching SQLite errors inside a transaction, it is generally recommended to re-throw the error to exit the transaction function cleanly [1][7].
Citations:
- 1: https://github.com/WiseLibs/better-sqlite3/blob/HEAD/docs/api.md
- 2: https://github.com/WiseLibs/better-sqlite3/blob/b615a52a9a00ec5c5504875b429136ad8302d3b1/docs/api.md
- 3: https://github.com/JoshuaWise/better-sqlite3/
- 4: https://github.com/WiseLibs/better-sqlite3/issues/319
- 5: https://github.com/WiseLibs/better-sqlite3/issues/1262
- 6: https://github.com/WiseLibs/better-sqlite3/pull/1364
- 7: https://github.com/JoshuaWise/better-sqlite3/issues/292
🌐 Web query:
node:sqlite module transaction support💡 Result:
The built-in Node.js node:sqlite module supports SQLite transactions, but it does not provide a high-level wrapper function (such as a db.transaction method) to manage them automatically [1]. To perform transactions, you must explicitly execute the standard SQL commands BEGIN, COMMIT, and ROLLBACK using the database.exec method [1]. Key details regarding transaction support in node:sqlite: 1. Manual Management: Because there is no built-in transaction wrapper, you must implement your own logic to handle transaction boundaries, typically using a try-catch block to ensure that COMMIT is called on success and ROLLBACK is called if an error occurs [1]. 2. Transaction Status: The DatabaseSync class provides a database.isTransaction property (a boolean), which functions as a wrapper around the SQLite sqlite3_get_autocommit function to check whether the database is currently within a transaction [2][3]. 3. Implementation Pattern: A common pattern for handling transactions in this module is to create a helper function that accepts a callback: function withTransaction(db, fn) { db.exec('BEGIN'); try { const result = fn; db.exec('COMMIT'); return result; } catch (e) { db.exec('ROLLBACK'); throw e; } } This manual approach is required as of July 2026 [1].
Citations:
- 1: https://jangwook.net/en/blog/en/node-sqlite-builtin-practical-guide-2026/
- 2: https://nodejs.org/dist/latest/docs/api/sqlite.html
- 3: https://nodejs.org/api/sqlite.md
🌐 Web query:
TanStack AI persistence drizzle sqlite driver transaction💡 Result:
Based on available documentation, it appears there is a distinction between TanStack DB and TanStack AI, as they are separate products within the TanStack ecosystem. TanStack AI is a provider-agnostic SDK for building AI features like streaming chat and tool calling [1][2]. It is not currently documented as having a built-in SQLite persistence layer that requires Drizzle ORM drivers or transactions. However, TanStack DB provides SQLite-backed persistence [3][4]. The core persistence logic for TanStack DB is defined in
@tanstack/db-sqlite-persistence-core[5]. This package defines a SQLiteDriver interface [6], which includes a transaction method: interface SQLiteDriver { //... other methods transaction: ( fn: (transactionDriver: SQLiteDriver) => Promise,) => Promise } This interface is intended to be implemented by platform-specific runtime wrappers (such as those for Node, Browser, or Cloudflare Durable Objects) [5]. While Drizzle ORM also provides a robust transaction API for SQLite [7], the TanStack DB persistence layer uses its own abstraction (the SQLiteDriver interface) to manage transactions across different runtime environments [6][5]. If you are looking to integrate Drizzle ORM with TanStack products, you would typically use Drizzle as your general-purpose ORM for database operations [7], while TanStack DB uses its own dedicated persistence adapters for its collection-based caching and sync logic [8][4]. They operate as separate layers in an application architecture.Citations:
- 1: https://tanstack.com/ai/latest
- 2: https://tanstack.com/ai/latest/docs/getting-started/overview
- 3: https://tanstack.com/blog/tanstack-db-0.6-app-ready-with-persistence-and-includes
- 4: https://skills.rest/skill/db-core-persistence
- 5: https://npmx.dev/package/@tanstack/db-sqlite-persistence-core
- 6: https://github.com/TanStack/db/blob/main/packages/db-sqlite-persistence-core/src/persisted.ts
- 7: https://drizzle-team-drizzle-orm.mintlify.app/api/core/transactions
- 8: https://github.com/TanStack/db/blob/main/packages/db/skills/db-core/persistence/SKILL.md
sqliteDriver.transaction()has no transaction control and can corrupt data on failures.The current implementation
transaction(fn) { return fn(driver) }never issuesBEGIN/COMMIT/ROLLBACKstatements. UnlikepgDriver(constrained by connection pooling), a SQLite client is single-threaded and synchronous (better-sqlite3ornode:sqlite), making real transactional semantics feasible via manualclient.prepare('BEGIN').run()/COMMIT/ROLLBACK.Without this, a failure partway through a multi-statement operation leaves partial state persisted. Since the
transactionmethod is part of theSqlDriverinterface and expected to returnPromise<T>, it must be async. Bothbetter-sqlite3(via manual prepare/run) andnode:sqlite(via manual exec) support this pattern.🔧 Proposed fix for real sqlite transactions
function sqliteDriver(client: SqliteClient): SqlDriver { const driver: SqlDriver = { dialect: 'sqlite', exec(sql, params = []) { client.prepare(sql).run(...params) return Promise.resolve() }, query<T extends SqlRow = SqlRow>( sql: string, params: ReadonlyArray<unknown> = [], ) { return Promise.resolve(client.prepare(sql).all(...params) as Array<T>) }, - transaction(fn) { - return fn(driver) - }, + async transaction(fn) { + client.prepare('BEGIN').run() + try { + const result = await fn(driver) + client.prepare('COMMIT').run() + return result + } catch (err) { + client.prepare('ROLLBACK').run() + throw err + } + }, } return driver }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change44-function sqliteDriver(client: SqliteClient): SqlDriver {45- const driver: SqlDriver = {46- dialect: 'sqlite',47- exec(sql, params = []) {48- client.prepare(sql).run(...params)49- return Promise.resolve()50- },51- query<T extends SqlRow = SqlRow>(52- sql: string,53- params: ReadonlyArray<unknown> = [],54- ) {55- return Promise.resolve(client.prepare(sql).all(...params) as Array<T>)56- },57- transaction(fn) {58- return fn(driver)59- },60- }61- return driver62-}44+function sqliteDriver(client: SqliteClient): SqlDriver {45+ const driver: SqlDriver = {46+ dialect: 'sqlite',47+ exec(sql, params = []) {48+ client.prepare(sql).run(...params)49+ return Promise.resolve()50+ },51+ query<T extends SqlRow = SqlRow>(52+ sql: string,53+ params: ReadonlyArray<unknown> = [],54+ ) {55+ return Promise.resolve(client.prepare(sql).all(...params) as Array<T>)56+ },57+ async transaction(fn) {58+ client.prepare('BEGIN').run()59+ try {60+ const result = await fn(driver)61+ client.prepare('COMMIT').run()62+ return result63+ } catch (err) {64+ client.prepare('ROLLBACK').run()65+ throw err66+ }67+ },68+ }69+ return driver70+}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-drizzle/src/index.ts` around lines 44 - 62, The sqliteDriver.transaction() implementation currently just calls fn(driver) without any transaction boundaries, so update sqliteDriver in index.ts to run the callback inside a real SQLite transaction. Make transaction async, issue BEGIN before invoking fn(driver), COMMIT on success, and ROLLBACK in a catch/finally path if the callback throws. Use the existing sqliteDriver/client helpers and keep the SqlDriver.transaction contract returning a Promise.@@ -0,0 +45,21 @@45+ runs: RunStore46+ /** @deprecated Use stores.publicEvents. */47+ events: LegacyEventLog48+ /** @deprecated Use stores.interrupts. */49+ approvals: ApprovalStore50+ /** @deprecated Use feature-based configuration instead of mode. */51+ mode: PersistenceMode52+}53+54+export function createSqlPersistence(55+ driver: SqlDriver,56+ opts?: SqlPersistenceOptions,57+): SqlPersistence {58+ const shouldMigrate = opts?.migrate ?? true59+60+ let migrated: Promise<void> | undefined61+ const ensureSchema = (): Promise<void> => {62+ if (!shouldMigrate) return Promise.resolve()63+ migrated ??= runMigrations(driver) // runs on the RAW driver — no recursion64+ return migrated65+ }🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Rejected migration promise is cached forever — no retry possible.
migrated ??= runMigrations(driver)only skips re-running migration whenmigratedis nullish. IfrunMigrationsrejects,migratedbecomes a rejectedPromise, which is not nullish — so it's never retried. A single transient failure (e.g., DB briefly unreachable at startup) permanently breaks this persistence instance for its entire lifetime, since every subsequentexec/query/transactioncall awaits and re-throws the same cached rejection.🔧 Proposed fix — reset cache on failure so future calls retry
let migrated: Promise<void> | undefined const ensureSchema = (): Promise<void> => { if (!shouldMigrate) return Promise.resolve() - migrated ??= runMigrations(driver) // runs on the RAW driver — no recursion - return migrated + if (!migrated) { + migrated = runMigrations(driver).catch((err) => { + migrated = undefined // allow retry on next call + throw err + }) + } + return migrated }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change60- let migrated: Promise<void> | undefined61- const ensureSchema = (): Promise<void> => {62- if (!shouldMigrate) return Promise.resolve()63- migrated ??= runMigrations(driver) // runs on the RAW driver — no recursion64- return migrated65- }60+ let migrated: Promise<void> | undefined61+ const ensureSchema = (): Promise<void> => {62+ if (!shouldMigrate) return Promise.resolve()63+ if (!migrated) {64+ migrated = runMigrations(driver).catch((err) => {65+ migrated = undefined // allow retry on next call66+ throw err67+ })68+ }69+ return migrated70+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-sql/src/sql-persistence.ts` around lines 60 - 65, The ensureSchema migration cache in sql-persistence.ts is retaining a rejected promise forever, so a transient failure in runMigrations(driver) permanently blocks later exec/query/transaction calls. Update the migrated/ensureSchema flow in sql-persistence.ts so that a failed migration clears the cached promise before rethrowing, allowing the next call to retry instead of reusing the rejection; keep the same raw-driver migration path and preserve the existing ensureSchema and runMigrations(driver) structure.@@ -0,0 +33,21 @@33+ "files": [34+ "dist",35+ "src"36+ ],37+ "scripts": {38+ "build": "vite build",39+ "clean": "premove ./build ./dist",40+ "lint:fix": "eslint ./src --fix",41+ "test:build": "publint --strict",42+ "test:eslint": "eslint ./src",43+ "test:lib": "vitest",44+ "test:lib:dev": "pnpm test:lib --watch",45+ "test:types": "tsc"46+ },47+ "peerDependencies": {48+ "@tanstack/ai": "workspace:^"49+ },50+ "devDependencies": {51+ "@tanstack/ai": "workspace:*",52+ "@vitest/coverage-v8": "4.0.14"53+ }📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
peerDependenciesusesworkspace:^instead ofworkspace:*.devDependencies correctly uses
workspace:*for@tanstack/ai, but peerDependencies usesworkspace:^, which is inconsistent with the required protocol for internal package dependencies.🔧 Suggested fix
"peerDependencies": { - "`@tanstack/ai`": "workspace:^" + "`@tanstack/ai`": "workspace:*" },As per coding guidelines, "Use the
workspace:*protocol for internal package dependencies in package.json."📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change49+ "peerDependencies": {50+ "`@tanstack/ai`": "workspace:*"51+ },52+ "devDependencies": {53+ "`@tanstack/ai`": "workspace:*",54+ "`@vitest/coverage-v8`": "4.0.14"55+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/package.json` around lines 47 - 53, The package manifest for ai-persistence has an inconsistent workspace protocol for the `@tanstack/ai` peer dependency. Update the peerDependencies entry in package.json to use workspace:* so it matches the internal dependency convention already used in devDependencies, and keep the `@tanstack/ai` reference aligned across the manifest.Source: Coding guidelines
@@ -0,0 +242,21 @@242+ )243+ }244+ if (decoded.seq > latestSeq) {245+ throw new Error(246+ `Resume cursor sequence ${decoded.seq} is beyond latest persisted sequence ${latestSeq} for run ${decoded.runId}.`,247+ )248+ }249+ if (250+ !(await publicEventExistsAtSeq(251+ persistence.stores.publicEvents,252+ decoded.runId,253+ decoded.seq,254+ ))255+ ) {256+ throw new Error(257+ `Resume cursor sequence ${decoded.seq} does not reference a persisted public event for run ${decoded.runId}.`,258+ )259+ }260+261+ return true262+}🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Consolidate duplicated cursor-validation logic with
resume-source.ts.publicEventExistsAtSeq(Lines 191-202) and the bulk ofvalidateReplayCursor's checks (decode,seq < 1, runId match,latestSeqbound, event-existence) are re-implemented almost verbatim inpackages/ai-persistence/src/resume-source.ts(Lines 11-64). Since this logic gates whether a client can bypass a run/thread boundary via a forged or stale cursor, having two independently-maintained copies risks silent divergence (already the two throw slightly differently-scoped errors: this file additionally checksrun.threadId !== ctx.threadId, which the resume-source copy doesn't do since it lacks thread context).Extract the shared seq/runId/existence checks into a single helper (e.g. in
cursor.ts) that both call sites use, layering the thread-ownership check on top inmiddleware.ts.♻️ Sketch of a shared helper
// cursor.ts export async function assertCursorResolvable( events: { read: PublicEventStore['read']; latestSeq: PublicEventStore['latestSeq'] }, runId: string, cursor: string, ): Promise<number> { const decoded = decodeCursor(cursor) if (decoded.seq < 1) { throw new Error(`Resume cursor sequence ${decoded.seq} is invalid; expected a persisted event sequence >= 1.`) } if (decoded.runId !== runId) { throw new Error(`Resume cursor runId ${decoded.runId} does not match request runId ${runId}.`) } const latestSeq = await events.latestSeq(runId) if (latestSeq === 0) { throw new Error(`Resume cursor references run ${runId}, but no public events are persisted.`) } if (decoded.seq > latestSeq) { throw new Error(`Resume cursor sequence ${decoded.seq} is beyond latest persisted sequence ${latestSeq} for run ${runId}.`) } for await (const persisted of events.read(runId, { afterSeq: decoded.seq - 1 })) { if (persisted.seq === decoded.seq) return decoded.seq break } throw new Error(`Resume cursor sequence ${decoded.seq} does not reference a persisted public event for run ${runId}.`) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/middleware.ts` around lines 191 - 262, The replay-cursor validation in validateReplayCursor and publicEventExistsAtSeq is duplicated with the equivalent checks in resume-source.ts, so consolidate the shared decode/seq/runId/latestSeq/existence logic into one reusable helper (for example in cursor.ts) and have both call sites use it. Keep the thread ownership check in middleware.ts on top of the shared helper, and reference validateReplayCursor, publicEventExistsAtSeq, and the resume-source cursor checks when wiring the refactor.@@ -0,0 +34,21 @@34+ "clean": "premove ./build ./dist",35+ "lint:fix": "eslint ./src --fix",36+ "test:build": "publint --strict",37+ "test:eslint": "eslint ./src",38+ "test:lib": "vitest",39+ "test:lib:dev": "pnpm test:lib --watch",40+ "test:types": "tsc"41+ },42+ "peerDependencies": {43+ "@tanstack/ai": "workspace:^",44+ "@tanstack/ai-persistence": "workspace:^",45+ "@tanstack/ai-persistence-sql": "workspace:^",46+ "@tanstack/ai-sandbox": "workspace:^"47+ },48+ "devDependencies": {49+ "@tanstack/ai": "workspace:*",50+ "@tanstack/ai-persistence": "workspace:*",51+ "@tanstack/ai-persistence-sql": "workspace:*",52+ "@tanstack/ai-sandbox": "workspace:*",53+ "@vitest/coverage-v8": "4.0.14"54+ }📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
peerDependenciesuseworkspace:^instead ofworkspace:*.devDependenciescorrectly useworkspace:*, butpeerDependenciesuseworkspace:^. As per coding guidelines,**/package.jsonshould "Use theworkspace:*protocol for internal package dependencies in package.json."🔧 Proposed fix
"peerDependencies": { - "`@tanstack/ai`": "workspace:^", - "`@tanstack/ai-persistence`": "workspace:^", - "`@tanstack/ai-persistence-sql`": "workspace:^", - "`@tanstack/ai-sandbox`": "workspace:^" + "`@tanstack/ai`": "workspace:*", + "`@tanstack/ai-persistence`": "workspace:*", + "`@tanstack/ai-persistence-sql`": "workspace:*", + "`@tanstack/ai-sandbox`": "workspace:*" },📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change42- "peerDependencies": {43- "@tanstack/ai": "workspace:^",44- "@tanstack/ai-persistence": "workspace:^",45- "@tanstack/ai-persistence-sql": "workspace:^",46- "@tanstack/ai-sandbox": "workspace:^"47- },48- "devDependencies": {49- "@tanstack/ai": "workspace:*",50- "@tanstack/ai-persistence": "workspace:*",51- "@tanstack/ai-persistence-sql": "workspace:*",52- "@tanstack/ai-sandbox": "workspace:*",53- "@vitest/coverage-v8": "4.0.14"54- }42+ "peerDependencies": {43+ "`@tanstack/ai`": "workspace:*",44+ "`@tanstack/ai-persistence`": "workspace:*",45+ "`@tanstack/ai-persistence-sql`": "workspace:*",46+ "`@tanstack/ai-sandbox`": "workspace:*"47+ },48+ "devDependencies": {49+ "`@tanstack/ai`": "workspace:*",50+ "`@tanstack/ai-persistence`": "workspace:*",51+ "`@tanstack/ai-persistence-sql`": "workspace:*",52+ "`@tanstack/ai-sandbox`": "workspace:*",53+ "`@vitest/coverage-v8`": "4.0.14"54+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-sandbox-persistence/package.json` around lines 42 - 54, The peerDependencies in the package.json for ai-sandbox-persistence are using workspace:^ instead of the required workspace:* protocol for internal packages. Update the peerDependencies entries for `@tanstack/ai`, `@tanstack/ai-persistence`, `@tanstack/ai-persistence-sql`, and `@tanstack/ai-sandbox` to match the same workspace:* convention already used in devDependencies.Source: Coding guidelines
@@ -0,0 +25,21 @@25+import type { ChatMiddleware } from '@tanstack/ai'26+import type { AIPersistence } from '@tanstack/ai-persistence'27+28+/** Durable, SQL-backed {@link SandboxStore}. Creates its table lazily on first use. */29+export function createSqlSandboxStore(driver: SqlDriver): SandboxStore {30+ const p = (i: number) => param(driver.dialect, i)31+ let ready: Promise<void> | undefined32+ const ensure = (): Promise<void> => {33+ ready ??= driver.exec(34+ `CREATE TABLE IF NOT EXISTS sandbox_instances (35+ key TEXT PRIMARY KEY,36+ provider TEXT NOT NULL,37+ provider_sandbox_id TEXT NOT NULL,38+ latest_snapshot_id TEXT,39+ thread_id TEXT NOT NULL,40+ latest_run_id TEXT,41+ updated_at ${driver.dialect === 'postgres' ? 'BIGINT' : 'INTEGER'} NOT NULL42+ )`,43+ )44+ return ready45+ }🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Lazy schema init permanently fails after a transient error.
ready ??= driver.exec(...)caches the promise once, but if the initialCREATE TABLErejects (e.g. transient connectivity),readystays a rejected promise forever — every subsequentget/upsert/deletecall fails permanently until process restart, since??=never re-assigns.🔧 Proposed fix to allow retry on failure
let ready: Promise<void> | undefined const ensure = (): Promise<void> => { - ready ??= driver.exec( - `CREATE TABLE IF NOT EXISTS sandbox_instances ( - key TEXT PRIMARY KEY, - provider TEXT NOT NULL, - provider_sandbox_id TEXT NOT NULL, - latest_snapshot_id TEXT, - thread_id TEXT NOT NULL, - latest_run_id TEXT, - updated_at ${driver.dialect === 'postgres' ? 'BIGINT' : 'INTEGER'} NOT NULL - )`, - ) + if (!ready) { + ready = driver + .exec( + `CREATE TABLE IF NOT EXISTS sandbox_instances ( + key TEXT PRIMARY KEY, + provider TEXT NOT NULL, + provider_sandbox_id TEXT NOT NULL, + latest_snapshot_id TEXT, + thread_id TEXT NOT NULL, + latest_run_id TEXT, + updated_at ${driver.dialect === 'postgres' ? 'BIGINT' : 'INTEGER'} NOT NULL + )`, + ) + .catch((err) => { + ready = undefined + throw err + }) + } return ready }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change31- let ready: Promise<void> | undefined32- const ensure = (): Promise<void> => {33- ready ??= driver.exec(34- `CREATE TABLE IF NOT EXISTS sandbox_instances (35- key TEXT PRIMARY KEY,36- provider TEXT NOT NULL,37- provider_sandbox_id TEXT NOT NULL,38- latest_snapshot_id TEXT,39- thread_id TEXT NOT NULL,40- latest_run_id TEXT,41- updated_at ${driver.dialect === 'postgres' ? 'BIGINT' : 'INTEGER'} NOT NULL42- )`,43- )44- return ready45- }31+ let ready: Promise<void> | undefined32+ const ensure = (): Promise<void> => {33+ if (!ready) {34+ ready = driver35+ .exec(36+ `CREATE TABLE IF NOT EXISTS sandbox_instances (37+ key TEXT PRIMARY KEY,38+ provider TEXT NOT NULL,39+ provider_sandbox_id TEXT NOT NULL,40+ latest_snapshot_id TEXT,41+ thread_id TEXT NOT NULL,42+ latest_run_id TEXT,43+ updated_at ${driver.dialect === 'postgres' ? 'BIGINT' : 'INTEGER'} NOT NULL44+ )`,45+ )46+ .catch((err) => {47+ ready = undefined48+ throw err49+ })50+ }51+ return ready52+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-sandbox-persistence/src/index.ts` around lines 31 - 45, The lazy schema initialization in ensure() caches a rejected promise in ready, so a transient failure makes every later call fail permanently. Update ensure() so it only stores the in-flight CREATE TABLE promise on success, and clears or avoids retaining ready when driver.exec rejects, allowing get/upsert/delete to retry initialization on the next call. Use the ready variable and the ensure() helper in packages/ai-sandbox-persistence/src/index.ts as the place to make this retry-safe.@@ -1394,14 +1468,8 @@139414681395- for (const chunk of this.buildApprovalChunks(1396- executionResult.needsApproval,1397- finishEvent,1398- )) {1399- yield* this.pipeThroughMiddleware(chunk)1400- }1401-1402- for (const chunk of this.buildClientToolChunks(1403- executionResult.needsClientExecution,1404- finishEvent,1405- )) {1406- yield* this.pipeThroughMiddleware(chunk)1407- }1469+ yield* this.pipeThroughMiddleware(1470+ this.buildInterruptFinishedChunk(1471+ finishEvent,1472+ executionResult.needsApproval,1473+ executionResult.needsClientExecution,1474+ ),1475+ )🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Use the active run ID for pending-tool interrupt terminals.
In
checkForPendingToolCalls(), this interrupt terminal is built fromcreateSyntheticFinishedEvent(), which currently assignsrunId: this.createId('pending'). Now that this is emitted as a publicRUN_FINISHED, it can produce a terminal event for a run that never had a matchingRUN_STARTEDand does not matchmiddlewareCtx.runId.Proposed fix
private createSyntheticFinishedEvent(): RunFinishedEvent { return { type: 'RUN_FINISHED', - runId: this.createId('pending'), + runId: this.middlewareCtx.runId, threadId: this.threadId, model: this.params.model, timestamp: Date.now(), finishReason: 'tool_calls', } as RunFinishedEvent }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/activities/chat/index.ts` around lines 1469 - 1475, The pending-tool interrupt terminal created in checkForPendingToolCalls via buildInterruptFinishedChunk/createSyntheticFinishedEvent is using a synthetic runId instead of the active one, which can emit a RUN_FINISHED without a matching RUN_STARTED. Update the synthetic finished event path to carry middlewareCtx.runId (or the current active run ID from the surrounding chat flow) so the terminal event matches the existing run; use the buildInterruptFinishedChunk and createSyntheticFinishedEvent symbols to locate the change.@@ -0,0 +35,21 @@35+ * In-memory {@link LockStore} — a per-key promise chain. Correct within a36+ * single process; multi-instance correctness needs a distributed lock from the37+ * persistence layer.38+ */39+export class InMemoryLockStore implements LockStore {40+ private readonly chains = new Map<string, Promise<unknown>>()41+42+ withLock<T>(key: string, fn: () => Promise<T>): Promise<T> {43+ const prior = this.chains.get(key) ?? Promise.resolve()44+ // Chain after the prior holder regardless of how it settled.45+ const run = prior.then(fn, fn)46+ // Keep the chain alive but swallow rejections so one failure doesn't poison the lock.47+ this.chains.set(48+ key,49+ run.then(50+ () => undefined,51+ () => undefined,52+ ),53+ )54+ return run55+ }🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Clean up idle lock chains.
chainsretains every key ever locked, so thread/run-derived keys can grow unbounded in long-lived servers even after the critical section completes.Proposed fix
withLock<T>(key: string, fn: () => Promise<T>): Promise<T> { const prior = this.chains.get(key) ?? Promise.resolve() // Chain after the prior holder regardless of how it settled. const run = prior.then(fn, fn) // Keep the chain alive but swallow rejections so one failure doesn't poison the lock. - this.chains.set( - key, - run.then( - () => undefined, - () => undefined, - ), + const tail = run.then( + () => undefined, + () => undefined, ) + this.chains.set(key, tail) + void tail.finally(() => { + if (this.chains.get(key) === tail) { + this.chains.delete(key) + } + }) return run }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change48+export class InMemoryLockStore implements LockStore {49+ private readonly chains = new Map<string, Promise<unknown>>()50+51+ withLock<T>(key: string, fn: () => Promise<T>): Promise<T> {52+ const prior = this.chains.get(key) ?? Promise.resolve()53+ // Chain after the prior holder regardless of how it settled.54+ const run = prior.then(fn, fn)55+ // Keep the chain alive but swallow rejections so one failure doesn't poison the lock.56+ const tail = run.then(57+ () => undefined,58+ () => undefined,59+ )60+ this.chains.set(key, tail)61+ void tail.finally(() => {62+ if (this.chains.get(key) === tail) {63+ this.chains.delete(key)64+ }65+ })66+ return run67+ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai/src/locks.ts` around lines 39 - 55, The InMemoryLockStore chains map never removes completed keys, so lock entries can accumulate indefinitely in long-lived processes. Update withLock to clean up the stored chain for a key once the returned operation settles, while still preserving the existing sequencing behavior; use the InMemoryLockStore.withLock flow and its chains map to ensure the entry is deleted only when it still points to the completed chain.@@ -310,3 +405,13 @@310405 },311406 })312407408+ useEffect(() => {409+ if (!serverPersistenceEnabled || attemptedStoredResumeRef.current) return410+ attemptedStoredResumeRef.current = true411+ if (!initialResumeSnapshot) return412+ if ((initialResumeSnapshot.pendingInterrupts ?? []).length > 0) return413+ try {414+ void resume(initialResumeSnapshot.resumeState)415+ } catch {416+ window.localStorage.removeItem(resumeStorageKey)417+ }🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Catch the rejected resume promise.
Line 414 discards the promise, so the
catchblock on Line 415 will not run ifresume()rejects. A bad stored cursor can then produce an unhandled rejection and keep the stale snapshot in localStorage.Proposed fix
- try { - void resume(initialResumeSnapshot.resumeState) - } catch { + void resume(initialResumeSnapshot.resumeState).catch(() => { window.localStorage.removeItem(resumeStorageKey) - } + })📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change413- try {414- void resume(initialResumeSnapshot.resumeState)415- } catch {416- window.localStorage.removeItem(resumeStorageKey)417- }413+ void resume(initialResumeSnapshot.resumeState).catch(() => {414+ window.localStorage.removeItem(resumeStorageKey)415+ })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@testing/e2e/src/routes/`$provider/$feature.tsx around lines 413 - 417, The resume flow in the route component does not handle a rejected promise because `void resume(initialResumeSnapshot.resumeState)` detaches the async error from the surrounding `try/catch`. Update the `resume` call so its rejection is explicitly caught within the same flow, and keep the `window.localStorage.removeItem(resumeStorageKey)` cleanup in that rejection path. Use the `resume` invocation and `resumeStorageKey` cleanup block to locate the fix.Actionable comments posted: 4
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
examples/ts-react-chat/src/routes/generations.image.tsx (1)
1-16: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winFix import order (ESLint
import/order).The relative import of
../lib/server-fnsshould sort before the type-only imports per the project's lint rule.🔧 Proposed fix
import { useState } from 'react' import { createFileRoute } from '`@tanstack/react-router`' import { useGenerateImage } from '`@tanstack/ai-react`' -import type { UseGenerateImageReturn } from '`@tanstack/ai-react`' import { fetchServerSentEvents } from '`@tanstack/ai-client`' -import type { ImageGenerateInput } from '`@tanstack/ai-client`' import { generateImageFn, generateImageStreamFn } from '../lib/server-fns' +import type { UseGenerateImageReturn } from '`@tanstack/ai-react`' +import type { ImageGenerateInput } from '`@tanstack/ai-client`'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/routes/generations.image.tsx` around lines 1 - 16, The import block in generations.image.tsx violates the import/order rule because the relative import from ../lib/server-fns is placed after type-only imports; reorder the imports so the relative module import is grouped and sorted before the type-only package imports, keeping useState, createFileRoute, useGenerateImage, fetchServerSentEvents, and ImageGenerateInput organized according to the project’s lint ordering.Source: Linters/SAST tools
🧹 Nitpick comments (3)
examples/ts-react-chat/src/routes/generations.image.tsx (1)
9-15: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract the shared
textPrompthelper intoexamples/ts-react-chat/src/lib— the same text-only prompt normalization is duplicated inexamples/ts-react-chat/src/routes/generations.image.tsxandexamples/ts-react-chat/src/routes/generations.video.tsx, so a shared helper would keep the routes aligned.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/routes/generations.image.tsx` around lines 9 - 15, The text-only prompt normalization logic in textPrompt is duplicated across the image and video generation routes, so extract it into a shared helper under examples/ts-react-chat/src/lib and reuse it from both routes. Move the prompt-to-string conversion into a single utility with the same behavior for string and structured prompt inputs, then update generations.image.tsx and generations.video.tsx to import and call that shared helper instead of keeping separate local copies.examples/ts-react-chat/src/routes/generations.video.tsx (1)
9-15: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate
textPrompthelper across generation routes.The identical
textPromptimplementation also exists ingenerations.image.tsx. Consider extracting it to a shared module (e.g.../lib/prompt.ts) to avoid divergence as more generation routes are added.♻️ Suggested extraction
-function textPrompt(prompt: VideoGenerateInput['prompt']): string { - if (typeof prompt === 'string') return prompt - return prompt - .filter((part) => part.type === 'text') - .map((part) => part.content) - .join('\n') -} +import { textPrompt } from '../lib/prompt'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/routes/generations.video.tsx` around lines 9 - 15, The textPrompt helper is duplicated in generations.video.tsx and generations.image.tsx, so extract the shared logic into a common prompt utility module (for example a prompt helper under lib) and update both generation routes to import and use it. Keep the existing textPrompt behavior the same, but move the implementation to a single shared function so future generation routes reuse the same source of truth.examples/ts-react-chat/src/routes/api.mysql-persistent-chat.ts (1)
52-94: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winFragile implicit-lock pattern for single-producer-per-run guarantee.
ensureProducerrelies on there being noawaitbetween re-readingactiveRuns.get(runId)after theexisting.catch()await and the subsequentactiveRuns.set(runId, promise)call — that's what currently prevents two concurrentresumerequests for the samerunId(e.g. two browser tabs sharing the same localStorage-backed thread) from spinning up duplicate producers. It works today only because JS run-to-completion guarantees no interleaving in that span, but it's easy to accidentally break (e.g. by adding a loggingawaitor refactoring) since there's no comment documenting the invariant. Also,activeRuns.delete(runId)right before the unconditionalactiveRuns.set(runId, promise)a few lines later is a no-op and can be removed.Consider adding a comment documenting why no lock is needed here, or replacing the ad-hoc check with an explicit compare-and-swap/mutex for clarity and future-proofing.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/routes/api.mysql-persistent-chat.ts` around lines 52 - 94, The single-producer-per-run logic in ensureProducer is relying on a subtle run-to-completion window between the second activeRuns.get(runId) check and activeRuns.set(runId, promise), which should be made explicit or replaced with a safer compare-and-swap/mutex pattern. Add a short comment near ensureProducer explaining the invariant that no await may occur before activeRuns.set, so future refactors don’t accidentally create duplicate producers for the same runId. Also remove the redundant activeRuns.delete(runId) branch immediately before the unconditional set, since it has no effect. Keep the existing activeRuns and resume handling behavior unchanged.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@examples/ts-react-chat/.env.example`: - Around line 5-13: The MYSQL_* example block is out of order and is triggering dotenv-linter in the ts-react-chat .env.example file. Reorder the variables in the MySQL persistence demo section so the MYSQL_* keys are sorted consistently, keeping the example lint-clean while preserving the existing values and comments. In `@examples/ts-react-chat/src/lib/mysql-persistence.ts`: - Around line 16-21: The MysqlExecutable interface is using shorthand method syntax that violates `@typescript-eslint/method-signature-style`. Update the execute member in MysqlExecutable to use function-property syntax instead of a method signature, keeping the same generic type parameter, arguments, and Promise return type so the shape stays unchanged while satisfying the lint rule. - Around line 105-122: The nested MySQL transaction handling in createDriver’s transaction method still opens a fresh pool connection via getConnection(), which breaks reuse of the active transaction context. Update transaction(fn) so nested tx.transaction(...) calls stay on the current connection used by the outer transaction, or implement SAVEPOINT-based nesting instead of acquiring a second connection. Keep the fix localized to createDriver/getConnection usage in mysql-persistence.ts so the transaction wrapper reuses the existing transactional connection consistently. In `@examples/ts-react-chat/src/routes/mysql-persistence.tsx`: - Around line 157-167: The resume retry in mysql-persistence’s useEffect is swallowing failures incorrectly because `resume(...)` is invoked with `void`, so the synchronous `try/catch` around it never sees promise rejections. Update the effect to handle the async result from `resume` directly in the `attemptedStoredResumeRef` / `initialResumeSnapshot` path, and remove `resumeStorageKey` from `window.localStorage` in the rejection handler so failed snapshots are cleaned up instead of retried forever. --- Outside diff comments: In `@examples/ts-react-chat/src/routes/generations.image.tsx`: - Around line 1-16: The import block in generations.image.tsx violates the import/order rule because the relative import from ../lib/server-fns is placed after type-only imports; reorder the imports so the relative module import is grouped and sorted before the type-only package imports, keeping useState, createFileRoute, useGenerateImage, fetchServerSentEvents, and ImageGenerateInput organized according to the project’s lint ordering. --- Nitpick comments: In `@examples/ts-react-chat/src/routes/api.mysql-persistent-chat.ts`: - Around line 52-94: The single-producer-per-run logic in ensureProducer is relying on a subtle run-to-completion window between the second activeRuns.get(runId) check and activeRuns.set(runId, promise), which should be made explicit or replaced with a safer compare-and-swap/mutex pattern. Add a short comment near ensureProducer explaining the invariant that no await may occur before activeRuns.set, so future refactors don’t accidentally create duplicate producers for the same runId. Also remove the redundant activeRuns.delete(runId) branch immediately before the unconditional set, since it has no effect. Keep the existing activeRuns and resume handling behavior unchanged. In `@examples/ts-react-chat/src/routes/generations.image.tsx`: - Around line 9-15: The text-only prompt normalization logic in textPrompt is duplicated across the image and video generation routes, so extract it into a shared helper under examples/ts-react-chat/src/lib and reuse it from both routes. Move the prompt-to-string conversion into a single utility with the same behavior for string and structured prompt inputs, then update generations.image.tsx and generations.video.tsx to import and call that shared helper instead of keeping separate local copies. In `@examples/ts-react-chat/src/routes/generations.video.tsx`: - Around line 9-15: The textPrompt helper is duplicated in generations.video.tsx and generations.image.tsx, so extract the shared logic into a common prompt utility module (for example a prompt helper under lib) and update both generation routes to import and use it. Keep the existing textPrompt behavior the same, but move the implementation to a single shared function so future generation routes reuse the same source of truth.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
1010a71f-9b6c-4662-a6fb-7d65eca3a0e2📥 Commits
Reviewing files that changed from the base of the PR and between d0d86721418b9b9aba8ae79681684f0922ac936a and fc45b4c23ebd24b35a379831a177bedc790816f8.
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (20)
.changeset/persistence-layer.mdexamples/ts-react-chat/.env.exampleexamples/ts-react-chat/README.mdexamples/ts-react-chat/package.jsonexamples/ts-react-chat/src/components/Header.tsxexamples/ts-react-chat/src/lib/mysql-persistence.test.tsexamples/ts-react-chat/src/lib/mysql-persistence.tsexamples/ts-react-chat/src/routeTree.gen.tsexamples/ts-react-chat/src/routes/api.mysql-persistent-chat.tsexamples/ts-react-chat/src/routes/generations.image.tsxexamples/ts-react-chat/src/routes/generations.structured-output.tsxexamples/ts-react-chat/src/routes/generations.video.tsxexamples/ts-react-chat/src/routes/index.tsxexamples/ts-react-chat/src/routes/mysql-persistence.tsxpackages/ai-persistence-sql/package.jsonpackages/ai-persistence-sql/src/driver.tspackages/ai-persistence-sql/src/index.tspackages/ai-persistence-sql/src/migrations.tspackages/ai-persistence-sql/src/stores.tspackages/ai-persistence-sql/tests/sql-persistence.test.ts
✅ Files skipped from review due to trivial changes (3)
- examples/ts-react-chat/README.md
- .changeset/persistence-layer.md
- examples/ts-react-chat/src/routeTree.gen.ts
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/ai-persistence-sql/package.json
- packages/ai-persistence-sql/src/migrations.ts
@@ -2,3 +2,12 @@22 # Get yours at: https://platform.openai.com/api-keys33 OPENAI_API_KEY=445+# MySQL persistence demo (/mysql-persistence)6+# Use MYSQL_URL or the individual MYSQL_* settings below.7+# MYSQL_URL=mysql://root:password@127.0.0.1:3306/tanstack_ai_chat8+MYSQL_HOST=127.0.0.19+MYSQL_PORT=330610+MYSQL_USER=root11+MYSQL_PASSWORD=12+MYSQL_DATABASE=tanstack_ai_chat13+📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Reorder the
MYSQL_*keys.dotenv-linteris already flagging this block as unordered; sorting the example will keep the file lint-clean.🧰 Tools
🪛 dotenv-linter (4.0.0)
[warning] 11-11: [UnorderedKey] The MYSQL_PASSWORD key should go before the MYSQL_PORT key
(UnorderedKey)
[warning] 12-12: [UnorderedKey] The MYSQL_DATABASE key should go before the MYSQL_HOST key
(UnorderedKey)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/.env.example` around lines 5 - 13, The MYSQL_* example block is out of order and is triggering dotenv-linter in the ts-react-chat .env.example file. Reorder the variables in the MySQL persistence demo section so the MYSQL_* keys are sorted consistently, keeping the example lint-clean while preserving the existing values and comments.Source: Linters/SAST tools
examples/ts-react-chat/src/lib/mysql-persistence.ts lines 16 to 21OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,21 @@1+import mysql from 'mysql2/promise'2+import { createSqlPersistence } from '@tanstack/ai-persistence-sql'3+import type {4+ FieldPacket,5+ Pool,6+ PoolConnection,7+ PoolOptions,8+ QueryResult,9+ RowDataPacket,10+} from 'mysql2/promise'11+import type { ExecuteValues } from 'mysql2'12+import type { SqlDriver } from '@tanstack/ai-persistence-sql'13+14+let pool: Pool | undefined15+16+interface MysqlExecutable {17+ execute<T extends QueryResult>(18+ sql: string,19+ values?: Array<ExecuteValues>,20+ ): Promise<[T, Array<FieldPacket>]>21+}📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use function-property style per lint rule.
@typescript-eslint/method-signature-styleflags the shorthand method signature.🔧 Proposed fix
interface MysqlExecutable { - execute<T extends QueryResult>( - sql: string, - values?: Array<ExecuteValues>, - ): Promise<[T, Array<FieldPacket>]> + execute: <T extends QueryResult>( + sql: string, + values?: Array<ExecuteValues>, + ) => Promise<[T, Array<FieldPacket>]> }📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change16-interface MysqlExecutable {17- execute<T extends QueryResult>(18- sql: string,19- values?: Array<ExecuteValues>,20- ): Promise<[T, Array<FieldPacket>]>21-}16+interface MysqlExecutable {17+ execute: <T extends QueryResult>(18+ sql: string,19+ values?: Array<ExecuteValues>,20+ ) => Promise<[T, Array<FieldPacket>]>21+}🧰 Tools
🪛 ESLint
[error] 17-20: Shorthand method signature is forbidden. Use a function property instead.
(
@typescript-eslint/method-signature-style)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/lib/mysql-persistence.ts` around lines 16 - 21, The MysqlExecutable interface is using shorthand method syntax that violates `@typescript-eslint/method-signature-style`. Update the execute member in MysqlExecutable to use function-property syntax instead of a method signature, keeping the same generic type parameter, arguments, and Promise return type so the shape stays unchanged while satisfying the lint rule.Source: Linters/SAST tools
✅ Addressed in commits b3464cb to f9db0af
examples/ts-react-chat/src/lib/mysql-persistence.ts lines 105 to 122OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +102,21 @@102+ )103+ return rows.map((row) => ({ ...row })) as Array<T>104+ },105+ async transaction(fn) {106+ const connection = await getConnection()107+ const tx = createDriver(connection, getConnection)108+ try {109+ await connection.execute(110+ 'SET TRANSACTION ISOLATION LEVEL READ COMMITTED',111+ )112+ await connection.beginTransaction()113+ const result = await fn(tx)114+ await connection.commit()115+ return result116+ } catch (error) {117+ await connection.rollback()118+ throw error119+ } finally {120+ connection.release()121+ }122+ },🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash # Look for any store code that nests transaction() calls rg -n "\.transaction\(" -A5 --type=ts | rg -n "tx\.transaction\("Repository: TanStack/ai
Length of output: 149
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '\n== file outline ==\n' ast-grep outline examples/ts-react-chat/src/lib/mysql-persistence.ts --view expanded || true printf '\n== file excerpt ==\n' sed -n '1,220p' examples/ts-react-chat/src/lib/mysql-persistence.ts printf '\n== transaction call sites ==\n' rg -n "transaction\s*\(" examples/ts-react-chat/src/lib -g '*.ts' -g '*.tsx' -A3 -B3 || true printf '\n== tx.transaction usage ==\n' rg -n "tx\.transaction\s*\(" -g '*.ts' -g '*.tsx' . || trueRepository: TanStack/ai
Length of output: 4971
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '\n== persistence sql exports ==\n' fd -a . . | rg '`@tanstack/ai-persistence-sql`|persistence-sql' || true printf '\n== transaction-related symbols in repo ==\n' rg -n "interface SqlDriver|type SqlDriver|transaction\s*\(" . -g '*.ts' -g '*.tsx' -A4 -B4 || trueRepository: TanStack/ai
Length of output: 16341
🏁 Script executed:
#!/bin/bash set -euo pipefail printf '\n== files with persistence sql package references ==\n' rg -n "`@tanstack/ai-persistence-sql`|SqlDriver|createSqlPersistence" . -g '*.ts' -g '*.tsx' -g 'package.json' -A3 -B3 || true printf '\n== candidate package files ==\n' fd -a ".*" . | rg "ai-persistence-sql|persistence-sql|sql-persistence" || trueRepository: TanStack/ai
Length of output: 50367
🏁 Script executed:
#!/bin/bash set -euo pipefail rg -n "createSqlPersistence|interface SqlDriver|type SqlDriver|transaction\s*\(" . \ -g '*.ts' -g '*.tsx' -g 'package.json' -A4 -B4 || trueRepository: TanStack/ai
Length of output: 44460
Make nested MySQL transactions reuse the active connection
tx.transaction(...)still callsgetConnection(), so a nested transaction grabs a second pool connection instead of staying inside the outer one. That splits the work across independent transactions and can exhaust the 10-connection pool. Reuse the current transactional connection here or useSAVEPOINTs for nesting.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/lib/mysql-persistence.ts` around lines 105 - 122, The nested MySQL transaction handling in createDriver’s transaction method still opens a fresh pool connection via getConnection(), which breaks reuse of the active transaction context. Update transaction(fn) so nested tx.transaction(...) calls stay on the current connection used by the outer transaction, or implement SAVEPOINT-based nesting instead of acquiring a second connection. Keep the fix localized to createDriver/getConnection usage in mysql-persistence.ts so the transaction wrapper reuses the existing transactional connection consistently.✅ Addressed in commits b3464cb to d5e8af7
examples/ts-react-chat/src/routes/mysql-persistence.tsx lines 157 to 167OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +147,21 @@147+ stop,148+ clear,149+ } = useChat({150+ id: threadId,151+ threadId,152+ connection: fetchServerSentEvents('/api/mysql-persistent-chat'),153+ persistence: messagePersistence,154+ ...(initialResumeSnapshot ? { initialResumeSnapshot } : {}),155+ })156+157+ useEffect(() => {158+ if (attemptedStoredResumeRef.current) return159+ attemptedStoredResumeRef.current = true160+ if (!initialResumeSnapshot) return161+ if ((initialResumeSnapshot.pendingInterrupts ?? []).length > 0) return162+ try {163+ void resume(initialResumeSnapshot.resumeState)164+ } catch {165+ window.localStorage.removeItem(resumeStorageKey)166+ }167+ }, [initialResumeSnapshot, resume, resumeStorageKey])🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
try/catchcannot catch the rejection from avoid-called async function.resume(...)returns a Promise; calling it withvoidinside a synchronoustryblock means any rejection happens asynchronously after thetryhas already exited, so thecatchnever runs andresumeStorageKeyis never cleaned up on failure — the stale/broken snapshot will be retried indefinitely on future reloads.🐛 Proposed fix
- try { - void resume(initialResumeSnapshot.resumeState) - } catch { - window.localStorage.removeItem(resumeStorageKey) - } + resume(initialResumeSnapshot.resumeState).catch(() => { + window.localStorage.removeItem(resumeStorageKey) + })📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change157- useEffect(() => {158- if (attemptedStoredResumeRef.current) return159- attemptedStoredResumeRef.current = true160- if (!initialResumeSnapshot) return161- if ((initialResumeSnapshot.pendingInterrupts ?? []).length > 0) return162- try {163- void resume(initialResumeSnapshot.resumeState)164- } catch {165- window.localStorage.removeItem(resumeStorageKey)166- }167- }, [initialResumeSnapshot, resume, resumeStorageKey])157+ useEffect(() => {158+ if (attemptedStoredResumeRef.current) return159+ attemptedStoredResumeRef.current = true160+ if (!initialResumeSnapshot) return161+ if ((initialResumeSnapshot.pendingInterrupts ?? []).length > 0) return162+ resume(initialResumeSnapshot.resumeState).catch(() => {163+ window.localStorage.removeItem(resumeStorageKey)164+ })165+ }, [initialResumeSnapshot, resume, resumeStorageKey])🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/ts-react-chat/src/routes/mysql-persistence.tsx` around lines 157 - 167, The resume retry in mysql-persistence’s useEffect is swallowing failures incorrectly because `resume(...)` is invoked with `void`, so the synchronous `try/catch` around it never sees promise rejections. Update the effect to handle the async result from `resume` directly in the `attemptedStoredResumeRef` / `initialResumeSnapshot` path, and remove `resumeStorageKey` from `window.localStorage` in the rejection handler so failed snapshots are cleaned up instead of retried forever.✅ Addressed in commits 253a2eb to c864d51
Actionable comments posted: 2
🧹 Nitpick comments (1)
packages/ai-persistence-cloudflare/tests/d1.test.ts (1)
88-96: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueFakeR2Bucket.listnever exercises cursor pagination.listhardcodestruncated: falseand ignorescursor, so the store'sdo…whilepagination loop (srclist) only ever iterates once. The multi-page cursor path is untested. Consider a fake that paginates (e.g., chunked results with a realcursor) if you want coverage of that branch.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts` around lines 88 - 96, The FakeR2Bucket.list helper always returns truncated: false and ignores cursor, so the store’s list pagination branch is never covered. Update FakeR2Bucket.list to simulate paginated results using cursor and truncated state, so repeated calls in the store list loop can fetch multiple pages and exercise the do...while path.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/ai-persistence-cloudflare/src/index.ts`: - Around line 18-21: The file has two separate imports from `@tanstack/ai-persistence`, which triggers import/no-duplicates. Merge the ArtifactRecord and ArtifactStore type imports into the existing `@tanstack/ai-persistence` import statement in index.ts, keeping the same imported symbols and removing the duplicate import so lint passes. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts`: - Around line 6-10: The import order in the d1 test file violates the import/order lint rule because the value import from ../src/index is placed after the `@tanstack/ai` type import. Reorder the imports so the ../src/index value import comes before the type-only `@tanstack/ai` import, keeping the grouped imports in the order expected by ESLint; use the existing import block in d1.test.ts as the place to fix it. --- Nitpick comments: In `@packages/ai-persistence-cloudflare/tests/d1.test.ts`: - Around line 88-96: The FakeR2Bucket.list helper always returns truncated: false and ignores cursor, so the store’s list pagination branch is never covered. Update FakeR2Bucket.list to simulate paginated results using cursor and truncated state, so repeated calls in the store list loop can fetch multiple pages and exercise the do...while path.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
6019e8fb-3ec2-4605-b731-67098ea7c177📥 Commits
Reviewing files that changed from the base of the PR and between 3a75f11bead9cecd8f5bb9fab4cf919b0367e608 and 0d661c87c857d677f614bd06ae30375d53f17559.
📒 Files selected for processing (5)
.changeset/persistence-layer.mddocs/config.jsondocs/persistence/overview.mdpackages/ai-persistence-cloudflare/src/index.tspackages/ai-persistence-cloudflare/tests/d1.test.ts
✅ Files skipped from review due to trivial changes (3)
- docs/config.json
- docs/persistence/overview.md
- .changeset/persistence-layer.md
packages/ai-persistence-cloudflare/src/index.ts lines 3 to 10OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,21 @@1+/// <reference types="@cloudflare/workers-types" />2+/**3+ * Cloudflare backend. D1 (SQLite-compatible) backs the shared SQL stores; an4+ * optional Durable Object namespace can back the distributed lock.5+ *6+ * COMPILE-VERIFIED ONLY: this package type-checks against real `@cloudflare`7+ * types but is not runtime-verified here (it needs a Workers runtime). The D18+ * driver itself is unit-tested against a fake D1 so the adapter logic is9+ * covered.10+ */11+import { createSqlPersistence } from '@tanstack/ai-persistence-sql'12+import type {13+ SqlDriver,14+ SqlPersistence,15+ SqlRow,16+} from '@tanstack/ai-persistence-sql'17+import type { PersistenceMode } from '@tanstack/ai-persistence'18+import type {19+ ArtifactRecord,20+ ArtifactStore,21+} from '@tanstack/ai-persistence'📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash rg -nP "from '`@tanstack/ai-persistence`'" packages/ai-persistence-cloudflare/src/index.tsRepository: TanStack/ai
Length of output: 253
Merge the duplicate
@tanstack/ai-persistenceimport. Combine the type imports into the existing statement to avoidimport/no-duplicatesand keep lint passing.🧰 Tools
🪛 ESLint
[error] 21-21: '
@tanstack/ai-persistence' imported multiple times.(import/no-duplicates)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 18 - 21, The file has two separate imports from `@tanstack/ai-persistence`, which triggers import/no-duplicates. Merge the ArtifactRecord and ArtifactStore type imports into the existing `@tanstack/ai-persistence` import statement in index.ts, keeping the same imported symbols and removing the duplicate import so lint passes.Source: Linters/SAST tools
✅ Addressed in commits 253a2eb to c864d51
packages/ai-persistence-cloudflare/tests/d1.test.ts lines 6 to 10OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,10 @@1+/// <reference types="@cloudflare/workers-types" />2+import { describe, expect, it } from 'vitest'3+import { DatabaseSync } from 'node:sqlite'4+import { EventType } from '@tanstack/ai'5+import type { StreamChunk } from '@tanstack/ai'6+import {7+ cloudflarePersistence,8+ createD1Driver,9+ createR2ArtifactStore,10+} from '../src/index'📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Fix import ordering to satisfy lint.
ESLint (
import/order, error-level) requires the../src/indexvalue import to appear before the@tanstack/aitype import, so this will fail lint CI. Reorder accordingly.🧰 Tools
🪛 ESLint
[error] 6-10:
../src/indeximport should occur before type import of@tanstack/ai(import/order)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts` around lines 6 - 10, The import order in the d1 test file violates the import/order lint rule because the value import from ../src/index is placed after the `@tanstack/ai` type import. Reorder the imports so the ../src/index value import comes before the type-only `@tanstack/ai` import, keeping the grouped imports in the order expected by ESLint; use the existing import block in d1.test.ts as the place to fix it.Source: Linters/SAST tools
✅ Addressed in commits 253a2eb to 8d61c54
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/ai-persistence-cloudflare/src/index.ts (1)
606-686: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick winDurable Object lock
fetchcalls still have no timeout.Response status is now validated on acquire/renew/release (fixing the earlier "silently proceeds" concern), but none of the
stub.fetch(...)calls have any timeout/AbortSignal. A hung Durable Object (network partition, DO overload, etc.) would block the acquire loop, the renewal interval, and the release call indefinitely, holding the caller's request thread — an external-call hazard on a path meant to guarantee mutual exclusion with bounded lease semantics.⏱️ Suggested direction
+const LOCK_FETCH_TIMEOUT_MS = 5_000 + // ... - const response = await stub.fetch('https://lock/acquire', { - method: 'POST', - body: JSON.stringify({ owner, leaseMs }), - }) + const response = await stub.fetch('https://lock/acquire', { + method: 'POST', + body: JSON.stringify({ owner, leaseMs }), + signal: AbortSignal.timeout(LOCK_FETCH_TIMEOUT_MS), + })Apply similarly to the
/renewand/releasefetch calls.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 606 - 686, The Durable Object lock path in withLock still uses stub.fetch without any timeout, so acquire, renew, and release can hang indefinitely. Add AbortSignal-based timeouts around the lock-related fetch calls in withLock, and apply the same timeout handling consistently to the /lock/acquire, /lock/renew, and /lock/release requests. Make sure the timeout errors are propagated through the existing responseError and renewal/release error handling so the caller does not block forever.🧹 Nitpick comments (3)
packages/ai-persistence-cloudflare/src/index.ts (1)
418-422: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPossibly-unnecessary type assertion.
ESLint flags
as CloudflarePersistencehere as unnecessary. Since every fieldCloudflarePersistenceadds beyondSqlPersistence(stores.locks,stores.artifacts,stores.blobs, deprecatedlocks/artifacts) is optional, theSqlPersistencereturn value fromcreateSqlPersistencemay already satisfyCloudflarePersistencestructurally without a cast.🧹 Proposed fix (verify it compiles once applied)
- const persistence = createSqlPersistence(driver, { - mode: opts.mode, - migrate: opts.migrate, - }) as CloudflarePersistence + const persistence: CloudflarePersistence = createSqlPersistence(driver, { + mode: opts.mode, + migrate: opts.migrate, + })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 418 - 422, The `createSqlPersistence` result in `index.ts` is being forced into `CloudflarePersistence` with an unnecessary type assertion. Remove the `as CloudflarePersistence` cast from the `createSqlPersistence` assignment and let the inferred `SqlPersistence` value flow through, since the extra `CloudflarePersistence` fields are optional and should already be structurally compatible. Verify the `createD1Driver`, `createSqlPersistence`, and `CloudflarePersistence` usage still type-check without the assertion.Source: Linters/SAST tools
packages/ai-persistence/src/memory.ts (1)
469-491: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winNon-null assertion flagged by lint; can be avoided.
this.blobs.get(key)!.record(Line 484) is functionally safe sincekeycomes from the same map'skeys(), but tripsno-non-null-assertion(error level per static analysis). Mapping overentries()instead of re-looking-up by key avoids the assertion.🧹 Proposed fix
const keys = [...this.blobs.keys()] .filter((key) => key.startsWith(options?.prefix ?? '')) .filter((key) => options?.cursor === undefined || key > options.cursor) .sort() const pageKeys = limit === undefined ? keys : keys.slice(0, limit) - const objects = pageKeys.map((key) => - blobRecordSnapshot(this.blobs.get(key)!.record), - ) + const objects = pageKeys.flatMap((key) => { + const entry = this.blobs.get(key) + return entry ? [blobRecordSnapshot(entry.record)] : [] + })🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence/src/memory.ts` around lines 469 - 491, The non-null assertion in MemoryBlobStore list is tripping lint because the code re-looks up each key from this.blobs after iterating its keys. Update list() to avoid the this.blobs.get(key)! access by iterating over entries() or otherwise carrying the BlobRecord alongside each key, and keep the existing pagination/filtering behavior in list, blobRecordSnapshot, and the cursor/truncated result unchanged.Source: Linters/SAST tools
packages/ai-persistence-cloudflare/tests/d1.test.ts (1)
9-16: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winFix import member ordering to satisfy lint.
Static analysis flags
sort-imports: members of the'../src/index'import aren't alphabetically ordered.🧹 Proposed fix
import { - createR2BlobStore, - createR2ArtifactStore, - createDurableObjectLockStore, - createCloudflareArtifactStore, cloudflareArtifactDdl, + createCloudflareArtifactStore, + createDurableObjectLockStore, + createR2ArtifactStore, + createR2BlobStore, LockDurableObject, } from '../src/index'🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts` around lines 9 - 16, The named imports from the ../src/index module are not alphabetically ordered, which violates sort-imports. Reorder the members in the import statement for createR2BlobStore, createR2ArtifactStore, createDurableObjectLockStore, createCloudflareArtifactStore, cloudflareArtifactDdl, and LockDurableObject so they are sorted consistently while leaving the SqlDriver type import unchanged.Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Inline comments: In `@packages/ai-persistence-cloudflare/src/index.ts`: - Around line 876-889: The deleteForRun flow in the artifacts persistence layer can leave D1 records referencing blobs that were already deleted if one blob removal fails during Promise.all. Update deleteForRun in the ai-persistence-cloudflare index to process rows sequentially, following the same per-row order used by delete(), so each blob is removed successfully before deleting its corresponding artifact row. This should use the existing deleteForRun method and blobStore.delete/driver.exec paths, but avoid the all-at-once batch behavior. --- Duplicate comments: In `@packages/ai-persistence-cloudflare/src/index.ts`: - Around line 606-686: The Durable Object lock path in withLock still uses stub.fetch without any timeout, so acquire, renew, and release can hang indefinitely. Add AbortSignal-based timeouts around the lock-related fetch calls in withLock, and apply the same timeout handling consistently to the /lock/acquire, /lock/renew, and /lock/release requests. Make sure the timeout errors are propagated through the existing responseError and renewal/release error handling so the caller does not block forever. --- Nitpick comments: In `@packages/ai-persistence-cloudflare/src/index.ts`: - Around line 418-422: The `createSqlPersistence` result in `index.ts` is being forced into `CloudflarePersistence` with an unnecessary type assertion. Remove the `as CloudflarePersistence` cast from the `createSqlPersistence` assignment and let the inferred `SqlPersistence` value flow through, since the extra `CloudflarePersistence` fields are optional and should already be structurally compatible. Verify the `createD1Driver`, `createSqlPersistence`, and `CloudflarePersistence` usage still type-check without the assertion. In `@packages/ai-persistence-cloudflare/tests/d1.test.ts`: - Around line 9-16: The named imports from the ../src/index module are not alphabetically ordered, which violates sort-imports. Reorder the members in the import statement for createR2BlobStore, createR2ArtifactStore, createDurableObjectLockStore, createCloudflareArtifactStore, cloudflareArtifactDdl, and LockDurableObject so they are sorted consistently while leaving the SqlDriver type import unchanged. In `@packages/ai-persistence/src/memory.ts`: - Around line 469-491: The non-null assertion in MemoryBlobStore list is tripping lint because the code re-looks up each key from this.blobs after iterating its keys. Update list() to avoid the this.blobs.get(key)! access by iterating over entries() or otherwise carrying the BlobRecord alongside each key, and keep the existing pagination/filtering behavior in list, blobRecordSnapshot, and the cursor/truncated result unchanged.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
21f5fb63-a7a3-4b14-b804-a4a228d826fe📥 Commits
Reviewing files that changed from the base of the PR and between 0d661c87c857d677f614bd06ae30375d53f17559 and 253a2eb8c1b06def51ccc40702b64cea3a134d53.
📒 Files selected for processing (9)
.changeset/persistence-layer.mddocs/persistence/overview.mdpackages/ai-persistence-cloudflare/src/index.tspackages/ai-persistence-cloudflare/tests/d1.test.tspackages/ai-persistence/src/index.tspackages/ai-persistence/src/memory.tspackages/ai-persistence/src/middleware.tspackages/ai-persistence/src/types.tspackages/ai-persistence/tests/memory.test.ts
✅ Files skipped from review due to trivial changes (2)
- .changeset/persistence-layer.md
- docs/persistence/overview.md
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/ai-persistence/src/index.ts
- packages/ai-persistence/src/middleware.ts
packages/ai-persistence-cloudflare/src/index.ts lines 876 to 889OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +869,21 @@869+ if (!row) return870+ if (row.blob_key) await blobStore.delete(String(row.blob_key))871+ await driver.exec('DELETE FROM artifacts WHERE artifact_id = ?', [872+ artifactId,873+ ])874+ },875+876+ async deleteForRun(runId) {877+ await ensureArtifactsTable()878+ const rows = await driver.query<ArtifactRow>(879+ 'SELECT * FROM artifacts WHERE run_id = ?',880+ [runId],881+ )882+ await Promise.all(883+ rows884+ .map((row) => row.blob_key)885+ .filter((key): key is string => typeof key === 'string')886+ .map((key) => blobStore.delete(key)),887+ )888+ await driver.exec('DELETE FROM artifacts WHERE run_id = ?', [runId])889+ },🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
git ls-files packages/ai-persistence-cloudflare/src/index.ts packages/ai-persistence-cloudflare/src/*.ts packages/ai-persistence-cloudflare/**/*.test.tsRepository: TanStack/ai
Length of output: 249
🏁 Script executed:
sed -n '820,930p' packages/ai-persistence-cloudflare/src/index.ts && printf '\n--- TESTS ---\n' && sed -n '1,260p' packages/ai-persistence-cloudflare/tests/d1.test.tsRepository: TanStack/ai
Length of output: 9167
🏁 Script executed:
sed -n '300,380p' packages/ai-persistence-cloudflare/src/index.ts && printf '\n--- DELETE-FOR-RUN TESTS ---\n' && rg -n "deleteForRun|delete\\(" packages/ai-persistence-cloudflare/tests/d1.test.ts -A6 -B6Repository: TanStack/ai
Length of output: 9339
deleteForRuncan leave D1 rows pointing at deleted blobs on partial failure.Promise.allstarts everyblobStore.delete()at once, so if one delete fails after another has already removed its blob, the batchDELETE FROM artifactsnever runs andget()will later return the row withoutbytes. Process rows one at a time and delete each row after its blob succeeds, matchingdelete().🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/ai-persistence-cloudflare/src/index.ts` around lines 876 - 889, The deleteForRun flow in the artifacts persistence layer can leave D1 records referencing blobs that were already deleted if one blob removal fails during Promise.all. Update deleteForRun in the ai-persistence-cloudflare index to process rows sequentially, following the same per-row order used by delete(), so each blob is removed successfully before deleting its corresponding artifact row. This should use the existing deleteForRun method and blobStore.delete/driver.exec paths, but avoid the all-at-once batch behavior.✅ Addressed in commits 253a2eb to a46c9bc
tombeckenham assignedtombeckenham
AlemTuzlak changed the title
tombeckenham reviewed
@@ -0,0 +12,21 @@12+`AIPersistence` stores it needs.13+14+## Lever 1: no persistence15+16+Use no persistence when a run can disappear after the request ends.17+18+```ts19+import { chat } from '@tanstack/ai'20+import { anthropicText } from '@tanstack/ai-anthropic'21+22+chat({23+ adapter: anthropicText('claude-sonnet-4-6'),24+ messages: [{ role: 'user', content: 'Draft a release note.' }],25+})26+```27+28+There is no resume cursor, no server transcript, and no durable run record. This29+is enough for one-shot server work, tests, or prototypes where reload recovery30+does not matter.31+32+## Lever 2: client snapshotsThese levels are really difficult to understand. I've read it several times, and I'm still not sure when I'd use what level. It needs something visualy, like an svg. Perhaps even a hyperframes video. It's also unclear if I can use the native conversations ids like Google interactions and OpenAI responses. For example, I could use local storage on the browser and a Google interaction id if I just want to continue a conversation and for Google to retain the context
tombeckenham reviewed
@@ -0,0 +61,21 @@61+import type {62+ ArtifactStore,63+ BlobStore,64+ InternalEventStore,65+ InterruptStore,66+ MessageStore,67+ MetadataStore,68+ PublicEventStore,69+ RunStore,70+} from '@tanstack/ai-persistence'71+import type { LockStore } from '@tanstack/ai'72+73+declare const messages: MessageStore74+declare const runs: RunStore75+declare const publicEvents: PublicEventStore76+declare const internalEvents: InternalEventStore77+declare const interrupts: InterruptStore78+declare const metadata: MetadataStore79+declare const locks: LockStore80+declare const artifacts: ArtifactStore81+declare const blobs: BlobStoreIt would be useful in this doc to explain what each of these storages is for. A one liner against each
tombeckenham reviewed
@@ -0,0 +1,21 @@1+---2+title: Persistence with Drizzle3+id: drizzle4+---5+6+Use Drizzle persistence when Drizzle already owns your SQLite or Postgres7+database connection and migration workflow. The adapter unwraps Drizzle's8+underlying client and exposes the shared SQL-backed `AIPersistence` stores to9+`withPersistence(...)`.10+11+By the end, your Drizzle-backed server can persist chat messages, replay12+events, interrupts, metadata, and other SQL-backed stores while keeping schema13+deployment in your existing Drizzle workflow.14+15+## Generate the migration16+17+Create a Drizzle migration file for your dialect.18+19+```sh20+pnpm exec tanstack-ai-persistence-drizzle --dialect postgres21+```This is all wrong.... we should never be generating migrations directly. We should instead generate schemas in typescript. if we generate the migrations, the schemas will be out of sync at all times. Look at how better-auth works
Also Drizzle now has 2 different migration formats, and different types of migrations that depend on what db is used behind it.
Scalability note: per-chunk event persistence on D1, and the path forward#
Wrote up a load analysis of the durable-replay hot path with the Cloudflare backend. TL;DR: the current per-chunk write path falls over well before quotas matter — the fixes are a batched append seam in the store contract and, longer term, a DO-per-thread hot tier.
What the hot path costs today#
With
cloudflarePersistence(...), durable replay is on by default (defaultFeaturesenables it whenever apublicEventsstore exists —packages/ai-persistence/src/middleware.ts:99-101), andonChunkawaits an append for every AG-UI event — every text delta (middleware.ts:975). D1 has no interactive transactions, so the driver'stransaction()is a pass-through (packages/ai-persistence-cloudflare/src/index.ts:382-384), which makes the CAS append (packages/ai-persistence-sql/src/stores.ts:206-244) 4 sequential D1 round trips per chunk: read-at-target,SELECT MAX(seq), INSERT, read-back. A typical streamed response is ~200-300 events.Which limits break, in order (100 concurrent users)#
- Per-invocation subrequest cap — breaks on a single long response. D1 queries count as subrequests (1,000 paid / 50 free). At 4 queries/chunk, any response past ~250 chunks dies mid-stream with "Too many subrequests". Needs zero concurrency to trigger.
- Write serialization — breaks with a handful of concurrent streams. One D1 database is single-writer SQLite. 100 streams × ~30-50 deltas/sec ≈ 3-5k appends/sec (12-20k queries/sec) against a store realistically good for hundreds to ~1k serial writes/sec. Because
onChunkawaits inline, the queue backpressures every user's stream. - Free-tier daily writes — minutes. 100k rows/day ÷ ~250 rows/message ≈ 400 messages total.
- 10 GB database cap — weeks. ~200-500 B/event; there's no pruning/retention API, and
public_eventsgrows forever, including for completed runs whose messages are already durably saved. - Paid monthly write quota — actually fine. ~$1/M rows overage; cost is not the constraint.
Recommendations#
1. Add
appendBatchtoPublicEventStore+ a buffering decorator. Buffer N events (usually-synconChunk), flush on count / boundary events (RUN_FINISHED, interrupt outcomes) / terminal drain inonFinish/onError/onAbort, with a high-water mark that degrades to per-chunk awaiting when the backend is slow. Each backend gets its atomic verb:db.batch()on D1, multi-rowINSERTon Postgres/MySQL,TransactWriteItemson DynamoDB (note:BatchWriteItemcan't carry the CAS condition), pipelinedXADDon Redis. At N=25 a 300-chunk response drops from ~1,200 D1 subrequests to ~12. Keeping one row per event preserves the idempotent re-append/retry safety the stores already have.2. Adopt clamp-and-replay resume semantics. With batching, a crashed run can leave a client cursor up to N−1 events past
latestSeq;resume-source.tscurrently throws on beyond-latest. The portable contract should be "resume frommin(clientSeq, latestSeq)" — the client harmlessly re-receives a few events it already rendered. This matters most on platforms withoutwaitUntil(Lambda, Cloud Run), where a hard kill can eat a buffered tail.3. Offer a
waitUntilescape hatch — e.g.withPersistence(p, { waitUntil: ctx.waitUntil })— so the final flush can ride past the response on CF/Vercel/Netlify-edge. Default stays drain-before-close, which is correct everywhere (Lambda freezes pending promises; Cloud Run throttles CPU post-response). The writer must be per-run, never module scope (Vercel Fluid / Lambda reuse).4. Longer term on Cloudflare: DO-per-thread hot tier. Running the stream inside a per-thread Durable Object makes appends local synchronous SQLite writes (zero subrequests, no CAS needed — single-writer by construction,
locksbecomes redundant), and WebSocket hibernation provides thestream.publishfan-out natively. OnRUN_FINISHED, one batched flush of messages/run status/interrupts to D1 or Postgres-via-Hyperdrive (three orders of magnitude less traffic — comfortably inside D1's envelope), raw event log optionally archived to R2 (~$0.015/GB-mo vs 10-50× that for DO/D1) or just dropped, then the DO prunes itself via alarm. The store contracts already split cleanly along this line; the missing piece is a DO-backedPublicEventStorewith the completion-time flush.5. Docs callout in the meantime:
features: ['messages', 'interrupts']disables durable replay while keeping thread persistence — worth stating prominently indocs/persistence/cloudflare.md, since the default silently opts every D1 user into the per-token write path. A retention/pruning story forpublic_eventsis needed regardless.Off-Cloudflare, for reference: a single Postgres instance absorbs this workload with an order of magnitude of headroom (the
ai-persistence-postgresbackend is the boring right answer); Redis Streams in front of Postgres is the optimal split if live-tail fan-out becomes a product feature.🤖 Generated with Claude Code
Warning
Review the following alerts detected in dependencies.
According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.
Action Severity Alert (click "▶" to expand/collapse) Warn Obfuscated code: npm
drizzle-ormis 90.0% likely obfuscatedConfidence: 0.90
Location: Package overview
From: pnpm-lock.yaml →
npm/@tanstack/nitro-v2-vite-plugin@1.154.7→npm/@tanstack/start@1.120.20→npm/drizzle-orm@0.45.2ℹ Read more on: This package | This alert | What is obfuscated code?
Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at
support@socket.dev.Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.
Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment
@SocketSecurity ignore npm/drizzle-orm@0.45.2. You can also ignore all packages with@SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.Warn Obfuscated code: npm
effectis 90.0% likely obfuscatedConfidence: 0.90
Location: Package overview
From: pnpm-lock.yaml →
npm/prisma@6.19.3→npm/effect@3.21.0ℹ Read more on: This package | This alert | What is obfuscated code?
Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at
support@socket.dev.Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.
Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment
@SocketSecurity ignore npm/effect@3.21.0. You can also ignore all packages with@SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.Warn Deprecated by its maintainer: npm
@esbuild-kit/core-utilsReason: Merged into tsx: https://tsx.hirok.io
From: pnpm-lock.yaml →
npm/drizzle-kit@0.31.10→npm/@esbuild-kit/core-utils@3.3.2ℹ Read more on: This package | This alert | What is a deprecated package?
Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at
support@socket.dev.Suggestion: Research the state of the package and determine if there are non-deprecated versions that can be used, or if it should be replaced with a new, supported solution.
Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment
@SocketSecurity ignore npm/@esbuild-kit/core-utils@3.3.2. You can also ignore all packages with@SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.Warn Deprecated by its maintainer: npm
@esbuild-kit/esm-loaderReason: Merged into tsx: https://tsx.hirok.io
From: pnpm-lock.yaml →
npm/drizzle-kit@0.31.10→npm/@esbuild-kit/esm-loader@2.6.5ℹ Read more on: This package | This alert | What is a deprecated package?
Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at
support@socket.dev.Suggestion: Research the state of the package and determine if there are non-deprecated versions that can be used, or if it should be replaced with a new, supported solution.
Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment
@SocketSecurity ignore npm/@esbuild-kit/esm-loader@2.6.5. You can also ignore all packages with@SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.Delivery durability (resumable streams) has been split out of this branch into its own PR: #955. This PR is now state persistence only — middleware, store contract, backends, and interrupt resume. The two features share no code; see #955 for the transport-level StreamDurability seam,
@tanstack/ai-durable-stream, and the resumable-SSE client.🤖 Generated with Claude Code
tombeckenham added labelspersistence
Superseded by the split this branch was reference material for. The work now lands as three stacked PRs:
- #984
feat/persistence-core→main— server persistence (@tanstack/ai-persistence) + client browser-refresh durability - #987
feat/generation-persistence→ #984 — generation persistence (client snapshot + durable media bytes) - #988
feat/persistence-sandbox→ #984 — sandbox persistence (SandboxStore + shared lock)
Three things changed materially versus this PR, so it is not a straight re-cut:
- No backend packages.
-drizzle,-prisma, and-cloudflareare not published. A backend package has to own DDL, which puts it in permanent conflict with the migration tooling the consuming app already runs.@tanstack/ai-persistenceships the store contracts and a conformance testkit; per-stack agent skills read your existing ORM config and write achat-persistence.tsagainst it. - Locks are not a store.
AIPersistence.storesaccepts exactly four keys and throws on anything else; coordination is wired separately withwithLocks. - The middleware no longer builds
ChatResumeToolState. The shipped ephemeral engine reconstructs resume state from the resume batch plus interrupt bindings in the server-loaded history; persistence records interrupts and gates new input.
Closing so the stack is the single live thread. The branch stays put if anything needs pulling from it.
- #984
tombeckenham closed this
Sign in to join this conversation.
Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
📝 Walkthrough
Walkthrough#
Adds durable chat persistence with interrupt-based waits, SQL-backed persistence packages and adapters, framework resume-state wiring, e2e persistence coverage, and updated docs and skills content.
Changes#
Durable chat persistence and resume flow
packages/ai/src/*,packages/ai/tests/*packages/ai-persistence/src/*,packages/ai-persistence/tests/*packages/ai-persistence-sql/src/*,packages/ai-persistence-sql/tests/*packages/ai-persistence-{sqlite,postgres,drizzle,prisma,cloudflare}/*,packages/ai-sandbox-persistence/*packages/ai-client/src/*,packages/ai-client/tests/*ChatClient, propagates cursor and resume through connection adapters, and exports updated client types and tests.packages/ai-react/src/*,packages/ai-vue/src/*,packages/ai-solid/src/*,packages/ai-svelte/src/*,packages/ai-preact/src/*testing/e2e/*docs/*,.changeset/*,packages/ai/skills/*Miscellaneous maintenance fixes
docs/media/transcription.md,packages/ai-angular/package.json,knip.json,packages/ai-sandbox/tests/harness-cwd.test.ts,packages/ai/package.jsonEstimated code review effort: 5 (Critical) | ~150 minutes
Sequence Diagram(s)#
Possibly related PRs
RunAgentInputContextand cursor/resume payload handling.Suggested reviewers:
tombeckenham🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share