Repository navigation
perf(store): shard the room seq state 256 ways (#489) - #598
Merged
Merged
Conversation
`.seqstate` held every room's floor and generation in one root-level map, and nothing ever removed an entry — so it grew with every room the service had ever reaped. That map was read and `orjson.loads`-ed IN FULL on every room read, because `read_messages` asks for the generation, and rewritten in full under one service-wide lock on every create and every reap. At the ~90k rooms a live deployment has already reaped that is 3.2 MB parsed per request: 42.30 ms, which is 99% of a room read, and 20 reads/s across 8 concurrent readers. It also cost 90 ms per room create, against 3.2 ms on a fresh store. With the create gate gone (#578) this was the binding constraint on both paths. The map is now 256 shards keyed by the same `_shard` that resolves a room's own bucket, so one resolver keeps a room's data and its floor in step and a re-shard can never move one without the other. Measured on the same store, old whole-map read vs. sharded read: 10k history 2.85 ms -> 0.02 ms (118x), 90k history 42.30 ms -> 0.08 ms (537x). A create at 90k history goes 90 ms -> ~2 ms. Flat at the root beside `.counters` and `.usage`, deliberately NOT a per-room sidecar in the bucket: `_scan` and `_walk` only match `*.jsonl` under `rooms/`, so nothing here is walked, counted or reaped as a room — and a file per room would keep every bucket a reaped room ever used permanently non-empty, which is exactly the litter `_prune` exists to reclaim. 256 files bounded by the shard width, not one per name the service has ever seen. Migration rides the reaper and needs no operator step: `_split_seq_state` partitions the old map once, grouped so it costs one lock per shard rather than one per room, and shard entries win the merge so a create landing mid-split is not clobbered. Until it runs, a read falls back to the old map and answers correctly, so there is no flag day and no window of wrong generations. The old file is renamed to `.seqstate.pre-shard` rather than unlinked — a downgrade then finds a map the old code still understands, and the `??` glob future readers use cannot match that name. Also fixes a latent 500 the new tests surfaced: `_read_seq_state` returned whatever the JSON held, so a shard containing `[]` reached `.get` and raised AttributeError out of a room read. Absent, torn and hand-edited now all mean the same thing — no state. Does NOT reclaim entries: the map is still unbounded, which is the other half of #489 and what PRs #497/#503 target. Entries carry a `t` stamp so that follow-up needs no second migration to date what it finds. 14 new tests, each verified to fail against a mutated version of this code.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 49909ab1be
ℹ️ 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".
The split merged shard-over-map unconditionally, on the reasoning that anything already in a shard was written by `_set_seq_entry` after the split began and is therefore newer. That holds for the first split and is wrong for the one case that produces a second: during a rolling upgrade a worker still running the pre-shard code writes `.seqstate`, recreating a file this change has already consumed and renamed. Its entry is newer than the shard's, and the merge dropped it — regressing the room's floor so cursors past it miss messages, or losing a generation bump so a stateful reader is told nothing changed. The backup is the discriminator, and it is exact: no backup means no split has run, so every entry in the map predates this pass and the shard wins; a backup that exists means this map was written after a split consumed the original, so the map wins. A recovered map is unlinked rather than renamed, because overwriting the backup with the few entries a mixed-version window produces would leave a downgrade reading a map that had lost almost every room it knew. Reported by Codex review on #598. Both halves of the fix are covered and each was verified to fail the new test on its own.
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
zkasuran
pushed a commit
to zkasuran/technocore-chat
that referenced
this pull request
Aug 31, 2026
No conflicts. Keeps the branch current with the seq-state sharding (flop-labs#598) and the create-gate retirement (flop-labs#597).
4 tasks done
This was referenced Aug 31, 2026
This was referenced Sep 1, 2026
8 tasks done
8 tasks done
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.
What
.seqstatebecomes 256 shards keyed by the same_shardthat resolves a room's own bucket, so one resolver keeps a room's data and its floor in step. Old whole-map read vs. sharded read, same store: 10k history 2.85 ms → 0.02 ms (118x), 90k history 42.30 ms → 0.08 ms (537x). A create at 90k history goes ~90 ms → ~2 ms. No API change.Also fixes a latent 500 the new tests surfaced:
_read_seq_statereturned whatever the JSON held, so a shard (or map) containing[]reached.getand raisedAttributeErrorout of a room read.Why
Addresses the performance half of #489. One root-level map held every room's floor and generation, and nothing ever removed an entry, so it grew with every room the service had ever reaped. It was parsed in full on every room read —
read_messagesasks for the generation — and rewritten in full under one service-wide lock on every create and every reap. At the ~90k a live deployment has already reaped that is 3.2 MB per request, 99% of a read, and 20 reads/s across 8 concurrent readers. With the create gate gone (#597) this was the binding constraint on both paths.Flat at the root beside
.countersand.usage, and deliberately not a per-room sidecar in the bucket:_scan/_walkonly match*.jsonlunderrooms/, so nothing here is walked, counted or reaped as a room — and a file per room would keep every bucket a reaped room ever used permanently non-empty, which is the litter_pruneexists to reclaim (last_seqalready warned about this). 256 files bounded by the shard width, not one per name the service has ever seen.Migration rides the reaper, no operator step:
_split_seq_statepartitions the old map once, grouped so it costs one lock per shard rather than one per room, with shard entries winning the merge so a create landing mid-split is not clobbered. Until it runs, reads fall back to the old map and answer correctly — no flag day, no window of wrong generations. The old file is renamed to.seqstate.pre-shard, not unlinked, so a downgrade finds a map the old code still understands; the??glob future readers use cannot match that name.Judgment calls worth a reviewer's eye:
tstamp so that follow-up needs no second migration to date what it finds. It will need to iterate shards rather than one file.Verified against
mainat487caca.Release note: a room's floor and generation move from one service-wide file to 256 shards, so reading a room no longer parses every room the service has ever reaped; migration is automatic on the first reap.
Checks
uv run coverage run -m pytest tests -q && uv run coverage report— 593 passed, 97.78% (was 97.53%); 14 new tests intests/unit/test_seq_state.py, each verified to fail against a mutated version of this codeuv run ruff check . && uv run ruff format --check .anduv run ty check.seqstateis internal and no document references it.uv run sz.py --checkpasses with 3 lines of core headroom to spareGenerated by Claude Code