Skip to content

fix(agents): resolve ready only after the initial state frame (#2268) - #2402

Merged
threepointone merged 3 commits into
fix/2118-rejected-subagent-socketfrom
fix/2268-ready-after-state
Sep 28, 2026
Merged

threepointone merged 3 commits into
fix/2118-rejected-subagent-socketfrom
fix/2268-ready-after-state

Conversation

@threepointone

@threepointone threepointone commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2268.

Problem

The server sends the identity frame before the state frame, and the tests in msg-ordering.test.ts pin that order as part of the wire protocol. AgentClient and useAgent resolved ready and identified on identity, so code awaiting ready read state as undefined on an agent that has state.

Fix

I kept the wire order and added a flag:

  • The server serializes the state frame first, and sends identity with stateFollows: true only when a state frame will actually follow. The new public sendConnectFrames() does this; Agent and the websockets capability both use it.
  • Clients hold ready and identified until the state lands, if the flag is set. They also complete when state arrived before identity, so a stateless host never waits.

Compatibility works in both directions. An unflagged server behaves as before, and a client that ignores the flag behaves as before.

useAgent keeps per-socket connect progress in a WeakMap, so a socket that was replaced cannot complete the new one's handshake.

Tests

  • A useAgent ReadyProbe test and an AgentClient test assert that state lands before identity resolves. Both fail without the fix.
  • Protocol tests check the flag on a stateful agent and its absence on a stateless one.
  • The old test "should have state undefined before any state update" pinned the bug, so it now points at an agent with no initial state.

@changeset-bot

changeset-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4e4c13c

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

This PR includes changesets to release 2 packages
Name Type
agents Patch
@cloudflare/agent-think 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 added this pull request to stack #2407 September 28, 2026 14:47
@threepointone threepointone changed the title fix(agents): resolve ready only after the initial state frame (#2268) feat(think): opt-in deferred tool discovery (#2277) Sep 28, 2026
@threepointone
threepointone marked this pull request as ready for review September 28, 2026 14:50
@threepointone threepointone changed the title feat(think): opt-in deferred tool discovery (#2277) fix(agents): resolve ready only after the initial state frame (#2268) Sep 28, 2026

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

Devin Review found 3 potential issues.

Devin Review

Comment thread packages/agents/src/client.ts
Comment thread packages/agents/src/client.ts Outdated
Comment thread docs/agents/lifecycle.md
@threepointone
threepointone force-pushed the fix/2268-ready-after-state branch from 30ad6e7 to 9cfa658 Compare September 28, 2026 15:46

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 new potential issues.

Devin Review

Comment thread packages/agents/src/react.tsx
Comment thread packages/agents/src/client.ts
@agent-think

agent-think Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

✅ agents import sizes: no significant changes (64155b9d → 4e4c13c0, workflow run)

threepointone and others added 3 commits September 28, 2026 17:50
The identity frame now carries stateFollows when a state frame is next, and
AgentClient and useAgent hold ready and identified until that state lands.
The server sets the flag only when it will send the frame (serialized before
the identity goes out), and clients also complete if the state arrived first,
so a stateless host never waits. Unflagged servers and clients that ignore
the flag keep the previous behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
…ady settles

Co-authored-by: Cursor <cursoragent@cursor.com>
@threepointone
threepointone force-pushed the fix/2268-ready-after-state branch from 9cfa658 to 4e4c13c Compare September 28, 2026 16:54
@pkg-pr-new

pkg-pr-new Bot commented Sep 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@2402

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@2402

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@2402

hono-agents

npm i https://pkg.pr.new/hono-agents@2402

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@2402

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@2402

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@2402

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@2402

commit: 4e4c13c

@threepointone
threepointone merged commit 01d190e into main Sep 28, 2026
26 of 32 checks passed
@threepointone
threepointone deleted the fix/2268-ready-after-state branch September 28, 2026 18:04
@github-actions github-actions Bot mentioned this pull request Oct 2, 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.

useAgent ready resolves on the identity frame, before the initial state frame

1 participant