Skip to content

Commit 57f5d84

Browse files
im-kvijayclaudejuliusmarminge
authored
fix(server): agent-device no longer launches the desktop app from Codex shells (pingdotgg#18301)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent f740a97 commit 57f5d84

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎apps/server/src/device/AgentDeviceShim.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if (!informational && !(hasValue("--config") && hasValue("--session"))) {
2929
console.error("Call device_open first and include its --config and --session flags.");
3030
process.exit(1);
3131
}
32-
const env = { ...process.env };
32+
const env = { ...process.env, ELECTRON_RUN_AS_NODE: "1" };
3333
delete env.AGENT_DEVICE_DAEMON_BASE_URL;
3434
delete env.AGENT_DEVICE_DAEMON_AUTH_TOKEN;
3535
delete env.AGENT_DEVICE_CONFIG;
@@ -39,13 +39,13 @@ child.on("exit", code => { process.exitCode = code ?? 1; });
3939
`,
4040
);
4141
if (platform === "win32") {
42-
const script = `@echo off\r\n"${node}" "${launcherPath}" %*\r\n`;
42+
const script = `@echo off\r\nsetlocal EnableExtensions DisableDelayedExpansion\r\nset "ELECTRON_RUN_AS_NODE=1"\r\n"${node}" "${launcherPath}" %*\r\n`;
4343
yield* fs.writeFileString(path.join(shimDir, "agent-device.cmd"), script);
4444
} else {
4545
const command = [node, launcherPath]
4646
.map((value) => "'" + value.replaceAll("'", "'\"'\"'") + "'")
4747
.join(" ");
48-
const script = `#!/bin/sh\nexec ${command} "$@"\n`;
48+
const script = `#!/bin/sh\nexport ELECTRON_RUN_AS_NODE=1\nexec ${command} "$@"\n`;
4949
const shimPath = path.join(shimDir, "agent-device");
5050
yield* fs.writeFileString(shimPath, script);
5151
yield* fs.chmod(shimPath, 0o755);

‎apps/server/src/device/AgentDeviceTarget.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,56 @@ import {
1717
const exec = NodeUtil.promisify(NodeChildProcess.execFile);
1818

1919
describe("host-bound agent commands", () => {
20+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
21+
"runs both desktop hops as Node with a stripped environment, preserving arguments and exit code",
22+
() =>
23+
Effect.gen(function* () {
24+
const fs = yield* FileSystem.FileSystem;
25+
const path = yield* Path.Path;
26+
const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-device-target-" });
27+
const dir = path.join(temp, "quotes '\" $HOME `literal`");
28+
yield* fs.makeDirectory(dir);
29+
const desktopApp = path.join(dir, "T3 Code");
30+
// Dropping Node mode after checking it forces each hop to set it independently.
31+
yield* fs.writeFileString(
32+
desktopApp,
33+
`#!/bin/sh
34+
if [ "\${ELECTRON_RUN_AS_NODE:-}" != "1" ]; then
35+
echo "started the desktop app" >&2
36+
exit 134
37+
fi
38+
unset ELECTRON_RUN_AS_NODE
39+
exec '${process.execPath.replaceAll("'", "'\"'\"'")}' "$@"
40+
`,
41+
);
42+
yield* fs.chmod(desktopApp, 0o755);
43+
const entryPath = path.join(dir, "cli.mjs");
44+
yield* fs.writeFileString(
45+
entryPath,
46+
"console.log(JSON.stringify(process.argv.slice(2))); process.exitCode = 23;",
47+
);
48+
const shim = yield* ensureAgentDeviceShim({ entryPath, stateDir: dir }).pipe(
49+
Effect.provideService(HostProcess.ExecutablePath, desktopApp),
50+
Effect.provideService(HostProcess.IsExecutable, false),
51+
);
52+
const args = [
53+
"snapshot",
54+
"--config",
55+
path.join(dir, "config.json"),
56+
"--session",
57+
"a!b!c",
58+
"",
59+
];
60+
yield* Effect.promise(() =>
61+
expect(
62+
exec(path.join(shim, "agent-device"), args, {
63+
env: { PATH: process.env.PATH ?? "", HOME: temp },
64+
}),
65+
).rejects.toMatchObject({ code: 23, stdout: `${JSON.stringify(args)}\n`, stderr: "" }),
66+
);
67+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
68+
);
69+
2070
it.effect("runs two hosts concurrently and only updates the reconnected host", () =>
2171
Effect.gen(function* () {
2272
const fs = yield* FileSystem.FileSystem;

0 commit comments

Comments
 (0)