Skip to content

Commit 2924577

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 cfafb4b commit 2924577

5 files changed

Lines changed: 118 additions & 49 deletions

File tree

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

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

5858
import * as ServerConfig from "../config.ts";
59+
import { stripAppImageRuntimeEnv } from "@t3tools/provider-core/server/appImageRuntimeEnv";
5960
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
6061
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
6162
import { makeClaudeEnvironment } from "../provider/Drivers/ClaudeHome.ts";
@@ -1208,51 +1209,6 @@ function shouldExcludeTerminalEnvKey(key: string): boolean {
12081209
return TERMINAL_ENV_BLOCKLIST.has(normalizedKey);
12091210
}
12101211

1211-
const APPIMAGE_RUNTIME_ENV_KEYS = ["APPIMAGE", "APPDIR", "ARGV0", "OWD"] as const;
1212-
const APPIMAGE_PATH_LIKE_ENV_KEYS = [
1213-
"PATH",
1214-
"LD_LIBRARY_PATH",
1215-
"XDG_DATA_DIRS",
1216-
"GSETTINGS_SCHEMA_DIR",
1217-
] as const;
1218-
1219-
function isPathSegmentUnderAppDir(segment: string, appDir: string): boolean {
1220-
return segment === appDir || segment.startsWith(`${appDir}/`);
1221-
}
1222-
1223-
// On Linux AppImage builds the runtime mounts the app under a temporary dir and
1224-
// injects APPIMAGE/APPDIR/ARGV0/OWD plus mount entries on PATH/LD_LIBRARY_PATH.
1225-
// The integrated terminal inherits the server process environment, so without
1226-
// this scrub those leak into the PTY and tools resolve against the AppImage
1227-
// mount instead of the user's real environment (e.g. `php` reporting PHP_BINARY
1228-
// as the AppImage path). See issue #1699.
1229-
function stripAppImageRuntimeEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
1230-
if (env.APPIMAGE === undefined && env.APPDIR === undefined) return env;
1231-
1232-
const scrubbed: NodeJS.ProcessEnv = { ...env };
1233-
for (const key of APPIMAGE_RUNTIME_ENV_KEYS) {
1234-
delete scrubbed[key];
1235-
}
1236-
1237-
const appDir = env.APPDIR?.replace(/\/+$/, "");
1238-
if (appDir) {
1239-
for (const key of APPIMAGE_PATH_LIKE_ENV_KEYS) {
1240-
const value = scrubbed[key];
1241-
if (value === undefined) continue;
1242-
const kept = value
1243-
.split(":")
1244-
.filter((segment) => segment.length > 0 && !isPathSegmentUnderAppDir(segment, appDir));
1245-
if (kept.length > 0) {
1246-
scrubbed[key] = kept.join(":");
1247-
} else {
1248-
delete scrubbed[key];
1249-
}
1250-
}
1251-
}
1252-
1253-
return scrubbed;
1254-
}
1255-
12561212
function createTerminalSpawnEnv(
12571213
baseEnv: NodeJS.ProcessEnv,
12581214
runtimeEnv: Record<string, string> | null | undefined,

‎packages/provider-core/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"types": "./src/server/adapterDriver.ts",
1212
"import": "./src/server/adapterDriver.ts"
1313
},
14+
"./server/appImageRuntimeEnv": {
15+
"types": "./src/server/appImageRuntimeEnv.ts",
16+
"import": "./src/server/appImageRuntimeEnv.ts"
17+
},
1418
"./server/attachmentPrompt": {
1519
"types": "./src/server/attachmentPrompt.ts",
1620
"import": "./src/server/attachmentPrompt.ts"
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+
}

‎packages/provider-core/src/server/instanceEnvironment.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,53 @@ describe("mergeProviderInstanceEnvironment", () => {
7171
});
7272
}),
7373
);
74+
75+
it.effect("keeps the AppImage runtime and the Electron-as-Node flag out of agent sessions", () =>
76+
Effect.gen(function* () {
77+
const appDir = "/tmp/.mount_T3-Codeabc123";
78+
79+
expect(
80+
yield* mergeProviderInstanceEnvironment(undefined, {
81+
APPIMAGE: "/home/user/T3-Code.AppImage",
82+
APPDIR: appDir,
83+
ARGV0: "/home/user/T3-Code.AppImage",
84+
OWD: "/home/user/project",
85+
ELECTRON_RUN_AS_NODE: "1",
86+
PATH: `${appDir}:${appDir}/usr/sbin:/usr/local/bin:/usr/bin`,
87+
LD_LIBRARY_PATH: `${appDir}/usr/lib`,
88+
HOME: "/home/user",
89+
}),
90+
).toEqual({ PATH: "/usr/local/bin:/usr/bin", HOME: "/home/user" });
91+
}),
92+
);
93+
94+
it.effect("drops only the Electron-as-Node flag outside an AppImage", () =>
95+
Effect.gen(function* () {
96+
expect(
97+
yield* mergeProviderInstanceEnvironment([], {
98+
ELECTRON_RUN_AS_NODE: "1",
99+
OWD: "/home/user/keep-this",
100+
PATH: "/usr/bin",
101+
}),
102+
).toEqual({ OWD: "/home/user/keep-this", PATH: "/usr/bin" });
103+
}),
104+
);
105+
106+
it.effect("applies provider settings after the scrub", () =>
107+
Effect.gen(function* () {
108+
expect(
109+
yield* mergeProviderInstanceEnvironment(
110+
[
111+
{ name: "ELECTRON_RUN_AS_NODE", value: "1", sensitive: false },
112+
{ name: "APPDIR", value: "/opt/configured", sensitive: false },
113+
],
114+
{
115+
APPIMAGE: "/home/user/T3-Code.AppImage",
116+
APPDIR: "/tmp/.mount_T3-x",
117+
PATH: "/usr/bin",
118+
},
119+
),
120+
).toEqual({ ELECTRON_RUN_AS_NODE: "1", APPDIR: "/opt/configured", PATH: "/usr/bin" });
121+
}),
122+
);
74123
});

‎packages/provider-core/src/server/instanceEnvironment.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import type { ProviderInstanceEnvironment } from "@t3tools/contracts";
22
import * as HostProcess from "@t3tools/shared/HostProcess";
33
import * as Effect from "effect/Effect";
44

5+
import { stripAppImageRuntimeEnv } from "./appImageRuntimeEnv.ts";
56
import { expandHomePath } from "./pathExpansion.ts";
67

78
export const mergeProviderInstanceEnvironment = Effect.fn(function* (
89
environment: ProviderInstanceEnvironment | undefined,
910
baseEnv: NodeJS.ProcessEnv = process.env,
1011
) {
11-
if (!environment || environment.length === 0) {
12-
return baseEnv;
13-
}
12+
// Agents and the shells they start get the user's environment, not the
13+
// AppImage runtime's. The packaged server runs as Electron-as-Node; an agent
14+
// that inherits that flag starts Node when it re-executes the app binary.
15+
// Spawns that need it set it explicitly.
16+
const next: NodeJS.ProcessEnv = { ...stripAppImageRuntimeEnv(baseEnv) };
17+
delete next.ELECTRON_RUN_AS_NODE;
18+
if (!environment || environment.length === 0) return next;
1419

1520
const home = yield* HostProcess.HomeDirectory;
16-
const next: NodeJS.ProcessEnv = { ...baseEnv };
1721
for (const variable of environment) {
1822
// Child processes do not apply shell expansion to environment values.
1923
next[variable.name] =

0 commit comments

Comments
 (0)