From a8e88cf0f0cf8729f69745a7974b0019cbb020d3 Mon Sep 17 00:00:00 2001 From: david raistrick <1108844+keen99@users.noreply.github.com> Date: Mon, 29 Jun 2026 16:36:43 -0400 Subject: [PATCH] tooling: audit-state pause+resume paired, guard advance-while-paused MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audit bug: pause fired turn%12, no resume in same iter. nextTurn then called on paused encounter → threw 'Encounter not running'. Throw is correct feature behavior (nextTurn refuses when paused); audit misuse. Fix: togglePause twice (pause+resume) in one iteration, plus guard 'advance-while-paused' check before nextTurn call. Result: 6 audit artifacts → 0 violations / 100 rounds. Confirms BUG-1 resolved as side effect of BUG-2 dup-id fix. Replay verify: 10 rounds, 103 turns, no skip/dupe. TODO: BUG-1 + BUG-2 marked RESOLVED/FIXED. --- TODO.md | 11 +++++------ scripts/audit-state.js | 6 +++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index 64d7e9a..17ea36f 100644 --- a/TODO.md +++ b/TODO.md @@ -25,6 +25,9 @@ ## Confirmed bugs (tests written, NOT fixed) ### BUG-1: addParticipant + pause/resume corrupts turn rotation +- **RESOLVED** as side effect of BUG-2 fix (dup-id rejection broke chain). +- Audit: 0 violations / 100 rounds after BUG-2 fix. +- Replay: 10 rounds clean, no skip/dupe. - Audit: 128 violations / 100 rounds, 4 symptom faces. - Symptom chain (one bug family): 1. pause blocks nextTurn advance → totalTurns stays frozen (e.g. 120) @@ -43,13 +46,9 @@ - Real repro = `node scripts/audit-state.js` (or audit-rotation.js). ### BUG-2: addParticipant allows duplicate id -- `addParticipant(enc, dup)` appends same id to participants[] twice. -- togglePause resume rebuilds order → id appears twice in turnOrderIds → - nextTurn stuck repeating that id. -- Reachable in normal app? App uses crypto.randomUUID (fresh ids) so - unlikely. But no guard exists — defensive bug. +- **FIXED** (commit: addParticipant throws on dup id). - Test: `shared/tests/turn.characterization.test.js` 'addParticipant rejects - duplicate id' — test.skip currently (validates current allow-dup behavior). + duplicate id' — GREEN. ## Pipeline - [ ] Red test: dead participant still in turnOrderIds, turn still advances to them diff --git a/scripts/audit-state.js b/scripts/audit-state.js index 3bc52f8..2abe1a2 100644 --- a/scripts/audit-state.js +++ b/scripts/audit-state.js @@ -110,7 +110,10 @@ for (let roundN = 1; roundN <= ROUNDS; roundN++) { const np = makeParticipant({ id: `r${totalTurns}`, name:`R${totalTurns}`, type:'monster', initiative:9, maxHp:100, currentHp:100 }); try { e = { ...e, ...addParticipant(e, np).patch }; } catch (err) {} } - if (totalTurns % 12 === 0) { try { e = { ...e, ...togglePause(e).patch }; } catch (err) {} } + if (totalTurns % 12 === 0) { + try { e = { ...e, ...togglePause(e).patch }; } catch (err) {} + try { e = { ...e, ...togglePause(e).patch }; } catch (err) {} + } // advance until round wraps or cap const cap = (e.participants.length + 4) * 2; @@ -118,6 +121,7 @@ for (let roundN = 1; roundN <= ROUNDS; roundN++) { const seenThisRound = []; while (e.round === startRound && guard < cap) { if (e.currentTurnParticipantId) seenThisRound.push(e.currentTurnParticipantId); + if (e.isPaused) { check('advance-while-paused', false, 'paused at advance'); break; } let t; try { t = nextTurn(e); } catch (err) { check('nextTurn-throws', false, err.message); break; } e = { ...e, ...t.patch };