fix(client): tile loading: centre-out, one request per tile, 404 recovery (#86)

The GM survey view asked row-major, at most 8 at a time, rebuilt the whole
wanted window on every settle, and paid two requests for every failed tile
(image error, then a fetch to learn the status). A 404/5xx was stored as
permanent `denied`, so a tile imported after page load stayed black until a
page reload, and `viewport.setScene` swallowed a same-id art_version change
so the old textures were never dropped.

- TILE_CONCURRENCY 8 -> 32, the measured knee on a Full HD scene (min-of-3
  headless: GM fit + player 0.5, tile 32/64; 64 is inside the noise).
- orderTilesCentreOut: nearest the viewport centre first.
- Settle path is incremental (one settle = one tile sprite/flag update);
  the window is rebuilt only on camera/scene changes.
- fetch + createImageBitmap: status from the same response, decode off the
  main thread, so a failure costs one request.
- 403 stays permanent (retried only on fog.fresh); 404/5xx/network retry
  with 250/500 ms backoff up to 3 loads, and `hello`/reconnect or a
  scene/art_version change resets the budget. A failure that lands after a
  reconnect is a fresh first attempt; a failure that leaves the window does
  not spend the budget.
- setScene emits for a same-id scene; a new art_version drops the texture
  cache and reschedules. Tiles are `Cache-Control: private, max-age=1y,
  immutable` (still gated), so a reload reuses them; DESIGN 3.3/11.1 and
  client/README updated.

Tests: vitest for ordering, incremental invariants, retry budget/backoff,
reconnect epoch and art_version invalidation; e2e for a GM survey fill (one
request per succeeded tile) and a 404 tile recovering after the file appears
and a real reconnect. Before -> after (min-of-3, saturated host, Full HD):
GM fit 32px 910->843 ms, player 0.5 32px 972->862 ms, GM fit 64px
320->249 ms, player 0.5 64px 332->368 ms (noise).

Part of #63. Blocks #77.
This commit is contained in:
2026-11-21 22:28:18 +01:00
parent 54dbd68587
commit 52ab50d765
11 changed files with 676 additions and 134 deletions
+20 -16
View File
@@ -129,22 +129,26 @@ change them only if a slice needs a new *core* event, and say so in the PR.
and `data-view` (viewport px).
- **Tiles** (`src/tiles.ts` scheduler + `src/render/modules/tiles.ts` layer): the wanted set is
`visibleTiles()` — every tile intersecting the viewport plus exactly one tile of margin, clamped
to the scene, in viewport order (row-major). The scheduler keeps at most `TILE_CONCURRENCY` (8)
loads in flight, drops queued tiles that leave the window, never asks twice for a tile, and
remembers a `403`/`404` as `denied` forever: the gate has said "not yours", so it is never retried
in a loop. A transient failure is retried only on a new window or after a reconnect (`hello`).
The decoded-texture cache is module-level and keyed by URL, so a reconnect keeps every tile and
the page makes no new tile request. `data-tiles` is `ready` once the current pass is complete:
every wanted tile has a terminal answer (`ok`, `denied`, or a stored transient `error` that waits
for a new window or a reconnect). A transient `error` is deliberately not pending, otherwise one
host hiccup would hold the flag at `loading` and hang every e2e readiness wait; the hole it leaves
fills on the next window or reconnect. `data-tile-requests` counts image attempts (plus the single
status fetch used to classify an image error); `data-tile-distinct` counts the distinct tile URLs
fetched, so the #70 re-ask of a tile whose 403 became allowed shows in the former, not the latter.
- **Why `<img>` and not `fetch` + `createImageBitmap`:** the browser's image loader does the
fetch/decode off the JS hot path; one `onload` per tile is the cheapest way to put the ~480 tiles
of a phone viewport on the GPU. A failed image is classified once with `fetch`, which is where the
`403` distinction comes from.
to the scene — ordered centre-out (`orderTilesCentreOut`), so a survey fill grows from what the
eye is on instead of the top-left corner. The scheduler keeps at most `TILE_CONCURRENCY` (32,
the measured knee on a Full HD scene) loads in flight, drops queued tiles that leave the window,
never asks twice for a tile, and remembers a `403` as `denied`: the gate has said "not yours", so
it is not retried in a loop (it is asked once more when `fog.fresh` says its cells were explored).
A `404`/`5xx`/network failure is transient: up to 3 loads with 250/500 ms backoff, and a
reconnect (`hello`) or a scene/`art_version` change resets the budget and re-asks. The settle path
is incremental (one settle = one tile), so filling tens of thousands of tiles is not quadratic.
The decoded-texture cache is module-level and keyed by URL, so a reconnect keeps every tile; a new
`art_version` changes every URL, so it drops the cache and refetches. `data-tiles` is `ready` once
the current pass is complete: every wanted tile has a terminal answer (`ok`, `denied`, or a
stored transient `error` that waits for a retry, a new window or a reconnect). A transient
`error` is deliberately not pending, otherwise one host hiccup would hold the flag at `loading`
and hang every e2e readiness wait. `data-tile-requests` counts fetches; `data-tile-distinct`
counts the distinct tile URLs fetched, so the #70 re-ask of a tile whose 403 became allowed shows
in the former, not the latter.
- **One request per tile:** `fetch` then `createImageBitmap` (decode off the main thread). An
`<img>` error carries no status, so classifying it cost a second `fetch` for *every* failure; the
fetch-first path gets 403 vs 404/5xx from the same response and lets the scheduler retry a
missing tile instead of leaving a permanent hole.
- **Grid** (`src/render/modules/grid.ts`): one `TilingSprite` over a `grid_size` cell texture, drawn
once, toggled by `#grid-toggle` (story 18). No per-frame redraw.
- **Tile-size gate (a)** (`client/e2e/map.test.mjs`): mobile emulation 412×869 CSS px, DPR 2.625,