diff --git a/doc/2026-11-09_design_id5813.md b/doc/2026-11-09_design_id5813.md new file mode 100644 index 0000000000..1de5164b95 --- /dev/null +++ b/doc/2026-11-09_design_id5813.md @@ -0,0 +1,108 @@ +# Design: relay & proxy standard protocol — ID-aware routing, durable store-and-forward, and the dumb-pipe fallback + +- Date: 2026-11-09 +- Status: **decision-ready proposal** — design only; no relay implementation here. +- Issue: [#5813](https://github.com/developing-today/code/issues/5813), parent [#5674](https://github.com/developing-today/code/issues/5674), programme epic [#2056](https://github.com/developing-today/code/issues/2056). +- Additive to: [#2036](https://github.com/developing-today/code/issues/2036) decision record `doc/2026-11-01_design_id2036.md` (PR #3833). This design **consumes** #2036's envelopes, membership-before-forward rule, hop records, and privacy posture; it does not re-decide them. Anything here that would change a #5674 or #2036 decision is flagged explicitly in §7 — there is none. +- Sibling alignment (by reference; none have landed records yet): envelope bytes [#5808](https://github.com/developing-today/code/issues/5808), version negotiation [#5809](https://github.com/developing-today/code/issues/5809), framing per carrier class [#5810](https://github.com/developing-today/code/issues/5810). +- Consumed by reference (not duplicated): relay trust model and broker authorization [#5795](https://github.com/developing-today/code/issues/5795). Security binding review follows in [#5815](https://github.com/developing-today/code/issues/5815). + +## Decision to make + +#5674 names relays and proxies as first-class standard protocols a third party can implement, and #5813 must settle: the chosen shape (dumb opaque pipe vs ID-aware routing vs durable store-and-forward), lookup vs publish/home-relay roles and multi-relay behaviour, delivery/retention/receipt guarantees, bounded backpressure and quotas, and stateless vs stateful scaling — plus the rejected alternatives and the invariants implementation must hold. + +## Recommendation + +**Three modes, one standard.** ID-aware relay is the standard protocol. The dumb opaque pipe is a constrained fallback for carriers that cannot speak ID (corporate proxies, port-443-only paths). Durable store-and-forward is an explicit opt-in offline-delivery mode, not the default. All three modes carry #2036 envelopes byte-identical — a relay never rewrites an origin envelope, per #2036 invariant 1. + +1. **ID-aware relay (the standard): routes on the envelope only, never reads payloads.** The relay parses the routing envelope (recipient/group ID, message ID, class, hop chain), applies the #2036 §4.2 membership-before-forward check, appends its signed hop record, and forwards. Payloads stay end-to-end opaque; the relay learns exactly the routing fields and nothing else (§5 privacy). +2. **Home-relay roles with lookup, no global directory.** Each identity publishes its current relay set (publish); a sender looks up the recipient's relay set through the existing discovery surface and sends via one or more of them (lookup). Multi-relay sends race — first delivery wins, duplicates collapse by stable message ID (#2036 §4.1). Relays never invent membership or addressing; a relay that cannot route a recipient refuses with a defined error rather than guessing. +3. **Delivery is at-most-once live forwarding by default.** The standard relay keeps no offline queue and makes no redelivery promise: a disconnected recipient's live frames are lost by design (#2036 class table), while chained facts remain recoverable through range sync. Whoever needs offline delivery opts into store-and-forward explicitly (§3). +4. **Store-and-forward is bounded and declared.** Per-recipient queues with a TTL and a byte quota (limits in the `WorldLimits` style, defaults set at implementation with the retention policy of #2036 §4.6, not fixed here). Expiry is a declared retention outcome reported to the sender where the session allows — never silent, never a validity change. Receipts are signed hop-level delivery acknowledgements: they prove a relay accepted custody, not that the content is valid. +5. **Backpressure is bounded and end-to-end.** Per-sender bounded queues; drop-newest with accounting when full; quota/rate-limit signalling as relay control frames so pressure propagates to the sender instead of OOMing the relay. A slow recipient degrades its own delivery, never the relay's stability (#5674 §5 requirement). +6. **Scale stateless-first.** The forwarding path is stateless: no per-session stickiness, any relay instance can forward any message, fleets scale horizontally behind the published relay set. Only store-and-forward custody is stateful, sharded by recipient ID (sticky by hash) so redelivery responsibility is unambiguous. + +## §1 Modes and when each applies + +| Mode | What the relay understands | When to use | What it never does | +| --- | --- | --- | --- | +| **ID-aware relay (standard)** | Routing envelope + membership frontier | Default for all ID traffic needing a relay | Read payloads; rewrite envelopes; invent membership | +| **Dumb opaque pipe (fallback)** | Nothing — bytes in, bytes out | Carriers that cannot speak ID (port-443-only proxies, CONNECT tunnels) | Apply ID policy; it is transport only, and both endpoints must still do full #2036 validation | +| **Store-and-forward (opt-in)** | Envelope + custody queue per recipient | Recipient opts in for offline delivery | Queue by default; retain past TTL/quota; merge, reorder, or treat custody as validity | + +A session negotiates the mode at relay handshake; the handshake reuses the #2036 session concepts and the version negotiation direction of #5809. Downgrade pressure (an attacker forcing dumb-pipe where ID-aware was available) is detectable: endpoints record the negotiated mode in the session header, and an unexpected mode is a policy event, per the #5674 §6 downgrade-protection direction. + +## §2 Roles: publish, lookup, multi-relay + +- **Publish:** an identity's relay set is published alongside its existing identity material; updates are signed by the identity so a relay cannot advertise custody it was not granted. +- **Lookup:** senders resolve a recipient's relay set at send time and cache it for the session; stale entries fail safe to refusal, never to misdelivery. +- **Multi-relay:** a sender MAY use more than one of the recipient's relays (latency race, path redundancy). First valid delivery wins; every copy carries the same stable message ID so dedupe is exact. Hop records distinguish the paths for audit without affecting validity. +- **Multi-hop:** relay-to-relay forwarding follows #2036's hop rules unchanged (append own record, refuse own prior hop, four-hop default cap). This design adds no new hop semantics. + +## §3 Delivery, retention, receipts + +- **Standard relay:** at-most-once, no queue, no redelivery. Loss is an availability outcome; chained facts catch up via range sync, live frames do not (#2036 §4.2). +- **Store-and-forward custody:** accepted only where the recipient opted in. Bounded by TTL and per-recipient byte quota; on expiry the message is dropped and the drop is accounted in the relay's delivery report. Custody never converts an invalid message into a valid one — the membership-before-forward check applies before custody, not after. +- **Receipts:** a signed statement `(relay_id, message_id, accepted_at, custody|forwarded)`. Receipts are audit evidence for the delivery report; they are not message validity, not read receipts, and not a substitute for #2036 checkpoints or attestations. + +## §4 Backpressure and quotas + +- Queues are bounded per sender and per recipient; bounds are runtime-configurable (`WorldLimits` style), not protocol constants. +- On overload: drop-newest with a counter, emit quota/rate-limit control frames toward the sender, and shed store-and-forward custody before live forwarding (live traffic has no second chance; custody can be re-offered). +- Control frames are versioned under the same schema major as the envelope (#5808 direction) and negotiated per #5809, framed per carrier per #5810. A relay that cannot parse a control frame must fail safe to refusal, not to unbounded acceptance. +- Conformance: a relay must demonstrate bounded memory under a slow-recipient soak test — this is the test that makes §4 real. + +## §5 Privacy at the relay + +The relay may see exactly: recipient/group ID, message ID, class, hop chain, and custody metadata it created itself. Everything else — payloads, session MACs, checkpoint contents — is end-to-end opaque per #2036 §4.5/§5. Metadata minimisation beyond this list belongs to #5795 and is consumed from there, not restated here. Store-and-forward custody extends what the relay *holds* (ciphertext plus the routing fields above) but not what it may *read*. + +## §6 Scaling + +Forwarding scales horizontally with no coordination: any instance holding the published relay identity can forward. Stickiness is forbidden on the forwarding path (it would turn a fleet into a set of single points of failure). Store-and-forward shards by recipient-ID hash; shard ownership changes are explicit handoffs with custody transfer, never silent drops. Observability follows #5674 §7 (per-relay bytes/frames, latency, error classes, negotiation outcomes) plus custody depth and drop-accounting counters. + +## §7 Parent-decision touch: none + +No answer in this design changes a #5674 or #2036 decision: + +- Envelopes, message IDs, hop rules, membership-before-forward, checkpointing, and the no-central-broker stance are consumed unchanged from #2036. +- Envelope bytes, version negotiation, and framing are aligned by reference to #5808/#5809/#5810's issue-stated direction; when those records land, this design gets a re-check pass for drift, filed as a follow-on, not a silent fork. +- Trust-model substance (capabilities, allowlists, compromised-broker analysis) is #5795's to decide; this design names the seams it needs (relay authentication at handshake, authorization before forward/custody) and stops there. +- Security binding review is #5815's; this design is its input. + +## Viable options + +| Option | Trade-offs | Decision | +| --- | --- | --- | +| **ID-aware relay as standard + dumb-pipe fallback + opt-in store-and-forward (this design)** | Standard path enforces membership and audit; fallback keeps restrictive networks reachable; offline delivery is explicit and bounded. Costs a relay handshake that negotiates mode. | **Recommend.** It is the only shape that meets the issue's brief without re-deciding #2036. | +| **Dumb opaque pipe as the standard** | Simplest relay to implement and operate; zero ID awareness needed. | Reject as standard: no membership-before-forward, no hop audit, no quota signalling — it discards every property #2036 §4.2 requires of an application relay. Keep as fallback only. | +| **Durable store-and-forward as the default** | Offline delivery everywhere; recipients never miss traffic. | Reject as default: unbounded custody becomes a mailbox the protocol must then retain, compact, and secure; contradicts #2036's live-frame loss-by-design and creates the central-availability trust #2036 rejects. Opt-in only. | +| **Central application broker** | Simple rendezvous, ordering, policy. | Reject: already rejected by #2036 (central availability/trust becomes message validity). Restated here so the relay fleet is never mistaken for one. | +| **Sync-emulation relay (continuous two-way sync through the relay)** | Feels like seamless connectivity. | Reject: couples relay availability to session liveness and fights #2036's multi-head model; range sync already covers durable catch-up without the relay. | + +## §8 Invariants implementation must hold + +1. **Envelopes are never rewritten by a relay.** Origin bytes verify identically over direct and relayed paths (#2036 invariant 1); hop records are the only relay-added data. +2. **No unauthorized onward propagation or custody.** The #2036 §4.2 check runs before forward *and* before store-and-forward custody. +3. **Bounded relaying.** Hop cap, per-sender/per-recipient queue bounds, TTL and byte quota on custody; overload drops newest with accounting and signals pressure toward the sender. +4. **Loss is declared, never silent.** Dropped live frames, expired custody, and refused routes are counted in delivery reports; expiry is a retention outcome, not a validity change. +5. **Receipts prove custody, nothing more.** No consumer treats a delivery receipt as message validity, read confirmation, or dispute-grade attestation. +6. **Mode downgrade is detectable.** The negotiated relay mode is bound into the session; an unexpected dumb-pipe is a policy event. +7. **Stateless forwarding, sharded custody.** No session stickiness on the forward path; custody shards by recipient hash with explicit handoffs. +8. **Relay-visible metadata is minimal and fixed.** Routing fields only (§5); anything further is E2E opaque, and trust-model substance comes from #5795. + +## Implementation slicing (follow-on issues, not this design) + +1. Relay handshake + mode negotiation + relay control frames (quota/rate-limit, refusal codes). +2. ID-aware forwarding path with membership-before-forward and hop records. +3. Home-relay publish/lookup and multi-relay race with message-ID dedupe. +4. Opt-in store-and-forward custody with TTL/quota, receipts, and delivery reports. +5. Backpressure soak conformance + observability counters. +6. Dumb-pipe fallback binding (transport-only, endpoints still fully validate). + +No splits are filed by this design beyond the slicing above; if a slice grows a piece, it files then, per the parent programme's rule. + +## Evidence map + +- **Request and scope:** #5813 body (chosen shape, roles, guarantees, backpressure, scaling); #5674 §§4–5 (adapters, relay-as-standard, backpressure/scaling/privacy requirements). +- **Consumed decisions:** #2036 record (envelopes, membership-before-forward, hops, privacy, no-broker, retention policy); #5795 issue (trust contract direction, consumed not duplicated); #5808/#5809/#5810 issues (schema/version/framing direction, aligned by reference). +- **Codebase seams this design builds on:** `pkgs/id/src/chain.rs` (signed envelope/ID), `pkgs/id/src/note_links.rs` (reference validity pattern for before-forward checks), `pkgs/id/src/connectivity.rs` (transport relay vs application relay distinction), `pkgs/id/src/world/mod.rs` (`WorldLimits` pattern for runtime-configurable bounds), `pkgs/id/src/frames.rs` (1 MiB frame cap).