server: per-recipient props and statuses in the one egress (#22)

The typed views carry the bucket filter: `PlayerTokenView` has `propsPublic`
always and `propsOwner` only when the recipient controls the token, and no
field for the `gm` bucket at all, so a later change to the frame builder
cannot leak one. Statuses are `StatusKind` values for a player (no `source`,
5.5/6.1 note 6) and full `(status, source)` entries for the GM.

Both negative controls for the new rules live behind `-d:leakfault` and in
the type, so the shipped binary has neither the fields nor the branches:
`gmProp` (the gm bucket reaches a player), `ownerProp` (the owner bucket
reaches a non-controller) and `statusSource` (a viewer learns the cause).
The `otherOwner` fault now carries the owner bucket too, so it stays one
class (the `mine` claim) instead of also tripping the bucket check.
This commit is contained in:
2026-11-15 11:02:35 +01:00
parent 38d8962f84
commit dcc813225f
+92 -15
View File
@@ -11,15 +11,21 @@
## both names. No `vision`, no `explored` ever.
## - player: tokens they control (always) plus tokens inside their LOS that are not `gm_only`;
## `props` carries only the buckets they may read (public, plus owner for the controller);
## `vision_radius` only for the controller (it is the step cap, 5.5); `source` never.
## `statuses` carries the status names only, never `source` (6.1 leak notes 5-6); `vision_radius`
## only for the controller (it is the step cap, 5.5); `statuses[].source` never.
## - a token's `name` is the descriptive name in this slice: the reveal machinery is slice 8 (#23),
## so no actual name has an egress path to a player at all.
##
## The bucket filter is applied when the *typed view* is built, not when the frame is serialised: a
## `PlayerTokenView` has no field for the `gm` bucket at all, so no later change to the frame builder
## can leak one. `statuses` for a player are `StatusKind` values, not `(kind, source)` entries, for
## the same reason.
##
## `Known` is the per-connection diff table (4.1/6.4): it holds the last frame sent for each token, so
## `token_gone` is sent only for a token this recipient did see, never for one it never knew. The
## comparison is on the built frame, so a coalesced state (M5) cannot desynchronise a client.
import std/[algorithm, json, math, options, sequtils, sets, tables]
import geometry, tokens, vision, world
import geometry, props, tokens, vision, world
import faults
type
@@ -40,6 +46,8 @@ type
visionRadius*: Option[float]
radiusOwn*: Option[float]
descriptiveName*, actualName*: string
statuses*: seq[StatusEntry] ## every entry, source included (GM only, 6.1)
props*: Props ## all three buckets, never nil
PlayerTokenView* = object
id*: string
@@ -50,6 +58,14 @@ type
mine*: bool
includeVisionRadius*: bool
visionRadius*: Option[float]
statuses*: seq[StatusKind] ## status names only: the cause is GM-only (5.5, 6.1 note 6)
propsPublic*: JsonNode ## the `public` bucket; never nil
propsOwner*: JsonNode ## the `owner` bucket, only for the controller (nil otherwise)
when defined(leakfault):
# Negative controls only (DESIGN 10.3): the shipped build has neither the fields nor the
# branches that would emit them, so a production binary is incapable of these two leaks.
faultGmProps*: JsonNode ## the `gm` bucket, when the `gmProp` fault is active
faultStatusEntries*: seq[StatusEntry] ## statuses with `source`, for the `statusSource` fault
GmView* = object
tokens*: seq[GmTokenView]
@@ -85,18 +101,32 @@ proc gmView*(ctx: Context): GmView =
flags: t.flags, visionRadius: t.visionRadius,
radiusOwn: t.radius,
descriptiveName: t.descriptiveName,
actualName: t.actualName)
actualName: t.actualName,
statuses: t.statuses, props: t.props)
proc objectOf(node: JsonNode): JsonNode =
## A bucket as a JSON object, never nil: a stored record always has three, but the typed view is
## also built in tests and a nil must not become a `null` in a frame.
if node.isNil or node.kind != JObject: newJObject() else: node
proc statusKinds(statuses: seq[StatusEntry]): seq[StatusKind] =
## The player-facing half of a token's status set: the names, in the stored order.
for e in statuses: result.add e.kind
proc playerView*(ctx: Context, playerId: int): PlayerView =
var pv = ctx.visions.getOrDefault(playerId)
for t in ctx.tokens:
if not pv.visibleTo(t, playerId): continue
let mine = t.owner == playerId
result.tokens.add PlayerTokenView(id: t.id, scene: t.scene,
name: t.descriptiveName, pos: t.pos,
radius: effectiveRadius(ctx.scene, t),
mine: t.owner == playerId,
includeVisionRadius: t.owner == playerId,
visionRadius: t.visionRadius)
mine: mine,
includeVisionRadius: mine,
visionRadius: t.visionRadius,
statuses: statusKinds(t.statuses),
propsPublic: objectOf(t.props[pbPublic]),
propsOwner: (if mine: objectOf(t.props[pbOwner]) else: nil))
when defined(leakfault):
# Negative controls (DESIGN 10.3): add the tokens the honest rule just refused, so the harness
# proves it would notice. Never a second copy of the visibility rule.
@@ -106,33 +136,71 @@ proc playerView*(ctx: Context, playerId: int): PlayerView =
result.tokens.add PlayerTokenView(id: t.id, scene: t.scene, name: t.descriptiveName,
pos: t.pos, radius: effectiveRadius(ctx.scene, t),
mine: false, includeVisionRadius: false,
visionRadius: t.visionRadius)
visionRadius: t.visionRadius,
statuses: statusKinds(t.statuses),
propsPublic: objectOf(t.props[pbPublic]),
propsOwner: nil)
if faultActive("gmOnly"):
for t in ctx.tokens:
if t.gmOnly and t.owner != playerId and pv.insideAny(t.pos):
result.tokens.add PlayerTokenView(id: t.id, scene: t.scene, name: t.descriptiveName,
pos: t.pos, radius: effectiveRadius(ctx.scene, t),
mine: false, includeVisionRadius: false,
visionRadius: t.visionRadius)
visionRadius: t.visionRadius,
statuses: statusKinds(t.statuses),
propsPublic: objectOf(t.props[pbPublic]),
propsOwner: nil)
if faultActive("withhold") and result.tokens.len > 1:
for i in countdown(result.tokens.high, 0):
if not result.tokens[i].mine:
result.tokens.delete i
break
if faultActive("otherOwner"):
# OTHER_PLAYER: somebody else's token presented as this player's own, owner bucket included so
# the fault stays one class (the checker reports the `mine` flag, not a bucket mismatch).
for t in result.tokens.mitems:
if not t.mine: t.mine = true; break
if not t.mine:
t.mine = true
for src in ctx.tokens:
if src.id == t.id: t.propsOwner = src.props[pbOwner]
break
if faultActive("ownerProp"):
# OWNER_PROP: the controller bucket reaches everyone.
for t in ctx.tokens:
if t.owner == playerId: continue
for v in result.tokens.mitems:
if v.id == t.id: v.propsOwner = t.props[pbOwner]
if faultActive("gmProp"):
# GM_PROP: the GM-only bucket reaches a player.
for t in ctx.tokens:
for v in result.tokens.mitems:
if v.id == t.id: v.faultGmProps = t.props[pbGm]
if faultActive("statusSource"):
# STATUS_SOURCE: a viewer learns what caused the lock (5.5, 6.1 note 6).
for t in ctx.tokens:
for v in result.tokens.mitems:
if v.id == t.id: v.faultStatusEntries = t.statuses
result.polys = pv.polys
result.fresh = pv.fresh
# ---------------------------------------------------------------------------------------------------
# Frames. These builders are the only producers of state frames in the server.
proc statusesJson(entries: seq[StatusEntry]): JsonNode =
## GM shape: `statuses:[{status, source}]` (6.1).
result = newJArray()
for e in entries: result.add %*{"status": $e.kind, "source": $e.source}
proc statusKindsJson(kinds: seq[StatusKind]): JsonNode =
## Player shape: `statuses:[{status}]`, never a `source` (5.5, 6.1 leak note 6).
result = newJArray()
for k in kinds: result.add %*{"status": $k}
proc gmTokenFrame*(v: GmTokenView): JsonNode =
result = %*{
"t": "token", "id": v.id, "scene": v.scene, "name": v.name,
"pos": [v.pos.x, v.pos.y], "radius": v.radius, "mine": false,
"statuses": [], "props": {"public": newJObject(), "owner": newJObject(), "gm": newJObject()},
"statuses": statusesJson(v.statuses), "props": props.propsJson(v.props),
"kind": v.kind, "controller": (if v.controller == GmControlled: newJNull() else: %v.controller),
"gm_only": v.gmOnly, "flags": v.flags,
"vision_radius": (if v.visionRadius.isSome: %v.visionRadius.get else: newJNull()),
@@ -142,17 +210,26 @@ proc gmTokenFrame*(v: GmTokenView): JsonNode =
proc playerTokenFrame*(v: PlayerTokenView): JsonNode =
## One token as a player may see it. `statuses` carries no `source` (3.1: the cause is GM-only);
## `props` carries the `public` bucket, plus `owner` for the controller (6.1).
var props = newJObject()
props["public"] = newJObject()
if v.mine: props["owner"] = newJObject()
## `props` carries the `public` bucket, plus `owner` for the controller (6.1). `playerView` fills
## `propsOwner` only for the controller, so "the view carries the bucket" *is* the rule; the
## leakfault build can carry it for a non-controller, which is what proves the checker notices.
## The typed view has no field for the `gm` bucket, so this builder cannot emit one.
var buckets = newJObject()
buckets["public"] = if v.propsPublic.isNil: newJObject() else: v.propsPublic
if not v.propsOwner.isNil:
buckets["owner"] = v.propsOwner
result = %*{
"t": "token", "id": v.id, "scene": v.scene, "name": v.name,
"pos": [v.pos.x, v.pos.y], "radius": v.radius, "mine": v.mine,
"statuses": [], "props": props
"statuses": statusKindsJson(v.statuses), "props": buckets
}
if v.includeVisionRadius:
result["vision_radius"] = if v.visionRadius.isSome: %v.visionRadius.get else: newJNull()
when defined(leakfault):
if faultActive("gmProp") and not v.faultGmProps.isNil:
buckets["gm"] = v.faultGmProps
if faultActive("statusSource") and v.faultStatusEntries.len > 0:
result["statuses"] = statusesJson(v.faultStatusEntries)
proc tokenGoneFrame*(id: string): JsonNode = %*{"t": "token_gone", "id": id}