Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/blob/main/tools/crumb/crumb-compile.sh # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-architecture-mv / tools / crumb / crumb-compile.sh` 135 lines ยท 8830 bytes Raw Blame History 1 #!/bin/sh 2 # crumb-compile: Crumb Compiler stage 1+2 reference implementation (RFC-0003). POSIX sh + git + jq + sha256sum. 3 # The repository compiles its own crumbs: hand-authored semantic kernel stays, everything structural is generated 4 # bottom-up into `.crumb` -> extensions.generated, with Merkle digests so one change invalidates only its ancestor path. 5 # 6 # DESIGN DECISIONS (the C port must keep every one of them; each closes a real trap): 7 # D1 source_tree excludes every .crumb and .crumb.local. Otherwise compiling changes the tree, which changes the 8 # digest, which requires recompiling: the compiler would never reach a fixed point. 9 # D2 Files are the TRACKED files of the working tree (git ls-files), hashed with git hash-object, not HEAD's tree: 10 # a PR author compiles before committing, and CI's checkout gives the same answer. Untracked files never count. 11 # D3 Digests cover content only: digest = sha256(semantic || source_tree || children_root || evidence_root). 12 # generated_at_commit and compiled_at are stamps, NOT inputs, and `verify` ignores them. Otherwise every commit 13 # would make every crumb "stale" and CI would fail forever. 14 # D4 Bottom-up order (deepest first) so a parent's children[] digests are the children's FINAL digests. 15 # D5 Semantic kernel = {purpose, layer, invariants, exports, related, boundaries} as hand-written (null-stripped, 16 # sorted keys). The compiler never writes these keys; `propose` writes only extensions.proposed.purpose. 17 # D6 last_commit per child uses pathspec ':!**/.crumb' so compile commits do not count as changes to the code. 18 # D7 Only directories that already hold a .crumb take part (crumb seed decides membership; this tool never creates 19 # directories' crumbs). Children = the NEAREST crumbed descendants: the walk descends through directories that 20 # hold no .crumb, so every crumb is reachable from the root digest (amended 2026-10-11, orphan crumbs); a deep 21 # child is named by its path relative to the parent crumb directory. 22 # D8 Evidence = tracked files under the directory whose path has a component named evidence or receipts, or that 23 # end in .receipt.json; evidence_root = sha256 of their blob ids in path order. 24 # D9 Output is written with jq -S (sorted keys) via tmp+rename, byte-stable, so `verify` can be an exact comparison. 25 # D10 (C tool only) /.crumbignore subtrees never take part in compile/verify/status; this shell reference 26 # does not implement D10, so the differential holds only for repositories without a .crumbignore. 27 set -eu 28 die() { echo "crumb-compile: $*" >&2; exit 1; } 29 need() { command -v "$1" >/dev/null 2>&1 || die "needs $1"; } 30 need git; need jq; need sha256sum 31 COMPILER="crumb-compile/1" 32 sha() { sha256sum | cut -c1-64; } 33 34 ROOT_ARG="${2:-.}" 35 TOP=$(git -C "$ROOT_ARG" rev-parse --show-toplevel 2>/dev/null) || die "not inside a git repository: $ROOT_ARG" 36 cd "$TOP" 37 HEAD=$(git rev-parse HEAD 2>/dev/null || echo "`0000000000000000000000000000000000000000`") 38 WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT 39 40 # One pass: "blob path" for every tracked file that exists, excluding crumbs (D1, D2). 41 git ls-files -z | tr '\0' '\n' | grep -v -e '/\.crumb$' -e '^\.crumb$' -e '/\.crumb\.local$' -e '^\.crumb\.local$' > "$WORK/paths" || true 42 : > "$WORK/blobs" 43 if [ -s "$WORK/paths" ]; then 44 while IFS= read -r p; do [ -f "$p" ] && printf '%s\n' "$p"; done < "$WORK/paths" > "$WORK/present" 45 if [ -s "$WORK/present" ]; then 46 git hash-object --stdin-paths < "$WORK/present" > "$WORK/hashes" 47 paste -d' ' "$WORK/hashes" "$WORK/present" | LC_ALL=C sort -k2 > "$WORK/blobs" 48 fi 49 fi 50 51 # Directories with a .crumb, deepest first (D4, D7). 52 git ls-files -z | tr '\0' '\n' | grep -e '/\.crumb$' -e '^\.crumb$' | sed 's|/\?\.crumb$||; s|^$|.|' \ 53 | awk '{ n=gsub("/","/"); print n, $0 }' | LC_ALL=C sort -k1,1nr -k2 | cut -d' ' -f2- > "$WORK/dirs" 54 # also include untracked .crumb files that exist on disk (freshly seeded, not yet committed) 55 find . -name .crumb -not -path './.git/*' | sed 's|^\./||; s|/\?\.crumb$||; s|^$|.|' >> "$WORK/dirs" 56 LC_ALL=C sort -u "$WORK/dirs" | awk '{ n=($0=="." ? -1 : gsub("/","/")); print n, $0 }' | LC_ALL=C sort -k1,1nr -k2 | cut -d' ' -f2- > "$WORK/dirs2" 57 mv "$WORK/dirs2" "$WORK/dirs" 58 59 mkdir -p "$WORK/digest" # digest/ -> digest of that dir after this run 60 enc() { if [ "$1" = "." ]; then echo _root; else printf "%s" "$1" | tr "/" "%"; fi; } 61 subtree() { # $1 dir -> lines "blob path" under it 62 if [ "$1" = "." ]; then cat "$WORK/blobs"; else awk -v d="$1/" 'index($2, d)==1' "$WORK/blobs"; fi 63 } 64 kids_of() { # $1 dir (repo-relative, "." = root) $2 name prefix -> lines "namedir": the NEAREST crumbed descendants (D7) 65 for k in $(ls -1A "$1" 2>/dev/null | LC_ALL=C sort); do 66 kd=$([ "$1" = "." ] && echo "$k" || echo "$1/$k") 67 [ -d "$kd" ] && [ ! -L "$kd" ] || continue 68 [ "$k" = ".git" ] && continue 69 if [ -f "$kd/.crumb" ]; then printf '%s\t%s\n' "$2$k" "$kd"; else kids_of "$kd" "$2$k/"; fi 70 done 71 } 72 compute() { # $1 dir -> prints the generated object (without stamps) as JSON 73 d="$1" 74 st=$(subtree "$d" | sha) 75 nfiles=$(subtree "$d" | wc -l | tr -d ' ') 76 ev=$(subtree "$d" | awk '$2 ~ /(^|\/)(evidence|receipts)\// || $2 ~ /\.receipt\.json$/') 77 evn=$(printf '%s' "$ev" | grep -c . || true) 78 evr=$(printf '%s' "$ev" | awk '{print $1}' | sha) 79 sem=$(jq -cS '{purpose, layer, invariants, exports, related, boundaries} | with_entries(select(.value != null))' "$d/.crumb" 2>/dev/null || echo '{}') 80 kids='[]' 81 kids_of "$d" "" | LC_ALL=C sort > "$WORK/kids.$$" 82 while IFS="$(printf '\t')" read -r k kd; do 83 kdig=$(cat "$WORK/digest/$(enc "$kd")" 2>/dev/null || jq -r '.extensions.generated.digest // "uncompiled"' "$kd/.crumb") 84 kpur=$(jq -r '(.purpose // "") | .[0:96]' "$kd/.crumb") 85 klc=$(git log -1 --format=%h HEAD -- "$kd" ':!**/.crumb' 2>/dev/null || echo "") 86 kids=$(printf '%s' "$kids" | jq -c --arg n "$k" --arg g "$kdig" --arg p "$kpur" --arg c "$klc" '. + [{name:$n,digest:$g,purpose:$p,last_commit:$c}]') 87 done < "$WORK/kids.$$" 88 cr=$(printf '%s' "$kids" | jq -r '.[].digest' | sha) 89 dig=$(printf '%s%s%s%s' "$sem" "$st" "$cr" "$evr" | sha) 90 printf '%s' "$dig" > "$WORK/digest/$(enc "$d")" 91 jq -cn --arg c "$COMPILER" --arg st "$st" --argjson nf "$nfiles" --argjson kids "$kids" --arg cr "$cr" \ 92 --argjson evn "$evn" --arg evr "$evr" --arg dig "$dig" \ 93 '{compiler:$c, source_tree:("sha256:"+$st), files:$nf, children:$kids, children_root:("sha256:"+$cr), 94 evidence_files:$evn, evidence_root:("sha256:"+$evr), digest:("sha256:"+$dig)}' 95 } 96 strip_stamps() { jq -cS "del(.generated_at_commit, .compiled_at)"; } 97 98 cmd=${1:-help} 99 changed=0; stale=0; total=0 100 case "$cmd" in 101 compile|verify|status) 102 while IFS= read -r d; do 103 [ -f "$d/.crumb" ] || continue 104 total=$((total+1)) 105 new=$(compute "$d" | jq -cS .) 106 old=$(jq -c '.extensions.generated // {}' "$d/.crumb" | strip_stamps) 107 if [ "$new" = "$old" ]; then 108 [ "$cmd" = status ] && echo "CURRENT $d" 109 continue 110 fi 111 stale=$((stale+1)) 112 case "$cmd" in 113 status) if [ "$old" = "{}" ]; then echo "UNCOMPILED $d"; else echo "STALE $d"; fi ;; 114 verify) echo "STALE $d" ;; 115 compile) 116 tmp="$d/.crumb.tmp.$$" 117 jq -S --argjson g "$new" --arg h "$HEAD" --arg t "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ 118 '.extensions = (.extensions // {}) | .extensions.generated = ($g + {generated_at_commit:$h, compiled_at:$t})' \ 119 "$d/.crumb" > "$tmp" && mv "$tmp" "$d/.crumb" 120 changed=$((changed+1)); echo "COMPILED $d" ;; 121 esac 122 done < "$WORK/dirs" 123 case "$cmd" in 124 compile) echo "crumb-compile: $total crumbs, $changed rewritten, HEAD ${HEAD%${HEAD#???????}}" ;; 125 verify) if [ "$stale" -gt 0 ]; then echo "crumb-compile: VERIFY FAIL: $stale of $total crumbs stale; run: crumb-compile compile"; exit 1; fi 126 echo "crumb-compile: VERIFY OK: $total crumbs current" ;; 127 status) echo "crumb-compile: $total crumbs, $stale not current" ;; 128 esac ;; 129 propose) # propose "": D5, never touches .purpose itself 130 d=${2:?dir}; p=${3:?purpose}; [ -f "$d/.crumb" ] || die "no .crumb in $d" 131 tmp="$d/.crumb.tmp.$$"; jq -S --arg p "$p" --arg by "${CRUMB_SESSION:-$USER}" --arg t "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ 132 '.extensions = (.extensions // {}) | .extensions.proposed = {purpose:$p, status:"PROPOSED", by:$by, at:$t}' "$d/.crumb" > "$tmp" && mv "$tmp" "$d/.crumb" 133 echo "PROPOSED purpose for $d (stays PROPOSED until a human moves it into .purpose)" ;; 134 *) echo "usage: crumb-compile.sh compile|verify|status [repo-path] | propose \"\""; echo "see header comments for the design decisions D1-D9"; exit 2 ;; 135 esac