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
aien-sovereign-core-mv / tools / aien-local-ci / crumb-doctor
| 1 | #!/usr/bin/env bash |
| 2 | # crumb-doctor: the Spark check that crumb failures cannot come back unnoticed (2026-10-19). |
| 3 | # |
| 4 | # The operator may inspect candidate crumbs, but must never build or run a |
| 5 | # candidate-selected gate, hosted tool, Makefile or pin. Main's gate, hosted |
| 6 | # pin and architecture source are the only authority for tool comparisons. |
| 7 | # Candidate changes to that authority need operator promotion, not a verdict |
| 8 | # obtained by executing the PR's own tool. Exit 1 keeps promotion visible in |
| 9 | # the CRUMB-DRIFT alarm until the change lands on trusted main. |
| 10 | # |
| 11 | # --heal moves only a clean trusted clone to origin/main; --heal-only just |
| 12 | # heals it. --alert maintains the CRUMB-DRIFT alert. --no-prs skips PR metadata. |
| 13 | # --repos selects repos. No candidate command is run. |
| 14 | set -u |
| 15 | CLONES=${AIEN_CLONES:-$HOME/workspace/pipeline/clones} |
| 16 | STATE=${AIEN_STATE:-$HOME/.local/state/aien-local-ci} |
| 17 | CACHE=${CRUMB_TOOL_CACHE:-$STATE/crumb-tools} |
| 18 | SANDBOX=${CRUMB_SANDBOX_TOOL:-/usr/local/bin/crumb} |
| 19 | WATCH=${AIEN_WATCH_STATE:-$HOME/.local/state/aienos/watch} |
| 20 | ARCH=$CLONES/aien-architecture |
| 21 | TRUSTED=$CLONES/aien-sovereign-core |
| 22 | REPOS="aien-sovereign-core omega aienux physics aien-protocols aegis-runtime spark-rsi aien-architecture benchmarks synapse interplane" |
| 23 | TOOL_RE='(^|/)crumb\\.lock$|^\\.github/workflows/aien-verification\\.yml$|^tools/crumb/|^tools/aien-local-ci/install\\.sh$|^tests/gates/run\\.sh$' |
| 24 | HEAL=0 HEAL_ONLY=0 ALERT=0 NOPRS=0 |
| 25 | while [ $# -gt 0 ]; do |
| 26 | case $1 in |
| 27 | --heal) HEAL=1 ;; --heal-only) HEAL=1; HEAL_ONLY=1 ;; --alert) ALERT=1 ;; --no-prs) NOPRS=1 ;; |
| 28 | --repos) REPOS=${2//,/ }; shift ;; |
| 29 | *) echo "usage: crumb-doctor [--heal|--heal-only] [--alert] [--no-prs] [--repos a,b]" >&2; exit 2 ;; |
| 30 | esac; shift |
| 31 | done |
| 32 | npass=0; FAILS=() |
| 33 | record() { # record STATUS CHECK SUBJECT DETAIL |
| 34 | printf '%-4s %-6s %-40s %s\n' "$1" "$2" "$3" "$4" |
| 35 | case $1 in PASS) npass=$((npass+1)) ;; FAIL) FAILS+=("$2 $3") ;; esac |
| 36 | } |
| 37 | g() { git -c advice.detachedHead=false "$@"; } |
| 38 | sha8() { [ -e "$1" ] && sha256sum "$1" | cut -c1-8 || echo -; } |
| 39 | |
| 40 | install_tool() { # install_tool <final> <archived trusted tools/crumb> |
| 41 | local final=$1 src=$2 tmp |
| 42 | make -s -C "$src" crumb >&2 || return 1 |
| 43 | mkdir -p "$(dirname "$final")"; tmp=$(mktemp "$(dirname "$final")/.crumb.XXXXXX") || return 1 |
| 44 | cp "$src/crumb" "$tmp" && chmod 0755 "$tmp" && mv -f "$tmp" "$final" |
| 45 | } |
| 46 | build_pin() { # only an architecture origin/main ancestor may become an operator tool |
| 47 | local pin=$1 tool=$CACHE/$1/crumb w |
| 48 | [[ $pin =~ ^[0-9a-f]{40}$ ]] || return 1 |
| 49 | git -C "$ARCH" merge-base --is-ancestor "$pin" refs/remotes/origin/main 2>/dev/null || return 1 |
| 50 | if [ ! -x "$tool" ]; then |
| 51 | w=$(mktemp -d) || return 1 |
| 52 | if ! git -C "$ARCH" archive "$pin" tools/crumb 2>/dev/null | tar -x -C "$w" \ |
| 53 | || [ ! -f "$w/tools/crumb/Makefile" ] || [ ! -f "$w/tools/crumb/crumb.c" ]; then |
| 54 | rm -rf "$w"; return 1 |
| 55 | fi |
| 56 | install_tool "$tool" "$w/tools/crumb"; local rc=$? |
| 57 | rm -rf "$w"; [ "$rc" = 0 ] || return 1 |
| 58 | fi |
| 59 | [ -x "$tool" ] && echo "$tool" |
| 60 | } |
| 61 | build_arch_main() { # only architecture's origin/main, never a checked-out candidate |
| 62 | local main tool w |
| 63 | main=$(git -C "$ARCH" rev-parse --verify -q 'refs/remotes/origin/main^{commit}') || return 1 |
| 64 | tool=$CACHE/main-$main/crumb |
| 65 | if [ ! -x "$tool" ]; then |
| 66 | w=$(mktemp -d) || return 1 |
| 67 | if ! git -C "$ARCH" archive "$main" tools/crumb 2>/dev/null | tar -x -C "$w" \ |
| 68 | || [ ! -f "$w/tools/crumb/Makefile" ] || [ ! -f "$w/tools/crumb/crumb.c" ]; then |
| 69 | rm -rf "$w"; return 1 |
| 70 | fi |
| 71 | install_tool "$tool" "$w/tools/crumb"; local rc=$? |
| 72 | rm -rf "$w"; [ "$rc" = 0 ] || return 1 |
| 73 | fi |
| 74 | [ -x "$tool" ] && echo "$tool" |
| 75 | } |
| 76 | gate_tool() { # gate_tool <main tree> <repo>: never pass a PR tree |
| 77 | local step pin |
| 78 | step=$(awk '/^[[:space:]]*crumb-verify\)/{on=1} on{print} on&&/;;/{exit}' "$1/tests/gates/run.sh" 2>/dev/null) |
| 79 | if [[ $step == *crumb.lock* ]]; then |
| 80 | pin=$(tr -d '[:space:]' < "$1/crumb.lock" | tr 'A-F' 'a-f') |
| 81 | echo "$(build_pin "$pin")|crumb.lock ${pin:0:8}" |
| 82 | elif [ "$2" = aien-architecture ] && [[ $step == *tools/crumb/crumb.c* ]]; then |
| 83 | echo "$(build_arch_main)|architecture origin/main tool" |
| 84 | elif [[ $step == *'CRUMB:-crumb'* || $step == *'crumb verify'* ]]; then |
| 85 | echo "$SANDBOX|sandbox $(sha8 "$SANDBOX")" |
| 86 | else echo "|no crumb-verify step"; fi |
| 87 | } |
| 88 | hosted_tool() { # hosted_tool <main tree> <repo>: never pass a PR tree |
| 89 | local wf=$1/.github/workflows/aien-verification.yml pin |
| 90 | [ -f "$wf" ] || { echo "|no hosted workflow"; return; } |
| 91 | pin=$(grep -A8 'pinned crumb tool source' "$wf" | grep -m1 -oE 'ref:[[:space:]]*[0-9a-f]{40}' | grep -oE '[0-9a-f]{40}') |
| 92 | if [ -n "$pin" ]; then echo "$(build_pin "$pin")|workflow pin ${pin:0:8}" |
| 93 | elif grep -q 'pinned crumb tool source' "$wf"; then echo "|workflow pin invalid" |
| 94 | elif [ "$2" = aien-architecture ] && grep -q 'Build the crumb tool from this repository' "$wf"; then |
| 95 | echo "$(build_arch_main)|architecture origin/main workflow tool" |
| 96 | else echo "|workflow has no crumb step"; fi |
| 97 | } |
| 98 | verdict() { # verdict <trusted tool> <tree>: "OK|<last line>" or "FAIL|<last line>" |
| 99 | local out rc |
| 100 | [ -n "$1" ] || { echo "n/a|"; return; } |
| 101 | out=$(cd "$2" && "$1" verify . 2>&1); rc=$? |
| 102 | out=$(printf '%s\n' "$out" | tail -1 | sed 's/^crumb verify: //' | cut -c1-70) |
| 103 | if [ $rc = 0 ] && [[ $out == *OK* ]]; then echo "OK|$out"; else echo "FAIL|$out"; fi |
| 104 | } |
| 105 | # Only main readiness is queried; candidate readiness may still run an older |
| 106 | # installed verifier until the operator installs the repaired worker. |
| 107 | heal_trusted() { |
| 108 | local head main |
| 109 | head=$(git -C "$TRUSTED" rev-parse HEAD 2>/dev/null); main=$(git -C "$TRUSTED" rev-parse origin/main 2>/dev/null) |
| 110 | [ "$HEAL" = 1 ] && [ -n "$main" ] && [ "$head" != "$main" ] || return 0 |
| 111 | if [ -n "$(git -C "$TRUSTED" status --porcelain --untracked-files=no)" ]; then |
| 112 | record FAIL HEAL "trusted clone" "work tree has local changes; not moved off ${head:0:8}" |
| 113 | else |
| 114 | g -C "$TRUSTED" checkout -q --detach origin/main && record INFO HEAL "trusted clone" "work tree moved ${head:0:8} -> origin/main ${main:0:8}" |
| 115 | fi |
| 116 | } |
| 117 | if [ "$HEAL_ONLY" = 1 ]; then heal_trusted; [ ${#FAILS[@]} = 0 ]; exit $?; fi |
| 118 | |
| 119 | SIBFILES=() |
| 120 | if [ "${AIEN_DOCTOR_NO_PROC:-}" != 1 ]; then |
| 121 | for p in /proc/[0-9]*; do |
| 122 | cmd=$(tr '\0' ' ' < "$p/cmdline" 2>/dev/null) || continue |
| 123 | [[ $cmd == *aien-test\ worker* ]] || continue |
| 124 | [[ $cmd =~ --siblings\ ([^ ]+) ]] && SIBFILES+=("${BASH_REMATCH[1]}|--siblings of pid ${p#/proc/}") |
| 125 | done |
| 126 | fi |
| 127 | |
| 128 | echo "crumb-doctor $(date -u +%FT%TZ): sandbox $SANDBOX $(sha8 "$SANDBOX"); tools from origin/main only" |
| 129 | |
| 130 | # SIBMAP |
| 131 | map_rows() { sed 's/#.*//' "$@" 2>/dev/null | awk 'NF==4 {print $1" "$2}' | sort -u; } |
| 132 | heal_trusted |
| 133 | main=$(git -C "$TRUSTED" rev-parse origin/main 2>/dev/null) |
| 134 | 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) |
| 135 | for entry in "$TRUSTED/tools/aien-local-ci/siblings|default (poll workers)" "${SIBFILES[@]}"; do |
| 136 | f=${entry%%|*}; who=${entry#*|} |
| 137 | missing=$(comm -23 <(printf '%s\n' "$want") <(map_rows "$f") | grep . | sed 's/ / ..\//' | paste -sd, -) |
| 138 | if [ -n "$missing" ]; then record FAIL SIBMAP "$f" "$who: missing $missing (origin/main ${main:0:8} has them)" |
| 139 | else record PASS SIBMAP "$f" "$who: matches origin/main ${main:0:8}"; fi |
| 140 | done |
| 141 | for repo in $REPOS; do |
| 142 | runsh=$(git -C "$CLONES/$repo" show origin/main:tests/gates/run.sh 2>/dev/null) |
| 143 | need=$( { printf '%s\n' "$runsh" | grep -oE 'need_sibling[[:blank:]]+[A-Za-z0-9._-]+' | awk '{print $2}' |
| 144 | printf '%s\n' "$runsh" | grep -E '^[[:blank:]]*pin[[:blank:]]+[^[:blank:]]+[[:blank:]]+[^[:blank:]]+[[:blank:]]+[^[:blank:]]+' | awk '{print $3}'; } | sort -u) |
| 145 | for d in $need; do |
| 146 | grep -qx "$repo $d" <<<"$want" || record FAIL SIBMAP "$repo needs ../$d" "origin/main sibling map has no line for it" |
| 147 | done |
| 148 | done |
| 149 | |
| 150 | # AGREE compares the trusted-main gate, hosted and readiness verdicts. For a |
| 151 | # PR only the trusted-main tool may inspect its crumbs; candidate metadata |
| 152 | # changes are reported as pending promotion, not treated as executable tools. |
| 153 | ready_check() { # ready_check <repo> <sha> <label> <main tree> <tool> <description> |
| 154 | [ "${AIEN_DOCTOR_NO_READY:-}" = 1 ] && return |
| 155 | command -v aien-local-ci >/dev/null || return |
| 156 | local want report got actual expected pin |
| 157 | want=$(verdict "$5" "$4"); [ "${want%%|*}" = OK ] && want=CURRENT || want=STALE |
| 158 | case $6 in |
| 159 | crumb.lock\ *) pin=$(tr -d '[:space:]' < "$4/crumb.lock" | tr 'A-F' 'a-f'); expected="trusted-base $pin" ;; |
| 160 | architecture\ origin/main\ tool) expected="trusted-base $(git -C "$ARCH" rev-parse origin/main)" ;; |
| 161 | sandbox\ *) expected="$SANDBOX (operator command; no lock at BASE)" ;; |
| 162 | *) record FAIL READY "$1 $3" "unknown trusted main tool $6"; return ;; |
| 163 | esac |
| 164 | report=$(aien-local-ci verify "$1" "$2" 2>/dev/null) |
| 165 | got=$(printf '%s\n' "$report" | sed -n 's/^Crumb status: //p' | head -1) |
| 166 | actual=$(printf '%s\n' "$report" | sed -n 's/^Crumb tool: //p' | head -1) |
| 167 | if [ "$got" = "$want" ] && [ "$actual" = "$expected" ]; then |
| 168 | record PASS READY "$1 $3" "aien-test verify Crumb status $got (trusted $actual)" |
| 169 | else |
| 170 | record FAIL READY "$1 $3" "verify status ${got:-none} tool ${actual:-none}; trusted status $want tool $expected" |
| 171 | fi |
| 172 | } |
| 173 | check_main() { # check_main <repo> |
| 174 | local repo=$1 clone=$CLONES/$1 w sha tree gt ht gv hv label |
| 175 | w=$(mktemp -d); sha=$(git -C "$clone" rev-parse --verify -q 'refs/remotes/origin/main^{commit}') |
| 176 | label="main ${sha:0:8}" |
| 177 | if [ -z "$sha" ] || ! git clone -q --shared --no-checkout "$clone" "$w/t" || ! g -C "$w/t" checkout -q --detach "$sha"; then |
| 178 | record FAIL AGREE "$repo $label" "cannot check out origin/main"; rm -rf "$w"; return |
| 179 | fi |
| 180 | tree=$w/t |
| 181 | if [ ! -e "$tree/.crumb" ]; then record PASS AGREE "$repo $label" "no crumbs"; rm -rf "$w"; return; fi |
| 182 | gt=$(gate_tool "$tree" "$repo"); ht=$(hosted_tool "$tree" "$repo") |
| 183 | # DRA-97 (2026-11-07) retired the crumb gates: a main with neither a crumb-verify step nor a |
| 184 | # hosted crumb step has nothing to agree on. One side alone retired is still a FAIL below. |
| 185 | if [ "$gt" = "|no crumb-verify step" ] && [[ $ht == "|no hosted workflow" || $ht == "|workflow has no crumb step" ]]; then |
| 186 | record PASS AGREE "$repo $label" "crumb gate retired (DRA-97): no crumb-verify step, ${ht#|}"; rm -rf "$w"; return |
| 187 | fi |
| 188 | if [ -z "${gt%%|*}" ]; then |
| 189 | record FAIL AGREE "$repo $label" "trusted gate tool unavailable (${gt#*|})"; rm -rf "$w"; return |
| 190 | fi |
| 191 | ready_check "$repo" "$sha" "$label" "$tree" "${gt%%|*}" "${gt#*|}" |
| 192 | gv=$(verdict "${gt%%|*}" "$tree"); hv=$(verdict "${ht%%|*}" "$tree") |
| 193 | if [ -z "${ht%%|*}" ] && [[ ${ht#*|} == workflow\ pin* || ${ht#*|} == architecture\ origin/main\ workflow* ]]; then |
| 194 | record FAIL AGREE "$repo $label" "trusted hosted tool unavailable (${ht#*|})" |
| 195 | elif [ "${hv%%|*}" = n/a ] || [ "${gv%%|*}" = "${hv%%|*}" ]; then |
| 196 | record PASS AGREE "$repo $label" "trusted gate[${gt#*|}]=${gv%%|*}; hosted[${ht#*|}]=${hv%%|*}" |
| 197 | else |
| 198 | record FAIL AGREE "$repo $label" "trusted gate[${gt#*|}]=${gv%%|*}; hosted[${ht#*|}]=${hv%%|*}" |
| 199 | fi |
| 200 | rm -rf "$w" |
| 201 | } |
| 202 | check_candidate() { # check_candidate <repo> <sha> <pr number>: inspect metadata only |
| 203 | local repo=$1 sha=$2 n=$3 clone=$CLONES/$1 changes |
| 204 | git -C "$clone" cat-file -e "$sha^{commit}" 2>/dev/null || git -C "$clone" fetch -q origin "pull/$n/head" 2>/dev/null |
| 205 | if ! git -C "$clone" cat-file -e "$sha^{commit}" 2>/dev/null; then |
| 206 | record FAIL PROMOTE "$repo #$n" "cannot inspect candidate $sha"; return |
| 207 | fi |
| 208 | changes=$(git -C "$clone" diff --name-only origin/main "$sha" -- crumb.lock tools/crumb \ |
| 209 | .github/workflows/aien-verification.yml tools/aien-local-ci/install.sh tests/gates/run.sh) |
| 210 | if [ -n "$changes" ]; then |
| 211 | record FAIL PROMOTE "$repo #$n" "operator promotion required for candidate tool/pin changes: $(printf '%s\n' "$changes" | paste -sd, -); no candidate tools executed" |
| 212 | else |
| 213 | record INFO PROMOTE "$repo #$n" "candidate metadata unchanged; no candidate tool verdict or execution" |
| 214 | fi |
| 215 | } |
| 216 | for repo in $REPOS; do |
| 217 | clone=$CLONES/$repo; [ -d "$clone/.git" ] || { record FAIL AGREE "$repo" "no trusted clone at $clone"; continue; } |
| 218 | git -C "$clone" fetch -q origin 2>/dev/null |
| 219 | check_main "$repo" |
| 220 | [ "$NOPRS" = 1 ] && continue |
| 221 | slug=$(git -C "$clone" remote get-url origin | sed -E 's#.*github\.com[:/]##; s#\.git$##') |
| 222 | prs=$(gh pr list -R "$slug" --state open --limit 100 --json number,headRefOid,title,files \ |
| 223 | --jq ".[] | select((.title|test(\"RFC-0003\")) or ([.files[]?.path] | any(test(\"$TOOL_RE\")))) | \"\(.number) \(.headRefOid)\"" 2>/dev/null) \ |
| 224 | || { record FAIL AGREE "$repo PR list" "gh pr list failed"; continue; } |
| 225 | while read -r n sha; do |
| 226 | [ -n "$n" ] || continue |
| 227 | check_candidate "$repo" "$sha" "$n" |
| 228 | done <<<"$prs" |
| 229 | done |
| 230 | |
| 231 | echo "crumb-doctor: $npass pass, ${#FAILS[@]} FAIL" |
| 232 | if [ "$ALERT" = 1 ]; then |
| 233 | mkdir -p "$WATCH"; stamp=$(date -u +%FT%TZ) |
| 234 | keep=$(grep -v '^CRUMB-DRIFT:' "$WATCH/ALERT" 2>/dev/null) |
| 235 | if [ ${#FAILS[@]} -gt 0 ]; then |
| 236 | text=$(printf '%s; ' "${FAILS[@]}" | cut -c1-600) |
| 237 | printf '%s CRUMB-DRIFT %s\n' "$stamp" "$text" >> "$WATCH/alerts.log" |
| 238 | keep=$(printf '%s\n%s' "$keep" "CRUMB-DRIFT: ${#FAILS[@]} crumb check(s) fail: $text(run: crumb-doctor; since $stamp)" | grep .) |
| 239 | fi |
| 240 | if [ -n "$keep" ]; then printf '%s\n' "$keep" > "$WATCH/ALERT.tmp" && mv -f "$WATCH/ALERT.tmp" "$WATCH/ALERT"; else rm -f "$WATCH/ALERT"; fi |
| 241 | fi |
| 242 | [ ${#FAILS[@]} = 0 ] |