Files
reverse-engineering-anything/src/domain/analysisView/nativeInventoryView.ts
T
Kaoru0822andClaude Opus 5.5 d2518a19f8 feat(native): label binary overview counts with their inventory scope (#8338)
* feat(native): label binary overview counts with their inventory scope

`binary_overview` described its counts as "exhaustive", and its result was
a pair of bare scalars. An agent could read them as a discovery of every
function and string in the file, or as comparable across providers. They
are exact totals of the rows each provider's inventories return.

The shared `projectBinaryOverview` now adds one provider-neutral
`inventory_coverage` record, so Ghidra's direct summary and the
listing fallback carry the same record:
`{ scope: "provider_inventory", discovery_coverage: "unknown" }`.
The record reuses the `discovery_coverage` vocabulary that
`list_procedures({detail:"summary"})` already reports, now owned by one
domain schema. The tool description states the scope and that counts are
not comparable across providers. Counts, segment omission semantics and
limitations are unchanged.

Fixes #8287

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(native): label overviews in the workflow so older snapshots stay valid

Analysis snapshots replay the projected inventory summary. A snapshot saved
before inventory_coverage existed would then fail the overview output schema.
The record describes what overview counts mean, whoever produced them, so
the binary_overview workflow now adds it on both its provider-summary and
listing paths. Provider values and projectBinaryOverview are unchanged, and
the composed overview's linked atomic record stays as the provider
reported it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(native): refresh composed overviews from before inventory_coverage

Bump the binary_overview workflow revision to 5, so snapshot and CLI replay
recompute a revision-4 composed overview from its atomic record. Replay
would otherwise return the cached overview without its count labels. The
revision-4 case joins the existing refresh scenario. The atomic Evidence
is not rewritten.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 11:12:52 -07:00

169 lines
6.0 KiB
TypeScript

import { z } from "zod";
import { analysisStringSchema } from "../native/nativeString.js";
import { inventoryDiscoveryCoverageSchema } from "../native/inventoryCoverage.js";
import { jsonValueSchema } from "../jsonValue.js";
import {
addressedValueSchema,
isNativeInventoryOperation,
nativeInventoryRowsSchemas,
} from "../native/nativeInventory.js";
import { err, ok } from "../result.js";
import type { AnalysisViewParent } from "./analysisViewParent.js";
import {
assertOwnedView,
type AdmittedView,
type SummaryAnalysisView,
} from "./analysisViewShape.js";
import { analysisViewInputError } from "./analysisViewErrors.js";
import { createAnalysisViewResultReader } from "./analysisViewParent.js";
import {
boundedViewMessages,
completeWithinViewCoverage,
pageViewCoverage,
} from "./analysisViewCoverage.js";
/** Preserve all canonical native row metadata, including string decoding facts. */
const inventoryRecordSchema = addressedValueSchema.extend({
provider_value: analysisStringSchema.shape.provider_value,
decoding: analysisStringSchema.shape.decoding,
string: z.union([
analysisStringSchema.shape.string,
addressedValueSchema.shape.string,
]),
});
type InventoryRecord = z.infer<typeof inventoryRecordSchema>;
const fieldSchema = inventoryRecordSchema.keyof();
export const nativeInventoryViewRequestSchema = z.strictObject({
kind: z.literal("native-inventory"),
offset: z.number().int().nonnegative().default(0),
limit: z.number().int().positive().default(64),
fields: z
.array(fieldSchema)
.exactOptional()
.describe(
"Select record fields; address and source_index preserve identity. Selecting value also retains its string and decoding qualifications. Omit for every recorded field. Search tools filter before retention.",
),
});
/** Parent Evidence ID plus source_index identifies even same-address alternatives. */
export const nativeInventoryViewRowSchema = z.strictObject({
source_index: z.number().int().nonnegative(),
record: inventoryRecordSchema.partial().required({ address: true }),
});
export const nativeInventorySummarySchema = z.strictObject({
collection: z.enum(["procedures", "strings", "names"]),
row_count: z.number().int().nonnegative(),
discovery_coverage: inventoryDiscoveryCoverageSchema,
selectable_fields: z.array(fieldSchema),
});
const readNames = createAnalysisViewResultReader(
nativeInventoryRowsSchemas.names,
);
const readStrings = createAnalysisViewResultReader(
nativeInventoryRowsSchemas.strings,
);
/** Project already completed provider observations without another provider read or sort. */
export const projectNativeInventoryView = (
parent: AnalysisViewParent,
view: AdmittedView,
) => {
if (
!isNativeInventoryOperation(parent.operation) ||
(view.kind !== "summary" && view.kind !== "native-inventory")
)
return err(
analysisViewInputError([
{
path: ["view"],
reason: "invalid_value",
message:
"Native inventory Evidence supports summary and native-inventory views.",
},
]),
);
assertOwnedView<
SummaryAnalysisView | z.output<typeof nativeInventoryViewRequestSchema>
>(view, "summary", "native-inventory");
const strings =
parent.operation === "list_strings" ||
parent.operation === "search_strings";
const parsed = strings ? readStrings(parent) : readNames(parent);
if (!parsed.ok) return parsed;
const rows: readonly InventoryRecord[] = parsed.value;
const shared = {
parent_evidence_id: parent.evidenceId,
parent_operation: parent.operation,
parent_digest: parent.evidenceId.slice(3),
artifact: parent.artifact ?? null,
source_provider: parent.provider ?? null,
source_analysis_profile_digest: parent.analysisProfile?.digest ?? null,
source_parameters: jsonValueSchema.parse(parent.parameters ?? null),
limitations: boundedViewMessages(
parent.limitations,
parent.evidenceId,
"limitations",
),
unknowns: [
"Counts and paging describe the retained provider-reported rows, not complete discovery of every program object. Missing matches do not establish absence outside that observation.",
...(parent.artifact == null
? ["No artifact identity was bound to the source Evidence."]
: []),
],
};
if (view.kind === "summary")
return ok({
...shared,
kind: "summary" as const,
view,
summary: {
collection: strings
? ("strings" as const)
: parent.operation === "list_names"
? ("names" as const)
: ("procedures" as const),
row_count: rows.length,
discovery_coverage: "unknown" as const,
selectable_fields: fieldSchema.options,
},
coverage: completeWithinViewCoverage(rows.length, rows.length),
});
// Normalize the selection once: repeated caller fields must not multiply
// work or temporary entries for every retained row. Keep the original view.
const fields =
view.fields === undefined
? undefined
: [...new Set(view.fields)].filter((field) => field !== "address");
const selectingValue = fields?.includes("value") === true;
const selected = rows.slice(view.offset, view.offset + view.limit);
return ok({
...shared,
kind: "native-inventory" as const,
view,
rows: selected.map((record, index) => ({
source_index: view.offset + index,
record:
fields === undefined
? record
: {
address: record.address,
...(selectingValue && record.string !== undefined
? { string: record.string }
: {}),
...(selectingValue && record.decoding !== undefined
? { decoding: record.decoding }
: {}),
...Object.fromEntries(
fields
.filter((field) => Object.hasOwn(record, field))
.map((field) => [field, record[field]]),
),
},
})),
coverage: pageViewCoverage(view.offset, selected.length, rows.length),
});
};