gunk-dev/the-valley

valley-request.sh

source57b716740065main's published tip history
138.2 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 bin/valley run straight from the checkout does
119# (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_WALK_OUTCOME="$w/posthoc.outcome" valley review -v topic/ten < "$w/answers" \
993 > "$w/posthoc.out" 2> "$w/posthoc.err"
994disarm_race
995
996grep -qx '? topic/ten approved · request consumed meanwhile · see: valley status topic/ten' "$w/posthoc.out"
997# In a walk, a request whose state approve could not establish is a
998# refusal of the step, as review takes it from approve's outcome.
999[ "$(cat "$w/posthoc.outcome")" = refused ] || {
1000 echo "valley-request: review took a request in an unknown state as $(cat "$w/posthoc.outcome")" >&2
1001 exit 1
1002}
1003# The claim the old code would have made, from a read that had gone out of
1004# date under it.
1005if grep -q 'the request already stood at this head' "$w/posthoc.out"; then
1006 echo "valley-request: a consumed request was reported as still standing" >&2
1007 cat "$w/posthoc.out" >&2
1008 exit 1
1009fi
1010grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out"
1011
1012# The evidence went, because that push succeeded; the request did not come
1013# back, because nothing here writes it.
1014git -C "$origin" rev-parse --verify --quiet \
1015 "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || {
1016 echo "valley-request: the evidence push was reported as done and is not on origin" >&2
1017 exit 1
1018}
1019if git -C "$origin" rev-parse --verify --quiet \
1020 refs/the-valley/integration-requests/main/topic-ten > /dev/null; then
1021 echo "valley-request: the consumed request was re-filed behind the operator" >&2
1022 exit 1
1023fi
1024# And that evidence went in one push, which is still the whole claim.
1025pushes="$(grep -c '^--- push' "$pushlog" || true)"
1026if [ "$pushes" != 1 ]; then
1027 echo "valley-request: the same-head re-approval took $pushes pushes, not 1" >&2
1028 cat "$pushlog" >&2
1029 exit 1
1030fi
1031git checkout --quiet main
1032
1033# ----------------------------------------------------------------------
1034# 16. A project other than the-valley carries no schema/. The schema ships
1035# with the tool, so the packaged CLI derives what a change requires
1036# and files the request from a checkout that has none. This is the case
1037# that failed in live use: [a]pprove in a valley's config repository, which
1038# carries its own floor at policy/instance, refused with "no policy
1039# schema" and published nothing.
1040cfgorigin="$TMPDIR/config.git"
1041git init --quiet --bare "$cfgorigin"
1042cfg="$TMPDIR/config"
1043git init --quiet "$cfg"
1044cd "$cfg" || exit 1
1045git remote add origin "$cfgorigin"
1046mkdir -p docs policy/instance
1047cp "$valleywork/policy/instance/floor.cue" policy/instance/floor.cue
1048echo "the readme" > docs/readme.md
1049git add -A
1050git commit --quiet -m base
1051git push --quiet -u origin main
1052git checkout --quiet -b topic/config
1053echo "a config readme" > docs/readme.md
1054git commit --quiet -am "a config readme"
1055git push --quiet origin topic/config
1056cfgdigest="$(attest digest --rev topic/config | cut -d: -f2)"
1057
1058printf 'a\n' > "$w/answers"
1059valley review -v topic/config < "$w/answers" > "$w/config.out" 2> "$w/config.err"
1060if grep -q 'no policy schema' "$w/config.err"; then
1061 echo "valley-request: [a]pprove looked for the schema in a project's checkout" >&2
1062 cat "$w/config.err" >&2
1063 exit 1
1064fi
1065grep -q '^◐ topic/config checking 1 check: tree-ok$' "$w/config.out"
1066grep -q '^✓ topic/config approved · ' "$w/config.out"
1067git -C "$cfgorigin" rev-parse --verify --quiet \
1068 "refs/the-valley/attestations/$cfgdigest/$keyhash" > /dev/null || {
1069 echo "valley-request: [a]pprove in a project without schema/ published no evidence" >&2
1070 exit 1
1071}
1072if [ "$(git -C "$cfgorigin" rev-parse refs/the-valley/integration-requests/main/topic-config)" \
1073 != "$(git rev-parse topic/config)" ]; then
1074 echo "valley-request: [a]pprove in a project without schema/ filed no request for its head" >&2
1075 exit 1
1076fi
1077
1078# checks without --schema reads the packaged schema too, and derives what
1079# [a]pprove ran from this repository's own floor.
1080valley checks > "$w/config-checks.out" 2> "$w/config-checks.err"
1081grep -qF '(this repository is its own valley)' "$w/config-checks.out"
1082grep -qE '^ tree-ok +mandatory +prose$' "$w/config-checks.out"
1083
1084# The packaged schema is a default and not a pin. A schema named in the
1085# environment is the one read, and --schema overrides both.
1086if VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks \
1087 > "$w/envschema.out" 2> "$w/envschema.err"; then
1088 echo "valley-request: VALLEY_VERIFICATION_SCHEMA did not replace the packaged schema" >&2
1089 exit 1
1090fi
1091grep -qF "no policy schema at $TMPDIR/no-such-schema.cue" "$w/envschema.err"
1092VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks --schema "$schemaFile" \
1093 > "$w/flagschema.out" 2> "$w/flagschema.err"
1094grep -qE '^ tree-ok +mandatory +prose$' "$w/flagschema.out"
1095
1096# bin/valley run straight from a checkout, with nothing setting the
1097# variable, reads schema/verification.cue in that checkout. the-valley's own
1098# tree carries one, as the first scratch project does, and every other
1099# project's tree does not.
1100if env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks \
1101 > "$w/bare-config.out" 2> "$w/bare-config.err"; then
1102 echo "valley-request: bin/valley run bare found a schema in a checkout without one" >&2
1103 exit 1
1104fi
1105grep -qF "no policy schema at $(git rev-parse --show-toplevel)/schema/verification.cue" \
1106 "$w/bare-config.err"
1107cd "$repo" || exit 1
1108env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "$first" topic/one \
1109 > "$w/bare.out" 2> "$w/bare.err"
1110grep -qE '^ tree-ok +mandatory +prose$' "$w/bare.out"
1111
1112# Every git call the default derivation makes fails closed: finding the
1113# target, reading both layers out of its tip, reading the pointer,
1114# fetching the owning valley, and the diff. fail-nth fails them one at a
1115# time, and each run must end non-zero, say so, and print nothing on
1116# stdout. The layer reads matter most here: a listing of policy/project
1117# that failed, read as an empty directory, would derive under the floor
1118# alone, and a read of origin/main that failed, read as no such ref,
1119# would derive from the local main.
1120shims="$TMPDIR/shims"
1121mkdir -p "$shims"
1122ln -s "$failNth" "$shims/git"
1123shimmed_checks() {
1124 echo 0 > "$TMPDIR/shim-count"
1125 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
1126 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
1127 PATH="$shims:$PATH" bash "$cliScript" checks "$first" topic/one
1128}
1129shimmed_checks 0 > "$w/shim-want.out"
1130grep -qE '^ tree-ok +mandatory +prose$' "$w/shim-want.out"
1131calls="$(cat "$TMPDIR/shim-count")"
1132[ "$calls" -gt 0 ] || {
1133 echo "valley-request: the derivation never called git, so failing it proves nothing" >&2
1134 exit 1
1135}
1136for ((at = 1; at <= calls; at++)); do
1137 if shimmed_checks "$at" > "$w/shim.out" 2> "$w/shim.err" || [ -s "$w/shim.out" ] \
1138 || ! grep -q '^valley: ' "$w/shim.err"; then
1139 echo "valley-request: git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
1140 cat "$w/shim.out" "$w/shim.err" >&2
1141 exit 1
1142 fi
1143done
1144
1145# The same, for the derivation that resolves its own base: `merge-base` is
1146# a call the explicit-base runs above never make, and --names is a second
1147# way of printing what the derivation found. A run is shimmed for each
1148# call, with the arguments the caller gives.
1149defaulted_checks() {
1150 echo 0 > "$TMPDIR/shim-count"
1151 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
1152 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
1153 PATH="$shims:$PATH" bash "$cliScript" checks "${@:2}"
1154}
1155git checkout --quiet topic/one
1156for mode in report names; do
1157 margs=()
1158 [ "$mode" != names ] || margs=(--names)
1159 defaulted_checks 0 "${margs[@]}" > "$w/dshim-want.out"
1160 [ -s "$w/dshim-want.out" ] || {
1161 echo "valley-request: default resolution ($mode) printed nothing" >&2
1162 exit 1
1163 }
1164 calls="$(cat "$TMPDIR/shim-count")"
1165 sawmb=0
1166 for ((at = 1; at <= calls; at++)); do
1167 if defaulted_checks "$at" "${margs[@]}" > "$w/dshim.out" 2> "$w/dshim.err" \
1168 || [ -s "$w/dshim.out" ] || ! grep -q '^valley: ' "$w/dshim.err"; then
1169 echo "valley-request: default resolution ($mode): git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
1170 cat "$w/dshim.out" "$w/dshim.err" >&2
1171 exit 1
1172 fi
1173 done
1174done
1175git checkout --quiet -
1176
1177# A write that fails is a failure of the derivation, not a shorter list:
1178# with no file allowed to grow, every intermediate file the derivation
1179# writes fails, and nothing may print. The pipe keeps the refusal readable,
1180# since a file stream would be cut off too.
1181for mode in report names; do
1182 margs=()
1183 [ "$mode" != names ] || margs=(--names)
1184 if wrote="$(
1185 ulimit -f 0
1186 trap '' XFSZ
1187 env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "${margs[@]}" "$first" topic/one 2> /dev/null
1188 )" || [ -n "$wrote" ]; then
1189 echo "valley-request: a derivation with unwritable scratch files ($mode) did not fail cleanly" >&2
1190 printf '%s\n' "$wrote" >&2
1191 exit 1
1192 fi
1193done
1194
1195# ----------------------------------------------------------------------
1196# 17. The approve verb: [a]pprove without the pager, for a front end that
1197# has shown the change its own way. It runs the function the key
1198# runs, so it publishes what the key's session in 1 published, the
1199# evidence and the request in one push, and prints what that session
1200# printed. stdin is closed, because no answer is read, and the change
1201# is named the way status accepts it, with origin/ in front.
1202git checkout --quiet -b topic/verb origin/main
1203echo "a readme approved without the pager" > docs/readme.md
1204git commit --quiet -am "a readme approved without the pager"
1205git push --quiet origin topic/verb
1206verbhead="$(git rev-parse topic/verb)"
1207verbdigest="$(attest digest --rev topic/verb | cut -d: -f2)"
1208git checkout --quiet main
1209
1210: > "$pushlog"
1211valley approve -v origin/topic/verb < /dev/null > "$w/verb.out" 2> "$w/verb.err"
1212grep -q '◐ topic/verb checking 1 check: tree-ok' "$w/verb.out"
1213grep -qx '✓ topic/verb approved · 1 check passed in [0-9]*s · landing on main' "$w/verb.out"
1214grep -qx " request refs/the-valley/integration-requests/main/topic-verb -> $(git rev-parse --short "$verbhead")" "$w/verb.out"
1215grep -qx " evidence refs/the-valley/attestations/$verbdigest/$keyhash" "$w/verb.out"
1216grep -qx ' checks tree-ok' "$w/verb.out"
1217if grep -q '\[[a-z]\]' "$w/verb.out"; then
1218 echo "valley-request: the approve verb offered a prompt" >&2
1219 cat "$w/verb.out" >&2
1220 exit 1
1221fi
1222if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-verb)" \
1223 != "$verbhead" ]; then
1224 echo "valley-request: the approve verb's request does not name the branch's head" >&2
1225 exit 1
1226fi
1227git -C "$origin" rev-parse --verify --quiet \
1228 "refs/the-valley/attestations/$verbdigest/$keyhash" > /dev/null || {
1229 echo "valley-request: the approve verb published no evidence" >&2
1230 exit 1
1231}
1232pushes="$(grep -c '^--- push' "$pushlog" || true)"
1233if [ "$pushes" != 1 ]; then
1234 echo "valley-request: the approve verb took $pushes pushes, not 1" >&2
1235 cat "$pushlog" >&2
1236 exit 1
1237fi
1238
1239# A refusal is the verb's exit status, after the same words the key's
1240# refusal prints, and it publishes nothing.
1241: > "$pushlog"
1242if valley approve -v topic/two < /dev/null > "$w/verb-refused.out" 2> "$w/verb-refused.err"; then
1243 echo "valley-request: the approve verb exited 0 for a change whose check failed" >&2
1244 exit 1
1245fi
1246grep -q '^⊘ topic/two not approved · 1 of [0-9]* checks* failed$' "$w/verb-refused.err"
1247if [ -s "$pushlog" ]; then
1248 echo "valley-request: a refused approve verb pushed something" >&2
1249 cat "$pushlog" >&2
1250 exit 1
1251fi
1252if valley approve -v topic/no-such-change < /dev/null > "$w/verb-missing.out" 2> "$w/verb-missing.err"; then
1253 echo "valley-request: the approve verb exited 0 for a change origin does not have" >&2
1254 exit 1
1255fi
1256grep -q 'no such branch on origin: topic/no-such-change' "$w/verb-missing.err"
1257
1258# ----------------------------------------------------------------------
1259# 17b. Timing never stands in the way of approval. A FIFO nobody reads,
1260# sitting at the timing log's path, would hold an open for writing
1261# forever: the log is refused with a warning, and approval goes on.
1262# With standard error a pipe nobody reads as well, the warning is
1263# dropped rather than ending the run. Both runs keep a state
1264# directory of their own, so the log the other cases read is not
1265# touched.
1266fifostate="$w/fifo-state"
1267mkdir -p "$fifostate/valley"
1268mkfifo "$fifostate/valley/timings.jsonl"
1269approved_past_fifo() {
1270 if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/topic-$1")" \
1271 != "$(git rev-parse "topic/$1")" ]; then
1272 echo "valley-request: approving past a FIFO at the timing log filed no request ($2)" >&2
1273 exit 1
1274 fi
1275 git -C "$origin" rev-parse --verify --quiet \
1276 "refs/the-valley/attestations/$(attest digest --rev "topic/$1" | cut -d: -f2)/$keyhash" > /dev/null || {
1277 echo "valley-request: approving past a FIFO at the timing log published no evidence ($2)" >&2
1278 exit 1
1279 }
1280}
1281for branch in fifo unread; do
1282 git checkout --quiet -b "topic/$branch" origin/main
1283 echo "a readme approved past a FIFO, $branch" > docs/readme.md
1284 git commit --quiet -am "a readme approved past a FIFO, $branch"
1285 git push --quiet origin "topic/$branch"
1286 git checkout --quiet main
1287done
1288
1289XDG_STATE_HOME="$fifostate" timeout 120 valley approve -v origin/topic/fifo \
1290 < /dev/null > "$w/fifo.out" 2> "$w/fifo.err" || {
1291 echo "valley-request: approve did not finish past a FIFO at the timing log (exit $?):" >&2
1292 cat "$w/fifo.out" "$w/fifo.err" >&2
1293 exit 1
1294}
1295grep -qE '^time tree-ok +passed +[0-9][0-9.hms]* \(ran\)$' "$w/fifo.out"
1296grep -qx "attest: warning: timings not logged: $fifostate/valley/timings.jsonl is not a regular file" "$w/fifo.err"
1297approved_past_fifo fifo "standard error read"
1298
1299# A pipe with no reader. The FIFO is opened to read and write, so opening it
1300# to write does not wait; closing the first leaves the writer alone.
1301mkfifo "$w/unread"
1302exec 3<> "$w/unread"
1303exec 4> "$w/unread"
1304exec 3<&-
1305status=0
1306XDG_STATE_HOME="$fifostate" timeout 120 valley approve -v origin/topic/unread \
1307 < /dev/null > "$w/unread.out" 2>&4 || status=$?
1308exec 4>&-
1309if [ "$status" != 0 ]; then
1310 echo "valley-request: approve with nobody reading standard error exited $status:" >&2
1311 cat "$w/unread.out" >&2
1312 exit 1
1313fi
1314approved_past_fifo unread "standard error unread"
1315
1316# ----------------------------------------------------------------------
1317# 18. The rebase verb: [b]ase without the pager. A stale branch is
1318# rebased onto main in a throwaway worktree and pushed under a lease,
1319# and the new head is printed. Nothing is approved and nothing is
1320# re-reviewed: rebasing is mechanics, and the judgment on what it
1321# produced stays with whoever reads it next.
1322git checkout --quiet -b topic/behind "$first"
1323echo "a readme left behind" > docs/behind.md
1324git add -A
1325git commit --quiet -m "a readme left behind"
1326git push --quiet origin topic/behind
1327behindhead="$(git rev-parse topic/behind)"
1328git checkout --quiet main
1329if git -C "$origin" merge-base --is-ancestor refs/heads/main refs/heads/topic/behind; then
1330 echo "valley-request: the branch to rebase fast-forwarded from main already" >&2
1331 exit 1
1332fi
1333before_rebase="$(git -C "$origin" rev-parse refs/heads/main)"
1334
1335: > "$pushlog"
1336valley rebase topic/behind < /dev/null > "$w/rebase.out" 2> "$w/rebase.err"
1337rebased="$(git -C "$origin" rev-parse refs/heads/topic/behind)"
1338grep -qx 'rebased topic/behind onto origin/main, pushed with --force-with-lease' "$w/rebase.out"
1339grep -qx " head $rebased" "$w/rebase.out"
1340if [ "$rebased" = "$behindhead" ]; then
1341 echo "valley-request: the rebase verb left the branch where it was" >&2
1342 exit 1
1343fi
1344git -C "$origin" merge-base --is-ancestor refs/heads/main "$rebased" || {
1345 echo "valley-request: the rebased head does not fast-forward from main" >&2
1346 exit 1
1347}
1348if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_rebase" ]; then
1349 echo "valley-request: the rebase verb moved main" >&2
1350 exit 1
1351fi
1352if git -C "$origin" rev-parse --verify --quiet \
1353 refs/the-valley/integration-requests/main/topic-behind > /dev/null; then
1354 echo "valley-request: the rebase verb filed a request" >&2
1355 exit 1
1356fi
1357pushes="$(grep -c '^--- push' "$pushlog" || true)"
1358if [ "$pushes" != 1 ] || grep -v '^--- push' "$pushlog" | grep -qv ' refs/heads/topic/behind$'; then
1359 echo "valley-request: the rebase verb pushed something besides the branch" >&2
1360 cat "$pushlog" >&2
1361 exit 1
1362fi
1363
1364# A branch that fast-forwards from main already is left as it is.
1365: > "$pushlog"
1366valley rebase topic/behind < /dev/null > "$w/rebase-again.out" 2> "$w/rebase-again.err"
1367grep -qx 'topic/behind already fast-forwards from origin/main; nothing rebased' "$w/rebase-again.out"
1368grep -qx " head $rebased" "$w/rebase-again.out"
1369if [ -s "$pushlog" ]; then
1370 echo "valley-request: rebasing a branch that fast-forwards pushed something" >&2
1371 cat "$pushlog" >&2
1372 exit 1
1373fi
1374
1375# A rebase that conflicts changes nothing, says so, and exits non-zero.
1376git checkout --quiet -b topic/clash "$first"
1377echo "a note that clashes" > docs/notes.md
1378git add -A
1379git commit --quiet -m "a note that clashes"
1380git push --quiet origin topic/clash
1381clashhead="$(git rev-parse topic/clash)"
1382git checkout --quiet main
1383if valley rebase topic/clash < /dev/null > "$w/clash.out" 2> "$w/clash.err"; then
1384 echo "valley-request: a rebase that conflicts exited 0" >&2
1385 exit 1
1386fi
1387grep -q 'topic/clash conflicts with main — resolve by hand; nothing changed' "$w/clash.err"
1388if [ "$(git -C "$origin" rev-parse refs/heads/topic/clash)" != "$clashhead" ]; then
1389 echo "valley-request: a rebase that conflicted moved the branch" >&2
1390 exit 1
1391fi
1392
1393# ----------------------------------------------------------------------
1394# 19. A change whose version names no branch is approved at its head like
1395# any other. The record allows a version with no branch, though valley
1396# ready always names one, so the record here is written by hand. The
1397# check it requires runs over the recorded head, and the evidence and the
1398# c-<change ID> request reach origin in one push.
1399git checkout --quiet -b branchless main
1400echo "a readme no branch carries" > docs/readme.md
1401git commit --quiet -am "a readme no branch carries"
1402barehead="$(git rev-parse HEAD)"
1403baredigest="$(attest digest --rev "$barehead" | cut -d: -f2)"
1404git checkout --quiet main
1405bareid=rrrrssssttttuuuuvvvvwwwwxxxxyyyy
1406blob="$(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' \
1407 "$bareid" "$barehead" | git hash-object -w --stdin)"
1408record="$(git commit-tree "$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" \
1409 -p "$barehead" -m 'a change record')"
1410git push --quiet origin "$record:refs/the-valley/changes/main/$bareid"
1411
1412: > "$pushlog"
1413valley approve -v --head "$barehead" rrrr < /dev/null > "$w/branchless.out" 2> "$w/branchless.err"
1414grep -qx '◐ rrrr v1 checking 1 check: tree-ok' "$w/branchless.out"
1415grep -qx " request refs/the-valley/integration-requests/main/c-$bareid -> $(git rev-parse --short "$barehead")" "$w/branchless.out"
1416grep -qx " evidence refs/the-valley/attestations/$baredigest/$keyhash" "$w/branchless.out"
1417if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$bareid")" != "$barehead" ]; then
1418 echo "valley-request: approve of a change with no branch did not file its head" >&2
1419 exit 1
1420fi
1421pushes="$(grep -c '^--- push' "$pushlog" || true)"
1422if [ "$pushes" != 1 ] \
1423 || ! grep -q "refs/the-valley/attestations/$baredigest/$keyhash" "$pushlog" \
1424 || ! grep -q "refs/the-valley/integration-requests/main/c-$bareid" "$pushlog"; then
1425 echo "valley-request: the evidence and request for a change with no branch did not go in one push" >&2
1426 cat "$pushlog" >&2
1427 exit 1
1428fi
1429# A change with a record is timed under its ID and the version approved.
1430if ! tail -n 1 "$timings" | jq -e --arg id "$bareid" --arg head "$barehead" \
1431 '.change == $id and .version == 1 and .head == $head and .check == "tree-ok"' > /dev/null; then
1432 echo "valley-request: the timing of a change with a record does not name the change and version:" >&2
1433 tail -n 1 "$timings" >&2
1434 exit 1
1435fi
1436
1437# ----------------------------------------------------------------------
1438# 20. Evidence origin already holds is reused, and only what is missing
1439# runs here. A check worker publishes evidence as this section's
1440# worker does: attest run under its own key, then a push of the
1441# attestation ref alone. This origin runs the protected host's real
1442# pre-receive hook, which admits a note only when a key it knows signed
1443# it, behind a wrapper that logs every push it receives. The project's
1444# owning valley publishes the signers a client verifies with, at
1445# refs/the-valley/signers, written on disk as the host's publisher
1446# writes it (signers-e2e drives the publisher itself). Both of the
1447# project's checks use the nix runner, through integrator-e2e's
1448# stand-in for nix, wrapped so that every call is logged: a check that
1449# ran is a check the log shows a build of.
1450r="$w/reuse"
1451mkdir -p "$r/bin" "$r/keys"
1452cp -r "$integratorE2e" "$r/stand-ins"
1453chmod -R +w "$r/stand-ins"
1454patchShebangs "$r/stand-ins" > /dev/null
1455nixlog="$r/nix.log"
1456for tool in nix nix-store; do
1457 cat > "$r/bin/$tool" <<EOF
1458#!$(command -v bash)
1459printf '%s %s\n' "$tool" "\$*" >> "$nixlog"
1460exec "$r/stand-ins/$tool" "\$@"
1461EOF
1462 chmod +x "$r/bin/$tool"
1463done
1464export PATH="$r/bin:$PATH" VALLEY_FAKE_STORE="$r/store"
1465
1466for who in worker operator integrator; do install -m 600 "$reuseKeys/$who" "$r/keys/$who"; done
1467workername=worker.valley.invalid/attestations
1468operatorname=operator.valley.invalid/attestations
1469integratorname=integrator.valley.invalid/attestations
1470workerhash="$(attest key --key "$r/keys/worker" --name "$workername" | cut -d+ -f2)"
1471operatorhash="$(attest key --key "$r/keys/operator" --name "$operatorname" | cut -d+ -f2)"
1472{
1473 attest key --key "$r/keys/worker" --name "$workername"
1474 attest key --key "$r/keys/operator" --name "$operatorname"
1475 attest key --key "$r/keys/integrator" --name "$integratorname"
1476} > "$r/known-signers"
1477
1478# The owning valley: the floor, which requires nothing, and the signers it
1479# publishes.
1480instance="$r/instance.git"
1481git init --quiet --bare "$instance"
1482git init --quiet "$r/seed"
1483mkdir -p "$r/seed/policy/instance" "$r/floor"
1484printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$r/floor/floor.cue"
1485cp "$r/floor/floor.cue" "$r/seed/policy/instance/"
1486git -C "$r/seed" add -A
1487git -C "$r/seed" commit --quiet -m 'the floor'
1488git -C "$r/seed" push --quiet "$instance" main
1489# published <file> [<mode> <type> <object>]: the instance publishes the
1490# known-signers file, and, given an entry, that entry as signer-projects.
1491published() {
1492 local keysblob nameblob entries
1493 keysblob="$(git -C "$instance" hash-object -w "$1")"
1494 nameblob="$(printf '%s\n' "$integratorname" | git -C "$instance" hash-object -w --stdin)"
1495 entries="$(printf '100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers' "$nameblob" "$keysblob")"
1496 [ $# -lt 4 ] || entries+="$(printf '\n%s %s %s\tsigner-projects' "$2" "$3" "$4")"
1497 git -C "$instance" update-ref refs/the-valley/signers "$(printf '%s\n' "$entries" | git -C "$instance" mktree)"
1498}
1499published "$r/known-signers"
1500
1501served="$r/project.git"
1502hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$reuseInitScriptPath" | head -n1)"
1503test -x "$hook"
1504git init --quiet --bare "$served"
1505reusepushes="$r/pushlog"
1506cat > "$served/hooks/pre-receive" <<EOF
1507#!/bin/sh
1508echo '--- push' >> "$reusepushes"
1509tee -a "$reusepushes" | "$hook"
1510EOF
1511chmod +x "$served/hooks/pre-receive"
1512
1513proj="$r/project"
1514git init --quiet "$proj"
1515cd "$proj" || exit 1
1516git remote add origin "$served"
1517mkdir -p docs src live checks policy/project
1518echo "$instance" > policy/valley
1519echo "the readme" > docs/readme.md
1520echo "the source" > src/main.txt
1521echo "the state" > live/state.txt
1522echo docs > checks/prose-format.inputs
1523echo src > checks/code-build.inputs
1524cat > policy/project/policy.cue <<'EOF'
1525package verification
1526
1527project: {
1528 checks: {
1529 "prose-format": {runner: "nix", attribute: "prose-format"}
1530 "code-build": {runner: "nix", attribute: "code-build"}
1531 "live-ok": {runner: "command", command: "true", validity: "1h"}
1532 }
1533 classes: {
1534 prose: {paths: "docs/**": true, requires: "prose-format": true}
1535 code: {paths: "src/**": true, requires: "code-build": true}
1536 live: {paths: "live/**": true, requires: "live-ok": true}
1537 }
1538 unclassified: {}
1539}
1540EOF
1541git add -A
1542git commit --quiet -m 'seed the project'
1543git push --quiet origin main
1544git clone --quiet "$served" "$r/worker"
1545
1546# edited <branch> <file…>: a branch off origin's main that adds a line to
1547# each file, pushed.
1548edited() {
1549 local name="$1" f
1550 shift
1551 git fetch --quiet origin
1552 git checkout --quiet -B "$name" origin/main
1553 for f in "$@"; do echo "$name" >> "$f"; done
1554 git add -A
1555 git commit --quiet -m "$name"
1556 git push --quiet origin "$name"
1557}
1558# worker <head> <attest run flag…>: the worker attests the head under its
1559# own key and pushes the attestation ref alone.
1560worker() {
1561 local head="$1" digest ref
1562 shift
1563 git -C "$r/worker" fetch --quiet origin
1564 digest="$(attest digest --repo "$r/worker" --rev "$head" | cut -d: -f2)"
1565 ref="refs/the-valley/attestations/$digest/$workerhash"
1566 attest run --repo "$r/worker" --rev "$head" --key "$r/keys/worker" --name "$workername" "$@" \
1567 > "$r/worker.out" 2>&1 || {
1568 echo "valley-request: the worker's attest run failed" >&2
1569 cat "$r/worker.out" >&2
1570 exit 1
1571 }
1572 git -C "$r/worker" push --quiet origin "$ref:$ref"
1573}
1574# approving <name> <command…>: run the command as the principal that holds
1575# the approve grant, signing as the operator, into <name>.out and .err in
1576# the scratch directory. Origin's pushes and the nix log start empty.
1577approving() {
1578 local name="$1"
1579 shift
1580 : > "$nixlog"
1581 : > "$reusepushes"
1582 VALLEY_PRINCIPAL=requester VALLEY_ATTEST_KEY="$r/keys/operator" VALLEY_ATTEST_NAME="$operatorname" \
1583 "$@" < /dev/null > "$w/$name.out" 2> "$w/$name.err"
1584}
1585# requested <branch>: where origin's request for the branch stands, or
1586# nothing.
1587requested() {
1588 git -C "$served" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/$1" || true
1589}
1590# integrated <request> [<signer projects file>]: one pass of the real
1591# integrator, given the file when there is one, lands the request.
1592integrated() {
1593 integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
1594 --known-signers "$r/known-signers" ${2:+--signer-projects "$2"} --instance "$r/floor" --schema "$schemaFile" \
1595 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
1596 echo "valley-request: the integrator failed" >&2
1597 cat "$r/integrator.out" >&2
1598 exit 1
1599 }
1600 grep -q "^$1 -> refs/heads/main .*: land\$" "$r/integrator.out" || {
1601 echo "valley-request: the integrator did not land $1" >&2
1602 cat "$r/integrator.out" >&2
1603 exit 1
1604 }
1605}
1606
1607# 20a. The worker covered every required check. approve reuses both, runs
1608# nothing, needs no signing key, and pushes the request alone.
1609edited cover docs/readme.md src/main.txt
1610coverhead="$(git rev-parse cover)"
1611coverdigest="$(attest digest --rev cover | cut -d: -f2)"
1612worker "$coverhead" --check prose-format --check code-build
1613approving cover env -u VALLEY_ATTEST_KEY valley approve -v origin/cover
1614grep -q "^reusing the evidence origin holds for cover at $(git rev-parse --short cover), tree " "$w/cover.out"
1615grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/cover.out"
1616grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/cover.out"
1617grep -qx " reused refs/the-valley/attestations/$coverdigest/$workerhash" "$w/cover.out"
1618grep -qx ' checks code-build, prose-format' "$w/cover.out"
1619if grep -qE '^(checking| evidence) ' "$w/cover.out" || [ -s "$nixlog" ]; then
1620 echo "valley-request: approve ran checks that origin's evidence covered" >&2
1621 cat "$w/cover.out" "$nixlog" >&2
1622 exit 1
1623fi
1624[ "$(requested cover)" = "$coverhead" ] || {
1625 echo "valley-request: a fully covered approval filed no request" >&2
1626 exit 1
1627}
1628if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1629 || ! grep -q ' refs/the-valley/integration-requests/main/cover$' "$reusepushes" \
1630 || grep -q ' refs/the-valley/attestations/' "$reusepushes"; then
1631 echo "valley-request: a fully covered approval did not push the request alone" >&2
1632 cat "$reusepushes" >&2
1633 exit 1
1634fi
1635
1636# 20b. The same head again: the evidence is on origin and the request
1637# stands, so there is nothing to push and no push is made.
1638approving cover-again env -u VALLEY_ATTEST_KEY valley approve -v origin/cover
1639grep -qx ' the request already stood at this head and origin already held the evidence, so nothing needed publishing' "$w/cover-again.out"
1640if [ -s "$reusepushes" ] || [ -s "$nixlog" ]; then
1641 echo "valley-request: approving a covered head again pushed or ran something" >&2
1642 cat "$reusepushes" "$nixlog" >&2
1643 exit 1
1644fi
1645# The integrator verifies the worker's evidence itself, and lands it.
1646integrated cover
1647
1648# 20c. The request write stays leased when nothing runs: a request filed
1649# while this approval is prepared wins, and nothing is published.
1650edited raced docs/readme.md
1651racedhead="$(git rev-parse raced)"
1652worker "$racedhead" --check prose-format
1653arm_race "git -C '$served' update-ref refs/the-valley/integration-requests/main/raced '$coverhead'"
1654if approving raced env -u VALLEY_ATTEST_KEY valley approve -v origin/raced; then
1655 echo "valley-request: a covered approval clobbered a request filed during it" >&2
1656 exit 1
1657fi
1658disarm_race
1659grep -q "^⊘ raced not approved · another approval filed $(git rev-parse --short "$coverhead") (.*) meanwhile$" "$w/raced.err"
1660if [ "$(requested raced)" != "$coverhead" ] || [ -s "$reusepushes" ]; then
1661 echo "valley-request: a covered approval that lost the lease published something" >&2
1662 exit 1
1663fi
1664git -C "$served" update-ref -d refs/the-valley/integration-requests/main/raced
1665
1666# 20d. The worker covered one check and has no note for the other. A worker
1667# note for the other check that names another flake attribute is not
1668# reused either (the attribute case below), and the integrator
1669# refuses it too. Without --build-here, approve
1670# files the request at once with the evidence origin holds: it builds
1671# nothing, pushes the request alone, names the missing check, and
1672# exits 0. review's [a]pprove files the same way. Each request is
1673# taken away again before the integrator reads it. With --build-here,
1674# that check runs here: exactly that one, signed by the operator, in
1675# one push with the request. The integrator lands it on the two
1676# signers' evidence. 21f has the integrator hold a request filed
1677# without evidence, and land it once the evidence arrives.
1678edited partial docs/readme.md src/main.txt
1679partialhead="$(git rev-parse partial)"
1680partialdigest="$(attest digest --rev partial | cut -d: -f2)"
1681worker "$partialhead" --check prose-format
1682for filed in partial-filed partial-review; do
1683 if [ "$filed" = partial-filed ]; then
1684 approving "$filed" valley approve -v origin/partial
1685 else
1686 approving "$filed" bash -c 'printf "a\n" | valley review -v origin/partial'
1687 fi
1688 if ! grep -qx '◇ partial approved · waiting for 1 check (no worker building)' "$w/$filed.out" \
1689 || ! grep -qx ' run: valley approve --build-here partial' "$w/$filed.out" \
1690 || ! grep -qx ' missing code-build' "$w/$filed.out" \
1691 || ! grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/$filed.out" \
1692 || grep -qE '^(checking| evidence) ' "$w/$filed.out" || [ -s "$nixlog" ] \
1693 || [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1694 || ! grep -q ' refs/the-valley/integration-requests/main/partial$' "$reusepushes" \
1695 || grep -q ' refs/the-valley/attestations/' "$reusepushes" \
1696 || [ "$(requested partial)" != "$partialhead" ]; then
1697 echo "valley-request: $filed did not file the request alone while naming the missing nix check" >&2
1698 cat "$w/$filed.out" "$w/$filed.err" "$nixlog" "$reusepushes" >&2
1699 exit 1
1700 fi
1701 git -C "$served" update-ref -d refs/the-valley/integration-requests/main/partial
1702done
1703approving partial valley approve -v --build-here origin/partial
1704grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/partial.out"
1705grep -qx "◐ partial checking 1 check: code-build" "$w/partial.out"
1706grep -qE '^check code-build +nix +passed$' "$w/partial.out"
1707if ! grep -qE '^nix build .*/code-build-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" || grep -q 'prose-format' "$nixlog"; then
1708 echo "valley-request: a partly covered approval did not run exactly the missing check" >&2
1709 cat "$nixlog" >&2
1710 exit 1
1711fi
1712grep -qx " evidence refs/the-valley/attestations/$partialdigest/$operatorhash" "$w/partial.out"
1713grep -qx " reused refs/the-valley/attestations/$partialdigest/$workerhash" "$w/partial.out"
1714if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
1715 || ! grep -q " refs/the-valley/attestations/$partialdigest/$operatorhash\$" "$reusepushes" \
1716 || ! grep -q ' refs/the-valley/integration-requests/main/partial$' "$reusepushes"; then
1717 echo "valley-request: the remainder's evidence and the request did not go in one push" >&2
1718 cat "$reusepushes" >&2
1719 exit 1
1720fi
1721git cat-file -p "refs/the-valley/attestations/$partialdigest/$operatorhash:code-build/statement.note" > /dev/null
1722if git cat-file -e "refs/the-valley/attestations/$partialdigest/$operatorhash:prose-format" 2> /dev/null; then
1723 echo "valley-request: the operator signed a check the worker's evidence covered" >&2
1724 exit 1
1725fi
1726integrated partial
1727
1728# 20e. Origin already holds the operator's own evidence for this tree, for
1729# one check alone. It is reused, and the other check has nowhere to
1730# go, since approve publishes a signer's evidence for a tree once:
1731# refused before anything runs (bd-7b7d58f).
1732edited occupied docs/readme.md src/main.txt
1733occupiedhead="$(git rev-parse occupied)"
1734occupieddigest="$(attest digest --rev occupied | cut -d: -f2)"
1735attest run --rev "$occupiedhead" --key "$r/keys/operator" --name "$operatorname" --check code-build \
1736 > "$r/operator.out" 2>&1
1737git push --quiet origin "refs/the-valley/attestations/$occupieddigest/$operatorhash:refs/the-valley/attestations/$occupieddigest/$operatorhash"
1738if approving occupied valley approve -v --build-here origin/occupied; then
1739 echo "valley-request: approve built evidence that origin could not take" >&2
1740 exit 1
1741fi
1742grep -qE "^ code-build +signed by $operatorname\+$operatorhash\$" "$w/occupied.out"
1743grep -qx "⊘ occupied not approved · this key's evidence for the tree, on origin, lacks prose-format" "$w/occupied.err"
1744grep -qx ' attest with another key: set VALLEY_ATTEST_KEY' "$w/occupied.err"
1745if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested occupied)" ]; then
1746 echo "valley-request: an approval refused for an occupied ref ran or published something" >&2
1747 exit 1
1748fi
1749
1750# 20f. A listing of origin's evidence that fails, and a fetch of it that
1751# fails, are refusals, never an empty answer: nothing runs and nothing
1752# is published. A stand-in for git, first on PATH, fails the one call
1753# named. It needs bin/valley run straight, since the package's wrapper
1754# puts its own git ahead of anything on PATH.
1755edited unread docs/readme.md
1756unreadhead="$(git rev-parse unread)"
1757unreaddigest="$(attest digest --rev unread | cut -d: -f2)"
1758worker "$unreadhead" --check prose-format
1759unreadoid="$(git -C "$served" rev-parse "refs/the-valley/attestations/$unreaddigest/$workerhash")"
1760realgit="$(command -v git)"
1761mkdir -p "$r/shim"
1762cat > "$r/shim/git" <<EOF
1763#!$(command -v bash)
1764# git, failing the one call REUSE_FAIL names: the listing of origin's
1765# evidence for one tree, or a fetch of the object one of its refs names.
1766case "\${REUSE_FAIL:-}:\$*" in
1767 inventory:ls-remote*"refs/the-valley/attestations/$unreaddigest/"*)
1768 echo "git: injected failure" >&2
1769 exit 128
1770 ;;
1771 fetch:fetch*--stdin*)
1772 wanted="\$(cat)"
1773 if grep -qF "$unreadoid" <<< "\$wanted"; then
1774 echo "git: injected failure" >&2
1775 exit 128
1776 fi
1777 printf '%s\n' "\$wanted" | exec "$realgit" "\$@"
1778 ;;
1779esac
1780exec "$realgit" "\$@"
1781EOF
1782chmod +x "$r/shim/git"
1783for failing in inventory fetch; do
1784 if REUSE_FAIL="$failing" PATH="$r/shim:$PATH" VALLEY_VERIFICATION_SCHEMA="$schemaFile" \
1785 approving "unread-$failing" bash "$cliScript" approve -v origin/unread; then
1786 echo "valley-request: approve went on past a failed $failing of origin's evidence" >&2
1787 exit 1
1788 fi
1789 case "$failing" in
1790 inventory) says='valley: could not list the evidence origin holds for this tree' ;;
1791 fetch) says='valley: could not fetch the evidence origin holds for this tree' ;;
1792 esac
1793 grep -qxF "$says" "$w/unread-$failing.err"
1794 grep -qx '⊘ unread not approved · could not read the evidence origin holds' "$w/unread-$failing.err"
1795 if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested unread)" ]; then
1796 echo "valley-request: approve ran or published something after a failed $failing" >&2
1797 exit 1
1798 fi
1799done
1800# And with origin answering, the same approval reuses the worker's
1801# evidence: the stand-in failed only what it was told to.
1802VALLEY_VERIFICATION_SCHEMA="$schemaFile" approving unread bash "$cliScript" approve -v origin/unread
1803grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/unread.out"
1804[ "$(requested unread)" = "$unreadhead" ]
1805integrated unread
1806
1807# 20g. approve skips a check only where the integrator accepts the same
1808# evidence. Each case below stores one set of evidence on origin.
1809# Approve, with no signing key, reuses nothing for it, naming the note
1810# the integrator judges the check by and why it is not reused. It files
1811# the request at once all the same, naming the check as missing and
1812# building nothing. Then one pass of the real integrator decides that
1813# request from the same refs. A note the hook would
1814# refuse is planted past it, straight into origin's storage. Two cases
1815# land: there approve is stricter than the integrator, on purpose.
1816#
1817# noted <file> <key> <name> <rev> <check> <attest run flag…>: attest the
1818# revision in the worker's clone under the key, and copy the note it stored
1819# for the check to the file, byte for byte. Nothing pushes the ref it
1820# stored.
1821noted() {
1822 local file="$1" key="$2" name="$3" rev="$4" check="$5" digest hash
1823 shift 5
1824 git -C "$r/worker" fetch --quiet origin
1825 digest="$(attest digest --repo "$r/worker" --rev "$rev" | cut -d: -f2)"
1826 hash="$(attest key --key "$key" --name "$name" | cut -d+ -f2)"
1827 attest run --repo "$r/worker" --rev "$rev" --key "$key" --name "$name" "$@" > "$r/noted.out" 2>&1 || {
1828 echo "valley-request: attesting $rev for a planted note failed" >&2
1829 cat "$r/noted.out" >&2
1830 exit 1
1831 }
1832 git -C "$r/worker" cat-file blob "refs/the-valley/attestations/$digest/$hash:$check/statement.note" > "$file"
1833}
1834# resigned <file> <sed expression>: the note's text, edited by the
1835# expression, signed again by the worker.
1836resigned() {
1837 sed -n '/^$/q;p' "$1" | sed "$2" > "$r/text"
1838 attest sign --key "$r/keys/worker" --name "$workername" "$r/text" > "$1"
1839}
1840# planted <ref> <check> <file>: the note stored at the ref on origin, in a
1841# tree shaped as attest shapes one, straight into storage.
1842planted() {
1843 local blob sub
1844 blob="$(git -C "$served" hash-object -w "$3")"
1845 sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$served" mktree)"
1846 git -C "$served" update-ref "$1" "$(printf '040000 tree %s\t%s\n' "$sub" "$2" | git -C "$served" mktree)"
1847}
1848# unreused <branch> <reason>: approve, with no signing key, reuses nothing
1849# for prose-format, says why, and files the request with prose-format
1850# missing, building nothing.
1851unreused() {
1852 approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"
1853 if ! grep -qF "valley: not reusing prose-format: the integrator judges it by " "$w/$1.err" \
1854 || ! grep -qF -- "$2" "$w/$1.err" || grep -q 'reusing' "$w/$1.out" \
1855 || ! grep -qx ' missing prose-format' "$w/$1.out" \
1856 || [ "$(requested "$1")" != "$(git rev-parse "$1")" ] || [ -s "$nixlog" ]; then
1857 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
1858 cat "$w/$1.err" >&2
1859 exit 1
1860 fi
1861}
1862# unpassed <branch> <reason>: the note the integrator judges prose-format
1863# by verifies and is no pass of it, so building the check again changes
1864# nothing for that tree. approve says why, refuses the head, and files
1865# and builds nothing.
1866unpassed() {
1867 if approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"; then
1868 echo "valley-request: approve filed $1, whose judged note is no pass" >&2
1869 exit 1
1870 fi
1871 if ! grep -qF -- "$2" "$w/$1.err" \
1872 || ! grep -q "^⊘ $1 not approved · 1 of [0-9]* checks* failed\$" "$w/$1.err" \
1873 || ! grep -qx " ✗ prose-format — see: valley status $1" "$w/$1.err" \
1874 || [ -n "$(requested "$1")" ] || [ -s "$nixlog" ]; then
1875 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
1876 cat "$w/$1.err" >&2
1877 exit 1
1878 fi
1879}
1880# judged <branch> <outcome> <reason> [<known-signers> [<signer projects>]]:
1881# the branch's head is filed, by hand when approve has not, and one pass of the real integrator,
1882# verifying with the known-signers file and given the signer projects file
1883# when there is one, decides it as the outcome, giving the reason. A
1884# request it does not land is taken away again.
1885judged() {
1886 git -C "$served" update-ref "refs/the-valley/integration-requests/main/$1" "$(git rev-parse "$1")"
1887 integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
1888 --known-signers "${4:-$r/known-signers}" ${5:+--signer-projects "$5"} --instance "$r/floor" --schema "$schemaFile" \
1889 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
1890 echo "valley-request: the integrator failed" >&2
1891 cat "$r/integrator.out" >&2
1892 exit 1
1893 }
1894 if ! grep -q "^$1 -> refs/heads/main @ [0-9a-f]*: $2\$" "$r/integrator.out" \
1895 || ! grep -qF -- "$3" "$r/integrator.out"; then
1896 echo "valley-request: the integrator did not decide $1 as $2, saying: $3" >&2
1897 cat "$r/integrator.out" >&2
1898 exit 1
1899 fi
1900 [ "$2" = land ] || git -C "$served" update-ref -d "refs/the-valley/integration-requests/main/$1"
1901}
1902under() { printf 'refs/the-valley/attestations/%s/%s' "$(attest digest --rev "$1" | cut -d: -f2)" "$2"; }
1903
1904# Stale trust: the host revokes the worker, and this operator's own
1905# known-signers file still trusts it. The override does not widen what the
1906# host publishes.
1907{
1908 attest key --key "$r/keys/operator" --name "$operatorname"
1909 attest key --key "$r/keys/integrator" --name "$integratorname"
1910} > "$r/revoked-signers"
1911edited revoked docs/readme.md
1912worker "$(git rev-parse revoked)" --check prose-format
1913published "$r/revoked-signers"
1914VALLEY_KNOWN_SIGNERS="$r/known-signers" unreused revoked \
1915 "attest verify refuses it under the host's signers: no signature on this note is by a key the verifier holds"
1916judged revoked stale 'signed by no signer the integrator holds' "$r/revoked-signers"
1917published "$r/known-signers"
1918
1919# Narrowed trust: an override that does not trust the worker keeps approve
1920# from reusing what the host would accept.
1921edited narrowed docs/readme.md
1922worker "$(git rev-parse narrowed)" --check prose-format
1923VALLEY_KNOWN_SIGNERS="$r/revoked-signers" unreused narrowed \
1924 'attest verify refuses it under VALLEY_KNOWN_SIGNERS: no signature on this note is by a key the verifier holds'
1925judged narrowed land 'transferred (unmoved)'
1926
1927# Conflicting notes: the integrator judges a check by the first note that
1928# verifies, in ref order. A failure the worker signed sits first, so the
1929# pass the worker published after it does not count.
1930edited failed docs/readme.md
1931noted "$r/failed.note" "$r/keys/worker" "$workername" "$(git rev-parse failed)" prose-format --check prose-format
1932resigned "$r/failed.note" 's/^predicate.result passed$/predicate.result failed/'
1933attest verify --note "$r/failed.note" --known-keys "$r/known-signers" --repo . --rev failed > /dev/null
1934planted "$(under failed 00000000)" prose-format "$r/failed.note"
1935worker "$(git rev-parse failed)" --check prose-format
1936unpassed failed 'its predicate.result is "failed", not "passed"'
1937judged failed stale 'the attestation records the check as failed'
1938
1939# The same, with an observation first: a note of the wrong kind is no
1940# evidence, so the request is stale.
1941edited kind docs/readme.md
1942noted "$r/kind.note" "$r/keys/worker" "$workername" "$(git rev-parse kind)" prose-format --command prose-format=true
1943planted "$(under kind 00000000)" prose-format "$r/kind.note"
1944worker "$(git rev-parse kind)" --check prose-format
1945unpassed kind 'its predicateType is "the-valley/check/effectful/v1", not "the-valley/check/pure/v1"'
1946judged kind stale 'the policy requires a pure check and the attestation is for an effectful check'
1947
1948# A malformed envelope: a note the worker signed, without its last newline.
1949# approve reads it byte for byte, as the integrator does.
1950edited envelope docs/readme.md
1951noted "$r/envelope.note" "$r/keys/worker" "$workername" "$(git rev-parse envelope)" prose-format --check prose-format
1952head -c -1 "$r/envelope.note" > "$r/envelope.cut"
1953planted "$(under envelope "$workerhash")" prose-format "$r/envelope.cut"
1954unreused envelope 'the signature block is empty or does not end with a newline'
1955judged envelope reject 'the attestation does not verify as the statement it claims to be'
1956
1957# An unknown signature: a key neither the host nor the hook knows.
1958ssh-keygen -q -t ed25519 -N "" -C stranger -f "$r/keys/stranger"
1959strangername=stranger.valley.invalid/attestations
1960edited stranger docs/readme.md
1961noted "$r/stranger.note" "$r/keys/stranger" "$strangername" "$(git rev-parse stranger)" prose-format --check prose-format
1962planted "$(under stranger "$(attest key --key "$r/keys/stranger" --name "$strangername" | cut -d+ -f2)")" \
1963 prose-format "$r/stranger.note"
1964unreused stranger 'no signature on this note is by a key the verifier holds'
1965judged stranger stale 'signed by no signer the integrator holds'
1966
1967# A wrong tree: the worker's note about main, stored under this head's tree.
1968edited tree docs/readme.md
1969noted "$r/tree.note" "$r/keys/worker" "$workername" origin/main prose-format --check prose-format
1970planted "$(under tree "$workerhash")" prose-format "$r/tree.note"
1971unreused tree 'tree digest mismatch'
1972judged tree reject 'tree digest mismatch'
1973
1974# An expired observation of a command check. approve never reuses one, so
1975# with no key it refuses; the integrator refuses the observation as too old.
1976edited expired live/state.txt
1977noted "$r/expired.note" "$r/keys/worker" "$workername" "$(git rev-parse expired)" live-ok --command live-ok=true
1978resigned "$r/expired.note" 's/^predicate.observedAt .*/predicate.observedAt 2020-01-01T00:00:00Z/'
1979planted "$(under expired "$workerhash")" live-ok "$r/expired.note"
1980if approving expired env -u VALLEY_ATTEST_KEY valley approve -v origin/expired \
1981 || ! grep -q 'no signing key' "$w/expired.err" || grep -q 'reusing' "$w/expired.out"; then
1982 echo "valley-request: approve reused an observation of a command check" >&2
1983 cat "$w/expired.out" "$w/expired.err" >&2
1984 exit 1
1985fi
1986judged expired stale 'the observation is '
1987
1988# The attribute: the worker's note for prose-format names another flake
1989# attribute. Approve compares it and refuses the head; the integrator
1990# compares it too, on an unmoved tree, and the note is no evidence for
1991# prose-format.
1992edited attribute docs/readme.md
1993worker "$(git rev-parse attribute)" --check prose-format=code-build
1994unpassed attribute 'its predicate.check.attribute is "code-build", not "prose-format"'
1995judged attribute stale 'the attestation is for runner "nix", attribute "code-build", and the policy requires runner "nix", attribute "prose-format"'
1996
1997# 20h. A signer can be restricted to projects. The host publishes its
1998# signer projects file beside the known-signers file, and approve
1999# reuses a note only when one of its signers counts in this project.
2000# The project is named project, after origin, as the integrator names
2001# it after its repository. Each integrator pass is given the file the
2002# host publishes.
2003#
2004# restricting <file>: the instance publishes the known-signers file with
2005# this signer projects file.
2006restricting() {
2007 published "$r/known-signers" 100644 blob "$(git -C "$instance" hash-object -w "$1")"
2008}
2009# unpublished <branch> <reason>: the publication is not used, so approve,
2010# with no signing key, reuses none of origin's evidence, says why, and
2011# files the request with prose-format missing, building nothing.
2012unpublished() {
2013 approving "$1" env -u VALLEY_ATTEST_KEY valley approve -v "origin/$1"
2014 if ! grep -qxF "valley: $2, so none of the evidence on origin is reused" "$w/$1.err" \
2015 || grep -q 'reusing' "$w/$1.out" \
2016 || ! grep -qx ' missing prose-format' "$w/$1.out" \
2017 || [ "$(requested "$1")" != "$(git rev-parse "$1")" ] || [ -s "$nixlog" ]; then
2018 echo "valley-request: approve did not refuse $1 for the reason: $2" >&2
2019 cat "$w/$1.err" >&2
2020 exit 1
2021 fi
2022}
2023
2024# The worker restricted to another project: its note is a stranger's here.
2025# approve does not reuse it, the integrator does not accept it, and an
2026# approval from review --build-here, with a signing key, builds the check
2027# under the operator's key.
2028printf '%s elsewhere\n' "$workername" > "$r/elsewhere.projects"
2029restricting "$r/elsewhere.projects"
2030edited elsewhere docs/readme.md
2031elsewherehead="$(git rev-parse elsewhere)"
2032worker "$elsewherehead" --check prose-format
2033unreused elsewhere \
2034 "none of its signers counts in project project: the published signer-projects lets $workername count only in elsewhere"
2035judged elsewhere stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects"
2036approving elsewhere-built bash -c 'printf "a\n" | valley review -v --build-here origin/elsewhere'
2037grep -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"
2038grep -qx "◐ elsewhere checking 1 check: prose-format" "$w/elsewhere-built.out"
2039grep -qx " evidence $(under elsewhere "$operatorhash")" "$w/elsewhere-built.out"
2040if ! grep -qE '^nix build .*/prose-format-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" \
2041 || grep -q '^ reused ' "$w/elsewhere-built.out"; then
2042 echo "valley-request: approve did not build the check whose only signer counts elsewhere" >&2
2043 cat "$w/elsewhere-built.out" "$nixlog" >&2
2044 exit 1
2045fi
2046[ "$(requested elsewhere)" = "$elsewherehead" ]
2047integrated elsewhere "$r/elsewhere.projects"
2048
2049# The same worker note, countersigned by the integrator. The integrator's
2050# own signature never counts for a check, so the note still has no signer
2051# who counts here.
2052edited countersigned docs/readme.md
2053noted "$r/countersigned.note" "$r/keys/worker" "$workername" "$(git rev-parse countersigned)" prose-format --check prose-format
2054attest sign --key "$r/keys/integrator" --name "$integratorname" "$r/countersigned.note" > "$r/countersigned.both"
2055planted "$(under countersigned "$workerhash")" prose-format "$r/countersigned.both"
2056unreused countersigned \
2057 "the published signer-projects lets $workername count only in elsewhere; $integratorname is the host's integrator, whose signature is a countersignature"
2058judged countersigned stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects"
2059
2060# A published list that does not read, and one that is no file: the
2061# publication is not used, and nothing on origin is reused.
2062edited listed docs/readme.md
2063listedhead="$(git rev-parse listed)"
2064worker "$listedhead" --check prose-format
2065printf '%s project\n%s elsewhere\n' "$workername" "$workername" > "$r/twice.projects"
2066restricting "$r/twice.projects"
2067unpublished listed \
2068 "signer-projects in refs/the-valley/signers in $instance does not read: line 2: $workername is named on an earlier line too"
2069printf '%s project\n' "$workername" > "$r/here.projects"
2070listblob="$(git -C "$instance" hash-object -w "$r/here.projects")"
2071for entry in "040000 tree $(printf '100644 blob %s\tlist\n' "$listblob" | git -C "$instance" mktree)" "100755 blob $listblob"; do
2072 # shellcheck disable=SC2086 # the entry is a mode, a type and an object
2073 published "$r/known-signers" $entry
2074 unpublished listed "signer-projects in refs/the-valley/signers in $instance is not a file"
2075done
2076
2077# The worker restricted to this project and one more: the same note is
2078# reused, nothing runs, and the integrator lands it.
2079printf '# the projects the worker builds for\n%s elsewhere project\n' "$workername" > "$r/here.projects"
2080restricting "$r/here.projects"
2081approving listed env -u VALLEY_ATTEST_KEY valley approve -v origin/listed
2082grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/listed.out"
2083grep -qx " reused $(under listed "$workerhash")" "$w/listed.out"
2084if [ -s "$nixlog" ] || [ "$(requested listed)" != "$listedhead" ]; then
2085 echo "valley-request: approve did not reuse the note of a worker restricted to this project" >&2
2086 cat "$w/listed.out" "$w/listed.err" "$nixlog" >&2
2087 exit 1
2088fi
2089integrated listed "$r/here.projects"
2090published "$r/known-signers"
2091
2092# 20i. The worker covered the one nix check, and the change requires a
2093# command check too. No worker runs a command check, so approve runs
2094# it here without --build-here, builds no nix check, and files.
2095edited commanded docs/readme.md live/state.txt
2096commandedhead="$(git rev-parse commanded)"
2097worker "$commandedhead" --check prose-format
2098approving commanded valley approve -v origin/commanded
2099grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/commanded.out"
2100grep -qx "◐ commanded checking 1 check: live-ok" "$w/commanded.out"
2101if grep -q '^nix build' "$nixlog" || [ "$(requested commanded)" != "$commandedhead" ]; then
2102 echo "valley-request: an approval whose only uncovered check is a command check built nix or filed nothing" >&2
2103 cat "$w/commanded.out" "$w/commanded.err" "$nixlog" >&2
2104 exit 1
2105fi
2106integrated commanded
2107
2108# 20j. No worker has built the change, and it requires a command check and
2109# a nix check. approve without --build-here runs the command check
2110# here, builds no nix check, and publishes the command check's
2111# evidence with the request in one push, naming the nix check as
2112# missing. It does not offer --build-here, which that evidence now
2113# blocks (bd-7b7d58f).
2114edited mixed docs/readme.md live/state.txt
2115mixedhead="$(git rev-parse mixed)"
2116mixeddigest="$(attest digest --rev mixed | cut -d: -f2)"
2117approving mixed valley approve -v origin/mixed
2118grep -qx "◐ mixed checking 1 check: live-ok" "$w/mixed.out"
2119grep -qx ' missing prose-format' "$w/mixed.out"
2120grep -qx '◇ mixed approved · waiting for 1 check (no worker building)' "$w/mixed.out"
2121grep -qx ' run: valley ready mixed' "$w/mixed.out"
2122grep -qx " evidence refs/the-valley/attestations/$mixeddigest/$operatorhash" "$w/mixed.out"
2123if grep -q '^nix build' "$nixlog" || [ "$(requested mixed)" != "$mixedhead" ] \
2124 || [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
2125 || ! grep -q " refs/the-valley/attestations/$mixeddigest/$operatorhash\$" "$reusepushes" \
2126 || ! grep -q ' refs/the-valley/integration-requests/main/mixed$' "$reusepushes"; then
2127 echo "valley-request: an approval missing a nix check did not run the command check alone and file with its evidence" >&2
2128 cat "$w/mixed.out" "$w/mixed.err" "$nixlog" "$reusepushes" >&2
2129 exit 1
2130fi
2131git -C "$served" update-ref -d refs/the-valley/integration-requests/main/mixed
2132
2133# ----------------------------------------------------------------------
2134# 21. The check worker builds what ready changes require before anyone
2135# approves them, and publishes it where approve reuses it. It runs as
2136# packaged, valley-worker, from a clone of the owning valley, which now
2137# publishes its project list, so the worker reads the project through
2138# its own cache clone, as valley pending --repo does. It signs with the
2139# worker's key and pushes as a principal that holds no grant, through
2140# the same protected hook. The host restricts the worker's key to this
2141# project, and each integrator pass is given that list. The worker's
2142# state directory holds its lock and its record of failed batches.
2143printf '%s project\n' "$workername" > "$r/worker.projects"
2144restricting "$r/worker.projects"
2145git -C "$instance" update-ref refs/the-valley/projects \
2146 "$(printf 'project\n' | git -C "$instance" hash-object -w --stdin)"
2147git clone --quiet "$instance" "$r/worker-home"
2148wstate="$r/worker-state"
2149git config valley.principal requester
2150# fresh: origin's pushes and the nix log start empty.
2151fresh() {
2152 : > "$nixlog"
2153 : > "$reusepushes"
2154}
2155# working <name> [<env assignment>…]: one pass of the worker, with the
2156# environment given, into $r/<name>.log. Its status is the pass's.
2157working() {
2158 local name="$1"
2159 shift
2160 (cd "$r/worker-home" && env VALLEY_PRINCIPAL=worker "$@" valley-worker --repo project \
2161 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/$name.log" 2>&1
2162}
2163# readied <branch> <file…>: edited, then readied as a change by the
2164# requester. It sets id, handle and head, and line, the start of each line
2165# the worker prints about the change's active version.
2166readied() {
2167 local name="$1"
2168 edited "$@"
2169 VALLEY_PRINCIPAL=requester valley ready "$name" > "$r/$name.ready" 2>&1
2170 id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$r/$name.ready")"
2171 handle="$(sed -n 's/^project \([^ ]*\) v1 ready$/\1/p' "$r/$name.ready")"
2172 head="$(git rev-parse "$name")"
2173 line="project: $handle v1 at ${head:0:7}"
2174 [ -n "$id" ] && [ -n "$handle" ] || {
2175 echo "valley-request: could not ready $name" >&2
2176 cat "$r/$name.ready" >&2
2177 exit 1
2178 }
2179}
2180# pushed <ref>: origin received one push since fresh, carrying that ref
2181# alone. pushed none: origin received no push.
2182pushed() {
2183 if [ "$1" = none ]; then
2184 [ ! -s "$reusepushes" ] && return
2185 elif [ "$(grep -c '^--- push' "$reusepushes")" = 1 ] \
2186 && [ "$(grep -vc '^--- push' "$reusepushes")" = 1 ] \
2187 && grep -q " $1\$" "$reusepushes"; then
2188 return
2189 fi
2190 echo "valley-request: the worker's pushes were not what was expected ($1)" >&2
2191 cat "$reusepushes" >&2
2192 exit 1
2193}
2194# built <check…>: nix built a derivation of exactly these checks, each
2195# once. attest evaluates the checks of a batch together, then builds each
2196# check's derivation with nix build <store>/<check>-XXXXXX.drv^*, which the
2197# stand-in names after the check, so every derivation a nix build line
2198# names is read as one build of that check. built nothing: nix did not run.
2199built() {
2200 local got
2201 if [ "$1" = nothing ]; then
2202 [ ! -s "$nixlog" ] && return
2203 echo "valley-request: the worker ran nix when it had nothing to build" >&2
2204 cat "$nixlog" >&2
2205 exit 1
2206 fi
2207 got="$(grep '^nix build ' "$nixlog" | tr ' ' '\n' \
2208 | sed -n 's|^.*/\([a-z0-9][a-z0-9-]*\)-[A-Za-z0-9]\{6\}\.drv^\*$|\1|p' | sort | paste -sd' ')"
2209 [ "$got" = "$(printf '%s\n' "$@" | sort | paste -sd' ')" ] && return
2210 echo "valley-request: the worker built '$got', not '$*'" >&2
2211 cat "$nixlog" >&2
2212 exit 1
2213}
2214# said <name> <line>: the pass's log holds the line.
2215said() {
2216 grep -qxF "$2" "$r/$1.log" && return
2217 echo "valley-request: the pass $1 did not say: $2" >&2
2218 cat "$r/$1.log" >&2
2219 exit 1
2220}
2221
2222# 21a. Evidence precedes approval. The worker builds the nix checks a ready
2223# change requires, together, and pushes the one attestation ref
2224# attest stored, before any request exists. A change whose only check
2225# is a command check is left to approve. The next pass finds the
2226# evidence on origin and does nothing. approve, with no signing key,
2227# reuses all of it and runs nothing, and the real integrator lands it.
2228readied lively live/state.txt
2229livelyline="$line"
2230readied early docs/readme.md src/main.txt
2231earlydigest="$(attest digest --rev early | cut -d: -f2)"
2232earlyref="refs/the-valley/attestations/$earlydigest/$workerhash"
2233fresh
2234working early
2235said early "$line: building code-build, prose-format"
2236said early "$line: published $earlyref"
2237said early "$livelyline: requires no nix check"
2238built code-build prose-format
2239pushed "$earlyref"
2240git -C "$served" cat-file -e "$earlyref:code-build/statement.note"
2241git -C "$served" cat-file -e "$earlyref:prose-format/statement.note"
2242[ -z "$(requested "c-$id")" ] || {
2243 echo "valley-request: a request existed before the worker's evidence" >&2
2244 exit 1
2245}
2246fresh
2247working early-again
2248said early-again "$line: origin holds evidence for every nix check it requires"
2249built nothing
2250pushed none
2251approving early env -u VALLEY_ATTEST_KEY valley approve -v --head "$head" "$handle"
2252grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/early.out"
2253grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/early.out"
2254built nothing
2255[ "$(requested "c-$id")" = "$head" ]
2256integrated "c-$id" "$r/worker.projects"
2257
2258# 21b. Concurrent passes take turns. The first holds the lock while it
2259# builds, and a stand-in hook keeps it building until the second says
2260# it is waiting. The second then reads origin after the first has
2261# published, and builds nothing.
2262readied together src/main.txt
2263togetherref="refs/the-valley/attestations/$(attest digest --rev together | cut -d: -f2)/$workerhash"
2264cat > "$r/hold" <<EOF
2265#!$(command -v bash)
2266[ -e "$r/hold-armed" ] || exit 0
2267rm -f "$r/hold-armed"
2268for _ in \$(seq 600); do
2269 grep -q 'waiting for it to end' "$r/together-second.log" 2> /dev/null && exit 0
2270 sleep 0.1
2271done
2272echo "the second pass never waited for the first" >&2
2273exit 1
2274EOF
2275chmod +x "$r/hold"
2276touch "$r/hold-armed"
2277fresh
2278working together-first VALLEY_E2E_DURING_PASS="$r/hold" &
2279first=$!
2280for _ in $(seq 600); do
2281 grep -q ': building ' "$r/together-first.log" 2> /dev/null && break
2282 sleep 0.1
2283done
2284working together-second
2285wait "$first"
2286said together-second "valley: another pass holds $wstate/lock; waiting for it to end"
2287said together-first "$line: published $togetherref"
2288said together-second "$line: origin holds evidence for every nix check it requires"
2289built code-build
2290pushed "$togetherref"
2291
2292# 21c. A failed batch backs off. Every nix call fails while the stand-in
2293# is told to fail. The pass reports the failure, publishes nothing,
2294# and exits 1. The batch is not built again until its wait, 15
2295# minutes, is over, then it fails again and waits twice as long. Once
2296# it passes it is published, and the record of it is gone.
2297readied flaky docs/readme.md
2298flakyref="refs/the-valley/attestations/$(attest digest --rev flaky | cut -d: -f2)/$workerhash"
2299fresh
2300if working flaky-1 VALLEY_NOW=2026-10-22T10:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2301 echo "valley-request: a pass whose batch failed exited 0" >&2
2302 exit 1
2303fi
2304said flaky-1 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:15:00Z"
2305pushed none
2306jq -e '.projects.project | length == 1 and (.[] | .failures == 1)' "$wstate/failures.json" > /dev/null
2307fresh
2308working flaky-2 VALLEY_NOW=2026-10-22T10:14:59Z
2309said 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"
2310built nothing
2311pushed none
2312if working flaky-3 VALLEY_NOW=2026-10-22T10:15:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2313 echo "valley-request: a pass whose batch failed again exited 0" >&2
2314 exit 1
2315fi
2316said flaky-3 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:45:00Z"
2317fresh
2318working flaky-4 VALLEY_NOW=2026-10-22T10:45:00Z
2319said flaky-4 "$line: building prose-format"
2320said flaky-4 "$line: published $flakyref"
2321built prose-format
2322pushed "$flakyref"
2323[ ! -e "$wstate/failures.json" ] || {
2324 echo "valley-request: the record of a batch that passed was kept" >&2
2325 cat "$wstate/failures.json" >&2
2326 exit 1
2327}
2328
2329# 21d. A lost reply. The worker's push is taken by a receive-pack that
2330# exits 1 when it is done, so origin lands the ref and the worker hears
2331# a failure: the pass says so, and exits 1. The next pass finds the
2332# evidence on origin, and builds and pushes nothing. Then a push that
2333# never reaches origin: once its one-minute wait is over, the next
2334# pass pushes the ref the first one stored, the same object, and
2335# builds nothing.
2336cat > "$r/lossy" <<EOF
2337#!$(command -v bash)
2338git-receive-pack "\$@"
2339echo "the connection dropped" >&2
2340exit 1
2341EOF
2342cat > "$r/unreached" <<EOF
2343#!$(command -v bash)
2344echo "the connection dropped" >&2
2345exit 1
2346EOF
2347chmod +x "$r/lossy" "$r/unreached"
2348readied landed docs/readme.md
2349landedref="refs/the-valley/attestations/$(attest digest --rev landed | cut -d: -f2)/$workerhash"
2350if 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
2351 echo "valley-request: a pass that heard its push fail exited 0" >&2
2352 exit 1
2353fi
2354said landed-1 "$line: could not publish $landedref; it is not pushed again before 2026-10-23T09:01:00Z"
2355git -C "$served" cat-file -e "$landedref:prose-format/statement.note"
2356fresh
2357working landed-2 VALLEY_NOW=2026-10-23T09:00:00Z
2358said landed-2 "$line: origin holds evidence for every nix check it requires"
2359built nothing
2360pushed none
2361readied unreached docs/readme.md
2362unreachedref="refs/the-valley/attestations/$(attest digest --rev unreached | cut -d: -f2)/$workerhash"
2363if 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
2364 echo "valley-request: a pass whose push never arrived exited 0" >&2
2365 exit 1
2366fi
2367if git -C "$served" rev-parse --verify --quiet "$unreachedref" > /dev/null; then
2368 echo "valley-request: a push that never arrived landed" >&2
2369 exit 1
2370fi
2371# The clone the worker reads the project through: keyed by its URL, which
2372# is the owning valley's with its last segment replaced (status/list.go).
2373cache="$HOME/.cache/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2374storedobject="$(git -C "$cache" rev-parse "$unreachedref")"
2375fresh
2376working unreached-2 VALLEY_NOW=2026-10-23T09:01:00Z
2377said unreached-2 "$line: an earlier pass stored $unreachedref, and origin does not hold it"
2378said unreached-2 "$line: published $unreachedref"
2379built nothing
2380pushed "$unreachedref"
2381[ "$(git -C "$served" rev-parse "$unreachedref")" = "$storedobject" ]
2382# A tag in the cache clone named like the ref the worker stores a
2383# change's evidence at, while that ref is missing. The worker reads its
2384# own ref by that exact name, so it does not take the tag for evidence an
2385# earlier pass stored: it builds the change and publishes what attest
2386# stored.
2387readied tagged docs/readme.md
2388taggedref="refs/the-valley/attestations/$(attest digest --rev tagged | cut -d: -f2)/$workerhash"
2389git -C "$cache" update-ref "refs/tags/$taggedref" "$storedobject"
2390fresh
2391working tagged VALLEY_NOW=2026-10-23T09:02:00Z && taggedpass=0 || taggedpass=$?
2392if grep -qF "an earlier pass stored $taggedref" "$r/tagged.log"; then
2393 echo "valley-request: the worker took a tag named like its own ref for evidence an earlier pass stored" >&2
2394 cat "$r/tagged.log" >&2
2395 exit 1
2396fi
2397if [ "$taggedpass" != 0 ]; then
2398 echo "valley-request: the pass with a tag named like the worker's own ref exited $taggedpass" >&2
2399 cat "$r/tagged.log" >&2
2400 exit 1
2401fi
2402said tagged "$line: published $taggedref"
2403pushed "$taggedref"
2404
2405# 21e. A head superseded while it is built. A stand-in hook readies a new
2406# version of the change in the middle of the batch. The batch runs to
2407# its end, and the pass publishes the evidence for the head it pinned,
2408# which the hook admits, since it is true of that tree. The pass then
2409# reads the change again and builds the new version, which approve
2410# reuses, and the real integrator lands. The next pass builds nothing.
2411readied superseded docs/readme.md
2412oldline="$line"
2413oldref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash"
2414cat > "$r/supersede" <<EOF
2415#!$(command -v bash)
2416[ -e "$r/supersede-armed" ] || exit 0
2417rm -f "$r/supersede-armed"
2418cd "$proj"
2419echo again >> docs/readme.md
2420git commit --quiet -am 'superseded, again'
2421VALLEY_PRINCIPAL=requester git push --quiet origin superseded
2422VALLEY_PRINCIPAL=requester valley ready --change "$id" superseded > "$r/superseded-v2.ready" 2>&1
2423# Its own pushes are not the worker's.
2424: > "$reusepushes"
2425EOF
2426chmod +x "$r/supersede"
2427touch "$r/supersede-armed"
2428fresh
2429working superseded-1 VALLEY_E2E_DURING_PASS="$r/supersede"
2430grep -qx "project $handle v2 ready" "$r/superseded-v2.ready"
2431said superseded-1 "$oldline: published $oldref"
2432newhead="$(git rev-parse superseded)"
2433newref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash"
2434said superseded-1 "project: $handle v2 at ${newhead:0:7}: published $newref"
2435if [ "$(grep -c '^--- push' "$reusepushes")" != 2 ] || ! grep -q " $oldref\$" "$reusepushes" \
2436 || ! grep -q " $newref\$" "$reusepushes"; then
2437 echo "valley-request: the pass did not publish the superseded version and then the new one" >&2
2438 cat "$reusepushes" "$r/superseded-1.log" >&2
2439 exit 1
2440fi
2441fresh
2442working superseded-2
2443said superseded-2 "project: $handle v2 at ${newhead:0:7}: origin holds evidence for every nix check it requires"
2444built nothing
2445pushed none
2446approving superseded env -u VALLEY_ATTEST_KEY valley approve -v --head "$newhead" "$handle"
2447built nothing
2448[ "$(requested "c-$id")" = "$newhead" ]
2449integrated "c-$id" "$r/worker.projects"
2450
2451# 21f. Origin already holds the worker's own ref for a tree, without one of
2452# the checks the change requires. A worker publishes a tree's
2453# evidence once, so the pass says so and builds nothing. approve without
2454# --build-here files the request at once, naming the missing check,
2455# and builds nothing. The real integrator holds that request as stale
2456# for want of evidence, and keeps it. approve --build-here then builds
2457# the rest under the operator's key and publishes the evidence alone,
2458# since the request already stands at that head. The integrator,
2459# judging again because evidence arrived, lands it on the two signers'
2460# evidence.
2461readied worker-occupied docs/readme.md src/main.txt
2462occupiedref="refs/the-valley/attestations/$(attest digest --rev worker-occupied | cut -d: -f2)/$workerhash"
2463attest run --rev worker-occupied --key "$r/keys/worker" --name "$workername" --check prose-format > "$r/worker-occupied-attest.out" 2>&1
2464git push --quiet origin "$occupiedref:$occupiedref"
2465fresh
2466working worker-occupied
2467said worker-occupied "$line: origin already holds $occupiedref, this worker's evidence for its tree, and it does not cover code-build. A worker publishes a tree's evidence once, so approve --build-here builds the rest (bd-7b7d58f)"
2468built nothing
2469pushed none
2470approving worker-occupied-filed valley approve -v --head "$head" "$handle"
2471grep -q "^◐ $handle v1 approved · lands when 1 check passes ([0-9]*/[0-9]* passed)$" "$w/worker-occupied-filed.out"
2472grep -qx ' missing code-build' "$w/worker-occupied-filed.out"
2473built nothing
2474if [ "$(requested "c-$id")" != "$head" ] || grep -q ' refs/the-valley/attestations/' "$reusepushes"; then
2475 echo "valley-request: approve without --build-here did not file the request alone" >&2
2476 cat "$w/worker-occupied-filed.out" "$w/worker-occupied-filed.err" "$reusepushes" >&2
2477 exit 1
2478fi
2479integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \
2480 --known-signers "$r/known-signers" --signer-projects "$r/worker.projects" --instance "$r/floor" --schema "$schemaFile" \
2481 --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || {
2482 echo "valley-request: the integrator failed" >&2
2483 cat "$r/integrator.out" >&2
2484 exit 1
2485}
2486if ! grep -q "^c-$id -> refs/heads/main @ [0-9a-f]*: stale\$" "$r/integrator.out" \
2487 || ! grep -qE '^ check +code-build +no attestation for this check accompanies the change$' "$r/integrator.out" \
2488 || [ "$(requested "c-$id")" != "$head" ]; then
2489 echo "valley-request: the integrator did not keep a request missing evidence as stale" >&2
2490 cat "$r/integrator.out" >&2
2491 exit 1
2492fi
2493approving worker-occupied valley approve -v --build-here --head "$head" "$handle"
2494built code-build
2495grep -qx ' the request already stood at this head; the evidence is what was published' "$w/worker-occupied.out"
2496if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \
2497 || ! grep -q " refs/the-valley/attestations/[0-9a-f]*/$operatorhash\$" "$reusepushes" \
2498 || grep -q ' refs/the-valley/integration-requests/' "$reusepushes"; then
2499 echo "valley-request: approve --build-here over a standing request did not publish the evidence alone" >&2
2500 cat "$reusepushes" >&2
2501 exit 1
2502fi
2503integrated "c-$id" "$r/worker.projects"
2504
2505# 21g. A read of origin that fails stops the pass, before anything is
2506# built: with the project's origin away, the pass exits 1 and says
2507# so, and the ready change is left for a pass that can read it.
2508readied unread-origin docs/readme.md
2509mv "$served" "$served.away"
2510fresh
2511if working unread-origin; then
2512 echo "valley-request: a pass went on past an origin it could not read" >&2
2513 exit 1
2514fi
2515mv "$served.away" "$served"
2516grep -q '^valley: project: .*; the pass stops here$' "$r/unread-origin.log"
2517built nothing
2518fresh
2519working unread-origin-again
2520said unread-origin-again "$line: building prose-format"
2521
2522# await <file> <text>: wait up to a minute for the file to hold the text.
2523await() {
2524 for _ in $(seq 600); do
2525 grep -qF -- "$2" "$1" 2> /dev/null && return
2526 sleep 0.1
2527 done
2528 echo "valley-request: $1 never said: $2" >&2
2529 exit 1
2530}
2531
2532# 21h. The wait after a failed batch runs from when the batch ended. The
2533# clock is not fixed here. The stand-in takes 3 seconds and then
2534# fails, so the batch's wait ends at least 15 minutes after it ended,
2535# which is more than 15 minutes after the pass began. Past the wait,
2536# the batch is built and published.
2537readied slow docs/readme.md
2538slowdigest="$(attest digest --rev slow | cut -d: -f2)"
2539began="$(date -u +%s)"
2540fresh
2541if working slow-1 VALLEY_E2E_DURING_PASS='sleep 3; exit 1'; then
2542 echo "valley-request: a pass whose batch failed exited 0" >&2
2543 exit 1
2544fi
2545retry="$(jq -r --arg key "build $slowdigest prose-format" '.projects.project[$key].retry' "$wstate/failures.json")"
2546if [ "$(($(date -u -d "$retry" +%s) - began))" -lt 903 ]; then
2547 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
2548 exit 1
2549fi
2550fresh
2551working slow-2 VALLEY_NOW=2099-01-01T00:00:00Z
2552said slow-2 "$line: published refs/the-valley/attestations/$slowdigest/$workerhash"
2553
2554# 21i. A failed push waits too, by the object it would publish. Two changes
2555# whose heads hold the same tree share one attestation ref, and origin
2556# takes no push. The first pass builds once and tries one push. It
2557# then reads both changes again, and each one's push waits behind that
2558# failure. A pass within the minute tries no push. The next failure doubles the wait. Past it, the
2559# ref is published once, and the record of it is gone.
2560cat > "$r/counted" <<EOF
2561#!$(command -v bash)
2562echo push >> "$r/push-attempts"
2563echo "the connection dropped" >&2
2564exit 1
2565EOF
2566chmod +x "$r/counted"
2567refusing=(GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/counted")
2568readied twin-a docs/readme.md
2569git checkout --quiet -B twin-b origin/main
2570git checkout --quiet twin-a -- docs/readme.md
2571git commit --quiet -m twin-b
2572VALLEY_PRINCIPAL=requester git push --quiet origin twin-b
2573VALLEY_PRINCIPAL=requester valley ready twin-b > "$r/twin-b.ready" 2>&1
2574[ "$(git rev-parse 'twin-a^{tree}')" = "$(git rev-parse 'twin-b^{tree}')" ]
2575twinref="refs/the-valley/attestations/$(attest digest --rev twin-a | cut -d: -f2)/$workerhash"
2576# attempts <n> <name> <failed> <waiting>: origin was offered n pushes in
2577# all, and the pass's log says failed and waiting for the twins' ref that
2578# many times each, with the wait ending at the time given last.
2579attempts() {
2580 local until="$5"
2581 if [ "$(wc -l < "$r/push-attempts")" != "$1" ] \
2582 || [ "$(grep -c ": could not publish $twinref; it is not pushed again before $until\$" "$r/$2.log")" != "$3" ] \
2583 || [ "$(grep -c ": publishing $twinref failed [0-9]* times\? in a row, so it is not pushed again before $until\$" "$r/$2.log")" != "$4" ]; then
2584 echo "valley-request: the pass $2 did not offer origin the twins' ref as often as expected" >&2
2585 cat "$r/push-attempts" "$r/$2.log" >&2
2586 exit 1
2587 fi
2588}
2589: > "$r/push-attempts"
2590fresh
2591if working twins-1 VALLEY_NOW=2026-10-24T09:00:00Z "${refusing[@]}"; then
2592 echo "valley-request: a pass whose push failed exited 0" >&2
2593 exit 1
2594fi
2595built prose-format
2596attempts 1 twins-1 1 2 2026-10-24T09:01:00Z
2597working twins-2 VALLEY_NOW=2026-10-24T09:00:59Z "${refusing[@]}"
2598attempts 1 twins-2 0 2 2026-10-24T09:01:00Z
2599if working twins-3 VALLEY_NOW=2026-10-24T09:01:00Z "${refusing[@]}"; then
2600 echo "valley-request: a pass whose push failed again exited 0" >&2
2601 exit 1
2602fi
2603attempts 2 twins-3 1 1 2026-10-24T09:03:00Z
2604fresh
2605working twins-4 VALLEY_NOW=2026-10-24T09:03:00Z
2606built nothing
2607pushed "$twinref"
2608[ ! -e "$wstate/failures.json" ] || {
2609 echo "valley-request: the record of a push that went through was kept" >&2
2610 cat "$wstate/failures.json" >&2
2611 exit 1
2612}
2613
2614# 21j. A pass forgets only the failures of the projects it read whole. The
2615# host serves a second project, other, with no ready change. A batch of
2616# this project fails. A pass over other alone leaves that wait as it
2617# is, so the next pass over this project does not build the batch.
2618git init --quiet --bare "$r/other.git"
2619git -C "$instance" update-ref refs/the-valley/projects \
2620 "$(printf 'other\nproject\n' | git -C "$instance" hash-object -w --stdin)"
2621readied flapping docs/readme.md
2622flappingref="refs/the-valley/attestations/$(attest digest --rev flapping | cut -d: -f2)/$workerhash"
2623if working flapping-1 VALLEY_NOW=2026-10-25T09:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then
2624 echo "valley-request: a pass whose batch failed exited 0" >&2
2625 exit 1
2626fi
2627(cd "$r/worker-home" && VALLEY_PRINCIPAL=worker VALLEY_NOW=2026-10-25T09:01:00Z valley-worker --repo other \
2628 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/other.log" 2>&1 || {
2629 echo "valley-request: a pass over other failed" >&2
2630 cat "$r/other.log" >&2
2631 exit 1
2632}
2633fresh
2634working flapping-2 VALLEY_NOW=2026-10-25T09:02:00Z
2635said 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"
2636built nothing
2637fresh
2638working flapping-3 VALLEY_NOW=2026-10-25T09:15:00Z
2639said flapping-3 "$line: published $flappingref"
2640
2641# 21k. A pass killed while attest runs. attest holds the lock it was
2642# handed, so a second pass waits for attest to end rather than build
2643# the same batch beside it. Then the second pass finds the ref the
2644# orphaned attest stored, publishes it, and builds nothing.
2645readied killed docs/readme.md
2646killedref="refs/the-valley/attestations/$(attest digest --rev killed | cut -d: -f2)/$workerhash"
2647cat > "$r/hold-kill" <<EOF
2648#!$(command -v bash)
2649[ -e "$r/kill-armed" ] || exit 0
2650rm -f "$r/kill-armed"
2651# The worker is the nearest process above this one named valley-go.
2652p=\$\$
2653while [ "\$p" -gt 1 ]; do
2654 if [ "\$(cat "/proc/\$p/comm")" = valley-go ]; then
2655 echo "\$p" > "$r/killed.pid"
2656 break
2657 fi
2658 read -r _ _ _ p _ < "/proc/\$p/stat"
2659done
2660for _ in \$(seq 600); do
2661 [ -e "$r/release" ] && exit 0
2662 sleep 0.1
2663done
2664exit 1
2665EOF
2666chmod +x "$r/hold-kill"
2667touch "$r/kill-armed"
2668fresh
2669working killed-1 VALLEY_E2E_DURING_PASS="$r/hold-kill" &
2670killedpass=$!
2671await "$r/killed.pid" ""
2672kill -9 "$(cat "$r/killed.pid")"
2673status=0
2674wait "$killedpass" || status=$?
2675[ "$status" = 137 ] || {
2676 echo "valley-request: the killed pass ended with $status" >&2
2677 exit 1
2678}
2679working killed-2 &
2680secondpass=$!
2681await "$r/killed-2.log" "valley: another pass holds $wstate/lock; waiting for it to end"
2682touch "$r/release"
2683wait "$secondpass"
2684said killed-2 "$line: an earlier pass stored $killedref, and origin does not hold it"
2685said killed-2 "$line: published $killedref"
2686built prose-format
2687pushed "$killedref"
2688
2689# 21l. A cache clone whose making was interrupted. A stand-in for git takes
2690# the clone's config.lock as it sets the origin, kills the worker, and
2691# dies holding the lock, as git killed mid-write does. The next pass
2692# makes the clone again and reads the project through it. The worker
2693# runs as valley-go here, since the package's wrapper puts its own git
2694# ahead of the stand-in.
2695cachehome="$r/interrupted-cache"
2696interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2697mkdir -p "$r/killing-git"
2698cat > "$r/killing-git/git" <<EOF
2699#!$(command -v bash)
2700if [ "\${1:-} \${2:-}" = "remote add" ]; then
2701 : > .git/config.lock
2702 kill -9 "\$PPID"
2703 kill -9 "\$\$"
2704fi
2705exec "$(command -v git)" "\$@"
2706EOF
2707chmod +x "$r/killing-git/git"
2708status=0
2709(cd "$r/worker-home" && VALLEY_PRINCIPAL=worker XDG_CACHE_HOME="$cachehome" PATH="$r/killing-git:$PATH" \
2710 VALLEY_VERIFICATION_SCHEMA="$schemaFile" valley-go work --repo project \
2711 --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/interrupted-1.log" 2>&1 || status=$?
2712if [ "$status" != 137 ] || [ -z "$(find "$cachehome" -name config.lock)" ]; then
2713 echo "valley-request: the stand-in did not interrupt the clone's making while holding its config.lock ($status)" >&2
2714 cat "$r/interrupted-1.log" >&2
2715 exit 1
2716fi
2717# recovered <name>: a pass over the cache at $cachehome succeeds, and the
2718# clone it reads the project through names origin.
2719recovered() {
2720 fresh
2721 working "$1" XDG_CACHE_HOME="$cachehome" || {
2722 echo "valley-request: the pass after an interrupted clone failed" >&2
2723 cat "$r/$1.log" >&2
2724 exit 1
2725 }
2726 [ "$(git -C "$interrupted" config --get remote.origin.url)" = "$served" ]
2727 said "$1" "$livelyline: requires no nix check"
2728 built nothing
2729}
2730recovered interrupted-2
2731
2732# 21m. A clone left half made in its place, as an older version left one:
2733# a repository with no origin, and a stale config.lock beside its
2734# configuration. The next pass sets it aside, makes the clone again,
2735# and reads the project through it.
2736cachehome="$r/half-made-cache"
2737interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project"
2738git init --quiet "$interrupted"
2739: > "$interrupted/.git/config.lock"
2740recovered half-made
2741[ ! -e "$interrupted/.git/config.lock" ]
2742
2743worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
2744if [ "$worktrees" != 1 ]; then
2745 echo "valley-request: a throwaway worktree was left behind in the reuse project" >&2
2746 git worktree list >&2
2747 exit 1
2748fi
2749
2750# ----------------------------------------------------------------------
2751# 22. approve reads each ref by its exact name. Given a ref name that is
2752# missing, git goes on to try other names for it, refs/tags/<name>
2753# and refs/heads/<name> among them. ls-remote and fetch take a ref
2754# whose name ends in the one asked for. Any pusher can make such a
2755# ref, and a fetch brings a tag along with the commits it names. Each
2756# case leaves a ref missing, makes the ref git would take for it, and
2757# shows that approve treats the ref as missing. Every branch here
2758# touches quiet/ alone, so nothing is required and no key is needed.
2759# The refs are read here with show-ref, which reads that exact name.
2760lookorigin="$TMPDIR/lookalike.git"
2761git init --quiet --bare "$lookorigin"
2762look="$TMPDIR/lookalike"
2763git init --quiet "$look"
2764cd "$look" || exit 1
2765git remote add origin "$lookorigin"
2766mkdir -p docs policy
2767echo "the readme" > docs/readme.md
2768echo "$valley" > policy/valley
2769git add -A
2770git commit --quiet -m base
2771git push --quiet origin main
2772lookbase="$(git rev-parse main)"
2773quiet_branch() {
2774 git checkout --quiet -b "topic/$1" "$2"
2775 mkdir -p quiet
2776 echo "a quiet note for $1" > quiet/note.md
2777 git add -A
2778 git commit --quiet -m "a quiet note for $1"
2779 git push --quiet origin "topic/$1"
2780 git checkout --quiet main
2781}
2782# The request ref approve filed on lookorigin, or nothing.
2783filed() {
2784 git -C "$lookorigin" show-ref --verify --hash \
2785 "refs/the-valley/integration-requests/main/topic-$1" 2> /dev/null || true
2786}
2787
2788# A request ref that is missing, with a branch on origin named like it at
2789# the head approved. Read through that branch, the request would already
2790# stand at this head, and approve would report success and file nothing.
2791# git push matches its destination the same way, so a push to the
2792# request ref would write that branch, and approve refuses to push at
2793# all while it stands. Once it is deleted, approve files the request.
2794quiet_branch lookalike main
2795lookhead="$(git rev-parse topic/lookalike)"
2796lookref=refs/heads/refs/the-valley/integration-requests/main/topic-lookalike
2797git -C "$lookorigin" update-ref "$lookref" "$lookhead"
2798if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/lookalike < /dev/null \
2799 > "$w/lookalike.out" 2> "$w/lookalike.err"; then
2800 echo "valley-request: approve reported success with a branch named like the request it files" >&2
2801 cat "$w/lookalike.out" >&2
2802 exit 1
2803fi
2804grep -qF "⊘ topic/lookalike not approved · origin holds $lookref, " "$w/lookalike.err"
2805grep -qxF " delete $lookref on origin" "$w/lookalike.err"
2806[ -z "$(filed lookalike)" ] || {
2807 echo "valley-request: approve filed a request past a branch named like it" >&2
2808 exit 1
2809}
2810git -C "$lookorigin" update-ref -d "$lookref"
2811env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/lookalike < /dev/null \
2812 > "$w/lookalike-again.out" 2> "$w/lookalike-again.err"
2813if [ "$(filed lookalike)" != "$lookhead" ]; then
2814 echo "valley-request: with the branch named like it gone, approve filed no request" >&2
2815 cat "$w/lookalike-again.out" "$w/lookalike-again.err" >&2
2816 exit 1
2817fi
2818
2819# A branch that is not on origin, with a tag origin carries named like its
2820# remote-tracking ref. The fetch brings the tag, and read through it the
2821# branch would be approved at the tag's commit.
2822git -C "$lookorigin" update-ref refs/tags/refs/remotes/origin/topic/gone "$lookhead"
2823if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/gone < /dev/null \
2824 > "$w/gone.out" 2> "$w/gone.err"; then
2825 echo "valley-request: approve filed a branch origin does not have, at a tag named like it" >&2
2826 exit 1
2827fi
2828# The verb refuses it itself, before approve reads the branch again.
2829grep -qx 'valley: no such branch on origin: topic/gone' "$w/gone.err"
2830git show-ref --verify --quiet refs/tags/refs/remotes/origin/topic/gone || {
2831 echo "valley-request: the fetch did not bring the tag, so nothing was tested" >&2
2832 exit 1
2833}
2834[ -z "$(filed gone)" ] || {
2835 echo "valley-request: approve filed a request for a branch origin does not have" >&2
2836 exit 1
2837}
2838if valley review topic/gone < /dev/null > "$w/gone-review.out" 2> "$w/gone-review.err"; then
2839 echo "valley-request: review went on with a branch origin does not have" >&2
2840 exit 1
2841fi
2842# review refuses it before it reads the stack, and says nothing else.
2843if [ "$(cat "$w/gone-review.err")" != 'valley: no such branch on origin: topic/gone' ]; then
2844 echo "valley-request: review read a branch origin does not have before refusing it:" >&2
2845 cat "$w/gone-review.err" >&2
2846 exit 1
2847fi
2848
2849# An owning valley with no main, and a branch on it named
2850# refs/heads/main, which git stores as refs/heads/refs/heads/main. Fetched
2851# through that branch, the floor would come from it.
2852lookvalley="$TMPDIR/lookalike-valley.git"
2853git init --quiet --bare "$lookvalley"
2854git -C "$valleywork" push --quiet "$lookvalley" main:refs/heads/refs/heads/main
2855echo "$lookvalley" > policy/valley
2856git commit --quiet -am "a valley with no main"
2857git push --quiet origin main
2858quiet_branch floorless main
2859if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/floorless < /dev/null \
2860 > "$w/floorless.out" 2> "$w/floorless.err"; then
2861 echo "valley-request: approve read a floor from a valley with no main" >&2
2862 exit 1
2863fi
2864grep -qF "the owning valley $lookvalley has no refs/heads/main" "$w/floorless.err"
2865[ -z "$(filed floorless)" ] || {
2866 echo "valley-request: approve filed a request under a valley with no main" >&2
2867 exit 1
2868}
2869
2870# origin with no main, and a tag origin carries named like main's
2871# remote-tracking ref, at a commit whose floor requires nothing of this
2872# branch. Read through the tag, approve would derive the policy from it
2873# and file the request.
2874quiet_branch mainless "$lookbase"
2875git -C "$lookorigin" update-ref refs/tags/refs/remotes/origin/main "$lookbase"
2876git -C "$lookorigin" update-ref -d refs/heads/main
2877if env -u VALLEY_ATTEST_KEY valley approve -v origin/topic/mainless < /dev/null \
2878 > "$w/mainless.out" 2> "$w/mainless.err"; then
2879 echo "valley-request: approve filed a request while origin has no main" >&2
2880 exit 1
2881fi
2882grep -qx '⊘ topic/mainless not approved · origin has no main to approve onto' "$w/mainless.err"
2883git show-ref --verify --quiet refs/tags/refs/remotes/origin/main || {
2884 echo "valley-request: the fetch did not bring the tag, so nothing was tested" >&2
2885 exit 1
2886}
2887[ -z "$(filed mainless)" ] || {
2888 echo "valley-request: approve filed a request while origin has no main" >&2
2889 exit 1
2890}
2891cd "$repo" || exit 1
2892
2893# ----------------------------------------------------------------------
2894# 23. review's [a]pprove runs valley-go approve on review's terminal, and
2895# approve tells review how the attempt ended. In a walk, review passes
2896# it on as the step's outcome: approved with the head filed, and
2897# refused after a failed check. A ctrl-C during the checks stops them,
2898# removes their worktree, records the attempt's end as interrupted and
2899# exits 130, from the approve verb and from review alike. The check
2900# that holds until it is stopped is main's for held/, which nothing
2901# after this scenario touches.
2902git fetch --quiet origin
2903for branch in walked leaked; do
2904 git checkout --quiet -b "topic/$branch" origin/main
2905 echo "a readme reviewed in a walk, $branch" > docs/readme.md
2906 [ "$branch" != leaked ] || { mkdir -p src && echo SECRET > src/leak.txt; }
2907 git add -A
2908 git commit --quiet -m "a readme reviewed in a walk, $branch"
2909 git push --quiet origin "topic/$branch"
2910done
2911git checkout --quiet main
2912printf 'a\n' > "$w/answers"
2913VALLEY_WALK_OUTCOME="$w/walked.outcome" valley review topic/walked < "$w/answers" \
2914 > "$w/walked.out" 2> "$w/walked.err"
2915if [ "$(cat "$w/walked.outcome")" != "$(printf 'approved\t\t\t\t%s' "$(git rev-parse topic/walked)")" ]; then
2916 echo "valley-request: review's outcome for a plain branch it approved is $(cat "$w/walked.outcome")" >&2
2917 exit 1
2918fi
2919printf 'a\ns\n' > "$w/answers"
2920VALLEY_WALK_OUTCOME="$w/leaked.outcome" valley review topic/leaked < "$w/answers" \
2921 > "$w/leaked.out" 2> "$w/leaked.err"
2922grep -qxF "topic/leaked · checks failed at $(git rev-parse --short topic/leaked)" "$w/leaked.out"
2923[ "$(cat "$w/leaked.outcome")" = refused ] || {
2924 echo "valley-request: review's outcome after a failed check is $(cat "$w/leaked.outcome")" >&2
2925 exit 1
2926}
2927
2928held="$TMPDIR/held"
2929git checkout --quiet -b held-policy origin/main
2930printf '\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' \
2931 "touch $held && sleep 600" >> policy/project/policy.cue
2932git commit --quiet -am "a check that holds until it is stopped"
2933git push --quiet origin HEAD:main
2934git checkout --quiet -b topic/held
2935mkdir held
2936echo "held" > held/readme.md
2937git add held
2938git commit --quiet -m "a change the held check gates"
2939git push --quiet origin topic/held
2940git checkout --quiet main
2941
2942# ctrl_c <name> <stdin> <command…>: run the command as a terminal runs a
2943# foreground job, in a process group of its own, and once the held check
2944# has started send the group SIGINT, as ctrl-C does. The command's status
2945# is left in stopped.
2946ctrl_c() {
2947 local name="$1" input="$2" pid
2948 shift 2
2949 rm -f "$held"
2950 setsid "$@" < "$input" > "$w/$name.out" 2> "$w/$name.err" &
2951 pid=$!
2952 until [ -e "$held" ] || ! kill -0 "$pid" 2> /dev/null; do sleep 0.1; done
2953 kill -INT -- "-$pid" 2> /dev/null || true
2954 stopped=0
2955 wait "$pid" || stopped=$?
2956}
2957# left_behind <who>: nothing of the interrupted approval is left: no
2958# worktree, no scratch directory, no request.
2959left_behind() {
2960 local worktrees scratch=("$TMPDIR"/valley-approve-*)
2961 worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
2962 if [ "$worktrees" != 1 ] || [ -e "${scratch[0]}" ]; then
2963 echo "valley-request: the checks' worktree outlived a ctrl-C ($1)" >&2
2964 git worktree list >&2
2965 exit 1
2966 fi
2967 if git -C "$origin" rev-parse --verify --quiet refs/the-valley/integration-requests/main/topic-held > /dev/null; then
2968 echo "valley-request: an approval interrupted during its checks filed a request ($1)" >&2
2969 exit 1
2970 fi
2971}
2972ctrl_c held-verb /dev/null env XDG_STATE_HOME="$w/held-state" GIT_CONFIG_COUNT=2 \
2973 GIT_CONFIG_KEY_0=valley.telemetry GIT_CONFIG_VALUE_0=true \
2974 GIT_CONFIG_KEY_1=valley.telemetry.remote GIT_CONFIG_VALUE_1=ssh://valley-host.invalid/project \
2975 valley approve -v origin/topic/held
2976[ "$stopped" = 130 ] || {
2977 echo "valley-request: the approve verb exited $stopped after a ctrl-C during its checks" >&2
2978 cat "$w/held-verb.out" "$w/held-verb.err" >&2
2979 exit 1
2980}
2981left_behind "the approve verb"
2982grep -rh '"type":"approve.end"' "$w/held-state" | grep -qF '"result":"interrupted"' || {
2983 echo "valley-request: an approval interrupted during its checks recorded no interrupted end" >&2
2984 exit 1
2985}
2986printf 'a\n' > "$w/answers"
2987mkdir "$w/held-tmp"
2988ctrl_c held-review "$w/answers" env TMPDIR="$w/held-tmp" VALLEY_WALK_OUTCOME="$w/held.outcome" valley review topic/held
2989[ "$stopped" = 130 ] && [ "$(cat "$w/held.outcome")" = interrupted ] || {
2990 echo "valley-request: review exited $stopped, with the outcome $(cat "$w/held.outcome"), after a ctrl-C during its checks" >&2
2991 exit 1
2992}
2993left_behind review
2994# Nor is anything of review's own: its pages, the approval's scratch, and
2995# attest run's tree.
2996if ls -A "$w/held-tmp" | grep .; then
2997 echo "valley-request: review left the above in its TMPDIR after a ctrl-C during its checks" >&2
2998 exit 1
2999fi
3000
3001# ----------------------------------------------------------------------
3002# The verbs the loop offers are the whole of what an operator can do to a
3003# branch, so no path may offer one outside the set: [a]pprove, [r]eject,
3004# [s]kip, and [v]iew full diff, which only shows. A change alone is never
3005# offered [b]ase. Every stream this check captured is scanned at the end,
3006# which is what makes the claim about paths rather than about the
3007# prompts pinned character for character above — the fast-forward prompt,
3008# the one under the behind-main line, and the ones a refused [a]pprove
3009# returns to are all in here. A verb that writes the protected ref from the
3010# operator's own checkout — the retired [i]ntegrate, or any successor to
3011# it — fails this wherever it is printed.
3012offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
3013outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]pprove|\[r\]eject|\[s\]kip|\[v\]iew' || true)"
3014if [ -n "$outside" ]; then
3015 echo "valley-request: a review path offered a verb outside the set:" >&2
3016 printf '%s\n' "$outside" >&2
3017 exit 1
3018fi
3019# The scan is only worth something if it saw the offers at all.
3020for verb in '[a]pprove' '[r]eject' '[s]kip' '[v]iew'; do
3021 printf '%s\n' "$offered" | grep -qxF "$verb" \
3022 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
3023done
3024
3025# No throwaway worktree outlived any of it: only the checkout is left.
3026worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
3027if [ "$worktrees" != 1 ]; then
3028 echo "valley-request: a throwaway worktree was left behind" >&2
3029 git worktree list >&2
3030 exit 1
3031fi
3032
3033echo "every scenario held"
3034{
3035 cat "$w/approve.out"
3036 printf '\n--- the scratch origin, after all of it ---\n'
3037 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
3038} > "$out"