maxpool 1.24.90 → 1.24.92

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/src/tui.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createInterface } from 'node:readline';
2
- import { readFileSync } from 'node:fs';
2
+ import { readFileSync, writeSync } from 'node:fs';
3
+ import { isatty } from 'node:tty';
3
4
  import { homedir } from 'node:os';
4
5
  import { fetchProfile, loginOAuth, tokenFingerprint, isLoginCancelled } from './oauth.js';
5
6
  import { appendEventLog, setConsoleStdoutSuppressed } from './event-log.js';
@@ -27,6 +28,64 @@ const cyan = s => fg(36, s);
27
28
  const gray = s => fg(90, s);
28
29
  const dimUnderline = s => `${ESC}2;4m${s}${RESET}`;
29
30
 
31
+ // Terminal output budget (2026-11-12: the pool froze for up to 60s inside a synchronous
32
+ // TTY write() — a full-screen redraw per log line, 4.9 GB in 12h49m). Frames are
33
+ // coalesced to one per RENDER_MIN_INTERVAL_MS (≤10/s), an unchanged frame is re-sent
34
+ // only after REPAINT_MS, and nothing is written while the terminal has not drained.
35
+ const RENDER_MIN_INTERVAL_MS = 100;
36
+ const REPAINT_MS = 5000;
37
+ const EXIT_SEQ = `${ESC}?25h${ESC}?1049l`; // show cursor + leave the alt screen
38
+
39
+ /**
40
+ * Write to the terminal synchronously at PROCESS EXIT. The TUI turns its output
41
+ * non-blocking, and Node drops a non-blocking stream's queued bytes on exit — the exit
42
+ * sequence written through the stream never arrives and the shell is left in the
43
+ * dashboard (pre-mortem r2 B1, real-pty probe). Blocking back on + a direct fd write
44
+ * delivers it. Exit paths only: a blocking write is exactly what froze the pool.
45
+ */
46
+ export function writeTerminalSync(s) {
47
+ if (!isatty(1)) return;
48
+ try { process.stdout._handle?.setBlocking?.(true); } catch { /* not a libuv tty */ }
49
+ try { writeSync(1, s); } catch { /* terminal gone */ }
50
+ }
51
+
52
+ /**
53
+ * Re-enter raw mode after another process reset the terminal (post-update reassert).
54
+ * setRawMode is tcsetattr(TCSADRAIN): it blocks the WHOLE process until the terminal
55
+ * has read everything already sent (2026-11-12, measured 2.8s on a paused pty). So the
56
+ * toggle waits until our own queued output has drained — the remaining wait is the
57
+ * kernel's ~1 KB — and a terminal that is not reading at all gets it on 'drain'.
58
+ * Resolves true when reclaimed now, false when deferred to 'drain'.
59
+ */
60
+ export async function reclaimRawMode({ stdin = process.stdin, stdout = process.stdout, flush, timeoutMs = 1500 } = {}) {
61
+ if (!stdin.isTTY) return true;
62
+ // OFF then ON: Node short-circuits setRawMode(true) while it believes isRaw is already
63
+ // true, and the reset happened in ANOTHER process (verified on a real pty 2026-08-07).
64
+ const reclaim = () => {
65
+ try { stdin.setRawMode(false); stdin.setRawMode(true); stdin.resume(); } catch { /* terminal gone */ }
66
+ };
67
+ if (!flush || await flush({ timeoutMs })) { reclaim(); return true; }
68
+ stdout.once('drain', reclaim);
69
+ return false;
70
+ }
71
+
72
+ /**
73
+ * reclaimRawMode + the update-window measurement (release B, B11): says how long the new
74
+ * worker took to take the terminal back, or that it was deferred to the terminal's drain
75
+ * (and how long that took when it finally happened). `now`/`log` are injectable for tests.
76
+ */
77
+ export async function reclaimRawModeLogged({ stdout = process.stdout, log = m => console.log(`[Maxpool] ${m}`), now = Date.now, ...rest } = {}) {
78
+ const t0 = now();
79
+ const done = await reclaimRawMode({ stdout, ...rest });
80
+ if (done) {
81
+ log(`reload: terminal reclaimed in ${now() - t0}ms`);
82
+ } else {
83
+ log('reload: terminal reclaim deferred to drain');
84
+ stdout.once('drain', () => log(`reload: terminal reclaimed after drain in ${now() - t0}ms`));
85
+ }
86
+ return done;
87
+ }
88
+
30
89
  const ANSI_RE = /\x1b\[[0-9;]*m/g;
31
90
  const strip = s => s.replace(ANSI_RE, '');
32
91
  const vw = s => strip(s).length;
@@ -204,19 +263,24 @@ function countdown(ts) {
204
263
  * "how long each request takes" is not something the owner acts on. An idle
205
264
  * account renders NOTHING.
206
265
  *
207
- * 2 live · 17 req/hr · 1 failed
266
+ * 2 live · 17 req/hr · 1 failed · 3 link drops
208
267
  * 17 req/hr working, idle this second
209
268
  * (blank) resting — nothing to say
269
+ *
270
+ * "link drops" (2026-11-12): the link to a team pool gave up, not the account —
271
+ * yellow, never folded into "failed".
210
272
  */
211
273
  function loadText(load) {
212
274
  const inflight = load?.current?.inFlight || 0;
213
275
  const hourReq = load?.last1h?.requests || 0;
214
276
  const failed = load?.last15m?.failed || 0;
215
- if (!inflight && !hourReq && !failed) return '';
277
+ const linkFailed = load?.last15m?.linkFailed || 0;
278
+ if (!inflight && !hourReq && !failed && !linkFailed) return '';
216
279
  const parts = [];
217
280
  if (inflight) parts.push(`${inflight} live`);
218
281
  if (hourReq) parts.push(`${hourReq} req/hr`);
219
282
  if (failed) parts.push(red(`${failed} failed`));
283
+ if (linkFailed) parts.push(yellow(`${linkFailed} link drop${linkFailed === 1 ? '' : 's'}`));
220
284
  return parts.join(' · ');
221
285
  }
222
286
 
@@ -516,6 +580,18 @@ export class TUI {
516
580
  this.timer = null;
517
581
  this._origLog = null;
518
582
  this._origErr = null;
583
+ // Terminal output state (2026-11-12 freeze fix): _nb = stdout made non-blocking by
584
+ // start(); _renderTimer = the one scheduled coalesced render; _lastFrame /
585
+ // _lastFullWriteAt = what the terminal last got and when; _forceNext = the next
586
+ // frame must be sent even if unchanged; _drainArmed = one 'drain' listener pending.
587
+ this._nb = false;
588
+ this._renderTimer = null;
589
+ this._lastRenderAt = 0;
590
+ this._lastFrame = null;
591
+ this._lastFullWriteAt = 0;
592
+ this._forceNext = false;
593
+ this._drainArmed = false;
594
+ this._onDrain = () => { this._drainArmed = false; if (this.running) this.render(); };
519
595
  }
520
596
 
521
597
  // ── lifecycle ──────────────────────────────────────
@@ -529,11 +605,19 @@ export class TUI {
529
605
  }
530
606
 
531
607
  this.running = true;
608
+ // Never block the pool on its own terminal (2026-11-12): Node writes a TTY
609
+ // synchronously, so a terminal that fell behind stopped EVERY request for up to 60s.
610
+ // Real TTYs only (r2 S3); a later stop() leaves it non-blocking (the exit path alone
611
+ // turns blocking back on — writeTerminalSync).
612
+ if (!this._nb && isatty(process.stdout.fd) && typeof process.stdout._handle?.setBlocking === 'function') {
613
+ process.stdout._handle.setBlocking(false);
614
+ this._nb = true;
615
+ }
532
616
  process.stdout.write(`${ESC}?1049h${ESC}?25l`);
533
617
  process.stdin.resume();
534
618
  process.stdin.setEncoding('utf8');
535
619
  this._dataHandler = d => this._onData(d);
536
- this._resizeHandler = () => this.render();
620
+ this._resizeHandler = () => this.forceRender();
537
621
  process.stdin.on('data', this._dataHandler);
538
622
  process.stdout.on('resize', this._resizeHandler);
539
623
 
@@ -543,30 +627,84 @@ export class TUI {
543
627
  console.log = (...a) => this._addLog(a.join(' '));
544
628
  console.error = (...a) => this._addLog(a.join(' '));
545
629
 
546
- this.render();
630
+ this.forceRender();
547
631
  this.timer = setInterval(() => {
548
632
  this.frame = (this.frame + 1) % SPINNER.length;
549
- this.render();
633
+ this.requestRender();
550
634
  }, 500);
551
635
  return true;
552
636
  }
553
637
 
554
- stop() {
638
+ /** Stop drawing frames (no more renders, no drain wake-up) and stop taking keys, so
639
+ * nothing typed during a hand-off wait acts; the screen stays as is. */
640
+ _haltRendering() {
555
641
  this.running = false;
642
+ if (this._dataHandler) process.stdin.removeListener('data', this._dataHandler);
556
643
  if (this.timer) { clearInterval(this.timer); this.timer = null; }
644
+ if (this._renderTimer) { clearTimeout(this._renderTimer); this._renderTimer = null; }
645
+ if (this._drainArmed) { process.stdout.removeListener('drain', this._onDrain); this._drainArmed = false; }
646
+ }
647
+
648
+ /** `exiting`: called from the process-exit path — the exit sequence must be written
649
+ * synchronously or it is lost with the non-blocking stream's queue (r2 B1).
650
+ * `keepRaw`: leave the terminal's raw mode as is. Node switches it with
651
+ * tcsetattr(TCSADRAIN), which BLOCKS the whole process until the terminal has read
652
+ * everything already sent (measured: 2.8s for a terminal that read nothing for 3s) —
653
+ * so a process that keeps serving after stop() must not switch it while the
654
+ * terminal may be behind. */
655
+ stop({ exiting = false, keepRaw = false } = {}) {
656
+ this._haltRendering();
557
657
  if (this._origLog) { console.log = this._origLog; console.error = this._origErr; }
558
658
  process.stdin.removeListener('data', this._dataHandler);
559
659
  process.stdout.removeListener('resize', this._resizeHandler);
560
- process.stdout.write(`${ESC}?25h${ESC}?1049l`);
561
- try { process.stdin.setRawMode(false); } catch {}
660
+ if (exiting) writeTerminalSync(EXIT_SEQ);
661
+ else process.stdout.write(EXIT_SEQ);
662
+ if (!keepRaw) { try { process.stdin.setRawMode(false); } catch {} }
562
663
  process.stdin.pause();
563
664
  }
564
665
 
666
+ /** Give the terminal up to someone else (a self-update's new worker, a login prompt)
667
+ * while this process keeps running (r2 B2): let the frames a slow terminal is still
668
+ * taking land, then the exit sequence, each bounded by timeoutMs — so the next
669
+ * writer never interleaves with our queue, and blocking is never turned back on. */
670
+ async handOff({ timeoutMs = 1500, flushing = null, keepRaw = false } = {}) {
671
+ await (flushing || this.beginHandOff({ timeoutMs }));
672
+ this.stop({ keepRaw });
673
+ await this.flushOutput({ timeoutMs });
674
+ }
675
+
676
+ /** First half of handOff, started early so the terminal catches up while the caller
677
+ * does other work: stop drawing and taking keys, and return the flush (pass it to
678
+ * handOff as `flushing`). Gate on that promise — `running` is already false. */
679
+ beginHandOff({ timeoutMs = 1500 } = {}) {
680
+ this._haltRendering();
681
+ return this.flushOutput({ timeoutMs });
682
+ }
683
+
684
+ /** Resolve once the terminal has taken everything queued for it: true when drained,
685
+ * false at timeoutMs. 'drain' fires only after a write hit the high-water mark, so
686
+ * the queue length is polled too. */
687
+ flushOutput({ timeoutMs = 1500 } = {}) {
688
+ const out = process.stdout;
689
+ if (!out.writableLength) return Promise.resolve(true);
690
+ return new Promise(resolve => {
691
+ const finish = drained => {
692
+ clearTimeout(deadline); clearInterval(poll);
693
+ out.removeListener('drain', onDrain);
694
+ resolve(drained);
695
+ };
696
+ const onDrain = () => { if (!out.writableLength) finish(true); };
697
+ const deadline = setTimeout(() => finish(false), timeoutMs);
698
+ const poll = setInterval(onDrain, 25);
699
+ out.on('drain', onDrain);
700
+ });
701
+ }
702
+
565
703
  // ── server hooks ───────────────────────────────────
566
704
 
567
705
  onRequestStart(id, info) {
568
706
  this.active.set(id, { ...info, t: timestamp(), started: Date.now(), account: null, caller: info.caller || null, conversation: info.conversation || null });
569
- this.render();
707
+ this.requestRender();
570
708
  }
571
709
 
572
710
  onRequestRouted(id, info) {
@@ -614,7 +752,9 @@ export class TUI {
614
752
  ttfbMs = r.firstByteAt - r.started;
615
753
  }
616
754
  const ttfbTag = ttfbMs != null ? ` ttfb=${(Math.max(0, ttfbMs) / 1000).toFixed(1)}s` : '';
617
- this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${provTag}${cvTag}${ttfbTag}`);
755
+ // B8: ' ns' = a non-streaming request (strict false: the oauth relay carries no shape, so no tag).
756
+ const nsTag = info.stream === false ? ' ns' : '';
757
+ this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${nsTag}${provTag}${cvTag}${ttfbTag}`);
618
758
  }
619
759
 
620
760
  _addLog(msg) {
@@ -624,7 +764,8 @@ export class TUI {
624
764
  appendEventLog(msg);
625
765
  this.log.unshift({ t: timestamp(), msg });
626
766
  if (this.log.length > 200) this.log.length = 200;
627
- if (this.running) this.render();
767
+ // Scheduled, not drawn: a burst of N lines is ONE frame (was N full screens).
768
+ this.requestRender();
628
769
  }
629
770
 
630
771
  // ── input handling ─────────────────────────────────
@@ -660,7 +801,7 @@ export class TUI {
660
801
  .split('')
661
802
  .filter(c => c >= ' ' && c !== '\x7f')
662
803
  .join('');
663
- if (typed) { this.inputBuf += typed; this.render(); }
804
+ if (typed) { this.inputBuf += typed; this.forceRender(); }
664
805
  if (nlIdx !== -1) { this._pendingPaste = ''; return this._key('enter'); }
665
806
  return;
666
807
  }
@@ -684,7 +825,7 @@ export class TUI {
684
825
  case 'input': this._keyInput(k); break;
685
826
  case 'confirm': this._keyConfirm(k); break;
686
827
  }
687
- this.render();
828
+ this.forceRender();
688
829
  }
689
830
 
690
831
  _keyNormal(k) {
@@ -756,14 +897,14 @@ export class TUI {
756
897
  if (!this.checkNow) { this._addLog('Update check unavailable on this worker'); return; }
757
898
  if (this.updateBusy) return; // already running
758
899
  this.updateBusy = 'Checking npm…';
759
- this.render();
900
+ this.forceRender();
760
901
  Promise.resolve(this.checkNow())
761
902
  .catch(() => {})
762
903
  .finally(() => {
763
904
  // If an update was found+applied, the seamless reload replaces this worker and
764
905
  // this never renders. Otherwise report the outcome in place.
765
906
  this.updateBusy = null;
766
- if (this.mode === 'updates') this.render();
907
+ if (this.mode === 'updates') this.forceRender();
767
908
  });
768
909
  } else if (k === 't') {
769
910
  this._toggleAutoUpdate();
@@ -1000,7 +1141,7 @@ export class TUI {
1000
1141
  const looksLikeSecretName = /^[A-Z][A-Z0-9_-]{2,60}$/.test(input) && !input.includes('.');
1001
1142
  if (!looksLikeSecretName) return input; // a pasted key
1002
1143
  this._addLog(`Resolving GCP secret "${input}"…`);
1003
- this.render();
1144
+ this.forceRender();
1004
1145
  try {
1005
1146
  const { resolveSecret } = await import('../secret-resolver.js');
1006
1147
  const v = await resolveSecret(input);
@@ -1102,7 +1243,7 @@ export class TUI {
1102
1243
  this.mode = 'accounts';
1103
1244
  if (secretName) this._addLog(`Resolving secret "${secretName}" from GCP…`);
1104
1245
  else this._addLog(`Adding "${name}" with direct API key…`);
1105
- this.render();
1246
+ this.forceRender();
1106
1247
  try {
1107
1248
  let token = null;
1108
1249
  if (secretName) {
@@ -1504,14 +1645,16 @@ export class TUI {
1504
1645
  // it's a RE-AUTH in place (revives a dead-refresh/reauth account), not a duplicate.
1505
1646
  async _doLogin() {
1506
1647
  const wasRunning = this.running;
1507
- if (wasRunning) this.stop();
1508
1648
  // Mute the console-log MIRROR to stdout during the interactive prompt so routing
1509
1649
  // logs (console.log, e.g. the glm-fallback 429 churn) don't flood over the readline
1510
1650
  // "Name this account" prompt. The prompts + errors use process.stdout.write, which
1511
1651
  // BYPASSES the mirror, so they stay visible. Login runs only on the terminal-owning
1512
- // primary (mirror baseline false), so the finally restores it to false.
1652
+ // primary (mirror baseline false), so the finally restores it to false. Muted BEFORE
1653
+ // the hand-off: its stop() hands console.log back to the mirror while the second
1654
+ // flush still waits, and those lines would print onto the user's normal screen.
1513
1655
  setConsoleStdoutSuppressed(true);
1514
1656
  try {
1657
+ if (wasRunning) await this.handOff(); // r2 B2: bounded flush + exit sequence before the prompt
1515
1658
  process.stdout.write('\nOpening browser to log into Claude…\n');
1516
1659
  const creds = await loginOAuth();
1517
1660
  const profile = await fetchProfile(creds.accessToken);
@@ -1997,9 +2140,30 @@ export class TUI {
1997
2140
 
1998
2141
  // ── rendering ──────────────────────────────────────
1999
2142
 
2143
+ /** Draw on the next tick at the latest — N calls inside one tick draw ONE frame
2144
+ * (2026-11-12: every log line used to redraw the full screen synchronously and the
2145
+ * pool froze on a terminal that fell behind). */
2146
+ requestRender() {
2147
+ if (!this.running || this._renderTimer) return;
2148
+ const wait = Math.max(0, RENDER_MIN_INTERVAL_MS - (Date.now() - this._lastRenderAt));
2149
+ this._renderTimer = setTimeout(() => {
2150
+ this._renderTimer = null;
2151
+ this.render();
2152
+ }, wait);
2153
+ this._renderTimer.unref?.();
2154
+ }
2155
+
2156
+ /** Draw NOW (keystroke, resize, takeover): the user is waiting on this frame. */
2157
+ forceRender() {
2158
+ if (!this.running) return;
2159
+ if (this._renderTimer) { clearTimeout(this._renderTimer); this._renderTimer = null; }
2160
+ this._forceNext = true;
2161
+ this.render();
2162
+ }
2163
+
2000
2164
  render() {
2001
2165
  if (!this.running) return;
2002
- // Guard against re-entry: clearing an expired quota logs, and _addLog calls
2166
+ // Guard against re-entry: clearing an expired quota logs, and _addLog schedules
2003
2167
  // render() again — without this the nested call would render twice.
2004
2168
  if (this._rendering) return;
2005
2169
  this._rendering = true;
@@ -2018,7 +2182,7 @@ export class TUI {
2018
2182
  const H = process.stdout.rows || 24;
2019
2183
 
2020
2184
  if (W < 40 || H < 8) {
2021
- process.stdout.write(`${ESC}H${ESC}2JTerminal too small (need 40x8+)\r\n`);
2185
+ this._writeScreen(`${ESC}H${ESC}2JTerminal too small (need 40x8+)\r\n`);
2022
2186
  return;
2023
2187
  }
2024
2188
 
@@ -2288,15 +2452,33 @@ export class TUI {
2288
2452
  }
2289
2453
 
2290
2454
  /** Full-screen buffer write: cursor home, H rows fitted to W, then hide the cursor
2291
- * (shown only in input mode). */
2455
+ * (shown only in input mode). The ONLY frame writer: it coalesces (an unchanged
2456
+ * frame is skipped unless forced / REPAINT_MS-old) and never writes into a terminal
2457
+ * that has not drained the last frame — it owes one render on 'drain' instead
2458
+ * (2026-11-12 freeze fix). Memory stays bounded: the stream's own high-water mark
2459
+ * stops us, and at most one frame is owed. */
2292
2460
  _writeScreen(lines, W, H) {
2293
- let buf = `${ESC}H`;
2294
- for (let i = 0; i < H; i++) {
2295
- buf += fitLine(lines[i] || '', W);
2296
- if (i < H - 1) buf += '\r\n';
2297
- }
2298
- // Show cursor only in input mode
2299
- buf += this.mode === 'input' ? `${ESC}?25h` : `${ESC}?25l`;
2461
+ let buf;
2462
+ if (typeof lines === 'string') {
2463
+ buf = lines; // pre-built screen (e.g. "Terminal too small")
2464
+ } else {
2465
+ buf = `${ESC}H`;
2466
+ for (let i = 0; i < H; i++) {
2467
+ buf += fitLine(lines[i] || '', W);
2468
+ if (i < H - 1) buf += '\r\n';
2469
+ }
2470
+ // Show cursor only in input mode
2471
+ buf += this.mode === 'input' ? `${ESC}?25h` : `${ESC}?25l`;
2472
+ }
2473
+ this._lastRenderAt = Date.now();
2474
+ if (!this._forceNext && this._lastFrame === buf && Date.now() - this._lastFullWriteAt < REPAINT_MS) return;
2475
+ if (this._nb && process.stdout.writableNeedDrain) {
2476
+ if (!this._drainArmed) { this._drainArmed = true; process.stdout.once('drain', this._onDrain); }
2477
+ return; // frame owed: written once, on 'drain'
2478
+ }
2479
+ this._forceNext = false;
2480
+ this._lastFrame = buf;
2481
+ this._lastFullWriteAt = Date.now();
2300
2482
  process.stdout.write(buf);
2301
2483
  }
2302
2484