maxpool 1.24.79 → 1.24.81

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.81",
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",
@@ -4,6 +4,9 @@ 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
 
7
+ // Review r2 A: consecutive request-scoped faults before they count against the account.
8
+ const REQUEST_FAULT_ESCALATE = 3;
9
+
7
10
  // Nominal window length per kind — mirrors capacity-ledger's WINDOW_MS (kept here as a
8
11
  // local table so account-manager does not import a private constant).
9
12
  const WINDOW_MS_BY_KIND = { ses: 5 * 3600_000, wk: 7 * 86400_000 };
@@ -183,10 +186,38 @@ const DEFAULT_SCHEDULER = {
183
186
  // a conversation's last-used healthy account sheds (every mode except sticky).
184
187
  // On an undiscounted row one in-flight weight unit costs ~12 points past the
185
188
  // 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.
189
+ // and a Claude Code turn weighs 3-30 units: 400 ≈ 33 units of extra depth before
190
+ // the conversation moves. It scales up with conversation size (to 3x) but is then
191
+ // CAPPED at cacheAffinityMaxExtraDepth below (40 units ≈ 480 points), so the
192
+ // effective bonus is 400-480 whatever the size. 0 = pure per-request.
188
193
  cacheAffinityBonus: 400,
189
- concurrencyWeight: 2, // multiplies in-flight load (activeWeight+reqWeight) — dominant
194
+ // ANTI-DOGPILE CAP on what the hold may buy (owner hard rule 2026-11-08: many parallel
195
+ // sessions must SPREAD; a hold must never stack conversations on one account until it
196
+ // throttles). The size-scaled bonus above reaches 3x (1200 points ≈ 100 weight units ≈
197
+ // 11 big turns) for a 450KB+ conversation — measured: 11 bound 450KB conversations on
198
+ // one account, depth 110 (old code with the account in weekly reserve: 2, depth 30).
199
+ // This is the most EXTRA depth, in weight units past the account's concurrency target
200
+ // (perAccountConcurrencyTarget, or the tighter reserveConcurrencyTarget for reserve /
201
+ // unlocked-critical), that a hold can ever tolerate on ANY account: 40 ≈ 4 turns of a
202
+ // ~450KB conversation (weight ~10). Priced per row at that row's own unit cost, so a
203
+ // discounted GLM row gets the same DEPTH. Hard bound on a held account's in-flight depth
204
+ // = concTarget + this + one request's weight, whatever the body size or bonus. A reserve /
205
+ // unlocked-critical row's hold is scaled by reserveConcurrencyTarget/perAccountConcurrencyTarget.
206
+ cacheAffinityMaxExtraDepth: 40,
207
+ // HOLD-UNTIL-CANNOT-SERVE (owner 2026-11-08): a bound conversation leaves its last
208
+ // account only when that account truly cannot serve it. A re-store costs the same
209
+ // whether the conversation leaves at 85% or at the wall, so leaving early buys
210
+ // nothing and staying avoids it if the conversation ends / the window resets first.
211
+ // This is the 5-hour-window level at which the hold lets go (providers; Claude
212
+ // benches at switchThreshold anyway). Weekly reserve/soft tiers do NOT release.
213
+ cacheAffinityFiveHourWall: 0.97,
214
+ // RETURN HOME after a provider detour (owner 2026-11-09): when a conversation's turn
215
+ // went to GLM/Kimi and a later turn is routed back to Claude, it prefers the Claude
216
+ // account it used last — that account still holds its stored copy. Only while the copy
217
+ // is alive: Claude Code writes 1-hour copies (measured 2026-11-09: 272 of 272 cache
218
+ // writes `ephemeral_1h`; 55 of 64 detours came back within the hour, median 533 s).
219
+ cacheAffinityReturnWindowMs: 3_600_000,
220
+ concurrencyWeight: 2, // multiplies in-flight load (activeWeight+reqWeight) — dominant
190
221
  perAccountConcurrencyTarget: 3, // D: soft per-account in-flight target; past it, capPenalty bites
191
222
  capPenaltyWeight: 10, // steep penalty per unit of in-flight depth past D (throttle safety floor)
192
223
  paceCostWeight: 1.5, // soft de-preference of accounts burning ahead of pace (was the ×6 term)
@@ -343,6 +374,15 @@ const PERSISTED_QUOTA_FIELDS = [
343
374
  ];
344
375
 
345
376
  const CONVERSATION_AFFINITY_MAX = 5000;
377
+ /** Bounded LRU insert: re-insert = most recent; the oldest key drops past the cap. */
378
+ function lruSet(map, key, value) {
379
+ map.delete(key);
380
+ map.set(key, value);
381
+ while (map.size > CONVERSATION_AFFINITY_MAX) map.delete(map.keys().next().value);
382
+ }
383
+ /** An Anthropic account with its own prompt cache (allow-list: a provider or legacy
384
+ * pool row can forward to GLM, so "not a provider" is not proof of a Claude cache). */
385
+ const isClaudeAccount = (a) => a?.type === 'oauth' || a?.type === 'apikey';
346
386
  const IDENTITY_WINDOW_MS = 15 * 60_000;
347
387
  const IDENTITY_MAX_SAMPLES = 4000;
348
388
  const IDENTITY_MIN_PER_SESSION = 8;
@@ -609,6 +649,9 @@ export class AccountManager {
609
649
  // Bounded LRU — oldest conversations drop first; a dropped entry only means the
610
650
  // next turn is balanced fresh.
611
651
  this.conversationLastAccount = new Map();
652
+ // Same key → { name, at } of the last CLAUDE account only (a GLM/Kimi turn never
653
+ // overwrites it), so a conversation returning from a provider detour can go home.
654
+ this.conversationLastClaudeAccount = new Map();
612
655
  // Conversation-identity HEALTH (owner 2026-11-08: "make sure this cannot degrade
613
656
  // silently"). Twice in one day a Claude Code request-shape detail (a moving
614
657
  // cache_control marker, then a per-request billing checksum) made every turn hash
@@ -884,7 +927,11 @@ export class AccountManager {
884
927
  const prevCurrentName = requestInfo.sessionKey
885
928
  ? this._sessionBinding(requestInfo.sessionKey)?.currentName
886
929
  : null;
930
+ requestInfo.returnedHome = null; // set by the selection loop when it sends the turn home
887
931
  const account = this.getActiveAccount(requestInfo, excludedIndexes);
932
+ // The D3 retry-same hint is good for exactly ONE acquire, honored or not — a stale
933
+ // hint would pull a later failover hop back onto the account it just left.
934
+ requestInfo.retrySameAccountName = null;
888
935
  if (!account) return null;
889
936
 
890
937
  const weight = Math.max(1, Number(requestInfo.weight) || 1);
@@ -902,7 +949,9 @@ export class AccountManager {
902
949
  this.notePoolSend(account);
903
950
  // Non-null only when this acquire moved the session off its prior account.
904
951
  const migratedFromName = (prevCurrentName && account.name !== prevCurrentName) ? prevCurrentName : null;
905
- return { account, weight, startedAt: Date.now(), upstreamThrottleProbe, migratedFromName };
952
+ const returnedHome = requestInfo.returnedHome?.name === account.name;
953
+ const returnedHomeSwitched = returnedHome && requestInfo.returnedHome.switched === true;
954
+ return { account, weight, startedAt: Date.now(), upstreamThrottleProbe, migratedFromName, returnedHome, returnedHomeSwitched };
906
955
  }
907
956
 
908
957
  /** Fail-safe: snap a session's binding back to the pre-migration issuing account
@@ -1013,13 +1062,31 @@ export class AccountManager {
1013
1062
  return out;
1014
1063
  }
1015
1064
 
1065
+ /**
1066
+ * outcome.neutral — the attempt says nothing about the account (client cancel,
1067
+ * request-shape refusal): no counters move at all.
1068
+ * outcome.scope — 'account' (default) | 'request'. A request-scoped failure is
1069
+ * recorded (failedRequests, lastError) but never extends the
1070
+ * consecutiveFailures streak, so it cannot trip the hold-release
1071
+ * gate for the account's other conversations.
1072
+ */
1016
1073
  releaseAccount(lease, outcome = {}) {
1017
1074
  if (!lease?.account) return;
1018
1075
  const account = lease.account;
1019
1076
 
1077
+ const depthBefore = account.activeWeight;
1020
1078
  account.inFlight = Math.max(0, account.inFlight - 1);
1021
1079
  account.activeWeight = Math.max(0, account.activeWeight - lease.weight);
1022
1080
 
1081
+ // Owner's throttling constraint: make any throttle on a return-home turn countable,
1082
+ // and say whether the switch (vs the loop's own pick) put the turn on home.
1083
+ if (lease.returnedHome && (outcome.status === 429 || (outcome.status === 503 && account.remote)) && outcome.error !== 'request_shape_refused') {
1084
+ // A pool-wide throttle (markUpstreamThrottled) is not about home's depth — tag it so
1085
+ // the post-release count can leave it out.
1086
+ const scope = outcome.upstreamThrottled ? ' pool-wide' : '';
1087
+ console.log(`[Maxpool] return-home lease throttled (${outcome.status} ${outcome.error || '-'}${scope}, ${lease.returnedHomeSwitched ? 'switched' : 'natural'}): ${account.name} depth=${depthBefore}`);
1088
+ }
1089
+
1023
1090
  if (lease.upstreamThrottleProbe) {
1024
1091
  if (outcome.success) {
1025
1092
  this.clearUpstreamThrottle('successful recovery probe');
@@ -1038,6 +1105,10 @@ export class AccountManager {
1038
1105
  if (outcome.success) {
1039
1106
  account.completedRequests++;
1040
1107
  account.consecutiveFailures = 0;
1108
+ // Review r2 A: any success ends a request-scoped fault run.
1109
+ account.requestFaultStreak = 0;
1110
+ account.requestFaultEscalated = false;
1111
+ account.softCooldownUntil = null;
1041
1112
  account.providerRateLimitStreak = 0;
1042
1113
  account.lastStatus = outcome.status || account.lastStatus;
1043
1114
  account.lastResponseMs = Date.now() - lease.startedAt;
@@ -1056,7 +1127,12 @@ export class AccountManager {
1056
1127
 
1057
1128
  if (outcome.error || outcome.status) {
1058
1129
  account.failedRequests++;
1059
- account.consecutiveFailures++;
1130
+ // scope:'request' (cancel-adjacent / first-byte timeout / link blip): the fault
1131
+ // belongs to THIS request, not the account. It must not bump the streak that
1132
+ // the hold-release gate (_affinityRelease `failures-N`) reads — one such fault
1133
+ // used to evict every conversation held on the account (4 in 1s, 2026-11-09
1134
+ // 08:13Z). Account-level faults (429 / caps / auth) keep the default 'account'.
1135
+ if (outcome.scope !== 'request') account.consecutiveFailures++;
1060
1136
  account.lastStatus = outcome.status || account.lastStatus;
1061
1137
  account.lastResponseMs = Date.now() - lease.startedAt;
1062
1138
  this._recordLoadEvent(account, lease, outcome);
@@ -1123,6 +1199,21 @@ export class AccountManager {
1123
1199
  return exhausted ? { resetAt: e.resetAt || null, family: fam } : null;
1124
1200
  }
1125
1201
 
1202
+ /** Review r2 A: inside a request-scoped soft cooldown (new placements only). */
1203
+ _softCooling(account, now = Date.now()) {
1204
+ return Boolean(account.softCooldownUntil && now < account.softCooldownUntil);
1205
+ }
1206
+
1207
+ /** Does `account` already hold this request's conversation (so a soft cooldown must
1208
+ * not move it)? The one-shot retry-same hint, or the last account for the affinity
1209
+ * key (which falls back to the sessionKey, so a sticky binding is covered). A
1210
+ * return-home target needs no clause: homeCand is captured before the skip. */
1211
+ _holdsRequest(account, requestInfo = {}) {
1212
+ if (requestInfo.retrySameAccountName === account.name) return true;
1213
+ const key = this._affinityKey(requestInfo);
1214
+ return Boolean(key && this.conversationLastAccount.get(key) === account.name);
1215
+ }
1216
+
1126
1217
  _isAvailable(account, options = {}) {
1127
1218
  if (!account) return false;
1128
1219
  if (!account.enabled) return false;
@@ -2303,10 +2394,13 @@ export class AccountManager {
2303
2394
  ];
2304
2395
 
2305
2396
  const bestUnlockMap = new Map();
2397
+ const home = this._returnHome(requestInfo, now);
2306
2398
  for (const weeklyOptions of weeklyPasses) {
2307
2399
  best = null;
2308
2400
  bestScore = Infinity;
2309
2401
  bestPriority = Infinity;
2402
+ let homeCand = null; // the home account as a candidate THIS pass, scored as holder
2403
+ let coolBest = null; // best soft-cooled account (review r2 A): used only if nothing else fits
2310
2404
 
2311
2405
  for (let i = 0; i < this.accounts.length; i++) {
2312
2406
  const idx = (this.nextIndex + i) % this.accounts.length;
@@ -2322,6 +2416,20 @@ export class AccountManager {
2322
2416
 
2323
2417
  const priority = this._effectivePriority(account, requestInfo, now);
2324
2418
  const score = this._scoreAccount(account, requestInfo, scoringCtx, critUnlock);
2419
+ if (home && account.name === home.name) {
2420
+ homeCand = { account, priority, critUnlock,
2421
+ score: this._scoreAccount(account, requestInfo, scoringCtx, critUnlock, home.name) };
2422
+ }
2423
+ // REQUEST-SCOPED SOFT COOLDOWN (review r2 A): after a request-scoped first-byte
2424
+ // timeout the account is deprioritised for NEW placements only — a cold pick
2425
+ // takes it last. A conversation it HOLDS (_holdsRequest) is not touched:
2426
+ // evicting those is exactly what a request-scoped fault must never do.
2427
+ if (this._softCooling(account, now) && !this._holdsRequest(account, requestInfo)) {
2428
+ if (!coolBest || priority < coolBest.priority || (priority === coolBest.priority && score < coolBest.score)) {
2429
+ coolBest = { account, priority, score, critUnlock };
2430
+ }
2431
+ continue;
2432
+ }
2325
2433
  if (priority < bestPriority || (priority === bestPriority && score < bestScore)) {
2326
2434
  bestPriority = priority;
2327
2435
  bestScore = score;
@@ -2330,6 +2438,47 @@ export class AccountManager {
2330
2438
  }
2331
2439
  }
2332
2440
 
2441
+ // Deprioritised, never benched: with no other candidate, the soft-cooled one serves.
2442
+ if (!best && coolBest) {
2443
+ best = coolBest.account;
2444
+ bestPriority = coolBest.priority;
2445
+ bestScore = coolBest.score;
2446
+ if (coolBest.critUnlock) bestUnlockMap.set(best.name, coolBest.critUnlock); else bestUnlockMap.delete(best.name);
2447
+ }
2448
+
2449
+ // RETURN HOME: only once this pass already picked a Claude account — a provider
2450
+ // pick is never touched, so this never pulls a conversation off GLM sooner. Home
2451
+ // wins when it is a candidate here, in the same priority tier, and its score AS
2452
+ // HOLDER (release gates, zeroed steering, anti-dogpile cap) beats the pick.
2453
+ let homeNote = null;
2454
+ let homeSwitched = false;
2455
+ const pickedBeforeHome = best;
2456
+ if (best && home && isClaudeAccount(best)) {
2457
+ if (best.name === home.name) {
2458
+ homeNote = `home=${home.name}:returned`;
2459
+ } else if (home.stale) {
2460
+ homeNote = `home=${home.name}:skipped(stale-${Math.round(home.ageMs / 60_000)}m)`;
2461
+ } else if (homeCand && homeCand.priority <= bestPriority && homeCand.score < bestScore) {
2462
+ // `<=`, not `===`: a soft-cooled HOME is skipped from the best-pick above but
2463
+ // still recorded in homeCand — home at equal-or-better priority wins even then
2464
+ // (review r3 R3-5: soft-cooldown must never override return-home).
2465
+ best = homeCand.account;
2466
+ homeSwitched = true;
2467
+ if (homeCand.critUnlock) bestUnlockMap.set(best.name, homeCand.critUnlock); else bestUnlockMap.delete(best.name);
2468
+ homeNote = `home=${home.name}:returned`;
2469
+ } else if (homeCand && this._softCooling(homeCand.account, now)) {
2470
+ // R3-5: a soft-cooled home that did NOT win as holder: name the soft cooldown
2471
+ // (it kept home out of the best-pick) instead of an `outscored:` line computed
2472
+ // as if home had competed there.
2473
+ homeNote = `home=${home.name}:skipped(soft-cooled;${homeCand.priority > bestPriority ? 'priority-tier' : `holder-score ${homeCand.score.toFixed(1)}>=pick ${bestScore.toFixed(1)}`})`;
2474
+ } else {
2475
+ let why = 'unknown';
2476
+ try { why = this._moveReason(home.name, best, requestInfo, excludedIndexes, scoringCtx, weeklyOptions, bestUnlockMap.get(best.name) || null, now, true); } catch (e) { why = `error:${e.message}`; }
2477
+ homeNote = `home=${home.name}:skipped(${why})`;
2478
+ }
2479
+ if (best.name === home.name) requestInfo.returnedHome = { name: home.name, switched: homeSwitched };
2480
+ }
2481
+
2333
2482
  if (best) {
2334
2483
  const switched = best.index !== this.currentIndex;
2335
2484
  this.currentIndex = best.index;
@@ -2358,7 +2507,11 @@ export class AccountManager {
2358
2507
  const ckey = this._affinityKey(requestInfo);
2359
2508
  const lastName = ckey ? this.conversationLastAccount.get(ckey) : null;
2360
2509
  if (lastName && lastName !== best.name) {
2361
- console.log(`[Maxpool] conversation moved: ${lastName} → ${best.name} (${requestInfo.bodyBytes ?? '?'}B)`);
2510
+ // why= explains why the conversation LEFT its last account — measured against
2511
+ // the loop's own pick, before any return-home switch.
2512
+ let why = 'unknown';
2513
+ try { why = this._moveReason(lastName, pickedBeforeHome, requestInfo, excludedIndexes, scoringCtx, weeklyOptions, bestUnlockMap.get(pickedBeforeHome.name) || null, now); } catch (e) { why = `error:${e.message}`; }
2514
+ console.log(`[Maxpool] conversation moved: ${lastName} → ${best.name} (${requestInfo.bodyBytes ?? '?'}B) why=${why}${homeNote ? ` ${homeNote}` : ''}`);
2362
2515
  }
2363
2516
  return best;
2364
2517
  }
@@ -3066,10 +3219,10 @@ export class AccountManager {
3066
3219
  return this._holdsUsableReset(account, now) ? 1 - Math.min(d, 0.9) : 1;
3067
3220
  }
3068
3221
 
3069
- _scoreAccount(account, requestInfo = {}, ctx = null, criticalUnlock = null) {
3222
+ _scoreTerms(account, requestInfo = {}, ctx = null, criticalUnlock = null, holderName = undefined) {
3070
3223
  const now = ctx?.now ?? Date.now();
3071
3224
  // CACHE AFFINITY — see _cacheAffinity.
3072
- const affinity = this._cacheAffinity(account, requestInfo);
3225
+ const affinity = this._cacheAffinity(account, requestInfo, criticalUnlock, holderName);
3073
3226
  const reqWeight = Math.max(1, requestInfo.weight || 1);
3074
3227
  // POOL LINK scores as the cloud's WORST-case load (owner 2026-10-29: "the team
3075
3228
  // pool will respect the accounts within it... included in the balancing act").
@@ -3173,15 +3326,24 @@ export class AccountManager {
3173
3326
  const reserveCost = this._reserveCost(account, now, weeklyState);
3174
3327
  const criticalCost = this._criticalCost(account, now, criticalUnlock, weeklyState);
3175
3328
  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);
3329
+ // HELD conversation: the quota/steering terms (pace, utilization, scoped pace,
3330
+ // spread, recovery ramp, reserve/critical overflow cost) exist to steer NEW work
3331
+ // away from tight accounts. They must not, by themselves, pull an already-bound
3332
+ // conversation off an account that can still serve it — a re-store costs more
3333
+ // than any of them. Zeroed for the bound account only while the hold is live; the
3334
+ // LOAD terms (concurrency, capPenalty) stay, so the load-gap escape is untouched.
3335
+ // Structural, not a bonus-size accident: holds at any cacheAffinityBonus > the load gap.
3336
+ const held = affinity > 0;
3337
+ const steer = held ? { paceCost: 0, utilizationCost: 0, scopedPace: 0, spread: 0, ramp: 0, reserveCost: 0, criticalCost: 0 }
3338
+ : { paceCost, utilizationCost, scopedPace, spread, ramp, reserveCost, criticalCost };
3339
+ const terms = { concurrency, capPenalty, ...steer, failurePenalty, affinity };
3340
+ return terms;
3341
+ }
3342
+
3343
+ /** Routing cost — lower is preferred. Sum of the named terms minus the affinity bonus. */
3344
+ _scoreAccount(account, requestInfo = {}, ctx = null, criticalUnlock = null, holderName = undefined) {
3345
+ const t = this._scoreTerms(account, requestInfo, ctx, criticalUnlock, holderName);
3346
+ 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
3347
  }
3186
3348
 
3187
3349
  /**
@@ -3205,10 +3367,29 @@ export class AccountManager {
3205
3367
  _noteConversationAccount(requestInfo, account) {
3206
3368
  const key = this._affinityKey(requestInfo);
3207
3369
  if (!key) return;
3208
- const map = this.conversationLastAccount;
3209
- map.delete(key); // re-insert = most recent (LRU order)
3210
- map.set(key, account.name);
3211
- while (map.size > CONVERSATION_AFFINITY_MAX) map.delete(map.keys().next().value);
3370
+ lruSet(this.conversationLastAccount, key, account.name);
3371
+ if (isClaudeAccount(account)) {
3372
+ lruSet(this.conversationLastClaudeAccount, key, { name: account.name, at: requestInfo.now ?? Date.now() });
3373
+ }
3374
+ }
3375
+
3376
+ /**
3377
+ * RETURN HOME (owner 2026-11-09): the Claude account a conversation should go back to
3378
+ * after a provider detour, or null. A detour = the last account differs from the last
3379
+ * CLAUDE account (the Claude memory only ever holds Claude names). That includes the
3380
+ * case where the last-account entry was evicted but the Claude entry survived: going
3381
+ * home then restores the evicted hold, which is harmless. `stale` when the stored copy
3382
+ * has outlived cacheAffinityReturnWindowMs — going home then buys nothing.
3383
+ */
3384
+ _returnHome(requestInfo, now = Date.now()) {
3385
+ const key = this._affinityKey(requestInfo);
3386
+ if (!key) return null;
3387
+ const home = this.conversationLastClaudeAccount.get(key);
3388
+ if (!home || home.name === this.conversationLastAccount.get(key)) return null;
3389
+ const ageMs = now - home.at;
3390
+ let windowMs = Number(this.scheduler.cacheAffinityReturnWindowMs);
3391
+ if (!Number.isFinite(windowMs) || windowMs < 0) windowMs = DEFAULT_SCHEDULER.cacheAffinityReturnWindowMs;
3392
+ return { name: home.name, stale: ageMs > windowMs, ageMs };
3212
3393
  }
3213
3394
 
3214
3395
  /** Conversation identity: terminal + prefix hash when the server computed one;
@@ -3259,30 +3440,60 @@ export class AccountManager {
3259
3440
  }
3260
3441
  }
3261
3442
 
3262
- _cacheAffinity(account, requestInfo = {}) {
3443
+ /**
3444
+ * HOLD-UNTIL-CANNOT-SERVE (owner 2026-11-08). Returns null while the hold is live,
3445
+ * or the NAME of the gate that lets the conversation go. The hold survives the
3446
+ * weekly reserve (85%) and soft tiers and a high-but-not-walled 5-hour window: a
3447
+ * move costs one full re-store whenever it happens, so leaving early only spends
3448
+ * the re-store sooner. It releases only when the account truly cannot serve:
3449
+ * critical-no-unlock / exhausted / capped weekly, the 5h window at/over
3450
+ * cacheAffinityFiveHourWall, a failure streak, or Kimi's unreadable console-only
3451
+ * quota (nothing would ever release the hold before a 429). Availability gates
3452
+ * (cooldown, 429 bench, exclusion, in-flight cap) never reach here — the account
3453
+ * is not a candidate at all — so a throttled account releases on the very next request.
3454
+ */
3455
+ _affinityRelease(account, criticalUnlock = null) {
3456
+ if (account.type === 'provider' && account.quota?.providerQuotaSource === 'console-only') return 'console-only';
3457
+ const weeklyState = this._weeklyRawState(account);
3458
+ if (weeklyState === 'critical') {
3459
+ if (!criticalUnlock) return 'critical-no-unlock';
3460
+ } else if (weeklyState === 'exhausted' || weeklyState === 'capped') {
3461
+ return `weekly-${weeklyState}`;
3462
+ }
3463
+ // 5-hour window: Anthropic reports unified5h; GLM/Kimi report providerSes.
3464
+ const five = account.type === 'provider' ? account.quota?.providerSes : account.quota?.unified5h;
3465
+ if (five != null && five >= this.scheduler.cacheAffinityFiveHourWall) return `5h-${five.toFixed(2)}`;
3466
+ const failures = account.consecutiveFailures || 0;
3467
+ if (failures > 0) return `failures-${failures}`;
3468
+ return null;
3469
+ }
3470
+
3471
+ /** `holderName` scores `account` as if it held the conversation (return-home after a
3472
+ * provider detour); omitted = the conversation's real last account. */
3473
+ _cacheAffinity(account, requestInfo = {}, criticalUnlock = null, holderName = undefined) {
3263
3474
  const base = Number(this.scheduler.cacheAffinityBonus) || 0;
3264
3475
  // Sticky mode has its own binding; its score loop runs only once the session has
3265
3476
  // decided to LEAVE a hot account — a discount there would pull it straight back.
3266
- if (base <= 0 || this.scheduler.routingMode === 'sticky') return 0;
3267
- const key = this._affinityKey(requestInfo);
3268
- if (!key || this.conversationLastAccount.get(key) !== account.name) return 0;
3477
+ // D3 retry-same (one acquire, set by server.js after a first-byte timeout on a big
3478
+ // prompt): the account that just timed out holds this prompt's warm copy, so it is
3479
+ // scored AS THE HOLDER — the same bonus, release gates, priority tiers, weekly
3480
+ // passes, fairness and anti-dogpile cap as any held conversation (red-team r1: no
3481
+ // bypass). Only the cooldown is skipped, upstream (server.js never cools it). It
3482
+ // applies in sticky mode too: pulling the turn back to that account IS the intent,
3483
+ // for exactly one acquire.
3484
+ const retrySame = requestInfo.retrySameAccountName != null && requestInfo.retrySameAccountName === account.name;
3485
+ if (base <= 0 || (this.scheduler.routingMode === 'sticky' && !retrySame)) return 0;
3486
+ if (!retrySame) {
3487
+ const key = this._affinityKey(requestInfo);
3488
+ if (!key || (holderName ?? this.conversationLastAccount.get(key)) !== account.name) return 0;
3489
+ }
3269
3490
  // Scale with the CONVERSATION'S SIZE too: losing the cache costs the prefix a
3270
3491
  // hop re-writes, and requestInfo.bodyBytes ≈ prefix + this turn. Checkpoint
3271
3492
  // 2026-11-08 11:07Z: a flat 400 cut full re-writes only 18.9%→14.8% of turns —
3272
3493
  // the rest were 100K-500K-token conversations whose single in-flight turn
3273
3494
  // already outweighed it. 1x below 150KB, rising to 3x at 450KB+.
3274
3495
  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;
3496
+ if (this._affinityRelease(account, criticalUnlock)) return 0;
3286
3497
  // Scale to THIS row's price of one in-flight unit, so every account tolerates the
3287
3498
  // same extra DEPTH (~33 units) — a fast-refill/soak-discounted GLM row prices a
3288
3499
  // unit at ~0.4x, and an unscaled bonus let it run ~2.5x deeper (red-team r1).
@@ -3291,7 +3502,63 @@ export class AccountManager {
3291
3502
  ? this._fastRefillMultiplier(account, { capFloor: true }) : refillMult;
3292
3503
  const unit = this.scheduler.concurrencyWeight * refillMult + this.scheduler.capPenaltyWeight * safetyMult;
3293
3504
  const fullUnit = this.scheduler.concurrencyWeight + this.scheduler.capPenaltyWeight;
3294
- return base * sizeMult * Math.min(1, unit / fullUnit);
3505
+ const bonus = base * sizeMult * Math.min(1, unit / fullUnit);
3506
+ // Anti-dogpile cap: never tolerate more than cacheAffinityMaxExtraDepth units of
3507
+ // extra depth at THIS row's unit price, however large the conversation or bonus.
3508
+ let maxExtra = Number(this.scheduler.cacheAffinityMaxExtraDepth);
3509
+ if (!Number.isFinite(maxExtra) || maxExtra < 0) return bonus;
3510
+ // A reserve (or unlocked-critical) account has the TIGHTER concurrency target; its
3511
+ // hold gets the same tightening so the hold never stretches the lowest-headroom tier
3512
+ // as far as a healthy one (reserveConcurrencyTarget / perAccountConcurrencyTarget).
3513
+ const wk = this._weeklyRawState(account);
3514
+ if (wk === 'reserve' || (wk === 'critical' && criticalUnlock)) {
3515
+ maxExtra *= Math.min(1, this.scheduler.reserveConcurrencyTarget / Math.max(1, this.scheduler.perAccountConcurrencyTarget));
3516
+ }
3517
+ return Math.min(bonus, maxExtra * unit);
3518
+ }
3519
+
3520
+ /**
3521
+ * WHY a known conversation left its last account — one short string for the
3522
+ * `conversation moved:` log line. `released:<gate>` = the hold let go (the gate
3523
+ * names the reason, incl. availability causes); `outscored:` = the hold was live and
3524
+ * the sibling still won, with the dominant score terms that beat the bonus.
3525
+ */
3526
+ _moveReason(lastName, best, requestInfo, excludedIndexes, scoringCtx, weeklyOptions, bestUnlock, now, asHolder = false) {
3527
+ // asHolder: explain why `lastName` lost while scored AS the holder (return-home).
3528
+ const holder = asHolder ? lastName : undefined;
3529
+ const last = this.accounts.find(a => a.name === lastName);
3530
+ if (!last) return 'released:account-gone';
3531
+ if (excludedIndexes.has(last.index)) return 'released:excluded';
3532
+ if (!this._matchesRequest(last, requestInfo.profile || 'claude', requestInfo)) return 'released:not-eligible';
3533
+ const unlock = weeklyOptions.allowWeeklyCritical ? null : this._criticalUnlock(last, requestInfo, excludedIndexes, null, now);
3534
+ const avail = { ...weeklyOptions, allowWeeklyCritical: weeklyOptions.allowWeeklyCritical || !!unlock, model: requestInfo.model, now };
3535
+ if (!this._isAvailable(last, avail)) return `released:unavailable(${this._unavailableCause(last, now)})`;
3536
+ const gate = this._affinityRelease(last, unlock);
3537
+ if (gate) return `released:${gate}`;
3538
+ if (this._effectivePriority(last, requestInfo, now) > this._effectivePriority(best, requestInfo, now)) return 'released:priority-tier';
3539
+ if (this._cacheAffinity(last, requestInfo, unlock, holder) <= 0) return 'released:no-hold';
3540
+ const lt = this._scoreTerms(last, requestInfo, scoringCtx, unlock, holder);
3541
+ const bt = this._scoreTerms(best, requestInfo, scoringCtx, bestUnlock);
3542
+ 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;
3543
+ const delta = sum(lt) - sum(bt);
3544
+ const diffs = ['concurrency', 'capPenalty', 'paceCost', 'utilizationCost', 'scopedPace', 'spread', 'ramp', 'reserveCost', 'criticalCost', 'failurePenalty', 'affinity']
3545
+ .map(k => [k, (k === 'affinity' ? -(lt[k] - bt[k]) : lt[k] - bt[k])])
3546
+ .filter(([, d]) => d > 0.05).sort((x, y) => y[1] - x[1]).slice(0, 2)
3547
+ .map(([k, d]) => `${k}+${d.toFixed(1)}`);
3548
+ return `outscored:${delta.toFixed(1)}(${diffs.join(',') || 'rank'};bonus=${lt.affinity.toFixed(0)};depth=${last.activeWeight}/${best.activeWeight})`;
3549
+ }
3550
+
3551
+ /** Short human cause for a not-available account (log text only). */
3552
+ _unavailableCause(account, now) {
3553
+ if (!account.enabled) return 'disabled';
3554
+ if (account.status === 'throttled' && account.rateLimitedUntil && now < account.rateLimitedUntil) return 'rate-limited';
3555
+ if (account.cooldownUntil && now < account.cooldownUntil) return 'cooldown';
3556
+ if (account.provisionalUpstreamUntil && now < account.provisionalUpstreamUntil) return 'upstream-failure';
3557
+ if (account.inFlight >= this.scheduler.safetyMaxActivePerAccount) return 'in-flight-cap';
3558
+ if (this._isSessionQuotaUnavailable(account, now)) return '5h-wall';
3559
+ const w = this._weeklyRawState(account, now);
3560
+ if (w !== 'normal' && w !== 'soft' && w !== 'unknown') return `weekly-${w}`;
3561
+ return account.status || 'other';
3295
3562
  }
3296
3563
 
3297
3564
  /**
@@ -4513,10 +4780,35 @@ export class AccountManager {
4513
4780
  console.log(`[Maxpool] Account "${account.name}" disabled after HTTP ${status} (${reason})`);
4514
4781
  }
4515
4782
 
4516
- markTransientFailure(accountIndex, reason = 'transient_error', { network = false } = {}) {
4783
+ markTransientFailure(accountIndex, reason = 'transient_error', { network = false, scope = 'account' } = {}) {
4517
4784
  const account = this.accounts[accountIndex];
4518
4785
  if (!account) return;
4519
4786
 
4787
+ // REQUEST-SCOPED (review r2 A): a first-byte/idle timeout on ONE request says nothing
4788
+ // about the account's other conversations. Deprioritise it for new placements only
4789
+ // (softCooldownUntil — held conversations keep it), and count consecutive request-
4790
+ // scoped faults. REQUEST_FAULT_ESCALATE in a row (no success between) is no longer
4791
+ // "one slow request": escalate to the account-scoped shape below (real cooldown +
4792
+ // failure streak → held conversations released), logged once per run.
4793
+ if (network && scope === 'request') {
4794
+ account.failedRequests++;
4795
+ account.lastError = reason;
4796
+ account.lastErrorAt = Date.now();
4797
+ account.requestFaultStreak = (account.requestFaultStreak || 0) + 1;
4798
+ if (account.requestFaultStreak < REQUEST_FAULT_ESCALATE) {
4799
+ account.softCooldownUntil = Date.now() + this.scheduler.networkCooldownMs;
4800
+ console.log(`[Maxpool] Account "${account.name}" deprioritised for new placements for ${Math.ceil(this.scheduler.networkCooldownMs / 1000)}s after ${reason} (request-scoped ${account.requestFaultStreak}/${REQUEST_FAULT_ESCALATE}; held conversations stay)`);
4801
+ return;
4802
+ }
4803
+ account.consecutiveFailures++;
4804
+ account.cooldownUntil = Date.now() + this.scheduler.networkCooldownMs;
4805
+ if (!account.requestFaultEscalated) {
4806
+ account.requestFaultEscalated = true;
4807
+ console.log(`[Maxpool] Account "${account.name}": ${account.requestFaultStreak} request-scoped faults in a row (${reason}) — treating as an ACCOUNT fault: cooling down ${Math.ceil(this.scheduler.networkCooldownMs / 1000)}s, held conversations released`);
4808
+ }
4809
+ return;
4810
+ }
4811
+
4520
4812
  if (network) {
4521
4813
  // A network-class failure (lost connectivity / token-refresh `fetch failed`)
4522
4814
  // is a FLEET-WIDE condition, not this account's fault — every account fails at