#!/usr/bin/env bash# ON A MAC, TAKE THE MACHINE'S BUILD MUTEX. ON LINUX, JUST RUN THE COMMAND.## scripts/ci/with-machine-lock.sh <command...>## WHY IT EXISTS. Since `rust` became a `cotton-rust` job it can land on the# Linux CI host or on a Mac, and the two want opposite things.## - A Mac is not a dedicated CI host. It runs a desktop, Xcode, agent# sessions, and this workflow's own `swift` and `ios` jobs — all of which# queue behind /tmp/tinmen-build.lock (`scripts/with-build-lock.sh`),# because two heavy builds at once swap a 16 GB machine to death. A cold# cargo build of this workspace is that kind of build, so it belongs in the# same queue.# - The Linux host's whole point is that four Cotton jobs run at once# (the procedure “Reach and maintain the CI host”). A machine-wide mutex there would put the `rust`# jobs of different PRs back in the single file #938 got them out of.## WHY IT IS ITS OWN SCRIPT AND NOT A LINE INSIDE `with-cargo-lock.sh`, which is# the other thing every cargo step already goes through. That script is called# by the SCRIPT TESTS (`cargo-cache.test.ts` and friends); teaching it to take a# machine-wide lock made `pnpm test:scripts` hang on this Mac the moment another# worktree was building — measured, not imagined. A CI wrapper belongs where CI# calls it and nowhere else.## The branch is on `uname`, deliberately, rather than on a CI variable: it is a# fact about the machine, not about the lane. `COTTON_MACHINE_LOCK_OS` overrides# it for the tests.## AND ON BOTH, A CHECK JOB OUTRANKS A BUILDER'S LOCAL RUN. On a Mac that is the# build lock's own business (`with-build-lock.sh`, "CHECK JOBS GO FIRST"). On# Linux there is no lock to rank, so the rule is the scheduler's: on 28 Sep 2026# the CI host read load 49 on 16 cores from builders' local test runs — each# `cargo test` at one test thread per core, at nice 0, beside the check jobs the# host exists for — and `rust-job-sizing.sh` then sized CI's own jobs DOWN to# fit. So a LOCAL caller (`scripts/lib/caller-class.sh`) runs:## - at nice 19, and on Linux at the lowest best-effort I/O priority — the# check jobs take the cores and the disk first, and the local run gets what# is left instead of half of everything;# - with its parallelism capped at 4 (`CARGO_BUILD_JOBS`, `RUST_TEST_THREADS`,# `NEXTEST_TEST_THREADS`), unless the caller set its own — a verify does not# need sixteen threads to answer, and a host shared by several builders# cannot give each of them sixteen.## A check job runs exactly as before. `BUILD_LOCK_LOCAL_NICE=0` turns the nice# off (as it does inside the lock).# Guard: `scripts/__tests__/with-machine-lock.test.ts`.set -uo pipefailif [ $# -lt 1 ]; then echo "usage: with-machine-lock.sh <command...>" >&2exit2firoot="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"# shellcheck source=../lib/caller-class.sh. "$root/scripts/lib/caller-class.sh" || { echo "[machine-lock] scripts/lib/caller-class.sh is missing" >&2exit2}class="$(caller_class)"os="${COTTON_MACHINE_LOCK_OS:-$(uname -s)}"if [ "$os" != "Darwin" ]; thenif [ "$class" != local ]; then exec "$@"fiexport CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-4}"export RUST_TEST_THREADS="${RUST_TEST_THREADS:-4}"export NEXTEST_TEST_THREADS="${NEXTEST_TEST_THREADS:-4}" local_nice="${BUILD_LOCK_LOCAL_NICE:-19}" [[ "$local_nice" =~ ^[0-9]+$ ]] || local_nice=19 prefix=()# The LEVEL, not an increment: `nice -n` clamps at 19 on Linux but 20 on# macOS, so a niced caller overshot to 20 there (run 5510). current_nice="$(python3 "$root/scripts/lib/mac_disk_policy.py" --process-info nice "$$")" || exit2 [[ "$current_nice" =~ ^-?[0-9]+$ ]] || current_nice=0 [ "$local_nice" -gt 0 ] && [ $((local_nice - current_nice)) -gt 0 ] && prefix+=(nice -n "$((local_nice - current_nice))")# util-linux's ionice; absent (macOS under COTTON_MACHINE_LOCK_OS, a minimal# image) it is simply not used — the CPU half of the rule still holds. command -v ionice >/dev/null 2>&1 && prefix+=(ionice -c 2 -n 7) echo "[machine-lock] local run — nice ${local_nice}, ${CARGO_BUILD_JOBS} build jobs, ${NEXTEST_TEST_THREADS} test threads; check jobs go first" >&2 exec ${prefix[@]+"${prefix[@]}"} "$@"fi# `with-build-lock.sh` is re-entrant — it walks its own parent chain — so a# caller that already holds the lock runs straight through, and the cargo steps# of one job never queue behind each other.exec env BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-ci-rust-mac}" \ bash "$root/scripts/with-build-lock.sh""$@"