fix!: restore caching, preserve failures, and verify fixture claims (#1211)
Restore profile-bound Ghidra snapshot reuse, preserve direct-analysis results through cleanup failures, and grade configured agent fixture claims against returned evidence. Register the compiled agent-evaluation fixture entry for static analysis. Co-authored-by: YouWish <29498139+Y0UW1SH@users.noreply.github.com> Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
This commit is contained in:
@@ -162,7 +162,12 @@ type CapabilityAvailability =
|
||||
};
|
||||
|
||||
export type CapabilityDescriptor = CapabilityAvailability & {
|
||||
/** Live external state must be re-observed, including after snapshot import. */
|
||||
/**
|
||||
* Snapshot opts immutable, profile-bound reads into replay despite incidental
|
||||
* private filesystem writes. Live state must always be re-observed. Omitting
|
||||
* this policy retains conservative effect checks; mutation and UI-state
|
||||
* exclusions apply to every policy.
|
||||
*/
|
||||
readonly cachePolicy?: "snapshot" | "live";
|
||||
readonly provider: ProviderIdentity;
|
||||
readonly operation: Exclude<AnalysisOperation, "health">;
|
||||
|
||||
+120
-111
@@ -50,6 +50,11 @@ import type { AnalysisProviderSelector } from "../contracts/providerSelection.js
|
||||
import { artifactInspectionResultSchema } from "../domain/artifactInspection.js";
|
||||
import { resolveXrefsAddress } from "./XrefsAddressResolution.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import {
|
||||
directAnalysisCleanupFailure,
|
||||
managedAnalysisCleanupFailure,
|
||||
withSessionCleanup,
|
||||
} from "./DirectAnalysisCleanup.js";
|
||||
|
||||
type DirectAnalysisTool =
|
||||
| "annotate_native_function"
|
||||
@@ -134,13 +139,15 @@ export const runManagedProviderExecution = async (
|
||||
): Promise<Result<AnalysisExecution, AnalysisError>> =>
|
||||
withProcessCancellation(signal, async (operationSignal) => {
|
||||
const session = dependencies.createManagedBinarySession();
|
||||
try {
|
||||
const opened = await session.open(path, { signal: operationSignal });
|
||||
if (!opened.ok) return opened;
|
||||
return await session.execute(tool, {}, { signal: operationSignal });
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
return withSessionCleanup(
|
||||
session,
|
||||
async () => {
|
||||
const opened = await session.open(path, { signal: operationSignal });
|
||||
if (!opened.ok) return opened;
|
||||
return await session.execute(tool, {}, { signal: operationSignal });
|
||||
},
|
||||
managedAnalysisCleanupFailure,
|
||||
);
|
||||
});
|
||||
|
||||
const runManagedProviderAnalysis = async (
|
||||
@@ -196,119 +203,121 @@ const runAnalysis = async (
|
||||
const config = parseConfig(options.environment ?? process.env);
|
||||
if (!config.ok) return cliError(config.error);
|
||||
const session = dependencies.createBinarySession(config.value, logger);
|
||||
try {
|
||||
const prepared = await prepareSnapshot({
|
||||
path,
|
||||
snapshotPath,
|
||||
...(options.formatHint === undefined
|
||||
? {}
|
||||
: { formatHint: options.formatHint }),
|
||||
});
|
||||
if (!prepared.ok) return cliError(prepared.error);
|
||||
const { snapshot } = prepared.value;
|
||||
let resolvedTarget: ResolvedSessionOpen | undefined;
|
||||
if (snapshot !== undefined && prepared.value.target !== undefined) {
|
||||
const preview = await session.previewTarget(prepared.value.target, {
|
||||
signal,
|
||||
snapshot,
|
||||
return withSessionCleanup(
|
||||
session,
|
||||
async () => {
|
||||
const prepared = await prepareSnapshot({
|
||||
path,
|
||||
snapshotPath,
|
||||
...(options.formatHint === undefined
|
||||
? {}
|
||||
: { formatHint: options.formatHint }),
|
||||
});
|
||||
if (!prepared.ok) return cliError(prepared.error);
|
||||
const { snapshot } = prepared.value;
|
||||
let resolvedTarget: ResolvedSessionOpen | undefined;
|
||||
if (snapshot !== undefined && prepared.value.target !== undefined) {
|
||||
const preview = await session.previewTarget(prepared.value.target, {
|
||||
signal,
|
||||
snapshot,
|
||||
...(options.formatHint === undefined
|
||||
? {}
|
||||
: { formatHint: options.formatHint }),
|
||||
...(options.providerId === undefined
|
||||
? {}
|
||||
: { providerId: options.providerId }),
|
||||
});
|
||||
if (!preview.ok) return cliError(preview.error);
|
||||
resolvedTarget = preview.value;
|
||||
const route = preview.value.route;
|
||||
const bindingProfile = route.profile ?? undefined;
|
||||
const evidenceProfile = analysisProfileForRoute(route, tool);
|
||||
if (
|
||||
bindingProfile !== undefined &&
|
||||
evidenceProfile !== undefined &&
|
||||
allowsSnapshotReplay(route, tool)
|
||||
) {
|
||||
const cached = snapshotEvidenceForQuery(snapshot, {
|
||||
target: preview.value.target,
|
||||
bindingProfile,
|
||||
operation: tool,
|
||||
parameters: arguments_,
|
||||
provider: isWorkflowEvidenceTool(tool)
|
||||
? REA_WORKFLOW_PROVIDER
|
||||
: providerIdentityForRoute(route, tool),
|
||||
evidenceProfile,
|
||||
});
|
||||
if (cached !== undefined) return cached;
|
||||
}
|
||||
}
|
||||
const openOptions = {
|
||||
signal,
|
||||
...(options.formatHint === undefined
|
||||
? {}
|
||||
: { formatHint: options.formatHint }),
|
||||
...(snapshot === undefined ? {} : { snapshot }),
|
||||
...(options.providerId === undefined
|
||||
? {}
|
||||
: { providerId: options.providerId }),
|
||||
};
|
||||
const opened =
|
||||
resolvedTarget === undefined
|
||||
? await session.open(path, openOptions)
|
||||
: await session.openResolvedTarget(resolvedTarget, openOptions);
|
||||
if (!opened.ok) return cliError(opened.error);
|
||||
const evidenceProfile = analysisProfileForEvidence(session, tool);
|
||||
const { output, evidence } = await executeAnalysisTool({
|
||||
session,
|
||||
openedTarget: opened.value,
|
||||
tool,
|
||||
arguments: arguments_,
|
||||
signal,
|
||||
evidenceProfile,
|
||||
});
|
||||
if (!preview.ok) return cliError(preview.error);
|
||||
resolvedTarget = preview.value;
|
||||
const route = preview.value.route;
|
||||
const bindingProfile = route.profile ?? undefined;
|
||||
const evidenceProfile = analysisProfileForRoute(route, tool);
|
||||
if (
|
||||
bindingProfile !== undefined &&
|
||||
evidenceProfile !== undefined &&
|
||||
allowsSnapshotReplay(route, tool)
|
||||
) {
|
||||
const cached = snapshotEvidenceForQuery(snapshot, {
|
||||
target: preview.value.target,
|
||||
bindingProfile,
|
||||
operation: tool,
|
||||
parameters: arguments_,
|
||||
provider: isWorkflowEvidenceTool(tool)
|
||||
? REA_WORKFLOW_PROVIDER
|
||||
: providerIdentityForRoute(route, tool),
|
||||
evidenceProfile,
|
||||
});
|
||||
if (cached !== undefined) return cached;
|
||||
}
|
||||
}
|
||||
const openOptions = {
|
||||
signal,
|
||||
...(options.formatHint === undefined
|
||||
? {}
|
||||
: { formatHint: options.formatHint }),
|
||||
...(snapshot === undefined ? {} : { snapshot }),
|
||||
...(options.providerId === undefined
|
||||
? {}
|
||||
: { providerId: options.providerId }),
|
||||
};
|
||||
const opened =
|
||||
resolvedTarget === undefined
|
||||
? await session.open(path, openOptions)
|
||||
: await session.openResolvedTarget(resolvedTarget, openOptions);
|
||||
if (!opened.ok) return cliError(opened.error);
|
||||
const evidenceProfile = analysisProfileForEvidence(session, tool);
|
||||
const { output, evidence } = await executeAnalysisTool({
|
||||
session,
|
||||
openedTarget: opened.value,
|
||||
tool,
|
||||
arguments: arguments_,
|
||||
signal,
|
||||
evidenceProfile,
|
||||
});
|
||||
if (evidence !== undefined) {
|
||||
const recorded = session.recordEvidence(evidence);
|
||||
if (!recorded.ok) return cliError(recorded.error);
|
||||
if (isWorkflowEvidenceTool(tool)) {
|
||||
const unknowns = recordWorkflowUnknowns({
|
||||
name: tool,
|
||||
result: evidence.normalized_result,
|
||||
evidenceId: evidence.evidence_id,
|
||||
recordUnknown: (unknown) => session.recordUnknown(unknown),
|
||||
});
|
||||
if (!unknowns.ok) return cliError(unknowns.error);
|
||||
}
|
||||
}
|
||||
if (
|
||||
isWorkflowEvidenceTool(tool) &&
|
||||
tool !== "trace_native_ui_action" &&
|
||||
snapshotPath !== undefined &&
|
||||
evidence !== undefined &&
|
||||
session.allowsSnapshotReplay(tool)
|
||||
) {
|
||||
const workflowRecord = workflowSnapshotRecord(evidence, tool);
|
||||
if (workflowRecord !== undefined) {
|
||||
const recorded = session.recordWorkflowSnapshot(workflowRecord);
|
||||
if (evidence !== undefined) {
|
||||
const recorded = session.recordEvidence(evidence);
|
||||
if (!recorded.ok) return cliError(recorded.error);
|
||||
if (isWorkflowEvidenceTool(tool)) {
|
||||
const unknowns = recordWorkflowUnknowns({
|
||||
name: tool,
|
||||
result: evidence.normalized_result,
|
||||
evidenceId: evidence.evidence_id,
|
||||
recordUnknown: (unknown) => session.recordUnknown(unknown),
|
||||
});
|
||||
if (!unknowns.ok) return cliError(unknowns.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (
|
||||
tool !== "trace_native_ui_action" &&
|
||||
snapshotPath !== undefined &&
|
||||
evidence !== undefined
|
||||
) {
|
||||
const snapshot = session.exportAnalysisSnapshot();
|
||||
if (!snapshot.ok) return cliError(snapshot.error);
|
||||
const written = await writeAnalysisSnapshot(
|
||||
snapshot.value,
|
||||
snapshotPath,
|
||||
true,
|
||||
);
|
||||
if (!written.ok) return cliError(written.error);
|
||||
}
|
||||
return output;
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
if (
|
||||
isWorkflowEvidenceTool(tool) &&
|
||||
tool !== "trace_native_ui_action" &&
|
||||
snapshotPath !== undefined &&
|
||||
evidence !== undefined &&
|
||||
session.allowsSnapshotReplay(tool)
|
||||
) {
|
||||
const workflowRecord = workflowSnapshotRecord(evidence, tool);
|
||||
if (workflowRecord !== undefined) {
|
||||
const recorded = session.recordWorkflowSnapshot(workflowRecord);
|
||||
if (!recorded.ok) return cliError(recorded.error);
|
||||
}
|
||||
}
|
||||
if (
|
||||
tool !== "trace_native_ui_action" &&
|
||||
snapshotPath !== undefined &&
|
||||
evidence !== undefined
|
||||
) {
|
||||
const snapshot = session.exportAnalysisSnapshot();
|
||||
if (!snapshot.ok) return cliError(snapshot.error);
|
||||
const written = await writeAnalysisSnapshot(
|
||||
snapshot.value,
|
||||
snapshotPath,
|
||||
true,
|
||||
);
|
||||
if (!written.ok) return cliError(written.error);
|
||||
}
|
||||
return output;
|
||||
},
|
||||
directAnalysisCleanupFailure,
|
||||
);
|
||||
};
|
||||
|
||||
const executeAnalysisTool = async (input: {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import { analysisErrorProjectionSchema } from "../contracts/errorSchemas.js";
|
||||
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import type { AnalysisExecution } from "./AnalysisProvider.js";
|
||||
import type { BinarySession } from "./binary/BinarySession.js";
|
||||
|
||||
/** Await one-shot cleanup and retain either the outcome or both failures. */
|
||||
export const withSessionCleanup = async <Value>(
|
||||
session: BinarySession,
|
||||
operation: () => Promise<Value>,
|
||||
onCleanupFailure: (value: Value, error: AnalysisError) => Value,
|
||||
): Promise<Value> => {
|
||||
let value: Value;
|
||||
try {
|
||||
value = await operation();
|
||||
} catch (cause: unknown) {
|
||||
const closed = await session.close();
|
||||
if (!closed.ok)
|
||||
throw new AggregateError(
|
||||
[cause, closed.error],
|
||||
"Analysis and provider cleanup both failed",
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
const closed = await session.close();
|
||||
return closed.ok ? value : onCleanupFailure(value, closed.error);
|
||||
};
|
||||
|
||||
/** Keep the primary CLI failure or completed Evidence alongside cleanup failure. */
|
||||
export const directAnalysisCleanupFailure = (
|
||||
output: JsonValue,
|
||||
error: AnalysisError,
|
||||
): JsonValue => {
|
||||
const cleanup = projectAnalysisError(error);
|
||||
if (typeof output === "object" && output !== null && !Array.isArray(output)) {
|
||||
const { error: label, ...projection } = output;
|
||||
const primary = analysisErrorProjectionSchema.safeParse(projection);
|
||||
if (typeof label === "string" && primary.success)
|
||||
return {
|
||||
error: label,
|
||||
...primary.data,
|
||||
code: "cleanup_incomplete",
|
||||
retryable: false,
|
||||
details: {
|
||||
...primary.data.details,
|
||||
cleanup: "incomplete",
|
||||
resources: error.cleanupResources.slice(),
|
||||
execution_failure:
|
||||
primary.data.details !== undefined &&
|
||||
"execution_failure" in primary.data.details
|
||||
? (primary.data.details.execution_failure ?? primary.data.code)
|
||||
: primary.data.code,
|
||||
primary_error: projection,
|
||||
cleanup_error: cleanup,
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
error: "Analysis failed",
|
||||
...cleanup,
|
||||
code: "cleanup_incomplete",
|
||||
retryable: false,
|
||||
details: {
|
||||
...cleanup.details,
|
||||
cleanup: "incomplete",
|
||||
partial_observation: output,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Preserve managed observations or the original typed failure in diagnostics. */
|
||||
export const managedAnalysisCleanupFailure = (
|
||||
result: Result<AnalysisExecution, AnalysisError>,
|
||||
error: AnalysisError,
|
||||
): Result<AnalysisExecution, AnalysisError> => {
|
||||
const cleanup = projectAnalysisError(error);
|
||||
return err(
|
||||
new ProviderAdapterError(
|
||||
error instanceof ProviderAdapterError ? error.providerId : "rea",
|
||||
"close_binary",
|
||||
{
|
||||
cause: result.ok ? error : result.error,
|
||||
cleanup: { reason: cleanup.message, resources: error.cleanupResources },
|
||||
diagnostics: {
|
||||
cleanup_error: cleanup,
|
||||
...(result.ok
|
||||
? { partial_observation: jsonValueSchema.parse(result.value) }
|
||||
: { primary_error: projectAnalysisError(result.error) }),
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
@@ -141,6 +141,74 @@ describe("analysis snapshot cache capacity", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("analysis snapshot cache policy", () => {
|
||||
it.each<{
|
||||
policy?: CapabilityDescriptor["cachePolicy"];
|
||||
operation?: CapabilityDescriptor["operation"];
|
||||
effects?: Partial<CapabilityDescriptor["effects"]>;
|
||||
parameters?: Record<string, string>;
|
||||
cacheable: boolean;
|
||||
}>([
|
||||
{ cacheable: false },
|
||||
{ policy: "snapshot", cacheable: true },
|
||||
{ policy: "live", cacheable: false },
|
||||
{
|
||||
policy: "live",
|
||||
effects: { mayWriteFilesystem: false },
|
||||
cacheable: false,
|
||||
},
|
||||
{
|
||||
policy: "snapshot",
|
||||
effects: { mutatesArtifact: true },
|
||||
cacheable: false,
|
||||
},
|
||||
{
|
||||
policy: "snapshot",
|
||||
effects: { changesPermissions: true },
|
||||
cacheable: false,
|
||||
},
|
||||
{ policy: "snapshot", operation: "list_documents", cacheable: false },
|
||||
{ policy: "snapshot", operation: "list_strings", cacheable: false },
|
||||
{
|
||||
policy: "snapshot",
|
||||
operation: "address_name",
|
||||
parameters: { document: "fixture" },
|
||||
cacheable: false,
|
||||
},
|
||||
])(
|
||||
"keeps explicit cache policy within immutable and state-independent boundaries: %j",
|
||||
({
|
||||
policy,
|
||||
operation = "analyze_function",
|
||||
effects,
|
||||
parameters = {},
|
||||
cacheable,
|
||||
}) => {
|
||||
const descriptor: CapabilityDescriptor = {
|
||||
provider: ANALYSIS_SNAPSHOT_PROVIDER,
|
||||
operation,
|
||||
available: true,
|
||||
reason: null,
|
||||
...(policy === undefined ? {} : { cachePolicy: policy }),
|
||||
effects: {
|
||||
mutatesArtifact: false,
|
||||
launchesProcess: true,
|
||||
mayShowUi: false,
|
||||
mayAccessNetwork: false,
|
||||
mayWriteFilesystem: true,
|
||||
changesPermissions: false,
|
||||
requiresRoot: false,
|
||||
...effects,
|
||||
},
|
||||
limitations: [],
|
||||
};
|
||||
expect(isSnapshotCacheable(operation, descriptor, parameters)).toBe(
|
||||
cacheable,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("analysis snapshot cache partitioning", () => {
|
||||
it.each<{
|
||||
operation: Exclude<AnalysisOperation, "health">;
|
||||
|
||||
@@ -71,7 +71,8 @@ export const isSnapshotCacheable = (
|
||||
typeof parameters.address === "string") &&
|
||||
descriptor?.effects.mutatesArtifact === false &&
|
||||
descriptor.cachePolicy !== "live" &&
|
||||
descriptor.effects.mayWriteFilesystem === false &&
|
||||
(descriptor.cachePolicy === "snapshot" ||
|
||||
descriptor.effects.mayWriteFilesystem === false) &&
|
||||
descriptor.effects.changesPermissions === false;
|
||||
|
||||
/** Bounded in-memory cache for one immutable binary identity. */
|
||||
|
||||
Reference in New Issue
Block a user