gunk-dev/the-valley

valley-request.sh

source6059a0558a50main's published tip history
31.8 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export XDG_STATE_HOME="$TMPDIR/state"
6export GIT_CONFIG_NOSYSTEM=1
7# What this check writes down, kept out of the repository under test: an
8# answer file or a captured stream sitting in the working tree would be
9# committed by the next `git add -A` and change the tree being attested.
10w="$TMPDIR/scratch"
11mkdir -p "$w"
12git config --global user.name valley-check
13git config --global user.email valley-check@localhost
14git config --global init.defaultBranch main
15git config --global advice.detachedHead false
16
17# The operator's signing key. Provisioning one is deployment, so the verb
18# takes it as a parameter and this check supplies a throwaway.
19ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host"
20export VALLEY_ATTEST_KEY="$TMPDIR/host"
21export VALLEY_ATTEST_NAME=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 [a]sk
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# ----------------------------------------------------------------------
39# The owning valley's repository, carrying the floor at policy/instance.
40# The floor is read from here and never from the project, which is the
41# tree under judgment: what governs a change has to come from outside it.
42# Fetching is all the deriver does to this repository, which is what
43# anyone the valley governs necessarily holds.
44valley="$TMPDIR/valley.git"
45git init --quiet --bare "$valley"
46valleywork="$TMPDIR/valley-work"
47git init --quiet "$valleywork"
48mkdir -p "$valleywork/policy/instance"
49cat > "$valleywork/policy/instance/floor.cue" <<'EOF'
50package verification
51
52floor: {
53 checks: "tree-ok": {
54 runner: "command"
55 command: "test -f docs/readme.md"
56 }
57 classes: prose: {
58 paths: "docs/**": true
59 requires: "tree-ok": true
60 }
61 // A class that covers paths and asks nothing of them. Owing no check
62 // is an ordinary outcome, not a hole: the policy has read these paths
63 // and has nothing to require of them.
64 classes: quiet: {
65 paths: "quiet/**": true
66 requires: {}
67 }
68 unclassified: "tree-ok": true
69}
70EOF
71git -C "$valleywork" add -A
72git -C "$valleywork" commit --quiet -m floor
73git -C "$valleywork" push --quiet "$valley" main
74
75# ----------------------------------------------------------------------
76# The project, laid out the way the deriver reads one without being told:
77# its own layer at policy/project, and its owning valley named at
78# policy/valley. Both checks use the command runner: the nix runner cannot
79# be exercised from inside a nix build, since the sandbox has no daemon to
80# call, and what is under test here is the sequencing rather than the
81# runner.
82#
83# examples/policy/ holds a second, contradictory pair of layers, and every
84# check they require is named `phantom`. Nothing may derive it: a gate that
85# came from a worked example is a gate nobody wrote, and the name is here
86# so that the failure would be visible rather than plausible.
87repo="$TMPDIR/project"
88git init --quiet "$repo"
89cd "$repo" || exit 1
90git remote add origin "$origin"
91
92mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project
93cp "$schemaFile" schema/verification.cue
94echo "the readme" > docs/readme.md
95echo "$valley" > policy/valley
96cat > policy/project/policy.cue <<'EOF'
97package verification
98
99project: {
100 checks: "no-secrets": {
101 runner: "command"
102 command: "! grep -rqF SECRET src"
103 }
104 classes: source: {
105 paths: "src/**": true
106 requires: "no-secrets": true
107 }
108}
109EOF
110cat > examples/policy/instance/floor.cue <<'EOF'
111package verification
112
113floor: {
114 checks: phantom: {
115 runner: "command"
116 command: "false"
117 }
118 classes: everything: {
119 paths: "**": true
120 requires: phantom: true
121 }
122 unclassified: phantom: true
123}
124EOF
125cat > examples/policy/project/policy.cue <<'EOF'
126package verification
127
128project: {}
129EOF
130git add -A
131git commit --quiet -m base
132git push --quiet -u origin main
133first="$(git rev-parse main)"
134
135# ----------------------------------------------------------------------
136# 1. A branch whose required check passes: the evidence and the request
137# reach origin in one push, and main does not move.
138git checkout --quiet -b topic/one
139echo "a better readme" > docs/readme.md
140git commit --quiet -am "improve the readme"
141git push --quiet origin topic/one
142digest="$(attest digest --rev topic/one | cut -d: -f2)"
143
144: > "$pushlog"
145printf 'a\n' > "$w/answers"
146valley review topic/one < "$w/answers" > "$w/ask.out" 2> "$w/ask.err"
147
148grep -q '^checking topic/one at .*: tree-ok$' "$w/ask.out"
149grep -qE '^check tree-ok +command passed$' "$w/ask.out"
150grep -qx "Submitted topic/one ($(git rev-parse --short topic/one)) for integration into origin/main." "$w/ask.out"
151grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/ask.out"
152grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/ask.out"
153grep -qx ' checks tree-ok' "$w/ask.out"
154grep -qx 'Your local checkout has not changed.' "$w/ask.out"
155grep -qx 'Check the result: valley status topic/one' "$w/ask.out"
156
157# What origin holds now. The attestation is keyed by the digest of the
158# BRANCH's tree, which is what says the checks ran over the right tree and
159# not over the operator's working copy.
160git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
161grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
162grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
163if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
164 != "$(git rev-parse topic/one)" ]; then
165 echo "valley-request: the request ref does not name the branch's head" >&2
166 exit 1
167fi
168if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
169 echo "valley-request: [a]sk moved main" >&2
170 exit 1
171fi
172
173# One push carried both. This is the friction the verb exists to remove:
174# `attest run --push` publishes the branch and the evidence and knows
175# nothing about a request ref, so doing this by hand is two beats.
176pushes="$(grep -c '^--- push' "$pushlog" || true)"
177if [ "$pushes" != 1 ]; then
178 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
179 cat "$pushlog" >&2
180 exit 1
181fi
182grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
183grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
184
185# The note origin holds is the one the operator signed, over that tree.
186git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
187 > "$w/statement.note"
188attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
189attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
190 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
191grep -q 'matches the recorded subject digest' "$w/verify.out"
192
193# ----------------------------------------------------------------------
194# 2. The layers the derivation used are the ones that govern, and it says
195# which they were: the floor fetched from the owning valley, and the
196# project's own layer at policy/project in the TARGET's tip.
197# examples/policy/ is in this tree and requires `phantom` of every path,
198# and no default reaches it — it is derivable only by naming it, which
199# is what the options are for.
200valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err"
201grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out"
202grep -qx 'classes matched: prose' "$w/checks.out"
203grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out"
204if grep -q phantom "$w/checks.out" "$w/checks.err"; then
205 echo "valley-request: a default derived a check from examples/policy/" >&2
206 cat "$w/checks.out" "$w/checks.err" >&2
207 exit 1
208fi
209
210# Named, the example layers derive exactly what they declare. This is the
211# other half of the claim above: examples/policy/ is readable, and reading
212# it is something a person asks for.
213valley checks --instance examples/policy/instance --project examples/policy/project \
214 "$first" topic/one > "$w/example.out"
215grep -qE '^ phantom +mandatory' "$w/example.out"
216grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out"
217
218# ----------------------------------------------------------------------
219# 3. main moves on, so the next branch is stale. [a]sk is offered there
220# too — the integrator applies a delta to the tip rather than
221# fast-forwarding — and a branch whose check fails publishes nothing
222# and leaves the review loop usable.
223git checkout --quiet main
224echo "a note" > docs/notes.md
225git add -A
226git commit --quiet -m "a landing that moves main"
227git push --quiet origin main
228
229git checkout --quiet -b topic/two "$first"
230printf 'SECRET\n' > src/leak.txt
231git add -A
232git commit --quiet -m "a leak"
233git push --quiet origin topic/two
234
235: > "$pushlog"
236printf 'a\ns\n' > "$w/answers"
237valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
238
239# The prompt is the whole of what the operator is offered, so it is pinned
240# character for character: a verb that stops being offered, or one that
241# starts, is a change to the review loop and not an incidental one.
242grep -qF 'topic/two does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out"
243grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
244grep -qE '^check no-secrets +command failed$' "$w/fail.out"
245grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
246# The prompt is printed without a newline, so what follows a refusal shares
247# its line — the answer to it is not anchored.
248grep -q 'skipped topic/two; nothing done' "$w/fail.out"
249if [ -s "$pushlog" ]; then
250 echo "valley-request: a failing check pushed something" >&2
251 cat "$pushlog" >&2
252 exit 1
253fi
254git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
255if grep -q 'topic-two' "$w/after.txt"; then
256 echo "valley-request: a failing check left a ref on origin" >&2
257 cat "$w/after.txt" >&2
258 exit 1
259fi
260
261# ----------------------------------------------------------------------
262# 4. No signing key, with a check owed: refused before any check is run,
263# and the prompt still comes back. The host signs, and there is no
264# unsigned mode.
265: > "$pushlog"
266printf 'a\ns\n' > "$w/answers"
267env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
268grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
269grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
270if [ -s "$pushlog" ]; then
271 echo "valley-request: a run with no signing key pushed something" >&2
272 exit 1
273fi
274
275# ----------------------------------------------------------------------
276# 5. A branch the policy requires no check of is filed as a request with
277# no evidence. The integrator lands one and records it as a landing
278# that required nothing, so refusing to file it here would strand
279# exactly the changes a policy leaves unrequired. Nothing is owed, so
280# nothing is signed: this runs with no signing key at all.
281git checkout --quiet main
282git checkout --quiet -b topic/quiet
283mkdir -p quiet
284echo "a quiet note" > quiet/note.md
285git add -A
286git commit --quiet -m "a change under a class that asks nothing"
287git push --quiet origin topic/quiet
288quiethead="$(git rev-parse --short topic/quiet)"
289
290: > "$pushlog"
291printf 'a\n' > "$w/answers"
292env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
293 > "$w/quiet.out" 2> "$w/quiet.err"
294
295grep -q 'owes no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
296grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
297grep -q '^Submitted topic/quiet .* for integration into origin/main\.$' "$w/quiet.out"
298grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
299grep -qx ' evidence none — nothing was owed, so nothing was signed' "$w/quiet.out"
300grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
301
302# One push, carrying the request and nothing else.
303pushes="$(grep -c '^--- push' "$pushlog" || true)"
304if [ "$pushes" != 1 ]; then
305 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
306 cat "$pushlog" >&2
307 exit 1
308fi
309if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
310 echo "valley-request: a request owing nothing published evidence" >&2
311 cat "$pushlog" >&2
312 exit 1
313fi
314if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
315 != "$(git rev-parse topic/quiet)" ]; then
316 echo "valley-request: the bare request does not name the branch's head" >&2
317 exit 1
318fi
319
320# ----------------------------------------------------------------------
321# 6. [b]ase, and the prompt it comes back to. A stale branch is rebased
322# onto main in a throwaway worktree and re-reviewed, and the prompt the
323# loop then prints is the fast-forward one — the path a reader of
324# bin/valley has to trace two loops to reach, so it is exercised here
325# rather than argued about.
326git checkout --quiet main
327git checkout --quiet -b topic/three "$first"
328echo "another readme" > docs/readme.md
329git commit --quiet -am "another readme"
330git push --quiet origin topic/three
331before_base="$(git -C "$origin" rev-parse refs/heads/main)"
332
333printf 'b\ns\n' > "$w/answers"
334valley review topic/three < "$w/answers" > "$w/base.out" 2>&1
335grep -qF 'topic/three does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out"
336grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out"
337# The post-[b]ase prompt: the branch fast-forwards now, so the loop falls
338# through to the prompt that has no [b]ase in it.
339grep -qF 'topic/three: [a]sk, [r]eject, [s]kip? ' "$w/base.out"
340grep -q 'skipped topic/three; nothing done' "$w/base.out"
341git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \
342 || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; }
343if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then
344 echo "valley-request: the review loop moved main" >&2
345 exit 1
346fi
347git checkout --quiet main
348
349# The request filed in 1 is still pending: nothing here consumes it, which
350# is the integrator's own step.
351git -C "$origin" rev-parse --verify --quiet \
352 refs/the-valley/integration-requests/main/topic-one > /dev/null
353
354# ----------------------------------------------------------------------
355# 7. A change does not supply the policy that gates it. This branch
356# deletes its own policy/project and repoints policy/valley at a
357# repository that does not exist. Both layers come from the target's
358# tip, so neither edit reaches the derivation: it composes the same two
359# layers it would for any other branch, and the fetch that a branch-read
360# pointer would have attempted never happens.
361git checkout --quiet -b topic/nolayer
362git rm --quiet -r policy/project
363echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
364git add -A
365git commit --quiet -m "delete the project layer and repoint the valley"
366git push --quiet origin topic/nolayer
367
368valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
369grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
370if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
371 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
372 cat "$w/nolayer.out" "$w/nolayer.err" >&2
373 exit 1
374fi
375
376: > "$pushlog"
377printf 'a\n' > "$w/answers"
378valley review topic/nolayer < "$w/answers" \
379 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
380grep -q '^Submitted topic/nolayer .* for integration into origin/main\.$' "$w/nolayer.review.out"
381
382# ----------------------------------------------------------------------
383# 8. The reviewer's checkout contributes nothing. This is the case that
384# failed in live use: the operator's checkout was an older branch
385# without the landed policy layer, and the deriver read the working
386# tree and refused. topic/nolayer is exactly such a checkout — no
387# policy/project in it, and a policy/valley naming nowhere — so every
388# derivation below runs from it.
389#
390# Byte-identical output is the claim, not merely a successful one:
391# review works the same from any branch, however stale.
392git checkout --quiet main
393valley checks "$first" topic/one > "$w/from-main.out"
394git checkout --quiet topic/nolayer
395valley checks "$first" topic/one > "$w/from-stale.out"
396if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
397 echo "valley-request: the checkout changed what the derivation said" >&2
398 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
399 exit 1
400fi
401
402# And [a]sk, which is where it failed: a request filed from that same
403# checkout, over a branch the stale tree knows nothing about.
404git checkout --quiet -b topic/four origin/main
405echo "one more readme" > docs/readme.md
406git commit --quiet -am "one more readme"
407git push --quiet origin topic/four
408git checkout --quiet topic/nolayer
409
410: > "$pushlog"
411printf 'a\n' > "$w/answers"
412valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
413grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out"
414grep -q '^Submitted topic/four .* for integration into origin/main\.$' "$w/stale.out"
415grep -qx ' checks tree-ok' "$w/stale.out"
416
417# ----------------------------------------------------------------------
418# 9. A target carrying no project layer at all is derived under the floor
419# alone, and says so. The project layer only ever adds, so the identity
420# of "adds" is "adds nothing"; refusing instead would make a repository
421# underivable until its first policy commit, and that commit could not
422# derive either. This is the integrator's own reading of an absent
423# layer (integrator/policy.go), and the two have to agree.
424bare2="$TMPDIR/floorless.git"
425git init --quiet --bare "$bare2"
426repo2="$TMPDIR/floorless"
427git init --quiet "$repo2"
428cd "$repo2" || exit 1
429git remote add origin "$bare2"
430mkdir -p docs schema policy
431cp "$schemaFile" schema/verification.cue
432echo "the readme" > docs/readme.md
433echo "$valley" > policy/valley
434git add -A
435git commit --quiet -m base
436git push --quiet -u origin main
437base2="$(git rev-parse main)"
438echo "a second readme" > docs/readme.md
439git commit --quiet -am "a second readme"
440
441valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
442grep -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"
443grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
444if grep -q no-secrets "$w/floorless.out"; then
445 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
446 exit 1
447fi
448cd "$repo" || exit 1
449git checkout --quiet main
450
451# ----------------------------------------------------------------------
452# 10. Resubmission after main moved. This is the case that wedged in live
453# use. A request goes stale with reason=evidence when main lands
454# inside a required check's input closure: no attestation over the
455# submitted tree can transfer again, so the cure is a new head rebased
456# onto the moved main. That head is not a descendant of the one the
457# request ref holds, which makes writing it a non-fast-forward
458# replacement — and a push without a lease is refused by git's own
459# rule before origin's hook is ever consulted. The verdict that sends
460# a change down this path is the integrator's and is pinned by
461# integrator-e2e; what is under test here is that [a]sk can act on it.
462git checkout --quiet -b topic/five origin/main
463echo "a readme worth asking about" > docs/readme.md
464git commit --quiet -am "a readme worth asking about"
465git push --quiet origin topic/five
466stalehead="$(git rev-parse topic/five)"
467staledigest="$(attest digest --rev topic/five | cut -d: -f2)"
468
469printf 'a\n' > "$w/answers"
470valley review topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err"
471grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out"
472
473# main moves, and the change is rebased onto it — the operator's half of
474# a resubmission, which [a]sk does not do and does not need to.
475git checkout --quiet main
476echo "a second note" > docs/second.md
477git add -A
478git commit --quiet -m "a landing under the same class"
479git push --quiet origin main
480git checkout --quiet topic/five
481git rebase --quiet main
482git push --quiet --force-with-lease origin topic/five
483newhead="$(git rev-parse topic/five)"
484newdigest="$(attest digest --rev topic/five | cut -d: -f2)"
485
486# The premise of the scenario, asserted rather than assumed: the rebase
487# produced a head off the old one's line and a tree the old evidence is
488# not about. Without both, the push below would be an ordinary one and
489# would prove nothing.
490if git merge-base --is-ancestor "$stalehead" "$newhead"; then
491 echo "valley-request: the rebased head still descends from the old one" >&2
492 exit 1
493fi
494if [ "$staledigest" = "$newdigest" ]; then
495 echo "valley-request: the rebase left the tree the old evidence is about" >&2
496 exit 1
497fi
498
499: > "$pushlog"
500printf 'a\n' > "$w/answers"
501valley review topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err"
502
503grep -qx 'requested topic/five -> refs/heads/main' "$w/five-again.out"
504grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out"
505# Replacing a pending request is said out loud, and the sentence names the
506# head that was replaced.
507grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead")" "$w/five-again.out"
508
509if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \
510 != "$newhead" ]; then
511 echo "valley-request: the resubmitted request does not name the rebased head" >&2
512 exit 1
513fi
514
515# Both attestations stand. The old one is about a tree that will never
516# land, but the namespace is create-only and a record that can be dropped
517# is not one; the resubmission publishes beside it, never over it.
518for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \
519 "refs/the-valley/attestations/$newdigest/$keyhash"; do
520 git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || {
521 echo "valley-request: $stands is not on origin after the resubmission" >&2
522 exit 1
523 }
524done
525
526# One push again, and the request ref moved off the old head inside it.
527pushes="$(grep -c '^--- push' "$pushlog" || true)"
528if [ "$pushes" != 1 ]; then
529 echo "valley-request: the resubmission took $pushes pushes, not 1" >&2
530 cat "$pushlog" >&2
531 exit 1
532fi
533grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog"
534# And every attestation ref in that push is a creation: an all-zero old id
535# is what the create-only namespace admits, and a resubmission that
536# updated one would be refused by the real hook.
537if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \
538 | grep -qv '^0* '; then
539 echo "valley-request: the resubmission updated an attestation ref" >&2
540 cat "$pushlog" >&2
541 exit 1
542fi
543if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then
544 echo "valley-request: the resubmission moved main" >&2
545 exit 1
546fi
547
548# ----------------------------------------------------------------------
549# 11. Two resubmissions of one change race for one ref, and the loser
550# loses visibly. The request ref's value is read before the push and
551# the push is leased against exactly that value, so the second one to
552# arrive is refused rather than allowed to overwrite the first. The
553# race is made real: a receive-pack wrapper moves the ref on origin
554# after [a]sk has read it and before the push it then makes, which is
555# what another operator getting there first looks like from here.
556git checkout --quiet main
557git checkout --quiet -b topic/six origin/main
558echo "a readme two resubmissions both rebased" > docs/readme.md
559git commit --quiet -am "a readme two resubmissions both rebased"
560git push --quiet origin topic/six
561
562printf 'a\n' > "$w/answers"
563valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err"
564grep -qx 'requested topic/six -> refs/heads/main' "$w/six-first.out"
565
566git checkout --quiet main
567echo "a third note" > docs/third.md
568git add -A
569git commit --quiet -m "another landing under the same class"
570git push --quiet origin main
571git checkout --quiet topic/six
572git rebase --quiet main
573git push --quiet --force-with-lease origin topic/six
574sixhead="$(git rev-parse topic/six)"
575sixdigest="$(attest digest --rev topic/six | cut -d: -f2)"
576
577# The other resubmission's head, built where origin can already reach
578# every object in it: the same tree, parented on the same tip, so it is
579# the commit a second operator rebasing this change would have produced.
580racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \
581 -p "$(git rev-parse origin/main)" -m "the other resubmission")"
582
583cat > "$TMPDIR/racing-receive-pack" <<EOF
584#!/bin/sh
585# One shot, so only the push under test is raced.
586if [ -e "$TMPDIR/race-armed" ]; then
587 rm -f "$TMPDIR/race-armed"
588 git -C "$origin" update-ref \
589 refs/the-valley/integration-requests/main/topic-six "$racer"
590fi
591exec git-receive-pack "\$@"
592EOF
593chmod +x "$TMPDIR/racing-receive-pack"
594git config remote.origin.receivepack "$TMPDIR/racing-receive-pack"
595touch "$TMPDIR/race-armed"
596
597: > "$pushlog"
598printf 'a\ns\n' > "$w/answers"
599valley review topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err"
600git config --unset remote.origin.receivepack
601if [ -e "$TMPDIR/race-armed" ]; then
602 echo "valley-request: the racing receive-pack never ran, so nothing was raced" >&2
603 exit 1
604fi
605
606# A refusal, named as one, and the review loop still usable after it.
607grep -q 'the pending request moved while this one was being prepared' "$w/race.err"
608grep -q 'the lease on refs/the-valley/integration-requests/main/topic-six refused' "$w/race.err"
609grep -q 'skipped topic/six; nothing done' "$w/race.out"
610
611# Nothing published. The winner's request stands untouched, the loser's
612# evidence never reached origin, and origin saw no push at all — the lease
613# refuses before any command is sent, which is what --atomic is worth.
614if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \
615 != "$racer" ]; then
616 echo "valley-request: the lease did not hold; the other resubmission was clobbered" >&2
617 exit 1
618fi
619if git -C "$origin" rev-parse --verify --quiet \
620 "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then
621 echo "valley-request: a refused resubmission published its evidence" >&2
622 exit 1
623fi
624if [ -s "$pushlog" ]; then
625 echo "valley-request: a refused resubmission reached origin's hook" >&2
626 cat "$pushlog" >&2
627 exit 1
628fi
629
630# The checks that were run are not lost with the refusal: their notes are
631# in the operator's own repository, and the refusal says where.
632git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \
633 > /dev/null || {
634 echo "valley-request: the refused resubmission kept no evidence locally" >&2
635 exit 1
636}
637grep -q 'the evidence is stored locally at:' "$w/race.err"
638grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err"
639git checkout --quiet main
640
641# ----------------------------------------------------------------------
642# 12. Asking again at the same head. Evidence with a validity window
643# expires while the tree stays the right tree, and the answer to that
644# is a fresh observation over the same head. The request ref already
645# names that head, so it is left out of the refspec entirely rather
646# than pushed at the value origin already holds.
647#
648# The request is filed by hand here. Origin holding a request at this
649# head is the whole of the state under test, and re-running [a]sk to
650# produce it would put a second-granular observation instant in the
651# note: the same tree re-attested inside one second is the identical
652# ref, and across a second boundary it is not.
653git checkout --quiet -b topic/seven origin/main
654echo "a readme asked about twice" > docs/readme.md
655git commit --quiet -am "a readme asked about twice"
656git push --quiet origin topic/seven
657sevenhead="$(git rev-parse topic/seven)"
658sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)"
659git push --quiet origin \
660 "$sevenhead:refs/the-valley/integration-requests/main/topic-seven"
661
662: > "$pushlog"
663printf 'a\n' > "$w/answers"
664valley review topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err"
665
666grep -qx 'requested topic/seven -> refs/heads/main' "$w/seven.out"
667grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out"
668# The evidence went; the request ref was not in the refspec at all.
669grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog"
670if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then
671 echo "valley-request: a same-head re-ask pushed the request ref again" >&2
672 cat "$pushlog" >&2
673 exit 1
674fi
675if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \
676 != "$sevenhead" ]; then
677 echo "valley-request: a same-head re-ask disturbed the request ref" >&2
678 exit 1
679fi
680
681# The same head with nothing owed: no evidence to publish and no ref to
682# write, so no push is made at all. topic/quiet's request has stood on
683# origin at this head since 5. A push assembled with an empty refspec
684# would fall back to origin's default one, and pushing whatever that
685# happens to name is not what asking about a change means.
686git checkout --quiet main
687: > "$pushlog"
688printf 'a\n' > "$w/answers"
689env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
690 > "$w/quiet-again.out" 2> "$w/quiet-again.err"
691grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead (already there; nothing to publish)" "$w/quiet-again.out"
692if [ -s "$pushlog" ]; then
693 echo "valley-request: a re-ask with nothing to publish still pushed" >&2
694 cat "$pushlog" >&2
695 exit 1
696fi
697
698# ----------------------------------------------------------------------
699# The verbs the loop offers are the whole of what an operator can do to a
700# branch, so no path may offer one outside the set: [a]sk, [b]ase,
701# [r]eject, [s]kip. Every stream this check captured is scanned at the end,
702# which is what makes the claim about paths rather than about the two
703# prompts pinned character for character above — the fast-forward prompt,
704# the stale one, the one [b]ase comes back to, and the ones a refused [a]sk
705# returns to are all in here. A verb that writes the protected ref from the
706# operator's own checkout — the retired [i]ntegrate, or any successor to
707# it — fails this wherever it is printed.
708offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
709outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]sk|\[b\]ase|\[r\]eject|\[s\]kip' || true)"
710if [ -n "$outside" ]; then
711 echo "valley-request: a review path offered a verb outside the set:" >&2
712 printf '%s\n' "$outside" >&2
713 exit 1
714fi
715# The scan is only worth something if it saw the offers at all.
716for verb in '[a]sk' '[b]ase' '[r]eject' '[s]kip'; do
717 printf '%s\n' "$offered" | grep -qxF "$verb" \
718 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
719done
720
721# No throwaway worktree outlived any of it: only the checkout is left.
722worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
723if [ "$worktrees" != 1 ]; then
724 echo "valley-request: a throwaway worktree was left behind" >&2
725 git worktree list >&2
726 exit 1
727fi
728
729echo "every scenario held"
730{
731 cat "$w/ask.out"
732 printf '\n--- the scratch origin, after all of it ---\n'
733 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
734} > "$out"