gunk-dev/the-valley

valley-request.sh

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