gunk-dev/the-valley

valley-request.sh

sourcebe2de7158769main's published tip history
40.3 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export XDG_STATE_HOME="$TMPDIR/state"
6export GIT_CONFIG_NOSYSTEM=1
7# What this check writes down, kept out of the repository under test: an
8# answer file or a captured stream sitting in the working tree would be
9# committed by the next `git add -A` and change the tree being attested.
10w="$TMPDIR/scratch"
11mkdir -p "$w"
12git config --global user.name valley-check
13git config --global user.email valley-check@localhost
14git config --global init.defaultBranch main
15git config --global advice.detachedHead false
16
17# The operator's signing key. Provisioning one is deployment, so the verb
18# takes it as a parameter and this check supplies a throwaway.
19ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host"
20export VALLEY_ATTEST_KEY="$TMPDIR/host"
21export VALLEY_ATTEST_NAME=laddie.valley.invalid/attestations
22keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)"
23
24origin="$TMPDIR/project.git"
25git init --quiet --bare "$origin"
26
27# Every push the bare repo receives, as its hook sees it. The claim [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# Interpose on origin's receive-pack so that the request namespace moves
39# between [a]sk'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.
116repo="$TMPDIR/project"
117git init --quiet "$repo"
118cd "$repo" || exit 1
119git remote add origin "$origin"
120
121mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project
122cp "$schemaFile" schema/verification.cue
123echo "the readme" > docs/readme.md
124echo "$valley" > policy/valley
125cat > policy/project/policy.cue <<'EOF'
126package verification
127
128project: {
129 checks: "no-secrets": {
130 runner: "command"
131 command: "! grep -rqF SECRET src"
132 }
133 classes: source: {
134 paths: "src/**": true
135 requires: "no-secrets": true
136 }
137}
138EOF
139cat > examples/policy/instance/floor.cue <<'EOF'
140package verification
141
142floor: {
143 checks: phantom: {
144 runner: "command"
145 command: "false"
146 }
147 classes: everything: {
148 paths: "**": true
149 requires: phantom: true
150 }
151 unclassified: phantom: true
152}
153EOF
154cat > examples/policy/project/policy.cue <<'EOF'
155package verification
156
157project: {}
158EOF
159git add -A
160git commit --quiet -m base
161git push --quiet -u origin main
162first="$(git rev-parse main)"
163
164# ----------------------------------------------------------------------
165# 1. A branch whose required check passes: the evidence and the request
166# reach origin in one push, and main does not move.
167git checkout --quiet -b topic/one
168echo "a better readme" > docs/readme.md
169git commit --quiet -am "improve the readme"
170git push --quiet origin topic/one
171digest="$(attest digest --rev topic/one | cut -d: -f2)"
172
173: > "$pushlog"
174printf 'a\n' > "$w/answers"
175valley review topic/one < "$w/answers" > "$w/ask.out" 2> "$w/ask.err"
176
177grep -q '^checking topic/one at .*: tree-ok$' "$w/ask.out"
178grep -qE '^check tree-ok +command passed$' "$w/ask.out"
179grep -qx "Submitted topic/one ($(git rev-parse --short topic/one)) for integration into origin/main." "$w/ask.out"
180grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/ask.out"
181grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/ask.out"
182grep -qx ' checks tree-ok' "$w/ask.out"
183grep -qx 'Your local checkout has not changed.' "$w/ask.out"
184grep -qx 'Check the result: valley status topic/one' "$w/ask.out"
185
186# What origin holds now. The attestation is keyed by the digest of the
187# BRANCH's tree, which is what says the checks ran over the right tree and
188# not over the operator's working copy.
189git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt"
190grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt"
191grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt"
192if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \
193 != "$(git rev-parse topic/one)" ]; then
194 echo "valley-request: the request ref does not name the branch's head" >&2
195 exit 1
196fi
197if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then
198 echo "valley-request: [a]sk moved main" >&2
199 exit 1
200fi
201
202# One push carried both. This is the friction the verb exists to remove:
203# `attest run --push` publishes the branch and the evidence and knows
204# nothing about a request ref, so doing this by hand is two beats.
205pushes="$(grep -c '^--- push' "$pushlog" || true)"
206if [ "$pushes" != 1 ]; then
207 echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2
208 cat "$pushlog" >&2
209 exit 1
210fi
211grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog"
212grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog"
213
214# The note origin holds is the one the operator signed, over that tree.
215git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \
216 > "$w/statement.note"
217attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys"
218attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \
219 --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out"
220grep -q 'matches the recorded subject digest' "$w/verify.out"
221
222# ----------------------------------------------------------------------
223# 2. The layers the derivation used are the ones that govern, and it says
224# which they were: the floor fetched from the owning valley, and the
225# project's own layer at policy/project in the TARGET's tip.
226# examples/policy/ is in this tree and requires `phantom` of every path,
227# and no default reaches it — it is derivable only by naming it, which
228# is what the options are for.
229valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err"
230grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out"
231grep -qx 'classes matched: prose' "$w/checks.out"
232grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out"
233if grep -q phantom "$w/checks.out" "$w/checks.err"; then
234 echo "valley-request: a default derived a check from examples/policy/" >&2
235 cat "$w/checks.out" "$w/checks.err" >&2
236 exit 1
237fi
238
239# Named, the example layers derive exactly what they declare. This is the
240# other half of the claim above: examples/policy/ is readable, and reading
241# it is something a person asks for.
242valley checks --instance examples/policy/instance --project examples/policy/project \
243 "$first" topic/one > "$w/example.out"
244grep -qE '^ phantom +mandatory' "$w/example.out"
245grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out"
246
247# ----------------------------------------------------------------------
248# 3. main moves on, so the next branch is stale. [a]sk is offered there
249# too — the integrator applies a delta to the tip rather than
250# fast-forwarding — and a branch whose check fails publishes nothing
251# and leaves the review loop usable.
252git checkout --quiet main
253echo "a note" > docs/notes.md
254git add -A
255git commit --quiet -m "a landing that moves main"
256git push --quiet origin main
257
258git checkout --quiet -b topic/two "$first"
259printf 'SECRET\n' > src/leak.txt
260git add -A
261git commit --quiet -m "a leak"
262git push --quiet origin topic/two
263
264: > "$pushlog"
265printf 'a\ns\n' > "$w/answers"
266valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err"
267
268# The prompt is the whole of what the operator is offered, so it is pinned
269# character for character: a verb that stops being offered, or one that
270# starts, is a change to the review loop and not an incidental one.
271grep -qF 'topic/two does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out"
272grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out"
273grep -qE '^check no-secrets +command failed$' "$w/fail.out"
274grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err"
275# The prompt is printed without a newline, so what follows a refusal shares
276# its line — the answer to it is not anchored.
277grep -q 'skipped topic/two; nothing done' "$w/fail.out"
278if [ -s "$pushlog" ]; then
279 echo "valley-request: a failing check pushed something" >&2
280 cat "$pushlog" >&2
281 exit 1
282fi
283git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt"
284if grep -q 'topic-two' "$w/after.txt"; then
285 echo "valley-request: a failing check left a ref on origin" >&2
286 cat "$w/after.txt" >&2
287 exit 1
288fi
289
290# ----------------------------------------------------------------------
291# 4. No signing key, with a check owed: refused before any check is run,
292# and the prompt still comes back. The host signs, and there is no
293# unsigned mode.
294: > "$pushlog"
295printf 'a\ns\n' > "$w/answers"
296env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err"
297grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err"
298grep -q 'skipped topic/two; nothing done' "$w/nokey.out"
299if [ -s "$pushlog" ]; then
300 echo "valley-request: a run with no signing key pushed something" >&2
301 exit 1
302fi
303
304# ----------------------------------------------------------------------
305# 5. A branch the policy requires no check of is filed as a request with
306# no evidence. The integrator lands one and records it as a landing
307# that required nothing, so refusing to file it here would strand
308# exactly the changes a policy leaves unrequired. Nothing is owed, so
309# nothing is signed: this runs with no signing key at all.
310git checkout --quiet main
311git checkout --quiet -b topic/quiet
312mkdir -p quiet
313echo "a quiet note" > quiet/note.md
314git add -A
315git commit --quiet -m "a change under a class that asks nothing"
316git push --quiet origin topic/quiet
317quiethead="$(git rev-parse --short topic/quiet)"
318
319: > "$pushlog"
320printf 'a\n' > "$w/answers"
321env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
322 > "$w/quiet.out" 2> "$w/quiet.err"
323
324grep -q 'owes no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out"
325grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out"
326grep -q '^Submitted topic/quiet .* for integration into origin/main\.$' "$w/quiet.out"
327grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out"
328grep -qx ' evidence none — nothing was owed, so nothing was signed' "$w/quiet.out"
329grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out"
330
331# One push, carrying the request and nothing else.
332pushes="$(grep -c '^--- push' "$pushlog" || true)"
333if [ "$pushes" != 1 ]; then
334 echo "valley-request: the bare request took $pushes pushes, not 1" >&2
335 cat "$pushlog" >&2
336 exit 1
337fi
338if grep -q 'refs/the-valley/attestations/' "$pushlog"; then
339 echo "valley-request: a request owing nothing published evidence" >&2
340 cat "$pushlog" >&2
341 exit 1
342fi
343if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \
344 != "$(git rev-parse topic/quiet)" ]; then
345 echo "valley-request: the bare request does not name the branch's head" >&2
346 exit 1
347fi
348
349# ----------------------------------------------------------------------
350# 6. [b]ase, and the prompt it comes back to. A stale branch is rebased
351# onto main in a throwaway worktree and re-reviewed, and the prompt the
352# loop then prints is the fast-forward one — the path a reader of
353# bin/valley has to trace two loops to reach, so it is exercised here
354# rather than argued about.
355git checkout --quiet main
356git checkout --quiet -b topic/three "$first"
357echo "another readme" > docs/readme.md
358git commit --quiet -am "another readme"
359git push --quiet origin topic/three
360before_base="$(git -C "$origin" rev-parse refs/heads/main)"
361
362printf 'b\ns\n' > "$w/answers"
363valley review topic/three < "$w/answers" > "$w/base.out" 2>&1
364grep -qF 'topic/three does not fast-forward from main: [a]sk, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out"
365grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out"
366# The post-[b]ase prompt: the branch fast-forwards now, so the loop falls
367# through to the prompt that has no [b]ase in it.
368grep -qF 'topic/three: [a]sk, [r]eject, [s]kip? ' "$w/base.out"
369grep -q 'skipped topic/three; nothing done' "$w/base.out"
370git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \
371 || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; }
372if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then
373 echo "valley-request: the review loop moved main" >&2
374 exit 1
375fi
376git checkout --quiet main
377
378# The request filed in 1 is still pending: nothing here consumes it, which
379# is the integrator's own step.
380git -C "$origin" rev-parse --verify --quiet \
381 refs/the-valley/integration-requests/main/topic-one > /dev/null
382
383# ----------------------------------------------------------------------
384# 7. A change does not supply the policy that gates it. This branch
385# deletes its own policy/project and repoints policy/valley at a
386# repository that does not exist. Both layers come from the target's
387# tip, so neither edit reaches the derivation: it composes the same two
388# layers it would for any other branch, and the fetch that a branch-read
389# pointer would have attempted never happens.
390git checkout --quiet -b topic/nolayer
391git rm --quiet -r policy/project
392echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley
393git add -A
394git commit --quiet -m "delete the project layer and repoint the valley"
395git push --quiet origin topic/nolayer
396
397valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err"
398grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out"
399if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then
400 echo "valley-request: the deriver followed the branch's own valley pointer" >&2
401 cat "$w/nolayer.out" "$w/nolayer.err" >&2
402 exit 1
403fi
404
405: > "$pushlog"
406printf 'a\n' > "$w/answers"
407valley review topic/nolayer < "$w/answers" \
408 > "$w/nolayer.review.out" 2> "$w/nolayer.review.err"
409grep -q '^Submitted topic/nolayer .* for integration into origin/main\.$' "$w/nolayer.review.out"
410
411# ----------------------------------------------------------------------
412# 8. The reviewer's checkout contributes nothing. This is the case that
413# failed in live use: the operator's checkout was an older branch
414# without the landed policy layer, and the deriver read the working
415# tree and refused. topic/nolayer is exactly such a checkout — no
416# policy/project in it, and a policy/valley naming nowhere — so every
417# derivation below runs from it.
418#
419# Byte-identical output is the claim, not merely a successful one:
420# review works the same from any branch, however stale.
421git checkout --quiet main
422valley checks "$first" topic/one > "$w/from-main.out"
423git checkout --quiet topic/nolayer
424valley checks "$first" topic/one > "$w/from-stale.out"
425if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then
426 echo "valley-request: the checkout changed what the derivation said" >&2
427 diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true
428 exit 1
429fi
430
431# And [a]sk, which is where it failed: a request filed from that same
432# checkout, over a branch the stale tree knows nothing about.
433git checkout --quiet -b topic/four origin/main
434echo "one more readme" > docs/readme.md
435git commit --quiet -am "one more readme"
436git push --quiet origin topic/four
437git checkout --quiet topic/nolayer
438
439: > "$pushlog"
440printf 'a\n' > "$w/answers"
441valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err"
442grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out"
443grep -q '^Submitted topic/four .* for integration into origin/main\.$' "$w/stale.out"
444grep -qx ' checks tree-ok' "$w/stale.out"
445
446# ----------------------------------------------------------------------
447# 9. A target carrying no project layer at all is derived under the floor
448# alone, and says so. The project layer only ever adds, so the identity
449# of "adds" is "adds nothing"; refusing instead would make a repository
450# underivable until its first policy commit, and that commit could not
451# derive either. This is the integrator's own reading of an absent
452# layer (integrator/policy.go), and the two have to agree.
453bare2="$TMPDIR/floorless.git"
454git init --quiet --bare "$bare2"
455repo2="$TMPDIR/floorless"
456git init --quiet "$repo2"
457cd "$repo2" || exit 1
458git remote add origin "$bare2"
459mkdir -p docs schema policy
460cp "$schemaFile" schema/verification.cue
461echo "the readme" > docs/readme.md
462echo "$valley" > policy/valley
463git add -A
464git commit --quiet -m base
465git push --quiet -u origin main
466base2="$(git rev-parse main)"
467echo "a second readme" > docs/readme.md
468git commit --quiet -am "a second readme"
469
470valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err"
471grep -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"
472grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out"
473if grep -q no-secrets "$w/floorless.out"; then
474 echo "valley-request: the floor-alone derivation saw another project's layer" >&2
475 exit 1
476fi
477cd "$repo" || exit 1
478git checkout --quiet main
479
480# ----------------------------------------------------------------------
481# 10. Resubmission after main moved. This is the case that wedged in live
482# use. A request goes stale with reason=evidence when main lands
483# inside a required check's input closure: no attestation over the
484# submitted tree can transfer again, so the cure is a new head rebased
485# onto the moved main. That head is not a descendant of the one the
486# request ref holds, which makes writing it a non-fast-forward
487# replacement — and a push without a lease is refused by git's own
488# rule before origin's hook is ever consulted. The verdict that sends
489# a change down this path is the integrator's and is pinned by
490# integrator-e2e; what is under test here is that [a]sk can act on it.
491git checkout --quiet -b topic/five origin/main
492echo "a readme worth asking about" > docs/readme.md
493git commit --quiet -am "a readme worth asking about"
494git push --quiet origin topic/five
495stalehead="$(git rev-parse topic/five)"
496staledigest="$(attest digest --rev topic/five | cut -d: -f2)"
497
498printf 'a\n' > "$w/answers"
499valley review topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err"
500grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out"
501
502# main moves, and the change is rebased onto it — the operator's half of
503# a resubmission, which [a]sk does not do and does not need to.
504git checkout --quiet main
505echo "a second note" > docs/second.md
506git add -A
507git commit --quiet -m "a landing under the same class"
508git push --quiet origin main
509git checkout --quiet topic/five
510git rebase --quiet main
511git push --quiet --force-with-lease origin topic/five
512newhead="$(git rev-parse topic/five)"
513newdigest="$(attest digest --rev topic/five | cut -d: -f2)"
514
515# The premise of the scenario, asserted rather than assumed: the rebase
516# produced a head off the old one's line and a tree the old evidence is
517# not about. Without both, the push below would be an ordinary one and
518# would prove nothing.
519if git merge-base --is-ancestor "$stalehead" "$newhead"; then
520 echo "valley-request: the rebased head still descends from the old one" >&2
521 exit 1
522fi
523if [ "$staledigest" = "$newdigest" ]; then
524 echo "valley-request: the rebase left the tree the old evidence is about" >&2
525 exit 1
526fi
527
528: > "$pushlog"
529printf 'a\n' > "$w/answers"
530valley review topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err"
531
532grep -q '^Submitted topic/five .* for integration into origin/main\.$' "$w/five-again.out"
533grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out"
534# Replacing a pending request is said out loud, and the sentence names the
535# head that was replaced.
536grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead") (a readme worth asking about)" "$w/five-again.out"
537
538if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \
539 != "$newhead" ]; then
540 echo "valley-request: the resubmitted request does not name the rebased head" >&2
541 exit 1
542fi
543
544# Both attestations stand. The old one is about a tree that will never
545# land, but the namespace is create-only and a record that can be dropped
546# is not one; the resubmission publishes beside it, never over it.
547for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \
548 "refs/the-valley/attestations/$newdigest/$keyhash"; do
549 git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || {
550 echo "valley-request: $stands is not on origin after the resubmission" >&2
551 exit 1
552 }
553done
554
555# One push again, and the request ref moved off the old head inside it.
556pushes="$(grep -c '^--- push' "$pushlog" || true)"
557if [ "$pushes" != 1 ]; then
558 echo "valley-request: the resubmission took $pushes pushes, not 1" >&2
559 cat "$pushlog" >&2
560 exit 1
561fi
562grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog"
563# And every attestation ref in that push is a creation: an all-zero old id
564# is what the create-only namespace admits, and a resubmission that
565# updated one would be refused by the real hook.
566if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \
567 | grep -qv '^0* '; then
568 echo "valley-request: the resubmission updated an attestation ref" >&2
569 cat "$pushlog" >&2
570 exit 1
571fi
572if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then
573 echo "valley-request: the resubmission moved main" >&2
574 exit 1
575fi
576
577# ----------------------------------------------------------------------
578# 11. Two resubmissions of one change race for one ref, and the loser
579# loses visibly. The request ref's value is read before the push and
580# the push is leased against exactly that value, so the second one to
581# arrive is refused rather than allowed to overwrite the first. The
582# race is made real: a receive-pack wrapper moves the ref on origin
583# after [a]sk has read it and before the push it then makes, which is
584# what another operator getting there first looks like from here.
585git checkout --quiet main
586git checkout --quiet -b topic/six origin/main
587echo "a readme two resubmissions both rebased" > docs/readme.md
588git commit --quiet -am "a readme two resubmissions both rebased"
589git push --quiet origin topic/six
590sixfiled="$(git rev-parse topic/six)"
591
592printf 'a\n' > "$w/answers"
593valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err"
594grep -q '^Submitted topic/six .* for integration into origin/main\.$' "$w/six-first.out"
595
596git checkout --quiet main
597echo "a third note" > docs/third.md
598git add -A
599git commit --quiet -m "another landing under the same class"
600git push --quiet origin main
601git checkout --quiet topic/six
602git rebase --quiet main
603git push --quiet --force-with-lease origin topic/six
604sixhead="$(git rev-parse topic/six)"
605sixdigest="$(attest digest --rev topic/six | cut -d: -f2)"
606
607# The other resubmission's head, built where origin can already reach
608# every object in it: the same tree, parented on the same tip, so it is
609# the commit a second operator rebasing this change would have produced.
610racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \
611 -p "$(git rev-parse origin/main)" -m "the other resubmission")"
612
613arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-six '$racer'"
614
615: > "$pushlog"
616printf 'a\ns\n' > "$w/answers"
617valley review topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err"
618disarm_race
619
620# A refusal, named as one, and the review loop still usable after it.
621grep -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"
622grep -q 'the lease on refs/the-valley/integration-requests/main/topic-six refused' "$w/race.err"
623grep -q 'skipped topic/six; nothing done' "$w/race.out"
624
625# Nothing published. The winner's request stands untouched, the loser's
626# evidence never reached origin, and origin saw no push at all — the lease
627# refuses before any command is sent, which is what --atomic is worth.
628if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \
629 != "$racer" ]; then
630 echo "valley-request: the lease did not hold; the other resubmission was clobbered" >&2
631 exit 1
632fi
633if git -C "$origin" rev-parse --verify --quiet \
634 "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then
635 echo "valley-request: a refused resubmission published its evidence" >&2
636 exit 1
637fi
638if [ -s "$pushlog" ]; then
639 echo "valley-request: a refused resubmission reached origin's hook" >&2
640 cat "$pushlog" >&2
641 exit 1
642fi
643
644# The checks that were run are not lost with the refusal: their notes are
645# in the operator's own repository, and the refusal says where.
646git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \
647 > /dev/null || {
648 echo "valley-request: the refused resubmission kept no evidence locally" >&2
649 exit 1
650}
651grep -qx 'valley: the evidence is stored locally at:' "$w/race.err"
652grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err"
653git checkout --quiet main
654
655# ----------------------------------------------------------------------
656# 12. Asking again at the same head. Evidence with a validity window
657# expires while the tree stays the right tree, and the answer to that
658# is a fresh observation over the same head. The request ref already
659# names that head, so it is left out of the refspec entirely rather
660# than pushed at the value origin already holds.
661#
662# The request is filed by hand here. Origin holding a request at this
663# head is the whole of the state under test, and re-running [a]sk to
664# produce it would put a second-granular observation instant in the
665# note: the same tree re-attested inside one second is the identical
666# ref, and across a second boundary it is not.
667git checkout --quiet -b topic/seven origin/main
668echo "a readme asked about twice" > docs/readme.md
669git commit --quiet -am "a readme asked about twice"
670git push --quiet origin topic/seven
671sevenhead="$(git rev-parse topic/seven)"
672sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)"
673git push --quiet origin \
674 "$sevenhead:refs/the-valley/integration-requests/main/topic-seven"
675
676: > "$pushlog"
677printf 'a\n' > "$w/answers"
678valley review topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err"
679
680grep -q '^Submitted topic/seven .* for integration into origin/main\.$' "$w/seven.out"
681grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out"
682# The evidence went; the request ref was not in the refspec at all.
683grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog"
684if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then
685 echo "valley-request: a same-head re-ask pushed the request ref again" >&2
686 cat "$pushlog" >&2
687 exit 1
688fi
689if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \
690 != "$sevenhead" ]; then
691 echo "valley-request: a same-head re-ask disturbed the request ref" >&2
692 exit 1
693fi
694
695# The same head with nothing owed: no evidence to publish and no ref to
696# write, so no push is made at all. topic/quiet's request has stood on
697# origin at this head since 5. A push assembled with an empty refspec
698# would fall back to origin's default one, and pushing whatever that
699# happens to name is not what asking about a change means.
700git checkout --quiet main
701: > "$pushlog"
702printf 'a\n' > "$w/answers"
703env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \
704 > "$w/quiet-again.out" 2> "$w/quiet-again.err"
705grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet-again.out"
706grep -qx ' the request already stood at this head and nothing was owed, so nothing needed publishing' "$w/quiet-again.out"
707if [ -s "$pushlog" ]; then
708 echo "valley-request: a re-ask with nothing to publish still pushed" >&2
709 cat "$pushlog" >&2
710 exit 1
711fi
712
713# ----------------------------------------------------------------------
714# 13. Two operators filing one change at once. Reading that the request
715# ref is absent is a value like any other, and the lease covers it:
716# git spells that expectation with an empty value and refuses if the
717# ref exists at all. Unleased, this push would have fast-forwarded the
718# other filing away without saying a word.
719git checkout --quiet -b topic/eight origin/main
720echo "a readme two operators both filed" > docs/readme.md
721git commit --quiet -am "a readme two operators both filed"
722git push --quiet origin topic/eight
723eighthead="$(git rev-parse topic/eight)"
724eightdigest="$(attest digest --rev topic/eight | cut -d: -f2)"
725# The other operator's filing: this same tree on this same tip, which is
726# what a second checkout of one branch produces. It is a descendant of
727# nothing here, so an unleased push over it would be a plain fast-forward.
728firstfiler="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/eight^{tree}')" \
729 -p "$(git rev-parse origin/main)" -m "the other operator's filing")"
730
731arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-eight '$firstfiler'"
732: > "$pushlog"
733printf 'a\ns\n' > "$w/answers"
734valley review topic/eight < "$w/answers" > "$w/create-race.out" 2> "$w/create-race.err"
735disarm_race
736
737# The other operator's commit is on origin and not here, so the subject
738# says so rather than inventing one. Scenario 10 pins the case where the
739# replaced head IS local and its subject is printed.
740grep -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"
741grep -q 'skipped topic/eight; nothing done' "$w/create-race.out"
742if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-eight)" \
743 != "$firstfiler" ]; then
744 echo "valley-request: an unleased filing clobbered the request that got there first" >&2
745 exit 1
746fi
747if git -C "$origin" rev-parse --verify --quiet \
748 "refs/the-valley/attestations/$eightdigest/$keyhash" > /dev/null; then
749 echo "valley-request: a refused filing published its evidence" >&2
750 exit 1
751fi
752if [ -s "$pushlog" ]; then
753 echo "valley-request: a refused filing reached origin's hook" >&2
754 cat "$pushlog" >&2
755 exit 1
756fi
757
758# ----------------------------------------------------------------------
759# 14. A lease that refuses because the ref is gone, not because it moved.
760# The integrator consumes the request ref when the change lands, so a
761# resubmission prepared just before that lands into a lease against a
762# ref that no longer exists. Git reports it as stale info either way,
763# and only reading the ref again tells the two apart — so the two are
764# told apart, and the operator is pointed at the landing rather than
765# at a race that did not happen.
766git checkout --quiet main
767git checkout --quiet -b topic/nine origin/main
768echo "a readme that lands mid-resubmission" > docs/readme.md
769git commit --quiet -am "a readme that lands mid-resubmission"
770git push --quiet origin topic/nine
771
772printf 'a\n' > "$w/answers"
773valley review topic/nine < "$w/answers" > "$w/nine-first.out" 2> "$w/nine-first.err"
774grep -q '^Submitted topic/nine .* for integration into origin/main\.$' "$w/nine-first.out"
775
776git checkout --quiet main
777echo "a fourth note" > docs/fourth.md
778git add -A
779git commit --quiet -m "a landing that moves main again"
780git push --quiet origin main
781git checkout --quiet topic/nine
782git rebase --quiet main
783git push --quiet --force-with-lease origin topic/nine
784ninedigest="$(attest digest --rev topic/nine | cut -d: -f2)"
785
786arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-nine"
787: > "$pushlog"
788printf 'a\ns\n' > "$w/answers"
789valley review topic/nine < "$w/answers" > "$w/consumed.out" 2> "$w/consumed.err"
790disarm_race
791
792grep -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"
793grep -q 'nothing was published; fetch and review the branch again before asking a second time' "$w/consumed.err"
794# Not reported as a race, which is the distinction the re-read buys.
795if grep -q 'the pending request moved from' "$w/consumed.err"; then
796 echo "valley-request: a consumed request was reported as a lost race" >&2
797 cat "$w/consumed.err" >&2
798 exit 1
799fi
800grep -q 'skipped topic/nine; nothing done' "$w/consumed.out"
801if git -C "$origin" rev-parse --verify --quiet \
802 refs/the-valley/integration-requests/main/topic-nine > /dev/null; then
803 echo "valley-request: a refused resubmission re-created the consumed request" >&2
804 exit 1
805fi
806if git -C "$origin" rev-parse --verify --quiet \
807 "refs/the-valley/attestations/$ninedigest/$keyhash" > /dev/null; then
808 echo "valley-request: a refused resubmission published its evidence" >&2
809 exit 1
810fi
811if [ -s "$pushlog" ]; then
812 echo "valley-request: a refused resubmission reached origin's hook" >&2
813 cat "$pushlog" >&2
814 exit 1
815fi
816
817# ----------------------------------------------------------------------
818# 15. The same-head push carries no request ref, so it carries no
819# compare-and-swap on one either — git leaves an up-to-date ref out of
820# what it sends. Where the ref stands after that push is therefore not
821# something the run established, and it is read again rather than
822# asserted from the read before it.
823#
824# Nothing re-files it. A request for a change already in the stream is
825# judged against an empty delta on every pass, which is why the
826# integrator consumes the ref in the first place; re-creating it here
827# would be a guess about which of consumption and deletion happened,
828# paid for with a second push behind the one this verb promises. So
829# the run says what is true and stops, and asking again is the repair.
830git checkout --quiet main
831git checkout --quiet -b topic/ten origin/main
832echo "a readme consumed under the evidence" > docs/readme.md
833git commit --quiet -am "a readme consumed under the evidence"
834git push --quiet origin topic/ten
835tenhead="$(git rev-parse topic/ten)"
836tendigest="$(attest digest --rev topic/ten | cut -d: -f2)"
837git push --quiet origin \
838 "$tenhead:refs/the-valley/integration-requests/main/topic-ten"
839
840arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-ten"
841: > "$pushlog"
842printf 'a\ns\n' > "$w/answers"
843valley review topic/ten < "$w/answers" > "$w/posthoc.out" 2> "$w/posthoc.err"
844disarm_race
845
846grep -q 'the request was consumed while this run was working, which is what the integrator does when a change lands' "$w/posthoc.err"
847grep -q 'Nothing here re-filed it' "$w/posthoc.err"
848# The claim the old code would have made, from a read that had gone out of
849# date under it.
850if grep -q 'the request already stood at this head' "$w/posthoc.out"; then
851 echo "valley-request: a consumed request was reported as still standing" >&2
852 cat "$w/posthoc.out" >&2
853 exit 1
854fi
855grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out"
856
857# The evidence went, because that push succeeded; the request did not come
858# back, because nothing here writes it.
859git -C "$origin" rev-parse --verify --quiet \
860 "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || {
861 echo "valley-request: the evidence push was reported as done and is not on origin" >&2
862 exit 1
863}
864if git -C "$origin" rev-parse --verify --quiet \
865 refs/the-valley/integration-requests/main/topic-ten > /dev/null; then
866 echo "valley-request: the consumed request was re-filed behind the operator" >&2
867 exit 1
868fi
869# And that evidence went in one push, which is still the whole claim.
870pushes="$(grep -c '^--- push' "$pushlog" || true)"
871if [ "$pushes" != 1 ]; then
872 echo "valley-request: the same-head re-ask took $pushes pushes, not 1" >&2
873 cat "$pushlog" >&2
874 exit 1
875fi
876git checkout --quiet main
877
878# ----------------------------------------------------------------------
879# The verbs the loop offers are the whole of what an operator can do to a
880# branch, so no path may offer one outside the set: [a]sk, [b]ase,
881# [r]eject, [s]kip. Every stream this check captured is scanned at the end,
882# which is what makes the claim about paths rather than about the two
883# prompts pinned character for character above — the fast-forward prompt,
884# the stale one, the one [b]ase comes back to, and the ones a refused [a]sk
885# returns to are all in here. A verb that writes the protected ref from the
886# operator's own checkout — the retired [i]ntegrate, or any successor to
887# it — fails this wherever it is printed.
888offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)"
889outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]sk|\[b\]ase|\[r\]eject|\[s\]kip' || true)"
890if [ -n "$outside" ]; then
891 echo "valley-request: a review path offered a verb outside the set:" >&2
892 printf '%s\n' "$outside" >&2
893 exit 1
894fi
895# The scan is only worth something if it saw the offers at all.
896for verb in '[a]sk' '[b]ase' '[r]eject' '[s]kip'; do
897 printf '%s\n' "$offered" | grep -qxF "$verb" \
898 || { echo "valley-request: no captured session offered $verb" >&2; exit 1; }
899done
900
901# No throwaway worktree outlived any of it: only the checkout is left.
902worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)"
903if [ "$worktrees" != 1 ]; then
904 echo "valley-request: a throwaway worktree was left behind" >&2
905 git worktree list >&2
906 exit 1
907fi
908
909echo "every scenario held"
910{
911 cat "$w/ask.out"
912 printf '\n--- the scratch origin, after all of it ---\n'
913 git -C "$origin" for-each-ref --format='%(objectname) %(refname)'
914} > "$out"