fix(process): aggregate incomplete trace-spec diagnostics

Summarize incomplete trace-specification pair declarations with exact counts and the first undeclared pair. Preserve complete pair coverage, successful Evidence and independent validation errors; remove temporary pair-scan copies and sorting.

Closes #1753.
This commit is contained in:
Ysopwnable
2026-10-11 16:04:46 +08:00
committed by GitHub
parent e5699d23b1
commit 857a1b7b04
3 changed files with 184 additions and 30 deletions
@@ -39,6 +39,81 @@ const specification = (): ProcessTraceSpecification => ({
});
describe("process trace specification", () => {
it("summarizes an incomplete language with its exact missing pair count", () => {
const events = Array.from({ length: 200 }, (_, index) => ({
id: `e${String(index)}`,
source: "lifecycle" as const,
exact: { fixture: index },
cardinality: { kind: "optional" as const },
}));
const result = processTraceSpecificationSchema.safeParse({
events,
language: { kind: "partial_order" },
});
expect(result.success).toBe(false);
if (result.success)
throw new Error("Expected incomplete pair declarations");
expect(result.error.issues).toEqual([
expect.objectContaining({
path: ["language"],
message:
"19900 event pairs must be explicitly ordered or unordered; first undeclared pair: e0/e1",
}),
]);
});
it("counts only undeclared pairs after transitive, not-before and unordered relations", () => {
const events = ["a", "b", "c", "d"].map((id) => ({
id,
source: "lifecycle" as const,
exact: { fixture: id },
cardinality: { kind: "optional" as const },
}));
const result = processTraceSpecificationSchema.safeParse({
events,
language: {
kind: "partial_order",
happens_before: [
{ before: "a", after: "b" },
{ before: "b", after: "c" },
],
not_before: [{ event: "d", anchor: "c" }],
unordered_groups: [{ events: ["a", "d"] }],
},
});
expect(result.success).toBe(false);
if (result.success) throw new Error("Expected the missing b/d relation");
expect(result.error.issues).toEqual([
expect.objectContaining({
path: ["language"],
message: "event pair b/d must be explicitly ordered or unordered",
}),
]);
});
it("retains duplicate unordered-pair diagnostics for reversed declarations", () => {
const result = processTraceSpecificationSchema.safeParse({
events: ["z", "a", "b"].map((id) => ({
id,
source: "lifecycle",
exact: { fixture: id },
cardinality: { kind: "optional" },
})),
language: {
kind: "partial_order",
unordered_groups: [{ events: ["z", "a", "b"] }, { events: ["a", "z"] }],
},
});
expect(result.success).toBe(false);
if (result.success) throw new Error("Expected a duplicate unordered pair");
expect(result.error.issues).toEqual([
expect.objectContaining({
path: ["language", "unordered_groups", 1],
message: "unordered event pairs must be declared once",
}),
]);
});
it("rejects cycles, implicit ordering gaps, invalid ignores, and unsatisfiable variants", () => {
const base = specification();
expect(
+35 -14
View File
@@ -143,7 +143,7 @@ const addReferenceIssue = (
};
const pairKey = (left: string, right: string): string =>
[left, right].sort().join("\0");
left <= right ? `${left}\0${right}` : `${right}\0${left}`;
const buildAdjacency = (
language: PartialOrderLanguage,
@@ -269,7 +269,13 @@ const validateUnorderedGroups = (
for (const [eventIndex, id] of group.events.entries())
addReferenceIssue(context, known, id, [...path, "events", eventIndex]);
for (const [leftIndex, leftId] of group.events.entries()) {
for (const rightId of group.events.slice(leftIndex + 1)) {
for (
let rightIndex = leftIndex + 1;
rightIndex < group.events.length;
rightIndex++
) {
const rightId = group.events[rightIndex];
if (rightId === undefined) continue;
const key = pairKey(leftId, rightId);
if (pairs.has(key))
context.addIssue({
@@ -299,18 +305,33 @@ const validateCompletePairDeclarations = (
unorderedPairs: ReadonlySet<string>,
context: z.RefinementCtx,
): void => {
for (const [leftIndex, leftId] of ids.entries())
for (const rightId of ids.slice(leftIndex + 1))
if (
!isOrdered(relations.reachable, leftId, rightId) &&
!relations.notBeforePairs.has(pairKey(leftId, rightId)) &&
!unorderedPairs.has(pairKey(leftId, rightId))
)
context.addIssue({
code: "custom",
message: `event pair ${leftId}/${rightId} must be explicitly ordered or unordered`,
path: ["language"],
});
let missingPairCount = 0;
let firstMissingPair = "";
for (const [leftIndex, leftId] of ids.entries()) {
for (
let rightIndex = leftIndex + 1;
rightIndex < ids.length;
rightIndex++
) {
const rightId = ids[rightIndex];
if (rightId === undefined) continue;
if (isOrdered(relations.reachable, leftId, rightId)) continue;
const key = pairKey(leftId, rightId);
if (!relations.notBeforePairs.has(key) && !unorderedPairs.has(key)) {
if (missingPairCount === 0) firstMissingPair = `${leftId}/${rightId}`;
missingPairCount++;
}
}
}
if (missingPairCount > 0)
context.addIssue({
code: "custom",
message:
missingPairCount === 1
? `event pair ${firstMissingPair} must be explicitly ordered or unordered`
: `${String(missingPairCount)} event pairs must be explicitly ordered or unordered; first undeclared pair: ${firstMissingPair}`,
path: ["language"],
});
};
const validatePartialOrder = (