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): release the audio session when the resume rejects

[._.]/ Adam Eivy8958e18c35ed
diff --git a/client/src/lib/lookaheadTransport.js b/client/src/lib/lookaheadTransport.js
index 2634acc949..e310fa42ea 100644
--- a/client/src/lib/lookaheadTransport.js
+++ b/client/src/lib/lookaheadTransport.js
@@ -148,8 +148,15 @@ export const createLookaheadTransport = ({
// and re-declared every play because another view on this document may have
// released it since (see the audio-session note in audioContext.js).
if (audioSession) declareAudioSession(audioSession);
- // Covers iOS Safari's `'interrupted'` state, not just `'suspended'`.
- await resumeAudioContext(c);
+ // Covers iOS Safari's `'interrupted'` state, not just `'suspended'`. A
+ // rejection here (autoplay policy, or a session iOS won't hand back) leaves
+ // play() before any teardown can run, so the session goes back on the way
+ // out — unless a newer play() already took it over. The rejection still
+ // propagates: the caller resets its Play button on it.
+ await resumeAudioContext(c).catch((err) => {
+ if (audioSession && token === playToken) releaseAudioSession();
+ throw err;
+ });
// Bail WITHOUT releasing the session: whatever bumped the token (stop, pause,
// or a newer play) owns it now. A stop/pause has already handed it back, and
// a newer play has re-declared it — releasing here would silence that one.
diff --git a/client/src/lib/scorePlayback.race.test.js b/client/src/lib/scorePlayback.race.test.js
index d8fd0355ba..b99cc19c99 100644
--- a/client/src/lib/scorePlayback.race.test.js
+++ b/client/src/lib/scorePlayback.race.test.js
@@ -79,6 +79,18 @@ describe('audio session across the resume window', () => {
expect(globalThis.navigator.audioSession.type).toBe('auto');
});
+ it('releases when the resume itself rejects', async () => {
+ // Autoplay policy (or a session iOS won't hand back) rejects the resume, and
+ // that exits play() before any teardown — so without an explicit release the
+ // failed play leaves the document pinned output-only for good. The rejection
+ // must still reach the caller, which resets its Play button on it.
+ audio.resumeRejection = new Error('not allowed');
+ const t = transport();
+ await expect(t.play()).rejects.toThrow('not allowed');
+ expect(t.isPlaying()).toBe(false);
+ expect(globalThis.navigator.audioSession.type).toBe('auto');
+ });
+
it('does not release out from under a newer play that took over mid-await', async () => {
// The superseded play() must bail WITHOUT releasing: the newer one already
// re-declared the session and is about to sound against it.
diff --git a/client/src/test/fakeAudioContext.js b/client/src/test/fakeAudioContext.js
index e5eb27ffa4..6f17f0204b 100644
--- a/client/src/test/fakeAudioContext.js
+++ b/client/src/test/fakeAudioContext.js
@@ -31,6 +31,12 @@ export const createFakeAudio = ({ state: initialState = 'running' } = {}) => {
// so a test can assert the transport actually brought the clock up.
state: initialState,
resumeCalls: 0,
+ // Set to an Error to make resume() reject instead of parking — the autoplay
+ // policy refusing a gesture, or iOS declining to hand the session back. Set
+ // on the shared `audio` (not via a re-stubbed constructor) because
+ // lib/audioContext.js memoizes the context on first use, so a later stub
+ // never reaches the instance the code under test is already holding.
+ resumeRejection: null,
oscillators: [],
gains: [],
// Bandpassed noise voices (createBufferSource + createBiquadFilter) — the
@@ -46,6 +52,7 @@ export const createFakeAudio = ({ state: initialState = 'running' } = {}) => {
this.now = 0;
this.state = initialState;
this.resumeCalls = 0;
+ this.resumeRejection = null;
this.oscillators.length = 0;
this.gains.length = 0;
this.filters.length = 0;
@@ -86,6 +93,7 @@ export const createFakeAudio = ({ state: initialState = 'running' } = {}) => {
get state() { return audio.state; },
resume: () => {
audio.resumeCalls += 1;
+ if (audio.resumeRejection) return Promise.reject(audio.resumeRejection);
if (audio.state === 'running') return Promise.resolve();
return new Promise((r) => { resumeResolvers.push(r); });
},