fix: marker_seen deletes commit with the wall change
A wall cause change (endpoint move, thickness/position, player_marker off, no longer window) committed the wall row in the FULL transaction while its marker_seen deletes rode the later NORMAL derived-memory transaction. A crash between them left the stale row, and restart restored the marker with the anchor recomputed from the NEW geometry, revealing a position the player never saw (DESIGN 5.8). updateWall now deletes the affected rows in the same FULL transaction as the replaced row, exactly as the composite FK cascade already does for wall.remove. The live del frames are unchanged and marker inserts stay in the derived transaction: wall+deletes commit, derived inserts, then ack. The marker cause signature moves to world.markerSig so the write-time delete and the online retraction cannot drift. Proof: a leakfault-only markerCrash hook quits between the wall commit and the derived write; tools/markers.test.mjs covers all four causes and restarts on a clean build, asserting no marker for the changed wall (no new anchor) and restoring the untouched window's marker as the positive control. Ref #60
This commit is contained in:
@@ -12,3 +12,9 @@ proc faultActive*(name: string): bool =
|
||||
getEnv("LEAK_FAULT") == name
|
||||
else:
|
||||
false
|
||||
|
||||
proc crashNow*() =
|
||||
## `-d:leakfault` only: die at a chosen point with no shutdown, modelling a hard kill between two
|
||||
## transactions (the `markerCrash` hook, TESTING 10.3, #60). A shipped build has no death path.
|
||||
when defined(leakfault):
|
||||
quit(70)
|
||||
|
||||
+12
-7
@@ -232,7 +232,8 @@ proc recordEncounters() =
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------
|
||||
# Derived player memory persistence: explored cells and player markers (DESIGN 4.2 step 6, 4.4, 5.8;
|
||||
# M3 slice 6 / #21, #59). Both live in memory and reach the DB only here. One rule: a mutation writes
|
||||
# M3 slice 6 / #21, #59). Both live in memory and reach the DB here, except a marker cause-change
|
||||
# delete, which `world.updateWall` commits with the wall row (5.8, #60). One rule: a mutation writes
|
||||
# exactly the explored chunks its refresh dirtied and the markers it minted, never the whole state,
|
||||
# and the write rides in the mutation's transaction so an ack cannot precede what it reports. NORMAL
|
||||
# durability (Q12): both are derived (explored only grows; a marker is minted and retracted by the
|
||||
@@ -254,8 +255,8 @@ proc persistMarkers(pids: openArray[int]) =
|
||||
## The marker half alone, for a connect where the explored grid must stay exactly as it was (a
|
||||
## schema-4/7 database rebuilds its grid on connect and stores nothing until a mutation): a marker
|
||||
## minted at the resync is still durable before it is sent (5.8, 4.2 step 6). The deletes run
|
||||
## before the inserts: a moved wall's old marker must be gone before its fresh one is stored, and
|
||||
## both are the same derived-memory transaction.
|
||||
## before the inserts: a moved wall's old rows are already gone (the wall transaction took them),
|
||||
## and a fresh one is stored after them, so one id can never be set and deleted in one refresh.
|
||||
for pid in pids:
|
||||
if pid == GmControlled: continue
|
||||
discard markers.writeDead(app.db, pid,
|
||||
@@ -264,10 +265,10 @@ proc persistMarkers(pids: openArray[int]) =
|
||||
app.visions.mgetOrPut(pid, PlayerVision()).markerFresh)
|
||||
|
||||
proc persistDerived(pids: openArray[int]) =
|
||||
## Writes the dirtied explored chunks and the markers the same refresh minted or retracted (5.8:
|
||||
## marker inserts and deletes are in the same derived-memory transaction as the explored chunks,
|
||||
## after the wall row commits). No transaction of its own: the caller wraps it together with the
|
||||
## mutation's row writes.
|
||||
## Writes the dirtied explored chunks and the markers the same refresh minted (5.8: marker inserts
|
||||
## ride the same derived-memory transaction as the explored chunks, after the wall row committed;
|
||||
## a cause-change delete already committed with that row). No transaction of its own: the caller
|
||||
## wraps it together with the mutation's row writes.
|
||||
let chunks = dirtyExplored(pids)
|
||||
if chunks.len > 0: discard explored.writeChunks(app.db, app.scene.id, chunks)
|
||||
persistMarkers(pids)
|
||||
@@ -2151,6 +2152,10 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
|
||||
var mr = moveScene(app.scene, app.tokens)
|
||||
outcome = updateWall(app.db, app.scene, candidate.get)
|
||||
if outcome == waOk:
|
||||
when defined(leakfault):
|
||||
# LEAK_FAULT=markerCrash (TESTING 10.3, #60): die between the wall commit and the
|
||||
# derived-memory transaction, to prove the marker deletes commit with the wall row.
|
||||
if faultActive("markerCrash"): crashNow()
|
||||
let updated = wallById(app.scene, id)
|
||||
discard moverules.updateWall(mr, idx, ruleWall(updated.get))
|
||||
discard applyRuleOutcome(mr)
|
||||
|
||||
+12
-13
@@ -9,9 +9,10 @@
|
||||
##
|
||||
## The marker is per-player memory like explored: it is minted once, gets that player's own opaque
|
||||
## 12-character id (never the wall id), keeps the wall's drawn midpoint rounded once as its anchor,
|
||||
## and is persisted in `marker_seen` as part of the same derived-memory transaction as the explored
|
||||
## chunks (4.2 step 6). `vision.PlayerVision.markers` holds it in memory and `restore` loads it at
|
||||
## startup, before the first connection (4.4).
|
||||
## and is persisted in `marker_seen`: a cause-change retraction's delete commits with the wall row
|
||||
## itself (so a crash cannot restore it), while a mint rides the derived-memory transaction with the
|
||||
## explored chunks (4.2 step 6). `vision.PlayerVision.markers` holds it in memory and `restore` loads
|
||||
## it at startup, before the first connection (4.4).
|
||||
##
|
||||
## Lifetime (5.8, #60): walking away, a door and lost sight never retract a marker. The GM's causes
|
||||
## do: removing the wall, clearing `player_marker`, making the wall stop deriving `window`, or
|
||||
@@ -32,11 +33,9 @@ proc anchorOf*(w: WallRec): (int, int) =
|
||||
(int(round((w.a.x + w.b.x) / 2.0)), int(round((w.a.y + w.b.y) / 2.0)))
|
||||
|
||||
proc wallSig(w: WallRec): string =
|
||||
## Everything whose change retracts a marker (5.8, #60): endpoints, thickness, placement, the
|
||||
## `player_marker` switch and the derived `window` gate. Height and label have no marker
|
||||
## consequence and are deliberately absent.
|
||||
$w.a.x & "," & $w.a.y & "," & $w.b.x & "," & $w.b.y & "," &
|
||||
$w.thickness & "," & $w.position & "," & $w.playerMarker & "," & derivedKind(w)
|
||||
## Alias for the shared signature in `world` (5.8, #60): the write path deletes the persisted
|
||||
## rows by the same rule the online retraction uses.
|
||||
markerSig(w)
|
||||
|
||||
proc extentSeen*(pv: PlayerVision, a, b: Vec): bool =
|
||||
## True when any point of the wall's visible extent (the segment in this round) is inside one of
|
||||
@@ -128,11 +127,11 @@ proc writeNew*(db: DbConn, sceneId, playerId: int, fresh: var seq[(int, Marker)]
|
||||
fresh.setLen(0)
|
||||
|
||||
proc writeDead*(db: DbConn, playerId: int, dead: var seq[string]): int =
|
||||
## Deletes the markers the last refresh retracted for one player and clears the buffer. Same
|
||||
## transaction and ordering rule as `writeNew` (5.8, 4.2 step 6): derived player memory rides the
|
||||
## mutation's `NORMAL` transaction, after the wall row commits and before the ack, so a
|
||||
## retraction's `del` can never precede the wall change it reports. A wall removal already took
|
||||
## its rows through the composite FK cascade, so that delete is a no-op here (and still correct).
|
||||
## Deletes the markers the last refresh retracted for one player and clears the buffer. A wall
|
||||
## cause change already took its rows in the wall row's own `FULL` transaction (`world.updateWall`)
|
||||
## and a removal took its rows through the composite FK cascade, so this is normally a no-op; it
|
||||
## stays as the idempotent backstop for the live `del` frames, in the same `NORMAL` transaction and
|
||||
## ordering rule as `writeNew` (5.8, 4.2 step 6).
|
||||
if dead.len == 0: return 0
|
||||
let ps = db.prepare(DeleteSql)
|
||||
try:
|
||||
|
||||
@@ -158,6 +158,14 @@ proc derivedKind*(w: WallRec): string =
|
||||
else:
|
||||
"custom"
|
||||
|
||||
proc markerSig*(w: WallRec): string =
|
||||
## The marker cause signature of DESIGN 5.8: exactly the wall fields whose change retracts a
|
||||
## player marker — endpoints, thickness, placement, the `player_marker` switch and the derived
|
||||
## `window` gate. Height and label have no marker consequence and are deliberately absent. Shared
|
||||
## with `server/markers.nim` so the online retraction and the write-time delete cannot drift.
|
||||
$w.a.x & "," & $w.a.y & "," & $w.b.x & "," & $w.b.y & "," &
|
||||
$w.thickness & "," & $w.position & "," & $w.playerMarker & "," & derivedKind(w)
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------
|
||||
# The one expand-and-validate path for wall writes (DESIGN 5.8, 6.1 FIXED #49/#55)
|
||||
|
||||
@@ -485,12 +493,18 @@ proc addWall*(db: DbConn, sc: var SceneRec, rec: WallRec): Option[int] =
|
||||
proc updateWall*(db: DbConn, sc: var SceneRec, rec: WallRec): WallApply =
|
||||
## `wall.update` = full replacement of the patched row (`remove + add` for the 5.5 status rules,
|
||||
## which slice 5 re-evaluates). Clearing the door switch closes and de-secrets the wall.
|
||||
## A marker cause change (5.8, #60) takes the affected `marker_seen` rows in this same FULL
|
||||
## transaction: on a crash before the derived-memory write, a survivor would be restored on
|
||||
## restart with the NEW geometry, revealing a position the player never saw.
|
||||
let idx = wallIndex(sc, rec.id)
|
||||
if idx.isNone: return waNotFound
|
||||
let stored = normalize(rec)
|
||||
if not validWall(sc, stored): return waInvalid
|
||||
let sigChanged = markerSig(stored) != markerSig(sc.walls[idx.get])
|
||||
authoringTransaction(db):
|
||||
replaceRow(db, sc.id, stored)
|
||||
if sigChanged:
|
||||
db.exec(sql"DELETE FROM marker_seen WHERE scene_id = ? AND wall_id = ?", sc.id, stored.id)
|
||||
sc.walls[idx.get] = stored
|
||||
waOk
|
||||
|
||||
|
||||
Reference in New Issue
Block a user