Files
FFF_vtt_frozen/prototype-tile-leak

prototype-tile-leak (throwaway): DESIGN.md Q4 / H8 / T5, tile-edge leak

Question

DESIGN 3.3: a 256 px tile is served iff >=1 explored 32 px cell overlaps it, so up to 255x255 px of unexplored art reaches the browser (devtools). Which fix is cheapest: (A) server-side per-player masked 256 px tiles, or (B) tiles the size of an explored cell (32 px; 64 px for comparison) served as-is?

Fact, not prototyped: client-side fog, flattening or masking cannot fix this. The bytes are already in the browser (network tab, cache, decoded texture) before any client code runs. Only what the server sends counts.

Method

  • Art: procedural pure function of world coords (src/art.nim), so every tile size slices the same picture. Two noise levels bracket real art: g6 (smooth, 256 px PNG ~83 KB) and g40 (grainy, ~133 KB). Synthetic: absolute sizes/ms depend on real art, ratios are what matter.
  • A: src/maskbench.nim. Decode PNG, set unexplored 32 px cells to opaque black (0,0,0,255), encode. Median of 200 runs, single thread, -d:release. Then replay of the real explored transcripts of prototype-server-vision/results/leak-A.jsonl (subjects A: 94 explored msgs, 2142 cells; B: 39 msgs, 1114 cells) on the 4096 px / 256 px-tile / 32 px-cell scene: every tile whose mask changed is re-masked for real.
  • B: src/gen.nim slices the 4096 px map into 32/64/256 px PNG files, measures disk. src/authsrv.nim (mummy, same shape as the vision prototype tile route: strict path parse, cookie lookup, explored-cell range check under a lock, X-Accel-Redirect) behind nginx (4 workers, internal alias). Load by h2load (nghttp2) on loopback, tools/netbench.sh. Requests/step computed by replaying the same transcripts.
  • Machine: AMD Ryzen 7 7840U, 16 threads, NixOS, kernel 6.18, governor left on powersave (as in the other prototypes). Page cache hot (cold disk not measured). Run-to-run noise on req/s is roughly +-30%.
  • Lib check (pixie 6.1.0, nimPNG 0.3.6): APIs checked in source + context7 docs, not memory.

Library choice (A)

pixie 6.1.0 nimPNG 0.3.6
dec+mask+enc, 1..63 of 64 cells explored 1.6..10.9 ms 9.3..26.5 ms
output size vs pixie baseline 5-25% smaller (auto palette/gray, MINSUM filter); all-black tile 88 B vs 853 B
deps 7 pure-Nim pkgs (vmath chroma bumpy flatty zippy nimsimd crunchy), ~1.4 MB source 0, ~0.24 MB source
Pick pixie: 2-3x faster, decodePng/encodePng expose raw RGBA so masking is a plain loop; nimPNG only if bytes matter more than CPU. No C libs in either (devbox stays minimal). Pixie has no compression-level knob via encodePng (zippy default). [MEASURED results/maskbench.log]

Numbers

A. Masked 256 px tiles (MEASURED, results/maskbench.log)

Per tile, pixie, ms (out bytes), explored cells of 64:

cells g6 ms (B) g40 ms (B)
0 1.5 (853) 1.8 (853)
1 1.7 (3.2K) 1.9 (4.0K)
8 2.4 (12.7K) 2.5 (19.7K)
32 6.0 (43.6K) 5.5 (69.0K)
63 10.9 (82.2K) 9.5 (131.5K)
64 (all) 0: serve the original file, no image work 0
Decode alone 0.8-1.0 ms; encode dominates (8-10 ms for a full tile). Exact-polygon variant (naive per-pixel even-odd, 32-gon): 6.3-6.7 ms, an upper bound, a scanline fill would be about cell-mask cost.

Replay, subject A (B similar), 4096 px map, g6 / g40:

  • tiles touched 71 (of 256); 366 re-masks in 94 steps = 3.9 tiles/step avg (max 13, the connect burst); CPU 20 ms/step avg, worst 70 ms, per-step cost parallelisable over tiles, must run outside the state lock (vision recompute for comparison: 1.18 ms/move).
  • Bytes to client: masked 13.1 / 20.7 MB vs whole-tile 5.8 / 9.5 MB (each tile once) = x2.0-2.2, because every tile whose mask grows must be refetched (new URL/version per tile, client must be told which tiles changed: protocol + client change).
  • Per-player cache of final masked tiles: A 2.9 / 4.6 MB (71 tiles, ~41-65 KB each), B 1.5 / 2.4 MB (35 tiles). It scales with explored area, not map size. 16384 px map (4096 tiles) worst case if every tile were partial: <= the original art, 340-545 MB per player [INFERRED, = 4096 x tile size]. Old versions evictable.
  • Re-mask when explored grows: only tiles whose cell set changed (above), results keyed (tile, mask-hash) so reconnects/other connections of the same player are free.
  • Leak assert (MEASURED): for n in {0,1,8,32,63} cells, through pixie AND nimPNG, a 8x8 pure-red "secret" is planted in every unexplored cell; asserts: no red pixel in output, every unexplored pixel == (0,0,0,255), every explored pixel identical to source. Pass.

B. Cell-size tiles

Tile counts [arithmetic]: 4096 px map: 32 px 16,384 / 64 px 4,096 / 256 px 256. 16384 px map: 262,144 / 65,536 / 4,096. Disk, 4096 px map, MEASURED (results/disk4096.log); 16384 px = x16 [INFERRED]. "on-disk" = 4 KiB filesystem blocks (ext4):

tile files apparent g6 / g40 on-disk g6 / g40 16384 px apparent g6/g40 16384 px on-disk g6/g40
256 256 21.1 / 34.1 MB 21.2 / 33.8 MB 337 / 545 MB 339 / 541 MB
64 4,096 24.6 / 38.6 MB (+17/+13%) 32.9 / 49.3 MB 393 / 618 MB 526 / 788 MB
32 16,384 28.4 / 43.5 MB (+35/+28%) 66.0 / 66.0 MB 454 / 696 MB 1.06 GB (262k inodes)
Small PNGs lose compression context and pay ~60 B header + one 4 KiB block each; no image work at serve time.

Requests per exploration step, MEASURED by transcript replay (results/steps.log), NEW tiles per step (a cell tile is fetched once, never again):

connect burst steady median p95 max total (A, 94 steps)
32 px 292 11 37 164 2142
64 px 87 3 11 46 603
256 px whole (today) 13 0 3 6 71
256 px masked (refetch on change) - - - 13 366
Bytes for A: 32 px 3.7 / 5.7 MB, 64 px 3.6 / 5.7 MB (count x avg tile bytes, g6 / g40) vs whole-256 5.8 / 9.5 MB vs masked-256 13.1 / 20.7 MB. Cell tiles send LESS than today, masking sends 2x more.

Serving, MEASURED (results/netbench.log), loopback, 20000 reqs, nginx -> Nim auth -> X-Accel-Redirect, upstream keepalive on:

case req/s median / p99
32 px h1 c=1 (unloaded) 10.4k 90 us / 149 us
256 px h1 c=1 (unloaded) 7.2k 132 us / 211 us
32 px h1 c=6 (browser-like) 46k 122 us / 188 us
64 px h1 c=6 45k 127 us / 213 us
256 px h1 c=6 19k 296 us / 636 us (byte bound, ~83 KB each)
32 px h1 c=32 54k 480 us / 724 us
32 px STATIC (no Nim, no auth) h1 c=32 87k 332 us / 534 us
32 px h2 c=1 m=100 18.6k 5.1 ms / 8.9 ms (queued 100 deep)
32 px h2 c=4 m=100 19.9k 5.6 ms / 25 ms
burst 292 x 32 px, h1 c=6 7-16 ms wall (3 runs)
burst 292 x 32 px, h2 c=1 m=100 ~16-22 ms wall
upstream keepalive off, 32 px h1 c=32 55k same as on (loopback connect is cheap)
  • Nim auth hop costs ~35% throughput vs static nginx (54k vs 87k) and ~50 us unloaded, still 1000x the need (steady 11 req/step).
  • HTTP/2 on loopback: no gain (one h2 connection is served by one nginx worker, ~20k req/s; h1 c=32 spreads over 4 workers). Loopback has RTT ~0, so the real h2 benefit (no 6-connection limit) is not measurable here. INFERRED: burst of 292 over h1 with 6 conns = ~49 round trips, ~2.4 s at 50 ms RTT, vs ~1-2 RTT + transfer on h2. TLS not measured. Needs h2 in the real nginx (HTTPS deploy gets it with http2 on). Note nginx default keepalive_requests 1000 closes the h2 connection; h2load does not reconnect, hence keepalive_requests 1000000 in the bench conf (a browser reconnects transparently).
  • Not measured: client cost of 2142+ small Pixi sprites/textures (INFERRED fine with batching; the client can also blit received cell tiles into one RenderTexture per 256 px block).

C. Leak bound demo (MEASURED, devbox run netbench, first section; asserts exit 1 on mismatch)

Explored = left half + one lone cell (64,3) just outside it; every tile of each size requested; allowed count must be half + 1 (PASS all three).

tile unexplored px revealed by the lone cell's tile
32 px 0
64 px 3,072 (3 cells)
256 px (today) 64,512 (63 cells)
Masked 256 px: 0 beyond explored cells, asserted pixel-exact in A (above).

Comparison

256 whole (today) A masked 256 B1 32 px tiles B2 64 px tiles
leak beyond explored cells up to 63 cells/tile 0 0 <= 3 cells/tile
server CPU 0 ~20 ms per step (avg), 70 ms burst, pixie ~0.1 ms/req (auth) same
bytes per run (A) 5.8-9.5 MB 13.1-20.7 MB 3.7-5.7 MB 3.6-5.7 MB
requests per step (median/burst) 0 / 13 ~4 / 13 refetch 11 / 292 3 / 87
storage 4096 px 21-34 MB + per-player cache 1.5-4.6 MB 28-43 MB (66 MB on disk) 25-39 MB (33-49 on disk)
storage 16384 px 340-545 MB + per-player <= 340-545 MB worst 454-696 MB, 1.06 GB on disk, 262k files 393-618 MB, 526-788 MB on disk, 65k files
deps / new code none pixie (7 pkgs), mask+cache+versioned URLs+refetch protocol importer cuts 32 px tiles importer cuts 64 px tiles
HTTP/2 needed no no yes (INFERRED) helpful
cacheability immutable per-version immutable, never refetched same

Recommendation

B1: set tile_size = explored cell (32 px). DESIGN already requires tile_size % cell == 0, so this is a scene-config change plus importer slicing; no image library in the server, no per-player state, rule "tile allowed iff its cell is explored" becomes exact (zero beyond-cell leak, MEASURED assert C). Total bytes to the client go DOWN vs today; masking doubles them and costs ~20 ms CPU/step plus a refetch protocol for the same residual leak. Fall back to 64 px if 262k files / ~1 GB on-disk for a 16384 px map is unwelcome (leak <= 3 cells per allowed tile, 4x fewer files). Keep masking (pixie) only if tiles must stay large for another reason; do not build it for Q4 alone. Require h2 in the nginx config.

Residual leak (all options, incl. masked): granularity of fog = the explored cell. A cell the polygon only grazed is disclosed entirely (<= 32x32 px beyond what LOS actually saw), and once sent never revoked. Authoring rule shrinks from "~256 px around reachable areas" to "~32 px". Exact-polygon masking would close it but needs per-pixel explored state and ~same CPU as cell masking (6-7 ms/tile naive), not worth it. Also unchanged: static art only, doors not in tiles.

Caveats

Synthetic art (real painted maps compress differently; ratios hold, absolutes move). Loopback only: no RTT, no TLS, no real browser; h2 benefit INFERRED. Hot page cache. Single laptop in powersave. 16384 px figures are x16 extrapolations. One scene/one player load; no concurrency mix with WS traffic.

Run

cd prototype-tile-leak
devbox run setup       # nimble setup (fetches pixie, nimPNG, mummy)
devbox run build       # bin/maskbench bin/gen bin/authsrv
devbox run gen         # data/g6, data/g40 tiles (~20 s, ~0.3 GB, gitignored) + results/disk4096.log
devbox run maskbench   # results/maskbench.log (~30 s)
devbox run netbench    # leak-bound demo + h2load matrix -> results/netbench.log

results/steps.log: python3 tools/steps.py > results/steps.log (replays ../prototype-server-vision/results/leak-A.jsonl; first message = connect burst, excluded from steady stats).