← Back to Pull requests

feat(chat): add a per-minute message limit per plan #65

Closed
unknown closed this pull request without merging on Nov 27, 2026 12:50 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 09:12 UTC
Author

Problem


The chat route only enforced maxMessagesPerMonth. Nothing limited bursts, so one client or script could spend an owner's whole monthly quota, and the matching model spend, in seconds.

Change


  • PlanLimits gains maxMessagesPerMinute: free 10, pro 100, business 1000, matching the hard rule in AGENTS.md.

  • Before calling the gateway, the route takes a slot in a new chat_rate_reservations table (supabase/schema.sql, RLS on, service role only). It then counts the owner's slots in the last 60 s, up to and including its own.

  • - Over the cap: the slot is given back and the route returns 429 with Retry-After: 60.
    - Count query error: the slot is given back and the route returns 503, so an unknown count is never admitted.
    - In-flight requests hold capacity, so a concurrent burst can't overshoot. Because the count is in the database, the limit holds across serverless instances.
  • Slots are separate from analytics_events, which stays append-only (AGENTS.md rule 3). message_sent is still inserted only after a reply, so slots never count toward the monthly quota or usage pages. An abandoned slot just ages out of the window, and slots older than 10 minutes are pruned.

  • Deploy note: apply the new table from supabase/schema.sql before shipping this.
  • Tests


  • api-routes.test.ts:

  • - at the cap: 429 with Retry-After, and the gateway is never called;
    - 20 concurrent free-plan requests: exactly 10 reach the gateway, and only those write message_sent;
    - a failed count query: 503 and the slot is freed;
    - a failed reply records no usage, and stale slots are pruned.
  • billing-plans.test.ts is updated for the new field.
  • <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Changes the critical chat API path and billing enforcement; relies on a new DB table and correct deploy order, but fails closed on count errors and doesn't alter auth.

    > Overview

    Adds a per-minute burst cap on top of existing monthly chat quotas so owners can't burn an entire plan (and model spend) in seconds.

    > Plan limits now include maxMessagesPerMinute by tier (free 10, pro 100, business 1000).

    > The chat API reserves a slot in a new chat_rate_reservations table before calling the gateway, counts the owner's slots in a rolling 60s window (including in-flight requests), and rejects with 429 (Retry-After: 60) when over cap or 503 when the count can't be read—releasing the slot on rejection. Slots are separate from append-only analytics_events, so burst tracking doesn't affect monthly usage reporting. Background cleanup prunes rows older than 10 minutes without blocking admitted requests.

    > Deploy: apply the new table in supabase/schema.sql before shipping.

    > Unit tests cover concurrency (exactly N admitted), cleanup failures, count errors, and gateway failures without spurious analytics.

    > <sup>Reviewed by Cursor Bugbot for commit 28def6ffc9db45f5a476c07ec0b69381461e09fa. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 398 lines (+298 / -100) across 4 files Reviewer: mangeshraut712 Review duration: 3098m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 398 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.358
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #65"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.2% (MEDIUM) · Higher = larger blast radius
    43.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.1% · Safe to merge
    17.1%