diff --git a/lib/conductor-env-shim.ts b/lib/conductor-env-shim.ts index f8804a1b9..6f4ae08d9 100644 --- a/lib/conductor-env-shim.ts +++ b/lib/conductor-env-shim.ts @@ -7,12 +7,30 @@ * * Import this for its side effect: `import "../lib/conductor-env-shim";` */ -export function promoteConductorEnv(): void { - for (const key of ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"] as const) { - if (!process.env[key] && process.env[`GSTACK_${key}`]) { - process.env[key] = process.env[`GSTACK_${key}`]; +const PROMOTED_KEYS = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY"] as const; + +/** + * Pure form: returns a copy of `base` with each GSTACK_-prefixed key promoted + * to its canonical name when the canonical is empty. Single source of truth + * for promotion semantics — used by the ambient mutator below and by the + * hermetic env builder (test/helpers/hermetic-env.ts), which must not mutate + * process.env. + */ +export function promotedEnv(base: NodeJS.ProcessEnv): NodeJS.ProcessEnv { + const out: NodeJS.ProcessEnv = { ...base }; + for (const key of PROMOTED_KEYS) { + if (!out[key] && out[`GSTACK_${key}`]) { + out[key] = out[`GSTACK_${key}`]; } } + return out; +} + +export function promoteConductorEnv(): void { + const promoted = promotedEnv(process.env); + for (const key of PROMOTED_KEYS) { + if (promoted[key]) process.env[key] = promoted[key]; + } } promoteConductorEnv();