Skip to content

fix(ai): cap toUsageWindow remainingPercent at 100 (#1904) - #1905

Merged
dezren39 merged 1 commit into
mainfrom
creator/1904-tousagewindow-remaining-cap
Nov 7, 2026
Merged

dezren39 merged 1 commit into
mainfrom
creator/1904-tousagewindow-remaining-cap

Conversation

@dezren39

@dezren39 dezren39 commented Nov 7, 2026

Copy link
Copy Markdown

Fixes #1904.

toUsageWindow (packages/ai/src/quota/utils.ts) derived remainingPercent as Math.max(0, 100 - usedPercent) — floored at 0 but never capped at 100, so a negative usedPercent published a remainingPercent of 105/120, a percent that cannot exist, against window-contract rule 5 (derived fields agree with the published values).

One-line fix: cap the derived field on both sides — Math.min(100, Math.max(0, 100 - usedPercent)). usedPercent itself is published unchanged, so provider dispositions are preserved: turso's publish-truthfully over-100 windows (code#6227) still stand with remainingPercent: 0, exactly as before. The change is a no-op for every usedPercent >= 0.

Verification (laptop worktree of origin/main 2d11e0f):

  • Probe of the real toUsageWindow, pre-fix: -20 -> remaining 120, -5 -> 105, 150 -> 0. Post-fix: -20 -> 100, -5 -> 100, all other inputs unchanged.
  • bun test test/quota.test.ts (packages/ai): 115 pass / 0 fail, including 3 new pins for the derivation (the builder's first direct pins).
  • Negative control: with the utils.ts change stashed, exactly 1 test fails (the negative-usedPercent pin); the rest pass.
  • bun run typecheck in packages/ai: clean.

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.

fix(ai): shared quota toUsageWindow derives remainingPercent above 100% — the percent cap floors at 0 but never caps at 100

1 participant