Problem
The connect sequence sends cf_agent_identity and then a separate cf_agent_state frame. AgentClient and useAgent resolve ready (and flip identified) when the identity frame arrives, so a caller that awaits ready on a host with stored state can observe state === undefined for one message event before the state frame is handled. In React this is one extra render with the default state.
This has always been Agent's behaviour. Since #2257 the WebSockets capability owns the connect sequence for every host, so a plain host has it too. Flagged by Devin on #2257 (packages/agents/src/websockets/websockets.ts, sendIdentity/sendState).
Options
- Carry the initial state inside the identity frame, so one frame resolves
ready with state already set. Cleanest; changes the identity frame shape for every client.
- Add a flag to the identity frame saying a state frame follows, and have the client hold
ready until it lands. Needs care so a stateless host, or one whose state is undefined, never waits forever.
Either way this is a wire change that has to land in client.ts, react.tsx, and the capability together, with the React and workers suites updated to the new ordering guarantee.
Where
packages/agents/src/websockets/websockets.ts: #connect, sendIdentity, sendState
packages/agents/src/client.ts, packages/agents/src/react.tsx: identity handling and _resolveReady
Problem
The connect sequence sends
cf_agent_identityand then a separatecf_agent_stateframe.AgentClientanduseAgentresolveready(and flipidentified) when the identity frame arrives, so a caller that awaitsreadyon a host with stored state can observestate === undefinedfor one message event before the state frame is handled. In React this is one extra render with the default state.This has always been Agent's behaviour. Since #2257 the
WebSocketscapability owns the connect sequence for every host, so a plain host has it too. Flagged by Devin on #2257 (packages/agents/src/websockets/websockets.ts,sendIdentity/sendState).Options
readywith state already set. Cleanest; changes the identity frame shape for every client.readyuntil it lands. Needs care so a stateless host, or one whose state isundefined, never waits forever.Either way this is a wire change that has to land in
client.ts,react.tsx, and the capability together, with the React and workers suites updated to the new ordering guarantee.Where
packages/agents/src/websockets/websockets.ts:#connect,sendIdentity,sendStatepackages/agents/src/client.ts,packages/agents/src/react.tsx: identity handling and_resolveReady