Merge remote-tracking branch 'origin/main' into feat/persistent-workspace-investigation-runs
# Conflicts: # README.md # scripts/verify-package.mjs # src/cli.ts # src/domain/errors.ts # src/server/registerInvestigationTools.ts # tests/errors.test.ts
This commit is contained in:
@@ -16,6 +16,7 @@ export interface CreateServerOptions {
|
||||
readonly logger?: Logger;
|
||||
readonly processPolicy?: ProcessExecutionPolicy;
|
||||
readonly evidenceFilePolicy?: EvidenceFilePolicy;
|
||||
readonly analysisSnapshotFilePolicy?: EvidenceFilePolicy;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,7 +13,7 @@ import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { enhancedInputSchemas } from "../contracts/enhancedInputs.js";
|
||||
import { UnknownRegistryError } from "../domain/errors.js";
|
||||
|
||||
@@ -69,15 +69,9 @@ const registerEnhancedTool = (
|
||||
annotations: contract.annotations,
|
||||
},
|
||||
async (input, context) => {
|
||||
const parsedInput = jsonValueSchema.parse(
|
||||
const parsedInput = jsonObjectSchema.parse(
|
||||
enhancedInputSchemas[name].parse(input),
|
||||
);
|
||||
if (
|
||||
typeof parsedInput !== "object" ||
|
||||
parsedInput === null ||
|
||||
Array.isArray(parsedInput)
|
||||
)
|
||||
throw new Error("Validated enhanced input was not a JSON object");
|
||||
const parameters: Record<string, JsonValue> = { ...parsedInput };
|
||||
const result = await logToolExecution(registration.logger, name, () =>
|
||||
services.execute(name, input, context.mcpReq.signal),
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import type { ToolContract } from "../contracts/toolContracts.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { jsonObjectSchema } from "../domain/jsonValue.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
@@ -32,7 +32,9 @@ export const registerEvidenceTools = (
|
||||
contract.name,
|
||||
toolRegistrationOptions(contract),
|
||||
async (input, context) => {
|
||||
const parameters = jsonObject(contract.inputSchema.parse(input));
|
||||
const parameters = jsonObjectSchema.parse(
|
||||
contract.inputSchema.parse(input),
|
||||
);
|
||||
const execution = await logToolExecution(
|
||||
options.logger,
|
||||
contract.name,
|
||||
@@ -62,10 +64,3 @@ export const registerEvidenceTools = (
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const jsonObject = (input: unknown): Readonly<Record<string, JsonValue>> => {
|
||||
const parsed = jsonValueSchema.parse(input);
|
||||
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
||||
throw new TypeError("Provider tool input was not an object");
|
||||
return parsed;
|
||||
};
|
||||
|
||||
@@ -94,7 +94,7 @@ const functionUnknownInput = ({
|
||||
{
|
||||
operation: "analyze_function",
|
||||
rationale:
|
||||
"Capture complete dossiers with equal providers and analysis limits.",
|
||||
"Capture complete dossiers for both functions under the same target context and analysis limits.",
|
||||
},
|
||||
],
|
||||
relationships: [],
|
||||
|
||||
@@ -129,7 +129,8 @@ const registerChangedBehavior = (
|
||||
result.behavior_status === "unknown" ||
|
||||
result.behavior_status === "truncated",
|
||||
{
|
||||
question: `Changed behavior result ${evidence.evidence_id} remains ${result.behavior_status}`,
|
||||
question:
|
||||
"Did both versions behave the same under a complete controlled replay?",
|
||||
domain: "changed-behavior",
|
||||
requiredAuthority: "controlled-replay",
|
||||
requiredConfidence: "observed",
|
||||
@@ -184,7 +185,8 @@ const registerCallPath = (
|
||||
parsed.unknown_registry_approved,
|
||||
result.status === "unknown" || result.status === "truncated",
|
||||
{
|
||||
question: `Call path ${parsed.start.address} → ${parsed.goal.address} is ${result.status}`,
|
||||
question:
|
||||
"Can the requested call path be established from complete analysis?",
|
||||
domain: "call-path",
|
||||
requiredAuthority: "shipped-artifact",
|
||||
requiredConfidence: "derived",
|
||||
@@ -239,7 +241,8 @@ const registerStaticRuntime = (
|
||||
parsed.unknown_registry_approved,
|
||||
result.status === "unknown" || result.status === "truncated",
|
||||
{
|
||||
question: `Static/runtime correlation result ${evidence.evidence_id} is ${result.status}`,
|
||||
question:
|
||||
"Does runtime behavior match the available static analysis?",
|
||||
domain: "static-runtime-correlation",
|
||||
requiredAuthority: null,
|
||||
requiredConfidence: "derived",
|
||||
@@ -318,7 +321,7 @@ const registerReconstruction = (
|
||||
parsed.unknown_registry_approved,
|
||||
result.status === "unknown",
|
||||
{
|
||||
question: `Reconstruction ${result.specification_sha256} has unknown claims`,
|
||||
question: "Does the reconstruction satisfy every declared claim?",
|
||||
domain: "reconstruction-verification",
|
||||
requiredAuthority: null,
|
||||
requiredConfidence: "derived",
|
||||
|
||||
@@ -3,7 +3,11 @@ import type { McpServer } from "@modelcontextprotocol/server";
|
||||
import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import { OFFICIAL_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import {
|
||||
jsonObjectSchema,
|
||||
jsonValueSchema,
|
||||
type JsonValue,
|
||||
} from "../domain/jsonValue.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
import type { Logger } from "../logger.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
@@ -92,9 +96,10 @@ const registerOfficialTool = (
|
||||
) {
|
||||
const unknown = registration.recordUnknown({
|
||||
approved: true,
|
||||
question: `${contract.name} is unavailable: ${result.error.reason}`,
|
||||
question:
|
||||
"The requested analysis is unavailable for the current target.",
|
||||
severity: "medium",
|
||||
domain: "provider-capability",
|
||||
domain: "analysis-capability",
|
||||
supporting_evidence_ids: [],
|
||||
contradicting_evidence_ids: [],
|
||||
required_authority: "shipped-artifact",
|
||||
@@ -104,7 +109,7 @@ const registerOfficialTool = (
|
||||
{
|
||||
operation: contract.name,
|
||||
rationale:
|
||||
"Use a provider that declares this capability available.",
|
||||
"Choose another analysis or target that can answer this question.",
|
||||
},
|
||||
],
|
||||
relationships: [],
|
||||
@@ -127,21 +132,12 @@ const projectOfficialArguments = (
|
||||
contract: (typeof OFFICIAL_TOOL_CONTRACTS)[number],
|
||||
input: unknown,
|
||||
): Readonly<Record<string, JsonValue>> => {
|
||||
const parsed = jsonValueSchema.safeParse(input);
|
||||
if (
|
||||
!parsed.success ||
|
||||
typeof parsed.data !== "object" ||
|
||||
parsed.data === null ||
|
||||
Array.isArray(parsed.data)
|
||||
) {
|
||||
// The SDK validates this with the same schema before invoking the callback.
|
||||
throw new Error("Validated MCP tool input was not a JSON object");
|
||||
}
|
||||
const parsed = jsonObjectSchema.parse(contract.inputSchema.parse(input));
|
||||
|
||||
const projected: Record<string, JsonValue> = {};
|
||||
for (const key of Object.keys(contract.inputSchema.shape)) {
|
||||
if (key === "unknown_registry_approved") continue;
|
||||
projected[key] = parsed.data[key] ?? null;
|
||||
projected[key] = jsonValueSchema.parse(parsed[key] ?? null);
|
||||
}
|
||||
return projected;
|
||||
};
|
||||
|
||||
@@ -119,7 +119,7 @@ const validateCaptureSources = (
|
||||
evidence.confidence !== "observed" ||
|
||||
evidence.authority !== "controlled-replay" ||
|
||||
evidenceCapture === undefined ||
|
||||
canonicalJson(evidenceCapture) !== canonicalJson(capture)
|
||||
canonicalize(evidenceCapture) !== canonicalize(capture)
|
||||
)
|
||||
return err(
|
||||
new EvidenceIntegrityError(
|
||||
@@ -130,13 +130,6 @@ const validateCaptureSources = (
|
||||
return { ok: true as const, value: null };
|
||||
};
|
||||
|
||||
const canonicalJson = (value: unknown): string => {
|
||||
const encoded = canonicalize(value);
|
||||
if (encoded === undefined)
|
||||
throw new TypeError("Process capture could not be canonicalized");
|
||||
return encoded;
|
||||
};
|
||||
|
||||
const comparisonUnknownInput = (
|
||||
parsed: {
|
||||
readonly left_evidence_id: string;
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { McpServer } from "@modelcontextprotocol/server";
|
||||
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import { SESSION_TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
|
||||
/** Register the read-only provider and target status operation. */
|
||||
export const registerSessionStatusTool = (
|
||||
server: McpServer,
|
||||
session: BinarySessionPort,
|
||||
contract: (typeof SESSION_TOOL_CONTRACTS)[2],
|
||||
): void => {
|
||||
server.registerTool(
|
||||
contract.name,
|
||||
{
|
||||
description: contract.description,
|
||||
inputSchema: contract.inputSchema,
|
||||
outputSchema: contract.outputSchema,
|
||||
annotations: contract.annotations,
|
||||
},
|
||||
() => toCallToolResult({ ok: true, value: session.status() }, contract),
|
||||
);
|
||||
};
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
import { processScenarioSchema } from "../domain/processCapture.js";
|
||||
import { captureProcessScenario } from "../application/ProcessHarness.js";
|
||||
import type { Evidence } from "../domain/evidence.js";
|
||||
import type { AnalysisSnapshot } from "../domain/analysisSnapshot.js";
|
||||
import {
|
||||
recordUnknownInputSchema,
|
||||
updateUnknownInputSchema,
|
||||
@@ -24,6 +25,10 @@ import {
|
||||
readEvidenceBundle,
|
||||
writeEvidenceBundle,
|
||||
} from "../application/EvidenceBundleFiles.js";
|
||||
import {
|
||||
readAnalysisSnapshot,
|
||||
writeAnalysisSnapshot,
|
||||
} from "../application/AnalysisSnapshotFiles.js";
|
||||
import { UnknownRegistryError, type AnalysisError } from "../domain/errors.js";
|
||||
import {
|
||||
DENY_EVIDENCE_FILE_POLICY,
|
||||
@@ -35,6 +40,11 @@ import { registerArtifactComparisonTool } from "./registerArtifactComparisonTool
|
||||
import { registerFunctionComparisonTool } from "./registerFunctionComparisonTool.js";
|
||||
import { registerBundleComparisonTool } from "./registerBundleComparisonTool.js";
|
||||
import { registerInvestigationTools } from "./registerInvestigationTools.js";
|
||||
import {
|
||||
closeBinaryInputSchema,
|
||||
openBinaryInputSchema,
|
||||
} from "../contracts/sessionLifecycleInputs.js";
|
||||
import { registerSessionStatusTool } from "./registerSessionStatusTool.js";
|
||||
|
||||
const recordProcessResidualUnknowns = (
|
||||
session: BinarySessionPort,
|
||||
@@ -46,7 +56,7 @@ const recordProcessResidualUnknowns = (
|
||||
for (const residual of residuals) {
|
||||
const unknown = session.recordUnknown({
|
||||
approved: true,
|
||||
question: `${residual.scope}: ${residual.reason}`,
|
||||
question: `Was ${residual.scope} behavior fully observed during capture?`,
|
||||
severity: "medium",
|
||||
domain: `process-${residual.scope}`,
|
||||
supporting_evidence_ids: [evidence.evidence_id],
|
||||
@@ -305,16 +315,25 @@ const registerUnknownTools = ({
|
||||
);
|
||||
};
|
||||
|
||||
const registerLifecycleTools = (
|
||||
server: McpServer,
|
||||
session: BinarySessionPort,
|
||||
logger: Logger,
|
||||
contracts: readonly [
|
||||
interface LifecycleToolRegistration {
|
||||
readonly server: McpServer;
|
||||
readonly session: BinarySessionPort;
|
||||
readonly logger: Logger;
|
||||
readonly contracts: readonly [
|
||||
(typeof SESSION_TOOL_CONTRACTS)[0],
|
||||
(typeof SESSION_TOOL_CONTRACTS)[1],
|
||||
(typeof SESSION_TOOL_CONTRACTS)[2],
|
||||
],
|
||||
): void => {
|
||||
];
|
||||
readonly snapshotFilePolicy: EvidenceFilePolicy;
|
||||
}
|
||||
|
||||
const registerLifecycleTools = ({
|
||||
server,
|
||||
session,
|
||||
logger,
|
||||
contracts,
|
||||
snapshotFilePolicy,
|
||||
}: LifecycleToolRegistration): void => {
|
||||
const [openContract, closeContract, statusContract] = contracts;
|
||||
server.registerTool(
|
||||
openContract.name,
|
||||
@@ -325,9 +344,21 @@ const registerLifecycleTools = (
|
||||
annotations: openContract.annotations,
|
||||
},
|
||||
async (input, context) => {
|
||||
const parsed = z.object({ path: z.string().min(1) }).parse(input);
|
||||
const parsed = openBinaryInputSchema.parse(input);
|
||||
let snapshot: AnalysisSnapshot | undefined;
|
||||
if (parsed.snapshot_path !== undefined) {
|
||||
const loaded = await readAnalysisSnapshot(
|
||||
parsed.snapshot_path,
|
||||
snapshotFilePolicy,
|
||||
);
|
||||
if (!loaded.ok) return toCallToolResult(loaded, openContract);
|
||||
snapshot = loaded.value;
|
||||
}
|
||||
const opened = await logToolExecution(logger, openContract.name, () =>
|
||||
session.open(parsed.path, { signal: context.mcpReq.signal }),
|
||||
session.open(parsed.path, {
|
||||
signal: context.mcpReq.signal,
|
||||
...(snapshot === undefined ? {} : { snapshot }),
|
||||
}),
|
||||
);
|
||||
return opened.ok
|
||||
? toCallToolResult(
|
||||
@@ -355,31 +386,48 @@ const registerLifecycleTools = (
|
||||
outputSchema: closeContract.outputSchema,
|
||||
annotations: closeContract.annotations,
|
||||
},
|
||||
async () =>
|
||||
toCallToolResult(
|
||||
async (input) => {
|
||||
const parsed = closeBinaryInputSchema.parse(input);
|
||||
if (parsed.snapshot_path !== undefined) {
|
||||
const snapshot = session.exportAnalysisSnapshot();
|
||||
if (!snapshot.ok) return toCallToolResult(snapshot, closeContract);
|
||||
const written = await writeAnalysisSnapshot(
|
||||
snapshot.value,
|
||||
parsed.snapshot_path,
|
||||
parsed.overwrite,
|
||||
snapshotFilePolicy,
|
||||
);
|
||||
if (!written.ok) return toCallToolResult(written, closeContract);
|
||||
const closed = await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close(),
|
||||
);
|
||||
return closed.ok
|
||||
? toCallToolResult(
|
||||
ok({
|
||||
path: written.value.path,
|
||||
bytes: written.value.bytes,
|
||||
entries: snapshot.value.entries.length,
|
||||
}),
|
||||
closeContract,
|
||||
)
|
||||
: toCallToolResult(closed, closeContract);
|
||||
}
|
||||
return toCallToolResult(
|
||||
await logToolExecution(logger, closeContract.name, () =>
|
||||
session.close(),
|
||||
),
|
||||
closeContract,
|
||||
),
|
||||
);
|
||||
server.registerTool(
|
||||
statusContract.name,
|
||||
{
|
||||
description: statusContract.description,
|
||||
inputSchema: statusContract.inputSchema,
|
||||
outputSchema: statusContract.outputSchema,
|
||||
annotations: statusContract.annotations,
|
||||
);
|
||||
},
|
||||
() =>
|
||||
toCallToolResult({ ok: true, value: session.status() }, statusContract),
|
||||
);
|
||||
registerSessionStatusTool(server, session, statusContract);
|
||||
};
|
||||
|
||||
/** Register MCP-only target lifecycle operations on a long-lived session. */
|
||||
export interface SessionToolOptions {
|
||||
readonly processPolicy?: ProcessExecutionPolicy;
|
||||
readonly evidenceFilePolicy?: EvidenceFilePolicy;
|
||||
readonly analysisSnapshotFilePolicy?: EvidenceFilePolicy;
|
||||
}
|
||||
|
||||
export const registerSessionTools = (
|
||||
@@ -391,6 +439,8 @@ export const registerSessionTools = (
|
||||
const processPolicy = options.processPolicy ?? DENY_PROCESS_POLICY;
|
||||
const evidenceFilePolicy =
|
||||
options.evidenceFilePolicy ?? DENY_EVIDENCE_FILE_POLICY;
|
||||
const analysisSnapshotFilePolicy =
|
||||
options.analysisSnapshotFilePolicy ?? DENY_EVIDENCE_FILE_POLICY;
|
||||
const [
|
||||
openContract,
|
||||
closeContract,
|
||||
@@ -411,11 +461,13 @@ export const registerSessionTools = (
|
||||
updateUnknownContract,
|
||||
verifyUnknownContract,
|
||||
] = SESSION_TOOL_CONTRACTS;
|
||||
registerLifecycleTools(server, session, logger, [
|
||||
openContract,
|
||||
closeContract,
|
||||
statusContract,
|
||||
]);
|
||||
registerLifecycleTools({
|
||||
server,
|
||||
session,
|
||||
logger,
|
||||
contracts: [openContract, closeContract, statusContract],
|
||||
snapshotFilePolicy: analysisSnapshotFilePolicy,
|
||||
});
|
||||
registerEvidenceTools({
|
||||
server,
|
||||
session,
|
||||
|
||||
@@ -17,7 +17,7 @@ export const resolveSessionEvidence = (
|
||||
for (const item of supplied) {
|
||||
const parsed = parseEvidence(item);
|
||||
const owned = session.evidenceById(parsed.evidence_id);
|
||||
if (owned === undefined || canonicalJson(owned) !== canonicalJson(parsed))
|
||||
if (owned === undefined || canonicalize(owned) !== canonicalize(parsed))
|
||||
return err(
|
||||
new EvidenceIntegrityError(
|
||||
"Comparison input does not match its session-owned Evidence",
|
||||
@@ -34,12 +34,5 @@ export const isSessionEvidence = (
|
||||
input: Evidence,
|
||||
): boolean => {
|
||||
const owned = session.evidenceById(input.evidence_id);
|
||||
return owned !== undefined && canonicalJson(owned) === canonicalJson(input);
|
||||
};
|
||||
|
||||
const canonicalJson = (value: unknown): string => {
|
||||
const encoded = canonicalize(value);
|
||||
if (encoded === undefined)
|
||||
throw new TypeError("Evidence could not be canonicalized");
|
||||
return encoded;
|
||||
return owned !== undefined && canonicalize(owned) === canonicalize(input);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import type { CallToolResult } from "@modelcontextprotocol/server";
|
||||
|
||||
import { projectAnalysisError, type AnalysisError } from "../domain/errors.js";
|
||||
import {
|
||||
AnalysisOutputError,
|
||||
projectAnalysisError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { ToolContract } from "../contracts/toolContracts.js";
|
||||
@@ -22,9 +26,10 @@ const errorResult = (error: AnalysisError): CallToolResult => {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `${projected.tag}: ${projected.message}`,
|
||||
text: projected.message,
|
||||
},
|
||||
],
|
||||
structuredContent: { error: projected },
|
||||
isError: true,
|
||||
};
|
||||
};
|
||||
@@ -40,9 +45,17 @@ const successResult = (
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "AnalysisOutputError: Analysis output does not match the tool contract",
|
||||
text: "Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
|
||||
},
|
||||
],
|
||||
structuredContent: {
|
||||
error: projectAnalysisError(
|
||||
new AnalysisOutputError(
|
||||
contract.name,
|
||||
"output does not match the tool contract",
|
||||
),
|
||||
),
|
||||
},
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user