| 1 | # Integrator internals |
| 2 | |
| 3 | How the integration automation actually performs its work, given an `integration-requested` event with a topic branch and attestations. The high-level controller pattern is established in [integration.md](./integration.md); this document fills in the operational detail. |
| 4 | |
| 5 | ## The integrator's loop |
| 6 | |
| 7 | Radically simple. For each `integration-requested` event the integrator owns: |
| 8 | |
| 9 | 1. Fetch the topic branch and its attestation refs from the bare repo. |
| 10 | 2. Verify attestation signatures against keys authorized by current policy. |
| 11 | 3. Determine whether the attestation **satisfies current integration policy** for the target ref. |
| 12 | 4. Attempt the integration onto the current tip of the target ref. |
| 13 | 5. If both succeed: update the target ref, emit `integration-succeeded`. |
| 14 | 6. If either fails for any non-trivially-recoverable reason: mark the request **stale**, emit a single `request-stale` event with the structured reason. Stop. |
| 15 | |
| 16 | The integrator never tries to be clever on the contributor's behalf. Anything that's not a clean integration of a valid attestation is the branch owner's problem to refresh. |
| 17 | |
| 18 | ## Attestation invariance under rebase |
| 19 | |
| 20 | Attestations cover the **tree hash** and **input hashes** of the change, not the commit hash. This makes a *trivial rebase* (target moved, no conflicts) attestation-invariant: the new commit has a different SHA but the same tree, and the original attestation still applies. |
| 21 | |
| 22 | | Scenario | Tree after rebase | Attestation status | |
| 23 | | --- | --- | --- | |
| 24 | | Fast-forward (target unmoved) | Identical | Valid | |
| 25 | | Trivial rebase (target moved, no conflicts) | Identical | Valid | |
| 26 | | Mechanical merge (git auto-resolves conflicts) | **Changed** | Stale (tree no longer matches) | |
| 27 | | Semantic conflict (git can't auto-resolve) | N/A — operation fails | Stale (rebase impossible) | |
| 28 | |
| 29 | Only fast-forward and trivial rebase preserve attestation validity. Everything else requires the branch owner to re-attest. |
| 30 | |
| 31 | This is a stronger primitive than GitHub's merge queue — they re-run all CI on every rebased commit because their checks aren't pure derivations. Ours are; we can prove invariance for the trivial case. |
| 32 | |
| 33 | ## Staleness as the unified failure mode |
| 34 | |
| 35 | There is no rejection event. There is only **stale** — a state the integrator marks on a request when it cannot progress. The request ref persists; the branch sits where it is; the owner produces fresh attestations or the branch is abandoned. |
| 36 | |
| 37 | Causes of staleness, all surfaced through the same mechanism: |
| 38 | |
| 39 | | Cause | How the integrator detects it | |
| 40 | | --- | --- | |
| 41 | | Rebase would change the tree (conflict) | Three-way merge produces a non-trivial result | |
| 42 | | Policy added a required check that the attestation doesn't cover | Current policy's required checks ⊄ attestation's covered checks | |
| 43 | | Signer's trust score dropped below the policy's threshold | Trust controller updated; threshold for this target no longer met | |
| 44 | | Witness denied an attestation the policy required confirmation on | Async re-derivation produced a different result | |
| 45 | | Attestation inputs garbage-collected from the cache | Required re-derivation impossible | |
| 46 | |
| 47 | All five collapse to: *the attestation no longer satisfies the integrator's policy for this target*. The `request-stale` event carries a structured `reason` field naming which cause(s) applied. |
| 48 | |
| 49 | ## Policy as a derived query over active principles |
| 50 | |
| 51 | The integration policy is not a separate config file. It is **derived from the knowledge graph**. |
| 52 | |
| 53 | A `principle` node with `enforced_by: [check:X]` adds X to the required-checks set for the protected refs it `applies_to`. The integrator computes the current policy by querying: |
| 54 | |
| 55 | ``` |
| 56 | principles where status = active AND enforced_by ≠ ∅ |
| 57 | ``` |
| 58 | |
| 59 | Adding a principle adds requirements. Superseding a principle drops them. Policy changes go through the same integration mechanism as any other change to the knowledge graph (see [knowledge.md](./knowledge.md)). |
| 60 | |
| 61 | Other policy dimensions (trust thresholds per protected ref, integrator key authorizations, witness requirements) are configured similarly — as structured nodes the integrator queries. |
| 62 | |
| 63 | ## What the branch owner does on staleness |
| 64 | |
| 65 | The branch owner has access to a `request-state` query (over bus or git) that returns one of: |
| 66 | |
| 67 | - **Pending** — not yet processed. |
| 68 | - **In-flight** — being processed; possibly waiting on witness confirmation. |
| 69 | - **Integrated** — done. The success event is the durable record. |
| 70 | - **Stale** — cannot progress. Includes the structured reason and a hint at remediation. |
| 71 | |
| 72 | On stale, the branch owner refreshes: |
| 73 | - For tree-conflict staleness: `git rebase` locally onto the new target, resolve conflicts, re-run `nix run .#attest`, atomic push. |
| 74 | - For policy-required-check staleness: add the missing check to local run, re-attest, push. |
| 75 | - For trust-score staleness: address the underlying signal (incident, drift); not always self-resolvable. |
| 76 | - For witness denial: investigate the divergence (reproducibility bug, environment drift); fix and re-attest. |
| 77 | |
| 78 | How the branch owner *does* the refresh is their tooling concern. Manual git + `nix run .#attest` is the baseline. They might opt into klaus-style agents that do it for them. |
| 79 | |
| 80 | ## Agentic resolution is out of scope |
| 81 | |
| 82 | Falls out naturally from the staleness reframe. When attestations go stale, the integrator does nothing — it's the owner's problem. If the owner chooses to dispatch an agent to handle the refresh on their behalf, the agent operates *in the owner's identity context* and produces signed attestations under their key. From the integrator's perspective, fresh attestations show up and integration proceeds. |
| 83 | |
| 84 | This pushes the agent-identity question (still open from [scenarios.md](./scenarios.md)) out of the integrator's critical path. Whatever the resolution there, the integrator is unaffected. |
| 85 | |
| 86 | The architecture *supports* this pattern — agent dispatch is just a controller reaction to a `request-stale` event scoped to the owner's repos. It's tooling, not integrator policy. |
| 87 | |
| 88 | ## Queue mechanics |
| 89 | |
| 90 | **One queue per protected ref.** Strict FIFO. Concurrent integration across different protected refs (different repos, or different protected branches in the same repo) is fine and runs in parallel. |
| 91 | |
| 92 | **Within a queue:** the integrator processes requests one at a time. When a request becomes stale, it's removed from the *active* queue (the request ref still exists in git, just not in the integrator's working set). When the owner pushes fresh attestations, the post-receive hook publishes a new `integration-requested` event and the request re-enters the queue at the back. |
| 93 | |
| 94 | **No priority lanes in v1.** Fairness within a queue is FIFO; cross-queue priority is implicit (different queues run in parallel). |
| 95 | |
| 96 | **No merge groups in v1.** GitHub's "synthesize multiple PRs into a trial merge and validate together" is an optimization for high-throughput scenarios. Not needed at the scales we're designing for; can be added later as an integrator strategy. |
| 97 | |
| 98 | ## Failure semantics |
| 99 | |
| 100 | | Event | When | Repetition | |
| 101 | | --- | --- | --- | |
| 102 | | `integration-succeeded` | Target ref updated | Once per integration | |
| 103 | | `request-stale` | Transition from pending/in-flight to stale | Once per transition; not repeated while still stale | |
| 104 | | `request-invalid` | Structurally malformed (bad signature, missing data, ref not found) | Once; integrator stops processing the request | |
| 105 | |
| 106 | The integrator does **not** spam events. A stale request that sits stale for a week emits one `request-stale` event, not a hundred. The owner queries state when they want to know it. |
| 107 | |
| 108 | A `request-invalid` is distinct from `request-stale`: invalid means *can never be processed as-is* (something is broken about the request itself), stale means *can't be processed against current policy* (refresh might fix it). |
| 109 | |
| 110 | ## Crash recovery |
| 111 | |
| 112 | Integrator state is fully derivable from: |
| 113 | - `refs/the-valley/integration-requests/*` in the bare repo (the durable record of what requests exist). |
| 114 | - The bus event stream (the durable record of what has been processed). |
| 115 | |
| 116 | On restart, the integrator reads both, computes the set of unprocessed requests, and resumes. No internal database, no per-integrator state file. The integrator process is stateless modulo configuration. |
| 117 | |
| 118 | ## Open questions |
| 119 | |
| 120 | See [openquestions.md](./openquestions.md) — items raised here live under *Policy & configuration*, *Cross-repo coordination*, *Attention, routing, and threads*, and *Storage, retention, and evolution*. |