interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

tools/aien-local-ci: one crumb tool for author, readiness and gate; crumb-doctor drift check

merged 0ce88f93d2694ef0 #675 · opened 2026-10-19 00:19 UTC · closed 2026-10-19 12:59 UTC · merged 2026-10-19 12:59 UTC

Imported from GitHub aien-dev/aien-sovereign-core-mv#675, by drakestapleton

Labels: ready-to-merge

Base a75b7c25e5fd3454af99d97bb6993a2e3481d733 · Head 84f0aba0847993b9397dca471451060f21435a0c

Merged on GitHub as 24c29ae9ca75495dea62500482e44a4d3ac09be8; reachable from main 007ac4ac5ee5edc6f79f411b1706752293604044 (observed 2026-11-18T16:35:47Z)

drakestapleton wrote on 2026-10-19 00:19 UTC

Problem

Crumb failures caused most of the failed merges of 2026-10-16..18. Counted on the Spark from gate receipts, journal and pipeline log:

  • 64 failed CRUMB-VERIFY judgements since 2026-10-14. 30 were not about crumbs at all: synapse ../aien-architecture is missing, exit 3. The other 34 said "stale".
  • Every PASS head in omega, aienos and physics published as BLOCKED PASS (Spark status error). That is 38 heads since 2026-10-16.
  • Every RFC-0003 amendment 1 migration head (arch#272, aegis#70, protocols#44, spark-rsi#63, synapse#117, aienos#434, physics#72, sc#674) read "N of N crumbs stale" in the readiness check. That is why none of them could merge.

Two root causes:

A. Tool skew. A crumb verdict depends on which crumb build runs it, and four consumers chose a build in four different ways:

  • each repo's CRUMB-VERIFY gate: crumb.lock pin, the repo's own tree, or /usr/local/bin/crumb
  • the worker's readiness check: the bare name crumb, which resolves to ~/.local/bin/crumb
  • the required hosted check: the pin in its workflow
  • the author's own crumb compile

~/.local/bin/crumb was a fixed 94fffbcf build. Replays show 12 of the 34 STALE failures came from authors compiling with a different tool than the gate. For example, sc 2d16cb94 was compiled with d0f0b95d, and omega 05d0d8e9 was compiled with 94fffbcf.

B. Stale operator config. The poll workers read tools/aien-local-ci/siblings from the work tree of the trusted clones/aien-sovereign-core. That work tree sat at 076585a7 (2026-10-11), so the synapse and interplane lines added since were never live. The same commit failed in robot runs and passed in operator runs with an explicit --siblings.

Change

  • crumb-shim.sh (installed as ~/.local/bin/crumb) picks the tool by the same rule as the gate: crumb.lock pin, then the repo's own tools/crumb for aien-architecture, then the sandbox tool. A malformed or unbuildable pin exits 97 and never falls back silently. Builds are cached and installed by atomic rename.
  • crumb-doctor checks that the gate, readiness and hosted tools give the same verdict on every main and every PR that moves a crumb pin. It also checks that the sibling maps match origin/main and cover each gate's need_sibling/pin. --heal moves this clone's work tree to origin/main. --alert writes a CRUMB-DRIFT line into the aien-watch ALERT. It is bash only (lane rule).
  • README: file table rows and a section with install steps.

Verification (Spark, 2026-10-19)

  • tests/crumb_shim_test.sh: 17 passed. Against the old fixed binary: 15 of 17 FAIL.
  • tests/crumb_doctor_test.sh: 7 passed (stale clone RED, heal, alert raise and clear, dirty clone refused, missing sibling RED).
  • tests/crumb_real_test.sh: 19 passed on real trees. Old tool FAIL / shim OK / gate OK on the omega, aienos and physics mains and on 4 migration heads. Legacy verdicts are unchanged on 6 mains. Replays: sc 2d16cb94 and omega 05d0d8e9 are RED with the author's tool and GREEN with the shim.
  • Live doctor: 15 FAIL before, 0 FAIL after (32 checks), on 30 to 32 heads.
  • aien-local-ci verify Crumb status was STALE on 7 of 8 heads and is now CURRENT on 8 of 8. physics main and arch#272 now say READY.
  • aien-local-ci once synapse bc69cbc0 (robot path, default sibling map): CRUMB-VERIFY PASS with sibling_aien_architecture_commit present (it was exit 3 before).

Already live on the Spark. The shim is at ~/.local/bin/crumb, with the old binary kept as crumb.bak-20261019T0010Z-94fffbcf. The doctor runs from cron-tick.sh. This PR puts the source in the repo so the live copy has a home.

Release limits

This touches tools/aien-local-ci/, so it is a pipeline configuration change (A4): operator run, then a merge by hand. It does not change aien-test. The in-code fix for the readiness tool is still #605 (crumb.lock-aware crumb_status). The two are compatible: #605's PATH fallback now lands on the shim.

Apply it

git checkout -b patch-0ce88f93d2694ef0 a75b7c25e5fd3454af99d97bb6993a2e3481d733
curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/0ce88f93d2694ef0.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aien-sovereign-core-mv
675
84f0aba0847993b9397dca471451060f21435a0c
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":675,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/675","export_sha256":"d815604dc795b494a931d4c9d256159461266bed51d6da460421ed1d94dab90f","imported_utc":"2026-11-18T16:35:47Z"}

Changes

diff --git a/.crumb b/.crumb
index 64aec8e2..3b277823 100644
--- a/.crumb
+++ b/.crumb
@@ -112,21 +112,21 @@
           "purpose": ""
         },
         {
-          "digest": "f44f81600d98021f83fd88d23ed440f13acaa945ce54b2ef276bdd27487f19b6",
-          "last_commit": "1db20d44",
+          "digest": "bb2a911a1a7d4088d5c767bba72d8248154b71fedd0d86102490a594a64a9947",
+          "last_commit": "f23e1cbd",
           "name": "tools/aien-local-ci",
           "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health "
         }
       ],
-      "children_root": "sha256:5f336a15f17c63933efa0604715564620ad61d5e6f37fa82f14d9114f3b1e99a",
-      "compiled_at": "2026-10-18T21:10:27Z",
+      "children_root": "sha256:7233404baa7d5c3ce664e4abee8970212f5e6213399ba74d2ad7a65e3a5324d5",
+      "compiled_at": "2026-10-19T00:25:49Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:d1061244d011eecf12dc11d7a5bea43cd45cbd57b551ed7f7e6392282c69a534",
+      "digest": "sha256:69cf673760ee93f6515aaf2f2a14e6dfcb7727fd42e17a40b228cbd595a9cd9b",
       "evidence_files": 1578,
       "evidence_root": "sha256:135e27a1a0704c4a8371cd2c1eb555f2b5beec61c212949b029a5457418a7160",
-      "files": 5886,
-      "generated_at_commit": "1db20d4424893632e3fe81882b3b9e1289e6956a",
-      "source_tree": "sha256:9227f9cb0fd0297c910632c526cc1574a3c0c58be7bc2ba3e43dd68b248bc712"
+      "files": 5891,
+      "generated_at_commit": "f23e1cbd1827d99d525a19052b6d8ae993951ad6",
+      "source_tree": "sha256:ce2e8982a235ab142a117b2520b276a256c772c9990ac51d31a27b08f5914dd3"
     }
   },
   "history": [],
diff --git a/tools/aien-local-ci/.crumb b/tools/aien-local-ci/.crumb
index 821b30b3..752ad287 100644
--- a/tools/aien-local-ci/.crumb
+++ b/tools/aien-local-ci/.crumb
@@ -3,14 +3,14 @@
     "generated": {
       "children": [],
       "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "compiled_at": "2026-10-18T17:36:55Z",
+      "compiled_at": "2026-10-19T00:25:49Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:f44f81600d98021f83fd88d23ed440f13acaa945ce54b2ef276bdd27487f19b6",
+      "digest": "sha256:bb2a911a1a7d4088d5c767bba72d8248154b71fedd0d86102490a594a64a9947",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 12,
-      "generated_at_commit": "28e3da9787b480b6b71ed92079261098b9d5f996",
-      "source_tree": "sha256:682b3ef8e09a99c9b7c95bf14db2e8eacc74ee3013264df68b6162155b525221"
+      "files": 17,
+      "generated_at_commit": "f23e1cbd1827d99d525a19052b6d8ae993951ad6",
+      "source_tree": "sha256:dc61b4ef713230725436ddaf2cfb6a6f3958daa16ea8a5a5d7727428519a3768"
     }
   },
   "layer": "infra",
diff --git a/tools/aien-local-ci/README.md b/tools/aien-local-ci/README.md
index 66e4c00d..7a5edee4 100644
--- a/tools/aien-local-ci/README.md
+++ b/tools/aien-local-ci/README.md
@@ -18,6 +18,10 @@ AIENOS.
 | `tests/run.sh` | proofs of the contract on this host (33 checks with two wt.sh worktrees present) |
 | `pipeline.sh` | ADR 0033 phase 3: publish verified bundles to aien-receipts, rerun the GitHub thin gate, merge `ready-to-merge` PRs behind guards |
 | `tests/pipeline_test.sh` | offline proofs of `pipeline.sh` with fake `gh`, fake `aien-test` and throwaway git repositories (56 checks) |
+| `crumb-shim.sh` | the operator's `~/.local/bin/crumb`: runs the crumb tool the repository's own CRUMB-VERIFY gate uses (crumb.lock pin, own tree for aien-architecture, else the sandbox tool), so author, readiness check and gate never disagree |
+| `crumb-doctor` | drift check, cron (via cron-tick.sh): gate, readiness and hosted crumb tools agree on every main and tool-changing PR; worker sibling maps match origin/main; `--heal` keeps this clone's work tree on origin/main |
+| `tests/crumb_shim_test.sh`, `tests/crumb_doctor_test.sh` | offline proofs of both on throwaway fixtures (17 and 7 checks) |
+| `tests/crumb_real_test.sh` | Spark-only red/green on real trees and on the real failing heads of 2026-10-16..18 (19 checks) |
 
 ## C8 exec contract
 
@@ -70,6 +74,25 @@ source read from the git object store of `AIEN_ARCH_CLONE`, default `~/workspace
 `cc -O2 -Wall -std=c11`. With gcc 13.3.0 the binary's sha256 is `22015e6b156e038e...`, identical to the operator's
 `~/.local/bin/crumb` on 2026-10-11. To move the pin, change `CRUMB_COMMIT` and `CRUMB_BLOB` in `install.sh` in a PR.
 
+## Operator crumb (shim) and crumb-doctor (2026-10-19)
+
+Four places used to pick a crumb build four different ways: each repo's CRUMB-VERIFY gate (crumb.lock pin, own
+tree, or this sandbox tool), the worker's readiness check (`aien-test verify` runs the bare name `crumb`, so
+`~/.local/bin/crumb`), the required hosted "AIEN Verification" (its workflow pin), and whoever ran `crumb compile`.
+`~/.local/bin/crumb` was a fixed 94fffbcf build. Effects seen 2026-10-16..18: every PASS head in omega, aienos and
+physics (pinned d0f0b95d) published as `BLOCKED PASS` / error; every RFC-0003 Am1 migration head read "N of N crumbs
+stale"; authors compiled sc with d0f0b95d and omega with 94fffbcf and the gates called the result stale (12 of 34
+STALE gate failures). Separately, the poll workers read `siblings` from this clone's WORK TREE, which nothing had
+moved since 2026-10-11, so synapse ran CRUMB-VERIFY without `../aien-architecture` (30 failed judgements, exit 3).
+
+- Install the shim: `install -m 0755 tools/aien-local-ci/crumb-shim.sh ~/.local/bin/crumb` (keep the old binary as
+  `crumb.bak-<ts>-<sha8>`). It never falls back silently: a malformed or unbuildable pin exits 97.
+- Install the doctor: `install -m 0755 tools/aien-local-ci/crumb-doctor ~/.local/bin/crumb-doctor`. cron-tick.sh
+  runs `crumb-doctor --heal-only` every tick and `crumb-doctor --heal --alert` at most every 15 minutes in the
+  background; failures become a `CRUMB-DRIFT` line in the aien-watch ALERT (shown by the gh guard and the chat hook).
+- Before merging any PR that moves a crumb pin (crumb.lock, the hosted workflow pin, tools/crumb, install.sh), run
+  `crumb-doctor --repos <repo>`: every consumer must give the same verdict on the PR head.
+
 ## Egress decision
 
 No nftables rule for uid aien-run. host and qemu checks run in a fresh network namespace with no interface but its own loopback, which is
diff --git a/tools/aien-local-ci/crumb-doctor b/tools/aien-local-ci/crumb-doctor
new file mode 100755
index 00000000..38472b05
--- /dev/null
+++ b/tools/aien-local-ci/crumb-doctor
@@ -0,0 +1,212 @@
+#!/usr/bin/env bash
+# crumb-doctor: the Spark check that crumb failures cannot come back unnoticed (2026-10-19).
+#
+# Root causes it guards (evidence: ~/workspace/crumb-rootcause on the Spark, handoff 2026-10-19-crumb-failures.md):
+#  A. Tool skew. A crumb verdict depends on which crumb build runs it, and four consumers picked a build four ways:
+#     the repo's CRUMB-VERIFY gate (crumb.lock / own tree / sandbox tool), the worker's readiness check (bare "crumb"
+#     on the worker's PATH), the required hosted "AIEN Verification" (a pin in its workflow) and the author's own
+#     "crumb compile". When they disagree, green heads publish as BLOCKED/error and authors commit crumbs the gate
+#     calls stale. ~/.local/bin/crumb (the author + readiness tool) is now the shim in crumb-shim.sh.
+#  B. Stale operator config. Workers read the sibling map from the WORK TREE of the trusted aien-sovereign-core
+#     clone, which nothing moved after 2026-10-11, so repos enrolled later ran CRUMB-VERIFY without their sibling
+#     (synapse: "../aien-architecture is missing", exit 3, 30 failed judgements 2026-10-16..18).
+#
+# Checks
+#   AGREE   <repo> <rev>  gate, hosted and readiness tools give the same `crumb verify` verdict on origin/main and
+#                         on every open PR that changes a crumb tool pin (crumb.lock, the hosted workflow,
+#                         tools/crumb, install.sh) or is an RFC-0003 migration.
+#   SIBMAP  <file>        every sibling map a worker reads equals origin/main's and covers every need_sibling / pin
+#                         the repos' tests/gates/run.sh ask for.
+# Options: --heal (move the trusted clone's work tree to origin/main when clean), --heal-only (just that; cheap, for
+# every cron tick), --alert (CRUMB-DRIFT line in the aien-watch ALERT, which the gh guard and the chat hook show;
+# removed when green), --no-prs (mains only), --repos a,b. Exit 0 all PASS, 1 any FAIL.
+# No Python (lane rule). Needs git, make, cc, sha256sum, gh (only without --no-prs).
+set -u
+CLONES=${AIEN_CLONES:-$HOME/workspace/pipeline/clones}
+STATE=${AIEN_STATE:-$HOME/.local/state/aien-local-ci}
+CACHE=${CRUMB_TOOL_CACHE:-$STATE/crumb-tools}
+SANDBOX=${CRUMB_SANDBOX_TOOL:-/usr/local/bin/crumb}
+WATCH=${AIEN_WATCH_STATE:-$HOME/.local/state/aienos/watch}
+ARCH=$CLONES/aien-architecture
+TRUSTED=$CLONES/aien-sovereign-core
+REPOS="aien-sovereign-core omega aienos physics aien-protocols aegis-runtime spark-rsi aien-architecture benchmarks synapse interplane"
+TOOL_RE='(^|/)crumb\\.lock$|^\\.github/workflows/aien-verification\\.yml$|^tools/crumb/|^tools/aien-local-ci/install\\.sh$'
+HEAL=0 HEAL_ONLY=0 ALERT=0 NOPRS=0
+while [ $# -gt 0 ]; do
+  case $1 in
+    --heal) HEAL=1 ;; --heal-only) HEAL=1; HEAL_ONLY=1 ;; --alert) ALERT=1 ;; --no-prs) NOPRS=1 ;;
+    --repos) REPOS=${2//,/ }; shift ;;
+    *) echo "usage: crumb-doctor [--heal|--heal-only] [--alert] [--no-prs] [--repos a,b]" >&2; exit 2 ;;
+  esac; shift
+done
+npass=0; FAILS=()
+record() {  # record STATUS CHECK SUBJECT DETAIL
+  printf '%-4s %-6s %-40s %s\n' "$1" "$2" "$3" "$4"
+  case $1 in PASS) npass=$((npass+1)) ;; FAIL) FAILS+=("$2 $3") ;; esac
+}
+g() { git -c advice.detachedHead=false "$@"; }
+sha8() { [ -e "$1" ] && sha256sum "$1" | cut -c1-8 || echo -; }
+
+install_tool() {  # install_tool <final> <dir with Makefile+crumb.c>: build, atomic rename
+  local final=$1 src=$2 tmp
+  make -s -C "$src" crumb >&2 || return 1
+  mkdir -p "$(dirname "$final")"; tmp=$(mktemp "$(dirname "$final")/.crumb.XXXXXX") || return 1
+  cp "$src/crumb" "$tmp" && chmod 0755 "$tmp" && mv -f "$tmp" "$final"
+}
+build_pin() {  # build_pin <40-hex>: prints the crumb build of that aien-architecture commit (what crumb.lock gates run)
+  local pin=$1 tool=$CACHE/$1/crumb w
+  if [ ! -x "$tool" ]; then
+    git -C "$ARCH" cat-file -e "$pin^{commit}" 2>/dev/null || git -C "$ARCH" fetch -q origin 2>/dev/null
+    w=$(mktemp -d)
+    git -C "$ARCH" archive "$pin" tools/crumb 2>/dev/null | tar -x -C "$w" && install_tool "$tool" "$w/tools/crumb"
+    rm -rf "$w"
+  fi
+  [ -x "$tool" ] && echo "$tool"
+}
+build_tree() {  # build_tree <tree>: the tool in an aien-architecture tree (its gate builds tools/crumb/crumb.c)
+  local key tool w
+  key=$(cat "$1/tools/crumb/Makefile" "$1/tools/crumb/crumb.c" | sha256sum | cut -c1-16); tool=$CACHE/tree-$key/crumb
+  if [ ! -x "$tool" ]; then
+    w=$(mktemp -d); cp "$1/tools/crumb/Makefile" "$1/tools/crumb/crumb.c" "$w/" && install_tool "$tool" "$w"; rm -rf "$w"
+  fi
+  [ -x "$tool" ] && echo "$tool"
+}
+gate_tool() {  # gate_tool <tree>: "<tool>|<desc>" for what the repo's OWN tests/gates/run.sh crumb-verify step runs
+  local step pin
+  step=$(awk '/^[[:space:]]*crumb-verify\)/{on=1} on{print} on&&/;;/{exit}' "$1/tests/gates/run.sh" 2>/dev/null)
+  if [[ $step == *crumb.lock* ]]; then pin=$(tr -d '[:space:]' < "$1/crumb.lock" | tr 'A-F' 'a-f'); echo "$(build_pin "$pin")|crumb.lock ${pin:0:8}"
+  elif [[ $step == *tools/crumb/crumb.c* ]]; then echo "$(build_tree "$1")|tree tools/crumb"
+  elif [[ $step == *'CRUMB:-crumb'* || $step == *'crumb verify'* ]]; then echo "$SANDBOX|sandbox $(sha8 "$SANDBOX")"
+  else echo "|no crumb-verify step"; fi
+}
+hosted_tool() {  # hosted_tool <tree>: "<tool>|<desc>" for the required hosted check's crumb build
+  local wf=$1/.github/workflows/aien-verification.yml pin
+  [ -f "$wf" ] || { echo "|no hosted workflow"; return; }
+  pin=$(grep -A8 'pinned crumb tool source' "$wf" | grep -m1 -oE 'ref:[[:space:]]*[0-9a-f]{40}' | grep -oE '[0-9a-f]{40}')
+  if [ -n "$pin" ]; then echo "$(build_pin "$pin")|workflow pin ${pin:0:8}"
+  elif grep -q 'Build the crumb tool from this repository' "$wf"; then echo "$(build_tree "$1")|workflow tree tool"
+  else echo "|workflow has no crumb step"; fi
+}
+verdict() {  # verdict <tool> <tree> [PATH]: "OK|<last line>" or "FAIL|<last line>"
+  local out rc
+  [ -n "$1" ] || { echo "n/a|"; return; }
+  if [ -n "${3:-}" ]; then out=$(cd "$2" && env PATH="$3" "$1" verify . 2>&1); rc=$?
+  else out=$(cd "$2" && "$1" verify . 2>&1); rc=$?; fi
+  out=$(printf '%s\n' "$out" | tail -1 | sed 's/^crumb verify: //' | cut -c1-70)
+  if [ $rc = 0 ] && [[ $out == *OK* ]]; then echo "OK|$out"; else echo "FAIL|$out"; fi
+}
+
+# Readiness tools: what the bare name "crumb" resolves to on each running worker's PATH (aien-test verify runs it).
+heal_trusted() {
+  local head main
+  head=$(git -C "$TRUSTED" rev-parse HEAD 2>/dev/null); main=$(git -C "$TRUSTED" rev-parse origin/main 2>/dev/null)
+  [ "$HEAL" = 1 ] && [ -n "$main" ] && [ "$head" != "$main" ] || return 0
+  if [ -n "$(git -C "$TRUSTED" status --porcelain --untracked-files=no)" ]; then
+    record FAIL HEAL "trusted clone" "work tree has local changes; not moved off ${head:0:8}"
+  else
+    g -C "$TRUSTED" checkout -q --detach origin/main && record INFO HEAL "trusted clone" "work tree moved ${head:0:8} -> origin/main ${main:0:8}"
+  fi
+}
+if [ "$HEAL_ONLY" = 1 ]; then heal_trusted; [ ${#FAILS[@]} = 0 ]; exit $?; fi
+
+declare -A RTOOL=() RPATH=(); SIBFILES=()
+if [ "${AIEN_DOCTOR_NO_PROC:-}" != 1 ]; then
+  for p in /proc/[0-9]*; do
+    cmd=$(tr '\0' ' ' < "$p/cmdline" 2>/dev/null) || continue
+    [[ $cmd == *aien-test\ worker* ]] || continue
+    path=$(tr '\0' '\n' < "$p/environ" 2>/dev/null | sed -n 's/^PATH=//p')
+    t=$(env PATH="$path" sh -c 'command -v crumb' 2>/dev/null)
+    [ -n "$t" ] && { RTOOL["$t"]=$t; RPATH["$t"]=$path; }
+    [[ $cmd =~ --siblings\ ([^ ]+) ]] && SIBFILES+=("${BASH_REMATCH[1]}|--siblings of pid ${p#/proc/}")
+  done
+fi
+if [ ${#RTOOL[@]} = 0 ]; then t=$(command -v crumb); [ -n "$t" ] && { RTOOL["$t"]=$t; RPATH["$t"]=$PATH; }; fi
+
+echo "crumb-doctor $(date -u +%FT%TZ): sandbox $SANDBOX $(sha8 "$SANDBOX"); readiness tools: ${!RTOOL[*]}"
+
+# SIBMAP
+map_rows() { sed 's/#.*//' "$@" 2>/dev/null | awk 'NF==4 {print $1" "$2}' | sort -u; }
+heal_trusted
+main=$(git -C "$TRUSTED" rev-parse origin/main 2>/dev/null)
+want=$(git -C "$TRUSTED" show origin/main:tools/aien-local-ci/siblings 2>/dev/null | sed 's/#.*//' | awk 'NF==4 {print $1" "$2}' | sort -u)
+for entry in "$TRUSTED/tools/aien-local-ci/siblings|default (poll workers)" "${SIBFILES[@]}"; do
+  f=${entry%%|*}; who=${entry#*|}
+  missing=$(comm -23 <(printf '%s\n' "$want") <(map_rows "$f") | grep . | sed 's/ / ..\//' | paste -sd, -)
+  if [ -n "$missing" ]; then record FAIL SIBMAP "$f" "$who: missing $missing (origin/main ${main:0:8} has them)"
+  else record PASS SIBMAP "$f" "$who: matches origin/main ${main:0:8}"; fi
+done
+for repo in $REPOS; do
+  runsh=$(git -C "$CLONES/$repo" show origin/main:tests/gates/run.sh 2>/dev/null)
+  need=$( { printf '%s\n' "$runsh" | grep -oE 'need_sibling[[:blank:]]+[A-Za-z0-9._-]+' | awk '{print $2}'
+            printf '%s\n' "$runsh" | grep -E '^[[:blank:]]*pin[[:blank:]]+[^[:blank:]]+[[:blank:]]+[^[:blank:]]+[[:blank:]]+[^[:blank:]]+' | awk '{print $3}'; } | sort -u)
+  for d in $need; do
+    grep -qx "$repo $d" <<<"$want" || record FAIL SIBMAP "$repo needs ../$d" "origin/main sibling map has no line for it"
+  done
+done
+
+# AGREE
+ready_check() {  # ready_check <repo> <sha> <label> <tree>: the worker's own readiness code (aien-test verify) must
+  # call the crumbs CURRENT exactly when the gate's tool says OK. Catches readiness bugs no tool swap can fix
+  # (2026-10-19: verify.rs called every repo without a root .crumb STALE, so benchmarks/interplane PASS -> error).
+  [ "${AIEN_DOCTOR_NO_READY:-}" = 1 ] && return
+  command -v aien-local-ci >/dev/null || return
+  local gt want got
+  if [ -e "$4/.crumb" ]; then gt=$(gate_tool "$4"); gt=${gt%%|*}; else gt=$SANDBOX; fi
+  want=$(verdict "$gt" "$4"); [ "${want%%|*}" = OK ] && want=CURRENT || want=STALE
+  got=$(aien-local-ci verify "$1" "$2" 2>/dev/null | sed -n 's/^Crumb status: //p' | head -1)
+  if [ "$got" = "$want" ]; then record PASS READY "$1 $3" "aien-test verify Crumb status $got (gate tool agrees)"
+  else record FAIL READY "$1 $3" "aien-test verify says Crumb status ${got:-none}, gate tool says $want"; fi
+}
+check_agree() {  # check_agree <repo> <rev> <label>
+  local repo=$1 clone=$CLONES/$1 w sha tree gt ht gv hv kinds detail k rv
+  w=$(mktemp -d); sha=$(git -C "$clone" rev-parse --verify -q "$2^{commit}")
+  if [ -z "$sha" ] || ! git clone -q --shared --no-checkout "$clone" "$w/t" || ! g -C "$w/t" checkout -q --detach "$sha"; then
+    record FAIL AGREE "$repo $3" "cannot check out $2"; rm -rf "$w"; return
+  fi
+  tree=$w/t
+  ready_check "$repo" "$sha" "$3" "$tree"
+  if [ ! -e "$tree/.crumb" ]; then record PASS AGREE "$repo $3" "no crumbs"; rm -rf "$w"; return; fi
+  gt=$(gate_tool "$tree"); ht=$(hosted_tool "$tree")
+  gv=$(verdict "${gt%%|*}" "$tree"); hv=$(verdict "${ht%%|*}" "$tree")
+  kinds="${gv%%|*}"; [ "${hv%%|*}" != n/a ] && kinds="$kinds ${hv%%|*}"
+  detail="gate[${gt#*|}]=${gv%%|*} ${gv#*|} ; hosted[${ht#*|}]=${hv%%|*}"
+  local extra=""
+  for k in "${!RTOOL[@]}"; do
+    rv=$(verdict "$k" "$tree" "${RPATH[$k]}"); kinds="$kinds ${rv%%|*}"; detail="$detail ; readiness[$k]=${rv%%|*}"
+    extra="$extra"$'\n'"        readiness[$k]: ${rv#*|}"
+  done
+  if [ "$(tr ' ' '\n' <<<"$kinds" | grep . | sort -u | wc -l)" = 1 ]; then
+    [ "${gv%%|*}" = OK ] && record PASS AGREE "$repo $3" "$detail" || record PASS AGREE "$repo $3" "all say FAIL (the PR's own crumbs): $detail"
+  else
+    record FAIL AGREE "$repo $3" "DISAGREE $detail"$'\n'"        gate: ${gv#*|}"$'\n'"        hosted: ${hv#*|}$extra"
+  fi
+  rm -rf "$w"
+}
+for repo in $REPOS; do
+  clone=$CLONES/$repo; [ -d "$clone/.git" ] || { record FAIL AGREE "$repo" "no trusted clone at $clone"; continue; }
+  git -C "$clone" fetch -q origin 2>/dev/null
+  check_agree "$repo" origin/main "main $(git -C "$clone" rev-parse --short=8 origin/main)"
+  [ "$NOPRS" = 1 ] && continue
+  slug=$(git -C "$clone" remote get-url origin | sed -E 's#.*github\.com[:/]##; s#\.git$##')
+  prs=$(gh pr list -R "$slug" --state open --limit 100 --json number,headRefOid,title,files \
+        --jq ".[] | select((.title|test(\"RFC-0003\")) or ([.files[]?.path] | any(test(\"$TOOL_RE\")))) | \"\(.number) \(.headRefOid)\"" 2>/dev/null) \
+    || { record FAIL AGREE "$repo PR list" "gh pr list failed"; continue; }
+  while read -r n sha; do
+    [ -n "$n" ] || continue
+    git -C "$clone" cat-file -e "$sha^{commit}" 2>/dev/null || git -C "$clone" fetch -q origin "pull/$n/head" 2>/dev/null
+    check_agree "$repo" "$sha" "#$n ${sha:0:8}"
+  done <<<"$prs"
+done
+
+echo "crumb-doctor: $npass pass, ${#FAILS[@]} FAIL"
+if [ "$ALERT" = 1 ]; then
+  mkdir -p "$WATCH"; stamp=$(date -u +%FT%TZ)
+  keep=$(grep -v '^CRUMB-DRIFT:' "$WATCH/ALERT" 2>/dev/null)
+  if [ ${#FAILS[@]} -gt 0 ]; then
+    text=$(printf '%s; ' "${FAILS[@]}" | cut -c1-600)
+    printf '%s CRUMB-DRIFT %s\n' "$stamp" "$text" >> "$WATCH/alerts.log"
+    keep=$(printf '%s\n%s' "$keep" "CRUMB-DRIFT: ${#FAILS[@]} crumb check(s) fail: $text(run: crumb-doctor; since $stamp)" | grep .)
+  fi
+  if [ -n "$keep" ]; then printf '%s\n' "$keep" > "$WATCH/ALERT.tmp" && mv -f "$WATCH/ALERT.tmp" "$WATCH/ALERT"; else rm -f "$WATCH/ALERT"; fi
+fi
+[ ${#FAILS[@]} = 0 ]
diff --git a/tools/aien-local-ci/crumb-shim.sh b/tools/aien-local-ci/crumb-shim.sh
new file mode 100755
index 00000000..25e37b04
--- /dev/null
+++ b/tools/aien-local-ci/crumb-shim.sh
@@ -0,0 +1,75 @@
+#!/usr/bin/env bash
+# crumb (operator shim, installed as ~/.local/bin/crumb on the Spark).
+#
+# Runs the crumb tool that the repository's own CRUMB-VERIFY gate uses, so the person or agent that
+# compiles crumbs, the worker's readiness check (aien-test verify runs the bare name "crumb") and the
+# gate can never disagree about which tool is right. Root cause it closes (2026-10-18 crumb failures):
+# four places picked a crumb tool four different ways, and ~/.local/bin/crumb was a fixed 94fffbcf
+# build, so omega/aienos/physics mains (pinned d0f0b95d) and every RFC-0003 Am1 migrated tree read
+# "stale" outside their own gate.
+#
+# Rule, the same order the gates use (tests/gates/run.sh in each repo):
+#   1. the tree has crumb.lock      -> the aien-architecture commit it names, built from the trusted
+#                                      clone (git archive + make, like the gate), cached by commit
+#   2. the tree is aien-architecture -> the tool in that tree (tools/crumb/crumb.c + Makefile), cached
+#      (has tools/crumb/crumb.c)        by content hash, like its gate
+#   3. anything else                 -> the sandbox tool /usr/local/bin/crumb, which is what
+#                                      "${CRUMB:-crumb}" resolves to inside the gate sandbox
+# A pin that cannot be built is an error (exit 97). Never a silent fallback: a fallback to some other
+# tool is exactly the bug this file exists to stop.
+#
+# The tree is the git top level of the first argument that names an existing path, else of the
+# current directory. Env: CRUMB_SHIM_WHICH=1 prints "<tool> <rule> <root>" and exits 0 (doctor, tests).
+# CRUMB_SANDBOX_TOOL, CRUMB_TOOL_CACHE, CRUMB_ARCH_CLONE override locations (tests only).
+set -u
+SANDBOX=${CRUMB_SANDBOX_TOOL:-/usr/local/bin/crumb}
+CACHE=${CRUMB_TOOL_CACHE:-$HOME/.local/state/aien-local-ci/crumb-tools}
+ARCH=${CRUMB_ARCH_CLONE:-$HOME/workspace/pipeline/clones/aien-architecture}
+
+die() { echo "crumb (shim): $*" >&2; exit 97; }
+
+target=.
+for a in "$@"; do
+  case $a in -*) continue ;; esac
+  if [ -e "$a" ]; then target=$a; break; fi
+done
+[ -d "$target" ] || target=$(dirname -- "$target")
+root=$(git -C "$target" rev-parse --show-toplevel 2>/dev/null || true)
+
+build_into() {  # build_into <final-path> <src-dir containing Makefile+crumb.c>; atomic rename
+  local final=$1 src=$2 tmpb
+  mkdir -p "$(dirname -- "$final")" || die "cannot create $(dirname -- "$final")"
+  make -s -C "$src" crumb >&2 || die "make crumb failed in $src"
+  tmpb=$(mktemp "$(dirname -- "$final")/.crumb.XXXXXX") || die "mktemp"
+  cp "$src/crumb" "$tmpb" && chmod 0755 "$tmpb" && mv -f "$tmpb" "$final" || { rm -f "$tmpb"; die "install $final"; }
+}
+
+tool=""; rule=""
+if [ -n "$root" ] && [ -f "$root/crumb.lock" ]; then
+  pin=$(tr -d '[:space:]' < "$root/crumb.lock" | tr 'A-F' 'a-f')
+  [[ $pin =~ ^[0-9a-f]{40}$ ]] || die "$root/crumb.lock is not one 40-hex commit"
+  tool=$CACHE/$pin/crumb; rule="crumb.lock:$pin"
+  if [ ! -x "$tool" ]; then
+    git -C "$ARCH" cat-file -e "$pin^{commit}" 2>/dev/null || git -C "$ARCH" fetch -q origin 2>/dev/null
+    git -C "$ARCH" cat-file -e "$pin^{commit}" 2>/dev/null || die "crumb.lock pin $pin is not in $ARCH"
+    w=$(mktemp -d) || die mktemp; trap 'rm -rf "$w"' EXIT
+    git -C "$ARCH" archive "$pin" tools/crumb | tar -x -C "$w" || die "git archive $pin tools/crumb"
+    build_into "$tool" "$w/tools/crumb"
+    rm -rf "$w"; trap - EXIT   # exec below skips EXIT traps
+  fi
+elif [ -n "$root" ] && [ -f "$root/tools/crumb/crumb.c" ] && [ -f "$root/tools/crumb/Makefile" ]; then
+  key=$(cat "$root/tools/crumb/Makefile" "$root/tools/crumb/crumb.c" | sha256sum | cut -c1-16)
+  tool=$CACHE/tree-$key/crumb; rule="tree:$key"
+  if [ ! -x "$tool" ]; then
+    w=$(mktemp -d) || die mktemp; trap 'rm -rf "$w"' EXIT
+    cp "$root/tools/crumb/Makefile" "$root/tools/crumb/crumb.c" "$w/" || die "copy tree tool"
+    build_into "$tool" "$w"
+    rm -rf "$w"; trap - EXIT
+  fi
+else
+  tool=$SANDBOX; rule=sandbox
+  [ -x "$tool" ] || die "sandbox tool $tool is missing"
+fi
+
+if [ "${CRUMB_SHIM_WHICH:-}" = 1 ]; then printf '%s %s %s\n' "$tool" "$rule" "${root:--}"; exit 0; fi
+exec "$tool" "$@"
diff --git a/tools/aien-local-ci/tests/crumb_doctor_test.sh b/tools/aien-local-ci/tests/crumb_doctor_test.sh
new file mode 100755
index 00000000..5d0e1e7b
--- /dev/null
+++ b/tools/aien-local-ci/tests/crumb_doctor_test.sh
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+# test-crumb-doctor.sh <doctor>: the doctor goes RED on a stale trusted clone (the 2026-10-11..18 synapse
+# CRUMB-VERIFY "../aien-architecture is missing" cause), heals it, and raises/clears its aien-watch alert.
+set -u
+DOC=$(readlink -f "${1:?usage: test-crumb-doctor.sh <doctor>}")
+T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
+pass=0; fail=0
+ok()  { pass=$((pass+1)); echo "ok   $1"; }
+bad() { fail=$((fail+1)); echo "FAIL $1: $2"; }
+g() { git -c user.name=t -c user.email=t@t -c commit.gpgsign=false "$@"; }
+export AIEN_CLONES=$T/clones AIEN_WATCH_STATE=$T/watch AIEN_DOCTOR_NO_PROC=1 AIEN_DOCTOR_NO_READY=1 CRUMB_TOOL_CACHE=$T/cache
+mkdir -p "$T/clones" "$T/watch"
+echo "OTHER: unrelated alert (must survive)" > "$T/watch/ALERT"
+
+# upstream sc: commit 1 without the synapse sibling line, commit 2 with it
+mkdir -p "$T/up/sc/tools/aien-local-ci"; cd "$T/up/sc"; g init -q -b main
+echo "omega aien-architecture aien-architecture main" > tools/aien-local-ci/siblings; g add -A; g commit -qm one
+c1=$(git rev-parse HEAD)
+echo "synapse aien-architecture aien-architecture main" >> tools/aien-local-ci/siblings; g commit -qam two
+# upstream synapse: its gate asks for ../aien-architecture
+mkdir -p "$T/up/synapse/tests/gates"; cd "$T/up/synapse"; g init -q -b main
+printf 'case $1 in\n  crumb-verify)\n    need_sibling aien-architecture\n    ;;\nesac\n' > tests/gates/run.sh; g add -A; g commit -qm s
+# trusted clones; sc work tree left on the OLD commit (what the Spark had)
+git clone -q "$T/up/sc" "$T/clones/aien-sovereign-core"; git -C "$T/clones/aien-sovereign-core" -c advice.detachedHead=false checkout -q --detach "$c1"
+git clone -q "$T/up/synapse" "$T/clones/synapse"
+
+o=$("$DOC" --no-prs --repos synapse --alert 2>&1); rc=$?
+(( rc == 1 )) && [[ $o == *"FAIL SIBMAP"*"missing synapse ../aien-architecture"* ]] && ok "stale trusted clone is RED (exit 1, names the missing sibling)" || bad "red" "rc=$rc $o"
+grep -q '^CRUMB-DRIFT: ' "$T/watch/ALERT" && grep -q '^OTHER: ' "$T/watch/ALERT" && ok "alert raised, other alerts kept" || bad "alert" "$(cat "$T/watch/ALERT")"
+
+o=$("$DOC" --heal-only 2>&1); rc=$?
+h=$(git -C "$T/clones/aien-sovereign-core" rev-parse HEAD); m=$(git -C "$T/clones/aien-sovereign-core" rev-parse origin/main)
+(( rc == 0 )) && [ "$h" = "$m" ] && ok "--heal-only moves the work tree to origin/main" || bad "heal" "rc=$rc head=$h main=$m $o"
+
+o=$("$DOC" --no-prs --repos synapse --alert 2>&1); rc=$?
+(( rc == 0 )) && [[ $o == *"PASS SIBMAP"* ]] && ok "after heal: GREEN" || bad "green" "rc=$rc $o"
+! grep -q '^CRUMB-DRIFT: ' "$T/watch/ALERT" && grep -q '^OTHER: ' "$T/watch/ALERT" && ok "alert cleared when green, other alerts kept" || bad "clear" "$(cat "$T/watch/ALERT")"
+
+# a dirty trusted clone is never moved (someone is using it): reported, not clobbered
+git -C "$T/clones/aien-sovereign-core" -c advice.detachedHead=false checkout -q --detach "$c1"
+echo "local edit" >> "$T/clones/aien-sovereign-core/tools/aien-local-ci/siblings"
+o=$("$DOC" --heal-only 2>&1); rc=$?
+(( rc == 1 )) && [[ $o == *"local changes"* ]] && grep -q "local edit" "$T/clones/aien-sovereign-core/tools/aien-local-ci/siblings" && ok "dirty work tree: refused, edit kept" || bad "dirty" "rc=$rc $o"
+
+# map on main missing a sibling a gate needs: RED even when the work tree is current
+git -C "$T/clones/aien-sovereign-core" checkout -q -- . ; git -C "$T/clones/aien-sovereign-core" -c advice.detachedHead=false checkout -q --detach origin/main
+cd "$T/up/synapse"; sed -i 's/need_sibling aien-architecture/need_sibling aien-architecture\n    need_sibling omega/' tests/gates/run.sh; g commit -qam needs-omega; git -C "$T/clones/synapse" fetch -q
+o=$("$DOC" --no-prs --repos synapse 2>&1); rc=$?
+(( rc == 1 )) && [[ $o == *"synapse needs ../omega"* ]] && ok "gate needs a sibling the map lacks: RED" || bad "coverage" "rc=$rc $o"
+
+echo "doctor tests: $pass passed, $fail failed"; (( fail == 0 ))
diff --git a/tools/aien-local-ci/tests/crumb_real_test.sh b/tools/aien-local-ci/tests/crumb_real_test.sh
new file mode 100755
index 00000000..c918a9fb
--- /dev/null
+++ b/tools/aien-local-ci/tests/crumb_real_test.sh
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+# test-crumb-real.sh <shim> <old-tool>: red/green on real Spark trees and on the real failing heads.
+# GREEN needs the shim to agree with each repo's own gate where the old fixed tool did not.
+set -u
+SHIM=$(readlink -f "$1"); OLD=$(readlink -f "$2")
+C=~/workspace/pipeline/clones; T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
+D0=~/.local/state/aien-local-ci/crumb-tools/d0f0b95d344b4c6628c3d868346932fd247ce168/crumb
+GATE_SANDBOX=/usr/local/bin/crumb
+pass=0; fail=0
+ok()  { pass=$((pass+1)); echo "ok   $1"; }
+bad() { fail=$((fail+1)); echo "FAIL $1: $2"; }
+tree() {  # resolve the rev in the SOURCE clone (its origin/main), never the copy's idea of it
+  local sha; sha=$(git -C "$C/$1" rev-parse --verify "$2^{commit}") || { echo "no $2 in $1"; return 1; }
+  rm -rf "$T/$1"; git clone -q --shared --no-checkout "$C/$1" "$T/$1" && git -C "$T/$1" -c advice.detachedHead=false checkout -q --detach "$sha"; }
+v() { (cd "$1" && "$2" verify . 2>&1 | tail -1); }
+g() { git -c user.name=t -c user.email=t@t -c commit.gpgsign=false "$@"; }
+
+# 1. readiness on mains that pin d0f0b95d: old tool says stale, shim says what the gate says
+for r in omega aienos physics; do
+  tree $r origin/main
+  o=$(v "$T/$r" "$OLD"); s=$(v "$T/$r" "$SHIM"); gt=$(v "$T/$r" "$D0")
+  [[ $o == *FAIL* ]] && [[ $s == *OK* ]] && [[ $gt == *OK* ]] && ok "$r main: old=FAIL shim=OK gate=OK" || bad "$r main" "old='$o' shim='$s' gate='$gt'"
+done
+# 2. migrated trees (RFC-0003 Am1 PR heads): old tool calls every crumb stale, shim agrees with the gate
+for x in aegis-runtime:aeefa8dad09954129a06ee429128ae640cb5d0bd aien-protocols:083601ab spark-rsi:d272ecb7 aien-architecture:00034398; do
+  r=${x%%:*}; tree $r "${x#*:}"
+  o=$(v "$T/$r" "$OLD"); s=$(v "$T/$r" "$SHIM")
+  [[ $o == *FAIL* ]] && [[ $s == *"OK"*"on demand"* ]] && ok "$r ${x#*:} (migrated): old=FAIL shim=OK" || bad "$r migrated" "old='$o' shim='$s'"
+done
+# 3. legacy repos without a lock: shim (sandbox tool) and old tool give the same verdict on main
+for r in aien-sovereign-core aegis-runtime aien-protocols spark-rsi aien-architecture synapse; do
+  tree $r origin/main
+  o=$(v "$T/$r" "$OLD"); s=$(v "$T/$r" "$SHIM")
+  [ "$o" = "$s" ] && ok "$r main legacy verdict unchanged by shim ($s)" || bad "$r main legacy" "old='$o' shim='$s'"
+done
+# 4. author replay on real failing heads: recompile the head's crumbs, then ask the GATE's tool
+#    sc 2d16cb94: author compiled with d0f0b95d, sc gate (sandbox tool) said 1 stale (2026-10-16)
+#    omega 05d0d8e9: author compiled with 94fffbcf, omega gate (crumb.lock d0f0b95d) said 10 stale
+replay() {  # replay <repo> <sha> <gate-tool> <author-tool> <label>
+  tree "$1" "$2"; (cd "$T/$1" && "$4" compile . >/dev/null 2>&1)
+  v "$T/$1" "$3"
+}
+s=$(replay aien-sovereign-core 2d16cb94 "$GATE_SANDBOX" "$D0"); [[ $s == *FAIL* ]] && ok "sc 2d16cb94 RED: compiled with d0f0b95d, gate says '$s'" || bad "sc replay red" "$s"
+s=$(replay aien-sovereign-core 2d16cb94 "$GATE_SANDBOX" "$SHIM"); [[ $s == *OK* ]] && ok "sc 2d16cb94 GREEN: compiled with shim, gate says '$s'" || bad "sc replay green" "$s"
+s=$(replay omega 05d0d8e9 "$D0" "$OLD"); [[ $s == *FAIL* ]] && ok "omega 05d0d8e9 RED: compiled with old tool, gate says '$s'" || bad "omega replay red" "$s"
+s=$(replay omega 05d0d8e9 "$D0" "$SHIM"); [[ $s == *OK* ]] && ok "omega 05d0d8e9 GREEN: compiled with shim, gate says '$s'" || bad "omega replay green" "$s"
+# 5. after migration an old-tool compile is refused by the gate (the protocol's own guard): proves the
+#    remaining failure mode is loud, not silent
+tree aegis-runtime aeefa8dad09954129a06ee429128ae640cb5d0bd; (cd "$T/aegis-runtime" && "$OLD" compile . >/dev/null 2>&1)
+s=$(v "$T/aegis-runtime" "$GATE_SANDBOX"); [[ $s == *FAIL* ]] && ok "old-tool compile on a migrated tree is caught by the gate ('$s')" || bad "migrated old compile" "$s"
+tree aegis-runtime aeefa8dad09954129a06ee429128ae640cb5d0bd; (cd "$T/aegis-runtime" && "$SHIM" compile . >/dev/null 2>&1)
+d=$(git -C "$T/aegis-runtime" status --porcelain | wc -l); s=$(v "$T/aegis-runtime" "$GATE_SANDBOX")
+(( d == 0 )) && [[ $s == *OK* ]] && ok "shim compile on a migrated tree changes nothing and verifies" || bad "migrated shim compile" "dirty=$d '$s'"
+
+echo "real-tree tests: $pass passed, $fail failed"; (( fail == 0 ))
diff --git a/tools/aien-local-ci/tests/crumb_shim_test.sh b/tools/aien-local-ci/tests/crumb_shim_test.sh
new file mode 100755
index 00000000..18e3e566
--- /dev/null
+++ b/tools/aien-local-ci/tests/crumb_shim_test.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+# test-crumb-shim.sh <shim>: unit tests for the operator crumb shim, on throwaway fixtures only.
+# Each case asserts which tool ran (the fake tools print their name), so a wrong pick is RED.
+set -u
+SHIM=$(readlink -f "${1:?usage: test-crumb-shim.sh <shim>}")
+T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
+pass=0; fail=0
+ok()   { pass=$((pass+1)); echo "ok   $1"; }
+bad()  { fail=$((fail+1)); echo "FAIL $1: $2"; }
+g() { git -c user.name=t -c user.email=t@t -c commit.gpgsign=false "$@"; }
+
+# fake sandbox tool
+printf '#!/bin/sh\necho SANDBOX "$@"\n' > "$T/sandbox"; chmod +x "$T/sandbox"
+# fake aien-architecture clone: two tool commits that print their own name
+mkdir -p "$T/arch/tools/crumb"; cd "$T/arch"; g init -q
+mk() { printf 'crumb: crumb.c\n\t$(CC) -o $@ crumb.c\n' > tools/crumb/Makefile
+       printf '#include <stdio.h>\nint main(int c,char**v){printf("%s");for(int i=1;i<c;i++)printf(" %%s",v[i]);printf("\\n");return 0;}\n' "$1" > tools/crumb/crumb.c; }
+mk PIN-A; g add -A; g commit -qm A; A=$(git rev-parse HEAD)
+mk PIN-B; g add -A; g commit -qm B; B=$(git rev-parse HEAD)
+export CRUMB_SANDBOX_TOOL=$T/sandbox CRUMB_TOOL_CACHE=$T/cache CRUMB_ARCH_CLONE=$T/arch
+repo() { mkdir -p "$T/$1"; (cd "$T/$1" && g init -q && echo '{}' > .crumb && g add -A && g commit -qm init); }
+
+repo locked; echo "$A" > "$T/locked/crumb.lock"
+repo lockedB; echo "  ${B^^}  " > "$T/lockedB/crumb.lock"          # whitespace + upper case tolerated
+repo plain
+repo badlock; echo "not-a-sha" > "$T/badlock/crumb.lock"
+repo nopin; echo "0123456789012345678901234567890123456789" > "$T/nopin/crumb.lock"
+repo treerepo; mkdir -p "$T/treerepo/tools/crumb"; (cd "$T/treerepo" && cp "$T/arch/tools/crumb/Makefile" tools/crumb/ \
+  && sed 's/PIN-B/TREE-T/' "$T/arch/tools/crumb/crumb.c" > tools/crumb/crumb.c)
+mkdir -p "$T/locked/sub/dir" "$T/notgit"
+
+out() { (cd "$1" && shift && "$SHIM" "$@" 2>&1); }
+expect() {  # expect <name> <expected-prefix> <dir> <args...>
+  local name=$1 want=$2 dir=$3; shift 3
+  local got; got=$(out "$dir" "$@"); [[ $got == "$want"* ]] && ok "$name" || bad "$name" "want '$want...', got '$got'"
+}
+expect "crumb.lock picks the pinned build"            "PIN-A verify ."        "$T/locked" verify .
+expect "pin with spaces/upper case still resolves"   "PIN-B verify ."        "$T/lockedB" verify .
+expect "subdirectory of a locked repo"               "PIN-A verify ."        "$T/locked/sub/dir" verify .
+expect "path argument decides the repo, not cwd"     "PIN-A verify $T/locked" "$T/plain" verify "$T/locked"
+expect "file argument resolves via its directory"    "PIN-A context"          "$T/notgit" context "$T/locked/.crumb"
+expect "no lock, no tool source: sandbox tool"       "SANDBOX verify ."       "$T/plain" verify .
+expect "not a git tree: sandbox tool"                "SANDBOX verify ."       "$T/notgit" verify .
+expect "aien-architecture tree: its own tools/crumb" "TREE-T verify ."        "$T/treerepo" verify .
+expect "worker's close call (non-path words skipped)" "PIN-A close aien-worker ." "$T/locked" close aien-worker . worker test "local verification finished"
+
+# errors are loud and never fall back to another tool
+r=$(out "$T/badlock" verify .); rc=$?
+(( rc == 97 )) && [[ $r == *"not one 40-hex commit"* ]] && [[ $r != *SANDBOX* ]] && ok "malformed crumb.lock: exit 97, no fallback" || bad "malformed lock" "rc=$rc out=$r"
+r=$(out "$T/nopin" verify .); rc=$?
+(( rc == 97 )) && [[ $r == *"is not in"* ]] && [[ $r != *SANDBOX* ]] && ok "pin missing from the arch clone: exit 97, no fallback" || bad "missing pin" "rc=$rc out=$r"
+r=$(CRUMB_SANDBOX_TOOL=$T/none out "$T/plain" verify .); rc=$?
+(( rc == 97 )) && ok "missing sandbox tool: exit 97" || bad "missing sandbox" "rc=$rc out=$r"
+
+# exit status and stdout pass through untouched
+printf '#!/bin/sh\necho out; echo err >&2; exit 5\n' > "$T/sandbox"
+o=$(cd "$T/plain" && "$SHIM" verify . 2>/dev/null); rc=$?
+(( rc == 5 )) && [ "$o" = out ] && ok "exit code and stdout pass through" || bad "pass-through" "rc=$rc out=$o"
+
+# cache: built once, reused; parallel first builds of one pin all succeed (atomic rename)
+m1=$(stat -c %Y "$T/cache/$A/crumb"); sleep 1; out "$T/locked" verify . >/dev/null; m2=$(stat -c %Y "$T/cache/$A/crumb")
+[ "$m1" = "$m2" ] && ok "cached build is reused" || bad "cache reuse" "$m1 != $m2"
+rm -rf "$T/cache"; for i in $(seq 8); do (out "$T/locked" verify . > "$T/par.$i") & done; wait
+n=$(cat "$T"/par.* | grep -c '^PIN-A verify \.$'); (( n == 8 )) && ok "8 parallel first runs agree" || bad "parallel" "$n/8: $(cat "$T"/par.* | sort | uniq -c)"
+ls "$T/cache/$A"/.crumb.* >/dev/null 2>&1 && bad "no temp files left" "$(ls -a "$T/cache/$A")" || ok "no temp files left"
+
+# WHICH mode for the doctor
+w=$(cd "$T/locked" && CRUMB_SHIM_WHICH=1 "$SHIM" verify .)
+[[ $w == "$T/cache/$A/crumb crumb.lock:$A $T/locked" ]] && ok "CRUMB_SHIM_WHICH reports tool, rule, root" || bad "which" "$w"
+
+echo "shim tests: $pass passed, $fail failed"; (( fail == 0 ))