gunk-dev/the-valley

architecture.md

sourced92fe21bcd74main's published tip history
24.7 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
106The bare repo enforces exactly one invariant, with two parts: **only the integrator's key may write
107protected refs, and attestation refs are create-only**. Everything else — topic branches, new
108attestation refs, integration requests — is wide open to anyone with push access. That's one short
109`pre-receive` hook; all complex policy lives in the integrator.
110
111## Bet: review is observability + feedback
112
113_Serves: observability & feedback; dissolves the "how do humans review agent code" question._
114
115"Review" is one slice of a larger problem: how the system gives feedback to anyone or anything that
116needs it. The PR-as-page model conflates:
117
118| Concern | What it does | Failure mode today |
119| ------------ | ------------------------------------------- | -------------------------------------------------------- |
120| Coordination | A place humans converge to discuss a change | Discussion thread is locked to the PR; orphaned at merge |
121| Gating | Correctness checks must pass before merge | Slow, brittle, false negatives |
122| Record | Persistent trace of why a change happened | Lives inside one vendor's database |
123| Notification | Someone needs to look at this | Firehose; no useful prioritization |
124
125Unbundled: **gating** is handled by attestations; the **record** is the event log itself;
126**coordination** happens in threads — named, persistent _views_ over events scoped to a change,
127derived rather than stored; **notification** becomes a priority/routing subsystem, which is the hard
128new bottleneck the design creates. The PR object disappears — what people call "a PR" becomes a
129query with a name. Pre-merge human review becomes the exception: humans engage when the priority
130layer flags something for them, and feedback accrues continuously before and after a change lands,
131on the same machinery.
132
133## Bet: project knowledge is a typed-node graph
134
135_Serves: project knowledge & discussion, agents as first-class authors._
136
137Everything in a project that isn't executable code or user-facing docs — outcomes, bugs, principles,
138decisions, ideas, threads — is a **typed node**: a markdown file whose YAML frontmatter carries the
139structured layer (type, id, status, typed edges) and whose body carries prose. The graph lives at
140the repo root, clones with the code, and is equally navigable by a human with `grep` and an agent
141parsing frontmatter. Nodes are signed by the same keys as commits; discussion threads are themselves
142nodes; and active principles can become load-bearing on integration policy. Not a wiki, not
143Obsidian, not Jira — a project substrate that exposes a knowledge-tool-shaped surface.
144
145## Bet: the knowledge graph read generatively — an outcome DAG
146
147_Serves: project knowledge & discussion (the work itself is knowledge), agents as first-class
148authors. Anchored at rungs S3/S4 of the [scenario ladder](./user-scenarios.md)._
149
150The typed-node graph already stores dependency structure: `blocked_by`/`blocks` edges form a
151dependency DAG. The bet is to make **outcome** — a thing someone wants to exist that does not yet —
152the central node type and read that DAG _generatively_: open outcomes are not a record of intent but
153a worklist the system is under pressure to complete. Root outcomes — the things a human actually
154asked for — carry a priority that propagates down to the sub-outcomes that must complete for them.
155The system dispatches the **unblocked frontier** — the open outcomes whose blockers are all closed —
156on the critical path toward the highest-priority root. Dispatch is klaus-shaped: one more subscriber
157on the log, spawning agent runs against frontier nodes.
158
159This splits the old "priority layer" question in two: **work-scheduling** — what the system produces
160next — is what this bet answers; **attention-routing** — what a human must see, and how urgently —
161stays open ([openquestions.md](./openquestions.md)). Scheduler mechanics and node formats are
162deliberately absent here; that detail returns when its rung is the top priority. The fuller sketch:
163[.the-valley/ideas/ida-eac723e-outcome-dag.md](../.the-valley/ideas/ida-eac723e-outcome-dag.md).
164
165## Federation: the valley is the unit
166
167_Serves: small trusted teams (rung S5 and the S7 limit of the [ladder](./user-scenarios.md))._
168
169A **valley** — a team, a company, a personal namespace — is the unit of federation. A valley is a
170set of people, agents, and projects under shared law, and it carries its own hosting and its own
171coordination: the bare repos, the bus, the integrator. The substrate defines what a valley is, and a
172valley is one of them ([dcr-8f069dd](../.the-valley/decisions/dcr-8f069dd-valley-is-the-unit.md)). A
173valley runs on a single machine — the v1 case — and distributes later without changing shape. This
174frame splits the cross-repo open questions in two: **intra-valley** (one bus, one integrator — the
175tractable near-term case) and **inter-valley** (genuine federation — harder, later). See
176[openquestions.md](./openquestions.md); anything deeper is deferred until its rung is in reach. The
177fuller sketch:
178[.the-valley/ideas/ida-8482624-federation-groups.md](../.the-valley/ideas/ida-8482624-federation-groups.md).
179
180## System shape and cardinalities
181
182The bets above say why each piece has the shape it has. This section says how the pieces fit
183together: what exists, what relates to what, and how many of each there are.
184
185### Governance and identity
186
187The valley owns a repository, the repository holds the governance, and the registry in it declares
188the principals whose keys and grants the enforcement boundaries check.
189
190```mermaid
191erDiagram
192 valley ||--|| valley-repository : "sovereign home"
193 valley-repository ||--|| identity-registry : holds
194 valley-repository ||--|| valley-floor : holds
195 valley-repository ||--o| host-declaration : holds
196 identity-registry ||--|{ principal : declares
197 principal ||--|{ key : holds
198 principal ||--o{ grant : carries
199 grant }|--|| enforcement-boundary : "checked at"
200 principal ||..o{ agent-run : "delegates to"
201```
202
203The valley is the unit that owns a repository, and the repository holds the valley's governance —
204the registry, the floor, and the declaration of any host the valley owns. The registry is one CUE
205document per valley. A principal is a human, a machine, or a service. A key carries its own expiry,
206and it signs under exactly one name, because the key hash binds the name and the key together. An
207enforcement boundary is a place that checks: the push boundary at the host, the protected refs a
208controller writes, and the bus. A grant exists only where a boundary checks it.
209
210The dashed edge is the one relation with no registry entry behind it. An agent run acts under a
211principal's delegated authority and holds no entry of its own. One entry is required at the birth of
212a valley: the genesis entry, which anchors governance of the registry's own stream.
213
214### Structure and machinery
215
216The substrate mandates a floor over valleys, valleys own projects, and hosts serve valleys by
217running a controller per protected project; the dashed edges are the ones still open.
218
219```mermaid
220flowchart TD
221 substrate["substrate: schema, module, floor"]
222 valley["valley: one repository, one floor, one registry"]
223 project["project: one git repository"]
224 policy["project policy"]
225 host["host"]
226 controller["integrator controller: a process, not an identity"]
227 principal["integrator principal"]
228 bus["bus: NATS JetStream"]
229 mirror["mirror: transitional"]
230 backup["offsite backup"]
231
232 substrate -->|"floor mandates over, 1 : N"| valley
233 valley -->|"owns and governs, 1 : N"| project
234 valley -->|"declares, 1 : 1"| principal
235 project -->|"carries, 1 : 1"| policy
236 host -->|"serves, N : M"| valley
237 host -->|"runs one per protected project, 1 : N"| controller
238 host -->|"backed up, periodic"| backup
239 controller -->|"writes protected refs of, 1 : 1"| project
240 controller -->|"signs as"| principal
241 substrate -.->|"open: the substrate is itself the project of one valley"| project
242 host -.->|"open: one bus per valley, or valley-scoped subjects"| bus
243 project -.->|"mirrored to, 1 : 0..1, credential mechanism open"| mirror
244```
245
246A host is shared infrastructure serving several valleys, and the valleys on it are strongly
247isolated: one unix user per valley at the push boundary, each user's keys compiled from that
248valley's registry alone. The host edge is the asymmetric one. Several valleys per host is the case
249the design carries today; a valley spanning several hosts is left unprecluded and deferred.
250Cross-valley contribution to the substrate is the federation edge, which is why that link is open
251rather than declared.
252
253### The cardinality ledger
254
255| Relation | Cardinality | Where declared |
256| ------------------------------ | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
257| valley — valley repository | 1 : 1 | [dcr-8f069dd](../.the-valley/decisions/dcr-8f069dd-valley-is-the-unit.md) — the valley's sovereign home |
258| 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 |
259| valley — push unix user | 1 : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — no compiled artifact unions two valleys |
260| 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 |
261| valley — integrator principal | 1 : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) — controllers are processes |
262| project — owning valley | N : 1 | [dcr-9b5da04](../.the-valley/decisions/dcr-9b5da04-hosts-serve-isolated-valleys.md) |
263| 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 |
264| valley — identity registry | 1 : 1 | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — one CUE document |
265| registry — principal | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) |
266| principal — key | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — expiry first-class |
267| 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 |
268| 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 |
269| 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 |
270| statement — signers | 1 : N | [dcr-de9d996](../.the-valley/decisions/dcr-de9d996-statement-text-and-signed-note.md) — siblings under one text |
271| project — git repository | 1 : 1 | [dcr-5da1f36](../.the-valley/decisions/dcr-5da1f36-project-is-repo.md) |
272| project — project policy | 1 : 1 | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) |
273| protected project — controller | 1 : 1 | [integration.md](./integration.md) — a protected ref is what a controller exists to write |
274| project — mirror | 1 : 0..1 | [dcr-d7952bc](../.the-valley/decisions/dcr-d7952bc-phase0-replication-github-transitional.md) — transitional; the credential mechanism is open |
275| request — integration outcome | 1 : 0..1 | [integration.md](./integration.md) — the request is consumed on land |
276
277### Bootstrap: the loops unroll in time
278
279Four parts of the system govern their own change. The registry governs the changes to itself. The
280floor judges its own amendments. The push boundary is compiled from content that sits behind the
281push boundary. The host is declared by a repository the host serves.
282
283The cycle unrolls into an induction over history: a hand-made genesis, then each tip judged by the
284rules at the tip before it.
285
286```mermaid
287flowchart LR
288 genesis["genesis: by hand, under the previous regime"]
289 tipN["tip N"]
290 tipNext["tip N+1"]
291 onward["and so on"]
292
293 genesis -->|"judged by genesis rules"| tipN
294 tipN -->|"judged by the rules at tip N"| tipNext
295 tipNext -->|"judged by the rules at tip N+1"| onward
296```
297
298One mechanism resolves all four: the cycle unrolls in time. The rules at tip N judge the landing
299that produces tip N+1, so a rule change is judged by the old rules and binds from the next landing.
300That is the amendment structure, and it is attested rather than assumed — a transfer statement
301records the policy commit it was judged against, so each step of the induction carries its own
302evidence.
303
304What differs per loop is only the base case and the layer recovery falls back to. Every base case is
305genesis before enforcement: the first state is made by hand, under whatever regime preceded the
306machinery. Every recovery is a layer below the machinery, reachable when the machinery itself is the
307thing that broke.
308
309| Loop | The rules | Base case | Recovery below |
310| ----------------------------------------- | ------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
311| 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 |
312| 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 |
313| 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 |
314| 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 |
315
316The sharpest case is an identity introducing itself: the landing that first publishes an
317integrator's key is countersigned by that very key, so it is verifiable only through the tree it
318creates. Its anchor is the signed human approval that the registry path class requires, which is the
319genesis pattern doing its job one landing at a time rather than once at birth.
320
321## Components
322
323One line each; these are the current picks, not commitments:
324
325- **Hosting** — bare git over SSH; each repo is just a directory; `cgit` or similar for browsing if
326 needed.
327- **Bus** — NATS JetStream; single binary, persistent streams, runs on the same Tailscale box;
328 replicate later if needed.
329- **Transparency log** — Tessera-backed tlog via
330 [tesseract](https://github.com/transparency-dev/tesseract); every attestation lands with an
331 inclusion proof, giving non-repudiation and external auditability without depending on any one
332 host.
333- **Pure reactions** — Nix derivations; inputs are events, outputs are content-addressed artifacts,
334 replayable from the log.
335- **Effectful reactions** — [armstrong](https://github.com/gunk-dev/armstrong) as a Go controller;
336 the controller-shaped successor to the current Actions-based armstrong.
337- **Agent dispatch** — klaus, with its existing run-budget mechanism.
338- **Schemas** — CUE, already used by armstrong; shared across event producers and consumers.