Skip to content

Commit 5d5a523

Browse files
committed
fix(server): agent sessions no longer inherit the AppImage runtime environment
Provider sessions got the server's environment unchanged, including APPIMAGE, APPDIR, ARGV0, OWD, the AppImage mount on PATH and LD_LIBRARY_PATH, and ELECTRON_RUN_AS_NODE=1. zsh uses an exported ARGV0 as argv[0] for every command, so Python in an agent shell reported the AppImage as sys.executable and re-running it started Node. mergeProviderInstanceEnvironment now applies the scrub the integrated terminal already used and drops ELECTRON_RUN_AS_NODE. Spawns that run T3's own binary as Node set that flag themselves.
1 parent bfec238 commit 5d5a523

4 files changed

Lines changed: 104 additions & 62 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Marker variables the AppImage runtime injects into the process it launches.
2+
// They describe the AppImage itself, not the user's session, so terminals and
3+
// provider agents must not inherit them.
4+
const APPIMAGE_RUNTIME_ENV_KEYS = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"] as const;
5+
// Colon-separated search-path variables the AppImage runtime points at its
6+
// temporary mount (e.g. /tmp/.mount_T3-XXXX/usr/bin, the bundled glib schemas,
7+
// and an $APPDIR/usr/share XDG data entry). Only the mount segments are
8+
// dropped; the user's real entries are preserved. When nothing but mount
9+
// segments remain the variable is removed entirely so consumers fall back to
10+
// their platform default (e.g. gsettings finds the host schemas instead of
11+
// reporting "No schemas installed"). See issues #1699 and #5059.
12+
const APPIMAGE_PATH_LIKE_ENV_KEYS = [
13+
"PATH",
14+
"LD_LIBRARY_PATH",
15+
"XDG_DATA_DIRS",
16+
"GSETTINGS_SCHEMA_DIR",
17+
] as const;
18+
19+
function isPathSegmentUnderAppDir(segment: string, appDir: string): boolean {
20+
return segment === appDir || segment.startsWith(`${appDir}/`);
21+
}
22+
23+
// On Linux AppImage builds the runtime mounts the app under a temporary dir and
24+
// injects APPIMAGE/APPDIR/ARGV0/OWD plus mount entries on PATH/LD_LIBRARY_PATH.
25+
// Terminals and provider agents inherit the server process environment, so
26+
// without this scrub those leak into their shells and tools resolve against the
27+
// AppImage mount instead of the user's real environment (e.g. `php` reporting
28+
// PHP_BINARY as the AppImage path, or zsh handing every command ARGV0 as its
29+
// argv[0]). See issues #1699 and #16456. The scrub is gated on an actual
30+
// AppImage launch so non-AppImage environments are left untouched.
31+
export function stripAppImageRuntimeEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
32+
if (env.APPIMAGE === undefined && env.APPDIR === undefined) return env;
33+
34+
const scrubbed: NodeJS.ProcessEnv = { ...env };
35+
for (const key of APPIMAGE_RUNTIME_ENV_KEYS) {
36+
delete scrubbed[key];
37+
}
38+
39+
const appDir = env.APPDIR?.replace(/\/+$/, "");
40+
if (appDir) {
41+
for (const key of APPIMAGE_PATH_LIKE_ENV_KEYS) {
42+
const value = scrubbed[key];
43+
if (value === undefined) continue;
44+
const kept = value
45+
.split(":")
46+
.filter((segment) => segment.length > 0 && !isPathSegmentUnderAppDir(segment, appDir));
47+
if (kept.length > 0) {
48+
scrubbed[key] = kept.join(":");
49+
} else {
50+
delete scrubbed[key];
51+
}
52+
}
53+
}
54+
55+
return scrubbed;
56+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,43 @@ describe("mergeProviderInstanceEnvironment", () => {
6565
PATH: "/bin",
6666
});
6767
});
68+
69+
it("keeps the AppImage runtime and the Electron-as-Node flag out of agent sessions", () => {
70+
const appDir = "/tmp/.mount_T3-Codeabc123";
71+
72+
expect(
73+
mergeProviderInstanceEnvironment(undefined, {
74+
APPIMAGE: "/home/user/T3-Code.AppImage",
75+
APPDIR: appDir,
76+
ARGV0: "/home/user/T3-Code.AppImage",
77+
OWD: "/home/user/project",
78+
ELECTRON_RUN_AS_NODE: "1",
79+
PATH: `${appDir}:${appDir}/usr/sbin:/usr/local/bin:/usr/bin`,
80+
LD_LIBRARY_PATH: `${appDir}/usr/lib`,
81+
HOME: "/home/user",
82+
}),
83+
).toEqual({ PATH: "/usr/local/bin:/usr/bin", HOME: "/home/user" });
84+
});
85+
86+
it("drops only the Electron-as-Node flag outside an AppImage", () => {
87+
expect(
88+
mergeProviderInstanceEnvironment([], {
89+
ELECTRON_RUN_AS_NODE: "1",
90+
OWD: "/home/user/keep-this",
91+
PATH: "/usr/bin",
92+
}),
93+
).toEqual({ OWD: "/home/user/keep-this", PATH: "/usr/bin" });
94+
});
95+
96+
it("applies provider settings after the scrub", () => {
97+
expect(
98+
mergeProviderInstanceEnvironment(
99+
[
100+
{ name: "ELECTRON_RUN_AS_NODE", value: "1", sensitive: false },
101+
{ name: "APPDIR", value: "/opt/configured", sensitive: false },
102+
],
103+
{ APPIMAGE: "/home/user/T3-Code.AppImage", APPDIR: "/tmp/.mount_T3-x", PATH: "/usr/bin" },
104+
),
105+
).toEqual({ ELECTRON_RUN_AS_NODE: "1", APPDIR: "/opt/configured", PATH: "/usr/bin" });
106+
});
68107
});

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import type { ProviderInstanceEnvironment } from "@t3tools/contracts";
22

3+
import { stripAppImageRuntimeEnv } from "../appImageRuntimeEnv.ts";
34
import { expandHomePath } from "../pathExpansion.ts";
45

56
export function mergeProviderInstanceEnvironment(
67
environment: ProviderInstanceEnvironment | undefined,
78
baseEnv: NodeJS.ProcessEnv = process.env,
89
): NodeJS.ProcessEnv {
9-
if (!environment || environment.length === 0) {
10-
return baseEnv;
11-
}
12-
13-
const next: NodeJS.ProcessEnv = { ...baseEnv };
14-
for (const variable of environment) {
10+
// Agents and the shells they start get the user's environment, not the
11+
// AppImage runtime's. The packaged server runs as Electron-as-Node; an agent
12+
// that inherits that flag starts Node when it re-executes the app binary.
13+
// Spawns that need it set it explicitly.
14+
const next: NodeJS.ProcessEnv = { ...stripAppImageRuntimeEnv(baseEnv) };
15+
delete next.ELECTRON_RUN_AS_NODE;
16+
for (const variable of environment ?? []) {
1517
// Child processes do not apply shell expansion to environment values.
1618
next[variable.name] =
1719
variable.name === "CODEX_HOME" || variable.name === "CLAUDE_CONFIG_DIR"

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import * as Scope from "effect/Scope";
6262
import * as SynchronizedRef from "effect/SynchronizedRef";
6363

6464
import * as ServerConfig from "../config.ts";
65+
import { stripAppImageRuntimeEnv } from "../appImageRuntimeEnv.ts";
6566
import { mergeProviderInstanceEnvironment } from "../provider/ProviderInstanceEnvironment.ts";
6667
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
6768
import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";
@@ -1250,62 +1251,6 @@ function shouldExcludeTerminalEnvKey(key: string): boolean {
12501251
return TERMINAL_ENV_BLOCKLIST.has(normalizedKey);
12511252
}
12521253

1253-
// Marker variables the AppImage runtime injects into the process it launches.
1254-
// They describe the AppImage itself, not the user's session, so terminals must
1255-
// not inherit them.
1256-
const APPIMAGE_RUNTIME_ENV_KEYS = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"] as const;
1257-
// Colon-separated search-path variables the AppImage runtime points at its
1258-
// temporary mount (e.g. /tmp/.mount_T3-XXXX/usr/bin, the bundled glib schemas,
1259-
// and an $APPDIR/usr/share XDG data entry). Only the mount segments are
1260-
// dropped; the user's real entries are preserved. When nothing but mount
1261-
// segments remain the variable is removed entirely so consumers fall back to
1262-
// their platform default (e.g. gsettings finds the host schemas instead of
1263-
// reporting "No schemas installed"). See issues #1699 and #5059.
1264-
const APPIMAGE_PATH_LIKE_ENV_KEYS = [
1265-
"PATH",
1266-
"LD_LIBRARY_PATH",
1267-
"XDG_DATA_DIRS",
1268-
"GSETTINGS_SCHEMA_DIR",
1269-
] as const;
1270-
1271-
function isPathSegmentUnderAppDir(segment: string, appDir: string): boolean {
1272-
return segment === appDir || segment.startsWith(`${appDir}/`);
1273-
}
1274-
1275-
// On Linux AppImage builds the runtime mounts the app under a temporary dir and
1276-
// injects APPIMAGE/APPDIR/ARGV0/OWD plus mount entries on PATH/LD_LIBRARY_PATH.
1277-
// The integrated terminal inherits the server process environment, so without
1278-
// this scrub those leak into the PTY and tools resolve against the AppImage
1279-
// mount instead of the user's real environment (e.g. `php` reporting
1280-
// PHP_BINARY as the AppImage path). See issue #1699. The scrub is gated on an
1281-
// actual AppImage launch so non-AppImage environments are left untouched.
1282-
function stripAppImageRuntimeEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
1283-
if (env.APPIMAGE === undefined && env.APPDIR === undefined) return env;
1284-
1285-
const scrubbed: NodeJS.ProcessEnv = { ...env };
1286-
for (const key of APPIMAGE_RUNTIME_ENV_KEYS) {
1287-
delete scrubbed[key];
1288-
}
1289-
1290-
const appDir = env.APPDIR?.replace(/\/+$/, "");
1291-
if (appDir) {
1292-
for (const key of APPIMAGE_PATH_LIKE_ENV_KEYS) {
1293-
const value = scrubbed[key];
1294-
if (value === undefined) continue;
1295-
const kept = value
1296-
.split(":")
1297-
.filter((segment) => segment.length > 0 && !isPathSegmentUnderAppDir(segment, appDir));
1298-
if (kept.length > 0) {
1299-
scrubbed[key] = kept.join(":");
1300-
} else {
1301-
delete scrubbed[key];
1302-
}
1303-
}
1304-
}
1305-
1306-
return scrubbed;
1307-
}
1308-
13091254
function createTerminalSpawnEnv(
13101255
baseEnv: NodeJS.ProcessEnv,
13111256
runtimeEnv: Record<string, string> | null | undefined,

0 commit comments

Comments
 (0)