gunk-dev/the-valley

run.sh

sourceb70056c25abamain's published tip history
25.2 KiB
1#!/usr/bin/env bash
2# The integrator, driven end to end over scratch repositories and throwaway
3# keys. It runs the real binaries: `attest` produces the evidence, the
4# `valley` deriver composes the policy, and the `integrator` judges and
5# lands. Only the nix evaluator is stood in for (integrator/e2e/nix), and
6# only because a nix build has no daemon to call.
7#
8# Each scenario below is one of the phase's exit criteria, observed rather
9# than asserted about. Nothing here touches a repository outside its own
10# scratch directory, and no ref is ever created anywhere but there.
11set -euo pipefail
12
13work="$(mktemp -d)"
14trap 'rm -rf "$work"' EXIT
15here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
17export HOME="$work"
18export GIT_CONFIG_NOSYSTEM=1
19# Scratch goes where the run puts it, as it does under the unit, so what
20# a pass leaves behind is observable rather than scattered through /tmp.
21export TMPDIR="$work/tmp"
22mkdir -p "$TMPDIR"
23export VALLEY_FAKE_STORE="$work/store"
24export PATH="$here:$PATH" # the nix stand-ins, ahead of anything real
25
26git config --global user.name valley-check
27git config --global user.email valley-check@localhost
28git config --global init.defaultBranch main
29git config --global advice.detachedHead false
30
31say() { printf '\n=== %s\n' "$*"; }
32die() {
33 printf '\nintegrator-e2e: %s\n' "$*" >&2
34 exit 1
35}
36holds() { printf ' ok: %s\n' "$*"; }
37
38# ----------------------------------------------------------------------
39say "keys: a contributor, an integrator, and a stranger"
40
41for who in contributor integrator stranger; do
42 ssh-keygen -q -t ed25519 -N "" -C "$who" -f "$work/$who"
43done
44contributor=contributor.valley.invalid/attestations
45integrator_name=integrator.valley.invalid/attestations
46stranger=stranger.elsewhere.invalid/attestations
47
48# The known-signers file is the registry's interim compilation
49# (dcr-b87f6e8): the integrator accepts the contributor and nobody else.
50attest key --key "$work/contributor" --name "$contributor" > "$work/known_signers"
51# What a reader of a landed note holds: both parties to it.
52cat "$work/known_signers" > "$work/reader_keys"
53attest key --key "$work/integrator" --name "$integrator_name" >> "$work/reader_keys"
54attest key --key "$work/stranger" --name "$stranger" > "$work/stranger_key"
55# The key hash the integrator's own attestation refs are keyed by.
56ihash="$(attest key --key "$work/integrator" --name "$integrator_name" | cut -d+ -f2)"
57holds "the integrator holds one signer: $(cut -d+ -f1 < "$work/known_signers")"
58
59# ----------------------------------------------------------------------
60say "the served repository, and the instance policy layer"
61
62origin="$work/project.git"
63git init --quiet --bare "$origin"
64repo="$work/project"
65git init --quiet "$repo"
66cd "$repo"
67git remote add origin "$origin"
68
69mkdir -p docs src deploy checks policy
70echo "the readme" > docs/readme.md
71echo "the source" > src/main.txt
72echo "the deployment" > deploy/target.txt
73# What each check reads, which the nix stand-in turns into its closure.
74echo docs > checks/prose-format.inputs
75echo src > checks/code-build.inputs
76
77# The project layer: it declares its own classes and checks above the
78# floor, and declines nothing.
79cat > policy/project.cue <<'EOF'
80package verification
81
82project: {
83 checks: {
84 "prose-format": {runner: "nix", attribute: "prose-format"}
85 "code-build": {runner: "nix", attribute: "code-build"}
86 // An effectful check, with the validity window dcr-439b771 calls
87 // Δ. Its observation transfers while nothing in the class that
88 // requires it has landed under it and it is younger than this.
89 reachable: {runner: "command", command: "true", validity: "1h"}
90 }
91 classes: {
92 prose: {paths: "docs/**": true, requires: "prose-format": true}
93 code: {paths: "src/**": true, requires: "code-build": true}
94 deploy: {paths: "deploy/**": true, requires: reachable: true}
95 }
96 unclassified: {}
97}
98EOF
99
100git add -A
101git commit --quiet -m "seed the project"
102git push --quiet origin main
103
104# The instance layer lives outside the project's tree, and the integrator
105# reads it from there: a change never supplies the policy that gates it.
106instance="$work/instance"
107mkdir -p "$instance"
108cat > "$instance/floor.cue" <<'EOF'
109package verification
110
111floor: {
112 checks: {}
113 classes: {}
114 unclassified: {}
115}
116EOF
117holds "policy composes from $instance and the project's own policy/"
118
119# ----------------------------------------------------------------------
120# Helpers.
121
122# attest_change <branch> <check spec…> — run the checks over the branch's
123# tip and publish the branch with its attestation ref, atomically.
124attest_change() {
125 local branch="$1" key="${2:-$work/contributor}" name="${3:-$contributor}"
126 shift 3
127 attest run --repo "$repo" --key "$key" --name "$name" \
128 --push origin --branch "$branch" "$@"
129}
130
131# request <change> <target> <commit> — the contributor asks for integration
132# by pushing one ref. That ref is the whole of the request.
133request() {
134 git push --quiet origin "$3:refs/the-valley/integration-requests/$2/$1"
135}
136
137integrate() {
138 integrator reconcile \
139 --repo "$origin" \
140 --key "$work/integrator" --name "$integrator_name" \
141 --known-signers "$work/known_signers" \
142 --instance "$instance" --project-policy policy \
143 --schema "$SCHEMA_VERIFICATION" \
144 --attest-schema "$SCHEMA_ATTESTATION" \
145 --event-schema "$SCHEMA_EVENTS" \
146 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
147 2>&1 | tee "$work/last.out"
148 grep -q . "$work/last.out" || die "the integrator said nothing"
149}
150
151tip() { git -C "$origin" rev-parse refs/heads/main; }
152forget() { git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$1"; }
153
154# ----------------------------------------------------------------------
155say "1. a change with valid transferring evidence lands"
156
157before="$(tip)"
158git checkout --quiet -b c1
159echo "a better readme" > docs/readme.md
160echo "a new target" > deploy/second.txt
161git add -A
162git commit --quiet -m "improve the readme and add a target"
163attest_change c1 "" "" --check prose-format --command reachable=true
164request c1 main "$(git rev-parse c1)"
165integrate
166
167grep -q "^c1 -> refs/heads/main .*: land$" "$work/last.out" || die "c1 did not land"
168grep -q "check prose-format .*transferred" "$work/last.out" || die "prose-format did not transfer"
169grep -q "check reachable .*transferred (untouched)" "$work/last.out" || die "the effectful check did not transfer"
170grep -q '"event":"integration-succeeded"' "$work/last.out" || die "no integration-succeeded event"
171[ "$(tip)" != "$before" ] || die "the protected ref did not move"
172[ "$(tip)" = "$(git rev-parse c1)" ] || die "main is not at the change's head"
173git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c1 > /dev/null \
174 && die "the request ref outlived the landing"
175holds "main moved $(git rev-parse --short "$before") -> $(git -C "$origin" rev-parse --short refs/heads/main)"
176
177# The landed note carries both signatures, under one text, and verifies.
178# The integrator writes under its own key hash, so its ref never races the
179# contributor's over the same subject.
180landed="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c1 | cut -d: -f2)/$ihash"
181git -C "$origin" rev-parse --verify --quiet "$landed" > /dev/null \
182 || die "no attestation ref by the integrator for the landed tree"
183git -C "$origin" cat-file blob "$landed:prose-format/statement.note" > "$work/landed.note"
184siblings="$(grep -c '^— ' "$work/landed.note")"
185[ "$siblings" -eq 2 ] || die "the landed note carries $siblings signatures, not two"
186attest verify --note "$work/landed.note" --known-keys "$work/reader_keys" \
187 --schema "$SCHEMA_ATTESTATION" --repo "$repo" --rev c1 \
188 --signer "$contributor" --signer "$integrator_name" > "$work/landed.verify"
189grep -q 'matches the recorded subject digest' "$work/landed.verify" || die "the landed note does not bind to the tree"
190holds "the landed note carries contributor and integrator sibling signatures and verifies"
191
192# And the integrator's own transfer statement sits beside it.
193git -C "$origin" cat-file blob "$landed:transfer.note" > "$work/transfer.note"
194head -n1 "$work/transfer.note" | grep -qx 'the-valley/attestation/v1' || die "the transfer note is not a statement"
195grep -qx 'predicateType the-valley/integration/transfer/v1' "$work/transfer.note" || die "wrong predicate type"
196grep -qx 'predicate.change c1' "$work/transfer.note" || die "the transfer statement names no change"
197grep -q '^predicate.policy.sha256 ' "$work/transfer.note" || die "the transfer statement names no policy"
198attest verify --note "$work/transfer.note" --known-keys "$work/reader_keys" \
199 --schema "$SCHEMA_ATTESTATION" --signer "$integrator_name" > /dev/null
200holds "the transfer statement records the verdict and the policy it was derived under"
201
202# ----------------------------------------------------------------------
203say "2. an intervening landing invalidates one check's closure"
204
205git checkout --quiet main
206git pull --quiet --ff-only origin main
207
208# c2 is authored against the current tip and reads both classes.
209git checkout --quiet -b c2
210echo "a readme with more in it" > docs/readme.md
211echo "the source, revised" > src/main.txt
212git add -A
213git commit --quiet -m "revise the readme and the source"
214attest_change c2 "" "" --check prose-format --check code-build
215c2="$(git rev-parse c2)"
216
217# Meanwhile another change lands, touching only what code-build reads.
218git checkout --quiet main
219git checkout --quiet -b intervening
220echo "a second source file" > src/other.txt
221git add -A
222git commit --quiet -m "add a second source file"
223attest_change intervening "" "" --check code-build
224request intervening main "$(git rev-parse intervening)"
225integrate
226grep -q "^intervening -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
227holds "the intervening change landed first; c2's base has moved"
228
229request c2 main "$c2"
230integrate
231grep -q "^c2 -> refs/heads/main .*: stale$" "$work/last.out" || die "c2 was not stale"
232grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
233 || die "prose-format should have transferred by closure equality"
234grep -q "check code-build .*input closure changed" "$work/last.out" \
235 || die "code-build's closure should have moved"
236grep -q '"event":"request-stale"' "$work/last.out" || die "no request-stale event"
237grep -q '"checks":\["code-build"\]' "$work/last.out" || die "the event does not name exactly code-build"
238git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c2 > /dev/null \
239 || die "the request ref was dropped; there is nothing to resubmit"
240holds "one request-stale naming exactly code-build; prose-format reported transferred"
241forget c2
242
243# ----------------------------------------------------------------------
244say "2b. evidence that survives the rebase lands on the moved tip"
245
246# The same shape as 2, with the intervening landing outside what the
247# change's checks read. The tree that lands is not the tree that was
248# attested, and the evidence stands anyway — which is the whole of what
249# content-addressed evidence buys.
250git checkout --quiet main
251git pull --quiet --ff-only origin main
252git checkout --quiet -b c2b
253echo "a readme revised again" > docs/readme.md
254git add -A
255git commit --quiet -m "revise the readme once more"
256attest_change c2b "" "" --check prose-format
257c2b="$(git rev-parse c2b)"
258attested="$(attest digest --repo "$repo" --rev c2b | cut -d: -f2)"
259
260git checkout --quiet main
261git checkout --quiet -b elsewhere
262echo "a third source file" > src/third.txt
263git add -A
264git commit --quiet -m "add a third source file"
265attest_change elsewhere "" "" --check code-build
266request elsewhere main "$(git rev-parse elsewhere)"
267integrate
268grep -q "^elsewhere -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
269
270request c2b main "$c2b"
271integrate
272grep -q "^c2b -> refs/heads/main .*: land$" "$work/last.out" || die "c2b did not land"
273grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
274 || die "the evidence did not transfer by closure equality"
275landedtree="$(git -C "$origin" rev-parse refs/heads/main^{tree})"
276[ "$(git -C "$origin" rev-parse "refs/heads/main^")" = "$(git rev-parse elsewhere)" ] \
277 || die "the landing is not a fast-forward from the tip it was judged against"
278[ "$(git -C "$origin" rev-parse refs/heads/main^{tree})" != "$(git rev-parse "$c2b^{tree}")" ] \
279 || die "the tree did not actually move; the scenario proves nothing"
280holds "the delta landed on a tree it was never attested over, and no check re-ran"
281
282# Two attestation refs now, because the two statements are about two
283# different trees: the contributor's is keyed by the tree it was made over,
284# and the integrator's transfer statement by the tree that landed.
285git fetch --quiet origin main
286landed_digest="$(attest digest --repo "$repo" --rev "$(git -C "$origin" rev-parse refs/heads/main)" | cut -d: -f2)"
287[ "$landed_digest" != "$attested" ] || die "the landed tree is the attested tree; the scenario proves nothing"
288git -C "$origin" cat-file blob \
289 "refs/the-valley/attestations/$attested/$ihash:prose-format/statement.note" > "$work/rebased.note"
290[ "$(grep -c '^— ' "$work/rebased.note")" -eq 2 ] \
291 || die "the countersigned note lost a sibling signature"
292grep -qx "subject.digest.valley-tree-v1 $attested" "$work/rebased.note" \
293 || die "the countersigned statement is not about the tree it was made over"
294git -C "$origin" cat-file blob \
295 "refs/the-valley/attestations/$landed_digest/$ihash:transfer.note" > "$work/rebased.transfer"
296grep -qx "subject.digest.valley-tree-v1 $landed_digest" "$work/rebased.transfer" \
297 || die "the transfer statement is not about the landed tree"
298grep -qx "predicate.base.valley-tree-v1 $attested" "$work/rebased.transfer" \
299 || die "the transfer statement does not cite the base the evidence was produced over"
300grep -qx "predicate.checks.0.rule closure" "$work/rebased.transfer" \
301 || die "the transfer statement does not record the rule that carried the evidence"
302holds "the contributor's statement stays about its own tree; the transfer statement names the landed one"
303
304# ----------------------------------------------------------------------
305say "3. a conflicting delta is stale, and demands full re-attestation"
306
307git checkout --quiet main
308git pull --quiet --ff-only origin main
309git checkout --quiet -b c3
310echo "the readme, as c3 would have it" > docs/readme.md
311git add -A
312git commit --quiet -m "rewrite the readme one way"
313attest_change c3 "" "" --check prose-format
314c3="$(git rev-parse c3)"
315
316git checkout --quiet main
317git checkout --quiet -b conflicting
318echo "the readme, as the other change would have it" > docs/readme.md
319git add -A
320git commit --quiet -m "rewrite the readme another way"
321attest_change conflicting "" "" --check prose-format
322request conflicting main "$(git rev-parse conflicting)"
323integrate
324grep -q "^conflicting -> refs/heads/main .*: land$" "$work/last.out" || die "the conflicting change did not land"
325
326request c3 main "$c3"
327integrate
328grep -q "^c3 -> refs/heads/main .*: stale$" "$work/last.out" || die "c3 was not stale"
329grep -q 'resolving it is new authorship' "$work/last.out" || die "the conflict was not reported as new authorship"
330grep -q 'CONFLICT (content): Merge conflict in docs/readme.md' "$work/last.out" \
331 || die "the conflict does not name what could not be merged"
332grep -q '"reason":"conflict"' "$work/last.out" || die "the event does not say conflict"
333grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "a conflict must re-demand every required check"
334holds "a conflict is stale, and every required check is re-demanded"
335forget c3
336
337# ----------------------------------------------------------------------
338say "4. an unknown signer's attestation does not transfer"
339
340git checkout --quiet main
341git pull --quiet --ff-only origin main
342git checkout --quiet -b c4
343echo "a readme signed by a stranger" > docs/readme.md
344git add -A
345git commit --quiet -m "a change a stranger attested"
346attest_change c4 "$work/stranger" "$stranger" --check prose-format
347request c4 main "$(git rev-parse c4)"
348integrate
349grep -q "^c4 -> refs/heads/main .*: stale$" "$work/last.out" || die "c4 was not stale"
350grep -q 'no signer the integrator holds' "$work/last.out" || die "the unknown signer was not reported"
351grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "the event does not name prose-format"
352[ "$(tip)" = "$(git -C "$origin" rev-parse refs/heads/main)" ] || die "main moved"
353holds "evidence from a signer the integrator does not hold does not transfer"
354forget c4
355
356# ----------------------------------------------------------------------
357say "5. a tampered statement is rejected, not called stale"
358
359git checkout --quiet main
360git checkout --quiet -b c5
361echo "an honest readme" > docs/readme.md
362git add -A
363git commit --quiet -m "an honest change"
364attest_change c5 "" "" --check prose-format
365c5="$(git rev-parse c5)"
366c5digest="$(attest digest --repo "$repo" --rev c5 | cut -d: -f2)"
367
368# Edit one value in the signed text and store the result where the real
369# note was. Everything else about the note is untouched.
370c5ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
371 "refs/the-valley/attestations/$c5digest" | head -n1)"
372git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note" \
373 | sed 's/^predicate.result passed$/predicate.result failed/' > "$work/tampered.note"
374cmp -s "$work/tampered.note" <(git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note") \
375 && die "the tamper changed nothing; the test proves nothing"
376blob="$(git -C "$origin" hash-object -w --stdin < "$work/tampered.note")"
377sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$origin" mktree)"
378top="$(printf '040000 tree %s\tprose-format\n' "$sub" | git -C "$origin" mktree)"
379git -C "$origin" update-ref "$c5ref" "$top"
380
381request c5 main "$c5"
382integrate
383grep -q "^c5 -> refs/heads/main .*: reject$" "$work/last.out" || die "a tampered statement was not rejected"
384grep -q 'does not verify as the statement it claims to be' "$work/last.out" || die "the refusal was not reported"
385grep -q 'request-stale' "$work/last.out" && die "a forgery was announced as staleness"
386holds "a tampered statement is a rejection, and no outcome event claims otherwise"
387forget c5
388
389# ----------------------------------------------------------------------
390say "6. an attestation about a different tree is rejected"
391
392git checkout --quiet main
393git checkout --quiet -b c6
394echo "the tree that was actually attested" > docs/readme.md
395git add -A
396git commit --quiet -m "the attested change"
397attest_change c6 "" "" --check prose-format
398c6ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
399 "refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c6 | cut -d: -f2)" | head -n1)"
400
401# A second, different tree — with no attestation of its own — and the
402# first tree's valid, correctly signed note filed under its digest.
403git checkout --quiet -b c6-lifted
404echo "the tree that was not" > docs/readme.md
405git add -A
406git commit --quiet -m "the change the evidence was lifted onto"
407git push --quiet origin c6-lifted
408lifted="$(attest digest --repo "$repo" --rev c6-lifted | cut -d: -f2)"
409git -C "$origin" update-ref \
410 "refs/the-valley/attestations/$lifted/${c6ref##*/}" \
411 "$(git -C "$origin" rev-parse "$c6ref^{tree}")"
412
413request c6 main "$(git rev-parse c6-lifted)"
414integrate
415grep -q "^c6 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lifted attestation was not rejected"
416grep -q 'tree digest mismatch' "$work/last.out" || die "the refusal does not name the tree digest"
417holds "a signature cannot be lifted onto a tree it was not made about"
418forget c6
419
420# ----------------------------------------------------------------------
421say "7. an attestation lying about its kind is rejected"
422
423# The policy runs prose-format with nix, so its evidence is content-
424# addressed. This attestation claims a notarization instead, and its class
425# was untouched by the intervening landing — so under the rule it nominated
426# for itself it would sail onto a base it was never produced over.
427git checkout --quiet main
428git pull --quiet --ff-only origin main
429git checkout --quiet -b c7
430echo "a readme attested as a notarization" > docs/readme.md
431git add -A
432git commit --quiet -m "a change whose attestation lies about its kind"
433attest_change c7 "" "" --command prose-format=true
434c7="$(git rev-parse c7)"
435
436git checkout --quiet main
437git checkout --quiet -b under-c7
438echo "a fourth source file" > src/fourth.txt
439git add -A
440git commit --quiet -m "add a fourth source file"
441attest_change under-c7 "" "" --check code-build
442request under-c7 main "$(git rev-parse under-c7)"
443integrate
444grep -q "^under-c7 -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
445
446request c7 main "$c7"
447integrate
448grep -q "^c7 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lie about the kind was not rejected"
449grep -q 'requires a pure check and the attestation claims an effectful check' "$work/last.out" \
450 || die "the refusal does not say the policy decides the rule"
451holds "the transfer rule follows the policy's runner, never the attestation's own claim"
452forget c7
453
454# ----------------------------------------------------------------------
455say "8. evidence pushed after a no-evidence verdict re-judges and lands"
456
457git checkout --quiet main
458git pull --quiet --ff-only origin main
459git checkout --quiet -b c8
460echo "a readme with no evidence yet" > docs/readme.md
461git add -A
462git commit --quiet -m "a change submitted before its checks ran"
463git push --quiet origin c8
464request c8 main "$(git rev-parse c8)"
465integrate
466grep -q "^c8 -> refs/heads/main .*: stale$" "$work/last.out" || die "a change with no evidence was not stale"
467grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
468 || die "the absent attestation was not reported"
469
470# Nothing about the request or the tip moves; only the evidence arrives.
471attest_change c8 "" "" --check prose-format
472integrate
473grep -q "^c8 -> refs/heads/main .*: land$" "$work/last.out" \
474 || die "the request was not re-judged after its evidence arrived"
475[ "$(tip)" = "$(git rev-parse c8)" ] || die "main is not at the change's head"
476holds "a pass re-judges when evidence arrives, with the request and the tip unmoved"
477
478# ----------------------------------------------------------------------
479say "9. a ref that is not a request does not hold up one that is"
480
481git checkout --quiet main
482git pull --quiet --ff-only origin main
483git checkout --quiet -b c9
484echo "a readme beside a garbage ref" > docs/readme.md
485git add -A
486git commit --quiet -m "a change beside a malformed request"
487attest_change c9 "" "" --check prose-format
488# A name with no target segment at all, and one with a segment too many.
489# Anyone with push access can write this namespace.
490git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/garbage"
491git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/main/too/deep"
492request c9 main "$(git rev-parse c9)"
493integrate
494grep -q 'refs/the-valley/integration-requests/garbage: not a request ref' "$work/last.out" \
495 || die "the malformed ref was not reported"
496grep -q 'refs/the-valley/integration-requests/main/too/deep: not a request ref' "$work/last.out" \
497 || die "the over-deep ref was not reported"
498grep -q "^c9 -> refs/heads/main .*: land$" "$work/last.out" \
499 || die "a malformed ref stopped the pass reaching a valid request"
500holds "malformed refs are reported and skipped; the valid request beside them landed"
501git -C "$origin" update-ref -d refs/the-valley/integration-requests/garbage
502git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/too/deep
503
504# ----------------------------------------------------------------------
505say "10. a pass leaves no scratch behind, and a leaked worktree is swept"
506
507# Nine scenarios of real passes, landings and rejections alike, and the
508# scratch root is empty: every worktree a pass takes is given back on the
509# way out, on the failure path as much as the success one.
510leftover="$(find "$TMPDIR" -maxdepth 1 -name 'valley-integrator-wt*')"
511[ -z "$leftover" ] || die "nine passes left scratch behind: $leftover"
512[ "$(git -C "$origin" worktree list --porcelain | grep -c '^worktree ')" -eq 1 ] \
513 || die "nine passes left a worktree attached to the repository"
514holds "no pass left a worktree or a scratch directory behind"
515
516# What a crash leaves: the checkout and its administrative entry, both
517# still valid, so nothing prunes them. Beside it, a worktree of the kind
518# an operator might attach by hand, which is not the integrator's to
519# remove.
520leak="$TMPDIR/valley-integrator-wt-crashed"
521mine="$work/an-operators-own-worktree"
522git -C "$origin" worktree add --quiet --detach "$leak" refs/heads/main
523git -C "$origin" worktree add --quiet --detach "$mine" refs/heads/main
524timeout 5 integrator watch --interval 1s \
525 --repo "$origin" \
526 --key "$work/integrator" --name "$integrator_name" \
527 --known-signers "$work/known_signers" \
528 --instance "$instance" --project-policy policy \
529 --schema "$SCHEMA_VERIFICATION" \
530 --attest-schema "$SCHEMA_ATTESTATION" \
531 --event-schema "$SCHEMA_EVENTS" \
532 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
533 > "$work/watch.out" 2>&1 || true
534[ ! -e "$leak" ] || die "a leaked scratch worktree survived the sweep"
535if git -C "$origin" worktree list --porcelain | grep -q "^worktree $leak$"; then
536 die "the leaked worktree is gone from disk and still registered"
537fi
538[ -e "$mine" ] || die "the sweep removed a worktree the integrator did not make"
539git -C "$origin" worktree remove --force "$mine"
540holds "a crash-leaked scratch worktree is swept at watch start; another hand's is not"
541
542printf '\nintegrator-e2e: every scenario held\n'