| 1 | # Architecture as deployed |
| 2 | |
| 3 | This document describes what the-valley runs today, read from the code. It makes three kinds of |
| 4 | statement. Behaviour is what the code does, cited to the files that do it. A deployment record |
| 5 | reports an observation of a host, and names the record and its date. A guarantee names the flake |
| 6 | check (`checks.x86_64-linux.<name>`) or test that demonstrates it; a statement with no named check |
| 7 | is unchecked. Security and rollouts are in [security.md](./security.md), planned work is in |
| 8 | [roadmap.md](./roadmap.md), and purpose and principles are in [purpose.md](./purpose.md). |
| 9 | |
| 10 | ## Overview |
| 11 | |
| 12 | A valley host keeps bare git repositories and accepts changes to them over ssh. Anyone with a key |
| 13 | may push a topic branch. A protected branch such as `main` accepts a push only from the principals |
| 14 | the project names as its writers. The integrator moves it by landing an integration request. The |
| 15 | integrator lands a request when every check the project's policy requires is covered by a signed |
| 16 | statement that the check passed. |
| 17 | |
| 18 | The parts are: |
| 19 | |
| 20 | - the host module, which installs everything below (`nix/valley-host.nix`); |
| 21 | - the push hook, which decides what a push may write (`valleyhook/`); |
| 22 | - the integrator, one controller per protected project (`integrator/`); |
| 23 | - `attest`, which runs checks and signs the evidence (`attest/`, `note/`); |
| 24 | - the identity compiler, which turns a registry into keys and grants (`identity/`); |
| 25 | - the `valley` command line tool (`bin/valley`); |
| 26 | - `sigverify`, a hardware-key signature verifier that nothing calls yet (`sigverify/`). |
| 27 | |
| 28 | ## The host module |
| 29 | |
| 30 | The NixOS module `services.valley` reads one CUE file, `services.valley.config`. The build vets it |
| 31 | against `schema/valley.cue` and fails on any error (`nix/valley-host.nix`). The file declares the |
| 32 | projects, their mirrors, their protection and the backup policy. Checked by `module-eval` and |
| 33 | `cue-vet`. |
| 34 | |
| 35 | ### Repositories and the git user |
| 36 | |
| 37 | Each project is a bare repository at `/srv/git/<name>.git`. All ssh git traffic logs in as one Unix |
| 38 | user, `git`. The git user's login shell runs `valleyhook shell` (`nix/valley-host.nix`, |
| 39 | `valleyhook/shell.go`). For each session, `valleyhook shell` does three things: |
| 40 | |
| 41 | 1. For a push only, it refuses the session when the hold file `/srv/git/.valley-hold` exists, or |
| 42 | when the converged record `/srv/git/.valley-converged` does not match the running configuration. |
| 43 | Fetches stay open. Both are checked when the session starts, so a push already past this check is |
| 44 | not stopped by a hold created later. |
| 45 | 2. It drops every `GIT_*` variable and any `VALLEY_PRINCIPAL` the client sent. It sets |
| 46 | `VALLEY_PRINCIPAL` from the tag on the authorized-keys entry of the key that logged in. An |
| 47 | untagged or ambiguous key gets no principal. |
| 48 | 3. It runs `git-shell` with the same arguments. `git-shell` limits the session to git's own |
| 49 | commands. |
| 50 | |
| 51 | The sshd `Match User git` block turns off TCP, agent and X11 forwarding and tunnels, and sets |
| 52 | `ExposeAuthInfo` so the shell can see which key logged in. It leaves stream-local (Unix socket) |
| 53 | forwarding on. cosmo adds its own block with `DisableForwarding yes` and `PermitTTY no` (cosmo |
| 54 | `hosts/classic-laddie/default.nix`). Build-time assertions refuse an sshd config that would let a |
| 55 | client set `VALLEY_PRINCIPAL` or `GIT_*` (`nix/valley-host.nix`). Checked by `ssh-e2e` (a real sshd |
| 56 | in a VM), `module-eval` and `valleyhook-unit`. |
| 57 | |
| 58 | ### valley-init and the push hold |
| 59 | |
| 60 | `valley-init` is a oneshot service that runs as git (`nix/valley-host.nix`). It first deletes the |
| 61 | converged record, so pushes are refused while it runs. It then creates any missing repository and |
| 62 | links each repository's hooks. A foreign `pre-receive` hook or any `core.hooksPath` setting is a |
| 63 | conflict. When there is no conflict, it writes the converged record. The record is the first 32 hex |
| 64 | characters of a SHA-256 of the init script. So pushes resume only when every repository's |
| 65 | `pre-receive` hook is this configuration's. A hand-written `post-receive` hook is left in place and |
| 66 | is not a conflict. Checked by `init-e2e` and `ssh-e2e`. |
| 67 | |
| 68 | The hold file belongs to the operator, who creates it to pause all pushes. `valley-init` never |
| 69 | touches it. |
| 70 | |
| 71 | ## What a push may write |
| 72 | |
| 73 | Every repository's `pre-receive` hook runs `valleyhook pre-receive` (`valleyhook/main.go`, |
| 74 | `valleyhook/policy.go`). The hook is default-deny. If any one ref update is refused, the whole push |
| 75 | is refused. A push may update at most 1000 refs, and the hook refuses one that updates more before |
| 76 | it decides any ref. It asks `git symbolic-ref` whether each pushed ref is symbolic, so it reads a |
| 77 | symbolic ref exactly as git does. Checked by `protect-e2e`, `ssh-e2e` and `valleyhook-unit`. |
| 78 | |
| 79 | | Ref namespace | Who may push | Rule | |
| 80 | | --------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 81 | | `refs/heads/*` (topic branches) | Any key | Create, update and delete. | |
| 82 | | Protected refs (default `refs/heads/main`) | Principals in `protection.writers` (default empty) | The integrator moves them on disk. cosmo names `integrator` as a writer of both its projects. | |
| 83 | | `refs/the-valley/attestations/<tree>/<key-hash>` | Any key | Create only. The ref must name a tree of notes within size bounds, and each note must verify under a known signer and be about that tree (`valleyhook/attestation.go`). | |
| 84 | | `refs/the-valley/integration-requests/<target>/<c>` | Principals with the `request` grant | Create, update and delete. | |
| 85 | | `refs/the-valley/changes/main/<change-id>` | Any named principal creates; the owner or a `request` holder updates; a `request` holder deletes | A change record. The hook allows only the transitions in [Change records](#change-records) (`valleyhook/record.go`). | |
| 86 | | Every other `refs/the-valley/*` | Nobody | This covers the integrator's outcome records. | |
| 87 | | `refs/replace/*`, `refs/notes/*` | Nobody | A replace ref would let git show other content under a watched name. | |
| 88 | | Symbolic refs | Nobody | | |
| 89 | | Tags and every other ref | Principals named in a project grant: `grants.<name>: {refs, writers}` (`schema/valley.cue`) | Grant patterns cannot name `heads`, `replace`, `notes` or `the-valley`. | |
| 90 | |
| 91 | The `request` grant comes from two places: the host's declared `services.valley.grants.request` and |
| 92 | the identity compiler's `grants` file. Why replace refs are closed is recorded in |
| 93 | [bd-75c8721](../archive/.the-valley/bugs/bd-75c8721-pushes-could-write-any-namespace.md). |
| 94 | |
| 95 | ### Change records |
| 96 | |
| 97 | A change is one piece of work aimed at `main`. Its ID is stable across rebases, in jj's form: 32 |
| 98 | letters from `k` to `z`. Each time a head of the change is marked ready, the change gains a version. |
| 99 | The change's record lists the versions, and the hook guards every write to it. Records are |
| 100 | observational: nothing reads them for authority. The integration request is still the only way a |
| 101 | change reaches `main`, and the integrator does not read records. |
| 102 | |
| 103 | A change is in one of three states: |
| 104 | |
| 105 | - `ready`: the change is offered for landing. A change is created ready. |
| 106 | - `draft`: the change is paused, by `valley unready`. A draft voids nothing. When it is readied |
| 107 | again, the approvals of its active version still stand. |
| 108 | - `withdrawn`: the change is given up. Withdrawing voids everything outstanding on the change: its |
| 109 | integration requests, and the approvals and reviews of every version. A change brought back from |
| 110 | withdrawn always needs fresh approvals. |
| 111 | |
| 112 | Nothing enforces what withdrawing voids yet. The integrator does not read records, so a request |
| 113 | filed before a change was paused or withdrawn can still land it. Until it does read them, |
| 114 | `valley unready` and `valley withdraw` warn when origin holds a request for the change. The request |
| 115 | is looked for under the name `approve` gives it, derived from the active version's branch, and under |
| 116 | `c-<change-id>`. Only a `request` holder may touch a request ref, so the warning says the operator |
| 117 | must drop it. Approvals are not records yet either; they become records with M1-C. Until then, |
| 118 | voiding an approval means the integrator refuses it, once the integrator reads records. |
| 119 | |
| 120 | The ref `refs/the-valley/changes/main/<change-id>` points at a record commit. The commit's tree |
| 121 | holds one entry, `record.json`, as a plain file. Its schema is `valley-change-record/v1`, read by |
| 122 | one strict parser that the hook and the CLI share (`change/record.go`): |
| 123 | |
| 124 | ```json |
| 125 | { |
| 126 | "schema": "valley-change-record/v1", |
| 127 | "change_id": "<32 letters from k to z>", |
| 128 | "target": "main", |
| 129 | "owner": "<principal>", |
| 130 | "state": "ready", |
| 131 | "seq": 3, |
| 132 | "versions": [ |
| 133 | { |
| 134 | "n": 1, |
| 135 | "head": "git-sha1:<40 lowercase hex>", |
| 136 | "title": "<the head's subject>", |
| 137 | "branch": "<branch, or empty>", |
| 138 | "uploader": "<principal>" |
| 139 | } |
| 140 | ], |
| 141 | "active": 1 |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | The parser refuses anything that is not valid UTF-8. It also refuses a duplicate key at any depth, |
| 146 | an unknown field, and data after the object. Every field must have the type shown. `seq` runs from 1 |
| 147 | to 100000, and `n` and `active` from 1 to 1000. Versions are numbered 1, 2, 3 with no gap. A title |
| 148 | is 1 to 200 bytes with no control characters. `state` is `ready`, `draft` or `withdrawn`. |
| 149 | |
| 150 | The record commit's parents tie it to what it records. In the table, `P` is the record commit's |
| 151 | parents, `old` is the ref's value before the push, and `h` is the head of the version being added. |
| 152 | |
| 153 | | Transition | Who | `P` | What the record must say | |
| 154 | | ------------------------- | ------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 155 | | Create | Any named principal | `[h]` | One version: `n` 1, `head` `h`, and the pusher as `uploader`. The pusher as `owner`, `seq` 1, `active` 1, `state` `ready`. `target` and `change_id` are the ref's. | |
| 156 | | New version | The owner or a `request` holder | `[old, h]` | `old`'s versions unchanged, plus one with the next `n`, `head` `h` other than the active head, and the pusher as `uploader`. `active` is the new `n`, `state` `ready`, `seq` one more than `old`'s. | |
| 157 | | Unready | The owner or a `request` holder | `[old]` | Everything as in `old`, except `state` goes from `ready` to `draft` and `seq` goes up by one. `active` stays. | |
| 158 | | Withdraw | The owner or a `request` holder | `[old]` | Everything as in `old`, except `state` goes from `ready` or `draft` to `withdrawn` and `seq` goes up by one. `active` stays. | |
| 159 | | Ready again, same version | The owner or a `request` holder | `[old]` | Everything as in `old`, except `state` goes from `draft` or `withdrawn` to `ready` and `seq` goes up by one. `active` stays. | |
| 160 | | Ready again, new version | The owner or a `request` holder | `[old, h]` | As for a new version, from `draft` or `withdrawn`. | |
| 161 | | Delete | A `request` holder | — | Allowed, to keep storage tidy. | |
| 162 | |
| 163 | The hook refuses every other write. That includes a key that names no principal, extra parents, |
| 164 | parents in another order, a head that is not a commit, and a tree of another shape. It also includes |
| 165 | a record with one parent that leaves the state as it was, such as `ready` to `ready` without a new |
| 166 | version, and `withdrawn` to `draft`. A withdrawn change comes back ready, never as a draft. The |
| 167 | owner, the target and the change ID never change. |
| 168 | |
| 169 | Each record commit is at most 4 KiB, its tree at most 1 KiB, and its `record.json` at most 64 KiB. A |
| 170 | push may update at most 32 records. The hook checks each of these bounds before it reads anything |
| 171 | past it. |
| 172 | |
| 173 | A repository may hold at most 2000 records, counted with one `for-each-ref`. This bound is soft. A |
| 174 | pre-receive hook runs before git installs any ref, and nothing serializes two pushes' hooks. So |
| 175 | pushes that run at the same time can each count the same records and each create more. The count can |
| 176 | then pass 2000 by at most the record updates those pushes had in flight, which is 32 per push. Once |
| 177 | the count is past 2000, every later create is refused. |
| 178 | |
| 179 | The writer numbers versions, and the hook holds them to append-only and gap-free. When the |
| 180 | integrator starts reading records, it takes this numbering as it is. For now, a change is owned by |
| 181 | the principal whose key pushed its first record, so `klaus-env` owns every agent change. |
| 182 | |
| 183 | A record keeps every head it names reachable, because each head is a parent of a record commit. So |
| 184 | records retain source objects the way topic branches do, and the hook restricts them no further. |
| 185 | |
| 186 | Checked by `valleyhook-unit`, with a case for each rule of each transition against real git objects, |
| 187 | by `change-unit`, which runs the parser's tests and its fuzz target's seed corpus, and by |
| 188 | `valley-ready`, which drives the transitions through real pushes. |
| 189 | |
| 190 | ## After a push: events and mirrors |
| 191 | |
| 192 | Each repository's `post-receive` hook runs every program in `hooks/post-receive.d/` |
| 193 | (`nix/valley-host.nix`). There are two: |
| 194 | |
| 195 | - **The bus publisher**, when the bus is on. It publishes one `ref-updated` event for every ref the |
| 196 | push updated, on the subject `valley.git.<repo>.ref-updated`. Checked by `bus-e2e`. |
| 197 | - **The mirror hook**, when the project declares mirrors. It does not push. It writes the push's ref |
| 198 | moves, one `<old> <new> <ref>` line each, as a file in the repository's publish queue, |
| 199 | `valley-publish-queue/`, and returns. A dead mirror therefore never slows or fails a push. |
| 200 | |
| 201 | The integrator moves refs with `git update-ref`, which runs no `post-receive` hook. So it does both |
| 202 | jobs itself for each landing (`integrator/bus.go`). It writes the move to the same publish queue. It |
| 203 | also publishes the `ref-updated` event straight to the bus, with the same payload the bus publisher |
| 204 | would send. It publishes only after the landing is fully recorded: the queue entry written, the |
| 205 | evidence stored and the request consumed. Each publish is stopped after 10 seconds, so a bus that |
| 206 | hangs cannot hold up a landing. Checked by `integrator-e2e`. |
| 207 | |
| 208 | One unit pushes the mirrors: `valley-publish@<project>`, a service that runs as git. A systemd path |
| 209 | unit starts it whenever the project's queue is not empty. Mirrors are a list of URLs |
| 210 | (`schema/valley.cue`). The service lists the queued files and then runs `git push --prune` of `main` |
| 211 | and all tags to each URL. After that it deletes every other branch on the mirror. Tags are copied |
| 212 | whether or not `main` reaches them, refs outside `refs/heads/` and `refs/tags/` on the mirror are |
| 213 | left alone, and the branch deletion is best effort. The push uses the git user's own ssh identity, |
| 214 | which the host provides. Checked by `mirror-e2e`. |
| 215 | |
| 216 | The service deletes the files it listed only after every mirror push succeeds. When a push or a |
| 217 | deletion fails, the service fails and the files stay. systemd then runs the service again after a |
| 218 | delay that grows from 30 seconds to 15 minutes. A run that lasts 15 minutes is stopped with |
| 219 | everything it started. Checked by `publish-e2e`. Because the integrator publishes its events itself, |
| 220 | a mirror outage never delays an event on the bus. |
| 221 | |
| 222 | Having one publisher is what keeps a mirror from being rewound. git reads the local refs before it |
| 223 | contacts the mirror. So when two pushes run at once, one can read an old `main`, stall, and then |
| 224 | force-push that old `main` over a newer one the other pushed meanwhile. systemd never runs two |
| 225 | instances of one unit at the same time, and nothing else pushes the mirrors. A move queued while a |
| 226 | push runs stays in the queue, so the next run pushes the newer `main`. `mirror-e2e` stalls a push in |
| 227 | that window: two pushers at once rewind the mirror, and the service ends at the newer `main`. |
| 228 | |
| 229 | That cosmo's deployed hosts run a publish queue is a deployment record: cosmo #934 merged and laddie |
| 230 | healthy, in the handoff of 2026-10-04. At that point the service also published the integrator's |
| 231 | events, and a push ran its own mirror pusher. |
| 232 | |
| 233 | The bus is NATS with JetStream, listening on `127.0.0.1:4222`. `valley-bus-init` creates one stream, |
| 234 | `valley`, over the subjects `valley.>`, with file storage and NATS defaults. The events are defined |
| 235 | in `schema/events.cue`: `ref-updated`, `integration-succeeded` and `request-stale`. The bus has no |
| 236 | authentication, so nothing treats an event as authoritative |
| 237 | ([bd-d853d9c](../archive/.the-valley/bugs/bd-d853d9c-bus-unauthenticated.md)). The integrator reads |
| 238 | git directly. |
| 239 | |
| 240 | ## The integrator |
| 241 | |
| 242 | Each protected project has one controller, `valley-integrator@<project>`, running as the |
| 243 | `valley-integrator` user in group git (`nix/valley-host.nix`). It makes one pass over the open |
| 244 | requests in order, then sleeps 15 seconds and starts again (`integrator/main.go`, |
| 245 | `integrator/refs.go`). No bound on how long a request waits is checked. |
| 246 | |
| 247 | 1. **Find requests.** A request is a ref `refs/the-valley/integration-requests/<target>/<change>` |
| 248 | that points at the change's head commit. |
| 249 | 2. **Skip what is decided.** The outcome record |
| 250 | `refs/the-valley/integration-outcomes/<target>/<change>` holds the head, the target tip, a hash |
| 251 | of all attestation refs, and the outcome. If the first three still match, the request is skipped. |
| 252 | A `failed` outcome is retried after 10 minutes. |
| 253 | 3. **Build the candidate.** The base is `git merge-base` of the tip and the head. If the base is the |
| 254 | tip, the candidate is the head. Otherwise `git merge-tree --write-tree` builds the tree, and the |
| 255 | integrator commits it with the tip as the only parent. The commit keeps the head's author. Its |
| 256 | committer is the integrator at `integrator@the-valley.invalid`. A conflict makes the request |
| 257 | stale. |
| 258 | 4. **Derive the required checks** (see Policy below). |
| 259 | 5. **Judge the evidence** (see Evidence below). The outcome is `land`, `stale` or `reject`. A stale |
| 260 | request publishes `request-stale`. |
| 261 | 6. **Land.** The integrator signs a transfer statement that records the policy digest and the checks |
| 262 | it relied on. Then it moves the target with compare-and-swap |
| 263 | (`git update-ref <target> <new> <tip>`). That is the commit point. Then it queues the landing for |
| 264 | the publisher, stores its countersigned evidence, deletes the request with compare-and-swap, |
| 265 | publishes `integration-succeeded`, and writes the outcome record. |
| 266 | |
| 267 | Each step is a separate `git update-ref` call. A crash between steps can leave a landing without its |
| 268 | evidence or outcome record. Checked by `integrator-e2e` (scenarios for fast-forward, merge, |
| 269 | conflict, stale evidence, tampered notes, retries and a moved target) and `integrator-unit`. |
| 270 | |
| 271 | ## Policy |
| 272 | |
| 273 | A project's required checks come from three CUE layers, unified as |
| 274 | `floor & templates[type] & project` (`schema/verification.cue`): |
| 275 | |
| 276 | - **The schema**, `schema/verification.cue`, ships inside the `valley` and integrator packages |
| 277 | (`nix/packages.nix`). |
| 278 | - **The floor and templates** come from the owning valley's instance repository, at |
| 279 | `policy/instance/*.cue` on its `main`. `policy/valley` names that repository. A value the floor |
| 280 | writes as `true` is mandatory. A template value written `bool | *true` is a default a project may |
| 281 | turn off. |
| 282 | - **The project layer** is `policy/project/*.cue`, read at the tip of the target branch. |
| 283 | |
| 284 | Both layers are read from integrated tips, never from the change being judged |
| 285 | (`integrator/policy.go`). A path class maps path globs to checks. A change owes the union of the |
| 286 | checks of every class one of its paths matches. A path no class matches owes the `unclassified` |
| 287 | checks. The integrator gets this answer by running `valley checks`, which diffs with renames counted |
| 288 | on both sides. Checked by `policy-deriver` and `cue-vet`. |
| 289 | |
| 290 | The derivation fails closed. A failed read of the diff, the target, a policy layer or the composed |
| 291 | policy makes `valley checks` exit non-zero and print no report. Without that, the failure would read |
| 292 | as an empty list, and an empty list derives a change that owes nothing. The integrator also refuses |
| 293 | output that is not a whole report: no header, no required-checks section, or a required-checks |
| 294 | heading with no checks under it. It believes a report of no changed paths only when git agrees that |
| 295 | the two trees are the same. Checked by `policy-deriver`, `valley-request` and `integrator-unit`. |
| 296 | |
| 297 | A check also declares a `cost`, `quick` by default. A `slow` check takes minutes, or needs special |
| 298 | hardware such as KVM. `valley checks` reads the cost from the composition it derives from. After the |
| 299 | report it prints two lines, `quick:` and `slow:`, each listing the owed checks of that cost. |
| 300 | `valley checks --names` prints the owed check names alone, one per line, and `--cost quick` or |
| 301 | `--cost slow` keeps one set. The integration gate does not read the cost. Approving runs every owed |
| 302 | check, and the integrator requires evidence for every owed check, whatever its cost. The cost |
| 303 | changes neither which checks a change owes nor what the integrator accepts. Checked by |
| 304 | `policy-deriver` and `integrator-unit`. |
| 305 | |
| 306 | the-valley's own project layer is `policy/project/policy.cue`. It gives each part of this repository |
| 307 | a class that owes the checks reading that part. A Go module owes its unit tests, and its end-to-end |
| 308 | check where it has one. The CLI owes its script and status checks. The Nix, the schemas and the |
| 309 | policy owe their evaluations and vets. The NixOS module also owes the end-to-end checks that run |
| 310 | what it renders, and two of those boot NixOS machines. A change to `flake.nix` or `flake.lock` does |
| 311 | not owe them, so a nixpkgs bump boots no machine. A check's own files, such as its script under |
| 312 | `nix/checks/`, owe that check. A change to documentation alone owes the floor's `prose-format` and |
| 313 | nothing else. A project layer that stopped composing would stop every derivation once it landed, |
| 314 | including the derivation of its own fix. So the `own-policy` check composes the layer with the |
| 315 | example floor before it lands. It fails if the layer names a check this flake does not define, if a |
| 316 | script under `nix/checks/` owes no check of its own, or if a docs-only change would owe more than |
| 317 | `prose-format`. |
| 318 | |
| 319 | The reasons for CUE here, and for the mandatory-versus-default encoding, are in |
| 320 | [dcr-0f5d9b1](../archive/.the-valley/decisions/dcr-0f5d9b1-cue-config-host-module.md) and |
| 321 | [dcr-f41f718](../archive/.the-valley/decisions/dcr-f41f718-declared-verification-policy.md). |
| 322 | |
| 323 | ## Evidence: attest and notes |
| 324 | |
| 325 | `attest run` runs checks against a commit exported with `git archive` (`attest/run.go`, |
| 326 | `attest/check.go`). A `nix` check builds the flake check's derivation. A `command` check runs a |
| 327 | shell command in the exported tree. If every check passes, `attest` writes a statement per check, |
| 328 | signs it, and stores it under `refs/the-valley/attestations/<tree>/<key-hash>`. |
| 329 | |
| 330 | A statement is line-based text that opens with `the-valley/attestation/v1` (`attest/text.go`, |
| 331 | `schema/attestation.cue`). Its subject is the tree digest. A pure statement records the derivation, |
| 332 | its inputs digest and the output. An effectful statement records the environment and the time of the |
| 333 | observation. The envelope is a signed note: the `sumdb/note` format with Ed25519, implemented in |
| 334 | `note/note.go`. Checked by `attest-e2e`, `attest-conformance`, `attest-schema` and `note-unit`. |
| 335 | |
| 336 | The integrator reads evidence at the head's tree and verifies each note with `attest verify` |
| 337 | (`integrator/evidence.go`). It does not run the checks itself. A required check is met when its note |
| 338 | is signed by a known signer, says the check `passed`, and one of these holds |
| 339 | (`integrator/verdict/verdict.go`): |
| 340 | |
| 341 | - **A pure check, unmoved tree.** The landed tree is the attested tree. No inputs are compared. |
| 342 | - **A pure check, same inputs.** `attest inputs` on the candidate gives the same inputs digest and |
| 343 | derivation hash. `attest inputs` evaluates the derivation and does not build it; evaluation can |
| 344 | still build anything the flake imports from a derivation. |
| 345 | - **An effectful check.** No class that requires it changed between the base and the tip, and the |
| 346 | evidence is younger than the check's `validity` window. |
| 347 | |
| 348 | Checked by `integrator-unit` (the verdict tests) and `integrator-e2e`. |
| 349 | |
| 350 | Check definitions come from the change being judged. `attest` builds the flake checks of the |
| 351 | exported tree, so a change can edit the check it is judged by |
| 352 | ([bd-eaefe82](../archive/.the-valley/bugs/bd-eaefe82-check-definitions-come-from-the-branch.md), |
| 353 | [ida-a9e274c](../archive/.the-valley/ideas/ida-a9e274c-mandated-checks-come-from-the-instance.md)). |
| 354 | the-valley's project layer declares no class for its Go, Nix and CUE paths. Whether the instance |
| 355 | floor adds one depends on qinling's private policy. |
| 356 | |
| 357 | ## Identity |
| 358 | |
| 359 | The identity registry is CUE under `identity/` in the instance repository, read at its `main` |
| 360 | (`identity/registry.go`, `schema/identity.cue`). It declares principals and boundaries. |
| 361 | |
| 362 | - A principal has a kind (`human`, `machine` or `service`), one or more `ssh-ed25519` keys, and |
| 363 | grants. A key may also sign attestations, under a name it declares. |
| 364 | - A grant names a boundary. A boundary has a kind: `git-push`, `registry` or `request`. |
| 365 | - A machine, service or external principal must declare `expires`. From that day on, a successful |
| 366 | compile leaves the principal out of every file. A compile that is refused keeps the previous |
| 367 | files, expired entries included. Keys the host declares statically are outside the registry and do |
| 368 | not expire. |
| 369 | |
| 370 | `valley-identity` compiles the registry every 5 minutes, as git (`nix/valley-host.nix`, |
| 371 | `identity/compile.go`, `identity/render.go`). It writes three files under |
| 372 | `/var/lib/valley-identity/`: |
| 373 | |
| 374 | - `authorized_keys.git`: one line per key of a principal with a `git-push` grant, tagged |
| 375 | `environment="VALLEY_PRINCIPAL=<principal>"`. sshd reads it after the host's declared keys. |
| 376 | - `known-signers`: the keys that may sign attestations, for the hook and the integrator. |
| 377 | - `grants`: one `request <principal>` line per holder of the `request` grant. |
| 378 | |
| 379 | The compiler refuses a registry where no principal holds a registry grant, where one key pushes for |
| 380 | two principals, or where a pushing key matches a host-declared key under a different tag or none. |
| 381 | Keys that only sign are not checked for collisions. It computes everything before it writes |
| 382 | anything, so a refused registry leaves the last good files in place. Checked by `identity-e2e` and |
| 383 | `identity-schema`, and by the Go tests in `identity/identity_test.go`. Why identity is a governed |
| 384 | registry, and why machine keys expire, is in |
| 385 | [dcr-b87f6e8](../archive/.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) and |
| 386 | [bd-8a591dc](../archive/.the-valley/bugs/bd-8a591dc-machine-credentials-never-expire.md). |
| 387 | |
| 388 | ## sigverify |
| 389 | |
| 390 | `sigverify` verifies OpenSSH SSHSIG signatures and signed git tags against an `allowed_signers` file |
| 391 | (`sigverify/`). It exists because `ssh-keygen -Y verify` and `git verify-tag` never check the |
| 392 | hardware key's flags. A key file edited with `ssh-keygen -p -O no-touch-required` signs without a |
| 393 | touch, and those tools accept the signature. |
| 394 | |
| 395 | - The default class, `fido-sk`, takes only security-key signatures. It requires the user-presence |
| 396 | bit. It also requires user verification under `--require-uv` or a `verify-required` signer line. |
| 397 | - The `tkey-signer` class takes an `ssh-ed25519` key marked as a TKey key in `allowed_signers`. The |
| 398 | verifier cannot tell a TKey key from any other ed25519 key, so the mark is trusted at enrollment. |
| 399 | - Plain software keys pass only with `--allow-non-sk` or `--allow-class software`. |
| 400 | |
| 401 | Checked by `sigverify-unit` and `sigverify-e2e`, including a touchless-signature negative test. |
| 402 | Nothing in the-valley or cosmo calls `sigverify` yet. |
| 403 | |
| 404 | ## The valley command |
| 405 | |
| 406 | `bin/valley` is a shell script, also packaged with its schema (`nix/packages.nix`). Its `status`, |
| 407 | `ready`, `unready` and `withdraw` verbs are a Go program, `valley-go` (`status/`), which the package |
| 408 | puts on the script's `PATH`. |
| 409 | |
| 410 | | Command | What it does | |
| 411 | | ---------------------------- | ----------------------------------------------------------------------------------------------------- | |
| 412 | | `valley pending` | Lists branches not merged into `main`, each with its head commit's subject, and then drafts apart. | |
| 413 | | `valley review <b>` | Shows a branch and offers `[a]pprove`, `[r]eject` and `[s]kip`, plus `[b]ase` when it needs a rebase. | |
| 414 | | `valley approve <change>` | Does what `[a]pprove` does, without showing the branch. | |
| 415 | | `valley rebase <change>` | Does what `[b]ase` does, without showing the branch, and prints the new head. | |
| 416 | | `valley checks` | Reports the checks a change owes, grouped by declared cost. It runs nothing. | |
| 417 | | `valley status [--json] <c>` | Reads the request and outcome record for a change. `--json` prints the `valley-status/v1` object. | |
| 418 | | `valley ready [<branch>]` | Marks a head of the branch ready as a new version of its change, in the change's record. | |
| 419 | | `valley unready <change>` | Makes a ready change a draft, as its owner or the operator. | |
| 420 | | `valley withdraw <change>` | Marks a ready change or a draft withdrawn, as its owner or the operator. | |
| 421 | | `valley tail [subj]` | Subscribes to bus events. | |
| 422 | | `valley replay [dir]` | Publishes one `ref-updated` event per current ref. | |
| 423 | |
| 424 | `[a]pprove` is the operator's approval of a change. It runs the owed checks with `attest` in a |
| 425 | worktree of the branch head. Then one atomic push carries the evidence refs and the integration |
| 426 | request, with a lease on the request ref. When the request already names this head, the push leaves |
| 427 | the request ref out. When nothing is owed as well, there is no push. `[b]ase` rebases the branch in |
| 428 | a temporary worktree and pushes it with a lease. `[r]eject` deletes the remote branch after a |
| 429 | prompt. |
| 430 | |
| 431 | When the checks fail under `[a]pprove`, approving the same head again can only fail again. So the |
| 432 | prompt then says approving needs a new head. It offers only `[r]eject` and `[s]kip`, plus `[b]ase` |
| 433 | where it is offered. `[a]pprove` comes back once the change is at another head, as it is after |
| 434 | `[b]ase`. A refusal that is not a check failure, such as a lost lease race, keeps the prompt as it |
| 435 | was. |
| 436 | |
| 437 | `valley approve` and `valley rebase` are for front ends, such as the klaus dashboard, that show a |
| 438 | change their own way. Each runs the same function as its key, so the output and the refusals are the |
| 439 | same. `approve` exits non-zero when it refuses. `rebase` does not re-review the result, and it |
| 440 | leaves a branch that already fast-forwards from `main` alone. Checked by `valley-cli`, |
| 441 | `valley-request`, `valley-status`, `valley-ready` and `review-notes`. |
| 442 | |
| 443 | `valley ready [--head <sha>] [--change <id>] [--stamp] [<branch>]` writes a change's record |
| 444 | (`status/ready.go`). The branch is the current one unless named. The head is `--head` when given, |
| 445 | and otherwise the branch's head on origin. Either way the head must already be on origin. klaus |
| 446 | passes `--head` with the head it pushed, so the version names exactly that head. ready reads the |
| 447 | existing record, if there is one, and adds a version. For a ready change, it refuses a head equal to |
| 448 | the active version's. It pushes the record ref with a lease on the value it read, and prints the |
| 449 | handle and version, such as `cosmo nxvp v2 ready`. A draft or a withdrawn change comes back by |
| 450 | `ready` too. At the head of its active version, it is ready again at that version, and the record |
| 451 | commit's one parent is the record before it. At another head, it gains the next version. The record |
| 452 | names the principal from `git config valley.principal`. The host derives the pushing principal from |
| 453 | the key, and the client cannot ask it, so the hook refuses a record that names anyone else. |
| 454 | |
| 455 | ready takes the change ID from the first of these that has one: |
| 456 | |
| 457 | 1. `--change <id>`; |
| 458 | 2. the head commit's `change-id` header, as jj writes it, read from the commit object itself; |
| 459 | 3. a `Change-Id:` trailer in the head's message; |
| 460 | 4. a new ID, which it prints. |
| 461 | |
| 462 | A header and a trailer that disagree are refused, and so are malformed values. IDs on other commits |
| 463 | of the change are not read. ready also refuses a change whose commits, from the merge base with |
| 464 | `main` to the head, include a merge, or that has more than one merge base. `--stamp` amends the |
| 465 | local branch's head to carry the ID as a trailer, and pushes the branch with a lease. It only starts |
| 466 | a new change. It refuses a change that already has a record, so it never decides whose a change is |
| 467 | from `valley.principal`, which is only what the checkout claims. It also refuses a head that is |
| 468 | already on origin. |
| 469 | |
| 470 | A change is named by its full ID or by its handle. The handle is the shortest prefix of the ID, at |
| 471 | least four letters, that no other change recorded on origin shares. A change can also be named by |
| 472 | its branch, as it was before records existed. Every verb that takes a change takes all three |
| 473 | (`status/changes.go`). |
| 474 | |
| 475 | `review`, `approve` and `rebase` act on a change named by ID or handle at the head its active |
| 476 | version records. They never act on the branch's tip. A record's branch is only where its head is |
| 477 | expected to be, and anyone who can push the branch can move it, even in the same push as the record. |
| 478 | `review` shows the recorded head. `approve` files the request at exactly that head. `approve`, |
| 479 | `rebase` and `review`'s `[r]eject` refuse when the branch's tip on origin is another commit, and the |
| 480 | refusal names both commits. `[r]eject` deletes the branch under a lease on the recorded head. A |
| 481 | rebased head is a commit no version records, so the change is readied again before its handle names |
| 482 | it. `approve`, `rebase`, and `review`'s `[a]pprove` and `[b]ase` refuse a change whose record is not |
| 483 | `ready`, and the refusal names its state. `valley-go resolve` gives these verbs the change's ID, |
| 484 | handle, active version, recorded head, branch and state on one line. |
| 485 | |
| 486 | `valley unready <change>` and `valley withdraw <change>` write a record that changes the state alone |
| 487 | (`status/ready.go`). They take a change by any of its names, and print the handle, the active |
| 488 | version and the new state, such as `cosmo nxvp v2 draft`. Each warns when a request for the change |
| 489 | is pending, as [Change records](#change-records) describes. `valley pending` lists the changes whose |
| 490 | record says they are drafts in a section of their own, at the head of each active version. A branch |
| 491 | whose tip is a draft's head is listed only there. `valley-go drafts` lists them for it. |
| 492 | |
| 493 | `valley status` takes one `ls-remote` snapshot of origin and fetches the objects it names, without |
| 494 | moving a local branch or writing `FETCH_HEAD`. It reads ancestry first: a head already in |
| 495 | `origin/main` is integrated, even if recording the outcome failed. An outcome record it does not |
| 496 | recognise is `unknown`, never a success. |
| 497 | |
| 498 | ### The status object |
| 499 | |
| 500 | `valley status --json <b>` prints one JSON object on one line. Front ends read it, klaus among them, |
| 501 | so it is a contract with a version. A change to the name or meaning of any field is a new version. |
| 502 | |
| 503 | ```json |
| 504 | { |
| 505 | "schema": "valley-status/v1", |
| 506 | "change": "topic/one", |
| 507 | "request_name": "topic-one", |
| 508 | "target": "main", |
| 509 | "target_tip": "<sha>", |
| 510 | "head": "<sha>", |
| 511 | "request": { "ref": "refs/the-valley/integration-requests/main/topic-one", "commit": "<sha>" }, |
| 512 | "outcome": { "decision": "stale", "head": "<sha>", "tip": "<sha>", "failed_at": null }, |
| 513 | "state": "stale" |
| 514 | } |
| 515 | ``` |
| 516 | |
| 517 | - `change` is the branch as given, without `origin/`. `request_name` names its integration request: |
| 518 | the branch with each `/` replaced by `-`. |
| 519 | - `target_tip` is `main` on origin. `head` is the branch head on origin. When the branch is gone, |
| 520 | `head` is the request's commit, or else the outcome's head, or else `null`. |
| 521 | - `request` is `null` when origin holds no integration request for the change. `outcome` is `null` |
| 522 | when origin holds no outcome record. |
| 523 | - `outcome.decision` is `land`, `stale`, `reject`, `failed` or `unknown`. A record that does not |
| 524 | parse is `unknown`. `outcome.head` and `outcome.tip` are `null` unless they are object ids, and |
| 525 | `failed_at` is `null` unless the decision is `failed`. |
| 526 | - `state` is one of these: |
| 527 | |
| 528 | | `state` | What it means | |
| 529 | | ----------------------- | ------------------------------------------------------------------------------------- | |
| 530 | | `integrated` | The head is in `origin/main`. | |
| 531 | | `integrated-recorded` | A `land` outcome names this head. The change landed as a different commit. | |
| 532 | | `outdated-request` | The request names an older commit than the branch head. | |
| 533 | | `outcome-unreadable` | The outcome record does not parse. | |
| 534 | | `not-requested` | No request stands for the change. An outcome from an earlier head may remain. | |
| 535 | | `awaiting-decision` | A request stands for this head, and no outcome names it. | |
| 536 | | `awaiting-new-decision` | The outcome names this head, but `main` or the evidence has moved since it was made. | |
| 537 | | `stale` | The integrator reported the change stale against the current `main` and evidence. | |
| 538 | | `rejected` | The integrator rejected the evidence. | |
| 539 | | `failed` | The integrator failed while processing the request. The change may still have landed. | |
| 540 | |
| 541 | An error prints no object. The message goes to stderr and the exit status is non-zero. The errors |
| 542 | are an unreadable origin, an origin without `main`, nothing on origin for the change, and a change |
| 543 | name that is not UTF-8, which JSON cannot carry exactly. Checked by `valley-status`, and by the Go |
| 544 | tests in `status/` for the outcome record's parser. |
| 545 | |
| 546 | ## The life of a change |
| 547 | |
| 548 | 1. An author pushes a topic branch. The hook accepts it, the bus gets `ref-updated`, and the mirrors |
| 549 | prune it. |
| 550 | 2. The operator approves the change, with `[a]pprove` in `valley review <branch>` or with |
| 551 | `valley approve <branch>`. The owed checks run, and one push files the evidence and the |
| 552 | integration request. The hook verifies each note and the `request` grant. |
| 553 | 3. On its next pass the integrator builds the candidate, derives the policy, judges the evidence, |
| 554 | and lands or refuses. |
| 555 | 4. On a landing, the publisher sends `ref-updated` to the bus and pushes `main` and tags to the |
| 556 | mirrors. `valley status <branch>` shows the outcome. |
| 557 | |
| 558 | ## How cosmo deploys it |
| 559 | |
| 560 | cosmo's `hosts/classic-laddie/default.nix` imports `the-valley.nixosModules.valley-host` and points |
| 561 | `services.valley.config` at `hosts/classic-laddie/valley.cue`. It turns on the bus, the integrator |
| 562 | and the identity compiler, with the private repository `qinling` as the instance project. The host |
| 563 | declares a small set of static keys beside the compiled registry keys. |
| 564 | |
| 565 | `valley.cue` declares two projects, `the-valley` and `qinling`. Both protect `main`. `the-valley` |
| 566 | mirrors to GitHub at `gunk-dev/the-valley`, and `qinling` has no mirror. The mirror push uses the |
| 567 | git user's ssh key, from an agenix secret. Backups are a nightly restic run of `/srv/git` to an |
| 568 | offsite Storage Box over sftp, kept 7 daily, 4 weekly and 6 monthly. The backup target still needs |
| 569 | provisioning, so the run logs a failure until it is set up. |
| 570 | |
| 571 | ## Not deployed or not wired yet |
| 572 | |
| 573 | | What | State | |
| 574 | | ------------------------------------------- | -------------------------------------------------------------------------------- | |
| 575 | | `sigverify` | On `main` and tested. Nothing calls it. | |
| 576 | | Hardware-backed keys in the registry | `schema/identity.cue` accepts `ssh-ed25519` keys only. | |
| 577 | | SSHSIG evidence | Attestations use the signed-note format. | |
| 578 | | Approvals | The integrator checks evidence only. No approval is required for any path class. | |
| 579 | | A crash-safe landing ledger | Landing is several separate ref updates. | |
| 580 | | Policy and registry snapshot in the outcome | The outcome record holds the head, the tip and an evidence hash. | |
| 581 | | Registry compile on a qinling landing | The compiler runs on its 5-minute timer only. | |
| 582 | | Bus authentication | The bus listens on localhost with no authentication. | |
| 583 | | Other projects on the valley | cosmo and klaus are not hosted on the valley. | |