Files
reverse-engineering-anything/tests/boundary/filesystem/artifactComparison.test.ts
T
morluto 1f24b3fa57 feat(evidence)!: name the Evidence subject by its target identity (format revision 2) (#11416)
* feat(evidence)!: name the Evidence subject by its target identity (format revision 2)

The Evidence subject carries subject.identity (kind, sha256, format,
architecture, selection) and the Evidence ID commits to it, so raw/DOL
entry points and ESP flash offsets of the same bytes are different
subjects. Every record declares evidence_format: 2; revision 1 bundles,
standalone records and snapshots are refused with the recreate guidance.
binaryTargetIdentity is the one projection of an open target, used by
Evidence and snapshot comparison.

BREAKING CHANGE: Evidence format revision 2. Evidence IDs commit to the
subject's target identity (subject.identity, including the raw/DOL entry
point or ESP flash offsets), and every record declares evidence_format: 2.
Evidence bundles, records and analysis snapshots from earlier releases are
refused; recreate the observations with this release.

* refactor(analysis-view): read parent subjects through the Evidence identity accessors (#11423)

Analysis views compare their parent's file identity (evidenceFileTarget)
and take path and digest from evidenceSubjectTarget instead of reading
the subject's digest, format and architecture members, which duplicate
subject.identity until they are removed. A counted guard
(evidence-subject-declaration-read) keeps new direct reads out and
records the 20 remaining files.

* fix(evidence): carry format refusals through Evidence readers and bind target selections

- safeParseEvidence/safeParseImmutableEvidence return the format refusal as
  an expected failure; every consumer maps it to the one integrity refusal
  with the recreate guidance. An unmarked object is an earlier record only
  when it has every member records have always carried.
- ArtifactProvider inventory roots and provider subjects that name the
  target's own bytes carry the target's selection (ESP flash offsets).
- Snapshot rows bind only Evidence whose identity, selection included,
  matches the snapshot target.
- Entry-point selections use the raw-binary entry point form.

* fix(evidence): refuse unauthenticated or earlier-format records instead of reading them as absent

- recognizeEvidence (evidenceValidation) replaces tryParseEvidence: input
  without an evidence_id is not Evidence (ok(undefined)); a record that names
  one but fails authentication or is from another format revision is an
  EvidenceIntegrityError carrying the recreate guidance, never undefined.
- Direct analysis retains that refusal as a secondary failure instead of
  silently skipping source capture; cleanup projection, source-closure
  annotation and the flutter, debugger and release-exposure recoveries use
  the same owner (the last three previously read the unauthenticated schema).
- A desktop capture.json from an earlier format is refused with the recreate
  guidance instead of "not desktop application capture Evidence".

* fix(evidence): advertise revision 2 ledger examples and rank delivery omissions by serialized size

- The obligation ledger and reconstruction coverage examples named
  evidence_format 1, which revision 2 refuses; they now use
  EVIDENCE_FORMAT_REVISION.
- MCP delivery chose which unretained recovery to omit by a parsed-storage
  lower bound clipped at the response budget. Once revision 2 subjects made
  the source record's estimate cross the budget, both candidates tied at the
  clip and the smaller source was omitted ahead of the larger completed
  observation. Candidates are now ranked by serialized size, which is what
  the budget counts.

* refactor(native): compare function snapshots by their Evidence target identity (#11450)

Function snapshots carry the subject's file target identity instead of the
duplicated subject declaration. Call paths and function comparisons compare
targets through sameTargetIdentity, so the same bytes opened at a different
raw entry point or ESP flash offset no longer count as one coordinate space:
build_call_path refuses the mix, and compare_functions no longer matches
entries by address across selections. Structural candidates and native
function matching read the architecture and digest through
evidenceFileTarget.

Refs #11417

* refactor(artifacts): compare artifact and WASM summaries by their Evidence target identity (#11461)

Artifact comparison authenticates a summary against its retained inventory
through sameTargetIdentity and rebinds the alias with the whole target,
selection included, so a flash-selected inventory summary taken under an
alias path is no longer refused. WASM comparison, the analysis view
artifact projection and the inventory root check read the digest, format
and display path through the Evidence identity accessors.

Refs #11417

* refactor(evidence): read record subjects through the identity accessors in JS, managed, ledger and bundle readers (#11470)

JavaScript application graphs, runtime reconciliation, managed inspection
and native verification sources, the record summary filter, the ledger's
residual-unknown target and bundle scope selection read the subject digest,
format and display path through evidenceFileTarget and
evidenceSubjectDisplay. No comparison changes: each reader matches one
record's facts against its own payload or a caller filter.

Refs #11417

* refactor(evidence)!: name the Evidence subject only by its target identity (#11497)

* refactor(evidence)!: name the Evidence subject only by its target identity

The subject keeps `identity` and the display facts `name` and `local_path`.
The duplicated `digest`, `format` and `architecture` members are removed; the
format-owned declaration now validates the identity itself. Readers the type
checker found move to the owner accessors; untyped reads in verify scripts,
the efficiency harness and test matchers move to `subject.identity`. The
`evidence-subject-declaration-read` text guard and its baseline are deleted.

Fixes #11473
Refs #11417

BREAKING CHANGE: the Evidence subject no longer has `digest`, `format` or
`architecture`; read `subject.identity.sha256`, `subject.identity.format` and
`subject.identity.architecture`. A record that still carries them is refused.

* test(evidence): give the Flutter verifier fixture the identity subject and check the DOL declaration without the ID

* test(evidence): drop the removed subject members from the declaration matrix and tamper fixtures

* feat(evidence)!: give every Evidence record an explicit subject identity (#11536)

* feat(evidence)!: give every Evidence record an explicit subject identity

A producer that cannot name a file passes an `unknown` target identity
with a closed reason (derived-from-linked-evidence, not-observed,
unsupported-subject) instead of no subject. The subject is non-null;
unknown subjects have no display facts. The generic missing-subject
limitation and `subjectUnavailableReason` are removed; producer-specific
explanations are ordinary limitations. Comparison identity requires the
derived unknown, and null-subject consumers ask the owner accessors.

Refs #11417

BREAKING CHANGE: every Evidence record has a subject. A record without a
file target names `subject.identity` `{ "kind": "unknown", "reason": … }`
with `name` and `local_path` null, instead of `subject: null` and the
limitation "Artifact identity is unavailable for this observation.".
Their Evidence IDs change.

* fix(evidence): name explicit unknown subjects in JavaScript callers and keep a managed graph's file subject

The verification scripts are not type-checked, so five createEvidence
callers still passed undefined (or a missing first target) after the subject
became required. They now pass unknownTarget("not-observed").

A managed application graph chose the first present input subject. Every
subject is now present, so an unknown artifact subject hid a file subject on
the member or boundary Evidence. The graph now takes the first input subject
that names a file.

* test(evidence): assert the subject kind instead of a presence the type now guarantees

Every Evidence record has a subject after this change, so test guards and
assertions that checked it for null always passed. Each now checks that the
fixture subject names a file, or is removed where a later fileIdentity,
fileSubjectTarget or fileSubjectDisplay call already requires that. Non-null
assertions on Evidence subjects are removed; analysis-view inputs, whose
subject is optional, keep an undefined check.

* test(evidence): expect the Evidence subject format under its identity

Two boundary tests still matched subject.format, which slice 2e moved into
subject.identity. toMatchObject is not type-checked against the Evidence
shape, so they failed only when run (the signature test on macOS).
2026-11-16 06:28:01 +08:00

1069 lines
39 KiB
TypeScript

import {
fileIdentity,
fileSubjectDisplay,
} from "../../fixtures/evidenceSubject.js";
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
import { fragmentInventoryEvidence } from "../../fixtures/artifactEvidence.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
import { ARTIFACT_COMPARISON_EXAMPLE } from "../../../src/contracts/artifactComparisonExample.js";
import {
artifactComparisonResultSchema,
compareArtifacts,
} from "../../../src/domain/artifactComparison.js";
import { parseArtifactInventoryEvidence } from "../../../src/domain/artifactInventoryEvidence.js";
import { createEvidence, type Evidence } from "../../../src/domain/evidence.js";
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
const PROVIDER = {
id: "rea-artifact-graph",
name: "REA artifact graph",
version: "1",
} as const;
const observe = async (path: string) => {
const inventory = await inventoryArtifact(path);
return createEvidence(
{
path,
sha256: inventory.manifest.root_sha256,
format: inventory.manifest.root_format,
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: inventory,
confidence: "observed",
authority: "shipped-artifact",
},
);
};
const inspectPages = (
pages: readonly Evidence[],
substepsCompleted = pages.length,
) => {
const first = pages[0];
if (first === undefined) throw new Error("Missing inspection page");
const inventory = artifactInventoryResultSchema.parse(
first.normalized_result,
);
return createEvidence(
{
path: "artifact-inspection-example",
sha256: inventory.manifest.root_sha256,
format: inventory.manifest.root_format,
},
PROVIDER,
{
operation: "inspect_artifact",
parameters: {},
result: {
inspection_id: `ai_${"f".repeat(64)}`,
subject: {
manifest_id: inventory.manifest.manifest_id,
root_artifact_id: inventory.manifest.root_artifact_id,
root_sha256: inventory.manifest.root_sha256,
root_format: inventory.manifest.root_format,
},
substeps: pages.map((evidence, index) => ({
substep_id: `ais_${(index + 1).toString(16).padStart(64, "0")}`,
operation: "inventory_artifact",
status: "completed",
evidence_id: evidence.evidence_id,
evidence,
})),
observations: [],
hypotheses: [],
contradictions: [],
unexplored_branches: [],
next_probes: [],
coverage: {
status: "complete-within-substeps",
substeps_completed: substepsCompleted,
observations_retained: 0,
observations_omitted: 0,
hypotheses_retained: 0,
hypotheses_omitted: 0,
unexplored_branches_retained: 0,
unexplored_branches_omitted: 0,
next_probes_retained: 0,
next_probes_omitted: 0,
},
evidence_links: pages.map(({ evidence_id }) => evidence_id),
limitations: [],
},
confidence: "observed",
authority: "shipped-artifact",
},
);
};
const changedArtifactComparison = async () => {
const parent = await createTestTempDirectory("rea-artifact-compare-");
const leftPath = join(parent, "left.app");
const rightPath = join(parent, "right.app");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
await Promise.all([
writeFile(join(leftPath, "main.js"), "old();"),
writeFile(join(leftPath, "same.txt"), "same"),
writeFile(join(rightPath, "main.js"), "newer();"),
writeFile(join(rightPath, "same.txt"), "same"),
writeFile(join(rightPath, "added.txt"), "added"),
]);
const left = await observe(leftPath);
const right = await observe(rightPath);
return {
left,
right,
result: compareArtifacts(left, right),
};
};
describe("leaf artifact comparison", () => {
it("rejects inventory Evidence without required producer coverage", async () => {
const root = await createTestTempDirectory("rea-artifact-plist-compare-");
const leftPath = join(root, "left.plist");
const xml =
'<?xml version="1.0"?><plist version="1.0"><dict><key>label</key><string>hello</string></dict></plist>';
await writeFile(leftPath, xml);
const left = await observe(leftPath);
const parsed = artifactInventoryResultSchema.parse(left.normalized_result);
const { coverage: _coverage, ...withoutCoverage } = parsed;
const malformed = createEvidence(
{
path: fileSubjectDisplay(left.subject).local_path,
sha256: fileIdentity(left.subject).sha256,
format: fileIdentity(left.subject).format,
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: withoutCoverage,
confidence: "observed",
authority: "shipped-artifact",
},
);
expect(() => parseArtifactInventoryEvidence(malformed)).toThrow();
});
it("keeps content and executable changes visible for complete plist roots", async () => {
const root = await createTestTempDirectory("rea-artifact-plist-deltas-");
const leftPath = join(root, "left.plist");
const rightPath = join(root, "right.plist");
const plist = (label: string) =>
`<?xml version="1.0"?><plist version="1.0"><dict><key>label</key><string>${label}</string></dict></plist>`;
await Promise.all([
writeFile(leftPath, plist("hello"), { mode: 0o644 }),
writeFile(rightPath, plist("world"), { mode: 0o644 }),
]);
const [left, changed] = await Promise.all([
observe(leftPath),
observe(rightPath),
]);
expect(compareArtifacts(left, changed)).toMatchObject({
status: "changed",
summary: { changed: 1, unknown: 0 },
changes: [
expect.objectContaining({
logical_path: ".",
classification: "changed",
dimensions: expect.arrayContaining(["content"]),
}),
],
});
if (process.platform !== "win32") {
await writeFile(rightPath, plist("hello"), { mode: 0o644 });
await chmod(rightPath, 0o755);
const executable = await observe(rightPath);
expect(compareArtifacts(left, executable)).toMatchObject({
status: "changed",
summary: { changed: 1, unknown: 0 },
changes: [
expect.objectContaining({
logical_path: ".",
classification: "changed",
dimensions: expect.arrayContaining(["executable"]),
}),
],
});
}
});
});
describe("artifact inspection inventory substeps", () => {
it("assembles every paged inventory substep and retains its outer Evidence once", async () => {
const root = await createTestTempDirectory("rea-inspect-pages-");
const path = join(root, "fixture.app");
await mkdir(path);
await Promise.all([
writeFile(join(path, "first.js"), "first();"),
writeFile(join(path, "second.js"), "second();"),
]);
const source = await observe(path);
const pages = fragmentInventoryEvidence(source, 2);
const inspection = inspectPages(pages);
const parsed = parseArtifactInventoryEvidence(inspection);
expect(parsed.evidence).toEqual([inspection]);
expect(parsed.inventory.complete).toBe(true);
expect(parsed.inventory.nodes).toHaveLength(
artifactInventoryResultSchema.parse(source.normalized_result).nodes
.length,
);
});
it("rejects inspection substeps with different inventory manifests", async () => {
const root = await createTestTempDirectory("rea-inspect-mixed-pages-");
const firstPath = join(root, "first.plist");
const secondPath = join(root, "second.plist");
await Promise.all([
writeFile(
firstPath,
'<?xml version="1.0"?><plist version="1.0"><dict><key>a</key><string>1</string></dict></plist>',
),
writeFile(
secondPath,
'<?xml version="1.0"?><plist version="1.0"><dict><key>b</key><string>2</string></dict></plist>',
),
]);
const inspection = inspectPages([
await observe(firstPath),
await observe(secondPath),
]);
expect(() => parseArtifactInventoryEvidence(inspection)).toThrow(
"Artifact inventory pages do not share one manifest",
);
});
it("rejects inspection coverage counts that disagree with retained substeps", async () => {
const root = await createTestTempDirectory("rea-inspect-count-mismatch-");
const path = join(root, "fixture.app");
await mkdir(path);
await Promise.all([
writeFile(join(path, "first.js"), "first();"),
writeFile(join(path, "second.js"), "second();"),
]);
const pages = fragmentInventoryEvidence(await observe(path), 2);
const inspection = inspectPages(pages, 1);
expect(() => parseArtifactInventoryEvidence(inspection)).toThrow(
"Artifact inspection substep count does not match its coverage declaration",
);
});
it("authenticates later inventory substeps instead of silently ignoring them", async () => {
const root = await createTestTempDirectory("rea-inspect-invalid-page-");
const path = join(root, "fixture.app");
await mkdir(path);
await writeFile(join(path, "main.js"), "main();");
const source = await observe(path);
const [first, later] = fragmentInventoryEvidence(source, 2);
if (first === undefined || later === undefined)
throw new Error("Missing inventory page");
const graph = artifactInventoryResultSchema.parse(later.normalized_result);
const tamperedLater: Evidence = {
...later,
normalized_result: jsonValueSchema.parse({
...graph,
limitations: ["Changed after this Evidence identifier was computed."],
}),
};
const inspection = inspectPages([first, tamperedLater]);
expect(() => parseArtifactInventoryEvidence(inspection)).toThrow(
"Evidence semantic identifier does not match its record",
);
});
});
describe("artifact inventory coverage", () => {
it("uses current unknown coverage without deriving it from limitation prose", async () => {
const root = await createTestTempDirectory("rea-artifact-coverage-");
const path = join(root, "complete.plist");
await writeFile(
path,
'<?xml version="1.0"?><plist version="1.0"><dict/></plist>',
);
const complete = await observe(path);
const graph = artifactInventoryResultSchema.parse(
complete.normalized_result,
);
const unknownCoverage = createEvidence(
{
path: fileSubjectDisplay(complete.subject).local_path,
sha256: fileIdentity(complete.subject).sha256,
format: fileIdentity(complete.subject).format,
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: {
...graph,
coverage: { scope: "root", status: "unknown" },
limitations: ["No extra child entries were observed."],
},
confidence: "derived",
authority: "shipped-artifact",
},
);
expect(
parseArtifactInventoryEvidence(unknownCoverage).inventory.complete,
).toBe(true);
expect(compareArtifacts(unknownCoverage, unknownCoverage)).toMatchObject({
status: "unknown",
summary: { unchanged: 0, unknown: 1 },
});
});
it("rejects inventory fragments that disagree on producer coverage", async () => {
const parent = await createTestTempDirectory("rea-artifact-page-coverage-");
const path = join(parent, "fixture.app");
await mkdir(path);
await writeFile(join(path, "main.js"), "main();");
const source = await observe(path);
const fragments = fragmentInventoryEvidence(source, 2);
const firstFragment = fragments[0];
const fragment = fragments[1];
if (firstFragment === undefined || fragment === undefined)
throw new Error("Missing test fragment");
const fragmentGraph = artifactInventoryResultSchema.parse(
fragment.normalized_result,
);
const mixed = createEvidence(
{
path: fileSubjectDisplay(fragment.subject).local_path,
sha256: fileIdentity(fragment.subject).sha256,
format: fileIdentity(fragment.subject).format,
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: fragment.parameters,
result: {
...fragmentGraph,
coverage: { ...fragmentGraph.coverage, status: "unknown" },
},
confidence: "derived",
authority: "shipped-artifact",
},
);
expect(() =>
parseArtifactInventoryEvidence([firstFragment, mixed]),
).toThrow("Artifact inventory pages do not share one coverage declaration");
});
});
describe("artifact comparison", () => {
it("keeps the canonical public example valid and fully comparable", () => {
const left = parseArtifactInventoryEvidence(
ARTIFACT_COMPARISON_EXAMPLE.left,
);
const right = parseArtifactInventoryEvidence(
ARTIFACT_COMPARISON_EXAMPLE.right,
);
expect(left.inventory).toMatchObject({
complete: true,
coverage: { scope: "root", status: "complete" },
});
expect(right.inventory).toMatchObject({
complete: true,
coverage: { scope: "root", status: "complete" },
});
expect(
artifactComparisonResultSchema.parse(
compareArtifacts(
ARTIFACT_COMPARISON_EXAMPLE.left,
ARTIFACT_COMPARISON_EXAMPLE.right,
),
),
).toMatchObject({
status: "changed",
summary: { changed: 1, unknown: 0 },
changes: [
expect.objectContaining({
logical_path: ".",
classification: "changed",
dimensions: expect.arrayContaining(["content"]),
}),
],
});
});
it("classifies deterministic path changes and cites both inventories", async () => {
const firstObservation = await changedArtifactComparison();
const secondObservation = await changedArtifactComparison();
const first = firstObservation.result;
const second = secondObservation.result;
expect(first).toEqual(second);
expect(artifactComparisonResultSchema.parse(first)).toMatchObject({
status: "changed",
summary: { added: 1, changed: 2, unknown: 0 },
changes: expect.arrayContaining([
expect.objectContaining({ logical_path: "added.txt" }),
]),
});
expect(first.changes[0]?.evidence_links).toEqual([
firstObservation.left.evidence_id,
firstObservation.right.evidence_id,
]);
expect(first.changes).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "added.txt",
classification: "added",
}),
expect.objectContaining({
logical_path: "main.js",
classification: "changed",
dimensions: expect.arrayContaining(["content", "size"]),
}),
]),
);
});
it("reports incomplete inventory as truncated, never unchanged", async () => {
const root = await createTestTempDirectory("rea-artifact-truncated-");
await writeFile(join(root, "one.txt"), "one");
const complete = await observe(root);
const completeInventory = artifactInventoryResultSchema.parse(
complete.normalized_result,
);
const incomplete = createEvidence(
{
path: root,
sha256: fileIdentity(complete.subject).sha256 ?? "0".repeat(64),
format: "directory",
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: {
...completeInventory,
nodes: [],
},
confidence: "observed",
authority: "shipped-artifact",
limitations: ["Inventory node observations are unavailable."],
},
);
const comparison = compareArtifacts(incomplete, incomplete);
expect(comparison).toMatchObject({
status: "truncated",
summary: { unchanged: 0, unknown: 2 },
});
expect(comparison.limitations).toContain(
"Left artifact inventory is incomplete.",
);
});
});
describe("artifact comparison completeness", () => {
it("does not infer child changes from root-only coverage", async () => {
const root = await createTestTempDirectory("rea-artifact-root-coverage-");
const leftPath = join(root, "left.app");
const rightPath = join(root, "right.app");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
await Promise.all([
writeFile(join(leftPath, "left-only.txt"), "left"),
writeFile(join(rightPath, "right-only.txt"), "right"),
]);
const [left, right] = await Promise.all([
observe(leftPath),
observe(rightPath),
]);
const rootOnly = (evidence: Awaited<ReturnType<typeof observe>>) => {
const inventory = artifactInventoryResultSchema.parse(
evidence.normalized_result,
);
return createEvidence(
{
path: evidence.subject?.local_path ?? "",
sha256: inventory.manifest.root_sha256,
format: inventory.manifest.root_format,
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: {
...inventory,
coverage: { scope: "root", status: "complete" },
},
confidence: "derived",
authority: "shipped-artifact",
},
);
};
const compared = compareArtifacts(rootOnly(left), rootOnly(right));
expect(compared.status).toBe("unknown");
expect(compared.summary).toMatchObject({ added: 0, removed: 0 });
expect(
compared.changes
.filter(({ logical_path }) => logical_path !== ".")
.map(({ logical_path, classification }) => ({
logical_path,
classification,
})),
).toEqual([
{ logical_path: "left-only.txt", classification: "unknown" },
{ logical_path: "right-only.txt", classification: "unknown" },
]);
expect(compared.limitations).toContain(
"Container entry coverage is unavailable for child paths.",
);
});
it("compares every graph member for inventories larger than 500 entries", async () => {
const root = await createTestTempDirectory("rea-artifact-pages-");
const entryCount = 501;
await Promise.all(
Array.from({ length: entryCount }, async (_, index) =>
writeFile(
join(root, `file-${String(index).padStart(3, "0")}.txt`),
String(index),
),
),
);
const complete = await observe(root);
const graph = artifactInventoryResultSchema.parse(
complete.normalized_result,
);
// The real risk above 500 entries is silent truncation, so assert the
// manifest counts reconcile with the arrays actually returned and that
// every written file is present. Deriving from `entryCount` keeps this
// valid at any size instead of freezing a page boundary.
expect(graph.manifest.node_count).toBe(graph.nodes.length);
expect(graph.manifest.occurrence_count).toBe(graph.occurrences.length);
expect(graph.manifest.edge_count).toBe(graph.edges.length);
const written = new Set(
Array.from(
{ length: entryCount },
(_, index) => `file-${String(index).padStart(3, "0")}.txt`,
),
);
// Occurrences carry the inventory paths, so completeness is checked there.
const paths = new Set(
graph.occurrences.map((occurrence) => occurrence.logical_path),
);
expect([...written].filter((path) => !paths.has(path))).toEqual([]);
expect(
graph.occurrences.filter(
(occurrence) => occurrence.entry_kind === "file",
),
).toHaveLength(entryCount);
expect(compareArtifacts(complete, complete)).toMatchObject({
status: "unchanged",
summary: { unchanged: 502, unknown: 0 },
changes: [],
});
});
});
it.runIf(process.platform !== "win32" && process.getuid?.() !== 0)(
"retains an observed map edge without certifying completeness after an omitted subtree",
async () => {
const root = await createTestTempDirectory("rea-map-omitted-subtree-");
const omitted = join(root, "omitted");
await mkdir(omitted);
await Promise.all([
writeFile(join(root, "main.js"), "source();"),
writeFile(join(root, "main.js.map"), "{}"),
writeFile(join(omitted, "member.txt"), "unexamined member"),
]);
const complete = await observe(root);
await chmod(omitted, 0);
const partial = await observe(root).finally(() => chmod(omitted, 0o700));
const { inventory } = parseArtifactInventoryEvidence(partial);
const map = inventory.occurrences.find(
({ logical_path }) => logical_path === "main.js.map",
);
expect(inventory.coverage.status).toBe("unknown");
expect(inventory.occurrences).not.toContainEqual(
expect.objectContaining({ logical_path: "omitted/member.txt" }),
);
expect(inventory.edges).toContainEqual(
expect.objectContaining({
occurrence_id: map?.occurrence_id,
relation: "maps-source",
}),
);
expect(map).toHaveProperty("relationship_coverage", "unknown");
expect(compareArtifacts(complete, partial).changes).toContainEqual(
expect.objectContaining({
logical_path: "main.js.map",
classification: "unknown",
dimensions: ["availability"],
}),
);
},
);
describe("artifact source-map relationship coverage", () => {
it("reports when an unchanged map points to changed source content", async () => {
const root = await createTestTempDirectory("rea-map-target-change-");
const leftPath = join(root, "left");
const rightPath = join(root, "right");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
await Promise.all([
writeFile(join(leftPath, "main.js"), "/* first source */\n"),
writeFile(join(rightPath, "main.js"), "/* second source */\n"),
writeFile(
join(leftPath, "main.js.map"),
'{"version":3,"sources":[],"names":[],"mappings":""}',
),
writeFile(
join(rightPath, "main.js.map"),
'{"version":3,"sources":[],"names":[],"mappings":""}',
),
]);
const [left, right] = await Promise.all([
observe(leftPath),
observe(rightPath),
]);
const comparison = compareArtifacts(left, right);
const leftInventory = parseArtifactInventoryEvidence(left).inventory;
const rightInventory = parseArtifactInventoryEvidence(right).inventory;
const leftMap = leftInventory.occurrences.find(
({ logical_path }) => logical_path === "main.js.map",
);
const rightMap = rightInventory.occurrences.find(
({ logical_path }) => logical_path === "main.js.map",
);
expect(leftMap?.artifact_id).toBe(rightMap?.artifact_id);
expect(comparison.changes).toContainEqual(
expect.objectContaining({
logical_path: "main.js.map",
classification: "changed",
dimensions: ["relations"],
left_artifact_id: leftMap?.artifact_id,
right_artifact_id: rightMap?.artifact_id,
}),
);
});
it("preserves unknown source-map relationships for duplicate ZIP paths", async () => {
const root = await createTestTempDirectory("rea-duplicate-source-map-");
const path = join(root, "duplicate-source-map.zip");
const uniquePath = join(root, "unique-source-map.zip");
const zip = async (otherSource: string) => {
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("main.js", new TextReader("first();"), { level: 0 });
await writer.add("your.js", new TextReader("second();"), { level: 0 });
await writer.add("main.js.map", new TextReader("{}"), { level: 0 });
await writer.add("other.js", new TextReader(otherSource), { level: 0 });
await writer.add("other.js.map", new TextReader("{}"), { level: 0 });
return Buffer.from(await writer.close());
};
const bytes = await zip("before();");
await writeFile(uniquePath, await zip("after();"));
for (const [signature, headerSize] of [
[Buffer.from([0x50, 0x4b, 0x03, 0x04]), 30],
[Buffer.from([0x50, 0x4b, 0x01, 0x02]), 46],
] as const) {
const first = bytes.indexOf(signature);
const second = bytes.indexOf(signature, first + 4);
if (first < 0 || second <= first)
throw new Error("Expected both source ZIP member headers");
expect(
bytes.subarray(second + headerSize, second + headerSize + 7).toString(),
).toBe("your.js");
bytes.write("main.js", second + headerSize);
}
await writeFile(path, bytes);
const evidence = await observe(path);
const parsed = parseArtifactInventoryEvidence(evidence);
const { inventory } = parsed;
const map = inventory.occurrences.find(
({ logical_path }) => logical_path === "main.js.map",
);
if (map === undefined) throw new Error("Expected source-map occurrence");
const mapEdge = inventory.edges.find(
({ occurrence_id }) => occurrence_id === map.occurrence_id,
);
expect(inventory.coverage.status).toBe("unknown");
expect(map.limitations).toContain(
'Source-map target is unknown because multiple occurrences share "main.js".',
);
expect(mapEdge).toMatchObject({ relation: "contains" });
expect(mapEdge?.relation).not.toBe("maps-source");
expect(inventory.integrityContradictions).toEqual([]);
expect(
inventory.limitations.some((limitation) =>
limitation.toLowerCase().includes("integrity"),
),
).toBe(false);
const unique = await observe(uniquePath);
for (const [left, right] of [
[unique, evidence],
[evidence, unique],
] as const) {
expect(compareArtifacts(left, right).changes).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js.map",
classification: "unknown",
dimensions: ["availability"],
left_artifact_id: map.artifact_id,
right_artifact_id: map.artifact_id,
}),
expect.objectContaining({
logical_path: "other.js.map",
classification: "changed",
dimensions: ["relations"],
}),
]),
);
}
expect(map).toHaveProperty("relationship_coverage", "unknown");
});
});
describe("duplicate archive occurrence ownership", () => {
it("keeps directory ancestry unknown when ZIP directory paths repeat", async () => {
const root = await createTestTempDirectory(
"rea-artifact-duplicate-directory-",
);
const path = join(root, "duplicate-directories.zip");
const uniquePath = join(root, "unique-directories.zip");
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("x/", new TextReader(""));
await writer.add("y/", new TextReader(""));
await writer.add("x/file.txt", new TextReader("member"));
await writer.add("standalone.js", new TextReader("source();"));
await writer.add("standalone.js.map", new TextReader("{}"));
const bytes = Buffer.from(await writer.close());
await writeFile(uniquePath, bytes);
for (const [signature, headerSize] of [
[Buffer.from([0x50, 0x4b, 0x03, 0x04]), 30],
[Buffer.from([0x50, 0x4b, 0x01, 0x02]), 46],
] as const) {
const first = bytes.indexOf(signature);
const second = bytes.indexOf(signature, first + 4);
if (first < 0 || second <= first)
throw new Error("Expected both ZIP directory headers");
expect(
bytes.subarray(second + headerSize, second + headerSize + 2).toString(),
).toBe("y/");
bytes.write("x/", second + headerSize);
}
await writeFile(path, bytes);
const evidence = await observe(path);
const inventory = parseArtifactInventoryEvidence(evidence).inventory;
const directory = inventory.occurrences.filter(
({ logical_path }) => logical_path === "x",
);
const child = inventory.occurrences.find(
({ logical_path }) => logical_path === "x/file.txt",
);
expect(directory).toHaveLength(2);
expect(directory.every(({ artifact_id }) => artifact_id === null)).toBe(
true,
);
expect(child?.parent_occurrence_id).toBeNull();
expect(child?.limitations).toContain(
'The parent occurrence for "x/file.txt" is unknown because multiple observed directory or container occurrences share "x".',
);
expect(inventory.coverage.status).toBe("unknown");
const unique = await observe(uniquePath);
for (const [left, right] of [
[unique, evidence],
[evidence, unique],
] as const)
expect(compareArtifacts(left, right).changes).toContainEqual(
expect.objectContaining({
logical_path: "x/file.txt",
classification: "unknown",
dimensions: ["availability"],
left_artifact_id: child?.artifact_id,
right_artifact_id: child?.artifact_id,
}),
);
expect(child).toHaveProperty("relationship_coverage", "unknown");
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: "standalone.js.map",
relationship_coverage: "complete",
}),
);
});
it("retains duplicate path occurrences across pages and leaves pairing unknown", async () => {
const root = await createTestTempDirectory("rea-artifact-duplicate-pages-");
const archives = [join(root, "left.zip"), join(root, "right.zip")] as const;
for (const [index, path] of archives.entries()) {
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("x.txt", new TextReader("first"));
await writer.add(
"y.txt",
new TextReader(index === 0 ? "second-left" : "second-right"),
);
await writer.add("neighbor.txt", new TextReader("neighbor"));
const bytes = Buffer.from(await writer.close());
// Produce a duplicate path the same way as real malformed/ambiguous ZIP
// inputs: retain each member record and rewrite only the equal-length name.
for (const [signature, headerSize] of [
[Buffer.from([0x50, 0x4b, 0x03, 0x04]), 30],
[Buffer.from([0x50, 0x4b, 0x01, 0x02]), 46],
] as const) {
const first = bytes.indexOf(signature);
const second = bytes.indexOf(signature, first + 4);
if (first < 0 || second <= first)
throw new Error("Expected both ZIP member headers");
expect(
bytes
.subarray(second + headerSize, second + headerSize + 5)
.toString(),
).toBe("y.txt");
bytes.write("x.txt", second + headerSize);
}
await writeFile(path, bytes);
}
const [left, right] = await Promise.all([
observe(archives[0]),
observe(archives[1]),
]);
const parsed = parseArtifactInventoryEvidence(left);
const repeated = parsed.inventory.occurrences.filter(
({ logical_path }) => logical_path === "x.txt",
);
expect(repeated).toHaveLength(2);
expect(
new Set(repeated.map(({ occurrence_id }) => occurrence_id)).size,
).toBe(2);
const leftPages = fragmentInventoryEvidence(left, 2);
const parsedPages = parseArtifactInventoryEvidence(leftPages);
expect(
parsedPages.inventory.occurrences.filter(
({ logical_path }) => logical_path === "x.txt",
),
).toHaveLength(2);
const comparison = compareArtifacts(
leftPages,
fragmentInventoryEvidence(right, 2),
);
expect(comparison).toMatchObject({
status: "unknown",
summary: { unknown: expect.any(Number) },
changes: expect.arrayContaining([
expect.objectContaining({
logical_path: "x.txt",
classification: "unknown",
dimensions: ["availability"],
left_occurrence_id: null,
right_occurrence_id: null,
left_artifact_id: null,
right_artifact_id: null,
}),
]),
});
expect(comparison.limitations).toContain(
"Duplicate logical paths cannot be paired across inventories; comparisons for those paths are unknown.",
);
// Occurrences on the unknown duplicate path are never counted unchanged.
// The archives differ, so the root changes and only neighbor.txt remains.
expect(comparison.summary).toMatchObject({ unchanged: 1, unknown: 1 });
expect(compareArtifacts(left, left).summary).toMatchObject({
unchanged: 2,
unknown: 1,
});
});
});
describe("artifact comparison fragment integrity", () => {
it("assembles real inventory fragments and retains every citation", async () => {
const parent = await createTestTempDirectory("rea-artifact-fragments-");
const leftPath = join(parent, "left.app");
const rightPath = join(parent, "right.app");
await Promise.all([mkdir(leftPath), mkdir(rightPath)]);
// Cross the former 100-citation limit with distinct, real graph members.
const fragmentCount = 101;
await Promise.all(
Array.from({ length: fragmentCount }, async (_, index) => {
await Promise.all([
writeFile(join(leftPath, `file-${index}.js`), `left(${index});`),
writeFile(join(rightPath, `file-${index}.js`), `right(${index});`),
]);
}),
);
const left = await observe(leftPath);
const right = await observe(rightPath);
const leftFragments = fragmentInventoryEvidence(left, fragmentCount);
const rightFragments = fragmentInventoryEvidence(right, fragmentCount);
const citations = [...leftFragments, ...rightFragments]
.map(({ evidence_id }) => evidence_id)
.sort();
expect(new Set(citations).size).toBe(fragmentCount * 2);
const comparison = compareArtifacts(leftFragments, rightFragments);
const complete = compareArtifacts(left, right);
expect(
comparison.changes
.filter(({ logical_path }) => logical_path !== ".")
.map(({ logical_path, classification }) => ({
logical_path,
classification,
}))
.sort((left, right) =>
left.logical_path.localeCompare(right.logical_path),
),
).toEqual(
Array.from({ length: fragmentCount }, (_, index) => ({
logical_path: `file-${index}.js`,
classification: "changed",
})).sort((left, right) =>
left.logical_path.localeCompare(right.logical_path),
),
);
expect({
...comparison,
changes: comparison.changes.map((change) => ({
...change,
evidence_links: [...change.evidence_links].sort(),
})),
}).toEqual({
...complete,
changes: complete.changes.map((change) => ({
...change,
evidence_links: citations,
})),
});
const fragment = leftFragments[0];
if (fragment === undefined) throw new Error("Expected inventory fragments");
const conflictingScope = createEvidence(
{
path: leftPath,
sha256: fileIdentity(left.subject).sha256 ?? "",
format: "directory",
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: {
...artifactInventoryResultSchema.parse(fragment.normalized_result),
coverage: { scope: "root", status: "complete" },
},
},
);
expect(() =>
compareArtifacts(
[conflictingScope, ...leftFragments.slice(1)],
rightFragments,
),
).toThrowError(
expect.objectContaining({
_tag: "AnalysisInputError",
userMessage: expect.stringContaining("coverage declaration"),
issues: [expect.objectContaining({ path: ["left"] })],
}),
);
});
it("rejects non-inventory and tampered Evidence", async () => {
const root = await createTestTempDirectory("rea-artifact-invalid-");
const evidence = await observe(root);
expect(() =>
compareArtifacts({ ...evidence, operation: "binary_overview" }, evidence),
).toThrow(/identifier/u);
const wrongOperation = createEvidence(
unknownTarget("not-observed"),
PROVIDER,
{
operation: "binary_overview",
parameters: {},
result: evidence.normalized_result,
},
);
expect(() => compareArtifacts(wrongOperation, evidence)).toThrowError(
expect.objectContaining({
_tag: "AnalysisInputError",
userMessage: expect.stringContaining("inspect_artifact"),
issues: [expect.objectContaining({ path: ["left"] })],
}),
);
const mismatchedSubject = createEvidence(
{ path: root, sha256: "f".repeat(64), format: "directory" },
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: evidence.normalized_result,
},
);
expect(() => compareArtifacts(mismatchedSubject, evidence)).toThrow(
/root digest/u,
);
});
});
describe("ZIP non-entry coverage", () => {
it("retains pre-directory APK block changes under current ZIP coverage", async () => {
const root = await createTestTempDirectory("rea-apk-block-compare-");
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("classes.dex", new TextReader("unchanged entry"));
const zip = Buffer.from(await writer.close());
const eocd = zip.length - 22;
expect(zip.readUInt32LE(eocd)).toBe(0x06054b50);
const directory = zip.readUInt32LE(eocd + 16);
// APK Signing Block framing with one opaque padding pair, not a verified signature.
// https://source.android.com/docs/security/features/apksigning/v2#apk-signing-block
const block = Buffer.alloc(48);
block.writeBigUInt64LE(40n, 0);
block.writeBigUInt64LE(8n, 8);
block.writeUInt32LE(0x42726577, 16);
block.writeBigUInt64LE(40n, 24);
block.write("APK Sig Block 42", 32, "ascii");
const paths = [join(root, "before.apk"), join(root, "after.apk")];
for (const [index, path] of paths.entries()) {
block.writeUInt32LE(index, 20);
const apk = Buffer.concat([
zip.subarray(0, directory),
block,
zip.subarray(directory),
]);
apk.writeUInt32LE(directory + block.length, eocd + block.length + 16);
await writeFile(path, apk);
}
const left = await observe(paths[0]!);
const right = await observe(paths[1]!);
const inventory = artifactInventoryResultSchema.parse(
left.normalized_result,
);
expect(inventory.limitations).toContain(
"ZIP inventory lists central-directory entries only. Non-entry bytes, including leading bytes before the first entry such as a self-extractor stub, an APK Signing Block before the central directory or a ZIP comment after it, are not occurrences.",
);
expect(
inventory.occurrences.map(({ logical_path }) => logical_path).sort(),
).toEqual([".", "classes.dex"]);
expect(compareArtifacts(left, right)).toMatchObject({
status: "changed",
summary: { changed: 1, unchanged: 1, unknown: 0 },
changes: [
expect.objectContaining({ logical_path: ".", dimensions: ["content"] }),
],
});
});
});