refactor(cli): share process environment preparation

Give the launcher and shared command worker one lightweight owner for package-version binding, Windows empty-home handling and Android browser-cache defaults. Preserve per-command preparation and environment restoration, package the owner explicitly, and retain its package and JavaScript recovery CI scopes.

Validation: dispatcher/version/shared-session, home fallback, evaluator/MCP/process cleanup and CI scope tests passed. Functional archive/install/repack, installed static CLI analysis and MCP checks passed; no real Codex/model trial or Windows/Termux runtime qualification was performed.
This commit is contained in:
morluto committed 2026-10-24 11:50:11 +08:00
1 parent 3ad465a666
commit c2caae88f8
9 files changed
+72 -43

No files matched your search

+12 -20
View File
@@ -1,8 +1,9 @@
import { Writable } from "node:stream";
import { access } from "node:fs/promises";
import { dirname, join, resolve } from "node:path";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { prepareCliProcessEnvironment } from "../../../scripts/lib/cli-process-environment.mjs";
import {
createCliSessionFrameReader,
encodeCliSessionFrame,
@@ -19,23 +20,6 @@ const { default: packageJson } = await import(
{ with: { type: "json" } }
);
const prepareCliProcess = () => {
process.env.REA_PACKAGE_VERSION = packageJson.version;
// Windows home lookup throws for an empty USERPROFILE. Dependencies resolve
// home during import, before REA's own resolver can apply its account fallback.
if (process.platform === "win32" && process.env.USERPROFILE === "") {
delete process.env.USERPROFILE;
}
if (
process.platform === "android" &&
process.env.PLAYWRIGHT_BROWSERS_PATH === undefined
) {
const cacheRoot =
process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? ".", ".cache");
process.env.PLAYWRIGHT_BROWSERS_PATH = join(cacheRoot, "ms-playwright");
}
};
const runtimeFiles = [
"dist/cli-runtime/cli.js",
"dist/cli-runtime/cliOutput.js",
@@ -43,7 +27,11 @@ const runtimeFiles = [
"dist/cli-runtime/cliResultOutput.js",
];
prepareCliProcess();
prepareCliProcessEnvironment(
process.env,
process.platform,
packageJson.version,
);
if (!(await compiledRuntimeExists(runtimeFiles))) {
process.stderr.write(
`REA's compiled runtime is missing. Run \`npm ci && npm run build:cached\` in ${packageRoot} to install dependencies and build REA, then restart it. If this is an installed package, reinstall rea-agents.\n`,
@@ -118,7 +106,11 @@ async function handle(value) {
}
for (const key of Object.keys(process.env)) delete process.env[key];
Object.assign(process.env, request.env);
prepareCliProcess();
prepareCliProcessEnvironment(
process.env,
process.platform,
packageJson.version,
);
process.chdir(request.cwd);
const result = await runOneShot(request.arguments);
await writeFrame({