Repository navigation
fix(backend): unreadable WAV duration must not skip STT budget check (#12718) - #12723
Merged
kodjima33 merged 1 commit intoSep 4, 2026
Merged
Conversation
There was a problem hiding this comment.
1 issue found across 3 files
Confidence score: 3/5
- In
backend/routers/chat.py,transcribe_voice_messageperforms 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
| 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) |
There was a problem hiding this comment.
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
force-pushed
the
watchdog/issue-12718-unreadable-wav-skips-stt-budget
branch
from
September 4, 2026 09:27
cc1e605 to
84cfcb9
Compare
kodjima33
deleted the
watchdog/issue-12718-unreadable-wav-skips-stt-budget
branch
September 4, 2026 10:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
On the WAV/multipart voice STT routes, when
read_wav_duration_msreturnsNone(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 calledtry_consume_budgetif duration_ms is not None./v2/voice-message/transcribe(multipart,backend/routers/chat.py:1183): summed only the readable files' durations, and skippedtry_consume_budgetentirely if the sum was0(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_msreturnsNone, 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
test_multipart_unreadable_wav_does_not_skip_budget_checkandtest_voice_messages_unreadable_wav_does_not_skip_budget_check: assert the budget call fires with theMAX_SESSION_DURATION_S * 1000fallback when duration is unreadable, and that it can trip the 429 gate.test_multipart_multi_file_budget_sums_durations, which previously asserted the old (buggy) skip-on-Nonebehavior, 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._chat_router_test_harness.py(which needed the sameMAX_SESSION_DURATION_Sstub 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 overFailure-Class: none
🤖 automated by hourly watchdog — tested and merged