fix(cloudflare): Set agent conversation id on the onRequest path - #22846
Merged
JPeer264 merged 2 commits intoJul 30, 2026
Conversation
`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>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
sergical
marked this pull request as ready for review
July 30, 2026 03:10
JPeer264
self-requested a review
July 30, 2026 11:30
JPeer264
approved these changes
Jul 30, 2026
JPeer264
left a comment
Member
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.gen_ai.conversation.idonChatMessage@callable()over WebSocketonMessageonRequestWe set the id inside the
onChatMessageandonMessageproxies. An HTTP request touches neither: it arrives through theobj.fetchproxy indurableobject.ts, which never sets it. This PR wrapsonRequestthe same way the other two are wrapped.Review notes:
agentsinstallsonRequestas an own property in theAgentconstructor, same asonMessage.obj.fetchproxy, whichinstrumentDurableObjectWithSentryshares — plain Durable Objects shouldn't run agent-specific code.ai-streaming.test.tsalready 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 thesrcchange. Confirmed on a deployed Worker.Closes #22845