| 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 |
| 10 | set -euo pipefail |
| 11 | |
| 12 | w="$TMPDIR/valley-status" |
| 13 | mkdir -p "$w" |
| 14 | export GIT_CONFIG_GLOBAL="$w/gitconfig" |
| 15 | export GIT_CONFIG_NOSYSTEM=1 |
| 16 | git config --global user.name valley-status-check |
| 17 | git config --global user.email valley-status-check@localhost |
| 18 | git config --global init.defaultBranch main |
| 19 | git config --global advice.detachedHead false |
| 20 | |
| 21 | origin="$w/project.git" |
| 22 | repo="$w/project" |
| 23 | git init --quiet --bare "$origin" |
| 24 | git init --quiet "$repo" |
| 25 | git -C "$repo" remote add origin "$origin" |
| 26 | printf 'base\n' > "$repo/readme.txt" |
| 27 | git -C "$repo" add readme.txt |
| 28 | git -C "$repo" commit --quiet -m base |
| 29 | git -C "$repo" push --quiet origin main |
| 30 | base="$(git -C "$repo" rev-parse HEAD)" |
| 31 | git -C "$repo" checkout --quiet -b topic |
| 32 | printf 'topic\n' > "$repo/readme.txt" |
| 33 | git -C "$repo" commit --quiet -am topic |
| 34 | head="$(git -C "$repo" rev-parse HEAD)" |
| 35 | git -C "$repo" push --quiet origin topic |
| 36 | git -C "$repo" checkout --quiet main |
| 37 | git -C "$repo" fetch --quiet origin |
| 38 | |
| 39 | request=refs/the-valley/integration-requests/main/topic |
| 40 | memo=refs/the-valley/integration-outcomes/main/topic |
| 41 | evidence_ref="refs/the-valley/attestations/$(printf '%064d' 0)/test" |
| 42 | |
| 43 | fail() { |
| 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 | |
| 50 | contains() { |
| 51 | grep -qiF -- "$1" "$w/status.out" || fail "$2" |
| 52 | } |
| 53 | |
| 54 | lacks() { |
| 55 | if grep -qiF -- "$1" "$w/status.out"; then fail "$2"; fi |
| 56 | } |
| 57 | |
| 58 | matches() { |
| 59 | grep -qiE -- "$1" "$w/status.out" || fail "$2" |
| 60 | } |
| 61 | |
| 62 | evidence_state() { |
| 63 | git -C "$origin" for-each-ref --format='%(refname) %(objectname)' \ |
| 64 | refs/the-valley/attestations | sha256sum | cut -c1-16 |
| 65 | } |
| 66 | |
| 67 | record_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 | |
| 74 | reset_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. |
| 85 | local_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 | |
| 97 | status() { |
| 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. |
| 107 | status_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. |
| 121 | json() { |
| 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. |
| 136 | json_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. |
| 145 | reset_remote |
| 146 | status |
| 147 | contains 'not submitted' 'an unsubmitted branch was not identified' |
| 148 | lacks 'integrated into origin/main' 'an unsubmitted branch was reported integrated' |
| 149 | json not-requested ".head == \"$head\"" '.request == null' '.outcome == null' |
| 150 | git -C "$origin" update-ref "$request" "$head" |
| 151 | status |
| 152 | contains 'awaiting' 'a new request was not reported as waiting' |
| 153 | contains 'decision' 'the pending message did not say what is awaited' |
| 154 | json 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. |
| 160 | record_outcome "$head" "$base" stale |
| 161 | status |
| 162 | contains 'needs attention' 'current staleness did not call for attention' |
| 163 | contains 'stale' 'current staleness lost the recorded outcome' |
| 164 | json stale ".outcome == {\"decision\": \"stale\", \"head\": \"$head\", \"tip\": \"$base\", \"failed_at\": null}" |
| 165 | record_outcome "$head" "$base" reject |
| 166 | status |
| 167 | contains 'rejected' 'a current rejection was not reported' |
| 168 | json rejected '.outcome.decision == "reject"' ".request.commit == \"$head\"" |
| 169 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 170 | status |
| 171 | contains 'failed' 'a controller failure was not reported' |
| 172 | matches 'may.*land|might.*land|cannot.*(rule out|confirm|determine)|does not.*(prove|mean|establish).*land|not.*confirm.*land' \ |
| 173 | 'a failure was presented as proof that nothing landed' |
| 174 | json failed '.outcome.decision == "failed"' '.outcome.failed_at == "2026-09-21T12:00:00Z"' |
| 175 | |
| 176 | record_outcome "$head" "$base" stale |
| 177 | other_tree="$(git -C "$origin" rev-parse "$base^{tree}")" |
| 178 | advanced="$(printf 'another landing\n' | git -C "$origin" commit-tree "$other_tree" -p "$base")" |
| 179 | git -C "$origin" update-ref refs/heads/main "$advanced" |
| 180 | status |
| 181 | contains 'awaiting' 'a moved target retained the previous refusal' |
| 182 | contains 'new decision' 'a moved target did not explain re-evaluation' |
| 183 | json awaiting-new-decision ".target_tip == \"$advanced\"" ".outcome.tip == \"$base\"" \ |
| 184 | '.outcome.decision == "stale"' |
| 185 | |
| 186 | reset_remote |
| 187 | git -C "$origin" update-ref "$request" "$head" |
| 188 | record_outcome "$head" "$base" stale |
| 189 | evidence="$(printf 'new evidence\n' | git -C "$origin" hash-object -w --stdin)" |
| 190 | git -C "$origin" update-ref "$evidence_ref" "$evidence" |
| 191 | status |
| 192 | contains 'awaiting' 'new evidence retained the previous refusal' |
| 193 | contains 'new decision' 'new evidence did not explain re-evaluation' |
| 194 | json awaiting-new-decision ".target_tip == \"$base\"" '.outcome.decision == "stale"' |
| 195 | |
| 196 | # A reused change name must not carry an earlier commit's rejection or |
| 197 | # landing forward to its replacement. |
| 198 | reset_remote |
| 199 | git -C "$origin" update-ref "$request" "$head" |
| 200 | record_outcome "$base" "$base" reject |
| 201 | status |
| 202 | contains 'awaiting' 'an earlier head verdict was applied to the current request' |
| 203 | lacks 'needs attention' 'an earlier rejection was treated as current' |
| 204 | json awaiting-decision ".head == \"$head\"" ".outcome.head == \"$base\"" \ |
| 205 | '.outcome.decision == "reject"' |
| 206 | git -C "$origin" update-ref -d "$request" |
| 207 | record_outcome "$base" "$base" land |
| 208 | status |
| 209 | contains 'not submitted' 'an old landing was applied to a newer branch head' |
| 210 | lacks 'integrated into origin/main' 'the newer branch inherited an old landing' |
| 211 | json not-requested ".head == \"$head\"" '.request == null' \ |
| 212 | ".outcome.head == \"$base\"" '.outcome.decision == "land"' |
| 213 | |
| 214 | # Main is authoritative for direct landings. A post-commit failure may |
| 215 | # leave both a failed memo and an unconsumed request behind. |
| 216 | reset_remote |
| 217 | git -C "$origin" update-ref refs/heads/main "$head" |
| 218 | git -C "$origin" update-ref "$request" "$head" |
| 219 | record_outcome "$head" "$base" 'failed 2026-09-21T12:00:00Z' |
| 220 | status |
| 221 | contains 'integrated into origin/main' 'a failed bookkeeping step hid an actual landing' |
| 222 | contains 'git pull --ff-only origin main' 'a behind local main had no update instruction' |
| 223 | json integrated ".head == \"$head\"" ".request.commit == \"$head\"" \ |
| 224 | '.outcome.decision == "failed"' |
| 225 | git -C "$origin" update-ref -d "$request" |
| 226 | record_outcome "$head" "$base" land |
| 227 | status |
| 228 | contains 'integrated into origin/main' 'a consumed request hid a direct landing' |
| 229 | json integrated ".head == \"$head\"" '.request == null' '.outcome.decision == "land"' |
| 230 | |
| 231 | # Reparenting changes the landed commit id. The matching durable outcome |
| 232 | # still identifies the submitted change, including after topic cleanup. |
| 233 | reset_remote |
| 234 | topic_tree="$(git -C "$origin" rev-parse "$head^{tree}")" |
| 235 | landed="$(printf 'reparented landing\n' | git -C "$origin" commit-tree "$topic_tree" -p "$advanced")" |
| 236 | git -C "$origin" update-ref refs/heads/main "$landed" |
| 237 | record_outcome "$head" "$advanced" land |
| 238 | status |
| 239 | contains 'integrated into origin/main' 'a reparented landing was missed' |
| 240 | json integrated-recorded ".head == \"$head\"" ".target_tip == \"$landed\"" \ |
| 241 | ".outcome.head == \"$head\"" |
| 242 | git -C "$origin" update-ref -d refs/heads/topic |
| 243 | status |
| 244 | contains 'integrated into origin/main' 'deleting a landed topic hid its outcome' |
| 245 | json integrated-recorded ".head == \"$head\"" '.request == null' |
| 246 | |
| 247 | # A disappeared request without a landing record cannot establish success. |
| 248 | reset_remote |
| 249 | git -C "$origin" update-ref -d refs/heads/topic |
| 250 | if (cd "$repo" && valley status topic) > "$w/status.out" 2>&1; then |
| 251 | fail 'a nonexistent branch and request were accepted' |
| 252 | fi |
| 253 | matches 'no.*recorded.*request|no.*request.*record|no.*branch.*request' \ |
| 254 | 'a missing branch and request had no actionable explanation' |
| 255 | lacks 'integrated into origin/main' 'absence was treated as a landing' |
| 256 | json_refused |
| 257 | |
| 258 | # A corrupted memo must not turn a valid pending request into success or |
| 259 | # abort the status read without explaining which record is unreadable. |
| 260 | reset_remote |
| 261 | git -C "$origin" update-ref "$request" "$head" |
| 262 | bad="$(printf 'not an outcome\n' | git -C "$origin" hash-object -w --stdin)" |
| 263 | git -C "$origin" update-ref "$memo" "$bad" |
| 264 | local_state > "$w/before" |
| 265 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 266 | local_state > "$w/after" |
| 267 | diff -u "$w/before" "$w/after" || fail 'a malformed memo changed local state' |
| 268 | matches 'malformed|unreadable|invalid|unrecognized|not recognized' \ |
| 269 | 'a malformed memo was silently accepted' |
| 270 | lacks 'integrated into origin/main' 'a malformed memo became a successful landing' |
| 271 | # An unreadable record is present and says nothing: no head, tip or time |
| 272 | # from it is reported as though it had been read. |
| 273 | json outcome-unreadable ".head == \"$head\"" \ |
| 274 | '.outcome == {"decision": "unknown", "head": null, "tip": null, "failed_at": null}' |
| 275 | |
| 276 | # Status must remain useful with uncommitted work, and its suggestion to |
| 277 | # update main must disappear once the local main already has the landing. |
| 278 | reset_remote |
| 279 | git -C "$origin" update-ref refs/heads/main "$head" |
| 280 | record_outcome "$head" "$base" land |
| 281 | printf 'staged\n' > "$repo/staged.txt" |
| 282 | git -C "$repo" add staged.txt |
| 283 | printf 'unstaged\n' >> "$repo/readme.txt" |
| 284 | status |
| 285 | contains 'integrated into origin/main' 'dirty local work obscured the remote result' |
| 286 | json integrated ".head == \"$head\"" |
| 287 | git -C "$repo" reset --quiet --hard "$head" |
| 288 | status |
| 289 | lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull' |
| 290 | |
| 291 | # On another branch, advice must describe local main rather than assume |
| 292 | # main is at the commit currently checked out. |
| 293 | git -C "$repo" checkout --quiet -b feature "$base" |
| 294 | status |
| 295 | contains 'git switch main' 'an up-to-date local main was not offered for viewing' |
| 296 | lacks 'git pull --ff-only origin main' 'an up-to-date local main was told to pull from a feature checkout' |
| 297 | git -C "$repo" branch --force main "$advanced" >/dev/null |
| 298 | status |
| 299 | contains 'diverged' 'a divergent local main was overlooked from a feature checkout' |
| 300 | lacks 'git pull --ff-only origin main' 'a divergent local main was told to fast-forward' |
| 301 | git -C "$repo" branch --force main "$head" >/dev/null |
| 302 | |
| 303 | # A newer topic commit must not be mistaken for the older pending request. |
| 304 | reset_remote |
| 305 | git -C "$origin" update-ref "$request" "$base" |
| 306 | status |
| 307 | contains 'latest commit not submitted' 'a newer topic inherited an older request' |
| 308 | contains 'older request' 'an outstanding older request was hidden' |
| 309 | json outdated-request ".head == \"$head\"" ".request.commit == \"$base\"" |
| 310 | |
| 311 | # A change name is carried exactly. A branch name may hold quotes and the |
| 312 | # characters a shell reads specially: JSON escapes them, the request name |
| 313 | # maps only the slash, and the command the sentences suggest reads back, |
| 314 | # in a shell, as the same name. |
| 315 | odd='odd/"quoted"&<name>;$(x)' |
| 316 | oddrequest='odd-"quoted"&<name>;$(x)' |
| 317 | git -C "$origin" update-ref "refs/heads/$odd" "$head" |
| 318 | git -C "$origin" update-ref "refs/the-valley/integration-requests/main/$oddrequest" "$head" |
| 319 | status "$odd" |
| 320 | suggested="$(sed -n 's/^Check again: valley status //p' "$w/status.out")" |
| 321 | eval "set -- $suggested" |
| 322 | [ "$#" = 1 ] && [ "$1" = "$odd" ] || fail "the suggested command names something other than $odd" |
| 323 | status_json "$odd" |
| 324 | jq -e --arg change "$odd" --arg name "$oddrequest" --arg head "$head" ' |
| 325 | .change == $change and .request_name == $name and .state == "awaiting-decision" |
| 326 | and .request == {ref: ("refs/the-valley/integration-requests/main/" + $name), commit: $head} |
| 327 | ' "$w/status.json" > /dev/null || fail "status --json did not carry the name $odd exactly" |
| 328 | git -C "$origin" update-ref -d "refs/heads/$odd" |
| 329 | git -C "$origin" update-ref -d "refs/the-valley/integration-requests/main/$oddrequest" |
| 330 | |
| 331 | # A target with no main is no answer either. |
| 332 | git -C "$origin" update-ref -d refs/heads/main |
| 333 | json_refused |
| 334 | grep -q 'no main branch' "$w/status.err" || fail 'a missing main had no useful diagnostic' |
| 335 | git -C "$origin" update-ref refs/heads/main "$base" |
| 336 | |
| 337 | # Connectivity failures are not an empty pending queue. These cases do not |
| 338 | # require one particular exit convention, only a diagnostic and no claim |
| 339 | # about a successfully inspected remote. |
| 340 | git -C "$repo" remote set-url origin "$w/missing.git" |
| 341 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 342 | matches 'origin|remote|fetch|repository' 'an unavailable origin had no useful diagnostic' |
| 343 | lacks 'integrated into origin/main' 'a failed fetch reported cached success' |
| 344 | lacks 'not submitted' 'a failed fetch was mistaken for an absent request' |
| 345 | json_refused |
| 346 | grep -q 'could not read origin' "$w/status.err" || fail 'an unreadable origin was not named as such' |
| 347 | git -C "$repo" remote remove origin |
| 348 | (cd "$repo" && valley status topic) > "$w/status.out" 2>&1 || true |
| 349 | matches 'origin|remote' 'a missing origin had no useful diagnostic' |
| 350 | lacks 'integrated into origin/main' 'a missing origin reported cached success' |
| 351 | json_refused |
| 352 | |
| 353 | echo 'valley-status: remote outcomes and local state checks passed' |
| 354 | touch "$out" |