feat(walls): one-way sight per-viewer filter and ONEWAY fault (#58)

The vision fan blocks a sight-blocking one-way wall only for a viewer on
its blind side (sight_dir names it; left is the (-dy,dx) normal of a->b),
one sign test inline after the radius cull, so the hoisted wall list stays
viewer-independent. Movement never reads the direction: one-way walls block
both ways. Explored and tiles therefore differ by side and only ever grow.

- geometry: WallDir + Wall.sightDir + blindTo, filter in visibilityPolygon.
- worldmap: sight_dir mapping (movement mapping unchanged).
- vision: the ONEWAY leak-fault build drops sight_dir player-side.
- leakcheck: own side test for the fan and the clear-segment oracle; ONEWAY
  class detected against the sight_dir-dropped fan.
- leak scenario gains a one-way wall the subject sees across; fault test
  asserts exactly ONEWAY.
- oneway.test.mjs: both sides, tile sweep with its own oracle, flip with
  fixed endpoints, monotone explored/tiles, restart, two-way movement.
- docs: DESIGN 5.8 mechanism sentence corrected; TESTING #58 markers built.
- nginx harness: temp dirs created recursively (restart on one state dir).
This commit is contained in:
2026-11-19 22:57:39 +01:00
parent 0f3aa2300f
commit 9fcbe885c1
14 changed files with 649 additions and 78 deletions
+137 -45
View File
@@ -26,8 +26,15 @@
## CONTROLLER_CHANGE a token changed controller in a group that asked for no such thing (8)
## CHAR_OWNER_PROP a character's owner bucket reached a player who does not own it (8)
## GEOMETRY the recomputed polygon disagrees with the clear-segment oracle (sanity)
## ONEWAY the subject's fan ignores `sight_dir` (the two-way fan; TESTING 10.3, #58)
## PROTOCOL a frame the catalogue does not allow, a bad `n` sequence, a missing hello block
##
## One-way sight (DESIGN 5.8, TESTING 10.3): the checker reads `sight_dir` from the GM's frames and
## applies its OWN side test (`blindTo`, the sign of the cross product with `(-dy, dx)`) to decide
## whether a one-way wall is in a viewer's fan. It never calls `server/vision.nim`. Once the
## subject's own `vision` frame proves the sight_dir-dropped fan (the ONEWAY fault), the oracles
## evaluate that two-way perspective so the fault is reported as exactly ONEWAY.
##
## usage: leakcheck <transcript.jsonl> <SUBJECT> exit 0 = clean
import std/[algorithm, json, math, options, os, sets, strutils, tables]
import geometry
@@ -87,6 +94,14 @@ proc fail(cls, s: string) =
proc vec(j: JsonNode): Vec = (j[0].getFloat, j[1].getFloat)
proc wallDir(j: JsonNode): WallDir =
## The GM's wall frames carry `sight_dir` (proto 2, #54); the checker reads it as its own input.
if j.kind != JString: return wdBoth
case j.getStr
of "left": wdLeft
of "right": wdRight
else: wdBoth
proc samePos(j: JsonNode, p: Vec): bool =
## JSON numbers lose their int/float spelling on the wire round trip: compare numerically.
j.kind == JArray and j.len == 2 and
@@ -127,12 +142,13 @@ proc applyGmWalls(truth: var Truth, ids: var seq[int], m: JsonNode) =
ids.setLen(0)
for w in m["walls"]:
truth.walls.add Wall(a: vec(w["a"]), b: vec(w["b"]),
door: w["door"].getBool, open: w["open"].getBool)
door: w["door"].getBool, open: w["open"].getBool,
sightDir: wallDir(w["sight_dir"]))
ids.add w["id"].getInt
of "wall":
let id = m["id"].getInt
let w = Wall(a: vec(m["a"]), b: vec(m["b"]), door: m["door"].getBool,
open: m["open"].getBool)
open: m["open"].getBool, sightDir: wallDir(m["sight_dir"]))
var found = false
for i in 0 ..< ids.len:
if ids[i] == id:
@@ -193,9 +209,39 @@ proc applyGmCharacters(truth: var Truth, m: JsonNode) =
# ---------------------------------------------------------------------------------------------------
# Independent oracles
var checkDir = true
## False once the subject's own `vision` frame shows `sight_dir` dropped (the ONEWAY fault): from
## then on the oracles evaluate the faulty perspective so the fault is reported as exactly ONEWAY,
## not as a cascade of WITHHELD/OWN_VIEW/EXPLORED.
proc blindTo(w: Wall, o: Vec): bool =
## The checker's OWN copy of the 5.8 side test, never `vision.nim`: cross(a->b, o - a) is the
## signed distance along the `(-dy, dx)` normal of `a -> b`, positive = left. On the drawn line
## both directions answer blind, so the predicate is never ambiguous.
if w.sightDir == wdBoth: return false
let s = cross(w.b - w.a, o - w.a)
case w.sightDir
of wdBoth: false
of wdLeft: s >= -Eps
of wdRight: s <= Eps
proc wallsFor(truth: Truth, o: Vec, honourDir: bool): seq[Wall] =
## The wall list one viewer's fan gets. With `honourDir`, a sight-blocking one-way wall is kept
## only for a viewer on its blind side (the checker's own side test); otherwise every `sight_dir`
## is dropped, which is exactly the ONEWAY fault's two-way wall list.
if honourDir:
for w in truth.walls:
if w.sightDir != wdBoth and not w.blindTo(o): continue
result.add w
else:
result = truth.walls
for w in result.mitems: w.sightDir = wdBoth
proc clearLine(walls: seq[Wall], o, p: Vec): bool =
## The clear-segment oracle: no blocking wall between o and p (independent of the ray fan).
## The clear-segment oracle: no blocking wall between o and p (independent of the ray fan). The
## one-way filter is the checker's own side test, for the sample's viewer `o`.
for w in walls:
if checkDir and w.sightDir != wdBoth and not w.blindTo(o): continue
if w.blocks and segsCross(o, p, w.a, w.b): return false
true
@@ -203,13 +249,41 @@ proc rangeOf(sceneDefault: float, t: TruthToken): float =
if t.hasVision and t.visionRadius.isSome and t.visionRadius.get > 0: t.visionRadius.get
else: sceneDefault
proc polygonsOf(truth: Truth, controller: int, sceneDefault: float): seq[(string, seq[Vec])] =
proc polygonsOf(truth: Truth, controller: int, sceneDefault: float,
honourDir = true): seq[(string, seq[Vec])] =
for id, t in truth.tokens:
if t.controller != controller or not t.hasVision: continue
result.add (id, visibilityPolygon(t.pos, truth.walls, rangeOf(sceneDefault, t)))
result.add (id, visibilityPolygon(t.pos, wallsFor(truth, t.pos, honourDir),
rangeOf(sceneDefault, t)))
# deterministic order: by token id (the server keeps creation order; the checker sorts)
result.sort(proc (a, b: (string, seq[Vec])): int = cmp(a[0], b[0]))
proc samePolySet(got, want: seq[(string, seq[Vec])], tol = 0.5): bool =
## Every polygon present with the same id, vertex count and vertices within `tol` px (the wire's
## integer rounding, 6.4). Used to tell the correct fan from the ONEWAY fault's two-way fan.
if got.len != want.len: return false
var used = newSeq[bool](want.len)
for (gid, gpts) in got:
var matched = false
for i in 0 ..< want.len:
if used[i] or want[i][0] != gid: continue
used[i] = true
matched = true
let wpts = want[i][1]
if gpts.len != wpts.len: return false
for k in 0 ..< gpts.len:
if abs(gpts[k].x - wpts[k].x) > tol + 1e-6 or
abs(gpts[k].y - wpts[k].y) > tol + 1e-6: return false
break
if not matched: return false
true
proc parsePolys(m: JsonNode): seq[(string, seq[Vec])] =
for p in m["polys"]:
var pts: seq[Vec]
for q in p["pts"]: pts.add vec(q)
result.add (p["id"].getStr, pts)
proc insideAny(polys: seq[(string, seq[Vec])], p: Vec): bool =
for (_, pts) in polys:
if inside(pts, p): return true
@@ -322,14 +396,31 @@ proc main() =
if me == 0: continue
let polys = polygonsOf(truth, me, sceneDefault)
let otherPolys = if other != 0: polygonsOf(truth, other, sceneDefault) else: @[]
var anyOneWay = false
for w in truth.walls:
if w.sightDir != wdBoth: anyOneWay = true
# The fault's fan is only worth recomputing in a world that has a one-way wall at all.
let faultPolys = if anyOneWay: polygonsOf(truth, me, sceneDefault, honourDir = false)
else: polys
# The subject's own `vision` frame decides the perspective this group is checked against: if it
# matches the sight_dir-dropped fan and not the correct one, the ONEWAY fault is live. Then the
# oracles evaluate that perspective, so the fault surfaces as exactly ONEWAY instead of a
# cascade of WITHHELD/OWN_VIEW/EXPLORED.
if anyOneWay:
for m in g.subjMsgs:
if m["t"].getStr != "vision": continue
let got = parsePolys(m)
if not samePolySet(got, polys) and samePolySet(got, faultPolys): checkDir = false
let polysChecks = if checkDir: polys else: faultPolys
let otherPolys = if other != 0: polygonsOf(truth, other, sceneDefault, honourDir = checkDir)
else: @[]
# The encountered oracle, computed from the GM's truth and the subject's own polygons - never
# from what the subject was sent: every token the subject can see right now is one it may have
# met. It only grows, exactly like the server's list (8).
var visibleNow = initHashSet[string]()
for id, actual in truth.tokens:
if actual.controller == me or (not actual.gmOnly and insideAny(polys, actual.pos)):
if actual.controller == me or (not actual.gmOnly and insideAny(polysChecks, actual.pos)):
visibleNow.incl id
encounterable.incl id
@@ -441,9 +532,9 @@ proc main() =
if got != want:
fail "CHAR_OWNER_PROP", "token " & id & " cprops public bucket is not the truth"
view[id] = m
let visible = wantMine or (not actual.gmOnly and insideAny(polys, actual.pos))
let visible = wantMine or (not actual.gmOnly and insideAny(polysChecks, actual.pos))
if not visible:
let cls = if actual.gmOnly and insideAny(polys, actual.pos): "GMONLY_TOKEN"
let cls = if actual.gmOnly and insideAny(polysChecks, actual.pos): "GMONLY_TOKEN"
else: "HIDDEN_TOKEN"
leaked[id] = cls
fail cls, "token " & id & " sent but not visible (group " & g.label & ")"
@@ -458,7 +549,7 @@ proc main() =
inc removes
if id in truth.tokens:
let actual = truth.tokens[id]
if actual.controller == me or (not actual.gmOnly and insideAny(polys, actual.pos)):
if actual.controller == me or (not actual.gmOnly and insideAny(polysChecks, actual.pos)):
fail "WITHHELD", "token_gone for a token still visible: " & id
of "character":
# 6.1: a player gets only the characters it owns, with both names and the public/owner
@@ -513,36 +604,37 @@ proc main() =
if pl["name"].kind == JNull or pl["name"].getStr.len == 0:
fail "PROTOCOL", "the roster handed a player an unnamed entry"
of "vision":
var got: seq[(string, seq[Vec])]
for p in m["polys"]:
var pts: seq[Vec]
for q in p["pts"]: pts.add vec(q)
got.add (p["id"].getStr, pts)
let want = polys
var haveIds = initHashSet[string]()
for (id, pts) in got:
haveIds.incl id
var matched = false
for (wid, wpts) in want:
if wid != id: continue
matched = true
if pts.len != wpts.len:
fail "OWN_VIEW", "polygon " & id & " has " & $pts.len & " of " & $wpts.len & " vertices"
else:
for i in 0 ..< pts.len:
# integer px on the wire (6.4): 0.5 px is the documented tolerance
if abs(pts[i].x - wpts[i].x) > 0.5 + 1e-6 or abs(pts[i].y - wpts[i].y) > 0.5 + 1e-6:
fail "OWN_VIEW", "polygon " & id & " vertex " & $i & " is " & $pts[i] &
" not " & $wpts[i]
break
if not matched:
var theirs = false
for (oid, _) in otherPolys:
if oid == id: theirs = true
fail (if theirs: "OTHER_PLAYER" else: "PROTOCOL"),
"vision carries a polygon for a token this player does not control: " & id
for (id, _) in want:
if id notin haveIds: fail "OWN_VIEW", "vision is missing the polygon of " & id
let got = parsePolys(m)
if not samePolySet(got, polys):
if anyOneWay and samePolySet(got, faultPolys):
# TESTING 10.3: the fan behaves as if `sight_dir` were dropped (a two-way wall).
fail "ONEWAY", "vision is the sight_dir-dropped fan (group " & g.label & ")"
else:
let want = polys
var haveIds = initHashSet[string]()
for (id, pts) in got:
haveIds.incl id
var matched = false
for (wid, wpts) in want:
if wid != id: continue
matched = true
if pts.len != wpts.len:
fail "OWN_VIEW", "polygon " & id & " has " & $pts.len & " of " & $wpts.len & " vertices"
else:
for i in 0 ..< pts.len:
# integer px on the wire (6.4): 0.5 px is the documented tolerance
if abs(pts[i].x - wpts[i].x) > 0.5 + 1e-6 or abs(pts[i].y - wpts[i].y) > 0.5 + 1e-6:
fail "OWN_VIEW", "polygon " & id & " vertex " & $i & " is " & $pts[i] &
" not " & $wpts[i]
break
if not matched:
var theirs = false
for (oid, _) in otherPolys:
if oid == id: theirs = true
fail (if theirs: "OTHER_PLAYER" else: "PROTOCOL"),
"vision carries a polygon for a token this player does not control: " & id
for (id, _) in want:
if id notin haveIds: fail "OWN_VIEW", "vision is missing the polygon of " & id
of "explored":
if m["full"].getBool:
# a resync snapshot replaces the set; every cell must be one the recomputation has seen
@@ -593,7 +685,7 @@ proc main() =
# was legitimately seen and is now invisible is a missing withdrawal. `seenAny` decides,
# so a `gm_only` flip after a legitimate sighting is not re-labelled as a gm_only leak.
if leaked.hasKey(id): fail leaked[id], "leaked token still in view: " & id
elif id notin seenAny and actual.gmOnly and insideAny(polys, actual.pos):
elif id notin seenAny and actual.gmOnly and insideAny(polysChecks, actual.pos):
fail "GMONLY_TOKEN", "gm_only token still in view: " & id
else: fail "NO_REMOVE", "token left the view and was never withdrawn: " & id &
" (group " & g.label & ")"
@@ -605,7 +697,7 @@ proc main() =
var recomputed: HashSet[int]
let cols = int(ceil(sceneW / float(cell)))
let rows = int(ceil(sceneW / float(cell)))
for (_, pts) in polys:
for (_, pts) in polysChecks:
for (cx, cy) in cellsTouched(pts, float(cell)):
# cells outside the world are not cells: the server drops them too (vision.refresh)
if cx < 0 or cy < 0 or cx >= cols or cy >= rows: continue
@@ -617,7 +709,7 @@ proc main() =
if k in recomputedSoFar: continue
let cx = k mod cols
let cy = k div cols
if definitelySeen(truth.walls, polys, sceneW, cell, cx, cy):
if definitelySeen(truth.walls, polysChecks, sceneW, cell, cx, cy):
recomputed.incl k # grazed cell the sampler does see: fine either way
continue
# attribution: a cell only the other player has explored is a presence leak, not a bad cell
@@ -637,7 +729,7 @@ proc main() =
if missing > 3:
fail "EXPLORED", $missing & " recomputed explored cells were never sent"
if getEnv("LEAK_DEBUG") != "":
for (id, pts) in polys:
for (id, pts) in polysChecks:
var cx0 = 100000; var cx1 = -1; var cy0 = 100000; var cy1 = -1
for (cx, cy) in cellsTouched(pts, float(cell)):
cx0 = min(cx0, cx); cx1 = max(cx1, cx); cy0 = min(cy0, cy); cy1 = max(cy1, cy)
@@ -645,7 +737,7 @@ proc main() =
" group ", g.label, " cell ", cell, " sceneW ", sceneW
# --- the clear-segment oracle on the polygon itself -----------------------------------------
for (id, pts) in polys:
for (id, pts) in polysChecks:
if pts.len < 3: continue
var t: TruthToken
for tid, tt in truth.tokens: