Repository navigation
feat(backend): Add trial metadata endpoint for desktop paywall UX - #7330
Conversation
Part of #7329: rework free trial paywall for macOS desktop app. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Adds structured trial metadata computation for desktop clients: - trial_started_at, trial_ends_at, trial_remaining_seconds - Fail-open on Firebase errors (never false-paywall) - Paid/BYOK users get trial_expired=False (trial is moot) - TRIAL_LENGTH_SECONDS now configurable via env var (default 3 days) Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Returns trial timing (start, end, remaining), features list, and post-trial plan name. Used by desktop clients for countdown UI and pre-expiry upgrade nudges. Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Source-level tests verify wiring, behavioral tests validate: - Active trial timing, expired trial, paid/BYOK bypass - Firebase failure fail-open, model serialization Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Verifies endpoint uses Depends(auth.get_current_user_uid), is a GET method, and returns get_trial_metadata(uid) result. Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Greptile SummaryAdds a new
Confidence Score: 4/5Safe to merge; the change is purely additive and does not alter existing paywall enforcement. The endpoint and model are correctly implemented and the fail-open design is sound. The docstring inside get_trial_metadata incorrectly states the function benefits from the existing Redis cache when it actually calls Firebase Auth directly on every invocation. Separately, the exception handler references names defined much later in the file, which could silently break the fail-open guarantee after future refactoring. backend/utils/subscription.py — the uncached Firebase call and the forward-reference in the exception handler both live here. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client as Desktop Client
participant Router as GET /v1/users/me/trial
participant Sub as get_trial_metadata()
participant UsersDB as users_db
participant Firebase as firebase_auth (uncached)
participant Redis as Redis Cache
Client->>Router: GET /v1/users/me/trial (Bearer token)
Router->>Sub: get_trial_metadata(uid)
Sub->>UsersDB: get_user_valid_subscription(uid)
UsersDB-->>Sub: subscription (plan)
alt Paid plan or BYOK
Sub->>UsersDB: is_byok_active(uid)
UsersDB-->>Sub: True/False
Sub-->>Router: "TrialMetadata(trial_expired=False, zeroed timing)"
else Basic plan, no BYOK
Sub->>Firebase: get_user(uid) [NO Redis cache]
Firebase-->>Sub: user_record.creation_timestamp
Note over Sub,Redis: Redis cache NOT consulted here
Sub->>Sub: "compute remaining = max(0, ends_at - now)"
Sub-->>Router: TrialMetadata(full timing)
end
alt Exception at any step
Sub-->>Router: "TrialMetadata(trial_expired=False) [fail-open]"
end
Router-->>Client: TrialMetadata JSON
Reviews (1): Last reviewed commit: "Add auth dependency verification tests p..." | Re-trigger Greptile |
| This reuses the same Firebase Auth lookup path as `_is_trial_expired_uncached` | ||
| and benefits from the same Redis cache for the expensive bits. |
There was a problem hiding this comment.
The docstring says the function "benefits from the same Redis cache for the expensive bits," but
get_trial_metadata never consults the Redis cache — it calls firebase_auth.get_user(uid) directly at line 136. The existing cache in _is_trial_expired_cached stores only a boolean expired flag (not the creation timestamp), so it can't be reused here anyway. For a countdown-banner endpoint that desktop clients will poll regularly, every call triggers an uncached Firebase Auth network round-trip. The comment should be corrected to avoid misleading future callers into thinking this path is cached.
| This reuses the same Firebase Auth lookup path as `_is_trial_expired_uncached` | |
| and benefits from the same Redis cache for the expensive bits. | |
| This uses the same Firebase Auth lookup path as `_is_trial_expired_uncached` | |
| but does NOT benefit from the Redis cache in `_is_trial_expired_cached` (which | |
| stores only a boolean flag, not the creation timestamp). Each call makes a | |
| direct, uncached Firebase Auth request. Consider caching the creation timestamp | |
| separately if this endpoint is polled frequently by desktop clients. |
| except Exception as e: | ||
| logger.warning("get_trial_metadata failed for uid=%s: %s", uid, e) | ||
| # Fail-open: report as active trial so UI doesn't flash paywall. | ||
| return TrialMetadata( | ||
| trial_expired=False, | ||
| trial_duration_seconds=TRIAL_LENGTH_SECONDS, | ||
| trial_features=TRIAL_FEATURES, | ||
| plan_after_trial=get_plan_display_name(PlanType.basic), | ||
| ) |
There was a problem hiding this comment.
Fragile fail-open: exception handler references forward-declared names
The except handler at line 162 references TRIAL_FEATURES (defined at line 399) and get_plan_display_name (defined at line 416). Python resolves names at call time, so this works while the module loads normally. However, if either name is undefined when this handler fires — for example, after a refactoring that moves or conditionally guards TRIAL_FEATURES — the handler itself raises an uncaught NameError, silently defeating the documented fail-open guarantee. Moving TRIAL_FEATURES to before get_trial_metadata in the file would make the dependency explicit and safe.
Addresses tester coverage gap: verifies behavior at the exact 3-day boundary (1 second before and after expiry). Part of #7329. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Summary
Adds
GET /v1/users/me/trialendpoint that returns structured trial metadata for desktop clients to render countdown banners and pre-expiry upgrade nudges. This is Phase 1 (backend) of the free trial paywall rework (#7329).Changes
models/users.py— NewTrialMetadataPydantic model with fields:trial_started_at,trial_ends_at,trial_remaining_seconds,trial_expired,trial_duration_seconds,trial_features,plan_after_trialutils/subscription.py—TRIAL_LENGTH_SECONDSnow env-configurable (default 3 days, same as before)TRIAL_FEATURESconstant listing features available during trialget_trial_metadata(uid)function: computes trial timing from Firebase Auth creation timestamp, handles paid/BYOK bypass, fails open on errorsrouters/users.py— NewGET /v1/users/me/trialendpoint returningTrialMetadatatests/unit/test_trial_metadata.py— 30 tests (source-level + behavioral + auth + boundary) covering active trial, expired trial, paid/BYOK bypass, Firebase failure, boundary edge cases, model serializationDesign decisions
trial_expired=False(never false-paywall)trial_expired=Falsewith zeroed timing (trial is irrelevant)_is_trial_expired_cached)/v1/users/me/paywallendpoint unchangedResponse shape
{ "trial_started_at": 1747123456, "trial_ends_at": 1747382656, "trial_remaining_seconds": 172800, "trial_expired": false, "trial_duration_seconds": 259200, "trial_features": ["unlimited_listening", "unlimited_transcription", "unlimited_memories", "unlimited_insights", "30_chat_questions_per_month"], "plan_after_trial": "Free" }Test plan
curl /v1/users/me/trialreturns 200 with correct JSON/v1/users/me/paywallendpoint unaffectedLive test evidence
Risks
Closes #7329 (Phase 1)
by AI for @beastoin