gunk-dev/the-valley

valley-request.sh

source937924e2f63emain's published tip history
140.1 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export XDG_STATE_HOME="$TMPDIR/state"
6export GIT_CONFIG_NOSYSTEM=1
7# What this check writes down, kept out of the repository under test: an
8# answer file or a captured stream sitting in the working tree would be
9# committed by the next `git add -A` and change the tree being attested.
10w="$TMPDIR/scratch"
11mkdir -p "$w"
12git config --global user.name valley-check
13git config --global user.email valley-check@localhost
14git config --global init.defaultBranch main
15git config --global advice.detachedHead false
16
17# The operator's signing key. Provisioning one is deployment, so the verb
18# takes it as a parameter and this check supplies a throwaway.
19ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host"
20export VALLEY_ATTEST_KEY="$TMPDIR/host"
21export VALLEY_ATTEST_NAME=valley-host.example.org/attestations
22keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)"
23
24origin="$TMPDIR/project.git"
25git init --quiet --bare "$origin"
26
27# Every push the bare repo receives, as its hook sees it. The claim approve
28# makes is that ONE push carries the evidence and the request together, and
29# that claim is only observable from the receiving side.
30pushlog="$TMPDIR/pushlog"
31cat > "$origin/hooks/pre-receive" <<EOF
32#!/bin/sh
33echo '--- push' >> $pushlog
34cat >> $pushlog
35EOF
36chmod +x "$origin/hooks/pre-receive"
37
38# Interpose on origin's receive-pack so that the request namespace moves
39# between approve's read of it and the push it then makes. That window is
40# where every race below lives, and a hook cannot reach it: by the time
41# pre-receive runs the client has already been told what the refs were.
42# One shot, so only the push under test is raced.
43arm_race() {
44 cat > "$TMPDIR/racing-receive-pack" <<EOF
45#!/bin/sh
46if [ -e "$TMPDIR/race-armed" ]; then
47 rm -f "$TMPDIR/race-armed"
48 $1
49fi
50exec git-receive-pack "\$@"
51EOF
52 chmod +x "$TMPDIR/racing-receive-pack"
53 git config remote.origin.receivepack "$TMPDIR/racing-receive-pack"
54 touch "$TMPDIR/race-armed"
55}
56
57# Put it away, and fail if it never fired: a race nothing raced is a
58# scenario that proved nothing.
59disarm_race() {
60 git config --unset remote.origin.receivepack
61 if [ -e "$TMPDIR/race-armed" ]; then
62 echo "valley-request: the racing receive-pack never ran, so nothing was raced" >&2
63 exit 1
64 fi
65}
66
67# ----------------------------------------------------------------------
68# The owning valley's repository, carrying the floor at policy/instance.
69# The floor is read from here and never from the project, which is the
70# tree under judgment: what governs a change has to come from outside it.
71# Fetching is all the deriver does to this repository, which is what
72# anyone the valley governs necessarily holds.
73valley="$TMPDIR/valley.git"
74git init --quiet --bare "$valley"
75valleywork="$TMPDIR/valley-work"
76git init --quiet "$valleywork"
77mkdir -p "$valleywork/policy/instance"
78cat > "$valleywork/policy/instance/floor.cue" <<'EOF'
79package verification
80
81floor: {
82 checks: "tree-ok": {
83 runner: "command"
84 command: "test -f docs/readme.md"
85 }
86 classes: prose: {
87 paths: "docs/**": true
88 requires: "tree-ok": true
89 }
90 // A class that covers paths and asks nothing of them. Requiring no
91 // check is an ordinary outcome, not a hole: the policy has read these paths
92 // and has nothing to require of them.
93 classes: quiet: {
94 paths: "quiet/**": true
95 requires: {}
96 }
97 unclassified: "tree-ok": true
98}
99EOF
100git -C "$valleywork" add -A
101git -C "$valleywork" commit --quiet -m floor
102git -C "$valleywork" push --quiet "$valley" main
103
104# ----------------------------------------------------------------------
105# The project, laid out the way the deriver reads one without being told:
106# its own layer at policy/project, and its owning valley named at
107# policy/valley. Both checks use the command runner: the nix runner cannot
108# be exercised from inside a nix build, since the sandbox has no daemon to
109# call, and what is under test here is the sequencing rather than the
110# runner.
111#
112# examples/policy/ holds a second, contradictory pair of layers, and every
113# check they require is named `phantom`. Nothing may derive it: a gate that
114# came from a worked example is a gate nobody wrote, and the name is here
115# so that the failure would be visible rather than plausible.
116#
117# schema/ is here because the-valley's own tree carries it. The packaged
118# CLI never reads it; only the raw binary, run with nothing setting the
119# schema, reads it from the checkout (16).
120repo="$TMPDIR/project"
121git init --quiet "$repo"
122cd "$repo" || exit 1
123git remote add origin "$origin"
124
125mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project
126cp "$schemaFile" schema/verification.cue
127echo "the readme" > docs/readme.md
128echo "$valley" > policy/valley
129cat > policy/project/policy.cue <<'EOF'
130package verification
131
132project: {
133 checks: "no-secrets": {
134 runner: "command"
135 command: "! grep -rqF SECRET src"
136 }
137 classes: source: {
138 paths: "src/**": true
139 requires: "no-secrets": true
140 }
141}
142EOF
143cat > examples/policy/instance/floor.cue <<'EOF'
144package verification
145
146floor: {
147 checks: phantom: {
148 runner: "command"
149 command: "false"
150 }
151 classes: everything: {
152 paths: "**": true
153 requires: phantom: true
154 }
155 unclassified: phantom: true
156}
157EOF
158cat > examples/policy/project/policy.cue <<'EOF'
159package verification
160
161project: {}
162EOF
163git add -A
164git commit --quiet -m base
165git push --quiet -u origin main
166first="$(git rev-parse main)"
167
168# ----------------------------------------------------------------------
169# 1. A branch whose required check passes: the evidence and the request
170# reach origin in one push, and main does not move.
171git checkout --quiet -b topic/one
172echo "a better readme" > docs/readme.md
173git commit --quiet -am "improve the readme"
174git push --quiet origin topic/one
175digest="$(attest digest --rev topic/one | cut -d: -f2)"
176
177: > "$pushlog"
178printf 'a\n' > "$w/answers"
179valley review -v topic/one < "$w/answers" > "$w/approve.out" 2> "$w/approve.err"
180
181grep -q '^◐ topic/one checking 1 check: tree-ok$' "$w/approve.out"
182grep -qE '^check tree-ok +command passed$' "$w/approve.out"
183grep -qx '✓ topic/one approved · 1 check passed in [0-9]*s · landing on main' "$w/approve.out"
184grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/approve.out"
185grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/approve.out"
186grep -qx ' checks tree-ok' "$w/approve.out"
187
188# How long the check took is printed after it, and logged on this machine:
189# one JSON line per required check, in the state directory, outside the
190# repository and the evidence. A branch no record is on names no change.
191grep -qE '^time tree-ok +passed +[0-9][0-9.hms]* \(ran\)$' "$w/approve.out"
192timings="$XDG_STATE_HOME/valley/timings.jsonl"
193if [ "$(wc -l < "$timings")" != 1 ] || ! jq -e \
194 --arg head "$(git rev-parse topic/one)" --arg origin "$origin" '
195 keys == ["check", "cost", "duration_ms", "head", "repository", "result", "runner", "satisfied", "time"]
196 and .check == "tree-ok" and .runner == "command" and .cost == "quick"
197 and .result == "passed" and .satisfied == "ran"
198 and .head == $head and .repository == $origin
199 and (.duration_ms | type == "number" and . >= 0)
200 and (.time | test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3}Z$"))' \
201 "$timings" > /dev/null; then
202 echo "valley-request: approve did not log one timing for its one required check:" >&2
203 cat "$timings" >&2
204 exit 1
205fi
206if [ "$(stat -c %a "$timings")" != 600 ]; then
207 echo "valley-request: the timing log was created $(stat -c %a "$timings"), not 600" >&2
208 exit 1
209fi
210
211# What origin holds now. The attestation is keyed by the digest of the
212# BRANCH's tree, which is what says the checks ran over the right tree and
213# not over the operator's working copy.
214git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
215grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
216grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
217if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
218 != "$(git rev-parse topic/one)" ]; then
219 echo "valley-request: the request ref does not name the branch's head" >&2
220 exit 1
221fi
222if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
223 echo "valley-request: [a]pprove moved main" >&2
224 exit 1
225fi
226
227# One push carried both. This is the friction the verb exists to remove:
228# `attest run --push` publishes the branch and the evidence and knows
229# nothing about a request ref, so doing this by hand is two beats.
230pushes="$(grep -c '^--- push' "$pushlog" || true)"
231if [ "$pushes" != 1 ]; then
232 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
233 cat "$pushlog" >&2
234 exit 1
235fi
236grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
237grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
238
239# The note origin holds is the one the operator signed, over that tree.
240git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
241 > "$w/statement.note"
242attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
243attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
244 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
245grep -q 'matches the recorded subject digest' "$w/verify.out"
246
247# ----------------------------------------------------------------------
248# 2. The layers the derivation used are the ones that govern, and it says
249# which they were: the floor fetched from the owning valley, and the
250# project's own layer at policy/project in the TARGET's tip.
251# examples/policy/ is in this tree and requires `phantom` of every path,
252# and no default reaches it — it is derivable only by naming it, which
253# is what the options are for.
254valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err"
255grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out"
256grep -qx 'classes matched: prose' "$w/checks.out"
257grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out"
258if grep -q phantom "$w/checks.out" "$w/checks.err"; then
259 echo "valley-request: a default derived a check from examples/policy/" >&2
260 cat "$w/checks.out" "$w/checks.err" >&2
261 exit 1
262fi
263
264# Named, the example layers derive exactly what they declare. This is the
265# other half of the claim above: examples/policy/ is readable, and reading
266# it is something a person asks for.
267valley checks --instance examples/policy/instance --project examples/policy/project \
268 "$first" topic/one > "$w/example.out"
269grep -qE '^ phantom +mandatory' "$w/example.out"
270grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out"
271
272# ----------------------------------------------------------------------
273# 3. main moves on, so the next branch is behind main. It is offered the
274# ordinary prompt under one line that says so — the integrator applies
275# a delta to the tip rather than fast-forwarding, so nothing is asked of
276# the operator — and a branch whose check fails publishes nothing and
277# leaves the review loop usable. Approving the same head again would
278# only fail again, so after the failure the prompt says approving needs
279# a new head and offers [v]iew full diff, [r]eject and [s]kip alone, and
280# an answer of a is not taken. Review never offers [b]ase.
281git checkout --quiet main
282echo "a note" > docs/notes.md
283git add -A
284git commit --quiet -m "a landing that moves main"
285git push --quiet origin main
286
287git checkout --quiet -b topic/two "$first"
288printf 'SECRET\n' > src/leak.txt
289git add -A
290git commit --quiet -m "a leak"
291git push --quiet origin topic/two
292
293: > "$pushlog"
294printf 'a\na\ns\n' > "$w/answers"
295valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
296
297# The prompt is the whole of what the operator is offered, so it is pinned
298# character for character: a verb that stops being offered, or one that
299# starts, is a change to the review loop and not an incidental one.
300behind='topic/two · behind main (integrator merges)'
301grep -qx "$behind" "$w/fail.out"
302grep -qF '[a]pprove [v]iew diff [r]eject [s]kip: ' "$w/fail.out"
303if grep -qF '[b]ase' "$w/fail.out"; then
304 echo "valley-request: review offered [b]ase for a branch behind main" >&2
305 exit 1
306fi
307grep -q '◐ topic/two checking 1 check: no-secrets' "$w/fail.out"
308twohead="$(git rev-parse --short topic/two)"
309grep -qxF "topic/two · checks failed at $twohead" "$w/fail.out"
310grep -qF '[v]iew diff [r]eject [s]kip: ' "$w/fail.out"
311grep -qF 'answer v, r, or s' "$w/fail.out"
312offers="$(grep -oF '[a]pprove' "$w/fail.out" | wc -l)"
313checked="$(grep -c '◐ topic/two checking ' "$w/fail.out" || true)"
314if [ "$offers" != 1 ] || [ "$checked" != 1 ]; then
315 echo "valley-request: after its checks failed, the prompt offered [a]pprove again ($offers offers, $checked runs)" >&2
316 cat "$w/fail.out" >&2
317 exit 1
318fi
319grep -qx '⊘ topic/two not approved · 1 of [0-9]* checks* failed' "$w/fail.err"
320grep -qx ' ✗ no-secrets — see: valley status topic/two' "$w/fail.err"
321# The prompt is printed without a newline, so what follows a refusal shares
322# its line — the answer to it is not anchored.
323grep -q 'skipped topic/two; nothing done' "$w/fail.out"
324if [ -s "$pushlog" ]; then
325 echo "valley-request: a failing check pushed something" >&2
326 cat "$pushlog" >&2
327 exit 1
328fi
329git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
330if grep -q 'topic-two' "$w/after.txt"; then
331 echo "valley-request: a failing check left a ref on origin" >&2
332 cat "$w/after.txt" >&2
333 exit 1
334fi
335
336# The same for a branch that fast-forwards: after its checks fail, the
337# prompt offers [r]eject and [s]kip alone.
338git checkout --quiet -b topic/leak main
339mkdir -p src
340printf 'SECRET\n' > src/leak.txt
341git add -A
342git commit --quiet -m "another leak"
343git push --quiet origin topic/leak
344leakhead="$(git rev-parse --short topic/leak)"
345: > "$pushlog"
346printf 'a\na\ns\n' > "$w/answers"
347valley review topic/leak < "$w/answers" > "$w/leak.out" 2> "$w/leak.err"
348grep -qF '[a]pprove [v]iew diff [r]eject [s]kip: ' "$w/leak.out"
349grep -qxF "topic/leak · checks failed at $leakhead" "$w/leak.out"
350grep -qF '[v]iew diff [r]eject [s]kip: ' "$w/leak.out"
351grep -qF 'answer v, r, or s' "$w/leak.out"
352grep -q 'skipped topic/leak; nothing done' "$w/leak.out"
353offers="$(grep -oF '[a]pprove' "$w/leak.out" | wc -l)"
354checked="$(grep -c '◐ topic/leak checking ' "$w/leak.out" || true)"
355if [ "$offers" != 1 ] || [ "$checked" != 1 ] || [ -s "$pushlog" ]; then
356 echo "valley-request: after its checks failed, a branch that fast-forwards was offered [a]pprove again ($offers offers, $checked runs)" >&2
357 cat "$w/leak.out" "$pushlog" >&2
358 exit 1
359fi
360
361# ----------------------------------------------------------------------
362# 4. No signing key, with a check required: refused before any check is run,
363# and the prompt still comes back. The host signs, and there is no
364# unsigned mode.
365: > "$pushlog"
366printf 'a\ns\n' > "$w/answers"
367env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
368grep -qx '⊘ topic/two not approved · no signing key' "$w/nokey.err"
369grep -q '^ set VALLEY_ATTEST_KEY' "$w/nokey.err"
370grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
371# A refusal that is no check failure leaves the prompt as it was, [a]pprove
372# and all: a new session starts with no check failed.
373offers="$(grep -oF '[a]pprove [v]iew diff [r]eject [s]kip: ' "$w/nokey.out" | wc -l)"
374if [ "$offers" != 2 ]; then
375 echo "valley-request: a refusal that was no check failure changed the prompt" >&2
376 cat "$w/nokey.out" >&2
377 exit 1
378fi
379if [ -s "$pushlog" ]; then
380 echo "valley-request: a run with no signing key pushed something" >&2
381 exit 1
382fi
383
384# ----------------------------------------------------------------------
385# 5. A branch the policy requires no check of is filed as a request with
386# no evidence. The integrator lands one and records it as a landing
387# that required nothing, so refusing to file it here would strand
388# exactly the changes a policy leaves unrequired. Nothing is required,
389# so nothing is signed: this runs with no signing key at all.
390git checkout --quiet main
391git checkout --quiet -b topic/quiet
392mkdir -p quiet
393echo "a quiet note" > quiet/note.md
394git add -A
395git commit --quiet -m "a change under a class that asks nothing"
396git push --quiet origin topic/quiet
397quiethead="$(git rev-parse --short topic/quiet)"
398
399: > "$pushlog"
400printf 'a\n' > "$w/answers"
401env -u VALLEY_ATTEST_KEY valley review -v topic/quiet < "$w/answers" \
402 > "$w/quiet.out" 2> "$w/quiet.err"
403
404grep -q 'requires no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
405grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
406grep -qx '✓ topic/quiet approved · no checks required · landing on main' "$w/quiet.out"
407grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
408grep -qx ' evidence none — nothing was required, so nothing was signed' "$w/quiet.out"
409grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
410
411# One push, carrying the request and nothing else.
412pushes="$(grep -c '^--- push' "$pushlog" || true)"
413if [ "$pushes" != 1 ]; then
414 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
415 cat "$pushlog" >&2
416 exit 1
417fi
418if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
419 echo "valley-request: a request that requires nothing published evidence" >&2
420 cat "$pushlog" >&2
421 exit 1
422fi
423if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
424 != "$(git rev-parse topic/quiet)" ]; then
425 echo "valley-request: the bare request does not name the branch's head" >&2
426 exit 1
427fi
428
429# ----------------------------------------------------------------------
430# 6. A branch behind main is approved as it is. Review offers no [b]ase:
431# an answer of b is not taken, and approving files the branch's own
432# head, not a rebased one, while main and the branch stay where they
433# were.
434git checkout --quiet main
435git checkout --quiet -b topic/three "$first"
436echo "another readme" > docs/readme.md
437git commit --quiet -am "another readme"
438git push --quiet origin topic/three
439before_base="$(git -C "$origin" rev-parse refs/heads/main)"
440
441printf 'b\na\n' > "$w/answers"
442: > "$pushlog"
443valley review topic/three < "$w/answers" > "$w/base.out" 2>&1
444grep -qx 'topic/three · behind main (integrator merges)' "$w/base.out"
445grep -qF '[a]pprove [v]iew diff [r]eject [s]kip: answer a, v, r, or s' "$w/base.out"
446if grep -qF -e '[b]ase' -e 'rebased topic/three' "$w/base.out"; then
447 echo "valley-request: review offered or ran a rebase of a branch behind main" >&2
448 cat "$w/base.out" >&2
449 exit 1
450fi
451[ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-three)" = "$(git rev-parse topic/three)" ] \
452 || { echo "valley-request: approving a branch behind main did not file its own head" >&2; exit 1; }
453git merge-base --is-ancestor refs/remotes/origin/main topic/three \
454 && { echo "valley-request: the branch behind main was rebased" >&2; exit 1; }
455if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then
456 echo "valley-request: the review loop moved main" >&2
457 exit 1
458fi
459git checkout --quiet main
460
461# The request filed in 1 is still pending: nothing here consumes it, which
462# is the integrator's own step.
463git -C "$origin" rev-parse --verify --quiet \
464 refs/the-valley/integration-requests/main/topic-one > /dev/null
465
466# ----------------------------------------------------------------------
467# 7. A change does not supply the policy that gates it. This branch
468# deletes its own policy/project and repoints policy/valley at a
469# repository that does not exist. Both layers come from the target's
470# tip, so neither edit reaches the derivation: it composes the same two
471# layers it would for any other branch, and the fetch that a branch-read
472# pointer would have attempted never happens.
473git checkout --quiet -b topic/nolayer
474git rm --quiet -r policy/project
475echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
476git add -A
477git commit --quiet -m "delete the project layer and repoint the valley"
478git push --quiet origin topic/nolayer
479
480valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
481grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
482if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
483 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
484 cat "$w/nolayer.out" "$w/nolayer.err" >&2
485 exit 1
486fi
487
488: > "$pushlog"
489printf 'a\n' > "$w/answers"
490valley review topic/nolayer < "$w/answers" \
491 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
492grep -q '^✓ topic/nolayer approved · ' "$w/nolayer.review.out"
493
494# ----------------------------------------------------------------------
495# 8. The reviewer's checkout contributes nothing. This is the case that
496# failed in live use: the operator's checkout was an older branch
497# without the landed policy layer, and the deriver read the working
498# tree and refused. topic/nolayer is exactly such a checkout — no
499# policy/project in it, and a policy/valley naming nowhere — so every
500# derivation below runs from it.
501#
502# Byte-identical output is the claim, not merely a successful one:
503# review works the same from any branch, however stale.
504git checkout --quiet main
505valley checks "$first" topic/one > "$w/from-main.out"
506git checkout --quiet topic/nolayer
507valley checks "$first" topic/one > "$w/from-stale.out"
508if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
509 echo "valley-request: the checkout changed what the derivation said" >&2
510 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
511 exit 1
512fi
513
514# And [a]pprove, which is where it failed: a request filed from that same
515# checkout, over a branch the stale tree knows nothing about.
516git checkout --quiet -b topic/four origin/main
517echo "one more readme" > docs/readme.md
518git commit --quiet -am "one more readme"
519git push --quiet origin topic/four
520git checkout --quiet topic/nolayer
521
522: > "$pushlog"
523printf 'a\n' > "$w/answers"
524valley review -v topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
525grep -q '^◐ topic/four checking 1 check: tree-ok$' "$w/stale.out"
526grep -q '^✓ topic/four approved · ' "$w/stale.out"
527grep -qx ' checks tree-ok' "$w/stale.out"
528
529# ----------------------------------------------------------------------
530# 9. A target carrying no project layer at all is derived under the floor
531# alone, and says so. The project layer only ever adds, so the identity
532# of "adds" is "adds nothing"; refusing instead would make a repository
533# underivable until its first policy commit, and that commit could not
534# derive either. This is the integrator's own reading of an absent
535# layer (integrator/policy.go), and the two have to agree.
536bare2="$TMPDIR/floorless.git"
537git init --quiet --bare "$bare2"
538repo2="$TMPDIR/floorless"
539git init --quiet "$repo2"
540cd "$repo2" || exit 1
541git remote add origin "$bare2"
542mkdir -p docs policy
543echo "the readme" > docs/readme.md
544echo "$valley" > policy/valley
545git add -A
546git commit --quiet -m base
547git push --quiet -u origin main
548base2="$(git rev-parse main)"
549echo "a second readme" > docs/readme.md
550git commit --quiet -am "a second readme"
551
552valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
553grep -qx "policy: $valley policy/instance@refs/heads/main + no policy/project@origin/main $(git rev-parse --short origin/main) — the floor alone" "$w/floorless.out"
554grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
555if grep -q no-secrets "$w/floorless.out"; then
556 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
557 exit 1
558fi
559cd "$repo" || exit 1
560git checkout --quiet main
561
562# ----------------------------------------------------------------------
563# 9b. What the labels and refusals of valley checks say, at each edge of
564# reading the layers.
565#
566# A layer named beside one left unnamed: the named one is labelled as
567# given, relative to the top level when under it, and the unnamed
568# floor is still the owning valley's.
569toplevel="$(git rev-parse --show-toplevel)"
570valley checks --project "$toplevel/examples/policy/project" "$first" topic/one > "$w/named.out"
571grep -qx "policy: $valley policy/instance@refs/heads/main + examples/policy/project" "$w/named.out"
572grep -qE '^ tree-ok +mandatory +prose$' "$w/named.out"
573cp -r examples/policy/project "$TMPDIR/outside-project"
574valley checks --project "$TMPDIR/outside-project" "$first" topic/one > "$w/outside.out"
575grep -qx "policy: $valley policy/instance@refs/heads/main + $TMPDIR/outside-project" "$w/outside.out"
576
577# --json prints the valley-checks/v1 document of the same derivation, and
578# is no list of names, so --json --names is refused with the usage line.
579valley checks --json "$first" topic/one > "$w/checks.json"
580jq -e --arg base "$first" --arg head "$(git rev-parse topic/one)" '
581 .schema == "valley-checks/v1" and .base == $base and .head == $head
582 and ([.required[].name] == ["tree-ok"])' "$w/checks.json" > /dev/null
583if valley checks --json --names "$first" topic/one > "$w/jsonnames.out" 2> "$w/jsonnames.err" \
584 || [ -s "$w/jsonnames.out" ] || ! grep -q 'usage: valley checks ' "$w/jsonnames.err"; then
585 echo "valley-request: valley checks took --json with --names" >&2
586 cat "$w/jsonnames.out" "$w/jsonnames.err" >&2
587 exit 1
588fi
589
590# With no origin/main, the target is the local main, and the label names it.
591git clone --quiet "$origin" "$TMPDIR/local-main"
592cd "$TMPDIR/local-main" || exit 1
593git remote remove origin
594valley checks > "$w/local-main.out"
595grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@main $(git rev-parse --short main)" "$w/local-main.out"
596
597# A pointer naming a transport that runs a command is refused before
598# anything is fetched: no git fetch or ls-remote runs.
599echo "ext::sh -c touch% $TMPDIR/ext-ran" > policy/valley
600git commit --quiet -am "point at a command"
601if GIT_TRACE="$w/ext.trace" valley checks > "$w/ext.out" 2> "$w/ext.err" \
602 || [ -s "$w/ext.out" ] || ! grep -qF 'names the transport ext::' "$w/ext.err"; then
603 echo "valley-request: valley checks took a pointer naming ext::" >&2
604 cat "$w/ext.out" "$w/ext.err" >&2
605 exit 1
606fi
607grep -q 'git rev-parse' "$w/ext.trace"
608if grep -qE 'git (fetch|ls-remote)|git-remote-' "$w/ext.trace"; then
609 echo "valley-request: valley checks fetched from a pointer it refuses" >&2
610 exit 1
611fi
612
613# A project layer whose tree cannot be read is refused, and never read as
614# absent, which would derive under the floor alone. The clone's objects
615# are loose, so the layer's tree is one file to remove.
616git init --quiet "$TMPDIR/torn"
617cd "$TMPDIR/torn" || exit 1
618git -c fetch.unpackLimit=100000 fetch --quiet "$origin" main:refs/remotes/origin/main
619torn="$(git rev-parse origin/main:policy/project)"
620rm ".git/objects/${torn:0:2}/${torn:2}"
621if valley checks origin/main origin/main > "$w/torn.out" 2> "$w/torn.err" \
622 || [ -s "$w/torn.out" ] || ! grep -qF 'policy/project/' "$w/torn.err"; then
623 echo "valley-request: valley checks derived over a project layer it could not read" >&2
624 cat "$w/torn.out" "$w/torn.err" >&2
625 exit 1
626fi
627cd "$repo" || exit 1
628
629# ----------------------------------------------------------------------
630# 10. Approving again after main moved. This is the case that wedged in live
631# use. A request goes stale with reason=evidence when main lands
632# inside a required check's input closure: no attestation over the
633# requested tree can transfer again, so the cure is a new head rebased
634# onto the moved main. That head is not a descendant of the one the
635# request ref holds, which makes writing it a non-fast-forward
636# replacement — and a push without a lease is refused by git's own
637# rule before origin's hook is ever consulted. The verdict that sends
638# a change down this path is the integrator's and is pinned by
639# integrator-e2e; what is under test here is that [a]pprove can act on it.
640git checkout --quiet -b topic/five origin/main
641echo "a readme worth approving" > docs/readme.md
642git commit --quiet -am "a readme worth approving"
643git push --quiet origin topic/five
644stalehead="$(git rev-parse topic/five)"
645staledigest="$(attest digest --rev topic/five | cut -d: -f2)"
646
647printf 'a\n' > "$w/answers"
648valley review -v topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err"
649grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out"
650
651# main moves, and the change is rebased onto it — the operator's half of
652# approving again, which [a]pprove does not do and does not need to.
653git checkout --quiet main
654echo "a second note" > docs/second.md
655git add -A
656git commit --quiet -m "a landing under the same class"
657git push --quiet origin main
658git checkout --quiet topic/five
659git rebase --quiet main
660git push --quiet --force-with-lease origin topic/five
661newhead="$(git rev-parse topic/five)"
662newdigest="$(attest digest --rev topic/five | cut -d: -f2)"
663
664# The premise of the scenario, asserted rather than assumed: the rebase
665# produced a head off the old one's line and a tree the old evidence is
666# not about. Without both, the push below would be an ordinary one and
667# would prove nothing.
668if git merge-base --is-ancestor "$stalehead" "$newhead"; then
669 echo "valley-request: the rebased head still descends from the old one" >&2
670 exit 1
671fi
672if [ "$staledigest" = "$newdigest" ]; then
673 echo "valley-request: the rebase left the tree the old evidence is about" >&2
674 exit 1
675fi
676
677: > "$pushlog"
678printf 'a\n' > "$w/answers"
679valley review -v topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err"
680
681grep -q '^✓ topic/five approved · ' "$w/five-again.out"
682grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out"
683# Replacing a pending request is said out loud, and the sentence names the
684# head that was replaced.
685grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead") (a readme worth approving)" "$w/five-again.out"
686
687if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \
688 != "$newhead" ]; then
689 echo "valley-request: the request filed again does not name the rebased head" >&2
690 exit 1
691fi
692
693# Both attestations stand. The old one is about a tree that will never
694# land, but published evidence is never removed, and a record that can be
695# dropped is not one; the second approval publishes beside it, never over it.
696for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \
697 "refs/the-valley/attestations/$newdigest/$keyhash"; do
698 git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || {
699 echo "valley-request: $stands is not on origin after the second approval" >&2
700 exit 1
701 }
702done
703
704# One push again, and the request ref moved off the old head inside it.
705pushes="$(grep -c '^--- push' "$pushlog" || true)"
706if [ "$pushes" != 1 ]; then
707 echo "valley-request: the second approval took $pushes pushes, not 1" >&2
708 cat "$pushlog" >&2
709 exit 1
710fi
711grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog"
712# And every attestation ref in that push is a creation: an all-zero old id.
713# approve pushes a ref of its own notes alone, so an approval that updated
714# one would drop the notes it held, which the real hook refuses.
715if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \
716 | grep -qv '^0* '; then
717 echo "valley-request: the second approval updated an attestation ref" >&2
718 cat "$pushlog" >&2
719 exit 1
720fi
721if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then
722 echo "valley-request: the second approval moved main" >&2
723 exit 1
724fi
725
726# ----------------------------------------------------------------------
727# 11. Two approvals of one change race for one ref, and the loser
728# loses visibly. The request ref's value is read before the push and
729# the push is leased against exactly that value, so the second one to
730# arrive is refused rather than allowed to overwrite the first. The
731# race is made real: a receive-pack wrapper moves the ref on origin
732# after approve has read it and before the push it then makes, which is
733# what another operator getting there first looks like from here.
734git checkout --quiet main
735git checkout --quiet -b topic/six origin/main
736echo "a readme two approvals both rebased" > docs/readme.md
737git commit --quiet -am "a readme two approvals both rebased"
738git push --quiet origin topic/six
739sixfiled="$(git rev-parse topic/six)"
740
741printf 'a\n' > "$w/answers"
742valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err"
743grep -q '^✓ topic/six approved · ' "$w/six-first.out"
744
745git checkout --quiet main
746echo "a third note" > docs/third.md
747git add -A
748git commit --quiet -m "another landing under the same class"
749git push --quiet origin main
750git checkout --quiet topic/six
751git rebase --quiet main
752git push --quiet --force-with-lease origin topic/six
753sixhead="$(git rev-parse topic/six)"
754sixdigest="$(attest digest --rev topic/six | cut -d: -f2)"
755
756# The other approval's head, built where origin can already reach
757# every object in it: the same tree, parented on the same tip, so it is
758# the commit a second operator rebasing this change would have produced.
759racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \
760 -p "$(git rev-parse origin/main)" -m "the other approval")"
761
762arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-six '$racer'"
763
764: > "$pushlog"
765printf 'a\ns\n' > "$w/answers"
766valley review -v topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err"
767disarm_race
768
769# A refusal, named as one, and the review loop still usable after it.
770grep -q "^⊘ topic/six not approved · another approval filed $(git rev-parse --short "$racer") (.*) meanwhile$" "$w/race.err"
771grep -qx ' run: valley approve topic/six' "$w/race.err"
772grep -q 'skipped topic/six; nothing done' "$w/race.out"
773
774# Nothing published. The winner's request stands untouched, the loser's
775# evidence never reached origin, and origin saw no push at all — the lease
776# refuses before any command is sent, which is what --atomic is worth.
777if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \
778 != "$racer" ]; then
779 echo "valley-request: the lease did not hold; the other approval was clobbered" >&2
780 exit 1
781fi
782if git -C "$origin" rev-parse --verify --quiet \
783 "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then
784 echo "valley-request: an approval that was refused published its evidence" >&2
785 exit 1
786fi
787if [ -s "$pushlog" ]; then
788 echo "valley-request: an approval that was refused reached origin's hook" >&2
789 cat "$pushlog" >&2
790 exit 1
791fi
792
793# The checks that were run are not lost with the refusal: their notes are
794# in the operator's own repository, and the refusal says where.
795git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \
796 > /dev/null || {
797 echo "valley-request: the refused approval kept no evidence locally" >&2
798 exit 1
799}
800grep -qx 'valley: the evidence is stored locally at:' "$w/race.err"
801grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err"
802git checkout --quiet main
803
804# ----------------------------------------------------------------------
805# 12. Approving again at the same head. Evidence with a validity window
806# expires while the tree stays the right tree, and the answer to that
807# is a fresh observation over the same head. The request ref already
808# names that head, so it is left out of the refspec entirely rather
809# than pushed at the value origin already holds.
810#
811# The request is filed by hand here. Origin holding a request at this
812# head is the whole of the state under test, and re-running [a]pprove to
813# produce it would put a second-granular observation instant in the
814# note: the same tree re-attested inside one second is the identical
815# ref, and across a second boundary it is not.
816git checkout --quiet -b topic/seven origin/main
817echo "a readme approved twice" > docs/readme.md
818git commit --quiet -am "a readme approved twice"
819git push --quiet origin topic/seven
820sevenhead="$(git rev-parse topic/seven)"
821sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)"
822git push --quiet origin \
823 "$sevenhead:refs/the-valley/integration-requests/main/topic-seven"
824
825: > "$pushlog"
826printf 'a\n' > "$w/answers"
827valley review -v topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err"
828
829grep -q '^✓ topic/seven approved · ' "$w/seven.out"
830grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out"
831# The evidence went; the request ref was not in the refspec at all.
832grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog"
833if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then
834 echo "valley-request: a same-head re-approval pushed the request ref again" >&2
835 cat "$pushlog" >&2
836 exit 1
837fi
838if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \
839 != "$sevenhead" ]; then
840 echo "valley-request: a same-head re-approval disturbed the request ref" >&2
841 exit 1
842fi
843
844# The same head with nothing required: no evidence to publish and no ref to
845# write, so no push is made at all. topic/quiet's request has stood on
846# origin at this head since 5. A push assembled with an empty refspec
847# would fall back to origin's default one, and pushing whatever that
848# happens to name is not what approving a change means.
849git checkout --quiet main
850: > "$pushlog"
851printf 'a\n' > "$w/answers"
852env -u VALLEY_ATTEST_KEY valley review -v topic/quiet < "$w/answers" \
853 > "$w/quiet-again.out" 2> "$w/quiet-again.err"
854grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet-again.out"
855grep -qx ' the request already stood at this head and nothing was required, so nothing needed publishing' "$w/quiet-again.out"
856if [ -s "$pushlog" ]; then
857 echo "valley-request: a re-approval with nothing to publish still pushed" >&2
858 cat "$pushlog" >&2
859 exit 1
860fi
861
862# ----------------------------------------------------------------------
863# 13. Two operators filing one change at once. Reading that the request
864# ref is absent is a value like any other, and the lease covers it:
865# git spells that expectation with an empty value and refuses if the
866# ref exists at all. Unleased, this push would have fast-forwarded the
867# other filing away without saying a word.
868git checkout --quiet -b topic/eight origin/main
869echo "a readme two operators both filed" > docs/readme.md
870git commit --quiet -am "a readme two operators both filed"
871git push --quiet origin topic/eight
872eighthead="$(git rev-parse topic/eight)"
873eightdigest="$(attest digest --rev topic/eight | cut -d: -f2)"
874# The other operator's filing: this same tree on this same tip, which is
875# what a second checkout of one branch produces. It is a descendant of
876# nothing here, so an unleased push over it would be a plain fast-forward.
877firstfiler="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/eight^{tree}')" \
878 -p "$(git rev-parse origin/main)" -m "the other operator's filing")"
879
880arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-eight '$firstfiler'"
881: > "$pushlog"
882printf 'a\ns\n' > "$w/answers"
883valley review topic/eight < "$w/answers" > "$w/create-race.out" 2> "$w/create-race.err"
884disarm_race
885
886# The other operator's commit is on origin and not here, so the subject
887# says so rather than inventing one. Scenario 10 pins the case where the
888# replaced head IS local and its subject is printed.
889grep -q "not approved · another approval filed $(git rev-parse --short "$firstfiler") (subject not in this repository) meanwhile" "$w/create-race.err"
890grep -q 'skipped topic/eight; nothing done' "$w/create-race.out"
891if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-eight)" \
892 != "$firstfiler" ]; then
893 echo "valley-request: an unleased filing clobbered the request that got there first" >&2
894 exit 1
895fi
896if git -C "$origin" rev-parse --verify --quiet \
897 "refs/the-valley/attestations/$eightdigest/$keyhash" > /dev/null; then
898 echo "valley-request: a refused filing published its evidence" >&2
899 exit 1
900fi
901if [ -s "$pushlog" ]; then
902 echo "valley-request: a refused filing reached origin's hook" >&2
903 cat "$pushlog" >&2
904 exit 1
905fi
906
907# ----------------------------------------------------------------------
908# 14. A lease that refuses because the ref is gone, not because it moved.
909# The integrator consumes the request ref when the change lands, so an
910# approval prepared just before that lands into a lease against a
911# ref that no longer exists. Git reports it as stale info either way,
912# and only reading the ref again tells the two apart — so the two are
913# told apart, and the operator is pointed at the landing rather than
914# at a race that did not happen.
915git checkout --quiet main
916git checkout --quiet -b topic/nine origin/main
917echo "a readme that lands mid-approval" > docs/readme.md
918git commit --quiet -am "a readme that lands mid-approval"
919git push --quiet origin topic/nine
920
921printf 'a\n' > "$w/answers"
922valley review topic/nine < "$w/answers" > "$w/nine-first.out" 2> "$w/nine-first.err"
923grep -q '^✓ topic/nine approved · ' "$w/nine-first.out"
924
925git checkout --quiet main
926echo "a fourth note" > docs/fourth.md
927git add -A
928git commit --quiet -m "a landing that moves main again"
929git push --quiet origin main
930git checkout --quiet topic/nine
931git rebase --quiet main
932git push --quiet --force-with-lease origin topic/nine
933ninedigest="$(attest digest --rev topic/nine | cut -d: -f2)"
934
935arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-nine"
936: > "$pushlog"
937printf 'a\ns\n' > "$w/answers"
938valley review topic/nine < "$w/answers" > "$w/consumed.out" 2> "$w/consumed.err"
939disarm_race
940
941grep -qx '⊘ topic/nine not approved · the pending request was consumed meanwhile' "$w/consumed.err"
942grep -qx ' see: valley status topic/nine' "$w/consumed.err"
943# Not reported as a race, which is the distinction the re-read buys.
944if grep -q 'another approval filed' "$w/consumed.err"; then
945 echo "valley-request: a consumed request was reported as a lost race" >&2
946 cat "$w/consumed.err" >&2
947 exit 1
948fi
949grep -q 'skipped topic/nine; nothing done' "$w/consumed.out"
950if git -C "$origin" rev-parse --verify --quiet \
951 refs/the-valley/integration-requests/main/topic-nine > /dev/null; then
952 echo "valley-request: an approval that was refused re-created the consumed request" >&2
953 exit 1
954fi
955if git -C "$origin" rev-parse --verify --quiet \
956 "refs/the-valley/attestations/$ninedigest/$keyhash" > /dev/null; then
957 echo "valley-request: an approval that was refused published its evidence" >&2
958 exit 1
959fi
960if [ -s "$pushlog" ]; then
961 echo "valley-request: an approval that was refused reached origin's hook" >&2
962 cat "$pushlog" >&2
963 exit 1
964fi
965
966# ----------------------------------------------------------------------
967# 15. The same-head push carries no request ref, so it carries no
968# compare-and-swap on one either — git leaves an up-to-date ref out of
969# what it sends. Where the ref stands after that push is therefore not
970# something the run established, and it is read again rather than
971# asserted from the read before it.
972#
973# Nothing re-files it. A request for a change already in the stream is
974# judged against an empty delta on every pass, which is why the
975# integrator consumes the ref in the first place; re-creating it here
976# would be a guess about which of consumption and deletion happened,
977# paid for with a second push behind the one this verb promises. So
978# the run says what is true and stops, and approving again is the repair.
979git checkout --quiet main
980git checkout --quiet -b topic/ten origin/main
981echo "a readme consumed under the evidence" > docs/readme.md
982git commit --quiet -am "a readme consumed under the evidence"
983git push --quiet origin topic/ten
984tenhead="$(git rev-parse topic/ten)"
985tendigest="$(attest digest --rev topic/ten | cut -d: -f2)"
986git push --quiet origin \
987 "$tenhead:refs/the-valley/integration-requests/main/topic-ten"
988
989arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-ten"
990: > "$pushlog"
991printf 'a\ns\n' > "$w/answers"
992valley review -v topic/ten < "$w/answers" > "$w/posthoc.out" 2> "$w/posthoc.err"
993disarm_race
994
995grep -qx '? topic/ten approved · request consumed meanwhile · see: valley status topic/ten' "$w/posthoc.out"
996# The claim the old code would have made, from a read that had gone out of
997# date under it.
998if grep -q 'the request already stood at this head' "$w/posthoc.out"; then
999 echo "valley-request: a consumed request was reported as still standing" >&2
1000 cat "$w/posthoc.out" >&2
1001 exit 1
1002fi
1003grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out"
1004
1005# The evidence went, because that push succeeded; the request did not come
1006# back, because nothing here writes it.
1007git -C "$origin" rev-parse --verify --quiet \
1008 "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || {
1009 echo "valley-request: the evidence push was reported as done and is not on origin" >&2
1010 exit 1
1011}
1012if git -C "$origin" rev-parse --verify --quiet \
1013 refs/the-valley/integration-requests/main/topic-ten > /dev/null; then
1014 echo "valley-request: the consumed request was re-filed behind the operator" >&2
1015 exit 1
1016fi
1017# And that evidence went in one push, which is still the whole claim.
1018pushes="$(grep -c '^--- push' "$pushlog" || true)"
1019if [ "$pushes" != 1 ]; then
1020 echo "valley-request: the same-head re-approval took $pushes pushes, not 1" >&2
1021 cat "$pushlog" >&2
1022 exit 1
1023fi
1024git checkout --quiet main
1025
1026# ----------------------------------------------------------------------
1027# 16. A project other than the-valley carries no schema/. The schema ships
1028# with the tool, so the packaged CLI derives what a change requires
1029# and files the request from a checkout that has none. This is the case
1030# that failed in live use: [a]pprove in a valley's config repository, which
1031# carries its own floor at policy/instance, refused with "no policy
1032# schema" and published nothing.
1033cfgorigin="$TMPDIR/config.git"
1034git init --quiet --bare "$cfgorigin"
1035cfg="$TMPDIR/config"
1036git init --quiet "$cfg"
1037cd "$cfg" || exit 1
1038git remote add origin "$cfgorigin"
1039mkdir -p docs policy/instance
1040cp "$valleywork/policy/instance/floor.cue" policy/instance/floor.cue
1041echo "the readme" > docs/readme.md
1042git add -A
1043git commit --quiet -m base
1044git push --quiet -u origin main
1045git checkout --quiet -b topic/config
1046echo "a config readme" > docs/readme.md
1047git commit --quiet -am "a config readme"
1048git push --quiet origin topic/config
1049cfgdigest="$(attest digest --rev topic/config | cut -d: -f2)"
1050
1051printf 'a\n' > "$w/answers"
1052valley review -v topic/config < "$w/answers" > "$w/config.out" 2> "$w/config.err"
1053if grep -q 'no policy schema' "$w/config.err"; then
1054 echo "valley-request: [a]pprove looked for the schema in a project's checkout" >&2
1055 cat "$w/config.err" >&2
1056 exit 1
1057fi
1058grep -q '^◐ topic/config checking 1 check: tree-ok$' "$w/config.out"
1059grep -q '^✓ topic/config approved · ' "$w/config.out"
1060git -C "$cfgorigin" rev-parse --verify --quiet \
1061 "refs/the-valley/attestations/$cfgdigest/$keyhash" > /dev/null || {
1062 echo "valley-request: [a]pprove in a project without schema/ published no evidence" >&2
1063 exit 1
1064}
1065if [ "$(git -C "$cfgorigin" rev-parse refs/the-valley/integration-requests/main/topic-config)" \
1066 != "$(git rev-parse topic/config)" ]; then
1067 echo "valley-request: [a]pprove in a project without schema/ filed no request for its head" >&2
1068 exit 1
1069fi
1070
1071# checks without --schema reads the packaged schema too, and derives what
1072# [a]pprove ran from this repository's own floor.
1073valley checks > "$w/config-checks.out" 2> "$w/config-checks.err"
1074grep -qF '(this repository is its own valley)' "$w/config-checks.out"
1075grep -qE '^ tree-ok +mandatory +prose$' "$w/config-checks.out"
1076
1077# The packaged schema is a default and not a pin. A schema named in the
1078# environment is the one read, and --schema overrides both.
1079if VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks \
1080 > "$w/envschema.out" 2> "$w/envschema.err"; then
1081 echo "valley-request: VALLEY_VERIFICATION_SCHEMA did not replace the packaged schema" >&2
1082 exit 1
1083fi
1084grep -qF "no policy schema at $TMPDIR/no-such-schema.cue" "$w/envschema.err"
1085VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks --schema "$schemaFile" \
1086 > "$w/flagschema.out" 2> "$w/flagschema.err"
1087grep -qE '^ tree-ok +mandatory +prose$' "$w/flagschema.out"
1088
1089# The raw binary run in a checkout, with nothing setting the
1090# variable, reads schema/verification.cue in that checkout. the-valley's own
1091# tree carries one, as the first scratch project does, and every other
1092# project's tree does not.
1093if env -u VALLEY_VERIFICATION_SCHEMA "$valleyUnwrapped" checks \
1094 > "$w/bare-config.out" 2> "$w/bare-config.err"; then
1095 echo "valley-request: the raw binary found a schema in a checkout without one" >&2
1096 exit 1
1097fi
1098grep -qF "no policy schema at $(git rev-parse --show-toplevel)/schema/verification.cue" \
1099 "$w/bare-config.err"
1100cd "$repo" || exit 1
1101env -u VALLEY_VERIFICATION_SCHEMA "$valleyUnwrapped" checks "$first" topic/one \
1102 > "$w/bare.out" 2> "$w/bare.err"
1103grep -qE '^ tree-ok +mandatory +prose$' "$w/bare.out"
1104
1105# Every git call the default derivation makes fails closed: finding the
1106# target, reading both layers out of its tip, reading the pointer,
1107# fetching the owning valley, and the diff. fail-nth fails them one at a
1108# time, and each run must end non-zero, say so, and print nothing on
1109# stdout. The layer reads matter most here: a listing of policy/project
1110# that failed, read as an empty directory, would derive under the floor
1111# alone, and a read of origin/main that failed, read as no such ref,
1112# would derive from the local main.
1113shims="$TMPDIR/shims"
1114mkdir -p "$shims"
1115ln -s "$failNth" "$shims/git"
1116shimmed_checks() {
1117 echo 0 > "$TMPDIR/shim-count"
1118 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
1119 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
1120 PATH="$shims:$PATH" "$valleyUnwrapped" checks "$first" topic/one
1121}
1122shimmed_checks 0 > "$w/shim-want.out"
1123grep -qE '^ tree-ok +mandatory +prose$' "$w/shim-want.out"
1124calls="$(cat "$TMPDIR/shim-count")"
1125[ "$calls" -gt 0 ] || {
1126 echo "valley-request: the derivation never called git, so failing it proves nothing" >&2
1127 exit 1
1128}
1129for ((at = 1; at <= calls; at++)); do
1130 if shimmed_checks "$at" > "$w/shim.out" 2> "$w/shim.err" || [ -s "$w/shim.out" ] \
1131 || ! grep -q '^valley: ' "$w/shim.err"; then
1132 echo "valley-request: git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
1133 cat "$w/shim.out" "$w/shim.err" >&2
1134 exit 1
1135 fi
1136done
1137
1138# The same, for the derivation that resolves its own base: `merge-base` is
1139# a call the explicit-base runs above never make, and --names is a second
1140# way of printing what the derivation found. A run is shimmed for each
1141# call, with the arguments the caller gives.
1142defaulted_checks() {
1143 echo 0 > "$TMPDIR/shim-count"
1144 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
1145 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
1146 PATH="$shims:$PATH" "$valleyUnwrapped" checks "${@:2}"
1147}
1148git checkout --quiet topic/one
1149for mode in report names; do
1150 margs=()
1151 [ "$mode" != names ] || margs=(--names)
1152 defaulted_checks 0 "${margs[@]}" > "$w/dshim-want.out"
1153 [ -s "$w/dshim-want.out" ] || {
1154 echo "valley-request: default resolution ($mode) printed nothing" >&2
1155 exit 1
1156 }
1157 calls="$(cat "$TMPDIR/shim-count")"
1158 sawmb=0
1159 for ((at = 1; at <= calls; at++)); do
1160 if defaulted_checks "$at" "${margs[@]}" > "$w/dshim.out" 2> "$w/dshim.err" \
1161 || [ -s "$w/dshim.out" ] || ! grep -q '^valley: ' "$w/dshim.err"; then
1162 echo "valley-request: default resolution ($mode): git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
1163 cat "$w/dshim.out" "$w/dshim.err" >&2
1164 exit 1
1165 fi
1166 done
1167done
1168git checkout --quiet -
1169
1170# A write that fails is a failure of the derivation, not a shorter list:
1171# with no file allowed to grow, every intermediate file the derivation
1172# writes fails, and nothing may print. The pipe keeps the refusal readable,
1173# since a file stream would be cut off too.
1174for mode in report names; do
1175 margs=()
1176 [ "$mode" != names ] || margs=(--names)
1177 if wrote="$(
1178 ulimit -f 0
1179 trap '' XFSZ
1180 env -u VALLEY_VERIFICATION_SCHEMA "$valleyUnwrapped" checks "${margs[@]}" "$first" topic/one 2> /dev/null
1181 )" || [ -n "$wrote" ]; then
1182 echo "valley-request: a derivation with unwritable scratch files ($mode) did not fail cleanly" >&2
1183 printf '%s\n' "$wrote" >&2
1184 exit 1
1185 fi
1186done
1187
1188# ----------------------------------------------------------------------
1189# 17. The approve verb: [a]pprove without the pager, for a front end that
1190# has shown the change its own way. It runs the function the key
1191# runs, so it publishes what the key's session in 1 published, the
1192# evidence and the request in one push, and prints what that session
1193# printed. stdin is closed, because no answer is read, and the change
1194# is named the way status accepts it, with origin/ in front.
1195git checkout --quiet -b topic/verb origin/main
1196echo "a readme approved without the pager" > docs/readme.md
1197git commit --quiet -am "a readme approved without the pager"
1198git push --quiet origin topic/verb
1199verbhead="$(git rev-parse topic/verb)"
1200verbdigest="$(attest digest --rev topic/verb | cut -d: -f2)"
1201git checkout --quiet main
1202
1203: > "$pushlog"
1204valley approve -v origin/topic/verb < /dev/null > "$w/verb.out" 2> "$w/verb.err"
1205grep -q '◐ topic/verb checking 1 check: tree-ok' "$w/verb.out"
1206grep -qx '✓ topic/verb approved · 1 check passed in [0-9]*s · landing on main' "$w/verb.out"
1207grep -qx " request refs/the-valley/integration-requests/main/topic-verb -> $(git rev-parse --short "$verbhead")" "$w/verb.out"
1208grep -qx " evidence refs/the-valley/attestations/$verbdigest/$keyhash" "$w/verb.out"
1209grep -qx ' checks tree-ok' "$w/verb.out"
1210if grep -q '\[[a-z]\]' "$w/verb.out"; then
1211 echo "valley-request: the approve verb offered a prompt" >&2
1212 cat "$w/verb.out" >&2
1213 exit 1
1214fi
1215if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-verb)" \
1216 != "$verbhead" ]; then
1217 echo "valley-request: the approve verb's request does not name the branch's head" >&2
1218 exit 1
1219fi
1220git -C "$origin" rev-parse --verify --quiet \
1221 "refs/the-valley/attestations/$verbdigest/$keyhash" > /dev/null || {
1222 echo "valley-request: the approve verb published no evidence" >&2
1223 exit 1
1224}
1225pushes="$(grep -c '^--- push' "$pushlog" || true)"
1226if [ "$pushes" != 1 ]; then
1227 echo "valley-request: the approve verb took $pushes pushes, not 1" >&2
1228 cat "$pushlog" >&2
1229 exit 1
1230fi
1231
1232# A refusal is the verb's exit status, after the same words the key's
1233# refusal prints, and it publishes nothing.
1234: > "$pushlog"
1235if valley approve -v topic/two < /dev/null > "$w/verb-refused.out" 2> "$w/verb-refused.err"; then
1236 echo "valley-request: the approve verb exited 0 for a change whose check failed" >&2
1237 exit 1
1238fi
1239grep -q '^⊘ topic/two not approved · 1 of [0-9]* checks* failed$' "$w/verb-refused.err"
1240if [ -s "$pushlog" ]; then
1241 echo "valley-request: a refused approve verb pushed something" >&2
1242 cat "$pushlog" >&2
1243 exit 1
1244fi
1245if valley approve -v topic/no-such-change < /dev/null > "$w/verb-missing.out" 2> "$w/verb-missing.err"; then
1246 echo "valley-request: the approve verb exited 0 for a change origin does not have" >&2
1247 exit 1
1248fi
1249grep -q 'no such branch on origin: topic/no-such-change' "$w/verb-missing.err"
1250
1251# ----------------------------------------------------------------------
1252# 17b. Timing never stands in the way of approval. A FIFO nobody reads,
1253# sitting at the timing log's path, would hold an open for writing
1254# forever: the log is refused with a warning, and approval goes on.
1255# With standard error a pipe nobody reads as well, the warning is
1256# dropped rather than ending the run. Both runs keep a state
1257# directory of their own, so the log the other cases read is not
1258# touched.
1259fifostate="$w/fifo-state"
1260mkdir -p "$fifostate/valley"
1261mkfifo "$fifostate/valley/timings.jsonl"
1262approved_past_fifo() {
1263 if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/topic-$1")" \
1264 != "$(git rev-parse "topic/$1")" ]; then
1265 echo "valley-request: approving past a FIFO at the timing log filed no request ($2)" >&2
1266 exit 1
1267 fi
1268 git -C "$origin" rev-parse --verify --quiet \
1269 "refs/the-valley/attestations/$(attest digest --rev "topic/$1" | cut -d: -f2)/$keyhash" > /dev/null || {
1270 echo "valley-request: approving past a FIFO at the timing log published no evidence ($2)" >&2
1271 exit 1
1272 }
1273}
1274for branch in fifo unread; do
1275 git checkout --quiet -b "topic/$branch" origin/main
1276 echo "a readme approved past a FIFO, $branch" > docs/readme.md
1277 git commit --quiet -am "a readme approved past a FIFO, $branch"
1278 git push --quiet origin "topic/$branch"
1279 git checkout --quiet main
1280done
1281
1282XDG_STATE_HOME="$fifostate" timeout 120 valley approve -v origin/topic/fifo \
1283 < /dev/null > "$w/fifo.out" 2> "$w/fifo.err" || {
1284 echo "valley-request: approve did not finish past a FIFO at the timing log (exit $?):" >&2
1285 cat "$w/fifo.out" "$w/fifo.err" >&2
1286 exit 1
1287}
1288grep -qE '^time tree-ok +passed +[0-9][0-9.hms]* \(ran\)$' "$w/fifo.out"
1289grep -qx "attest: warning: timings not logged: $fifostate/valley/timings.jsonl is not a regular file" "$w/fifo.err"
1290approved_past_fifo fifo "standard error read"
1291
1292# A pipe with no reader. The FIFO is opened to read and write, so opening it
1293# to write does not wait; closing the first leaves the writer alone.
1294mkfifo "$w/unread"
1295exec 3<> "$w/unread"
1296exec 4> "$w/unread"
1297exec 3<&-
1298status=0
1299XDG_STATE_HOME="$fifostate" timeout 120 valley approve -v origin/topic/unread \
1300 < /dev/null > "$w/unread.out" 2>&4 || status=$?
1301exec 4>&-
1302if [ "$status" != 0 ]; then
1303 echo "valley-request: approve with nobody reading standard error exited $status:" >&2
1304 cat "$w/unread.out" >&2
1305 exit 1
1306fi
1307approved_past_fifo unread "standard error unread"
1308
1309# ----------------------------------------------------------------------
1310# 18. The rebase verb: [b]ase without the pager. A stale branch is
1311# rebased onto main in a throwaway worktree and pushed under a lease,
1312# and the new head is printed. Nothing is approved and nothing is
1313# re-reviewed: rebasing is mechanics, and the judgment on what it
1314# produced stays with whoever reads it next.
1315git checkout --quiet -b topic/behind "$first"
1316echo "a readme left behind" > docs/behind.md
1317git add -A
1318git commit --quiet -m "a readme left behind"
1319git push --quiet origin topic/behind
1320behindhead="$(git rev-parse topic/behind)"
1321git checkout --quiet main
1322if git -C "$origin" merge-base --is-ancestor refs/heads/main refs/heads/topic/behind; then
1323 echo "valley-request: the branch to rebase fast-forwarded from main already" >&2
1324 exit 1
1325fi
1326before_rebase="$(git -C "$origin" rev-parse refs/heads/main)"
1327
1328: > "$pushlog"
1329valley rebase topic/behind < /dev/null > "$w/rebase.out" 2> "$w/rebase.err"
1330rebased="$(git -C "$origin" rev-parse refs/heads/topic/behind)"
1331grep -qx 'rebased topic/behind onto origin/main, pushed with --force-with-lease' "$w/rebase.out"
1332grep -qx " head $rebased" "$w/rebase.out"
1333if [ "$rebased" = "$behindhead" ]; then
1334 echo "valley-request: the rebase verb left the branch where it was" >&2
1335 exit 1
1336fi
1337git -C "$origin" merge-base --is-ancestor refs/heads/main "$rebased" || {
1338 echo "valley-request: the rebased head does not fast-forward from main" >&2
1339 exit 1
1340}
1341if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_rebase" ]; then
1342 echo "valley-request: the rebase verb moved main" >&2
1343 exit 1
1344fi
1345if git -C "$origin" rev-parse --verify --quiet \
1346 refs/the-valley/integration-requests/main/topic-behind > /dev/null; then
1347 echo "valley-request: the rebase verb filed a request" >&2
1348 exit 1
1349fi
1350pushes="$(grep -c '^--- push' "$pushlog" || true)"
1351if [ "$pushes" != 1 ] || grep -v '^--- push' "$pushlog" | grep -qv ' refs/heads/topic/behind$'; then
1352 echo "valley-request: the rebase verb pushed something besides the branch" >&2
1353 cat "$pushlog" >&2
1354 exit 1
1355fi
1356
1357# A branch that fast-forwards from main already is left as it is.
1358: > "$pushlog"
1359valley rebase topic/behind < /dev/null > "$w/rebase-again.out" 2> "$w/rebase-again.err"
1360grep -qx 'topic/behind already fast-forwards from origin/main; nothing rebased' "$w/rebase-again.out"
1361grep -qx " head $rebased" "$w/rebase-again.out"
1362if [ -s "$pushlog" ]; then
1363 echo "valley-request: rebasing a branch that fast-forwards pushed something" >&2
1364 cat "$pushlog" >&2
1365 exit 1
1366fi
1367
1368# A rebase that conflicts changes nothing, says so, and exits non-zero.
1369git checkout --quiet -b topic/clash "$first"
1370echo "a note that clashes" > docs/notes.md
1371git add -A
1372git commit --quiet -m "a note that clashes"
1373git push --quiet origin topic/clash
1374clashhead="$(git rev-parse topic/clash)"
1375git checkout --quiet main
1376if valley rebase topic/clash < /dev/null > "$w/clash.out" 2> "$w/clash.err"; then
1377 echo "valley-request: a rebase that conflicts exited 0" >&2
1378 exit 1
1379fi
1380grep -q 'topic/clash conflicts with main — resolve by hand; nothing changed' "$w/clash.err"
1381if [ "$(git -C "$origin" rev-parse refs/heads/topic/clash)" != "$clashhead" ]; then
1382 echo "valley-request: a rebase that conflicted moved the branch" >&2
1383 exit 1
1384fi
1385
1386# ----------------------------------------------------------------------
1387# 19. A change whose version names no branch is approved at its head like
1388# any other. The record allows a version with no branch, though valley
1389# ready always names one, so the record here is written by hand. The
1390# check it requires runs over the recorded head, and the evidence and the
1391# c-<change ID> request reach origin in one push.
1392git checkout --quiet -b branchless main
1393echo "a readme no branch carries" > docs/readme.md
1394git commit --quiet -am "a readme no branch carries"
1395barehead="$(git rev-parse HEAD)"
1396baredigest="$(attest digest --rev "$barehead" | cut -d: -f2)"
1397git checkout --quiet main
1398bareid=rrrrssssttttuuuuvvvvwwwwxxxxyyyy
1399blob="$(printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "ready", "seq": 1, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "no branch", "branch": "", "uploader": "contributor"}], "active": 1}\n' \
1400 "$bareid" "$barehead" | git hash-object -w --stdin)"
1401record="$(git commit-tree "$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" \
1402 -p "$barehead" -m 'a change record')"
1403git push --quiet origin "$record:refs/the-valley/changes/main/$bareid"
1404
1405: > "$pushlog"
1406valley approve -v --head "$barehead" rrrr < /dev/null > "$w/branchless.out" 2> "$w/branchless.err"
1407grep -qx '◐ rrrr v1 checking 1 check: tree-ok' "$w/branchless.out"
1408grep -qx " request refs/the-valley/integration-requests/main/c-$bareid -> $(git rev-parse --short "$barehead")" "$w/branchless.out"
1409grep -qx " evidence refs/the-valley/attestations/$baredigest/$keyhash" "$w/branchless.out"
1410if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$bareid")" != "$barehead" ]; then
1411 echo "valley-request: approve of a change with no branch did not file its head" >&2
1412 exit 1
1413fi
1414pushes="$(grep -c '^--- push' "$pushlog" || true)"
1415if [ "$pushes" != 1 ] \
1416 || ! grep -q "refs/the-valley/attestations/$baredigest/$keyhash" "$pushlog" \
1417 || ! grep -q "refs/the-valley/integration-requests/main/c-$bareid" "$pushlog"; then
1418 echo "valley-request: the evidence and request for a change with no branch did not go in one push" >&2
1419 cat "$pushlog" >&2
1420 exit 1
1421fi
1422# A change with a record is timed under its ID and the version approved.
1423if ! tail -n 1 "$timings" | jq -e --arg id "$bareid" --arg head "$barehead" \
1424 '.change == $id and .version == 1 and .head == $head and .check == "tree-ok"' > /dev/null; then
1425 echo "valley-request: the timing of a change with a record does not name the change and version:" >&2
1426 tail -n 1 "$timings" >&2
1427 exit 1
1428fi
1429
1430# ----------------------------------------------------------------------
1431# 20. Evidence origin already holds is reused, and only what is missing
1432# runs here. A check worker publishes evidence as this section's
1433# worker does: attest run under its own key, then a push of the
1434# attestation ref alone. This origin runs the protected host's real
1435# pre-receive hook, which admits a note only when a key it knows signed
1436# it, behind a wrapper that logs every push it receives. The project's
1437# owning valley publishes the signers a client verifies with, at
1438# refs/the-valley/signers, written on disk as the host's publisher
1439# writes it (signers-e2e drives the publisher itself). Both of the
1440# project's checks use the nix runner, through integrator-e2e's
1441# stand-in for nix, wrapped so that every call is logged: a check that
1442# ran is a check the log shows a build of.
1443r="$w/reuse"
1444mkdir -p "$r/bin" "$r/keys"
1445cp -r "$integratorE2e" "$r/stand-ins"
1446chmod -R +w "$r/stand-ins"
1447patchShebangs "$r/stand-ins" > /dev/null
1448nixlog="$r/nix.log"
1449for tool in nix nix-store; do
1450 cat > "$r/bin/$tool" <<EOF
1451#!$(command -v bash)
1452printf '%s %s\n' "$tool" "\$*" >> "$nixlog"
1453exec "$r/stand-ins/$tool" "\$@"
1454EOF
1455 chmod +x "$r/bin/$tool"
1456done
1457export PATH="$r/bin:$PATH" VALLEY_FAKE_STORE="$r/store"
1458
1459for who in worker operator integrator; do install -m 600 "$reuseKeys/$who" "$r/keys/$who"; done
1460workername=worker.valley.invalid/attestations
1461operatorname=operator.valley.invalid/attestations
1462integratorname=integrator.valley.invalid/attestations
1463workerhash="$(attest key --key "$r/keys/worker" --name "$workername" | cut -d+ -f2)"
1464operatorhash="$(attest key --key "$r/keys/operator" --name "$operatorname" | cut -d+ -f2)"
1465{
1466 attest key --key "$r/keys/worker" --name "$workername"
1467 attest key --key "$r/keys/operator" --name "$operatorname"
1468 attest key --key "$r/keys/integrator" --name "$integratorname"
1469} > "$r/known-signers"
1470
1471# The owning valley: the floor, which requires nothing, and the signers it
1472# publishes.
1473instance="$r/instance.git"
1474git init --quiet --bare "$instance"
1475git init --quiet "$r/seed"
1476mkdir -p "$r/seed/policy/instance" "$r/floor"
1477printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$r/floor/floor.cue"
1478cp "$r/floor/floor.cue" "$r/seed/policy/instance/"
1479git -C "$r/seed" add -A
1480git -C "$r/seed" commit --quiet -m 'the floor'
1481git -C "$r/seed" push --quiet "$instance" main
1482# published <file> [<mode> <type> <object>]: the instance publishes the
1483# known-signers file, and, given an entry, that entry as signer-projects.
1484published() {
1485 local keysblob nameblob entries
1486 keysblob="$(git -C "$instance" hash-object -w "$1")"
1487 nameblob="$(printf '%s\n' "$integratorname" | git -C "$instance" hash-object -w --stdin)"
1488 entries="$(printf '100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers' "$nameblob" "$keysblob")"
1489 [ $# -lt 4 ] || entries+="$(printf '\n%s %s %s\tsigner-projects' "$2" "$3" "$4")"
1490 git -C "$instance" update-ref refs/the-valley/signers "$(printf '%s\n' "$entries" | git -C "$instance" mktree)"
1491}
1492published "$r/known-signers"
1493
1494served="$r/project.git"
1495hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$reuseInitScriptPath" | head -n1)"
1496test -x "$hook"
1497git init --quiet --bare "$served"
1498reusepushes="$r/pushlog"
1499cat > "$served/hooks/pre-receive" <<EOF
1500#!/bin/sh
1501echo '--- push' >> "$reusepushes"
1502tee -a "$reusepushes" | "$hook"
1503EOF
1504chmod +x "$served/hooks/pre-receive"
1505
1506proj="$r/project"
1507git init --quiet "$proj"
1508cd "$proj" || exit 1
1509git remote add origin "$served"
1510mkdir -p docs src live checks policy/project
1511echo "$instance" > policy/valley
1512echo "the readme" > docs/readme.md
1513echo "the source" > src/main.txt
1514echo "the state" > live/state.txt
1515echo docs > checks/prose-format.inputs
1516echo src > checks/code-build.inputs
1517cat > policy/project/policy.cue <<'EOF'
1518package verification
1519
1520project: {
1521 checks: {
1522 "prose-format": {runner: "nix", attribute: "prose-format"}
1523 "code-build": {runner: "nix", attribute: "code-build"}
1524 "live-ok": {runner: "command", command: "true", validity: "1h"}
1525 }
1526 classes: {
1527 prose: {paths: "docs/**": true, requires: "prose-format": true}
1528 code: {paths: "src/**": true, requires: "code-build": true}
1529 live: {paths: "live/**": true, requires: "live-ok": true}
1530 }
1531 unclassified: {}
1532}
1533EOF
1534git add -A
1535git commit --quiet -m 'seed the project'
1536git push --quiet origin main
1537git clone --quiet "$served" "$r/worker"
1538
1539# edited <branch> <file…>: a branch off origin's main that adds a line to
1540# each file, pushed.
1541edited() {
1542 local name="$1" f
1543 shift
1544 git fetch --quiet origin
1545 git checkout --quiet -B "$name" origin/main
1546 for f in "$@"; do echo "$name" >> "$f"; done
1547 git add -A
1548 git commit --quiet -m "$name"
1549 git push --quiet origin "$name"
1550}
1551# worker <head> <attest run flag…>: the worker attests the head under its
1552# own key and pushes the attestation ref alone.
1553worker() {
1554 local head="$1" digest ref
1555 shift
1556 git -C "$r/worker" fetch --quiet origin
1557 digest="$(attest digest --repo "$r/worker" --rev "$head" | cut -d: -f2)"
1558 ref="refs/the-valley/attestations/$digest/$workerhash"
1559 attest run --repo "$r/worker" --rev "$head" --key "$r/keys/worker" --name "$workername" "$@" \
1560 > "$r/worker.out" 2>&1 || {
1561 echo "valley-request: the worker's attest run failed" >&2
1562 cat "$r/worker.out" >&2
1563 exit 1
1564 }
1565 git -C "$r/worker" push --quiet origin "$ref:$ref"
1566}
1567# approving <name> <command…>: run the command as the principal that holds
1568# the approve grant, signing as the operator, into <name>.out and .err in
1569# the scratch directory. Origin's pushes and the nix log start empty.
1570approving() {
1571 local name="$1"
1572 shift
1573 : > "$nixlog"
1574 : > "$reusepushes"
1575 VALLEY_PRINCIPAL=requester VALLEY_ATTEST_KEY="$r/keys/operator" VALLEY_ATTEST_NAME="$operatorname" \
1576 "$@" < /dev/null > "$w/$name.out" 2> "$w/$name.err"
1577}
1578# requested <branch>: where origin's request for the branch stands, or
1579# nothing.
1580requested() {
1581 git -C "$served" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/$1" || true
1582}
1583# integrated <request> [<signer projects file>]: one pass of the real
1584# integrator, given the file when there is one, lands the request.
1585integrated() {
1586 integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
1587 --known-signers "$r/known-signers" ${2:+--signer-projects "$2"} --instance "$r/floor" --schema "$schemaFile" \
1588 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
1589 echo "valley-request: the integrator failed" >&2
1590 cat "$r/integrator.out" >&2
1591 exit 1
1592 }
1593 grep -q "^$1 -> refs/heads/main .*: land\$" "$r/integrator.out" || {
1594 echo "valley-request: the integrator did not land $1" >&2
1595 cat "$r/integrator.out" >&2
1596 exit 1
1597 }
1598}
1599
1600# 20a. The worker covered every required check. approve reuses both, runs
1601# nothing, needs no signing key, and pushes the request alone.
1602edited cover docs/readme.md src/main.txt
1603coverhead="$(git rev-parse cover)"
1604coverdigest="$(attest digest --rev cover | cut -d: -f2)"
1605worker "$coverhead" --check prose-format --check code-build
1606approving cover env -u VALLEY_ATTEST_KEY valley approve -v origin/cover
1607grep -q "^reusing the evidence origin holds for cover at $(git rev-parse --short cover), tree " "$w/cover.out"
1608grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/cover.out"
1609grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/cover.out"
1610grep -qx " reused refs/the-valley/attestations/$coverdigest/$workerhash" "$w/cover.out"
1611grep -qx ' checks code-build, prose-format' "$w/cover.out"
1612if grep -qE '^(checking| evidence) ' "$w/cover.out" || [ -s "$nixlog" ]; then
1613 echo "valley-request: approve ran checks that origin's evidence covered" >&2
1614 cat "$w/cover.out" "$nixlog" >&2
1615 exit 1
1616fi
1617[ "$(requested cover)" = "$coverhead" ] || {
1618 echo "valley-request: a fully covered approval filed no request" >&2
1619 exit 1
1620}
1621if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1622 || ! grep -q ' refs/the-valley/integration-requests/main/cover$' "$reusepushes" \
1623 || grep -q ' refs/the-valley/attestations/' "$reusepushes"; then
1624 echo "valley-request: a fully covered approval did not push the request alone" >&2
1625 cat "$reusepushes" >&2
1626 exit 1
1627fi
1628
1629# 20b. The same head again: the evidence is on origin and the request
1630# stands, so there is nothing to push and no push is made.
1631approving cover-again env -u VALLEY_ATTEST_KEY valley approve -v origin/cover
1632grep -qx ' the request already stood at this head and origin already held the evidence, so nothing needed publishing' "$w/cover-again.out"
1633if [ -s "$reusepushes" ] || [ -s "$nixlog" ]; then
1634 echo "valley-request: approving a covered head again pushed or ran something" >&2
1635 cat "$reusepushes" "$nixlog" >&2
1636 exit 1
1637fi
1638# The integrator verifies the worker's evidence itself, and lands it.
1639integrated cover
1640
1641# 20c. The request write stays leased when nothing runs: a request filed
1642# while this approval is prepared wins, and nothing is published.
1643edited raced docs/readme.md
1644racedhead="$(git rev-parse raced)"
1645worker "$racedhead" --check prose-format
1646arm_race "git -C '$served' update-ref refs/the-valley/integration-requests/main/raced '$coverhead'"
1647if approving raced env -u VALLEY_ATTEST_KEY valley approve -v origin/raced; then
1648 echo "valley-request: a covered approval clobbered a request filed during it" >&2
1649 exit 1
1650fi
1651disarm_race
1652grep -q "^⊘ raced not approved · another approval filed $(git rev-parse --short "$coverhead") (.*) meanwhile$" "$w/raced.err"
1653if [ "$(requested raced)" != "$coverhead" ] || [ -s "$reusepushes" ]; then
1654 echo "valley-request: a covered approval that lost the lease published something" >&2
1655 exit 1
1656fi
1657git -C "$served" update-ref -d refs/the-valley/integration-requests/main/raced
1658
1659# 20d. The worker covered one check and has no note for the other. A worker
1660# note for the other check that names another flake attribute is not
1661# reused either (the attribute case below), and the integrator
1662# refuses it too. Without --build-here, approve
1663# files the request at once with the evidence origin holds: it builds
1664# nothing, pushes the request alone, names the missing check, and
1665# exits 0. review's [a]pprove files the same way. Each request is
1666# taken away again before the integrator reads it. With --build-here,
1667# that check runs here: exactly that one, signed by the operator, in
1668# one push with the request. The integrator lands it on the two
1669# signers' evidence. 21f has the integrator hold a request filed
1670# without evidence, and land it once the evidence arrives.
1671edited partial docs/readme.md src/main.txt
1672partialhead="$(git rev-parse partial)"
1673partialdigest="$(attest digest --rev partial | cut -d: -f2)"
1674worker "$partialhead" --check prose-format
1675for filed in partial-filed partial-review; do
1676 if [ "$filed" = partial-filed ]; then
1677 approving "$filed" valley approve -v origin/partial
1678 else
1679 approving "$filed" bash -c 'printf "a\n" | valley review -v origin/partial'
1680 fi
1681 if ! grep -qx '◇ partial approved · waiting for 1 check (no worker building)' "$w/$filed.out" \
1682 || ! grep -qx ' run: valley approve --build-here partial' "$w/$filed.out" \
1683 || ! grep -qx ' missing code-build' "$w/$filed.out" \
1684 || ! grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/$filed.out" \
1685 || grep -qE '^(checking| evidence) ' "$w/$filed.out" || [ -s "$nixlog" ] \
1686 || [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1687 || ! grep -q ' refs/the-valley/integration-requests/main/partial$' "$reusepushes" \
1688 || grep -q ' refs/the-valley/attestations/' "$reusepushes" \
1689 || [ "$(requested partial)" != "$partialhead" ]; then
1690 echo "valley-request: $filed did not file the request alone while naming the missing nix check" >&2
1691 cat "$w/$filed.out" "$w/$filed.err" "$nixlog" "$reusepushes" >&2
1692 exit 1
1693 fi
1694 git -C "$served" update-ref -d refs/the-valley/integration-requests/main/partial
1695done
1696approving partial valley approve -v --build-here origin/partial
1697grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/partial.out"
1698grep -qx "◐ partial checking 1 check: code-build" "$w/partial.out"
1699grep -qE '^check code-build +nix +passed$' "$w/partial.out"
1700if ! grep -qE '^nix build .*/code-build-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" || grep -q 'prose-format' "$nixlog"; then
1701 echo "valley-request: a partly covered approval did not run exactly the missing check" >&2
1702 cat "$nixlog" >&2
1703 exit 1
1704fi
1705grep -qx " evidence refs/the-valley/attestations/$partialdigest/$operatorhash" "$w/partial.out"
1706grep -qx " reused refs/the-valley/attestations/$partialdigest/$workerhash" "$w/partial.out"
1707if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1708 || ! grep -q " refs/the-valley/attestations/$partialdigest/$operatorhash\$" "$reusepushes" \
1709 || ! grep -q ' refs/the-valley/integration-requests/main/partial$' "$reusepushes"; then
1710 echo "valley-request: the remainder's evidence and the request did not go in one push" >&2
1711 cat "$reusepushes" >&2
1712 exit 1
1713fi
1714git cat-file -p "refs/the-valley/attestations/$partialdigest/$operatorhash:code-build/statement.note" > /dev/null
1715if git cat-file -e "refs/the-valley/attestations/$partialdigest/$operatorhash:prose-format" 2> /dev/null; then
1716 echo "valley-request: the operator signed a check the worker's evidence covered" >&2
1717 exit 1
1718fi
1719integrated partial
1720
1721# 20e. Origin already holds the operator's own evidence for this tree, for
1722# one check alone. It is reused, and the other check has nowhere to
1723# go, since approve publishes a signer's evidence for a tree once:
1724# refused before anything runs (bd-7b7d58f).
1725edited occupied docs/readme.md src/main.txt
1726occupiedhead="$(git rev-parse occupied)"
1727occupieddigest="$(attest digest --rev occupied | cut -d: -f2)"
1728attest run --rev "$occupiedhead" --key "$r/keys/operator" --name "$operatorname" --check code-build \
1729 > "$r/operator.out" 2>&1
1730git push --quiet origin "refs/the-valley/attestations/$occupieddigest/$operatorhash:refs/the-valley/attestations/$occupieddigest/$operatorhash"
1731if approving occupied valley approve -v --build-here origin/occupied; then
1732 echo "valley-request: approve built evidence that origin could not take" >&2
1733 exit 1
1734fi
1735grep -qE "^ code-build +signed by $operatorname\+$operatorhash\$" "$w/occupied.out"
1736grep -qx "⊘ occupied not approved · this key's evidence for the tree, on origin, lacks prose-format" "$w/occupied.err"
1737grep -qx ' attest with another key: set VALLEY_ATTEST_KEY' "$w/occupied.err"
1738if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested occupied)" ]; then
1739 echo "valley-request: an approval refused for an occupied ref ran or published something" >&2
1740 exit 1
1741fi
1742
1743# 20f. A listing of origin's evidence that fails, and a fetch of it that
1744# fails, are refusals, never an empty answer: nothing runs and nothing
1745# is published. A stand-in for git, first on PATH, fails the one call
1746# named. It needs the raw binary, since the package's wrapper puts
1747# its own git ahead of anything on PATH.
1748edited unread docs/readme.md
1749unreadhead="$(git rev-parse unread)"
1750unreaddigest="$(attest digest --rev unread | cut -d: -f2)"
1751worker "$unreadhead" --check prose-format
1752unreadoid="$(git -C "$served" rev-parse "refs/the-valley/attestations/$unreaddigest/$workerhash")"
1753realgit="$(command -v git)"
1754mkdir -p "$r/shim"
1755cat > "$r/shim/git" <<EOF
1756#!$(command -v bash)
1757# git, failing the one call REUSE_FAIL names: the listing of origin's
1758# evidence for one tree, or a fetch of the object one of its refs names.
1759case "\${REUSE_FAIL:-}:\$*" in
1760 inventory:ls-remote*"refs/the-valley/attestations/$unreaddigest/"*)
1761 echo "git: injected failure" >&2
1762 exit 128
1763 ;;
1764 fetch:fetch*--stdin*)
1765 wanted="\$(cat)"
1766 if grep -qF "$unreadoid" <<< "\$wanted"; then
1767 echo "git: injected failure" >&2
1768 exit 128
1769 fi
1770 printf '%s\n' "\$wanted" | exec "$realgit" "\$@"
1771 ;;
1772esac
1773exec "$realgit" "\$@"
1774EOF
1775chmod +x "$r/shim/git"
1776for failing in inventory fetch; do
1777 if REUSE_FAIL="$failing" PATH="$r/shim:$PATH" VALLEY_VERIFICATION_SCHEMA="$schemaFile" \
1778 approving "unread-$failing" "$valleyUnwrapped" approve -v origin/unread; then
1779 echo "valley-request: approve went on past a failed $failing of origin's evidence" >&2
1780 exit 1
1781 fi
1782 case "$failing" in
1783 inventory) says='valley: could not list the evidence origin holds for this tree' ;;
1784 fetch) says='valley: could not fetch the evidence origin holds for this tree' ;;
1785 esac
1786 grep -qxF "$says" "$w/unread-$failing.err"
1787 grep -qx '⊘ unread not approved · could not read the evidence origin holds' "$w/unread-$failing.err"
1788 if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested unread)" ]; then
1789 echo "valley-request: approve ran or published something after a failed $failing" >&2
1790 exit 1
1791 fi
1792done
1793# And with origin answering, the same approval reuses the worker's
1794# evidence: the stand-in failed only what it was told to.
1795VALLEY_VERIFICATION_SCHEMA="$schemaFile" approving unread "$valleyUnwrapped" approve -v origin/unread
1796grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/unread.out"
1797[ "$(requested unread)" = "$unreadhead" ]
1798integrated unread
1799
1800# 20g. approve skips a check only where the integrator accepts the same
1801# evidence. Each case below stores one set of evidence on origin.
1802# Approve, with no signing key, reuses nothing for it, naming the note
1803# the integrator judges the check by and why it is not reused. It files
1804# the request at once all the same, naming the check as missing and
1805# building nothing. Then one pass of the real integrator decides that
1806# request from the same refs. A note the hook would
1807# refuse is planted past it, straight into origin's storage. Two cases
1808# land: there approve is stricter than the integrator, on purpose.
1809#
1810# noted <file> <key> <name> <rev> <check> <attest run flag…>: attest the
1811# revision in the worker's clone under the key, and copy the note it stored
1812# for the check to the file, byte for byte. Nothing pushes the ref it
1813# stored.
1814noted() {
1815 local file="$1" key="$2" name="$3" rev="$4" check="$5" digest hash
1816 shift 5
1817 git -C "$r/worker" fetch --quiet origin
1818 digest="$(attest digest --repo "$r/worker" --rev "$rev" | cut -d: -f2)"
1819 hash="$(attest key --key "$key" --name "$name" | cut -d+ -f2)"
1820 attest run --repo "$r/worker" --rev "$rev" --key "$key" --name "$name" "$@" > "$r/noted.out" 2>&1 || {
1821 echo "valley-request: attesting $rev for a planted note failed" >&2
1822 cat "$r/noted.out" >&2
1823 exit 1
1824 }
1825 git -C "$r/worker" cat-file blob "refs/the-valley/attestations/$digest/$hash:$check/statement.note" > "$file"
1826}
1827# resigned <file> <sed expression>: the note's text, edited by the
1828# expression, signed again by the worker.
1829resigned() {
1830 sed -n '/^$/q;p' "$1" | sed "$2" > "$r/text"
1831 attest sign --key "$r/keys/worker" --name "$workername" "$r/text" > "$1"
1832}
1833# planted <ref> <check> <file>: the note stored at the ref on origin, in a
1834# tree shaped as attest shapes one, straight into storage.
1835planted() {
1836 local blob sub
1837 blob="$(git -C "$served" hash-object -w "$3")"
1838 sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$served" mktree)"
1839 git -C "$served" update-ref "$1" "$(printf '040000 tree %s\t%s\n' "$sub" "$2" | git -C "$served" mktree)"
1840}
1841# unreused <branch> <reason>: approve, with no signing key, reuses nothing
1842# for prose-format, says why, and files the request with prose-format
1843# missing, building nothing.
1844unreused() {
1845 approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"
1846 if ! grep -qF "valley: not reusing prose-format: the integrator judges it by " "$w/$1.err" \
1847 || ! grep -qF -- "$2" "$w/$1.err" || grep -q 'reusing' "$w/$1.out" \
1848 || ! grep -qx ' missing prose-format' "$w/$1.out" \
1849 || [ "$(requested "$1")" != "$(git rev-parse "$1")" ] || [ -s "$nixlog" ]; then
1850 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
1851 cat "$w/$1.err" >&2
1852 exit 1
1853 fi
1854}
1855# unpassed <branch> <reason>: the note the integrator judges prose-format
1856# by verifies and is no pass of it, so building the check again changes
1857# nothing for that tree. approve says why, refuses the head, and files
1858# and builds nothing.
1859unpassed() {
1860 if approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"; then
1861 echo "valley-request: approve filed $1, whose judged note is no pass" >&2
1862 exit 1
1863 fi
1864 if ! grep -qF -- "$2" "$w/$1.err" \
1865 || ! grep -q "^⊘ $1 not approved · 1 of [0-9]* checks* failed\$" "$w/$1.err" \
1866 || ! grep -qx " ✗ prose-format — see: valley status $1" "$w/$1.err" \
1867 || [ -n "$(requested "$1")" ] || [ -s "$nixlog" ]; then
1868 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
1869 cat "$w/$1.err" >&2
1870 exit 1
1871 fi
1872}
1873# judged <branch> <outcome> <reason> [<known-signers> [<signer projects>]]:
1874# the branch's head is filed, by hand when approve has not, and one pass of the real integrator,
1875# verifying with the known-signers file and given the signer projects file
1876# when there is one, decides it as the outcome, giving the reason. A
1877# request it does not land is taken away again.
1878judged() {
1879 git -C "$served" update-ref "refs/the-valley/integration-requests/main/$1" "$(git rev-parse "$1")"
1880 integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
1881 --known-signers "${4:-$r/known-signers}" ${5:+--signer-projects "$5"} --instance "$r/floor" --schema "$schemaFile" \
1882 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
1883 echo "valley-request: the integrator failed" >&2
1884 cat "$r/integrator.out" >&2
1885 exit 1
1886 }
1887 if ! grep -q "^$1 -> refs/heads/main @ [0-9a-f]*: $2\$" "$r/integrator.out" \
1888 || ! grep -qF -- "$3" "$r/integrator.out"; then
1889 echo "valley-request: the integrator did not decide $1 as $2, saying: $3" >&2
1890 cat "$r/integrator.out" >&2
1891 exit 1
1892 fi
1893 [ "$2" = land ] || git -C "$served" update-ref -d "refs/the-valley/integration-requests/main/$1"
1894}
1895under() { printf 'refs/the-valley/attestations/%s/%s' "$(attest digest --rev "$1" | cut -d: -f2)" "$2"; }
1896
1897# Stale trust: the host revokes the worker, and this operator's own
1898# known-signers file still trusts it. The override does not widen what the
1899# host publishes.
1900{
1901 attest key --key "$r/keys/operator" --name "$operatorname"
1902 attest key --key "$r/keys/integrator" --name "$integratorname"
1903} > "$r/revoked-signers"
1904edited revoked docs/readme.md
1905worker "$(git rev-parse revoked)" --check prose-format
1906published "$r/revoked-signers"
1907VALLEY_KNOWN_SIGNERS="$r/known-signers" unreused revoked \
1908 "attest verify refuses it under the host's signers: no signature on this note is by a key the verifier holds"
1909judged revoked stale 'signed by no signer the integrator holds' "$r/revoked-signers"
1910published "$r/known-signers"
1911
1912# Narrowed trust: an override that does not trust the worker keeps approve
1913# from reusing what the host would accept.
1914edited narrowed docs/readme.md
1915worker "$(git rev-parse narrowed)" --check prose-format
1916VALLEY_KNOWN_SIGNERS="$r/revoked-signers" unreused narrowed \
1917 'attest verify refuses it under VALLEY_KNOWN_SIGNERS: no signature on this note is by a key the verifier holds'
1918judged narrowed land 'transferred (unmoved)'
1919
1920# Conflicting notes: the integrator judges a check by the first note that
1921# verifies, in ref order. A failure the worker signed sits first, so the
1922# pass the worker published after it does not count.
1923edited failed docs/readme.md
1924noted "$r/failed.note" "$r/keys/worker" "$workername" "$(git rev-parse failed)" prose-format --check prose-format
1925resigned "$r/failed.note" 's/^predicate.result passed$/predicate.result failed/'
1926attest verify --note "$r/failed.note" --known-keys "$r/known-signers" --repo . --rev failed > /dev/null
1927planted "$(under failed 00000000)" prose-format "$r/failed.note"
1928worker "$(git rev-parse failed)" --check prose-format
1929unpassed failed 'its predicate.result is "failed", not "passed"'
1930judged failed stale 'the attestation records the check as failed'
1931
1932# The same, with an observation first: a note of the wrong kind is no
1933# evidence, so the request is stale.
1934edited kind docs/readme.md
1935noted "$r/kind.note" "$r/keys/worker" "$workername" "$(git rev-parse kind)" prose-format --command prose-format=true
1936planted "$(under kind 00000000)" prose-format "$r/kind.note"
1937worker "$(git rev-parse kind)" --check prose-format
1938unpassed kind 'its predicateType is "the-valley/check/effectful/v1", not "the-valley/check/pure/v1"'
1939judged kind stale 'the policy requires a pure check and the attestation is for an effectful check'
1940
1941# A malformed envelope: a note the worker signed, without its last newline.
1942# approve reads it byte for byte, as the integrator does.
1943edited envelope docs/readme.md
1944noted "$r/envelope.note" "$r/keys/worker" "$workername" "$(git rev-parse envelope)" prose-format --check prose-format
1945head -c -1 "$r/envelope.note" > "$r/envelope.cut"
1946planted "$(under envelope "$workerhash")" prose-format "$r/envelope.cut"
1947unreused envelope 'the signature block is empty or does not end with a newline'
1948judged envelope reject 'the attestation does not verify as the statement it claims to be'
1949
1950# An unknown signature: a key neither the host nor the hook knows.
1951ssh-keygen -q -t ed25519 -N "" -C stranger -f "$r/keys/stranger"
1952strangername=stranger.valley.invalid/attestations
1953edited stranger docs/readme.md
1954noted "$r/stranger.note" "$r/keys/stranger" "$strangername" "$(git rev-parse stranger)" prose-format --check prose-format
1955planted "$(under stranger "$(attest key --key "$r/keys/stranger" --name "$strangername" | cut -d+ -f2)")" \
1956 prose-format "$r/stranger.note"
1957unreused stranger 'no signature on this note is by a key the verifier holds'
1958judged stranger stale 'signed by no signer the integrator holds'
1959
1960# A wrong tree: the worker's note about main, stored under this head's tree.
1961edited tree docs/readme.md
1962noted "$r/tree.note" "$r/keys/worker" "$workername" origin/main prose-format --check prose-format
1963planted "$(under tree "$workerhash")" prose-format "$r/tree.note"
1964unreused tree 'tree digest mismatch'
1965judged tree reject 'tree digest mismatch'
1966
1967# An expired observation of a command check. approve never reuses one, so
1968# with no key it refuses; the integrator refuses the observation as too old.
1969edited expired live/state.txt
1970noted "$r/expired.note" "$r/keys/worker" "$workername" "$(git rev-parse expired)" live-ok --command live-ok=true
1971resigned "$r/expired.note" 's/^predicate.observedAt .*/predicate.observedAt 2020-01-01T00:00:00Z/'
1972planted "$(under expired "$workerhash")" live-ok "$r/expired.note"
1973if approving expired env -u VALLEY_ATTEST_KEY valley approve -v origin/expired \
1974 || ! grep -q 'no signing key' "$w/expired.err" || grep -q 'reusing' "$w/expired.out"; then
1975 echo "valley-request: approve reused an observation of a command check" >&2
1976 cat "$w/expired.out" "$w/expired.err" >&2
1977 exit 1
1978fi
1979judged expired stale 'the observation is '
1980
1981# The attribute: the worker's note for prose-format names another flake
1982# attribute. Approve compares it and refuses the head; the integrator
1983# compares it too, on an unmoved tree, and the note is no evidence for
1984# prose-format.
1985edited attribute docs/readme.md
1986worker "$(git rev-parse attribute)" --check prose-format=code-build
1987unpassed attribute 'its predicate.check.attribute is "code-build", not "prose-format"'
1988judged attribute stale 'the attestation is for runner "nix", attribute "code-build", and the policy requires runner "nix", attribute "prose-format"'
1989
1990# 20h. A signer can be restricted to projects. The host publishes its
1991# signer projects file beside the known-signers file, and approve
1992# reuses a note only when one of its signers counts in this project.
1993# The project is named project, after origin, as the integrator names
1994# it after its repository. Each integrator pass is given the file the
1995# host publishes.
1996#
1997# restricting <file>: the instance publishes the known-signers file with
1998# this signer projects file.
1999restricting() {
2000 published "$r/known-signers" 100644 blob "$(git -C "$instance" hash-object -w "$1")"
2001}
2002# unpublished <branch> <reason>: the publication is not used, so approve,
2003# with no signing key, reuses none of origin's evidence, says why, and
2004# files the request with prose-format missing, building nothing.
2005unpublished() {
2006 approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"
2007 if ! grep -qxF "valley: $2, so none of the evidence on origin is reused" "$w/$1.err" \
2008 || grep -q 'reusing' "$w/$1.out" \
2009 || ! grep -qx ' missing prose-format' "$w/$1.out" \
2010 || [ "$(requested "$1")" != "$(git rev-parse "$1")" ] || [ -s "$nixlog" ]; then
2011 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
2012 cat "$w/$1.err" >&2
2013 exit 1
2014 fi
2015}
2016
2017# The worker restricted to another project: its note is a stranger's here.
2018# approve does not reuse it, the integrator does not accept it, and an
2019# approval from review --build-here, with a signing key, builds the check
2020# under the operator's key.
2021printf '%s elsewhere\n' "$workername" > "$r/elsewhere.projects"
2022restricting "$r/elsewhere.projects"
2023edited elsewhere docs/readme.md
2024elsewherehead="$(git rev-parse elsewhere)"
2025worker "$elsewherehead" --check prose-format
2026unreused elsewhere \
2027 "none of its signers counts in project project: the published signer-projects lets $workername count only in elsewhere"
2028judged elsewhere stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects"
2029approving elsewhere-built bash -c 'printf "a\n" | valley review -v --build-here origin/elsewhere'
2030grep -qF "valley: not reusing prose-format: the integrator judges it by $(under elsewhere "$workerhash"):prose-format/statement.note, and none of its signers counts in project project" "$w/elsewhere-built.err"
2031grep -qx "◐ elsewhere checking 1 check: prose-format" "$w/elsewhere-built.out"
2032grep -qx " evidence $(under elsewhere "$operatorhash")" "$w/elsewhere-built.out"
2033if ! grep -qE '^nix build .*/prose-format-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" \
2034 || grep -q '^ reused ' "$w/elsewhere-built.out"; then
2035 echo "valley-request: approve did not build the check whose only signer counts elsewhere" >&2
2036 cat "$w/elsewhere-built.out" "$nixlog" >&2
2037 exit 1
2038fi
2039[ "$(requested elsewhere)" = "$elsewherehead" ]
2040integrated elsewhere "$r/elsewhere.projects"
2041
2042# The same worker note, countersigned by the integrator. The integrator's
2043# own signature never counts for a check, so the note still has no signer
2044# who counts here.
2045edited countersigned docs/readme.md
2046noted "$r/countersigned.note" "$r/keys/worker" "$workername" "$(git rev-parse countersigned)" prose-format --check prose-format
2047attest sign --key "$r/keys/integrator" --name "$integratorname" "$r/countersigned.note" > "$r/countersigned.both"
2048planted "$(under countersigned "$workerhash")" prose-format "$r/countersigned.both"
2049unreused countersigned \
2050 "the published signer-projects lets $workername count only in elsewhere; $integratorname is the host's integrator, whose signature is a countersignature"
2051judged countersigned stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects"
2052
2053# A published list that does not read, and one that is no file: the
2054# publication is not used, and nothing on origin is reused.
2055edited listed docs/readme.md
2056listedhead="$(git rev-parse listed)"
2057worker "$listedhead" --check prose-format
2058printf '%s project\n%s elsewhere\n' "$workername" "$workername" > "$r/twice.projects"
2059restricting "$r/twice.projects"
2060unpublished listed \
2061 "signer-projects in refs/the-valley/signers in $instance does not read: line 2: $workername is named on an earlier line too"
2062printf '%s project\n' "$workername" > "$r/here.projects"
2063listblob="$(git -C "$instance" hash-object -w "$r/here.projects")"
2064for entry in "040000 tree $(printf '100644 blob %s\tlist\n' "$listblob" | git -C "$instance" mktree)" "100755 blob $listblob"; do
2065 # shellcheck disable=SC2086 # the entry is a mode, a type and an object
2066 published "$r/known-signers" $entry
2067 unpublished listed "signer-projects in refs/the-valley/signers in $instance is not a file"
2068done
2069
2070# The worker restricted to this project and one more: the same note is
2071# reused, nothing runs, and the integrator lands it.
2072printf '# the projects the worker builds for\n%s elsewhere project\n' "$workername" > "$r/here.projects"
2073restricting "$r/here.projects"
2074approving listed env -u VALLEY_ATTEST_KEY valley approve -v origin/listed
2075grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/listed.out"
2076grep -qx " reused $(under listed "$workerhash")" "$w/listed.out"
2077if [ -s "$nixlog" ] || [ "$(requested listed)" != "$listedhead" ]; then
2078 echo "valley-request: approve did not reuse the note of a worker restricted to this project" >&2
2079 cat "$w/listed.out" "$w/listed.err" "$nixlog" >&2
2080 exit 1
2081fi
2082integrated listed "$r/here.projects"
2083published "$r/known-signers"
2084
2085# 20i. The worker covered the one nix check, and the change requires a
2086# command check too. No worker runs a command check, so approve runs
2087# it here without --build-here, builds no nix check, and files.
2088edited commanded docs/readme.md live/state.txt
2089commandedhead="$(git rev-parse commanded)"
2090worker "$commandedhead" --check prose-format
2091approving commanded valley approve -v origin/commanded
2092grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/commanded.out"
2093grep -qx "◐ commanded checking 1 check: live-ok" "$w/commanded.out"
2094if grep -q '^nix build' "$nixlog" || [ "$(requested commanded)" != "$commandedhead" ]; then
2095 echo "valley-request: an approval whose only uncovered check is a command check built nix or filed nothing" >&2
2096 cat "$w/commanded.out" "$w/commanded.err" "$nixlog" >&2
2097 exit 1
2098fi
2099integrated commanded
2100
2101# 20j. No worker has built the change, and it requires a command check and
2102# a nix check. approve without --build-here runs the command check
2103# here, builds no nix check, and publishes the command check's
2104# evidence with the request in one push, naming the nix check as
2105# missing. It does not offer --build-here, which that evidence now
2106# blocks (bd-7b7d58f).
2107edited mixed docs/readme.md live/state.txt
2108mixedhead="$(git rev-parse mixed)"
2109mixeddigest="$(attest digest --rev mixed | cut -d: -f2)"
2110approving mixed valley approve -v origin/mixed
2111grep -qx "◐ mixed checking 1 check: live-ok" "$w/mixed.out"
2112grep -qx ' missing prose-format' "$w/mixed.out"
2113grep -qx '◇ mixed approved · waiting for 1 check (no worker building)' "$w/mixed.out"
2114grep -qx ' run: valley ready mixed' "$w/mixed.out"
2115grep -qx " evidence refs/the-valley/attestations/$mixeddigest/$operatorhash" "$w/mixed.out"
2116if grep -q '^nix build' "$nixlog" || [ "$(requested mixed)" != "$mixedhead" ] \
2117 || [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
2118 || ! grep -q " refs/the-valley/attestations/$mixeddigest/$operatorhash\$" "$reusepushes" \
2119 || ! grep -q ' refs/the-valley/integration-requests/main/mixed$' "$reusepushes"; then
2120 echo "valley-request: an approval missing a nix check did not run the command check alone and file with its evidence" >&2
2121 cat "$w/mixed.out" "$w/mixed.err" "$nixlog" "$reusepushes" >&2
2122 exit 1
2123fi
2124git -C "$served" update-ref -d refs/the-valley/integration-requests/main/mixed
2125
2126# ----------------------------------------------------------------------
2127# 21. The check worker builds what ready changes require before anyone
2128# approves them, and publishes it where approve reuses it. It runs as
2129# packaged, valley-worker, from a clone of the owning valley, which now
2130# publishes its project list, so the worker reads the project through
2131# its own cache clone, as valley pending --repo does. It signs with the
2132# worker's key and pushes as a principal that holds no grant, through
2133# the same protected hook. The host restricts the worker's key to this
2134# project, and each integrator pass is given that list. The worker's
2135# state directory holds its lock and its record of failed batches.
2136printf '%s project\n' "$workername" > "$r/worker.projects"
2137restricting "$r/worker.projects"
2138git -C "$instance" update-ref refs/the-valley/projects \
2139 "$(printf 'project\n' | git -C "$instance" hash-object -w --stdin)"
2140git clone --quiet "$instance" "$r/worker-home"
2141wstate="$r/worker-state"
2142git config valley.principal requester
2143# fresh: origin's pushes and the nix log start empty.
2144fresh() {
2145 : > "$nixlog"
2146 : > "$reusepushes"
2147}
2148# working <name> [<env assignment>…]: one pass of the worker, with the
2149# environment given, into $r/<name>.log. Its status is the pass's.
2150working() {
2151 local name="$1"
2152 shift
2153 (cd "$r/worker-home" && env VALLEY_PRINCIPAL=worker "$@" valley-worker --repo project \
2154 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/$name.log" 2>&1
2155}
2156# readied <branch> <file…>: edited, then readied as a change by the
2157# requester. It sets id, handle and head, and line, the start of each line
2158# the worker prints about the change's active version.
2159readied() {
2160 local name="$1"
2161 edited "$@"
2162 VALLEY_PRINCIPAL=requester valley ready "$name" > "$r/$name.ready" 2>&1
2163 id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$r/$name.ready")"
2164 handle="$(sed -n 's/^project \([^ ]*\) v1 ready$/\1/p' "$r/$name.ready")"
2165 head="$(git rev-parse "$name")"
2166 line="project: $handle v1 at ${head:0:7}"
2167 [ -n "$id" ] && [ -n "$handle" ] || {
2168 echo "valley-request: could not ready $name" >&2
2169 cat "$r/$name.ready" >&2
2170 exit 1
2171 }
2172}
2173# pushed <ref>: origin received one push since fresh, carrying that ref
2174# alone. pushed none: origin received no push.
2175pushed() {
2176 if [ "$1" = none ]; then
2177 [ ! -s "$reusepushes" ] && return
2178 elif [ "$(grep -c '^--- push' "$reusepushes")" = 1 ] \
2179 && [ "$(grep -vc '^--- push' "$reusepushes")" = 1 ] \
2180 && grep -q " $1\$" "$reusepushes"; then
2181 return
2182 fi
2183 echo "valley-request: the worker's pushes were not what was expected ($1)" >&2
2184 cat "$reusepushes" >&2
2185 exit 1
2186}
2187# built <check…>: nix built a derivation of exactly these checks, each
2188# once. attest evaluates the checks of a batch together, then builds each
2189# check's derivation with nix build <store>/<check>-XXXXXX.drv^*, which the
2190# stand-in names after the check, so every derivation a nix build line
2191# names is read as one build of that check. built nothing: nix did not run.
2192built() {
2193 local got
2194 if [ "$1" = nothing ]; then
2195 [ ! -s "$nixlog" ] && return
2196 echo "valley-request: the worker ran nix when it had nothing to build" >&2
2197 cat "$nixlog" >&2
2198 exit 1
2199 fi
2200 got="$(grep '^nix build ' "$nixlog" | tr ' ' '\n' \
2201 | sed -n 's|^.*/\([a-z0-9][a-z0-9-]*\)-[A-Za-z0-9]\{6\}\.drv^\*$|\1|p' | sort | paste -sd' ')"
2202 [ "$got" = "$(printf '%s\n' "$@" | sort | paste -sd' ')" ] && return
2203 echo "valley-request: the worker built '$got', not '$*'" >&2
2204 cat "$nixlog" >&2
2205 exit 1
2206}
2207# said <name> <line>: the pass's log holds the line.
2208said() {
2209 grep -qxF "$2" "$r/$1.log" && return
2210 echo "valley-request: the pass $1 did not say: $2" >&2
2211 cat "$r/$1.log" >&2
2212 exit 1
2213}
2214
2215# 21a. Evidence precedes approval. The worker builds the nix checks a ready
2216# change requires, together, and pushes the one attestation ref
2217# attest stored, before any request exists. A change whose only check
2218# is a command check is left to approve. The next pass finds the
2219# evidence on origin and does nothing. approve, with no signing key,
2220# reuses all of it and runs nothing, and the real integrator lands it.
2221readied lively live/state.txt
2222livelyline="$line"
2223readied early docs/readme.md src/main.txt
2224earlydigest="$(attest digest --rev early | cut -d: -f2)"
2225earlyref="refs/the-valley/attestations/$earlydigest/$workerhash"
2226fresh
2227working early
2228said early "$line: building code-build, prose-format"
2229said early "$line: published $earlyref"
2230said early "$livelyline: requires no nix check"
2231built code-build prose-format
2232pushed "$earlyref"
2233git -C "$served" cat-file -e "$earlyref:code-build/statement.note"
2234git -C "$served" cat-file -e "$earlyref:prose-format/statement.note"
2235[ -z "$(requested "c-$id")" ] || {
2236 echo "valley-request: a request existed before the worker's evidence" >&2
2237 exit 1
2238}
2239fresh
2240working early-again
2241said early-again "$line: origin holds evidence for every nix check it requires"
2242built nothing
2243pushed none
2244approving early env -u VALLEY_ATTEST_KEY valley approve -v --head "$head" "$handle"
2245grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/early.out"
2246grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/early.out"
2247built nothing
2248[ "$(requested "c-$id")" = "$head" ]
2249integrated "c-$id" "$r/worker.projects"
2250
2251# 21b. Concurrent passes take turns. The first holds the lock while it
2252# builds, and a stand-in hook keeps it building until the second says
2253# it is waiting. The second then reads origin after the first has
2254# published, and builds nothing.
2255readied together src/main.txt
2256togetherref="refs/the-valley/attestations/$(attest digest --rev together | cut -d: -f2)/$workerhash"
2257cat > "$r/hold" <<EOF
2258#!$(command -v bash)
2259[ -e "$r/hold-armed" ] || exit 0
2260rm -f "$r/hold-armed"
2261for _ in \$(seq 600); do
2262 grep -q 'waiting for it to end' "$r/together-second.log" 2> /dev/null && exit 0
2263 sleep 0.1
2264done
2265echo "the second pass never waited for the first" >&2
2266exit 1
2267EOF
2268chmod +x "$r/hold"
2269touch "$r/hold-armed"
2270fresh
2271working together-first VALLEY_E2E_DURING_PASS="$r/hold" &
2272first=$!
2273for _ in $(seq 600); do
2274 grep -q ': building ' "$r/together-first.log" 2> /dev/null && break
2275 sleep 0.1
2276done
2277working together-second
2278wait "$first"
2279said together-second "valley: another pass holds $wstate/lock; waiting for it to end"
2280said together-first "$line: published $togetherref"
2281said together-second "$line: origin holds evidence for every nix check it requires"
2282built code-build
2283pushed "$togetherref"
2284
2285# 21c. A failed batch backs off. Every nix call fails while the stand-in
2286# is told to fail. The pass reports the failure, publishes nothing,
2287# and exits 1. The batch is not built again until its wait, 15
2288# minutes, is over, then it fails again and waits twice as long. Once
2289# it passes it is published, and the record of it is gone.
2290readied flaky docs/readme.md
2291flakyref="refs/the-valley/attestations/$(attest digest --rev flaky | cut -d: -f2)/$workerhash"
2292fresh
2293if working flaky-1 VALLEY_NOW=2026-10-22T10:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2294 echo "valley-request: a pass whose batch failed exited 0" >&2
2295 exit 1
2296fi
2297said flaky-1 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:15:00Z"
2298pushed none
2299jq -e '.projects.project | length == 1 and (.[] | .failures == 1)' "$wstate/failures.json" > /dev/null
2300fresh
2301working flaky-2 VALLEY_NOW=2026-10-22T10:14:59Z
2302said flaky-2 "$line: its batch prose-format failed 1 time in a row, so it is not built again before 2026-10-22T10:15:00Z"
2303built nothing
2304pushed none
2305if working flaky-3 VALLEY_NOW=2026-10-22T10:15:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2306 echo "valley-request: a pass whose batch failed again exited 0" >&2
2307 exit 1
2308fi
2309said flaky-3 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:45:00Z"
2310fresh
2311working flaky-4 VALLEY_NOW=2026-10-22T10:45:00Z
2312said flaky-4 "$line: building prose-format"
2313said flaky-4 "$line: published $flakyref"
2314built prose-format
2315pushed "$flakyref"
2316[ ! -e "$wstate/failures.json" ] || {
2317 echo "valley-request: the record of a batch that passed was kept" >&2
2318 cat "$wstate/failures.json" >&2
2319 exit 1
2320}
2321
2322# 21d. A lost reply. The worker's push is taken by a receive-pack that
2323# exits 1 when it is done, so origin lands the ref and the worker hears
2324# a failure: the pass says so, and exits 1. The next pass finds the
2325# evidence on origin, and builds and pushes nothing. Then a push that
2326# never reaches origin: once its one-minute wait is over, the next
2327# pass pushes the ref the first one stored, the same object, and
2328# builds nothing.
2329cat > "$r/lossy" <<EOF
2330#!$(command -v bash)
2331git-receive-pack "\$@"
2332echo "the connection dropped" >&2
2333exit 1
2334EOF
2335cat > "$r/unreached" <<EOF
2336#!$(command -v bash)
2337echo "the connection dropped" >&2
2338exit 1
2339EOF
2340chmod +x "$r/lossy" "$r/unreached"
2341readied landed docs/readme.md
2342landedref="refs/the-valley/attestations/$(attest digest --rev landed | cut -d: -f2)/$workerhash"
2343if working landed-1 VALLEY_NOW=2026-10-23T09:00:00Z GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/lossy"; then
2344 echo "valley-request: a pass that heard its push fail exited 0" >&2
2345 exit 1
2346fi
2347said landed-1 "$line: could not publish $landedref; it is not pushed again before 2026-10-23T09:01:00Z"
2348git -C "$served" cat-file -e "$landedref:prose-format/statement.note"
2349fresh
2350working landed-2 VALLEY_NOW=2026-10-23T09:00:00Z
2351said landed-2 "$line: origin holds evidence for every nix check it requires"
2352built nothing
2353pushed none
2354readied unreached docs/readme.md
2355unreachedref="refs/the-valley/attestations/$(attest digest --rev unreached | cut -d: -f2)/$workerhash"
2356if working unreached-1 VALLEY_NOW=2026-10-23T09:00:00Z GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/unreached"; then
2357 echo "valley-request: a pass whose push never arrived exited 0" >&2
2358 exit 1
2359fi
2360if git -C "$served" rev-parse --verify --quiet "$unreachedref" > /dev/null; then
2361 echo "valley-request: a push that never arrived landed" >&2
2362 exit 1
2363fi
2364# The clone the worker reads the project through: keyed by its URL, which
2365# is the owning valley's with its last segment replaced (status/list.go).
2366cache="$HOME/.cache/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2367storedobject="$(git -C "$cache" rev-parse "$unreachedref")"
2368fresh
2369working unreached-2 VALLEY_NOW=2026-10-23T09:01:00Z
2370said unreached-2 "$line: an earlier pass stored $unreachedref, and origin does not hold it"
2371said unreached-2 "$line: published $unreachedref"
2372built nothing
2373pushed "$unreachedref"
2374[ "$(git -C "$served" rev-parse "$unreachedref")" = "$storedobject" ]
2375# A tag in the cache clone named like the ref the worker stores a
2376# change's evidence at, while that ref is missing. The worker reads its
2377# own ref by that exact name, so it does not take the tag for evidence an
2378# earlier pass stored: it builds the change and publishes what attest
2379# stored.
2380readied tagged docs/readme.md
2381taggedref="refs/the-valley/attestations/$(attest digest --rev tagged | cut -d: -f2)/$workerhash"
2382git -C "$cache" update-ref "refs/tags/$taggedref" "$storedobject"
2383fresh
2384working tagged VALLEY_NOW=2026-10-23T09:02:00Z && taggedpass=0 || taggedpass=$?
2385if grep -qF "an earlier pass stored $taggedref" "$r/tagged.log"; then
2386 echo "valley-request: the worker took a tag named like its own ref for evidence an earlier pass stored" >&2
2387 cat "$r/tagged.log" >&2
2388 exit 1
2389fi
2390if [ "$taggedpass" != 0 ]; then
2391 echo "valley-request: the pass with a tag named like the worker's own ref exited $taggedpass" >&2
2392 cat "$r/tagged.log" >&2
2393 exit 1
2394fi
2395said tagged "$line: published $taggedref"
2396pushed "$taggedref"
2397
2398# 21e. A head superseded while it is built. A stand-in hook readies a new
2399# version of the change in the middle of the batch. The batch runs to
2400# its end, and the pass publishes the evidence for the head it pinned,
2401# which the hook admits, since it is true of that tree. The pass then
2402# reads the change again and builds the new version, which approve
2403# reuses, and the real integrator lands. The next pass builds nothing.
2404readied superseded docs/readme.md
2405oldline="$line"
2406oldref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash"
2407cat > "$r/supersede" <<EOF
2408#!$(command -v bash)
2409[ -e "$r/supersede-armed" ] || exit 0
2410rm -f "$r/supersede-armed"
2411cd "$proj"
2412echo again >> docs/readme.md
2413git commit --quiet -am 'superseded, again'
2414VALLEY_PRINCIPAL=requester git push --quiet origin superseded
2415VALLEY_PRINCIPAL=requester valley ready --change "$id" superseded > "$r/superseded-v2.ready" 2>&1
2416# Its own pushes are not the worker's.
2417: > "$reusepushes"
2418EOF
2419chmod +x "$r/supersede"
2420touch "$r/supersede-armed"
2421fresh
2422working superseded-1 VALLEY_E2E_DURING_PASS="$r/supersede"
2423grep -qx "project $handle v2 ready" "$r/superseded-v2.ready"
2424said superseded-1 "$oldline: published $oldref"
2425newhead="$(git rev-parse superseded)"
2426newref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash"
2427said superseded-1 "project: $handle v2 at ${newhead:0:7}: published $newref"
2428if [ "$(grep -c '^--- push' "$reusepushes")" != 2 ] || ! grep -q " $oldref\$" "$reusepushes" \
2429 || ! grep -q " $newref\$" "$reusepushes"; then
2430 echo "valley-request: the pass did not publish the superseded version and then the new one" >&2
2431 cat "$reusepushes" "$r/superseded-1.log" >&2
2432 exit 1
2433fi
2434fresh
2435working superseded-2
2436said superseded-2 "project: $handle v2 at ${newhead:0:7}: origin holds evidence for every nix check it requires"
2437built nothing
2438pushed none
2439approving superseded env -u VALLEY_ATTEST_KEY valley approve -v --head "$newhead" "$handle"
2440built nothing
2441[ "$(requested "c-$id")" = "$newhead" ]
2442integrated "c-$id" "$r/worker.projects"
2443
2444# 21f. Origin already holds the worker's own ref for a tree, without one of
2445# the checks the change requires. The worker adds the missing check's
2446# note to that ref, through the protected hook, which takes the ref
2447# with every note it held kept, and approve reuses both and runs
2448# nothing. For a second change with such a ref, no pass runs before
2449# approve: approve without --build-here files the request at once,
2450# naming the missing check, and builds nothing. The real integrator
2451# holds that request as stale for want of evidence, and keeps it.
2452# approve --build-here then builds the rest under the operator's key
2453# and publishes the evidence alone, since the request already stands
2454# at that head. The integrator, judging again because evidence
2455# arrived, lands it on the two signers' evidence.
2456readied worker-extends docs/readme.md src/main.txt
2457extendsref="refs/the-valley/attestations/$(attest digest --rev worker-extends | cut -d: -f2)/$workerhash"
2458attest run --rev worker-extends --key "$r/keys/worker" --name "$workername" --check prose-format > "$r/worker-extends-attest.out" 2>&1
2459git push --quiet origin "$extendsref:$extendsref"
2460fresh
2461working worker-extends
2462said worker-extends "$line: added code-build to $extendsref on origin"
2463built code-build
2464pushed "$extendsref"
2465approving worker-extends env -u VALLEY_ATTEST_KEY valley approve -v --head "$head" "$handle"
2466grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/worker-extends.out"
2467built nothing
2468integrated "c-$id" "$r/worker.projects"
2469readied worker-occupied docs/readme.md src/main.txt
2470occupiedref="refs/the-valley/attestations/$(attest digest --rev worker-occupied | cut -d: -f2)/$workerhash"
2471attest run --rev worker-occupied --key "$r/keys/worker" --name "$workername" --check prose-format > "$r/worker-occupied-attest.out" 2>&1
2472git push --quiet origin "$occupiedref:$occupiedref"
2473approving worker-occupied-filed valley approve -v --head "$head" "$handle"
2474grep -q "^◐ $handle v1 approved · lands when 1 check passes ([0-9]*/[0-9]* passed)$" "$w/worker-occupied-filed.out"
2475grep -qx ' missing code-build' "$w/worker-occupied-filed.out"
2476built nothing
2477if [ "$(requested "c-$id")" != "$head" ] || grep -q ' refs/the-valley/attestations/' "$reusepushes"; then
2478 echo "valley-request: approve without --build-here did not file the request alone" >&2
2479 cat "$w/worker-occupied-filed.out" "$w/worker-occupied-filed.err" "$reusepushes" >&2
2480 exit 1
2481fi
2482integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
2483 --known-signers "$r/known-signers" --signer-projects "$r/worker.projects" --instance "$r/floor" --schema "$schemaFile" \
2484 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
2485 echo "valley-request: the integrator failed" >&2
2486 cat "$r/integrator.out" >&2
2487 exit 1
2488}
2489if ! grep -q "^c-$id -> refs/heads/main @ [0-9a-f]*: stale\$" "$r/integrator.out" \
2490 || ! grep -qE '^ check +code-build +no attestation for this check accompanies the change$' "$r/integrator.out" \
2491 || [ "$(requested "c-$id")" != "$head" ]; then
2492 echo "valley-request: the integrator did not keep a request missing evidence as stale" >&2
2493 cat "$r/integrator.out" >&2
2494 exit 1
2495fi
2496approving worker-occupied valley approve -v --build-here --head "$head" "$handle"
2497built code-build
2498grep -qx ' the request already stood at this head; the evidence is what was published' "$w/worker-occupied.out"
2499if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
2500 || ! grep -q " refs/the-valley/attestations/[0-9a-f]*/$operatorhash\$" "$reusepushes" \
2501 || grep -q ' refs/the-valley/integration-requests/' "$reusepushes"; then
2502 echo "valley-request: approve --build-here over a standing request did not publish the evidence alone" >&2
2503 cat "$reusepushes" >&2
2504 exit 1
2505fi
2506integrated "c-$id" "$r/worker.projects"
2507
2508# 21g. A read of origin that fails stops the pass, before anything is
2509# built: with the project's origin away, the pass exits 1 and says
2510# so, and the ready change is left for a pass that can read it.
2511readied unread-origin docs/readme.md
2512mv "$served" "$served.away"
2513fresh
2514if working unread-origin; then
2515 echo "valley-request: a pass went on past an origin it could not read" >&2
2516 exit 1
2517fi
2518mv "$served.away" "$served"
2519grep -q '^valley: project: .*; the pass stops here$' "$r/unread-origin.log"
2520built nothing
2521fresh
2522working unread-origin-again
2523said unread-origin-again "$line: building prose-format"
2524
2525# await <file> <text>: wait up to a minute for the file to hold the text.
2526await() {
2527 for _ in $(seq 600); do
2528 grep -qF -- "$2" "$1" 2> /dev/null && return
2529 sleep 0.1
2530 done
2531 echo "valley-request: $1 never said: $2" >&2
2532 exit 1
2533}
2534
2535# 21h. The wait after a failed batch runs from when the batch ended. The
2536# clock is not fixed here. The stand-in takes 3 seconds and then
2537# fails, so the batch's wait ends at least 15 minutes after it ended,
2538# which is more than 15 minutes after the pass began. Past the wait,
2539# the batch is built and published.
2540readied slow docs/readme.md
2541slowdigest="$(attest digest --rev slow | cut -d: -f2)"
2542began="$(date -u +%s)"
2543fresh
2544if working slow-1 VALLEY_E2E_DURING_PASS='sleep 3; exit 1'; then
2545 echo "valley-request: a pass whose batch failed exited 0" >&2
2546 exit 1
2547fi
2548retry="$(jq -r --arg key "build $slowdigest prose-format" '.projects.project[$key].retry' "$wstate/failures.json")"
2549if [ "$(($(date -u -d "$retry" +%s) - began))" -lt 903 ]; then
2550 echo "valley-request: the wait after a 3-second batch ends at $retry, less than 15 minutes after it ended; the pass began at $(date -u -d "@$began" +%FT%TZ)" >&2
2551 exit 1
2552fi
2553fresh
2554working slow-2 VALLEY_NOW=2099-01-01T00:00:00Z
2555said slow-2 "$line: published refs/the-valley/attestations/$slowdigest/$workerhash"
2556
2557# 21i. A failed push waits too, by the object it would publish. Two changes
2558# whose heads hold the same tree share one attestation ref, and origin
2559# takes no push. The first pass builds once and tries one push. It
2560# then reads both changes again, and each one's push waits behind that
2561# failure. A pass within the minute tries no push. The next failure doubles the wait. Past it, the
2562# ref is published once, and the record of it is gone.
2563cat > "$r/counted" <<EOF
2564#!$(command -v bash)
2565echo push >> "$r/push-attempts"
2566echo "the connection dropped" >&2
2567exit 1
2568EOF
2569chmod +x "$r/counted"
2570refusing=(GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/counted")
2571readied twin-a docs/readme.md
2572git checkout --quiet -B twin-b origin/main
2573git checkout --quiet twin-a -- docs/readme.md
2574git commit --quiet -m twin-b
2575VALLEY_PRINCIPAL=requester git push --quiet origin twin-b
2576VALLEY_PRINCIPAL=requester valley ready twin-b > "$r/twin-b.ready" 2>&1
2577[ "$(git rev-parse 'twin-a^{tree}')" = "$(git rev-parse 'twin-b^{tree}')" ]
2578twinref="refs/the-valley/attestations/$(attest digest --rev twin-a | cut -d: -f2)/$workerhash"
2579# attempts <n> <name> <failed> <waiting>: origin was offered n pushes in
2580# all, and the pass's log says failed and waiting for the twins' ref that
2581# many times each, with the wait ending at the time given last.
2582attempts() {
2583 local until="$5"
2584 if [ "$(wc -l < "$r/push-attempts")" != "$1" ] \
2585 || [ "$(grep -c ": could not publish $twinref; it is not pushed again before $until\$" "$r/$2.log")" != "$3" ] \
2586 || [ "$(grep -c ": publishing $twinref failed [0-9]* times\? in a row, so it is not pushed again before $until\$" "$r/$2.log")" != "$4" ]; then
2587 echo "valley-request: the pass $2 did not offer origin the twins' ref as often as expected" >&2
2588 cat "$r/push-attempts" "$r/$2.log" >&2
2589 exit 1
2590 fi
2591}
2592: > "$r/push-attempts"
2593fresh
2594if working twins-1 VALLEY_NOW=2026-10-24T09:00:00Z "${refusing[@]}"; then
2595 echo "valley-request: a pass whose push failed exited 0" >&2
2596 exit 1
2597fi
2598built prose-format
2599attempts 1 twins-1 1 2 2026-10-24T09:01:00Z
2600working twins-2 VALLEY_NOW=2026-10-24T09:00:59Z "${refusing[@]}"
2601attempts 1 twins-2 0 2 2026-10-24T09:01:00Z
2602if working twins-3 VALLEY_NOW=2026-10-24T09:01:00Z "${refusing[@]}"; then
2603 echo "valley-request: a pass whose push failed again exited 0" >&2
2604 exit 1
2605fi
2606attempts 2 twins-3 1 1 2026-10-24T09:03:00Z
2607fresh
2608working twins-4 VALLEY_NOW=2026-10-24T09:03:00Z
2609built nothing
2610pushed "$twinref"
2611[ ! -e "$wstate/failures.json" ] || {
2612 echo "valley-request: the record of a push that went through was kept" >&2
2613 cat "$wstate/failures.json" >&2
2614 exit 1
2615}
2616
2617# 21j. A pass forgets only the failures of the projects it read whole. The
2618# host serves a second project, other, with no ready change. A batch of
2619# this project fails. A pass over other alone leaves that wait as it
2620# is, so the next pass over this project does not build the batch.
2621git init --quiet --bare "$r/other.git"
2622git -C "$instance" update-ref refs/the-valley/projects \
2623 "$(printf 'other\nproject\n' | git -C "$instance" hash-object -w --stdin)"
2624readied flapping docs/readme.md
2625flappingref="refs/the-valley/attestations/$(attest digest --rev flapping | cut -d: -f2)/$workerhash"
2626if working flapping-1 VALLEY_NOW=2026-10-25T09:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2627 echo "valley-request: a pass whose batch failed exited 0" >&2
2628 exit 1
2629fi
2630(cd "$r/worker-home" && VALLEY_PRINCIPAL=worker VALLEY_NOW=2026-10-25T09:01:00Z valley-worker --repo other \
2631 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/other.log" 2>&1 || {
2632 echo "valley-request: a pass over other failed" >&2
2633 cat "$r/other.log" >&2
2634 exit 1
2635}
2636fresh
2637working flapping-2 VALLEY_NOW=2026-10-25T09:02:00Z
2638said flapping-2 "$line: its batch prose-format failed 1 time in a row, so it is not built again before 2026-10-25T09:15:00Z"
2639built nothing
2640fresh
2641working flapping-3 VALLEY_NOW=2026-10-25T09:15:00Z
2642said flapping-3 "$line: published $flappingref"
2643
2644# 21k. A pass killed while attest runs. attest holds the lock it was
2645# handed, so a second pass waits for attest to end rather than build
2646# the same batch beside it. Then the second pass finds the ref the
2647# orphaned attest stored, publishes it, and builds nothing.
2648readied killed docs/readme.md
2649killedref="refs/the-valley/attestations/$(attest digest --rev killed | cut -d: -f2)/$workerhash"
2650cat > "$r/hold-kill" <<EOF
2651#!$(command -v bash)
2652[ -e "$r/kill-armed" ] || exit 0
2653rm -f "$r/kill-armed"
2654# The worker is the nearest process above this one named valley.
2655p=\$\$
2656while [ "\$p" -gt 1 ]; do
2657 if [ "\$(cat "/proc/\$p/comm")" = valley ]; then
2658 echo "\$p" > "$r/killed.pid"
2659 break
2660 fi
2661 read -r _ _ _ p _ < "/proc/\$p/stat"
2662done
2663for _ in \$(seq 600); do
2664 [ -e "$r/release" ] && exit 0
2665 sleep 0.1
2666done
2667exit 1
2668EOF
2669chmod +x "$r/hold-kill"
2670touch "$r/kill-armed"
2671fresh
2672working killed-1 VALLEY_E2E_DURING_PASS="$r/hold-kill" &
2673killedpass=$!
2674await "$r/killed.pid" ""
2675kill -9 "$(cat "$r/killed.pid")"
2676status=0
2677wait "$killedpass" || status=$?
2678[ "$status" = 137 ] || {
2679 echo "valley-request: the killed pass ended with $status" >&2
2680 exit 1
2681}
2682working killed-2 &
2683secondpass=$!
2684await "$r/killed-2.log" "valley: another pass holds $wstate/lock; waiting for it to end"
2685touch "$r/release"
2686wait "$secondpass"
2687said killed-2 "$line: an earlier pass stored $killedref, and origin does not hold it"
2688said killed-2 "$line: published $killedref"
2689built prose-format
2690pushed "$killedref"
2691
2692# 21l. A cache clone whose making was interrupted. A stand-in for git takes
2693# the clone's config.lock as it sets the origin, kills the worker, and
2694# dies holding the lock, as git killed mid-write does. The next pass
2695# makes the clone again and reads the project through it. The worker
2696# runs as the raw binary here, since the package's wrapper puts its
2697# own git ahead of the stand-in.
2698cachehome="$r/interrupted-cache"
2699interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2700mkdir -p "$r/killing-git"
2701cat > "$r/killing-git/git" <<EOF
2702#!$(command -v bash)
2703if [ "\${1:-} \${2:-}" = "remote add" ]; then
2704 : > .git/config.lock
2705 kill -9 "\$PPID"
2706 kill -9 "\$\$"
2707fi
2708exec "$(command -v git)" "\$@"
2709EOF
2710chmod +x "$r/killing-git/git"
2711status=0
2712(cd "$r/worker-home" && VALLEY_PRINCIPAL=worker XDG_CACHE_HOME="$cachehome" PATH="$r/killing-git:$PATH" \
2713 VALLEY_VERIFICATION_SCHEMA="$schemaFile" "$valleyUnwrapped" work --repo project \
2714 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/interrupted-1.log" 2>&1 || status=$?
2715if [ "$status" != 137 ] || [ -z "$(find "$cachehome" -name config.lock)" ]; then
2716 echo "valley-request: the stand-in did not interrupt the clone's making while holding its config.lock ($status)" >&2
2717 cat "$r/interrupted-1.log" >&2
2718 exit 1
2719fi
2720# recovered <name>: a pass over the cache at $cachehome succeeds, and the
2721# clone it reads the project through names origin.
2722recovered() {
2723 fresh
2724 working "$1" XDG_CACHE_HOME="$cachehome" || {
2725 echo "valley-request: the pass after an interrupted clone failed" >&2
2726 cat "$r/$1.log" >&2
2727 exit 1
2728 }
2729 [ "$(git -C "$interrupted" config --get remote.origin.url)" = "$served" ]
2730 said "$1" "$livelyline: requires no nix check"
2731 built nothing
2732}
2733recovered interrupted-2
2734
2735# 21m. A clone left half made in its place, as an older version left one:
2736# a repository with no origin, and a stale config.lock beside its
2737# configuration. The next pass sets it aside, makes the clone again,
2738# and reads the project through it.
2739cachehome="$r/half-made-cache"
2740interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2741git init --quiet "$interrupted"
2742: > "$interrupted/.git/config.lock"
2743recovered half-made
2744[ ! -e "$interrupted/.git/config.lock" ]
2745
2746# 21n. A check request lands nothing, and its evidence serves the change
2747# readied later at the same head. The author of a draft files a
2748# request for its head. With no ready change to build for, the worker
2749# builds the nix checks the head requires, as it builds a ready
2750# head's, and publishes them under its key, and the integrator lands
2751# nothing. Readied again, the change finds that evidence on origin:
2752# the worker builds nothing, approve reuses it and runs nothing, and
2753# the integrator lands it.
2754readied asked docs/readme.md
2755VALLEY_PRINCIPAL=requester valley unready "$id" > "$r/asked.unready" 2>&1
2756askedref="refs/the-valley/attestations/$(attest digest --rev asked | cut -d: -f2)/$workerhash"
2757request=refs/the-valley/check-requests/requester/0123456789abcdef/0000000000000001
2758VALLEY_PRINCIPAL=requester git push --quiet origin "$head:$request"
2759fresh
2760working asked-1
2761said asked-1 "project: request requester/0123456789abcdef/0000000000000001 at ${head:0:7}: building prose-format"
2762said asked-1 "project: request requester/0123456789abcdef/0000000000000001 at ${head:0:7}: published $askedref"
2763built prose-format
2764pushed "$askedref"
2765main="$(git -C "$served" rev-parse main)"
2766integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
2767 --known-signers "$r/known-signers" --signer-projects "$r/worker.projects" --instance "$r/floor" --schema "$schemaFile" \
2768 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/asked.integrator" 2>&1
2769[ "$(git -C "$served" rev-parse main)" = "$main" ]
2770VALLEY_PRINCIPAL=requester valley ready --change "$id" asked > "$r/asked.ready" 2>&1
2771fresh
2772working asked-2
2773said asked-2 "$line: origin holds evidence for every nix check it requires"
2774built nothing
2775pushed none
2776approving asked env -u VALLEY_ATTEST_KEY valley approve -v --head "$head" "$handle"
2777grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/asked.out"
2778built nothing
2779integrated "c-$id" "$r/worker.projects"
2780VALLEY_PRINCIPAL=requester git push --quiet origin ":$request"
2781
2782worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
2783if [ "$worktrees" != 1 ]; then
2784 echo "valley-request: a throwaway worktree was left behind in the reuse project" >&2
2785 git worktree list >&2
2786 exit 1
2787fi
2788
2789# ----------------------------------------------------------------------
2790# 22. approve reads each ref by its exact name. Given a ref name that is
2791# missing, git goes on to try other names for it, refs/tags/<name>
2792# and refs/heads/<name> among them. ls-remote and fetch take a ref
2793# whose name ends in the one asked for. Any pusher can make such a
2794# ref, and a fetch brings a tag along with the commits it names. Each
2795# case leaves a ref missing, makes the ref git would take for it, and
2796# shows that approve treats the ref as missing. Every branch here
2797# touches quiet/ alone, so nothing is required and no key is needed.
2798# The refs are read here with show-ref, which reads that exact name.
2799lookorigin="$TMPDIR/lookalike.git"
2800git init --quiet --bare "$lookorigin"
2801look="$TMPDIR/lookalike"
2802git init --quiet "$look"
2803cd "$look" || exit 1
2804git remote add origin "$lookorigin"
2805mkdir -p docs policy
2806echo "the readme" > docs/readme.md
2807echo "$valley" > policy/valley
2808git add -A
2809git commit --quiet -m base
2810git push --quiet origin main
2811lookbase="$(git rev-parse main)"
2812quiet_branch() {
2813 git checkout --quiet -b "topic/$1" "$2"
2814 mkdir -p quiet
2815 echo "a quiet note for $1" > quiet/note.md
2816 git add -A
2817 git commit --quiet -m "a quiet note for $1"
2818 git push --quiet origin "topic/$1"
2819 git checkout --quiet main
2820}
2821# The request ref approve filed on lookorigin, or nothing.
2822filed() {
2823 git -C "$lookorigin" show-ref --verify --hash \
2824 "refs/the-valley/integration-requests/main/topic-$1" 2> /dev/null || true
2825}
2826
2827# A request ref that is missing, with a branch on origin named like it at
2828# the head approved. Read through that branch, the request would already
2829# stand at this head, and approve would report success and file nothing.
2830# git push matches its destination the same way, so a push to the
2831# request ref would write that branch, and approve refuses to push at
2832# all while it stands. Once it is deleted, approve files the request.
2833quiet_branch lookalike main
2834lookhead="$(git rev-parse topic/lookalike)"
2835lookref=refs/heads/refs/the-valley/integration-requests/main/topic-lookalike
2836git -C "$lookorigin" update-ref "$lookref" "$lookhead"
2837if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/lookalike < /dev/null \
2838 > "$w/lookalike.out" 2> "$w/lookalike.err"; then
2839 echo "valley-request: approve reported success with a branch named like the request it files" >&2
2840 cat "$w/lookalike.out" >&2
2841 exit 1
2842fi
2843grep -qF "⊘ topic/lookalike not approved · origin holds $lookref, " "$w/lookalike.err"
2844grep -qxF " delete $lookref on origin" "$w/lookalike.err"
2845[ -z "$(filed lookalike)" ] || {
2846 echo "valley-request: approve filed a request past a branch named like it" >&2
2847 exit 1
2848}
2849git -C "$lookorigin" update-ref -d "$lookref"
2850env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/lookalike < /dev/null \
2851 > "$w/lookalike-again.out" 2> "$w/lookalike-again.err"
2852if [ "$(filed lookalike)" != "$lookhead" ]; then
2853 echo "valley-request: with the branch named like it gone, approve filed no request" >&2
2854 cat "$w/lookalike-again.out" "$w/lookalike-again.err" >&2
2855 exit 1
2856fi
2857
2858# A branch that is not on origin, with a tag origin carries named like its
2859# remote-tracking ref. The fetch brings the tag, and read through it the
2860# branch would be approved at the tag's commit.
2861git -C "$lookorigin" update-ref refs/tags/refs/remotes/origin/topic/gone "$lookhead"
2862if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/gone < /dev/null \
2863 > "$w/gone.out" 2> "$w/gone.err"; then
2864 echo "valley-request: approve filed a branch origin does not have, at a tag named like it" >&2
2865 exit 1
2866fi
2867# The verb refuses it itself, before approve reads the branch again.
2868grep -qx 'valley: no such branch on origin: topic/gone' "$w/gone.err"
2869git show-ref --verify --quiet refs/tags/refs/remotes/origin/topic/gone || {
2870 echo "valley-request: the fetch did not bring the tag, so nothing was tested" >&2
2871 exit 1
2872}
2873[ -z "$(filed gone)" ] || {
2874 echo "valley-request: approve filed a request for a branch origin does not have" >&2
2875 exit 1
2876}
2877if valley review topic/gone < /dev/null > "$w/gone-review.out" 2> "$w/gone-review.err"; then
2878 echo "valley-request: review went on with a branch origin does not have" >&2
2879 exit 1
2880fi
2881# review refuses it before it reads the stack, and says nothing else.
2882if [ "$(cat "$w/gone-review.err")" != 'valley: no such branch on origin: topic/gone' ]; then
2883 echo "valley-request: review read a branch origin does not have before refusing it:" >&2
2884 cat "$w/gone-review.err" >&2
2885 exit 1
2886fi
2887
2888# An owning valley with no main, and a branch on it named
2889# refs/heads/main, which git stores as refs/heads/refs/heads/main. Fetched
2890# through that branch, the floor would come from it.
2891lookvalley="$TMPDIR/lookalike-valley.git"
2892git init --quiet --bare "$lookvalley"
2893git -C "$valleywork" push --quiet "$lookvalley" main:refs/heads/refs/heads/main
2894echo "$lookvalley" > policy/valley
2895git commit --quiet -am "a valley with no main"
2896git push --quiet origin main
2897quiet_branch floorless main
2898if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/floorless < /dev/null \
2899 > "$w/floorless.out" 2> "$w/floorless.err"; then
2900 echo "valley-request: approve read a floor from a valley with no main" >&2
2901 exit 1
2902fi
2903grep -qF "the owning valley $lookvalley has no refs/heads/main" "$w/floorless.err"
2904[ -z "$(filed floorless)" ] || {
2905 echo "valley-request: approve filed a request under a valley with no main" >&2
2906 exit 1
2907}
2908
2909# origin with no main, and a tag origin carries named like main's
2910# remote-tracking ref, at a commit whose floor requires nothing of this
2911# branch. Read through the tag, approve would derive the policy from it
2912# and file the request.
2913quiet_branch mainless "$lookbase"
2914git -C "$lookorigin" update-ref refs/tags/refs/remotes/origin/main "$lookbase"
2915git -C "$lookorigin" update-ref -d refs/heads/main
2916if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/mainless < /dev/null \
2917 > "$w/mainless.out" 2> "$w/mainless.err"; then
2918 echo "valley-request: approve filed a request while origin has no main" >&2
2919 exit 1
2920fi
2921grep -qx '⊘ topic/mainless not approved · origin has no main to approve onto' "$w/mainless.err"
2922git show-ref --verify --quiet refs/tags/refs/remotes/origin/main || {
2923 echo "valley-request: the fetch did not bring the tag, so nothing was tested" >&2
2924 exit 1
2925}
2926[ -z "$(filed mainless)" ] || {
2927 echo "valley-request: approve filed a request while origin has no main" >&2
2928 exit 1
2929}
2930cd "$repo" || exit 1
2931
2932# ----------------------------------------------------------------------
2933# 23. review's [a]pprove runs approve on review's terminal:
2934# a plain branch approved is filed at the head review showed, and a
2935# failed check is said at the prompt. A ctrl-C during the checks stops them,
2936# removes their worktree, records the attempt's end as interrupted and
2937# exits 130, from the approve verb and from review alike. The check
2938# that holds until it is stopped is main's for held/, which nothing
2939# after this scenario touches.
2940git fetch --quiet origin
2941for branch in walked leaked; do
2942 git checkout --quiet -b "topic/$branch" origin/main
2943 echo "a readme reviewed in a walk, $branch" > docs/readme.md
2944 [ "$branch" != leaked ] || { mkdir -p src && echo SECRET > src/leak.txt; }
2945 git add -A
2946 git commit --quiet -m "a readme reviewed in a walk, $branch"
2947 git push --quiet origin "topic/$branch"
2948done
2949git checkout --quiet main
2950printf 'a\n' > "$w/answers"
2951valley review topic/walked < "$w/answers" > "$w/walked.out" 2> "$w/walked.err"
2952walked="$(git -C "$origin" show-ref --verify --hash refs/the-valley/integration-requests/main/topic-walked || true)"
2953if [ "$walked" != "$(git rev-parse topic/walked)" ]; then
2954 echo "valley-request: review's approval of a plain branch filed $walked, not its head" >&2
2955 exit 1
2956fi
2957printf 'a\ns\n' > "$w/answers"
2958valley review topic/leaked < "$w/answers" > "$w/leaked.out" 2> "$w/leaked.err"
2959grep -qxF "topic/leaked · checks failed at $(git rev-parse --short topic/leaked)" "$w/leaked.out"
2960grep -qF 'skipped topic/leaked; nothing done' "$w/leaked.out"
2961
2962held="$TMPDIR/held"
2963git checkout --quiet -b held-policy origin/main
2964printf '\nproject: {\n\tchecks: hold: {\n\t\trunner: "command"\n\t\tcommand: "%s"\n\t}\n\tclasses: held: {\n\t\tpaths: "held/**": true\n\t\trequires: hold: true\n\t}\n}\n' \
2965 "touch $held && sleep 600" >> policy/project/policy.cue
2966git commit --quiet -am "a check that holds until it is stopped"
2967git push --quiet origin HEAD:main
2968git checkout --quiet -b topic/held
2969mkdir held
2970echo "held" > held/readme.md
2971git add held
2972git commit --quiet -m "a change the held check gates"
2973git push --quiet origin topic/held
2974git checkout --quiet main
2975
2976# ctrl_c <name> <stdin> <command…>: run the command as a terminal runs a
2977# foreground job, in a process group of its own, and once the held check
2978# has started send the group SIGINT, as ctrl-C does. The command's status
2979# is left in stopped.
2980ctrl_c() {
2981 local name="$1" input="$2" pid
2982 shift 2
2983 rm -f "$held"
2984 setsid "$@" < "$input" > "$w/$name.out" 2> "$w/$name.err" &
2985 pid=$!
2986 until [ -e "$held" ] || ! kill -0 "$pid" 2> /dev/null; do sleep 0.1; done
2987 kill -INT -- "-$pid" 2> /dev/null || true
2988 stopped=0
2989 wait "$pid" || stopped=$?
2990}
2991# left_behind <who>: nothing of the interrupted approval is left: no
2992# worktree, no scratch directory, no request.
2993left_behind() {
2994 local worktrees scratch=("$TMPDIR"/valley-approve-*)
2995 worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
2996 if [ "$worktrees" != 1 ] || [ -e "${scratch[0]}" ]; then
2997 echo "valley-request: the checks' worktree outlived a ctrl-C ($1)" >&2
2998 git worktree list >&2
2999 exit 1
3000 fi
3001 if git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/topic-held > /dev/null; then
3002 echo "valley-request: an approval interrupted during its checks filed a request ($1)" >&2
3003 exit 1
3004 fi
3005}
3006ctrl_c held-verb /dev/null env XDG_STATE_HOME="$w/held-state" GIT_CONFIG_COUNT=2 \
3007 GIT_CONFIG_KEY_0=valley.telemetry GIT_CONFIG_VALUE_0=true \
3008 GIT_CONFIG_KEY_1=valley.telemetry.remote GIT_CONFIG_VALUE_1=ssh://valley-host.invalid/project \
3009 valley approve -v origin/topic/held
3010[ "$stopped" = 130 ] || {
3011 echo "valley-request: the approve verb exited $stopped after a ctrl-C during its checks" >&2
3012 cat "$w/held-verb.out" "$w/held-verb.err" >&2
3013 exit 1
3014}
3015left_behind "the approve verb"
3016grep -rh '"type":"approve.end"' "$w/held-state" | grep -qF '"result":"interrupted"' || {
3017 echo "valley-request: an approval interrupted during its checks recorded no interrupted end" >&2
3018 exit 1
3019}
3020printf 'a\n' > "$w/answers"
3021mkdir "$w/held-tmp"
3022ctrl_c held-review "$w/answers" env TMPDIR="$w/held-tmp" valley review topic/held
3023[ "$stopped" = 130 ] || {
3024 echo "valley-request: review exited $stopped after a ctrl-C during its checks" >&2
3025 exit 1
3026}
3027left_behind review
3028# Nor is anything of review's own: its pages, the approval's scratch, and
3029# attest run's tree.
3030if ls -A "$w/held-tmp" | grep .; then
3031 echo "valley-request: review left the above in its TMPDIR after a ctrl-C during its checks" >&2
3032 exit 1
3033fi
3034
3035# ----------------------------------------------------------------------
3036# The verbs the loop offers are the whole of what an operator can do to a
3037# branch, so no path may offer one outside the set: [a]pprove, [r]eject,
3038# [s]kip, and [v]iew full diff, which only shows. A change alone is never
3039# offered [b]ase. Every stream this check captured is scanned at the end,
3040# which is what makes the claim about paths rather than about the
3041# prompts pinned character for character above — the fast-forward prompt,
3042# the one under the behind-main line, and the ones a refused [a]pprove
3043# returns to are all in here. A verb that writes the protected ref from the
3044# operator's own checkout — the retired [i]ntegrate, or any successor to
3045# it — fails this wherever it is printed.
3046offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
3047outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]pprove|\[r\]eject|\[s\]kip|\[v\]iew' || true)"
3048if [ -n "$outside" ]; then
3049 echo "valley-request: a review path offered a verb outside the set:" >&2
3050 printf '%s\n' "$outside" >&2
3051 exit 1
3052fi
3053# The scan is only worth something if it saw the offers at all.
3054for verb in '[a]pprove' '[r]eject' '[s]kip' '[v]iew'; do
3055 printf '%s\n' "$offered" | grep -qxF "$verb" \
3056 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
3057done
3058
3059# No throwaway worktree outlived any of it: only the checkout is left.
3060worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
3061if [ "$worktrees" != 1 ]; then
3062 echo "valley-request: a throwaway worktree was left behind" >&2
3063 git worktree list >&2
3064 exit 1
3065fi
3066
3067echo "every scenario held"
3068{
3069 cat "$w/approve.out"
3070 printf '\n--- the scratch origin, after all of it ---\n'
3071 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
3072} > "$out"