Skip to content

fix(audio): unbuildable audio surfaces as terminal 'unavailable' instead of pending-forever - #7877

Merged
mdmohsin7 merged 8 commits into
mainfrom
fix/audio-unavailable-marker
Jun 13, 2026
Merged

mdmohsin7 merged 8 commits into
mainfrom
fix/audio-unavailable-marker

Conversation

@mdmohsin7

Copy link
Copy Markdown
Member

Bug (found in prod ~1h after flipping #7872)

A March conversation got stuck at "preparing audio" until timeout on share/play. Trace:

  1. The merge task ran and correctly found the source chunks no longer exist in storage (audio_merge: chunks missing ... dropping) — that conversation's audio predates current chunk retention and is unrecoverable by any pipeline (it failed under the old inline merge too, with a different timeout flavor).
  2. But nothing recorded that verdict: /urls kept reporting the file as pending, while Cloud Tasks named-task tombstones silently blocked every re-enqueue (already enqueued, skipping duplicate on each poll).
  3. Result: clients poll to exhaustion — pending forever for audio that can never exist.

Fix

A terminal unavailable state:

  • Handler: on chunks_missing, writes a tiny marker blob (playback/{uid}/{conv}/{file}.unavailable, content = reason) before consuming the task.
  • /urls: artifact missing + marker present → status: "unavailable" — no enqueue, no poll hint.
  • Flutter: unavailable is excluded from hasPending, so the poll loop exits immediately and the existing error path (snackbar + audioPlaybackFailed) fires instead of 90s of spinning. Share fails fast the same way.
  • Web: shows "Audio is no longer available" immediately.

The marker lives under playback/, so the existing 30-day lifecycle rule auto-clears it — even genuinely transient chunk-listing failures get a retry within a month, with zero extra machinery.

Tests

4 new structural tests in test_audio_merge_tasks.py (marker written on chunks-missing, /urls unavailable branch without enqueue, storage helpers, app terminal handling) — 15/15 passing; tsc + dart analyze clean.

🤖 Generated with Claude Code

…ot pending-forever

The handler consumed the task but nothing recorded the verdict, so /urls
kept reporting pending while named-task tombstones silently blocked
re-enqueues — clients polled to exhaustion (live prod report: a March
conversation whose chunks no longer exist in storage).
@greptile-apps

greptile-apps Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces a terminal unavailable state for audio files whose source chunks are permanently gone, fixing a production bug where affected conversations would spin at "preparing audio" forever. The core issue was that Cloud Task named-task tombstones silently blocked re-enqueues while /urls kept reporting the file as pending, causing 90 s poll exhaustion on every load.

  • Backend: On chunks_missing, empty_audio, and exhausted retries (failed_final), the merge handler now writes a tiny {audio_file_id}.unavailable marker blob before consuming the task; /urls checks for this marker and returns status: "unavailable" with no re-enqueue.
  • Flutter: hasPending now excludes unavailable files, so the poll loop exits immediately and the existing error path fires instead of spinning.
  • Web: AudioPlayer catches unavailable both on initial load (before the polling loop) and inside the loop, showing "Audio is no longer available" right away.
  • Types/cleanup: TypeScript AudioFileUrlInfo.status and AudioFile.status both include 'unavailable'; ConversationDetailPanel now forwards status through mapAudioUrls; the rest of the panel changes are pure formatting.

Confidence Score: 5/5

Safe to merge — all three terminal-drop paths now write the unavailable marker, clients stop polling immediately, and the 30-day lifecycle rule handles eventual cleanup.

The change is narrow and well-contained: a single new marker blob and matching read on each /urls poll, with coordinated changes across the merge handler, Flutter, and web that all point at the same new state. All three previously open gaps (chunks_missing, failed_final, empty_audio) are now plugged, the pre-loop fast-path in AudioPlayer correctly short-circuits before entering the 90 s polling loop, and the type definitions on both web and Flutter match the new backend contract. No existing happy-path logic is modified.

No files require special attention.

Important Files Changed

Filename Overview
backend/routers/sync.py Adds unavailable branch to _get_audio_urls_via_artifacts (no re-enqueue) and calls mark_playback_unavailable in all three terminal-drop paths: chunks_missing, failed_final, and empty_audio. Previously flagged failed_final gap is now plugged.
backend/utils/other/storage.py Adds _playback_unavailable_blob, mark_playback_unavailable, and is_playback_unavailable helpers. Marker blob lives under playback/ so the existing 30-day lifecycle rule auto-clears it.
web/app/src/components/conversations/AudioPlayer.tsx Checks status === 'unavailable' both before the polling loop (initial load fast-path) and inside the loop; shows error immediately in both cases. Previously flagged extra-round-trip concern is resolved by the pre-loop guard.
app/lib/backend/http/api/audio.dart Updates comment to `'cached'
web/app/src/types/conversation.ts Adds 'unavailable' to AudioFileUrlInfo.status union and optional status to AudioFile; rest of changes are formatting-only.
web/app/src/components/conversations/ConversationDetailPanel.tsx Forwards status through mapAudioUrls so AudioPlayer receives it on initial load; remainder of changes are trailing-comma / arrow-function formatting.
backend/tests/unit/test_audio_merge_tasks.py Adds TestUnavailableContract with four structural tests verifying the marker is written on chunks_missing, /urls doesn't enqueue for unavailable, helpers exist in storage, and Flutter treats unavailable as terminal.

Sequence Diagram

sequenceDiagram
    participant Client as Client (Flutter / Web)
    participant URLs as /urls endpoint
    participant GCS as GCS Storage
    participant Task as Merge Cloud Task

    Note over Client,Task: Happy path (unchanged)
    Client->>URLs: GET /urls
    URLs->>GCS: get_playback_artifact_signed_url
    GCS-->>URLs: signed URL
    URLs-->>Client: status cached

    Note over Client,Task: Unavailable path (NEW)
    Task->>GCS: _build_playback_artifact FileNotFoundError
    Task->>GCS: mark_playback_unavailable write .unavailable blob
    Task-->>Task: return 200 dropped chunks_missing

    Client->>URLs: GET /urls next poll or initial load
    URLs->>GCS: get_playback_artifact_signed_url null
    URLs->>GCS: is_playback_unavailable true
    URLs-->>Client: status unavailable
    Client-->>Client: Show error immediately stop polling

    Note over Client,Task: Pending path (unchanged)
    Client->>URLs: GET /urls
    URLs->>GCS: get_playback_artifact_signed_url null
    URLs->>GCS: is_playback_unavailable false
    URLs-->>Client: status pending poll_after_ms N
    URLs->>Task: enqueue_conversation_audio_merge
    Client->>Client: sleep pollAfterMs retry
Loading

Reviews (2): Last reviewed commit: "fix(web): thread audio status so unavail..." | Re-trigger Greptile

…comes

Greptile P1: the chunks_missing branch wrote the unavailable marker, but
the failed_final (max-retry exhaustion) and empty_audio branches consumed
the task without one — the named-task tombstone then blocks re-enqueue,
leaving the file pending forever. Same trap, different failure mode.
Greptile nit: AudioPlayer guarded firstFile.status but mapAudioUrls
dropped the field, making the guard dead code (one wasted poll round-trip
before the loop caught it). Carry status through the type and mapper.
@mdmohsin7

Copy link
Copy Markdown
Member Author

@greptile-apps re-review

@mdmohsin7
mdmohsin7 merged commit f05687a into main Jun 13, 2026
3 checks passed
@mdmohsin7
mdmohsin7 deleted the fix/audio-unavailable-marker branch June 13, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant