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:
morluto
2026-10-09 20:12:07 +08:00
parent abc4f47683
commit 2a7746998d
18 changed files with 419 additions and 353 deletions
+10 -25
View File
@@ -182,11 +182,6 @@ export interface UnverifiedProcessCapture {
readonly selected_executable_sha256: string | null;
/** Digest associated with the launch only when path metadata stayed stable across spawn. */
readonly executable_sha256: string | null;
/**
* Digest recorded by the original v3 format before selected and launch
* executable digests were distinguished. Kept only on migrated captures.
*/
readonly legacy_executable_sha256?: string | undefined;
readonly executable_identity: {
readonly state: "path_metadata_unchanged" | "unknown";
readonly reason: string | null;
@@ -205,12 +200,8 @@ export interface UnverifiedProcessCapture {
readonly settlement: VerifiedProcessSettlement;
readonly process_samples: readonly ProcessSample[];
readonly filesystem_checkpoints: readonly FilesystemCheckpoint[];
/**
* Global observation order across independently recorded collections.
*
* Absence is accepted for captures written before this journal existed.
*/
readonly event_journal?: readonly ProcessCaptureEventJournalEntry[];
/** Global observation order across independently recorded collections. */
readonly event_journal: readonly ProcessCaptureEventJournalEntry[];
readonly files_before: readonly FileState[];
readonly files_after: readonly FileState[];
readonly filesystem_effects: readonly FileEffect[];
@@ -401,10 +392,6 @@ const processCaptureShapeSchema = z.strictObject({
.string()
.regex(/^[a-f0-9]{64}$/u)
.nullable(),
legacy_executable_sha256: z
.string()
.regex(/^[a-f0-9]{64}$/u)
.optional(),
executable_identity: z.strictObject({
state: z.enum(["path_metadata_unchanged", "unknown"]),
reason: z.string().nullable(),
@@ -479,15 +466,13 @@ const processCaptureShapeSchema = z.strictObject({
truncated: z.boolean(),
}),
),
event_journal: z
.array(
z.object({
capture_order: z.number().int().nonnegative(),
collection: z.enum(PROCESS_CAPTURE_EVENT_COLLECTIONS),
index: z.number().int().nonnegative(),
}),
)
.default([]),
event_journal: z.array(
z.object({
capture_order: z.number().int().nonnegative(),
collection: z.enum(PROCESS_CAPTURE_EVENT_COLLECTIONS),
index: z.number().int().nonnegative(),
}),
),
files_before: z.array(fileStateSchema),
files_after: z.array(fileStateSchema),
filesystem_effects: z.array(fileEffectSchema),
@@ -681,7 +666,7 @@ export const processCaptureSchema = processCaptureShapeSchema
});
})
.describe(
"The capture must preserve its canonical scenario, comparison, and normalization SHA-256 commitments; ordered capture timestamps and contiguous sequence numbers; before and final filesystem snapshots with truncation propagated; and exit-code consistency with deadline termination. When parsing older input without an event journal, REA supplies an empty journal; empty journals are valid. A non-empty journal must reference every captured observation exactly once with unique in-range references. These cross-field invariants are checked by REA after capture.",
"The capture must preserve its canonical scenario, comparison, and normalization SHA-256 commitments; ordered capture timestamps and contiguous sequence numbers; before and final filesystem snapshots with truncation propagated; and exit-code consistency with deadline termination. The event journal is required; empty journals are valid. A non-empty journal must reference every captured observation exactly once with unique in-range references. These cross-field invariants are checked by REA after capture.",
);
export { parseProcessCapture } from "./processCaptureParsing.js";
@@ -3,7 +3,6 @@ import { expect, it } from "vitest";
import {
digestProcessCommitment,
parseProcessCapture,
type UnverifiedProcessCapture,
} from "./processCapture.js";
import {
compareUnverifiedProcessCaptures as compareProcessCaptures,
@@ -28,6 +27,7 @@ it("never considers truncated captures equivalent", () => {
exit: { code: 0, signal: null, reason: "exited" as const },
process_samples: [],
filesystem_checkpoints: emptyCapture().filesystem_checkpoints,
event_journal: [],
files_before: [],
files_after: [],
filesystem_effects: [],
@@ -92,10 +92,10 @@ it("rejects settlement and cleanup combinations that cannot occur", () => {
).toThrow("cleanup_outcome");
});
it("accepts old captures without a journal and validates complete journals", () => {
it("requires an explicit journal and validates complete journals", () => {
const capture = emptyCapture();
const { event_journal: _eventJournal, ...oldCapture } = capture;
expect(parseProcessCapture(oldCapture).event_journal).toEqual([]);
expect(() => parseProcessCapture(oldCapture)).toThrow("event_journal");
const eventJournal = [
{ capture_order: 0, collection: "filesystem_checkpoints", index: 0 },
@@ -142,90 +142,25 @@ it("accepts old captures without a journal and validates complete journals", ()
}
});
it("migrates persisted v3 executable digests without claiming a selected digest", () => {
it("rejects old executable identity representations without migration", () => {
const capture = emptyCapture();
const {
selected_executable_sha256: _selectedExecutableSha256,
executable_identity: _executableIdentity,
executable_sha256,
...legacyManifest
selected_executable_sha256: _selected,
executable_identity: _identity,
...oldManifest
} = capture.manifest;
const legacyCapture = {
...capture,
manifest: { ...legacyManifest, executable_sha256 },
};
const migrated = parseProcessCapture(legacyCapture);
expect(migrated.manifest).toMatchObject({
selected_executable_sha256: null,
executable_sha256: null,
executable_identity: {
state: "unknown",
reason: expect.stringContaining("did not distinguish"),
},
legacy_executable_sha256: executable_sha256,
scenario: capture.manifest.scenario,
full_scenario_sha256: capture.manifest.full_scenario_sha256,
});
expect(compareProcessCaptures(migrated, migrated)).toMatchObject({
status: "unchanged",
});
const exported = JSON.stringify(migrated);
expect(parseProcessCapture(JSON.parse(exported))).toEqual(migrated);
expect(() =>
parseProcessCapture({
...legacyCapture,
manifest: {
...legacyCapture.manifest,
executable_sha256: "f".repeat(64),
},
}),
).toThrow("legacy_executable_sha256");
const nullDigestScenario = {
...legacyCapture.manifest.scenario,
executable_sha256: null,
};
const malformedLegacyBase = {
...legacyCapture,
manifest: {
...legacyCapture.manifest,
executable_sha256: null,
scenario: nullDigestScenario,
full_scenario_sha256: digestProcessCommitment(nullDigestScenario),
},
};
expect(() => parseProcessCapture(malformedLegacyBase)).toThrow();
expect(() =>
parseProcessCapture({
...malformedLegacyBase,
manifest: {
...malformedLegacyBase.manifest,
executable_sha256: "not-a-digest",
},
}),
parseProcessCapture({ ...capture, manifest: oldManifest }),
).toThrow();
const {
executable_sha256: _missingLegacyDigest,
...manifestWithoutLegacyDigest
} = malformedLegacyBase.manifest;
expect(() =>
parseProcessCapture({
...malformedLegacyBase,
manifest: manifestWithoutLegacyDigest,
}),
).toThrow();
const { executable_identity: _missingIdentity, ...incompleteModernManifest } =
capture.manifest;
expect(() =>
parseProcessCapture({
...capture,
manifest: incompleteModernManifest,
manifest: {
...capture.manifest,
legacy_executable_sha256: capture.manifest.executable_sha256,
},
}),
).toThrow();
).toThrow("legacy_executable_sha256");
});
it("requires compatible contracts and enforces capture age through a clock seam", () => {
@@ -54,6 +54,7 @@ export const EMPTY_PROCESS_CAPTURE_EXAMPLE = {
truncated: false,
},
],
event_journal: [],
files_before: [],
files_after: [],
filesystem_effects: [],
+1 -40
View File
@@ -2,7 +2,6 @@ import {
processCaptureSchema,
type UnverifiedProcessCapture,
} from "./processCapture.js";
import { z } from "zod";
declare class ProcessCaptureProof {
private readonly verified: never;
@@ -23,47 +22,9 @@ const attachProof = (capture: UnverifiedProcessCapture): ProcessCapture => {
throw new TypeError("Process Capture proof ownership failed");
};
const recordSchema = z.record(z.string(), z.unknown());
const LEGACY_EXECUTABLE_IDENTITY_REASON =
"Legacy v3 capture did not distinguish selected and launch executable digests";
const migrateLegacyV3Capture = (input: unknown): unknown => {
const capture = recordSchema.safeParse(input);
if (!capture.success) return input;
const manifest = recordSchema.safeParse(capture.data.manifest);
if (!manifest.success) return input;
if (
manifest.data.provider_version !== "3" ||
"selected_executable_sha256" in manifest.data ||
"executable_identity" in manifest.data ||
"legacy_executable_sha256" in manifest.data
)
return input;
const { executable_sha256 } = manifest.data;
if (
typeof executable_sha256 !== "string" ||
!/^[a-f0-9]{64}$/u.test(executable_sha256)
)
return input;
return {
...capture.data,
manifest: {
...manifest.data,
selected_executable_sha256: null,
executable_sha256: null,
executable_identity: {
state: "unknown",
reason: LEGACY_EXECUTABLE_IDENTITY_REASON,
},
legacy_executable_sha256: executable_sha256,
},
};
};
/** Parse unknown input and reject invalid commitments or semantics. */
export const parseProcessCapture = (input: unknown): ProcessCapture => {
const parsed = processCaptureSchema.safeParse(migrateLegacyV3Capture(input));
const parsed = processCaptureSchema.safeParse(input);
if (parsed.success) return attachProof(parsed.data);
const issues = parsed.error.issues.flatMap((issue) =>
issue.code === "custom"
+3 -12
View File
@@ -26,17 +26,8 @@ const validateCommitments = (
require: RequireInvariant,
): void => {
const { manifest } = capture;
if (manifest.legacy_executable_sha256 !== undefined) {
require(manifest.selected_executable_sha256 === null &&
manifest.executable_sha256 === null &&
manifest.executable_identity.state === "unknown" &&
manifest.executable_identity.reason !== null &&
manifest.scenario.executable_sha256 ===
manifest.legacy_executable_sha256, "manifest.legacy_executable_sha256", "legacy executable digest must remain distinct from unknown selected and launch digests");
} else {
require(manifest.scenario.executable_sha256 ===
manifest.selected_executable_sha256, "manifest.selected_executable_sha256", "selected executable commitment does not match the scenario projection");
}
require(manifest.scenario.executable_sha256 ===
manifest.selected_executable_sha256, "manifest.selected_executable_sha256", "selected executable commitment does not match the scenario projection");
require(manifest.executable_identity.state === "path_metadata_unchanged"
? manifest.executable_sha256 === manifest.selected_executable_sha256 &&
manifest.executable_identity.reason === null
@@ -82,7 +73,7 @@ const validateEventJournal = (
capture: UnverifiedProcessCapture,
require: RequireInvariant,
): void => {
const journal = capture.event_journal ?? [];
const journal = capture.event_journal;
if (journal.length === 0) return;
const sizes = {
frames: capture.frames.length,
@@ -241,7 +241,7 @@ describe("process trace constraints and evidence sufficiency", () => {
expect(() =>
parseProcessCapture({
...complete,
event_journal: (complete.event_journal ?? []).slice(1),
event_journal: complete.event_journal.slice(1),
}),
).toThrow("event_journal");
});
+2 -2
View File
@@ -160,7 +160,7 @@ const collectRecords = (
sources: ReadonlySet<ProcessTraceSource>,
): readonly TraceRecord[] => {
const records: TraceRecord[] = [];
for (const location of capture.event_journal ?? []) {
for (const location of capture.event_journal) {
const record = projectProcessObservation(capture, location);
if (record !== null && sources.has(record.source))
records.push({
@@ -416,7 +416,7 @@ export const evaluateProcessTraceSide = (
capture.residual_unknowns.some(({ scope }) => relevantScopes.has(scope))
)
return { result: unknownSide(), diagnostic: null };
if ((capture.event_journal ?? []).length === 0)
if (capture.event_journal.length === 0)
return {
result: unknownSide(),
diagnostic: {