# OIP-0004: Federation: bindings, authority leases and partition behaviour

| Field | Value |
|---|---|
| Status | DRAFT |
| Function | `federate` (section 5.7); no function added, the register keeps exactly twelve |
| Affects | §4.2 (three objects), §5.7 (FED-1 to FED-10), §9 (G-FED-1 closed); two repository tests that named G-FED-1; new schemas `federation-binding`, `authority-lease`, `federation-event`; `registers/functions.yaml` (`federate` CONTRACTED); `registers/requirements.yaml` regenerated; cases CT-GRID-060 to CT-GRID-067, CT-SITE-060, CT-SITE-061; `formal/federation/` (TLA+ model, `make formal`) |
| Protocol version | obsp/0.1 |
| Closes | G-FED-1 (FederationBinding and AuthorityLease schemas and the peer exchange protocol) |
| Depends on | OIP-0008 (signature envelope, `urn:obsp:schema:v3.1:signatureenvelope`), handled as OIP-0003 does; see "Dependency and acceptance order" |
| Relates to | OIP-0003 (signed plans: a peer's orchestration under a lease uses signed plans), section 5.9 (correction candidates) |

## Problem

Section 5.7 requires an explicit FederationBinding, forbids command authority without an unexpired authority lease for
a named scope, and requires federation to survive partition. None of it has a contract: §4.2 lists FederationBinding
and AuthorityLease as gap G-FED-1, and the only federate case (CT-GRID-001) checks a fleet projection. The threat
"partitioned federation" of section 8 has no test. Lease and partition rules are easy to state and easy to get wrong
(two holders after a partition, a holder that keeps writing on an expired lease, a rejoin that silently drops one side's
writes), so the gap row says the design is model-checked before it is normative. This proposal does both: contracts and
rules, and a TLA+ model of the rules checked with TLC.

## Change

### Model

| Object | Schema | What it carries |
|---|---|---|
| FederationBinding | `federation-binding` | exactly two parties (authority UNI, role, trust domain, exchange endpoints of the exchange, event or evidence plane only); scope (namespaces, functions from the twelve, data classes); direction; honoured policies by UNI and digest; retention; lease terms (`grantors`, `quorum`, `maxTermSeconds`, `renewBeforeSeconds`, `maxClockSkewSeconds`); `validFrom`, `validUntil`; exit terms; one signature per party |
| AuthorityLease | `authority-lease` | binding reference (UNI, version, digest); `leaseKind` `GRANT` or `RENEWAL` (a renewal names the lease it renews by UNI and digest); scope (one namespace of the binding, functions, optional data classes); holder; `epoch`; term (`startsAt`, `expiresAt`); the binding's quorum; grantor votes, each a signed envelope with `votedAt` |
| FederationEvent | `federation-event` | binding reference; `eventType` `JOIN`, `LEAVE`, `PARTITION` or `REJOIN`, with one block of that type (JOIN: data classes announced; LEAVE: reason, data on exit, leases released; PARTITION: last contact and the scopes that turn read-only; REJOIN: the writes made while apart, the current version per record, the correction candidates, order `EPOCH_THEN_DIGEST`); the lease epochs known; the observer's signature |

Leases on the same binding and namespace are one scope. Grantors are named by the binding and may include a witness
authority that is not a party, so that two parties can keep a majority while one of them is cut off (the example uses
two parties and one witness, quorum 2). Every signature is a signature envelope (OIP-0008). OIP-0008 is a draft, so
each of the three schemas carries an identical copy of its `$defs.SignatureEnvelope` and `$defs.ProtocolRole`, and the
specification names the envelope by its `$id`, as OIP-0003 does for `plan`.

### Normative text (§5.7; the exact text is in `patches/spec.patch`)

| Id | Requirement (summary) | Rejection code |
|---|---|---|
| FED-1 | no exchange without a binding that names both parties, is signed once by each over its payload and is valid | `OBS-FED-0001` |
| FED-2 | no export of a data class, namespace or function the binding does not list; leases stay inside the binding's scope with a party as holder | `OBS-FED-0002` |
| FED-3 | endpoints are https on the exchange, event or evidence plane; never a control or management path into a site (P7) | |
| FED-4 | command authority only under a lease voted by at least the binding's quorum of distinct listed grantors, quorum more than half; votes no later than the start; term within the maximum and the binding's validity | `OBS-FED-0003` |
| FED-5 | at most one holder per scope per epoch; a new holder takes a higher epoch; a grantor does not vote for a higher epoch or another holder while a lease it voted for has not expired by trusted time plus the skew allowance; receivers reject an older epoch | `OBS-FED-0004` |
| FED-6 | a holder acts only in scope, from the start until expiry less the skew allowance, by trusted time | `OBS-FED-0005` |
| FED-7 | renewal keeps holder, epoch and scope, is voted by quorum and issued before expiry; an expired lease is replaced only by a new grant at a higher epoch | |
| FED-8 | a holder that cannot renew during a partition stops consequential writes at expiry less the allowance and serves read-only; each domain stays authoritative for its own facts (P2); a PARTITION event names the scopes | |
| FED-9 | rejoin reconciles per record by epoch, then digest order; a write that is neither current nor an ancestor of current becomes a correction candidate moved to DISPUTED by a CorrectionNotice (§5.9), never a silent overwrite or deletion (P9) | |
| FED-10 | joins, leaves, partitions and rejoins are signed FederationEvents about and by parties; LEAVE applies the exit terms | |

The three bullets of §5.7 stay; FED-1 to FED-10 make them testable. §4.2 names the three schemas; §9 loses G-FED-1.

### Model check (`patches/formal.patch`)

`formal/federation/Federation.tla` models three trust domains that are all grantors, one scope, lease requests,
separate grantor votes, completion on a majority quorum, renewal in the last tick of a lease, a clock, one partition
and its rejoin, writes by the lease holder replicated on the writer's side, and reconciliation at rejoin by epoch, then
write identifier (standing in for the digest), with every write outside the current version's ancestry kept as a
correction candidate. Invariants:

| Invariant | Rule |
|---|---|
| `SingleHolderPerEpoch` | FED-5: two grants of one scope and epoch have the same holder |
| `ExclusiveAuthority` | FED-5: the grant intervals of two holders of one scope never overlap |
| `NoWriteWithoutLease` | FED-6, FED-8: every write lies inside a grant interval of its writer, scope and epoch |
| `NoLostWrite` | FED-9: every write a domain holds is its current version, an ancestor of it, or a correction candidate |
| `NoErasure` | P9: every write stays in its writer's store |

TLC results (TLC 2.19 of the TLA+ tools release 1.7.4, Java 21, 4 workers; constants in `Federation.cfg`: 3 domains,
1 scope, `Term` 2, `MaxTime` 3, `MaxEpoch` 2, `MaxWrites` 2, `MaxPartitions` 1, symmetry over the domains):

| Configuration | Result | Distinct states | Depth |
|---|---|---|---|
| `Federation.cfg` | Model checking completed. No error has been found (3,947,701 states generated) | 756,200 | 22 |
| `FederationT4.cfg` (`MaxTime` 4) | Model checking completed. No error has been found (26,401,981 states generated) | 5,074,770 | 26 |

Each rule is shown to be needed by a mutant configuration that switches it off, and the conflict path is shown to be
reachable by a witness; TLC reports each named invariant violated with a counterexample:

| Configuration | Rule switched off | TLC result |
|---|---|---|
| `MutantGrantorNoWait.cfg` | FED-5 (grantor waits for expiry) | `ExclusiveAuthority` violated at depth 10: two holders' leases overlap |
| `MutantNoStopOnExpiry.cfg` | FED-6, FED-8 (stop at expiry) | `NoWriteWithoutLease` violated at depth 9: a write on an expired lease |
| `MutantNoConflictFlag.cfg` | FED-9 (correction candidates) | `NoLostWrite` violated at depth 15: a partition-era write lost at rejoin |
| `WitnessConflict.cfg` | none | `NoConflictEver` violated at depth 14, as intended: a conflicting write is reached and kept as a candidate |

Abstractions, stated in `formal/federation/README.md`: one trusted clock (FED-5 and FED-6 take the skew out with the
binding's allowance, so the model checks skew zero); messages are lost or delayed but never altered (envelopes are
signed); requests are voted on separately and completed atomically by the requester; data classes are left to the
schemas and invariants; the bounds are small. A TLC run is evidence for these bounds only, not a proof for all sizes;
liveness (that a lease is eventually granted after a partition heals) is not checked.

`make formal` runs the six configurations (`formal/federation/check.sh`). It is not part of `make check`: it needs a
Java runtime and the TLA+ tools jar, which is not stored in the repository (pinned version and digest in the README).

### Reference checks (`patches/cases-invariants.patch`)

`sdk/python/src/obsp/federation.py` holds nine suite invariants, registered by `conformance.py` with one import placed
where no other draft edits the file. They compare documents: a binding's parties and signatures, leases with their
binding and with each other, renewals with the leases they renew, recorded writes with the leases they name, the
reconciliation with its writes, candidates with their correction notices. Envelopes are checked for the payload digest
they claim; the cryptographic check is the envelope's rule (OIP-0008 SIG-2) and is added when that proposal is accepted.

## Dependency and acceptance order

1. OIP-0008 first, then this proposal; the patches here apply to the base tree alone and do not need it. If OIP-0008
   is accepted first, the rebase replaces the three copied definitions by references once the envelope schema exists,
   adds rows `federation-binding` (member `signatures[].envelope`), `authority-lease` (`votes[].envelope`) and
   `federation-event` (`signature`) to `registers/algorithms.yaml`, and makes the invariants call the OIP-0008
   verifier. If OIP-0008 is not accepted, this proposal stands with the copies and signature verification stays an
   open question.
2. With OIP-0008, `correction-notice.signature` changes form; the example notice `correction-notice.federation-rejoin`
   of this proposal is regenerated with `tools/gen_federation_examples.py` after that change.

Overlaps with the other drafts (checked with `git apply --check` after these patches are applied to the base tree):
every patch of OIP-0006, OIP-0007, OIP-0008 and OIP-0009, the OIP-0001 and OIP-0005 patches other than `spec.patch`,
OIP-0002 `schemas-fixtures.patch` and `cases-invariants.patch`, and OIP-0003 `registers.patch` and
`schemas-fixtures.patch` still apply. Six do not, each a small rebase for whichever proposal is accepted second:

- the `spec.patch` of OIP-0001, OIP-0002 and OIP-0003 remove their own §9 gap rows next to the G-FED-1 row this
  proposal removes (two proposals that each close a gap always overlap there);
- OIP-0005 `spec.patch` adds its §4.2 row with the FederationBinding row as context, which this proposal rewrites;
- OIP-0002 `registers.patch` rewrites `test_gap_must_be_in_spec` in `tests/test_validate.py` to use G-FED-1 (because it
  closes G-PROVE-1), next to `test_object_needs_schema`, which this proposal changes from `federation-binding` to a name
  that has no schema; once both are accepted that test needs a gap that is still open, or a gap row written by the test;
- OIP-0003 `cases-invariants.patch` regenerates `registers/requirements.yaml`, as this proposal does (regenerate with
  `python3 tools/requirements.py` after the rebase).

The registration import in `conformance.py` sits between `load_evidence` and `run_case`, where no other draft edits
the file. `tests/test_requirements.py` now checks gap propagation for every function that still has a gap, instead of
naming G-FED-1, so it keeps passing as other gaps close.

## Compatibility

Additive: three new schemas, no change to an existing contract. Deployments that federate today without a binding no
longer conform once this is accepted (FED-1), which is the intent of §5.7. The new contracts carry `schemaVersion`
`3.1.0` like every other contract of obsp/0.1.

## Tests

| Requirement | Case | Kind | Checks |
|---|---|---|---|
| FED-1 to FED-10 | CT-GRID-060 (GRID) | schema | binding, lease and event documents validate |
| FED-1, FED-3, FED-4 | CT-GRID-061 (GRID) | invariant | `federation-binding-signed-by-parties` |
| FED-2 | CT-GRID-062 (GRID) | invariant | `federation-scope-bound` |
| FED-4 | CT-GRID-063 (GRID) | invariant | `federation-lease-quorum` |
| FED-5 | CT-GRID-064 (GRID) | invariant | `federation-single-holder` |
| FED-7 | CT-GRID-065 (GRID) | invariant | `federation-renewal-before-expiry` |
| FED-6, FED-8 | CT-GRID-066 (GRID) | invariant | `federation-writes-within-lease` |
| FED-9, FED-10 | CT-GRID-067 (GRID) | invariant | `federation-rejoin-reconciled`, `federation-events-signed` |
| FED-9 (`correct`) | CT-SITE-060 (SITE, GRID) | invariant | `federation-candidates-disputed`, `federation-rejoin-reconciled` |
| FED-5, FED-6, FED-8 (`orchestrate`) | CT-SITE-061 (SITE, GRID) | invariant | `federation-single-holder`, `federation-writes-within-lease` |

The example sets of OBSP-SITE and OBSP-GRID gain one scenario (`tools/gen_federation_examples.py`): a binding between
two domains with a witness; B's lease at epoch 1 and its renewal; a partition that turns B read-only at its lease's
expiry less the skew allowance; A's lease at epoch 2 voted by A and the witness; both wrote one record while apart; on
rejoin A's epoch-2 write is current and B's write is a correction candidate disputed by a CorrectionNotice.

Fixtures: positive `federation-binding.valid`, `authority-lease.valid`, `authority-lease.renewal.valid`,
`federation-event.join.valid`, `.partition.valid`, `.rejoin.valid`, `.leave.valid`. Negative, each failing one rule:
`federation-binding.one-party`, `.single-signature`, `.no-honoured-policies`, `.no-data-classes`, `.unknown-function`,
`.control-plane-endpoint`, `.plain-http-endpoint`, `.quorum-zero`, `.missing-clock-skew`, `.missing-exit-terms`,
`.envelope-without-signature`; `authority-lease.epoch-zero`, `.missing-expiry`, `.no-votes`, `.empty-functions`,
`.renewal-without-previous`, `.grant-with-renewal`, `.unknown-kind`, `.missing-binding-ref`;
`federation-event.unknown-type`, `.rejoin-without-reconciliation`, `.partition-with-rejoin-block`, `.last-writer-wins`,
`.candidate-without-notice`, `.missing-signature`, `.write-without-lease`. Two holders, overlap, late renewal, writes
after expiry and silent overwrites compare documents and times, so their negative cases are unit tests:
`sdk/python/tests/test_federation.py` (35 tests: the scenario against every invariant and schema, the example sets
through the runner, the generator up to date, and one or more failing mutations per invariant, including the
same-epoch digest tie-break and an ancestor that is not a conflict). `tests/test_formal.py` checks that every invariant
a configuration names is defined in the model and that the README records the TLC results.

## Binding note: dataspace protocols (informative, not in the patches)

A federation binding can be negotiated over the Eclipse Dataspace Protocol (catalog protocol, contract negotiation
protocol, transfer process protocol; edition and clause numbers `checked: not checked`). A possible mapping: a JOIN
event's announced data classes correspond to the datasets a catalog offers; a signed FederationBinding corresponds to
the contract agreement that ends a negotiation, the binding's honoured policies to the agreement's policy; each
exchange under the binding corresponds to a transfer process that names the agreement; LEAVE corresponds to
terminating the agreement. Authority leases, epochs and partition behaviour have no counterpart there and stay OBSP's.
This note is not normative and adds no relation row; a binding profile under `bindings/` would follow the pattern of
OIP-0009 if the editors want one.

## Open questions

1. Clock skew: FED-5 and FED-6 rely on a per-binding `maxClockSkewSeconds` and on trusted time (§5.4). Should the
   allowance be bounded by the time-quality of both parties (`time-quality`), and should a holder whose time is not
   trusted be read-only at once?
2. Witness grantors: the binding may name grantors that are not parties. Should a witness be a registered role
   (`VERIFIER`?) with its own key record, and may one witness serve several bindings?
3. Scope granularity: one namespace is one scope. Should overlapping namespaces (a prefix and a sub-namespace) be
   forbidden in one binding, or should FED-5 compare prefixes?
4. Digest order: FED-9 orders equal-epoch writes by the digest's bytes, so the order depends on the digest algorithm
   when two writes use different algorithms. Require `sha256` for the comparison?
5. Peer protocol: the gap row named "the peer exchange protocol". This proposal defines what is exchanged and the
   rules; the operations (request and vote over the exchange plane, event delivery) would extend the gateway API
   (`schemas/openapi/`) in a later proposal, with live harness cases for lease refusal.
6. Liveness and larger bounds: the model checks safety for small bounds. Should the editors require a liveness
   property (a lease is granted within a bound after rejoin) or a proof (TLAPS) before acceptance?
7. Not checked against the sources: the dataspace protocol mapping above, and the TLA+ tools version and digest
   recorded in the README (they were the release fetched for this run).
8. `schemaVersion`: the new schemas use `3.1.0`; the same question as OIP-0003 and OIP-0008 on when a new version is
   cut.

Acceptance needs appointed editors and a last-call window. Until then this proposal stays at DRAFT and the live tree
keeps gap G-FED-1.
