diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 47badbc..b0dd69f 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -242,7 +242,7 @@ explored_chunk(player_id, scene_id, cx, cy, bits BLOB, PRIMARY KEY(player_id, sc - **Token visibility** to a player: token centre inside any of the player's polygons (even-odd `inside`). - **Move validation (prototype)**: 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. Replaced by the M2 rules in 5.5; the prototype result is a baseline only. -### 5.2 Edge cases handled and tested [MEASURED, `tests/tgeometry.nim`, 33 test blocks / 98 checks ported, none dropped, `results/nimble-test.log`] +### 5.2 Edge cases handled and tested [MEASURED, `tests/tgeometry.nim`; M1 port baseline (history): 33 test blocks / 98 checks, none dropped; current count: 87 `[OK]` test cases in `results/nimble-test.log` (M2, 2026-11-08)] - 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. @@ -261,12 +261,12 @@ 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, `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 test target, Q2: owner has no map size yet; revisit with real maps]). +- 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 test target, Q2: owner has no map size yet; revisit with real maps]). The 5k target is the sparse primary layout `genScene(16384, 5000, 1)` (`tools/scenegen.nim`): 0.082 ms avg [MEASURED, `results/bench.log`]. The dense layout `genScene(4096, 5000, 1)` is a stress reference (1.28 ms avg, ~207 walls in the vision bbox) [MEASURED]; revisit the uniform grid if real maps approach that density. - 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: `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. Fixed in M2 by the escape rule (5.5). The escape is required by Q14 so the GM can move such a token: mechanism [PROPOSED], requirement [FIXED]. A validated move cannot reach this state (a destination within r of a wall is rejected) [INFERRED from `moveBlocked`]; it arises from a wall/door edit (`blocked` trigger, 5.5) or from a GM placement on a wall (GM placements ignore walls, 5.5); the escape rule covers both. -- Closing a door (or editing a wall) on a token [FIXED, Q14]: the token gets `blocked` (trigger narrowed to circle overlap, 5.5). The server refuses that token's player moves; the GM can still move it; status clears when the wall is removed or the door opened, or when the GM clears it. Current pin: `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. It pins `moveBlocked` only; it is replaced in M2 by tests of the 5.5 policy (under the 5.5 trigger this token is `blocked`, so player moves are refused). +- Closing a door (or editing a wall) on a token [FIXED, Q14]: the token gets `blocked` (trigger narrowed to circle overlap, 5.5). The server refuses that token's player moves; the GM can still move it; status clears per the 5.5 Keep/Clear rules (wall removed or door opened and no other wall overlaps, GM move to a free spot, or GM clear). Current pin: `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. It pins `moveBlocked` only; it is replaced in M2 by tests of the 5.5 policy (under the 5.5 trigger this token is `blocked`, so player moves are refused). - 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 (caveat in 10.2). New independent oracle in section 10. - Doors/walls changing does not un-explore anything; explored only grows [MEASURED behaviour]. @@ -278,11 +278,15 @@ Normative for M2. Replaces the prototype move validation (5.1) and the move-rela Notation: token centre `c`, radius `r` (scene `token_radius`), start `s`, destination `d`. Blocking wall: segment with `kind=wall`, or `kind=door` with `open=false`; open doors do not block. `dist(X, W)` = distance from point or segment `X` to segment `W`. "Overlaps" = `dist < r` (strict; touching at exactly r is clear). -**`blocked` flag** (stored bool on the entity, persisted) [FIXED, owner (B) and Q14, except where marked] -- Set [FIXED, owner (B)]: a wall is added, or a door is closed, and the new blocking segment overlaps any part of the token's circle, i.e. `dist(c, W) < r`. Centre-on-wall is not required. -- Clear [FIXED, Q14]: the wall is removed, the door is opened, or the GM clears it. The GM-clear wire intent is defined in M3, not M2. +**`blocked` flag** ("stuck": the player cannot move the token at all; the GM always can) (stored bool on the entity, persisted) [FIXED, owner (B), Q14, refined 2026-11-08] +- Overlap: `dist(c, W) < r` for a blocking wall `W`. An open door stops blocking, so opening = removing that wall for this rule; closing = adding it. Centre-on-wall is not required. +- Set: a wall is added or updated, or a door is closed -> every token overlapping that wall becomes `blocked`. The GM places or moves a token to a position overlapping any blocking wall -> `blocked`. +- Keep: while any blocking wall still overlaps the token it stays `blocked` (opening a door does not free it if another wall overlaps). +- Clear: a wall is removed or a door opened -> each token that overlapped that wall gets `blocked` = overlaps any remaining blocking wall. The GM moves the token to a position overlapping no blocking wall -> cleared. GM manual clear -> cleared unconditionally (wire intent defined in M3). +- An event changes only the tokens it touches (those overlapping the changed wall, or the moved token). An unrelated wall change elsewhere never re-blocks a GM-cleared token. +- After a GM manual clear while a wall still overlaps, the player escape rule (step 3 below) applies unchanged: the token may step out without crossing, destination >= r from every blocking wall. - `wall.update` = remove then add for this rule [PROPOSED; not in the owner's list]. -- GM placements and GM moves never set `blocked` [PROPOSED; follows from (D)]. +- Player moves while `blocked`: the uniform nack (unchanged). **Player move `s -> d`** [FIXED, accepted default]. Checks in order; any failure gives the uniform nack. 1. Owner check (non-owned -> `error`, as now) and token not `blocked`. @@ -292,7 +296,7 @@ Notation: token centre `c`, radius `r` (scene `token_radius`), start `s`, destin - otherwise (swept/capsule check): allowed only if `dist(segment s->d, W) >= r`, which also gives `dist(d, W) >= r`. 4. Accept: `moveAck{ok:true, pos:d}`. Nack: `moveAck{ok:false, pos:s}` for blocked, over-cap and collision alike. No reason field. -**GM move** [FIXED, owner (D)]: no wall check, no step cap, ignores `blocked`. Bounds and referential checks still apply. +**GM move** [FIXED, owner (D)]: no wall check, no step cap, ignores `blocked`. Bounds and referential checks still apply; after the move `blocked` is recomputed (set/clear rules above). A GM move out of bounds gets the same uniform nack as a player move (`moveAck{ok:false, pos:old}`) [RECORDED, as implemented]. **Wall index** [FIXED, accepted default, H3]: radius-bbox cull first; uniform spatial grid over walls only if the 5k-wall bench misses target (13, M2). @@ -300,7 +304,7 @@ Notation: token centre `c`, radius `r` (scene `token_radius`), start `s`, destin **Regression baseline** [FIXED, accepted default]: "results equal old implementation" = `visibilityPolygon` and `cellsTouched` on the prototype leak transcripts plus `tests/fixtures/scene.json`. `moveBlocked` is excluded: its results change on purpose. -**Unconfirmed** [PROPOSED, owner to confirm]: (a) `wall.update` as remove + add; (b) a door opened while another closed wall still overlaps the token clears `blocked` (literal Q14 wording). +**Unconfirmed** [PROPOSED, owner to confirm]: (a) `wall.update` as remove + add. Former (b) (door opened while another wall overlaps clears `blocked`) is closed by the Keep rule above [FIXED, owner, 2026-11-08]. --- @@ -314,9 +318,9 @@ Client -> server (intents). Authority column: P = player, G = GM. | t | fields (contract) | who | server behaviour | |---|---|---|---| -| `move` | `reqId:int, id, pos:[x,y]` | P (owned) / G (any) | validate; reply `moveAck` (+ `token` diffs); non-owned -> `error`; P move refused (`blocked`, over step cap, or wall collision; 5.5) -> `moveAck{ok:false, pos:old}`, one uniform nack; G move ignores walls, step cap and `blocked` (5.5) | -| `door` | `id, open:bool` | G | toggle; recompute all players; close sets `blocked` on tokens whose circle overlaps the door, open clears it (Q14, 5.5) | -| `wall.add` / `wall.update` / `wall.remove` | wall fields / `id` | G | validate geometry; recompute all players; GM-only broadcast of wall delta; add sets `blocked` on tokens whose circle overlaps the wall, remove clears it, update = remove + add (Q14, 5.5; update PROPOSED) | +| `move` | `reqId:int, id, pos:[x,y]` | P (owned) / G (any) | validate; reply `moveAck` (+ `token` diffs); non-owned -> `error`; P move refused (`blocked`, over step cap, or wall collision; 5.5) -> `moveAck{ok:false, pos:old}`, one uniform nack; G move ignores walls, step cap and `blocked`, then sets/clears `blocked` by overlap; out-of-bounds G move gets the same nack (5.5) | +| `door` | `id, open:bool` | G | toggle; recompute all players; close sets `blocked` on tokens whose circle overlaps the door, open recomputes it for those tokens (still blocked if another wall overlaps; Q14, 5.5) | +| `wall.add` / `wall.update` / `wall.remove` | wall fields / `id` | G | validate geometry; recompute all players; GM-only broadcast of wall delta; add sets `blocked` on tokens whose circle overlaps the wall, remove recomputes it for those tokens (Keep rule), update = remove + add (Q14, 5.5; update PROPOSED) | | `entity.create` / `entity.update` / `entity.remove` | kind, name, pos, owner, props buckets | G | validate against property schema; view diff | | `prop.set` | `id, bucket, key, value` | G (v1) | owner-bucket writes by players come with FFF actions (section 9) | | `scene.set` | scene meta | G | broadcasts new `hello`-equivalent reset to all; no `scene.activate` (one scene, Q3) | @@ -408,7 +412,7 @@ Prototype `client/src/main.ts`: explored rects appended to a `Graphics` mask for ## 8. Data model -All ids are server-assigned, opaque, non-guessable where they are exposed to players (entities); walls use dense integers GM-side only. +All ids are server-assigned, opaque, non-guessable where they are exposed to players (entities); walls use dense integers GM-side only. M3 item: id stability on removal. `removeWall` currently shifts later indices down (dense ids); M3 must give walls stable ids (or renumber with a broadcast) before the WS layer exposes them. - **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`, `art_version`, `active`, free-form `extra`. - **Wall/Door**: `id, scene, 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. @@ -448,7 +452,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 test blocks / 98 checks ported, none dropped) | `tests/tgeometry.nim`, `results/nimble-test.log` | Port with `geometry.nim`. | +| Geometry unit tests (M1 port baseline: 33 test blocks / 98 checks, none dropped; current count in `results/nimble-test.log`) | `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. | @@ -519,7 +523,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`); closing a door or editing a wall on a token | Decision [FIXED, Q14]: token gets `blocked` (player moves refused, GM moves allowed, clears on wall removal, door open, or GM clear). Trigger = circle overlap [FIXED, owner]; escape + capsule rule [FIXED, accepted default]; both in 5.5. Pin tests replaced in M2. | M2 | +| H2 | Token on a wall can't leave (inclusive `segsCross`); closing a door or editing a wall on a token | Decision [FIXED, Q14]: token gets `blocked` (player moves refused, GM moves allowed). Set/Keep/Clear per 5.5: stays while any blocking wall overlaps; clears on wall removal or door open only if no other wall overlaps, on a GM move to a free spot, or on GM clear; GM placements/moves onto a wall set it. Trigger = circle overlap [FIXED, owner]; escape + capsule rule [FIXED, accepted default]; both in 5.5. Pin tests replaced in M2. | M2 | | H3 | O(rays x walls) vision | Decision: radius-bbox cull now; uniform spatial grid over walls only if the 5k-wall bench misses target (5.5) | 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 | @@ -553,10 +557,10 @@ Each is small, ends with a green gate, and no milestone depends on browser UI be - **M1 - Port geometry + tests.** - 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. + - Accept: `nimble test` green: M1 port baseline 33 test blocks / 98 checks, none dropped (plus new tests on top; current count in `results/nimble-test.log`); mutations a-d each fail >= 1 test (M1; M2 adds e-l in `tools/mutation.sh`), control passes; repo builds with devbox only. - **M2 - Vision engine v2 and move rules** (decisions in 5.5, recorded 2026-11-08). - - Scope: scanline `cellsTouched`, differential + independent explored oracle tests, radius-bbox wall cull (spatial grid only if the 5k bench misses), capsule/swept move check and token-on-wall escape (5.5), step cap = effective vision radius (5.5), `blocked` flag with circle-overlap trigger (5.5), GM moves ignore walls and step cap, bench harness incl. 5k-wall scene, pure in-process move-rules module with unit tests. The WS/hostile-client harness is M3, not M2. - - Accept: `visibilityPolygon` and `cellsTouched` equal the old implementation on the corpus (5.5); `moveBlocked` excluded (its results change on purpose); polygon+cells bench <= prototype (0.451 ms) on the prototype scene and polygon < 1 ms on the 5k-wall scene [targets PROPOSED]; in-process probing test passes; escape and capsule positive and negative unit tests pass; `blocked` tests pass (overlap trigger, player move refused, GM move allowed, cleared on wall removal / door open / GM clear); over-cap move gives the same nack as a blocked move; thresholds documented in the module header; oracle tolerance 0.5 px. The KNOWN LIMITATION test and the Q14 pin test are replaced with positive assertions and `blocked`-policy tests. + - Scope: scanline `cellsTouched`, differential + independent explored oracle tests, radius-bbox wall cull (spatial grid only if the 5k bench misses), capsule/swept move check and token-on-wall escape (5.5), step cap = effective vision radius (5.5), `blocked` flag with circle-overlap trigger and Set/Keep/Clear policy (5.5), GM moves ignore walls and step cap (and set/clear `blocked`), bench harness incl. 5k-wall scene, pure in-process move-rules module with unit tests. The WS/hostile-client harness is M3, not M2. + - Accept: `visibilityPolygon` and `cellsTouched` equal the old implementation on the corpus (5.5); `moveBlocked` excluded (its results change on purpose); polygon+cells bench <= prototype (0.451 ms) on the prototype scene and polygon < 1 ms on the 5k-wall scene [targets PROPOSED]; in-process probing test passes; escape and capsule positive and negative unit tests pass; `blocked` tests pass (every Set/Keep/Clear rule of 5.5: overlap trigger, doorway, two overlapping walls, GM drop/move sets and clears, GM clear then escape, unrelated change does not re-block); mutations k (no Keep) and l (GM moves do not set blocked) each fail >= 1 test; over-cap move gives the same nack as a blocked move; thresholds documented in the module header; oracle tolerance 0.5 px. The KNOWN LIMITATION test and the Q14 pin test are replaced with positive assertions and `blocked`-policy tests. - **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, plaintext invite secrets and non-expiring sessions (Q10, Q11), 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.