[P0] Introduce an arbitrated AttributionWindow registry for relic/effect one-shot windows #250

Closed
opened 2026-07-03 02:30:49 -07:00 by nelsong6 · 3 comments
nelsong6 commented 2026-07-03 02:30:49 -07:00 (Migrated from github.com)

Priority: P0. From the 2026-07-02 codebase audit. This is the single highest-leverage refactor — it retires an entire class of confirmed and likely double-attribution bugs.

Problem

~14 hand-rolled, independent, non-exclusive attribution flags feed the same game event to multiple consumers with no arbitration, so overlapping armed windows double- or mis-attribute. HookAfterBlockGainedPatch feeds one player block-gain event to Orichalcum / The Abacus / Bone Flute / Cloak Clasp recorders simultaneously.

Confirmed instances (verified against decompiled game code):

  • Gremlin Horn energy + draws double-counted: recorded under the relic AND under whatever card was resolving when the enemy died (PlayerGainEnergyPatch.cs:34 + GremlinHornStatsPatch.cs:54; RunTracker.cs RecordEnergyGained/RecordGremlinHornEnergyGained, draw path via Hook.ShouldDraw→NoteDrawAttempt).
  • Cloak Clasp window is non-consuming and stays open from the relic's BeforeSideTurnEnd until Hook.AfterTurnEnd, absorbing every other block gain in the rest of the end-of-turn chain and falsely consuming Orichalcum's one-shot (RunTracker.cs:3112; decompiled CloakClasp grants block in one GainBlock call so the open window is unnecessary).

Likely instances (internally consistent, verify against the decompile before fixing):

  • Happy Flower disarm hook fires before its arm hook, so the flag survives 2/3 turns (HappyFlowerAfterSideTurnStartPatch.cs:30).
  • Orichalcum one-shot consumed by the first block gain after arming regardless of source.

Build

An AttributionWindow / WindowRegistry on the per-combat context:

  • Arm(key, ownerId, exclusive, maxHistoryAdvance) / Disarm(key) / TryConsume(eventKind) -> window?
  • Exclusive consumption: at most one armed window claims a given event.
  • An always-on (rate-limited) log when 2+ windows for the same event kind are armed at once — makes overlap visible.
  • Fold in the history-count staleness rule (see the poison-window issue) so a never-consumed window auto-expires.
  • Auto-clear every window at the combat boundary (kills the "forgot to reset" class — see the Bone Flute case already fixed inline, and _pendingRelicHeals / _pendingWhiteBeastPotionRewards which are never reset at run boundary).

Port each existing one-shot onto it one at a time (Orichalcum, Abacus, Bone Flute, Cloak Clasp, Happy Flower, Gremlin Horn ×2, block-clear, poison-tick, Noxious Fumes, effect-source, recent-play), each pinned by its existing per-relic test. Make Orichalcum arm only when it will fire (read _shouldTrigger) and Cloak Clasp arm only on its real hand-count condition, both consuming.

Relates to: the STS2-compat issue (this rework removes the dependency on the now-removed Hook.AfterTurnEnd disarm).

**Priority: P0.** From the 2026-07-02 codebase audit. This is the single highest-leverage refactor — it retires an entire class of confirmed and likely double-attribution bugs. ## Problem ~14 hand-rolled, independent, non-exclusive attribution flags feed the **same** game event to multiple consumers with no arbitration, so overlapping armed windows double- or mis-attribute. `HookAfterBlockGainedPatch` feeds one player block-gain event to Orichalcum / The Abacus / Bone Flute / Cloak Clasp recorders simultaneously. Confirmed instances (verified against decompiled game code): - **Gremlin Horn** energy + draws double-counted: recorded under the relic AND under whatever card was resolving when the enemy died (`PlayerGainEnergyPatch.cs:34` + `GremlinHornStatsPatch.cs:54`; `RunTracker.cs` `RecordEnergyGained`/`RecordGremlinHornEnergyGained`, draw path via `Hook.ShouldDraw`→`NoteDrawAttempt`). - **Cloak Clasp** window is non-consuming and stays open from the relic's `BeforeSideTurnEnd` until `Hook.AfterTurnEnd`, absorbing every other block gain in the rest of the end-of-turn chain and falsely consuming Orichalcum's one-shot (`RunTracker.cs:3112`; decompiled CloakClasp grants block in one `GainBlock` call so the open window is unnecessary). Likely instances (internally consistent, **verify against the decompile before fixing**): - **Happy Flower** disarm hook fires before its arm hook, so the flag survives 2/3 turns (`HappyFlowerAfterSideTurnStartPatch.cs:30`). - Orichalcum one-shot consumed by the first block gain after arming regardless of source. ## Build An `AttributionWindow` / `WindowRegistry` on the per-combat context: - `Arm(key, ownerId, exclusive, maxHistoryAdvance)` / `Disarm(key)` / `TryConsume(eventKind) -> window?` - **Exclusive consumption**: at most one armed window claims a given event. - An always-on (rate-limited) log when 2+ windows for the same event kind are armed at once — makes overlap visible. - Fold in the history-count staleness rule (see the poison-window issue) so a never-consumed window auto-expires. - Auto-clear every window at the combat boundary (kills the "forgot to reset" class — see the Bone Flute case already fixed inline, and `_pendingRelicHeals` / `_pendingWhiteBeastPotionRewards` which are never reset at run boundary). Port each existing one-shot onto it one at a time (Orichalcum, Abacus, Bone Flute, Cloak Clasp, Happy Flower, Gremlin Horn ×2, block-clear, poison-tick, Noxious Fumes, effect-source, recent-play), each pinned by its existing per-relic test. Make Orichalcum arm only when it will fire (read `_shouldTrigger`) and Cloak Clasp arm only on its real hand-count condition, both consuming. Relates to: the STS2-compat issue (this rework removes the dependency on the now-removed `Hook.AfterTurnEnd` disarm).
nelsong6 commented 2026-07-03 09:34:21 -07:00 (Migrated from github.com)

Uncapped audit pass — additional confirmed overlaps to fold into the registry (each pinned by a coexistence test):

  • Cloak Clasp's non-consuming open window double-books Orichalcum's block every turn (#18, #47) — make it a one-shot / capture around the relic call.
  • HookAfterBlockGainedPatch fan-out feeds the same block gain to 4 non-exclusive consumers (#142) — first-match-consumes.
  • Happy Flower arms at AfterSideTurnStart but its cleanup patch targets AfterPlayerTurnStart, which fires before it, so on 2 of 3 turns the flag eats an unrelated energy gain (#46, HIGH) — trigger-gate the arm, delete the wrong-boundary cleanup.
  • Noxious Fumes window has a recipient budget but no temporal bound and arms unconditionally (#31).
**Uncapped audit pass — additional confirmed overlaps to fold into the registry (each pinned by a coexistence test):** - Cloak Clasp's non-consuming open window double-books Orichalcum's block every turn (#18, #47) — make it a one-shot / capture around the relic call. - `HookAfterBlockGainedPatch` fan-out feeds the same block gain to 4 non-exclusive consumers (#142) — first-match-consumes. - Happy Flower arms at `AfterSideTurnStart` but its cleanup patch targets `AfterPlayerTurnStart`, which fires *before* it, so on 2 of 3 turns the flag eats an unrelated energy gain (#46, HIGH) — trigger-gate the arm, delete the wrong-boundary cleanup. - Noxious Fumes window has a recipient budget but no temporal bound and arms unconditionally (#31).
nelsong6 commented 2026-07-03 11:16:00 -07:00 (Migrated from github.com)

Implemented (confirmed-buggy subset) in #268: new AttributionWindowRegistry (per-combat, FIFO, exclusive, lives on PendingCombat so it self-resets). Multiple windows per kind arbitrated FIFO at consume (Orichalcum+CloakClasp both fire legitimately in one turn). Ported block (Orichalcum/Anchor/Abacus/BoneFlute/CloakClasp), energy (HappyFlower/BoomingConch/GremlinHorn + card fallback), draw (GremlinHorn) via single dispatchers replacing the fan-out. Fixes the 4 confirmed double-attribution bugs. Arbitration core pinned by 9 unit tests. DEFERRED (non-buggy, kept as-is): Akabeko, Pendulum, Parrying Shield, Horn Cleat. Old per-relic Record methods left as dead code pending cleanup. NOT yet behaviorally verified in live combat (needs the 4 relic setups).

Implemented (confirmed-buggy subset) in #268: new AttributionWindowRegistry (per-combat, FIFO, exclusive, lives on PendingCombat so it self-resets). Multiple windows per kind arbitrated FIFO at consume (Orichalcum+CloakClasp both fire legitimately in one turn). Ported block (Orichalcum/Anchor/Abacus/BoneFlute/CloakClasp), energy (HappyFlower/BoomingConch/GremlinHorn + card fallback), draw (GremlinHorn) via single dispatchers replacing the fan-out. Fixes the 4 confirmed double-attribution bugs. Arbitration core pinned by 9 unit tests. DEFERRED (non-buggy, kept as-is): Akabeko, Pendulum, Parrying Shield, Horn Cleat. Old per-relic Record methods left as dead code pending cleanup. NOT yet behaviorally verified in live combat (needs the 4 relic setups).
nelsong6 commented 2026-07-05 15:45:15 -07:00 (Migrated from github.com)

Closing during repository issue cleanup. If this becomes active work again, we can reopen or recreate it as a fresh, specific task.

Closing during repository issue cleanup. If this becomes active work again, we can reopen or recreate it as a fresh, specific task.
Sign in to join this conversation.
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#250
No description provided.