diff --git a/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.test.ts b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.test.ts index 2cde8e51642..e228bb1dc77 100644 --- a/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.test.ts +++ b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.test.ts @@ -2048,6 +2048,9 @@ describe("Codex ACPX runtime adapter", () => { resolveRetainedClose = resolve; }); vi.mocked(runtime.close).mockReturnValueOnce(retainedClose); + let finishPendingWork!: () => void; + const pendingWork = new Promise((resolve) => { finishPendingWork = resolve; }); + runtime.shutdown = vi.fn(() => pendingWork); const port = await openCodexAcpxRuntime(openOptions(fakeCommand()), { createRegistry: () => registry(), createStore: () => store(), @@ -2063,17 +2066,64 @@ describe("Codex ACPX runtime adapter", () => { await close; expect(runtime.close).toHaveBeenCalledOnce(); + expect(runtime.shutdown).not.toHaveBeenCalled(); + resolveRetainedClose(); await vi.advanceTimersByTimeAsync(0); + expect(runtime.shutdown).toHaveBeenCalledOnce(); + finishPendingWork(); await expect( port.close({ reason: "observe late success" }), ).resolves.toBeUndefined(); expect(runtime.close).toHaveBeenCalledOnce(); + expect(runtime.shutdown).toHaveBeenCalledOnce(); } finally { vi.useRealTimers(); } }); + it("does not finish closing until the runtime's abandoned turn work has stopped writing state", async () => { + const runtime = fakeRuntime(); + vi.mocked(runtime.close).mockResolvedValue(undefined); + let finishPendingWork!: () => void; + const pendingWork = new Promise((resolve) => { finishPendingWork = resolve; }); + runtime.shutdown = vi.fn(() => pendingWork); + const port = await openCodexAcpxRuntime(openOptions(fakeCommand()), { + createRegistry: () => registry(), + createStore: () => store(), + createRuntime: () => runtime, + }); + + let closed = false; + let rejoined = false; + const close = port.close({ reason: "provider died mid-turn" }).then(() => { closed = true; }); + await vi.waitFor(() => expect(runtime.shutdown).toHaveBeenCalledOnce()); + const rejoin = port.close({ reason: "second caller" }).then(() => { rejoined = true; }); + await new Promise((resolve) => setImmediate(resolve)); + expect([closed, rejoined]).toEqual([false, false]); + + finishPendingWork(); + await Promise.all([close, rejoin]); + expect(runtime.close).toHaveBeenCalledOnce(); + expect(runtime.shutdown).toHaveBeenCalledOnce(); + }); + + it("reports a runtime shutdown failure after the session itself closed", async () => { + const runtime = fakeRuntime(); + vi.mocked(runtime.close).mockResolvedValue(undefined); + runtime.shutdown = vi.fn().mockRejectedValue(new Error("owned client close failed")); + const port = await openCodexAcpxRuntime(openOptions(fakeCommand()), { + createRegistry: () => registry(), + createStore: () => store(), + createRuntime: () => runtime, + }); + + await expect(port.close({ reason: "close" })).rejects.toThrow("ACPX runtime and provider cleanup failed"); + await expect(port.close({ reason: "repeat" })).rejects.toThrow("ACPX runtime and provider cleanup failed"); + expect(runtime.close).toHaveBeenCalledOnce(); + expect(runtime.shutdown).toHaveBeenCalledOnce(); + }); + it("retains cleanup after guardian exit until provider exit is proven", async () => { const runtime = fakeRuntime(); const child = fakeChild(); diff --git a/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.ts b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.ts index 3680089046c..8823d182312 100644 --- a/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.ts +++ b/packages/paperclip-runner/src/drivers/acpx/acpx-runtime-adapter.ts @@ -1327,6 +1327,7 @@ function runtimePort( pendingExternalIntent: boolean; }; let runtimeClosed = false; + let runtimeShutdown: Promise | undefined; let providerProcessLost = false; let runtimeCloseAttempt: RuntimeCloseAttempt | undefined; let lateReconciliationOwner: Promise | undefined; @@ -1431,6 +1432,7 @@ function runtimePort( attempt.reconciliationGeneration, ); runtimeClosed = !hasUnreconciledLateFailure(); + if (runtimeClosed) void shutdownClosedRuntime(); } scheduleLateFailureReconciliation(); return; @@ -1451,6 +1453,11 @@ function runtimePort( }); }; + const shutdownClosedRuntime = (): Promise => { + runtimeShutdown ??= boundedCloseOutcome(ownedRuntimeShutdownOutcome(runtime), runtimeCloseTimeoutMs); + return runtimeShutdown; + }; + async function closeRuntime(input: { reason: string; reconciliation?: { @@ -1459,7 +1466,13 @@ function runtimePort( }; }): Promise { extensionBoundary.active?.controller.abort(new Error("ACPX runtime is closing")); - if (runtimeClosed) return; + if (runtimeClosed) { + const shutdownError = await shutdownClosedRuntime(); + if (shutdownError !== null) { + throw new AggregateError([shutdownError], "ACPX runtime and provider cleanup failed"); + } + return; + } if ( runtimeCloseAttempt?.origin.kind === "reconciliation" && input.reconciliation === undefined @@ -1526,9 +1539,10 @@ function runtimePort( } else { runtimeClosed = false; } + const shutdownError = runtimeClosed ? await shutdownClosedRuntime() : null; scheduleLateFailureReconciliation(); - if (closeError !== null || processErrors.length > 0) { - const errors = [closeError, ...processErrors].filter( + if (closeError !== null || processErrors.length > 0 || shutdownError !== null) { + const errors = [closeError, ...processErrors, shutdownError].filter( (error): error is unknown => error !== null, ); throw new AggregateError( @@ -1940,6 +1954,17 @@ function ownedRuntimeCloseOutcome( return retainRuntimeCleanupOwner(cleanup); } +function ownedRuntimeShutdownOutcome(runtime: AcpRuntime): Promise { + return retainRuntimeCleanupOwner( + Promise.resolve() + .then(() => runtime.shutdown?.()) + .then( + () => null, + (error: unknown) => error, + ), + ); +} + function retainRuntimeCleanupOwner(cleanup: Promise): Promise { activeRuntimeCleanupOwners.add(cleanup); void cleanup