Files
reverse-engineering-anything/src/hopper/protocol.ts
T
morluto b1b0bdf011 fix(hopper): name xrefs when a selector resolves outside every procedure (#14188)
* fix(hopper): name xrefs when a selector resolves outside every procedure

The bridge collapsed every procedure-selector miss into "No procedure
exists at the requested address", even when the caller passed a name
that Hopper resolved to an import slot or data label. The TypeScript
error then attached one fixed remediation that mixed CLI and MCP
spellings and recommended list_names, which has no CLI command.

The bridge now reports how the selector resolved (symbol, address,
cursor, or unresolved name) with the resolved address. HopperRemoteError
derives its remediation and related operations from those facts: a
resolved non-procedure symbol names xrefs on its address, and the
remediation names contract operations only so each adapter renders its
own spelling through related_operations.

Fixes #14165

* test(hopper): parse selector diagnostics through the bridge wire contract

The procedure-selection regression cast the bridge diagnostic to never
and projected it directly, so the selector-resolution schema never ran.
It now frames each diagnostic as an NDJSON reply and parses it with the
Hopper reply envelope, covers a cursor outside every procedure, and
requires malformed selector resolutions to fail as protocol errors.
2026-11-23 23:27:12 +00:00

152 lines
4.2 KiB
TypeScript

import { z } from "zod";
import {
HOPPER_DIAGNOSTIC_TYPES,
HopperProtocolError,
HopperRemoteError,
type HopperSelectorResolution,
} from "./HopperErrors.js";
import { ownedJsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
import { safeParseJson } from "../domain/safeJson.js";
import type { JsonLineBridgeEnvelope } from "../process/JsonLineBridgeReader.js";
const hopperAddressSchema = z.string().regex(/^0x[0-9a-f]+$/u);
/** How the bridge resolved a selector that named no procedure. */
const selectorResolutionSchema = z.discriminatedUnion("source", [
z.object({ value: z.string(), source: z.literal("unresolved") }).strict(),
z
.object({
value: z.string(),
source: z.enum(["symbol", "address"]),
address: hopperAddressSchema,
})
.strict(),
z
.object({
value: z.null(),
source: z.literal("cursor"),
address: hopperAddressSchema,
})
.strict(),
]) satisfies z.ZodType<HopperSelectorResolution>;
const remoteErrorSchema = z
.object({
code: z.int(),
message: z.string(),
type: z.enum(HOPPER_DIAGNOSTIC_TYPES).default("remote"),
selector: selectorResolutionSchema.optional(),
})
.strict();
const responseSchema = z.union([
z
.object({
id: z.int().nonnegative(),
result: ownedJsonValueSchema,
})
.strict(),
z
.object({
id: z.int().nonnegative(),
error: remoteErrorSchema,
})
.strict(),
]);
const eventSchema = z
.object({
id: z.int().nonnegative(),
event: z.discriminatedUnion("type", [
z
.object({
type: z.literal("progress"),
phase: z.string().min(1),
completed: z.number().nonnegative().finite(),
total: z.number().nonnegative().finite().nullable(),
message: z.string().min(1),
terminal: z.boolean().optional(),
})
.strict(),
z
.object({
type: z.literal("diagnostic"),
error: remoteErrorSchema,
})
.strict(),
]),
})
.strict()
.superRefine((message, context) => {
if (
message.event.type === "progress" &&
message.event.total !== null &&
message.event.completed > message.event.total
) {
context.addIssue({
code: "custom",
message: "Progress total cannot be less than completed",
path: ["event", "total"],
});
}
});
const messageSchema = z.union([responseSchema, eventSchema]);
export type HopperResponse = z.infer<typeof responseSchema>;
export type HopperBridgeEvent = z.infer<typeof eventSchema>;
export type HopperBridgeMessage = z.infer<typeof messageSchema>;
/** Parse one complete Hopper NDJSON response or event line. */
const parseBridgeMessageLine = (
line: string,
): Result<HopperBridgeMessage, HopperProtocolError> => {
const decodedResult = safeParseJson(line);
if (!decodedResult.ok)
return err(
new HopperProtocolError("Hopper returned malformed JSON", {
cause: decodedResult.cause,
}),
);
const decoded: unknown = decodedResult.value;
const parsed = messageSchema.safeParse(decoded);
return parsed.success
? ok(parsed.data)
: err(
new HopperProtocolError(
"Hopper returned a message outside the bridge contract",
{ cause: parsed.error },
),
);
};
const hopperRemoteError = (
error: z.infer<typeof remoteErrorSchema>,
): HopperRemoteError =>
new HopperRemoteError(error.code, error.message, {
diagnosticType: error.type,
...(error.selector === undefined ? {} : { selector: error.selector }),
});
/** Project a parsed response into its result or expected remote failure. */
export const responseResult = (
response: HopperResponse,
): Result<JsonValue, HopperRemoteError> =>
"error" in response
? err(hopperRemoteError(response.error))
: ok(response.result);
/** Hopper's `result | error` plus events envelope for the shared JSON-line reader. */
export const hopperReplyEnvelope: JsonLineBridgeEnvelope<
HopperBridgeMessage,
HopperRemoteError
> = {
parse: parseBridgeMessageLine,
id: (message) => message.id,
remoteFailure: (message) =>
"error" in message ? hopperRemoteError(message.error) : undefined,
};