Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(cli): give the login-shell PATH probe its own deadline and a 1 MiB output cap
AdityaUmale75935bae2829
diff --git a/server/env-path.test.ts b/server/env-path.test.tsindex ebce88ccbb..1434c6e644 100644--- a/server/env-path.test.ts+++ b/server/env-path.test.ts@@ -1,5 +1,5 @@import { execFile } from "node:child_process";-import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { homedir, tmpdir } from "node:os";import { delimiter, join, relative } from "node:path";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";@@ -136,6 +136,81 @@ describe("augmentedPath", () => {}});+ posixIt("uses the login-shell PATH once the shell exits, even when its rc leaves a job holding stdout open", async () => {+ const shell = join(homedir(), "fake-login-shell");+ const rcOnlyBin = join(homedir(), "rc-only", "bin");+ const jobPid = join(homedir(), "rc-job.pid");+ writeFileSync(shell, `#!/bin/sh\nprintf '__OMB_PATH__%s' '${rcOnlyBin}'\nsleep 30 &\necho $! > '${jobPid}'\n`);+ chmodSync(shell, 0o755);++ const saved = { SHELL: process.env.SHELL, VITEST: process.env.VITEST };+ try {+ process.env.SHELL = shell;+ delete process.env.VITEST;+ resetPathCacheForTests();++ augmentedPath();+ await vi.waitFor(() => expect(augmentedPath().split(delimiter)).toContain(rcOnlyBin), { timeout: 3000 });+ } finally {+ if (existsSync(jobPid)) {+ try {+ process.kill(Number(readFileSync(jobPid, "utf8").trim()));+ } catch {}+ }+ if (saved.SHELL === undefined) delete process.env.SHELL;+ else process.env.SHELL = saved.SHELL;+ if (saved.VITEST === undefined) delete process.env.VITEST;+ else process.env.VITEST = saved.VITEST;+ resetPathCacheForTests();+ }+ });++ posixIt("drops a login-shell probe whose output passes 1 MiB", async () => {+ const shell = join(homedir(), "fake-login-shell");+ const rcOnlyBin = join(homedir(), "rc-only", "bin");+ const shellPid = join(homedir(), "flood-shell.pid");+ const finished = join(homedir(), "flood-finished");+ writeFileSync(+ shell,+ `#!/bin/sh\necho $$ > '${shellPid}'\nprintf '__OMB_PATH__%s\\n' '${rcOnlyBin}'\nhead -c 2000000 /dev/zero\n: > '${finished}'\n`,+ );+ chmodSync(shell, 0o755);+ const alive = (pid: number) => {+ try {+ process.kill(pid, 0);+ return true;+ } catch {+ return false;+ }+ };++ const saved = { SHELL: process.env.SHELL, VITEST: process.env.VITEST };+ try {+ process.env.SHELL = shell;+ delete process.env.VITEST;+ resetPathCacheForTests();++ augmentedPath();+ const pid = await vi.waitFor(+ () => {+ const written = existsSync(shellPid) ? Number(readFileSync(shellPid, "utf8").trim()) : 0;+ expect(written).toBeGreaterThan(0);+ return written;+ },+ { timeout: 5000 },+ );+ await vi.waitFor(() => expect(alive(pid)).toBe(false), { timeout: 5000 });+ expect(existsSync(finished)).toBe(false);+ expect(augmentedPath().split(delimiter)).not.toContain(rcOnlyBin);+ } finally {+ if (saved.SHELL === undefined) delete process.env.SHELL;+ else process.env.SHELL = saved.SHELL;+ if (saved.VITEST === undefined) delete process.env.VITEST;+ else process.env.VITEST = saved.VITEST;+ resetPathCacheForTests();+ }+ });+posixIt("keeps the login-shell PATH in the same order after a rescan", async () => {const knownBin = join(homedir(), ".local", "bin");mkdirSync(knownBin, { recursive: true });diff --git a/server/env-path.ts b/server/env-path.tsindex d686803938..f039dc9dc6 100644--- a/server/env-path.ts+++ b/server/env-path.ts@@ -134,6 +134,9 @@ function mergePaths(parts: string[]): string {return [...new Set(parts.filter(Boolean))].join(delimiter);}+const PROBE_TIMEOUT_MS = 5000;+const PROBE_MAX_OUTPUT_BYTES = 1024 * 1024;+function probeLoginShellPath(): void {const shell = process.env.SHELL || "/bin/zsh";// -l -i: nvm and friends live in .zshrc/.bashrc, which only interactive shells read; a marker isolates $PATH from rc noise.@@ -141,20 +144,46 @@ function probeLoginShellPath(): void {const child = spawn(shell, ["-l", "-i", "-c", 'printf "__OMB_PATH__%s" "$PATH"'], {detached: true,stdio: ["ignore", "pipe", "ignore"],- timeout: 5000,});let stdout = "";- child.stdout.setEncoding("utf8").on("data", (chunk: string) => {- stdout += chunk;- });- child.on("error", () => {});- child.on("close", (code) => {- if (code !== 0 || !stdout) return;+ let bytes = 0;+ let exitCode: number | null | undefined;+ let finished = false;+ const finish = (): void => {+ if (finished) return;+ finished = true;+ clearTimeout(deadline);+ child.stdout.destroy();+ // An interactive shell ignores SIGTERM, and something its rc started in the background can hold stdout open after it exits.+ if (exitCode === undefined && child.pid) {+ try {+ process.kill(-child.pid, "SIGKILL");+ } catch {}+ }+ };+ const deadline = setTimeout(finish, PROBE_TIMEOUT_MS);+ deadline.unref();+ // Applied as soon as the shell has exited cleanly, not when stdout closes: its rc may leave a background job holding it.+ const applyIfDone = (): void => {+ if (exitCode !== 0) return;const m = /__OMB_PATH__([^\n]*)/.exec(stdout);- if (!m || !m[1]) return;+ if (!m || !m[1] || m[1] === loginShellPath) return;loginShellPath = m[1];cached = buildPath();+ };+ child.stdout.setEncoding("utf8").on("data", (chunk: string) => {+ bytes += Buffer.byteLength(chunk);+ if (bytes > PROBE_MAX_OUTPUT_BYTES) return finish();+ stdout += chunk;+ applyIfDone();+ });+ child.on("error", finish);+ child.on("exit", (code) => {+ exitCode = code;+ if (code === 0) applyIfDone();+ else finish();});+ child.on("close", finish);}export function resetPathCacheForTests(): void {