maxpool 1.24.37 → 1.24.39

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.37",
3
+ "version": "1.24.39",
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",
@@ -1,4 +1,6 @@
1
1
  import { refreshAccessToken, isTokenExpiringSoon, modelFamily, tokenFingerprint } from './oauth.js';
2
+ import { computeDuplicates, credentialFp } from './pool-link.js';
3
+ import { appendEventLog } from './event-log.js';
2
4
  import { hourFloor } from './outside-usage.js';
3
5
  import { CapacityLedger, TANK_MIN_UTIL } from './capacity-ledger.js';
4
6
 
@@ -468,17 +470,21 @@ export class AccountManager {
468
470
  // Named-caller request attribution (team pools): callerName → requests served.
469
471
  // Server-side (not TUI-side) so headless pools feed the web screen too.
470
472
  this.callerCounts = new Map();
473
+ // Team-pool links (owner D8): remote pools as accounts + duplicate benching.
474
+ this.poolLinks = [];
475
+ this._remoteIdentities = new Map();
476
+ this._benchedByPool = [];
471
477
  this.poolUpSince = Date.now();
472
478
  this.accounts = accounts.map((acct, index) => ({
473
479
  index,
474
480
  name: acct.name,
475
481
  type: acct.type,
476
- provider: acct.provider || (acct.type === 'provider' ? 'provider' : 'anthropic'),
482
+ provider: acct.provider || (acct.type === 'provider' ? 'provider' : acct.type === 'pool' ? 'pool' : 'anthropic'),
477
483
  accountUuid: acct.accountUuid || null,
478
484
  credential: acct.accessToken || acct.authToken || acct.apiKey,
479
485
  upstream: acct.upstream || null,
480
486
  authHeader: acct.authHeader || null,
481
- profiles: acct.profiles || (acct.type === 'provider' ? ['all'] : ['claude', 'all']),
487
+ profiles: acct.profiles || (acct.type === 'provider' || acct.type === 'pool' ? ['all'] : ['claude', 'all']),
482
488
  priority: Number.isFinite(acct.priority) ? acct.priority : 0,
483
489
  // USAGE CAP (owner-directed 2026-08-26): reserve capacity on this account —
484
490
  // the proxy benches it at capUtilization of BOTH the 5h and 7d windows, keeping
@@ -966,6 +972,14 @@ export class AccountManager {
966
972
  // Subscription latched gone (org-disabled 403): benched until re-subscribed. The
967
973
  // clear path is a successful re-auth (updateAccountTokens) after re-purchasing.
968
974
  if (account.subscriptionGone) return false;
975
+ // Pool link: available only while its last poll succeeded AND the remote has a
976
+ // usable account — a down/empty team pool must not swallow requests that local
977
+ // accounts could serve.
978
+ if (account.type === 'pool') {
979
+ const link = account._link;
980
+ if (!link || link.lastError || link.lastOkAt == null) return false;
981
+ if (!link.summary || link.summary.usable < 1) return false;
982
+ }
969
983
  const now = options.now ?? Date.now();
970
984
 
971
985
  // Check rate limit expiry
@@ -1176,7 +1190,7 @@ export class AccountManager {
1176
1190
  _hasAvailableProvider(requestInfo = {}, excludedIndexes = new Set()) {
1177
1191
  const profile = requestInfo.profile || 'claude';
1178
1192
  return this.accounts.some(account => {
1179
- if (account.type !== 'provider' || excludedIndexes.has(account.index)) return false;
1193
+ if ((account.type !== 'provider' && account.type !== 'pool') || excludedIndexes.has(account.index)) return false;
1180
1194
  if (!this._matchesRequest(account, profile, requestInfo)) return false;
1181
1195
  return this._isAvailable(account, { allowWeeklyReserve: true, allowWeeklyCritical: true });
1182
1196
  });
@@ -2349,14 +2363,18 @@ export class AccountManager {
2349
2363
 
2350
2364
  _matchesRequest(account, profile, requestInfo = {}) {
2351
2365
  if (this.admissionPaused) return false;
2366
+ // Duplicate guard: a local account whose identity also lives in a team pool
2367
+ // never serves — the cloud copy is the single writer.
2368
+ if (account.poolBenchedBy) return false;
2352
2369
  if (!this._isRequestCompatible(account, profile, requestInfo)) return false;
2370
+ const providerLike = account.type === 'provider' || account.type === 'pool';
2353
2371
  if (
2354
- account.type !== 'provider'
2372
+ !providerLike
2355
2373
  && this.queueState.waiting.length
2356
2374
  && !requestInfo.queueTicket
2357
2375
  && !requestInfo.queueAdmitted
2358
2376
  ) return false;
2359
- if (account.type !== 'provider' && this._isUpstreamThrottleBlocking()) return false;
2377
+ if (!providerLike && this._isUpstreamThrottleBlocking()) return false;
2360
2378
  return true;
2361
2379
  }
2362
2380
 
@@ -2391,7 +2409,7 @@ export class AccountManager {
2391
2409
  const incompatible = this._effectiveIncompatible(requestInfo).incompatible;
2392
2410
  // An incompatible session is pinned to its provider family — no mode overrides that.
2393
2411
  if (incompatible) {
2394
- const isHome = account.type === 'provider'
2412
+ const isHome = (account.type === 'provider' || account.type === 'pool')
2395
2413
  && (mode === 'prefer-zai' ? account.provider === 'zai'
2396
2414
  : mode === 'prefer-kimi' ? account.provider === 'kimi' : true);
2397
2415
  return isHome ? 0 : base;
@@ -2609,6 +2627,11 @@ export class AccountManager {
2609
2627
 
2610
2628
  _isRequestCompatible(account, profile, requestInfo = {}) {
2611
2629
  if (!this._matchesProfile(account, profile)) return false;
2630
+ // A pool link carries Anthropic-API traffic only (/v1/…). Browser noise
2631
+ // (favicon, stray GETs) must never be forwarded — on the remote it finds no
2632
+ // route, queues up to the hold window, and pins a link lease the whole time
2633
+ // (live-rehearsal 2026-10-29: 4 favicon GETs showed as "4 running").
2634
+ if (account.type === 'pool' && !String(requestInfo.path || '').startsWith('/v1/')) return false;
2612
2635
 
2613
2636
  // Kimi (Moonshot) 400s on images Anthropic/GLM accept ("failed to decode
2614
2637
  // image") — and a provider 400 is TERMINAL (not retried to another account), so
@@ -2638,7 +2661,7 @@ export class AccountManager {
2638
2661
  // providerCrossFallback is explicitly off; by default GLM↔Kimi crossing stays
2639
2662
  // allowed even under 'never' (that reliable direction is governed separately
2640
2663
  // from the Claude→provider policy).
2641
- if (account.type !== 'provider') return false;
2664
+ if (account.type !== 'provider' && account.type !== 'pool') return false;
2642
2665
  if (policy === 'never' && homeProvider && account.provider !== homeProvider
2643
2666
  && this.scheduler.providerCrossFallback === false) return false;
2644
2667
  return true;
@@ -4130,6 +4153,10 @@ export class AccountManager {
4130
4153
  // read-only probing, and re-enabling refreshes on the next real request.
4131
4154
  // `force` still wins: an explicit re-auth/enable path may need the rotation.
4132
4155
  if (!force && account.enabled === false) return true;
4156
+ // Duplicate guard: a benched local copy must NEVER spend its single-use
4157
+ // refresh token — the cloud pool owns rotation now; a local rotation would
4158
+ // invalidate the cloud's token (invalid_grant → bricked account).
4159
+ if (account.poolBenchedBy) return true;
4133
4160
 
4134
4161
  if (!force && !isTokenExpiringSoon(account.expiresAt)) return true;
4135
4162
 
@@ -4576,6 +4603,64 @@ export class AccountManager {
4576
4603
  /**
4577
4604
  * Return a status summary of all accounts (safe to expose, no credentials).
4578
4605
  */
4606
+ /** Register pool links (from config.pools). Each link polls its remote pool's
4607
+ * status; duplicate benching is re-computed on every poll tick. */
4608
+ setPoolLinks(links, onPoll) {
4609
+ for (const l of this.poolLinks) l.stop?.();
4610
+ this.poolLinks = Array.isArray(links) ? links : [];
4611
+ this._remoteIdentities = new Map();
4612
+ const recompute = () => this.applyPoolLinks();
4613
+ for (const l of this.poolLinks) l.start(() => { recompute(); onPoll?.(l); });
4614
+ this.applyPoolLinks();
4615
+ }
4616
+
4617
+ /** Apply duplicate benching + link health from the links' last polls. Idempotent. */
4618
+ applyPoolLinks() {
4619
+ const ids = new Map();
4620
+ for (const l of this.poolLinks) {
4621
+ if (l.identities) for (const [k, v] of l.identities) ids.set(k, v);
4622
+ }
4623
+ const dups = computeDuplicates(this.accounts, ids);
4624
+ const benchedIdx = new Set(dups.map(d => d.index));
4625
+ for (const a of this.accounts) {
4626
+ const wasBenched = Boolean(a.poolBenchedBy);
4627
+ const d = dups.find(x => x.index === a.index);
4628
+ a.poolBenchedBy = d ? (d.remoteName || 'team pool') : null;
4629
+ if (d && !wasBenched) {
4630
+ console.log(`[Maxpool] Duplicate guard: local account "${a.name}" also lives in team pool (${a.poolBenchedBy}) — benched locally, no routing, no token refresh`);
4631
+ appendEventLog(`[Maxpool] Duplicate guard benched "${a.name}" (also in team pool as ${a.poolBenchedBy})`);
4632
+ } else if (!d && wasBenched) {
4633
+ console.log(`[Maxpool] Duplicate guard released "${a.name}" (no longer in team pool)`);
4634
+ appendEventLog(`[Maxpool] Duplicate guard released "${a.name}"`);
4635
+ }
4636
+ }
4637
+ this._benchedByPool = [...benchedIdx];
4638
+ this._remoteIdentities = ids;
4639
+ }
4640
+
4641
+ /** Add (or update) the routable account row for a pool link. */
4642
+ upsertPoolAccount(link) {
4643
+ let a = this.accounts.find(x => x.type === 'pool' && x.name === link.name);
4644
+ if (!a) {
4645
+ this.upsertRuntimeAccount({
4646
+ name: link.name, type: 'pool', provider: 'pool',
4647
+ authToken: link.key, upstream: String(link.url).replace(/\/+$/, ''),
4648
+ profiles: ['claude', 'all'], priority: 0, configSourced: true,
4649
+ });
4650
+ a = this.accounts.find(x => x.type === 'pool' && x.name === link.name);
4651
+ }
4652
+ a.credential = link.key;
4653
+ a.upstream = String(link.url).replace(/\/+$/, '');
4654
+ a._link = link;
4655
+ a.configSourced = true;
4656
+ return a;
4657
+ }
4658
+
4659
+ /** True when account is benched by the duplicate guard. */
4660
+ isPoolBenched(account) {
4661
+ return Boolean(account && account.poolBenchedBy);
4662
+ }
4663
+
4579
4664
  getStatus() {
4580
4665
  const now = Date.now();
4581
4666
  return {
@@ -4600,6 +4685,8 @@ export class AccountManager {
4600
4685
  type: a.type,
4601
4686
  provider: a.provider,
4602
4687
  enabled: a.enabled,
4688
+ poolBenchedBy: a.poolBenchedBy || null,
4689
+ keyFingerprint: (a.type === 'provider' && a.credential) ? credentialFp(a.credential) : null,
4603
4690
  upstream: a.upstream,
4604
4691
  profiles: a.profiles,
4605
4692
  priority: a.priority,
@@ -4688,6 +4775,23 @@ export class AccountManager {
4688
4775
  : 0,
4689
4776
  },
4690
4777
  callers: Object.fromEntries([...this.callerCounts.entries()].map(([n, c]) => [n, { requests: c }])),
4778
+ pools: this.poolLinks.map(l => ({
4779
+ name: l.name,
4780
+ url: l.url,
4781
+ ok: l.lastOkAt != null && !l.lastError,
4782
+ lastError: l.lastError,
4783
+ lastOkAt: l.lastOkAt ? new Date(l.lastOkAt).toISOString() : null,
4784
+ summary: l.summary,
4785
+ accounts: (l.status?.accounts || []).map(ra => ({
4786
+ name: ra.name,
4787
+ type: ra.type,
4788
+ provider: ra.provider,
4789
+ enabled: ra.enabled,
4790
+ refreshDead: Boolean(ra.refreshDead),
4791
+ quota: ra.quota || null,
4792
+ status: ra.status || null,
4793
+ })),
4794
+ })),
4691
4795
  sessions: {
4692
4796
  stickyBindings: this.sessionBindings.size,
4693
4797
  thinkingProtected: [...this.sessionPolicies.values()].filter(p => p.requiresAnthropicThinkingIntegrity).length,
package/src/index.js CHANGED
@@ -679,6 +679,34 @@ async function serverWorkerCommand() {
679
679
  }
680
680
  }
681
681
 
682
+ // ── team-pool links (owner D8): each remote pool becomes ONE routable account ──
683
+ // config.pools: [{ name, url, keySecret? | key? }]. The person's pool key is
684
+ // resolved from GCP Secret Manager when keySecret is set (never written to disk),
685
+ // or taken literally from `key`. The link polls the remote status; duplicates of
686
+ // local accounts are auto-benched (single writer per login/key).
687
+ if (Array.isArray(config.pools) && config.pools.length > 0) {
688
+ try {
689
+ const { PoolLink } = await import('./pool-link.js');
690
+ const defs = config.pools.filter(p => p && p.name && p.url);
691
+ const secretNames = defs.map(p => p.keySecret).filter(Boolean);
692
+ let resolved = {};
693
+ if (secretNames.length) {
694
+ const { resolveSecrets } = await import('./secret-resolver.js');
695
+ resolved = await resolveSecrets(secretNames);
696
+ }
697
+ const links = defs.map(p => new PoolLink({
698
+ ...p,
699
+ key: p.keySecret ? (resolved[p.keySecret] || null) : (p.key || null),
700
+ }));
701
+ for (const l of links) accountManager.upsertPoolAccount(l);
702
+ accountManager.setPoolLinks(links);
703
+ const armed = links.filter(l => l.key).length;
704
+ console.log(`[Maxpool] Team pools: ${armed} linked${links.length - armed ? `, ${links.length - armed} unresolved key` : ''}`);
705
+ } catch (err) {
706
+ console.error(`[Maxpool] Team pool link setup failed: ${err.message}`);
707
+ }
708
+ }
709
+
682
710
  // Track the state-file generation we last observed so a stale flush is refused.
683
711
  let stateGeneration = Number(savedState?._generation) || 0;
684
712
 
@@ -1732,20 +1760,22 @@ async function poolCommand() {
1732
1760
  console.error(' e.g. maxpool pool shared maxpool.tail-scale.ts.net:3456 mp-personal-key');
1733
1761
  process.exit(1);
1734
1762
  }
1735
- config.client.mode = 'shared';
1736
- config.client.sharedHost = host;
1737
- config.client.sharedKey = key;
1738
- await saveConfig(config);
1763
+ // atomicConfigUpdate, not load→save: a running local pool may rotate an OAuth
1764
+ // token in the same instant, and a whole-file save would undo that refresh.
1765
+ await atomicConfigUpdate(c => {
1766
+ c.client = { ...(c.client || {}), mode: 'shared', sharedHost: host, sharedKey: key };
1767
+ });
1739
1768
  console.log(`This machine now points at the SHARED pool: http://${host}`);
1740
1769
  console.log('Apply it in your shells (or restart terminals):');
1741
1770
  console.log(` export ANTHROPIC_BASE_URL=http://${host}`);
1742
1771
  console.log(` export ANTHROPIC_API_KEY=${key}`);
1743
1772
  console.log('rc-gate users: also set RC_GATE_MAXPOOL_HOST/PORT/KEY in the LaunchAgent and restart it.');
1744
1773
  } else if (sub === 'local') {
1745
- config.client.mode = 'local';
1746
- delete config.client.sharedHost;
1747
- delete config.client.sharedKey;
1748
- await saveConfig(config);
1774
+ await atomicConfigUpdate(c => {
1775
+ c.client = { ...(c.client || {}), mode: 'local' };
1776
+ delete c.client.sharedHost;
1777
+ delete c.client.sharedKey;
1778
+ });
1749
1779
  console.log(`This machine points at its OWN local pool: http://${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`);
1750
1780
  console.log('Unset ANTHROPIC_BASE_URL/ANTHROPIC_API_KEY (or re-export the local ones) and restart terminals.');
1751
1781
  } else if (sub === 'status') {
@@ -1774,15 +1804,19 @@ async function runCommand() {
1774
1804
  // which the proxy accepts from localhost. Not setting ANTHROPIC_API_KEY
1775
1805
  // lets Claude Code stay in subscription mode (full model access).
1776
1806
  // Use spawnSync so the Node process blocks entirely — behaves like execvp.
1807
+ const shared = config.client?.mode === 'shared' && !!config.client?.sharedHost;
1777
1808
  const result = spawnSync('claude', claudeArgs, {
1778
1809
  stdio: 'inherit',
1779
1810
  env: {
1780
1811
  ...process.env,
1781
1812
  // Follows the `pool shared` switch like envCommand (client.mode 'shared'
1782
1813
  // redirects run-subcommand traffic at the team pool).
1783
- ANTHROPIC_BASE_URL: config.client?.mode === 'shared' && config.client?.sharedHost
1814
+ ANTHROPIC_BASE_URL: shared
1784
1815
  ? `http://${config.client.sharedHost}`
1785
1816
  : `http://${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`,
1817
+ // A shared pool trusts no address, so it needs the person's key; the local
1818
+ // pool keeps today's keyless subscription-mode behavior.
1819
+ ...(shared && config.client.sharedKey ? { ANTHROPIC_API_KEY: config.client.sharedKey } : {}),
1786
1820
  },
1787
1821
  });
1788
1822
 
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Team-pool link — one remote maxpool appearing as a single routable account
3
+ * inside a local pool (owner decision D8, 2026-10-29).
4
+ *
5
+ * The remote pool is configured as `pools: [{ name, url, keySecret? | key? }]`
6
+ * in the local config. This module OWNS three things:
7
+ *
8
+ * 1. Remote status polling (fetchRemoteStatus): GET <url>/maxpool/status with
9
+ * the person's key. Refreshed on a slow interval (default 30s). The result
10
+ * drives BOTH the routing score (an unavailable remote pool must not eat
11
+ * requests) and the TUI/screen rows (every remote account, labelled).
12
+ * 2. Duplicate detection (computeDuplicates): identity of a remote account is
13
+ * (uuid) for Claude OAuth, (provider + credential fingerprint) for GLM/Kimi
14
+ * keys. A local account with the same identity is AUTO-BENCHED — never
15
+ * routed, never token-refreshed — and labelled "lives in team pool".
16
+ * Benching is computed here but APPLIED by AccountManager.applyPoolLinks so
17
+ * the same logic drives eligibility + ensureTokenFresh + TUI.
18
+ * 3. Hop marking (hopHeader): every forwarded request carries x-maxpool-hops
19
+ * so a downstream pool can refuse a loop (A→B→A) instead of hanging.
20
+ */
21
+ import { createHash } from 'node:crypto';
22
+
23
+ export const HOP_HEADER = 'x-maxpool-hops';
24
+ export const MAX_HOPS = 2;
25
+
26
+ /** Fingerprint a credential for duplicate matching WITHOUT exposing it.
27
+ * Same shape as the TUI's token fingerprints (first chars of a sha). */
28
+ export function credentialFp(credential) {
29
+ if (!credential) return null;
30
+ return createHash('sha256').update(String(credential)).digest('hex').slice(0, 12);
31
+ }
32
+
33
+ /** Stable identity of an account for duplicate matching. Claude OAuth →
34
+ * accountUuid; provider key → provider + key fingerprint. null = no identity
35
+ * (an unnamed apikey account can't be matched — it never benches anything). */
36
+ export function accountIdentity(account) {
37
+ if (!account) return null;
38
+ if (account.type === 'oauth') {
39
+ return account.accountUuid ? `uuid:${account.accountUuid}` : null;
40
+ }
41
+ if (account.type === 'provider' && account.provider && account.credential) {
42
+ return `key:${account.provider}:${credentialFp(account.credential)}`;
43
+ }
44
+ return null;
45
+ }
46
+
47
+ /** Identity map from a REMOTE pool's status JSON (key-gated /maxpool/status):
48
+ * { accounts: [{name,type,provider,accountUuid,enabled,...}], callers, ... }.
49
+ * Provider rows don't carry credentials — the remote pool exports a
50
+ * keyFingerprint per provider account so links can match without secrets. */
51
+ export function remoteIdentities(status) {
52
+ const out = new Map();
53
+ if (!status || !Array.isArray(status.accounts)) return out;
54
+ for (const a of status.accounts) {
55
+ if (a.enabled === false) continue; // a benched remote row doesn't bench the local copy
56
+ let id = null;
57
+ if (a.type === 'oauth' && a.accountUuid) id = `uuid:${a.accountUuid}`;
58
+ else if (a.type === 'provider' && a.keyFingerprint) id = `key:${a.provider}:${a.keyFingerprint}`;
59
+ if (id) out.set(id, { name: a.name, provider: a.provider, type: a.type });
60
+ }
61
+ return out;
62
+ }
63
+
64
+ /** Given local accounts and remote identities, return the local indexes that
65
+ * must be benched (duplicates) — { index, remoteName }. Pure: no mutation. */
66
+ export function computeDuplicates(accounts, remoteIdentitiesMap) {
67
+ const out = [];
68
+ if (!remoteIdentitiesMap || remoteIdentitiesMap.size === 0) return out;
69
+ for (const a of accounts) {
70
+ const id = accountIdentity(a);
71
+ const hit = id ? remoteIdentitiesMap.get(id) : null;
72
+ if (hit) out.push({ index: a.index, identity: id, remoteName: hit.name, provider: hit.provider });
73
+ }
74
+ return out;
75
+ }
76
+
77
+ /** Parse/step the hop header. Returns the new value for the OUTGOING request,
78
+ * or null when forwarding must be refused (loop). */
79
+ export function nextHopCount(headerValue) {
80
+ const n = Number.parseInt(String(headerValue ?? ''), 10);
81
+ const hops = Number.isFinite(n) && n >= 0 ? n : 0;
82
+ return hops + 1 <= MAX_HOPS ? hops + 1 : null;
83
+ }
84
+
85
+ /** Fetch a remote pool's status. Returns { ok, status, json, error, at }.
86
+ * Never throws; a network failure is a normal condition (remote down),
87
+ * surfaced via ok:false so the caller can bench the link account. */
88
+ export async function fetchRemoteStatus(url, key, { fetchFn = fetch, timeoutMs = 10_000, now = Date.now } = {}) {
89
+ const ctl = new AbortController();
90
+ const t = setTimeout(() => ctl.abort(), timeoutMs);
91
+ try {
92
+ const res = await fetchFn(`${String(url).replace(/\/+$/, '')}/maxpool/status`, {
93
+ headers: key ? { 'x-api-key': key } : {},
94
+ signal: ctl.signal,
95
+ cache: 'no-store',
96
+ });
97
+ if (res.status === 401 || res.status === 403) {
98
+ return { ok: false, status: res.status, json: null, error: 'key rejected', at: now() };
99
+ }
100
+ if (!res.ok) {
101
+ return { ok: false, status: res.status, json: null, error: `HTTP ${res.status}`, at: now() };
102
+ }
103
+ const json = await res.json();
104
+ return { ok: true, status: 200, json, error: null, at: now() };
105
+ } catch (err) {
106
+ return { ok: false, status: null, json: null, error: err?.name === 'AbortError' ? 'timeout' : (err?.message || 'network error'), at: now() };
107
+ } finally {
108
+ clearTimeout(t);
109
+ }
110
+ }
111
+
112
+ /** Aggregate a remote status into link health + capacity, for scoring.
113
+ * conservative: counts an account as spent when its weekly window is ≥95%,
114
+ * needs-login rows as unusable, and unknown quota as usable-but-unknown. */
115
+ export function summarizeRemote(status) {
116
+ const accts = Array.isArray(status?.accounts) ? status.accounts.filter(a => a.enabled !== false) : [];
117
+ const usable = accts.filter(a => {
118
+ if (a.refreshDead) return false;
119
+ const week = a.quota?.unified7d ?? a.quota?.providerWk ?? null;
120
+ if (week != null && week >= 0.95) return false;
121
+ return a.status !== 'error';
122
+ });
123
+ return {
124
+ total: accts.length,
125
+ usable: usable.length,
126
+ needsLogin: accts.filter(a => a.refreshDead).length,
127
+ callers: status?.callers || {},
128
+ };
129
+ }
130
+
131
+ /** Test seam holder for PoolLink's timer (so tests don't leak handles). */
132
+ export class PoolLink {
133
+ constructor(def, deps = {}) {
134
+ this.name = def.name;
135
+ this.url = def.url;
136
+ this.key = def.key || null; // resolved by caller (secret) or literal
137
+ this.keySecret = def.keySecret || null;
138
+ this.intervalMs = Math.max(5_000, Number(def.intervalMs) || 30_000);
139
+ this._fetchRemoteStatus = deps.fetchRemoteStatus || fetchRemoteStatus;
140
+ this._now = deps.now || Date.now;
141
+ this._timer = null;
142
+ this._polling = false;
143
+ // Last poll result. ok:false → link account benched + UI shows why.
144
+ this.status = null; // raw remote status JSON when ok
145
+ this.lastError = null;
146
+ this.lastOkAt = null;
147
+ this.identities = new Map();
148
+ this.summary = null;
149
+ }
150
+
151
+ async poll() {
152
+ if (this._polling) return; // never stack polls
153
+ this._polling = true;
154
+ try {
155
+ const r = await this._fetchRemoteStatus(this.url, this.key);
156
+ if (r.ok) {
157
+ this.status = r.json;
158
+ this.lastOkAt = r.at;
159
+ this.lastError = null;
160
+ this.identities = remoteIdentities(r.json);
161
+ this.summary = summarizeRemote(r.json);
162
+ } else {
163
+ // Keep the LAST good status for display, but mark the link degraded so
164
+ // routing benches it and the UI names the error.
165
+ this.lastError = r.error;
166
+ this.summary = null;
167
+ }
168
+ return r;
169
+ } finally {
170
+ this._polling = false;
171
+ }
172
+ }
173
+
174
+ start(onPoll) {
175
+ this.stop();
176
+ const tick = async () => {
177
+ await this.poll();
178
+ onPoll?.(this);
179
+ };
180
+ this._timer = setInterval(() => { tick().catch(() => {}); }, this.intervalMs);
181
+ this._timer.unref?.();
182
+ tick().catch(() => {}); // immediate first poll
183
+ }
184
+
185
+ stop() {
186
+ if (this._timer) { clearInterval(this._timer); this._timer = null; }
187
+ }
188
+ }
package/src/prober.js CHANGED
@@ -103,7 +103,8 @@ export class Prober {
103
103
  // Skip only auth-dead accounts (dead refresh token): probing them just re-POSTs
104
104
  // the rejected token every cycle — a 400 storm. They recover only on re-auth.
105
105
  const oauth = this.am.accounts.filter(a => a.type === 'oauth' && a.credential && !a.refreshDead);
106
- const providers = this.am.accounts.filter(a => a.type === 'provider' && a.credential);
106
+ // Pool links are probed by their own PoolLink status poll, not here.
107
+ const providers = this.am.accounts.filter(a => a.type === 'provider' && a.credential); // 'pool' rows are excluded by type
107
108
 
108
109
  // Providers read DISTINCT hosts (z.ai monitor / Kimi) — no collision — so
109
110
  // they run concurrently. Every OAuth account, though, reads the SAME
package/src/screen.html CHANGED
@@ -84,6 +84,29 @@ h1{font-size:22px;margin:0;letter-spacing:-.3px}
84
84
  return ['ok', 'Ready'];
85
85
  }
86
86
 
87
+ // Team-pool rows (owner D8/D11): each remote pool's accounts shown individually,
88
+ // tagged with the pool name so cloud vs local is visible at a glance.
89
+ function poolRows(s) {
90
+ var pools = s.pools || [];
91
+ var out = '';
92
+ for (var i = 0; i < pools.length; i++) {
93
+ var pl = pools[i];
94
+ var tag = '<span class="chip" style="background:var(--chip)">' + esc(pl.name) + (pl.ok ? '' : ' · unreachable') + '</span>';
95
+ var accts = (pl.accounts || []).map(function (ra) {
96
+ var q = ra.quota || {};
97
+ var five = q.unified5h != null ? q.unified5h : q.providerSes;
98
+ var week = q.unified7d != null ? q.unified7d : q.providerWk;
99
+ var st = ra.refreshDead ? ['bad', 'Needs login'] : ra.status === 'error' ? ['bad', 'Error'] : (week != null && week >= 0.999) ? ['warn', 'Used up this week'] : ra.status === 'throttled' ? ['warn', 'Resting'] : ['ok', 'Ready'];
100
+ return '<div class="acct">' +
101
+ '<div><div class="name">' + esc(ra.name) + '</div><div class="sub">' + esc(ra.provider || (ra.type === 'oauth' ? 'Claude' : ra.type)) + ' · cloud</div></div>' +
102
+ '<div class="bars">' + bar('5h', five) + bar('7d', week) + '</div>' +
103
+ '<span class="state ' + st[0] + '">' + st[1] + '</span></div>';
104
+ }).join('');
105
+ out += '<div style="margin:14px 0 4px" class="sub">' + tag + '</div><div class="grid">' + accts + '</div>';
106
+ }
107
+ return out;
108
+ }
109
+
87
110
  function render(s) {
88
111
  // Live needs-login accounts first; switched-off accounts last.
89
112
  function rank(a) { return a.enabled === false ? 2 : (a.refreshDead ? 0 : 1); }
@@ -98,23 +121,25 @@ h1{font-size:22px;margin:0;letter-spacing:-.3px}
98
121
  }).join('');
99
122
  var rows = accts.map(function (a) {
100
123
  var st = stateOf(a), q = a.quota || {};
124
+ if (a.poolBenchedBy) st = ['', 'In team pool'];
101
125
  // Field names from getStatus().accounts[].quota: Claude = unified5h/unified7d,
102
126
  // providers (z.ai/kimi) = providerSes/providerWk. All 0..1 fractions.
103
127
  var five = q.unified5h != null ? q.unified5h : q.providerSes;
104
128
  var week = q.unified7d != null ? q.unified7d : q.providerWk;
105
129
  return '<div class="acct' + (a.refreshDead ? ' dead' : '') + '">' +
106
- '<div><div class="name">' + esc(a.name) + '</div><div class="sub">' + esc(a.provider || (a.type === 'oauth' ? 'Claude' : a.type)) +
130
+ '<div><div class="name">' + esc(a.name) + '</div><div class="sub">' + esc(a.type === 'pool' ? 'Team pool' : (a.provider || (a.type === 'oauth' ? 'Claude' : a.type))) +
107
131
  (a.inFlight ? ' · ' + a.inFlight + ' running' : '') + '</div></div>' +
108
132
  '<div class="bars">' + bar('5h', five) + bar('7d', week) + '</div>' +
109
133
  '<span class="state ' + st[0] + '">' + st[1] + '</span></div>';
110
134
  }).join('');
111
135
  var ver = (s.runningVersion || (s.version && s.version.current) || '');
112
- app.innerHTML = '<header><h1>Maxpool accounts</h1><span class="meta">' + accts.length + ' accounts' +
136
+ 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' +
113
138
  (dead ? ' · <b style="color:var(--bad)">' + dead + (dead === 1 ? ' needs login' : ' need login') + '</b>' : '') +
114
139
  (ver ? ' · v' + esc(ver) : '') + ' · updated ' + new Date().toLocaleTimeString() + '</span>' +
115
140
  '<button class="link" id="out">Forget key</button></header>' +
116
141
  (people ? '<div class="people">' + people + '</div>' : '') +
117
- '<div class="grid">' + rows + '</div>';
142
+ '<div class="grid">' + rows + '</div>' + poolRows(s);
118
143
  document.getElementById('out').onclick = function () { setKey(''); gate(); };
119
144
  }
120
145
 
package/src/server.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import http from 'node:http';
2
2
  import { appendEventLog } from './event-log.js';
3
+ import { HOP_HEADER, nextHopCount } from './pool-link.js';
3
4
  import { ThreadOwners, readThreadIntent, threadRefusalBody, isThreadlessAccount } from './thread-gate.js';
4
5
  import { writeFile, mkdir } from 'node:fs/promises';
5
6
  import { readFileSync } from 'node:fs';
@@ -12,6 +13,7 @@ const HOP_BY_HOP_HEADERS = new Set([
12
13
  'te', 'trailer', 'upgrade', 'proxy-authorization', 'proxy-authenticate',
13
14
  ]);
14
15
  const MAXPOOL_HEADER_PREFIX = 'x-maxpool-';
16
+ const MAX_HOPS_SRV = 2;
15
17
 
16
18
  const DEFAULT_RETRY = {
17
19
  maxAttemptsPerRequest: 0,
@@ -168,6 +170,9 @@ export function createProxyServer(accountManager, config, hooks = {}) {
168
170
  ? config.proxy.clients.filter(c => c && typeof c.key === 'string' && c.key)
169
171
  : [];
170
172
  const clientByKey = new Map(clientList.map(c => [c.key, c.name]));
173
+ // '0.0.0.0' stays in the trusted set for backward compatibility (a solo user who
174
+ // binds every interface keeps keyless local use); it is warned about at startup
175
+ // because it also exposes the pool to the network. Shared pools set `clients`.
171
176
  const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '0.0.0.0']);
172
177
  const hostIsLoopback = (config.proxy?.host == null) || LOOPBACK_HOSTS.has(String(config.proxy.host).toLowerCase());
173
178
  const strictClients = clientByKey.size > 0 || !hostIsLoopback;
@@ -186,6 +191,8 @@ export function createProxyServer(accountManager, config, hooks = {}) {
186
191
  if (strictClients) {
187
192
  const who = clientByKey.size > 0 ? `${clientByKey.size} named client${clientByKey.size > 1 ? 's' : ''}` : 'non-loopback listen host';
188
193
  appendEventLog(`[Maxpool] Strict client auth ON (${who}; loopback bypass removed)`);
194
+ } else if (String(config.proxy?.host) === '0.0.0.0') {
195
+ appendEventLog('[Maxpool] WARNING: listening on every interface (0.0.0.0) with local callers trusted keyless — set proxy.clients to require a key from everyone');
189
196
  }
190
197
 
191
198
  if (logDir) {
@@ -223,6 +230,15 @@ export function createProxyServer(accountManager, config, hooks = {}) {
223
230
  if (draining) {
224
231
  try { res.setHeader('Connection', 'close'); } catch { /* headers may be sent */ }
225
232
  }
233
+ // Loop guard (owner D8): a request forwarded by another pool carries a hop
234
+ // count. Refuse beyond MAX_HOPS with a clear error instead of cycling pools.
235
+ const inboundHops = headerValue(req.headers, HOP_HEADER);
236
+ if (inboundHops != null && Number.parseInt(inboundHops, 10) >= MAX_HOPS_SRV) {
237
+ res.writeHead(508, { 'Content-Type': 'application/json' });
238
+ res.end(JSON.stringify({ type: 'error', error: { type: 'request_loop', message: 'Maxpool hop limit reached — two pools are forwarding to each other. Check each pool\'s `pools` config.' } }));
239
+ return;
240
+ }
241
+
226
242
  // Auth check — per-person clients when configured, legacy single key otherwise.
227
243
  // strictClients (clients list present, or a non-loopback listen host) removes
228
244
  // the loopback bypass entirely: every caller must present a known key.
@@ -269,8 +285,10 @@ export function createProxyServer(accountManager, config, hooks = {}) {
269
285
  rejectUnauthorized();
270
286
  return;
271
287
  }
272
- req.callerName = caller.name;
273
- accountManager.countRequest?.(caller.name);
288
+ // In strict mode the legacy single key is still accepted; label it so its
289
+ // traffic is attributed and logged rather than anonymous.
290
+ req.callerName = caller.name || (strictClients ? 'legacy-key' : null);
291
+ accountManager.countRequest?.(req.callerName);
274
292
 
275
293
  // Let client token refresh requests pass through to upstream untouched.
276
294
  // The proxy manages its own tokens via ensureTokenFresh(); intercepting
@@ -304,6 +322,7 @@ export function createProxyServer(accountManager, config, hooks = {}) {
304
322
  path: req.url,
305
323
  account: ctx.account,
306
324
  status: ctx.status,
325
+ caller: req.callerName || null,
307
326
  });
308
327
  }
309
328
  return;
@@ -706,6 +725,70 @@ async function forwardRequest(
706
725
  delete headers['authorization'];
707
726
  }
708
727
 
728
+ // POOL LINK (owner D8): this "account" is another maxpool. Forward the raw
729
+ // request to it with the person's key, a stepped hop header, and the client's
730
+ // session header preserved — the remote pool does its own account selection,
731
+ // so no body rewriting, no credential substitution.
732
+ if (account.type === 'pool') {
733
+ const hop = nextHopCount(headerValue(req.headers, HOP_HEADER));
734
+ if (hop == null) {
735
+ accountManager.releaseAccount(lease, { neutral: true });
736
+ ctx.status = 508;
737
+ sendErrorResponse(res, requestInfo, 508, { type: 'error', error: { type: 'request_loop', message: 'Maxpool hop limit reached — pools are forwarding in a circle.' } });
738
+ return;
739
+ }
740
+ const linkHeaders = {};
741
+ for (const [k, v] of Object.entries(req.headers)) {
742
+ const lk = k.toLowerCase();
743
+ if (HOP_BY_HOP_HEADERS.has(lk) || lk === 'content-length' || lk === 'host' || lk === 'connection') continue;
744
+ linkHeaders[k] = v;
745
+ }
746
+ linkHeaders[HOP_HEADER] = String(hop);
747
+ linkHeaders['x-api-key'] = account.credential; // person's pool key
748
+ delete linkHeaders.authorization;
749
+ const linkUrl = `${account.upstream}${req.url}`;
750
+ ctx.account = account.name;
751
+ // The client-gone listener from the pre-response window would release the
752
+ // lease a SECOND time when the stream closes normally — remove just the
753
+ // listener (calling releaseOnClientGone would release the lease itself).
754
+ res.off('close', onClientClose);
755
+ let linkDone = false;
756
+ const finishLink = (outcome) => {
757
+ if (linkDone) return; linkDone = true;
758
+ accountManager.releaseAccount(lease, outcome);
759
+ };
760
+ try {
761
+ const upRes = await fetch(linkUrl, { method: req.method, headers: linkHeaders, body: ['GET', 'HEAD'].includes(req.method) ? undefined : body, duplex: 'half' });
762
+ ctx.status = upRes.status;
763
+ const outHeaders = {};
764
+ upRes.headers.forEach((v, k) => { if (!HOP_BY_HOP_HEADERS.has(k) && k !== 'content-length' && k !== 'transfer-encoding') outHeaders[k] = v; });
765
+ res.writeHead(upRes.status, outHeaders);
766
+ if (upRes.body) {
767
+ const reader = upRes.body.getReader();
768
+ try {
769
+ for (;;) {
770
+ const { done, value } = await reader.read();
771
+ if (done) break;
772
+ if (res.destroyed) { try { await reader.cancel(); } catch { } break; }
773
+ res.write(Buffer.from(value));
774
+ }
775
+ } finally { try { await reader.cancel(); } catch { /* already done */ } }
776
+ }
777
+ res.end();
778
+ finishLink({ success: ctx.status < 500, status: ctx.status });
779
+ hooks.onRequestRouted?.(reqId, { account: account.name });
780
+ } catch (err) {
781
+ finishLink({ status: 502, error: 'pool_link_unreachable' });
782
+ const retried = retryCount + 1 < maxAttempts && hasEligibleRoute(accountManager, requestInfo, excludedIndexes)
783
+ ? await forwardRequest(req, res, body, accountManager, upstream, retryCount + 1, hooks, reqId, ctx, logDir, retryConfig, queueConfig, requestInfo, canRetryBufferedBody, canQueueBufferedBody, excludedIndexes)
784
+ : null;
785
+ if (retried) return;
786
+ ctx.status = 502;
787
+ sendErrorResponse(res, requestInfo, 502, { type: 'error', error: { type: 'api_error', message: `Team pool "${account.name}" unreachable: ${err?.message || err}` } });
788
+ }
789
+ return;
790
+ }
791
+
709
792
  const upstreamUrl = `${account.upstream || upstream}${req.url}`;
710
793
  const method = req.method;
711
794
  const upstreamBody = rewriteBodyForAccount(body, account);
package/src/tui.js CHANGED
@@ -44,6 +44,7 @@ const ROW_PREFIX = ' '; // ' ' + sel(1) + cur(1) + ' ' — 4 cols before the
44
44
  // the column width so a long name can never misalign the row.
45
45
  const PROVIDER_LABELS = { anthropic: 'Anthropic', zai: 'z.ai', kimi: 'Moonshot', openai: 'OpenAI', codex: 'Codex', grok: 'Grok', xai: 'Grok' };
46
46
  function providerLabel(a) {
47
+ if (a.type === 'pool') return 'Team pool';
47
48
  const key = a.provider || (a.type === 'provider' ? 'provider' : 'anthropic');
48
49
  const label = PROVIDER_LABELS[key] || (key.charAt(0).toUpperCase() + key.slice(1));
49
50
  return label.slice(0, PROVIDER_W);
@@ -2122,7 +2123,7 @@ export class TUI {
2122
2123
  // the one running the recovery probe as "probing" and the rest "waiting"
2123
2124
  // (the pool-wide throttle banner above conveys the cause).
2124
2125
  const upstreamBlocking = a.type !== 'provider' && this.am._isUpstreamThrottleBlocking?.();
2125
- let effectiveStatus = a.enabled === false ? 'disabled' : a.status;
2126
+ let effectiveStatus = a.poolBenchedBy ? 'pool-benched' : (a.enabled === false ? 'disabled' : a.status);
2126
2127
  if (a.enabled !== false && upstreamBlocking && a.status === 'active') {
2127
2128
  effectiveStatus = a.inFlight > 0 ? 'probing' : 'waiting';
2128
2129
  }
@@ -2173,6 +2174,7 @@ export class TUI {
2173
2174
  case 'waiting': status = yellow('waiting'); break;
2174
2175
  case 'paused': status = yellow('paused'); break;
2175
2176
  case 'disabled': status = red('✕ disabled'); break;
2177
+ case 'pool-benched': status = gray('⟐ team pool'); break;
2176
2178
  // Disabled AND needs re-login — both facts matter, and DISABLED LEADS.
2177
2179
  // The owner's question that drove this (2026-09-30): "when I disable the account
2178
2180
  // and then the token expires, does it show disabled or reauth? It needs to