Skip to content
Source
Sign inSign up
TanStack/ai

feat(ai): add shared Scope identity type (precursor for persistence + memory) #980

Mergedtombeckenhammerged 1 commit intomain fromrefs/pull/980/headmerged by AlemTuzlak View on GitHub

tombeckenhamcommented

🎯 Changes

Adds a single shared Scope identity type to @tanstack/ai — the one identity/isolation vocabulary for the subsystems that persist or recall per-conversation data: @tanstack/ai-persistence (#785) and @tanstack/ai-memory (#541).

interface Scope {
  threadId: string   // required — the single conversation key (same as ctx.threadId)
  userId?: string    // durable end-user identity; required in practice for multi-user apps
  tenantId?: string  // multi-tenant boundary
  namespace?: string // reserved logical partition; no subsystem keys on it yet
}

Why this is its own PR, merged first. The two in-flight subsystems currently diverge on identity:

  • memory invented MemoryScope = { sessionId, userId? } — sessionId is a second name for the conversation key.
  • persistence keys on a bare threadId: string and a separate flat scope: string (metadata store), with no userId/tenantId at all.

threadId is already the single conversation key across the codebase (ChatMiddlewareContext.threadId, with conversationId deprecated in favor of it). Public identity surface is irreversible after release, so pinning it once — here, ahead of both PRs — lets #785 and #541 rebase onto one settled contract instead of racing to set the vocabulary. Persistence drops its flat scope: string/bare threadId; memory renames sessionId → threadId and inherits userId/tenantId/namespace.

Security. Every field is an isolation boundary and must be derived server-side from trusted session state, never from client input — a bare client threadId is not an authorization boundary. This is documented on the type.

Scope of this PR. Additive and inert: Scope is exported from the package root but nothing consumes it yet. No behavior change, no other subsystem touched.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally (narrow gate: test:types, test:oxlint, build, test:knip for @tanstack/ai).

🚀 Release Impact

  • This change affects published code, and I have generated a changeset (@tanstack/ai minor).
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • Added a shared Scope type for consistently identifying and isolating conversation data.
    • Supports conversation, user, tenant, and namespace identifiers, with conversation ID required.
    • Exported the type for use across TanStack AI integrations.
  • Documentation

    • Documented server-side derivation requirements and compatibility guidance for unsupported namespace handling.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      📝 Walkthrough

      Walkthrough

      Adds a shared Scope interface for conversation identity and isolation, exports it from @tanstack/ai, and documents the additive minor release. The type includes required threadId plus optional user, tenant, and namespace fields.

      Changes

      Shared Scope contract

      Layer / File(s)Summary
      Define and export Scope
      packages/ai/src/scope.ts, packages/ai/src/index.ts, .changeset/shared-scope.md
      Defines the documented Scope interface, re-exports it from the package root, and adds a minor release note describing its field contract and current unconsumed status.

      Estimated code review effort: 1 (Trivial) | ~5 minutes

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely matches the main change: adding a shared Scope identity type for persistence and memory.
      Description check✅ PassedThe description follows the required template and includes the changes, checklist, and release impact sections with concrete details.
      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.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `feat/core-scope`

      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. 🚀 Changeset Version Preview

      3 package(s) bumped directly, 44 bumped as dependents.

      🟥 Major bumps

      PackageVersionReason
      @tanstack/ai-durable-stream0.0.0 → 1.0.0Changeset
      @tanstack/ai-acp0.2.3 → 1.0.0Dependent
      @tanstack/ai-angular0.3.1 → 1.0.0Dependent
      @tanstack/ai-anthropic0.16.3 → 1.0.0Dependent
      @tanstack/ai-bedrock0.1.4 → 1.0.0Dependent
      @tanstack/ai-claude-code0.2.3 → 1.0.0Dependent
      @tanstack/ai-code-mode0.3.8 → 1.0.0Dependent
      @tanstack/ai-code-mode-skills0.3.11 → 1.0.0Dependent
      @tanstack/ai-codex0.2.3 → 1.0.0Dependent
      @tanstack/ai-elevenlabs0.2.34 → 1.0.0Dependent
      @tanstack/ai-fal0.9.12 → 1.0.0Dependent
      @tanstack/ai-gemini0.20.1 → 1.0.0Dependent
      @tanstack/ai-grok0.14.9 → 1.0.0Dependent
      @tanstack/ai-grok-build0.2.3 → 1.0.0Dependent
      @tanstack/ai-groq0.5.3 → 1.0.0Dependent
      @tanstack/ai-isolate-node0.1.47 → 1.0.0Dependent
      @tanstack/ai-isolate-quickjs0.1.47 → 1.0.0Dependent
      @tanstack/ai-mistral0.2.3 → 1.0.0Dependent
      @tanstack/ai-ollama0.8.16 → 1.0.0Dependent
      @tanstack/ai-openai0.17.1 → 1.0.0Dependent
      @tanstack/ai-opencode0.2.3 → 1.0.0Dependent
      @tanstack/ai-openrouter0.15.10 → 1.0.0Dependent
      @tanstack/ai-preact0.11.1 → 1.0.0Dependent
      @tanstack/ai-react0.18.1 → 1.0.0Dependent
      @tanstack/ai-react-ui0.8.15 → 1.0.0Dependent
      @tanstack/ai-sandbox0.2.4 → 1.0.0Dependent
      @tanstack/ai-sandbox-cloudflare0.2.4 → 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-solid0.15.1 → 1.0.0Dependent
      @tanstack/ai-solid-ui0.7.14 → 1.0.0Dependent
      @tanstack/ai-svelte0.15.1 → 1.0.0Dependent
      @tanstack/ai-vue0.15.1 → 1.0.0Dependent
      @tanstack/openai-base0.9.9 → 1.0.0Dependent

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/ai0.42.0 → 0.43.0Changeset
      @tanstack/ai-client0.22.1 → 0.23.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/ai-devtools-core0.4.24 → 0.4.25Dependent
      @tanstack/ai-isolate-cloudflare0.2.38 → 0.2.39Dependent
      @tanstack/ai-mcp0.2.5 → 0.2.6Dependent
      @tanstack/ai-vue-ui0.2.34 → 0.2.35Dependent
      @tanstack/preact-ai-devtools0.1.67 → 0.1.68Dependent
      @tanstack/react-ai-devtools0.2.67 → 0.2.68Dependent
      @tanstack/solid-ai-devtools0.2.67 → 0.2.68Dependent
      ag-ui0.0.2 → 0.0.3Dependent
    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 35070e7a9a631e92557014b8f5461177ced3ca24

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded1m 44sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-07-22 06:40:15 UTC

    4. Actionable comments posted: 1

      🤖 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/src/scope.ts`:
      - Around line 41-46: The Scope namespace contract currently permits adapters to
      silently ignore a potentially isolating boundary. Update the namespace
      documentation and related Scope handling so unsupported non-empty namespace
      values are rejected by adapters, or explicitly define namespace as having no
      isolation semantics until implementation support exists; preserve behavior for
      omitted or empty namespaces.
      🪄 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: dd514cf4-abde-4639-ad02-98d04fba793b

      📥 Commits

      Reviewing files that changed from the base of the PR and between bcb1b939c46c4ea58b288a262ac844827fe209cc and 35070e7a9a631e92557014b8f5461177ced3ca24.

      📒 Files selected for processing (3)
      • .changeset/shared-scope.md
      • packages/ai/src/index.ts
      • packages/ai/src/scope.ts
      @@ -0,0 +26,21 @@
      26+ * key both subsystems already center on. Same concept as
      27+ * `ChatMiddlewareContext.threadId`.
      28+ */
      29+ threadId: string
      30+ /**
      31+ * Durable end-user identity, for cross-thread recall and per-user isolation.
      32+ * Optional, but required in practice for any multi-user deployment — a
      33+ * `threadId` alone is not an authorization boundary (see Security above).
      34+ */
      35+ userId?: string
      36+ /**
      37+ * Tenant/organization boundary for multi-tenant deployments. When present,
      38+ * every read and write must be confined to it.
      39+ */
      40+ tenantId?: string
      41+ /**
      42+ * Logical partition within a tenant/user (e.g. separating distinct memory
      43+ * banks or persistence namespaces). Reserved — no subsystem keys on it yet;
      44+ * adapters that don't understand it must ignore it rather than error.
      45+ */
      46+ namespace?: string
      coderabbitai[bot](GitHub profile, opens in a new tab)commented

      🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

      Do not silently ignore an unsupported isolation boundary.

      If namespace separates memory or persistence partitions, an adapter that ignores it can return data from another namespace. Require adapters to reject unsupported non-empty namespaces, or explicitly state that namespace has no isolation semantics until supported.

      🤖 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/scope.ts` around lines 41 - 46, The Scope namespace contract
      currently permits adapters to silently ignore a potentially isolating boundary.
      Update the namespace documentation and related Scope handling so unsupported
      non-empty namespace values are rejected by adapters, or explicitly define
      namespace as having no isolation semantics until implementation support exists;
      preserve behavior for omitted or empty namespaces.
    5. Open in StackBlitz

      @tanstack/ai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@980

      @tanstack/ai-acp

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-acp@980

      @tanstack/ai-angular

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-angular@980

      @tanstack/ai-anthropic

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@980

      @tanstack/ai-bedrock

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-bedrock@980

      @tanstack/ai-claude-code

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-claude-code@980

      @tanstack/ai-client

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@980

      @tanstack/ai-code-mode

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode@980

      @tanstack/ai-code-mode-skills

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-code-mode-skills@980

      @tanstack/ai-codex

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-codex@980

      @tanstack/ai-devtools-core

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@980

      @tanstack/ai-durable-stream

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-durable-stream@980

      @tanstack/ai-elevenlabs

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-elevenlabs@980

      @tanstack/ai-event-client

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-event-client@980

      @tanstack/ai-fal

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-fal@980

      @tanstack/ai-gemini

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@980

      @tanstack/ai-grok

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok@980

      @tanstack/ai-grok-build

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-grok-build@980

      @tanstack/ai-groq

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-groq@980

      @tanstack/ai-isolate-cloudflare

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-cloudflare@980

      @tanstack/ai-isolate-node

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-node@980

      @tanstack/ai-isolate-quickjs

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-isolate-quickjs@980

      @tanstack/ai-mcp

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mcp@980

      @tanstack/ai-mistral

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-mistral@980

      @tanstack/ai-ollama

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@980

      @tanstack/ai-openai

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@980

      @tanstack/ai-opencode

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-opencode@980

      @tanstack/ai-openrouter

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openrouter@980

      @tanstack/ai-preact

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-preact@980

      @tanstack/ai-react

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@980

      @tanstack/ai-react-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@980

      @tanstack/ai-sandbox

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox@980

      @tanstack/ai-sandbox-cloudflare

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-cloudflare@980

      @tanstack/ai-sandbox-daytona

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-daytona@980

      @tanstack/ai-sandbox-docker

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-docker@980

      @tanstack/ai-sandbox-local-process

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-local-process@980

      @tanstack/ai-sandbox-sprites

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-sprites@980

      @tanstack/ai-sandbox-vercel

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-sandbox-vercel@980

      @tanstack/ai-solid

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@980

      @tanstack/ai-solid-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@980

      @tanstack/ai-svelte

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-svelte@980

      @tanstack/ai-utils

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-utils@980

      @tanstack/ai-vue

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@980

      @tanstack/ai-vue-ui

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@980

      @tanstack/openai-base

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/openai-base@980

      @tanstack/preact-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/preact-ai-devtools@980

      @tanstack/react-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@980

      @tanstack/solid-ai-devtools

      npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@980

      commit: 35070e7

    6. AlemTuzlak approved these changes

    7. AlemTuzlak merged this4ce7600

    8. AlemTuzlak closed this

Sign in to join this conversation.