Files
buzz/test-fixtures/read-state-vectors.json
Will Pfleger 890824f072 feat(read-state): sync mark read, channel mark unread and thread follows (#9501)
Reading, mark read, whole-channel mark unread and thread follow choices
now sync across a person's installations. This PR specifies the rules in
NIP-RS and implements them in the mobile app.

## Spec (`docs/nips/NIP-RS.md`)

- Defines *catch-up* and *displayed*.
- A catch-up advances the channel's own frontier (top-level messages) or
`thread:<root>`. `activity:` and `thread-activity:` become read-only
legacy keys.
- Explicit mark read is synchronized state. A first explicit read of a
message with no override group writes only its `msg:` frontier. Display,
dwell, scroll, catch-up and mention visibility create no `msg:` entries.
- Only whole-channel override groups are published, merged and cleared
with the counter. Readers ignore peer `msg:` and `thread:` groups.
- Single-message and thread mark unread stay on the device. They end
once the merged frontier covering them passes their baseline, from any
installation, or on display, catch-up or explicit read. The section also
states clearing scope, the accepted concurrency limitation and capacity.
- When the merged override union no longer fits the primary, the client
rewrites the primary compactly from the groups its last published
primary carried, each canonical against current frontiers. It never
omits merged groups or tombstone floors that primary carried, and
override actions fail visibly.
- Adds Buzz Thread Follow Choices: `thread-follows:<32hex>` coordinates,
validation, three-state verdict, last-writer merge with ties going to
unfollow, publication and recovery, legacy import at `at = 0`, and
capacity.

## Mobile

- Catch-up writes the main frontier. A flat timeline reads up to the
newest top-level message it reached.
- Explicit mark read publishes. Passive `msg:` marks stay on the device.
- Whole-channel mark unread publishes an override group and merges peer
groups. An explicit read or catch-up clears it, even when no frontier
moves. The action fails visibly at the uint32 limit or when the primary
is full. A synced clear the primary can't carry is rolled back, and the
channel view reports it once while the failure stands.
- Device-local message and Inbox unread overrides record a baseline and
clear when a covering frontier passes it, including one merged from a
peer.
- Thread follows move from local storage to the synced `thread-follows:`
slots. They share the read-state `client_id`, journal, `created_at`
sequence, snapshot and live subscription, and spill across up to 16
slots with stable placement. An unreadable coordinate marks follow state
incomplete and blocks this installation's follow publication.
- Follow and unfollow are refused on every cold start of the writer
until its first complete recovery of its own state succeeds and every
owned follow head is readable. Cached follows show at once. A refused
tap changes nothing and shows "Follow choices are not ready. Connect and
try again." Reads, catch-up and both kinds of mark unread are not gated.
- A follow tap is accepted only once journaled. The accepted own head,
pending edits and the exact in-flight packet are kept apart, so a retry
or restore never drops or re-attributes an edit.
- Pre-sync local follows are imported at `at = 0` after a complete
merge, once their channel is known from a relay event. An imported key
is settled, and its legacy copy retired, only when the journal write the
import awaited covered it and that entry is still current.
- An explicit unfollow suppresses ordinary replies at live and catch-up
admission. A change in follow choices drops the unread replies already
counted for an unfollowed thread. A newly followed thread re-runs
catch-up only for its own channel.
- A reading clear refused because the view hasn't completely loaded is
retried after the next complete snapshot, including when publishing was
shut off at the time. The view also counts as not completely loaded
while the live subscription is reconnecting, so a read made while the
phone is offline can't erase a newer unread mark set on another device.
Offline, whole-channel mark unread is still refused, with a message.
- Read-state events, including live ones, are verified with native
libsecp256k1 before they are used.
- A pending unread packet is counted as published only when no newer own
head outranks it. When a newer head supersedes it, a successor is built
above that head from the saved intent, and a late echo of a replaced
packet doesn't overwrite what was published.
- When the merged unread state overfills the primary and the compact
fallback can't carry a pending mark, the packet stays pending and the
"read state full" error shows. Other queued reads and follows in the
same flush still publish.
- When this installation's own unread record can't be decoded, only the
unread step is held: that record isn't overwritten, its pending marks
stay queued and the sync error shows. Other queued reads and follows in
the same flush still publish.
- The sync notice stays up after an unsupported refusal until a new
session. Offline, it can't be tapped and says it will retry when back
online.
- Two-device fixtures against an in-memory relay cover channel
round-trips and clears, stray message groups, the first explicit message
read, the primary cap, follow propagation, same-second and
backward-clock follow publication, legacy import, retry after a failed
publish, unreadable coordinates and slot packing. Recovery fixtures
cover the startup gate, journal failures during flush, interleaved
legacy imports, and restore of an older journal.

## Relay

- Conforming `thread-follows:<32 hex>` records get the NIP-RS treatment:
a replaced record is purged, and a stale resend of an older one is
refused. Records with a mismatched `t` tag keep ordinary
replaceable-event behavior.

## Known gap

- An unread packet held for capacity can age past the relay's timestamp
window. The relay then refuses it, and the refusal path retires it
without the capacity check, so the error clears and other installations
don't get that mark. The mark stays on the device.

Related:
[block/buzz-app#3552](https://github.com/block/buzz-app/pull/3552)

---------

Signed-off-by: Will Pfleger <wpfleger@block.xyz>
Signed-off-by: Will Pfleger <wpfleger@squareup.com>
Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
2026-11-19 17:02:58 -05:00

640 lines
16 KiB
JSON

{
"schemaVersion": 1,
"now": 1800000000,
"retentionSeconds": 2592000,
"floor": 1797408000,
"notes": [
"Evaluation uses messageAt > max(floor, merged key and named parent frontiers).",
"Packing inputs are already retention-filtered, locally-owned frontier maps; override groups never enter read-position slots; they live on the version-2 unread record. Clients write msg: frontier keys only for an explicit per-message Mark read; msg: and thread: override groups stay device-local and never sync; legacy msg: keys remain readable.",
"Initial allocation packs keys in ascending order into empty slots. Subsequent edits preserve persisted key-to-slot placement; keys are sorted only within each slot. Envelope uses compact JSON and raw UTF-8.",
"Overrides remain outside the age floor; full-state snapshot retrieval must not use since.",
"Lifecycle traces are obligations for suite-local persistence/relay tests, not a substitute implementation."
],
"evaluation": [
{
"id": "missing-at-floor",
"states": [],
"key": "room",
"parents": [],
"messageAt": 1797408000,
"expectedEffective": 1797408000,
"expectedUnread": false
},
{
"id": "missing-one-second-newer",
"states": [],
"key": "room",
"parents": [],
"messageAt": 1797408001,
"expectedEffective": 1797408000,
"expectedUnread": true
},
{
"id": "old-frontier-below-floor",
"states": [
{
"room": 1797407999
}
],
"key": "room",
"parents": [],
"messageAt": 1797408000,
"expectedEffective": 1797408000,
"expectedUnread": false
},
{
"id": "merged-max-across-slots",
"states": [
{
"room": 1797408020
},
{
"room": 1797408010
},
{
"room": 1797408020
}
],
"key": "room",
"parents": [],
"messageAt": 1797408021,
"expectedEffective": 1797408020,
"expectedUnread": true
},
{
"id": "channel-covers-thread",
"states": [
{
"room": 1797408030
},
{
"thread:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1797408010
}
],
"key": "thread:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"parents": [
"room"
],
"messageAt": 1797408030,
"expectedEffective": 1797408030,
"expectedUnread": false
},
{
"id": "legacy-msg-still-read",
"states": [
{
"msg:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 1797408050
}
],
"key": "msg:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"parents": [
"room",
"thread:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
],
"messageAt": 1797408050,
"expectedEffective": 1797408050,
"expectedUnread": false
},
{
"id": "old-carrier-high-frontier",
"carrierCreatedAt": 1797407999,
"states": [
{
"room": 1800000050
}
],
"key": "room",
"parents": [],
"messageAt": 1800000040,
"expectedEffective": 1800000050,
"expectedUnread": false
}
],
"packing": [
{
"id": "utf8-and-escaped-json",
"clientId": "fixture",
"maxPlaintextBytes": 110,
"frontiers": {
"room-a": 1797408000,
"room-b": 1797408001,
"room-c": 1797408002,
"quote\"slash\\": 1797408003,
"雪": 1797408004,
"é": 1797408005
},
"expectedBlobKeys": [
[
"quote\"slash\\",
"room-a",
"room-b"
],
[
"room-c",
"é",
"雪"
]
],
"expectedPlaintextBytes": [
110,
95
]
},
{
"id": "more-than-twelve-blobs",
"clientId": "fixture",
"maxPlaintextBytes": 40960,
"expectedPlaintextBytes": [
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
40927,
22652
],
"generate": {
"count": 7000,
"keyPrefix": "thread:",
"indexFormat": "lowercase hex padded to 64 characters",
"valueStart": 1797408001,
"valueStep": 1
},
"expectedBlobRanges": [
{
"firstIndex": 0,
"lastIndex": 480,
"count": 481
},
{
"firstIndex": 481,
"lastIndex": 961,
"count": 481
},
{
"firstIndex": 962,
"lastIndex": 1442,
"count": 481
},
{
"firstIndex": 1443,
"lastIndex": 1923,
"count": 481
},
{
"firstIndex": 1924,
"lastIndex": 2404,
"count": 481
},
{
"firstIndex": 2405,
"lastIndex": 2885,
"count": 481
},
{
"firstIndex": 2886,
"lastIndex": 3366,
"count": 481
},
{
"firstIndex": 3367,
"lastIndex": 3847,
"count": 481
},
{
"firstIndex": 3848,
"lastIndex": 4328,
"count": 481
},
{
"firstIndex": 4329,
"lastIndex": 4809,
"count": 481
},
{
"firstIndex": 4810,
"lastIndex": 5290,
"count": 481
},
{
"firstIndex": 5291,
"lastIndex": 5771,
"count": 481
},
{
"firstIndex": 5772,
"lastIndex": 6252,
"count": 481
},
{
"firstIndex": 6253,
"lastIndex": 6733,
"count": 481
},
{
"firstIndex": 6734,
"lastIndex": 6999,
"count": 266
}
]
}
],
"ownership": [
{
"id": "peer-merge-does-not-become-local-write",
"local": {
"room": 1797408005
},
"remote": [
{
"room": 1797408050,
"peer-only": 1797408070
}
],
"expectedMerged": {
"room": 1797408050,
"peer-only": 1797408070
},
"expectedPublished": {
"room": 1797408005
}
},
{
"id": "advance-below-observed-peer-max",
"local": {
"room": 1797408005
},
"remote": [
{
"room": 1797408050
}
],
"localAdvance": {
"room": 1797408010
},
"expectedMerged": {
"room": 1797408050
},
"expectedPublished": {
"room": 1797408010
}
}
],
"pruning": [
{
"id": "floor-boundary-frontiers",
"frontiers": {
"old": 1797407999,
"equal": 1797408000,
"new": 1797408001
},
"expectedRequiredKeys": [
"new"
],
"expectedOptionalInertKeys": [
"equal"
],
"expectedAbsentKeys": [
"old"
]
}
],
"lifecycle": [
{
"id": "unchanged-blob-not-republished",
"steps": [
"publish initial packed state",
"advance one key without changing earlier blob boundaries",
"flush"
],
"expect": [
"publish only changed coordinates",
"keep unchanged blob IDs and content"
]
},
{
"id": "partial-publish-restart",
"steps": [
"persist slot list and signed pending batch",
"accept first coordinate",
"second coordinate fails with unknown outcome",
"restart then retry"
],
"expect": [
"retry unknown signed event unchanged until observed",
"do not acknowledge full local revision while any required blob remains unsynced",
"no frontier lost from persisted local state"
]
},
{
"id": "shrink-and-slot-reuse",
"steps": [
"publish two frontier-only coordinates",
"advance clock so second coordinate contents fall below floor",
"pack fewer blobs",
"later require second blob again"
],
"expect": [
"persist stable primary slot",
"unused old blob cannot lose required frontier on another device",
"reuse owned persisted additional slot or safely retire it; never overwrite foreign slot"
]
},
{
"id": "old-event-new-frontier",
"eventCreatedAt": 1797407999,
"frontiers": {
"room": 1797408100
},
"messageAt": 1797408080,
"expect": [
"if full snapshot delivered event, do not discard its newer frontier merely because event is old",
"floor never prunes override tombstones"
]
}
],
"placement": [
{
"id": "early-key-does-not-move-carriers",
"clientId": "fixture",
"maxPlaintextBytes": 110,
"slots": [
{
"slot": "00000000000000000000000000000000",
"frontiers": {
"room-b": 1797408001,
"room-c": 1797408002
}
},
{
"slot": "11111111111111111111111111111111",
"frontiers": {
"room-z": 1797408003
}
}
],
"advance": {
"room-a": 1797408004
},
"expectedPreservedAssignments": {
"room-b": "00000000000000000000000000000000",
"room-c": "00000000000000000000000000000000",
"room-z": "11111111111111111111111111111111"
},
"expect": [
"persist assignment before publishing any new coordinate",
"failure publishing new key must not erase existing sole frontier carriers",
"do not republish untouched slot merely because new key sorts earlier"
]
}
],
"writePolicy": [
{
"id": "frontier-only-even-for-mentions",
"localObserved": {
"room": 1797408010,
"thread:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1797408020,
"msg:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 1797408030
},
"messageIsDirectMention": true,
"expectedPublished": {
"room": 1797408010,
"thread:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1797408020
},
"expectedAbsentKeys": [
"msg:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
]
}
],
"unreadV2": {
"notes": [
"Version-2 unread record: d=read-state-unread-v2:<32 lowercase hex>, t=read-state-unread-v2, plaintext {\"v\":2,\"client_id\",\"contexts\"}. Each group travels with its channel frontier.",
"Stamps are uint32 Unix seconds. Lifetime 2592000 s; discard guard 600 s; receive defers a group more than 660 s ahead; allocation is refused more than 60 s ahead or past uint32.",
"Clock checks inspect max(S,C), never B or created_at. Version-1 override keys are never imported."
],
"liveness": [
{
"id": "live-at-lifetime-edge",
"register": {
"s": 1797408000,
"c": 0,
"b": 10
},
"frontier": 10,
"expectedActive": true,
"expectedExpired": false,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_s:room": 1797408000,
"ov_c:room": 0,
"ov_b:room": 10
}
},
{
"id": "unread-expires-one-second-past",
"register": {
"s": 1797407999,
"c": 0,
"b": 10
},
"frontier": 10,
"expectedActive": false,
"expectedExpired": true,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_c:room": 1797407999
}
},
{
"id": "zero-baseline-excludes-retention-floor",
"register": {
"s": 1799999990,
"c": 0,
"b": 0
},
"frontier": 0,
"expectedActive": true,
"expectedExpired": false,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_s:room": 1799999990,
"ov_c:room": 0,
"ov_b:room": 0
}
},
{
"id": "read-past-baseline-is-dead",
"register": {
"s": 1799999990,
"c": 0,
"b": 10
},
"frontier": 11,
"expectedActive": false,
"expectedExpired": false,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_c:room": 1799999990
}
},
{
"id": "same-stamp-clear-wins",
"register": {
"s": 1799999990,
"c": 1799999990,
"b": 10
},
"frontier": 10,
"expectedActive": false,
"expectedExpired": false,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_c:room": 1799999990
}
},
{
"id": "tombstone-retained-at-discard-boundary",
"register": {
"s": 0,
"c": 1797407400,
"b": 0
},
"frontier": 0,
"expectedActive": false,
"expectedExpired": true,
"expectedDiscardable": false,
"expectedCanonical": {
"ov_c:room": 1797407400
}
},
{
"id": "tombstone-discardable-one-second-past",
"register": {
"s": 0,
"c": 1797407399,
"b": 0
},
"frontier": 0,
"expectedActive": false,
"expectedExpired": true,
"expectedDiscardable": true,
"expectedCanonical": {
"ov_c:room": 1797407399
}
}
],
"allocation": [
{
"id": "fresh-group-stamps-now",
"register": {
"s": 0,
"c": 0,
"b": 0
},
"now": 1800000000,
"expectedStamp": 1800000000
},
{
"id": "same-second-action-advances",
"register": {
"s": 1800000000,
"c": 0,
"b": 5
},
"now": 1800000000,
"expectedStamp": 1800000001
},
{
"id": "peer-ahead-within-60s",
"register": {
"s": 0,
"c": 1800000059,
"b": 0
},
"now": 1800000000,
"expectedStamp": 1800000060
},
{
"id": "peer-ahead-refused",
"register": {
"s": 0,
"c": 1800000060,
"b": 0
},
"now": 1800000000,
"expectedStamp": null
},
{
"id": "uint32-exhausted-refused",
"register": {
"s": 4294967295,
"c": 0,
"b": 0
},
"now": 4294967295,
"expectedStamp": null
}
],
"receive": [
{
"id": "older-than-lifetime-dropped",
"register": {
"s": 1797407999,
"c": 0,
"b": 10
},
"now": 1800000000,
"expected": "drop"
},
{
"id": "at-lifetime-merged",
"register": {
"s": 0,
"c": 1797408000,
"b": 0
},
"now": 1800000000,
"expected": "merge"
},
{
"id": "660s-ahead-merged",
"register": {
"s": 1800000660,
"c": 0,
"b": 10
},
"now": 1800000000,
"expected": "merge"
},
{
"id": "661s-ahead-deferred",
"register": {
"s": 0,
"c": 1800000661,
"b": 0
},
"now": 1800000000,
"expected": "defer"
},
{
"id": "baseline-not-clock-checked",
"register": {
"s": 1800000000,
"c": 0,
"b": 1800100000
},
"now": 1800000000,
"expected": "merge"
}
]
}
}