Skip to content

Commit 22468dc

Browse files
fix(server): managed Codex token helper keeps Electron-as-Node mode
Provider environments no longer carry ELECTRON_RUN_AS_NODE, but managed Codex's auth command re-invokes the server executable, which is Electron on desktop. Set the flag on that command alone (via env on POSIX), so Codex's other children still run without it. Windows has no env, so the flag stays in the managed Codex environment there, as on main. AI-assisted; verified with the tests and tools listed above.
1 parent 3c56aef commit 22468dc

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/server/src/provider/CodexManagedRuntime.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ it.live.each(
156156
const [command, helperArgs] = decodeCommand(
157157
`[${commandMatch![1]},${commandMatch![2]}]`,
158158
);
159+
// The desktop backend runs Electron as Node; only the helper may inherit that mode.
160+
if ((yield* HostProcess.Platform) !== "win32") {
161+
assert.deepEqual(
162+
[command, ...helperArgs.slice(0, 2)],
163+
["/usr/bin/env", "ELECTRON_RUN_AS_NODE=1", process.execPath],
164+
);
165+
assert.isUndefined(effective.environment.ELECTRON_RUN_AS_NODE);
166+
}
159167
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
160168
const runHelper = Effect.gen(function* () {
161169
const child = yield* spawner.spawn(
@@ -217,6 +225,10 @@ it.live.each(
217225
);
218226
}).pipe(
219227
Effect.provide(CodexManagedTokenBroker.layer),
228+
Effect.provideService(HostProcess.Environment, {
229+
...process.env,
230+
ELECTRON_RUN_AS_NODE: "1",
231+
}),
220232
Effect.provideService(HostProcess.Arguments, [
221233
process.execPath,
222234
NodeURL.fileURLToPath(new URL("../bin.ts", import.meta.url)),

‎apps/server/src/provider/CodexManagedRuntime.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Schema from "effect/Schema";
77
import * as ServerConfig from "../config.ts";
88
import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts";
99
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
10+
import * as HostProcess from "@t3tools/shared/HostProcess";
1011
import { resolveSelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeRuntime";
1112
import * as CodexInstallation from "./CodexInstallation.ts";
1213
import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
@@ -45,6 +46,10 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
4546
const environmentIdentity = yield* ServerEnvironment.ServerEnvironmentIdentity;
4647
const environmentId = yield* environmentIdentity.getEnvironmentId;
4748
const invocation = yield* resolveSelfInvocation();
49+
// On desktop the self-invocation is Electron, which runs the helper as Node only with this flag.
50+
// Scope it to the auth command so Codex's own children do not inherit it; Windows has no `env`.
51+
const electronAsNode = (yield* HostProcess.Environment).ELECTRON_RUN_AS_NODE === "1";
52+
const wrapAuthCommand = electronAsNode && (yield* HostProcess.Platform) !== "win32";
4853
const auth = yield* makeCodexChatGptAuth({
4954
instanceId: options.instanceId,
5055
defaultReturnUrl: new URL(
@@ -116,6 +121,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
116121
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
117122
const environment: NodeJS.ProcessEnv = {
118123
...options.environment,
124+
...(electronAsNode && !wrapAuthCommand ? { ELECTRON_RUN_AS_NODE: "1" } : {}),
119125
CODEX_HOME: homePath,
120126
};
121127
delete environment.ACCESS_TOKEN;
@@ -128,7 +134,13 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
128134
setupMode: "managed",
129135
binaryPath: executable.executablePath,
130136
homePath,
131-
launchArgs: managedCodexLaunchArgs(invocation.command, authArgs),
137+
launchArgs: wrapAuthCommand
138+
? managedCodexLaunchArgs("/usr/bin/env", [
139+
"ELECTRON_RUN_AS_NODE=1",
140+
invocation.command,
141+
...authArgs,
142+
])
143+
: managedCodexLaunchArgs(invocation.command, authArgs),
132144
}),
133145
environment,
134146
revision: profileKey,

0 commit comments

Comments
 (0)