gunk-dev/the-valley

valley-status.sh

source25d41084d6famain's published tip history
10.5 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.
6# The derivation supplies its output path.
7# shellcheck disable=SC2154
8set -euo pipefail
9
10w="$TMPDIR/valley-status"
11mkdir -p "$w"
12export GIT_CONFIG_GLOBAL="$w/gitconfig"
13export GIT_CONFIG_NOSYSTEM=1
14git config --global user.name valley-status-check
15git config --global user.email valley-status-check@localhost
16git config --global init.defaultBranch main
17git config --global advice.detachedHead false
18
19origin="$w/project.git"
20repo="$w/project"
21git init --quiet --bare "$origin"
22git init --quiet "$repo"
23git -C "$repo" remote add origin "$origin"
24printf 'base\n' > "$repo/readme.txt"
25git -C "$repo" add readme.txt
26git -C "$repo" commit --quiet -m base
27git -C "$repo" push --quiet origin main
28base="$(git -C "$repo" rev-parse HEAD)"
29git -C "$repo" checkout --quiet -b topic
30printf 'topic\n' > "$repo/readme.txt"
31git -C "$repo" commit --quiet -am topic
32head="$(git -C "$repo" rev-parse HEAD)"
33git -C "$repo" push --quiet origin topic
34git -C "$repo" checkout --quiet main
35git -C "$repo" fetch --quiet origin
36
37request=refs/the-valley/integration-requests/main/topic
38memo=refs/the-valley/integration-outcomes/main/topic
39evidence_ref="refs/the-valley/attestations/$(printf '%064d' 0)/test"
40
41fail() {
42 printf 'valley-status: %s\n' "$*" >&2
43 if [ -f "$w/status.out" ]; then cat "$w/status.out" >&2; fi
44 exit 1
45}
46
47contains() {
48 grep -qiF -- "$1" "$w/status.out" || fail "$2"
49}
50
51lacks() {
52 if grep -qiF -- "$1" "$w/status.out"; then fail "$2"; fi
53}
54
55matches() {
56 grep -qiE -- "$1" "$w/status.out" || fail "$2"
57}
58
59evidence_state() {
60 git -C "$origin" for-each-ref --format='%(refname) %(objectname)' \
61 refs/the-valley/attestations | sha256sum | cut -c1-16
62}
63
64record_outcome() {
65 local blob
66 blob="$(printf '%s %s %s %s\n' "$1" "$2" "$(evidence_state)" "$3" |
67 git -C "$origin" hash-object -w --stdin)"
68 git -C "$origin" update-ref "$memo" "$blob"
69}
70
71reset_remote() {
72 git -C "$origin" update-ref refs/heads/main "$base"
73 git -C "$origin" update-ref refs/heads/topic "$head"
74 git -C "$origin" update-ref -d "$request"
75 git -C "$origin" update-ref -d "$memo"
76 git -C "$origin" update-ref -d "$evidence_ref"
77}
78
79# A read must preserve every local branch and both kinds of uncommitted
80# change. FETCH_HEAD is included because scripts and people may be using
81# the preceding fetch result independently of status.
82local_state() {
83 git -C "$repo" symbolic-ref -q HEAD || true
84 git -C "$repo" rev-parse HEAD
85 git -C "$repo" for-each-ref --format='%(refname) %(objectname)' refs/heads
86 git -C "$repo" status --porcelain=v1
87 git -C "$repo" diff --binary
88 git -C "$repo" diff --cached --binary
89 if [ -f "$repo/.git/FETCH_HEAD" ]; then
90 sha256sum "$repo/.git/FETCH_HEAD"
91 fi
92}
93
94status() {
95 local_state > "$w/before"
96 (cd "$repo" && valley status "${1:-topic}") > "$w/status.out" 2>&1 \
97 || fail "status could not inspect the remote"
98 local_state > "$w/after"
99 diff -u "$w/before" "$w/after" || fail "status changed the local checkout or FETCH_HEAD"
100}
101
102# No request is different from a request waiting to be judged.
103reset_remote
104status
105contains 'not submitted' 'an unsubmitted branch was not identified'
106lacks 'integrated into origin/main' 'an unsubmitted branch was reported integrated'
107git -C "$origin" update-ref "$request" "$head"
108status
109contains 'awaiting' 'a new request was not reported as waiting'
110contains 'decision' 'the pending message did not say what is awaited'
111
112# A current refusal is useful, but a verdict for an earlier input is not
113# the current answer. Changing the target or adding evidence schedules a
114# new controller decision without requiring another request.
115record_outcome "$head" "$base" stale
116status
117contains 'needs attention' 'current staleness did not call for attention'
118contains 'stale' 'current staleness lost the recorded outcome'
119record_outcome "$head" "$base" reject
120status
121contains 'rejected' 'a current rejection was not reported'
122record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z'
123status
124contains 'failed' 'a controller failure was not reported'
125matches 'may.*land|might.*land|cannot.*(rule out|confirm|determine)|does not.*(prove|mean|establish).*land|not.*confirm.*land' \
126 'a failure was presented as proof that nothing landed'
127
128record_outcome "$head" "$base" stale
129other_tree="$(git -C "$origin" rev-parse "$base^{tree}")"
130advanced="$(printf 'another landing\n' | git -C "$origin" commit-tree "$other_tree" -p "$base")"
131git -C "$origin" update-ref refs/heads/main "$advanced"
132status
133contains 'awaiting' 'a moved target retained the previous refusal'
134contains 'new decision' 'a moved target did not explain re-evaluation'
135
136reset_remote
137git -C "$origin" update-ref "$request" "$head"
138record_outcome "$head" "$base" stale
139evidence="$(printf 'new evidence\n' | git -C "$origin" hash-object -w --stdin)"
140git -C "$origin" update-ref "$evidence_ref" "$evidence"
141status
142contains 'awaiting' 'new evidence retained the previous refusal'
143contains 'new decision' 'new evidence did not explain re-evaluation'
144
145# A reused change name must not carry an earlier commit's rejection or
146# landing forward to its replacement.
147reset_remote
148git -C "$origin" update-ref "$request" "$head"
149record_outcome "$base" "$base" reject
150status
151contains 'awaiting' 'an earlier head verdict was applied to the current request'
152lacks 'needs attention' 'an earlier rejection was treated as current'
153git -C "$origin" update-ref -d "$request"
154record_outcome "$base" "$base" land
155status
156contains 'not submitted' 'an old landing was applied to a newer branch head'
157lacks 'integrated into origin/main' 'the newer branch inherited an old landing'
158
159# Main is authoritative for direct landings. A post-commit failure may
160# leave both a failed memo and an unconsumed request behind.
161reset_remote
162git -C "$origin" update-ref refs/heads/main "$head"
163git -C "$origin" update-ref "$request" "$head"
164record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z'
165status
166contains 'integrated into origin/main' 'a failed bookkeeping step hid an actual landing'
167contains 'git pull --ff-only origin main' 'a behind local main had no update instruction'
168git -C "$origin" update-ref -d "$request"
169record_outcome "$head" "$base" land
170status
171contains 'integrated into origin/main' 'a consumed request hid a direct landing'
172
173# Reparenting changes the landed commit id. The matching durable outcome
174# still identifies the submitted change, including after topic cleanup.
175reset_remote
176topic_tree="$(git -C "$origin" rev-parse "$head^{tree}")"
177landed="$(printf 'reparented landing\n' | git -C "$origin" commit-tree "$topic_tree" -p "$advanced")"
178git -C "$origin" update-ref refs/heads/main "$landed"
179record_outcome "$head" "$advanced" land
180status
181contains 'integrated into origin/main' 'a reparented landing was missed'
182git -C "$origin" update-ref -d refs/heads/topic
183status
184contains 'integrated into origin/main' 'deleting a landed topic hid its outcome'
185
186# A disappeared request without a landing record cannot establish success.
187reset_remote
188git -C "$origin" update-ref -d refs/heads/topic
189if (cd "$repo" && valley status topic) > "$w/status.out" 2>&1; then
190 fail 'a nonexistent branch and request were accepted'
191fi
192matches 'no.*recorded.*request|no.*request.*record|no.*branch.*request' \
193 'a missing branch and request had no actionable explanation'
194lacks 'integrated into origin/main' 'absence was treated as a landing'
195
196# A corrupted memo must not turn a valid pending request into success or
197# abort the status read without explaining which record is unreadable.
198reset_remote
199git -C "$origin" update-ref "$request" "$head"
200bad="$(printf 'not an outcome\n' | git -C "$origin" hash-object -w --stdin)"
201git -C "$origin" update-ref "$memo" "$bad"
202local_state > "$w/before"
203(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
204local_state > "$w/after"
205diff -u "$w/before" "$w/after" || fail 'a malformed memo changed local state'
206matches 'malformed|unreadable|invalid|unrecognized|not recognized' \
207 'a malformed memo was silently accepted'
208lacks 'integrated into origin/main' 'a malformed memo became a successful landing'
209
210# Status must remain useful with uncommitted work, and its suggestion to
211# update main must disappear once the local main already has the landing.
212reset_remote
213git -C "$origin" update-ref refs/heads/main "$head"
214record_outcome "$head" "$base" land
215printf 'staged\n' > "$repo/staged.txt"
216git -C "$repo" add staged.txt
217printf 'unstaged\n' >> "$repo/readme.txt"
218status
219contains 'integrated into origin/main' 'dirty local work obscured the remote result'
220git -C "$repo" reset --quiet --hard "$head"
221status
222lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull'
223
224# On another branch, advice must describe local main rather than assume
225# main is at the commit currently checked out.
226git -C "$repo" checkout --quiet -b feature "$base"
227status
228contains 'git switch main' 'an up-to-date local main was not offered for viewing'
229lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull from a feature checkout'
230git -C "$repo" branch --force main "$advanced" >/dev/null
231status
232contains 'diverged' 'a divergent local main was overlooked from a feature checkout'
233lacks 'git pull --ff-only origin main' 'a divergent local main was told to fast-forward'
234git -C "$repo" branch --force main "$head" >/dev/null
235
236# A newer topic commit must not be mistaken for the older pending request.
237reset_remote
238git -C "$origin" update-ref "$request" "$base"
239status
240contains 'latest commit not submitted' 'a newer topic inherited an older request'
241contains 'older request' 'an outstanding older request was hidden'
242
243# Connectivity failures are not an empty pending queue. These cases do not
244# require one particular exit convention, only a diagnostic and no claim
245# about a successfully inspected remote.
246git -C "$repo" remote set-url origin "$w/missing.git"
247(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
248matches 'origin|remote|fetch|repository' 'an unavailable origin had no useful diagnostic'
249lacks 'integrated into origin/main' 'a failed fetch reported cached success'
250lacks 'not submitted' 'a failed fetch was mistaken for an absent request'
251git -C "$repo" remote remove origin
252(cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true
253matches 'origin|remote' 'a missing origin had no useful diagnostic'
254lacks 'integrated into origin/main' 'a missing origin reported cached success'
255
256echo 'valley-status: remote outcomes and local state checks passed'
257touch "$out"