M4: session cache, explored bitmap and lock-free tile gate #65

Closed
opened 2026-11-20 22:31:44 +01:00 by SirStone · 2 comments
Owner

Part of #63

Goal

Tile requests stop taking the global lock and stop hashing/selecting in SQLite; sessions come from an in-memory table, explored from a bitmap snapshot.

Acceptance criteria

  • Tile gate reads cache + bit snapshot with no dbLock
  • Revocation/replace/remove/restart reflected immediately (auth, restart, logs groups unchanged)
  • Explored wire byte-identical
  • A tile burst answers while a mutation holds the lock
  • A fault that keeps a revoked session fails the auth group
  • Standing gate: full devbox run test green on a clean checkout, no test weakened or skipped, and the behaviour above proven at the seam(s) listed under "Proven at".

User stories covered

  • 67 — As a player, I want tile requests answered without waiting for the game's write lock, so that my map does not freeze while the GM edits walls.

Proven at

server auth/session, explored, tile gate; Nim unit + HTTP suites

Notes

  • Size: M (about one agent session, ~100K tokens). Lane: S (server, serial, after T1).
Part of #63 ## Goal Tile requests stop taking the global lock and stop hashing/selecting in SQLite; sessions come from an in-memory table, explored from a bitmap snapshot. ## Acceptance criteria - [ ] Tile gate reads cache + bit snapshot with no `dbLock` - [ ] Revocation/replace/remove/restart reflected immediately (auth, restart, logs groups unchanged) - [ ] Explored wire byte-identical - [ ] A tile burst answers while a mutation holds the lock - [ ] A fault that keeps a revoked session fails the auth group - [ ] Standing gate: full `devbox run test` green on a clean checkout, no test weakened or skipped, and the behaviour above proven at the seam(s) listed under "Proven at". ## User stories covered - 67 — As a player, I want tile requests answered without waiting for the game's write lock, so that my map does not freeze while the GM edits walls. ## Proven at server auth/session, explored, tile gate; Nim unit + HTTP suites ## Notes - Size: M (about one agent session, ~100K tokens). Lane: S (server, serial, after T1).
SirStone added the ready-for-agentenhancement labels 2026-11-20 22:31:44 +01:00
SirStone added a new dependency 2026-11-20 22:31:45 +01:00
SirStone added a new dependency 2026-11-20 22:31:45 +01:00
SirStone added a new dependency 2026-11-20 22:31:45 +01:00
SirStone added a new dependency 2026-11-20 22:31:46 +01:00
Author
Owner

Implemented in 2727617 (rebased on 3c0258e; full devbox run test green on the rebased tree: 157 node tests + Nim suite, evidence 19/19).

What changed

  • server/sessions.nim (new): the tile gate's published read-side view — token hash → (playerId, isGm) loaded from session/player at startup, plus one explored BitMap per player. All reads are scalar/borrowed (TileScene, openArray[byte]), no ORC refcount traffic across workers.
  • main.nim: tileGate is one snapshot read (gateTile: parse → session → bit → redirect), no dbLock, no SQL, one SHA-256. refreshPlayer publishes the cells a refresh added before the ack. POST /join adds; key.replace and player.remove drop in the same dbLock as the write; startup reloads sessions + bitmaps.
  • explored.nim: BitMap/bitMapOf/setBit/bitSet; tiles.nim: TileScene + tileCoveredByBits. Explored wire (views) and chunk persistence untouched.

AC evidence

  • Gate off dbLock: tools/tiles.test.mjs static check (no app.scene/dbLock in tileGate, exactly one gateTile) + tile gate: a burst is answered while a mutation holds the global lock (LEAK_FAULT=lockhold sleeps 1.2 s inside token.move under the lock; 32 concurrent tiles answer in <700 ms).
  • Revocation/replace/remove/restart: tile gate: revocation is immediate and sessions survive a restart (old cookie 403 at the gate and 401 at upgrade on the next request; new key allowed; restart keeps live sessions, does not resurrect revoked ones); tests/tsessions.nim pins drop/restore and the bitmap lifecycle.
  • Fault: fault sessioncache: a revoked session kept alive fails the authentication group (LEAK_FAULT=sessioncache skips the cache drop → old cookie upgrades 101 instead of 401; the clean key.replace group assertion is the other half).
  • Mutation: mutation.sh c5 (drop the cache delete) fails tsessions "a revocation drops every cached session" — verified against a mutated copy.
  • Explored wire byte-identical: views untouched; leak/doors/oneway/markers/restart/persistence groups unchanged and green.

Bench (devbox run bench, in-process, release): tile gate old (parse + SHA-256 + session JOIN + HashSet) 0.006 ms/request → new (parse + SHA-256 + cache lookup + bitmap) 0.001 ms; incremental explored mark (16 cells) 0.001 ms; full snapshot build 2.0 ms at 262 144 cells (startup/restore only). The old 0.006 ms is an in-memory SQLite lower bound and still paid dbLock; the gate's win is structural, not just these numbers.

Results scratch files were reverted; no test was weakened or skipped (the #31 static test was strengthened for the new architecture).

Implemented in `2727617` (rebased on `3c0258e`; full `devbox run test` green on the rebased tree: 157 node tests + Nim suite, evidence 19/19). **What changed** - `server/sessions.nim` (new): the tile gate's published read-side view — token hash → `(playerId, isGm)` loaded from `session`/`player` at startup, plus one explored `BitMap` per player. All reads are scalar/borrowed (`TileScene`, `openArray[byte]`), no ORC refcount traffic across workers. - `main.nim`: `tileGate` is one snapshot read (`gateTile`: parse → session → bit → redirect), no `dbLock`, no SQL, one SHA-256. `refreshPlayer` publishes the cells a refresh added before the ack. `POST /join` adds; `key.replace` and `player.remove` drop in the same `dbLock` as the write; startup reloads sessions + bitmaps. - `explored.nim`: `BitMap`/`bitMapOf`/`setBit`/`bitSet`; `tiles.nim`: `TileScene` + `tileCoveredByBits`. Explored wire (views) and chunk persistence untouched. **AC evidence** - Gate off `dbLock`: `tools/tiles.test.mjs` static check (no `app.scene`/`dbLock` in `tileGate`, exactly one `gateTile`) + `tile gate: a burst is answered while a mutation holds the global lock` (`LEAK_FAULT=lockhold` sleeps 1.2 s inside `token.move` under the lock; 32 concurrent tiles answer in <700 ms). - Revocation/replace/remove/restart: `tile gate: revocation is immediate and sessions survive a restart` (old cookie 403 at the gate and 401 at upgrade on the next request; new key allowed; restart keeps live sessions, does not resurrect revoked ones); `tests/tsessions.nim` pins drop/restore and the bitmap lifecycle. - Fault: `fault sessioncache: a revoked session kept alive fails the authentication group` (`LEAK_FAULT=sessioncache` skips the cache drop → old cookie upgrades 101 instead of 401; the clean `key.replace` group assertion is the other half). - Mutation: `mutation.sh c5` (drop the cache delete) fails `tsessions` "a revocation drops every cached session" — verified against a mutated copy. - Explored wire byte-identical: views untouched; leak/doors/oneway/markers/restart/persistence groups unchanged and green. **Bench** (`devbox run bench`, in-process, release): tile gate old (parse + SHA-256 + `session JOIN` + HashSet) 0.006 ms/request → new (parse + SHA-256 + cache lookup + bitmap) 0.001 ms; incremental explored mark (16 cells) 0.001 ms; full snapshot build 2.0 ms at 262 144 cells (startup/restore only). The old 0.006 ms is an in-memory SQLite lower bound and still paid `dbLock`; the gate's win is structural, not just these numbers. Results scratch files were reverted; no test was weakened or skipped (the #31 static test was strengthened for the new architecture).
Author
Owner

Review: no race (TSAN/ASAN stress clean, negative control races); gate uses a private mutex, dbLock-free; DESIGN §4.3 corrected in e254a0e. GateCache.setScene has no production caller — kept for scene.set when tile fields become patchable.

Review: no race (TSAN/ASAN stress clean, negative control races); gate uses a private mutex, dbLock-free; DESIGN §4.3 corrected in e254a0e. GateCache.setScene has no production caller — kept for scene.set when tile fields become patchable.
Sign in to join this conversation.