Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(uploader): record toolchain versions for nested projects

ogsamratba0a56bf187f
diff --git a/apps/server/src/toolchain.test.ts b/apps/server/src/toolchain.test.ts
index 250ff5ce..a4fd096d 100644
--- a/apps/server/src/toolchain.test.ts
+++ b/apps/server/src/toolchain.test.ts
@@ -357,3 +357,290 @@ test.skipIf(!hasZstd)("a short session still records it: the last snapshot waits
assert.equal("toolchain" in start.environment, false);
assert.deepEqual(end.environment.toolchain.versions, { node: "v22.19.0" });
});
+
+// Nested projects: a monorepo's apps/web and apps/api, a backend/python with
+// its own venv. Manifests are searched a few levels down, each Python project
+// is frozen with the environment next to it, each installed Node folder gets
+// its own npm ls, and the size caps are shared between them.
+
+const npmTree = (name, deps) => ok(JSON.stringify({ name, version: "1.0.0", dependencies: Object.fromEntries(Object.entries(deps).map(([dep, version]) => [dep, { version }])) }));
+
+test("nested: backend/python with its venv records its manifests, venv interpreter and pip freeze; frontend its npm ls", async () => {
+ const root = tempProject({
+ "README.md": "# app\n",
+ "backend/python/requirements.txt": "fastapi\n",
+ "backend/python/venv/pyvenv.cfg": "home = /usr/bin\n",
+ "backend/python/venv/bin/python": "",
+ "backend/python/venv/lib/python3.12/site-packages/fastapi/setup.py": "",
+ "frontend/package.json": "{}",
+ "frontend/package-lock.json": "{}",
+ "frontend/node_modules/react/package.json": "{}",
+ });
+ const calls = [];
+ const result = await toolchain.collectToolchain(root, {
+ env: {},
+ resolve: (command) => (["python3", "npm"].includes(command) ? `/usr/bin/${command}` : null),
+ run: fakeRun({
+ python3: ok("Python 3.10.12\n"),
+ python: (args) => (args[0] === "--version" ? ok("Python 3.12.3\n") : ok("fastapi==0.115.0\nuvicorn==0.30.6\n")),
+ npm: (args) => (args[0] === "ls" ? npmTree("frontend", { react: "18.3.1" }) : ok("10.8.2\n")),
+ }, calls),
+ });
+ assert.deepEqual(result.manifests, ["backend/python/requirements.txt", "frontend/package.json"]);
+ assert.deepEqual(result.lockfiles, ["frontend/package-lock.json"]);
+ assert.equal(result.python_executable_kind, "venv");
+ assert.equal(result.versions.venv_python, "Python 3.12.3");
+ assert.equal(result.versions.python3, "Python 3.10.12");
+ assert.deepEqual(result.pip_freeze, ["fastapi==0.115.0", "uvicorn==0.30.6"], "old consumers: the first project's freeze at the top level");
+ assert.equal(result.pip_freeze_tool, "pip");
+ assert.equal(result.pip_freeze_dir, "backend/python");
+ assert.deepEqual(result.pip_freeze_by_dir, { "backend/python": ["fastapi==0.115.0", "uvicorn==0.30.6"] });
+ assert.deepEqual(result.python_executable_kind_by_dir, { "backend/python": "venv" });
+ assert.deepEqual(result.venv_python_by_dir, { "backend/python": "Python 3.12.3" });
+ assert.deepEqual(result.npm_ls, { name: "frontend", version: "1.0.0", dependencies: { react: "18.3.1" } });
+ assert.equal(result.npm_ls_dir, "frontend");
+ assert.deepEqual(result.npm_ls_by_dir, { frontend: { name: "frontend", version: "1.0.0", dependencies: { react: "18.3.1" } } });
+ assert.equal(result.skipped, undefined);
+ const venvPython = path.join(root, "backend", "python", "venv", "bin", "python");
+ const freeze = calls.find((call) => call.args[0] === "-m");
+ assert.equal(freeze.file, venvPython);
+ assert.equal(freeze.cwd, path.join(root, "backend", "python"));
+ assert.equal(calls.find((call) => call.args[0] === "ls").cwd, path.join(root, "frontend"));
+ assert.ok(!calls.some((call) => call.file === "/usr/bin/python3" && call.args[0] === "-m"), "the system interpreter is not frozen for a project with its own venv");
+ assert.ok(!JSON.stringify(result).includes(root), "no absolute path is recorded");
+
+ // Without node_modules the nested package.json says why there is no npm ls.
+ const bare = await toolchain.collectToolchain(tempProject({ "web/package.json": "{}" }), { env: {}, resolve: () => null, run: fakeRun({}) });
+ assert.deepEqual(bare.manifests, ["web/package.json"]);
+ assert.equal(bare.skipped?.npm_ls, "not_installed");
+});
+
+test("nested: a monorepo with apps/web and apps/api; a venv above a project is found; one interpreter is frozen once", async () => {
+ const root = tempProject({
+ "package.json": "{}",
+ "pnpm-lock.yaml": "",
+ "node_modules/turbo/package.json": "{}",
+ "apps/web/package.json": "{}",
+ "apps/web/node_modules/next/package.json": "{}",
+ "apps/api/pyproject.toml": "[project]\nname='api'\n",
+ "apps/api/.venv/pyvenv.cfg": "",
+ "apps/api/.venv/bin/python": "",
+ "apps/api/worker/requirements.txt": "celery\n",
+ "apps/docs/package.json": "{}",
+ "tools/requirements-dev.txt": "ruff\n",
+ });
+ const calls = [];
+ const result = await toolchain.collectToolchain(root, {
+ env: {},
+ resolve: (command) => (["python3", "npm", "uv"].includes(command) ? `/usr/bin/${command}` : null),
+ run: fakeRun({
+ python3: ok("Python 3.11.9\n"),
+ python: ok("Python 3.12.3\n"),
+ uv: (args) => {
+ if (args[0] !== "pip") return ok("uv 0.5.0\n");
+ return args[3].includes(".venv") ? ok("fastapi==0.115.0\n") : ok("ruff==0.6.9\n");
+ },
+ npm: (args) => {
+ if (args[0] !== "ls") return ok("10.8.2\n");
+ return calls.at(-1).cwd.endsWith("web") ? npmTree("web", { next: "15.0.3" }) : npmTree("mono", { turbo: "2.1.0" });
+ },
+ }, calls),
+ });
+ assert.deepEqual(result.manifests, ["apps/api/pyproject.toml", "apps/api/worker/requirements.txt", "apps/docs/package.json", "apps/web/package.json", "package.json", "tools/requirements-dev.txt"]);
+ assert.deepEqual(result.lockfiles, ["pnpm-lock.yaml"]);
+ // The root is a Node project only: the first Python project found (shallowest, then by name) is the top level.
+ assert.equal(result.pip_freeze_dir, "tools");
+ assert.deepEqual(result.pip_freeze, ["ruff==0.6.9"]);
+ assert.equal(result.python_executable_kind, "system");
+ assert.equal(result.pip_freeze_tool, "uv");
+ assert.deepEqual(result.pip_freeze_by_dir, {
+ tools: ["ruff==0.6.9"],
+ "apps/api": ["fastapi==0.115.0"],
+ "apps/api/worker": ["fastapi==0.115.0"],
+ });
+ assert.deepEqual(result.python_executable_kind_by_dir, { tools: "system", "apps/api": "venv", "apps/api/worker": "venv" });
+ assert.deepEqual(result.venv_python_by_dir, { "apps/api": "Python 3.12.3", "apps/api/worker": "Python 3.12.3" });
+ const freezes = calls.filter((call) => call.args[0] === "pip");
+ assert.equal(freezes.length, 2, "apps/api and its worker share the .venv: frozen once");
+ assert.equal(result.npm_ls_dir, ".");
+ assert.deepEqual(result.npm_ls.dependencies, { turbo: "2.1.0" });
+ assert.deepEqual(Object.keys(result.npm_ls_by_dir), [".", "apps/web"]);
+ assert.deepEqual(result.npm_ls_by_dir["apps/web"].dependencies, { next: "15.0.3" });
+ assert.equal(calls.filter((call) => call.args[0] === "ls").length, 2, "apps/docs has no node_modules: no npm ls there");
+
+ // A project whose root alone is Python looks exactly as before: no per-folder fields.
+ const flat = await toolchain.collectToolchain(tempProject({ "requirements.txt": "attrs\n", ".venv/pyvenv.cfg": "", ".venv/bin/python": "" }), {
+ env: {},
+ resolve: () => null,
+ run: fakeRun({ python: (args) => (args[0] === "--version" ? ok("Python 3.12.3\n") : ok("attrs==24.2.0\n")) }),
+ });
+ assert.deepEqual(flat.pip_freeze, ["attrs==24.2.0"]);
+ assert.equal(flat.python_executable_kind, "venv");
+ for (const key of ["pip_freeze_dir", "pip_freeze_by_dir", "python_executable_kind_by_dir", "venv_python_by_dir", "npm_ls_dir", "npm_ls_by_dir"]) {
+ assert.equal(flat[key], undefined, key);
+ }
+});
+
+test("nested: dependency, build, cache and environment folders are never searched; three levels down at most; no symlinks", async () => {
+ const root = tempProject({
+ "package.json": "{}",
+ "node_modules/left-pad/package.json": "{}",
+ ".git/package.json": "{}",
+ ".cache/pyproject.toml": "",
+ ".tox/py312/setup.py": "",
+ "dist/package.json": "{}",
+ "build/requirements.txt": "",
+ "target/Cargo.toml": "",
+ "vendor/github.com/x/go.mod": "",
+ "__pycache__/setup.py": "",
+ "env/pyvenv.cfg": "",
+ "env/lib/python3.12/site-packages/pkg/setup.py": "",
+ "myenv/pyvenv.cfg": "",
+ "myenv/lib/python3.12/site-packages/other/pyproject.toml": "",
+ "conda/conda-meta/history": "",
+ "conda/lib/pkg/setup.py": "",
+ "a/b/c/go.mod": "",
+ "a/b/c/d/Cargo.toml": "",
+ "a/b/c/venv/pyvenv.cfg": "",
+ "services/api/Gemfile": "",
+ "services/api/Gemfile.lock": "",
+ "services/java/pom.xml": "",
+ "services/java/build.gradle.kts": "",
+ "services/py/setup.cfg": "",
+ "services/py/Pipfile": "",
+ "services/py/poetry.lock": "",
+ "services/py/uv.lock": "",
+ "services/js/bun.lockb": "",
+ "services/js/yarn.lock": "",
+ });
+ if (process.platform !== "win32") fs.symlinkSync(path.join(root, "services"), path.join(root, "link"), "dir");
+ const files = await toolchain.projectFiles(root);
+ assert.deepEqual(files.manifests, [
+ "a/b/c/go.mod",
+ "package.json",
+ "services/api/Gemfile",
+ "services/java/build.gradle.kts",
+ "services/java/pom.xml",
+ "services/py/Pipfile",
+ "services/py/setup.cfg",
+ ]);
+ assert.deepEqual(files.lockfiles, ["services/api/Gemfile.lock", "services/js/bun.lockb", "services/js/yarn.lock", "services/py/poetry.lock", "services/py/uv.lock"]);
+ // Any folder with pyvenv.cfg is an environment, found at the last level too.
+ assert.deepEqual(files.venvs, ["a/b/c/venv", "env", "myenv"]);
+ assert.deepEqual(files.pythonDirs, [".", "services/py", "a/b/c"], "shallowest first");
+ assert.equal(files.truncated, false);
+ assert.equal(toolchain.venvFor("services/py", files.venvs), "env", "the nearest environment above, preferred names first");
+ assert.equal(toolchain.venvFor("a/b/c", files.venvs), "a/b/c/venv");
+ assert.equal(toolchain.venvFor(".", ["myenv", ".venv", "venv"]), ".venv");
+ assert.equal(toolchain.venvFor("x", []), null);
+});
+
+test("nested: the search stops at 200 folders and 2 s, and says so", async () => {
+ const many = {};
+ for (let i = 0; i < 260; i += 1) many[`pkg${String(i).padStart(3, "0")}/package.json`] = "{}";
+ const root = tempProject(many);
+ const files = await toolchain.projectFiles(root);
+ assert.equal(files.truncated, true);
+ assert.equal(files.manifests.length, 199, "the root and 199 folders read");
+ assert.equal(toolchain.MANIFEST_SCAN_MAX_DIRS, 200);
+ assert.equal(toolchain.MANIFEST_SCAN_BUDGET_MS, 2_000);
+
+ let clock = 0;
+ const slow = await toolchain.projectFiles(root, { now: () => (clock += 500) });
+ assert.equal(slow.truncated, true);
+ assert.ok(slow.manifests.length <= 4, `${slow.manifests.length} folders read past the deadline`);
+
+ const result = await toolchain.collectToolchain(root, { env: {}, files, resolve: () => null, run: fakeRun({}) });
+ assert.equal(result.manifests_truncated, true);
+ assert.equal(result.manifests.length, 199);
+});
+
+test("nested: more than 8 projects are not all probed; the freeze caps are shared, the top level keeps its own", async () => {
+ const layout = {};
+ for (let i = 0; i < 10; i += 1) {
+ layout[`svc${i}/requirements.txt`] = "x\n";
+ layout[`svc${i}/.venv/pyvenv.cfg`] = "";
+ layout[`svc${i}/.venv/bin/python`] = "";
+ }
+ const root = tempProject(layout);
+ const freezeOf = (cwd) => {
+ const n = Number(/svc(\d)/.exec(cwd)[1]);
+ // svc0: small; the rest: 1500 entries each.
+ return n === 0 ? "tiny==1.0\n" : Array.from({ length: 1500 }, (_, i) => `svc${n}-package-with-a-long-name-${i}==1.0.${i}`).join("\n");
+ };
+ const calls = [];
+ const result = await toolchain.collectToolchain(root, {
+ env: {},
+ resolve: () => null,
+ run: async (file, args, options) => {
+ calls.push({ file, args, cwd: options.cwd });
+ return args[0] === "--version" ? ok("Python 3.12.3\n") : ok(freezeOf(options.cwd));
+ },
+ });
+ assert.equal(result.projects_truncated, true);
+ assert.equal(calls.filter((call) => call.args[0] === "-m").length, 8);
+ const byDir = result.pip_freeze_by_dir;
+ assert.equal(Object.keys(byDir).length, 8);
+ assert.deepEqual(byDir.svc0, ["tiny==1.0"]);
+ const all = Object.values(byDir).flat();
+ assert.ok(all.length <= toolchain.MAX_FREEZE_LINES, `${all.length} lines in all`);
+ assert.ok(all.reduce((sum, line) => sum + Buffer.byteLength(line) + 1, 0) <= toolchain.MAX_FREEZE_BYTES);
+ assert.deepEqual(result.pip_freeze_truncated_dirs, ["svc1", "svc2", "svc3", "svc4", "svc5", "svc6", "svc7"]);
+ assert.equal(result.pip_freeze_dir, "svc0");
+ assert.deepEqual(result.pip_freeze, ["tiny==1.0"]);
+
+ assert.deepEqual(toolchain.splitBudget([10, 5000, 5000], 2000), [10, 995, 995]);
+ assert.deepEqual(toolchain.splitBudget([100, 200], 2000), [100, 200]);
+ assert.deepEqual(toolchain.splitBudget([], 2000), []);
+
+ // npm ls: one 64 KiB cap shared between folders.
+ const web = tempProject({ "a/package.json": "{}", "a/node_modules/x/package.json": "{}", "b/package.json": "{}", "b/node_modules/x/package.json": "{}" });
+ const big = (prefix) => ok(JSON.stringify({ dependencies: Object.fromEntries(Array.from({ length: 3000 }, (_, i) => [`${prefix}-dependency-${i}`, { version: "1.2.3" }])) }));
+ const npm = await toolchain.collectToolchain(web, {
+ env: {},
+ resolve: (command) => (command === "npm" ? "/usr/bin/npm" : null),
+ run: async (_file, args, options) => (args[0] === "ls" ? big(path.basename(options.cwd)) : ok("10.8.2\n")),
+ });
+ assert.equal(npm.npm_ls_dir, "a");
+ assert.ok(npm.npm_ls.truncated);
+ const sizes = Object.values(npm.npm_ls_by_dir).map((summary) => JSON.stringify(summary).length);
+ assert.equal(sizes.length, 2);
+ assert.ok(sizes.reduce((a, b) => a + b, 0) <= toolchain.MAX_NPM_LS_BYTES + 64, sizes.join(" + "));
+ assert.ok(Object.values(npm.npm_ls_by_dir).every((summary) => summary.truncated));
+});
+
+test.skipIf(process.platform === "win32")("nested: a project's freeze that hangs is killed at its timeout; the others are still recorded", async () => {
+ const script = (freeze) => `#!/bin/sh\nif [ "$1" = "--version" ]; then echo "Python 3.12.3"; exit 0; fi\n${freeze}\n`;
+ const root = tempProject({
+ "fast/requirements.txt": "attrs\n",
+ "fast/venv/pyvenv.cfg": "",
+ "fast/venv/bin/python": script('echo "attrs==24.2.0"'),
+ "slow/requirements.txt": "attrs\n",
+ "slow/venv/pyvenv.cfg": "",
+ "slow/venv/bin/python": script("exec sleep 30"),
+ });
+ fs.chmodSync(path.join(root, "fast/venv/bin/python"), 0o755);
+ fs.chmodSync(path.join(root, "slow/venv/bin/python"), 0o755);
+ const started = Date.now();
+ const result = await toolchain.collectToolchain(root, { env: {}, resolve: () => null, freezeTimeoutMs: 300, probes: [] });
+ assert.ok(Date.now() - started < 5_000, `took ${Date.now() - started} ms`);
+ assert.deepEqual(result.pip_freeze_by_dir, { fast: ["attrs==24.2.0"] });
+ assert.deepEqual(result.python_executable_kind_by_dir, { fast: "venv", slow: "venv" });
+ assert.deepEqual(result.venv_python_by_dir, { fast: "Python 3.12.3", slow: "Python 3.12.3" });
+});
+
+test("nested: the cache collects again when a nested manifest or environment changes", async () => {
+ const root = tempProject({ "backend/requirements.txt": "attrs\n" });
+ const cache = new toolchain.ToolchainCache({ waitMs: 10_000, env: {}, resolve: () => null, run: fakeRun({}) });
+ await cache.get(root);
+ await cache.get(root);
+ assert.equal(cache.collections, 1);
+ fs.mkdirSync(path.join(root, "backend", "venv"));
+ fs.writeFileSync(path.join(root, "backend", "venv", "pyvenv.cfg"), "");
+ await cache.get(root);
+ assert.equal(cache.collections, 2);
+ fs.writeFileSync(path.join(root, "backend", "requirements.txt"), "attrs\nrequests\n");
+ const last = await cache.get(root);
+ assert.equal(cache.collections, 3);
+ assert.deepEqual(last.manifests, ["backend/requirements.txt"]);
+});
diff --git a/apps/server/src/toolchain.ts b/apps/server/src/toolchain.ts
index 92fc141f..272dd21b 100644
--- a/apps/server/src/toolchain.ts
+++ b/apps/server/src/toolchain.ts
@@ -6,13 +6,16 @@
// and .tool-versions shims answer for the version the project actually uses),
// only for a command that resolves on PATH, with a short timeout, all in
// parallel. A probe that fails, times out or prints nothing is left out.
+// Projects nested in the root (a monorepo's apps/web, a backend/python with
+// its own venv) are found by a bounded manifest search and get their own
+// pip freeze and npm ls, run in their folder.
// Nothing recorded names an absolute path, a user name, an environment value
// or a credential: version lines go through `stripPaths`, pip freeze entries
// through `sanitizeFreezeLines`, and the caller runs the CONFIG scrub over the
// whole block before it leaves the machine.
import { spawn } from "node:child_process";
-import { existsSync, readdirSync, statSync } from "node:fs";
+import { readdirSync, statSync, type Dirent } from "node:fs";
import { readdir, stat } from "node:fs/promises";
import { constants as osConstants, setPriority } from "node:os";
import nodePath from "node:path";
@@ -49,17 +52,46 @@ export type UploadToolchain = {
versions: Record<string, string>;
/** Tools on PATH that were deliberately not run, and why (`xcode_clt_missing`). */
skipped?: Record<string, string>;
+ /** Of the interpreter the top-level pip_freeze came from. */
python_executable_kind?: PythonExecutableKind;
- /** Manifest and version-pin files at the project root (names only). */
+ /**
+ * Manifest and version-pin files, as paths relative to the project root,
+ * nested projects included (`package.json`, `backend/python/requirements.txt`).
+ */
manifests: string[];
- /** Lockfiles at the project root (names only). */
+ /** Lockfiles, as paths relative to the project root. */
lockfiles: string[];
- /** `pip freeze` of the project's interpreter, sanitised; only with a Python manifest. */
+ /** The manifest search stopped at its folder or time cap. */
+ manifests_truncated?: boolean;
+ /**
+ * `pip freeze` of the project's interpreter, sanitised; only with a Python
+ * project. The root's, or the first nested project's when the root is not
+ * one (`pip_freeze_dir` then names it).
+ */
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. */
+ /** With nested Python projects: the folder (`.` for the root) the top-level pip_freeze describes. */
+ pip_freeze_dir?: string;
+ /** With nested Python projects: pip freeze per project folder, the size caps shared between them. */
+ pip_freeze_by_dir?: Record<string, string[]>;
+ /** Folders whose pip_freeze_by_dir entry was cut to its share of the caps. */
+ pip_freeze_truncated_dirs?: string[];
+ /** With nested Python projects: the interpreter kind per project folder. */
+ python_executable_kind_by_dir?: Record<string, PythonExecutableKind>;
+ /** With nested Python projects: the virtual environment's `python --version` per project folder that has one. */
+ venv_python_by_dir?: Record<string, string>;
+ /**
+ * `npm ls --json --depth=0`, reduced to direct dependency versions; only with
+ * package.json and node_modules. The root's, or the first nested folder's.
+ */
npm_ls?: NpmLs;
+ /** With nested Node projects: the folder the top-level npm_ls describes. */
+ npm_ls_dir?: string;
+ /** With nested Node projects: npm ls per folder with package.json and node_modules, the size cap shared between them. */
+ npm_ls_by_dir?: Record<string, NpmLs>;
+ /** More than MAX_PROJECT_DIRS Python or Node projects: the rest were not probed. */
+ projects_truncated?: boolean;
/** OS release, WSL, CPU. */
system?: SystemInfo;
/** Shell name and version, version managers in use, PATH (home as `~`, never the user name). */
@@ -125,62 +157,210 @@ const MANIFESTS = new Set([
]);
const PYTHON_MANIFESTS = new Set(["pyproject.toml", "setup.py", "setup.cfg", "Pipfile", "Pipfile.lock", "poetry.lock", "uv.lock", "pdm.lock", ".python-version"]);
const REQUIREMENTS = /^requirements[\w.-]*\.(txt|in)$/i;
+/** Preferred virtual environment names, in order; any folder holding pyvenv.cfg is one too. */
const VENV_DIRS = [".venv", "venv", "env"];
+/** How far below the project root manifests are searched (the root is level 0). */
+export const MANIFEST_SCAN_DEPTH = 3;
+/** Folders read per search, the root included. */
+export const MANIFEST_SCAN_MAX_DIRS = 200;
+/** Wall time for one search. */
+export const MANIFEST_SCAN_BUDGET_MS = 2_000;
+/** Python environments, and Node folders, probed per project (each). */
+export const MAX_PROJECT_DIRS = 8;
+/**
+ * Never searched: installed dependencies, build output, caches. Dotted folders
+ * (.git, .cache, .venv, .tox …) and virtual or conda environments are skipped too.
+ */
+const SKIP_DIRS = new Set([
+ "node_modules", "bower_components", "jspm_packages", "vendor", "build", "dist", "target", "out", "coverage",
+ "__pycache__", "site-packages", "Pods", "DerivedData",
+]);
+
export type ProjectFiles = {
+ /** Manifest and version-pin files, as paths relative to the root (`backend/python/requirements.txt`). */
manifests: string[];
+ /** Lockfiles, as paths relative to the root. */
lockfiles: string[];
- /** Changes when a manifest, lockfile or root .env file appears, goes, or is rewritten. */
+ /** Changes when a manifest, lockfile, virtual environment, node_modules or root .env file appears, goes, or is rewritten. */
signature: string;
- /** A Python project: a Python manifest or lockfile, a virtual environment, or a .py file at the root. */
+ /** A Python project at the root: a Python manifest or lockfile, a virtual environment, or a .py file. */
python: boolean;
/** package.json at the root. */
node: boolean;
/** node_modules/ at the root (installed dependencies, for npm ls). */
nodeModules: boolean;
+ /** Folders holding a Python project (`.` for the root), shallowest first. */
+ pythonDirs: string[];
+ /** Folders holding a package.json, shallowest first. */
+ nodeDirs: string[];
+ /** Folders holding a package.json and node_modules/, shallowest first. */
+ nodeModulesDirs: string[];
+ /** Virtual environments (folders with pyvenv.cfg), relative to the root. */
+ venvs: string[];
+ /** The search stopped at its folder or time cap. */
+ truncated: boolean;
};
-/** The manifest and lockfile names at `root` and a signature of their set and stamps. */
-export async function projectFiles(root: string): Promise<ProjectFiles> {
- let names: string[] = [];
- try {
- names = await readdir(root);
- } catch {
- names = [];
- }
+export type ScanOptions = { maxDepth?: number; maxDirs?: number; budgetMs?: number; now?: () => number };
+
+/** `a/b` → `a`, `a` → `.`. */
+function parentDir(rel: string): string {
+ const cut = rel.lastIndexOf("/");
+ return cut < 0 ? "." : rel.slice(0, cut);
+}
+
+function dirDepth(rel: string): number {
+ return rel === "." ? 0 : rel.split("/").length;
+}
+
+/** Shallowest first, then by name: the root, then `api`, then `apps/web`. */
+function byDepth(a: string, b: string): number {
+ return dirDepth(a) - dirDepth(b) || (a < b ? -1 : a > b ? 1 : 0);
+}
+
+function isVenvListing(name: string, names: Set<string>): boolean {
+ if (names.has("pyvenv.cfg")) return true;
+ // virtualenv < 20 wrote no pyvenv.cfg.
+ return VENV_DIRS.includes(name) && (names.has("bin") || names.has("Scripts")) && (names.has("lib") || names.has("Lib"));
+}
+
+/**
+ * The manifests and lockfiles of the project at `root` and of the projects
+ * nested in it (a monorepo's `apps/web`, a `backend/python`), searched
+ * breadth first down to MANIFEST_SCAN_DEPTH levels, at most
+ * MANIFEST_SCAN_MAX_DIRS folders and MANIFEST_SCAN_BUDGET_MS; symlinks are
+ * never followed. Paths are relative to the root, with `/`.
+ */
+export async function projectFiles(root: string, scan: ScanOptions = {}): Promise<ProjectFiles> {
+ const maxDepth = scan.maxDepth ?? MANIFEST_SCAN_DEPTH;
+ const maxDirs = scan.maxDirs ?? MANIFEST_SCAN_MAX_DIRS;
+ const now = scan.now ?? Date.now;
+ const deadline = now() + (scan.budgetMs ?? MANIFEST_SCAN_BUDGET_MS);
const manifests: string[] = [];
const lockfiles: string[] = [];
- for (const name of names) {
- if (LOCKFILES.has(name)) lockfiles.push(name);
- else if (MANIFESTS.has(name) || REQUIREMENTS.test(name)) manifests.push(name);
+ const venvs: string[] = [];
+ const pythonDirs = new Set<string>();
+ const nodeDirs: string[] = [];
+ const nodeModulesDirs: string[] = [];
+ let rootNames: string[] = [];
+ let visited = 0;
+ let truncated = false;
+ const queue: Array<{ rel: string; depth: number }> = [{ rel: "", depth: 0 }];
+ for (let next = queue.shift(); next; next = queue.shift()) {
+ if (visited >= maxDirs || now() > deadline) {
+ truncated = true;
+ break;
+ }
+ const { rel, depth } = next;
+ const abs = rel ? nodePath.join(root, ...rel.split("/")) : root;
+ let entries: Dirent[];
+ try {
+ entries = await readdir(abs, { withFileTypes: true });
+ } catch {
+ continue;
+ }
+ visited += 1;
+ entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
+ const names = new Set(entries.map((entry) => entry.name));
+ const key = rel || ".";
+ if (rel) {
+ const base = rel.slice(rel.lastIndexOf("/") + 1);
+ if (isVenvListing(base, names)) {
+ venvs.push(rel);
+ pythonDirs.add(parentDir(rel));
+ continue;
+ }
+ if (base.startsWith(".") || names.has("conda-meta")) continue;
+ } else {
+ rootNames = [...names];
+ }
+ const at = (name: string) => (rel ? `${rel}/${name}` : name);
+ const children: string[] = [];
+ for (const entry of entries) {
+ const name = entry.name;
+ if (entry.isDirectory()) {
+ if (!name.startsWith(".") && !SKIP_DIRS.has(name)) children.push(name);
+ else if (VENV_DIRS.includes(name)) children.push(name); // .venv: read only to confirm it is one
+ continue;
+ }
+ if (LOCKFILES.has(name)) lockfiles.push(at(name));
+ else if (MANIFESTS.has(name) || REQUIREMENTS.test(name)) manifests.push(at(name));
+ else continue;
+ if (PYTHON_MANIFESTS.has(name) || REQUIREMENTS.test(name)) pythonDirs.add(key);
+ }
+ if (names.has("package.json")) {
+ nodeDirs.push(key);
+ if (names.has("node_modules")) nodeModulesDirs.push(key);
+ }
+ if (depth < maxDepth) {
+ for (const name of children) queue.push({ rel: at(name), depth: depth + 1 });
+ } else {
+ // A manifest at the last level still finds the environment next to it.
+ const found = await Promise.all(children.slice(0, 32).map(async (name) => {
+ try {
+ return (await stat(nodePath.join(abs, name, "pyvenv.cfg"))).isFile() ? name : null;
+ } catch {
+ return null;
+ }
+ }));
+ for (const name of found) {
+ if (!name) continue;
+ venvs.push(at(name));
+ pythonDirs.add(key);
+ }
+ }
}
manifests.sort();
lockfiles.sort();
- const envFiles = names.filter((name) => /^\.env(?:\..+)?$/.test(name)).sort();
- const stamps = await Promise.all([...manifests, ...lockfiles, ...envFiles].map(async (name) => {
+ venvs.sort();
+ const envFiles = rootNames.filter((name) => /^\.env(?:\..+)?$/.test(name)).sort();
+ const stamps = await Promise.all([...manifests, ...lockfiles, ...envFiles].map(async (rel) => {
try {
- const info = await stat(nodePath.join(root, name));
- return `${name}:${info.size}:${Math.trunc(info.mtimeMs)}`;
+ const info = await stat(nodePath.join(root, ...rel.split("/")));
+ return `${rel}:${info.size}:${Math.trunc(info.mtimeMs)}`;
} catch {
- return `${name}:-`;
+ return `${rel}:-`;
}
}));
- const venv = VENV_DIRS.filter((dir) => existsSync(nodePath.join(root, dir, "pyvenv.cfg")));
// A script-only Python project (`main.py`, no manifest) still runs on the interpreter's installed packages.
- const pyScripts = names.some((name) => /\.py$/i.test(name));
- const python = venv.length > 0 || pyScripts || [...manifests, ...lockfiles].some((name) => PYTHON_MANIFESTS.has(name) || REQUIREMENTS.test(name));
- const node = names.includes("package.json");
- const nodeModules = node && existsSync(nodePath.join(root, "node_modules"));
+ const pyScripts = rootNames.some((name) => /\.py$/i.test(name));
+ if (pyScripts) pythonDirs.add(".");
+ const node = nodeDirs.includes(".");
+ const nodeModules = nodeModulesDirs.includes(".");
return {
manifests,
lockfiles,
- signature: [...stamps, ...venv.map((dir) => `venv:${dir}`), nodeModules ? "node_modules" : "", pyScripts ? "py" : ""].join("|"),
- python,
+ signature: [
+ ...stamps,
+ ...venvs.map((dir) => `venv:${dir}`),
+ ...nodeModulesDirs.map((dir) => `node_modules:${dir}`),
+ pyScripts ? "py" : "",
+ ].join("|"),
+ python: pythonDirs.has("."),
node,
nodeModules,
+ pythonDirs: [...pythonDirs].sort(byDepth),
+ nodeDirs: nodeDirs.sort(byDepth),
+ nodeModulesDirs: nodeModulesDirs.sort(byDepth),
+ venvs,
+ truncated,
};
}
+/** The virtual environment a Python project in `dir` runs on: one next to it, else the nearest one above it, up to the root. */
+export function venvFor(dir: string, venvs: readonly string[]): string | null {
+ const rank = (rel: string) => {
+ const index = VENV_DIRS.indexOf(rel.slice(rel.lastIndexOf("/") + 1));
+ return index < 0 ? VENV_DIRS.length : index;
+ };
+ for (let at = dir; ; at = parentDir(at)) {
+ const here = venvs.filter((venv) => parentDir(venv) === at).sort((a, b) => rank(a) - rank(b) || (a < b ? -1 : 1));
+ if (here.length > 0) return here[0]!;
+ if (at === ".") return null;
+ }
+}
+
// --- command resolution ------------------------------------------------------
export { resolveCommand, type ResolveOptions } from "./command-guard.js";
@@ -326,7 +506,12 @@ function packageNameFrom(text: string): string | null {
* `name @ vcs` or `name @ url`; comments and index options are dropped.
* Capped at MAX_FREEZE_LINES entries and MAX_FREEZE_BYTES.
*/
-export function sanitizeFreezeLines(text: string): { lines: string[]; truncated: boolean } {
+export function sanitizeFreezeLines(
+ text: string,
+ caps: { maxLines?: number; maxBytes?: number } = {},
+): { lines: string[]; truncated: boolean } {
+ const maxLines = caps.maxLines ?? MAX_FREEZE_LINES;
+ const maxBytes = caps.maxBytes ?? MAX_FREEZE_BYTES;
const lines: string[] = [];
let bytes = 0;
let truncated = false;
@@ -362,7 +547,7 @@ export function sanitizeFreezeLines(text: string): { lines: string[]; truncated:
pendingEditableName = null;
if (!entry) continue;
const size = Buffer.byteLength(entry, "utf8") + 1;
- if (lines.length >= MAX_FREEZE_LINES || bytes + size > MAX_FREEZE_BYTES) {
+ if (lines.length >= maxLines || bytes + size > maxBytes) {
truncated = true;
break;
}
@@ -380,16 +565,21 @@ function localName(target: string): string | null {
// --- python ------------------------------------------------------------------
+/** The interpreter inside the virtual environment at `venv`, or null. */
+export function venvInterpreter(venv: string, platform: NodeJS.Platform = process.platform, isFile: (path: string) => boolean = defaultIsFile): string | null {
+ const path = platform === "win32" ? nodePath.win32 : nodePath.posix;
+ const candidates = platform === "win32"
+ ? [path.join(venv, "Scripts", "python.exe")]
+ : [path.join(venv, "bin", "python"), path.join(venv, "bin", "python3")];
+ return candidates.find((candidate) => isFile(candidate)) ?? null;
+}
+
/** The interpreter of the project's own virtual environment (.venv, venv, env), or null. */
export function projectVenvPython(root: string, platform: NodeJS.Platform = process.platform, isFile: (path: string) => boolean = defaultIsFile): string | null {
const path = platform === "win32" ? nodePath.win32 : nodePath.posix;
for (const dir of VENV_DIRS) {
- const candidates = platform === "win32"
- ? [path.join(root, dir, "Scripts", "python.exe")]
- : [path.join(root, dir, "bin", "python"), path.join(root, dir, "bin", "python3")];
- for (const candidate of candidates) {
- if (isFile(candidate)) return candidate;
- }
+ const python = venvInterpreter(path.join(root, dir), platform, isFile);
+ if (python) return python;
}
return null;
}
@@ -464,15 +654,44 @@ export async function collectToolchain(root: string, options: CollectOptions = {
return version ? [probe.key, version] : null;
};
- const venvPython = files.python ? projectVenvPython(root, platform) : null;
- const freeze = files.python ? freezeSnapshot(root, venvPython, where, run, options.freezeTimeoutMs ?? FREEZE_TIMEOUT_MS, skipped) : Promise.resolve(null);
- // The project's own interpreter, which PATH does not reach unless the venv is active.
- const venvVersion: Promise<[string, string] | null> = venvPython
- ? run(venvPython, ["--version"], { cwd: root, timeoutMs: probeTimeoutMs, maxBytes: 16 * 1024 }).catch(() => null).then((result) => {
- const version = result && result.code === 0 ? parseProbeOutput(result.stdout, result.stderr) : null;
- return version ? ["venv_python", version] : null;
- })
+ const freezeTimeoutMs = options.freezeTimeoutMs ?? FREEZE_TIMEOUT_MS;
+ const abs = (dir: string) => (dir === "." ? root : nodePath.join(root, ...dir.split("/")));
+ let projectsTruncated = false;
+
+ // Python: every project folder runs on the environment next to it (or the
+ // nearest one above it), else on the interpreter PATH finds.
+ if (files.pythonDirs.length > MAX_PROJECT_DIRS) projectsTruncated = true;
+ const pyProjects = files.pythonDirs.slice(0, MAX_PROJECT_DIRS).map((dir) => {
+ const venv = venvFor(dir, files.venvs);
+ return { dir, venvPython: venv ? venvInterpreter(abs(venv), platform) : null };
+ });
+ const systemPython = where("python3") ?? where("python");
+ // `uv pip freeze --python /usr/bin/python3` would run the shim too.
+ const systemSkip = systemPython && pyProjects.some((project) => !project.venvPython)
+ ? cltSkipReason(where("python3") === systemPython ? "python3" : "python", systemPython)
: Promise.resolve(null);
+ const interpreters = new Map<string, { cwd: string; venv: boolean }>();
+ for (const project of pyProjects) {
+ const python = project.venvPython ?? systemPython;
+ if (python && !interpreters.has(python)) interpreters.set(python, { cwd: abs(project.dir), venv: Boolean(project.venvPython) });
+ }
+ const freezes = Promise.all([...interpreters].map(async ([python, { cwd, venv }]) => {
+ const reason = venv ? null : await systemSkip;
+ if (reason) {
+ skipped.pip_freeze = reason;
+ return [python, null] as const;
+ }
+ return [python, await freezeRaw(python, cwd, where, run, freezeTimeoutMs)] as const;
+ })).then((entries) => new Map(entries));
+ // The project's own interpreter, which PATH does not reach unless the venv is active.
+ const venvVersions = Promise.all([...interpreters].filter(([, info]) => info.venv).map(async ([python, { cwd }]) => {
+ const result = await run(python, ["--version"], { cwd, timeoutMs: probeTimeoutMs, maxBytes: 16 * 1024 }).catch(() => null);
+ return [python, result && result.code === 0 ? parseProbeOutput(result.stdout, result.stderr) : null] as const;
+ })).then((entries) => new Map(entries));
+
+ // Node: npm ls in every folder with package.json and node_modules.
+ if (files.nodeModulesDirs.length > MAX_PROJECT_DIRS) projectsTruncated = true;
+ const npmDirs = files.nodeModulesDirs.slice(0, MAX_PROJECT_DIRS);
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> => {
@@ -481,19 +700,22 @@ export async function collectToolchain(root: string, options: CollectOptions = {
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),
+ npmDirs.length > 0 ? npmLsRaw(npmDirs.map(abs), where, run, freezeTimeoutMs, skipped) : Promise.resolve([]),
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 pairs = await Promise.all((options.probes ?? TOOLCHAIN_PROBES).map(versionOf));
const versions: Record<string, string> = {};
for (const pair of pairs) {
if (pair) versions[pair[0]] = pair[1];
}
if (versions.python && versions.python === versions.python3) delete versions.python;
- const snapshot = await freeze;
+ const [frozen, venvVersion] = await Promise.all([freezes, venvVersions]);
+ const primary = pyProjects[0];
+ const primaryVenv = primary?.venvPython ?? null;
+ if (primaryVenv && venvVersion.get(primaryVenv)) versions.venv_python = venvVersion.get(primaryVenv)!;
const toolchain: UploadToolchain = {
schema: TOOLCHAIN_SCHEMA_VERSION,
@@ -503,20 +725,69 @@ export async function collectToolchain(root: string, options: CollectOptions = {
manifests: files.manifests,
lockfiles: files.lockfiles,
};
- const python = venvPython ?? where("python3") ?? where("python");
- if (python && (venvPython || versions.python3 || versions.python)) {
- toolchain.python_executable_kind = pythonExecutableKind(python, { venv: Boolean(venvPython) });
+ if (files.truncated) toolchain.manifests_truncated = true;
+ const systemRuns = Boolean(versions.python3 || versions.python);
+ const kindOf = (venvPython: string | null): PythonExecutableKind | null => {
+ const python = venvPython ?? systemPython;
+ if (!python || !(venvPython || systemRuns)) return null;
+ return pythonExecutableKind(python, { venv: Boolean(venvPython) });
+ };
+ const primaryKind = kindOf(primaryVenv);
+ if (primaryKind) toolchain.python_executable_kind = primaryKind;
+ const primarySnapshot = primary ? frozen.get(primaryVenv ?? systemPython ?? "") : null;
+ if (primarySnapshot) {
+ const { lines, truncated } = sanitizeFreezeLines(primarySnapshot.stdout);
+ toolchain.pip_freeze = lines;
+ toolchain.pip_freeze_tool = primarySnapshot.tool;
+ if (truncated) toolchain.pip_freeze_truncated = true;
}
- // A package.json without node_modules: nothing is installed for npm ls to list; say so rather than leave it out silently.
- if (files.node && !files.nodeModules && !skipped.npm_ls) skipped.npm_ls = "not_installed";
- if (Object.keys(skipped).length > 0) toolchain.skipped = skipped;
- if (snapshot) {
- toolchain.pip_freeze = snapshot.lines;
- toolchain.pip_freeze_tool = snapshot.tool;
- if (snapshot.truncated) toolchain.pip_freeze_truncated = true;
+ if (pyProjects.some((project) => project.dir !== ".")) {
+ toolchain.pip_freeze_dir = primary!.dir;
+ // One share of the caps per interpreter; a project sharing one with an earlier folder repeats its list.
+ const full = new Map([...frozen].flatMap(([python, snapshot]) => (snapshot ? [[python, sanitizeFreezeLines(snapshot.stdout, { maxLines: Infinity, maxBytes: Infinity }).lines] as const] : [])));
+ const keys = [...full.keys()];
+ const lineShares = splitBudget(keys.map((key) => full.get(key)!.length), MAX_FREEZE_LINES);
+ const byteShares = splitBudget(keys.map((key) => full.get(key)!.reduce((sum, line) => sum + Buffer.byteLength(line, "utf8") + 1, 0)), MAX_FREEZE_BYTES);
+ const capped = new Map(keys.map((key, index) => [key, capLines(full.get(key)!, lineShares[index]!, byteShares[index]!)] as const));
+ const byDir: Record<string, string[]> = {};
+ const kinds: Record<string, PythonExecutableKind> = {};
+ const venvByDir: Record<string, string> = {};
+ const truncatedDirs: string[] = [];
+ for (const project of pyProjects) {
+ const kind = kindOf(project.venvPython);
+ if (kind) kinds[project.dir] = kind;
+ const version = project.venvPython ? venvVersion.get(project.venvPython) : null;
+ if (version) venvByDir[project.dir] = version;
+ const lines = capped.get(project.venvPython ?? systemPython ?? "");
+ if (!lines) continue;
+ byDir[project.dir] = lines.lines;
+ if (lines.truncated) truncatedDirs.push(project.dir);
+ }
+ if (Object.keys(byDir).length > 0) toolchain.pip_freeze_by_dir = byDir;
+ if (truncatedDirs.length > 0) toolchain.pip_freeze_truncated_dirs = truncatedDirs;
+ if (Object.keys(kinds).length > 0) toolchain.python_executable_kind_by_dir = kinds;
+ if (Object.keys(venvByDir).length > 0) toolchain.venv_python_by_dir = venvByDir;
}
- const [npm, system, shell, envFiles, namesRead] = await extras;
+ // A package.json without node_modules: nothing is installed for npm ls to list; say so rather than leave it out silently.
+ if (files.nodeDirs.length > 0 && npmDirs.length === 0 && !skipped.npm_ls) skipped.npm_ls = "not_installed";
+ const [npmOutputs, system, shell, envFiles, namesRead] = await extras;
+ const npm = npmOutputs[0] ? summarizeNpmLs(npmOutputs[0]) : null;
if (npm) toolchain.npm_ls = npm;
+ if (npmDirs.some((dir) => dir !== ".")) {
+ toolchain.npm_ls_dir = npmDirs[0]!;
+ const full = npmOutputs.map((stdout) => (stdout ? summarizeNpmLs(stdout, Infinity) : null));
+ // As summarizeNpmLs counts: the JSON plus a separator per dependency.
+ const shares = splitBudget(full.map((summary) => (summary ? JSON.stringify(summary).length + Object.keys(summary.dependencies).length : 0)), MAX_NPM_LS_BYTES);
+ const byDir: Record<string, NpmLs> = {};
+ npmDirs.forEach((dir, index) => {
+ const stdout = npmOutputs[index];
+ const summary = stdout ? summarizeNpmLs(stdout, shares[index]!) : null;
+ if (summary) byDir[dir] = summary;
+ });
+ if (Object.keys(byDir).length > 0) toolchain.npm_ls_by_dir = byDir;
+ }
+ if (projectsTruncated) toolchain.projects_truncated = true;
+ if (Object.keys(skipped).length > 0) toolchain.skipped = skipped;
if (system) toolchain.system = system;
if (shell) toolchain.shell = shell;
if (envFiles.length > 0) toolchain.env_files = envFiles;
@@ -535,7 +806,7 @@ const NPM_VERSION = /^[0-9A-Za-z.+-]{1,64}$/;
* 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 {
+export function summarizeNpmLs(stdout: string, maxBytes: number = MAX_NPM_LS_BYTES): NpmLs | null {
let parsed: unknown;
try {
parsed = JSON.parse(stdout);
@@ -552,7 +823,7 @@ export function summarizeNpmLs(stdout: string): NpmLs | null {
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) {
+ if (bytes + size > maxBytes) {
summary.truncated = true;
break;
}
@@ -562,54 +833,80 @@ export function summarizeNpmLs(stdout: string): NpmLs | null {
return summary;
}
-async function npmLs(
- root: string,
+/** `npm ls --json --depth=0` output in each of `dirs` (null where it failed), npm resolved and guarded once. */
+async function npmLsRaw(
+ dirs: string[],
where: (command: string) => string | null,
run: RunCommand,
timeoutMs: number,
skipped: Record<string, string>,
-): Promise<NpmLs | null> {
+): Promise<Array<string | null>> {
const npm = where("npm");
- if (!npm) return null;
+ if (!npm) return dirs.map(() => null);
const reason = await cltSkipReason("npm", npm);
if (reason) {
skipped.npm_ls = reason;
- return null;
+ return dirs.map(() => 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);
+ return Promise.all(dirs.map(async (cwd) => {
+ const result = await run(npm, ["ls", "--json", "--depth=0"], { cwd, timeoutMs, maxBytes: MAX_PROBE_OUTPUT_BYTES * 4 }).catch(() => null);
+ // npm ls exits 1 for missing or extraneous packages and still prints the tree.
+ return result && (result.code === 0 || result.code === 1) ? result.stdout : null;
+ }));
}
-async function freezeSnapshot(
- root: string,
- venvPython: string | null,
+/** `pip freeze` output of `python`, run in `cwd`: through uv when it is on PATH, else `python -m pip`. */
+async function freezeRaw(
+ python: string,
+ cwd: string,
where: (command: string) => string | null,
run: RunCommand,
timeoutMs: number,
- skipped: Record<string, string>,
-): Promise<{ lines: string[]; truncated: boolean; tool: "pip" | "uv" } | null> {
- const python = venvPython ?? where("python3") ?? where("python");
- if (!python) return null;
- // `uv pip freeze --python /usr/bin/python3` would run the shim too.
- const reason = venvPython ? null : await cltSkipReason(where("python3") === python ? "python3" : "python", python);
- if (reason) {
- skipped.pip_freeze = reason;
- return null;
- }
- const options = { cwd: root, timeoutMs, maxBytes: MAX_PROBE_OUTPUT_BYTES };
+): Promise<{ stdout: string; tool: "pip" | "uv" } | null> {
+ const options = { cwd, timeoutMs, maxBytes: MAX_PROBE_OUTPUT_BYTES };
const uv = where("uv");
// uv is much faster and also reads a uv-made venv, which has no pip.
if (uv) {
const result = await run(uv, ["pip", "freeze", "--python", python], options).catch(() => null);
- if (result && result.code === 0) return { ...sanitizeFreezeLines(result.stdout), tool: "uv" };
+ if (result && result.code === 0) return { stdout: result.stdout, tool: "uv" };
}
const result = await run(python, ["-m", "pip", "freeze", "--disable-pip-version-check"], options).catch(() => null);
- if (result && result.code === 0) return { ...sanitizeFreezeLines(result.stdout), tool: "pip" };
+ if (result && result.code === 0) return { stdout: result.stdout, tool: "pip" };
return null;
}
+/**
+ * Splits `total` between items that want `wants[i]` each: the smaller wants
+ * are met in full, the rest share what is left equally. Never more than
+ * `total` in all.
+ */
+export function splitBudget(wants: readonly number[], total: number): number[] {
+ const shares = wants.map(() => 0);
+ const order = wants.map((_, index) => index).sort((a, b) => wants[a]! - wants[b]!);
+ let left = total;
+ let count = order.length;
+ for (const index of order) {
+ const share = Math.min(wants[index]!, Math.floor(left / count));
+ shares[index] = share;
+ left -= share;
+ count -= 1;
+ }
+ return shares;
+}
+
+/** The leading `lines` that fit in `maxLines` entries and `maxBytes`. */
+function capLines(lines: readonly string[], maxLines: number, maxBytes: number): { lines: string[]; truncated: boolean } {
+ const kept: string[] = [];
+ let bytes = 0;
+ for (const line of lines) {
+ const size = Buffer.byteLength(line, "utf8") + 1;
+ if (kept.length >= maxLines || bytes + size > maxBytes) return { lines: kept, truncated: true };
+ kept.push(line);
+ bytes += size;
+ }
+ return { lines: kept, truncated: false };
+}
+
// macOS: /usr/bin/java without a JDK opens a "no Java runtime" dialog (the
// Command Line Tools shims are handled by command-guard.ts).
function javaWouldPrompt(command: string, file: string): boolean {
@@ -627,7 +924,7 @@ type CacheEntry = { signature: string; pending: Promise<UploadToolchain | null>;
/**
* One toolchain per project root, collected when a session starts (or at its
- * first upload) and again only when the root's manifest/lockfile set
+ * first upload) and again only when the project's manifest/lockfile set
* changes. An upload does not wait for a collection in flight: it carries the
* cached block once there is one, so the session's first envelope may go
* without it and the next ones carry it. `waitMs` bounds a wait: the