From afbbfff561a9d83689e49f61a5192b98bbfe5c07 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:19:31 -0500 Subject: [PATCH] fix(functions): preserve proven referenced string membership changes (#13421) --- src/domain/functionComparison.test.ts | 138 ++++++++---------- src/domain/functionComparisonDimensions.ts | 5 +- .../functionComparisonIntegrityMcp.test.ts | 3 +- .../mcp/functionComparisonMcp.test.ts | 7 +- ...unctionComparisonReferenceFactsMcp.test.ts | 1 - ...unctionComparisonStringDecodingMcp.test.ts | 41 +++++- 6 files changed, 108 insertions(+), 87 deletions(-) diff --git a/src/domain/functionComparison.test.ts b/src/domain/functionComparison.test.ts index de5ee14885..d8b5bd408e 100644 --- a/src/domain/functionComparison.test.ts +++ b/src/domain/functionComparison.test.ts @@ -516,31 +516,6 @@ describe("function call relationship coverage", () => { }, ); - it("retains complete observations with execution diagnostics as comparable", () => { - const coverage = { - status: "complete" as const, - diagnostics: ["Provider reported a non-call xref warning."], - }; - const left = observe( - "c", - dossier("return 0;", "0x1000"), - "rea-workflow", - coverage, - ); - const right = observe( - "d", - dossier("return 0;", "0x2000"), - "rea-workflow", - coverage, - ); - expect( - compareFunctions( - functionSnapshotFixture(left), - functionSnapshotFixture(right), - ).dimensions.find(({ dimension }) => dimension === "calls"), - ).toMatchObject({ status: "unchanged", left_count: 0, right_count: 0 }); - }); - it("keeps unresolved call sites unknown despite complete resolved-set coverage", () => { const left = observe("e", dossier("return 0;", "0x1000")); const rightDossier = functionDossierSchema.parse({ @@ -911,7 +886,18 @@ describe("function comparison unresolved text", () => { ); expect( comparison.dimensions.find(({ dimension }) => dimension === "pseudocode"), - ).toMatchObject({ status: "unknown", left_count: 11, right_count: 11 }); + ).toMatchObject({ + status: "unknown", + left_count: 11, + right_count: 11, + left_digest: null, + right_digest: null, + text_delta: null, + }); + expect(comparison.changes.map(({ dimension }) => dimension)).toEqual([ + "pseudocode", + "assembly", + ]); expect(comparison.summary).toEqual({ unchanged: 6, changed: 0, @@ -927,35 +913,58 @@ describe("function comparison unresolved text", () => { }); describe("function comparison", () => { - it("retains complete call coverage diagnostics without blocking observed-set equality", () => { - const diagnostic = "Indirect calls remain unresolved."; - const left = observe("a", dossier("return 0;", "0x1000"), "rea-workflow", { - status: "complete", - diagnostics: [diagnostic], - discovery: { - callers: { status: "complete" }, - callees: { status: "complete" }, - }, - }); - const right = observe("b", dossier("return 0;", "0x2000"), "rea-workflow", { - status: "complete", - diagnostics: [diagnostic], - discovery: { - callers: { status: "complete" }, - callees: { status: "complete" }, - }, - }); - const comparison = compareFunctions( - functionSnapshotFixture(left), - functionSnapshotFixture(right), - ); - expect( - comparison.dimensions.find(({ dimension }) => dimension === "calls"), - ).toMatchObject({ - status: "unchanged", - limitations: [`Left: ${diagnostic}`, `Right: ${diagnostic}`], - }); - }); + it.each(["legacy", "discovery"] as const)( + "retains complete call coverage diagnostics for %s observations", + (coverageKind) => { + const diagnostic = "Indirect calls remain unresolved."; + const coverage: RelationshipCoverage = { + status: "complete", + diagnostics: [diagnostic], + ...(coverageKind === "legacy" + ? {} + : { + discovery: { + callers: { status: "complete" }, + callees: { status: "complete" }, + }, + }), + }; + const left = observe( + "a", + dossier("return 0;", "0x1000"), + "rea-workflow", + coverage, + ); + const right = observe( + "b", + dossier("return 0;", "0x2000"), + "rea-workflow", + coverage, + ); + const comparison = compareFunctions( + functionSnapshotFixture(left), + functionSnapshotFixture(right), + ); + expect( + comparison.dimensions.find(({ dimension }) => dimension === "calls"), + ).toMatchObject({ + status: "unchanged", + left_count: 0, + right_count: 0, + discovery_status: coverageKind === "legacy" ? "unknown" : "complete", + limitations: [ + `Left: ${diagnostic}`, + `Right: ${diagnostic}`, + ...(coverageKind === "legacy" + ? [ + "Left: Static direct-call discovery is not established for callers and callees.", + "Right: Static direct-call discovery is not established for callers and callees.", + ] + : []), + ], + }); + }, + ); it("retains diagnostics alongside incomplete call coverage blockers", () => { const diagnostic = "Indirect calls remain unresolved."; @@ -1039,25 +1048,6 @@ describe("function comparison normalization and provenance", () => { ).toMatchObject({ status: "changed" }); }); - it("treats provider-specific text as unknown and validates Evidence", () => { - const left = observe("5", dossier("return 0;", "0x1000"), "provider-a"); - const right = observe("6", dossier("return 1;", "0x2000"), "provider-b"); - const comparison = compareFunctions( - functionSnapshotFixture(left), - functionSnapshotFixture(right), - ); - expect( - comparison.dimensions.find(({ dimension }) => dimension === "pseudocode"), - ).toMatchObject({ status: "unknown" }); - expect(comparison.changes).toHaveLength(2); - expect(() => - compareFunctions( - functionSnapshotFixture({ ...left, operation: "binary_overview" }), - functionSnapshotFixture(right), - ), - ).toThrow(/identifier/u); - }); - it("matches entries by address only within the same opened target", () => { const raw = (entryPoint: string): EvidenceSubjectTarget => ({ path: "/tmp/function.bin", diff --git a/src/domain/functionComparisonDimensions.ts b/src/domain/functionComparisonDimensions.ts index 8ba35bb710..3e2d84aaa5 100644 --- a/src/domain/functionComparisonDimensions.ts +++ b/src/domain/functionComparisonDimensions.ts @@ -401,7 +401,10 @@ const compareReferencedStrings = ( continue; } if (leftRows === undefined || rightRows === undefined) { - if (leftUnlocated || rightUnlocated) + // Only an unlocated row on the missing side could occupy this offset. + const missingSideHasUnlocated = + leftRows === undefined ? leftUnlocated : rightUnlocated; + if (missingSideHasUnlocated) limitations.add( `Referenced string membership at source offset ${offset} is ambiguous because another source location is unavailable.`, ); diff --git a/tests/boundary/mcp/functionComparisonIntegrityMcp.test.ts b/tests/boundary/mcp/functionComparisonIntegrityMcp.test.ts index 8dd960b5cd..777b41a1a5 100644 --- a/tests/boundary/mcp/functionComparisonIntegrityMcp.test.ts +++ b/tests/boundary/mcp/functionComparisonIntegrityMcp.test.ts @@ -1,4 +1,4 @@ -import { expect, it, onTestFinished } from "vitest"; +import { expect, it } from "vitest"; import { FUNCTION_COMPARISON_EXAMPLE } from "../../../src/contracts/functionComparisonExample.js"; import { @@ -194,7 +194,6 @@ it.each([ ); }); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); for (const record of [ FUNCTION_COMPARISON_EXAMPLE.left, FUNCTION_COMPARISON_EXAMPLE.right, diff --git a/tests/boundary/mcp/functionComparisonMcp.test.ts b/tests/boundary/mcp/functionComparisonMcp.test.ts index 82adb0c4a0..e695c5e970 100644 --- a/tests/boundary/mcp/functionComparisonMcp.test.ts +++ b/tests/boundary/mcp/functionComparisonMcp.test.ts @@ -4,7 +4,7 @@ import { Ajv2020 } from "ajv/dist/2020.js"; import type { AnySchema } from "ajv"; import { functionSnapshotFixture } from "../../../src/domain/functionDossierEvidence.fixture.js"; import { ok as resultOk } from "../../../src/domain/result.js"; -import { describe, expect, it, onTestFinished } from "vitest"; +import { describe, expect, it } from "vitest"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -33,7 +33,6 @@ it.each([ ); }); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); const source = (side: "1" | "2", space: string) => { const example = FUNCTION_COMPARISON_EXAMPLE.left; const value = functionDossierSchema.parse(example.normalized_result); @@ -327,7 +326,6 @@ describe("function comparison input-kind diagnostics", () => { throw new Error("Wrong-kind function input must not launch a provider"); }); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); const wrong = wrongKindFunctionEvidence(mismatch); const good = FUNCTION_COMPARISON_EXAMPLE[side === "left" ? "right" : "left"]; @@ -376,7 +374,6 @@ describe("function comparison input-kind diagnostics", () => { true, ); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); const result = await harness.client.callTool({ name: "compare_functions", arguments: { @@ -407,7 +404,6 @@ describe("function comparison input-kind diagnostics", () => { throw new Error("Tampered function input must not launch a provider"); }); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); const tampered = { ...FUNCTION_COMPARISON_EXAMPLE.left, limitations: [ @@ -513,7 +509,6 @@ it.each([ throw new Error("Imported comparison must not launch a provider"); }); const harness = await createApplicationMcpHarness({ session }); - onTestFinished(harness.close); const imported = await harness.client.callTool({ name: "import_evidence_bundle", arguments: { path }, diff --git a/tests/boundary/mcp/functionComparisonReferenceFactsMcp.test.ts b/tests/boundary/mcp/functionComparisonReferenceFactsMcp.test.ts index 4aaf3e3b35..12e2aa916e 100644 --- a/tests/boundary/mcp/functionComparisonReferenceFactsMcp.test.ts +++ b/tests/boundary/mcp/functionComparisonReferenceFactsMcp.test.ts @@ -48,7 +48,6 @@ const createHarness = async () => { throw new Error("Function comparison must not launch a provider"); }), }); - onTestFinished(harness.close); return harness; }; diff --git a/tests/boundary/mcp/functionComparisonStringDecodingMcp.test.ts b/tests/boundary/mcp/functionComparisonStringDecodingMcp.test.ts index 651e2f834f..5b10edf60f 100644 --- a/tests/boundary/mcp/functionComparisonStringDecodingMcp.test.ts +++ b/tests/boundary/mcp/functionComparisonStringDecodingMcp.test.ts @@ -121,6 +121,42 @@ beforeEach(async () => { const stringsNames = (result: Comparison) => result.dimensions.find(({ dimension }) => dimension === "strings_names"); +it("keeps located membership changes when only that side has an unlocated reference", async () => { + const located = reference("known", { sourceAddress: "0x1004" }); + const unlocated = reference("unplaced", { sourceAddress: "0x0fff" }); + const mixed = evidence("mixed", [located, unlocated]); + const empty = evidence("empty", []); + const unknown = evidence("unknown", [unlocated]); + + for (const [left, right, leftCount, rightCount] of [ + [mixed, empty, 2, 0], + [empty, mixed, 0, 2], + ] as const) { + expect(stringsNames(await compare(left, right))).toMatchObject({ + status: "changed", + left_count: leftCount, + right_count: rightCount, + evidence_links: [left.evidence_id, right.evidence_id], + limitations: [ + "Referenced string source location could not be normalized.", + ], + }); + } + + for (const [left, right] of [ + [mixed, unknown], + [unknown, mixed], + ] as const) + expect(stringsNames(await compare(left, right))).toMatchObject({ + status: "unknown", + left_digest: null, + right_digest: null, + limitations: expect.arrayContaining([ + "Referenced string membership at source offset 4 is ambiguous because another source location is unavailable.", + ]), + }); +}); + describe("compare_functions referenced string decoding", () => { const reason = "The native string bytes are unavailable"; const undecodable = (id: string, display: string) => @@ -208,8 +244,7 @@ describe("compare_functions referenced string decoding", () => { termination: | "missing" | "present_or_not_required" = "present_or_not_required", - byteLength = 6, - ) => evidence(id, [reference("hello", { termination, byteLength })]); + ) => evidence(id, [reference("hello", { termination })]); const equal = await compare(decoded("5"), decoded("6")); expect(equal.status).toBe("unchanged"); expect(stringsNames(equal)).toMatchObject({ @@ -222,7 +257,7 @@ describe("compare_functions referenced string decoding", () => { stringsNames(equal)?.left_digest, ); - const different = await compare(decoded("7"), decoded("8", "missing", 5)); + const different = await compare(decoded("7"), decoded("8", "missing")); expect(different.status).toBe("changed"); expect(stringsNames(different)).toMatchObject({ status: "changed" }); });