fix(process): normalize capture environment overrides

This commit is contained in:
morluto committed 2026-10-27 12:45:57 +08:00
1 parent 160648cc60
commit 7968ceb450
2 files changed
+77 -12

No files matched your search

@@ -0,0 +1,53 @@
import { expect, it } from "vitest";
import { processScenarioSchema } from "../../domain/process/processScenario.js";
import { makeProcessCaptureEnvironment } from "./ProcessCaptureEnvironment.js";
it("uses Windows environment identity for caller overrides and capture-owned values", () => {
const scenario = processScenarioSchema.parse({
executable: "node",
environment: {
path: "caller-bin",
term: "caller-terminal",
rea_process_run_id: "caller-token",
},
});
expect(
makeProcessCaptureEnvironment({
scenario,
hostEnvironment: {
Path: "host-bin",
TERM: "host-terminal",
REA_PROCESS_RUN_ID: "host-token",
OMITTED: undefined,
},
runId: "capture-token",
platform: "win32",
}),
).toEqual({
PATH: "caller-bin",
TERM: "caller-terminal",
REA_PROCESS_RUN_ID: "capture-token",
});
});
it("preserves distinct POSIX names and supplies the terminal default", () => {
const scenario = processScenarioSchema.parse({
executable: "node",
environment: { path: "caller-bin", term: "lowercase-terminal" },
});
expect(
makeProcessCaptureEnvironment({
scenario,
hostEnvironment: { PATH: "host-bin", OMITTED: undefined },
runId: "capture-token",
platform: "linux",
}),
).toEqual({
PATH: "host-bin",
path: "caller-bin",
term: "lowercase-terminal",
TERM: "xterm-256color",
REA_PROCESS_RUN_ID: "capture-token",
});
});
@@ -1,24 +1,36 @@
import type { ProcessScenario } from "../../domain/process/processScenario.js";
import {
mergeEnvironmentOverrides,
snapshotEnvironment,
} from "../snapshotEnvironment.js";
interface ProcessCaptureEnvironmentOptions {
readonly scenario: ProcessScenario;
readonly runId: string;
readonly hostEnvironment: Readonly<Record<string, string | undefined>>;
readonly platform?: NodeJS.Platform;
}
/** Build the child environment from the host plus caller-selected overrides. */
export const makeProcessCaptureEnvironment = (
options: ProcessCaptureEnvironmentOptions,
): Record<string, string> => ({
...Object.fromEntries(
Object.entries(options.hostEnvironment).filter(
(entry): entry is [string, string] => entry[1] !== undefined,
): Record<string, string> => {
const platform = options.platform ?? process.platform;
const environment = snapshotEnvironment(
mergeEnvironmentOverrides(
options.hostEnvironment,
options.scenario.environment,
platform,
),
),
...options.scenario.environment,
TERM:
options.scenario.environment.TERM ??
options.hostEnvironment.TERM ??
"xterm-256color",
REA_PROCESS_RUN_ID: options.runId,
});
platform,
);
return {
...Object.fromEntries(
Object.entries(environment).filter(
(entry): entry is [string, string] => entry[1] !== undefined,
),
),
TERM: environment.TERM ?? "xterm-256color",
REA_PROCESS_RUN_ID: options.runId,
};
};