[GH-ISSUE #2755] [Issue]: TTS audio cache — unbounded IndexedDB growth, write amplification, and fragile open path #1393

Closed
opened 2026-10-21 05:32:54 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2755

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The persistent TTS audio cache (tts-audio-cache.ts) has several storage-layer defects: it never evicts, it rewrites whole blobs on every read, it self-disables permanently on a transient open error, and its cross-message alias key is a collision-prone 32-bit hash.

Sub-bug 1: Unbounded growth (#5) [primary]

  • How it breaks: putPersistentBlob writes a row on every miss (and one per alias, 173-176) but nothing ever deletes persistent rows. The lastUsedAt index (60-62) is created and updated but never queried for pruning; only the 150-entry in-memory Map is bounded. The store is write-only-grows until quota exhaustion (then silently falls back to memory-only).
  • Repro: Heavy long-term TTS use; the IndexedDB origin store accumulates every generated line + aliases forever.

Sub-bug 2: Write amplification (#44)

  • How it breaks: Every cache hit that misses memory but hits IndexedDB does a readwrite put({ ...record, lastUsedAt }) (84-92) that re-serializes the FULL audio Blob just to bump a timestamp — and the timestamp is never consumed by any eviction.
  • Repro: Cross-reload/post-eviction replays of a line rewrite the whole M-byte clip per cold read.

Sub-bug 3: Cache self-disables on transient open failure (#43)

  • How it breaks: dbPromise is memoized and resolves to null on onerror/onblocked (66-67) but is never reset, so openDb() returns the same resolved-null promise forever. The first line to hit a failing open (e.g. private-mode quota hiccup) poisons the persistent cache for the whole session.
  • Repro: Trigger a transient IndexedDB open error on the first cached line; persistence stays dead until reload.

Sub-bug 4: 32-bit alias collision (#45)

  • How it breaks: The cross-message alias chat-voice-line-text-v1:<hash> uses a 32-bit DJB-xor hash of (config+voice+speaker+tone+text). On a collision, an alias hit is treated as authoritative and copied to the primary key, so one line plays another's audio. Records store no plaintext to validate against. (Low probability — birthday-bound — but never-pruned population grows; the sibling game path prefixes a length discriminator, this one doesn't.)

Suggested Fix

  • Add an LRU/byte-budget prune (throttled, using the existing lastUsedAt index) and revoke/delete evicted rows.
  • Store recency metadata separately from the blob, or skip the lastUsedAt rewrite entirely until an eviction consumer exists.
  • On onerror/onblocked, resolve(null) AND reset dbPromise = null so a later call retries; optionally retry once on onblocked.
  • Widen the alias key (53/64/128-bit, or include a content-length/prefix discriminator).

Verification checklist (all manual)

  • Verify the persistent store stays bounded under sustained use (prune runs)
  • Verify a cache read no longer rewrites the full blob
  • Verify a transient IndexedDB open error does not permanently disable persistence for the session
  • Verify the widened alias key is in place (collision risk reduced)
  • Run pnpm check

Affected files

  • packages/client/src/lib/tts-audio-cache.ts
  • packages/client/src/lib/tts-dialogue.ts

Audit provenance

Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged staging tree. Severity: MEDIUM.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2755 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The persistent TTS audio cache (`tts-audio-cache.ts`) has several storage-layer defects: it never evicts, it rewrites whole blobs on every read, it self-disables permanently on a transient open error, and its cross-message alias key is a collision-prone 32-bit hash. ## Sub-bug 1: Unbounded growth (#5) [primary] - **How it breaks:** `putPersistentBlob` writes a row on every miss (and one per alias, 173-176) but nothing ever deletes persistent rows. The `lastUsedAt` index (60-62) is created and updated but never queried for pruning; only the 150-entry in-memory Map is bounded. The store is write-only-grows until quota exhaustion (then silently falls back to memory-only). - **Repro:** Heavy long-term TTS use; the IndexedDB origin store accumulates every generated line + aliases forever. ## Sub-bug 2: Write amplification (#44) - **How it breaks:** Every cache hit that misses memory but hits IndexedDB does a readwrite `put({ ...record, lastUsedAt })` (84-92) that re-serializes the FULL audio Blob just to bump a timestamp — and the timestamp is never consumed by any eviction. - **Repro:** Cross-reload/post-eviction replays of a line rewrite the whole M-byte clip per cold read. ## Sub-bug 3: Cache self-disables on transient open failure (#43) - **How it breaks:** `dbPromise` is memoized and resolves to null on `onerror`/`onblocked` (66-67) but is never reset, so `openDb()` returns the same resolved-null promise forever. The first line to hit a failing open (e.g. private-mode quota hiccup) poisons the persistent cache for the whole session. - **Repro:** Trigger a transient IndexedDB open error on the first cached line; persistence stays dead until reload. ## Sub-bug 4: 32-bit alias collision (#45) - **How it breaks:** The cross-message alias `chat-voice-line-text-v1:<hash>` uses a 32-bit DJB-xor hash of (config+voice+speaker+tone+text). On a collision, an alias hit is treated as authoritative and copied to the primary key, so one line plays another's audio. Records store no plaintext to validate against. (Low probability — birthday-bound — but never-pruned population grows; the sibling game path prefixes a length discriminator, this one doesn't.) ## Suggested Fix - Add an LRU/byte-budget prune (throttled, using the existing `lastUsedAt` index) and revoke/delete evicted rows. - Store recency metadata separately from the blob, or skip the `lastUsedAt` rewrite entirely until an eviction consumer exists. - On `onerror`/`onblocked`, resolve(null) AND reset `dbPromise = null` so a later call retries; optionally retry once on `onblocked`. - Widen the alias key (53/64/128-bit, or include a content-length/prefix discriminator). ## Verification checklist (all manual) - [ ] Verify the persistent store stays bounded under sustained use (prune runs) - [ ] Verify a cache read no longer rewrites the full blob - [ ] Verify a transient IndexedDB open error does not permanently disable persistence for the session - [ ] Verify the widened alias key is in place (collision risk reduced) - [ ] Run `pnpm check` --- ### Affected files - `packages/client/src/lib/tts-audio-cache.ts` - `packages/client/src/lib/tts-dialogue.ts` ### Audit provenance Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged `staging` tree. Severity: **MEDIUM**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Addressed on PR #2747. The TTS audio cache now uses DB v2 with a metadata store, metadata-only recency touches on cold reads, retryable IndexedDB open failures, throttled persistent pruning by entry/byte budget, and no blob-copy writes on alias hits or in-flight alias reuse. The TTS cache alias hash now includes a length discriminator and a widened two-lane hash instead of the old 32-bit key. Validated with git diff --check for the touched files; full pnpm validation is deferred until the end of the issue sweep per maintainer request.

<!-- gh-comment-id:4763301604 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on PR #2747. The TTS audio cache now uses DB v2 with a metadata store, metadata-only recency touches on cold reads, retryable IndexedDB open failures, throttled persistent pruning by entry/byte budget, and no blob-copy writes on alias hits or in-flight alias reuse. The TTS cache alias hash now includes a length discriminator and a widened two-lane hash instead of the old 32-bit key. Validated with git diff --check for the touched files; full pnpm validation is deferred until the end of the issue sweep per maintainer request.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1393
No description provided.