maxpool 1.24.51 → 1.24.53

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.51",
3
+ "version": "1.24.53",
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/pool-link.js CHANGED
@@ -129,6 +129,34 @@ export function summarizeRemote(status) {
129
129
  }
130
130
 
131
131
  /** Test seam holder for PoolLink's timer (so tests don't leak handles). */
132
+ /** Strip terminal control characters (ESC + C0/C1 except tab) from a string that
133
+ * came off-box. Remote pool/account/caller names are rendered raw into the owner's
134
+ * terminal; an OSC 52 / CSI sequence there can rewrite the clipboard or blank the
135
+ * screen (security review 2026-10-31). Sanitized at INGRESS so no renderer has to. */
136
+ export function safeText(v) {
137
+ if (typeof v !== 'string') return v;
138
+ return v.replace(/[\u0000-\u0008\u000A-\u001F\u007F-\u009F]/g, '');
139
+ }
140
+
141
+ function sanitizeRemoteStatus(json) {
142
+ if (!json || typeof json !== 'object') return json;
143
+ for (const a of Array.isArray(json.accounts) ? json.accounts : []) {
144
+ if (a && typeof a === 'object') { a.name = safeText(a.name); a.provider = safeText(a.provider); a.lastError = safeText(a.lastError); }
145
+ }
146
+ for (const p of Array.isArray(json.pools) ? json.pools : []) {
147
+ if (p && typeof p === 'object') {
148
+ p.name = safeText(p.name);
149
+ for (const a of Array.isArray(p.accounts) ? p.accounts : []) if (a && typeof a === 'object') a.name = safeText(a.name);
150
+ }
151
+ }
152
+ if (json.callers && typeof json.callers === 'object') {
153
+ const clean = {};
154
+ for (const [k, v] of Object.entries(json.callers)) clean[safeText(k)] = v;
155
+ json.callers = clean;
156
+ }
157
+ return json;
158
+ }
159
+
132
160
  export class PoolLink {
133
161
  constructor(def, deps = {}) {
134
162
  this.name = def.name;
@@ -154,7 +182,7 @@ export class PoolLink {
154
182
  try {
155
183
  const r = await this._fetchRemoteStatus(this.url, this.key);
156
184
  if (r.ok) {
157
- this.status = r.json;
185
+ this.status = sanitizeRemoteStatus(r.json);
158
186
  this.lastOkAt = r.at;
159
187
  this.lastError = null;
160
188
  this.identities = remoteIdentities(r.json);
@@ -60,9 +60,7 @@ export function decideZai(account, cards, now = Date.now()) {
60
60
  if (!cards || cards.error) return null;
61
61
  if (account?.outsideUsage?.outside) return null; // R7: key used by someone else — a card would feed the leak
62
62
  const q = account.quota || {};
63
- const ses = q.providerSes ?? null;
64
- const wk = q.weeklyAbsent ? null : (q.providerWk ?? null);
65
- const wkUtil = wk; const sesUtil = ses;
63
+ const wkUtil = q.weeklyAbsent ? null : (q.providerWk ?? null);
66
64
 
67
65
  const weeklyCards = (cards.weekly || []).filter(c => !c.expired && c.recordId != null);
68
66
  // R4: soonest expiry first inside each class.
@@ -72,10 +70,12 @@ export function decideZai(account, cards, now = Date.now()) {
72
70
 
73
71
  // --- weekly card: clears BOTH windows (z.ai docs; measured: glm1 100%->0% both)
74
72
  if (weeklyCard) {
75
- const worst = Math.max(wkUtil ?? 0, sesUtil ?? 0);
76
73
  const atWall = wkUtil != null && wkUtil >= POLICY.wallWeekly;
74
+ // Dying gate reads the WEEKLY window alone (owner 2026-10-30: resets are never
75
+ // spent on session limits). The old Math.max(wk,ses) let a 96% 5h window redeem
76
+ // a scarce weekly card — the exact trade the owner banned.
77
77
  const dying = msLeft(weeklyCard.expiresAt, now) < POLICY.dyingWindowMs
78
- && worst >= POLICY.dyingMinUtilWeekly;
78
+ && wkUtil != null && wkUtil >= POLICY.dyingMinUtilWeekly;
79
79
  if (atWall || dying) {
80
80
  // R6: a wall redeem waits when the FREE weekly refill is imminent and the
81
81
  // account isn't critically full (dying-value redeems stay exempt above).
@@ -111,7 +111,6 @@ export function decideClaude(account, listing, now = Date.now()) {
111
111
 
112
112
  const q = account.quota || {};
113
113
  const wkUtil = q.unified7d ?? null;
114
- const sesUtil = q.unified5h ?? null;
115
114
  const clearsWk = chosen.clears?.includes('seven_day');
116
115
 
117
116
  // Owner, 2026-10-30: "never spend resets on session limits, only on weekly."
@@ -122,7 +121,7 @@ export function decideClaude(account, listing, now = Date.now()) {
122
121
 
123
122
  // use_requires_limit: some grants may only fire AT the wall (server-enforced).
124
123
  const dying = msLeft(chosen.endsAt, now) < POLICY.dyingWindowMs
125
- && Math.max(wkUtil ?? 0, sesUtil ?? 0) >= POLICY.dyingMinUtilWeekly;
124
+ && wkUtil != null && wkUtil >= POLICY.dyingMinUtilWeekly; // weekly window only (owner 2026-10-30)
126
125
  // A requires-limit grant cannot be redeemed pre-wall even for dying value.
127
126
  if (atWall || (dying && !chosen.requiresLimit)) {
128
127
  return { kind: 'grant', grant: chosen, reason: atWall ? 'at wall' : 'dying value' };
package/src/screen.html CHANGED
@@ -110,7 +110,9 @@ h1{font-size:22px;margin:0;letter-spacing:-.3px}
110
110
  function render(s) {
111
111
  // Live needs-login accounts first; switched-off accounts last.
112
112
  function rank(a) { return a.enabled === false ? 2 : (a.refreshDead ? 0 : 1); }
113
- var accts = (s.accounts || []).slice().sort(function (x, y) {
113
+ // Mirror rows (a.remote) render only under their pool chip below — keeping
114
+ // them in the main grid duplicated every cloud account on the linking machine.
115
+ var accts = (s.accounts || []).filter(function (a) { return !a.remote; }).sort(function (x, y) {
114
116
  return rank(x) - rank(y) || String(x.name).localeCompare(String(y.name));
115
117
  });
116
118
  var dead = accts.filter(function (a) { return a.refreshDead && a.enabled !== false; }).length;
@@ -134,7 +136,7 @@ h1{font-size:22px;margin:0;letter-spacing:-.3px}
134
136
  }).join('');
135
137
  var ver = (s.runningVersion || (s.version && s.version.current) || '');
136
138
  var cloud = (s.pools || []).reduce(function (n, p) { return n + (p.accounts || []).length; }, 0);
137
- app.innerHTML = '<header><h1>Maxpool accounts</h1><span class="meta">' + accts.length + ' local' + (cloud ? ' · ' + cloud + ' cloud' : '') + ' accounts' +
139
+ app.innerHTML = '<header><h1>Maxpool accounts</h1><span class="meta">' + accts.length + ' on this Mac' + (cloud ? ' · ' + cloud + ' cloud' : '') + ' accounts' +
138
140
  (dead ? ' · <b style="color:var(--bad)">' + dead + (dead === 1 ? ' needs login' : ' need login') + '</b>' : '') +
139
141
  (ver ? ' · v' + esc(ver) : '') + ' · updated ' + new Date().toLocaleTimeString() + '</span>' +
140
142
  '<button class="link" id="out">Forget key</button></header>' +
package/src/server.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import http from 'node:http';
2
2
  import { appendEventLog } from './event-log.js';
3
3
  import { atomicConfigUpdate } from './config.js';
4
- import { HOP_HEADER, nextHopCount } from './pool-link.js';
4
+ import { HOP_HEADER, nextHopCount, safeText } from './pool-link.js';
5
5
  import { ThreadOwners, readThreadIntent, threadRefusalBody, isThreadlessAccount } from './thread-gate.js';
6
6
  import { writeFile, mkdir } from 'node:fs/promises';
7
7
  import { readFileSync } from 'node:fs';
@@ -167,16 +167,23 @@ export function createProxyServer(accountManager, config, hooks = {}) {
167
167
  // A non-loopback listen host also forces strict even without clients (a shared
168
168
  // pool must never trust by address). Default local config (loopback host, no
169
169
  // clients) keeps today's behavior exactly.
170
- const clientList = Array.isArray(config.proxy?.clients)
171
- ? config.proxy.clients.filter(c => c && typeof c.key === 'string' && c.key)
172
- : [];
170
+ const rawClients = Array.isArray(config.proxy?.clients) ? config.proxy.clients : [];
171
+ const clientList = rawClients.filter(c => c && typeof c.key === 'string' && c.key);
172
+ // Fail CLOSED on intent: a non-empty `clients` list means the operator asked for
173
+ // per-person keys, even if every entry is malformed (hand-edited VM config, a key
174
+ // quoted as a number). Each dropped entry is logged so the typo is visible.
175
+ for (const c of rawClients) {
176
+ if (!(c && typeof c.key === 'string' && c.key)) {
177
+ console.error(`[Maxpool] proxy.clients entry ignored (missing/invalid key): name=${safeText(String(c?.name ?? '?'))}`);
178
+ }
179
+ }
173
180
  const clientByKey = new Map(clientList.map(c => [c.key, c.name]));
174
181
  // '0.0.0.0' stays in the trusted set for backward compatibility (a solo user who
175
182
  // binds every interface keeps keyless local use); it is warned about at startup
176
183
  // because it also exposes the pool to the network. Shared pools set `clients`.
177
184
  const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '0.0.0.0']);
178
185
  const hostIsLoopback = (config.proxy?.host == null) || LOOPBACK_HOSTS.has(String(config.proxy.host).toLowerCase());
179
- const strictClients = clientByKey.size > 0 || !hostIsLoopback;
186
+ const strictClients = rawClients.length > 0 || !hostIsLoopback;
180
187
  const resolveCaller = (key, isLocal) => {
181
188
  if (key != null && clientByKey.has(key)) return { name: clientByKey.get(key), ok: true };
182
189
  // Legacy single key still works — including strict mode, so a solo user can
@@ -295,11 +302,22 @@ export function createProxyServer(accountManager, config, hooks = {}) {
295
302
  rejectUnauthorized();
296
303
  return;
297
304
  }
298
- const chunks = [];
299
- for await (const c of req) chunks.push(c);
305
+ // An oauth payload is ~2 KB; cap well above that so a key holder can't grow the heap.
306
+ const chunks = []; let bodyBytes = 0;
307
+ for await (const c of req) {
308
+ bodyBytes += c.length;
309
+ if (bodyBytes > 256 * 1024) {
310
+ res.writeHead(413, { 'Content-Type': 'application/json' });
311
+ res.end(JSON.stringify({ ok: false, error: 'too_large' }));
312
+ req.destroy();
313
+ return;
314
+ }
315
+ chunks.push(c);
316
+ }
300
317
  let body;
301
318
  try { body = JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { body = null; }
302
319
  const acct = body?.account;
320
+ if (acct && typeof acct.name === 'string') acct.name = safeText(acct.name);
303
321
  if (!acct || acct.type !== 'oauth' || typeof acct.accountUuid !== 'string' || !acct.accountUuid ||
304
322
  typeof acct.refreshToken !== 'string' || !acct.refreshToken || typeof acct.accessToken !== 'string' || !acct.accessToken) {
305
323
  res.writeHead(400, { 'Content-Type': 'application/json' });
@@ -611,7 +629,7 @@ async function forwardRequest(
611
629
  // anything and a 2-account fleet got exactly one repair, while the chain is now six
612
630
  // deep. Each repair latches its own flag, so this budget is a backstop, not the bound.
613
631
  const repairCount = requestInfo.repairCount || 0;
614
- const canRepairBody = repairCount < 6 && !res.headersSent;
632
+ const canRepairBody = repairCount < 6 && !clientHasRealBytes(res, requestInfo);
615
633
 
616
634
  // PRE-STRIP a session already known to carry provider-authored thinking. The client
617
635
  // resends the whole poisoned history every turn, so without this each turn pays another
@@ -873,20 +891,41 @@ async function forwardRequest(
873
891
  // Pinned cloud account refused (rate-limited / unavailable) before any byte
874
892
  // streamed: cool THIS mirror row and fail over through the normal path to the
875
893
  // 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) {
894
+ if (account.remote && (upRes.status === 429 || upRes.status === 503) && !clientHasRealBytes(res, requestInfo)) {
877
895
  try { await upRes.body?.cancel(); } catch { /* drained */ }
878
896
  const ra = Number(upRes.headers.get('retry-after'));
879
897
  // P3: "busy" is a 2s back-off on this row only; "unavailable"/429 cools longer.
880
898
  account.cooldownUntil = Date.now() + (Number.isFinite(ra) && ra > 0 ? Math.min(ra, 300) * 1000 : 30_000);
881
899
  finishLink({ status: upRes.status, error: 'remote_pinned_unavailable' });
882
900
  excludedIndexes.add(account.index);
883
- if (hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
901
+ if (retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
884
902
  return forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes);
885
903
  }
904
+ // Every route refused: that is capacity, so it waits here. The body was cancelled
905
+ // above, so falling through handed the client a 503 with no body.
906
+ const queued = await queueAndRetry(
907
+ `all routes failed after ${upRes.status} from "${account.name}"`,
908
+ req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
909
+ retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, 'capacity',
910
+ );
911
+ if (queued) return;
912
+ ctx.status = upRes.status;
913
+ sendErrorResponse(res, requestInfo, upRes.status, { type: 'error', error: { type: 'overloaded_error', message: `Team pool account "${account.name}" is at capacity` } });
914
+ return;
915
+ }
916
+ // A request released from the queue already holds a committed keep-alive stream.
917
+ if (requestInfo.queueHeartbeatActive) {
918
+ clearQueueHeartbeat(requestInfo);
919
+ if (upRes.status >= 400) {
920
+ const errorText = await readErrorBody(upRes);
921
+ sendErrorBody(res, requestInfo, upRes.status, errorText, upRes.headers);
922
+ finishLink({ success: upRes.status < 500, status: upRes.status });
923
+ return;
924
+ }
886
925
  }
887
926
  const outHeaders = {};
888
927
  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);
928
+ if (!res.headersSent) res.writeHead(upRes.status, outHeaders);
890
929
  if (upRes.body) {
891
930
  const reader = upRes.body.getReader();
892
931
  try {
@@ -910,10 +949,17 @@ async function forwardRequest(
910
949
  } catch (err) {
911
950
  finishLink({ status: 502, error: 'pool_link_unreachable' });
912
951
  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;
952
+ if (!clientHasRealBytes(res, requestInfo)) {
953
+ if (retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)) {
954
+ return forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes);
955
+ }
956
+ const queued = await queueAndRetry(
957
+ `all routes failed after a link error on "${account.name}"`,
958
+ req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
959
+ retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, 'network',
960
+ );
961
+ if (queued) return;
962
+ }
917
963
  ctx.status = 502;
918
964
  sendErrorResponse(res, requestInfo, 502, { type: 'error', error: { type: 'api_error', message: `Team pool "${account.name}" unreachable: ${err?.message || err}` } });
919
965
  }
@@ -1315,7 +1361,7 @@ async function forwardRequest(
1315
1361
  logSections.push(`=== RESPONSE ${upstreamRes.status} — "${account.name}" cooling down, failing over ===\n${formatHeaders(upstreamRes.headers)}`);
1316
1362
  }
1317
1363
 
1318
- if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1364
+ if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1319
1365
  return forwardRequest(
1320
1366
  req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir,
1321
1367
  retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes,
@@ -1466,7 +1512,7 @@ async function forwardRequest(
1466
1512
  : null;
1467
1513
  const issuerIsClaude = issuer && issuer.type !== 'provider';
1468
1514
  if (lease.migratedFromName && issuerIsClaude && requestInfo.sessionKey
1469
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1515
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1470
1516
  accountManager.revertSessionBinding(requestInfo.sessionKey, lease.migratedFromName);
1471
1517
  excludedIndexes.add(account.index);
1472
1518
  console.log(`[Maxpool] Thinking-signature fail-safe: reverting session to issuer "${lease.migratedFromName}" and retrying`);
@@ -1481,7 +1527,7 @@ async function forwardRequest(
1481
1527
  // EFFORT REPAIR. A rejected output_config.effort is a hard failure of whatever the
1482
1528
  // client was doing (a web search, a tool call) — worth healing rather than surfacing.
1483
1529
  if (effortMode && !requestInfo.effortRepaired
1484
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1530
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1485
1531
  const fix = repairEffort(body, effortMode, errorBody);
1486
1532
  if (fix.body) {
1487
1533
  // Latch it so LATER turns apply the working level up front: the client resends the
@@ -1623,7 +1669,7 @@ async function forwardRequest(
1623
1669
  // it when a provider can actually serve it (profile=all with a GLM/Kimi token).
1624
1670
  const providerAvailable = accountManager.accounts?.some(a => a.type === 'provider' && a.enabled !== false);
1625
1671
  if (anthropicIncompat && requestInfo.sessionKey && providerAvailable
1626
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1672
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1627
1673
  accountManager.markSessionIncompatible?.(requestInfo.sessionKey, requestInfo.homeProvider);
1628
1674
  excludedIndexes.add(account.index);
1629
1675
  console.log(`[Maxpool] Anthropic rejected this transcript (server_tool_use/thinking); pinning session to GLM/Kimi and retrying`);
@@ -1644,7 +1690,7 @@ async function forwardRequest(
1644
1690
  // Claude account exists to serve it; with none, the 400 surfaces (nothing bigger).
1645
1691
  const claudeAvailable = accountManager.accounts?.some(a => a.type !== 'provider' && a.enabled !== false);
1646
1692
  if (providerTooSmall && requestInfo.sessionKey && claudeAvailable
1647
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1693
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1648
1694
  accountManager.markSessionLargeContext?.(requestInfo.sessionKey);
1649
1695
  // Bench EVERY provider: today's GLM + Kimi coding legs both cap at ~256K, so once
1650
1696
  // one 400s on size the others can't hold it either. If a genuine 1M-context
@@ -1666,7 +1712,7 @@ async function forwardRequest(
1666
1712
  // latch the session — the fault is one request's shape, not a durable property of
1667
1713
  // the conversation, and latching would evict a session from GLM on a single blip.
1668
1714
  if (providerRejectedShape && claudeAvailable
1669
- && canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
1715
+ && canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1670
1716
  for (const a of (accountManager.accounts || [])) {
1671
1717
  if (a.type === 'provider') excludedIndexes.add(a.index);
1672
1718
  }
@@ -1684,7 +1730,7 @@ async function forwardRequest(
1684
1730
  // Trigger on the DETERMINISTIC signature rejection only — never the fuzzy
1685
1731
  // isAnthropicIncompatBody heuristic, so an unrelated malformed request keeps its
1686
1732
  // own upstream error instead of being mislabelled a provider-contamination.
1687
- if (isSignatureRejection && !res.headersSent) {
1733
+ if (isSignatureRejection && !clientHasRealBytes(res, requestInfo)) {
1688
1734
  const provs = (accountManager.accounts || []).filter(a => a.type === 'provider');
1689
1735
  // Say only what ACTUALLY happened. `thinkingStripped` is set only when the strip
1690
1736
  // ran; without it we found nothing provider-shaped, so claiming we stripped —
@@ -1964,7 +2010,7 @@ async function forwardRequest(
1964
2010
  }
1965
2011
 
1966
2012
  accountManager.releaseAccount(lease, { error: err.message });
1967
- if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !res.headersSent) {
2013
+ if (canRetryBufferedBody && retryCount + 1 < maxAttempts && !clientHasRealBytes(res, requestInfo)) {
1968
2014
  account.status = 'error';
1969
2015
  excludedIndexes.add(account.index);
1970
2016
  return forwardRequest(
@@ -2874,6 +2920,13 @@ function sendErrorBody(res, requestInfo, status, body, headers) {
2874
2920
  res.end(body);
2875
2921
  }
2876
2922
 
2923
+ /** True once the client received REAL response bytes. The queue/grace keep-alive commits
2924
+ * headers plus pings, so res.headersSent is true while nothing is delivered yet: repairs
2925
+ * and failover stay safe until streamResponse stops the keep-alive. */
2926
+ function clientHasRealBytes(res, requestInfo) {
2927
+ return res.headersSent && !requestInfo.queueHeartbeatActive;
2928
+ }
2929
+
2877
2930
  async function queueAndRetry(
2878
2931
  reason, req, res, body, accountManager, upstream, hooks, reqId, ctx, logDir,
2879
2932
  retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody = canRetryBufferedBody, cause = 'quota',
package/src/tui.js CHANGED
@@ -52,7 +52,6 @@ function whereCell(a) {
52
52
  function providerLabel(a) {
53
53
  if (a.type === 'pool') return 'Team pool';
54
54
 
55
- if (a._cloud) return (PROVIDER_LABELS[a.provider] || a.provider || 'Cloud').slice(0, PROVIDER_W - 1) + '☁';
56
55
  const key = a.provider || (a.type === 'provider' ? 'provider' : 'anthropic');
57
56
  const label = PROVIDER_LABELS[key] || (key.charAt(0).toUpperCase() + key.slice(1));
58
57
  return label.slice(0, PROVIDER_W);
@@ -1507,7 +1506,6 @@ export class TUI {
1507
1506
  async _sendLoginToRemote(mirror, creds, profile) {
1508
1507
  const remote = mirror.remote;
1509
1508
  const name = remote.remoteName || mirror.name;
1510
- const replace = Boolean(mirror.refreshDead || mirror.status === 'error');
1511
1509
  process.stdout.write(`\n"${name}" lives in the team pool (${remote.linkName}) — sending the fresh login there…\n`);
1512
1510
  const ctl = new AbortController();
1513
1511
  const t = setTimeout(() => ctl.abort(), 20_000);
@@ -1524,14 +1522,11 @@ export class TUI {
1524
1522
  expiresAt: creds.expiresAt,
1525
1523
  source: 'remote-login',
1526
1524
  },
1527
- replace,
1528
1525
  }),
1529
1526
  signal: ctl.signal,
1530
1527
  });
1531
1528
  const json = await res.json().catch(() => ({}));
1532
- if (res.status === 409) {
1533
- process.stdout.write(`\nTeam pool says "${name}" is already healthy — nothing to do. Nothing was saved on this laptop.\n`);
1534
- } else if (res.ok && json.ok) {
1529
+ if (res.ok && json.ok) {
1535
1530
  process.stdout.write(`\nDone: "${name}" ${json.result === 'added' ? 'added to' : 're-authenticated in'} the team pool. Nothing was saved on this laptop.\n`);
1536
1531
  this._addLog(`Remote login: "${name}" → team pool ${remote.linkName} (${json.result})`);
1537
1532
  } else {
@@ -2211,6 +2206,17 @@ export class TUI {
2211
2206
  // A row benched by the duplicate guard renders the CLOUD's live copy (status,
2212
2207
  // bars, in-flight, request counts) — never the stale local snapshot. Identity
2213
2208
  // and name stay local so the row doesn't move.
2209
+ const live = a?.poolBenchedBy ? this.am.remoteRowFor?.(a) : null;
2210
+ if (live) {
2211
+ const r = live.row;
2212
+ a = Object.assign(Object.create(Object.getPrototypeOf(a)), a, {
2213
+ quota: { ...a.quota, ...(r.quota || {}) },
2214
+ inFlight: r.inFlight ?? 0,
2215
+ completedRequests: r.completedRequests ?? a.completedRequests,
2216
+ refreshDead: Boolean(r.refreshDead),
2217
+ rateLimitedUntil: r.rateLimitedUntil ? Date.parse(r.rateLimitedUntil) : null,
2218
+ });
2219
+ }
2214
2220
 
2215
2221
  // Highlight the ACTIVE account(s) — drives BOTH the ► marker and the green
2216
2222
  // "active" status color. In manual mode that's the single pinned account (all
@@ -2250,8 +2256,12 @@ export class TUI {
2250
2256
  // the one running the recovery probe as "probing" and the rest "waiting"
2251
2257
  // (the pool-wide throttle banner above conveys the cause).
2252
2258
  const upstreamBlocking = a.type !== 'provider' && this.am._isUpstreamThrottleBlocking?.();
2253
- let effectiveStatus = a._cloud
2254
- ? (a._cloudOk ? a.status : 'cloud-down')
2259
+ // A mirror row whose link is down/stale is benched by syncRemoteMirrors with
2260
+ // enabled=false — show it as cloud-unreachable, never as "disabled" (which reads
2261
+ // as "I switched this off"). _cloud/_cloudOk were never set; key on a.remote.
2262
+ const linkDown = a.remote && (a.remote.link?.lastError || a.remote.link?.lastOkAt == null);
2263
+ let effectiveStatus = a.remote && (linkDown || a.enabled === false)
2264
+ ? (linkDown ? 'cloud-down' : 'disabled')
2255
2265
  : (a.poolBenchedBy ? 'pool-benched' : (a.enabled === false ? 'disabled' : a.status));
2256
2266
  if (a.enabled !== false && upstreamBlocking && a.status === 'active') {
2257
2267
  effectiveStatus = a.inFlight > 0 ? 'probing' : 'waiting';
@@ -2652,7 +2662,7 @@ export class TUI {
2652
2662
  .map(c => lpadVis(String(c), CW)).join('')));
2653
2663
 
2654
2664
  const link = (this.am.poolLinks || []).find(l => l.lastOkAt);
2655
- out.push(dim(` data since boot${link ? ` · team pool synced ${new Date(link.lastOkAt).toLocaleTimeString()}` : ''}`));
2665
+ out.push(dim(` rolling windows · restarts included${link ? ` · team pool synced ${new Date(link.lastOkAt).toLocaleTimeString()}` : ''}`));
2656
2666
  return out;
2657
2667
  }
2658
2668