GeezerVison is showing github.com — Merge pull request #1466 from developing-today/queue/6641-1389-pool-q… · developing-today-forks/opencode@8c81dee
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-resolver.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -739,7 +739,7 @@ export const layer = Layer.effect( | 739 | 739 | const unavailable = new Map<number, string>() | 740 | 740 | const overLimit = new Set<number>() | 741 | 741 | // Pool-shaped pull-out (#1014 PR3): a leaf whose nested group (pool) has had two | 742 | | - // distinct leaves answer quota in turn is out with its pool, derived from the | | 742 | + // distinct quota evidence sources answer quota in turn is out with its pool, derived from the | 743 | 743 | // leaves' live quota benches/gates at this moment. | 744 | 744 | const poolExhausted = new Map<number, boolean>() | 745 | 745 | for (const index of indexes) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/model-route.ts +40-22Lines changed: 40 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -559,8 +559,8 @@ export const activeBenches = (now = Date.now()): ActiveBench[] => { | 559 | 559 | return benches.toSorted((left, right) => left.until - right.until) | 560 | 560 | } | 561 | 561 |
| 562 | | -/** How many distinct leaves of one pool must be quota-out before the pool itself is pulled: | 563 | | - * one leaf's quota answer can be leaf-specific, but a second distinct leaf of the same pool | | 562 | +/** How many distinct quota evidence sources of one pool must be out before the pool itself is pulled: | | 563 | + * one leaf's quota answer can be leaf-specific, but a second distinct evidence source of the same pool | 564 | 564 | * answering quota in turn is the pool-shaped signal (#1014 PR3). */ | 565 | 565 | const POOL_QUOTA_LEAVES = 2 | 566 | 566 |
| @@ -569,25 +569,37 @@ const quotaBench = (state: HealthState | undefined, now: number) => | 569 | 569 | state.cooldownUntil > now && | 570 | 570 | (state.benchReason === "quota" || state.benchReason === "quota retry-after") | 571 | 571 |
| 572 | | -/** Whether one leaf is currently out for a quota-class reason: a quota bench on the leaf (or | 573 | | - * on the credential that serves it), a quota gate on its credential, or a quota gate on its | 574 | | - * provider (a known-full snapshot, a balance hold, or the reactive attempt-reported skip). | 575 | | - * Balance holds count until a fresh balance increase releases them, so a pool held by a | 576 | | - * balance gate stays out with no timer-based re-admission (#1014). */ | 577 | | -const quotaOut = (target: Model.Ref, credentialID: string | undefined, now: number, location?: string): boolean => { | | 572 | +/** One leaf's quota-out evidence, or undefined when the leaf is not quota-out: a quota | | 573 | + * bench on the leaf (or on the credential that serves it) counts per leaf, a quota gate | | 574 | + * on the credential counts once per credential, and a quota gate on the provider (a | | 575 | + * known-full snapshot, a balance hold, or the reactive attempt-reported skip) counts | | 576 | + * once per provider. Bench evidence takes priority over a shared gate on the same | | 577 | + * leaf, so two genuinely independent benches under one gate still count separately. | | 578 | + * Balance holds count until a fresh balance increase releases them, so a pool held by | | 579 | + * a balance gate stays out with no timer-based re-admission (#1014). */ | | 580 | +const quotaEvidence = ( | | 581 | + target: Model.Ref, | | 582 | + credentialID: string | undefined, | | 583 | + now: number, | | 584 | + location?: string, | | 585 | +): string | undefined => { | 578 | 586 | if (credentialID) { | 579 | | - if (quotaBench(credentialHealth.get(credentialKey(target, credentialID)), now)) return true | 580 | | - return credentialQuotaBlocked(credentialID, now, location) !== undefined | | 587 | + if (quotaBench(credentialHealth.get(credentialKey(target, credentialID)), now)) | | 588 | + return `bench:${credentialKey(target, credentialID)}` | | 589 | + if (credentialQuotaBlocked(credentialID, now, location) !== undefined) return `cred-gate:${credentialID}` | | 590 | + return undefined | 581 | 591 | } | 582 | | - if (quotaBench(health.get(key(target)), now)) return true | | 592 | + if (quotaBench(health.get(key(target)), now)) return `bench:${key(target)}` | 583 | 593 | const provider = providerQuota.get(target.providerID) | 584 | 594 | if ( | 585 | 595 | provider && | 586 | 596 | (provider.balanceHold !== undefined || | 587 | 597 | QuotaSnapshot.isKnownFull(provider.snapshot, now, currentConstants(location).quotaGate.balanceDeadThreshold)) | 588 | 598 | ) | 589 | | - return true | 590 | | - return activeAdjustments(now).some((item) => item.id === `quota:${target.providerID}` && item.action === "skip") | | 599 | + return `prov:${target.providerID}` | | 600 | + if (activeAdjustments(now).some((item) => item.id === `quota:${target.providerID}` && item.action === "skip")) | | 601 | + return `prov:${target.providerID}` | | 602 | + return undefined | 591 | 603 | } | 592 | 604 |
| 593 | 605 | interface PoolMembership { | @@ -629,12 +641,16 @@ const poolOf = (definition: Definition, index: number): PoolMembership | undefin | 629 | 641 | /** The id of the pool (nested group) a leaf belongs to, or undefined when it sits on the root. */ | 630 | 642 | export const poolIDOf = (definition: Definition, index: number) => poolOf(definition, index)?.id | 631 | 643 |
| 632 | | -/** Whether a leaf's pool is quota-exhausted: at least two distinct leaves of the pool are | 633 | | - * currently quota-out, so the pool is pulled as a whole instead of spending attempts on its | 634 | | - * remaining leaves one by one (#1014 PR3 dynamic pull-out). The verdict is derived from the | 635 | | - * leaves' live quota state, so it lifts by itself when the benches reach their stated reset, | 636 | | - * when a balance hold is released by a fresh increase, or when a benched leaf recovers — a | 637 | | - * recovered pool re-enters rotation with no second producer. */ | | 644 | +/** Whether a leaf's pool is quota-exhausted: at least two distinct quota evidence | | 645 | + * sources across the pool's leaves are currently out, so the pool is pulled as a whole | | 646 | + * instead of spending attempts on its remaining leaves one by one (#1014 PR3 dynamic | | 647 | + * pull-out). Evidence is deduplicated by identity — two leaves out only through one | | 648 | + * shared credential or provider gate count as one signal, so a single provider's quota | | 649 | + * wall cannot hard-exclude a healthy leaf from another provider (#1389) — while a | | 650 | + * leaf's own quota bench always counts per leaf. The verdict is derived from the | | 651 | + * leaves' live quota state, so it lifts by itself when the benches reach their stated | | 652 | + * reset, when a balance hold is released by a fresh increase, or when a benched leaf | | 653 | + * recovers — a recovered pool re-enters rotation with no second producer. */ | 638 | 654 | export const poolQuotaExhausted = ( | 639 | 655 | definition: Definition, | 640 | 656 | index: number, | @@ -644,12 +660,14 @@ export const poolQuotaExhausted = ( | 644 | 660 | ): boolean => { | 645 | 661 | const pool = poolOf(definition, index) | 646 | 662 | if (!pool || pool.leaves.length < POOL_QUOTA_LEAVES) return false | 647 | | - const out = new Set<number>() | | 663 | + const evidence = new Set<string>() | 648 | 664 | for (const leaf of pool.leaves) { | 649 | 665 | const target = definition.targets[leaf] | 650 | | - if (target && quotaOut(target, credentialOf?.(leaf), now, location)) out.add(leaf) | | 666 | + if (!target) continue | | 667 | + const source = quotaEvidence(target, credentialOf?.(leaf), now, location) | | 668 | + if (source) evidence.add(source) | 651 | 669 | } | 652 | | - return out.size >= POOL_QUOTA_LEAVES | | 670 | + return evidence.size >= POOL_QUOTA_LEAVES | 653 | 671 | } | 654 | 672 |
| 655 | 673 | export const failed = ( | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/model-route.test.ts +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2226,4 +2226,50 @@ describe("ModelRoute pool-shaped quota pull-out (#1014 PR3)", () => { | 2226 | 2226 | expect(ModelRoute.poolQuotaExhausted(definition, 2, resetAt + 1)).toBe(false) | 2227 | 2227 | ModelRoute.resetHealth() | 2228 | 2228 | }) | | 2229 | + | | 2230 | + test("one shared credential or provider gate counts once, so a healthy leaf keeps the pool open (#1389)", () => { | | 2231 | + const shared = [0, 1].map((index) => ModelRoute.ref({ providerID: "pool-shared", model: `s${index}` })) | | 2232 | + const healthy = ModelRoute.ref({ providerID: "pool-healthy", model: "m2" }) | | 2233 | + const sharedDefinition = { | | 2234 | + id: "pool-shared-route", | | 2235 | + targets: [...shared, healthy], | | 2236 | + targetVariants: [], | | 2237 | + health: policy, | | 2238 | + nodes: [ | | 2239 | + { selection: "ordered" as const, weights: [1], children: [{ node: 1 }] }, | | 2240 | + { routeID: "pool-shared-plan", selection: "ordered" as const, weights: [1, 1, 1], children: [{ leaf: 0 }, { leaf: 1 }, { leaf: 2 }] }, | | 2241 | + ], | | 2242 | + } as unknown as ModelRoute.Definition | | 2243 | + const now = Date.now() | | 2244 | + const resetAt = now + 30 * 60_000 | | 2245 | + | | 2246 | + // Two leaves behind one gated credential are one quota signal, not two. | | 2247 | + ModelRoute.resetHealth() | | 2248 | + const sharedCredential = (index: number) => | | 2249 | + sharedDefinition.targets[index].providerID === "pool-shared" ? "cred-shared" : undefined | | 2250 | + ModelRoute.observeQuotaFailure(shared[0], "cred-shared", resetAt, now) | | 2251 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 0, now, sharedCredential)).toBe(false) | | 2252 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 1, now, sharedCredential)).toBe(false) | | 2253 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now, sharedCredential)).toBe(false) | | 2254 | + // A second, genuinely distinct gate on the healthy leaf's provider still exhausts the pool. | | 2255 | + ModelRoute.observeQuotaFailure(healthy, undefined, resetAt, now) | | 2256 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now, sharedCredential)).toBe(true) | | 2257 | + | | 2258 | + // Two credential-less leaves behind one gated provider are one quota signal, not two. | | 2259 | + ModelRoute.resetHealth() | | 2260 | + ModelRoute.observeQuotaFailure(shared[0], undefined, resetAt, now) | | 2261 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now)).toBe(false) | | 2262 | + ModelRoute.observeQuotaFailure(healthy, undefined, resetAt, now) | | 2263 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now)).toBe(true) | | 2264 | + | | 2265 | + // Two independent quota benches on distinct credentials still exhaust the pool. | | 2266 | + ModelRoute.resetHealth() | | 2267 | + const distinctCredential = (index: number) => | | 2268 | + sharedDefinition.targets[index].providerID === "pool-shared" ? `cred-${index}` : undefined | | 2269 | + ModelRoute.failed(shared[0], policy, now, { quota: true }, "cred-0") | | 2270 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now, distinctCredential)).toBe(false) | | 2271 | + ModelRoute.failed(shared[1], policy, now, { quota: true }, "cred-1") | | 2272 | + expect(ModelRoute.poolQuotaExhausted(sharedDefinition, 2, now, distinctCredential)).toBe(true) | | 2273 | + ModelRoute.resetHealth() | | 2274 | + }) | 2229 | 2275 | }) |
|