8000 feat(sync-relay): add self-hosted sync relay server by ahmet-cetinkaya · Pull Request #346 · ahmet-cetinkaya/whph · GitHub
[go: up one dir, main page]

Skip to content

feat(sync-relay): add self-hosted sync relay server - #346

Merged
ahmet-cetinkaya merged 17 commits into
mainfrom
feat/sync-relay-server
Dec 3, 2026
Merged

ahmet-cetinkaya merged 17 commits into
mainfrom
feat/sync-relay-server

Conversation

@ahmet-cetinkaya
@ahmet-cetinkaya ahmet-cetinkaya commented Dec 1, 2026 •
Copy link
Copy Markdown
Owner

Summary

Adds packages/whph_sync_relay, an optional self-hosted store-and-forward relay for devices of one sync group that are never online at the same time. Sync stays LAN-first and works without it. The relay stores and forwards encrypted, signed envelopes it cannot read or forge.

How the relay stores data

  • One append-only log per sync group and origin (log/<origin>/<seq>.env), plus members.json, index.json and meta.json. A change is stored once, whatever the number of members.

  • Each member 8000 has two positions per origin:

    • held: the highest seq the member provably holds without a gap. It moves by ack, or by the member's own vector claim (a LAN session may have filled a gap). A claim below held is believed and lowers it.
    • delivered: the highest seq the relay served and the member acked. Serving starts there, so nothing is sent twice. held <= delivered always.
  • index.json keeps per origin the covered ranges (declared by the pushing author, including ranges with no envelope) and the lost ranges (removed while a member still lacked them). A declared range counts only if it starts at or below the origin's current top, and it is cut at the highest pushed seq.

  • Compaction deletes seqs at or below the minimum delivered of active members. A dormant member's range is marked lost. Lost marks are written to index.json before files are deleted, and startup repairs a file with a lost mark.

  • When an author re-pushes a lost seq, members that lack it get it again. A per-member generation stops a stale session ack from undoing that.

  • Caps: log bytes (oldest first), members per group, envelopes and bytes per session, a session deadline, and a handshake deadline. While the log is near its cap the relay advertises its top vector and asks for no re-push, so a full relay cannot loop.

  • Pushed manifests are kept when they cover an envelope pushed in the same batch. Before, a manifest that reached beyond the batch's declared ranges was dropped, and the relay later served the envelopes without it; the receiver rejected them while the ranges still moved its cursor.

  • The relay acknowledges every pushed batch after it stored it, so a client knows what the relay holds, and ends its stream with one empty batch (end marker), sent once.

  • The relay stores the origin registrations members push and sends them before its vector, so a member that only reaches the group through the relay can verify another member's new origin. The relay does not verify them; the member does. Only exact duplicates (origin, member key, HLC, signature) are merged, so a bad row cannot hide the real one. At most 4096 rows per group (drops are logged) and 1024 per session (more closes the session).

  • What a member is served is fixed at the vector exchange, so an envelope it relays later in the same session is not served back to it.

  • An unreadable key or token file ends in a one-line error and exit 77 instead of a stack trace.

Gap reporting (RelayGap, message tag 13)

  • After its VectorExchange the relay sends RelayGap when the member has lost ranges: u32 n then n times (16 byte origin, i64 from, i64 to), ranges (from, to]. Tags 11 and 12 stay retired.
  • The core session accepts it once after the peer's VectorExchange, keeps the ranges per origin in SessionStats.relayLost, and completes normally. A second or early RelayGap is a protocol error. The app shows the status in a later change.

Trust model

Protected: an outsider without the token, and the relay operator (envelopes stay encrypted and signed; the operator can only drop data, which the spec states as a known residual risk). Trusted: a paired member. Members are the user's own devices that hold the group token, and the core already trusts them as direct peers. A hostile member could pre-claim coverage for another origin's future seqs and so suppress other devices' data at the relay. The remedy is to revoke that member and rotate the token (a new token is a new group directory). An owner pin on origins was considered and rejected: the relay does not know the group id, pins would be trust-on-first-use, and relayed origins and group switches break it. DESIGN.md has the full reasoning.

Transport and server

  • Own RFC 6455 server framing with a linear reassembly buffer, rejection of non-minimal length encodings, a serialized write chain, a close grace period, a handshake deadline held until admission, and IPv6 pacing by /64 prefix.
  • Static key from --static-key-file (mode 0600, refuses to start otherwise) or WHPH_RELAY_STATIC_KEY. Token file with one 64-character hex token per group, constant-time compare.
  • Logs carry reason categories and counts only, never tokens, keys or envelope content.

Verification

  • dart analyze clean in whph_sync_core and whph_sync_relay.
  • dart test in whph_sync_relay: 85/85, including a manifest that spans batches, batch acks with a single end marker, registration pass-through with restart, exact-duplicate dedupe and the session cap, the fixed snapshot, and an unreadable key file; each of these fails on the code before it. Also including an end-to-end run of three sessions at a full cap with a lagging member (no repeated re-push), refill after a re-push, 40 dropped clients leaving no open sockets, non-minimal length frames, declared ranges above the top, and meta.json and last-seen refresh.
  • whph_sync_core: test/session and test/protocol 181/181, including a session that receives RelayGap and completes with the ranges recorded (fails on the previous code).
  • Not run: the sim-drift soak (sim-soak) and the Docker image, which belongs to a separate change.
  • Several new unit tests use APIs that do not exist in earlier commits of this branch, so they cannot compile there; the end-to-end and session tests were checked against the earlier code and the changed ones fail on it.

…nput

Store each pushed change and its manifest for every other member of the
group, never back to the pusher or the author. Decode the client Hello
without the trailing token. Release connection slots only for admitted
requests. Parse WebSocket frames in the relay so an oversized message is
refused from its header, bound the inbox, session time and ingest volume.
Persist member pins, write files atomically, split served batches by size,
create the state directory owner-only, validate member ids, hash group
directory names, sweep every group, and log refusal categories.
A member the relay reports a gap for used to fail every session on the
unexpected message. The session now stores the lost ranges per origin in
SessionStats.relayLost and completes; the app shows them in a later change.
…ng it

The relay kept a pushed manifest only when it fit inside one declared range of
the same batch. Authors split batches by size and count and sign one manifest
over a longer run, so the relay served those envelopes without a manifest; the
receiver rejected them while the ranges still moved its cursor. A manifest is
now kept when it covers an envelope pushed in the same batch.
…gin registrations, serve a fixed snapshot

The client side of the relay leg needs a complete contract:
- every pushed batch is acknowledged after it is stored
- the relay ends its stream with one empty batch, sent once
- origin registrations members push are stored (deduplicated, capped) and sent
  before the relay's vector, so a relay-only member can verify another
  member's new origin
- the envelopes a member is served are fixed at the vector exchange, so an
  envelope it relays afterwards is not echoed back
… a session offers

- only an exact duplicate (origin, member key, hlc, signature) is ignored, so a
  bad row cannot hide the real registration of the same origin
- a session may offer at most as many registrations as a group has origins
- rows dropped at the group cap are logged
- test that an envelope a member relays after the vector exchange is not
  served back to it (fails without the snapshot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0