Skip to content

fix(backend): enforce free-tier chat quota on AI app generate + generate-icon (#12715) - #12739

Merged
kodjima33 merged 3 commits into
mainfrom
watchdog/issue-12715-app-generate-quota-gate
Sep 4, 2026
Merged

kodjima33 merged 3 commits into
mainfrom
watchdog/issue-12715-app-generate-quota-gate

Conversation

@kodjima33

@kodjima33 kodjima33 commented Sep 4, 2026 •

Copy link
Copy Markdown
Collaborator

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 in track_usage(..., Features.APP_GENERATOR) but never called enforce_chat_quota, unlike the sibling generate-description / generate-description-emoji endpoints in the same router, which already gate on it (a0cbbd90a5). track_usage only 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 both generate_app_endpoint and generate_app_icon_endpoint. Added the X-App-Platform header 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_cap and test_generate_app_icon_blocked_past_cap, mirroring the existing description-generator tests. Ran the full file locally: 14 passed. Also ran the broader test_apps_*.py suite (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

Review in cubic

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

mintlify Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated
omi 🟢 Ready View Preview Sep 4, 2026, 12:47 PM

💡 Tip: Enable Automations to automatically generate PRs for you.

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

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 — run enforce_chat_quota through run_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

Comment thread backend/routers/apps.py
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)

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: 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
@kodjima33
kodjima33 merged commit 1fff74f into main Sep 4, 2026
45 of 46 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-12715-app-generate-quota-gate branch September 4, 2026 14:44
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

1 active deployment
staging - docs — ada5aa3d Deployed Sep 4, 2026 by mintlify[bot]
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.

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

1 participant