gunk-dev/the-valley

architecture.md

source2cf7aa81b2ddmain's published tip history
25.8 KiB · Rendered
1# Architecture
2
3The bets, each traced to a requirement ([requirements.md](./requirements.md)) and taken no deeper
4than rationale. Detailed design lives in the rest of `design/`, and only for roadmap phases that are
5current or next — later phases get their detail re-fleshed when they start, informed by what
6validation has taught us.
7
8## Shape of the system
9
10```
11 bare git (Tailscale) ──┐ ┌──► nix build (pure reactions)
12 build outputs ───┤ │
13 agent runs (klaus) ───┼──► event bus ──► subscribers ──► armstrong (effectful reactions)
14 deploy state ───┤ (NATS JS) │
15 external signals ──┘ └──► integrator (per-repo integration)
16
17 attestations published to a Tessera-backed transparency log on every push
18```
19
20## The concerns, unbundled
21
22| Concern | Current state | Direction |
23| ------------------------ | --------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
24| Hosting | Works fine, lock-in is the cost | Bare git over SSH on a Tailscale-reachable box |
25| Identity / access | Works fine | Tailscale ACLs + SSH keys |
26| Verification | Slow, YAML-shaped | `nix flake check` triggered by events |
27| Artifacts | Opaque, ephemeral | Nix derivations, content-addressed, pushed to a cache |
28| Automation | Actions: push-based, brittle | Reactive controllers subscribing to an event log |
29| Integration / merge | Branch protection in vendor UI | Pull-based integrator controllers reacting to integration-requested events |
30| Observability & feedback | PR-shaped, conflates four concerns | Continuous feedback as events; threads as derived views; priority/routing as a first-class subsystem |
31| Project knowledge | Scattered: issues, wikis, docs, scratch files, agent histories, heads | Unified typed-node graph in markdown, equally navigable by humans and agents; discussion threads are themselves nodes |
32
33## Bet: git as event source — a log, not a workflow engine
34
35_Serves: automation, observability._
36
37GitHub Actions, GitLab CI, Jenkins — all push-based pipelines. An event happens, a workflow runs, it
38exits, the trail is a log file.
39
40A reactive log inverts this:
41
42- **Causality is preserved.** The chain of "commit → build → deploy → notification" is one queryable
43 history, not seven disconnected job UIs.
44- **Reactions are independent.** Adding a new subscriber doesn't touch any other subscriber or any
45 workflow file.
46- **Replay is free.** Want to know what would have happened if a reaction had been different? Replay
47 the log against a new subscriber.
48- **No central workflow file.** Each subscriber owns its own logic and lifecycle.
49
50Per-repo events (refs, attestations, integration requests) are durable in git itself; the bus is a
51projection that can be rebuilt. Only ephemeral cross-system events (deploys, metrics, agent runs)
52have the bus as their source of truth — so the log is never load-bearing for the crown-jewel data.
53
54Prior art: Atomist (defunct, but had this model). Kubernetes controllers. Datomic / event sourcing
55generally. `git` itself — commits are events, refs are views.
56
57## Bet: attestation with revocation, not CI as gate
58
59_Serves: verification (feedback in seconds), agents as first-class authors._
60
61The contributor's own hermetic environment runs the canonical checks and produces a signed
62attestation of what ran, against what inputs, with what result. Subscribers act on the attestation
63immediately. Re-verifiers cross-check it asynchronously. Trust is _measured_ per attester from
64re-verification confirm rates, and divergence revokes it.
65
66This trades two failure modes:
67
68| Model | Failure mode |
69| ----------- | ------------------------------------------------------------------------------ |
70| CI-as-gate | False negatives — slow feedback, flakes, infra outages block correct changes |
71| Attestation | False positives — a bad attestation can land before the re-verifier catches it |
72
73For most environments — personal projects, small teams, trusted contributor sets — false positives
74that are _detected and revocable_ are a much better trade than a slow, brittle gate. The primary win
75is latency; the security properties are a side effect of doing it well. See
76[verification.md](./verification.md) for what makes an attestation hard to forge.
77
78## Bet: a pull-based integrator, not a pre-receive gate
79
80_Serves: integration (observable, policy-driven), architectural minimalism._
81
82The simplest option is a `pre-receive` hook that checks attestations on every push to a protected
83branch. It works — but it makes integration a different _shape_ from the rest of the system, a
84synchronous special case whose failure mode is a terse line of stderr. The design instead adopts a
85**pull-based integrator**: a controller subscribing to integration-request events, performing
86integration as a reaction, emitting outcome events back into the log.
87
88| | Pre-receive gate | Pull-based integrator |
89| ------------------------------- | -------------------------------------- | -------------------------------- |
90| Where policy lives | Hook script on git server | Controller subscribing to events |
91| Failure mode | Push error to stderr | Outcome event in log, threadable |
92| Multiple policies | Hard (one hook per repo) | Trivial (multiple subscribers) |
93| Async validation | No — must answer before push completes | Yes — controller takes its time |
94| Re-running on transient failure | Re-push | Re-fire the event |
95| Architectural consistency | Special case | Same pattern as everything else |
96
97Merge-queue semantics fall out for free: requests targeting the same ref serialize in the
98integrator. And the failure model is deliberately unified — there is no rejection, only
99**staleness**: a request that can't progress against current policy is marked stale, once, and
100refreshing it is the branch owner's problem, not the integrator's. What makes staleness computable
101per check, so that refreshing one is not refreshing all of them, is
102[integration.md](./integration.md).
103
104### The one structural git invariant
105
106Every repository a valley host serves enforces exactly one invariant: **what a push may write**. A
107write must pass every rule that applies to its ref, and no rule stands in for another:
108
109- Topic branches are open to anyone with push access.
110- Attestation refs may only be created, and only holding what their name says: notes about that
111 tree, signed by that signer.
112- An integration request takes the request grant
113 ([dcr-e544f20](../.the-valley/decisions/dcr-e544f20-access-is-verbs-on-projects.md)).
114- Every other ref, tags included, takes a named grant of the project.
115- A protected ref also takes a declared writer. The integrator writes protected refs on the host,
116 where no push hook applies.
117- Replacement refs, notes and the rest of the valley's own namespace take no push at all, and
118 neither does a ref that is symbolic in the repository.
119
120Replacement refs matter most. A replacement ref makes one object stand in for another for every
121reader of the repository, so it would let a push change what the integrator judges without moving a
122ref. The programs that make decisions also read git with replacement refs turned off, so one that is
123already there decides nothing
124([bd-75c8721](../.the-valley/bugs/bd-75c8721-pushes-could-write-any-namespace.md)).
125
126The repository that carries the floor and the registry has to protect its main. Both are read from
127that tip, so a main any key could move would hand every key the floor and the registry.
128
129That is one small `pre-receive` hook; all complex policy lives in the integrator.
130
131## Bet: review is observability + feedback
132
133_Serves: observability & feedback; dissolves the "how do humans review agent code" question._
134
135"Review" is one slice of a larger problem: how the system gives feedback to anyone or anything that
136needs it. The PR-as-page model conflates:
137
138| Concern | What it does | Failure mode today |
139| ------------ | ------------------------------------------- | -------------------------------------------------------- |
140| Coordination | A place humans converge to discuss a change | Discussion thread is locked to the PR; orphaned at merge |
141| Gating | Correctness checks must pass before merge | Slow, brittle, false negatives |
142| Record | Persistent trace of why a change happened | Lives inside one vendor's database |
143| Notification | Someone needs to look at this | Firehose; no useful prioritization |
144
145Unbundled: **gating** is handled by attestations; the **record** is the event log itself;
146**coordination** happens in threads — named, persistent _views_ over events scoped to a change,
147derived rather than stored; **notification** becomes a priority/routing subsystem, which is the hard
148new bottleneck the design creates. The PR object disappears — what people call "a PR" becomes a
149query with a name. Pre-merge human review becomes the exception: humans engage when the priority
150layer flags something for them, and feedback accrues continuously before and after a change lands,
151on the same machinery.
152
153## Bet: project knowledge is a typed-node graph
154
155_Serves: project knowledge & discussion, agents as first-class authors._
156
157Everything in a project that isn't executable code or user-facing docs — outcomes, bugs, principles,
158decisions, ideas, threads — is a **typed node**: a markdown file whose YAML frontmatter carries the
159structured layer (type, id, status, typed edges) and whose body carries prose. The graph lives at
160the repo root, clones with the code, and is equally navigable by a human with `grep` and an agent
161parsing frontmatter. Nodes are signed by the same keys as commits; discussion threads are themselves
162nodes; and active principles can become load-bearing on integration policy. Not a wiki, not
163Obsidian, not Jira — a project substrate that exposes a knowledge-tool-shaped surface.
164
165## Bet: the knowledge graph read generatively — an outcome DAG
166
167_Serves: project knowledge & discussion (the work itself is knowledge), agents as first-class
168authors. Anchored at rungs S3/S4 of the [scenario ladder](./user-scenarios.md)._
169
170The typed-node graph already stores dependency structure: `blocked_by`/`blocks` edges form a
171dependency DAG. The bet is to make **outcome** — a thing someone wants to exist that does not yet —
172the central node type and read that DAG _generatively_: open outcomes are not a record of intent but
173a worklist the system is under pressure to complete. Root outcomes — the things a human actually
174asked for — carry a priority that propagates down to the sub-outcomes that must complete for them.
175The system dispatches the **unblocked frontier** — the open outcomes whose blockers are all closed —
176on the critical path toward the highest-priority root. Dispatch is klaus-shaped: one more subscriber
177on the log, spawning agent runs against frontier nodes.
178
179This splits the old "priority layer" question in two: **work-scheduling** — what the system produces
180next — is what this bet answers; **attention-routing** — what a human must see, and how urgently —
181stays open ([openquestions.md](./openquestions.md)). Scheduler mechanics and node formats are
182deliberately absent here; that detail returns when its rung is the top priority. The fuller sketch:
183[.the-valley/ideas/ida-eac723e-outcome-dag.md](../.the-valley/ideas/ida-eac723e-outcome-dag.md).
184
185## Federation: the valley is the unit
186
187_Serves: small trusted teams (rung S5 and the S7 limit of the [ladder](./user-scenarios.md))._
188
189A **valley** — a team, a company, a personal namespace — is the unit of federation. A valley is a
190set of people, agents, and projects under shared law, and it carries its own hosting and its own
191coordination: the bare repos, the bus, the integrator. The substrate defines what a valley is, and a
192valley is one of them ([dcr-8f069dd](../.the-valley/decisions/dcr-8f069dd-valley-is-the-unit.md)). A
193valley runs on a single machine — the v1 case — and distributes later without changing shape. This
194frame splits the cross-repo open questions in two: **intra-valley** (one bus, one integrator — the
195tractable near-term case) and **inter-valley** (genuine federation — harder, later). See
196[openquestions.md](./openquestions.md); anything deeper is deferred until its rung is in reach. The
197fuller sketch:
198[.the-valley/ideas/ida-8482624-federation-groups.md](../.the-valley/ideas/ida-8482624-federation-groups.md).
199
200## System shape and cardinalities
201
202The bets above say why each piece has the shape it has. This section says how the pieces fit
203together: what exists, what relates to what, and how many of each there are.
204
205### Governance and identity
206
207The valley owns a repository, the repository holds the governance, and the registry in it declares
208the principals whose keys and grants the enforcement boundaries check.
209
210```mermaid
211erDiagram
212 valley ||--|| valley-repository : "sovereign home"
213 valley-repository ||--|| identity-registry : holds
214 valley-repository ||--|| valley-floor : holds
215 valley-repository ||--o| host-declaration : holds
216 identity-registry ||--|{ principal : declares
217 principal ||--|{ key : holds
218 principal ||--o{ grant : carries
219 grant }|--|| enforcement-boundary : "checked at"
220 principal ||..o{ agent-run : "delegates to"
221```
222
223The valley is the unit that owns a repository, and the repository holds the valley's governance —
224the registry, the floor, and the declaration of any host the valley owns. The registry is one CUE
225document per valley. A principal is a human, a machine, or a service. A key carries its own expiry,
226and it signs under exactly one name, because the key hash binds the name and the key together. An
227enforcement boundary is a place that checks: the push boundary at the host, the protected refs a
228controller writes, and the bus. A grant exists only where a boundary checks it.
229
230The dashed edge is the one relation with no registry entry behind it. An agent run acts under a
231principal's delegated authority and holds no entry of its own. One entry is required at the birth of
232a valley: the genesis entry, which anchors governance of the registry's own stream.
233
234### Structure and machinery
235
236The substrate mandates a floor over valleys, valleys own projects, and hosts serve valleys by
237running a controller per protected project; the dashed edges are the ones still open.
238
239```mermaid
240flowchart TD
241 substrate["substrate: schema, module, floor"]
242 valley["valley: one repository, one floor, one registry"]
243 project["project: one git repository"]
244 policy["project policy"]
245 host["host"]
246 controller["integrator controller: a process, not an identity"]
247 principal["integrator principal"]
248 bus["bus: NATS JetStream"]
249 mirror["mirror: transitional"]
250 backup["offsite backup"]
251
252 substrate -->|"floor mandates over, 1 : N"| valley
253 valley -->|"owns and governs, 1 : N"| project
254 valley -->|"declares, 1 : 1"| principal
255 project -->|"carries, 1 : 1"| policy
256 host -->|"serves, N : M"| valley
257 host -->|"runs one per protected project, 1 : N"| controller
258 host -->|"backed up, periodic"| backup
259 controller -->|"writes protected refs of, 1 : 1"| project
260 controller -->|"signs as"| principal
261 substrate -.->|"open: the substrate is itself the project of one valley"| project
262 host -.->|"open: one bus per valley, or valley-scoped subjects"| bus
263 project -.->|"mirrored to, 1 : 0..1, credential mechanism open"| mirror
264```
265
266A host is shared infrastructure serving several valleys, and the valleys on it are strongly
267isolated: one unix user per valley at the push boundary, each user's keys compiled from that
268valley's registry alone. The host edge is the asymmetric one. Several valleys per host is the case
269the design carries today; a valley spanning several hosts is left unprecluded and deferred.
270Cross-valley contribution to the substrate is the federation edge, which is why that link is open
271rather than declared.
272
273### The cardinality ledger
274
275| Relation | Cardinality | Where declared |
276| ------------------------------ | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
277| valley — valley repository | 1 : 1 | [dcr-8f069dd](../.the-valley/decisions/dcr-8f069dd-valley-is-the-unit.md) — the valley's sovereign home |
278| valley — host | N : M | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — several valleys per host now, a valley across hosts deferred |
279| valley — push unix user | 1 : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — no compiled artifact unions two valleys |
280| identity — valley | N : M | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — the same keys cited by each valley's registry |
281| valley — integrator principal | 1 : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — controllers are processes |
282| project — owning valley | N : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) |
283| valley — project (governs) | 1 : N | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) — the floor read from the valley repository's tip |
284| valley — identity registry | 1 : 1 | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — one CUE document |
285| registry — principal | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) |
286| principal — key | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — expiry first-class |
287| grant — enforcement boundary | N : 1 | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — a grant exists only where a boundary checks it |
288| principal — agent run | 1 : N | [ida-a8243d2](../.the-valley/ideas/ida-a8243d2-agent-runs-act-under-delegated-authority.md) — a run holds no registry entry |
289| key — signing name | 1 : 1 | [dcr-de9d996](../.the-valley/decisions/dcr-de9d996-statement-text-and-signed-note.md) — the key hash binds name and key |
290| statement — signers | 1 : N | [dcr-de9d996](../.the-valley/decisions/dcr-de9d996-statement-text-and-signed-note.md) — siblings under one text |
291| project — git repository | 1 : 1 | [dcr-5da1f36](../.the-valley/decisions/dcr-5da1f36-project-is-repo.md) |
292| project — project policy | 1 : 1 | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) |
293| protected project — controller | 1 : 1 | [integration.md](./integration.md) — a protected ref is what a controller exists to write |
294| project — mirror | 1 : 0..1 | [dcr-d7952bc](../.the-valley/decisions/dcr-d7952bc-phase0-replication-github-transitional.md) — transitional; the credential mechanism is open |
295| request — integration outcome | 1 : 0..1 | [integration.md](./integration.md) — the request is consumed on land |
296
297### Bootstrap: the loops unroll in time
298
299Four parts of the system govern their own change. The registry governs the changes to itself. The
300floor judges its own amendments. The push boundary is compiled from content that sits behind the
301push boundary. The host is declared by a repository the host serves.
302
303The cycle unrolls into an induction over history: a hand-made genesis, then each tip judged by the
304rules at the tip before it.
305
306```mermaid
307flowchart LR
308 genesis["genesis: by hand, under the previous regime"]
309 tipN["tip N"]
310 tipNext["tip N+1"]
311 onward["and so on"]
312
313 genesis -->|"judged by genesis rules"| tipN
314 tipN -->|"judged by the rules at tip N"| tipNext
315 tipNext -->|"judged by the rules at tip N+1"| onward
316```
317
318One mechanism resolves all four: the cycle unrolls in time. The rules at tip N judge the landing
319that produces tip N+1, so a rule change is judged by the old rules and binds from the next landing.
320That is the amendment structure, and it is attested rather than assumed — a transfer statement
321records the policy commit it was judged against, so each step of the induction carries its own
322evidence.
323
324What differs per loop is only the base case and the layer recovery falls back to. Every base case is
325genesis before enforcement: the first state is made by hand, under whatever regime preceded the
326machinery. Every recovery is a layer below the machinery, reachable when the machinery itself is the
327thing that broke.
328
329| Loop | The rules | Base case | Recovery below |
330| ----------------------------------------- | ------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
331| registry over its own changes | the registry path class, human approval required | genesis entries land by hand review, before compilation is enabled | host root; re-rooting after a genesis-key compromise is open |
332| floor over its own amendments | the policy at the valley repository's tip | the repository is seeded before the integrator watches it | the human operator is a declared writer; the history is rebased by hand |
333| push boundary over the registry behind it | the compiled `authorized_keys` | a hand-written key list until the first compile | a failed compile keeps the last-good artifact, and a render that orphans registry governance is refused |
334| host over its own declaration | the declaration at the tip the host converges on | the first install is manual | offsite backup, and the restore runbook |
335
336The sharpest case is an identity introducing itself: the landing that first publishes an
337integrator's key is countersigned by that very key, so it is verifiable only through the tree it
338creates. Its anchor is the signed human approval that the registry path class requires, which is the
339genesis pattern doing its job one landing at a time rather than once at birth.
340
341## Components
342
343One line each; these are the current picks, not commitments:
344
345- **Hosting** — bare git over SSH; each repo is just a directory; `cgit` or similar for browsing if
346 needed.
347- **Bus** — NATS JetStream; single binary, persistent streams, runs on the same Tailscale box;
348 replicate later if needed.
349- **Transparency log** — Tessera-backed tlog via
350 [tesseract](https://github.com/transparency-dev/tesseract); every attestation lands with an
351 inclusion proof, giving non-repudiation and external auditability without depending on any one
352 host.
353- **Pure reactions** — Nix derivations; inputs are events, outputs are content-addressed artifacts,
354 replayable from the log.
355- **Effectful reactions** — [armstrong](https://github.com/gunk-dev/armstrong) as a Go controller;
356 the controller-shaped successor to the current Actions-based armstrong.
357- **Agent dispatch** — klaus, with its existing run-budget mechanism.
358- **Schemas** — CUE, already used by armstrong; shared across event producers and consumers.