Files
reverse-engineering-anything/src/cli/cliEvidencePublication.ts
T
morluto 2bb554e8b0 feat(cli)!: keep over-budget CLI results recoverable by default (#14610)
* feat(cli)!: keep over-budget CLI results recoverable by default

An over-budget CLI result without --evidence_output now saves its
complete Evidence to a private retained-Evidence cache under the user
cache directory and returns the summary with the file's path, size and
digest, so it pages with inspect-analysis-view without a rerun.

Files are named by the complete record's Evidence ID, created
exclusively as 0600 by the Evidence bundle writer in a 0700 directory
the user owns, reused by repeat runs and never overwritten. Each
retention removes records unused for 7 days, then the least recently
used over delivery.cli_retained_evidence_bytes (REA_CLI_RETAINED_EVIDENCE_BYTES,
default 256 MiB). --no-retain or a 0 setting writes nothing; a record
that cannot be kept falls back to the rerun arguments with the reason.

Fixes #14594

BREAKING CHANGE: An over-budget CLI result without --evidence_output is
now saved to REA's private retained-Evidence cache and reported as
{summary, evidence_bundle, delivery} with delivery.retained cache-file,
instead of writing nothing. Pass --no-retain, or set
REA_CLI_RETAINED_EVIDENCE_BYTES=0, to write nothing as before.

* fix(cli): key retained Evidence by its content and keep its owner's facts

Review fixes for default CLI retention:

- Name files by the SHA-256 of the complete record's exact JSON, since
  the Evidence ID leaves out caller paths; report it as
  evidence_bundle.sha256.
- Remove a bundle that outgrows the cache after the write.
- Renew a record's age when inspect-analysis-view reads it.
- Re-check a record's use time before evicting it.
- Never use a cache whose path holds a value marked sensitive, and
  redact retention failure reasons.
- Report a save that committed but then failed with its receipt and
  cleanup owner, as --evidence_output does.
- Request source records whenever the cache may receive a bundle.
- Refresh the advertised-schema summaries for the new setting.

* fix(cli): withdraw the receipt of a bundle the cache discards

A bundle that outgrew the cache was removed after the writer had already
recorded its publication receipt, so output recovery could name a
missing file. Retention now withdraws that receipt whenever it keeps
nothing, unless the file committed and its failure is reported.

* fix(cli): retain publication receipt when removing an oversized retained bundle fails
2026-11-25 21:39:33 +00:00

68 lines
2.5 KiB
TypeScript

import { AsyncLocalStorage } from "node:async_hooks";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { CliEvidenceRetention } from "./cliEvidenceRetention.js";
export interface PublishedEvidenceReceipt {
readonly path?: string;
readonly bytes: number;
}
interface PublicationContext {
receipt?: PublishedEvidenceReceipt;
readonly invocation?: CliPublicationInvocation;
}
const publication = new AsyncLocalStorage<PublicationContext>();
/** What one CLI invocation captured that its delivery reads. */
export interface CliPublicationInvocation {
/** REA_AGENT_RESULT_BUDGET_BYTES from the environment this invocation captured. */
readonly agentBudgetSeed?: string | undefined;
/** Whether the caller selected specific output fields with `--filter-output`. */
readonly hasFilterOutput?: boolean | undefined;
/**
* Where an over-budget result without --evidence_output is kept, from the
* environment this invocation captured; undefined retains nothing
* (--no-retain, or the setting is 0).
*/
readonly retention?: CliEvidenceRetention | undefined;
}
/** Keep publication state in the invocation that owns delivery and formatting. */
export const withCliEvidencePublication = <Value>(
run: () => Value,
invocation?: CliPublicationInvocation,
): Value =>
publication.run(invocation === undefined ? {} : { invocation }, run);
/** What this invocation captured; nothing outside one. */
export const cliPublicationInvocation = (): CliPublicationInvocation =>
publication.getStore()?.invocation ?? {};
export const recordCliEvidencePublication = (
receipt: PublishedEvidenceReceipt,
): void => {
const context = publication.getStore();
if (context !== undefined) context.receipt = receipt;
};
/** Withdraw this invocation's receipt when the file it named was discarded. */
export const clearCliEvidencePublication = (): void => {
const context = publication.getStore();
if (context !== undefined) delete context.receipt;
};
/** Snapshot a receipt for output recovery after the invocation context ends. */
export const cliEvidencePublicationReceipt = ():
| PublishedEvidenceReceipt
| undefined => publication.getStore()?.receipt;
/** Preserve an already committed file when subsequent output selection fails. */
export const retainCliPublicationOnError = <ErrorType extends AnalysisError>(
error: ErrorType,
receipt = cliEvidencePublicationReceipt(),
): ErrorType => {
return receipt === undefined
? error
: error.retainDiagnostics({ evidence_bundle: { ...receipt } });
};