| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export XDG_STATE_HOME="$TMPDIR/state" |
| 6 | export GIT_CONFIG_NOSYSTEM=1 |
| 7 | # What this check writes down, kept out of the repository under test: an |
| 8 | # answer file or a captured stream sitting in the working tree would be |
| 9 | # committed by the next `git add -A` and change the tree being attested. |
| 10 | w="$TMPDIR/scratch" |
| 11 | mkdir -p "$w" |
| 12 | git config --global user.name valley-check |
| 13 | git config --global user.email valley-check@localhost |
| 14 | git config --global init.defaultBranch main |
| 15 | git config --global advice.detachedHead false |
| 16 | |
| 17 | # The operator's signing key. Provisioning one is deployment, so the verb |
| 18 | # takes it as a parameter and this check supplies a throwaway. |
| 19 | ssh-keygen -q -t ed25519 -N "" -C valley-check -f "$TMPDIR/host" |
| 20 | export VALLEY_ATTEST_KEY="$TMPDIR/host" |
| 21 | export VALLEY_ATTEST_NAME=valley-host.example.org/attestations |
| 22 | keyhash="$(attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" | cut -d+ -f2)" |
| 23 | |
| 24 | origin="$TMPDIR/project.git" |
| 25 | git init --quiet --bare "$origin" |
| 26 | |
| 27 | # Every push the bare repo receives, as its hook sees it. The claim approve |
| 28 | # makes is that ONE push carries the evidence and the request together, and |
| 29 | # that claim is only observable from the receiving side. |
| 30 | pushlog="$TMPDIR/pushlog" |
| 31 | cat > "$origin/hooks/pre-receive" <<EOF |
| 32 | #!/bin/sh |
| 33 | echo '--- push' >> $pushlog |
| 34 | cat >> $pushlog |
| 35 | EOF |
| 36 | chmod +x "$origin/hooks/pre-receive" |
| 37 | |
| 38 | # Interpose on origin's receive-pack so that the request namespace moves |
| 39 | # between approve's read of it and the push it then makes. That window is |
| 40 | # where every race below lives, and a hook cannot reach it: by the time |
| 41 | # pre-receive runs the client has already been told what the refs were. |
| 42 | # One shot, so only the push under test is raced. |
| 43 | arm_race() { |
| 44 | cat > "$TMPDIR/racing-receive-pack" <<EOF |
| 45 | #!/bin/sh |
| 46 | if [ -e "$TMPDIR/race-armed" ]; then |
| 47 | rm -f "$TMPDIR/race-armed" |
| 48 | $1 |
| 49 | fi |
| 50 | exec git-receive-pack "\$@" |
| 51 | EOF |
| 52 | chmod +x "$TMPDIR/racing-receive-pack" |
| 53 | git config remote.origin.receivepack "$TMPDIR/racing-receive-pack" |
| 54 | touch "$TMPDIR/race-armed" |
| 55 | } |
| 56 | |
| 57 | # Put it away, and fail if it never fired: a race nothing raced is a |
| 58 | # scenario that proved nothing. |
| 59 | disarm_race() { |
| 60 | git config --unset remote.origin.receivepack |
| 61 | if [ -e "$TMPDIR/race-armed" ]; then |
| 62 | echo "valley-request: the racing receive-pack never ran, so nothing was raced" >&2 |
| 63 | exit 1 |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # ---------------------------------------------------------------------- |
| 68 | # The owning valley's repository, carrying the floor at policy/instance. |
| 69 | # The floor is read from here and never from the project, which is the |
| 70 | # tree under judgment: what governs a change has to come from outside it. |
| 71 | # Fetching is all the deriver does to this repository, which is what |
| 72 | # anyone the valley governs necessarily holds. |
| 73 | valley="$TMPDIR/valley.git" |
| 74 | git init --quiet --bare "$valley" |
| 75 | valleywork="$TMPDIR/valley-work" |
| 76 | git init --quiet "$valleywork" |
| 77 | mkdir -p "$valleywork/policy/instance" |
| 78 | cat > "$valleywork/policy/instance/floor.cue" <<'EOF' |
| 79 | package verification |
| 80 | |
| 81 | floor: { |
| 82 | checks: "tree-ok": { |
| 83 | runner: "command" |
| 84 | command: "test -f docs/readme.md" |
| 85 | } |
| 86 | classes: prose: { |
| 87 | paths: "docs/**": true |
| 88 | requires: "tree-ok": true |
| 89 | } |
| 90 | // A class that covers paths and asks nothing of them. Requiring no |
| 91 | // check is an ordinary outcome, not a hole: the policy has read these paths |
| 92 | // and has nothing to require of them. |
| 93 | classes: quiet: { |
| 94 | paths: "quiet/**": true |
| 95 | requires: {} |
| 96 | } |
| 97 | unclassified: "tree-ok": true |
| 98 | } |
| 99 | EOF |
| 100 | git -C "$valleywork" add -A |
| 101 | git -C "$valleywork" commit --quiet -m floor |
| 102 | git -C "$valleywork" push --quiet "$valley" main |
| 103 | |
| 104 | # ---------------------------------------------------------------------- |
| 105 | # The project, laid out the way the deriver reads one without being told: |
| 106 | # its own layer at policy/project, and its owning valley named at |
| 107 | # policy/valley. Both checks use the command runner: the nix runner cannot |
| 108 | # be exercised from inside a nix build, since the sandbox has no daemon to |
| 109 | # call, and what is under test here is the sequencing rather than the |
| 110 | # runner. |
| 111 | # |
| 112 | # examples/policy/ holds a second, contradictory pair of layers, and every |
| 113 | # check they require is named `phantom`. Nothing may derive it: a gate that |
| 114 | # came from a worked example is a gate nobody wrote, and the name is here |
| 115 | # so that the failure would be visible rather than plausible. |
| 116 | # |
| 117 | # schema/ is here because the-valley's own tree carries it. The packaged |
| 118 | # CLI never reads it; only bin/valley run straight from the checkout does |
| 119 | # (16). |
| 120 | repo="$TMPDIR/project" |
| 121 | git init --quiet "$repo" |
| 122 | cd "$repo" || exit 1 |
| 123 | git remote add origin "$origin" |
| 124 | |
| 125 | mkdir -p docs src schema policy/project examples/policy/instance examples/policy/project |
| 126 | cp "$schemaFile" schema/verification.cue |
| 127 | echo "the readme" > docs/readme.md |
| 128 | echo "$valley" > policy/valley |
| 129 | cat > policy/project/policy.cue <<'EOF' |
| 130 | package verification |
| 131 | |
| 132 | project: { |
| 133 | checks: "no-secrets": { |
| 134 | runner: "command" |
| 135 | command: "! grep -rqF SECRET src" |
| 136 | } |
| 137 | classes: source: { |
| 138 | paths: "src/**": true |
| 139 | requires: "no-secrets": true |
| 140 | } |
| 141 | } |
| 142 | EOF |
| 143 | cat > examples/policy/instance/floor.cue <<'EOF' |
| 144 | package verification |
| 145 | |
| 146 | floor: { |
| 147 | checks: phantom: { |
| 148 | runner: "command" |
| 149 | command: "false" |
| 150 | } |
| 151 | classes: everything: { |
| 152 | paths: "**": true |
| 153 | requires: phantom: true |
| 154 | } |
| 155 | unclassified: phantom: true |
| 156 | } |
| 157 | EOF |
| 158 | cat > examples/policy/project/policy.cue <<'EOF' |
| 159 | package verification |
| 160 | |
| 161 | project: {} |
| 162 | EOF |
| 163 | git add -A |
| 164 | git commit --quiet -m base |
| 165 | git push --quiet -u origin main |
| 166 | first="$(git rev-parse main)" |
| 167 | |
| 168 | # ---------------------------------------------------------------------- |
| 169 | # 1. A branch whose required check passes: the evidence and the request |
| 170 | # reach origin in one push, and main does not move. |
| 171 | git checkout --quiet -b topic/one |
| 172 | echo "a better readme" > docs/readme.md |
| 173 | git commit --quiet -am "improve the readme" |
| 174 | git push --quiet origin topic/one |
| 175 | digest="$(attest digest --rev topic/one | cut -d: -f2)" |
| 176 | |
| 177 | : > "$pushlog" |
| 178 | printf 'a\n' > "$w/answers" |
| 179 | valley review topic/one < "$w/answers" > "$w/approve.out" 2> "$w/approve.err" |
| 180 | |
| 181 | grep -q '^checking topic/one at .*: tree-ok$' "$w/approve.out" |
| 182 | grep -qE '^check tree-ok +command passed$' "$w/approve.out" |
| 183 | grep -qx "Approved topic/one ($(git rev-parse --short topic/one)): its integration request for origin/main is filed." "$w/approve.out" |
| 184 | grep -qx " request refs/the-valley/integration-requests/main/topic-one -> $(git rev-parse --short topic/one)" "$w/approve.out" |
| 185 | grep -qx " evidence refs/the-valley/attestations/$digest/$keyhash" "$w/approve.out" |
| 186 | grep -qx ' checks tree-ok' "$w/approve.out" |
| 187 | grep -qx 'Your local checkout has not changed.' "$w/approve.out" |
| 188 | grep -qx 'Check the result: valley status topic/one' "$w/approve.out" |
| 189 | |
| 190 | # How long the check took is printed after it, and logged on this machine: |
| 191 | # one JSON line per required check, in the state directory, outside the |
| 192 | # repository and the evidence. A branch no record is on names no change. |
| 193 | grep -qE '^time tree-ok +passed +[0-9][0-9.hms]* \(ran\)$' "$w/approve.out" |
| 194 | timings="$XDG_STATE_HOME/valley/timings.jsonl" |
| 195 | if [ "$(wc -l < "$timings")" != 1 ] || ! jq -e \ |
| 196 | --arg head "$(git rev-parse topic/one)" --arg origin "$origin" ' |
| 197 | keys == ["check", "cost", "duration_ms", "head", "repository", "result", "runner", "satisfied", "time"] |
| 198 | and .check == "tree-ok" and .runner == "command" and .cost == "quick" |
| 199 | and .result == "passed" and .satisfied == "ran" |
| 200 | and .head == $head and .repository == $origin |
| 201 | and (.duration_ms | type == "number" and . >= 0) |
| 202 | and (.time | test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3}Z$"))' \ |
| 203 | "$timings" > /dev/null; then |
| 204 | echo "valley-request: approve did not log one timing for its one required check:" >&2 |
| 205 | cat "$timings" >&2 |
| 206 | exit 1 |
| 207 | fi |
| 208 | if [ "$(stat -c %a "$timings")" != 600 ]; then |
| 209 | echo "valley-request: the timing log was created $(stat -c %a "$timings"), not 600" >&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | |
| 213 | # What origin holds now. The attestation is keyed by the digest of the |
| 214 | # BRANCH's tree, which is what says the checks ran over the right tree and |
| 215 | # not over the operator's working copy. |
| 216 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/published.txt" |
| 217 | grep -qx "refs/the-valley/integration-requests/main/topic-one" "$w/published.txt" |
| 218 | grep -qx "refs/the-valley/attestations/$digest/$keyhash" "$w/published.txt" |
| 219 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-one)" \ |
| 220 | != "$(git rev-parse topic/one)" ]; then |
| 221 | echo "valley-request: the request ref does not name the branch's head" >&2 |
| 222 | exit 1 |
| 223 | fi |
| 224 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$first" ]; then |
| 225 | echo "valley-request: [a]pprove moved main" >&2 |
| 226 | exit 1 |
| 227 | fi |
| 228 | |
| 229 | # One push carried both. This is the friction the verb exists to remove: |
| 230 | # `attest run --push` publishes the branch and the evidence and knows |
| 231 | # nothing about a request ref, so doing this by hand is two beats. |
| 232 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 233 | if [ "$pushes" != 1 ]; then |
| 234 | echo "valley-request: the evidence and the request took $pushes pushes, not 1" >&2 |
| 235 | cat "$pushlog" >&2 |
| 236 | exit 1 |
| 237 | fi |
| 238 | grep -q "refs/the-valley/attestations/$digest/$keyhash" "$pushlog" |
| 239 | grep -q 'refs/the-valley/integration-requests/main/topic-one' "$pushlog" |
| 240 | |
| 241 | # The note origin holds is the one the operator signed, over that tree. |
| 242 | git cat-file -p "refs/the-valley/attestations/$digest/$keyhash:tree-ok/statement.note" \ |
| 243 | > "$w/statement.note" |
| 244 | attest key --key "$TMPDIR/host" --name "$VALLEY_ATTEST_NAME" > "$w/known_keys" |
| 245 | attest verify --note "$w/statement.note" --repo "$repo" --rev topic/one \ |
| 246 | --known-keys "$w/known_keys" --signer "$VALLEY_ATTEST_NAME" > "$w/verify.out" |
| 247 | grep -q 'matches the recorded subject digest' "$w/verify.out" |
| 248 | |
| 249 | # ---------------------------------------------------------------------- |
| 250 | # 2. The layers the derivation used are the ones that govern, and it says |
| 251 | # which they were: the floor fetched from the owning valley, and the |
| 252 | # project's own layer at policy/project in the TARGET's tip. |
| 253 | # examples/policy/ is in this tree and requires `phantom` of every path, |
| 254 | # and no default reaches it — it is derivable only by naming it, which |
| 255 | # is what the options are for. |
| 256 | valley checks "$first" topic/one > "$w/checks.out" 2> "$w/checks.err" |
| 257 | grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/checks.out" |
| 258 | grep -qx 'classes matched: prose' "$w/checks.out" |
| 259 | grep -qE '^ tree-ok +mandatory +prose$' "$w/checks.out" |
| 260 | if grep -q phantom "$w/checks.out" "$w/checks.err"; then |
| 261 | echo "valley-request: a default derived a check from examples/policy/" >&2 |
| 262 | cat "$w/checks.out" "$w/checks.err" >&2 |
| 263 | exit 1 |
| 264 | fi |
| 265 | |
| 266 | # Named, the example layers derive exactly what they declare. This is the |
| 267 | # other half of the claim above: examples/policy/ is readable, and reading |
| 268 | # it is something a person asks for. |
| 269 | valley checks --instance examples/policy/instance --project examples/policy/project \ |
| 270 | "$first" topic/one > "$w/example.out" |
| 271 | grep -qE '^ phantom +mandatory' "$w/example.out" |
| 272 | grep -qx 'policy: examples/policy/instance + examples/policy/project' "$w/example.out" |
| 273 | |
| 274 | # ---------------------------------------------------------------------- |
| 275 | # 3. main moves on, so the next branch is stale. [a]pprove is offered there |
| 276 | # too — the integrator applies a delta to the tip rather than |
| 277 | # fast-forwarding — and a branch whose check fails publishes nothing |
| 278 | # and leaves the review loop usable. Approving the same head again |
| 279 | # would only fail again, so after the failure the prompt says approving |
| 280 | # needs a new head and offers [b]ase, [r]eject and [s]kip alone, and an |
| 281 | # answer of a is not taken. |
| 282 | git checkout --quiet main |
| 283 | echo "a note" > docs/notes.md |
| 284 | git add -A |
| 285 | git commit --quiet -m "a landing that moves main" |
| 286 | git push --quiet origin main |
| 287 | |
| 288 | git checkout --quiet -b topic/two "$first" |
| 289 | printf 'SECRET\n' > src/leak.txt |
| 290 | git add -A |
| 291 | git commit --quiet -m "a leak" |
| 292 | git push --quiet origin topic/two |
| 293 | |
| 294 | : > "$pushlog" |
| 295 | printf 'a\na\ns\n' > "$w/answers" |
| 296 | valley review topic/two < "$w/answers" > "$w/fail.out" 2> "$w/fail.err" |
| 297 | |
| 298 | # The prompt is the whole of what the operator is offered, so it is pinned |
| 299 | # character for character: a verb that stops being offered, or one that |
| 300 | # starts, is a change to the review loop and not an incidental one. |
| 301 | grep -qF 'topic/two does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/fail.out" |
| 302 | grep -q '^checking topic/two at .*: no-secrets$' "$w/fail.out" |
| 303 | twohead="$(git rev-parse --short topic/two)" |
| 304 | grep -qF "topic/two does not fast-forward from main. Its checks failed at $twohead, so approving needs a new head: [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? " "$w/fail.out" |
| 305 | grep -qF 'answer v, b, r, or s' "$w/fail.out" |
| 306 | offers="$(grep -oF '[a]pprove' "$w/fail.out" | wc -l)" |
| 307 | checked="$(grep -c '^checking topic/two at ' "$w/fail.out" || true)" |
| 308 | if [ "$offers" != 1 ] || [ "$checked" != 1 ]; then |
| 309 | echo "valley-request: after its checks failed, the prompt offered [a]pprove again ($offers offers, $checked runs)" >&2 |
| 310 | cat "$w/fail.out" >&2 |
| 311 | exit 1 |
| 312 | fi |
| 313 | grep -qE '^check no-secrets +command failed$' "$w/fail.out" |
| 314 | grep -q 'the checks did not pass; nothing published, main untouched' "$w/fail.err" |
| 315 | # The prompt is printed without a newline, so what follows a refusal shares |
| 316 | # its line — the answer to it is not anchored. |
| 317 | grep -q 'skipped topic/two; nothing done' "$w/fail.out" |
| 318 | if [ -s "$pushlog" ]; then |
| 319 | echo "valley-request: a failing check pushed something" >&2 |
| 320 | cat "$pushlog" >&2 |
| 321 | exit 1 |
| 322 | fi |
| 323 | git -C "$origin" for-each-ref --format='%(refname)' refs/the-valley > "$w/after.txt" |
| 324 | if grep -q 'topic-two' "$w/after.txt"; then |
| 325 | echo "valley-request: a failing check left a ref on origin" >&2 |
| 326 | cat "$w/after.txt" >&2 |
| 327 | exit 1 |
| 328 | fi |
| 329 | |
| 330 | # The same for a branch that fast-forwards: after its checks fail, the |
| 331 | # prompt offers [r]eject and [s]kip alone. |
| 332 | git checkout --quiet -b topic/leak main |
| 333 | mkdir -p src |
| 334 | printf 'SECRET\n' > src/leak.txt |
| 335 | git add -A |
| 336 | git commit --quiet -m "another leak" |
| 337 | git push --quiet origin topic/leak |
| 338 | leakhead="$(git rev-parse --short topic/leak)" |
| 339 | : > "$pushlog" |
| 340 | printf 'a\na\ns\n' > "$w/answers" |
| 341 | valley review topic/leak < "$w/answers" > "$w/leak.out" 2> "$w/leak.err" |
| 342 | grep -qF 'topic/leak: [a]pprove, [v]iew full diff, [r]eject, [s]kip? ' "$w/leak.out" |
| 343 | grep -qF "topic/leak: its checks failed at $leakhead, so approving needs a new head: [v]iew full diff, [r]eject, [s]kip? " "$w/leak.out" |
| 344 | grep -qF 'answer v, r, or s' "$w/leak.out" |
| 345 | grep -q 'skipped topic/leak; nothing done' "$w/leak.out" |
| 346 | offers="$(grep -oF '[a]pprove' "$w/leak.out" | wc -l)" |
| 347 | checked="$(grep -c '^checking topic/leak at ' "$w/leak.out" || true)" |
| 348 | if [ "$offers" != 1 ] || [ "$checked" != 1 ] || [ -s "$pushlog" ]; then |
| 349 | echo "valley-request: after its checks failed, a branch that fast-forwards was offered [a]pprove again ($offers offers, $checked runs)" >&2 |
| 350 | cat "$w/leak.out" "$pushlog" >&2 |
| 351 | exit 1 |
| 352 | fi |
| 353 | |
| 354 | # ---------------------------------------------------------------------- |
| 355 | # 4. No signing key, with a check required: refused before any check is run, |
| 356 | # and the prompt still comes back. The host signs, and there is no |
| 357 | # unsigned mode. |
| 358 | : > "$pushlog" |
| 359 | printf 'a\ns\n' > "$w/answers" |
| 360 | env -u VALLEY_ATTEST_KEY valley review topic/two < "$w/answers" > "$w/nokey.out" 2> "$w/nokey.err" |
| 361 | grep -q 'no signing key: set VALLEY_ATTEST_KEY' "$w/nokey.err" |
| 362 | grep -q 'skipped topic/two; nothing done' "$w/nokey.out" |
| 363 | # A refusal that is no check failure leaves the prompt as it was, [a]pprove |
| 364 | # and all: a new session starts with no check failed. |
| 365 | offers="$(grep -oF 'topic/two does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/nokey.out" | wc -l)" |
| 366 | if [ "$offers" != 2 ]; then |
| 367 | echo "valley-request: a refusal that was no check failure changed the prompt" >&2 |
| 368 | cat "$w/nokey.out" >&2 |
| 369 | exit 1 |
| 370 | fi |
| 371 | if [ -s "$pushlog" ]; then |
| 372 | echo "valley-request: a run with no signing key pushed something" >&2 |
| 373 | exit 1 |
| 374 | fi |
| 375 | |
| 376 | # ---------------------------------------------------------------------- |
| 377 | # 5. A branch the policy requires no check of is filed as a request with |
| 378 | # no evidence. The integrator lands one and records it as a landing |
| 379 | # that required nothing, so refusing to file it here would strand |
| 380 | # exactly the changes a policy leaves unrequired. Nothing is required, |
| 381 | # so nothing is signed: this runs with no signing key at all. |
| 382 | git checkout --quiet main |
| 383 | git checkout --quiet -b topic/quiet |
| 384 | mkdir -p quiet |
| 385 | echo "a quiet note" > quiet/note.md |
| 386 | git add -A |
| 387 | git commit --quiet -m "a change under a class that asks nothing" |
| 388 | git push --quiet origin topic/quiet |
| 389 | quiethead="$(git rev-parse --short topic/quiet)" |
| 390 | |
| 391 | : > "$pushlog" |
| 392 | printf 'a\n' > "$w/answers" |
| 393 | env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \ |
| 394 | > "$w/quiet.out" 2> "$w/quiet.err" |
| 395 | |
| 396 | grep -q 'requires no check under .* + policy/project@origin/main .*; filing the request with no evidence' "$w/quiet.out" |
| 397 | grep -qF "$valley policy/instance@refs/heads/main" "$w/quiet.out" |
| 398 | grep -q '^Approved topic/quiet .*: its integration request for origin/main is filed\.$' "$w/quiet.out" |
| 399 | grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet.out" |
| 400 | grep -qx ' evidence none — nothing was required, so nothing was signed' "$w/quiet.out" |
| 401 | grep -qx ' checks none — the policy requires no check of this change' "$w/quiet.out" |
| 402 | |
| 403 | # One push, carrying the request and nothing else. |
| 404 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 405 | if [ "$pushes" != 1 ]; then |
| 406 | echo "valley-request: the bare request took $pushes pushes, not 1" >&2 |
| 407 | cat "$pushlog" >&2 |
| 408 | exit 1 |
| 409 | fi |
| 410 | if grep -q 'refs/the-valley/attestations/' "$pushlog"; then |
| 411 | echo "valley-request: a request that requires nothing published evidence" >&2 |
| 412 | cat "$pushlog" >&2 |
| 413 | exit 1 |
| 414 | fi |
| 415 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-quiet)" \ |
| 416 | != "$(git rev-parse topic/quiet)" ]; then |
| 417 | echo "valley-request: the bare request does not name the branch's head" >&2 |
| 418 | exit 1 |
| 419 | fi |
| 420 | |
| 421 | # ---------------------------------------------------------------------- |
| 422 | # 6. [b]ase, and the prompt it comes back to. A stale branch is rebased |
| 423 | # onto main in a throwaway worktree and re-reviewed, and the prompt the |
| 424 | # loop then prints is the fast-forward one — the path a reader of |
| 425 | # bin/valley has to trace two loops to reach, so it is exercised here |
| 426 | # rather than argued about. |
| 427 | git checkout --quiet main |
| 428 | git checkout --quiet -b topic/three "$first" |
| 429 | echo "another readme" > docs/readme.md |
| 430 | git commit --quiet -am "another readme" |
| 431 | git push --quiet origin topic/three |
| 432 | before_base="$(git -C "$origin" rev-parse refs/heads/main)" |
| 433 | |
| 434 | printf 'b\ns\n' > "$w/answers" |
| 435 | valley review topic/three < "$w/answers" > "$w/base.out" 2>&1 |
| 436 | grep -qF 'topic/three does not fast-forward from main: [a]pprove, [v]iew full diff, [b]ase onto main, [r]eject, [s]kip? ' "$w/base.out" |
| 437 | grep -qF 'rebased topic/three onto origin/main, pushed with --force-with-lease' "$w/base.out" |
| 438 | # The post-[b]ase prompt: the branch fast-forwards now, so the loop falls |
| 439 | # through to the prompt that has no [b]ase in it. |
| 440 | grep -qF 'topic/three: [a]pprove, [v]iew full diff, [r]eject, [s]kip? ' "$w/base.out" |
| 441 | grep -q 'skipped topic/three; nothing done' "$w/base.out" |
| 442 | git merge-base --is-ancestor refs/remotes/origin/main refs/remotes/origin/topic/three \ |
| 443 | || { echo "valley-request: [b]ase left the branch stale" >&2; exit 1; } |
| 444 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_base" ]; then |
| 445 | echo "valley-request: the review loop moved main" >&2 |
| 446 | exit 1 |
| 447 | fi |
| 448 | git checkout --quiet main |
| 449 | |
| 450 | # The request filed in 1 is still pending: nothing here consumes it, which |
| 451 | # is the integrator's own step. |
| 452 | git -C "$origin" rev-parse --verify --quiet \ |
| 453 | refs/the-valley/integration-requests/main/topic-one > /dev/null |
| 454 | |
| 455 | # ---------------------------------------------------------------------- |
| 456 | # 7. A change does not supply the policy that gates it. This branch |
| 457 | # deletes its own policy/project and repoints policy/valley at a |
| 458 | # repository that does not exist. Both layers come from the target's |
| 459 | # tip, so neither edit reaches the derivation: it composes the same two |
| 460 | # layers it would for any other branch, and the fetch that a branch-read |
| 461 | # pointer would have attempted never happens. |
| 462 | git checkout --quiet -b topic/nolayer |
| 463 | git rm --quiet -r policy/project |
| 464 | echo "$TMPDIR/there-is-no-such-valley.git" > policy/valley |
| 465 | git add -A |
| 466 | git commit --quiet -m "delete the project layer and repoint the valley" |
| 467 | git push --quiet origin topic/nolayer |
| 468 | |
| 469 | valley checks "$first" topic/nolayer > "$w/nolayer.out" 2> "$w/nolayer.err" |
| 470 | grep -qx "policy: $valley policy/instance@refs/heads/main + policy/project@origin/main $(git rev-parse --short origin/main)" "$w/nolayer.out" |
| 471 | if grep -q 'there-is-no-such-valley' "$w/nolayer.out" "$w/nolayer.err"; then |
| 472 | echo "valley-request: the deriver followed the branch's own valley pointer" >&2 |
| 473 | cat "$w/nolayer.out" "$w/nolayer.err" >&2 |
| 474 | exit 1 |
| 475 | fi |
| 476 | |
| 477 | : > "$pushlog" |
| 478 | printf 'a\n' > "$w/answers" |
| 479 | valley review topic/nolayer < "$w/answers" \ |
| 480 | > "$w/nolayer.review.out" 2> "$w/nolayer.review.err" |
| 481 | grep -q '^Approved topic/nolayer .*: its integration request for origin/main is filed\.$' "$w/nolayer.review.out" |
| 482 | |
| 483 | # ---------------------------------------------------------------------- |
| 484 | # 8. The reviewer's checkout contributes nothing. This is the case that |
| 485 | # failed in live use: the operator's checkout was an older branch |
| 486 | # without the landed policy layer, and the deriver read the working |
| 487 | # tree and refused. topic/nolayer is exactly such a checkout — no |
| 488 | # policy/project in it, and a policy/valley naming nowhere — so every |
| 489 | # derivation below runs from it. |
| 490 | # |
| 491 | # Byte-identical output is the claim, not merely a successful one: |
| 492 | # review works the same from any branch, however stale. |
| 493 | git checkout --quiet main |
| 494 | valley checks "$first" topic/one > "$w/from-main.out" |
| 495 | git checkout --quiet topic/nolayer |
| 496 | valley checks "$first" topic/one > "$w/from-stale.out" |
| 497 | if ! cmp -s "$w/from-main.out" "$w/from-stale.out"; then |
| 498 | echo "valley-request: the checkout changed what the derivation said" >&2 |
| 499 | diff -u "$w/from-main.out" "$w/from-stale.out" >&2 || true |
| 500 | exit 1 |
| 501 | fi |
| 502 | |
| 503 | # And [a]pprove, which is where it failed: a request filed from that same |
| 504 | # checkout, over a branch the stale tree knows nothing about. |
| 505 | git checkout --quiet -b topic/four origin/main |
| 506 | echo "one more readme" > docs/readme.md |
| 507 | git commit --quiet -am "one more readme" |
| 508 | git push --quiet origin topic/four |
| 509 | git checkout --quiet topic/nolayer |
| 510 | |
| 511 | : > "$pushlog" |
| 512 | printf 'a\n' > "$w/answers" |
| 513 | valley review topic/four < "$w/answers" > "$w/stale.out" 2> "$w/stale.err" |
| 514 | grep -q '^checking topic/four at .*: tree-ok$' "$w/stale.out" |
| 515 | grep -q '^Approved topic/four .*: its integration request for origin/main is filed\.$' "$w/stale.out" |
| 516 | grep -qx ' checks tree-ok' "$w/stale.out" |
| 517 | |
| 518 | # ---------------------------------------------------------------------- |
| 519 | # 9. A target carrying no project layer at all is derived under the floor |
| 520 | # alone, and says so. The project layer only ever adds, so the identity |
| 521 | # of "adds" is "adds nothing"; refusing instead would make a repository |
| 522 | # underivable until its first policy commit, and that commit could not |
| 523 | # derive either. This is the integrator's own reading of an absent |
| 524 | # layer (integrator/policy.go), and the two have to agree. |
| 525 | bare2="$TMPDIR/floorless.git" |
| 526 | git init --quiet --bare "$bare2" |
| 527 | repo2="$TMPDIR/floorless" |
| 528 | git init --quiet "$repo2" |
| 529 | cd "$repo2" || exit 1 |
| 530 | git remote add origin "$bare2" |
| 531 | mkdir -p docs policy |
| 532 | echo "the readme" > docs/readme.md |
| 533 | echo "$valley" > policy/valley |
| 534 | git add -A |
| 535 | git commit --quiet -m base |
| 536 | git push --quiet -u origin main |
| 537 | base2="$(git rev-parse main)" |
| 538 | echo "a second readme" > docs/readme.md |
| 539 | git commit --quiet -am "a second readme" |
| 540 | |
| 541 | valley checks "$base2" HEAD > "$w/floorless.out" 2> "$w/floorless.err" |
| 542 | grep -qx "policy: $valley policy/instance@refs/heads/main + no policy/project@origin/main $(git rev-parse --short origin/main) — the floor alone" "$w/floorless.out" |
| 543 | grep -qE '^ tree-ok +mandatory +prose$' "$w/floorless.out" |
| 544 | if grep -q no-secrets "$w/floorless.out"; then |
| 545 | echo "valley-request: the floor-alone derivation saw another project's layer" >&2 |
| 546 | exit 1 |
| 547 | fi |
| 548 | cd "$repo" || exit 1 |
| 549 | git checkout --quiet main |
| 550 | |
| 551 | # ---------------------------------------------------------------------- |
| 552 | # 10. Approving again after main moved. This is the case that wedged in live |
| 553 | # use. A request goes stale with reason=evidence when main lands |
| 554 | # inside a required check's input closure: no attestation over the |
| 555 | # requested tree can transfer again, so the cure is a new head rebased |
| 556 | # onto the moved main. That head is not a descendant of the one the |
| 557 | # request ref holds, which makes writing it a non-fast-forward |
| 558 | # replacement — and a push without a lease is refused by git's own |
| 559 | # rule before origin's hook is ever consulted. The verdict that sends |
| 560 | # a change down this path is the integrator's and is pinned by |
| 561 | # integrator-e2e; what is under test here is that [a]pprove can act on it. |
| 562 | git checkout --quiet -b topic/five origin/main |
| 563 | echo "a readme worth approving" > docs/readme.md |
| 564 | git commit --quiet -am "a readme worth approving" |
| 565 | git push --quiet origin topic/five |
| 566 | stalehead="$(git rev-parse topic/five)" |
| 567 | staledigest="$(attest digest --rev topic/five | cut -d: -f2)" |
| 568 | |
| 569 | printf 'a\n' > "$w/answers" |
| 570 | valley review topic/five < "$w/answers" > "$w/five-first.out" 2> "$w/five-first.err" |
| 571 | grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$stalehead")" "$w/five-first.out" |
| 572 | |
| 573 | # main moves, and the change is rebased onto it — the operator's half of |
| 574 | # approving again, which [a]pprove does not do and does not need to. |
| 575 | git checkout --quiet main |
| 576 | echo "a second note" > docs/second.md |
| 577 | git add -A |
| 578 | git commit --quiet -m "a landing under the same class" |
| 579 | git push --quiet origin main |
| 580 | git checkout --quiet topic/five |
| 581 | git rebase --quiet main |
| 582 | git push --quiet --force-with-lease origin topic/five |
| 583 | newhead="$(git rev-parse topic/five)" |
| 584 | newdigest="$(attest digest --rev topic/five | cut -d: -f2)" |
| 585 | |
| 586 | # The premise of the scenario, asserted rather than assumed: the rebase |
| 587 | # produced a head off the old one's line and a tree the old evidence is |
| 588 | # not about. Without both, the push below would be an ordinary one and |
| 589 | # would prove nothing. |
| 590 | if git merge-base --is-ancestor "$stalehead" "$newhead"; then |
| 591 | echo "valley-request: the rebased head still descends from the old one" >&2 |
| 592 | exit 1 |
| 593 | fi |
| 594 | if [ "$staledigest" = "$newdigest" ]; then |
| 595 | echo "valley-request: the rebase left the tree the old evidence is about" >&2 |
| 596 | exit 1 |
| 597 | fi |
| 598 | |
| 599 | : > "$pushlog" |
| 600 | printf 'a\n' > "$w/answers" |
| 601 | valley review topic/five < "$w/answers" > "$w/five-again.out" 2> "$w/five-again.err" |
| 602 | |
| 603 | grep -q '^Approved topic/five .*: its integration request for origin/main is filed\.$' "$w/five-again.out" |
| 604 | grep -qx " request refs/the-valley/integration-requests/main/topic-five -> $(git rev-parse --short "$newhead")" "$w/five-again.out" |
| 605 | # Replacing a pending request is said out loud, and the sentence names the |
| 606 | # head that was replaced. |
| 607 | grep -qx " replaced the pending request, which stood at $(git rev-parse --short "$stalehead") (a readme worth approving)" "$w/five-again.out" |
| 608 | |
| 609 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-five)" \ |
| 610 | != "$newhead" ]; then |
| 611 | echo "valley-request: the request filed again does not name the rebased head" >&2 |
| 612 | exit 1 |
| 613 | fi |
| 614 | |
| 615 | # Both attestations stand. The old one is about a tree that will never |
| 616 | # land, but the namespace is create-only and a record that can be dropped |
| 617 | # is not one; the second approval publishes beside it, never over it. |
| 618 | for stands in "refs/the-valley/attestations/$staledigest/$keyhash" \ |
| 619 | "refs/the-valley/attestations/$newdigest/$keyhash"; do |
| 620 | git -C "$origin" rev-parse --verify --quiet "$stands" > /dev/null || { |
| 621 | echo "valley-request: $stands is not on origin after the second approval" >&2 |
| 622 | exit 1 |
| 623 | } |
| 624 | done |
| 625 | |
| 626 | # One push again, and the request ref moved off the old head inside it. |
| 627 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 628 | if [ "$pushes" != 1 ]; then |
| 629 | echo "valley-request: the second approval took $pushes pushes, not 1" >&2 |
| 630 | cat "$pushlog" >&2 |
| 631 | exit 1 |
| 632 | fi |
| 633 | grep -qx "$stalehead $newhead refs/the-valley/integration-requests/main/topic-five" "$pushlog" |
| 634 | # And every attestation ref in that push is a creation: an all-zero old id |
| 635 | # is what the create-only namespace admits, and an approval that |
| 636 | # updated one would be refused by the real hook. |
| 637 | if grep '^[0-9a-f]* [0-9a-f]* refs/the-valley/attestations/' "$pushlog" \ |
| 638 | | grep -qv '^0* '; then |
| 639 | echo "valley-request: the second approval updated an attestation ref" >&2 |
| 640 | cat "$pushlog" >&2 |
| 641 | exit 1 |
| 642 | fi |
| 643 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$(git rev-parse main)" ]; then |
| 644 | echo "valley-request: the second approval moved main" >&2 |
| 645 | exit 1 |
| 646 | fi |
| 647 | |
| 648 | # ---------------------------------------------------------------------- |
| 649 | # 11. Two approvals of one change race for one ref, and the loser |
| 650 | # loses visibly. The request ref's value is read before the push and |
| 651 | # the push is leased against exactly that value, so the second one to |
| 652 | # arrive is refused rather than allowed to overwrite the first. The |
| 653 | # race is made real: a receive-pack wrapper moves the ref on origin |
| 654 | # after approve has read it and before the push it then makes, which is |
| 655 | # what another operator getting there first looks like from here. |
| 656 | git checkout --quiet main |
| 657 | git checkout --quiet -b topic/six origin/main |
| 658 | echo "a readme two approvals both rebased" > docs/readme.md |
| 659 | git commit --quiet -am "a readme two approvals both rebased" |
| 660 | git push --quiet origin topic/six |
| 661 | sixfiled="$(git rev-parse topic/six)" |
| 662 | |
| 663 | printf 'a\n' > "$w/answers" |
| 664 | valley review topic/six < "$w/answers" > "$w/six-first.out" 2> "$w/six-first.err" |
| 665 | grep -q '^Approved topic/six .*: its integration request for origin/main is filed\.$' "$w/six-first.out" |
| 666 | |
| 667 | git checkout --quiet main |
| 668 | echo "a third note" > docs/third.md |
| 669 | git add -A |
| 670 | git commit --quiet -m "another landing under the same class" |
| 671 | git push --quiet origin main |
| 672 | git checkout --quiet topic/six |
| 673 | git rebase --quiet main |
| 674 | git push --quiet --force-with-lease origin topic/six |
| 675 | sixhead="$(git rev-parse topic/six)" |
| 676 | sixdigest="$(attest digest --rev topic/six | cut -d: -f2)" |
| 677 | |
| 678 | # The other approval's head, built where origin can already reach |
| 679 | # every object in it: the same tree, parented on the same tip, so it is |
| 680 | # the commit a second operator rebasing this change would have produced. |
| 681 | racer="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/six^{tree}')" \ |
| 682 | -p "$(git rev-parse origin/main)" -m "the other approval")" |
| 683 | |
| 684 | arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-six '$racer'" |
| 685 | |
| 686 | : > "$pushlog" |
| 687 | printf 'a\ns\n' > "$w/answers" |
| 688 | valley review topic/six < "$w/answers" > "$w/race.out" 2> "$w/race.err" |
| 689 | disarm_race |
| 690 | |
| 691 | # A refusal, named as one, and the review loop still usable after it. |
| 692 | grep -q "the pending request moved from $(git rev-parse --short "$sixfiled") to $(git rev-parse --short "$racer") while this one was being prepared" "$w/race.err" |
| 693 | grep -q 'the lease on refs/the-valley/integration-requests/main/topic-six refused' "$w/race.err" |
| 694 | grep -q 'skipped topic/six; nothing done' "$w/race.out" |
| 695 | |
| 696 | # Nothing published. The winner's request stands untouched, the loser's |
| 697 | # evidence never reached origin, and origin saw no push at all — the lease |
| 698 | # refuses before any command is sent, which is what --atomic is worth. |
| 699 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-six)" \ |
| 700 | != "$racer" ]; then |
| 701 | echo "valley-request: the lease did not hold; the other approval was clobbered" >&2 |
| 702 | exit 1 |
| 703 | fi |
| 704 | if git -C "$origin" rev-parse --verify --quiet \ |
| 705 | "refs/the-valley/attestations/$sixdigest/$keyhash" > /dev/null; then |
| 706 | echo "valley-request: an approval that was refused published its evidence" >&2 |
| 707 | exit 1 |
| 708 | fi |
| 709 | if [ -s "$pushlog" ]; then |
| 710 | echo "valley-request: an approval that was refused reached origin's hook" >&2 |
| 711 | cat "$pushlog" >&2 |
| 712 | exit 1 |
| 713 | fi |
| 714 | |
| 715 | # The checks that were run are not lost with the refusal: their notes are |
| 716 | # in the operator's own repository, and the refusal says where. |
| 717 | git rev-parse --verify --quiet "refs/the-valley/attestations/$sixdigest/$keyhash" \ |
| 718 | > /dev/null || { |
| 719 | echo "valley-request: the refused approval kept no evidence locally" >&2 |
| 720 | exit 1 |
| 721 | } |
| 722 | grep -qx 'valley: the evidence is stored locally at:' "$w/race.err" |
| 723 | grep -qx " refs/the-valley/attestations/$sixdigest/$keyhash" "$w/race.err" |
| 724 | git checkout --quiet main |
| 725 | |
| 726 | # ---------------------------------------------------------------------- |
| 727 | # 12. Approving again at the same head. Evidence with a validity window |
| 728 | # expires while the tree stays the right tree, and the answer to that |
| 729 | # is a fresh observation over the same head. The request ref already |
| 730 | # names that head, so it is left out of the refspec entirely rather |
| 731 | # than pushed at the value origin already holds. |
| 732 | # |
| 733 | # The request is filed by hand here. Origin holding a request at this |
| 734 | # head is the whole of the state under test, and re-running [a]pprove to |
| 735 | # produce it would put a second-granular observation instant in the |
| 736 | # note: the same tree re-attested inside one second is the identical |
| 737 | # ref, and across a second boundary it is not. |
| 738 | git checkout --quiet -b topic/seven origin/main |
| 739 | echo "a readme approved twice" > docs/readme.md |
| 740 | git commit --quiet -am "a readme approved twice" |
| 741 | git push --quiet origin topic/seven |
| 742 | sevenhead="$(git rev-parse topic/seven)" |
| 743 | sevendigest="$(attest digest --rev topic/seven | cut -d: -f2)" |
| 744 | git push --quiet origin \ |
| 745 | "$sevenhead:refs/the-valley/integration-requests/main/topic-seven" |
| 746 | |
| 747 | : > "$pushlog" |
| 748 | printf 'a\n' > "$w/answers" |
| 749 | valley review topic/seven < "$w/answers" > "$w/seven.out" 2> "$w/seven.err" |
| 750 | |
| 751 | grep -q '^Approved topic/seven .*: its integration request for origin/main is filed\.$' "$w/seven.out" |
| 752 | grep -qx ' the request already stood at this head; the evidence is what was published' "$w/seven.out" |
| 753 | # The evidence went; the request ref was not in the refspec at all. |
| 754 | grep -q "refs/the-valley/attestations/$sevendigest/$keyhash" "$pushlog" |
| 755 | if grep -q 'refs/the-valley/integration-requests/main/topic-seven' "$pushlog"; then |
| 756 | echo "valley-request: a same-head re-approval pushed the request ref again" >&2 |
| 757 | cat "$pushlog" >&2 |
| 758 | exit 1 |
| 759 | fi |
| 760 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-seven)" \ |
| 761 | != "$sevenhead" ]; then |
| 762 | echo "valley-request: a same-head re-approval disturbed the request ref" >&2 |
| 763 | exit 1 |
| 764 | fi |
| 765 | |
| 766 | # The same head with nothing required: no evidence to publish and no ref to |
| 767 | # write, so no push is made at all. topic/quiet's request has stood on |
| 768 | # origin at this head since 5. A push assembled with an empty refspec |
| 769 | # would fall back to origin's default one, and pushing whatever that |
| 770 | # happens to name is not what approving a change means. |
| 771 | git checkout --quiet main |
| 772 | : > "$pushlog" |
| 773 | printf 'a\n' > "$w/answers" |
| 774 | env -u VALLEY_ATTEST_KEY valley review topic/quiet < "$w/answers" \ |
| 775 | > "$w/quiet-again.out" 2> "$w/quiet-again.err" |
| 776 | grep -qx " request refs/the-valley/integration-requests/main/topic-quiet -> $quiethead" "$w/quiet-again.out" |
| 777 | grep -qx ' the request already stood at this head and nothing was required, so nothing needed publishing' "$w/quiet-again.out" |
| 778 | if [ -s "$pushlog" ]; then |
| 779 | echo "valley-request: a re-approval with nothing to publish still pushed" >&2 |
| 780 | cat "$pushlog" >&2 |
| 781 | exit 1 |
| 782 | fi |
| 783 | |
| 784 | # ---------------------------------------------------------------------- |
| 785 | # 13. Two operators filing one change at once. Reading that the request |
| 786 | # ref is absent is a value like any other, and the lease covers it: |
| 787 | # git spells that expectation with an empty value and refuses if the |
| 788 | # ref exists at all. Unleased, this push would have fast-forwarded the |
| 789 | # other filing away without saying a word. |
| 790 | git checkout --quiet -b topic/eight origin/main |
| 791 | echo "a readme two operators both filed" > docs/readme.md |
| 792 | git commit --quiet -am "a readme two operators both filed" |
| 793 | git push --quiet origin topic/eight |
| 794 | eighthead="$(git rev-parse topic/eight)" |
| 795 | eightdigest="$(attest digest --rev topic/eight | cut -d: -f2)" |
| 796 | # The other operator's filing: this same tree on this same tip, which is |
| 797 | # what a second checkout of one branch produces. It is a descendant of |
| 798 | # nothing here, so an unleased push over it would be a plain fast-forward. |
| 799 | firstfiler="$(git -C "$origin" commit-tree "$(git rev-parse 'topic/eight^{tree}')" \ |
| 800 | -p "$(git rev-parse origin/main)" -m "the other operator's filing")" |
| 801 | |
| 802 | arm_race "git -C '$origin' update-ref refs/the-valley/integration-requests/main/topic-eight '$firstfiler'" |
| 803 | : > "$pushlog" |
| 804 | printf 'a\ns\n' > "$w/answers" |
| 805 | valley review topic/eight < "$w/answers" > "$w/create-race.out" 2> "$w/create-race.err" |
| 806 | disarm_race |
| 807 | |
| 808 | # The other operator's commit is on origin and not here, so the subject |
| 809 | # says so rather than inventing one. Scenario 10 pins the case where the |
| 810 | # replaced head IS local and its subject is printed. |
| 811 | grep -q "a request for this change appeared while this one was being prepared, at $(git rev-parse --short "$firstfiler") (subject not in this repository)" "$w/create-race.err" |
| 812 | grep -q 'skipped topic/eight; nothing done' "$w/create-race.out" |
| 813 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-eight)" \ |
| 814 | != "$firstfiler" ]; then |
| 815 | echo "valley-request: an unleased filing clobbered the request that got there first" >&2 |
| 816 | exit 1 |
| 817 | fi |
| 818 | if git -C "$origin" rev-parse --verify --quiet \ |
| 819 | "refs/the-valley/attestations/$eightdigest/$keyhash" > /dev/null; then |
| 820 | echo "valley-request: a refused filing published its evidence" >&2 |
| 821 | exit 1 |
| 822 | fi |
| 823 | if [ -s "$pushlog" ]; then |
| 824 | echo "valley-request: a refused filing reached origin's hook" >&2 |
| 825 | cat "$pushlog" >&2 |
| 826 | exit 1 |
| 827 | fi |
| 828 | |
| 829 | # ---------------------------------------------------------------------- |
| 830 | # 14. A lease that refuses because the ref is gone, not because it moved. |
| 831 | # The integrator consumes the request ref when the change lands, so an |
| 832 | # approval prepared just before that lands into a lease against a |
| 833 | # ref that no longer exists. Git reports it as stale info either way, |
| 834 | # and only reading the ref again tells the two apart — so the two are |
| 835 | # told apart, and the operator is pointed at the landing rather than |
| 836 | # at a race that did not happen. |
| 837 | git checkout --quiet main |
| 838 | git checkout --quiet -b topic/nine origin/main |
| 839 | echo "a readme that lands mid-approval" > docs/readme.md |
| 840 | git commit --quiet -am "a readme that lands mid-approval" |
| 841 | git push --quiet origin topic/nine |
| 842 | |
| 843 | printf 'a\n' > "$w/answers" |
| 844 | valley review topic/nine < "$w/answers" > "$w/nine-first.out" 2> "$w/nine-first.err" |
| 845 | grep -q '^Approved topic/nine .*: its integration request for origin/main is filed\.$' "$w/nine-first.out" |
| 846 | |
| 847 | git checkout --quiet main |
| 848 | echo "a fourth note" > docs/fourth.md |
| 849 | git add -A |
| 850 | git commit --quiet -m "a landing that moves main again" |
| 851 | git push --quiet origin main |
| 852 | git checkout --quiet topic/nine |
| 853 | git rebase --quiet main |
| 854 | git push --quiet --force-with-lease origin topic/nine |
| 855 | ninedigest="$(attest digest --rev topic/nine | cut -d: -f2)" |
| 856 | |
| 857 | arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-nine" |
| 858 | : > "$pushlog" |
| 859 | printf 'a\ns\n' > "$w/answers" |
| 860 | valley review topic/nine < "$w/answers" > "$w/consumed.out" 2> "$w/consumed.err" |
| 861 | disarm_race |
| 862 | |
| 863 | grep -q 'the pending request was consumed while this one was being prepared, which is what the integrator does when a change lands' "$w/consumed.err" |
| 864 | grep -q 'nothing was published; fetch and review the branch again before approving it a second time' "$w/consumed.err" |
| 865 | # Not reported as a race, which is the distinction the re-read buys. |
| 866 | if grep -q 'the pending request moved from' "$w/consumed.err"; then |
| 867 | echo "valley-request: a consumed request was reported as a lost race" >&2 |
| 868 | cat "$w/consumed.err" >&2 |
| 869 | exit 1 |
| 870 | fi |
| 871 | grep -q 'skipped topic/nine; nothing done' "$w/consumed.out" |
| 872 | if git -C "$origin" rev-parse --verify --quiet \ |
| 873 | refs/the-valley/integration-requests/main/topic-nine > /dev/null; then |
| 874 | echo "valley-request: an approval that was refused re-created the consumed request" >&2 |
| 875 | exit 1 |
| 876 | fi |
| 877 | if git -C "$origin" rev-parse --verify --quiet \ |
| 878 | "refs/the-valley/attestations/$ninedigest/$keyhash" > /dev/null; then |
| 879 | echo "valley-request: an approval that was refused published its evidence" >&2 |
| 880 | exit 1 |
| 881 | fi |
| 882 | if [ -s "$pushlog" ]; then |
| 883 | echo "valley-request: an approval that was refused reached origin's hook" >&2 |
| 884 | cat "$pushlog" >&2 |
| 885 | exit 1 |
| 886 | fi |
| 887 | |
| 888 | # ---------------------------------------------------------------------- |
| 889 | # 15. The same-head push carries no request ref, so it carries no |
| 890 | # compare-and-swap on one either — git leaves an up-to-date ref out of |
| 891 | # what it sends. Where the ref stands after that push is therefore not |
| 892 | # something the run established, and it is read again rather than |
| 893 | # asserted from the read before it. |
| 894 | # |
| 895 | # Nothing re-files it. A request for a change already in the stream is |
| 896 | # judged against an empty delta on every pass, which is why the |
| 897 | # integrator consumes the ref in the first place; re-creating it here |
| 898 | # would be a guess about which of consumption and deletion happened, |
| 899 | # paid for with a second push behind the one this verb promises. So |
| 900 | # the run says what is true and stops, and approving again is the repair. |
| 901 | git checkout --quiet main |
| 902 | git checkout --quiet -b topic/ten origin/main |
| 903 | echo "a readme consumed under the evidence" > docs/readme.md |
| 904 | git commit --quiet -am "a readme consumed under the evidence" |
| 905 | git push --quiet origin topic/ten |
| 906 | tenhead="$(git rev-parse topic/ten)" |
| 907 | tendigest="$(attest digest --rev topic/ten | cut -d: -f2)" |
| 908 | git push --quiet origin \ |
| 909 | "$tenhead:refs/the-valley/integration-requests/main/topic-ten" |
| 910 | |
| 911 | arm_race "git -C '$origin' update-ref -d refs/the-valley/integration-requests/main/topic-ten" |
| 912 | : > "$pushlog" |
| 913 | printf 'a\ns\n' > "$w/answers" |
| 914 | valley review topic/ten < "$w/answers" > "$w/posthoc.out" 2> "$w/posthoc.err" |
| 915 | disarm_race |
| 916 | |
| 917 | grep -q 'the request was consumed while this run was working, which is what the integrator does when a change lands' "$w/posthoc.err" |
| 918 | grep -q 'Nothing here re-filed it' "$w/posthoc.err" |
| 919 | # The claim the old code would have made, from a read that had gone out of |
| 920 | # date under it. |
| 921 | if grep -q 'the request already stood at this head' "$w/posthoc.out"; then |
| 922 | echo "valley-request: a consumed request was reported as still standing" >&2 |
| 923 | cat "$w/posthoc.out" >&2 |
| 924 | exit 1 |
| 925 | fi |
| 926 | grep -q 'skipped topic/ten; nothing done' "$w/posthoc.out" |
| 927 | |
| 928 | # The evidence went, because that push succeeded; the request did not come |
| 929 | # back, because nothing here writes it. |
| 930 | git -C "$origin" rev-parse --verify --quiet \ |
| 931 | "refs/the-valley/attestations/$tendigest/$keyhash" > /dev/null || { |
| 932 | echo "valley-request: the evidence push was reported as done and is not on origin" >&2 |
| 933 | exit 1 |
| 934 | } |
| 935 | if git -C "$origin" rev-parse --verify --quiet \ |
| 936 | refs/the-valley/integration-requests/main/topic-ten > /dev/null; then |
| 937 | echo "valley-request: the consumed request was re-filed behind the operator" >&2 |
| 938 | exit 1 |
| 939 | fi |
| 940 | # And that evidence went in one push, which is still the whole claim. |
| 941 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 942 | if [ "$pushes" != 1 ]; then |
| 943 | echo "valley-request: the same-head re-approval took $pushes pushes, not 1" >&2 |
| 944 | cat "$pushlog" >&2 |
| 945 | exit 1 |
| 946 | fi |
| 947 | git checkout --quiet main |
| 948 | |
| 949 | # ---------------------------------------------------------------------- |
| 950 | # 16. A project other than the-valley carries no schema/. The schema ships |
| 951 | # with the tool, so the packaged CLI derives what a change requires |
| 952 | # and files the request from a checkout that has none. This is the case |
| 953 | # that failed in live use: [a]pprove in a valley's config repository, which |
| 954 | # carries its own floor at policy/instance, refused with "no policy |
| 955 | # schema" and published nothing. |
| 956 | cfgorigin="$TMPDIR/config.git" |
| 957 | git init --quiet --bare "$cfgorigin" |
| 958 | cfg="$TMPDIR/config" |
| 959 | git init --quiet "$cfg" |
| 960 | cd "$cfg" || exit 1 |
| 961 | git remote add origin "$cfgorigin" |
| 962 | mkdir -p docs policy/instance |
| 963 | cp "$valleywork/policy/instance/floor.cue" policy/instance/floor.cue |
| 964 | echo "the readme" > docs/readme.md |
| 965 | git add -A |
| 966 | git commit --quiet -m base |
| 967 | git push --quiet -u origin main |
| 968 | git checkout --quiet -b topic/config |
| 969 | echo "a config readme" > docs/readme.md |
| 970 | git commit --quiet -am "a config readme" |
| 971 | git push --quiet origin topic/config |
| 972 | cfgdigest="$(attest digest --rev topic/config | cut -d: -f2)" |
| 973 | |
| 974 | printf 'a\n' > "$w/answers" |
| 975 | valley review topic/config < "$w/answers" > "$w/config.out" 2> "$w/config.err" |
| 976 | if grep -q 'no policy schema' "$w/config.err"; then |
| 977 | echo "valley-request: [a]pprove looked for the schema in a project's checkout" >&2 |
| 978 | cat "$w/config.err" >&2 |
| 979 | exit 1 |
| 980 | fi |
| 981 | grep -q '^checking topic/config at .*: tree-ok$' "$w/config.out" |
| 982 | grep -qx "Approved topic/config ($(git rev-parse --short topic/config)): its integration request for origin/main is filed." "$w/config.out" |
| 983 | git -C "$cfgorigin" rev-parse --verify --quiet \ |
| 984 | "refs/the-valley/attestations/$cfgdigest/$keyhash" > /dev/null || { |
| 985 | echo "valley-request: [a]pprove in a project without schema/ published no evidence" >&2 |
| 986 | exit 1 |
| 987 | } |
| 988 | if [ "$(git -C "$cfgorigin" rev-parse refs/the-valley/integration-requests/main/topic-config)" \ |
| 989 | != "$(git rev-parse topic/config)" ]; then |
| 990 | echo "valley-request: [a]pprove in a project without schema/ filed no request for its head" >&2 |
| 991 | exit 1 |
| 992 | fi |
| 993 | |
| 994 | # checks without --schema reads the packaged schema too, and derives what |
| 995 | # [a]pprove ran from this repository's own floor. |
| 996 | valley checks > "$w/config-checks.out" 2> "$w/config-checks.err" |
| 997 | grep -qF '(this repository is its own valley)' "$w/config-checks.out" |
| 998 | grep -qE '^ tree-ok +mandatory +prose$' "$w/config-checks.out" |
| 999 | |
| 1000 | # The packaged schema is a default and not a pin. A schema named in the |
| 1001 | # environment is the one read, and --schema overrides both. |
| 1002 | if VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks \ |
| 1003 | > "$w/envschema.out" 2> "$w/envschema.err"; then |
| 1004 | echo "valley-request: VALLEY_VERIFICATION_SCHEMA did not replace the packaged schema" >&2 |
| 1005 | exit 1 |
| 1006 | fi |
| 1007 | grep -qF "no policy schema at $TMPDIR/no-such-schema.cue" "$w/envschema.err" |
| 1008 | VALLEY_VERIFICATION_SCHEMA="$TMPDIR/no-such-schema.cue" valley checks --schema "$schemaFile" \ |
| 1009 | > "$w/flagschema.out" 2> "$w/flagschema.err" |
| 1010 | grep -qE '^ tree-ok +mandatory +prose$' "$w/flagschema.out" |
| 1011 | |
| 1012 | # bin/valley run straight from a checkout, with nothing setting the |
| 1013 | # variable, reads schema/verification.cue in that checkout. the-valley's own |
| 1014 | # tree carries one, as the first scratch project does, and every other |
| 1015 | # project's tree does not. |
| 1016 | if env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks \ |
| 1017 | > "$w/bare-config.out" 2> "$w/bare-config.err"; then |
| 1018 | echo "valley-request: bin/valley run bare found a schema in a checkout without one" >&2 |
| 1019 | exit 1 |
| 1020 | fi |
| 1021 | grep -qF "no policy schema at $(git rev-parse --show-toplevel)/schema/verification.cue" \ |
| 1022 | "$w/bare-config.err" |
| 1023 | cd "$repo" || exit 1 |
| 1024 | env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "$first" topic/one \ |
| 1025 | > "$w/bare.out" 2> "$w/bare.err" |
| 1026 | grep -qE '^ tree-ok +mandatory +prose$' "$w/bare.out" |
| 1027 | |
| 1028 | # Every git call the default derivation makes fails closed: finding the |
| 1029 | # target, reading both layers out of its tip, reading the pointer, |
| 1030 | # fetching the owning valley, and the diff. fail-nth fails them one at a |
| 1031 | # time, and each run must end non-zero, say so, and print nothing on |
| 1032 | # stdout. The layer reads matter most here: a listing of policy/project |
| 1033 | # that failed, read as an empty directory, would derive under the floor |
| 1034 | # alone, and a read of origin/main that failed, read as no such ref, |
| 1035 | # would derive from the local main. |
| 1036 | shims="$TMPDIR/shims" |
| 1037 | mkdir -p "$shims" |
| 1038 | ln -s "$failNth" "$shims/git" |
| 1039 | shimmed_checks() { |
| 1040 | echo 0 > "$TMPDIR/shim-count" |
| 1041 | env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \ |
| 1042 | SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \ |
| 1043 | PATH="$shims:$PATH" bash "$cliScript" checks "$first" topic/one |
| 1044 | } |
| 1045 | shimmed_checks 0 > "$w/shim-want.out" |
| 1046 | grep -qE '^ tree-ok +mandatory +prose$' "$w/shim-want.out" |
| 1047 | calls="$(cat "$TMPDIR/shim-count")" |
| 1048 | [ "$calls" -gt 0 ] || { |
| 1049 | echo "valley-request: the derivation never called git, so failing it proves nothing" >&2 |
| 1050 | exit 1 |
| 1051 | } |
| 1052 | for ((at = 1; at <= calls; at++)); do |
| 1053 | if shimmed_checks "$at" > "$w/shim.out" 2> "$w/shim.err" || [ -s "$w/shim.out" ] \ |
| 1054 | || ! grep -q '^valley: ' "$w/shim.err"; then |
| 1055 | echo "valley-request: git call $at of $calls failed, and the deriver did not refuse cleanly" >&2 |
| 1056 | cat "$w/shim.out" "$w/shim.err" >&2 |
| 1057 | exit 1 |
| 1058 | fi |
| 1059 | done |
| 1060 | |
| 1061 | # The same, for the derivation that resolves its own base: `merge-base` is |
| 1062 | # a call the explicit-base runs above never make, and --names is a second |
| 1063 | # way of printing what the derivation found. A run is shimmed for each |
| 1064 | # call, with the arguments the caller gives. |
| 1065 | defaulted_checks() { |
| 1066 | echo 0 > "$TMPDIR/shim-count" |
| 1067 | env -u VALLEY_VERIFICATION_SCHEMA SHIM_REAL_PATH="$PATH" SHIM_TOOL=git \ |
| 1068 | SHIM_FAIL_AT="$1" SHIM_STATUS=128 SHIM_COUNT="$TMPDIR/shim-count" \ |
| 1069 | PATH="$shims:$PATH" bash "$cliScript" checks "${@:2}" |
| 1070 | } |
| 1071 | git checkout --quiet topic/one |
| 1072 | for mode in report names; do |
| 1073 | margs=() |
| 1074 | [ "$mode" != names ] || margs=(--names) |
| 1075 | defaulted_checks 0 "${margs[@]}" > "$w/dshim-want.out" |
| 1076 | [ -s "$w/dshim-want.out" ] || { |
| 1077 | echo "valley-request: default resolution ($mode) printed nothing" >&2 |
| 1078 | exit 1 |
| 1079 | } |
| 1080 | calls="$(cat "$TMPDIR/shim-count")" |
| 1081 | sawmb=0 |
| 1082 | for ((at = 1; at <= calls; at++)); do |
| 1083 | if defaulted_checks "$at" "${margs[@]}" > "$w/dshim.out" 2> "$w/dshim.err" \ |
| 1084 | || [ -s "$w/dshim.out" ] || ! grep -q '^valley: ' "$w/dshim.err"; then |
| 1085 | echo "valley-request: default resolution ($mode): git call $at of $calls failed, and the deriver did not refuse cleanly" >&2 |
| 1086 | cat "$w/dshim.out" "$w/dshim.err" >&2 |
| 1087 | exit 1 |
| 1088 | fi |
| 1089 | done |
| 1090 | done |
| 1091 | git checkout --quiet - |
| 1092 | |
| 1093 | # A write that fails is a failure of the derivation, not a shorter list. |
| 1094 | # diff_paths runs with errexit off, so its printf is the case: the function |
| 1095 | # is cut out of the script and given a full disk to write to. |
| 1096 | fn="$(sed -n '/^diff_paths() {/,/^}/p' "$cliScript")" |
| 1097 | [ -n "$fn" ] || { echo "valley-request: diff_paths not found in the script" >&2; exit 1; } |
| 1098 | if ( |
| 1099 | eval "$fn" |
| 1100 | printf 'M\0docs/a.md\0R100\0old\0new\0' | diff_paths > /dev/full |
| 1101 | ) 2> "$w/full.err"; then |
| 1102 | echo "valley-request: diff_paths returned 0 after its write failed" >&2 |
| 1103 | exit 1 |
| 1104 | fi |
| 1105 | # And end to end: with no file allowed to grow, every intermediate file the |
| 1106 | # derivation writes fails, and nothing may print. The pipe keeps the |
| 1107 | # refusal readable, since a file stream would be cut off too. |
| 1108 | for mode in report names; do |
| 1109 | margs=() |
| 1110 | [ "$mode" != names ] || margs=(--names) |
| 1111 | if wrote="$( |
| 1112 | ulimit -f 0 |
| 1113 | trap '' XFSZ |
| 1114 | env -u VALLEY_VERIFICATION_SCHEMA bash "$cliScript" checks "${margs[@]}" "$first" topic/one 2> /dev/null |
| 1115 | )" || [ -n "$wrote" ]; then |
| 1116 | echo "valley-request: a derivation with unwritable scratch files ($mode) did not fail cleanly" >&2 |
| 1117 | printf '%s\n' "$wrote" >&2 |
| 1118 | exit 1 |
| 1119 | fi |
| 1120 | done |
| 1121 | |
| 1122 | # ---------------------------------------------------------------------- |
| 1123 | # 17. The approve verb: [a]pprove without the pager, for a front end that |
| 1124 | # has shown the change its own way. It runs the function the key |
| 1125 | # runs, so it publishes what the key's session in 1 published, the |
| 1126 | # evidence and the request in one push, and prints what that session |
| 1127 | # printed. stdin is closed, because no answer is read, and the change |
| 1128 | # is named the way status accepts it, with origin/ in front. |
| 1129 | git checkout --quiet -b topic/verb origin/main |
| 1130 | echo "a readme approved without the pager" > docs/readme.md |
| 1131 | git commit --quiet -am "a readme approved without the pager" |
| 1132 | git push --quiet origin topic/verb |
| 1133 | verbhead="$(git rev-parse topic/verb)" |
| 1134 | verbdigest="$(attest digest --rev topic/verb | cut -d: -f2)" |
| 1135 | git checkout --quiet main |
| 1136 | |
| 1137 | : > "$pushlog" |
| 1138 | valley approve origin/topic/verb < /dev/null > "$w/verb.out" 2> "$w/verb.err" |
| 1139 | grep -q '^checking topic/verb at .*: tree-ok$' "$w/verb.out" |
| 1140 | grep -qx "Approved topic/verb ($(git rev-parse --short "$verbhead")): its integration request for origin/main is filed." "$w/verb.out" |
| 1141 | grep -qx " request refs/the-valley/integration-requests/main/topic-verb -> $(git rev-parse --short "$verbhead")" "$w/verb.out" |
| 1142 | grep -qx " evidence refs/the-valley/attestations/$verbdigest/$keyhash" "$w/verb.out" |
| 1143 | grep -qx ' checks tree-ok' "$w/verb.out" |
| 1144 | grep -qx 'Check the result: valley status topic/verb' "$w/verb.out" |
| 1145 | if grep -q '\[[a-z]\]' "$w/verb.out"; then |
| 1146 | echo "valley-request: the approve verb offered a prompt" >&2 |
| 1147 | cat "$w/verb.out" >&2 |
| 1148 | exit 1 |
| 1149 | fi |
| 1150 | if [ "$(git -C "$origin" rev-parse refs/the-valley/integration-requests/main/topic-verb)" \ |
| 1151 | != "$verbhead" ]; then |
| 1152 | echo "valley-request: the approve verb's request does not name the branch's head" >&2 |
| 1153 | exit 1 |
| 1154 | fi |
| 1155 | git -C "$origin" rev-parse --verify --quiet \ |
| 1156 | "refs/the-valley/attestations/$verbdigest/$keyhash" > /dev/null || { |
| 1157 | echo "valley-request: the approve verb published no evidence" >&2 |
| 1158 | exit 1 |
| 1159 | } |
| 1160 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1161 | if [ "$pushes" != 1 ]; then |
| 1162 | echo "valley-request: the approve verb took $pushes pushes, not 1" >&2 |
| 1163 | cat "$pushlog" >&2 |
| 1164 | exit 1 |
| 1165 | fi |
| 1166 | |
| 1167 | # A refusal is the verb's exit status, after the same words the key's |
| 1168 | # refusal prints, and it publishes nothing. |
| 1169 | : > "$pushlog" |
| 1170 | if valley approve topic/two < /dev/null > "$w/verb-refused.out" 2> "$w/verb-refused.err"; then |
| 1171 | echo "valley-request: the approve verb exited 0 for a change whose check failed" >&2 |
| 1172 | exit 1 |
| 1173 | fi |
| 1174 | grep -q 'the checks did not pass; nothing published, main untouched' "$w/verb-refused.err" |
| 1175 | if [ -s "$pushlog" ]; then |
| 1176 | echo "valley-request: a refused approve verb pushed something" >&2 |
| 1177 | cat "$pushlog" >&2 |
| 1178 | exit 1 |
| 1179 | fi |
| 1180 | if valley approve topic/no-such-change < /dev/null > "$w/verb-missing.out" 2> "$w/verb-missing.err"; then |
| 1181 | echo "valley-request: the approve verb exited 0 for a change origin does not have" >&2 |
| 1182 | exit 1 |
| 1183 | fi |
| 1184 | grep -q 'no such branch on origin: topic/no-such-change' "$w/verb-missing.err" |
| 1185 | |
| 1186 | # ---------------------------------------------------------------------- |
| 1187 | # 17b. Timing never stands in the way of approval. A FIFO nobody reads, |
| 1188 | # sitting at the timing log's path, would hold an open for writing |
| 1189 | # forever: the log is refused with a warning, and approval goes on. |
| 1190 | # With standard error a pipe nobody reads as well, the warning is |
| 1191 | # dropped rather than ending the run. Both runs keep a state |
| 1192 | # directory of their own, so the log the other cases read is not |
| 1193 | # touched. |
| 1194 | fifostate="$w/fifo-state" |
| 1195 | mkdir -p "$fifostate/valley" |
| 1196 | mkfifo "$fifostate/valley/timings.jsonl" |
| 1197 | approved_past_fifo() { |
| 1198 | if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/topic-$1")" \ |
| 1199 | != "$(git rev-parse "topic/$1")" ]; then |
| 1200 | echo "valley-request: approving past a FIFO at the timing log filed no request ($2)" >&2 |
| 1201 | exit 1 |
| 1202 | fi |
| 1203 | git -C "$origin" rev-parse --verify --quiet \ |
| 1204 | "refs/the-valley/attestations/$(attest digest --rev "topic/$1" | cut -d: -f2)/$keyhash" > /dev/null || { |
| 1205 | echo "valley-request: approving past a FIFO at the timing log published no evidence ($2)" >&2 |
| 1206 | exit 1 |
| 1207 | } |
| 1208 | } |
| 1209 | for branch in fifo unread; do |
| 1210 | git checkout --quiet -b "topic/$branch" origin/main |
| 1211 | echo "a readme approved past a FIFO, $branch" > docs/readme.md |
| 1212 | git commit --quiet -am "a readme approved past a FIFO, $branch" |
| 1213 | git push --quiet origin "topic/$branch" |
| 1214 | git checkout --quiet main |
| 1215 | done |
| 1216 | |
| 1217 | XDG_STATE_HOME="$fifostate" timeout 120 valley approve origin/topic/fifo \ |
| 1218 | < /dev/null > "$w/fifo.out" 2> "$w/fifo.err" || { |
| 1219 | echo "valley-request: approve did not finish past a FIFO at the timing log (exit $?):" >&2 |
| 1220 | cat "$w/fifo.out" "$w/fifo.err" >&2 |
| 1221 | exit 1 |
| 1222 | } |
| 1223 | grep -qE '^time tree-ok +passed +[0-9][0-9.hms]* \(ran\)$' "$w/fifo.out" |
| 1224 | grep -qx "attest: warning: timings not logged: $fifostate/valley/timings.jsonl is not a regular file" "$w/fifo.err" |
| 1225 | approved_past_fifo fifo "standard error read" |
| 1226 | |
| 1227 | # A pipe with no reader. The FIFO is opened to read and write, so opening it |
| 1228 | # to write does not wait; closing the first leaves the writer alone. |
| 1229 | mkfifo "$w/unread" |
| 1230 | exec 3<> "$w/unread" |
| 1231 | exec 4> "$w/unread" |
| 1232 | exec 3<&- |
| 1233 | status=0 |
| 1234 | XDG_STATE_HOME="$fifostate" timeout 120 valley approve origin/topic/unread \ |
| 1235 | < /dev/null > "$w/unread.out" 2>&4 || status=$? |
| 1236 | exec 4>&- |
| 1237 | if [ "$status" != 0 ]; then |
| 1238 | echo "valley-request: approve with nobody reading standard error exited $status:" >&2 |
| 1239 | cat "$w/unread.out" >&2 |
| 1240 | exit 1 |
| 1241 | fi |
| 1242 | approved_past_fifo unread "standard error unread" |
| 1243 | |
| 1244 | # ---------------------------------------------------------------------- |
| 1245 | # 18. The rebase verb: [b]ase without the pager. A stale branch is |
| 1246 | # rebased onto main in a throwaway worktree and pushed under a lease, |
| 1247 | # and the new head is printed. Nothing is approved and nothing is |
| 1248 | # re-reviewed: rebasing is mechanics, and the judgment on what it |
| 1249 | # produced stays with whoever reads it next. |
| 1250 | git checkout --quiet -b topic/behind "$first" |
| 1251 | echo "a readme left behind" > docs/behind.md |
| 1252 | git add -A |
| 1253 | git commit --quiet -m "a readme left behind" |
| 1254 | git push --quiet origin topic/behind |
| 1255 | behindhead="$(git rev-parse topic/behind)" |
| 1256 | git checkout --quiet main |
| 1257 | if git -C "$origin" merge-base --is-ancestor refs/heads/main refs/heads/topic/behind; then |
| 1258 | echo "valley-request: the branch to rebase fast-forwarded from main already" >&2 |
| 1259 | exit 1 |
| 1260 | fi |
| 1261 | before_rebase="$(git -C "$origin" rev-parse refs/heads/main)" |
| 1262 | |
| 1263 | : > "$pushlog" |
| 1264 | valley rebase topic/behind < /dev/null > "$w/rebase.out" 2> "$w/rebase.err" |
| 1265 | rebased="$(git -C "$origin" rev-parse refs/heads/topic/behind)" |
| 1266 | grep -qx 'rebased topic/behind onto origin/main, pushed with --force-with-lease' "$w/rebase.out" |
| 1267 | grep -qx " head $rebased" "$w/rebase.out" |
| 1268 | if [ "$rebased" = "$behindhead" ]; then |
| 1269 | echo "valley-request: the rebase verb left the branch where it was" >&2 |
| 1270 | exit 1 |
| 1271 | fi |
| 1272 | git -C "$origin" merge-base --is-ancestor refs/heads/main "$rebased" || { |
| 1273 | echo "valley-request: the rebased head does not fast-forward from main" >&2 |
| 1274 | exit 1 |
| 1275 | } |
| 1276 | if [ "$(git -C "$origin" rev-parse refs/heads/main)" != "$before_rebase" ]; then |
| 1277 | echo "valley-request: the rebase verb moved main" >&2 |
| 1278 | exit 1 |
| 1279 | fi |
| 1280 | if git -C "$origin" rev-parse --verify --quiet \ |
| 1281 | refs/the-valley/integration-requests/main/topic-behind > /dev/null; then |
| 1282 | echo "valley-request: the rebase verb filed a request" >&2 |
| 1283 | exit 1 |
| 1284 | fi |
| 1285 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1286 | if [ "$pushes" != 1 ] || grep -v '^--- push' "$pushlog" | grep -qv ' refs/heads/topic/behind$'; then |
| 1287 | echo "valley-request: the rebase verb pushed something besides the branch" >&2 |
| 1288 | cat "$pushlog" >&2 |
| 1289 | exit 1 |
| 1290 | fi |
| 1291 | |
| 1292 | # A branch that fast-forwards from main already is left as it is. |
| 1293 | : > "$pushlog" |
| 1294 | valley rebase topic/behind < /dev/null > "$w/rebase-again.out" 2> "$w/rebase-again.err" |
| 1295 | grep -qx 'topic/behind already fast-forwards from origin/main; nothing rebased' "$w/rebase-again.out" |
| 1296 | grep -qx " head $rebased" "$w/rebase-again.out" |
| 1297 | if [ -s "$pushlog" ]; then |
| 1298 | echo "valley-request: rebasing a branch that fast-forwards pushed something" >&2 |
| 1299 | cat "$pushlog" >&2 |
| 1300 | exit 1 |
| 1301 | fi |
| 1302 | |
| 1303 | # A rebase that conflicts changes nothing, says so, and exits non-zero. |
| 1304 | git checkout --quiet -b topic/clash "$first" |
| 1305 | echo "a note that clashes" > docs/notes.md |
| 1306 | git add -A |
| 1307 | git commit --quiet -m "a note that clashes" |
| 1308 | git push --quiet origin topic/clash |
| 1309 | clashhead="$(git rev-parse topic/clash)" |
| 1310 | git checkout --quiet main |
| 1311 | if valley rebase topic/clash < /dev/null > "$w/clash.out" 2> "$w/clash.err"; then |
| 1312 | echo "valley-request: a rebase that conflicts exited 0" >&2 |
| 1313 | exit 1 |
| 1314 | fi |
| 1315 | grep -q 'topic/clash conflicts with main — resolve by hand; nothing changed' "$w/clash.err" |
| 1316 | if [ "$(git -C "$origin" rev-parse refs/heads/topic/clash)" != "$clashhead" ]; then |
| 1317 | echo "valley-request: a rebase that conflicted moved the branch" >&2 |
| 1318 | exit 1 |
| 1319 | fi |
| 1320 | |
| 1321 | # ---------------------------------------------------------------------- |
| 1322 | # 19. A change whose version names no branch is approved at its head like |
| 1323 | # any other. The record allows a version with no branch, though valley |
| 1324 | # ready always names one, so the record here is written by hand. The |
| 1325 | # check it requires runs over the recorded head, and the evidence and the |
| 1326 | # c-<change ID> request reach origin in one push. |
| 1327 | git checkout --quiet -b branchless main |
| 1328 | echo "a readme no branch carries" > docs/readme.md |
| 1329 | git commit --quiet -am "a readme no branch carries" |
| 1330 | barehead="$(git rev-parse HEAD)" |
| 1331 | baredigest="$(attest digest --rev "$barehead" | cut -d: -f2)" |
| 1332 | git checkout --quiet main |
| 1333 | bareid=rrrrssssttttuuuuvvvvwwwwxxxxyyyy |
| 1334 | blob="$(printf '{"schema": "valley-change-record/v1", "change_id": "%s", "target": "main", "owner": "contributor", "state": "ready", "seq": 1, "versions": [{"n": 1, "head": "git-sha1:%s", "title": "no branch", "branch": "", "uploader": "contributor"}], "active": 1}\n' \ |
| 1335 | "$bareid" "$barehead" | git hash-object -w --stdin)" |
| 1336 | record="$(git commit-tree "$(printf '100644 blob %s\trecord.json\n' "$blob" | git mktree)" \ |
| 1337 | -p "$barehead" -m 'a change record')" |
| 1338 | git push --quiet origin "$record:refs/the-valley/changes/main/$bareid" |
| 1339 | |
| 1340 | : > "$pushlog" |
| 1341 | valley approve --head "$barehead" rrrr < /dev/null > "$w/branchless.out" 2> "$w/branchless.err" |
| 1342 | grep -q '^checking rrrr v1 at .*: tree-ok$' "$w/branchless.out" |
| 1343 | grep -qx " request refs/the-valley/integration-requests/main/c-$bareid -> $(git rev-parse --short "$barehead")" "$w/branchless.out" |
| 1344 | grep -qx " evidence refs/the-valley/attestations/$baredigest/$keyhash" "$w/branchless.out" |
| 1345 | if [ "$(git -C "$origin" rev-parse "refs/the-valley/integration-requests/main/c-$bareid")" != "$barehead" ]; then |
| 1346 | echo "valley-request: approve of a change with no branch did not file its head" >&2 |
| 1347 | exit 1 |
| 1348 | fi |
| 1349 | pushes="$(grep -c '^--- push' "$pushlog" || true)" |
| 1350 | if [ "$pushes" != 1 ] \ |
| 1351 | || ! grep -q "refs/the-valley/attestations/$baredigest/$keyhash" "$pushlog" \ |
| 1352 | || ! grep -q "refs/the-valley/integration-requests/main/c-$bareid" "$pushlog"; then |
| 1353 | echo "valley-request: the evidence and request for a change with no branch did not go in one push" >&2 |
| 1354 | cat "$pushlog" >&2 |
| 1355 | exit 1 |
| 1356 | fi |
| 1357 | # A change with a record is timed under its ID and the version approved. |
| 1358 | if ! tail -n 1 "$timings" | jq -e --arg id "$bareid" --arg head "$barehead" \ |
| 1359 | '.change == $id and .version == 1 and .head == $head and .check == "tree-ok"' > /dev/null; then |
| 1360 | echo "valley-request: the timing of a change with a record does not name the change and version:" >&2 |
| 1361 | tail -n 1 "$timings" >&2 |
| 1362 | exit 1 |
| 1363 | fi |
| 1364 | |
| 1365 | # ---------------------------------------------------------------------- |
| 1366 | # 20. Evidence origin already holds is reused, and only what is missing |
| 1367 | # runs here. A check worker publishes evidence as this section's |
| 1368 | # worker does: attest run under its own key, then a push of the |
| 1369 | # attestation ref alone. This origin runs the protected host's real |
| 1370 | # pre-receive hook, which admits a note only when a key it knows signed |
| 1371 | # it, behind a wrapper that logs every push it receives. The project's |
| 1372 | # owning valley publishes the signers a client verifies with, at |
| 1373 | # refs/the-valley/signers, written on disk as the host's publisher |
| 1374 | # writes it (signers-e2e drives the publisher itself). Both of the |
| 1375 | # project's checks use the nix runner, through integrator-e2e's |
| 1376 | # stand-in for nix, wrapped so that every call is logged: a check that |
| 1377 | # ran is a check the log shows a build of. |
| 1378 | r="$w/reuse" |
| 1379 | mkdir -p "$r/bin" "$r/keys" |
| 1380 | cp -r "$integratorE2e" "$r/stand-ins" |
| 1381 | chmod -R +w "$r/stand-ins" |
| 1382 | patchShebangs "$r/stand-ins" > /dev/null |
| 1383 | nixlog="$r/nix.log" |
| 1384 | for tool in nix nix-store; do |
| 1385 | cat > "$r/bin/$tool" <<EOF |
| 1386 | #!$(command -v bash) |
| 1387 | printf '%s %s\n' "$tool" "\$*" >> "$nixlog" |
| 1388 | exec "$r/stand-ins/$tool" "\$@" |
| 1389 | EOF |
| 1390 | chmod +x "$r/bin/$tool" |
| 1391 | done |
| 1392 | export PATH="$r/bin:$PATH" VALLEY_FAKE_STORE="$r/store" |
| 1393 | |
| 1394 | for who in worker operator integrator; do install -m 600 "$reuseKeys/$who" "$r/keys/$who"; done |
| 1395 | workername=worker.valley.invalid/attestations |
| 1396 | operatorname=operator.valley.invalid/attestations |
| 1397 | integratorname=integrator.valley.invalid/attestations |
| 1398 | workerhash="$(attest key --key "$r/keys/worker" --name "$workername" | cut -d+ -f2)" |
| 1399 | operatorhash="$(attest key --key "$r/keys/operator" --name "$operatorname" | cut -d+ -f2)" |
| 1400 | { |
| 1401 | attest key --key "$r/keys/worker" --name "$workername" |
| 1402 | attest key --key "$r/keys/operator" --name "$operatorname" |
| 1403 | attest key --key "$r/keys/integrator" --name "$integratorname" |
| 1404 | } > "$r/known-signers" |
| 1405 | |
| 1406 | # The owning valley: the floor, which requires nothing, and the signers it |
| 1407 | # publishes. |
| 1408 | instance="$r/instance.git" |
| 1409 | git init --quiet --bare "$instance" |
| 1410 | git init --quiet "$r/seed" |
| 1411 | mkdir -p "$r/seed/policy/instance" "$r/floor" |
| 1412 | printf 'package verification\n\nfloor: {\n\tchecks: {}\n\tclasses: {}\n\tunclassified: {}\n}\n' > "$r/floor/floor.cue" |
| 1413 | cp "$r/floor/floor.cue" "$r/seed/policy/instance/" |
| 1414 | git -C "$r/seed" add -A |
| 1415 | git -C "$r/seed" commit --quiet -m 'the floor' |
| 1416 | git -C "$r/seed" push --quiet "$instance" main |
| 1417 | # published <file> [<mode> <type> <object>]: the instance publishes the |
| 1418 | # known-signers file, and, given an entry, that entry as signer-projects. |
| 1419 | published() { |
| 1420 | local keysblob nameblob entries |
| 1421 | keysblob="$(git -C "$instance" hash-object -w "$1")" |
| 1422 | nameblob="$(printf '%s\n' "$integratorname" | git -C "$instance" hash-object -w --stdin)" |
| 1423 | entries="$(printf '100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers' "$nameblob" "$keysblob")" |
| 1424 | [ $# -lt 4 ] || entries+="$(printf '\n%s %s %s\tsigner-projects' "$2" "$3" "$4")" |
| 1425 | git -C "$instance" update-ref refs/the-valley/signers "$(printf '%s\n' "$entries" | git -C "$instance" mktree)" |
| 1426 | } |
| 1427 | published "$r/known-signers" |
| 1428 | |
| 1429 | served="$r/project.git" |
| 1430 | hook="$(grep -o '/nix/store/[^ ]*-valley-protect-open' "$reuseInitScriptPath" | head -n1)" |
| 1431 | test -x "$hook" |
| 1432 | git init --quiet --bare "$served" |
| 1433 | reusepushes="$r/pushlog" |
| 1434 | cat > "$served/hooks/pre-receive" <<EOF |
| 1435 | #!/bin/sh |
| 1436 | echo '--- push' >> "$reusepushes" |
| 1437 | tee -a "$reusepushes" | "$hook" |
| 1438 | EOF |
| 1439 | chmod +x "$served/hooks/pre-receive" |
| 1440 | |
| 1441 | proj="$r/project" |
| 1442 | git init --quiet "$proj" |
| 1443 | cd "$proj" || exit 1 |
| 1444 | git remote add origin "$served" |
| 1445 | mkdir -p docs src live checks policy/project |
| 1446 | echo "$instance" > policy/valley |
| 1447 | echo "the readme" > docs/readme.md |
| 1448 | echo "the source" > src/main.txt |
| 1449 | echo "the state" > live/state.txt |
| 1450 | echo docs > checks/prose-format.inputs |
| 1451 | echo src > checks/code-build.inputs |
| 1452 | cat > policy/project/policy.cue <<'EOF' |
| 1453 | package verification |
| 1454 | |
| 1455 | project: { |
| 1456 | checks: { |
| 1457 | "prose-format": {runner: "nix", attribute: "prose-format"} |
| 1458 | "code-build": {runner: "nix", attribute: "code-build"} |
| 1459 | "live-ok": {runner: "command", command: "true", validity: "1h"} |
| 1460 | } |
| 1461 | classes: { |
| 1462 | prose: {paths: "docs/**": true, requires: "prose-format": true} |
| 1463 | code: {paths: "src/**": true, requires: "code-build": true} |
| 1464 | live: {paths: "live/**": true, requires: "live-ok": true} |
| 1465 | } |
| 1466 | unclassified: {} |
| 1467 | } |
| 1468 | EOF |
| 1469 | git add -A |
| 1470 | git commit --quiet -m 'seed the project' |
| 1471 | git push --quiet origin main |
| 1472 | git clone --quiet "$served" "$r/worker" |
| 1473 | |
| 1474 | # edited <branch> <file…>: a branch off origin's main that adds a line to |
| 1475 | # each file, pushed. |
| 1476 | edited() { |
| 1477 | local name="$1" f |
| 1478 | shift |
| 1479 | git fetch --quiet origin |
| 1480 | git checkout --quiet -B "$name" origin/main |
| 1481 | for f in "$@"; do echo "$name" >> "$f"; done |
| 1482 | git add -A |
| 1483 | git commit --quiet -m "$name" |
| 1484 | git push --quiet origin "$name" |
| 1485 | } |
| 1486 | # worker <head> <attest run flag…>: the worker attests the head under its |
| 1487 | # own key and pushes the attestation ref alone. |
| 1488 | worker() { |
| 1489 | local head="$1" digest ref |
| 1490 | shift |
| 1491 | git -C "$r/worker" fetch --quiet origin |
| 1492 | digest="$(attest digest --repo "$r/worker" --rev "$head" | cut -d: -f2)" |
| 1493 | ref="refs/the-valley/attestations/$digest/$workerhash" |
| 1494 | attest run --repo "$r/worker" --rev "$head" --key "$r/keys/worker" --name "$workername" "$@" \ |
| 1495 | > "$r/worker.out" 2>&1 || { |
| 1496 | echo "valley-request: the worker's attest run failed" >&2 |
| 1497 | cat "$r/worker.out" >&2 |
| 1498 | exit 1 |
| 1499 | } |
| 1500 | git -C "$r/worker" push --quiet origin "$ref:$ref" |
| 1501 | } |
| 1502 | # approving <name> <command…>: run the command as the principal that holds |
| 1503 | # the approve grant, signing as the operator, into <name>.out and .err in |
| 1504 | # the scratch directory. Origin's pushes and the nix log start empty. |
| 1505 | approving() { |
| 1506 | local name="$1" |
| 1507 | shift |
| 1508 | : > "$nixlog" |
| 1509 | : > "$reusepushes" |
| 1510 | VALLEY_PRINCIPAL=requester VALLEY_ATTEST_KEY="$r/keys/operator" VALLEY_ATTEST_NAME="$operatorname" \ |
| 1511 | "$@" < /dev/null > "$w/$name.out" 2> "$w/$name.err" |
| 1512 | } |
| 1513 | # requested <branch>: where origin's request for the branch stands, or |
| 1514 | # nothing. |
| 1515 | requested() { |
| 1516 | git -C "$served" rev-parse --verify --quiet "refs/the-valley/integration-requests/main/$1" || true |
| 1517 | } |
| 1518 | # integrated <request> [<signer projects file>]: one pass of the real |
| 1519 | # integrator, given the file when there is one, lands the request. |
| 1520 | integrated() { |
| 1521 | integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \ |
| 1522 | --known-signers "$r/known-signers" ${2:+--signer-projects "$2"} --instance "$r/floor" --schema "$schemaFile" \ |
| 1523 | --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || { |
| 1524 | echo "valley-request: the integrator failed" >&2 |
| 1525 | cat "$r/integrator.out" >&2 |
| 1526 | exit 1 |
| 1527 | } |
| 1528 | grep -q "^$1 -> refs/heads/main .*: land\$" "$r/integrator.out" || { |
| 1529 | echo "valley-request: the integrator did not land $1" >&2 |
| 1530 | cat "$r/integrator.out" >&2 |
| 1531 | exit 1 |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | # 20a. The worker covered every required check. approve reuses both, runs |
| 1536 | # nothing, needs no signing key, and pushes the request alone. |
| 1537 | edited cover docs/readme.md src/main.txt |
| 1538 | coverhead="$(git rev-parse cover)" |
| 1539 | coverdigest="$(attest digest --rev cover | cut -d: -f2)" |
| 1540 | worker "$coverhead" --check prose-format --check code-build |
| 1541 | approving cover env -u VALLEY_ATTEST_KEY valley approve origin/cover |
| 1542 | grep -qx "reusing the evidence origin holds for cover at $(git rev-parse --short cover):" "$w/cover.out" |
| 1543 | grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/cover.out" |
| 1544 | grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/cover.out" |
| 1545 | grep -qx " reused refs/the-valley/attestations/$coverdigest/$workerhash" "$w/cover.out" |
| 1546 | grep -qx ' checks code-build, prose-format' "$w/cover.out" |
| 1547 | if grep -qE '^(checking| evidence) ' "$w/cover.out" || [ -s "$nixlog" ]; then |
| 1548 | echo "valley-request: approve ran checks that origin's evidence covered" >&2 |
| 1549 | cat "$w/cover.out" "$nixlog" >&2 |
| 1550 | exit 1 |
| 1551 | fi |
| 1552 | [ "$(requested cover)" = "$coverhead" ] || { |
| 1553 | echo "valley-request: a fully covered approval filed no request" >&2 |
| 1554 | exit 1 |
| 1555 | } |
| 1556 | if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \ |
| 1557 | || ! grep -q ' refs/the-valley/integration-requests/main/cover$' "$reusepushes" \ |
| 1558 | || grep -q ' refs/the-valley/attestations/' "$reusepushes"; then |
| 1559 | echo "valley-request: a fully covered approval did not push the request alone" >&2 |
| 1560 | cat "$reusepushes" >&2 |
| 1561 | exit 1 |
| 1562 | fi |
| 1563 | |
| 1564 | # 20b. The same head again: the evidence is on origin and the request |
| 1565 | # stands, so there is nothing to push and no push is made. |
| 1566 | approving cover-again env -u VALLEY_ATTEST_KEY valley approve origin/cover |
| 1567 | grep -qx ' the request already stood at this head and origin already held the evidence, so nothing needed publishing' "$w/cover-again.out" |
| 1568 | if [ -s "$reusepushes" ] || [ -s "$nixlog" ]; then |
| 1569 | echo "valley-request: approving a covered head again pushed or ran something" >&2 |
| 1570 | cat "$reusepushes" "$nixlog" >&2 |
| 1571 | exit 1 |
| 1572 | fi |
| 1573 | # The integrator verifies the worker's evidence itself, and lands it. |
| 1574 | integrated cover |
| 1575 | |
| 1576 | # 20c. The request write stays leased when nothing runs: a request filed |
| 1577 | # while this approval is prepared wins, and nothing is published. |
| 1578 | edited raced docs/readme.md |
| 1579 | racedhead="$(git rev-parse raced)" |
| 1580 | worker "$racedhead" --check prose-format |
| 1581 | arm_race "git -C '$served' update-ref refs/the-valley/integration-requests/main/raced '$coverhead'" |
| 1582 | if approving raced env -u VALLEY_ATTEST_KEY valley approve origin/raced; then |
| 1583 | echo "valley-request: a covered approval clobbered a request filed during it" >&2 |
| 1584 | exit 1 |
| 1585 | fi |
| 1586 | disarm_race |
| 1587 | grep -q "a request for this change appeared while this one was being prepared, at $(git rev-parse --short "$coverhead")" "$w/raced.err" |
| 1588 | if [ "$(requested raced)" != "$coverhead" ] || [ -s "$reusepushes" ]; then |
| 1589 | echo "valley-request: a covered approval that lost the lease published something" >&2 |
| 1590 | exit 1 |
| 1591 | fi |
| 1592 | git -C "$served" update-ref -d refs/the-valley/integration-requests/main/raced |
| 1593 | |
| 1594 | # 20d. The worker covered one check. Its note for the other names another |
| 1595 | # flake attribute, so it is not reused, and that check runs here: |
| 1596 | # exactly that one, signed by the operator, in one push with the |
| 1597 | # request. The integrator lands it on the two signers' evidence. |
| 1598 | edited partial docs/readme.md src/main.txt |
| 1599 | partialhead="$(git rev-parse partial)" |
| 1600 | partialdigest="$(attest digest --rev partial | cut -d: -f2)" |
| 1601 | worker "$partialhead" --check prose-format --check code-build=prose-format |
| 1602 | approving partial valley approve origin/partial |
| 1603 | grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/partial.out" |
| 1604 | grep -qxF "valley: not reusing code-build: the integrator judges it by refs/the-valley/attestations/$partialdigest/$workerhash:code-build/statement.note, and its predicate.check.attribute is \"prose-format\", not \"code-build\"" "$w/partial.err" |
| 1605 | grep -qx "checking partial at $(git rev-parse --short partial): code-build" "$w/partial.out" |
| 1606 | grep -qE '^check code-build +nix +passed$' "$w/partial.out" |
| 1607 | if ! grep -qE '^nix build .*/code-build-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" || grep -q 'prose-format' "$nixlog"; then |
| 1608 | echo "valley-request: a partly covered approval did not run exactly the missing check" >&2 |
| 1609 | cat "$nixlog" >&2 |
| 1610 | exit 1 |
| 1611 | fi |
| 1612 | grep -qx " evidence refs/the-valley/attestations/$partialdigest/$operatorhash" "$w/partial.out" |
| 1613 | grep -qx " reused refs/the-valley/attestations/$partialdigest/$workerhash" "$w/partial.out" |
| 1614 | if [ "$(grep -c '^--- push' "$reusepushes")" != 1 ] \ |
| 1615 | || ! grep -q " refs/the-valley/attestations/$partialdigest/$operatorhash\$" "$reusepushes" \ |
| 1616 | || ! grep -q ' refs/the-valley/integration-requests/main/partial$' "$reusepushes"; then |
| 1617 | echo "valley-request: the remainder's evidence and the request did not go in one push" >&2 |
| 1618 | cat "$reusepushes" >&2 |
| 1619 | exit 1 |
| 1620 | fi |
| 1621 | git cat-file -p "refs/the-valley/attestations/$partialdigest/$operatorhash:code-build/statement.note" > /dev/null |
| 1622 | if git cat-file -e "refs/the-valley/attestations/$partialdigest/$operatorhash:prose-format" 2> /dev/null; then |
| 1623 | echo "valley-request: the operator signed a check the worker's evidence covered" >&2 |
| 1624 | exit 1 |
| 1625 | fi |
| 1626 | integrated partial |
| 1627 | |
| 1628 | # 20e. Origin already holds the operator's own evidence for this tree, for |
| 1629 | # one check alone. It is reused, and the other check has nowhere to |
| 1630 | # go, since the namespace admits creations only: refused before |
| 1631 | # anything runs (bd-7b7d58f). |
| 1632 | edited occupied docs/readme.md src/main.txt |
| 1633 | occupiedhead="$(git rev-parse occupied)" |
| 1634 | occupieddigest="$(attest digest --rev occupied | cut -d: -f2)" |
| 1635 | attest run --rev "$occupiedhead" --key "$r/keys/operator" --name "$operatorname" --check code-build \ |
| 1636 | > "$r/operator.out" 2>&1 |
| 1637 | git push --quiet origin "refs/the-valley/attestations/$occupieddigest/$operatorhash:refs/the-valley/attestations/$occupieddigest/$operatorhash" |
| 1638 | if approving occupied valley approve origin/occupied; then |
| 1639 | echo "valley-request: approve built evidence that origin could not take" >&2 |
| 1640 | exit 1 |
| 1641 | fi |
| 1642 | grep -qE "^ code-build +signed by $operatorname\+$operatorhash\$" "$w/occupied.out" |
| 1643 | grep -qF "origin already holds refs/the-valley/attestations/$occupieddigest/$operatorhash, this signer's evidence for this tree, and it does not cover prose-format" "$w/occupied.err" |
| 1644 | grep -qF 'that is bd-7b7d58f, which approving again will not clear. Nothing run, nothing published' "$w/occupied.err" |
| 1645 | if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested occupied)" ]; then |
| 1646 | echo "valley-request: an approval refused for an occupied ref ran or published something" >&2 |
| 1647 | exit 1 |
| 1648 | fi |
| 1649 | |
| 1650 | # 20f. A listing of origin's evidence that fails, and a fetch of it that |
| 1651 | # fails, are refusals, never an empty answer: nothing runs and nothing |
| 1652 | # is published. A stand-in for git, first on PATH, fails the one call |
| 1653 | # named. It needs bin/valley run straight, since the package's wrapper |
| 1654 | # puts its own git ahead of anything on PATH. |
| 1655 | edited unread docs/readme.md |
| 1656 | unreadhead="$(git rev-parse unread)" |
| 1657 | unreaddigest="$(attest digest --rev unread | cut -d: -f2)" |
| 1658 | worker "$unreadhead" --check prose-format |
| 1659 | unreadoid="$(git -C "$served" rev-parse "refs/the-valley/attestations/$unreaddigest/$workerhash")" |
| 1660 | realgit="$(command -v git)" |
| 1661 | mkdir -p "$r/shim" |
| 1662 | cat > "$r/shim/git" <<EOF |
| 1663 | #!$(command -v bash) |
| 1664 | # git, failing the one call REUSE_FAIL names: the listing of origin's |
| 1665 | # evidence for one tree, or a fetch of the object one of its refs names. |
| 1666 | case "\${REUSE_FAIL:-}:\$*" in |
| 1667 | inventory:ls-remote*"refs/the-valley/attestations/$unreaddigest/"*) |
| 1668 | echo "git: injected failure" >&2 |
| 1669 | exit 128 |
| 1670 | ;; |
| 1671 | fetch:fetch*--stdin*) |
| 1672 | wanted="\$(cat)" |
| 1673 | if grep -qF "$unreadoid" <<< "\$wanted"; then |
| 1674 | echo "git: injected failure" >&2 |
| 1675 | exit 128 |
| 1676 | fi |
| 1677 | printf '%s\n' "\$wanted" | exec "$realgit" "\$@" |
| 1678 | ;; |
| 1679 | esac |
| 1680 | exec "$realgit" "\$@" |
| 1681 | EOF |
| 1682 | chmod +x "$r/shim/git" |
| 1683 | for failing in inventory fetch; do |
| 1684 | if REUSE_FAIL="$failing" PATH="$r/shim:$PATH" VALLEY_VERIFICATION_SCHEMA="$schemaFile" \ |
| 1685 | approving "unread-$failing" bash "$cliScript" approve origin/unread; then |
| 1686 | echo "valley-request: approve went on past a failed $failing of origin's evidence" >&2 |
| 1687 | exit 1 |
| 1688 | fi |
| 1689 | case "$failing" in |
| 1690 | inventory) says='valley: could not list the evidence origin holds for this tree' ;; |
| 1691 | fetch) says='valley: could not fetch the evidence origin holds for this tree' ;; |
| 1692 | esac |
| 1693 | grep -qxF "$says" "$w/unread-$failing.err" |
| 1694 | grep -qF 'valley: could not read the evidence origin holds for unread; nothing run, nothing published' "$w/unread-$failing.err" |
| 1695 | if [ -s "$nixlog" ] || [ -s "$reusepushes" ] || [ -n "$(requested unread)" ]; then |
| 1696 | echo "valley-request: approve ran or published something after a failed $failing" >&2 |
| 1697 | exit 1 |
| 1698 | fi |
| 1699 | done |
| 1700 | # And with origin answering, the same approval reuses the worker's |
| 1701 | # evidence: the stand-in failed only what it was told to. |
| 1702 | VALLEY_VERIFICATION_SCHEMA="$schemaFile" approving unread bash "$cliScript" approve origin/unread |
| 1703 | grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/unread.out" |
| 1704 | [ "$(requested unread)" = "$unreadhead" ] |
| 1705 | integrated unread |
| 1706 | |
| 1707 | # 20g. approve skips a check only where the integrator accepts the same |
| 1708 | # evidence. Each case below stores one set of evidence on origin. |
| 1709 | # Approve, with no signing key, reuses nothing for it and so refuses, |
| 1710 | # naming the note the integrator judges the check by and why it is not |
| 1711 | # reused. Then the request is filed by hand, and one pass of the real |
| 1712 | # integrator decides it from the same refs. A note the hook would |
| 1713 | # refuse is planted past it, straight into origin's storage. Two cases |
| 1714 | # land: there approve is stricter than the integrator, on purpose. |
| 1715 | # |
| 1716 | # noted <file> <key> <name> <rev> <check> <attest run flag…>: attest the |
| 1717 | # revision in the worker's clone under the key, and copy the note it stored |
| 1718 | # for the check to the file, byte for byte. Nothing pushes the ref it |
| 1719 | # stored. |
| 1720 | noted() { |
| 1721 | local file="$1" key="$2" name="$3" rev="$4" check="$5" digest hash |
| 1722 | shift 5 |
| 1723 | git -C "$r/worker" fetch --quiet origin |
| 1724 | digest="$(attest digest --repo "$r/worker" --rev "$rev" | cut -d: -f2)" |
| 1725 | hash="$(attest key --key "$key" --name "$name" | cut -d+ -f2)" |
| 1726 | attest run --repo "$r/worker" --rev "$rev" --key "$key" --name "$name" "$@" > "$r/noted.out" 2>&1 || { |
| 1727 | echo "valley-request: attesting $rev for a planted note failed" >&2 |
| 1728 | cat "$r/noted.out" >&2 |
| 1729 | exit 1 |
| 1730 | } |
| 1731 | git -C "$r/worker" cat-file blob "refs/the-valley/attestations/$digest/$hash:$check/statement.note" > "$file" |
| 1732 | } |
| 1733 | # resigned <file> <sed expression>: the note's text, edited by the |
| 1734 | # expression, signed again by the worker. |
| 1735 | resigned() { |
| 1736 | sed -n '/^$/q;p' "$1" | sed "$2" > "$r/text" |
| 1737 | attest sign --key "$r/keys/worker" --name "$workername" "$r/text" > "$1" |
| 1738 | } |
| 1739 | # planted <ref> <check> <file>: the note stored at the ref on origin, in a |
| 1740 | # tree shaped as attest shapes one, straight into storage. |
| 1741 | planted() { |
| 1742 | local blob sub |
| 1743 | blob="$(git -C "$served" hash-object -w "$3")" |
| 1744 | sub="$(printf '100644 blob %s\tstatement.note\n' "$blob" | git -C "$served" mktree)" |
| 1745 | git -C "$served" update-ref "$1" "$(printf '040000 tree %s\t%s\n' "$sub" "$2" | git -C "$served" mktree)" |
| 1746 | } |
| 1747 | # unreused <branch> <reason>: approve, with no signing key, reuses nothing |
| 1748 | # for prose-format, says why, and refuses for want of a key. |
| 1749 | unreused() { |
| 1750 | if approving "$1" env -u VALLEY_ATTEST_KEY valley approve "origin/$1"; then |
| 1751 | echo "valley-request: approve reused evidence for $1 that the integrator would not accept" >&2 |
| 1752 | cat "$w/$1.out" >&2 |
| 1753 | exit 1 |
| 1754 | fi |
| 1755 | if ! grep -qF "valley: not reusing prose-format: the integrator judges it by " "$w/$1.err" \ |
| 1756 | || ! grep -qF -- "$2" "$w/$1.err" || ! grep -q 'no signing key' "$w/$1.err" \ |
| 1757 | || [ -n "$(requested "$1")" ] || [ -s "$nixlog" ]; then |
| 1758 | echo "valley-request: approve did not refuse $1 for the reason: $2" >&2 |
| 1759 | cat "$w/$1.err" >&2 |
| 1760 | exit 1 |
| 1761 | fi |
| 1762 | } |
| 1763 | # judged <branch> <outcome> <reason> [<known-signers> [<signer projects>]]: |
| 1764 | # the branch's head is filed by hand, and one pass of the real integrator, |
| 1765 | # verifying with the known-signers file and given the signer projects file |
| 1766 | # when there is one, decides it as the outcome, giving the reason. A |
| 1767 | # request it does not land is taken away again. |
| 1768 | judged() { |
| 1769 | git -C "$served" update-ref "refs/the-valley/integration-requests/main/$1" "$(git rev-parse "$1")" |
| 1770 | integrator reconcile --repo "$served" --key "$r/keys/integrator" --name "$integratorname" \ |
| 1771 | --known-signers "${4:-$r/known-signers}" ${5:+--signer-projects "$5"} --instance "$r/floor" --schema "$schemaFile" \ |
| 1772 | --attest-schema "$attestSchema" --event-schema "$eventSchema" > "$r/integrator.out" 2>&1 || { |
| 1773 | echo "valley-request: the integrator failed" >&2 |
| 1774 | cat "$r/integrator.out" >&2 |
| 1775 | exit 1 |
| 1776 | } |
| 1777 | if ! grep -q "^$1 -> refs/heads/main @ [0-9a-f]*: $2\$" "$r/integrator.out" \ |
| 1778 | || ! grep -qF -- "$3" "$r/integrator.out"; then |
| 1779 | echo "valley-request: the integrator did not decide $1 as $2, saying: $3" >&2 |
| 1780 | cat "$r/integrator.out" >&2 |
| 1781 | exit 1 |
| 1782 | fi |
| 1783 | [ "$2" = land ] || git -C "$served" update-ref -d "refs/the-valley/integration-requests/main/$1" |
| 1784 | } |
| 1785 | under() { printf 'refs/the-valley/attestations/%s/%s' "$(attest digest --rev "$1" | cut -d: -f2)" "$2"; } |
| 1786 | |
| 1787 | # Stale trust: the host revokes the worker, and this operator's own |
| 1788 | # known-signers file still trusts it. The override does not widen what the |
| 1789 | # host publishes. |
| 1790 | { |
| 1791 | attest key --key "$r/keys/operator" --name "$operatorname" |
| 1792 | attest key --key "$r/keys/integrator" --name "$integratorname" |
| 1793 | } > "$r/revoked-signers" |
| 1794 | edited revoked docs/readme.md |
| 1795 | worker "$(git rev-parse revoked)" --check prose-format |
| 1796 | published "$r/revoked-signers" |
| 1797 | VALLEY_KNOWN_SIGNERS="$r/known-signers" unreused revoked \ |
| 1798 | "attest verify refuses it under the host's signers: no signature on this note is by a key the verifier holds" |
| 1799 | judged revoked stale 'signed by no signer the integrator holds' "$r/revoked-signers" |
| 1800 | published "$r/known-signers" |
| 1801 | |
| 1802 | # Narrowed trust: an override that does not trust the worker keeps approve |
| 1803 | # from reusing what the host would accept. |
| 1804 | edited narrowed docs/readme.md |
| 1805 | worker "$(git rev-parse narrowed)" --check prose-format |
| 1806 | VALLEY_KNOWN_SIGNERS="$r/revoked-signers" unreused narrowed \ |
| 1807 | 'attest verify refuses it under VALLEY_KNOWN_SIGNERS: no signature on this note is by a key the verifier holds' |
| 1808 | judged narrowed land 'transferred (unmoved)' |
| 1809 | |
| 1810 | # Conflicting notes: the integrator judges a check by the first note that |
| 1811 | # verifies, in ref order. A failure the worker signed sits first, so the |
| 1812 | # pass the worker published after it does not count. |
| 1813 | edited failed docs/readme.md |
| 1814 | noted "$r/failed.note" "$r/keys/worker" "$workername" "$(git rev-parse failed)" prose-format --check prose-format |
| 1815 | resigned "$r/failed.note" 's/^predicate.result passed$/predicate.result failed/' |
| 1816 | attest verify --note "$r/failed.note" --known-keys "$r/known-signers" --repo . --rev failed > /dev/null |
| 1817 | planted "$(under failed 00000000)" prose-format "$r/failed.note" |
| 1818 | worker "$(git rev-parse failed)" --check prose-format |
| 1819 | unreused failed 'its predicate.result is "failed", not "passed"' |
| 1820 | judged failed stale 'the attestation records the check as failed' |
| 1821 | |
| 1822 | # The same, with an observation first: a note of the wrong kind. |
| 1823 | edited kind docs/readme.md |
| 1824 | noted "$r/kind.note" "$r/keys/worker" "$workername" "$(git rev-parse kind)" prose-format --command prose-format=true |
| 1825 | planted "$(under kind 00000000)" prose-format "$r/kind.note" |
| 1826 | worker "$(git rev-parse kind)" --check prose-format |
| 1827 | unreused kind 'its predicateType is "the-valley/check/effectful/v1", not "the-valley/check/pure/v1"' |
| 1828 | judged kind reject 'the policy requires a pure check and the attestation claims an effectful check' |
| 1829 | |
| 1830 | # A malformed envelope: a note the worker signed, without its last newline. |
| 1831 | # approve reads it byte for byte, as the integrator does. |
| 1832 | edited envelope docs/readme.md |
| 1833 | noted "$r/envelope.note" "$r/keys/worker" "$workername" "$(git rev-parse envelope)" prose-format --check prose-format |
| 1834 | head -c -1 "$r/envelope.note" > "$r/envelope.cut" |
| 1835 | planted "$(under envelope "$workerhash")" prose-format "$r/envelope.cut" |
| 1836 | unreused envelope 'the signature block is empty or does not end with a newline' |
| 1837 | judged envelope reject 'the attestation does not verify as the statement it claims to be' |
| 1838 | |
| 1839 | # An unknown signature: a key neither the host nor the hook knows. |
| 1840 | ssh-keygen -q -t ed25519 -N "" -C stranger -f "$r/keys/stranger" |
| 1841 | strangername=stranger.valley.invalid/attestations |
| 1842 | edited stranger docs/readme.md |
| 1843 | noted "$r/stranger.note" "$r/keys/stranger" "$strangername" "$(git rev-parse stranger)" prose-format --check prose-format |
| 1844 | planted "$(under stranger "$(attest key --key "$r/keys/stranger" --name "$strangername" | cut -d+ -f2)")" \ |
| 1845 | prose-format "$r/stranger.note" |
| 1846 | unreused stranger 'no signature on this note is by a key the verifier holds' |
| 1847 | judged stranger stale 'signed by no signer the integrator holds' |
| 1848 | |
| 1849 | # A wrong tree: the worker's note about main, stored under this head's tree. |
| 1850 | edited tree docs/readme.md |
| 1851 | noted "$r/tree.note" "$r/keys/worker" "$workername" origin/main prose-format --check prose-format |
| 1852 | planted "$(under tree "$workerhash")" prose-format "$r/tree.note" |
| 1853 | unreused tree 'tree digest mismatch' |
| 1854 | judged tree reject 'tree digest mismatch' |
| 1855 | |
| 1856 | # An expired observation of a command check. approve never reuses one, so |
| 1857 | # with no key it refuses; the integrator refuses the observation as too old. |
| 1858 | edited expired live/state.txt |
| 1859 | noted "$r/expired.note" "$r/keys/worker" "$workername" "$(git rev-parse expired)" live-ok --command live-ok=true |
| 1860 | resigned "$r/expired.note" 's/^predicate.observedAt .*/predicate.observedAt 2020-01-01T00:00:00Z/' |
| 1861 | planted "$(under expired "$workerhash")" live-ok "$r/expired.note" |
| 1862 | if approving expired env -u VALLEY_ATTEST_KEY valley approve origin/expired \ |
| 1863 | || ! grep -q 'no signing key' "$w/expired.err" || grep -q 'reusing' "$w/expired.out"; then |
| 1864 | echo "valley-request: approve reused an observation of a command check" >&2 |
| 1865 | cat "$w/expired.out" "$w/expired.err" >&2 |
| 1866 | exit 1 |
| 1867 | fi |
| 1868 | judged expired stale 'the observation is ' |
| 1869 | |
| 1870 | # The attribute: the worker's note for prose-format names another flake |
| 1871 | # attribute. The integrator's rule for an unmoved tree compares none, so it |
| 1872 | # lands; approve compares it, and builds rather than reuse. |
| 1873 | edited attribute docs/readme.md |
| 1874 | worker "$(git rev-parse attribute)" --check prose-format=code-build |
| 1875 | unreused attribute 'its predicate.check.attribute is "code-build", not "prose-format"' |
| 1876 | judged attribute land 'transferred (unmoved)' |
| 1877 | |
| 1878 | # 20h. A signer can be restricted to projects. The host publishes its |
| 1879 | # signer projects file beside the known-signers file, and approve |
| 1880 | # reuses a note only when one of its signers counts in this project. |
| 1881 | # The project is named project, after origin, as the integrator names |
| 1882 | # it after its repository. Each integrator pass is given the file the |
| 1883 | # host publishes. |
| 1884 | # |
| 1885 | # restricting <file>: the instance publishes the known-signers file with |
| 1886 | # this signer projects file. |
| 1887 | restricting() { |
| 1888 | published "$r/known-signers" 100644 blob "$(git -C "$instance" hash-object -w "$1")" |
| 1889 | } |
| 1890 | # unpublished <branch> <reason>: the publication is not used, so approve, |
| 1891 | # with no signing key, reuses none of origin's evidence, says why, and |
| 1892 | # refuses for want of a key. |
| 1893 | unpublished() { |
| 1894 | if approving "$1" env -u VALLEY_ATTEST_KEY valley approve "origin/$1"; then |
| 1895 | echo "valley-request: approve reused evidence for $1 under a publication it cannot use" >&2 |
| 1896 | cat "$w/$1.out" >&2 |
| 1897 | exit 1 |
| 1898 | fi |
| 1899 | if ! grep -qxF "valley: $2, so none of the evidence on origin is reused" "$w/$1.err" \ |
| 1900 | || grep -q 'reusing' "$w/$1.out" || ! grep -q 'no signing key' "$w/$1.err" \ |
| 1901 | || [ -n "$(requested "$1")" ] || [ -s "$nixlog" ]; then |
| 1902 | echo "valley-request: approve did not refuse $1 for the reason: $2" >&2 |
| 1903 | cat "$w/$1.err" >&2 |
| 1904 | exit 1 |
| 1905 | fi |
| 1906 | } |
| 1907 | |
| 1908 | # The worker restricted to another project: its note is a stranger's here. |
| 1909 | # approve does not reuse it, the integrator does not accept it, and an |
| 1910 | # approval with a signing key builds the check under the operator's key. |
| 1911 | printf '%s elsewhere\n' "$workername" > "$r/elsewhere.projects" |
| 1912 | restricting "$r/elsewhere.projects" |
| 1913 | edited elsewhere docs/readme.md |
| 1914 | elsewherehead="$(git rev-parse elsewhere)" |
| 1915 | worker "$elsewherehead" --check prose-format |
| 1916 | unreused elsewhere \ |
| 1917 | "none of its signers counts in project project: the published signer-projects lets $workername count only in elsewhere" |
| 1918 | judged elsewhere stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects" |
| 1919 | approving elsewhere-built valley approve origin/elsewhere |
| 1920 | grep -qF "valley: not reusing prose-format: the integrator judges it by $(under elsewhere "$workerhash"):prose-format/statement.note, and none of its signers counts in project project" "$w/elsewhere-built.err" |
| 1921 | grep -qx "checking elsewhere at $(git rev-parse --short elsewhere): prose-format" "$w/elsewhere-built.out" |
| 1922 | grep -qx " evidence $(under elsewhere "$operatorhash")" "$w/elsewhere-built.out" |
| 1923 | if ! grep -qE '^nix build .*/prose-format-[A-Za-z0-9]{6}\.drv\^\*$' "$nixlog" \ |
| 1924 | || grep -q '^ reused ' "$w/elsewhere-built.out"; then |
| 1925 | echo "valley-request: approve did not build the check whose only signer counts elsewhere" >&2 |
| 1926 | cat "$w/elsewhere-built.out" "$nixlog" >&2 |
| 1927 | exit 1 |
| 1928 | fi |
| 1929 | [ "$(requested elsewhere)" = "$elsewherehead" ] |
| 1930 | integrated elsewhere "$r/elsewhere.projects" |
| 1931 | |
| 1932 | # The same worker note, countersigned by the integrator. The integrator's |
| 1933 | # own signature never counts for a check, so the note still has no signer |
| 1934 | # who counts here. |
| 1935 | edited countersigned docs/readme.md |
| 1936 | noted "$r/countersigned.note" "$r/keys/worker" "$workername" "$(git rev-parse countersigned)" prose-format --check prose-format |
| 1937 | attest sign --key "$r/keys/integrator" --name "$integratorname" "$r/countersigned.note" > "$r/countersigned.both" |
| 1938 | planted "$(under countersigned "$workerhash")" prose-format "$r/countersigned.both" |
| 1939 | unreused countersigned \ |
| 1940 | "the published signer-projects lets $workername count only in elsewhere; $integratorname is the host's integrator, whose signature is a countersignature" |
| 1941 | judged countersigned stale 'signed by no signer the integrator holds' "$r/known-signers" "$r/elsewhere.projects" |
| 1942 | |
| 1943 | # A published list that does not read, and one that is no file: the |
| 1944 | # publication is not used, and nothing on origin is reused. |
| 1945 | edited listed docs/readme.md |
| 1946 | listedhead="$(git rev-parse listed)" |
| 1947 | worker "$listedhead" --check prose-format |
| 1948 | printf '%s project\n%s elsewhere\n' "$workername" "$workername" > "$r/twice.projects" |
| 1949 | restricting "$r/twice.projects" |
| 1950 | unpublished listed \ |
| 1951 | "signer-projects in refs/the-valley/signers in $instance does not read: line 2: $workername is named on an earlier line too" |
| 1952 | printf '%s project\n' "$workername" > "$r/here.projects" |
| 1953 | listblob="$(git -C "$instance" hash-object -w "$r/here.projects")" |
| 1954 | for entry in "040000 tree $(printf '100644 blob %s\tlist\n' "$listblob" | git -C "$instance" mktree)" "100755 blob $listblob"; do |
| 1955 | # shellcheck disable=SC2086 # the entry is a mode, a type and an object |
| 1956 | published "$r/known-signers" $entry |
| 1957 | unpublished listed "signer-projects in refs/the-valley/signers in $instance is not a file" |
| 1958 | done |
| 1959 | |
| 1960 | # The worker restricted to this project and one more: the same note is |
| 1961 | # reused, nothing runs, and the integrator lands it. |
| 1962 | printf '# the projects the worker builds for\n%s elsewhere project\n' "$workername" > "$r/here.projects" |
| 1963 | restricting "$r/here.projects" |
| 1964 | approving listed env -u VALLEY_ATTEST_KEY valley approve origin/listed |
| 1965 | grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/listed.out" |
| 1966 | grep -qx " reused $(under listed "$workerhash")" "$w/listed.out" |
| 1967 | if [ -s "$nixlog" ] || [ "$(requested listed)" != "$listedhead" ]; then |
| 1968 | echo "valley-request: approve did not reuse the note of a worker restricted to this project" >&2 |
| 1969 | cat "$w/listed.out" "$w/listed.err" "$nixlog" >&2 |
| 1970 | exit 1 |
| 1971 | fi |
| 1972 | integrated listed "$r/here.projects" |
| 1973 | published "$r/known-signers" |
| 1974 | |
| 1975 | # ---------------------------------------------------------------------- |
| 1976 | # 21. The check worker builds what ready changes require before anyone |
| 1977 | # approves them, and publishes it where approve reuses it. It runs as |
| 1978 | # packaged, valley-worker, from a clone of the owning valley, which now |
| 1979 | # publishes its project list, so the worker reads the project through |
| 1980 | # its own cache clone, as valley pending --repo does. It signs with the |
| 1981 | # worker's key and pushes as a principal that holds no grant, through |
| 1982 | # the same protected hook. The host restricts the worker's key to this |
| 1983 | # project, and each integrator pass is given that list. The worker's |
| 1984 | # state directory holds its lock and its record of failed batches. |
| 1985 | printf '%s project\n' "$workername" > "$r/worker.projects" |
| 1986 | restricting "$r/worker.projects" |
| 1987 | git -C "$instance" update-ref refs/the-valley/projects \ |
| 1988 | "$(printf 'project\n' | git -C "$instance" hash-object -w --stdin)" |
| 1989 | git clone --quiet "$instance" "$r/worker-home" |
| 1990 | wstate="$r/worker-state" |
| 1991 | git config valley.principal requester |
| 1992 | # fresh: origin's pushes and the nix log start empty. |
| 1993 | fresh() { |
| 1994 | : > "$nixlog" |
| 1995 | : > "$reusepushes" |
| 1996 | } |
| 1997 | # working <name> [<env assignment>…]: one pass of the worker, with the |
| 1998 | # environment given, into $r/<name>.log. Its status is the pass's. |
| 1999 | working() { |
| 2000 | local name="$1" |
| 2001 | shift |
| 2002 | (cd "$r/worker-home" && env VALLEY_PRINCIPAL=worker "$@" valley-worker --repo project \ |
| 2003 | --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/$name.log" 2>&1 |
| 2004 | } |
| 2005 | # readied <branch> <file…>: edited, then readied as a change by the |
| 2006 | # requester. It sets id, handle and head, and line, the start of each line |
| 2007 | # the worker prints about the change's active version. |
| 2008 | readied() { |
| 2009 | local name="$1" |
| 2010 | edited "$@" |
| 2011 | VALLEY_PRINCIPAL=requester valley ready "$name" > "$r/$name.ready" 2>&1 |
| 2012 | id="$(sed -n 's/^The change ID \([k-z]*\) is new\..*/\1/p' "$r/$name.ready")" |
| 2013 | handle="$(sed -n 's/^project \([^ ]*\) v1 ready$/\1/p' "$r/$name.ready")" |
| 2014 | head="$(git rev-parse "$name")" |
| 2015 | line="project: $handle v1 at ${head:0:7}" |
| 2016 | [ -n "$id" ] && [ -n "$handle" ] || { |
| 2017 | echo "valley-request: could not ready $name" >&2 |
| 2018 | cat "$r/$name.ready" >&2 |
| 2019 | exit 1 |
| 2020 | } |
| 2021 | } |
| 2022 | # pushed <ref>: origin received one push since fresh, carrying that ref |
| 2023 | # alone. pushed none: origin received no push. |
| 2024 | pushed() { |
| 2025 | if [ "$1" = none ]; then |
| 2026 | [ ! -s "$reusepushes" ] && return |
| 2027 | elif [ "$(grep -c '^--- push' "$reusepushes")" = 1 ] \ |
| 2028 | && [ "$(grep -vc '^--- push' "$reusepushes")" = 1 ] \ |
| 2029 | && grep -q " $1\$" "$reusepushes"; then |
| 2030 | return |
| 2031 | fi |
| 2032 | echo "valley-request: the worker's pushes were not what was expected ($1)" >&2 |
| 2033 | cat "$reusepushes" >&2 |
| 2034 | exit 1 |
| 2035 | } |
| 2036 | # built <check…>: nix built a derivation of exactly these checks, each |
| 2037 | # once. attest evaluates the checks of a batch together, then builds each |
| 2038 | # check's derivation with nix build <store>/<check>-XXXXXX.drv^*, which the |
| 2039 | # stand-in names after the check, so every derivation a nix build line |
| 2040 | # names is read as one build of that check. built nothing: nix did not run. |
| 2041 | built() { |
| 2042 | local got |
| 2043 | if [ "$1" = nothing ]; then |
| 2044 | [ ! -s "$nixlog" ] && return |
| 2045 | echo "valley-request: the worker ran nix when it had nothing to build" >&2 |
| 2046 | cat "$nixlog" >&2 |
| 2047 | exit 1 |
| 2048 | fi |
| 2049 | got="$(grep '^nix build ' "$nixlog" | tr ' ' '\n' \ |
| 2050 | | sed -n 's|^.*/\([a-z0-9][a-z0-9-]*\)-[A-Za-z0-9]\{6\}\.drv^\*$|\1|p' | sort | paste -sd' ')" |
| 2051 | [ "$got" = "$(printf '%s\n' "$@" | sort | paste -sd' ')" ] && return |
| 2052 | echo "valley-request: the worker built '$got', not '$*'" >&2 |
| 2053 | cat "$nixlog" >&2 |
| 2054 | exit 1 |
| 2055 | } |
| 2056 | # said <name> <line>: the pass's log holds the line. |
| 2057 | said() { |
| 2058 | grep -qxF "$2" "$r/$1.log" && return |
| 2059 | echo "valley-request: the pass $1 did not say: $2" >&2 |
| 2060 | cat "$r/$1.log" >&2 |
| 2061 | exit 1 |
| 2062 | } |
| 2063 | |
| 2064 | # 21a. Evidence precedes approval. The worker builds the nix checks a ready |
| 2065 | # change requires, together, and pushes the one attestation ref |
| 2066 | # attest stored, before any request exists. A change whose only check |
| 2067 | # is a command check is left to approve. The next pass finds the |
| 2068 | # evidence on origin and does nothing. approve, with no signing key, |
| 2069 | # reuses all of it and runs nothing, and the real integrator lands it. |
| 2070 | readied lively live/state.txt |
| 2071 | livelyline="$line" |
| 2072 | readied early docs/readme.md src/main.txt |
| 2073 | earlydigest="$(attest digest --rev early | cut -d: -f2)" |
| 2074 | earlyref="refs/the-valley/attestations/$earlydigest/$workerhash" |
| 2075 | fresh |
| 2076 | working early |
| 2077 | said early "$line: building code-build, prose-format" |
| 2078 | said early "$line: published $earlyref" |
| 2079 | said early "$livelyline: requires no nix check" |
| 2080 | built code-build prose-format |
| 2081 | pushed "$earlyref" |
| 2082 | git -C "$served" cat-file -e "$earlyref:code-build/statement.note" |
| 2083 | git -C "$served" cat-file -e "$earlyref:prose-format/statement.note" |
| 2084 | [ -z "$(requested "c-$id")" ] || { |
| 2085 | echo "valley-request: a request existed before the worker's evidence" >&2 |
| 2086 | exit 1 |
| 2087 | } |
| 2088 | fresh |
| 2089 | working early-again |
| 2090 | said early-again "$line: origin holds evidence for every nix check it requires" |
| 2091 | built nothing |
| 2092 | pushed none |
| 2093 | approving early env -u VALLEY_ATTEST_KEY valley approve --head "$head" "$handle" |
| 2094 | grep -qE "^ prose-format +signed by $workername\+$workerhash\$" "$w/early.out" |
| 2095 | grep -qE "^ code-build +signed by $workername\+$workerhash\$" "$w/early.out" |
| 2096 | built nothing |
| 2097 | [ "$(requested "c-$id")" = "$head" ] |
| 2098 | integrated "c-$id" "$r/worker.projects" |
| 2099 | |
| 2100 | # 21b. Concurrent passes take turns. The first holds the lock while it |
| 2101 | # builds, and a stand-in hook keeps it building until the second says |
| 2102 | # it is waiting. The second then reads origin after the first has |
| 2103 | # published, and builds nothing. |
| 2104 | readied together src/main.txt |
| 2105 | togetherref="refs/the-valley/attestations/$(attest digest --rev together | cut -d: -f2)/$workerhash" |
| 2106 | cat > "$r/hold" <<EOF |
| 2107 | #!$(command -v bash) |
| 2108 | [ -e "$r/hold-armed" ] || exit 0 |
| 2109 | rm -f "$r/hold-armed" |
| 2110 | for _ in \$(seq 600); do |
| 2111 | grep -q 'waiting for it to end' "$r/together-second.log" 2> /dev/null && exit 0 |
| 2112 | sleep 0.1 |
| 2113 | done |
| 2114 | echo "the second pass never waited for the first" >&2 |
| 2115 | exit 1 |
| 2116 | EOF |
| 2117 | chmod +x "$r/hold" |
| 2118 | touch "$r/hold-armed" |
| 2119 | fresh |
| 2120 | working together-first VALLEY_E2E_DURING_PASS="$r/hold" & |
| 2121 | first=$! |
| 2122 | for _ in $(seq 600); do |
| 2123 | grep -q ': building ' "$r/together-first.log" 2> /dev/null && break |
| 2124 | sleep 0.1 |
| 2125 | done |
| 2126 | working together-second |
| 2127 | wait "$first" |
| 2128 | said together-second "valley: another pass holds $wstate/lock; waiting for it to end" |
| 2129 | said together-first "$line: published $togetherref" |
| 2130 | said together-second "$line: origin holds evidence for every nix check it requires" |
| 2131 | built code-build |
| 2132 | pushed "$togetherref" |
| 2133 | |
| 2134 | # 21c. A failed batch backs off. Every nix call fails while the stand-in |
| 2135 | # is told to fail. The pass reports the failure, publishes nothing, |
| 2136 | # and exits 1. The batch is not built again until its wait, 15 |
| 2137 | # minutes, is over, then it fails again and waits twice as long. Once |
| 2138 | # it passes it is published, and the record of it is gone. |
| 2139 | readied flaky docs/readme.md |
| 2140 | flakyref="refs/the-valley/attestations/$(attest digest --rev flaky | cut -d: -f2)/$workerhash" |
| 2141 | fresh |
| 2142 | if working flaky-1 VALLEY_NOW=2026-10-22T10:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then |
| 2143 | echo "valley-request: a pass whose batch failed exited 0" >&2 |
| 2144 | exit 1 |
| 2145 | fi |
| 2146 | said flaky-1 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:15:00Z" |
| 2147 | pushed none |
| 2148 | jq -e '.projects.project | length == 1 and (.[] | .failures == 1)' "$wstate/failures.json" > /dev/null |
| 2149 | fresh |
| 2150 | working flaky-2 VALLEY_NOW=2026-10-22T10:14:59Z |
| 2151 | said flaky-2 "$line: its batch prose-format failed 1 time in a row, so it is not built again before 2026-10-22T10:15:00Z" |
| 2152 | built nothing |
| 2153 | pushed none |
| 2154 | if working flaky-3 VALLEY_NOW=2026-10-22T10:15:00Z VALLEY_E2E_DURING_PASS='exit 1'; then |
| 2155 | echo "valley-request: a pass whose batch failed again exited 0" >&2 |
| 2156 | exit 1 |
| 2157 | fi |
| 2158 | said flaky-3 "$line: the batch did not pass (exit status 1); it is not built again before 2026-10-22T10:45:00Z" |
| 2159 | fresh |
| 2160 | working flaky-4 VALLEY_NOW=2026-10-22T10:45:00Z |
| 2161 | said flaky-4 "$line: building prose-format" |
| 2162 | said flaky-4 "$line: published $flakyref" |
| 2163 | built prose-format |
| 2164 | pushed "$flakyref" |
| 2165 | [ ! -e "$wstate/failures.json" ] || { |
| 2166 | echo "valley-request: the record of a batch that passed was kept" >&2 |
| 2167 | cat "$wstate/failures.json" >&2 |
| 2168 | exit 1 |
| 2169 | } |
| 2170 | |
| 2171 | # 21d. A lost reply. The worker's push is taken by a receive-pack that |
| 2172 | # exits 1 when it is done, so origin lands the ref and the worker hears |
| 2173 | # a failure: the pass says so, and exits 1. The next pass finds the |
| 2174 | # evidence on origin, and builds and pushes nothing. Then a push that |
| 2175 | # never reaches origin: once its one-minute wait is over, the next |
| 2176 | # pass pushes the ref the first one stored, the same object, and |
| 2177 | # builds nothing. |
| 2178 | cat > "$r/lossy" <<EOF |
| 2179 | #!$(command -v bash) |
| 2180 | git-receive-pack "\$@" |
| 2181 | echo "the connection dropped" >&2 |
| 2182 | exit 1 |
| 2183 | EOF |
| 2184 | cat > "$r/unreached" <<EOF |
| 2185 | #!$(command -v bash) |
| 2186 | echo "the connection dropped" >&2 |
| 2187 | exit 1 |
| 2188 | EOF |
| 2189 | chmod +x "$r/lossy" "$r/unreached" |
| 2190 | readied landed docs/readme.md |
| 2191 | landedref="refs/the-valley/attestations/$(attest digest --rev landed | cut -d: -f2)/$workerhash" |
| 2192 | if working landed-1 VALLEY_NOW=2026-10-23T09:00:00Z GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/lossy"; then |
| 2193 | echo "valley-request: a pass that heard its push fail exited 0" >&2 |
| 2194 | exit 1 |
| 2195 | fi |
| 2196 | said landed-1 "$line: could not publish $landedref; it is not pushed again before 2026-10-23T09:01:00Z" |
| 2197 | git -C "$served" cat-file -e "$landedref:prose-format/statement.note" |
| 2198 | fresh |
| 2199 | working landed-2 VALLEY_NOW=2026-10-23T09:00:00Z |
| 2200 | said landed-2 "$line: origin holds evidence for every nix check it requires" |
| 2201 | built nothing |
| 2202 | pushed none |
| 2203 | readied unreached docs/readme.md |
| 2204 | unreachedref="refs/the-valley/attestations/$(attest digest --rev unreached | cut -d: -f2)/$workerhash" |
| 2205 | if working unreached-1 VALLEY_NOW=2026-10-23T09:00:00Z GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/unreached"; then |
| 2206 | echo "valley-request: a pass whose push never arrived exited 0" >&2 |
| 2207 | exit 1 |
| 2208 | fi |
| 2209 | if git -C "$served" rev-parse --verify --quiet "$unreachedref" > /dev/null; then |
| 2210 | echo "valley-request: a push that never arrived landed" >&2 |
| 2211 | exit 1 |
| 2212 | fi |
| 2213 | # The clone the worker reads the project through: keyed by its URL, which |
| 2214 | # is the owning valley's with its last segment replaced (status/list.go). |
| 2215 | cache="$HOME/.cache/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project" |
| 2216 | storedobject="$(git -C "$cache" rev-parse "$unreachedref")" |
| 2217 | fresh |
| 2218 | working unreached-2 VALLEY_NOW=2026-10-23T09:01:00Z |
| 2219 | said unreached-2 "$line: an earlier pass stored $unreachedref, and origin does not hold it" |
| 2220 | said unreached-2 "$line: published $unreachedref" |
| 2221 | built nothing |
| 2222 | pushed "$unreachedref" |
| 2223 | [ "$(git -C "$served" rev-parse "$unreachedref")" = "$storedobject" ] |
| 2224 | |
| 2225 | # 21e. A head superseded while it is built. A stand-in hook readies a new |
| 2226 | # version of the change in the middle of the batch. The pass does not |
| 2227 | # read the record again: it publishes the evidence for the head it |
| 2228 | # pinned, which the hook admits, since it is true of that tree. The |
| 2229 | # next pass builds the new version, which approve then reuses, and |
| 2230 | # the real integrator lands. |
| 2231 | readied superseded docs/readme.md |
| 2232 | oldline="$line" |
| 2233 | oldref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash" |
| 2234 | cat > "$r/supersede" <<EOF |
| 2235 | #!$(command -v bash) |
| 2236 | [ -e "$r/supersede-armed" ] || exit 0 |
| 2237 | rm -f "$r/supersede-armed" |
| 2238 | cd "$proj" |
| 2239 | echo again >> docs/readme.md |
| 2240 | git commit --quiet -am 'superseded, again' |
| 2241 | VALLEY_PRINCIPAL=requester git push --quiet origin superseded |
| 2242 | VALLEY_PRINCIPAL=requester valley ready --change "$id" superseded > "$r/superseded-v2.ready" 2>&1 |
| 2243 | # Its own pushes are not the worker's. |
| 2244 | : > "$reusepushes" |
| 2245 | EOF |
| 2246 | chmod +x "$r/supersede" |
| 2247 | touch "$r/supersede-armed" |
| 2248 | fresh |
| 2249 | working superseded-1 VALLEY_E2E_DURING_PASS="$r/supersede" |
| 2250 | grep -qx "project $handle v2 ready" "$r/superseded-v2.ready" |
| 2251 | said superseded-1 "$oldline: published $oldref" |
| 2252 | pushed "$oldref" |
| 2253 | newhead="$(git rev-parse superseded)" |
| 2254 | newref="refs/the-valley/attestations/$(attest digest --rev superseded | cut -d: -f2)/$workerhash" |
| 2255 | fresh |
| 2256 | working superseded-2 |
| 2257 | said superseded-2 "project: $handle v2 at ${newhead:0:7}: published $newref" |
| 2258 | built prose-format |
| 2259 | approving superseded env -u VALLEY_ATTEST_KEY valley approve --head "$newhead" "$handle" |
| 2260 | built nothing |
| 2261 | [ "$(requested "c-$id")" = "$newhead" ] |
| 2262 | integrated "c-$id" "$r/worker.projects" |
| 2263 | |
| 2264 | # 21f. Origin already holds the worker's own ref for a tree, without one of |
| 2265 | # the checks the change requires. The namespace admits creations |
| 2266 | # only, so the pass says so and builds nothing. approve builds the |
| 2267 | # rest under the operator's key, and the integrator lands it on the |
| 2268 | # two signers' evidence. |
| 2269 | readied worker-occupied docs/readme.md src/main.txt |
| 2270 | occupiedref="refs/the-valley/attestations/$(attest digest --rev worker-occupied | cut -d: -f2)/$workerhash" |
| 2271 | attest run --rev worker-occupied --key "$r/keys/worker" --name "$workername" --check prose-format > "$r/worker-occupied-attest.out" 2>&1 |
| 2272 | git push --quiet origin "$occupiedref:$occupiedref" |
| 2273 | fresh |
| 2274 | working worker-occupied |
| 2275 | said worker-occupied "$line: origin already holds $occupiedref, this worker's evidence for its tree, and it does not cover code-build. That namespace admits creations only, so approve builds the rest (bd-7b7d58f)" |
| 2276 | built nothing |
| 2277 | pushed none |
| 2278 | approving worker-occupied valley approve --head "$head" "$handle" |
| 2279 | built code-build |
| 2280 | integrated "c-$id" "$r/worker.projects" |
| 2281 | |
| 2282 | # 21g. A read of origin that fails stops the pass, before anything is |
| 2283 | # built: with the project's origin away, the pass exits 1 and says |
| 2284 | # so, and the ready change is left for a pass that can read it. |
| 2285 | readied unread-origin docs/readme.md |
| 2286 | mv "$served" "$served.away" |
| 2287 | fresh |
| 2288 | if working unread-origin; then |
| 2289 | echo "valley-request: a pass went on past an origin it could not read" >&2 |
| 2290 | exit 1 |
| 2291 | fi |
| 2292 | mv "$served.away" "$served" |
| 2293 | grep -q '^valley: project: .*; the pass stops here$' "$r/unread-origin.log" |
| 2294 | built nothing |
| 2295 | fresh |
| 2296 | working unread-origin-again |
| 2297 | said unread-origin-again "$line: building prose-format" |
| 2298 | |
| 2299 | # await <file> <text>: wait up to a minute for the file to hold the text. |
| 2300 | await() { |
| 2301 | for _ in $(seq 600); do |
| 2302 | grep -qF -- "$2" "$1" 2> /dev/null && return |
| 2303 | sleep 0.1 |
| 2304 | done |
| 2305 | echo "valley-request: $1 never said: $2" >&2 |
| 2306 | exit 1 |
| 2307 | } |
| 2308 | |
| 2309 | # 21h. The wait after a failed batch runs from when the batch ended. The |
| 2310 | # clock is not fixed here. The stand-in takes 3 seconds and then |
| 2311 | # fails, so the batch's wait ends at least 15 minutes after it ended, |
| 2312 | # which is more than 15 minutes after the pass began. Past the wait, |
| 2313 | # the batch is built and published. |
| 2314 | readied slow docs/readme.md |
| 2315 | slowdigest="$(attest digest --rev slow | cut -d: -f2)" |
| 2316 | began="$(date -u +%s)" |
| 2317 | fresh |
| 2318 | if working slow-1 VALLEY_E2E_DURING_PASS='sleep 3; exit 1'; then |
| 2319 | echo "valley-request: a pass whose batch failed exited 0" >&2 |
| 2320 | exit 1 |
| 2321 | fi |
| 2322 | retry="$(jq -r --arg key "build $slowdigest prose-format" '.projects.project[$key].retry' "$wstate/failures.json")" |
| 2323 | if [ "$(($(date -u -d "$retry" +%s) - began))" -lt 903 ]; then |
| 2324 | echo "valley-request: the wait after a 3-second batch ends at $retry, less than 15 minutes after it ended; the pass began at $(date -u -d "@$began" +%FT%TZ)" >&2 |
| 2325 | exit 1 |
| 2326 | fi |
| 2327 | fresh |
| 2328 | working slow-2 VALLEY_NOW=2099-01-01T00:00:00Z |
| 2329 | said slow-2 "$line: published refs/the-valley/attestations/$slowdigest/$workerhash" |
| 2330 | |
| 2331 | # 21i. A failed push waits too, by the object it would publish. Two changes |
| 2332 | # whose heads hold the same tree share one attestation ref, and origin |
| 2333 | # takes no push. The first pass builds once and tries one push, and the |
| 2334 | # other change's push waits behind that failure. A pass within the |
| 2335 | # minute tries no push. The next failure doubles the wait. Past it, the |
| 2336 | # ref is published once, and the record of it is gone. |
| 2337 | cat > "$r/counted" <<EOF |
| 2338 | #!$(command -v bash) |
| 2339 | echo push >> "$r/push-attempts" |
| 2340 | echo "the connection dropped" >&2 |
| 2341 | exit 1 |
| 2342 | EOF |
| 2343 | chmod +x "$r/counted" |
| 2344 | refusing=(GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=remote.origin.receivepack GIT_CONFIG_VALUE_0="$r/counted") |
| 2345 | readied twin-a docs/readme.md |
| 2346 | git checkout --quiet -B twin-b origin/main |
| 2347 | git checkout --quiet twin-a -- docs/readme.md |
| 2348 | git commit --quiet -m twin-b |
| 2349 | VALLEY_PRINCIPAL=requester git push --quiet origin twin-b |
| 2350 | VALLEY_PRINCIPAL=requester valley ready twin-b > "$r/twin-b.ready" 2>&1 |
| 2351 | [ "$(git rev-parse 'twin-a^{tree}')" = "$(git rev-parse 'twin-b^{tree}')" ] |
| 2352 | twinref="refs/the-valley/attestations/$(attest digest --rev twin-a | cut -d: -f2)/$workerhash" |
| 2353 | # attempts <n> <name> <failed> <waiting>: origin was offered n pushes in |
| 2354 | # all, and the pass's log says failed and waiting for the twins' ref that |
| 2355 | # many times each, with the wait ending at the time given last. |
| 2356 | attempts() { |
| 2357 | local until="$5" |
| 2358 | if [ "$(wc -l < "$r/push-attempts")" != "$1" ] \ |
| 2359 | || [ "$(grep -c ": could not publish $twinref; it is not pushed again before $until\$" "$r/$2.log")" != "$3" ] \ |
| 2360 | || [ "$(grep -c ": publishing $twinref failed [0-9]* times\? in a row, so it is not pushed again before $until\$" "$r/$2.log")" != "$4" ]; then |
| 2361 | echo "valley-request: the pass $2 did not offer origin the twins' ref as often as expected" >&2 |
| 2362 | cat "$r/push-attempts" "$r/$2.log" >&2 |
| 2363 | exit 1 |
| 2364 | fi |
| 2365 | } |
| 2366 | : > "$r/push-attempts" |
| 2367 | fresh |
| 2368 | if working twins-1 VALLEY_NOW=2026-10-24T09:00:00Z "${refusing[@]}"; then |
| 2369 | echo "valley-request: a pass whose push failed exited 0" >&2 |
| 2370 | exit 1 |
| 2371 | fi |
| 2372 | built prose-format |
| 2373 | attempts 1 twins-1 1 1 2026-10-24T09:01:00Z |
| 2374 | working twins-2 VALLEY_NOW=2026-10-24T09:00:59Z "${refusing[@]}" |
| 2375 | attempts 1 twins-2 0 2 2026-10-24T09:01:00Z |
| 2376 | if working twins-3 VALLEY_NOW=2026-10-24T09:01:00Z "${refusing[@]}"; then |
| 2377 | echo "valley-request: a pass whose push failed again exited 0" >&2 |
| 2378 | exit 1 |
| 2379 | fi |
| 2380 | attempts 2 twins-3 1 1 2026-10-24T09:03:00Z |
| 2381 | fresh |
| 2382 | working twins-4 VALLEY_NOW=2026-10-24T09:03:00Z |
| 2383 | built nothing |
| 2384 | pushed "$twinref" |
| 2385 | [ ! -e "$wstate/failures.json" ] || { |
| 2386 | echo "valley-request: the record of a push that went through was kept" >&2 |
| 2387 | cat "$wstate/failures.json" >&2 |
| 2388 | exit 1 |
| 2389 | } |
| 2390 | |
| 2391 | # 21j. A pass forgets only the failures of the projects it read whole. The |
| 2392 | # host serves a second project, other, with no ready change. A batch of |
| 2393 | # this project fails. A pass over other alone leaves that wait as it |
| 2394 | # is, so the next pass over this project does not build the batch. |
| 2395 | git init --quiet --bare "$r/other.git" |
| 2396 | git -C "$instance" update-ref refs/the-valley/projects \ |
| 2397 | "$(printf 'other\nproject\n' | git -C "$instance" hash-object -w --stdin)" |
| 2398 | readied flapping docs/readme.md |
| 2399 | flappingref="refs/the-valley/attestations/$(attest digest --rev flapping | cut -d: -f2)/$workerhash" |
| 2400 | if working flapping-1 VALLEY_NOW=2026-10-25T09:00:00Z VALLEY_E2E_DURING_PASS='exit 1'; then |
| 2401 | echo "valley-request: a pass whose batch failed exited 0" >&2 |
| 2402 | exit 1 |
| 2403 | fi |
| 2404 | (cd "$r/worker-home" && VALLEY_PRINCIPAL=worker VALLEY_NOW=2026-10-25T09:01:00Z valley-worker --repo other \ |
| 2405 | --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/other.log" 2>&1 || { |
| 2406 | echo "valley-request: a pass over other failed" >&2 |
| 2407 | cat "$r/other.log" >&2 |
| 2408 | exit 1 |
| 2409 | } |
| 2410 | fresh |
| 2411 | working flapping-2 VALLEY_NOW=2026-10-25T09:02:00Z |
| 2412 | said flapping-2 "$line: its batch prose-format failed 1 time in a row, so it is not built again before 2026-10-25T09:15:00Z" |
| 2413 | built nothing |
| 2414 | fresh |
| 2415 | working flapping-3 VALLEY_NOW=2026-10-25T09:15:00Z |
| 2416 | said flapping-3 "$line: published $flappingref" |
| 2417 | |
| 2418 | # 21k. A pass killed while attest runs. attest holds the lock it was |
| 2419 | # handed, so a second pass waits for attest to end rather than build |
| 2420 | # the same batch beside it. Then the second pass finds the ref the |
| 2421 | # orphaned attest stored, publishes it, and builds nothing. |
| 2422 | readied killed docs/readme.md |
| 2423 | killedref="refs/the-valley/attestations/$(attest digest --rev killed | cut -d: -f2)/$workerhash" |
| 2424 | cat > "$r/hold-kill" <<EOF |
| 2425 | #!$(command -v bash) |
| 2426 | [ -e "$r/kill-armed" ] || exit 0 |
| 2427 | rm -f "$r/kill-armed" |
| 2428 | # The worker is the nearest process above this one named valley-go. |
| 2429 | p=\$\$ |
| 2430 | while [ "\$p" -gt 1 ]; do |
| 2431 | if [ "\$(cat "/proc/\$p/comm")" = valley-go ]; then |
| 2432 | echo "\$p" > "$r/killed.pid" |
| 2433 | break |
| 2434 | fi |
| 2435 | read -r _ _ _ p _ < "/proc/\$p/stat" |
| 2436 | done |
| 2437 | for _ in \$(seq 600); do |
| 2438 | [ -e "$r/release" ] && exit 0 |
| 2439 | sleep 0.1 |
| 2440 | done |
| 2441 | exit 1 |
| 2442 | EOF |
| 2443 | chmod +x "$r/hold-kill" |
| 2444 | touch "$r/kill-armed" |
| 2445 | fresh |
| 2446 | working killed-1 VALLEY_E2E_DURING_PASS="$r/hold-kill" & |
| 2447 | killedpass=$! |
| 2448 | await "$r/killed.pid" "" |
| 2449 | kill -9 "$(cat "$r/killed.pid")" |
| 2450 | status=0 |
| 2451 | wait "$killedpass" || status=$? |
| 2452 | [ "$status" = 137 ] || { |
| 2453 | echo "valley-request: the killed pass ended with $status" >&2 |
| 2454 | exit 1 |
| 2455 | } |
| 2456 | working killed-2 & |
| 2457 | secondpass=$! |
| 2458 | await "$r/killed-2.log" "valley: another pass holds $wstate/lock; waiting for it to end" |
| 2459 | touch "$r/release" |
| 2460 | wait "$secondpass" |
| 2461 | said killed-2 "$line: an earlier pass stored $killedref, and origin does not hold it" |
| 2462 | said killed-2 "$line: published $killedref" |
| 2463 | built prose-format |
| 2464 | pushed "$killedref" |
| 2465 | |
| 2466 | # 21l. A cache clone whose making was interrupted. A stand-in for git takes |
| 2467 | # the clone's config.lock as it sets the origin, kills the worker, and |
| 2468 | # dies holding the lock, as git killed mid-write does. The next pass |
| 2469 | # makes the clone again and reads the project through it. The worker |
| 2470 | # runs as valley-go here, since the package's wrapper puts its own git |
| 2471 | # ahead of the stand-in. |
| 2472 | cachehome="$r/interrupted-cache" |
| 2473 | interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project" |
| 2474 | mkdir -p "$r/killing-git" |
| 2475 | cat > "$r/killing-git/git" <<EOF |
| 2476 | #!$(command -v bash) |
| 2477 | if [ "\${1:-} \${2:-}" = "remote add" ]; then |
| 2478 | : > .git/config.lock |
| 2479 | kill -9 "\$PPID" |
| 2480 | kill -9 "\$\$" |
| 2481 | fi |
| 2482 | exec "$(command -v git)" "\$@" |
| 2483 | EOF |
| 2484 | chmod +x "$r/killing-git/git" |
| 2485 | status=0 |
| 2486 | (cd "$r/worker-home" && VALLEY_PRINCIPAL=worker XDG_CACHE_HOME="$cachehome" PATH="$r/killing-git:$PATH" \ |
| 2487 | VALLEY_VERIFICATION_SCHEMA="$schemaFile" valley-go work --repo project \ |
| 2488 | --key "$r/keys/worker" --name "$workername" --state "$wstate") > "$r/interrupted-1.log" 2>&1 || status=$? |
| 2489 | if [ "$status" != 137 ] || [ -z "$(find "$cachehome" -name config.lock)" ]; then |
| 2490 | echo "valley-request: the stand-in did not interrupt the clone's making while holding its config.lock ($status)" >&2 |
| 2491 | cat "$r/interrupted-1.log" >&2 |
| 2492 | exit 1 |
| 2493 | fi |
| 2494 | # recovered <name>: a pass over the cache at $cachehome succeeds, and the |
| 2495 | # clone it reads the project through names origin. |
| 2496 | recovered() { |
| 2497 | fresh |
| 2498 | working "$1" XDG_CACHE_HOME="$cachehome" || { |
| 2499 | echo "valley-request: the pass after an interrupted clone failed" >&2 |
| 2500 | cat "$r/$1.log" >&2 |
| 2501 | exit 1 |
| 2502 | } |
| 2503 | [ "$(git -C "$interrupted" config --get remote.origin.url)" = "$served" ] |
| 2504 | said "$1" "$livelyline: requires no nix check" |
| 2505 | built nothing |
| 2506 | } |
| 2507 | recovered interrupted-2 |
| 2508 | |
| 2509 | # 21m. A clone left half made in its place, as an older version left one: |
| 2510 | # a repository with no origin, and a stale config.lock beside its |
| 2511 | # configuration. The next pass sets it aside, makes the clone again, |
| 2512 | # and reads the project through it. |
| 2513 | cachehome="$r/half-made-cache" |
| 2514 | interrupted="$cachehome/valley/repos/$(printf '%s' "$served" | sha256sum | cut -c1-16)-project" |
| 2515 | git init --quiet "$interrupted" |
| 2516 | : > "$interrupted/.git/config.lock" |
| 2517 | recovered half-made |
| 2518 | [ ! -e "$interrupted/.git/config.lock" ] |
| 2519 | |
| 2520 | worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)" |
| 2521 | if [ "$worktrees" != 1 ]; then |
| 2522 | echo "valley-request: a throwaway worktree was left behind in the reuse project" >&2 |
| 2523 | git worktree list >&2 |
| 2524 | exit 1 |
| 2525 | fi |
| 2526 | cd "$repo" || exit 1 |
| 2527 | |
| 2528 | # ---------------------------------------------------------------------- |
| 2529 | # The verbs the loop offers are the whole of what an operator can do to a |
| 2530 | # branch, so no path may offer one outside the set: [a]pprove, [b]ase, |
| 2531 | # [r]eject, [s]kip, and [v]iew full diff, which only shows. Every stream this check captured is scanned at the end, |
| 2532 | # which is what makes the claim about paths rather than about the two |
| 2533 | # prompts pinned character for character above — the fast-forward prompt, |
| 2534 | # the stale one, the one [b]ase comes back to, and the ones a refused [a]pprove |
| 2535 | # returns to are all in here. A verb that writes the protected ref from the |
| 2536 | # operator's own checkout — the retired [i]ntegrate, or any successor to |
| 2537 | # it — fails this wherever it is printed. |
| 2538 | offered="$(cat "$w"/*.out | grep -o '\[[a-z]\][a-z]*' | sort -u || true)" |
| 2539 | outside="$(printf '%s\n' "$offered" | grep -vxE '\[a\]pprove|\[b\]ase|\[r\]eject|\[s\]kip|\[v\]iew' || true)" |
| 2540 | if [ -n "$outside" ]; then |
| 2541 | echo "valley-request: a review path offered a verb outside the set:" >&2 |
| 2542 | printf '%s\n' "$outside" >&2 |
| 2543 | exit 1 |
| 2544 | fi |
| 2545 | # The scan is only worth something if it saw the offers at all. |
| 2546 | for verb in '[a]pprove' '[b]ase' '[r]eject' '[s]kip' '[v]iew'; do |
| 2547 | printf '%s\n' "$offered" | grep -qxF "$verb" \ |
| 2548 | || { echo "valley-request: no captured session offered $verb" >&2; exit 1; } |
| 2549 | done |
| 2550 | |
| 2551 | # No throwaway worktree outlived any of it: only the checkout is left. |
| 2552 | worktrees="$(git worktree list --porcelain | grep -c '^worktree ' || true)" |
| 2553 | if [ "$worktrees" != 1 ]; then |
| 2554 | echo "valley-request: a throwaway worktree was left behind" >&2 |
| 2555 | git worktree list >&2 |
| 2556 | exit 1 |
| 2557 | fi |
| 2558 | |
| 2559 | echo "every scenario held" |
| 2560 | { |
| 2561 | cat "$w/approve.out" |
| 2562 | printf '\n--- the scratch origin, after all of it ---\n' |
| 2563 | git -C "$origin" for-each-ref --format='%(objectname) %(refname)' |
| 2564 | } > "$out" |