From ef19d362c69a889884656e01f9ed53613f57febd Mon Sep 17 00:00:00 2001 From: Davide Cappellini Date: Sun, 1 Nov 2026 23:25:10 +0100 Subject: [PATCH] prototype-server-vision: fix scrambled visibility fan (angle range mix), leaktest SUBJECT=A|B + independent LOS oracle Co-Authored-By: Claude Sonnet 5.5 --- prototype-server-vision/NOTES.md | 4 +++ prototype-server-vision/README.md | 2 +- prototype-server-vision/devbox.json | 3 +- prototype-server-vision/src/geometry.nim | 2 +- prototype-server-vision/tests/tgeometry.nim | 6 ++++ prototype-server-vision/tools/leak.mjs | 6 ++-- prototype-server-vision/tools/leakcheck.nim | 36 +++++++++++++-------- 7 files changed, 40 insertions(+), 19 deletions(-) diff --git a/prototype-server-vision/NOTES.md b/prototype-server-vision/NOTES.md index 7aa86c9..8df4d9b 100644 --- a/prototype-server-vision/NOTES.md +++ b/prototype-server-vision/NOTES.md @@ -23,3 +23,7 @@ - Leak scenario bugs were scenario bugs (diagonal steps through walls), not server bugs; server answered `ok:false` as designed. - Leak checker B-string check initially flagged legit `remove{id:"B"}`; exempted token/remove. - Perf finding: cellsTouched (~0.6 ms) is 15x the visibility polygon (~0.04 ms); per-move vision_ms is dominated by explored-cell computation. + +## Post-phase-3 manual-test bug +- BUG (server, geometry.nim): `visibilityPolygon` sorted circle-segment angles in [0,2pi) together with wall-endpoint atan2 angles in (-pi,pi] -> fan vertices out of angular order -> bow-tie polygon, even-odd `inside`/`cellsTouched` dropped regions (tokens in plain LOS not shown, explored/tiles missing). Position dependent. FIX: wrap circle angles with atan2(sin,cos). +- Why tests missed it: leakcheck recomputed its oracle with the same `visibilityPolygon` (only checked "nothing extra leaks", never "everything visible shows"), and only subject A was checked. Now: independent clear-LOS (segment test) oracle, `SUBJECT=A|B`, geometry regression test. diff --git a/prototype-server-vision/README.md b/prototype-server-vision/README.md index 202af6e..1047238 100644 --- a/prototype-server-vision/README.md +++ b/prototype-server-vision/README.md @@ -21,7 +21,7 @@ Browser rendering was NOT verified (no browser in the build environment): only H | `devbox run test` | regen map + `nimble test` (geometry: visibility, moves, cells, seed sanity) | | `devbox run client` | includes client reducer test (`npm run check`, incl. reconnect reset) | | `PORT=8088 devbox run smoke` | fresh DB, server+nginx, auth/cookie/tile/steal/garbage checks | -| `PORT=8088 devbox run leaktest` | fresh DB; A walks ~240 steps through rooms, corridor, open/closed doors while GM + B act; Nim checker (`tools/leakcheck.nim`) replays `run/leak-transcript.jsonl` | +| `PORT=8088 devbox run leaktest` | runs twice (SUBJECT=A, SUBJECT=B), fresh DB each; subject walks ~240 steps through rooms, corridor, open/closed doors while GM + B act; Nim checker (`tools/leakcheck.nim`) replays `run/leak-transcript.jsonl` | | `PORT=8088 devbox run concurrency` | 60 s (`DUR=` secs) A, B, GM(6 monsters + doors) at 20-50 ms per token | | `devbox run bench` | vision microbench | diff --git a/prototype-server-vision/devbox.json b/prototype-server-vision/devbox.json index 30713fe..027f810 100644 --- a/prototype-server-vision/devbox.json +++ b/prototype-server-vision/devbox.json @@ -37,7 +37,8 @@ "nginx -p \"$PWD/\" -c run/nginx.conf -e run/error.log" ], "leaktest": [ - "tools/withserver.sh sh -c 'node tools/leak.mjs && nim c -d:release --hints:off -o:run/leakcheck tools/leakcheck.nim && run/leakcheck'" + "nim c -d:release --hints:off -o:run/leakcheck tools/leakcheck.nim", + "for s in A B; do SUBJECT=$s tools/withserver.sh sh -c 'node tools/leak.mjs && run/leakcheck' || exit 1; done" ], "concurrency": [ "tools/withserver.sh node tools/concurrency.mjs" diff --git a/prototype-server-vision/src/geometry.nim b/prototype-server-vision/src/geometry.nim index ed1fb92..46a49dd 100644 --- a/prototype-server-vision/src/geometry.nim +++ b/prototype-server-vision/src/geometry.nim @@ -36,7 +36,7 @@ proc visibilityPolygon*(o: Vec, walls: openArray[Wall], radius: float, let a1 = 2 * PI * (i + 1).float / circleSegs.float segs.add(((o.x + radius * cos a0, o.y + radius * sin a0), (o.x + radius * cos a1, o.y + radius * sin a1))) - angles.add a0 + angles.add arctan2(sin a0, cos a0) # same (-pi,pi] range as the wall angles, else the sort scrambles the fan for w in walls: if not w.blocks: continue # shortcut: walls beyond the circle are only culled by ray clipping, O(rays*walls); upgrade: spatial grid diff --git a/prototype-server-vision/tests/tgeometry.nim b/prototype-server-vision/tests/tgeometry.nim index eada2f1..90931db 100644 --- a/prototype-server-vision/tests/tgeometry.nim +++ b/prototype-server-vision/tests/tgeometry.nim @@ -88,6 +88,12 @@ suite "visibility": check not sees(w, (5.0, 0.0), (1010.0, 0.0), 2000) check sees(w, (5.0, 0.0), (900.0, 8.0), 2000) + test "wall endpoints behind the viewer: fan stays angularly ordered": + # regression: circle angles in [0,2pi) were sorted against atan2 angles in (-pi,pi] -> bow-tie polygon + let w = box(600, 600, 680, 680) & @[W(1024, 256, 1024, 608)] + check sees(w, (908.66, 672.03), (789.36, 584.65), 400) + check sees(w, (789.36, 584.65), (908.66, 672.03), 400) + test "radius clips": check not sees(@[], (0.0, 0.0), (300.0, 0.0), 200) diff --git a/prototype-server-vision/tools/leak.mjs b/prototype-server-vision/tools/leak.mjs index 28c554a..a741493 100644 --- a/prototype-server-vision/tools/leak.mjs +++ b/prototype-server-vision/tools/leak.mjs @@ -1,10 +1,12 @@ // Leak test driver: scripted walk of A, GM + B as ground truth/noise. Writes run/leak-transcript.jsonl; tools/leakcheck.nim verifies it. +// SUBJECT=A|B (default A) is the player whose tile sweeps/view are checked (leakcheck reads the same env); the other walks as noise. // Everything is sequential (one actor at a time) so every tile sweep sees a quiescent server at a known gseq. import fs from 'node:fs' import { O, connect, sleep } from './lib.mjs' const out = fs.createWriteStream(new URL('../run/leak-transcript.jsonl', import.meta.url)) const log = o => out.write(JSON.stringify(o) + '\n') let gmG = 0 +const S = process.env.SUBJECT ?? 'A' const acks = new Map(), req = { A: 0, B: 0, GM: 0 } const mk = who => connect(who === 'GM' ? 'gm' : who, m => { log({ k: 'msg', who, m }) @@ -22,7 +24,7 @@ async function sweep(label) { const g = gmG for (let i = 0; i < tiles.length; i += 32) await Promise.all(tiles.slice(i, i + 32).map(async ([x, y]) => { - const r = await fetch(`${O}/tiles/${x}_${y}.png`, { headers: { Cookie: A.cookie } }) + const r = await fetch(`${O}/tiles/${x}_${y}.png`, { headers: { Cookie: cl[S].cookie } }) await r.arrayBuffer(); log({ k: 'tile', g, x, y, s: r.status, label }) })) } @@ -54,7 +56,7 @@ for (const a of acts) { if (a.door) { cl.GM.ws.send(JSON.stringify({ t: 'door', id: a.door[0], open: a.door[1] })); await sleep(40); continue } const m = await mv(a.id, a.p) if (m.ok === !!a.fail) { bad++; console.log('unexpected ack', a, m) } - if (a.id === 'A' && ++nA % 5 === 0) await sweep('walk'); else await sleep(15) + if (a.id === S && ++nA % 5 === 0) await sweep('walk'); else await sleep(15) } await sleep(200) for (const c of [gm, A, B]) c.ws.close() diff --git a/prototype-server-vision/tools/leakcheck.nim b/prototype-server-vision/tools/leakcheck.nim index f76f15c..f388371 100644 --- a/prototype-server-vision/tools/leakcheck.nim +++ b/prototype-server-vision/tools/leakcheck.nim @@ -1,8 +1,10 @@ ## Leak checker: replays run/leak-transcript.jsonl. Ground truth = GM socket (tokens+doors per gseq); -## A's vision/explored are recomputed with geometry.nim, never taken from A's own messages. -import std/[json, tables, sets, strutils] +## subject (SUBJECT env, A|B)'s vision/explored are recomputed with geometry.nim, never taken from A's own messages. +import std/[json, tables, sets, strutils, os, math] import geometry +let me = getEnv("SUBJECT", "A") # subject whose view is checked; the other player is noise +let other = if me == "A": "B" else: "A" type Side = enum Out, Amb, In var fails = 0 proc fail(s: string) = @@ -33,8 +35,8 @@ for line in lines("run/leak-transcript.jsonl"): let j = parseJson(line) if j["k"].getStr == "tile": tiles.add j elif j["who"].getStr == "GM": gm.add j["m"] - elif j["who"].getStr == "A": am.add j["m"] - # B transcript is deliberately unused: B is only noise + elif j["who"].getStr == me: am.add j["m"] + # the other player's transcript is deliberately unused: it is only noise let radius = parseFile("data/scene.json")["visionRadius"].getFloat var walls: seq[Wall] @@ -48,7 +50,13 @@ var seen: HashSet[string] proc finalize(g: int) = inc groups - let poly = visibilityPolygon(truth["A"], walls, radius) + let poly = visibilityPolygon(truth[me], walls, radius) + # independent oracle (segment tests, not the ray fan): clear straight line to a point in range => point is in the polygon + for i in 0 ..< 64: + for k in [0.3, 0.6, 0.95]: + let o = truth[me] + let p: Vec = (o.x + k * radius * cos(2 * PI * i.float / 64), o.y + k * radius * sin(2 * PI * i.float / 64)) + if not moveBlocked(walls, o, p) and side(poly, p) == Out: fail "polygon misses clear-LOS point " & $p & " at g=" & $g for (ix, iy) in cellsTouched(poly, cell.float): if ix >= 0 and iy >= 0 and ix < cw and iy < cw and (iy * cw + ix) notin firstG: firstG[iy * cw + ix] = g; inc expN @@ -60,16 +68,16 @@ proc finalize(g: int) = if t == "moveAck": continue scan(m, t) if m["gseq"].getInt != g: fail "A msg gseq " & $m["gseq"] & " not in GM log (group " & $g & ")" - if t notin ["token", "remove"] and "\"B\"" in $m: fail "B mentioned in " & $m + if t notin ["token", "remove"] and "\"" & other & "\"" in $m: fail other & " mentioned in " & $m case t - of "hello": (if m["tokens"] != %["A"]: fail "hello tokens " & $m["tokens"]) + of "hello": (if m["tokens"] != %[me]: fail "hello tokens " & $m["tokens"]) of "token": let id = m["id"].getStr if id notin truth: fail "unknown token " & id; continue if vec(m["pos"]) != truth[id]: fail "token " & id & " pos != truth at g=" & $g - if m["mine"].getBool != (id == "A"): fail "mine flag " & id - if id != "B" and m{"by"}.getStr == "B": fail "B leak via by on " & id - if id != "A" and side(poly, truth[id]) == Out: fail "LEAK token " & id & " outside A vision at g=" & $g + if m["mine"].getBool != (id == me): fail "mine flag " & id + if id != other and m{"by"}.getStr == other: fail other & " leak via by on " & id + if id != me and side(poly, truth[id]) == Out: fail "LEAK token " & id & " outside A vision at g=" & $g view[id] = vec(m["pos"]); seen.incl id of "remove": let id = m["id"].getStr @@ -91,8 +99,8 @@ proc finalize(g: int) = if aExp.len != expN: fail "explored count at g=" & $g & ": A " & $aExp.len & " expected " & $expN for id, p in truth: let s = side(poly, p) - if s == Amb and id != "A": continue - let want = id == "A" or s == In + if s == Amb and id != me: continue + let want = id == me or s == In if want != (id in view): fail "view mismatch " & id & " want=" & $want & " at g=" & $g elif want and view[id] != p: fail "view pos stale " & id & " at g=" & $g @@ -124,9 +132,9 @@ for t in tiles: if s != (if exp: 200 else: 403): fail "tile " & $t["x"] & "_" & $t["y"] & " g=" & $g & " (" & t["label"].getStr & ") status " & $s & " expected " & (if exp: "200" else: "403") # coverage: the scripted path must actually exercise enter/leave -for id in ["M1", "M2", "B", "M5"]: +for id in (if me == "A": @["M1", "M2", "B", "M5"] else: @["A", "M5"]): if id notin seen: fail "scenario never showed A token " & id -if removes < 3: fail "only " & $removes & " removes" +if removes < (if me == "A": 3 else: 1): fail "only " & $removes & " removes" echo "leakcheck: ", groups, " gseq groups, A msgs ", am.len, ", tokens seen ", seen.len - 1, " others, removes ", removes, ", tiles ", tiles.len, " (200:", n200, " 403:", n403, " other:", nOther, "), explored cells ", expN, ", failures ", fails quit(if fails > 0: 1 else: 0)