|
| 1 | +import { type FauxResponseStep, fauxAssistantMessage } from "@earendil-works/pi-ai"; |
| 2 | +import { afterEach, describe, expect, it } from "vitest"; |
| 3 | +import type { CustomMessage } from "../../../src/core/messages.js"; |
| 4 | +import { createHarness, getAssistantTexts, type Harness } from "../harness.js"; |
| 5 | + |
| 6 | +interface Gate { |
| 7 | + promise: Promise<void>; |
| 8 | + open: () => void; |
| 9 | +} |
| 10 | + |
| 11 | +function gate(): Gate { |
| 12 | + let open = () => {}; |
| 13 | + const promise = new Promise<void>((resolve) => { |
| 14 | + open = resolve; |
| 15 | + }); |
| 16 | + return { promise, open }; |
| 17 | +} |
| 18 | + |
| 19 | +/** Faux step that holds the provider stream open until the gate opens or the request aborts. */ |
| 20 | +function heldResponse(options: { record: () => void; release: Promise<void>; text: string }): FauxResponseStep { |
| 21 | + return async (_context, streamOptions) => { |
| 22 | + options.record(); |
| 23 | + const signal = streamOptions?.signal; |
| 24 | + await new Promise<void>((resolve) => { |
| 25 | + if (signal?.aborted) { |
| 26 | + resolve(); |
| 27 | + return; |
| 28 | + } |
| 29 | + signal?.addEventListener("abort", () => resolve(), { once: true }); |
| 30 | + void options.release.then(() => resolve()); |
| 31 | + }); |
| 32 | + return fauxAssistantMessage(options.text); |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function terminalNotices(messages: readonly unknown[]): CustomMessage[] { |
| 37 | + return messages.filter( |
| 38 | + (message): message is CustomMessage => |
| 39 | + typeof message === "object" && |
| 40 | + message !== null && |
| 41 | + (message as { role?: unknown }).role === "custom" && |
| 42 | + ((message as { customType?: unknown }).customType === "rlm_child_terminal_notice" || |
| 43 | + (message as { customType?: unknown }).customType === "rlm_child_failure"), |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +describe("issue #25 requestAbort cascades into active RLM child runs", () => { |
| 48 | + const harnesses: Harness[] = []; |
| 49 | + const gates: Gate[] = []; |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + while (gates.length > 0) { |
| 53 | + gates.pop()?.open(); |
| 54 | + } |
| 55 | + while (harnesses.length > 0) { |
| 56 | + harnesses.pop()?.cleanup(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + async function createParent(child: Harness): Promise<Harness> { |
| 61 | + const parent = await createHarness({ |
| 62 | + rlmDepth: 0, |
| 63 | + rlmMaxDepth: 1, |
| 64 | + subagentRuntimeHost: { |
| 65 | + createRlmSubagentRuntime: async () => ({ session: child.session }), |
| 66 | + deleteRlmSubagentRuntime: async () => {}, |
| 67 | + }, |
| 68 | + }); |
| 69 | + harnesses.push(parent); |
| 70 | + return parent; |
| 71 | + } |
| 72 | + |
| 73 | + it("terminates an in-flight child provider stream and settles the parent", async () => { |
| 74 | + const child = await createHarness(); |
| 75 | + harnesses.push(child); |
| 76 | + const release = gate(); |
| 77 | + gates.push(release); |
| 78 | + let childRequests = 0; |
| 79 | + child.setResponses([ |
| 80 | + heldResponse({ |
| 81 | + record: () => { |
| 82 | + childRequests++; |
| 83 | + }, |
| 84 | + release: release.promise, |
| 85 | + text: "child finished after the abort", |
| 86 | + }), |
| 87 | + fauxAssistantMessage("child must not start a second turn"), |
| 88 | + ]); |
| 89 | + const parent = await createParent(child); |
| 90 | + parent.setResponses([fauxAssistantMessage("parent recovered")]); |
| 91 | + |
| 92 | + const spawned = await parent.session.runRlmChild("long shard", { name: "cascade-worker" }); |
| 93 | + await expect.poll(() => childRequests).toBe(1); |
| 94 | + expect(parent.session.getRlmChildRunStatus(spawned.rlm_child_id)).toBe("running"); |
| 95 | + |
| 96 | + parent.session.requestAbort(); |
| 97 | + |
| 98 | + expect(parent.session.getRlmChildRunStatus(spawned.rlm_child_id)).toBe("cancelled"); |
| 99 | + // The run unwinds on its own once the child's provider stream is cut. |
| 100 | + await expect.poll(() => parent.session.getRlmChildRunStatus(spawned.rlm_child_id)).toBeUndefined(); |
| 101 | + expect(parent.session.hasRunningRlmChildren()).toBe(false); |
| 102 | + expect(child.session.isStreaming).toBe(false); |
| 103 | + // No further child request reached the provider. |
| 104 | + expect(child.faux.state.callCount).toBe(1); |
| 105 | + expect(child.getPendingResponseCount()).toBe(1); |
| 106 | + // A cancelled child does not report an outcome to the parent. |
| 107 | + expect(terminalNotices(parent.session.messages)).toEqual([]); |
| 108 | + expect(parent.session.getPendingNextTurnMessageSnapshots()).toEqual([]); |
| 109 | + |
| 110 | + // The cut leaves no quiescence waiter behind, and the next turn runs. |
| 111 | + await expect(parent.session.waitForRlmQuiescence()).resolves.toBeUndefined(); |
| 112 | + parent.session.resumeQueuedWork(); |
| 113 | + await parent.session.prompt("what happened?"); |
| 114 | + await expect(parent.session.waitForRlmQuiescence()).resolves.toBeUndefined(); |
| 115 | + expect(getAssistantTexts(parent)).toEqual(["parent recovered"]); |
| 116 | + }); |
| 117 | + |
| 118 | + it("leaves a retained background subagent running", async () => { |
| 119 | + const child = await createHarness(); |
| 120 | + harnesses.push(child); |
| 121 | + const release = gate(); |
| 122 | + gates.push(release); |
| 123 | + let childRequests = 0; |
| 124 | + child.setResponses([ |
| 125 | + fauxAssistantMessage("first shard done"), |
| 126 | + heldResponse({ |
| 127 | + record: () => { |
| 128 | + childRequests++; |
| 129 | + }, |
| 130 | + release: release.promise, |
| 131 | + text: "background work finished", |
| 132 | + }), |
| 133 | + ]); |
| 134 | + const parent = await createParent(child); |
| 135 | + parent.setResponses([fauxAssistantMessage("parent consumed the child result")]); |
| 136 | + |
| 137 | + const spawned = await parent.session.runRlmChild("first shard", { name: "retained-worker" }); |
| 138 | + await expect.poll(() => terminalNotices(parent.session.messages)).toHaveLength(1); |
| 139 | + const retained = parent.session.getRlmChildSession(spawned.rlm_child_id); |
| 140 | + expect(retained).toBe(child.session); |
| 141 | + |
| 142 | + // Retained children stay addressable, so their own work is background work the |
| 143 | + // parent turn does not own. |
| 144 | + const background = child.session.prompt("keep working in the background"); |
| 145 | + await expect.poll(() => childRequests).toBe(1); |
| 146 | + |
| 147 | + parent.session.requestAbort(); |
| 148 | + |
| 149 | + expect(child.session.isStreaming).toBe(true); |
| 150 | + release.open(); |
| 151 | + await background; |
| 152 | + expect(child.session.getLastAssistantText()).toBe("background work finished"); |
| 153 | + expect(child.faux.state.callCount).toBe(2); |
| 154 | + expect(parent.session.getRlmChildSession(spawned.rlm_child_id)).toBe(child.session); |
| 155 | + }); |
| 156 | +}); |
0 commit comments