fix: count daily quota against the org that owns the limit - #378
Merged
Merged
Conversation
Signed-off-by: Priyanshu-u07 <connect.priyanshu8271@gmail.com>
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.
The daily limit comes from an org-wide policy. The query says so:
but the Redis counters were keyed on
user_id, which isapikey:<id>for an API-key request. Each key got the whole org's allowance to itself, so an org with five keys could spend five times its limit.Both sides now resolve the same scope through one helper, and the key is built in one place because a limit counted against a different key than it is enforced on is not a limit.
increment_redis_onlygained adbparameter. It had no way to turn an API key into an org on its own, androuter.pyalready had a session. The alternative carrying the org from the resolved context would have meant a wire-format change across the inference boundary for a bug that lives inside the policy engine.Identities that are not API keys fall back to their own id. That is narrower than an org, so a sandbox token cannot spend someone else's quota.
On deploy
Counters under the old key shape are orphaned rather than migrated. They are daily keys and age out on their own, but every org starts that day from zero regardless of what it has already spent. Deliberate: migrating the would mean summing each org's keys at an arbitrary moment, and the result would be wrong in a different way.
Closes #347