gunk-dev/the-valley

run.sh

source3a4d93424a80main's published tip history
33.7 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/project
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. It sits at policy/project, which is where the
79# integrator looks without being told (dcr-f41f718).
80cat > policy/project/policy.cue <<'EOF'
81package verification
82
83project: {
84 checks: {
85 "prose-format": {runner: "nix", attribute: "prose-format"}
86 "code-build": {runner: "nix", attribute: "code-build"}
87 // An effectful check, with the validity window dcr-439b771 calls
88 // Δ. Its observation transfers while nothing in the class that
89 // requires it has landed under it and it is younger than this.
90 reachable: {runner: "command", command: "true", validity: "1h"}
91 }
92 classes: {
93 prose: {paths: "docs/**": true, requires: "prose-format": true}
94 code: {paths: "src/**": true, requires: "code-build": true}
95 deploy: {paths: "deploy/**": true, requires: reachable: true}
96 }
97 unclassified: {}
98}
99EOF
100
101git add -A
102git commit --quiet -m "seed the project"
103git push --quiet origin main
104
105# The instance layer lives outside the project's tree, and the integrator
106# reads it from there: a change never supplies the policy that gates it. The
107# floor is the group's own repository's, read at the tip that has been
108# integrated (dcr-f41f718), and it sits at policy/instance in that tip.
109instance_repo="$work/instance.git"
110git init --quiet --bare "$instance_repo"
111floor="$work/instance-project"
112git init --quiet "$floor"
113mkdir -p "$floor/policy/instance"
114cat > "$floor/policy/instance/floor.cue" <<'EOF'
115package verification
116
117floor: {
118 checks: {}
119 classes: {}
120 unclassified: {}
121}
122EOF
123git -C "$floor" add -A
124git -C "$floor" commit --quiet -m "seed the floor"
125git -C "$floor" push --quiet "$instance_repo" main
126
127# The fallback source, for a host that serves no instance repository: the
128# same documents, materialized as a directory. One scenario below reads the
129# floor that way, and every other reads it from the repository.
130instance="$work/instance"
131mkdir -p "$instance"
132cp "$floor/policy/instance/floor.cue" "$instance/floor.cue"
133
134holds "the floor is policy/instance at $instance_repo's tip, and the project's layer is policy/project at its own"
135
136# ----------------------------------------------------------------------
137# Helpers.
138
139# attest_change <branch> <check spec…> — run the checks over the branch's
140# tip and publish the branch with its attestation ref, atomically.
141attest_change() {
142 local branch="$1" key="${2:-$work/contributor}" name="${3:-$contributor}"
143 shift 3
144 attest run --repo "$repo" --key "$key" --name "$name" \
145 --push origin --branch "$branch" "$@"
146}
147
148# request <change> <target> <commit> — the contributor asks for integration
149# by pushing one ref. That ref is the whole of the request.
150request() {
151 git push --quiet origin "$3:refs/the-valley/integration-requests/$2/$1"
152}
153
154integrate() {
155 # Neither policy path is stated: both layers come from where the
156 # integrator defaults to looking, which is the whole of what a deployment
157 # following the convention says about policy.
158 integrator reconcile \
159 --repo "$origin" \
160 --key "$work/integrator" --name "$integrator_name" \
161 --known-signers "$work/known_signers" \
162 --instance-repo "$instance_repo" \
163 --schema "$SCHEMA_VERIFICATION" \
164 --attest-schema "$SCHEMA_ATTESTATION" \
165 --event-schema "$SCHEMA_EVENTS" \
166 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
167 2>&1 | tee "$work/last.out"
168 grep -q . "$work/last.out" || die "the integrator said nothing"
169}
170
171tip() { git -C "$origin" rev-parse refs/heads/main; }
172forget() { git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$1"; }
173
174# ----------------------------------------------------------------------
175say "1. a change with valid transferring evidence lands"
176
177before="$(tip)"
178git checkout --quiet -b c1
179echo "a better readme" > docs/readme.md
180echo "a new target" > deploy/second.txt
181git add -A
182git commit --quiet -m "improve the readme and add a target"
183attest_change c1 "" "" --check prose-format --command reachable=true
184request c1 main "$(git rev-parse c1)"
185integrate
186
187grep -q "^c1 -> refs/heads/main .*: land$" "$work/last.out" || die "c1 did not land"
188grep -q "check prose-format .*transferred" "$work/last.out" || die "prose-format did not transfer"
189grep -q "check reachable .*transferred (untouched)" "$work/last.out" || die "the effectful check did not transfer"
190grep -q '"event":"integration-succeeded"' "$work/last.out" || die "no integration-succeeded event"
191[ "$(tip)" != "$before" ] || die "the protected ref did not move"
192[ "$(tip)" = "$(git rev-parse c1)" ] || die "main is not at the change's head"
193git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c1 > /dev/null \
194 && die "the request ref outlived the landing"
195holds "main moved $(git rev-parse --short "$before") -> $(git -C "$origin" rev-parse --short refs/heads/main)"
196
197# The landed note carries both signatures, under one text, and verifies.
198# The integrator writes under its own key hash, so its ref never races the
199# contributor's over the same subject.
200landed="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c1 | cut -d: -f2)/$ihash"
201git -C "$origin" rev-parse --verify --quiet "$landed" > /dev/null \
202 || die "no attestation ref by the integrator for the landed tree"
203git -C "$origin" cat-file blob "$landed:prose-format/statement.note" > "$work/landed.note"
204siblings="$(grep -c '^— ' "$work/landed.note")"
205[ "$siblings" -eq 2 ] || die "the landed note carries $siblings signatures, not two"
206attest verify --note "$work/landed.note" --known-keys "$work/reader_keys" \
207 --schema "$SCHEMA_ATTESTATION" --repo "$repo" --rev c1 \
208 --signer "$contributor" --signer "$integrator_name" > "$work/landed.verify"
209grep -q 'matches the recorded subject digest' "$work/landed.verify" || die "the landed note does not bind to the tree"
210holds "the landed note carries contributor and integrator sibling signatures and verifies"
211
212# And the integrator's own transfer statement sits beside it.
213git -C "$origin" cat-file blob "$landed:transfer.note" > "$work/transfer.note"
214head -n1 "$work/transfer.note" | grep -qx 'the-valley/attestation/v1' || die "the transfer note is not a statement"
215grep -qx 'predicateType the-valley/integration/transfer/v1' "$work/transfer.note" || die "wrong predicate type"
216grep -qx 'predicate.change c1' "$work/transfer.note" || die "the transfer statement names no change"
217grep -q '^predicate.policy.sha256 ' "$work/transfer.note" || die "the transfer statement names no policy"
218attest verify --note "$work/transfer.note" --known-keys "$work/reader_keys" \
219 --schema "$SCHEMA_ATTESTATION" --signer "$integrator_name" > /dev/null
220holds "the transfer statement records the verdict and the policy it was derived under"
221
222# ----------------------------------------------------------------------
223say "2. an intervening landing invalidates one check's closure"
224
225git checkout --quiet main
226git pull --quiet --ff-only origin main
227
228# c2 is authored against the current tip and reads both classes.
229git checkout --quiet -b c2
230echo "a readme with more in it" > docs/readme.md
231echo "the source, revised" > src/main.txt
232git add -A
233git commit --quiet -m "revise the readme and the source"
234attest_change c2 "" "" --check prose-format --check code-build
235c2="$(git rev-parse c2)"
236
237# Meanwhile another change lands, touching only what code-build reads.
238git checkout --quiet main
239git checkout --quiet -b intervening
240echo "a second source file" > src/other.txt
241git add -A
242git commit --quiet -m "add a second source file"
243attest_change intervening "" "" --check code-build
244request intervening main "$(git rev-parse intervening)"
245integrate
246grep -q "^intervening -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
247holds "the intervening change landed first; c2's base has moved"
248
249request c2 main "$c2"
250integrate
251grep -q "^c2 -> refs/heads/main .*: stale$" "$work/last.out" || die "c2 was not stale"
252grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
253 || die "prose-format should have transferred by closure equality"
254grep -q "check code-build .*input closure changed" "$work/last.out" \
255 || die "code-build's closure should have moved"
256grep -q '"event":"request-stale"' "$work/last.out" || die "no request-stale event"
257grep -q '"checks":\["code-build"\]' "$work/last.out" || die "the event does not name exactly code-build"
258git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c2 > /dev/null \
259 || die "the request ref was dropped; there is nothing to resubmit"
260holds "one request-stale naming exactly code-build; prose-format reported transferred"
261forget c2
262
263# ----------------------------------------------------------------------
264say "2b. evidence that survives the rebase lands on the moved tip"
265
266# The same shape as 2, with the intervening landing outside what the
267# change's checks read. The tree that lands is not the tree that was
268# attested, and the evidence stands anyway — which is the whole of what
269# content-addressed evidence buys.
270git checkout --quiet main
271git pull --quiet --ff-only origin main
272git checkout --quiet -b c2b
273echo "a readme revised again" > docs/readme.md
274git add -A
275git commit --quiet -m "revise the readme once more"
276attest_change c2b "" "" --check prose-format
277c2b="$(git rev-parse c2b)"
278attested="$(attest digest --repo "$repo" --rev c2b | cut -d: -f2)"
279
280git checkout --quiet main
281git checkout --quiet -b elsewhere
282echo "a third source file" > src/third.txt
283git add -A
284git commit --quiet -m "add a third source file"
285attest_change elsewhere "" "" --check code-build
286request elsewhere main "$(git rev-parse elsewhere)"
287integrate
288grep -q "^elsewhere -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
289
290request c2b main "$c2b"
291integrate
292grep -q "^c2b -> refs/heads/main .*: land$" "$work/last.out" || die "c2b did not land"
293grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
294 || die "the evidence did not transfer by closure equality"
295landedtree="$(git -C "$origin" rev-parse refs/heads/main^{tree})"
296[ "$(git -C "$origin" rev-parse "refs/heads/main^")" = "$(git rev-parse elsewhere)" ] \
297 || die "the landing is not a fast-forward from the tip it was judged against"
298[ "$(git -C "$origin" rev-parse refs/heads/main^{tree})" != "$(git rev-parse "$c2b^{tree}")" ] \
299 || die "the tree did not actually move; the scenario proves nothing"
300holds "the delta landed on a tree it was never attested over, and no check re-ran"
301
302# Two attestation refs now, because the two statements are about two
303# different trees: the contributor's is keyed by the tree it was made over,
304# and the integrator's transfer statement by the tree that landed.
305git fetch --quiet origin main
306landed_digest="$(attest digest --repo "$repo" --rev "$(git -C "$origin" rev-parse refs/heads/main)" | cut -d: -f2)"
307[ "$landed_digest" != "$attested" ] || die "the landed tree is the attested tree; the scenario proves nothing"
308git -C "$origin" cat-file blob \
309 "refs/the-valley/attestations/$attested/$ihash:prose-format/statement.note" > "$work/rebased.note"
310[ "$(grep -c '^— ' "$work/rebased.note")" -eq 2 ] \
311 || die "the countersigned note lost a sibling signature"
312grep -qx "subject.digest.valley-tree-v1 $attested" "$work/rebased.note" \
313 || die "the countersigned statement is not about the tree it was made over"
314git -C "$origin" cat-file blob \
315 "refs/the-valley/attestations/$landed_digest/$ihash:transfer.note" > "$work/rebased.transfer"
316grep -qx "subject.digest.valley-tree-v1 $landed_digest" "$work/rebased.transfer" \
317 || die "the transfer statement is not about the landed tree"
318grep -qx "predicate.base.valley-tree-v1 $attested" "$work/rebased.transfer" \
319 || die "the transfer statement does not cite the base the evidence was produced over"
320grep -qx "predicate.checks.0.rule closure" "$work/rebased.transfer" \
321 || die "the transfer statement does not record the rule that carried the evidence"
322holds "the contributor's statement stays about its own tree; the transfer statement names the landed one"
323
324# ----------------------------------------------------------------------
325say "3. a conflicting delta is stale, and demands full re-attestation"
326
327git checkout --quiet main
328git pull --quiet --ff-only origin main
329git checkout --quiet -b c3
330echo "the readme, as c3 would have it" > docs/readme.md
331git add -A
332git commit --quiet -m "rewrite the readme one way"
333attest_change c3 "" "" --check prose-format
334c3="$(git rev-parse c3)"
335
336git checkout --quiet main
337git checkout --quiet -b conflicting
338echo "the readme, as the other change would have it" > docs/readme.md
339git add -A
340git commit --quiet -m "rewrite the readme another way"
341attest_change conflicting "" "" --check prose-format
342request conflicting main "$(git rev-parse conflicting)"
343integrate
344grep -q "^conflicting -> refs/heads/main .*: land$" "$work/last.out" || die "the conflicting change did not land"
345
346request c3 main "$c3"
347integrate
348grep -q "^c3 -> refs/heads/main .*: stale$" "$work/last.out" || die "c3 was not stale"
349grep -q 'resolving it is new authorship' "$work/last.out" || die "the conflict was not reported as new authorship"
350grep -q 'CONFLICT (content): Merge conflict in docs/readme.md' "$work/last.out" \
351 || die "the conflict does not name what could not be merged"
352grep -q '"reason":"conflict"' "$work/last.out" || die "the event does not say conflict"
353grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "a conflict must re-demand every required check"
354holds "a conflict is stale, and every required check is re-demanded"
355forget c3
356
357# ----------------------------------------------------------------------
358say "4. an unknown signer's attestation does not transfer"
359
360git checkout --quiet main
361git pull --quiet --ff-only origin main
362git checkout --quiet -b c4
363echo "a readme signed by a stranger" > docs/readme.md
364git add -A
365git commit --quiet -m "a change a stranger attested"
366attest_change c4 "$work/stranger" "$stranger" --check prose-format
367request c4 main "$(git rev-parse c4)"
368integrate
369grep -q "^c4 -> refs/heads/main .*: stale$" "$work/last.out" || die "c4 was not stale"
370grep -q 'no signer the integrator holds' "$work/last.out" || die "the unknown signer was not reported"
371grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "the event does not name prose-format"
372[ "$(tip)" = "$(git -C "$origin" rev-parse refs/heads/main)" ] || die "main moved"
373holds "evidence from a signer the integrator does not hold does not transfer"
374forget c4
375
376# ----------------------------------------------------------------------
377say "5. a tampered statement is rejected, not called stale"
378
379git checkout --quiet main
380git checkout --quiet -b c5
381echo "an honest readme" > docs/readme.md
382git add -A
383git commit --quiet -m "an honest change"
384attest_change c5 "" "" --check prose-format
385c5="$(git rev-parse c5)"
386c5digest="$(attest digest --repo "$repo" --rev c5 | cut -d: -f2)"
387
388# Edit one value in the signed text and store the result where the real
389# note was. Everything else about the note is untouched.
390c5ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
391 "refs/the-valley/attestations/$c5digest" | head -n1)"
392git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note" \
393 | sed 's/^predicate.result passed$/predicate.result failed/' > "$work/tampered.note"
394cmp -s "$work/tampered.note" <(git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note") \
395 && die "the tamper changed nothing; the test proves nothing"
396blob="$(git -C "$origin" hash-object -w --stdin < "$work/tampered.note")"
397sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$origin" mktree)"
398top="$(printf '040000 tree %s\tprose-format\n' "$sub" | git -C "$origin" mktree)"
399git -C "$origin" update-ref "$c5ref" "$top"
400
401request c5 main "$c5"
402integrate
403grep -q "^c5 -> refs/heads/main .*: reject$" "$work/last.out" || die "a tampered statement was not rejected"
404grep -q 'does not verify as the statement it claims to be' "$work/last.out" || die "the refusal was not reported"
405grep -q 'request-stale' "$work/last.out" && die "a forgery was announced as staleness"
406holds "a tampered statement is a rejection, and no outcome event claims otherwise"
407forget c5
408
409# ----------------------------------------------------------------------
410say "6. an attestation about a different tree is rejected"
411
412git checkout --quiet main
413git checkout --quiet -b c6
414echo "the tree that was actually attested" > docs/readme.md
415git add -A
416git commit --quiet -m "the attested change"
417attest_change c6 "" "" --check prose-format
418c6ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
419 "refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c6 | cut -d: -f2)" | head -n1)"
420
421# A second, different tree — with no attestation of its own — and the
422# first tree's valid, correctly signed note filed under its digest.
423git checkout --quiet -b c6-lifted
424echo "the tree that was not" > docs/readme.md
425git add -A
426git commit --quiet -m "the change the evidence was lifted onto"
427git push --quiet origin c6-lifted
428lifted="$(attest digest --repo "$repo" --rev c6-lifted | cut -d: -f2)"
429git -C "$origin" update-ref \
430 "refs/the-valley/attestations/$lifted/${c6ref##*/}" \
431 "$(git -C "$origin" rev-parse "$c6ref^{tree}")"
432
433request c6 main "$(git rev-parse c6-lifted)"
434integrate
435grep -q "^c6 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lifted attestation was not rejected"
436grep -q 'tree digest mismatch' "$work/last.out" || die "the refusal does not name the tree digest"
437holds "a signature cannot be lifted onto a tree it was not made about"
438forget c6
439
440# ----------------------------------------------------------------------
441say "7. an attestation lying about its kind is rejected"
442
443# The policy runs prose-format with nix, so its evidence is content-
444# addressed. This attestation claims a notarization instead, and its class
445# was untouched by the intervening landing — so under the rule it nominated
446# for itself it would sail onto a base it was never produced over.
447git checkout --quiet main
448git pull --quiet --ff-only origin main
449git checkout --quiet -b c7
450echo "a readme attested as a notarization" > docs/readme.md
451git add -A
452git commit --quiet -m "a change whose attestation lies about its kind"
453attest_change c7 "" "" --command prose-format=true
454c7="$(git rev-parse c7)"
455
456git checkout --quiet main
457git checkout --quiet -b under-c7
458echo "a fourth source file" > src/fourth.txt
459git add -A
460git commit --quiet -m "add a fourth source file"
461attest_change under-c7 "" "" --check code-build
462request under-c7 main "$(git rev-parse under-c7)"
463integrate
464grep -q "^under-c7 -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
465
466request c7 main "$c7"
467integrate
468grep -q "^c7 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lie about the kind was not rejected"
469grep -q 'requires a pure check and the attestation claims an effectful check' "$work/last.out" \
470 || die "the refusal does not say the policy decides the rule"
471holds "the transfer rule follows the policy's runner, never the attestation's own claim"
472forget c7
473
474# ----------------------------------------------------------------------
475say "8. evidence pushed after a no-evidence verdict re-judges and lands"
476
477git checkout --quiet main
478git pull --quiet --ff-only origin main
479git checkout --quiet -b c8
480echo "a readme with no evidence yet" > docs/readme.md
481git add -A
482git commit --quiet -m "a change submitted before its checks ran"
483git push --quiet origin c8
484request c8 main "$(git rev-parse c8)"
485integrate
486grep -q "^c8 -> refs/heads/main .*: stale$" "$work/last.out" || die "a change with no evidence was not stale"
487grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
488 || die "the absent attestation was not reported"
489
490# Nothing about the request or the tip moves; only the evidence arrives.
491attest_change c8 "" "" --check prose-format
492integrate
493grep -q "^c8 -> refs/heads/main .*: land$" "$work/last.out" \
494 || die "the request was not re-judged after its evidence arrived"
495[ "$(tip)" = "$(git rev-parse c8)" ] || die "main is not at the change's head"
496holds "a pass re-judges when evidence arrives, with the request and the tip unmoved"
497
498# ----------------------------------------------------------------------
499say "9. a ref that is not a request does not hold up one that is"
500
501git checkout --quiet main
502git pull --quiet --ff-only origin main
503git checkout --quiet -b c9
504echo "a readme beside a garbage ref" > docs/readme.md
505git add -A
506git commit --quiet -m "a change beside a malformed request"
507attest_change c9 "" "" --check prose-format
508# A name with no target segment at all, and one with a segment too many.
509# Anyone with push access can write this namespace.
510git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/garbage"
511git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/main/too/deep"
512request c9 main "$(git rev-parse c9)"
513integrate
514grep -q 'refs/the-valley/integration-requests/garbage: not a request ref' "$work/last.out" \
515 || die "the malformed ref was not reported"
516grep -q 'refs/the-valley/integration-requests/main/too/deep: not a request ref' "$work/last.out" \
517 || die "the over-deep ref was not reported"
518grep -q "^c9 -> refs/heads/main .*: land$" "$work/last.out" \
519 || die "a malformed ref stopped the pass reaching a valid request"
520holds "malformed refs are reported and skipped; the valid request beside them landed"
521git -C "$origin" update-ref -d refs/the-valley/integration-requests/garbage
522git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/too/deep
523
524# ----------------------------------------------------------------------
525say "10. a pass leaves no scratch behind, and a leaked worktree is swept"
526
527# Nine scenarios of real passes, landings and rejections alike, and the
528# scratch root is empty: every worktree a pass takes is given back on the
529# way out, on the failure path as much as the success one.
530leftover="$(find "$TMPDIR" -maxdepth 1 -name 'valley-integrator-wt*')"
531[ -z "$leftover" ] || die "nine passes left scratch behind: $leftover"
532[ "$(git -C "$origin" worktree list --porcelain | grep -c '^worktree ')" -eq 1 ] \
533 || die "nine passes left a worktree attached to the repository"
534holds "no pass left a worktree or a scratch directory behind"
535
536# What a crash leaves: the checkout and its administrative entry, both
537# still valid, so nothing prunes them. Beside it, a worktree of the kind
538# an operator might attach by hand, which is not the integrator's to
539# remove.
540leak="$TMPDIR/valley-integrator-wt-crashed"
541mine="$work/an-operators-own-worktree"
542git -C "$origin" worktree add --quiet --detach "$leak" refs/heads/main
543git -C "$origin" worktree add --quiet --detach "$mine" refs/heads/main
544timeout 5 integrator watch --interval 1s \
545 --repo "$origin" \
546 --key "$work/integrator" --name "$integrator_name" \
547 --known-signers "$work/known_signers" \
548 --instance "$instance" \
549 --schema "$SCHEMA_VERIFICATION" \
550 --attest-schema "$SCHEMA_ATTESTATION" \
551 --event-schema "$SCHEMA_EVENTS" \
552 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
553 > "$work/watch.out" 2>&1 || true
554[ ! -e "$leak" ] || die "a leaked scratch worktree survived the sweep"
555if git -C "$origin" worktree list --porcelain | grep -q "^worktree $leak$"; then
556 die "the leaked worktree is gone from disk and still registered"
557fi
558[ -e "$mine" ] || die "the sweep removed a worktree the integrator did not make"
559git -C "$origin" worktree remove --force "$mine"
560holds "a crash-leaked scratch worktree is swept at watch start; another hand's is not"
561
562# ----------------------------------------------------------------------
563say "11. a scratch root it may not write is reported as that, and nothing else"
564
565# The deployment failure this guards is a sandbox failure: the root TMPDIR
566# names is not writable. That is a statement about the unit, not about the
567# change being judged, and it has to arrive as one — with the path and where
568# the path came from — rather than as git complaining about the leading
569# directories of a .git file.
570refused="$work/refused"
571mkdir -p "$refused"
572chmod a-w "$refused"
573git checkout --quiet main
574git pull --quiet --ff-only origin main
575git checkout --quiet -b c10
576echo "a change that never gets a worktree" > docs/readme.md
577git add -A
578git commit --quiet -m "a change the integrator cannot make scratch for"
579attest_change c10 "" "" --check prose-format
580request c10 main "$(git rev-parse c10)"
581export TMPDIR="$refused"
582integrate || true
583export TMPDIR="$work/tmp"
584chmod u+w "$refused"
585grep -q "scratch directory under $refused, which TMPDIR names" "$work/last.out" \
586 || die "an unwritable scratch root was not reported as one: $(cat "$work/last.out")"
587forget c10
588holds "an unwritable scratch root names the path and where it came from"
589
590# ----------------------------------------------------------------------
591say "12. a floor the instance repository does not carry names the directory looked in"
592
593# The deployment failure this guards is a controller pointed at the wrong
594# directory: the floor is somewhere, and not where this controller looked.
595# The report has to name the repository, the directory and the ref, because
596# those three are what the operator compares against the repository in front
597# of them. A floor is either not there at all, or there under a name that
598# holds no documents, and both arrive that way.
599git checkout --quiet main
600git pull --quiet --ff-only origin main
601git checkout --quiet -b c11
602echo "a change judged under no floor" > docs/readme.md
603git add -A
604git commit --quiet -m "a change whose floor the controller cannot find"
605attest_change c11 "" "" --check prose-format
606request c11 main "$(git rev-parse c11)"
607
608floorless() {
609 integrator reconcile \
610 --repo "$origin" \
611 --key "$work/integrator" --name "$integrator_name" \
612 --known-signers "$work/known_signers" \
613 --instance-repo "$origin" "$@" \
614 --schema "$SCHEMA_VERIFICATION" \
615 --attest-schema "$SCHEMA_ATTESTATION" \
616 --event-schema "$SCHEMA_EVENTS" \
617 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
618 2>&1 | tee "$work/last.out"
619}
620
621# The project's own repository carries policy/project and no policy/instance,
622# so the default directory is not there.
623floorless || true
624grep -qF "no instance policy layer: $origin has no policy/instance/ at refs/heads/main" "$work/last.out" \
625 || die "a missing floor did not name the directory looked in: $(cat "$work/last.out")"
626
627# And the flat layout, which is a directory that exists and holds no
628# documents of its own: policy/ has the two layers under it and no *.cue in
629# it. Said as that, rather than as an empty composition further on.
630#
631# Under a second request name, because the refusal above is now this
632# controller's recorded answer to c11 and it stands until the request, the
633# tip or the evidence moves — which is scenario 14's whole subject. A
634# request is what an answer is remembered against, so a second one asks the
635# question again.
636request c11-flat main "$(git rev-parse c11)"
637floorless --instance-policy policy || true
638grep -qF "no instance policy layer: $origin holds no *.cue in policy/ at refs/heads/main" "$work/last.out" \
639 || die "a floor directory holding no documents did not say so: $(cat "$work/last.out")"
640forget c11
641forget c11-flat
642holds "a floor that is not where the controller looked names the repository, the directory and the ref"
643
644# ----------------------------------------------------------------------
645say "13. a change the policy requires no check of lands, and says so"
646
647# Requiring nothing is an ordinary outcome, not an edge: this project's
648# classes cover docs/, src/ and deploy/, and its `unclassified` set is
649# empty, so a path outside all three is answered with nothing owed. The
650# landing still writes a transfer statement — a commit point that records
651# nothing is a landing nothing attests to — and that statement says the
652# required set was empty rather than carrying a list with nothing in it,
653# which has no written form at all.
654git checkout --quiet main
655git pull --quiet --ff-only origin main
656git checkout --quiet -b c12
657echo "outside every class this policy covers" > notes.txt
658git add -A
659git commit --quiet -m "a change no class covers"
660request c12 main "$(git rev-parse c12)"
661integrate
662grep -q '^c12 -> refs/heads/main .*: land$' "$work/last.out" \
663 || die "a change with nothing required did not land: $(cat "$work/last.out")"
664grep -q 'the policy requires no check of the paths this change touches' "$work/last.out" \
665 || die "the verdict did not say the policy required nothing"
666[ "$(tip)" = "$(git rev-parse c12)" ] || die "c12 did not land"
667git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c12" > /dev/null \
668 && die "the request ref survived the landing"
669
670nothing="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c12 | cut -d: -f2)/$ihash"
671git -C "$origin" cat-file blob "$nothing:transfer.note" > "$work/nothing.note"
672grep -qx 'predicate.required none' "$work/nothing.note" \
673 || die "the transfer statement does not say the required set was empty: $(cat "$work/nothing.note")"
674grep -q '^predicate.checks' "$work/nothing.note" \
675 && die "the transfer statement carries a check list beside the empty-set line"
676attest verify --note "$work/nothing.note" --known-keys "$work/reader_keys" \
677 --repo "$repo" --rev c12 --signer "$integrator_name" > /dev/null \
678 || die "the transfer statement for a landing with nothing required does not verify"
679holds "a landing with no required checks is signed, stored, and consumed its request"
680
681# ----------------------------------------------------------------------
682say "14. a request the controller cannot judge is reported once, not every tick"
683
684# The failure this guards is the one that turns a broken deployment into a
685# broken deployment nobody can read: a pass that cannot act on its verdict,
686# retried at tick rate, filling the journal with one refusal per interval
687# forever. The same request, tip and evidence produce the same failure, so
688# repeating it says nothing new. It is reported once and then the answer
689# stands, exactly as an outcome stands.
690git checkout --quiet main
691git pull --quiet --ff-only origin main
692git checkout --quiet -b c13
693echo "a change judged under no floor, repeatedly" > docs/readme.md
694git add -A
695git commit --quiet -m "a change whose floor the controller cannot find, under watch"
696attest_change c13 "" "" --check prose-format
697request c13 main "$(git rev-parse c13)"
698
699# Five seconds at a one-second tick: five passes over one request the
700# controller cannot judge.
701timeout 5 integrator watch --interval 1s \
702 --repo "$origin" \
703 --key "$work/integrator" --name "$integrator_name" \
704 --known-signers "$work/known_signers" \
705 --instance-repo "$origin" --instance-policy policy \
706 --schema "$SCHEMA_VERIFICATION" \
707 --attest-schema "$SCHEMA_ATTESTATION" \
708 --event-schema "$SCHEMA_EVENTS" \
709 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
710 > "$work/repeat.out" 2>&1 || true
711reports="$(grep -c 'no instance policy layer' "$work/repeat.out" || true)"
712[ "$reports" -eq 1 ] \
713 || die "the same refusal was reported $reports times over five ticks: $(cat "$work/repeat.out")"
714forget c13
715holds "a failure a pass cannot get past is reported once and then backs off"
716
717printf '\nintegrator-e2e: every scenario held\n'