fix(credits): apply a due monthly refill at the gate before denying

Merged
SW
3mo ago
+83-8

Implements recoupable/chat#1962 — the sole row in that issue's PR matrix.

The credit gate read the raw credits_usage balance, so the first credit-gated request after a monthly refill boundary saw the stale pre-refill balance and 402d spuriously until something called GET /api/accounts/{id}/credits, the only place the refill ran.

What changes

checkCreditsAvailable keeps the single-read happy path, and on a shortfall runs checkAndResetCredits — the same refill the balance GET applies — then re-evaluates with the refreshed balance:

  • Sufficient raw balance → available, one DB read, checkAndResetCredits never called. Safe to skip because the refill is a floor (api#837): it can only raise a balance, so it cannot turn a pass into a fail, and can wait for the next balance read.
  • Shortfall + refill due → the refill applies (floor semantics, timestamp advances) and the request passes if the refilled balance covers the cost. This is the bug case.
  • Shortfall + refill applied but still short, or no refill due → 402 with the refreshed balance in remaining_credits.

All eight credit-gated call sites go through ensureCreditsOrShortCircuit, whose contract is unchanged — no call-site edits.

Verification

TDD, red before green: added a describe("a due-but-unapplied monthly refill") block with 4 cases (applied-on-shortfall passes, still-short 402s with the refreshed balance, happy path never consults the refill, missing row → zero). Ran it: RED, 2 failed (the two behavior-changing cases). Implemented: GREEN.

CheckResult
vitest run lib/credits15 files / 95 tests passed
eslint lib/creditsclean
tsc --noEmit202 errors, identical to the pre-existing baseline, zero in touched files

Not yet done: the issue's live Done-when (seed a spent balance + month-old timestamp, confirm the first credit-gated call passes with no prior GET /credits, and the timestamp advances) against the preview. Flagging rather than implying it.


Summary by cubic

Applies a due monthly credits refill during gating before denying, preventing false 402s right after a refill boundary. Previously the gate read the raw credits_usage balance; the first gated call after the boundary could 402 until GET /api/accounts/{id}/credits applied the refill.

  • On shortfall, checkCreditsAvailable now calls checkAndResetCredits(accountId) and re-evaluates; it passes if the refilled balance covers the cost, otherwise 402s with the refreshed remainingCredits.
  • Happy path unchanged: sufficient balance returns available with a single DB read; the refill is skipped since it only raises the balance.
  • Contract and call sites unchanged; no changes to ensureCreditsOrShortCircuit.
  • Tests cover applied-on-shortfall pass, still-short denial with refreshed balance, single-read happy path, and missing-row treated as zero.

Summary by CodeRabbit

  • Bug Fixes
    • Monthly credits now reset when due before insufficient-credit status is reported.
    • Credit availability checks now reflect the refreshed balance after a reset.