test(m4): finish audit, measurement and archive (#76)

- client/e2e/audit.test.mjs: independent player-browser traffic audit (own
  frame catalogue and banned-key list, never leakcheck.nim), with a
  -d:leakfault/otherVision negative control.
- client/e2e/fogbench.test.mjs + tools/fogbench.nim: 50 tokens on a
  16384 px map (server move path and headless SwiftShader cadence);
  results/m4-fog-bench.md records both honestly (SW is not the phone gate).
- Archive prototype-* under docs/archive/prototypes (Q24); DESIGN 13 M4
  done, TESTING 13 M4 accept result, HANDOFF-M4 close-out.
- mutation.sh c1/c2 patch the post-#65 tile-gate line again (they had
  silently become sed no-ops).
This commit is contained in:
2026-11-21 15:43:08 +01:00
parent fd711de152
commit fcd90529b6
142 changed files with 1111 additions and 242 deletions
+103
View File
@@ -0,0 +1,103 @@
## M4 fog benchmark (#76): the 50-token, 16384 px tile map measurement the finish ticket records
## in `results/m4-fog-bench.md`. `devbox run fogbench` compiles and runs it; the log lands in
## `results/m4-fog-bench.log`.
##
## Two numbers the phone gate needs to make sense of:
## 1. server recompute on a move: one player's vision refresh (one polygon, ~5k sparse walls,
## 16384 px map) plus the egress pass for that player with 50 visible tokens;
## 2. the client's headless frame time is measured separately by `client/e2e/fogbench.test.mjs`
## (SwiftShader, so it is a floor, never the phone number it is compared with).
##
## No timing code lives in server/: this harness times the real modules from outside.
import std/[json, monotimes, times, strformat, math, options, tables, strutils, sets]
import geometry
import characters, props, tokens, views, vision, world
import scenegen
var sink = 0
proc bench(name: string, n: int, f: proc(i: int)): float =
for i in 0 ..< 50: f(i) # warm-up
var sum, worst = 0.0
for i in 0 ..< n:
let t0 = getMonoTime(); f(i)
let ms = (getMonoTime() - t0).inNanoseconds.float / 1e6
sum += ms; worst = max(worst, ms)
echo &"{name}: n={n} avg={sum / n.float:.3f}ms worst={worst:.3f}ms"
sum / n.float
const World = 16384.0
const PlayerId = 2
## 50 tokens: the player's one sighted token at the map centre, 49 NPCs inside its radius. The
## cluster is the phone scene (50 tokens rendered with the fog on), so the wall generator's walls
## inside that box are removed: the sight cost still comes from the ~5k walls elsewhere.
proc build(): ref ContextStore =
var tokens: seq[TokenRec]
tokens.add TokenRec(id: "vision-token", scene: 1, kind: "pc", descriptiveName: "Ann",
actualName: "Ann", pos: (World / 2, World / 2), owner: PlayerId,
flags: %*{"vision": true}, visionRadius: some(300.0), radius: some(20.0),
props: newProps())
for i in 0 ..< 49:
let dx = float((i mod 7) - 3) * 28.0
let dy = float((i div 7) - 3) * 28.0
tokens.add TokenRec(id: &"npc-{i}", scene: 1, kind: "npc", descriptiveName: &"T{i}",
actualName: &"T{i}", pos: (World / 2 + dx, World / 2 + dy),
owner: GmControlled, flags: %*{}, props: newProps())
let centre = World / 2
var walls: seq[WallRec]
var id = 0
for w in genScene(World, 5000, 1):
let near = max(w.a.x, w.b.x) >= centre - 400 and min(w.a.x, w.b.x) <= centre + 400 and
max(w.a.y, w.b.y) >= centre - 400 and min(w.a.y, w.b.y) <= centre + 400
if near: continue
inc id
walls.add WallRec(id: id, a: w.a, b: w.b, thickness: 0, position: wpCenter,
blocksSight: true, blocksMovement: true, sightDir: sdBoth,
playerMarker: false, door: false, open: false, secret: false,
locked: false, height: none(float), label: none(string),
flags: newJObject())
result = (ref ContextStore)(scene: SceneRec(
id: 1, name: "Scene", w: World, h: World, tileSize: 32, cell: 32,
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1, nextWallId: id + 1,
extra: newJObject(), walls: walls), tokens: tokens)
for pid in 2 .. 7:
var pv: PlayerVision
pv.refresh(result.scene, result.tokens, pid)
result.visions[pid] = pv
result.encountered[pid] = initTable[string, characters.EncounteredEntry]()
let store = build()
echo &"== M4 fog bench: 16384 px map, cell 32 ({store.scene.cols}x{store.scene.rows} cells), " &
&"{store.tokens.len} tokens, {store.scene.walls.len} walls =="
# 1. A move refreshes the mover's player: one polygon + its explored cells (main.refreshPlayer).
let refreshAvg = bench("server vision refresh, one player (1 polygon, radius 300, ~5k walls)", 500, proc(i: int) =
store.visions[PlayerId].refresh(store.scene, store.tokens, PlayerId)
sink += store.visions[PlayerId].polys.len)
# 2. The same player's egress pass with all 50 tokens visible (fresh diff base = a reconnect).
let ctx = store.borrow
var firstKnown: Known
let frames = egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, firstKnown)
var kinds: seq[string]
for frame in frames: kinds.add $frame{"t"}
echo &"player egress frames with 50 visible tokens: {frames.len} frames, {kinds.join(\",\")}"
let egressAvg = bench("player egress pass, 50 visible tokens (fresh Known)", 300, proc(i: int) =
var known: Known
sink += egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, known).len)
# 3. The steady state after the connect snapshot: a Known that already holds every frame.
var warmKnown: Known
discard egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, warmKnown)
discard bench("player egress pass, warm Known (no changes)", 2000, proc(i: int) =
sink += egressFrames(Recipient(isGm: false, playerId: PlayerId), ctx, warmKnown).len)
echo &"explored snapshot size: {store.visions[PlayerId].explored.card} cells on the 16384 px map"
let budget = 16.7
echo &"TARGET move path: refresh + 50-token egress {refreshAvg + egressAvg:.3f} ms vs <= {budget} ms " &
&"(one 60 fps frame): {(if refreshAvg + egressAvg <= budget: \"PASS\" else: \"MISS\")}"
echo "(sink ", sink, ")"
quit(if refreshAvg + egressAvg <= budget: 0 else: 1)
+1 -1
View File
@@ -1,5 +1,5 @@
## One-off corpus extractor (dev-time only; tests never read prototype paths).
## usage: nim c -r tools/mkcorpus.nim prototype-server-vision/results/leak-A.jsonl prototype-server-vision/results/leak-B.jsonl > tests/fixtures/corpus.jsonl
## usage: nim c -r tools/mkcorpus.nim docs/archive/prototypes/server-vision/results/leak-A.jsonl docs/archive/prototypes/server-vision/results/leak-B.jsonl > tests/fixtures/corpus.jsonl
## Replays the GM socket (token positions, door states per gseq) and emits one line per player `vision` message:
## {"src":"A|B","g":gseq,"id":"A|B","pos":[x,y],"open":[door ids],"poly":[[x,y]..]} (poly = what the prototype server sent,
## computed by the old geometry.nim; "open" = ALL open doors, initial states included). The scene (walls, radius, cell) is tests/fixtures/scene.json.
+2 -2
View File
@@ -205,8 +205,8 @@ run e3 "a link patch keeps the token's own names" server/tokens.nim 's/^ if r
TILE_GATE="an unexplored tile is refused|a tile is allowed when any overlapping cell"
TILE_ZERO="leading zero tile coordinates"
TILE_RANGE="outside the grid is out of range"
run c1 "the tile gate ignores the explored set" server/tiles.nim 's/ if sc\.cellIndex(cx, cy) in explored: return true/ if true: return true/' "$TILE_GATE"
run c2 "the tile gate needs every overlapping cell" server/tiles.nim 's/ if sc\.cellIndex(cx, cy) in explored: return true/ if sc.cellIndex(cx, cy) notin explored: return false/' "$TILE_GATE"
run c1 "the tile gate ignores the explored set" server/tiles.nim 's/ if cy \* sc\.cols + cx in explored: return true/ if true: return true/' "$TILE_GATE"
run c2 "the tile gate needs every overlapping cell" server/tiles.nim 's/ if cy \* sc\.cols + cx in explored: return true/ if cy * sc.cols + cx notin explored: return false/' "$TILE_GATE"
run c3 "an out-of-grid tile coordinate is accepted" server/tiles.nim 's/^ x >= 0 and y >= 0 and x < tileCols(sc) and y < tileRows(sc)$/ x >= 0 and y >= 0/' "$TILE_RANGE"
run c4 "a leading-zero tile coordinate is canonical" server/tiles.nim "s/ if raw\[0\] == '0' and raw.len > 1: return false/ discard/" "$TILE_ZERO"
# #65: the in-memory session cache must drop a player's entry on revocation (server/sessions.nim)
+1 -1
View File
@@ -1,4 +1,4 @@
// Isolated server for the Node harnesses (ported from prototype-server-vision/tools/withserver.sh).
// Isolated server for the Node harnesses (ported from docs/archive/prototypes/server-vision/tools/withserver.sh).
// Own state dir, own port, never a live session: the server is the real binary, built from the
// working tree, and its stdout/stderr land in <stateDir>/server.log so a check can grep them.
import { spawn, spawnSync } from 'node:child_process'