feat(walls): marker retraction, re-earn and marker fault classes (#60)

Retract a player marker when the GM removes the wall, clears
player_marker, makes the wall stop deriving window, or moves its
geometry (a/b, thickness, position). Retraction emits del only and
never the new anchor; a re-earned marker gets a fresh id. The
marker_seen deletes ride the same derived-memory NORMAL transaction as
the explored chunks, after the wall row commits and before the ack.

Replace the leak checker's aggregate MARKER class with MARKER_EARLY,
MARKER_EXTRA and MARKER_OFF, add their leakfault hooks and exact-class
fault tests, and extend the leak scenario with an unseen marked window
and a marked solid wall as the negative controls. WS black-box tests
cover all four causes, fresh re-earns, no untouched re-send, no
set/del for one id in a frame, no explored write from a retraction,
and delete/cascade durability across a restart.
This commit is contained in:
2026-11-19 23:43:44 +01:00
parent edd4437f7e
commit c3c0a39188
9 changed files with 425 additions and 60 deletions
+215 -1
View File
@@ -8,7 +8,8 @@
// 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 { withGmServer, openAs } from './withserver.mjs'
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}$/
@@ -180,3 +181,216 @@ test('marker lifetime and resync: survives walk-away and doors, full after explo
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 })
}
})