fix(process): preserve partial evidence and verify detached cleanup (#1011)
* test(ci): allow transport delivery in source identity regression * fix(process): preserve partial evidence and verify detached cleanup * fix(process): scope Linux token sweeps by stable process identity * fix(process): retain environment permission failure codes * fix(process): wait for signaled processes before verifying cleanup * fix(process): verify sampled detached groups during cleanup * fix(electron): preserve prelaunch identities through cleanup * fix(process): retry growing Darwin process table snapshots * fix(process): retain compatibility with saved v3 capture evidence * fix(process): migrate saved v3 captures during reconstruction verification * fix(process): preserve cancellation during Darwin helper compilation * fix(process): propagate cancellation through ownership baselines * perf(javascript): cache canonical export comparison sort keys * fix(process): cancel provider ownership preparation before spawn * fix(process): isolate native compiler waiter cancellation
This commit is contained in:
@@ -1,11 +1,27 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { processScenarioSchema } from "../../domain/process/processScenario.js";
|
||||
import { AnalysisCapabilityUnavailableError } from "../../domain/analysisErrorCore.js";
|
||||
import { captureProcessScenario } from "./ProcessHarness.js";
|
||||
import {
|
||||
captureProcessScenario,
|
||||
normalizeCaptureFailure,
|
||||
ProcessCaptureError,
|
||||
} from "./ProcessHarness.js";
|
||||
import { settleProcessCaptureJournal } from "./ProcessCaptureLifecycle.js";
|
||||
import { processCaptureSchema } from "../../domain/process/processCapture.js";
|
||||
import {
|
||||
parseProcessCapture,
|
||||
partialProcessCaptureObservationSchema,
|
||||
processCaptureSchema,
|
||||
} from "../../domain/process/processCapture.js";
|
||||
import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "../../domain/process/processCapture.fixture.js";
|
||||
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
|
||||
import { emptyProcessCapture } from "../../domain/process/processCapture.fixture.js";
|
||||
import { analysisErrorProjectionSchema } from "../../contracts/errorSchemas.js";
|
||||
import {
|
||||
releaseProcessResources,
|
||||
resolveProcessResult,
|
||||
type ProcessCaptureCleanupHost,
|
||||
} from "./ProcessCaptureLifecycle.js";
|
||||
import { DarwinProcessOwnershipInspectionError } from "../DarwinProcessRunTokenReader.js";
|
||||
|
||||
it("rejects legacy replay output instead of silently discarding it", () => {
|
||||
expect(
|
||||
@@ -16,6 +32,21 @@ it("rejects legacy replay output instead of silently discarding it", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves the actionable Darwin ownership compiler prerequisite", () => {
|
||||
const normalized = normalizeCaptureFailure(
|
||||
new DarwinProcessOwnershipInspectionError(
|
||||
"macOS process ownership inspection requires the Apple Swift compiler via xcrun",
|
||||
),
|
||||
undefined,
|
||||
);
|
||||
|
||||
expect(normalized).toBeInstanceOf(ProcessCaptureError);
|
||||
expect(normalized).toMatchObject({
|
||||
message:
|
||||
"macOS process ownership inspection requires the Apple Swift compiler via xcrun",
|
||||
});
|
||||
});
|
||||
|
||||
it("waits for terminal observations delivered after the exit callback", async () => {
|
||||
const journal: Array<{
|
||||
capture_order: number;
|
||||
@@ -41,6 +72,265 @@ it("waits for terminal observations delivered after the exit callback", async ()
|
||||
expect(journal[1]).toMatchObject({ collection: "frames", index: 1 });
|
||||
});
|
||||
|
||||
it("keeps empty cleanup exception messages actionable in the report", async () => {
|
||||
const host: ProcessCaptureCleanupHost = {
|
||||
platform: process.platform,
|
||||
cleanupProcessGroup: async () => ({
|
||||
cleaned: false,
|
||||
reason: "unused process cleanup",
|
||||
}),
|
||||
verifyTokenOwnedProcesses: async () => ({
|
||||
cleaned: false,
|
||||
reason: "unused process cleanup",
|
||||
}),
|
||||
removeTemporaryRoot: async () => {
|
||||
throw new Error("");
|
||||
},
|
||||
};
|
||||
const report = await releaseProcessResources({
|
||||
timers: new Set(),
|
||||
terminal: undefined,
|
||||
renderer: undefined,
|
||||
runId: "fixture-run",
|
||||
temporaryRoot: "/fixture/root",
|
||||
host,
|
||||
});
|
||||
|
||||
expect(report.temporary_root).toEqual({ state: "failed", reason: "Error" });
|
||||
});
|
||||
|
||||
it("waits for token-owned processes to exit after one cleanup signal", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
let verificationCalls = 0;
|
||||
const exitsAt = Date.now() + 50;
|
||||
let cleanupCalls = 0;
|
||||
const host: ProcessCaptureCleanupHost = {
|
||||
platform: "linux",
|
||||
cleanupProcessGroup: async () => {
|
||||
cleanupCalls += 1;
|
||||
return { cleaned: true, signaled: true };
|
||||
},
|
||||
verifyTokenOwnedProcesses: async () => {
|
||||
verificationCalls += 1;
|
||||
return Date.now() < exitsAt
|
||||
? { cleaned: false, reason: "owned process is exiting" }
|
||||
: { cleaned: true, signaled: false };
|
||||
},
|
||||
removeTemporaryRoot: async () => undefined,
|
||||
};
|
||||
const result = releaseProcessResources({
|
||||
timers: new Set(),
|
||||
terminal: { pid: 321 },
|
||||
renderer: undefined,
|
||||
runId: "fixture-run",
|
||||
temporaryRoot: "/fixture/root",
|
||||
host,
|
||||
});
|
||||
|
||||
await vi.runAllTimersAsync();
|
||||
const report = await result;
|
||||
|
||||
expect(report.owned_process_group).toEqual({
|
||||
state: "cleaned",
|
||||
reason: null,
|
||||
});
|
||||
expect(cleanupCalls).toBe(1);
|
||||
expect(verificationCalls).toBeGreaterThan(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("retains the last ownership failure when the verification grace expires", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
let verificationCalls = 0;
|
||||
let cleanupCalls = 0;
|
||||
const host: ProcessCaptureCleanupHost = {
|
||||
platform: "linux",
|
||||
cleanupProcessGroup: async () => {
|
||||
cleanupCalls += 1;
|
||||
return { cleaned: true, signaled: true };
|
||||
},
|
||||
verifyTokenOwnedProcesses: async () => {
|
||||
verificationCalls += 1;
|
||||
return {
|
||||
cleaned: false,
|
||||
reason:
|
||||
verificationCalls === 1
|
||||
? "owned process has not exited yet"
|
||||
: "owned process remained after identity recheck",
|
||||
};
|
||||
},
|
||||
removeTemporaryRoot: async () => undefined,
|
||||
};
|
||||
const result = releaseProcessResources({
|
||||
timers: new Set(),
|
||||
terminal: { pid: 322 },
|
||||
renderer: undefined,
|
||||
runId: "fixture-run",
|
||||
temporaryRoot: "/fixture/root",
|
||||
host,
|
||||
});
|
||||
|
||||
await vi.runAllTimersAsync();
|
||||
const report = await result;
|
||||
|
||||
expect(report.owned_process_group).toEqual({
|
||||
state: "unverified",
|
||||
reason: "owned process remained after identity recheck",
|
||||
});
|
||||
expect(cleanupCalls).toBe(1);
|
||||
expect(verificationCalls).toBeGreaterThan(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("passes sampled detached groups into cleanup even when their token is unknown", async () => {
|
||||
let sampledGroups: readonly number[] | undefined;
|
||||
const host: ProcessCaptureCleanupHost = {
|
||||
platform: "linux",
|
||||
cleanupProcessGroup: async (ownership) => {
|
||||
sampledGroups = ownership.sampledProcessGroupIds;
|
||||
return {
|
||||
cleaned: false,
|
||||
reason: "sampled group has no verifiable run token",
|
||||
};
|
||||
},
|
||||
verifyTokenOwnedProcesses: async () => ({
|
||||
cleaned: true,
|
||||
signaled: false,
|
||||
}),
|
||||
removeTemporaryRoot: async () => undefined,
|
||||
};
|
||||
|
||||
const report = await releaseProcessResources({
|
||||
timers: new Set(),
|
||||
terminal: { pid: 321 },
|
||||
renderer: undefined,
|
||||
runId: "fixture-run",
|
||||
temporaryRoot: "/fixture/root",
|
||||
sampledProcessGroupIds: [654],
|
||||
host,
|
||||
});
|
||||
|
||||
expect(sampledGroups).toEqual([654]);
|
||||
expect(report.owned_process_group).toEqual({
|
||||
state: "unverified",
|
||||
reason: "sampled group has no verifiable run token",
|
||||
});
|
||||
});
|
||||
|
||||
it("retains observations and both causes when process cleanup is unverifiable", () => {
|
||||
const verified = emptyProcessCapture();
|
||||
const capture = parseProcessCapture({
|
||||
...verified,
|
||||
frames: [{ sequence: 0, at_ms: 0, data: "observed output" }],
|
||||
process_samples: [
|
||||
{
|
||||
at_ms: 1,
|
||||
pid: 654,
|
||||
parent_pid: 321,
|
||||
command: "sanitized-detached-child",
|
||||
process_group_id: 654,
|
||||
session_id: 654,
|
||||
},
|
||||
],
|
||||
event_journal: [],
|
||||
});
|
||||
const executionFailure = new Error("capture ended after a fixture error");
|
||||
const cleanup = {
|
||||
owned_process_group: {
|
||||
state: "unverified" as const,
|
||||
reason:
|
||||
"process ownership token could not be read for 1 live process(es): environment_unavailable=1",
|
||||
},
|
||||
terminal_renderer: { state: "cleaned" as const, reason: null },
|
||||
temporary_root: { state: "cleaned" as const, reason: null },
|
||||
};
|
||||
|
||||
let error: ProcessCaptureError | undefined;
|
||||
try {
|
||||
resolveProcessResult(capture, executionFailure, cleanup);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ProcessCaptureError)) throw cause;
|
||||
error = cause;
|
||||
}
|
||||
expect(error).toBeDefined();
|
||||
if (error === undefined) throw new Error("expected cleanup-incomplete error");
|
||||
|
||||
const projection = projectAnalysisError(error);
|
||||
const parsedProjection = analysisErrorProjectionSchema.parse(projection);
|
||||
expect(parsedProjection).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
cleanup_report: cleanup,
|
||||
execution_failure: "capture ended after a fixture error",
|
||||
partial_observation: {
|
||||
capture: {
|
||||
frames: [{ data: "observed output" }],
|
||||
process_samples: [{ pid: 654, process_group_id: 654 }],
|
||||
settlement: { cleanup_outcome: "failed" },
|
||||
},
|
||||
execution_failure: "capture ended after a fixture error",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(error.cause).toBe(executionFailure);
|
||||
const partialObservation = error.partialObservation;
|
||||
expect(partialObservation).toBeDefined();
|
||||
if (partialObservation === undefined)
|
||||
throw new Error("expected validated partial observation");
|
||||
expect(
|
||||
partialProcessCaptureObservationSchema.safeParse({
|
||||
...partialObservation,
|
||||
cleanup: {
|
||||
owned_process_group: { state: "cleaned", reason: null },
|
||||
terminal_renderer: { state: "cleaned", reason: null },
|
||||
temporary_root: { state: "cleaned", reason: null },
|
||||
},
|
||||
}).success,
|
||||
).toBe(false);
|
||||
if (!("capture" in partialObservation))
|
||||
throw new Error("expected completed partial capture observations");
|
||||
const partialCapture = partialObservation.capture;
|
||||
expect(() => parseProcessCapture(partialCapture)).toThrow();
|
||||
});
|
||||
|
||||
it("projects execution and cleanup failures when capture never completed", () => {
|
||||
const executionFailure = new Error("terminal startup failed");
|
||||
const cleanup = {
|
||||
owned_process_group: {
|
||||
state: "unverified" as const,
|
||||
reason: "process ownership token could not be read",
|
||||
},
|
||||
terminal_renderer: { state: "cleaned" as const, reason: null },
|
||||
temporary_root: { state: "cleaned" as const, reason: null },
|
||||
};
|
||||
|
||||
let error: ProcessCaptureError | undefined;
|
||||
try {
|
||||
resolveProcessResult(undefined, executionFailure, cleanup);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ProcessCaptureError)) throw cause;
|
||||
error = cause;
|
||||
}
|
||||
expect(error).toBeDefined();
|
||||
if (error === undefined) throw new Error("expected cleanup-incomplete error");
|
||||
|
||||
expect(error.partialObservation).toBeUndefined();
|
||||
expect(error.cause).toBe(executionFailure);
|
||||
expect(projectAnalysisError(error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
cleanup_report: cleanup,
|
||||
execution_failure: "terminal startup failed",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("fails closed on Windows before resolving or launching scenario paths", async () => {
|
||||
const scenario = processScenarioSchema.parse({
|
||||
executable: "Z:/missing/should-never-be-resolved.exe",
|
||||
|
||||
Reference in New Issue
Block a user