gunk-dev/the-valley

valley-status.sh

source8c4ff6fd0e3bmain's published tip history
17.7 KiB
1# shellcheck shell=bash
2# The status command reads an asynchronous integration result. Exercise it
3# against real refs, including the gaps between updating main, recording an
4# outcome, and consuming a request. No controller or timing assumption is
5# needed to make those states observable. Every state is read twice: as
6# the sentences an operator reads, and through --json, the valley-status/v1
7# object front ends parse.
8# The derivation supplies its output path.
9# shellcheck disable=SC2154
10set -euo pipefail
11
12w="$TMPDIR/valley-status"
13mkdir -p "$w"
14export GIT_CONFIG_GLOBAL="$w/gitconfig"
15export GIT_CONFIG_NOSYSTEM=1
16git config --global user.name valley-status-check
17git config --global user.email valley-status-check@localhost
18git config --global init.defaultBranch main
19git config --global advice.detachedHead false
20
21origin="$w/project.git"
22repo="$w/project"
23git init --quiet --bare "$origin"
24git init --quiet "$repo"
25git -C "$repo" remote add origin "$origin"
26printf 'base\n' > "$repo/readme.txt"
27git -C "$repo" add readme.txt
28git -C "$repo" commit --quiet -m base
29git -C "$repo" push --quiet origin main
30base="$(git -C "$repo" rev-parse HEAD)"
31git -C "$repo" checkout --quiet -b topic
32printf 'topic\n' > "$repo/readme.txt"
33git -C "$repo" commit --quiet -am topic
34head="$(git -C "$repo" rev-parse HEAD)"
35git -C "$repo" push --quiet origin topic
36git -C "$repo" checkout --quiet main
37git -C "$repo" fetch --quiet origin
38
39request=refs/the-valley/integration-requests/main/topic
40memo=refs/the-valley/integration-outcomes/main/topic
41evidence_ref="refs/the-valley/attestations/$(printf '%064d' 0)/test"
42
43fail() {
44 printf 'valley-status: %s\n' "$*" >&2
45 if [ -f "$w/status.out" ]; then cat "$w/status.out" >&2; fi
46 if [ -f "$w/status.json" ]; then cat "$w/status.json" >&2; fi
47 exit 1
48}
49
50contains() {
51 grep -qiF -- "$1" "$w/status.out" || fail "$2"
52}
53
54lacks() {
55 if grep -qiF -- "$1" "$w/status.out"; then fail "$2"; fi
56}
57
58matches() {
59 grep -qiE -- "$1" "$w/status.out" || fail "$2"
60}
61
62evidence_state() {
63 git -C "$origin" for-each-ref --format='%(refname) %(objectname)' \
64 refs/the-valley/attestations | sha256sum | cut -c1-16
65}
66
67record_outcome() {
68 local blob
69 blob="$(printf '%s %s %s %s\n' "$1" "$2" "$(evidence_state)" "$3" |
70 git -C "$origin" hash-object -w --stdin)"
71 git -C "$origin" update-ref "$memo" "$blob"
72}
73
74reset_remote() {
75 git -C "$origin" update-ref refs/heads/main "$base"
76 git -C "$origin" update-ref refs/heads/topic "$head"
77 git -C "$origin" update-ref -d "$request"
78 git -C "$origin" update-ref -d "$memo"
79 git -C "$origin" update-ref -d "$evidence_ref"
80}
81
82# A read must preserve every local branch and both kinds of uncommitted
83# change. FETCH_HEAD is included because scripts and people may be using
84# the preceding fetch result independently of status.
85local_state() {
86 git -C "$repo" symbolic-ref -q HEAD || true
87 git -C "$repo" rev-parse HEAD
88 git -C "$repo" for-each-ref --format='%(refname) %(objectname)' refs/heads
89 git -C "$repo" status --porcelain=v1
90 git -C "$repo" diff --binary
91 git -C "$repo" diff --cached --binary
92 if [ -f "$repo/.git/FETCH_HEAD" ]; then
93 sha256sum "$repo/.git/FETCH_HEAD"
94 fi
95}
96
97status() {
98 local_state > "$w/before"
99 (cd "$repo" && valley status "${1:-topic}") > "$w/status.out" 2>&1 \
100 || fail "status could not inspect the remote"
101 local_state > "$w/after"
102 diff -u "$w/before" "$w/after" || fail "status changed the local checkout or FETCH_HEAD"
103}
104
105# The same read through --json: one object on one line, on stdout alone,
106# and the local checkout left as it was.
107status_json() {
108 local_state > "$w/before"
109 (cd "$repo" && valley status --json "${1:-topic}") > "$w/status.json" 2> "$w/status.err" \
110 || fail "status --json could not inspect the remote"
111 local_state > "$w/after"
112 diff -u "$w/before" "$w/after" || fail "status --json changed the local checkout or FETCH_HEAD"
113 [ "$(wc -l < "$w/status.json")" = 1 ] || fail "status --json printed other than one line"
114 jq -e 'type == "object"' "$w/status.json" > /dev/null \
115 || fail "status --json printed something other than one JSON object"
116}
117
118# json <state> [<jq condition>...]: topic's state through --json, the
119# fields every object carries, and each condition given. The ids in a
120# condition are hex, so they are written into it directly.
121json() {
122 local want="$1" tip got cond
123 shift
124 status_json
125 tip="$(git -C "$origin" rev-parse refs/heads/main)"
126 got="$(jq -r .state "$w/status.json")"
127 [ "$got" = "$want" ] || fail "status --json reported $got where $want was expected"
128 for cond in '.schema == "valley-status/v1"' '.change == "topic"' \
129 '.request_name == "topic"' '.target == "main"' ".target_tip == \"$tip\"" "$@"; do
130 jq -e "$cond" "$w/status.json" > /dev/null || fail "status --json: $cond does not hold"
131 done
132}
133
134# A read that cannot give a status prints no object at all: a front end
135# must never parse half of one, or an empty one, as an answer.
136json_refused() {
137 if (cd "$repo" && valley status --json "${1:-topic}") > "$w/status.json" 2> "$w/status.err"; then
138 fail "status --json succeeded where it had to refuse"
139 fi
140 [ ! -s "$w/status.json" ] || fail "a refused status --json printed to stdout"
141 [ -s "$w/status.err" ] || fail "a refused status --json did not say why"
142}
143
144# No request is different from a request waiting to be judged.
145reset_remote
146status
147contains 'not approved' 'an unapproved branch was not identified'
148lacks 'integrated into origin/main' 'an unapproved branch was reported integrated'
149json not-requested ".head == \"$head\"" '.request == null' '.outcome == null'
150git -C "$origin" update-ref "$request" "$head"
151status
152contains 'awaiting' 'a new request was not reported as waiting'
153contains 'decision' 'the pending message did not say what is awaited'
154json awaiting-decision ".head == \"$head\"" \
155 ".request == {\"ref\": \"$request\", \"commit\": \"$head\"}" '.outcome == null'
156
157# A current refusal is useful, but a verdict for an earlier input is not
158# the current answer. Changing the target or adding evidence schedules a
159# new controller decision without requiring another request.
160record_outcome "$head" "$base" stale
161status
162contains 'needs attention' 'current staleness did not call for attention'
163contains 'stale' 'current staleness lost the recorded outcome'
164json stale ".outcome == {\"decision\": \"stale\", \"head\": \"$head\", \"tip\": \"$base\", \"failed_at\": null}"
165record_outcome "$head" "$base" reject
166status
167contains 'rejected' 'a current rejection was not reported'
168json rejected '.outcome.decision == "reject"' ".request.commit == \"$head\""
169# The integrator's refusals: a head that is a version of a change that is
170# not ready, and commits that hold a merge. The request stands for both.
171record_outcome "$head" "$base" refused-not-ready
172status
173contains 'did not say ready' 'a refusal for a record that is not ready was not reported'
174json refused-not-ready '.outcome.decision == "refused-not-ready"' ".request.commit == \"$head\"" \
175 '.change_id == null and .handle == null and .landing == null'
176record_outcome "$head" "$base" refused-merge
177status
178contains 'include a merge' 'a refusal for a merge was not reported'
179json refused-merge '.outcome.decision == "refused-merge"'
180# A request the integrator voided for a withdrawal is gone, and its outcome
181# says why. A request filed again at that head is a fresh approval.
182git -C "$origin" update-ref -d "$request"
183record_outcome "$head" "$base" voided-withdrawn
184status
185contains 'voided its request because the change was withdrawn' 'a voided request was not reported'
186json voided-withdrawn '.outcome.decision == "voided-withdrawn"' '.request == null'
187git -C "$origin" update-ref "$request" "$head"
188json awaiting-decision '.outcome.decision == "voided-withdrawn"'
189# A stored record that does not read, and that may name the request's
190# head, is warned of: the integrator cannot tell whether it holds the head
191# back. Only corrupt storage holds one, since the hook refuses it.
192broken="$(printf '{}\n' | git -C "$origin" hash-object -w --stdin)"
193broken="$(printf '100644 blob %s\trecord.json\n' "$broken" | git -C "$origin" mktree)"
194broken="$(git -C "$origin" commit-tree "$broken" -p "$head" -m 'a broken record')"
195git -C "$origin" update-ref refs/the-valley/changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk "$broken"
196status
197contains 'changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk does not read' 'a record that does not read was not warned of'
198contains 'awaiting' 'a record that does not read stopped the status read'
199git -C "$origin" update-ref -d refs/the-valley/changes/main/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
200record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z'
201status
202contains 'failed' 'a controller failure was not reported'
203matches 'may.*land|might.*land|cannot.*(rule out|confirm|determine)|does not.*(prove|mean|establish).*land|not.*confirm.*land' \
204 'a failure was presented as proof that nothing landed'
205json failed '.outcome.decision == "failed"' '.outcome.failed_at == "2026-09-21T12:00:00Z"'
206
207record_outcome "$head" "$base" stale
208other_tree="$(git -C "$origin" rev-parse "$base^{tree}")"
209advanced="$(printf 'another landing\n' | git -C "$origin" commit-tree "$other_tree" -p "$base")"
210git -C "$origin" update-ref refs/heads/main "$advanced"
211status
212contains 'awaiting' 'a moved target retained the previous refusal'
213contains 'new decision' 'a moved target did not explain re-evaluation'
214json awaiting-new-decision ".target_tip == \"$advanced\"" ".outcome.tip == \"$base\"" \
215 '.outcome.decision == "stale"'
216
217reset_remote
218git -C "$origin" update-ref "$request" "$head"
219record_outcome "$head" "$base" stale
220evidence="$(printf 'new evidence\n' | git -C "$origin" hash-object -w --stdin)"
221git -C "$origin" update-ref "$evidence_ref" "$evidence"
222status
223contains 'awaiting' 'new evidence retained the previous refusal'
224contains 'new decision' 'new evidence did not explain re-evaluation'
225json awaiting-new-decision ".target_tip == \"$base\"" '.outcome.decision == "stale"'
226
227# A reused change name must not carry an earlier commit's rejection or
228# landing forward to its replacement.
229reset_remote
230git -C "$origin" update-ref "$request" "$head"
231record_outcome "$base" "$base" reject
232status
233contains 'awaiting' 'an earlier head verdict was applied to the current request'
234lacks 'needs attention' 'an earlier rejection was treated as current'
235json awaiting-decision ".head == \"$head\"" ".outcome.head == \"$base\"" \
236 '.outcome.decision == "reject"'
237git -C "$origin" update-ref -d "$request"
238record_outcome "$base" "$base" land
239status
240contains 'not approved' 'an old landing was applied to a newer branch head'
241lacks 'integrated into origin/main' 'the newer branch inherited an old landing'
242json not-requested ".head == \"$head\"" '.request == null' \
243 ".outcome.head == \"$base\"" '.outcome.decision == "land"'
244
245# Main is authoritative for direct landings. A post-commit failure may
246# leave both a failed memo and an unconsumed request behind.
247reset_remote
248git -C "$origin" update-ref refs/heads/main "$head"
249git -C "$origin" update-ref "$request" "$head"
250record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z'
251status
252contains 'integrated into origin/main' 'a failed bookkeeping step hid an actual landing'
253contains 'git pull --ff-only origin main' 'a behind local main had no update instruction'
254json integrated ".head == \"$head\"" ".request.commit == \"$head\"" \
255 '.outcome.decision == "failed"'
256git -C "$origin" update-ref -d "$request"
257record_outcome "$head" "$base" land
258status
259contains 'integrated into origin/main' 'a consumed request hid a direct landing'
260json integrated ".head == \"$head\"" '.request == null' '.outcome.decision == "land"'
261
262# Reparenting changes the landed commit id. The matching durable outcome
263# still identifies the change that was approved, including after topic
264# cleanup.
265reset_remote
266topic_tree="$(git -C "$origin" rev-parse "$head^{tree}")"
267landed="$(printf 'reparented landing\n' | git -C "$origin" commit-tree "$topic_tree" -p "$advanced")"
268git -C "$origin" update-ref refs/heads/main "$landed"
269record_outcome "$head" "$advanced" land
270status
271contains 'integrated into origin/main' 'a reparented landing was missed'
272json integrated-recorded ".head == \"$head\"" ".target_tip == \"$landed\"" \
273 ".outcome.head == \"$head\""
274git -C "$origin" update-ref -d refs/heads/topic
275status
276contains 'integrated into origin/main' 'deleting a landed topic hid its outcome'
277json integrated-recorded ".head == \"$head\"" '.request == null'
278
279# A disappeared request without a landing record cannot establish success.
280reset_remote
281git -C "$origin" update-ref -d refs/heads/topic
282if (cd "$repo" && valley status topic) > "$w/status.out" 2>&1; then
283 fail 'a nonexistent branch and request were accepted'
284fi
285matches 'no.*recorded.*request|no.*request.*record|no.*branch.*request' \
286 'a missing branch and request had no actionable explanation'
287lacks 'integrated into origin/main' 'absence was treated as a landing'
288json_refused
289
290# A corrupted memo must not turn a valid pending request into success or
291# abort the status read without explaining which record is unreadable.
292reset_remote
293git -C "$origin" update-ref "$request" "$head"
294bad="$(printf 'not an outcome\n' | git -C "$origin" hash-object -w --stdin)"
295git -C "$origin" update-ref "$memo" "$bad"
296local_state > "$w/before"
297(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
298local_state > "$w/after"
299diff -u "$w/before" "$w/after" || fail 'a malformed memo changed local state'
300matches 'malformed|unreadable|invalid|unrecognized|not recognized' \
301 'a malformed memo was silently accepted'
302lacks 'integrated into origin/main' 'a malformed memo became a successful landing'
303# An unreadable record is present and says nothing: no head, tip or time
304# from it is reported as though it had been read.
305json outcome-unreadable ".head == \"$head\"" \
306 '.outcome == {"decision": "unknown", "head": null, "tip": null, "failed_at": null}'
307
308# Status must remain useful with uncommitted work, and its suggestion to
309# update main must disappear once the local main already has the landing.
310reset_remote
311git -C "$origin" update-ref refs/heads/main "$head"
312record_outcome "$head" "$base" land
313printf 'staged\n' > "$repo/staged.txt"
314git -C "$repo" add staged.txt
315printf 'unstaged\n' >> "$repo/readme.txt"
316status
317contains 'integrated into origin/main' 'dirty local work obscured the remote result'
318json integrated ".head == \"$head\""
319git -C "$repo" reset --quiet --hard "$head"
320status
321lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull'
322
323# On another branch, advice must describe local main rather than assume
324# main is at the commit currently checked out.
325git -C "$repo" checkout --quiet -b feature "$base"
326status
327contains 'git switch main' 'an up-to-date local main was not offered for viewing'
328lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull from a feature checkout'
329git -C "$repo" branch --force main "$advanced" >/dev/null
330status
331contains 'diverged' 'a divergent local main was overlooked from a feature checkout'
332lacks 'git pull --ff-only origin main' 'a divergent local main was told to fast-forward'
333git -C "$repo" branch --force main "$head" >/dev/null
334
335# A newer topic commit must not be mistaken for the older pending request.
336reset_remote
337git -C "$origin" update-ref "$request" "$base"
338status
339contains 'latest commit not approved' 'a newer topic inherited an older request'
340contains 'older request' 'an outstanding older request was hidden'
341json outdated-request ".head == \"$head\"" ".request.commit == \"$base\""
342
343# A change name is carried exactly. A branch name may hold quotes, the
344# characters a shell reads specially, and any space but ASCII's — here a
345# no-break space: JSON escapes them, the request name maps only the slash,
346# and the command the sentences suggest reads back, in a shell, as the
347# same name.
348odd='odd/"quoted"&<name>;$(x)'$'\302\240''nbsp'
349oddrequest='odd-"quoted"&<name>;$(x)'$'\302\240''nbsp'
350git -C "$origin" update-ref "refs/heads/$odd" "$head"
351git -C "$origin" update-ref "refs/the-valley/integration-requests/main/$oddrequest" "$head"
352status "$odd"
353suggested="$(sed -n 's/^Check again: valley status //p' "$w/status.out")"
354eval "set -- $suggested"
355[ "$#" = 1 ] && [ "$1" = "$odd" ] || fail "the suggested command names something other than $odd"
356status_json "$odd"
357jq -e --arg change "$odd" --arg name "$oddrequest" --arg head "$head" '
358 .change == $change and .request_name == $name and .state == "awaiting-decision"
359 and .request == {ref: ("refs/the-valley/integration-requests/main/" + $name), commit: $head}
360' "$w/status.json" > /dev/null || fail "status --json did not carry the name $odd exactly"
361git -C "$origin" update-ref -d "refs/heads/$odd"
362git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$oddrequest"
363
364# A target with no main is no answer either.
365git -C "$origin" update-ref -d refs/heads/main
366json_refused
367grep -q 'no main branch' "$w/status.err" || fail 'a missing main had no useful diagnostic'
368git -C "$origin" update-ref refs/heads/main "$base"
369
370# Connectivity failures are not an empty pending queue. These cases do not
371# require one particular exit convention, only a diagnostic and no claim
372# about a successfully inspected remote.
373git -C "$repo" remote set-url origin "$w/missing.git"
374(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
375matches 'origin|remote|fetch|repository' 'an unavailable origin had no useful diagnostic'
376lacks 'integrated into origin/main' 'a failed fetch reported cached success'
377lacks 'not approved' 'a failed fetch was mistaken for an absent request'
378json_refused
379grep -q 'could not read origin' "$w/status.err" || fail 'an unreadable origin was not named as such'
380git -C "$repo" remote remove origin
381(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
382matches 'origin|remote' 'a missing origin had no useful diagnostic'
383lacks 'integrated into origin/main' 'a missing origin reported cached success'
384json_refused
385
386echo 'valley-status: remote outcomes and local state checks passed'
387touch "$out"