diff --git a/docs/studio/development.md b/docs/studio/development.md index 60428002..90b59b61 100644 --- a/docs/studio/development.md +++ b/docs/studio/development.md @@ -61,7 +61,7 @@ A review trial (#502) compared this review with `gemini-4-neon-high` and with me `npm run check` takes a check slot (below), then runs `npm run check:steps`, which runs them all in order: `cargo fmt --all --check`, `cargo machete` (unused Rust dependencies), `npm run lint`, `npm run build`, `knip --no-progress` (unused exports and files, `knip.jsonc`), `cargo clippy --locked --workspace --all-targets -- -D warnings`, `rm -f scripts/wire/*.ts`, `cargo nextest run --locked --workspace` (the shell included), the map check (`cargo run --locked -q -p studio-system-map -- check`), `tsc -p scripts/wire` on the samples the wire tests just wrote, among them each command's argument names as `server/src/api.rs` reads them (`server-args.ts`, checked against `Commands` in `ui/types.ts`, #554), and `npm run test:node`, which builds again and type-checks the tests before it runs them. nextest runs the same tests as `cargo test`, but runs every test in a process of its own, all binaries' at once; install it with `brew install cargo-nextest` on macOS (on Linux, `cargo install --locked cargo-nextest`), and cargo machete with `cargo install --locked cargo-machete`. Clippy's lints are the `[workspace.lints]` table of the root `Cargo.toml`, which every crate takes; an allow names its lint and says its `reason`. CI runs `npm ci` and `npm run check` in two jobs in `.github/workflows/studio.yml`, `macos` and `ubuntu` (#699: the same steps on `ubuntu-latest`, not required until the suite passes there, spec #510; research in [cross-platform](research/cross-platform.md)), and `npm run build` and `npm run snap:all -- --shard=/2` in two shard jobs in `.github/workflows/snap.yml` (#432), on macOS (the snap jobs stay there). Both run for each push to `main` and for each pull request that is not a draft: a draft runs nothing until it is marked ready, then each push to it runs. A change to Markdown files alone, or to `archive/` alone, runs no check job; snap runs only when `ui/`, `scripts/snap/`, `package.json`, `package-lock.json`, `tsconfig.json` or `snap.yml` changes, as it reads nothing else (the server is stubbed). The engine's `checks.yml` and `ci-checks` skip the studio. -At most two `npm run check` run on a machine at once, across every worktree and clone (#783, spec #778): four at once kept a 10-core Mac at load 15-35, half of it in the kernel. `scripts/check/slot.mjs` takes one of two slots, files `1` and `2` in `check-slots/` beside the runs folder (`~/Library/Caches/plotkeeper/check-slots/` on macOS; on Linux `$XDG_CACHE_HOME/plotkeeper/check-slots/` when it holds an absolute path, else `~/.cache/plotkeeper/check-slots/`), each holding its check's pid, worktree and start time; `cat` one to see who holds it. It then runs `npm run check:steps` in a process group of its own and passes on its output, its exit code and a stop (a Ctrl+C, a `SIGTERM` or a closed terminal's `SIGHUP`), and gives the slot back when it ends. A third check prints `check: waiting for a check slot, held by () and ()` once and starts when a slot frees, polling once a second; stopped while it waits, it exits without running anything. A slot whose pid is gone, or which its holder has not touched for 60 s (a holder touches its slot every 10 s), is taken back, so a killed check never blocks the rest. A check whose slot folder can't be made or written, as in a sandboxed background run, runs without a slot and says so in one line. The wait counts against a foreground command's timeout, such as Claude Code's 10-minute Bash limit: with both slots busy, run the check as a background task. `npm run check:steps` runs the same steps without a slot. `ui/tests/check-slot.test.mjs` drives the wrapper with its folder and limits set through `CHECK_SLOT_DIR` and the `CHECK_SLOT_*_MS` variables, which are for the tests, not a setting. +At most two `npm run check` run on a machine at once, across every worktree and clone (#783, spec #778): four at once kept a 10-core Mac at load 15-35, half of it in the kernel. `scripts/check/slot.mjs` takes one of two slots, files `1` and `2` in `check-slots/` beside the runs folder (`~/Library/Caches/plotkeeper/check-slots/` on macOS; on Linux `$XDG_CACHE_HOME/plotkeeper/check-slots/` when it holds an absolute path, else `~/.cache/plotkeeper/check-slots/`), each holding its check's pid, worktree and start time; `cat` one to see who holds it. It then runs `npm run check:steps` in a process group of its own and passes on its output, its exit code and a stop (a Ctrl+C, a `SIGTERM` or a closed terminal's `SIGHUP`), and gives the slot back when it ends. A third check prints `check: waiting for a check slot, held by () and ()` once and starts when a slot frees, polling once a second; stopped while it waits, it exits without running anything. A slot whose pid is gone, or which its holder has not touched for 60 s (a holder touches its slot every 10 s), is taken back, so a killed check never blocks the rest. A check whose slot folder can't be made or written, as in a sandboxed background run, runs without a slot and says so in one line. The wait counts against a foreground command's timeout, such as Claude Code's 10-minute Bash limit: with both slots busy, run the check as a background task. On macOS, when the first `git` on `PATH` is `/usr/bin/git`, a stub that runs `xcrun` on every call, the wrapper puts the folder of the `git` it forwards to (`xcrun -f git`) right before `/usr/bin` on the steps' `PATH`, so the folders before it keep their place. The tests make about 18,000 `git` calls, 78% of the test run's CPU, and the stub more than doubles a short call's cost (#803). Any other first `git`, such as Homebrew's or a test's fake, stays first. `npm run check:steps` runs the same steps without a slot or that `PATH`. `ui/tests/check-slot.test.mjs` drives the wrapper with its folder and limits set through `CHECK_SLOT_DIR` and the `CHECK_SLOT_*_MS` variables, which are for the tests, not a setting. ## The npm package diff --git a/scripts/check/slot.mjs b/scripts/check/slot.mjs index ffe1d03a..df510a33 100644 --- a/scripts/check/slot.mjs +++ b/scripts/check/slot.mjs @@ -7,12 +7,15 @@ // check writes its pid, worktree and start time to a temporary file there and hard-links it to a slot's name: the link // fails when the name exists, so exactly one check wins a slot, and a slot file is never seen half written. // +// On macOS it also puts the real `git` ahead of `/usr/bin/git` on the command's PATH when that stub, which runs `xcrun` on +// every call, would be found first (#803, `gitPath` below). +// // The tests set the folder and the timing limits through `CHECK_SLOT_DIR`, `CHECK_SLOT_POLL_MS`, `CHECK_SLOT_TOUCH_MS` // and `CHECK_SLOT_STALE_MS`; they are not a setting. -import { spawn } from "node:child_process"; +import { execFileSync, spawn } from "node:child_process"; import { closeSync, fstatSync, ftruncateSync, futimesSync, linkSync, mkdirSync, openSync, readFileSync, rmdirSync, statSync, unlinkSync, writeSync } from "node:fs"; import { constants, homedir } from "node:os"; -import { join } from "node:path"; +import { delimiter, dirname, join } from "node:path"; const SLOTS = ["1", "2"]; const env = process.env; @@ -217,12 +220,38 @@ const hold = async () => { } }; +/** + * The command's PATH. On macOS `/usr/bin/git` is a stub that asks `xcrun` for Xcode's or the Command Line Tools' `git` on + * every call, which more than doubles the cost of a short call; a check's tests make about 18,000 `git` calls, which were + * 78% of its test run's CPU (#803). When the stub is the first `git` on PATH, the folder of the `git` it forwards to goes + * right before `/usr/bin`. Every folder before `/usr/bin` keeps its place, and the folder's other tools pass over only + * `/usr/bin`'s stubs, which run those same tools. Any other first `git`, such as Homebrew's or a test's fake, is left first. + */ +const gitPath = (path = "") => { + if (process.platform !== "darwin") return path; + const folders = path.split(delimiter); + const first = folders.findIndex((folder) => { + try { + return statSync(join(folder, "git")).isFile(); + } catch { + return false; + } + }); + if (folders[first] !== "/usr/bin") return path; + try { + const real = execFileSync("xcrun", ["-f", "git"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 10_000 }).trim(); + return real ? [...folders.slice(0, first), dirname(real), ...folders.slice(first)].join(delimiter) : path; + } catch { + return path; + } +}; + const run = (mine) => { // Through the open file, so a slot that was taken back and is now another check's file is never touched. if (mine) setInterval(() => futimesSync(mine.fd, new Date(), new Date()), TOUCH_MS).unref(); // No standard input: in a terminal the child's group is not the foreground one, and a read of the terminal from it, such // as nextest's key handler, would stop it with SIGTTIN. - child = spawn(command, args, { stdio: ["ignore", "inherit", "inherit"], detached: true }); + child = spawn(command, args, { stdio: ["ignore", "inherit", "inherit"], detached: true, env: { ...env, PATH: gitPath(env.PATH) } }); child.on("close", (code, signal) => { if (mine) release(mine); process.exit(code ?? statusOf(signal)); diff --git a/ui/tests/check-slot.test.mjs b/ui/tests/check-slot.test.mjs index 2a243faa..eca7b236 100644 --- a/ui/tests/check-slot.test.mjs +++ b/ui/tests/check-slot.test.mjs @@ -210,3 +210,35 @@ for (const { kind, code, make } of UNWRITABLE) { assert.equal(ran.stderr, `check: the check slot folder ${slots} can't be written (${code}), so this check runs without a slot\n`); }); } + +// The `git` the wrapped command finds first on PATH (#803). On macOS `/usr/bin/git` is a stub that runs `xcrun` on every +// call, so the wrapper puts the `git` it forwards to right before `/usr/bin`; any other first `git`, such as a test's fake, +// stays first. The command prints its `git` and its PATH's first folder, which must keep its place. +const xcodeGit = process.platform === "darwin" ? spawnSync("xcrun", ["-f", "git"], { encoding: "utf8" }).stdout.trim() : ""; +const GIT_ON_PATH = [ + { + kind: "the macOS stub `/usr/bin/git`", + skip: !xcodeGit && "no xcrun git here", + path: (dir) => `${dir}:/usr/bin:/bin`, + want: (dir) => `${xcodeGit} ${dir}`, + }, + { + kind: "a fake `git` before the stub", + path: (dir) => { + mkdirSync(join(dir, "bin")); + writeFileSync(join(dir, "bin/git"), "#!/bin/sh\n", { mode: 0o755 }); + return `${join(dir, "bin")}:/usr/bin:/bin`; + }, + want: (dir) => `${join(dir, "bin/git")} ${join(dir, "bin")}`, + }, +]; + +for (const { kind, skip, path, want } of GIT_ON_PATH) { + test(`the wrapped command's git, with ${kind} first on PATH`, { skip }, () => { + const dir = scratch(); + const env = { ...process.env, PATH: path(dir), CHECK_SLOT_DIR: join(dir, "slots") }; + const ran = spawnSync(process.execPath, [script, "/bin/sh", "-c", 'git=$(command -v git); IFS=:; set -- $PATH; echo "$git $1"'], { cwd: dir, env, encoding: "utf8" }); + assert.equal(ran.status, 0, ran.stderr); + assert.equal(ran.stdout.trim(), want(dir)); + }); +}