maxpool 1.24.81 → 1.24.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/account-manager.js +530 -27
- package/src/claude-reset-grants.js +124 -24
- package/src/index.js +1 -0
- package/src/prober.js +162 -14
- package/src/reset-policy.js +130 -25
- package/src/reset-timing.js +27 -0
- package/src/server.js +62 -0
- package/src/tui.js +111 -28
- package/src/zai-reset-cards.js +20 -6
package/src/account-manager.js
CHANGED
|
@@ -3,6 +3,13 @@ import { computeDuplicates, credentialFp, accountIdentity, remoteAccountIdentity
|
|
|
3
3
|
import { appendEventLog } from './event-log.js';
|
|
4
4
|
import { hourFloor } from './outside-usage.js';
|
|
5
5
|
import { CapacityLedger, TANK_MIN_UTIL, mergeCycles, tankStatsFromCycles } from './capacity-ledger.js';
|
|
6
|
+
// A1: the ONE shared grant-scope answer (display, routing, claim and lift all read it).
|
|
7
|
+
import { appliesTo, forgetClaimOrg } from './claude-reset-grants.js';
|
|
8
|
+
import { POLICY, CLAUDE_WALL_WEEKLY, probeSentAt } from './reset-policy.js';
|
|
9
|
+
import {
|
|
10
|
+
LISTING_FRESH_MS, LIFT_GRACE_MS, CLAIM_STALE_LISTING_MAX_MS, CLAIM_BACKOFF_STEPS_MS,
|
|
11
|
+
CLAIM_BACKOFF_CAP_MS, REACTIVE_CLAIM_TIMEOUT_MS,
|
|
12
|
+
} from './reset-timing.js';
|
|
6
13
|
|
|
7
14
|
// Review r2 A: consecutive request-scoped faults before they count against the account.
|
|
8
15
|
const REQUEST_FAULT_ESCALATE = 3;
|
|
@@ -95,6 +102,7 @@ function emptyQuota() {
|
|
|
95
102
|
// marker — a swallowed failing probe no longer silently freezes a stale tag.
|
|
96
103
|
// Header-driven updates do NOT stamp this (headers can't refresh scoped/provider).
|
|
97
104
|
lastProbeOkAt: null, // ms
|
|
105
|
+
lastProbeOkSentAt: null, // ms — SEND time of that probe (claim ordering, C1)
|
|
98
106
|
// Last background probe FAILURE — surfaced in the TUI/status so a persistently
|
|
99
107
|
// failing probe (e.g. the usage endpoint rate-limiting us) is VISIBLE instead of
|
|
100
108
|
// silently swallowed (which let a stale weekly keep looking fresh). Cleared on
|
|
@@ -537,6 +545,46 @@ function _windowElapsedRatio(resetAt, durationMs, now) {
|
|
|
537
545
|
return Math.max(0, (durationMs - (resetAt - now)) / durationMs);
|
|
538
546
|
}
|
|
539
547
|
|
|
548
|
+
/** B1/B2 (2026-11-10): normalize a REMOTE status row's reset payload. One shared
|
|
549
|
+
* shape for syncRemoteMirrors and the TUI benched-twin overlay. An older cloud
|
|
550
|
+
* omits the new fields: a missing `eligible` reads `grants.length>0` (holdings
|
|
551
|
+
* still display, never a silent 0 — the "cloud rows show no resets" bug);
|
|
552
|
+
* missing new grant fields take the fail-closed default from A1; a missing
|
|
553
|
+
* capLift means no lift (never recomputed from the mirror's listing copy). */
|
|
554
|
+
export function normalizeRemoteResets(ra) {
|
|
555
|
+
if (!ra) return { resetCards: null, resetGrants: null, capLift: null };
|
|
556
|
+
const out = { resetCards: null, resetGrants: null, capLift: null };
|
|
557
|
+
if (ra.resetCards && typeof ra.resetCards === 'object') {
|
|
558
|
+
out.resetCards = {
|
|
559
|
+
fiveHour: Array.isArray(ra.resetCards.fiveHour) ? ra.resetCards.fiveHour : [],
|
|
560
|
+
weekly: Array.isArray(ra.resetCards.weekly) ? ra.resetCards.weekly : [],
|
|
561
|
+
checkedAt: ra.resetCards.checkedAt ?? null,
|
|
562
|
+
stale: Boolean(ra.resetCards.stale),
|
|
563
|
+
lastError: ra.resetCards.lastError ?? null,
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
if (ra.resetGrants && typeof ra.resetGrants === 'object') {
|
|
567
|
+
const grants = Array.isArray(ra.resetGrants.grants) ? ra.resetGrants.grants : [];
|
|
568
|
+
out.resetGrants = {
|
|
569
|
+
grants,
|
|
570
|
+
nextGrantId: ra.resetGrants.nextGrantId ?? null,
|
|
571
|
+
cooldownUntil: ra.resetGrants.cooldownUntil ?? null,
|
|
572
|
+
// Missing eligible (an older cloud export) → display iff grants exist.
|
|
573
|
+
eligible: ra.resetGrants.eligible !== undefined ? ra.resetGrants.eligible !== false : grants.length > 0,
|
|
574
|
+
checkedAt: ra.resetGrants.checkedAt ?? null,
|
|
575
|
+
stale: Boolean(ra.resetGrants.stale),
|
|
576
|
+
lastError: ra.resetGrants.lastError ?? null,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
if (ra.capLift && typeof ra.capLift === 'object') {
|
|
580
|
+
// `ceiling` is the holder's computed lifted cap (null = full lift). A give-back
|
|
581
|
+
// lift MUST carry it — without it the mirror would read a full lift.
|
|
582
|
+
const ceiling = Number.isFinite(ra.capLift.ceiling) ? ra.capLift.ceiling : null;
|
|
583
|
+
out.capLift = { active: ra.capLift.active === true, since: ra.capLift.since ?? null, mode: ra.capLift.mode ?? null, ceiling };
|
|
584
|
+
}
|
|
585
|
+
return out;
|
|
586
|
+
}
|
|
587
|
+
|
|
540
588
|
export class AccountManager {
|
|
541
589
|
constructor(accounts, switchThreshold = 0.90, schedulerOptions = {}, dependencies = {}) {
|
|
542
590
|
this.scheduler = { ...DEFAULT_SCHEDULER, ...schedulerOptions };
|
|
@@ -649,8 +697,9 @@ export class AccountManager {
|
|
|
649
697
|
// Bounded LRU — oldest conversations drop first; a dropped entry only means the
|
|
650
698
|
// next turn is balanced fresh.
|
|
651
699
|
this.conversationLastAccount = new Map();
|
|
652
|
-
//
|
|
653
|
-
//
|
|
700
|
+
// _homeKey (affinityKey, else sessionKey) → { name, at, detoured } of the last CLAUDE
|
|
701
|
+
// account only (a GLM/Kimi turn never overwrites the name; it sets `detoured`), so a
|
|
702
|
+
// conversation returning from a provider detour can go home.
|
|
654
703
|
this.conversationLastClaudeAccount = new Map();
|
|
655
704
|
// Conversation-identity HEALTH (owner 2026-11-08: "make sure this cannot degrade
|
|
656
705
|
// silently"). Twice in one day a Claude Code request-shape detail (a moving
|
|
@@ -1787,6 +1836,19 @@ export class AccountManager {
|
|
|
1787
1836
|
_effectiveCap(account, window = 'wk', now = Date.now()) {
|
|
1788
1837
|
const floor = account?.capUtilization;
|
|
1789
1838
|
if (floor == null) return null; // uncapped — unchanged
|
|
1839
|
+
// C3 CAP LIFT (D2, owner 2026-11-10): while an account holds a usable
|
|
1840
|
+
// full-refill reset (a `clears:['seven_day',...]` grant or a usable z.ai
|
|
1841
|
+
// weekly card), its cap does not apply — it runs to its weekly limit, the
|
|
1842
|
+
// pool spends the reset there, and the cap applies again after. Suspends the
|
|
1843
|
+
// cap on BOTH windows (the session bench reads the cap too) and the provider
|
|
1844
|
+
// max(ses,wk) cap (a GLM capped below 0.90 never reaches the z.ai wall).
|
|
1845
|
+
// Give-back-only grants (gives_back_weekly, no seven_day) lift to a measured
|
|
1846
|
+
// ceiling, not the full wall — see _capLiftCeiling.
|
|
1847
|
+
if (this.capLiftActive(account, now)) {
|
|
1848
|
+
const ceil = this._capLiftCeiling(account, window, now);
|
|
1849
|
+
if (ceil != null) return ceil;
|
|
1850
|
+
return null; // full lift for this window
|
|
1851
|
+
}
|
|
1790
1852
|
if (account.capMode !== 'dynamic') return floor; // fixed — byte-for-byte as before
|
|
1791
1853
|
const ceiling = account.capCeiling ?? this.switchThreshold;
|
|
1792
1854
|
if (floor >= ceiling) return floor; // never pull a high floor DOWN
|
|
@@ -1798,6 +1860,155 @@ export class AccountManager {
|
|
|
1798
1860
|
return floor + (ceiling - floor) * ramp;
|
|
1799
1861
|
}
|
|
1800
1862
|
|
|
1863
|
+
// ── C3: cap lift (D2) ─────────────────────────────────────────────────────
|
|
1864
|
+
// capLift is computed ONLY by the holder pool (pre-mortem #13): mirrors honour
|
|
1865
|
+
// the exported value and never recompute it from their copy of the listing.
|
|
1866
|
+
// State per account: account.capLift = { active, since, graceUntil, reason,
|
|
1867
|
+
// ceilingOverride? } — evaluated live by capLiftActive() from the CURRENT
|
|
1868
|
+
// listing, so a grant missing from a fresh listing ends the lift at once.
|
|
1869
|
+
|
|
1870
|
+
/** D4 (finding 3): the free weekly refill of the limit the lift lets the account
|
|
1871
|
+
* burn is < 24h away — the lift would spend a scarce reset for a few hours of
|
|
1872
|
+
* capacity the free refill brings anyway. Held unless the lifting reset itself
|
|
1873
|
+
* dies inside the same 24h (its value is lost either way). Unknown refill = no hold. */
|
|
1874
|
+
_liftRefillHeld(account, sourceEndsAt, now = Date.now()) {
|
|
1875
|
+
const q = account.quota || {};
|
|
1876
|
+
const refillAt = account.type === 'provider' ? q.providerWkReset : q.unified7dReset;
|
|
1877
|
+
if (refillAt == null || refillAt - now >= POLICY.dyingWindowMs) return false;
|
|
1878
|
+
const sourceDying = sourceEndsAt != null && sourceEndsAt - now < POLICY.dyingWindowMs;
|
|
1879
|
+
return !sourceDying;
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
/** True while the account's usage cap is suspended by a held full-refill reset. */
|
|
1883
|
+
capLiftActive(account, now = Date.now()) {
|
|
1884
|
+
if (!account || account.enabled === false) return false;
|
|
1885
|
+
if (account.remote) {
|
|
1886
|
+
// Mirror: honour the holder pool's export only (never recompute).
|
|
1887
|
+
return account.capLift?.active === true;
|
|
1888
|
+
}
|
|
1889
|
+
const st = account.capLift;
|
|
1890
|
+
if (!st?.active) return false;
|
|
1891
|
+
// DEAD/OUT conditions end the lift regardless of grace.
|
|
1892
|
+
if (account.poolBenchedBy || account.refreshDead || account.subscriptionGone) {
|
|
1893
|
+
this.endCapLift(account.index, 'account dead/benched', now);
|
|
1894
|
+
return false;
|
|
1895
|
+
}
|
|
1896
|
+
// GRACE: listing failures hold the lift for LIFT_GRACE_MS, then end it.
|
|
1897
|
+
if (now > (st.graceUntil ?? 0)) {
|
|
1898
|
+
this.endCapLift(account.index, 'grace expired', now);
|
|
1899
|
+
return false;
|
|
1900
|
+
}
|
|
1901
|
+
// The free refill moved inside 24h while lifted: the cap returns now.
|
|
1902
|
+
if (this._liftRefillHeld(account, st.sourceEndsAt ?? null, now)) {
|
|
1903
|
+
this.endCapLift(account.index, 'free refill <24h away', now);
|
|
1904
|
+
return false;
|
|
1905
|
+
}
|
|
1906
|
+
return true;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
/** The lifted ceiling for a window, or null for "no cap at all".
|
|
1910
|
+
* A give-back grant with a measured drop lifts only to cap + drop (per window,
|
|
1911
|
+
* the weekly is where the give-back lands); a full seven_day grant lifts fully.
|
|
1912
|
+
* A MIRROR reads the holder's exported ceiling (it carries no giveBack); a
|
|
1913
|
+
* give-back lift whose ceiling is unknown fails CLOSED to the cap itself. */
|
|
1914
|
+
_capLiftCeiling(account, _window, _now) {
|
|
1915
|
+
const st = account.capLift;
|
|
1916
|
+
if (!st) return null;
|
|
1917
|
+
if (st.mode !== 'giveback') return null; // full lift
|
|
1918
|
+
const cap = Number(account.capUtilization) || 0;
|
|
1919
|
+
if (account.remote) return Number.isFinite(st.ceiling) ? st.ceiling : cap;
|
|
1920
|
+
const drop = Number(account.giveBack?.drop);
|
|
1921
|
+
return Number.isFinite(drop) && drop > 0 ? Math.min(1, cap + drop) : cap;
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
/** Re-evaluate the lift from the CURRENT listing (holder pool only). Called by
|
|
1925
|
+
* applyResetGrants / applyResetCards after every successful listing, and by
|
|
1926
|
+
* endCapLift on terminal events. Idempotent; logs each change once. */
|
|
1927
|
+
refreshCapLift(accountIndex, now = Date.now(), reason = 'listing refreshed') {
|
|
1928
|
+
const account = this.accounts[accountIndex];
|
|
1929
|
+
if (!account || account.remote) return;
|
|
1930
|
+
const was = Boolean(account.capLift?.active);
|
|
1931
|
+
|
|
1932
|
+
// START conditions (all must hold).
|
|
1933
|
+
let active = false;
|
|
1934
|
+
let mode = null;
|
|
1935
|
+
let sources = []; // the resets that carry the lift (their expiry decides the refill hold)
|
|
1936
|
+
// No cap configured → nothing to lift (starting a lift on an uncapped account
|
|
1937
|
+
// would remap its ordinary weekly tiers and log noise for no benefit).
|
|
1938
|
+
const capFloor = account.capUtilization;
|
|
1939
|
+
if (capFloor != null && account.enabled !== false && !account.poolBenchedBy && !account.refreshDead && !account.subscriptionGone) {
|
|
1940
|
+
const rg = account.resetGrants;
|
|
1941
|
+
const cards = account.resetCards;
|
|
1942
|
+
const listingOk = rg && rg.stale !== true && rg.eligible !== false
|
|
1943
|
+
&& rg.checkedAt != null && (now - rg.checkedAt) <= LISTING_FRESH_MS
|
|
1944
|
+
// No cooldown, no claim backoff for the grant that would lift (C3).
|
|
1945
|
+
&& !(rg.cooldownUntil != null && rg.cooldownUntil > now);
|
|
1946
|
+
if (listingOk) {
|
|
1947
|
+
const inBackoff = g => {
|
|
1948
|
+
const until = this._claimRetryUntil?.get(`${account.name}:${g.id}`);
|
|
1949
|
+
return until != null && until > now;
|
|
1950
|
+
};
|
|
1951
|
+
const fresh = (rg.grants || []).filter(g =>
|
|
1952
|
+
g && g.usableNow !== false && !g.paused && !g.expired && !g.blocking && !g.unavailableReason
|
|
1953
|
+
&& (g.endsAt == null || g.endsAt > now) && (g.resetsLeft ?? 1) > 0 && !inBackoff(g));
|
|
1954
|
+
// appliesTo is the ONE scope answer: full = clears seven_day; give-back =
|
|
1955
|
+
// gives_back_weekly on a grant that does NOT clear seven_day.
|
|
1956
|
+
const fullGrants = fresh.filter(g => Array.isArray(g.clears) && g.clears.includes('seven_day'));
|
|
1957
|
+
const givebackGrants = fresh.filter(g => g.givesBackWeekly === true
|
|
1958
|
+
&& !(Array.isArray(g.clears) && g.clears.includes('seven_day')));
|
|
1959
|
+
if (fullGrants.length) { active = true; mode = 'full'; sources = fullGrants.map(g => g.endsAt); }
|
|
1960
|
+
else if (givebackGrants.length) {
|
|
1961
|
+
// Give-back lifts ONLY once a measured claim recorded the drop (pre-mortem #7:
|
|
1962
|
+
// a give-back grant returns what the MODEL used, not the whole weekly).
|
|
1963
|
+
const drop = Number(account.giveBack?.drop);
|
|
1964
|
+
if (Number.isFinite(drop) && drop > 0) { active = true; mode = 'giveback'; sources = givebackGrants.map(g => g.endsAt); }
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
// z.ai weekly card: a full refill by the same rule (pre-mortem #9: a GLM
|
|
1968
|
+
// capped below 0.90 never reaches the z.ai wall otherwise).
|
|
1969
|
+
if (!active && account.type === 'provider' && account.provider === 'zai' && cards && cards.stale !== true
|
|
1970
|
+
&& cards.checkedAt != null && (now - cards.checkedAt) <= LISTING_FRESH_MS) {
|
|
1971
|
+
const hasWeeklyLimit = account.quota?.providerWk != null || account.quota?.providerWkReset != null;
|
|
1972
|
+
const liveWeekly = (cards.weekly || []).filter(c => c && !c.expired && (c.expiresAt == null || c.expiresAt > now));
|
|
1973
|
+
if (hasWeeklyLimit && liveWeekly.length) { active = true; mode = 'full'; sources = liveWeekly.map(c => c.expiresAt); }
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
// The soonest-expiring source is the one a claim would spend first.
|
|
1977
|
+
const known = sources.filter(t => t != null);
|
|
1978
|
+
const sourceEndsAt = known.length ? Math.min(...known) : null;
|
|
1979
|
+
// D4 (finding 3): never start or keep the lift inside 24h of the free refill.
|
|
1980
|
+
let endWhy = 'grant missing from fresh listing';
|
|
1981
|
+
if (active && this._liftRefillHeld(account, sourceEndsAt, now)) { active = false; endWhy = 'free refill <24h away'; }
|
|
1982
|
+
|
|
1983
|
+
if (active) {
|
|
1984
|
+
const prev = account.capLift;
|
|
1985
|
+
account.capLift = {
|
|
1986
|
+
active: true,
|
|
1987
|
+
since: prev?.active ? prev.since : now,
|
|
1988
|
+
graceUntil: now + LIFT_GRACE_MS,
|
|
1989
|
+
mode: mode || 'full',
|
|
1990
|
+
sourceEndsAt,
|
|
1991
|
+
reason,
|
|
1992
|
+
};
|
|
1993
|
+
if (!was) this._logCapLift(account, 'cap lifted', reason);
|
|
1994
|
+
} else if (was) {
|
|
1995
|
+
this.endCapLift(accountIndex, endWhy, now);
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
/** Force-end the lift (claim success/failure, grant gone, resetsLeft 0, cooldown). */
|
|
2000
|
+
endCapLift(accountIndex, why = 'lift ended', now = Date.now()) {
|
|
2001
|
+
const account = this.accounts[accountIndex];
|
|
2002
|
+
if (!account?.capLift?.active) return;
|
|
2003
|
+
account.capLift = { ...account.capLift, active: false, endedAt: now };
|
|
2004
|
+
this._logCapLift(account, 'cap restored', why);
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
_logCapLift(account, verb, why) {
|
|
2008
|
+
console.log(`[reset-grant] ${account.name}: ${verb} (${why})`);
|
|
2009
|
+
appendEventLog(`[reset-grant] ${account.name}: ${verb} (${why})`);
|
|
2010
|
+
}
|
|
2011
|
+
|
|
1801
2012
|
/**
|
|
1802
2013
|
* When a DYNAMIC cap will have risen far enough to stop benching `utilization` on
|
|
1803
2014
|
* this window — i.e. the instant the ramp crosses it. Null when that never happens
|
|
@@ -3203,10 +3414,18 @@ export class AccountManager {
|
|
|
3203
3414
|
*/
|
|
3204
3415
|
_holdsUsableReset(account, now = Date.now()) {
|
|
3205
3416
|
if (!account || account.enabled === false) return false;
|
|
3417
|
+
if (account.refreshDead || account.subscriptionGone) return false; // C4: a dead account can never spend
|
|
3418
|
+
// A spent/unknown listing holds nothing (C4: only a reset C1/C3 could spend).
|
|
3206
3419
|
const grants = account.resetGrants?.grants || [];
|
|
3207
|
-
|
|
3420
|
+
const listingFresh = account.resetGrants && account.resetGrants.stale !== true;
|
|
3421
|
+
// appliesTo is the ONE scope answer (C4): only a grant that can refill a WEEKLY
|
|
3422
|
+
// limit counts — session-only and unmapped-scope grants are never claimed, so
|
|
3423
|
+
// they earn no routing discount.
|
|
3424
|
+
if (grants.some(g => g && g.usableNow !== false && !g.paused && !g.blocking && !g.unavailableReason
|
|
3425
|
+
&& (g.endsAt == null || g.endsAt > now) && (g.resetsLeft ?? 1) > 0
|
|
3426
|
+
&& appliesTo(g).some(s => s !== 'session'))) return listingFresh;
|
|
3208
3427
|
const cards = account.resetCards;
|
|
3209
|
-
if (!cards) return false;
|
|
3428
|
+
if (!cards || cards.stale === true) return false;
|
|
3210
3429
|
const live = c => c && !c.expired && (c.expiresAt == null || c.expiresAt > now);
|
|
3211
3430
|
if ((cards.fiveHour || []).some(live)) return true;
|
|
3212
3431
|
const hasWeeklyLimit = account.quota?.providerWk != null || account.quota?.providerWkReset != null;
|
|
@@ -3216,6 +3435,10 @@ export class AccountManager {
|
|
|
3216
3435
|
_resetMultiplier(account, now = Date.now()) {
|
|
3217
3436
|
const d = Number(this.scheduler.resetBoostDiscount) || 0;
|
|
3218
3437
|
if (d <= 0) return 1;
|
|
3438
|
+
// C3/C4 (pre-mortem #10): NO discount while the cap lift is active — a lifted
|
|
3439
|
+
// reset-holder must attract ORDINARY share, not be preferred from 10% upward
|
|
3440
|
+
// (which would pile traffic onto ~10 accounts and 429 them).
|
|
3441
|
+
if (this.capLiftActive(account, now)) return 1;
|
|
3219
3442
|
return this._holdsUsableReset(account, now) ? 1 - Math.min(d, 0.9) : 1;
|
|
3220
3443
|
}
|
|
3221
3444
|
|
|
@@ -3366,26 +3589,43 @@ export class AccountManager {
|
|
|
3366
3589
|
*/
|
|
3367
3590
|
_noteConversationAccount(requestInfo, account) {
|
|
3368
3591
|
const key = this._affinityKey(requestInfo);
|
|
3369
|
-
if (
|
|
3370
|
-
|
|
3592
|
+
if (key) lruSet(this.conversationLastAccount, key, account.name);
|
|
3593
|
+
// Home memory: ONE key per conversation, independent of identity health, so the
|
|
3594
|
+
// degraded <-> ok flip of _affinityKey can neither hide a home nor lend it to the
|
|
3595
|
+
// sibling conversations that share the terminal key. The entry carries its own
|
|
3596
|
+
// detour state: a Claude turn clears it, a provider turn sets it (same name and
|
|
3597
|
+
// `at`, only the LRU position moves so other traffic cannot evict a long detour).
|
|
3598
|
+
const homeKey = this._homeKey(requestInfo);
|
|
3599
|
+
if (!homeKey) return;
|
|
3371
3600
|
if (isClaudeAccount(account)) {
|
|
3372
|
-
lruSet(this.conversationLastClaudeAccount,
|
|
3601
|
+
lruSet(this.conversationLastClaudeAccount, homeKey,
|
|
3602
|
+
{ name: account.name, at: requestInfo.now ?? Date.now(), detoured: false });
|
|
3603
|
+
} else {
|
|
3604
|
+
const home = this.conversationLastClaudeAccount.get(homeKey);
|
|
3605
|
+
if (home) lruSet(this.conversationLastClaudeAccount, homeKey, { ...home, detoured: true });
|
|
3373
3606
|
}
|
|
3374
3607
|
}
|
|
3375
3608
|
|
|
3609
|
+
/** Key of the conversation's home entry: the conversation itself, never the shared
|
|
3610
|
+
* terminal key when a conversation id exists. When identity is genuinely broken
|
|
3611
|
+
* (a fresh hash every request) the home is simply never found, so the normal pick
|
|
3612
|
+
* stands — never a forced or wrong return. */
|
|
3613
|
+
_homeKey(requestInfo = {}) {
|
|
3614
|
+
return requestInfo.affinityKey || requestInfo.sessionKey || null;
|
|
3615
|
+
}
|
|
3616
|
+
|
|
3376
3617
|
/**
|
|
3377
3618
|
* RETURN HOME (owner 2026-11-09): the Claude account a conversation should go back to
|
|
3378
|
-
* after a provider detour, or null. A detour =
|
|
3379
|
-
*
|
|
3380
|
-
*
|
|
3381
|
-
*
|
|
3382
|
-
* has outlived cacheAffinityReturnWindowMs — going home then buys nothing.
|
|
3619
|
+
* after a provider detour, or null. A detour = a provider turn was served after the
|
|
3620
|
+
* last Claude turn (the entry's own `detoured` flag), so a conversation that never
|
|
3621
|
+
* left Claude has no home to return to. `stale` when the stored copy has outlived
|
|
3622
|
+
* cacheAffinityReturnWindowMs — going home then buys nothing.
|
|
3383
3623
|
*/
|
|
3384
3624
|
_returnHome(requestInfo, now = Date.now()) {
|
|
3385
|
-
const
|
|
3386
|
-
if (!
|
|
3387
|
-
const home = this.conversationLastClaudeAccount.get(
|
|
3388
|
-
if (!home || home.
|
|
3625
|
+
const homeKey = this._homeKey(requestInfo);
|
|
3626
|
+
if (!homeKey) return null;
|
|
3627
|
+
const home = this.conversationLastClaudeAccount.get(homeKey);
|
|
3628
|
+
if (!home || !home.detoured) return null;
|
|
3389
3629
|
const ageMs = now - home.at;
|
|
3390
3630
|
let windowMs = Number(this.scheduler.cacheAffinityReturnWindowMs);
|
|
3391
3631
|
if (!Number.isFinite(windowMs) || windowMs < 0) windowMs = DEFAULT_SCHEDULER.cacheAffinityReturnWindowMs;
|
|
@@ -3893,6 +4133,13 @@ export class AccountManager {
|
|
|
3893
4133
|
// session window is its ONLY quota signal. Session availability otherwise
|
|
3894
4134
|
// belongs to _isSessionQuotaUnavailable + PROVIDER_SES_WALL.
|
|
3895
4135
|
const used = wkUsed != null ? wkUsed : (sesUsed ?? 0);
|
|
4136
|
+
// C3 CAP LIFT (provider twin): a lifted z.ai account tiers `normal` below
|
|
4137
|
+
// the wall so it can climb to the 0.90 card wall instead of being stranded
|
|
4138
|
+
// in reserve/critical by its own cap.
|
|
4139
|
+
if (this.capLiftActive(account, now)) {
|
|
4140
|
+
const ceil = this._capLiftCeiling(account, 'wk', now);
|
|
4141
|
+
if ((ceil == null && used < this.scheduler.weeklyExhaustedThreshold) || (ceil != null && used < ceil)) return 'normal';
|
|
4142
|
+
}
|
|
3896
4143
|
// USAGE CAP — checked FIRST, before every tier: a reservation is owner intent
|
|
3897
4144
|
// and outranks both the tier ladder and the upstream verdict. There is no
|
|
3898
4145
|
// upstreamAllows carve-out for providers anyway, but the ordering documents
|
|
@@ -3911,6 +4158,16 @@ export class AccountManager {
|
|
|
3911
4158
|
if (q.unified7d == null) return 'unknown';
|
|
3912
4159
|
|
|
3913
4160
|
const used = clamp01(q.unified7d);
|
|
4161
|
+
// C3 CAP LIFT (pre-mortem #10): while a general reset can be spent, the
|
|
4162
|
+
// soft/reserve/critical tiers would strand it — above 0.95 an account is
|
|
4163
|
+
// last-resort-only, so it never reaches the 0.99 claim point. Below the wall
|
|
4164
|
+
// (0.999, or the give-back ceiling cap+drop) a lifted account tiers as
|
|
4165
|
+
// `normal`, at ordinary share; above the ceiling the ordinary logic applies.
|
|
4166
|
+
if (this.capLiftActive(account, now)) {
|
|
4167
|
+
const ceil = this._capLiftCeiling(account, 'wk', now);
|
|
4168
|
+
if (ceil == null && used < this.scheduler.weeklyExhaustedThreshold) return 'normal';
|
|
4169
|
+
if (ceil != null && used < ceil) return 'normal';
|
|
4170
|
+
}
|
|
3914
4171
|
// NEVER bench on utilization alone while the upstream explicitly says the account is
|
|
3915
4172
|
// ALLOWED. Anthropic reports both a percentage and a verdict; the verdict is the
|
|
3916
4173
|
// authority, and it keeps saying "allowed"/"allowed_warning" at 100% used. This mirrors
|
|
@@ -3962,7 +4219,7 @@ export class AccountManager {
|
|
|
3962
4219
|
* Update an account's quota from a background usage probe (fetchUsage result).
|
|
3963
4220
|
* Same effect as learning quota from a live response, but for idle accounts.
|
|
3964
4221
|
*/
|
|
3965
|
-
applyUsageData(accountIndex, usage) {
|
|
4222
|
+
applyUsageData(accountIndex, usage, { sentAt = null } = {}) {
|
|
3966
4223
|
// A successful probe ends any escalation streak.
|
|
3967
4224
|
if (this.accounts[accountIndex]?.quota) this.accounts[accountIndex].quota.consecutiveProbeFailures = 0;
|
|
3968
4225
|
const account = this.accounts[accountIndex];
|
|
@@ -4039,6 +4296,18 @@ export class AccountManager {
|
|
|
4039
4296
|
}
|
|
4040
4297
|
|
|
4041
4298
|
q.lastProbeOkAt = Date.now();
|
|
4299
|
+
// C1 (finding 1): the SEND time orders this reading against a claim — a probe
|
|
4300
|
+
// sent before a claim and applied after it carries the pre-claim reading.
|
|
4301
|
+
q.lastProbeOkSentAt = Number.isFinite(sentAt) ? sentAt : q.lastProbeOkAt;
|
|
4302
|
+
// C3 give-back measurement: the FIRST successful probe after a give-back claim
|
|
4303
|
+
// records how far unified7d DROPPED — the value a give-back grant actually
|
|
4304
|
+
// returns (it only returns what the model used, never the whole weekly;
|
|
4305
|
+
// pre-mortem #7). Later lifts for this account use cap + drop as the ceiling.
|
|
4306
|
+
if (account.giveBack?.pendingAt != null && q.lastProbeOkSentAt > account.giveBack.pendingAt
|
|
4307
|
+
&& usage.sevenDay?.utilization != null) {
|
|
4308
|
+
const utilBefore = account.giveBack.utilBefore;
|
|
4309
|
+
account.giveBack = { drop: Math.max(0, utilBefore - clamp01(usage.sevenDay.utilization)), at: Date.now() };
|
|
4310
|
+
}
|
|
4042
4311
|
// A successful probe clears any recorded failure — freshness confirmed — including
|
|
4043
4312
|
// the org-403 streak (a healthy probe proves the org still accepts OAuth).
|
|
4044
4313
|
q.consecutiveOrgGone = 0;
|
|
@@ -4138,17 +4407,29 @@ export class AccountManager {
|
|
|
4138
4407
|
* the OAuth quota gates.
|
|
4139
4408
|
*/
|
|
4140
4409
|
/** Store the latest Claude reset-grant listing (prober piggyback). */
|
|
4141
|
-
applyResetGrants(accountIndex, listing) {
|
|
4410
|
+
applyResetGrants(accountIndex, listing, now = Date.now()) {
|
|
4142
4411
|
const account = this.accounts[accountIndex];
|
|
4143
4412
|
if (!account || !listing) return;
|
|
4144
|
-
if (listing.error) {
|
|
4413
|
+
if (listing.error) {
|
|
4414
|
+
// A failed check must not erase the last good count (2026-11-10: "2~119d" went
|
|
4415
|
+
// blank after one transient non-2xx, with no reset claimed). Keep the previous
|
|
4416
|
+
// listing, mark it stale, and keep checkedAt = the last GOOD read. Never good
|
|
4417
|
+
// before → stay null (nothing to show).
|
|
4418
|
+
if (account.resetGrants) {
|
|
4419
|
+
account.resetGrants = { ...account.resetGrants, stale: true, lastError: String(listing.error), lastErrorAt: now };
|
|
4420
|
+
}
|
|
4421
|
+
return;
|
|
4422
|
+
}
|
|
4145
4423
|
account.resetGrants = {
|
|
4146
4424
|
grants: listing.grants || [],
|
|
4147
4425
|
nextGrantId: listing.nextGrantId ?? null,
|
|
4148
4426
|
cooldownUntil: listing.cooldownUntil ?? null,
|
|
4149
4427
|
eligible: listing.eligible !== false,
|
|
4150
|
-
checkedAt:
|
|
4428
|
+
checkedAt: now,
|
|
4429
|
+
stale: false,
|
|
4151
4430
|
};
|
|
4431
|
+
// C3: re-evaluate the cap lift from the fresh listing.
|
|
4432
|
+
this.refreshCapLift(accountIndex, now, 'listing refreshed');
|
|
4152
4433
|
}
|
|
4153
4434
|
|
|
4154
4435
|
/** Store the latest reset-card listing for an account (prober piggyback).
|
|
@@ -4187,11 +4468,50 @@ export class AccountManager {
|
|
|
4187
4468
|
applyResetCards(accountIndex, cards) {
|
|
4188
4469
|
const account = this.accounts[accountIndex];
|
|
4189
4470
|
if (!account || !cards) return;
|
|
4190
|
-
if (cards.error) {
|
|
4471
|
+
if (cards.error) {
|
|
4472
|
+
// A4 (2026-11-10): keep-last-good, same as Claude grants. The old
|
|
4473
|
+
// `resetCards = null` blanked the count on any transient error (the z.ai
|
|
4474
|
+
// twin of the "2 → gone → back" flicker) and handed a wrong 0 to routing.
|
|
4475
|
+
// Never good before → stay null.
|
|
4476
|
+
if (account.resetCards) {
|
|
4477
|
+
account.resetCards = { ...account.resetCards, stale: true, lastError: String(cards.error), lastErrorAt: Date.now() };
|
|
4478
|
+
}
|
|
4479
|
+
return;
|
|
4480
|
+
}
|
|
4481
|
+
// DRIFT = a 200 the parser can't have meant (pre-mortem #17): expected arrays
|
|
4482
|
+
// missing, or raw entries present but none kept — treated EXACTLY like an
|
|
4483
|
+
// error: keep the last good cards (marked stale) and leave the cap lift on
|
|
4484
|
+
// its grace window (finding 11: re-evaluating the lift on a drifted listing
|
|
4485
|
+
// ended it at once instead of after the grace).
|
|
4486
|
+
const drift = !Array.isArray(cards.fiveHour) || !Array.isArray(cards.weekly);
|
|
4487
|
+
if (drift) {
|
|
4488
|
+
if (account.resetCards) {
|
|
4489
|
+
account.resetCards = { ...account.resetCards, stale: true, lastError: 'listing drift (unexpected payload shape)', lastErrorAt: Date.now() };
|
|
4490
|
+
}
|
|
4491
|
+
return;
|
|
4492
|
+
}
|
|
4191
4493
|
account.resetCards = {
|
|
4192
|
-
fiveHour: cards.fiveHour
|
|
4193
|
-
weekly: cards.weekly
|
|
4494
|
+
fiveHour: cards.fiveHour,
|
|
4495
|
+
weekly: cards.weekly,
|
|
4194
4496
|
checkedAt: Date.now(),
|
|
4497
|
+
stale: false,
|
|
4498
|
+
lastError: null,
|
|
4499
|
+
};
|
|
4500
|
+
// C3: a z.ai weekly card can start the lift too.
|
|
4501
|
+
this.refreshCapLift(accountIndex, Date.now(), 'cards refreshed');
|
|
4502
|
+
}
|
|
4503
|
+
|
|
4504
|
+
/** Remove one card recordId from the stored list after a SUCCESSFUL redeem
|
|
4505
|
+
* (A4): the follow-up read can fail, and a redeemed card that lingers keeps
|
|
4506
|
+
* feeding _holdsUsableReset a discount for capacity that no longer exists. */
|
|
4507
|
+
removeResetCard(accountIndex, recordId) {
|
|
4508
|
+
const account = this.accounts[accountIndex];
|
|
4509
|
+
if (!account?.resetCards || recordId == null) return;
|
|
4510
|
+
const rm = c => c.recordId !== recordId;
|
|
4511
|
+
account.resetCards = {
|
|
4512
|
+
...account.resetCards,
|
|
4513
|
+
fiveHour: (account.resetCards.fiveHour || []).filter(rm),
|
|
4514
|
+
weekly: (account.resetCards.weekly || []).filter(rm),
|
|
4195
4515
|
};
|
|
4196
4516
|
}
|
|
4197
4517
|
|
|
@@ -5036,6 +5356,9 @@ export class AccountManager {
|
|
|
5036
5356
|
account.refreshDead = false; // fresh tokens from re-auth revive a dead-refresh account
|
|
5037
5357
|
account.subscriptionGone = false; // a working re-auth means the org accepts OAuth again
|
|
5038
5358
|
if (account.status === 'error') account.status = 'active';
|
|
5359
|
+
// A re-login may be a DIFFERENT org under the same name: forget the cached
|
|
5360
|
+
// claim org and the claim backoff (finding 10).
|
|
5361
|
+
this._forgetClaimState(account.name);
|
|
5039
5362
|
console.log(`[Maxpool] Updated tokens for account "${account.name}"`);
|
|
5040
5363
|
this._onTokenRefresh?.(accountIndex, {
|
|
5041
5364
|
accessToken,
|
|
@@ -5300,6 +5623,9 @@ export class AccountManager {
|
|
|
5300
5623
|
const removed = this.accounts[index];
|
|
5301
5624
|
if (removed.inFlight > 0) return false;
|
|
5302
5625
|
const removedName = removed.name;
|
|
5626
|
+
// A later account under the same name must start clean (finding 10). Mirrors
|
|
5627
|
+
// never claimed, so only local rows carry claim state.
|
|
5628
|
+
if (!removed.remote) this._forgetClaimState(removedName);
|
|
5303
5629
|
this.accounts.splice(index, 1);
|
|
5304
5630
|
this.accounts.forEach((a, i) => a.index = i);
|
|
5305
5631
|
if (this.currentIndex >= this.accounts.length) {
|
|
@@ -5517,8 +5843,15 @@ export class AccountManager {
|
|
|
5517
5843
|
a.capCeiling = ra.capCeiling ?? a.capCeiling;
|
|
5518
5844
|
a.capRampStart = ra.capRampStart ?? a.capRampStart;
|
|
5519
5845
|
a.addedAt = null; // H3: mirrors never count as "warming"
|
|
5520
|
-
|
|
5521
|
-
|
|
5846
|
+
// B1/B2 (2026-11-10): one shared normalizeRemoteResets for the mirror row AND
|
|
5847
|
+
// the benched-twin overlay — an OLDER cloud omits `eligible`/new fields; a
|
|
5848
|
+
// missing `eligible` reads grants.length>0 so its holdings still display.
|
|
5849
|
+
// The lift is NEVER recomputed here (C3/pre-mortem #13): the exported
|
|
5850
|
+
// capLift flag is honoured as-is, missing = no lift.
|
|
5851
|
+
const rr = normalizeRemoteResets(ra);
|
|
5852
|
+
a.resetCards = rr.resetCards;
|
|
5853
|
+
a.resetGrants = rr.resetGrants;
|
|
5854
|
+
a.capLift = rr.capLift;
|
|
5522
5855
|
a.remoteMonthlyPriceUSD = Number.isFinite(ra.monthlyPriceUSD) ? ra.monthlyPriceUSD : null;
|
|
5523
5856
|
a.remoteTank = ra.tank || null;
|
|
5524
5857
|
a.remoteKeyFp = ra.keyFingerprint || null;
|
|
@@ -5547,6 +5880,154 @@ export class AccountManager {
|
|
|
5547
5880
|
return Boolean(account && account.poolBenchedBy);
|
|
5548
5881
|
}
|
|
5549
5882
|
|
|
5883
|
+
// ── C2: the reactive claim (a 429 on the serving lease claims AT ONCE) ──────
|
|
5884
|
+
// Runs only on the serving lease's LOCAL account holding the credential — never
|
|
5885
|
+
// a mirror row (its 429 is the CLOUD's; the cloud claims on its own side) and
|
|
5886
|
+
// never a relayed 429 (pre-mortem #21: the wrong pool would double-spend).
|
|
5887
|
+
// The grant comes from the STORED listing: stale is allowed up to 6h with
|
|
5888
|
+
// endsAt>now (a fresh listing is exactly what's missing at the wall — the usage
|
|
5889
|
+
// endpoint 429s). The claim uses the CURRENT access token; a 401 defers to the
|
|
5890
|
+
// sweep (no refresh on this path).
|
|
5891
|
+
|
|
5892
|
+
/** Pick the grant for a reactive claim. weekly=true → a general grant; a family
|
|
5893
|
+
* name → that model's grant, only if the probe MEASURED the bucket at the wall
|
|
5894
|
+
* (a reactive 429's own 1.0 mark is not measurement). Returns the grant or null. */
|
|
5895
|
+
_reactiveClaimGrant(account, kind, now = Date.now()) {
|
|
5896
|
+
if (!account?.resetGrants || account.resetGrants.eligible === false) return null;
|
|
5897
|
+
const checkedAt = account.resetGrants.checkedAt;
|
|
5898
|
+
if (checkedAt == null || now - checkedAt > CLAIM_STALE_LISTING_MAX_MS) return null; // stored listing ≤ 6h
|
|
5899
|
+
const grants = (account.resetGrants.grants || [])
|
|
5900
|
+
.filter(g => g && g.usableNow !== false && !g.paused && !g.expired && !g.blocking && !g.unavailableReason
|
|
5901
|
+
&& (g.endsAt == null || g.endsAt > now) && (g.resetsLeft ?? 1) > 0)
|
|
5902
|
+
.sort((a, b) => (a.endsAt ?? Infinity) - (b.endsAt ?? Infinity));
|
|
5903
|
+
if (!grants.length) return null;
|
|
5904
|
+
if (kind === 'weekly') return grants.find(g => appliesTo(g).includes('general')) || null;
|
|
5905
|
+
// Model-scoped: the probe must have MEASURED that family's bucket at the wall.
|
|
5906
|
+
const e = account.quota?.scopedWeekly?.[kind];
|
|
5907
|
+
const measured = e && e.reactive !== true && e.utilization != null && e.utilization >= CLAUDE_WALL_WEEKLY;
|
|
5908
|
+
return measured ? (grants.find(g => appliesTo(g).includes(kind)) || null) : null;
|
|
5909
|
+
}
|
|
5910
|
+
|
|
5911
|
+
/** A claim SUCCEEDED: spend it in the stored listing at once (finding 1). Until
|
|
5912
|
+
* the next listing (≤ 5 min, or hours when the endpoint 429s) the stored copy
|
|
5913
|
+
* otherwise still offers the spent reset to the reactive path, the lift and
|
|
5914
|
+
* the routing discount. resetsLeft drops by one; at 0 the grant leaves. */
|
|
5915
|
+
noteGrantClaimed(account, grantId) {
|
|
5916
|
+
const rg = account?.resetGrants;
|
|
5917
|
+
if (!rg || !Array.isArray(rg.grants)) return;
|
|
5918
|
+
const grants = [];
|
|
5919
|
+
for (const g of rg.grants) {
|
|
5920
|
+
if (g?.id !== grantId) { grants.push(g); continue; }
|
|
5921
|
+
const left = Math.floor(g.resetsLeft ?? 1) - 1;
|
|
5922
|
+
if (left >= 1) grants.push({ ...g, resetsLeft: left });
|
|
5923
|
+
}
|
|
5924
|
+
account.resetGrants = { ...rg, grants };
|
|
5925
|
+
}
|
|
5926
|
+
|
|
5927
|
+
/** Forget everything claim-related keyed by this account name: the cached org
|
|
5928
|
+
* and the per-grant backoff (finding 10 — a same-name re-login to another org
|
|
5929
|
+
* must not POST to the old org or inherit its backoff). */
|
|
5930
|
+
_forgetClaimState(accountName) {
|
|
5931
|
+
forgetClaimOrg(accountName);
|
|
5932
|
+
const prefix = `${accountName}:`;
|
|
5933
|
+
for (const m of [this._claimTries, this._claimRetryUntil]) {
|
|
5934
|
+
if (!m) continue;
|
|
5935
|
+
for (const k of [...m.keys()]) if (k.startsWith(prefix)) m.delete(k);
|
|
5936
|
+
}
|
|
5937
|
+
}
|
|
5938
|
+
|
|
5939
|
+
/** C6: the ONE claim lock per account, shared by the prober and the reactive
|
|
5940
|
+
* path. Returns true when the lock was ACQUIRED (caller must release). */
|
|
5941
|
+
acquireClaimLock(accountName) {
|
|
5942
|
+
if (!this._claimLocks) this._claimLocks = new Set();
|
|
5943
|
+
if (this._claimLocks.has(accountName)) return false;
|
|
5944
|
+
this._claimLocks.add(accountName);
|
|
5945
|
+
return true;
|
|
5946
|
+
}
|
|
5947
|
+
|
|
5948
|
+
releaseClaimLock(accountName) {
|
|
5949
|
+
this._claimLocks?.delete(accountName);
|
|
5950
|
+
}
|
|
5951
|
+
|
|
5952
|
+
/** C6: record a claim FAILURE — backoff per (account, grantId) 15m/30m/1h, cap 6h.
|
|
5953
|
+
* Shared state with the prober path (same maps). Returns the wait ms. */
|
|
5954
|
+
noteClaimFailure(accountName, grantId, now = Date.now()) {
|
|
5955
|
+
const key = `${accountName}:${grantId}`;
|
|
5956
|
+
if (!this._claimTries) this._claimTries = new Map();
|
|
5957
|
+
if (!this._claimRetryUntil) this._claimRetryUntil = new Map();
|
|
5958
|
+
const tries = (this._claimTries.get(key) || 0) + 1;
|
|
5959
|
+
this._claimTries.set(key, tries);
|
|
5960
|
+
const steps = CLAIM_BACKOFF_STEPS_MS;
|
|
5961
|
+
const waitMs = Math.min(CLAIM_BACKOFF_CAP_MS, steps[Math.min(tries - 1, steps.length - 1)]);
|
|
5962
|
+
this._claimRetryUntil.set(key, now + waitMs);
|
|
5963
|
+
return { tries, waitMs };
|
|
5964
|
+
}
|
|
5965
|
+
|
|
5966
|
+
/** C6: a SUCCESSFUL claim clears the backoff for that grant. */
|
|
5967
|
+
noteClaimSuccess(accountName, grantId) {
|
|
5968
|
+
this._claimRetryUntil?.set(`${accountName}:${grantId}`, 0);
|
|
5969
|
+
this._claimTries?.set(`${accountName}:${grantId}`, 0);
|
|
5970
|
+
}
|
|
5971
|
+
|
|
5972
|
+
/** Try the reactive claim for a weekly-class 429 (7d header ≥ 0.999) or a scoped
|
|
5973
|
+
* 429 (model family). Returns true when a claim was attempted. NEVER called for
|
|
5974
|
+
* a mirror row or a benched/disabled account. Shares the C6 lock and backoff
|
|
5975
|
+
* state with the prober path. The server calls it fire-and-forget; the claim
|
|
5976
|
+
* itself gives up after REACTIVE_CLAIM_TIMEOUT_MS so the lock is never held for
|
|
5977
|
+
* minutes. A 401 (token rejected) defers to the next sweep — it refreshes the
|
|
5978
|
+
* token first — and is NOT counted as a failed claim (no backoff). */
|
|
5979
|
+
async tryReactiveClaim(account, { kind, weeklyUtil: _weeklyUtil = null }, now = Date.now()) {
|
|
5980
|
+
if (!account) return false;
|
|
5981
|
+
if (account.remote || account.poolBenchedBy || account.enabled === false) return false;
|
|
5982
|
+
if (!account.credential || account.type !== 'oauth') return false;
|
|
5983
|
+
// C6 (finding 1): ONE claim per wall — after a claim, only a probe SENT after
|
|
5984
|
+
// it may show the wall again. Repeated 429s on the same wall never re-claim.
|
|
5985
|
+
const lastClaimAt = account.lastClaimAt ?? null;
|
|
5986
|
+
if (lastClaimAt != null && !((probeSentAt(account.quota) ?? -Infinity) > lastClaimAt)) return false;
|
|
5987
|
+
// No grant (or a scoped 429 without a probe-measured bucket) → the next sweep
|
|
5988
|
+
// decides on its own fresh reading.
|
|
5989
|
+
const grant = this._reactiveClaimGrant(account, kind, now);
|
|
5990
|
+
if (!grant) return false;
|
|
5991
|
+
// C6: shared backoff per (account, grantId).
|
|
5992
|
+
const key = `${account.name}:${grant.id}`;
|
|
5993
|
+
const until = this._claimRetryUntil?.get(key);
|
|
5994
|
+
if (until && until > now) return false;
|
|
5995
|
+
if (!this.acquireClaimLock(account.name)) return false;
|
|
5996
|
+
try {
|
|
5997
|
+
const { claimResetGrant } = await import('./claude-reset-grants.js');
|
|
5998
|
+
const r = await claimResetGrant(account.credential, grant.id, {
|
|
5999
|
+
accountKey: account.name, signal: AbortSignal.timeout(REACTIVE_CLAIM_TIMEOUT_MS),
|
|
6000
|
+
});
|
|
6001
|
+
if (!r?.ok && r?.status === 401) {
|
|
6002
|
+
console.log(`[reset-grant] ${account.name}: reactive claim deferred to the next sweep (token rejected)`);
|
|
6003
|
+
return true; // attempted; no backoff, no lift change
|
|
6004
|
+
}
|
|
6005
|
+
if (r?.ok) {
|
|
6006
|
+
this.noteClaimSuccess(account.name, grant.id);
|
|
6007
|
+
account.lastClaimAt = Date.now();
|
|
6008
|
+
this.noteGrantClaimed(account, grant.id);
|
|
6009
|
+
// Clear the wall this claim refills.
|
|
6010
|
+
if (kind !== 'weekly' && account.quota?.scopedWeekly?.[kind]) delete account.quota.scopedWeekly[kind];
|
|
6011
|
+
if (account.status === 'throttled') {
|
|
6012
|
+
account.status = 'active'; account.rateLimitedUntil = null;
|
|
6013
|
+
if (account.lastError === 'rate_limited') { account.lastError = null; account.lastErrorAt = null; }
|
|
6014
|
+
}
|
|
6015
|
+
this.endCapLift(account.index, 'reactive claim succeeded');
|
|
6016
|
+
console.log(`[reset-grant] ${account.name}: grant ${grant.id} claimed reactively (${kind === 'weekly' ? 'weekly 429 at the wall' : `${kind} scoped 429`})`);
|
|
6017
|
+
return true;
|
|
6018
|
+
}
|
|
6019
|
+
const { tries, waitMs } = this.noteClaimFailure(account.name, grant.id, now);
|
|
6020
|
+
console.log(`[reset-grant] ${account.name}: reactive claim FAILED ${grant.id} (try ${tries}, next attempt in ${Math.round(waitMs / 60000)}min): ${r?.error}`);
|
|
6021
|
+
this.endCapLift(account.index, 'reactive claim failed');
|
|
6022
|
+
return true; // attempted
|
|
6023
|
+
} catch (e) {
|
|
6024
|
+
console.log(`[reset-grant] ${account.name}: reactive claim error ${e.message}`);
|
|
6025
|
+
return true;
|
|
6026
|
+
} finally {
|
|
6027
|
+
this.releaseClaimLock(account.name);
|
|
6028
|
+
}
|
|
6029
|
+
}
|
|
6030
|
+
|
|
5550
6031
|
getStatus() {
|
|
5551
6032
|
const now = Date.now();
|
|
5552
6033
|
return {
|
|
@@ -5583,8 +6064,30 @@ export class AccountManager {
|
|
|
5583
6064
|
remote: Boolean(a.remote),
|
|
5584
6065
|
// Reset cards / grants (display + cross-pool routing preference). Card ids,
|
|
5585
6066
|
// counts and expiry only — no credentials; the redeem stays with the holder.
|
|
5586
|
-
|
|
5587
|
-
|
|
6067
|
+
// B1 (2026-11-10): carries eligible/stale/lastError/checkedAt/capLift/giveBack
|
|
6068
|
+
// so a linked pool (and its TUI) can display and honour them; a DISABLED
|
|
6069
|
+
// account exports null resets (pre-mortem #20: the API must not report
|
|
6070
|
+
// resets for accounts the pool can't use).
|
|
6071
|
+
resetCards: a.enabled === false || !a.resetCards ? null : {
|
|
6072
|
+
fiveHour: a.resetCards.fiveHour, weekly: a.resetCards.weekly,
|
|
6073
|
+
checkedAt: a.resetCards.checkedAt, stale: Boolean(a.resetCards.stale), lastError: a.resetCards.lastError ?? null,
|
|
6074
|
+
},
|
|
6075
|
+
resetGrants: a.enabled === false || !a.resetGrants ? null : {
|
|
6076
|
+
grants: a.resetGrants.grants, cooldownUntil: a.resetGrants.cooldownUntil,
|
|
6077
|
+
checkedAt: a.resetGrants.checkedAt, stale: Boolean(a.resetGrants.stale), lastError: a.resetGrants.lastError ?? null,
|
|
6078
|
+
eligible: a.resetGrants.eligible !== false,
|
|
6079
|
+
},
|
|
6080
|
+
// C3: the holder pool's cap-lift verdict. Mirrors honour this as-is;
|
|
6081
|
+
// missing = no lift. Never recomputed on the mirror side.
|
|
6082
|
+
// `ceiling` = the holder's lifted cap (null = full lift): a give-back lift
|
|
6083
|
+
// exported without it read as a FULL lift on every mirror (finding 2).
|
|
6084
|
+
// The EVALUATED verdict (grace, refill proximity, dead/benched), never the
|
|
6085
|
+
// raw stored flag — a lift that capLiftActive would end must not reach mirrors.
|
|
6086
|
+
capLift: (a.remote ? a.capLift?.active === true : this.capLiftActive(a, now))
|
|
6087
|
+
? { active: true, since: a.capLift.since ?? null, mode: a.capLift.mode ?? null,
|
|
6088
|
+
ceiling: a.remote ? (a.capLift.ceiling ?? null) : this._capLiftCeiling(a, 'wk', now) }
|
|
6089
|
+
: { active: false },
|
|
6090
|
+
giveBack: a.giveBack ?? null,
|
|
5588
6091
|
// Capacity-page inputs for linked pools (a mirror has no credential and no
|
|
5589
6092
|
// local cycles): this pool's plan price and measured tanks, per window.
|
|
5590
6093
|
monthlyPriceUSD: a.remote ? null : this.monthlyPriceFor(a),
|