fix(sync): keep each account's encrypted activity within 1 MiB

Merged
D3
4d ago
+107-32

Problem

sync_event took about 60% of the public instance's database (~915 MiB). Clients pad every activity event to one 64 KiB block, so each record of a few hundred bytes is stored as 87,610 bytes, and an account keeps up to 100 of them for 30 days. Activity is a broadcast that every device reads with its own cursor, so nothing deletes it on delivery.

Change

  • sync_event::append inserts a broadcast, then keeps the account's newest broadcasts, ordered by created_at, while they fit in 1 MiB and number at most 100. Released clients keep about their 11 newest padded events; clients that pad events in 1 KiB blocks (OpenTubeX/OpenTubeX#3740) keep all 100 in about 160 KB.
  • Sizes come from octet_length(payload), which reads the length from the record header instead of loading the payload from its overflow pages, so this adds no payload reads to the write transaction. SQLite gained that optimization in 3.43, so the server now refuses to start with an older SQLite and says why.
  • Device requests are only counted, as before. Their per-recipient trim could never delete anything, because the account-wide limit of 100 pending requests already applies, so it is gone.
  • README and privacy policy describe the new limit and the smaller event padding.

The server can't read payloads, so end-to-end encryption is unchanged, and released clients keep working. On the public instance, this alone shrinks today's 11,570 events to 3,197 (about −72% of the table). Once clients pad in 1 KiB blocks, the table would hold about 18 MiB for today's event count. Freed pages are reused; the file itself only shrinks after a VACUUM.

Proof: extended events_are_atomic_bounded_expiring_and_scoped_to_account_and_recipient (padded events are trimmed to the newest 11, small ones all kept, a delayed older upload does not evict newer events — this assertion fails when the trim treats the incoming event as newest); writes_and_polls_do_not_read_stored_event_payloads fails with length() and passes with octet_length(); the startup check exits with its error when the probed function is missing. cargo test --workspace, clippy (SQLite and Postgres) and rustfmt pass.

Reviewed with sol-loop: 3 rounds with gpt-6.2-sol on high.
Sol verdict: approved

Made with Claude Opus 5.5 in Claude Code, running in T3 Code.