Files
rea/src/process/capture/ProcessCaptureError.test.ts
T
morluto 974487f00f fix(process): preserve cleanup ownership through retries and close
Carry historical cleanup reports with the exact lease that can confirm
release. Bind originating capture errors and typed retry aggregates to
those owners while preserving primary failures and completed observations.

Keep those bindings through MCP close and compose mixed session/process
cleanup failures. Start both close operations even when a child throws a
typed failure synchronously.

Fixes #5144
Fixes #5163
Fixes #5166
2026-10-19 10:52:55 +08:00

174 lines
5.4 KiB
TypeScript

import { expect, it } from "vitest";
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { DarwinProcessOwnershipInspectionError } from "../DarwinProcessRunTokenReader.js";
import {
normalizeCaptureFailure,
ProcessCaptureError,
} from "./ProcessCaptureError.js";
import {
resolveProcessResult,
type ProcessCaptureCleanupOutcome,
} from "./ProcessCaptureLifecycle.js";
import type { ProcessCaptureCleanupReport } from "../../domain/process/processCapture.js";
const cleanupReport = {
owned_process_group: { state: "not_required" as const, reason: null },
terminal_renderer: { state: "not_required" as const, reason: null },
temporary_root: { state: "cleaned" as const, reason: null },
};
const historicalCleanupOutcome = (
report: ProcessCaptureCleanupReport,
): ProcessCaptureCleanupOutcome => ({ report, cleanupLease: undefined });
const fixture = () => {
const cleanup = new ProviderCleanupError(
"process-ownership",
["/owned/helper-root"],
{ reason: "controlled removal failure" },
);
const compiler = new DarwinProcessOwnershipInspectionError(
"compiler failed",
{
cleanupFailure: cleanup,
capturedOutput: {
stdout: "observed stdout",
stderr: "controlled compiler diagnostic",
truncated: false,
stdout_bytes: 15,
stderr_bytes: 30,
exit_code: 7,
signal: null,
},
compilerOutcome: {
outcome: "exited",
code: 7,
signal: null,
stderr: "controlled compiler diagnostic",
stderrBytes: 30,
stderrTruncated: false,
outputTruncated: false,
killed: false,
},
},
);
const error = normalizeCaptureFailure(compiler, undefined);
if (!(error instanceof ProcessCaptureError))
throw new Error("Expected normalized capture failure");
return { cleanup, compiler, error };
};
it("preserves the compiler output and live cleanup owner at capture normalization", () => {
const { cleanup, compiler, error } = fixture();
expect(error.cause).toBe(compiler);
expect(error).toMatchObject({
cleanupIncomplete: true,
cleanupResources: ["/owned/helper-root"],
capturedOutput: { stderr: "controlled compiler diagnostic", exit_code: 7 },
});
expect(projectAnalysisError(error)).toMatchObject({
code: "cleanup_incomplete",
});
cleanup.resolveCleanup();
expect(error.cleanupIncomplete).toBe(false);
expect(error.capturedOutput?.exit_code).toBe(7);
});
it("retains compiler facts and cleanup dependency through capture finalization", () => {
const { cleanup, error } = fixture();
let finalized: unknown;
try {
resolveProcessResult(
undefined,
error,
historicalCleanupOutcome(cleanupReport),
);
} catch (cause: unknown) {
finalized = cause;
}
expect(finalized).toBeInstanceOf(ProcessCaptureError);
expect(finalized).toMatchObject({
cleanupIncomplete: true,
capturedOutput: { stderr: "controlled compiler diagnostic", exit_code: 7 },
cleanupReport,
});
cleanup.resolveCleanup();
expect(finalized).toMatchObject({ cleanupIncomplete: false });
});
it("does not recreate unresolved cleanup after the exact owner already confirmed retry", () => {
const { cleanup, compiler } = fixture();
cleanup.resolveCleanup();
const normalized = normalizeCaptureFailure(compiler, undefined);
expect(normalized).toMatchObject({
cleanupIncomplete: false,
capturedOutput: { exit_code: 7 },
});
});
it("keeps later capture cleanup unresolved after compiler cleanup succeeds", () => {
const { cleanup, error } = fixture();
let finalized: unknown;
try {
resolveProcessResult(
undefined,
error,
historicalCleanupOutcome({
...cleanupReport,
temporary_root: {
state: "failed",
reason: "capture root removal denied",
},
}),
);
} catch (cause: unknown) {
finalized = cause;
}
expect(finalized).toMatchObject({
cleanupIncomplete: true,
capturedOutput: { exit_code: 7 },
cleanupResources: expect.arrayContaining([
"/owned/helper-root",
"temporary_root",
]),
});
cleanup.resolveCleanup();
expect(finalized).toMatchObject({ cleanupIncomplete: true });
});
it.each(["compiler first", "capture first"] as const)(
"keeps both cleanup owners through %s retry",
(order) => {
const { cleanup, error } = fixture();
const captureCleanup = new ProviderCleanupError(
"process-capture",
["/owned/capture-root"],
{ reason: "controlled capture root failure" },
);
const finalized = error.withFinalization({
cleanupFailure: captureCleanup,
cleanupReport,
});
const first = order === "compiler first" ? cleanup : captureCleanup;
const second = order === "compiler first" ? captureCleanup : cleanup;
first.resolveCleanup();
expect(finalized.cleanupIncomplete).toBe(true);
second.resolveCleanup();
expect(finalized.cleanupIncomplete).toBe(false);
expect(finalized.capturedOutput?.exit_code).toBe(7);
},
);
it("does not infer captured stdout from partial compiler diagnostics", () => {
const { compiler } = fixture();
const partial = new DarwinProcessOwnershipInspectionError(
"partial diagnostics",
{
compilerOutcome: compiler.compilerOutcome!,
},
);
expect(normalizeCaptureFailure(partial, undefined)).toMatchObject({
capturedOutput: undefined,
});
});