gunk-dev/the-valley

valley-request.sh

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