Skip to content

fix(observability): parent AI SDK v7 spans under invoke_agent#1982

Open
mattzcarey wants to merge 3 commits into
mainfrom
fix/v7-tracing-parentage
Open

fix(observability): parent AI SDK v7 spans under invoke_agent#1982
mattzcarey wants to merge 3 commits into
mainfrom
fix/v7-tracing-parentage

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • restore AI SDK v7 trace hierarchy by starting invoke_agent before the SDK captures its operation context
  • keep chat and execute_tool spans under that operation without replaying unrelated AsyncLocalStorage state
  • record requested, approved, and denied tool_approval spans as children of execute_tool
  • preserve provider subrequests under chat, nested inference inside execute_tool, and customer telemetry integrations
  • add real AI SDK v7 coverage for streaming, multi-step tools, object operations, adapter deduplication, and provider/tool authorization context

Root cause

AI SDK v7 captures its asynchronous operation context before invoking telemetry lifecycle callbacks. Opening invoke_agent in onStart therefore leaves later model and tool work in the context captured before that span existed.

The first revision re-entered an AsyncLocalStorage.snapshot() captured while invoke_agent was active. That fixed span parentage, but snapshots restore every active ALS store. A provider or tool that replaced an authorization, tenant, or request store after onStart could see stale state.

wrapAISDK now detects AI SDK v7 and opens invoke_agent around 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 one double tool call between two model steps and completed successfully on Worker version 35eff47d-ec69-4179-bc54-fef76a5a0c3a.

Queried the otel dataset through Cloudflare MCP. Trace c53e771b9d662ce367f079aa5f96840c has the expected hierarchy:

Span Span ID Parent span ID
invoke_agent ParentageProbe 2ff6ad1c324afb12 198dc386312b8c51
first chat 08160837f6179077 2ff6ad1c324afb12
execute_tool double 854dad8e54e062d3 2ff6ad1c324afb12
second chat 19e6c448f396f111 2ff6ad1c324afb12

Both model calls and the tool use the invoke_agent span ID as parentSpanId. 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:

State invoke_agent execute_tool deploy (parent) tool_approval deploy (parent)
requested 638998c6ff2a6a75 d3a9739c5094df4d (638998c6ff2a6a75) e3122636e7345b9f (d3a9739c5094df4d)
approved 051c2eef8050e0b3 e1401ce71357e5a5 (051c2eef8050e0b3) 7cd1a5234f29bf32 (e1401ce71357e5a5)
denied 83bb57fd427dc680 659f9300e4740af0 (83bb57fd427dc680) 4e365427221e8941 (659f9300e4740af0)

The approved path uses the actual tool execution span. Requested and denied paths use bounded synthetic execute_tool parents because no tool executes. The probe executed the tool exactly once and was deleted after validation.

Verification

  • focused v7 adapter + real-SDK tests: 3 files / 31 tests
  • Agents: 124 files / 2,417 tests
  • Think: 52 files / 1,000 tests
  • agent-think: 11 files / 39 tests
  • full monorepo build: 25 projects
  • pnpm run check: exports, formatting, lint, and all 117 TypeScript projects

Open in Devin Review

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-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2264c36

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
agents Patch
@cloudflare/think Patch
@cloudflare/agent-think Patch

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

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Open in Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Jul 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1982

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1982

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1982

create-think

npm i https://pkg.pr.new/create-think@1982

hono-agents

npm i https://pkg.pr.new/hono-agents@1982

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1982

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1982

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1982

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1982

commit: 2264c36

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.

1 participant