Skip to content
Source
Sign inSign up
TanStack/ai

feat: add AI persistence and generation resumability #785

ClosedAlemTuzlakwants to merge 43 commits intomain fromfeat/persistenceopened View on GitHub

AlemTuzlakcommented

Summary

  • Adds generation artifact persistence on top of withPersistence, including input/output artifact refs, blob-backed media storage, generation:artifacts events, 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:docs
  • git 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. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      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_commits setting.

      Use the following commands to manage reviews:

      • @coderabbitai resume to resume automatic reviews.
      • @coderabbitai review to trigger a single review.

      Use the checkboxes below for quick actions:

      • ▶️ Resume reviews
      • 🔍 Trigger review
      📝 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

      Layer / File(s)Summary
      Core protocol and chat engine
      packages/ai/src/*, packages/ai/tests/*
      Adds interrupt/resume types, cursor helpers, custom events, lock and resume-source capabilities, chat-engine replay, and interrupt outcome emission.
      ai-persistence core
      packages/ai-persistence/src/*, packages/ai-persistence/tests/*
      Adds persistence contracts, in-memory stores, cursor replay helpers, middleware, controllers, capabilities, and history loading.
      SQL core and migrations
      packages/ai-persistence-sql/src/*, packages/ai-persistence-sql/tests/*
      Adds the shared SQL driver contract, versioned migrations, SQL-backed stores, lazy migration gating, and SQL persistence assembly.
      Backend adapters and sandbox bridge
      packages/ai-persistence-{sqlite,postgres,drizzle,prisma,cloudflare}/*, packages/ai-sandbox-persistence/*
      Adds SQLite, Postgres, Drizzle, Prisma, Cloudflare D1, and sandbox persistence adapters with package scaffolding and tests.
      Client resume state and adapters
      packages/ai-client/src/*, packages/ai-client/tests/*
      Tracks resume state in ChatClient, propagates cursor and resume through connection adapters, and exports updated client types and tests.
      Framework hooks
      packages/ai-react/src/*, packages/ai-vue/src/*, packages/ai-solid/src/*, packages/ai-svelte/src/*, packages/ai-preact/src/*
      Adds resume state, pending interrupts, and resume controls to React, Vue, Solid, Svelte, and Preact hooks and their tests.
      E2E persistence demo
      testing/e2e/*
      Threads server persistence through the demo app, adds interrupt-aware UI state and localStorage resume snapshots, and covers durable replay and approval resumption in Playwright tests.
      Docs and skills
      docs/*, .changeset/*, packages/ai/skills/*
      Adds persistence docs, updates approval-flow and connection-adapter docs, refreshes docs navigation, and updates persistence and tool-calling skills.

      Miscellaneous maintenance fixes

      Layer / File(s)Summary
      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.json
      Updates transcription docs wording, switches the Angular build cleanup command, adds a knip workspace entry, normalizes sandbox test path separators, and bumps the AG-UI core dependency.

      Estimated code review effort: 5 (Critical) | ~150 minutes

      Sequence Diagram(s)

      sequenceDiagram
        participant App
        participant ChatClient
        participant ConnectionAdapter
        participant Server
        participant withPersistence
        participant AIPersistence
      
        App->>ChatClient: chat(messages) / resumeInterrupts(items)
        ChatClient->>ConnectionAdapter: connect(runContext incl. cursor, resume)
        ConnectionAdapter->>Server: POST /api/chat { cursor, resume }
        Server->>withPersistence: onConfig(cursor, resume)
        withPersistence->>AIPersistence: validateReplayCursor / loadPendingInterrupts
        AIPersistence-->>withPersistence: run record, pending interrupts
        alt cursor valid, no resume
          withPersistence-->>Server: replay persisted events
        else resume matches pending interrupts
          withPersistence->>AIPersistence: resolve/cancel interrupts
          Server->>Server: continue adapter execution
        end
        Server-->>ConnectionAdapter: stream chunks
        ConnectionAdapter-->>ChatClient: chunks
        ChatClient-->>App: onResumeStateChange(resumeState, pendingInterrupts)

      Possibly related PRs

      • TanStack/ai#597: Both update the adapter-facing RunAgentInputContext and cursor/resume payload handling.
      • TanStack/ai#763: Both use the capability and middleware plumbing that wires persistence into chat.
      • TanStack/ai#681: Both change connection-adapter examples and server-function fetcher wiring.

      Suggested reviewers: tombeckenham

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Description check⚠️ WarningThe description is off-template and omits the required Changes, Checklist, and Release Impact sections.Rewrite the PR body to match the template with ## 🎯 Changes, ## ✅ Checklist, and ## 🚀 Release Impact, and fill in the checklist and release details.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Title check✅ PassedThe title is concise and accurately captures the main theme of persistence and resumability.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `feat/persistence`

      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
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab)commented

      🚀 Changeset Version Preview

      14 package(s) bumped directly, 36 bumped as dependents.

      🟥 Major bumps

      PackageVersionReason
      @tanstack/ai-angular0.2.4 → 1.0.0Changeset
      @tanstack/ai-persistence0.0.0 → 1.0.0Changeset
      @tanstack/ai-persistence-cloudflare0.0.0 → 1.0.0Changeset
      @tanstack/ai-persistence-drizzle0.0.0 → 1.0.0Changeset
      @tanstack/ai-persistence-prisma0.0.0 → 1.0.0Changeset
      @tanstack/ai-preact0.10.4 → 1.0.0Changeset
      @tanstack/ai-react0.17.0 → 1.0.0Changeset
      @tanstack/ai-sandbox0.2.3 → 1.0.0Changeset
      @tanstack/ai-solid0.14.4 → 1.0.0Changeset
      @tanstack/ai-svelte0.14.4 → 1.0.0Changeset
      @tanstack/ai-vue0.14.4 → 1.0.0Changeset
      @tanstack/ai-acp0.2.2 → 1.0.0Dependent
      @tanstack/ai-anthropic0.16.2 → 1.0.0Dependent
      @tanstack/ai-bedrock0.1.3 → 1.0.0Dependent
      @tanstack/ai-claude-code0.2.2 → 1.0.0Dependent
      @tanstack/ai-code-mode0.3.7 → 1.0.0Dependent
      @tanstack/ai-code-mode-skills0.3.10 → 1.0.0Dependent
      @tanstack/ai-codex0.2.2 → 1.0.0Dependent
      @tanstack/ai-elevenlabs0.2.33 → 1.0.0Dependent
      @tanstack/ai-fal0.9.11 → 1.0.0Dependent
      @tanstack/ai-gemini0.20.0 → 1.0.0Dependent
      @tanstack/ai-grok0.14.8 → 1.0.0Dependent
      @tanstack/ai-grok-build0.2.2 → 1.0.0Dependent
      @tanstack/ai-groq0.5.2 → 1.0.0Dependent
      @tanstack/ai-isolate-node0.1.46 → 1.0.0Dependent
      @tanstack/ai-isolate-quickjs0.1.46 → 1.0.0Dependent
      @tanstack/ai-mistral0.2.2 → 1.0.0Dependent
      @tanstack/ai-ollama0.8.15 → 1.0.0Dependent
      @tanstack/ai-openai0.17.0 → 1.0.0Dependent
      @tanstack/ai-opencode0.2.2 → 1.0.0Dependent
      @tanstack/ai-openrouter0.15.9 → 1.0.0Dependent
      @tanstack/ai-react-ui0.8.14 → 1.0.0Dependent
      @tanstack/ai-sandbox-cloudflare0.2.3 → 1.0.0Dependent
      @tanstack/ai-sandbox-daytona0.2.0 → 1.0.0Dependent
      @tanstack/ai-sandbox-docker0.2.0 → 1.0.0Dependent
      @tanstack/ai-sandbox-local-process0.2.0 → 1.0.0Dependent
      @tanstack/ai-sandbox-sprites0.2.1 → 1.0.0Dependent
      @tanstack/ai-sandbox-vercel0.2.0 → 1.0.0Dependent
      @tanstack/ai-solid-ui0.7.13 → 1.0.0Dependent
      @tanstack/openai-base0.9.8 → 1.0.0Dependent

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/ai0.41.0 → 0.42.0Changeset
      @tanstack/ai-client0.21.0 → 0.22.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/ai-event-client0.6.8 → 0.6.9Changeset
      @tanstack/ai-devtools-core0.4.23 → 0.4.24Dependent
      @tanstack/ai-isolate-cloudflare0.2.37 → 0.2.38Dependent
      @tanstack/ai-mcp0.2.4 → 0.2.5Dependent
      @tanstack/ai-vue-ui0.2.32 → 0.2.33Dependent
      @tanstack/preact-ai-devtools0.1.66 → 0.1.67Dependent
      @tanstack/react-ai-devtools0.2.66 → 0.2.67Dependent
      @tanstack/solid-ai-devtools0.2.66 → 0.2.67Dependent
    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 090f86d2c12fe63221e2f787985e8877cac1a1b8

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded2m 10sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-07-17 06:58:31 UTC

    4. socket-security[bot](GitHub profile, opens in a new tab)commented

      Review the following changes in direct dependencies. Learn more about Socket for GitHub.

      Diff Package Supply Chain
      Security
      Vulnerability Quality Maintenance License
      Addedprisma@​6.19.3761009896100
      Addedfake-indexeddb@​6.2.510010010084100
      Added@​prisma/​client@​6.19.3941008598100
      Added@​ag-ui/​core@​0.0.571001008796100
      Addeddrizzle-kit@​0.31.10991009697100

      View full report

    5. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @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@785

      commit: 090f86d

    6. Awesome to see that this is in progress, it was one of the final bits that prevented it from feeling completely batteries included!

    7. AlemTuzlak changed the title

    8. 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 lift

      Defer tool-call terminals before middleware side effects run.

      runOnChunk() runs before shouldDeferToolCallRunFinished(), so persistence/devtools middleware can record the original RUN_FINISHED even when discardDeferredToolCallRunFinishedChunks() 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 win

      Same stale-client race as the Preact binding.

      sendMessage/append/reload/clear/addToolApprovalResponse/resume/resumeInterrupts call syncResumeState(client) from closures bound to whatever client was active when the callback was created. If client is recreated (e.g., id prop changes) while one of these is in-flight, the stale closure's finally can overwrite resumeState/pendingInterrupts with data from the disposed client — the same hazard the activeClientRef.current !== instance guard on the other ChatClient callbacks (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 win

      Stale-client race when syncing resume state after async operations.

      sendMessage/append/reload/clear/addToolApprovalResponse/resume/resumeInterrupts call syncResumeState(client) where client is the closure-captured memoized instance. If clientId changes (client recreated) while one of these is in flight, the old closure still resolves and overwrites resumeState/pendingInterrupts with data from the now-inactive client — the exact scenario the activeClientRef.current !== instance guard on the other ChatClient callbacks (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/core 0.0.57 changes EventType

      THINKING_* events are deprecated in favor of REASONING_*, so the EventType consumers in packages/ai/src/types.ts and packages/ai/src/activities/summarize/chat-stream-summarize.ts need 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 win

      Add a language tag to the pseudocode fence.

      The block at Line 226 is untyped, which trips markdownlint MD040. Mark it as text so 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 win

      Handle 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 win

      Fix require-yield lint 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 win

      Fix import ordering/sorting per ESLint.

      ./test-utils should be imported before the type import of ../src/resume, and createMockAdapter should 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 win

      Assert the tool result exists before checking ordering.

      findIndex() returns -1 when no TOOL_CALL_RESULT is emitted, and any real runFinishedIndex will 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 win

      Remove the unnecessary stream cast.

      ESLint flags Line 495 as an unnecessary type assertion; collectChunks already 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 win

      Fix import order (ESLint sort-imports).

      collectChunks is not alphabetically ordered relative to createMockAdapter.

      🔧 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 win

      Prefer the tool call's owner message over "active" message when resolving approval target.

      resolvedMessageId picks getActiveAssistantMessageId() first and only falls back to toolCallToMessage.get(toolCallId). Elsewhere in this same file, the ui-resource CUSTOM 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 a RUN_FINISHED interrupts batch contains tool calls owned by a message other than the currently "active" one, updateToolCallApproval will patch the wrong message. The onApprovalRequest callback itself still carries the correct toolCallId/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 win

      Fix import order per ESLint sort-imports.

      fetcherToConnectionAdapter is listed before fetchServerSentEvents, but the rule sorts case-sensitively (uppercase S < lowercase e), so fetchServerSentEvents should 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 win

      resume/resumeInterrupts skip the resync-on-error safeguard used elsewhere in this file.

      sendMessage, append, and reload were just wrapped in try/finally specifically so syncResumeState() still runs when the underlying call throws. resume and resumeInterrupts — the two methods most likely to encounter a failed reconnect/replay — don't get the same treatment, so a rejected client().resume(...)/resumeInterrupts(...) call leaves resumeState/pendingInterrupts stale.

      ♻️ 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 win

      Block image auto-send while an interrupt is pending.

      The new guard only covers handleSubmit; the file input path bypasses it and calls onSendMessageWithImage directly. If showImageInput and hasPendingInterrupt are 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 win

      Sync resume refs on failed resume attempts too.

      Line 271 and Line 280 only call syncResumeState() after a successful await, unlike sendMessage/append/reload. If client.resume() rejects, Vue state can keep stale resumeState/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 win

      Split and reorder the persistence import to satisfy lint.

      ESLint reports import/order, sort-imports, and import/consistent-type-specifier-style on 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 win

      Merge 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 win

      onError can overwrite an already-interrupted run with 'failed'.

      onFinish (Line 420) explicitly guards if (runState.get(ctx)?.interrupted) return before marking a run 'completed', but onError has no equivalent guard — if an error is thrown after onChunk already persisted the interrupt outcome and set runState.interrupted = true (Lines 388-414), onError would 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 win

      Fix 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 win

      Fix 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 win

      Fix import order per ESLint.

      Static analysis flags node:sqlite import ordering before vitest, and ../src/index before the @tanstack/ai type 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 win

      Doc gap: cursor+resume interaction not documented.

      Per resume-seam.test.ts, when both cursor and resume are supplied, replay is skipped and the adapter runs normally (interrupt-resume takes precedence over durable replay). This important interaction isn't captured in the cursor doc 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 value

      Interrupts without toolCallId are silently dropped.

      Per the AG-UI spec toolCallId is optional on Interrupt, so future/non-tool interrupt reasons could legitimately omit it. Right now they're silently skipped with no observability, unlike the ui-resource handler which console.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 win

      Duplicated terminal-clearing condition risks future desync.

      The 5-flag boolean (isRunlessSessionError || isTrackedRunTerminal || isPendingInterruptRunTerminal || isCurrentRunTerminal || isCurrentStreamTerminal) is computed twice — once to null lastResume, once to clear pendingInterrupts/pendingInterruptRunId/pendingInterruptResumeItems. These two pieces of state must always change together (per the guard in sendMessage/append that 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 value

      Redundant ?? undefined.

      client.resume(state ?? undefined) — state is already ChatResumeState | 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 win

      Tighten featureRequirements value 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 the as cast.

      🤖 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 | 🔵 Trivial

      Interrupt "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 win

      create() isn't idempotent against overwriting existing interrupts.

      create unconditionally overwrites any existing record for interruptId. If it's ever called twice for the same id (e.g. a redelivered chunk after resolve/cancel already ran), an already-resolved interrupt would silently revert to pending, losing resolvedAt/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 value

      LegacyEventLog type not exported despite being part of the public SqlPersistence shape.

      SqlPersistence.events (see sql-persistence.ts) is typed LegacyEventLog, but this barrel only exports SqlPersistence/SqlPersistenceOptions, not LegacyEventLog itself. Consumers can still use .events structurally, 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 win

      Avoid this binding inside the returned store object.

      createOrResume calls this.get(...), relying on the method being invoked with the store object as receiver. Every other store in this file uses closures instead of this. 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 win

      Fix import order flagged by ESLint.

      import type { StreamChunk } from '@tanstack/ai' should come after the ./sqlite-driver import per the import/order rule.

      🔧 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 win

      Consider adding indexes for interrupts thread/run lookups.

      interrupts is queried by thread_id (list/listPending/decisionsForThread) and run_id (listByRun/listPendingByRun) in stores.ts, but the table only has a PK on interrupt_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 value

      Unnecessary type assertion.

      createSqlPersistence(...) already returns a value structurally assignable to CloudflarePersistence (an intersection with an optional locks field), so the as CloudflarePersistence cast 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 value

      Minor 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 value

      Minor static-analysis nits: import order and unnecessary assertion.

      ESLint flags the @tanstack/ai type import ordering relative to ../src/index (Line 3) and an unnecessary non-null assertion on p.events! at Line 61 (the createResumeSource call 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 value

      peerDependencies use workspace:^ instead of workspace:*.

      As per coding guidelines, "Use the workspace:* protocol for internal package dependencies in package.json," but the peerDependencies here use workspace:^. 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

      @@ -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 driver
      62+}
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🗄️ 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 -n

      Repository: 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:


      🌐 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:


      🌐 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:


      sqliteDriver.transaction() has no transaction control and can corrupt data on failures.

      The current implementation transaction(fn) { return fn(driver) } never issues BEGIN/COMMIT/ROLLBACK statements. Unlike pgDriver (constrained by connection pooling), a SQLite client is single-threaded and synchronous (better-sqlite3 or node:sqlite), making real transactional semantics feasible via manual client.prepare('BEGIN').run() / COMMIT / ROLLBACK.

      Without this, a failure partway through a multi-statement operation leaves partial state persisted. Since the transaction method is part of the SqlDriver interface and expected to return Promise<T>, it must be async. Both better-sqlite3 (via manual prepare/run) and node: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 change
      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 driver
      62-}
      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 result
      63+ } catch (err) {
      64+ client.prepare('ROLLBACK').run()
      65+ throw err
      66+ }
      67+ },
      68+ }
      69+ return driver
      70+}
      🤖 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: RunStore
      46+ /** @deprecated Use stores.publicEvents. */
      47+ events: LegacyEventLog
      48+ /** @deprecated Use stores.interrupts. */
      49+ approvals: ApprovalStore
      50+ /** @deprecated Use feature-based configuration instead of mode. */
      51+ mode: PersistenceMode
      52+}
      53+
      54+export function createSqlPersistence(
      55+ driver: SqlDriver,
      56+ opts?: SqlPersistenceOptions,
      57+): SqlPersistence {
      58+ const shouldMigrate = opts?.migrate ?? true
      59+
      60+ let migrated: Promise<void> | undefined
      61+ const ensureSchema = (): Promise<void> => {
      62+ if (!shouldMigrate) return Promise.resolve()
      63+ migrated ??= runMigrations(driver) // runs on the RAW driver — no recursion
      64+ return migrated
      65+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

      Rejected migration promise is cached forever — no retry possible.

      migrated ??= runMigrations(driver) only skips re-running migration when migrated is nullish. If runMigrations rejects, migrated becomes a rejected Promise, 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 subsequent exec/query/transaction call 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 change
      60- let migrated: Promise<void> | undefined
      61- const ensureSchema = (): Promise<void> => {
      62- if (!shouldMigrate) return Promise.resolve()
      63- migrated ??= runMigrations(driver) // runs on the RAW driver — no recursion
      64- return migrated
      65- }
      60+ let migrated: Promise<void> | undefined
      61+ 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 call
      66+ throw err
      67+ })
      68+ }
      69+ return migrated
      70+ }
      🤖 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+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

      peerDependencies uses workspace:^ instead of workspace:*.

      devDependencies correctly uses workspace:* for @tanstack/ai, but peerDependencies uses workspace:^, 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 change
      49+ "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 true
      262+}
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

      Consolidate duplicated cursor-validation logic with resume-source.ts.

      publicEventExistsAtSeq (Lines 191-202) and the bulk of validateReplayCursor's checks (decode, seq < 1, runId match, latestSeq bound, event-existence) are re-implemented almost verbatim in packages/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 checks run.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 in middleware.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+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

      peerDependencies use workspace:^ instead of workspace:*.

      devDependencies correctly use workspace:*, but peerDependencies use workspace:^. As per coding guidelines, **/package.json should "Use the workspace:* 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 change
      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- }
      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> | undefined
      32+ 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 NULL
      42+ )`,
      43+ )
      44+ return ready
      45+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

      Lazy schema init permanently fails after a transient error.

      ready ??= driver.exec(...) caches the promise once, but if the initial CREATE TABLE rejects (e.g. transient connectivity), ready stays a rejected promise forever — every subsequent get/upsert/delete call 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 change
      31- let ready: Promise<void> | undefined
      32- 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 NULL
      42- )`,
      43- )
      44- return ready
      45- }
      31+ let ready: Promise<void> | undefined
      32+ const ensure = (): Promise<void> => {
      33+ if (!ready) {
      34+ ready = driver
      35+ .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 NULL
      44+ )`,
      45+ )
      46+ .catch((err) => {
      47+ ready = undefined
      48+ throw err
      49+ })
      50+ }
      51+ return ready
      52+ }
      🤖 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 @@
      13941468
      1395- 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+ )
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

      Use the active run ID for pending-tool interrupt terminals.

      In checkForPendingToolCalls(), this interrupt terminal is built from createSyntheticFinishedEvent(), which currently assigns runId: this.createId('pending'). Now that this is emitted as a public RUN_FINISHED, it can produce a terminal event for a run that never had a matching RUN_STARTED and does not match middlewareCtx.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 a
      36+ * single process; multi-instance correctness needs a distributed lock from the
      37+ * 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 run
      55+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

      Clean up idle lock chains.

      chains retains 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 change
      48+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 run
      67+ }
      🤖 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 })
      312407
      408+ useEffect(() => {
      409+ if (!serverPersistenceEnabled || attemptedStoredResumeRef.current) return
      410+ attemptedStoredResumeRef.current = true
      411+ if (!initialResumeSnapshot) return
      412+ if ((initialResumeSnapshot.pendingInterrupts ?? []).length > 0) return
      413+ try {
      414+ void resume(initialResumeSnapshot.resumeState)
      415+ } catch {
      416+ window.localStorage.removeItem(resumeStorageKey)
      417+ }
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

      Catch the rejected resume promise.

      Line 414 discards the promise, so the catch block on Line 415 will not run if resume() 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 change
      413- 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.
    9. 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 win

      Fix import order (ESLint import/order).

      The relative import of ../lib/server-fns should 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 win

      Extract the shared textPrompt helper into examples/ts-react-chat/src/lib — the same text-only prompt normalization is duplicated in examples/ts-react-chat/src/routes/generations.image.tsx and examples/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 win

      Duplicate textPrompt helper across generation routes.

      The identical textPrompt implementation also exists in generations.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 win

      Fragile implicit-lock pattern for single-producer-per-run guarantee.

      ensureProducer relies on there being no await between re-reading activeRuns.get(runId) after the existing.catch() await and the subsequent activeRuns.set(runId, promise) call — that's what currently prevents two concurrent resume requests for the same runId (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 logging await or refactoring) since there's no comment documenting the invariant. Also, activeRuns.delete(runId) right before the unconditional activeRuns.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.yaml is excluded by !**/pnpm-lock.yaml
      📒 Files selected for processing (20)
      • .changeset/persistence-layer.md
      • examples/ts-react-chat/.env.example
      • examples/ts-react-chat/README.md
      • examples/ts-react-chat/package.json
      • examples/ts-react-chat/src/components/Header.tsx
      • examples/ts-react-chat/src/lib/mysql-persistence.test.ts
      • examples/ts-react-chat/src/lib/mysql-persistence.ts
      • examples/ts-react-chat/src/routeTree.gen.ts
      • examples/ts-react-chat/src/routes/api.mysql-persistent-chat.ts
      • examples/ts-react-chat/src/routes/generations.image.tsx
      • examples/ts-react-chat/src/routes/generations.structured-output.tsx
      • examples/ts-react-chat/src/routes/generations.video.tsx
      • examples/ts-react-chat/src/routes/index.tsx
      • examples/ts-react-chat/src/routes/mysql-persistence.tsx
      • packages/ai-persistence-sql/package.json
      • packages/ai-persistence-sql/src/driver.ts
      • packages/ai-persistence-sql/src/index.ts
      • packages/ai-persistence-sql/src/migrations.ts
      • packages/ai-persistence-sql/src/stores.ts
      • packages/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-keys
      33 OPENAI_API_KEY=
      44
      5+# 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_chat
      8+MYSQL_HOST=127.0.0.1
      9+MYSQL_PORT=3306
      10+MYSQL_USER=root
      11+MYSQL_PASSWORD=
      12+MYSQL_DATABASE=tanstack_ai_chat
      13+
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

      Reorder the MYSQL_* keys.

      dotenv-linter is 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

    10. Actionable comments posted: 2

      🧹 Nitpick comments (1)
      packages/ai-persistence-cloudflare/tests/d1.test.ts (1)

      88-96: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

      FakeR2Bucket.list never exercises cursor pagination.

      list hardcodes truncated: false and ignores cursor, so the store's do…while pagination loop (src list) only ever iterates once. The multi-page cursor path is untested. Consider a fake that paginates (e.g., chunked results with a real cursor) 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.md
      • docs/config.json
      • docs/persistence/overview.md
      • packages/ai-persistence-cloudflare/src/index.ts
      • packages/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
    11. Actionable comments posted: 1

      ♻️ Duplicate comments (1)
      packages/ai-persistence-cloudflare/src/index.ts (1)

      606-686: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

      Durable Object lock fetch calls 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 /renew and /release fetch 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 win

      Possibly-unnecessary type assertion.

      ESLint flags as CloudflarePersistence here as unnecessary. Since every field CloudflarePersistence adds beyond SqlPersistence (stores.locks, stores.artifacts, stores.blobs, deprecated locks/artifacts) is optional, the SqlPersistence return value from createSqlPersistence may already satisfy CloudflarePersistence structurally 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 win

      Non-null assertion flagged by lint; can be avoided.

      this.blobs.get(key)!.record (Line 484) is functionally safe since key comes from the same map's keys(), but trips no-non-null-assertion (error level per static analysis). Mapping over entries() 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 win

      Fix 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.md
      • docs/persistence/overview.md
      • packages/ai-persistence-cloudflare/src/index.ts
      • packages/ai-persistence-cloudflare/tests/d1.test.ts
      • packages/ai-persistence/src/index.ts
      • packages/ai-persistence/src/memory.ts
      • packages/ai-persistence/src/middleware.ts
      • packages/ai-persistence/src/types.ts
      • packages/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
    12. tombeckenham assignedtombeckenham

    13. AlemTuzlak changed the title

    14. tombeckenham reviewed

      @@ -0,0 +12,21 @@
      12+`AIPersistence` stores it needs.
      13+
      14+## Lever 1: no persistence
      15+
      16+Use no persistence when a run can disappear after the request ends.
      17+
      18+```ts
      19+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. This
      29+is enough for one-shot server work, tests, or prototypes where reload recovery
      30+does not matter.
      31+
      32+## Lever 2: client snapshots
      tombeckenhamcommented

      These 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

    15. 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: MessageStore
      74+declare const runs: RunStore
      75+declare const publicEvents: PublicEventStore
      76+declare const internalEvents: InternalEventStore
      77+declare const interrupts: InterruptStore
      78+declare const metadata: MetadataStore
      79+declare const locks: LockStore
      80+declare const artifacts: ArtifactStore
      81+declare const blobs: BlobStore
      tombeckenhamcommented

      It would be useful in this doc to explain what each of these storages is for. A one liner against each

    16. tombeckenham reviewed

      @@ -0,0 +1,21 @@
      1+---
      2+title: Persistence with Drizzle
      3+id: drizzle
      4+---
      5+
      6+Use Drizzle persistence when Drizzle already owns your SQLite or Postgres
      7+database connection and migration workflow. The adapter unwraps Drizzle's
      8+underlying client and exposes the shared SQL-backed `AIPersistence` stores to
      9+`withPersistence(...)`.
      10+
      11+By the end, your Drizzle-backed server can persist chat messages, replay
      12+events, interrupts, metadata, and other SQL-backed stores while keeping schema
      13+deployment in your existing Drizzle workflow.
      14+
      15+## Generate the migration
      16+
      17+Create a Drizzle migration file for your dialect.
      18+
      19+```sh
      20+pnpm exec tanstack-ai-persistence-drizzle --dialect postgres
      21+```
      tombeckenhamcommented

      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

      tombeckenhamcommented

      Also Drizzle now has 2 different migration formats, and different types of migrations that depend on what db is used behind it.

    17. 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 (defaultFeatures enables it whenever a publicEvents store exists — packages/ai-persistence/src/middleware.ts:99-101), and onChunk awaits an append for every AG-UI event — every text delta (middleware.ts:975). D1 has no interactive transactions, so the driver's transaction() 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)

      1. 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.
      2. 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 onChunk awaits inline, the queue backpressures every user's stream.
      3. Free-tier daily writes — minutes. 100k rows/day ÷ ~250 rows/message ≈ 400 messages total.
      4. 10 GB database cap — weeks. ~200-500 B/event; there's no pruning/retention API, and public_events grows forever, including for completed runs whose messages are already durably saved.
      5. Paid monthly write quota — actually fine. ~$1/M rows overage; cost is not the constraint.

      Recommendations

      1. Add appendBatch to PublicEventStore + a buffering decorator. Buffer N events (usually-sync onChunk), flush on count / boundary events (RUN_FINISHED, interrupt outcomes) / terminal drain in onFinish/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-row INSERT on Postgres/MySQL, TransactWriteItems on DynamoDB (note: BatchWriteItem can't carry the CAS condition), pipelined XADD on 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.ts currently throws on beyond-latest. The portable contract should be "resume from min(clientSeq, latestSeq)" — the client harmlessly re-receives a few events it already rendered. This matters most on platforms without waitUntil (Lambda, Cloud Run), where a hard kill can eat a buffered tail.

      3. Offer a waitUntil escape 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, locks becomes redundant), and WebSocket hibernation provides the stream.publish fan-out natively. On RUN_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-backed PublicEventStore with the completion-time flush.

      5. Docs callout in the meantime: features: ['messages', 'interrupts'] disables durable replay while keeping thread persistence — worth stating prominently in docs/persistence/cloudflare.md, since the default silently opts every D1 user into the per-token write path. A retention/pruning story for public_events is needed regardless.

      Off-Cloudflare, for reference: a single Postgres instance absorbs this workload with an order of magnitude of headroom (the ai-persistence-postgres backend 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

    18. socket-security[bot](GitHub profile, opens in a new tab)commented

      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 High
      Obfuscated code: npm drizzle-orm is 90.0% likely obfuscated

      Confidence: 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 High
      Obfuscated code: npm effect is 90.0% likely obfuscated

      Confidence: 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 Medium
      Deprecated by its maintainer: npm @esbuild-kit/core-utils

      Reason: 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 Medium
      Deprecated by its maintainer: npm @esbuild-kit/esm-loader

      Reason: 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.

      View full report

    19. 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

      https://claude.ai/code/session_01A6Arc9bWdLq1aRRnDRCLMt

    20. tombeckenham added labelspersistence

    21. 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 -cloudflare are 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-persistence ships the store contracts and a conformance testkit; per-stack agent skills read your existing ORM config and write a chat-persistence.ts against it.
      • Locks are not a store. AIPersistence.stores accepts exactly four keys and throws on anything else; coordination is wired separately with withLocks.
      • 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.

    22. tombeckenham closed this

Sign in to join this conversation.