@@ -1631,6 +1631,45 @@ function previewReusableAffinityAccount(
16311631 return entry . accountId ;
16321632}
16331633
1634+ /**
1635+ * Re-evaluate an affined account under the quota strategy. Returns a strictly
1636+ * cooler replacement, or null when the current binding should remain.
1637+ */
1638+ function reevaluateAffinityQuota (
1639+ entry : ThreadAffinityEntry ,
1640+ config : OcxConfig ,
1641+ now : number ,
1642+ quotaScope ?: CodexQuotaScope ,
1643+ selectionOptions ?: CodexAccountUsabilityOptions ,
1644+ ) : string | null {
1645+ if ( normalizeAccountPoolStrategy ( config . accountPoolStrategy ) !== "quota" ) return null ;
1646+ const threshold = config . autoSwitchThreshold ?? 80 ;
1647+ const usage = threshold > 0
1648+ ? computeCodexUsageScore (
1649+ getAccountQuota ( entry . accountId ) ,
1650+ getPoolAccountPlanForSelection ( config , entry . accountId , selectionOptions ) ,
1651+ )
1652+ : 0 ;
1653+ const overThreshold = threshold > 0 && ! isUnknownUsage ( usage ) && usage >= threshold ;
1654+ if (
1655+ ! overThreshold
1656+ && now - entry . lastReevalAt < CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS
1657+ ) {
1658+ return null ;
1659+ }
1660+ entry . lastReevalAt = now ;
1661+ if ( ! overThreshold ) return null ;
1662+ const best = pickLowerUsageAccount (
1663+ config ,
1664+ entry . accountId ,
1665+ usage ,
1666+ now ,
1667+ quotaScope ,
1668+ selectionOptions ,
1669+ ) ;
1670+ return best === entry . accountId ? null : best ;
1671+ }
1672+
16341673/**
16351674 * Side-effect-free preview of the Codex pool account native routing would prefer.
16361675 * Used for subagent fallback quota decisions before final auth.
@@ -1766,35 +1805,16 @@ export function resolveCodexAccountForThreadDetailed(
17661805 // Model detours follow the same affinity policy as ordinary bindings:
17671806 // RR/fill-first stay sticky, while quota strategy may re-evaluate an
17681807 // over-threshold account without changing the ordinary lane.
1769- if ( normalizeAccountPoolStrategy ( config . accountPoolStrategy ) === "quota" ) {
1770- const threshold = config . autoSwitchThreshold ?? 80 ;
1771- const usage = threshold > 0
1772- ? computeCodexUsageScore (
1773- getAccountQuota ( detourEntry . accountId ) ,
1774- getPoolAccountPlanForSelection ( config , detourEntry . accountId , selectionOptions ) ,
1775- )
1776- : 0 ;
1777- const overThreshold = threshold > 0 && ! isUnknownUsage ( usage ) && usage >= threshold ;
1778- if (
1779- overThreshold
1780- || now - detourEntry . lastReevalAt >= CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS
1781- ) {
1782- detourEntry . lastReevalAt = now ;
1783- if ( overThreshold ) {
1784- const best = pickLowerUsageAccount (
1785- config ,
1786- detourEntry . accountId ,
1787- usage ,
1788- now ,
1789- quotaScope ,
1790- selectionOptions ,
1791- ) ;
1792- if ( best !== detourEntry . accountId ) {
1793- bindModelDetourAffinity ( threadId , best , now , modelId , quotaScope ) ;
1794- return { status : "selected" , accountId : best } ;
1795- }
1796- }
1797- }
1808+ const cooler = reevaluateAffinityQuota (
1809+ detourEntry ,
1810+ config ,
1811+ now ,
1812+ quotaScope ,
1813+ selectionOptions ,
1814+ ) ;
1815+ if ( cooler ) {
1816+ bindModelDetourAffinity ( threadId , cooler , now , modelId , quotaScope ) ;
1817+ return { status : "selected" , accountId : cooler } ;
17981818 }
17991819 return { status : "selected" , accountId : detourEntry . accountId } ;
18001820 }
@@ -1834,29 +1854,13 @@ export function resolveCodexAccountForThreadDetailed(
18341854 // serving for up to 60s after a secondary with quota is available (#584).
18351855 // Non-quota strategies (RR / fill-first) keep affinity for ongoing threads —
18361856 // rotation is new-session-only (affinity policy A).
1837- const strategy = normalizeAccountPoolStrategy ( config . accountPoolStrategy ) ;
1838- if ( strategy === "quota" ) {
1839- const threshold = config . autoSwitchThreshold ?? 80 ;
1840- const usage = threshold > 0
1841- ? computeCodexUsageScore (
1842- getAccountQuota ( entry . accountId ) ,
1843- getPoolAccountPlanForSelection ( config , entry . accountId , selectionOptions ) ,
1844- )
1845- : 0 ;
1846- const overThreshold = threshold > 0 && ! isUnknownUsage ( usage ) && usage >= threshold ;
1847- if ( overThreshold || now - entry . lastReevalAt >= CODEX_THREAD_AFFINITY_REEVAL_INTERVAL_MS ) {
1848- entry . lastReevalAt = now ;
1849- if ( overThreshold ) {
1850- const best = pickLowerUsageAccount ( config , entry . accountId , usage , now , quotaScope , selectionOptions ) ;
1851- if ( best !== entry . accountId ) {
1852- if ( ! isIndependentCodexQuotaScope ( quotaScope ) ) {
1853- setActiveCodexAccount ( config , best ) ;
1854- }
1855- bindThreadAffinity ( threadId , best , now , quotaScope ) ; // rebinds + resets clocks
1856- return { status : "selected" , accountId : best } ;
1857- }
1858- }
1857+ const cooler = reevaluateAffinityQuota ( entry , config , now , quotaScope , selectionOptions ) ;
1858+ if ( cooler ) {
1859+ if ( ! isIndependentCodexQuotaScope ( quotaScope ) ) {
1860+ setActiveCodexAccount ( config , cooler ) ;
18591861 }
1862+ bindThreadAffinity ( threadId , cooler , now , quotaScope ) ; // rebinds + resets clocks
1863+ return { status : "selected" , accountId : cooler } ;
18601864 }
18611865 return { status : "selected" , accountId : entry . accountId } ;
18621866 }
0 commit comments