fix(observability): parent AI SDK v7 spans under invoke_agent#1982
Open
mattzcarey wants to merge 3 commits into
Open
fix(observability): parent AI SDK v7 spans under invoke_agent#1982mattzcarey wants to merge 3 commits into
mattzcarey wants to merge 3 commits into
Conversation
Capture the invoke_agent async context during the AI SDK v7 onStart callback and re-enter it for deferred model and tool execution hooks. This restores chat and execute_tool parentage while preserving nested provider and tool work.\n\nAdd real AI SDK v7 streaming and multi-step tool-loop coverage, plus concurrent-operation parent assertions.
🦋 Changeset detectedLatest commit: 2264c36 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: |
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_agentbefore the SDK captures its operation contextchatandexecute_toolspans under that operation without replaying unrelatedAsyncLocalStoragestatetool_approvalspans as children ofexecute_toolchat, nested inference insideexecute_tool, and customer telemetry integrationsRoot cause
AI SDK v7 captures its asynchronous operation context before invoking telemetry lifecycle callbacks. Opening
invoke_agentinonStarttherefore leaves later model and tool work in the context captured before that span existed.The first revision re-entered an
AsyncLocalStorage.snapshot()captured whileinvoke_agentwas active. That fixed span parentage, but snapshots restore every active ALS store. A provider or tool that replaced an authorization, tenant, or request store afteronStartcould see stale state.wrapAISDKnow detects AI SDK v7 and opensinvoke_agentaround the AI operation itself. The SDK naturally captures that parent context, while a per-call v7 telemetry adapter creates model and tool spans. No tracing snapshot runs around provider or tool execution. Think uses this wrapper for both supported AI SDK majors and preserves non-Agents telemetry integrations.Production validation
Deployed the revised Think probe with Workers AI model
@cf/moonshotai/kimi-k2.7-code. It forced onedoubletool call between two model steps and completed successfully on Worker version35eff47d-ec69-4179-bc54-fef76a5a0c3a.Queried the
oteldataset through Cloudflare MCP. Tracec53e771b9d662ce367f079aa5f96840chas the expected hierarchy:invoke_agent ParentageProbe2ff6ad1c324afb12198dc386312b8c51chat08160837f61790772ff6ad1c324afb12execute_tool double854dad8e54e062d32ff6ad1c324afb12chat19e6c448f396f1112ff6ad1c324afb12Both model calls and the tool use the
invoke_agentspan ID asparentSpanId. The temporary probe Worker was deleted after validation.Tool approval validation
A second deployed probe exercised real AI SDK v7 approval requests and approved/denied continuations with Workers AI. Cloudflare MCP returned trace
65ed20a8422ad2f20d770de776fb17fc:invoke_agentexecute_tool deploy(parent)tool_approval deploy(parent)638998c6ff2a6a75d3a9739c5094df4d(638998c6ff2a6a75)e3122636e7345b9f(d3a9739c5094df4d)051c2eef8050e0b3e1401ce71357e5a5(051c2eef8050e0b3)7cd1a5234f29bf32(e1401ce71357e5a5)83bb57fd427dc680659f9300e4740af0(83bb57fd427dc680)4e365427221e8941(659f9300e4740af0)The approved path uses the actual tool execution span. Requested and denied paths use bounded synthetic
execute_toolparents because no tool executes. The probe executed the tool exactly once and was deleted after validation.Verification
pnpm run check: exports, formatting, lint, and all 117 TypeScript projects