Files
reverse-engineering-anything/src/cliLoader.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

120 lines
4.2 KiB
TypeScript

import type { CliGlobalArguments } from "./cliBuiltinArguments.js";
import { RECORDED_CLI_COMMAND_REGISTRATIONS } from "./recordedCliCommandRegistrations.js";
import {
CLI_COMMAND_REGISTRATIONS,
type CliCommandRegistrationId,
} from "./cli/cliCommandRegistrations.js";
import type { CliResultOutput } from "./cli/cliResultOutput.js";
import type { CliInstance } from "./cli/types.js";
/** Global switches that change only how a command's result is written. */
const COMMAND_OUTPUT_FLAGS: ReadonlySet<string> = new Set([
"--json",
"--full-output",
"--token-count",
"--timings",
"--no-timings",
"--no-retain",
]);
/** Global switches that inspect a single known command without listing others. */
const COMMAND_DISCOVERY_FLAGS: ReadonlySet<string> = new Set([
"--help",
"-h",
"--schema",
"--llms",
"--llms-full",
]);
const isRegistrationId = (id: string): id is CliCommandRegistrationId =>
Object.hasOwn(CLI_COMMAND_REGISTRATIONS, id);
/**
* The registrars the command these arguments run needs, or undefined when the
* complete CLI must answer, as it does for shell completion, which has no
* global arguments. A standalone version or retired per-command MCP query
* needs no registrar. A recorded top-level command's `--help`, `-h`, `--schema`,
* `--llms`, or `--llms-full` only needs that command's registrar. Root help/
* manifests, unknown names ("did you mean"), other CLI builtins, and a missing
* or unusable recording all describe or search every command.
*/
const selectCliRegistrations = (
global: CliGlobalArguments | undefined,
recorded: Readonly<Record<string, string>> | undefined,
): readonly CliCommandRegistrationId[] | undefined => {
if (global === undefined) return undefined;
const { command, flags } = global;
if (flags.has("--mcp") && command.length > 0) return [];
if (command.length === 0 && flags.size === 1 && flags.has("--version"))
return [];
if (recorded === undefined) return undefined;
for (const flag of flags)
if (
!COMMAND_OUTPUT_FLAGS.has(flag) &&
!COMMAND_DISCOVERY_FLAGS.has(flag) &&
!flag.startsWith("--timings=")
)
return undefined;
const name = command[0];
if (name === undefined || !Object.hasOwn(recorded, name)) return undefined;
const id = recorded[name]!;
return isRegistrationId(id) ? [id] : undefined;
};
/** The command runtime one invocation loaded, before its CLI is built. */
export interface CliRuntime {
createCli(resultOutput: CliResultOutput | undefined): CliInstance;
/** Closes the process-owned resources of this runtime's commands. */
closeCliResources(): Promise<void>;
}
/**
* Load the command runtime for one invocation. A recorded top-level command
* loads only its registrar's module graph. A standalone version or retired
* mode query loads none; anything else loads the complete CLI.
*/
export const loadCliRuntime = async (
global: CliGlobalArguments | undefined,
environment: NodeJS.ProcessEnv,
savedDefaults: ReadonlySet<string> | undefined,
): Promise<CliRuntime> => {
const ids = selectCliRegistrations(
global,
RECORDED_CLI_COMMAND_REGISTRATIONS,
);
if (ids === undefined) {
const { createCli, closeCliResources } = await import("./cli.js");
return {
createCli: (resultOutput) =>
createCli(environment, resultOutput, undefined, savedDefaults),
closeCliResources,
};
}
const selected = ids.map((id) => CLI_COMMAND_REGISTRATIONS[id]);
const [modules, { createCliShell }, { closeCliResources }, analysis] =
await Promise.all([
Promise.all(selected.map((registration) => registration.load())),
import("./cli/cliShell.js"),
import("./cli/cliResources.js"),
selected.some((registration) => registration.analysis)
? import("./composition/directAnalysis.js")
: undefined,
]);
return {
createCli: (resultOutput) =>
createCliShell({
selectedEnvironment: environment,
savedDefaults,
resultOutput,
registrations: selected.map((registration, index) => ({
registration,
module: modules[index],
})),
...(analysis === undefined
? {}
: { createAnalysis: analysis.createDirectAnalysis }),
}),
closeCliResources,
};
};