Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix: retry custom alert sounds when the audio context is missing (#289)

Erik Zwepe0c2d1913b60
diff --git a/src/renderer/lib/customSfx.test.ts b/src/renderer/lib/customSfx.test.ts
index a5acbe604..810b81b31 100644
--- a/src/renderer/lib/customSfx.test.ts
+++ b/src/renderer/lib/customSfx.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest'
-import { createCustomSfxPlayer, playAlert, type CustomSfxPlayer } from './customSfx'
+import { createCustomSfxPlayer, playAlert, TransientSfxError, type CustomSfxPlayer } from './customSfx'
type Buf = { id: string }
@@ -85,6 +85,34 @@ describe('createCustomSfxPlayer', () => {
expect(deps.read).toHaveBeenCalledTimes(1)
})
+ it('a missing/closed audio context is TRANSIENT: chime now, the next alert retries', async () => {
+ let ctx = false
+ const { deps, p } = player({
+ decode: vi.fn(async (b64: string): Promise<Buf> => {
+ if (!ctx) throw new TransientSfxError('no audio context')
+ return { id: b64 }
+ })
+ })
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ ctx = true
+ expect(await p.play('done', 1, 0.5)).toBe(true)
+ expect(deps.read).toHaveBeenCalledTimes(2)
+ })
+
+ it('a transient error from trim is not negative-cached either', async () => {
+ let fail = true
+ const { deps, p } = player({
+ trim: vi.fn((b: Buf) => {
+ if (fail) throw new TransientSfxError('no audio context')
+ return b
+ })
+ })
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ fail = false
+ expect(await p.play('done', 1, 0.5)).toBe(true)
+ expect(deps.read).toHaveBeenCalledTimes(2)
+ })
+
it('preload decodes without playing', async () => {
const { deps, p } = player()
expect(await p.preload('needsYou', 3)).toBe(true)
diff --git a/src/renderer/lib/customSfx.ts b/src/renderer/lib/customSfx.ts
index e7d6c7558..0e44534dd 100644
--- a/src/renderer/lib/customSfx.ts
+++ b/src/renderer/lib/customSfx.ts
@@ -16,10 +16,17 @@
// A DECODE failure IS cached for that stamp: the bytes will not decode any better next time,
// and re-reading them per alert is a multi-MB transfer (a WS payload on the Server Edition)
// plus a full decode, every time. A new pick has a new stamp and is tried afresh.
+// A missing or closed AudioContext is NOT a decode failure — the deps throw `TransientSfxError`
+// for it, and it is retried like a read failure (else the first alert before a context exists
+// would pin the chime until the user picks the file again).
// • Nothing here throws into the caller, synchronously or as an unhandled rejection.
import { customAlertSoundFor, type AlertSoundKind } from '@shared/alert-sound'
+/** Thrown by `decode`/`trim` for a condition that says nothing about the FILE (no or a closed
+ * audio context). Retried on the next alert, never negative-cached. */
+export class TransientSfxError extends Error {}
+
export interface CustomSfxDeps<B> {
/** The stored sound's bytes (base64), or null when there is none. May reject. */
read(kind: AlertSoundKind): Promise<string | null>
@@ -60,8 +67,9 @@ export function createCustomSfxPlayer<B>(deps: CustomSfxDeps<B>): CustomSfxPlaye
try {
const decoded = await deps.decode(b64)
return deps.trim ? deps.trim(decoded) : decoded
- } catch {
- return null // a decode failure: stays cached for this stamp
+ } catch (e) {
+ if (e instanceof TransientSfxError) transient = true
+ return null // otherwise a real decode failure: stays cached for this stamp
}
})()
const entry = { stamp, buf }
diff --git a/src/renderer/lib/sfx.ts b/src/renderer/lib/sfx.ts
index e58422d9e..b2e4eef8e 100644
--- a/src/renderer/lib/sfx.ts
+++ b/src/renderer/lib/sfx.ts
@@ -15,7 +15,7 @@
// either surface). The fallback rules live in lib/customSfx.ts: any failure plays the chime below.
import type { AlertSoundKind } from '@shared/alert-sound'
-import { createCustomSfxPlayer, playAlert, type CustomSfxPlayer } from './customSfx'
+import { createCustomSfxPlayer, playAlert, TransientSfxError, type CustomSfxPlayer } from './customSfx'
export type SfxKind = AlertSoundKind
@@ -71,6 +71,8 @@ let ctx: AudioContext | null = null
let noiseBuf: AudioBuffer | null = null
function audio(): AudioContext | null {
+ // A closed context can never play again; drop it and build a fresh one.
+ if (ctx && ctx.state === 'closed') ctx = null
if (ctx) return ctx
const Ctor: typeof AudioContext | undefined =
typeof window === 'undefined'
@@ -175,8 +177,14 @@ function custom(): CustomSfxPlayer {
},
decode: async (b64) => {
const c = audio()
- if (!c) throw new Error('no audio context')
- return c.decodeAudioData(base64ToArrayBuffer(b64))
+ if (!c) throw new TransientSfxError('no audio context')
+ try {
+ return await c.decodeAudioData(base64ToArrayBuffer(b64))
+ } catch (e) {
+ // A context closed mid-decode rejects too — that is not the file's fault.
+ if ((c.state as string) === 'closed') throw new TransientSfxError('audio context closed')
+ throw e
+ }
},
// Keep only what can be heard: the decoded clip is float32 PCM (a 5 MB MP3 can be ~200 MB and a
// heavily compressed file far more), and playback stops at CUSTOM_MAX_SECONDS anyway. The full
@@ -185,7 +193,7 @@ function custom(): CustomSfxPlayer {
const frames = Math.floor(buf.sampleRate * CUSTOM_MAX_SECONDS)
if (buf.length <= frames) return buf
const c = audio()
- if (!c) throw new Error('no audio context')
+ if (!c) throw new TransientSfxError('no audio context')
const head = c.createBuffer(buf.numberOfChannels, frames, buf.sampleRate)
for (let ch = 0; ch < buf.numberOfChannels; ch++) {
head.copyToChannel(buf.getChannelData(ch).subarray(0, frames), ch)