Skip to content

fix: cleaner tests, better error handling, and protocol improvements#1035

Merged
threepointone merged 2 commits intomainfrom
less-noisy-tests
Mar 1, 2026
Merged

fix: cleaner tests, better error handling, and protocol improvements#1035
threepointone merged 2 commits intomainfrom
less-noisy-tests

Conversation

@threepointone
Copy link
Contributor

Summary

This PR improves error handling, reduces test noise, and fixes several protocol-level issues across the Agent and MCP subsystems.

Changes

Agent core (src/index.ts)

  • this.sql throws SqlError directly — Previously, SQL errors were routed through this.onError(), which logged the error and re-threw it, causing confusing double error logs. Now this.sql wraps failures in SqlError and throws directly. onError is reserved for WebSocket connection errors and unhandled server errors.

  • sendIdentityOnConnect warning rewritten — The warning is now informational rather than a deprecation notice. It only fires when using custom routing (where the instance name is not visible in the URL), and tells the developer the exact name being sent. The old warning fired on every agent class regardless of routing.

MCP subsystem (src/mcp/)

  • JSON-RPC error responses (rpc.ts) — RPCServerTransport.handle() now returns a proper JSON-RPC -32600 Invalid Request error response for malformed messages instead of throwing an unhandled exception. This aligns with the JSON-RPC 2.0 spec.

  • McpAgent protocol message suppression (index.ts) — McpAgent now overrides shouldSendProtocolMessages() to suppress CF_AGENT_IDENTITY, CF_AGENT_STATE, and CF_AGENT_MCP_SERVERS frames on MCP transport connections (detected via the cf-mcp-method header). Regular WebSocket connections to a hybrid McpAgent are unaffected.

  • SSE handler MCP header (utils.ts) — Added MCP_HTTP_METHOD_HEADER to the SSE handler's internal WebSocket upgrade request so shouldSendProtocolMessages can detect SSE-originated MCP connections.

  • CORS warning removed (utils.ts, worker-transport.ts) — Removed the noisy one-time warning about Authorization in Access-Control-Allow-Headers with wildcard origin. The combination is valid for non-credentialed requests and the warning was unhelpful.

Session provider (src/experimental/memory/session/providers/agent.ts)

  • Removed explicit BEGIN TRANSACTION/COMMIT/ROLLBACK — Durable Objects auto-coalesce all synchronous SQL writes within a single I/O gate into one atomic batch, making explicit transactions unnecessary. The old code threw "cannot start a transaction within a transaction" errors because the DO runtime already wraps synchronous writes.

Lifecycle rename

  • onStateUpdateonStateChanged (server-side) — The server-side lifecycle hook has been renamed. The old name still works (backward-compatible shim with a one-time console warning). The client-side onStateUpdate callback in useAgent/AgentClient is unchanged.

Test improvements

  • routing.test.ts — All WebSocket upgrade tests now properly accept and close WebSockets after assertions, eliminating 17 "other end of WebSocketPipe was destroyed" log lines on teardown.
  • state.test.ts — Updated to use onStateChanged naming throughout.
  • rpc.test.ts — Updated to expect JSON-RPC error response instead of thrown exception.
  • Test agents (state.ts, readonly.ts) — Renamed onStateUpdate to onStateChanged.

Docs

  • docs/client-sdk.md — Fixed server-side method reference from onStateUpdate() to onStateChanged().

Changesets

  • Updated security-hardening.md — Rewrote stale deprecation warnings bullet to reflect actual behavior.
  • Created fix-sql-error-handling.md — Documents the SqlError direct-throw change.
  • Created fix-mcp-protocol-handling.md — Documents JSON-RPC error responses, McpAgent protocol suppression, and CORS warning removal.

Test results

All tests pass:

  • routing.test.ts — 25 tests, no WebSocketPipe noise
  • state.test.ts — all state management tests with onStateChanged
  • rpc.test.ts — JSON-RPC error response handling
  • workflow.test.ts — 42 tests (remaining workerd logs are expected miniflare teardown artifacts)
  • email-routing.test.ts — 39 tests

Upgrade several dev dependencies (e.g. @types/node, lint-staged, partyserver and related lockfile updates) and propagate those version bumps across package.json files. Rename the state notification hook used in tests and test agents from onStateUpdate to onStateChanged (including helper names, error messages and test strings). Change Agent warning behavior to only warn about sendIdentityOnConnect when custom routing could leak identity (checks URL path), and move the experimental forever API warning from module top-level to the Agent constructor so it only logs when instantiated.
Multiple fixes and improvements across MCP, agent SQL handling, docs, and tests:

- Return JSON-RPC -32600 error for malformed RPC messages instead of throwing, aligning with JSON-RPC 2.0 and updating RPC transport tests.
- McpAgent now suppresses agent protocol frames (identity/state/MCP servers) for MCP bridge connections by detecting the MCP method header.
- Removed noisy CORS one-time warning about Authorization with wildcard origin.
- Make this.sql throw SqlError directly (includes query) so SQL errors can be caught without double-logging or being swallowed by onError.
- Remove an explicit transaction in the in-memory session provider; rely on Durable Objects' synchronous I/O batching and simplify writes.
- Update sendIdentityOnConnect warning text to clarify when instance names are sent and how to opt out.
- Rename doc reference from onStateUpdate to onStateChanged.
- Tests: assert JSON-RPC error responses and close WebSocket connections in routing tests to avoid teardown logs.

These changes tighten protocol behavior, improve error semantics, reduce noisy warnings, and stabilize tests.
@changeset-bot
Copy link

changeset-bot bot commented Mar 1, 2026

🦋 Changeset detected

Latest commit: 0cd330c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

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

@threepointone threepointone marked this pull request as ready for review March 1, 2026 13:56
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 1, 2026

Open in StackBlitz

npm i https://pkg.pr.new/agents@1035
npm i https://pkg.pr.new/@cloudflare/ai-chat@1035
npm i https://pkg.pr.new/@cloudflare/codemode@1035
npm i https://pkg.pr.new/hono-agents@1035

commit: 5b43274

@threepointone threepointone merged commit 24cf279 into main Mar 1, 2026
3 checks passed
@threepointone threepointone deleted the less-noisy-tests branch March 1, 2026 14:01
@github-actions github-actions bot mentioned this pull request Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant