gunk-dev/the-valley

valley-request.sh

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