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 / ops / test-spark-cleanup.sh

631 lines · 54115 bytes Raw Blame History
1#!/usr/bin/env bash
2# test-spark-cleanup.sh [SPARK_CLEANUP]: offline test of spark-cleanup classes 1, 2, 5 and 8 and of
3# --landed, each in a scratch HOME.
4# Never touches the real home, ~/git or the team board: HOME, the git store and the board all point
5# into a mktemp folder. Cases:
6# c2-store a worktree whose HEAD is in the scratch ~/git store, no upstream: removed under --apply
7# c2-remote a worktree whose HEAD was pushed to a bare repo OUTSIDE ~/git (it has an upstream and
8# `branch -r --contains` finds it): must be kept and logged not-in-git-store
9# c8-left an idle clone whose every branch head is in the store: ALARM leftover-copy
10# c8-unst an idle clone with a commit not in the store: ALARM unstored-copy
11# c8-dirty an idle clone with an uncommitted change: ALARM dirty-copy
12# c8-model a 1.1 GiB sparse .safetensors file under ~/work: ALARM model-copy
13# c8-fresh a clone written now: no ALARM
14# c8-wt-own in the dry run the idle worktree wt-store (HEAD in the store) is a leftover-copy, not
15# unstored because a sibling worktree's branch is missing from the store
16# c8-rerun the alarms of the first (dry) run still fire on the second run: the checks themselves
17# must not rewrite a copy and make it look recently used
18# c8-board the --apply run writes exactly one ALARM line to the scratch board; a dry run writes none
19# c8-nodel class 8 deletes nothing
20# c8-stash an idle clone whose only unsaved work is a stash: unstored-copy, not leftover
21# c8-ignored an idle clone with results in a git-ignored folder: dirty-copy, not leftover
22# c8-unread an idle linked worktree whose admin folder is gone (git cannot read it): unreadable-copy
23# c8-nested an idle clone whose head is only in a nested store repo (git/sub/n.git): leftover-copy
24# c8-hardlink a weight file hard-linked from ~/models (2 links) is not called a copy
25# c8-dedupe a second --apply run over the same copies adds no second board line
26# c8-badthr SPARK_CLEANUP_LEFTOVER_ALARM_GB=0.5 is reported loudly, not a shell error
27# c8-nolock with a quiet-build in the scratch home, an --apply run gives the locked run classes
28# without 8 and runs class 8 afterwards on its own (classes=8 line, after quiet-build ended)
29# store-empty an empty store gives the loud "holds no bare repositories" ALARM and one board line,
30# not a second one on the next run
31# c8-emerg with free space under 10 % an --apply run still runs class 8 on its own, after the rest
32# c8-main main clones (no '-' in the name, real .git folder) are still checked: the clean one
33# ~/workspace/ref is a SKIP main-clone line, ~/workspace/proj (branches not in the store) is an
34# ALARM marked "main clone"
35# c8-drymark a dry --classes 8 run neither reads nor writes the --apply scan's start and finish marks
36# c2-nested an idle clean worktree holding a repository in an ignored folder (.lake/packages/dep) is kept
37# c2-untracked a worktree whose status.showUntrackedFiles=no hides notes.txt: kept, file unchanged
38# c2-ignored the same setting hides an ignored secret/: kept, file unchanged
39# c2-skipwt a skip-worktree edit that status does not show: kept, the edit unchanged
40# c2-assume an assume-unchanged edit that status does not show: kept, the edit unchanged
41# c2-bare a bare repo .lake/packages/dep.git whose commit is not in ~/git: kept, not leftover
42# c8-nestbad a nested repo in an ignored build/, one in .lake, and a submodule commit, none of them
43# in ~/git: unstored, not leftover, and the files still there
44# c8-remoteref a commit reachable only from refs/remotes/other/main, not in ~/git: unstored, not leftover
45# c8-fourbit ~/work/hive/uxl-brief-20261113/FourBit is not deleted and not named for removal.
46# A verified cargo target under ~/handoffs/hive/factory-bays is removed; one under
47# evidence, and a plain file under ~/handoffs, stays.
48# c8-inner an idle repository inside a copy that is in use now is not reported on its own
49# c8-hook the test in-use hook says in the log that it was used
50# c8-agentwt an idle linked worktree inside the main clone (proj/.claude/worktrees/agent-x) is reported
51# c8-dashwt an idle linked worktree ~/workspace/kflow (no '-', but not a main clone) is reported
52# c8-unfin a class 8 scan that started and never finished gives a loud ALARM and one board line
53# c8-maxh SPARK_CLEANUP_CLASS8_MAX_H=1.5 is reported loudly, not a shell error
54# c8-only8 --apply --classes 8 does not go through quiet-build (no deleting run to lock)
55# c8-help --help prints the whole header, through the Usage section
56# f1-bare-c2 a worktree holding a bare repository not named *.git (.lake/packages/dep): kept, unstored
57# f1-bare-c8 a clone holding such a bare repository: unstored-copy, the repository still there
58# f1-bare-c1 a cargo cache holding a bare repository named vendor: class 1 SKIP has-git
59# f2-subbranch a clone whose submodule has a branch not in ~/git (HEAD back on main): unstored-copy
60# f3-nestfile a clone whose nested checkout .lake/packages/dep has an untracked notes.txt: dirty-copy
61# f4-mode an assume-unchanged file whose only change is the executable bit: worktree kept
62# f5-mktemp TMPDIR that does not exist: loud ALARM, non-zero exit, nothing removed
63# --landed lrepo (own scratch home, ~/git/lrepo.git landed):
64# ld-exit exit 0, nothing on stdout or stderr, one summary line: removed 4, archived 2, kept 7
65# ld-saved a saved idle clone (an old CLAIM 30 days back, a fresh FETCH_HEAD) is removed
66# ld-root a saved clone with no remote, matched by its root commit, is removed
67# ld-recent a saved clone with a file written now is kept (written in the last 6 h)
68# ld-open a saved clone that is a running process's working folder is kept (open)
69# ld-claim a clone named by a current CLAIM line on the board is kept
70# ld-claims a clone inside ~/work/lane2, a folder claimed in ~/handoffs/hive/CLAIMS.md ("~/work/lane2:"), is kept
71# ld-dashmain the dashed main clone ~/workspace/ld-main (store ld-main-mv.git, same root commit) is kept,
72# while the dashed task copy ~/workspace/task-lrepo is removed
73# ld-unsaved-recent a clone with a commit not in ~/git, written 1 day ago, is kept, not archived
74# ld-other a clone of another repository is not looked at
75# ld-main the main clone ~/workspace/lrepo is kept
76# ld-handoffs a clone under ~/handoffs is not looked at
77# ld-wt a saved linked worktree with an ignored target/ is removed and pruned
78# ld-archive an unsaved idle clone is archived, replaced by ARCHIVED.txt, and its restore line
79# brings back the commit and the untracked file
80# ld-wtarch an unsaved idle linked worktree (detached commit, dirty file) is archived with a
81# bundle of its HEAD; after gc in the main clone, the one restore line brings it back
82# ld-badname --landed '../x' is a loud ALARM and does nothing
83# --landed, second scratch home with no ~/handoffs/hive/CLAIMS.md (three landings: lrepo, brepo with a huge
84# archive reserve, orepo); the board's CLAIM lines count although the other claim file is missing:
85# l2-rootclaim a CLAIM of ~/work, ~/workspace/ and ~/.claude/jobs is not used: the saved idle copy under it
86# is removed and the log names the claim in an ALARM
87# l2-rootboard that claim is one ALARM line on the board, not repeated at the next two landings, and that
88# ALARM line is not itself read as a claim (no lane "?" naming the folders; still 2 claimed paths)
89# l2-release a copy claimed by LaneR and then released by LaneR is removed
90# l2-otherlane a copy claimed by LaneS stays when only LaneT posts a RELEASE
91# l2-relfirst a copy claimed by LaneU after an earlier RELEASE of LaneU stays
92# l2-pending a saved copy written now is kept and listed in landed-pending; the next landing (of brepo)
93# looks at it again; once it is idle the landing of orepo removes it ([kept at an earlier landing])
94# l2-buildout an unsaved idle copy is archived without its ignored cargo target/, ARCHIVED.txt names it
95# in a "left out" line, and an ignored .lake/ holding an unsaved nested repository stays in
96# l2-noroom with SPARK_CLEANUP_ARCHIVE_RESERVE_GB=999999999 an unsaved idle copy stays, with a "not enough
97# free space" ALARM, and is listed in landed-pending
98# l2-recheck the landing of orepo, with the default reserve, archives that copy; landed-pending then
99# lists only the copies still claimed
100# c2-claim class 2 in the same home: a saved idle worktree that a current CLAIM names stays (class2 SKIP
101# claimed); the claim of ~/workspace/ is not used (a class2 ALARM), so the unclaimed one is removed
102# class 5 (own scratch home, a scratch folder in place of /tmp, SPARK_CLEANUP_TMP_MAX=1 ignored):
103# c5-hook the test hook says class 5 read the scratch folder
104# c5-unsaved a /tmp clone with a commit not in ~/git stays: class5 SKIP unsaved naming the commit
105# c5-nested a /tmp folder holding a clone with an untracked file stays
106# c5-saved a saved /tmp clone over the old size cap goes, no over-cap line
107# c5-junk a /tmp folder with no repository goes
108# c5-bigalarm one ALARM line names each entry over SPARK_CLEANUP_BIG_ALARM, removed or kept, not the small one
109# c5-locked a saved, locked linked worktree in /tmp stays: class5 SKIP locked, its main clone still lists it
110# Then mutants of spark-cleanup must each make at least one case fail.
111# Ends `TEST PASS cases=N mutants=M/M` (exit 0) or `TEST FAIL ...` (exit 1).
112set -u
113here=$(cd "$(dirname "$0")" && pwd)
114sc=${1:-$here/spark-cleanup}
115[ -f "$sc" ] || { echo "TEST FAIL no spark-cleanup at $sc" >&2; exit 2; }
116top=$(mktemp -d) || exit 2
117[ -n "${KEEP:-}" ] || trap 'rm -rf "$top"' EXIT
118export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@t GIT_TERMINAL_PROMPT=0
119unset GIT_DIR GIT_WORK_TREE
120# nothing in the scratch home is open by another process; skip the machine-wide scan
121: > "$top/inuse"; export SPARK_CLEANUP_TEST_INUSE_FILE="$top/inuse"
122
123setup() { # $1 scratch home
124 local h=$1
125 mkdir -p "$h/git" "$h/workspace" "$h/work/lane" "$h/elsewhere" "$h/handoffs"
126 : > "$h/handoffs/BOARD.md"
127 git init -q -b main "$h/workspace/proj"
128 echo a > "$h/workspace/proj/a"; git -C "$h/workspace/proj" add a; git -C "$h/workspace/proj" commit -qm a
129 git clone -q --bare "$h/workspace/proj" "$h/git/proj.git"
130 # c2-store: worktree at a commit the store has, no upstream
131 git -C "$h/workspace/proj" worktree add -q -b wt-store "$h/workspace/wt-store" main
132 # c2-remote: worktree with a new commit pushed only to a bare repo outside ~/git
133 git clone -q --bare "$h/workspace/proj" "$h/elsewhere/other.git"
134 git -C "$h/workspace/proj" remote add other "$h/elsewhere/other.git"
135 git -C "$h/workspace/proj" worktree add -q -b wt-remote "$h/workspace/wt-remote" main
136 echo b > "$h/workspace/wt-remote/b"; git -C "$h/workspace/wt-remote" add b; git -C "$h/workspace/wt-remote" commit -qm b
137 git -C "$h/workspace/wt-remote" push -q -u other wt-remote 2>/dev/null
138 # class 8 clones
139 git clone -q "$h/git/proj.git" "$h/work/lane/left"
140 git clone -q "$h/git/proj.git" "$h/work/lane/unst"
141 echo c > "$h/work/lane/unst/c"; git -C "$h/work/lane/unst" add c; git -C "$h/work/lane/unst" commit -qm c
142 git clone -q "$h/git/proj.git" "$h/work/lane/dirty"; echo x >> "$h/work/lane/dirty/a"
143 git clone -q "$h/git/proj.git" "$h/work/lane/stash"; echo s >> "$h/work/lane/stash/a"; git -C "$h/work/lane/stash" stash -q
144 git clone -q "$h/git/proj.git" "$h/work/lane/ign"; mkdir -p "$h/work/lane/ign/secret"; echo r > "$h/work/lane/ign/secret/results"
145 echo secret/ >> "$h/work/lane/ign/.git/info/exclude"
146 git clone -q "$h/git/proj.git" "$h/workspace/ref"
147 echo .lake/ >> "$h/workspace/proj/.git/info/exclude"
148 git -C "$h/workspace/proj" worktree add -q -b wt-nest "$h/workspace/wt-nest" main
149 git init -q -b main "$h/workspace/wt-nest/.lake/packages/dep"
150 echo d > "$h/workspace/wt-nest/.lake/packages/dep/d"
151 git -C "$h/workspace/wt-nest/.lake/packages/dep" add d
152 git -C "$h/workspace/wt-nest/.lake/packages/dep" commit -qm d
153 git -C "$h/workspace/proj" worktree add -q -b agent-x "$h/workspace/proj/.claude/worktrees/agent-x" main
154 git -C "$h/workspace/proj" worktree add -q -b kflow "$h/workspace/kflow" main
155 git -C "$h/workspace/proj" worktree add -q -b wt-orphan "$h/work/lane/orphan" main
156 echo z > "$h/work/lane/orphan/z"; git -C "$h/work/lane/orphan" add z; git -C "$h/work/lane/orphan" commit -qm z
157 rm -rf "$h/workspace/proj/.git/worktrees/orphan"
158 mkdir -p "$h/git/sub" "$h/work/lane/nested-src"; git init -q -b main "$h/work/lane/nested-src"
159 echo n > "$h/work/lane/nested-src/n"; git -C "$h/work/lane/nested-src" add n; git -C "$h/work/lane/nested-src" commit -qm n
160 git clone -q --bare "$h/work/lane/nested-src" "$h/git/sub/n.git"; rm -rf "$h/work/lane/nested-src"
161 git clone -q "$h/git/sub/n.git" "$h/work/lane/nested"
162
163 mkdir -p "$h/work/lane/weights"; truncate -s 1100M "$h/work/lane/weights/m.safetensors"
164 mkdir -p "$h/models"; truncate -s 1100M "$h/models/w.gguf"; ln "$h/models/w.gguf" "$h/work/lane/weights/linked.gguf"
165 # hidden from a plain git status: untracked, ignored, and a skip-worktree edit. HEAD is in the store.
166 git init -q -b main "$h/workspace/hide-repo"
167 echo a > "$h/workspace/hide-repo/a"; git -C "$h/workspace/hide-repo" add a; git -C "$h/workspace/hide-repo" commit -qm a
168 git clone -q --bare "$h/workspace/hide-repo" "$h/git/hide.git"
169 git -C "$h/workspace/hide-repo" worktree add -q -b wt-untracked "$h/workspace/wt-untracked" main
170 git -C "$h/workspace/hide-repo" worktree add -q -b wt-ignored "$h/workspace/wt-ignored" main
171 git -C "$h/workspace/hide-repo" config status.showUntrackedFiles no
172 echo notes > "$h/workspace/wt-untracked/notes.txt"
173 mkdir -p "$h/workspace/wt-ignored/secret"; echo z > "$h/workspace/wt-ignored/secret/z"
174 echo secret/ >> "$h/workspace/hide-repo/.git/info/exclude"
175 git init -q -b main "$h/workspace/skip-repo"
176 echo a > "$h/workspace/skip-repo/a"; git -C "$h/workspace/skip-repo" add a; git -C "$h/workspace/skip-repo" commit -qm a
177 git clone -q --bare "$h/workspace/skip-repo" "$h/git/skip.git"
178 git -C "$h/workspace/skip-repo" worktree add -q -b wt-skip "$h/workspace/wt-skip" main
179 git -C "$h/workspace/wt-skip" update-index --skip-worktree a
180 echo edited >> "$h/workspace/wt-skip/a"
181 git -C "$h/workspace/skip-repo" worktree add -q -b wt-assume "$h/workspace/wt-assume" main
182 git -C "$h/workspace/wt-assume" update-index --assume-unchanged a
183 echo edited >> "$h/workspace/wt-assume/a"
184 # bare repository inside an ignored .lake/, commit not in the store
185 git -C "$h/workspace/proj" worktree add -q -b wt-bare "$h/workspace/wt-bare" main
186 git init -q -b main "$h/elsewhere/bare-src"
187 echo only > "$h/elsewhere/bare-src/only"
188 git -C "$h/elsewhere/bare-src" add only; git -C "$h/elsewhere/bare-src" commit -qm only
189 mkdir -p "$h/workspace/wt-bare/.lake/packages"
190 git clone -q --bare "$h/elsewhere/bare-src" "$h/workspace/wt-bare/.lake/packages/dep.git"
191 rm -rf "$h/elsewhere/bare-src"
192 # nested repository inside an ignored build/, commit not in the store; the parent commit is
193 git clone -q "$h/git/proj.git" "$h/work/lane/outer"
194 printf 'build/\n' >> "$h/work/lane/outer/.gitignore"
195 git -C "$h/work/lane/outer" add .gitignore; git -C "$h/work/lane/outer" commit -qm gi
196 git -C "$h/work/lane/outer" push -q origin HEAD:main
197 git init -q -b main "$h/work/lane/outer/build/mywork"
198 echo w > "$h/work/lane/outer/build/mywork/w"
199 git -C "$h/work/lane/outer/build/mywork" add w; git -C "$h/work/lane/outer/build/mywork" commit -qm w
200 # submodule whose commit object lives only inside the clone
201 git init -q -b main "$h/elsewhere/dep-src"
202 echo d > "$h/elsewhere/dep-src/d"; git -C "$h/elsewhere/dep-src" add d; git -C "$h/elsewhere/dep-src" commit -qm d
203 git clone -q "$h/git/proj.git" "$h/work/lane/super"
204 git -C "$h/work/lane/super" -c protocol.file.allow=always submodule add -q "$h/elsewhere/dep-src" dep
205 git -C "$h/work/lane/super" commit -qm sub
206 git -C "$h/work/lane/super" push -q origin HEAD:main
207 rm -rf "$h/elsewhere/dep-src"
208 # commit reachable only from a remote-tracking ref
209 git clone -q "$h/git/proj.git" "$h/work/lane/rem"
210 git -C "$h/work/lane/rem" commit -q --allow-empty -m extra
211 git -C "$h/work/lane/rem" update-ref refs/remotes/other/main HEAD
212 git -C "$h/work/lane/rem" reset -q --hard origin/main
213 # FourBit is excluded whole. A verified cargo target under factory-bays may go;
214 # one under evidence, and any other file under handoffs, stays.
215 local fb="$h/work/hive/uxl-brief-20261113/FourBit" bay="$h/handoffs/hive/factory-bays/job"
216 mkdir -p "$fb/target/debug" "$bay/target/debug" "$bay/evidence/target/debug"
217 printf 'Signature: 8a477f597d28d172789f06886806bc55\n' > "$fb/target/CACHEDIR.TAG"
218 echo only > "$fb/target/ONLY-COPY.txt"; echo keep > "$fb/keep.txt"
219 git clone -q "$h/git/proj.git" "$fb/checkout"
220 printf 'Signature: 8a477f597d28d172789f06886806bc55\n' > "$bay/target/CACHEDIR.TAG"
221 echo rebuild > "$bay/target/rebuild.txt"
222 printf 'Signature: 8a477f597d28d172789f06886806bc55\n' > "$bay/evidence/target/CACHEDIR.TAG"
223 echo keep > "$bay/evidence/target/keep.txt"
224 echo keep > "$h/handoffs/keep.txt"
225 # f1: bare repositories whose folder name does not end in .git, holding a commit in no store repo
226 git init -q -b main "$h/elsewhere/nb-src"
227 echo nb > "$h/elsewhere/nb-src/nb"; git -C "$h/elsewhere/nb-src" add nb; git -C "$h/elsewhere/nb-src" commit -qm nb
228 git -C "$h/workspace/proj" worktree add -q -b wt-barenose "$h/workspace/wt-barenose" main
229 mkdir -p "$h/workspace/wt-barenose/.lake/packages"
230 git clone -q --bare "$h/elsewhere/nb-src" "$h/workspace/wt-barenose/.lake/packages/dep"
231 git init -q -b main "$h/elsewhere/fx-src"
232 echo f > "$h/elsewhere/fx-src/f"; git -C "$h/elsewhere/fx-src" add f; git -C "$h/elsewhere/fx-src" commit -qm f
233 git clone -q --bare "$h/elsewhere/fx-src" "$h/git/fx.git"; rm -rf "$h/elsewhere/fx-src"
234 git clone -q "$h/git/fx.git" "$h/work/lane/baren"; echo .lake/ >> "$h/work/lane/baren/.git/info/exclude"
235 mkdir -p "$h/work/lane/baren/.lake/packages"
236 git clone -q --bare "$h/elsewhere/nb-src" "$h/work/lane/baren/.lake/packages/dep"
237 mkdir -p "$bay/cache-nosuffix/debug"
238 printf 'Signature: 8a477f597d28d172789f06886806bc55\n' > "$bay/cache-nosuffix/CACHEDIR.TAG"
239 git clone -q --bare "$h/elsewhere/nb-src" "$bay/cache-nosuffix/vendor"
240 rm -rf "$h/elsewhere/nb-src"
241 # f2: a submodule whose checked-out commit is stored, but whose branch wip is in no store repo
242 git init -q -b main "$h/elsewhere/d2-src"
243 echo e > "$h/elsewhere/d2-src/e"; git -C "$h/elsewhere/d2-src" add e; git -C "$h/elsewhere/d2-src" commit -qm e
244 git clone -q --bare "$h/elsewhere/d2-src" "$h/git/dep2.git"; rm -rf "$h/elsewhere/d2-src"
245 git clone -q "$h/git/fx.git" "$h/work/lane/super2"
246 git -C "$h/work/lane/super2" -c protocol.file.allow=always submodule add -q "$h/git/dep2.git" dep
247 git -C "$h/work/lane/super2" commit -qm sub2; git -C "$h/work/lane/super2" push -q origin HEAD:main
248 git -C "$h/work/lane/super2/dep" checkout -q -b wip; git -C "$h/work/lane/super2/dep" commit -q --allow-empty -m wip
249 git -C "$h/work/lane/super2/dep" checkout -q main
250 # f3: a nested repository in an ignored .lake/, every commit stored, an untracked file inside it
251 git init -q -b main "$h/elsewhere/nf-src"
252 echo n > "$h/elsewhere/nf-src/n"; git -C "$h/elsewhere/nf-src" add n; git -C "$h/elsewhere/nf-src" commit -qm n
253 git clone -q --bare "$h/elsewhere/nf-src" "$h/git/nf.git"; rm -rf "$h/elsewhere/nf-src"
254 git clone -q "$h/git/fx.git" "$h/work/lane/nestfile"; echo .lake/ >> "$h/work/lane/nestfile/.git/info/exclude"
255 git clone -q "$h/git/nf.git" "$h/work/lane/nestfile/.lake/packages/dep"
256 echo mine > "$h/work/lane/nestfile/.lake/packages/dep/notes.txt"
257 # f4: an assume-unchanged file whose bytes are stored but whose executable bit was turned on
258 git -C "$h/workspace/skip-repo" worktree add -q -b wt-mode "$h/workspace/wt-mode" main
259 git -C "$h/workspace/wt-mode" update-index --assume-unchanged a; chmod +x "$h/workspace/wt-mode/a"
260 find "$h/workspace" "$h/work" "$h/elsewhere" "$h/handoffs" -exec touch -h -d '5 days ago' {} + 2>/dev/null
261 git clone -q "$h/git/proj.git" "$h/work/lane/fresh"
262 # c8-inner: a copy in use now (fresh files) holding an idle clone
263 git clone -q "$h/git/proj.git" "$h/work/lane/active"; git clone -q "$h/git/proj.git" "$h/work/lane/active/sub/inner"
264 find "$h/work/lane/active/sub" -exec touch -h -d '5 days ago' {} +
265}
266
267run() { # $1 home, $2 spark-cleanup copy, rest: args; prints the log
268 local h=$1 s=$2; shift 2
269 HOME=$h SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_OFFLINE=1 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
270 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_DRY_CLASSES= bash "$s" "$@" > "$h/out.txt" 2>&1
271}
272
273pass=0 fail=0
274check() { # $1 case name, $2 0|1 result (0 = holds)
275 if [ "$2" = 0 ]; then pass=$((pass+1)); [ -n "${quiet:-}" ] || echo "PASS $1"; else fail=$((fail+1)); [ -n "${quiet:-}" ] || echo "FAIL $1"; fi
276}
277has() { grep -qE -- "$2" "$1"; }
278
279# --landed lrepo in its own scratch home: which copies of the landed repository go, which are
280# archived, which stay, and that an archive restores the unsaved work
281landed_cases() { # $1 spark-cleanup copy
282 local s=$1 h n uc wc lrc sp i A
283 h=$(mktemp -d "$top/l.XXXX")
284 mkdir -p "$h/git" "$h/workspace" "$h/work/lane" "$h/work/lane2" "$h/handoffs/land" "$h/handoffs/hive" "$h/elsewhere"
285 git init -q -b main "$h/workspace/lrepo"
286 echo a > "$h/workspace/lrepo/a"; git -C "$h/workspace/lrepo" add a; git -C "$h/workspace/lrepo" commit -qm a
287 git clone -q --bare "$h/workspace/lrepo" "$h/git/lrepo.git"
288 git -C "$h/workspace/lrepo" remote add origin "$h/git/lrepo.git"; git -C "$h/workspace/lrepo" fetch -q origin
289 echo target/ >> "$h/workspace/lrepo/.git/info/exclude"
290 git init -q -b main "$h/elsewhere/o"
291 echo o > "$h/elsewhere/o/o"; git -C "$h/elsewhere/o" add o; git -C "$h/elsewhere/o" commit -qm o
292 git clone -q --bare "$h/elsewhere/o" "$h/git/orepo.git"; rm -rf "$h/elsewhere/o"
293 for n in saved-idle saved-noremote saved-recent saved-open claimed unsaved-old unsaved-recent; do
294 git clone -q "$h/git/lrepo.git" "$h/work/lane/$n"
295 done
296 git clone -q "$h/git/lrepo.git" "$h/work/lane2/child"
297 git clone -q --bare "$h/git/lrepo.git" "$h/git/ld-main-mv.git"
298 git clone -q "$h/git/ld-main-mv.git" "$h/workspace/ld-main"
299 git clone -q "$h/git/lrepo.git" "$h/workspace/task-lrepo"
300 printf '%s | CLAIM | lane-y | isolated ~/work/lane2: host only\n' "$(date -u +%FT%H:%M:%SZ)" > "$h/handoffs/hive/CLAIMS.md"
301 git -C "$h/work/lane/saved-noremote" remote remove origin
302 echo u > "$h/work/lane/unsaved-old/u"; git -C "$h/work/lane/unsaved-old" add u; git -C "$h/work/lane/unsaved-old" commit -qm u
303 uc=$(git -C "$h/work/lane/unsaved-old" rev-parse HEAD); echo mine > "$h/work/lane/unsaved-old/notes.txt"
304 git -C "$h/work/lane/unsaved-recent" commit -q --allow-empty -m r
305 git clone -q "$h/git/orepo.git" "$h/work/lane/other"
306 git clone -q "$h/git/lrepo.git" "$h/handoffs/land/x"
307 git -C "$h/workspace/lrepo" worktree add -q --detach "$h/work/lane/wt-saved" main
308 mkdir -p "$h/work/lane/wt-saved/target/debug"; echo o > "$h/work/lane/wt-saved/target/debug/o"
309 git -C "$h/workspace/lrepo" worktree add -q --detach "$h/work/lane/wt-unsaved" main
310 echo w > "$h/work/lane/wt-unsaved/w"; git -C "$h/work/lane/wt-unsaved" add w; git -C "$h/work/lane/wt-unsaved" commit -qm w
311 wc=$(git -C "$h/work/lane/wt-unsaved" rev-parse HEAD); echo dirty > "$h/work/lane/wt-unsaved/d"
312 printf '%s CLAIM omp lane-x ~/work/lane/claimed for a test\n' "$(date -u +%FT%H:%M:%SZ)" > "$h/handoffs/BOARD.md"
313 printf '%s CLAIM omp lane-old ~/work/lane/saved-idle long ago\n' "$(date -u -d '30 days ago' +%FT%H:%M:%SZ)" >> "$h/handoffs/BOARD.md"
314 find "$h/workspace" "$h/work" "$h/handoffs/land" -exec touch -h -d '5 days ago' {} +
315 touch "$h/work/lane/saved-recent/a" "$h/work/lane/saved-idle/.git/FETCH_HEAD"
316 find "$h/work/lane/unsaved-recent" -exec touch -h -d '1 day ago' {} +
317 # a real process with its working folder in saved-open, as the in-use list
318 (cd "$h/work/lane/saved-open" && exec sleep 600) & sp=$!
319 for i in $(seq 50); do [ "$(readlink "/proc/$sp/cwd" 2>/dev/null)" = "$h/work/lane/saved-open" ] && break; sleep 0.1; done
320 readlink "/proc/$sp/cwd" > "$h/inuse"
321 HOME=$h SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_OFFLINE=1 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
322 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_TEST_INUSE_FILE=$h/inuse bash "$s" --landed lrepo > "$h/out.txt" 2>&1; lrc=$?
323 kill "$sp" 2>/dev/null; wait "$sp" 2>/dev/null
324 cat "$h"/.local/state/spark-cleanup/*.log > "$h/l1.txt"
325 HOME=$h SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_OFFLINE=1 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
326 SPARK_CLEANUP_TEST_INUSE_FILE=$h/inuse bash "$s" --landed '../x' > "$h/out2.txt" 2>&1
327 cat "$h"/.local/state/spark-cleanup/*.log > "$h/l2.txt"
328 local L="$h/l1.txt" W="$h/work/lane"
329 check ld-exit "$( [ "$lrc" = 0 ] && [ ! -s "$h/out.txt" ] && has "$L" "landed lrepo done: removed 4, archived 2, kept 7"; echo $?)"
330 check ld-saved "$( [ ! -e "$W/saved-idle" ] && has "$L" "landed REMOVED .*$W/saved-idle "; echo $?)"
331 check ld-root "$( [ ! -e "$W/saved-noremote" ] && has "$L" "landed REMOVED .*$W/saved-noremote "; echo $?)"
332 check ld-recent "$( [ -f "$W/saved-recent/a" ] && has "$L" "landed KEEP $W/saved-recent \(written in the last 6 h\)"; echo $?)"
333 check ld-open "$( [ -f "$W/saved-open/a" ] && has "$L" "landed KEEP $W/saved-open \(open by a running process\)"; echo $?)"
334 check ld-claim "$( [ -f "$W/claimed/a" ] && has "$L" "landed KEEP $W/claimed \(claimed"; echo $?)"
335 check ld-claims "$( [ -f "$h/work/lane2/child/a" ] && has "$L" "landed KEEP $h/work/lane2/child \(claimed"; echo $?)"
336 check ld-dashmain "$( [ -f "$h/workspace/ld-main/a" ] && has "$L" "landed KEEP $h/workspace/ld-main \(main clone\)" \
337 && [ ! -e "$h/workspace/task-lrepo" ] && has "$L" "landed REMOVED .*$h/workspace/task-lrepo "; echo $?)"
338 check ld-unsaved-recent "$( [ -d "$W/unsaved-recent/.git" ] && [ ! -e "$W/unsaved-recent/ARCHIVED.txt" ] && has "$L" "landed KEEP $W/unsaved-recent \(unsaved"; echo $?)"
339 check ld-other "$( [ -f "$W/other/o" ] && ! has "$L" "$W/other"; echo $?)"
340 check ld-main "$( [ -d "$h/workspace/lrepo/.git" ] && has "$L" "landed KEEP $h/workspace/lrepo \(main clone\)"; echo $?)"
341 check ld-handoffs "$( [ -d "$h/handoffs/land/x/.git" ] && ! has "$L" "handoffs/land"; echo $?)"
342 check ld-wt "$( [ ! -e "$W/wt-saved" ] && [ ! -e "$h/workspace/lrepo/.git/worktrees/wt-saved" ] && has "$L" "landed REMOVED .*$W/wt-saved "; echo $?)"
343 # the archived clone: only ARCHIVED.txt is left; its restore line brings back the commit and the file
344 A=$(sed -n 's/^archive: //p' "$W/unsaved-old/ARCHIVED.txt" 2>/dev/null)
345 check ld-archive "$( [ -f "$A" ] && [ "$(ls -A "$W/unsaved-old")" = ARCHIVED.txt ] && has "$L" "landed ARCHIVED .*$W/unsaved-old -> $A" \
346 && (eval "$(sed -n 's/^restore: //p' "$W/unsaved-old/ARCHIVED.txt")") && git -C "$W/unsaved-old" cat-file -e "$uc" \
347 && [ "$(cat "$W/unsaved-old/notes.txt")" = mine ] && [ ! -e "$W/unsaved-old/ARCHIVED.txt" ]; echo $?)"
348 # the archived worktree: its detached commit is gone from the main clone after gc; the one restore line brings it back
349 git -C "$h/workspace/lrepo" reflog expire --expire=now --all; git -C "$h/workspace/lrepo" gc -q --prune=now
350 check ld-wtarch "$( [ -f "$W/wt-unsaved/ARCHIVED.txt" ] && ! git -C "$h/workspace/lrepo" cat-file -e "$wc" 2>/dev/null \
351 && (eval "$(sed -n 's/^restore: //p' "$W/wt-unsaved/ARCHIVED.txt")") \
352 && [ "$(git -C "$W/wt-unsaved" rev-parse HEAD)" = "$wc" ] && git -C "$W/wt-unsaved" cat-file -e "$wc" && [ "$(cat "$W/wt-unsaved/d")" = dirty ] \
353 && [ -z "$(git -C "$W/wt-unsaved" status --porcelain -uno -- w)" ] && [ ! -e "$W/wt-unsaved.HEAD.bundle" ] && [ ! -e "$W/wt-unsaved/ARCHIVED.txt" ]; echo $?)"
354 check ld-badname "$(has "$h/l2.txt" "landed ALARM bad repository name '\.\./x'"; echo $?)"
355}
356
357# --landed in a second scratch home: CLAIM/RELEASE, root-folder claims, copies kept for a passing reason
358# looked at again at later landings, build output left out of archives, and the free-space check
359landed2_cases() { # $1 spark-cleanup copy
360 local s=$1 h n W A P L1 L2 L3 now
361 h=$(mktemp -d "$top/l2.XXXX"); W="$h/work"; P="$h/.local/state/spark-cleanup/landed-pending"
362 mkdir -p "$h/git" "$h/workspace" "$W" "$h/handoffs/hive" "$h/src"
363 for n in lrepo brepo orepo; do
364 git init -q -b main "$h/src/$n"; echo "$n" > "$h/src/$n/f"; git -C "$h/src/$n" add f; git -C "$h/src/$n" commit -qm "$n"
365 git clone -q --bare "$h/src/$n" "$h/git/$n.git"
366 done
367 rm -rf "$h/src"
368 for n in rootlane/idle relr/cr rels/cs relu/cu fresh/cf arch/ca; do git clone -q "$h/git/lrepo.git" "$W/$n"; done
369 git clone -q "$h/git/brepo.git" "$W/big/cb"
370 echo u > "$W/arch/ca/u"; git -C "$W/arch/ca" add u; git -C "$W/arch/ca" commit -qm u
371 printf 'target/\n.lake/\n' >> "$W/arch/ca/.git/info/exclude"
372 mkdir -p "$W/arch/ca/target/debug"; echo o > "$W/arch/ca/target/debug/o"
373 printf 'Signature: 8a477f597d28d172789f06886806bc55\n' > "$W/arch/ca/target/CACHEDIR.TAG"
374 git init -q -b main "$W/arch/ca/.lake/packages/dep"; echo d > "$W/arch/ca/.lake/packages/dep/d"
375 git -C "$W/arch/ca/.lake/packages/dep" add d; git -C "$W/arch/ca/.lake/packages/dep" commit -qm d
376 echo b > "$W/big/cb/b"; git -C "$W/big/cb" add b; git -C "$W/big/cb" commit -qm b
377 now=$(date -u +%FT%H:%M:%SZ)
378 {
379 printf '%s CLAIM omp RootLane sweeping ~/work, ~/workspace/ and ~/.claude/jobs\n' "$now"
380 printf '%s RELEASE LaneU earlier work\n' "$now"
381 printf '%s CLAIM omp LaneR ~/work/relr for a test\n' "$now"
382 printf '%s CLAIM omp LaneS ~/work/rels for a test\n' "$now"
383 printf '%s CLAIM omp LaneU ~/work/relu for a test\n' "$now"
384 printf '%s RELEASE omp LaneR done\n' "$now"
385 printf '%s LaneT RELEASE ~/work/rels\n' "$now"
386 } > "$h/handoffs/BOARD.md"
387 find "$W" -exec touch -h -d '5 days ago' {} +
388 find "$W/fresh/cf" -exec touch -h {} +
389 l2run() { # $1 repo, rest: extra environment assignments for this landing
390 local repo=$1; shift
391 env "$@" HOME=$h SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_OFFLINE=1 SPARK_CLEANUP_GITSTORE=$h/git \
392 SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_TEST_INUSE_FILE=$h/inuse \
393 bash "$s" --landed "$repo" > "$h/out.txt" 2>&1
394 }
395 : > "$h/inuse"
396 l2run lrepo; cat "$h"/.local/state/spark-cleanup/*.log > "$h/r1.txt"; cp "$P" "$h/p1.txt" 2>/dev/null
397 l2run brepo SPARK_CLEANUP_ARCHIVE_RESERVE_GB=999999999
398 cat "$h"/.local/state/spark-cleanup/*.log | tail -n +$(( $(wc -l < "$h/r1.txt") + 1 )) > "$h/r2.txt"; cp "$P" "$h/p2.txt" 2>/dev/null
399 find "$W/fresh/cf" -exec touch -h -d '5 days ago' {} +
400 l2run orepo
401 cat "$h"/.local/state/spark-cleanup/*.log | tail -n +$(( $(wc -l < "$h/r1.txt") + $(wc -l < "$h/r2.txt") + 1 )) > "$h/r3.txt"
402 L1="$h/r1.txt" L2="$h/r2.txt" L3="$h/r3.txt"
403 check l2-rootclaim "$( [ ! -e "$W/rootlane/idle" ] && has "$L1" "landed REMOVED .*$W/rootlane/idle " \
404 && has "$L1" "landed ALARM CLAIM of a whole copy folder not used .*RootLane names $h/work[;\$]" \
405 && has "$L1" "RootLane names $h/workspace" && has "$L1" "RootLane names $h/.claude/jobs"; echo $?)"
406 check l2-rootboard "$( [ "$(grep -c 'ALARM spark-cleanup --landed to all: a claim line names a whole copy folder' "$h/handoffs/BOARD.md")" = 1 ] \
407 && has "$L2" "landed ALARM CLAIM of a whole copy folder" && has "$L3" "root-claim board line not repeated" \
408 && ! grep -qF ' ? names ' "$L2" "$L3" && has "$L3" "landed: 2 claimed path\(s\) "; echo $?)"
409 check l2-release "$( [ ! -e "$W/relr/cr" ] && has "$L1" "landed REMOVED .*$W/relr/cr "; echo $?)"
410 check l2-otherlane "$( [ -f "$W/rels/cs/f" ] && has "$L1" "landed KEEP $W/rels/cs \(claimed"; echo $?)"
411 check l2-relfirst "$( [ -f "$W/relu/cu/f" ] && has "$L1" "landed KEEP $W/relu/cu \(claimed"; echo $?)"
412 check l2-pending "$( has "$L1" "landed KEEP $W/fresh/cf \(written in the last 6 h\)\$" && grep -qxF "$W/fresh/cf" "$h/p1.txt" \
413 && has "$L2" "landed KEEP $W/fresh/cf \(written in the last 6 h\) \[kept at an earlier landing\]" \
414 && [ ! -e "$W/fresh/cf" ] && has "$L3" "landed REMOVED .*$W/fresh/cf .*\[kept at an earlier landing\]"; echo $?)"
415 A=$(sed -n 's/^archive: //p' "$W/arch/ca/ARCHIVED.txt" 2>/dev/null)
416 check l2-buildout "$( [ -f "$A" ] && has "$L1" "landed ARCHIVED .*$W/arch/ca -> .*build output left out: 1 folder\(s\)" \
417 && has "$W/arch/ca/ARCHIVED.txt" "^left out: build output, rebuild it after a restore: $W/arch/ca/target\$" \
418 && zstd -dc "$A" | tar -tf - > "$h/a.list" && ! grep -q '/arch/ca/target' "$h/a.list" \
419 && grep -q '/arch/ca/.lake/packages/dep/.git/HEAD$' "$h/a.list" && grep -q '/arch/ca/u$' "$h/a.list"; echo $?)"
420 check l2-noroom "$( has "$L2" "landed ALARM not enough free space to archive $W/big/cb: " \
421 && has "$L2" "landed KEEP $W/big/cb \(unsaved .*not enough free space to archive\)" && grep -qxF "$W/big/cb" "$h/p2.txt"; echo $?)"
422 check l2-recheck "$( [ -f "$W/big/cb/ARCHIVED.txt" ] && has "$L3" "landed ARCHIVED .*$W/big/cb -> .*\[kept at an earlier landing\]" \
423 && [ "$(cat "$P")" = "$(printf '%s\n%s' "$W/rels/cs" "$W/relu/cu")" ]; echo $?)"
424 git clone -q "$h/git/lrepo.git" "$h/workspace/lrepo"
425 git -C "$h/workspace/lrepo" worktree add -q -b c2a "$h/workspace/wt-c2claim"
426 git -C "$h/workspace/lrepo" worktree add -q -b c2b "$h/workspace/wt-c2free"
427 printf '%s CLAIM omp LaneW ~/workspace/wt-c2claim for a test\n' "$(date -u +%FT%H:%M:%SZ)" >> "$h/handoffs/BOARD.md"
428 find "$h/workspace" -exec touch -h -d '5 days ago' {} +
429 run "$h" "$s" --apply --classes 2
430 check c2-claim "$( [ -f "$h/workspace/wt-c2claim/f" ] && has "$h/out.txt" "class2 SKIP claimed $h/workspace/wt-c2claim\$" \
431 && [ ! -e "$h/workspace/wt-c2free" ] && has "$h/out.txt" "class2 REMOVED .*$h/workspace/wt-c2free" \
432 && has "$h/out.txt" "class2 ALARM claim of a whole copy folder not used: .*RootLane $h/workspace"; echo $?)"
433}
434
435# class 5 in its own scratch home, reading a scratch folder in place of /tmp: a /tmp git copy
436# goes only when everything in it is saved in ~/git; size never decides; a big entry is named
437# in one ALARM line
438class5_cases() { # $1 spark-cleanup copy
439 local s=$1 h t L
440 h=$(mktemp -d "$top/t5.XXXX"); t="$h/tmp"
441 mkdir -p "$h/git" "$h/handoffs" "$h/src" "$t/junk" "$t/plain"
442 : > "$h/handoffs/BOARD.md"
443 git init -q -b main "$h/src/c5"
444 head -c 300000 /dev/urandom > "$h/src/c5/big"; git -C "$h/src/c5" add big; git -C "$h/src/c5" commit -qm big
445 git clone -q --bare "$h/src/c5" "$h/git/c5repo.git"; rm -rf "$h/src"
446 git clone -q "$h/git/c5repo.git" "$t/saved"
447 git clone -q "$h/git/c5repo.git" "$t/unsaved"
448 echo u > "$t/unsaved/u"; git -C "$t/unsaved" add u; git -C "$t/unsaved" commit -qm u
449 git clone -q "$h/git/c5repo.git" "$t/plain/sub"; echo mine > "$t/plain/sub/notes.txt"
450 git clone -q "$h/git/c5repo.git" "$h/main"; git -C "$h/main" worktree add -q "$t/lockedwt" -b lk
451 git -C "$h/main" worktree lock "$t/lockedwt"
452 echo j > "$t/junk/j"
453 find "$t" -exec touch -h -d '5 days ago' {} +
454 : > "$h/inuse"
455 HOME=$h SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_OFFLINE=1 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
456 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_DRY_CLASSES= SPARK_CLEANUP_TEST_INUSE_FILE=$h/inuse \
457 SPARK_CLEANUP_TEST_TMPROOT=$t SPARK_CLEANUP_BIG_ALARM=200000 SPARK_CLEANUP_TMP_MAX=1 \
458 bash "$s" --apply --classes 5 > "$h/out.txt" 2>&1
459 L=$h/out.txt
460 check c5-hook "$(has "$L" "test hook: class 5 reads $t instead of /tmp"; echo $?)"
461 check c5-unsaved "$( [ -f "$t/unsaved/u" ] && git -C "$t/unsaved" cat-file -e HEAD && has "$L" "class5 SKIP unsaved \(ref [0-9a-f]+ of $t/unsaved not in ~/git\) .*$t/unsaved$" && ! has "$L" "class5 REMOVED .*$t/unsaved "; echo $?)"
462 check c5-nested "$( [ -f "$t/plain/sub/notes.txt" ] && has "$L" "class5 SKIP unsaved \(.*$t/plain/sub\) .*$t/plain$"; echo $?)"
463 check c5-saved "$( [ ! -e "$t/saved" ] && has "$L" "class5 REMOVED .*$t/saved " && ! has "$L" 'over-cap'; echo $?)"
464 check c5-junk "$( [ ! -e "$t/junk" ] && has "$L" "class5 REMOVED .*$t/junk "; echo $?)"
465 check c5-bigalarm "$( [ "$(grep -c 'ALARM entries over' "$L")" = 1 ] && has "$L" "ALARM entries over .*$t/saved [0-9.]+G \(class5 removed\)" && has "$L" "ALARM entries over .*$t/unsaved [0-9.]+G \(class5 kept: unsaved\)" && ! has "$L" "ALARM entries over .*$t/junk "; echo $?)"
466 check c5-locked "$( [ -f "$t/lockedwt/.git" ] && has "$L" "class5 SKIP locked $t/lockedwt$" && ! has "$L" "class5 REMOVED .*$t/lockedwt " && git -C "$h/main" worktree list | grep -q "$t/lockedwt"; echo $?)"
467}
468
469suite() { # $1 spark-cleanup copy; sets pass/fail
470 local s=$1 h
471 h=$(mktemp -d "$top/h.XXXX"); setup "$h"
472 # f5: no scratch folder (TMPDIR missing): loud, nonzero, nothing removed
473 TMPDIR="$h/no-such-dir" run "$h" "$s" --apply --classes 2; local mk_rc=$?
474 local mk_ok=1; [ "$mk_rc" != 0 ] && has "$h/out.txt" 'ALARM cannot create a scratch folder' && ! has "$h/out.txt" 'REMOVED' && [ -d "$h/workspace/wt-store" ] && mk_ok=0
475 run "$h" "$s" --classes 2,8
476 local dry="$h/out.txt"; cp "$dry" "$h/dry.txt"
477 [ ! -s "$h/handoffs/BOARD.md" ]; local dryboard=$?
478 run "$h" "$s" --apply --classes 2,8
479 local o="$h/out.txt"; cp "$o" "$h/apply1.txt"
480 run "$h" "$s" --apply --classes 8
481 local o2="$h/apply2.txt"; cp "$h/out.txt" "$o2"
482 SPARK_CLEANUP_LEFTOVER_ALARM_GB=0.5 run "$h" "$s" --classes 8
483 local o3="$h/out.txt"; cp "$o3" "$h/badthr.txt"
484 # c8-nolock: fake quiet-build records the env it ran with, then runs the command
485 mkdir -p "$h/.local/bin"
486 printf '#!/bin/bash\necho "qb SKIP8=${SPARK_CLEANUP_SKIP8:-} $(date +%%s%%N)" >> "%s/qb.log"\n"$@"\nrc=$?\necho "qb-end $(date +%%s%%N)" >> "%s/qb.log"\nexit $rc\n' "$h" "$h" > "$h/.local/bin/quiet-build"
487 chmod +x "$h/.local/bin/quiet-build"
488 HOME=$h SPARK_CLEANUP_TEST_FREE_PCT=50 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
489 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_DRY_CLASSES= bash "$s" --apply --classes 2,8 > "$h/nolock.txt" 2>&1
490 # store-empty: a separate scratch home whose store is empty
491 local e; e=$(mktemp -d "$top/e.XXXX"); mkdir -p "$e/git" "$e/workspace" "$e/handoffs"; : > "$e/handoffs/BOARD.md"
492 HOME=$e SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_GITSTORE=$e/git SPARK_CLEANUP_BOARD=$e/handoffs/BOARD.md bash "$s" --apply --classes 2,8 > "$e/out.txt" 2>&1
493 HOME=$e SPARK_CLEANUP_QUIET=1 SPARK_CLEANUP_GITSTORE=$e/git SPARK_CLEANUP_BOARD=$e/handoffs/BOARD.md bash "$s" --apply --classes 2,8 > "$e/out2.txt" 2>&1
494 # c8-unfin: a scan that started and never wrote its finish mark
495 echo 1700000000 > "$h/.local/state/spark-cleanup/class8.started"; rm -f "$h/.local/state/spark-cleanup/class8.finished"
496 SPARK_CLEANUP_CLASS8_MAX_H=1.5 run "$h" "$s" --apply --classes 8; cp "$h/out.txt" "$h/unfin.txt"
497 # c8-drymark: a dry run with an unfinished --apply mark left in place
498 echo 1700000001 > "$h/.local/state/spark-cleanup/class8.started"; rm -f "$h/.local/state/spark-cleanup/class8.finished"
499 local bd0; bd0=$(wc -l < "$h/handoffs/BOARD.md")
500 run "$h" "$s" --classes 8; cp "$h/out.txt" "$h/drymark.txt"
501 local dm_ok=1; [ "$(cat "$h/.local/state/spark-cleanup/class8.started")" = 1700000001 ] && [ "$(wc -l < "$h/handoffs/BOARD.md")" = "$bd0" ] && ! has "$h/drymark.txt" 'did not finish' && dm_ok=0
502 cp "$h/.local/state/spark-cleanup/class8.started" "$h/.local/state/spark-cleanup/class8.finished" # leave no unfinished mark for later cases
503 # c8-only8: --apply --classes 8 through the real wrapper (fake quiet-build present)
504 local qb0; qb0=$(wc -l < "$h/qb.log" 2>/dev/null || echo 0)
505 HOME=$h SPARK_CLEANUP_TEST_FREE_PCT=50 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
506 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_DRY_CLASSES= bash "$s" --apply --classes 8 > "$h/only8.txt" 2>&1
507 local qb1; qb1=$(wc -l < "$h/qb.log" 2>/dev/null || echo 0)
508 bash "$s" --help > "$h/help.txt" 2>&1
509 # c8-emerg: under 10 % free the deleting classes run without quiet-build; class 8 still runs alone after them
510 local qbn; qbn=$(wc -l < "$h/qb.log" 2>/dev/null || echo 0)
511 HOME=$h SPARK_CLEANUP_TEST_FREE_PCT=5 SPARK_CLEANUP_GITSTORE=$h/git SPARK_CLEANUP_BOARD=$h/handoffs/BOARD.md \
512 SPARK_CLEANUP_PROTECT=none-protected SPARK_CLEANUP_DRY_CLASSES= bash "$s" --apply --classes 2,8 > "$h/emerg.txt" 2>&1
513 run "$h" "$s" --apply --classes 1; cp "$h/out.txt" "$h/class1.txt"
514 o="$h/apply1.txt"
515 check c2-store "$( [ ! -d "$h/workspace/wt-store" ] && has "$o" 'class2 REMOVED .*wt-store'; echo $?)"
516 check c2-remote "$( [ -d "$h/workspace/wt-remote" ] && has "$o" 'class2 SKIP not-in-git-store .*wt-remote'; echo $?)"
517 check c8-left "$(has "$o" 'class8 ALARM leftover-copy .*/work/lane/left '; echo $?)"
518 check c8-unst "$(has "$o" 'class8 ALARM unstored-copy .*/work/lane/unst '; echo $?)"
519 check c8-dirty "$(has "$o" 'class8 ALARM dirty-copy .*/work/lane/dirty '; echo $?)"
520 check c8-model "$(has "$o" 'class8 ALARM model-copy .*m\.safetensors'; echo $?)"
521 check c8-fresh "$( ! has "$o" 'class8 ALARM .*/work/lane/fresh'; echo $?)"
522 check c8-wt-own "$(has "$h/dry.txt" 'class8 ALARM leftover-copy .*/workspace/wt-store '; echo $?)"
523 check c8-rerun "$(has "$h/dry.txt" 'class8 ALARM leftover-copy .*/work/lane/left ' && has "$o" 'class8 ALARM leftover-copy .*/work/lane/left '; echo $?)"
524 check c8-board "$( [ "$dryboard" = 0 ] && [ "$(grep -c 'ALARM spark-cleanup class8 to all: [0-9]* agent copies left behind' "$h/handoffs/BOARD.md")" = 1 ]; echo $?)"
525 check c8-stash "$(has "$o" 'class8 ALARM unstored-copy .*/work/lane/stash '; echo $?)"
526 check c8-ignored "$(has "$o" 'class8 ALARM dirty-copy .*/work/lane/ign .*ignored files'; echo $?)"
527 check c8-unread "$(has "$o" 'class8 ALARM unreadable-copy .*/work/lane/orphan '; echo $?)"
528 check c8-nested "$(has "$o" 'class8 ALARM leftover-copy .*/work/lane/nested '; echo $?)"
529 check c8-hardlink "$( ! has "$o" 'model-copy .*linked\.gguf'; echo $?)"
530 check c8-dedupe "$(has "$o2" 'board line not repeated' && [ "$(grep -c 'ALARM spark-cleanup class8 to all: [0-9]* agent copies left behind' "$h/handoffs/BOARD.md")" = 1 ]; echo $?)"
531 check c8-badthr "$(has "$h/badthr.txt" "class8 ALARM bad SPARK_CLEANUP_LEFTOVER_ALARM_GB='0.5'" && ! has "$h/badthr.txt" 'syntax error|arithmetic'; echo $?)"
532 check c8-nolock "$(grep -q '^qb SKIP8=1 ' "$h/qb.log" 2>/dev/null && has "$h/nolock.txt" 'APPLY classes=2 ' && has "$h/nolock.txt" 'APPLY classes=8 ' && ! has "$h/nolock.txt" 'APPLY classes=2,8'; echo $?)"
533 check store-empty "$(has "$e/out.txt" 'holds no bare repositories' && has "$e/out2.txt" 'holds no bare repositories' && [ "$(grep -c 'holds no bare repositories' "$e/handoffs/BOARD.md")" = 1 ]; echo $?)"
534 check c8-emerg "$(has "$h/emerg.txt" 'APPLY classes=2 ' && has "$h/emerg.txt" 'APPLY classes=8 ' && ! has "$h/emerg.txt" 'APPLY classes=2,8' && [ "$(wc -l < "$h/qb.log")" = "$qbn" ]; echo $?)"
535 check c8-main "$(has "$h/dry.txt" 'class8 SKIP main-clone .*/workspace/ref ' && has "$h/dry.txt" 'class8 ALARM unstored-copy .*/workspace/proj .*main clone:' && ! has "$h/dry.txt" 'class8 ALARM [a-z]+-copy .*/workspace/ref '; echo $?)"
536 check c8-drymark "$dm_ok"
537 check c2-nested "$( [ -d "$h/workspace/wt-nest" ] && has "$o" 'class2 SKIP nested-repo .*wt-nest'; echo $?)"
538 check c2-untracked "$( [ -f "$h/workspace/wt-untracked/notes.txt" ] && [ "$(cat "$h/workspace/wt-untracked/notes.txt")" = notes ] && has "$o" 'class2 SKIP unsaved .*wt-untracked' && ! has "$o" 'class2 REMOVED .*wt-untracked' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-untracked '; echo $?)"
539 check c2-ignored "$( [ -f "$h/workspace/wt-ignored/secret/z" ] && [ "$(cat "$h/workspace/wt-ignored/secret/z")" = z ] && has "$o" 'class2 SKIP unsaved .*wt-ignored' && ! has "$o" 'class2 REMOVED .*wt-ignored' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-ignored '; echo $?)"
540 check c2-skipwt "$( [ -f "$h/workspace/wt-skip/a" ] && grep -q edited "$h/workspace/wt-skip/a" && has "$o" 'class2 SKIP unsaved .*wt-skip' && ! has "$o" 'class2 REMOVED .*wt-skip' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-skip '; echo $?)"
541 check c2-assume "$( [ -f "$h/workspace/wt-assume/a" ] && grep -q edited "$h/workspace/wt-assume/a" && has "$o" 'class2 SKIP unsaved .*wt-assume' && ! has "$o" 'class2 REMOVED .*wt-assume' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-assume '; echo $?)"
542 check c2-bare "$( [ -d "$h/workspace/wt-bare/.lake/packages/dep.git" ] && git -C "$h/workspace/wt-bare/.lake/packages/dep.git" cat-file -e HEAD && has "$o" 'class2 SKIP nested-repo .*wt-bare' && ! has "$o" 'class2 REMOVED .*wt-bare' && has "$o" 'class8 ALARM unstored-copy .*/workspace/wt-bare ' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-bare '; echo $?)"
543 check c8-nestbad "$( has "$o" 'class8 ALARM unstored-copy .*/work/lane/outer ' && ! has "$o" 'class8 ALARM leftover-copy .*/work/lane/outer ' && [ -f "$h/work/lane/outer/build/mywork/w" ] && has "$o" 'class8 ALARM unstored-copy .*/workspace/wt-nest ' && ! has "$o" 'class8 ALARM leftover-copy .*/workspace/wt-nest ' && [ -d "$h/workspace/wt-nest/.lake/packages/dep" ] && has "$o" 'class8 ALARM unstored-copy .*/work/lane/super ' && ! has "$o" 'class8 ALARM leftover-copy .*/work/lane/super ' && [ -f "$h/work/lane/super/dep/d" ]; echo $?)"
544 check c8-remoteref "$( has "$o" 'class8 ALARM unstored-copy .*/work/lane/rem ' && ! has "$o" 'class8 ALARM leftover-copy .*/work/lane/rem ' && [ -d "$h/work/lane/rem" ]; echo $?)"
545 check c8-fourbit "$( [ -f "$h/work/hive/uxl-brief-20261113/FourBit/target/ONLY-COPY.txt" ] && [ -f "$h/work/hive/uxl-brief-20261113/FourBit/keep.txt" ] && [ -d "$h/work/hive/uxl-brief-20261113/FourBit/checkout" ] && ! has "$h/class1.txt" 'REMOVED .*FourBit' && ! has "$o" 'leftover-copy .*FourBit' && [ ! -e "$h/handoffs/hive/factory-bays/job/target/rebuild.txt" ] && has "$h/class1.txt" 'class1 REMOVED .*factory-bays/job/target' && [ -f "$h/handoffs/hive/factory-bays/job/evidence/target/keep.txt" ] && has "$h/class1.txt" 'class1 SKIP protected .*evidence/target' && [ -f "$h/handoffs/keep.txt" ]; echo $?)"
546 check c8-inner "$( ! has "$o" 'class8 ALARM .*/work/lane/active'; echo $?)"
547 check c8-agentwt "$(has "$h/dry.txt" 'class8 ALARM [a-z]+-copy .*/workspace/proj/\.claude/worktrees/agent-x '; echo $?)"
548 check c8-dashwt "$(has "$h/dry.txt" 'class8 ALARM [a-z]+-copy .*/workspace/kflow '; echo $?)"
549 check c8-unfin "$(has "$h/unfin.txt" 'previous leftover-copy scan .*did not finish' && [ "$(grep -c 'did not finish' "$h/handoffs/BOARD.md")" = 1 ]; echo $?)"
550 check c8-maxh "$(has "$h/unfin.txt" "ALARM bad SPARK_CLEANUP_CLASS8_MAX_H='1.5'" && ! has "$h/unfin.txt" 'syntax error|arithmetic'; echo $?)"
551 check c8-only8 "$( [ "$qb0" = "$qb1" ] && has "$h/only8.txt" 'APPLY classes=8 '; echo $?)"
552 check c8-help "$(has "$h/help.txt" '^Usage|^ *spark-cleanup --classes' && ! has "$h/help.txt" 'pipefail'; echo $?)"
553 check c8-hook "$(has "$o" 'test hook: in-use list read from'; echo $?)"
554 check c8-nodel "$( [ -d "$h/work/lane/left" ] && [ -d "$h/work/lane/unst" ] && [ -d "$h/work/lane/dirty" ] && [ -f "$h/work/lane/weights/m.safetensors" ]; echo $?)"
555 check f1-bare-c2 "$( [ -d "$h/workspace/wt-barenose/.lake/packages/dep/objects" ] && has "$o" 'class2 SKIP nested-repo .*wt-barenose' && ! has "$o" 'class2 REMOVED .*wt-barenose' && has "$o" 'class8 ALARM unstored-copy .*/workspace/wt-barenose '; echo $?)"
556 check f1-bare-c8 "$(has "$o" 'class8 ALARM unstored-copy .*/work/lane/baren ' && [ -d "$h/work/lane/baren/.lake/packages/dep/objects" ]; echo $?)"
557 check f1-bare-c1 "$( [ -d "$h/handoffs/hive/factory-bays/job/cache-nosuffix/vendor/objects" ] && has "$h/class1.txt" 'class1 SKIP has-git .*cache-nosuffix'; echo $?)"
558 check f2-subbranch "$(has "$o" 'class8 ALARM unstored-copy .*/work/lane/super2 ' && ! has "$o" 'leftover-copy .*/work/lane/super2 '; echo $?)"
559 check f3-nestfile "$(has "$o" 'class8 ALARM dirty-copy .*/work/lane/nestfile .*nested repository has unsaved files' && [ -f "$h/work/lane/nestfile/.lake/packages/dep/notes.txt" ]; echo $?)"
560 check f4-mode "$( [ -x "$h/workspace/wt-mode/a" ] && has "$o" 'class2 SKIP unsaved .*wt-mode' && ! has "$o" 'class2 REMOVED .*wt-mode'; echo $?)"
561 check f5-mktemp "$mk_ok"
562 landed_cases "$s"
563 landed2_cases "$s"
564 class5_cases "$s"
565}
566
567suite "$sc"
568cases=$pass; real_fail=$fail
569# mutants: each must make the suite fail
570mutate() { # $1 name, $2 perl substitution
571 local m="$top/mut-$1"
572 perl -0pe "$2" "$sc" > "$m"
573 if cmp -s "$m" "$sc"; then echo "MUTANT $1 did not apply"; return 1; fi
574 pass=0; fail=0; quiet=1; suite "$m"; quiet=
575 if [ "$fail" -gt 0 ]; then echo "MUTANT $1 caught ($fail failing)"; return 0; fi
576 echo "MUTANT $1 NOT caught"; return 1
577}
578mt=0 mc=0
579for spec in \
580 'store-always-true|s/(in_git_store\(\) \{ # \$1 commit sha[^\n]*\n)/$1 return 0\n/' \
581 'old-pushed-check|s/if \[ -z "\$head" \] \|\| ! in_git_store "\$head" >\/dev\/null; then/if [ -z "\$(git -C "\$wt" branch -r --contains HEAD 2>\/dev\/null | head -1)" ] \&\& ! in_git_store "\$head" >\/dev\/null; then/' \
582 'no-idle-check|s/ recent "\$r" "\$idle_min" && continue\n//' \
583 'unreadable-is-leftover|s/kind=unreadable; why=/kind=leftover; why=/' \
584 'heads-only|s#for-each-ref --format=.%\(objectname\) %\(refname\).#for-each-ref --format="%(objectname)" refs/heads#' \
585 'no-dedupe|s/if \[ "\$sig" = "\$last" \]; then/if false; then/' \
586 'class8-in-lock|s/SPARK_CLEANUP_SKIP8=1 "\$QB"/"\$QB"/' \
587 'no-emerg-split|s/else SPARK_CLEANUP_SKIP8=1 "\$0" "\$@"; _rc=\$\?; fi/else exec "\$0" "\$@"; fi/' \
588 'no-inner|s/ if \[ \$inner = 1 \] && ! grep -qs [^\n]*; then continue; fi\n//' \
589 'main-clone|s/(is_main_clone\(\) \{\n)/$1 return 1\n/' \
590 'inner-wt|s/if \[ \$inner = 1 \] && ! grep -qs [^\n]*; then continue; fi/[ \$inner = 1 ] \&\& continue/' \
591 'main-by-name|s/ \[ -d "\$1\/\.git" \] \|\| return 1\n//' \
592 'no-unfinished|s/if \[ -n "\$_st" \] && \[ "\$\(cat "\$STATE\/class8\.finished" 2>\/dev\/null\)" != "\$_st" \]; then/if false; then/' \
593 'dry-marks|s/if \[ "\$CLASSES" = 8 \] && \[ \$APPLY = 1 \]; then/if [ "\$CLASSES" = 8 ]; then/' \
594 'no-nested-check|s/ if \[ -n "\$inner" \]; then log "class2 SKIP nested-repo[^\n]*\n//' \
595 'main-silent|s/main=0; is_main_clone "\$r" && main=1/is_main_clone "\$r" \&\& continue; main=0/' \
596 'bare-by-name|s/-o -type f -name HEAD -print 2>/-o -type f -path "*.git\/HEAD" -print 2>/' \
597 'wt-heads-only|s/ \[ "\$gd" != "\$cd" \] && return 0\n/ [ -f "\$1\/.git" ] \&\& return 0\n/' \
598 'nested-unsaved|s/ if why=\$\(copy_unsaved "\$d" nested\); then [^\n]*\n//' \
599 'no-mode-check|s/\{ echo "skip-worktree or assume-unchanged file executable bit differs from the saved copy \(\$path\)"; return 0; \}/true/' \
600 'mktemp-continue|s/ \[ -n "\$LANDED" \] && exit 0\n exit 1\nfi/fi/' \
601 'landed-idle|s/if ! idle_outside_git "\$r" \$\(\(LANDED_IDLE_H\*60\)\); then/if false; then/' \
602 'landed-inuse|s/if in_use "\$r"; then lwait/if false; then lwait/' \
603 'landed-claim|s/if claimed "\$r" "\$TMPD\/claims"; then/if false; then/' \
604 'claims-board-only|s/ "\$\{SPARK_CLEANUP_CLAIMS:-\$H\/handoffs\/hive\/CLAIMS\.md\}"//g' \
605 'claim-exact-only|s/ case "\$1\/" in "\$p"\/\*\) return 0 ;; esac\n/ [ "\$1" = "\$p" ] \&\& return 0\n/' \
606 'main-dash-name|s/ \[ -n "\$\{STORE_NAME\[\$n\]:-\}" \] && return 0\n//' \
607 'landed-unsaved-removed|s/if \[ "\$bad" = 0 \] && \[ -z "\$nest" \] && \[ -z "\$uns" \]; then landed_remove/if true; then landed_remove/' \
608 'landed-archive-recent|s/if ! idle_outside_git "\$r" \$\(\(LANDED_ARCHIVE_D\*1440\)\); then/if false; then/' \
609 'landed-no-bundle|s/ if \[ -n "\$head" \] && ! in_git_store "\$head"; then/ if false; then/' \
610 'c5-no-git-check|s/if \[ -d "\$d" \] && why=\$\(repos_unsaved "\$d"\); then/if false; then/' \
611 'c5-top-only|s/; each_nested_repo "\$1"\)\n return 1/)\n return 1/' \
612 'c5-cap-back|s/(\n remove_dir 5 )/\n sz=\$(size_of "\$d"); [ "\$sz" -gt "\${SPARK_CLEANUP_TMP_MAX:-5368709120}" ] \&\& { log "class5 SKIP over-cap \$d"; continue; }$1/' \
613 'no-big-alarm|s/\n\[ \$\{#BIG\[@\]\} -gt 0 \] && log/\n[ 0 = 1 ] \&\& log/' \
614 'index-write|s/export GIT_OPTIONAL_LOCKS=0\n//' \
615 'no-board|s/ "\$\(date -u \+%FT%H:%MZ\)" "\$n" "\$\(human "\$bytes"\)" "\$LOG" >> "\$board"/ "x" "\$n" "x" "x" >\/dev\/null/' \
616 'root-claim-used|s/if \[ "\$root" = 1 \]; then printf/if false; then printf/' \
617 'release-ignored|s/gone = rn\[j\] > cn\[i\]/gone = 0/' \
618 'release-before-claim|s/gone = rn\[j\] > cn\[i\]/gone = 1/' \
619 'release-any-lane|s/if \(cl\[i\] == "" \|\| rl\[j\] != cl\[i\]\) continue/if (cl[i] == "") continue/' \
620 'pending-unread|s/grep -qxF -- "\$r" "\$pending"; then via=/false; then via=/' \
621 'build-in-archive|s/(build_output\(\) \{ # \$1 copy\n)/$1 return 0\n/' \
622 'no-room-check|s/ \|\| \[ "\$room" -lt "\$need" \]; then/; then/' \
623 'claim-file-missing|s/if \[ -r "\$f" \]; then files\+=/if true; then files+=/' \
624 'class2-claim-ignored|s/if claimed "\$wt" "\$TMPD\/c2claims"; then/if false; then/' \
625 'alarm-reads-as-claim|s/to all: a claim line names/to all: a CLAIM line names/' \
626 'c5-lock-ignored|s/log "class5 SKIP locked \$d"; continue;/:;/' ; do
627 name=${spec%%|*}; expr=${spec#*|}
628 mt=$((mt+1)); mutate "$name" "$expr" && mc=$((mc+1))
629done
630if [ "$real_fail" = 0 ] && [ "$mc" = "$mt" ]; then echo "TEST PASS cases=$cases mutants=$mc/$mt"; exit 0; fi
631echo "TEST FAIL failing=$real_fail mutants=$mc/$mt"; exit 1