maxpool 1.24.93 → 1.24.94
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 +155 -4
- package/src/claude-reset-grants.js +8 -0
- package/src/prober.js +51 -9
- package/src/reset-policy.js +94 -9
package/package.json
CHANGED
package/src/account-manager.js
CHANGED
|
@@ -4,11 +4,11 @@ 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
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';
|
|
7
|
+
import { appliesTo, clearsGeneralWeekly, forgetClaimOrg } from './claude-reset-grants.js';
|
|
8
|
+
import { POLICY, CLAUDE_WALL_WEEKLY, probeSentAt, wallClaimHeld, HOLD_POOL_HAS_ROOM } from './reset-policy.js';
|
|
9
9
|
import {
|
|
10
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,
|
|
11
|
+
CLAIM_BACKOFF_CAP_MS, REACTIVE_CLAIM_TIMEOUT_MS, CLAIM_FRESH_MS,
|
|
12
12
|
} from './reset-timing.js';
|
|
13
13
|
|
|
14
14
|
// Review r2 A: consecutive request-scoped faults before they count against the account.
|
|
@@ -3449,6 +3449,110 @@ export class AccountManager {
|
|
|
3449
3449
|
* - ramp: ease a just-recovered account back in instead of slamming it
|
|
3450
3450
|
* - failures: direct per-account backoff after errors
|
|
3451
3451
|
*/
|
|
3452
|
+
/** R7 (owner, 2026-11-13 — task-2026-11-13-maxpool-hold-resets-when-pool-has-room):
|
|
3453
|
+
* how many OTHER accounts in THIS pool have room to absorb the load while the
|
|
3454
|
+
* GENERAL weekly wall claim waits for the account's free weekly refill (< 24h
|
|
3455
|
+
* out). General only (red-team r3): model walls are never held, so there is no
|
|
3456
|
+
* per-model count. The number
|
|
3457
|
+
* feeds decideClaudeWithReason's hold gate (opts.peersWithRoom), so it must be
|
|
3458
|
+
* computed LIVE from current account state — the hold re-evaluates every sweep.
|
|
3459
|
+
* Conservative per peer: only a probe-FRESH (≤ CLAIM_FRESH_MS) reading counts,
|
|
3460
|
+
* every disqualifier means "cannot confirm room" (claim, never hold on a guess):
|
|
3461
|
+
* - serves the SAME traffic (red-team r3 #4, r4 #5): counted PER routing profile
|
|
3462
|
+
* self serves, by the router's own _matchesProfile, and the THINNEST profile
|
|
3463
|
+
* decides — every kind of traffic the walled account carries needs its own
|
|
3464
|
+
* roomy peers (a peer serving only 'all' takes none of its 'claude' requests).
|
|
3465
|
+
* A self that serves no profile has no confirmed room (0).
|
|
3466
|
+
* - routable NOW: enabled, not throttled/error/exhausted, not pool-benched
|
|
3467
|
+
* (another process owns it), not a remote mirror (routed by its holder pool),
|
|
3468
|
+
* not auth/subscription-dead.
|
|
3469
|
+
* - weekly util (unified7d) leaves ≥ POLICY.holdPeerWeeklyRoom to the peer's
|
|
3470
|
+
* weekly ceiling — its effective usage cap, else 1.0 (uncapped: below the 0.65
|
|
3471
|
+
* `soft` tier the router already spreads load at). Red-team #4: room is
|
|
3472
|
+
* measured to the CAP, so a 0.70 cap at 0.64 is not room.
|
|
3473
|
+
* - 5h util (unified5h) leaves ≥ POLICY.holdPeerSessionRoom to the peer's
|
|
3474
|
+
* session bench (_sessionBenchThreshold: its 5h cap, else the 0.90 switch
|
|
3475
|
+
* threshold) — a session-benched account absorbs nothing.
|
|
3476
|
+
* - a DIFFERENT identity (accountUuid/credential fingerprint) — not this same
|
|
3477
|
+
* account under a second name; peers sharing one capacityIdentity (two local
|
|
3478
|
+
* names for one subscription) count ONCE (red-team #6).
|
|
3479
|
+
* - every ACTIVE model bucket the peer reports also has ≥ holdPeerWeeklyRoom
|
|
3480
|
+
* left (red-team #1) — the walled account serves no model at all, so a peer
|
|
3481
|
+
* whose Fable is exhausted/full (or 429-marked, or unreadable) would leave
|
|
3482
|
+
* Fable with no route for up to a day. An inactive bucket binds nothing (the
|
|
3483
|
+
* router's _scopedExhausted rule).
|
|
3484
|
+
* - a RESOLVABLE identity (red-team r2 #4): a peer whose capacityIdentity is
|
|
3485
|
+
* null is unknown capacity and is not counted; one equal to self's is self
|
|
3486
|
+
* (or self under a second name). Self itself is excluded by the same check —
|
|
3487
|
+
* its identity is either null or its own.
|
|
3488
|
+
* Returns a plain integer (the minimum over self's profiles); the POLICY decides
|
|
3489
|
+
* sufficiency (holdMinPeers × holders). */
|
|
3490
|
+
countPeersWithRoom(self, now = Date.now()) {
|
|
3491
|
+
const selfId = this.capacityIdentity(self);
|
|
3492
|
+
const selfProfiles = self?.profiles || ['claude', 'all'];
|
|
3493
|
+
const roomLeft = (util, ceiling, room) => Number.isFinite(util) && util <= ceiling - room;
|
|
3494
|
+
const roomy = [];
|
|
3495
|
+
for (const p of this.accounts) {
|
|
3496
|
+
const id = this.capacityIdentity(p);
|
|
3497
|
+
if (id == null || id === selfId) continue;
|
|
3498
|
+
if (p.type !== 'oauth' || p.enabled === false) continue;
|
|
3499
|
+
if (p.remote || p.poolBenchedBy) continue;
|
|
3500
|
+
if (p.refreshDead || p.subscriptionGone) continue;
|
|
3501
|
+
if (p.status === 'throttled' || p.status === 'error' || p.status === 'exhausted') continue;
|
|
3502
|
+
if (p.cooldownUntil && p.cooldownUntil > now) continue;
|
|
3503
|
+
const q = p.quota || {};
|
|
3504
|
+
// Probe-fresh only: a peer whose reading predates this sweep's decision is
|
|
3505
|
+
// unknown capacity, and unknown capacity must not hold a claim.
|
|
3506
|
+
const fresh = q.lastProbeOkAt != null && now - q.lastProbeOkAt <= CLAIM_FRESH_MS;
|
|
3507
|
+
if (!fresh) continue;
|
|
3508
|
+
const wkCeiling = this._effectiveCap(p, 'wk', now) ?? 1;
|
|
3509
|
+
if (!roomLeft(q.unified7d, wkCeiling, POLICY.holdPeerWeeklyRoom)) continue;
|
|
3510
|
+
if (!roomLeft(q.unified5h, this._sessionBenchThreshold(p, now), POLICY.holdPeerSessionRoom)) continue;
|
|
3511
|
+
const buckets = Object.values(q.scopedWeekly || {}).filter(e => e && e.isActive !== false);
|
|
3512
|
+
if (buckets.some(e => !roomLeft(e.utilization, 1, POLICY.holdPeerWeeklyRoom))) continue;
|
|
3513
|
+
roomy.push({ p, id });
|
|
3514
|
+
}
|
|
3515
|
+
if (!selfProfiles.length) return 0;
|
|
3516
|
+
return Math.min(...selfProfiles.map(pr =>
|
|
3517
|
+
new Set(roomy.filter(({ p }) => this._matchesProfile(p, pr)).map(({ id }) => id)).size));
|
|
3518
|
+
}
|
|
3519
|
+
|
|
3520
|
+
// ── R7 hold register (red-team r2 #1/#2) ──────────────────────────────────
|
|
3521
|
+
// account._wallHold = { at, refillAt } is stamped whenever either claim path HOLDS the
|
|
3522
|
+
// GENERAL weekly wall (the only wall R7 holds — red-team r3). It is live only
|
|
3523
|
+
// while the hold still describes reality — see isHoldingWall — so a general
|
|
3524
|
+
// claim, the refill or leaving the wall retires it without any clearing code.
|
|
3525
|
+
|
|
3526
|
+
/** Record that `account`'s general weekly wall claim was held, and WHICH refill
|
|
3527
|
+
* it waits for (red-team r4 #4). */
|
|
3528
|
+
noteWallHold(account, now = Date.now()) {
|
|
3529
|
+
if (account) account._wallHold = { at: now, refillAt: account.quota?.unified7dReset ?? null };
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
/** Is `account` holding its general weekly wall RIGHT NOW? A stamped hold that no
|
|
3533
|
+
* claim CLEARING the general weekly has superseded (lastFullGeneralClaimAt before
|
|
3534
|
+
* the stamp — a Fable claim refills nothing the hold waits on, red-team r3 #3; a
|
|
3535
|
+
* give-back claim retires it only through the live reading below, r5 #1), on an
|
|
3536
|
+
* enabled, live (r5 #2), locally-owned account still at the wall with its refill
|
|
3537
|
+
* still ahead — and that refill is still the one it waited for (a later window is
|
|
3538
|
+
* a new wall, not this hold; holdRefillSlackMs absorbs the two sources' reading
|
|
3539
|
+
* precision). */
|
|
3540
|
+
isHoldingWall(account, now = Date.now()) {
|
|
3541
|
+
const h = account?._wallHold;
|
|
3542
|
+
if (!h || account.enabled === false || account.poolBenchedBy) return false;
|
|
3543
|
+
if (account.refreshDead || account.subscriptionGone) return false; // a dead account serves nothing to hold for
|
|
3544
|
+
if (account.lastFullGeneralClaimAt != null && account.lastFullGeneralClaimAt >= h.at) return false;
|
|
3545
|
+
const q = account.quota || {};
|
|
3546
|
+
if (!(q.unified7dReset <= h.refillAt + POLICY.holdRefillSlackMs)) return false;
|
|
3547
|
+
return Number.isFinite(q.unified7d) && q.unified7d >= CLAUDE_WALL_WEEKLY && q.unified7dReset > now;
|
|
3548
|
+
}
|
|
3549
|
+
|
|
3550
|
+
/** The walls a hold must budget for: every OTHER account holding now, plus
|
|
3551
|
+
* `self` (the wall being decided). Feeds wallClaimHeld's holdersInPool. */
|
|
3552
|
+
countHoldingWalls(self, now = Date.now()) {
|
|
3553
|
+
return 1 + this.accounts.filter(a => a !== self && this.isHoldingWall(a, now)).length;
|
|
3554
|
+
}
|
|
3555
|
+
|
|
3452
3556
|
/**
|
|
3453
3557
|
* Does this account hold a banked reset that can actually lift its BINDING limit?
|
|
3454
3558
|
* Claude grants: usable, not paused, unexpired. z.ai cards: unexpired five-hour cards
|
|
@@ -5716,6 +5820,14 @@ export class AccountManager {
|
|
|
5716
5820
|
// count instead of nothing; restoreQuotaState marks it stale.
|
|
5717
5821
|
if (a.resetGrants) out.resetGrants = { ...a.resetGrants, grants: (a.resetGrants.grants || []).map(g => ({ ...g })) };
|
|
5718
5822
|
if (a.resetCards) out.resetCards = { ...a.resetCards, fiveHour: (a.resetCards.fiveHour || []).map(c => ({ ...c })), weekly: (a.resetCards.weekly || []).map(c => ({ ...c })) };
|
|
5823
|
+
// R7 HOLD REGISTER survives restart (red-team r5 #3): the hold and the claim
|
|
5824
|
+
// stamps that retire it / guard one claim per wall. isHoldingWall re-checks the
|
|
5825
|
+
// restored hold against the live wall and refill, so a stale one self-retires.
|
|
5826
|
+
// Unset fields are undefined and drop out of the JSON state file; a hold is
|
|
5827
|
+
// only ever replaced whole (noteWallHold), so sharing the reference is safe.
|
|
5828
|
+
out.wallHold = a._wallHold;
|
|
5829
|
+
out.lastGeneralClaimAt = a.lastGeneralClaimAt;
|
|
5830
|
+
out.lastFullGeneralClaimAt = a.lastFullGeneralClaimAt;
|
|
5719
5831
|
return out;
|
|
5720
5832
|
});
|
|
5721
5833
|
}
|
|
@@ -5762,6 +5874,11 @@ export class AccountManager {
|
|
|
5762
5874
|
if (match.resetCards && !account.resetCards) {
|
|
5763
5875
|
account.resetCards = { ...match.resetCards, stale: true };
|
|
5764
5876
|
}
|
|
5877
|
+
// R7 hold register (r5 #3). Boot-time only (index.js), so nothing live is
|
|
5878
|
+
// overwritten; an absent field restores as undefined, i.e. never stamped.
|
|
5879
|
+
account._wallHold = match.wallHold;
|
|
5880
|
+
account.lastGeneralClaimAt = match.lastGeneralClaimAt;
|
|
5881
|
+
account.lastFullGeneralClaimAt = match.lastFullGeneralClaimAt;
|
|
5765
5882
|
// OUTSIDE-USAGE restored: keep holding R7 and force the next sweep to
|
|
5766
5883
|
// re-verify immediately (fresh poolHourly can only re-prove the leak).
|
|
5767
5884
|
if (match.outsideUsage?.outside) {
|
|
@@ -6061,12 +6178,40 @@ export class AccountManager {
|
|
|
6061
6178
|
if (!account.credential || account.type !== 'oauth') return false;
|
|
6062
6179
|
// C6 (finding 1): ONE claim per wall — after a claim, only a probe SENT after
|
|
6063
6180
|
// it may show the wall again. Repeated 429s on the same wall never re-claim.
|
|
6064
|
-
|
|
6181
|
+
// The general wall counts only claims that cleared it (red-team r4 #2): after a
|
|
6182
|
+
// Fable claim under the hold, a dark probe can't move past the account-wide
|
|
6183
|
+
// stamp, and the rescue / weekly 429 must still reach the general reset.
|
|
6184
|
+
const lastClaimAt = (kind === 'weekly' ? account.lastGeneralClaimAt : account.lastClaimAt) ?? null;
|
|
6065
6185
|
if (lastClaimAt != null && !((probeSentAt(account.quota) ?? -Infinity) > lastClaimAt)) return false;
|
|
6066
6186
|
// No grant (or a scoped 429 without a probe-measured bucket) → the next sweep
|
|
6067
6187
|
// decides on its own fresh reading.
|
|
6068
6188
|
const grant = this._reactiveClaimGrant(account, kind, now);
|
|
6069
6189
|
if (!grant) return false;
|
|
6190
|
+
// R7 (owner, 2026-11-13): a wall 429 claims a held reset only when the free
|
|
6191
|
+
// refill of the limit it clears ISN'T imminent-with-pool-room — the same
|
|
6192
|
+
// wallClaimHeld rule the sweep uses, so the fast path cannot spend a reset the
|
|
6193
|
+
// sweep deliberately holds. Evaluated DIRECTLY here, not via the sweep's full
|
|
6194
|
+
// decision (red-team #3): at the wall the account's own usage probe and listing
|
|
6195
|
+
// are routinely stale (the usage endpoint 429s), and the sweep's freshness skips
|
|
6196
|
+
// would read as "not held" and fall through to a claim. The hold needs no fresh
|
|
6197
|
+
// reading of THIS account: the refill stamp is kept current by every response's
|
|
6198
|
+
// rate-limit headers (updateQuota), the grant end comes from the stored listing,
|
|
6199
|
+
// and capacity comes from the PEERS' own fresh probes (countPeersWithRoom). A
|
|
6200
|
+
// grant dying before (or within 24h after) the refill never waits. The budget
|
|
6201
|
+
// counts every wall held in this pool right now (r2 #2) and the hold is
|
|
6202
|
+
// registered (r2 #1) so the sweep's rescue can find it again.
|
|
6203
|
+
// GENERAL ONLY (red-team r3): a model (Fable) 429 claims exactly as before R7.
|
|
6204
|
+
if (kind === 'weekly' && wallClaimHeld(account.quota?.unified7dReset, grant, now,
|
|
6205
|
+
{ peersWithRoom: () => this.countPeersWithRoom(account, now),
|
|
6206
|
+
holdersInPool: () => this.countHoldingWalls(account, now),
|
|
6207
|
+
listingCheckedAt: account.resetGrants?.checkedAt })) {
|
|
6208
|
+
// Log once per hold EPISODE: the sweep's rescue re-evaluates a held wall every
|
|
6209
|
+
// failed tick, and a line per tick would bury the console.
|
|
6210
|
+
const already = this.isHoldingWall(account, now);
|
|
6211
|
+
this.noteWallHold(account, now);
|
|
6212
|
+
if (!already) console.log(`[reset-grant] ${account.name}: at wall, claim held (${HOLD_POOL_HAS_ROOM})`);
|
|
6213
|
+
return false;
|
|
6214
|
+
}
|
|
6070
6215
|
// C6: shared backoff per (account, grantId).
|
|
6071
6216
|
const key = `${account.name}:${grant.id}`;
|
|
6072
6217
|
const until = this._claimRetryUntil?.get(key);
|
|
@@ -6084,6 +6229,12 @@ export class AccountManager {
|
|
|
6084
6229
|
if (r?.ok) {
|
|
6085
6230
|
this.noteClaimSuccess(account.name, grant.id);
|
|
6086
6231
|
account.lastClaimAt = Date.now();
|
|
6232
|
+
// A spent grant that applies to the general weekly — whatever the 429 kind —
|
|
6233
|
+
// counts as the general wall's claim (r3 #3, r4 #3: one claim per wall). Only
|
|
6234
|
+
// one that CLEARS it retires the hold (r5 #1): a give-back returns just what
|
|
6235
|
+
// the model used, and the live reading retires the hold once a probe sees it.
|
|
6236
|
+
if (appliesTo(grant).includes('general')) account.lastGeneralClaimAt = account.lastClaimAt;
|
|
6237
|
+
if (clearsGeneralWeekly(grant)) account.lastFullGeneralClaimAt = account.lastClaimAt;
|
|
6087
6238
|
this.noteGrantClaimed(account, grant.id);
|
|
6088
6239
|
// Clear the wall this claim refills.
|
|
6089
6240
|
if (kind !== 'weekly' && account.quota?.scopedWeekly?.[kind]) delete account.quota.scopedWeekly[kind];
|
|
@@ -80,6 +80,14 @@ export function appliesTo(grant) {
|
|
|
80
80
|
return [...out];
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/** Does spending this grant REFILL the general weekly outright (clears seven_day)?
|
|
84
|
+
* A give-back-only grant also applies to 'general' (appliesTo), but it returns
|
|
85
|
+
* only what one model used — whether that leaves the general wall is known only
|
|
86
|
+
* once a probe measures the drop (red-team r5 #1). */
|
|
87
|
+
export function clearsGeneralWeekly(grant) {
|
|
88
|
+
return Array.isArray(grant?.clears) && grant.clears.includes('seven_day');
|
|
89
|
+
}
|
|
90
|
+
|
|
83
91
|
// Process-wide: the first listing logs the raw grant KEY SET (keys only, never
|
|
84
92
|
// values) once, so a schema drift is visible without per-sweep spam. Set by
|
|
85
93
|
// listResetGrants on the first grants-bearing response.
|
package/src/prober.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
import { fetchUsage, fetchProviderUsage } from './oauth.js';
|
|
11
11
|
import { listResetCards, redeemResetCard, fetchZaiModelUsage } from './zai-reset-cards.js';
|
|
12
12
|
import { listSubscriptions, monthlyPriceUSD } from './zai-subs.js';
|
|
13
|
-
import { listResetGrants, claimResetGrant } from './claude-reset-grants.js';
|
|
13
|
+
import { listResetGrants, claimResetGrant, appliesTo, clearsGeneralWeekly } from './claude-reset-grants.js';
|
|
14
14
|
import { decideZai, decideClaudeWithReason, CLAUDE_WALL_WEEKLY } from './reset-policy.js';
|
|
15
15
|
import { zaiHourlyCalls, compareOutsideUsage, isUsableZaiUsage, OUTSIDE_CHECK_MS } from './outside-usage.js';
|
|
16
16
|
// Reset-machinery timing lives in ONE leaf module (re-exported for callers that
|
|
@@ -22,6 +22,10 @@ export { GRANTS_EVERY_MS, GRANTS_RETRY_MS, CLAIM_BACKOFF_STEPS_MS, CLAIM_BACKOFF
|
|
|
22
22
|
// token refresh per window — cheap, and bounded so a truly canceled plan isn't hammered).
|
|
23
23
|
export const SUB_RECHECK_MS = 30 * 60_000;
|
|
24
24
|
|
|
25
|
+
// The sweep-decision skips that mean "this tick's LISTING can't be trusted" — the
|
|
26
|
+
// only skips after a successful probe that hand a held wall to the rescue (r2 #1).
|
|
27
|
+
const STALE_LISTING_SKIPS = new Set(['listing stale', 'listing failed']);
|
|
28
|
+
|
|
25
29
|
export class Prober {
|
|
26
30
|
constructor(accountManager, { intervalMs = 0, probeFn = fetchUsage, providerProbeFn = fetchProviderUsage, grantsFn = listResetGrants, timeoutMs = 10_000, log = console.log, usageGapMs = null, zaiUsageFn = fetchZaiModelUsage, cardsFn = listResetCards, now = Date.now } = {}) {
|
|
27
31
|
this.am = accountManager;
|
|
@@ -360,6 +364,12 @@ export class Prober {
|
|
|
360
364
|
if (r?.ok) {
|
|
361
365
|
this.am.noteClaimSuccess?.(account.name, act.grant.id);
|
|
362
366
|
account.lastClaimAt = Date.now();
|
|
367
|
+
// A spent grant that applies to the general weekly — whatever scope it was
|
|
368
|
+
// claimed at — is the general wall's claim (r3 #3, r4 #3); only one that
|
|
369
|
+
// CLEARS it retires the hold (r5 #1, same rule as the 429 fast path). A
|
|
370
|
+
// Fable-only grant refills nothing the hold is waiting on.
|
|
371
|
+
if (appliesTo(act.grant).includes('general')) account.lastGeneralClaimAt = account.lastClaimAt;
|
|
372
|
+
if (clearsGeneralWeekly(act.grant)) account.lastFullGeneralClaimAt = account.lastClaimAt;
|
|
363
373
|
// Spend it in the STORED listing too: until the next listing (≤5 min) the
|
|
364
374
|
// held count, the lift and the reactive path must not see it as unspent.
|
|
365
375
|
this.am.noteGrantClaimed?.(account, act.grant.id);
|
|
@@ -481,13 +491,11 @@ export class Prober {
|
|
|
481
491
|
usage = await this._withTimeout(this.probeFn(account.credential));
|
|
482
492
|
}
|
|
483
493
|
}
|
|
484
|
-
if (usage == null) { // timed out
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
this.am.recordProbeError?.(account.index, usage.error, usage.status ?? null);
|
|
490
|
-
return { ok: false, status: usage.status ?? null };
|
|
494
|
+
if (usage == null || usage.error) { // timed out / HTTP error (e.g. 429) / fetch failure
|
|
495
|
+
const e = usage == null ? 'probe timed out' : usage.error;
|
|
496
|
+
this.am.recordProbeError?.(account.index, e, usage?.status ?? null);
|
|
497
|
+
await this._rescueHeldWall(account); // red-team r2 #1: a held wall must not strand
|
|
498
|
+
return { ok: false, status: usage?.status ?? null };
|
|
491
499
|
}
|
|
492
500
|
this.am.applyUsageData(account.index, usage, { sentAt: probeSentAt }); // clears the error + stamps freshness
|
|
493
501
|
// C1: decide the claim on the reading JUST stored (this sweep's probe), on
|
|
@@ -496,6 +504,7 @@ export class Prober {
|
|
|
496
504
|
return { ok: true, status: 200 };
|
|
497
505
|
} catch (e) { // best-effort; never let a probe throw
|
|
498
506
|
this.am.recordProbeError?.(account.index, e?.message || String(e), null);
|
|
507
|
+
await this._rescueHeldWall(account); // red-team r2 #1
|
|
499
508
|
return { ok: false, status: null };
|
|
500
509
|
} finally {
|
|
501
510
|
account._subRecheck = false;
|
|
@@ -509,12 +518,27 @@ export class Prober {
|
|
|
509
518
|
if (!this.am.applyResetGrants) return;
|
|
510
519
|
const listing = account.resetGrants;
|
|
511
520
|
if (!listing) return;
|
|
512
|
-
|
|
521
|
+
// R7 (2026-11-13): the GENERAL weekly wall claim (the only one R7 holds — red-team
|
|
522
|
+
// r3) waits for an imminent free refill only while
|
|
523
|
+
// other accounts in THIS pool have room. Counted LIVE per sweep, so the hold
|
|
524
|
+
// ends the moment capacity disappears; unknown capacity claims (never hold).
|
|
525
|
+
// holdersInPool budgets one hold per holdMinPeers roomy peers across the pool
|
|
526
|
+
// (r2 #2); a hold is registered so a later failed probe can still rescue it (r2 #1).
|
|
527
|
+
const peersWithRoom = this.am.countPeersWithRoom ? () => this.am.countPeersWithRoom(account) : null;
|
|
528
|
+
const holdersInPool = this.am.countHoldingWalls ? () => this.am.countHoldingWalls(account) : null;
|
|
529
|
+
const { action, reason, heldScope } = decideClaudeWithReason(account, listing, Date.now(), { peersWithRoom, holdersInPool });
|
|
530
|
+
// heldScope rides on every return once the general wall is held — a dying model
|
|
531
|
+
// grant claimed in the same decision included (r3 #2) — so the hold is always registered.
|
|
532
|
+
if (heldScope === 'general') this.am.noteWallHold?.(account);
|
|
513
533
|
if (action) {
|
|
514
534
|
account._claimSkipKey = null;
|
|
515
535
|
await this._claimClaudeGrant(account, action);
|
|
516
536
|
return;
|
|
517
537
|
}
|
|
538
|
+
// The probe answered but the stored LISTING is stale/failed (the grants call
|
|
539
|
+
// shares the rate-limited endpoint): the fresh decision can't run, so a held
|
|
540
|
+
// wall goes through the rescue exactly as on a failed probe (r2 #1).
|
|
541
|
+
if (STALE_LISTING_SKIPS.has(reason)) await this._rescueHeldWall(account);
|
|
518
542
|
// C5: at a reached limit with a held-but-unspent grant, say WHY once per
|
|
519
543
|
// (account, reason) change — the owner's live check reads exactly these lines.
|
|
520
544
|
// The reason is the POLICY's own rejection (stale listing, cooldown, already
|
|
@@ -537,6 +561,24 @@ export class Prober {
|
|
|
537
561
|
}
|
|
538
562
|
}
|
|
539
563
|
|
|
564
|
+
/** Red-team r2 #1 — the held-wall rescue. A wall this pool HELD (its stamp is
|
|
565
|
+
* still live) must not strand: while the account's own usage endpoint is dark
|
|
566
|
+
* (a 429'd probe, a timeout, a throw — every early return of probeOne), no fresh
|
|
567
|
+
* decision can be made, yet "capacity unknown" means CLAIM. Re-evaluate the hold
|
|
568
|
+
* directly (wallClaimHeld: header/probe refill stamp, stored listing ≤ 6h,
|
|
569
|
+
* peers' LIVE probe freshness) and claim through the reactive route — the same
|
|
570
|
+
* one the 429 fast path uses, whose pick tolerates a stale listing on purpose.
|
|
571
|
+
* A still-roomy pool keeps holding; a refill that moved past 24h (or a dying
|
|
572
|
+
* grant) makes wallClaimHeld itself release. Never claims a wall nobody held.
|
|
573
|
+
* General weekly wall only (red-team r3): no model wall is ever held. */
|
|
574
|
+
async _rescueHeldWall(account) {
|
|
575
|
+
if (!this.am.isHoldingWall?.(account)) return; // never a wall nobody held
|
|
576
|
+
// A listing-side cooldown is a server instruction the reactive route doesn't read.
|
|
577
|
+
const cd = account.resetGrants?.cooldownUntil;
|
|
578
|
+
if (cd != null && cd > Date.now()) return;
|
|
579
|
+
await this.am.tryReactiveClaim(account, { kind: 'weekly' });
|
|
580
|
+
}
|
|
581
|
+
|
|
540
582
|
_withTimeout(promise) {
|
|
541
583
|
return Promise.race([
|
|
542
584
|
promise,
|
package/src/reset-policy.js
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
* value cannot survive a wait.
|
|
44
44
|
*/
|
|
45
45
|
import { appliesTo as defaultAppliesTo } from './claude-reset-grants.js';
|
|
46
|
-
import { CLAIM_FRESH_MS } from './reset-timing.js';
|
|
46
|
+
import { CLAIM_FRESH_MS, CLAIM_STALE_LISTING_MAX_MS } from './reset-timing.js';
|
|
47
47
|
|
|
48
48
|
export const POLICY = {
|
|
49
49
|
wallWeekly: 0.90, // weekly at 90% = at the wall (z.ai cards; UNCHANGED — pre-mortem #22)
|
|
@@ -53,8 +53,29 @@ export const POLICY = {
|
|
|
53
53
|
dyingMinUtilSes: 0.50, // 5h refills are cheap capacity; demand more waste first
|
|
54
54
|
freeRefillWaitMs: 24 * 3600 * 1000, // R6: hold a wall redeem this close to the free refill
|
|
55
55
|
freeRefillImmediate: 0.98, // R6: ...unless this full — never block the owner
|
|
56
|
+
// R7 (owner, 2026-11-13): the Claude GENERAL weekly wall claim also waits for the
|
|
57
|
+
// free refill when OTHER accounts can absorb the load. Room per peer: probe-fresh
|
|
58
|
+
// (≤5 min) readings, weekly util with ≥ 35% headroom left, 5h util with ≥ 25%
|
|
59
|
+
// headroom to the 0.90 session bench, routable now.
|
|
60
|
+
holdPeerWeeklyRoom: 0.35,
|
|
61
|
+
holdPeerSessionRoom: 0.25,
|
|
62
|
+
holdMinPeers: 2,
|
|
63
|
+
// A hold is tied to the refill it waited for (red-team r4 #4). The same refill is
|
|
64
|
+
// read from two sources with different precision (rate-limit header: whole
|
|
65
|
+
// seconds; usage probe: fractional), so a reading up to this much later is still
|
|
66
|
+
// that refill; a rolled window moves it by days.
|
|
67
|
+
holdRefillSlackMs: 3600 * 1000,
|
|
68
|
+
// The 429 fast path and the rescue claim from the STORED listing, which they
|
|
69
|
+
// refuse past CLAIM_STALE_LISTING_MAX_MS (6h). A hold therefore lets go this long
|
|
70
|
+
// before that bound (red-team r4 #6), while the reset is still claimable — a dark
|
|
71
|
+
// listing is unknown state, and unknown means claim. The rescue runs every sweep.
|
|
72
|
+
holdListingMarginMs: 30 * 60 * 1000,
|
|
56
73
|
};
|
|
57
74
|
|
|
75
|
+
// The plain-words decision reason for an R7 hold — exported so status/log surfaces
|
|
76
|
+
// and tests share one string.
|
|
77
|
+
export const HOLD_POOL_HAS_ROOM = 'free refill <24h away, other accounts have room';
|
|
78
|
+
|
|
58
79
|
// Claude cedar_ember claims read their OWN wall (pre-mortem #22): 0.99, deliberately
|
|
59
80
|
// NOT POLICY.wallWeekly (0.90) which z.ai shares — moving the shared constant would
|
|
60
81
|
// redeem GLM cards at a different point. Claude benches at 0.999 (weeklyExhausted)
|
|
@@ -69,6 +90,34 @@ export { CLAIM_FRESH_MS };
|
|
|
69
90
|
|
|
70
91
|
function msLeft(t, now) { return t == null ? Infinity : t - now; }
|
|
71
92
|
|
|
93
|
+
/** R7 (owner, 2026-11-13): should a GENERAL weekly WALL claim of `grant` wait for the
|
|
94
|
+
* free refill at `refillAt`? GENERAL ONLY (red-team r3, orchestrator decision): a
|
|
95
|
+
* per-model (Fable) wall and a dying-value grant claim exactly as before R7. The ONE
|
|
96
|
+
* hold rule, shared by the sweep (decideClaudeWithReason) and the
|
|
97
|
+
* reactive 429 path (account-manager tryReactiveClaim) so neither can spend what the
|
|
98
|
+
* other holds. Reads only the refill stamp, the grant's end and a LIVE peer count —
|
|
99
|
+
* never the walled account's own usage reading, which is routinely stale at the wall.
|
|
100
|
+
* Conservative: an unknown / past / ≥24h-away refill, a grant that dies before (or
|
|
101
|
+
* within dyingWindowMs after) the refill — it would sit unspent at ~0% used — no
|
|
102
|
+
* counter, or a count that isn't an integer ≥ the pool-wide budget ⇒ false (claim).
|
|
103
|
+
* BUDGET (red-team r2 #2): roomy peers must cover EVERY wall currently held in this
|
|
104
|
+
* pool — holdMinPeers × holdersInPool(), where holdersInPool counts the walls
|
|
105
|
+
* holding now INCLUDING this one. Without a holders counter, this wall alone (1).
|
|
106
|
+
* LISTING AGE (red-team r4 #6): when the caller passes `listingCheckedAt`, a stored
|
|
107
|
+
* listing within holdListingMarginMs of the 6h claim bound — or of unknown age
|
|
108
|
+
* (null) — releases the hold: past that bound the reset could not be claimed at
|
|
109
|
+
* all while the listing stays dark. Both claim paths pass it. */
|
|
110
|
+
export function wallClaimHeld(refillAt, grant, now, { peersWithRoom, holdersInPool, listingCheckedAt } = {}) {
|
|
111
|
+
if (!(refillAt > now) || refillAt - now >= POLICY.freeRefillWaitMs) return false;
|
|
112
|
+
if (listingCheckedAt !== undefined
|
|
113
|
+
&& !(now - listingCheckedAt < CLAIM_STALE_LISTING_MAX_MS - POLICY.holdListingMarginMs)) return false;
|
|
114
|
+
if (grant?.endsAt != null && grant.endsAt - refillAt < POLICY.dyingWindowMs) return false;
|
|
115
|
+
const holders = holdersInPool ? holdersInPool() : 1;
|
|
116
|
+
if (!Number.isInteger(holders) || holders < 1) return false;
|
|
117
|
+
const count = peersWithRoom?.();
|
|
118
|
+
return Number.isInteger(count) && count >= POLICY.holdMinPeers * holders;
|
|
119
|
+
}
|
|
120
|
+
|
|
72
121
|
/** Decide the z.ai action. Returns {kind:'card', card, resetType} | null. */
|
|
73
122
|
export function decideZai(account, cards, now = Date.now()) {
|
|
74
123
|
if (account?.enabled === false) return null; // R0
|
|
@@ -128,8 +177,11 @@ export function probeSentAt(q) {
|
|
|
128
177
|
|
|
129
178
|
/** Decide the Claude action AND say why when there is none.
|
|
130
179
|
* Returns { action: {kind:'grant', grant, scope, giveBack?, reason} | null,
|
|
131
|
-
* reason: string | null } — `reason` names
|
|
132
|
-
* is null (C5: the skip log prints exactly this), or the
|
|
180
|
+
* reason: string | null, heldScope?: string | null } — `reason` names
|
|
181
|
+
* the rejection when action is null (C5: the skip log prints exactly this), or the
|
|
182
|
+
* claim reason; `heldScope` is 'general' whenever the R7 hold kept the general
|
|
183
|
+
* weekly wall unclaimed on this decision — on EVERY return after that point, a claim
|
|
184
|
+
* of a dying model grant included, so the caller always registers the hold (null: none).
|
|
133
185
|
*
|
|
134
186
|
* C1 (amended, 2026-11-10): the claim is decided per grant by appliesTo (D1),
|
|
135
187
|
* NEVER by the server's nextGrantId order alone (pre-mortem #8: a Fable 429 could
|
|
@@ -139,7 +191,20 @@ export function probeSentAt(q) {
|
|
|
139
191
|
* any claim we already made (no second claim for one wall).
|
|
140
192
|
* D4: a dying-value claim (not at the wall) waits when the free refill of the
|
|
141
193
|
* limit it clears is < 24h away; a wall claim never waits (limit effectively full).
|
|
142
|
-
* Session-only grants are NEVER claimed (owner 2026-10-30).
|
|
194
|
+
* Session-only grants are NEVER claimed (owner 2026-10-30).
|
|
195
|
+
* R7 (owner, 2026-11-13, task-2026-11-13-maxpool-hold-resets-when-pool-has-room):
|
|
196
|
+
* the GENERAL weekly WALL claim is HELD when its free refill is < 24h away
|
|
197
|
+
* (freeRefillWaitMs) AND opts.peersWithRoom() answers at least POLICY.holdMinPeers
|
|
198
|
+
* × opts.holdersInPool() — other accounts in THIS pool can absorb the load. Model
|
|
199
|
+
* (Fable) walls are never held (red-team r3) — but while the general wall is held the
|
|
200
|
+
* model WALL claim is skipped too: the account is benched by its general wall, so a
|
|
201
|
+
* Fable reset would buy nothing until the general refill lands — unless that grant
|
|
202
|
+
* would expire before (or within dyingWindowMs after) the refill: then it is claimed. The hold
|
|
203
|
+
* re-evaluates every decision tick (the caller supplies a live count), and fails
|
|
204
|
+
* CLAIM-side: capacity unknown (no counter, non-number answer) or a grant that
|
|
205
|
+
* would expire before the refill (or within a day after it — it would sit unspent
|
|
206
|
+
* at ~0% used) is claimed as today. Dying-value claims stay exempt: an expiring
|
|
207
|
+
* grant's value cannot survive any wait. */
|
|
143
208
|
export function decideClaudeWithReason(account, listing, now = Date.now(), opts = {}) {
|
|
144
209
|
const skip = reason => ({ action: null, reason });
|
|
145
210
|
if (account?.enabled === false) return skip('account disabled'); // R0
|
|
@@ -178,6 +243,14 @@ export function decideClaudeWithReason(account, listing, now = Date.now(), opts
|
|
|
178
243
|
const weeklyWall = wkUtil != null && wkUtil >= CLAUDE_WALL_WEEKLY;
|
|
179
244
|
// D4: the free refill of a limit < 24h away makes a not-at-the-wall spend redundant.
|
|
180
245
|
const refillSoon = resetAt => resetAt != null && msLeft(resetAt, now) < POLICY.freeRefillWaitMs;
|
|
246
|
+
// R7: hold the GENERAL wall claim for the imminent free refill while other accounts
|
|
247
|
+
// in this pool can absorb the load (wallClaimHeld). A hold NOTES its reason and
|
|
248
|
+
// moves on (red-team #2): the model-scoped loop below must still run, so a dying
|
|
249
|
+
// model grant is spent. `generalHeld` rides on every return after it (red-team r3
|
|
250
|
+
// #2) so the caller registers the hold even when this decision claims a dying model
|
|
251
|
+
// grant — it feeds the pool-wide budget and the held-wall rescue (r2 #1/#2).
|
|
252
|
+
let generalHeld = false;
|
|
253
|
+
const out = (action, why) => ({ action, reason: why, heldScope: generalHeld ? 'general' : null });
|
|
181
254
|
let reason = null;
|
|
182
255
|
const note = r => { if (reason == null) reason = r; };
|
|
183
256
|
|
|
@@ -188,16 +261,21 @@ export function decideClaudeWithReason(account, listing, now = Date.now(), opts
|
|
|
188
261
|
for (const g of general) {
|
|
189
262
|
const giveBack = g.givesBackWeekly === true && !(Array.isArray(g.clears) && g.clears.includes('seven_day'));
|
|
190
263
|
if (weeklyWall) {
|
|
264
|
+
// R7: the free refill is imminent and the pool has room — hold the claim.
|
|
265
|
+
if (wallClaimHeld(q.unified7dReset, g, now,
|
|
266
|
+
{ peersWithRoom: opts.peersWithRoom, holdersInPool: opts.holdersInPool, listingCheckedAt: listing.checkedAt })) {
|
|
267
|
+
generalHeld = true; note(HOLD_POOL_HAS_ROOM); continue;
|
|
268
|
+
}
|
|
191
269
|
// A give-back-only grant (no seven_day in clears) returns only what the model
|
|
192
270
|
// used — the claim snapshots unified7d so the next probe measures the drop.
|
|
193
|
-
return {
|
|
271
|
+
return out({ kind: 'grant', grant: g, scope: 'general', giveBack, reason: 'weekly at wall' }, 'weekly at wall');
|
|
194
272
|
}
|
|
195
273
|
const dying = msLeft(g.endsAt, now) < POLICY.dyingWindowMs
|
|
196
274
|
&& wkUtil != null && wkUtil >= POLICY.dyingMinUtilWeekly; // weekly window only (owner 2026-10-30)
|
|
197
275
|
if (!dying) continue;
|
|
198
276
|
if (g.requiresLimit) { note('requires limit'); continue; }
|
|
199
277
|
if (refillSoon(q.unified7dReset)) { note('free refill <24h away'); continue; }
|
|
200
|
-
return {
|
|
278
|
+
return out({ kind: 'grant', grant: g, scope: 'general', giveBack, reason: 'dying value' }, 'dying value');
|
|
201
279
|
}
|
|
202
280
|
if (weeklyWall && !general.length) note('general not held');
|
|
203
281
|
|
|
@@ -215,14 +293,21 @@ export function decideClaudeWithReason(account, listing, now = Date.now(), opts
|
|
|
215
293
|
const cand = grants.find(g => appliesTo(g).includes(fam));
|
|
216
294
|
if (!cand) continue;
|
|
217
295
|
if (e.utilization >= CLAUDE_WALL_WEEKLY) {
|
|
218
|
-
|
|
296
|
+
// Never held on its own (red-team r3) — but skipped while the GENERAL wall is
|
|
297
|
+
// held: the account is benched by its general wall, so a model reset buys
|
|
298
|
+
// nothing now; the hold re-evaluates every tick and, once the general refill
|
|
299
|
+
// lands (or the hold lets go), this wall claims as before R7. ONLY when the
|
|
300
|
+
// grant outlives the general refill by a day (red-team r4 #1) — one that
|
|
301
|
+
// would expire before then is claimed now, or it is lost.
|
|
302
|
+
if (generalHeld && (cand.endsAt == null || cand.endsAt - q.unified7dReset >= POLICY.dyingWindowMs)) continue;
|
|
303
|
+
return out({ kind: 'grant', grant: cand, scope: fam, reason: `${fam} weekly at wall` }, `${fam} weekly at wall`);
|
|
219
304
|
}
|
|
220
305
|
const dying = msLeft(cand.endsAt, now) < POLICY.dyingWindowMs
|
|
221
306
|
&& e.utilization >= POLICY.dyingMinUtilWeekly;
|
|
222
307
|
if (!dying) continue;
|
|
223
308
|
if (cand.requiresLimit) { note('requires limit'); continue; }
|
|
224
309
|
if (refillSoon(e.resetAt)) { note('free refill <24h away'); continue; }
|
|
225
|
-
return {
|
|
310
|
+
return out({ kind: 'grant', grant: cand, scope: fam, reason: 'dying value' }, 'dying value');
|
|
226
311
|
}
|
|
227
312
|
// A scoped bucket measured at the wall with no grant for it.
|
|
228
313
|
for (const [fam, e] of Object.entries(q.scopedWeekly || {})) {
|
|
@@ -231,5 +316,5 @@ export function decideClaudeWithReason(account, listing, now = Date.now(), opts
|
|
|
231
316
|
}
|
|
232
317
|
|
|
233
318
|
// Session-only grants: NEVER claimed (owner 2026-10-30) — falls through.
|
|
234
|
-
return
|
|
319
|
+
return out(null, reason ?? 'below wall');
|
|
235
320
|
}
|