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 (claude): pin the resume await test + guard repress orphan tone

Adam Eivy8f0a18208398
diff --git a/client/src/components/meatspace/post/MorseTrainer.jsx b/client/src/components/meatspace/post/MorseTrainer.jsx
index c4f17125d9..d3164e4d7f 100644
--- a/client/src/components/meatspace/post/MorseTrainer.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.jsx
@@ -252,17 +252,24 @@ function useKeyingDecoder({ unitMs, hz, ensureCtx, enabled = false }) {
// depends on them, and re-running per keystroke would tear down the
// just-scheduled flush timers and cut off the active tone mid-press.
const pressingRef = useRef(false);
+ // Bumped once per beginPress so a startTone whose resume await settles after a
+ // newer press started can tell it's been superseded and bail.
+ const toneGenRef = useRef(0);
const [pattern, setPattern] = useState('');
const [decoded, setDecoded] = useState('');
const [pressing, setPressing] = useState(false);
const startTone = useCallback(async () => {
+ const gen = ++toneGenRef.current;
const ctx = await ensureCtx();
- // A fast tap can release (endPress → stopTone) before the first-press-only
- // resume await settles. Bail so we don't spin up an orphan oscillator that
- // has no matching stop and drones on.
- if (!pressingRef.current) return;
+ // A fast tap can release (endPress → stopTone), or a newer press can start,
+ // before the first-press-only resume await settles. Bail if the key is no
+ // longer held (`!pressingRef.current`) OR a newer press superseded this one
+ // (`gen !== toneGenRef.current`) — a bare boolean can't distinguish the two,
+ // so two overlapping starts would both create an oscillator and orphan the
+ // first, leaving it droning with no matching stop.
+ if (!pressingRef.current || gen !== toneGenRef.current) return;
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
diff --git a/client/src/components/meatspace/post/MorseTrainer.test.jsx b/client/src/components/meatspace/post/MorseTrainer.test.jsx
index 311b98c0e7..96ece184e3 100644
--- a/client/src/components/meatspace/post/MorseTrainer.test.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.test.jsx
@@ -199,9 +199,15 @@ describe('MorseTrainer iOS audio unlock', () => {
let createdWhileSuspended;
class SuspendedAudioContext {
constructor() { this.currentTime = 0; this.destination = {}; this.state = 'suspended'; }
- // Resolve on a later microtask, mirroring the real async resume — the
- // component must await this before touching createOscillator.
- resume() { return Promise.resolve().then(() => { this.state = 'running'; }); }
+ // Resolve on a macrotask (setTimeout), NOT a microtask. iOS resume latency
+ // is real time, and the component must AWAIT it before touching
+ // createOscillator. A microtask-resolving mock would let the unawaited
+ // (buggy) path pass too — playPrompt's own `await ensureCtx()` yields a
+ // microtask, so a microtask state-flip always beats createOscillator
+ // regardless of the await. The macrotask makes the missing-await path
+ // schedule the oscillator while still 'suspended', so the test fails if the
+ // fix regresses.
+ resume() { return new Promise((resolve) => { setTimeout(() => { this.state = 'running'; resolve(); }, 0); }); }
createOscillator() {
if (this.state === 'suspended') createdWhileSuspended = true;
return new MockOscillator();