Defer relic classification normalization until the game's model database exists #321

Merged
nelsong6 merged 1 commit from claude/strange-wiles-445c7a into main 2026-08-12 20:46:48 -07:00
nelsong6 commented 2026-08-12 20:46:43 -07:00 (Migrated from github.com)

The bug

RelicClassificationStore.Initialize() enumerated ModelDb.AllRelics from CoreMain.Initialize(). On a cold start that runs too early, and the enumeration threw:

[ERROR] [SpireLens] Could not enumerate current relics for classification:
        The given key 'CHARACTER.IRONCLAD' was not present in the dictionary.

ModelDb.AllRelics reaches AllCharacters before it touches a single relic, and ModelDb.Get<T>() uses a raw dictionary indexer — hence a KeyNotFoundException on a character key during a relic enumeration.

Why it only happens at startup

The game loads mods before it builds the model database, on purpose (so ModelDb and LocManager pick up mod injections). The observed stack trace:

NGame._EnterTree -> GameStartup -> OneTimeInitialization.ExecuteVeryEarly
  -> ModManager.Initialize -> TryLoadMod -> CallModInitializer
  -> LoaderMain.Initialize -> LoadCore -> CoreMain.Initialize
  -> RelicClassificationStore.Initialize -> NormalizeAgainstCurrentRelics

ModelDb.Init() is one step later, in OneTimeInitialization.ExecuteEssential. A Core hot reload re-enters CoreMain.Initialize() long after that, so it always succeeds — which is exactly why a normal player session permanently ran an unreconciled store while every dev session that pressed F5 got the correct one. The bug was invisible in dev.

What it cost

The enumeration was wrapped in a log-only try/catch, so the session continued with the reconciliation pass skipped:

  • relics the document never listed were never defaulted into CombatIds — and SetCombatRelevantUntilTurn gates on CombatIds.Contains(relicId), so the compendium's "hide after turn N" control would silently refuse to stick for any relic a game update added;
  • ids the game no longer defines were never pruned, and the cold-start session wrote the stale shape back to disk;
  • turn cutoffs were never validated against the 1-3 range or against still being a combat relic.

The relic bar's own hide/show decisions were not affected: they read NonCombatIds and CombatRelevantUntilTurns, both populated straight from the document. This is a latent-correctness fix — it pays off the next time MegaCrit adds or renames a relic.

The fix

  • IsLiveRelicDatabaseReady() probes with ModelDb.Contains(typeof(Ironclad)) — the same key, but ContainsKey answers false on an empty database instead of throwing. Probing rather than catching matters: a catch would cost one throw per relic on the bar-refresh path for as long as the database stayed empty.
  • Initialize() attempts normalization and, when it is too early, says so in its log line rather than logging an error.
  • Every accessor routes through EnsureReady(), which reconciles on the first read or write. The relic-bar filter therefore normalizes before it answers rather than reading a degraded store.
  • The reconciliation rules move into a pure NormalizeClassifications(...) over the collections it is handed, and only persist when the pass actually changed something.

Verification

Built, deployed, and hot-reloaded into the running game. --- HOT RELOAD DONE ---, Core.Initialize complete, Patch install: 428 classes patched, 0 failed/skipped, no Core.Initialize threw, and the load line printed without the deferred-normalization suffix — the new code ran and took the database-ready branch. No restart required: no Harmony target was added or changed.

Adds RelicClassificationNormalizationTests (7 cases). One asserts the readiness probe reports not-ready instead of throwing against an uninitialized ModelDb — the test host is the cold-start state, so that case pins the exact branch that was broken. Per the repo's user-owned verification rule the suite was not run; both projects compile clean.

Documents the ordering trap in docs/sts2-runtime-primer.md under "Core Loads Before The Game's Model Database Exists", with a pointer from docs/architecture.md.

## The bug `RelicClassificationStore.Initialize()` enumerated `ModelDb.AllRelics` from `CoreMain.Initialize()`. On a cold start that runs too early, and the enumeration threw: ``` [ERROR] [SpireLens] Could not enumerate current relics for classification: The given key 'CHARACTER.IRONCLAD' was not present in the dictionary. ``` `ModelDb.AllRelics` reaches `AllCharacters` before it touches a single relic, and `ModelDb.Get<T>()` uses a raw dictionary indexer — hence a `KeyNotFoundException` on a *character* key during a *relic* enumeration. ## Why it only happens at startup The game loads mods before it builds the model database, on purpose (so `ModelDb` and `LocManager` pick up mod injections). The observed stack trace: ``` NGame._EnterTree -> GameStartup -> OneTimeInitialization.ExecuteVeryEarly -> ModManager.Initialize -> TryLoadMod -> CallModInitializer -> LoaderMain.Initialize -> LoadCore -> CoreMain.Initialize -> RelicClassificationStore.Initialize -> NormalizeAgainstCurrentRelics ``` `ModelDb.Init()` is one step later, in `OneTimeInitialization.ExecuteEssential`. A Core hot reload re-enters `CoreMain.Initialize()` long after that, so it always succeeds — which is exactly why a normal player session permanently ran an unreconciled store while every dev session that pressed F5 got the correct one. The bug was invisible in dev. ## What it cost The enumeration was wrapped in a log-only try/catch, so the session continued with the reconciliation pass skipped: - relics the document never listed were never defaulted into `CombatIds` — and `SetCombatRelevantUntilTurn` gates on `CombatIds.Contains(relicId)`, so the compendium's "hide after turn N" control would silently refuse to stick for any relic a game update added; - ids the game no longer defines were never pruned, and the cold-start session wrote the stale shape back to disk; - turn cutoffs were never validated against the 1-3 range or against still being a combat relic. The relic bar's own hide/show decisions were not affected: they read `NonCombatIds` and `CombatRelevantUntilTurns`, both populated straight from the document. This is a latent-correctness fix — it pays off the next time MegaCrit adds or renames a relic. ## The fix - `IsLiveRelicDatabaseReady()` probes with `ModelDb.Contains(typeof(Ironclad))` — the same key, but `ContainsKey` answers `false` on an empty database instead of throwing. Probing rather than catching matters: a catch would cost one throw per relic on the bar-refresh path for as long as the database stayed empty. - `Initialize()` attempts normalization and, when it is too early, says so in its log line rather than logging an error. - Every accessor routes through `EnsureReady()`, which reconciles on the first read or write. The relic-bar filter therefore normalizes before it answers rather than reading a degraded store. - The reconciliation rules move into a pure `NormalizeClassifications(...)` over the collections it is handed, and only persist when the pass actually changed something. ## Verification Built, deployed, and hot-reloaded into the running game. `--- HOT RELOAD DONE ---`, `Core.Initialize complete`, `Patch install: 428 classes patched, 0 failed/skipped`, no `Core.Initialize threw`, and the load line printed without the deferred-normalization suffix — the new code ran and took the database-ready branch. No restart required: no Harmony target was added or changed. Adds `RelicClassificationNormalizationTests` (7 cases). One asserts the readiness probe reports not-ready instead of throwing against an uninitialized `ModelDb` — the test host *is* the cold-start state, so that case pins the exact branch that was broken. Per the repo's user-owned verification rule the suite was not run; both projects compile clean. Documents the ordering trap in `docs/sts2-runtime-primer.md` under "Core Loads Before The Game's Model Database Exists", with a pointer from `docs/architecture.md`.
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!321
No description provided.