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
+45 -18
View File
@@ -28,8 +28,9 @@
##
## This module does no locking (the caller holds the global lock, 4.3) and no wire shapes (that is
## server/views.nim); it owns the records and their SQLite rows.
import std/[algorithm, json, options, sets, strutils, tables, times]
import std/[algorithm, json, options, sequtils, sets, strutils, tables, times]
import db_connector/db_sqlite
import db
import props, text, tokens, world
type
@@ -183,6 +184,24 @@ proc revealed*(e: EncounteredEntry, actual: string): EncounteredEntry =
result = e
result.actualName = some(actual)
proc refreshed*(e: EncounteredEntry, n: SubjectNames, knows: bool): EncounteredEntry =
## The snapshot after any change the name rule can see: the names and the character link are the
## subject's now, and the actual name is exactly what this player may know (`knows`, from 8).
## Recomputing it instead of patching the old value is what keeps DB and memory in step (#30):
## a rename keeps a not-told player not-told, a reveal fills the one player in, a link change moves
## the snapshot to the character's names and link, and an unlink drops the link again.
EncounteredEntry(entityId: e.entityId, characterId: n.characterId,
descriptiveName: n.descriptive,
actualName: (if knows: some(n.actual) else: none(string)))
proc entryMatches*(e: EncounteredEntry, subject: NameSubject, subjectId: string): bool =
## Does this entry snapshot the subject? A character's entries are the ones that carry its link;
## a plain token's are the ones keyed by its own id (they may carry a character link after a
## later join, which is why the character test cannot be `entityId == subjectId`).
case subject
of nsCharacter: e.characterId == some(subjectId)
of nsToken: e.entityId == subjectId
# ---------------------------------------------------------------------------------------------------
# Persistence (write-through; GM authoring runs in the caller's FULL transaction, 4.4/Q12)
@@ -271,24 +290,32 @@ proc deleteEncountered*(db: DbConn, player: int, entityId: string): bool =
db.execAffectedRows(sql"DELETE FROM encountered WHERE player_id = ? AND entity_id = ?",
player, entityId) > 0
proc renameEncountered*(db: DbConn, characterId: string, descriptive, actual: string) =
## `character.update`: every snapshot of this character follows the new names. A player who was
## never told the actual name keeps not knowing it (an unknown actual name is stored as `''`).
db.exec(sql"""UPDATE encountered SET descriptive_name = ? WHERE character_id = ?""",
descriptive, characterId)
db.exec(sql"""UPDATE encountered SET actual_name = ? WHERE character_id = ?
AND actual_name <> ''""", actual, characterId)
proc rewriteEncountered*(db: DbConn, enc: var Encountered, subject: NameSubject, subjectId: string,
n: SubjectNames, reveals: Reveals, onlyPlayer = 0):
seq[(int, string)] =
## The one path that keeps `encountered` in step with its subject (#27, #29, #30). Given the
## subject's current names (`n`) it rewrites every matching snapshot - SQLite row and in-memory
## entry together - so a restart cannot disagree with the live wire, and it applies the 8 name rule
## per player (`reveals`) so the actual name is set exactly for the players allowed to know it.
## `onlyPlayer` = 0 means every player; any other value scopes the whole rewrite to that player
## (`name.reveal`), which is why the old subject-only SQL leaked the actual name to everyone.
## The persistence is NORMAL (a snapshot is derived data, Q12). The caller holds the global lock.
var changes: seq[(int, EncounteredEntry)]
for pid in enc.keys.toSeq:
if onlyPlayer != 0 and pid != onlyPlayer: continue
if not enc.hasKey(pid): continue
for entityId, e in enc[pid]:
if not e.entryMatches(subject, subjectId): continue
let updated = e.refreshed(n, knowsActual(n.owner == pid and n.owner > 0, n.revealAll,
reveals.isRevealed(pid, n.id)))
if updated != e: changes.add (pid, updated)
if changes.len == 0: return
db.withTransaction:
for (pid, e) in changes: writeEncountered(db, pid, e)
for (pid, e) in changes:
enc[pid][e.entityId] = e
result.add (pid, e.entityId)
proc revealEncountered*(db: DbConn, characterId: string, actual: string) =
db.exec(sql"UPDATE encountered SET actual_name = ? WHERE character_id = ?", actual, characterId)
proc revealEncounteredToken*(db: DbConn, entityId: string, actual: string) =
db.exec(sql"UPDATE encountered SET actual_name = ? WHERE entity_id = ?", actual, entityId)
proc renameEncounteredToken*(db: DbConn, entityId, descriptive, actual: string) =
db.exec(sql"UPDATE encountered SET descriptive_name = ? WHERE entity_id = ?", descriptive, entityId)
db.exec(sql"""UPDATE encountered SET actual_name = ? WHERE entity_id = ?
AND actual_name <> ''""", actual, entityId)
# ---------------------------------------------------------------------------------------------------
# Loading (4.4 startup: load everything into memory once)