gunk-dev/the-valley

valley-changes.sh

sourcebd760cae2dadmain's published tip history
39.4 KiB
1# shellcheck shell=bash
2# valley changes and valley pending, end to end and unmocked, over the
3# repositories one test host serves. The host is made by the integrator
4# host's rendered valley-init, relocated as init-e2e relocates it, so the
5# project list discovery reads is the one a host publishes, and every
6# record is pushed through the hook that host installs. What the
7# integrator writes on disk when it lands a change — main, the outcome
8# record, the transfer statement — is written here the same way, with a
9# key generated for the run.
10# Paths and fixtures come from the derivation environment.
11# shellcheck disable=SC2154
12set -euo pipefail
13
14w="$TMPDIR/valley-changes"
15mkdir -p "$w"
16export HOME="$w"
17export XDG_CACHE_HOME="$w/cache"
18export GIT_CONFIG_NOSYSTEM=1
19git config --global user.name valley-changes-check
20git config --global user.email valley-changes-check@localhost
21git config --global init.defaultBranch main
22git config --global advice.detachedHead false
23unset VALLEY_KNOWN_SIGNERS VALLEY_INTEGRATOR_SIGNER
24
25fail() {
26 printf 'valley-changes: %s\n' "$*" >&2
27 for f in "$w"/out "$w"/err; do
28 if [ -s "$f" ]; then cat "$f" >&2; fi
29 done
30 exit 1
31}
32
33# ok <command…>: the command succeeds, and its output is kept.
34ok() {
35 "$@" > "$w/out" 2> "$w/err" || fail "$* failed"
36}
37
38# holds <jq condition…>: the last JSON printed holds each.
39holds() {
40 local cond
41 for cond in "$@"; do
42 jq -e "$cond" "$w/out" > /dev/null || fail "$cond does not hold"
43 done
44}
45
46# line <ERE>: the last output has a line matching it.
47line() {
48 grep -qE -- "$1" "$w/out" || fail "no line matches $1"
49}
50
51no_line() {
52 if grep -qE -- "$1" "$w/out"; then fail "a line matches $1"; fi
53}
54
55# after <ERE> <ERE…>: the first line of the last output that matches the
56# first is followed, line by line, by lines matching the rest.
57after() {
58 local n re
59 n="$(grep -nE -m 1 -- "$1" "$w/out" | cut -d: -f1 || true)"
60 [ -n "$n" ] || fail "no line matches $1"
61 for re in "${@:2}"; do
62 n=$((n + 1))
63 sed -n "${n}p" "$w/out" | grep -qE -- "$re" || fail "line $n, after $1, does not match $re"
64 done
65}
66
67# The host: guarded, open, released and sealed, with sealed the instance
68# repository. valley-init creates them, wires their hooks, and publishes
69# the project list in sealed.
70data="$w/srv/git"
71mkdir -p "$data"
72sed "s|/srv/git|$data|g" "$initScriptPath" > "$w/init.sh"
73bash "$w/init.sh" 2> "$w/init.err" || fail "valley-init failed: $(cat "$w/init.err")"
74instance="$data/sealed.git"
75# The integrator's repositories are group-shared, and git then sets the
76# setgid bit on every directory it makes, which this sandbox refuses. The
77# setting is dropped so the pushes below can write objects; it decides
78# nothing this check reads.
79for name in guarded open released sealed; do
80 git -C "$data/$name.git" config --unset core.sharedRepository || true
81done
82[ "$(git -C "$instance" cat-file blob refs/the-valley/projects)" = "$(printf '%s\n' guarded open released sealed)" ] ||
83 fail "valley-init published another project list"
84
85# A change's ID, by the letter it repeats. Each is 32 letters from k to z.
86id() { printf '%s%s' "$1" "$(printf 'k%.0s' $(seq $((32 - ${#1}))))"; }
87A="$(id nxvpwlkm)" B="$(id mmmm)" C="$(id pppp)" D="$(id qqqq)" E="$(id rrrr)"
88W="$(id ssss)" F="$(id tttt)" G="$(id uuuu)" U="$(id vvvv)" H="$(id wwww)"
89K="$(id kkkk)" X="$(id oooo)" I="$(id zzzz)" J="$(id lmlm)" X2="$(id pqpq)"
90R="$(id nxvpwlkz)" D2="$(id xxxx)" E2="$(id yyyy)"
91
92# A checkout of a project, whose main names sealed as its owning valley.
93# Every push carries the principal, as the key sshd authenticated would.
94export VALLEY_PRINCIPAL=alice
95checkout() {
96 local name="$1" dir="$w/$1"
97 git clone --quiet "$data/$name.git" "$dir" 2> /dev/null
98 git -C "$dir" config valley.principal alice
99 mkdir -p "$dir/policy"
100 printf '# the owning valley\n%s\n' "$instance" > "$dir/policy/valley"
101 git -C "$dir" add policy/valley
102 git -C "$dir" commit --quiet -m base
103 VALLEY_PRINCIPAL=integrator git -C "$dir" push --quiet origin main
104}
105
106# branch <name> <from> <subject…>: a branch from a commit, with a commit
107# per subject, each adding a file of its own, pushed to origin.
108branch() {
109 local name="$1" from="$2" s
110 shift 2
111 git checkout --quiet -b "$name" "$from"
112 for s in "$@"; do
113 echo "$s" > "$s.txt"
114 git add "$s.txt"
115 git commit --quiet -m "$s"
116 done
117 git push --quiet origin "$name"
118}
119
120# ready <id> <branch>: the branch's head readied as the change.
121ready() {
122 ok valley ready --change "$1" "$2"
123}
124
125# ----------------------------------------------------------------------
126# open: a stack of three, a draft, a withdrawn change with a ready change
127# on top of it, a change landed by fast-forward, one landed as a new
128# commit with its transfer statement, one whose request was consumed with
129# no evidence stored, one whose transfer statement another key signed,
130# and a branch no record is on.
131checkout open
132cd "$w/open"
133base="$(git rev-parse HEAD)"
134branch stack-a "$base" stack-a
135ready "$A" stack-a
136branch stack-b stack-a stack-b
137ready "$B" stack-b
138branch stack-c stack-b stack-c
139ready "$C" stack-c
140branch paused "$base" paused
141ready "$D" paused
142ok valley unready "$D"
143branch gone "$base" gone
144ready "$E" gone
145ok valley withdraw "$E"
146branch atop-gone gone atop-gone
147ready "$W" atop-gone
148branch ff "$base" ff
149ready "$F" ff
150branch reparent "$base" reparent
151ready "$G" reparent
152branch lost "$base" lost
153ready "$U" lost
154branch forged "$base" forged
155ready "$H" forged
156branch loose "$base" loose
157
158# Before anything lands, ff names its change.
159ok valley-go resolve ff
160line "^change"$'\t'"$F"$'\t'
161
162# The integrator's keys: the integrator's own, and another a known
163# signer holds, which may sign evidence and not a landing.
164ssh-keygen -q -t ed25519 -N '' -C integrator -f "$w/integrator.key"
165ssh-keygen -q -t ed25519 -N '' -C mallory -f "$w/mallory.key"
166{
167 attest key --key "$w/integrator.key" --name integrator
168 attest key --key "$w/mallory.key" --name mallory
169} > "$w/known-signers"
170
171# F lands by fast-forward.
172VALLEY_PRINCIPAL=integrator git push --quiet origin ff:main
173fhead="$(git rev-parse ff)"
174
175# G lands as a new commit on main, and the integrator records it: main
176# moves, the outcome record says land, the request is consumed, and the
177# transfer statement is stored under the landed tree.
178git checkout --quiet -b landing ff
179git cherry-pick reparent > /dev/null
180landed="$(git rev-parse HEAD)"
181VALLEY_PRINCIPAL=integrator git push --quiet origin landing:main
182tree_digest() { attest digest --repo "$w/open" --rev "$1" | cut -d: -f2; }
183
184# memo <name> <head> <decision…>: the integrator's outcome record.
185memo() {
186 local name="$1" head="$2" blob
187 shift 2
188 blob="$(printf '%s %s %s %s\n' "$head" "$fhead" 0123456789abcdef "$*" | git -C "$data/open.git" hash-object -w --stdin)"
189 git -C "$data/open.git" update-ref "refs/the-valley/integration-outcomes/main/$name" "$blob"
190}
191
192# transfer <change> <head> <landed> <key> <name> [single]: a transfer
193# statement, as the integrator composes, signs and stores one, at
194# transfer.d/<2 hex>/<2 hex>/<rest>.note, split from the landed commit's
195# ID. With single, it is stored as the one transfer.note at the top of the
196# tree, the layout of a ref the integrator wrote before it kept one
197# statement per landed commit, which the reader still reads.
198transfer() {
199 local change="$1" head="$2" at="$3" key="$4" name="$5" layout="${6:-}" ldigest blob tree
200 ldigest="$(tree_digest "$at")"
201 jq -n --arg change "$change" --arg base "$(tree_digest "$head")" --arg landed "$ldigest" \
202 --arg commit "$at" --arg tip "$fhead" '{
203 statementType: "the-valley/attestation/v1",
204 subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $landed, "git-sha1": $commit}},
205 predicateType: "the-valley/integration/transfer/v1",
206 predicate: {change: $change, target: "refs/heads/main", base: {"valley-tree-v1": $base},
207 policy: {sha256: ("ab" * 32), "git-sha1": $tip}, required: "none"}
208 }' > "$w/statement.json"
209 attest render "$w/statement.json" | attest sign --key "$key" --name "$name" > "$w/transfer.note"
210 blob="$(git -C "$data/open.git" hash-object -w "$w/transfer.note")"
211 if [ "$layout" = single ]; then
212 tree="$(printf '100644 blob %s\ttransfer.note\n' "$blob" | git -C "$data/open.git" mktree)"
213 else
214 tree="$(printf '100644 blob %s\t%s.note\n' "$blob" "${at:4}" | git -C "$data/open.git" mktree)"
215 tree="$(printf '040000 tree %s\t%s\n' "$tree" "${at:2:2}" | git -C "$data/open.git" mktree)"
216 tree="$(printf '040000 tree %s\t%s\n' "$tree" "${at:0:2}" | git -C "$data/open.git" mktree)"
217 tree="$(printf '040000 tree %s\ttransfer.d\n' "$tree" | git -C "$data/open.git" mktree)"
218 fi
219 git -C "$data/open.git" update-ref "refs/the-valley/attestations/$ldigest/$(printf '%08x' "${#name}")" "$tree"
220}
221memo reparent "$(git rev-parse reparent)" land
222transfer reparent "$(git rev-parse reparent)" "$landed" "$w/integrator.key" integrator single
223
224# U's request was consumed, and no evidence was stored.
225memo lost "$(git rev-parse lost)" land
226# H's request was consumed too, and its transfer statement is signed by a
227# known key that is not the integrator's: it is no record of a landing.
228memo forged "$(git rev-parse forged)" land
229git checkout --quiet -b landing-h landing
230git cherry-pick forged > /dev/null
231VALLEY_PRINCIPAL=integrator git push --quiet origin landing-h:main
232transfer forged "$(git rev-parse forged)" "$(git rev-parse HEAD)" "$w/mallory.key" mallory
233# A's request is pending.
234git -C "$data/open.git" update-ref refs/the-valley/integration-requests/main/stack-a "$(git rev-parse stack-a)"
235
236# I lands as a new commit, L2, filed as c-<I>, and the integrator records
237# it. Then I's next version, H', has H's tree with L2 as its parent. Its
238# name and its tree digest match the transfer statement, but it is another
239# delta: merged onto L2's parent it does not give L2's tree. I is then made
240# a draft.
241branch again "$base" again
242ready "$I" again
243hhead="$(git rev-parse again)"
244git checkout --quiet -b landing-i landing-h
245git cherry-pick again > /dev/null
246landed2="$(git rev-parse HEAD)"
247VALLEY_PRINCIPAL=integrator git push --quiet origin landing-i:main
248memo "c-$I" "$hhead" land
249transfer "c-$I" "$hhead" "$landed2" "$w/integrator.key" integrator
250hprime="$(git commit-tree "again^{tree}" -p "$landed2" -m again)"
251git push --quiet --force origin "$hprime:refs/heads/again"
252ready "$I" again
253ok valley unready "$I"
254
255# J lands by fast-forward, and the integrator records it: its outcome
256# record says land at J's head, and its transfer statement is about J's
257# tree. Then main moves on to Q, which adds a file, and J's next version,
258# J', has J's tree with Q as its parent: it deletes Q's file. J' matches
259# the statement's name and tree, and merged onto the parent of the commit
260# that landed it gives that commit's tree. But the outcome record names
261# J's head, not J', so J' has not landed. J is then made a draft.
262branch ffrec landing-i ffrec
263ready "$J" ffrec
264jhead="$(git rev-parse ffrec)"
265VALLEY_PRINCIPAL=integrator git push --quiet origin ffrec:main
266memo ffrec "$jhead" land
267transfer ffrec "$jhead" "$jhead" "$w/integrator.key" integrator
268git checkout --quiet -b moved-on ffrec
269echo q > q.txt
270git add q.txt
271git commit --quiet -m q
272VALLEY_PRINCIPAL=integrator git push --quiet origin moved-on:main
273jprime="$(git commit-tree "ffrec^{tree}" -p moved-on -m ffrec)"
274git push --quiet --force origin "$jprime:refs/heads/ffrec"
275ready "$J" ffrec
276ok valley unready "$J"
277
278# K is built on main after F, whose active version landed by ancestry
279# while its record is still ready. K's branch starts at main's tip, so F's
280# head is below it, and F has landed: K includes nothing.
281branch after-landing moved-on after-landing
282ready "$K" after-landing
283
284# ----------------------------------------------------------------------
285# guarded: a ready change whose ID shares seven letters with A's, a draft
286# and a withdrawn change.
287checkout guarded
288cd "$w/guarded"
289gbase="$(git rev-parse HEAD)"
290branch g-ready "$gbase" g-ready
291ready "$R" g-ready
292branch g-draft "$gbase" g-draft
293ready "$D2" g-draft
294ok valley unready "$D2"
295branch g-gone "$gbase" g-gone
296ready "$E2" g-gone
297ok valley withdraw "$E2"
298# B's change ID, recorded here too.
299branch g-dup "$gbase" g-dup
300ready "$B" g-dup
301
302# A merge on main can drop the content of either parent, so no head below
303# one has landed by ancestry. X is ready, and a merge of it whose tree
304# drops its changes lands on main by fast-forward: M = (main, X), with
305# main's tree. Then X2, branched from that main, is dropped by a merge with
306# its parents reversed: M2 = (X2, main), with main's tree. X2 is on main's
307# first-parent history, below the merge that dropped it.
308branch dropped "$gbase" dropped
309ready "$X" dropped
310merge="$(git commit-tree "$gbase^{tree}" -p "$gbase" -p dropped -m 'merge, dropping X')"
311VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge:refs/heads/main"
312branch reversed "$merge" reversed
313ready "$X2" reversed
314merge2="$(git commit-tree "$merge^{tree}" -p reversed -p "$merge" -m 'merge, dropping X2')"
315VALLEY_PRINCIPAL=integrator git push --quiet origin "$merge2:refs/heads/main"
316
317# origin: a project named origin, recording B's change ID too. A host that
318# declared it would list it, so it is added to the list on disk, as
319# valley-init writes the list.
320git init --quiet --bare "$data/origin.git"
321checkout origin
322cd "$w/origin"
323branch o-dup "$(git rev-parse HEAD)" o-dup
324ready "$B" o-dup
325projects="$(printf '%s\n' guarded open origin released sealed | git -C "$instance" hash-object -w --stdin)"
326git -C "$instance" update-ref refs/the-valley/projects "$projects"
327
328# released is listed and cannot be read.
329mv "$data/released.git" "$data/released.moved"
330
331export VALLEY_KNOWN_SIGNERS="$w/known-signers" VALLEY_INTEGRATOR_SIGNER=integrator
332
333# ----------------------------------------------------------------------
334# One repository: every record in every state, as valley-changes/v1.
335cd "$w/open"
336ok valley changes --json
337[ "$(wc -l < "$w/out")" = 1 ] || fail "changes printed more than one line"
338holds '.schema == "valley-changes/v1"' '.host == "localhost"' '(.repos | length) == 1' \
339 '.repos[0].repo == "open"' ".repos[0].url == \"$data/open.git\"" '.repos[0].error == null' \
340 '(.repos[0].changes | length) == 13' '.repos[0] | has("drafts") | not'
341c() { printf '.repos[0].changes[] | select(.change_id == "%s")' "$1"; }
342holds "$(c "$A") | .handle == \"nxvp\" and .state == \"ready\" and .owner == \"alice\" and .target == \"main\" and .active == 1 and .seq == 1" \
343 "$(c "$A") | .record == \"$(git -C "$data/open.git" rev-parse "refs/the-valley/changes/main/$A")\"" \
344 "$(c "$A") | .versions[0] | .n == 1 and .title == \"stack-a\" and .branch == \"stack-a\" and .uploader == \"alice\"" \
345 "$(c "$A") | .versions[0] | .head == \"$(git rev-parse stack-a)\" and .landing == \"not-landed\" and .landed_commit == null" \
346 "$(c "$A") | .versions[0].request == {ref: \"refs/the-valley/integration-requests/main/stack-a\", commit: \"$(git rev-parse stack-a)\"}" \
347 "$(c "$A") | .versions[0].outcome == null" \
348 "$(c "$D") | .state == \"draft\" and .seq == 2" \
349 "$(c "$E") | .state == \"withdrawn\""
350# Landing evidence, one version each way.
351holds "$(c "$F") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$fhead\"" \
352 "$(c "$G") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed\"" \
353 "$(c "$G") | .versions[0].outcome == {decision: \"land\", head: \"$(git rev-parse reparent)\", tip: \"$fhead\"}" \
354 "$(c "$G") | .versions[0].request == null" \
355 "$(c "$U") | .versions[0] | .landing == \"uncertain\" and .landed_commit == null" \
356 "$(c "$H") | .versions[0].landing == \"uncertain\"" \
357 "$(c "$J") | .versions[0] | .landing == \"landed\" and .landed_commit == \"$jhead\"" \
358 "$(c "$J") | .versions[1] | .head == \"$jprime\" and .landing == \"not-landed\" and .landed_commit == null" \
359 "$(c "$J") | .state == \"draft\" and .active == 2" \
360 "$(c "$I") | .versions[0] | .landing == \"landed-recorded\" and .landed_commit == \"$landed2\"" \
361 "$(c "$I") | .versions[1] | .head == \"$hprime\" and .landing == \"not-landed\" and .landed_commit == null" \
362 "$(c "$I") | .state == \"draft\" and .active == 2"
363# includes is ancestry, nearest only.
364holds "$(c "$A") | .includes == []" "$(c "$B") | .includes == [\"$A\"]" \
365 "$(c "$C") | .includes == [\"$B\"]" "$(c "$W") | .includes == [\"$E\"]" \
366 "$(c "$F") | .state == \"ready\" and .versions[0].landing == \"landed\"" "$(c "$K") | .includes == []"
367# The one branch no record is on, and no branch main reaches.
368holds '.repos[0].branches == [{branch: "loose", head: "'"$(git rev-parse loose)"'"}]'
369
370# Without the registry's keys, the transfer statement is not believed, and
371# that is said.
372env -u VALLEY_KNOWN_SIGNERS valley changes --json > "$w/out" 2> "$w/err" || fail "changes failed without the keys"
373holds "$(c "$G") | .versions[0].landing == \"uncertain\""
374grep -q 'could not be verified' "$w/err" || fail "an unverified transfer statement was not reported"
375
376# ----------------------------------------------------------------------
377# Every project the host publishes. Handles are unique across both
378# repositories, the repository that cannot be read carries its error, and
379# the others are listed all the same.
380ok valley changes --json --all
381holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]' \
382 '.repos[] | select(.repo == "released") | .error != null and .changes == [] and .url == "'"$data/released.git"'"' \
383 '.repos[] | select(.repo != "released") | .error == null' \
384 '.repos[] | select(.repo == "sealed") | .changes == []' \
385 '[.repos[].changes[].handle] | length == (unique | length)' \
386 "[.repos[].changes[] | select(.change_id == \"$A\" or .change_id == \"$R\") | .handle] == [\"nxvpwlkz\", \"nxvpwlkm\"]" \
387 "[.repos[].changes[] | select(.change_id == \"$B\") | .handle] == [\"guarded:mmmm\", \"open:mmmm\", \"origin:mmmm\"]" \
388 '.repos[] | select(.repo == "guarded") | [.changes[].state] == ["ready", "ready", "ready", "ready", "draft", "withdrawn"]'
389# ls-remote matches a ref's tail, so refs whose names end like the
390# refs discovery reads must not stand in for them: one in open whose
391# policy/valley names another instance, and one in the instance listing
392# another project.
393git checkout --quiet -b decoy "$base"
394printf '%s\n' "$w/elsewhere.git" > policy/valley
395git commit --quiet -am decoy
396git push --quiet origin decoy:refs/heads/a/refs/heads/main
397decoy_list="$(printf 'decoy\n' | git -C "$instance" hash-object -w --stdin)"
398git -C "$instance" update-ref refs/tags/a/refs/the-valley/projects "$decoy_list"
399git checkout --quiet landing
400ok valley changes --json --all
401holds '[.repos[].repo] == ["guarded", "open", "origin", "released", "sealed"]'
402git push --quiet origin :refs/heads/a/refs/heads/main
403git -C "$instance" update-ref -d refs/tags/a/refs/the-valley/projects
404# --repo limits the listing to one repository, read through its clone.
405ok valley changes --json --repo guarded
406holds '[.repos[].repo] == ["guarded"]' "[.repos[0].changes[].change_id] == [\"$B\", \"$R\", \"$X\", \"$X2\", \"$D2\", \"$E2\"]" \
407 '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]' \
408 "[.repos[0].changes[] | select(.change_id == \"$X\" or .change_id == \"$X2\") | .versions[0] | [.landing, .landed_commit]] == [[\"not-landed\", null], [\"not-landed\", null]]"
409cd "$w/guarded"
410ok valley changes --json
411holds '[.repos[].repo] == ["guarded"]' '[.repos[0].changes[].handle] == ["mmmm", "nxvp", "oooo", "pqpq", "xxxx", "yyyy"]'
412# A qualified handle names the change in the repository it qualifies, and
413# a qualifier naming another repository is refused.
414ok valley-go resolve guarded:mmmm
415line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse g-dup)"$'\t'
416cd "$w/open"
417ok valley-go resolve open:mmmm
418line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse stack-b)"$'\t'
419if valley-go resolve origin:mmmm > "$w/out" 2> "$w/err"; then fail "open resolved a handle qualified by origin"; fi
420grep -q 'origin:mmmm names a change in origin, and this repository is open' "$w/err" || fail "origin:mmmm was refused without saying why"
421cd "$w/origin"
422ok valley-go resolve origin:mmmm
423line "^change"$'\t'"$B"$'\t'"mmmm"$'\t'"1"$'\t'"$(git rev-parse o-dup)"$'\t'
424ok valley status origin:mmmm
425cd "$w/guarded"
426if valley changes --json --repo nowhere > "$w/out" 2> "$w/err"; then fail "--repo took a project the host does not list"; fi
427grep -q 'lists no project nowhere' "$w/err" || fail "--repo nowhere was refused without saying why"
428cd "$w/open"
429
430# A repository whose main names no owning valley lists itself alone, and
431# says discovery is unavailable.
432git init --quiet --bare "$w/solo.git"
433git clone --quiet "$w/solo.git" "$w/solo" 2> /dev/null
434git -C "$w/solo" commit --quiet --allow-empty -m solo
435git -C "$w/solo" push --quiet origin main
436(cd "$w/solo" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed without discovery"
437holds '[.repos[].repo] == ["solo"]'
438grep -q 'discovery is unavailable' "$w/err" || fail "changes --all did not say discovery is unavailable"
439
440# A policy/valley that git would read as an option, or that names a remote
441# helper, is refused before git sees it, and nothing it names runs, even
442# where the caller's own configuration allows ext::.
443for url in "--upload-pack=touch $w/pwned; git-upload-pack" "ext::sh -c touch% $w/pwned"; do
444 rm -rf "$w/evil.git" "$w/evil"
445 git init --quiet --bare "$w/evil.git"
446 git clone --quiet "$w/evil.git" "$w/evil" 2> /dev/null
447 mkdir -p "$w/evil/policy"
448 printf '%s\n' "$url" > "$w/evil/policy/valley"
449 git -C "$w/evil" add policy
450 git -C "$w/evil" commit --quiet -m evil
451 git -C "$w/evil" push --quiet origin main
452 export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=protocol.ext.allow GIT_CONFIG_VALUE_0=always
453 (cd "$w/evil" && valley changes --json --all) > "$w/out" 2> "$w/err" || fail "changes --all failed over policy/valley $url"
454 holds '[.repos[].repo] == ["evil"]'
455 grep -q 'discovery is unavailable' "$w/err" || fail "policy/valley $url was not refused"
456 if (cd "$w/evil" && valley changes --json --repo other) > "$w/out" 2> "$w/err"; then
457 fail "--repo read through policy/valley $url"
458 fi
459 unset GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0
460 [ ! -e "$w/pwned" ] || fail "policy/valley $url ran a command"
461done
462
463# ----------------------------------------------------------------------
464# pending: a block per entry, in three sections. A stack is its top
465# entry, then every entry the top contains, as one list, each counted from
466# the entry it contains. atop-lost is a branch no record is on, holding U,
467# a ready change. a-parked is a branch last committed to eleven days ago.
468# deep-4 tops a chain of four branches. fork-left and fork-right are two
469# branches on one base, fork-base. The chain and the fork were last
470# committed to twenty days ago.
471branch atop-lost lost atop-lost
472parked="@$(($(date +%s) - 11 * 86400))"
473GIT_AUTHOR_DATE="$parked" GIT_COMMITTER_DATE="$parked" branch a-parked "$base" a-parked
474old="@$(($(date +%s) - 20 * 86400))"
475export GIT_AUTHOR_DATE="$old" GIT_COMMITTER_DATE="$old"
476branch deep-1 "$base" deep-1
477branch deep-2 deep-1 deep-2
478branch deep-3 deep-2 deep-3
479branch deep-4 deep-3 deep-4
480branch fork-base "$base" fork-base
481branch fork-left fork-base fork-left
482branch fork-right fork-base fork-right
483unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE
484ok valley pending
485no_line $'\x1b'
486# pending also writes the handle cache completion reads: one line for each
487# change not landed or withdrawn, ready changes first.
488cache="$(git rev-parse --git-common-dir)/valley/handles"
489[ -f "$cache" ] || fail "pending wrote no handle cache at $cache"
490cut -f1,2 "$cache" > "$w/out"
491[ "$(cat "$w/out")" = "$(printf '%s\t%s\n' kkkk ready mmmm ready nxvp ready pppp ready ssss ready vvvv ready wwww ready lmlm draft qqqq draft zzzz draft)" ] ||
492 fail "the handle cache lists other changes than the ready and the drafts"
493grep -qE "^pppp"$'\t'"ready"$'\t'"[^"$'\t'"]*"$'\t'"stack-c$" "$cache" || fail "the handle cache has no line for pppp with its title"
494grep -qE "^qqqq"$'\t'"draft"$'\t'"paused"$'\t' "$cache" || fail "the handle cache has no line for qqqq with its branch"
495ok valley pending
496line '^ready$'
497# The stack of three, outermost first, each layer counted on its own.
498after '^pppp v1 +[0-9]+ [a-z]+ ago · valley-changes-check$' '^ stack-c$' '^ 1 commit · 1 file \+1 −0$' \
499 '^ approving it lands the 2 below too$' \
500 '^ ├ mmmm v1 +.* ago · valley-changes-check$' '^ │ stack-b$' '^ │ 1 commit · 1 file \+1 −0$' \
501 '^ └ nxvp v1 +.* ago · valley-changes-check$' '^ stack-a$' '^ 1 commit · 1 file \+1 −0$' '^$'
502# W includes E, which is withdrawn, so it is counted from main.
503after '^ssss v1 ' '^ atop-gone$' '^ 2 commits · 2 files \+2 −0$' '^ includes rrrr \(withdrawn\)$' '^$'
504after '^wwww v1 ' '^ forged$' '^ 1 commit · 1 file \+1 −0$' '^ uncertain: it may have landed without its evidence being stored$'
505no_line '(tttt|uuuu|rrrr) v1'
506no_line 'qqqq v1 '
507# The branches, latest first, and atop-lost with U listed under it,
508# marked as ready.
509after '^branches$' '^atop-lost +.* ago · valley-changes-check$' '^ atop-lost$' '^ 1 commit · 1 file \+1 −0$' \
510 '^ approving it lands the one below too$' \
511 '^ └ vvvv v1 \(ready\) +.* ago · valley-changes-check$' '^ lost$' '^ 1 commit · 1 file \+1 −0$' \
512 '^ uncertain: ' '^$' '^loose +.* ago · valley-changes-check$' '^ loose$' '^ 1 commit · 1 file \+1 −0$' '^$' \
513 '^a-parked +11 days ago · valley-changes-check$' '^ a-parked$' '^ 1 commit · 1 file \+1 −0$' '^$' '^deep-4 '
514# A chain four deep is one flat list, with no on line: each entry is on
515# the one listed after it.
516after '^deep-4 +3 weeks ago · valley-changes-check$' '^ deep-4$' '^ 1 commit · 1 file \+1 −0$' \
517 '^ approving it lands the 3 below too$' \
518 '^ ├ deep-3 +3 weeks ago · valley-changes-check$' '^ │ deep-3$' '^ │ 1 commit · 1 file \+1 −0$' \
519 '^ ├ deep-2 +3 weeks ago · valley-changes-check$' '^ │ deep-2$' '^ │ 1 commit · 1 file \+1 −0$' \
520 '^ └ deep-1 +3 weeks ago · valley-changes-check$' '^ deep-1$' '^ 1 commit · 1 file \+1 −0$' '^$'
521no_line '^ *(│ *)?on deep-'
522# Of two branches on one base, one lists the base, and the other says it
523# is on it. The base is listed once.
524[ "$(grep -cE '(^|├ |└ )fork-base ' "$w/out")" = 1 ] || fail "pending did not list fork-base exactly once"
525[ "$(grep -cx ' on fork-base' "$w/out")" = 1 ] || fail "not exactly one of the fork says it is on fork-base"
526after '^fork-left ' '^ fork-left$' '^ 1 commit · 1 file \+1 −0$' '^ approving it lands the one below too$' \
527 '^ └ fork-base ' '^ fork-base$' '^ 1 commit · 1 file \+1 −0$' '^$'
528after '^fork-right ' '^ fork-right$' '^ 1 commit · 1 file \+1 −0$' '^ on fork-base$' '^$' '^drafts$'
529after '^qqqq v1 on paused +.* ago · valley-changes-check$' '^ paused$' '^ 1 commit · 1 file \+1 −0$'
530after '^zzzz v2 on again ' '^ again$' '^ 1 commit · '
531after '^lmlm v2 on ffrec ' '^ ffrec$' '^ 1 commit · 1 file \+0 −1$'
532[ "$(grep -n '^ready$' "$w/out" | cut -d: -f1)" -lt "$(grep -n '^branches$' "$w/out" | cut -d: -f1)" ] ||
533 fail "pending listed the branches before the ready changes"
534# --json is the same listing in valley-changes/v1's shape: the pending
535# changes alone in changes, and the drafts in drafts.
536ok valley pending --json
537holds '.schema == "valley-pending/v1"' \
538 '[.repos[0].changes[].handle] == ["kkkk", "mmmm", "nxvp", "pppp", "ssss", "vvvv", "wwww"]' \
539 '[.repos[0].changes[].state] | unique == ["ready"]' \
540 '[.repos[0].drafts[] | [.handle, .state]] == [["lmlm", "draft"], ["qqqq", "draft"], ["zzzz", "draft"]]' \
541 '[.repos[0].branches[].branch] == ["a-parked", "atop-lost", "deep-1", "deep-2", "deep-3", "deep-4", "fork-base", "fork-left", "fork-right", "loose"]'
542# Across the host, each repository is headed by its name, and handles
543# are qualified where a change ID is recorded in more than one. In origin,
544# a branch no record is on holds the one ready change, so the ready
545# section says where it is.
546(cd "$w/origin" && branch atop-dup o-dup atop-dup)
547ok valley pending --all
548after '^guarded$' '^(─)+$' '^ready$'
549after '^nxvpwlkz v1 ' '^ g-ready$' '^ 1 commit · 1 file \+1 −0$'
550after '^guarded:mmmm v1 ' '^ g-dup$'
551after '^pqpq v1 ' '^ reversed$' '^ no commits · ' '^ approving it lands the one below too$' '^ └ oooo v1 ' \
552 '^ dropped$' '^ no commits · no file changes$'
553line '^xxxx v1 on g-draft '
554line '^open$'
555after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ approving it lands the 2 below too$' '^ ├ open:mmmm v1 ' \
556 '^ │ stack-b$' '^ │ 1 commit · ' '^ └ nxvpwlkm v1 ' '^ stack-a$'
557after '^origin$' '^(─)+$' '^nothing is ready on its own; 1 ready change is in a stack under branches$' '^$' \
558 '^branches$' '^atop-dup ' '^ atop-dup$' '^ 1 commit · ' '^ approving it lands the one below too$' \
559 '^ └ origin:mmmm v1 \(ready\) ' '^ o-dup$'
560line '^released: could not read '
561after '^sealed$' '^(─)+$' '^nothing is ready$'
562
563# --checks adds a line to each change with the checks it requires, by
564# cost, as valley checks derives them, here and in a repository read
565# through its clone. The instance gets a floor for that, written to its
566# main on disk as the integrator would.
567git init --quiet "$w/floor"
568mkdir -p "$w/floor/policy/instance"
569cp "$floor"/*.cue "$w/floor/policy/instance/"
570git -C "$w/floor" add policy
571git -C "$w/floor" commit --quiet -m floor
572git -C "$w/floor" push --quiet "$instance" HEAD:refs/heads/floor
573git -C "$instance" update-ref refs/heads/main "$(git -C "$w/floor" rev-parse HEAD)"
574ok valley pending --checks --all
575after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 pending$' \
576 '^ approving it lands the 2 below too$' \
577 '^ ├ open:mmmm v1 ' '^ │ stack-b$' '^ │ 1 commit · ' '^ │ requires 1 quick check, 0 slow$' \
578 '^ │ checks: 1 pending$' '^ └ nxvpwlkm v1 ' \
579 '^ stack-a$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 pending$'
580after '^nxvpwlkz v1 ' '^ g-ready$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 pending$'
581# Without --checks, no change lists its checks.
582ok valley changes --json --all
583holds '[.repos[].changes[].versions[] | select(has("checks"))] == []'
584
585# ----------------------------------------------------------------------
586# A branch name is used again once its change has landed: ff now names no
587# change, and reparent none either, once its transfer statement verifies.
588ok valley-go resolve ff
589line "^branch"$'\t'"ff$"
590ok valley-go resolve reparent
591line "^branch"$'\t'"reparent$"
592env -u VALLEY_KNOWN_SIGNERS valley-go resolve reparent > "$w/out" 2> "$w/err" || fail "resolve failed"
593line "^change"$'\t'"$G"$'\t'
594ok valley-go resolve stack-a
595line "^change"$'\t'"$A"$'\t'
596
597# H' has not landed, so its branch still names I, which is a draft, and
598# approving the branch is refused for I's state rather than taken as a
599# plain branch.
600ok valley-go resolve again
601line "^change"$'\t'"$I"$'\t'"zzzz"$'\t'"2"$'\t'"$hprime"$'\t'"again"$'\t'"draft"$'\t'"alice$"
602if valley approve again > "$w/out" 2> "$w/err"; then fail "approve took the branch of a draft"; fi
603grep -q "change zzzz v2 is draft, not ready" "$w/err" || fail "approve did not refuse the draft for its state"
604
605# J' is not J's landing replayed, so its branch names J, a draft, too.
606ok valley-go resolve ffrec
607line "^change"$'\t'"$J"$'\t'"lmlm"$'\t'"2"$'\t'"$jprime"$'\t'"ffrec"$'\t'"draft"$'\t'"alice$"
608if valley approve ffrec > "$w/out" 2> "$w/err"; then fail "approve took the branch of a replayed landing"; fi
609grep -q "change lmlm v2 is draft, not ready" "$w/err" || fail "approve did not refuse J' for its state"
610
611# A landed change names its branch no more once the branch moves on to new
612# work: ff is a branch, free for that work.
613git checkout --quiet -b ff-again ff
614echo more > more.txt
615git add more.txt
616git commit --quiet -m more
617git push --quiet origin ff-again:ff
618ok valley-go resolve ff
619line "^branch"$'\t'"ff$"
620
621# ----------------------------------------------------------------------
622# The listings name the knots that link each change: every knot in its
623# project's plan graph whose changes lists valley:<project>/<change ID>,
624# active knots first. The graph is written through the hook. nxvp is
625# linked from two knots, one of them closed, mmmm from one, pppp from
626# none, and the draft qqqq from the closed one. mmmm's change ID is linked
627# in guarded's name too, and guarded has no graph, so its mmmm has none.
628ok valley knot new outcome "The stack lands" --json
629goal="$(jq -r .knot.id "$w/out")"
630ok valley knot new bug "The stack reads the wrong base" --json
631bug="$(jq -r .knot.id "$w/out")"
632ok valley knot link "$goal" --change "valley:open/$A" --change "valley:open/$B"
633ok valley knot link "$bug" --change "valley:open/$A" --change "valley:open/$D" --change "valley:guarded/$B"
634ok valley knot set "$bug" status=closed
635goal_ref="[\"$goal\", \"outcome\", \"open\", \"The stack lands\"]"
636bug_ref="[\"$bug\", \"bug\", \"closed\", \"The stack reads the wrong base\"]"
637k() { printf '[.repos[0].%s[] | select(.change_id == "%s") | .knots[] | [.id, .type, .status, .title]]' "$1" "$2"; }
638ok valley changes --json
639holds "$(k changes "$A") == [$goal_ref, $bug_ref]" "$(k changes "$B") == [$goal_ref]" "$(k changes "$C") == []" \
640 "$(k changes "$D") == [$bug_ref]" '[.repos[0].changes[] | .knots | type] | unique == ["array"]'
641if grep -q 'plan graph' "$w/err"; then fail 'changes spoke of the plan graph without -v'; fi
642ok valley pending --json
643holds "$(k changes "$A") == [$goal_ref, $bug_ref]" "$(k changes "$C") == []" "$(k drafts "$D") == [$bug_ref]"
644# pending shows the first knot's ID after the title.
645ok valley pending
646after '^pppp v1 ' '^ stack-c$' '^ 1 commit · ' '^ approving it lands the 2 below too$' '^ ├ mmmm v1 ' \
647 "^ │ stack-b · $goal\$" '^ │ 1 commit · ' '^ └ nxvp v1 ' "^ stack-a · $goal\$"
648after '^qqqq v1 on paused ' "^ paused · $bug\$"
649ok valley status --json nxvp
650holds "[.knots[] | [.id, .type, .status, .title]] == [$goal_ref, $bug_ref]"
651ok valley status --json pppp
652holds '.knots == []'
653ok valley status --json loose
654holds '.change_id == null and .knots == []'
655ok valley changes --json --all
656holds "[.repos[] | select(.repo == \"open\") | .changes[] | select(.change_id == \"$B\") | .knots[].id] == [\"$goal\"]" \
657 "[.repos[] | select(.repo == \"guarded\") | .changes[] | select(.change_id == \"$B\") | .knots] == [[]]"
658if grep -q 'plan graph' "$w/err"; then fail 'changes --all spoke of a missing plan graph without -v'; fi
659ok valley changes --json --repo guarded -v
660grep -qF 'guarded has no plan graph at refs/the-valley/knots, so no change shows a knot' "$w/err" \
661 || fail 'changes -v did not say guarded has no plan graph'
662# A graph that does not read links no change, and the listings go on as
663# before. Only -v says why.
664graph_tip="$(git -C "$data/open.git" rev-parse refs/the-valley/knots)"
665junk="$(printf 'junk\n' | git -C "$data/open.git" hash-object -w --stdin)"
666junk_tree="$(printf '100644 blob %s\tjunk.txt\n' "$junk" | git -C "$data/open.git" mktree)"
667git -C "$data/open.git" update-ref refs/the-valley/knots "$(git -C "$data/open.git" commit-tree "$junk_tree" -m junk)"
668ok valley changes --json
669holds '(.repos[0].changes | length) == 13' '[.repos[0].changes[].knots] | unique == [[]]'
670if grep -q 'plan graph' "$w/err"; then fail 'changes spoke of a broken plan graph without -v'; fi
671ok valley pending
672line '^ stack-a$'
673no_line " · ($goal|$bug)\$"
674ok valley status --json nxvp
675holds '.knots == []'
676ok valley changes --json -v
677grep -qF "open's plan graph could not be read, so no change shows a knot" "$w/err" \
678 || fail 'changes -v did not say why it shows no knot'
679git -C "$data/open.git" update-ref refs/the-valley/knots "$graph_tip"
680
681# ----------------------------------------------------------------------
682# --checks: each ready change's required checks, passed, failed or
683# pending by the note the integrator would judge each by. The host
684# publishes its signers in the instance, as its publisher writes them,
685# and a check worker's key is among them. The floor requires prose-format
686# of each change here. The worker's note for one head's tree records a
687# pass, for another a failure, and a third head has none. A note is
688# stored on origin as attest run stores one: <check>/statement.note at
689# refs/the-valley/attestations/<tree digest>/<key hash>.
690cd "$w/open"
691ssh-keygen -q -t ed25519 -N '' -C worker -f "$w/worker.key"
692workerhash="$(attest key --key "$w/worker.key" --name worker | cut -d+ -f2)"
693{
694 cat "$w/known-signers"
695 attest key --key "$w/worker.key" --name worker
696} > "$w/host-signers"
697keysblob="$(git -C "$instance" hash-object -w "$w/host-signers")"
698nameblob="$(printf 'integrator\n' | git -C "$instance" hash-object -w --stdin)"
699git -C "$instance" update-ref refs/the-valley/signers \
700 "$(printf '100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers\n' "$nameblob" "$keysblob" | git -C "$instance" mktree)"
701
702# evidence <branch> <result>: the worker's note about prose-format for the
703# branch head's tree, stored on origin.
704evidence() {
705 local head digest blob tree
706 head="$(git rev-parse "$1")"
707 digest="$(tree_digest "$head")"
708 jq -n --arg tree "$digest" --arg commit "$head" --arg result "$2" '{
709 statementType: "the-valley/attestation/v1",
710 subject: {primary: "valley-tree-v1", digest: {"valley-tree-v1": $tree, "git-sha1": $commit}},
711 predicateType: "the-valley/check/pure/v1",
712 predicate: {check: {name: "prose-format", runner: "nix", attribute: "prose-format"}, result: $result,
713 inputs: {"valley-inputs-v1": ("cd" * 32)}, derivation: {sha256: ("ef" * 32)}, output: {sha256: ("01" * 32)}}
714 }' > "$w/statement.json"
715 attest render "$w/statement.json" | attest sign --key "$w/worker.key" --name worker > "$w/check.note"
716 blob="$(git -C "$data/open.git" hash-object -w "$w/check.note")"
717 tree="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$data/open.git" mktree)"
718 tree="$(printf '040000 tree %s\tprose-format\n' "$tree" | git -C "$data/open.git" mktree)"
719 git -C "$data/open.git" update-ref "refs/the-valley/attestations/$digest/$workerhash" "$tree"
720}
721K1="$(id klkl)" K2="$(id kmkm)" K3="$(id knkn)"
722git fetch --quiet origin
723branch chk-pass origin/main chk-pass
724ready "$K1" chk-pass
725evidence chk-pass passed
726branch chk-fail origin/main chk-fail
727ready "$K2" chk-fail
728evidence chk-fail failed
729branch chk-none origin/main chk-none
730ready "$K3" chk-none
731
732ok valley changes --json --checks
733checks() { printf '.repos[0].changes[] | select(.change_id == "%s") | .versions[.active-1].checks' "$1"; }
734holds "$(checks "$K1") | .state == \"passed\" and .tree == \"$(tree_digest chk-pass)\"" \
735 "$(checks "$K1") | .required == [{name: \"prose-format\", state: \"passed\", signer: \"worker+$workerhash\"}]" \
736 "$(checks "$K2") | .state == \"failed\" and (.required | length) == 1" \
737 "$(checks "$K2") | .required[0] | .name == \"prose-format\" and .state == \"failed\" and .signer == \"worker+$workerhash\"" \
738 "$(checks "$K2") | .required[0].why | contains(\"predicate.result\")" \
739 "$(checks "$K3") | .state == \"pending\" and .required == [{name: \"prose-format\", state: \"pending\"}]" \
740 '[.repos[0].changes[] | select(.state != "ready") | .versions[] | select(has("checks"))] == []' \
741 '[.repos[0].changes[] | .versions[] | select(.landing == "landed" or .landing == "landed-recorded") | select(has("checks"))] == []'
742ok valley pending --json --checks
743holds "$(checks "$K2") | .state == \"failed\""
744ok valley pending --checks
745after '^klkl v1 ' '^ chk-pass$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 passed$'
746after '^kmkm v1 ' '^ chk-fail$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 failed \(prose-format\)$'
747after '^knkn v1 ' '^ chk-none$' '^ 1 commit · ' '^ requires 1 quick check, 0 slow$' '^ checks: 1 pending$'
748
749# valley help names the verbs front ends look for.
750valley help > "$w/out"
751line '^ ready '
752line '^ changes '
753
754touch "$out"