Repository navigation
feat(routing): add quota-aware policy scoring #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b562fdb
feat(routing): add quota-aware policy scoring (RI-07)
Wibias 288dd8a
fix(routing): address full-review findings and approved simplify (RI-07)
Wibias e74a211
docs(routing): document runtime unknown quota; add review regression …
Wibias 7aebbd3
test(routing): update score totals for quota-weighted composite (RI-07)
Wibias 480f157
fix(routing): address review threads on quota evidence (RI-07)
Wibias 850c625
fix(routing): address CodeRabbit review on quota evidence (RI-07)
Wibias 3fd1a00
docs(routing): drop cost weight from profile scoring docs (RI-07)
Wibias f1ec2d6
docs(routing): document actual optimize scoring semantics (RI-07)
Wibias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /** | ||
| * Quota-aware policy scoring (RI-07). | ||
| * | ||
| * Evidence reuses the existing privacy-safe quota sources: the Codex pool | ||
| * account quota cache (usage percents only - no emails, tokens, or raw | ||
| * responses) and the provider per-account quota cache (Anthropic). Unknown | ||
| * quota stays unknown; no raw quota response ever reaches a trace. | ||
| * | ||
| * Boundary: policy profiles select provider/model TARGETS. Exact account | ||
| * selectors and Codex pool strategies remain authoritative inside their | ||
| * existing scope; account-level quota evidence is consumed when a candidate | ||
| * carries an account reference (dry-run/evaluate), never invented. | ||
| */ | ||
|
|
||
| import { getAccountQuota, isCodexQuotaExhausted } from "../codex/quota"; | ||
| import { getCachedProviderAccountQuota } from "../providers/quota"; | ||
| import type { RouteQuotaEvidence } from "./trace"; | ||
|
|
||
| export interface QuotaEvidenceInput { | ||
| provider: string; | ||
| model: string; | ||
| /** Opaque account reference for per-account quota sources. */ | ||
| accountRef?: string; | ||
| /** Codex pool account id (provider "openai"). */ | ||
| codexAccountId?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Assemble quota evidence from canonical local caches only (no network). | ||
| * Unknown dimensions stay unknown - never zero. | ||
| */ | ||
| export function quotaEvidenceForCandidate(input: QuotaEvidenceInput): RouteQuotaEvidence { | ||
| if (input.provider === "openai" && input.codexAccountId) { | ||
| const quota = getAccountQuota(input.codexAccountId); | ||
| if (quota) { | ||
| const percents = [quota.weeklyPercent, quota.monthlyPercent] | ||
| .filter((value): value is number => typeof value === "number" && Number.isFinite(value)); | ||
| const maxPercent = percents.length > 0 ? Math.max(...percents) : undefined; | ||
| const resets = [quota.weeklyResetAt, quota.monthlyResetAt] | ||
| .filter((value): value is number => typeof value === "number" && Number.isFinite(value)) | ||
| .filter(value => value > Date.now()); | ||
| return { | ||
| known: true, | ||
| ...(maxPercent !== undefined | ||
| ? { headroom: Math.max(0, Math.min(1, 1 - maxPercent / 100)) } | ||
| : {}), | ||
| exhausted: isCodexQuotaExhausted(quota), | ||
| ...(resets.length > 0 ? { resetAtMs: Math.min(...resets) } : {}), | ||
| source: "codex-pool", | ||
|
Wibias marked this conversation as resolved.
|
||
| }; | ||
| } | ||
| } | ||
|
|
||
| if (input.provider === "anthropic" && input.accountRef) { | ||
| const quota = getCachedProviderAccountQuota("anthropic", input.accountRef); | ||
| if (quota) { | ||
| // Anthropic per-family buckets (e.g. Opus / Sonnet) are stricter than the | ||
| // broad account windows: fold the candidate model's matching bucket into | ||
| // headroom and exhaustion so a model-specific overage is not hidden by | ||
| // a healthy aggregate window. | ||
| const family = anthropicFamilyWindow(input.model, quota.customWindows ?? []); | ||
| const percents = [quota.fiveHourPercent, quota.weeklyPercent, quota.monthlyPercent, family?.percent] | ||
| .filter((value): value is number => typeof value === "number" && Number.isFinite(value)); | ||
| const maxPercent = percents.length > 0 ? Math.max(...percents) : undefined; | ||
| const resets = [quota.fiveHourResetAt, quota.weeklyResetAt, quota.monthlyResetAt, family?.resetAt] | ||
| .filter((value): value is number => typeof value === "number" && Number.isFinite(value)) | ||
| .filter(value => value > Date.now()); | ||
| return { | ||
| known: true, | ||
| ...(maxPercent !== undefined | ||
| ? { headroom: Math.max(0, Math.min(1, 1 - maxPercent / 100)) } | ||
| : {}), | ||
| exhausted: maxPercent !== undefined && maxPercent >= 100, | ||
| ...(resets.length > 0 ? { resetAtMs: Math.min(...resets) } : {}), | ||
| source: "provider-report", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { known: false }; | ||
| } | ||
|
|
||
| /** | ||
| * Match the candidate model to an Anthropic per-family quota window. Window | ||
| * labels from the provider probe are "Opus" / "Sonnet"; model ids carry the | ||
| * family as a segment (e.g. `claude-opus-...`, `claude-sonnet-...`). Returns | ||
| * undefined when no family window is cached or no label matches. | ||
| */ | ||
| function anthropicFamilyWindow( | ||
| model: string, | ||
| windows: Array<{ label: string; percent?: number; resetAt?: number }>, | ||
| ): { percent?: number; resetAt?: number } | undefined { | ||
| const normalized = model.toLowerCase(); | ||
| for (const window of windows) { | ||
| const family = window.label.trim().toLowerCase(); | ||
| if (family && normalized.includes(family)) { | ||
| return window; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Deterministic quota score in [0,1]: larger available headroom scores | ||
| * higher; exhausted evidence scores 0. Unknown evidence returns null so the | ||
| * caller can apply the profile's unknownEvidence policy. | ||
| */ | ||
| export function quotaScore(evidence: RouteQuotaEvidence | undefined): number | null { | ||
| if (!evidence || !evidence.known) return null; | ||
| if (evidence.exhausted === true) return 0; | ||
| if (typeof evidence.headroom === "number") return Math.max(0, Math.min(1, evidence.headroom)); | ||
| return null; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.