# Larger systems: units and roles > Large systems are built from units of the same shape, and a parent trusts its children through contracts called roles. - Site: CanonBall. The lifecycle agents can run and people can trust. - HTML page: https://canonball.org/guide/composition/ - This file: https://canonball.org/guide/composition/agents.txt --- Guide part: Building with it. Chapter 25 of 28. Key points: - A unit is a separately owned part of a system with its own canon, build and Verification Record. - A parent depends on roles. One file, the assembly, says who fills them. - Evidence composes through contract tests, so the whole system never has to be verified at once. ## The problem with scale A promise such as "a customer is never charged twice" is kept by several services together: checkout starts the charge, payments executes it, and orders records it. No single team's canon can contain the whole promise, and three teams' code cannot be verified as one system. CanonBall handles this with two ideas, units and roles. ## A unit A **unit** is a separately owned part of a system with its own canon, blueprint, build and Verification Record. A service is a unit, and so are the subdomain that contains it and the domain above that; the shape repeats at every level of the tree, and only the size differs. In the terms of domain-driven design, a unit is close to a bounded context. A tree of units is kept consistent by the following rules. 1. **A unit exists only where there is an owner and an interface.** Anything else is a module inside someone's design. 2. **Authority runs downward.** A child's canon may not contradict its parent's. 3. **Constraints and policy are inherited, and may only get stricter.** 4. **The vocabulary is inherited.** A child redefines a word only with an explicit translation. 5. **Evidence runs upward through contracts.** A parent never reads a child's code. 6. **One team owns a unit.** A team may own several. ## Two relationships people confuse **Governance** means that the parent sets rules every descendant follows: the constraints, the policy and the vocabulary. The child adopts them, and the parent does not track how. **Composition** means that a parent promise is delivered by several parts working together. The parent designs how they cooperate, so it must know what it needs from each part, though not who provides it. Roles exist to serve composition. ## A role A **role** is a contract that a unit defines for something it needs someone else to provide. It is written in the style of design by contract: what the provider guarantees, and what it may assume of its caller. The same word names the agent roles, such as builder and reviewer, described under [people and agents](../people-and-agents/agents.txt); a role between units is always a contract. ```markdown id: ROLE-payer version: 2 owner: post-purchase Guarantees: - A charge with key K executes at most once. - Retrying charge(K) returns the original result. Assumes: - The caller supplies a K that is unique per order. ``` A role is published as a package. It holds the contract, which the parent designs against; the **contract tests**, which any provider must pass; and a **fake**, a stand-in built from the contract for the parent's own tests. No design refers to a concrete partner: the parent writes its design against `ROLE-payer`, and one file, the **assembly**, says who fills each role. [Figure: A parent depends on roles. The assembly says who fills them.] A parent unit, post-purchase, defines three roles: order book, payer and gateway. The assembly binds them to the units orders and payments and to an outside vendor. Evidence flows upward: each provider passes the role's contract tests, and the parent reads that result without reading the provider's code. ```yaml bindings: ROLE-orderbook: { provider: orders } ROLE-payer: { provider: payments } ROLE-gateway: { provider: external/acme-pay, fallback: "queue refunds; settle on recovery" } ``` Replacing a provider is a one-line change to that file, and the parent's proofs are unaffected. ## How evidence composes A promise delivered by several providers has an assurance case with three parts, none of which names a child. - The **protocol** part shows that the cooperation is correct, given the contracts. It is a model check over the role contracts alone, with failures, retries and duplicates as hostile inputs. - The **assembly** part shows that every role is filled by a provider that meets it. It is computed from each provider's own Record. - The **integration** part shows that the assembled system behaves, through end-to-end checks. The parent's proof takes each role's guarantees as given, and each provider's Record shows that its implementation meets the contract, so the whole system never has to be verified at once. This is assume-guarantee reasoning, and because each team verifies only its own part, it is expected to scale with the number of teams. ## Staleness crosses boundaries Each role has a content hash computed from its contract, its tests and its fake, so if the parent changes the role, every provider's test result goes stale. Each team also pins the [CanonBall](../canonball/agents.txt) versions it depends on, and re-runs the checks that rest on a provider when that provider publishes a new one. ## Outside dependencies are roles too A third-party payment gateway is handled in the same way: define the role you need, bind the current vendor in the assembly, and write your assumptions against the role. In the refunds canon, ASM-011 (the payment gateway pays at most once per request key) is an assumption of this kind. An assumption on your side is a promise on the vendor's side. If the vendor does not publish evidence, the binding carries its documentation in place of a Record, and your treatment of the assumption has to do more of the work. Each binding is marked **hard** or **soft**. A hard binding means your capability fails when the provider fails, and a soft one means it degrades. A hard binding needs a fallback, or a risk that someone has accepted in writing. ## One coordinator per protocol A cooperation among five services with no owner is a common source of distributed failures, so the responsibility for running a protocol is assigned to exactly one unit, and the others promise only local guarantees. ## Independent releases Each unit moves its own milestones through the [seven stations](../workflow/agents.txt) and releases its own CanonBalls, so teams need not wait for each other. A consumer's milestone can be released while the provider's is still being built, and the affected clauses show as *pending* until the provider passes the contract tests. ## What it costs The cost is two extra artifacts, the role contracts and the assembly file. A role with a single provider can look like an interface with one implementation, but it is worth keeping, because the contract records an allocation of responsibility that would have had to be written down in any case. That leaves the assembly as the only new file, and the first change of provider is expected to repay it. A parent's status is only as good as its providers' contract tests, a risk discussed under [open bets](../open-bets/agents.txt). --- ## References - Previous chapter: An architecture agents can work in: https://canonball.org/guide/architecture/agents.txt - Next chapter: Start in a week: https://canonball.org/guide/start/agents.txt - Related: What a CanonBall is: https://canonball.org/guide/canonball/agents.txt - Related: The repository layout: https://canonball.org/guide/repository/agents.txt - Related: Evidence and the Verification Record: https://canonball.org/guide/evidence/agents.txt - Related: How work moves: https://canonball.org/guide/workflow/agents.txt - Glossary: https://canonball.org/glossary/agents.txt - Site overview and index of every chapter: https://canonball.org/agents.txt