Repository navigation
fix(backend): enforce free-tier chat quota on AI app generate + generate-icon (#12715) - #12739
Merged
Merged
Conversation
…ate-icon A free user past the monthly chat quota could still trigger billable AI app-config generation (/v1/app/generate) and DALL-E icon generation (/v1/app/generate-icon): both wrap spend in track_usage(..., Features.APP_GENERATOR) but, unlike the sibling generate-description / generate-description-emoji endpoints in the same router, never called enforce_chat_quota before doing the LLM work. Add the same enforce_chat_quota(uid, platform=x_app_platform) gate used by the sibling endpoints, plus regression tests pinning the wiring (mirrors the existing TestAppGeneratorGates pattern in test_free_quota_gate_wiring.py). Fixes #12715.
…param Companion to the enforce_chat_quota fix — /v1/app/generate and /v1/app/generate-icon now accept an X-App-Platform header, matching their sibling generate-description endpoints.
Companion to the app-client OpenAPI regen — keeps the generated Swift and TypeScript clients in sync with the new header parameter on /v1/app/generate and /v1/app/generate-icon.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Automations to automatically generate PRs for you. |
There was a problem hiding this comment.
1 issue found across 8 files
Confidence score: 3/5
- In
backend/routers/apps.py, the async handler performs synchronous Firebase/Firestore and usage reads for ordinary requests, which can block the event loop and degrade concurrency or latency under load — runenforce_chat_quotathroughrun_blocking(db_executor, ...).
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:1596">
P2: On ordinary requests, this synchronous quota lookup blocks the event loop of the async handler while Firebase/Firestore and usage reads run. Await `enforce_chat_quota` through `run_blocking(db_executor, ...)` instead.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| from utils.llm.app_generator import generate_app_from_prompt | ||
|
|
||
| # User-initiated LLM generation — same free-tier gate as chat (402 past cap). | ||
| enforce_chat_quota(uid, platform=x_app_platform) |
There was a problem hiding this comment.
P2: On ordinary requests, this synchronous quota lookup blocks the event loop of the async handler while Firebase/Firestore and usage reads run. Await enforce_chat_quota through run_blocking(db_executor, ...) instead.
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 1596:
<comment>On ordinary requests, this synchronous quota lookup blocks the event loop of the async handler while Firebase/Firestore and usage reads run. Await `enforce_chat_quota` through `run_blocking(db_executor, ...)` instead.</comment>
<file context>
@@ -1581,13 +1581,20 @@ async def generate_sample_prompts_endpoint(
from utils.llm.app_generator import generate_app_from_prompt
+ # User-initiated LLM generation — same free-tier gate as chat (402 past cap).
+ enforce_chat_quota(uid, platform=x_app_platform)
+
prompt = data.prompt.strip()
</file context>
Suggested change
| enforce_chat_quota(uid, platform=x_app_platform) | |
| await run_blocking(db_executor, enforce_chat_quota, uid, platform=x_app_platform) |
kodjima33
added a commit
that referenced
this pull request
Sep 4, 2026
…leanup (#12740) The `Backend unit suite` CI lane (added in c094859, merged this morning) fails unconditionally on every PR to main: - tests/slow_guardrail_manifest.txt lists tests/unit/test_ws_g_module_aliases.py, which was deleted by a7bc5fd (dead-code removal) the day before the manifest was built, so run-slow-guardrails-ci.sh exits 1 before running anything. - Once that's fixed, test_managed_call_sites_absent_from_the_inventory_fail_ci (backend/tests/unit/test_llm_gateway_coverage_guardrails.py) fails because docs/llm/model_endpoint_inventory.yaml still lists call sites in utils/llm/promotion_proposals.py and utils/llm/promotion_routes.py, also removed by a7bc5fd. Both are stale references to the same commit's deletions, not code bugs. Verified: 5 of 6 currently-open backend PRs (#12739, #12721, #12719, #12714, #12712) show this exact "Backend unit suite" FAILURE. Fix: remove the stale manifest line (with a dated note per the file's own "never silently dropped" convention) and the two stale inventory entries. Tested: `PYTHON=.venv/bin/python bash scripts/run-slow-guardrails-ci.sh` now passes (103 passed, 1 skipped, was: hard-failing before any test ran). Also ran test_llm_gateway_coverage_guardrails.py + test_firestore_query_contract.py directly: 72 passed. Failure-Class: none
undivisible
pushed a commit
that referenced
this pull request
Sep 5, 2026
/v1/app/generate-prompts runs the same billable Features.APP_GENERATOR spend as its siblings but sat behind rate limiting alone, with no enforce_chat_quota call. #12739 closed /v1/app/generate and /v1/app/generate-icon for #12715 and correctly left this one, since it was outside that issue's scope. It is now the only ungated managed-spend route in the router: a free user past the monthly cap can still draw LLM-generated app suggestions from it indefinitely. Add the same gate the other four generators use, in the same shape #12739 established -- enforce_chat_quota(uid, platform=x_app_platform) before any provider work, with the X-App-Platform header accepted so the desktop trial paywall applies here too rather than silently never firing on platform=None. The generated-client churn is a reordering, not an addition. X-App-Platform was already advertised on this route through the shared auth dependency; declaring it explicitly moves it ahead of the injected headers, which is exactly the ordering /v1/app/generate, /v1/app/generate-icon and /v1/app/generate-description already have. This route now matches its siblings byte for byte. Verified by mutation: with the gate removed, both new tests fail. Failure-Class: none Co-authored-by: Claude Opus 5 <[email protected]>
This branch was successfully deployed
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
A free user past the monthly chat quota could still trigger billable AI app-config generation (
POST /v1/app/generate) and DALL-E icon generation (POST /v1/app/generate-icon). Both wrap spend intrack_usage(..., Features.APP_GENERATOR)but never calledenforce_chat_quota, unlike the siblinggenerate-description/generate-description-emojiendpoints in the same router, which already gate on it (a0cbbd90a5).track_usageonly attributes spend — it does not block past-cap callers.Fix
Add the same
enforce_chat_quota(uid, platform=x_app_platform)call used by the sibling endpoints, before any LLM/DALL-E work, to bothgenerate_app_endpointandgenerate_app_icon_endpoint. Added theX-App-Platformheader param to match.Tests
Extended
backend/tests/unit/test_free_quota_gate_wiring.py::TestAppGeneratorGates(the existing wiring-test class for this exact router) with two new regression tests,test_generate_app_blocked_past_capandtest_generate_app_icon_blocked_past_cap, mirroring the existing description-generator tests. Ran the full file locally: 14 passed. Also ran the broadertest_apps_*.pysuite (52 tests) to confirm no regression.Line-Count-Exception: backend/routers/apps.py | 2454 -> 2468 | 14-line net growth is the enforce_chat_quota call + X-App-Platform header param added to two endpoints, mirroring the existing pattern already in this same file for generate-description/generate-description-emoji
Fixes #12715.
🤖 automated by hourly watchdog — tested and merged
Failure-Class: none