Skip to content

fix(backend): gate app generate and icon routes on chat quota - #12717

Closed
aryanorastar wants to merge 1 commit into
BasedHardware:mainfrom
aryanorastar:fix/app-generator-chat-quota
Closed

aryanorastar wants to merge 1 commit into
BasedHardware:mainfrom
aryanorastar:fix/app-generator-chat-quota

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #12715.

Summary

POST /v1/app/generate and POST /v1/app/generate-icon bill managed LLM/image spend under Features.APP_GENERATOR via track_usage, but never called enforce_chat_quota. Sibling description generators in the same router already gate free users past the monthly chat cap with a 402.

This adds the same enforce_chat_quota(uid) call before provider work on those two endpoints.

Change

  • backend/routers/apps.py — enforce_chat_quota on generate + generate-icon
  • backend/tests/unit/test_free_quota_gate_wiring.py — blocked + allowed wiring coverage for both routes
  • .github/failure-classes/FC-sibling-managed-llm-missing-chat-quota-gate.json — new failure class

Left alone on purpose: /v1/app/generate-prompts, /v1/personas/twitter/initial-message. No OpenAPI/signature change (platform header not added) so free-tier gating lands without generated-client churn.

Verification

  • Code on main (fab60317): description endpoints call enforce_chat_quota; generate/generate-icon only track_usage (no quota). track_usage does not enforce.
  • After fix: pytest tests/unit/test_free_quota_gate_wiring.py::TestAppGeneratorGates → 6 passed; full wiring file → 15 passed

Honest gaps

  • No live free-tier Firebase/Redis integration run (hermetic gate patch only).
  • Does not pass X-App-Platform into the gate (description siblings do); free monthly cap still enforces. Platform-aware trial paywall on these two routes can follow later if product wants parity.
  • Physical device smoke not run (no kit; backend-only change).

Failure-Class: new

Line-Count-Exception: backend/routers/apps.py | 2454 -> 2462 | reason: add enforce_chat_quota calls to generate and generate-icon to match sibling description generators; no safe split for a two-call-site wiring fix

Review in cubic

Match /v1/app/generate and /v1/app/generate-icon to the description
generators so free users past the monthly chat cap cannot trigger
Omi-paid APP_GENERATOR LLM/image spend.

Failure-Class: new
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@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.

2 issues found across 3 files

Confidence score: 3/5

  • backend/routers/apps.py performs synchronous subscription and usage lookups inside async endpoints, so slow Firestore or Redis calls can block the event loop and stall unrelated requests; move the blocking work off the event-loop thread or use async clients.
  • The new quota gates in backend/routers/apps.py omit platform, unlike sibling description routes, so is_trial_paywalled may return false and allow trial requests through without the intended platform-specific enforcement; pass x_app_platform consistently.
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/apps.py">

<violation number="1" location="backend/routers/apps.py:1593">
P2: Every request to these async endpoints now runs synchronous subscription and usage lookups on the event-loop thread. A slow Firestore/Redis lookup can therefore stall unrelated requests handled by the same worker; run the gate through the backend’s blocking executor before awaiting provider work.</violation>

<violation number="2" location="backend/routers/apps.py:1593">
P2: The sibling description routes in the same router call `enforce_chat_quota(uid, platform=x_app_platform)`, but these two new gates pass only `uid`. Because `is_trial_paywalled` returns False when `platform` is None, the desktop 3-day trial paywall never applies to `/v1/app/generate` and `/v1/app/generate-icon`, so a desktop free user on trial can still trigger managed APP_GENERATOR/image spend here while the sibling description routes are blocked. The free monthly cap still enforces, but the gate is inconsistent with the pattern this fix claims to match. Accept the `X-App-Platform` header (as the description endpoints do) and pass it through, or document the divergence explicitly.</violation>
</file>

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

Re-trigger cubic

Comment thread backend/routers/apps.py

# User-initiated LLM generation — same free-tier gate as chat (402 past cap).
# Sibling description generators already enforce; this path was left ungated.
enforce_chat_quota(uid)

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: Every request to these async endpoints now runs synchronous subscription and usage lookups on the event-loop thread. A slow Firestore/Redis lookup can therefore stall unrelated requests handled by the same worker; run the gate through the backend’s blocking executor before awaiting provider work.

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

<comment>Every request to these async endpoints now runs synchronous subscription and usage lookups on the event-loop thread. A slow Firestore/Redis lookup can therefore stall unrelated requests handled by the same worker; run the gate through the backend’s blocking executor before awaiting provider work.</comment>

<file context>
@@ -1588,6 +1588,10 @@ async def generate_app_endpoint(data: GenerateAppRequest, uid: str = Depends(aut
 
+    # User-initiated LLM generation — same free-tier gate as chat (402 past cap).
+    # Sibling description generators already enforce; this path was left ungated.
+    enforce_chat_quota(uid)
+
     prompt = data.prompt.strip()
</file context>

Comment thread backend/routers/apps.py

# User-initiated LLM generation — same free-tier gate as chat (402 past cap).
# Sibling description generators already enforce; this path was left ungated.
enforce_chat_quota(uid)

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: The sibling description routes in the same router call enforce_chat_quota(uid, platform=x_app_platform), but these two new gates pass only uid. Because is_trial_paywalled returns False when platform is None, the desktop 3-day trial paywall never applies to /v1/app/generate and /v1/app/generate-icon, so a desktop free user on trial can still trigger managed APP_GENERATOR/image spend here while the sibling description routes are blocked. The free monthly cap still enforces, but the gate is inconsistent with the pattern this fix claims to match. Accept the X-App-Platform header (as the description endpoints do) and pass it through, or document the divergence explicitly.

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

<comment>The sibling description routes in the same router call `enforce_chat_quota(uid, platform=x_app_platform)`, but these two new gates pass only `uid`. Because `is_trial_paywalled` returns False when `platform` is None, the desktop 3-day trial paywall never applies to `/v1/app/generate` and `/v1/app/generate-icon`, so a desktop free user on trial can still trigger managed APP_GENERATOR/image spend here while the sibling description routes are blocked. The free monthly cap still enforces, but the gate is inconsistent with the pattern this fix claims to match. Accept the `X-App-Platform` header (as the description endpoints do) and pass it through, or document the divergence explicitly.</comment>

<file context>
@@ -1588,6 +1588,10 @@ async def generate_app_endpoint(data: GenerateAppRequest, uid: str = Depends(aut
 
+    # User-initiated LLM generation — same free-tier gate as chat (402 past cap).
+    # Sibling description generators already enforce; this path was left ungated.
+    enforce_chat_quota(uid)
+
     prompt = data.prompt.strip()
</file context>
Suggested change
enforce_chat_quota(uid)
enforce_chat_quota(uid, platform=x_app_platform)

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @aryanorastar — verified end to end; this closes a real managed-spend leak and matches the router's existing gate architecture. Per-file notes:

backend/routers/apps.py — Correct. On main, generate_app_endpoint and generate_app_icon_endpoint wrap spend in track_usage(uid, Features.APP_GENERATOR) but never call enforce_chat_quota, while the sibling description generators gate before the same tracker — track_usage only attributes usage, it doesn't block. Placing enforce_chat_quota(uid) before validation and provider work is right, and the 402 propagates cleanly. I also verified the monthly free-cap path is platform-independent: get_chat_quota_snapshot enforces used < limit regardless of the platform argument.

Two notes on the earlier bot review's P2s, both non-blocking:

  1. platform not passed (parity with the description siblings): with platform=None, is_trial_paywalled returns False, so the desktop 3-day trial paywall can never fire on these two routes. Today that's moot (TRIAL_PAYWALL_ENABLED defaults to false), and threading the header through would change the endpoint signatures/OpenAPI — the scoping call in "Honest gaps" is reasonable. A small follow-up (or a TODO) would keep the parity from being forgotten if the trial paywall is re-enabled.
  2. Sync gate on the event loop: enforce_chat_quota does Firestore/Redis I/O synchronously inside the async handler — but that's exactly the pre-existing pattern of the sibling description endpoints in this router (and routers/goals.py); routers/chat_generation.py shows the run_blocking alternative. Following the local convention is the right call for this fix; the executor migration is a router-wide cleanup rather than something this PR should carry.

backend/tests/unit/test_free_quota_gate_wiring.py — Good wiring tests. The blocked cases assert both 402 propagation and llm.assert_not_awaited() (proving gate-before-spend), the allowed cases assert the proceed path, and gate.assert_called_once_with('u1') pins the new call signature. Patching utils.llm.app_generator.* at the source module correctly handles the in-function imports, and the docstring update keeps the file's coverage map accurate.

.github/failure-classes/FC-sibling-managed-llm-missing-chat-quota-gate.json — Follows the established schema (violated_contract / canonical_prevention / canonical_prevention_artifact / scope_hints). evidence_prs: [] is fine for a newly born class; referencing this PR once merged would help future greps.

One residual for maintainers (explicitly out of scope in #12715, so not a change request): /v1/app/generate-prompts still bills Features.APP_GENERATOR under rate-limit only.

No split needed — three files, one concern. Leaving for human maintainer merge sign-off since this sits on the billing/quota surface, alongside the minor product call on X-App-Platform parity above.

— automated review on behalf of the Omi maintainers (AI-generated).


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 positive-signal Automation verified a genuine fix/quality contribution backend Backend Task (python) labels Sep 4, 2026
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Closing — superseded on main by #12739 (1fff74f7be), which fixed #12715 and closed it as completed today.

The diagnosis held: on this PR's base (fab60317cd) backend/routers/apps.py had three enforce_chat_quota references — the import and the two description generators — and generate_app_endpoint / generate_app_icon_endpoint billed Features.APP_GENERATOR through track_usage with no gate. main now has five, with both of those endpoints gated.

#12739 also took the option I scoped out. I passed uid alone and wrote in "Honest gaps" that threading X-App-Platform would churn the generated clients; cubic's P2 said to pass it anyway. #12739 passes platform=x_app_platform and regenerates all six client artifacts plus the OpenAPI document. That is the better call — with platform=None the desktop trial paywall could never fire on these routes, so the parity gap would have been left to rediscover if TRIAL_PAYWALL_ENABLED were ever turned back on. I under-weighted that against the client-regeneration cost.

Two things from this PR that did not land in #12739, neither of which needs this branch:

/v1/app/generate-prompts is still ungated. generate_sample_prompts_endpoint on current main runs with track_usage(uid, Features.APP_GENERATOR) behind auth.with_rate_limit(...) and no enforce_chat_quota. It was explicitly out of scope for #12715, so #12739 correctly left it — but it is the same managed-spend leak on the same feature, and it is now the only one left in this router. Happy to send that as a one-line PR matching #12739's shape exactly.

.github/failure-classes/FC-sibling-managed-llm-missing-chat-quota-gate.json is not on main. The class is real — a route billing managed spend while its siblings in the same router gate it — and #12739's test is the natural canonical_prevention_artifact. Worth a registry entry only if a maintainer wants one; not worth a PR on its own otherwise.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Hey @aryanorastar 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our product principles and locked invariants

Before your next PR, please skim:

  • PRODUCT.md — product north star
  • Product invariants — locked rules (shared chat, memory tiers, agent control plane, integrations, brand)

If this was declined for direction or taste, maintainers should cite an invariant ID or open a proposed one — ask if that citation is missing.

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Free-tier chat quota not enforced on /v1/app/generate and /v1/app/generate-icon

2 participants