feat(process): compare declared concurrent traces
This commit is contained in:
@@ -84,25 +84,30 @@ type RuntimeStatus = z.infer<typeof processCaptureComparisonSchema>["status"];
|
||||
|
||||
const EXPECTED_PROVIDERS = {
|
||||
compare_process_captures: {
|
||||
predicate: "rea.process-comparison/v3",
|
||||
predicates: ["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
id: "rea-process",
|
||||
name: "REA deterministic process harness",
|
||||
version: "3",
|
||||
},
|
||||
compare_artifacts: {
|
||||
predicate: "rea.artifact-comparison/v1",
|
||||
predicates: ["rea.artifact-comparison/v1"],
|
||||
id: "rea-artifact-comparison",
|
||||
name: "REA artifact comparison",
|
||||
version: "1",
|
||||
},
|
||||
compare_functions: {
|
||||
predicate: "rea.function-comparison/v1",
|
||||
predicates: ["rea.function-comparison/v1"],
|
||||
id: "rea-function-comparison",
|
||||
name: "REA function comparison",
|
||||
version: "1",
|
||||
},
|
||||
} as const;
|
||||
|
||||
const acceptsPredicate = (
|
||||
expected: readonly string[],
|
||||
actual: string,
|
||||
): boolean => expected.some((predicate) => predicate === actual);
|
||||
|
||||
/** Find observed runtime changes and separately report static change candidates. */
|
||||
export const findChangedBehavior = (
|
||||
comparisonsInput: unknown,
|
||||
@@ -182,7 +187,7 @@ const parseComparisonEvidence = (input: unknown): Evidence => {
|
||||
.parse(evidence.operation);
|
||||
const expected = EXPECTED_PROVIDERS[operation];
|
||||
if (
|
||||
evidence.predicate_type !== expected.predicate ||
|
||||
!acceptsPredicate(expected.predicates, evidence.predicate_type) ||
|
||||
evidence.provider.id !== expected.id ||
|
||||
evidence.provider.name !== expected.name ||
|
||||
evidence.provider.version !== expected.version ||
|
||||
|
||||
@@ -5,10 +5,14 @@ import { validateProcessCapture } from "./processCapture.js";
|
||||
import { jsonValueSchema } from "./jsonValue.js";
|
||||
import {
|
||||
compareProcessTraces,
|
||||
processTraceOutcomesDiffer,
|
||||
processTraceComparisonResultSchema,
|
||||
type ProcessTraceSource,
|
||||
type ProcessTraceSpecification,
|
||||
} from "./processTraceComparison.js";
|
||||
import {
|
||||
dimensionsForTraceSources,
|
||||
traceCoversObservedDimension,
|
||||
} from "./processTraceDimensionProjection.js";
|
||||
|
||||
/** Comparison classification that never equates incomplete evidence. */
|
||||
export const comparisonStatusSchema = z.enum([
|
||||
@@ -276,82 +280,19 @@ const truncatedComparison = (): ProcessCaptureComparison => ({
|
||||
limitations: ["At least one capture is truncated."],
|
||||
});
|
||||
|
||||
const dimensionsForTraceSources = (
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
): ReadonlySet<(typeof PROCESS_COMPARISON_DIMENSIONS)[number]> => {
|
||||
const dimensions = new Set<(typeof PROCESS_COMPARISON_DIMENSIONS)[number]>();
|
||||
for (const source of sources) {
|
||||
if (source.startsWith("terminal_")) dimensions.add("terminal");
|
||||
else if (source === "interaction") dimensions.add("interaction");
|
||||
else if (source === "lifecycle") dimensions.add("exit");
|
||||
else if (source === "filesystem") dimensions.add("filesystem");
|
||||
else if (
|
||||
source === "http" ||
|
||||
source === "websocket" ||
|
||||
source === "replay_transition"
|
||||
)
|
||||
dimensions.add("protocol");
|
||||
else if (source === "process") dimensions.add("process");
|
||||
else if (source === "shim") dimensions.add("shim");
|
||||
}
|
||||
return dimensions;
|
||||
};
|
||||
|
||||
const traceCoversObservedDimension = (
|
||||
dimension: (typeof PROCESS_COMPARISON_DIMENSIONS)[number],
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
left: ProcessCapture,
|
||||
right: ProcessCapture,
|
||||
): boolean => {
|
||||
const any = <Value>(
|
||||
leftValues: readonly Value[],
|
||||
rightValues: readonly Value[],
|
||||
): boolean => leftValues.length > 0 || rightValues.length > 0;
|
||||
switch (dimension) {
|
||||
case "terminal":
|
||||
return (
|
||||
(!any(left.frames, right.frames) || sources.has("terminal_raw")) &&
|
||||
(!any(left.rendered_frames, right.rendered_frames) ||
|
||||
sources.has("terminal_rendered"))
|
||||
);
|
||||
case "interaction":
|
||||
return sources.has("interaction");
|
||||
case "exit":
|
||||
return sources.has("lifecycle");
|
||||
case "filesystem":
|
||||
return false;
|
||||
case "protocol":
|
||||
return (
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "http",
|
||||
) ||
|
||||
sources.has("http")) &&
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "websocket",
|
||||
) ||
|
||||
sources.has("websocket")) &&
|
||||
(!any(left.replay_transitions, right.replay_transitions) ||
|
||||
sources.has("replay_transition"))
|
||||
);
|
||||
case "process":
|
||||
return sources.has("process");
|
||||
case "shim":
|
||||
return sources.has("shim");
|
||||
}
|
||||
};
|
||||
|
||||
const applyTraceVerdict = (
|
||||
dimensions: ComparisonDimensions,
|
||||
specification: ProcessTraceSpecification,
|
||||
trace: ReturnType<typeof compareProcessTraces>,
|
||||
captures: readonly [left: ProcessCapture, right: ProcessCapture],
|
||||
): ComparisonDimensions => {
|
||||
const traceStatus: ComparisonStatus =
|
||||
trace.verdict === "equivalent"
|
||||
? "unchanged"
|
||||
: trace.verdict === "different"
|
||||
? "changed"
|
||||
: "unknown";
|
||||
const traceShowsDifference = processTraceOutcomesDiffer(trace);
|
||||
const traceStatus =
|
||||
trace.verdict === "unknown"
|
||||
? "unknown"
|
||||
: trace.verdict === "equivalent" || !traceShowsDifference
|
||||
? "unchanged"
|
||||
: "changed";
|
||||
const sources = new Set(specification.events.map(({ source }) => source));
|
||||
const covered = dimensionsForTraceSources(sources);
|
||||
const statusFor = (
|
||||
@@ -359,7 +300,8 @@ const applyTraceVerdict = (
|
||||
): ComparisonStatus => {
|
||||
const exact = dimensions[dimension];
|
||||
if (!covered.has(dimension)) return exact;
|
||||
if (trace.verdict === "different") return "changed";
|
||||
if (trace.verdict === "different")
|
||||
return traceShowsDifference ? "changed" : exact;
|
||||
if (
|
||||
trace.verdict === "unknown" &&
|
||||
["changed", "added", "removed"].includes(exact)
|
||||
|
||||
@@ -51,3 +51,11 @@ export const compareProcessTraces = (
|
||||
diagnostic,
|
||||
});
|
||||
};
|
||||
|
||||
/** Whether trace conformance or raw declared order differs across captures. */
|
||||
export const processTraceOutcomesDiffer = (
|
||||
comparison: ProcessTraceComparisonResult,
|
||||
): boolean =>
|
||||
comparison.left.status !== comparison.right.status ||
|
||||
JSON.stringify(comparison.left.raw_trace.map(({ event_id }) => event_id)) !==
|
||||
JSON.stringify(comparison.right.raw_trace.map(({ event_id }) => event_id));
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { ProcessCapture } from "./processCapture.js";
|
||||
import type { ProcessTraceSource } from "./processTraceSpecification.js";
|
||||
|
||||
export type ProcessComparisonDimension =
|
||||
| "terminal"
|
||||
| "interaction"
|
||||
| "exit"
|
||||
| "filesystem"
|
||||
| "protocol"
|
||||
| "process"
|
||||
| "shim";
|
||||
|
||||
/** Map declared trace sources to legacy process-comparison dimensions. */
|
||||
export const dimensionsForTraceSources = (
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
): ReadonlySet<ProcessComparisonDimension> => {
|
||||
const dimensions = new Set<ProcessComparisonDimension>();
|
||||
for (const source of sources) {
|
||||
if (source.startsWith("terminal_")) dimensions.add("terminal");
|
||||
else if (source === "interaction") dimensions.add("interaction");
|
||||
else if (source === "lifecycle") dimensions.add("exit");
|
||||
else if (source === "filesystem") dimensions.add("filesystem");
|
||||
else if (
|
||||
source === "http" ||
|
||||
source === "websocket" ||
|
||||
source === "replay_transition"
|
||||
)
|
||||
dimensions.add("protocol");
|
||||
else if (source === "process") dimensions.add("process");
|
||||
else if (source === "shim") dimensions.add("shim");
|
||||
}
|
||||
return dimensions;
|
||||
};
|
||||
|
||||
/** Whether a trace specification covers every observed source in a dimension. */
|
||||
export const traceCoversObservedDimension = (
|
||||
dimension: ProcessComparisonDimension,
|
||||
sources: ReadonlySet<ProcessTraceSource>,
|
||||
left: ProcessCapture,
|
||||
right: ProcessCapture,
|
||||
): boolean => {
|
||||
const any = <Value>(
|
||||
leftValues: readonly Value[],
|
||||
rightValues: readonly Value[],
|
||||
): boolean => leftValues.length > 0 || rightValues.length > 0;
|
||||
switch (dimension) {
|
||||
case "terminal":
|
||||
return (
|
||||
(!any(left.frames, right.frames) || sources.has("terminal_raw")) &&
|
||||
(!any(left.rendered_frames, right.rendered_frames) ||
|
||||
sources.has("terminal_rendered"))
|
||||
);
|
||||
case "interaction":
|
||||
return sources.has("interaction");
|
||||
case "exit":
|
||||
return sources.has("lifecycle");
|
||||
case "filesystem":
|
||||
return false;
|
||||
case "protocol":
|
||||
return (
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "http",
|
||||
) ||
|
||||
sources.has("http")) &&
|
||||
(![...left.protocol_events, ...right.protocol_events].some(
|
||||
({ protocol }) => protocol === "websocket",
|
||||
) ||
|
||||
sources.has("websocket")) &&
|
||||
(!any(left.replay_transitions, right.replay_transitions) ||
|
||||
sources.has("replay_transition"))
|
||||
);
|
||||
case "process":
|
||||
return sources.has("process");
|
||||
case "shim":
|
||||
return sources.has("shim");
|
||||
}
|
||||
};
|
||||
@@ -43,6 +43,21 @@ const eventDeclarationSchema = z.strictObject({
|
||||
id: identifierSchema,
|
||||
source: processTraceSourceSchema,
|
||||
exact: jsonValueSchema,
|
||||
ignore_fields: z
|
||||
.array(
|
||||
z.enum([
|
||||
"sequence",
|
||||
"at_ms",
|
||||
"scheduled_at_ms",
|
||||
"dispatched_at_ms",
|
||||
"elapsed_ms",
|
||||
]),
|
||||
)
|
||||
.max(5)
|
||||
.refine((values) => new Set(values).size === values.length, {
|
||||
message: "ignored event fields must be unique",
|
||||
})
|
||||
.optional(),
|
||||
cardinality: cardinalitySchema.default({ kind: "required" }),
|
||||
});
|
||||
|
||||
@@ -57,6 +72,15 @@ const partialOrderLanguageSchema = z.strictObject({
|
||||
)
|
||||
.max(4_096)
|
||||
.default([]),
|
||||
not_before: z
|
||||
.array(
|
||||
z.strictObject({
|
||||
event: identifierSchema,
|
||||
anchor: identifierSchema,
|
||||
}),
|
||||
)
|
||||
.max(4_096)
|
||||
.default([]),
|
||||
unordered_groups: z
|
||||
.array(
|
||||
z.strictObject({ events: z.array(identifierSchema).min(2).max(256) }),
|
||||
@@ -106,6 +130,23 @@ export const canonicalTraceJson = (value: unknown): string => {
|
||||
return serialized;
|
||||
};
|
||||
|
||||
/** Remove only explicitly declared schedule metadata before exact matching. */
|
||||
export const comparableTracePayload = (
|
||||
value: unknown,
|
||||
ignoredFields: readonly string[] = [],
|
||||
): unknown => {
|
||||
if (
|
||||
ignoredFields.length === 0 ||
|
||||
typeof value !== "object" ||
|
||||
value === null ||
|
||||
Array.isArray(value)
|
||||
)
|
||||
return value;
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).filter(([name]) => !ignoredFields.includes(name)),
|
||||
);
|
||||
};
|
||||
|
||||
export const processTraceCardinalityBounds = (
|
||||
cardinality: ProcessTraceSpecification["events"][number]["cardinality"],
|
||||
): readonly [number, number] => {
|
||||
@@ -156,6 +197,18 @@ const buildAdjacency = (
|
||||
});
|
||||
adjacency.get(edge.before)?.add(edge.after);
|
||||
}
|
||||
for (const [index, constraint] of language.not_before.entries()) {
|
||||
const path = ["language", "not_before", index];
|
||||
addReferenceIssue(context, known, constraint.event, [...path, "event"]);
|
||||
addReferenceIssue(context, known, constraint.anchor, [...path, "anchor"]);
|
||||
if (constraint.event === constraint.anchor)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "not-before constraints must reference distinct events",
|
||||
path,
|
||||
});
|
||||
adjacency.get(constraint.anchor)?.add(constraint.event);
|
||||
}
|
||||
return adjacency;
|
||||
};
|
||||
|
||||
@@ -274,7 +327,7 @@ const validatePartialOrder = (
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "happens-before graph must be acyclic",
|
||||
path: ["language", "happens_before"],
|
||||
path: ["language"],
|
||||
});
|
||||
const reachable = computeReachability(ids, adjacency);
|
||||
const unorderedPairs = validateUnorderedGroups(
|
||||
@@ -309,6 +362,7 @@ const validateFiniteTraces = (
|
||||
if (specification.language.kind !== "finite_traces") return;
|
||||
const known = new Set(specification.events.map(({ id }) => id));
|
||||
const variantIds = new Set<string>();
|
||||
const variantTraces = new Set<string>();
|
||||
for (const [
|
||||
variantIndex,
|
||||
variant,
|
||||
@@ -321,6 +375,14 @@ const validateFiniteTraces = (
|
||||
path: [...variantPath, "id"],
|
||||
});
|
||||
variantIds.add(variant.id);
|
||||
const canonicalTrace = canonicalTraceJson(variant.trace);
|
||||
if (variantTraces.has(canonicalTrace))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "finite trace variants must not duplicate a trace",
|
||||
path: [...variantPath, "trace"],
|
||||
});
|
||||
variantTraces.add(canonicalTrace);
|
||||
for (const [tokenIndex, token] of variant.trace.entries()) {
|
||||
const values = typeof token === "string" ? [token] : token.unordered;
|
||||
for (const [eventIndex, id] of values.entries())
|
||||
@@ -352,6 +414,7 @@ export const processTraceSpecificationSchema =
|
||||
specificationShapeSchema.superRefine((specification, context) => {
|
||||
const ids = new Set<string>();
|
||||
const predicates = new Set<string>();
|
||||
const ignoredFieldsBySource = new Map<string, string>();
|
||||
for (const [index, event] of specification.events.entries()) {
|
||||
if (ids.has(event.id))
|
||||
context.addIssue({
|
||||
@@ -360,7 +423,45 @@ export const processTraceSpecificationSchema =
|
||||
path: ["events", index, "id"],
|
||||
});
|
||||
ids.add(event.id);
|
||||
const predicate = `${event.source}\0${canonicalTraceJson(event.exact)}`;
|
||||
const ignoredFields = [...(event.ignore_fields ?? [])].sort();
|
||||
if (
|
||||
ignoredFields.length > 0 &&
|
||||
(typeof event.exact !== "object" ||
|
||||
event.exact === null ||
|
||||
Array.isArray(event.exact))
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "ignored schedule fields require an exact object payload",
|
||||
path: ["events", index, "ignore_fields"],
|
||||
});
|
||||
const ignoredKey = canonicalTraceJson(ignoredFields);
|
||||
const priorIgnoredKey = ignoredFieldsBySource.get(event.source);
|
||||
if (priorIgnoredKey !== undefined && priorIgnoredKey !== ignoredKey)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message:
|
||||
"events from one source must use the same ignored schedule fields",
|
||||
path: ["events", index, "ignore_fields"],
|
||||
});
|
||||
ignoredFieldsBySource.set(event.source, ignoredKey);
|
||||
if (
|
||||
ignoredFields.some(
|
||||
(field) =>
|
||||
typeof event.exact === "object" &&
|
||||
event.exact !== null &&
|
||||
!Array.isArray(event.exact) &&
|
||||
field in event.exact,
|
||||
)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "exact payload must omit every ignored schedule field",
|
||||
path: ["events", index, "exact"],
|
||||
});
|
||||
const predicate = `${event.source}\0${canonicalTraceJson(
|
||||
comparableTracePayload(event.exact, ignoredFields),
|
||||
)}`;
|
||||
if (predicates.has(predicate))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
|
||||
@@ -46,24 +46,29 @@ const MAX_CANONICAL_BYTES = 32 * 1024 * 1024;
|
||||
const providers = {
|
||||
behavioral: {
|
||||
operation: "compare_process_captures",
|
||||
predicate: "rea.process-comparison/v3",
|
||||
predicates: ["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
id: "rea-process",
|
||||
name: "REA deterministic process harness",
|
||||
},
|
||||
"structural-function": {
|
||||
operation: "compare_functions",
|
||||
predicate: "rea.function-comparison/v1",
|
||||
predicates: ["rea.function-comparison/v1"],
|
||||
id: "rea-function-comparison",
|
||||
name: "REA function comparison",
|
||||
},
|
||||
"structural-artifact": {
|
||||
operation: "compare_artifacts",
|
||||
predicate: "rea.artifact-comparison/v1",
|
||||
predicates: ["rea.artifact-comparison/v1"],
|
||||
id: "rea-artifact-comparison",
|
||||
name: "REA artifact comparison",
|
||||
},
|
||||
} as const;
|
||||
|
||||
const acceptsPredicate = (
|
||||
expected: readonly string[],
|
||||
actual: string,
|
||||
): boolean => expected.some((predicate) => predicate === actual);
|
||||
|
||||
/** Verify only explicitly declared claims; no global equivalence is inferred. */
|
||||
export const verifyReconstruction = (
|
||||
specificationInput: unknown,
|
||||
@@ -229,7 +234,7 @@ const validateComparisonIdentity = (claim: Claim, evidence: Evidence): void => {
|
||||
const expected = providers[claim.kind];
|
||||
if (
|
||||
evidence.operation !== expected.operation ||
|
||||
evidence.predicate_type !== expected.predicate ||
|
||||
!acceptsPredicate(expected.predicates, evidence.predicate_type) ||
|
||||
evidence.provider.id !== expected.id ||
|
||||
evidence.provider.name !== expected.name ||
|
||||
evidence.provider.version !== (claim.kind === "behavioral" ? "3" : "1") ||
|
||||
|
||||
@@ -160,7 +160,7 @@ const EXPECTED = {
|
||||
"REA function comparison",
|
||||
],
|
||||
compare_process_captures: [
|
||||
"rea.process-comparison/v3",
|
||||
["rea.process-comparison/v3", "rea.process-comparison/v4"],
|
||||
"rea-process",
|
||||
"REA deterministic process harness",
|
||||
],
|
||||
@@ -405,7 +405,9 @@ const assertComparisonIdentity = (evidence: Evidence): void => {
|
||||
const expected = EXPECTED[evidence.operation as keyof typeof EXPECTED];
|
||||
if (
|
||||
expected === undefined ||
|
||||
evidence.predicate_type !== expected[0] ||
|
||||
!(Array.isArray(expected[0])
|
||||
? expected[0].includes(evidence.predicate_type)
|
||||
: evidence.predicate_type === expected[0]) ||
|
||||
evidence.provider.id !== expected[1] ||
|
||||
evidence.provider.name !== expected[2] ||
|
||||
evidence.provider.version !==
|
||||
|
||||
Reference in New Issue
Block a user