Skip to content

perf(store): shard the room seq state 256 ways (#489) - #598

Merged
sv merged 2 commits into
mainfrom
claude/issue-578-room-locking-349ftf
Aug 31, 2026
Merged

sv merged 2 commits into
mainfrom
claude/issue-578-room-locking-349ftf

Conversation

@sv

@sv sv commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

What

.seqstate becomes 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. 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_state returned whatever the JSON held, so a shard (or map) containing [] reached .get and raised AttributeError out 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_messages asks 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 .counters and .usage, and deliberately not a per-room sidecar in the bucket: _scan/_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 the litter _prune exists to reclaim (last_seq already 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_state partitions 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:

Verified against main at 487caca.

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 in tests/unit/test_seq_state.py, each verified to fail against a mutated version of this code
  • uv run ruff check . && uv run ruff format --check . and uv run ty check
  • Docs that would now be wrong are updated: none — .seqstate is internal and no document references it. uv run sz.py --check passes with 3 lines of core headroom to spare
  • New surface on a world-writable service: nothing new — no route, parameter or response field changes; the shards are not reachable or nameable by a caller

Generated by Claude Code

`.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.
@github-actions

Copy link
Copy Markdown

Open pull requests citing the same issues:

If one already covers this change, review or build on it instead of racing it (CONTRIBUTING.md "Overlapping work").

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-08-31T07:50:24.644399Z 49909ab PR opened
ℹ️ 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.

@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: 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".

Comment thread src/store.py Resolved
@sv sv mentioned this pull request Aug 31, 2026
4 tasks done
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.
@sv
sv merged commit 5083351 into main Aug 31, 2026
6 checks passed
@sv
sv deleted the claude/issue-578-room-locking-349ftf branch August 31, 2026 08:01
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).
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.

2 participants