| 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 |
| 8 | set -euo pipefail |
| 9 | |
| 10 | w="$TMPDIR/valley-status" |
| 11 | mkdir -p "$w" |
| 12 | export GIT_CONFIG_GLOBAL="$w/gitconfig" |
| 13 | export GIT_CONFIG_NOSYSTEM=1 |
| 14 | git config --global user.name valley-status-check |
| 15 | git config --global user.email valley-status-check@localhost |
| 16 | git config --global init.defaultBranch main |
| 17 | git config --global advice.detachedHead false |
| 18 | |
| 19 | origin="$w/project.git" |
| 20 | repo="$w/project" |
| 21 | git init --quiet --bare "$origin" |
| 22 | git init --quiet "$repo" |
| 23 | git -C "$repo" remote add origin "$origin" |
| 24 | printf 'base\n' > "$repo/readme.txt" |
| 25 | git -C "$repo" add readme.txt |
| 26 | git -C "$repo" commit --quiet -m base |
| 27 | git -C "$repo" push --quiet origin main |
| 28 | base="$(git -C "$repo" rev-parse HEAD)" |
| 29 | git -C "$repo" checkout --quiet -b topic |
| 30 | printf 'topic\n' > "$repo/readme.txt" |
| 31 | git -C "$repo" commit --quiet -am topic |
| 32 | head="$(git -C "$repo" rev-parse HEAD)" |
| 33 | git -C "$repo" push --quiet origin topic |
| 34 | git -C "$repo" checkout --quiet main |
| 35 | git -C "$repo" fetch --quiet origin |
| 36 | |
| 37 | request=refs/the-valley/integration-requests/main/topic |
| 38 | memo=refs/the-valley/integration-outcomes/main/topic |
| 39 | evidence_ref="refs/the-valley/attestations/$(printf '%064d' 0)/test" |
| 40 | |
| 41 | fail() { |
| 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 | |
| 47 | contains() { |
| 48 | grep -qiF -- "$1" "$w/status.out" || fail "$2" |
| 49 | } |
| 50 | |
| 51 | lacks() { |
| 52 | if grep -qiF -- "$1" "$w/status.out"; then fail "$2"; fi |
| 53 | } |
| 54 | |
| 55 | matches() { |
| 56 | grep -qiE -- "$1" "$w/status.out" || fail "$2" |
| 57 | } |
| 58 | |
| 59 | evidence_state() { |
| 60 | git -C "$origin" for-each-ref --format='%(refname) %(objectname)' \ |
| 61 | refs/the-valley/attestations | sha256sum | cut -c1-16 |
| 62 | } |
| 63 | |
| 64 | record_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 | |
| 71 | reset_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. |
| 82 | local_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 | |
| 94 | status() { |
| 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. |
| 103 | reset_remote |
| 104 | status |
| 105 | contains 'not submitted' 'an unsubmitted branch was not identified' |
| 106 | lacks 'integrated into origin/main' 'an unsubmitted branch was reported integrated' |
| 107 | git -C "$origin" update-ref "$request" "$head" |
| 108 | status |
| 109 | contains 'awaiting' 'a new request was not reported as waiting' |
| 110 | contains '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. |
| 115 | record_outcome "$head" "$base" stale |
| 116 | status |
| 117 | contains 'needs attention' 'current staleness did not call for attention' |
| 118 | contains 'stale' 'current staleness lost the recorded outcome' |
| 119 | record_outcome "$head" "$base" reject |
| 120 | status |
| 121 | contains 'rejected' 'a current rejection was not reported' |
| 122 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 123 | status |
| 124 | contains 'failed' 'a controller failure was not reported' |
| 125 | matches '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 | |
| 128 | record_outcome "$head" "$base" stale |
| 129 | other_tree="$(git -C "$origin" rev-parse "$base^{tree}")" |
| 130 | advanced="$(printf 'another landing\n' | git -C "$origin" commit-tree "$other_tree" -p "$base")" |
| 131 | git -C "$origin" update-ref refs/heads/main "$advanced" |
| 132 | status |
| 133 | contains 'awaiting' 'a moved target retained the previous refusal' |
| 134 | contains 'new decision' 'a moved target did not explain re-evaluation' |
| 135 | |
| 136 | reset_remote |
| 137 | git -C "$origin" update-ref "$request" "$head" |
| 138 | record_outcome "$head" "$base" stale |
| 139 | evidence="$(printf 'new evidence\n' | git -C "$origin" hash-object -w --stdin)" |
| 140 | git -C "$origin" update-ref "$evidence_ref" "$evidence" |
| 141 | status |
| 142 | contains 'awaiting' 'new evidence retained the previous refusal' |
| 143 | contains '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. |
| 147 | reset_remote |
| 148 | git -C "$origin" update-ref "$request" "$head" |
| 149 | record_outcome "$base" "$base" reject |
| 150 | status |
| 151 | contains 'awaiting' 'an earlier head verdict was applied to the current request' |
| 152 | lacks 'needs attention' 'an earlier rejection was treated as current' |
| 153 | git -C "$origin" update-ref -d "$request" |
| 154 | record_outcome "$base" "$base" land |
| 155 | status |
| 156 | contains 'not submitted' 'an old landing was applied to a newer branch head' |
| 157 | lacks '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. |
| 161 | reset_remote |
| 162 | git -C "$origin" update-ref refs/heads/main "$head" |
| 163 | git -C "$origin" update-ref "$request" "$head" |
| 164 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 165 | status |
| 166 | contains 'integrated into origin/main' 'a failed bookkeeping step hid an actual landing' |
| 167 | contains 'git pull --ff-only origin main' 'a behind local main had no update instruction' |
| 168 | git -C "$origin" update-ref -d "$request" |
| 169 | record_outcome "$head" "$base" land |
| 170 | status |
| 171 | contains '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. |
| 175 | reset_remote |
| 176 | topic_tree="$(git -C "$origin" rev-parse "$head^{tree}")" |
| 177 | landed="$(printf 'reparented landing\n' | git -C "$origin" commit-tree "$topic_tree" -p "$advanced")" |
| 178 | git -C "$origin" update-ref refs/heads/main "$landed" |
| 179 | record_outcome "$head" "$advanced" land |
| 180 | status |
| 181 | contains 'integrated into origin/main' 'a reparented landing was missed' |
| 182 | git -C "$origin" update-ref -d refs/heads/topic |
| 183 | status |
| 184 | contains 'integrated into origin/main' 'deleting a landed topic hid its outcome' |
| 185 | |
| 186 | # A disappeared request without a landing record cannot establish success. |
| 187 | reset_remote |
| 188 | git -C "$origin" update-ref -d refs/heads/topic |
| 189 | if (cd "$repo" && valley status topic) > "$w/status.out" 2>&1; then |
| 190 | fail 'a nonexistent branch and request were accepted' |
| 191 | fi |
| 192 | matches 'no.*recorded.*request|no.*request.*record|no.*branch.*request' \ |
| 193 | 'a missing branch and request had no actionable explanation' |
| 194 | lacks '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. |
| 198 | reset_remote |
| 199 | git -C "$origin" update-ref "$request" "$head" |
| 200 | bad="$(printf 'not an outcome\n' | git -C "$origin" hash-object -w --stdin)" |
| 201 | git -C "$origin" update-ref "$memo" "$bad" |
| 202 | local_state > "$w/before" |
| 203 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 204 | local_state > "$w/after" |
| 205 | diff -u "$w/before" "$w/after" || fail 'a malformed memo changed local state' |
| 206 | matches 'malformed|unreadable|invalid|unrecognized|not recognized' \ |
| 207 | 'a malformed memo was silently accepted' |
| 208 | lacks '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. |
| 212 | reset_remote |
| 213 | git -C "$origin" update-ref refs/heads/main "$head" |
| 214 | record_outcome "$head" "$base" land |
| 215 | printf 'staged\n' > "$repo/staged.txt" |
| 216 | git -C "$repo" add staged.txt |
| 217 | printf 'unstaged\n' >> "$repo/readme.txt" |
| 218 | status |
| 219 | contains 'integrated into origin/main' 'dirty local work obscured the remote result' |
| 220 | git -C "$repo" reset --quiet --hard "$head" |
| 221 | status |
| 222 | lacks '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. |
| 226 | git -C "$repo" checkout --quiet -b feature "$base" |
| 227 | status |
| 228 | contains 'git switch main' 'an up-to-date local main was not offered for viewing' |
| 229 | lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull from a feature checkout' |
| 230 | git -C "$repo" branch --force main "$advanced" >/dev/null |
| 231 | status |
| 232 | contains 'diverged' 'a divergent local main was overlooked from a feature checkout' |
| 233 | lacks 'git pull --ff-only origin main' 'a divergent local main was told to fast-forward' |
| 234 | git -C "$repo" branch --force main "$head" >/dev/null |
| 235 | |
| 236 | # A newer topic commit must not be mistaken for the older pending request. |
| 237 | reset_remote |
| 238 | git -C "$origin" update-ref "$request" "$base" |
| 239 | status |
| 240 | contains 'latest commit not submitted' 'a newer topic inherited an older request' |
| 241 | contains '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. |
| 246 | git -C "$repo" remote set-url origin "$w/missing.git" |
| 247 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 248 | matches 'origin|remote|fetch|repository' 'an unavailable origin had no useful diagnostic' |
| 249 | lacks 'integrated into origin/main' 'a failed fetch reported cached success' |
| 250 | lacks 'not submitted' 'a failed fetch was mistaken for an absent request' |
| 251 | git -C "$repo" remote remove origin |
| 252 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 253 | matches 'origin|remote' 'a missing origin had no useful diagnostic' |
| 254 | lacks 'integrated into origin/main' 'a missing origin reported cached success' |
| 255 | |
| 256 | echo 'valley-status: remote outcomes and local state checks passed' |
| 257 | touch "$out" |