fix(analysis): resolve procedure identities and preserve literal queries
This commit is contained in:
@@ -5,6 +5,7 @@ import { parseRelatedAddresses } from "../domain/hopperValues.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { ok, type Result } from "../domain/result.js";
|
||||
import type { EnhancedResult } from "./EnhancedToolTypes.js";
|
||||
import { resolveProcedureAddress } from "./ProcedureAddressResolution.js";
|
||||
|
||||
type AnalysisCall = (
|
||||
name: AnalysisOperation,
|
||||
@@ -61,7 +62,20 @@ export const traceCallPath = async (
|
||||
input: CallPathTraceInput,
|
||||
signal?: AbortSignal,
|
||||
): EnhancedResult => {
|
||||
const state = createTraceState(input);
|
||||
const start = await resolveProcedureAddress(call, input.start, signal);
|
||||
if (!start.ok) return start;
|
||||
let goal: string | undefined;
|
||||
if (input.goal !== undefined) {
|
||||
const resolved = await resolveProcedureAddress(call, input.goal, signal);
|
||||
if (!resolved.ok) return resolved;
|
||||
goal = resolved.value;
|
||||
}
|
||||
const resolvedInput = {
|
||||
start: start.value,
|
||||
...(goal === undefined ? {} : { goal }),
|
||||
direction: input.direction,
|
||||
};
|
||||
const state = createTraceState(resolvedInput);
|
||||
while (
|
||||
state.queueIndex < state.queue.length &&
|
||||
state.traversalPath.length === 0
|
||||
@@ -71,7 +85,7 @@ export const traceCallPath = async (
|
||||
const current = state.visited.get(address);
|
||||
if (current === undefined) continue;
|
||||
const found = await expandAddress(call, {
|
||||
input,
|
||||
input: resolvedInput,
|
||||
state,
|
||||
address,
|
||||
current,
|
||||
@@ -79,7 +93,7 @@ export const traceCallPath = async (
|
||||
});
|
||||
if (found) break;
|
||||
}
|
||||
return ok(projectTrace(input, state));
|
||||
return ok(projectTrace(resolvedInput, state));
|
||||
};
|
||||
|
||||
const createTraceState = (input: CallPathTraceInput): TraceState => ({
|
||||
|
||||
@@ -71,40 +71,37 @@ const literalMatches = async (
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<{ matches: TraceMatch[] }, AnalysisError>> => {
|
||||
const matches: TraceMatch[] = [];
|
||||
const needles = [...new Set(input.query.trim().split(/\s+/u))];
|
||||
const matched = new Set<string>();
|
||||
for (const needle of needles) {
|
||||
for (const [tool, type] of sources) {
|
||||
const result = await call(
|
||||
tool,
|
||||
{
|
||||
pattern: needle,
|
||||
mode: "literal",
|
||||
case_sensitive: input.case_sensitive,
|
||||
},
|
||||
signal,
|
||||
for (const [tool, type] of sources) {
|
||||
const result = await call(
|
||||
tool,
|
||||
{
|
||||
pattern: input.query,
|
||||
mode: "literal",
|
||||
case_sensitive: input.case_sensitive,
|
||||
},
|
||||
signal,
|
||||
);
|
||||
if (!result.ok) return result;
|
||||
const items = z
|
||||
.array(z.object({ address: z.string(), value: z.string() }))
|
||||
.safeParse(result.value);
|
||||
if (!items.success)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
tool,
|
||||
"provider returned an invalid search result",
|
||||
),
|
||||
);
|
||||
if (!result.ok) return result;
|
||||
const items = z
|
||||
.array(z.object({ address: z.string(), value: z.string() }))
|
||||
.safeParse(result.value);
|
||||
if (!items.success)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
tool,
|
||||
"provider returned an invalid search result",
|
||||
),
|
||||
);
|
||||
addPageMatches({
|
||||
items: items.data.map(({ address, value: name }) => ({
|
||||
address,
|
||||
name,
|
||||
})),
|
||||
type,
|
||||
matches,
|
||||
matched,
|
||||
});
|
||||
}
|
||||
addPageMatches({
|
||||
items: items.data.map(({ address, value: name }) => ({
|
||||
address,
|
||||
name,
|
||||
})),
|
||||
type,
|
||||
matches,
|
||||
matched,
|
||||
});
|
||||
}
|
||||
return ok({ matches });
|
||||
};
|
||||
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
} from "./EnhancedToolTypes.js";
|
||||
import { resolveAnalysisDocument } from "./AnalysisDocument.js";
|
||||
import { traceCallPath } from "./CallPathTracing.js";
|
||||
import { resolveProcedureAddress } from "./ProcedureAddressResolution.js";
|
||||
import { traceLiteralFeature } from "./EnhancedLiteralTracing.js";
|
||||
import { projectNativeApiInspection } from "./native/NativeApiInspection.js";
|
||||
export type { ValidatedEnhancedCall } from "./EnhancedToolTypes.js";
|
||||
@@ -296,9 +297,15 @@ export class EnhancedTools {
|
||||
const relation = input.direction === "forward" ? "callees" : "callers";
|
||||
const tool =
|
||||
input.direction === "forward" ? "procedure_callees" : "procedure_callers";
|
||||
const discovered = new Set([input.address]);
|
||||
const resolved = await resolveProcedureAddress(
|
||||
this.#call.bind(this),
|
||||
input.address,
|
||||
signal,
|
||||
);
|
||||
if (!resolved.ok) return resolved;
|
||||
const discovered = new Set([resolved.value]);
|
||||
const queue: Array<{ address: string; depth: number }> = [
|
||||
{ address: input.address, depth: 0 },
|
||||
{ address: resolved.value, depth: 0 },
|
||||
];
|
||||
let queueIndex = 0;
|
||||
const graph: Record<string, JsonValue[]> = {};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { AnalysisOperation } from "./AnalysisProvider.js";
|
||||
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
type AnalysisCall = (
|
||||
operation: AnalysisOperation,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<Result<JsonValue, AnalysisError>>;
|
||||
|
||||
/** Resolve a caller selector to an observed provider procedure identity. */
|
||||
export const resolveProcedureAddress = async (
|
||||
call: AnalysisCall,
|
||||
procedure: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<string, AnalysisError>> => {
|
||||
const result = await call("procedure_address", { procedure }, signal);
|
||||
if (!result.ok) return result;
|
||||
return typeof result.value === "string" && result.value.length > 0
|
||||
? ok(result.value)
|
||||
: err(
|
||||
new AnalysisOutputError(
|
||||
"procedure_address",
|
||||
"provider returned no canonical procedure address",
|
||||
),
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user