gunk-dev/the-valley

valley-request.sh

source73e9a20c2d42main's published tip history
54.3 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=laddie.valley.invalid/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. Owing no check
91 // 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# What origin holds now. The attestation is keyed by the digest of the
191# BRANCH's tree, which is what says the checks ran over the right tree and
192# not over the operator's working copy.
193git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
194grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
195grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
196if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
197 != "$(git rev-parse topic/one)" ]; then
198 echo "valley-request: the request ref does not name the branch's head" >&2
199 exit 1
200fi
201if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
202 echo "valley-request: [a]pprove moved main" >&2
203 exit 1
204fi
205
206# One push carried both. This is the friction the verb exists to remove:
207# `attest run --push` publishes the branch and the evidence and knows
208# nothing about a request ref, so doing this by hand is two beats.
209pushes="$(grep -c '^--- push' "$pushlog" || true)"
210if [ "$pushes" != 1 ]; then
211 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
212 cat "$pushlog" >&2
213 exit 1
214fi
215grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
216grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
217
218# The note origin holds is the one the operator signed, over that tree.
219git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
220 > "$w/statement.note"
221attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
222attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
223 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
224grep -q 'matches the recorded subject digest' "$w/verify.out"
225
226# ----------------------------------------------------------------------
227# 2. The layers the derivation used are the ones that govern, and it says
228# which they were: the floor fetched from the owning valley, and the
229# project's own layer at policy/project in the TARGET's tip.
230# examples/policy/ is in this tree and requires `phantom` of every path,
231# and no default reaches it — it is derivable only by naming it, which
232# is what the options are for.
233valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err"
234grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out"
235grep -qx 'classes matched: prose' "$w/checks.out"
236grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out"
237if grep -q phantom "$w/checks.out" "$w/checks.err"; then
238 echo "valley-request: a default derived a check from examples/policy/" >&2
239 cat "$w/checks.out" "$w/checks.err" >&2
240 exit 1
241fi
242
243# Named, the example layers derive exactly what they declare. This is the
244# other half of the claim above: examples/policy/ is readable, and reading
245# it is something a person asks for.
246valley checks --instance examples/policy/instance --project examples/policy/project \
247 "$first" topic/one > "$w/example.out"
248grep -qE '^ phantom +mandatory' "$w/example.out"
249grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out"
250
251# ----------------------------------------------------------------------
252# 3. main moves on, so the next branch is stale. [a]pprove is offered there
253# too — the integrator applies a delta to the tip rather than
254# fast-forwarding — and a branch whose check fails publishes nothing
255# and leaves the review loop usable.
256git checkout --quiet main
257echo "a note" > docs/notes.md
258git add -A
259git commit --quiet -m "a landing that moves main"
260git push --quiet origin main
261
262git checkout --quiet -b topic/two "$first"
263printf 'SECRET\n' > src/leak.txt
264git add -A
265git commit --quiet -m "a leak"
266git push --quiet origin topic/two
267
268: > "$pushlog"
269printf 'a\ns\n' > "$w/answers"
270valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
271
272# The prompt is the whole of what the operator is offered, so it is pinned
273# character for character: a verb that stops being offered, or one that
274# starts, is a change to the review loop and not an incidental one.
275grep -qF 'topic/two does not fast-forward from main: [a]pprove, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out"
276grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
277grep -qE '^check no-secrets +command failed$' "$w/fail.out"
278grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
279# The prompt is printed without a newline, so what follows a refusal shares
280# its line — the answer to it is not anchored.
281grep -q 'skipped topic/two; nothing done' "$w/fail.out"
282if [ -s "$pushlog" ]; then
283 echo "valley-request: a failing check pushed something" >&2
284 cat "$pushlog" >&2
285 exit 1
286fi
287git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
288if grep -q 'topic-two' "$w/after.txt"; then
289 echo "valley-request: a failing check left a ref on origin" >&2
290 cat "$w/after.txt" >&2
291 exit 1
292fi
293
294# ----------------------------------------------------------------------
295# 4. No signing key, with a check owed: refused before any check is run,
296# and the prompt still comes back. The host signs, and there is no
297# unsigned mode.
298: > "$pushlog"
299printf 'a\ns\n' > "$w/answers"
300env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
301grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
302grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
303if [ -s "$pushlog" ]; then
304 echo "valley-request: a run with no signing key pushed something" >&2
305 exit 1
306fi
307
308# ----------------------------------------------------------------------
309# 5. A branch the policy requires no check of is filed as a request with
310# no evidence. The integrator lands one and records it as a landing
311# that required nothing, so refusing to file it here would strand
312# exactly the changes a policy leaves unrequired. Nothing is owed, so
313# nothing is signed: this runs with no signing key at all.
314git checkout --quiet main
315git checkout --quiet -b topic/quiet
316mkdir -p quiet
317echo "a quiet note" > quiet/note.md
318git add -A
319git commit --quiet -m "a change under a class that asks nothing"
320git push --quiet origin topic/quiet
321quiethead="$(git rev-parse --short topic/quiet)"
322
323: > "$pushlog"
324printf 'a\n' > "$w/answers"
325env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
326 > "$w/quiet.out" 2> "$w/quiet.err"
327
328grep -q 'owes no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
329grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
330grep -q '^Approved topic/quiet .*: its integration request for origin/main is filed\.$' "$w/quiet.out"
331grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
332grep -qx ' evidence none — nothing was owed, so nothing was signed' "$w/quiet.out"
333grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
334
335# One push, carrying the request and nothing else.
336pushes="$(grep -c '^--- push' "$pushlog" || true)"
337if [ "$pushes" != 1 ]; then
338 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
339 cat "$pushlog" >&2
340 exit 1
341fi
342if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
343 echo "valley-request: a request owing nothing published evidence" >&2
344 cat "$pushlog" >&2
345 exit 1
346fi
347if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
348 != "$(git rev-parse topic/quiet)" ]; then
349 echo "valley-request: the bare request does not name the branch's head" >&2
350 exit 1
351fi
352
353# ----------------------------------------------------------------------
354# 6. [b]ase, and the prompt it comes back to. A stale branch is rebased
355# onto main in a throwaway worktree and re-reviewed, and the prompt the
356# loop then prints is the fast-forward one — the path a reader of
357# bin/valley has to trace two loops to reach, so it is exercised here
358# rather than argued about.
359git checkout --quiet main
360git checkout --quiet -b topic/three "$first"
361echo "another readme" > docs/readme.md
362git commit --quiet -am "another readme"
363git push --quiet origin topic/three
364before_base="$(git -C "$origin" rev-parse refs/heads/main)"
365
366printf 'b\ns\n' > "$w/answers"
367valley review topic/three < "$w/answers" > "$w/base.out" 2>&1
368grep -qF 'topic/three does not fast-forward from main: [a]pprove, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out"
369grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out"
370# The post-[b]ase prompt: the branch fast-forwards now, so the loop falls
371# through to the prompt that has no [b]ase in it.
372grep -qF 'topic/three: [a]pprove, [r]eject, [s]kip? ' "$w/base.out"
373grep -q 'skipped topic/three; nothing done' "$w/base.out"
374git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \
375 || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; }
376if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then
377 echo "valley-request: the review loop moved main" >&2
378 exit 1
379fi
380git checkout --quiet main
381
382# The request filed in 1 is still pending: nothing here consumes it, which
383# is the integrator's own step.
384git -C "$origin" rev-parse --verify --quiet \
385 refs/the-valley/integration-requests/main/topic-one > /dev/null
386
387# ----------------------------------------------------------------------
388# 7. A change does not supply the policy that gates it. This branch
389# deletes its own policy/project and repoints policy/valley at a
390# repository that does not exist. Both layers come from the target's
391# tip, so neither edit reaches the derivation: it composes the same two
392# layers it would for any other branch, and the fetch that a branch-read
393# pointer would have attempted never happens.
394git checkout --quiet -b topic/nolayer
395git rm --quiet -r policy/project
396echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
397git add -A
398git commit --quiet -m "delete the project layer and repoint the valley"
399git push --quiet origin topic/nolayer
400
401valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
402grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
403if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
404 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
405 cat "$w/nolayer.out" "$w/nolayer.err" >&2
406 exit 1
407fi
408
409: > "$pushlog"
410printf 'a\n' > "$w/answers"
411valley review topic/nolayer < "$w/answers" \
412 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
413grep -q '^Approved topic/nolayer .*: its integration request for origin/main is filed\.$' "$w/nolayer.review.out"
414
415# ----------------------------------------------------------------------
416# 8. The reviewer's checkout contributes nothing. This is the case that
417# failed in live use: the operator's checkout was an older branch
418# without the landed policy layer, and the deriver read the working
419# tree and refused. topic/nolayer is exactly such a checkout — no
420# policy/project in it, and a policy/valley naming nowhere — so every
421# derivation below runs from it.
422#
423# Byte-identical output is the claim, not merely a successful one:
424# review works the same from any branch, however stale.
425git checkout --quiet main
426valley checks "$first" topic/one > "$w/from-main.out"
427git checkout --quiet topic/nolayer
428valley checks "$first" topic/one > "$w/from-stale.out"
429if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
430 echo "valley-request: the checkout changed what the derivation said" >&2
431 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
432 exit 1
433fi
434
435# And [a]pprove, which is where it failed: a request filed from that same
436# checkout, over a branch the stale tree knows nothing about.
437git checkout --quiet -b topic/four origin/main
438echo "one more readme" > docs/readme.md
439git commit --quiet -am "one more readme"
440git push --quiet origin topic/four
441git checkout --quiet topic/nolayer
442
443: > "$pushlog"
444printf 'a\n' > "$w/answers"
445valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
446grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out"
447grep -q '^Approved topic/four .*: its integration request for origin/main is filed\.$' "$w/stale.out"
448grep -qx ' checks tree-ok' "$w/stale.out"
449
450# ----------------------------------------------------------------------
451# 9. A target carrying no project layer at all is derived under the floor
452# alone, and says so. The project layer only ever adds, so the identity
453# of "adds" is "adds nothing"; refusing instead would make a repository
454# underivable until its first policy commit, and that commit could not
455# derive either. This is the integrator's own reading of an absent
456# layer (integrator/policy.go), and the two have to agree.
457bare2="$TMPDIR/floorless.git"
458git init --quiet --bare "$bare2"
459repo2="$TMPDIR/floorless"
460git init --quiet "$repo2"
461cd "$repo2" || exit 1
462git remote add origin "$bare2"
463mkdir -p docs policy
464echo "the readme" > docs/readme.md
465echo "$valley" > policy/valley
466git add -A
467git commit --quiet -m base
468git push --quiet -u origin main
469base2="$(git rev-parse main)"
470echo "a second readme" > docs/readme.md
471git commit --quiet -am "a second readme"
472
473valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
474grep -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"
475grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
476if grep -q no-secrets "$w/floorless.out"; then
477 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
478 exit 1
479fi
480cd "$repo" || exit 1
481git checkout --quiet main
482
483# ----------------------------------------------------------------------
484# 10. Approving again after main moved. This is the case that wedged in live
485# use. A request goes stale with reason=evidence when main lands
486# inside a required check's input closure: no attestation over the
487# requested tree can transfer again, so the cure is a new head rebased
488# onto the moved main. That head is not a descendant of the one the
489# request ref holds, which makes writing it a non-fast-forward
490# replacement — and a push without a lease is refused by git's own
491# rule before origin's hook is ever consulted. The verdict that sends
492# a change down this path is the integrator's and is pinned by
493# integrator-e2e; what is under test here is that [a]pprove can act on it.
494git checkout --quiet -b topic/five origin/main
495echo "a readme worth approving" > docs/readme.md
496git commit --quiet -am "a readme worth approving"
497git push --quiet origin topic/five
498stalehead="$(git rev-parse topic/five)"
499staledigest="$(attest digest --rev topic/five | cut -d: -f2)"
500
501printf 'a\n' > "$w/answers"
502valley review topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err"
503grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out"
504
505# main moves, and the change is rebased onto it — the operator's half of
506# approving again, which [a]pprove does not do and does not need to.
507git checkout --quiet main
508echo "a second note" > docs/second.md
509git add -A
510git commit --quiet -m "a landing under the same class"
511git push --quiet origin main
512git checkout --quiet topic/five
513git rebase --quiet main
514git push --quiet --force-with-lease origin topic/five
515newhead="$(git rev-parse topic/five)"
516newdigest="$(attest digest --rev topic/five | cut -d: -f2)"
517
518# The premise of the scenario, asserted rather than assumed: the rebase
519# produced a head off the old one's line and a tree the old evidence is
520# not about. Without both, the push below would be an ordinary one and
521# would prove nothing.
522if git merge-base --is-ancestor "$stalehead" "$newhead"; then
523 echo "valley-request: the rebased head still descends from the old one" >&2
524 exit 1
525fi
526if [ "$staledigest" = "$newdigest" ]; then
527 echo "valley-request: the rebase left the tree the old evidence is about" >&2
528 exit 1
529fi
530
531: > "$pushlog"
532printf 'a\n' > "$w/answers"
533valley review topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err"
534
535grep -q '^Approved topic/five .*: its integration request for origin/main is filed\.$' "$w/five-again.out"
536grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out"
537# Replacing a pending request is said out loud, and the sentence names the
538# head that was replaced.
539grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead") (a readme worth approving)" "$w/five-again.out"
540
541if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \
542 != "$newhead" ]; then
543 echo "valley-request: the request filed again does not name the rebased head" >&2
544 exit 1
545fi
546
547# Both attestations stand. The old one is about a tree that will never
548# land, but the namespace is create-only and a record that can be dropped
549# is not one; the second approval publishes beside it, never over it.
550for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \
551 "refs/the-valley/attestations/$newdigest/$keyhash"; do
552 git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || {
553 echo "valley-request: $stands is not on origin after the second approval" >&2
554 exit 1
555 }
556done
557
558# One push again, and the request ref moved off the old head inside it.
559pushes="$(grep -c '^--- push' "$pushlog" || true)"
560if [ "$pushes" != 1 ]; then
561 echo "valley-request: the second approval took $pushes pushes, not 1" >&2
562 cat "$pushlog" >&2
563 exit 1
564fi
565grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog"
566# And every attestation ref in that push is a creation: an all-zero old id
567# is what the create-only namespace admits, and an approval that
568# updated one would be refused by the real hook.
569if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \
570 | grep -qv '^0* '; then
571 echo "valley-request: the second approval updated an attestation ref" >&2
572 cat "$pushlog" >&2
573 exit 1
574fi
575if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then
576 echo "valley-request: the second approval moved main" >&2
577 exit 1
578fi
579
580# ----------------------------------------------------------------------
581# 11. Two approvals of one change race for one ref, and the loser
582# loses visibly. The request ref's value is read before the push and
583# the push is leased against exactly that value, so the second one to
584# arrive is refused rather than allowed to overwrite the first. The
585# race is made real: a receive-pack wrapper moves the ref on origin
586# after approve has read it and before the push it then makes, which is
587# what another operator getting there first looks like from here.
588git checkout --quiet main
589git checkout --quiet -b topic/six origin/main
590echo "a readme two approvals both rebased" > docs/readme.md
591git commit --quiet -am "a readme two approvals both rebased"
592git push --quiet origin topic/six
593sixfiled="$(git rev-parse topic/six)"
594
595printf 'a\n' > "$w/answers"
596valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err"
597grep -q '^Approved topic/six .*: its integration request for origin/main is filed\.$' "$w/six-first.out"
598
599git checkout --quiet main
600echo "a third note" > docs/third.md
601git add -A
602git commit --quiet -m "another landing under the same class"
603git push --quiet origin main
604git checkout --quiet topic/six
605git rebase --quiet main
606git push --quiet --force-with-lease origin topic/six
607sixhead="$(git rev-parse topic/six)"
608sixdigest="$(attest digest --rev topic/six | cut -d: -f2)"
609
610# The other approval's head, built where origin can already reach
611# every object in it: the same tree, parented on the same tip, so it is
612# the commit a second operator rebasing this change would have produced.
613racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \
614 -p "$(git rev-parse origin/main)" -m "the other approval")"
615
616arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-six '$racer'"
617
618: > "$pushlog"
619printf 'a\ns\n' > "$w/answers"
620valley review topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err"
621disarm_race
622
623# A refusal, named as one, and the review loop still usable after it.
624grep -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"
625grep -q 'the lease on refs/the-valley/integration-requests/main/topic-six refused' "$w/race.err"
626grep -q 'skipped topic/six; nothing done' "$w/race.out"
627
628# Nothing published. The winner's request stands untouched, the loser's
629# evidence never reached origin, and origin saw no push at all — the lease
630# refuses before any command is sent, which is what --atomic is worth.
631if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \
632 != "$racer" ]; then
633 echo "valley-request: the lease did not hold; the other approval was clobbered" >&2
634 exit 1
635fi
636if git -C "$origin" rev-parse --verify --quiet \
637 "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then
638 echo "valley-request: an approval that was refused published its evidence" >&2
639 exit 1
640fi
641if [ -s "$pushlog" ]; then
642 echo "valley-request: an approval that was refused reached origin's hook" >&2
643 cat "$pushlog" >&2
644 exit 1
645fi
646
647# The checks that were run are not lost with the refusal: their notes are
648# in the operator's own repository, and the refusal says where.
649git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \
650 > /dev/null || {
651 echo "valley-request: the refused approval kept no evidence locally" >&2
652 exit 1
653}
654grep -qx 'valley: the evidence is stored locally at:' "$w/race.err"
655grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err"
656git checkout --quiet main
657
658# ----------------------------------------------------------------------
659# 12. Approving again at the same head. Evidence with a validity window
660# expires while the tree stays the right tree, and the answer to that
661# is a fresh observation over the same head. The request ref already
662# names that head, so it is left out of the refspec entirely rather
663# than pushed at the value origin already holds.
664#
665# The request is filed by hand here. Origin holding a request at this
666# head is the whole of the state under test, and re-running [a]pprove to
667# produce it would put a second-granular observation instant in the
668# note: the same tree re-attested inside one second is the identical
669# ref, and across a second boundary it is not.
670git checkout --quiet -b topic/seven origin/main
671echo "a readme approved twice" > docs/readme.md
672git commit --quiet -am "a readme approved twice"
673git push --quiet origin topic/seven
674sevenhead="$(git rev-parse topic/seven)"
675sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)"
676git push --quiet origin \
677 "$sevenhead:refs/the-valley/integration-requests/main/topic-seven"
678
679: > "$pushlog"
680printf 'a\n' > "$w/answers"
681valley review topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err"
682
683grep -q '^Approved topic/seven .*: its integration request for origin/main is filed\.$' "$w/seven.out"
684grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out"
685# The evidence went; the request ref was not in the refspec at all.
686grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog"
687if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then
688 echo "valley-request: a same-head re-approval pushed the request ref again" >&2
689 cat "$pushlog" >&2
690 exit 1
691fi
692if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \
693 != "$sevenhead" ]; then
694 echo "valley-request: a same-head re-approval disturbed the request ref" >&2
695 exit 1
696fi
697
698# The same head with nothing owed: no evidence to publish and no ref to
699# write, so no push is made at all. topic/quiet's request has stood on
700# origin at this head since 5. A push assembled with an empty refspec
701# would fall back to origin's default one, and pushing whatever that
702# happens to name is not what approving a change means.
703git checkout --quiet main
704: > "$pushlog"
705printf 'a\n' > "$w/answers"
706env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
707 > "$w/quiet-again.out" 2> "$w/quiet-again.err"
708grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet-again.out"
709grep -qx ' the request already stood at this head and nothing was owed, so nothing needed publishing' "$w/quiet-again.out"
710if [ -s "$pushlog" ]; then
711 echo "valley-request: a re-approval with nothing to publish still pushed" >&2
712 cat "$pushlog" >&2
713 exit 1
714fi
715
716# ----------------------------------------------------------------------
717# 13. Two operators filing one change at once. Reading that the request
718# ref is absent is a value like any other, and the lease covers it:
719# git spells that expectation with an empty value and refuses if the
720# ref exists at all. Unleased, this push would have fast-forwarded the
721# other filing away without saying a word.
722git checkout --quiet -b topic/eight origin/main
723echo "a readme two operators both filed" > docs/readme.md
724git commit --quiet -am "a readme two operators both filed"
725git push --quiet origin topic/eight
726eighthead="$(git rev-parse topic/eight)"
727eightdigest="$(attest digest --rev topic/eight | cut -d: -f2)"
728# The other operator's filing: this same tree on this same tip, which is
729# what a second checkout of one branch produces. It is a descendant of
730# nothing here, so an unleased push over it would be a plain fast-forward.
731firstfiler="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/eight^{tree}')" \
732 -p "$(git rev-parse origin/main)" -m "the other operator's filing")"
733
734arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-eight '$firstfiler'"
735: > "$pushlog"
736printf 'a\ns\n' > "$w/answers"
737valley review topic/eight < "$w/answers" > "$w/create-race.out" 2> "$w/create-race.err"
738disarm_race
739
740# The other operator's commit is on origin and not here, so the subject
741# says so rather than inventing one. Scenario 10 pins the case where the
742# replaced head IS local and its subject is printed.
743grep -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"
744grep -q 'skipped topic/eight; nothing done' "$w/create-race.out"
745if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-eight)" \
746 != "$firstfiler" ]; then
747 echo "valley-request: an unleased filing clobbered the request that got there first" >&2
748 exit 1
749fi
750if git -C "$origin" rev-parse --verify --quiet \
751 "refs/the-valley/attestations/$eightdigest/$keyhash" > /dev/null; then
752 echo "valley-request: a refused filing published its evidence" >&2
753 exit 1
754fi
755if [ -s "$pushlog" ]; then
756 echo "valley-request: a refused filing reached origin's hook" >&2
757 cat "$pushlog" >&2
758 exit 1
759fi
760
761# ----------------------------------------------------------------------
762# 14. A lease that refuses because the ref is gone, not because it moved.
763# The integrator consumes the request ref when the change lands, so an
764# approval prepared just before that lands into a lease against a
765# ref that no longer exists. Git reports it as stale info either way,
766# and only reading the ref again tells the two apart — so the two are
767# told apart, and the operator is pointed at the landing rather than
768# at a race that did not happen.
769git checkout --quiet main
770git checkout --quiet -b topic/nine origin/main
771echo "a readme that lands mid-approval" > docs/readme.md
772git commit --quiet -am "a readme that lands mid-approval"
773git push --quiet origin topic/nine
774
775printf 'a\n' > "$w/answers"
776valley review topic/nine < "$w/answers" > "$w/nine-first.out" 2> "$w/nine-first.err"
777grep -q '^Approved topic/nine .*: its integration request for origin/main is filed\.$' "$w/nine-first.out"
778
779git checkout --quiet main
780echo "a fourth note" > docs/fourth.md
781git add -A
782git commit --quiet -m "a landing that moves main again"
783git push --quiet origin main
784git checkout --quiet topic/nine
785git rebase --quiet main
786git push --quiet --force-with-lease origin topic/nine
787ninedigest="$(attest digest --rev topic/nine | cut -d: -f2)"
788
789arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-nine"
790: > "$pushlog"
791printf 'a\ns\n' > "$w/answers"
792valley review topic/nine < "$w/answers" > "$w/consumed.out" 2> "$w/consumed.err"
793disarm_race
794
795grep -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"
796grep -q 'nothing was published; fetch and review the branch again before approving it a second time' "$w/consumed.err"
797# Not reported as a race, which is the distinction the re-read buys.
798if grep -q 'the pending request moved from' "$w/consumed.err"; then
799 echo "valley-request: a consumed request was reported as a lost race" >&2
800 cat "$w/consumed.err" >&2
801 exit 1
802fi
803grep -q 'skipped topic/nine; nothing done' "$w/consumed.out"
804if git -C "$origin" rev-parse --verify --quiet \
805 refs/the-valley/integration-requests/main/topic-nine > /dev/null; then
806 echo "valley-request: an approval that was refused re-created the consumed request" >&2
807 exit 1
808fi
809if git -C "$origin" rev-parse --verify --quiet \
810 "refs/the-valley/attestations/$ninedigest/$keyhash" > /dev/null; then
811 echo "valley-request: an approval that was refused published its evidence" >&2
812 exit 1
813fi
814if [ -s "$pushlog" ]; then
815 echo "valley-request: an approval that was refused reached origin's hook" >&2
816 cat "$pushlog" >&2
817 exit 1
818fi
819
820# ----------------------------------------------------------------------
821# 15. The same-head push carries no request ref, so it carries no
822# compare-and-swap on one either — git leaves an up-to-date ref out of
823# what it sends. Where the ref stands after that push is therefore not
824# something the run established, and it is read again rather than
825# asserted from the read before it.
826#
827# Nothing re-files it. A request for a change already in the stream is
828# judged against an empty delta on every pass, which is why the
829# integrator consumes the ref in the first place; re-creating it here
830# would be a guess about which of consumption and deletion happened,
831# paid for with a second push behind the one this verb promises. So
832# the run says what is true and stops, and approving again is the repair.
833git checkout --quiet main
834git checkout --quiet -b topic/ten origin/main
835echo "a readme consumed under the evidence" > docs/readme.md
836git commit --quiet -am "a readme consumed under the evidence"
837git push --quiet origin topic/ten
838tenhead="$(git rev-parse topic/ten)"
839tendigest="$(attest digest --rev topic/ten | cut -d: -f2)"
840git push --quiet origin \
841 "$tenhead:refs/the-valley/integration-requests/main/topic-ten"
842
843arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-ten"
844: > "$pushlog"
845printf 'a\ns\n' > "$w/answers"
846valley review topic/ten < "$w/answers" > "$w/posthoc.out" 2> "$w/posthoc.err"
847disarm_race
848
849grep -q 'the request was consumed while this run was working, which is what the integrator does when a change lands' "$w/posthoc.err"
850grep -q 'Nothing here re-filed it' "$w/posthoc.err"
851# The claim the old code would have made, from a read that had gone out of
852# date under it.
853if grep -q 'the request already stood at this head' "$w/posthoc.out"; then
854 echo "valley-request: a consumed request was reported as still standing" >&2
855 cat "$w/posthoc.out" >&2
856 exit 1
857fi
858grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out"
859
860# The evidence went, because that push succeeded; the request did not come
861# back, because nothing here writes it.
862git -C "$origin" rev-parse --verify --quiet \
863 "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || {
864 echo "valley-request: the evidence push was reported as done and is not on origin" >&2
865 exit 1
866}
867if git -C "$origin" rev-parse --verify --quiet \
868 refs/the-valley/integration-requests/main/topic-ten > /dev/null; then
869 echo "valley-request: the consumed request was re-filed behind the operator" >&2
870 exit 1
871fi
872# And that evidence went in one push, which is still the whole claim.
873pushes="$(grep -c '^--- push' "$pushlog" || true)"
874if [ "$pushes" != 1 ]; then
875 echo "valley-request: the same-head re-approval took $pushes pushes, not 1" >&2
876 cat "$pushlog" >&2
877 exit 1
878fi
879git checkout --quiet main
880
881# ----------------------------------------------------------------------
882# 16. A project other than the-valley carries no schema/. The schema ships
883# with the tool, so the packaged CLI derives what a change owes and
884# files the request from a checkout that has none. This is the case
885# that failed in live use: [a]pprove in a valley's config repository, which
886# carries its own floor at policy/instance, refused with "no policy
887# schema" and published nothing.
888cfgorigin="$TMPDIR/config.git"
889git init --quiet --bare "$cfgorigin"
890cfg="$TMPDIR/config"
891git init --quiet "$cfg"
892cd "$cfg" || exit 1
893git remote add origin "$cfgorigin"
894mkdir -p docs policy/instance
895cp "$valleywork/policy/instance/floor.cue" policy/instance/floor.cue
896echo "the readme" > docs/readme.md
897git add -A
898git commit --quiet -m base
899git push --quiet -u origin main
900git checkout --quiet -b topic/config
901echo "a config readme" > docs/readme.md
902git commit --quiet -am "a config readme"
903git push --quiet origin topic/config
904cfgdigest="$(attest digest --rev topic/config | cut -d: -f2)"
905
906printf 'a\n' > "$w/answers"
907valley review topic/config < "$w/answers" > "$w/config.out" 2> "$w/config.err"
908if grep -q 'no policy schema' "$w/config.err"; then
909 echo "valley-request: [a]pprove looked for the schema in a project's checkout" >&2
910 cat "$w/config.err" >&2
911 exit 1
912fi
913grep -q '^checking topic/config at .*: tree-ok$' "$w/config.out"
914grep -qx "Approved topic/config ($(git rev-parse --short topic/config)): its integration request for origin/main is filed." "$w/config.out"
915git -C "$cfgorigin" rev-parse --verify --quiet \
916 "refs/the-valley/attestations/$cfgdigest/$keyhash" > /dev/null || {
917 echo "valley-request: [a]pprove in a project without schema/ published no evidence" >&2
918 exit 1
919}
920if [ "$(git -C "$cfgorigin" rev-parse refs/the-valley/integration-requests/main/topic-config)" \
921 != "$(git rev-parse topic/config)" ]; then
922 echo "valley-request: [a]pprove in a project without schema/ filed no request for its head" >&2
923 exit 1
924fi
925
926# checks without --schema reads the packaged schema too, and derives what
927# [a]pprove ran from this repository's own floor.
928valley checks > "$w/config-checks.out" 2> "$w/config-checks.err"
929grep -qF '(this repository is its own valley)' "$w/config-checks.out"
930grep -qE '^ tree-ok +mandatory +prose$' "$w/config-checks.out"
931
932# The packaged schema is a default and not a pin. A schema named in the
933# environment is the one read, and --schema overrides both.
934if VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks \
935 > "$w/envschema.out" 2> "$w/envschema.err"; then
936 echo "valley-request: VALLEY_VERIFICATION_SCHEMA did not replace the packaged schema" >&2
937 exit 1
938fi
939grep -qF "no policy schema at $TMPDIR/no-such-schema.cue" "$w/envschema.err"
940VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks --schema "$schemaFile" \
941 > "$w/flagschema.out" 2> "$w/flagschema.err"
942grep -qE '^ tree-ok +mandatory +prose$' "$w/flagschema.out"
943
944# bin/valley run straight from a checkout, with nothing setting the
945# variable, reads schema/verification.cue in that checkout. the-valley's own
946# tree carries one, as the first scratch project does, and every other
947# project's tree does not.
948if env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks \
949 > "$w/bare-config.out" 2> "$w/bare-config.err"; then
950 echo "valley-request: bin/valley run bare found a schema in a checkout without one" >&2
951 exit 1
952fi
953grep -qF "no policy schema at $(git rev-parse --show-toplevel)/schema/verification.cue" \
954 "$w/bare-config.err"
955cd "$repo" || exit 1
956env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "$first" topic/one \
957 > "$w/bare.out" 2> "$w/bare.err"
958grep -qE '^ tree-ok +mandatory +prose$' "$w/bare.out"
959
960# Every git call the default derivation makes fails closed: finding the
961# target, reading both layers out of its tip, reading the pointer,
962# fetching the owning valley, and the diff. fail-nth fails them one at a
963# time, and each run must end non-zero, say so, and print nothing on
964# stdout. The layer reads matter most here: a listing of policy/project
965# that failed, read as an empty directory, would derive under the floor
966# alone, and a read of origin/main that failed, read as no such ref,
967# would derive from the local main.
968shims="$TMPDIR/shims"
969mkdir -p "$shims"
970ln -s "$failNth" "$shims/git"
971shimmed_checks() {
972 echo 0 > "$TMPDIR/shim-count"
973 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
974 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
975 PATH="$shims:$PATH" bash "$cliScript" checks "$first" topic/one
976}
977shimmed_checks 0 > "$w/shim-want.out"
978grep -qE '^ tree-ok +mandatory +prose$' "$w/shim-want.out"
979calls="$(cat "$TMPDIR/shim-count")"
980[ "$calls" -gt 0 ] || {
981 echo "valley-request: the derivation never called git, so failing it proves nothing" >&2
982 exit 1
983}
984for ((at = 1; at <= calls; at++)); do
985 if shimmed_checks "$at" > "$w/shim.out" 2> "$w/shim.err" || [ -s "$w/shim.out" ] \
986 || ! grep -q '^valley: ' "$w/shim.err"; then
987 echo "valley-request: git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
988 cat "$w/shim.out" "$w/shim.err" >&2
989 exit 1
990 fi
991done
992
993# The same, for the derivation that resolves its own base: `merge-base` is
994# a call the explicit-base runs above never make, and --names is a second
995# way of printing what the derivation found. A run is shimmed for each
996# call, with the arguments the caller gives.
997defaulted_checks() {
998 echo 0 > "$TMPDIR/shim-count"
999 env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \
1000 SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \
1001 PATH="$shims:$PATH" bash "$cliScript" checks "${@:2}"
1002}
1003git checkout --quiet topic/one
1004for mode in report names; do
1005 margs=()
1006 [ "$mode" != names ] || margs=(--names)
1007 defaulted_checks 0 "${margs[@]}" > "$w/dshim-want.out"
1008 [ -s "$w/dshim-want.out" ] || {
1009 echo "valley-request: default resolution ($mode) printed nothing" >&2
1010 exit 1
1011 }
1012 calls="$(cat "$TMPDIR/shim-count")"
1013 sawmb=0
1014 for ((at = 1; at <= calls; at++)); do
1015 if defaulted_checks "$at" "${margs[@]}" > "$w/dshim.out" 2> "$w/dshim.err" \
1016 || [ -s "$w/dshim.out" ] || ! grep -q '^valley: ' "$w/dshim.err"; then
1017 echo "valley-request: default resolution ($mode): git call $at of $calls failed, and the deriver did not refuse cleanly" >&2
1018 cat "$w/dshim.out" "$w/dshim.err" >&2
1019 exit 1
1020 fi
1021 done
1022done
1023git checkout --quiet -
1024
1025# A write that fails is a failure of the derivation, not a shorter list.
1026# diff_paths runs with errexit off, so its printf is the case: the function
1027# is cut out of the script and given a full disk to write to.
1028fn="$(sed -n '/^diff_paths() {/,/^}/p' "$cliScript")"
1029[ -n "$fn" ] || { echo "valley-request: diff_paths not found in the script" >&2; exit 1; }
1030if (
1031 eval "$fn"
1032 printf 'M\0docs/a.md\0R100\0old\0new\0' | diff_paths > /dev/full
1033) 2> "$w/full.err"; then
1034 echo "valley-request: diff_paths returned 0 after its write failed" >&2
1035 exit 1
1036fi
1037# And end to end: with no file allowed to grow, every intermediate file the
1038# derivation writes fails, and nothing may print. The pipe keeps the
1039# refusal readable, since a file stream would be cut off too.
1040for mode in report names; do
1041 margs=()
1042 [ "$mode" != names ] || margs=(--names)
1043 if wrote="$(
1044 ulimit -f 0
1045 trap '' XFSZ
1046 env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "${margs[@]}" "$first" topic/one 2> /dev/null
1047 )" || [ -n "$wrote" ]; then
1048 echo "valley-request: a derivation with unwritable scratch files ($mode) did not fail cleanly" >&2
1049 printf '%s\n' "$wrote" >&2
1050 exit 1
1051 fi
1052done
1053
1054# ----------------------------------------------------------------------
1055# 17. The approve verb: [a]pprove without the pager, for a front end that
1056# has shown the change its own way. It runs the function the key
1057# runs, so it publishes what the key's session in 1 published, the
1058# evidence and the request in one push, and prints what that session
1059# printed. stdin is closed, because no answer is read, and the change
1060# is named the way status accepts it, with origin/ in front.
1061git checkout --quiet -b topic/verb origin/main
1062echo "a readme approved without the pager" > docs/readme.md
1063git commit --quiet -am "a readme approved without the pager"
1064git push --quiet origin topic/verb
1065verbhead="$(git rev-parse topic/verb)"
1066verbdigest="$(attest digest --rev topic/verb | cut -d: -f2)"
1067git checkout --quiet main
1068
1069: > "$pushlog"
1070valley approve origin/topic/verb < /dev/null > "$w/verb.out" 2> "$w/verb.err"
1071grep -q '^checking topic/verb at .*: tree-ok$' "$w/verb.out"
1072grep -qx "Approved topic/verb ($(git rev-parse --short "$verbhead")): its integration request for origin/main is filed." "$w/verb.out"
1073grep -qx " request refs/the-valley/integration-requests/main/topic-verb -> $(git rev-parse --short "$verbhead")" "$w/verb.out"
1074grep -qx " evidence refs/the-valley/attestations/$verbdigest/$keyhash" "$w/verb.out"
1075grep -qx ' checks tree-ok' "$w/verb.out"
1076grep -qx 'Check the result: valley status topic/verb' "$w/verb.out"
1077if grep -q '\[[a-z]\]' "$w/verb.out"; then
1078 echo "valley-request: the approve verb offered a prompt" >&2
1079 cat "$w/verb.out" >&2
1080 exit 1
1081fi
1082if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-verb)" \
1083 != "$verbhead" ]; then
1084 echo "valley-request: the approve verb's request does not name the branch's head" >&2
1085 exit 1
1086fi
1087git -C "$origin" rev-parse --verify --quiet \
1088 "refs/the-valley/attestations/$verbdigest/$keyhash" > /dev/null || {
1089 echo "valley-request: the approve verb published no evidence" >&2
1090 exit 1
1091}
1092pushes="$(grep -c '^--- push' "$pushlog" || true)"
1093if [ "$pushes" != 1 ]; then
1094 echo "valley-request: the approve verb took $pushes pushes, not 1" >&2
1095 cat "$pushlog" >&2
1096 exit 1
1097fi
1098
1099# A refusal is the verb's exit status, after the same words the key's
1100# refusal prints, and it publishes nothing.
1101: > "$pushlog"
1102if valley approve topic/two < /dev/null > "$w/verb-refused.out" 2> "$w/verb-refused.err"; then
1103 echo "valley-request: the approve verb exited 0 for a change whose check failed" >&2
1104 exit 1
1105fi
1106grep -q 'the checks did not pass; nothing published, main untouched' "$w/verb-refused.err"
1107if [ -s "$pushlog" ]; then
1108 echo "valley-request: a refused approve verb pushed something" >&2
1109 cat "$pushlog" >&2
1110 exit 1
1111fi
1112if valley approve topic/no-such-change < /dev/null > "$w/verb-missing.out" 2> "$w/verb-missing.err"; then
1113 echo "valley-request: the approve verb exited 0 for a change origin does not have" >&2
1114 exit 1
1115fi
1116grep -q 'no such branch on origin: topic/no-such-change' "$w/verb-missing.err"
1117
1118# ----------------------------------------------------------------------
1119# 18. The rebase verb: [b]ase without the pager. A stale branch is
1120# rebased onto main in a throwaway worktree and pushed under a lease,
1121# and the new head is printed. Nothing is approved and nothing is
1122# re-reviewed: rebasing is mechanics, and the judgment on what it
1123# produced stays with whoever reads it next.
1124git checkout --quiet -b topic/behind "$first"
1125echo "a readme left behind" > docs/behind.md
1126git add -A
1127git commit --quiet -m "a readme left behind"
1128git push --quiet origin topic/behind
1129behindhead="$(git rev-parse topic/behind)"
1130git checkout --quiet main
1131if git -C "$origin" merge-base --is-ancestor refs/heads/main refs/heads/topic/behind; then
1132 echo "valley-request: the branch to rebase fast-forwarded from main already" >&2
1133 exit 1
1134fi
1135before_rebase="$(git -C "$origin" rev-parse refs/heads/main)"
1136
1137: > "$pushlog"
1138valley rebase topic/behind < /dev/null > "$w/rebase.out" 2> "$w/rebase.err"
1139rebased="$(git -C "$origin" rev-parse refs/heads/topic/behind)"
1140grep -qx 'rebased topic/behind onto origin/main, pushed with --force-with-lease' "$w/rebase.out"
1141grep -qx " head $rebased" "$w/rebase.out"
1142if [ "$rebased" = "$behindhead" ]; then
1143 echo "valley-request: the rebase verb left the branch where it was" >&2
1144 exit 1
1145fi
1146git -C "$origin" merge-base --is-ancestor refs/heads/main "$rebased" || {
1147 echo "valley-request: the rebased head does not fast-forward from main" >&2
1148 exit 1
1149}
1150if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_rebase" ]; then
1151 echo "valley-request: the rebase verb moved main" >&2
1152 exit 1
1153fi
1154if git -C "$origin" rev-parse --verify --quiet \
1155 refs/the-valley/integration-requests/main/topic-behind > /dev/null; then
1156 echo "valley-request: the rebase verb filed a request" >&2
1157 exit 1
1158fi
1159pushes="$(grep -c '^--- push' "$pushlog" || true)"
1160if [ "$pushes" != 1 ] || grep -v '^--- push' "$pushlog" | grep -qv ' refs/heads/topic/behind$'; then
1161 echo "valley-request: the rebase verb pushed something besides the branch" >&2
1162 cat "$pushlog" >&2
1163 exit 1
1164fi
1165
1166# A branch that fast-forwards from main already is left as it is.
1167: > "$pushlog"
1168valley rebase topic/behind < /dev/null > "$w/rebase-again.out" 2> "$w/rebase-again.err"
1169grep -qx 'topic/behind already fast-forwards from origin/main; nothing rebased' "$w/rebase-again.out"
1170grep -qx " head $rebased" "$w/rebase-again.out"
1171if [ -s "$pushlog" ]; then
1172 echo "valley-request: rebasing a branch that fast-forwards pushed something" >&2
1173 cat "$pushlog" >&2
1174 exit 1
1175fi
1176
1177# A rebase that conflicts changes nothing, says so, and exits non-zero.
1178git checkout --quiet -b topic/clash "$first"
1179echo "a note that clashes" > docs/notes.md
1180git add -A
1181git commit --quiet -m "a note that clashes"
1182git push --quiet origin topic/clash
1183clashhead="$(git rev-parse topic/clash)"
1184git checkout --quiet main
1185if valley rebase topic/clash < /dev/null > "$w/clash.out" 2> "$w/clash.err"; then
1186 echo "valley-request: a rebase that conflicts exited 0" >&2
1187 exit 1
1188fi
1189grep -q 'topic/clash conflicts with main — resolve by hand; nothing changed' "$w/clash.err"
1190if [ "$(git -C "$origin" rev-parse refs/heads/topic/clash)" != "$clashhead" ]; then
1191 echo "valley-request: a rebase that conflicted moved the branch" >&2
1192 exit 1
1193fi
1194
1195# ----------------------------------------------------------------------
1196# The verbs the loop offers are the whole of what an operator can do to a
1197# branch, so no path may offer one outside the set: [a]pprove, [b]ase,
1198# [r]eject, [s]kip. Every stream this check captured is scanned at the end,
1199# which is what makes the claim about paths rather than about the two
1200# prompts pinned character for character above — the fast-forward prompt,
1201# the stale one, the one [b]ase comes back to, and the ones a refused [a]pprove
1202# returns to are all in here. A verb that writes the protected ref from the
1203# operator's own checkout — the retired [i]ntegrate, or any successor to
1204# it — fails this wherever it is printed.
1205offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
1206outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]pprove|\[b\]ase|\[r\]eject|\[s\]kip' || true)"
1207if [ -n "$outside" ]; then
1208 echo "valley-request: a review path offered a verb outside the set:" >&2
1209 printf '%s\n' "$outside" >&2
1210 exit 1
1211fi
1212# The scan is only worth something if it saw the offers at all.
1213for verb in '[a]pprove' '[b]ase' '[r]eject' '[s]kip'; do
1214 printf '%s\n' "$offered" | grep -qxF "$verb" \
1215 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
1216done
1217
1218# No throwaway worktree outlived any of it: only the checkout is left.
1219worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
1220if [ "$worktrees" != 1 ]; then
1221 echo "valley-request: a throwaway worktree was left behind" >&2
1222 git worktree list >&2
1223 exit 1
1224fi
1225
1226echo "every scenario held"
1227{
1228 cat "$w/approve.out"
1229 printf '\n--- the scratch origin, after all of it ---\n'
1230 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
1231} > "$out"