fix(contracts)!: return structured workflow failures
Replace prose-encoded partial failures in batch decompilation, call graphs, and named xref lookup with discriminated structured results. BREAKING CHANGE: batch_decompile, get_call_graph, and find_xrefs_to_name now return structured discriminated output shapes.
This commit is contained in:
@@ -8,9 +8,11 @@ import {
|
||||
AnalysisInputError,
|
||||
AnalysisCancelledError,
|
||||
AnalysisOutputError,
|
||||
projectAnalysisError,
|
||||
type AnalysisError,
|
||||
} from "../domain/errors.js";
|
||||
import {
|
||||
addressDistance,
|
||||
parseDocuments,
|
||||
parseFunctionDossier,
|
||||
parseAddressedPage,
|
||||
@@ -134,26 +136,40 @@ export class EnhancedTools {
|
||||
addresses: readonly string[],
|
||||
signal?: AbortSignal,
|
||||
): EnhancedResult {
|
||||
if (addresses.length === 0) return ok({ error: "No addresses provided" });
|
||||
|
||||
const entries = await Promise.all(
|
||||
const items = await Promise.all(
|
||||
addresses.map(async (address) => {
|
||||
const result = await this.#call(
|
||||
"procedure_pseudo_code",
|
||||
{ procedure: address },
|
||||
signal,
|
||||
);
|
||||
return [
|
||||
address,
|
||||
result.ok
|
||||
? result.value === null || result.value === ""
|
||||
? "No output"
|
||||
: result.value
|
||||
: `Error: ${result.error.message}`,
|
||||
] as const;
|
||||
if (!result.ok)
|
||||
return {
|
||||
address,
|
||||
status: "error" as const,
|
||||
error: projectAnalysisError(result.error),
|
||||
};
|
||||
if (typeof result.value !== "string" || result.value.length === 0)
|
||||
return {
|
||||
address,
|
||||
status: "error" as const,
|
||||
error: projectAnalysisError(
|
||||
new AnalysisOutputError(
|
||||
"procedure_pseudo_code",
|
||||
"provider returned empty pseudocode",
|
||||
),
|
||||
),
|
||||
};
|
||||
return { address, status: "ok" as const, pseudocode: result.value };
|
||||
}),
|
||||
);
|
||||
return ok(Object.fromEntries(entries));
|
||||
const succeeded = items.filter(({ status }) => status === "ok").length;
|
||||
return ok({
|
||||
items,
|
||||
total: items.length,
|
||||
succeeded,
|
||||
failed: items.length - succeeded,
|
||||
});
|
||||
}
|
||||
|
||||
async #callGraph(
|
||||
@@ -190,7 +206,8 @@ export class EnhancedTools {
|
||||
if (!result.ok) {
|
||||
graph[level].push({
|
||||
address: current.address,
|
||||
error: result.error.message,
|
||||
status: "error",
|
||||
error: projectAnalysisError(result.error),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
@@ -198,12 +215,14 @@ export class EnhancedTools {
|
||||
if (!related.ok) {
|
||||
graph[level].push({
|
||||
address: current.address,
|
||||
error: related.error.message,
|
||||
status: "error",
|
||||
error: projectAnalysisError(related.error),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
graph[level].push({
|
||||
address: current.address,
|
||||
status: "ok",
|
||||
calls: [...related.value],
|
||||
});
|
||||
if (current.depth + 1 < input.depth) {
|
||||
@@ -226,21 +245,34 @@ export class EnhancedTools {
|
||||
}
|
||||
|
||||
async #findXrefs(name: string, signal?: AbortSignal): EnhancedResult {
|
||||
const resolved = await this.#call(
|
||||
"address_name",
|
||||
{ address: name },
|
||||
const names = await this.#allAddressed("list_names", signal);
|
||||
if (!names.ok) return names;
|
||||
const resolved = names.value.find((entry) => entry.name === name);
|
||||
if (resolved === undefined)
|
||||
return ok({ status: "unresolved", name, reason: "name_not_found" });
|
||||
|
||||
const xrefs = await this.#call(
|
||||
"xrefs",
|
||||
{ address: resolved.address },
|
||||
signal,
|
||||
);
|
||||
if (!resolved.ok) return resolved;
|
||||
const address = resolveAddress(resolved.value);
|
||||
if (address === undefined)
|
||||
return ok({ error: `Could not resolve name: ${name}` });
|
||||
|
||||
const xrefs = await this.#call("xrefs", { address }, signal);
|
||||
if (!xrefs.ok) return xrefs;
|
||||
return ok(
|
||||
Array.isArray(xrefs.value) ? { xrefs: xrefs.value } : xrefs.value,
|
||||
);
|
||||
if (
|
||||
!Array.isArray(xrefs.value) ||
|
||||
xrefs.value.some((xref) => typeof xref !== "string")
|
||||
)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
"xrefs",
|
||||
"provider returned an invalid address list",
|
||||
),
|
||||
);
|
||||
return ok({
|
||||
status: "resolved",
|
||||
name,
|
||||
address: resolved.address,
|
||||
xrefs: xrefs.value,
|
||||
});
|
||||
}
|
||||
|
||||
async #binaryOverview(
|
||||
@@ -491,17 +523,3 @@ const invalidInput = (name: EnhancedToolName, cause: Error): EnhancedResult =>
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const resolveAddress = (value: JsonValue): string | undefined => {
|
||||
if (typeof value === "string" && value.length > 0) return value;
|
||||
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
||||
return undefined;
|
||||
}
|
||||
const candidate = value.address ?? value.name;
|
||||
return typeof candidate === "string" && candidate.length > 0
|
||||
? candidate
|
||||
: undefined;
|
||||
};
|
||||
|
||||
const addressDistance = (start: string, end: string): number =>
|
||||
Math.max(0, Number.parseInt(end, 16) - Number.parseInt(start, 16));
|
||||
|
||||
@@ -365,12 +365,12 @@ export const ENHANCED_TOOL_CONTRACTS = [
|
||||
),
|
||||
enhanced(
|
||||
"batch_decompile",
|
||||
"Decompile up to 20 explicit procedure symbols or addresses concurrently. Per-item strings may contain errors or no-output markers, so validate each result; use analyze_function for a richer single-function dossier.",
|
||||
"Decompile up to 20 explicit procedure symbols or addresses concurrently. Returns ordered per-item ok/error variants and aggregate counts; use analyze_function for a richer single-function dossier.",
|
||||
enhancedInputSchemas.batch_decompile,
|
||||
),
|
||||
enhanced(
|
||||
"get_call_graph",
|
||||
"Traverse Hopper's caller or callee relationships from one symbol or address for at most five levels. Nodes preserve per-procedure errors; indirect calls may be missing and results are not a whole-program CFG.",
|
||||
"Traverse Hopper's caller or callee relationships from one symbol or address for at most five levels. Every node has an ok/error status and failures use safe typed projections; indirect calls may be missing and results are not a whole-program CFG.",
|
||||
enhancedInputSchemas.get_call_graph,
|
||||
),
|
||||
enhanced(
|
||||
@@ -380,7 +380,7 @@ export const ENHANCED_TOOL_CONTRACTS = [
|
||||
),
|
||||
enhanced(
|
||||
"find_xrefs_to_name",
|
||||
"Resolve a name through Hopper and return analyzed references to its address. Use when starting from a selector or symbol; resolution failure is returned explicitly and xrefs remain untyped.",
|
||||
"Resolve an exact name through Hopper's exhaustively paged name inventory and return a resolved or unresolved result. Unresolved names use the stable name_not_found reason; xrefs remain untyped.",
|
||||
enhancedInputSchemas.find_xrefs_to_name,
|
||||
),
|
||||
enhanced(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { z } from "zod";
|
||||
import { ANALYSIS_ERROR_TAGS } from "../domain/errors.js";
|
||||
import { evidenceSchema } from "../domain/evidence.js";
|
||||
import {
|
||||
processCaptureComparisonSchema,
|
||||
@@ -177,9 +178,25 @@ const symbolDiscoveryOutput = (property: "classes" | "protocols") =>
|
||||
[property]: z.array(addressedEntry),
|
||||
}),
|
||||
);
|
||||
const graphNode = z.union([
|
||||
z.object({ address: z.string(), calls: z.array(z.string()) }),
|
||||
z.object({ address: z.string(), error: z.string() }),
|
||||
/** Exact public projection of an expected analysis failure. */
|
||||
export const analysisErrorProjectionSchema = z
|
||||
.object({
|
||||
tag: z.enum(ANALYSIS_ERROR_TAGS),
|
||||
message: z.string(),
|
||||
details: z.record(z.string(), z.union([z.string(), z.number(), z.null()])),
|
||||
})
|
||||
.strict();
|
||||
const graphNode = z.discriminatedUnion("status", [
|
||||
z.object({
|
||||
address: z.string(),
|
||||
status: z.literal("ok"),
|
||||
calls: z.array(z.string()),
|
||||
}),
|
||||
z.object({
|
||||
address: z.string(),
|
||||
status: z.literal("error"),
|
||||
error: analysisErrorProjectionSchema,
|
||||
}),
|
||||
]);
|
||||
const functionDossierOutput = resultOf(functionDossierSchema);
|
||||
|
||||
@@ -242,10 +259,25 @@ export const enhancedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
|
||||
get_objc_classes: symbolDiscoveryOutput("classes"),
|
||||
get_objc_protocols: symbolDiscoveryOutput("protocols"),
|
||||
batch_decompile: resultOf(
|
||||
z.union([
|
||||
z.object({ error: z.literal("No addresses provided") }),
|
||||
z.record(z.string(), z.string()),
|
||||
]),
|
||||
z.object({
|
||||
items: z.array(
|
||||
z.discriminatedUnion("status", [
|
||||
z.object({
|
||||
address: z.string(),
|
||||
status: z.literal("ok"),
|
||||
pseudocode: z.string().min(1),
|
||||
}),
|
||||
z.object({
|
||||
address: z.string(),
|
||||
status: z.literal("error"),
|
||||
error: analysisErrorProjectionSchema,
|
||||
}),
|
||||
]),
|
||||
),
|
||||
total: z.number().int().min(0),
|
||||
succeeded: z.number().int().min(0),
|
||||
failed: z.number().int().min(0),
|
||||
}),
|
||||
),
|
||||
get_call_graph: resultOf(z.record(z.string(), z.array(graphNode))),
|
||||
analyze_swift_types: resultOf(
|
||||
@@ -261,9 +293,18 @@ export const enhancedOutputSchemas: Readonly<Record<string, z.ZodObject>> = {
|
||||
}),
|
||||
),
|
||||
find_xrefs_to_name: resultOf(
|
||||
z.union([
|
||||
z.object({ xrefs: addressList }),
|
||||
z.object({ error: z.string() }),
|
||||
z.discriminatedUnion("status", [
|
||||
z.object({
|
||||
status: z.literal("resolved"),
|
||||
name: z.string(),
|
||||
address: z.string(),
|
||||
xrefs: addressList,
|
||||
}),
|
||||
z.object({
|
||||
status: z.literal("unresolved"),
|
||||
name: z.string(),
|
||||
reason: z.literal("name_not_found"),
|
||||
}),
|
||||
]),
|
||||
),
|
||||
binary_overview: resultOf(
|
||||
|
||||
+33
-25
@@ -1,27 +1,34 @@
|
||||
export type AnalysisErrorTag =
|
||||
| "AnalysisProtocolError"
|
||||
| "AnalysisInputError"
|
||||
| "AnalysisOutputError"
|
||||
| "AnalysisCapabilityUnavailableError"
|
||||
| "AnalysisCancelledError"
|
||||
| "AnalysisTimeoutError"
|
||||
| "ProviderSelectionError"
|
||||
| "ProviderAdapterError"
|
||||
| "ArtifactOperationError"
|
||||
| "ProcessCaptureError"
|
||||
| "EvidenceIntegrityError"
|
||||
| "EvidenceLimitError"
|
||||
| "EvidenceFileError"
|
||||
| "UnknownRegistryError"
|
||||
| "HopperTimeoutError"
|
||||
| "HopperCancelledError"
|
||||
| "HopperProtocolError"
|
||||
| "HopperRemoteError"
|
||||
| "HopperProcessError"
|
||||
| "HopperStartError"
|
||||
| "ConfigurationError"
|
||||
| "NoBinaryOpenError"
|
||||
| "BinaryTargetError";
|
||||
import type { JsonValue } from "./jsonValue.js";
|
||||
|
||||
/** Stable tags exposed by safe analysis-error projections. */
|
||||
export const ANALYSIS_ERROR_TAGS = [
|
||||
"AnalysisProtocolError",
|
||||
"AnalysisInputError",
|
||||
"AnalysisOutputError",
|
||||
"AnalysisCapabilityUnavailableError",
|
||||
"AnalysisCancelledError",
|
||||
"AnalysisTimeoutError",
|
||||
"ProviderSelectionError",
|
||||
"ProviderAdapterError",
|
||||
"ArtifactOperationError",
|
||||
"ProcessCaptureError",
|
||||
"EvidenceIntegrityError",
|
||||
"EvidenceLimitError",
|
||||
"EvidenceFileError",
|
||||
"UnknownRegistryError",
|
||||
"HopperTimeoutError",
|
||||
"HopperCancelledError",
|
||||
"HopperProtocolError",
|
||||
"HopperRemoteError",
|
||||
"HopperProcessError",
|
||||
"HopperStartError",
|
||||
"ConfigurationError",
|
||||
"NoBinaryOpenError",
|
||||
"BinaryTargetError",
|
||||
] as const;
|
||||
|
||||
/** Stable tag for an expected analysis failure. */
|
||||
export type AnalysisErrorTag = (typeof ANALYSIS_ERROR_TAGS)[number];
|
||||
|
||||
/** Base class for expected analysis, provider, and session failures. */
|
||||
export abstract class AnalysisError extends Error {
|
||||
@@ -280,7 +287,8 @@ export class BinaryTargetError extends AnalysisError {
|
||||
}
|
||||
}
|
||||
|
||||
export interface AnalysisErrorProjection {
|
||||
export interface AnalysisErrorProjection
|
||||
extends Readonly<Record<string, JsonValue>> {
|
||||
readonly tag: AnalysisErrorTag;
|
||||
readonly message: string;
|
||||
readonly details: Readonly<Record<string, string | number | null>>;
|
||||
|
||||
@@ -24,6 +24,10 @@ export interface AddressedPage {
|
||||
readonly hasMore: boolean;
|
||||
}
|
||||
|
||||
/** Return the non-negative byte distance between hexadecimal addresses. */
|
||||
export const addressDistance = (start: string, end: string): number =>
|
||||
Math.max(0, Number.parseInt(end, 16) - Number.parseInt(start, 16));
|
||||
|
||||
const procedureMapSchema = z.record(z.string(), z.string());
|
||||
const addressedNamesSchema = z.array(
|
||||
z.object({ address: z.string(), name: z.string() }),
|
||||
|
||||
Reference in New Issue
Block a user