mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* 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).
1266 lines
44 KiB
TypeScript
1266 lines
44 KiB
TypeScript
import { unknownTarget } from "../../../src/domain/targetIdentity.js";
|
|
import {
|
|
lstat,
|
|
mkdir,
|
|
readdir,
|
|
readFile,
|
|
stat,
|
|
symlink,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { getHeapStatistics } from "node:v8";
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import {
|
|
inventoryArtifact,
|
|
withArtifactResourceScope,
|
|
} from "../../fixtures/artifactInventory.js";
|
|
import { createArtifactInspection } from "../../../src/domain/artifactInspection.js";
|
|
|
|
import {
|
|
readEvidenceBundle,
|
|
readImmutableEvidenceBundle,
|
|
writeEvidenceBundle,
|
|
} from "../../../src/application/EvidenceBundleFiles.js";
|
|
import {
|
|
compareEvidenceBundlesCommand,
|
|
publishEvidenceDelivery,
|
|
} from "../../../src/application/EvidenceBundleCommands.js";
|
|
import { EvidenceLedger } from "../../../src/application/investigation/EvidenceLedger.js";
|
|
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
|
|
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
|
|
import { OwnedStagedOutput } from "../../../src/filesystem/OwnedStagedOutput.js";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import {
|
|
createEvidence,
|
|
parseEvidence,
|
|
withEvidenceCallerInputPath,
|
|
type Evidence,
|
|
} from "../../../src/domain/evidence.js";
|
|
import { writeTextParts } from "../../../src/resources/TextFileWrite.js";
|
|
import { failFileReadAfterAbort } from "../../support/failFileReadAfterAbort.js";
|
|
import { holdFirstFileRead } from "../../support/holdFirstFileRead.js";
|
|
import { JsonInputPhysicalReadFailure } from "../../../src/application/JsonInputFile.js";
|
|
import {
|
|
createEvidenceBundle,
|
|
createImmutableEvidenceBundle,
|
|
parseEvidenceBundle,
|
|
serializeEvidenceBundle,
|
|
isImmutableEvidenceBundle,
|
|
} from "../../../src/domain/evidenceBundle.js";
|
|
import { analysisViewLayoutEvidence } from "../../fixtures/analysisView.js";
|
|
import { publishCliAnalysisEvidence } from "../../../src/cli/analysisEvidenceDelivery.js";
|
|
import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "../../../src/domain/process/processCaptureExample.js";
|
|
import { PROCESS_PROVIDER } from "../../../src/domain/process/processEvidenceProvider.js";
|
|
import * as processCaptureParsing from "../../../src/domain/process/processCaptureParsing.js";
|
|
import type { JsonValue } from "../../../src/domain/jsonValue.js";
|
|
|
|
vi.mock("node:v8", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("node:v8")>();
|
|
return {
|
|
...actual,
|
|
getHeapStatistics: vi.fn(actual.getHeapStatistics),
|
|
};
|
|
});
|
|
|
|
const bundle = (result = true) =>
|
|
createEvidenceBundle([
|
|
createEvidence(
|
|
unknownTarget("not-observed"),
|
|
{ id: "fixture", name: "Fixture", version: "1" },
|
|
{ operation: "health", parameters: {}, result },
|
|
),
|
|
]);
|
|
|
|
it.each(["cycle", "accessor"] as const)(
|
|
"rejects an unowned bundle containing a %s before estimating or publishing it",
|
|
async (malformation) => {
|
|
const root = await createTestTempDirectory("rea-malformed-checkpoint-");
|
|
const path = join(root, "caller-selected.json");
|
|
const original = Buffer.from(
|
|
"The caller's previous checkpoint remains intact.\n",
|
|
);
|
|
await writeFile(path, original);
|
|
const value = bundle();
|
|
const record = value.records[0];
|
|
if (record === undefined) throw new Error("Expected the fixture record");
|
|
const getter = vi.fn(() => {
|
|
throw new Error("Malformed bundle accessors must never be invoked");
|
|
});
|
|
if (malformation === "cycle") {
|
|
const cycle: Record<string, JsonValue> = {};
|
|
cycle.self = cycle;
|
|
record.normalized_result = cycle;
|
|
} else {
|
|
Object.defineProperty(record, "normalized_result", {
|
|
get: getter,
|
|
enumerable: true,
|
|
configurable: true,
|
|
});
|
|
}
|
|
expect(await writeBundle(value, path, true)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceIntegrityError" },
|
|
});
|
|
expect(getter).not.toHaveBeenCalled();
|
|
expect((await readFile(path)).equals(original)).toBe(true);
|
|
expect(await readdir(root)).toEqual(["caller-selected.json"]);
|
|
},
|
|
);
|
|
|
|
it("admits the live process-payload validation working set before writing an owned checkpoint", async () => {
|
|
const root = await createTestTempDirectory(
|
|
"rea-owned-process-checkpoint-memory-",
|
|
);
|
|
const path = join(root, "caller-selected.json");
|
|
const original = Buffer.from(
|
|
"The caller's previous checkpoint remains intact.\n",
|
|
);
|
|
await writeFile(path, original);
|
|
// This minimal authored process fixture proves the format boundary. It does
|
|
// not claim a real process/provider capture or an actual heap exhaustion.
|
|
const capture = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
PROCESS_PROVIDER,
|
|
{
|
|
predicateType: "rea.process-capture",
|
|
operation: "capture_process_scenario",
|
|
parameters: {},
|
|
result: EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
|
confidence: "observed",
|
|
authority: "controlled-replay",
|
|
},
|
|
{ immutable: true },
|
|
);
|
|
// Construct the owner before mocking current headroom; its initial retention
|
|
// policy must not be confused with this later payload-validation allowance.
|
|
const ledger = new EvidenceLedger();
|
|
expect(ledger.record(capture).ok).toBe(true);
|
|
const owned = ledger.immutableBundle();
|
|
expect(isImmutableEvidenceBundle(owned)).toBe(true);
|
|
const retained = ledger.forAnalysis(capture.evidence_id);
|
|
const statistics = ledger.statistics();
|
|
const heap = getHeapStatistics();
|
|
const parser = vi.spyOn(processCaptureParsing, "safeParseProcessCapture");
|
|
const publication = vi.spyOn(
|
|
OwnedStagedOutput.prototype,
|
|
"assertReadyForPublication",
|
|
);
|
|
vi.mocked(getHeapStatistics).mockReturnValue({
|
|
...heap,
|
|
total_available_size: 0,
|
|
});
|
|
try {
|
|
const refused = await withArtifactResourceScope((resources) =>
|
|
writeEvidenceBundle(
|
|
owned,
|
|
path,
|
|
true,
|
|
resources,
|
|
undefined,
|
|
"reset_investigation",
|
|
),
|
|
);
|
|
expect(refused).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisResourceConstraintError",
|
|
operation: "reset_investigation",
|
|
resource: "memory",
|
|
reportedLimits: { input_path: path, available_heap_bytes: 0 },
|
|
},
|
|
});
|
|
expect(parser).not.toHaveBeenCalled();
|
|
expect(publication).not.toHaveBeenCalled();
|
|
expect((await readFile(path)).equals(original)).toBe(true);
|
|
expect(await readdir(root)).toEqual(["caller-selected.json"]);
|
|
expect(ledger.statistics()).toEqual(statistics);
|
|
expect(ledger.forAnalysis(capture.evidence_id)).toBe(retained);
|
|
} finally {
|
|
vi.mocked(getHeapStatistics).mockReset();
|
|
parser.mockRestore();
|
|
publication.mockRestore();
|
|
}
|
|
|
|
const published = await withArtifactResourceScope((resources) =>
|
|
writeEvidenceBundle(
|
|
owned,
|
|
path,
|
|
true,
|
|
resources,
|
|
undefined,
|
|
"reset_investigation",
|
|
),
|
|
);
|
|
expect(published).toMatchObject({ ok: true, value: { path } });
|
|
expect(await readEvidenceBundle(path)).toEqual({ ok: true, value: owned });
|
|
expect(ledger.statistics()).toEqual(statistics);
|
|
});
|
|
|
|
it("preserves tagged refusal JSON without interpreting Evidence-shaped fields", async () => {
|
|
const root = await createTestTempDirectory("rea-refused-delivery-");
|
|
const refusal = {
|
|
code: "unreadable_output",
|
|
evidence_id: "caller-selected diagnostic",
|
|
normalized_result: { partial: true },
|
|
};
|
|
const delivered = await publishCliAnalysisEvidence(
|
|
{ ok: false, error: refusal },
|
|
{
|
|
operation: "inspect_artifact",
|
|
signal: new AbortController().signal,
|
|
detail: "summary",
|
|
evidenceOutput: join(root, "refused.json"),
|
|
},
|
|
);
|
|
expect(delivered).toBe(refusal);
|
|
expect(await readdir(root)).toEqual([]);
|
|
});
|
|
|
|
it("merges CLI identities for repeated inspection sources", async () => {
|
|
const root = await createTestTempDirectory("rea-repeated-delivery-");
|
|
const input = join(root, "input.txt");
|
|
await writeFile(input, "artifact contents");
|
|
const inventory = await inventoryArtifact(input);
|
|
const source = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
{ id: "fixture", name: "Fixture", version: "1" },
|
|
{
|
|
operation: "inventory_artifact",
|
|
parameters: {},
|
|
result: inventory,
|
|
},
|
|
);
|
|
const inspection = createArtifactInspection(source);
|
|
const complete = withEvidenceCallerInputPath(
|
|
createEvidence(unknownTarget("not-observed"), source.provider, {
|
|
operation: "inspect_artifact",
|
|
parameters: {},
|
|
result: {
|
|
...inspection,
|
|
substeps: [...inspection.substeps, ...inspection.substeps],
|
|
},
|
|
}),
|
|
"./selected",
|
|
);
|
|
const path = join(root, "cli.json");
|
|
const delivered = await publishCliAnalysisEvidence(
|
|
{ ok: true, value: complete },
|
|
{
|
|
operation: "inspect_artifact",
|
|
signal: new AbortController().signal,
|
|
detail: "complete",
|
|
evidenceOutput: path,
|
|
sourceEvidence: [withEvidenceCallerInputPath(complete, "./old"), source],
|
|
},
|
|
);
|
|
expect(delivered).toEqual(complete);
|
|
const saved = await readEvidenceBundle(path);
|
|
if (!saved.ok) throw saved.error;
|
|
expect(saved.value.records).toHaveLength(2);
|
|
expect(
|
|
saved.value.records.find(
|
|
(record) => record.evidence_id === complete.evidence_id,
|
|
)?.caller_input_path,
|
|
).toBe("./selected");
|
|
});
|
|
|
|
it("retains a malformed artifact parent and refuses its publication", async () => {
|
|
const root = await createTestTempDirectory("rea-malformed-delivery-");
|
|
const evidence = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
{ id: "fixture", name: "Fixture", version: "1" },
|
|
{ operation: "inspect_artifact", parameters: {}, result: {} },
|
|
);
|
|
const cli = await publishCliAnalysisEvidence(
|
|
{ ok: true, value: evidence },
|
|
{
|
|
operation: "inspect_artifact",
|
|
signal: new AbortController().signal,
|
|
detail: "complete",
|
|
evidenceOutput: join(root, "cli.json"),
|
|
},
|
|
);
|
|
// The one-shot CLI explains that malformed embedded sources could not be
|
|
// collected before exit.
|
|
expect(cli).toMatchObject({
|
|
details: {
|
|
partial_observation: evidence,
|
|
source_closure: {
|
|
publication: "not_published",
|
|
complete: false,
|
|
collection_failure: "Error",
|
|
records: [],
|
|
omitted_count: 0,
|
|
omitted: [],
|
|
recovery: expect.stringContaining(
|
|
"this invocation retains nothing after exit",
|
|
),
|
|
},
|
|
},
|
|
});
|
|
expect(JSON.stringify(cli)).toContain(evidence.evidence_id);
|
|
expect(await readdir(root)).toEqual([]);
|
|
});
|
|
|
|
it.each([undefined, "../selected-link/./artifact"])(
|
|
"keeps same-invocation caller input on delivered summary and saved parent (%s)",
|
|
async (callerInputPath) => {
|
|
const root = await createTestTempDirectory("rea-delivery-input-path-");
|
|
const path = join(root, "bundle.json");
|
|
const original = analysisViewLayoutEvidence();
|
|
const parent =
|
|
callerInputPath === undefined
|
|
? original
|
|
: withEvidenceCallerInputPath(original, callerInputPath, {
|
|
operations_path: "./query 雪.graphql",
|
|
});
|
|
const output = await publishCliAnalysisEvidence(
|
|
{ ok: true, value: parent },
|
|
{
|
|
operation: parent.operation,
|
|
signal: new AbortController().signal,
|
|
detail: "summary",
|
|
evidenceOutput: path,
|
|
},
|
|
);
|
|
const delivered = parseEvidence((output as { summary: unknown }).summary);
|
|
expect(delivered.caller_input_path).toBe(callerInputPath);
|
|
expect(delivered.caller_input_paths).toEqual(parent.caller_input_paths);
|
|
expect(delivered.evidence_links).toContain(parent.evidence_id);
|
|
expect(original.caller_input_path).toBeUndefined();
|
|
const saved = await readEvidenceBundle(path);
|
|
if (!saved.ok) throw saved.error;
|
|
const savedParent = saved.value.records.find(
|
|
(record) => record.evidence_id === parent.evidence_id,
|
|
);
|
|
expect(savedParent?.caller_input_path).toBe(callerInputPath);
|
|
expect(savedParent?.caller_input_paths).toEqual(parent.caller_input_paths);
|
|
},
|
|
);
|
|
|
|
it.each([0, 9 * 1024 * 1024])(
|
|
"preserves the format owner's BOM recovery with %i bytes of padding",
|
|
async (padding) => {
|
|
const root = await createTestTempDirectory("rea-bundle-bom-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(
|
|
path,
|
|
`\uFEFF${" ".repeat(padding)}${JSON.stringify(bundle())}`,
|
|
);
|
|
const result = await readEvidenceBundle(path);
|
|
if (result.ok) throw new Error("Expected strict JSON BOM rejection");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "evidence_integrity_mismatch",
|
|
message: expect.stringMatching(
|
|
/byte-order mark.*UTF-8 without a byte-order mark/iu,
|
|
),
|
|
remediation: {
|
|
action: expect.stringMatching(
|
|
/byte-order mark.*UTF-8 without a byte-order mark/iu,
|
|
),
|
|
},
|
|
details: {
|
|
path,
|
|
reason: "invalid-json",
|
|
content_issues: [
|
|
{
|
|
reason: "invalid_format",
|
|
message: expect.stringMatching(
|
|
/byte-order mark.*UTF-8 without a byte-order mark/iu,
|
|
),
|
|
},
|
|
],
|
|
},
|
|
});
|
|
// Ordinary malformed JSON keeps its distinct parser diagnostic and the
|
|
// existing general recovery; consumers do not infer BOMs from text.
|
|
await writeFile(path, `${" ".repeat(padding)}{`);
|
|
const malformed = await readEvidenceBundle(path);
|
|
if (malformed.ok) throw new Error("Expected malformed JSON rejection");
|
|
expect(projectAnalysisError(malformed.error).message).toContain(
|
|
"Repair or recreate",
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["immutable", readImmutableEvidenceBundle],
|
|
["mutable", readEvidenceBundle],
|
|
] as const)(
|
|
"cancels a %s bundle read before opening its selected path",
|
|
async (_, read) => {
|
|
const root = await createTestTempDirectory("rea-bundle-pre-cancel-");
|
|
const controller = new AbortController();
|
|
const reason = new Error("cancel before bundle read");
|
|
controller.abort(reason);
|
|
const result = await read(join(root, "missing.json"), controller.signal);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
operation: "import_evidence_bundle",
|
|
cause: controller.signal.reason,
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected cancelled bundle read");
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(await readdir(root)).toEqual([]);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["mutable", readEvidenceBundle],
|
|
["immutable", readImmutableEvidenceBundle],
|
|
] as const)(
|
|
"preserves the caller cancellation reason during %s read",
|
|
async (_, read) => {
|
|
const root = await createTestTempDirectory("rea-bundle-read-cancel-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(path, JSON.stringify(bundle()));
|
|
const reader = await holdFirstFileRead(path);
|
|
const controller = new AbortController();
|
|
const reason = new Error("stop bundle import");
|
|
try {
|
|
const pending = read(path, controller.signal);
|
|
await reader.reached(pending, "first file read");
|
|
controller.abort(reason);
|
|
reader.release();
|
|
const result = await pending;
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
operation: "import_evidence_bundle",
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected cancelled bundle read");
|
|
expect(result.error.cause).toBe(reason);
|
|
} finally {
|
|
controller.abort(reason);
|
|
reader.release();
|
|
vi.restoreAllMocks();
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["mutable", readEvidenceBundle],
|
|
["immutable", readImmutableEvidenceBundle],
|
|
] as const)(
|
|
"keeps the physical null read cause for %s import after cancellation",
|
|
async (_, read) => {
|
|
const root = await createTestTempDirectory("rea-bundle-late-io-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(path, JSON.stringify(bundle()));
|
|
const controller = new AbortController();
|
|
await failFileReadAfterAbort(
|
|
path,
|
|
controller,
|
|
new Error("later abort"),
|
|
null,
|
|
);
|
|
try {
|
|
const result = await read(path, controller.signal);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "io", path },
|
|
});
|
|
if (result.ok) throw new Error("Expected physical JSON read failure");
|
|
expect(result.error.cause).toBeNull();
|
|
} finally {
|
|
vi.restoreAllMocks();
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["mutable", readEvidenceBundle],
|
|
["immutable", readImmutableEvidenceBundle],
|
|
] as const)(
|
|
"keeps an identical physical and abort reason for %s import",
|
|
async (_, read) => {
|
|
const root = await createTestTempDirectory("rea-bundle-identical-abort-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(path, JSON.stringify(bundle()));
|
|
const controller = new AbortController();
|
|
const physical = new JsonInputPhysicalReadFailure(
|
|
Object.assign(new Error("physical EIO"), { code: "EIO" }),
|
|
);
|
|
await failFileReadAfterAbort(path, controller, physical, physical);
|
|
try {
|
|
const result = await read(path, controller.signal);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "io" },
|
|
});
|
|
if (result.ok) throw new Error("Expected physical JSON read failure");
|
|
expect(result.error.cause).toBe(physical);
|
|
} finally {
|
|
vi.restoreAllMocks();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("preserves the failed-close owner when import is also cancelled", async () => {
|
|
const root = await createTestTempDirectory("rea-import-cancel-cleanup-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(path, JSON.stringify(bundle()));
|
|
const controller = new AbortController();
|
|
const cleanupCause = new Error("fixture close failure");
|
|
const cancellationReason = new JsonInputPhysicalReadFailure(
|
|
new Error("opaque cancellation reason"),
|
|
);
|
|
let owner: OwnedFileHandle | undefined;
|
|
const resources = new ArtifactResourceScope();
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
owner = this;
|
|
controller.abort(cancellationReason);
|
|
throw cleanupCause;
|
|
});
|
|
try {
|
|
const result = await readImmutableEvidenceBundle(
|
|
path,
|
|
controller.signal,
|
|
resources,
|
|
);
|
|
if (result.ok) throw new Error("Expected failed finalization");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "EvidenceFileError",
|
|
cleanupIncomplete: true,
|
|
cleanupPrimary: {
|
|
_tag: "AnalysisCancelledError",
|
|
cause: cancellationReason,
|
|
},
|
|
});
|
|
expect(owner?.handle.fd).toBeGreaterThanOrEqual(0);
|
|
close.mockRestore();
|
|
await resources.close();
|
|
expect(owner?.handle.fd).toBe(-1);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
} finally {
|
|
close.mockRestore();
|
|
await resources.close();
|
|
}
|
|
expect(await readEvidenceBundle(path)).toMatchObject({ ok: true });
|
|
});
|
|
|
|
describe("evidence bundle export cancellation", () => {
|
|
it("retains the completed parent and exact reason when delivery is cancelled before publication", async () => {
|
|
const root = await createTestTempDirectory("rea-delivery-cancel-");
|
|
const parent = analysisViewLayoutEvidence();
|
|
const controller = new AbortController();
|
|
const reason = new Error("cancel before bundle publication");
|
|
controller.abort(reason);
|
|
const result = await publishParent(
|
|
parent,
|
|
join(root, "bundle.json"),
|
|
controller.signal,
|
|
);
|
|
if (result.ok) throw new Error("Cancelled delivery reported success");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
operation: parent.operation,
|
|
});
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "cancelled",
|
|
details: { operation: parent.operation },
|
|
});
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.partialObservation).toBe(parent);
|
|
expect(await readdir(root)).toEqual([]);
|
|
});
|
|
|
|
it("does not inspect the path or advance a source cancelled before writing", async () => {
|
|
const root = await createTestTempDirectory("rea-export-pre-cancel-");
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
let advanced = false;
|
|
function* parts() {
|
|
advanced = true;
|
|
yield "unused";
|
|
}
|
|
expect(
|
|
await writeParts(parts(), join(root, "missing", "bundle.json"), false, {
|
|
signal: controller.signal,
|
|
operation: "export_evidence_bundle",
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
operation: "export_evidence_bundle",
|
|
cause: controller.signal.reason,
|
|
},
|
|
});
|
|
expect(advanced).toBe(false);
|
|
expect(await readdir(root)).toEqual([]);
|
|
expect(
|
|
await writeBundle(
|
|
bundle(),
|
|
join(root, "bundle.json"),
|
|
false,
|
|
controller.signal,
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
cause: controller.signal.reason,
|
|
},
|
|
});
|
|
expect(await readdir(root)).toEqual([]);
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"closes a cancelled source and preserves the destination (overwrite: %s)",
|
|
async (overwrite) => {
|
|
const root = await createTestTempDirectory("rea-export-cancel-");
|
|
const path = join(root, "bundle.json");
|
|
if (overwrite) await writeFile(path, "original");
|
|
const controller = new AbortController();
|
|
const cancellationReason = new Error("cancel staged publication");
|
|
let closed = false;
|
|
let continued = false;
|
|
function* parts() {
|
|
try {
|
|
yield "staged partial data";
|
|
controller.abort(cancellationReason);
|
|
yield "cancelled data";
|
|
continued = true;
|
|
yield "unreachable";
|
|
} finally {
|
|
closed = true;
|
|
}
|
|
}
|
|
expect(
|
|
await writeParts(parts(), path, overwrite, {
|
|
signal: controller.signal,
|
|
operation: "export_evidence_bundle",
|
|
}),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisCancelledError",
|
|
operation: "export_evidence_bundle",
|
|
cause: cancellationReason,
|
|
},
|
|
});
|
|
expect(closed).toBe(true);
|
|
expect(continued).toBe(false);
|
|
expect(await readdir(root)).toEqual(overwrite ? ["bundle.json"] : []);
|
|
if (overwrite) expect(await readFile(path, "utf8")).toBe("original");
|
|
expect(await writeBundle(bundle(), path, overwrite)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(await readEvidenceBundle(path)).toEqual({
|
|
ok: true,
|
|
value: bundle(),
|
|
});
|
|
},
|
|
);
|
|
|
|
it("does not publish when cancellation arrives at source exhaustion", async () => {
|
|
const root = await createTestTempDirectory("rea-export-final-cancel-");
|
|
const path = join(root, "bundle.json");
|
|
await writeFile(path, "original");
|
|
const controller = new AbortController();
|
|
function* parts() {
|
|
yield "complete replacement";
|
|
controller.abort();
|
|
}
|
|
expect(
|
|
await writeParts(parts(), path, true, {
|
|
signal: controller.signal,
|
|
operation: "export_evidence_bundle",
|
|
}),
|
|
).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
|
|
expect(await readFile(path, "utf8")).toBe("original");
|
|
expect(await readdir(root)).toEqual(["bundle.json"]);
|
|
});
|
|
});
|
|
|
|
it.each([1, 2])(
|
|
"reports only unresolved cancellation cleanup after %s failed close attempts",
|
|
async (failedAttempts) => {
|
|
const root = await createTestTempDirectory("rea-delivery-cleanup-retry-");
|
|
const parent = analysisViewLayoutEvidence();
|
|
const controller = new AbortController();
|
|
const reason = new Error("cancel before bundle publication");
|
|
const cleanupCause = new Error("fixture staged-output close failure");
|
|
const ready = OwnedStagedOutput.prototype.assertReadyForPublication;
|
|
const close = OwnedStagedOutput.prototype.close;
|
|
let owner: OwnedStagedOutput | undefined;
|
|
let attempts = 0;
|
|
const publication = vi
|
|
.spyOn(OwnedStagedOutput.prototype, "assertReadyForPublication")
|
|
.mockImplementationOnce(async function (this: OwnedStagedOutput, staged) {
|
|
await ready.call(this, staged);
|
|
controller.abort(reason);
|
|
});
|
|
const cleanup = vi
|
|
.spyOn(OwnedStagedOutput.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedStagedOutput) {
|
|
owner = this;
|
|
if (++attempts <= failedAttempts) throw cleanupCause;
|
|
await close.call(this);
|
|
});
|
|
try {
|
|
const result = await publishParent(
|
|
parent,
|
|
join(root, "bundle.json"),
|
|
controller.signal,
|
|
);
|
|
if (result.ok) throw new Error("Cancelled delivery reported success");
|
|
expect(attempts).toBe(2);
|
|
expect(result.error._tag).toBe("AnalysisCancelledError");
|
|
expect(result.error.partialObservation).toBe(parent);
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.cleanupPrimary).toMatchObject({
|
|
operation: parent.operation,
|
|
});
|
|
expect(result.error.cleanupPrimary?.cause).toBe(reason);
|
|
expect(result.error.cleanup?.reason).toBe(cleanupCause.message);
|
|
const incomplete = failedAttempts === 2;
|
|
expect(result.error.cleanupIncomplete).toBe(incomplete);
|
|
expect(result.error.cleanupResources).toEqual(
|
|
incomplete ? [owner?.directory] : [],
|
|
);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: incomplete ? "cleanup_incomplete" : "cancelled",
|
|
details: { operation: parent.operation },
|
|
});
|
|
expect((await readdir(root)).map((entry) => join(root, entry))).toEqual(
|
|
incomplete ? [owner?.directory] : [],
|
|
);
|
|
} finally {
|
|
publication.mockRestore();
|
|
cleanup.mockRestore();
|
|
await owner?.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
/** Publish a parent's complete bundle and deliver the parent itself. */
|
|
const publishParent = (parent: Evidence, path: string, signal: AbortSignal) =>
|
|
publishEvidenceDelivery({
|
|
bundle: createImmutableEvidenceBundle([parent]),
|
|
complete: parent,
|
|
delivered: parent,
|
|
outputPath: path,
|
|
operation: parent.operation,
|
|
signal,
|
|
});
|
|
|
|
const writeBundle = (
|
|
value: ReturnType<typeof bundle>,
|
|
path: string,
|
|
overwrite: boolean,
|
|
signal?: AbortSignal,
|
|
) =>
|
|
withArtifactResourceScope((resources) =>
|
|
writeEvidenceBundle(value, path, overwrite, resources, signal),
|
|
);
|
|
|
|
const writeParts = (
|
|
parts: Iterable<string>,
|
|
path: string,
|
|
overwrite: boolean,
|
|
cancellation?: {
|
|
readonly signal: AbortSignal;
|
|
readonly operation: string;
|
|
},
|
|
) =>
|
|
withArtifactResourceScope((resources) =>
|
|
writeTextParts(parts, path, overwrite, resources, cancellation),
|
|
);
|
|
|
|
describe("evidence bundle publication", () => {
|
|
it("retains the committed bundle receipt when cancellation arrives during final cleanup", async () => {
|
|
const root = await createTestTempDirectory(
|
|
"rea-delivery-published-cancel-",
|
|
);
|
|
const path = join(root, "bundle.json");
|
|
const parent = analysisViewLayoutEvidence();
|
|
const controller = new AbortController();
|
|
const reason = new Error("cancel after actual staged-output cleanup");
|
|
const close = OwnedStagedOutput.prototype.close;
|
|
const seam = vi
|
|
.spyOn(OwnedStagedOutput.prototype, "close")
|
|
.mockImplementationOnce(async function (this: OwnedStagedOutput) {
|
|
await close.call(this);
|
|
controller.abort(reason);
|
|
});
|
|
let result: Awaited<ReturnType<typeof publishParent>>;
|
|
try {
|
|
result = await publishParent(parent, path, controller.signal);
|
|
} finally {
|
|
seam.mockRestore();
|
|
}
|
|
if (result.ok) throw new Error("Cancelled delivery reported success");
|
|
expect(result.error._tag).toBe("AnalysisCancelledError");
|
|
expect(result.error.cause).toBe(reason);
|
|
const bytes = await readFile(path);
|
|
expect(result.error.diagnostics?.evidence_bundle).toEqual({
|
|
path,
|
|
bytes: bytes.length,
|
|
});
|
|
expect(result.error.partialObservation).toBe(parent);
|
|
expect(
|
|
parseEvidenceBundle(JSON.parse(bytes.toString("utf8"))).records[0]
|
|
?.evidence_id,
|
|
).toBe(parent.evidence_id);
|
|
expect(await readdir(root)).toEqual(["bundle.json"]);
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"never publishes a partial streamed file (overwrite: %s)",
|
|
async (overwrite) => {
|
|
const root = await createTestTempDirectory("rea-stream-publication-");
|
|
const path = join(root, "bundle.json");
|
|
if (overwrite) await writeFile(path, "original");
|
|
function* brokenParts() {
|
|
yield "partial";
|
|
throw new Error("Source stream failed");
|
|
}
|
|
expect(await writeParts(brokenParts(), path, overwrite)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "io" },
|
|
});
|
|
expect(await readdir(root)).toEqual(overwrite ? ["bundle.json"] : []);
|
|
if (overwrite) expect(await readFile(path, "utf8")).toBe("original");
|
|
},
|
|
);
|
|
|
|
it("counts complete Unicode bytes and publishes a private file", async () => {
|
|
const root = await createTestTempDirectory("rea-stream-bytes-");
|
|
const path = join(root, "bundle.json");
|
|
const parts = ["雪", "😀", "\n"];
|
|
expect(await writeParts(parts, path, false)).toEqual({
|
|
ok: true,
|
|
value: { path, bytes: Buffer.byteLength(parts.join("")) },
|
|
});
|
|
expect(await readFile(path, "utf8")).toBe(parts.join(""));
|
|
if (process.platform !== "win32")
|
|
expect((await stat(path)).mode & 0o777).toBe(0o600);
|
|
});
|
|
it("allows only one simultaneous export without overwrite approval", async () => {
|
|
const root = await createTestTempDirectory("rea-evidence-exclusive-");
|
|
const path = join(root, "bundle.json");
|
|
const candidates = [bundle(), bundle(false)];
|
|
const results = await Promise.all(
|
|
candidates.map((value) => writeBundle(value, path, false)),
|
|
);
|
|
expect(results.filter(({ ok }) => ok)).toHaveLength(1);
|
|
expect(results.filter(({ ok }) => !ok)).toMatchObject([
|
|
{ ok: false, error: { _tag: "EvidenceFileError", reason: "exists" } },
|
|
]);
|
|
const winner = candidates[results.findIndex(({ ok }) => ok)];
|
|
expect(await readEvidenceBundle(path)).toEqual({
|
|
ok: true,
|
|
value: winner,
|
|
});
|
|
expect(await readdir(root)).toEqual(["bundle.json"]);
|
|
});
|
|
|
|
it("round trips caller-selected paths without configured roots", async () => {
|
|
const root = await createTestTempDirectory("rea-evidence-dot-child-");
|
|
const directory = join(root, "..cache");
|
|
await mkdir(directory);
|
|
const path = join(directory, "bundle.json");
|
|
const value = bundle();
|
|
expect(await writeBundle(value, path, false)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(await readEvidenceBundle(path)).toEqual({
|
|
ok: true,
|
|
value,
|
|
});
|
|
});
|
|
});
|
|
|
|
it("reuses authenticated file snapshots while isolating mutable public reads", async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-owned-");
|
|
const path = join(directory, "bundle.json");
|
|
const evidence = createEvidence(
|
|
{
|
|
path: "/local/observed-target",
|
|
sha256: "a".repeat(64),
|
|
format: "file",
|
|
},
|
|
{ id: "fixture", name: "Fixture", version: "1" },
|
|
{ operation: "health", parameters: {}, result: { nested: [true] } },
|
|
);
|
|
const source = serializeEvidenceBundle(createEvidenceBundle([evidence]));
|
|
await writeFile(path, source);
|
|
const mutable = await readEvidenceBundle(path);
|
|
if (!mutable.ok) throw mutable.error;
|
|
expect(Object.isFrozen(mutable.value.records[0])).toBe(false);
|
|
const loaded = await readImmutableEvidenceBundle(path);
|
|
if (!loaded.ok) throw loaded.error;
|
|
const record = loaded.value.records[0];
|
|
if (record === undefined) throw new Error("Expected an imported record");
|
|
expect(record).toEqual(evidence);
|
|
expect(Object.isFrozen(loaded.value)).toBe(true);
|
|
expect(Object.isFrozen(record.normalized_result)).toBe(true);
|
|
expect(Reflect.set(record, "operation", "forged")).toBe(false);
|
|
|
|
const ledger = new EvidenceLedger();
|
|
const imported = ledger.import(loaded.value);
|
|
if (!imported.ok) throw imported.error;
|
|
expect(imported.value).toMatchObject({ recordsAdded: 1, changed: true });
|
|
expect(ledger.forAnalysis(evidence.evidence_id)).toBe(record);
|
|
const detached = ledger.get(evidence.evidence_id);
|
|
if (detached === undefined) throw new Error("Expected a detached record");
|
|
expect(Reflect.set(detached, "normalized_result", false)).toBe(true);
|
|
expect(ledger.record(detached).ok).toBe(false);
|
|
expect(ledger.export()).toEqual(loaded.value);
|
|
expect(await readFile(path, "utf8")).toBe(source);
|
|
ledger.clear();
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"rejects a regular-file parent traversal without writing to its lexical sibling",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-parent-");
|
|
const blocker = join(directory, "file");
|
|
await writeFile(blocker, "unchanged");
|
|
const selected = `${blocker}/../bundle.json`;
|
|
expect(await writeBundle(bundle(), selected, false)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", path: selected },
|
|
});
|
|
expect(await readdir(directory)).toEqual(["file"]);
|
|
expect(await readFile(blocker, "utf8")).toBe("unchanged");
|
|
const corrected = join(directory, "bundle.json");
|
|
expect(await writeBundle(bundle(), corrected, false)).toMatchObject({
|
|
ok: true,
|
|
value: { path: corrected },
|
|
});
|
|
expect(await readEvidenceBundle(corrected)).toEqual({
|
|
ok: true,
|
|
value: bundle(),
|
|
});
|
|
},
|
|
);
|
|
|
|
describe("evidence bundle process-capture payloads", () => {
|
|
it.each([false, true])(
|
|
"rejects semantically invalid capture export before publication (overwrite: %s)",
|
|
async (overwrite) => {
|
|
const directory = await createTestTempDirectory(
|
|
"rea-invalid-capture-bundle-",
|
|
);
|
|
const path = join(directory, "bundle.json");
|
|
if (overwrite) await writeFile(path, "original");
|
|
// Authenticate the enclosing Evidence correctly while breaking its
|
|
// capture commitment, so generic bundle authentication still succeeds.
|
|
const capture = EMPTY_PROCESS_CAPTURE_EXAMPLE;
|
|
const record = createEvidence(
|
|
unknownTarget("not-observed"),
|
|
PROCESS_PROVIDER,
|
|
{
|
|
predicateType: "rea.process-capture",
|
|
operation: "capture_process_scenario",
|
|
parameters: {},
|
|
result: {
|
|
...capture,
|
|
manifest: {
|
|
...capture.manifest,
|
|
full_scenario_sha256: "0".repeat(64),
|
|
},
|
|
},
|
|
},
|
|
);
|
|
const value = createEvidenceBundle([record]);
|
|
const rejected = await writeBundle(value, path, overwrite);
|
|
if (rejected.ok) throw new Error("Expected invalid capture rejection");
|
|
expect(rejected.error).toMatchObject({
|
|
_tag: "EvidenceIntegrityError",
|
|
userMessage: expect.stringContaining(
|
|
`Process capture record ${record.evidence_id} has an invalid normalized_result`,
|
|
),
|
|
});
|
|
expect(await readdir(directory)).toEqual(
|
|
overwrite ? ["bundle.json"] : [],
|
|
);
|
|
if (overwrite) expect(await readFile(path, "utf8")).toBe("original");
|
|
// Existing files and inline authentication use the same rejection facts.
|
|
await writeFile(path, serializeEvidenceBundle(value));
|
|
for (const read of [readEvidenceBundle, readImmutableEvidenceBundle]) {
|
|
const loaded = await read(path);
|
|
if (loaded.ok) throw new Error("Expected invalid capture rejection");
|
|
expect(projectAnalysisError(loaded.error)).toEqual(
|
|
projectAnalysisError(rejected.error),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{
|
|
label: "current provider",
|
|
provider: PROCESS_PROVIDER,
|
|
operation: "capture_process_scenario",
|
|
predicateType: "rea.process-capture",
|
|
result: EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
|
},
|
|
{
|
|
label: "historical provider name",
|
|
provider: {
|
|
...PROCESS_PROVIDER,
|
|
name: "REA deterministic process harness",
|
|
},
|
|
operation: "capture_process_scenario",
|
|
predicateType: "rea.process-capture",
|
|
result: EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
|
},
|
|
{
|
|
label: "foreign provider and operation",
|
|
provider: { id: "foreign", name: "Foreign capture", version: "1" },
|
|
operation: "historical_capture",
|
|
predicateType: "rea.process-capture",
|
|
result: EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
|
},
|
|
{
|
|
label: "unrelated predicate",
|
|
provider: PROCESS_PROVIDER,
|
|
operation: "capture_process_scenario",
|
|
predicateType: "foreign.payload",
|
|
result: {},
|
|
},
|
|
])(
|
|
"round trips capture-related records without narrowing their identities ($label)",
|
|
async ({ provider, operation, predicateType, result }) => {
|
|
const directory = await createTestTempDirectory(
|
|
"rea-capture-bundle-identity-",
|
|
);
|
|
const path = join(directory, "bundle.json");
|
|
const value = createEvidenceBundle([
|
|
createEvidence(unknownTarget("not-observed"), provider, {
|
|
operation,
|
|
predicateType,
|
|
result,
|
|
parameters: {},
|
|
}),
|
|
]);
|
|
expect(await writeBundle(value, path, false)).toMatchObject({ ok: true });
|
|
for (const read of [readEvidenceBundle, readImmutableEvidenceBundle])
|
|
expect(await read(path)).toEqual({ ok: true, value });
|
|
},
|
|
);
|
|
});
|
|
|
|
it.each([true, false])(
|
|
"refuses symlink exports in both overwrite modes (target exists: %s)",
|
|
async (targetExists) => {
|
|
const directory = await createTestTempDirectory("rea-evidence-link-");
|
|
const outside = join(directory, "outside");
|
|
await mkdir(outside);
|
|
const outsidePath = join(outside, "bundle.json");
|
|
const encoded = serializeEvidenceBundle(bundle());
|
|
if (targetExists) await writeFile(outsidePath, encoded);
|
|
const link = join(directory, "escaped.json");
|
|
await symlink(outsidePath, link);
|
|
if (targetExists)
|
|
expect(await readEvidenceBundle(link)).toMatchObject({
|
|
ok: true,
|
|
value: bundle(),
|
|
});
|
|
for (const overwrite of [false, true]) {
|
|
expect(await writeBundle(bundle(), link, overwrite)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "not-file", path: link },
|
|
});
|
|
expect((await lstat(link)).isSymbolicLink()).toBe(true);
|
|
expect((await readdir(directory)).sort()).toEqual([
|
|
"escaped.json",
|
|
"outside",
|
|
]);
|
|
expect(await readdir(outside)).toEqual(
|
|
targetExists ? ["bundle.json"] : [],
|
|
);
|
|
if (targetExists)
|
|
expect(await readFile(outsidePath, "utf8")).toBe(encoded);
|
|
}
|
|
},
|
|
);
|
|
|
|
describe("evidence bundle filesystem adapter", () => {
|
|
it.skipIf(process.platform === "win32").each(["/", "/."])(
|
|
"preserves the directory requirement of a missing output ending in %s",
|
|
async (suffix) => {
|
|
const directory = await createTestTempDirectory(
|
|
"rea-evidence-directory-",
|
|
);
|
|
const selected = `${join(directory, "report.json")}${suffix}`;
|
|
expect(await writeBundle(bundle(), selected, false)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "not-file" },
|
|
});
|
|
expect(await readdir(directory)).toEqual([]);
|
|
},
|
|
);
|
|
|
|
it("round trips canonical bytes and requires explicit overwrite", async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-");
|
|
const path = join(directory, "bundle.json");
|
|
const evidenceBundle = bundle();
|
|
const first = await writeBundle(evidenceBundle, path, false);
|
|
expect(first).toMatchObject({ ok: true, value: { path } });
|
|
expect(await readFile(path, "utf8")).toBe(
|
|
serializeEvidenceBundle(evidenceBundle),
|
|
);
|
|
expect(await readEvidenceBundle(path)).toEqual({
|
|
ok: true,
|
|
value: evidenceBundle,
|
|
});
|
|
expect(
|
|
await compareEvidenceBundlesCommand({
|
|
leftPath: path,
|
|
rightPath: path,
|
|
}),
|
|
).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
status: "unchanged",
|
|
summary: { records_unchanged: 1 },
|
|
},
|
|
});
|
|
expect(await writeBundle(evidenceBundle, path, false)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "exists" },
|
|
});
|
|
expect(await writeBundle(evidenceBundle, path, true)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
});
|
|
|
|
it("reports a missing file or output directory with the selected path", async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-");
|
|
const absent = join(directory, "absent.json");
|
|
const read = await readEvidenceBundle(absent);
|
|
expect(read).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "missing", path: absent },
|
|
});
|
|
if (read.ok) throw new Error("Expected a missing-file failure");
|
|
expect(projectAnalysisError(read.error)).toMatchObject({
|
|
message: expect.stringContaining("does not exist at the selected path"),
|
|
details: { operation: "read", reason: "missing", path: absent },
|
|
});
|
|
|
|
const orphan = join(directory, "absent", "bundle.json");
|
|
const written = await writeBundle(bundle(), orphan, false);
|
|
expect(written).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "missing", path: orphan },
|
|
});
|
|
if (written.ok) throw new Error("Expected a missing-directory failure");
|
|
expect(projectAnalysisError(written.error).message).toContain(
|
|
"output directory does not exist",
|
|
);
|
|
});
|
|
|
|
it.each([readEvidenceBundle, readImmutableEvidenceBundle])(
|
|
"rejects malformed and tampered input (%s)",
|
|
async (readBundle) => {
|
|
const directory = await createTestTempDirectory("rea-evidence-");
|
|
const malformed = join(directory, "malformed.json");
|
|
await writeFile(malformed, "{");
|
|
expect(await readBundle(malformed)).toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "EvidenceFileError", reason: "invalid-json" },
|
|
});
|
|
|
|
const tampered = bundle();
|
|
const tamperedPath = join(directory, "tampered.json");
|
|
await writeFile(
|
|
tamperedPath,
|
|
JSON.stringify({
|
|
...tampered,
|
|
records: [{ ...tampered.records[0], normalized_result: "changed" }],
|
|
}),
|
|
);
|
|
expect(await readBundle(tamperedPath)).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "EvidenceIntegrityError",
|
|
userMessage: expect.stringContaining(
|
|
"at records.0.evidence_id: Evidence semantic identifier does not match its record",
|
|
),
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("names schema mismatches that prevent bundle parsing", async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-");
|
|
const partial = join(directory, "partial.json");
|
|
await writeFile(partial, JSON.stringify({ evidence_id: "typo" }));
|
|
const rejected = await readEvidenceBundle(partial);
|
|
expect(rejected).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
userMessage: expect.stringContaining(
|
|
"does not match the bundle schema",
|
|
),
|
|
},
|
|
});
|
|
expect(rejected).toMatchObject({
|
|
error: { _tag: "EvidenceIntegrityError" },
|
|
});
|
|
|
|
const mismatched = join(directory, "mismatched.json");
|
|
await writeFile(mismatched, JSON.stringify({ ...bundle(), records: {} }));
|
|
expect(await readEvidenceBundle(mismatched)).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "EvidenceIntegrityError",
|
|
userMessage: expect.stringContaining(
|
|
"does not match the bundle schema at records:",
|
|
),
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("standalone Evidence bundle inputs", () => {
|
|
it("classifies a record only after authenticating it", async () => {
|
|
const directory = await createTestTempDirectory("rea-evidence-");
|
|
const record = bundle().records[0];
|
|
const single = join(directory, "record.json");
|
|
await writeFile(single, JSON.stringify(record));
|
|
expect(await readEvidenceBundle(single)).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "EvidenceFileError",
|
|
reason: "wrong-kind",
|
|
path: single,
|
|
detectedKind: "evidence-record",
|
|
userMessage: expect.stringContaining(String(record?.evidence_id)),
|
|
},
|
|
});
|
|
|
|
const tamperedRecord = join(directory, "tampered-record.json");
|
|
await writeFile(
|
|
tamperedRecord,
|
|
JSON.stringify({ ...record, normalized_result: "changed" }),
|
|
);
|
|
expect(await readEvidenceBundle(tamperedRecord)).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "EvidenceIntegrityError",
|
|
userMessage: expect.stringContaining(
|
|
"does not match the bundle schema",
|
|
),
|
|
},
|
|
});
|
|
});
|
|
});
|