Stop a finished run getting a second, empty record #300

Merged
nelsong6 merged 2 commits from claude/fix-duplicate-run-record into main 2026-08-08 11:18:49 -07:00
nelsong6 commented 2026-08-08 11:07:47 -07:00 (Migrated from github.com)

Symptom

Run history showed all-zero card stats for a real, completed run. Hovering
Glacier reported Played 0/0, in deck 0, everything zero — while correctly
showing Received floor 25, came upgraded +1.

Two independent defects

Creation. OnRunEnded deliberately nulls _currentRun. But ~40
Record*/getter paths funnel through EnsureLazyCurrentRunLocked, which
re-creates it on demand and cannot distinguish "no run yet" — the legitimate
hot-loaded-mid-run case it exists for — from "run just ended". One post-run
event resurrects the finished game as a second, empty record, stamped with the
same game_start_time read from the still-live RunManager.

Observed on disk: a win at floor 49, then 9 ms later an in_progress record with
null character/ascension/floor, 0 events, and 106 freshly renumbered card
aggregates carrying real floor_added metadata but zero counters. No
RunStarted log line, confirming it did not come from the run-start path.

Resolution. FindHistoricalByGameStartTime sorted by newest write time and
returned the first game_start_time match. The stray is written after the real
record, so newest-mtime picked the empty one.

Fix

Guard the write, not the ~40 call sites. A finished game's record is written
exactly once, by OnRunEnded. SaveCurrentRun now refuses to persist a record
whose game_start_time belongs to an already-terminal run. A resurrected
instance may still accumulate in memory; it is discarded at the next run start
and never reaches disk.

Rank duplicates instead of taking the first match. Finished beats
in-progress, because a game run cannot un-finish. Then higher floor, which
separates a real run from an empty stand-in. Write time only breaks remaining
ties. This half recovers runs whose duplicate is already on disk, with no file
cleanup, and logs a warning so a duplicate stays visible rather than being
silently resolved.

Both halves are pure functions (RunTracker.IsResurrectedEndedRun,
RunStorage.SelectBestRunFileCandidate) with unit coverage: every terminal
outcome, the ended run writing itself, a genuinely new run, the legitimate lazy
mint, missing start time, and each tier of the ranking.

Verification

Deployed and hot-reloaded; the live lookup now resolves the real run:

[WARN] FindHistoricalByGameStartTime: 2 run files share game_start_time=1786054674;
chose ae7dd19d4c224f898202c4319ef20734.json (finished=True, floor=49).
Others: 8bda8c8afb2d4f4ca9fcaa517c9b43fc.json(finished=False, floor=-1)

No new Harmony targets, so hot reload is sufficient — no restart. Unit tests were
not run, per the repo user-owned verification rule.

Not fixed here

The underlying design smell remains: run lifecycle is created as a side effect
from ~40 scattered call sites, two of which are read-only getters
(GetMutablePotionHistoryLocked, GetMutableMaxHpHistoryLocked) — reading stats
can create a run. Lifecycle should have exactly one owner. That refactor is
larger than this fix and deliberately left separate.

## Symptom Run history showed all-zero card stats for a real, completed run. Hovering Glacier reported `Played 0/0`, `in deck 0`, everything zero — while correctly showing `Received floor 25, came upgraded +1`. ## Two independent defects **Creation.** `OnRunEnded` deliberately nulls `_currentRun`. But ~40 `Record*`/getter paths funnel through `EnsureLazyCurrentRunLocked`, which re-creates it on demand and cannot distinguish *"no run yet"* — the legitimate hot-loaded-mid-run case it exists for — from *"run just ended"*. One post-run event resurrects the finished game as a second, empty record, stamped with the same `game_start_time` read from the still-live `RunManager`. Observed on disk: a win at floor 49, then 9 ms later an `in_progress` record with null character/ascension/floor, 0 events, and 106 freshly renumbered card aggregates carrying real `floor_added` metadata but zero counters. No `RunStarted` log line, confirming it did not come from the run-start path. **Resolution.** `FindHistoricalByGameStartTime` sorted by newest write time and returned the first `game_start_time` match. The stray is written *after* the real record, so newest-mtime picked the empty one. ## Fix **Guard the write, not the ~40 call sites.** A finished game's record is written exactly once, by `OnRunEnded`. `SaveCurrentRun` now refuses to persist a record whose `game_start_time` belongs to an already-terminal run. A resurrected instance may still accumulate in memory; it is discarded at the next run start and never reaches disk. **Rank duplicates instead of taking the first match.** Finished beats in-progress, because a game run cannot un-finish. Then higher floor, which separates a real run from an empty stand-in. Write time only breaks remaining ties. This half recovers runs whose duplicate is *already* on disk, with no file cleanup, and logs a warning so a duplicate stays visible rather than being silently resolved. Both halves are pure functions (`RunTracker.IsResurrectedEndedRun`, `RunStorage.SelectBestRunFileCandidate`) with unit coverage: every terminal outcome, the ended run writing itself, a genuinely new run, the legitimate lazy mint, missing start time, and each tier of the ranking. ## Verification Deployed and hot-reloaded; the live lookup now resolves the real run: ``` [WARN] FindHistoricalByGameStartTime: 2 run files share game_start_time=1786054674; chose ae7dd19d4c224f898202c4319ef20734.json (finished=True, floor=49). Others: 8bda8c8afb2d4f4ca9fcaa517c9b43fc.json(finished=False, floor=-1) ``` No new Harmony targets, so hot reload is sufficient — no restart. Unit tests were not run, per the repo user-owned verification rule. ## Not fixed here The underlying design smell remains: run lifecycle is created as a side effect from ~40 scattered call sites, two of which are read-only getters (`GetMutablePotionHistoryLocked`, `GetMutableMaxHpHistoryLocked`) — reading stats can create a run. Lifecycle should have exactly one owner. That refactor is larger than this fix and deliberately left separate.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/spirelens!300
No description provided.