-
Notifications
You must be signed in to change notification settings - Fork 312
Readonly state #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Readonly state #610
Conversation
🦋 Changeset detectedLatest commit: 66d1916 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
e426f6f to
d3c190a
Compare
Claude Code ReviewPR #610: Readonly State This PR implements readonly connections, allowing WebSocket clients to receive state updates and call RPC methods without permission to modify state. The implementation is solid with comprehensive test coverage. Issues Found1. SQL Query Performance Issue (packages/agents/src/index.ts:775-795)
isConnectionReadonly(connection: Connection): boolean {
if (this._readonlyConnections.has(connection.id)) {
return true;
}
// This SQL query runs every time for non-readonly connections
const result = this.sql<{ connection_id: string }>`
SELECT connection_id FROM cf_agents_readonly_connections
WHERE connection_id = ${connection.id}
`;
// ...
}Recommendation: Cache negative results (non-readonly connections) as well, or at minimum document the performance characteristics. Consider:
2. Race Condition on Cleanup (packages/agents/src/index.ts:648-675) The this.onClose = (connection: Connection) => {
this._readonlyConnections.delete(connection.id);
this.sql`DELETE FROM cf_agents_readonly_connections WHERE connection_id = ${connection.id}`;
// No await - might not complete before hibernation
};Impact: Low probability but could leave orphaned entries in the database that accumulate over time. Recommendation: If SQL operations should complete before hibernation, consider awaiting or document this tradeoff. Architecture & Testing✅ Proper separation of concerns with server-side and client-side APIs Minor Observations
Overall: Good implementation. Address the SQL performance pattern before merging. |
Adds comprehensive documentation for the new readonly connections feature introduced in PR #610. Key additions: - Server-side methods: shouldConnectionBeReadonly(), setConnectionReadonly(), isConnectionReadonly() - Client-side API: onStateUpdateError callback for error handling - Multiple usage examples covering common scenarios (query params, RBAC, admin dashboards, dynamic permissions) - Implementation details including SQL persistence and hibernation support - Best practices for authentication, user feedback, and access control This feature allows restricting certain WebSocket connections from modifying agent state while still allowing them to receive state updates and call RPC methods. Related PR: cloudflare/agents#610 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Introduces readonly connections to restrict certain WebSocket clients from modifying agent state while allowing state updates and RPC calls. Adds server-side methods for managing readonly status, persists status in SQL for hibernation, and client-side error handling via onStateUpdateError. Updates documentation and relevant types, client, and React hook implementations.
Introduces a new TestReadonlyAgent Durable Object and comprehensive tests for readonly connection behavior, including state update restrictions, RPC permissions, persistence, and cleanup. Updates wrangler config to register the new agent for testing.
Improved type safety in readonly-connections.test.ts by introducing explicit message interfaces and type guards, replacing 'any' with specific types in test helpers and assertions. Also updated TestReadonlyAgent to ignore unused connection parameter in shouldConnectionBeReadonly. These changes enhance code reliability and maintainability in the test suite.
05da9cb to
66d1916
Compare
Synced from cloudflare/agents PR #610 (cloudflare/agents#610) Introduces documentation for the readonly connections feature which allows restricting certain WebSocket connections from modifying Agent state while still allowing them to receive state updates and call RPC methods. Key features documented: - Server-side methods: shouldConnectionBeReadonly, setConnectionReadonly, isConnectionReadonly - Client-side API: onStateUpdateError callback - Usage examples for query parameter based access, role-based access control, admin dashboards, and dynamic permission changes - Behavior details, best practices, and migration guide - Implementation details including persistence across hibernation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
moving to draft, might have a better implementation here |
fixes #255