fix(cli): resolve xrefs symbol selectors after address validation (#1209)
* fix(cli): resolve xrefs symbol selectors after address validation * style(cli): format xrefs fallback call
This commit is contained in:
@@ -48,6 +48,8 @@ import {
|
||||
} from "./WorkflowEvidence.js";
|
||||
import type { AnalysisProviderSelector } from "../contracts/providerSelection.js";
|
||||
import { artifactInspectionResultSchema } from "../domain/artifactInspection.js";
|
||||
import { resolveXrefsAddress } from "./XrefsAddressResolution.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
|
||||
type DirectAnalysisTool =
|
||||
| "annotate_native_function"
|
||||
@@ -356,7 +358,34 @@ const executeAnalysisTool = async (input: {
|
||||
});
|
||||
return { output: evidence, evidence };
|
||||
}
|
||||
const result = await session.execute(tool, input.arguments, { signal });
|
||||
let result = await session.execute(tool, input.arguments, { signal });
|
||||
if (
|
||||
tool === "xrefs" &&
|
||||
typeof input.arguments.address === "string" &&
|
||||
!result.ok &&
|
||||
result.error instanceof AnalysisInputError &&
|
||||
!result.error.cleanupIncomplete &&
|
||||
result.error.issues.length > 0 &&
|
||||
result.error.issues.every(
|
||||
({ path, reason }) =>
|
||||
path.length === 1 &&
|
||||
path[0] === "address" &&
|
||||
reason === "invalid_format",
|
||||
)
|
||||
) {
|
||||
const address = await resolveXrefsAddress(
|
||||
session,
|
||||
input.arguments.address,
|
||||
signal,
|
||||
);
|
||||
if (!address.ok) return { output: cliError(address.error) };
|
||||
if (address.value !== input.arguments.address)
|
||||
result = await session.execute(
|
||||
tool,
|
||||
{ ...input.arguments, address: address.value },
|
||||
{ signal },
|
||||
);
|
||||
}
|
||||
if (!result.ok) return { output: cliError(result.error) };
|
||||
const evidence = createEvidence(
|
||||
result.value.subject ?? input.openedTarget,
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { addressedValue } from "../contracts/toolOutputSchemaPrimitives.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import {
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisInputError,
|
||||
AnalysisOutputError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { AnalysisOperationPort } from "./AnalysisProvider.js";
|
||||
import { resolveProcedureAddress } from "./ProcedureAddressResolution.js";
|
||||
|
||||
/** Resolve a CLI symbol selector without changing existing address spellings. */
|
||||
export const resolveXrefsAddress = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
selector: string,
|
||||
signal: AbortSignal,
|
||||
): Promise<Result<string, AnalysisError>> => {
|
||||
if (
|
||||
selector.length === 0 ||
|
||||
/^[0-9a-f]+$/iu.test(selector) ||
|
||||
/^0x/iu.test(selector) ||
|
||||
/:0x/iu.test(selector)
|
||||
)
|
||||
return ok(selector);
|
||||
|
||||
const inventory = await analysis.execute("list_names", {}, { signal });
|
||||
if (!inventory.ok) {
|
||||
if (
|
||||
inventory.error instanceof AnalysisCapabilityUnavailableError &&
|
||||
inventory.error.operation === "list_names" &&
|
||||
!inventory.error.cleanupIncomplete
|
||||
)
|
||||
return resolveProcedureAddress(
|
||||
async (operation, parameters, signal) => {
|
||||
const result = await analysis.execute(
|
||||
operation,
|
||||
parameters,
|
||||
signal === undefined ? {} : { signal },
|
||||
);
|
||||
return result.ok ? ok(result.value.result) : result;
|
||||
},
|
||||
selector,
|
||||
signal,
|
||||
);
|
||||
return inventory;
|
||||
}
|
||||
if (!Array.isArray(inventory.value.result))
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
"list_names",
|
||||
"Expected a symbol inventory array",
|
||||
),
|
||||
);
|
||||
|
||||
const addresses = new Set<string>();
|
||||
for (const [index, item] of inventory.value.result.entries()) {
|
||||
const parsed = addressedValue.safeParse(item);
|
||||
if (!parsed.success || parsed.data.address.length === 0)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
"list_names",
|
||||
`Invalid symbol inventory entry at index ${index}`,
|
||||
),
|
||||
);
|
||||
if (parsed.data.value === selector) addresses.add(parsed.data.address);
|
||||
}
|
||||
const [address] = addresses;
|
||||
if (addresses.size === 1 && address !== undefined) return ok(address);
|
||||
return err(
|
||||
new AnalysisInputError("xrefs", undefined, [
|
||||
{
|
||||
path: ["address"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
addresses.size === 0
|
||||
? `Unknown analyzed symbol name ${JSON.stringify(selector)}. Use an exact analyzed name or a hexadecimal address.`
|
||||
: `Analyzed symbol name ${JSON.stringify(selector)} is ambiguous at ${[...addresses].join(", ")}. Select an exact address.`,
|
||||
},
|
||||
]),
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user