Conversation
…ted query()
The wrapped query() returned a fresh { [Symbol.asyncIterator] } object, so
every method the SDK declares on Query (interrupt(), setPermissionMode(),
setModel(), next()/return()/throw(), ...) was undefined once tracing was on.
It also deferred the SDK call until iteration began, whereas the unwrapped
SDK spawns the Claude Code process at query() time.
Call the SDK at query() time, start the AGENT span there so the injected tool
hooks have their parent, and return a Proxy over the real Query: the async
iterator and next()/return()/throw() route through one shared traced iterator
(so the span ends exactly once), everything else forwards bound to the SDK
object.
Fixes Arize-ai#3774
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MXxV7qtX4GyTfo7QWaREnL
Author
|
I have read the CLA Document and I hereby sign the CLA |
Contributor
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
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.
Fixes #3774
What changed
wrapQuery()inopeninference-instrumentation-claude-agent-sdkreturned a new{ [Symbol.asyncIterator]() {...} }object instead of the SDK'sQuery. Every control method onQuery(interrupt(),setPermissionMode(),setModel(),setMaxThinkingTokens(),applyFlagSettings(),reinitialize(),initializationResult()) and theAsyncGeneratormethods (next()/return()/throw()) wereundefinedon the instrumented result. Code that stops a running query withq.interrupt()silently stopped working when tracing was enabled.The wrapper now:
query()time, as the unwrapped SDK does (it spawns the Claude Code process on call), and starts the AGENT span at that point so the injectedPreToolUse/PostToolUsehooks have their parent span before the SDK sees them;Proxyover the realQuery:[Symbol.asyncIterator],next,returnandthrowroute through one shared traced iterator (created on first use, so the span ends exactly once whichever entry point the caller uses); every other member forwards to the SDK object, bound to it so implementations that rely onthiskeep working.The traced iterator body is unchanged from before; it just moved out of
[Symbol.asyncIterator]()so it can be shared.wrapQuerybecame generic over the return type somanuallyInstrument()keeps returning the same type the module exposes.Why the suite did not catch it
Every test in
test/v1Query.test.tsmocksquery()as a plain{ [Symbol.asyncIterator] }, so there were no methods to lose. This PR adds three tests:Query(async iterator plusinterrupt()andsetPermissionMode()), asserting the methods survive instrumentation, are callable, and that iteration still yields one OK AGENT span;next()andreturn()on the returned object is traced and ends the span;query()is invoked when the wrapper is called, not when iteration begins.All three fail on
mainand pass here. The rest of the suite is unchanged (45/45).Behaviour notes for review
query()call. In practice callers iterate immediately, and this is what the unwrapped SDK's process lifetime already looks like.@anthropic-ai/claude-agent-sdk@0.3.218before the fix:Reflect.ownKeys(wrapped.query(...))is[Symbol(Symbol.asyncIterator)], all eleven methodsundefined.Changeset included (patch).
🤖 Generated with Claude Code
https://claude.ai/code/session_01MXxV7qtX4GyTfo7QWaREnL