Skip to content

fix(backend): unreadable WAV duration must not skip STT budget check (#12718) - #12723

Merged
kodjima33 merged 1 commit into
mainfrom
watchdog/issue-12718-unreadable-wav-skips-stt-budget
Sep 4, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
watchdog/issue-12718-unreadable-wav-skips-stt-budget

Conversation

@kodjima33

@kodjima33 kodjima33 commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

On the WAV/multipart voice STT routes, when read_wav_duration_ms returns None (unreadable/corrupt audio), the daily STT duration budget check was skipped entirely — but paid transcription still ran on that file. Free/under-metered STT spend, and a way to dodge the daily budget by uploading files that fail duration parsing.

  • /v2/voice-messages (SSE, backend/routers/chat.py:879): only called try_consume_budget if duration_ms is not None.
  • /v2/voice-message/transcribe (multipart, backend/routers/chat.py:1183): summed only the readable files' durations, and skipped try_consume_budget entirely if the sum was 0 (e.g. every file in the batch was unreadable).

The PCM REST path (compute_pcm_duration_ms → try_consume_budget, always run) never had this gap.

Fix

When read_wav_duration_ms returns None, charge the worst case (MAX_SESSION_DURATION_S = 120s) against the budget instead of skipping the check, on both endpoints. This closes the free-transcription/budget-bypass gap while still letting legitimate short unreadable-duration files through unless the user is already near their daily limit.

Tested

  • Added test_multipart_unreadable_wav_does_not_skip_budget_check and test_voice_messages_unreadable_wav_does_not_skip_budget_check: assert the budget call fires with the MAX_SESSION_DURATION_S * 1000 fallback when duration is unreadable, and that it can trip the 429 gate.
  • Updated test_multipart_multi_file_budget_sums_durations, which previously asserted the old (buggy) skip-on-None behavior, to expect the worst-case charge for the unreadable file in the batch.
  • ENCRYPTION_SECRET=test .venv/bin/python -m pytest backend/tests/unit/test_desktop_transcribe.py backend/tests/unit/test_voice_duration_limiter.py -q → 107 passed.
  • Also ran the other 6 suites sharing _chat_router_test_harness.py (which needed the same MAX_SESSION_DURATION_S stub addition): 39 passed, no regressions.

Fixes #12718.

Line-Count-Exception: backend/routers/chat.py | 2130 -> 2133 | net +3 lines from the budget-check fallback (comment + one fewer nested if), not a size violation worth splitting the file over

Failure-Class: none

🤖 automated by hourly watchdog — tested and merged

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Confidence score: 3/5

  • In backend/routers/chat.py, transcribe_voice_message performs an unconditional synchronous Redis budget call when a multipart batch has no readable durations, which can block the event loop during Redis latency and degrade concurrent request handling — offload the call to an async-compatible path or thread.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/routers/chat.py">

<violation number="1" location="backend/routers/chat.py:1190">
P2: When a multipart batch has no readable durations, this new unconditional call performs synchronous Redis I/O inside `transcribe_voice_message`, blocking the event loop during Redis latency. Offload the budget call with `run_blocking` on the database executor.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread backend/routers/chat.py
if not allowed:
raise HTTPException(status_code=429, detail='Daily transcription budget exhausted')
total_duration_ms += duration_ms if duration_ms is not None else MAX_SESSION_DURATION_S * 1000
allowed, used_ms, remaining_ms = try_consume_budget(uid, total_duration_ms)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When a multipart batch has no readable durations, this new unconditional call performs synchronous Redis I/O inside transcribe_voice_message, blocking the event loop during Redis latency. Offload the budget call with run_blocking on the database executor.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/routers/chat.py, line 1190:

<comment>When a multipart batch has no readable durations, this new unconditional call performs synchronous Redis I/O inside `transcribe_voice_message`, blocking the event loop during Redis latency. Offload the budget call with `run_blocking` on the database executor.</comment>

<file context>
@@ -1178,15 +1181,15 @@ def _save_wav(path, file_obj):
-            if not allowed:
-                raise HTTPException(status_code=429, detail='Daily transcription budget exhausted')
+            total_duration_ms += duration_ms if duration_ms is not None else MAX_SESSION_DURATION_S * 1000
+        allowed, used_ms, remaining_ms = try_consume_budget(uid, total_duration_ms)
+        if not allowed:
+            raise HTTPException(status_code=429, detail='Daily transcription budget exhausted')
</file context>
Suggested change
allowed, used_ms, remaining_ms = try_consume_budget(uid, total_duration_ms)
allowed, used_ms, remaining_ms = await run_blocking(db_executor, try_consume_budget, uid, total_duration_ms)

…12718)

read_wav_duration_ms returning None (corrupt/unreadable audio) caused both
/v2/voice-messages and /v2/voice-message/transcribe to skip try_consume_budget
entirely while still running paid STT on the file. Charge the worst case
(MAX_SESSION_DURATION_S) when duration can't be determined, matching the
always-metered PCM path.

Tested: added regression tests for both endpoints asserting the budget call
fires with the MAX_SESSION_DURATION_S*1000 fallback when duration is
unreadable, and can trip the 429 gate; updated the existing multi-file test
that encoded the old (buggy) skip-on-None behavior. All 107 tests in
test_desktop_transcribe.py + test_voice_duration_limiter.py pass, plus the
6 other suites sharing the chat-router test harness (39 tests).

🤖 automated by hourly watchdog — tested and merged
@kodjima33
kodjima33 force-pushed the watchdog/issue-12718-unreadable-wav-skips-stt-budget branch from cc1e605 to 84cfcb9 Compare September 4, 2026 09:27
@kodjima33
kodjima33 merged commit e0d970d into main Sep 4, 2026
38 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-12718-unreadable-wav-skips-stt-budget branch September 4, 2026 10:29
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.

Unreadable WAV duration skips daily STT budget while paid STT still runs

1 participant