gunk-dev/the-valley

architecture.md

source71e69b2e4721main's published tip history
54.9 KiB · Rendered
1# Architecture as deployed
2
3This document describes what the-valley runs today, read from the code. It makes three kinds of
4statement. Behaviour is what the code does, cited to the files that do it. A deployment record
5reports an observation of a host, and names the record and its date. A guarantee names the flake
6check (`checks.x86_64-linux.<name>`) or test that demonstrates it; a statement with no named check
7is 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
12A valley host keeps bare git repositories and accepts changes to them over ssh. Anyone with a key
13may push a topic branch. A protected branch such as `main` accepts a push only from the principals
14the project names as its writers. The integrator moves it by landing an integration request. The
15integrator lands a request when every check the project's policy requires is covered by a signed
16statement that the check passed.
17
18The 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
30The NixOS module `services.valley` reads one CUE file, `services.valley.config`. The build vets it
31against `schema/valley.cue` and fails on any error (`nix/valley-host.nix`). The file declares the
32projects, their mirrors, their protection and the backup policy. Checked by `module-eval` and
33`cue-vet`.
34
35### Repositories and the git user
36
37Each project is a bare repository at `/srv/git/<name>.git`. All ssh git traffic logs in as one Unix
38user, `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
411. 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.
452. 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.
483. It runs `git-shell` with the same arguments. `git-shell` limits the session to git's own
49 commands.
50
51The 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)
53forwarding 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
55client set `VALLEY_PRINCIPAL` or `GIT_*` (`nix/valley-host.nix`). Checked by `ssh-e2e` (a real sshd
56in 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
61converged record, so pushes are refused while it runs. It then creates any missing repository and
62links each repository's hooks. A foreign `pre-receive` hook or any `core.hooksPath` setting is a
63conflict. When there is no conflict, it writes the converged record. The record is the first 32 hex
64characters 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
66is not a conflict. Checked by `init-e2e` and `ssh-e2e`.
67
68A host with an instance repository also publishes its project list there. `valley-init` writes a
69blob at `refs/the-valley/projects` that names every enabled git project, one per line, sorted, and
70nothing else. Clients read it to find the host's projects (`valley changes --all`). It discloses
71every project name to whoever can read the instance repository. A host without an instance
72repository publishes nothing. A list that cannot be written is a warning, not a conflict. Checked by
73`init-e2e` and `valley-changes`.
74
75The hold file belongs to the operator, who creates it to pause all pushes. `valley-init` never
76touches it.
77
78## What a push may write
79
80Every repository's `pre-receive` hook runs `valleyhook pre-receive` (`valleyhook/main.go`,
81`valleyhook/policy.go`). The hook is default-deny. If any one ref update is refused, the whole push
82is refused. A push may update at most 1000 refs, and the hook refuses one that updates more before
83it decides any ref. It asks `git symbolic-ref` whether each pushed ref is symbolic, so it reads a
84symbolic ref exactly as git does. Checked by `protect-e2e`, `ssh-e2e` and `valleyhook-unit`.
85
86| Ref namespace | Who may push | Rule |
87| --------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
88| `refs/heads/*` (topic branches) | Any key | Create, update and delete. |
89| 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. |
90| `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`). |
91| `refs/the-valley/integration-requests/<target>/<c>` | Principals with the `request` grant | Create, update and delete. |
92| `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`). |
93| `refs/the-valley/projects` (instance repository) | Nobody | `valley-init` writes it on disk: the host's project list (see [valley-init](#valley-init-and-the-push-hold)). |
94| Every other `refs/the-valley/*` | Nobody | This covers the integrator's outcome records. |
95| `refs/replace/*`, `refs/notes/*` | Nobody | A replace ref would let git show other content under a watched name. |
96| Symbolic refs | Nobody | |
97| 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`. |
98
99The `request` grant comes from two places: the host's declared `services.valley.grants.request` and
100the identity compiler's `grants` file. Why replace refs are closed is recorded in
101[bd-75c8721](../archive/.the-valley/bugs/bd-75c8721-pushes-could-write-any-namespace.md).
102
103### Change records
104
105A change is one piece of work aimed at `main`. Its ID is stable across rebases, in jj's form: 32
106letters from `k` to `z`. Each time a head of the change is marked ready, the change gains a version.
107The change's record lists the versions, and the hook guards every write to it. Records are
108observational: nothing reads them for authority. The integration request is still the only way a
109change reaches `main`, and the integrator does not read records.
110
111A change is in one of three states:
112
113- `ready`: the change is offered for landing. A change is created ready.
114- `draft`: the change is paused, by `valley unready`. A draft voids nothing. When it is readied
115 again, the approvals of its active version still stand.
116- `withdrawn`: the change is given up. Withdrawing voids everything outstanding on the change: its
117 integration requests, and the approvals and reviews of every version. A change brought back from
118 withdrawn always needs fresh approvals.
119
120Nothing enforces what withdrawing voids yet. The integrator does not read records, so a request
121filed before a change was paused or withdrawn can still land it. Until it does read them,
122`valley unready` and `valley withdraw` warn when origin holds a request for the change. The request
123is looked for under the name `approve` gives it, derived from the branch of each of the change's
124versions, and under `c-<change-id>`. Only a `request` holder may touch a request ref, so the warning
125says the operator must drop it. Approvals are not records yet either; they become records with M1-C.
126Until then, voiding an approval means the integrator refuses it, once the integrator reads records.
127
128The ref `refs/the-valley/changes/main/<change-id>` points at a record commit. The commit's tree
129holds one entry, `record.json`, as a plain file. Its schema is `valley-change-record/v1`, read by
130one strict parser that the hook and the CLI share (`change/record.go`):
131
132```json
133{
134 "schema": "valley-change-record/v1",
135 "change_id": "<32 letters from k to z>",
136 "target": "main",
137 "owner": "<principal>",
138 "state": "ready",
139 "seq": 3,
140 "versions": [
141 {
142 "n": 1,
143 "head": "git-sha1:<40 lowercase hex>",
144 "title": "<the head's subject>",
145 "branch": "<branch, or empty>",
146 "uploader": "<principal>"
147 }
148 ],
149 "active": 1
150}
151```
152
153The parser refuses anything that is not valid UTF-8. It also refuses a duplicate key at any depth,
154an unknown field, and data after the object. Every field must have the type shown. `seq` runs from 1
155to 100000, and `n` and `active` from 1 to 1000. Versions are numbered 1, 2, 3 with no gap. A title
156is 1 to 200 bytes with no control characters. `state` is `ready`, `draft` or `withdrawn`.
157
158The record commit's parents tie it to what it records. In the table, `P` is the record commit's
159parents, `old` is the ref's value before the push, and `h` is the head of the version being added.
160
161| Transition | Who | `P` | What the record must say |
162| ------------------------- | ------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
163| 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. |
164| 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. |
165| 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. |
166| 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. |
167| 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. |
168| Ready again, new version | The owner or a `request` holder | `[old, h]` | As for a new version, from `draft` or `withdrawn`. |
169| Delete | A `request` holder | — | Allowed, to keep storage tidy. |
170
171The hook refuses every other write. That includes a key that names no principal, extra parents,
172parents in another order, a head that is not a commit, and a tree of another shape. It also includes
173a record with one parent that leaves the state as it was, such as `ready` to `ready` without a new
174version, and `withdrawn` to `draft`. A withdrawn change comes back ready, never as a draft. The
175owner, the target and the change ID never change.
176
177Each record commit is at most 4 KiB, its tree at most 1 KiB, and its `record.json` at most 64 KiB. A
178push may update at most 32 records. The hook checks each of these bounds before it reads anything
179past it.
180
181A repository may hold at most 2000 records, counted with one `for-each-ref`. This bound is soft. A
182pre-receive hook runs before git installs any ref, and nothing serializes two pushes' hooks. So
183pushes that run at the same time can each count the same records and each create more. The count can
184then pass 2000 by at most the record updates those pushes had in flight, which is 32 per push. Once
185the count is past 2000, every later create is refused.
186
187The writer numbers versions, and the hook holds them to append-only and gap-free. When the
188integrator starts reading records, it takes this numbering as it is. For now, a change is owned by
189the principal whose key pushed its first record, so `klaus-env` owns every agent change.
190
191A record keeps every head it names reachable, because each head is a parent of a record commit. So
192records retain source objects the way topic branches do, and the hook restricts them no further.
193
194Checked by `valleyhook-unit`, with a case for each rule of each transition against real git objects,
195by `change-unit`, which runs the parser's tests and its fuzz target's seed corpus, and by
196`valley-ready`, which drives the transitions through real pushes.
197
198## After a push: events and mirrors
199
200Each repository's `post-receive` hook runs every program in `hooks/post-receive.d/`
201(`nix/valley-host.nix`). There are two:
202
203- **The bus publisher**, when the bus is on. It publishes one `ref-updated` event for every ref the
204 push updated, on the subject `valley.git.<repo>.ref-updated`. Checked by `bus-e2e`.
205- **The mirror hook**, when the project declares mirrors. It does not push. It writes the push's ref
206 moves, one `<old> <new> <ref>` line each, as a file in the repository's publish queue,
207 `valley-publish-queue/`, and returns. A dead mirror therefore never slows or fails a push.
208
209The integrator moves refs with `git update-ref`, which runs no `post-receive` hook. So it does both
210jobs itself for each landing (`integrator/bus.go`). It writes the move to the same publish queue. It
211also publishes the `ref-updated` event straight to the bus, with the same payload the bus publisher
212would send. It publishes only after the landing is fully recorded: the queue entry written, the
213evidence stored and the request consumed. Each publish is stopped after 10 seconds, so a bus that
214hangs cannot hold up a landing. Checked by `integrator-e2e`.
215
216One unit pushes the mirrors: `valley-publish@<project>`, a service that runs as git. A systemd path
217unit starts it whenever the project's queue is not empty. Mirrors are a list of URLs
218(`schema/valley.cue`). The service lists the queued files and then runs `git push --prune` of `main`
219and all tags to each URL. After that it deletes every other branch on the mirror. Tags are copied
220whether or not `main` reaches them, refs outside `refs/heads/` and `refs/tags/` on the mirror are
221left alone, and the branch deletion is best effort. The push uses the git user's own ssh identity,
222which the host provides. Checked by `mirror-e2e`.
223
224The service deletes the files it listed only after every mirror push succeeds. When a push or a
225deletion fails, the service fails and the files stay. systemd then runs the service again after a
226delay that grows from 30 seconds to 15 minutes. A run that lasts 15 minutes is stopped with
227everything it started. Checked by `publish-e2e`. Because the integrator publishes its events itself,
228a mirror outage never delays an event on the bus.
229
230Having one publisher is what keeps a mirror from being rewound. git reads the local refs before it
231contacts the mirror. So when two pushes run at once, one can read an old `main`, stall, and then
232force-push that old `main` over a newer one the other pushed meanwhile. systemd never runs two
233instances of one unit at the same time, and nothing else pushes the mirrors. A move queued while a
234push runs stays in the queue, so the next run pushes the newer `main`. `mirror-e2e` stalls a push in
235that window: two pushers at once rewind the mirror, and the service ends at the newer `main`.
236
237That cosmo's deployed hosts run a publish queue is a deployment record: cosmo #934 merged and laddie
238healthy, in the handoff of 2026-10-04. At that point the service also published the integrator's
239events, and a push ran its own mirror pusher.
240
241The bus is NATS with JetStream, listening on `127.0.0.1:4222`. `valley-bus-init` creates one stream,
242`valley`, over the subjects `valley.>`, with file storage and NATS defaults. The events are defined
243in `schema/events.cue`: `ref-updated`, `integration-succeeded` and `request-stale`. The bus has no
244authentication, so nothing treats an event as authoritative
245([bd-d853d9c](../archive/.the-valley/bugs/bd-d853d9c-bus-unauthenticated.md)). The integrator reads
246git directly.
247
248## The integrator
249
250Each protected project has one controller, `valley-integrator@<project>`, running as the
251`valley-integrator` user in group git (`nix/valley-host.nix`). It makes one pass over the open
252requests in order, then sleeps 15 seconds and starts again (`integrator/main.go`,
253`integrator/refs.go`). No bound on how long a request waits is checked.
254
2551. **Find requests.** A request is a ref `refs/the-valley/integration-requests/<target>/<change>`
256 that points at the change's head commit.
2572. **Skip what is decided.** The outcome record
258 `refs/the-valley/integration-outcomes/<target>/<change>` holds the head, the target tip, a hash
259 of all attestation refs, and the outcome. If the first three still match, the request is skipped.
260 A `failed` outcome is retried after 10 minutes.
2613. **Build the candidate.** The base is `git merge-base` of the tip and the head. If the base is the
262 tip, the candidate is the head. Otherwise `git merge-tree --write-tree` builds the tree, and the
263 integrator commits it with the tip as the only parent. The commit keeps the head's author. Its
264 committer is the integrator at `integrator@the-valley.invalid`. A conflict makes the request
265 stale.
2664. **Derive the required checks** (see Policy below).
2675. **Judge the evidence** (see Evidence below). The outcome is `land`, `stale` or `reject`. A stale
268 request publishes `request-stale`.
2696. **Land.** The integrator signs a transfer statement that records the policy digest and the checks
270 it relied on. Then it moves the target with compare-and-swap
271 (`git update-ref <target> <new> <tip>`). That is the commit point. Then it queues the landing for
272 the publisher, stores its countersigned evidence, deletes the request with compare-and-swap,
273 publishes `integration-succeeded`, and writes the outcome record.
274
275Each step is a separate `git update-ref` call. A crash between steps can leave a landing without its
276evidence or outcome record. Checked by `integrator-e2e` (scenarios for fast-forward, merge,
277conflict, stale evidence, tampered notes, retries and a moved target) and `integrator-unit`.
278
279## Policy
280
281A project's required checks come from three CUE layers, unified as
282`floor & templates[type] & project` (`schema/verification.cue`):
283
284- **The schema**, `schema/verification.cue`, ships inside the `valley` and integrator packages
285 (`nix/packages.nix`).
286- **The floor and templates** come from the owning valley's instance repository, at
287 `policy/instance/*.cue` on its `main`. `policy/valley` names that repository. A value the floor
288 writes as `true` is mandatory. A template value written `bool | *true` is a default a project may
289 turn off.
290- **The project layer** is `policy/project/*.cue`, read at the tip of the target branch.
291
292Both layers are read from integrated tips, never from the change being judged
293(`integrator/policy.go`). A path class maps path globs to checks. A change owes the union of the
294checks of every class one of its paths matches. A path no class matches owes the `unclassified`
295checks. The integrator gets this answer by running `valley checks`, which diffs with renames counted
296on both sides. Checked by `policy-deriver` and `cue-vet`.
297
298The derivation fails closed. A failed read of the diff, the target, a policy layer or the composed
299policy makes `valley checks` exit non-zero and print no report. Without that, the failure would read
300as an empty list, and an empty list derives a change that owes nothing. The integrator also refuses
301output that is not a whole report: no header, no required-checks section, or a required-checks
302heading with no checks under it. It believes a report of no changed paths only when git agrees that
303the two trees are the same. Checked by `policy-deriver`, `valley-request` and `integrator-unit`.
304
305A check also declares a `cost`, `quick` by default. A `slow` check takes minutes, or needs special
306hardware such as KVM. `valley checks` reads the cost from the composition it derives from. After the
307report it prints two lines, `quick:` and `slow:`, each listing the owed checks of that cost.
308`valley checks --names` prints the owed check names alone, one per line, and `--cost quick` or
309`--cost slow` keeps one set. The integration gate does not read the cost. Approving runs every owed
310check, and the integrator requires evidence for every owed check, whatever its cost. The cost
311changes neither which checks a change owes nor what the integrator accepts. Checked by
312`policy-deriver` and `integrator-unit`.
313
314the-valley's own project layer is `policy/project/policy.cue`. It gives each part of this repository
315a class that owes the checks reading that part. A Go module owes its unit tests, and its end-to-end
316check where it has one. The CLI owes its script and status checks. The Nix, the schemas and the
317policy owe their evaluations and vets. The NixOS module also owes the end-to-end checks that run
318what it renders, and two of those boot NixOS machines. A change to `flake.nix` or `flake.lock` does
319not owe them, so a nixpkgs bump boots no machine. A check's own files, such as its script under
320`nix/checks/`, owe that check. A change to documentation alone owes the floor's `prose-format` and
321nothing else. A project layer that stopped composing would stop every derivation once it landed,
322including the derivation of its own fix. So the `own-policy` check composes the layer with the
323example floor before it lands. It fails if the layer names a check this flake does not define, if a
324script under `nix/checks/` owes no check of its own, or if a docs-only change would owe more than
325`prose-format`.
326
327The reasons for CUE here, and for the mandatory-versus-default encoding, are in
328[dcr-0f5d9b1](../archive/.the-valley/decisions/dcr-0f5d9b1-cue-config-host-module.md) and
329[dcr-f41f718](../archive/.the-valley/decisions/dcr-f41f718-declared-verification-policy.md).
330
331## Evidence: attest and notes
332
333`attest run` runs checks against a commit exported with `git archive` (`attest/run.go`,
334`attest/check.go`). A `nix` check builds the flake check's derivation. A `command` check runs a
335shell command in the exported tree. If every check passes, `attest` writes a statement per check,
336signs it, and stores it under `refs/the-valley/attestations/<tree>/<key-hash>`.
337
338A statement is line-based text that opens with `the-valley/attestation/v1` (`attest/text.go`,
339`schema/attestation.cue`). Its subject is the tree digest. A pure statement records the derivation,
340its inputs digest and the output. An effectful statement records the environment and the time of the
341observation. The envelope is a signed note: the `sumdb/note` format with Ed25519, implemented in
342`note/note.go`. Checked by `attest-e2e`, `attest-conformance`, `attest-schema` and `note-unit`.
343
344The integrator reads evidence at the head's tree and verifies each note with `attest verify`
345(`integrator/evidence.go`). It does not run the checks itself. A required check is met when its note
346is signed by a known signer, says the check `passed`, and one of these holds
347(`integrator/verdict/verdict.go`):
348
349- **A pure check, unmoved tree.** The landed tree is the attested tree. No inputs are compared.
350- **A pure check, same inputs.** `attest inputs` on the candidate gives the same inputs digest and
351 derivation hash. `attest inputs` evaluates the derivation and does not build it; evaluation can
352 still build anything the flake imports from a derivation.
353- **An effectful check.** No class that requires it changed between the base and the tip, and the
354 evidence is younger than the check's `validity` window.
355
356Checked by `integrator-unit` (the verdict tests) and `integrator-e2e`.
357
358Check definitions come from the change being judged. `attest` builds the flake checks of the
359exported tree, so a change can edit the check it is judged by
360([bd-eaefe82](../archive/.the-valley/bugs/bd-eaefe82-check-definitions-come-from-the-branch.md),
361[ida-a9e274c](../archive/.the-valley/ideas/ida-a9e274c-mandated-checks-come-from-the-instance.md)).
362the-valley's project layer declares no class for its Go, Nix and CUE paths. Whether the instance
363floor adds one depends on qinling's private policy.
364
365## Identity
366
367The identity registry is CUE under `identity/` in the instance repository, read at its `main`
368(`identity/registry.go`, `schema/identity.cue`). It declares principals and boundaries.
369
370- A principal has a kind (`human`, `machine` or `service`), one or more `ssh-ed25519` keys, and
371 grants. A key may also sign attestations, under a name it declares.
372- A grant names a boundary. A boundary has a kind: `git-push`, `registry` or `request`.
373- A machine, service or external principal must declare `expires`. From that day on, a successful
374 compile leaves the principal out of every file. A compile that is refused keeps the previous
375 files, expired entries included. Keys the host declares statically are outside the registry and do
376 not expire.
377
378`valley-identity` compiles the registry every 5 minutes, as git (`nix/valley-host.nix`,
379`identity/compile.go`, `identity/render.go`). It writes three files under
380`/var/lib/valley-identity/`:
381
382- `authorized_keys.git`: one line per key of a principal with a `git-push` grant, tagged
383 `environment="VALLEY_PRINCIPAL=<principal>"`. sshd reads it after the host's declared keys.
384- `known-signers`: the keys that may sign attestations, for the hook and the integrator.
385- `grants`: one `request <principal>` line per holder of the `request` grant.
386
387The compiler refuses a registry where no principal holds a registry grant, where one key pushes for
388two principals, or where a pushing key matches a host-declared key under a different tag or none.
389Keys that only sign are not checked for collisions. It computes everything before it writes
390anything, so a refused registry leaves the last good files in place. Checked by `identity-e2e` and
391`identity-schema`, and by the Go tests in `identity/identity_test.go`. Why identity is a governed
392registry, and why machine keys expire, is in
393[dcr-b87f6e8](../archive/.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) and
394[bd-8a591dc](../archive/.the-valley/bugs/bd-8a591dc-machine-credentials-never-expire.md).
395
396## sigverify
397
398`sigverify` verifies OpenSSH SSHSIG signatures and signed git tags against an `allowed_signers` file
399(`sigverify/`). It exists because `ssh-keygen -Y verify` and `git verify-tag` never check the
400hardware key's flags. A key file edited with `ssh-keygen -p -O no-touch-required` signs without a
401touch, and those tools accept the signature.
402
403- The default class, `fido-sk`, takes only security-key signatures. It requires the user-presence
404 bit. It also requires user verification under `--require-uv` or a `verify-required` signer line.
405- The `tkey-signer` class takes an `ssh-ed25519` key marked as a TKey key in `allowed_signers`. The
406 verifier cannot tell a TKey key from any other ed25519 key, so the mark is trusted at enrollment.
407- Plain software keys pass only with `--allow-non-sk` or `--allow-class software`.
408
409Checked by `sigverify-unit` and `sigverify-e2e`, including a touchless-signature negative test.
410Nothing in the-valley or cosmo calls `sigverify` yet.
411
412## The valley command
413
414`bin/valley` is a shell script, also packaged with its schema (`nix/packages.nix`). Its `status`,
415`ready`, `unready`, `withdraw`, `changes` and `pending` verbs are a Go program, `valley-go`
416(`status/`), which the package puts on the script's `PATH`.
417
418| Command | What it does |
419| ---------------------------- | ----------------------------------------------------------------------------------------------------- |
420| `valley pending` | Lists the ready changes not yet landed, then the branches no change is on, then the drafts. |
421| `valley changes --json` | Prints every change in every state, across the host with `--all`, as `valley-changes/v1`. |
422| `valley review <b>` | Shows a branch and offers `[a]pprove`, `[r]eject` and `[s]kip`, plus `[b]ase` when it needs a rebase. |
423| `valley approve <change>` | Does what `[a]pprove` does, without showing the branch. |
424| `valley rebase <change>` | Does what `[b]ase` does, without showing the branch, and prints the new head. |
425| `valley checks` | Reports the checks a change owes, grouped by declared cost. It runs nothing. |
426| `valley status [--json] <c>` | Reads the request and outcome record for a change. `--json` prints the `valley-status/v1` object. |
427| `valley ready [<branch>]` | Marks a head of the branch ready as a new version of its change, in the change's record. |
428| `valley unready <change>` | Makes a ready change a draft, as its owner or the operator. |
429| `valley withdraw <change>` | Marks a ready change or a draft withdrawn, as its owner or the operator. |
430| `valley tail [subj]` | Subscribes to bus events. |
431| `valley replay [dir]` | Publishes one `ref-updated` event per current ref. |
432
433`[a]pprove` is the operator's approval of a change. It runs the owed checks with `attest` in a
434worktree of the branch head. Then one atomic push carries the evidence refs and the integration
435request, with a lease on the request ref. When the request already names this head, the push leaves
436the request ref out. When nothing is owed as well, there is no push. `[b]ase` rebases the branch in
437a temporary worktree and pushes it with a lease. `[r]eject` deletes the remote branch after a
438prompt.
439
440When the checks fail under `[a]pprove`, approving the same head again can only fail again. So the
441prompt then says approving needs a new head. It offers only `[r]eject` and `[s]kip`, plus `[b]ase`
442where it is offered. `[a]pprove` comes back once the change is at another head, as it is after
443`[b]ase`. A refusal that is not a check failure, such as a lost lease race, keeps the prompt as it
444was.
445
446`valley approve` and `valley rebase` are for front ends, such as the klaus dashboard, that show a
447change their own way. Each runs the same function as its key, so the output and the refusals are the
448same. `approve` exits non-zero when it refuses. `rebase` does not re-review the result, and it
449leaves a branch that already fast-forwards from `main` alone. Checked by `valley-cli`,
450`valley-request`, `valley-status`, `valley-ready` and `review-notes`.
451
452`valley ready [--head <sha>] [--change <id>] [--stamp] [<branch>]` writes a change's record
453(`status/ready.go`). The branch is the current one unless named. The head is `--head` when given,
454and otherwise the branch's head on origin. Either way the head must already be on origin. klaus
455passes `--head` with the head it pushed, so the version names exactly that head. ready reads the
456existing record, if there is one, and adds a version. For a ready change, it refuses a head equal to
457the active version's. It pushes the record ref with a lease on the value it read, and prints the
458handle and version, such as `cosmo nxvp v2 ready`. A draft or a withdrawn change comes back by
459`ready` too. At the head of its active version, it is ready again at that version, and the record
460commit's one parent is the record before it. At another head, it gains the next version. The record
461names the principal from `git config valley.principal`. The host derives the pushing principal from
462the key, and the client cannot ask it, so the hook refuses a record that names anyone else.
463
464ready takes the change ID from the first of these that has one:
465
4661. `--change <id>`;
4672. the head commit's `change-id` header, as jj writes it, read from the commit object itself;
4683. a `Change-Id:` trailer in the head's message;
4694. a new ID, which it prints.
470
471A header and a trailer that disagree are refused, and so are malformed values. IDs on other commits
472of the change are not read. ready also refuses a change whose commits, from the merge base with
473`main` to the head, include a merge, or that has more than one merge base. `--stamp` amends the
474local branch's head to carry the ID as a trailer, and pushes the branch with a lease. It only starts
475a new change. It refuses a change that already has a record, so it never decides whose a change is
476from `valley.principal`, which is only what the checkout claims. It also refuses a head that is
477already on origin.
478
479A change is named by its full ID or by its handle. The handle is the shortest prefix of the ID, at
480least four letters, that no other change recorded on origin shares. A change can also be named by
481its branch, as it was before records existed. Every verb that takes a change takes all three
482(`status/changes.go`).
483
484`review`, `approve` and `rebase` read a branch name further, whether it is written as `<branch>` or
485`origin/<branch>`. They read every record on origin and look for the ones whose active version is on
486that branch:
487
488- If there are none, the name is a branch, as it was before records existed.
489- If there is exactly one, the name is that record's change, exactly as if its ID had been given.
490- If there is more than one, the verb refuses and lists their change IDs, so that one can be named.
491
492So whether a change's record is read never depends on how the change is named. These three verbs
493need the `valley-go` helper for that reason.
494
495`review`, `approve` and `rebase` act on a change with a record at the head its active version
496records. They never act on the branch's tip. A record's branch is only where its head is expected to
497be, and anyone who can push the branch can move it, even in the same push as the record. `review`
498shows the recorded head. `approve` files the request at exactly that head. `approve`, `rebase` and
499`review`'s `[r]eject` refuse when the branch's tip on origin is another commit, and the refusal
500names both commits. `[r]eject` deletes the branch under a lease on the recorded head. A rebased head
501is a commit no version records, so the change is readied again before its handle names it.
502`approve`, `rebase`, and `review`'s `[a]pprove` and `[b]ase` refuse a change whose record is not
503`ready`, and the refusal names its state. `valley-go resolve` gives these verbs the change's ID,
504handle, active version, recorded head, branch and state on one line.
505
506The record can change while one of these verbs runs. Another principal may unready or withdraw the
507change, or ready a new version, while a review is open or the checks run. So `approve` reads the
508record again before it runs the checks, and again before it files the request. `rebase` reads it
509again before it starts, and again before it pushes. Each refuses when the change is no longer
510`ready`, or when its active version is at another head than the one the verb began with. Nothing on
511origin makes this check until the integrator reads records.
512
513A name that read as a plain branch can come to name a change while the verb runs. For example,
514someone readies the branch, and then unreadies it, while a review of it is open. So before `approve`
515files and before `rebase` pushes, the verb reads the original name again. If it names a change by
516then, the same rules hold: the change must be `ready`, and its recorded head must be the head the
517verb began with.
518
519`valley unready <change>` and `valley withdraw <change>` write a record that changes the state alone
520(`status/ready.go`). They take a change by any of its names, and print the handle, the active
521version and the new state, such as `cosmo nxvp v2 draft`. Each warns when a request for the change
522is pending, as [Change records](#change-records) describes. `valley pending` lists the changes whose
523record says they are drafts in a section of their own, at the head of each active version. A branch
524whose tip is a draft's head is listed only there.
525
526`unready` and `withdraw` stop an approval that begins after them. They do not always stop one that
527is already under way. An approval reads the record one last time just before it files the request,
528and a change unreadied or withdrawn after that read is filed all the same. No client can close this
529window. git compares only the refs a push changes, and the record ref is not one of them, so a push
530cannot be made conditional on the record. The authoritative check is planned in the integrator: it
531will refuse to land any request whose head is a version of a change that is not `ready`. That check
532at landing closes the window.
533
534`valley status` takes one `ls-remote` snapshot of origin and fetches the objects it names, without
535moving a local branch or writing `FETCH_HEAD`. It reads ancestry first: a head already in
536`origin/main` is integrated, even if recording the outcome failed. An outcome record it does not
537recognise is `unknown`, never a success.
538
539### The status object
540
541`valley status --json <b>` prints one JSON object on one line. Front ends read it, klaus among them,
542so it is a contract with a version. A change to the name or meaning of any field is a new version.
543
544```json
545{
546 "schema": "valley-status/v1",
547 "change": "topic/one",
548 "request_name": "topic-one",
549 "target": "main",
550 "target_tip": "<sha>",
551 "head": "<sha>",
552 "request": { "ref": "refs/the-valley/integration-requests/main/topic-one", "commit": "<sha>" },
553 "outcome": { "decision": "stale", "head": "<sha>", "tip": "<sha>", "failed_at": null },
554 "state": "stale"
555}
556```
557
558- `change` is the branch as given, without `origin/`. `request_name` names its integration request:
559 the branch with each `/` replaced by `-`.
560- `target_tip` is `main` on origin. `head` is the branch head on origin. When the branch is gone,
561 `head` is the request's commit, or else the outcome's head, or else `null`.
562- `request` is `null` when origin holds no integration request for the change. `outcome` is `null`
563 when origin holds no outcome record.
564- `outcome.decision` is `land`, `stale`, `reject`, `failed` or `unknown`. A record that does not
565 parse is `unknown`. `outcome.head` and `outcome.tip` are `null` unless they are object ids, and
566 `failed_at` is `null` unless the decision is `failed`.
567- `state` is one of these:
568
569| `state` | What it means |
570| ----------------------- | ------------------------------------------------------------------------------------- |
571| `integrated` | The head is in `origin/main`. |
572| `integrated-recorded` | A `land` outcome names this head. The change landed as a different commit. |
573| `outdated-request` | The request names an older commit than the branch head. |
574| `outcome-unreadable` | The outcome record does not parse. |
575| `not-requested` | No request stands for the change. An outcome from an earlier head may remain. |
576| `awaiting-decision` | A request stands for this head, and no outcome names it. |
577| `awaiting-new-decision` | The outcome names this head, but `main` or the evidence has moved since it was made. |
578| `stale` | The integrator reported the change stale against the current `main` and evidence. |
579| `rejected` | The integrator rejected the evidence. |
580| `failed` | The integrator failed while processing the request. The change may still have landed. |
581
582An error prints no object. The message goes to stderr and the exit status is non-zero. The errors
583are an unreadable origin, an origin without `main`, nothing on origin for the change, and a change
584name that is not UTF-8, which JSON cannot carry exactly. Checked by `valley-status`, and by the Go
585tests in `status/` for the outcome record's parser.
586
587### Listing changes
588
589`valley changes --json` prints every change a repository holds a record of, in every state, as the
590`valley-changes/v1` object (`status/list.go`). klaus reads it, so it is a contract with a version,
591like the status object. `valley pending` is the view a person reads (`status/pending.go`).
592
593Both read one snapshot of origin for each repository they list. For each version of each change,
594they decide its landing evidence (`status/inventory.go`). The first rule that applies is the answer:
595
5961. `landed`: the version's head is on `main`'s first-parent history, and no commit on that history
597 between the head and `main`'s tip is a merge. That is, `git rev-list --first-parent` from `main`
598 lists the head, and `git rev-list --first-parent --merges <head>..main` lists nothing. The
599 integrator never writes a merge, and a fast-forward puts every commit of a change on that
600 history. Ancestry alone is not enough: a merge can drop the content of either of its parents, so
601 a head below any merge has not landed by this rule.
6022. `landed-recorded`: every one of these holds.
603 - A commit `L` on `main`'s first-parent history, with no merge above it, carries a transfer
604 statement whose `change` is one of the version's request names. The request names are the
605 version's branch with each `/` replaced by `-`, and `c-<change id>`.
606 - `L` has exactly one parent, as every commit the integrator lands as does.
607 - The statement's `base` is the `valley-tree-v1` digest of the version's tree, and its signature
608 verifies as the integrator's.
609 - The integrator's outcome record for that request name says `land`, and its head is this
610 version's head. Only the integrator writes outcome records: the hook refuses a push to them.
611 The record names the exact head that landed, which a statement about trees cannot do.
612 - The integrator's merge of the version's head onto `L`'s parent gives `L`'s tree. The client
613 recomputes it as `integrator/git.go` makes it:
614 `git merge-tree --write-tree --merge-base=<merge base> L^1 <head>`.
615
616 A head with a landed tree on another parent is another delta. These rules keep it from borrowing
617 the landing's evidence.
618
6193. `uncertain`: a request for the version was consumed, or its outcome record says `failed`. The
620 integrator moves `main` before it stores its evidence, so a landing can exist without its
621 evidence. A request is consumed when its outcome record says `land` for this head and the request
622 ref no longer points at it.
6234. `not-landed`: otherwise.
624
625The outcome record alone is never proof of a landing, because a later pass can overwrite it. A
626landing whose outcome record a later pass overwrote reads `uncertain`, never `landed-recorded`.
627
628The client checks a transfer statement with `attest verify`. The signature must be by the name in
629`VALLEY_INTEGRATOR_SIGNER`, under a key in the file `VALLEY_KNOWN_SIGNERS` names, which is the
630identity registry's compiled known-signers file. Without both, no transfer statement is believed: a
631version that only a statement could show landed reads as `uncertain` or `not-landed`, and the
632command says so on stderr.
633
634Without `--all`, the commands list the current repository. With `--all`, they list every project the
635serving host publishes at `refs/the-valley/projects`. The client reads the instance repository's URL
636from `policy/valley` at `main`'s tip on origin, never from a checked-out or pushed tree. A
637repository whose `main` carries `policy/instance` is its own instance. Each project's URL is the
638instance's URL with its last path segment replaced, so the transport, user and host are kept. A
639repository other than the current one is read through a clone under `$XDG_CACHE_HOME/valley/repos`.
640When discovery is unavailable, `--all` says so and lists the current repository alone.
641`--repo <name>` limits either command to one project.
642
643`policy/valley` is data that anyone who can land on `main` controls, so the client is careful with
644the URL it names. It refuses a URL that starts with `-`, which git would read as an option. It
645refuses git's `<transport>::<address>` form, such as `ext::`, which runs a command. It refuses any
646scheme other than `ssh://`, `git://`, `https://` and `file://`. A scp-like URL and a local path are
647allowed. Every git call that takes such a URL passes `--end-of-options` first, and runs with
648`GIT_ALLOW_PROTOCOL=ssh:git:https:file`, which overrides any `protocol.*.allow` in the caller's own
649configuration.
650
651```json
652{
653 "schema": "valley-changes/v1",
654 "host": "classic-laddie",
655 "repos": [
656 {
657 "repo": "cosmo",
658 "url": "git@classic-laddie:cosmo.git",
659 "error": null,
660 "changes": [
661 {
662 "change_id": "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz",
663 "handle": "nxvp",
664 "target": "main",
665 "owner": "klaus-env",
666 "state": "ready",
667 "record": "<record commit>",
668 "seq": 3,
669 "active": 2,
670 "versions": [
671 {
672 "n": 1,
673 "head": "<sha>",
674 "title": "the head's subject",
675 "branch": "topic",
676 "uploader": "klaus-env",
677 "landing": "not-landed",
678 "landed_commit": null,
679 "request": { "ref": "refs/the-valley/integration-requests/main/topic", "commit": "<sha>" },
680 "outcome": { "decision": "stale", "head": "<sha>", "tip": "<sha>" }
681 }
682 ],
683 "includes": ["<change id>"]
684 }
685 ],
686 "branches": [{ "branch": "loose", "head": "<sha>" }]
687 }
688 ]
689}
690```
691
692- The object is one line. `host` is the host the instance's URL reaches, or origin's without
693 `--all`, with its port and without its user. A local path reaches `localhost`.
694- A repository that cannot be read has its message in `error`, and empty `changes` and `branches`.
695 The others are listed all the same.
696- Changes are keyed by repository, target and change ID, in the order of their IDs. `state` is
697 `ready`, `draft` or `withdrawn`. `record` is the record commit.
698- A handle is unique among every change listed, in every repository. Every record of a listed
699 repository counts, so a handle also names its change within that repository alone. When one change
700 ID is recorded in more than one listed repository, each of those entries has the qualified handle
701 `<repo>:<prefix>`, such as `cosmo:nxvp`. git refuses a colon in a ref name, so a qualified handle
702 is never also a branch, and never reads as `origin/<branch>`. Every verb that takes a handle
703 accepts the qualified form, in the repository it names, and refuses one that names another
704 repository.
705- `landed_commit` is the commit on `main` the version landed as: its own head when `landed`, the
706 transfer statement's commit when `landed-recorded`, and otherwise `null`.
707- `request` is the request filed at the version's head under either of its request names, or `null`.
708 `outcome` is the outcome record about the version's head, or `null`; its `tip` is `null` unless it
709 is an object ID.
710- `includes` lists the changes whose active head is a strict ancestor of this one's, nearest only.
711 It is ancestry alone: approving this change lands their content too.
712- `branches` lists each branch on origin, other than `main`, that `main` does not reach and that is
713 no change's active head. A branch name that is not UTF-8 is left out, with a warning on stderr.
714
715`valley pending` lists the ready changes whose active version is neither `landed` nor
716`landed-recorded`, one line each: the handle and version, the title, `includes <handle>` when the
717change includes another that has not landed, and `uncertain` when that applies. `--owes` adds the
718checks each change owes, by cost, as `valley checks` derives them. Then come the branches no change
719is on, and the drafts. With `--all`, each repository is headed by its name.
720
721`valley pending --json` prints `valley-pending/v1`. It has the shape of `valley-changes/v1`, with
722the human view's sections. `changes` holds only the ready changes that have not landed. Each
723repository has a `drafts` array as well, holding the drafts, with the same entry shape. `branches`
724is the same as in `valley-changes/v1`. Its `includes` counts only changes that have not landed.
725
726```text
727mmmm v1 | stack-b | includes nxvp
728nxvp v1 | stack-a
729vvvv v1 | lost | uncertain
730
731branches
732loose | loose | 6 seconds ago | klaus-env | ahead 1 | 1 file changed, 1 insertion(+)
733
734drafts
735qqqq v1 on paused | paused | 6 seconds ago | klaus-env | ahead 1 | 1 file changed, 1 insertion(+)
736```
737
738`review`, `approve`, `rebase`, `unready` and `withdraw` read a branch name as the change whose
739active version is on it. A change whose active version is `landed` or `landed-recorded` is skipped
740while the branch's tip is still that version's head, so a branch name can be used again once its
741change has landed. A branch that has moved since names the change as before. Checked by
742`valley-changes`, and by the Go tests in `status/` for handles, URLs and the JSON writer.
743
744## The life of a change
745
7461. An author pushes a topic branch. The hook accepts it, the bus gets `ref-updated`, and the mirrors
747 prune it.
7482. The operator approves the change, with `[a]pprove` in `valley review <branch>` or with
749 `valley approve <branch>`. The owed checks run, and one push files the evidence and the
750 integration request. The hook verifies each note and the `request` grant.
7513. On its next pass the integrator builds the candidate, derives the policy, judges the evidence,
752 and lands or refuses.
7534. On a landing, the publisher sends `ref-updated` to the bus and pushes `main` and tags to the
754 mirrors. `valley status <branch>` shows the outcome.
755
756## How cosmo deploys it
757
758cosmo's `hosts/classic-laddie/default.nix` imports `the-valley.nixosModules.valley-host` and points
759`services.valley.config` at `hosts/classic-laddie/valley.cue`. It turns on the bus, the integrator
760and the identity compiler, with the private repository `qinling` as the instance project. The host
761declares a small set of static keys beside the compiled registry keys.
762
763`valley.cue` declares two projects, `the-valley` and `qinling`. Both protect `main`. `the-valley`
764mirrors to GitHub at `gunk-dev/the-valley`, and `qinling` has no mirror. The mirror push uses the
765git user's ssh key, from an agenix secret. Backups are a nightly restic run of `/srv/git` to an
766offsite Storage Box over sftp, kept 7 daily, 4 weekly and 6 monthly. The backup target still needs
767provisioning, so the run logs a failure until it is set up.
768
769## Not deployed or not wired yet
770
771| What | State |
772| ------------------------------------------- | -------------------------------------------------------------------------------- |
773| `sigverify` | On `main` and tested. Nothing calls it. |
774| Hardware-backed keys in the registry | `schema/identity.cue` accepts `ssh-ed25519` keys only. |
775| SSHSIG evidence | Attestations use the signed-note format. |
776| Approvals | The integrator checks evidence only. No approval is required for any path class. |
777| A crash-safe landing ledger | Landing is several separate ref updates. |
778| Policy and registry snapshot in the outcome | The outcome record holds the head, the tip and an evidence hash. |
779| Registry compile on a qinling landing | The compiler runs on its 5-minute timer only. |
780| Bus authentication | The bus listens on localhost with no authentication. |
781| Other projects on the valley | cosmo and klaus are not hosted on the valley. |