maxpool 1.24.50 → 1.24.52

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.50",
3
+ "version": "1.24.52",
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",
@@ -7,6 +7,7 @@ import { CapacityLedger, TANK_MIN_UTIL } from './capacity-ledger.js';
7
7
  // Nominal window length per kind — mirrors capacity-ledger's WINDOW_MS (kept here as a
8
8
  // local table so account-manager does not import a private constant).
9
9
  const WINDOW_MS_BY_KIND = { ses: 5 * 3600_000, wk: 7 * 86400_000 };
10
+ const PROVIDER_SES_WALL = 1; // provider 5h bench: serve to 100% (owner 2026-10-30); a usage cap still wins
10
11
 
11
12
  // A capacity window boundary must move by at least this much to count as a real
12
13
  // window ADVANCE rather than reset-stamp jitter (see noteCapacityWindowAdvance).
@@ -1567,7 +1568,8 @@ export class AccountManager {
1567
1568
  */
1568
1569
  _sessionBenchThreshold(account, now = Date.now()) {
1569
1570
  const cap = this._effectiveCap(account, 'ses', now);
1570
- return (cap != null && cap < this.switchThreshold) ? cap : this.switchThreshold;
1571
+ const wall = account?.type === 'provider' ? PROVIDER_SES_WALL : this.switchThreshold;
1572
+ return (cap != null && cap < wall) ? cap : wall;
1571
1573
  }
1572
1574
 
1573
1575
  /**
package/src/server.js CHANGED
@@ -611,7 +611,7 @@ async function forwardRequest(
611
611
  // anything and a 2-account fleet got exactly one repair, while the chain is now six
612
612
  // deep. Each repair latches its own flag, so this budget is a backstop, not the bound.
613
613
  const repairCount = requestInfo.repairCount || 0;
614
- const canRepairBody = repairCount < 6 && !res.headersSent;
614
+ const canRepairBody = repairCount < 6 && !clientHasRealBytes(res, requestInfo);
615
615
 
616
616
  // PRE-STRIP a session already known to carry provider-authored thinking. The client
617
617
  // resends the whole poisoned history every turn, so without this each turn pays another
@@ -873,20 +873,41 @@ async function forwardRequest(
873
873
  // Pinned cloud account refused (rate-limited / unavailable) before any byte
874
874
  // streamed: cool THIS mirror row and fail over through the normal path to the
875
875
  // next-best account, local or cloud — exactly like a local account's 429.
876
- if (account.remote && (upRes.status === 429 || upRes.status === 503) && retryCount + 1 < maxAttempts && !res.headersSent) {
876
+ if (account.remote && (upRes.status === 429 || upRes.status === 503) && !clientHasRealBytes(res, requestInfo)) {
877
877
  try { await upRes.body?.cancel(); } catch { /* drained */ }
878
878
  const ra = Number(upRes.headers.get('retry-after'));
879
879
  // P3: "busy" is a 2s back-off on this row only; "unavailable"/429 cools longer.
880
880
  account.cooldownUntil = Date.now() + (Number.isFinite(ra) && ra > 0 ? Math.min(ra, 300) * 1000 : 30_000);
881
881
  finishLink({ status: upRes.status, error: 'remote_pinned_unavailable' });
882
882
  excludedIndexes.add(account.index);
883
- if (hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
883
+ if (retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
884
884
  return forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes);
885
885
  }
886
+ // Every route refused: that is capacity, so it waits here. The body was cancelled
887
+ // above, so falling through handed the client a 503 with no body.
888
+ const queued = await queueAndRetry(
889
+ `all routes failed after ${upRes.status} from "${account.name}"`,
890
+ req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
891
+ retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, 'capacity',
892
+ );
893
+ if (queued) return;
894
+ ctx.status = upRes.status;
895
+ sendErrorResponse(res, requestInfo, upRes.status, { type: 'error', error: { type: 'overloaded_error', message: `Team pool account "${account.name}" is at capacity` } });
896
+ return;
897
+ }
898
+ // A request released from the queue already holds a committed keep-alive stream.
899
+ if (requestInfo.queueHeartbeatActive) {
900
+ clearQueueHeartbeat(requestInfo);
901
+ if (upRes.status >= 400) {
902
+ const errorText = await readErrorBody(upRes);
903
+ sendErrorBody(res, requestInfo, upRes.status, errorText, upRes.headers);
904
+ finishLink({ success: upRes.status < 500, status: upRes.status });
905
+ return;
906
+ }
886
907
  }
887
908
  const outHeaders = {};
888
909
  upRes.headers.forEach((v, k) => { if (!HOP_BY_HOP_HEADERS.has(k) && k !== 'content-length' && k !== 'transfer-encoding') outHeaders[k] = v; });
889
- res.writeHead(upRes.status, outHeaders);
910
+ if (!res.headersSent) res.writeHead(upRes.status, outHeaders);
890
911
  if (upRes.body) {
891
912
  const reader = upRes.body.getReader();
892
913
  try {
@@ -910,10 +931,17 @@ async function forwardRequest(
910
931
  } catch (err) {
911
932
  finishLink({ status: 502, error: 'pool_link_unreachable' });
912
933
  excludedIndexes.add(account.index); // P4b: never re-pick a row that just failed
913
- const retried = retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)
914
- ? await forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes)
915
- : null;
916
- if (retried) return;
934
+ if (!clientHasRealBytes(res, requestInfo)) {
935
+ if (retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
936
+ return forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes);
937
+ }
938
+ const queued = await queueAndRetry(
939
+ `all routes failed after a link error on "${account.name}"`,
940
+ req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
941
+ retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, 'network',
942
+ );
943
+ if (queued) return;
944
+ }
917
945
  ctx.status = 502;
918
946
  sendErrorResponse(res, requestInfo, 502, { type: 'error', error: { type: 'api_error', message: `Team pool "${account.name}" unreachable: ${err?.message || err}` } });
919
947
  }
@@ -1315,7 +1343,7 @@ async function forwardRequest(
1315
1343
  logSections.push(`=== RESPONSE ${upstreamRes.status} — "${account.name}" cooling down, failing over ===\n${formatHeaders(upstreamRes.headers)}`);
1316
1344
  }
1317
1345
 
1318
- if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1346
+ if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1319
1347
  return forwardRequest(
1320
1348
  req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir,
1321
1349
  retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes,
@@ -1466,7 +1494,7 @@ async function forwardRequest(
1466
1494
  : null;
1467
1495
  const issuerIsClaude = issuer && issuer.type !== 'provider';
1468
1496
  if (lease.migratedFromName && issuerIsClaude && requestInfo.sessionKey
1469
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1497
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1470
1498
  accountManager.revertSessionBinding(requestInfo.sessionKey, lease.migratedFromName);
1471
1499
  excludedIndexes.add(account.index);
1472
1500
  console.log(`[Maxpool] Thinking-signature fail-safe: reverting session to issuer "${lease.migratedFromName}" and retrying`);
@@ -1481,7 +1509,7 @@ async function forwardRequest(
1481
1509
  // EFFORT REPAIR. A rejected output_config.effort is a hard failure of whatever the
1482
1510
  // client was doing (a web search, a tool call) — worth healing rather than surfacing.
1483
1511
  if (effortMode && !requestInfo.effortRepaired
1484
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1512
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1485
1513
  const fix = repairEffort(body, effortMode, errorBody);
1486
1514
  if (fix.body) {
1487
1515
  // Latch it so LATER turns apply the working level up front: the client resends the
@@ -1623,7 +1651,7 @@ async function forwardRequest(
1623
1651
  // it when a provider can actually serve it (profile=all with a GLM/Kimi token).
1624
1652
  const providerAvailable = accountManager.accounts?.some(a => a.type === 'provider' && a.enabled !== false);
1625
1653
  if (anthropicIncompat && requestInfo.sessionKey && providerAvailable
1626
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1654
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1627
1655
  accountManager.markSessionIncompatible?.(requestInfo.sessionKey, requestInfo.homeProvider);
1628
1656
  excludedIndexes.add(account.index);
1629
1657
  console.log(`[Maxpool] Anthropic rejected this transcript (server_tool_use/thinking); pinning session to GLM/Kimi and retrying`);
@@ -1644,7 +1672,7 @@ async function forwardRequest(
1644
1672
  // Claude account exists to serve it; with none, the 400 surfaces (nothing bigger).
1645
1673
  const claudeAvailable = accountManager.accounts?.some(a => a.type !== 'provider' && a.enabled !== false);
1646
1674
  if (providerTooSmall && requestInfo.sessionKey && claudeAvailable
1647
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1675
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1648
1676
  accountManager.markSessionLargeContext?.(requestInfo.sessionKey);
1649
1677
  // Bench EVERY provider: today's GLM + Kimi coding legs both cap at ~256K, so once
1650
1678
  // one 400s on size the others can't hold it either. If a genuine 1M-context
@@ -1666,7 +1694,7 @@ async function forwardRequest(
1666
1694
  // latch the session — the fault is one request's shape, not a durable property of
1667
1695
  // the conversation, and latching would evict a session from GLM on a single blip.
1668
1696
  if (providerRejectedShape && claudeAvailable
1669
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1697
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1670
1698
  for (const a of (accountManager.accounts || [])) {
1671
1699
  if (a.type === 'provider') excludedIndexes.add(a.index);
1672
1700
  }
@@ -1684,7 +1712,7 @@ async function forwardRequest(
1684
1712
  // Trigger on the DETERMINISTIC signature rejection only — never the fuzzy
1685
1713
  // isAnthropicIncompatBody heuristic, so an unrelated malformed request keeps its
1686
1714
  // own upstream error instead of being mislabelled a provider-contamination.
1687
- if (isSignatureRejection && !res.headersSent) {
1715
+ if (isSignatureRejection && !clientHasRealBytes(res, requestInfo)) {
1688
1716
  const provs = (accountManager.accounts || []).filter(a => a.type === 'provider');
1689
1717
  // Say only what ACTUALLY happened. `thinkingStripped` is set only when the strip
1690
1718
  // ran; without it we found nothing provider-shaped, so claiming we stripped —
@@ -1964,7 +1992,7 @@ async function forwardRequest(
1964
1992
  }
1965
1993
 
1966
1994
  accountManager.releaseAccount(lease, { error: err.message });
1967
- if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1995
+ if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1968
1996
  account.status = 'error';
1969
1997
  excludedIndexes.add(account.index);
1970
1998
  return forwardRequest(
@@ -2874,6 +2902,13 @@ function sendErrorBody(res, requestInfo, status, body, headers) {
2874
2902
  res.end(body);
2875
2903
  }
2876
2904
 
2905
+ /** True once the client received REAL response bytes. The queue/grace keep-alive commits
2906
+ * headers plus pings, so res.headersSent is true while nothing is delivered yet: repairs
2907
+ * and failover stay safe until streamResponse stops the keep-alive. */
2908
+ function clientHasRealBytes(res, requestInfo) {
2909
+ return res.headersSent && !requestInfo.queueHeartbeatActive;
2910
+ }
2911
+
2877
2912
  async function queueAndRetry(
2878
2913
  reason, req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
2879
2914
  retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody = canRetryBufferedBody, cause = 'quota',