GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Kuiper, cosmos and drawing engines / galaxies-wafers / iterations/30-journey

Iterations — 30 Journey

A JavaScript module in Galaxies Wafers that draws lines meta, families, crossname and substations, using SHA-256. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/30-journey/.

700 lines of JavaScript from galaxies-wafers, in 1 file. Topic: Kuiper, cosmos and drawing engines. Section 1201 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Iterations — 30 Journey render · galaxies-wafers / iterations/30-journey/app.mjs lines 1–700

Iterations — 30 Journey

A JavaScript module in Galaxies Wafers that draws lines meta, families, crossname and substations, using SHA-256. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/30-journey/.

rendergalaxies-wafers / iterations/30-journey/app.mjs · lines 1–700 of 1165 · Entry point · pattern visibleBand (shared by 2 files)

What it does
It draws lines meta, families, crossname and substations. Defines 60 functions or classes, including levelOf, $, enqueue, pump, fetchWithTimeout. Side effects: changes the web page (DOM); makes network requests.
Why it exists in the grid workflow
Domain stage sub33kv_design (voltage levels 11kV (all 33kV or below)): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: substations, 11kV. README: "The page (`index.html`, `app.mjs`, `lib.mjs`) is inherited unchanged from testcode/202609151339."
Inputs & outputs
In: JSON file index.json; JSON file all-lines.meta.json; JSON file families.json; JSON file crossname.json. Out: canvas drawing. Voltage levels named: 11kV.
Downstream impact
Used directly by 1 file: Iterations — 30 Journey Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/30-journey/). It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/30-journey/.
What it achieves
It fetches timeout (`fetchWithTimeout`), fetches JSON (`fetchJSON`) and fetches bin (`fetchBin`). Journey: the Line Wafer read by level of detail, and by following one thread. Its code works with 11 kV. It responds to pointerdown, pointermove and pointerup events.
Why it matters
Loaded by the page of the same name at https://ventusltd.github.io/galaxies-wafers/iterations/30-journey/.
Application
galaxies-wafers · iterations/30-journey · JavaScript module
Uses
Galaxies Wafers — Lib (import)
Used by
loaded by Iterations — 30 Journey Page (script src)
Produces
levelOf, $, enqueue, pump, fetchWithTimeout, fetchJSON, fetchBin, tilesToggle, wantTiles, paintTileRow, loadModule, scheduleHubs
Defines here
levelOf, $, enqueue, pump, fetchWithTimeout, fetchJSON, fetchBin, tilesToggle, wantTiles, paintTileRow, loadModule, scheduleHubs, woken, paintLoadRow, bin, tier1, tier2, buildDensity, sx, sy, resize, homeZoom, home, flyTo, lowerBound, visibleBand, draw, strokeXY, legsInto, streamRoutes
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/30-journey/app.mjs:1-700 · blob de1694499db2cbdbb106f480f7c683231b67c0bd · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unknown
/* Journey: the Line Wafer read by level of detail, and by following one thread.
 *
 * Started from iteration 21 (dark pixels). What changed is the whole drawing
 * model: 21 hands every one of the numbered lines to the GPU every frame. This
 * page never does. It draws only what the current level of detail needs, the
 * way a map gates its 11 kV lines behind a zoom and a streaming game only
 * builds what is near the camera:
 *
 *   GALAXY   no individual lines. A density image of the substrate, built once
 *            at load into a small offscreen canvas, and each woken module route
 *            as one thick simplified trunk through its functions' first lines.
 *   SYSTEM   module routes as cased lines, the first line of each function as a
 *            white dot (tap for its card), labels for the largest routes, and
 *            shared lines as substation symbols with their count.
 *   PLANET   individual lines, but only those whose key lies in the radius band
 *            the screen can see. The band comes from the placement law itself:
 *            r = S * sqrt(key), so a screen whose nearest and farthest points
 *            from the centre are r0 and r1 can only show keys in
 *            [(r0/S)^2, (r1/S)^2]. Those keys are found by binary search.
 *   SURFACE  the same band, larger, and every line can be tapped for its card.
 *
 * The level, the band and the number of keys drawn are printed every frame,
 * counted, never typed.
 *
 * FOLLOW. Pick a thread (a module route, a function inside it, or a chain of
 * functions copied under other names) and the camera drives it stop by stop.
 * The strip at the bottom redraws the same thread as a single straight line
 * with its stops as busbars, the way a single-line diagram untangles a
 * network; a stop that other threads also pass through is drawn as a
 * substation symbol with its count, not as crossing lines.
 *
 * WHAT IS NOT CLAIMED. A route is a function's lines in numbering order and a
 * junction is one numbered line carried by more than one route. Neither is a
 * call graph or a dependency. The single-line-diagram drawing is a way of
 * reading code; it is not an electrical diagram and shows no electrical result.
 */

import { place, placeAll, indexOfKey, esc, fmt, SPACING } from '../../lib.mjs';

const DATA = 'https://globalgrid2050.com/testcode/202609142202/data/';
const MANIFEST = '../../layers/manifest.json';
const S = SPACING;

const LEVELS = [
  { id: 'GALAXY',  min: 0,  go: 0 },
  { id: 'SYSTEM',  min: 1,  go: 1.8 },
  { id: 'PLANET',  min: 5,  go: 9 },
  { id: 'SURFACE', min: 28, go: 48 }
];
const levelOf = z => z >= LEVELS[3].min ? 3 : z >= LEVELS[2].min ? 2 : z >= LEVELS[1].min ? 1 : 0;
const DRAW_CAP = 60000;   /* a hard ceiling on points per frame; reaching it is printed */
const BANDS = 10;         /* radius bands used by the pattern questions: tenths of the wafer radius */

const $ = id => document.getElementById(id);
const stage = $('stage');
const ctx = stage.getContext('2d', { alpha: false });

const U = { keys: null, lens: null, inFam: null, pos: null, n: 0, meta: null, maxR: 0,
            families: null, famLines: null, famCount: null, tier2: 'WAIT' };
const view = { x: 0, y: 0, zoom: 1, w: 0, h: 0, dpr: 1 };
const stats = { frames: [], last: null, stopsVisited: 0 };
const F = { thread: null, i: 0, keySet: null, cross: null };
window.__journey = { stats, get view() { return { ...view }; },
  get follow() { return F.thread ? { id: F.thread.id, stop: F.i, stops: F.thread.stops.length, key: F.thread.stops[F.i].key } : null; } };

/* ── modules: the manifest, a bounded queue, and what each key is part of ─── */

const MODS = [];                 /* manifest rows for module layers, in manifest order */
const MOD = new Map();           /* id -> row; row.state WAIT LOAD OK EMPTY FAIL; row.data when loaded */
const keyMods = new Map();       /* key -> Map(moduleId -> function name) */
let hubs = [];                   /* [key, count] where two or more woken routes share a line */

/* Loading, as Grid Atlas does it (repd_grid_atlasv8 ventus-corev8engine.js):
   one fetch queue for all network work, a timeout on every fetch, one shared
   promise per URL that is dropped again on failure so a retry is possible, and
   a hydrate that returns at once when a thing is loaded or loading. The Atlas
   queue runs four at a time; the wafer's builder rules cap it at three. */
const QUEUE_MAX = 3, FETCH_TIMEOUT_MS = 15000;
const Q = []; let active = 0;
function enqueue(task) {
  return new Promise((res, rej) => { Q.push({ task, res, rej }); pump(); });
}
function pump() {
  while (active < QUEUE_MAX && Q.length) {
    const j = Q.shift(); active++;
    j.task().then(j.res, j.rej).finally(() => { active--; pump(); });
  }
}
async function fetchWithTimeout(url) {
  const ctl = new AbortController(), t = setTimeout(() => ctl.abort(), FETCH_TIMEOUT_MS);
  try {
    const r = await fetch(url, { signal: ctl.signal });
    if (!r.ok) throw new Error(`HTTP ${r.status} for ${url.replace(/^.*\/(layers|data)\//, '$1/')}`);
    return r;
  } catch (e) {
    throw e.name === 'AbortError' ? new Error(`timed out after ${FETCH_TIMEOUT_MS / 1000} s: ${url.replace(/^.*\//, '')}`) : e;
  } finally { clearTimeout(t); }
}
const urlCache = new Map();
function fetchJSON(url) {
  if (urlCache.has(url)) return urlCache.get(url);
  const p = enqueue(() => fetchWithTimeout(url).then(r => r.json()))
    .catch(e => { urlCache.delete(url); throw e; });
  urlCache.set(url, p);
  return p;
}
const fetchBin = (url, Kind) => enqueue(() => fetchWithTimeout(url).then(r => r.arrayBuffer())).then(b => new Kind(b));

/* ── copied lines, tiled by radius band: fetched only when the view reaches them ──
   layers/tiles/copying splits the copying layer by the law band = isqrt(key) // S,
   so band b holds keys [(b*S)^2, ((b+1)*S)^2). A band is fetched only when the
   view is at PLANET or closer AND the visible key band intersects it. Loaded
   bands are held up to TILE_FEATURE_CAP features; past that, the band whose
   centre is farthest from the view's radius is evicted and its URL forgotten,
   so it would be fetched again if the reader comes back. */
const TILE_INDEX = '../../layers/tiles/copying/index.json';
const TILE_FEATURE_CAP = 12000;
const TILES = { on: false, index: null, state: 'OFF', bands: new Map(), held: 0, fetched: 0, evicted: 0, withheld: 0, why: '' };

async function tilesToggle() {
  TILES.on = !TILES.on;
  $('copies').classList.toggle('on', TILES.on);
  if (TILES.on && !TILES.index) {
    TILES.state = 'LOAD'; paintTileRow();
    try {
      TILES.index = await fetchJSON(TILE_INDEX);
      TILES.state = 'OK';
    } catch (e) { TILES.state = 'FAIL'; TILES.why = e.message; TILES.on = false; $('copies').classList.remove('on'); }
  }
  paintTileRow(); draw();
}

function wantTiles(band) {
  if (!TILES.on || !TILES.index) return;
  const S2 = TILES.index.scheme.S, rMid = (band.r0 + band.r1) / 2;
  const want = TILES.index.bands.filter(b => b.key_max > band.k0 && b.key_min <= band.k1)
    .sort((a, b) => Math.abs((a.band + .5) * S2 - rMid) - Math.abs((b.band + .5) * S2 - rMid));
  const wantSet = new Set(want.map(b => b.band));
  TILES.withheld = 0;
  for (const b of want) {
    if (TILES.bands.has(b.band)) continue;             /* loaded or loading: never fetched twice */
    /* make room by evicting bands the view no longer needs, farthest first */
    while (TILES.held + b.features > TILE_FEATURE_CAP) {
      let far = null, farD = -1;
      for (const [id, x] of TILES.bands) {
        if (wantSet.has(id) || x.state !== 'OK') continue;
        const d = Math.abs((id + .5) * S2 - rMid);
        if (d > farD) { farD = d; far = id; }
      }
      if (far === null) break;
      const x = TILES.bands.get(far);
      TILES.held -= x.features; TILES.bands.delete(far); urlCache.delete('../../' + x.file); TILES.evicted++;
    }
    if (TILES.held + b.features > TILE_FEATURE_CAP) { TILES.withheld++; continue; }
    const rec = { state: 'LOAD', features: b.features, file: b.file, keys: null, xy: null };
    TILES.bands.set(b.band, rec); TILES.held += b.features;
    fetchJSON('../../' + b.file).then(doc => {
      urlCache.delete('../../' + b.file);             /* the typed copy is what is held, not the JSON */
      if (TILES.bands.get(b.band) !== rec) return;     /* evicted while in flight */
      if (doc.features.length !== b.features) throw new Error(`band ${b.band} holds ${doc.features.length} features, index says ${b.features}`);
      rec.keys = Uint32Array.from(doc.features, f => f.geometry.key);
      rec.xy = placeAll(rec.keys);
      rec.state = 'OK'; TILES.fetched++;
      paintTileRow(); draw();
    }).catch(e => {
      if (TILES.bands.get(b.band) === rec) { TILES.bands.delete(b.band); TILES.held -= b.features; }
      TILES.state = 'FAIL'; TILES.why = e.message; paintTileRow();
    });
  }
  paintTileRow();
}

let tileRowText = '';
function paintTileRow() {
  let t;
  if (TILES.state === 'OFF') t = '';
  else if (TILES.state === 'FAIL') t = `copied lines FAIL: ${TILES.why}`;
  else if (!TILES.on) t = `copied lines hidden · ${fmt(TILES.bands.size)} bands still held`;
  else if (!TILES.index) t = 'copied lines LOAD index';
  else {
    let loading = 0;
    for (const x of TILES.bands.values()) if (x.state === 'LOAD') loading++;
    t = `copied lines: ${fmt(TILES.bands.size - loading)} of ${fmt(TILES.index.bands.length)} bands held (${fmt(TILES.held)} of cap ${fmt(TILE_FEATURE_CAP)} features)`
      + (loading ? ` · LOAD ${fmt(loading)}` : '') + ` · fetched ${fmt(TILES.fetched)} · evicted ${fmt(TILES.evicted)}`
      + (TILES.withheld ? ` · ${fmt(TILES.withheld)} withheld by the cap` : '')
      + (levelOf(view.zoom) < 2 ? ' · drawn from PLANET inward' : '');
  }
  if (t !== tileRowText) { tileRowText = t; $('tilerow').textContent = t; }
}

let loadRowTimer = 0;
function loadModule(id) {
  const row = MOD.get(id);
  if (!row) return Promise.reject(new Error('EMPTY: no module layer is named ' + id));
  if (row.promise) return row.promise;
  row.state = 'LOAD'; paintLoadRow();
  row.promise = fetchJSON('../../' + row.file).then(fc => {
    const routes = [], firsts = [];
    let lines = 0;
    for (const f of fc.features || []) {
      const p = f.properties || {};
      if (f.geometry?.type === 'LineString') {
        const keys = Uint32Array.from(f.geometry.keys || []);
        routes.push({ fn: p.function, family: p.family, lines: p.lines, keys, xy: placeAll(keys) });
        lines += keys.length;
        for (const k of keys) {
          let m = keyMods.get(k);
          if (!m) keyMods.set(k, m = new Map());
          if (!m.has(id)) m.set(id, p.function);
        }
      } else if (f.geometry?.type === 'Point') {
        const [x, y] = place(f.geometry.key);
        firsts.push({ key: f.geometry.key, fn: p.function, family: p.family, lines: p.lines, x, y });
      }
    }
    firsts.sort((a, b) => a.key - b.key);
    const trunk = new Float32Array(firsts.length * 2);
    firsts.forEach((s, i) => { trunk[i * 2] = s.x; trunk[i * 2 + 1] = s.y; });
    row.data = { routes, firsts, lines, trunk, stats: fc.stats || {} };
    row.state = firsts.length ? 'OK' : 'EMPTY';
    if (!firsts.length) row.why = 'the layer file holds no first-line points';
    scheduleHubs(); paintLoadRow(); draw();
    return row;
  }, e => { row.state = 'FAIL'; row.why = e.message; row.promise = null; paintLoadRow(); throw e; });
  return row.promise;
}

let hubTimer = 0;
function scheduleHubs() {
  clearTimeout(hubTimer);
  hubTimer = setTimeout(() => {
    hubs = [];
    for (const [k, m] of keyMods) if (m.size >= 2) hubs.push([k, m.size]);
    hubs.sort((a, b) => b[1] - a[1]);
    if (F.thread) { paintStop(); paintStrip(); }
    draw();
  }, 80);
}
const woken = () => MODS.filter(r => r.state === 'OK' || r.state === 'EMPTY');

/* one row, updated in place, throttled so a burst of loads rewrites it once */
function paintLoadRow() {
  if (loadRowTimer) return;
  loadRowTimer = setTimeout(() => {
    loadRowTimer = 0;
    const c = { WAIT: 0, LOAD: 0, OK: 0, EMPTY: 0, FAIL: 0 };
    for (const r of MODS) c[r.state]++;
    const fails = MODS.filter(r => r.state === 'FAIL');
    let t = `module routes of ${fmt(MODS.length)}: OK ${fmt(c.OK)} · LOAD ${fmt(c.LOAD)} · WAIT ${fmt(c.WAIT)}`;
    if (c.EMPTY) t += ` · EMPTY ${fmt(c.EMPTY)}`;
    if (fails.length) t += ` · FAIL ${fmt(fails.length)} (${fails[0].id}: ${fails[0].why})`;
    t += ` · family index ${U.tier2}`;
    $('loadrow').textContent = t;
  }, 50);
}

/* ── the numbered database ───────────────────────────────────────────────── */

const bin = (name, Kind) => fetchBin(DATA + name, Kind);

async function tier1() {
  const [meta, keys, lens, inFam, manifest] = await Promise.all([
    enqueue(() => fetchWithTimeout(DATA + 'all-lines.meta.json').then(r => r.json())),
    bin('all-lines.bin', Uint32Array),
    bin('all-lines.len.bin', Uint16Array),
    bin('all-lines.family.bin', Uint8Array),
    fetchJSON(MANIFEST)
  ]);
  Object.assign(U, { meta, keys, lens, inFam, n: keys.length, pos: placeAll(keys) });
  U.maxR = S * Math.sqrt(meta.max);
  U.manifestBuilt = String(manifest.built_utc);
  let carried = 0;
  for (let i = 0; i < inFam.length; i++) carried += inFam[i];
  $('count').textContent = `${fmt(U.n)} numbered lines · 1 to ${fmt(meta.max)} · ${fmt(carried)} carried by a family`;
  $('prov').textContent = `numbered database built ${meta.built_utc.slice(0, 16).replace('T', ' ')} UTC · pack ${meta.source.sha256.slice(0, 12)} · layers built ${U.manifestBuilt.slice(0, 16).replace('T', ' ')} UTC`;
  for (const l of manifest.layers) {
    if (!/^module-/.test(l.id)) continue;
    const row = { ...l, state: 'WAIT' };
    MODS.push(row); MOD.set(l.id, row);
  }
  fillPicker();
  paintLoadRow();
  paintPageQuestions();
}

/* Tier 2, only when a thread is followed or a line card asks: how many families carry each line. */
let tier2Promise = null;
function tier2() {
  if (tier2Promise) return tier2Promise;
  U.tier2 = 'LOAD'; paintLoadRow();
  tier2Promise = Promise.all([
    enqueue(() => fetchWithTimeout(DATA + 'families.json').then(r => r.json())),
    bin('lines.bin', Uint32Array)
  ]).then(([families, famLines]) => {
    const count = new Uint16Array(U.n), seen = new Int32Array(U.n).fill(-1);
    for (let f = 0; f < families.length; f++) {
      const o = families[f].lineOffset, c = families[f].lineCount;
      for (let j = o; j < o + c; j++) {
        const i = indexOfKey(U.keys, famLines[j]);
        if (i < 0 || seen[i] === f) continue;
        seen[i] = f; if (count[i] < 65535) count[i]++;
      }
    }
    Object.assign(U, { families, famLines, famCount: count, tier2: 'OK' });
    paintLoadRow();
    if (F.thread) paintStop(); else if (cardKey >= 0) keyCard(cardKey);
  }, e => { U.tier2 = 'FAIL (' + e.message + ')'; tier2Promise = null; paintLoadRow(); });
  return tier2Promise;
}

/* ── the substrate density image: built once, never per frame ────────────── */

const DENS_N = 256;
let dens = null;
function buildDensity() {
  const N = DENS_N, R = U.maxR, cnt = new Uint32Array(N * N);
  let max = 0;
  for (let i = 0; i < U.n; i++) {
    const gx = Math.min(N - 1, Math.floor((U.pos[i * 2] + R) / (2 * R) * N));
    const gy = Math.min(N - 1, Math.floor((R - U.pos[i * 2 + 1]) / (2 * R) * N));
    cnt[gy * N + gx]++;
  }
  for (let i = 0; i < cnt.length; i++) if (cnt[i] > max) max = cnt[i];
  dens = document.createElement('canvas'); dens.width = dens.height = N;
  const dc = dens.getContext('2d'), img = dc.createImageData(N, N);
  for (let i = 0; i < cnt.length; i++) {
    const a = cnt[i] ? Math.sqrt(cnt[i] / max) : 0;
    img.data[i * 4] = 92; img.data[i * 4 + 1] = 108; img.data[i * 4 + 2] = 146;
    img.data[i * 4 + 3] = Math.round(a * 150);
  }
  dc.putImageData(img, 0, 0);
  U.densMax = max;
}

/* ── the camera ──────────────────────────────────────────────────────────── */

const sx = x => (x - view.x) * view.zoom + view.w / 2;
const sy = y => view.h / 2 - (y - view.y) * view.zoom;

function resize() {
  view.dpr = Math.min(window.devicePixelRatio || 1, 2);
  view.w = stage.clientWidth; view.h = stage.clientHeight;
  stage.width = Math.round(view.w * view.dpr); stage.height = Math.round(view.h * view.dpr);
  sizeStrip();
}

const homeZoom = () => Math.min(view.w, view.h) / (U.maxR * 2.15);
function home() { view.x = 0; view.y = 0; view.zoom = homeZoom(); draw(); }

let flight = 0;
function flyTo(x, y, z1, ms = 700) {
  const id = ++flight; window.__journey.flying = true;
  const z0 = view.zoom, x0 = view.x, y0 = view.y, t0 = performance.now();
  const hop = Math.hypot(x - x0, y - y0);
  /* a long drive rises out of the street and comes back down: zoom dips mid-flight when the hop leaves the screen */
  const lift = Math.min(1, hop * Math.min(z0, z1) / (3 * Math.min(view.w, view.h)));
  return new Promise(res => {
    (function step(t) {
      if (id !== flight) return res(false);
      const u = Math.min(1, (t - t0) / ms);
      const e = u < .5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
      const zl = Math.exp(Math.log(z0) + (Math.log(z1) - Math.log(z0)) * e);
      view.x = x0 + (x - x0) * e; view.y = y0 + (y - y0) * e;
      view.zoom = Math.max(0.02, zl * (1 - 0.8 * lift * Math.sin(Math.PI * e)));
      render();
      if (u < 1) requestAnimationFrame(step); else { window.__journey.flying = false; res(true); }
    })(t0);
  });
}

/* ── the visible radius band, from the placement law ─────────────────────── */

function lowerBound(a, v) { let lo = 0, hi = a.length; while (lo < hi) { const m = (lo + hi) >> 1; if (a[m] < v) lo = m + 1; else hi = m; } return lo; }

function visibleBand() {
  const hw = view.w / 2 / view.zoom, hh = view.h / 2 / view.zoom;
  const x0 = view.x - hw, x1 = view.x + hw, y0 = view.y - hh, y1 = view.y + hh;
  const nx = Math.max(x0, Math.min(0, x1)), ny = Math.max(y0, Math.min(0, y1));
  const r0 = Math.hypot(nx, ny);
  const r1 = Math.max(Math.hypot(x0, y0), Math.hypot(x0, y1), Math.hypot(x1, y0), Math.hypot(x1, y1));
  const k0 = Math.floor((r0 / S) ** 2), k1 = Math.ceil((r1 / S) ** 2);
  const i0 = lowerBound(U.keys, k0), i1 = lowerBound(U.keys, k1 + 1);
  return { r0, r1, k0, k1, i0, i1, x0, x1, y0, y1 };
}

/* ── drawing ─────────────────────────────────────────────────────────────── */

const drawnIdx = new Int32Array(DRAW_CAP);
let drawnN = 0;
let pending = false;
function draw() { if (!pending) { pending = true; requestAnimationFrame(() => { pending = false; render(); }); } }

function strokeXY(c, xy) {
  c.beginPath();
  for (let i = 0; i < xy.length / 2; i++) {
    const X = sx(xy[i * 2]), Y = sy(xy[i * 2 + 1]);
    if (i === 0) c.moveTo(X, Y); else c.lineTo(X, Y);
  }
  c.stroke();
}

/* A route whose legs run into a heavily shared line is drawn as short stubs
   pointing at it, ending at the substation symbol, instead of spokes crossing
   the wafer. The followed thread is always drawn in full.

   STREAMED, NOT ALL AT ONCE. A software rasteriser (WebKit here) pays for a
   stroke by the pixels it covers, so a crowd of routes crossing the screen can
   stall a frame for a second. Routes other than the followed thread are drawn
   into their own layer canvas in chunks of at most INK_PER_FRAME screen pixels
   of length. While the camera moves only the first chunk exists; as soon as it
   holds still, each following frame adds the next chunk until every visible
   leg is drawn. The page prints how far the stream has got. */
const INK_PER_FRAME = 120000;
const RL = { canvas: document.createElement('canvas'), key: '', mi: 0, ri: 0, li: 0, legs: 0, done: true, chunks: 0 };

function legsInto(c, d, hubSet, full, budget, cur) {
  const W = view.w, H = view.h;
  let ink = 0, legs = 0;
  c.beginPath();
  for (; cur.ri < d.routes.length; cur.ri++, cur.li = 0) {
    const r = d.routes[cur.ri], xy = r.xy, n = r.keys.length;
    for (; cur.li + 1 < n; cur.li++) {
      if (!full && ink >= budget) { c.stroke(); return { ink, legs, more: true }; }
      const i = cur.li;
      const ha = !full && hubSet.has(r.keys[i]), hb = !full && hubSet.has(r.keys[i + 1]);
      if (ha && hb) continue;
      let ax = sx(xy[i * 2]), ay = sy(xy[i * 2 + 1]), bx = sx(xy[i * 2 + 2]), by = sy(xy[i * 2 + 3]);
      if (ha || hb) {
        if (ha) { const tx = ax, ty = ay; ax = bx; ay = by; bx = tx; by = ty; }
        const len = Math.hypot(bx - ax, by - ay) || 1, t = Math.min(1, 16 / len);
        bx = ax + (bx - ax) * t; by = ay + (by - ay) * t;
      }
      if ((ax < 0 && bx < 0) || (ay < 0 && by < 0) || (ax > W && bx > W) || (ay > H && by > H)) continue;
      c.moveTo(ax, ay); c.lineTo(bx, by);
      ink += Math.min(Math.abs(bx - ax) + Math.abs(by - ay), W + H); legs++;
    }
  }
  c.stroke();
  return { ink, legs, more: false };
}

function streamRoutes(order, style, hubSet) {
  const cv = RL.canvas;
  if (cv.width !== stage.width || cv.height !== stage.height) { cv.width = stage.width; cv.height = stage.height; RL.key = ''; }
  const key = [view.x, view.y, view.zoom, view.w, view.h, F.thread?.id, F.i, order.length, hubs.length].join('|');
  const rc = cv.getContext('2d');
  rc.setTransform(view.dpr, 0, 0, view.dpr, 0, 0);
  if (key !== RL.key) {
    RL.key = key; RL.mi = 0; RL.ri = 0; RL.li = 0; RL.legs = 0; RL.done = false; RL.chunks = 0;
    rc.clearRect(0, 0, view.w, view.h);
  }
  if (RL.done) return;
  let budget = INK_PER_FRAME;
  rc.lineCap = 'butt';
  while (RL.mi < order.length && budget > 0) {
    const row = order[RL.mi], st = style(row);
    if (!st) { RL.mi++; RL.ri = 0; RL.li = 0; continue; }
    rc.globalAlpha = st.alpha; rc.strokeStyle = row.colour; rc.lineWidth = st.width;
    const cur = { ri: RL.ri, li: RL.li };
    const got = legsInto(rc, row.data, hubSet, false, budget, cur);
    budget -= got.ink; RL.legs += got.legs;
    if (got.more) { RL.ri = cur.ri; RL.li = cur.li; break; }
    RL.mi++; RL.ri = 0; RL.li = 0;
  }
  rc.globalAlpha = 1;
  RL.chunks++;
  RL.done = RL.mi >= order.length;
  if (!RL.done) draw();                       /* the next frame adds the next chunk */
}

function substation(c, X, Y, count, lit) {
  c.fillStyle = '#05070b'; c.strokeStyle = lit ? '#ffd54a' : '#ffffff'; c.lineWidth = 1.3;
  c.fillRect(X - 6, Y - 6, 12, 12); c.strokeRect(X - 6, Y - 6, 12, 12);
  c.beginPath(); c.moveTo(X - 4, Y); c.lineTo(X + 4, Y); c.stroke();
  c.font = '10px ui-monospace,Menlo,Consolas,monospace'; c.fillStyle = lit ? '#ffd54a' : '#e6e9ef';
  c.fillText(fmt(count), X + 9, Y - 6);
}

function render() {
  if (!U.keys) return;
  const t0 = performance.now();
  const c = ctx, z = view.zoom, L = levelOf(z);
  c.setTransform(view.dpr, 0, 0, view.dpr, 0, 0);
  c.fillStyle = '#030406'; c.fillRect(0, 0, view.w, view.h);
  const following = !!F.thread;
  let band = null, capped = false, bandKeys = 0; drawnN = 0;

  /* GALAXY and SYSTEM: the precomputed substrate image, no individual lines */
  if (L <= 1 && dens) {
    c.globalAlpha = following ? 0.35 : (L === 0 ? 1 : 0.6);
    c.imageSmoothingEnabled = true;
    c.drawImage(dens, sx(-U.maxR), sy(U.maxR), 2 * U.maxR * z, 2 * U.maxR * z);
    c.globalAlpha = 1;
  }

  /* PLANET and SURFACE: only keys inside the visible radius band exist */
  if (L >= 2) {
    band = visibleBand();
    bandKeys = band.i1 - band.i0;
    const { x0, x1, y0, y1 } = band, P = U.pos, K = U.keys;
    const size = L === 3 ? Math.min(7, 1.2 + z * 0.06) : Math.max(1.2, z * 0.18);
    const lit = F.keySet;
    const groups = [[], [], []];   /* 0 no family, 1 carried, 2 on the followed thread */
    for (let i = band.i0; i < band.i1; i++) {
      const x = P[i * 2], y = P[i * 2 + 1];
      if (x < x0 || x > x1 || y < y0 || y > y1) continue;
      if (drawnN >= DRAW_CAP) { capped = true; break; }
      drawnIdx[drawnN++] = i;
      groups[lit && lit.has(K[i]) ? 2 : U.inFam[i]].push(i);
    }
    const cols = following ? ['#0e1117', '#171b23', '#ffd54a'] : ['#1b1f28', '#3a4152', '#ffd54a'];
    for (let g = 0; g < 3; g++) {
      c.fillStyle = cols[g];
      const s = g === 2 ? size + 2 : size, h = s / 2, arr = groups[g];
      for (let j = 0; j < arr.length; j++) { const i = arr[j]; c.fillRect(sx(P[i * 2]) - h, sy(P[i * 2 + 1]) - h, s, s); }
    }
    if (L === 3 && drawnN <= 400) {
      c.font = '9px ui-monospace,Menlo,Consolas,monospace'; c.fillStyle = '#6f7890';
      for (let j = 0; j < drawnN; j++) { const i = drawnIdx[j]; c.fillText(String(K[i]), sx(P[i * 2]) + size, sy(P[i * 2 + 1]) - size); }
    }
  }

  /* copied lines from the band tiles the view has reached */
  let tileDrawn = 0;
  if (L >= 2 && TILES.on) {
    wantTiles(band);
    const { x0, x1, y0, y1 } = band;
    c.fillStyle = following ? '#5a1a55' : '#ff2bd6';
    const s2 = Math.max(1.6, Math.min(6, view.zoom * 0.14)), h2 = s2 / 2;
    for (const t of TILES.bands.values()) {
      if (t.state !== 'OK') continue;
      const K = t.keys, XY = t.xy;
      const a = lowerBound(K, band.k0), e = lowerBound(K, band.k1 + 1);
      for (let i = a; i < e; i++) {
        const x = XY[i * 2], y = XY[i * 2 + 1];
        if (x < x0 || x > x1 || y < y0 || y > y1) continue;
        c.fillRect(sx(x) - h2, sy(y) - h2, s2, s2); tileDrawn++;
      }
    }
  } else if (TILES.on) paintTileRow();

  /* module routes, by level */
  const W = woken();
  const hubSet = new Set();
  for (const [k, n] of hubs) if (n >= 3) hubSet.add(k);
  const crossing = F.cross || new Set();
  /* the followed thread's neighbours stream first, then the rest in manifest order */
  const order = following ? [...W].sort((a, b) => crossing.has(b.id) - crossing.has(a.id)) : W;
  const crowd = Math.min(1, 4 / Math.sqrt(Math.max(1, W.length)));
  const dots = [];
  if (L === 0) {
    for (const row of W) {
      const d = row.data; if (!d || d.firsts.length < 2) continue;
      const isThread = following && F.thread.module === row.id;
      c.globalAlpha = (!following ? crowd : isThread ? 1 : crossing.has(row.id) ? 0.3 : 0.05) * 0.7;
      c.strokeStyle = row.colour; c.lineWidth = isThread ? 5 : 3.2;
      strokeXY(c, d.trunk);
    }
  } else if (W.length) {
    streamRoutes(order, row => {
      if (following && F.thread.module === row.id) return null;
      const neighbour = following && crossing.has(row.id);
      return { alpha: !following ? crowd : neighbour ? 0.3 : 0.05, width: 1.3 };
    }, hubSet);
    c.drawImage(RL.canvas, 0, 0, view.w, view.h);
    const tm = following && F.thread.module ? MOD.get(F.thread.module) : null;
    if (tm && tm.data) {
      /* the whole block stays faint; only the current stop's own function is drawn bright (drawThreadOnWafer) */
      const cur = { ri: 0, li: 0 };
      c.globalAlpha = 0.28; c.strokeStyle = tm.colour; c.lineWidth = 1.2; legsInto(c, tm.data, hubSet, true, Infinity, cur);
      c.globalAlpha = 1;
    }
    for (const row of W) {
      const d = row.data; if (!d) continue;
      if (following && F.thread.module !== row.id && !crossing.has(row.id)) continue;
      for (const s of d.firsts) {
        const X = sx(s.x), Y = sy(s.y);
        if (X < -8 || Y < -8 || X > view.w + 8 || Y > view.h + 8) continue;
        dots.push(X, Y);
      }
    }
  }
  c.globalAlpha = 1;
  if (dots.length) {
    c.fillStyle = '#ffffff'; c.beginPath();
    for (let j = 0; j < dots.length; j += 2) { c.moveTo(dots[j] + 2.4, dots[j + 1]); c.arc(dots[j], dots[j + 1], 2.4, 0, 6.2832); }
    c.fill();
  }
  c.globalAlpha = 1;

  /* labels for the largest woken routes at SYSTEM */
  if (L === 1 && !following) {
    const big = W.filter(r => r.data && r.data.firsts.length).sort((a, b) => b.data.lines - a.data.lines).slice(0, 6);
    c.font = '11px ui-monospace,Menlo,Consolas,monospace';
    for (const r of big) {
      const s = r.data.firsts[r.data.firsts.length >> 1];
      const X = sx(s.x), Y = sy(s.y);
      if (X < 0 || Y < 0 || X > view.w || Y > view.h) continue;
      const tw = c.measureText(r.label).width;
      c.fillStyle = '#000000cc'; c.fillRect(X + 6, Y - 13, tw + 6, 15);
      c.fillStyle = r.colour; c.fillText(r.label, X + 9, Y - 2);
    }
  }

  /* junctions as substation symbols with their counts */
  if (L >= 1 && hubs.length) {
    const minCount = L === 1 ? 8 : 3;
    let shown = 0;
    for (const [k, n] of hubs) {
      if (n < minCount || shown >= 120) continue;
      if (following && !F.keySet.has(k)) continue;
      const [x, y] = place(k), X = sx(x), Y = sy(y);
      if (X < -10 || Y < -10 || X > view.w + 10 || Y > view.h + 10) continue;
      substation(c, X, Y, n, following); shown++;
    }
  }

  if (following) drawThreadOnWafer(c);

  const ms = performance.now() - t0;
  const rec = { level: LEVELS[L].id, zoom: z, drawn: drawnN, legs: RL.legs, streamDone: RL.done, chunks: RL.chunks, tileDrawn, tilesHeld: TILES.held, bandKeys, capped, ms, routes: W.length };
  stats.last = rec; stats.frames.push(rec); if (stats.frames.length > 4000) stats.frames.splice(0, 2000);
  paintHud(rec, band);
}

function drawThreadOnWafer(c) {
  const st = F.thread.stops, cur = st[F.i];
  c.setLineDash([6, 5]); c.strokeStyle = '#ffd54a'; c.lineWidth = 1.2; c.globalAlpha = 0.8;
  c.beginPath();
  st.forEach((s, j) => { const [x, y] = place(s.key); j ? c.lineTo(sx(x), sy(y)) : c.moveTo(sx(x), sy(y)); });
  c.stroke(); c.setLineDash([]); c.globalAlpha = 1;
  if (cur.xy) { c.strokeStyle = '#000'; c.lineWidth = 5; strokeXY(c, cur.xy); c.strokeStyle = '#ffd54a'; c.lineWidth = 2.2; strokeXY(c, cur.xy); }
  for (const j of [F.i - 1, F.i + 1]) {
    if (j < 0 || j >= st.length) continue;
    const [x, y] = place(st[j].key);
    c.strokeStyle = '#ffffff'; c.lineWidth = 1.2; c.beginPath(); c.arc(sx(x), sy(y), 8, 0, 6.2832); c.stroke();
  }
  /* the next stop, when off screen, is an arrow on the edge pointing along the road */
  const [x, y] = place(cur.key), X = sx(x), Y = sy(y);
  const nx = st[F.i + 1];
  if (nx) {
    const [qx, qy] = place(nx.key), QX = sx(qx), QY = sy(qy), m = 22;
    if (QX < m || QY < m || QX > view.w - m || QY > view.h - m) {
      const cx = view.w / 2, cy = view.h / 2, dx = QX - cx, dy = QY - cy;
      const k = Math.min((view.w / 2 - m) / Math.abs(dx || 1e-9), (view.h / 2 - m) / Math.abs(dy || 1e-9));
      const ax = cx + dx * k, ay = cy + dy * k, ang = Math.atan2(dy, dx);
      c.fillStyle = '#ffd54a'; c.beginPath();
      c.moveTo(ax + Math.cos(ang) * 10, ay + Math.sin(ang) * 10);
      c.lineTo(ax + Math.cos(ang + 2.5) * 9, ay + Math.sin(ang + 2.5) * 9);
      c.lineTo(ax + Math.cos(ang - 2.5) * 9, ay + Math.sin(ang - 2.5) * 9); c.fill();
      c.font = '10px ui-monospace,Menlo,Consolas,monospace';
      const label = `next ${fmt(nx.key)} · ${fmt(Math.round(Math.hypot(qx - x, qy - y)))} units`;
      const tw = c.measureText(label).width;
      c.fillText(label, Math.max(4, Math.min(view.w - tw - 4, ax - tw / 2)), ay + (ay > cy ? -14 : 20));
    }
  }
  c.strokeStyle = '#ffd54a'; c.lineWidth = 1.6;
  c.beginPath(); c.arc(X, Y, 12, 0, 6.2832); c.stroke();
  c.beginPath(); c.moveTo(X - 20, Y); c.lineTo(X - 14, Y); c.moveTo(X + 14, Y); c.lineTo(X + 20, Y);
  c.moveTo(X, Y - 20); c.lineTo(X, Y - 14); c.moveTo(X, Y + 14); c.lineTo(X, Y + 20); c.stroke();
}

let hudLevel = -1;
function paintHud(rec, band) {
  const L = LEVELS.findIndex(l => l.id === rec.level);
  if (L !== hudLevel) {
    hudLevel = L;
    document.querySelectorAll('#levels button').forEach(b => b.classList.toggle('on', +b.dataset.level === L));
  }
  $('lvl').textContent = `LEVEL ${rec.level} · zoom ${rec.zoom < 10 ? rec.zoom.toFixed(2) : rec.zoom.toFixed(0)}`;
  let t;
  if (rec.level === 'GALAXY') t = `0 keys drawn · density image ${DENS_N}×${DENS_N} · ${fmt(rec.routes)} route trunks`;
  else if (rec.level === 'SYSTEM') t = `0 keys drawn · ${fmt(rec.routes)} routes, ${fmt(rec.legs)} legs streamed${rec.streamDone ? ' (all visible)' : ` (still streaming, chunk ${fmt(rec.chunks)})`} · symbols where 8 or more routes share a line`;
  else t = `${fmt(rec.drawn)} keys drawn this frame${rec.capped ? ' (ceiling reached)' : ''}${TILES.on ? `, ${fmt(rec.tileDrawn)} copied` : ''} · band ${fmt(band.k0)}–${fmt(band.k1)} holds ${fmt(rec.bandKeys)} · ${fmt(rec.legs)} route legs${rec.streamDone ? '' : ' so far'}`;
  $('drawn').textContent = t;
}

/* ── threads ─────────────────────────────────────────────────────────────── */

function fillPicker() {
  const sel = $('thread');
  const groups = new Map();
  for (const r of MODS) {
    if (!groups.has(r.group)) { const g = document.createElement('optgroup'); g.label = r.group; groups.set(r.group, g); sel.appendChild(g); }
    const o = document.createElement('option'); o.value = r.id; o.textContent = r.label;
    groups.get(r.group).appendChild(o);
  }
}

async function moduleThread(id) {
  const row = await loadModule(id);
  const d = row.data;
  if (!d.firsts.length) throw new Error(`EMPTY: ${row.label} has no first-line points to stop at`);
  const byFamily = new Map(d.routes.map(r => [r.family, r]));
  const stops = d.firsts.map(s => {
    const r = byFamily.get(s.family);
    return { key: s.key, fn: s.fn, title: s.fn, family: s.family, lines: s.lines, xy: r ? r.xy : null, routeKeys: r ? r.keys : null };
  });
  return { kind: 'module', id, module: id, label: row.label, colour: row.colour, stops,
           order: 'stops are the first lines of the block\'s functions, in numbering order' };