← Back to Pull requests

Distributed brief lock, Redis-native rate limits, health endpoint #5

Closed
unknown closed this pull request without merging on Nov 22, 2026 19:14 UTC
View on GitHub
unknown unknown commented on Nov 22, 2026 19:11 UTC
Author

Why


  • Silent fallback. Shared rate limiting was deployed, but the limiter had only ever been tested with fakes. Run across separate processes against a real Redis, every process fell back to its own local limit, so 8 of 8 requests got through instead of 5. The cause: @upstash/ratelimit 2.2.0 uses an Upstash-only Lua flag (allow-key-locking) that standard Redis rejects, so its behavior can't be verified outside Upstash.

  • Duplicate writes. Two instances could each write the same brief.
  • Changes


  • Rate limits. One atomic sliding-window Lua script using only plain Redis commands and the Redis server's clock. Same limits as before: 5 an hour per caller, 60 an hour overall, plus search and usage events. @upstash/ratelimit is removed.

  • Brief lock (lib/lock.ts):

  • - SET NX PX with an owner token, released by a compare-and-delete script so a stale worker can't release someone else's lock. Expires after 90 s.
    - Other instances wait for the saved brief, up to 40 s, then get a clear 409.
    - A failed generation releases the lock. A crashed holder's lock expires.
    - The holder checks for a saved brief after taking the lock, so it never pays twice.
  • /api/health.

  • - Reports whether limits are global or per instance, Redis reachability and latency, budget use against the cap, and whether a model key is configured.
    - No secret values. A degraded list names any fallback.
    - The smoke test checks it, and EXPECT_GLOBAL_LIMITS=1 makes the monitor fail if limits aren't global.
  • CI. A new distributed job runs the cross-process tests against Redis plus the serverless-redis-http emulator as service containers.
  • Verification


  • 211 unit tests pass.

  • Cross-process tests against real Redis, where each worker is a separate Node process:

  • - 8 instances, one caller: exactly 5 allowed
    - 80 different callers across 4 instances: exactly 60 allowed
    - 10 instances racing a budget of 3: exactly 3 reserved
    - 6 instances asking for the same brief: 1 model call, and all 6 get the brief
  • Health checked locally with Redis up (global, 20 ms) and with Redis unreachable (per-instance, plus the reason).

  • Not deployed. Production still has no Redis variables.
  • 🤖 Generated with Claude Code

    Diff: 286 lines (+214 / -72) across 3 files Reviewer: cursor[bot] Review duration: 3m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.40) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 286 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 55% · Reviewer familiarity: 35%
    44%
    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.339
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #5"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.9% 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: 39.8% (MEDIUM) · Higher = larger blast radius
    39.8%
    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: 44.9% · Lower = rushed or shallow review
    44.9%
    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: 21.9% · Safe to merge
    21.9%