GeezerVison is showing github.com — Merge pull request #991 from developing-today/quota-auto-disable · developing-today-forks/opencode@195bdae
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/model-route-limits.ts +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,14 +115,23 @@ export const ABSOLUTE_TIME = | 115 | 115 | /(?:retry|try again|wait|back|resume|reset|returns?|available|until|expires?(?: at)?)[-a-z ]{0,20}(?:after|at|on|by)?\s*(\d{4}-\d{2}-\d{2}(?:[t ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:z|[+-]\d{2}:?\d{2})?)?)/ | 116 | 116 |
| 117 | 117 | // A relative wait is one amount+unit part or a compound sequence of them ("in 1 hour 30 | 118 | | -// minutes"), and an amount may be decimal ("in 1.5 hours") — providers phrase resets both ways. | 119 | | -const RELATIVE_PART = String.raw`\d+(?:\.\d+)?\s*(?:second|minute|hour|day|sec|s)s?(?![a-z])` | | 118 | +// minutes"), and an amount may be decimal ("in 1.5 hours") — providers phrase resets both | | 119 | +// ways, and some use compact units ("resets in 6d 13h"). The letter units are safe here: | | 120 | +// every part must start with an amount and the (?![a-z]) guard keeps a unit from matching | | 121 | +// the head of a following word ("in 5 deals" states no wait). | | 122 | +const RELATIVE_PART = String.raw`\d+(?:\.\d+)?\s*(?:second|minute|hour|day|sec|h|d|s)s?(?![a-z])` | 120 | 123 | const RELATIVE_TIME = new RegExp(String.raw`in\s+(${RELATIVE_PART}(?:[\s,]+(?:and\s+)?${RELATIVE_PART})*)`) | 121 | | -const RELATIVE_PARTS = /(\d+(?:\.\d+)?)\s*(second|minute|hour|day|sec|s)s?(?![a-z])/g | 122 | | -const RELATIVE_UNITS = { second: 1_000, sec: 1_000, s: 1_000, minute: 60_000, hour: 3_600_000, day: 86_400_000 } as Record< | 123 | | - string, | 124 | | - number | 125 | | -> | | 124 | +const RELATIVE_PARTS = /(\d+(?:\.\d+)?)\s*(second|minute|hour|day|sec|h|d|s)s?(?![a-z])/g | | 125 | +const RELATIVE_UNITS = { | | 126 | + second: 1_000, | | 127 | + sec: 1_000, | | 128 | + s: 1_000, | | 129 | + minute: 60_000, | | 130 | + hour: 3_600_000, | | 131 | + h: 3_600_000, | | 132 | + day: 86_400_000, | | 133 | + d: 86_400_000, | | 134 | +} as Record<string, number> | 126 | 135 |
| 127 | 136 | /** The total wait a relative phrase states, summed over its parts; undefined when it states none. */ | 128 | 137 | const relativeMs = (message: string): number | undefined => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/model-route.ts +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -511,6 +511,58 @@ export const observeCredentialQuota = ( | 511 | 511 | if (!full && previous?.gated) logHealth(target, "quota-readmitted", `credential:${credentialID}`) | 512 | 512 | } | 513 | 513 |
| | 514 | +/** The longest a single exhaustion reading may hold a gate: a stated reset further out than | | 515 | + * this is capped rather than believed (mirrors ModelRouteTuning.MAX_QUOTA_RESET_MS). */ | | 516 | +const QUOTA_GATE_MAX_MS = 30 * 86_400_000 | | 517 | +/** The hold when an exhaustion error states no usable reset: long enough to stop a 429 on | | 518 | + * every turn, short enough that a recovered pool is retried promptly (mirrors the tuning | | 519 | + * review's default hold). */ | | 520 | +const QUOTA_GATE_FALLBACK_MS = 30 * 60_000 | | 521 | + | | 522 | +/** | | 523 | + * The reactive half of #984: one attempt's quota-exhaustion answer gates its pool until the | | 524 | + * reset the provider stated, or the bounded fallback hold when it stated none, so selection | | 525 | + * stops spending attempts on a pool that has already answered 100%. A credentialed attempt | | 526 | + * gates only its credential — sibling credentials behind the same provider keep serving. | | 527 | + * A credential-less provider is gated by a skip adjustment on the provider itself, which | | 528 | + * selection honours as last-resort: a route with no other door still tries it rather than | | 529 | + * idling. Re-admission needs no second producer: the gate re-derives from the stated reset | | 530 | + * at every check, and a fresher below-limit snapshot replaces the synthesized one. | | 531 | + */ | | 532 | +export const observeQuotaFailure = ( | | 533 | + target: Model.Ref, | | 534 | + credentialID: string | undefined, | | 535 | + resetAt: number | undefined, | | 536 | + now = Date.now(), | | 537 | +) => { | | 538 | + const until = Math.min(resetAt ?? now + QUOTA_GATE_FALLBACK_MS, now + QUOTA_GATE_MAX_MS) | | 539 | + if (credentialID) { | | 540 | + observeCredentialQuota( | | 541 | + target, | | 542 | + credentialID, | | 543 | + { | | 544 | + planID: `attempt:${target.providerID}`, | | 545 | + window: "exhaustion", | | 546 | + providerID: target.providerID, | | 547 | + percent: 100, | | 548 | + remaining: 0, | | 549 | + resetAt: until, | | 550 | + observedAt: now, | | 551 | + stale: false, | | 552 | + }, | | 553 | + now, | | 554 | + ) | | 555 | + return | | 556 | + } | | 557 | + addAdjustment({ | | 558 | + id: `quota:${target.providerID}`, | | 559 | + match: `${target.providerID}/`, | | 560 | + action: "skip", | | 561 | + until, | | 562 | + note: "quota exhausted (attempt-reported); held until the stated reset", | | 563 | + }) | | 564 | +} | | 565 | + | 514 | 566 | /** The gate on one credential at `now`: `{ until }` (the snapshot's reset, null when it gave | 515 | 567 | * none) while a known-full snapshot holds it out of routes, undefined while it is eligible. | 516 | 568 | * Passing the reset re-admits and logs it, like a cooldown expiring. */ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/runner/step.ts +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -432,6 +432,27 @@ export const make = Effect.gen(function* () { | 432 | 432 | Date.now(), | 433 | 433 | input.model.routing?.credentialID, | 434 | 434 | ) | | 435 | + // A quota-exhaustion answer gates its pool until the reset it stated (#984): the | | 436 | + // credential when the attempt carried one, the provider itself otherwise. Selection | | 437 | + // then stops spending attempts — and collecting 429s — on an exhausted pool. The | | 438 | + // text check catches providers that report exhaustion without a quota tag | | 439 | + // (commandcode's "insufficient credits" arrives as an unknown-provider error). | | 440 | + const exhaustionText = /insufficient credits|quota exhausted|usage limit exceeded|reached your .* limit/i | | 441 | + if ( | | 442 | + failed && | | 443 | + (isQuotaFailure(llmFailure) || | | 444 | + exhaustionText.test( | | 445 | + `${(llmFailure.reason as { message?: string }).message ?? ""} ${(llmFailure.reason as { body?: string }).body ?? ""}`, | | 446 | + )) | | 447 | + ) { | | 448 | + const hint = failureHint(llmFailure) | | 449 | + ModelRoute.observeQuotaFailure( | | 450 | + input.model.ref, | | 451 | + input.model.routing?.credentialID, | | 452 | + hint.retryAfterMs === undefined ? undefined : Date.now() + hint.retryAfterMs, | | 453 | + Date.now(), | | 454 | + ) | | 455 | + } | 435 | 456 | // A success that advertised no limit headers replaces the serving credential's hold: | 436 | 457 | // the provider's last word for that credential is now a healthy response (#870). Only | 437 | 458 | // a successful stream counts: an interrupted one never got the provider's answer, so | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/model-route-credential.test.ts +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -207,3 +207,31 @@ describe("ModelRoute credential quota gate (#6)", () => { | 207 | 207 | expect(ModelRoute.credentialQuotaBlocked("cred-lab", 20_000)).toEqual({ until: 100_000 }) | 208 | 208 | }) | 209 | 209 | }) | | 210 | + | | 211 | +describe("ModelRoute quota-failure gate (#984)", () => { | | 212 | + test("a credentialed exhaustion gates only that credential until the stated reset", () => { | | 213 | + ModelRoute.observeQuotaFailure(target, "cred-lab", 100_000, 10_000) | | 214 | + expect(ModelRoute.credentialQuotaBlocked("cred-lab", 10_000)).toEqual({ until: 100_000 }) | | 215 | + expect(ModelRoute.credentialQuotaBlocked("cred-lab", 99_999)).toEqual({ until: 100_000 }) | | 216 | + expect(ModelRoute.credentialQuotaBlocked("cred-lab", 100_000)).toBeUndefined() | | 217 | + expect(ModelRoute.credentialQuotaBlocked("cred-personal", 10_000)).toBeUndefined() | | 218 | + }) | | 219 | + | | 220 | + test("no stated reset holds for the bounded fallback, and an absurd one is capped", () => { | | 221 | + ModelRoute.observeQuotaFailure(target, "cred-lab", undefined, 10_000) | | 222 | + expect(ModelRoute.credentialQuotaBlocked("cred-lab", 10_000)).toEqual({ until: 10_000 + 30 * 60_000 }) | | 223 | + | | 224 | + ModelRoute.resetHealth() | | 225 | + ModelRoute.observeQuotaFailure(target, "cred-lab", 10_000 + 90 * 86_400_000, 10_000) | | 226 | + expect(ModelRoute.credentialQuotaBlocked("cred-lab", 10_000)).toEqual({ until: 10_000 + 30 * 86_400_000 }) | | 227 | + }) | | 228 | + | | 229 | + test("a credential-less exhaustion skips the provider until the reset, then lapses", () => { | | 230 | + const plain = ModelRoute.ref({ providerID: "cline-pass", model: "claude-sonnet-5" }) | | 231 | + ModelRoute.observeQuotaFailure(plain, undefined, 100_000, 10_000) | | 232 | + const active = ModelRoute.activeAdjustments(10_000) | | 233 | + expect(active).toHaveLength(1) | | 234 | + expect(active[0]).toMatchObject({ id: "quota:cline-pass", match: "cline-pass/", action: "skip", until: 100_000 }) | | 235 | + expect(ModelRoute.activeAdjustments(100_000)).toHaveLength(0) | | 236 | + }) | | 237 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/model-route-limits.test.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -249,6 +249,17 @@ describe("ModelRouteLimits.retryHintFromMessage", () => { | 249 | 249 | expect(ModelRouteLimits.retryHintFromMessage("try again in 2.5 minutes", at)).toBe(150_000) | 250 | 250 | }) | 251 | 251 |
| | 252 | + test("reads compact relative units (resets in 6d 13h)", () => { | | 253 | + const total = 6 * 86_400_000 + 13 * 3_600_000 | | 254 | + expect(ModelRouteLimits.retryHintFromMessage("your quota resets in 6d 13h", at)).toBe(total) | | 255 | + expect(ModelRouteLimits.resetAtFromMessage("your quota resets in 6d 13h", at)).toBe(at + total) | | 256 | + expect(ModelRouteLimits.resetAtFromMessage("resets in 2d", at)).toBe(at + 2 * 86_400_000) | | 257 | + expect(ModelRouteLimits.resetAtFromMessage("resets in 3h", at)).toBe(at + 3 * 3_600_000) | | 258 | + // The letter units must not eat the head of a word: these state no wait at all. | | 259 | + expect(ModelRouteLimits.resetAtFromMessage("come back in 5 deals", at)).toBeUndefined() | | 260 | + expect(ModelRouteLimits.resetAtFromMessage("come back in 2 homes", at)).toBeUndefined() | | 261 | + }) | | 262 | + | 252 | 263 | test("reads every absolute-time keyword parseQuotaReset reads (paired with model-route-tuning)", () => { | 253 | 264 | const instant = Date.parse("2026-01-02T00:00:00Z") | 254 | 265 | for (const message of [ | |
|