feat(native): inspect API boundaries with evidence

This commit is contained in:
morluto
2026-07-25 14:29:28 +00:00
parent 5184816199
commit 521139d4a5
39 changed files with 1382 additions and 63 deletions
+4 -2
View File
@@ -1,8 +1,9 @@
import { z } from "zod";
import type { JsonValue } from "./jsonValue.js";
import { jsonValueSchema, type JsonValue } from "./jsonValue.js";
import { AnalysisOutputError, HopperProtocolError } from "./errors.js";
import { err, ok, type Result } from "./result.js";
import { nativeApiBoundarySchema } from "./nativeApiBoundary.js";
export interface AddressedName {
readonly address: string;
@@ -275,6 +276,7 @@ export const functionDossierSchema = z
instruction_scan: z
.object({ scanned: z.number().int().min(0), truncated: z.boolean() })
.strict(),
native_api: nativeApiBoundarySchema.optional(),
limitations: z.array(z.string()).default([]),
})
.strict();
@@ -288,7 +290,7 @@ export const parseFunctionDossier = (
): Result<JsonValue, AnalysisOutputError> => {
const parsed = functionDossierSchema.safeParse(value);
return parsed.success
? ok(parsed.data)
? ok(jsonValueSchema.parse(parsed.data))
: err(
new AnalysisOutputError(
"analyze_function",
+153
View File
@@ -0,0 +1,153 @@
import { z } from "zod";
const inferenceConfidenceSchema = z.enum(["low", "medium", "high"]);
const inferenceEvidenceSchema = z
.object({
kind: z.enum([
"signature-source",
"decompiler-type",
"type-lock",
"calling-convention",
"jump-table",
]),
source: z.string().min(1),
detail: z.string().min(1),
})
.strict();
const inferredBoundaryTypeSchema = z
.object({
role: z.enum(["return", "parameter"]),
ordinal: z.number().int().min(0).nullable(),
name: z.string().min(1).nullable(),
data_type: z.string().min(1),
size_bytes: z.number().int().min(0).nullable(),
storage: z.string().min(1).nullable(),
confidence: inferenceConfidenceSchema,
evidence: z.array(inferenceEvidenceSchema).min(1),
decompiler_artifacts: z.array(
z.enum([
"recovered-signature",
"register-or-stack-storage",
"compiler-generated-variable",
]),
),
})
.strict();
const jumpTableDataSourceSchema = z
.object({
address: z.string().min(1),
provenance: z.enum([
"ghidra-decompiler-load-table",
"ghidra-dispatch-block-data-reference",
]),
entry_size_bytes: z.number().int().min(1).nullable(),
entry_count: z.number().int().min(0).nullable(),
confidence: inferenceConfidenceSchema,
evidence: z.array(inferenceEvidenceSchema).min(1),
})
.strict();
const jumpTableMappingSchema = z
.object({
case_value: z.number().int().nullable(),
target_address: z.string().min(1),
data_addresses: z.array(z.string().min(1)),
confidence: inferenceConfidenceSchema,
evidence: z.array(inferenceEvidenceSchema).min(1),
})
.strict();
const inferredJumpTableSchema = z
.object({
dispatch_address: z.string().min(1),
data_sources: z.array(jumpTableDataSourceSchema),
data_sources_truncated: z.boolean(),
mappings: z.array(jumpTableMappingSchema),
mappings_truncated: z.boolean(),
limitations: z.array(z.string()),
})
.strict();
const availableNativeApiBoundarySchema = z
.object({
available: z.literal(true),
provenance: z.string().min(1),
signature_source: z.string().min(1),
calling_convention: z.string().min(1),
return_type: inferredBoundaryTypeSchema,
parameters: z.array(inferredBoundaryTypeSchema),
parameters_truncated: z.boolean(),
jump_tables: z.array(inferredJumpTableSchema),
jump_tables_truncated: z.boolean(),
pseudocode: z
.object({
classification: z.literal("decompiler-generated-non-source"),
compilable: z.literal(false),
})
.strict(),
decompiler_artifacts: z.array(
z.enum([
"recovered-signature",
"register-and-stack-variables",
"compiler-generated-control-flow",
"pointer-arithmetic",
"pseudocode",
]),
),
limitations: z.array(z.string()),
})
.strict();
const unavailableNativeApiBoundarySchema = z
.object({
available: z.literal(false),
reason: z.string().min(1),
residual_unknowns: z.array(z.string().min(1)).min(1),
})
.strict();
/** Provider-neutral native API boundary observations attached to a function dossier. */
export const nativeApiBoundarySchema = z.discriminatedUnion("available", [
unavailableNativeApiBoundarySchema,
availableNativeApiBoundarySchema,
]);
/** One validated provider-neutral native API boundary observation. */
export type NativeApiBoundary = z.infer<typeof nativeApiBoundarySchema>;
const nativeApiInspectionSubstepSchema = z
.object({
operation: z.enum([
"analyze_function",
"project_native_api_boundary",
"preserve_residual_unknowns",
]),
status: z.enum(["completed", "unsupported"]),
observations: z.array(z.string().min(1)),
})
.strict();
/** Agent-facing projection of structured native API reconstruction substeps. */
export const nativeApiInspectionResultSchema = z
.object({
schema_version: z.literal(1),
procedure: z
.object({
address: z.string().min(1),
name: z.string().min(1),
})
.strict(),
boundary: nativeApiBoundarySchema,
substeps: z.array(nativeApiInspectionSubstepSchema).min(3).max(3),
unsupported_branches: z.array(z.string().min(1)),
residual_unknowns: z.array(z.string().min(1)),
})
.strict();
/** One validated agent-facing native API inspection result. */
export type NativeApiInspectionResult = z.infer<
typeof nativeApiInspectionResultSchema
>;