maxpool 1.24.35 → 1.24.37
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 +1 -1
- package/src/account-manager.js +11 -0
- package/src/index.js +72 -5
- package/src/screen.html +138 -0
- package/src/secret-resolver.js +3 -1
- package/src/server.js +101 -42
- package/src/tui.js +5 -2
package/package.json
CHANGED
package/src/account-manager.js
CHANGED
|
@@ -465,6 +465,9 @@ export class AccountManager {
|
|
|
465
465
|
else this.scheduler.routingMode = 'sticky';
|
|
466
466
|
}
|
|
467
467
|
this._refreshAccessToken = dependencies.refreshAccessToken || refreshAccessToken;
|
|
468
|
+
// Named-caller request attribution (team pools): callerName → requests served.
|
|
469
|
+
// Server-side (not TUI-side) so headless pools feed the web screen too.
|
|
470
|
+
this.callerCounts = new Map();
|
|
468
471
|
this.poolUpSince = Date.now();
|
|
469
472
|
this.accounts = accounts.map((acct, index) => ({
|
|
470
473
|
index,
|
|
@@ -840,6 +843,13 @@ export class AccountManager {
|
|
|
840
843
|
this.sessionBindings.set(sessionKey, binding);
|
|
841
844
|
}
|
|
842
845
|
|
|
846
|
+
// Named-caller attribution: one bump per CLIENT request (server calls this
|
|
847
|
+
// right after auth, so retries/reroutes never double-count).
|
|
848
|
+
countRequest(callerName) {
|
|
849
|
+
if (!callerName) return;
|
|
850
|
+
this.callerCounts.set(callerName, (this.callerCounts.get(callerName) || 0) + 1);
|
|
851
|
+
}
|
|
852
|
+
|
|
843
853
|
releaseAccount(lease, outcome = {}) {
|
|
844
854
|
if (!lease?.account) return;
|
|
845
855
|
const account = lease.account;
|
|
@@ -4677,6 +4687,7 @@ export class AccountManager {
|
|
|
4677
4687
|
? Math.max(0, now - this.queueState.waiting[0].queuedAt)
|
|
4678
4688
|
: 0,
|
|
4679
4689
|
},
|
|
4690
|
+
callers: Object.fromEntries([...this.callerCounts.entries()].map(([n, c]) => [n, { requests: c }])),
|
|
4680
4691
|
sessions: {
|
|
4681
4692
|
stickyBindings: this.sessionBindings.size,
|
|
4682
4693
|
thinkingProtected: [...this.sessionPolicies.values()].filter(p => p.requiresAnthropicThinkingIntegrity).length,
|
package/src/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { stat } from 'node:fs/promises';
|
|
|
15
15
|
import { spawn, spawnSync } from 'node:child_process';
|
|
16
16
|
import { createInterface } from 'node:readline';
|
|
17
17
|
import { loadOrCreateConfig, loadConfig, saveConfig, atomicConfigUpdate, getConfigPath, loadState, saveState, getStatePath, getLogPath, readGeneration, flushConfigWrites, flushStateWrites, saveStateUnbumped } from './config.js';
|
|
18
|
-
import { setEventLogPath, installConsoleMirror, setConsoleStdoutSuppressed } from './event-log.js';
|
|
18
|
+
import { setEventLogPath, installConsoleMirror, setConsoleStdoutSuppressed, appendEventLog } from './event-log.js';
|
|
19
19
|
import { SleepGuard } from './sleep-guard.js';
|
|
20
20
|
import { AccountManager } from './account-manager.js';
|
|
21
21
|
import { createProxyServer, REQUEST_IDLE_MAX_MS } from './server.js';
|
|
@@ -133,6 +133,10 @@ switch (command) {
|
|
|
133
133
|
await envCommand();
|
|
134
134
|
process.exit(0);
|
|
135
135
|
break;
|
|
136
|
+
case 'pool':
|
|
137
|
+
await poolCommand();
|
|
138
|
+
process.exit(0);
|
|
139
|
+
break;
|
|
136
140
|
case 'status':
|
|
137
141
|
await statusCommand();
|
|
138
142
|
process.exit(0);
|
|
@@ -947,7 +951,14 @@ async function serverWorkerCommand() {
|
|
|
947
951
|
},
|
|
948
952
|
onRequestEnd: (id, info) => {
|
|
949
953
|
restartController.requestEnded(id);
|
|
950
|
-
tui
|
|
954
|
+
if (tui) {
|
|
955
|
+
tui.onRequestEnd(id, info);
|
|
956
|
+
} else if (info.caller) {
|
|
957
|
+
// Headless (service / shared VM) pools have no TUI log buffer, so named-caller
|
|
958
|
+
// attribution would otherwise reach no log at all. Only named callers are
|
|
959
|
+
// logged here — a solo headless pool stays as quiet as before.
|
|
960
|
+
appendEventLog(`[Maxpool] ${info.caller} · ${info.method} ${info.path} → ${info.account || '?'} (${info.status})`);
|
|
961
|
+
}
|
|
951
962
|
},
|
|
952
963
|
};
|
|
953
964
|
|
|
@@ -1695,9 +1706,58 @@ async function loginOAuthCommand() {
|
|
|
1695
1706
|
|
|
1696
1707
|
async function envCommand() {
|
|
1697
1708
|
const config = await loadOrCreateConfig();
|
|
1698
|
-
|
|
1709
|
+
// A `pool shared` switch redirects this machine's client traffic at the team
|
|
1710
|
+
// pool; `env`/`run` must follow it or they'd keep hitting the local pool.
|
|
1711
|
+
const client = config.client?.mode === 'shared' && config.client?.sharedHost
|
|
1712
|
+
? { host: config.client.sharedHost, key: config.client.sharedKey || '' }
|
|
1713
|
+
: { host: `${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`, key: '' };
|
|
1714
|
+
console.log(`export ANTHROPIC_BASE_URL=http://${client.host}`);
|
|
1699
1715
|
if (args.includes('--with-key')) {
|
|
1700
|
-
console.log(`export ANTHROPIC_API_KEY=${config.proxy.apiKey}`);
|
|
1716
|
+
console.log(`export ANTHROPIC_API_KEY=${client.key || config.proxy.apiKey}`);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
// ── pool (client-side switch between local and shared pool) ─
|
|
1721
|
+
|
|
1722
|
+
async function poolCommand() {
|
|
1723
|
+
const sub = args[1];
|
|
1724
|
+
const config = await loadOrCreateConfig();
|
|
1725
|
+
config.client = config.client || {};
|
|
1726
|
+
|
|
1727
|
+
if (sub === 'shared') {
|
|
1728
|
+
const host = args[2];
|
|
1729
|
+
const key = args[3];
|
|
1730
|
+
if (!host || !key || key.startsWith('-')) {
|
|
1731
|
+
console.error('Usage: maxpool pool shared <host:port> <your-pool-key>');
|
|
1732
|
+
console.error(' e.g. maxpool pool shared maxpool.tail-scale.ts.net:3456 mp-personal-key');
|
|
1733
|
+
process.exit(1);
|
|
1734
|
+
}
|
|
1735
|
+
config.client.mode = 'shared';
|
|
1736
|
+
config.client.sharedHost = host;
|
|
1737
|
+
config.client.sharedKey = key;
|
|
1738
|
+
await saveConfig(config);
|
|
1739
|
+
console.log(`This machine now points at the SHARED pool: http://${host}`);
|
|
1740
|
+
console.log('Apply it in your shells (or restart terminals):');
|
|
1741
|
+
console.log(` export ANTHROPIC_BASE_URL=http://${host}`);
|
|
1742
|
+
console.log(` export ANTHROPIC_API_KEY=${key}`);
|
|
1743
|
+
console.log('rc-gate users: also set RC_GATE_MAXPOOL_HOST/PORT/KEY in the LaunchAgent and restart it.');
|
|
1744
|
+
} else if (sub === 'local') {
|
|
1745
|
+
config.client.mode = 'local';
|
|
1746
|
+
delete config.client.sharedHost;
|
|
1747
|
+
delete config.client.sharedKey;
|
|
1748
|
+
await saveConfig(config);
|
|
1749
|
+
console.log(`This machine points at its OWN local pool: http://${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`);
|
|
1750
|
+
console.log('Unset ANTHROPIC_BASE_URL/ANTHROPIC_API_KEY (or re-export the local ones) and restart terminals.');
|
|
1751
|
+
} else if (sub === 'status') {
|
|
1752
|
+
const mode = config.client?.mode || 'local';
|
|
1753
|
+
if (mode === 'shared') {
|
|
1754
|
+
console.log(`mode: shared → http://${config.client.sharedHost}`);
|
|
1755
|
+
} else {
|
|
1756
|
+
console.log(`mode: local → http://${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`);
|
|
1757
|
+
}
|
|
1758
|
+
} else {
|
|
1759
|
+
console.error('Usage: maxpool pool <shared <host:port> <key> | local | status>');
|
|
1760
|
+
process.exit(1);
|
|
1701
1761
|
}
|
|
1702
1762
|
}
|
|
1703
1763
|
|
|
@@ -1718,7 +1778,11 @@ async function runCommand() {
|
|
|
1718
1778
|
stdio: 'inherit',
|
|
1719
1779
|
env: {
|
|
1720
1780
|
...process.env,
|
|
1721
|
-
|
|
1781
|
+
// Follows the `pool shared` switch like envCommand (client.mode 'shared'
|
|
1782
|
+
// redirects run-subcommand traffic at the team pool).
|
|
1783
|
+
ANTHROPIC_BASE_URL: config.client?.mode === 'shared' && config.client?.sharedHost
|
|
1784
|
+
? `http://${config.client.sharedHost}`
|
|
1785
|
+
: `http://${config.proxy.host || '127.0.0.1'}:${config.proxy.port}`,
|
|
1722
1786
|
},
|
|
1723
1787
|
});
|
|
1724
1788
|
|
|
@@ -2021,6 +2085,9 @@ Commands:
|
|
|
2021
2085
|
login OAuth login via browser (adds the account you're signed into at claude.ai)
|
|
2022
2086
|
login --api Add an API key account
|
|
2023
2087
|
env [--with-key] Print env vars to use with Claude
|
|
2088
|
+
pool shared <host> <key> Point this machine at a shared team pool
|
|
2089
|
+
pool local Point this machine back at its own local pool
|
|
2090
|
+
pool status Show which pool this machine points at
|
|
2024
2091
|
run [-- args...] Run Claude Code through the proxy
|
|
2025
2092
|
status Show proxy & account status (live)
|
|
2026
2093
|
accounts List configured accounts
|
package/src/screen.html
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
6
|
+
<title>Maxpool Accounts</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root{
|
|
9
|
+
--bg:#f7f6f3; --fg:#1b1a17; --muted:#6c665c; --line:#e3ded5; --card:#fff;
|
|
10
|
+
--ok:#1f7a5c; --warn:#a86500; --bad:#b3261e; --bar:#e9e5dd; --chip:#efece6;
|
|
11
|
+
color-scheme:light;
|
|
12
|
+
}
|
|
13
|
+
@media (prefers-color-scheme: dark){
|
|
14
|
+
:root{ --bg:#131210; --fg:#ebe6dc; --muted:#a29a8c; --line:#2b2823; --card:#1b1a16;
|
|
15
|
+
--ok:#56c3a0; --warn:#e2a650; --bad:#f07b72; --bar:#2a2722; --chip:#24221d; color-scheme:dark; }
|
|
16
|
+
}
|
|
17
|
+
*{box-sizing:border-box}
|
|
18
|
+
body{margin:0;background:var(--bg);color:var(--fg);
|
|
19
|
+
font:14px/1.5 ui-sans-serif,-apple-system,"Segoe UI",Roboto,sans-serif;
|
|
20
|
+
padding-left:max(16px,env(safe-area-inset-left,0px));padding-right:max(16px,env(safe-area-inset-right,0px))}
|
|
21
|
+
.wrap{max-width:980px;margin:0 auto;padding-block:24px 48px}
|
|
22
|
+
header{display:flex;flex-wrap:wrap;align-items:baseline;gap:8px 16px;margin-bottom:16px}
|
|
23
|
+
h1{font-size:22px;margin:0;letter-spacing:-.3px}
|
|
24
|
+
.meta{color:var(--muted);font-size:13px}
|
|
25
|
+
.people{display:flex;flex-wrap:wrap;gap:8px;margin:0 0 18px}
|
|
26
|
+
.chip{background:var(--chip);border:1px solid var(--line);border-radius:999px;padding:3px 11px;font-size:12.5px}
|
|
27
|
+
.grid{display:grid;gap:10px}
|
|
28
|
+
.acct{background:var(--card);border:1px solid var(--line);border-radius:12px;padding:12px 14px;display:grid;
|
|
29
|
+
grid-template-columns:minmax(0,1.2fr) minmax(0,2fr) auto;gap:6px 16px;align-items:center}
|
|
30
|
+
@media (max-width:640px){.acct{grid-template-columns:minmax(0,1fr) auto}.bars{grid-column:1/-1}}
|
|
31
|
+
.name{font-weight:600;overflow-wrap:anywhere}
|
|
32
|
+
.sub{color:var(--muted);font-size:12px}
|
|
33
|
+
.bars{display:grid;gap:5px}
|
|
34
|
+
.bar{display:grid;grid-template-columns:28px minmax(0,1fr) 42px;gap:8px;align-items:center;font-size:12px;color:var(--muted)}
|
|
35
|
+
.track{height:8px;background:var(--bar);border-radius:99px;overflow:hidden}
|
|
36
|
+
.fill{height:100%;background:var(--ok);border-radius:99px}
|
|
37
|
+
.fill.warn{background:var(--warn)}.fill.bad{background:var(--bad)}
|
|
38
|
+
.state{justify-self:start;font-size:12.5px;font-weight:600;padding:3px 10px;border-radius:999px;white-space:nowrap;background:var(--chip)}
|
|
39
|
+
.state.ok{color:var(--ok)}.state.warn{color:var(--warn)}.state.bad{color:#fff;background:var(--bad)}
|
|
40
|
+
.acct.dead{border-color:var(--bad)}
|
|
41
|
+
.gate{max-width:420px;margin:12vh auto;background:var(--card);border:1px solid var(--line);border-radius:14px;padding:22px}
|
|
42
|
+
.gate input{width:100%;font:inherit;padding:9px 11px;border:1px solid var(--line);border-radius:8px;background:var(--bg);color:var(--fg);margin:10px 0}
|
|
43
|
+
.gate button,.link{font:inherit;padding:8px 14px;border-radius:8px;border:1px solid var(--line);background:var(--chip);color:var(--fg);cursor:pointer}
|
|
44
|
+
.err{color:var(--bad);font-size:13px;min-height:18px}
|
|
45
|
+
.link{padding:3px 9px;font-size:12px}
|
|
46
|
+
</style>
|
|
47
|
+
</head>
|
|
48
|
+
<body>
|
|
49
|
+
<div class="wrap" id="app"></div>
|
|
50
|
+
<script>
|
|
51
|
+
(function () {
|
|
52
|
+
var KEY_STORE = 'maxpool.screen.key';
|
|
53
|
+
var app = document.getElementById('app');
|
|
54
|
+
function getKey() { try { return localStorage.getItem(KEY_STORE) || ''; } catch (e) { return ''; } }
|
|
55
|
+
function setKey(k) { try { if (k) localStorage.setItem(KEY_STORE, k); else localStorage.removeItem(KEY_STORE); } catch (e) {} }
|
|
56
|
+
function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return ({'&':'&','<':'<','>':'>','"':'"',"'":'''})[c]; }); }
|
|
57
|
+
function pct(v) { if (v == null || !isFinite(v)) return null; return Math.max(0, Math.min(100, v > 1 ? v : v * 100)); }
|
|
58
|
+
|
|
59
|
+
function gate(msg) {
|
|
60
|
+
app.innerHTML = '<div class="gate"><h1>Maxpool accounts</h1>' +
|
|
61
|
+
'<p class="sub">Enter your personal pool key. It stays in this browser only.</p>' +
|
|
62
|
+
'<input id="k" type="password" autocomplete="off" placeholder="Pool key">' +
|
|
63
|
+
'<div class="err">' + esc(msg || '') + '</div><button id="go">Open</button></div>';
|
|
64
|
+
document.getElementById('go').onclick = function () { setKey(document.getElementById('k').value.trim()); load(); };
|
|
65
|
+
document.getElementById('k').onkeydown = function (e) { if (e.key === 'Enter') document.getElementById('go').click(); };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function bar(label, v) {
|
|
69
|
+
var p = pct(v);
|
|
70
|
+
if (p == null) return '';
|
|
71
|
+
var cls = p >= 90 ? 'bad' : p >= 75 ? 'warn' : '';
|
|
72
|
+
return '<div class="bar"><span>' + label + '</span><div class="track"><div class="fill ' + cls + '" style="width:' + p.toFixed(0) + '%"></div></div><span>' + p.toFixed(0) + '%</span></div>';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// status enum from account-manager: 'active' | 'throttled' | 'error'.
|
|
76
|
+
function stateOf(a) {
|
|
77
|
+
var q = a.quota || {};
|
|
78
|
+
var week = q.unified7d != null ? q.unified7d : q.providerWk;
|
|
79
|
+
if (a.enabled === false) return ['', 'Off'];
|
|
80
|
+
if (a.refreshDead) return ['bad', 'Needs login'];
|
|
81
|
+
if (a.status === 'error') return ['bad', 'Error'];
|
|
82
|
+
if (week != null && week >= 0.999) return ['warn', 'Used up this week'];
|
|
83
|
+
if (a.status === 'throttled' || a.rateLimitedUntil) return ['warn', 'Resting'];
|
|
84
|
+
return ['ok', 'Ready'];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function render(s) {
|
|
88
|
+
// Live needs-login accounts first; switched-off accounts last.
|
|
89
|
+
function rank(a) { return a.enabled === false ? 2 : (a.refreshDead ? 0 : 1); }
|
|
90
|
+
var accts = (s.accounts || []).slice().sort(function (x, y) {
|
|
91
|
+
return rank(x) - rank(y) || String(x.name).localeCompare(String(y.name));
|
|
92
|
+
});
|
|
93
|
+
var dead = accts.filter(function (a) { return a.refreshDead && a.enabled !== false; }).length;
|
|
94
|
+
var callers = s.callers || {};
|
|
95
|
+
var people = Object.keys(callers).sort().map(function (n) {
|
|
96
|
+
var c = callers[n].requests;
|
|
97
|
+
return '<span class="chip">' + esc(n) + ' · ' + c + (c === 1 ? ' request' : ' requests') + '</span>';
|
|
98
|
+
}).join('');
|
|
99
|
+
var rows = accts.map(function (a) {
|
|
100
|
+
var st = stateOf(a), q = a.quota || {};
|
|
101
|
+
// Field names from getStatus().accounts[].quota: Claude = unified5h/unified7d,
|
|
102
|
+
// providers (z.ai/kimi) = providerSes/providerWk. All 0..1 fractions.
|
|
103
|
+
var five = q.unified5h != null ? q.unified5h : q.providerSes;
|
|
104
|
+
var week = q.unified7d != null ? q.unified7d : q.providerWk;
|
|
105
|
+
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)) +
|
|
107
|
+
(a.inFlight ? ' · ' + a.inFlight + ' running' : '') + '</div></div>' +
|
|
108
|
+
'<div class="bars">' + bar('5h', five) + bar('7d', week) + '</div>' +
|
|
109
|
+
'<span class="state ' + st[0] + '">' + st[1] + '</span></div>';
|
|
110
|
+
}).join('');
|
|
111
|
+
var ver = (s.runningVersion || (s.version && s.version.current) || '');
|
|
112
|
+
app.innerHTML = '<header><h1>Maxpool accounts</h1><span class="meta">' + accts.length + ' accounts' +
|
|
113
|
+
(dead ? ' · <b style="color:var(--bad)">' + dead + (dead === 1 ? ' needs login' : ' need login') + '</b>' : '') +
|
|
114
|
+
(ver ? ' · v' + esc(ver) : '') + ' · updated ' + new Date().toLocaleTimeString() + '</span>' +
|
|
115
|
+
'<button class="link" id="out">Forget key</button></header>' +
|
|
116
|
+
(people ? '<div class="people">' + people + '</div>' : '') +
|
|
117
|
+
'<div class="grid">' + rows + '</div>';
|
|
118
|
+
document.getElementById('out').onclick = function () { setKey(''); gate(); };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
var timer = null;
|
|
122
|
+
function load() {
|
|
123
|
+
var k = getKey();
|
|
124
|
+
if (!k) { gate(); return; }
|
|
125
|
+
fetch('/maxpool/status', { headers: { 'x-api-key': k }, cache: 'no-store' })
|
|
126
|
+
.then(function (r) {
|
|
127
|
+
if (r.status === 401) { setKey(''); gate('That key was not accepted.'); throw null; }
|
|
128
|
+
if (!r.ok) throw new Error('Pool returned ' + r.status);
|
|
129
|
+
return r.json();
|
|
130
|
+
})
|
|
131
|
+
.then(function (s) { render(s); clearTimeout(timer); timer = setTimeout(load, 5000); })
|
|
132
|
+
.catch(function (e) { if (e) { clearTimeout(timer); timer = setTimeout(load, 5000); } });
|
|
133
|
+
}
|
|
134
|
+
load();
|
|
135
|
+
})();
|
|
136
|
+
</script>
|
|
137
|
+
</body>
|
|
138
|
+
</html>
|
package/src/secret-resolver.js
CHANGED
|
@@ -17,7 +17,9 @@ import { join } from 'node:path';
|
|
|
17
17
|
|
|
18
18
|
const execFileAsync = promisify(execFile);
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// Configurable so public-npm users point at their own GCP project (set
|
|
21
|
+
// MAXPOOL_GCP_PROJECT); our team default stays ours.
|
|
22
|
+
const DEFAULT_PROJECT = process.env.MAXPOOL_GCP_PROJECT || 'mokka-business-automations';
|
|
21
23
|
|
|
22
24
|
// A single `gcloud secrets versions access` costs ~17-33s on this machine (python
|
|
23
25
|
// interpreter start + an ADC round-trip), and running them CONCURRENTLY makes it
|
package/src/server.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import http from 'node:http';
|
|
2
|
+
import { appendEventLog } from './event-log.js';
|
|
2
3
|
import { ThreadOwners, readThreadIntent, threadRefusalBody, isThreadlessAccount } from './thread-gate.js';
|
|
3
4
|
import { writeFile, mkdir } from 'node:fs/promises';
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
4
6
|
import { join } from 'node:path';
|
|
5
7
|
import { modelFamily } from './oauth.js';
|
|
6
8
|
|
|
@@ -39,6 +41,15 @@ const isNetworkCode = c => Boolean(c) && NETWORK_ERROR_CODES.has(c);
|
|
|
39
41
|
const NETWORK_SOAK_NONSTREAM_MS = Math.max(5_000,
|
|
40
42
|
Number(process.env.MAXPOOL_NETWORK_SOAK_NONSTREAM_MS) || 45_000);
|
|
41
43
|
const QUEUE_KEEPALIVE = 'event: ping\ndata: {}\n\n';
|
|
44
|
+
// Account screen shell (served keyless at /maxpool/screen; the page itself prompts
|
|
45
|
+
// for a pool key and reads the key-gated /maxpool/status). Read once at boot.
|
|
46
|
+
const SCREEN_HTML = (() => {
|
|
47
|
+
try {
|
|
48
|
+
return readFileSync(new URL('./screen.html', import.meta.url), 'utf8');
|
|
49
|
+
} catch {
|
|
50
|
+
return '<!doctype html><title>Maxpool</title><p>screen.html missing</p>';
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
42
53
|
// Claude Code's absolute per-stream ceiling (binary-verified 2026-10-07: idle timeout =
|
|
43
54
|
// min(env-or-300s-floor, 1800000)). Nothing resets it; the stream dies at 30 min.
|
|
44
55
|
const CLI_STREAM_CEILING_MS = 25 * 60_000; // bound network holds strictly below it
|
|
@@ -146,6 +157,37 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
146
157
|
const logDir = config.logDir || null;
|
|
147
158
|
let requestCounter = 0;
|
|
148
159
|
|
|
160
|
+
// ── team clients (per-person keys + caller identity) ──
|
|
161
|
+
// `clients: [{name, key}]` opts into strict auth: EVERY request (proxy traffic,
|
|
162
|
+
// /maxpool/status, /maxpool/screen) must carry a listed key, the loopback bypass
|
|
163
|
+
// is removed, and each request is attributed to the named person in logs/status.
|
|
164
|
+
// A non-loopback listen host also forces strict even without clients (a shared
|
|
165
|
+
// pool must never trust by address). Default local config (loopback host, no
|
|
166
|
+
// clients) keeps today's behavior exactly.
|
|
167
|
+
const clientList = Array.isArray(config.proxy?.clients)
|
|
168
|
+
? config.proxy.clients.filter(c => c && typeof c.key === 'string' && c.key)
|
|
169
|
+
: [];
|
|
170
|
+
const clientByKey = new Map(clientList.map(c => [c.key, c.name]));
|
|
171
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', 'localhost', '::1', '0.0.0.0']);
|
|
172
|
+
const hostIsLoopback = (config.proxy?.host == null) || LOOPBACK_HOSTS.has(String(config.proxy.host).toLowerCase());
|
|
173
|
+
const strictClients = clientByKey.size > 0 || !hostIsLoopback;
|
|
174
|
+
const resolveCaller = (key, isLocal) => {
|
|
175
|
+
if (key != null && clientByKey.has(key)) return { name: clientByKey.get(key), ok: true };
|
|
176
|
+
// Legacy single key still works — including strict mode, so a solo user can
|
|
177
|
+
// point clients at a shared host without defining named clients.
|
|
178
|
+
if (proxyApiKey && key === proxyApiKey) return { name: null, ok: true };
|
|
179
|
+
if (!strictClients) {
|
|
180
|
+
// Legacy trust model: loopback callers are trusted; when NO key is
|
|
181
|
+
// configured at all the pool is open (today's no-apiKey behavior).
|
|
182
|
+
if (isLocal || !proxyApiKey) return { name: null, ok: true };
|
|
183
|
+
}
|
|
184
|
+
return { name: null, ok: false };
|
|
185
|
+
};
|
|
186
|
+
if (strictClients) {
|
|
187
|
+
const who = clientByKey.size > 0 ? `${clientByKey.size} named client${clientByKey.size > 1 ? 's' : ''}` : 'non-loopback listen host';
|
|
188
|
+
appendEventLog(`[Maxpool] Strict client auth ON (${who}; loopback bypass removed)`);
|
|
189
|
+
}
|
|
190
|
+
|
|
149
191
|
if (logDir) {
|
|
150
192
|
mkdir(logDir, { recursive: true }).catch(() => {});
|
|
151
193
|
}
|
|
@@ -181,20 +223,41 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
181
223
|
if (draining) {
|
|
182
224
|
try { res.setHeader('Connection', 'close'); } catch { /* headers may be sent */ }
|
|
183
225
|
}
|
|
184
|
-
// Auth check —
|
|
226
|
+
// Auth check — per-person clients when configured, legacy single key otherwise.
|
|
227
|
+
// strictClients (clients list present, or a non-loopback listen host) removes
|
|
228
|
+
// the loopback bypass entirely: every caller must present a known key.
|
|
185
229
|
const clientKey = req.headers['x-api-key'];
|
|
186
230
|
const remoteAddr = req.socket.remoteAddress;
|
|
187
231
|
const isLocal = remoteAddr === '127.0.0.1' || remoteAddr === '::1' || remoteAddr === '::ffff:127.0.0.1';
|
|
232
|
+
const caller = resolveCaller(clientKey, isLocal);
|
|
233
|
+
|
|
234
|
+
const rejectUnauthorized = () => {
|
|
235
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
236
|
+
res.end(JSON.stringify({
|
|
237
|
+
type: 'error',
|
|
238
|
+
error: { type: 'authentication_error', message: strictClients ? 'Invalid or missing pool key' : 'Invalid proxy API key' },
|
|
239
|
+
}));
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// Account screen: a static page with NO data in it — it asks the viewer for
|
|
243
|
+
// their pool key and reads /maxpool/status with it (which stays key-gated
|
|
244
|
+
// below), so serving the shell itself keyless leaks nothing.
|
|
245
|
+
if (req.method === 'GET' && (req.url === '/maxpool/screen' || req.url === '/maxpool/screen/')) {
|
|
246
|
+
res.writeHead(200, {
|
|
247
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
248
|
+
'Cache-Control': 'no-store',
|
|
249
|
+
'X-Frame-Options': 'DENY',
|
|
250
|
+
'Referrer-Policy': 'no-referrer',
|
|
251
|
+
});
|
|
252
|
+
res.end(SCREEN_HTML);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
188
255
|
|
|
189
256
|
// Status exposes account names and quota state, so require the local
|
|
190
257
|
// proxy key even for loopback callers.
|
|
191
258
|
if (req.method === 'GET' && req.url === '/maxpool/status') {
|
|
192
|
-
if (proxyApiKey && clientKey !== proxyApiKey) {
|
|
193
|
-
|
|
194
|
-
res.end(JSON.stringify({
|
|
195
|
-
type: 'error',
|
|
196
|
-
error: { type: 'authentication_error', message: 'Invalid proxy API key' },
|
|
197
|
-
}));
|
|
259
|
+
if (strictClients ? !caller.ok : (proxyApiKey && clientKey !== proxyApiKey)) {
|
|
260
|
+
rejectUnauthorized();
|
|
198
261
|
return;
|
|
199
262
|
}
|
|
200
263
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
@@ -202,14 +265,12 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
202
265
|
return;
|
|
203
266
|
}
|
|
204
267
|
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
res.end(JSON.stringify({
|
|
208
|
-
type: 'error',
|
|
209
|
-
error: { type: 'authentication_error', message: 'Invalid proxy API key' },
|
|
210
|
-
}));
|
|
268
|
+
if (!caller.ok) {
|
|
269
|
+
rejectUnauthorized();
|
|
211
270
|
return;
|
|
212
271
|
}
|
|
272
|
+
req.callerName = caller.name;
|
|
273
|
+
accountManager.countRequest?.(caller.name);
|
|
213
274
|
|
|
214
275
|
// Let client token refresh requests pass through to upstream untouched.
|
|
215
276
|
// The proxy manages its own tokens via ensureTokenFresh(); intercepting
|
|
@@ -217,7 +278,7 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
217
278
|
if (req.method === 'POST' && req.url === '/v1/oauth/token') {
|
|
218
279
|
const reqId = ++requestCounter;
|
|
219
280
|
const ctx = { account: '(oauth relay)', status: null };
|
|
220
|
-
const accepted = hooks.onRequestStart?.(reqId, { method: req.method, path: req.url, sessionKey: headerValue(req.headers, 'x-maxpool-session') });
|
|
281
|
+
const accepted = hooks.onRequestStart?.(reqId, { method: req.method, path: req.url, sessionKey: headerValue(req.headers, 'x-maxpool-session'), caller: req.callerName || null });
|
|
221
282
|
if (accepted === false) {
|
|
222
283
|
res.writeHead(503, {
|
|
223
284
|
'Content-Type': 'application/json',
|
|
@@ -253,7 +314,7 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
253
314
|
// sessionKey (from the per-terminal x-maxpool-session header) lets the TUI show
|
|
254
315
|
// how many distinct client sessions the in-flight requests span — so "N in-flight"
|
|
255
316
|
// isn't misread as "N sessions" (subagents of one terminal share one session key).
|
|
256
|
-
const accepted = hooks.onRequestStart?.(reqId, { method: req.method, path: req.url, sessionKey: headerValue(req.headers, 'x-maxpool-session') });
|
|
317
|
+
const accepted = hooks.onRequestStart?.(reqId, { method: req.method, path: req.url, sessionKey: headerValue(req.headers, 'x-maxpool-session'), caller: req.callerName || null });
|
|
257
318
|
if (accepted === false) {
|
|
258
319
|
res.writeHead(503, {
|
|
259
320
|
'Content-Type': 'application/json',
|
|
@@ -338,6 +399,7 @@ export function createProxyServer(accountManager, config, hooks = {}) {
|
|
|
338
399
|
hooks.onRequestEnd?.(reqId, {
|
|
339
400
|
method: req.method, path: req.url,
|
|
340
401
|
account: ctx.account, status: ctx.status,
|
|
402
|
+
caller: req.callerName || null,
|
|
341
403
|
});
|
|
342
404
|
}
|
|
343
405
|
} catch (err) {
|
|
@@ -1935,7 +1997,7 @@ function isContextLengthError(errorBody) {
|
|
|
1935
1997
|
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);
|
|
1936
1998
|
}
|
|
1937
1999
|
|
|
1938
|
-
export const __serverTest = { directiveOnlySystemMessages, rewriteBodyForAccount, sanitizeBlocksForProvider, reanchorOrphanedSystemMessages, unavailableMessage, computeQueueWindowMs, 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 };
|
|
2000
|
+
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 };
|
|
1939
2001
|
|
|
1940
2002
|
async function readErrorBody(upstreamRes, limitBytes = 64 * 1024) {
|
|
1941
2003
|
if (!upstreamRes.body) return '';
|
|
@@ -2715,30 +2777,23 @@ async function queueAndRetry(
|
|
|
2715
2777
|
// terminal (disabled / error / auth-dead)". This is what stops an indefinite
|
|
2716
2778
|
// hold from silently hanging every session when something is actually broken
|
|
2717
2779
|
// (all accounts logged out, the only healthy account removed, etc.).
|
|
2718
|
-
const retryAfterMs = retryPlan.retryAfterMs;
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
2731
|
-
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
// quota rollover, which wrongly lost the chain (red-team 2026-10-12, finding 3).
|
|
2736
|
-
if (requestInfo.stream && queueWindowMs > 0 && Number.isFinite(retryAfterMs)
|
|
2737
|
-
&& !CAPACITY_CLASS_RETRY_CAUSES.has(retryPlan.cause)) {
|
|
2738
|
-
requestInfo.queueHoldChain = true;
|
|
2739
|
-
} else {
|
|
2740
|
-
return finishQueuedStreamIfNeeded(res, requestInfo, honestMessage);
|
|
2741
|
-
}
|
|
2780
|
+
const retryAfterMs = retryPlan.retryMs ?? retryPlan.retryAfterMs;
|
|
2781
|
+
// ALWAYS HOLD FIRST (owner 2026-10-28: "zero reason to drop those requests… even
|
|
2782
|
+
// when I'm out of accounts completely, I buy new ones"). The old oracle errored
|
|
2783
|
+
// fast whenever no FINITE scheduled reset existed (no_eligible_route,
|
|
2784
|
+
// weekly_reset_unknown, reset beyond window) — but recovery does not need a
|
|
2785
|
+
// schedule: config auto-sync adopts a newly bought account in ~15s, and a sibling
|
|
2786
|
+
// completing frees a slot in seconds; hasAvailableRoute polls both every tick.
|
|
2787
|
+
// So: hold the FULL window regardless of the plan, and only at expiry end with
|
|
2788
|
+
// the ≤60s-retriable 429 (clientRetryAfterSeconds caps the header), which the
|
|
2789
|
+
// CLI obeys silently — that IS the chaining mechanism, for streams and
|
|
2790
|
+
// non-streams alike. Windows stay hard-capped at the client's own ceilings
|
|
2791
|
+
// (25m stream / 5m non-stream), so nothing can hang past what the client
|
|
2792
|
+
// survives. queueHoldChain is now purely informational in the log line.
|
|
2793
|
+
if (queueWindowMs > 0 && Number.isFinite(retryAfterMs)
|
|
2794
|
+
&& retryAfterMs > queueWindowMs
|
|
2795
|
+
&& !CAPACITY_CLASS_RETRY_CAUSES.has(retryPlan.cause)) {
|
|
2796
|
+
requestInfo.queueHoldChain = true;
|
|
2742
2797
|
}
|
|
2743
2798
|
|
|
2744
2799
|
requestInfo.queueStartedAt ||= Date.now();
|
|
@@ -2894,8 +2949,12 @@ async function waitForAvailableRoute(req, res, accountManager, requestInfo, queu
|
|
|
2894
2949
|
// removed, or the reset is unknown → retryAfterMs Infinity), stop holding
|
|
2895
2950
|
// and error fast instead of spinning to the 7d ceiling. Hold is valid only
|
|
2896
2951
|
// while ≥1 eligible route has a finite, known reset.
|
|
2897
|
-
|
|
2898
|
-
|
|
2952
|
+
// NOTE (owner 2026-10-28, queue-never-fail): the old non-finite-plan bail is
|
|
2953
|
+
// GONE. hasAvailableRoute above is checked every tick and returns true the
|
|
2954
|
+
// moment a route exists — including one created by config auto-sync adopting
|
|
2955
|
+
// a newly added account, which nextRetryForRequest cannot foresee. The
|
|
2956
|
+
// window (hard-capped at the client ceilings) bounds the poll; expiry hands
|
|
2957
|
+
// back a ≤60s-retriable 429 the client retries silently.
|
|
2899
2958
|
|
|
2900
2959
|
const remaining = maxWaitMs - (Date.now() - startedAt);
|
|
2901
2960
|
// Jitter the poll so a synchronized weekly-reset event doesn't re-align
|
package/src/tui.js
CHANGED
|
@@ -522,7 +522,7 @@ export class TUI {
|
|
|
522
522
|
// ── server hooks ───────────────────────────────────
|
|
523
523
|
|
|
524
524
|
onRequestStart(id, info) {
|
|
525
|
-
this.active.set(id, { ...info, t: timestamp(), started: Date.now(), account: null });
|
|
525
|
+
this.active.set(id, { ...info, t: timestamp(), started: Date.now(), account: null, caller: info.caller || null });
|
|
526
526
|
this.render();
|
|
527
527
|
}
|
|
528
528
|
|
|
@@ -536,6 +536,9 @@ export class TUI {
|
|
|
536
536
|
this.active.delete(id);
|
|
537
537
|
const dur = r ? ((Date.now() - r.started) / 1000).toFixed(1) : '?';
|
|
538
538
|
const acct = info.account || r?.account || '?';
|
|
539
|
+
// Named-caller attribution (team pools): prefix the log line with the person
|
|
540
|
+
// whose pool key authenticated the request. Null on legacy/local setups.
|
|
541
|
+
const who = info.caller || r?.caller ? `${info.caller || r?.caller} · ` : '';
|
|
539
542
|
// Tag PROVIDER-routed requests with the maxpool session id so provider traffic
|
|
540
543
|
// (when-exhausted overflow, or an incompatible-session pin) is traceable back to
|
|
541
544
|
// the session that caused it: grep the event log for `→ glm-fallback … [sess X]`,
|
|
@@ -543,7 +546,7 @@ export class TUI {
|
|
|
543
546
|
// guard — a header-less request has sessionKey '' (server.js headerValue) → no tag.
|
|
544
547
|
const isProvider = this.am.accounts.find(a => a.name === acct)?.type === 'provider';
|
|
545
548
|
const tag = (isProvider && r?.sessionKey) ? ` [sess ${String(r.sessionKey).slice(0, 8)}]` : '';
|
|
546
|
-
this._addLog(`${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${tag}`);
|
|
549
|
+
this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${tag}`);
|
|
547
550
|
}
|
|
548
551
|
|
|
549
552
|
_addLog(msg) {
|