maxpool 1.24.77 → 1.24.79

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.77",
3
+ "version": "1.24.79",
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",
@@ -343,6 +343,12 @@ const PERSISTED_QUOTA_FIELDS = [
343
343
  ];
344
344
 
345
345
  const CONVERSATION_AFFINITY_MAX = 5000;
346
+ const IDENTITY_WINDOW_MS = 15 * 60_000;
347
+ const IDENTITY_MAX_SAMPLES = 4000;
348
+ const IDENTITY_MIN_PER_SESSION = 8;
349
+ const IDENTITY_MIN_SAMPLE = 60;
350
+ const IDENTITY_DEGRADED_RATE = 0.9;
351
+ const IDENTITY_RECOVER_RATE = 0.7;
346
352
  const PROVIDER_429_MIN_BENCH_SEC = 60;
347
353
  const PROVIDER_429_MAX_BENCH_SEC = 15 * 60;
348
354
  const PROVIDER_429_STREAK_FORGET_MS = 60 * 60 * 1000;
@@ -603,6 +609,12 @@ export class AccountManager {
603
609
  // Bounded LRU — oldest conversations drop first; a dropped entry only means the
604
610
  // next turn is balanced fresh.
605
611
  this.conversationLastAccount = new Map();
612
+ // Conversation-identity HEALTH (owner 2026-11-08: "make sure this cannot degrade
613
+ // silently"). Twice in one day a Claude Code request-shape detail (a moving
614
+ // cache_control marker, then a per-request billing checksum) made every turn hash
615
+ // to a new conversation id — affinity went silently inert. Rolling sample of
616
+ // (terminal, conversation id) per request; see _noteIdentityHealth.
617
+ this.identityHealth = { samples: [], degraded: false, since: null, lastRate: null, lastN: 0 };
606
618
  this._peakCache = null; // per-UTC-minute peak-window memo (see _peakStateFor)
607
619
  // CAPACITY LEDGER (2026-08-22): observed tokens per completed window cycle, so
608
620
  // "true capacity" is comparable across providers. Restored from state on boot.
@@ -882,6 +894,7 @@ export class AccountManager {
882
894
  // disagree with the peak tier that caused the move (defaults to real time).
883
895
  this._bindSession(requestInfo.sessionKey, account, requestInfo.model, requestInfo.now);
884
896
  }
897
+ this._noteIdentityHealth(requestInfo);
885
898
  this._noteConversationAccount(requestInfo, account);
886
899
  account.inFlight++;
887
900
  account.activeWeight += weight;
@@ -3202,9 +3215,50 @@ export class AccountManager {
3202
3215
  * falls back to the terminal session key (callers that never set the hash,
3203
3216
  * e.g. tests and pre-hash call sites). */
3204
3217
  _affinityKey(requestInfo = {}) {
3218
+ // SELF-HEAL: while conversation identity looks broken, fall back to the
3219
+ // per-terminal key — cruder (subagents share it) but immune to request-shape
3220
+ // drift, so stickiness degrades instead of switching off.
3221
+ if (this.identityHealth.degraded) return requestInfo.sessionKey || requestInfo.affinityKey || null;
3205
3222
  return requestInfo.affinityKey || requestInfo.sessionKey || null;
3206
3223
  }
3207
3224
 
3225
+ /** Track how often each terminal's requests START a new conversation id. Healthy
3226
+ * traffic reuses ids (measured 2026-11-08 on 1.24.77: 68 turns → 19 conversations;
3227
+ * a busy terminal 32 requests → 6 ids). A broken identity gives ~every request a
3228
+ * fresh id. Window: last IDENTITY_WINDOW_MS; judged only over terminals with at
3229
+ * least IDENTITY_MIN_PER_SESSION requests in it (a terminal sending 2 requests
3230
+ * proves nothing) and IDENTITY_MIN_SAMPLE requests in total. Flip to degraded at
3231
+ * ≥ IDENTITY_DEGRADED_RATE new-id share; recover below IDENTITY_RECOVER_RATE
3232
+ * (hysteresis — no flapping). One log line per transition; status shows the state. */
3233
+ _noteIdentityHealth(requestInfo, now = Date.now()) {
3234
+ if (!requestInfo.sessionKey || !requestInfo.conversationHash) return;
3235
+ const h = this.identityHealth;
3236
+ h.samples.push({ t: now, s: requestInfo.sessionKey, c: requestInfo.conversationHash });
3237
+ const cutoff = now - IDENTITY_WINDOW_MS;
3238
+ while (h.samples.length && h.samples[0].t < cutoff) h.samples.shift();
3239
+ if (h.samples.length > IDENTITY_MAX_SAMPLES) h.samples.splice(0, h.samples.length - IDENTITY_MAX_SAMPLES);
3240
+ const perSession = new Map();
3241
+ for (const x of h.samples) {
3242
+ if (!perSession.has(x.s)) perSession.set(x.s, []);
3243
+ perSession.get(x.s).push(x.c);
3244
+ }
3245
+ let reqs = 0; let fresh = 0;
3246
+ for (const ids of perSession.values()) {
3247
+ if (ids.length < IDENTITY_MIN_PER_SESSION) continue;
3248
+ reqs += ids.length; fresh += new Set(ids).size;
3249
+ }
3250
+ if (reqs < IDENTITY_MIN_SAMPLE) return;
3251
+ const rate = fresh / reqs;
3252
+ h.lastRate = rate; h.lastN = reqs;
3253
+ if (!h.degraded && rate >= IDENTITY_DEGRADED_RATE) {
3254
+ h.degraded = true; h.since = now;
3255
+ console.log(`[Maxpool] cache affinity: conversation identity looks BROKEN (${Math.round(rate * 100)}% of ${reqs} requests started a new conversation) — Claude Code's request shape probably changed. Falling back to per-terminal stickiness until it recovers; the hash in describeRequest needs updating.`);
3256
+ } else if (h.degraded && rate < IDENTITY_RECOVER_RATE) {
3257
+ h.degraded = false; h.since = now;
3258
+ console.log(`[Maxpool] cache affinity: conversation identity healthy again (${Math.round(rate * 100)}% new over ${reqs} requests) — per-conversation stickiness restored.`);
3259
+ }
3260
+ }
3261
+
3208
3262
  _cacheAffinity(account, requestInfo = {}) {
3209
3263
  const base = Number(this.scheduler.cacheAffinityBonus) || 0;
3210
3264
  // Sticky mode has its own binding; its score loop runs only once the session has
@@ -5213,6 +5267,14 @@ export class AccountManager {
5213
5267
  // verifies the wrong thing (measured 2026-08-23: reported 1.8.7 while executing
5214
5268
  // 1.8.6). This field is the one to assert on.
5215
5269
  runningVersion: this.runningVersion || null,
5270
+ // Cache-affinity identity health — 'ok' | 'degraded' (+ the measured new-id
5271
+ // share). A post-release check asserts 'ok' on live traffic.
5272
+ affinityIdentity: {
5273
+ state: this.identityHealth.degraded ? 'degraded' : 'ok',
5274
+ newConversationShare: this.identityHealth.lastRate,
5275
+ sampledRequests: this.identityHealth.lastN,
5276
+ since: this.identityHealth.since,
5277
+ },
5216
5278
  currentAccount: this.accounts[this.currentIndex]?.name,
5217
5279
  switchThreshold: this.switchThreshold,
5218
5280
  routing: {
package/src/index.js CHANGED
@@ -51,7 +51,7 @@ import {
51
51
  RELOAD_SWAPPED, RELOAD_ROLLED_BACK,
52
52
  MSG_LISTEN, MSG_RELEASE, MSG_TAKEOVER, MSG_PROBE_READY,
53
53
  MSG_RELOAD_REQUEST, MSG_READY, MSG_FAILED, MSG_RELEASED, MSG_PRIMARY, MSG_ROLLED_BACK,
54
- MSG_TTY_REASSERT,
54
+ MSG_TTY_REASSERT, MSG_CONN,
55
55
  } from './reload-protocol.js';
56
56
 
57
57
  const args = process.argv.slice(2);
@@ -222,12 +222,25 @@ async function supervisorCommand() {
222
222
 
223
223
  // Bind once. EADDRINUSE / EACCES here is a cold-start failure → exit(1) is
224
224
  // correct (there's no worker to keep alive yet).
225
- let masterServer = createServer();
226
- // We DROP any connection the supervisor accidentally accepts while a worker is
227
- // also accepting on the shared handle — but the design avoids that: the
228
- // supervisor's acceptor is only LIVE during the brief cutover gap (it stops
229
- // once a worker confirms it is the sole acceptor). A bare handler is required
230
- // so the rare gap-accepted socket isn't left dangling.
225
+ // pauseOnConnect: a socket the supervisor accepts must reach the worker with every
226
+ // byte still unread, so the supervisor never consumes the request it is relaying.
227
+ let masterServer = createServer({ pauseOnConnect: true });
228
+ // The supervisor's accept loop is LIVE from bind until a worker confirms it is the
229
+ // sole acceptor (MSG_PRIMARY), and again over the reload cutover gap. It shares the
230
+ // listening socket with the worker, so it WINS some accepts. A connection accepted
231
+ // here used to be left dangling (this server had no 'connection' handler): the client
232
+ // hung, then got ECONNRESET, and the request never reached any worker (the flaky
233
+ // reload-oauth-refresh test, 2026-11-08). Hand every such socket to the worker that
234
+ // is serving, or about to: `acceptTarget` (the incoming worker during a reload) else
235
+ // the active one. No live worker → close it so the client retries now, never hangs.
236
+ let acceptTarget = null;
237
+ masterServer.on('connection', sock => {
238
+ const target = acceptTarget || activeWorker;
239
+ if (!target || !target.child.connected) { sock.destroy(); return; }
240
+ try {
241
+ target.child.send({ type: MSG_CONN }, sock, err => { if (err) sock.destroy(); });
242
+ } catch { sock.destroy(); }
243
+ });
231
244
  const relistenMaster = (retriesOnBusy = 20) => new Promise((resolve, reject) => {
232
245
  if (masterServer.listening) { resolve(); return; }
233
246
  const onListen = () => { masterServer.removeListener('error', onErr); resolve(); };
@@ -246,6 +259,14 @@ async function supervisorCommand() {
246
259
  masterServer.once('listening', onListen);
247
260
  masterServer.listen(port, host);
248
261
  });
262
+ // What goes over IPC to a worker: the RAW listening handle, not the net.Server wrapper.
263
+ // Sent as a net.Server, Node builds an intermediate net.Server in the worker that
264
+ // listen()s on it and ACCEPTS whatever is already queued before user code can attach
265
+ // a listener (measured: with 8 connections queued, the worker's HTTP server got 0-5
266
+ // and the rest sat unread in that intermediate server, black-holed). The raw handle
267
+ // has no such wrapper: the worker's http.Server listen()s on it directly.
268
+ // Falls back to the wrapper if a future Node hides the handle (old behaviour).
269
+ const masterHandle = () => masterServer._handle || masterServer;
249
270
  const closeMasterAccept = () => new Promise(resolve => {
250
271
  if (!masterServer.listening) { resolve(); return; }
251
272
  masterServer.close(() => resolve());
@@ -327,7 +348,7 @@ async function supervisorCommand() {
327
348
  const worker = makeWorkerChannel(child);
328
349
  if (!reload) {
329
350
  // Cold start: hand the socket and tell it to go primary immediately.
330
- worker.send({ type: MSG_LISTEN }, masterServer);
351
+ worker.send({ type: MSG_LISTEN }, masterHandle());
331
352
  }
332
353
  return worker;
333
354
  };
@@ -373,11 +394,13 @@ async function supervisorCommand() {
373
394
  const outcome = await runReloadBaton({
374
395
  oldWorker,
375
396
  newWorker,
376
- handle: masterServer,
397
+ handle: masterHandle(),
377
398
  // After the OLD worker has released its fd, re-arm the supervisor's own
378
399
  // listener to cover the cutover gap, then hand THAT live handle to the
379
400
  // new worker. The new worker's MSG_PRIMARY then closes it again.
380
- prepareHandle: async () => { await relistenMaster(); return masterServer; },
401
+ // The old worker has released, so gap-accepted connections now belong to the
402
+ // incoming worker (before this point the still-serving old one is the target).
403
+ prepareHandle: async () => { acceptTarget = newWorker; await relistenMaster(); return masterHandle(); },
381
404
  // Generous, env-tunable readiness/takeover budgets — the 10s default rolled back
382
405
  // on this user's loaded Mac (the update never landed). See RELOAD_READY_MS.
383
406
  readyTimeoutMs: RELOAD_READY_MS,
@@ -427,6 +450,7 @@ async function supervisorCommand() {
427
450
  activeWorker = null;
428
451
  endTurn?.({ code: null, signal: 'SIGKILL', fallback: true });
429
452
  } finally {
453
+ acceptTarget = null;
430
454
  reloadInFlight = false;
431
455
  }
432
456
  };
@@ -1564,6 +1588,11 @@ async function serverWorkerCommand() {
1564
1588
  // never sit alive-but-not-primary (the blank-screen `r`-restart hang).
1565
1589
  console.error('[Maxpool] Cold worker received MSG_LISTEN with no socket handle — exiting for respawn.');
1566
1590
  process.exit(SERVER_RESTART_EXIT_CODE);
1591
+ } else if (msg?.type === MSG_CONN) {
1592
+ // A connection the supervisor's own accept loop won the race for (cold start
1593
+ // before MSG_PRIMARY, or the reload cutover gap). Serve it exactly as if we
1594
+ // had accepted it: http.Server wires up on the 'connection' event.
1595
+ if (handle) { handle.resume?.(); server.emit('connection', handle); }
1567
1596
  } else if (msg?.type === MSG_PROBE_READY) {
1568
1597
  // Headless reload worker: we've booted the new code and restored quota
1569
1598
  // in memory. Confirm readiness (we are NOT yet accepting / writing).
@@ -30,6 +30,7 @@ export const MSG_FAILED = 'failed'; // worker failed to boot/bind/restore
30
30
  export const MSG_RELEASED = 'released'; // old worker stopped accepting + writing, flushed
31
31
  export const MSG_PRIMARY = 'primary'; // new worker is now sole acceptor + lease holder
32
32
  export const MSG_QUOTA_STATE = 'quota-state'; // old worker hands its in-memory quota to supervisor
33
+ export const MSG_CONN = 'conn'; // (with socket) supervisor-accepted connection handed to a worker to serve
33
34
 
34
35
  /**
35
36
  * Outcome codes for a reload attempt, surfaced to the supervisor so it can log