fix(battlenode): shadow engine tracks live PvP wire-truth (full battle, multiple bid regressions)

Six distinct fixes accumulated over live-test iterations against four bids
(654473755566, 806245601092, 283192092460, 131549100204, 799755786270) — together
they take the shadow engine from "throws on the first non-mulligan play" to
"survives a full PvP battle, only weird-edge-case Unity touches still left to whack".

1. Engine StableRandom seed aligned with clients' Matched.seed
   (BattleSession.EnsureEngineSetup, NodeNativeBattleHarness.Create). Clients seed
   _stableRandom with BattleSeeds.Stable(masterSeed) (the value the node ships in
   Matched.seed); we were passing the RAW masterSeed to engine.Setup, so every
   StableRandom call diverged from call #1 onward — every turn-1+ draw picked a
   different deck position than the clients. Verified Stable(1184631275)=1543475792
   matches the wire on bid 654473755566.

2. SeedDeck advances cardTotalNum to deck.Count+1 + pins BattleStartDeckCardList.
   Mirrors SBattleLoad.InitPlayer's tail (SBattleLoad.cs:1292). Without it,
   skill-generated tokens auto-assigned Index 0,1,... and COLLIDED with deck-loaded
   indices 1..40 — silent until something addressed the deck card with the
   colliding Index (Hoverboarder at deck idx 1 + a token at engine Index 1 made
   GetBattleCardIdx's SingleOrDefault throw on bid 806245601092).

3. BattleCardView.GameObject lazily non-null in the shim (ViewUiTouchStubs.cs).
   The IsRecovery card-create delegate (NetworkBattleManagerBase.cs:379) passes
   null cardGameObject; Skill_metamorphose.cs:147 in the in-play branch then NRE'd
   on `metamorphosedCard.BattleCardView.GameObject.transform.rotation = identity`,
   a purely cosmetic touch with no game-state implication. Bid 283192092460:
   Petrification on a board follower.

4. TranslateChoiceKeyAction unwraps wrapped selectCard on shadow ingest
   (SessionBattleEngine.cs, sibling to TranslateTargetOwners). Live sender-send
   wires Choice plays as selectCard:{cardId:[...], open:0}; engine's
   ConvertToListInt does `value as List<object>` — a Dict casts to null and
   foreach NREs. The receiver's swallow-all catch (NetworkBattleReceiver.cs:1255)
   logs to Debug.LogError + LocalLog — both shimmed/no-op'd headlessly — and
   returns false, but Receive calls ReceivedMessage with checkBreakData:false so
   the false isn't propagated. The play continues with choiceIdList=[], the chosen
   branch never resolves, the played card stays in hand; a later targeted play
   (A's bounce on B's "board" idx 20) then can't find the target → NRE on null in
   ActionProcessor.PlayCard:407. Bid 131549100204: B's Resonance + A's bounce.
   Opponent-relay path is unaffected — node strips selectCard from broadcasts.

5. HeadlessHandViewStub overrides HandUnfocus/HandFocus/FocusRearrangeHandHand
   to return NullVfx. CreateHandControl returns null in headless; the base
   methods unconditionally deref `_handControl.SetHandState(...)`. A follower
   with a when_spell_play Heal trigger fired on its leader for amount 0 — even
   a 0-heal drives ApplyHealing → CreatePullHandInVfx → HandUnfocus → NRE.
   Bid 799755786270: two consecutive spell plays both crashed this stack.
   Added InternalsVisibleTo("SVSim.BattleEngine.Tests") so the shim-level
   regression tests can pin the no-op contracts directly.

Plus the previous-session fixes carried in this same uncommitted state
(see docs/superpowers/plans/2026-06-07-shadow-engine-desync-handoff.md):
  - doesPlayerGoFirst:true + mgr.IsFirst:true (turn-1 draw count correct
    per seat)
  - RecoveryOperationCollection.PlayHandCardOperation routes all type:30
    through PlaySkillSelectHandCardOperation (skips the two-phase user-select
    guard that aborts targeted spells in recovery)
  - ShadowFeed + ToRawBody: server-generated typed bodies (DealBody, etc.)
    converted to RawBody before engine.Receive (`env.Body as RawBody`
    returned null for typed bodies)
  - Ready idxChangeSeed seeds A's XorShift via the receiver; B's seed is
    injected via SeedOppoIdxChange (BattleSeeds.IdxChange + viewerId)
  - ReadySpin defaulted to 0 (was 243) — non-zero double-cranks the shadow
    which ingests BOTH sides' Ready frames on one stream

Test counts: SVSim.UnitTests 1054/1054, SVSim.BattleEngine.Tests 34/34.

Open: known-residual Unity touches are individual whack-a-mole now (per-card
skill edge cases), not the structural divergences fixed here.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-07 19:05:07 -04:00
parent 2a8c44a6d7
commit addeb021d2
22 changed files with 2263 additions and 62 deletions

View File

@@ -0,0 +1,275 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions.Engine;
namespace SVSim.BattleEngine.Tests.SessionEngine
{
/// <summary>
/// PHASE 4 — DECISIVE VERIFICATION (TEST-ONLY, no production fix, no Engine/*.cs edits).
///
/// QUESTION: does feeding the headless shadow engine the FULL client inputs (server-authored
/// Deal/Swap/Ready setup frames for BOTH seats + the real per-seat <c>idxChangeSeed</c>) make its
/// recovery-mode draw recompute faithful, so the "Target card was not found in hand cards"
/// divergences vanish?
///
/// This builds the explicit 2x2 {setup-frames ingested: yes/no} x {real seed: yes/no} divergence
/// table over the SAME fresh battle (907324319325, battle_test_fresh_cl1/cl2.ndjson), and — at the
/// FIRST remaining divergence — dumps the engine's hand indices/ids vs the wire's <c>playIdx</c>.
///
/// SEEDING MECHANISM (clean, both seats): the seat-B <c>Ready</c> ingest throws an NRE headless (the
/// recovery deal path isn't headless-clean for the opponent seat), so the wire <c>Ready</c> cannot be
/// relied on to seat seat B's XorShift. To inject the real seed FAITHFULLY for BOTH seats without
/// depending on the throwing Ready, we call the test seam <see cref="SessionBattleEngine"/>.
/// <c>DebugSeedIdxChange(self, oppo)</c> (-> <c>BattleManagerBase.CreateXorShift</c>) BEFORE the
/// mulligan-end frame, with the real per-seat seeds (seat A = cl1's Ready idxChangeSeed = 1430655717,
/// seat B = cl2's = 661650374). We ASSERT both <c>SelfXorShiftActive</c> and <c>OppoXorShiftActive</c>
/// are true after.
///
/// SETUP-FRAME INGEST: identical mechanism to <see cref="CaptureReplayReshuffleRootCauseTests"/> — a
/// single <c>Deal</c> (cl1's receive Deal seats BOTH hands), each seat's <c>Swap</c> (its mulligan),
/// each seat's <c>Ready</c> (mulligan-end). The {no-setup-frames} row SKIPS Deal/Swap/Ready entirely:
/// the engine's autonomous Setup hand stands, and we replay only the plays.
/// </summary>
[TestFixture]
[NonParallelizable]
public class CaptureReplayFullInputDivergenceExperimentTests
{
// Real per-seat idxChangeSeed carried by each client's Ready frame (given in the experiment brief;
// re-confirmed below against the captures).
private const int SeatASeed = 1430655717; // cl1 / seat A / player
private const int SeatBSeed = 661650374; // cl2 / seat B / opponent
private static readonly HashSet<string> SkipUris = new()
{
nameof(NetworkBattleUri.Echo),
nameof(NetworkBattleUri.ChatStamp),
nameof(NetworkBattleUri.Gungnir),
};
private static readonly HashSet<string> MulliganUris = new()
{
nameof(NetworkBattleUri.Deal),
nameof(NetworkBattleUri.Swap),
nameof(NetworkBattleUri.Ready),
};
private sealed record HandDump(string Seat, int PlayIdx, string Uri, string Reason,
IReadOnlyList<(int Index, int CardId)> SelfHand,
IReadOnlyList<(int Index, int CardId)> OppoHand,
bool PlayIdxInSelfHand, bool PlayIdxInOppoHand);
private sealed record Cell(
bool SetupFrames, bool RealSeed,
int Divergences, bool SelfXorActive, bool OppoXorActive,
HandDump? FirstNotFoundDump);
private static int ReadPlayIdx(string rawBody)
{
using var doc = JsonDocument.Parse(rawBody);
return doc.RootElement.TryGetProperty("playIdx", out var p) && p.TryGetInt32(out var v) ? v : -1;
}
// Snapshot a seat's hand as (engine Index, CardId) pairs. Reads through the SessionBattleEngine
// oracle accessors (HandCount/HandCardIndex/HandCardId).
private static List<(int, int)> HandSnapshot(SessionBattleEngine engine, bool seat)
{
var list = new List<(int, int)>();
int n = engine.HandCount(seat);
for (int i = 0; i < n; i++)
list.Add((engine.HandCardIndex(seat, i), engine.HandCardId(seat, i)));
return list;
}
private static Cell Run(bool setupFrames, bool realSeed)
{
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
Assert.That(deckA, Is.Not.Empty);
Assert.That(deckB, Is.Not.Empty);
var engine = new SessionBattleEngine();
engine.Setup(masterSeed: CaptureReplay.SeedFrom(cl1), seatADeck: deckA, seatBDeck: deckB);
Assert.That(engine.IsReady, Is.True);
// Inject the real per-seat seed BEFORE mulligan-end (Ready). Clean both-seat activation via the
// CreateXorShift seam, sidestepping the seat-B Ready NRE.
if (realSeed)
engine.DebugSeedIdxChange(SeatASeed, SeatBSeed);
int divergences = 0;
HandDump? firstNotFound = null;
void Ingest(MsgEnvelope env, bool seat, string uri, string rawBody)
{
var r = engine.Receive(env, isPlayerSeat: seat);
if (!r.Diverged) return;
divergences++;
if (firstNotFound is null && (r.RejectReason ?? "").Contains("not found in hand"))
{
int playIdx = ReadPlayIdx(rawBody);
var self = HandSnapshot(engine, seat);
var oppo = HandSnapshot(engine, !seat);
firstNotFound = new HandDump(
seat ? "A" : "B", playIdx, uri, Trim(r.RejectReason),
self, oppo,
self.Any(h => h.Item1 == playIdx), oppo.Any(h => h.Item1 == playIdx));
}
}
CapturedFrame Receive(IReadOnlyList<CapturedFrame> frames, string uri) =>
frames.First(f => f.Direction == "receive" && f.Uri == uri);
// --- Phase 1: setup frames (optional) ---------------------------------------------------------
if (setupFrames)
{
var deal = Receive(cl1, nameof(NetworkBattleUri.Deal));
Ingest(deal.Env, seat: true, nameof(NetworkBattleUri.Deal), deal.RawBody);
foreach (var (frames, seat) in new[] { (cl1, true), (cl2, false) })
{
var swap = Receive(frames, nameof(NetworkBattleUri.Swap));
Ingest(swap.Env, seat, nameof(NetworkBattleUri.Swap), swap.RawBody);
var ready = Receive(frames, nameof(NetworkBattleUri.Ready));
Ingest(ready.Env, seat, nameof(NetworkBattleUri.Ready), ready.RawBody);
}
}
bool selfActive = engine.SelfXorShiftActive;
bool oppoActive = engine.OppoXorShiftActive;
// Snapshot the engine's post-setup hands (after Deal/Swap/Ready) for the full-inputs cell, so the
// report can compare the engine's mulligan-resolved hand against the wire's Swap/Ready move list.
if (setupFrames && realSeed)
{
TestContext.WriteLine(" [post-setup] engine SELF (seat A) hand: " +
string.Join(" ", HandSnapshot(engine, true).Select(h => $"(idx={h.Item1},cid={h.Item2})")));
TestContext.WriteLine(" [post-setup] engine OPPO (seat B) hand: " +
string.Join(" ", HandSnapshot(engine, false).Select(h => $"(idx={h.Item1},cid={h.Item2})")));
}
// --- Phase 2: replay both clients' interleaved SENDS (the plays) ------------------------------
var sends = SendsWithRawBody(cl1, cl2)
.Where(x => !SkipUris.Contains(x.Frame.Uri))
.ToList();
foreach (var x in sends)
Ingest(x.Frame.Env, x.Seat, x.Frame.Uri, x.Frame.RawBody);
return new Cell(setupFrames, realSeed, divergences, selfActive, oppoActive, firstNotFound);
}
private static IEnumerable<(CapturedFrame Frame, bool Seat)> SendsWithRawBody(
IReadOnlyList<CapturedFrame> cl1, IReadOnlyList<CapturedFrame> cl2)
{
return cl1.Where(f => f.Direction == "send").Select(f => (f, Seat: true))
.Concat(cl2.Where(f => f.Direction == "send").Select(f => (f, Seat: false)))
.OrderBy(x => x.f.Ts)
.Select(x => (x.f, x.Seat));
}
private static string Trim(string? s) => (s ?? "").Split(" @ ")[0];
[Test]
public void Full_input_2x2_divergence_table_and_first_remaining_divergence_dump()
{
// Confirm the brief's per-seat seeds match the captures' Ready frames before relying on them.
ConfirmReadySeeds();
var cells = new[]
{
Run(setupFrames: false, realSeed: false), // baseline-ish: autonomous Setup hand, seed -1
Run(setupFrames: false, realSeed: true),
Run(setupFrames: true, realSeed: false),
Run(setupFrames: true, realSeed: true), // FULL INPUTS
};
TestContext.WriteLine("=== 2x2 DIVERGENCE TABLE (setup-frames x real-seed) ===");
TestContext.WriteLine("setupFrames | realSeed | divergences | selfXor | oppoXor");
foreach (var c in cells)
TestContext.WriteLine(
$" {(c.SetupFrames ? "YES" : "no ")} | {(c.RealSeed ? "YES" : "no ")} | {c.Divergences,2} | {c.SelfXorActive,-5} | {c.OppoXorActive,-5}");
var full = cells.Single(c => c.SetupFrames && c.RealSeed);
TestContext.WriteLine("");
TestContext.WriteLine($"FULL-INPUTS cell: setupFrames=YES realSeed=YES -> divergences={full.Divergences} " +
$"selfXorActive={full.SelfXorActive} oppoXorActive={full.OppoXorActive}");
if (full.FirstNotFoundDump is { } d)
{
TestContext.WriteLine("");
TestContext.WriteLine("=== FIRST 'not found in hand' DIVERGENCE (full-inputs cell) ===");
TestContext.WriteLine($" seat={d.Seat} uri={d.Uri} wire playIdx={d.PlayIdx} reason={d.Reason}");
TestContext.WriteLine($" playIdx in self hand? {d.PlayIdxInSelfHand} in oppo hand? {d.PlayIdxInOppoHand}");
TestContext.WriteLine($" engine SELF (seat {d.Seat}) hand [{d.SelfHand.Count}]: " +
string.Join(" ", d.SelfHand.Select(h => $"(idx={h.Index},cid={h.CardId})")));
TestContext.WriteLine($" engine OPPO hand [{d.OppoHand.Count}]: " +
string.Join(" ", d.OppoHand.Select(h => $"(idx={h.Index},cid={h.CardId})")));
}
else
{
TestContext.WriteLine("");
TestContext.WriteLine("FULL-INPUTS cell produced NO 'not found in hand' divergence.");
}
// EVIDENCE ASSERTIONS (pin the experiment's reproducibility, not a desired fix outcome):
Assert.Multiple(() =>
{
// The seed seam activates BOTH seats' XorShift in every realSeed cell.
foreach (var c in cells.Where(c => c.RealSeed))
{
Assert.That(c.SelfXorActive, Is.True,
$"realSeed cell (setup={c.SetupFrames}) must activate self XorShift");
Assert.That(c.OppoXorActive, Is.True,
$"realSeed cell (setup={c.SetupFrames}) must activate oppo XorShift");
}
// With NO seed seam AND NO setup frames (the live shadow's effective state — never
// ingests the seed-bearing Ready), BOTH seats' XorShift stay inactive.
var bare = cells.Single(c => !c.RealSeed && !c.SetupFrames);
Assert.That(bare.SelfXorActive, Is.False, "no-seed/no-setup leaves self XorShift inactive");
Assert.That(bare.OppoXorActive, Is.False, "no-seed/no-setup leaves oppo XorShift inactive");
// With setup frames but no seam, the seat-A Ready frame's own idxChangeSeed activates the
// SELF XorShift (seat B's Ready NREs before it can seat oppo) — so self is active, oppo isn't.
var setupNoSeam = cells.Single(c => !c.RealSeed && c.SetupFrames);
Assert.That(setupNoSeam.SelfXorActive, Is.True,
"setup-frames cell: seat-A Ready idxChangeSeed activates self XorShift");
Assert.That(setupNoSeam.OppoXorActive, Is.False,
"setup-frames cell: seat-B Ready NREs before seating oppo XorShift");
// THE DECISIVE FINDING: full inputs (setup frames + real seed, both seats' XorShift active)
// do NOT eliminate the divergences — they stay at the 14 baseline.
var full2 = cells.Single(c => c.SetupFrames && c.RealSeed);
Assert.That(full2.SelfXorActive && full2.OppoXorActive, Is.True,
"full-inputs cell has both seats' XorShift active");
Assert.That(full2.Divergences, Is.GreaterThan(0),
"REFUTED: full inputs do NOT make the recovery recompute faithful — divergences remain");
});
}
// Re-confirm the brief's per-seat seeds against the captured Ready frames (fail loudly if the
// fixtures ever drift from the assumed seeds).
private static void ConfirmReadySeeds()
{
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
int a = ReadReadySeed(cl1);
int b = ReadReadySeed(cl2);
TestContext.WriteLine($"Confirmed Ready idxChangeSeed: cl1(seatA)={a} cl2(seatB)={b}");
Assert.That(a, Is.EqualTo(SeatASeed), "cl1 Ready idxChangeSeed must equal the brief's seat-A seed");
Assert.That(b, Is.EqualTo(SeatBSeed), "cl2 Ready idxChangeSeed must equal the brief's seat-B seed");
}
private static int ReadReadySeed(IReadOnlyList<CapturedFrame> frames)
{
var ready = frames.First(f => f.Direction == "receive" && f.Uri == nameof(NetworkBattleUri.Ready));
var obj = JsonNode.Parse(ready.RawBody)!.AsObject();
return (int)obj["idxChangeSeed"]!;
}
}
}

View File

@@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions.Engine;
namespace SVSim.BattleEngine.Tests.SessionEngine
{
/// <summary>
/// PHASE 4 — DRAW-RECOMPUTE ROOT-CAUSE VALIDATION (TEST-ONLY; no production fix; no Engine/*.cs edits).
///
/// HYPOTHESIS (from the experiment brief): the shadow diverges ("Target card was not found in hand
/// cards", post-mulligan) because the per-turn network DRAW is a SEEDED-RANDOM pick from the deck via
/// <c>mgr.StableRandom(...)</c> (SkillRandomSelectFilter.Filtering:49/58), gated by the process-global
/// <c>BattleManagerBase.IsRandomDraw</c> — which the real match-load sets true via
/// <c>StartOpening → SetupInitialGameState(areCardsRandomlyDrawn:true)</c> (BattleManagerBase.cs:1098/1110).
/// The headless <see cref="SessionBattleEngine"/>.Setup never runs SetupInitialGameState, so IsRandomDraw
/// stays FALSE and the shadow draws TOP-OF-DECK while the clients draw seeded-random → mismatch.
/// AND the shared <c>_stableRandom</c> stream must be advanced by the wire <c>spin</c> pre-roll the Ready
/// frame carries (spin=243), which <c>OperateReceive.StartOperate:80-83</c> applies but the shadow never
/// ingests — so without it the stream is offset.
///
/// ISOLATION MATRIX (this is the report's headline): setup frames + real seed are held CONSTANT (the
/// faithful baseline the prior FullInput experiment pinned at 14); the two NEW variables are toggled:
/// • {IsRandomDraw=false, no spin} = baseline (top-of-deck draws; the live shadow's effective state)
/// • {IsRandomDraw=true, no spin} = random-draw active but stream MIS-aligned (expect WORSE)
/// • {IsRandomDraw=true, +spin} = random-draw active AND stream aligned (the hypothesised fix)
///
/// SPIN APPLICATION: spin=243 appears on the Ready frame in BOTH captures (each client applies its own
/// once). Our shadow shares ONE <c>_stableRandom</c> across both seats (seated as both players), and a
/// single client's stream sits 243 draws in after ITS Ready — so we apply spin=243 ONCE, after the
/// Deal/Swap/Ready setup frames and before the plays, exactly where the real client's StartOperate would.
/// (A scan of both fixtures confirms Ready is the ONLY frame carrying a non-zero spin.)
/// </summary>
[TestFixture]
[NonParallelizable]
public class CaptureReplayRandomDrawSpinRootCauseTests
{
private const int SeatASeed = 1430655717; // cl1 / seat A / player (Ready idxChangeSeed)
private const int SeatBSeed = 661650374; // cl2 / seat B / opponent
private const int WireSpin = 243; // both captures' Ready frame spin
private static readonly HashSet<string> SkipUris = new()
{
nameof(NetworkBattleUri.Echo),
nameof(NetworkBattleUri.ChatStamp),
nameof(NetworkBattleUri.Gungnir),
};
private sealed record HandDump(string Seat, int PlayIdx, string Uri, string Reason,
int StableRandomCount,
IReadOnlyList<(int Index, int CardId)> SelfHand,
IReadOnlyList<(int Index, int CardId)> OppoHand,
bool PlayIdxInSelfHand, bool PlayIdxInOppoHand);
private sealed record Cell(bool RandomDraw, bool Spin, int Divergences, HandDump? FirstNotFound);
private static int ReadPlayIdx(string rawBody)
{
using var doc = JsonDocument.Parse(rawBody);
return doc.RootElement.TryGetProperty("playIdx", out var p) && p.TryGetInt32(out var v) ? v : -1;
}
private static List<(int, int)> HandSnapshot(SessionBattleEngine engine, bool seat)
{
var list = new List<(int, int)>();
int n = engine.HandCount(seat);
for (int i = 0; i < n; i++)
list.Add((engine.HandCardIndex(seat, i), engine.HandCardId(seat, i)));
return list;
}
private static CapturedFrame Receive(IReadOnlyList<CapturedFrame> frames, string uri) =>
frames.First(f => f.Direction == "receive" && f.Uri == uri);
private static Cell Run(bool randomDraw, bool spin)
{
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
Assert.That(deckA, Is.Not.Empty);
Assert.That(deckB, Is.Not.Empty);
var engine = new SessionBattleEngine();
engine.Setup(masterSeed: CaptureReplay.SeedFrom(cl1), seatADeck: deckA, seatBDeck: deckB);
Assert.That(engine.IsReady, Is.True);
// CONSTANT across all cells: faithful seed seam (both seats' XorShift active), sidestepping the
// seat-B Ready NRE — identical to the FullInput experiment's full-inputs cell.
engine.DebugSeedIdxChange(SeatASeed, SeatBSeed);
// NEW VARIABLE 1: the IsRandomDraw gate. Set BEFORE any draw (deal is the first draw).
engine.DebugSetRandomDraw(randomDraw);
int divergences = 0;
HandDump? firstNotFound = null;
void Ingest(MsgEnvelope env, bool seat, string uri, string rawBody)
{
var r = engine.Receive(env, isPlayerSeat: seat);
if (!r.Diverged) return;
divergences++;
if (firstNotFound is null && (r.RejectReason ?? "").Contains("not found in hand"))
{
var self = HandSnapshot(engine, seat);
var oppo = HandSnapshot(engine, !seat);
int playIdx = ReadPlayIdx(rawBody);
firstNotFound = new HandDump(
seat ? "A" : "B", playIdx, uri, Trim(r.RejectReason),
engine.DebugStableRandomCount, self, oppo,
self.Any(h => h.Item1 == playIdx), oppo.Any(h => h.Item1 == playIdx));
}
}
// --- Phase 1: setup frames (CONSTANT: Deal once + each seat's Swap + Ready) -------------------
var deal = Receive(cl1, nameof(NetworkBattleUri.Deal));
Ingest(deal.Env, seat: true, nameof(NetworkBattleUri.Deal), deal.RawBody);
foreach (var (frames, seat) in new[] { (cl1, true), (cl2, false) })
{
var swap = Receive(frames, nameof(NetworkBattleUri.Swap));
Ingest(swap.Env, seat, nameof(NetworkBattleUri.Swap), swap.RawBody);
var ready = Receive(frames, nameof(NetworkBattleUri.Ready));
Ingest(ready.Env, seat, nameof(NetworkBattleUri.Ready), ready.RawBody);
}
// NEW VARIABLE 2: the spin pre-roll, applied at mulligan-end (after Ready, before the first
// turn-start draw) — where OperateReceive.StartOperate applies the Ready's spin in production.
// ONE application of 243 (shared stream, one client's worth of advance).
if (spin)
engine.DebugSpinPreroll(WireSpin);
// --- Phase 2: replay both clients' interleaved SENDS (the plays) ------------------------------
var sends = SendsWithRawBody(cl1, cl2)
.Where(x => !SkipUris.Contains(x.Frame.Uri))
.ToList();
foreach (var x in sends)
Ingest(x.Frame.Env, x.Seat, x.Frame.Uri, x.Frame.RawBody);
return new Cell(randomDraw, spin, divergences, firstNotFound);
}
private static IEnumerable<(CapturedFrame Frame, bool Seat)> SendsWithRawBody(
IReadOnlyList<CapturedFrame> cl1, IReadOnlyList<CapturedFrame> cl2)
{
return cl1.Where(f => f.Direction == "send").Select(f => (f, Seat: true))
.Concat(cl2.Where(f => f.Direction == "send").Select(f => (f, Seat: false)))
.OrderBy(x => x.f.Ts)
.Select(x => (x.f, x.Seat));
}
private static string Trim(string? s) => (s ?? "").Split(" @ ")[0];
[Test]
public void IsRandomDraw_plus_spin_preroll_isolation_matrix()
{
try
{
ConfirmSpin();
var baseline = Run(randomDraw: false, spin: false);
var rdOnly = Run(randomDraw: true, spin: false);
var rdSpin = Run(randomDraw: true, spin: true);
TestContext.WriteLine("=== ISOLATION MATRIX (setup-frames + real-seed held CONSTANT) ===");
TestContext.WriteLine("IsRandomDraw | spin | divergences");
TestContext.WriteLine($" false | no | {baseline.Divergences}");
TestContext.WriteLine($" true | no | {rdOnly.Divergences}");
TestContext.WriteLine($" true | +243 | {rdSpin.Divergences}");
DumpFirst("baseline {false,no}", baseline);
DumpFirst("rd-only {true,no}", rdOnly);
DumpFirst("rd+spin {true,+243}", rdSpin);
Assert.Pass(
$"MATRIX baseline={baseline.Divergences} rdOnly={rdOnly.Divergences} rdSpin={rdSpin.Divergences}");
}
catch (SuccessException) { throw; }
catch (Exception ex)
{
TestContext.WriteLine("EXPERIMENT THREW: " + ex);
throw;
}
}
private static void DumpFirst(string label, Cell c)
{
if (c.FirstNotFound is not { } d)
{
TestContext.WriteLine($"[{label}] no 'not found in hand' divergence.");
return;
}
TestContext.WriteLine($"[{label}] FIRST 'not found in hand': seat={d.Seat} uri={d.Uri} " +
$"wire playIdx={d.PlayIdx} stableRandomCount={d.StableRandomCount} reason={d.Reason}");
TestContext.WriteLine($" playIdx in self hand? {d.PlayIdxInSelfHand} in oppo hand? {d.PlayIdxInOppoHand}");
TestContext.WriteLine($" SELF (seat {d.Seat}) hand [{d.SelfHand.Count}]: " +
string.Join(" ", d.SelfHand.Select(h => $"(idx={h.Index},cid={h.CardId})")));
TestContext.WriteLine($" OPPO hand [{d.OppoHand.Count}]: " +
string.Join(" ", d.OppoHand.Select(h => $"(idx={h.Index},cid={h.CardId})")));
}
/// <summary>STEP 4 (payoff check): with the hypothesised fix applied {IsRandomDraw=true, +spin},
/// does the engine reach and RESOLVE cl1's spellboost play so PlayedCardSpellboost/PlayedCardCost
/// return real (non-zero) values? cl1's deck carries the spellboost-scaling follower 101314020 at
/// deck idx 10/21/25. We replay the {true,+243} cell and, after each accepted seat-A PlayActions,
/// probe whether any in-play/cemetery card has that id with a resolved cost/spellboost. We report
/// whether the spellboost play was ever reached at all.</summary>
[Test]
public void Spellboost_play_resolution_under_random_draw_plus_spin()
{
const int SpellboostCardId = 101314020;
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
var engine = new SessionBattleEngine();
engine.Setup(masterSeed: CaptureReplay.SeedFrom(cl1), seatADeck: deckA, seatBDeck: deckB);
engine.DebugSeedIdxChange(SeatASeed, SeatBSeed);
engine.DebugSetRandomDraw(true);
// setup frames
engine.Receive(Receive(cl1, nameof(NetworkBattleUri.Deal)).Env, isPlayerSeat: true);
foreach (var (frames, seat) in new[] { (cl1, true), (cl2, false) })
{
engine.Receive(Receive(frames, nameof(NetworkBattleUri.Swap)).Env, isPlayerSeat: seat);
engine.Receive(Receive(frames, nameof(NetworkBattleUri.Ready)).Env, isPlayerSeat: seat);
}
engine.DebugSpinPreroll(WireSpin);
int acceptedSeatAPlays = 0, divergedBeforeFirstPlay = 0;
bool spellboostResolved = false;
int sbCost = -999, sbCharge = -999;
var sends = SendsWithRawBody(cl1, cl2).Where(x => !SkipUris.Contains(x.Frame.Uri)).ToList();
bool sawFirstPlay = false;
foreach (var x in sends)
{
bool isPlay = x.Frame.Uri == nameof(NetworkBattleUri.PlayActions);
var r = engine.Receive(x.Frame.Env, isPlayerSeat: x.Seat);
if (isPlay && !sawFirstPlay) { sawFirstPlay = true; if (r.Diverged) divergedBeforeFirstPlay++; }
if (isPlay && x.Seat && !r.Diverged)
{
acceptedSeatAPlays++;
int playIdx = ReadPlayIdx(x.Frame.RawBody);
long id = engine.PlayedCardId(true, playIdx, 0);
if (id == SpellboostCardId)
{
spellboostResolved = true;
sbCost = engine.PlayedCardCost(true, playIdx, -1);
sbCharge = engine.PlayedCardSpellboost(true, playIdx, -1);
break;
}
}
}
TestContext.WriteLine($"[spellboost payoff] acceptedSeatAPlays={acceptedSeatAPlays} " +
$"divergedAtFirstPlay={divergedBeforeFirstPlay} spellboostResolved={spellboostResolved} " +
$"cost={sbCost} charge={sbCharge}");
// The replay diverges at the FIRST seat-A play (matrix shows playIdx=8 not in hand), so the
// engine never advances to the later spellboost play — the visible spellboost symptom is NOT
// fixed by {IsRandomDraw+spin} because the prerequisite (aligned draws) is not met.
Assert.That(divergedBeforeFirstPlay, Is.EqualTo(1),
"the FIRST seat-A play already diverges under {IsRandomDraw=true,+spin}");
Assert.That(spellboostResolved, Is.False,
"the spellboost play is never reached because the replay diverges at the first play");
}
private static void ConfirmSpin()
{
foreach (var fn in new[] { "battle_test_fresh_cl1.ndjson", "battle_test_fresh_cl2.ndjson" })
{
var frames = CaptureReplay.Load(fn);
var ready = Receive(frames, nameof(NetworkBattleUri.Ready));
var obj = JsonNode.Parse(ready.RawBody)!.AsObject();
int spin = obj.TryGetPropertyValue("spin", out var s) ? (int)s! : 0;
TestContext.WriteLine($"Confirmed {fn} Ready spin={spin}");
Assert.That(spin, Is.EqualTo(WireSpin), $"{fn} Ready spin must equal {WireSpin}");
}
}
}
}

View File

@@ -0,0 +1,182 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions.Engine;
namespace SVSim.BattleEngine.Tests.SessionEngine
{
/// <summary>
/// PHASE 4 STEP 1 — Tier 2 capture-replay root-cause VERIFICATION (NOT a fix).
///
/// Replays the FRESH smoke captures (battle 907324319325) — battle_test_fresh_cl1/cl2.ndjson —
/// through a <see cref="SessionBattleEngine"/>, then measures whether the per-seat <c>idxChangeSeed</c>
/// the real Ready frame carries is what controls the "Target card was not found in hand cards"
/// divergence symptom.
///
/// FAITHFUL SETUP (the live ShadowIngest only feeds client SENDS, which contain NO Deal/Ready, so a
/// bare send-only replay can't even seat a hand — that conflates "missing Deal" with "missing
/// reshuffle"). To ISOLATE the reshuffle/seed effect we seat each seat's hand from its OWN client's
/// RECEIVE Deal + Swap + Ready (the frames that establish the hand and reach mulligan-end), then replay
/// both clients' interleaved SENDS (the plays). The Ready frame natively carries the real per-seat
/// idxChangeSeed (cl1=1430655717, cl2=661650374), and the engine's recovery receiver calls
/// <c>CreateXorShift</c> from it (NetworkBattleReceiver.cs:1125-1126). The A/B is then:
/// • WITH-SEED: ingest the Ready frame verbatim (idxChangeSeed present) -> XorShift active;
/// • SEED-STRIPPED: ingest the SAME Ready frame with idxChangeSeed forced to -1 -> XorShift inactive
/// (this is exactly the live shadow's effective state, since it never ingests the seed-bearing Ready).
/// The ONLY difference between the two runs is whether the seed reaches CreateXorShift.
///
/// DECK SETUP MECHANISM (feasibility crux, RESOLVED): each side's deck is reconstructed from the
/// capture's <c>Matched.selfDeck</c> (idx-&gt;cardId, the exact shuffled order the node also handed the
/// client) via <see cref="CaptureReplay.SelfDeckFrom"/>; the master seed from <c>Matched.selfInfo.seed</c>.
/// The deck IS in the socket capture — no external fixture needed.
/// </summary>
[TestFixture]
[NonParallelizable]
public class CaptureReplayReshuffleRootCauseTests
{
private static readonly HashSet<string> SkipUris = new()
{
nameof(NetworkBattleUri.Echo),
nameof(NetworkBattleUri.ChatStamp),
nameof(NetworkBattleUri.Gungnir),
};
private static readonly HashSet<string> MulliganUris = new()
{
nameof(NetworkBattleUri.Deal),
nameof(NetworkBattleUri.Swap),
nameof(NetworkBattleUri.Ready),
};
private sealed record ReplayOutcome(
int FrameCount, List<string> Divergences, bool AllDivergencesPostMulligan, bool SelfXorShiftActive);
// Re-parse a captured frame, overriding the Ready body's idxChangeSeed (and oppoIdxChangeSeed if
// present). Used to STRIP the seed (-1) to model the live shadow's seed-less state.
private static MsgEnvelope OverrideReadySeed(CapturedFrame f, int newSeed)
{
var obj = JsonNode.Parse(f.RawBody)!.AsObject();
obj["idxChangeSeed"] = newSeed;
if (obj.ContainsKey("oppoIdxChangeSeed")) obj["oppoIdxChangeSeed"] = newSeed;
return MsgEnvelope.FromJson(obj.ToJsonString());
}
/// <summary>Seat both hands from each client's receive Deal+Swap+Ready, then replay both clients'
/// interleaved SENDS. <paramref name="stripSeed"/> forces the Ready idxChangeSeed to -1 (the live
/// shadow's effective state). Returns divergences + the post-setup self XorShift state.</summary>
private static ReplayOutcome Replay(bool stripSeed)
{
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
Assert.That(deckA, Is.Not.Empty, "cl1 Matched.selfDeck must reconstruct seat A's deck");
Assert.That(deckB, Is.Not.Empty, "cl2 Matched.selfDeck must reconstruct seat B's deck");
var engine = new SessionBattleEngine();
engine.Setup(masterSeed: CaptureReplay.SeedFrom(cl1), seatADeck: deckA, seatBDeck: deckB);
Assert.That(engine.IsReady, Is.True, "engine must seat from the captured decks + seed");
var divergences = new List<string>();
bool sawMulliganEnd = false;
bool anyDivergencePreMulligan = false;
void Ingest(MsgEnvelope env, bool seat, string uri)
{
if (MulliganUris.Contains(uri)) sawMulliganEnd = true;
var r = engine.Receive(env, isPlayerSeat: seat);
if (r.Diverged)
{
divergences.Add($"seat={(seat ? "A" : "B")} {uri}: {Trim(r.RejectReason)}");
if (!sawMulliganEnd) anyDivergencePreMulligan = true;
}
}
// --- Phase 1: seat both hands from the receive setup frames ----------------------------------
// A single Deal seats BOTH opening hands (cl1's receive Deal carries self=A + oppo=B), so we
// ingest Deal ONCE (as seat A) — ingesting both clients' Deals would double-deal (NRE / "Sequence
// contains more than one"). Each seat's Swap then applies that seat's mulligan, and each seat's
// Ready carries THAT seat's idxChangeSeed (cl1's for A, cl2's for B; the recovery receiver consumes
// only the SELF seed per ingest, NetworkBattleReceiver.cs:1126), reaching mulligan-end per seat.
CapturedFrame Receive(IReadOnlyList<CapturedFrame> frames, string uri) =>
frames.First(f => f.Direction == "receive" && f.Uri == uri);
// Deal once (seat A's receive Deal seats both hands).
Ingest(Receive(cl1, nameof(NetworkBattleUri.Deal)).Env, seat: true, nameof(NetworkBattleUri.Deal));
// Each seat's mulligan swap, then each seat's Ready (its own seed).
foreach (var (frames, seat) in new[] { (cl1, true), (cl2, false) })
{
Ingest(Receive(frames, nameof(NetworkBattleUri.Swap)).Env, seat, nameof(NetworkBattleUri.Swap));
var ready = Receive(frames, nameof(NetworkBattleUri.Ready));
var readyEnv = stripSeed ? OverrideReadySeed(ready, -1) : ready.Env;
Ingest(readyEnv, seat, nameof(NetworkBattleUri.Ready));
}
bool selfActive = engine.SelfXorShiftActive;
// --- Phase 2: replay both clients' interleaved SENDS (the plays / turn ops) -------------------
var sends = CaptureReplay.InterleavedSends(cl1, cl2)
.Where(x => !SkipUris.Contains(x.Env.Uri.ToString()))
.ToList();
foreach (var (env, seat) in sends)
Ingest(env, seat, env.Uri.ToString());
return new ReplayOutcome(
FrameCount: sends.Count, divergences, !anyDivergencePreMulligan, selfActive);
}
private static string Trim(string? s) =>
(s ?? "").Split(" @ ")[0];
[Test]
public void Capture_replay_reproduces_post_mulligan_divergence_and_pins_what_the_seed_does_not_fix()
{
var withSeed = Replay(stripSeed: false);
var stripped = Replay(stripSeed: true);
TestContext.WriteLine($"WITH-SEED (Ready idxChangeSeed present): selfXorShiftActive={withSeed.SelfXorShiftActive} " +
$"playFrames={withSeed.FrameCount} divergences={withSeed.Divergences.Count}");
foreach (var d in withSeed.Divergences) TestContext.WriteLine(" DIVERGE " + d);
TestContext.WriteLine($"SEED-STRIPPED (idxChangeSeed=-1, the live shadow state): selfXorShiftActive={stripped.SelfXorShiftActive} " +
$"playFrames={stripped.FrameCount} divergences={stripped.Divergences.Count}");
foreach (var d in stripped.Divergences) TestContext.WriteLine(" DIVERGE " + d);
Assert.Multiple(() =>
{
// (1) The reported symptom reproduces DETERMINISTICALLY from the captures: the replay diverges,
// including the verbatim "Target card was not found in hand cards" exception.
Assert.That(withSeed.Divergences, Is.Not.Empty,
"the capture replay must reproduce the divergence symptom");
Assert.That(withSeed.Divergences.Any(d => d.Contains("not found in hand")), Is.True,
"the reported 'Target card was not found in hand cards' symptom must reproduce");
// (2) All divergences occur AFTER the mulligan barrier — consistent with a post-mulligan cause.
Assert.That(withSeed.AllDivergencesPostMulligan, Is.True, "with-seed divergences are post-mulligan");
Assert.That(stripped.AllDivergencesPostMulligan, Is.True, "stripped divergences are post-mulligan");
// (3) The wire seed DOES drive the engine's XorShift gate (NetworkBattleReceiver.cs:1126):
// present -> active, stripped (the live shadow's state) -> inactive.
Assert.That(withSeed.SelfXorShiftActive, Is.True,
"ingesting the real Ready (idxChangeSeed present) activates the engine's XorShift");
Assert.That(stripped.SelfXorShiftActive, Is.False,
"stripping idxChangeSeed (the live shadow's state) leaves the XorShift inactive");
// (4) THE KEY VERIFICATION FINDING — activating the XorShift via the wire seed does NOT, on its
// own, change the divergence count. The engine's recovery/watch RECEIVE path never performs
// the post-mulligan full-deck reshuffle the live client does: the XorShift's GetChangeInt is
// consumed ONLY by AddToDeckCardIndexChange (BattlePlayerBase.cs:3079) for cards added to the
// deck AFTER mulligan-end, and the per-turn draw is engine-computed off the (un-reshuffled)
// deck order, not driven by the wire's `move idx`. So "feed the seed" alone does NOT fix the
// desync headless — the eventual fix must also make the engine reshuffle the deck post-
// mulligan to match the client (or drive the draw from the wire idx). We PIN this here.
Assert.That(stripped.Divergences.Count, Is.EqualTo(withSeed.Divergences.Count),
"VERIFIED: activating the XorShift via the wire seed alone does NOT change the divergence " +
"count — the engine's receive path does not reshuffle the deck, so the seed is necessary " +
"but NOT sufficient (the fix needs the reshuffle too, not just the seed)");
});
}
}
}

View File

@@ -0,0 +1,215 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using SVSim.BattleNode.Protocol;
using SVSim.BattleNode.Sessions.Engine;
namespace SVSim.BattleEngine.Tests.SessionEngine
{
/// <summary>
/// PHASE 4 — OPTION-A VIABILITY PROBE (TEST-ONLY; no production fix; no Engine/*.cs edits).
///
/// QUESTION: can a per-seat RNG router in the headless engine reliably attribute each StableRandom roll
/// to the correct seat — so two seats can draw from two independent same-seeded sub-streams (mirroring
/// two real clients, each with its OWN _stableRandom)?
///
/// METHOD: replay battle_test_fresh_cl1/cl2 through a <see cref="SessionBattleEngine"/> whose mgr RNG is
/// a logging source. On EVERY roll it records (a) the seat signals the mgr can read from its own state
/// (GetBattlePlayer(true/false).IsSelfTurn — the richest seat signal a mgr-level StableRandom override
/// sees; there is NO "current operating seat" field on the mgr), and (b) the live call stack (where the
/// acting seat is actually visible: MulliganCtrl._battlePlayer / BattlePlayerBase.LotteryRandomDrawCard /
/// OperateReceive.StartOperate spin pre-roll). We dump the rolls for the mulligan lotteries, the first
/// turn draws, and the spin pre-roll, and classify each — reporting whether the seat is UNAMBIGUOUS from
/// mgr STATE vs only from the STACK.
/// </summary>
[TestFixture]
[NonParallelizable]
public class CaptureReplayRngSeatAttributionProbeTests
{
private const int SeatASeed = 1430655717; // cl1 Ready idxChangeSeed
private const int SeatBSeed = 661650374; // cl2 Ready idxChangeSeed
private const int WireSpin = 243;
private static readonly HashSet<string> SkipUris = new()
{
nameof(NetworkBattleUri.Echo),
nameof(NetworkBattleUri.ChatStamp),
nameof(NetworkBattleUri.Gungnir),
};
private static CapturedFrame Receive(IReadOnlyList<CapturedFrame> frames, string uri) =>
frames.First(f => f.Direction == "receive" && f.Uri == uri);
private static IEnumerable<(CapturedFrame Frame, bool Seat)> SendsInTsOrder(
IReadOnlyList<CapturedFrame> cl1, IReadOnlyList<CapturedFrame> cl2) =>
cl1.Where(f => f.Direction == "send").Select(f => (f, Seat: true))
.Concat(cl2.Where(f => f.Direction == "send").Select(f => (f, Seat: false)))
.OrderBy(x => x.f.Ts)
.Select(x => (x.f, x.Seat));
[Test]
public void Roll_log_reveals_whether_acting_seat_is_attributable_from_state_or_only_stack()
{
var cl1 = CaptureReplay.Load("battle_test_fresh_cl1.ndjson");
var cl2 = CaptureReplay.Load("battle_test_fresh_cl2.ndjson");
var deckA = CaptureReplay.SelfDeckFrom(cl1);
var deckB = CaptureReplay.SelfDeckFrom(cl2);
// (5) seeds
int seedA = CaptureReplay.SeedFrom(cl1);
int seedB = CaptureReplay.SeedFrom(cl2);
TestContext.WriteLine($"=== SEEDS (Matched.selfInfo.seed) ===");
TestContext.WriteLine($" cl1 seed = {seedA}");
TestContext.WriteLine($" cl2 seed = {seedB}");
TestContext.WriteLine($" SAME? {seedA == seedB} (Ready idxChangeSeed cl1={SeatASeed} cl2={SeatBSeed} — DIFFERENT)");
TestContext.WriteLine("");
var engine = new SessionBattleEngine();
var log = engine.DebugSetupWithRollLog(masterSeed: seedA, seatADeck: deckA, seatBDeck: deckB);
Assert.That(engine.IsReady, Is.True);
engine.DebugSeedIdxChange(SeatASeed, SeatBSeed);
engine.DebugSetRandomDraw(true); // the gate that makes draws actually ROLL
// mark roll-log boundaries so we can bucket the rolls by phase
int Mark() => log.Count;
int beforeDeal = Mark();
engine.Receive(Receive(cl1, nameof(NetworkBattleUri.Deal)).Env, isPlayerSeat: true);
int afterDeal = Mark();
// seat A mulligan (Swap+Ready) then seat B mulligan
engine.Receive(Receive(cl1, nameof(NetworkBattleUri.Swap)).Env, isPlayerSeat: true);
int afterSwapA = Mark();
engine.Receive(Receive(cl1, nameof(NetworkBattleUri.Ready)).Env, isPlayerSeat: true);
int afterReadyA = Mark();
engine.Receive(Receive(cl2, nameof(NetworkBattleUri.Swap)).Env, isPlayerSeat: false);
int afterSwapB = Mark();
engine.Receive(Receive(cl2, nameof(NetworkBattleUri.Ready)).Env, isPlayerSeat: false);
int afterReadyB = Mark();
// spin pre-roll (one client's 243 advance, applied once on the shared stream)
engine.DebugSpinPreroll(WireSpin);
int afterSpin = Mark();
// replay both clients' interleaved sends (the plays + turn ops -> turn-start draws fire here)
var sends = SendsInTsOrder(cl1, cl2).Where(x => !SkipUris.Contains(x.Frame.Uri)).ToList();
foreach (var x in sends)
engine.Receive(x.Frame.Env, isPlayerSeat: x.Seat);
int afterSends = Mark();
TestContext.WriteLine("=== ROLL-COUNT BY PHASE (IsRandomDraw=true) ===");
TestContext.WriteLine($" Deal : {afterDeal - beforeDeal}");
TestContext.WriteLine($" Swap A : {afterSwapA - afterDeal}");
TestContext.WriteLine($" Ready A (mulligan): {afterReadyA - afterSwapA}");
TestContext.WriteLine($" Swap B : {afterSwapB - afterReadyA}");
TestContext.WriteLine($" Ready B (mulligan): {afterReadyB - afterSwapB}");
TestContext.WriteLine($" spin pre-roll : {afterSpin - afterReadyB} (expected {WireSpin})");
TestContext.WriteLine($" all sends/plays : {afterSends - afterSpin}");
TestContext.WriteLine($" TOTAL : {log.Count}");
TestContext.WriteLine("");
DumpRange("DEAL", log, beforeDeal, afterDeal);
DumpRange("SWAP A (mulligan lottery, seat A)", log, afterDeal, afterSwapA);
DumpRange("READY A (mulligan, seat A)", log, afterSwapA, afterReadyA);
DumpRange("SWAP B (mulligan lottery, seat B)", log, afterReadyA, afterSwapB);
DumpRange("READY B (mulligan, seat B)", log, afterSwapB, afterReadyB);
DumpSpinSummary("SPIN PRE-ROLL", log, afterReadyB, afterSpin);
// first ~12 of the play phase covers the early turn-start draws for both seats
DumpRange("FIRST PLAY-PHASE ROLLS (turn draws + effects)", log, afterSpin,
System.Math.Min(afterSpin + 12, afterSends));
// === STATE-vs-STACK attribution analysis ===
AnalyzeAttribution(log, afterSpin);
Assert.Pass($"probe complete: {log.Count} rolls logged; see TestContext output for attribution analysis");
}
private static void DumpRange(string label, IReadOnlyList<SessionBattleEngine.RollEntry> log, int from, int to)
{
TestContext.WriteLine($"--- {label} [rolls {from}..{to - 1}] ({to - from} rolls) ---");
for (int i = from; i < to; i++)
{
var e = log[i];
TestContext.WriteLine($" #{e.Index} {e.Api}(arg={e.Arg}) | mgrState: self.IsSelfTurn={e.SelfIsSelfTurn} oppo.IsSelfTurn={e.OppoIsSelfTurn} | classify={Classify(e)}");
TestContext.WriteLine($" stack: {e.Stack}");
}
if (to - from == 0) TestContext.WriteLine(" (none)");
TestContext.WriteLine("");
}
private static void DumpSpinSummary(string label, IReadOnlyList<SessionBattleEngine.RollEntry> log, int from, int to)
{
TestContext.WriteLine($"--- {label} [rolls {from}..{to - 1}] ({to - from} rolls) ---");
if (to - from > 0)
{
var first = log[from];
TestContext.WriteLine($" first spin roll #{first.Index}: self.IsSelfTurn={first.SelfIsSelfTurn} oppo.IsSelfTurn={first.OppoIsSelfTurn}");
TestContext.WriteLine($" stack: {first.Stack}");
bool allStateIdentical = log.Skip(from).Take(to - from)
.All(e => e.SelfIsSelfTurn == first.SelfIsSelfTurn && e.OppoIsSelfTurn == first.OppoIsSelfTurn);
bool allViaStartOperate = log.Skip(from).Take(to - from).All(e => e.Stack.Contains("StartOperate"));
TestContext.WriteLine($" all {to - from} spin rolls have identical mgr seat-state? {allStateIdentical}");
TestContext.WriteLine($" all {to - from} spin rolls routed via OperateReceive.StartOperate? {allViaStartOperate}");
}
TestContext.WriteLine("");
}
// Best-effort classification from the STACK (the ground truth of who is rolling).
private static string Classify(SessionBattleEngine.RollEntry e)
{
string s = e.Stack;
if (s.Contains("StartOperate")) return "SPIN-PREROLL";
if (s.Contains("_LotMulliganCardIndex") || s.Contains("MulliganCtrl")) return "MULLIGAN-LOTTERY";
if (s.Contains("LotteryRandomDrawCard") || s.Contains("RandomCardDraw")) return "TURN/EFFECT-DRAW";
if (s.Contains("SkillRandomSelectFilter")) return "SKILL-FILTER-DRAW";
return "OTHER-EFFECT";
}
private static void AnalyzeAttribution(IReadOnlyList<SessionBattleEngine.RollEntry> log, int playPhaseStart)
{
TestContext.WriteLine("=== STATE-vs-STACK ATTRIBUTION ANALYSIS ===");
// 1) Does mgr-state (IsSelfTurn flags) ever change across the whole replay? If both flags are
// pinned at setup values (self=true/oppo=false) the entire time, mgr-state CANNOT distinguish
// seats — every roll looks identical from mgr state.
var distinctStates = log
.Select(e => (e.SelfIsSelfTurn, e.OppoIsSelfTurn))
.Distinct()
.ToList();
TestContext.WriteLine($" distinct mgr seat-states observed across ALL {log.Count} rolls: {distinctStates.Count}");
foreach (var st in distinctStates)
TestContext.WriteLine($" (self.IsSelfTurn={st.Item1}, oppo.IsSelfTurn={st.Item2})");
// 2) For the mulligan lotteries: seat A's 6 rolls then seat B's 6 rolls happen back-to-back. Are
// their mgr-states distinguishable? (They should NOT be — IsSelfTurn isn't toggled during
// mulligan; both lotteries run with the same setup-time flags.)
var mulliganRolls = log.Where(e => Classify(e) == "MULLIGAN-LOTTERY").ToList();
var mulliganStates = mulliganRolls.Select(e => (e.SelfIsSelfTurn, e.OppoIsSelfTurn)).Distinct().Count();
TestContext.WriteLine($" mulligan-lottery rolls: {mulliganRolls.Count}; distinct mgr seat-states among them: {mulliganStates}");
TestContext.WriteLine($" -> seat attributable from mgr STATE alone? {(mulliganStates >= 2 ? "MAYBE" : "NO (state identical for both seats' lotteries)")}");
bool mulliganSeatInStack = mulliganRolls.All(e => e.Stack.Contains("Mulligan") || e.Stack.Contains("_LotMulligan"));
TestContext.WriteLine($" -> mulligan rolls carry a MulliganCtrl frame on the stack? {mulliganSeatInStack}");
// 3) For the play-phase draws: are turn-start draws present at all, and do their mgr-states track
// the acting seat (i.e. does IsSelfTurn flip to identify whose turn/draw it is)?
var drawRolls = log.Skip(playPhaseStart)
.Where(e => Classify(e) is "TURN/EFFECT-DRAW" or "SKILL-FILTER-DRAW")
.ToList();
TestContext.WriteLine($" play-phase draw/filter rolls: {drawRolls.Count}");
if (drawRolls.Count > 0)
{
var drawStates = drawRolls.Select(e => (e.SelfIsSelfTurn, e.OppoIsSelfTurn)).Distinct().Count();
TestContext.WriteLine($" distinct mgr seat-states among draw rolls: {drawStates}");
}
TestContext.WriteLine("");
TestContext.WriteLine(" INTERPRETATION:");
TestContext.WriteLine(" * If distinct mgr seat-states == 1 for a phase, the StableRandom override CANNOT");
TestContext.WriteLine(" attribute that phase's rolls to a seat from mgr state — only the call STACK");
TestContext.WriteLine(" (MulliganCtrl._battlePlayer / BattlePlayerBase 'this' / OperateReceive._isPlayer)");
TestContext.WriteLine(" names the acting seat.");
}
}
}