Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 9 additions & 46 deletions packages/core/src/model-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,21 +798,6 @@ export const layer = Layer.effect(
const indexes = definition.targets.flatMap((_, index) => (triedTargets.has(index) ? [] : [index]))
const unavailable = new Map<number, string>()
const overLimit = new Set<number>()
// Pool-shaped pull-out (#1014 PR3): a leaf whose nested group (pool) has had two
// distinct quota evidence sources answer quota in turn is out with its pool, derived from the
// leaves' live quota benches/gates at this moment.
const poolExhausted = new Map<number, boolean>()
for (const index of indexes)
poolExhausted.set(
index,
ModelRoute.poolQuotaExhausted(
definition,
index,
now,
(leaf) => effectiveCredential(definition.targets[leaf], now)?.id,
locationDirectory,
),
)
const ready = indexes.filter((index) => {
const target = definition.targets[index]
const until = ModelRoute.cooldownUntil(target, now, locationDirectory)
Expand All @@ -836,25 +821,20 @@ export const layer = Layer.effect(
limited > now && ModelRoute.keepsThrough(definition.id, sessionID, target, now, credentialID, locationDirectory)
const credentialUntil = credentialID ? ModelRoute.credentialCooldownUntil(target, credentialID, now) : 0
const quotaBlock = credentialID ? ModelRoute.credentialQuotaBlocked(credentialID, now, locationDirectory) : undefined
const providerBlock = ModelRoute.providerQuotaBlocked(target, now, locationDirectory)
// Half-open probe (#1123): a bench whose known quota window reset has passed admits
// one probe attempt instead of excluding the leaf until the ladder expires. This
// admission is tentative — the claim is spent only when selection actually picks
// the target, below — so a probe is never consumed by a target the route passed over.
const targetProbe = until > now && ModelRoute.probeDue(target, undefined, now)
const credentialProbe =
credentialUntil > now && credentialID !== undefined && ModelRoute.probeDue(target, credentialID, now)
const isPoolExhausted = poolExhausted.get(index) ?? false
// A due half-open probe is the pool's recovery path, not more exhaustion
// evidence (#1390): the pool verdict (which already carves due probes out of
// its count) must not exclude the very leaf whose probe is due, e.g. a larger
// pool where two other leaves keep the verdict exhausted.
const poolProbe = targetProbe || credentialProbe
if (until > now && !targetProbe) unavailable.set(index, `cooldown-until:${until}`)
else if (credentialUntil > now && !credentialProbe)
unavailable.set(index, `credential-cooldown-until:${credentialUntil}`)
else if (quotaBlock) unavailable.set(index, `quota-known-full-until:${quotaBlock.until ?? "unknown"}`)
else if (isPoolExhausted && !poolProbe)
unavailable.set(index, `pool-quota-exhausted:${ModelRoute.poolIDOf(definition, index) ?? "pool"}`)
else if (providerBlock)
unavailable.set(index, `provider-quota-known-full-until:${providerBlock.until ?? "unknown"}`)
else if (limited > now && !kept) unavailable.set(index, `provider-limit-until:${limited}`)
else if (isSkipped) unavailable.set(index, "user-skip")
else if (isOverBudget) unavailable.set(index, "soft-budget")
Expand All @@ -863,7 +843,7 @@ export const layer = Layer.effect(
(until <= now || targetProbe) &&
(credentialUntil <= now || credentialProbe) &&
quotaBlock === undefined &&
(!isPoolExhausted || poolProbe) &&
providerBlock === undefined &&
(limited <= now || kept) &&
!isSkipped &&
!isOverBudget &&
Expand All @@ -883,28 +863,11 @@ export const layer = Layer.effect(
const credentialID = effectiveCredential(definition.targets[index], now)?.id
return credentialID === undefined || ModelRoute.credentialQuotaBlocked(credentialID, now, locationDirectory) === undefined
})
// A quota-exhausted pool is a HARD exclusion, unlike budgets and skips: the last
// resort never re-offers a pool its leaves have proven walled (#1014 PR3) — the
// route resolves nothing and the cascade looks elsewhere instead. A due half-open
// probe is the exception (#1390): the last resort still offers a leaf whose own
// probe is due, so recovery is possible even when the rest of the pool keeps the
// verdict exhausted. When every
// usable target sits in an exhausted pool there is no last resort at all.
const nonPool = usable.filter((index) => {
if (!poolExhausted.get(index)) return true
const target = definition.targets[index]
const credentialID = credentialByProvider.get(definition.targets[index].providerID)
const until = ModelRoute.cooldownUntil(target, now, locationDirectory)
const credentialUntil = credentialID
? ModelRoute.credentialCooldownUntil(target, credentialID, now)
: 0
return (
(until > now && ModelRoute.probeDue(target, undefined, now)) ||
(credentialUntil > now && credentialID !== undefined && ModelRoute.probeDue(target, credentialID, now))
)
})
const nonPoolNonFull = nonPool.filter((index) => nonFull.includes(index))
const lastResort = nonPool.length === 0 ? [] : nonPoolNonFull.length > 0 ? nonPoolNonFull : nonPool
// No pool-level hard exclusion (Drew, 2026-11-07): a pool cannot run out —
// only a leaf's own model, credential, or provider can — so the last resort
// is the usable set itself, preferring targets whose credential is not
// known-full while any such target remains.
const lastResort = nonFull.length > 0 ? nonFull : usable
const eligible = ready.length > 0 ? ready : allowCooling ? lastResort : []
const index = ModelRoute.nextTarget(
definition,
Expand Down
193 changes: 34 additions & 159 deletions packages/core/src/model-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,16 +833,6 @@ export const availability = (now = Date.now()): AvailabilityEntry[] => {
)
}

/** How many distinct quota evidence sources of one pool must be out before the pool itself is pulled:
* one leaf's quota answer can be leaf-specific, but a second distinct evidence source of the same pool
* answering quota in turn is the pool-shaped signal (#1014 PR3). */
const POOL_QUOTA_LEAVES = 2

const quotaBench = (state: HealthState | undefined, now: number) =>
state !== undefined &&
state.cooldownUntil > now &&
(state.benchReason === "quota" || state.benchReason === "quota retry-after")

/** Whether a balance is dead under a threshold, with the same floating-point tolerance
* QuotaSnapshot.isKnownFull applies to live snapshots (#1461): a balance a hair above the
* threshold from rounding (0.4 - 0.3 = 0.10000000000000003 against the 0.1 default) is
Expand All @@ -869,155 +859,6 @@ const quotaSkipBlocks = (item: Adjustment, now: number, location?: string): bool
return deadBalance(judged, currentConstants(location).quotaGate.balanceDeadThreshold)
}

/** One leaf's quota-out evidence, or undefined when the leaf is not quota-out: a quota
* bench on the leaf (or on the credential that serves it) counts per leaf, a quota gate
* on the credential counts once per credential, and a quota gate on the provider (a
* known-full snapshot, a balance hold, or the reactive attempt-reported skip) counts
* once per provider. Bench evidence takes priority over a shared gate on the same
* leaf, so two genuinely independent benches under one gate still count separately.
* Balance holds count until a fresh balance increase releases them, so a pool held by
* a balance gate stays out with no timer-based re-admission (#1014). A bench whose
* half-open probe is due still produces evidence here; `poolQuotaExhausted` carves
* due probes back out so the pool verdict does not defeat the recovery path (#1390). */
const quotaEvidence = (
target: Model.Ref,
credentialID: string | undefined,
now: number,
location?: string,
): string | undefined => {
if (credentialID) {
if (quotaBench(credentialHealth.get(credentialKey(target, credentialID)), now))
return `bench:${credentialKey(target, credentialID)}`
if (credentialQuotaBlocked(credentialID, now, location) !== undefined) return `cred-gate:${credentialID}`
return undefined
}
if (quotaBench(health.get(key(target)), now)) return `bench:${key(target)}`
const provider = providerQuota.get(target.providerID)
// A threshold balance hold is judged under the judging Location (#1461), but its
// verdict is not final for the provider's other skips: a timed attempt-reported
// `quota:` skip installed while the hold stands must still count. The fallthrough
// is safe for the hold's own durable record because quotaSkipBlocks re-judges
// threshold records, so an admitted threshold hold does not re-block through it.
if (provider?.balanceHold !== undefined && holdBlocks(provider.balanceHold, provider.snapshot, now, location))
return `prov:${target.providerID}`
if (
provider &&
QuotaSnapshot.isKnownFull(provider.snapshot, now, currentConstants(location).quotaGate.balanceDeadThreshold)
)
return `prov:${target.providerID}`
if (
activeAdjustments(now).some(
(item) => item.id === `quota:${target.providerID}` && item.action === "skip" && quotaSkipBlocks(item, now, location),
)
)
return `prov:${target.providerID}`
return undefined
}

interface PoolMembership {
readonly id: string
readonly leaves: readonly number[]
}

/** The pool a leaf belongs to: the deepest nested group (a node other than the route root)
* that contains it. A provider-route group is named by its provider route, a nested route by
* its route id. A leaf sitting directly on the root belongs to no pool. When a leaf appears
* under several groups the deepest one is its pool — that is the tightest shared fate. */
const poolOf = (definition: Definition, index: number): PoolMembership | undefined => {
let best: { depth: number; nodeIndex: number; leaves: number[] } | undefined
const collect = (nodeIndex: number, into: number[]) => {
const node = definition.nodes[nodeIndex]
if (!node) return
for (const child of node.children) {
if (child.leaf !== undefined) into.push(child.leaf)
else if (child.node !== undefined) collect(child.node, into)
}
}
const visit = (nodeIndex: number, depth: number) => {
const node = definition.nodes[nodeIndex]
if (!node) return
if (nodeIndex !== 0) {
const leaves: number[] = []
collect(nodeIndex, leaves)
if (leaves.includes(index) && (best === undefined || depth > best.depth)) best = { depth, nodeIndex, leaves }
}
for (const child of node.children) if (child.node !== undefined) visit(child.node, depth + 1)
}
visit(0, 0)
if (best === undefined) return undefined
const found: { depth: number; nodeIndex: number; leaves: number[] } = best
const node = definition.nodes[found.nodeIndex]
return { id: node?.providerRoute ?? node?.routeID ?? `${definition.id}#${found.nodeIndex}`, leaves: found.leaves }
}

/** The id of the pool (nested group) a leaf belongs to, or undefined when it sits on the root. */
export const poolIDOf = (definition: Definition, index: number) => poolOf(definition, index)?.id

/** Whether an independent quota gate currently blocks a leaf, apart from any bench:
* a quota gate on its credential, or one on its provider (a known-full snapshot, a
* balance hold, or the reactive attempt-reported skip). A due bench probe never
* bypasses these gates (see `probeDue`), so a gated leaf cannot recover through its
* probe until the gate's own reset passes. */
const independentQuotaGate = (
target: Model.Ref,
credentialID: string | undefined,
now: number,
location?: string,
): boolean => {
if (credentialID && credentialQuotaBlocked(credentialID, now, location) !== undefined) return true
const provider = providerQuota.get(target.providerID)
if (provider?.balanceHold !== undefined && holdBlocks(provider.balanceHold, provider.snapshot, now, location))
return true
if (
provider &&
QuotaSnapshot.isKnownFull(provider.snapshot, now, currentConstants(location).quotaGate.balanceDeadThreshold)
)
return true
return activeAdjustments(now).some(
(item) => item.id === `quota:${target.providerID}` && item.action === "skip" && quotaSkipBlocks(item, now, location),
)
}

/** Whether a leaf's pool is quota-exhausted: at least two distinct quota evidence
* sources across the pool's leaves are currently out, so the pool is pulled as a whole
* instead of spending attempts on its remaining leaves one by one (#1014 PR3 dynamic
* pull-out). Evidence is deduplicated by identity — two leaves out only through one
* shared credential or provider gate count as one signal, so a single provider's quota
* wall cannot hard-exclude a healthy leaf from another provider (#1389) — while a
* leaf's own quota bench always counts per leaf. A leaf whose bench probe is due
* contributes no evidence — unless an independent quota gate (on its credential or
* its provider) still blocks it: a probe never bypasses a gate whose own reset is
* still ahead, so a gated leaf's due probe cannot recover it yet and the leaf keeps
* counting (Codex review, PR #1473). An ungated due probe's bench still excludes
* ordinary candidacy, but the pool verdict must not defeat the half-open recovery
* the probe exists for (#1390). A claimed or not-yet-due bench keeps counting,
* because `probeDue` is false while a probe is outstanding and before its reset
* passes. The verdict is derived from the
* leaves' live quota state, so it lifts by itself when the benches reach their stated
* reset, when a balance hold is released by a fresh increase, or when a benched leaf
* recovers — a recovered pool re-enters rotation with no second producer. */
export const poolQuotaExhausted = (
definition: Definition,
index: number,
now = Date.now(),
credentialOf?: (index: number) => string | undefined,
location?: string,
): boolean => {
const pool = poolOf(definition, index)
if (!pool || pool.leaves.length < POOL_QUOTA_LEAVES) return false
const evidence = new Set<string>()
for (const leaf of pool.leaves) {
const target = definition.targets[leaf]
if (!target) continue
const credentialID = credentialOf?.(leaf)
const source = quotaEvidence(target, credentialID, now, location)
if (!source) continue
if (probeDue(target, credentialID, now) && !independentQuotaGate(target, credentialID, now, location)) continue
evidence.add(source)
}
return evidence.size >= POOL_QUOTA_LEAVES
}

/** The half-open bench probe claim a routed pick spent (#1478, #1486): the token names the
* pick's spend across every scope it claimed, so settlement lands only on scopes whose
* outstanding claim is still this pick's. The credential is the snapshot credential at pick
Expand Down Expand Up @@ -2123,6 +1964,40 @@ export const credentialQuotaBlocked = (
return undefined
}

/** Whether a target's own provider quota gate currently blocks it: the provider-scope
* mirror of `credentialQuotaBlocked`, judged from the provider's own gate state alone.
* There is no pool-level verdict (Drew, 2026-11-07): a pool cannot run out — only
* models, credentials, and providers can — so a leaf is excluded by its own provider's
* gate or not at all, never by the state of other leaves in its route group. A restored
* provider balance record with no in-memory state yet is not judged here: its durable
* `quota:` skip excludes through `skipped` until the first fed snapshot re-arms the
* state, so the leaf stays out on its own provider's record either way. */
export const providerQuotaBlocked = (
target: Model.Ref,
now = Date.now(),
location?: string,
): { readonly until: number | null } | undefined => {
const state = providerQuota.get(target.providerID)
if (!state) return undefined
// Judged exactly as the credential scope is (#1461): a threshold-derived hold blocks
// only a Location whose own threshold still judges the retained snapshot known-full;
// a retained reactive timed gate blocks every Location until its reset.
if (state.balanceHold) {
if (holdBlocks(state.balanceHold, state.snapshot, now, location)) return { until: null }
if (state.timedGateUntil !== undefined && state.timedGateUntil > now) return { until: state.timedGateUntil }
return undefined
}
if (QuotaSnapshot.isKnownFull(state.snapshot, now, currentConstants(location).quotaGate.balanceDeadThreshold)) {
state.gated = true
return { until: state.snapshot.resetAt }
}
if (state.gated) {
state.gated = false
logHealth(state.target, "quota-readmitted", `provider:${target.providerID}`)
}
return undefined
}

/** Ends a state's bench on a success: an ordinary success only ends an expired bench
* (recover), but a success on a claimed half-open probe ends the bench outright (#1123) — the
* probe was admitted precisely to learn whether the leaf serves again, and it does. */
Expand Down
Loading