Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat(uploader): record the project's env names, host, shell and excluded files

ogsamrat81b31ffff039
diff --git a/apps/server/src/command-guard.test.ts b/apps/server/src/command-guard.test.ts
index 57b5a571..26a3a6c7 100644
--- a/apps/server/src/command-guard.test.ts
+++ b/apps/server/src/command-guard.test.ts
@@ -118,7 +118,8 @@ test.skipIf(process.platform === "win32")("the toolchain never runs a /usr/bin s
});
assert.deepEqual(result.versions, { node: "v22.19.0", uv: "uv 0.5.0" });
assert.deepEqual(result.skipped, { python3: "xcode_clt_missing", gcc: "xcode_clt_missing", clang: "xcode_clt_missing", pip_freeze: "xcode_clt_missing" });
- assert.ok(spawned.every((line) => !line.startsWith("/usr/bin/") && !line.includes("--python /usr/bin")), spawned.join("\n"));
+ // /usr/bin/sw_vers is part of macOS, not a Command Line Tools shim.
+ assert.ok(spawned.every((line) => !/^\/usr\/bin\/(python3|gcc|clang)\b/.test(line) && !line.includes("--python /usr/bin")), spawned.join("\n"));
assert.ok(spawned.includes("/opt/homebrew/bin/node -v"));
assert.equal(result.pip_freeze, undefined);
} finally {
diff --git a/apps/server/src/excluded-files.test.ts b/apps/server/src/excluded-files.test.ts
new file mode 100644
index 00000000..9e6dbf10
--- /dev/null
+++ b/apps/server/src/excluded-files.test.ts
@@ -0,0 +1,133 @@
+// @ts-nocheck -- ported from the CLI suite; exercises loosely typed fakes.
+import { test } from "bun:test";
+import assert from "node:assert/strict";
+import { createHash } from "node:crypto";
+import { execFileSync } from "node:child_process";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import zlib from "node:zlib";
+
+import * as excludedFiles from "./excluded-files.js";
+import * as uploader from "./session-uploader.js";
+
+const hasZstd = typeof zlib.zstdDecompressSync === "function";
+const sha = (data) => createHash("sha256").update(data).digest("hex");
+
+function project(files) {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "omnirush-excl-"));
+ for (const [name, content] of Object.entries(files)) {
+ fs.mkdirSync(path.dirname(path.join(root, name)), { recursive: true });
+ fs.writeFileSync(path.join(root, name), content);
+ }
+ return root;
+}
+
+test("ExcludedFiles: sizes and digests filled in, privacy never hashed, the first reason wins, capped with a count", async () => {
+ const root = project({ "big.log": "x".repeat(1000), ".env": "API_KEY=sk-secret\n", "img.png": Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 1, 2]) });
+ const hashes = new Map();
+ const excluded = new excludedFiles.ExcludedFiles(root, { uploadPath: (p) => p, hashes });
+ excluded.add(".env", "privacy");
+ excluded.add("big.log", "too_large");
+ excluded.add("big.log", "gitignored");
+ excluded.add("img.png", "binary", { size: 7 });
+ excluded.add("node_modules/", "gitignored");
+ excluded.add("gone.txt", "snapshot_cap");
+ const block = await excluded.finish();
+ assert.deepEqual(block.entries, [
+ { path: ".env", size: 18, sha256: null, reason: "privacy" },
+ { path: "big.log", size: 1000, sha256: sha("x".repeat(1000)), reason: "too_large" },
+ { path: "img.png", size: 7, sha256: sha(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 1, 2])), reason: "binary" },
+ { path: "node_modules/", size: null, sha256: null, reason: "gitignored" },
+ { path: "gone.txt", size: null, sha256: null, reason: "snapshot_cap" },
+ ]);
+ assert.deepEqual(block.counts, { privacy: 1, too_large: 1, binary: 1, gitignored: 1, snapshot_cap: 1 });
+ assert.equal(block.truncated_count, 0);
+
+ // A digest is reused while size, mtime and inode stay put.
+ const stamp = hashes.get("big.log").stamp;
+ hashes.set("big.log", { stamp, sha256: "f".repeat(64) });
+ const again = new excludedFiles.ExcludedFiles(root, { uploadPath: (p) => p, hashes });
+ again.add("big.log", "too_large");
+ assert.equal((await again.finish()).entries[0].sha256, "f".repeat(64));
+
+ // Past the read budget: listed without a digest.
+ const thrifty = new excludedFiles.ExcludedFiles(root, { uploadPath: (p) => p, hashBudgetBytes: 10 });
+ thrifty.add("big.log", "too_large");
+ assert.deepEqual((await thrifty.finish()).entries[0], { path: "big.log", size: 1000, sha256: null, reason: "too_large" });
+
+ const many = new excludedFiles.ExcludedFiles(root, { uploadPath: (p) => p });
+ for (let i = 0; i < 5200; i += 1) many.add(`f${i}.bin`, "binary", { size: 1, sha256: "a".repeat(64) });
+ const capped = await many.finish();
+ assert.equal(capped.entries.length, 5000);
+ assert.equal(capped.truncated_count, 200);
+ assert.equal(capped.counts.binary, 5200);
+});
+
+async function snapshots(root, options = {}) {
+ const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "omnirush-excl-state-"));
+ const envelopes = [];
+ const workspaceSync = new uploader.SessionUploader({
+ stateDir,
+ fallbackScanMs: 60_000,
+ toolchain: false,
+ capabilities: async () => ({ schema_versions: [2], canonical_trace: false }),
+ upload: async (_sessionId, compressed) => {
+ envelopes.push(JSON.parse(zlib.zstdDecompressSync(Buffer.from(compressed)).toString("utf8")));
+ return new Response("{}", { status: 201 });
+ },
+ ...options,
+ });
+ const sessionId = "01a0dcd2-d8ee-7222-80eb-240063770433";
+ workspaceSync.startSession(sessionId, "ws", root);
+ await workspaceSync.idle(sessionId);
+ await workspaceSync.stop();
+ return envelopes;
+}
+
+test.skipIf(!hasZstd || process.platform === "win32")("a start snapshot lists what it left out, and privacy counts binaries and oversized files", async () => {
+ const big = "y".repeat(5 * 1024 * 1024);
+ const png = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0]), Buffer.alloc(64)]);
+ const root = project({
+ "README.md": "# demo\n",
+ ".gitignore": "dist/\n*.log\n.env\n",
+ ".env": "API_KEY=sk-proj-abcdefghijklmnopqrstuvwxyz0123456789ABCD\n",
+ "id_rsa": "-----BEGIN OPENSSH PRIVATE KEY-----\nabc\n-----END OPENSSH PRIVATE KEY-----\n",
+ "data/huge.csv": big,
+ "assets/logo.png": png,
+ "dist/bundle.js": "built\n",
+ "debug.log": "log line\n",
+ });
+ execFileSync("git", ["init", "-q"], { cwd: root });
+ const envelopes = await snapshots(root);
+ const start = envelopes.find((envelope) => envelope.snapshot_type === "start");
+ const byPath = Object.fromEntries(start.excluded_files.entries.map((entry) => [entry.path, entry]));
+ assert.deepEqual(byPath["data/huge.csv"], { path: "data/huge.csv", size: big.length, sha256: sha(big), reason: "too_large" });
+ assert.deepEqual(byPath["assets/logo.png"], { path: "assets/logo.png", size: png.length, sha256: sha(png), reason: "binary" });
+ assert.equal(byPath["id_rsa"].reason, "privacy");
+ assert.equal(byPath["id_rsa"].sha256, null);
+ assert.equal(byPath[".env"].reason, "privacy");
+ assert.equal(byPath[".env"].sha256, null);
+ assert.deepEqual(byPath["dist/"], { path: "dist/", size: null, sha256: null, reason: "gitignored" });
+ assert.deepEqual(byPath["debug.log"], { path: "debug.log", size: 9, sha256: sha("log line\n"), reason: "gitignored" });
+ assert.equal(byPath["README.md"], undefined);
+ assert.equal(start.excluded_files.truncated_count, 0);
+ assert.equal(start.privacy.binary_file_count, 1);
+ assert.equal(start.privacy.too_large_file_count, 1);
+ const text = JSON.stringify(envelopes);
+ assert.ok(!text.includes("sk-proj-abcdefghij"));
+ assert.ok(!text.includes("OPENSSH PRIVATE KEY-----\nabc"));
+});
+
+test.skipIf(!hasZstd || process.platform === "win32")("files over the snapshot's byte budget are listed as snapshot_cap with the manifest digest", async () => {
+ const large = "z".repeat(700 * 1024);
+ const root = project({ "README.md": "# demo\n", "notes/large.txt": large });
+ const envelopes = await snapshots(root, { snapshotMaxBytes: 1024 * 1024 + 400 * 1024 });
+ const start = envelopes.find((envelope) => envelope.snapshot_type === "start");
+ const entry = start.excluded_files.entries.find((item) => item.path === "notes/large.txt");
+ assert.equal(entry.reason, "snapshot_cap");
+ assert.equal(entry.size, large.length);
+ assert.equal(entry.sha256, start.manifest.find((item) => item.path === "notes/large.txt").sha256);
+ assert.ok(start.privacy.snapshot_cap_omitted_count >= 1);
+ assert.ok(!start.files.some((file) => file.path === "notes/large.txt"));
+});
diff --git a/apps/server/src/excluded-files.ts b/apps/server/src/excluded-files.ts
new file mode 100644
index 00000000..1f860fb5
--- /dev/null
+++ b/apps/server/src/excluded-files.ts
@@ -0,0 +1,135 @@
+// The files a snapshot left out, so a replay knows what it is missing:
+// `excluded_files` on every snapshot envelope, written after files[] (when
+// the snapshot cap's omissions are known).
+//
+// Each entry names the path (in its upload form, scrubbed like every other
+// path), its size, the SHA-256 of its bytes on disk and why it was left out:
+// privacy a denylisted path (credentials, keys, app data); never hashed
+// too_large over the per-file cap
+// binary not text
+// gitignored ignored by git (a wholly ignored folder is one entry, `dir/`)
+// snapshot_cap over the file-count or byte budget of the snapshot (the
+// SHA-256 is the manifest's, of the redacted text)
+// At most MAX_EXCLUDED_ENTRIES are listed; `truncated_count` says how many
+// more there were, and `counts` has every reason's full total. A file is
+// hashed only when it is at most MAX_EXCLUDED_HASH_FILE_BYTES, within a
+// per-snapshot read budget, and a hash is reused while the file's size,
+// mtime and inode stay the same.
+
+import { createHash } from "node:crypto";
+import { execFile } from "node:child_process";
+import { createReadStream } from "node:fs";
+import { lstat } from "node:fs/promises";
+import nodePath from "node:path";
+
+import { gitSkipReason } from "./command-guard.js";
+
+export type ExcludedReason = "privacy" | "too_large" | "binary" | "gitignored" | "snapshot_cap";
+export type ExcludedEntry = { path: string; size: number | null; sha256: string | null; reason: ExcludedReason };
+export type ExcludedFilesBlock = {
+ schema: 1;
+ entries: ExcludedEntry[];
+ truncated_count: number;
+ counts: Partial<Record<ExcludedReason, number>>;
+};
+
+export const MAX_EXCLUDED_ENTRIES = 5_000;
+export const MAX_EXCLUDED_HASH_FILE_BYTES = 64 * 1024 * 1024;
+/** Bytes read for hashing per snapshot; past it, entries go without a SHA-256. */
+export const EXCLUDED_HASH_BUDGET_BYTES = 256 * 1024 * 1024;
+const MAX_GITIGNORED_LISTED = 20_000;
+/** The serialized block's cap; the snapshot reserves this much of its budget. */
+export const MAX_EXCLUDED_FILES_BYTES = 1024 * 1024;
+
+type Pending = { path: string; reason: ExcludedReason; size?: number | null; sha256?: string | null };
+export type HashCache = Map<string, { stamp: string; sha256: string }>;
+
+export class ExcludedFiles {
+ private readonly pending = new Map<string, Pending>();
+ private readonly counts: Partial<Record<ExcludedReason, number>> = {};
+
+ constructor(
+ private readonly root: string,
+ private readonly options: { uploadPath: (path: string) => string; hashes?: HashCache; hashBudgetBytes?: number },
+ ) {}
+
+ /** Records one left-out file; the first reason given for a path wins. Every call counts. */
+ add(path: string, reason: ExcludedReason, info: { size?: number | null; sha256?: string | null } = {}): void {
+ if (!path) return;
+ if (this.pending.has(path)) return;
+ this.counts[reason] = (this.counts[reason] ?? 0) + 1;
+ this.pending.set(path, { path, reason, ...info });
+ }
+
+ count(reason: ExcludedReason): number {
+ return this.counts[reason] ?? 0;
+ }
+
+ /** The block, with sizes and digests filled in (bounded; never throws). */
+ async finish(): Promise<ExcludedFilesBlock> {
+ const listed = [...this.pending.values()].slice(0, MAX_EXCLUDED_ENTRIES);
+ let budget = this.options.hashBudgetBytes ?? EXCLUDED_HASH_BUDGET_BYTES;
+ const entries: ExcludedEntry[] = [];
+ let bytes = 200;
+ for (const item of listed) {
+ const directory = item.path.endsWith("/");
+ let size = item.size ?? null;
+ let sha256 = item.sha256 || null;
+ if (!directory && (size === null || (sha256 === null && item.reason !== "privacy"))) {
+ const full = nodePath.join(this.root, item.path);
+ try {
+ const info = await lstat(full);
+ if (info.isFile()) {
+ size = info.size;
+ if (sha256 === null && item.reason !== "privacy" && info.size <= MAX_EXCLUDED_HASH_FILE_BYTES) {
+ const stamp = `${info.size}:${Math.trunc(info.mtimeMs)}:${info.ino}`;
+ const cached = this.options.hashes?.get(item.path);
+ if (cached && cached.stamp === stamp) sha256 = cached.sha256;
+ else if (info.size <= budget) {
+ budget -= info.size;
+ sha256 = await hashFile(full);
+ if (sha256) this.options.hashes?.set(item.path, { stamp, sha256 });
+ }
+ }
+ }
+ } catch {
+ // Gone since the listing: listed without size or digest.
+ }
+ }
+ const uploadPath = this.options.uploadPath(directory ? item.path.slice(0, -1) : item.path);
+ const entry: ExcludedEntry = { path: directory ? `${uploadPath}/` : uploadPath, size, sha256, reason: item.reason };
+ bytes += JSON.stringify(entry).length + 1;
+ if (bytes > MAX_EXCLUDED_FILES_BYTES) break;
+ entries.push(entry);
+ }
+ return { schema: 1, entries, truncated_count: Math.max(0, this.pending.size - entries.length), counts: { ...this.counts } };
+ }
+}
+
+function hashFile(path: string): Promise<string | null> {
+ return new Promise((resolvePromise) => {
+ const hash = createHash("sha256");
+ const stream = createReadStream(path);
+ stream.on("data", (chunk) => hash.update(chunk));
+ stream.on("error", () => resolvePromise(null));
+ stream.on("end", () => resolvePromise(hash.digest("hex")));
+ });
+}
+
+/**
+ * The untracked files git ignores under `root`, a wholly ignored folder as
+ * one `dir/` entry (`git ls-files --others --ignored --exclude-standard
+ * --directory`). Empty when git cannot answer or must not run.
+ */
+export async function listGitIgnored(root: string): Promise<string[]> {
+ if (await gitSkipReason()) return [];
+ return new Promise((resolvePromise) => {
+ execFile("git", ["-C", root, "ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"], {
+ encoding: "buffer", maxBuffer: 16 * 1024 * 1024, timeout: 15_000, windowsHide: true,
+ env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_OPTIONAL_LOCKS: "0" },
+ }, (error, stdout) => {
+ if (error) return resolvePromise([]);
+ resolvePromise(Buffer.from(stdout).toString("utf8").split("\0").filter(Boolean).slice(0, MAX_GITIGNORED_LISTED));
+ });
+ });
+}
diff --git a/apps/server/src/project-env.test.ts b/apps/server/src/project-env.test.ts
new file mode 100644
index 00000000..7365e196
--- /dev/null
+++ b/apps/server/src/project-env.test.ts
@@ -0,0 +1,268 @@
+// @ts-nocheck -- ported from the CLI suite; exercises loosely typed fakes.
+import { test } from "bun:test";
+import assert from "node:assert/strict";
+import { createHash } from "node:crypto";
+import { execFileSync } from "node:child_process";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import zlib from "node:zlib";
+
+import * as env from "./project-env.js";
+import * as toolchain from "./toolchain.js";
+import * as uploader from "./session-uploader.js";
+
+const hasZstd = typeof zlib.zstdDecompressSync === "function";
+const SECRETS = {
+ apiKey: "sk-proj-abcdefghijklmnopqrstuvwxyz0123456789ABCD",
+ dbPassword: "hunter2",
+ jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N_XgL0n3I9PlFUP0THsR8U",
+ pem: "MIIBOgIBAAJBAKj34GkxFhD90vcNLYLInFEX6Ppy1tPf9Cnzj4p4WGeKLs1Pt8Qu",
+};
+const DOTENV = [
+ "# local settings",
+ "NODE_ENV=development",
+ "PORT=3000",
+ "export DEBUG=true",
+ `API_KEY=${SECRETS.apiKey}`,
+ `DATABASE_URL=postgres://admin:${SECRETS.dbPassword}@db.example.com:5432/app`,
+ "LOCAL_DB=postgres://localhost:5432/app",
+ `REDIS_URL=redis://user:${SECRETS.dbPassword}@localhost:6379`,
+ `AUTH=${SECRETS.jwt}`,
+ `TOKENISH=${SECRETS.jwt}`,
+ "SESSION_SECRET=true",
+ 'GREETING="hello world"',
+ "LOG_LEVEL=info # verbose in CI",
+ "MODE='production'",
+ "TEAM_NAME=acme-internal",
+ "HOME_DIR=/home/alice/app",
+ `PRIVATE_PEM="-----BEGIN RSA PRIVATE KEY-----`,
+ SECRETS.pem,
+ `-----END RSA PRIVATE KEY-----"`,
+ "AFTER_PEM=1",
+ "",
+].join("\n");
+
+function tempDir(prefix = "omnirush-penv-") {
+ return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
+}
+
+function write(root, files) {
+ for (const [name, content] of Object.entries(files)) {
+ fs.mkdirSync(path.dirname(path.join(root, name)), { recursive: true });
+ fs.writeFileSync(path.join(root, name), content);
+ }
+ return root;
+}
+
+const checks = {
+ isSecretName: uploader.isSecretAssignmentKey,
+ scrubText: (text) => uploader.redactUploadText(text).text,
+ isDenied: uploader.isUploadPathDenied,
+};
+
+test(".env: names always, values only when clearly not secret", () => {
+ const { keys } = env.parseEnvFile(DOTENV, checks);
+ assert.deepEqual(keys, [
+ { name: "NODE_ENV", value: "development" },
+ { name: "PORT", value: "3000" },
+ { name: "DEBUG", value: "true" },
+ { name: "API_KEY" },
+ { name: "DATABASE_URL" },
+ { name: "LOCAL_DB", value: "postgres://localhost:5432/app" },
+ { name: "REDIS_URL" },
+ { name: "AUTH" },
+ { name: "TOKENISH" },
+ { name: "SESSION_SECRET" },
+ { name: "GREETING" },
+ { name: "LOG_LEVEL", value: "info" },
+ { name: "MODE", value: "production" },
+ { name: "TEAM_NAME" },
+ { name: "HOME_DIR" },
+ { name: "PRIVATE_PEM" },
+ { name: "AFTER_PEM", value: "1" },
+ ]);
+ const flat = JSON.stringify(keys);
+ for (const secret of [...Object.values(SECRETS), "admin", "alice", "acme", "BEGIN"]) assert.ok(!flat.includes(secret), `leaked ${secret}`);
+});
+
+test("safeEnvValue keeps booleans, ports, enum words and credential-free localhost URLs only", () => {
+ const keep = (name, value) => env.safeEnvValue(name, value, checks);
+ assert.equal(keep("FEATURE_X", "false"), "false");
+ assert.equal(keep("WORKERS", "8"), "8");
+ assert.equal(keep("APP_URL", "http://localhost:3000/api"), "http://localhost:3000/api");
+ assert.equal(keep("APP_URL", "https://app.example.com"), undefined);
+ assert.equal(keep("APP_URL", "http://u:p@localhost:3000"), undefined);
+ assert.equal(keep("APP_URL", "http://localhost:3000/?token=abc"), undefined);
+ assert.equal(keep("ACCOUNT_NUMBER", "123456"), undefined);
+ assert.equal(keep("SECRET_PORT", "3000"), undefined);
+ assert.equal(keep("PASSWORD", "true"), undefined);
+ assert.equal(keep("SOMETHING", "hunter2"), undefined);
+ assert.equal(keep("BIG", "12345678901234"), undefined);
+ assert.equal(keep("REF", "${OTHER}"), undefined);
+});
+
+test(".env files are found in the root and subfolders; templates and dependency folders are not", async () => {
+ const root = write(tempDir(), {
+ ".env": "A=1\n",
+ ".env.local": "B=true\n",
+ ".env.example": "C=1\n",
+ ".env.sample": "D=1\n",
+ "services/api/.env.development": "NODE_ENV=development\n",
+ "node_modules/pkg/.env": "E=1\n",
+ "notenv.txt": "F=1\n",
+ });
+ const files = await env.collectEnvFiles(root, checks);
+ assert.deepEqual(files.map((file) => file.path), [".env", ".env.local", "services/api/.env.development"]);
+ assert.deepEqual(files[2].keys, [{ name: "NODE_ENV", value: "development" }]);
+});
+
+test("env var names read by the code, across languages, sorted and capped", async () => {
+ const names = env.envNamesIn([
+ "const a = process.env.STRIPE_KEY; const b = process.env['DB_HOST']; const c = import.meta.env.VITE_URL;",
+ "Deno.env.get(\"DENO_X\"); Bun.env.BUN_X;",
+ "os.environ['PY_A']; os.environ.get(\"PY_B\"); os.getenv('PY_C')",
+ "std::env::var(\"RUST_X\"); env!(\"RUST_BUILD\"); option_env!(\"RUST_OPT\");",
+ "os.Getenv(\"GO_Y\"); os.LookupEnv(\"GO_Z\")",
+ "System.getenv(\"JAVA_HOME\"); ENV['RUBY_A']; ENV.fetch(\"RUBY_B\"); $_ENV['PHP_A']; getenv('PHP_B');",
+ "Environment.GetEnvironmentVariable(\"DOTNET_X\")",
+ "process.env.lower_ok; process.env[name]; os.environ[key]",
+ ].join("\n"));
+ assert.deepEqual([...names].sort(), [
+ "BUN_X", "DB_HOST", "DENO_X", "DOTNET_X", "GO_Y", "GO_Z", "JAVA_HOME", "PHP_A", "PHP_B", "PY_A", "PY_B", "PY_C",
+ "RUBY_A", "RUBY_B", "RUST_BUILD", "RUST_OPT", "RUST_X", "STRIPE_KEY", "VITE_URL", "lower_ok",
+ ]);
+
+ const root = write(tempDir(), {
+ "src/app.ts": "process.env.APP_A; process.env.APP_B;",
+ "src/many.js": Array.from({ length: 600 }, (_, i) => `process.env.N_${String(i).padStart(3, "0")};`).join("\n"),
+ "node_modules/dep/index.js": "process.env.FROM_DEPENDENCY",
+ ".env": "process.env.IN_DOTENV",
+ "README.md": "process.env.IN_DOCS",
+ });
+ const read = await env.collectEnvNamesRead(root, checks);
+ assert.equal(read.names.length, 500);
+ assert.equal(read.truncated, true);
+ assert.ok(read.names.includes("APP_A"));
+ assert.ok(!read.names.includes("FROM_DEPENDENCY") && !read.names.includes("IN_DOTENV") && !read.names.includes("IN_DOCS"));
+});
+
+test("host: os-release, sw_vers, WSL and CPU", async () => {
+ assert.deepEqual(env.parseOsRelease('NAME="Ubuntu"\nID=ubuntu\nVERSION_ID="24.04"\nPRETTY_NAME="Ubuntu 24.04.1 LTS"\n'), {
+ NAME: "Ubuntu", ID: "ubuntu", VERSION_ID: "24.04", PRETTY_NAME: "Ubuntu 24.04.1 LTS",
+ });
+ assert.deepEqual(env.parseSwVers("ProductName:\t\tmacOS\nProductVersion:\t\t15.1\nBuildVersion:\t\t24B83\n"), { version: "15.1", build: "24B83" });
+ assert.equal(env.isWsl("Linux version 5.15.153.1-microsoft-standard-WSL2", {}), true);
+ assert.equal(env.isWsl("Linux version 6.8.0-139-generic", {}), false);
+ assert.equal(env.isWsl("Linux version 6.8.0", { WSL_DISTRO_NAME: "Ubuntu" }), true);
+
+ const mac = await env.systemInfo(async (file) => (file === "/usr/bin/sw_vers" ? "ProductVersion:\t14.6.1\nBuildVersion:\t23G93\n" : null), "darwin");
+ assert.equal(mac.macos_version, "14.6.1");
+ assert.equal(mac.macos_build, "23G93");
+ assert.equal(mac.wsl, false);
+ assert.ok(mac.cpu_logical_cores >= 1);
+ assert.equal(typeof mac.cpu_model, "string");
+ if (process.platform === "linux" && fs.existsSync("/etc/os-release")) {
+ const linux = await env.systemInfo(async () => null, "linux");
+ assert.equal(typeof linux.distro_id, "string");
+ }
+});
+
+test("shell: name, version, version managers from env/PATH only, PATH without home or user name", async () => {
+ assert.deepEqual(env.sanitizedPath("/home/alice/.nvm/versions/node/v22/bin:/usr/bin:/opt/alice-tools/bin:/home/alice", { home: "/home/alice", user: "alice", platform: "linux" }), [
+ "~/.nvm/versions/node/v22/bin", "/usr/bin", "/opt/<user>-tools/bin", "~",
+ ]);
+ assert.deepEqual(env.sanitizedPath("C:\\Users\\Bob\\AppData\\Roaming\\npm;C:\\Windows\\system32;D:\\bob\\bin", { home: "C:\\Users\\Bob", user: "bob", platform: "win32" }), [
+ "~\\AppData\\Roaming\\npm", "C:\\Windows\\system32", "D:\\<user>\\bin",
+ ]);
+ // A short user name is replaced only as a whole path component.
+ assert.deepEqual(env.sanitizedPath("/opt/al/bin:/usr/local/bin", { home: "/home/al", user: "al", platform: "linux" }), ["/opt/<user>/bin", "/usr/local/bin"]);
+
+ assert.deepEqual(env.versionManagers({ NVM_DIR: "x", PYENV_ROOT: "y" }, ["~/.asdf/shims"]), ["nvm", "pyenv", "asdf"]);
+ const ran = [];
+ const info = await env.shellInfo(async (file, args) => {
+ ran.push(`${file} ${args.join(" ")}`);
+ return "GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)\nCopyright\n";
+ }, { env: { SHELL: "/bin/bash", PATH: `${os.homedir()}/.volta/bin:/usr/bin`, VOLTA_HOME: "x" }, platform: "linux" });
+ assert.equal(info.name, "bash");
+ assert.equal(info.version, "GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)");
+ assert.deepEqual(info.version_managers, ["volta"]);
+ assert.deepEqual(info.path, ["~/.volta/bin", "/usr/bin"]);
+ assert.deepEqual(ran, ["/bin/bash --version"], "only the shell itself runs, never a shim");
+ const odd = await env.shellInfo(async () => assert.fail("no version probe for an unknown shell"), { env: { SHELL: "/usr/local/bin/mysh", PATH: "" }, platform: "linux" });
+ assert.equal(odd.name, "mysh");
+ assert.equal(odd.version, undefined);
+});
+
+test("maven, gradle and cmake versions; npm ls only with package.json and node_modules, reduced to versions", async () => {
+ const gradleOut = "\n------------------------------------------------------------\nGradle 8.5\n------------------------------------------------------------\n\nBuild time: 2023\n";
+ const npmTree = JSON.stringify({
+ name: "demo",
+ version: "1.0.0",
+ dependencies: {
+ react: { version: "18.3.1", resolved: "https://registry.npmjs.org/react/-/react-18.3.1.tgz" },
+ local: { version: "file:../local", resolved: "file:/home/alice/local" },
+ private: { version: "1.0.0", resolved: "git+https://alice:[email protected]/acme/private.git" },
+ gone: { missing: true },
+ },
+ });
+ const run = async (file, args) => {
+ const name = path.basename(file);
+ if (name === "mvn") return { code: 0, stdout: "Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)\nMaven home: /home/alice/.sdkman/candidates/maven\n", stderr: "" };
+ if (name === "gradle") return { code: 0, stdout: gradleOut, stderr: "" };
+ if (name === "cmake") return { code: 0, stdout: "cmake version 3.28.3\n\nCMake suite maintained by Kitware\n", stderr: "" };
+ if (name === "npm" && args[0] === "ls") return { code: 1, stdout: npmTree, stderr: "npm ERR! missing: gone" };
+ return null;
+ };
+ const resolve = (command) => (["mvn", "gradle", "cmake", "npm"].includes(command) ? `/opt/bin/${command}` : null);
+ const root = write(tempDir(), { "package.json": "{}", "node_modules/react/package.json": "{}" });
+ const result = await toolchain.collectToolchain(root, { env: {}, resolve, run });
+ assert.equal(result.versions.mvn, "Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)");
+ assert.equal(result.versions.gradle, "Gradle 8.5");
+ assert.equal(result.versions.cmake, "cmake version 3.28.3");
+ assert.deepEqual(result.npm_ls, { name: "demo", version: "1.0.0", dependencies: { react: "18.3.1", local: "local", private: "1.0.0", gone: "missing" } });
+ assert.ok(!JSON.stringify(result.npm_ls).match(/alice|ghp_|registry|github/));
+
+ const withoutModules = await toolchain.collectToolchain(write(tempDir(), { "package.json": "{}" }), { env: {}, resolve, run });
+ assert.equal(withoutModules.npm_ls, undefined);
+ const big = toolchain.summarizeNpmLs(JSON.stringify({ dependencies: Object.fromEntries(Array.from({ length: 5000 }, (_, i) => [`package-number-${i}`, { version: "1.2.3" }])) }));
+ assert.equal(big.truncated, true);
+ assert.ok(JSON.stringify(big).length <= 64 * 1024);
+});
+
+test.skipIf(!hasZstd || process.platform === "win32")("the upload envelope carries env names, host and shell detail, and never a secret value", async () => {
+ const root = write(tempDir(), {
+ ".env": DOTENV,
+ "config/.env.production": `STRIPE_SECRET_KEY=${SECRETS.apiKey}\nNODE_ENV=production\n`,
+ "src/server.js": "const k = process.env.STRIPE_SECRET_KEY; const p = process.env.PORT;",
+ "package.json": "{}",
+ });
+ execFileSync("git", ["init", "-q"], { cwd: root });
+ const stateDir = tempDir("omnirush-penv-state-");
+ const envelopes = [];
+ const workspaceSync = new uploader.SessionUploader({
+ stateDir,
+ fallbackScanMs: 60_000,
+ capabilities: async () => ({ schema_versions: [2], canonical_trace: false }),
+ toolchain: { waitMs: 10_000, resolve: () => null, env: { SHELL: "/bin/sh", PATH: `${os.homedir()}/bin:/usr/bin` } },
+ upload: async (_sessionId, compressed) => {
+ envelopes.push(zlib.zstdDecompressSync(Buffer.from(compressed)).toString("utf8"));
+ return new Response("{}", { status: 201 });
+ },
+ });
+ const sessionId = "01a0dcd2-d8ee-7222-80eb-240063770432";
+ workspaceSync.startSession(sessionId, "ws", root);
+ await workspaceSync.idle(sessionId);
+ await workspaceSync.stop();
+ assert.ok(envelopes.length >= 1);
+ const toolchainBlock = JSON.parse(envelopes.at(-1)).environment.toolchain;
+ assert.deepEqual(toolchainBlock.env_files.map((file) => file.path), [".env", "config/.env.production"]);
+ assert.deepEqual(toolchainBlock.env_files[1].keys, [{ name: "STRIPE_SECRET_KEY" }, { name: "NODE_ENV", value: "production" }]);
+ assert.deepEqual(toolchainBlock.env_names_read, ["PORT", "STRIPE_SECRET_KEY"]);
+ assert.deepEqual(toolchainBlock.shell.path, ["~/bin", "/usr/bin"]);
+ assert.equal(toolchainBlock.system.wsl, false);
+ for (const text of envelopes) {
+ for (const secret of Object.values(SECRETS)) assert.ok(!text.includes(secret), `an envelope leaked ${secret.slice(0, 12)}…`);
+ assert.ok(!text.includes(os.userInfo().username) || os.userInfo().username.length < 3 || !text.includes(`/${os.userInfo().username}/`));
+ }
+});
diff --git a/apps/server/src/project-env.ts b/apps/server/src/project-env.ts
new file mode 100644
index 00000000..c573ffb7
--- /dev/null
+++ b/apps/server/src/project-env.ts
@@ -0,0 +1,446 @@
+// What a session's project expects from its environment, for
+// `environment.toolchain` (see toolchain.ts): the names in its .env files
+// (values only when clearly not secret), the environment variable names its
+// code reads, the host (OS release, WSL, CPU) and the shell (name, version,
+// version managers, PATH with the home directory and user name removed).
+//
+// Nothing here spawns a version-manager shim. Every value that could hold a
+// secret is left out; when in doubt, the value is omitted and only the name
+// is kept.
+
+import { execFile } from "node:child_process";
+import { readFile, readdir, lstat } from "node:fs/promises";
+import { cpus, homedir, release as osRelease, userInfo } from "node:os";
+import nodePath from "node:path";
+
+import { gitSkipReason } from "./command-guard.js";
+
+export const MAX_ENV_FILES = 50;
+export const MAX_ENV_KEYS_PER_FILE = 500;
+export const MAX_ENV_FILE_BYTES = 256 * 1024;
+export const MAX_ENV_NAMES_READ = 500;
+export const MAX_PATH_ENTRIES = 64;
+const MAX_ENV_VALUE_CHARS = 100;
+const MAX_WALK_DEPTH = 6;
+const MAX_WALK_DIRECTORIES = 5_000;
+const MAX_SCANNED_FILES = 20_000;
+const MAX_SCANNED_FILE_BYTES = 512 * 1024;
+const MAX_SCANNED_TOTAL_BYTES = 32 * 1024 * 1024;
+
+/** Folders never searched for .env files or source: dependencies, environments, build output. */
+const SKIPPED_DIRS = new Set([
+ "node_modules", ".git", ".hg", ".svn", ".venv", "venv", "env", ".tox", ".nox", "__pycache__", ".mypy_cache",
+ ".pytest_cache", ".ruff_cache", "dist", "build", "out", "target", "vendor", ".next", ".nuxt", ".cache", ".turbo",
+ "coverage", ".gradle", ".idea", ".terraform", "bower_components", "Pods", ".dart_tool", ".pnpm-store", ".yarn",
+]);
+
+export type EnvKey = { name: string; value?: string };
+export type EnvFile = { path: string; keys: EnvKey[]; keys_truncated?: boolean };
+
+export type SecretChecks = {
+ /** The capture's secret-name rule (workspace-sync isSecretAssignmentKey). */
+ isSecretName?: (name: string) => boolean;
+ /** The capture's text scrub; a value it would change is never kept. */
+ scrubText?: (value: string) => string;
+ /** The capture's path denylist; a denied file's code is never scanned. */
+ isDenied?: (path: string) => boolean;
+};
+
+// --- .env files ------------------------------------------------------------------
+
+const ENV_FILE = /^\.env(?:\..+)?$/;
+/** Documented placeholders, normal project files: not environment files. */
+const ENV_TEMPLATE = /\.(?:example|sample|template)$/i;
+// Names whose value is never kept, on top of the capture's own secret rule:
+// anything that might be a credential, a personal detail or an identifier.
+const SECRETISH_NAME = /KEY|SECRET|TOKEN|PASS|PWD|AUTH|CRED|PRIVATE|CERT|SALT|SESSION|COOKIE|SIGN|DSN|HASH|SEED|OTP|PIN|CODE|ACCOUNT|USER|LOGIN|EMAIL|MAIL|PHONE|TENANT|WEBHOOK|ENCRYPT|BEARER|JWT|API/i;
+const BOOLEAN = /^(?:true|false|yes|no|on|off)$/i;
+const NUMBER = /^-?\d{1,6}(?:\.\d{1,4})?$/;
+const LOCAL_URL = /^(?:https?|wss?|postgres(?:ql)?|mysql|mariadb|redis|rediss|mongodb|amqp|nats|grpc):\/\/(?:localhost|127\.0\.0\.1|\[::1\]|0\.0\.0\.0|host\.docker\.internal)(?::\d{1,5})?(?:\/[A-Za-z0-9._~\/-]*)?$/i;
+const SAFE_WORDS = new Set([
+ "development", "dev", "production", "prod", "staging", "stage", "test", "testing", "local", "preview", "qa", "ci",
+ "sandbox", "release", "debug", "info", "warn", "warning", "error", "trace", "fatal", "silent", "verbose", "none",
+ "utc", "json", "text", "pretty", "auto", "always", "never", "enabled", "disabled", "localhost", "0.0.0.0",
+ "127.0.0.1", "memory", "disk", "sqlite", "postgres", "postgresql", "mysql", "redis", "docker", "node", "python",
+ "http", "https", "light", "dark", "default", "strict", "lax", "inline", "headless",
+]);
+
+/**
+ * The value of `name=raw` when it is clearly not a secret: a boolean, a short
+ * number (a port), a well-known enum word (`NODE_ENV=development`) or a
+ * localhost URL without credentials. Undefined otherwise, and always for a
+ * secret-looking name or a value the scrub would touch.
+ */
+export function safeEnvValue(name: string, raw: string, checks: SecretChecks = {}): string | undefined {
+ if (checks.isSecretName?.(name) || SECRETISH_NAME.test(name)) return undefined;
+ const value = unquoteEnvValue(raw);
+ if (value === undefined || value.length > MAX_ENV_VALUE_CHARS) return undefined;
+ const safe = value === ""
+ || BOOLEAN.test(value)
+ || NUMBER.test(value)
+ || SAFE_WORDS.has(value.toLowerCase())
+ || (LOCAL_URL.test(value) && !value.includes("@"));
+ if (!safe) return undefined;
+ if (checks.scrubText && checks.scrubText(value) !== value) return undefined;
+ return value;
+}
+
+/** The literal value of a dotenv right-hand side, or undefined when it is not one plain value. */
+function unquoteEnvValue(raw: string): string | undefined {
+ const text = raw.trim();
+ const quote = text[0];
+ if (quote === '"' || quote === "'" || quote === "`") {
+ const end = text.indexOf(quote, 1);
+ if (end < 0) return undefined;
+ const rest = text.slice(end + 1).trim();
+ if (rest && !rest.startsWith("#")) return undefined;
+ const inner = text.slice(1, end);
+ return /\$\{?|\\/.test(inner) ? undefined : inner;
+ }
+ // An inline comment starts at a `#` after whitespace.
+ const value = text.replace(/\s+#.*$/, "").trim();
+ return /[$\\\s"'`]/.test(value) ? undefined : value;
+}
+
+/** The keys of one dotenv text, in order, each with its value only when safeEnvValue keeps it. */
+export function parseEnvFile(text: string, checks: SecretChecks = {}): { keys: EnvKey[]; truncated: boolean } {
+ const keys: EnvKey[] = [];
+ const seen = new Set<string>();
+ let openQuote: string | null = null;
+ let truncated = false;
+ for (const line of text.split(/\r?\n/)) {
+ if (openQuote) {
+ // Inside a multi-line quoted value (a PEM key): its lines are never keys.
+ if (line.includes(openQuote)) openQuote = null;
+ continue;
+ }
+ const match = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_.-]*)\s*=(.*)$/.exec(line);
+ if (!match) continue;
+ const name = match[1]!;
+ const raw = match[2]!.trim();
+ const quote = raw[0];
+ if ((quote === '"' || quote === "'" || quote === "`") && raw.indexOf(quote, 1) < 0) openQuote = quote;
+ if (seen.has(name)) continue;
+ if (keys.length >= MAX_ENV_KEYS_PER_FILE) {
+ truncated = true;
+ continue;
+ }
+ seen.add(name);
+ const value = openQuote ? undefined : safeEnvValue(name, raw, checks);
+ keys.push(value === undefined ? { name } : { name, value });
+ }
+ return { keys, truncated };
+}
+
+/** Every .env file under `root` (template files excluded), as project-relative paths. */
+export async function findEnvFiles(root: string): Promise<string[]> {
+ const found: string[] = [];
+ let directories = 0;
+ const walk = async (dir: string, depth: number): Promise<void> => {
+ if (found.length >= MAX_ENV_FILES || directories >= MAX_WALK_DIRECTORIES) return;
+ directories += 1;
+ let entries;
+ try {
+ entries = await readdir(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ entries.sort((a, b) => a.name.localeCompare(b.name));
+ for (const entry of entries) {
+ if (found.length >= MAX_ENV_FILES) return;
+ if (entry.isFile() && ENV_FILE.test(entry.name) && !ENV_TEMPLATE.test(entry.name)) {
+ found.push(nodePath.relative(root, nodePath.join(dir, entry.name)).split(nodePath.sep).join("/"));
+ }
+ }
+ if (depth >= MAX_WALK_DEPTH) return;
+ for (const entry of entries) {
+ if (entry.isDirectory() && !SKIPPED_DIRS.has(entry.name) && !entry.name.startsWith(".")) await walk(nodePath.join(dir, entry.name), depth + 1);
+ }
+ };
+ await walk(root, 0);
+ return found;
+}
+
+export async function collectEnvFiles(root: string, checks: SecretChecks = {}): Promise<EnvFile[]> {
+ const files: EnvFile[] = [];
+ for (const path of await findEnvFiles(root)) {
+ const full = nodePath.join(root, path);
+ try {
+ const info = await lstat(full);
+ if (!info.isFile() || info.size > MAX_ENV_FILE_BYTES) {
+ files.push({ path, keys: [], keys_truncated: info.isFile() });
+ continue;
+ }
+ const { keys, truncated } = parseEnvFile(await readFile(full, "utf8"), checks);
+ files.push(truncated ? { path, keys, keys_truncated: true } : { path, keys });
+ } catch {
+ // Unreadable: left out.
+ }
+ }
+ return files;
+}
+
+// --- environment variable names the code reads -----------------------------------
+
+const NAME = "([A-Za-z_][A-Za-z0-9_]{0,127})";
+const Q = "[\"'`]";
+const ENV_READS = new RegExp([
+ `process\\.env\\.${NAME}`,
+ `process\\.env\\[\\s*${Q}${NAME}${Q}\\s*\\]`,
+ `import\\.meta\\.env\\.${NAME}`,
+ `Bun\\.env\\.${NAME}`,
+ `Deno\\.env\\.get\\(\\s*${Q}${NAME}${Q}`,
+ `os\\.environ\\[\\s*${Q}${NAME}${Q}\\s*\\]`,
+ `os\\.environ\\.get\\(\\s*${Q}${NAME}${Q}`,
+ `getenv\\(\\s*${Q}${NAME}${Q}`,
+ `env::var(?:_os)?\\(\\s*"${NAME}"`,
+ `env!\\(\\s*"${NAME}"`,
+ `os\\.(?:Getenv|LookupEnv)\\(\\s*"${NAME}"`,
+ `System\\.getenv\\(\\s*"${NAME}"`,
+ `\\bENV(?:\\.fetch\\(\\s*|\\[\\s*)${Q}${NAME}${Q}`,
+ `\\$_ENV\\[\\s*${Q}${NAME}${Q}`,
+ `Environment\\.GetEnvironmentVariable\\(\\s*"${NAME}"`,
+].join("|"), "g");
+const CODE_EXTENSIONS = new Set([
+ ".js", ".jsx", ".mjs", ".cjs", ".ts", ".tsx", ".mts", ".cts", ".vue", ".svelte", ".astro", ".py", ".pyi", ".rs",
+ ".go", ".java", ".kt", ".kts", ".scala", ".groovy", ".rb", ".php", ".cs", ".fs", ".c", ".cc", ".cpp", ".cxx",
+ ".h", ".hpp", ".swift", ".ex", ".exs", ".dart", ".lua", ".jl", ".r",
+]);
+
+/** The environment variable names in `text` read through a known accessor. */
+export function envNamesIn(text: string, into: Set<string> = new Set()): Set<string> {
+ ENV_READS.lastIndex = 0;
+ for (const match of text.matchAll(ENV_READS)) {
+ const name = match.slice(1).find((group) => group !== undefined);
+ if (name) into.add(name);
+ }
+ return into;
+}
+
+async function projectSourceFiles(root: string, checks: SecretChecks): Promise<string[]> {
+ const keep = (path: string) => CODE_EXTENSIONS.has(nodePath.extname(path).toLowerCase()) && !checks.isDenied?.(path)
+ && !path.split("/").some((part) => SKIPPED_DIRS.has(part));
+ if (!(await gitSkipReason())) {
+ const listed = await new Promise<string[] | null>((resolvePromise) => {
+ execFile("git", ["-C", root, "ls-files", "-co", "--exclude-standard", "-z"], {
+ encoding: "buffer", maxBuffer: 16 * 1024 * 1024, timeout: 10_000, windowsHide: true,
+ env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_OPTIONAL_LOCKS: "0" },
+ }, (error, stdout) => resolvePromise(error ? null : Buffer.from(stdout).toString("utf8").split("\0").filter(Boolean)));
+ });
+ if (listed) return listed.filter(keep).slice(0, MAX_SCANNED_FILES);
+ }
+ const files: string[] = [];
+ let directories = 0;
+ const walk = async (dir: string, depth: number): Promise<void> => {
+ if (files.length >= MAX_SCANNED_FILES || directories >= MAX_WALK_DIRECTORIES || depth > MAX_WALK_DEPTH * 2) return;
+ directories += 1;
+ let entries;
+ try {
+ entries = await readdir(dir, { withFileTypes: true });
+ } catch {
+ return;
+ }
+ for (const entry of entries) {
+ const full = nodePath.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ if (!SKIPPED_DIRS.has(entry.name) && !entry.name.startsWith(".")) await walk(full, depth + 1);
+ } else if (entry.isFile()) {
+ const path = nodePath.relative(root, full).split(nodePath.sep).join("/");
+ if (keep(path)) files.push(path);
+ }
+ }
+ };
+ await walk(root, 0);
+ return files.slice(0, MAX_SCANNED_FILES);
+}
+
+/** The environment variable names the project's code reads, sorted, capped at MAX_ENV_NAMES_READ. */
+export async function collectEnvNamesRead(root: string, checks: SecretChecks = {}): Promise<{ names: string[]; truncated: boolean }> {
+ const names = new Set<string>();
+ let total = 0;
+ for (const path of await projectSourceFiles(root, checks)) {
+ if (total >= MAX_SCANNED_TOTAL_BYTES) break;
+ try {
+ const full = nodePath.join(root, path);
+ const info = await lstat(full);
+ if (!info.isFile() || info.size > MAX_SCANNED_FILE_BYTES) continue;
+ total += info.size;
+ envNamesIn(await readFile(full, "utf8"), names);
+ } catch {
+ // Gone or unreadable.
+ }
+ }
+ const sorted = [...names].sort();
+ return { names: sorted.slice(0, MAX_ENV_NAMES_READ), truncated: sorted.length > MAX_ENV_NAMES_READ };
+}
+
+// --- host ------------------------------------------------------------------------
+
+export type SystemInfo = {
+ distro_id?: string;
+ distro_version?: string;
+ distro_name?: string;
+ macos_version?: string;
+ macos_build?: string;
+ windows_build?: string;
+ wsl: boolean;
+ cpu_model?: string;
+ cpu_logical_cores?: number;
+};
+
+/** Fields of an os-release file. */
+export function parseOsRelease(text: string): Record<string, string> {
+ const fields: Record<string, string> = {};
+ for (const line of text.split(/\r?\n/)) {
+ const match = /^([A-Z_]+)=(.*)$/.exec(line.trim());
+ if (match) fields[match[1]!] = match[2]!.replace(/^(["'])(.*)\1$/, "$2");
+ }
+ return fields;
+}
+
+/** `sw_vers` output: ProductVersion and BuildVersion. */
+export function parseSwVers(text: string): { version?: string; build?: string } {
+ const field = (key: string) => new RegExp(`^${key}:\\s*(\\S+)`, "m").exec(text)?.[1];
+ return { version: field("ProductVersion"), build: field("BuildVersion") };
+}
+
+export function isWsl(procVersion: string | null, env: NodeJS.ProcessEnv = process.env): boolean {
+ return Boolean(env.WSL_DISTRO_NAME || env.WSL_INTEROP) || /microsoft|wsl/i.test(procVersion ?? "");
+}
+
+export type RunText = (file: string, args: string[]) => Promise<string | null>;
+
+export async function systemInfo(run: RunText, platform: NodeJS.Platform = process.platform): Promise<SystemInfo> {
+ const info: SystemInfo = { wsl: false };
+ const clip = (value: string | undefined, limit = 128) => (value ? value.trim().slice(0, limit) || undefined : undefined);
+ if (platform === "linux") {
+ const osRelease = await readFile("/etc/os-release", "utf8").catch(() => readFile("/usr/lib/os-release", "utf8").catch(() => ""));
+ const fields = parseOsRelease(osRelease);
+ info.distro_id = clip(fields.ID, 64);
+ info.distro_version = clip(fields.VERSION_ID, 64);
+ info.distro_name = clip(fields.PRETTY_NAME);
+ info.wsl = isWsl(await readFile("/proc/version", "utf8").catch(() => null));
+ } else if (platform === "darwin") {
+ // /usr/bin/sw_vers is part of the OS, not a Command Line Tools shim.
+ const output = await run("/usr/bin/sw_vers", []);
+ const { version, build } = parseSwVers(output ?? "");
+ info.macos_version = clip(version, 32);
+ info.macos_build = clip(build, 32);
+ } else if (platform === "win32") {
+ info.windows_build = clip(osRelease().split(".")[2], 16);
+ }
+ try {
+ const list = cpus();
+ info.cpu_model = clip(list[0]?.model?.replace(/\s+/g, " "));
+ info.cpu_logical_cores = list.length || undefined;
+ } catch {
+ // No CPU information in this runtime.
+ }
+ for (const key of Object.keys(info) as (keyof SystemInfo)[]) if (info[key] === undefined) delete info[key];
+ return info;
+}
+
+// --- shell -------------------------------------------------------------------------
+
+export type ShellInfo = {
+ name?: string;
+ version?: string;
+ version_managers: string[];
+ path: string[];
+};
+
+const MANAGER_ENV: Record<string, string[]> = {
+ nvm: ["NVM_DIR", "NVM_BIN"],
+ fnm: ["FNM_DIR", "FNM_MULTISHELL_PATH"],
+ volta: ["VOLTA_HOME"],
+ pyenv: ["PYENV_ROOT", "PYENV_SHELL", "PYENV_VERSION"],
+ asdf: ["ASDF_DIR", "ASDF_DATA_DIR"],
+ mise: ["MISE_SHELL", "__MISE_ACTIVATE", "RTX_SHELL"],
+ rbenv: ["RBENV_ROOT", "RBENV_SHELL"],
+ conda: ["CONDA_PREFIX", "CONDA_EXE"],
+ sdkman: ["SDKMAN_DIR"],
+};
+const MANAGER_PATH: Record<string, RegExp> = {
+ nvm: /[\\/]\.nvm[\\/]/,
+ fnm: /fnm_multishells|[\\/]fnm[\\/]/,
+ volta: /[\\/]\.volta[\\/]/,
+ pyenv: /[\\/]\.pyenv[\\/]|pyenv-win/,
+ asdf: /[\\/]\.asdf[\\/]/,
+ mise: /[\\/]mise[\\/](shims|installs)/,
+ rbenv: /[\\/]\.rbenv[\\/]/,
+ conda: /[\\/](ana|mini)conda\d*[\\/]|miniforge|mambaforge/i,
+ sdkman: /[\\/]\.sdkman[\\/]/,
+};
+
+/** Version managers in use, from environment variable names and PATH entries only (no shim is run). */
+export function versionManagers(env: NodeJS.ProcessEnv = process.env, pathEntries: string[] = []): string[] {
+ const found: string[] = [];
+ for (const [name, keys] of Object.entries(MANAGER_ENV)) {
+ if (keys.some((key) => env[key]) || pathEntries.some((entry) => MANAGER_PATH[name]!.test(entry))) found.push(name);
+ }
+ return found;
+}
+
+/**
+ * PATH as a list: the home directory becomes `~`, and the user name, wherever
+ * else it appears, becomes `<user>`. Capped at MAX_PATH_ENTRIES.
+ */
+export function sanitizedPath(pathValue: string, options: { home?: string; user?: string; platform?: NodeJS.Platform } = {}): string[] {
+ const platform = options.platform ?? process.platform;
+ const home = (options.home ?? safeHome()).replace(/[\\/]+$/, "");
+ const user = options.user ?? safeUser();
+ const separator = platform === "win32" ? ";" : ":";
+ const entries: string[] = [];
+ for (const raw of pathValue.split(separator)) {
+ let entry = raw.trim().replace(/^"(.*)"$/, "$1");
+ if (!entry) continue;
+ if (home && (platform === "win32" ? entry.toLowerCase().startsWith(home.toLowerCase()) : entry.startsWith(home))) {
+ const rest = entry.slice(home.length);
+ if (rest === "" || rest.startsWith("/") || rest.startsWith("\\")) entry = `~${rest}`;
+ }
+ if (user) {
+ const escaped = user.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ // A short name is replaced only as a whole path component.
+ entry = user.length >= 3
+ ? entry.replace(new RegExp(escaped, "gi"), "<user>")
+ : entry.replace(new RegExp(`(^|[\\\\/])${escaped}(?=[\\\\/]|$)`, "gi"), "$1<user>");
+ }
+ entries.push(entry.slice(0, 256));
+ if (entries.length >= MAX_PATH_ENTRIES) break;
+ }
+ return entries;
+}
+
+function safeHome(): string {
+ try {
+ return homedir();
+ } catch {
+ return "";
+ }
+}
+
+function safeUser(): string {
+ try {
+ return userInfo().username;
+ } catch {
+ return process.env.USER ?? process.env.USERNAME ?? "";
+ }
+}
+
+const VERSIONED_SHELLS = new Set(["bash", "zsh", "fish", "ksh", "tcsh", "nu", "elvish", "xonsh"]);
+
+export async function shellInfo(run: RunText, options: { env?: NodeJS.ProcessEnv; platform?: NodeJS.Platform } = {}): Promise<ShellInfo> {
+ const env = options.env ?? process.env;
+ const platform = options.platform ?? process.platform;
+ const shellPath = (platform === "win32" ? env.ComSpec ?? env.COMSPEC : env.SHELL)?.trim();
+ const name = shellPath ? nodePath.basename(shellPath.replace(/\\/g, "/")).replace(/\.exe$/i, "").toLowerCase() : undefined;
+ const path = sanitizedPath((platform === "win32" ? env.Path ?? env.PATH : env.PATH) ?? "", { platform });
+ const info: ShellInfo = { version_managers: versionManagers(env, path), path };
+ if (name) info.name = name.slice(0, 32);
+ if (shellPath && name && VERSIONED_SHELLS.has(name) && nodePath.isAbsolute(shellPath)) {
+ const output = await run(shellPath, ["--version"]);
+ const line = output?.split(/\r?\n/).map((part) => part.trim()).find(Boolean);
+ if (line) info.version = line.slice(0, 200);
+ }
+ return info;
+}
diff --git a/apps/server/src/session-uploader.ts b/apps/server/src/session-uploader.ts
index 274b0d37..b1ef09c0 100644
--- a/apps/server/src/session-uploader.ts
+++ b/apps/server/src/session-uploader.ts
@@ -23,6 +23,7 @@ import { TurnBaseStore, TurnDiffBuilder, type TurnDiffInput } from "./turn-diff.
import { SUBAGENT_MODEL_FALLBACK_TRACE } from "./omnirush-swarm.js";
import { writeFileAtomic } from "./atomic-write.js";
import { XCODE_CLT_MISSING, gitSkipReason } from "./command-guard.js";
+import { ExcludedFiles, MAX_EXCLUDED_FILES_BYTES, listGitIgnored, type HashCache } from "./excluded-files.js";
import { ToolchainCache, type CollectOptions as ToolchainOptions, type UploadToolchain } from "./toolchain.js";
const execFileAsync = promisify(execFile);
@@ -2228,7 +2229,15 @@ function boundedTraceBatches(
}
/** Eligible files plus the number of non-ignored paths the denylist dropped. */
-type WorkspaceListing = { paths: string[]; denied: number; truncated: boolean };
+type WorkspaceListing = {
+ paths: string[];
+ denied: number;
+ truncated: boolean;
+ /** excluded-files.ts: the denied paths (a denied folder as `dir/`) and those past the listing cap, up to MAX_LISTED_EXCLUSIONS each. */
+ deniedPaths?: string[];
+ overflowPaths?: string[];
+};
+const MAX_LISTED_EXCLUSIONS = 5_000;
const MAX_RAW_LISTING_FILES = MAX_FILES * 4;
async function walkFallback(
@@ -2259,11 +2268,15 @@ async function walkFallback(
if (entry.isDirectory()) {
// Only the unconditional rules apply to a directory name: "src/token/"
// is walked so its source files can be kept and scrubbed.
- if (hasDeniedComponent(pathComponents(path))) listing.denied += 1;
- else await walkFallback(root, fullPath, ignoreRules, listing, priorityPaths);
+ if (hasDeniedComponent(pathComponents(path))) {
+ listing.denied += 1;
+ if ((listing.deniedPaths ??= []).length < MAX_LISTED_EXCLUSIONS) listing.deniedPaths.push(`${path}/`);
+ } else await walkFallback(root, fullPath, ignoreRules, listing, priorityPaths);
} else if (entry.isFile()) {
- if (isUploadPathDenied(path)) listing.denied += 1;
- else listing.paths.push(path);
+ if (isUploadPathDenied(path)) {
+ listing.denied += 1;
+ if ((listing.deniedPaths ??= []).length < MAX_LISTED_EXCLUSIONS) listing.deniedPaths.push(path);
+ } else listing.paths.push(path);
}
}
return listing;
@@ -2271,16 +2284,21 @@ async function walkFallback(
function filterListing(candidates: string[], priorityPaths: ReadonlySet<string> = new Set(), limit = MAX_RAW_LISTING_FILES): WorkspaceListing {
const eligible: string[] = [];
+ const deniedPaths: string[] = [];
let denied = 0;
for (const path of candidates) {
if (!path) continue;
- if (isUploadPathDenied(path)) denied += 1;
- else eligible.push(path);
+ if (isUploadPathDenied(path)) {
+ denied += 1;
+ if (deniedPaths.length < MAX_LISTED_EXCLUSIONS) deniedPaths.push(path);
+ } else eligible.push(path);
}
const prioritized = eligible.filter((path) => priorityPaths.has(portablePathForUpload(path)));
const ordinary = eligible.filter((path) => !priorityPaths.has(portablePathForUpload(path)));
- const paths = [...prioritized, ...ordinary.slice(0, Math.max(0, limit - prioritized.length))];
- return { paths, denied, truncated: eligible.length > paths.length };
+ const kept = Math.max(0, limit - prioritized.length);
+ const paths = [...prioritized, ...ordinary.slice(0, kept)];
+ const overflowPaths = ordinary.slice(kept, kept + MAX_LISTED_EXCLUSIONS);
+ return { paths, denied, truncated: eligible.length > paths.length, deniedPaths, overflowPaths };
}
async function listWorkspaceFiles(root: string, priorityPaths: ReadonlySet<string> = new Set(), limit = MAX_FILES): Promise<WorkspaceListing> {
@@ -2295,7 +2313,7 @@ async function listWorkspaceFiles(root: string, priorityPaths: ReadonlySet<strin
} catch {
const raw = await walkFallback(root, root, [], { paths: [], denied: 0, truncated: false }, priorityPaths);
const limited = filterListing(raw.paths, priorityPaths, limit);
- return { paths: limited.paths, denied: raw.denied, truncated: raw.truncated || limited.truncated };
+ return { paths: limited.paths, denied: raw.denied, truncated: raw.truncated || limited.truncated, deniedPaths: raw.deniedPaths, overflowPaths: limited.overflowPaths };
}
}
@@ -3202,6 +3220,11 @@ type ScanResult = {
removed: number;
deniedCount: number;
manifestTruncated: boolean;
+ /** excluded-files.ts (full scans): denied, binary, oversized and capped paths (sizes by path). */
+ deniedPaths?: string[];
+ binaryPaths?: Map<string, number>;
+ tooLargePaths?: Map<string, number>;
+ cappedPaths?: string[];
};
/** A changed file competing for the snapshot's content budget. */
@@ -3363,20 +3386,43 @@ async function scanWorkspaceFull(
const entries = await mapBounded(listing.paths, SCAN_CONCURRENCY, (path) => inspectFile(context, path));
const manifest: Manifest = new Map();
const changed = includeAll ? null : new Set<string>();
+ const binaryPaths = new Map<string, number>();
+ const unreadPaths: string[] = [];
+ const cappedPaths = [...(listing.overflowPaths ?? [])];
listing.paths.forEach((path, index) => {
const entry = entries[index];
- if (!entry) return;
+ if (!entry) {
+ if (unreadPaths.length < MAX_LISTED_EXCLUSIONS) unreadPaths.push(path);
+ return;
+ }
cache.set(path, entry);
- if (entry.binary || manifest.size >= MAX_FILES) return;
+ if (entry.binary) {
+ binaryPaths.set(path, entry.size);
+ return;
+ }
+ if (manifest.size >= MAX_FILES) {
+ if (cappedPaths.length < MAX_LISTED_EXCLUSIONS) cappedPaths.push(path);
+ return;
+ }
manifest.set(path, entry);
if (changed && previous?.get(path)?.sha256 !== entry.sha256) changed.add(path);
});
+ // A listed file the scan did not read: over the per-file cap, or gone.
+ const tooLargePaths = new Map<string, number>();
+ await mapBounded(unreadPaths, SCAN_CONCURRENCY, async (path) => {
+ const file = await lstat(resolve(root, path)).catch(() => null);
+ if (file?.isFile() && file.size > MAX_UPLOAD_FILE_BYTES) tooLargePaths.set(path, file.size);
+ });
return {
manifest,
changed,
removed: countRemoved(previous, manifest),
deniedCount: listing.denied,
manifestTruncated: listing.truncated || listing.paths.length >= MAX_FILES,
+ deniedPaths: listing.deniedPaths,
+ binaryPaths,
+ tooLargePaths,
+ cappedPaths,
};
}
@@ -3651,6 +3697,8 @@ type EnvelopeBody = {
manifest: () => Iterable<HashCacheEntry>;
/** Computed once files[] is written, so cap omissions are known. */
privacy: () => Record<string, unknown>;
+ /** Fields written after privacy, once files[] is out (excluded_files). */
+ trailer?: () => Promise<Record<string, unknown>>;
trace?: readonly unknown[];
schemaVersion?: 2 | 3;
};
@@ -3764,6 +3812,7 @@ export class SessionUploader {
private readonly snapshotMaxBytes: number;
private environmentCache: Promise<UploadEnvironment> | null = null;
private readonly toolchains: ToolchainCache | null;
+ private readonly excludedHashes: HashCache = new Map();
private spoolCounter = 0;
private spoolTail: Promise<void> = Promise.resolve();
private retryTimer: ReturnType<typeof setTimeout> | null = null;
@@ -3841,7 +3890,11 @@ export class SessionUploader {
this.texts = new RedactedTextCache(options.redactedTextCacheBytes ?? REDACTED_TEXT_CACHE_BYTES);
this.toolchains = options.toolchain === false
? null
- : new ToolchainCache({ ...(options.toolchain ?? {}), scrub: (toolchain) => redactUploadJson(toolchain).value as UploadToolchain });
+ : new ToolchainCache({
+ checks: { isSecretName: isSecretAssignmentKey, scrubText: (text) => redactUploadText(text).text, isDenied: isUploadPathDenied },
+ ...(options.toolchain ?? {}),
+ scrub: (toolchain) => redactUploadJson(toolchain).value as UploadToolchain,
+ });
this.onSessionClosed = options.onSessionClosed;
this.onPathTouched = options.onPathTouched;
this.tempDir = stateDir ? join(stateDir, UPLOAD_TEMP_DIRECTORY) : join(tmpdir(), `omnirush-upload-${process.pid}`);
@@ -3999,6 +4052,24 @@ export class SessionUploader {
return this.environmentCache;
}
+ /**
+ * excluded-files.ts: what this snapshot left out. A full scan names the
+ * denied, binary, oversized, gitignored and capped files; a targeted one
+ * only what it inspected.
+ */
+ private async excludedFiles(root: string, scan: ScanResult, omitted: readonly SnapshotCandidate[], full: boolean): Promise<unknown> {
+ const excluded = new ExcludedFiles(root, { uploadPath: portablePathForUpload, hashes: this.excludedHashes });
+ for (const path of scan.deniedPaths ?? []) excluded.add(path, "privacy");
+ for (const [path, size] of scan.tooLargePaths ?? []) excluded.add(path, "too_large", { size });
+ for (const [path, size] of scan.binaryPaths ?? []) excluded.add(path, "binary", { size });
+ if (full) {
+ for (const path of await listGitIgnored(root)) excluded.add(path, isUploadPathDenied(path.replace(/\/$/, "")) ? "privacy" : "gitignored");
+ }
+ for (const path of scan.cappedPaths ?? []) excluded.add(path, "snapshot_cap");
+ for (const candidate of omitted) excluded.add(candidate.path, "snapshot_cap", { size: candidate.bytes, sha256: scan.manifest.get(candidate.path)?.sha256 ?? null });
+ return excluded.finish();
+ }
+
/** The environment block plus the toolchain of the session's project root, when one was captured. */
private async sessionEnvironment(root: string): Promise<UploadEnvironment> {
const environment = await this.environment();
@@ -4464,7 +4535,13 @@ export class SessionUploader {
}
const raw = await inFlight;
const limited = filterListing(raw.paths, priorityPaths, MAX_FILES);
- return { paths: limited.paths, denied: raw.denied, truncated: raw.truncated || limited.truncated };
+ return {
+ paths: limited.paths,
+ denied: raw.denied,
+ truncated: raw.truncated || limited.truncated,
+ deniedPaths: raw.deniedPaths,
+ overflowPaths: [...(raw.overflowPaths ?? []), ...(limited.overflowPaths ?? [])].slice(0, MAX_LISTED_EXCLUSIONS),
+ };
}
private releaseCache(state: SessionState): void {
@@ -5306,8 +5383,9 @@ export class SessionUploader {
let fixedBytes = serializedBytes(extras) + SNAPSHOT_WRAPPER_MARGIN_BYTES;
for (const file of leading) fixedBytes += serializedBytes(file) + 1;
for (const entry of scan.manifest.values()) fixedBytes += Buffer.byteLength(manifestEntryJson(entry)) + 1;
- const budget = Math.max(0, this.snapshotMaxBytes - fixedBytes);
+ const budget = Math.max(0, this.snapshotMaxBytes - fixedBytes - MAX_EXCLUDED_FILES_BYTES);
const selection = selectSnapshotContent(candidates, budget);
+ const capOmitted: SnapshotCandidate[] = [];
let omittedCount = selection.omittedCount;
let omittedBytes = selection.omittedBytes;
const manifest = scan.manifest;
@@ -5334,6 +5412,7 @@ export class SessionUploader {
if (used + cost + reserved > budget) {
omittedCount += 1;
omittedBytes += read.entry.bytes;
+ capOmitted.push(candidate);
continue;
}
// files[] and the manifest describe the same bytes even if the
@@ -5354,6 +5433,14 @@ export class SessionUploader {
diff_truncated: git?.diff_truncated ?? false,
snapshot_cap_omitted_count: omittedCount,
snapshot_cap_omitted_bytes: omittedBytes,
+ binary_file_count: scan.binaryPaths?.size ?? 0,
+ too_large_file_count: scan.tooLargePaths?.size ?? 0,
+ }),
+ trailer: async () => ({
+ excluded_files: await this.excludedFiles(state.root, scan, [
+ ...candidates.filter((candidate) => !selection.selected.has(candidate.path)),
+ ...capOmitted,
+ ], !(targeted && previous !== null)),
}),
});
if (omittedCount > 0) {
@@ -5633,6 +5720,9 @@ export class SessionUploader {
first = false;
}
await writer.write(`],"privacy":${JSON.stringify(body.privacy())}`);
+ for (const [key, value] of Object.entries(body.trailer ? await body.trailer() : {})) {
+ await writer.write(`,${JSON.stringify(key)}:${JSON.stringify(value)}`);
+ }
if (body.trace) {
await writer.write(',"trace":[');
for (let index = 0; index < body.trace.length; index += 1) {
diff --git a/apps/server/src/toolchain.test.ts b/apps/server/src/toolchain.test.ts
index 48f104f2..f94256fe 100644
--- a/apps/server/src/toolchain.test.ts
+++ b/apps/server/src/toolchain.test.ts
@@ -53,6 +53,7 @@ test("a tool that is not on PATH is never spawned and is left out", async () =>
const root = tempProject();
const calls = [];
const result = await toolchain.collectToolchain(root, {
+ env: {},
resolve: (command) => (command === "node" || command === "go" ? `/opt/bin/${command}` : null),
run: fakeRun({ node: ok("v22.19.0\n"), go: ok("go version go1.23.2 linux/amd64\n") }, calls),
});
@@ -220,6 +221,8 @@ test("the cache collects once per project and again only when its manifest/lockf
const root = tempProject({ "package.json": "{}" });
let runs = 0;
const cache = new toolchain.ToolchainCache({
+ waitMs: 10_000,
+ env: {},
resolve: (command) => (command === "node" ? "/usr/bin/node" : null),
run: async () => {
runs += 1;
diff --git a/apps/server/src/toolchain.ts b/apps/server/src/toolchain.ts
index 6cd0eafb..a96c4eee 100644
--- a/apps/server/src/toolchain.ts
+++ b/apps/server/src/toolchain.ts
@@ -18,6 +18,16 @@ import { constants as osConstants, setPriority } from "node:os";
import nodePath from "node:path";
import { cltSkipReason, resolveCommand } from "./command-guard.js";
+import {
+ collectEnvFiles,
+ collectEnvNamesRead,
+ shellInfo,
+ systemInfo,
+ type EnvFile,
+ type SecretChecks,
+ type ShellInfo,
+ type SystemInfo,
+} from "./project-env.js";
export const TOOLCHAIN_SCHEMA_VERSION = 1;
/** Per version probe. */
@@ -48,8 +58,22 @@ export type UploadToolchain = {
pip_freeze?: string[];
pip_freeze_tool?: "pip" | "uv";
pip_freeze_truncated?: boolean;
+ /** `npm ls --json --depth=0`, reduced to direct dependency versions; only with package.json and node_modules. */
+ npm_ls?: NpmLs;
+ /** OS release, WSL, CPU. */
+ system?: SystemInfo;
+ /** Shell name and version, version managers in use, PATH (home as `~`, never the user name). */
+ shell?: ShellInfo;
+ /** .env files (templates excluded): key names, values only when clearly not secret. */
+ env_files?: EnvFile[];
+ /** Environment variable names the project's code reads. */
+ env_names_read?: string[];
+ env_names_read_truncated?: boolean;
};
+export type NpmLs = { name?: string; version?: string; dependencies: Record<string, string>; truncated?: boolean };
+export const MAX_NPM_LS_BYTES = 64 * 1024;
+
type Probe = {
/** Key in `versions`. */
key: string;
@@ -57,6 +81,8 @@ type Probe = {
args: string[];
/** Read the version from stderr (java prints `-version` there). */
stderr?: boolean;
+ /** The line holding the version, when it is not the first (gradle). */
+ pick?: RegExp;
};
export const TOOLCHAIN_PROBES: readonly Probe[] = [
@@ -78,6 +104,9 @@ export const TOOLCHAIN_PROBES: readonly Probe[] = [
{ key: "gcc", command: "gcc", args: ["--version"] },
{ key: "clang", command: "clang", args: ["--version"] },
{ key: "uv", command: "uv", args: ["--version"] },
+ { key: "mvn", command: "mvn", args: ["-v"] },
+ { key: "gradle", command: "gradle", args: ["-v"], pick: /^Gradle\s+\S+/ },
+ { key: "cmake", command: "cmake", args: ["--version"] },
{ key: "poetry", command: "poetry", args: ["--version"] },
];
@@ -101,9 +130,13 @@ const VENV_DIRS = [".venv", "venv", "env"];
export type ProjectFiles = {
manifests: string[];
lockfiles: string[];
- /** Changes when a manifest or lockfile appears, goes, or is rewritten. */
+ /** Changes when a manifest, lockfile or root .env file appears, goes, or is rewritten. */
signature: string;
python: boolean;
+ /** package.json at the root. */
+ node: boolean;
+ /** node_modules/ at the root (installed dependencies, for npm ls). */
+ nodeModules: boolean;
};
/** The manifest and lockfile names at `root` and a signature of their set and stamps. */
@@ -122,7 +155,8 @@ export async function projectFiles(root: string): Promise<ProjectFiles> {
}
manifests.sort();
lockfiles.sort();
- const stamps = await Promise.all([...manifests, ...lockfiles].map(async (name) => {
+ const envFiles = names.filter((name) => /^\.env(?:\..+)?$/.test(name)).sort();
+ const stamps = await Promise.all([...manifests, ...lockfiles, ...envFiles].map(async (name) => {
try {
const info = await stat(nodePath.join(root, name));
return `${name}:${info.size}:${Math.trunc(info.mtimeMs)}`;
@@ -132,7 +166,16 @@ export async function projectFiles(root: string): Promise<ProjectFiles> {
}));
const venv = VENV_DIRS.filter((dir) => existsSync(nodePath.join(root, dir, "pyvenv.cfg")));
const python = venv.length > 0 || [...manifests, ...lockfiles].some((name) => PYTHON_MANIFESTS.has(name) || REQUIREMENTS.test(name));
- return { manifests, lockfiles, signature: [...stamps, ...venv.map((dir) => `venv:${dir}`)].join("|"), python };
+ const node = names.includes("package.json");
+ const nodeModules = node && existsSync(nodePath.join(root, "node_modules"));
+ return {
+ manifests,
+ lockfiles,
+ signature: [...stamps, ...venv.map((dir) => `venv:${dir}`), nodeModules ? "node_modules" : ""].join("|"),
+ python,
+ node,
+ nodeModules,
+ };
}
// --- command resolution ------------------------------------------------------
@@ -200,7 +243,7 @@ export const runCommand: RunCommand = (file, args, { cwd, timeoutMs, maxBytes })
// Probes run in the background: never at the expense of the agent's own work.
if (child.pid) {
try {
- setPriority(child.pid, osConstants.priority.PRIORITY_LOW);
+ setPriority(child.pid, osConstants.priority.PRIORITY_BELOW_NORMAL);
} catch {
// Not permitted here; it runs at normal priority.
}
@@ -256,8 +299,8 @@ export function stripPaths(text: string): string {
}
/** The version line a probe printed: its first non-empty line, paths stripped, capped. */
-export function parseProbeOutput(stdout: string, stderr: string, preferStderr = false): string | null {
- const pick = (text: string) => text.replace(ANSI, "").split(/\r?\n/).map((line) => line.trim()).find((line) => line.length > 0) ?? "";
+export function parseProbeOutput(stdout: string, stderr: string, preferStderr = false, line?: RegExp): string | null {
+ const pick = (text: string) => text.replace(ANSI, "").split(/\r?\n/).map((part) => part.trim()).find((part) => (line ? line.test(part) : part.length > 0)) ?? "";
const first = preferStderr ? (pick(stderr) || pick(stdout)) : (pick(stdout) || pick(stderr));
if (!first) return null;
const clean = stripPaths(first).replace(/[\u0000-\u001f\u007f]/g, " ").trim();
@@ -377,6 +420,10 @@ export type CollectOptions = {
platform?: NodeJS.Platform;
now?: () => number;
files?: ProjectFiles;
+ /** The capture's secret rules, for .env values and the source scan. */
+ checks?: SecretChecks;
+ /** Environment for the shell and version-manager detection (process.env). */
+ env?: NodeJS.ProcessEnv;
};
/**
@@ -410,7 +457,7 @@ export async function collectToolchain(root: string, options: CollectOptions = {
if (platform === "darwin" && javaWouldPrompt(probe.command, file)) return null;
const result = await run(file, probe.args, { cwd: root, timeoutMs: probeTimeoutMs, maxBytes: 16 * 1024 }).catch(() => null);
if (!result || result.code !== 0) return null;
- const version = parseProbeOutput(result.stdout, result.stderr, probe.stderr);
+ const version = parseProbeOutput(result.stdout, result.stderr, probe.stderr, probe.pick);
return version ? [probe.key, version] : null;
};
@@ -423,6 +470,20 @@ export async function collectToolchain(root: string, options: CollectOptions = {
return version ? ["venv_python", version] : null;
})
: Promise.resolve(null);
+ const checks = options.checks ?? {};
+ // One-shot text runs (sw_vers, the shell's --version), under the same guard.
+ const runText = async (file: string, args: string[]): Promise<string | null> => {
+ if (await cltSkipReason(nodePath.basename(file), file)) return null;
+ const result = await run(file, args, { cwd: root, timeoutMs: probeTimeoutMs, maxBytes: 16 * 1024 }).catch(() => null);
+ return result && result.code === 0 ? result.stdout || result.stderr : null;
+ };
+ const extras = Promise.all([
+ files.node && files.nodeModules ? npmLs(root, where, run, options.freezeTimeoutMs ?? FREEZE_TIMEOUT_MS, skipped) : Promise.resolve(null),
+ systemInfo(runText, platform).catch(() => null),
+ shellInfo(runText, { env: options.env, platform }).catch(() => null),
+ collectEnvFiles(root, checks).catch(() => []),
+ collectEnvNamesRead(root, checks).catch(() => ({ names: [], truncated: false })),
+ ]);
const pairs = await Promise.all([...(options.probes ?? TOOLCHAIN_PROBES).map(versionOf), venvVersion]);
const versions: Record<string, string> = {};
for (const pair of pairs) {
@@ -449,10 +510,73 @@ export async function collectToolchain(root: string, options: CollectOptions = {
toolchain.pip_freeze_tool = snapshot.tool;
if (snapshot.truncated) toolchain.pip_freeze_truncated = true;
}
+ const [npm, system, shell, envFiles, namesRead] = await extras;
+ if (npm) toolchain.npm_ls = npm;
+ if (system) toolchain.system = system;
+ if (shell) toolchain.shell = shell;
+ if (envFiles.length > 0) toolchain.env_files = envFiles;
+ if (namesRead.names.length > 0) toolchain.env_names_read = namesRead.names;
+ if (namesRead.truncated) toolchain.env_names_read_truncated = true;
toolchain.duration_ms = Math.max(0, Math.round(now() - started));
return toolchain;
}
+const NPM_NAME = /^(?:@[a-z0-9._~-]+\/)?[a-z0-9._~-]+$/i;
+const NPM_VERSION = /^[0-9A-Za-z.+-]{1,64}$/;
+
+/**
+ * `npm ls --json --depth=0` reduced to `{name, version, dependencies: {dep:
+ * version}}`: a dependency with no plain version (a link, a git or file
+ * reference) is `local`, a missing one `missing`; nothing else (resolved
+ * URLs, paths) is kept. Capped at MAX_NPM_LS_BYTES.
+ */
+export function summarizeNpmLs(stdout: string): NpmLs | null {
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(stdout);
+ } catch {
+ return null;
+ }
+ if (!parsed || typeof parsed !== "object") return null;
+ const tree = parsed as { name?: unknown; version?: unknown; dependencies?: Record<string, { version?: unknown; missing?: unknown }> };
+ const summary: NpmLs = { dependencies: {} };
+ if (typeof tree.name === "string" && NPM_NAME.test(tree.name)) summary.name = tree.name;
+ if (typeof tree.version === "string" && NPM_VERSION.test(tree.version)) summary.version = tree.version;
+ let bytes = JSON.stringify(summary).length;
+ for (const [name, dep] of Object.entries(tree.dependencies ?? {})) {
+ if (!NPM_NAME.test(name)) continue;
+ const version = typeof dep?.version === "string" && NPM_VERSION.test(dep.version) ? dep.version : dep?.missing ? "missing" : "local";
+ const size = JSON.stringify(name).length + JSON.stringify(version).length + 2;
+ if (bytes + size > MAX_NPM_LS_BYTES) {
+ summary.truncated = true;
+ break;
+ }
+ summary.dependencies[name] = version;
+ bytes += size;
+ }
+ return summary;
+}
+
+async function npmLs(
+ root: string,
+ where: (command: string) => string | null,
+ run: RunCommand,
+ timeoutMs: number,
+ skipped: Record<string, string>,
+): Promise<NpmLs | null> {
+ const npm = where("npm");
+ if (!npm) return null;
+ const reason = await cltSkipReason("npm", npm);
+ if (reason) {
+ skipped.npm_ls = reason;
+ return null;
+ }
+ const result = await run(npm, ["ls", "--json", "--depth=0"], { cwd: root, timeoutMs, maxBytes: MAX_PROBE_OUTPUT_BYTES * 4 }).catch(() => null);
+ // npm ls exits 1 for missing or extraneous packages and still prints the tree.
+ if (!result || (result.code !== 0 && result.code !== 1)) return null;
+ return summarizeNpmLs(result.stdout);
+}
+
async function freezeSnapshot(
root: string,
venvPython: string | null,