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: bound custom alert-sound memory and stop re-decoding bad files (#289)

Erik Zwepab219d267eaf
diff --git a/src/renderer/lib/customSfx.test.ts b/src/renderer/lib/customSfx.test.ts
index 50f51969a..a5acbe604 100644
--- a/src/renderer/lib/customSfx.test.ts
+++ b/src/renderer/lib/customSfx.test.ts
@@ -55,6 +55,36 @@ describe('createCustomSfxPlayer', () => {
expect(deps.read).toHaveBeenCalledTimes(2)
})
+ it('caches only the trimmed copy of a decoded buffer — the full decode is not what is kept', async () => {
+ const trim = vi.fn((b: Buf): Buf => ({ id: `${b.id}-first10s` }))
+ const { deps, p } = player({ trim })
+ await p.play('done', 1, 0.5)
+ await p.play('done', 1, 0.5)
+ expect(trim).toHaveBeenCalledTimes(1)
+ expect(trim).toHaveBeenCalledWith({ id: 'b64-done' })
+ expect(deps.play).toHaveBeenNthCalledWith(1, { id: 'b64-done-first10s' }, 0.5)
+ expect(deps.play).toHaveBeenNthCalledWith(2, { id: 'b64-done-first10s' }, 0.5)
+ })
+
+ it('negative-caches a DECODE failure per stamp: later alerts neither re-read nor re-decode', async () => {
+ const { deps, p } = player({ decode: vi.fn(async () => { throw new Error('EncodingError') }) })
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ expect(await p.preload('done', 1)).toBe(false)
+ expect(deps.read).toHaveBeenCalledTimes(1)
+ expect(deps.decode).toHaveBeenCalledTimes(1)
+ // A new pick (new stamp) is tried afresh.
+ await p.play('done', 2, 0.5)
+ expect(deps.read).toHaveBeenCalledTimes(2)
+ })
+
+ it('a trim that throws counts as a decode failure (negative-cached, never thrown)', async () => {
+ const { deps, p } = player({ trim: vi.fn(() => { throw new Error('NotSupportedError') }) })
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ expect(await p.play('done', 1, 0.5)).toBe(false)
+ expect(deps.read).toHaveBeenCalledTimes(1)
+ })
+
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 000874bad..e7d6c7558 100644
--- a/src/renderer/lib/customSfx.ts
+++ b/src/renderer/lib/customSfx.ts
@@ -9,8 +9,13 @@
// • A custom sound is loaded by KIND from the core (never by path), decoded once per `stamp`,
// and played at the user's volume. Anything that goes wrong — missing file, a read the bridge
// refuses, a decode error, a playback error — answers `false`, and the caller plays the chime.
-// • A failure is NOT cached: the next alert retries, so a transiently dropped Server Edition
-// socket does not pin the chime for the rest of the run.
+// • Only the playable head of a decoded sound is cached (`trim`): a 5 MB compressed file can
+// decode to hundreds of MB of float32 PCM, and playback is cut at 10 s anyway.
+// • A READ failure (no file, refused/dropped read) is NOT cached: the next alert retries, so a
+// transiently dropped Server Edition socket does not pin the chime for the rest of the run.
+// 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.
// • Nothing here throws into the caller, synchronously or as an unhandled rejection.
import { customAlertSoundFor, type AlertSoundKind } from '@shared/alert-sound'
@@ -22,6 +27,9 @@ export interface CustomSfxDeps<B> {
decode(dataBase64: string): Promise<B>
/** Start playback at `gain` (0..1, the user's volume). May throw. */
play(buf: B, gain: number): void
+ /** Reduce a decoded buffer to what is kept (the playable head). Absent = keep as decoded.
+ * A throw counts as a decode failure. */
+ trim?(buf: B): B
}
export interface CustomSfxPlayer {
@@ -37,20 +45,30 @@ export function createCustomSfxPlayer<B>(deps: CustomSfxDeps<B>): CustomSfxPlaye
const load = (kind: AlertSoundKind, stamp: number): Promise<B | null> => {
const hit = cache.get(kind)
if (hit && hit.stamp === stamp) return hit.buf
+ let transient = false
const buf = (async (): Promise<B | null> => {
+ let b64: string | null
try {
- const b64 = await deps.read(kind)
- if (!b64) return null
- return await deps.decode(b64)
+ b64 = await deps.read(kind)
} catch {
+ b64 = null
+ }
+ if (!b64) {
+ transient = true
return null
}
+ 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
+ }
})()
const entry = { stamp, buf }
cache.set(kind, entry)
- // Only a success stays cached; a failure is forgotten so the next alert asks again.
- void buf.then((b) => {
- if (b === null && cache.get(kind) === entry) cache.delete(kind)
+ // A read failure is forgotten so the next alert asks again; success and decode failure stay.
+ void buf.then(() => {
+ if (transient && cache.get(kind) === entry) cache.delete(kind)
})
return buf
}
diff --git a/src/renderer/lib/sfx.ts b/src/renderer/lib/sfx.ts
index 2d41e5458..e58422d9e 100644
--- a/src/renderer/lib/sfx.ts
+++ b/src/renderer/lib/sfx.ts
@@ -178,6 +178,20 @@ function custom(): CustomSfxPlayer {
if (!c) throw new Error('no audio context')
return c.decodeAudioData(base64ToArrayBuffer(b64))
},
+ // 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
+ // buffer is dropped once this copy exists; the decode itself still peaks at full size.
+ trim: (buf) => {
+ 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')
+ 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)
+ }
+ return head
+ },
play: (buf, gain) => {
const c = audio()
if (!c) throw new Error('no audio context')