maxpool 1.24.91 → 1.24.93

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.91",
3
+ "version": "1.24.93",
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",
package/src/event-log.js CHANGED
@@ -6,6 +6,8 @@
6
6
  // write is fire-and-forget and all errors are swallowed.
7
7
 
8
8
  import { appendFile, stat, rename, chmod } from 'node:fs/promises';
9
+ import { isatty } from 'node:tty';
10
+ import { writeSync } from 'node:fs';
9
11
 
10
12
  const MAX_BYTES = 5 * 1024 * 1024; // rotate at ~5 MB
11
13
  // Keep 5 generations (~30 MB total), not 1. With a single backup the log rotated away the
@@ -122,6 +124,78 @@ let stdoutSuppressed = false;
122
124
  */
123
125
  export function setConsoleStdoutSuppressed(v) { stdoutSuppressed = !!v; }
124
126
 
127
+ // Streams made non-blocking by setConsoleNonBlocking (real terminals only).
128
+ const nonBlockingStreams = new Set();
129
+
130
+ /**
131
+ * Never let the terminal stall this process (2026-11-13: the supervisor sat inside a
132
+ * console.log for 7+ minutes while the terminal was not reading, so a failed reload was
133
+ * neither rolled back nor reaped). Node writes a TTY synchronously; this turns the real
134
+ * terminal streams non-blocking — the same switch the TUI makes for its own frames
135
+ * (tui.js start()) — and the console mirror then drops a line's terminal copy while the
136
+ * terminal has not taken what it already got. The event log still gets every line.
137
+ * libuv reopens a TTY per process, so this never changes another process's terminal.
138
+ * Returns the number of streams switched.
139
+ */
140
+ export function setConsoleNonBlocking({ streams = [process.stdout, process.stderr], isTTY = s => isatty(s.fd) } = {}) {
141
+ let n = 0;
142
+ for (const s of streams) {
143
+ if (!s || nonBlockingStreams.has(s) || !isTTY(s) || typeof s._handle?.setBlocking !== 'function') continue;
144
+ try { s._handle.setBlocking(false); nonBlockingStreams.add(s); n++; } catch { /* not a libuv tty */ }
145
+ }
146
+ return n;
147
+ }
148
+
149
+ /**
150
+ * Turn the switched streams non-blocking AGAIN. Call right after spawning a child with
151
+ * inherited stdio: libuv's child setup clears O_NONBLOCK on fds 0-2 (uv__process_child_init),
152
+ * and an inherited fd shares the parent's open file description, so every such spawn
153
+ * silently makes the parent's terminal blocking again (2026-11-13: the supervisor still
154
+ * froze in write() after spawning the reload worker). spawn() returns only after the
155
+ * child has exec'd, so the child's reset is already done when this runs.
156
+ */
157
+ export function reassertConsoleNonBlocking() {
158
+ for (const s of nonBlockingStreams) {
159
+ try { s._handle?.setBlocking?.(false); } catch { /* stream gone */ }
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Undo setConsoleNonBlocking — for EXIT paths only. A non-blocking write still queued at
165
+ * process.exit is dropped, so a fatal message printed on the way out vanished
166
+ * (red-team 2026-11-13: a respawn's EADDRINUSE). Blocking is right there: the process
167
+ * is ending anyway, and the message waits for the terminal instead of being lost.
168
+ */
169
+ export function setConsoleBlocking() {
170
+ for (const s of nonBlockingStreams) {
171
+ try { s._handle?.setBlocking?.(true); } catch { /* stream gone */ }
172
+ }
173
+ nonBlockingStreams.clear();
174
+ }
175
+
176
+ /**
177
+ * Print the lines that explain why the process is about to exit, so that they reach the
178
+ * terminal even when it is not reading. A stderr we made non-blocking is made blocking
179
+ * again and written directly (fd 2), past anything still queued in front of it; every
180
+ * other stderr prints through console.error as usual.
181
+ */
182
+ export function printFatal(lines, { stream = process.stderr, write = writeSync } = {}) {
183
+ if (!nonBlockingStreams.has(stream)) {
184
+ for (const line of lines) console.error(line);
185
+ return;
186
+ }
187
+ setConsoleBlocking();
188
+ for (const line of lines) {
189
+ try { appendEventLog(line); } catch { /* never */ }
190
+ try { write(stream.fd, `${line}\n`); } catch { /* terminal gone */ }
191
+ }
192
+ }
193
+
194
+ /** True when `stream` is one we made non-blocking and the terminal has not drained it. */
195
+ function terminalBehind(stream) {
196
+ return nonBlockingStreams.has(stream) && Boolean(stream.writableNeedDrain);
197
+ }
198
+
125
199
  export function installConsoleMirror() {
126
200
  if (origConsole) return; // idempotent
127
201
  origConsole = { log: console.log.bind(console), error: console.error.bind(console) };
@@ -129,7 +203,10 @@ export function installConsoleMirror() {
129
203
  const orig = origConsole[method];
130
204
  console[method] = (...args) => {
131
205
  try { appendEventLog(args.map(a => (typeof a === 'string' ? a : String(a))).join(' ')); } catch { /* never */ }
132
- if (!stdoutSuppressed) orig(...args);
206
+ if (stdoutSuppressed) return;
207
+ // Read the stream live: a test (or the TUI) may swap process.stdout.
208
+ if (terminalBehind(method === 'log' ? process.stdout : process.stderr)) return;
209
+ orig(...args);
133
210
  };
134
211
  }
135
212
  }
@@ -142,4 +219,5 @@ export function __resetEventLogForTest() {
142
219
  if (origConsole) { console.log = origConsole.log; console.error = origConsole.error; origConsole = null; }
143
220
  if (rotationTimer) { clearInterval(rotationTimer); rotationTimer = null; }
144
221
  logPath = null; manageRotation = false; writeChain = Promise.resolve(); queued = 0; dropped = 0; stdoutSuppressed = false;
222
+ nonBlockingStreams.clear();
145
223
  }