Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): guard overlapping playPrompt during audio unlock

Adam Eivy2bcd3bfc610e
diff --git a/client/src/components/meatspace/post/MorseTrainer.jsx b/client/src/components/meatspace/post/MorseTrainer.jsx
index d3164e4d7f..0fa39b651b 100644
--- a/client/src/components/meatspace/post/MorseTrainer.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.jsx
@@ -803,8 +803,17 @@ function CopyDrill({ prefs, updatePrefs, ensureCtx, onExit, onSessionComplete, h
const [done, setDone] = useState(false);
const inputRef = useRef(null);
const roundStartRef = useRef(0);
+ // Re-entrancy guard. On the first play of a session `ensureCtx()` awaits the
+ // iOS audio-unlock, and `prompt`/`playing` aren't set until after it — so the
+ // Start Round / New Round button stays live during that window. A second tap
+ // would otherwise start a second overlapping playPrompt, scheduling two Morse
+ // prompts over each other with the UI tracking only whichever set state last.
+ // A ref (not `playing` state) because it must gate synchronously, before the
+ // first await, where a state update wouldn't have rendered yet.
+ const playingRef = useRef(false);
async function startRound() {
+ if (playingRef.current) return;
setResults([]);
setFeedback(null);
setDone(false);
@@ -813,6 +822,8 @@ function CopyDrill({ prefs, updatePrefs, ensureCtx, onExit, onSessionComplete, h
}
async function playPrompt(isNew) {
+ if (playingRef.current) return;
+ playingRef.current = true;
const ctx = await ensureCtx();
const text = isNew ? pickKochPrompt(prefs.kochLevel) : prompt;
if (isNew) {
@@ -823,6 +834,7 @@ function CopyDrill({ prefs, updatePrefs, ensureCtx, onExit, onSessionComplete, h
setPlaying(true);
await playMorse(ctx, text, prefs);
setPlaying(false);
+ playingRef.current = false;
setTimeout(() => inputRef.current?.focus(), 50);
}
diff --git a/client/src/components/meatspace/post/MorseTrainer.test.jsx b/client/src/components/meatspace/post/MorseTrainer.test.jsx
index 96ece184e3..6ae13a7810 100644
--- a/client/src/components/meatspace/post/MorseTrainer.test.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.test.jsx
@@ -197,6 +197,7 @@ describe('MorseTrainer iOS audio unlock', () => {
// sounds. The trainer must await resume() (like metronome.js / scorePlayback.js)
// so no oscillator is ever created while the context is still 'suspended'.
let createdWhileSuspended;
+ let oscCount;
class SuspendedAudioContext {
constructor() { this.currentTime = 0; this.destination = {}; this.state = 'suspended'; }
// Resolve on a macrotask (setTimeout), NOT a microtask. iOS resume latency
@@ -210,6 +211,7 @@ describe('MorseTrainer iOS audio unlock', () => {
resume() { return new Promise((resolve) => { setTimeout(() => { this.state = 'running'; resolve(); }, 0); }); }
createOscillator() {
if (this.state === 'suspended') createdWhileSuspended = true;
+ oscCount += 1;
return new MockOscillator();
}
createGain() { return new MockGainNode(); }
@@ -218,6 +220,7 @@ describe('MorseTrainer iOS audio unlock', () => {
beforeEach(() => {
createdWhileSuspended = false;
+ oscCount = 0;
submitTrainingEntry.mockClear();
getTrainingStats.mockClear();
window.AudioContext = SuspendedAudioContext;
@@ -232,6 +235,22 @@ describe('MorseTrainer iOS audio unlock', () => {
await screen.findByPlaceholderText('????');
expect(createdWhileSuspended).toBe(false);
});
+
+ it('ignores a second Start-Round tap during the audio-unlock window (no overlapping prompt)', async () => {
+ // While the first play awaits resume() (the iOS unlock window), prompt/playing
+ // aren't set yet, so the Start Round button is still live. A second tap in that
+ // window must be ignored — otherwise two playPrompt runs schedule two Morse
+ // prompts over each other. playMorse uses exactly one oscillator per prompt, so
+ // two overlapping prompts would create two oscillators for one round start.
+ renderMorse({ mode: 'copy', onSelectMode: vi.fn(), onExitMode: vi.fn() });
+ const startBtn = await screen.findByRole('button', { name: /Start Round/i });
+ // Two synchronous taps land inside the unlock window (resume() resolves on a
+ // later macrotask, so neither startRound has reached createOscillator yet).
+ fireEvent.click(startBtn);
+ fireEvent.click(startBtn);
+ await screen.findByPlaceholderText('????');
+ expect(oscCount).toBe(1);
+ });
});
describe('MorseTrainer Tree reference view', () => {