Skip to content
TypeScript

fix(server): agent-device no longer launches the desktop app from Codex shells (#18301)

Krishna Vijay57f5d84073b0
diff --git a/apps/server/src/device/AgentDeviceShim.ts b/apps/server/src/device/AgentDeviceShim.ts
index 1c958250672..4a84b895ee3 100644
--- a/apps/server/src/device/AgentDeviceShim.ts
+++ b/apps/server/src/device/AgentDeviceShim.ts
@@ -29,7 +29,7 @@ if (!informational && !(hasValue("--config") && hasValue("--session"))) {
console.error("Call device_open first and include its --config and --session flags.");
process.exit(1);
}
-const env = { ...process.env };
+const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
delete env.AGENT_DEVICE_DAEMON_BASE_URL;
delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
delete env.AGENT_DEVICE_CONFIG;
@@ -39,13 +39,13 @@ child.on("exit", code => { process.exitCode = code ?? 1; });
`,
);
if (platform === "win32") {
- const script = `@echo off\r\n"${node}" "${launcherPath}" %*\r\n`;
+ const script = `@echo off\r\nsetlocal EnableExtensions DisableDelayedExpansion\r\nset "ELECTRON_RUN_AS_NODE=1"\r\n"${node}" "${launcherPath}" %*\r\n`;
yield* fs.writeFileString(path.join(shimDir, "agent-device.cmd"), script);
} else {
const command = [node, launcherPath]
.map((value) => "'" + value.replaceAll("'", "'\"'\"'") + "'")
.join(" ");
- const script = `#!/bin/sh\nexec ${command} "$@"\n`;
+ const script = `#!/bin/sh\nexport ELECTRON_RUN_AS_NODE=1\nexec ${command} "$@"\n`;
const shimPath = path.join(shimDir, "agent-device");
yield* fs.writeFileString(shimPath, script);
yield* fs.chmod(shimPath, 0o755);
diff --git a/apps/server/src/device/AgentDeviceTarget.test.ts b/apps/server/src/device/AgentDeviceTarget.test.ts
index 2cedbe1e8be..448fd592e69 100644
--- a/apps/server/src/device/AgentDeviceTarget.test.ts
+++ b/apps/server/src/device/AgentDeviceTarget.test.ts
@@ -17,6 +17,56 @@ import {
const exec = NodeUtil.promisify(NodeChildProcess.execFile);
describe("host-bound agent commands", () => {
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "runs both desktop hops as Node with a stripped environment, preserving arguments and exit code",
+ () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-target-" });
+ const dir = path.join(temp, "quotes '\" $HOME `literal`");
+ yield* fs.makeDirectory(dir);
+ const desktopApp = path.join(dir, "T3 Code");
+ // Dropping Node mode after checking it forces each hop to set it independently.
+ yield* fs.writeFileString(
+ desktopApp,
+ `#!/bin/sh
+if [ "\${ELECTRON_RUN_AS_NODE:-}" != "1" ]; then
+ echo "started the desktop app" >&2
+ exit 134
+fi
+unset ELECTRON_RUN_AS_NODE
+exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@"
+`,
+ );
+ yield* fs.chmod(desktopApp, 0o755);
+ const entryPath = path.join(dir, "cli.mjs");
+ yield* fs.writeFileString(
+ entryPath,
+ "console.log(JSON.stringify(process.argv.slice(2))); process.exitCode = 23;",
+ );
+ const shim = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir }).pipe(
+ Effect.provideService(HostProcess.ExecutablePath, desktopApp),
+ Effect.provideService(HostProcess.IsExecutable, false),
+ );
+ const args = [
+ "snapshot",
+ "--config",
+ path.join(dir, "config.json"),
+ "--session",
+ "a!b!c",
+ "",
+ ];
+ yield* Effect.promise(() =>
+ expect(
+ exec(path.join(shim, "agent-device"), args, {
+ env: { PATH: process.env.PATH ?? "", HOME: temp },
+ }),
+ ).rejects.toMatchObject({ code: 23, stdout: `${JSON.stringify(args)}\n`, stderr: "" }),
+ );
+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
+ );
+
it.effect("runs two hosts concurrently and only updates the reconnected host", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;