Skip to content

fix(cloudflare): Set agent conversation id on the onRequest path - #22846

Merged
JPeer264 merged 2 commits into
developfrom
fix/cloudflare-agent-onrequest-conversation-id
Jul 30, 2026
Merged

fix(cloudflare): Set agent conversation id on the onRequest path#22846
JPeer264 merged 2 commits into
developfrom
fix/cloudflare-agent-onrequest-conversation-id

Conversation

@sergical

@sergical sergical commented Jul 30, 2026

Copy link
Copy Markdown
Member

An Agent that handles a plain HTTP request — a webhook, a REST endpoint — produces AI spans with no gen_ai.conversation.id, so Sentry can't group its turns into a conversation. Chat agents and @callable() RPC agents work fine.

How the agent is reached Handler gen_ai.conversation.id
Chat onChatMessage set
@callable() over WebSocket onMessage set
HTTP onRequest missing — fixed here

We set the id inside the onChatMessage and onMessage proxies. An HTTP request touches neither: it arrives through the obj.fetch proxy in durableobject.ts, which never sets it. This PR wraps onRequest the same way the other two are wrapped.

Review notes:

  • Wrapping after construction is safe — agents installs onRequest as an own property in the Agent constructor, same as onMessage.
  • It stays out of the obj.fetch proxy, which instrumentDurableObjectWithSentry shares — plain Durable Objects shouldn't run agent-specific code.

ai-streaming.test.ts already drove both fixture agents over HTTP but never asserted this attribute, which is why CI was green. It does now, plus unit tests that fail without the src change. Confirmed on a deployed Worker.

Closes #22845

`instrumentAgentWithSentry` set the conversation id for chat turns and
`@callable()` RPC, but not for agents entered over plain HTTP. Agents
reached through `onRequest` — REST endpoints, webhooks, any non-WebSocket
caller — produced `gen_ai` spans with no `gen_ai.conversation.id`, so their
turns could not be grouped.

Wrap `onRequest` alongside the existing two hooks. `agents` installs it as
an own property in the `Agent` constructor (the same treatment `onMessage`
gets) and we instrument after construction, so the wrapped property is what
the routing layer calls.

Kept out of the `obj.fetch` proxy in `durableobject.ts`, which is shared
with `instrumentDurableObjectWithSentry` — plain Durable Objects should not
run agent-specific code.

Verified on a deployed Cloudflare Worker: before, all 14 spans of a trace
were unstamped; after, every `gen_ai` span carries the instance name, with
only `http.server` and `rpc` spans unstamped as `conversationIdIntegration`
intends.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sergical sergical self-assigned this Jul 30, 2026
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sergical
sergical marked this pull request as ready for review July 30, 2026 03:10
@sergical
sergical requested a review from a team as a code owner July 30, 2026 03:10
@sergical
sergical requested review from isaacs and mydea and removed request for a team July 30, 2026 03:10
@JPeer264
JPeer264 self-requested a review July 30, 2026 11:30

@JPeer264 JPeer264 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I could have sworn I added this already. I think I got so many different experiments and different branches that on my clean up that got removed. Thanks for adding that.

@JPeer264
JPeer264 merged commit 96ff33c into develop Jul 30, 2026
82 checks passed
@JPeer264
JPeer264 deleted the fix/cloudflare-agent-onrequest-conversation-id branch July 30, 2026 11:32
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.

Agents entered via onRequest never get gen_ai.conversation.id

2 participants