feat(core): add request context for per-user entity settings#6457
Conversation
Implements AsyncLocalStorage-based request context that allows: - Per-request entity settings that override agent settings - getSetting() now checks request context first - runWithRequestContext() and getRequestContext() APIs This enables multi-tenant runtime sharing where each user's settings (API keys, OAuth tokens) are isolated per-request. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
✨ Finishing touches🧪 Generate unit tests (beta)
Important Action Needed: IP Allowlist UpdateIf your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:
Reviews will stop working after February 8, 2026 if the new IP is not added to your allowlist. 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 |
|
Claude finished @0xbbjoker's task in 1m 23s —— View job Reviewing PR #6457: feat(core): add request context for per-user entity settings
✅ APPROVEDThis is a well-implemented feature that adds request context for per-user entity settings using AsyncLocalStorage. The implementation is clean, follows existing patterns (similar to Summary
What's Good
Minor Suggestions💡 SUGGESTION: Consider adding a browser implementation stub → 💡 SUGGESTION: The // In runtime.ts getSetting():
// - undefined in entitySettings map = fall through to agent settings
// - null in entitySettings map = explicitly unset, return nullCode Quality Notes
The implementation correctly prioritizes entity settings > character settings > character secrets > environment, which aligns with the multi-tenant use case described in the PR. |
|
Claude finished @0xbbjoker's task in 55s —— View job Reviewing PR #6457: feat(core): add request context for per-user entity settings
✅ APPROVEDThis is a well-designed feature that adds request context for per-entity settings using AsyncLocalStorage, enabling multi-tenant use of a shared agent runtime. Summary
What's Good
Implementation Details
Verified
Minor Suggestions💡 SUGGESTION: Consider adding a browser implementation stub ( 💡 SUGGESTION: Consider documenting the null vs undefined behavior inline in Note: Previous review comment mentioned package.json version change to |
Summary
RequestContextusing AsyncLocalStorage to propagate per-request entity settingswithEntityContext(),getRequestContext(),getEntitySettings()Test plan
RequestContextclass🤖 Generated with Claude Code
Note
Medium Risk
Changes
AgentRuntime.getSetting()resolution to prefer request-scoped entity settings (includingnullas an explicit override), which can affect how secrets/config are sourced in multi-tenant flows. Uses global AsyncLocalStorage state in Node, so regressions would show up as mis-scoped settings across concurrent requests if misused.Overview
Introduces a new request-scoped
RequestContextAPI (runWithRequestContext,getRequestContext, and a pluggableIRequestContextManager) to propagate per-entity settings through async execution.Adds a Node.js implementation backed by
AsyncLocalStorageand initializes it fromindex.node.ts, while exporting the new utilities from bothindex.tsandindex.node.ts.Updates
AgentRuntime.getSetting()to checkrequestCtx.entitySettingsfirst (treatingundefinedas fallthrough andnullas an explicit return) and return entity values withoutdecryptSecret, with new unit/integration tests covering async propagation, concurrency isolation, and precedence over character settings/secrets.Written by Cursor Bugbot for commit a0e2d2d. This will update automatically on new commits. Configure here.
Greptile Overview
Greptile Summary
This PR adds
RequestContextinfrastructure to enable per-entity settings in multi-tenant deployments, allowing multiple users to share a single agent runtime while maintaining isolated settings (API keys, OAuth tokens, etc.).Key Changes:
RequestContextinterface andrunWithRequestContext()API for propagating per-request entity settingsrequest-context.node.ts) ensuring proper async context isolationruntime.getSetting()to check request context first before falling back to character settingsDesign:
streaming-context.ts)Testing:
getSetting()behavior and priorityIssue Found:
package.jsonversion changed to local development version (1.0.0-local.1768325621) - must be reverted before mergeConfidence Score: 4/5
packages/core/package.jsonneeds version reverted to 1.7.2-alpha.1Important Files Changed
Sequence Diagram
sequenceDiagram participant User as User/Entity participant Server as Server/Handler participant RC as RequestContext participant ALS as AsyncLocalStorage participant Runtime as AgentRuntime participant DB as Database User->>Server: Send message/request Note over Server: Fetch entity settings Server->>DB: Query entity settings for userId DB-->>Server: Return settings Map Server->>RC: runWithRequestContext(context, fn) Note over RC: context = {entityId, agentId, entitySettings} RC->>ALS: storage.run(context, fn) Note over ALS: Store context in AsyncLocalStorage ALS->>Runtime: Execute fn() → runtime.handleMessage() Runtime->>Runtime: getSetting(key) Runtime->>RC: getRequestContext() RC->>ALS: storage.getStore() ALS-->>RC: Return active context RC-->>Runtime: Return context alt Entity setting exists Runtime-->>Runtime: Return entitySettings.get(key) Note over Runtime: Pre-decrypted, return as-is else Fallback to character settings Runtime-->>Runtime: Check character.settings/secrets Note over Runtime: Apply decryption if needed end Runtime-->>Server: Complete message processing Server-->>User: Send response Note over ALS: Context automatically cleared after fn() completes