Skip to content

fix(observability): preserve AI span hierarchy across WebSocket turns - #1982

Open
mattzcarey wants to merge 1 commit into
mainfrom
fix/v7-tracing-parentage
Open

fix(observability): preserve AI span hierarchy across WebSocket turns#1982
mattzcarey wants to merge 1 commit into
mainfrom
fix/v7-tracing-parentage

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • use the existing AI SDK namespace wrapper for both v6 and v7 so invoke_agent is active before model and tool work starts
  • keep chat, execute_tool, and requested/approved/denied tool_approval spans in the expected hierarchy
  • preserve provider/tool AsyncLocalStorage state instead of replaying a captured snapshot
  • close WebSocket-owned Agent and AI spans when async work hands off beyond the native invocation
  • keep RPC, alarm, and other non-WebSocket spans open through normal completion

Root cause

Think used the AI SDK v7 lifecycle adapter directly. The SDK captures its asynchronous operation context before lifecycle callbacks run, so opening invoke_agent from onStart is too late: later chat and execute_tool spans 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_ended warning.

Fix

Think now uses wrapAISDK for 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 renamed telemetry and runtimeContext fields and handles v7's top-level toolApproval policy.

Invocation-bounded spans use one finishOnAsyncHandoff policy. 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:

  • real AI SDK v7 multi-step model/tool hierarchy
  • provider and tool auth-store replacement
  • needsApproval and top-level v7 approval policies
  • delayed model → tool → model WebSocket turns for both v6 and v7
  • the tracer's async-handoff lifetime contract

No CI-only test job, demo project, generated fixture, documentation rewrite, or lockfile change is included.

Verification

  • observability: 7 files / 112 tests
  • Agents: 124 files / 2,412 tests
  • Think: 52 files / 1,000 tests
  • AIChat: 50 files / 737 tests
  • agent-think: 11 files / 39 tests
  • actual AI SDK v6: focused regressions 19 tests; Think typecheck; Think workers 46 files / 920 tests
  • monorepo build: 25 projects
  • pnpm run check: exports, formatting, lint, and all 117 TypeScript projects

Open in Devin Review

@changeset-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3ff1e76

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: 3ff1e76

@mattzcarey mattzcarey changed the title fix(observability): parent AI SDK v7 spans under invoke_agent fix(observability): preserve AI SDK span hierarchy and lifetimes Jul 27, 2026
@mattzcarey
mattzcarey force-pushed the fix/v7-tracing-parentage branch from ee4a8c4 to 3ff1e76 Compare July 27, 2026 11:11
@mattzcarey mattzcarey changed the title fix(observability): preserve AI SDK span hierarchy and lifetimes fix(observability): preserve AI span hierarchy across WebSocket turns Jul 27, 2026
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