feat(ai): add shared Scope identity type (precursor for persistence + memory)#980
Conversation
Introduce a single identity/isolation vocabulary (Scope) in @tanstack/ai for the subsystems that persist or recall per-conversation data — ai-persistence and ai-memory. threadId is the one conversation key (matching ChatMiddlewareContext.threadId); userId/tenantId/namespace are optional. Every field is an isolation boundary derived server-side, never from client input. Additive precursor: nothing consumes Scope yet. Landed ahead of the persistence (#785) and memory (#541) PRs so both build on one settled identity contract instead of diverging (sessionId vs threadId, bare threadId vs flat scope:string).
📝 WalkthroughWalkthroughAdds a shared ChangesShared Scope contract
Estimated code review effort: 1 (Trivial) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🚀 Changeset Version Preview3 package(s) bumped directly, 44 bumped as dependents. 🟥 Major bumps
🟨 Minor bumps
🟩 Patch bumps
|
|
View your CI Pipeline Execution ↗ for commit 35070e7
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
.changeset/shared-scope.mdpackages/ai/src/index.tspackages/ai/src/scope.ts
| /** | ||
| * Logical partition within a tenant/user (e.g. separating distinct memory | ||
| * banks or persistence namespaces). Reserved — no subsystem keys on it yet; | ||
| * adapters that don't understand it must ignore it rather than error. | ||
| */ | ||
| namespace?: string |
There was a problem hiding this comment.
🔒 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.
@tanstack/ai
@tanstack/ai-acp
@tanstack/ai-angular
@tanstack/ai-anthropic
@tanstack/ai-bedrock
@tanstack/ai-claude-code
@tanstack/ai-client
@tanstack/ai-code-mode
@tanstack/ai-code-mode-skills
@tanstack/ai-codex
@tanstack/ai-devtools-core
@tanstack/ai-durable-stream
@tanstack/ai-elevenlabs
@tanstack/ai-event-client
@tanstack/ai-fal
@tanstack/ai-gemini
@tanstack/ai-grok
@tanstack/ai-grok-build
@tanstack/ai-groq
@tanstack/ai-isolate-cloudflare
@tanstack/ai-isolate-node
@tanstack/ai-isolate-quickjs
@tanstack/ai-mcp
@tanstack/ai-mistral
@tanstack/ai-ollama
@tanstack/ai-openai
@tanstack/ai-opencode
@tanstack/ai-openrouter
@tanstack/ai-preact
@tanstack/ai-react
@tanstack/ai-react-ui
@tanstack/ai-sandbox
@tanstack/ai-sandbox-cloudflare
@tanstack/ai-sandbox-daytona
@tanstack/ai-sandbox-docker
@tanstack/ai-sandbox-local-process
@tanstack/ai-sandbox-sprites
@tanstack/ai-sandbox-vercel
@tanstack/ai-solid
@tanstack/ai-solid-ui
@tanstack/ai-svelte
@tanstack/ai-utils
@tanstack/ai-vue
@tanstack/ai-vue-ui
@tanstack/openai-base
@tanstack/preact-ai-devtools
@tanstack/react-ai-devtools
@tanstack/solid-ai-devtools
commit: |
🎯 Changes
Adds a single shared
Scopeidentity 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).Why this is its own PR, merged first. The two in-flight subsystems currently diverge on identity:
MemoryScope = { sessionId, userId? }—sessionIdis a second name for the conversation key.threadId: stringand a separate flatscope: string(metadata store), with nouserId/tenantIdat all.threadIdis already the single conversation key across the codebase (ChatMiddlewareContext.threadId, withconversationIddeprecated 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 flatscope: string/barethreadId; memory renamessessionId → threadIdand inheritsuserId/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
threadIdis not an authorization boundary. This is documented on the type.Scope of this PR. Additive and inert:
Scopeis exported from the package root but nothing consumes it yet. No behavior change, no other subsystem touched.✅ Checklist
test:types,test:oxlint,build,test:knipfor@tanstack/ai).🚀 Release Impact
@tanstack/aiminor).Summary by CodeRabbit
New Features
Scopetype for consistently identifying and isolating conversation data.Documentation