Repository navigation
fix(ai): cap toUsageWindow remainingPercent at 100 (#1904) - #1905
Merged
Merged
Conversation
3 tasks
Closed
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1904.
toUsageWindow(packages/ai/src/quota/utils.ts) derivedremainingPercentasMath.max(0, 100 - usedPercent)— floored at 0 but never capped at 100, so a negativeusedPercentpublished aremainingPercentof 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)).usedPercentitself is published unchanged, so provider dispositions are preserved: turso's publish-truthfully over-100 windows (code#6227) still stand withremainingPercent: 0, exactly as before. The change is a no-op for everyusedPercent >= 0.Verification (laptop worktree of origin/main 2d11e0f):
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).bun run typecheckin packages/ai: clean.