mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 13:41:17 -05:00
a10037ef10
* refactor(cli): render help pages with REA's help owner REA now renders the root page, every command page, help after an unknown command, the mcp group page, the mcp doctor builtin page and the completions page before incur sees argv, with incur's precedence (update check, --update without help, --mcp and the LLM manifests first; then the integrations; then --version; then root and command pages). help.ts gains arguments, custom global options, the root-only flags and integrations, and a command-list page. The --format row on the root and command pages now lists csv and tsv (#10250), the approved difference; the mcp and mcp add pages keep the document formats they accept. Every other help byte is unchanged: 397 pages compared with incur's Help for all 196 registered commands. Fixes #10250 * test(cli): elide the root command list wherever the root page is printed The golden renderer chose root-page elision by case name, so help after an unknown command froze the whole command list. It now elides any stdout that opens with the root page's rea@<version> title. * refactor(cli): answer --version, --schema and the LLM manifests in REA src/cli/discovery.ts answers help, --version, --schema, --llms and --llms-full with incur's precedence, reading the arguments as the dispatcher receives them (after result output has adapted them, so token counts and buffering are unchanged). commandManifests.ts renders the Markdown index and full manifest, the structured manifests and the --schema document; commandSuggestion.ts ranks Did-you-mean candidates. Both are adapted from incur under its MIT licence. --schema --format md stays with incur: incur renders it without Markdown escaping, unlike REA's renderer, and that difference awaits a decision on #11654. Compared with incur's serve for every registered command and format, 3746 of 3945 discovery invocations are identical; the rest are the 196 Markdown schemas left to incur and the three root help pages with the approved csv/tsv row. * test(cli): pin which discovery document wins when several are asked for Goldens for --llms with --help, --llms with --version, --llms with --llms-full, --schema with --version and --schema with --help, recorded with incur's rendering on the base branch. * refactor(cli): report unknown commands and the update notice from REA src/cli/updateNotice.ts owns the terminal update notice: the same trigger (terminal stdout, no explicit format), text, cache file ($XDG_CACHE_HOME/incur/updates/<package>.json), daily detached refresh through rea --incur-update-check, and opt-outs (NO_UPDATE_NOTIFIER, npm_config_update_notifier=false, CI). Discovery answers the detached refresh first, and reports an unknown command with incur's Did-you-mean suggestion, corrected invocation, help command and notice, as text on a terminal or as the error document, full-output envelope, token count or token window otherwise. outputEnvelope.ts renders these dispatcher failures. Unknown commands with --format md stay with incur pending the Markdown escaping decision on #11654. Compared with incur's serve, 224 of 224 unknown-command invocations are identical, piped and on a terminal with a due notice. * fix(cli): redact unknown-command guidance before rendering Share the producing reply boundary for root lookup, unknown schemas and MCP subcommands. Preserve ordinary argument identity and existing MCP suggestion policies; omit sensitive or unsafe replay commands and redact full-output command metadata. Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --------- Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
199 lines
7.4 KiB
TypeScript
199 lines
7.4 KiB
TypeScript
import { Cli } from "incur";
|
|
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
|
|
|
|
import type { FridaInstrumentationPort } from "../ports/frida/FridaInstrumentationPort.js";
|
|
import { currentClientRegistrationCommand } from "../application/ClientRegistrationIdentity.js";
|
|
import {
|
|
adaptCliOutputArgumentsForIncur,
|
|
parseCliGlobalArguments,
|
|
} from "../cliBuiltinArguments.js";
|
|
import type { DirectAnalysis } from "../composition/directAnalysis.js";
|
|
import { parseLogLevel } from "../config/processStart.js";
|
|
import { parseClientRegistrationCommand } from "../domain/clientRegistrationCommand.js";
|
|
import { PRODUCT_IDENTITY } from "../identity.js";
|
|
import { createLogger } from "../logger.js";
|
|
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
|
import {
|
|
encodeCliArgumentErrors,
|
|
presentCliArgumentErrors,
|
|
} from "./argumentErrors.js";
|
|
import type {
|
|
CliCommandRegistration,
|
|
CliRegistrationContext,
|
|
} from "./cliCommandRegistration.js";
|
|
import {
|
|
streamCliCommandResults,
|
|
type CliResultOutput,
|
|
} from "./cliResultOutput.js";
|
|
import { completionHelpNote, createCliCompletions } from "./completions.js";
|
|
import { excludeDeclaredSensitiveArguments } from "./explicitSensitiveArguments.js";
|
|
import {
|
|
CLI_CUSTOM_GLOBAL_OPTIONS,
|
|
CLI_DESCRIPTION,
|
|
serveDiscovery,
|
|
} from "./discovery.js";
|
|
import { reportUnknownMcpSubcommand } from "./mcpBuiltinCommands.js";
|
|
import { serveMcpRegistration } from "./mcpRegistration.js";
|
|
import { presentOmittableDefaults } from "./omittableDefaults.js";
|
|
import { validateCliArguments } from "./strictPositionals.js";
|
|
import type { CliInstance } from "./types.js";
|
|
|
|
/** A registrar and the module it was loaded from. */
|
|
export interface LoadedCliCommandRegistration {
|
|
readonly registration: CliCommandRegistration;
|
|
readonly module: unknown;
|
|
}
|
|
|
|
export interface CliShellOptions {
|
|
readonly selectedEnvironment: Readonly<Record<string, string | undefined>>;
|
|
/** Registrars to run, in registration order. */
|
|
readonly registrations: readonly LoadedCliCommandRegistration[];
|
|
/** Required when any registrar declares analysis. */
|
|
readonly createAnalysis?: (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
) => DirectAnalysis;
|
|
readonly resultOutput?: CliResultOutput | undefined;
|
|
readonly fridaInstrumentation?: FridaInstrumentationPort | undefined;
|
|
}
|
|
|
|
/** Run each registrar, creating analysis once for those that declare it. */
|
|
const registerCommands = (
|
|
context: CliRegistrationContext,
|
|
options: CliShellOptions,
|
|
): void => {
|
|
let analysis: DirectAnalysis | undefined;
|
|
for (const { registration, module } of options.registrations) {
|
|
if (!registration.analysis) {
|
|
registration.register(module, context);
|
|
continue;
|
|
}
|
|
if (options.createAnalysis === undefined)
|
|
throw new Error("A CLI registrar needs analysis that was not provided");
|
|
analysis ??= options.createAnalysis(context.environment);
|
|
registration.register(module, { ...context, analysis });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Build the Incur CLI around the given registrars. Identity, global options,
|
|
* argument policy, completions, and registration builtins are the same for
|
|
* every set of registrars; they apply to whichever commands were registered.
|
|
*/
|
|
export const createCliShell = (options: CliShellOptions): CliInstance => {
|
|
const environment = snapshotEnvironment(
|
|
options.selectedEnvironment,
|
|
HOST_PLATFORM.platform,
|
|
);
|
|
const defaultRegistration = parseClientRegistrationCommand(
|
|
currentClientRegistrationCommand({ environment }),
|
|
);
|
|
// The executable reports an invalid level before every command except doctor.
|
|
// Doctor keeps going so its readiness report can name REA_LOG_LEVEL.
|
|
const level =
|
|
environment.REA_LOG_LEVEL === undefined
|
|
? undefined
|
|
: parseLogLevel(environment.REA_LOG_LEVEL);
|
|
const logger = createLogger(
|
|
"cli",
|
|
level?.ok === true ? level.value : "silent",
|
|
);
|
|
const timedCli = Cli.create(PRODUCT_IDENTITY.cliBinary, {
|
|
version: PRODUCT_IDENTITY.packageVersion,
|
|
description: CLI_DESCRIPTION,
|
|
globals: CLI_CUSTOM_GLOBAL_OPTIONS,
|
|
mcp: {
|
|
instructions:
|
|
"Ask what software, artifact, protocol, or behavior the user wants to understand, then choose the available investigation capabilities that can produce evidence.",
|
|
},
|
|
sync: false,
|
|
update: {
|
|
// Incur's update:false only suppresses notices. Reject its explicit
|
|
// installer so ambiguous flags cannot bypass REA's verified workflow.
|
|
install: () => {
|
|
throw new Error(
|
|
"Use 'rea update' to update the identified installation. The '--update' alias accepts only global output flags.",
|
|
);
|
|
},
|
|
},
|
|
});
|
|
|
|
timedCli.use(async (context, next) => {
|
|
if (!context.globals.timings) return next();
|
|
const started = performance.now();
|
|
try {
|
|
await next();
|
|
} finally {
|
|
// This interval covers CLI command dispatch, including result delivery.
|
|
// It is not a provider-only or analysis-only measurement.
|
|
process.stderr.write(
|
|
`${JSON.stringify({ rea_timings: { version: 1, command_ms: Math.round(performance.now() - started) } })}\n`,
|
|
);
|
|
}
|
|
});
|
|
// Command registrars do not inspect global options; preserve their existing
|
|
// type while Incur owns parsing and the middleware owns this added flag.
|
|
const cli = timedCli as unknown as CliInstance;
|
|
|
|
registerCommands(
|
|
{
|
|
cli,
|
|
logger,
|
|
environment,
|
|
fridaInstrumentation: options.fridaInstrumentation,
|
|
},
|
|
options,
|
|
);
|
|
|
|
presentOmittableDefaults(cli);
|
|
// Discovery reads the arguments the dispatcher would, after result output
|
|
// has adapted them, so its documents are counted and buffered alike.
|
|
const dispatch = cli.serve.bind(cli);
|
|
const updateEnvironment = {
|
|
variables: environment,
|
|
platform: HOST_PLATFORM.platform,
|
|
};
|
|
cli.serve = (arguments_ = process.argv.slice(2), serveOptions) =>
|
|
serveDiscovery(cli, arguments_, serveOptions ?? {}, updateEnvironment) ??
|
|
dispatch(arguments_, serveOptions);
|
|
if (options.resultOutput !== undefined)
|
|
streamCliCommandResults(cli, options.resultOutput);
|
|
const serveStrict = validateCliArguments(cli);
|
|
presentCliArgumentErrors(cli);
|
|
const serveDeclared = excludeDeclaredSensitiveArguments(cli);
|
|
encodeCliArgumentErrors(cli);
|
|
const serve = cli.serve.bind(cli);
|
|
const completions = createCliCompletions(cli, environment);
|
|
cli.serve = (arguments_ = process.argv.slice(2), serveOptions) => {
|
|
if (completions.complete(arguments_, serveOptions ?? {}))
|
|
return Promise.resolve();
|
|
const global = parseCliGlobalArguments(arguments_);
|
|
if (completions.writeScript(global, serveOptions ?? {}))
|
|
return Promise.resolve();
|
|
const registration = serveMcpRegistration(
|
|
global,
|
|
environment,
|
|
defaultRegistration,
|
|
serveOptions ?? {},
|
|
);
|
|
if (registration !== undefined) return registration;
|
|
const result = serveStrict(global, serveOptions ?? {}, (selected) =>
|
|
serveDeclared(global, selected, (declaredOptions) =>
|
|
reportUnknownMcpSubcommand(cli.name, global, declaredOptions)
|
|
? Promise.resolve()
|
|
: serve(
|
|
adaptCliOutputArgumentsForIncur(global.arguments),
|
|
declaredOptions,
|
|
),
|
|
),
|
|
);
|
|
const note = completionHelpNote(global);
|
|
if (note === undefined) return result;
|
|
return result.then(() => {
|
|
const write =
|
|
serveOptions?.stdout ?? ((text: string) => process.stdout.write(text));
|
|
write(`\n${note}`);
|
|
});
|
|
};
|
|
return cli;
|
|
};
|