feat(evidence): discover retained records without full payloads (#1682)
* feat(evidence): discover retained records without full payloads * fix(evidence): guide oversized results to retained discovery * style(tests): format retained discovery recovery assertion
This commit is contained in:
@@ -47,7 +47,7 @@ export const analysisErrorRemediationAction = (
|
||||
return (
|
||||
error.remediationAction ??
|
||||
(error.resource === "transport"
|
||||
? "Export retained session evidence through export_evidence_bundle to a caller-selected path, or use complete CLI JSON output. Tools that accept retained-evidence references can inspect compatible records. The connection remains usable."
|
||||
? "Discover retained records with get_evidence_bundle and detail: summary, then inspect compatible records through inspect_analysis_view using an exact Evidence reference. Export complete retained session evidence through export_evidence_bundle to a caller-selected path, or use complete CLI JSON output. The connection remains usable."
|
||||
: error.resource === "cpu"
|
||||
? "Review the reported worker CPU limits and observed signal. Retry with sufficient CPU time or a smaller artifact; REA retains tighter inherited limits."
|
||||
: error.resource === "file-size"
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { createEvidence } from "./evidence.js";
|
||||
import { createEvidenceBundle } from "./evidenceBundle.js";
|
||||
import { summarizeEvidenceBundle } from "./evidenceRecordSummary.js";
|
||||
|
||||
it("preserves native identity independently of missing or incompatible value-flow metadata", () => {
|
||||
const records = [
|
||||
undefined,
|
||||
{ available: true, truncated: true },
|
||||
{ unexpected: "legacy" },
|
||||
{ available: false, reason: "Decompiler unavailable" },
|
||||
].map((flow) =>
|
||||
createEvidence(
|
||||
{ path: "/fixtures/native.exe", format: "pe", sha256: "a".repeat(64) },
|
||||
{ id: "provider", name: "Provider", version: "1" },
|
||||
{
|
||||
operation: "analyze_function",
|
||||
parameters: {},
|
||||
result: {
|
||||
procedure: { address: "space:0x10", name: "entry" },
|
||||
...(flow === undefined ? {} : { native_value_flow: flow }),
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
const summary = summarizeEvidenceBundle(createEvidenceBundle(records), {
|
||||
procedure_address: "space:0x10",
|
||||
});
|
||||
expect(summary.matching_records).toBe(4);
|
||||
const native = summary.records.map((r) => r.native_dossier);
|
||||
expect(
|
||||
native.every((r) => r.available && r.procedure_address === "space:0x10"),
|
||||
).toBe(true);
|
||||
expect(native).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ value_flow: { status: "not-recorded" } }),
|
||||
expect.objectContaining({
|
||||
value_flow: {
|
||||
status: "observed",
|
||||
truncated: true,
|
||||
omitted_operations_lower_bound: null,
|
||||
known_omitted_inputs: null,
|
||||
known_omitted_edges: null,
|
||||
},
|
||||
}),
|
||||
expect.objectContaining({
|
||||
value_flow: { status: "unknown", reason: expect.any(String) },
|
||||
}),
|
||||
expect.objectContaining({
|
||||
value_flow: { status: "unavailable", reason: "Decompiler unavailable" },
|
||||
}),
|
||||
]),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,166 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { digestSchema, prefixedDigestSchema } from "./digests.js";
|
||||
import { evidenceSchema, type Evidence } from "./evidence.js";
|
||||
import type { EvidenceBundle } from "./evidenceBundle.js";
|
||||
|
||||
/** Exact retained-record selectors; selection never resolves a provider address. */
|
||||
export const evidenceRecordFiltersSchema = z.strictObject({
|
||||
evidence_id: prefixedDigestSchema("ev").optional(),
|
||||
operation: z.string().min(1).optional(),
|
||||
target_sha256: digestSchema.optional(),
|
||||
analysis_profile_digest: digestSchema.optional(),
|
||||
procedure_address: z.string().min(1).optional(),
|
||||
});
|
||||
export type EvidenceRecordFilters = z.infer<typeof evidenceRecordFiltersSchema>;
|
||||
|
||||
const countSchema = z.number().int().nonnegative();
|
||||
const nativeMetadataSchema = z.object({
|
||||
procedure: z.object({ address: z.string(), name: z.string().nullish() }),
|
||||
native_value_flow: z.unknown().optional(),
|
||||
});
|
||||
const valueFlowMetadataSchema = z.union([
|
||||
z.object({
|
||||
available: z.literal(true),
|
||||
truncated: z.boolean(),
|
||||
omitted_operations_lower_bound: countSchema.optional(),
|
||||
known_omitted_inputs: countSchema.optional(),
|
||||
known_omitted_edges: countSchema.optional(),
|
||||
}),
|
||||
z.object({ available: z.literal(false), reason: z.string() }),
|
||||
]);
|
||||
|
||||
const nativeSummarySchema = z.discriminatedUnion("available", [
|
||||
z.strictObject({
|
||||
available: z.literal(true),
|
||||
procedure_address: z.string(),
|
||||
procedure_name: z.string().nullable(),
|
||||
value_flow: z.discriminatedUnion("status", [
|
||||
z.strictObject({
|
||||
status: z.literal("observed"),
|
||||
truncated: z.boolean(),
|
||||
omitted_operations_lower_bound: countSchema.nullable(),
|
||||
known_omitted_inputs: countSchema.nullable(),
|
||||
known_omitted_edges: countSchema.nullable(),
|
||||
}),
|
||||
z.strictObject({ status: z.literal("unavailable"), reason: z.string() }),
|
||||
z.strictObject({ status: z.literal("not-recorded") }),
|
||||
z.strictObject({ status: z.literal("unknown"), reason: z.string() }),
|
||||
]),
|
||||
}),
|
||||
z.strictObject({
|
||||
available: z.literal(false),
|
||||
reason: z.enum(["different-operation", "not-recorded-or-incompatible"]),
|
||||
}),
|
||||
]);
|
||||
|
||||
/** Metadata for complete retained records, independent of analysis completeness. */
|
||||
export const evidenceBundleSummarySchema = z.strictObject({
|
||||
kind: z.literal("evidence-bundle-summary"),
|
||||
filters: z.record(z.string(), z.string()),
|
||||
total_retained_records: countSchema,
|
||||
matching_records: countSchema,
|
||||
unknown_revisions: countSchema,
|
||||
records: z.array(
|
||||
z.strictObject({
|
||||
evidence_id: evidenceSchema.shape.evidence_id,
|
||||
subject: evidenceSchema.shape.subject,
|
||||
provider: evidenceSchema.shape.provider,
|
||||
operation: evidenceSchema.shape.operation,
|
||||
confidence: evidenceSchema.shape.confidence,
|
||||
authority: evidenceSchema.shape.authority,
|
||||
analysis_profile_digest: digestSchema.nullable(),
|
||||
retention: z.literal("complete-record"),
|
||||
limitation_count: countSchema,
|
||||
native_dossier: nativeSummarySchema,
|
||||
}),
|
||||
),
|
||||
});
|
||||
export type EvidenceBundleSummary = z.infer<typeof evidenceBundleSummarySchema>;
|
||||
|
||||
const nativeSummary = (
|
||||
evidence: Evidence,
|
||||
): z.infer<typeof nativeSummarySchema> => {
|
||||
if (evidence.operation !== "analyze_function")
|
||||
return { available: false, reason: "different-operation" };
|
||||
// Read only producer metadata. Parsing the full dossier would allocate every
|
||||
// large collection merely to discover its already authenticated parent ID.
|
||||
const parsed = nativeMetadataSchema.safeParse(evidence.normalized_result);
|
||||
if (!parsed.success)
|
||||
return { available: false, reason: "not-recorded-or-incompatible" };
|
||||
const flow = valueFlowMetadataSchema.safeParse(parsed.data.native_value_flow);
|
||||
return {
|
||||
available: true,
|
||||
procedure_address: parsed.data.procedure.address,
|
||||
procedure_name: parsed.data.procedure.name ?? null,
|
||||
value_flow:
|
||||
parsed.data.native_value_flow == null
|
||||
? { status: "not-recorded" }
|
||||
: !flow.success
|
||||
? {
|
||||
status: "unknown",
|
||||
reason: "Value-flow metadata has an incompatible representation.",
|
||||
}
|
||||
: flow.data.available
|
||||
? {
|
||||
status: "observed",
|
||||
truncated: flow.data.truncated,
|
||||
omitted_operations_lower_bound:
|
||||
flow.data.omitted_operations_lower_bound ?? null,
|
||||
known_omitted_inputs: flow.data.known_omitted_inputs ?? null,
|
||||
known_omitted_edges: flow.data.known_omitted_edges ?? null,
|
||||
}
|
||||
: { status: "unavailable", reason: flow.data.reason },
|
||||
};
|
||||
};
|
||||
|
||||
/** Discover retained Evidence without reading or copying its complete payloads. */
|
||||
export const summarizeEvidenceBundle = (
|
||||
bundle: EvidenceBundle,
|
||||
filters: EvidenceRecordFilters = {},
|
||||
): EvidenceBundleSummary => {
|
||||
const selectedFilters: Record<string, string> = {};
|
||||
for (const [field, value] of Object.entries(filters))
|
||||
if (value !== undefined) selectedFilters[field] = value;
|
||||
const records: EvidenceBundleSummary["records"] = [];
|
||||
for (const evidence of bundle.records) {
|
||||
if (
|
||||
(filters.evidence_id !== undefined &&
|
||||
filters.evidence_id !== evidence.evidence_id) ||
|
||||
(filters.operation !== undefined &&
|
||||
filters.operation !== evidence.operation) ||
|
||||
(filters.target_sha256 !== undefined &&
|
||||
filters.target_sha256 !== evidence.subject?.digest.sha256) ||
|
||||
(filters.analysis_profile_digest !== undefined &&
|
||||
filters.analysis_profile_digest !== evidence.analysis_profile?.digest)
|
||||
)
|
||||
continue;
|
||||
const native = nativeSummary(evidence);
|
||||
if (
|
||||
filters.procedure_address !== undefined &&
|
||||
(!native.available ||
|
||||
filters.procedure_address !== native.procedure_address)
|
||||
)
|
||||
continue;
|
||||
records.push({
|
||||
evidence_id: evidence.evidence_id,
|
||||
subject: evidence.subject,
|
||||
provider: evidence.provider,
|
||||
operation: evidence.operation,
|
||||
confidence: evidence.confidence,
|
||||
authority: evidence.authority,
|
||||
analysis_profile_digest: evidence.analysis_profile?.digest ?? null,
|
||||
retention: "complete-record",
|
||||
limitation_count: evidence.limitations.length,
|
||||
native_dossier: native,
|
||||
});
|
||||
}
|
||||
return {
|
||||
kind: "evidence-bundle-summary",
|
||||
filters: selectedFilters,
|
||||
total_retained_records: bundle.records.length,
|
||||
matching_records: records.length,
|
||||
unknown_revisions: bundle.unknowns.length,
|
||||
records,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user