Files
reverse-engineering-anything/src/application/analysisView/NativeLoadImageViews.test.ts
T
morluto b138f52e12 refactor: prune internal seams and preserve configuration ownership (#10430)
* refactor: prune internal seams and preserve configuration ownership

* fix(setup): keep YAML aliases and inherited registration settings on refresh

- Reuse an unchanged YAML collection only while every alias in it still
  resolves after the edit; otherwise rebuild it so the alias becomes its
  value. A sibling anchor replaced or moved in the same refresh no longer
  leaves an unresolved alias in Hermes or Goose output.
- Keep a connection selector whose existing value already chooses stdio
  (for example an inherited Hermes transport: stdio) instead of superseding
  it; remote selectors are still replaced. Explicit stdio selectors are
  therefore no longer rewritten (Kimi expectation updated).
- When the whole Hermes registration is inherited through a merge,
  override it with that merge and write only the changed fields.

* fix(setup): edit registration fields whose YAML key is an alias

A key written as an alias to a string scalar (for example *field with
field: &field env) was treated as a source-only key, so any change to
its value was refused in Hermes and Goose. Resolve such keys to their
string, keep the aliased spelling while its anchor survives the edit,
and write the plain key when the same edit rebuilds the anchor.

* test(process): bound cleanup-boundary waits and always release gates

Both cancellation tests awaited a bare started promise and released
their gates only on success, so a missed boundary waited for the outer
deadline and could leave cleanup parked. Use awaitAcknowledgement and
release and join the cleanup in finally.

* fix(setup): repair only aliases an edit leaves unresolved

Replace the pre-edit alias survival guess, which expanded aliases to
sibling anchors that survive unchanged, with an exact repair: record
what each alias in the replaced value resolves to, write the edit, then
replace only aliases left without a preceding anchor by that value.

- Keep an aliased key's source position, so its anchor stays ahead of
  later aliases.
- Treat an aliased key as its string only when no other key in the map
  resolves to the same string; an ambiguous one stays guarded.
- Override an inherited Hermes registration only when the requested
  entry keeps every inherited field, so an explicit replacement can drop
  remote selectors.

* fix(setup): repair aliases whose binding an edit changes, keeping comments

An alias whose own anchor was removed could resolve to an earlier anchor
with the same name and silently change value. Record the node each alias
resolved to and repair any alias that no longer resolves to that node.
Carry the alias's comments and spacing onto the repaired value.

* test(setup): split the Hermes anchor and merge refresh cases

* fix(setup): refuse removing an overridden key its merge would restore

The merge-retention check skipped keys the mapping also overrides, so
dropping such a key (for example a remote url during an explicit stdio
replacement) let the retained merge restore it. Check removal before
skipping overridden keys, so the edit is refused with the existing
message instead of producing a registration that cannot be applied.
2026-11-14 04:08:37 -05:00

362 lines
11 KiB
TypeScript

import { expect, it } from "vitest";
import { createEvidence } from "../../domain/evidence.js";
import { inspectAnalysisViewInputSchema } from "../../domain/analysisView/analysisViewRequest.js";
import { analysisViewResultSchema } from "../../domain/analysisView/analysisViewResult.js";
import {
RELOCATION_SYMBOL_UNRECORDED,
projectNativeLoadImageView,
} from "../../domain/analysisView/nativeLoadImageView.js";
import type { NativeLoadImage } from "../../domain/native/nativeLoadImage.js";
import {
nativeDosLoadImageFixture,
nativeLoadImageObservationFixture,
} from "../../../tests/fixtures/nativeLoadImage.js";
import { inspectAnalysisView } from "./AnalysisViewService.js";
const source = (): Extract<NativeLoadImage, { status: "unsupported" }> => ({
status: "unsupported",
reason:
"ELF load observations have no independent verification in this fixture.",
observations: {
...nativeLoadImageObservationFixture(),
executable_format: "Executable and Linking Format (ELF)",
relocations: [
{
address: "ram:0x0010",
segment: null,
offset: null,
status: "APPLIED",
type: 8,
values: [16],
original_bytes_hex: "0000",
memory_bytes_hex: "1000",
},
{
address: "ram:0x0010",
segment: null,
offset: null,
status: "UNSUPPORTED",
type: 9,
values: [],
original_bytes_hex: null,
memory_bytes_hex: null,
},
],
},
limitations: [
"Load observations do not establish runtime relocation behavior.",
],
});
const evidence = (report: NativeLoadImage) =>
createEvidence(
{ path: "/selected/fixture", sha256: "a".repeat(64), format: "elf" },
{ id: "ghidra", name: "Ghidra", version: "12.1.4" },
{
operation: "inspect_native_load_image",
parameters: {},
result: report,
limitations: ["Provider observation remains historical."],
},
{ immutable: true },
);
const view = async (report: NativeLoadImage, request: unknown) => {
const result = await inspectAnalysisView({
source: { kind: "inline", evidence: evidence(report) },
view: request,
});
if (!result.ok) throw result.error;
return analysisViewResultSchema.parse(result.value.normalized_result);
};
it("distinguishes recorded, absent, and historical unrecorded relocation symbols", async () => {
const report = source();
report.observations.relocations[0]!.symbol = "external_symbol";
report.observations.relocations[1]!.symbol = null;
const request = { kind: "native-load-image", collection: "relocations" };
const current = await view(report, request);
if (current.kind !== "native-load-image") throw new Error("Expected page");
if (current.items.collection !== "relocations")
throw new Error("Expected relocations");
expect(current.items.rows).toMatchObject([
{ record: { symbol: "external_symbol" } },
{ record: { symbol: null } },
]);
expect(current.unknowns).not.toContain(RELOCATION_SYMBOL_UNRECORDED);
report.observations.relocations[1]!.symbol = "";
const emptyName = await view(report, request);
if (emptyName.kind !== "native-load-image") throw new Error("Expected page");
if (emptyName.items.collection !== "relocations")
throw new Error("Expected relocations");
expect(emptyName.items.rows?.[1]?.record).toMatchObject({ symbol: "" });
expect(emptyName.unknowns).not.toContain(RELOCATION_SYMBOL_UNRECORDED);
delete report.observations.relocations[0]!.symbol;
const historical = await view(report, request);
if (historical.kind !== "native-load-image") throw new Error("Expected page");
expect(historical.items.rows?.[0]?.record).not.toHaveProperty("symbol");
expect(historical.unknowns).toContain(RELOCATION_SYMBOL_UNRECORDED);
const omitted = await view(report, { ...request, fields: ["address"] });
expect(omitted.unknowns).not.toContain(RELOCATION_SYMBOL_UNRECORDED);
});
it("summarizes measured ELF facts without upgrading unsupported verification", async () => {
const report = source();
const parent = evidence(report);
const summary = await view(report, { kind: "summary" });
expect(summary).toMatchObject({
parent_evidence_id: parent.evidence_id,
parent_operation: "inspect_native_load_image",
source_provider: parent.provider,
source_analysis_profile_digest: null,
source_parameters: {},
artifact: { path: "/selected/fixture", sha256: "a".repeat(64) },
summary: {
verification: { status: "unsupported", reason: report.reason },
observation: {
executable_format: "Executable and Linking Format (ELF)",
},
counts: {
relocations: 2,
checks: null,
sections: null,
metadata_recovery: null,
},
},
limitations: expect.arrayContaining([
...report.limitations,
"Provider observation remains historical.",
]),
unknowns: expect.arrayContaining([report.reason]),
});
expect(JSON.stringify(summary)).not.toContain("memory_bytes_hex");
});
it("preserves same-address alternatives, padding, nulls and byte facts over pages", async () => {
const report = source();
const records = [];
for (let offset = 0; offset < 2; offset++) {
const page = await view(report, {
kind: "native-load-image",
collection: "relocations",
fields: [
"address",
"segment",
"offset",
"status",
"type",
"values",
"original_bytes_hex",
"memory_bytes_hex",
],
offset,
limit: 1,
});
if (page.kind !== "native-load-image") throw new Error("Expected page");
expect(page.verification_status).toBe("unsupported");
expect(
analysisViewResultSchema.safeParse({
...page,
items: {
...page.items,
rows: null,
},
}).success,
).toBe(false);
expect(page.items.rows?.[0]?.source_index).toBe(offset);
expect(page.coverage).toMatchObject({
total: 2,
next_offset: offset === 0 ? 1 : null,
exhausted: offset === 1,
});
records.push(...(page.items.rows ?? []).map((row) => row.record));
}
expect(records).toEqual(report.observations.relocations);
});
it("filters indices before paging and projects only selected fields", async () => {
const page = await view(source(), {
kind: "native-load-image",
collection: "relocations",
indices: [1, 400, 0, 1],
offset: 1,
fields: ["address", "status", "status"],
limit: 1,
});
expect(page).toMatchObject({
items: {
collection: "relocations",
rows: [
{
source_index: 1,
record: { address: "ram:0x0010", status: "UNSUPPORTED" },
},
],
},
coverage: { examined: 1, total: 2, next_offset: null, exhausted: true },
});
if (page.kind !== "native-load-image") throw new Error("Expected page");
expect(page.items.rows?.[0]?.record).toEqual({
address: "ram:0x0010",
status: "UNSUPPORTED",
});
});
it.each(["checks", "sections"])(
"keeps unrecorded or inapplicable %s null rather than inventing empty observations",
async (collection) => {
expect(
await view(source(), { kind: "native-load-image", collection }),
).toMatchObject({
items: { collection, rows: null },
coverage: {
status: "unavailable",
examined: 0,
total: null,
next_offset: null,
exhausted: false,
},
unknowns: expect.arrayContaining([
"Null collection counts or rows mean not recorded or not applicable, not observed absence.",
]),
});
},
);
it("distinguishes an explicitly recorded empty metadata collection and exhausted selection", async () => {
const report = source();
report.observations.metadata_recovery = [];
expect(await view(report, { kind: "summary" })).toMatchObject({
summary: { counts: { metadata_recovery: 0 } },
});
for (const request of [
{ collection: "entry_context" },
{ collection: "relocations", indices: [] },
{ collection: "relocations", offset: 400 },
])
expect(
await view(report, { kind: "native-load-image", ...request }),
).toMatchObject({
items: { rows: [] },
coverage: { status: "empty", exhausted: true, next_offset: null },
});
});
it("preserves recorded DOS verification details and ordered mismatch checks", async () => {
const report = nativeDosLoadImageFixture();
const summary = await view(report, { kind: "summary" });
expect(summary).toMatchObject({
summary: {
verification: {
status: "verified",
format: "dos-mz",
target_sha256: report.target_sha256,
},
counts: { checks: report.checks.length },
},
});
const mismatch = {
...report,
status: "mismatch" as const,
checks: [
{
...report.checks[0]!,
matched: false,
expected: { original: 1 },
observed: null,
},
],
};
expect(
await view(mismatch, {
kind: "native-load-image",
collection: "checks",
fields: [
"name",
"matched",
"expected",
"observed",
"file_offset",
"address",
],
}),
).toMatchObject({
items: { rows: [{ source_index: 0, record: mismatch.checks[0] }] },
});
expect(await view(mismatch, { kind: "summary" })).toMatchObject({
summary: { verification: { status: "mismatch" } },
});
});
it("keeps byte and potentially large comparison payloads opt-in while retaining full source facts", async () => {
const relocations = await view(source(), {
kind: "native-load-image",
collection: "relocations",
});
if (relocations.kind !== "native-load-image")
throw new Error("Expected page");
expect(relocations.items.rows?.[0]?.record).toEqual({
address: "ram:0x0010",
segment: null,
offset: null,
status: "APPLIED",
type: 8,
values: [16],
});
const report = nativeDosLoadImageFixture();
const body = { complete_comparison: "retained proof body".repeat(128) };
report.checks[0]!.expected = body;
const checks = await view(report, {
kind: "native-load-image",
collection: "checks",
});
if (checks.kind !== "native-load-image") throw new Error("Expected page");
expect(checks.items.rows?.[0]?.record).toEqual({
name: "original_file_sha256",
matched: true,
file_offset: null,
address: null,
});
expect(
await view(report, {
kind: "native-load-image",
collection: "checks",
fields: ["expected"],
}),
).toMatchObject({
items: { rows: [{ source_index: 0, record: { expected: body } }] },
});
expect(report.checks[0]!.expected).toEqual(body);
});
it("rejects collection-inapplicable fields and malformed sibling facts even for an empty selection", async () => {
expect(
inspectAnalysisViewInputSchema.safeParse({
source: { kind: "inline", evidence: evidence(source()) },
view: {
kind: "native-load-image",
collection: "entry_points",
fields: ["original_bytes_hex"],
},
}).success,
).toBe(false);
const report = source();
const parent = {
predicateType: "rea.analysis",
evidenceId: "ev_" + "b".repeat(64),
operation: "inspect_native_load_image",
normalizedResult: report,
limitations: [],
};
const request = {
kind: "native-load-image" as const,
collection: "relocations" as const,
indices: [],
offset: 0,
limit: 1,
};
expect(projectNativeLoadImageView(parent, request).ok).toBe(true);
Reflect.set(report.observations.relocations[1]!, "status", "");
expect(projectNativeLoadImageView(parent, request).ok).toBe(false);
});