#!/usr/bin/env bash # # Run CI's gate on this branch, here, before anything reaches `main`. # # ./verify-branch [--base origin/main] [--scope auto|min|full] [--only step,step] # # Every command below is copied from `.forgejo/workflows/ci.yml`. That is the # whole design: the branch used to be judged by opening a pull request, which # started a 15-25 minute run on a capacity-3 runner shared with nominatim and # overpass — and the merge to `main` then started a second one from the same # tree. Running the same commands where the code already is removes the first # run entirely, and the PR with it. # # ## Why this is allowed to be the gate # # `docs/notes/agent-verification.md` says a gate may not depend on anything the # agent controls, and its corollary 1 says a check written as prompt text is # advice. Both still hold, and this is written to satisfy them: # # * **It is a script, not a sentence.** The invocations are fixed here, in the # repository, next to the workflow they mirror. A session cannot narrow # `--workspace` to the one package it was working in, because it does not # choose the argument. # * **It records what it proved.** A pass writes the head sha into # `.git/cartopolis-verified` (and the landing records it at the hub), and # the finish-branch skill refuses a head that has no receipt. Rebasing or # committing moves the sha, so a stale pass cannot be spent on different # code. # * **CI still runs**, on the push to `main` (`ci.yml` has `push: branches: # [main]`), and on `main` it runs *more* than a pull request ever did — the # smokes and both cross-target lanes. What changed is that its verdict # arrives after the landing instead of before it, and a red one is fixed # forward. That is a real weakening and is stated in workflow.md rather than # papered over. # # What this cannot reproduce is CI's *environment*: a clean checkout, a cold # cache, a different rustfmt. The first two do not decide any of these steps and # the third is byte-identical to the runner's (measured, ci-does-not-gate-merges). # What it genuinely cannot see is anything needing a GPU — colour, exposure, # bloom, lighting and device loss are not gateable here or on the runner, for the # same reason, and `docs/notes/headless-shots-software-renderer.md` is unchanged. # # ## Scope # # `auto` reads the diff against the base and mirrors ci.yml's own deny-list: a # commit touching nothing outside `docs/`, `crates/simulator/`, `.forgejo/`, # `.claude/`, `*.md` and the workspace members the client does not link cannot # change a frame, so it skips the two render smokes and the cross-target lanes. # The web UI (`crates/atlas/web`) gets its own `web` step and compiles no Rust. # Everything skipped is *named on stdout*, because a gate that quietly narrows # itself reads as a pass over ground nobody covered. `fmt` and `web` build # nothing, so they run before a node is taken. set -uo pipefail base="origin/main" scope="auto" only="" while [ $# -gt 0 ]; do case "$1" in --base) base="$2"; shift 2 ;; --scope) scope="$2"; shift 2 ;; --only) only="$2"; shift 2 ;; -h|--help) sed -n '2,53p' "$0"; exit 0 ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac done root=$(git rev-parse --show-toplevel) || exit 1 cd "$root" || exit 1 head=$(git rev-parse HEAD) # ── Environment ────────────────────────────────────────────────────────────── # The container has no ALSA and no in-container sudo; the debs are extracted # into a user prefix instead (docs/notes/container-audio.md). `bevy_audio` is a # default feature, so without this every cartopolis build dies in `alsa-sys`' # build script with a pkg-config error that reads as a missing crate. Harmless # where the prefix does not exist — a workstation has the real package. alsa="$HOME/opt/alsa/usr/lib/x86_64-linux-gnu" if [ -f "$alsa/pkgconfig/alsa.pc" ]; then export PKG_CONFIG_PATH="$alsa/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" export LD_LIBRARY_PATH="$alsa${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" fi # Mesa lavapipe, exactly as the CI image is configured. Wayland + GLES flickers # and there is no compositor here anyway. A Mac node has Metal and no Vulkan. case "$(uname -s)" in Darwin) export WGPU_BACKEND="${WGPU_BACKEND:-metal}" ;; *) export WGPU_BACKEND="${WGPU_BACKEND:-vulkan}" ;; esac # The smokes are bounded by `timeout`, which macOS does not ship. if command -v timeout >/dev/null; then bounded="timeout 900" elif command -v gtimeout >/dev/null; then bounded="gtimeout 900" else bounded="perl -e 'alarm shift; exec @ARGV' 900" fi # The smokes run the client built with the workspace's features, which the # tests step resolved already, not `-p cartopolis`'s: alone the client takes # other features of shared crates (rand, regex, libc, serde_core …), so every # workspace crate above an edit compiled a second time. After an edit in # cartopolis_core on mac-1 (2026-11-27), `cargo build -p cartopolis` behind the # tests compiled 6 units in 15 s, this 1 in 2 s. The binary that ships is built # by its deploy, with its own flags; the suite already runs on this feature set. client_build="cargo build --locked --workspace --exclude cartopolis_android --bin cartopolis" client="${CARGO_TARGET_DIR:-$root/target}/debug/cartopolis" export CARGO_TERM_COLOR="${CARGO_TERM_COLOR:-always}" tiles="${CARTO_TILE_URL:-https://tiles.cartopolis.org/tiles/osm/{z}/{x}/{y}}" # ── What is worth running for this branch ──────────────────────────────────── git rev-parse --verify -q "$base" >/dev/null || { echo "base $base does not resolve — run: git fetch origin main" >&2; exit 2; } mb=$(git merge-base HEAD "$base") changed=$(git diff --name-only "$mb" HEAD) # Three filters. The first two are ci.yml's, and they are not the same one. # # * `paths-ignore` on the triggers decides whether a run happens **at all**: # `docs/`, `*.md`, `.claude/`. A commit inside that set starts no CI run on # `main` either, so compiling the workspace to prove prose is a build this # branch would never have paid for. The web UI's own tree, # `crates/atlas/web/`, is added here: it is TypeScript with a step of its own # (`web`), and no Rust reads it. # * the `What is worth running` step's deny-list adds `crates/simulator/`, # `tools/` and `.forgejo/` and decides only whether the *render smokes* run # — the simulator holds no map data and the tools are Python, TypeScript and # bash, so neither can change a frame, but both very much have a suite. # * and a workspace member the client does not link — `cartopolis` and # `cartopolis_android`'s normal and build dependencies, followed through # every path dependency, read from `cargo metadata --no-deps` (no resolve, # a tenth of a second, works offline) — cannot change a frame or the wasm # and Android builds either: today atlas, the viewer, web_chat and the # simulator. A member under `[patch]` (`web_task`) counts as linked, and # the vendored trees that are not members (`crates/wgpu`, …) are # unrecognised paths. 2026-10-31: 64 of 572 commits on main since 10-01 # touched `crates/atlas/web`, and each paid for both smokes and both # cross-target checks. # # All three are deny-lists, so an unrecognised path (`assets/`, `crates/geo/`, # `Cargo.lock`, anything new) runs everything, and so does a `cargo metadata` # that fails. Fail safe, not silent. unlinked=$(cargo metadata --offline --no-deps --format-version 1 2>/dev/null | jq -r --arg root "$root/" ' (.packages | map({key: .name, value: { dir: (.manifest_path | sub("/Cargo.toml$"; "") | ltrimstr($root)), deps: [.dependencies[] | select(.path != null and .kind != "dev") | .name]}}) | from_entries) as $p | def close($todo; $seen): if ($todo | length) == 0 then $seen elif ($seen | index($todo[0])) then close($todo[1:]; $seen) else close($todo[1:] + ($p[$todo[0]].deps // []); $seen + [$todo[0]]) end; close(["cartopolis", "cartopolis_android"]; []) as $linked | if ($linked | length) < 3 then empty else $p | to_entries[] | select(.key as $k | $linked | index($k) | not) | .value.dir end' 2>/dev/null) patched=$(sed -n '/^\[patch/,/^\[/s/.*path *= *"\([^"]*\)".*/\1/p' Cargo.toml) unlinked=$(printf '%s\n' "$unlinked" | grep . | grep -vxF "$(printf '%s\n' "$patched" | grep . || echo /)") unlinked_re=$(printf '%s\n' "$unlinked" | grep . | sed 's|[.]|\\.|g; s|$|/|' | paste -sd'|' -) compile=true render=true targets=true web=false if [ "$scope" = "full" ]; then web=true elif [ "$scope" = "min" ]; then compile=false; render=false; targets=false elif [ -n "$changed" ]; then # The call files the page imports live in the client's tree and are tested # by the web suite (deploy/build/web.sh). printf '%s\n' "$changed" | grep -qE '^crates/(atlas/web|cartopolis/web)/' && web=true if ! printf '%s\n' "$changed" | grep -qvE '^(docs/|\.claude/|crates/atlas/web/|[^/]*\.md$)'; then compile=false; render=false; targets=false elif ! printf '%s\n' "$changed" \ | grep -qvE "^(docs/|crates/simulator/|tools/|\.forgejo/|\.claude/|[^/]*\.md\$${unlinked_re:+|$unlinked_re})"; then render=false; targets=false fi fi echo "branch $(git rev-parse --abbrev-ref HEAD)" echo "head $head" echo "base $base ($(git rev-parse --short "$mb"))" echo "changed $(printf '%s\n' "$changed" | grep -c .) file(s)" if ! $compile; then echo "nothing outside the docs, the notes, .claude and the web UI changed —" echo "ci.yml's own paths-ignore means no Rust run would start on main for this" echo "either, so there is nothing here to compile. Formatting still runs." else $render || echo "skipping the render smokes: nothing the client links changed (${unlinked//$'\n'/, } are not linked)" $targets || echo "skipping the wasm and android target checks: same reason" fi # The cheap steps first, the smokes last: an Android-only error (dead code # only that cfg sees) is on screen a minute in rather than after five minutes # of smoke build. The total is the same when everything passes. steps="fmt" $web && steps="$steps web" $compile && steps="$steps tests" $targets && steps="$steps wasm android" $render && steps="$steps smoke-orbit smoke-phone" [ -n "$only" ] && steps=$(printf '%s' "$only" | tr ',' ' ') failed="" passed="" skipped="" run() { # run ; command comes from stdin local name="$1" desc="$2" t0 t1 printf '\n\033[1m==> %s\033[0m — %s\n' "$name" "$desc" t0=$(date +%s) if bash -eo pipefail; then t1=$(date +%s) printf ' \033[32mok\033[0m %ss\n' "$((t1 - t0))" passed="$passed $name" else t1=$(date +%s) printf ' \033[31mFAILED\033[0m %ss\n' "$((t1 - t0))" failed="$failed $name" fi } want() { printf '%s\n' $steps | grep -qx "$1"; } # ── The steps that need no build ───────────────────────────────────────────── # Order differs from ci.yml on purpose. There, formatting runs *last* because a # failing step ends the job and a whitespace error would hide a real one (two # months of Android releases were lost that way). Here nothing is hidden: every # step runs and the failures are summarised together, so the cheap ones go # first and a rustfmt slip is on screen two seconds in rather than ten minutes. # # These two also run *before* a node is taken, wherever the gate was started: # a red one there ends the gate without holding a node, where it used to cost # the whole run (rustfmt slips reached the gate in 8 of the 61 sessions of # 26-31 October, and main took 11 formatting fixes that month). cheap() { if want fmt; then run fmt "cargo fmt --check over every owned crate, and the notes' triggers" <<'CMD' # A Mac node's toolchain ships without it. command -v rustfmt >/dev/null && rustfmt --version >/dev/null 2>&1 \ || rustup component add rustfmt # Every member but the two vendored ones (big_space is a fork, web-task a # patched copy): five crates were checked, and the rest drifted unseen. cargo fmt --check $(cargo metadata --offline --no-deps --format-version 1 \ | jq -r '.packages[].name | select(. != "big_space" and . != "web-task") | "-p", .') # A trigger naming a renamed file silently stops a note reaching anyone. bun tools/notes-lint.ts --quiet CMD fi if want web; then # deploy/build/web.sh's checks without the chat worker, whose types are # generated by a wasm-bindgen build (`build:chat-core`) the deploy runs. run web "the web UI: typecheck and tests (crates/atlas/web)" <<'CMD' cd crates/atlas/web bun install --frozen-lockfile bun x tsc --noEmit bun test bun test ../../cartopolis/web CMD fi } # ── A build node of its own ────────────────────────────────────────────────── # **The gate runs on a build node, never in the agent container.** Builds there # starve the box (every session at once on 8 cores) and the container's own # cargo refuses them. A midas node (jeroen/midas) is a contained # checkout that follows this worktree over ssh — each ssh to it first sends the # commits and uncommitted edits it lacks. So the gate takes a node as # `gate-` (the flush accepts that lease for this worktree), runs this # same script there with the merge-base sha as its base, and brings back the # exit status and the receipt (checked against this head, and marked with the # node). A VPS node's image carries what the steps need: lavapipe, nightly's wasm32 # target, the Android target and the NDK; a Mac renders on Metal and fetches the # NDK and the targets on first use. Measured 2026-10-11 on vps-2 from a # cold build directory: all six steps in 49 min. # # On the node itself (CARTO_GATE_NO_SLOT=1, set by the run that sent it there), # or on a machine with no midas (a workstation), the steps run in place. So do # fmt and web alone: they build nothing. needs_build=$(printf '%s\n' $steps | grep -vxE 'fmt|web' | head -1) if [ -n "$needs_build" ] && [ -z "${CARTO_GATE_NO_SLOT:-}" ] && command -v midas >/dev/null; then # The build-free steps here first; a red one ends the gate before it holds # a node. The node runs them again (seconds), so its receipt names them. cheap if [ -n "$failed" ]; then echo echo "FAILED $(echo $failed) — not taking a node for the rest." [ "$failed" = " fmt" ] && echo "Formatting: cargo fmt (and bun tools/notes-lint.ts for the notes), then re-run." rm -f "$(git rev-parse --git-dir)/cartopolis-verified" exit 1 fi failed=""; passed="" # **The node this worktree already holds, if it holds one** — a VPS node or a # Mac. Taking a second lease for the gate meant releasing the node the work # was done on and then queueing for a VPS node another session held, with the # Mac idle (2026-10-16: twice in one landing). The steps below run on Linux # and macOS alike; the node's checkout follows this worktree either way. gate_node=$(midas mine 2>/dev/null) if [ -n "$gate_node" ]; then echo "node $gate_node — this worktree holds it; running the gate there" else # **The fastest free node, not the first VPS one.** Taking only `vps` # put every gate in line for one node, 8-28 minutes each, while two # Macs that gate in half the time sat free (week to 2026-10-17). The # hub ranks the free nodes by this script's own measured run time, by # node or by machine, and puts a GPU node last (`midas take # --fastest-for`). A midas too old to rank still gets a VPS node. gate_owner="gate-$(basename "$root")" gate_purpose="landing gate for $(git rev-parse --abbrev-ref HEAD)" if midas take --help 2>/dev/null | grep -q -- --fastest-for; then gate_node=$(midas take any --fastest-for tools/verify-branch \ --owner "$gate_owner" --wait 3600 --purpose "$gate_purpose") else gate_node=$(midas take vps --owner "$gate_owner" --wait 3600 --purpose "$gate_purpose") fi || { echo "no node came free in an hour; not gating" >&2; exit 3; } trap 'midas release --owner "$gate_owner" >/dev/null 2>&1' EXIT echo "node $gate_node — running the gate there" fi ssh -o BatchMode=yes "$gate_node" \ "CARTO_GATE_NO_SLOT=1 tools/verify-branch --base $mb --scope $scope${only:+ --only $only}" status=$? remote=$(ssh -o BatchMode=yes "$gate_node" 'cat "$(git rev-parse --git-dir)/cartopolis-verified" 2>/dev/null') receipt="$(git rev-parse --git-dir)/cartopolis-verified" if [ "$status" -eq 0 ] && printf '%s\n' "$remote" | grep -qx "sha=$head"; then printf '%s\n' "$remote" | sed "s|^base=.*|base=$(git rev-parse "$mb")|" > "$receipt" printf '%s\n' "node=$gate_node" >> "$receipt" # And at the hub, which is what `midas deploy` asks before it publishes # this commit. Best effort: a hub that cannot be told does not make the # gate red — the deploy will say the receipt is missing instead. midas verified --sha "$head" --scope "$scope" --node "$gate_node" \ --steps "$(printf '%s\n' "$remote" | sed -n 's/^steps=//p')" >/dev/null 2>&1 \ || echo "note: the hub did not take the receipt; \`midas deploy\` will ask for --unverified" >&2 else rm -f "$receipt" [ "$status" -eq 0 ] && { echo "the node passed, but its receipt is not for ${head:0:8}" >&2; status=1; } fi exit "$status" fi cheap if want tests; then # One invocation, not five: with resolver v2 each `cargo test -p` unifies # features over only its own packages, so five invocations mean five rebuilds # of the shared Bevy stack. ci.yml's own comment measures it at a third of # the job. `--locked` because a Cargo.toml edit arriving without its lockfile # update must fail rather than silently re-resolve. # The script host's own suite as well: the sandbox, pausing and memory # ceilings prim scripts rely on are tested in Bun, not in Rust # (docs/notes/javascript-prim-scripts.md). It needs Bun 1.4, which the # nodes have and the container's 1.3 is not. # And the crate graph against CLAUDE.md's "no bevy" / "never links" rules # (tools/dep-rules.ts). Here rather than in `cheap`: it resolves the graph, # which needs every crate in the registry cache, and only a node has them. run tests "cargo test --locked --workspace --exclude cartopolis_android, the script host's bun test, and the crate graph's rules" <<'CMD' bun tools/dep-rules.ts --quiet cargo test --locked --workspace --exclude cartopolis_android cd tools/script-host bun install --frozen-lockfile bun test CMD fi if want wasm; then # `--features audio` is not optional: dropping the defaults also drops # bevy_audio, and systems::voice's playback half becomes dead code that # `unused = "deny"` turns into nine errors in files you did not touch. # Keep in step with ci.yml's targets lane and index.html's data-cargo-features # (`ui-dom`: the component library's screens as DOM, part of the web build). run wasm "cargo check for wasm32 (the web client's cfg dimension)" <<'CMD' # Probe the toolchain the check below actually uses. This asked *stable* whether # it had wasm32 and then built with nightly, which is invisible wherever both # happen to carry the target and fatal where only nightly does: in this container # stable has neither the target nor a writable RUSTUP_HOME to add it to, so the # step died in `rustup target add` — "component download failed" — with the # nightly toolchain that could have run it sitting right there. export RUSTUP_TOOLCHAIN=nightly rustup target list --installed 2>/dev/null | grep -qx wasm32-unknown-unknown \ || rustup target add wasm32-unknown-unknown RUSTUP_TOOLCHAIN=nightly \ RUSTFLAGS="--cfg=web_sys_unstable_apis -C target-feature=+atomics,+bulk-memory,+mutable-globals,+simd128" \ cargo check --locked -p cartopolis --no-default-features --features audio,ui-dom \ --target wasm32-unknown-unknown CMD fi if want android; then # The only thing anywhere that compiles cartopolis_android's real code — the # crate's dependencies are target-gated, so a host build proves nothing about # it. Needs the NDK's clang because blake3's build script runs `cc` at check # time. In this container the toolchain copy and the NDK are under $HOME # (the shared RUSTUP_HOME is root-owned); on a Linux node the NDK is /opt, # on a Mac node it is fetched once into the node's tools directory. run android "cargo check for aarch64-linux-android" <<'CMD' # `&&` on its own line would be the last command of a list under `bash -e`, # so a missing first candidate would end the step rather than try the second. # Otherwise the deploy builds' own: /opt/android-ndk, or the copy a Mac node # fetches into its tools directory (`deploy/build/lib.sh`'s android_ndk). if [ -d "$HOME/android-ndk" ]; then ndk="$HOME/android-ndk" else . deploy/build/lib.sh android_ndk ndk="$ANDROID_NDK_HOME" fi # linux-x86_64, or darwin-x86_64 on a Mac: whichever host the NDK was built for. NDK_BIN="" for d in "$ndk"/toolchains/llvm/prebuilt/*/bin; do if [ -d "$d" ]; then NDK_BIN="$d"; break; fi done [ -n "$NDK_BIN" ] || { echo "no NDK toolchain under $ndk"; exit 1; } if [ -d "$HOME/rustup" ]; then export RUSTUP_HOME="$HOME/rustup"; fi rustup target list --installed 2>/dev/null | grep -qx aarch64-linux-android \ || rustup target add aarch64-linux-android export CC_aarch64_linux_android="$NDK_BIN/aarch64-linux-android28-clang" export AR_aarch64_linux_android="$NDK_BIN/llvm-ar" cargo check --locked -p cartopolis_android --target aarch64-linux-android CMD fi # The smokes launch the client. If the workspace did not build there is nothing # to launch, and each of these costs up to 15 minutes discovering that. if printf '%s' "$failed" | grep -qw tests; then for s in smoke-orbit smoke-phone; do want "$s" && { skipped="$skipped $s"; } done want smoke-orbit && echo " skipping the render smokes: the suite failed, so there is no binary to launch" steps=$(printf '%s\n' $steps | grep -vE '^smoke-' | tr '\n' ' ') fi if want smoke-orbit; then run smoke-orbit "headless orbit capture: a planet is on screen" <0' \\ --expect 'draws<=200' --expect 'upload_peak_asset_kb<=8192' || { echo "the metrics behind it:"; cat /tmp/verify_orbit.json 2>/dev/null \\ || echo "(no dump: the run died before it captured)"; exit 1; } CMD fi if want smoke-phone; then # Not a performance variant of the orbit shot: the phone tier takes a # different sky, and that absent `AtmosphereSettings` is what once deleted # the planet on the handset while the tier above kept passing. run smoke-phone "the same frame on the tier Android runs" <0' \\ --expect 'upload_peak_asset_kb<=8192' || { echo "the metrics behind it:"; cat /tmp/verify_phone.json 2>/dev/null \\ || echo "(no dump: the run died before it captured)"; exit 1; } CMD fi # ── Verdict ────────────────────────────────────────────────────────────────── echo echo "──────────────────────────────────────────────" [ -n "$passed" ] && echo "passed $(echo $passed)" [ -n "$skipped" ] && echo "skipped $(echo $skipped)" if [ -n "$failed" ]; then echo "FAILED $(echo $failed)" echo echo "Fix and re-run — one step at a time with --only $(echo $failed | tr ' ' ',')." rm -f "$(git rev-parse --git-dir)/cartopolis-verified" exit 1 fi # The receipt. Keyed on the head sha, so a commit or a rebase after this point # invalidates it and a landing asks for the gate again. receipt="$(git rev-parse --git-dir)/cartopolis-verified" { echo "sha=$head" echo "at=$(date -u +%Y-%m-%dT%H:%M:%SZ)" echo "scope=$scope" echo "steps=$(echo $passed)" echo "base=$(git rev-parse "$mb")" } > "$receipt" echo "green — receipt written for ${head:0:8}" # And at the hub, when the gate ran here rather than on a node (whose caller # records it, with the node's name): a scope that compiles nothing (skills, # scripts, prose) runs in place, and its landings carried no receipt at the # hub — so `midas deploy` refused main after a docs-only landing on top of a # verified one (99bf1cd8, 2026-10-16). Best effort, as on the node path. if [ -z "${CARTO_GATE_NO_SLOT:-}" ] && command -v midas >/dev/null; then midas verified --sha "$head" --scope "$scope" --node "in place" --steps "$(echo $passed)" >/dev/null 2>&1 \ || echo "note: the hub did not take the receipt; \`midas deploy\` will ask for --unverified" >&2 fi