|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | -import * as announceOutput from "./subagent-announce-output.js"; |
| 2 | +import type * as gatewayCallModule from "../gateway/call.js"; |
3 | 3 | import { |
4 | 4 | LOST_ACTIVE_EXECUTION_CONTEXT_ERROR, |
5 | 5 | resolveStaleActiveSubagentOutcome, |
6 | 6 | } from "./subagent-lost-context-completion.js"; |
7 | 7 |
|
| 8 | +const CHILD_SESSION = "agent:main:subagent:child"; |
| 9 | + |
| 10 | +function assistantTextMessage(text: string) { |
| 11 | + return { role: "assistant", content: [{ type: "text", text }] }; |
| 12 | +} |
| 13 | + |
| 14 | +function assistantStringMessage(text: string) { |
| 15 | + return { role: "assistant", content: text }; |
| 16 | +} |
| 17 | + |
| 18 | +function toolCallOnlyMessage() { |
| 19 | + return { |
| 20 | + role: "assistant", |
| 21 | + content: [{ type: "tool_use", id: "t1", name: "read", input: {} }], |
| 22 | + }; |
| 23 | +} |
| 24 | + |
8 | 25 | describe("resolveStaleActiveSubagentOutcome", () => { |
9 | | - beforeEach(() => { |
| 26 | + let callGatewaySpy: ReturnType<typeof vi.spyOn>; |
| 27 | + |
| 28 | + beforeEach(async () => { |
10 | 29 | vi.restoreAllMocks(); |
| 30 | + callGatewaySpy = vi.spyOn( |
| 31 | + (await import("../gateway/call.js")) as typeof gatewayCallModule, |
| 32 | + "callGateway", |
| 33 | + ); |
11 | 34 | }); |
12 | 35 |
|
13 | | - it("returns ok when child session has readable assistant output", async () => { |
14 | | - vi.spyOn(announceOutput, "readSubagentOutput").mockResolvedValue( |
15 | | - "# ARCHITECTURE.md\nrelease readiness design", |
16 | | - ); |
| 36 | + it("returns ok when child session has visible assistant text", async () => { |
| 37 | + callGatewaySpy.mockResolvedValue({ |
| 38 | + messages: [assistantTextMessage("# ARCHITECTURE.md\nrelease readiness design")], |
| 39 | + }); |
17 | 40 | await expect( |
18 | | - resolveStaleActiveSubagentOutcome({ |
19 | | - childSessionKey: "agent:main:subagent:child", |
20 | | - }), |
| 41 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
21 | 42 | ).resolves.toEqual({ status: "ok" }); |
22 | 43 | }); |
23 | 44 |
|
24 | | - it("returns lost-context error when no readable output is available", async () => { |
25 | | - vi.spyOn(announceOutput, "readSubagentOutput").mockResolvedValue(undefined); |
| 45 | + it("returns lost-context error when chat.history returns no messages", async () => { |
| 46 | + callGatewaySpy.mockResolvedValue({ messages: [] }); |
| 47 | + await expect( |
| 48 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
| 49 | + ).resolves.toEqual({ |
| 50 | + status: "error", |
| 51 | + error: LOST_ACTIVE_EXECUTION_CONTEXT_ERROR, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns lost-context error for silent reply token text", async () => { |
| 56 | + callGatewaySpy.mockResolvedValue({ |
| 57 | + messages: [assistantTextMessage("NO_REPLY")], |
| 58 | + }); |
26 | 59 | await expect( |
27 | | - resolveStaleActiveSubagentOutcome({ |
28 | | - childSessionKey: "agent:main:subagent:child", |
29 | | - }), |
| 60 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
30 | 61 | ).resolves.toEqual({ |
31 | 62 | status: "error", |
32 | 63 | error: LOST_ACTIVE_EXECUTION_CONTEXT_ERROR, |
33 | 64 | }); |
34 | 65 | }); |
35 | 66 |
|
36 | | - it("returns lost-context error when output is whitespace only", async () => { |
37 | | - vi.spyOn(announceOutput, "readSubagentOutput").mockResolvedValue(" \n "); |
| 67 | + it("returns lost-context error when history only contains tool calls without visible output", async () => { |
| 68 | + callGatewaySpy.mockResolvedValue({ |
| 69 | + messages: [toolCallOnlyMessage()], |
| 70 | + }); |
38 | 71 | await expect( |
39 | | - resolveStaleActiveSubagentOutcome({ |
40 | | - childSessionKey: "agent:main:subagent:child", |
41 | | - }), |
| 72 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
42 | 73 | ).resolves.toEqual({ |
43 | 74 | status: "error", |
44 | 75 | error: LOST_ACTIVE_EXECUTION_CONTEXT_ERROR, |
45 | 76 | }); |
46 | 77 | }); |
| 78 | + |
| 79 | + it("returns lost-context error when string-content assistant message is whitespace only", async () => { |
| 80 | + callGatewaySpy.mockResolvedValue({ |
| 81 | + messages: [assistantStringMessage(" \n ")], |
| 82 | + }); |
| 83 | + await expect( |
| 84 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
| 85 | + ).resolves.toEqual({ |
| 86 | + status: "error", |
| 87 | + error: LOST_ACTIVE_EXECUTION_CONTEXT_ERROR, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns ok for assistant message with string content", async () => { |
| 92 | + callGatewaySpy.mockResolvedValue({ |
| 93 | + messages: [assistantStringMessage("result text")], |
| 94 | + }); |
| 95 | + await expect( |
| 96 | + resolveStaleActiveSubagentOutcome({ childSessionKey: CHILD_SESSION }), |
| 97 | + ).resolves.toEqual({ status: "ok" }); |
| 98 | + }); |
47 | 99 | }); |
0 commit comments