names: per-player reveal, one encountered write path, link by update (#27, #29, #30)

name.reveal wrote the subject's actual name into the encountered row of every
player who had met it: the SQL had no player predicate, so the leak was silent
live and visible only after a restart. rewriteEncountered is now the one path
that keeps a snapshot in step with its subject: it rewrites the SQLite row and
the live in-memory entry together from the name rule, takes onlyPlayer for
name.reveal (every player for reveal_all), and also runs on a rename, a link
change and an owner change, so the wire and a post-restart reload cannot
disagree (#30). The DB-only renameEncountered*/revealEncountered* helpers are
gone, which is what let the two stores drift.

token.update {character_id} now drops the token's own two names as part of the
link patch (#29): a plain token can be linked, names supplied with a link stay
invalid and unlinking still needs both names.

Tests: tcharacters covers the per-player row, the DB/memory equality, the link
handover; restart.test.mjs builds three named players, reveals to one of them
and asserts the rows directly and the whole resync over SIGTERM/SIGKILL, plus a
mid-session rename; leak.mjs/leakcheck.nim pit two players on the same subject
and pin each entry's expected name (STALE_NAME). Mutations e1-e3 and the
repointed b2 pin the three rules.
This commit is contained in:
2026-11-15 14:26:07 +01:00
parent e68c6eefbf
commit 8e045b0ac8
9 changed files with 528 additions and 93 deletions
+194 -25
View File
@@ -6,9 +6,17 @@
// sighting in the encountered list, explored cells, players and their names), then compares what a
// fresh GM connection and a fresh player connection receive as their full 6.3 resync before and
// after the restart. SIGTERM and SIGKILL are both checked: every acked commit is durable (4.2).
//
// #27: three named players meet the same subjects, and the reveals are per player, so the restart
// group also pins that the persisted `encountered` snapshot is the name *that* player knows. Before
// the fix the reveal SQL had no player predicate: the non-revealed players saw the actual name only
// after the restart. Each player's encountered list is compared before/after AND against the name
// that player may know (from the GM truth), so a leak cannot hide behind two equally wrong snapshots.
import test from 'node:test'
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import { createPlayer, namePlayer, sleep } from './lib.mjs'
import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs'
@@ -25,11 +33,20 @@ async function buildWorld(origin, gmKey) {
const gm = await gmConnect(origin, gmKey)
const saved = {}
try {
// Players: a GM plus one named player with a key (its own CRUD path is key.* + player.setname).
// Players: a GM plus three named players with keys (their CRUD path is key.* + player.setname).
// Three, because the per-player reveal bug (#27) needs a second and a third encountered list to
// be wrong: one player is revealed to, the others must keep the descriptive name across a
// restart. Their tokens all stand west of the x=700 wall and see the same two subjects.
const alice = await createPlayer(gm.ws)
await namePlayer(origin, alice.key, 'Alice')
saved.alice = alice
// A second, still unnamed player: the GM roster carries it (`name:null`), players never see it.
const bob = await createPlayer(gm.ws)
await namePlayer(origin, bob.key, 'Bob')
saved.bob = bob
const carol = await createPlayer(gm.ws)
await namePlayer(origin, carol.key, 'Carol')
saved.carol = carol
// A fourth, still unnamed player: the GM roster carries it (`name:null`), players never see it.
await createPlayer(gm.ws)
// A wall and a door; the door is opened (door.set is its own write path).
const wall = await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [700, 0],
@@ -49,6 +66,17 @@ async function buildWorld(origin, gmKey) {
props: { public: { aura: 'cold' }, owner: { hp: 5 },
gm: { secret: 1 } } })
assert.equal(pc.ok, true, JSON.stringify(pc))
// Bob and Carol: sight of their own, at the same subjects. They never own a character, so only a
// reveal can tell them an actual name.
for (const [p, pos] of [[bob, [250, 500]], [carol, [300, 650]]]) {
const sight = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos, descriptive_name: 'sight', actual_name: 'Sight',
controller: p.id, flags: { vision: true },
radius: 35 })
assert.equal(sight.ok, true, JSON.stringify(sight))
if (p === bob) saved.ids = { ...(saved.ids ?? {}), bobTok: sight.id }
else saved.ids = { ...(saved.ids ?? {}), carolTok: sight.id }
}
// A monster right next to it: Alice meets it (one encountered entry, grey after it moves away).
const monster = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [500, 400], descriptive_name: 'a goblin',
@@ -77,13 +105,17 @@ async function buildWorld(origin, gmKey) {
assert.equal((await gm.ws.request({ t: 'status.set', id: monster.id,
status: 'stuck' })).ok, true)
assert.equal((await gm.ws.request({ t: 'token.resize', id: monster.id, radius: 28 })).ok, true)
// A reveal for one player, reveal-all for a plain token, one encountered entry removed later.
// Per-player reveals: the character's actual name to Alice only, the monster's to Bob only.
// Alice must then keep "a goblin" and Bob "a cloaked figure" - before the fix both flipped to
// the actual name after a restart (#27). reveal-all for a plain token stays all-players.
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'character',
id: character.id, player: alice.id })).ok, true)
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'token',
id: monster.id, player: bob.id })).ok, true)
assert.equal((await gm.ws.request({ t: 'name.reveal_all', subject: 'token',
id: pc.id })).ok, true)
saved.ids = { pc: pc.id, monster: monster.id, character: character.id, linked: linked.id,
wall: wall.id, door: door.id }
saved.ids = { ...saved.ids, pc: pc.id, monster: monster.id, character: character.id,
linked: linked.id, wall: wall.id, door: door.id }
gm.ws.close()
} finally {
try { gm.ws.close() } catch {}
@@ -150,29 +182,50 @@ async function snapshot(origin, key, expected, isGm) {
}
}
// The full before/after comparison: one fresh GM connection and one fresh Alice connection.
async function compareAcross(origin, gmKey, aliceKey, ids, expected) {
// The name each player may know for a subject, from the fixture's own truth: the character reveal
// went to Alice only, the monster's to Bob only, and the pc's reveal_all covers everyone.
function expectedEncounteredName(who, ids) {
if (who === 'alice') return { [ids.monster]: 'a goblin', [ids.linked]: 'Lady Vex',
[ids.pc]: 'Wanderer' }
return { [ids.monster]: who === 'bob' ? 'Grib' : 'a goblin',
[ids.linked]: 'a cloaked figure', [ids.pc]: 'Wanderer' }
}
// The full before/after comparison: one fresh GM connection and one fresh connection per player.
async function compareAcross(origin, gmKey, players, ids, expected) {
const beforeGm = await snapshot(origin, gmKey, expected, true)
const beforeAlice = await snapshot(origin, aliceKey, expected, false)
const before = { gm: beforeGm }
for (const [who, p] of Object.entries(players)) {
// Only the character's owner is sent a `character` record (6.1).
before[who] = await snapshot(origin, p.key,
{ ...expected, characters: who === 'alice' ? 1 : 0 }, false)
}
// The CRUD inventory is not empty: a restart test over an empty world would prove nothing.
assert.ok(beforeGm.walls.walls.length >= 2, 'the wall and the door are there')
assert.equal(beforeGm.walls.walls.find(w => w.id === ids.door).open, true, 'the door is open')
assert.ok(beforeGm.tokens.length >= 3, 'plain, monster, player and linked tokens are there')
assert.ok(beforeGm.tokens.length >= 5, 'plain, monster, player, linked and sight tokens are there')
assert.ok(beforeGm.characters.length >= 1, 'the character is there')
assert.equal(beforeGm.characters.find(c => c.id === ids.character).props.public.banner, 'red')
assert.equal(beforeGm.tokens.find(t => t.id === ids.monster).radius, 28,
'the resize survived')
assert.ok(beforeGm.tokens.find(t => t.id === ids.monster).statuses
.some(s => s.status === 'stuck' && s.source === 'GM'), 'the GM status survived')
assert.equal(beforeAlice.encountered.set.find(e => e.id === ids.monster)?.name, 'a goblin')
assert.ok(beforeAlice.vision.polys.length >= 1, 'the player has sight')
assert.ok(beforeAlice.explored.rle.length >= 1, 'the player explored something')
return { beforeGm, beforeAlice }
// #27: each player's encountered list holds the name *that* player knows, live.
for (const [who, snap] of Object.entries(before)) {
if (who === 'gm') continue
for (const [id, name] of Object.entries(expectedEncounteredName(who, ids))) {
assert.equal(snap.encountered.set.find(e => e.id === id)?.name, name,
`${who}'s encountered entry for ${id} live`)
}
assert.ok(snap.vision.polys.length >= 1, `${who} has sight`)
assert.ok(snap.explored.rle.length >= 1, `${who} explored something`)
}
return before
}
async function restartAndCompare(stateDir, gmKey, aliceKey, ids, expected, hard) {
async function restartAndCompare(stateDir, gmKey, players, ids, expected, hard) {
let s = await startServer({ stateDir })
const before = await compareAcross(s.origin, gmKey, aliceKey, ids, expected)
const before = await compareAcross(s.origin, gmKey, players, ids, expected)
if (hard) {
// Every ack is sent after COMMIT (4.2), so a hard kill cannot lose an acked mutation.
s.child.kill('SIGKILL')
@@ -186,47 +239,163 @@ async function restartAndCompare(stateDir, gmKey, aliceKey, ids, expected, hard)
s = await startServer({ stateDir })
try {
const afterGm = await snapshot(s.origin, gmKey, expected, true)
const afterAlice = await snapshot(s.origin, aliceKey, expected, false)
assert.deepEqual(afterGm, before.beforeGm, 'the GM resync after the restart is identical')
assert.deepEqual(afterAlice, before.beforeAlice,
'the player resync after the restart is identical')
assert.deepEqual(afterGm, before.gm, 'the GM resync after the restart is identical')
for (const [who, p] of Object.entries(players)) {
const after = await snapshot(s.origin, p.key,
{ ...expected, characters: who === 'alice' ? 1 : 0 }, false)
assert.deepEqual(after, before[who],
`${who}'s resync after the restart is identical`)
// ... and the absolute truth, so two equally wrong snapshots cannot pass (#27).
for (const [id, name] of Object.entries(expectedEncounteredName(who, ids))) {
assert.equal(after.encountered.set.find(e => e.id === id)?.name, name,
`${who}'s encountered entry for ${id} after the restart`)
}
}
} finally {
await stopServer(s)
}
}
const EXPECTED = { tokens: 3, characters: 1 }
// The persisted snapshot is where #27 lived: assert directly that the reveal changed exactly one
// player's row for the revealed subject, and nobody else's.
function dbEncounteredNames(stateDir, entityId) {
const r = spawnSync('sqlite3', [path.join(stateDir, 'state.db'),
`SELECT player_id || '=' || IFNULL(actual_name, '') FROM encountered ` +
`WHERE entity_id = '${entityId}' ORDER BY player_id`], { encoding: 'utf8' })
assert.equal(r.status, 0, r.stderr)
return Object.fromEntries(r.stdout.trim().split('\n').filter(Boolean)
.map(line => { const [id, name] = line.split('='); return [id, name] }))
}
test('restart: full state, all CRUD, before and after SIGTERM', async () => {
const EXPECTED = { tokens: 5, characters: 1 }
async function restartGroup(hard) {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const players = {}
let s = await startServer({ stateDir })
let saved
try {
saved = await buildWorld(s.origin, gmKey)
players.alice = saved.alice
players.bob = saved.bob
players.carol = saved.carol
// #27, asserted against the rows while the server is still up: the monster's actual name was
// revealed to Bob only, so exactly one of the three rows carries it.
const monsterRows = dbEncounteredNames(stateDir, saved.ids.monster)
assert.equal(monsterRows[String(saved.bob.id)], 'Grib')
assert.equal(monsterRows[String(saved.alice.id)], '')
assert.equal(monsterRows[String(saved.carol.id)], '',
'the reveal to Bob did not write Alice\'s or Carol\'s row (#27)')
const linkedRows = dbEncounteredNames(stateDir, saved.ids.linked)
assert.equal(linkedRows[String(saved.alice.id)], 'Lady Vex')
assert.equal(linkedRows[String(saved.bob.id)], '')
assert.equal(linkedRows[String(saved.carol.id)], '',
'the reveal to Alice did not write Bob\'s or Carol\'s row (#27)')
} finally {
await stopServer(s)
}
try {
await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, false)
await restartAndCompare(stateDir, gmKey, players, saved.ids, EXPECTED, hard)
} finally {
fs.rmSync(stateDir, { recursive: true, force: true })
}
}
test('restart: full state, all CRUD, before and after SIGTERM', async () => {
await restartGroup(false)
})
test('restart: full state, all CRUD, before and after SIGKILL', async () => {
await restartGroup(true)
})
// #30: a rename must reach the live encountered list, not just the row a restart loads. The wire
// and the DB are asserted to agree before and after a restart, for a plain token and a character,
// and for a player who was never told the actual name.
test('restart: a mid-session rename reaches the live encountered list (#30)', async () => {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
let s = await startServer({ stateDir })
let saved
let ids = {}
let alice, bob
try {
saved = await buildWorld(s.origin, gmKey)
const gm = await gmConnect(s.origin, gmKey)
alice = await createPlayer(gm.ws)
await namePlayer(s.origin, alice.key, 'Alice')
bob = await createPlayer(gm.ws)
await namePlayer(s.origin, bob.key, 'Bob')
for (const [p, pos] of [[alice, [350, 350]], [bob, [300, 650]]]) {
const t = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos,
descriptive_name: 'sight', actual_name: 'Sight',
controller: p.id, flags: { vision: true } })
assert.equal(t.ok, true, JSON.stringify(t))
}
const wolf = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [450, 450], descriptive_name: 'a wolf',
actual_name: 'Fang' })
const char = await gm.ws.request({ t: 'character.create',
descriptive_name: 'a cloaked figure',
actual_name: 'Lady Vex', owner: alice.id })
const linked = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [500, 400], character_id: char.id })
assert.ok(wolf.ok && char.ok && linked.ok)
ids = { wolf: wolf.id, char: char.id, linked: linked.id }
// Both players are connected while the rename happens, so the live frame is what is checked.
const a = await openAs(s.origin, alice.key)
await a.ws.waitText('hello')
const b = await openAs(s.origin, bob.key)
await b.ws.waitText('hello')
await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id && e.name === 'a wolf'))
await b.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id && e.name === 'a wolf'))
let from = a.ws.messages.length
const w = await gm.ws.request({ t: 'token.update', id: wolf.id,
descriptive_name: 'a huge wolf' })
assert.equal(w.ok, true, JSON.stringify(w))
const renamed = await a.ws.waitFor((m, i) => i >= from && m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id))
assert.equal(renamed.set.find(e => e.id === wolf.id).name, 'a huge wolf',
'the live list follows a plain-token rename (#30)')
await b.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id && e.name === 'a huge wolf'))
const bFrom = b.ws.messages.length
const c = await gm.ws.request({ t: 'character.update', id: char.id,
descriptive_name: 'a tall stranger' })
assert.equal(c.ok, true, JSON.stringify(c))
// Alice owns the character, so she sees the actual name and the descriptive rename is invisible
// to her; Bob (never told) must see the new descriptive name on the wire, with no restart.
const moved = await b.ws.waitFor((m, i) => i >= bFrom && m.t === 'encountered' &&
m.set.some(e => e.id === linked.id))
assert.equal(moved.set.find(e => e.id === linked.id).name, 'a tall stranger',
'the live list follows a character rename for a player who was not told (#30)')
assert.equal(renamed.set.find(e => e.id === wolf.id).name, 'a huge wolf')
a.ws.close(); b.ws.close(); gm.ws.close()
} finally {
await stopServer(s)
}
try {
await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, true)
s = await startServer({ stateDir })
for (const [key, who] of [[alice.key, 'alice'], [bob.key, 'bob']]) {
const c = await openAs(s.origin, key)
try {
await c.ws.waitFor(m => m.t === 'encountered' && m.full === true)
const e = c.ws.messages.filter(m => m.t === 'encountered').pop()
assert.equal(e.set.find(x => x.id === ids.wolf)?.name, 'a huge wolf',
`${who}: the plain-token rename survives the restart`)
assert.equal(e.set.find(x => x.id === ids.linked)?.name,
who === 'alice' ? 'Lady Vex' : 'a tall stranger',
`${who}: the character rename survives the restart, per player`)
} finally {
c.ws.close()
}
}
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
})