Skip to content

fix(backend): PTT transcribe-stream fails over at the audio send path - #12492

Merged
Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/ptt-stt-send-path-failover
Aug 31, 2026
Merged

Git-on-my-level merged 2 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/ptt-stt-send-path-failover

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Problem

Live 24h realtime_voice on the PTT surface (/v2/voice-message/transcribe-stream): 4 success, 13 failure — all provider_error, 5 cancelled. Cloud Logging pairs 14 event=ptt_transcription_stream outcome=provider_terminal_failure with 14 Modulate streaming error, clustered 13:00–14:56 UTC. Not a metrics lie, not a Beta-vs-prod app gap: the same Cloud Run backend serves both, and the serving SHA (e1eca38) already carries #12469.

#12469 fixed listen mid-session STT failover at the send path. PTT still did this on the first rejected send:

async def send_stt_audio_or_close(audio: bytes) -> bool:
    ...
    if accepted:
        return True
    await close_stt_failure()  # 1011 + journey fail('provider_error')
    return False

Velma/Modulate accepts the WebSocket upgrade and only then dies ~300ms later on the first audio send — the exact shape #12469 closed for listen. PTT's connect-time connect_stt_socket_with_fallback cannot help there, so a recoverable death terminated the session with 1011 while the chain still held a healthy replacement. The 13 failures never received a nonempty transcript; ClientJourneyAttempt.finish being one-shot is why the 4 working sessions stayed successes.

Not Beta vs prod. Not dashboard math. Out of scope by design: Grafana/client_kind (SCA-388), success-rate PromQL, provider chain order, desktop client changes.

Fix

Port the listen send-path failover onto PTT — reuse, not a fork:

  • send_stt_audio_or_close now routes through utils/stt/live_failure.py::send_live_stt_audio(..., attempt_failover=...).
  • The callback mirrors listen's _failover_stt_socket: dead-provider exclusion set fed to get_stt_service_for_language(..., surface=PTT, exclude=...), bounded by MAX_STT_FAILOVERS (moved to utils/stt/live_failure.py so both surfaces share one bound), replacement proven serving (fallback_socket_is_serving) before adoption, rejected rebuilds released.
  • A successful failover reports the chunk unsent and the bounded retry sends it against the replacement socket — no audio dropped (listen's flush contract).
  • Terminal handling now shares terminate_live_stt_session with listen via a small _PTTStreamSession adapter: journey fail('provider_error') only when the chain is exhausted (or no socket was ever initialized), one terminal service_status event, then close 1011 — the same wire contract as /v4/listen. Both desktop PTT lane parsers ignore non-array frames (Array.isArray guard in desktop/windows/src/main/ipc/omiListen.ts), and the e2e suites collect segment arrays only.
  • Kept: succeed() on first nonempty transcript; the event=ptt_transcription_stream outcome=provider_terminal_failure log line on every terminal; codec/paywall/budget/idle-timeout cancel behavior.

Verification

  • backend/tests/unit/test_desktop_transcribe.py — 71 passed. New behavioral tests:
    • test_ws_send_path_death_fails_over_and_retries_chunk (GREEN: swaps parakeet→modulate, retries the same chunk verbatim, no provider_error, later transcript succeed()s; RED against the pre-fix endpoint: same test 1011s with no failover — verified by reverting routers/chat.py).
    • test_ws_send_path_death_with_exhausted_chain_closes_1011 (replacement that never serves is released; provider_error + 1011; no usage charge).
    • Updated terminal-wire tests assert the status event before close 1011.
  • test_live_stt_failure.py + test_stt_session_failover.py (listen failover) — 27 passed. test_excluding_every_provider_selects_nothing fails when these files run combined with test_desktop_transcribe.py, identically on pristine origin/main — pre-existing selection-order interaction, untouched by this PR.
  • Live self-test (commands + observed behavior): real uvicorn serving the real endpoint (auth bypassed via app.dependency_overrides only), local fake Parakeet /v3/stream WebSocket, dummy Modulate key. Healthy phase: connect-time fallback from=modulate to=parakeet outcome=recovered, transcript segment received over the live socket. Killed the provider mid-session: send-path death latched, failover attempted, chain exhausted, terminal service_status stt_failed event received, close 1011, event=ptt_transcription_stream outcome=provider_terminal_failure logged.
  • scripts/backend-python-format --check, backend/scripts/scan_async_blockers.py, backend/scripts/check_module_stub_pollution.py: clean.

Failure-Class: FC-recoverable-provider-failure-terminal

Line-Count-Exception: backend/routers/chat.py | 1935 -> 2097 | PTT send-path failover is session closure state over ~15 endpoint locals (socket, provider selection, journey, buffers), mirroring listen whose equivalent lives inside its receiver class; splitting the endpoint out of chat.py is a separate mechanical PR. +3 lines are a lazy live_failure import to break the chat-test circular import.

Review in cubic

Live 24h realtime_voice on the PTT surface: 4 success, 13 provider_error
terminals, all paired with "Modulate streaming error" frames. BasedHardware#12469 gave
listen a send-path failover; PTT still terminated the session on the first
rejected send, so a Velma/Modulate socket that accepts the upgrade and dies
~300ms later on the first audio chunk ended the session with 1011 even while
the chain held a healthy replacement (14 ptt provider_terminal_failure in 24h,
zero mid-session failovers on the PTT path).

Port the listen pattern instead of forking it: send_stt_audio_or_close now
routes through utils/stt/live_failure.send_live_stt_audio with an
attempt_failover callback that rebuilds the socket on the next provider in the
chain (selector exclude set, MAX_STT_FAILOVERS bound, liveness-proven before
adoption). A successful failover reports the chunk unsent and the bounded retry
sends it against the replacement socket, so no audio is dropped. The terminal
path now shares terminate_live_stt_session with listen: journey
fail('provider_error') via the shared session adapter, a terminal
service_status event, then close 1011 — previously the PTT close carried no
status event and a different reason string, and both desktop PTT clients
ignore non-array frames (Array.isArray guard in omiListen.ts). The
event=ptt_transcription_stream outcome=provider_terminal_failure log line is
kept on every terminal. Codec/paywall/budget/idle-timeout paths are unchanged;
MAX_STT_FAILOVERS moved to utils/stt/live_failure.py so both surfaces share
one bound.

Verification:
- backend/tests/unit/test_desktop_transcribe.py: 71 passed (RED confirmed by
  reverting routers/chat.py: the new failover test fails with a 1011 close and
  no transcript; GREEN with the fix).
- test_live_stt_failure.py + test_stt_session_failover.py: 27 passed
  standalone (test_excluding_every_provider_selects_nothing fails in a
  combined run identically on pristine origin/main — pre-existing, untouched).
- Live self-test: real uvicorn serving the endpoint, local fake Parakeet
  /v3/stream, dummy Modulate key — healthy segment received; killing the
  provider mid-session produced the connect-time fallback telemetry, the
  failover attempt, the terminal service_status event, close 1011, and the
  provider_terminal_failure log line.

Failure-Class: FC-recoverable-provider-failure-terminal
@Git-on-my-level

Copy link
Copy Markdown
Member Author

CI Backend unit suite is red on 92f19b23 (https://github.com/BasedHardware/omi/actions/runs/33418608093).

ImportError: cannot import name 'record_live_stt_failure' from 'utils.observability.transcription' (unknown location)

This is a circular import, not a missing symbol (record_live_stt_failure exists at backend/utils/observability/transcription.py:216 on this head). Chat tests import routers.chat; the new module-level from utils.stt.live_failure import send_live_stt_audio, ... pulls live_failure → transcription while that module is still loading.

Fix on this branch (keep product behavior):

  1. Remove the top-level utils.stt.live_failure import from backend/routers/chat.py.
  2. Import send_live_stt_audio / MAX_STT_FAILOVERS / whatever PTT needs inside transcribe_voice_message_stream (or the send/failover helpers), same as other lazy STT imports.
  3. Re-run CI’s chat unit files: tests/unit/test_chat_quota_counting_router.py, test_chat_generate_reply_stateless.py, test_chat_stream_error_fallback.py plus the new PTT failover tests.

Do not revert the send-path failover. Push to fix/ptt-stt-send-path-failover.

…test cycle

Module-level import of utils.stt.live_failure from routers.chat pulled
observability.transcription while chat unit tests imported the router,
raising ImportError: cannot import name record_live_stt_failure.

Failure-Class: FC-recoverable-provider-failure-terminal
@Git-on-my-level
Git-on-my-level merged commit 62713e0 into BasedHardware:main Aug 31, 2026
29 checks passed
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