From efc72d68595d8eb6495daab5df7a3879eab2a167 Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Sun, 8 Nov 2026 10:24:12 +0100 Subject: [PATCH] docs: DESIGN.md fixes after M1 (layout, test counts, Q14 pointers, token-on-wall limitation, refs) - 13 M1: drop client/ and nix/ from M1 scope; client/ -> M4, nix/ -> M6 (Q20) - 33 checks -> 33 test blocks / 98 checks ported, none dropped (5.2, 10.1, 13 M1) - 5.4 and 12 H2: door closing on a token left open (Q14), with pinning test - 5.4: token-on-wall limitation stated as code behaviour + KNOWN LIMITATION test; escape fix as PROPOSED item in 13 M2 - 5.1/5.2/5.3/5.4: prototype README/NOTES/HANDOFF refs repointed to tests and server/geometry.nim; prototype NOTES kept as bug/measurement evidence Co-Authored-By: Claude Haiku 5.5 --- docs/DESIGN.md | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 5cd30ee..64e22a1 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -229,16 +229,16 @@ explored_chunk(player_id, scene_id, cx, cy, bits BLOB, PRIMARY KEY(player_id, sc ### 5.1 Algorithm (ported from `prototype-server-vision/src/geometry.nim`) - **Visibility polygon** from token centre: cast rays to every blocking wall endpoint at angle -eps, 0, +eps (`RayEps` = 1e-4 rad), plus an inscribed 64-gon of radius R as range limit; sort by angle; each ray returns nearest hit among wall segments + circle segments; result = CCW polygon. Open doors (`door && open`) do not block. -- **Explored**: `cellsTouched(poly, cell)` marks every grid cell that the polygon overlaps; union into the player's explored set. Vertex-in-cell test added after a bug (polygon wholly inside one cell) [MEASURED bug log, `NOTES.md`]. +- **Explored**: `cellsTouched(poly, cell)` marks every grid cell that the polygon overlaps; union into the player's explored set. Vertex-in-cell test added after a bug (polygon wholly inside one cell) [MEASURED bug log, prototype `NOTES.md`; covered by `tests/tgeometry.nim`, suite "cells", test "tiny polygon inside one cell"]. - **Token visibility** to a player: token centre inside any of the player's polygons (even-odd `inside`). - **Move validation**: inclusive segment intersection on blocking walls (touching = blocked, so no squeezing through shared vertices) AND destination circle at distance >= token radius from every blocking wall. -### 5.2 Edge cases handled and tested [MEASURED, `tests/tgeometry.nim`, 33 checks, `results/nimble-test.log`] +### 5.2 Edge cases handled and tested [MEASURED, `tests/tgeometry.nim`, 33 test blocks / 98 checks ported, none dropped, `results/nimble-test.log`] - Collinear rays skipped (neighbouring +-eps rays cover them). - Token standing on an endpoint: that endpoint skipped, hits at t <= 1e-9 ignored. - T-junction, shared vertex, free-standing wall, '<' apex, box corner, behind-viewer fan, long thin corridor. -- Angle-sort bug: circle angles must use the same (-pi, pi] range as wall angles (`atan2(sin,cos)`), otherwise bow-tie polygon (position dependent) [MEASURED bug, `NOTES.md` "Post-phase-3"]. Regression test exists. -- Vision circle is an inscribed 64-gon: error <= ~0.12% of R [MEASURED, NOTES]. +- Angle-sort bug: circle angles must use the same (-pi, pi] range as wall angles (`atan2(sin,cos)`), otherwise bow-tie polygon (position dependent) [MEASURED bug, prototype `NOTES.md` "Post-phase-3"]. Regression test: `tests/tgeometry.nim`, suite "visibility", test "wall endpoints behind the viewer: fan stays angularly ordered". +- Vision circle is an inscribed 64-gon: error <= ~0.12% of R [MEASURED, prototype `NOTES.md`]. ### 5.3 Performance [MEASURED; single laptop, release build, loopback, power-save CPU; `results/bench.log`, `results/concurrency-final1-server.log`] | item | avg | worst | @@ -252,13 +252,14 @@ explored_chunk(player_id, scene_id, cx, cy, bits BLOB, PRIMARY KEY(player_id, sc Conclusions: - `cellsTouched` is ~12x the polygon itself and dominates [MEASURED]. M2 target: replace per-cell polygon tests with scanline rasterization of the polygon over the explored grid, differential-tested against the ported implementation [PROPOSED]. -- Polygon cost is O(rays x walls in bbox) [MEASURED code reading, `HANDOFF.md`]. Prototype scene has 60 walls and 8 tokens, so this says nothing about big maps. With radius-bbox culling the cost scales with local wall density, not total walls [INFERRED]; a uniform spatial grid over walls is needed when scenes reach thousands of walls (M2 gate: bench with a synthetic 5k-wall scene, target polygon < 1 ms [PROPOSED target]). +- Polygon cost is O(rays x walls in bbox) [MEASURED code reading, `server/geometry.nim` `visibilityPolygon` (ported unchanged from prototype)]. Prototype scene has 60 walls and 8 tokens, so this says nothing about big maps. With radius-bbox culling the cost scales with local wall density, not total walls [INFERRED]; a uniform spatial grid over walls is needed when scenes reach thousands of walls (M2 gate: bench with a synthetic 5k-wall scene, target polygon < 1 ms [PROPOSED target]). - Skip recompute when the token moved but its polygon is unchanged (prototype already compares `np == r.polys`). ### 5.4 Known limitations (carry forward) -- Token exactly on a wall cannot move off it (inclusive test); server must keep tokens >= radius off walls, including when a door closes on a token [MEASURED limit, README/NOTES]. Decision: closing a door with a token in its radius is allowed; the token may step perpendicular out? Not supported by prototype; see Q14. +- Token exactly on a wall cannot move off it: `moveBlocked` counts any touch as a hit, so every move from that point is blocked, even a perpendicular step to the open side [MEASURED limit, `tests/tgeometry.nim` test "KNOWN LIMITATION: token exactly on a wall cannot move off it"; also in the `server/geometry.nim` header]. Known limitation, not a policy. Fix proposed in 13 M2 [PROPOSED]. A validated move cannot reach this state (a destination within r of a wall is rejected) [INFERRED from `moveBlocked`]; it can arise from a wall/door edit landing on a token, or from a placement intent (6.1 does not say whether placements are collision-checked). +- Closing a door (or editing a wall) on a token: policy open, see Q14; no decision here. Current behaviour is pinned by `tests/tgeometry.nim`, suite "door/wall closing on a token", test "door closed on a token: Q14 undecided, DESIGN 5.4 vs 12 H2 conflict": a token whose centre is 10 px off a closed door may move to a clear spot, may not stay overlapped, and may not cross the door. That test pins `moveBlocked` only; what the server does on a door edit under a token is not pinned here. - Boundary epsilon ambiguity (RayEps 1e-4, inclusive tolerances). -- Explored and tile checks in the leak test recompute with the server's own `cellsTouched`: self-consistency only (README caveat). New independent oracle in section 10. +- Explored and tile checks in the leak test recompute with the server's own `cellsTouched`: self-consistency only (caveat in 10.2). New independent oracle in section 10. - Doors/walls changing does not un-explore anything; explored only grows [MEASURED behaviour]. - Single uniform vision radius in prototype; v1 uses a per-entity numeric property (`vision_radius`) with scene default; no light model. - Vision origin = token centre, radius is not clipped by the token's own size. @@ -409,7 +410,7 @@ Principles carried from the prototype: the checker must be independent of the co ### 10.1 Carry over (port, keep as acceptance gates) | Test | From | Notes | |---|---|---| -| Geometry unit tests (33 checks) | `tests/tgeometry.nim`, `results/nimble-test.log` | Port with `geometry.nim`. | +| Geometry unit tests (33 test blocks / 98 checks ported, none dropped) | `tests/tgeometry.nim`, `results/nimble-test.log` | Port with `geometry.nim`. | | Geometry mutation check | `tools/mutation.sh` (a: no +-RayEps rays, b/c: upper/lower segment end exclusive) | Control passes, each mutation fails >= 1 test [MEASURED 1, 2, 2]. Port. | | Leak test | `tools/leak.mjs`, `leakcheck.nim`, `leaktest.sh` | Subject A and B, fresh DB, ~240 steps; asserts (a)-(e) in README. Independent LOS oracle for token visibility (added after the bow-tie bug). | | Negative control / fault injection | `tools/faulttest.sh`, `-d:leakfault` build | 8 faults, exact failure-class set each, clean build passes, `LEAK_FAULT` string asserted absent from clean binary [MEASURED 8/8]. Port and extend. | @@ -480,7 +481,7 @@ Source: `prototype-server-vision/HANDOFF.md` (and README/NOTES worries). | # | Item | Decision / open | Milestone | |---|---|---|---| | H1 | Port geometry + tests as-is | Decision: port `geometry.nim`, `tgeometry.nim`, mutation check, leak harness first | M1 | -| H2 | Token on a wall can't leave (inclusive `segsCross`) | Open Q14; v1 policy: server keeps tokens >= r off walls when the GM edits walls/doors; closing a door on a token: see Q14 | M2 | +| H2 | Token on a wall can't leave (inclusive `segsCross`); closing a door or editing a wall on a token | Open Q14: no decision here. Today: token exactly on a wall is blocked from any move (`tests/tgeometry.nim`, test "KNOWN LIMITATION: token exactly on a wall cannot move off it"); token overlapping a closed door may walk to a clear spot, may not stay overlapped, may not cross (`tests/tgeometry.nim`, suite "door/wall closing on a token", test "door closed on a token: Q14 undecided, DESIGN 5.4 vs 12 H2 conflict"). Escape fix for the on-wall case: PROPOSED, see 13 M2. Edit policy under a token: open, Q14. | M2 | | H3 | O(rays x walls) vision | Decision: radius-bbox cull now, uniform spatial grid over walls when bench shows need; 5k-wall bench gate | M2 | | H4 | `cellsTouched` ~12x polygon | Decision: scanline rasterization, differential test vs ported version | M2 | | H5 | Shared-endpoint test gap | Decision: port with a test that fails if either arm is removed; new mutation (d) | M1 | @@ -513,22 +514,23 @@ Source: `prototype-server-vision/HANDOFF.md` (and README/NOTES worries). Each is small, ends with a green gate, and no milestone depends on browser UI before M4. - **M1 - Port geometry + tests.** - - Scope: new repo layout (`server/`, `client/`, `tools/`, `tests/`, `nix/`), devbox config, `geometry.nim` + `tgeometry.nim`, mutation script, add mutation (d) and the shared-endpoint arm test. - - Accept: `nimble test` >= 33 checks green; mutations a-d each fail >= 1 test, control passes; repo builds with devbox only. + - Scope: new repo layout (`server/`, `tools/`, `tests/`), devbox config, `geometry.nim` + `tgeometry.nim`, mutation script, add mutation (d) and the shared-endpoint arm test. + - Accept: `nimble test` green: 33 test blocks / 98 checks ported, none dropped (plus new tests on top); mutations a-d each fail >= 1 test, control passes; repo builds with devbox only. - **M2 - Vision engine v2.** - Scope: scanline `cellsTouched`, differential + independent explored oracle tests, spatial grid for walls, swept-circle move check, step cap, bench harness incl. 5k-wall scene. - - Accept: results equal old implementation on the corpus; polygon+cells bench <= prototype (0.451 ms) on the prototype scene and polygon < 1 ms on the 5k-wall scene [targets PROPOSED]; probing test passes. + - Token-on-wall escape [PROPOSED fix for 5.4 / H2]: in `moveBlocked`, ignore the t=0 hit of a wall the token stands on when the segment does not cross that wall at t>eps and the destination is >= r from it. Moves through the wall stay blocked; moves along it stay blocked (collinear overlap is not an escape). Replace the KNOWN LIMITATION test with positive assertions. + - Accept: results equal old implementation on the corpus; polygon+cells bench <= prototype (0.451 ms) on the prototype scene and polygon < 1 ms on the 5k-wall scene [targets PROPOSED]; probing test passes; token-on-wall escape positive tests pass. - **M3 - Server core.** - Scope: entities with bucketed props, players, invites/sessions, scenes, chunked explored persistence, typed per-recipient views and single egress, per-connection counters, proto versioning, wall/entity CRUD intents, Origin check, SQLite schema + migrations. - Accept: ported leak test (A and B) + fault test with the extended fault classes: all caught with exact class sets, clean build passes; restart test passes incl. CRUD; auth tests pass; no secrets in logs. - **M4 - Client v1.** - - Scope: Vite + TS + Pixi v8 client: layers, tiles, grid, tokens (BitmapText), fog textures, wall/door editor, token drag with prediction/rollback, DOM overlay, reconnect backoff, reducer tests. + - Scope: new `client/` dir (first real client build): Vite + TS + Pixi v8 client: layers, tiles, grid, tokens (BitmapText), fog textures, wall/door editor, token drag with prediction/rollback, DOM overlay, reconnect backoff, reducer tests. - Accept: headless e2e: fog pixel checks, player traffic audit clean, reconnect restores; manual run on laptop and Note 10 at 60 fps with 50 tokens, 16384 px map (tiled), fog active. - **M5 - Robustness and observability.** - Scope: SIGTERM handler, bounded metrics + SLOW lines, connection cap, frame/rate limits, slow-client handling per Q13, healthz. - Accept: concurrency 60 s x3 pass with p99 <= prototype (4.98 ms) +50%; slow-client test bounded; stats computation shown outside the lock (lock hold time unchanged with metrics on, measured). - **M6 - NixOS deployment.** - - Scope: NixOS module, nginx vhost, systemd hardening, backups timer, import CLI for maps. + - Scope: `nix/` dir (Nix packaging of the Nim server and client build; method per Q20), NixOS module, nginx vhost, systemd hardening, backups timer, import CLI for maps. - Accept: deployed behind HTTPS on the owner's server; `/_tiles/` 404 directly; WS upgrade works; invite join works from phone; access log has no secret; backup + restore drill passes. - **M7 - Real-world pass.** - Scope: real-network latency/throughput run, iOS/Safari check (if available), one real session with the owner's table, collect SLOW lines.