Files
BT411/scratchpad/test_seat_identity.py
T
arcattackandClaude Opus 5 b7e4837738 seat identity swap (SAURON played as 'Draco'): the LEFT handler ate the pref of a re-seating player
THE CHAIN (reconstructed from the night's logs, then REPRODUCED offline in
scratchpad/test_seat_identity.py before fixing):

  1. At round end every pod relaunches and seat-requests again.  SAURON's
     request was walk-up-assigned the departed Draco's old seat, and his pref
     (callsign/mech) was correctly written -- the assign line even printed
     callsign='SAURON'.
  2. His request conn then closed BY DESIGN (the pod re-dials to HELLO) --
     and the beacon-death branch of _drop_game, added 2026-07-26 for the
     departed-player roster fix, treated any beacon death on an unclaimed
     seat as a LEAVE: it printed a false 'PLAYER n LEFT' and POPPED the pref
     written milliseconds earlier.
  3. The next egg release _reload_egg_file()'d the DISK egg -- where the GUI's
     Start Session had saved the ADOPTED roster names, including 'Draco' on
     that row -- and with no pref left to override it, Draco's callsign
     shipped on SAURON's seat.  His plasma (and that round's score
     attribution) wore the wrong name.  Per-seat HELLO-vs-close ordering
     roulette explains why only one seat swapped.

THE FIX: a LEFT-grace window (SEAT_LEFT_GRACE_SECONDS=15).  A beacon that
dies younger than the grace is the pod's designed post-assign re-dial: keep
the pref and the reservation, print nothing.  A real join-menu leaver has
held the seat far longer, so the departed-player roster fix keeps working
(pop + LEFT exactly as before); claimed seats were already exempt.

REGRESSION SUITE (new, offline, drives the real Relay class -- no ports):
  A. designed instant close: pref survives, seat stays protected  [was FAIL]
  B. real leave (aged beacon): pref popped + seat freed           [unchanged]
  C. claimed seat: pref survives an unrelated conn death          [unchanged]
  D. a different identity assigned onto a held seat overwrites
     the held pref (the operator's original suspicion, locked in) [unchanged]
All four console suites pass (rearm 25, net 17, roster 22, identity 7).

Python-only: no client update needed; the running console picks it up on its
next Start Session.  Awaiting live verification next games night.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 00:11:59 -05:00

160 lines
5.6 KiB
Python

"""Seat-identity regression: the SAURON-played-as-Draco swap (2026-07-26 night).
The chain, reconstructed from the live logs:
1. a seat-request conn is assigned a seat and writes the player's pref
(callsign/mech); the conn then CLOSES BY DESIGN ("the pod re-dials and
claims the seat with a normal HELLO");
2. the game-conn death handler treated ANY beacon death on an unclaimed seat
as "player left" and POPPED the just-written pref (code added 2026-07-26
for the departed-player roster fix -- correct for real leaves, wrong for
the designed instant close);
3. at release, _reload_egg_file resurrected the DISK egg's stale callsign
(the GUI saves adopted names), and with no pref to override it a departed
player's name shipped on the new occupant's seat.
Asserts:
A. the designed instant close does NOT pop the pref and does NOT print LEFT;
B. a REAL leave (beacon older than the grace) still pops + prints LEFT
(the departed-player roster fix must keep working);
C. a claimed seat (HELLO landed) keeps prefs regardless;
D. a different identity walk-up-assigned onto a held seat OVERWRITES the
held pref (the original suspicion -- lock it in).
Offline: drives the Relay class in-process, no sockets bound.
"""
import os
import sys
import time
REPO = r"C:\git\bt411"
sys.path.insert(0, os.path.join(REPO, "tools"))
os.chdir(os.path.join(REPO, "content"))
import btconsole # noqa: E402
EGG = os.path.join(REPO, "content", "FOGDAY.EGG")
fails = []
def check(label, ok, extra=""):
print(" %-56s %s %s" % (label, "OK" if ok else "*** FAIL ***", extra))
if not ok:
fails.append(label)
class FakeSock:
def __init__(self, ip="10.0.0.9"):
self.ip = ip
self.sent = []
def getpeername(self):
return (self.ip, 4242)
def sendall(self, data):
self.sent.append(data)
def settimeout(self, _):
pass
def setblocking(self, _):
pass
def close(self):
pass
class FakeGameConn:
def __init__(self, ip="10.0.0.9"):
self.sock = FakeSock(ip)
self.addr = (ip, 4242)
self.host_id = None
self.last_seen = time.time()
self.dead = False
self.out = b""
def name(self):
return "%s:%d" % self.addr
def queue_out(self, b):
self.out += b
def flush_out(self):
pass
def new_relay():
return btconsole.Relay(1500, EGG, "127.0.0.1", 0, manual_launch=True)
def seat_request(r, conn, callsign, mech=b"lok2", claim=b""):
"""Deliver a ROUTE_SEAT_REQUEST payload through the real handler."""
payload = callsign + b"\0" + mech + b"\0" + claim
r._handle_game_frame(conn, btconsole.ROUTE_SEAT_REQUEST, payload)
# Locate the real frame router (name may differ); fall back to the documented
# entry point so the test fails loudly rather than silently testing nothing.
if not hasattr(btconsole.Relay, "_handle_game_frame"):
import inspect
cands = [n for n, _ in inspect.getmembers(btconsole.Relay)
if "game" in n and ("frame" in n or "route" in n or "data" in n)]
print("NOTE: _route_game_frame not found; candidates:", cands)
print("=== A. designed instant close must NOT pop the pref ===")
r = new_relay()
conn = FakeGameConn()
seat_request(r, conn, b"SAURON")
seat = conn.seat_host
check("seat assigned", seat is not None, "host=%s" % seat)
check("pref written", r.seat_prefs.get(seat, ("", ""))[0] == "SAURON",
repr(r.seat_prefs.get(seat)))
# the request conn closes IMMEDIATELY (the designed behavior)
r._drop_game(conn, "closed")
check("pref SURVIVES the instant close",
r.seat_prefs.get(seat, ("", ""))[0] == "SAURON",
repr(r.seat_prefs.get(seat)))
check("beacon still marks the seat occupied-ish or reserved",
seat in r.seat_beacons or seat in r.seat_reservations)
print("=== B. a REAL leave still frees the seat (roster fix preserved) ===")
r = new_relay()
conn = FakeGameConn()
seat_request(r, conn, b"Draco", b"vulture")
seat = conn.seat_host
# age the beacon past any grace window
conn.seated_at = time.time() - 3600
r._drop_game(conn, "closed")
check("pref popped on a real leave", seat not in r.seat_prefs,
repr(r.seat_prefs.get(seat)))
print("=== C. a claimed seat keeps prefs on unrelated conn death ===")
r = new_relay()
conn = FakeGameConn()
seat_request(r, conn, b"Elengil", b"bhk1")
seat = conn.seat_host
r.by_host[seat] = object() # HELLO landed: the seat is claimed
conn.seated_at = time.time() - 3600
r._drop_game(conn, "closed")
check("pref survives (seat claimed)",
r.seat_prefs.get(seat, ("", ""))[0] == "Elengil",
repr(r.seat_prefs.get(seat)))
print("=== D. different identity on a held seat overwrites the pref ===")
r = new_relay()
c1 = FakeGameConn("10.0.0.5")
seat_request(r, c1, b"Draco", b"vulture")
seat1 = c1.seat_host
# Draco leaves for real: hold starts, pref state per current design
c1.seated_at = time.time() - 3600
r._drop_game(c1, "closed")
# force the hold shape tonight had: pref somehow still present for the seat
r.seat_prefs[seat1] = ("Draco", "vulture")
r.seat_reclaim.clear() # hold expired
c2 = FakeGameConn("10.0.0.7") # DIFFERENT ip + callsign
seat_request(r, c2, b"SAURON", b"lok2")
if c2.seat_host == seat1:
check("new identity's pref overwrote the held one",
r.seat_prefs.get(seat1, ("", ""))[0] == "SAURON",
repr(r.seat_prefs.get(seat1)))
else:
check("walk-up assigned a different seat (hold honored) -- pref intact "
"on its own seat",
r.seat_prefs.get(c2.seat_host, ("", ""))[0] == "SAURON",
"seat1=%s seat2=%s" % (seat1, c2.seat_host))
print()
print("ALL PASS" if not fails else "FAILURES: %s" % fails)
sys.exit(1 if fails else 0)