gunk-dev/the-valley

valley-request.sh

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