Adv 2.0 D&D Phase R7: post-completion audit hardening

High-priority fixes from the multi-agent audit of Adventure 2.0:

- Phase 11 nil-deref cluster: zoneOrFallback() helper replaces 8 unsafe
  getZone(_) sites in dnd_zone_cmd.go. Corrupted zone IDs render a
  placeholder instead of panicking.
- Briefing/recap idempotency: deliverBriefing/deliverRecap now claim
  the rollover via a conditional UPDATE. Double-fires from clock skew
  or restart become no-ops; supply burn and day++ no longer reapply.
- Graceful ticker shutdown: AdventurePlugin gains stopCh + Stop(); all
  11 background tickers now select on stopCh in addition to ticker.C.
- Mood decay (§3.2): math.Round(elapsed*2) replaces int() truncation
  so sub-hour gaps decay correctly.
- 24h auto-abandon (§4.3): getActiveZoneRun returns clean slate and
  abandons stale runs whose LastActionAt is over 24h old.
- Respec / auto-migrate orphan cleanup: !respec and the auto-migrated
  draft wipe now abandon active zone runs and expeditions before
  deleting the dnd_character row.
- Phase R combat-link: applyBossDefeatThreat now wired from
  resolveBossRoom (-20 threat); applyRoomCombatThreatForUser adds
  +5/+8 from non-boss/elite kills (§8.1).
- Starvation → forced extraction: briefing-time check forces extract
  with §10.2 coin tax when supplies hit zero.
- GMNat20/Nat1 narration wired into resolveCombatRoom and
  resolveBossRoom (nat-20 takes precedence over nat-1).
- Treasure-undo race: LoadAndDelete on both timer-fire and `undo`
  paths so only one side wins.
- Battle Master: Disarming, Menacing, Parry maneuvers added (3 → 6
  of 10). Remaining 4 (Pushing, Goading, Riposte, Commander's Strike)
  documented inline as needing ally/reaction mechanics the engine
  doesn't model.
- Threat-70 warning: tracked in RegionState["siege_warning_fired"]
  so a drop-and-recross doesn't re-fire the beat.
- region_state JSON decode error now logged via slog.Warn instead
  of silently discarded.

Failing TestProdDB_DnDLayer fixed via option (a): track migrated
characters and only run round-trip / idempotency assertions on those,
skipping pre-existing prod-DB rows accumulated from live bot use.

New tests in dnd_audit_phase_R7_test.go cover: 24h auto-abandon,
briefing double-fire idempotency, threat-70 warning idempotency,
multi-region extract→resume state preservation, and starvation
forced-extraction.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
prosolis
2026-05-08 19:29:54 -07:00
parent 8a1d9a16ce
commit 45863bd5f3
19 changed files with 837 additions and 273 deletions

View File

@@ -406,6 +406,15 @@ func (p *AdventurePlugin) handleDnDRespecCmd(ctx MessageContext) error {
if err := wipeSpellsForUser(ctx.Sender); err != nil {
slog.Error("dnd: respec spell wipe", "user", ctx.Sender, "err", err)
}
// Phase R hardening: a respec also abandons any active zone-run /
// expedition keyed to the wiped character so they don't orphan with
// pointers to a stat sheet that no longer exists.
if err := abandonZoneRun(ctx.Sender); err != nil && err != ErrNoActiveRun {
slog.Warn("dnd: respec zone-run cleanup", "user", ctx.Sender, "err", err)
}
if err := abandonExpedition(ctx.Sender); err != nil && err != ErrNoActiveExpedition {
slog.Warn("dnd: respec expedition cleanup", "user", ctx.Sender, "err", err)
}
// Debit last. If this fails (rare race — euros spent elsewhere between
// the pre-check and now), the player got a free respec. Strictly better
// than the alternative of euros-lost-with-wipe-not-applied.
@@ -453,6 +462,14 @@ func loadOrInitDraft(userID id.UserID) (*DnDCharacter, error) {
); err != nil {
return nil, fmt.Errorf("clear auto-migrated row: %w", err)
}
// Auto-migrated wipe: also abandon active zone-run /
// expedition so they don't orphan against the deleted row.
if err := abandonZoneRun(userID); err != nil && err != ErrNoActiveRun {
slog.Warn("dnd: auto-migrated wipe zone-run cleanup", "user", userID, "err", err)
}
if err := abandonExpedition(userID); err != nil && err != ErrNoActiveExpedition {
slog.Warn("dnd: auto-migrated wipe expedition cleanup", "user", userID, "err", err)
}
// fall through to fresh draft
} else if !c.PendingSetup {
return nil, fmt.Errorf("character already finalized — use !respec")