Repository navigation
feat(sync-relay): add self-hosted sync relay server - #346
Merged
Merged
Conversation
…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.
…cate pushes, compaction and gaps
…rom held position, persist lost marks first
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.
… error with exit 77
…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)
This was referenced Dec 3, 2026
7B48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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), plusmembers.json,index.jsonandmeta.json. A change is stored once, whatever the number of members.Each member 10BC8 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 belowheldis believed and lowers it.delivered: the highest seq the relay served and the member acked. Serving starts there, so nothing is sent twice.held <= deliveredalways.index.jsonkeeps per origin thecoveredranges (declared by the pushing author, including ranges with no envelope) and thelostranges (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
deliveredof active members. A dormant member's range is marked lost. Lost marks are written toindex.jsonbefore 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)VectorExchangethe relay sendsRelayGapwhen the member has lost ranges:u32 nthenntimes (16 byte origin, i64 from, i64 to), ranges(from, to]. Tags 11 and 12 stay retired.VectorExchange, keeps the ranges per origin inSessionStats.relayLost, and completes normally. A second or earlyRelayGapis 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.mdhas the full reasoning.Transport and server
--static-key-file(mode0600, refuses to start otherwise) orWHPH_RELAY_STATIC_KEY. Token file with one 64-character hex token per group, constant-time compare.Verification
dart analyzeclean inwhph_sync_coreandwhph_sync_relay.dart testinwhph_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, andmeta.jsonand last-seen refresh.whph_sync_core:test/sessionandtest/protocol181/181, including a session that receivesRelayGapand completes with the ranges recorded (fails on the previous code).sim-soak) and the Docker image, which belongs to a separate change.