Repository navigation
fix(backend): gate app generate-prompts on the free chat quota - #12749
undivisible merged 1 commit into
Conversation
/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. BasedHardware#12739 closed /v1/app/generate and /v1/app/generate-icon for BasedHardware#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 BasedHardware#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]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
2 issues found across 8 files
Confidence score: 2/5
- In
backend/routers/apps.py, free users can bypass quota enforcement by calling only/v1/app/generate-prompts, allowing usage beyond the intended limit—include app-generator telemetry in the chat quota snapshot and atomically increment the sharedquota_questionscounter. - In
backend/routers/apps.py, synchronous Firestore quota reads run on the event loop for ordinary requests, which can block concurrent request handling—offload the quota check throughrun_blockingon the database 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:1545">
P2: For ordinary requests, this line performs synchronous Firestore quota reads on the event loop before awaiting the LLM. Offload the blocking quota check through `run_blocking` on the database executor.</violation>
<violation number="2" location="backend/routers/apps.py:1545">
P1: When a free user calls only `/v1/app/generate-prompts`, this gate never sees those calls because app-generator telemetry is not part of the chat quota snapshot. Increment the shared `quota_questions` counter atomically for app-generation requests, or make the quota calculation include app-generator usage.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| import json | ||
|
|
||
| # 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.
P1: When a free user calls only /v1/app/generate-prompts, this gate never sees those calls because app-generator telemetry is not part of the chat quota snapshot. Increment the shared quota_questions counter atomically for app-generation requests, or make the quota calculation include app-generator usage.
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 1545:
<comment>When a free user calls only `/v1/app/generate-prompts`, this gate never sees those calls because app-generator telemetry is not part of the chat quota snapshot. Increment the shared `quota_questions` counter atomically for app-generation requests, or make the quota calculation include app-generator usage.</comment>
<file context>
@@ -1540,6 +1541,9 @@ async def generate_sample_prompts_endpoint(
import json
+ # User-initiated LLM generation — same free-tier gate as chat (402 past cap).
+ enforce_chat_quota(uid, platform=x_app_platform)
+
system_prompt = """Generate 5 creative and diverse ideas for apps that are either:
</file context>
| import json | ||
|
|
||
| # 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: For ordinary requests, this line performs synchronous Firestore quota reads on the event loop before awaiting the LLM. Offload the blocking quota check through run_blocking on the database executor.
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 1545:
<comment>For ordinary requests, this line performs synchronous Firestore quota reads on the event loop before awaiting the LLM. Offload the blocking quota check through `run_blocking` on the database executor.</comment>
<file context>
@@ -1540,6 +1541,9 @@ async def generate_sample_prompts_endpoint(
import json
+ # User-initiated LLM generation — same free-tier gate as chat (402 past cap).
+ enforce_chat_quota(uid, platform=x_app_platform)
+
system_prompt = """Generate 5 creative and diverse ideas for apps that are either:
</file context>
| enforce_chat_quota(uid, platform=x_app_platform) | |
| await run_blocking(db_executor, enforce_chat_quota, uid, platform=x_app_platform) |
|
Verified against head 3341508 — this closes the remaining spend leak from the #12739 sweep, in exactly the shape that PR established. Notes per file: backend/routers/apps.py — backend/tests/unit/test_free_quota_gate_wiring.py — Generated artifacts ( Two notes on the earlier automated review's suggestions, for the record — neither is a defect introduced by this diff:
One small accuracy nit on the description: "the last ungated managed-spend route in this router" — Thanks for the thorough write-up, the mutation check, and the regression tests — tidy completion of the sweep. This is billing/quota enforcement surface, so maintainer sign-off before merge; no concerns from my side beyond the notes above. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
undivisible
left a comment
There was a problem hiding this comment.
Verdict: approve
Closes the last ungated Features.APP_GENERATOR route in this router. Gate shape matches the #12739 siblings: X-App-Platform header + enforce_chat_quota(uid, platform=…) before any provider work, with tests that pin 402 before get_llm and the proceed path. Generated-client churn is header reordering only — matches sibling OpenAPI order. CI green across backend unit, hermetic, Swift, Windows/Linux smokes, OpenAPI.
On cubic
- P1 (quota counter / prompts-only bypass): not a regression introduced here — same
enforce_chat_quota+track_usage(APP_GENERATOR)pairing the other generators already use. If app-generator spend should incrementquota_questions, that is a shared follow-up across the router, not a blocker for wiring parity. - P2 (sync Firestore on event loop): same call pattern as the sibling generators; out of scope for this one-route fix.
Not merging from here.
|
The accuracy nit is right, and I have corrected the description in place rather than leaving the wrong claim standing above a correction. "The last ungated managed-spend route in this router" was wrong. It is the last ungated I enumerated every
Worth noting it is less protected than this route was, not equally: Filed as #12781, split into the wiring step (give it the rate limiter its siblings have — no policy decision needed) and the policy question you and cubic both raised, which is whether generator and persona spend should increment No code change here — the diff is unchanged and still the four lines. Only the description was inaccurate. |
Follow-up to #12739, which fixed #12715.
What changed and why
/v1/app/generate-promptsruns the same billableFeatures.APP_GENERATORspend as the other AI-app generators, but sat behindauth.with_rate_limit(...)alone with noenforce_chat_quotacall:track_usageattributes spend; it does not block. #12739 closed/v1/app/generateand/v1/app/generate-icon, and correctly left this one alone because it was outside #12715's scope. That makes it the last ungatedAPP_GENERATORroute in the router — a free user past the monthly cap can keep drawing LLM-generated app suggestions from it indefinitely.This adds the same gate the other four generators use, in the shape #12739 established:
enforce_chat_quota(uid, platform=x_app_platform)before any provider work, with theX-App-Platformheader accepted so the desktop trial paywall applies here too rather than silently never firing onplatform=None.Rate limiting and quota enforcement solve different problems, which is why one does not substitute for the other: the limiter bounds request frequency, the quota bounds monthly billable spend against the user's plan.
On the generated-client churn
The six generated artifacts change by a reordering, not an addition.
X-App-Platformwas already advertised on this route through the shared auth dependency; declaring it explicitly on the handler moves it ahead of the injected headers. That is exactly the ordering the sibling routes already have:So the route converges on the router's existing convention rather than introducing a new shape. No wire-level parameter is added or removed.
How it was verified
Mutation-checked — with the gate removed, both new tests fail:
pytest tests/unit/test_free_quota_gate_wiring.py— 16 passedscripts/openapi_runner.sh scripts/export_openapi.py --surface app-client --check— up to dateTests
test_generate_prompts_blocked_past_cap— asserts the 402 propagates and thatget_llmis never called, so the gate precedes spend rather than merely accompanying ittest_generate_prompts_proceeds_within_cap— asserts the proceed path still reaches the provider, and pinsenforce_chat_quota('u1', platform='macos')so the platform argument cannot be silently droppedHonest gaps
Failure-Class: none
Line-Count-Exception: backend/routers/apps.py | 2468 -> 2472 | Four lines: the X-App-Platform header parameter and the enforce_chat_quota call, matching the four sibling generators in this router. Splitting the file is a larger refactor than a one-gate wiring fix should carry.