|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createDeferred } from "../../../test/helpers/promise.js"; |
| 3 | +import { |
| 4 | + createSilentIdleArgv, |
| 5 | + createStubChildAdapter, |
| 6 | + createWriteStdoutArgv, |
| 7 | + spawnChild, |
| 8 | + type StubChildAdapter, |
| 9 | +} from "./supervisor.test-support.js"; |
| 10 | + |
| 11 | +const { createChildAdapterMock, createPtyAdapterMock } = vi.hoisted(() => ({ |
| 12 | + createChildAdapterMock: vi.fn(), |
| 13 | + createPtyAdapterMock: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("./adapters/child.js", () => ({ |
| 17 | + createChildAdapter: createChildAdapterMock, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("./adapters/pty.js", () => ({ |
| 21 | + createPtyAdapter: createPtyAdapterMock, |
| 22 | +})); |
| 23 | + |
| 24 | +let createProcessSupervisor: typeof import("./supervisor.js").createProcessSupervisor; |
| 25 | + |
| 26 | +describe("process supervisor scope extinction", () => { |
| 27 | + beforeEach(async () => { |
| 28 | + vi.resetModules(); |
| 29 | + ({ createProcessSupervisor } = await import("./supervisor.js")); |
| 30 | + createChildAdapterMock.mockReset(); |
| 31 | + createPtyAdapterMock.mockReset(); |
| 32 | + vi.useRealTimers(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + vi.useRealTimers(); |
| 37 | + vi.restoreAllMocks(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("keeps extinction waiting optional for ordinary child adapters", async () => { |
| 41 | + const adapter = createStubChildAdapter(); |
| 42 | + createChildAdapterMock.mockResolvedValue(adapter); |
| 43 | + |
| 44 | + const supervisor = createProcessSupervisor(); |
| 45 | + const run = await spawnChild(supervisor, { |
| 46 | + sessionId: "ordinary-child", |
| 47 | + scopeKey: "scope:ordinary-child", |
| 48 | + argv: createSilentIdleArgv(), |
| 49 | + }); |
| 50 | + |
| 51 | + expect(run.waitForExtinction).toBeUndefined(); |
| 52 | + const drain = supervisor.waitForScope("scope:ordinary-child"); |
| 53 | + const drained = vi.fn(); |
| 54 | + void drain.then(drained); |
| 55 | + await Promise.resolve(); |
| 56 | + expect(drained).not.toHaveBeenCalled(); |
| 57 | + |
| 58 | + adapter.settle(0); |
| 59 | + await expect(run.wait()).resolves.toMatchObject({ reason: "exit", exitCode: 0 }); |
| 60 | + await expect(drain).resolves.toBeUndefined(); |
| 61 | + expect(adapter.disposeMock).toHaveBeenCalledOnce(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("preserves root output when authoritative extinction settles first", async () => { |
| 65 | + const adapter = createStubChildAdapter(); |
| 66 | + adapter.oomScoreWrapperSelected = true; |
| 67 | + const extinction = createDeferred(); |
| 68 | + adapter.waitForExtinction = async () => await extinction.promise; |
| 69 | + createChildAdapterMock.mockResolvedValue(adapter); |
| 70 | + |
| 71 | + const supervisor = createProcessSupervisor(); |
| 72 | + const run = await spawnChild(supervisor, { |
| 73 | + sessionId: "s1", |
| 74 | + argv: createWriteStdoutArgv("ok"), |
| 75 | + timeoutMs: 1_000, |
| 76 | + stdinMode: "pipe-closed", |
| 77 | + }); |
| 78 | + |
| 79 | + expect(run.waitForExtinction).toBeTypeOf("function"); |
| 80 | + extinction.resolve(); |
| 81 | + await Promise.resolve(); |
| 82 | + expect(adapter.disposeMock).not.toHaveBeenCalled(); |
| 83 | + adapter.emitStdout("ok"); |
| 84 | + adapter.settle(0); |
| 85 | + |
| 86 | + const exit = await run.wait(); |
| 87 | + expect(exit.reason).toBe("exit"); |
| 88 | + expect(exit.exitCode).toBe(0); |
| 89 | + expect(exit.stdout).toBe("ok"); |
| 90 | + expect(exit.oomScoreWrapperSelected).toBe(true); |
| 91 | + expect(adapter.disposeMock).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it.each([ |
| 95 | + { outcome: "process-tree extinction", failure: false }, |
| 96 | + { outcome: "cleanup identity loss", failure: true }, |
| 97 | + ])("retains root-result cancellation ownership until $outcome", async ({ failure }) => { |
| 98 | + const extinction = createDeferred(); |
| 99 | + const adapter = Object.assign(createStubChildAdapter(), { |
| 100 | + waitForExtinction: () => extinction.promise, |
| 101 | + }); |
| 102 | + createChildAdapterMock.mockResolvedValue(adapter); |
| 103 | + const supervisor = createProcessSupervisor(); |
| 104 | + const run = await spawnChild(supervisor, { |
| 105 | + sessionId: "root-result-before-extinction", |
| 106 | + scopeKey: "scope:root-result-before-extinction", |
| 107 | + argv: createSilentIdleArgv(), |
| 108 | + }); |
| 109 | + expect(run.waitForExtinction).toBeTypeOf("function"); |
| 110 | + adapter.emitStdout("authentic root output"); |
| 111 | + adapter.settle(23); |
| 112 | + const root = await run.wait(); |
| 113 | + |
| 114 | + expect(root).toMatchObject({ reason: "exit", exitCode: 23, stdout: "authentic root output" }); |
| 115 | + expect(adapter.disposeMock).not.toHaveBeenCalled(); |
| 116 | + supervisor.cancelScope("scope:root-result-before-extinction"); |
| 117 | + expect(adapter.killMock).toHaveBeenCalledWith("SIGKILL"); |
| 118 | + expect(supervisor.getRecord(run.runId)).toMatchObject({ |
| 119 | + state: "exited", |
| 120 | + terminationReason: "exit", |
| 121 | + exitCode: 23, |
| 122 | + }); |
| 123 | + |
| 124 | + if (failure) { |
| 125 | + extinction.reject(new Error("cleanup identity lost")); |
| 126 | + await expect(run.waitForExtinction?.()).rejects.toThrow("cleanup identity lost"); |
| 127 | + } else { |
| 128 | + extinction.resolve(); |
| 129 | + await expect(run.waitForExtinction?.()).resolves.toBeUndefined(); |
| 130 | + } |
| 131 | + expect(adapter.disposeMock).toHaveBeenCalledOnce(); |
| 132 | + await expect(run.wait()).resolves.toBe(root); |
| 133 | + supervisor.cancel(run.runId); |
| 134 | + expect(adapter.killMock).toHaveBeenCalledOnce(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("drains cancelled startups and live siblings before reporting ownership failure", async () => { |
| 138 | + const first = createStubChildAdapter(); |
| 139 | + const sibling = createStubChildAdapter({ pid: 4321 }); |
| 140 | + const [firstExtinction, siblingExtinction] = [createDeferred(), createDeferred()]; |
| 141 | + first.waitForExtinction = async () => await firstExtinction.promise; |
| 142 | + sibling.waitForExtinction = async () => await siblingExtinction.promise; |
| 143 | + const startup = createDeferred<StubChildAdapter>(); |
| 144 | + createChildAdapterMock.mockReturnValueOnce(startup.promise).mockResolvedValueOnce(sibling); |
| 145 | + |
| 146 | + const supervisor = createProcessSupervisor(); |
| 147 | + const pending = ["failed-owner", "pending-owner"].map((sessionId) => |
| 148 | + spawnChild(supervisor, { |
| 149 | + sessionId, |
| 150 | + scopeKey: "scope:failed-drain", |
| 151 | + argv: createSilentIdleArgv(), |
| 152 | + }), |
| 153 | + ); |
| 154 | + supervisor.cancelScope("scope:failed-drain"); |
| 155 | + const drain = supervisor.waitForScope("scope:failed-drain"); |
| 156 | + startup.resolve(first); |
| 157 | + const runs = await Promise.all(pending); |
| 158 | + expect(first.killMock).toHaveBeenCalledWith("SIGTERM"); |
| 159 | + expect(sibling.killMock).toHaveBeenCalledWith("SIGTERM"); |
| 160 | + first.settle(0); |
| 161 | + sibling.settle(0); |
| 162 | + await Promise.all(runs.map((run) => run.wait())); |
| 163 | + |
| 164 | + const drained = vi.fn(); |
| 165 | + void drain.then(drained, drained); |
| 166 | + firstExtinction.reject(new Error("first owner lost authority")); |
| 167 | + await Promise.resolve(); |
| 168 | + expect(drained).not.toHaveBeenCalled(); |
| 169 | + expect(sibling.disposeMock).not.toHaveBeenCalled(); |
| 170 | + |
| 171 | + siblingExtinction.resolve(); |
| 172 | + await expect(drain).rejects.toThrow("first owner lost authority"); |
| 173 | + expect(sibling.disposeMock).toHaveBeenCalledTimes(1); |
| 174 | + }); |
| 175 | +}); |
0 commit comments