feat(process): add deterministic capture v3
This commit is contained in:
+192
-20
@@ -18,19 +18,44 @@ export const processCaptureComparisonSchema = z
|
||||
.object({
|
||||
status: comparisonStatusSchema,
|
||||
terminal: comparisonStatusSchema,
|
||||
interaction: comparisonStatusSchema,
|
||||
exit: comparisonStatusSchema,
|
||||
filesystem: comparisonStatusSchema,
|
||||
protocol: comparisonStatusSchema,
|
||||
process: comparisonStatusSchema,
|
||||
shim: comparisonStatusSchema,
|
||||
first_divergence: z.discriminatedUnion("status", [
|
||||
z.object({ status: z.literal("none") }),
|
||||
z.object({ status: z.literal("unknown"), reason: z.string() }),
|
||||
z.object({
|
||||
status: z.literal("found"),
|
||||
dimension: z.enum([
|
||||
"terminal",
|
||||
"interaction",
|
||||
"exit",
|
||||
"filesystem",
|
||||
"protocol",
|
||||
"process",
|
||||
"shim",
|
||||
]),
|
||||
index: z.number().int().nonnegative(),
|
||||
left_at_ms: z.number().int().nonnegative().nullable(),
|
||||
right_at_ms: z.number().int().nonnegative().nullable(),
|
||||
left: z.unknown().nullable(),
|
||||
right: z.unknown().nullable(),
|
||||
}),
|
||||
]),
|
||||
limitations: z.array(z.string()),
|
||||
})
|
||||
.superRefine((comparison, context) => {
|
||||
const dimensions = [
|
||||
comparison.terminal,
|
||||
comparison.interaction,
|
||||
comparison.exit,
|
||||
comparison.filesystem,
|
||||
comparison.protocol,
|
||||
comparison.process,
|
||||
comparison.shim,
|
||||
];
|
||||
const expected = dimensions.includes("truncated")
|
||||
? "truncated"
|
||||
@@ -50,12 +75,30 @@ export const processCaptureComparisonSchema = z
|
||||
});
|
||||
type ProcessCaptureComparison = z.infer<typeof processCaptureComparisonSchema>;
|
||||
|
||||
const stableFiles = (capture: ProcessCapture): string =>
|
||||
JSON.stringify(
|
||||
[...capture.files_after].sort((left, right) =>
|
||||
left.path.localeCompare(right.path),
|
||||
),
|
||||
);
|
||||
const terminalObservations = (capture: ProcessCapture): readonly unknown[] =>
|
||||
[
|
||||
...capture.frames.map((frame) => ({ kind: "raw" as const, ...frame })),
|
||||
...capture.rendered_frames.map((frame) => ({
|
||||
kind: "rendered" as const,
|
||||
...frame,
|
||||
})),
|
||||
].sort((left, right) => {
|
||||
const time = left.at_ms - right.at_ms;
|
||||
if (time !== 0) return time;
|
||||
if (left.kind !== right.kind) return left.kind === "raw" ? -1 : 1;
|
||||
return left.sequence - right.sequence;
|
||||
});
|
||||
|
||||
const filesystemObservations = (
|
||||
capture: ProcessCapture,
|
||||
): readonly unknown[] => [
|
||||
...capture.filesystem_checkpoints,
|
||||
{
|
||||
name: "final",
|
||||
files: capture.files_after,
|
||||
effects: capture.filesystem_effects,
|
||||
},
|
||||
];
|
||||
|
||||
const classifyCollection = (
|
||||
left: readonly unknown[],
|
||||
@@ -84,6 +127,75 @@ const comparisonStatus = (
|
||||
return dimensions.includes("unknown") ? "unknown" : "unchanged";
|
||||
};
|
||||
|
||||
type DivergenceDimension = Exclude<
|
||||
ProcessCaptureComparison["first_divergence"],
|
||||
{ readonly status: "none" | "unknown" }
|
||||
>["dimension"];
|
||||
|
||||
const eventTime = (value: unknown): number | null => {
|
||||
if (
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
"at_ms" in value &&
|
||||
typeof value.at_ms === "number" &&
|
||||
Number.isSafeInteger(value.at_ms) &&
|
||||
value.at_ms >= 0
|
||||
)
|
||||
return value.at_ms;
|
||||
return null;
|
||||
};
|
||||
|
||||
const firstCollectionDivergence = (
|
||||
dimension: DivergenceDimension,
|
||||
left: readonly unknown[],
|
||||
right: readonly unknown[],
|
||||
): Extract<
|
||||
ProcessCaptureComparison["first_divergence"],
|
||||
{ readonly status: "found" }
|
||||
> | null => {
|
||||
const length = Math.max(left.length, right.length);
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
const leftValue = left[index] ?? null;
|
||||
const rightValue = right[index] ?? null;
|
||||
if (JSON.stringify(leftValue) === JSON.stringify(rightValue)) continue;
|
||||
return {
|
||||
status: "found",
|
||||
dimension,
|
||||
index,
|
||||
left_at_ms: eventTime(leftValue),
|
||||
right_at_ms: eventTime(rightValue),
|
||||
left: leftValue,
|
||||
right: rightValue,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const chooseFirstDivergence = (
|
||||
candidates: ReadonlyArray<Extract<
|
||||
ProcessCaptureComparison["first_divergence"],
|
||||
{ readonly status: "found" }
|
||||
> | null>,
|
||||
): ProcessCaptureComparison["first_divergence"] => {
|
||||
const found = candidates.filter(
|
||||
(candidate): candidate is NonNullable<typeof candidate> =>
|
||||
candidate !== null,
|
||||
);
|
||||
return (
|
||||
found.sort((left, right) => {
|
||||
const leftTime = Math.min(
|
||||
left.left_at_ms ?? Number.MAX_SAFE_INTEGER,
|
||||
left.right_at_ms ?? Number.MAX_SAFE_INTEGER,
|
||||
);
|
||||
const rightTime = Math.min(
|
||||
right.left_at_ms ?? Number.MAX_SAFE_INTEGER,
|
||||
right.right_at_ms ?? Number.MAX_SAFE_INTEGER,
|
||||
);
|
||||
return leftTime - rightTime;
|
||||
})[0] ?? { status: "none" }
|
||||
);
|
||||
};
|
||||
|
||||
/** Compare bounded captures without equating missing or incompatible evidence. */
|
||||
export const compareProcessCaptures = (
|
||||
left: ProcessCapture,
|
||||
@@ -93,10 +205,16 @@ export const compareProcessCaptures = (
|
||||
return {
|
||||
status: "truncated",
|
||||
terminal: "truncated",
|
||||
interaction: "truncated",
|
||||
exit: "truncated",
|
||||
filesystem: "truncated",
|
||||
protocol: "truncated",
|
||||
process: "truncated",
|
||||
shim: "truncated",
|
||||
first_divergence: {
|
||||
status: "unknown",
|
||||
reason: "At least one capture is truncated.",
|
||||
},
|
||||
limitations: ["At least one capture is truncated."],
|
||||
};
|
||||
}
|
||||
@@ -112,8 +230,13 @@ export const compareProcessCaptures = (
|
||||
: classifyCollection(leftValues, rightValues);
|
||||
const terminal = classify(
|
||||
"terminal",
|
||||
left.frames.map(({ data }) => data),
|
||||
right.frames.map(({ data }) => data),
|
||||
terminalObservations(left),
|
||||
terminalObservations(right),
|
||||
);
|
||||
const interaction = classify(
|
||||
"terminal",
|
||||
left.interaction_events,
|
||||
right.interaction_events,
|
||||
);
|
||||
const exit =
|
||||
hasUnknown(left, "exit") || hasUnknown(right, "exit")
|
||||
@@ -121,17 +244,11 @@ export const compareProcessCaptures = (
|
||||
: JSON.stringify(left.exit) === JSON.stringify(right.exit)
|
||||
? "unchanged"
|
||||
: "changed";
|
||||
const filesystem =
|
||||
!hasUnknown(left, "filesystem") &&
|
||||
!hasUnknown(right, "filesystem") &&
|
||||
sameNormalization &&
|
||||
stableFiles(left) === stableFiles(right)
|
||||
? "unchanged"
|
||||
: classify(
|
||||
"filesystem",
|
||||
left.filesystem_effects,
|
||||
right.filesystem_effects,
|
||||
);
|
||||
const filesystem = classify(
|
||||
"filesystem",
|
||||
filesystemObservations(left),
|
||||
filesystemObservations(right),
|
||||
);
|
||||
const protocol = classify(
|
||||
"protocol",
|
||||
left.protocol_events,
|
||||
@@ -142,13 +259,68 @@ export const compareProcessCaptures = (
|
||||
left.process_samples,
|
||||
right.process_samples,
|
||||
);
|
||||
const shim = sameNormalization
|
||||
? classifyCollection(left.shim_events, right.shim_events)
|
||||
: "unknown";
|
||||
const observedFirstDivergence = chooseFirstDivergence([
|
||||
firstCollectionDivergence(
|
||||
"terminal",
|
||||
terminalObservations(left),
|
||||
terminalObservations(right),
|
||||
),
|
||||
firstCollectionDivergence(
|
||||
"interaction",
|
||||
left.interaction_events,
|
||||
right.interaction_events,
|
||||
),
|
||||
firstCollectionDivergence("exit", [left.exit], [right.exit]),
|
||||
firstCollectionDivergence(
|
||||
"filesystem",
|
||||
filesystemObservations(left),
|
||||
filesystemObservations(right),
|
||||
),
|
||||
firstCollectionDivergence(
|
||||
"protocol",
|
||||
left.protocol_events,
|
||||
right.protocol_events,
|
||||
),
|
||||
firstCollectionDivergence(
|
||||
"process",
|
||||
left.process_samples,
|
||||
right.process_samples,
|
||||
),
|
||||
firstCollectionDivergence("shim", left.shim_events, right.shim_events),
|
||||
]);
|
||||
const firstDivergence =
|
||||
observedFirstDivergence.status === "found"
|
||||
? observedFirstDivergence
|
||||
: !sameNormalization ||
|
||||
left.residual_unknowns.length > 0 ||
|
||||
right.residual_unknowns.length > 0
|
||||
? ({
|
||||
status: "unknown",
|
||||
reason:
|
||||
"Residual unknowns prevent proving that no divergence occurred.",
|
||||
} as const)
|
||||
: observedFirstDivergence;
|
||||
return {
|
||||
status: comparisonStatus([terminal, exit, filesystem, protocol, process]),
|
||||
status: comparisonStatus([
|
||||
terminal,
|
||||
interaction,
|
||||
exit,
|
||||
filesystem,
|
||||
protocol,
|
||||
process,
|
||||
shim,
|
||||
]),
|
||||
terminal,
|
||||
interaction,
|
||||
exit,
|
||||
filesystem,
|
||||
protocol,
|
||||
process,
|
||||
shim,
|
||||
first_divergence: firstDivergence,
|
||||
limitations: [
|
||||
...left.limitations,
|
||||
...right.limitations,
|
||||
|
||||
Reference in New Issue
Block a user