diff --git a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts index 1d263755c2..18bfa4da2a 100644 --- a/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts +++ b/apps/server/src/provider/acp/AcpSessionRuntime.processTree.test.ts @@ -955,6 +955,43 @@ describe("terminatePosixOwnedProcessTree", () => { }), ); + it.live("detects an owned child first observed in the final survivor snapshot", () => + Effect.gen(function* () { + const lateChild = identity(111, 110, 111, 111); + const fixture = makeController({ + processes: [server(), identity(100, process.pid, 100, 100), identity(110, 100, 110, 110)], + onProcess: (processes, pid) => { + // Leave the descendant parent alive through teardown so it can fork + // immediately after the last signal pass. + if (pid !== 110) processes.delete(pid); + }, + }); + let snapshotCalls = 0; + const controller: AcpPosixProcessTreeController = { + ...fixture.controller, + snapshot: () => { + snapshotCalls += 1; + // Two TERM discovery passes, the root TERM pass, and two KILL passes + // precede the final survivor snapshot. + if (snapshotCalls === 6) fixture.processes.set(lateChild.pid, lateChild); + return fixture.controller.snapshot(); + }, + }; + + const error = yield* Effect.flip( + terminatePosixOwnedProcessTree({ + controller, + discoveryPasses: 2, + grace: 0, + rootPid: 100, + }), + ); + + expect(snapshotCalls).toBe(6); + expect(error.detail).toContain("110, 111"); + }), + ); + it.live("retains the ledger across root exit and an explicit failure for finalizer retry", () => Effect.gen(function* () { let ignoreSignals = true; diff --git a/apps/server/src/provider/acp/AcpSessionRuntime.ts b/apps/server/src/provider/acp/AcpSessionRuntime.ts index 7318da81c4..03f6e0ac14 100644 --- a/apps/server/src/provider/acp/AcpSessionRuntime.ts +++ b/apps/server/src/provider/acp/AcpSessionRuntime.ts @@ -970,7 +970,9 @@ export function terminatePosixOwnedProcessTree(input: { }); yield* Effect.sleep("10 millis"); } - const survivors = new Map(snapshot().map((entry) => [entry.pid, entry])); + const finalTable = snapshot(); + discover(finalTable); + const survivors = new Map(finalTable.map((entry) => [entry.pid, entry])); const residual = [...ledger.values()].filter((owned) => { const observed = survivors.get(owned.pid); if (!samePosixProcessIdentity(owned, observed) || observed === undefined) {