interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / crumb / test.sh

339 lines · 38205 bytes Raw Blame History
1#!/bin/sh
2# crumb self-test. usage: sh test.sh ./crumb. Uses a scratch dir only.
3C=${1:-./crumb}; C=$(cd "$(dirname "$C")" && pwd)/$(basename "$C")
4HERE=$(cd "$(dirname "$0")" && pwd)
5T=$(mktemp -d); trap 'rm -rf "$T"' EXIT
6fail=0; ok() { echo "PASS $1"; }; bad() { echo "FAIL $1"; fail=1; }
7mkdir -p "$T/r/src/deep" "$T/r/docs" "$T/r/old"; cd "$T/r" || exit 1
8git init -q . 2>/dev/null; echo 'int main(void){return 0;}' > src/a.c; echo x > docs/n.md; echo y > src/deep/b.h
9echo '{"version":1,"dir_path":"x","history":[]}' > old/.crumb; echo z > old/f.c
10
11"$C" seed . >/dev/null; "$C" seed . | grep -q 'created 0, updated 0' && ok "seed idempotent" || bad "seed idempotent"
12grep -q '^.crumb.local$' .gitignore && ok "gitignore has .crumb.local" || bad "gitignore"
13grep -q 'version' old/.crumb && ! grep -q schema_version old/.crumb && ok "legacy .crumb untouched" || bad "legacy"
14"$C" validate . | grep -q 'INVALID' && bad "validate" || ok "validate"
15sed -i 's|"purpose": "Directory src. Purpose not yet described by a human."|"purpose": "Hand written C sources", "invariants": ["no globals"]|' src/.crumb
16"$C" seed . >/dev/null; grep -q 'Hand written C sources' src/.crumb && grep -q 'no globals' src/.crumb && ok "hand edits survive reseed" || bad "hand edits"
17echo w > src/new.c; "$C" seed . | grep -q 'updated 1' && grep -q new.c src/.crumb && ok "reseed picks up new file" || bad "reseed update"
18
19"$C" claim laneA src src/a.c "refactor a" 600 | grep -q CLAIMED && ok claim || bad claim
20"$C" claim laneB src src/a.c "also a" >/dev/null; [ $? -eq 2 ] && ok "second claim blocked (exit 2)" || bad "block"
21"$C" claim laneB src src/b.c "other file" | grep -q CLAIMED && ok "claim different target" || bad "claim2"
22"$C" update laneA src "still on a.c" | grep -q SCENT && ok update || bad update
23"$C" whisper laneA src laneB "a.c done soon" high src/a.c | grep -q WHISPER && ok whisper || bad whisper
24"$C" sniff laneB src | grep -q 'whisper laneA -> laneB' && ok "sniff sees whisper" || bad sniff
25"$C" sniff laneB src | grep -q 'invariant: no globals' && ok "sniff shows invariants" || bad "sniff inv"
26"$C" list . | grep -q 'lock .*src/a.c' && ok list || bad list
27"$C" close laneB src src/a.c >/dev/null; [ $? -eq 2 ] && ok "close by non-holder refused" || bad "close refuse"
28"$C" close laneA src src/a.c modify "done, ready" | grep -q CLOSED && ok close || bad close
29"$C" list . | grep -q 'src/a.c' && bad "lock released" || ok "lock released"
30grep -q '"history"' .git/crumb/v1/dirs/src.json && grep -q '"action": "modify"' .git/crumb/v1/dirs/src.json && [ ! -e src/.crumb.local ] && ok "history vector written" || bad history
31# expiry sweep
32"$C" claim laneC src src/c.c "short" 1 >/dev/null; sleep 2; "$C" claim laneD src src/c.c "takes over" | grep -q CLAIMED && ok "expired lock swept" || bad sweep
33echo '{broken' > docs/.crumb.local; "$C" sniff x docs >/dev/null 2>&1; [ $? -ne 0 ] && ok "bad local refused" || bad "bad local"
34
35# ---- RFC-0002 common coordination plane: two real linked worktrees ----
36cd "$T" || exit 1
37mkdir rc && cd rc && git init -q -b main . && mkdir src && echo a > src/runtime.c && echo b > src/other.c && echo c > src/legacy.c
38git add -A && git -c user.name=t -c user.email=t@t commit -qm init
39git worktree add -q -b A "$T/wa" && git worktree add -q -b B "$T/wb"
40STORE=$(cd "$T/rc" && cd "$(git rev-parse --git-common-dir)" && pwd)/crumb/v1
41# T1 cross-worktree visibility
42(cd "$T/wa" && "$C" claim agent-a src runtime.c "rewrite runtime" | grep -q CLAIMED) && ok "T1 claim in worktree A" || bad "T1 claim A"
43out=$(cd "$T/wb" && "$C" sniff agent-b src)
44echo "$out" | grep -q 'shared coordination: ACTIVE' && echo "$out" | grep -q "held by agent-a \[A\]" && echo "$out" | grep -q 'src/runtime.c' && ok "T1 B sniff sees A's claim (branch A)" || bad "T1 visibility"
45echo "$out" | grep -q 'CONFLICT: src/runtime.c' && ok "T1 sniff OTHER WORKTREES reports conflict" || bad "T1 other worktrees"
46[ -f "$STORE/dirs/src.json" ] && [ ! -e "$T/wa/src/.crumb.local" ] && ok "T1 state lives in common store, no .crumb.local written" || bad "T1 store"
47# T2 conflict, deterministic
48(cd "$T/wb" && "$C" claim agent-b src runtime.c "also" >/dev/null); [ $? -eq 2 ] && ok "T2 B claim refused (exit 2)" || bad "T2 refuse"
49(cd "$T/wb" && "$C" claim agent-b . src/runtime.c "also, repo-relative form" >/dev/null); [ $? -eq 2 ] && ok "T2 refused via repo-relative form" || bad "T2 refuse2"
50(cd "$T/wa" && "$C" whisper agent-a src agent-b "runtime half done" high | grep -q WHISPER) && (cd "$T/wb" && "$C" sniff agent-b src | grep -q 'whisper agent-a -> agent-b') && ok "T2 whisper crosses worktrees" || bad "T2 whisper"
51# T3 release
52(cd "$T/wb" && "$C" close agent-b src runtime.c >/dev/null); [ $? -eq 2 ] && ok "T3 non-holder close refused" || bad "T3 nonholder"
53(cd "$T/wa" && "$C" close agent-a src runtime.c modify "done" | grep -q CLOSED) && ok "T3 A closes" || bad "T3 close"
54(cd "$T/wb" && "$C" claim agent-b src runtime.c "my turn" | grep -q CLAIMED) && ok "T3 B claim succeeds after release" || bad "T3 claim"
55(cd "$T/wb" && "$C" close agent-b src runtime.c >/dev/null)
56# T4 independent targets
57(cd "$T/wa" && "$C" claim agent-a src one.c "one" >/dev/null) && (cd "$T/wb" && "$C" claim agent-b src two.c "two" >/dev/null) && \
58 out=$(cd "$T/wa" && "$C" sniff agent-a src) && echo "$out" | grep -q 'src/one.c' && echo "$out" | grep -q 'src/two.c' && ok "T4 independent targets both survive" || bad "T4 independent"
59(cd "$T/wa" && "$C" close agent-a src one.c >/dev/null); (cd "$T/wb" && "$C" close agent-b src two.c >/dev/null)
60# T5 concurrent mutation: 8 different targets, then 8 agents on one target
61i=1; while [ $i -le 8 ]; do ( cd "$T/w$( [ $((i%2)) -eq 0 ] && echo b || echo a )" && "$C" claim "cagent$i" src "c$i.c" "par" >/dev/null ) & i=$((i+1)); done; wait
62n=$(cd "$T/wa" && "$C" sniff x src | grep -c '^ lock .*src/c[1-8].c')
63[ "$n" -eq 8 ] && ok "T5 8 concurrent claims on 8 targets: 8 locks survive" || bad "T5 lost updates ($n of 8)"
64i=1; while [ $i -le 8 ]; do ( cd "$T/w$( [ $((i%2)) -eq 0 ] && echo b || echo a )" && "$C" claim "sagent$i" src same.c "race" >/dev/null; echo $? > "$T/rc$i" ) & i=$((i+1)); done; wait
65w=$(cat "$T"/rc[1-8] | grep -c '^0$'); b=$(cat "$T"/rc[1-8] | grep -c '^2$')
66[ "$w" -eq 1 ] && [ "$b" -eq 7 ] && ok "T5 8 racers on one target: exactly 1 wins, 7 exit 2" || bad "T5 race (wins=$w blocked=$b)"
67nl=$(cd "$T/wa" && "$C" sniff x src | grep -c '^ lock .*src/same.c'); [ "$nl" -eq 1 ] && ok "T5 exactly one lock record for raced target" || bad "T5 lock count"
68# T6 TTL / crash recovery: holder never closes
69(cd "$T/wa" && "$C" claim dead-agent src ttl.c "crashes" 1 >/dev/null); sleep 2
70(cd "$T/wb" && "$C" claim live-agent src ttl.c "takes over" | grep -q CLAIMED) && ok "T6 expired lock pruned, other worktree claims" || bad "T6 ttl"
71(cd "$T/wb" && "$C" sniff x src | grep -q 'lock .*ttl.c.*held by dead-agent') && bad "T6 dead lock still listed" || ok "T6 dead agent's lock gone from sniff"
72# T7 path normalization: two absolute worktree paths of the same file are one lock
73(cd "$T/wa" && "$C" claim agent-a . "$T/wa/src/abs.c" "abs" | grep -q 'CLAIMED src/abs.c') && ok "T7 absolute path in A normalised to src/abs.c" || bad "T7 abs A"
74(cd "$T/wb" && "$C" claim agent-b src "$T/wb/src/abs.c" "abs b" >/dev/null); [ $? -eq 2 ] && ok "T7 same file via B's absolute path is the same lock" || bad "T7 abs B"
75(cd "$T/wb" && "$C" claim agent-b src ../src/./abs.c "dots" >/dev/null); [ $? -eq 2 ] && ok "T7 ../ and ./ collapse to the same lock" || bad "T7 dots"
76(cd "$T/wb" && "$C" claim agent-b . "$T/wa/elsewhere.c" "x" >/dev/null 2>&1); [ $? -eq 1 ] && ok "T7 path outside this worktree refused" || bad "T7 outside"
77# agent identity is never a PID alone
78grep -q '"session": "agent-a@' "$STORE/agents/agent-a.json" && ok "agent record carries session id (agent@host), not a PID" || bad "agent session id"
79(cd "$T/wa" && CRUMB_SESSION=sess-123 "$C" update agent-a src "pinned session" >/dev/null) && grep -q '"session": "sess-123"' "$STORE/agents/agent-a.json" && ok "CRUMB_SESSION overrides session id" || bad "CRUMB_SESSION"
80# T8 legacy .crumb.local: read, import once with provenance, never overridden, file kept
81NOW=$(date -u +%Y-%m-%dT%H:%M:%SZ)
82(cd "$T/wb" && "$C" claim agent-new src legacy2.c "fresh shared" >/dev/null)
83cat > "$T/wa/src/.crumb.local" <<JSON
84{"schema_version":"1.0.0","directory":"x","active_scents":{"old-agent":{"focus":"legacy work","updated_at":"$NOW","ttl_seconds":600}},
85 "locks":{"src/legacy.c":{"holder":"old-agent","intent":"legacy lock","acquired_at":"$NOW","ttl_seconds":600},
86 "src/legacy2.c":{"holder":"old-agent","intent":"stale view","acquired_at":"$NOW","ttl_seconds":600}},
87 "whispers":[{"from":"old-agent","message":"legacy note","timestamp":"$NOW"}]}
88JSON
89(cd "$T/wa" && "$C" sniff agent-a src | grep -q 'held by old-agent.*imported from') && ok "T8 sniff reads legacy .crumb.local (in memory)" || bad "T8 legacy read"
90[ ! -e "$STORE/agents/old-agent.json" ] && ok "T8 sniff does not write the store" || bad "T8 sniff wrote"
91(cd "$T/wa" && "$C" update agent-a src "trigger import" >/dev/null)
92out=$(cd "$T/wb" && "$C" sniff agent-b src)
93echo "$out" | grep -q "src/legacy.c.*held by old-agent.*imported from $T/wa" && ok "T8 legacy lock imported with provenance (worktree path)" || bad "T8 import"
94echo "$out" | grep -q 'src/legacy2.c.*held by agent-new' && ok "T8 legacy never overrides newer shared lock" || bad "T8 override"
95echo "$out" | grep -q 'whisper old-agent' && ok "T8 legacy whisper imported" || bad "T8 whisper"
96(cd "$T/wb" && "$C" claim agent-b src legacy.c "x" >/dev/null); [ $? -eq 2 ] && ok "T8 imported lock blocks other worktree" || bad "T8 block"
97[ -f "$T/wa/src/.crumb.local" ] && ok "T8 legacy file left in place" || bad "T8 file kept"
98n1=$(grep -c old-agent "$STORE/dirs/src.json"); (cd "$T/wa" && "$C" update agent-a src "again" >/dev/null); n2=$(grep -c old-agent "$STORE/dirs/src.json")
99[ "$n1" -eq "$n2" ] && ok "T8 import is one-time (no duplicates)" || bad "T8 duplicates"
100# outside any git repo: legacy behaviour kept, sniff says so
101mkdir -p "$T/plain" && "$C" claim p1 "$T/plain" x.c "plain" | grep -q CLAIMED && [ -f "$T/plain/.crumb.local" ] && "$C" sniff p1 "$T/plain" | grep -q 'shared coordination: none (not a git repository)' && ok "non-git dir keeps .crumb.local, sniff reports none" || bad "non-git"
102cd "$T/r" || exit 1
103# backfill: known commit with a PR number lands in the right dir with the right sha; second run changes nothing
104git add -A >/dev/null 2>&1; git -c user.name=t -c user.email=t@t commit -qm "feat: add b (#7)" >/dev/null 2>&1
105sha=$(git rev-parse --short=7 HEAD)
106HOME="$T/home" "$C" backfill . >/dev/null
107grep -q '"pr": 7' src/.crumb && grep -q "\"sha\": \"$sha\"" src/.crumb && grep -q backfill-git src/.crumb && ok "backfill: PR 7 + sha in src/.crumb" || bad "backfill entry"
108grep -q "$sha #7" docs/crumbs/BACKFILL.md && ok "BACKFILL.md lists commit" || bad "BACKFILL.md"
109HOME="$T/home" "$C" backfill . | grep -q 'updated 0' && HOME="$T/home" "$C" backfill . | grep -q 'BACKFILL.md: unchanged' && ok "backfill idempotent" || bad "backfill idempotent"
110# ---- RFC-0002 section 7: crumb explain ----
111cd "$T/r" || exit 1
112ex=$("$C" explain src)
113for h in 'NEAREST CRUMB' 'INHERITED INVARIANTS' 'SHARED COORDINATION' 'RECENT COMMITS' 'EVIDENCE REFERENCES' 'NEWEST CONTINUATION'; do
114 echo "$ex" | grep -q "^== $h ==" && ok "explain prints section: $h" || bad "explain section: $h"
115done
116echo "$ex" | grep -q 'purpose: Hand written C sources' && echo "$ex" | grep -q 'invariant: no globals' && ok "explain shows nearest .crumb purpose + invariants" || bad "explain nearest"
117echo "$ex" | grep -q 'feat: add b (#7)' && ok "explain lists commits touching the path" || bad "explain commits"
118"$C" explain src/a.c | grep -q 'repository path: src/a.c' && ok "explain accepts a file" || bad "explain file"
119# The path is tracked repository data, not a shell command. A quoted Git argument
120# must preserve the apostrophe and metacharacters without touching the marker.
121evil="src/quote'; touch EXPLAIN_INJECTED; echo '"
122printf 'test\n' > "$evil"
123git add -- "$evil" && git -c user.name=t -c user.email=t@t commit -qm "explain hostile pathname"
124ex=$("$C" explain "$evil")
125echo "$ex" | grep -q 'explain hostile pathname' && [ ! -e EXPLAIN_INJECTED ] && ok "explain quotes hostile tracked pathname" || bad "explain shell injection or missing commit"
126plain=$("$C" explain src/a.c)
127echo "$plain" | grep -q 'repository path: src/a.c' && [ ! -e EXPLAIN_INJECTED ] && ok "explain ordinary pathname control" || bad "explain ordinary pathname control"
128echo "$ex" | grep -q '(none touches this path)' && ok "explain: no continuation yet" || bad "explain none"
129echo '{"version":1,"dir_path":"x","history":[]}' > /dev/null
130"$C" claim agent-x src a.c "explain test" >/dev/null; "$C" whisper agent-x src - "explain whisper" >/dev/null
131ex=$("$C" explain src); echo "$ex" | grep -q 'held by agent-x' && echo "$ex" | grep -q 'whisper agent-x -> all: explain whisper' && ok "explain shows shared locks + whispers" || bad "explain coordination"
132STORE=$(git rev-parse --absolute-git-dir)/crumb/v1; mkdir -p "$STORE/continuations"
133cat > "$STORE/continuations/cc-9001-test.json" <<JSON
134{"acr_version":1,"identity":{"checkpoint_id":"cc-9001-test","created_by":"tester","sealed_at":"2026-10-04T10:00:00Z","reason":"explain test"},
135 "state":{"items":[{"tag":"PROVEN","claim":"src parser works","evidence":["commit:abc1234"]}]},
136 "evidence":{"commits":["abc1234"],"prs":["repo#7"],"receipts":[],"tests":[{"name":"parser","result":"PASS"}]},
137 "coordination":{"owned_paths":["src/"],"scope":[],"do_not_touch":[]},"revalidation":{"status":"UNVERIFIED"}}
138JSON
139cat > "$STORE/continuations/cc-9002-other.json" <<JSON
140{"identity":{"checkpoint_id":"cc-9002-other","sealed_at":"2026-10-04T11:00:00Z"},"state":{"items":[{"tag":"OPEN","claim":"docs thing"}]},"coordination":{"scope":["docs/.crumb"]}}
141JSON
142ex=$("$C" explain src)
143echo "$ex" | grep -q 'id: cc-9001-test' && echo "$ex" | grep -q '\[PROVEN\] src parser works' && echo "$ex" | grep -q 'continuation test: parser -> PASS' && ok "explain surfaces continuation whose owned_paths match" || bad "explain continuation"
144echo "$ex" | grep -q 'cc-9002-other' && bad "explain ignores continuation about another path" || ok "explain ignores continuation about another path"
145"$C" explain docs | grep -q 'id: cc-9002-test\|id: cc-9002-other' && ok "explain matches via coordination.scope" || bad "explain scope"
146"$C" explain "$T/nonexistent" >/dev/null 2>&1; [ $? -ne 0 ] && ok "explain on missing path fails" || bad "explain missing"
147# ---- RFC-0003: Crumb Compiler (compile / verify / status / propose / context) ----
148printf 'abc' | "$C" sha256 | grep -q '^ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad$' && ok "C3 sha256 known vector (abc)" || bad "C3 sha256 vector"
149head -c 300000 /dev/urandom > "$T/rand.bin"
150[ "$("$C" sha256 < "$T/rand.bin")" = "$(sha256sum < "$T/rand.bin" | cut -c1-64)" ] && ok "C3 sha256 matches sha256sum on 300 KB" || bad "C3 sha256 vs sha256sum"
151CC3="$T/cc3"; mkdir -p "$CC3/src/deep" "$CC3/docs" "$CC3/evidence" && cd "$CC3" || exit 1
152git init -q -b main . && echo 'int a;' > src/a.c && echo 'int b;' > src/deep/b.c && echo n > docs/n.md && echo r > evidence/r.json && echo top > README
153"$C" seed . >/dev/null; git add -A && git -c user.name=t -c user.email=t@t commit -qm init
154"$C" status . | grep -q 'UNCOMPILED' && ok "C3 status: fresh crumbs are UNCOMPILED" || bad "C3 status uncompiled"
155"$C" verify . >/dev/null; [ $? -eq 1 ] && ok "C3 verify exits 1 on uncompiled crumbs" || bad "C3 verify uncompiled"
156"$C" compile . | grep -q 'rewritten' && ok "C3 compile runs" || bad "C3 compile"
157git add -A && git -c user.name=t -c user.email=t@t commit -qm compiled
158"$C" compile . | grep -q ', 0 rewritten' && ok "C3 fixed point: second compile rewrites 0" || bad "C3 fixed point"
159"$C" verify . | grep -q 'OK' && ok "C3 verify OK after compile" || bad "C3 verify OK"
160git -c user.name=t -c user.email=t@t commit -q --allow-empty -m "empty" && "$C" verify . >/dev/null && ok "C3 empty commit keeps verify OK (stamps are not inputs)" || bad "C3 empty commit"
161echo 'int b2;' > src/deep/b.c; git add -A
162out=$("$C" verify .); st=$(echo "$out" | grep '^STALE' | awk '{print $2}' | sort | tr '\n' ' ')
163[ "$st" = ". src src/deep " ] && ok "C3 touching src/deep/b.c: STALE is exactly src/deep, src and the root (ancestors only)" || bad "C3 ancestor-only staleness (got: $st)"
164echo "$out" | grep -q 'docs\|evidence' && bad "C3 sibling dirs stay current" || ok "C3 sibling dirs stay current"
165"$C" compile . >/dev/null; "$C" verify . >/dev/null && ok "C3 compile after the change: verify OK again" || bad "C3 recompile"
166git add -A; git -c user.name=t -c user.email=t@t commit -qm "change + compile"; "$C" verify . >/dev/null && ok "C3 verify stays OK after the compiled change is committed (last_commit is not an input, D3a)" || bad "C3 D3a last_commit"
167before=$(jq -r .purpose docs/.crumb); sem0=$(jq -cS '{purpose,layer,invariants,exports,related,boundaries}' src/.crumb)
168"$C" propose docs "Design notes and decision records" | grep -q PROPOSED && ok "C3 propose prints PROPOSED" || bad "C3 propose"
169[ "$(jq -r .purpose docs/.crumb)" = "$before" ] && [ "$(jq -r .extensions.proposed.purpose docs/.crumb)" = "Design notes and decision records" ] && [ "$(jq -r .extensions.proposed.status docs/.crumb)" = PROPOSED ] && ok "C3 propose never changes .purpose, writes extensions.proposed" || bad "C3 propose purpose"
170[ "$sem0" = "$(jq -cS '{purpose,layer,invariants,exports,related,boundaries}' src/.crumb)" ] && ok "C3 compile never touches the semantic kernel" || bad "C3 kernel"
171ctx=$("$C" context src/deep/b.c)
172for h in 'TREE' 'YOUR TASK TOUCHES' 'READ THESE CRUMBS' 'CURRENT CONFLICTS' 'RELEVANT CONTINUATION'; do
173 echo "$ctx" | grep -q "^== $h" && ok "C3 context prints section: $h" || bad "C3 context section: $h"
174done
175"$C" verify . >/dev/null && ok "C3 propose does not stale crumbs (proposal is not a digest input)" || bad "C3 propose stale"
176echo x >> src/a.c; git add -A; "$C" context src/a.c | grep -q "^Crumbs STALE: 2 .*run crumb compile" && ok "C3 context freshness: STALE: n (run crumb compile)" || bad "C3 context stale line"
177"$C" compile . >/dev/null; "$C" context src | grep -q '^Crumbs CURRENT at HEAD' && ok "C3 context freshness: CURRENT at HEAD <sha>" || bad "C3 context current line"
178"$C" context src/deep/b.c | grep -q 'src/.crumb' && "$C" context src/deep/b.c | grep -q 'src/deep/.crumb' && ok "C3 context recommends the path's ancestor crumbs" || bad "C3 context ancestors"
179"$C" context . | grep -q 'PROPOSED: Design notes' && ok "C3 context shows PROPOSED purpose for a child without .purpose" || bad "C3 context proposed"
180"$C" claim lane-x src src/a.c "ctx test" >/dev/null; "$C" context src/a.c | grep -q 'held by lane-x' && ok "C3 context lists a shared-store lock on the path" || bad "C3 context conflict"
181"$C" context docs | grep -q 'held by lane-x' && bad "C3 context ignores locks elsewhere" || ok "C3 context ignores locks elsewhere"
182"$C" close lane-x src src/a.c >/dev/null
183[ "$("$C" context src | wc -l)" -lt 80 ] && ok "C3 context output is bounded" || bad "C3 context bounded"
184# orphan crumbs (lane CRUMB proof 19 b2, 2026-10-11): a .crumb under a directory that has NO .crumb is a child of the
185# nearest crumbed ancestor (D7 amended), so deleting it is visible to verify (pr-gate.sh also saw it until the
186# DRA-97 retirement, 2026-11-07; it no longer runs crumb verify)
187"$C" compile . >/dev/null; git add -A; git -c user.name=t -c user.email=t@t commit -qm "pre-orphan"
188mkdir -p src/mid/leaf && echo 'int l;' > src/mid/leaf/l.c && "$C" create src/mid/leaf leaf "leaf under an uncrumbed directory" code >/dev/null
189git add -A; st=$("$C" verify . | grep '^STALE' | awk '{print $2}' | sort | tr '\n' ' ')
190[ "$st" = ". src src/mid/leaf " ] && ok "C3 D7 new crumb under an uncrumbed dir stales itself, its nearest crumbed ancestor and the root" || bad "C3 D7 orphan stale set (got: $st)"
191"$C" compile . >/dev/null; git add -A; git -c user.name=t -c user.email=t@t commit -qm "orphan compiled"
192"$C" verify . >/dev/null && ok "C3 D7 verify OK after compiling the deep crumb" || bad "C3 D7 verify after deep crumb"
193"$C" compile . | grep -q ', 0 rewritten' && ok "C3 D7 fixed point holds with a deep child" || bad "C3 D7 fixed point deep child"
194jq -e '.extensions.generated.children | map(.name) | index("mid/leaf") != null' src/.crumb >/dev/null && ok "C3 D7 deep crumb is listed as child \"mid/leaf\" of src (path relative to the parent crumb)" || bad "C3 D7 deep child not listed"
195jq -e '.extensions.generated.children | map(.name) | index("mid") == null' src/.crumb >/dev/null && ok "C3 D7 the uncrumbed directory itself is not a child" || bad "C3 D7 uncrumbed dir listed"
196git rm -q src/mid/leaf/.crumb; st=$("$C" verify . | grep '^STALE' | awk '{print $2}' | sort | tr '\n' ' ')
197[ "$st" = ". src " ] && ok "C3 D7 deleting the deep crumb stales src and the root (orphan deletion is visible)" || bad "C3 D7 orphan deletion invisible (got: $st)"
198"$C" verify . >/dev/null; [ $? -eq 1 ] && ok "C3 D7 verify exits 1 after the orphan deletion" || bad "C3 D7 verify exit after orphan deletion"
199"$C" compile . >/dev/null; "$C" verify . >/dev/null && ok "C3 D7 recompile after the deletion returns to OK" || bad "C3 D7 recompile after deletion"
200jq -e '.extensions.generated.children | map(.name) | index("mid/leaf") == null' src/.crumb >/dev/null && ok "C3 D7 deleted deep crumb is gone from src children" || bad "C3 D7 deleted deep child still listed"
201git add -A; git -c user.name=t -c user.email=t@t commit -qm "orphan removed"
202# D10: .crumbignore subtrees never participate in compile/verify/status (omega evidence/, physics nvrm, m16)
203"$C" compile . >/dev/null; git add -A; git -c user.name=t -c user.email=t@t commit -qm "pre-ignore"
204ev0=$(cat src/deep/.crumb); printf '# guarded tree\nsrc/deep/\n' > .crumbignore
205st=$("$C" verify . | grep '^STALE' | awk '{print $2}' | sort | tr '\n' ' ')
206[ "$st" = ". src " ] && ok "C3 D10 adding .crumbignore stales only the parent and its ancestors (children list changed)" || bad "C3 D10 ignore stale set (got: $st)"
207"$C" compile . >/dev/null
208[ "$(cat src/deep/.crumb)" = "$ev0" ] && ok "C3 D10 compile never rewrites a .crumb under an ignored subtree" || bad "C3 D10 ignored crumb rewritten"
209jq -e '.extensions.generated.children | map(.name) | index("deep") == null' src/.crumb >/dev/null && ok "C3 D10 ignored directory is not a child of its parent" || bad "C3 D10 ignored child listed"
210"$C" status . | grep -q 'src/deep' && bad "C3 D10 status lists ignored dir" || ok "C3 D10 status does not list the ignored directory"
211echo 'int b3;' > src/deep/b.c; git add -A; st=$("$C" verify . | grep '^STALE' | awk '{print $2}' | sort | tr '\n' ' ')
212[ "$st" = ". src " ] && ok "C3 D10 a file under the ignored subtree still stales the enclosing crumbs, never the ignored one" || bad "C3 D10 enclosing stale (got: $st)"
213"$C" compile . >/dev/null; [ "$(cat src/deep/.crumb)" = "$ev0" ] && ok "C3 D10 ignored crumb still untouched after that compile" || bad "C3 D10 ignored crumb touched"
214rm .crumbignore; "$C" compile . >/dev/null; "$C" verify . >/dev/null && ok "C3 D10 removing .crumbignore and recompiling returns to OK" || bad "C3 D10 remove ignore"
215git add -A; git -c user.name=t -c user.email=t@t commit -qm "post-ignore"
216# sovereign-core#355: generated build directories never take part in compile/verify/status (one shared policy)
217SAVE=$(pwd); cd "$T" || exit 1; rm -rf g355; mkdir g355 && cd g355 && git init -q -b main .
218mkdir -p src target/debug/build/x/out/auth node_modules/pkg out/cache keep/target
219echo 'int main(void){return 0;}' > src/a.c; echo 'fn x(){}' > target/debug/build/x/out/auth/a.rs; echo 'y' > node_modules/pkg/i.js; echo 'z' > out/cache/o.c; echo 's' > keep/target/real.c
220for d in . src target/debug/build/x/out/auth node_modules/pkg out/cache keep/target; do printf '{"schema_version":1,"purpose":"p %s"}\n' "$d" > "$d/.crumb"; done
221printf 'out/\n' > .gitignore
222git add .gitignore .crumb src keep; git -c user.name=t -c user.email=t@t commit -qm init # target/ and node_modules/ stay untracked and NOT gitignored: the built-in names must suffice
223tgt0=$(sha256sum target/debug/build/x/out/auth/.crumb node_modules/pkg/.crumb out/cache/.crumb)
224"$C" compile . >/dev/null
225[ "$(sha256sum target/debug/build/x/out/auth/.crumb node_modules/pkg/.crumb out/cache/.crumb)" = "$tgt0" ] && ok "355 .crumb under target/, node_modules/ and a gitignored dir are byte-identical after compile" || bad "355 build-dir crumbs rewritten"
226jq -e '.extensions.generated' src/.crumb >/dev/null && jq -e '.extensions.generated' .crumb >/dev/null && ok "355 source crumbs are still compiled" || bad "355 source crumb not compiled"
227jq -e '.extensions.generated' keep/target/.crumb >/dev/null && ok "355 a directory named target that holds tracked files is real source and is still compiled" || bad "355 tracked target skipped"
228"$C" status . | grep -Eq 'target/debug|node_modules|out/cache' && bad "355 status lists a build dir" || ok "355 status ignores the build dirs"
229"$C" verify . | grep -Eq 'target/debug|node_modules|out/cache' && bad "355 verify lists a build dir" || ok "355 verify ignores the build dirs"
230"$C" status . | grep -q 'keep/target' && ok "355 status still lists the tracked target dir" || bad "355 status lost tracked target"
231jq -e '[.extensions.generated.children[].name] | (index("node_modules") == null and index("out") == null and index("target") == null)' .crumb >/dev/null && ok "355 build dirs are not children of the root crumb" || bad "355 build dir listed as child"
232git add .gitignore .crumb src keep; git -c user.name=t -c user.email=t@t commit -qm compiled
233"$C" verify . >/dev/null && ok "355 verify OK after compile" || bad "355 verify after compile"
234echo 'int q;' >> target/debug/build/x/out/auth/a.rs; echo 'more' > out/cache/more.c
235"$C" verify . >/dev/null && ok "355 changing files inside build dirs does not stale anything" || bad "355 build-dir change staled crumbs"
236cd "$SAVE" || exit 1
237# ---- RFC-0003 amendment 1 (2026-10-18): generated data derived on demand, never committed ----
238# The regression this guards: two PRs touching unrelated files both rewrite the root crumb's roll-up lines
239# (source_tree, children_root, digest, stamps), so after the first squash-merges the second CONFLICTS on GitHub.
240SAVE=$(pwd); AM="$T/am1"; rm -rf "$AM"; mkdir -p "$AM/src/deep" "$AM/docs" "$AM/evidence" && cd "$AM" || exit 1
241gc() { git -c user.name=t -c user.email=t@t -c commit.gpgsign=false "$@"; }
242git init -q -b main . && echo 'int a;' > src/a.c && echo 'int b;' > src/deep/b.c && echo n > docs/n.md && echo r > evidence/r.json && echo top > README
243"$C" seed . >/dev/null; git add -A; "$C" compile . >/dev/null; git add -A; gc commit -qm "legacy compiled"
244[ "$("$C" verify .)" = "crumb verify: OK: 4 crumbs current" ] && ok "AM1 legacy repo (no root marker): verify output unchanged" || bad "AM1 legacy verify output changed"
245twoprs() { # twoprs <tag>: PR A edits src/a.c, PR B edits docs/n.md, each compiled per protocol; A squash-lands; prints B's conflicts
246 for b in A B; do gc checkout -q -b "$1$b" main; [ $b = A ] && echo "/* $1 */" >> src/a.c || echo "$1" >> docs/n.md
247 gc commit -qam "$1$b edit"; "$C" compile . >/dev/null; [ -n "$(git status --porcelain)" ] && { git add -A; gc commit -qm "crumb compile"; }; gc checkout -q main; done
248 gc merge -q --squash "$1A" >/dev/null 2>&1; gc commit -qm "squash $1A"
249 git merge-tree --write-tree --name-only main "$1B" | sed -n '2,/^$/p' | grep -v '^$' | tr '\n' ' '
250}
251conf=$(twoprs leg); [ -n "$conf" ] && ok "AM1 RED reproduced on legacy: unrelated PR conflicts after the first lands (in: $conf)" || bad "AM1 legacy conflict not reproduced (test cannot go red)"
252gc reset -q --hard HEAD~1
253legacy_root=$(jq -r .extensions.generated.digest .crumb); sem0=$(for f in $(git ls-files '*.crumb' '**/.crumb'); do jq -cS '{purpose,layer,invariants,exports,related,boundaries}' "$f"; done)
254"$C" migrate . | grep -q '^crumb migrate: 4 crumbs, 4 rewritten' && ok "AM1 migrate rewrites every crumb once" || bad "AM1 migrate"
255"$C" migrate . | grep -q ', 0 rewritten' && ok "AM1 migrate is idempotent" || bad "AM1 migrate idempotent"
256[ "$(git ls-files '*.crumb' '**/.crumb' | xargs cat | grep -c '"generated"')" = 0 ] && ok "AM1 no committed crumb carries extensions.generated" || bad "AM1 generated left behind"
257[ "$(jq -r .extensions.generated_policy .crumb)" = on-demand ] && ok "AM1 root crumb carries generated_policy on-demand" || bad "AM1 root marker"
258[ "$sem0" = "$(for f in $(git ls-files '*.crumb' '**/.crumb'); do jq -cS '{purpose,layer,invariants,exports,related,boundaries}' "$f"; done)" ] && ok "AM1 migrate never touches the semantic kernel" || bad "AM1 kernel changed"
259v=$("$C" verify .); [ $? -eq 0 ] && echo "$v" | grep -q "^crumb verify: OK: 4 crumbs current at HEAD .*root digest $legacy_root$" && ok "AM1 verify OK and prints the same root digest the legacy root crumb held (provenance kept)" || bad "AM1 verify/root digest ($v vs $legacy_root)"
260[ "$(echo "$v" | wc -l)" = 1 ] && ok "AM1 verify prints one line (verify.rs readiness note)" || bad "AM1 verify lines"
261[ "$("$C" digest . | jq -r .digest)" = "$legacy_root" ] && "$C" digest src | jq -e '.children | map(.name) == ["deep"]' >/dev/null && ok "AM1 digest prints the generated block on demand" || bad "AM1 digest"
262gc commit -qam "crumb migrate"
263conf=$(twoprs od); [ -z "$conf" ] && ok "AM1 GREEN: after migration the unrelated PR merges cleanly" || bad "AM1 on-demand still conflicts (in: $conf)"
264gc merge -q --squash odB >/dev/null 2>&1 && gc commit -qm "squash odB"; "$C" verify . >/dev/null && ok "AM1 GREEN: merged main verifies with no recompile" || bad "AM1 merged main does not verify"
265[ -z "$(git diff --name-only HEAD~2 HEAD | grep '\.crumb$')" ] && ok "AM1 neither PR changed any .crumb" || bad "AM1 PRs touched crumbs"
266d1=$("$C" digest . | jq -r .digest); echo x >> src/deep/b.c; "$C" verify . >/dev/null && [ "$("$C" digest . | jq -r .digest)" != "$d1" ] && ok "AM1 a source change keeps verify OK and moves the computed root digest" || bad "AM1 source change"
267git checkout -q -- src/deep/b.c
268git show HEAD~3:src/.crumb > src/.crumb; out=$("$C" verify .); rc=$?
269[ $rc -eq 1 ] && echo "$out" | grep -q '^STALE src (committed generated data' && ok "AM1 a re-added generated block (old tool) fails verify, exit 1" || bad "AM1 re-added generated block passed ($rc)"
270"$C" compile . | grep -q 'STRIPPED src' && "$C" verify . >/dev/null && ok "AM1 compile strips it and verify is OK again" || bad "AM1 compile strip"
271"$C" status . | grep -c '^CURRENT' | grep -qx 4 && "$C" context src | grep -q '^Crumbs CURRENT at HEAD' && ok "AM1 status and context report CURRENT" || bad "AM1 status/context"
272d1=$("$C" digest . | jq -r .digest); git rm -q docs/.crumb; "$C" verify . >/dev/null && [ "$("$C" digest . | jq -r .digest)" != "$d1" ] && ok "AM1 deleting a crumb moves the root digest (D7 deletion still visible)" || bad "AM1 crumb deletion"
273gc reset -q --hard HEAD; echo '{broken' > docs/.crumb; "$C" verify . >/dev/null; [ $? -eq 1 ] && ok "AM1 a crumb that is not JSON fails verify" || bad "AM1 broken crumb passed"
274git checkout -q -- docs/.crumb
275cd "$SAVE" || exit 1
276# differential vs the shell reference (needs jq, git, sha256sum)
277REF="$HERE/crumb-compile.sh"
278if command -v jq >/dev/null 2>&1 && [ -f "$REF" ]; then
279 # compile from nothing, so last_commit is fresh in both. The differential compares the LEGACY generator (the shell
280 # reference has no on-demand mode), so the RFC-0003 amendment 1 root marker is dropped from the scratch clones too;
281 # the on-demand path is covered by the AM1 cases above.
282 wipe() { for f in $(cd "$1" && find . -name .crumb -not -path "./.git/*"); do jq -S "del(.extensions.generated, .extensions.generated_policy)" "$1/$f" > "$1/$f.t" && mv "$1/$f.t" "$1/$f"; done; }
283 diffgen() { # $1 repoA (shell) $2 repoB (C): every .crumb extensions.generated minus stamps must match
284 r=0; for f in $(cd "$1" && find . -name .crumb -not -path './.git/*'); do
285 a=$(jq -cS '.extensions.generated | del(.generated_at_commit, .compiled_at)' "$1/$f"); b=$(jq -cS '.extensions.generated | del(.generated_at_commit, .compiled_at)' "$2/$f")
286 [ "$a" = "$b" ] || { echo "DIFF $f"; r=1; }
287 done; return $r; }
288 cd "$T" || exit 1; rm -rf dA dB; git clone -q --no-hardlinks "$CC3" dA && git clone -q --no-hardlinks "$CC3" dB
289 wipe "$T/dA"; wipe "$T/dB"
290 (cd dA && sh "$REF" compile . >/dev/null) && (cd dB && "$C" compile . >/dev/null)
291 diffgen "$T/dA" "$T/dB" >/dev/null && ok "C3 differential on scratch repo: generated blocks identical to crumb-compile.sh" || bad "C3 differential scratch"
292 (cd dB && sh "$REF" verify . >/dev/null) && (cd dA && "$C" verify . >/dev/null) && ok "C3 each tool verifies the other's output" || bad "C3 cross verify"
293 TOP=$(git -C "$HERE" rev-parse --show-toplevel 2>/dev/null)
294 if [ -n "$TOP" ]; then
295 rm -rf eA eB; git clone -q --no-hardlinks "$TOP" eA && git clone -q --no-hardlinks "$TOP" eB
296 wipe "$T/eA"; wipe "$T/eB"
297 (cd eA && sh "$REF" compile . >/dev/null) && (cd eB && "$C" compile . >/dev/null)
298 diffgen "$T/eA" "$T/eB" > "$T/diff.out" && ok "C3 differential on this repo: generated blocks identical" || { cat "$T/diff.out"; bad "C3 differential this repo"; }
299 cmp -s "$T/eA/.crumb" "$T/eB/.crumb" 2>/dev/null; sed '/compiled_at/d' "$T/eA/tools/.crumb" > "$T/ta"; sed '/compiled_at/d' "$T/eB/tools/.crumb" > "$T/tb"
300 cmp -s "$T/ta" "$T/tb" && ok "C3 whole .crumb file byte-identical apart from compiled_at (same commit stamp)" || bad "C3 file bytes"
301 else echo "NOT_RUN differential on this repo (not in a git checkout)"; fi
302else echo "NOT_RUN C3 differential (jq or crumb-compile.sh missing)"; fi
303# ---- #234 store identity: outputs and record bytes unchanged, before vs after, unregistered and registered ----
304# Same scripted session in the same path with the pre-#234 tool (built from BASE) and this tool, unregistered and
305# registered: stdout of claim/update/whisper/close/sniff/list and every dirs/ and agents/ record must match byte for
306# byte once ISO timestamps and heartbeat ages are masked. Only STORE_ID (and the stderr dry-run line) is new.
307# crumb924 added lease generations (fencing tokens): the " generation N" suffix of CLAIMED, each lease's "generation"
308# and the record's "generations" map are masked too (nogen: jq prints these records in the tool's own layout, so the
309# rest is still compared byte for byte); test-namespace.sh covers generations themselves.
310nogen() { jq 'del(.generations) | if (.locks | type) == "object" then .locks |= map_values(del(.generation)) else . end' "$1"; }
311BASE=e6e48a0
312export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null; unset CRUMB_COORD_ROOT CRUMB_EXPECT_STORE_ID CRUMB_STRICT_STORE_ID
313if git -C "$HERE" cat-file -e "$BASE:tools/crumb/crumb.c" 2>/dev/null; then
314 git -C "$HERE" show "$BASE:tools/crumb/crumb.c" > "$T/base.c" && cc -std=c11 -O2 -o "$T/crumb-base" "$T/base.c"
315 session() { # session <crumb> <out-dir> [registered]: fresh repo at one fixed path, fixed commit dates
316 rm -rf "$T/same"; mkdir -p "$T/same/src" && cd "$T/same" || exit 1
317 git init -q -b main . && echo a > src/a.c && git add -A && GIT_AUTHOR_DATE=2026-01-01T00:00:00Z GIT_COMMITTER_DATE=2026-01-01T00:00:00Z git -c user.name=t -c user.email=t@t commit -qm init
318 git worktree add -q -b wb "$T/same-wb" 2>/dev/null || { rm -rf "$T/same-wb"; git worktree prune; git worktree add -q -b wb "$T/same-wb"; }
319 if [ -n "${3:-}" ]; then git config crumb.storeId "$("$1" store-id . 2>/dev/null)"; fi
320 mkdir -p "$2"
321 { "$1" claim agent-a src a.c "rewrite a" 600; (cd "$T/same-wb" && "$1" claim agent-b src a.c "also a"); echo "rc=$?"
322 "$1" update agent-a src "still on a"; "$1" whisper agent-a src agent-b "half done" high src/a.c
323 (cd "$T/same-wb" && "$1" sniff agent-b src); "$1" close agent-b src a.c; echo "rc=$?"
324 "$1" close agent-a src a.c modify "done"; (cd "$T/same-wb" && "$1" claim agent-b src a.c "my turn"); "$1" list .
325 (cd "$T/same-wb" && "$1" sniff agent-b src); } 2>"$2/stderr" | sed -E 's/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:]{8}Z/TS/g; s/heartbeat [0-9-]+s/heartbeat Ns/g; s/ generation [0-9]+$//' > "$2/stdout"
326 S=$(git rev-parse --absolute-git-dir)/crumb/v1
327 (cd "$S" && find dirs agents -type f | sort | while read -r f; do echo "== $f"; case $f in dirs/*) nogen "$f";; *) cat "$f";; esac | sed -E 's/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:]{8}Z/TS/g'; done) > "$2/records"
328 [ -f "$S/STORE_ID" ] && cp "$S/STORE_ID" "$2/STORE_ID"
329 cd "$T" || exit 1; rm -rf "$T/same" "$T/same-wb"
330 }
331 session "$T/crumb-base" "$T/s-base"; session "$C" "$T/s-new"; session "$C" "$T/s-reg" registered
332 cmp -s "$T/s-base/stdout" "$T/s-new/stdout" && ok "#234 unregistered: claim/update/whisper/close/sniff/list stdout identical to $BASE" || { diff "$T/s-base/stdout" "$T/s-new/stdout" | head; bad "#234 unregistered stdout differs"; }
333 cmp -s "$T/s-base/records" "$T/s-new/records" && ok "#234 unregistered: every dirs/ and agents/ record byte-identical to $BASE (timestamps masked)" || { diff "$T/s-base/records" "$T/s-new/records" | head; bad "#234 unregistered records differ"; }
334 cmp -s "$T/s-base/stdout" "$T/s-reg/stdout" && cmp -s "$T/s-base/records" "$T/s-reg/records" && ok "#234 registered: stdout and records identical to $BASE" || { diff "$T/s-base/stdout" "$T/s-reg/stdout" | head; bad "#234 registered output differs"; }
335 [ ! -f "$T/s-base/STORE_ID" ] && grep -Eqx 'crumb-coord-store-id/1 [0-9a-f]{32}' "$T/s-new/STORE_ID" && ok "#234 only addition on disk: STORE_ID marker (synapse-coord format)" || bad "#234 STORE_ID addition"
336 grep -q 'STORE_UNREGISTERED (dry run)' "$T/s-new/stderr" && ! grep -q STORE_UNREGISTERED "$T/s-reg/stderr" && [ ! -s "$T/s-base/stderr" ] && ok "#234 dry-run warning only on stderr, only when unregistered" || bad "#234 stderr warning"
337 grep -q 'STORE_ID' "$T/s-new/records" && bad "#234 STORE_ID leaked into records" || ok "#234 STORE_ID never appears in records"
338else echo "NOT_RUN #234 before/after differential (no $BASE in this checkout's history)"; fi
339exit $fail