refactor(mcp): make analysis states unrepresentable (#468)
* refactor(config): parse capability settings into exact states * refactor(runtime): encode replay and process outcomes exactly * refactor: make analysis states unrepresentable * refactor(mcp): register canonical schemas with SDK * fix: preserve capability and catalog invariants * fix(build): remove stale compiled modules * fix(replay): update real verifier policy dependency * fix(package): verify the complete MCP catalog * fix(test): supply complete replay reload policy * refactor(contracts): centralize output schema lookup * refactor(mcp): delegate protocol boundaries to SDK * fix(browser): approve real source capture * fix(mcp): preserve catalog guidance
This commit is contained in:
@@ -492,7 +492,7 @@ export class GhidraClient {
|
||||
}
|
||||
|
||||
#interruptionFailure(deadline: ProviderStartupDeadline): GhidraSessionError {
|
||||
return deadline.cancelled
|
||||
return deadline.interruption === "cancelled"
|
||||
? this.#failure("cancelled", "Ghidra startup was cancelled")
|
||||
: this.#failure("timeout", "Ghidra startup deadline elapsed", undefined, {
|
||||
timeoutMs: deadline.timeoutMs,
|
||||
|
||||
@@ -66,7 +66,7 @@ export async function completeGhidraStartupHandshake(
|
||||
const interruptionFailure = (
|
||||
options: GhidraStartupHandshakeOptions,
|
||||
): GhidraSessionError =>
|
||||
options.deadline.cancelled
|
||||
options.deadline.interruption === "cancelled"
|
||||
? options.failure("cancelled", "Ghidra startup was cancelled")
|
||||
: options.failure("timeout", "Ghidra startup deadline elapsed", undefined, {
|
||||
timeoutMs: options.startupTimeoutMs,
|
||||
|
||||
@@ -6,11 +6,7 @@ import {
|
||||
functionDossierSchema,
|
||||
type FunctionDossier,
|
||||
} from "../domain/hopperValues.js";
|
||||
import {
|
||||
jsonObjectSchema,
|
||||
jsonValueSchema,
|
||||
type JsonValue,
|
||||
} from "../domain/jsonValue.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { nativeApiBoundarySchema } from "../domain/nativeApiBoundary.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import {
|
||||
@@ -117,7 +113,7 @@ export const parseGhidraFunctionInput = (
|
||||
): Result<Readonly<Record<string, JsonValue>>, AnalysisInputError> => {
|
||||
const parsed = inputSchemas[operation].safeParse(value);
|
||||
return parsed.success
|
||||
? ok(jsonObjectSchema.parse(parsed.data))
|
||||
? ok(parsed.data)
|
||||
: err(new AnalysisInputError(operation, { cause: parsed.error }));
|
||||
};
|
||||
|
||||
@@ -171,15 +167,24 @@ const referenceEdge = z
|
||||
kind: referenceKind,
|
||||
})
|
||||
.strict();
|
||||
const boundedReferenceFacts = {
|
||||
items: z.array(referenceEdge),
|
||||
total: z.number().int().min(0).nullable(),
|
||||
returned: z.number().int().min(0),
|
||||
} as const;
|
||||
const boundedReferences = z
|
||||
.object({
|
||||
items: z.array(referenceEdge),
|
||||
total: z.number().int().min(0).nullable(),
|
||||
returned: z.number().int().min(0),
|
||||
truncated: z.boolean(),
|
||||
next_offset: z.number().int().min(0).nullable(),
|
||||
})
|
||||
.strict()
|
||||
.discriminatedUnion("truncated", [
|
||||
z.strictObject({
|
||||
...boundedReferenceFacts,
|
||||
truncated: z.literal(false),
|
||||
next_offset: z.null(),
|
||||
}),
|
||||
z.strictObject({
|
||||
...boundedReferenceFacts,
|
||||
truncated: z.literal(true),
|
||||
next_offset: z.number().int().min(0).nullable(),
|
||||
}),
|
||||
])
|
||||
.superRefine((value, context) => {
|
||||
if (value.returned !== value.items.length)
|
||||
context.addIssue({
|
||||
@@ -193,12 +198,6 @@ const boundedReferences = z
|
||||
path: ["total"],
|
||||
message: "total cannot be smaller than returned",
|
||||
});
|
||||
if (value.next_offset !== null && !value.truncated)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["next_offset"],
|
||||
message: "a continuation requires truncated output",
|
||||
});
|
||||
});
|
||||
const procedureReferences = z
|
||||
.object({
|
||||
@@ -345,7 +344,7 @@ export const parseGhidraFunctionResult = (
|
||||
): Result<JsonValue, AnalysisOutputError> => {
|
||||
const parsed = resultSchemas[operation].safeParse(value);
|
||||
return parsed.success
|
||||
? ok(jsonValueSchema.parse(parsed.data))
|
||||
? ok(parsed.data)
|
||||
: err(
|
||||
new AnalysisOutputError(
|
||||
operation,
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { AnalysisInputError, AnalysisOutputError } from "../domain/errors.js";
|
||||
import {
|
||||
jsonObjectSchema,
|
||||
jsonValueSchema,
|
||||
type JsonValue,
|
||||
} from "../domain/jsonValue.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
/** Read-only direct operations admitted by the Ghidra v1 adapter. */
|
||||
@@ -80,7 +76,7 @@ export const parseGhidraInventoryInput = (
|
||||
const parsed = inputSchemas[operation].safeParse(value);
|
||||
if (!parsed.success)
|
||||
return err(new AnalysisInputError(operation, { cause: parsed.error }));
|
||||
return ok(jsonObjectSchema.parse(parsed.data));
|
||||
return ok(parsed.data);
|
||||
};
|
||||
|
||||
export const ghidraCanonicalAddressSchema = z
|
||||
@@ -126,17 +122,29 @@ const procedureItem = z
|
||||
const stringItem = z.object({ ...baseItem, string: stringFacts }).strict();
|
||||
const searchItem = z.object(baseItem).strict();
|
||||
|
||||
const pageSchema = <Item extends z.ZodType>(item: Item, maximumLimit: number) =>
|
||||
z
|
||||
.object({
|
||||
items: z.array(item),
|
||||
offset: z.number().int().min(0),
|
||||
limit: z.number().int().min(1).max(maximumLimit),
|
||||
total: z.number().int().min(0),
|
||||
next_offset: z.number().int().min(0).nullable(),
|
||||
has_more: z.boolean(),
|
||||
})
|
||||
.strict()
|
||||
const pageSchema = <Item extends z.ZodType>(
|
||||
item: Item,
|
||||
maximumLimit: number,
|
||||
) => {
|
||||
const pageFacts = {
|
||||
items: z.array(item),
|
||||
offset: z.number().int().min(0),
|
||||
limit: z.number().int().min(1).max(maximumLimit),
|
||||
total: z.number().int().min(0),
|
||||
} as const;
|
||||
return z
|
||||
.discriminatedUnion("has_more", [
|
||||
z.strictObject({
|
||||
...pageFacts,
|
||||
has_more: z.literal(true),
|
||||
next_offset: z.number().int().min(0),
|
||||
}),
|
||||
z.strictObject({
|
||||
...pageFacts,
|
||||
has_more: z.literal(false),
|
||||
next_offset: z.null(),
|
||||
}),
|
||||
])
|
||||
.superRefine((page, context) => {
|
||||
const next = page.offset + page.items.length;
|
||||
const hasMore = next < page.total;
|
||||
@@ -171,6 +179,7 @@ const pageSchema = <Item extends z.ZodType>(item: Item, maximumLimit: number) =>
|
||||
message: "page continuation offset is inconsistent",
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const availablePermissions = z
|
||||
.object({
|
||||
@@ -241,7 +250,7 @@ export const parseGhidraInventoryResult = (
|
||||
): Result<JsonValue, AnalysisOutputError> => {
|
||||
const parsed = resultSchemas[operation].safeParse(value);
|
||||
return parsed.success
|
||||
? ok(jsonValueSchema.parse(parsed.data))
|
||||
? ok(parsed.data)
|
||||
: err(
|
||||
new AnalysisOutputError(
|
||||
operation,
|
||||
|
||||
@@ -37,11 +37,11 @@ export interface GhidraLaunchSession {
|
||||
}
|
||||
|
||||
/** Process plus private log coordinates returned by a Ghidra launcher. */
|
||||
export interface GhidraLaunch extends ProviderProcessLaunch {
|
||||
export type GhidraLaunch = ProviderProcessLaunch & {
|
||||
readonly projectRoot: string;
|
||||
readonly ghidraLogPath: string;
|
||||
readonly scriptLogPath: string;
|
||||
}
|
||||
};
|
||||
|
||||
/** Provider-owned capability that starts one isolated headless analysis. */
|
||||
export interface GhidraLauncher {
|
||||
@@ -178,7 +178,7 @@ const ownedGhidraProcess = (
|
||||
spawned: SpawnedOwnedProviderProcess,
|
||||
platform: NodeJS.Platform,
|
||||
): Pick<
|
||||
GhidraLaunch,
|
||||
Extract<ProviderProcessLaunch, { readonly ownsProcessLifetime: true }>,
|
||||
"process" | "ownsProcessLifetime" | "ownership" | "cleanup"
|
||||
> => ({
|
||||
process: spawned.process,
|
||||
|
||||
@@ -71,7 +71,6 @@ import {
|
||||
export { GHIDRA_PROVIDER_IDENTITY, GHIDRA_PROVIDER_TOOL_CONTRACTS };
|
||||
|
||||
const SUPPORTED_ARCHITECTURES = new Set(["x86", "x86_64", "arm", "arm64"]);
|
||||
const SUPPORTED_FORMATS = new Set(["elf", "pe", "mach-o"]);
|
||||
|
||||
/** Production seam for exercising provider projection without a real process. */
|
||||
export type GhidraProviderClientFactory = (
|
||||
@@ -155,17 +154,7 @@ export class GhidraProvider implements AnalysisProviderCandidate {
|
||||
};
|
||||
if (hostPlatform === "win32")
|
||||
return inspectWindowsP0TargetSupport(target, diagnostics);
|
||||
if (!SUPPORTED_FORMATS.has(target.format))
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "target_format_unsupported",
|
||||
reason: `Ghidra v1 does not import ${target.format} through this adapter.`,
|
||||
diagnostics,
|
||||
};
|
||||
if (
|
||||
target.architecture === undefined ||
|
||||
!SUPPORTED_ARCHITECTURES.has(target.architecture)
|
||||
)
|
||||
if (!SUPPORTED_ARCHITECTURES.has(target.architecture))
|
||||
return {
|
||||
status: "unsupported",
|
||||
code: "architecture_unsupported",
|
||||
|
||||
@@ -148,7 +148,7 @@ const connectGhidraSocketOnce = (
|
||||
const interruptionFailure = (
|
||||
options: GhidraSocketConnectOptions,
|
||||
): GhidraSessionError =>
|
||||
options.deadline.cancelled
|
||||
options.deadline.interruption === "cancelled"
|
||||
? options.failure("cancelled", "Ghidra startup was cancelled")
|
||||
: options.failure("timeout", "Ghidra startup deadline elapsed", undefined, {
|
||||
timeoutMs: options.startupTimeoutMs,
|
||||
|
||||
+13
-41
@@ -3,46 +3,23 @@ import { z } from "zod";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
const responseSchema = z
|
||||
.object({
|
||||
const responseSchema = z.discriminatedUnion("ok", [
|
||||
z.strictObject({
|
||||
id: z.number().int().positive(),
|
||||
ok: z.boolean(),
|
||||
result: jsonValueSchema.optional(),
|
||||
ok: z.literal(true),
|
||||
result: jsonValueSchema,
|
||||
}),
|
||||
z.strictObject({
|
||||
id: z.number().int().positive(),
|
||||
ok: z.literal(false),
|
||||
error: z
|
||||
.object({
|
||||
code: z.string().min(1).max(128),
|
||||
message: z.string().min(1).max(4_096),
|
||||
})
|
||||
.strict()
|
||||
.optional(),
|
||||
})
|
||||
.strict()
|
||||
.superRefine((value, context) => {
|
||||
if (value.ok && value.result === undefined)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["result"],
|
||||
message: "Successful Ghidra response requires a result",
|
||||
});
|
||||
if (!value.ok && value.error === undefined)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["error"],
|
||||
message: "Failed Ghidra response requires an error",
|
||||
});
|
||||
if (!value.ok && value.result !== undefined)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["result"],
|
||||
message: "Failed Ghidra response cannot include a result",
|
||||
});
|
||||
if (value.ok && value.error !== undefined)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["error"],
|
||||
message: "Successful Ghidra response cannot include an error",
|
||||
});
|
||||
});
|
||||
.strict(),
|
||||
}),
|
||||
]);
|
||||
|
||||
/** Validated response emitted by REA's packaged Ghidra script. */
|
||||
export type GhidraBridgeResponse = z.infer<typeof responseSchema>;
|
||||
@@ -80,10 +57,5 @@ export const ghidraResponseResult = (
|
||||
response: GhidraBridgeResponse,
|
||||
): Result<JsonValue, GhidraRemoteError> =>
|
||||
response.ok
|
||||
? ok(response.result ?? null)
|
||||
: err(
|
||||
new GhidraRemoteError(
|
||||
response.error?.code ?? "bridge_error",
|
||||
response.error?.message ?? "Ghidra bridge request failed",
|
||||
),
|
||||
);
|
||||
? ok(response.result)
|
||||
: err(new GhidraRemoteError(response.error.code, response.error.message));
|
||||
|
||||
Reference in New Issue
Block a user