perf(tests): run plain CLI commands on the file's shared process (#10412)

* refactor(cli): run one-shot commands through one shared owner

scripts/rea.mjs and the test CLI session worker each implemented the
one-shot command sequence: argument normalization, output admission,
configuration errors, version documents, the Amp scope refusal, and
result delivery. The worker copy had drifted: it never rendered
formatted version reports or the Amp refusal.

Move the sequence into src/cliOneShot.ts. Both callers supply a host
for their streams, exit and resource ownership. The launcher keeps
closed-pipe status, worker shutdown and process exit; the session
worker captures output per command.

The cliOutput bundle entry is superseded and removed. The dispatcher
test now replaces only the command runtime, so output admission runs
production code. A new parity test compares the file session with a
fresh process.

* test(cli): bundle the one-shot entry in the runtime bundle fixture

* perf(tests): run plain CLI commands on the file's shared process

`cli.run` in `cliTest` files started a fresh compiled CLI for every
command, about 0.45s and 260 MB each before any work. The file-scoped
session already existed, but callers had to opt in through
`runFileCli` or `sharedCliRun`, so 1,300 call sites still spawned.

Make the fixture choose: `cli.run` uses the shared process when it
reports the command exactly as a fresh process would, and starts one
otherwise (stdin, explicit deadlines, MCP and MCP doctor routing,
NODE_OPTIONS, or enabled logging). `freshProcess: true` keeps
explicit process-level checks. The session worker now presents the
launcher's entrypoint, so registration identity matches a fresh
process.

`runFileCli` and `sharedCliRun` are superseded and removed; their
call sites use `cli.run`, and paired fresh/shared checks use
`freshProcess`.

* style(tests): format the managed member summary call site

* test(cli): give each shared-session command an empty stdin

The shared CLI process reads session frames from its own stdin. A
command that reads stdin without supplied input, such as
`compare-wasm-interfaces - --json`, read that pipe and hung, where a
fresh process receives EOF. The worker now presents an ended, empty
stdin to each command and restores its own afterwards.

The parity test compared the routed path with itself after cli.run
began choosing the session. It now compares a fresh process with the
session directly, including the `mcp add` refusal and a command
reading absent stdin.

* test(cli): keep default command logging and launcher checks on fresh processes

environment-serve and environment-build log at info when REA_LOG_LEVEL is
unset, through a logger that writes to stderr directly. The shared CLI
process captures only process.stderr writes, so routed runs lost those
lines. Declare the default-logging commands beside the command names,
have infoLogger read that set, and start a fresh process for them.

The log-level precedence checks compared the shared process with itself
after routing; their fresh side now passes freshProcess: true.
This commit is contained in:
morluto authored and GitHub committed 2026-11-14 14:28:08 +08:00
1 parent 6938696256
commit b3657c557f
86 files changed
+632 -548

No files matched your search

+16 -1
View File
@@ -1,4 +1,4 @@
import { Writable } from "node:stream";
import { Readable, Writable } from "node:stream";
import { access } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
@@ -21,6 +21,10 @@ const { default: packageJson } = await import(
{ with: { type: "json" } }
);
// Present the launcher's entrypoint, as a fresh `rea` process does. The
// runtime derives the current registration command from it at load time.
process.argv[1] = resolve(packageRoot, "scripts/rea.mjs");
const runtimeFiles = [
"dist/cli-runtime/cli.js",
"dist/cli-runtime/cliOneShot.js",
@@ -158,6 +162,15 @@ async function runOneShot(args) {
const originalStderrWrite = process.stderr.write.bind(process.stderr);
process.stdout.write = captureWrite(stdoutChunks);
process.stderr.write = captureWrite(stderrChunks);
// This process's stdin carries session frames. Give each command the empty,
// ended stdin a fresh process receives when the caller supplies no input.
const stdinDescriptor = Object.getOwnPropertyDescriptor(process, "stdin");
const commandStdin = Readable.from([]);
Object.defineProperty(process, "stdin", {
configurable: true,
enumerable: stdinDescriptor?.enumerable ?? true,
get: () => commandStdin,
});
const destination = new Writable({
write(chunk, encoding, callback) {
process.stdout.write(chunk, encoding, callback);
@@ -209,6 +222,8 @@ async function runOneShot(args) {
} finally {
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
if (stdinDescriptor !== undefined)
Object.defineProperty(process, "stdin", stdinDescriptor);
}
const exitCode =
explicitExit ??