server: scene.set closes the 29-intent catalogue, plus a log-leak fault hook (#25)

This commit is contained in:
2026-11-15 12:52:17 +01:00
parent df2b186267
commit 70a984c55a
7 changed files with 453 additions and 11 deletions
+75
View File
@@ -27,6 +27,9 @@
## server/tiles.nim: a strict canonical URL, the player's explored rule (the GM always), `404` for a
## tile that does not exist and a bodyless `200` with `X-Accel-Redirect` to the internal `/_tiles/`
## location, which nginx alone can serve (DESIGN 3.3, 6.6, 11.1).
## M3 slice 10 (#25): the finish line - `scene.set` (the 29th catalogue intent: the settable scene
## fields, the token-radius-default resize through 5.6, and a hello + full resync of every
## connection), plus the restart and logs acceptance groups and the group checklist.
## Close reasons (FIXED, owner decision #18): mummy 0.4.8 cannot put a code in the close frame, so
## every server-initiated close is preceded by a `closing {reason, code}` message and the client
## reads 1005 + that message (DESIGN 6.5).
@@ -285,6 +288,9 @@ proc sendHello(connId: int, player: Player) =
## The `hello` frame (6.1): the first frame on a connection, and again after `player.setname`
## (which is why it lives in a proc both paths call). Keyed by the app connId so a caller holding
## only the id (the upgrade handler) and one holding the record (the fan-out) share it.
## A `hello` tells the client to reset everything (6.1), so the server's diff base for that
## connection resets with it: without this, a `scene.set` resync would skip every unchanged token
## and the client would hold an empty world. `resync` runs right after every `hello`.
let named = player.name.isSome
let me = if named: %player.name.get else: newJNull()
var sceneJson: JsonNode
@@ -294,6 +300,7 @@ proc sendHello(connId: int, player: Player) =
let helloN = app.conns.nextFrameNo(connId)
let found = app.conns.connById(connId)
if helloN <= 0 or found.isNone: return
app.known.del connId
found.get.ws.send($helloFrame(ProtoVersion, helloN, $player.role, player.id, me, sceneJson,
extraJson))
@@ -332,6 +339,16 @@ proc resync(connId: int, named: bool, player: Player) =
let n = nextN(conn)
if n > 0: conn.ws.send($withN(f, n))
proc resyncEveryone() =
## `scene.set` (6.1): `hello` plus a full resync to every connection, unnamed players included
## (they get `hello` and nothing else, 6.3). The scene block in `hello` may have changed, so even
## a connection that has no state yet is told. Caller holds the global lock.
for c in app.conns.snapshot():
let p = playerById(app.db, c.playerId)
if p.isSome:
sendHello(c.id, p.get)
resync(c.id, c.named, p.get)
proc settle(conn: Conn, reqId: int, extra: JsonNode = nil, roster = false) =
## 4.2 step 7 for a mutation: the ack and then every recipient's diff are enqueued under the global
## lock, so no two threads touch a connection's `known` table at once and the ack always precedes
@@ -554,6 +571,12 @@ proc join(request: Request) {.gcsafe.} =
app.lg.log(lvInfo, "join_refused")
request.respond(401, body = "")
return
if faultActive("keylog"):
# Test-only negative control for the "logs" acceptance group (#25): write the raw secret to
# stdout, bypassing `eventlog` on purpose, so the group's grep must find it in a fault build.
# Compiled out of the shipped binary (faults.nim).
stdout.write("FAULT keylog key=" & key & "\n")
stdout.flushFile()
# The token is deliberately never registered for redaction: sessions never expire, so a value
# registry would grow without bound. It is only ever written to the Set-Cookie header; if a future
@@ -1731,6 +1754,58 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
settle(conn, env.reqId)
return
of "scene.set":
# DESIGN 6.1, slice 10 / #25: a hello-equivalent reset of every connection (the client resets on
# `hello`, 6.1) after a GM edit of the settable scene fields. `w`, `h`, `tile_size`, `cell` and
# `art_version` are not patch fields (import is the M6 CLI) and are ignored as unknown fields
# (6.2); a patch carrying only those has no field and is `invalid`.
if not sceneOk(conn, env): return
let patch = parseScenePatch(env.node)
if patch.isNone:
ackErr(conn, env.reqId, CodeInvalid)
return
var candidate = app.scene.patched(patch.get)
var changed: seq[string]
if patch.get.name.isSome: changed.add "name"
if patch.get.gridSize.isSome: changed.add "grid_size"
if patch.get.visionRadiusDefault.isSome: changed.add "vision_radius_default"
if patch.get.tokenRadiusDefault.isSome: changed.add "token_radius_default"
if patch.get.extra.isSome: changed.add "extra"
var tokenChanges: seq[(int, TokenRec)]
withLock app.dbLock:
# 5.6 `token_radius_default`: every token that uses the default changes effective size; a grow
# pushes (or squeezes), a shrink clears the `walls` entries only when the overlap ends. The
# rules run on a snapshot with the OLD radii (the candidate default is not in `app.scene` yet),
# then one FULL transaction writes the scene row and every token it resized.
if candidate.tokenRadiusDefault != app.scene.tokenRadiusDefault:
var mr = moveScene(app.scene, app.tokens)
for i, t in app.tokens:
if t.radius.isSome: continue # an own radius is not the default (#4)
discard moverules.resizeToken(mr, i, candidate.tokenRadiusDefault)
var rec = app.tokens[i]
let outcome = rec.applyOutcome(mr.tokens[i])
if outcome.moved or outcome.statuses: tokenChanges.add (i, rec)
app.db.exec(sql"PRAGMA synchronous=FULL")
try:
app.db.withTransaction:
writeSceneRow(app.db, candidate)
for (_, rec) in tokenChanges:
discard writePosition(app.db, rec)
writeStatuses(app.db, rec)
finally:
app.db.exec(sql"PRAGMA synchronous=NORMAL")
app.scene = candidate
for (i, rec) in tokenChanges: app.tokens[i] = rec
refreshAll()
persistAllExplored()
recordEncounters()
ackOk(conn, env.reqId)
resyncEveryone()
app.lg.log(lvInfo, "scene_set", [("scene", $candidate.id),
("fields", changed.join(",")),
("tokens_resized", $tokenChanges.len)])
return
of "wall.add":
if not sceneOk(conn, env): return
let a = parseVec(fieldOrNull(env.node, "a"))