2026-10-09 20:02:52 +08:00
|
|
|
import type { EvidenceMcpServer } from "./EvidenceMcpServer.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-10-09 22:45:18 +08:00
|
|
|
import type { BinarySessionPort } from "../application/binary/BinarySessionPort.js";
|
2026-10-05 21:15:04 +08:00
|
|
|
import { toolContract } from "../contracts/toolContracts.js";
|
2026-09-29 16:28:46 +08:00
|
|
|
import {
|
|
|
|
|
createEvidence,
|
|
|
|
|
parseEvidence,
|
|
|
|
|
type Evidence,
|
|
|
|
|
} from "../domain/evidence.js";
|
2026-10-06 05:57:38 +08:00
|
|
|
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
|
2026-09-29 16:28:46 +08:00
|
|
|
import { err } from "../domain/result.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
import { jsonValueSchema } from "../domain/jsonValue.js";
|
|
|
|
|
import type { RecordUnknownInput } from "../domain/residualUnknown.js";
|
|
|
|
|
import { recordDerivedEvidence } from "./recordDerivedEvidence.js";
|
2026-09-29 16:28:46 +08:00
|
|
|
import { recordSessionEvidenceSources } from "./sessionEvidence.js";
|
2026-08-10 02:28:11 +08:00
|
|
|
import { runDerivedOperation } from "./runDerivedOperation.js";
|
2026-10-09 22:45:18 +08:00
|
|
|
import { FUNCTION_COMPARISON_PROVIDER } from "../application/InvestigationProviders.js";
|
2026-07-12 21:45:26 +00:00
|
|
|
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
2026-10-11 00:03:24 +08:00
|
|
|
import { withAdmittedAnalysis } from "./analysisAdmission.js";
|
|
|
|
|
import { runAdmittedToolOperation } from "./admittedToolOperation.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
|
2026-10-09 22:45:18 +08:00
|
|
|
import { compareFunctions } from "../domain/functionComparison.js";
|
2026-10-13 04:12:34 +08:00
|
|
|
import { uniqueSorted } from "../domain/canonicalOrdering.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
/** Register explicit Evidence-backed function comparison. */
|
|
|
|
|
export const registerFunctionComparisonTool = (
|
2026-10-09 20:02:52 +08:00
|
|
|
server: EvidenceMcpServer,
|
2026-07-12 21:12:18 +00:00
|
|
|
session: BinarySessionPort,
|
2026-10-05 21:15:04 +08:00
|
|
|
contract: ReturnType<typeof toolContract<"compare_functions">>,
|
2026-07-12 21:12:18 +00:00
|
|
|
): void => {
|
2026-10-11 00:03:24 +08:00
|
|
|
const admission = withAdmittedAnalysis({ kind: "session", session });
|
2026-07-12 21:12:18 +00:00
|
|
|
server.registerTool(
|
|
|
|
|
contract.name,
|
2026-07-12 21:45:26 +00:00
|
|
|
toolRegistrationOptions(contract),
|
2026-10-11 00:03:24 +08:00
|
|
|
async (input, context) =>
|
|
|
|
|
runAdmittedToolOperation(
|
|
|
|
|
server,
|
|
|
|
|
admission,
|
|
|
|
|
contract.name,
|
|
|
|
|
context.mcpReq.signal,
|
|
|
|
|
async () => {
|
|
|
|
|
let leftEvidence: Evidence;
|
|
|
|
|
let rightEvidence: Evidence;
|
|
|
|
|
try {
|
|
|
|
|
leftEvidence = parseEvidence(input.left);
|
|
|
|
|
rightEvidence = parseEvidence(input.right);
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
return server.delivery.toCallToolResult(
|
|
|
|
|
err(
|
|
|
|
|
new EvidenceIntegrityError(
|
|
|
|
|
cause instanceof Error ? cause.message : "Invalid Evidence",
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
contract,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
leftEvidence.operation !== "analyze_function" ||
|
|
|
|
|
rightEvidence.operation !== "analyze_function" ||
|
|
|
|
|
leftEvidence.predicate_type !== "rea.analysis" ||
|
|
|
|
|
rightEvidence.predicate_type !== "rea.analysis"
|
|
|
|
|
)
|
|
|
|
|
return server.delivery.toCallToolResult(
|
|
|
|
|
err(
|
|
|
|
|
new EvidenceIntegrityError(
|
|
|
|
|
"Expected analyze_function Evidence",
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
contract,
|
|
|
|
|
);
|
|
|
|
|
const leftIds = [leftEvidence.evidence_id];
|
|
|
|
|
const rightIds = [rightEvidence.evidence_id];
|
|
|
|
|
const computed = await runDerivedOperation(
|
|
|
|
|
context,
|
|
|
|
|
contract.name,
|
|
|
|
|
() => compareFunctions(leftEvidence, rightEvidence),
|
|
|
|
|
);
|
|
|
|
|
if (!computed.ok)
|
|
|
|
|
return server.delivery.toCallToolResult(computed, contract);
|
|
|
|
|
const comparison = computed.value;
|
|
|
|
|
const recordedSources = recordSessionEvidenceSources(
|
|
|
|
|
(evidence) => session.recordEvidence(evidence),
|
|
|
|
|
[leftEvidence, rightEvidence],
|
|
|
|
|
);
|
|
|
|
|
if (!recordedSources.ok)
|
|
|
|
|
return server.delivery.toCallToolResult(recordedSources, contract);
|
|
|
|
|
const evidence = createEvidence(
|
|
|
|
|
undefined,
|
|
|
|
|
FUNCTION_COMPARISON_PROVIDER,
|
|
|
|
|
{
|
|
|
|
|
predicateType: "rea.function-comparison",
|
|
|
|
|
operation: contract.name,
|
|
|
|
|
parameters: {
|
|
|
|
|
left_evidence_id: leftEvidence.evidence_id,
|
|
|
|
|
right_evidence_id: rightEvidence.evidence_id,
|
|
|
|
|
},
|
|
|
|
|
result: jsonValueSchema.parse(comparison),
|
|
|
|
|
confidence: "derived",
|
|
|
|
|
authority: "analyst-inference",
|
|
|
|
|
limitations: comparison.limitations,
|
|
|
|
|
evidenceLinks: [...leftIds, ...rightIds],
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
const recorded = recordDerivedEvidence(
|
|
|
|
|
session,
|
|
|
|
|
evidence,
|
2026-10-13 04:12:34 +08:00
|
|
|
comparisonUnknownInput(
|
|
|
|
|
comparison,
|
2026-10-11 00:03:24 +08:00
|
|
|
leftIds,
|
|
|
|
|
rightIds,
|
2026-10-13 04:12:34 +08:00
|
|
|
evidence.evidence_id,
|
|
|
|
|
),
|
2026-10-11 00:03:24 +08:00
|
|
|
);
|
|
|
|
|
return server.delivery.toEvidenceToolResult(
|
|
|
|
|
evidence,
|
|
|
|
|
contract,
|
|
|
|
|
recorded,
|
|
|
|
|
);
|
2026-07-12 21:12:18 +00:00
|
|
|
},
|
2026-10-11 00:03:24 +08:00
|
|
|
),
|
2026-07-12 21:12:18 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-13 04:12:34 +08:00
|
|
|
const comparisonUnknownInput = (
|
|
|
|
|
comparison: ReturnType<typeof compareFunctions>,
|
|
|
|
|
leftIds: readonly string[],
|
|
|
|
|
rightIds: readonly string[],
|
|
|
|
|
comparisonEvidenceId: string,
|
|
|
|
|
): RecordUnknownInput | undefined =>
|
|
|
|
|
functionUnknownInput({
|
|
|
|
|
status: comparison.status,
|
|
|
|
|
dimensions: comparison.dimensions
|
|
|
|
|
.filter(({ status }) =>
|
|
|
|
|
comparison.status === "unknown"
|
|
|
|
|
? status === "unknown"
|
|
|
|
|
: status === "changed",
|
|
|
|
|
)
|
|
|
|
|
.map(({ dimension }) => dimension),
|
|
|
|
|
leftIds,
|
|
|
|
|
rightIds,
|
|
|
|
|
comparisonEvidenceId,
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-12 21:12:18 +00:00
|
|
|
const functionUnknownInput = ({
|
|
|
|
|
status,
|
2026-10-13 04:12:34 +08:00
|
|
|
dimensions,
|
2026-07-12 21:12:18 +00:00
|
|
|
leftIds,
|
|
|
|
|
rightIds,
|
2026-10-09 06:47:48 +08:00
|
|
|
comparisonEvidenceId,
|
2026-07-12 21:12:18 +00:00
|
|
|
}: {
|
|
|
|
|
status: ReturnType<typeof compareFunctions>["status"];
|
2026-10-13 04:12:34 +08:00
|
|
|
dimensions: readonly ReturnType<
|
|
|
|
|
typeof compareFunctions
|
|
|
|
|
>["dimensions"][number]["dimension"][];
|
2026-07-12 21:12:18 +00:00
|
|
|
leftIds: readonly string[];
|
|
|
|
|
rightIds: readonly string[];
|
2026-10-09 06:47:48 +08:00
|
|
|
comparisonEvidenceId: string;
|
2026-07-12 21:12:18 +00:00
|
|
|
}): RecordUnknownInput | undefined => {
|
2026-09-29 11:22:00 +08:00
|
|
|
if (status === "unchanged") return undefined;
|
2026-10-13 04:12:34 +08:00
|
|
|
const supportingIds = [
|
|
|
|
|
...new Set([...leftIds, ...rightIds, comparisonEvidenceId]),
|
|
|
|
|
];
|
2026-10-09 06:47:48 +08:00
|
|
|
const contradictingIds = rightIds.filter((id) => !leftIds.includes(id));
|
|
|
|
|
const contradictory = status === "changed";
|
2026-07-12 21:12:18 +00:00
|
|
|
return {
|
2026-10-09 06:47:48 +08:00
|
|
|
question: `Function comparison is ${status} (comparison ${comparisonEvidenceId})`,
|
2026-07-12 21:12:18 +00:00
|
|
|
severity: status === "changed" ? "medium" : "high",
|
|
|
|
|
domain: "function-comparison",
|
2026-10-13 04:12:34 +08:00
|
|
|
comparison_conclusion_scope: {
|
|
|
|
|
comparison_evidence_id: comparisonEvidenceId,
|
|
|
|
|
dimensions: uniqueSorted([
|
|
|
|
|
...(status === "unknown" || dimensions.length === 0
|
|
|
|
|
? ["overall" as const]
|
|
|
|
|
: []),
|
|
|
|
|
...dimensions,
|
|
|
|
|
]),
|
|
|
|
|
},
|
|
|
|
|
supporting_evidence_ids: contradictory
|
|
|
|
|
? [...leftIds, comparisonEvidenceId]
|
|
|
|
|
: supportingIds,
|
2026-10-09 06:47:48 +08:00
|
|
|
contradicting_evidence_ids: contradictory ? contradictingIds : [],
|
2026-07-12 21:12:18 +00:00
|
|
|
required_authority: "shipped-artifact",
|
|
|
|
|
required_confidence: "observed",
|
|
|
|
|
required_environment: null,
|
|
|
|
|
recommended_probes: [
|
|
|
|
|
{
|
|
|
|
|
operation: "analyze_function",
|
|
|
|
|
rationale:
|
2026-09-29 07:08:46 +08:00
|
|
|
"Capture complete dossiers for both functions under the same target context.",
|
2026-07-12 21:12:18 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
relationships: [],
|
|
|
|
|
};
|
|
|
|
|
};
|