You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Provide comprehensive feedback and recommendations
✅ Summary
This PR implements anonymous user sessions using Better Auth's anonymous plugin, enabling public-facing widgets to provide stable user identity without requiring authentication. The implementation follows a well-designed dual authentication pattern: API Key (for project scope) + Session Token (for user scope).
🎯 Strengths
Excellent Architecture
Clear separation of concerns: Run API allows anonymous sessions, Manage API rejects them
Dual auth pattern: API key for project context + session token for user identity is elegant
Comprehensive spec documentation: The anonymous-user-tokens.md spec is thorough and well-structured
Proper security model: Server-managed sessions with cryptographic signing prevent impersonation
Clean Implementation
Middleware design: Clean separation between API key validation and session validation
Principal abstraction: Well-defined Principal type with proper isAnonymous handling
Database schema: Minimal, appropriate addition of isAnonymous boolean field
Development Environment Bypass (agents-run-api/src/middleware/api-key-auth.ts:382-417):
if(isDev){// ... bypass authentication}
Issue: Development mode bypasses session validation entirely, which could hide integration issues. Recommendation: Allow session validation to run in development mode but fall back gracefully on failure.
API Design & Consistency
Inconsistent Auth Header Usage:
Documentation shows both X-API-Key + Authorization: Bearer <session> (new pattern)
Code also supports Authorization: Bearer <api_key> (backward compatibility)
UI examples still use x-api-key header alongside apiKey field
Recommendation:
Standardize on X-API-Key + Authorization: Bearer <session> for new implementations
Missing Error Scenarios: Documentation doesn't cover important error cases:
Session token expired
Rate limiting exceeded
CORS/origin validation failures
Recommendation: Add error handling examples in documentation.
Anonymous User Data Retention: Spec mentions "indefinite retention" but lacks:
Privacy compliance considerations
Data cleanup strategies
GDPR/CCPA implications for anonymous users
Recommendation: Add privacy section to spec addressing data retention policies.
Implementation Gaps
Rate Limiting: No mention of rate limiting for anonymous users, who could potentially abuse the system. Recommendation: Implement per-session or per-IP rate limiting for anonymous users.
Cross-Device Session Sync: Spec mentions each device gets separate sessions, but there's no mechanism for users to link devices or transfer sessions. Recommendation: Consider adding an optional session transfer mechanism for user convenience.
🔧 Minor Issues
Type Safety (agents-run-api/src/types/execution-context.ts:15):
isAnonymous: boolean;
The isAnonymous field is redundant when type already indicates this. Consider removing for cleaner interface.
Logging Consistency: Some logs use distinctId, others use principalId for the same concept. Standardize terminology.
🚀 Future Enhancements
Account Linking: The spec mentions this as future work, but consider how to preserve conversation history when anonymous users decide to create accounts.
Analytics Integration: While distinctId is well-designed for analytics, consider adding standardized analytics events for session lifecycle (created, expired, linked).
✅ Overall Assessment
This is a high-quality implementation of a complex feature. The dual authentication pattern is elegant and secure. The separation between Run API (allows anonymous) and Manage API (requires authentication) is clean and well-implemented.
Primary concerns are around error handling robustness and API consistency. The security model is sound, but error scenarios need more thorough handling.
Recommendation: Address the critical error handling and API consistency issues before merging, but the overall architecture and implementation are excellent.
Definition of Done Check
✅ Anonymous plugin enabled in Better Auth
✅ Database migration for isAnonymous field
✅ Session token validation in Run API middleware
✅ Dual auth enforcement (API key + session)
✅ Principal abstraction implemented
❓ Session refresh handling (needs improvement)
Priority: Fix error handling (#1, #2) and API consistency (#3, #4) before merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.