maxpool 1.24.79 → 1.24.80

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maxpool",
3
- "version": "1.24.79",
3
+ "version": "1.24.80",
4
4
  "description": "Multi-account Claude Code proxy with adaptive, rate-aware load balancing across Claude accounts",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -183,9 +183,31 @@ const DEFAULT_SCHEDULER = {
183
183
  // a conversation's last-used healthy account sheds (every mode except sticky).
184
184
  // On an undiscounted row one in-flight weight unit costs ~12 points past the
185
185
  // per-account target (the bonus is rescaled per row so discounted rows match),
186
- // and a Claude Code turn weighs 3-30 units: 400 ≈ 33 units, about two large
187
- // turns of extra depth before the conversation moves. 0 = pure per-request.
186
+ // and a Claude Code turn weighs 3-30 units: 400 ≈ 33 units of extra depth before
187
+ // the conversation moves. It scales up with conversation size (to 3x) but is then
188
+ // CAPPED at cacheAffinityMaxExtraDepth below (40 units ≈ 480 points), so the
189
+ // effective bonus is 400-480 whatever the size. 0 = pure per-request.
188
190
  cacheAffinityBonus: 400,
191
+ // ANTI-DOGPILE CAP on what the hold may buy (owner hard rule 2026-11-08: many parallel
192
+ // sessions must SPREAD; a hold must never stack conversations on one account until it
193
+ // throttles). The size-scaled bonus above reaches 3x (1200 points ≈ 100 weight units ≈
194
+ // 11 big turns) for a 450KB+ conversation — measured: 11 bound 450KB conversations on
195
+ // one account, depth 110 (old code with the account in weekly reserve: 2, depth 30).
196
+ // This is the most EXTRA depth, in weight units past the account's concurrency target
197
+ // (perAccountConcurrencyTarget, or the tighter reserveConcurrencyTarget for reserve /
198
+ // unlocked-critical), that a hold can ever tolerate on ANY account: 40 ≈ 4 turns of a
199
+ // ~450KB conversation (weight ~10). Priced per row at that row's own unit cost, so a
200
+ // discounted GLM row gets the same DEPTH. Hard bound on a held account's in-flight depth
201
+ // = concTarget + this + one request's weight, whatever the body size or bonus. A reserve /
202
+ // unlocked-critical row's hold is scaled by reserveConcurrencyTarget/perAccountConcurrencyTarget.
203
+ cacheAffinityMaxExtraDepth: 40,
204
+ // HOLD-UNTIL-CANNOT-SERVE (owner 2026-11-08): a bound conversation leaves its last
205
+ // account only when that account truly cannot serve it. A re-store costs the same
206
+ // whether the conversation leaves at 85% or at the wall, so leaving early buys
207
+ // nothing and staying avoids it if the conversation ends / the window resets first.
208
+ // This is the 5-hour-window level at which the hold lets go (providers; Claude
209
+ // benches at switchThreshold anyway). Weekly reserve/soft tiers do NOT release.
210
+ cacheAffinityFiveHourWall: 0.97,
189
211
  concurrencyWeight: 2, // multiplies in-flight load (activeWeight+reqWeight) — dominant
190
212
  perAccountConcurrencyTarget: 3, // D: soft per-account in-flight target; past it, capPenalty bites
191
213
  capPenaltyWeight: 10, // steep penalty per unit of in-flight depth past D (throttle safety floor)
@@ -2358,7 +2380,9 @@ export class AccountManager {
2358
2380
  const ckey = this._affinityKey(requestInfo);
2359
2381
  const lastName = ckey ? this.conversationLastAccount.get(ckey) : null;
2360
2382
  if (lastName && lastName !== best.name) {
2361
- console.log(`[Maxpool] conversation moved: ${lastName} → ${best.name} (${requestInfo.bodyBytes ?? '?'}B)`);
2383
+ let why = 'unknown';
2384
+ try { why = this._moveReason(lastName, best, requestInfo, excludedIndexes, scoringCtx, weeklyOptions, bestUnlockMap.get(best.name) || null, now); } catch (e) { why = `error:${e.message}`; }
2385
+ console.log(`[Maxpool] conversation moved: ${lastName} → ${best.name} (${requestInfo.bodyBytes ?? '?'}B) why=${why}`);
2362
2386
  }
2363
2387
  return best;
2364
2388
  }
@@ -3066,10 +3090,10 @@ export class AccountManager {
3066
3090
  return this._holdsUsableReset(account, now) ? 1 - Math.min(d, 0.9) : 1;
3067
3091
  }
3068
3092
 
3069
- _scoreAccount(account, requestInfo = {}, ctx = null, criticalUnlock = null) {
3093
+ _scoreTerms(account, requestInfo = {}, ctx = null, criticalUnlock = null) {
3070
3094
  const now = ctx?.now ?? Date.now();
3071
3095
  // CACHE AFFINITY — see _cacheAffinity.
3072
- const affinity = this._cacheAffinity(account, requestInfo);
3096
+ const affinity = this._cacheAffinity(account, requestInfo, criticalUnlock);
3073
3097
  const reqWeight = Math.max(1, requestInfo.weight || 1);
3074
3098
  // POOL LINK scores as the cloud's WORST-case load (owner 2026-10-29: "the team
3075
3099
  // pool will respect the accounts within it... included in the balancing act").
@@ -3173,15 +3197,24 @@ export class AccountManager {
3173
3197
  const reserveCost = this._reserveCost(account, now, weeklyState);
3174
3198
  const criticalCost = this._criticalCost(account, now, criticalUnlock, weeklyState);
3175
3199
  const failurePenalty = account.consecutiveFailures * 5;
3176
- // NO unknown-quota bonus. An account whose quota we cannot see must never be
3177
- // MORE attractive than a known-healthy one — the old -0.5 nudge (safe only
3178
- // while the prober quickly resolved "unknown") turned into a relentless pull
3179
- // toward blind accounts once probing was off, driving an out-of-band-burned
3180
- // account to exhaustion. Unknown now scores neutral; the prober (on by
3181
- // default) learns the real number within a cycle. `probing`/requalify still
3182
- // flags a never-seen account for learning — that path is unchanged.
3183
-
3184
- return Math.max(0, concurrency + capPenalty + paceCost + utilizationCost + scopedPace + spread + ramp + reserveCost + criticalCost + failurePenalty - affinity);
3200
+ // HELD conversation: the quota/steering terms (pace, utilization, scoped pace,
3201
+ // spread, recovery ramp, reserve/critical overflow cost) exist to steer NEW work
3202
+ // away from tight accounts. They must not, by themselves, pull an already-bound
3203
+ // conversation off an account that can still serve it — a re-store costs more
3204
+ // than any of them. Zeroed for the bound account only while the hold is live; the
3205
+ // LOAD terms (concurrency, capPenalty) stay, so the load-gap escape is untouched.
3206
+ // Structural, not a bonus-size accident: holds at any cacheAffinityBonus > the load gap.
3207
+ const held = affinity > 0;
3208
+ const steer = held ? { paceCost: 0, utilizationCost: 0, scopedPace: 0, spread: 0, ramp: 0, reserveCost: 0, criticalCost: 0 }
3209
+ : { paceCost, utilizationCost, scopedPace, spread, ramp, reserveCost, criticalCost };
3210
+ const terms = { concurrency, capPenalty, ...steer, failurePenalty, affinity };
3211
+ return terms;
3212
+ }
3213
+
3214
+ /** Routing cost — lower is preferred. Sum of the named terms minus the affinity bonus. */
3215
+ _scoreAccount(account, requestInfo = {}, ctx = null, criticalUnlock = null) {
3216
+ const t = this._scoreTerms(account, requestInfo, ctx, criticalUnlock);
3217
+ return Math.max(0, t.concurrency + t.capPenalty + t.paceCost + t.utilizationCost + t.scopedPace + t.spread + t.ramp + t.reserveCost + t.criticalCost + t.failurePenalty - t.affinity);
3185
3218
  }
3186
3219
 
3187
3220
  /**
@@ -3259,7 +3292,35 @@ export class AccountManager {
3259
3292
  }
3260
3293
  }
3261
3294
 
3262
- _cacheAffinity(account, requestInfo = {}) {
3295
+ /**
3296
+ * HOLD-UNTIL-CANNOT-SERVE (owner 2026-11-08). Returns null while the hold is live,
3297
+ * or the NAME of the gate that lets the conversation go. The hold survives the
3298
+ * weekly reserve (85%) and soft tiers and a high-but-not-walled 5-hour window: a
3299
+ * move costs one full re-store whenever it happens, so leaving early only spends
3300
+ * the re-store sooner. It releases only when the account truly cannot serve:
3301
+ * critical-no-unlock / exhausted / capped weekly, the 5h window at/over
3302
+ * cacheAffinityFiveHourWall, a failure streak, or Kimi's unreadable console-only
3303
+ * quota (nothing would ever release the hold before a 429). Availability gates
3304
+ * (cooldown, 429 bench, exclusion, in-flight cap) never reach here — the account
3305
+ * is not a candidate at all — so a throttled account releases on the very next request.
3306
+ */
3307
+ _affinityRelease(account, criticalUnlock = null) {
3308
+ if (account.type === 'provider' && account.quota?.providerQuotaSource === 'console-only') return 'console-only';
3309
+ const weeklyState = this._weeklyRawState(account);
3310
+ if (weeklyState === 'critical') {
3311
+ if (!criticalUnlock) return 'critical-no-unlock';
3312
+ } else if (weeklyState === 'exhausted' || weeklyState === 'capped') {
3313
+ return `weekly-${weeklyState}`;
3314
+ }
3315
+ // 5-hour window: Anthropic reports unified5h; GLM/Kimi report providerSes.
3316
+ const five = account.type === 'provider' ? account.quota?.providerSes : account.quota?.unified5h;
3317
+ if (five != null && five >= this.scheduler.cacheAffinityFiveHourWall) return `5h-${five.toFixed(2)}`;
3318
+ const failures = account.consecutiveFailures || 0;
3319
+ if (failures > 0) return `failures-${failures}`;
3320
+ return null;
3321
+ }
3322
+
3323
+ _cacheAffinity(account, requestInfo = {}, criticalUnlock = null) {
3263
3324
  const base = Number(this.scheduler.cacheAffinityBonus) || 0;
3264
3325
  // Sticky mode has its own binding; its score loop runs only once the session has
3265
3326
  // decided to LEAVE a hot account — a discount there would pull it straight back.
@@ -3272,17 +3333,7 @@ export class AccountManager {
3272
3333
  // the rest were 100K-500K-token conversations whose single in-flight turn
3273
3334
  // already outweighed it. 1x below 150KB, rising to 3x at 450KB+.
3274
3335
  const sizeMult = Math.min(3, Math.max(1, (requestInfo.bodyBytes || 0) / 150_000));
3275
- // No readable quota (Kimi's coding key is console-only) → nothing would ever
3276
- // release the hold before a 429; give such rows plain balancing (red-team r1).
3277
- if (account.type === 'provider' && account.quota?.providerQuotaSource === 'console-only') return 0;
3278
- const weeklyState = this._weeklyRawState(account);
3279
- // Holds through 'soft' (≥65% weekly — most accounts by mid-week) and 'unknown'
3280
- // (never probed); reserve/critical/exhausted/capped release the conversation.
3281
- if (!['normal', 'soft', 'unknown'].includes(weeklyState)) return 0;
3282
- // 5-hour window: Anthropic reports unified5h; GLM/Kimi report providerSes.
3283
- const five = account.type === 'provider' ? account.quota?.providerSes : account.quota?.unified5h;
3284
- if (five != null && five >= this.scheduler.weeklyReserveThreshold) return 0;
3285
- if ((account.consecutiveFailures || 0) > 0) return 0;
3336
+ if (this._affinityRelease(account, criticalUnlock)) return 0;
3286
3337
  // Scale to THIS row's price of one in-flight unit, so every account tolerates the
3287
3338
  // same extra DEPTH (~33 units) — a fast-refill/soak-discounted GLM row prices a
3288
3339
  // unit at ~0.4x, and an unscaled bonus let it run ~2.5x deeper (red-team r1).
@@ -3291,7 +3342,61 @@ export class AccountManager {
3291
3342
  ? this._fastRefillMultiplier(account, { capFloor: true }) : refillMult;
3292
3343
  const unit = this.scheduler.concurrencyWeight * refillMult + this.scheduler.capPenaltyWeight * safetyMult;
3293
3344
  const fullUnit = this.scheduler.concurrencyWeight + this.scheduler.capPenaltyWeight;
3294
- return base * sizeMult * Math.min(1, unit / fullUnit);
3345
+ const bonus = base * sizeMult * Math.min(1, unit / fullUnit);
3346
+ // Anti-dogpile cap: never tolerate more than cacheAffinityMaxExtraDepth units of
3347
+ // extra depth at THIS row's unit price, however large the conversation or bonus.
3348
+ let maxExtra = Number(this.scheduler.cacheAffinityMaxExtraDepth);
3349
+ if (!Number.isFinite(maxExtra) || maxExtra < 0) return bonus;
3350
+ // A reserve (or unlocked-critical) account has the TIGHTER concurrency target; its
3351
+ // hold gets the same tightening so the hold never stretches the lowest-headroom tier
3352
+ // as far as a healthy one (reserveConcurrencyTarget / perAccountConcurrencyTarget).
3353
+ const wk = this._weeklyRawState(account);
3354
+ if (wk === 'reserve' || (wk === 'critical' && criticalUnlock)) {
3355
+ maxExtra *= Math.min(1, this.scheduler.reserveConcurrencyTarget / Math.max(1, this.scheduler.perAccountConcurrencyTarget));
3356
+ }
3357
+ return Math.min(bonus, maxExtra * unit);
3358
+ }
3359
+
3360
+ /**
3361
+ * WHY a known conversation left its last account — one short string for the
3362
+ * `conversation moved:` log line. `released:<gate>` = the hold let go (the gate
3363
+ * names the reason, incl. availability causes); `outscored:` = the hold was live and
3364
+ * the sibling still won, with the dominant score terms that beat the bonus.
3365
+ */
3366
+ _moveReason(lastName, best, requestInfo, excludedIndexes, scoringCtx, weeklyOptions, bestUnlock, now) {
3367
+ const last = this.accounts.find(a => a.name === lastName);
3368
+ if (!last) return 'released:account-gone';
3369
+ if (excludedIndexes.has(last.index)) return 'released:excluded';
3370
+ if (!this._matchesRequest(last, requestInfo.profile || 'claude', requestInfo)) return 'released:not-eligible';
3371
+ const unlock = weeklyOptions.allowWeeklyCritical ? null : this._criticalUnlock(last, requestInfo, excludedIndexes, null, now);
3372
+ const avail = { ...weeklyOptions, allowWeeklyCritical: weeklyOptions.allowWeeklyCritical || !!unlock, model: requestInfo.model, now };
3373
+ if (!this._isAvailable(last, avail)) return `released:unavailable(${this._unavailableCause(last, now)})`;
3374
+ const gate = this._affinityRelease(last, unlock);
3375
+ if (gate) return `released:${gate}`;
3376
+ if (this._effectivePriority(last, requestInfo, now) > this._effectivePriority(best, requestInfo, now)) return 'released:priority-tier';
3377
+ if (this._cacheAffinity(last, requestInfo, unlock) <= 0) return 'released:no-hold';
3378
+ const lt = this._scoreTerms(last, requestInfo, scoringCtx, unlock);
3379
+ const bt = this._scoreTerms(best, requestInfo, scoringCtx, bestUnlock);
3380
+ const sum = t => t.concurrency + t.capPenalty + t.paceCost + t.utilizationCost + t.scopedPace + t.spread + t.ramp + t.reserveCost + t.criticalCost + t.failurePenalty - t.affinity;
3381
+ const delta = sum(lt) - sum(bt);
3382
+ const diffs = ['concurrency', 'capPenalty', 'paceCost', 'utilizationCost', 'scopedPace', 'spread', 'ramp', 'reserveCost', 'criticalCost', 'failurePenalty', 'affinity']
3383
+ .map(k => [k, (k === 'affinity' ? -(lt[k] - bt[k]) : lt[k] - bt[k])])
3384
+ .filter(([, d]) => d > 0.05).sort((x, y) => y[1] - x[1]).slice(0, 2)
3385
+ .map(([k, d]) => `${k}+${d.toFixed(1)}`);
3386
+ return `outscored:${delta.toFixed(1)}(${diffs.join(',') || 'rank'};bonus=${lt.affinity.toFixed(0)};depth=${last.activeWeight}/${best.activeWeight})`;
3387
+ }
3388
+
3389
+ /** Short human cause for a not-available account (log text only). */
3390
+ _unavailableCause(account, now) {
3391
+ if (!account.enabled) return 'disabled';
3392
+ if (account.status === 'throttled' && account.rateLimitedUntil && now < account.rateLimitedUntil) return 'rate-limited';
3393
+ if (account.cooldownUntil && now < account.cooldownUntil) return 'cooldown';
3394
+ if (account.provisionalUpstreamUntil && now < account.provisionalUpstreamUntil) return 'upstream-failure';
3395
+ if (account.inFlight >= this.scheduler.safetyMaxActivePerAccount) return 'in-flight-cap';
3396
+ if (this._isSessionQuotaUnavailable(account, now)) return '5h-wall';
3397
+ const w = this._weeklyRawState(account, now);
3398
+ if (w !== 'normal' && w !== 'soft' && w !== 'unknown') return `weekly-${w}`;
3399
+ return account.status || 'other';
3295
3400
  }
3296
3401
 
3297
3402
  /**