|
7 | 7 | buildCompactAnnounceStatsLine, |
8 | 8 | buildChildCompletionFindings, |
9 | 9 | readSubagentOutput, |
| 10 | + readTerminalChildResult, |
| 11 | + resolveTerminalChildResultText, |
10 | 12 | } from "./subagent-announce-output.js"; |
11 | 13 |
|
12 | 14 | type CallGateway = typeof import("../gateway/call.js").callGateway; |
@@ -415,3 +417,137 @@ describe("applySubagentWaitOutcome", () => { |
415 | 417 | }); |
416 | 418 | }); |
417 | 419 | }); |
| 420 | + |
| 421 | +function assistantTextTurn(text: string) { |
| 422 | + return { |
| 423 | + role: "assistant", |
| 424 | + stopReason: "stop", |
| 425 | + content: [{ type: "text", text }], |
| 426 | + }; |
| 427 | +} |
| 428 | + |
| 429 | +function toolCallOnlyTurn(name = "read") { |
| 430 | + return { |
| 431 | + role: "assistant", |
| 432 | + stopReason: "toolUse", |
| 433 | + content: [{ type: "toolCall", id: `call-${name}`, name, arguments: {} }], |
| 434 | + }; |
| 435 | +} |
| 436 | + |
| 437 | +describe("resolveTerminalChildResultText", () => { |
| 438 | + it("returns the final visible assistant result for a genuine terminal turn", () => { |
| 439 | + expect( |
| 440 | + resolveTerminalChildResultText([assistantTextTurn("# ARCHITECTURE.md\nDesign complete.")]), |
| 441 | + ).toBe("# ARCHITECTURE.md\nDesign complete."); |
| 442 | + }); |
| 443 | + |
| 444 | + it("returns undefined for a toolUse turn that has visible pre-tool text plus a tool call", () => { |
| 445 | + // Regression for ClawSweeper P1 on #92791: a normal assistant turn can carry |
| 446 | + // progress text AND a tool call (stopReason "toolUse"); the model expected to |
| 447 | + // continue after tool results, so that text is NOT a terminal result. |
| 448 | + const mixedTurn = { |
| 449 | + role: "assistant", |
| 450 | + stopReason: "toolUse", |
| 451 | + content: [ |
| 452 | + { type: "text", text: "Let me read the brief first." }, |
| 453 | + { type: "toolCall", id: "call-read", name: "read", arguments: {} }, |
| 454 | + ], |
| 455 | + }; |
| 456 | + expect(resolveTerminalChildResultText([mixedTurn])).toBeUndefined(); |
| 457 | + }); |
| 458 | + |
| 459 | + it("returns undefined when a terminal text turn is followed by a trailing tool-only turn", () => { |
| 460 | + // The child produced text but then kept working (more tool calls) → not done. |
| 461 | + expect( |
| 462 | + resolveTerminalChildResultText([ |
| 463 | + assistantTextTurn("Draft ready, verifying…"), |
| 464 | + toolCallOnlyTurn(), |
| 465 | + ]), |
| 466 | + ).toBeUndefined(); |
| 467 | + }); |
| 468 | + |
| 469 | + it("returns undefined for a tool-call-only history (no visible result)", () => { |
| 470 | + // Regression for #90299 / PR #90492: the broad display helper would surface a |
| 471 | + // synthetic "1 tool call(s) made without visible output." string here, which |
| 472 | + // must NOT count as a terminal result. |
| 473 | + expect(resolveTerminalChildResultText([toolCallOnlyTurn()])).toBeUndefined(); |
| 474 | + }); |
| 475 | + |
| 476 | + it("returns undefined for a sessions_yield waiting turn", () => { |
| 477 | + // Regression for #90299 / PR #91400: a child parked on sessions_yield is |
| 478 | + // waiting on descendants, not finished, and must not be recovered as ok. |
| 479 | + expect(resolveTerminalChildResultText(sessionsYieldTurn())).toBeUndefined(); |
| 480 | + }); |
| 481 | + |
| 482 | + it("returns the final assistant result that arrives after a sessions_yield wait", () => { |
| 483 | + expect( |
| 484 | + resolveTerminalChildResultText([ |
| 485 | + ...sessionsYieldTurn(), |
| 486 | + assistantTextTurn("Final report ready."), |
| 487 | + ]), |
| 488 | + ).toBe("Final report ready."); |
| 489 | + }); |
| 490 | + |
| 491 | + it("returns undefined for whitespace-only assistant text", () => { |
| 492 | + expect(resolveTerminalChildResultText([assistantTextTurn(" ")])).toBeUndefined(); |
| 493 | + }); |
| 494 | + |
| 495 | + it("returns undefined for an empty history", () => { |
| 496 | + expect(resolveTerminalChildResultText([])).toBeUndefined(); |
| 497 | + }); |
| 498 | +}); |
| 499 | + |
| 500 | +describe("readTerminalChildResult", () => { |
| 501 | + afterEach(() => { |
| 502 | + testing.setDepsForTest(); |
| 503 | + }); |
| 504 | + |
| 505 | + it("reads gateway history and returns the genuine terminal result", async () => { |
| 506 | + const deps = installOutputDeps({ |
| 507 | + messages: [assistantTextTurn("# ARCHITECTURE.md\nDesign complete.")], |
| 508 | + }); |
| 509 | + await expect(readTerminalChildResult("agent:main:subagent:child")).resolves.toBe( |
| 510 | + "# ARCHITECTURE.md\nDesign complete.", |
| 511 | + ); |
| 512 | + expect(deps.callGateway).toHaveBeenCalledOnce(); |
| 513 | + }); |
| 514 | + |
| 515 | + it("returns undefined for a tool-call-only history while readSubagentOutput surfaces a summary", async () => { |
| 516 | + // Same transcript, two predicates: the display helper still reports a |
| 517 | + // tool-call summary, but the strict terminal predicate reports no result — |
| 518 | + // this is exactly the boundary the lost-context sweeper must respect. |
| 519 | + installOutputDeps({ messages: [toolCallOnlyTurn()] }); |
| 520 | + await expect(readSubagentOutput("agent:main:subagent:child")).resolves.toBe( |
| 521 | + "1 tool call(s) made without visible output.", |
| 522 | + ); |
| 523 | + |
| 524 | + installOutputDeps({ messages: [toolCallOnlyTurn()] }); |
| 525 | + await expect(readTerminalChildResult("agent:main:subagent:child")).resolves.toBeUndefined(); |
| 526 | + }); |
| 527 | + |
| 528 | + it("returns undefined for a sessions_yield waiting transcript", async () => { |
| 529 | + installOutputDeps({ messages: sessionsYieldTurn() }); |
| 530 | + await expect(readTerminalChildResult("agent:main:subagent:child")).resolves.toBeUndefined(); |
| 531 | + }); |
| 532 | + |
| 533 | + it("returns undefined for a toolUse turn while readSubagentOutput keeps the display text", async () => { |
| 534 | + const mixed = [ |
| 535 | + { |
| 536 | + role: "assistant", |
| 537 | + stopReason: "toolUse", |
| 538 | + content: [ |
| 539 | + { type: "text", text: "Let me read the brief first." }, |
| 540 | + { type: "toolCall", id: "call-read", name: "read", arguments: {} }, |
| 541 | + ], |
| 542 | + }, |
| 543 | + ]; |
| 544 | + // Display behavior is intentionally unchanged: it still shows the visible text. |
| 545 | + installOutputDeps({ messages: mixed }); |
| 546 | + await expect(readSubagentOutput("agent:main:subagent:child")).resolves.toBe( |
| 547 | + "Let me read the brief first.", |
| 548 | + ); |
| 549 | + // But the lifecycle predicate refuses to treat that pre-tool text as terminal. |
| 550 | + installOutputDeps({ messages: mixed }); |
| 551 | + await expect(readTerminalChildResult("agent:main:subagent:child")).resolves.toBeUndefined(); |
| 552 | + }); |
| 553 | +}); |
0 commit comments