Skip to content

fix(backend): refuse a custom vocabulary over 100 terms - #22401

Merged
kodjima33 merged 3 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/backend-vocabulary-over-limit
Oct 19, 2026
Merged

kodjima33 merged 3 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/backend-vocabulary-over-limit

Conversation

@apoorvdarshan

Copy link
Copy Markdown
Collaborator

What changed and why

Fixes #22400

PATCH /v1/users/transcription-preferences replaces the account's custom vocabulary with the list it is sent. set_user_transcription_preferences stored only vocabulary[:100], but the route still answered {'status': 'ok'}, and the clients take that answer to mean the whole list was saved. The mobile app's UserProvider.updateTranscriptionVocabulary kept and showed (and cached) every term it sent, and web kept its optimistic list. So once a user had 100 terms, each new one looked saved, was never used for transcription (live STT reads the stored list), and was gone at the next load, with no error.

The route now refuses a list over 100 terms with 400 Custom vocabulary is limited to 100 terms and stores nothing, including a single_language_mode sent in the same request. Lists of up to 100 terms are saved as before.

How the clients handle the 400, with no client change:

  • Mobile: setTranscriptionPreferences returns false on a non-200, so the provider keeps the list it had and the word is not added. On main the page shows no error for a failed save; fix(app): keep typed vocabulary words when saving them fails #22354 adds one.
  • Web: updateCustomVocabulary throws and SettingsPage puts the previous list back.
  • macOS: the pane adds the word to its local list before the PATCH and only logs a failed save, so it shows the word until Settings reloads the account's list, as it already did with the silent trim. That edit path is reworked in fix(desktop): keep the vocabulary list when it failed to load #22356, so it is not touched here.
  • Windows has no vocabulary editor on main.

The check is in the handler, so the request schema and the app-client OpenAPI snapshot are unchanged. The endpoint description already says "max 100".

Line-Count-Exception: backend/routers/users.py | 2715 -> 2718 | Three-line limit check inside the existing transcription-preferences handler, before its only write

Product invariants affected

none

How it was verified

  • Read the write path: the route passes data.vocabulary to set_user_transcription_preferences in backend/database/users.py, which writes vocabulary[:100], then answers ok. Read the client save paths listed above and the consumers of the stored list (utils/listen_session_bootstrap.py, utils/stt/live_session.py).
  • Checked that no caller sends more than 100 terms on purpose: the backend e2e tests and the macOS vocabulary bridge flow send two terms, and Windows does not write the vocabulary.
  • Ran python -m py_compile on the changed Python files. The pre-commit hook ran the pinned black on them, with no changes.
  • I did not run tests locally. CI with ci:full runs the new test and the backend suites. I wrote the over-limit test to fail without the fix: before it, the route answers 200 and the save is called with all 101 terms.

Tests

backend/tests/unit/test_transcription_vocabulary_limit.py, over HTTP with a fake save, in the style of test_daily_summary_depth_settings.py:

  • test_vocabulary_over_the_limit_is_refused_and_nothing_is_saved: 101 terms get a 400 and nothing is saved. Before the fix this got 200.
  • test_vocabulary_at_the_limit_is_saved_whole: 100 terms are saved unchanged and answered ok.

UI evidence

UI-Evidence: none -- backend-only change with no UI edits; the clients' existing failed-save paths handle the 400, as described above

Failure class (fixes)

Failure-Class: new

Failure-class transition narrative

New class FC-truncated-write-acknowledged-as-saved.

  • Violated contract: a write endpoint must not answer success after storing less than it was sent.
  • Canonical guard: enforce the limit at the route boundary by refusing the request before anything is written, so every client's failed-save path keeps its state. A data-layer trim is only a backstop. The guard artifact is the new test.
  • Evidence: this PR. The route answered ok after writing only vocabulary[:100].

🤖 Generated with Claude Code

PATCH /v1/users/transcription-preferences stored only the first 100
vocabulary terms but still answered {'status': 'ok'}. The mobile app
then kept and showed every term it had sent, and web kept its
optimistic list, so terms past 100 looked saved, were never used for
transcription, and were gone at the next load.

The route now answers 400 for a list over 100 terms and stores
nothing, so each client's failed-save path keeps the list it had.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@apoorvdarshan apoorvdarshan added the ci:full Maintainer-granted: run full CI on this fork PR label Oct 19, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The PATCH route's UserStatusResponse model also serializes its optional
message as null, so the exact-dict assertion failed in CI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The production change here is correct and nicely scoped — approving.

  • backend/routers/users.py:941-943 — the guard fires before the route's only write call, returns 400 with a clear detail, and only when data.vocabulary is not None, so the language-mode-only call path at line 902 is unaffected. Refusing before the write (rather than trimming and answering ok) is the right contract: every client's existing failed-save path keeps its previous state.
  • backend/database/users.py:2341 — vocabulary[:100] still trims as a backstop behind the route check; consistent with the failure-class guidance that a trim must never be the only enforcement behind a success answer.
  • backend/tests/unit/test_transcription_vocabulary_limit.py — covers both edges: 101 terms refused with nothing saved (saved == []), and exactly 100 terms saved whole. I ran the file locally: 2 passed.
  • .github/failure-classes/FC-truncated-write-acknowledged-as-saved.json — matches the existing per-class JSON protocol (schema_version 1, violated_contract, canonical_prevention, artifact, scope_hints); scripts/failure-class validate against the PR body passes.

USER IMPACT: users who try to save more than 100 custom vocabulary terms now get a clear refusal instead of terms silently disappearing after seemingly saving; lists up to 100 terms save exactly as before.

Remaining wants (non-blocking): a client-side 400 surfacing is tracked separately (#22354); the macOS pane still shows the word locally until reload, with that path reworked in #22356.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution labels Oct 19, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Backend bug fix, root cause clearly stated, scoped diff (61 lines), test added, CI green. Confidence 5/5.

@kodjima33
kodjima33 merged commit 851fca8 into BasedHardware:main Oct 19, 2026
54 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) ci:full Maintainer-granted: run full CI on this fork PR positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Vocabulary: words after the 100th look saved, but the server drops them

3 participants