Skip to content

perf(read-state): fetch only changed read state on resume - #10075

Draft
brow wants to merge 11 commits into
mainfrom
mobile-read-state-since
Draft

brow wants to merge 11 commits into
mainfrom
mobile-read-state-since

Conversation

@brow

@brow brow commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Every time the mobile app resumes or reconnects, it downloads the account's whole read-state snapshot: one encrypted kind 30078 event per installation that ever synced. On an account with about 30 installations that is around 1.2 MB per resume, and the client downloads, verifies and merges every event before unread counts settle. Usually only one or two of those events changed.

This PR lets a client ask the relay for only what changed:

  • Relay: the NIP-RS snapshot filter accepts an optional received_after cursor. With it, the relay returns only own-author kind 30078 events it received after that point (delta: true, no completeness claim). Every response, full or delta, carries a new cursor. The cursor is based on the relay's receive time minus a 5-minute overlap, so a device's skewed clock can't hide an event, and a write that commits late is still returned as long as its receive time is newer than the cursor. NIP-11 advertises delta: 1, and NIP-RS documents it.
  • Mobile: the sync path (resume, reconnect, live-subscription ready) uses a delta when it has a cursor from the same relay, identity and community, the relay advertises deltas, and its last complete read is under an hour old (the first sync after that hour reads the full snapshot). Everything that needs a complete view still reads the full snapshot: cold start, publish, and dropping coordinates a peer deleted. Deltas are folded into the follow-capacity basis, and a follow refused on a basis that includes a delta first asks the sync loop for one complete read (shared by every tap waiting on it) and re-checks once a complete basis is installed; a newer tap on the same thread during that read keeps its choice. A relay that refuses a delta with HTTP 400 (for example an older instance mid-rollout) gets one complete read in the same call. A failed complete read drops the cursor, including one the transport chose itself, so retries stay complete reads with their normal backoff.
  • Rate limits: snapshot requests now wait out the relay's rate-limit gate before sending, and sign their NIP-98 auth after the wait, so retries and resumes stop spending quota while the relay throttles.

Regressions

  • Older relays reject received_after and don't advertise delta, so mobile keeps doing full reads against them.
  • A coordinate a peer deleted now lingers until the next complete read (publish, cold start, or the first sync after an hour). For read positions that's harmless, since they only move forward. A deleted thread-follow coordinate could also linger (for example a stale follow:false that mutes a thread), but no shipped client deletes thread-follow coordinates, so this only matters for hand-edited state.
  • A delta costs the relay the same database work as a full read: it runs the full snapshot query and its size checks, then drops events at or before the cursor. Only the download shrinks. Cutting the database cost is a follow-up.
  • At the cap, after a delta, a follow tap waits for one complete read (longer during a relay rate-limit pause) before it succeeds or shows the "full" message, where main answered at once.
  • Publish still downloads the full snapshot. Moving publish onto deltas is a larger follow-up.

Validation

Snapshot response size from a local relay built from this branch, for an account with 33 slots of about 36 KB each (random content, so it compresses like real encrypted blobs):

Read Events Bytes
Full snapshot (every resume today) 33 1,201,189
Delta, one slot changed 1 36,593
Delta, nothing changed 0 203

A delta taken within 5 minutes of the previous read also re-sends the slots written in that window (the overlap).

  • read_state_sync_test: the in-memory relay now models deltas, so the existing sync journeys re-sync through one. New journeys check that a delta never drops a peer's coordinate and that the next complete read does, that a refused follow is accepted when a delta lands during its complete read, that refused taps waiting together share one complete read, that a newer tap on the thread survives that read, and that a failed fallback read after a refused delta drops the cursor.
  • read_state_snapshot_transport_test: new rows for delta requests, envelope mismatches, and falling back to full reads.
  • buzz-db Postgres test for delta reads against a real database, including a write received within the cursor's millisecond.
  • Automated app run: two iOS simulators signed in as one test account, against a local relay built from this branch (before the latest follow-capacity and cursor fixes, which only change the at-cap follow and failed-read paths), with a proxy logging each snapshot request. Passed: cold start and relaunch read in full; publish reads in full; a resume after another device read a channel took a delta (1 to 2 events, about 1 KB) and cleared the unread within a second; a reconnect with nothing new returned 0 events (203 bytes); Mark Unread, follow and unfollow synced between the two devices, and an unfollow arrived through a delta; after a relay restart both devices reconnected with deltas. Not covered: physical devices, Android, the one-hour cutoff, and relays without delta support.

Each resume or reconnect re-downloaded the whole own-author kind 30078
read-state snapshot. The relay now accepts an optional received_after
cursor on the NIP-RS snapshot filter and returns only events it received
after that point, with a safety overlap for writes that commit out of
order. Every snapshot response carries a cursor, and NIP-11 advertises
delta support so clients fall back to complete reads on older relays.

Mobile uses a delta only on the sync path, within an hour of its last
complete read and only for a cursor from the same relay origin, identity
and community. Publish, capacity, absence and own-slot decisions keep
using complete snapshots, and a delta never drops peer coordinates.

Signed-off-by: Tom Brow <tomb@block.xyz>
@brow

brow commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 30b09b8cd2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-01T19:26:02.665025Z 68c47ae Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

brow added 5 commits December 1, 2026 10:13
A delta selected and budgeted only the rows received after its cursor,
so on an account over the event, stored-byte or encoded-byte budget a
small delta succeeded where a full read is refused with 413. The client
then treated its state as loaded, accepted Mark Unread, and the full
read before publishing rejected the journaled mark.

A delta now runs the full snapshot read unchanged (selection, budgets,
encoded cap, signature and scope checks) and only then drops the rows
received at or before its cursor, so it fails exactly when a full read
would. Comparing the cursor in Rust also removes the timestamp cast
that turned an out-of-range cursor into a 503.

NIP-RS now says when a delta counts as a new snapshot (reconnect
catch-up and override gating) and what still needs a complete one.

Signed-off-by: Tom Brow <tomb@block.xyz>
During a rolling deploy or rollback, discovery can reach a relay that
offers deltas while the query reaches one that predates them, which
refuses the cursor with 400. Every retry kept the cursor and failed the
same way. A 400 to a delta request now retries once, in the same call,
as a newly signed full read.

Signed-off-by: Tom Brow <tomb@block.xyz>
Only complete reads refreshed the follow capacity basis, so after a
delta resume it went stale both ways: a peer filling the last room left
Follow accepting a choice that could never publish, and a peer freeing
room left it refusing a choice that fit. The refused tap returns before
publication, so no later full read rescued it.

Each delta's events now replace or add their coordinates in the basis,
so it can only overcount a coordinate deleted since the complete read.
Before refusing a follow on a basis that includes a delta, Follow reads
the complete snapshot once and checks again.

Signed-off-by: Tom Brow <tomb@block.xyz>
When complete reads failed but deltas succeeded (a link too slow for
the whole snapshot), each successful delta reset the backoff that
belonged to the failing complete read and scheduled another publish,
whose complete read failed again. Ten minutes gave about 60 failed
complete reads and a sync notice that flickered each time.

A failed complete read now drops the delta cursor, so the retry reads
the complete snapshot too and the backoff grows as before.

Signed-off-by: Tom Brow <tomb@block.xyz>
A 429 on a snapshot read started the rate-limit gate, but no snapshot
request waited on it, so a backoff retry or resume inside the pause
spent quota and drew another 429. Wait on the gate before the discovery
GET and the POST /query, recheck the session context after the wait, and
build and sign the NIP-98 request only after it.

Signed-off-by: Tom Brow <tomb@block.xyz>
@brow

brow commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a7ddc3f5d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

brow added 5 commits December 1, 2026 10:39
A live-ready delta that finished during a refused follow's complete read
outranked it, so the capacity basis stayed stale and the retry refused
again. The refresh now joins the sync loop as a complete read and
rechecks only once a complete basis is installed. A newer tap on the
same thread during the refresh keeps its choice. Every failed read now
drops the cursor, since the transport can read in full on its own (no
delta offered, or the 400 fallback).

Signed-off-by: Tom Brow <tomb@block.xyz>
The delta filter truncated received_at to milliseconds, so a row
received within the cursor's millisecond but after it was dropped.

Signed-off-by: Tom Brow <tomb@block.xyz>
Refused follow taps waiting on a capacity refresh now share the complete
read already in flight instead of queueing another. A tap superseded by
a newer one on the same thread finishes as a success without writing.
The snapshot transport reports when it sends a complete query, so a
failed read drops the cursor whenever a complete read was sent, and a
failed delta keeps it.

Signed-off-by: Tom Brow <tomb@block.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
@brow

brow commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 68c47ae6ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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