|
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import type { SessionDeliveryState } from "../../../config/sessions/types.js"; |
5 | 5 | import type { AgentRunTerminalReplySnapshot } from "../../agent-run-terminal-reply.js"; |
| 6 | +import { maybeSpawnVisibleSession } from "../../tools/sessions-spawn-visible.js"; |
| 7 | +import { createSessionsYieldTool } from "../../tools/sessions-yield-tool.js"; |
6 | 8 | import { testing as subagentAnnounceDeliveryTesting } from "../announce/subagent-announce-delivery.test-support.js"; |
7 | 9 | import { testing as subagentAnnounceOutputTesting } from "../announce/subagent-announce-output.test-support.js"; |
8 | 10 | import { testing as subagentAnnounceTesting } from "../announce/subagent-announce.js"; |
@@ -403,6 +405,98 @@ describe("subagent registry lifecycle error grace", () => { |
403 | 405 | }); |
404 | 406 | } |
405 | 407 |
|
| 408 | + it("yields an owned visible child and delivers its requester final exactly once", async () => { |
| 409 | + const requesterTurnRunId = "run-requester-visible-yield"; |
| 410 | + const runId = "run-visible-yield"; |
| 411 | + const childSessionKey = "agent:main:dashboard:visible-yield"; |
| 412 | + const spawnResult = await maybeSpawnVisibleSession({ |
| 413 | + raw: { visible: true }, |
| 414 | + task: "finish visible dashboard work", |
| 415 | + label: "Visible child", |
| 416 | + runtime: "subagent", |
| 417 | + sandbox: "inherit", |
| 418 | + options: { |
| 419 | + agentSessionKey: MAIN_REQUESTER_SESSION_KEY, |
| 420 | + requesterTurnRunId, |
| 421 | + requesterAgentIdOverride: "main", |
| 422 | + config: { |
| 423 | + agents: { list: [{ id: "main" }] }, |
| 424 | + session: { mainKey: "main", scope: "per-sender" }, |
| 425 | + }, |
| 426 | + callGateway: vi.fn(async () => ({ |
| 427 | + key: childSessionKey, |
| 428 | + runStarted: true, |
| 429 | + runId, |
| 430 | + })) as never, |
| 431 | + registerRun: mod.registerSubagentRun, |
| 432 | + countActiveRuns: () => 0, |
| 433 | + }, |
| 434 | + }); |
| 435 | + expect(spawnResult).toMatchObject({ status: "accepted", runId, childSessionKey }); |
| 436 | + setAssistantOutput(childSessionKey, "visible dashboard child complete"); |
| 437 | + |
| 438 | + const onYield = vi.fn(); |
| 439 | + const yieldTool = createSessionsYieldTool({ |
| 440 | + sessionId: "sess-main", |
| 441 | + claimYield: () => |
| 442 | + mod.markRequesterTurnYielded({ |
| 443 | + requesterSessionKey: MAIN_REQUESTER_SESSION_KEY, |
| 444 | + requesterAgentId: "main", |
| 445 | + requesterTurnRunId, |
| 446 | + }) > 0, |
| 447 | + onYield, |
| 448 | + }); |
| 449 | + const yieldResult = await yieldTool.execute("yield-visible-child", { |
| 450 | + message: "Wait for the visible dashboard child", |
| 451 | + }); |
| 452 | + expect(yieldResult.details).toEqual({ |
| 453 | + status: "yielded", |
| 454 | + message: "Wait for the visible dashboard child", |
| 455 | + }); |
| 456 | + expect(onYield).toHaveBeenCalledOnce(); |
| 457 | + |
| 458 | + expect( |
| 459 | + mod.settleRequesterAfterSessionSpawns({ |
| 460 | + requesterSessionKey: MAIN_REQUESTER_SESSION_KEY, |
| 461 | + requesterAgentId: "main", |
| 462 | + requesterTurnRunId, |
| 463 | + requesterYielded: true, |
| 464 | + acceptedSessionSpawns: [{ runId, childSessionKey }], |
| 465 | + }), |
| 466 | + ).toBe(true); |
| 467 | + expect( |
| 468 | + mod |
| 469 | + .listSubagentRunsForRequester(MAIN_REQUESTER_SESSION_KEY) |
| 470 | + .find((run) => run.runId === runId)?.execution.status, |
| 471 | + ).toBe("running"); |
| 472 | + expect(getAgentCalls()).toHaveLength(0); |
| 473 | + |
| 474 | + const endedAt = Date.now(); |
| 475 | + const terminalResult = { |
| 476 | + phase: "end", |
| 477 | + endedAt, |
| 478 | + terminalReply: { |
| 479 | + disposition: "visible" as const, |
| 480 | + text: "visible dashboard child complete", |
| 481 | + }, |
| 482 | + }; |
| 483 | + emitLifecycleEvent(runId, terminalResult, { sessionKey: childSessionKey }); |
| 484 | + await waitForAgentCallCount(1); |
| 485 | + await waitForDeliveredCleanup(runId); |
| 486 | + expect(getAgentCalls()).toHaveLength(1); |
| 487 | + expect(getRequesterWakeCalls()).toHaveLength(1); |
| 488 | + expect(getRequesterWakeCalls()[0]?.params).toMatchObject({ |
| 489 | + sessionKey: MAIN_REQUESTER_SESSION_KEY, |
| 490 | + message: expect.stringContaining("visible final answer"), |
| 491 | + }); |
| 492 | + |
| 493 | + emitLifecycleEvent(runId, terminalResult, { sessionKey: childSessionKey }); |
| 494 | + await vi.advanceTimersByTimeAsync(30_000); |
| 495 | + await flushAsync(); |
| 496 | + expect(getAgentCalls()).toHaveLength(1); |
| 497 | + expect(getRequesterWakeCalls()).toHaveLength(1); |
| 498 | + }); |
| 499 | + |
406 | 500 | it("does not replay a requester-owned final already delivered before its turn yields", async () => { |
407 | 501 | const requesterTurnRunId = "run-requester-already-delivered"; |
408 | 502 | const runId = "run-completed-before-yield"; |
|
0 commit comments