fix(errors): improve recovery guidance

This commit is contained in:
morluto
2026-07-13 19:09:27 +00:00
parent af8c4efb4a
commit 77454dc8f4
42 changed files with 1492 additions and 304 deletions
+2 -8
View File
@@ -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),
+4 -9
View File
@@ -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;
};
+1 -1
View File
@@ -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: [],
+7 -4
View File
@@ -86,7 +86,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",
@@ -140,7 +141,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",
@@ -195,7 +197,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",
@@ -274,7 +277,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",
+11 -15
View File
@@ -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;
};
+1 -8
View File
@@ -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;
+1 -1
View File
@@ -46,7 +46,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],
+2 -9
View File
@@ -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);
};