fix(process)!: require current captures and preserve cleanup failures
Remove v3 capture conversion and require explicit executable identity and event journals. Preserve cancellation, execution diagnostics, snapshots, and cleanup outcomes from preparation through CLI delivery.
This commit is contained in:
@@ -37,6 +37,7 @@ import { classifyFilesystemEffects } from "./ProcessFilesystemEffects.js";
|
||||
import {
|
||||
cleanupOwnedProcessGroup,
|
||||
verifyNoTokenOwnedProcesses,
|
||||
type OwnedProcessGroup,
|
||||
type ProcessCleanupResult,
|
||||
type ProcessOwnershipBaseline,
|
||||
} from "../ProcessOwnership.js";
|
||||
@@ -47,6 +48,7 @@ import {
|
||||
} from "../ProcessOwnershipObservation.js";
|
||||
import {
|
||||
describeProcessCaptureExecutionFailure,
|
||||
normalizeCaptureFailure,
|
||||
ProcessCaptureError,
|
||||
} from "./ProcessCaptureError.js";
|
||||
import { assertNotCancelled } from "./ProcessScenarioRuntimeValidation.js";
|
||||
@@ -685,6 +687,66 @@ const verifyTokenOwnedProcessesUntilSettled = async (
|
||||
return result;
|
||||
};
|
||||
|
||||
const releaseCapturedProcessGroup = async (
|
||||
ownership: OwnedProcessGroup,
|
||||
host: ProcessCaptureCleanupHost,
|
||||
): Promise<ProcessCaptureCleanupReport["owned_process_group"]> => {
|
||||
if (host.platform === "win32")
|
||||
return {
|
||||
state: "unverified",
|
||||
reason:
|
||||
"owned process cleanup is unverifiable on Windows without process-job authority",
|
||||
};
|
||||
let outcome: ProcessCaptureCleanupReport["owned_process_group"] = {
|
||||
state: "cleaned",
|
||||
reason: null,
|
||||
};
|
||||
const unverified: Array<{
|
||||
readonly pid: number;
|
||||
readonly diagnostic: string;
|
||||
}> = [];
|
||||
try {
|
||||
const cleaned = await host.cleanupProcessGroup(ownership);
|
||||
unverified.push(...(cleaned.unverified ?? []));
|
||||
if (!cleaned.cleaned) {
|
||||
outcome = { state: "unverified", reason: cleaned.reason };
|
||||
} else {
|
||||
const verified = await verifyTokenOwnedProcessesUntilSettled(() =>
|
||||
host.verifyTokenOwnedProcesses(
|
||||
ownership.runId,
|
||||
undefined,
|
||||
ownership.captureBaseline,
|
||||
ownership,
|
||||
),
|
||||
);
|
||||
unverified.push(...(verified.unverified ?? []));
|
||||
if (!verified.cleaned)
|
||||
outcome = { state: "unverified", reason: verified.reason };
|
||||
}
|
||||
} catch (cause: unknown) {
|
||||
outcome = {
|
||||
state: "failed",
|
||||
reason:
|
||||
cause instanceof Error
|
||||
? cause.message || cause.name || "owned process cleanup failed"
|
||||
: "owned process cleanup failed",
|
||||
};
|
||||
}
|
||||
return unverified.length === 0
|
||||
? outcome
|
||||
: {
|
||||
...outcome,
|
||||
unverified_processes: [
|
||||
...new Map(
|
||||
unverified.map(({ pid, diagnostic }) => [
|
||||
`${String(pid)}:${diagnostic}`,
|
||||
{ pid, reason: diagnostic },
|
||||
]),
|
||||
).values(),
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
export const releaseProcessResources = async (options: {
|
||||
readonly timers: ReadonlySet<ProcessTimer>;
|
||||
readonly terminal: Pick<IPty, "pid"> | undefined;
|
||||
@@ -712,68 +774,24 @@ export const releaseProcessResources = async (options: {
|
||||
: "renderer dispose failed",
|
||||
};
|
||||
}
|
||||
let ownedProcessGroup: ProcessCaptureCleanupReport["owned_process_group"] = {
|
||||
state: options.terminal === undefined ? "not_required" : "cleaned",
|
||||
reason: null,
|
||||
};
|
||||
if (options.terminal !== undefined) {
|
||||
if (host.platform === "win32") {
|
||||
ownedProcessGroup = {
|
||||
state: "unverified",
|
||||
reason:
|
||||
"owned process cleanup is unverifiable on Windows without process-job authority",
|
||||
};
|
||||
} else {
|
||||
const terminalPid = options.terminal.pid;
|
||||
const relation = {
|
||||
leaderPid: terminalPid,
|
||||
processGroupId: terminalPid,
|
||||
...(options.sampledProcessGroupIds === undefined
|
||||
? {}
|
||||
: { sampledProcessGroupIds: options.sampledProcessGroupIds }),
|
||||
};
|
||||
const cleaned = await host.cleanupProcessGroup({
|
||||
runId: options.runId,
|
||||
leaderPid: terminalPid,
|
||||
processGroupId: terminalPid,
|
||||
sweepTokenOwnedProcesses: true,
|
||||
...(options.sampledProcessGroupIds === undefined
|
||||
? {}
|
||||
: { sampledProcessGroupIds: options.sampledProcessGroupIds }),
|
||||
...(options.captureBaseline === undefined
|
||||
? {}
|
||||
: { captureBaseline: options.captureBaseline }),
|
||||
});
|
||||
const unverified = [...(cleaned.unverified ?? [])];
|
||||
if (!cleaned.cleaned) {
|
||||
ownedProcessGroup = { state: "unverified", reason: cleaned.reason };
|
||||
} else {
|
||||
const verified = await verifyTokenOwnedProcessesUntilSettled(() =>
|
||||
host.verifyTokenOwnedProcesses(
|
||||
options.runId,
|
||||
undefined,
|
||||
options.captureBaseline,
|
||||
relation,
|
||||
),
|
||||
const ownedProcessGroup =
|
||||
options.terminal === undefined
|
||||
? { state: "not_required" as const, reason: null }
|
||||
: await releaseCapturedProcessGroup(
|
||||
{
|
||||
runId: options.runId,
|
||||
leaderPid: options.terminal.pid,
|
||||
processGroupId: options.terminal.pid,
|
||||
sweepTokenOwnedProcesses: true,
|
||||
...(options.sampledProcessGroupIds === undefined
|
||||
? {}
|
||||
: { sampledProcessGroupIds: options.sampledProcessGroupIds }),
|
||||
...(options.captureBaseline === undefined
|
||||
? {}
|
||||
: { captureBaseline: options.captureBaseline }),
|
||||
},
|
||||
host,
|
||||
);
|
||||
unverified.push(...(verified.unverified ?? []));
|
||||
if (!verified.cleaned)
|
||||
ownedProcessGroup = { state: "unverified", reason: verified.reason };
|
||||
}
|
||||
if (unverified.length > 0)
|
||||
ownedProcessGroup = {
|
||||
...ownedProcessGroup,
|
||||
unverified_processes: [
|
||||
...new Map(
|
||||
unverified.map(({ pid, diagnostic }) => [
|
||||
`${String(pid)}:${diagnostic}`,
|
||||
{ pid, reason: diagnostic },
|
||||
]),
|
||||
).values(),
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
let temporaryRoot: ProcessCaptureCleanupReport["temporary_root"] = {
|
||||
state: "cleaned",
|
||||
reason: null,
|
||||
@@ -1018,16 +1036,36 @@ export const prepareProcessCapture = async (
|
||||
await host.prepareOwnershipInspector?.(signal);
|
||||
assertNotCancelled(signal);
|
||||
const before = await captureSnapshot(scenario, signal);
|
||||
const runId = randomUUID();
|
||||
const temporaryRoot = await host.createTemporaryRoot();
|
||||
try {
|
||||
assertNotCancelled(signal);
|
||||
const runId = randomUUID();
|
||||
const ownershipBaseline =
|
||||
(await host.captureOwnershipBaseline?.(signal)) ?? [];
|
||||
assertNotCancelled(signal);
|
||||
return { temporaryRoot, runId, ownershipBaseline, before };
|
||||
} catch (cause: unknown) {
|
||||
await host.cleanup(temporaryRoot);
|
||||
const cleanup = await releaseProcessResources({
|
||||
timers: new Set(),
|
||||
terminal: undefined,
|
||||
renderer: undefined,
|
||||
runId,
|
||||
temporaryRoot,
|
||||
host: { ...processCaptureCleanupHost, removeTemporaryRoot: host.cleanup },
|
||||
});
|
||||
resolveProcessResult(
|
||||
undefined,
|
||||
normalizeCaptureFailure(cause, signal),
|
||||
cleanup,
|
||||
createProcessCaptureObservationBuffer({
|
||||
frames: [],
|
||||
interactions: [],
|
||||
samples: [],
|
||||
eventJournal: [],
|
||||
before,
|
||||
}),
|
||||
{ scenario },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user