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
+140 -2
View File
@@ -32,6 +32,11 @@ proc plainToken(id = "tok1", owner = 0): TokenRec =
TokenRec(id: id, scene: 1, kind: "npc", descriptiveName: "a wolf", actualName: "Fang",
owner: owner, flags: %*{"vision": true}, props: newProps())
proc sceneOf(): SceneRec =
SceneRec(id: 1, name: "Scene", w: 4096, h: 4096, tileSize: 32, cell: 32, gridSize: 0,
visionRadiusDefault: 400, tokenRadiusDefault: 20, artVersion: 1, nextWallId: 1,
extra: newJObject(), walls: @[])
suite "the name rule (DESIGN 8: one copy, used by every frame)":
test "an owner, a reveal_all and a per-player reveal each expose the actual name":
check knowsActual(isOwner = true, revealAll = false, revealed = false)
@@ -150,6 +155,30 @@ suite "names and validation (6.5: 1..64 code points, trimmed, no control charact
t.actualName = ""
check validToken(sc, t)
test "linking through a patch hands the names over (#29)":
# `patched` is the one place a link patch is applied, so a plain token's own names go away with
# it: validation would otherwise fail every link attempt, and only unlinking would work.
let sc = sceneOf()
var t = plainToken()
check validToken(sc, t)
let linked = t.patched(TokenPatch(characterId: some(some("char1"))))
check linked.characterId == some("char1")
check linked.descriptiveName.len == 0
check linked.actualName.len == 0
check validToken(sc, linked)
# unlinking needs both names on the patch and restores them
let unlinked = linked.patched(TokenPatch(characterId: some(none(string)),
descriptiveName: some("a wolf"),
actualName: some("Fang")))
check unlinked.characterId.isNone
check unlinked.descriptiveName == "a wolf"
check unlinked.actualName == "Fang"
check validToken(sc, unlinked)
# a name patch on a plain token still applies
let renamed = t.patched(TokenPatch(descriptiveName: some("a huge wolf")))
check renamed.descriptiveName == "a huge wolf"
check renamed.actualName == "Fang"
test "parseSubject accepts exactly the two wire words":
check parseSubject("character").get == nsCharacter
check parseSubject("token").get == nsToken
@@ -267,6 +296,103 @@ suite "persistence (write-through, restart-identical)":
check not deleteEncountered(conn, created.id, "t1")
check loadEncountered(conn)[created.id].len == 1
test "a character rename rewrites the live snapshots and the rows alike (#30, #27)":
# The bug: `character.update` rewrote SQLite only, so the live encountered frame kept the old
# name until a restart. One helper now rewrites both stores from the 8 name rule.
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let p1 = createPlayerWithKey(conn)
let p2 = createPlayerWithKey(conn)
let p3 = createPlayerWithKey(conn)
var enc = loadEncountered(conn)
for pid in [p1.id, p2.id, p3.id]:
enc.mgetOrPut(pid, initTable[string, EncounteredEntry]())["t1"] =
EncounteredEntry(entityId: "t1", characterId: some("char1"),
descriptiveName: "a grumpy man",
actualName: (if pid == p1.id: some("Lord Vex") else: none(string)))
writeEncountered(conn, pid, enc[pid]["t1"])
var reveals: Reveals
reveals.withReveal(p1.id, "char1") # p1 was told; p2 and p3 were not
let changed = rewriteEncountered(conn, enc, nsCharacter, "char1",
SubjectNames(id: "char1", descriptive: "a tall stranger",
actual: "Lady Vex", revealAll: false,
owner: 0, characterId: some("char1")),
reveals)
# every player's entry follows, and the memory equals a reload (no restart cliff)
check changed.len == 3
let loaded = loadEncountered(conn)
for pid in [p1.id, p2.id, p3.id]:
check enc[pid]["t1"].descriptiveName == "a tall stranger"
check loaded[pid]["t1"] == enc[pid]["t1"]
check enc[p1.id]["t1"].entryName == "Lady Vex"
check enc[p2.id]["t1"].actualName.isNone # never told, still not told
check enc[p2.id]["t1"].entryName == "a tall stranger"
test "a reveal to one player rewrites exactly that player's row (#27)":
# The leak: the reveal SQL had no `player_id` predicate, so every player who met the subject got
# the actual name persisted and saw it after a restart.
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let p1 = createPlayerWithKey(conn)
let p2 = createPlayerWithKey(conn)
let p3 = createPlayerWithKey(conn)
var enc: Encountered
for pid in [p1.id, p2.id, p3.id]:
enc.mgetOrPut(pid, initTable[string, EncounteredEntry]())["t1"] =
EncounteredEntry(entityId: "t1", characterId: none(string),
descriptiveName: "a wolf", actualName: none(string))
writeEncountered(conn, pid, enc[pid]["t1"])
var reveals: Reveals
reveals.withReveal(p2.id, "t1")
let changed = rewriteEncountered(conn, enc, nsToken, "t1",
SubjectNames(id: "t1", descriptive: "a wolf", actual: "Fang",
revealAll: false, owner: 0,
characterId: none(string)),
reveals, onlyPlayer = p2.id)
check changed == @[(p2.id, "t1")]
let loaded = loadEncountered(conn)
check loaded[p1.id]["t1"].actualName.isNone
check loaded[p3.id]["t1"].actualName.isNone
check loaded[p2.id]["t1"].entryName == "Fang"
check loadEncountered(conn)[p1.id]["t1"].entryName == "a wolf" # the other players' rows
# and memory agrees with the rows for every player
for pid in [p1.id, p2.id, p3.id]: check enc[pid]["t1"] == loaded[pid]["t1"]
test "a link change moves the snapshot to the character's names, link and knowledge (#29)":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let p1 = createPlayerWithKey(conn)
var enc: Encountered
enc.mgetOrPut(p1.id, initTable[string, EncounteredEntry]())["t1"] =
EncounteredEntry(entityId: "t1", characterId: none(string), descriptiveName: "a wolf",
actualName: some("Fang"))
writeEncountered(conn, p1.id, enc[p1.id]["t1"])
var reveals: Reveals
# the plain token is linked to a character p1 has never been told about: the token-level actual
# name must not survive the handover
check rewriteEncountered(conn, enc, nsToken, "t1",
SubjectNames(id: "char1", descriptive: "a cloaked figure",
actual: "Lady Vex", revealAll: false, owner: 0,
characterId: some("char1")), reveals) ==
@[(p1.id, "t1")]
check enc[p1.id]["t1"].characterId == some("char1")
check enc[p1.id]["t1"].entryName == "a cloaked figure"
check loadEncountered(conn)[p1.id]["t1"] == enc[p1.id]["t1"]
# unlinking moves it back and drops the character link
check rewriteEncountered(conn, enc, nsToken, "t1",
SubjectNames(id: "t1", descriptive: "a wolf", actual: "Fang",
revealAll: false, owner: 0,
characterId: none(string)),
reveals).len == 1
check enc[p1.id]["t1"].characterId.isNone
check enc[p1.id]["t1"].entryName == "a wolf"
test "a character rename follows the snapshots of everyone who met it":
let (conn, d) = freshDb()
defer:
@@ -280,13 +406,25 @@ suite "persistence (write-through, restart-identical)":
writeEncountered(conn, p2.id, EncounteredEntry(entityId: "t1", characterId: some("char1"),
descriptiveName: "a grumpy man",
actualName: some("Lord Vex")))
renameEncountered(conn, "char1", "a tall stranger", "Lady Vex")
var enc = loadEncountered(conn)
var reveals: Reveals
reveals.withReveal(p2.id, "char1")
discard rewriteEncountered(conn, enc, nsCharacter, "char1",
SubjectNames(id: "char1", descriptive: "a tall stranger",
actual: "Lady Vex", revealAll: false, owner: 0,
characterId: some("char1")), reveals)
let loaded = loadEncountered(conn)
check loaded[p1.id]["t1"].entryName == "a tall stranger" # still not told the actual name
check loaded[p1.id]["t1"].actualName.isNone
check loaded[p2.id]["t1"].entryName == "Lady Vex"
revealEncountered(conn, "char1", "Lady Vex")
# a later all-players reveal fills in exactly the ones who were not told
reveals.withReveal(p1.id, "char1")
discard rewriteEncountered(conn, enc, nsCharacter, "char1",
SubjectNames(id: "char1", descriptive: "a tall stranger",
actual: "Lady Vex", revealAll: false, owner: 0,
characterId: some("char1")), reveals)
check loadEncountered(conn)[p1.id]["t1"].entryName == "Lady Vex"
check enc[p1.id]["t1"].entryName == "Lady Vex"
test "player.remove hands the player's characters to the GM (DESIGN 3.2)":
let (conn, d) = freshDb()