gunk-dev/the-valley

valley-request.sh

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