mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
* fix(cli): apply the retained Evidence cache cap when reusing bundles * fix(cli): preserve earlier evidence receipts on rejected reuse --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
70 lines
2.6 KiB
TypeScript
70 lines
2.6 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 only the receipt owned by a publication whose file was discarded. */
|
|
export const clearCliEvidencePublication = (
|
|
expected: PublishedEvidenceReceipt,
|
|
): void => {
|
|
const context = publication.getStore();
|
|
if (context?.receipt === expected) 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 } });
|
|
};
|