// Walls r1 (#59): player markers over the real wire. A marker exists only for a wall that is both // `player_marker:true` and derives `window`; it carries one opaque per-player id and the drawn // midpoint in integer px, nothing else. It is memory like explored: it survives walking away, a door // closing and lost sight, the full set comes after explored on connect/resync, and a player never // sees another player's marker (DESIGN 3.1, 5.8, 6.1, 6.3, 8; ADR-0015). // // Retraction, re-earning and the MARKER_* fault classes are #60 and are deliberately not asserted // here: this group proves the emission gate, the anchor, storage/resync and privacy. import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { withGmServer, openAs, makeStateDir, gmKeyOf, startServer, stopServer } from './withserver.mjs' import { createPlayer, namePlayer, sleep } from './lib.mjs' const ID_RE = /^[A-Za-z0-9_-]{12}$/ async function gmConnect(origin, gmKey) { const conn = await openAs(origin, gmKey) await conn.ws.waitText('hello') const walls = await conn.ws.waitText('walls') return { ...conn, scene: walls.scene } } async function addWall(gm, fields) { const ack = await gm.ws.request({ t: 'wall.add', scene: gm.scene, ...fields }) assert.equal(ack.ok, true, JSON.stringify(ack)) return ack } async function namedPlayer(gm, origin, name) { const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, name) const conn = await openAs(origin, player.key) await conn.ws.waitText('hello') return { ...player, conn } } async function addSightToken(gm, player, pos) { const ack = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos, descriptive_name: 'sight', actual_name: 'Sight', controller: player.id, flags: { vision: true } }) assert.equal(ack.ok, true, JSON.stringify(ack)) return ack.id } const markerFrames = ws => ws.messages.filter(m => m && m.t === 'marker') function assertMarkerShape(frame) { const keys = frame.full ? ['full', 'n', 'set', 't'] : ['del', 'full', 'n', 'set', 't'] assert.deepEqual(Object.keys(frame).sort(), keys) assert.equal(Array.isArray(frame.del ?? []), true) for (const entry of frame.set) { assert.deepEqual(Object.keys(entry).sort(), ['id', 'pos'], 'a marker carries only id and pos') assert.match(entry.id, ID_RE, 'the marker id is 12 characters from the token alphabet') assert.equal(entry.pos.length, 2) assert.equal(Number.isInteger(entry.pos[0]) && Number.isInteger(entry.pos[1]), true, 'the anchor is integer px (6.1)') } } const markersAfter = (ws, from) => ws.messages.filter((m, i) => i >= from && m && m.t === 'marker') test('marker emission: only a marked window, exact anchor and count, no wall field (#59)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) // The first window is in view of where the subject's token will stand: (200,600) midpoint. await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] }) const player = await namedPlayer(gm, s.origin, 'Marker One') const tokenId = await addSightToken(gm, player, [300, 300]) const first = await player.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0) assert.equal(first.full, false, 'a mutation sends a diff, not a full set') assertMarkerShape(first) assert.equal(first.set.length, 1, 'the window preset emits exactly one marker') assert.deepEqual(first.set[0].pos, [200, 600], 'the anchor is the drawn midpoint, integer px') assert.match(first.set[0].id, ID_RE) // The wall id (an integer) and any wall field are absent from the whole frame. for (const banned of ['wall', 'walls', 'a', 'b', 'kind', 'player_marker', 'door']) { assert.equal(JSON.stringify(first).includes(`"${banned}"`), false, `a marker frame carries no wall field ${banned}`) } // A solid wall with the switch on stores the switch and sends nothing (5.8 gate). let from = player.conn.ws.messages.length await addWall(gm, { a: [100, 700], b: [400, 700], player_marker: true }) await sleep(120) assert.equal(markersAfter(player.conn.ws, from).length, 0, 'a solid wall with player_marker:true emits no marker') // The same see-through shape with the switch off is an invisible barrier. from = player.conn.ws.messages.length await addWall(gm, { blocks_sight: false, blocks_movement: true, door: true, player_marker: false, a: [500, 500], b: [500, 700] }) await sleep(120) assert.equal(markersAfter(player.conn.ws, from).length, 0, 'a window with its marker off emits nothing') // Any part of the extent qualifies, and the anchor stays the midpoint even when the midpoint // is outside the vision radius: (300,650) is 350 px from the token, the midpoint (300,950) 650. from = player.conn.ws.messages.length await addWall(gm, { preset: 'window', a: [300, 650], b: [300, 1250] }) const second = await player.conn.ws.waitFor(m => m.t === 'marker' && m.set.some(e => e.pos[0] === 300 && e.pos[1] === 950)) assertMarkerShape(second) assert.equal(second.set.length, 1, 'one newly seen window yields exactly one marker') assert.notEqual(second.set[0].id, first.set[0].id, 'each wall gets its own marker id') const all = markerFrames(player.conn.ws).flatMap(m => m.set) assert.equal(new Set(all.map(e => e.id)).size, 2, 'the player holds exactly two markers') // The GM never receives a marker frame (5.8: a player's memory stays a player's). assert.equal(markerFrames(gm.ws).length, 0) gm.ws.close(); player.conn.ws.close() }) }) test('marker lifetime and resync: survives walk-away and doors, full after explored, private (#59)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const window = await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] }) const alice = await namedPlayer(gm, s.origin, 'Marker Alice') const aliceToken = await addSightToken(gm, alice, [300, 300]) const seen = await alice.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0) const aliceMarker = seen.set[0] assert.equal(seen.set.length, 1) // Walk away: the marker is memory, so no retraction and no new frame. let from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'token.move', id: aliceToken, pos: [2000, 2000] })).ok, true) await sleep(120) assert.equal(markersAfter(alice.conn.ws, from).length, 0, 'walking away never retracts or re-sends a marker') // A door closing is not a cause either (the window preset is an opening window). for (const open of [true, false]) { from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'door.set', scene: gm.scene, id: window.id, open })).ok, true) await sleep(80) assert.equal(markersAfter(alice.conn.ws, from).length, 0, `door.set open:${open} leaves the marker alone`) } // A fresh connection gets the full set after the explored snapshot (6.3). const again = await openAs(s.origin, alice.key) await again.ws.waitText('hello') const explored = await again.ws.waitFor(m => m.t === 'explored' && m.full === true) const full = await again.ws.waitFor(m => m.t === 'marker' && m.full === true) assertMarkerShape(full) assert.ok(again.ws.messages.indexOf(explored) < again.ws.messages.indexOf(full), 'the marker full set comes after explored') assert.equal(full.set.length, 1) assert.equal(full.set[0].id, aliceMarker.id, 'the same opaque id survives the reconnect') assert.deepEqual(full.set[0].pos, aliceMarker.pos) // A second player sees the same window with their own, different id; neither receives the // other's (5.8: never another player's marker). const bob = await namedPlayer(gm, s.origin, 'Marker Bob') await addSightToken(gm, bob, [350, 350]) const bobSeen = await bob.conn.ws.waitFor(m => m.t === 'marker' && m.set.length > 0) const bobMarker = bobSeen.set[0] assert.notEqual(bobMarker.id, aliceMarker.id, 'ids are per player') assert.deepEqual(bobMarker.pos, aliceMarker.pos) assert.equal(JSON.stringify(alice.conn.ws.messages).includes(bobMarker.id), false, "Alice never receives Bob's marker id") assert.equal(JSON.stringify(bob.conn.ws.messages).includes(aliceMarker.id), false, "Bob never receives Alice's marker id") // Turning the switch off makes the barrier invisible: a player who sees it now gets no marker. assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: window.id, player_marker: false })).ok, true) const carol = await namedPlayer(gm, s.origin, 'Marker Carol') await addSightToken(gm, carol, [300, 300]) await sleep(150) const carolMarkers = markerFrames(carol.conn.ws).flatMap(m => m.set) assert.equal(carolMarkers.length, 0, 'a window with the switch off is an invisible barrier') assert.equal(markerFrames(gm.ws).length, 0, 'the GM receives no marker') gm.ws.close(); alice.conn.ws.close(); again.ws.close(); bob.conn.ws.close(); carol.conn.ws.close() }) }) // ------------------------------------------------------------------------------------------------- // Walls r1 (#60): retraction, re-earning and the derived-memory ordering. const framesAfter = (ws, from) => ws.messages.filter((m, i) => i >= from) async function waitAfter(ws, from, pred, what, timeoutMs = 5000) { const deadline = Date.now() + timeoutMs for (;;) { const hit = ws.messages.find((m, i) => i >= from && m && pred(m)) if (hit) return hit if (Date.now() > deadline) { throw new Error(`no ${what} after message ${from}; saw: ` + JSON.stringify(ws.messages.slice(from).map(m => m && m.t))) } await sleep(10) } } function assertDelOnly(frames, id, what) { const markers = frames.filter(m => m && m.t === 'marker') assert.equal(markers.length, 1, `${what}: exactly one marker frame`) const frame = markers[0] assert.equal(frame.full, false, `${what}: a retraction is a diff`) assert.deepEqual(frame.set, [], `${what}: a retraction carries no set`) assert.deepEqual(frame.del, [id], `${what}: the marker is retracted by id`) } function assertNoSameIdBothWays(ws) { for (const frame of markerFrames(ws)) { const set = new Set(frame.set.map(e => e.id)) for (const id of frame.del ?? []) { assert.equal(set.has(id), false, `set and del for ${id} in one frame`) } } } test('retraction: every cause sends del only, a re-earned marker gets a fresh id (#60)', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) // Two marked windows the subject sees at connect: one is edited throughout, the other is // untouched and must never be re-sent. const win = await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] }) await addWall(gm, { preset: 'window', a: [400, 300], b: [400, 400] }) const alice = await namedPlayer(gm, s.origin, 'Retract Alice') const tokenId = await addSightToken(gm, alice, [300, 300]) const seen = await alice.conn.ws.waitFor(m => m.t === 'marker' && m.set.length === 2) assertMarkerShape(seen) const byPos = p => seen.set.find(e => e.pos[0] === p[0] && e.pos[1] === p[1]) const keepId = byPos([400, 350]).id let current = byPos([200, 600]).id const editedAt = () => m => m.t === 'marker' && (m.set.some(e => e.pos[0] === 200) || (m.del ?? []).includes(current)) const away = async () => { assert.equal((await gm.ws.request({ t: 'token.move', id: tokenId, pos: [2000, 2000] })).ok, true) await sleep(100) } const back = async () => { assert.equal((await gm.ws.request({ t: 'token.move', id: tokenId, pos: [300, 300] })).ok, true) await sleep(100) } const retract = async action => { const from = alice.conn.ws.messages.length assert.equal((await action()).ok, true) await sleep(100) return framesAfter(alice.conn.ws, from) } const reEarn = async what => { const from = alice.conn.ws.messages.length await back() const frame = await waitAfter(alice.conn.ws, from, editedAt(), what) assert.equal(frame.set.length, 1, `${what}: one marker is re-earned`) return { frame, from } } // Walking away is not a cause: memory stays, nothing is sent. let from = alice.conn.ws.messages.length await away() assert.equal(markersAfter(alice.conn.ws, from).length, 0, 'walking away never retracts a marker') // Cause 4a: endpoints move while the subject cannot see the new geometry -> del only. let frames = await retract(() => gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, a: [200, 520], b: [200, 720] })) assertDelOnly(frames, current, 'endpoint move') assert.equal(frames.some(m => m.t === 'explored'), false, 'a retraction writes no explored cell') let earned = await reEarn('endpoint move') assert.deepEqual(earned.frame.set[0].pos, [200, 620], 'the new anchor is the new midpoint') assert.notEqual(earned.frame.set[0].id, current, 'a re-earned marker gets a fresh id') current = earned.frame.set[0].id assert.equal(JSON.stringify(framesAfter(alice.conn.ws, earned.from)).includes(keepId), false, 'the untouched marker is never re-sent') // Cause 4b: thickness. await away() frames = await retract(() => gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, thickness: 7 })) assertDelOnly(frames, current, 'thickness change') earned = await reEarn('thickness change') assert.notEqual(earned.frame.set[0].id, current, 'thickness re-earn is a fresh id') current = earned.frame.set[0].id // Cause 4c: placement. await away() frames = await retract(() => gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, position: 'left' })) assertDelOnly(frames, current, 'placement change') earned = await reEarn('placement change') assert.notEqual(earned.frame.set[0].id, current, 'placement re-earn is a fresh id') current = earned.frame.set[0].id // Cause 2: the player_marker switch is turned off. Nothing about sight changes, so the only // frame is the retraction itself. frames = await retract(() => gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, player_marker: false })) assertDelOnly(frames, current, 'marker switch off') assert.equal(frames.some(m => m.t === 'explored' || m.t === 'vision'), false, 'a switch-off retraction changes no sight state') // Turning it back on re-earns a fresh id (the subject still sees the window). from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, player_marker: true })).ok, true) const re4 = await waitAfter(alice.conn.ws, from, editedAt(), 'switch-on re-earn') assert.equal(re4.set.length, 1) assert.notEqual(re4.set[0].id, current, 'a switch-on re-earn is a fresh id') assert.equal(JSON.stringify(framesAfter(alice.conn.ws, from)).includes(keepId), false, 'the untouched marker is never re-sent') current = re4.set[0].id // Cause 3: the wall stops deriving `window` (a solid closed door now). frames = await retract(() => gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, blocks_sight: true })) assertDelOnly(frames, current, 'no longer a window') assert.equal(frames.some(m => m.t === 'explored'), false, 'making a window opaque reveals no new explored cell') // Back to a window: a fresh id again. from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: win.id, blocks_sight: false })).ok, true) const re5 = await waitAfter(alice.conn.ws, from, editedAt(), 'window re-earn') assert.equal(re5.set.length, 1) assert.notEqual(re5.set[0].id, current, 'a window re-earn is a fresh id') current = re5.set[0].id // Cause 1: the wall is removed. frames = await retract(() => gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: win.id })) assertDelOnly(frames, current, 'wall removed') // The full set on reconnect carries only the untouched marker. const again = await openAs(s.origin, alice.key) await again.ws.waitText('hello') const full2 = await again.ws.waitFor(m => m.t === 'marker' && m.full === true) assert.deepEqual(full2.set.map(e => e.id), [keepId], 'the retracted marker is gone and the untouched one survives') assertNoSameIdBothWays(alice.conn.ws) assertNoSameIdBothWays(again.ws) assert.equal(markerFrames(gm.ws).length, 0, 'the GM receives no marker') gm.ws.close(); alice.conn.ws.close(); again.ws.close() }) }) test('marker retraction is durable: delete and cascade survive a restart (#60)', async () => { const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) let aliceKey = null let keptId = null let s = await startServer({ stateDir }) try { const gm = await gmConnect(s.origin, gmKey) const off = await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] }) const gone = await addWall(gm, { preset: 'window', a: [300, 500], b: [300, 700] }) await addWall(gm, { preset: 'window', a: [400, 300], b: [400, 400] }) const alice = await namedPlayer(gm, s.origin, 'Restart Alice') aliceKey = alice.key await addSightToken(gm, alice, [300, 320]) const seen = await alice.conn.ws.waitFor(m => m.t === 'marker' && m.set.length === 3) const idAt = p => seen.set.find(e => e.pos[0] === p[0] && e.pos[1] === p[1]).id const offId = idAt([200, 600]) const goneId = idAt([300, 600]) keptId = idAt([400, 350]) let from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: off.id, player_marker: false })).ok, true) await sleep(100) assert.deepEqual(markersAfter(alice.conn.ws, from)[0].del, [offId]) from = alice.conn.ws.messages.length assert.equal((await gm.ws.request({ t: 'wall.remove', scene: gm.scene, id: gone.id })).ok, true) await sleep(100) assert.deepEqual(markersAfter(alice.conn.ws, from)[0].del, [goneId]) gm.ws.close(); alice.conn.ws.close() } finally { await stopServer(s) } s = await startServer({ stateDir }) try { const again = await openAs(s.origin, aliceKey) await again.ws.waitText('hello') const full = await again.ws.waitFor(m => m.t === 'marker' && m.full === true) assert.deepEqual(full.set.map(e => e.id), [keptId], 'the deleted marker rows stay deleted across the restart') again.ws.close() } finally { await stopServer(s) fs.rmSync(stateDir, { recursive: true, force: true }) } }) // ------------------------------------------------------------------------------------------------- // #60 leak fix: a cause change and its `marker_seen` deletes are one commit. // // The bug: `wall.update` committed the wall row in its FULL transaction, but the retraction deletes // rode the later NORMAL derived-memory transaction. A crash in between left the stale row; on // restart `markers.restore` recomputed the anchor from the NEW wall, so a player who never saw the // new geometry got it handed to them in the full marker set. The fix puts the deletes in the wall // transaction, exactly as the FK cascade already does for `wall.remove`. // // The `LEAK_FAULT=markerCrash` hook (a `-d:leakfault` build only, never shipped) quits the process // immediately after the wall commit and before the derived write. Each case then restarts on a clean // build: the changed wall's old marker must not come back, while an untouched window's marker must // be restored unchanged (the positive control, so the harness can fail). async function waitProcessExit(handle, timeoutMs = 5000) { const deadline = Date.now() + timeoutMs while (handle.child.exitCode === null && handle.child.signalCode === null && Date.now() < deadline) { await sleep(20) } assert.ok(handle.child.exitCode !== null || handle.child.signalCode !== null, 'the markerCrash fault did not kill the server') } async function markerCrashCase({ cause, patch, newAnchor, reEarns }) { const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) let alice let s = await startServer({ stateDir, defines: ['leakfault'], env: { LEAK_FAULT: 'markerCrash' } }) let winMarker let keepMarker try { const gm = await gmConnect(s.origin, gmKey) // Two marked windows in the subject's sight: one is edited into a cause change, the other is // untouched and is the positive control across the restart. const changed = await addWall(gm, { preset: 'window', a: [200, 500], b: [200, 700] }) await addWall(gm, { preset: 'window', a: [400, 300], b: [400, 400] }) alice = await namedPlayer(gm, s.origin, `Crash ${cause}`) await addSightToken(gm, alice, [300, 320]) const seen = await alice.conn.ws.waitFor(m => m.t === 'marker' && m.set.length === 2) winMarker = seen.set.find(e => e.pos[0] === 200).id keepMarker = seen.set.find(e => e.pos[0] === 400).id // Fire the wall write by hand (no awaited ack): the fault kills the process inside the handler, // after the wall+delete commit and before the derived transaction that used to carry the delete. gm.ws.sendText(JSON.stringify({ t: 'wall.update', scene: gm.scene, id: changed.id, ...patch, reqId: 9001 })) await waitProcessExit(s) assert.equal(alice.conn.ws.messages.some(m => m.t === 'marker' && (m.del ?? []).includes(winMarker)), false, `${cause}: the process died before the live del was sent`) } finally { try { alice.conn.ws.close() } catch {} await stopServer(s) } s = await startServer({ stateDir }) try { const again = await openAs(s.origin, alice.key) await again.ws.waitText('hello') const full = await again.ws.waitFor(m => m.t === 'marker' && m.full === true) const kept = full.set.find(e => e.pos[0] === 400 && e.pos[1] === 350) assert.equal(kept?.id, keepMarker, `${cause}: positive control, the untouched window's marker is restored with the same id`) assert.equal(full.set.some(e => e.id === winMarker), false, `${cause}: a restart never restores a marker whose wall cause changed`) if (newAnchor) { assert.equal(full.set.some(e => e.pos[0] === newAnchor[0] && e.pos[1] === newAnchor[1]), false, `${cause}: the player never receives the changed wall's new anchor`) } if (reEarns) { const fresh = full.set.find(e => !(e.pos[0] === 400 && e.pos[1] === 350)) assert.ok(fresh, `${cause}: the still-visible wall is re-earned only through the normal gate`) assert.notEqual(fresh.id, winMarker, `${cause}: a re-earned marker gets a fresh id`) } again.ws.close() } finally { await stopServer(s) fs.rmSync(stateDir, { recursive: true, force: true }) } } test('marker cause change and deletes are one commit: a crash before the derived write (#60)', async t => { await t.test('endpoint move', () => markerCrashCase({ cause: 'endpoint move', patch: { a: [1000, 1000], b: [1000, 1200] }, newAnchor: [1000, 1100], reEarns: false })) await t.test('thickness', () => markerCrashCase({ cause: 'thickness', patch: { thickness: 7 }, reEarns: true })) await t.test('player_marker off', () => markerCrashCase({ cause: 'player_marker off', patch: { player_marker: false }, reEarns: false })) await t.test('no longer a window', () => markerCrashCase({ cause: 'no longer a window', patch: { blocks_sight: true }, reEarns: false })) })