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:
@@ -132,6 +132,14 @@ export type RecordProcessCaptureEvent = (
|
||||
|
||||
/** Observed process-tree settlement and the cleanup required by that state. */
|
||||
export type ProcessSettlement =
|
||||
| VerifiedProcessSettlement
|
||||
| {
|
||||
readonly state: "quiesced";
|
||||
readonly elapsed_ms: number;
|
||||
readonly cleanup_outcome: "failed";
|
||||
};
|
||||
|
||||
export type VerifiedProcessSettlement =
|
||||
| {
|
||||
readonly state: "quiesced";
|
||||
readonly elapsed_ms: number;
|
||||
@@ -162,7 +170,19 @@ export interface UnverifiedProcessCapture {
|
||||
readonly comparison_contract: Readonly<Record<string, unknown>>;
|
||||
readonly full_scenario_sha256: string;
|
||||
readonly comparison_contract_sha256: string;
|
||||
readonly executable_sha256: string;
|
||||
/** Digest of the selected file sampled before spawn, when readable. */
|
||||
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;
|
||||
};
|
||||
readonly normalization_sha256: string;
|
||||
};
|
||||
readonly normalization: z.infer<typeof normalizationSchema>;
|
||||
@@ -174,7 +194,7 @@ export interface UnverifiedProcessCapture {
|
||||
readonly signal: number | null;
|
||||
readonly reason: "exited" | "timeout" | "idle_timeout";
|
||||
};
|
||||
readonly settlement: ProcessSettlement;
|
||||
readonly settlement: VerifiedProcessSettlement;
|
||||
readonly process_samples: readonly ProcessSample[];
|
||||
readonly filesystem_checkpoints: readonly FilesystemCheckpoint[];
|
||||
/**
|
||||
@@ -211,6 +231,66 @@ const fileStateShape = {
|
||||
mode: z.number().int().nonnegative(),
|
||||
size: z.number().int().nonnegative(),
|
||||
};
|
||||
|
||||
/** A resource cleanup result retained when observations cannot be verified. */
|
||||
export interface ProcessCaptureResourceCleanup {
|
||||
readonly state: "cleaned" | "failed" | "unverified" | "not_required";
|
||||
readonly reason: string | null;
|
||||
}
|
||||
|
||||
export interface ProcessCaptureCleanupReport {
|
||||
readonly owned_process_group: ProcessCaptureResourceCleanup;
|
||||
readonly terminal_renderer: ProcessCaptureResourceCleanup;
|
||||
readonly temporary_root: ProcessCaptureResourceCleanup;
|
||||
}
|
||||
|
||||
/** One observation field that may be unavailable when capture completion fails. */
|
||||
export type PartialProcessObservationField<T> =
|
||||
| { readonly state: "available"; readonly value: T }
|
||||
| { readonly state: "unavailable"; readonly reason: string };
|
||||
|
||||
/** Snapshot facts available before filesystem comparison can be completed. */
|
||||
/** Snapshot facts available before filesystem comparison can be completed. */
|
||||
export interface PartialFilesystemSnapshot {
|
||||
readonly files: readonly FileState[];
|
||||
readonly truncated: boolean;
|
||||
}
|
||||
|
||||
/** Available and unavailable fields when a run never becomes a full capture. */
|
||||
/** Available and unavailable fields when a run never becomes a full capture. */
|
||||
export interface IncompleteProcessCaptureObservations {
|
||||
readonly target_pid: PartialProcessObservationField<number>;
|
||||
readonly frames: PartialProcessObservationField<readonly TerminalFrame[]>;
|
||||
readonly rendered_frames: PartialProcessObservationField<
|
||||
readonly RenderedTerminalFrame[]
|
||||
>;
|
||||
readonly interaction_events: PartialProcessObservationField<
|
||||
readonly InteractionEvent[]
|
||||
>;
|
||||
readonly exit: PartialProcessObservationField<{
|
||||
readonly code: number | null;
|
||||
readonly signal: number | null;
|
||||
readonly reason: "exited" | "timeout" | "idle_timeout" | "cancelled";
|
||||
}>;
|
||||
readonly settlement: PartialProcessObservationField<{
|
||||
readonly state: "quiesced" | "alive_at_deadline" | "unverifiable";
|
||||
readonly elapsed_ms: number;
|
||||
}>;
|
||||
readonly process_samples: PartialProcessObservationField<
|
||||
readonly ProcessSample[]
|
||||
>;
|
||||
readonly filesystem_snapshots: {
|
||||
readonly before: PartialProcessObservationField<PartialFilesystemSnapshot>;
|
||||
readonly after: PartialProcessObservationField<PartialFilesystemSnapshot>;
|
||||
};
|
||||
readonly event_journal: PartialProcessObservationField<
|
||||
readonly ProcessCaptureEventJournalEntry[]
|
||||
>;
|
||||
readonly manifest: PartialProcessObservationField<
|
||||
UnverifiedProcessCapture["manifest"]
|
||||
>;
|
||||
}
|
||||
|
||||
const fileStateSchema = z.discriminatedUnion("type", [
|
||||
z.object({
|
||||
...fileStateShape,
|
||||
@@ -255,128 +335,309 @@ const fileEffectSchema = z.discriminatedUnion("status", [
|
||||
}),
|
||||
]);
|
||||
/** Exact serialized shape of a process capture. */
|
||||
const processCaptureShapeSchema: z.ZodType<UnverifiedProcessCapture> =
|
||||
z.strictObject({
|
||||
manifest: z.strictObject({
|
||||
rea_version: z.string().min(1),
|
||||
provider_version: z.string().min(1),
|
||||
platform: z.string().min(1),
|
||||
architecture: z.string().min(1),
|
||||
pty_backend: z.literal("node-pty"),
|
||||
started_at: z.iso.datetime(),
|
||||
completed_at: z.iso.datetime(),
|
||||
scenario: z.record(z.string(), jsonValueSchema),
|
||||
comparison_contract: z.record(z.string(), jsonValueSchema),
|
||||
full_scenario_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
comparison_contract_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
executable_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
normalization_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
const processSettlementSchema = z.discriminatedUnion("state", [
|
||||
z.object({
|
||||
state: z.literal("quiesced"),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.enum(["not_required", "failed"]),
|
||||
}),
|
||||
z.object({
|
||||
state: z.enum(["alive_at_deadline", "unverifiable"]),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.enum(["cleaned", "failed"]),
|
||||
}),
|
||||
]);
|
||||
|
||||
const processCaptureShapeSchema = z.strictObject({
|
||||
manifest: z.strictObject({
|
||||
rea_version: z.string().min(1),
|
||||
provider_version: z.string().min(1),
|
||||
platform: z.string().min(1),
|
||||
architecture: z.string().min(1),
|
||||
pty_backend: z.literal("node-pty"),
|
||||
started_at: z.iso.datetime(),
|
||||
completed_at: z.iso.datetime(),
|
||||
scenario: z.record(z.string(), jsonValueSchema),
|
||||
comparison_contract: z.record(z.string(), jsonValueSchema),
|
||||
full_scenario_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
comparison_contract_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
selected_executable_sha256: z
|
||||
.string()
|
||||
.regex(/^[a-f0-9]{64}$/u)
|
||||
.nullable(),
|
||||
executable_sha256: z
|
||||
.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(),
|
||||
}),
|
||||
normalization: normalizationSchema,
|
||||
frames: z.array(
|
||||
normalization_sha256: z.string().regex(/^[a-f0-9]{64}$/u),
|
||||
}),
|
||||
normalization: normalizationSchema,
|
||||
frames: z.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
data: z.string(),
|
||||
}),
|
||||
),
|
||||
rendered_frames: z.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
columns: z.number().int().positive(),
|
||||
rows: z.number().int().positive(),
|
||||
cursor_x: z.number().int().nonnegative(),
|
||||
cursor_y: z.number().int().nonnegative(),
|
||||
active_buffer: z.enum(["normal", "alternate"]),
|
||||
lines: z.array(z.string()),
|
||||
serialized_state: z.string(),
|
||||
}),
|
||||
),
|
||||
interaction_events: z.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
scheduled_at_ms: z.number().int().nonnegative(),
|
||||
dispatched_at_ms: z.number().int().nonnegative(),
|
||||
type: z.enum(["input", "resize", "signal"]),
|
||||
data: z.string(),
|
||||
outcome: z.enum(["dispatched", "target_exited", "failed"]),
|
||||
}),
|
||||
),
|
||||
exit: z.object({
|
||||
code: z.number().int().nullable(),
|
||||
signal: z.number().int().nullable(),
|
||||
reason: z.enum(["exited", "timeout", "idle_timeout"]),
|
||||
}),
|
||||
settlement: z.discriminatedUnion("state", [
|
||||
z.object({
|
||||
state: z.literal("quiesced"),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.literal("not_required"),
|
||||
}),
|
||||
z.object({
|
||||
state: z.enum(["alive_at_deadline", "unverifiable"]),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.enum(["cleaned", "failed"]),
|
||||
}),
|
||||
]),
|
||||
process_samples: z.array(
|
||||
z.object({
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
pid: z.number().int().positive(),
|
||||
parent_pid: z.number().int().nonnegative(),
|
||||
command: z.string(),
|
||||
process_group_id: z.number().int().positive().nullable(),
|
||||
session_id: z.number().int().nonnegative().nullable(),
|
||||
}),
|
||||
),
|
||||
filesystem_checkpoints: z.array(
|
||||
z.object({
|
||||
name: z.enum(["before", "after_settlement"]),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
files: z.array(fileStateSchema),
|
||||
effects: z.array(fileEffectSchema),
|
||||
truncated: z.boolean(),
|
||||
}),
|
||||
),
|
||||
event_journal: z
|
||||
.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
data: z.string(),
|
||||
capture_order: z.number().int().nonnegative(),
|
||||
collection: z.enum(PROCESS_CAPTURE_EVENT_COLLECTIONS),
|
||||
index: z.number().int().nonnegative(),
|
||||
}),
|
||||
),
|
||||
rendered_frames: z.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
columns: z.number().int().positive(),
|
||||
rows: z.number().int().positive(),
|
||||
cursor_x: z.number().int().nonnegative(),
|
||||
cursor_y: z.number().int().nonnegative(),
|
||||
active_buffer: z.enum(["normal", "alternate"]),
|
||||
lines: z.array(z.string()),
|
||||
serialized_state: z.string(),
|
||||
}),
|
||||
),
|
||||
interaction_events: z.array(
|
||||
z.object({
|
||||
sequence: z.number().int().nonnegative(),
|
||||
scheduled_at_ms: z.number().int().nonnegative(),
|
||||
dispatched_at_ms: z.number().int().nonnegative(),
|
||||
type: z.enum(["input", "resize", "signal"]),
|
||||
data: z.string(),
|
||||
outcome: z.enum(["dispatched", "target_exited", "failed"]),
|
||||
}),
|
||||
),
|
||||
exit: z.object({
|
||||
)
|
||||
.default([]),
|
||||
files_before: z.array(fileStateSchema),
|
||||
files_after: z.array(fileStateSchema),
|
||||
filesystem_effects: z.array(fileEffectSchema),
|
||||
truncated: z.boolean(),
|
||||
limitations: z.array(z.string()),
|
||||
residual_unknowns: z.array(
|
||||
z.object({
|
||||
scope: z.enum([
|
||||
"terminal",
|
||||
"interaction",
|
||||
"exit",
|
||||
"process",
|
||||
"filesystem",
|
||||
"cleanup",
|
||||
"network",
|
||||
"environment",
|
||||
]),
|
||||
reason: z.string(),
|
||||
}),
|
||||
),
|
||||
cleanup: z.object({
|
||||
owned_process_group: z.literal("verified"),
|
||||
temporary_root: z.literal("removed"),
|
||||
}),
|
||||
});
|
||||
|
||||
const processCleanupResourceSchema = z.strictObject({
|
||||
state: z.enum(["cleaned", "failed", "unverified", "not_required"]),
|
||||
reason: z.string().nullable(),
|
||||
});
|
||||
|
||||
const partialObservationFieldSchema = <Schema extends z.ZodType>(
|
||||
value: Schema,
|
||||
) =>
|
||||
z.discriminatedUnion("state", [
|
||||
z.strictObject({ state: z.literal("available"), value }),
|
||||
z.strictObject({ state: z.literal("unavailable"), reason: z.string() }),
|
||||
]);
|
||||
|
||||
const incompleteProcessCaptureObservationsSchema = z.strictObject({
|
||||
target_pid: partialObservationFieldSchema(z.number().int().positive()),
|
||||
frames: partialObservationFieldSchema(processCaptureShapeSchema.shape.frames),
|
||||
rendered_frames: partialObservationFieldSchema(
|
||||
processCaptureShapeSchema.shape.rendered_frames,
|
||||
),
|
||||
interaction_events: partialObservationFieldSchema(
|
||||
processCaptureShapeSchema.shape.interaction_events,
|
||||
),
|
||||
exit: partialObservationFieldSchema(
|
||||
z.strictObject({
|
||||
code: z.number().int().nullable(),
|
||||
signal: z.number().int().nullable(),
|
||||
reason: z.enum(["exited", "timeout", "idle_timeout"]),
|
||||
reason: z.enum(["exited", "timeout", "idle_timeout", "cancelled"]),
|
||||
}),
|
||||
settlement: z.discriminatedUnion("state", [
|
||||
z.object({
|
||||
state: z.literal("quiesced"),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.literal("not_required"),
|
||||
}),
|
||||
z.object({
|
||||
state: z.enum(["alive_at_deadline", "unverifiable"]),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
cleanup_outcome: z.enum(["cleaned", "failed"]),
|
||||
}),
|
||||
]),
|
||||
process_samples: z.array(
|
||||
z.object({
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
pid: z.number().int().positive(),
|
||||
parent_pid: z.number().int().nonnegative(),
|
||||
command: z.string(),
|
||||
process_group_id: z.number().int().positive().nullable(),
|
||||
session_id: z.number().int().nonnegative().nullable(),
|
||||
}),
|
||||
),
|
||||
filesystem_checkpoints: z.array(
|
||||
z.object({
|
||||
name: z.enum(["before", "after_settlement"]),
|
||||
at_ms: z.number().int().nonnegative(),
|
||||
),
|
||||
settlement: partialObservationFieldSchema(
|
||||
z.strictObject({
|
||||
state: z.enum(["quiesced", "alive_at_deadline", "unverifiable"]),
|
||||
elapsed_ms: z.number().int().nonnegative(),
|
||||
}),
|
||||
),
|
||||
process_samples: partialObservationFieldSchema(
|
||||
processCaptureShapeSchema.shape.process_samples,
|
||||
),
|
||||
filesystem_snapshots: z.strictObject({
|
||||
before: partialObservationFieldSchema(
|
||||
z.strictObject({
|
||||
files: z.array(fileStateSchema),
|
||||
effects: z.array(fileEffectSchema),
|
||||
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([]),
|
||||
files_before: z.array(fileStateSchema),
|
||||
files_after: z.array(fileStateSchema),
|
||||
filesystem_effects: z.array(fileEffectSchema),
|
||||
truncated: z.boolean(),
|
||||
limitations: z.array(z.string()),
|
||||
residual_unknowns: z.array(
|
||||
z.object({
|
||||
scope: z.enum([
|
||||
"terminal",
|
||||
"interaction",
|
||||
"exit",
|
||||
"process",
|
||||
"filesystem",
|
||||
"cleanup",
|
||||
"network",
|
||||
"environment",
|
||||
]),
|
||||
reason: z.string(),
|
||||
after: partialObservationFieldSchema(
|
||||
z.strictObject({
|
||||
files: z.array(fileStateSchema),
|
||||
truncated: z.boolean(),
|
||||
}),
|
||||
),
|
||||
cleanup: z.object({
|
||||
owned_process_group: z.literal("verified"),
|
||||
temporary_root: z.literal("removed"),
|
||||
}),
|
||||
}),
|
||||
event_journal: partialObservationFieldSchema(
|
||||
processCaptureShapeSchema.shape.event_journal,
|
||||
),
|
||||
manifest: partialObservationFieldSchema(
|
||||
processCaptureShapeSchema.shape.manifest,
|
||||
),
|
||||
});
|
||||
|
||||
const partialCleanupReportSchema = z.strictObject({
|
||||
owned_process_group: processCleanupResourceSchema,
|
||||
terminal_renderer: processCleanupResourceSchema,
|
||||
temporary_root: processCleanupResourceSchema,
|
||||
});
|
||||
const partialObservationMetadataSchema = z.strictObject({
|
||||
cleanup: partialCleanupReportSchema,
|
||||
execution_failure: z.string().nullable(),
|
||||
});
|
||||
const completedCaptureObservationSchema = z.strictObject({
|
||||
capture: processCaptureShapeSchema
|
||||
.omit({ cleanup: true })
|
||||
.extend({ settlement: processSettlementSchema }),
|
||||
});
|
||||
const incompleteCaptureObservationSchema = z.strictObject({
|
||||
observations: incompleteProcessCaptureObservationsSchema,
|
||||
});
|
||||
|
||||
/** Validated, non-comparable process observations attached to cleanup errors. */
|
||||
export const partialProcessCaptureObservationSchema = z
|
||||
.union([
|
||||
completedCaptureObservationSchema.extend(
|
||||
partialObservationMetadataSchema.shape,
|
||||
),
|
||||
incompleteCaptureObservationSchema.extend(
|
||||
partialObservationMetadataSchema.shape,
|
||||
),
|
||||
])
|
||||
.superRefine((partial, context) => {
|
||||
if (
|
||||
!Object.values(partial.cleanup).some(
|
||||
({ state }) => state === "failed" || state === "unverified",
|
||||
)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["cleanup"],
|
||||
message: "partial observations require incomplete resource cleanup",
|
||||
});
|
||||
|
||||
if ("observations" in partial) {
|
||||
const { observations } = partial;
|
||||
if (
|
||||
![
|
||||
observations.frames,
|
||||
observations.interaction_events,
|
||||
observations.process_samples,
|
||||
observations.filesystem_snapshots.before,
|
||||
observations.event_journal,
|
||||
].some(({ state }) => state === "available")
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["observations"],
|
||||
message: "incomplete observations contain no collected evidence",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const { capture } = partial;
|
||||
const settlement =
|
||||
capture.settlement.state === "quiesced"
|
||||
? {
|
||||
...capture.settlement,
|
||||
cleanup_outcome: "not_required" as const,
|
||||
}
|
||||
: { ...capture.settlement, cleanup_outcome: "cleaned" as const };
|
||||
const validationCapture: UnverifiedProcessCapture = {
|
||||
...capture,
|
||||
settlement,
|
||||
cleanup: {
|
||||
owned_process_group: "verified",
|
||||
temporary_root: "removed",
|
||||
},
|
||||
};
|
||||
for (const issue of collectProcessCaptureIssues(validationCapture))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["capture", ...issue.path.split(".")],
|
||||
message: issue.message,
|
||||
});
|
||||
});
|
||||
|
||||
/** One collected payload: completed capture data or explicit unavailable fields. */
|
||||
export type PartialProcessCaptureObservation = z.infer<
|
||||
typeof partialProcessCaptureObservationSchema
|
||||
>;
|
||||
|
||||
/** Exact serialized shape plus all process-capture semantic invariants. */
|
||||
export const processCaptureSchema = processCaptureShapeSchema
|
||||
.superRefine((capture, context) => {
|
||||
if (capture.settlement.cleanup_outcome === "failed")
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["settlement", "cleanup_outcome"],
|
||||
message: "verified captures cannot contain failed cleanup",
|
||||
});
|
||||
for (const issue of collectProcessCaptureIssues(capture))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
|
||||
@@ -65,7 +65,7 @@ it("rejects altered v4 commitments and accepts canonical key reordering", () =>
|
||||
executable_sha256: "f".repeat(64),
|
||||
},
|
||||
}),
|
||||
).toThrow("executable_sha256");
|
||||
).toThrow("executable_identity");
|
||||
});
|
||||
|
||||
it("rejects settlement and cleanup combinations that cannot occur", () => {
|
||||
@@ -142,6 +142,92 @@ it("accepts old captures without a journal and validates complete journals", ()
|
||||
}
|
||||
});
|
||||
|
||||
it("migrates persisted v3 executable digests without claiming a selected digest", () => {
|
||||
const capture = emptyCapture();
|
||||
const {
|
||||
selected_executable_sha256: _selectedExecutableSha256,
|
||||
executable_identity: _executableIdentity,
|
||||
executable_sha256,
|
||||
...legacyManifest
|
||||
} = 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",
|
||||
},
|
||||
}),
|
||||
).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,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("requires compatible contracts and enforces capture age through a clock seam", () => {
|
||||
const capture = emptyCapture();
|
||||
expect(() =>
|
||||
|
||||
@@ -25,7 +25,12 @@ export const EMPTY_PROCESS_CAPTURE_EXAMPLE = {
|
||||
comparison_contract: comparisonContract,
|
||||
full_scenario_sha256: digestProcessCommitment(scenario),
|
||||
comparison_contract_sha256: digestProcessCommitment(comparisonContract),
|
||||
selected_executable_sha256: "0".repeat(64),
|
||||
executable_sha256: "0".repeat(64),
|
||||
executable_identity: {
|
||||
state: "path_metadata_unchanged",
|
||||
reason: null,
|
||||
},
|
||||
normalization_sha256: digestProcessCommitment(normalization),
|
||||
},
|
||||
normalization,
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
processCaptureSchema,
|
||||
type UnverifiedProcessCapture,
|
||||
} from "./processCapture.js";
|
||||
import { z } from "zod";
|
||||
|
||||
declare class ProcessCaptureProof {
|
||||
private readonly verified: never;
|
||||
@@ -22,9 +23,47 @@ 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(input);
|
||||
const parsed = processCaptureSchema.safeParse(migrateLegacyV3Capture(input));
|
||||
if (parsed.success) return attachProof(parsed.data);
|
||||
const issues = parsed.error.issues.flatMap((issue) =>
|
||||
issue.code === "custom"
|
||||
|
||||
@@ -26,8 +26,23 @@ const validateCommitments = (
|
||||
require: RequireInvariant,
|
||||
): void => {
|
||||
const { manifest } = capture;
|
||||
require(manifest.scenario.executable_sha256 ===
|
||||
manifest.executable_sha256, "manifest.executable_sha256", "executable commitment does not match the scenario projection");
|
||||
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.executable_identity.state === "path_metadata_unchanged"
|
||||
? manifest.executable_sha256 === manifest.selected_executable_sha256 &&
|
||||
manifest.executable_identity.reason === null
|
||||
: manifest.executable_sha256 === null &&
|
||||
manifest.executable_identity.reason !==
|
||||
null, "manifest.executable_identity", "launch identity and executable digest do not agree");
|
||||
for (const [field, value] of [
|
||||
["full_scenario_sha256", manifest.scenario],
|
||||
["comparison_contract_sha256", manifest.comparison_contract],
|
||||
|
||||
Reference in New Issue
Block a user