From 857a1b7b04da4488db2b224c270ad78998c6b6c7 Mon Sep 17 00:00:00 2001 From: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:04:46 +0800 Subject: [PATCH] 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. --- .../process/processTraceSpecification.test.ts | 75 ++++++++++++++++ .../process/processTraceSpecification.ts | 49 +++++++--- .../mcp/processTraceAdaptersParity.test.ts | 90 +++++++++++++++---- 3 files changed, 184 insertions(+), 30 deletions(-) diff --git a/src/domain/process/processTraceSpecification.test.ts b/src/domain/process/processTraceSpecification.test.ts index 2c7641332..aeedf4d31 100644 --- a/src/domain/process/processTraceSpecification.test.ts +++ b/src/domain/process/processTraceSpecification.test.ts @@ -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( diff --git a/src/domain/process/processTraceSpecification.ts b/src/domain/process/processTraceSpecification.ts index 0f121c42b..61ace7ca1 100644 --- a/src/domain/process/processTraceSpecification.ts +++ b/src/domain/process/processTraceSpecification.ts @@ -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, 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 = ( diff --git a/tests/boundary/mcp/processTraceAdaptersParity.test.ts b/tests/boundary/mcp/processTraceAdaptersParity.test.ts index 7f9d3d9c3..cb7c1439c 100644 --- a/tests/boundary/mcp/processTraceAdaptersParity.test.ts +++ b/tests/boundary/mcp/processTraceAdaptersParity.test.ts @@ -91,6 +91,29 @@ const captureEvidence = (side: "left" | "right") => locations: [{ kind: "artifact-path", path: `/fixture/${side}` }], }); +const comparisonClient = async ( + left: ReturnType, + right: ReturnType, +) => { + const session = createTestBinarySession( + (_path) => + ({ + execute: () => Promise.resolve(ok(null)), + close: () => Promise.resolve(resultOk(null)), + }) satisfies AnalysisClient, + ); + expect(session.recordEvidence(left).ok).toBe(true); + expect(session.recordEvidence(right).ok).toBe(true); + const server = createServer({ kind: "session", session }); + const client = new Client({ name: "trace-parity", version: "1" }); + const [clientTransport, serverTransport] = + InMemoryTransport.createLinkedPair(); + resources.push(client, server); + await server.connect(serverTransport); + await client.connect(clientTransport); + return client; +}; + afterEach(async () => { await Promise.all(resources.splice(0).map((resource) => resource.close())); await Promise.all( @@ -117,22 +140,7 @@ describe("declared trace adapter parity", () => { const cliEvidence = parseEvidence( await compareProcessEvidenceFiles(leftPath, rightPath, specificationPath), ); - const session = createTestBinarySession( - (_path) => - ({ - execute: () => Promise.resolve(ok(null)), - close: () => Promise.resolve(resultOk(null)), - }) satisfies AnalysisClient, - ); - expect(session.recordEvidence(left).ok).toBe(true); - expect(session.recordEvidence(right).ok).toBe(true); - const server = createServer({ kind: "session", session }); - const client = new Client({ name: "trace-parity", version: "1" }); - const [clientTransport, serverTransport] = - InMemoryTransport.createLinkedPair(); - resources.push(client, server); - await server.connect(serverTransport); - await client.connect(clientTransport); + const client = await comparisonClient(left, right); const response = await client.callTool({ name: "compare_process_captures", arguments: { @@ -157,4 +165,54 @@ describe("declared trace adapter parity", () => { { kind: "artifact-path", path: "/fixture/right" }, ]); }); + + it("reports one complete missing-pair diagnostic through CLI and MCP", async () => { + const left = captureEvidence("left"); + const right = captureEvidence("right"); + const incompleteSpecification = { + events: Array.from({ length: 200 }, (_, index) => ({ + id: `e${String(index)}`, + source: "lifecycle", + exact: { fixture: index }, + cardinality: { kind: "optional" }, + })), + language: { kind: "partial_order" }, + }; + const root = await createTestTempDirectory("rea-trace-diagnostics-"); + temporaryRoots.push(root); + const leftPath = join(root, "left.json"); + const rightPath = join(root, "right.json"); + const specificationPath = join(root, "trace.json"); + await Promise.all([ + writeFile(leftPath, JSON.stringify(left)), + writeFile(rightPath, JSON.stringify(right)), + writeFile(specificationPath, JSON.stringify(incompleteSpecification)), + ]); + const message = + "19900 event pairs must be explicitly ordered or unordered; first undeclared pair: e0/e1"; + const cli = await compareProcessEvidenceFiles( + leftPath, + rightPath, + specificationPath, + ); + expect(cli).toMatchObject({ + category: "invalid_input", + details: { + issues: [{ path: ["trace_spec", "language"], message }], + }, + }); + const client = await comparisonClient(left, right); + const response = await client.callTool({ + name: "compare_process_captures", + arguments: { left, right, trace_spec: incompleteSpecification }, + }); + expect(response.isError).toBe(true); + const text = response.content + .filter((block) => block.type === "text") + .map((block) => block.text) + .join("\n"); + expect(text).toContain(message); + expect(text.match(/first undeclared pair/g)).toHaveLength(1); + await client.ping(); + }); });