Skip to content

Commit 2d7b945

Browse files
committed
fix(service): build service definitions from an injected proxy env, not process.env
The #2107 tests assigned HTTP_PROXY/HTTPS_PROXY/NO_PROXY onto the real process.env and restored them in a finally. That looked airtight and was not: `bun test a.test.ts b.test.ts` runs every file in ONE process, and --isolate does not change that. The values outlived the file. The Lab sandbox calls rejectProxyEnvironment() against the live process.env and treats any proxy variable as a harness_failure, by design — it must not dial out through a proxy. So every Lab file that loaded after service.test.ts died on a leaked variable it never set: 73 failures on the unsharded macOS lane, zero when the Lab suites ran alone, which is exactly the shape that makes this look like flake rather than a defect. The fix is to stop mutating global state to test a pure function. buildUnit() and buildPlist() now take the resolved proxy entries as a parameter defaulting to resolvedProxyEnv(), so production behavior is unchanged and the tests hand in a literal environment. resolvedProxyEnv() already accepted an env argument; it is now exported so a test can use it the way the runtime does. A third case is added while the seam is open: a lower-case http_proxy must be baked under the canonical upper-case name. That was implemented and documented but never asserted. Refs #2107 Verification: the five suites that carried the failure — service, lab-live-probe, lab-fabric-task, lab-automation, api-key-attribution — go 50 fail -> 0 fail, 236 pass. tsc --noEmit exit 0.
1 parent d7caaa9 commit 2d7b945

2 files changed

Lines changed: 39 additions & 40 deletions

File tree

‎src/service.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ function writeServiceApiTokenFile(): string | null {
390390
return path;
391391
}
392392

393-
export function buildPlist(): string {
393+
export function buildPlist(proxyEnv: { name: string; value: string }[] = resolvedProxyEnv()): string {
394394
const { bun, bunRuntimeSource, cli } = cliEntry();
395395
const log = logPath();
396396
const path = process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin";
@@ -405,7 +405,7 @@ export function buildPlist(): string {
405405
codexHome ? ` <key>CODEX_HOME</key><string>${plistString(codexHome)}</string>` : null,
406406
codexSqliteHome ? ` <key>CODEX_SQLITE_HOME</key><string>${plistString(codexSqliteHome)}</string>` : null,
407407
opencodexHome ? ` <key>OPENCODEX_HOME</key><string>${plistString(opencodexHome)}</string>` : null,
408-
...resolvedProxyEnv().map(({ name, value }) =>
408+
...proxyEnv.map(({ name, value }) =>
409409
` <key>${name}</key><string>${plistString(value)}</string>`),
410410
].filter((line): line is string => Boolean(line)).join("\n");
411411
const command = buildServiceShellCommand(bun, cli);
@@ -659,7 +659,7 @@ function systemdEnvironmentAssignment(name: string, value: string | undefined):
659659
* own `applyProxyEnv` already treats both cases as equivalent. Only the canonical
660660
* upper-case name is baked, so a definition never carries two spellings of one setting.
661661
*/
662-
function resolvedProxyEnv(env: NodeJS.ProcessEnv = process.env): { name: string; value: string }[] {
662+
export function resolvedProxyEnv(env: NodeJS.ProcessEnv = process.env): { name: string; value: string }[] {
663663
const resolved: { name: string; value: string }[] = [];
664664
for (const key of PROXY_ENV_KEYS) {
665665
const value = env[key]?.trim() || env[key.toLowerCase()]?.trim();
@@ -2444,7 +2444,7 @@ function unitPath(): string {
24442444
return join(unitDir(), `${TASK}.service`);
24452445
}
24462446

2447-
export function buildUnit(): string {
2447+
export function buildUnit(proxyEnv: { name: string; value: string }[] = resolvedProxyEnv()): string {
24482448
const { bun, bunRuntimeSource, cli } = cliEntry();
24492449
const log = logPath();
24502450
const path = process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin";
@@ -2459,7 +2459,7 @@ export function buildUnit(): string {
24592459
codexHome,
24602460
codexSqliteHome,
24612461
opencodexHome,
2462-
...resolvedProxyEnv().map(({ name, value }) => systemdEnvironmentAssignment(name, value)),
2462+
...proxyEnv.map(({ name, value }) => systemdEnvironmentAssignment(name, value)),
24632463
].filter((line): line is string => Boolean(line)).join("\n");
24642464
return `[Unit]
24652465
Description=OpenCodex Proxy Server

‎tests/service.test.ts‎

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { saveConfig } from "../src/config";
77
import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths";
88
import { assertServiceAuthEnvironment, assertServiceEnvironmentMatchesInstall, bakedServicePathsDiagnostic, confirmServiceServing, launchdListenPort, systemdListenPort, buildPlist, buildUnit, buildWindowsLauncherVbs, buildWindowsSchtasksCreateArgs, buildWindowsSchtasksCreateArgsForXml, buildWindowsServiceScript, buildWindowsTaskXml, deriveWindowsServiceDiagnostic, installFreshWindowsSchedulerSafely, installServiceSafely, launchctlLoadFailed, launchdJobMatchesPlist, normalizeServiceSubcommand, parseServiceInstallState, prepareServiceInstall, readWindowsSchedulerXmlState, registerFreshWindowsSchedulerTask, removeNativeWindowsServiceForScheduler, repairService, resolveServiceListenPort, runLaunchctl, serviceLogPath, serviceStartableFromTray, serviceStatusReport, serviceRetryCommand, serviceStatusSummary, systemdNeedsDaemonReload, windowsListenPort, winswListenPort, startLaunchd, windowsTaskRegistrationHealthy } from "../src/service";
99
import type { ServiceDiagnostic } from "../src/service";
10+
import { resolvedProxyEnv } from "../src/service";
1011
import { buildWinswXml } from "../src/lib/winsw";
1112
import { CONFIG_OWNER_FILE, CONFIG_UNINSTALL_MANIFEST, recordOwnedConfigPath, removeOwnedConfigState } from "../src/lib/config-ownership";
1213
import { serviceApiTokenFilePath } from "../src/lib/service-secrets";
@@ -115,49 +116,47 @@ describe("systemd service unit", () => {
115116
// /bin/sh -lc — which is dash on Ubuntu/WSL and reads .profile, not .bashrc. A user
116117
// whose proxy lives in the shell therefore gets a service that dials upstream direct,
117118
// the socket is reset, and the request surfaces as 502 Provider unreachable.
118-
const saved = { ...process.env };
119-
try {
120-
process.env.HTTP_PROXY = "http://127.0.0.1:7890";
121-
process.env.HTTPS_PROXY = "http://127.0.0.1:7890";
122-
process.env.NO_PROXY = "localhost,127.0.0.1";
123-
delete process.env.ALL_PROXY;
119+
//
120+
// The shell is passed in rather than assigned onto `process.env`. Mutating the real
121+
// environment here leaked `HTTP_PROXY` out of this file: Bun runs a `bun test a b`
122+
// invocation in ONE process, and the Lab sandbox calls `rejectProxyEnvironment()` on
123+
// the live `process.env`, so every Lab file that loaded afterwards died with
124+
// `harness_failure`. That was 73 failures on the unsharded macOS lane and zero when
125+
// the Lab suites ran alone.
126+
const proxyEnv = resolvedProxyEnv({
127+
HTTP_PROXY: "http://127.0.0.1:7890",
128+
HTTPS_PROXY: "http://127.0.0.1:7890",
129+
NO_PROXY: "localhost,127.0.0.1",
130+
});
124131

125-
const unit = buildUnit();
126-
expect(unit).toContain('Environment="HTTP_PROXY=http://127.0.0.1:7890"');
127-
expect(unit).toContain('Environment="HTTPS_PROXY=http://127.0.0.1:7890"');
128-
expect(unit).toContain("NO_PROXY=");
129-
// An unset key must not produce an empty assignment.
130-
expect(unit).not.toContain('Environment="ALL_PROXY="');
132+
const unit = buildUnit(proxyEnv);
133+
expect(unit).toContain('Environment="HTTP_PROXY=http://127.0.0.1:7890"');
134+
expect(unit).toContain('Environment="HTTPS_PROXY=http://127.0.0.1:7890"');
135+
expect(unit).toContain("NO_PROXY=");
136+
// An unset key must not produce an empty assignment.
137+
expect(unit).not.toContain('Environment="ALL_PROXY="');
131138

132-
const plist = buildPlist();
133-
expect(plist).toContain("<key>HTTP_PROXY</key><string>http://127.0.0.1:7890</string>");
134-
expect(plist).not.toContain("<key>ALL_PROXY</key>");
135-
} finally {
136-
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"]) {
137-
if (saved[key] === undefined) delete process.env[key];
138-
else process.env[key] = saved[key];
139-
}
140-
}
139+
const plist = buildPlist(proxyEnv);
140+
expect(plist).toContain("<key>HTTP_PROXY</key><string>http://127.0.0.1:7890</string>");
141+
expect(plist).not.toContain("<key>ALL_PROXY</key>");
141142
});
142143

143144
test("omits proxy env entirely when the installing shell has none (#2107)", () => {
144-
const saved = { ...process.env };
145-
try {
146-
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
147-
"http_proxy", "https_proxy", "all_proxy", "no_proxy"]) delete process.env[key];
148-
149-
const unit = buildUnit();
150-
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"]) {
151-
expect(unit).not.toContain(`${key}=`);
152-
}
153-
} finally {
154-
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
155-
"http_proxy", "https_proxy", "all_proxy", "no_proxy"]) {
156-
if (saved[key] !== undefined) process.env[key] = saved[key];
157-
}
145+
const unit = buildUnit(resolvedProxyEnv({}));
146+
for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"]) {
147+
expect(unit).not.toContain(`${key}=`);
158148
}
159149
});
160150

151+
test("lower-case shell spellings are baked under the canonical name (#2107)", () => {
152+
// curl-style tooling sets the lower-case pair; only the upper-case name is emitted so a
153+
// definition never carries two spellings of one setting.
154+
const unit = buildUnit(resolvedProxyEnv({ http_proxy: "http://127.0.0.1:7890" }));
155+
156+
expect(unit).toContain('Environment="HTTP_PROXY=http://127.0.0.1:7890"');
157+
expect(unit).not.toContain("http_proxy=");
158+
});
159+
161160
test("preserves custom Codex and OpenCodex homes", () => {
162161
const oldCodexHome = process.env.CODEX_HOME;
163162
const oldCodexSqliteHome = process.env.CODEX_SQLITE_HOME;

0 commit comments

Comments
 (0)