|
| 1 | +import type { SDKMessage } from "@anthropic-ai/claude-agent-sdk"; |
1 | 2 | import { SpanStatusCode, context, trace } from "@opentelemetry/api"; |
2 | 3 | import { |
3 | 4 | BasicTracerProvider, |
@@ -423,6 +424,20 @@ describe("wrapQuery", () => { |
423 | 424 | expect(subagent.status.code).toBe(SpanStatusCode.ERROR); |
424 | 425 | }, |
425 | 426 | ); |
| 427 | + |
| 428 | + it("N2: matches a task_notification without tool_use_id (optional, sdk.d.ts:5997) by its task_id", async () => { |
| 429 | + const journey = backgroundedSubagentJourney().map((m) => { |
| 430 | + if (m.type === "system" && m.subtype === "task_notification") { |
| 431 | + const { tool_use_id: _dropped, ...rest } = m as typeof m & { tool_use_id?: string }; |
| 432 | + return rest as SDKMessage; |
| 433 | + } |
| 434 | + return m; |
| 435 | + }); |
| 436 | + const { spans } = await run(journey); |
| 437 | + const subagent = one(spans, "claude_agent.subagent.code-reviewer"); |
| 438 | + expect(subagent.attributes["claude_agent.subagent.status"]).toBe("completed"); |
| 439 | + expect(subagent.status.code).toBe(SpanStatusCode.OK); |
| 440 | + }); |
426 | 441 | }); |
427 | 442 |
|
428 | 443 | describe("J3: MCP tool error and error result", () => { |
@@ -618,6 +633,65 @@ describe("wrapQuery", () => { |
618 | 633 | } |
619 | 634 | }); |
620 | 635 |
|
| 636 | + /** Read until the stream has yielded a result message (done is still false). */ |
| 637 | + async function readThroughResult(q: AsyncGenerator<SDKMessage, void>): Promise<void> { |
| 638 | + for (;;) { |
| 639 | + const step = await q.next(); |
| 640 | + if (step.done) throw new Error("stream ended before a result"); |
| 641 | + if (step.value.type === "result") return; |
| 642 | + } |
| 643 | + } |
| 644 | + |
| 645 | + it.each([ |
| 646 | + ["close", "simpleToolJourney"], |
| 647 | + ["asyncDispose", "simpleToolJourney"], |
| 648 | + ["close", "streamingInputJourney"], |
| 649 | + ["asyncDispose", "streamingInputJourney"], |
| 650 | + ] as const)( |
| 651 | + "N1: %s() after a result with nothing in flight ends the conversation OK, not cancelled (%s)", |
| 652 | + async (method, journeyName) => { |
| 653 | + const { provider, exporter } = memoryProvider(); |
| 654 | + const journey = journeyName === "simpleToolJourney" ? simpleToolJourney() : streamingInputJourney(); |
| 655 | + const fake = makeFakeQuery(journey); |
| 656 | + const q = wrapQuery(fake.query, { tracerProvider: provider })({ prompt: PROMPT }); |
| 657 | + await readThroughResult(q); |
| 658 | + expect(exporter.getFinishedSpans().some((s) => s.name === "claude_agent.conversation")).toBe(false); |
| 659 | + |
| 660 | + if (method === "close") { |
| 661 | + q.close(); |
| 662 | + } else { |
| 663 | + await (q as unknown as Record<symbol, () => Promise<void>>)[ASYNC_DISPOSE](); |
| 664 | + } |
| 665 | + |
| 666 | + const spans = exporter.getFinishedSpans(); |
| 667 | + const conversation = one(spans, "claude_agent.conversation"); |
| 668 | + expect(conversation.status.code).toBe(SpanStatusCode.OK); |
| 669 | + expect(conversation.attributes["claude_agent.cancelled"]).toBeUndefined(); |
| 670 | + for (const span of spans) { |
| 671 | + expect(span.ended).toBe(true); |
| 672 | + expect(span.attributes["claude_agent.cancelled"]).toBeUndefined(); |
| 673 | + } |
| 674 | + }, |
| 675 | + ); |
| 676 | + |
| 677 | + it("N1: close() after a result while a background subagent still runs stays cancelled", async () => { |
| 678 | + const { provider, exporter } = memoryProvider(); |
| 679 | + // The result arrives before the background subagent's task_notification. |
| 680 | + const journey = backgroundedSubagentJourney().filter( |
| 681 | + (m) => !(m.type === "system" && m.subtype === "task_notification"), |
| 682 | + ); |
| 683 | + const fake = makeFakeQuery(journey); |
| 684 | + const q = wrapQuery(fake.query, { tracerProvider: provider })({ prompt: PROMPT }); |
| 685 | + await readThroughResult(q); |
| 686 | + q.close(); |
| 687 | + const spans = exporter.getFinishedSpans(); |
| 688 | + const subagent = one(spans, "claude_agent.subagent.code-reviewer"); |
| 689 | + expect(subagent.attributes["claude_agent.cancelled"]).toBe(true); |
| 690 | + const conversation = one(spans, "claude_agent.conversation"); |
| 691 | + expect(conversation.status.code).toBe(SpanStatusCode.ERROR); |
| 692 | + expect(conversation.attributes["claude_agent.cancelled"]).toBe(true); |
| 693 | + }); |
| 694 | + |
621 | 695 | it("ends open spans when the consumer breaks out early", async () => { |
622 | 696 | const { provider, exporter } = memoryProvider(); |
623 | 697 | const fake = makeFakeQuery(simpleToolJourney()); |
|
0 commit comments