Files
reverse-engineering-anything/tests/boundary/filesystem/undeliveredSourceClosure.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

780 lines
27 KiB
TypeScript

import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import { readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import type { UndeliveredSourceClosure } from "../../../src/application/AnalysisEvidenceSources.js";
import { runDirectAnalysis } from "../../../src/application/DirectAnalysis.js";
import {
deliverCliAnalysisEvidence,
publishCliAnalysisEvidence,
} from "../../../src/cli/analysisEvidenceDelivery.js";
import {
AnalysisCancelledError,
AnalysisOutputError,
} from "../../../src/domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { createArtifactInspection } from "../../../src/domain/artifactInspection.js";
import {
createEvidence,
parseEvidence,
type Evidence,
} from "../../../src/domain/evidence.js";
import { parseEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
import { jsonByteLength } from "../../../src/domain/jsonSerialization.js";
import type { JsonValue } from "../../../src/domain/jsonValue.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
import { err, ok } from "../../../src/domain/result.js";
import { OwnedStagedOutput } from "../../../src/filesystem/OwnedStagedOutput.js";
import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
import { composedWorkflowFixture } from "../../fixtures/composedWorkflowEvidence.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const provider = { id: "fixture", name: "Fixture", version: "1" };
const source = (name: string, links: readonly string[] = []) =>
createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_source",
parameters: { name },
result: { name },
evidenceLinks: [...links],
});
const observed = (links: readonly string[]) =>
createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_observation",
parameters: {},
result: { completed: true },
evidenceLinks: [...links],
});
const cancelledWith = (observation: unknown): JsonValue =>
projectAnalysisError(
new AnalysisCancelledError("fixture_observation", {
partialObservation: observation as Evidence,
}),
"invocation",
);
const delivery = (root: string, sources?: readonly Evidence[]) => ({
providerId: "fixture",
operation: "fixture_observation",
signal: new AbortController().signal,
detail: "complete" as const,
evidenceOutput: join(root, "evidence.json"),
...(sources === undefined ? {} : { sourceEvidence: sources }),
});
const closureOf = (output: unknown) =>
(output as { details: { source_closure: UndeliveredSourceClosure } }).details
.source_closure;
it("reports never-captured links as explicit omissions without fabricating records", async () => {
const root = await createTestTempDirectory("rea-closure-missing-");
const held = source("held", ["ev_".padEnd(67, "a")]);
const neverCaptured = "ev_".padEnd(67, "b");
const observation = observed([held.evidence_id, neverCaptured]);
const closure = closureOf(
await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
delivery(root, [held]),
),
);
expect(closure.records.map((record) => record.evidence_id)).toEqual([
held.evidence_id,
]);
expect(parseEvidence(closure.records[0])).toEqual(held);
// The ancestor of a held record is unresolved here, not absent.
expect(closure.omitted).toEqual([
{ evidence_id: neverCaptured, reason: "not_captured" },
{ evidence_id: "ev_".padEnd(67, "a"), reason: "not_captured" },
]);
expect(closure).toMatchObject({
publication: "not_published",
complete: false,
omitted_count: 2,
});
expect(await readdir(root)).toEqual([]);
});
it("reports an unlinked captured source and stops at nothing linked or captured", async () => {
const root = await createTestTempDirectory("rea-closure-unlinked-");
const selected = source("selected");
const observation = observed([]);
expect(
closureOf(
await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
delivery(root, [selected]),
),
).records,
).toEqual([selected]);
const failure = cancelledWith(observation);
expect(
await publishCliAnalysisEvidence(err(failure), delivery(root, [])),
).toBe(failure);
// A self-reference is the observation, not one of its sources.
expect(
await publishCliAnalysisEvidence(
err(failure),
delivery(root, [observation]),
),
).toBe(failure);
});
it("leaves failures untouched unless a completed observation requested delivery", async () => {
const root = await createTestTempDirectory("rea-closure-untouched-");
const held = source("held");
const observation = observed([held.evidence_id]);
const requested = delivery(root, [held]);
// Without a selected output no source closure was requested on success either.
const noOutput = cancelledWith(observation);
expect(
await publishCliAnalysisEvidence(err(noOutput), {
...requested,
evidenceOutput: undefined,
}),
).toBe(noOutput);
const noObservation = projectAnalysisError(
new AnalysisCancelledError("fixture_observation"),
"invocation",
);
expect(await publishCliAnalysisEvidence(err(noObservation), requested)).toBe(
noObservation,
);
const notEvidence = cancelledWith({ kind: "file-snapshot-read" });
expect(await publishCliAnalysisEvidence(err(notEvidence), requested)).toBe(
notEvidence,
);
// A failed authentication cannot select which sources an observation cites.
const altered = cancelledWith({
...observation,
evidence_links: [],
normalized_result: { completed: false },
});
expect(await publishCliAnalysisEvidence(err(altered), requested)).toBe(
altered,
);
});
it("bounds records and listed omissions while counting everything", async () => {
const root = await createTestTempDirectory("rea-closure-bounds-");
const held = Array.from({ length: 200 }, (_, index) =>
createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_source",
parameters: { index },
result: { text: "x".repeat(4 * 1024) },
}),
);
const missing = Array.from({ length: 100 }, (_, index) =>
`ev_${index.toString(16).padStart(4, "0")}`.padEnd(67, "c"),
);
const observation = observed([
...held.map((record) => record.evidence_id),
...missing,
]);
const closure = closureOf(
await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
delivery(root, held),
),
);
expect(closure.complete).toBe(false);
expect(closure.records.length).toBeGreaterThan(0);
expect(closure.records.length).toBeLessThan(held.length);
const expectedOmissions = held.length + missing.length;
expect(closure.omitted_count).toBe(
expectedOmissions - closure.records.length,
);
expect(closure.omitted).toHaveLength(64);
expect(jsonByteLength(closure)).toBeLessThan(80 * 1024);
});
/** Above V8's default call-argument limit of about 125,000 on a forked main thread. */
const DATA_SIZED_LINKS = 200_000;
const digestLink = (index: number) =>
`ev_${index.toString(16).padStart(64, "0")}`;
const wideSource = (links: readonly string[]) =>
createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_source",
parameters: {},
result: { wide: true },
evidenceLinks: links,
});
it.each(["final-close cancellation", "missing sources"] as const)(
"keeps the original failure when a captured source has a data-sized link set: %s",
async (failure) => {
const root = await createTestTempDirectory("rea-closure-wide-");
const links = Array.from({ length: DATA_SIZED_LINKS }, (_, index) =>
digestLink(index),
);
const wide = wideSource(links);
const observation = observed([wide.evidence_id]);
const requested = delivery(root, [wide]);
const output = await publishCliAnalysisEvidence(
failure === "missing sources"
? ok(observation)
: err(cancelledWith(observation)),
requested,
);
expect(output).toMatchObject(
failure === "missing sources"
? {
code: "evidence_integrity_mismatch",
details: { reason: "missing" },
}
: { code: "cancelled" },
);
const closure = closureOf(output);
expect(closure.records).toEqual([]);
expect(closure.omitted_count).toBe(DATA_SIZED_LINKS + 1);
expect(closure.omitted).toHaveLength(64);
expect(closure.omitted[0]).toEqual({
evidence_id: wide.evidence_id,
reason: "not_inlined",
});
expect(jsonByteLength(closure)).toBeLessThan(16 * 1024);
},
);
it("reports a data-sized link set cited by the observation itself", async () => {
const root = await createTestTempDirectory("rea-closure-wide-observation-");
const closure = closureOf(
await publishCliAnalysisEvidence(
err(
cancelledWith(
observed(
Array.from({ length: DATA_SIZED_LINKS }, (_, index) =>
digestLink(index),
),
),
),
),
delivery(root),
),
);
expect(closure.omitted_count).toBe(DATA_SIZED_LINKS);
expect(closure.omitted).toHaveLength(64);
});
it("examines repeated links once across every captured source", async () => {
const root = await createTestTempDirectory("rea-closure-dedupe-");
const shared = source("shared");
const repeated = Array.from(
{ length: DATA_SIZED_LINKS },
() => shared.evidence_id,
);
const first = wideSource(repeated);
const second = createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_source",
parameters: { second: true },
result: { wide: true },
evidenceLinks: repeated,
});
const closure = closureOf(
await publishCliAnalysisEvidence(
err(cancelledWith(observed([first.evidence_id, second.evidence_id]))),
delivery(root, [first, second, shared]),
),
);
expect(closure.records).toEqual([shared]);
expect(closure.omitted_count).toBe(2);
});
it.each([
["a captured source", "captured"],
["the completed observation", "observation"],
] as const)(
"keeps the original failure when examining %s throws",
async (_, thrower) => {
const root = await createTestTempDirectory("rea-closure-throws-");
const held = source("held");
const failing = (record: Evidence) =>
Object.defineProperty({ ...record }, "evidence_links", {
enumerable: true,
get: () => {
throw new RangeError("quoted analyzed-value text");
},
}) as Evidence;
const observation = observed([held.evidence_id]);
const projected = cancelledWith(observation) as {
details: Record<string, JsonValue>;
};
const partial =
thrower === "observation"
? (failing(observation) as unknown as JsonValue)
: projected.details.partial_observation!;
const failure = {
...projected,
details: { ...projected.details, partial_observation: partial },
} as JsonValue;
const output = (await publishCliAnalysisEvidence(
err(failure),
delivery(root, thrower === "captured" ? [failing(held)] : [held]),
)) as {
code: string;
message: string;
details: Record<string, unknown>;
};
expect(output.code).toBe("cancelled");
expect(output.message).toBe((failure as { message: string }).message);
expect(output.details.partial_observation).toBe(partial);
expect(output.details.operation).toBe("fixture_observation");
expect(output.details.source_closure).toEqual({
publication: "not_published",
complete: false,
collection_failure: "RangeError",
recovery: expect.any(String),
});
expect(JSON.stringify(output.details.source_closure)).not.toContain(
"quoted analyzed-value text",
);
expect(await readdir(root)).toEqual([]);
},
);
it("spends the allowance on smaller later records after a record does not fit", async () => {
const root = await createTestTempDirectory("rea-closure-greedy-");
const large = createEvidence(unknownTarget("not-observed"), provider, {
operation: "fixture_source",
parameters: {},
result: { text: "x".repeat(128 * 1024) },
});
const small = source("small");
const closure = closureOf(
await publishCliAnalysisEvidence(
err(
cancelledWith(observed([large.evidence_id, small.evidence_id].sort())),
),
delivery(root, [large, small]),
),
);
expect(closure.records.map((record) => record.evidence_id)).toEqual([
small.evidence_id,
]);
expect(closure.omitted).toEqual([
{ evidence_id: large.evidence_id, reason: "not_inlined" },
]);
});
it("keeps captured records out of error output under explicit exclusions", async () => {
const root = await createTestTempDirectory("rea-closure-sensitive-");
const held = source("literal-secret-value");
const output = await publishCliAnalysisEvidence(
err(cancelledWith(observed([held.evidence_id]))),
{ ...delivery(root, [held]), sensitiveValues: ["literal-secret-value"] },
);
const closure = closureOf(output);
expect(closure.records).toEqual([]);
expect(closure.omitted).toEqual([
{ evidence_id: held.evidence_id, reason: "not_inlined" },
]);
expect(JSON.stringify(closure)).not.toContain("literal-secret-value");
});
it("does not duplicate sources an artifact inspection already embeds", async () => {
const root = await createTestTempDirectory("rea-closure-embedded-");
const input = join(root, "input.txt");
await writeFile(input, "artifact contents");
const embedded = createEvidence(unknownTarget("not-observed"), provider, {
operation: "inventory_artifact",
parameters: {},
result: await inventoryArtifact(input),
});
const observation = createEvidence(unknownTarget("not-observed"), provider, {
operation: "inspect_artifact",
parameters: {},
result: createArtifactInspection(embedded),
evidenceLinks: [embedded.evidence_id],
});
const output = await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
{ ...delivery(root), evidenceOutput: join(root, "bundle.json") },
);
expect(closureOf(output)).toMatchObject({
publication: "not_published",
complete: true,
records: [],
omitted_count: 0,
});
});
it("reports the closure when cancellation precedes bundle commit", async () => {
const root = await createTestTempDirectory("rea-closure-precommit-");
const held = source("held");
const observation = observed([held.evidence_id]);
const controller = new AbortController();
controller.abort(new Error("cancelled before publication"));
const output = await publishCliAnalysisEvidence(ok(observation), {
...delivery(root, [held]),
signal: controller.signal,
});
expect(output).toMatchObject({ code: "cancelled" });
expect(closureOf(output).records).toEqual([held]);
expect(await readdir(root)).toEqual([]);
});
it("reports the closure when an existing output refuses publication", async () => {
const root = await createTestTempDirectory("rea-closure-exists-");
const held = source("held");
const requested = delivery(root, [held]);
await writeFile(requested.evidenceOutput, "original");
const output = await publishCliAnalysisEvidence(
ok(observed([held.evidence_id])),
requested,
);
expect(output).toMatchObject({ details: { reason: "exists" } });
expect(closureOf(output).records).toEqual([held]);
expect(await readFile(requested.evidenceOutput, "utf8")).toBe("original");
});
it("reports result sources when an existing output refuses publication", async () => {
const root = await createTestTempDirectory("rea-closure-result-sources-");
const held = source("held");
const requested = delivery(root);
await writeFile(requested.evidenceOutput, "original");
const output = await publishCliAnalysisEvidence(
{ ...ok(observed([held.evidence_id])), sourceEvidence: [held] },
requested,
);
expect(output).toMatchObject({ details: { reason: "exists" } });
expect(closureOf(output)).toMatchObject({
publication: "not_published",
complete: true,
omitted_count: 0,
omitted: [],
});
expect(closureOf(output).records).toEqual([held]);
expect(await readFile(requested.evidenceOutput, "utf8")).toBe("original");
});
it("defers to the committed bundle receipt after cancellation wins late", async () => {
const root = await createTestTempDirectory("rea-closure-committed-");
const held = source("held");
const controller = new AbortController();
const close = OwnedStagedOutput.prototype.close;
const seam = vi
.spyOn(OwnedStagedOutput.prototype, "close")
.mockImplementationOnce(async function (this: OwnedStagedOutput) {
await close.call(this);
controller.abort(new Error("cancelled after commit"));
});
const requested = {
...delivery(root, [held]),
signal: controller.signal,
};
let output: JsonValue;
try {
output = await publishCliAnalysisEvidence(
ok(observed([held.evidence_id])),
requested,
);
} finally {
seam.mockRestore();
}
expect(output).toMatchObject({
code: "cancelled",
details: {
diagnostics: { evidence_bundle: { path: requested.evidenceOutput } },
},
});
expect(output).not.toHaveProperty("details.source_closure");
});
it("defers to the published file when staging cleanup stays incomplete after commit", async () => {
const root = await createTestTempDirectory("rea-closure-published-");
const held = source("held");
const requested = delivery(root, [held]);
const close = OwnedStagedOutput.prototype.close;
const owners = new Set<OwnedStagedOutput>();
const seam = vi
.spyOn(OwnedStagedOutput.prototype, "close")
.mockImplementation(async function (this: OwnedStagedOutput) {
owners.add(this);
throw new Error("controlled staging cleanup failure");
});
let output: JsonValue;
try {
output = await publishCliAnalysisEvidence(
ok(observed([held.evidence_id])),
requested,
);
} finally {
seam.mockRestore();
for (const owner of owners) await close.call(owner);
}
expect(output).toMatchObject({
code: "cleanup_incomplete",
details: { published_path: requested.evidenceOutput },
});
expect(output).not.toHaveProperty("details.source_closure");
});
it("does not change ordinary successful delivery", async () => {
const root = await createTestTempDirectory("rea-closure-success-");
const held = source("held");
const observation = observed([held.evidence_id]);
const requested = delivery(root, [held]);
const delivered = await publishCliAnalysisEvidence(
ok(observation),
requested,
);
expect(JSON.stringify(delivered)).toBe(JSON.stringify(observation));
const withoutOutput = await publishCliAnalysisEvidence(ok(observation), {
...requested,
evidenceOutput: undefined,
});
expect(JSON.stringify(withoutOutput)).toBe(JSON.stringify(observation));
});
it.each(["interrupt", "cleanup-failure", "interrupt-cleanup-failure"] as const)(
"states the sources a production direct analysis never captured: %s",
async (scenario) => {
const workflow = await composedWorkflowFixture();
const controller = new AbortController();
const close = workflow.session.close.bind(workflow.session);
workflow.session.close = async () => {
// The fixture's own final close retries this owner after the command.
workflow.session.close = close;
if (scenario !== "cleanup-failure") controller.abort();
const closed = await close();
if (scenario === "interrupt" || !closed.ok) return closed;
return err(
new ProviderCleanupError(
"fixture",
["owned-root"],
{ reason: "controlled session close failure" },
{ operation: "close_binary" },
),
);
};
const root = await createTestTempDirectory("rea-closure-direct-");
const complete = await runDirectAnalysis(
workflow.dependencies,
workflow.path,
"binary_overview",
{},
{ signal: controller.signal },
);
const output = await publishCliAnalysisEvidence(complete, {
...delivery(root),
signal: controller.signal,
});
const failure = output as {
code: string;
details: { partial_observation: unknown };
};
expect(failure.code).toBe(
scenario === "interrupt" ? "cancelled" : "cleanup_incomplete",
);
const links = parseEvidence(
failure.details.partial_observation,
).evidence_links;
expect(links.length).toBeGreaterThan(0);
expect(closureOf(output)).toMatchObject({
publication: "not_published",
complete: false,
records: [],
omitted_count: links.length,
omitted: links.map((evidence_id) => ({
evidence_id,
reason: "not_captured",
})),
});
expect(await readdir(root)).toEqual([]);
},
);
it.each(["interrupt", "cleanup-failure"] as const)(
"preserves captured sources when direct finalization fails: %s",
async (scenario) => {
const workflow = await composedWorkflowFixture();
const controller = new AbortController();
const close = workflow.session.close.bind(workflow.session);
workflow.session.close = async () => {
workflow.session.close = close;
if (scenario === "interrupt") controller.abort();
const closed = await close();
if (scenario === "interrupt" || !closed.ok) return closed;
return err(
new ProviderCleanupError(
"fixture",
["owned-root"],
{ reason: "controlled session close failure" },
{ operation: "close_binary" },
),
);
};
const root = await createTestTempDirectory("rea-closure-direct-captured-");
const complete = await runDirectAnalysis(
workflow.dependencies,
workflow.path,
"binary_overview",
{},
{ signal: controller.signal, includeEvidenceSources: true },
);
const output = await publishCliAnalysisEvidence(complete, {
...delivery(root),
signal: controller.signal,
});
const failure = output as {
code: string;
details: { partial_observation: unknown };
};
expect(failure.code).toBe(
scenario === "interrupt" ? "cancelled" : "cleanup_incomplete",
);
const links = parseEvidence(
failure.details.partial_observation,
).evidence_links;
expect(links.length).toBeGreaterThan(0);
if (scenario === "cleanup-failure") {
const path = join(root, "evidence.json");
expect(output).toMatchObject({
details: { diagnostics: { evidence_bundle: { path } } },
});
expect(output).not.toHaveProperty("details.source_closure");
const bundle = parseEvidenceBundle(
JSON.parse(await readFile(path, "utf8")),
);
expect(bundle.records.map((record) => record.evidence_id).sort()).toEqual(
[
parseEvidence(failure.details.partial_observation).evidence_id,
...links,
].sort(),
);
return;
}
expect(closureOf(output)).toMatchObject({
publication: "not_published",
complete: true,
omitted_count: 0,
});
expect(
closureOf(output)
.records.map((record) => record.evidence_id)
.sort(),
).toEqual([...links].sort());
expect(await readdir(root)).toEqual([]);
},
);
it("reports through the shared one-shot delivery when cancellation arrives during close", async () => {
const root = await createTestTempDirectory("rea-closure-deliver-");
const held = source("held");
const uncaptured = "ev_".padEnd(67, "d");
const observation = observed([held.evidence_id, uncaptured]);
const controller = new AbortController();
const output = await deliverCliAnalysisEvidence({
...delivery(root, [held]),
signal: controller.signal,
run: async () => ok(observation),
service: {
close: async () => {
controller.abort(new Error("cancelled during final close"));
},
},
});
expect(output).toMatchObject({
code: "cancelled",
details: { partial_observation: { evidence_id: observation.evidence_id } },
});
expect(closureOf(output)).toMatchObject({
complete: false,
records: [held],
omitted: [{ evidence_id: uncaptured, reason: "not_captured" }],
});
expect(await readdir(root)).toEqual([]);
});
it("reports the closure when delivered view projection refuses a completed observation", async () => {
const root = await createTestTempDirectory("rea-closure-view-");
const held = source("held");
const observation = observed([held.evidence_id]);
const output = await publishCliAnalysisEvidence(ok(observation), {
...delivery(root, [held]),
detail: "summary",
// A view applying to another provider's facts cannot be selected here.
view: { kind: "native", facet: "pseudocode", offset: 0, limit: 1 },
});
expect(closureOf(output).records).toEqual([held]);
expect(await readdir(root)).toEqual([]);
});
it("leaves a primary failure without a completed observation untouched", async () => {
const root = await createTestTempDirectory("rea-closure-primary-");
const held = source("held");
const primary = projectAnalysisError(
new AnalysisOutputError("fixture_observation", "primary reply failure"),
"invocation",
);
expect(
await publishCliAnalysisEvidence(err(primary), delivery(root, [held])),
).toBe(primary);
});
it.each([false, true])(
"keeps source closure unknown when embedded inspection sources cannot be read (captured link: %s)",
async (captured) => {
const root = await createTestTempDirectory(
"rea-closure-invalid-inspection-",
);
const held = source("held");
const observation = createEvidence(
unknownTarget("not-observed"),
provider,
{
operation: "inspect_artifact",
parameters: {},
result: { malformed: true },
evidenceLinks: captured ? [held.evidence_id] : [],
},
);
const output = await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
delivery(root, captured ? [held] : []),
);
expect(output).toMatchObject({
details: {
source_closure: {
publication: "not_published",
complete: false,
collection_failure: expect.any(String),
},
},
});
expect(closureOf(output).records).toEqual(captured ? [held] : []);
expect(await readdir(root)).toEqual([]);
},
);
it("reports missing links as unknown when an embedded source list cannot be read", async () => {
const root = await createTestTempDirectory("rea-closure-invalid-missing-");
const missing = "ev_".padEnd(67, "a");
const observation = createEvidence(unknownTarget("not-observed"), provider, {
operation: "inspect_artifact",
parameters: {},
result: { malformed: true },
evidenceLinks: [missing],
});
const output = await publishCliAnalysisEvidence(
err(cancelledWith(observation)),
delivery(root, []),
);
expect(closureOf(output)).toMatchObject({
complete: false,
collection_failure: expect.any(String),
omitted_count: 1,
omitted: [{ evidence_id: missing, reason: "not_captured" }],
});
});