maxpool 1.24.81 → 1.24.82

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.
@@ -0,0 +1,27 @@
1
+ // Reset-machinery timing constants — ONE leaf module (judge note, 2026-11-10: the
2
+ // 5*60_000 literal had spread across account-manager.js, prober.js and tui.js).
3
+ // Leaf by construction: imports nothing, so every consumer can share it.
4
+
5
+ // A2: grants are listed at most this often per account (the call shares the
6
+ // per-IP rate-limited /api/oauth/usage endpoint with the usage probe).
7
+ export const GRANTS_EVERY_MS = 5 * 60_000;
8
+ // A stale/errored listing retries at this shorter cadence so a transient blank
9
+ // recovers fast.
10
+ export const GRANTS_RETRY_MS = 30_000;
11
+ // C3: the cap lift survives listing failures this long before ending.
12
+ export const LIFT_GRACE_MS = 10 * 60_000;
13
+ // C2: a stored listing may serve a reactive claim up to this age.
14
+ export const CLAIM_STALE_LISTING_MAX_MS = 6 * 3600_000;
15
+ // C6: a FAILED Claude claim backs off per (account, grantId) on these steps,
16
+ // capped at CLAIM_BACKOFF_CAP_MS (matches the z.ai card-redeem schedule).
17
+ export const CLAIM_BACKOFF_STEPS_MS = [15 * 60_000, 30 * 60_000, 60 * 60_000];
18
+ export const CLAIM_BACKOFF_CAP_MS = 6 * 3600_000;
19
+ // B3: a listing older than LISTING_FRESH_MULT × GRANTS_EVERY_MS reads not-fresh
20
+ // in the TUI and cannot start a cap lift (C3).
21
+ export const LISTING_FRESH_MULT = 3;
22
+ export const LISTING_FRESH_MS = LISTING_FRESH_MULT * GRANTS_EVERY_MS;
23
+ // C1: the usage reading a claim decides on must be this fresh.
24
+ export const CLAIM_FRESH_MS = 5 * 60_000;
25
+ // C2: a reactive claim runs off the request path and gives up after this long,
26
+ // so a hung profile GET / POST can never hold the claim lock for minutes.
27
+ export const REACTIVE_CLAIM_TIMEOUT_MS = 10_000;
package/src/server.js CHANGED
@@ -473,6 +473,8 @@ export function createProxyServer(accountManager, config, hooks = {}) {
473
473
  return;
474
474
  }
475
475
 
476
+ markFirstByte(res, () => hooks.onRequestFirstByte?.(reqId));
477
+
476
478
  // The request is now TRACKED (onRequestStart accepted → tui.active +
477
479
  // restartController.activeRequests both hold it). EVERYTHING that can throw —
478
480
  // including body buffering — must run inside the try whose finally fires
@@ -572,6 +574,11 @@ export function createProxyServer(accountManager, config, hooks = {}) {
572
574
  // belonged to, so a turn's account is exact instead of a timestamp guess.
573
575
  conversation: requestInfo?.conversationHash || null,
574
576
  sessionKey: headerValue(req.headers, 'x-maxpool-session') || null,
577
+ // D (2026-11-10): the first REAL upstream byte (the fetch resolve), for
578
+ // the TUI's ttfb= tag. Prefer this over the client-write stamp: a queued
579
+ // request's heartbeat commits writes LONG before upstream answers.
580
+ firstUpstreamByteAt: requestInfo?._firstUpstreamByteAt ?? null,
581
+ upstreamForwardStartAt: requestInfo?._upstreamForwardStartAt ?? null,
575
582
  });
576
583
  }
577
584
  } catch (err) {
@@ -1240,6 +1247,10 @@ async function forwardRequest(
1240
1247
  const streamGraceTimer = armStreamForwardGrace(res, requestInfo, queueConfig, accountManager);
1241
1248
  try {
1242
1249
  let upstreamRes;
1250
+ // D (finding 5): THIS attempt's upstream forward start. ttfb is measured from
1251
+ // here — never from the request's arrival, which includes queue wait and the
1252
+ // earlier attempts of a failover.
1253
+ const upstreamForwardStartAt = Date.now();
1243
1254
  try {
1244
1255
  upstreamRes = await fetch(upstreamUrl, {
1245
1256
  method,
@@ -1254,6 +1265,14 @@ async function forwardRequest(
1254
1265
  // a re-forward (failover) re-arms for the REMAINING time to the shared deadline.
1255
1266
  if (streamGraceTimer) clearTimeout(streamGraceTimer);
1256
1267
  }
1268
+ // D (2026-11-10, finding 5): the upstream ANSWERED (fetch resolved — a failed
1269
+ // or timed-out fetch threw above and never reaches here). Stamped on EVERY
1270
+ // attempt together with that attempt's forward start, so after a failover the
1271
+ // pair describes the attempt that actually served (a quick 429 then a slow
1272
+ // second account reports the slow one). The queue heartbeat never stamps it.
1273
+ // Log-only: no setting reads it.
1274
+ requestInfo._upstreamForwardStartAt = upstreamForwardStartAt;
1275
+ requestInfo._firstUpstreamByteAt = Date.now();
1257
1276
  // Response arrived — the pre-response leak window is over. Stop guarding for
1258
1277
  // client-disconnect via abort (streamResponse handles mid-stream disconnects).
1259
1278
  res.off('close', onClientClose);
@@ -1438,6 +1457,27 @@ async function forwardRequest(
1438
1457
  fingerprint: rateLimit.scope === 'unknown' ? rateLimit.fingerprint : null,
1439
1458
  modelScope: rateLimit.modelScope || null,
1440
1459
  });
1460
+ // C2 REACTIVE CLAIM (2026-11-10): a 429 on the serving lease claims a held
1461
+ // reset AT ONCE, not after a full prober sweep. Only on the lease's LOCAL
1462
+ // account holding the credential (never a mirror row — its 429 is the
1463
+ // cloud's, and the cloud claims on its own side). A weekly-CLASS 429 (the
1464
+ // 7d header itself ≥ the wall) claims a general grant; a scoped 429 claims
1465
+ // only if the probe MEASURED that model's bucket at the wall. Uses the
1466
+ // current token; a 401 defers to the sweep.
1467
+ // FIRE-AND-FORGET (finding 4): the claim (profile GET + POST) runs OFF the
1468
+ // user's request — this request fails over at once; the claim unbenches the
1469
+ // account for the NEXT request when it lands. Never awaited here.
1470
+ if (accountManager.tryReactiveClaim && !account.remote) {
1471
+ const weeklyHdr = Number(rateLimitHeaders['anthropic-ratelimit-unified-7d-utilization']);
1472
+ const weeklyWall = Number.isFinite(weeklyHdr) && weeklyHdr >= 0.999;
1473
+ const kind = weeklyWall ? 'weekly' : (rateLimit.modelScope || null);
1474
+ if (kind) {
1475
+ try {
1476
+ Promise.resolve(accountManager.tryReactiveClaim(account, { kind, weeklyUtil: weeklyHdr }))
1477
+ .catch(() => { /* best-effort; the sweep retries */ });
1478
+ } catch { /* a synchronous throw must not break the failover below */ }
1479
+ }
1480
+ }
1441
1481
  accountManager.releaseAccount(lease, { status: 429, error: rateLimit.modelScope ? 'model_rate_limited' : 'rate_limited' });
1442
1482
 
1443
1483
  if (logDir) {
@@ -4045,6 +4085,28 @@ function prepareRuntimeProviders(accountManager, headers) {
4045
4085
  }
4046
4086
  }
4047
4087
 
4088
+ /**
4089
+ * Call `onFirst` once, the first time anything is flushed to the client (headers or
4090
+ * body). This is the moment a waiting client first sees life: Claude Code reports
4091
+ * "Waiting for API response" when nothing arrives for ~20s, so the request log carries
4092
+ * time-to-first-byte separately from the total duration. Pure observer: the wrapped
4093
+ * methods behave identically, and a throwing callback never affects the response.
4094
+ */
4095
+ export function markFirstByte(res, onFirst) {
4096
+ let fired = false;
4097
+ const fire = () => { if (fired) return; fired = true; try { onFirst(); } catch { /* observer only */ } };
4098
+ for (const m of ['write', 'end', 'flushHeaders']) {
4099
+ const orig = res[m];
4100
+ if (typeof orig !== 'function') continue;
4101
+ res[m] = function (...args) {
4102
+ // end() with no body and write('') send no bytes of their own, but end() still
4103
+ // flushes the headers, so any call to these three means bytes are on the way.
4104
+ fire();
4105
+ return orig.apply(this, args);
4106
+ };
4107
+ }
4108
+ }
4109
+
4048
4110
  function headerValue(headers, name) {
4049
4111
  const lname = name.toLowerCase();
4050
4112
  let value = headers[lname];
package/src/tui.js CHANGED
@@ -5,6 +5,9 @@ import { fetchProfile, loginOAuth, tokenFingerprint, isLoginCancelled } from './
5
5
  import { appendEventLog, setConsoleStdoutSuppressed } from './event-log.js';
6
6
  import { sendLoginToPool } from './cloud-login.js';
7
7
  import { describeLinkError } from './pool-link.js';
8
+ import { normalizeRemoteResets } from './account-manager.js';
9
+ import { appliesTo } from './claude-reset-grants.js';
10
+ import { LISTING_FRESH_MS } from './reset-timing.js';
8
11
 
9
12
  // ── ANSI helpers ─────────────────────────────────────────────
10
13
 
@@ -571,6 +574,12 @@ export class TUI {
571
574
  if (r) r.account = info.account;
572
575
  }
573
576
 
577
+ /** First response byte reached the client (server.js markFirstByte). */
578
+ onRequestFirstByte(id) {
579
+ const r = this.active.get(id);
580
+ if (r && r.firstByteAt == null) r.firstByteAt = Date.now();
581
+ }
582
+
574
583
  onRequestEnd(id, info) {
575
584
  const r = this.active.get(id);
576
585
  this.active.delete(id);
@@ -593,7 +602,19 @@ export class TUI {
593
602
  const provTag = (isProvider && r?.sessionKey) ? ` [sess ${String(r.sessionKey).slice(0, 8)}]` : '';
594
603
  const cv = info.conversation || r?.conversation;
595
604
  const cvTag = cv ? ` [cv ${cv.slice(0, 8)} ${String(info.sessionKey || r?.sessionKey || '').slice(0, 8)}]` : '';
596
- this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${provTag}${cvTag}`);
605
+ // ttfb LAST: the UPSTREAM wait of the attempt that served — its fetch resolve
606
+ // minus its own forward start (D, 2026-11-10). Never measured from arrival:
607
+ // that would add queue wait and earlier failover attempts. Falls back to the
608
+ // client-write stamp from arrival when no upstream pair exists (a request that
609
+ // never reached upstream, the link path). Omitted when no byte was ever sent.
610
+ let ttfbMs = null;
611
+ if (info.firstUpstreamByteAt != null && info.upstreamForwardStartAt != null) {
612
+ ttfbMs = info.firstUpstreamByteAt - info.upstreamForwardStartAt;
613
+ } else if (r?.firstByteAt != null) {
614
+ ttfbMs = r.firstByteAt - r.started;
615
+ }
616
+ const ttfbTag = ttfbMs != null ? ` ttfb=${(Math.max(0, ttfbMs) / 1000).toFixed(1)}s` : '';
617
+ this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${provTag}${cvTag}${ttfbTag}`);
597
618
  }
598
619
 
599
620
  _addLog(msg) {
@@ -2287,12 +2308,33 @@ export class TUI {
2287
2308
  const live = a?.poolBenchedBy ? this.am.remoteRowFor?.(a) : null;
2288
2309
  if (live) {
2289
2310
  const r = live.row;
2311
+ // B2 (2026-11-10): the overlay also carries the cloud twin's RESETS via the
2312
+ // same normalizeRemoteResets the mirror rows use. When the twin's snapshot is
2313
+ // MISSING the reset fields (one failed link poll — the "2 → gone → back"
2314
+ // flicker through a new path), keep the PREVIOUS overlay marked not-fresh,
2315
+ // never 0.
2316
+ let resetsOverlay;
2317
+ if (r.resetGrants != null || r.resetCards != null || r.capLift != null) {
2318
+ const nr = normalizeRemoteResets(r);
2319
+ resetsOverlay = { resetGrants: nr.resetGrants, resetCards: nr.resetCards, capLift: nr.capLift };
2320
+ } else {
2321
+ const prev = a._twinResetsOverlay || {};
2322
+ resetsOverlay = {
2323
+ resetGrants: prev.resetGrants ? { ...prev.resetGrants, stale: true } : null,
2324
+ resetCards: prev.resetCards ? { ...prev.resetCards, stale: true } : null,
2325
+ capLift: prev.capLift ?? null,
2326
+ };
2327
+ }
2328
+ a._twinResetsOverlay = resetsOverlay; // remember for the next frame's fallback
2290
2329
  a = Object.assign(Object.create(Object.getPrototypeOf(a)), a, {
2291
2330
  quota: { ...a.quota, ...(r.quota || {}) },
2292
2331
  inFlight: r.inFlight ?? 0,
2293
2332
  completedRequests: r.completedRequests ?? a.completedRequests,
2294
2333
  refreshDead: Boolean(r.refreshDead),
2295
2334
  rateLimitedUntil: r.rateLimitedUntil ? Date.parse(r.rateLimitedUntil) : null,
2335
+ resetGrants: resetsOverlay.resetGrants,
2336
+ resetCards: resetsOverlay.resetCards,
2337
+ capLift: resetsOverlay.capLift,
2296
2338
  });
2297
2339
  }
2298
2340
 
@@ -2428,41 +2470,82 @@ export class TUI {
2428
2470
  // column showing 0 or the count). z.ai cards + Claude cedar_ember grants,
2429
2471
  // not-yet-expired, on ENABLED accounts only: a disabled account's resets are
2430
2472
  // not ours to spend, and showing a count there would imply otherwise.
2473
+ //
2474
+ // B3 grammar (2026-11-10), rendered width EXACTLY RESETS_W (6):
2475
+ // general n~Xd e.g. 2~119d
2476
+ // scoped nF~Xd e.g. 2F~4d (model-scoped, e.g. Fable)
2477
+ // mixed g+sF e.g. 1+2F
2478
+ // unknown n? scope unknown — count shown, never claimed
2479
+ // not-fresh = gray + trailing '?' (stale listing, dead account, old check)
2480
+ // Count = SUM of resetsLeft over spendable grants + live cards (not entries).
2481
+ // Overflow order: drop the suffix first, then collapse to total+'*'.
2482
+ // Expiry is checked LIVE here: grants endsAt>now, cards !expired &&
2483
+ // (expiresAt==null || expiresAt>now) — a frozen `expired` flag never keeps a
2484
+ // dead reset on screen (pre-mortem #18).
2431
2485
  let resetsCell;
2432
2486
  {
2433
- let n = 0;
2487
+ const now = Date.now();
2488
+ let general = 0; // resetsLeft of general-weekly grants + live cards
2489
+ let scoped = 0; // resetsLeft of model-scoped grants (tagged F)
2490
+ let unknown = 0; // resetsLeft of scope-unknown grants
2491
+ let soonest = Infinity;
2434
2492
  if (a.enabled !== false) {
2493
+ const liveCard = c => c && !c.expired && (c.expiresAt == null || c.expiresAt > now);
2435
2494
  const rc = a.resetCards;
2436
- if (rc) n += (rc.fiveHour || []).filter(c => !c.expired).length
2437
- + (rc.weekly || []).filter(c => !c.expired).length;
2438
- const rg = a.resetGrants;
2439
- if (rg?.eligible) n += (rg.grants || []).filter(g => g.usableNow && !g.expired).length;
2440
- }
2441
- // Expiry rides with the count when it's tight: "1~2d" = 1 reset, expires in ~2 days.
2442
- // Claude grants end mid-Oct (weeks out) — a day-count would be noise; only the
2443
- // SOONEST-expiring card under 7 days gets a suffix, so the column stays narrow
2444
- // and the number it carries still answers "use it or lose it?".
2445
- let suffix = '';
2446
- if (n > 0) {
2447
- let soonest = Infinity;
2448
- const rc = a.resetCards;
2449
- if (rc) for (const c of [...(rc.fiveHour || []), ...(rc.weekly || [])]) {
2450
- if (!c.expired && c.expiresAt != null && c.expiresAt < soonest) soonest = c.expiresAt;
2495
+ if (rc) {
2496
+ for (const c of [...(rc.fiveHour || []), ...(rc.weekly || [])]) {
2497
+ if (liveCard(c)) { general += 1; if (c.expiresAt != null && c.expiresAt < soonest) soonest = c.expiresAt; }
2498
+ }
2451
2499
  }
2452
2500
  const rg = a.resetGrants;
2453
- if (rg?.eligible) for (const g of (rg.grants || [])) {
2454
- if (g.usableNow && !g.expired && g.endsAt != null && g.endsAt < soonest) soonest = g.endsAt;
2455
- }
2456
- // Expiry is ALWAYS shown when a reset exists (owner 2026-10-01: "what is the
2457
- // proper way to communicate if we have a reset, and when does it expire" —
2458
- // both facts, always). Hours under 24h, days at or above.
2459
- const days = soonest === Infinity ? null : (soonest - Date.now()) / 86400_000;
2460
- if (days != null) {
2461
- suffix = days < 1 ? '~' + Math.max(1, Math.round(days * 24)) + 'h'
2462
- : '~' + Math.max(1, Math.round(days)) + 'd';
2501
+ if (rg?.eligible !== false && rg) {
2502
+ for (const g of (rg.grants || [])) {
2503
+ if (!g || g.usableNow === false || g.paused || g.expired) continue;
2504
+ if (g.endsAt != null && g.endsAt <= now) continue; // live expiry
2505
+ // The ONE shared scope answer (A1) — display reads the same
2506
+ // classification as claim and lift.
2507
+ const scopes = appliesTo(g);
2508
+ const hasGeneral = scopes.includes('general');
2509
+ const hasScoped = scopes.some(x => x !== 'general' && x !== 'session');
2510
+ const left = Math.max(0, Math.floor(g.resetsLeft ?? 1));
2511
+ if (hasGeneral) general += left;
2512
+ else if (hasScoped) scoped += left;
2513
+ else unknown += left;
2514
+ if (g.endsAt != null && g.endsAt < soonest) soonest = g.endsAt;
2515
+ }
2463
2516
  }
2464
2517
  }
2465
- resetsCell = rpad(n > 0 ? green(String(n) + suffix) : gray('0'), RESETS_W);
2518
+ const total = general + scoped + unknown;
2519
+ // Not-fresh (pre-mortem #15): stale listing, a dead account, or a check older
2520
+ // than 3x the grants cadence. An account that stopped being probed never gets
2521
+ // re-listed, so checkedAt alone would show a green count forever.
2522
+ const freshGrants = a.resetGrants && a.resetGrants.stale !== true
2523
+ && (a.resetGrants.checkedAt == null || now - a.resetGrants.checkedAt <= LISTING_FRESH_MS);
2524
+ const freshCards = !a.resetCards || (a.resetCards.stale !== true
2525
+ && (a.resetCards.checkedAt == null || now - a.resetCards.checkedAt <= LISTING_FRESH_MS));
2526
+ const notFresh = total > 0 && (!freshGrants || !freshCards || a.refreshDead || a.subscriptionGone);
2527
+
2528
+ // Core text per the grammar.
2529
+ let core;
2530
+ if (total === 0) core = '0';
2531
+ else if (unknown > 0 && general === 0 && scoped === 0) core = `${unknown}?`;
2532
+ else if (general > 0 && scoped > 0) core = `${general}+${scoped}F`;
2533
+ else if (scoped > 0) core = `${scoped}F`;
2534
+ else core = `${general}`;
2535
+
2536
+ // Expiry suffix (owner 2026-10-01: always show when a reset exists).
2537
+ let suffix = '';
2538
+ if (total > 0 && soonest !== Infinity) {
2539
+ const days = (soonest - now) / 86400_000;
2540
+ suffix = days < 1 ? `~${Math.max(1, Math.round(days * 24))}h` : `~${Math.max(1, Math.round(days))}d`;
2541
+ }
2542
+
2543
+ // Fit to EXACTLY RESETS_W: full form, then drop the suffix, then total+'*'.
2544
+ let text = core + suffix;
2545
+ if (text.length > RESETS_W) text = core;
2546
+ if (text.length > RESETS_W) text = `${total}*`.slice(0, RESETS_W);
2547
+ if (notFresh) text = (text + '?').length <= RESETS_W ? text + '?' : `${total}?`.slice(0, RESETS_W);
2548
+ resetsCell = rpad(total > 0 && !notFresh ? green(text) : gray(text), RESETS_W);
2466
2549
  }
2467
2550
 
2468
2551
  if (a.type === 'provider') {
@@ -60,7 +60,10 @@ function normalizeCards(arr, now = Date.now()) {
60
60
  }
61
61
 
62
62
  /** List this account's available reset cards. Zero-spend read.
63
- * Returns { fiveHour: [], weekly: [] } | { error }. */
63
+ * Returns { fiveHour: [], weekly: [] } | { error }.
64
+ * DRIFT (A4, 2026-11-10): a 200 whose expected arrays are missing, or whose raw
65
+ * entries all vanish in normalization (renamed field), is reported as an ERROR —
66
+ * never as an empty "good" list that overwrites the last known cards. */
64
67
  export async function listResetCards(token, { signal, fetchImpl = fetch } = {}) {
65
68
  if (!token) return { error: 'no token' };
66
69
  try {
@@ -74,11 +77,22 @@ export async function listResetCards(token, { signal, fetchImpl = fetch } = {})
74
77
  return { error: `bad_envelope${data?.code != null ? ' code=' + data.code : ''}` };
75
78
  }
76
79
  const now = Date.now();
77
- return {
78
- fiveHour: normalizeCards(data.data.fiveHourResets, now),
79
- weekly: normalizeCards(data.data.weekResets, now),
80
- source: 'zai',
81
- };
80
+ const fiveHour = normalizeCards(data.data.fiveHourResets, now);
81
+ const weekly = normalizeCards(data.data.weekResets, now);
82
+ // Drift guard: the raw arrays are the expected shape; if either is missing, or
83
+ // raw entries exist but none survived normalization, the schema moved. An
84
+ // `available:false` entry is LEGITIMATELY dropped by normalizeCards — only
85
+ // entries that claim to be available count toward the drift check.
86
+ const rawFive = Array.isArray(data.data.fiveHourResets) ? data.data.fiveHourResets : null;
87
+ const rawWeek = Array.isArray(data.data.weekResets) ? data.data.weekResets : null;
88
+ if (rawFive == null || rawWeek == null) {
89
+ return { error: 'drift: expected arrays missing' };
90
+ }
91
+ const availableRaw = [...rawFive, ...rawWeek].filter(c => c && c.available !== false).length;
92
+ if (availableRaw > 0 && fiveHour.length + weekly.length === 0) {
93
+ return { error: `drift: ${availableRaw} raw entr${availableRaw === 1 ? 'y' : 'ies'}, 0 kept` };
94
+ }
95
+ return { fiveHour, weekly, source: 'zai' };
82
96
  } catch (err) {
83
97
  return { error: err.message || String(err) };
84
98
  }