Skip to content

Sub-agent connection state set in onConnect is lost before the first message #2206

Description

@O4epegb

Describe the bug

For a Think sub-agent, connection state set during onConnect can be lost before the first client message. Immediately after connection.setState(...), connection.state is correct; when the first message arrives on the same connection, it is null.

To Reproduce

  1. Connect a browser to a Think instance through a parent Agent as a sub-agent.
  2. In onConnect, call:
    connection.setState({ userId: "user-123" });
  3. Immediately send a chat message.
  4. Read connection.state while handling that message.
  5. Observe that the state is null.

Expected behavior

Connection state set in onConnect should be available to subsequent messages on that connection.

Version:

  • agents@0.22.0
  • @cloudflare/think@0.17.0

Additional context

This appears to be a regression from agents@0.20.1. It only reproduces through the sub-agent/facet connection path.

A suspected cause is the operation queue added in PR #2090: Think sends initial connection frames before application onConnect, so setState is queued behind them. The connect handler can finish before the state reaches the root socket, allowing the first client frame to replay stale null metadata into the sub-agent.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions