fix(process): bound trace comparison semantics
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { ProcessCapture } from "./processCapture.js";
|
||||
import { validateProcessCapture } from "./processCaptureValidation.js";
|
||||
import {
|
||||
evaluateProcessTraceSide,
|
||||
processTraceComparisonResultSchema,
|
||||
@@ -27,6 +28,14 @@ export const compareProcessTraces = (
|
||||
right: ProcessCapture,
|
||||
specificationInput: ProcessTraceSpecification,
|
||||
): ProcessTraceComparisonResult => {
|
||||
const captureIssues = [
|
||||
...validateProcessCapture(left),
|
||||
...validateProcessCapture(right),
|
||||
];
|
||||
if (captureIssues.length > 0)
|
||||
throw new TypeError(
|
||||
`Invalid Process Capture v4: ${captureIssues[0]!.path}`,
|
||||
);
|
||||
const specification =
|
||||
processTraceSpecificationSchema.parse(specificationInput);
|
||||
const leftEvaluation = evaluateProcessTraceSide(left, specification);
|
||||
@@ -37,6 +46,14 @@ export const compareProcessTraces = (
|
||||
? null
|
||||
: { side: "right" as const, ...rightEvaluation.diagnostic }
|
||||
: { side: "left" as const, ...leftEvaluation.diagnostic };
|
||||
const outcomesDiffer =
|
||||
leftEvaluation.result.status !== rightEvaluation.result.status ||
|
||||
JSON.stringify(
|
||||
leftEvaluation.result.raw_trace.map(({ event_id }) => event_id),
|
||||
) !==
|
||||
JSON.stringify(
|
||||
rightEvaluation.result.raw_trace.map(({ event_id }) => event_id),
|
||||
);
|
||||
return processTraceComparisonResultSchema.parse({
|
||||
verdict:
|
||||
leftEvaluation.result.status === "unknown" ||
|
||||
@@ -45,7 +62,9 @@ export const compareProcessTraces = (
|
||||
: leftEvaluation.result.status === "pass" &&
|
||||
rightEvaluation.result.status === "pass"
|
||||
? "equivalent"
|
||||
: "different",
|
||||
: outcomesDiffer
|
||||
? "different"
|
||||
: "nonconforming",
|
||||
left: leftEvaluation.result,
|
||||
right: rightEvaluation.result,
|
||||
diagnostic,
|
||||
|
||||
@@ -73,7 +73,7 @@ const diagnosticSchema = z.strictObject({
|
||||
|
||||
/** Structured verdict for one declared process trace language. */
|
||||
export const processTraceComparisonResultSchema = z.strictObject({
|
||||
verdict: z.enum(["equivalent", "different", "unknown"]),
|
||||
verdict: z.enum(["equivalent", "different", "nonconforming", "unknown"]),
|
||||
left: sideResultSchema,
|
||||
right: sideResultSchema,
|
||||
diagnostic: diagnosticSchema.nullable(),
|
||||
@@ -257,31 +257,41 @@ const matchRecords = (
|
||||
): MatchedTrace | EvaluatedSide => {
|
||||
const rawTrace: z.infer<typeof matchedEventSchema>[] = [];
|
||||
const locationsById = new Map<string, ProcessTraceLocation[]>();
|
||||
for (const record of records) {
|
||||
const matches = specification.events.filter(
|
||||
(event) =>
|
||||
event.source === record.source &&
|
||||
canonicalTraceJson(
|
||||
comparableTracePayload(event.exact, event.ignore_fields),
|
||||
) ===
|
||||
canonicalTraceJson(
|
||||
comparableTracePayload(record.payload, event.ignore_fields),
|
||||
),
|
||||
const ignoredFieldsBySource = new Map<
|
||||
ProcessTraceSource,
|
||||
readonly string[]
|
||||
>();
|
||||
const eventsByKey = new Map<
|
||||
string,
|
||||
ProcessTraceSpecification["events"][number]
|
||||
>();
|
||||
for (const event of specification.events) {
|
||||
const ignoredFields = event.ignore_fields ?? [];
|
||||
ignoredFieldsBySource.set(event.source, ignoredFields);
|
||||
eventsByKey.set(
|
||||
`${event.source}\0${canonicalTraceJson(
|
||||
comparableTracePayload(event.exact, ignoredFields),
|
||||
)}`,
|
||||
event,
|
||||
);
|
||||
if (matches.length !== 1)
|
||||
}
|
||||
for (const record of records) {
|
||||
const match = eventsByKey.get(
|
||||
`${record.source}\0${canonicalTraceJson(
|
||||
comparableTracePayload(
|
||||
record.payload,
|
||||
ignoredFieldsBySource.get(record.source),
|
||||
),
|
||||
)}`,
|
||||
);
|
||||
if (match === undefined)
|
||||
return failure({
|
||||
kind: "predicate",
|
||||
message:
|
||||
matches.length === 0
|
||||
? "Observed event does not match any declared exact predicate."
|
||||
: "Observed event matches multiple predicates.",
|
||||
eventIds: matches.map(({ id }) => id),
|
||||
message: "Observed event does not match any declared exact predicate.",
|
||||
eventIds: [],
|
||||
locations: [record.location],
|
||||
rawTrace,
|
||||
});
|
||||
const match = matches[0];
|
||||
if (match === undefined)
|
||||
throw new TypeError("Unique trace match is missing");
|
||||
rawTrace.push({ event_id: match.id, location: record.location });
|
||||
const locations = locationsById.get(match.id) ?? [];
|
||||
locations.push(record.location);
|
||||
|
||||
@@ -197,6 +197,16 @@ const buildAdjacency = (
|
||||
});
|
||||
adjacency.get(edge.before)?.add(edge.after);
|
||||
}
|
||||
return adjacency;
|
||||
};
|
||||
|
||||
const validateNotBefore = (
|
||||
language: PartialOrderLanguage,
|
||||
context: z.RefinementCtx,
|
||||
known: ReadonlySet<string>,
|
||||
reachable: ReadonlyMap<string, ReadonlySet<string>>,
|
||||
): ReadonlySet<string> => {
|
||||
const pairs = new Set<string>();
|
||||
for (const [index, constraint] of language.not_before.entries()) {
|
||||
const path = ["language", "not_before", index];
|
||||
addReferenceIssue(context, known, constraint.event, [...path, "event"]);
|
||||
@@ -207,9 +217,22 @@ const buildAdjacency = (
|
||||
message: "not-before constraints must reference distinct events",
|
||||
path,
|
||||
});
|
||||
adjacency.get(constraint.anchor)?.add(constraint.event);
|
||||
const key = pairKey(constraint.event, constraint.anchor);
|
||||
if (pairs.has(key))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "not-before event pairs must be declared once",
|
||||
path,
|
||||
});
|
||||
pairs.add(key);
|
||||
if (reachable.get(constraint.event)?.has(constraint.anchor) === true)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "not-before constraint conflicts with happens-before order",
|
||||
path,
|
||||
});
|
||||
}
|
||||
return adjacency;
|
||||
return pairs;
|
||||
};
|
||||
|
||||
const graphHasCycle = (
|
||||
@@ -257,11 +280,16 @@ const isOrdered = (
|
||||
reachable.get(left)?.has(right) === true ||
|
||||
reachable.get(right)?.has(left) === true;
|
||||
|
||||
type OrderingRelations = {
|
||||
readonly reachable: ReadonlyMap<string, ReadonlySet<string>>;
|
||||
readonly notBeforePairs: ReadonlySet<string>;
|
||||
};
|
||||
|
||||
const validateUnorderedGroups = (
|
||||
language: PartialOrderLanguage,
|
||||
context: z.RefinementCtx,
|
||||
known: ReadonlySet<string>,
|
||||
reachable: ReadonlyMap<string, ReadonlySet<string>>,
|
||||
relations: OrderingRelations,
|
||||
): ReadonlySet<string> => {
|
||||
const pairs = new Set<string>();
|
||||
for (const [groupIndex, group] of language.unordered_groups.entries()) {
|
||||
@@ -284,7 +312,10 @@ const validateUnorderedGroups = (
|
||||
path,
|
||||
});
|
||||
pairs.add(key);
|
||||
if (isOrdered(reachable, leftId, rightId))
|
||||
if (
|
||||
isOrdered(relations.reachable, leftId, rightId) ||
|
||||
relations.notBeforePairs.has(key)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
message: "ordered events cannot also be declared unordered",
|
||||
@@ -298,14 +329,15 @@ const validateUnorderedGroups = (
|
||||
|
||||
const validateCompletePairDeclarations = (
|
||||
ids: readonly string[],
|
||||
reachable: ReadonlyMap<string, ReadonlySet<string>>,
|
||||
relations: OrderingRelations,
|
||||
unorderedPairs: ReadonlySet<string>,
|
||||
context: z.RefinementCtx,
|
||||
): void => {
|
||||
for (const [leftIndex, leftId] of ids.entries())
|
||||
for (const rightId of ids.slice(leftIndex + 1))
|
||||
if (
|
||||
!isOrdered(reachable, leftId, rightId) &&
|
||||
!isOrdered(relations.reachable, leftId, rightId) &&
|
||||
!relations.notBeforePairs.has(pairKey(leftId, rightId)) &&
|
||||
!unorderedPairs.has(pairKey(leftId, rightId))
|
||||
)
|
||||
context.addIssue({
|
||||
@@ -330,13 +362,20 @@ const validatePartialOrder = (
|
||||
path: ["language"],
|
||||
});
|
||||
const reachable = computeReachability(ids, adjacency);
|
||||
const unorderedPairs = validateUnorderedGroups(
|
||||
const notBeforePairs = validateNotBefore(
|
||||
specification.language,
|
||||
context,
|
||||
known,
|
||||
reachable,
|
||||
);
|
||||
validateCompletePairDeclarations(ids, reachable, unorderedPairs, context);
|
||||
const relations = { reachable, notBeforePairs };
|
||||
const unorderedPairs = validateUnorderedGroups(
|
||||
specification.language,
|
||||
context,
|
||||
known,
|
||||
relations,
|
||||
);
|
||||
validateCompletePairDeclarations(ids, relations, unorderedPairs, context);
|
||||
for (const [kind, values] of [
|
||||
["prefix", specification.language.prefix],
|
||||
["suffix", specification.language.suffix],
|
||||
|
||||
Reference in New Issue
Block a user