fix(observability): preserve AI span hierarchy across WebSocket turns - #1982
Open
mattzcarey wants to merge 1 commit into
Open
fix(observability): preserve AI span hierarchy across WebSocket turns#1982mattzcarey wants to merge 1 commit into
mattzcarey wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 3ff1e76 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
mattzcarey
force-pushed
the
fix/v7-tracing-parentage
branch
from
July 27, 2026 11:11
ee4a8c4 to
3ff1e76
Compare
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.
Summary
invoke_agentis active before model and tool work startschat,execute_tool, and requested/approved/deniedtool_approvalspans in the expected hierarchyAsyncLocalStoragestate instead of replaying a captured snapshotRoot cause
Think used the AI SDK v7 lifecycle adapter directly. The SDK captures its asynchronous operation context before lifecycle callbacks run, so opening
invoke_agentfromonStartis too late: laterchatandexecute_toolspans inherit the pre-operation context.Delayed hibernatable WebSocket work has a second constraint. A later model step can retain the original workerd tracing context after that native invocation ends. An open span created in that stale context can then be force-closed at the earlier invocation timestamp, producing a negative duration or
span_not_endedwarning.Fix
Think now uses
wrapAISDKfor both supported majors. AI SDK v7 still exposes the structural model middleware used by the existing wrapper, so no duplicate v7 operation wrapper or tracing snapshot is needed. The shared wrapper now reads v7's renamedtelemetryandruntimeContextfields and handles v7's top-leveltoolApprovalpolicy.Invocation-bounded spans use one
finishOnAsyncHandoffpolicy. WebSocket Agent spans and streaming operation/model/tool spans close before pending async work escapes the invocation, while their ended context remains active so later children retain the correct parent ID.Regression coverage
The normal Agents test suite covers:
needsApprovaland top-level v7 approval policiesmodel → tool → modelWebSocket turns for both v6 and v7No CI-only test job, demo project, generated fixture, documentation rewrite, or lockfile change is included.
Verification
pnpm run check: exports, formatting, lint, and all 117 TypeScript projects