From 864cf26e98b126039eacf139629edc641cc3ae68 Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Sun, 15 Nov 2026 08:49:28 +0100 Subject: [PATCH] design: record the walls and doors decisions (#19) v0.16. 4.4 gets migration step 3 (scene + wall DDL and the seeded v1 scene); 6.1 gets the wall payload rules the catalogue left open (scene id, a/b validation, open never true on a wall, flags as an object, wall.update's patch set, fields before lookup, ids from next_wall_id, synchronous=FULL); 6.1/6.3 note what slice 4 actually sends (`walls` on GM connect); 8 points next_wall_id at the scene row; 14 records the whole slice as FIXED (owner-delegated). --- docs/DESIGN.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/docs/DESIGN.md b/docs/DESIGN.md index e108fdb..f602db6 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -1,6 +1,6 @@ # FFF_vtt design -Status: draft v0.15, 2026-11-15 (owner decisions recorded in section 14; v0.4 records Q4 (tile_size 32 px), the outbox lock invariant (4.2, 4.3), and the follow-ups in 3.4, 8, 11.1, 12, 13; v0.5 records the M2 move-rule decisions in 5.5 and updates 3.4, 4.2, 5.1, 5.4, 6.1, 7.4, 8, 10.3, 12, 13; v0.6 records #14 (characters and player data) in 3.1, 3.4, 4.1, 4.4, 6.1, 8, 9, 10.3, 13, 14; v0.7 records #6 (players and invites) in 3.1, 3.2, 3.4, 4.4, 6.1, 8, 12, 13, 14; v0.8 records #7 (map tiles) in 3.3, 3.4, 11.1, 13, 14; v0.9 records #9 (test tools) in 2, 10.4, 13, 14; v0.10 records #12 (what M3 done means) in 13, 14; v0.11 records #15 (M3 limits) in 4.3, 6.4, 10.1, 13, 14; v0.12 records #11 (M3 message list) in 3.1, 3.2, 3.4, 4.2, 4.3, 5.5, 5.6, 6, 7.5, 13, 14; v0.13 records the owner review of #11 flags in 1, 3.1, 5.5, 6.1, 8, 14; v0.14 records #17 (getting in: keys, sessions, the auth slice) in 3.2, 4.4, 6.5, 6.6, 8, 11.1, 13, 14; v0.15 records #18 (talking safely: the protocol shell and the `closing` close-reason message) in 3.2, 4.3, 6, 6.1, 6.2, 6.4, 6.5, 10.3, 13, 14. All of Q1-Q24 are RESOLVED or DEFERRED; none OPEN). Audience: the owner and coding agents. +Status: draft v0.16, 2026-11-15 (owner decisions recorded in section 14; v0.4 records Q4 (tile_size 32 px), the outbox lock invariant (4.2, 4.3), and the follow-ups in 3.4, 8, 11.1, 12, 13; v0.5 records the M2 move-rule decisions in 5.5 and updates 3.4, 4.2, 5.1, 5.4, 6.1, 7.4, 8, 10.3, 12, 13; v0.6 records #14 (characters and player data) in 3.1, 3.4, 4.1, 4.4, 6.1, 8, 9, 10.3, 13, 14; v0.7 records #6 (players and invites) in 3.1, 3.2, 3.4, 4.4, 6.1, 8, 12, 13, 14; v0.8 records #7 (map tiles) in 3.3, 3.4, 11.1, 13, 14; v0.9 records #9 (test tools) in 2, 10.4, 13, 14; v0.10 records #12 (what M3 done means) in 13, 14; v0.11 records #15 (M3 limits) in 4.3, 6.4, 10.1, 13, 14; v0.12 records #11 (M3 message list) in 3.1, 3.2, 3.4, 4.2, 4.3, 5.5, 5.6, 6, 7.5, 13, 14; v0.13 records the owner review of #11 flags in 1, 3.1, 5.5, 6.1, 8, 14; v0.14 records #17 (getting in: keys, sessions, the auth slice) in 3.2, 4.4, 6.5, 6.6, 8, 11.1, 13, 14; v0.15 records #18 (talking safely: the protocol shell and the `closing` close-reason message) in 3.2, 4.3, 6, 6.1, 6.2, 6.4, 6.5, 10.3, 13, 14; v0.16 records #19 (walls and doors: the scene/wall schema, the wall payload rules and the slice-4 rollout) in 4.4, 6.1, 8, 13, 14. All of Q1-Q24 are RESOLVED or DEFERRED; none OPEN). Audience: the owner and coding agents. Legend: - **[FIXED]** owner decision, not up for debate here. @@ -257,6 +257,7 @@ explored_chunk(player_id, scene_id, cx, cy, bits BLOB, PRIMARY KEY(player_id, sc - Prototype stores one row per explored cell (`exp(role, c)`, one INSERT per new cell, `HANDOFF.md`); real build stores **chunked bitmaps**: a chunk is e.g. 64x64 cells = 512 B; dirty chunks upserted per mutation [PROPOSED]. Sizes [INFERRED]: 4096 px map / 32 px cell = 16 384 cells = 2 KB bitmap; 16384 px map = 262 144 cells = 32 KB. - Entity move = one UPSERT of x,y (prototype: `INSERT OR REPLACE INTO tok`). - Migrations: `PRAGMA user_version` + forward-only migration list; startup refuses to run on a newer DB [PROPOSED]. Detail [FIXED (owner-delegated, 2026-11-14, #16)]: `schema_migration(version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)` is the history; `user_version` carries the same latest version, and startup refuses when the two disagree or the history is not contiguous from 1. One transaction per step (DDL + history row + `user_version`); a failing step rolls back completely, and an up-to-date DB is not written at all (re-open is a no-op). Step 1 (M3 slice 1) creates `meta(k TEXT PRIMARY KEY, v TEXT NOT NULL)`. Step 2 (M3 slice 2, `auth`) creates `player(id, name TEXT UNIQUE NULL until first sign-in, role 'gm'|'player', colour, created_at)`, a partial unique index `player_single_gm` on `role='gm'`, `player_key(player_id PK FK ON DELETE CASCADE, secret TEXT plaintext, created_at)`, and `session(id, player_id FK ON DELETE CASCADE, token_hash TEXT UNIQUE, created_at, last_seen)` plus `session_player`; removing a player therefore removes its key and sessions. `session.last_seen` exists but is not updated in M3 (sessions never expire, Q10). A step may hold several statements; the runner executes them in order inside the step's transaction (db_connector prepares one statement per call). +- Step 3 (M3 slice 4, `world`) [FIXED (owner-delegated, 2026-11-15, #19)]: creates `scene` and `wall` and seeds the single v1 scene. `scene(id PK, name, w REAL, h REAL, tile_size INT, cell INT, grid_size REAL, vision_radius_default REAL, token_radius_default REAL, art_version INT, active INT, next_wall_id INT, extra TEXT)`; the seeded row is `id 1, name 'Scene', 4096x4096, tile_size 32, cell 32, grid_size 0 (no grid), vision_radius_default 400, token_radius_default 20, art_version 1, active 1, next_wall_id 1, extra '{}'` (the prototype's world and radii, Q4's 32 px cell). `wall(scene_id FK ON DELETE CASCADE, id, ax, ay, bx, by REAL, kind TEXT CHECK (kind IN ('wall','door')), open INT CHECK (open IN (0,1)), flags TEXT JSON DEFAULT '{}', PRIMARY KEY (scene_id, id))`. Two consequences: (a) M3 has a scene without waiting for the M6 import CLI, so `wall.*` and `door.set` work in slice 4; `w, h, tile_size, cell, art_version` stay off the wire (6.1 `scene.set`). (b) The scene row carries `next_wall_id`, so wall ids survive restarts, removals and re-imports of the same scene (#3). Seeding in the migration rather than at startup keeps the scene identical on every start and lets the migration test assert it (10.3). - Startup: load everything into memory, recompute vision for all players (prototype does, in one transaction) so `vision` is never stale vs walls. - Shutdown: install a SIGTERM handler [prototype has only SIGINT, `HANDOFF.md`]: stop accepting, close sockets, final `wal_checkpoint(TRUNCATE)`, exit. Durability does not depend on it (WAL commit before ack). @@ -360,7 +361,7 @@ Conventions: - **Effects.** An intent's effects reach clients only as state messages, each built per recipient (3.1, 8) and sent only when that recipient's view changed (T11). "GM" below = every GM connection; "player X" = every connection of player X; "visible" = per the view rules in 3.1. - **Roles.** Sender column: P = player, G = GM. A P-sender intent from a connection whose player has no name yet is refused `not-allowed`, except `conn.ping` and `player.setname`. - **Not in M3:** `act.do` is reserved (v1 answers `no-rules`); server -> client `event` is reserved (9); GM `view.player` (7.3) is later; none is built in M3. Wire changes for M4/M5/M6 are limited to what 6.5 marks. -- **Slice-3 rollout note [FIXED (owner-delegated), #18].** The catalogue below is the contract; the handlers land slice by slice. Until a slice lands, its intents still get exactly one `ack` with the static code `unknown-type` and no state, so a half-built server never lies about having applied anything. Slice 3 builds the envelope, `hello`, `error`, `closing`, `conn.ping`, `act.do` (`no-rules`), and the three auth intents of slice 2 (`key.create`, `key.replace`, `player.remove`). +- **Slice-3 rollout note [FIXED (owner-delegated), #18].** The catalogue below is the contract; the handlers land slice by slice. Until a slice lands, its intents still get exactly one `ack` with the static code `unknown-type` and no state, so a half-built server never lies about having applied anything. Slice 3 builds the envelope, `hello`, `error`, `closing`, `conn.ping`, `act.do` (`no-rules`), and the three auth intents of slice 2 (`key.create`, `key.replace`, `player.remove`). Slice 4 (#19) builds `wall.add`, `wall.update`, `wall.remove` and `door.set`, plus the `walls` snapshot a GM gets on connect; everything else still answers `unknown-type`. ### 6.1 Catalogue @@ -400,6 +401,16 @@ Every row also carries `reqId` (omitted below). Codes are those of 6.5; `not-all Validation for `wall.*`: `a != b`, both in bounds, `kind` in `wall|door`, `flags` an object or absent. +**Wall payload rules [FIXED (owner-delegated, 2026-11-15, #19)]** (6.1 was silent on these; slice 4 builds them): +- `scene` is an integer scene id. v1 has exactly one scene (Q3), seeded by migration step 3 with id `1` (4.4); any other value is `not-found`, a non-integer is `invalid`. +- `a`/`b` are `[x,y]` arrays of exactly two finite JSON numbers (integers accepted); `a == b` and any endpoint outside `0 <= x <= w, 0 <= y <= h` are `invalid`. The border itself is in bounds. +- `open` is a boolean; omitted = `false`. `wall.add` with `kind:"wall"` and `open:true` is `invalid` (a wall is never open), and a stored wall always has `open=false` (a `kind` change door -> wall through `wall.update` therefore closes the door). +- `flags` is a JSON object or absent (absent = `{}`); anything else is `invalid`. It is stored verbatim and never interpreted. +- `wall.update` patches `a`, `b`, `kind`, `flags` only; at least one of them is required, otherwise `invalid`. `open` is not a patch field: it belongs to `door.set`, and an `open` field in `wall.update` is an unknown field (ignored, 6.2). Fields are checked before the target lookup (6.5 steps 7 then 8), so a fieldless `wall.update` is `invalid` whatever the id. +- `id` is an integer; lookup failures are `not-found` (a GM's missing target, 6.5 step 8). `door.set` on a row whose `kind` is not `door` is `invalid`. +- `wall.add` assigns the id from the scene's `next_wall_id`, which is monotonic and never reused (#3, 8; removal does not touch it, and a restart reloads it from the DB). +- Every write is one transaction with `synchronous=FULL` around it (Q12: GM authoring is the data that is expensive to lose) and `NORMAL` again afterwards; the in-memory scene is updated only after COMMIT, so a failed write leaves memory and DB identical. + **Tokens, statuses, props (9)** | t | fields | who | ack ok / extra codes | effects and recipients | @@ -460,7 +471,7 @@ Leak notes (R5 review, #11): (1) `kind` is GM-only because a value like `npc` wo - Breaking change -> bump `proto`, document in `docs/` changelog. ### 6.3 Reconnect and resync -- Reconnect = full resync. After `hello` the server sends, in this order [PROPOSED]: `roster`; GM: `walls`, `character` for all, `encountered` per player (full), then `token` for all; player: `character` for owned, `encountered` (full), `vision`, `explored` (full), then `token` for all visible and controlled. An unnamed player gets only `hello` until `player.setname` succeeds, which triggers the same sequence on that connection. The client reducer resets everything on `hello` (reconnect-reset bug fixed in prototype, test in `client/src/state.test.ts`, 4/4 pass) [MEASURED]. Clients must tolerate any order after `hello`. +- Reconnect = full resync. After `hello` the server sends, in this order [PROPOSED]: `roster`; GM: `walls`, `character` for all, `encountered` per player (full), then `token` for all; player: `character` for owned, `encountered` (full), `vision`, `explored` (full), then `token` for all visible and controlled. Slice 4 sends the `walls` part: one frame to every GM connection immediately after `hello` (players never get one); the rest of the order lands with slices 5-8. An unnamed player gets only `hello` until `player.setname` succeeds, which triggers the same sequence on that connection. The client reducer resets everything on `hello` (reconnect-reset bug fixed in prototype, test in `client/src/state.test.ts`, 4/4 pass) [MEASURED]. Clients must tolerate any order after `hello`. - No gap detection and no `resume(since)`: WS over TCP is ordered and lossless while connected, and a reconnect restarts from a full snapshot. Full resync on every reconnect [FIXED, Q15]. Revisit if a measured snapshot exceeds ~100 KB. - Client reconnect: exponential backoff with jitter, cap ~10 s (prototype: fixed 2 s). Pending `reqId`s are dropped with the old connection (client-side, M4); a `hello` on a live connection (`scene.set`, first name) does **not** drop them: their acks still arrive. Assets (tiles) are kept across reconnect because explored only grows [MEASURED prototype behaviour]. - Same player on two connections: both synced; each has its own `known` table. Multiple devices per player allowed (Q10, FIXED), up to 4 (4.3). @@ -583,7 +594,7 @@ Prototype `client/src/main.ts`: explored rects appended to a `Graphics` mask for ## 8. Data model -All ids are server-assigned. **Walls [FIXED, owner decision 2026-11-10, #3]**: per-scene ids, each scene counts from 1. Removal never renumbers; ids are never reused. Each scene stores `next_wall_id` (monotonic, never decreases, even if the newest wall is deleted). Wall identity = `(scene, id)` in DB, server and wire. Wall ids are GM-only in v1. **Tokens/entities [FIXED, owner decision 2026-11-10, #3]**: random opaque id, 12 chars from `[A-Za-z0-9_-]`, from a CSPRNG; server checks collision on create. Unique game-wide (not per-scene), so a token may later change scene keeping its id (cross-scene move not in M3). Counted ids would leak hidden tokens through gaps. **Doors are walls** and use wall ids; toggling is GM-only in v1, so any door-id exposure later leaks at most about the wall count (accepted). Rejected: dense ids with renumber broadcast (in-flight intents hit the wrong wall); game-wide wall counter (per-scene ids keep scene copy/export stable). +All ids are server-assigned. **Walls [FIXED, owner decision 2026-11-10, #3]**: per-scene ids, each scene counts from 1. Removal never renumbers; ids are never reused. Each scene stores `next_wall_id` (monotonic, never decreases, even if the newest wall is deleted). The column lives on the `scene` row created by migration step 3 (4.4), and the wire rules for the four wall intents are in 6.1. Wall identity = `(scene, id)` in DB, server and wire. Wall ids are GM-only in v1. **Tokens/entities [FIXED, owner decision 2026-11-10, #3]**: random opaque id, 12 chars from `[A-Za-z0-9_-]`, from a CSPRNG; server checks collision on create. Unique game-wide (not per-scene), so a token may later change scene keeping its id (cross-scene move not in M3). Counted ids would leak hidden tokens through gaps. **Doors are walls** and use wall ids; toggling is GM-only in v1, so any door-id exposure later leaks at most about the wall count (accepted). Rejected: dense ids with renumber broadcast (in-flight intents hit the wrong wall); game-wide wall counter (per-scene ids keep scene copy/export stable). - **Scene** (exactly one in v1, Q3): `id, name, w, h` (px), `tile_size` (32 default = `explored_cell` [FIXED, Q4]; 64 is the per-scene fallback), `explored_cell` (32 default; `tile_size % cell == 0` required, prototype assumes it silently), `grid_size`, `vision_radius_default`, `token_radius_default` (px, > 0, no upper cap [FIXED, #4]), `art_version`, `active`, free-form `extra`. - **Wall/Door**: `scene, id` (per-scene, see above), `a:[x,y], b:[x,y], kind ('wall'|'door'), open:bool, flags`. `blocks = not (door and open)`. GM-only everywhere. Later kinds (window, one-way, secret door) are extra `kind`/`flags` values; v1 geometry treats any non-open blocker identically. @@ -831,4 +842,6 @@ Owner also decided 2026-11-14 [FIXED (owner-delegated, 2026-11-14, #17)]: M3 sli Owner also decided 2026-11-15 [FIXED (owner decision), #18]: close codes at the mummy pin, option A. Mummy 0.4.8 cannot send an application close code (`close(ws)` -> an empty close frame -> the browser sees 1005); mummy is not forked or patched for it. Every server-initiated close is preceded by exactly one `closing {reason, code}` message carrying the reason of 6.5, then the close frame; a client treats 1005 plus that last message as the reason. `closing` is a catalogue message (server -> client, dotless noun) and the catalogue count is 15. Slice 2's eviction and revocation paths send it. Also recorded FIXED here: the server Ping interval (20 s, plus one right after `hello`; lag enforcement stays M5), the per-connection `n` rule (server frames only, no number for Ping/Pong, no global counter), the own `maxMessageLen` (64 KB) rather than mummy's default, and the slice-3 rollout note that catalogue intents whose slice has not landed answer `unknown-type`. See 3.2, 4.3, 6, 6.1, 6.2, 6.4, 6.5, 10.3. +Owner also decided 2026-11-15 [FIXED (owner-delegated, 2026-11-15, #19)]: walls and doors (M3 slice 4). Schema is migration step 3 (`world`): `scene` and `wall`, with the single v1 scene seeded at `id 1` (4096x4096, tile 32 = cell 32, no grid, vision 400, token radius 20, art_version 1, `next_wall_id` 1) (4.4). Wall ids are per scene from `next_wall_id`, monotonic and never reused across removal or restart (#3). The four intents `wall.add`/`wall.update`/`wall.remove`/`door.set` are GM-only and answer the 6.1 acks; the payload rules 6.1 left open are fixed there: integer `scene` (else `not-found`), `a`/`b` finite and in bounds with `a != b`, `open` boolean and never true on a plain wall, `flags` an object (default `{}`), `wall.update` patches only `a`/`b`/`kind`/`flags` and needs at least one, `door.set` on a non-door is `invalid`, fields before lookup. Effects: `wall`/`wall_gone` to every GM connection only, and a `walls` snapshot right after `hello` on a GM connection; players never receive wall data in any message (3.1, T2). Every wall write is one transaction with `synchronous=FULL` (Q12) and memory follows COMMIT, so a restart reloads exactly what was acked. See 4.4, 6.1, 6.3, 8, 10.3. + Owner also decided 2026-11-14 [FIXED (owner-delegated, 2026-11-14, #16)]: M3 slice 1 groundwork ("bare server"). Config keys, defaults, precedence and the canonical origin: 11.1. Migration history DDL, the agreement/contiguity rule and one-transaction-per-step: 4.4. Secret redaction (value registry plus sensitive field names, minimum secret length 8): 11.3. `GET /healthz` answers `200 {"status":"ok"}` from slice 1; the lock-bounded health check stays M5: 6.6, 11.3. `devbox run test` runs `nimble test` first and only then `node --test tools/*.test.mjs` (10.4); both logs land in `results/`. The server's listen address is the constant `config.BindAddress` = `127.0.0.1`, not a config key (T15). Dependencies: `fff_vtt.nimble` requires `mummy == 0.4.8` and `db_connector == 0.1.0`; devbox adds nodejs 26 and sqlite (`out`+`bin`, with `LD_LIBRARY_PATH=$DEVBOX_PACKAGES_DIR/lib` for the dlopen). Node 26 `{headers}` re-check: 10.4.