maxpool 1.24.71 → 1.24.73

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.71",
3
+ "version": "1.24.73",
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",
@@ -179,6 +179,13 @@ const DEFAULT_SCHEDULER = {
179
179
  // accounts. So in-flight concurrency is the DOMINANT term, with a steep
180
180
  // per-account soft cap; burn-pace is only a soft de-preference (never a
181
181
  // bench); quota "use-it-or-lose-it" is intentionally a minor signal here.
182
+ // Cache-affinity discount (owner 2026-11-08, see _cacheAffinity): score points
183
+ // a conversation's last-used healthy account sheds (every mode except sticky).
184
+ // On an undiscounted row one in-flight weight unit costs ~12 points past the
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.
188
+ cacheAffinityBonus: 400,
182
189
  concurrencyWeight: 2, // multiplies in-flight load (activeWeight+reqWeight) — dominant
183
190
  perAccountConcurrencyTarget: 3, // D: soft per-account in-flight target; past it, capPenalty bites
184
191
  capPenaltyWeight: 10, // steep penalty per unit of in-flight depth past D (throttle safety floor)
@@ -332,9 +339,10 @@ const PERSISTED_QUOTA_FIELDS = [
332
339
  // drops until the next probe sweep re-learns it. The probe still rewrites it on
333
340
  // every successful sweep, so a plan change heals on the same cadence.
334
341
  'weeklyAbsent', 'capSpanSes', 'capSpanWk', 'providerSes', 'providerSesReset', 'providerWk', 'providerWkReset',
335
- 'providerQuotaSource', 'lastProbeOkAt',
342
+ 'providerQuotaSource', 'lastProbeOkAt', 'noPlan',
336
343
  ];
337
344
 
345
+ const CONVERSATION_AFFINITY_MAX = 5000;
338
346
  const PROVIDER_429_MIN_BENCH_SEC = 60;
339
347
  const PROVIDER_429_MAX_BENCH_SEC = 15 * 60;
340
348
  const PROVIDER_429_STREAK_FORGET_MS = 60 * 60 * 1000;
@@ -591,6 +599,10 @@ export class AccountManager {
591
599
  this.routingMode = 'automatic';
592
600
  this.preferredAccountName = null;
593
601
  this.sessionBindings = new Map();
602
+ // Cache affinity: conversation (terminal + prefix hash) → last account name.
603
+ // Bounded LRU — oldest conversations drop first; a dropped entry only means the
604
+ // next turn is balanced fresh.
605
+ this.conversationLastAccount = new Map();
594
606
  this._peakCache = null; // per-UTC-minute peak-window memo (see _peakStateFor)
595
607
  // CAPACITY LEDGER (2026-08-22): observed tokens per completed window cycle, so
596
608
  // "true capacity" is comparable across providers. Restored from state on boot.
@@ -870,6 +882,7 @@ export class AccountManager {
870
882
  // disagree with the peak tier that caused the move (defaults to real time).
871
883
  this._bindSession(requestInfo.sessionKey, account, requestInfo.model, requestInfo.now);
872
884
  }
885
+ this._noteConversationAccount(requestInfo, account);
873
886
  account.inFlight++;
874
887
  account.activeWeight += weight;
875
888
  account.lastUsedAt = Date.now();
@@ -2232,9 +2245,9 @@ export class AccountManager {
2232
2245
  }
2233
2246
  }
2234
2247
  // Session binding only applies in 'sticky' mode. Every other mode scores each
2235
- // request independently — the whole point of 'balance' is that 20 sessions that
2236
- // happened to start on the same account do NOT keep hammering it while others
2237
- // idle. Warmup-pull still works under the non-sticky modes because it keys on
2248
+ // request — so 20 sessions that started on one account do NOT keep hammering it
2249
+ // while others idle — but with a bounded cache-affinity discount for the
2250
+ // conversation's last account (_cacheAffinity, owner 2026-11-08). Warmup-pull still works under the non-sticky modes because it keys on
2238
2251
  // `_isWarming`, not on a binding.
2239
2252
  const isStickyMode = this.scheduler.routingMode === 'sticky';
2240
2253
  const bound = isStickyMode
@@ -3035,6 +3048,8 @@ export class AccountManager {
3035
3048
 
3036
3049
  _scoreAccount(account, requestInfo = {}, ctx = null, criticalUnlock = null) {
3037
3050
  const now = ctx?.now ?? Date.now();
3051
+ // CACHE AFFINITY — see _cacheAffinity.
3052
+ const affinity = this._cacheAffinity(account, requestInfo);
3038
3053
  const reqWeight = Math.max(1, requestInfo.weight || 1);
3039
3054
  // POOL LINK scores as the cloud's WORST-case load (owner 2026-10-29: "the team
3040
3055
  // pool will respect the accounts within it... included in the balancing act").
@@ -3146,7 +3161,70 @@ export class AccountManager {
3146
3161
  // default) learns the real number within a cycle. `probing`/requalify still
3147
3162
  // flags a never-seen account for learning — that path is unchanged.
3148
3163
 
3149
- return concurrency + capPenalty + paceCost + utilizationCost + scopedPace + spread + ramp + reserveCost + criticalCost + failurePenalty;
3164
+ return Math.max(0, concurrency + capPenalty + paceCost + utilizationCost + scopedPace + spread + ramp + reserveCost + criticalCost + failurePenalty - affinity);
3165
+ }
3166
+
3167
+ /**
3168
+ * CACHE AFFINITY (owner 2026-11-08): Anthropic's prompt cache is PER ACCOUNT, so
3169
+ * a conversation that hops accounts re-writes its whole prefix — 72% of all
3170
+ * Anthropic cache writes over 24h were such re-writes (measured 2026-11-08). In
3171
+ * balance mode the account this session used LAST sheds `cacheAffinityBonus`
3172
+ * score points, so the conversation stays put unless there is a MAJOR reason to
3173
+ * move ("stay unless a major preference or truly no other account" — owner):
3174
+ * - availability gates reject it (limit, exclusion, auth) → never rescued here;
3175
+ * - it is no longer healthy: weekly at/over the reserve tier, the 5h window at
3176
+ * or over the same mark, or a failure streak → affinity off, plain balancing;
3177
+ * - it carries roughly two more large requests than the best sibling (one
3178
+ * weight unit in flight costs ~12 points past the per-account target).
3179
+ * Every account type (owner 2026-11-08: treat GLM the same). Measured the same
3180
+ * day: GLM conversations hopped accounts on 39% of turns yet z.ai still reported
3181
+ * 98.6% cache reads, so z.ai's cache is not per-key — affinity buys GLM nothing
3182
+ * there but costs nothing either, and one rule is simpler to reason about.
3183
+ * Applies in every scored mode (balance, prefer-*); never in sticky (see below).
3184
+ */
3185
+ _noteConversationAccount(requestInfo, account) {
3186
+ const key = this._affinityKey(requestInfo);
3187
+ if (!key) return;
3188
+ const map = this.conversationLastAccount;
3189
+ map.delete(key); // re-insert = most recent (LRU order)
3190
+ map.set(key, account.name);
3191
+ while (map.size > CONVERSATION_AFFINITY_MAX) map.delete(map.keys().next().value);
3192
+ }
3193
+
3194
+ /** Conversation identity: terminal + prefix hash when the server computed one;
3195
+ * falls back to the terminal session key (callers that never set the hash,
3196
+ * e.g. tests and pre-hash call sites). */
3197
+ _affinityKey(requestInfo = {}) {
3198
+ return requestInfo.affinityKey || requestInfo.sessionKey || null;
3199
+ }
3200
+
3201
+ _cacheAffinity(account, requestInfo = {}) {
3202
+ const base = Number(this.scheduler.cacheAffinityBonus) || 0;
3203
+ // Sticky mode has its own binding; its score loop runs only once the session has
3204
+ // decided to LEAVE a hot account — a discount there would pull it straight back.
3205
+ if (base <= 0 || this.scheduler.routingMode === 'sticky') return 0;
3206
+ const key = this._affinityKey(requestInfo);
3207
+ if (!key || this.conversationLastAccount.get(key) !== account.name) return 0;
3208
+ // No readable quota (Kimi's coding key is console-only) → nothing would ever
3209
+ // release the hold before a 429; give such rows plain balancing (red-team r1).
3210
+ if (account.type === 'provider' && account.quota?.providerQuotaSource === 'console-only') return 0;
3211
+ const weeklyState = this._weeklyRawState(account);
3212
+ // Holds through 'soft' (≥65% weekly — most accounts by mid-week) and 'unknown'
3213
+ // (never probed); reserve/critical/exhausted/capped release the conversation.
3214
+ if (!['normal', 'soft', 'unknown'].includes(weeklyState)) return 0;
3215
+ // 5-hour window: Anthropic reports unified5h; GLM/Kimi report providerSes.
3216
+ const five = account.type === 'provider' ? account.quota?.providerSes : account.quota?.unified5h;
3217
+ if (five != null && five >= this.scheduler.weeklyReserveThreshold) return 0;
3218
+ if ((account.consecutiveFailures || 0) > 0) return 0;
3219
+ // Scale to THIS row's price of one in-flight unit, so every account tolerates the
3220
+ // same extra DEPTH (~33 units) — a fast-refill/soak-discounted GLM row prices a
3221
+ // unit at ~0.4x, and an unscaled bonus let it run ~2.5x deeper (red-team r1).
3222
+ const refillMult = this._fastRefillMultiplier(account);
3223
+ const safetyMult = this.scheduler.soakDiscount < 1
3224
+ ? this._fastRefillMultiplier(account, { capFloor: true }) : refillMult;
3225
+ const unit = this.scheduler.concurrencyWeight * refillMult + this.scheduler.capPenaltyWeight * safetyMult;
3226
+ const fullUnit = this.scheduler.concurrencyWeight + this.scheduler.capPenaltyWeight;
3227
+ return base * Math.min(1, unit / fullUnit);
3150
3228
  }
3151
3229
 
3152
3230
  /**
@@ -3802,7 +3880,20 @@ export class AccountManager {
3802
3880
  if (usage.source === 'console-only') q.providerQuotaSource = 'console-only';
3803
3881
  return;
3804
3882
  }
3883
+ if (usage.noPlan) {
3884
+ // The key authenticates but the account HAS no plan (z.ai code 500 "no coding
3885
+ // plan", e.g. cancelled/expired — measured 2026-11-08). That is a stable
3886
+ // terminal state for the quota probe: show it in the row instead of "probing"
3887
+ // forever, and stop treating the row as a quota unknown.
3888
+ q.providerQuotaSource = 'zai';
3889
+ q.noPlan = true;
3890
+ q.providerSes = null; q.providerSesReset = null;
3891
+ q.providerWk = null; q.providerWkReset = null;
3892
+ q.lastProbeOkAt = Date.now();
3893
+ return;
3894
+ }
3805
3895
  q.providerQuotaSource = usage.source || 'zai';
3896
+ q.noPlan = false; // a real reading means the plan exists (renewed / re-bought)
3806
3897
  if (usage.ses) {
3807
3898
  if (usage.ses.utilization != null) q.providerSes = clamp01(usage.ses.utilization);
3808
3899
  if (usage.ses.resetAt != null) {
package/src/oauth.js CHANGED
@@ -417,6 +417,13 @@ export async function fetchProviderUsage(account) {
417
417
  if (!res.ok) return { error: `HTTP ${res.status}`, status: res.status };
418
418
  const data = await res.json();
419
419
  if (data?.code !== 200 || !data?.data) {
420
+ // HTTP 200 with a `success:false` body is a POSITIVE answer, not a transient
421
+ // probe failure: "当前用户不存在coding plan" (code 500 — the account has NO
422
+ // coding plan, measured 2026-11-08 on a cancelled plan) or an empty envelope.
423
+ // Returning it as `error` kept the TUI bars on "probing" forever while the
424
+ // row's real state — no plan to read — went unnamed.
425
+ const noPlan = data?.code === 500 && /coding plan/i.test(String(data?.msg || ''));
426
+ if (noPlan) return { noPlan: true, source: 'zai' };
420
427
  return { error: `bad_envelope${data?.code != null ? ' code=' + data.code : ''}` };
421
428
  }
422
429
  const now = Date.now();
package/src/server.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash } from 'node:crypto';
1
2
  import http from 'node:http';
2
3
  import { normalizeClientHeaders, rewriteMetadataUserIdRaw } from './client-identity.js';
3
4
  import { appendEventLog } from './event-log.js';
@@ -496,6 +497,10 @@ export function createProxyServer(accountManager, config, hooks = {}) {
496
497
  console.error('[Maxpool] x-maxpool-pin-account dropped: the link authenticated with the remote\'s legacy apiKey — mint a named client key (proxy.clients) so pins and per-person attribution work');
497
498
  }
498
499
  requestInfo.sessionKey = headerValue(req.headers, 'x-maxpool-session');
500
+ // Affinity key = terminal + conversation. Works without the session header
501
+ // too (bare `claude`, teammates' launchers): the conversation hash alone.
502
+ requestInfo.affinityKey = requestInfo.conversationHash
503
+ ? `${requestInfo.sessionKey || ''}:${requestInfo.conversationHash}` : null;
499
504
  requestInfo.caller = req._caller || null;
500
505
  // Read per request (not captured at boot) so a config flip + seamless reload
501
506
  // takes effect on the next request with no restart.
@@ -918,6 +923,28 @@ async function forwardRequest(
918
923
  upRes = await fetch(linkUrl, { method: req.method, headers: linkHeaders, body: ['GET', 'HEAD'].includes(req.method) ? undefined : body, duplex: 'half', signal: linkCtl.signal });
919
924
  } finally { clearTimeout(linkTtfb); }
920
925
  ctx.status = upRes.status;
926
+ // The linked pool refused the request's SHAPE (not Claude Code). The cloud
927
+ // subscription accounts are healthy — exclude them (this mirror + every local
928
+ // oauth row) and fail over to routes that CAN serve the shape (apikey/provider);
929
+ // pass the refusal through only when none remains. (Red-team r2: answering
930
+ // outright starved the request of local routes when a mirror was picked first.)
931
+ if (account.remote && upRes.status === 429 && upRes.headers.get(REQUEST_SHAPE_REFUSAL_HEADER) && !clientHasRealBytes(res, requestInfo)) {
932
+ const errorText = await readErrorBody(upRes);
933
+ finishLink({ status: 429, error: 'request_shape_refused', neutral: true });
934
+ excludedIndexes.add(account.index);
935
+ for (const a of accountManager.accounts) {
936
+ if (a.type === 'oauth') excludedIndexes.add(a.index);
937
+ }
938
+ if (
939
+ retryCount + 1 < maxAttempts
940
+ && !res.headersSent
941
+ && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)
942
+ ) {
943
+ return forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes);
944
+ }
945
+ sendErrorBody(res, requestInfo, 429, errorText, upRes.headers);
946
+ return;
947
+ }
921
948
  // Pinned cloud account refused (rate-limited / unavailable) before any byte
922
949
  // streamed: cool THIS mirror row and fail over through the normal path to the
923
950
  // next-best account, local or cloud — exactly like a local account's 429.
@@ -1122,6 +1149,54 @@ async function forwardRequest(
1122
1149
  const errorBody = await readErrorBody(upstreamRes);
1123
1150
  const retryAfter = parseRetryAfter(upstreamRes.headers.get('retry-after'))
1124
1151
  || parseProviderRetryAfter(errorBody, account.provider);
1152
+ // REQUEST-SHAPE refusal (2026-11-08): on a subscription login Anthropic serves
1153
+ // Opus/Sonnet only to Claude-Code-shaped requests; anything else gets a bare 429
1154
+ // ("Error", no quota headers) while Haiku on the same account is served —
1155
+ // bisected live, the identity system line alone flips 429 → 200. The ACCOUNT is
1156
+ // healthy, so benching it (60s) blocked every real session on it, and failing
1157
+ // over just collected the same refusal from every other account. Answer THIS
1158
+ // request and leave the account untouched.
1159
+ if (account.type === 'oauth' && !/haiku/.test(String(requestInfo.model || '')) && isRequestShapeRefusal(rateLimitHeaders, errorBody, body)) {
1160
+ // Anthropic answered, but a bare headerless "Error" 429 is also what a REAL
1161
+ // fleet throttle looks like to this shape — evidence neither way. Hand the
1162
+ // recovery probe back UNUSED: no deferral, no clearing; the very next request
1163
+ // (often the apikey failover in this same chain) claims it (red-team r2).
1164
+ accountManager.relinquishUpstreamProbe(lease);
1165
+ accountManager.releaseAccount(lease, { status: 429, error: 'request_shape_refused', neutral: true });
1166
+ console.log(`[Maxpool] 429 on "${account.name}" is a request-shape refusal (not Claude Code) — subscription accounts left active`);
1167
+ if (logDir) {
1168
+ logSections.push(`=== RESPONSE 429 — "${account.name}" refused request shape (subscription accounts untouched) ===\n${formatHeaders(upstreamRes.headers)}`);
1169
+ if (errorBody) logSections.push(`=== ERROR BODY ===\n${errorBody}`);
1170
+ }
1171
+ // Only SUBSCRIPTION logins refuse this shape. API-key and provider rows can
1172
+ // still serve it — exclude every subscription-shaped account (local oauth rows
1173
+ // and their cloud mirrors) and fail over if such a route remains (red-team
1174
+ // 2026-11-08: answering outright starved a request GLM would have served).
1175
+ excludedIndexes.add(account.index);
1176
+ for (const a of accountManager.accounts) {
1177
+ if (a.type === 'oauth') excludedIndexes.add(a.index); // local logins AND their cloud mirrors
1178
+ }
1179
+ if (
1180
+ canRetryBufferedBody
1181
+ && retryCount + 1 < maxAttempts
1182
+ && !res.headersSent
1183
+ && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)
1184
+ ) {
1185
+ if (logDir) writeRequestLog(logDir, reqId, logSections);
1186
+ return forwardRequest(
1187
+ req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir,
1188
+ retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes,
1189
+ );
1190
+ }
1191
+ if (logDir) writeRequestLog(logDir, reqId, logSections);
1192
+ ctx.status = 429;
1193
+ sendErrorResponse(res, requestInfo, 429, {
1194
+ type: 'error',
1195
+ error: { type: 'rate_limit_error', message: REQUEST_SHAPE_REFUSAL_MESSAGE },
1196
+ }, { [REQUEST_SHAPE_REFUSAL_HEADER]: '1' });
1197
+ return;
1198
+ }
1199
+
1125
1200
  const rateLimit = classifyRateLimit(account, rateLimitHeaders, errorBody, { model: requestInfo.model, retryAfter });
1126
1201
  if (rateLimit.scope === 'upstream') {
1127
1202
  const parsedError = parseJsonError(errorBody);
@@ -2353,7 +2428,7 @@ function isContextLengthError(errorBody) {
2353
2428
  return /exceeded model token limit|maximum context length|context length exceeded|context window (?:size )?(?:exceeded|too)|prompt is too long|input is too long|reduce the length of|too many (?:input )?tokens|request too large/i.test(errorBody);
2354
2429
  }
2355
2430
 
2356
- export const __serverTest = { directiveOnlySystemMessages, rewriteBodyForAccount, sanitizeBlocksForProvider, reanchorOrphanedSystemMessages, unavailableMessage, computeQueueWindowMs, waitForAvailableRoute, isRetriableUpstreamStatus, classifyEffortRejection, repairEffort, isCapacitySignalStatus, isStrippableThinkingBlock, stripForeignThinkingBlocks, parseRejectedBlockPath, stripRejectedBlockClass, peekRejectedBlockType, describeRejectedBlock, headerValue, getMaxpoolProfile, ensureQueueHeartbeat, clearQueueHeartbeat, commitStreamGraceHeartbeat, describeRequest, classifyRateLimit, detectTranscriptOrigin, isAnthropicIncompatBody, isContextLengthError, isProviderParamRejection, describeBodyShape, streamResponse, startIdleRequestReaper, normalizeModelEcho, classifyHeldStreamPrefix };
2431
+ export const __serverTest = { directiveOnlySystemMessages, rewriteBodyForAccount, sanitizeBlocksForProvider, reanchorOrphanedSystemMessages, unavailableMessage, computeQueueWindowMs, waitForAvailableRoute, isRetriableUpstreamStatus, classifyEffortRejection, repairEffort, isCapacitySignalStatus, isStrippableThinkingBlock, stripForeignThinkingBlocks, parseRejectedBlockPath, stripRejectedBlockClass, peekRejectedBlockType, describeRejectedBlock, headerValue, getMaxpoolProfile, ensureQueueHeartbeat, clearQueueHeartbeat, commitStreamGraceHeartbeat, describeRequest, classifyRateLimit, isRequestShapeRefusal, looksLikeClaudeCode, parseProviderRetryAfter, secondsUntilParsedTime, detectTranscriptOrigin, isAnthropicIncompatBody, isContextLengthError, isProviderParamRejection, describeBodyShape, streamResponse, startIdleRequestReaper, normalizeModelEcho, classifyHeldStreamPrefix };
2357
2432
 
2358
2433
  async function readErrorBody(upstreamRes, limitBytes = 64 * 1024) {
2359
2434
  if (!upstreamRes.body) return '';
@@ -2384,9 +2459,13 @@ function parseProviderRetryAfter(body, provider) {
2384
2459
  const message = parsed?.message || '';
2385
2460
 
2386
2461
  if (provider === 'zai') {
2387
- const nextFlush = message.match(/reset at\s+`?([^`\n]+?)`?$/i)?.[1]
2388
- || message.match(/next_flush_time[:\s]+`?([^`\n]+?)`?$/i)?.[1];
2389
- const resetSeconds = secondsUntilParsedTime(nextFlush);
2462
+ // z.ai embeds the reset as "...will reset at 2026-11-12 04:28:53][<request-id>]"
2463
+ // (measured 2026-11-08, code 1310): the bracketed request id trails the time, so
2464
+ // an anchor to end-of-string never matches and the parsed time became garbage.
2465
+ // Capture the time inside ITS bracket and hand only that to the time parser.
2466
+ const resetMatch = message.match(/reset at\s+`?(\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)`?/i)
2467
+ || message.match(/next_flush_time[:\s]+`?([^`\]\n]+?)`?(?:\]|$)/i);
2468
+ const resetSeconds = secondsUntilParsedTime(resetMatch?.[1], { tzOffsetMs: ZAI_TZ_OFFSET_MS });
2390
2469
  if (resetSeconds) return resetSeconds;
2391
2470
 
2392
2471
  if (['1302', '1303', '1305'].includes(String(code))) return 60;
@@ -2418,6 +2497,34 @@ function parseJsonError(body) {
2418
2497
  }
2419
2498
  }
2420
2499
 
2500
+ const REQUEST_SHAPE_REFUSAL_HEADER = 'x-maxpool-request-shape-refused';
2501
+ const REQUEST_SHAPE_REFUSAL_MESSAGE = 'Anthropic serves this model on subscription accounts only to Claude Code requests. '
2502
+ + 'This request does not identify as Claude Code (its system prompt lacks the Claude Code identity line), '
2503
+ + 'so every pooled account refuses it the same way. Send it from Claude Code, or use a Haiku model.';
2504
+ const CLAUDE_CODE_IDENTITY = /^You are (Claude Code, Anthropic's official CLI for Claude|a Claude agent, built on Anthropic's Claude Agent SDK)/;
2505
+
2506
+ /** True when the request carries Claude Code's identity line as a leading system
2507
+ * block. Parse failure → true (unknown shape is never treated as a refusal). */
2508
+ function looksLikeClaudeCode(requestBody) {
2509
+ let parsed;
2510
+ try { parsed = JSON.parse(Buffer.isBuffer(requestBody) ? requestBody.toString() : String(requestBody)); } catch { return true; }
2511
+ const sys = parsed?.system;
2512
+ const blocks = typeof sys === 'string' ? [sys] : Array.isArray(sys) ? sys.map(b => b?.text) : [];
2513
+ return blocks.slice(0, 3).some(t => typeof t === 'string' && CLAUDE_CODE_IDENTITY.test(t.trimStart()));
2514
+ }
2515
+
2516
+ /** A 429 that refuses the request's SHAPE, not the account's quota: NO unified quota
2517
+ * headers at all, Anthropic's bare `rate_limit_error` "Error" body, and a request
2518
+ * that does not identify as Claude Code. All three must hold — a headerless 429 on a
2519
+ * real Claude Code request keeps the account-wide handling (red-team: a genuine cap
2520
+ * with stripped headers must still bench). */
2521
+ function isRequestShapeRefusal(headers, errorBody, requestBody) {
2522
+ if (Object.keys(headers).some(k => k.toLowerCase().startsWith('anthropic-ratelimit-unified'))) return false;
2523
+ const parsed = parseJsonError(errorBody);
2524
+ if (String(parsed?.type || '') !== 'rate_limit_error' || String(parsed?.message || '').trim() !== 'Error') return false;
2525
+ return !looksLikeClaudeCode(requestBody);
2526
+ }
2527
+
2421
2528
  function classifyRateLimit(account, headers, body, opts = {}) {
2422
2529
  if (account.type === 'provider') return { scope: 'account', fingerprint: null };
2423
2530
 
@@ -2514,11 +2621,24 @@ function recordRequestIncident(requestInfo, fingerprint, accountIndex, retryAfte
2514
2621
  return incident;
2515
2622
  }
2516
2623
 
2517
- function secondsUntilParsedTime(value) {
2624
+ // z.ai prints its reset timestamps in China time (UTC+8) with no zone marker —
2625
+ // measured 2026-11-08: the 1310 message said 04:28:53 while the account's own
2626
+ // quota API reported the same window as 20:28:53Z. Parsing it as UTC made every
2627
+ // reset 8h too late and once even landed in the past. Anthropic's own header
2628
+ // times are ISO-with-zone and never carry a raw offset.
2629
+ const ZAI_TZ_OFFSET_MS = 8 * 60 * 60 * 1000;
2630
+
2631
+ function secondsUntilParsedTime(value, { tzOffsetMs = 0 } = {}) {
2518
2632
  if (!value) return null;
2519
2633
  const trimmed = String(value).trim();
2520
- const dateMs = Date.parse(trimmed);
2521
- if (!Number.isNaN(dateMs)) {
2634
+ const naive = trimmed.match(/^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)$/);
2635
+ const asUtcMs = naive
2636
+ ? Date.parse(`${naive[1]}T${naive[2]}Z`)
2637
+ : Date.parse(trimmed);
2638
+ if (!Number.isNaN(asUtcMs)) {
2639
+ // The offset applies ONLY to a zone-less "YYYY-MM-DD HH:MM:SS"; a time that
2640
+ // names its zone is already absolute.
2641
+ const dateMs = naive ? asUtcMs - tzOffsetMs : asUtcMs;
2522
2642
  return Math.min(Math.max(Math.ceil((dateMs - Date.now()) / 1000), 1), 24 * 60 * 60);
2523
2643
  }
2524
2644
  const n = Number(trimmed);
@@ -3358,6 +3478,19 @@ function describeRequest(req, body) {
3358
3478
  const json = JSON.parse(body.toString());
3359
3479
  if (json.model) info.model = json.model;
3360
3480
  if (json.stream) info.stream = true;
3481
+ // CONVERSATION identity for cache affinity (red-team 2026-11-08): the
3482
+ // x-maxpool-session header names a TERMINAL, and a main thread plus its parallel
3483
+ // subagents share it — so "last account used by this session" chased whichever
3484
+ // request landed last. A conversation's system prompt + first message never
3485
+ // change within it (Claude Code relies on that for its own cache prefix), so a
3486
+ // short hash of those separates the main thread from each subagent.
3487
+ if (Array.isArray(json.messages) && json.messages.length) {
3488
+ info.conversationHash = createHash('sha256')
3489
+ .update(JSON.stringify(json.system ?? ''))
3490
+ .update('\u0000')
3491
+ .update(JSON.stringify(json.messages[0] ?? ''))
3492
+ .digest('hex').slice(0, 16);
3493
+ }
3361
3494
  if (json.max_tokens && json.max_tokens > 16_000) weight += 1;
3362
3495
  if (json.thinking || json.effort) weight += 1;
3363
3496
  if (requiresAnthropicThinkingIntegrity(json)) {
package/src/tui.js CHANGED
@@ -2631,6 +2631,12 @@ export class TUI {
2631
2631
  sesCell = emptyBar('n/a', bw);
2632
2632
  wkCell = emptyBar('n/a', bw);
2633
2633
  note = ` ${dim('console-only')}`;
2634
+ } else if (q.noPlan) {
2635
+ // z.ai answered: this key's account has no coding plan (cancelled/expired).
2636
+ // Name it — "probing" here read as a pool fault for days (owner, 2026-11-08).
2637
+ sesCell = emptyBar('no plan', bw);
2638
+ wkCell = emptyBar('no plan', bw);
2639
+ note = ` ${red('z.ai says no coding plan — check billing')}`;
2634
2640
  } else {
2635
2641
  sesCell = emptyBar('probing', bw);
2636
2642
  wkCell = emptyBar('probing', bw);