Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Run the owned composition browser as root in tests and name its startup failure

Claude29d432c714f1
diff --git a/server/services/htmlComposition/ownedBrowser.js b/server/services/htmlComposition/ownedBrowser.js
index dd7c52f3b7..dd38befa1b 100644
--- a/server/services/htmlComposition/ownedBrowser.js
+++ b/server/services/htmlComposition/ownedBrowser.js
@@ -5,6 +5,16 @@ import { spawn } from '../../lib/childProcess.js';
import { safeChildProcessOptions } from '../../lib/processEnv.js';
import { killWithEscalation } from '../../lib/killWithEscalation.js';
import { browserExecutablePath } from '../../lib/browserConfig.js';
+import { isVitestRunner } from '../../lib/runtimeEnv.js';
+
+// Chrome refuses to run as root with its sandbox on. Only a Vitest worker
+// running as root (a root dev/CI container) drops it, matching the test-owned
+// Chrome launches in testBrowserCleanup.js; production keeps the sandbox.
+const sandboxArgs = () => (isVitestRunner() && process.getuid?.() === 0 ? ['--no-sandbox'] : []);
+
+// Raw browser output can hold private paths, so a startup failure names a
+// recognized cause instead of copying stderr.
+const ROOT_SANDBOX_REFUSAL = /Running as root without --no-sandbox is not supported/;
// Chrome helpers (crashpad, GPU) can still be writing into the profile for a
// moment after the browser's close event, so a recursive rm may lose the race
@@ -105,7 +115,7 @@ export async function launchCompositionBrowser({ signal, startupMs = 20000, shut
try {
signal?.throwIfAborted();
proc = spawn(executable, [
- '--headless=new', '--remote-debugging-address=127.0.0.1', '--remote-debugging-port=0',
+ '--headless=new', ...sandboxArgs(), '--remote-debugging-address=127.0.0.1', '--remote-debugging-port=0',
`--user-data-dir=${profile}`, '--no-first-run', '--no-default-browser-check',
'--mute-audio', '--disable-background-networking', '--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows', '--disable-renderer-backgrounding', 'about:blank',
@@ -126,11 +136,15 @@ export async function launchCompositionBrowser({ signal, startupMs = 20000, shut
error ? reject(error) : resolve(endpoint);
};
const fail = error => finish(new Error(`Composition browser failed to start (${error.code || 'spawn error'})`));
- const stopped = () => finish(new Error('Composition browser exited during startup'));
+ const stopped = () => {
+ const status = proc.signalCode ? `signal ${proc.signalCode}` : `code ${proc.exitCode ?? 'unknown'}`;
+ const cause = ROOT_SANDBOX_REFUSAL.test(tail) ? ': Chrome refuses to run as root with its sandbox enabled' : '';
+ finish(new Error(`Composition browser exited during startup (${status})${cause}`));
+ };
const canceled = () => finish(signal.reason ?? new Error('Render canceled'));
const onData = bytes => {
tail = (tail + bytes.toString()).slice(-8192);
- // No paths or browser output are copied into errors. An endpoint must
+ // No paths or raw browser output are copied into errors. An endpoint must
// be loopback and come from this child's complete DevTools line.
const match = tail.match(/(?:^|\n)DevTools listening on (ws:\/\/127\.0\.0\.1:\d+\/devtools\/browser\/[a-zA-Z0-9-]+)\r?\n/);
if (match) finish(null, match[1]);
diff --git a/server/services/htmlComposition/ownedBrowser.test.js b/server/services/htmlComposition/ownedBrowser.test.js
index c6e66afbe5..a4bb35456e 100644
--- a/server/services/htmlComposition/ownedBrowser.test.js
+++ b/server/services/htmlComposition/ownedBrowser.test.js
@@ -37,10 +37,11 @@ beforeEach(() => {
});
vi.mocked(rm).mockClear();
});
-afterEach(() => { child.stderr.destroy(); });
+afterEach(() => { child.stderr.destroy(); vi.restoreAllMocks(); vi.unstubAllEnvs(); });
describe('owned composition capture browser lifecycle', () => {
it('uses the configured executable with a fresh sandboxed profile and disposes only its child', async () => {
+ vi.spyOn(process, 'getuid').mockReturnValue(1000);
const owner = await launchCompositionBrowser();
expect(owner.webSocketDebuggerUrl).toBe(endpoint);
const [executable, args, options] = launch.spawn.mock.calls[0];
@@ -55,6 +56,27 @@ describe('owned composition capture browser lifecycle', () => {
await expect(access(profile)).rejects.toThrow();
});
+ it.each([
+ ['a root Vitest worker drops', {}, true],
+ ['a root production launch keeps', { VITEST: undefined, NODE_ENV: 'production' }, false],
+ ])('%s the sandbox Chrome refuses to start as root', async (_, env, unsandboxed) => {
+ vi.spyOn(process, 'getuid').mockReturnValue(0);
+ for (const [name, value] of Object.entries(env)) vi.stubEnv(name, value);
+ await (await launchCompositionBrowser()).close();
+ expect(launch.spawn.mock.calls[0][1].includes('--no-sandbox')).toBe(unsandboxed);
+ });
+
+ it('names a recognized startup exit cause without copying browser output', async () => {
+ startup = () => {
+ child.emit('spawn');
+ child.stderr.write('[0101/000000:ERROR:zygote_host_impl_linux.cc(101)] Running as root without --no-sandbox is not supported. /private/profile/path\n');
+ setImmediate(() => { child.exitCode = 1; child.emit('exit', 1, null); child.emit('close', 1, null); });
+ };
+ const error = await launchCompositionBrowser().catch(caught => caught);
+ expect(error.message).toBe('Composition browser exited during startup (code 1): Chrome refuses to run as root with its sandbox enabled');
+ await expect(access(profile)).rejects.toThrow();
+ });
+
it('cleans a child and profile after a bounded startup timeout without leaking stderr', async () => {
startup = () => { child.emit('spawn'); child.stderr.write('private profile path and diagnostics'); };
await expect(launchCompositionBrowser({ startupMs: 20 })).rejects.toThrow('startup exceeded its deadline');