| 1 | # Integration |
| 2 | |
| 3 | How a change lands. The position this implements is |
| 4 | [dcr-439b771](../.the-valley/decisions/dcr-439b771-integration-occ-over-content-addressed-evidence.md): |
| 5 | integration is optimistic concurrency control over content-addressed evidence. This document is the |
| 6 | detailed design — the request's shape, the rules in the order they are applied, what the integrator |
| 7 | writes down, and what it publishes. The bet behind it is |
| 8 | [architecture.md](./architecture.md#bet-a-pull-based-integrator-not-a-pre-receive-gate)'s pull-based |
| 9 | integrator, and the failure mode is staleness. |
| 10 | |
| 11 | The integrator runs no check. Verification happens where a change is authored |
| 12 | ([verification.md](./verification.md)), and the serialized section at the commit point is digest |
| 13 | comparison and a ref write. |
| 14 | |
| 15 | ## A request is a ref |
| 16 | |
| 17 | A contributor asks for integration by pushing one ref: |
| 18 | |
| 19 | ``` |
| 20 | refs/the-valley/integration-requests/<target>/<change> -> commit |
| 21 | ``` |
| 22 | |
| 23 | The target segment names the protected branch, so the stream is `refs/heads/<target>`; the last |
| 24 | segment names the change. The commit is the change's head. Its tree is what the attestations were |
| 25 | produced over, and the delta is the merge base with the target's tip up to that head. Nothing else |
| 26 | is encoded, because nothing else has to be: a change is a diff targeting a stream |
| 27 | ([ida-93e4f91](../.the-valley/ideas/ida-93e4f91-changes-not-branches.md)), and every part of that is |
| 28 | already in git. |
| 29 | |
| 30 | This settles what [contribute.md](./contribute.md) left open at its step 7. The attestations |
| 31 | themselves arrive the way that document already describes, at |
| 32 | `refs/the-valley/attestations/<tree digest>/<signer key hash>`, and the integrator finds them by |
| 33 | digesting the request's tree. |
| 34 | |
| 35 | ## The controller polls, deliberately |
| 36 | |
| 37 | The integrator is a reconciliation loop over those refs. Each pass lists the pending requests, judges |
| 38 | each against the current tip of its target, and acts where the answer has changed since the last |
| 39 | pass. It subscribes to nothing. |
| 40 | |
| 41 | That is not a shortcut around the event bus; it is what the bus's own contract says to do. The bus is |
| 42 | unauthenticated ([bd-d853d9c](../.the-valley/bugs/bd-d853d9c-bus-unauthenticated.md)), and that bug's |
| 43 | gate is exactly "before any automated consumer acts on an event". And per |
| 44 | [architecture.md](./architecture.md#bet-git-as-event-source--a-log-not-a-workflow-engine) the |
| 45 | requests are durable in git with the bus as a projection over them, so level-triggering on the |
| 46 | durable state is the converging shape and a subscription would be the lossy one. Consuming events |
| 47 | arrives in a later phase, and the integrator does not become a different program when it does. |
| 48 | |
| 49 | Its memory of what it last decided is durable in git too, at |
| 50 | `refs/the-valley/integration-outcomes/<target>/<change>`: the request's head, the tip it was judged |
| 51 | against, and the outcome. A restarted controller does not re-announce a judgement it has already |
| 52 | made, and an unchanged pair is not re-judged — which is what keeps a level-triggered loop from being |
| 53 | a retry storm. |
| 54 | |
| 55 | ## The commit rule |
| 56 | |
| 57 | Applied in this order. The whole of it is a function of data, so it is stated once in |
| 58 | `integrator/verdict.go` and exercised without a repository by the `integrator-unit` check. |
| 59 | |
| 60 | **1. The delta must apply cleanly to the tip.** A three-way merge of the tip and the head against the |
| 61 | base the delta was authored against, with no working tree. A conflict means resolving it would |
| 62 | produce a tree nobody has authored; that resolution is new authorship, and new authorship needs new |
| 63 | evidence. So a conflict is stale with **every** required check named: retry is not partial from a |
| 64 | tree nobody wrote. |
| 65 | |
| 66 | **2. The required checks come from the policy at the target tip.** The deriver of |
| 67 | [dcr-f41f718](../.the-valley/decisions/dcr-f41f718-declared-verification-policy.md) — `valley checks` |
| 68 | — matches the diff against the composed classes. Both layers are read from the integrator's own side: |
| 69 | the project layer from a checkout of the target tip, the instance layer from where the integrator is |
| 70 | configured to find it. A change never supplies the policy that gates it. |
| 71 | |
| 72 | **3. A pure check transfers by closure-digest equality.** The integrator recomputes the check's |
| 73 | input-closure digest over the tree that would land — `attest inputs`, which evaluates and digests and |
| 74 | builds nothing — and compares it with the digest the attestation recorded. Equality means the landed |
| 75 | tree presents the same computation over the same inputs, so the recorded result is the result on the |
| 76 | new base too. Inequality means that check, and only that check, is stale. When the landed tree is the |
| 77 | attested tree there is nothing to compare and the evidence stands by construction. |
| 78 | |
| 79 | **4. An effectful check transfers while its scope is untouched and its observation is fresh.** Both |
| 80 | must hold: no intervening landed change touched the paths of a class that requires the check, and the |
| 81 | observation is younger than the validity window Δ the policy declares for it. The first is asked of |
| 82 | the deriver rather than matched here — the classes it reports over the intervening range are exactly |
| 83 | the classes those landings touched. Absent a declared window the check is re-demanded: a window |
| 84 | nobody wrote down is not a window a tool may invent. |
| 85 | |
| 86 | **5. Everything standing lands.** The protected ref is fast-forwarded by a compare-and-swap against |
| 87 | the tip the verdict was computed over, so a request that lost a race lands nothing and is judged again |
| 88 | next pass against the tip that won. That local ref write is the integrator's own privilege, and the |
| 89 | structural invariant on the bare repo is what makes it the only one. |
| 90 | |
| 91 | **6. Anything invalidated is one `request-stale` naming exactly those checks.** The request ref stays |
| 92 | where it is, so resubmitting is re-attesting the named checks and pushing again. |
| 93 | |
| 94 | ## What the integrator writes down |
| 95 | |
| 96 | Two statements, side by side in one attestation ref per subject. |
| 97 | |
| 98 | The contributor's original notes gain the integrator's signature as **sibling lines under the text |
| 99 | they already carry** — several parties attesting to one statement, which is the composition |
| 100 | [dcr-0de694f](../.the-valley/decisions/dcr-0de694f-phase2-attestation-shape.md) already provides. |
| 101 | Nothing about the contributor's statement changes; it stays about the tree it was made over, and it |
| 102 | stays at a ref keyed by that tree's digest. |
| 103 | |
| 104 | The **transfer statement** is the integrator's own claim, and it is a separate statement because it is |
| 105 | about a different tree: the one that landed. Its predicate type is |
| 106 | `the-valley/integration/transfer/v1`, and it records the change, the target stream, the base the |
| 107 | evidence was produced over, the policy the verdict was derived under, and the per-check verdicts — |
| 108 | each citing the statement it is about by a digest of the bytes that statement's signature covers. The |
| 109 | policy it names is the one the integrator composed from its own checkout at the target tip, never the |
| 110 | submitted tree, which is the direction |
| 111 | [bd-eaefe82](../.the-valley/bugs/bd-eaefe82-check-definitions-come-from-the-branch.md) points at. |
| 112 | |
| 113 | When nothing intervened the two trees are the same tree and both notes sit under one ref. When the |
| 114 | delta landed on a moved tip they are two refs, each keyed by the tree its statement is about. |
| 115 | |
| 116 | ## What it publishes |
| 117 | |
| 118 | Two events, in [schema/events.cue](../schema/events.cue), on |
| 119 | `valley.git.<repo>.integration-succeeded` and `valley.git.<repo>.request-stale`. The mechanism is the |
| 120 | post-receive hook's, exactly: one `nats pub`, best-effort, never fatal — git is the source of truth |
| 121 | and the bus is the replaceable component. |
| 122 | |
| 123 | **There is no rejection event, on purpose.** A note whose signature does not check out, text that is |
| 124 | not the written form of what it says, or a statement about a different tree is not staleness: |
| 125 | re-attesting would not fix it, and calling it stale would tell a contributor to redo work that is not |
| 126 | the problem. Rather than stretch either event to cover a case they do not describe, the vocabulary |
| 127 | stays as it is and a refusal is the integrator's own output. Naming that case is a decision this |
| 128 | build deliberately does not take alone. |
| 129 | |
| 130 | ## Identity, for now |
| 131 | |
| 132 | The integrator signs with a key given by configuration, and accepts attestations from the signers in |
| 133 | a known-signers file — one verifier key per line, the format |
| 134 | [dcr-de9d996](../.the-valley/decisions/dcr-de9d996-statement-text-and-signed-note.md) fixes. |
| 135 | |
| 136 | That file is the registry's interim compilation. Identity is a governed registry |
| 137 | ([dcr-b87f6e8](../.the-valley/decisions/dcr-b87f6e8-identity-is-a-governed-registry.md)) whose |
| 138 | entries compile into what each enforcement boundary checks, and no compiler exists yet, so the |
| 139 | compiled artifact is supplied directly. Nothing here parses a registry, and nothing should start to |
| 140 | until that decision's first compiler ships. |
| 141 | |
| 142 | An attestation signed by nobody in the file does not transfer, and that is reported per check rather |
| 143 | than treated as a forgery. What to do about an unknown attester — re-verification, trust measurement, |
| 144 | a threshold — belongs to [Phase 6](./roadmap.md#phase-6--trust-backstop) and is deliberately absent. |
| 145 | |
| 146 | ## What is deliberately absent |
| 147 | |
| 148 | - **Bus consumption.** Above. |
| 149 | - **Batching, reservations, sharding.** The known knobs for queue semantics under contention |
| 150 | ([dcr-439b771](../.the-valley/decisions/dcr-439b771-integration-occ-over-content-addressed-evidence.md), |
| 151 | _Open_). None is designed, and the verdict stays per-change so none is precluded. |
| 152 | - **Trust thresholds and re-verification.** Above. |
| 153 | - **Self-integration.** The integrator is code in a repo; how its own changes land is open where it |
| 154 | already is ([openquestions.md](./openquestions.md)). |
| 155 | - **Any transport but refs.** The verdict is a function of a change, a snapshot and the known signers, |
| 156 | with no git, nix or clock inside it, so a second transport supplies those three and reuses the rule |
| 157 | unchanged. |
| 158 | - **Check definitions from the instance's pin.** The policy comes from the integrator's side; the |
| 159 | check derivations still come from the tree being gated. That is |
| 160 | [bd-eaefe82](../.the-valley/bugs/bd-eaefe82-check-definitions-come-from-the-branch.md), and an |
| 161 | enforcement point existing now is what turns it from a structural defect into a live one. |