Skip to content

Commit 4dac02b

Browse files
committed
fix: pass --no-sandbox to agent-browser on CI and in containers
The QA runner and the pane's shared-session backend drive agent-browser, which only hints at --args "--no-sandbox" — it does not apply it. On Linux CI the engine's Chrome died before the first command returned, so test:qa failed with "engine command failed (1)". Factor the sandbox detection into a shared module and set AGENT_BROWSER_ARGS for agent-browser invocations, matching the direct launch-mode fix.
1 parent 6be6ed7 commit 4dac02b

4 files changed

Lines changed: 57 additions & 30 deletions

File tree

‎bin/chromium-args.mjs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Shared Chromium sandbox fallback for CI and containers.
2+
//
3+
// Chrome's sandbox needs a setuid helper or unprivileged user namespaces;
4+
// root, CI runners, and containers often have neither, so Chrome dies before
5+
// writing DevToolsActivePort and any launch times out. Mirror the signals
6+
// agent-browser keys on and fall back to --no-sandbox there. The companion
7+
// --disable-dev-shm-usage moves shared memory off a tiny container /dev/shm,
8+
// which crashes Chrome the same way. Non-root desktop boxes keep the sandbox.
9+
import fs from "node:fs";
10+
11+
export function chromiumSandboxArgs(env = process.env) {
12+
const root = typeof process.getuid === "function" && process.getuid() === 0;
13+
let container = false;
14+
if (env.CI) {
15+
container = true;
16+
} else {
17+
try {
18+
container =
19+
fs.existsSync("/.dockerenv") ||
20+
fs.existsSync("/run/.containerenv") ||
21+
/(?:docker|lxc|kubepods|containerd)/i.test(
22+
fs.readFileSync("/proc/1/cgroup", "utf8"),
23+
);
24+
} catch {
25+
container = false;
26+
}
27+
}
28+
if (!root && !container) return [];
29+
return container ? ["--no-sandbox", "--disable-dev-shm-usage"] : ["--no-sandbox"];
30+
}
31+
32+
// agent-browser reads AGENT_BROWSER_ARGS (comma or newline separated) for the
33+
// same purpose; the pane's shared-session backend and the QA runner both drive
34+
// agent-browser, so both need it on CI/container boxes.
35+
export function agentBrowserSandboxEnv(env = process.env) {
36+
const args = chromiumSandboxArgs(env);
37+
return args.length ? { AGENT_BROWSER_ARGS: args.join(",") } : {};
38+
}

‎bin/qa.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { randomUUID, createHash } from "node:crypto";
77
import { execFile } from "node:child_process";
88
import { promisify } from "node:util";
99
import { pathToFileURL } from "node:url";
10+
import { agentBrowserSandboxEnv } from "./chromium-args.mjs";
1011

1112
const execute = promisify(execFile);
1213
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
@@ -134,6 +135,10 @@ export function engineEnvironment(env, timeoutMs) {
134135
}
135136
result.AGENT_BROWSER_DEFAULT_TIMEOUT = String(timeoutMs);
136137
result.AGENT_BROWSER_IDLE_TIMEOUT_MS = "60000";
138+
// On CI/container boxes the engine needs --no-sandbox or its Chrome dies
139+
// before the first command returns. Only the launch-args knob is added;
140+
// nothing else from the host environment leaks into the fresh session.
141+
Object.assign(result, agentBrowserSandboxEnv(env));
137142
return result;
138143
}
139144

‎bin/renderer.mjs‎

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import fs from "node:fs";
1010
import path from "node:path";
1111
import { makeCdpBrowser, cdpSupported, redactWsUrl } from "./cdp.mjs";
1212
import { formatRequests, parseAgentSnapshot, diffSnapshot } from "./page-text.mjs";
13+
import { chromiumSandboxArgs, agentBrowserSandboxEnv } from "./chromium-args.mjs";
14+
15+
export { chromiumSandboxArgs } from "./chromium-args.mjs";
1316

1417
const pExecFile = promisify(execFile);
1518
const ESC = "\x1b";
@@ -128,33 +131,6 @@ export function findChromium(env, configDirValue, probe) {
128131
return null;
129132
}
130133

131-
// Chrome's sandbox needs a setuid helper or unprivileged user namespaces.
132-
// Root, CI runners, and containers often have neither, so Chrome dies before
133-
// writing DevToolsActivePort — the launch then times out. Mirror the signals
134-
// agent-browser keys on and fall back to --no-sandbox there. The companion
135-
// --disable-dev-shm-usage moves shared memory off a tiny container /dev/shm,
136-
// which crashes Chrome the same way.
137-
export function chromiumSandboxArgs(env = process.env) {
138-
const root = typeof process.getuid === "function" && process.getuid() === 0;
139-
let container = false;
140-
if (env.CI) {
141-
container = true;
142-
} else {
143-
try {
144-
container =
145-
fs.existsSync("/.dockerenv") ||
146-
fs.existsSync("/run/.containerenv") ||
147-
/(?:docker|lxc|kubepods|containerd)/i.test(
148-
fs.readFileSync("/proc/1/cgroup", "utf8"),
149-
);
150-
} catch {
151-
container = false;
152-
}
153-
}
154-
if (!root && !container) return [];
155-
return container ? ["--no-sandbox", "--disable-dev-shm-usage"] : ["--no-sandbox"];
156-
}
157-
158134
// Render-mode precedence: explicit config > kitty probe > symbols > text.
159135
// probeResponse is the raw bytes the terminal answered to a kitty graphics
160136
// query; empty/undefined means no answer (not supported). Kitty mode emits
@@ -465,6 +441,9 @@ export function makeBrowser(session, bin = "agent-browser", env = process.env) {
465441
const maxBuffer = 16 * 1024 * 1024;
466442
// Inherit the caller's configuration unchanged. Injecting an idle timeout
467443
// makes agent-browser restart an existing daemon and lose its browser.
444+
// On CI/container boxes agent-browser needs AGENT_BROWSER_ARGS to fall back
445+
// to --no-sandbox, or its Chrome dies before any command returns.
446+
const runEnv = { ...env, ...agentBrowserSandboxEnv(env) };
468447
const parse = (stdout, what) => {
469448
let parsed;
470449
try {
@@ -482,7 +461,7 @@ export function makeBrowser(session, bin = "agent-browser", env = process.env) {
482461
const { stdout } = await pExecFile(
483462
bin,
484463
["--session", session, "batch", "--bail", "--json", ...cmds],
485-
{ timeout, maxBuffer, env },
464+
{ timeout, maxBuffer, env: runEnv },
486465
);
487466
const arr = parse(stdout, "batch output");
488467
for (const r of arr) {
@@ -500,7 +479,7 @@ export function makeBrowser(session, bin = "agent-browser", env = process.env) {
500479
{
501480
timeout: 10_000,
502481
maxBuffer,
503-
env,
482+
env: runEnv,
504483
},
505484
);
506485
const parsed = parse(stdout, "output");
@@ -599,7 +578,7 @@ export function makeBrowser(session, bin = "agent-browser", env = process.env) {
599578
const { stdout } = await pExecFile(bin, ["session", "list", "--json"], {
600579
timeout: 10_000,
601580
maxBuffer,
602-
env,
581+
env: runEnv,
603582
});
604583
// Exact match only: a substring hit (herdr-ws-w2 vs herdr-ws-w22)
605584
// would make the pane attach and auto-create a session it must not.

‎tests/renderer.test.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
chromiumSandboxArgs,
3333
truthyConfig,
3434
} from "../bin/renderer.mjs";
35+
import { agentBrowserSandboxEnv } from "../bin/chromium-args.mjs";
3536

3637
const repoRoot = path.resolve(
3738
path.dirname(fileURLToPath(import.meta.url)),
@@ -2697,6 +2698,10 @@ test("chromiumSandboxArgs: CI gets the sandbox fallback, a clean box does not",
26972698
assert.ok(args.includes("--disable-dev-shm-usage"), "CI gets --disable-dev-shm-usage");
26982699
// A non-root, non-CI desktop box (no container markers) keeps the sandbox.
26992700
assert.deepEqual(chromiumSandboxArgs({}), []);
2701+
assert.deepEqual(agentBrowserSandboxEnv({}), {});
2702+
assert.deepEqual(agentBrowserSandboxEnv({ CI: "true" }), {
2703+
AGENT_BROWSER_ARGS: "--no-sandbox,--disable-dev-shm-usage",
2704+
});
27002705
});
27012706

27022707
const fakeChromiumScript = () => {

0 commit comments

Comments
 (0)