Skip to content

feat(backend): plan-gate desktop Gemini generate for basic non-BYOK users (S14) - #12839

Merged
Git-on-my-level merged 1 commit into
mainfrom
feat/free-tier-s14-plan-gate
Sep 6, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
feat/free-tier-s14-plan-gate

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

S14 — server plan gate on desktop Gemini generate/stream

Shard S14 of the ratified local-models free-tier program (omi-knowledge-base/projects/local-models-free-tier/implementation/60-shards.md S14; spec 30-desktop-proactivity.md §3.2), scoped to the Gen-1 Gemini proxy only.

authorize_managed_compute has been on main since S1. POST /v1/proxy/gemini/** still spent Omi-billed Gemini for every basic non-BYOK desktop user (~37 generate calls per active hour). This PR closes that door at the route.

What changed

  • gemini_proxy and gemini_stream_proxy now call authorize_managed_compute on generateContent / streamGenerateContent before _proxy.
  • Feature is screen_frame_judge (the configured Gemini-provider key) so a request-scoped Gemini BYOK key counts; funding owner is byok when that key is present, otherwise omi.
  • Denied basic users get 402 with {'error': 'plan_gated', 'plan_type': 'basic', 'reason': ...}. 70-status 2026-09-04 settled this over the shard's original 403: these endpoints already return 402 for trial_expired, and chat already discriminates on a structured 402 body. A new 403 would make every shipped desktop build learn a second refusal code.
  • authorization_unavailable is 503, not a fall-through to a live key.
  • embedContent is not gated (TBD-4 / S24).
  • Inventory desktop_gemini_proxy.fallback_on_deny moves none_unwired → http_402.

Deliberately not this PR

desktop_proactivity.py completions are the JIT / context-bucket lane. S24 already warned that a blanket 403/402 on proactive_extraction kills the ambient nano triage, and test_legacy_clients_are_not_gated_by_jit_rollout exists because a previous 403 on that lane silently killed the shipped fleet. S14's remaining half waits on that lane's owner.

Automatic-or-dead check

test_desktop_proxy.py now drives the route functions (gemini_proxy / gemini_stream_proxy), not only _authorized_desktop_user:

  • basic + generateContent → 402 plan_gated, _proxy never runs
  • basic + streamGenerateContent → same
  • paid → _proxy runs
  • basic + Gemini BYOK → _proxy runs, authorize sees funding_owner=byok
  • embedContent → authorize is not consulted
  • authorization_unavailable → 503, _proxy never runs

Red-proof (in the generate test docstring): drop the await _enforce_managed_plan_gate in gemini_proxy and the test fails because it would reach _proxy.

Proof

  • tests/unit/test_desktop_proxy.py 106 passed (2m31s), including the six new route-level cases and the existing paywall test.
  • test_inventory_surfaces_have_status_guardrails_and_resolvable_code_paths passed after the inventory update.

Live dest check after merge: a basic dogfood account hitting POST /v1/proxy/gemini/models/gemini-2.5-flash:generateContent must 402 plan_gated on the auto-deployed dev revision; a paid/BYOK account must still 200.

Cut list

  • Gating desktop_proactivity completions (JIT lane; S24 / other agent).
  • Pixel-vs-text discrimination inside generateContent (S24).
  • embedContent / screen-vector 403 (TBD-4 / S24).
  • Client .planGated (S13).
  • Paid-tier ceilings.

Line-count ratchet

Line-Count-Exception: backend/routers/desktop_proxy.py | 1632 -> 1673 | S14 route-level plan gate on generate/stream; the check lives next to the existing trial paywall, not in a new module

Product invariants

none

Failure class

Failure-Class: none

Review in cubic

…sers

Free-tier S14: stop unpaid Gen-1 screen-intelligence spend at the proxy
route. Completions stay open so the JIT ambient lane is not killed.

Co-authored-by: Cursor <[email protected]>

@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 3 files

Confidence score: 3/5

  • In backend/routers/desktop_proxy.py, the plan-gated request returns the gate dictionary nested under FastAPI’s detail field instead of the documented wire body, which can break clients that parse the response schema; return a JSONResponse containing the gate dictionary directly or add a route-specific exception handler.
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/desktop_proxy.py">

<violation number="1" location="backend/routers/desktop_proxy.py:1661">
P2: The plan-gated request does not return the documented wire body: FastAPI wraps `_plan_gate_detail(decision)` inside `detail`. Return a `JSONResponse` with the gate dictionary (or add a route-specific exception handler) so clients receive the promised top-level fields.</violation>
</file>

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

Re-trigger cubic

return
if decision.reason == 'authorization_unavailable':
raise HTTPException(status_code=503, detail='plan authorization is temporarily unavailable')
raise HTTPException(status_code=402, detail=_plan_gate_detail(decision))

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 plan-gated request does not return the documented wire body: FastAPI wraps _plan_gate_detail(decision) inside detail. Return a JSONResponse with the gate dictionary (or add a route-specific exception handler) so clients receive the promised top-level fields.

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

<comment>The plan-gated request does not return the documented wire body: FastAPI wraps `_plan_gate_detail(decision)` inside `detail`. Return a `JSONResponse` with the gate dictionary (or add a route-specific exception handler) so clients receive the promised top-level fields.</comment>

<file context>
@@ -1622,11 +1623,51 @@ async def _authorized_desktop_user(uid: str = Depends(get_current_user_uid)) ->
+        return
+    if decision.reason == 'authorization_unavailable':
+        raise HTTPException(status_code=503, detail='plan authorization is temporarily unavailable')
+    raise HTTPException(status_code=402, detail=_plan_gate_detail(decision))
+
+
</file context>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Settled, not a defect. FastAPI HTTPException(detail=dict) is the existing 402 wire on these endpoints and on chat ({"detail":{"error":"quota_exceeded","plan_type":"basic"}}). The 2026-09-04 free-tier note chose that shape on purpose so shipped desktop builds keep discriminating on a structured 402 body instead of learning a second status or a new top-level schema.

JSONResponse with top-level fields would break that contract. Tests pin exc.detail["error"] == "plan_gated", matching chat's handler that reads exc.detail.get("error").

@Git-on-my-level

Copy link
Copy Markdown
Member Author

CI is green on bd3578caa9 (30 pass / 11 skip / 0 fail). Review still required and kodjima33 has not returned; merging via admin override the same way S0/S4 did when the only remaining gate was approval. Cubic's wire-body note is settled: structured 402 detail matches chat and the 2026-09-04 free-tier decision.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

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.

1 participant