gunk-dev/the-valley

run.sh

sourcef4ba77eea55dmain's published tip history
43.0 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 "2c. a re-parenting landing states its own committer, and needs none from the environment"
326
327# Re-parenting is the one landing that makes a commit of the integrator's
328# own, so it is the one that needs a committer. Under the unit there is
329# nothing to take one from: no git config the service can read, no GIT_* in
330# its environment, and a hostname git will not guess an address from. So the
331# integrator states both identities itself — the asker authored the change,
332# the integrator committed the commit that carries it onto the tip — and the
333# pass below runs with everything git could have fallen back to taken away.
334
335# A scrubbed environment: no global or system config, no GIT_* identity, and
336# auto-detection refused outright, so that a host whose own hostname does
337# resolve cannot hide a dependency on it.
338scrubbed() {
339 env -u GIT_AUTHOR_NAME -u GIT_AUTHOR_EMAIL -u GIT_AUTHOR_DATE \
340 -u GIT_COMMITTER_NAME -u GIT_COMMITTER_EMAIL -u GIT_COMMITTER_DATE -u EMAIL \
341 GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 \
342 GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.useConfigOnly GIT_CONFIG_VALUE_0=true \
343 "$@"
344}
345scrubbed git -C "$origin" var GIT_COMMITTER_IDENT > /dev/null 2>&1 \
346 && die "the scrub left an identity for git to find; the scenario proves nothing"
347
348git checkout --quiet main
349git pull --quiet --ff-only origin main
350git checkout --quiet -b c2c
351echo "a readme revised by a stated author" > docs/readme.md
352git add -A
353# A distinct author, so that what the landed commit carries over from the
354# head and what it states of its own are told apart by reading it.
355GIT_AUTHOR_NAME="a contributor" GIT_AUTHOR_EMAIL="contributor@valley.invalid" \
356 git commit --quiet -m "revise the readme under an author of its own"
357attest_change c2c "" "" --check prose-format
358c2c="$(git rev-parse c2c)"
359
360# The intervening landing, outside what prose-format reads, is what moves
361# c2c's base and makes the landing below a re-parent rather than a
362# fast-forward.
363git checkout --quiet main
364git checkout --quiet -b under-c2c
365echo "a fifth source file" > src/fifth.txt
366git add -A
367git commit --quiet -m "add a fifth source file"
368attest_change under-c2c "" "" --check code-build
369request under-c2c main "$(git rev-parse under-c2c)"
370integrate
371grep -q "^under-c2c -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
372
373request c2c main "$c2c"
374scrubbed integrator reconcile \
375 --repo "$origin" \
376 --key "$work/integrator" --name "$integrator_name" \
377 --known-signers "$work/known_signers" \
378 --instance-repo "$instance_repo" \
379 --schema "$SCHEMA_VERIFICATION" \
380 --attest-schema "$SCHEMA_ATTESTATION" \
381 --event-schema "$SCHEMA_EVENTS" \
382 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
383 2>&1 | tee "$work/last.out"
384grep -q "^c2c -> refs/heads/main .*: land$" "$work/last.out" \
385 || die "a landing with no git identity in the environment did not land: $(cat "$work/last.out")"
386
387landed_commit="$(git -C "$origin" rev-parse refs/heads/main)"
388[ "$landed_commit" != "$c2c" ] || die "the landing was a fast-forward; the scenario proves nothing"
389[ "$(git -C "$origin" log -1 --format='%an <%ae>' "$landed_commit")" = "a contributor <contributor@valley.invalid>" ] \
390 || die "the landed commit is not authored by the asker: $(git -C "$origin" log -1 --format='%an <%ae>' "$landed_commit")"
391[ "$(git -C "$origin" log -1 --format='%cn <%ce>' "$landed_commit")" = "$integrator_name <integrator@the-valley.invalid>" ] \
392 || die "the landed commit is not committed by the integrator: $(git -C "$origin" log -1 --format='%cn <%ce>' "$landed_commit")"
393# The commit id is a function of what the pass decided from and nothing
394# else, so a pass that dies before the ref write recomputes this same
395# commit rather than leaving another behind.
396[ "$(git -C "$origin" log -1 --format=%aI "$landed_commit")" = "$(git -C "$origin" log -1 --format=%cI "$landed_commit")" ] \
397 || die "the committer date is not the author's, so the landed commit id is not reproducible"
398holds "the integrator committed under its own name with nothing in the environment to take one from"
399
400# ----------------------------------------------------------------------
401say "3. a conflicting delta is stale, and demands full re-attestation"
402
403git checkout --quiet main
404git pull --quiet --ff-only origin main
405git checkout --quiet -b c3
406echo "the readme, as c3 would have it" > docs/readme.md
407git add -A
408git commit --quiet -m "rewrite the readme one way"
409attest_change c3 "" "" --check prose-format
410c3="$(git rev-parse c3)"
411
412git checkout --quiet main
413git checkout --quiet -b conflicting
414echo "the readme, as the other change would have it" > docs/readme.md
415git add -A
416git commit --quiet -m "rewrite the readme another way"
417attest_change conflicting "" "" --check prose-format
418request conflicting main "$(git rev-parse conflicting)"
419integrate
420grep -q "^conflicting -> refs/heads/main .*: land$" "$work/last.out" || die "the conflicting change did not land"
421
422request c3 main "$c3"
423integrate
424grep -q "^c3 -> refs/heads/main .*: stale$" "$work/last.out" || die "c3 was not stale"
425grep -q 'resolving it is new authorship' "$work/last.out" || die "the conflict was not reported as new authorship"
426grep -q 'CONFLICT (content): Merge conflict in docs/readme.md' "$work/last.out" \
427 || die "the conflict does not name what could not be merged"
428grep -q '"reason":"conflict"' "$work/last.out" || die "the event does not say conflict"
429grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "a conflict must re-demand every required check"
430holds "a conflict is stale, and every required check is re-demanded"
431forget c3
432
433# ----------------------------------------------------------------------
434say "4. an unknown signer's attestation does not transfer"
435
436git checkout --quiet main
437git pull --quiet --ff-only origin main
438git checkout --quiet -b c4
439echo "a readme signed by a stranger" > docs/readme.md
440git add -A
441git commit --quiet -m "a change a stranger attested"
442attest_change c4 "$work/stranger" "$stranger" --check prose-format
443request c4 main "$(git rev-parse c4)"
444integrate
445grep -q "^c4 -> refs/heads/main .*: stale$" "$work/last.out" || die "c4 was not stale"
446grep -q 'no signer the integrator holds' "$work/last.out" || die "the unknown signer was not reported"
447grep -q '"checks":\["prose-format"\]' "$work/last.out" || die "the event does not name prose-format"
448[ "$(tip)" = "$(git -C "$origin" rev-parse refs/heads/main)" ] || die "main moved"
449holds "evidence from a signer the integrator does not hold does not transfer"
450forget c4
451
452# ----------------------------------------------------------------------
453say "5. a tampered statement is rejected, not called stale"
454
455git checkout --quiet main
456git checkout --quiet -b c5
457echo "an honest readme" > docs/readme.md
458git add -A
459git commit --quiet -m "an honest change"
460attest_change c5 "" "" --check prose-format
461c5="$(git rev-parse c5)"
462c5digest="$(attest digest --repo "$repo" --rev c5 | cut -d: -f2)"
463
464# Edit one value in the signed text and store the result where the real
465# note was. Everything else about the note is untouched.
466c5ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
467 "refs/the-valley/attestations/$c5digest" | head -n1)"
468git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note" \
469 | sed 's/^predicate.result passed$/predicate.result failed/' > "$work/tampered.note"
470cmp -s "$work/tampered.note" <(git -C "$origin" cat-file blob "$c5ref:prose-format/statement.note") \
471 && die "the tamper changed nothing; the test proves nothing"
472blob="$(git -C "$origin" hash-object -w --stdin < "$work/tampered.note")"
473sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$origin" mktree)"
474top="$(printf '040000 tree %s\tprose-format\n' "$sub" | git -C "$origin" mktree)"
475git -C "$origin" update-ref "$c5ref" "$top"
476
477request c5 main "$c5"
478integrate
479grep -q "^c5 -> refs/heads/main .*: reject$" "$work/last.out" || die "a tampered statement was not rejected"
480grep -q 'does not verify as the statement it claims to be' "$work/last.out" || die "the refusal was not reported"
481grep -q 'request-stale' "$work/last.out" && die "a forgery was announced as staleness"
482holds "a tampered statement is a rejection, and no outcome event claims otherwise"
483forget c5
484
485# ----------------------------------------------------------------------
486say "6. an attestation about a different tree is rejected"
487
488git checkout --quiet main
489git checkout --quiet -b c6
490echo "the tree that was actually attested" > docs/readme.md
491git add -A
492git commit --quiet -m "the attested change"
493attest_change c6 "" "" --check prose-format
494c6ref="$(git -C "$origin" for-each-ref --format='%(refname)' \
495 "refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c6 | cut -d: -f2)" | head -n1)"
496
497# A second, different tree — with no attestation of its own — and the
498# first tree's valid, correctly signed note filed under its digest.
499git checkout --quiet -b c6-lifted
500echo "the tree that was not" > docs/readme.md
501git add -A
502git commit --quiet -m "the change the evidence was lifted onto"
503git push --quiet origin c6-lifted
504lifted="$(attest digest --repo "$repo" --rev c6-lifted | cut -d: -f2)"
505git -C "$origin" update-ref \
506 "refs/the-valley/attestations/$lifted/${c6ref##*/}" \
507 "$(git -C "$origin" rev-parse "$c6ref^{tree}")"
508
509request c6 main "$(git rev-parse c6-lifted)"
510integrate
511grep -q "^c6 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lifted attestation was not rejected"
512grep -q 'tree digest mismatch' "$work/last.out" || die "the refusal does not name the tree digest"
513holds "a signature cannot be lifted onto a tree it was not made about"
514forget c6
515
516# ----------------------------------------------------------------------
517say "7. an attestation lying about its kind is rejected"
518
519# The policy runs prose-format with nix, so its evidence is content-
520# addressed. This attestation claims a notarization instead, and its class
521# was untouched by the intervening landing — so under the rule it nominated
522# for itself it would sail onto a base it was never produced over.
523git checkout --quiet main
524git pull --quiet --ff-only origin main
525git checkout --quiet -b c7
526echo "a readme attested as a notarization" > docs/readme.md
527git add -A
528git commit --quiet -m "a change whose attestation lies about its kind"
529attest_change c7 "" "" --command prose-format=true
530c7="$(git rev-parse c7)"
531
532git checkout --quiet main
533git checkout --quiet -b under-c7
534echo "a fourth source file" > src/fourth.txt
535git add -A
536git commit --quiet -m "add a fourth source file"
537attest_change under-c7 "" "" --check code-build
538request under-c7 main "$(git rev-parse under-c7)"
539integrate
540grep -q "^under-c7 -> refs/heads/main .*: land$" "$work/last.out" || die "the intervening change did not land"
541
542request c7 main "$c7"
543integrate
544grep -q "^c7 -> refs/heads/main .*: reject$" "$work/last.out" || die "a lie about the kind was not rejected"
545grep -q 'requires a pure check and the attestation claims an effectful check' "$work/last.out" \
546 || die "the refusal does not say the policy decides the rule"
547holds "the transfer rule follows the policy's runner, never the attestation's own claim"
548forget c7
549
550# ----------------------------------------------------------------------
551say "8. evidence pushed after a no-evidence verdict re-judges and lands"
552
553git checkout --quiet main
554git pull --quiet --ff-only origin main
555git checkout --quiet -b c8
556echo "a readme with no evidence yet" > docs/readme.md
557git add -A
558git commit --quiet -m "a change submitted before its checks ran"
559git push --quiet origin c8
560request c8 main "$(git rev-parse c8)"
561integrate
562grep -q "^c8 -> refs/heads/main .*: stale$" "$work/last.out" || die "a change with no evidence was not stale"
563grep -q 'no attestation for this check accompanies the change' "$work/last.out" \
564 || die "the absent attestation was not reported"
565
566# Nothing about the request or the tip moves; only the evidence arrives.
567attest_change c8 "" "" --check prose-format
568integrate
569grep -q "^c8 -> refs/heads/main .*: land$" "$work/last.out" \
570 || die "the request was not re-judged after its evidence arrived"
571[ "$(tip)" = "$(git rev-parse c8)" ] || die "main is not at the change's head"
572holds "a pass re-judges when evidence arrives, with the request and the tip unmoved"
573
574# ----------------------------------------------------------------------
575say "9. a ref that is not a request does not hold up one that is"
576
577git checkout --quiet main
578git pull --quiet --ff-only origin main
579git checkout --quiet -b c9
580echo "a readme beside a garbage ref" > docs/readme.md
581git add -A
582git commit --quiet -m "a change beside a malformed request"
583attest_change c9 "" "" --check prose-format
584# A name with no target segment at all, and one with a segment too many.
585# Anyone with push access can write this namespace.
586git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/garbage"
587git push --quiet origin "$(git rev-parse c9):refs/the-valley/integration-requests/main/too/deep"
588request c9 main "$(git rev-parse c9)"
589integrate
590grep -q 'refs/the-valley/integration-requests/garbage: not a request ref' "$work/last.out" \
591 || die "the malformed ref was not reported"
592grep -q 'refs/the-valley/integration-requests/main/too/deep: not a request ref' "$work/last.out" \
593 || die "the over-deep ref was not reported"
594grep -q "^c9 -> refs/heads/main .*: land$" "$work/last.out" \
595 || die "a malformed ref stopped the pass reaching a valid request"
596holds "malformed refs are reported and skipped; the valid request beside them landed"
597git -C "$origin" update-ref -d refs/the-valley/integration-requests/garbage
598git -C "$origin" update-ref -d refs/the-valley/integration-requests/main/too/deep
599
600# ----------------------------------------------------------------------
601say "10. a pass leaves no scratch behind, and a leaked worktree is swept"
602
603# Nine scenarios of real passes, landings and rejections alike, and the
604# scratch root is empty: every worktree a pass takes is given back on the
605# way out, on the failure path as much as the success one.
606leftover="$(find "$TMPDIR" -maxdepth 1 -name 'valley-integrator-wt*')"
607[ -z "$leftover" ] || die "nine passes left scratch behind: $leftover"
608[ "$(git -C "$origin" worktree list --porcelain | grep -c '^worktree ')" -eq 1 ] \
609 || die "nine passes left a worktree attached to the repository"
610holds "no pass left a worktree or a scratch directory behind"
611
612# What a crash leaves: the checkout and its administrative entry, both
613# still valid, so nothing prunes them. Beside it, a worktree of the kind
614# an operator might attach by hand, which is not the integrator's to
615# remove.
616leak="$TMPDIR/valley-integrator-wt-crashed"
617mine="$work/an-operators-own-worktree"
618git -C "$origin" worktree add --quiet --detach "$leak" refs/heads/main
619git -C "$origin" worktree add --quiet --detach "$mine" refs/heads/main
620timeout 5 integrator watch --interval 1s \
621 --repo "$origin" \
622 --key "$work/integrator" --name "$integrator_name" \
623 --known-signers "$work/known_signers" \
624 --instance "$instance" \
625 --schema "$SCHEMA_VERIFICATION" \
626 --attest-schema "$SCHEMA_ATTESTATION" \
627 --event-schema "$SCHEMA_EVENTS" \
628 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
629 > "$work/watch.out" 2>&1 || true
630[ ! -e "$leak" ] || die "a leaked scratch worktree survived the sweep"
631if git -C "$origin" worktree list --porcelain | grep -q "^worktree $leak$"; then
632 die "the leaked worktree is gone from disk and still registered"
633fi
634[ -e "$mine" ] || die "the sweep removed a worktree the integrator did not make"
635git -C "$origin" worktree remove --force "$mine"
636holds "a crash-leaked scratch worktree is swept at watch start; another hand's is not"
637
638# ----------------------------------------------------------------------
639say "11. a scratch root it may not write is reported as that, and nothing else"
640
641# The deployment failure this guards is a sandbox failure: the root TMPDIR
642# names is not writable. That is a statement about the unit, not about the
643# change being judged, and it has to arrive as one — with the path and where
644# the path came from — rather than as git complaining about the leading
645# directories of a .git file.
646refused="$work/refused"
647mkdir -p "$refused"
648chmod a-w "$refused"
649git checkout --quiet main
650git pull --quiet --ff-only origin main
651git checkout --quiet -b c10
652echo "a change that never gets a worktree" > docs/readme.md
653git add -A
654git commit --quiet -m "a change the integrator cannot make scratch for"
655attest_change c10 "" "" --check prose-format
656request c10 main "$(git rev-parse c10)"
657export TMPDIR="$refused"
658integrate || true
659export TMPDIR="$work/tmp"
660chmod u+w "$refused"
661grep -q "scratch directory under $refused, which TMPDIR names" "$work/last.out" \
662 || die "an unwritable scratch root was not reported as one: $(cat "$work/last.out")"
663forget c10
664holds "an unwritable scratch root names the path and where it came from"
665
666# ----------------------------------------------------------------------
667say "12. a floor the instance repository does not carry names the directory looked in"
668
669# The deployment failure this guards is a controller pointed at the wrong
670# directory: the floor is somewhere, and not where this controller looked.
671# The report has to name the repository, the directory and the ref, because
672# those three are what the operator compares against the repository in front
673# of them. A floor is either not there at all, or there under a name that
674# holds no documents, and both arrive that way.
675#
676# A missing floor is refused rather than read as requiring nothing, which is
677# the direction the other layer's absence defaults in (dcr-f41f718). The
678# refusal says why in its own sentence, because the operator reading it is
679# being told that the deployment is not one that can land anything.
680git checkout --quiet main
681git pull --quiet --ff-only origin main
682git checkout --quiet -b c11
683echo "a change judged under no floor" > docs/readme.md
684git add -A
685git commit --quiet -m "a change whose floor the controller cannot find"
686attest_change c11 "" "" --check prose-format
687request c11 main "$(git rev-parse c11)"
688
689# The sentence every refusal to find a floor ends with.
690unwritten="a valley without a written floor is not a valley whose changes land"
691
692floorless() {
693 integrator reconcile \
694 --repo "$origin" \
695 --key "$work/integrator" --name "$integrator_name" \
696 --known-signers "$work/known_signers" "$@" \
697 --schema "$SCHEMA_VERIFICATION" \
698 --attest-schema "$SCHEMA_ATTESTATION" \
699 --event-schema "$SCHEMA_EVENTS" \
700 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
701 2>&1 | tee "$work/last.out"
702}
703
704# The project's own repository carries policy/project and no policy/instance,
705# so the default directory is not there.
706floorless --instance-repo "$origin" || true
707grep -qF "no instance policy layer: $origin has no policy/instance/ at refs/heads/main: $unwritten" "$work/last.out" \
708 || die "a missing floor did not name the directory looked in, and why that is refused: $(cat "$work/last.out")"
709
710# And the flat layout, which is a directory that exists and holds no
711# documents of its own: policy/ has the two layers under it and no *.cue in
712# it. Said as that, rather than as an empty composition further on.
713#
714# Under a second request name, because the refusal above is now this
715# controller's recorded answer to c11 and it stands until the request, the
716# tip or the evidence moves — which is scenario 14's whole subject. A
717# request is what an answer is remembered against, so a second one asks the
718# question again.
719request c11-flat main "$(git rev-parse c11)"
720floorless --instance-repo "$origin" --instance-policy policy || true
721grep -qF "no instance policy layer: $origin holds no *.cue in policy/ at refs/heads/main: $unwritten" "$work/last.out" \
722 || die "a floor directory holding no documents did not say so: $(cat "$work/last.out")"
723
724# And the materialized layer, which is how a host that serves no instance
725# repository is given the floor. A directory with nothing in it is the same
726# absence and is refused the same way.
727request c11-bare main "$(git rev-parse c11)"
728nofloor="$work/no-floor-here"
729mkdir -p "$nofloor"
730floorless --instance "$nofloor" || true
731grep -qF "no instance policy layer: $nofloor holds no *.cue: $unwritten" "$work/last.out" \
732 || die "an empty materialized floor was not refused: $(cat "$work/last.out")"
733forget c11
734forget c11-flat
735forget c11-bare
736holds "a floor that is not where the controller looked names the repository, the directory and the ref"
737
738# ----------------------------------------------------------------------
739say "13. a change the policy requires no check of lands, and says so"
740
741# Requiring nothing is an ordinary outcome, not an edge: this project's
742# classes cover docs/, src/ and deploy/, and its `unclassified` set is
743# empty, so a path outside all three is answered with nothing owed. The
744# landing still writes a transfer statement — a commit point that records
745# nothing is a landing nothing attests to — and that statement says the
746# required set was empty rather than carrying a list with nothing in it,
747# which has no written form at all.
748git checkout --quiet main
749git pull --quiet --ff-only origin main
750git checkout --quiet -b c12
751echo "outside every class this policy covers" > notes.txt
752git add -A
753git commit --quiet -m "a change no class covers"
754request c12 main "$(git rev-parse c12)"
755integrate
756grep -q '^c12 -> refs/heads/main .*: land$' "$work/last.out" \
757 || die "a change with nothing required did not land: $(cat "$work/last.out")"
758grep -q 'the policy requires no check of the paths this change touches' "$work/last.out" \
759 || die "the verdict did not say the policy required nothing"
760[ "$(tip)" = "$(git rev-parse c12)" ] || die "c12 did not land"
761git -C "$origin" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/c12" > /dev/null \
762 && die "the request ref survived the landing"
763
764nothing="refs/the-valley/attestations/$(attest digest --repo "$repo" --rev c12 | cut -d: -f2)/$ihash"
765git -C "$origin" cat-file blob "$nothing:transfer.note" > "$work/nothing.note"
766grep -qx 'predicate.required none' "$work/nothing.note" \
767 || die "the transfer statement does not say the required set was empty: $(cat "$work/nothing.note")"
768grep -q '^predicate.checks' "$work/nothing.note" \
769 && die "the transfer statement carries a check list beside the empty-set line"
770attest verify --note "$work/nothing.note" --known-keys "$work/reader_keys" \
771 --repo "$repo" --rev c12 --signer "$integrator_name" > /dev/null \
772 || die "the transfer statement for a landing with nothing required does not verify"
773holds "a landing with no required checks is signed, stored, and consumed its request"
774
775# ----------------------------------------------------------------------
776say "14. a request the controller cannot judge is reported once, not every tick"
777
778# The failure this guards is the one that turns a broken deployment into a
779# broken deployment nobody can read: a pass that cannot act on its verdict,
780# retried at tick rate, filling the journal with one refusal per interval
781# forever. The same request, tip and evidence produce the same failure, so
782# repeating it says nothing new. It is reported once and then the answer
783# stands, exactly as an outcome stands.
784git checkout --quiet main
785git pull --quiet --ff-only origin main
786git checkout --quiet -b c13
787echo "a change judged under no floor, repeatedly" > docs/readme.md
788git add -A
789git commit --quiet -m "a change whose floor the controller cannot find, under watch"
790attest_change c13 "" "" --check prose-format
791request c13 main "$(git rev-parse c13)"
792
793# Five seconds at a one-second tick: five passes over one request the
794# controller cannot judge.
795timeout 5 integrator watch --interval 1s \
796 --repo "$origin" \
797 --key "$work/integrator" --name "$integrator_name" \
798 --known-signers "$work/known_signers" \
799 --instance-repo "$origin" --instance-policy policy \
800 --schema "$SCHEMA_VERIFICATION" \
801 --attest-schema "$SCHEMA_ATTESTATION" \
802 --event-schema "$SCHEMA_EVENTS" \
803 ${VALLEY_E2E_NOW:+--now "$VALLEY_E2E_NOW"} \
804 > "$work/repeat.out" 2>&1 || true
805reports="$(grep -c 'no instance policy layer' "$work/repeat.out" || true)"
806[ "$reports" -eq 1 ] \
807 || die "the same refusal was reported $reports times over five ticks: $(cat "$work/repeat.out")"
808forget c13
809holds "a failure a pass cannot get past is reported once and then backs off"
810
811# ----------------------------------------------------------------------
812say "15. a target that carries no project layer of its own is judged under the floor alone"
813
814# The bootstrap this guards is the first policy commit: a project that has
815# not written a layer yet cannot land the commit that writes one, because a
816# change never supplies the policy that gates it. The project layer only
817# ever adds (dcr-f41f718), so its absence adds nothing, and what is left is
818# the floor — which still governs, and is observed governing here.
819
820# The floor grows a requirement of its own first, so that what remains after
821# the project layer goes away is something rather than nothing.
822cat > "$floor/policy/instance/floor.cue" <<'EOF'
823package verification
824
825floor: {
826 checks: "prose-format": {runner: "nix", attribute: "prose-format"}
827 classes: prose: {paths: "docs/**": true, requires: "prose-format": true}
828 unclassified: {}
829}
830EOF
831git -C "$floor" commit --quiet -am "the floor requires prose-format of docs/"
832git -C "$floor" push --quiet "$instance_repo" main
833
834# The project stops carrying a layer. Nothing classifies policy/project
835# itself, so this lands with nothing owed — which is how a project could
836# have arrived without one in the first place.
837git checkout --quiet main
838git pull --quiet --ff-only origin main
839git checkout --quiet -b c14
840git rm --quiet -r policy/project
841git commit --quiet -m "stop carrying a project policy layer"
842request c14 main "$(git rev-parse c14)"
843integrate
844grep -q "^c14 -> refs/heads/main .*: land$" "$work/last.out" \
845 || die "dropping the project layer did not land: $(cat "$work/last.out")"
846
847git checkout --quiet main
848git pull --quiet --ff-only origin main
849[ ! -e policy/project ] || die "the project layer is still at the tip; the scenario proves nothing"
850git checkout --quiet -b c15
851echo "a readme judged under the floor alone" > docs/readme.md
852git add -A
853git commit --quiet -m "a change to a project that declares no policy of its own"
854attest_change c15 "" "" --check prose-format
855request c15 main "$(git rev-parse c15)"
856integrate
857grep -q "^c15 -> refs/heads/main .*: land$" "$work/last.out" \
858 || die "a change to a project with no layer of its own did not land: $(cat "$work/last.out")"
859grep -qF "policy no project layer at policy/project; the floor alone" "$work/last.out" \
860 || die "the pass did not say it composed the floor alone"
861grep -q "check prose-format .*transferred" "$work/last.out" \
862 || die "the floor's own requirement did not govern a project with no layer"
863[ "$(tip)" = "$(git rev-parse c15)" ] || die "main is not at the change's head"
864holds "no project layer is no addition; the floor alone required prose-format and the change landed"
865
866# ----------------------------------------------------------------------
867say "16. a floor that requires nothing, written down, composes and judges"
868
869# Emptiness a floor states is not the same thing as a floor nobody wrote.
870# The document is there, somebody landed it, and what it composes to is a
871# policy that requires nothing — so a change under it lands owing nothing,
872# rather than being refused the way scenario 12's missing floor is.
873cat > "$floor/policy/instance/floor.cue" <<'EOF'
874package verification
875
876floor: {
877 checks: {}
878 classes: {}
879 unclassified: {}
880}
881EOF
882git -C "$floor" commit --quiet -am "the floor requires nothing, and says so"
883git -C "$floor" push --quiet "$instance_repo" main
884
885git checkout --quiet main
886git pull --quiet --ff-only origin main
887git checkout --quiet -b c16
888echo "a readme under a floor that requires nothing" > docs/readme.md
889git add -A
890git commit --quiet -m "a change under an empty floor and no project layer"
891request c16 main "$(git rev-parse c16)"
892integrate
893grep -q "^c16 -> refs/heads/main .*: land$" "$work/last.out" \
894 || die "a change under an explicitly empty floor did not land: $(cat "$work/last.out")"
895grep -q 'the policy requires no check of the paths this change touches' "$work/last.out" \
896 || die "the verdict did not say the policy required nothing"
897grep -qF "policy no project layer at policy/project; the floor alone" "$work/last.out" \
898 || die "the pass did not say it composed the floor alone"
899[ "$(tip)" = "$(git rev-parse c16)" ] || die "main is not at the change's head"
900holds "a floor that states it requires nothing composes, judges, and lands the change owing nothing"
901
902printf '\nintegrator-e2e: every scenario held\n'