gunk-dev/the-valley

architecture.md

source05b3b299b9f7main's published tip history
24.4 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 group is the unit
166
167_Serves: small trusted teams (rung S5 and the S7 limit of the [ladder](./user-scenarios.md))._
168
169A **group** — a team, a company, a personal namespace — is the unit of federation. Each group runs
170exactly one instance of the substrate: its hosting and its coordination (the bare repos, the bus,
171the integrator). An instance is instantiable on a single machine — the v1 case — and distributable
172later without changing shape. This frame splits the cross-repo open questions in two:
173**intra-group** (one bus, one integrator — the tractable near-term case) and **inter-group**
174(genuine federation — harder, later). See [openquestions.md](./openquestions.md); anything deeper is
175deferred until its rung is in reach. The fuller sketch:
176[.the-valley/ideas/ida-8482624-federation-groups.md](../.the-valley/ideas/ida-8482624-federation-groups.md).
177
178## System shape and cardinalities
179
180The bets above say why each piece has the shape it has. This section says how the pieces fit
181together: what exists, what relates to what, and how many of each there are.
182
183### Governance and identity
184
185The group owns a repository, the repository holds the governance, and the registry in it declares
186the principals whose keys and grants the enforcement boundaries check.
187
188```mermaid
189erDiagram
190 group ||--|| group-repository : "sovereign home"
191 group-repository ||--|| identity-registry : holds
192 group-repository ||--|| group-floor : holds
193 group-repository ||--o| host-declaration : holds
194 identity-registry ||--|{ principal : declares
195 principal ||--|{ key : holds
196 principal ||--o{ grant : carries
197 grant }|--|| enforcement-boundary : "checked at"
198 principal ||..o{ agent-run : "delegates to"
199```
200
201The group is the unit that owns a repository, and the repository holds the group's governance — the
202registry, the floor, and the declaration of any host the group owns. The registry is one CUE
203document per group. A principal is a human, a machine, or a service. A key carries its own expiry,
204and it signs under exactly one name, because the key hash binds the name and the key together. An
205enforcement boundary is a place that checks: the push boundary at the host, the protected refs a
206controller writes, and the bus. A grant exists only where a boundary checks it.
207
208The dashed edge is the one relation with no registry entry behind it. An agent run acts under a
209principal's delegated authority and holds no entry of its own. One entry is required at group birth:
210the genesis entry, which anchors governance of the registry's own stream.
211
212### Structure and machinery
213
214The substrate mandates a floor over groups, groups own projects, and hosts serve groups by running a
215controller per protected project; the dashed edges are the ones still open.
216
217```mermaid
218flowchart TD
219 substrate["substrate: schema, module, floor"]
220 group["group: one repository, one floor, one registry"]
221 project["project: one git repository"]
222 policy["project policy"]
223 host["host"]
224 controller["integrator controller: a process, not an identity"]
225 principal["integrator principal"]
226 bus["bus: NATS JetStream"]
227 mirror["mirror: transitional"]
228 backup["offsite backup"]
229
230 substrate -->|"floor mandates over, 1 : N"| group
231 group -->|"owns and governs, 1 : N"| project
232 group -->|"declares, 1 : 1"| principal
233 project -->|"carries, 1 : 1"| policy
234 host -->|"serves, N : M"| group
235 host -->|"runs one per protected project, 1 : N"| controller
236 host -->|"backed up, periodic"| backup
237 controller -->|"writes protected refs of, 1 : 1"| project
238 controller -->|"signs as"| principal
239 substrate -.->|"open: the substrate is itself the project of one group"| project
240 host -.->|"open: one bus per group, or group-scoped subjects"| bus
241 project -.->|"mirrored to, 1 : 0..1, credential mechanism open"| mirror
242```
243
244A host is shared infrastructure serving several groups, and the groups on it are strongly isolated:
245one unix user per group at the push boundary, each user's keys compiled from that group's registry
246alone. The host edge is the asymmetric one. Several groups per host is the case the design carries
247today; a group spanning several hosts is left unprecluded and deferred. Cross-group contribution to
248the substrate is the federation edge, which is why that link is open rather than declared.
249
250### The cardinality ledger
251
252| Relation | Cardinality | Where declared |
253| ------------------------------ | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
254| group — group repository | 1 : 1 | [dcr-0e9278a](../.the-valley/decisions/dcr-0e9278a-group-is-the-unit.md) — the group's sovereign home |
255| group — host | N : M | [dcr-2f03be3](../.the-valley/decisions/dcr-2f03be3-hosts-serve-isolated-groups.md) — several groups per host now, a group across hosts deferred |
256| group — push unix user | 1 : 1 | [dcr-2f03be3](../.the-valley/decisions/dcr-2f03be3-hosts-serve-isolated-groups.md) — no compiled artifact unions two groups |
257| identity — group | N : M | [dcr-2f03be3](../.the-valley/decisions/dcr-2f03be3-hosts-serve-isolated-groups.md) — the same keys cited by each group's registry |
258| group — integrator principal | 1 : 1 | [dcr-2f03be3](../.the-valley/decisions/dcr-2f03be3-hosts-serve-isolated-groups.md) — controllers are processes |
259| project — owning group | N : 1 | [dcr-2f03be3](../.the-valley/decisions/dcr-2f03be3-hosts-serve-isolated-groups.md) |
260| group — project (governs) | 1 : N | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) — the floor read from the group repository's tip |
261| group — identity registry | 1 : 1 | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — one CUE document |
262| registry — principal | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) |
263| principal — key | 1 : N | [dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md) — expiry first-class |
264| 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 |
265| 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 |
266| 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 |
267| statement — signers | 1 : N | [dcr-de9d996](../.the-valley/decisions/dcr-de9d996-statement-text-and-signed-note.md) — siblings under one text |
268| project — git repository | 1 : 1 | [dcr-5da1f36](../.the-valley/decisions/dcr-5da1f36-project-is-repo.md) |
269| project — project policy | 1 : 1 | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) |
270| protected project — controller | 1 : 1 | [integration.md](./integration.md) — a protected ref is what a controller exists to write |
271| project — mirror | 1 : 0..1 | [dcr-d7952bc](../.the-valley/decisions/dcr-d7952bc-phase0-replication-github-transitional.md) — transitional; the credential mechanism is open |
272| request — integration outcome | 1 : 0..1 | [integration.md](./integration.md) — the request is consumed on land |
273
274### Bootstrap: the loops unroll in time
275
276Four parts of the system govern their own change. The registry governs the changes to itself. The
277floor judges its own amendments. The push boundary is compiled from content that sits behind the
278push boundary. The host is declared by a repository the host serves.
279
280The cycle unrolls into an induction over history: a hand-made genesis, then each tip judged by the
281rules at the tip before it.
282
283```mermaid
284flowchart LR
285 genesis["genesis: by hand, under the previous regime"]
286 tipN["tip N"]
287 tipNext["tip N+1"]
288 onward["and so on"]
289
290 genesis -->|"judged by genesis rules"| tipN
291 tipN -->|"judged by the rules at tip N"| tipNext
292 tipNext -->|"judged by the rules at tip N+1"| onward
293```
294
295One mechanism resolves all four: the cycle unrolls in time. The rules at tip N judge the landing
296that produces tip N+1, so a rule change is judged by the old rules and binds from the next landing.
297That is the amendment structure, and it is attested rather than assumed — a transfer statement
298records the policy commit it was judged against, so each step of the induction carries its own
299evidence.
300
301What differs per loop is only the base case and the layer recovery falls back to. Every base case is
302genesis before enforcement: the first state is made by hand, under whatever regime preceded the
303machinery. Every recovery is a layer below the machinery, reachable when the machinery itself is the
304thing that broke.
305
306| Loop | The rules | Base case | Recovery below |
307| ----------------------------------------- | ------------------------------------------------ | ------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------- |
308| 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 |
309| floor over its own amendments | the policy at the group 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 |
310| 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 |
311| 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 |
312
313The sharpest case is an identity introducing itself: the landing that first publishes an
314integrator's key is countersigned by that very key, so it is verifiable only through the tree it
315creates. Its anchor is the signed human approval that the registry path class requires, which is the
316genesis pattern doing its job one landing at a time rather than once at birth.
317
318## Components
319
320One line each; these are the current picks, not commitments:
321
322- **Hosting** — bare git over SSH; each repo is just a directory; `cgit` or similar for browsing if
323 needed.
324- **Bus** — NATS JetStream; single binary, persistent streams, runs on the same Tailscale box;
325 replicate later if needed.
326- **Transparency log** — Tessera-backed tlog via
327 [tesseract](https://github.com/transparency-dev/tesseract); every attestation lands with an
328 inclusion proof, giving non-repudiation and external auditability without depending on any one
329 host.
330- **Pure reactions** — Nix derivations; inputs are events, outputs are content-addressed artifacts,
331 replayable from the log.
332- **Effectful reactions** — [armstrong](https://github.com/gunk-dev/armstrong) as a Go controller;
333 the controller-shaped successor to the current Actions-based armstrong.
334- **Agent dispatch** — klaus, with its existing run-budget mechanism.
335- **Schemas** — CUE, already used by armstrong; shared across event producers and consumers.