gunk-dev/the-valley

run.sh

source08dfaa862db7main's published tip history
68.5 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 files an integration
149# request 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# The landing's own ref-updated event, vetted against #RefUpdated like the rest.
192grep -qF "event ref-updated {\"event\":\"ref-updated\",\"repo\":" "$work/last.out" \
193 || die "no ref-updated event"
194[ "$(tip)" != "$before" ] || die "the protected ref did not move"
195[ "$(tip)" = "$(git rev-parse c1)" ] || die "main is not at the change's head"
196git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c1 > /dev/null \
197 && die "the request ref outlived the landing"
198holds "main moved $(git rev-parse --short "$before") -> $(git -C "$origin" rev-parse --short refs/heads/main)"
199
200# The landed note carries both signatures, under one text, and verifies.
201# The integrator writes under its own key hash, so its ref never races the
202# contributor's over the same subject.
203landed="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c1 | cut -d: -f2)/$ihash"
204git -C "$origin" rev-parse --verify --quiet "$landed" > /dev/null \
205 || die "no attestation ref by the integrator for the landed tree"
206git -C "$origin" cat-file blob "$landed:prose-format/statement.note" > "$work/landed.note"
207siblings="$(grep -c '^— ' "$work/landed.note")"
208[ "$siblings" -eq 2 ] || die "the landed note carries $siblings signatures, not two"
209attest verify --note "$work/landed.note" --known-keys "$work/reader_keys" \
210 --schema "$SCHEMA_ATTESTATION" --repo "$repo" --rev c1 \
211 --signer "$contributor" --signer "$integrator_name" > "$work/landed.verify"
212grep -q 'matches the recorded subject digest' "$work/landed.verify" || die "the landed note does not bind to the tree"
213holds "the landed note carries contributor and integrator sibling signatures and verifies"
214
215# And the integrator's own transfer statement sits beside it.
216git -C "$origin" cat-file blob "$landed:transfer.note" > "$work/transfer.note"
217head -n1 "$work/transfer.note" | grep -qx 'the-valley/attestation/v1' || die "the transfer note is not a statement"
218grep -qx 'predicateType the-valley/integration/transfer/v1' "$work/transfer.note" || die "wrong predicate type"
219grep -qx 'predicate.change c1' "$work/transfer.note" || die "the transfer statement names no change"
220grep -q '^predicate.policy.sha256 ' "$work/transfer.note" || die "the transfer statement names no policy"
221attest verify --note "$work/transfer.note" --known-keys "$work/reader_keys" \
222 --schema "$SCHEMA_ATTESTATION" --signer "$integrator_name" > /dev/null
223holds "the transfer statement records the verdict and the policy it was derived under"
224
225# ----------------------------------------------------------------------
226say "2. an intervening landing invalidates one check's closure"
227
228git checkout --quiet main
229git pull --quiet --ff-only origin main
230
231# c2 is authored against the current tip and reads both classes.
232git checkout --quiet -b c2
233echo "a readme with more in it" > docs/readme.md
234echo "the source, revised" > src/main.txt
235git add -A
236git commit --quiet -m "revise the readme and the source"
237attest_change c2 "" "" --check prose-format --check code-build
238c2="$(git rev-parse c2)"
239
240# Meanwhile another change lands, touching only what code-build reads.
241git checkout --quiet main
242git checkout --quiet -b intervening
243echo "a second source file" > src/other.txt
244git add -A
245git commit --quiet -m "add a second source file"
246attest_change intervening "" "" --check code-build
247request intervening main "$(git rev-parse intervening)"
248integrate
249grep -q "^intervening -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
250holds "the intervening change landed first; c2's base has moved"
251
252request c2 main "$c2"
253integrate
254grep -q "^c2 -> refs/heads/main .*: stale$" "$work/last.out" || die "c2 was not stale"
255grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
256 || die "prose-format should have transferred by closure equality"
257grep -q "check code-build .*input closure changed" "$work/last.out" \
258 || die "code-build's closure should have moved"
259grep -q '"event":"request-stale"' "$work/last.out" || die "no request-stale event"
260grep -q '"checks":\["code-build"\]' "$work/last.out" || die "the event does not name exactly code-build"
261git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c2 > /dev/null \
262 || die "the request ref was dropped; there is nothing to approve again"
263holds "one request-stale naming exactly code-build; prose-format reported transferred"
264forget c2
265
266# ----------------------------------------------------------------------
267say "2b. evidence that survives the rebase lands on the moved tip"
268
269# The same shape as 2, with the intervening landing outside what the
270# change's checks read. The tree that lands is not the tree that was
271# attested, and the evidence stands anyway — which is the whole of what
272# content-addressed evidence buys.
273git checkout --quiet main
274git pull --quiet --ff-only origin main
275git checkout --quiet -b c2b
276echo "a readme revised again" > docs/readme.md
277git add -A
278git commit --quiet -m "revise the readme once more"
279attest_change c2b "" "" --check prose-format
280c2b="$(git rev-parse c2b)"
281attested="$(attest digest --repo "$repo" --rev c2b | cut -d: -f2)"
282
283git checkout --quiet main
284git checkout --quiet -b elsewhere
285echo "a third source file" > src/third.txt
286git add -A
287git commit --quiet -m "add a third source file"
288attest_change elsewhere "" "" --check code-build
289request elsewhere main "$(git rev-parse elsewhere)"
290integrate
291grep -q "^elsewhere -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
292
293request c2b main "$c2b"
294integrate
295grep -q "^c2b -> refs/heads/main .*: land$" "$work/last.out" || die "c2b did not land"
296grep -q "check prose-format .*transferred (closure)" "$work/last.out" \
297 || die "the evidence did not transfer by closure equality"
298landedtree="$(git -C "$origin" rev-parse refs/heads/main^{tree})"
299[ "$(git -C "$origin" rev-parse "refs/heads/main^")" = "$(git rev-parse elsewhere)" ] \
300 || die "the landing is not a fast-forward from the tip it was judged against"
301[ "$(git -C "$origin" rev-parse refs/heads/main^{tree})" != "$(git rev-parse "$c2b^{tree}")" ] \
302 || die "the tree did not actually move; the scenario proves nothing"
303holds "the delta landed on a tree it was never attested over, and no check re-ran"
304
305# Two attestation refs now, because the two statements are about two
306# different trees: the contributor's is keyed by the tree it was made over,
307# and the integrator's transfer statement by the tree that landed.
308git fetch --quiet origin main
309landed_digest="$(attest digest --repo "$repo" --rev "$(git -C "$origin" rev-parse refs/heads/main)" | cut -d: -f2)"
310[ "$landed_digest" != "$attested" ] || die "the landed tree is the attested tree; the scenario proves nothing"
311git -C "$origin" cat-file blob \
312 "refs/the-valley/attestations/$attested/$ihash:prose-format/statement.note" > "$work/rebased.note"
313[ "$(grep -c '^— ' "$work/rebased.note")" -eq 2 ] \
314 || die "the countersigned note lost a sibling signature"
315grep -qx "subject.digest.valley-tree-v1 $attested" "$work/rebased.note" \
316 || die "the countersigned statement is not about the tree it was made over"
317git -C "$origin" cat-file blob \
318 "refs/the-valley/attestations/$landed_digest/$ihash:transfer.note" > "$work/rebased.transfer"
319grep -qx "subject.digest.valley-tree-v1 $landed_digest" "$work/rebased.transfer" \
320 || die "the transfer statement is not about the landed tree"
321grep -qx "predicate.base.valley-tree-v1 $attested" "$work/rebased.transfer" \
322 || die "the transfer statement does not cite the base the evidence was produced over"
323grep -qx "predicate.checks.0.rule closure" "$work/rebased.transfer" \
324 || die "the transfer statement does not record the rule that carried the evidence"
325holds "the contributor's statement stays about its own tree; the transfer statement names the landed one"
326
327# ----------------------------------------------------------------------
328say "2c. a re-parenting landing states its own committer, and needs none from the environment"
329
330# Re-parenting is the one landing that makes a commit of the integrator's
331# own, so it is the one that needs a committer. Under the unit there is
332# nothing to take one from: no git config the service can read, no GIT_* in
333# its environment, and a hostname git will not guess an address from. So the
334# integrator states both identities itself — the asker authored the change,
335# the integrator committed the commit that carries it onto the tip — and the
336# pass below runs with everything git could have fallen back to taken away.
337
338# A scrubbed environment: no global or system config, no GIT_* identity, and
339# auto-detection refused outright, so that a host whose own hostname does
340# resolve cannot hide a dependency on it.
341scrubbed() {
342 env -u GIT_AUTHOR_NAME -u GIT_AUTHOR_EMAIL -u GIT_AUTHOR_DATE \
343 -u GIT_COMMITTER_NAME -u GIT_COMMITTER_EMAIL -u GIT_COMMITTER_DATE -u EMAIL \
344 GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 \
345 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.useConfigOnly GIT_CONFIG_VALUE_0=true \
346 "$@"
347}
348scrubbed git -C "$origin" var GIT_COMMITTER_IDENT > /dev/null 2>&1 \
349 && die "the scrub left an identity for git to find; the scenario proves nothing"
350
351git checkout --quiet main
352git pull --quiet --ff-only origin main
353git checkout --quiet -b c2c
354echo "a readme revised by a stated author" > docs/readme.md
355git add -A
356# A distinct author, so that what the landed commit carries over from the
357# head and what it states of its own are told apart by reading it.
358GIT_AUTHOR_NAME="a contributor" GIT_AUTHOR_EMAIL="contributor@valley.invalid" \
359 git commit --quiet -m "revise the readme under an author of its own"
360attest_change c2c "" "" --check prose-format
361c2c="$(git rev-parse c2c)"
362
363# The intervening landing, outside what prose-format reads, is what moves
364# c2c's base and makes the landing below a re-parent rather than a
365# fast-forward.
366git checkout --quiet main
367git checkout --quiet -b under-c2c
368echo "a fifth source file" > src/fifth.txt
369git add -A
370git commit --quiet -m "add a fifth source file"
371attest_change under-c2c "" "" --check code-build
372request under-c2c main "$(git rev-parse under-c2c)"
373integrate
374grep -q "^under-c2c -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
375
376request c2c main "$c2c"
377scrubbed integrator reconcile \
378 --repo "$origin" \
379 --key "$work/integrator" --name "$integrator_name" \
380 --known-signers "$work/known_signers" \
381 --instance-repo "$instance_repo" \
382 --schema "$SCHEMA_VERIFICATION" \
383 --attest-schema "$SCHEMA_ATTESTATION" \
384 --event-schema "$SCHEMA_EVENTS" \
385 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
386 2>&1 | tee "$work/last.out"
387grep -q "^c2c -> refs/heads/main .*: land$" "$work/last.out" \
388 || die "a landing with no git identity in the environment did not land: $(cat "$work/last.out")"
389
390landed_commit="$(git -C "$origin" rev-parse refs/heads/main)"
391[ "$landed_commit" != "$c2c" ] || die "the landing was a fast-forward; the scenario proves nothing"
392[ "$(git -C "$origin" log -1 --format='%an <%ae>' "$landed_commit")" = "a contributor <contributor@valley.invalid>" ] \
393 || die "the landed commit is not authored by the asker: $(git -C "$origin" log -1 --format='%an <%ae>' "$landed_commit")"
394[ "$(git -C "$origin" log -1 --format='%cn <%ce>' "$landed_commit")" = "$integrator_name <integrator@the-valley.invalid>" ] \
395 || die "the landed commit is not committed by the integrator: $(git -C "$origin" log -1 --format='%cn <%ce>' "$landed_commit")"
396# The commit id is a function of what the pass decided from and nothing
397# else, so a pass that dies before the ref write recomputes this same
398# commit rather than leaving another behind.
399[ "$(git -C "$origin" log -1 --format=%aI "$landed_commit")" = "$(git -C "$origin" log -1 --format=%cI "$landed_commit")" ] \
400 || die "the committer date is not the author's, so the landed commit id is not reproducible"
401holds "the integrator committed under its own name with nothing in the environment to take one from"
402
403# ----------------------------------------------------------------------
404say "3. a conflicting delta is stale, and demands full re-attestation"
405
406git checkout --quiet main
407git pull --quiet --ff-only origin main
408git checkout --quiet -b c3
409echo "the readme, as c3 would have it" > docs/readme.md
410git add -A
411git commit --quiet -m "rewrite the readme one way"
412attest_change c3 "" "" --check prose-format
413c3="$(git rev-parse c3)"
414
415git checkout --quiet main
416git checkout --quiet -b conflicting
417echo "the readme, as the other change would have it" > docs/readme.md
418git add -A
419git commit --quiet -m "rewrite the readme another way"
420attest_change conflicting "" "" --check prose-format
421request conflicting main "$(git rev-parse conflicting)"
422integrate
423grep -q "^conflicting -> refs/heads/main .*: land$" "$work/last.out" || die "the conflicting change did not land"
424
425request c3 main "$c3"
426integrate
427grep -q "^c3 -> refs/heads/main .*: stale$" "$work/last.out" || die "c3 was not stale"
428grep -q 'resolving it is new authorship' "$work/last.out" || die "the conflict was not reported as new authorship"
429grep -q 'CONFLICT (content): Merge conflict in docs/readme.md' "$work/last.out" \
430 || die "the conflict does not name what could not be merged"
431grep -q '"reason":"conflict"' "$work/last.out" || die "the event does not say conflict"
432grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "a conflict must re-demand every required check"
433holds "a conflict is stale, and every required check is re-demanded"
434forget c3
435
436# ----------------------------------------------------------------------
437say "4. an unknown signer's attestation does not transfer"
438
439git checkout --quiet main
440git pull --quiet --ff-only origin main
441git checkout --quiet -b c4
442echo "a readme signed by a stranger" > docs/readme.md
443git add -A
444git commit --quiet -m "a change a stranger attested"
445attest_change c4 "$work/stranger" "$stranger" --check prose-format
446request c4 main "$(git rev-parse c4)"
447integrate
448grep -q "^c4 -> refs/heads/main .*: stale$" "$work/last.out" || die "c4 was not stale"
449grep -q 'no signer the integrator holds' "$work/last.out" || die "the unknown signer was not reported"
450grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "the event does not name prose-format"
451[ "$(tip)" = "$(git -C "$origin" rev-parse refs/heads/main)" ] || die "main moved"
452holds "evidence from a signer the integrator does not hold does not transfer"
453forget c4
454
455# ----------------------------------------------------------------------
456say "5. a tampered statement is rejected, not called stale"
457
458git checkout --quiet main
459git checkout --quiet -b c5
460echo "an honest readme" > docs/readme.md
461git add -A
462git commit --quiet -m "an honest change"
463attest_change c5 "" "" --check prose-format
464c5="$(git rev-parse c5)"
465c5digest="$(attest digest --repo "$repo" --rev c5 | cut -d: -f2)"
466
467# Edit one value in the signed text and store the result where the real
468# note was. Everything else about the note is untouched.
469c5ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
470 "refs/the-valley/attestations/$c5digest" | head -n1)"
471git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note" \
472 | sed 's/^predicate.result passed$/predicate.result failed/' > "$work/tampered.note"
473cmp -s "$work/tampered.note" <(git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note") \
474 && die "the tamper changed nothing; the test proves nothing"
475blob="$(git -C "$origin" hash-object -w --stdin < "$work/tampered.note")"
476sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$origin" mktree)"
477top="$(printf '040000 tree %s\tprose-format\n' "$sub" | git -C "$origin" mktree)"
478git -C "$origin" update-ref "$c5ref" "$top"
479
480request c5 main "$c5"
481integrate
482grep -q "^c5 -> refs/heads/main .*: reject$" "$work/last.out" || die "a tampered statement was not rejected"
483grep -q 'does not verify as the statement it claims to be' "$work/last.out" || die "the refusal was not reported"
484grep -q 'request-stale' "$work/last.out" && die "a forgery was announced as staleness"
485holds "a tampered statement is a rejection, and no outcome event claims otherwise"
486forget c5
487
488# ----------------------------------------------------------------------
489say "6. an attestation about a different tree is rejected"
490
491git checkout --quiet main
492git checkout --quiet -b c6
493echo "the tree that was actually attested" > docs/readme.md
494git add -A
495git commit --quiet -m "the attested change"
496attest_change c6 "" "" --check prose-format
497c6ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
498 "refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c6 | cut -d: -f2)" | head -n1)"
499
500# A second, different tree — with no attestation of its own — and the
501# first tree's valid, correctly signed note filed under its digest.
502git checkout --quiet -b c6-lifted
503echo "the tree that was not" > docs/readme.md
504git add -A
505git commit --quiet -m "the change the evidence was lifted onto"
506git push --quiet origin c6-lifted
507lifted="$(attest digest --repo "$repo" --rev c6-lifted | cut -d: -f2)"
508git -C "$origin" update-ref \
509 "refs/the-valley/attestations/$lifted/${c6ref##*/}" \
510 "$(git -C "$origin" rev-parse "$c6ref^{tree}")"
511
512request c6 main "$(git rev-parse c6-lifted)"
513integrate
514grep -q "^c6 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lifted attestation was not rejected"
515grep -q 'tree digest mismatch' "$work/last.out" || die "the refusal does not name the tree digest"
516holds "a signature cannot be lifted onto a tree it was not made about"
517forget c6
518
519# ----------------------------------------------------------------------
520say "7. an attestation lying about its kind is rejected"
521
522# The policy runs prose-format with nix, so its evidence is content-
523# addressed. This attestation claims a notarization instead, and its class
524# was untouched by the intervening landing — so under the rule it nominated
525# for itself it would sail onto a base it was never produced over.
526git checkout --quiet main
527git pull --quiet --ff-only origin main
528git checkout --quiet -b c7
529echo "a readme attested as a notarization" > docs/readme.md
530git add -A
531git commit --quiet -m "a change whose attestation lies about its kind"
532attest_change c7 "" "" --command prose-format=true
533c7="$(git rev-parse c7)"
534
535git checkout --quiet main
536git checkout --quiet -b under-c7
537echo "a fourth source file" > src/fourth.txt
538git add -A
539git commit --quiet -m "add a fourth source file"
540attest_change under-c7 "" "" --check code-build
541request under-c7 main "$(git rev-parse under-c7)"
542integrate
543grep -q "^under-c7 -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
544
545request c7 main "$c7"
546integrate
547grep -q "^c7 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lie about the kind was not rejected"
548grep -q 'requires a pure check and the attestation claims an effectful check' "$work/last.out" \
549 || die "the refusal does not say the policy decides the rule"
550holds "the transfer rule follows the policy's runner, never the attestation's own claim"
551forget c7
552
553# ----------------------------------------------------------------------
554say "8. evidence pushed after a no-evidence verdict re-judges and lands"
555
556git checkout --quiet main
557git pull --quiet --ff-only origin main
558git checkout --quiet -b c8
559echo "a readme with no evidence yet" > docs/readme.md
560git add -A
561git commit --quiet -m "a change submitted before its checks ran"
562git push --quiet origin c8
563request c8 main "$(git rev-parse c8)"
564integrate
565grep -q "^c8 -> refs/heads/main .*: stale$" "$work/last.out" || die "a change with no evidence was not stale"
566grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
567 || die "the absent attestation was not reported"
568
569# Nothing about the request or the tip moves; only the evidence arrives.
570attest_change c8 "" "" --check prose-format
571integrate
572grep -q "^c8 -> refs/heads/main .*: land$" "$work/last.out" \
573 || die "the request was not re-judged after its evidence arrived"
574[ "$(tip)" = "$(git rev-parse c8)" ] || die "main is not at the change's head"
575holds "a pass re-judges when evidence arrives, with the request and the tip unmoved"
576
577# ----------------------------------------------------------------------
578say "9. a ref that is not a request does not hold up one that is"
579
580git checkout --quiet main
581git pull --quiet --ff-only origin main
582git checkout --quiet -b c9
583echo "a readme beside a garbage ref" > docs/readme.md
584git add -A
585git commit --quiet -m "a change beside a malformed request"
586attest_change c9 "" "" --check prose-format
587# A name with no target segment at all, and one with a segment too many.
588# This repository is served with no pre-receive hook, so the namespace takes
589# whatever is pushed; on a host only a holder of the request grant writes
590# it, and a holder can still write a name that is not a request.
591git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/garbage"
592git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/main/too/deep"
593request c9 main "$(git rev-parse c9)"
594integrate
595grep -q 'refs/the-valley/integration-requests/garbage: not a request ref' "$work/last.out" \
596 || die "the malformed ref was not reported"
597grep -q 'refs/the-valley/integration-requests/main/too/deep: not a request ref' "$work/last.out" \
598 || die "the over-deep ref was not reported"
599grep -q "^c9 -> refs/heads/main .*: land$" "$work/last.out" \
600 || die "a malformed ref stopped the pass reaching a valid request"
601holds "malformed refs are reported and skipped; the valid request beside them landed"
602git -C "$origin" update-ref -d refs/the-valley/integration-requests/garbage
603git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/too/deep
604
605# ----------------------------------------------------------------------
606say "10. a pass leaves no scratch behind, and a leaked worktree is swept"
607
608# Nine scenarios of real passes, landings and rejections alike, and the
609# scratch root is empty: every worktree a pass takes is given back on the
610# way out, on the failure path as much as the success one.
611leftover="$(find "$TMPDIR" -maxdepth 1 -name 'valley-integrator-wt*')"
612[ -z "$leftover" ] || die "nine passes left scratch behind: $leftover"
613[ "$(git -C "$origin" worktree list --porcelain | grep -c '^worktree ')" -eq 1 ] \
614 || die "nine passes left a worktree attached to the repository"
615holds "no pass left a worktree or a scratch directory behind"
616
617# What a crash leaves: the checkout and its administrative entry, both
618# still valid, so nothing prunes them. Beside it, a worktree of the kind
619# an operator might attach by hand, which is not the integrator's to
620# remove.
621leak="$TMPDIR/valley-integrator-wt-crashed"
622mine="$work/an-operators-own-worktree"
623git -C "$origin" worktree add --quiet --detach "$leak" refs/heads/main
624git -C "$origin" worktree add --quiet --detach "$mine" refs/heads/main
625timeout 5 integrator watch --interval 1s \
626 --repo "$origin" \
627 --key "$work/integrator" --name "$integrator_name" \
628 --known-signers "$work/known_signers" \
629 --instance "$instance" \
630 --schema "$SCHEMA_VERIFICATION" \
631 --attest-schema "$SCHEMA_ATTESTATION" \
632 --event-schema "$SCHEMA_EVENTS" \
633 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
634 > "$work/watch.out" 2>&1 || true
635[ ! -e "$leak" ] || die "a leaked scratch worktree survived the sweep"
636if git -C "$origin" worktree list --porcelain | grep -q "^worktree $leak$"; then
637 die "the leaked worktree is gone from disk and still registered"
638fi
639[ -e "$mine" ] || die "the sweep removed a worktree the integrator did not make"
640git -C "$origin" worktree remove --force "$mine"
641holds "a crash-leaked scratch worktree is swept at watch start; another hand's is not"
642
643# ----------------------------------------------------------------------
644say "11. a scratch root it may not write is reported as that, and nothing else"
645
646# The deployment failure this guards is a sandbox failure: the root TMPDIR
647# names is not writable. That is a statement about the unit, not about the
648# change being judged, and it has to arrive as one — with the path and where
649# the path came from — rather than as git complaining about the leading
650# directories of a .git file.
651refused="$work/refused"
652mkdir -p "$refused"
653chmod a-w "$refused"
654git checkout --quiet main
655git pull --quiet --ff-only origin main
656git checkout --quiet -b c10
657echo "a change that never gets a worktree" > docs/readme.md
658git add -A
659git commit --quiet -m "a change the integrator cannot make scratch for"
660attest_change c10 "" "" --check prose-format
661request c10 main "$(git rev-parse c10)"
662export TMPDIR="$refused"
663integrate || true
664export TMPDIR="$work/tmp"
665chmod u+w "$refused"
666grep -q "scratch directory under $refused, which TMPDIR names" "$work/last.out" \
667 || die "an unwritable scratch root was not reported as one: $(cat "$work/last.out")"
668forget c10
669holds "an unwritable scratch root names the path and where it came from"
670
671# ----------------------------------------------------------------------
672say "12. a floor the instance repository does not carry names the directory looked in"
673
674# The deployment failure this guards is a controller pointed at the wrong
675# directory: the floor is somewhere, and not where this controller looked.
676# The report has to name the repository, the directory and the ref, because
677# those three are what the operator compares against the repository in front
678# of them. A floor is either not there at all, or there under a name that
679# holds no documents, and both arrive that way.
680#
681# A missing floor is refused rather than read as requiring nothing, which is
682# the direction the other layer's absence defaults in (dcr-f41f718). The
683# refusal says why in its own sentence, because the operator reading it is
684# being told that the deployment is not one that can land anything.
685git checkout --quiet main
686git pull --quiet --ff-only origin main
687git checkout --quiet -b c11
688echo "a change judged under no floor" > docs/readme.md
689git add -A
690git commit --quiet -m "a change whose floor the controller cannot find"
691attest_change c11 "" "" --check prose-format
692request c11 main "$(git rev-parse c11)"
693
694# The sentence every refusal to find a floor ends with.
695unwritten="a valley without a written floor is not a valley whose changes land"
696
697floorless() {
698 integrator reconcile \
699 --repo "$origin" \
700 --key "$work/integrator" --name "$integrator_name" \
701 --known-signers "$work/known_signers" "$@" \
702 --schema "$SCHEMA_VERIFICATION" \
703 --attest-schema "$SCHEMA_ATTESTATION" \
704 --event-schema "$SCHEMA_EVENTS" \
705 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
706 2>&1 | tee "$work/last.out"
707}
708
709# The project's own repository carries policy/project and no policy/instance,
710# so the default directory is not there.
711floorless --instance-repo "$origin" || true
712grep -qF "no instance policy layer: $origin has no policy/instance/ at refs/heads/main: $unwritten" "$work/last.out" \
713 || die "a missing floor did not name the directory looked in, and why that is refused: $(cat "$work/last.out")"
714
715# And the flat layout, which is a directory that exists and holds no
716# documents of its own: policy/ has the two layers under it and no *.cue in
717# it. Said as that, rather than as an empty composition further on.
718#
719# Under a second request name, because the refusal above is now this
720# controller's recorded answer to c11 and it stands until the request, the
721# tip or the evidence moves — which is scenario 14's whole subject. A
722# request is what an answer is remembered against, so a second one asks the
723# question again.
724request c11-flat main "$(git rev-parse c11)"
725floorless --instance-repo "$origin" --instance-policy policy || true
726grep -qF "no instance policy layer: $origin holds no *.cue in policy/ at refs/heads/main: $unwritten" "$work/last.out" \
727 || die "a floor directory holding no documents did not say so: $(cat "$work/last.out")"
728
729# And the materialized layer, which is how a host that serves no instance
730# repository is given the floor. A directory with nothing in it is the same
731# absence and is refused the same way.
732request c11-bare main "$(git rev-parse c11)"
733nofloor="$work/no-floor-here"
734mkdir -p "$nofloor"
735floorless --instance "$nofloor" || true
736grep -qF "no instance policy layer: $nofloor holds no *.cue: $unwritten" "$work/last.out" \
737 || die "an empty materialized floor was not refused: $(cat "$work/last.out")"
738forget c11
739forget c11-flat
740forget c11-bare
741holds "a floor that is not where the controller looked names the repository, the directory and the ref"
742
743# ----------------------------------------------------------------------
744say "13. a change the policy requires no check of lands, and says so"
745
746# Requiring nothing is an ordinary outcome, not an edge: this project's
747# classes cover docs/, src/ and deploy/, and its `unclassified` set is
748# empty, so a path outside all three is answered with nothing required. The
749# landing still writes a transfer statement — a commit point that records
750# nothing is a landing nothing attests to — and that statement says the
751# required set was empty rather than carrying a list with nothing in it,
752# which has no written form at all.
753git checkout --quiet main
754git pull --quiet --ff-only origin main
755git checkout --quiet -b c12
756echo "outside every class this policy covers" > notes.txt
757git add -A
758git commit --quiet -m "a change no class covers"
759request c12 main "$(git rev-parse c12)"
760integrate
761grep -q '^c12 -> refs/heads/main .*: land$' "$work/last.out" \
762 || die "a change with nothing required did not land: $(cat "$work/last.out")"
763grep -q 'the policy requires no check of the paths this change touches' "$work/last.out" \
764 || die "the verdict did not say the policy required nothing"
765[ "$(tip)" = "$(git rev-parse c12)" ] || die "c12 did not land"
766git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c12" > /dev/null \
767 && die "the request ref survived the landing"
768
769nothing="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c12 | cut -d: -f2)/$ihash"
770git -C "$origin" cat-file blob "$nothing:transfer.note" > "$work/nothing.note"
771grep -qx 'predicate.required none' "$work/nothing.note" \
772 || die "the transfer statement does not say the required set was empty: $(cat "$work/nothing.note")"
773grep -q '^predicate.checks' "$work/nothing.note" \
774 && die "the transfer statement carries a check list beside the empty-set line"
775attest verify --note "$work/nothing.note" --known-keys "$work/reader_keys" \
776 --repo "$repo" --rev c12 --signer "$integrator_name" > /dev/null \
777 || die "the transfer statement for a landing with nothing required does not verify"
778holds "a landing with no required checks is signed, stored, and consumed its request"
779
780# ----------------------------------------------------------------------
781say "14. a request the controller cannot judge is reported once, not every tick"
782
783# The failure this guards is the one that turns a broken deployment into a
784# broken deployment nobody can read: a pass that cannot act on its verdict,
785# retried at tick rate, filling the journal with one refusal per interval
786# forever. The same request, tip and evidence produce the same failure, so
787# repeating it says nothing new. It is reported once and then the answer
788# stands, exactly as an outcome stands.
789git checkout --quiet main
790git pull --quiet --ff-only origin main
791git checkout --quiet -b c13
792echo "a change judged under no floor, repeatedly" > docs/readme.md
793git add -A
794git commit --quiet -m "a change whose floor the controller cannot find, under watch"
795attest_change c13 "" "" --check prose-format
796request c13 main "$(git rev-parse c13)"
797
798# Five seconds at a one-second tick: five passes over one request the
799# controller cannot judge.
800timeout 5 integrator watch --interval 1s \
801 --repo "$origin" \
802 --key "$work/integrator" --name "$integrator_name" \
803 --known-signers "$work/known_signers" \
804 --instance-repo "$origin" --instance-policy policy \
805 --schema "$SCHEMA_VERIFICATION" \
806 --attest-schema "$SCHEMA_ATTESTATION" \
807 --event-schema "$SCHEMA_EVENTS" \
808 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
809 > "$work/repeat.out" 2>&1 || true
810reports="$(grep -c 'no instance policy layer' "$work/repeat.out" || true)"
811[ "$reports" -eq 1 ] \
812 || die "the same refusal was reported $reports times over five ticks: $(cat "$work/repeat.out")"
813forget c13
814holds "a failure a pass cannot get past is reported once and then backs off"
815
816# ----------------------------------------------------------------------
817say "15. a target that carries no project layer of its own is judged under the floor alone"
818
819# The bootstrap this guards is the first policy commit: a project that has
820# not written a layer yet cannot land the commit that writes one, because a
821# change never supplies the policy that gates it. The project layer only
822# ever adds (dcr-f41f718), so its absence adds nothing, and what is left is
823# the floor — which still governs, and is observed governing here.
824
825# The floor grows a requirement of its own first, so that what remains after
826# the project layer goes away is something rather than nothing.
827cat > "$floor/policy/instance/floor.cue" <<'EOF'
828package verification
829
830floor: {
831 checks: "prose-format": {runner: "nix", attribute: "prose-format"}
832 classes: prose: {paths: "docs/**": true, requires: "prose-format": true}
833 unclassified: {}
834}
835EOF
836git -C "$floor" commit --quiet -am "the floor requires prose-format of docs/"
837git -C "$floor" push --quiet "$instance_repo" main
838
839# The project stops carrying a layer. Nothing classifies policy/project
840# itself, so this lands with nothing required — which is how a project could
841# have arrived without one in the first place.
842git checkout --quiet main
843git pull --quiet --ff-only origin main
844git checkout --quiet -b c14
845git rm --quiet -r policy/project
846git commit --quiet -m "stop carrying a project policy layer"
847request c14 main "$(git rev-parse c14)"
848integrate
849grep -q "^c14 -> refs/heads/main .*: land$" "$work/last.out" \
850 || die "dropping the project layer did not land: $(cat "$work/last.out")"
851
852git checkout --quiet main
853git pull --quiet --ff-only origin main
854[ ! -e policy/project ] || die "the project layer is still at the tip; the scenario proves nothing"
855git checkout --quiet -b c15
856echo "a readme judged under the floor alone" > docs/readme.md
857git add -A
858git commit --quiet -m "a change to a project that declares no policy of its own"
859attest_change c15 "" "" --check prose-format
860request c15 main "$(git rev-parse c15)"
861integrate
862grep -q "^c15 -> refs/heads/main .*: land$" "$work/last.out" \
863 || die "a change to a project with no layer of its own did not land: $(cat "$work/last.out")"
864grep -qF "policy no project layer at policy/project; the floor alone" "$work/last.out" \
865 || die "the pass did not say it composed the floor alone"
866grep -q "check prose-format .*transferred" "$work/last.out" \
867 || die "the floor's own requirement did not govern a project with no layer"
868[ "$(tip)" = "$(git rev-parse c15)" ] || die "main is not at the change's head"
869holds "no project layer is no addition; the floor alone required prose-format and the change landed"
870
871# ----------------------------------------------------------------------
872say "16. a floor that requires nothing, written down, composes and judges"
873
874# Emptiness a floor states is not the same thing as a floor nobody wrote.
875# The document is there, somebody landed it, and what it composes to is a
876# policy that requires nothing — so a change under it lands requiring nothing,
877# rather than being refused the way scenario 12's missing floor is.
878cat > "$floor/policy/instance/floor.cue" <<'EOF'
879package verification
880
881floor: {
882 checks: {}
883 classes: {}
884 unclassified: {}
885}
886EOF
887git -C "$floor" commit --quiet -am "the floor requires nothing, and says so"
888git -C "$floor" push --quiet "$instance_repo" main
889
890git checkout --quiet main
891git pull --quiet --ff-only origin main
892git checkout --quiet -b c16
893echo "a readme under a floor that requires nothing" > docs/readme.md
894git add -A
895git commit --quiet -m "a change under an empty floor and no project layer"
896request c16 main "$(git rev-parse c16)"
897integrate
898grep -q "^c16 -> refs/heads/main .*: land$" "$work/last.out" \
899 || die "a change under an explicitly empty floor did not land: $(cat "$work/last.out")"
900grep -q 'the policy requires no check of the paths this change touches' "$work/last.out" \
901 || die "the verdict did not say the policy required nothing"
902grep -qF "policy no project layer at policy/project; the floor alone" "$work/last.out" \
903 || die "the pass did not say it composed the floor alone"
904[ "$(tip)" = "$(git rev-parse c16)" ] || die "main is not at the change's head"
905holds "a floor that states it requires nothing composes, judges, and lands the change requiring nothing"
906
907# ----------------------------------------------------------------------
908say "17. a planted replacement ref changes nothing the controller reads"
909
910# refs/replace/<id> makes git read another object wherever it looks <id> up.
911# Two are planted here, and each alone would land a change that requires
912# evidence and carries none. One swaps the floor's blob in the instance
913# repository for a floor that requires nothing. The other swaps the change's
914# head in the served repository for a commit that touches no class. Neither
915# moves a ref the controller watches. A controller that left replacement
916# refs on would read both and land the change; this one reads the objects
917# the refs actually name, and holds the change stale.
918cat > "$floor/policy/instance/floor.cue" <<'EOF'
919package verification
920
921floor: {
922 checks: "prose-format": {runner: "nix", attribute: "prose-format"}
923 classes: prose: {paths: "docs/**": true, requires: "prose-format": true}
924 unclassified: {}
925}
926EOF
927git -C "$floor" commit --quiet -am "the floor requires prose-format of docs/ again"
928git -C "$floor" push --quiet "$instance_repo" main
929landed_floor="$(git -C "$instance_repo" rev-parse main:policy/instance/floor.cue)"
930empty_floor="$(git -C "$instance_repo" rev-parse main~1:policy/instance/floor.cue)"
931git -C "$instance_repo" update-ref "refs/replace/$landed_floor" "$empty_floor"
932git -C "$instance_repo" show main:policy/instance/floor.cue | grep 'checks: {}' > /dev/null \
933 || die "the floor's replacement did not take, so this scenario proves nothing"
934
935git checkout --quiet main
936git pull --quiet --ff-only origin main
937git checkout --quiet -b c17-decoy
938echo "a note no class covers" > notes.txt
939git commit --quiet -am "a decoy that touches no class"
940git checkout --quiet main
941git checkout --quiet -b c17
942echo "a readme no evidence covers" > docs/readme.md
943git commit --quiet -am "a change that requires prose-format and carries no evidence"
944git push --quiet origin c17 c17-decoy
945git -C "$origin" update-ref "refs/replace/$(git rev-parse c17)" "$(git rev-parse c17-decoy)"
946git -C "$origin" diff --name-only main c17 | grep -x notes.txt > /dev/null \
947 || die "the head's replacement did not take, so this scenario proves nothing"
948request c17 main "$(git rev-parse c17)"
949
950# The environment asks for replacement refs too, and is not listened to.
951before="$(tip)"
952GIT_CONFIG_PARAMETERS="'core.usereplacerefs'='true'" integrate
953grep -q "^c17 -> refs/heads/main .*: stale$" "$work/last.out" \
954 || die "a planted replacement ref decided the verdict: $(cat "$work/last.out")"
955grep -q "check prose-format" "$work/last.out" \
956 || die "the landed floor's requirement was not what the controller read"
957grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
958 || die "the missing evidence was not reported"
959[ "$(tip)" = "$before" ] || die "main moved under a change that requires evidence"
960git -C "$instance_repo" update-ref -d "refs/replace/$landed_floor"
961git -C "$origin" update-ref -d "refs/replace/$(git rev-parse c17)"
962forget c17
963holds "the floor and the change were read as landed and as pushed, past both replacements"
964
965# ----------------------------------------------------------------------
966say "18. a request approved again while its previous head lands stands for the next pass"
967
968# A second approval is leased against the request it read, and
969# nothing stops it reading the head this pass is about to land. So the
970# request can move between the pass reading it and the pass consuming it.
971# The landing is still the old head's, and the request that moved is a new
972# request: consuming it would drop a change nobody judged.
973git checkout --quiet main
974git pull --quiet --ff-only origin main
975git checkout --quiet -b c18
976echo "a readme requested once" > docs/readme.md
977git commit --quiet -am "the first request"
978attest_change c18 "" "" --check prose-format
979request c18 main "$(git rev-parse c18)"
980# main moves under the request, outside anything the check reads. The pass
981# then recomputes the check's closure over the landed tree, and that
982# evaluation is where the second approval happens.
983git checkout --quiet main
984echo "a note landed in between" > notes.txt
985git commit --quiet -am "a note landed in between"
986git push --quiet origin main
987git checkout --quiet c18
988git checkout --quiet -b c18-again
989echo "a readme requested again" > docs/readme.md
990git commit --quiet -am "the second request"
991git push --quiet origin c18-again
992again="$(git rev-parse c18-again)"
993moving="refs/the-valley/integration-requests/main/c18"
994before="$(tip)"
995VALLEY_E2E_DURING_PASS="git -C '$origin' update-ref '$moving' '$again'" integrate
996grep -q "^c18 -> refs/heads/main .*: land$" "$work/last.out" \
997 || die "the first request did not land: $(cat "$work/last.out")"
998[ "$(tip)" != "$before" ] || die "main did not move for the first request"
999[ "$(git -C "$origin" rev-parse --verify --quiet "$moving")" = "$again" ] \
1000 || die "the request approved again was consumed with the landing of the one before it: $(git -C "$origin" for-each-ref refs/the-valley/integration-requests) $(cat "$work/last.out")"
1001grep -q "request $moving moved to .* while .* landed; it stands for the next pass" "$work/last.out" \
1002 || die "the moved request was not reported: $(cat "$work/last.out")"
1003forget c18
1004holds "a request that moved while its previous head landed was left standing, and said so"
1005
1006# ----------------------------------------------------------------------
1007say "19. a bus that never answers does not hold up a landing"
1008
1009# The landing's own record comes first: main moves, the move is queued for
1010# the mirrors, the evidence is stored and the request consumed. Only then
1011# does the integrator publish, and each publish is cut off at its deadline.
1012# The shipped wrapper puts the real nats first on the path, so the bare
1013# binary runs here, behind a nats that never exits.
1014mkdir "$work/hung-bus" "$work/queue"
1015cat > "$work/hung-bus/nats" <<EOF
1016#!$(command -v bash)
1017exec sleep 1000
1018EOF
1019chmod +x "$work/hung-bus/nats"
1020git checkout --quiet main
1021git pull --quiet --ff-only origin main
1022git checkout --quiet -b c19
1023echo "landed past a bus that never answers" > docs/bus.md
1024git add -A
1025git commit --quiet -m "past a hung bus"
1026attest_change c19 "" "" --check prose-format
1027request c19 main "$(git rev-parse c19)"
1028before="$(tip)"
1029started=$SECONDS
1030PATH="$work/hung-bus:$PATH" timeout 120 "$INTEGRATOR_UNWRAPPED" reconcile \
1031 --repo "$origin" \
1032 --key "$work/integrator" --name "$integrator_name" \
1033 --known-signers "$work/known_signers" \
1034 --instance-repo "$instance_repo" \
1035 --schema "$SCHEMA_VERIFICATION" \
1036 --attest-schema "$SCHEMA_ATTESTATION" \
1037 --event-schema "$SCHEMA_EVENTS" \
1038 --bus nats://127.0.0.1:1 \
1039 --publish-queue "$work/queue" \
1040 2>&1 | tee "$work/last.out" \
1041 || die "the pass did not finish with a bus that never answers: $(cat "$work/last.out")"
1042elapsed=$((SECONDS - started))
1043grep -q "^c19 -> refs/heads/main .*: land$" "$work/last.out" || die "c19 did not land: $(cat "$work/last.out")"
1044[ "$(tip)" = "$(git rev-parse c19)" ] || die "main is not at c19's head"
1045[ "$(cat "$work"/queue/*)" = "$before $(git rev-parse c19) refs/heads/main" ] \
1046 || die "the move was not queued for the mirrors: $(ls -A "$work/queue")"
1047git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c19 > /dev/null \
1048 && die "the request outlived the landing"
1049grep -q "^ evidence " "$work/last.out" || die "the evidence was not stored"
1050first_evidence="$(grep -n "^ evidence " "$work/last.out" | head -n1 | cut -d: -f1)"
1051first_bus="$(grep -n "^ bus " "$work/last.out" | head -n1 | cut -d: -f1)"
1052[ "$first_evidence" -lt "$first_bus" ] || die "the integrator asked the bus before it stored the evidence"
1053grep -q "bus publish of valley.git.project.ref-updated FAILED: no answer within 10s" "$work/last.out" \
1054 || die "the hung ref-updated publish was not cut off: $(cat "$work/last.out")"
1055grep -q "bus publish of valley.git.project.integration-succeeded FAILED: no answer within 10s" "$work/last.out" \
1056 || die "the hung integration-succeeded publish was not cut off"
1057[ "$elapsed" -lt 60 ] || die "the pass took ${elapsed}s behind a bus that never answers"
1058holds "c19 landed, queued and consumed its request before a hung bus was asked, and each publish gave up after 10s (${elapsed}s in all)"
1059
1060# ----------------------------------------------------------------------
1061say "20. a cue that never answers a payload's vet does not hold up a landing"
1062
1063# The payload is vetted before it is published, and the vet is bounded the
1064# same way. This cue hangs only when asked to vet against the event schema;
1065# every other use, judging included, goes to the real one.
1066real_cue="$(command -v cue)"
1067mkdir "$work/hung-cue" "$work/queue20"
1068cat > "$work/hung-cue/cue" <<EOF
1069#!$(command -v bash)
1070for arg; do [ "\$arg" = "$SCHEMA_EVENTS" ] && exec sleep 1000; done
1071exec "$real_cue" "\$@"
1072EOF
1073chmod +x "$work/hung-cue/cue"
1074git checkout --quiet main
1075git pull --quiet --ff-only origin main
1076git checkout --quiet -b c20
1077echo "landed past a cue that never answers" > docs/cue.md
1078git add -A
1079git commit --quiet -m "past a hung cue"
1080attest_change c20 "" "" --check prose-format
1081request c20 main "$(git rev-parse c20)"
1082before="$(tip)"
1083started=$SECONDS
1084PATH="$work/hung-cue:$PATH" timeout 120 "$INTEGRATOR_UNWRAPPED" reconcile \
1085 --repo "$origin" \
1086 --key "$work/integrator" --name "$integrator_name" \
1087 --known-signers "$work/known_signers" \
1088 --instance-repo "$instance_repo" \
1089 --schema "$SCHEMA_VERIFICATION" \
1090 --attest-schema "$SCHEMA_ATTESTATION" \
1091 --event-schema "$SCHEMA_EVENTS" \
1092 --publish-queue "$work/queue20" \
1093 2>&1 | tee "$work/last.out" \
1094 || die "the pass did not finish with a cue that never answers: $(cat "$work/last.out")"
1095elapsed=$((SECONDS - started))
1096grep -q "^c20 -> refs/heads/main .*: land$" "$work/last.out" || die "c20 did not land: $(cat "$work/last.out")"
1097[ "$(tip)" = "$(git rev-parse c20)" ] || die "main is not at c20's head"
1098[ "$(cat "$work"/queue20/*)" = "$before $(git rev-parse c20) refs/heads/main" ] \
1099 || die "the move was not queued for the mirrors: $(ls -A "$work/queue20")"
1100git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/c20 > /dev/null \
1101 && die "the request outlived the landing"
1102grep -q "^ evidence " "$work/last.out" || die "the evidence was not stored"
1103grep -q "bus ref-updated not published: cue vet against #RefUpdated gave no answer within 10s" "$work/last.out" \
1104 || die "the hung vet of ref-updated was not cut off: $(cat "$work/last.out")"
1105grep -q "bus integration-succeeded not published: cue vet against #IntegrationSucceeded gave no answer within 10s" "$work/last.out" \
1106 || die "the hung vet of integration-succeeded was not cut off"
1107[ "$elapsed" -lt 60 ] || die "the pass took ${elapsed}s behind a cue that never answers"
1108holds "c20 landed, queued and consumed its request, and each hung vet gave up after 10s and skipped its publish (${elapsed}s in all)"
1109
1110# ----------------------------------------------------------------------
1111say "21. a request named for a change that is ready lands"
1112
1113# A change with a record is requested as c-<change ID>. The records are
1114# written by the valley command itself, as its owner would write them; this
1115# repository is served without the hook that guards them on a host. The
1116# integrator reads them only to refuse.
1117git config valley.principal contributor
1118# ready_change <branch>: the branch's head on origin readied as a new
1119# change, whose ID is left in $readied.
1120ready_change() {
1121 valley ready "$1" > "$work/ready.out" || die "valley ready $1 failed"
1122 readied="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$work/ready.out")"
1123 [ "${#readied}" = 32 ] || die "valley ready $1 made no change ID: $(cat "$work/ready.out")"
1124}
1125outcome_of() { git -C "$origin" cat-file blob "refs/the-valley/integration-outcomes/main/$1" | cut -d' ' -f4; }
1126
1127git checkout --quiet main
1128git pull --quiet --ff-only origin main
1129git checkout --quiet -b c21
1130echo "a change with a record" > docs/c21.md
1131git add -A
1132git commit --quiet -m "a change with a record"
1133attest_change c21 "" "" --check prose-format
1134ready_change c21
1135id21="$readied"
1136request "c-$id21" main "$(git rev-parse c21)"
1137integrate
1138grep -q "^c-$id21 -> refs/heads/main .*: land$" "$work/last.out" || die "c-$id21 did not land"
1139[ "$(tip)" = "$(git rev-parse c21)" ] || die "main is not at c21's head"
1140landed="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c21 | cut -d: -f2)/$ihash"
1141git -C "$origin" cat-file blob "$landed:transfer.note" | grep -qx "predicate.change c-$id21" \
1142 || die "the transfer statement does not name the request c-$id21"
1143holds "a ready change's request, named for its ID, landed, and its transfer statement names it so"
1144
1145# ----------------------------------------------------------------------
1146say "22. a change unreadied after its request was filed is refused, and lands once it is ready again"
1147
1148git checkout --quiet main
1149git pull --quiet --ff-only origin main
1150git checkout --quiet -b c22
1151echo "a change paused after it was approved" > docs/c22.md
1152git add -A
1153git commit --quiet -m "a change paused after it was approved"
1154attest_change c22 "" "" --check prose-format
1155ready_change c22
1156id22="$readied"
1157request "c-$id22" main "$(git rev-parse c22)"
1158valley unready "$id22" > /dev/null 2>&1 || die "valley unready failed"
1159before="$(tip)"
1160integrate
1161grep -q "^c-$id22 -> refs/heads/main .*: refused-not-ready$" "$work/last.out" \
1162 || die "a draft's request was not refused: $(cat "$work/last.out")"
1163grep -q "change $id22 is draft, not ready" "$work/last.out" || die "the refusal did not say the change is a draft"
1164[ "$(tip)" = "$before" ] || die "a draft's request moved main"
1165git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c-$id22" > /dev/null \
1166 || die "the refused request was dropped"
1167[ "$(outcome_of "c-$id22")" = refused-not-ready ] || die "the outcome record does not say refused-not-ready"
1168
1169# The refusal is decided again on every pass, and said once.
1170timeout 4 integrator watch --interval 1s \
1171 --repo "$origin" \
1172 --key "$work/integrator" --name "$integrator_name" \
1173 --known-signers "$work/known_signers" \
1174 --instance-repo "$instance_repo" \
1175 --schema "$SCHEMA_VERIFICATION" \
1176 --attest-schema "$SCHEMA_ATTESTATION" \
1177 --event-schema "$SCHEMA_EVENTS" \
1178 > "$work/repeat.out" 2>&1 || true
1179[ "$(grep -c "c-$id22 .*refused-not-ready" "$work/repeat.out" || true)" -eq 0 ] \
1180 || die "a standing refusal was reported again: $(cat "$work/repeat.out")"
1181[ "$(tip)" = "$before" ] || die "a draft's request moved main under watch"
1182
1183# Readied again at the same head, the same request lands on the next pass.
1184valley ready --change "$id22" c22 > /dev/null 2>&1 || die "valley ready --change $id22 failed"
1185integrate
1186grep -q "^c-$id22 -> refs/heads/main .*: land$" "$work/last.out" \
1187 || die "the change readied again did not land: $(cat "$work/last.out")"
1188[ "$(tip)" = "$(git rev-parse c22)" ] || die "main is not at c22's head"
1189holds "unready stopped a filed request, the refusal was said once, and ready let the same request land"
1190
1191# ----------------------------------------------------------------------
1192say "23. a request named for a branch, at a draft's head, is refused"
1193
1194# Whatever a request is named, a head that is a version of a change that
1195# is not ready does not land.
1196git checkout --quiet main
1197git pull --quiet --ff-only origin main
1198git checkout --quiet -b c23
1199echo "a draft requested under its branch's name" > docs/c23.md
1200git add -A
1201git commit --quiet -m "a draft requested under its branch's name"
1202attest_change c23 "" "" --check prose-format
1203ready_change c23
1204id23="$readied"
1205valley unready "$id23" > /dev/null 2>&1 || die "valley unready failed"
1206request c23 main "$(git rev-parse c23)"
1207before="$(tip)"
1208integrate
1209grep -q "^c23 -> refs/heads/main .*: refused-not-ready$" "$work/last.out" \
1210 || die "a branch-named request at a draft's head was not refused: $(cat "$work/last.out")"
1211grep -q "is v1 of change $id23, which is draft, not ready" "$work/last.out" \
1212 || die "the refusal did not name the draft"
1213[ "$(tip)" = "$before" ] || die "a draft's head landed under its branch's name"
1214[ "$(outcome_of c23)" = refused-not-ready ] || die "the outcome record does not say refused-not-ready"
1215forget c23
1216holds "a draft's head was refused under a branch's name too"
1217
1218# ----------------------------------------------------------------------
1219say "24. a request whose commits hold a merge is refused"
1220
1221# A merge can make other work look landed by ancestry, so the integrator
1222# lands a line of commits and nothing else.
1223git checkout --quiet main
1224git pull --quiet --ff-only origin main
1225git checkout --quiet -b c24-side
1226echo "a side line" > docs/c24-side.md
1227git add -A
1228git commit --quiet -m "a side line"
1229git checkout --quiet main
1230git checkout --quiet -b c24
1231echo "a line with a merge in it" > docs/c24.md
1232git add -A
1233git commit --quiet -m "a line with a merge in it"
1234git merge --quiet --no-ff --no-edit c24-side
1235git push --quiet origin c24
1236request c24 main "$(git rev-parse c24)"
1237before="$(tip)"
1238integrate
1239grep -q "^c24 -> refs/heads/main .*: refused-merge$" "$work/last.out" \
1240 || die "a request holding a merge was not refused: $(cat "$work/last.out")"
1241grep -q "include the merge $(git rev-parse c24 | cut -c1-12)" "$work/last.out" || die "the refusal did not name the merge"
1242[ "$(tip)" = "$before" ] || die "a request holding a merge moved main"
1243[ "$(outcome_of c24)" = refused-merge ] || die "the outcome record does not say refused-merge"
1244forget c24
1245holds "a request holding a merge was refused, and main did not move"
1246
1247# ----------------------------------------------------------------------
1248say "25. a line of commits above a draft's head is refused, naming the draft"
1249
1250# A request brings every commit from its merge base to its head. So a child
1251# of a draft's head, approved under another name, would land the draft too.
1252git checkout --quiet main
1253git pull --quiet --ff-only origin main
1254git checkout --quiet -b c25-draft
1255echo "a draft below another change" > docs/c25-draft.md
1256git add -A
1257git commit --quiet -m "a draft below another change"
1258git push --quiet origin c25-draft
1259ready_change c25-draft
1260id25="$readied"
1261valley unready "$id25" > /dev/null 2>&1 || die "valley unready failed"
1262git checkout --quiet -b c25
1263echo "a change above a draft" > docs/c25.md
1264git add -A
1265git commit --quiet -m "a change above a draft"
1266git push --quiet origin c25
1267request c25 main "$(git rev-parse c25)"
1268before="$(tip)"
1269integrate
1270grep -q "^c25 -> refs/heads/main .*: refused-not-ready$" "$work/last.out" \
1271 || die "a child of a draft's head was not refused: $(cat "$work/last.out")"
1272grep -q "the commit $(git rev-parse c25-draft | cut -c1-12) it brings is v1 of change $id25, which is draft, not ready" \
1273 "$work/last.out" || die "the refusal did not name the draft"
1274[ "$(tip)" = "$before" ] || die "a child of a draft's head moved main"
1275forget c25
1276holds "a line above a draft's head was refused, and the refusal named the draft"
1277
1278# ----------------------------------------------------------------------
1279say "26. withdrawing a change voids its request, and readying it again needs a fresh approval"
1280
1281# integrate_quiet: a pass that may have nothing to say.
1282integrate_quiet() {
1283 integrator reconcile \
1284 --repo "$origin" \
1285 --key "$work/integrator" --name "$integrator_name" \
1286 --known-signers "$work/known_signers" \
1287 --instance-repo "$instance_repo" \
1288 --schema "$SCHEMA_VERIFICATION" \
1289 --attest-schema "$SCHEMA_ATTESTATION" \
1290 --event-schema "$SCHEMA_EVENTS" \
1291 2>&1 | tee "$work/last.out"
1292}
1293git checkout --quiet main
1294git pull --quiet --ff-only origin main
1295git checkout --quiet -b c26
1296echo "a change withdrawn after it was approved" > docs/c26.md
1297git add -A
1298git commit --quiet -m "a change withdrawn after it was approved"
1299attest_change c26 "" "" --check prose-format
1300ready_change c26
1301id26="$readied"
1302request "c-$id26" main "$(git rev-parse c26)"
1303valley withdraw "$id26" > /dev/null 2>&1 || die "valley withdraw failed"
1304before="$(tip)"
1305integrate
1306grep -q "^c-$id26 -> refs/heads/main .*: voided-withdrawn$" "$work/last.out" \
1307 || die "a withdrawn change's request was not voided: $(cat "$work/last.out")"
1308git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c-$id26" > /dev/null \
1309 && die "the voided request is still there"
1310[ "$(outcome_of "c-$id26")" = voided-withdrawn ] || die "the outcome record does not say voided-withdrawn"
1311[ "$(tip)" = "$before" ] || die "a withdrawn change moved main"
1312
1313# Readied again at the same head, the change has no request: the old
1314# approval is gone, and nothing lands.
1315valley ready --change "$id26" c26 > /dev/null 2>&1 || die "valley ready --change $id26 failed"
1316integrate_quiet
1317git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c-$id26" > /dev/null \
1318 && die "the old request came back"
1319[ "$(tip)" = "$before" ] || die "a change readied again landed with no fresh approval"
1320
1321# A fresh approval at the same head lands.
1322request "c-$id26" main "$(git rev-parse c26)"
1323integrate
1324grep -q "^c-$id26 -> refs/heads/main .*: land$" "$work/last.out" \
1325 || die "the fresh approval did not land: $(cat "$work/last.out")"
1326[ "$(tip)" = "$(git rev-parse c26)" ] || die "main is not at c26's head"
1327holds "withdraw voided the request on the next pass; readied again, the change landed only on a fresh approval"
1328
1329# ----------------------------------------------------------------------
1330say "27. a stack is rebased whole, a landed change under it drops out, and approving its top lands every layer"
1331
1332# approve reads the floor where the integrator does: the owning valley,
1333# which policy/valley names at main's tip.
1334git checkout --quiet main
1335git pull --quiet --ff-only origin main
1336mkdir -p policy
1337echo "$instance_repo" > policy/valley
1338git add policy/valley
1339git commit --quiet -m "name the owning valley"
1340git push --quiet origin main
1341
1342# A change of two commits, a plain branch on it, and two changes on that.
1343git checkout --quiet -b c27-a
1344echo "the first half of a" > docs/c27-a1.md
1345git add -A
1346git commit --quiet -m "c27: the first half of a"
1347echo "the second half of a" > docs/c27-a2.md
1348git add -A
1349git commit --quiet -m "c27: the second half of a"
1350attest_change c27-a "" "" --check prose-format
1351ready_change c27-a
1352id27a="$readied"
1353for layer in p b c; do
1354 git checkout --quiet -b "c27-$layer"
1355 echo "layer $layer" > "docs/c27-$layer.md"
1356 git add -A
1357 git commit --quiet -m "c27: layer $layer"
1358 git push --quiet origin "c27-$layer"
1359done
1360ready_change c27-b
1361id27b="$readied"
1362ready_change c27-c
1363id27c="$readied"
1364a="$(git rev-parse c27-a)" p="$(git rev-parse c27-p)" b="$(git rev-parse c27-b)" c="$(git rev-parse c27-c)"
1365
1366# main moves under the stack, outside what prose-format reads, and then a
1367# lands through an integrator merge: one new commit, not its own two.
1368git checkout --quiet main
1369git checkout --quiet -b c27-x
1370mkdir -p notes
1371echo "main moves on" > notes/c27-x.txt
1372git add -A
1373git commit --quiet -m "c27: main moves on"
1374git push --quiet origin c27-x
1375request c27-x main "$(git rev-parse c27-x)"
1376integrate
1377grep -q "^c27-x -> refs/heads/main .*: land$" "$work/last.out" || die "c27-x did not land"
1378request "c-$id27a" main "$a"
1379integrate
1380grep -q "^c-$id27a -> refs/heads/main .*: land$" "$work/last.out" || die "c-$id27a did not land"
1381main27="$(tip)"
1382git -C "$origin" merge-base --is-ancestor "$a" "$main27" && die "a landed by fast-forward; the scenario proves nothing"
1383
1384# The rebase reads a's landing from its transfer statement, so a is no
1385# layer, and its two commits under the plain branch drop out. Each layer
1386# keeps its own diff, on the rebased layer below.
1387export VALLEY_KNOWN_SIGNERS="$work/reader_keys" VALLEY_INTEGRATOR_SIGNER="$integrator_name"
1388valley status --json "$id27a" | grep -q '"landing": *"landed-recorded"' || die "a's landing was not read as recorded"
1389valley rebase "$id27c" > "$work/rebase.out" 2>&1 || die "the rebase failed: $(cat "$work/rebase.out")"
1390git fetch --quiet origin
1391np="$(git rev-parse origin/c27-p)" nb="$(git rev-parse origin/c27-b)" nc="$(git rev-parse origin/c27-c)"
1392git merge-base --is-ancestor "$main27" "$np" || die "the bottom is not on main"
1393git merge-base --is-ancestor "$np" "$nb" || die "b is not on the rebased plain branch"
1394git merge-base --is-ancestor "$nb" "$nc" || die "c is not on b"
1395[ "$(git rev-list --count "$main27..$np")" = 1 ] || die "a's commits did not drop out from under the plain branch"
1396[ "$(git diff "$a" "$p")" = "$(git diff "$main27" "$np")" ] || die "the plain branch's diff changed"
1397[ "$(git diff "$p" "$b")" = "$(git diff "$np" "$nb")" ] || die "b's diff changed"
1398[ "$(git diff "$b" "$c")" = "$(git diff "$nb" "$nc")" ] || die "c's diff changed"
1399grep -q "^ c27-p .* origin/c27-p pushed$" "$work/rebase.out" || die "the manifest did not list the plain branch: $(cat "$work/rebase.out")"
1400holds "the stack moved whole onto main, and the landed change's commits dropped out from under it"
1401
1402# Approving the rebased top, and landing it, lands every layer.
1403VALLEY_ATTEST_KEY="$work/contributor" VALLEY_ATTEST_NAME="$contributor" \
1404 valley approve --head "$nc" "$id27c" > "$work/approve.out" 2>&1 \
1405 || die "approving the rebased top failed: $(cat "$work/approve.out")"
1406integrate
1407grep -q "^c-$id27c -> refs/heads/main .*: land$" "$work/last.out" || die "c-$id27c did not land"
1408[ "$(tip)" = "$nc" ] || die "main is not at the top's rebased head"
1409for id in "$id27b" "$id27c"; do
1410 valley status --json "$id" | grep -q '"landing": *"landed"' || die "change $id does not read landed"
1411done
1412git -C "$origin" merge-base --is-ancestor "$np" "$(tip)" || die "the plain branch did not land"
1413unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER
1414holds "the rebased stack landed as one fast-forward, and every layer reads landed"
1415
1416printf '\nintegrator-e2e: every scenario held\n'