-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(server): type the account rate-limit runtime payload #5473
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| * @module CodexAdapterLive | ||
| */ | ||
| import { | ||
| type AccountRateLimitsUpdatedPayload, | ||
| type CanonicalItemType, | ||
| type CanonicalRequestType, | ||
| type CodexSettings, | ||
|
|
@@ -436,6 +437,47 @@ function providerRefsFromEvent( | |
| return Object.keys(refs).length > 0 ? (refs as ProviderRuntimeEvent["providerRefs"]) : undefined; | ||
| } | ||
|
|
||
| function rateLimitWindow( | ||
| kind: string, | ||
| window: EffectCodexSchema.V2AccountRateLimitsUpdatedNotification__RateLimitWindow | null, | ||
| ): AccountRateLimitsUpdatedPayload["windows"][number] | undefined { | ||
| if (!window) { | ||
| return undefined; | ||
| } | ||
| return { | ||
| kind, | ||
| usedPercent: window.usedPercent, | ||
| ...(window.resetsAt != null ? { resetsAt: window.resetsAt } : {}), | ||
| ...(window.windowDurationMins != null ? { windowDurationMins: window.windowDurationMins } : {}), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes a Codex rate-limit snapshot onto the canonical payload. Codex | ||
| * reports two windows at once and signals exhaustion out of band, through | ||
| * `rateLimitReachedType` and `spendControlReached`, rather than as a status on | ||
| * the windows themselves. | ||
| * | ||
| * These notifications are sparse: an omitted field means "unknown", not | ||
| * "recovered" — the generated schema says as much on `spendControlReached`. | ||
| * So a status is claimed only on positive evidence of exhaustion, and left | ||
| * absent otherwise rather than asserting `allowed` the snapshot never stated. | ||
| */ | ||
| function rateLimitsPayloadFromNotification( | ||
| snapshot: EffectCodexSchema.V2AccountRateLimitsUpdatedNotification__RateLimitSnapshot, | ||
| ): AccountRateLimitsUpdatedPayload { | ||
| const windows = [ | ||
| rateLimitWindow("primary", snapshot.primary ?? null), | ||
| rateLimitWindow("secondary", snapshot.secondary ?? null), | ||
| ].filter((window) => window !== undefined); | ||
| const exhausted = snapshot.rateLimitReachedType != null || snapshot.spendControlReached === true; | ||
|
|
||
| return { | ||
| ...(exhausted ? { status: "rejected" as const } : {}), | ||
| windows, | ||
| }; | ||
|
cursor[bot] marked this conversation as resolved.
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function runtimeEventBase( | ||
| event: ProviderEvent, | ||
| canonicalThreadId: ThreadId, | ||
|
|
@@ -1393,16 +1435,18 @@ function mapToRuntimeEvents( | |
| } | ||
|
|
||
| if (event.method === "account/rateLimits/updated") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium The 🤖 Copy this AI Prompt to have your agent fix this: |
||
| if (!readPayload(EffectCodexSchema.V2AccountRateLimitsUpdatedNotification, event.payload)) { | ||
| const payload = readPayload( | ||
| EffectCodexSchema.V2AccountRateLimitsUpdatedNotification, | ||
| event.payload, | ||
| ); | ||
| if (!payload) { | ||
| return []; | ||
| } | ||
| return [ | ||
| { | ||
| type: "account.rate-limits.updated", | ||
| ...runtimeEventBase(event, canonicalThreadId), | ||
| payload: { | ||
| rateLimits: event.payload ?? {}, | ||
| }, | ||
| payload: rateLimitsPayloadFromNotification(payload.rateLimits), | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude utilization scale mismatch
High Severity
The
rateLimitsPayloadFromSdkfunction directly copies Claude'sSDKRateLimitInfo.utilization(a 0-1 fraction) toAccountRateLimitsUpdatedPayload.usedPercent, which expects a 0-100 percentage. This causes Claude rate limit utilization to be reported at 1/100th of its actual value.Reviewed by Cursor Bugbot for commit 60e720f. Configure here.