Skip to content

Commit 378acc5

Browse files
committed
fix(claude-agent-sdk): start streaming-input turns after the previous result
In a streaming-input session one query() yields a result per user turn. The next main-loop turn fell back to the query start time (spans.ts startTurn: nextTurnStartMs ?? this.startTimeMs), so the second turn overlapped the first. onResult now sets the main scope's nextTurnStartMs, the same way a user message does. Test: a two-result streaming-input journey (new fixture, numbers taken from the real CLI against the mock) checks turn 2 starts at or after turn 1's end; it failed by 28 ms before the fix. approved: Nikhil 2026-10-03 blanket Refs: TH-8235
1 parent 14e3adf commit 378acc5

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎typescript/packages/traceai_claude_agent_sdk/src/__tests__/fixtures/messages.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,58 @@ export function mcpErrorJourney(): SDKMessage[] {
271271
resultError(),
272272
];
273273
}
274+
275+
type ModelUsage = SDKResultSuccess["modelUsage"][string];
276+
277+
/** One `modelUsage` entry (sdk.d.ts ModelUsage). */
278+
export function modelUsage(
279+
input: number,
280+
output: number,
281+
costUSD: number,
282+
cacheRead = 0,
283+
cacheCreation = 0,
284+
): ModelUsage {
285+
return {
286+
inputTokens: input,
287+
outputTokens: output,
288+
cacheReadInputTokens: cacheRead,
289+
cacheCreationInputTokens: cacheCreation,
290+
webSearchRequests: 0,
291+
costUSD,
292+
contextWindow: 200000,
293+
maxOutputTokens: 64000,
294+
};
295+
}
296+
297+
/** `result.usage` for one main-loop turn (sdk.d.ts:5683: main agent loop only, per turn). */
298+
export function turnUsage(input: number, output: number): NonNullableUsage {
299+
return { ...resultUsage, input_tokens: input, output_tokens: output, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 };
300+
}
301+
302+
/** Cost of one 20-in / 8-out call on claude-sonnet-4-5 list prices, as the real CLI reports it. */
303+
export const TURN_COST_USD = 0.00018;
304+
305+
/**
306+
* Streaming input: one query(), two user turns, two results. `total_cost_usd`
307+
* and `modelUsage` are running totals (sdk.d.ts:5679, 5687); `usage` is per
308+
* turn (sdk.d.ts:5683). Same numbers the real CLI reported against the mock.
309+
*/
310+
export function streamingInputJourney(): SDKMessage[] {
311+
return [
312+
init(),
313+
assistant("msg_s1", [text("First answer.")]),
314+
resultSuccess("First answer.", {
315+
num_turns: 1,
316+
total_cost_usd: TURN_COST_USD,
317+
usage: turnUsage(20, 8),
318+
modelUsage: { [MODEL]: modelUsage(20, 8, TURN_COST_USD) },
319+
}),
320+
assistant("msg_s2", [text("Second answer.")]),
321+
resultSuccess("Second answer.", {
322+
num_turns: 2,
323+
total_cost_usd: 2 * TURN_COST_USD,
324+
usage: turnUsage(20, 8),
325+
modelUsage: { [MODEL]: modelUsage(40, 16, 2 * TURN_COST_USD) },
326+
}),
327+
];
328+
}

‎typescript/packages/traceai_claude_agent_sdk/src/__tests__/wrapper.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
TOTAL_COST_USD,
2828
mcpErrorJourney,
2929
simpleToolJourney,
30+
streamingInputJourney,
3031
subagentJourney,
3132
} from "./fixtures/messages";
3233
import { StackContextManager, allAttributeText, byName, drain, memoryProvider, one, parentId } from "./helpers";
@@ -630,6 +631,30 @@ describe("wrapQuery", () => {
630631
expect(exporter.getFinishedSpans()).toHaveLength(0);
631632
});
632633

634+
describe("streaming input (one query(), two user turns)", () => {
635+
async function* userTurns() {
636+
yield { type: "user" as const, message: { role: "user" as const, content: "one" }, parent_tool_use_id: null };
637+
yield { type: "user" as const, message: { role: "user" as const, content: "two" }, parent_tool_use_id: null };
638+
}
639+
const hrMs = (t: [number, number]) => t[0] * 1e3 + t[1] / 1e6;
640+
641+
it("R5: starts the second turn after the first result, so the turns do not overlap", async () => {
642+
const { provider, exporter } = memoryProvider();
643+
const fake = makeFakeQuery(streamingInputJourney(), { delayMs: 5 });
644+
await drain(wrapQuery(fake.query, { tracerProvider: provider })({ prompt: userTurns() }));
645+
const spans = exporter.getFinishedSpans();
646+
const conversation = one(spans, "claude_agent.conversation");
647+
const [first, second] = byName(spans, "claude_agent.assistant_turn").sort(
648+
(a, b) => hrMs(a.startTime) - hrMs(b.startTime),
649+
);
650+
expect(first.attributes["claude_agent.num_turns"]).toBe(1);
651+
expect(second.attributes["claude_agent.num_turns"]).toBe(2);
652+
expect(hrMs(first.startTime)).toBe(hrMs(conversation.startTime));
653+
expect(hrMs(second.startTime)).toBeGreaterThanOrEqual(hrMs(first.endTime));
654+
expect(hrMs(second.endTime)).toBeLessThanOrEqual(hrMs(conversation.endTime));
655+
});
656+
});
657+
633658
it("does not trace a streaming-input prompt's content", async () => {
634659
const { provider, exporter } = memoryProvider();
635660
const traced = wrapQuery(makeFakeQuery(simpleToolJourney()).query, {

‎typescript/packages/traceai_claude_agent_sdk/src/spans.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ export class QueryTracer {
464464
if (main && !main.ended) {
465465
this.endTurn(main);
466466
}
467+
// Streaming input: the next main-loop turn (if any) answers a user message
468+
// sent after this result, so it cannot start before now.
469+
this.scope(null).nextTurnStartMs = clockMs();
467470

468471
if (typeof message.session_id === "string" && message.session_id) {
469472
this.setSessionId(message.session_id);

0 commit comments

Comments
 (0)