fix(contracts): preserve local inputs and comparison uncertainty

This commit is contained in:
morluto
2026-10-09 06:49:02 +08:00
parent 7a7460f04f
commit 70e8a8f860
35 changed files with 460 additions and 386 deletions
@@ -0,0 +1,126 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { expect, it, onTestFinished } from "vitest";
import { z } from "zod";
import { ARTIFACT_COMPARISON_EXAMPLE } from "../../../src/contracts/artifactComparisonExample.js";
import { FUNCTION_COMPARISON_EXAMPLE } from "../../../src/contracts/functionComparisonExample.js";
import {
createEvidence,
parseEvidence,
type Evidence,
} from "../../../src/domain/evidence.js";
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
const connect = async () => {
const session = createTestBinarySession(() => {
throw new Error("Evidence comparison must not launch a provider");
});
const server = createServer(session, session);
const client = new Client({ name: "comparison-unknowns", version: "1" });
onTestFinished(async () => {
await Promise.allSettled([client.close(), server.close(), session.close()]);
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
return { client, session };
};
const observeAgain = (
source: Evidence,
observation: number,
result = source.normalized_result,
) =>
createEvidence(
source.subject === null
? undefined
: {
path: `/tmp/fixture-${observation}`,
sha256: source.subject.digest.sha256,
format: source.subject.format,
},
source.provider,
{
predicateType: source.predicate_type,
operation: source.operation,
parameters: { observation },
result,
confidence: source.confidence,
authority: source.authority,
},
);
it("retains one open unknown for an incomplete artifact compared with itself", async () => {
const { client, session } = await connect();
const source = ARTIFACT_COMPARISON_EXAMPLE.left;
const result = jsonValueSchema.parse(source.normalized_result);
if (result === null || Array.isArray(result) || typeof result !== "object")
throw new Error("Expected an inventory object");
const incomplete = observeAgain(source, 1, {
...result,
nodes: [],
occurrences: [],
});
const response = await client.callTool({
name: "compare_artifacts",
arguments: { left: incomplete, right: incomplete },
});
expect(response.isError, JSON.stringify(response)).not.toBe(true);
expect(response.structuredContent).toMatchObject({
result: { status: "truncated" },
});
expect(session.listUnknowns({ domain: "artifact-comparison" })).toMatchObject(
[
{
status: "open",
supporting_evidence_ids: [incomplete.evidence_id],
contradicting_evidence_ids: [],
},
],
);
});
it.each([
{
tool: "compare_artifacts",
domain: "artifact-comparison",
pair: ARTIFACT_COMPARISON_EXAMPLE,
},
{
tool: "compare_functions",
domain: "function-comparison",
pair: FUNCTION_COMPARISON_EXAMPLE,
},
])(
"keeps independent $domain observations and repeated comparisons idempotent",
async ({ tool, domain, pair }) => {
const { client, session } = await connect();
const right = observeAgain(pair.right, 2);
for (const source of [pair.right, right]) {
const response = await client.callTool({
name: tool,
arguments: { left: pair.left, right: source },
});
expect(response.isError, JSON.stringify(response)).not.toBe(true);
expect(response.structuredContent).toMatchObject({
result: { status: "changed" },
});
}
expect(session.listUnknowns({ domain })).toHaveLength(2);
const before = session.exportEvidenceBundle();
const repeated = await client.callTool({
name: tool,
arguments: { left: pair.left, right },
});
expect(repeated.isError, JSON.stringify(repeated)).not.toBe(true);
const compared = parseEvidence(
z.object({ evidence: z.unknown() }).parse(repeated.structuredContent)
.evidence,
);
expect(session.evidenceById(compared.evidence_id)).toEqual(compared);
expect(session.exportEvidenceBundle()).toEqual(before);
},
);
@@ -70,6 +70,14 @@ describe.skipIf(process.platform !== "darwin")(
expect(JSON.stringify(spanning.content)).toContain(
"Each Swift symbol must be one line.",
);
const invalidNativeString = await client.callTool({
name: "demangle_swift",
arguments: { symbols: ["$s4main\0FooV"] },
});
expect(invalidNativeString.isError).toBe(true);
expect(JSON.stringify(invalidNativeString.content)).toContain(
"Swift symbols cannot contain NUL.",
);
},
);
},
@@ -9,6 +9,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { itWithCaptureCapability } from "./processCaptureCapability.js";
import { captureProcessScenario } from "../../../src/process/capture/ProcessHarness.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { snapshotRoots } from "../../../src/process/capture/FilesystemSnapshot.js";
import { ProcessCaptureError } from "../../../src/process/capture/ProcessCaptureError.js";
import {
@@ -264,6 +265,13 @@ itWithCaptureCapability(
expect(result.error.executionFailure).toBe(
"fixture final snapshot failure",
);
const projected = projectAnalysisError(result.error);
expect(projected).toMatchObject({
code: "cleanup_incomplete",
details: { cleanup: "incomplete", resources: ["owned_process_group"] },
});
expect(projected).not.toHaveProperty("stack");
expect(projected).not.toHaveProperty("cause");
expect(partial).toBeDefined();
if (partial === undefined || !("observations" in partial))
throw new Error("expected incomplete process observations");
@@ -489,6 +497,11 @@ itWithCaptureCapability(
);
if (result.ok) throw new Error("expected initial snapshot cancellation");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "cancelled",
category: "cancelled",
details: { operation: "process_capture", cleanup: "complete" },
});
expect(result.error).toMatchObject({
reason: "cancelled",
userCategory: "cancelled",