Skip to content

Commit 2d0bbeb

Browse files
committed
fix(agents): resume yielded requesters after visible spawns
1 parent 2f3f540 commit 2d0bbeb

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎src/agents/subagents/registry/subagent-registry.lifecycle-retry-grace.e2e.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
44
import type { SessionDeliveryState } from "../../../config/sessions/types.js";
55
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";
68
import { testing as subagentAnnounceDeliveryTesting } from "../announce/subagent-announce-delivery.test-support.js";
79
import { testing as subagentAnnounceOutputTesting } from "../announce/subagent-announce-output.test-support.js";
810
import { testing as subagentAnnounceTesting } from "../announce/subagent-announce.js";
@@ -403,6 +405,98 @@ describe("subagent registry lifecycle error grace", () => {
403405
});
404406
}
405407

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+
406500
it("does not replay a requester-owned final already delivered before its turn yields", async () => {
407501
const requesterTurnRunId = "run-requester-already-delivered";
408502
const runId = "run-completed-before-yield";

‎src/agents/tools/sessions-spawn-tool.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ describe("sessions_spawn tool", () => {
510510
const registerRun = vi.fn();
511511
const tool = createSessionsSpawnTool({
512512
agentSessionKey: "agent:main:main",
513+
requesterTurnRunId: "run-requester-visible-worktree",
513514
agentChannel: "slack",
514515
agentTo: "channel:C-stale",
515516
agentThreadId: "stale-thread",
@@ -567,6 +568,7 @@ describe("sessions_spawn tool", () => {
567568
expect(registerRun).toHaveBeenCalledWith(
568569
expect.objectContaining({
569570
runId: "run-visible",
571+
requesterTurnRunId: "run-requester-visible-worktree",
570572
childSessionKey: "agent:main:dashboard:child",
571573
requesterSessionKey: "agent:main:main",
572574
requesterOrigin: {

‎src/agents/tools/sessions-spawn-visible.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type VisibleSessionsSpawnDeps = {
6363

6464
type VisibleSessionsSpawnOptions = VisibleSessionsSpawnDeps & {
6565
agentSessionKey?: string;
66+
requesterTurnRunId?: string;
6667
completionOwnerKey?: string;
6768
agentChannel?: string;
6869
agentAccountId?: string;
@@ -381,6 +382,7 @@ export async function maybeSpawnVisibleSession(params: {
381382
try {
382383
(params.options?.registerRun ?? registerSubagentRun)({
383384
runId,
385+
requesterTurnRunId: params.options?.requesterTurnRunId,
384386
childSessionKey,
385387
controllerSessionKey: ownership.controllerSessionKey,
386388
requesterSessionKey: ownership.completionRequesterSessionKey,

0 commit comments

Comments
 (0)