Skip to content

backend: track mobile custom-STT users on the user doc (#7690 step 1) - #7723

Merged
mdmohsin7 merged 4 commits into
mainfrom
caleb/track-custom-stt
Jun 9, 2026
Merged

mdmohsin7 merged 4 commits into
mainfrom
caleb/track-custom-stt

Conversation

@mdmohsin7

Copy link
Copy Markdown
Member

Why

Mobile custom‑STT usage is currently not recorded anywhere — the app only sends a per‑session custom_stt=enabled WS param, so there's no way to query "who uses custom STT" or meter it. This blocks acting on #7690 (custom‑STT users get uncapped Omi LLM post‑processing on the free tier). Step 1 is to make these users identifiable.

What

Persist custom‑STT usage onto the user doc, written from the listen handler only when it changes (kept off the hot path):

  • transcription_preferences.uses_custom_stt — bool, current state (defaults to false / absent).
  • transcription_preferences.custom_stt_since — timestamp the current custom‑STT streak began (set on off→on, cleared on on→off).

Changes:

  • database/users.py: new set_user_custom_stt_usage(uid, uses_custom_stt); get_user_transcription_preferences now returns the two fields.
  • routers/transcribe.py: after reading prefs, if use_custom_stt differs from the stored value, write via run_blocking(db_executor, …). Steady‑state users (no change) trigger no extra write.
  • routers/users.py: TranscriptionPreferencesResponse exposes the two fields via the existing GET endpoint.

Notes

Refs #7690.

@greptile-apps

greptile-apps Bot commented Jun 9, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR persists mobile custom-STT usage onto the Firestore user document so that custom-STT users are queryable and meterable (prerequisite for #7690). Writes are conditional — only when uses_custom_stt changes state — keeping the change off the WebSocket hot path.

  • database/users.py: Adds set_user_custom_stt_usage which stamps transcription_preferences.uses_custom_stt and transcription_preferences.custom_stt_since on every off→on/on→off transition, and extends get_user_transcription_preferences to return both new fields.
  • routers/transcribe.py: Calls the new function after reading prefs, wrapped in run_blocking + try/except so a Firestore failure never aborts the WebSocket session (addressing the previous review concern).
  • routers/users.py: Exposes the two new fields through the existing GET /v1/users/transcription-preferences endpoint.

Confidence Score: 5/5

Safe to merge — the tracking write is fully isolated from the WebSocket session via try/except, so no Firestore failure can affect users.

The change is additive and narrowly scoped: it reads one extra field from Firestore and conditionally fires a best-effort write that can never crash the session. The previous concern about an unguarded write propagating out of the handler has been resolved. The remaining note is about future call-site safety for custom_stt_since, which does not affect current behaviour.

No files require special attention; database/users.py carries a minor design note about the custom_stt_since reset contract being caller-enforced rather than function-enforced.

Important Files Changed

Filename Overview
backend/database/users.py Adds set_user_custom_stt_usage and extends get_user_transcription_preferences with two new fields. Logic is sound for single-caller scenarios; the custom_stt_since reset contract is entirely caller-enforced.
backend/routers/transcribe.py Calls set_user_custom_stt_usage on off→on/on→off transitions via run_blocking, guarded by a try/except so a Firestore failure cannot tear down the WebSocket session. Clean implementation.
backend/routers/users.py Adds uses_custom_stt and custom_stt_since fields to TranscriptionPreferencesResponse with correct defaults; straightforward extension of the existing schema.

Sequence Diagram

sequenceDiagram
    participant App as Mobile App
    participant WS as _stream_handler (transcribe.py)
    participant DB as Firestore (users.py)

    App->>WS: "WebSocket connect (custom_stt=enabled)"
    WS->>DB: get_user_transcription_preferences(uid)
    DB-->>WS: "{uses_custom_stt: false, ...}"

    Note over WS: use_custom_stt (True) != stored (False) → change detected

    WS->>DB: run_blocking → set_user_custom_stt_usage(uid, True)
    DB-->>WS: "write {uses_custom_stt:true, custom_stt_since: now()} [best-effort]"

    Note over WS: Session continues regardless of write success/failure

    App->>WS: "WebSocket connect (custom_stt=disabled)"
    WS->>DB: get_user_transcription_preferences(uid)
    DB-->>WS: "{uses_custom_stt: true, ...}"

    Note over WS: use_custom_stt (False) != stored (True) → change detected

    WS->>DB: run_blocking → set_user_custom_stt_usage(uid, False)
    DB-->>WS: "write {uses_custom_stt:false, custom_stt_since: null}"
Loading

Reviews (2): Last reviewed commit: "backend: guard custom-STT tracking write..." | Re-trigger Greptile

Comment thread backend/routers/transcribe.py Outdated
Comment on lines +337 to +338
if use_custom_stt != transcription_prefs.get('uses_custom_stt', False):
await run_blocking(db_executor, user_db.set_user_custom_stt_usage, uid, use_custom_stt)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Unguarded write can drop the WebSocket session

This run_blocking call sits in the pre-setup section of _stream_handler, before the main try/finally block that starts at the "# Start" comment (~line 2665). An unhandled exception here propagates straight out of _stream_handler without any cleanup, aborting the WebSocket connection for the user. Because set_user_custom_stt_usage is a non-critical metadata write (the session needs no return value from it), a transient Firestore error during high load or a network hiccup would silently drop users who are toggling custom STT for the first time, with no indication other than a failed connection. Wrap it the same way send_credit_limit_notification is wrapped a few lines above: catch, log, and continue.

@mdmohsin7

Copy link
Copy Markdown
Member Author

@greptile-apps re-review

@mdmohsin7
mdmohsin7 merged commit 062a8b2 into main Jun 9, 2026
3 checks passed
@mdmohsin7
mdmohsin7 deleted the caleb/track-custom-stt branch June 9, 2026 14:17
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