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 +1 -1
- package/src/event-log.js +79 -1
- package/src/index.js +332 -149
- package/src/reload-protocol.js +92 -2
- package/src/server.js +353 -27
- package/src/terminal-mode.js +124 -0
- package/src/tui.js +119 -9
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Switch the terminal's raw mode WITHOUT stalling the thread that serves requests.
|
|
2
|
+
//
|
|
3
|
+
// Node switches it with tcsetattr(TCSADRAIN), which waits until the terminal has read
|
|
4
|
+
// everything already queued for it — up to the pty's ~1 KB kernel queue, which no Node
|
|
5
|
+
// API can see. A worker that has just taken over the pool cannot know whether its
|
|
6
|
+
// terminal is reading (red-team 2026-11-13: a dashboard frame under 1 KB, or a terminal
|
|
7
|
+
// that reads in bursts, stalled the new sole acceptor for ~1.7s inside setRawMode).
|
|
8
|
+
// So that switch runs in a helper PROCESS with its own handle on the terminal (our
|
|
9
|
+
// stdin, else /dev/tty): the helper waits for the terminal, the pool keeps serving.
|
|
10
|
+
// termios belongs to the terminal device, so a mode set from the helper is the mode
|
|
11
|
+
// every reader of the terminal sees (verified on a real pty, 2026-11-13).
|
|
12
|
+
//
|
|
13
|
+
// A process, not a worker thread (red-team round 2, 2026-11-13): process.exit JOINS
|
|
14
|
+
// worker threads, a thread inside tcsetattr cannot be interrupted (Worker.terminate()
|
|
15
|
+
// included — libuv retries the call on EINTR), so a worker with a switch pending lived
|
|
16
|
+
// until the terminal read: on the abrupt-restart path it kept the port. A child process
|
|
17
|
+
// is never joined, and SIGKILL ends it mid-wait without applying the pending mode.
|
|
18
|
+
// The helper only ever dies by SIGKILL: a normal exit would run Node's terminal reset
|
|
19
|
+
// and put the terminal back in the mode it found before its first switch.
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
21
|
+
|
|
22
|
+
const HELPER_SRC = `
|
|
23
|
+
const fs = require('node:fs');
|
|
24
|
+
const tty = require('node:tty');
|
|
25
|
+
const die = () => process.kill(process.pid, 'SIGKILL');
|
|
26
|
+
process.on('disconnect', die); // our parent is gone: never apply a stale mode
|
|
27
|
+
process.on('uncaughtException', die);
|
|
28
|
+
let term = null;
|
|
29
|
+
// fd 3 is the parent's stdin (the terminal the dashboard takes keys from — it need not
|
|
30
|
+
// be the controlling terminal), else the controlling terminal.
|
|
31
|
+
function openTerminal() {
|
|
32
|
+
if (tty.isatty(3)) return new tty.ReadStream(3);
|
|
33
|
+
let fd = null;
|
|
34
|
+
try {
|
|
35
|
+
fd = fs.openSync('/dev/tty', fs.constants.O_RDWR | fs.constants.O_NOCTTY);
|
|
36
|
+
if (tty.isatty(fd)) return new tty.ReadStream(fd);
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if (fd != null) { try { fs.closeSync(fd); } catch {} }
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
try { fs.closeSync(fd); } catch {}
|
|
42
|
+
throw Object.assign(new Error('/dev/tty is not a terminal'), { code: 'ENOTTY' });
|
|
43
|
+
}
|
|
44
|
+
process.on('message', ({ id, raw, toggle }) => {
|
|
45
|
+
try {
|
|
46
|
+
// Never read from it (no resume): the parent owns the keyboard.
|
|
47
|
+
if (!term) term = openTerminal();
|
|
48
|
+
// libuv skips a switch to the mode it believes is already set; another process
|
|
49
|
+
// may have changed the terminal since, so 'toggle' goes through the other mode.
|
|
50
|
+
if (toggle) term.setRawMode(!raw);
|
|
51
|
+
term.setRawMode(raw);
|
|
52
|
+
process.send({ id, ok: true });
|
|
53
|
+
} catch (err) {
|
|
54
|
+
process.send({ id, ok: false, error: err.code || err.message });
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
const spawnHelper = () => spawn(process.execPath, ['-e', HELPER_SRC], {
|
|
60
|
+
// fd 3 = our stdin; nothing on 0-2, so the helper's own exit never touches them.
|
|
61
|
+
stdio: ['ignore', 'ignore', 'ignore', 0, 'ipc'],
|
|
62
|
+
env: { PATH: process.env.PATH || '' },
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `setRaw(raw, { toggle })` resolves {ok, error?, ms} once the terminal is in that mode
|
|
67
|
+
* — which may be long after the call while the terminal is not reading; the caller's
|
|
68
|
+
* thread is never blocked. ok:false (no terminal, helper failed) means the caller must
|
|
69
|
+
* fall back to its own switch. The helper starts on demand and is killed once it has
|
|
70
|
+
* nothing pending; close() kills it at once — a pending switch then never lands — and
|
|
71
|
+
* so does this process's exit. `makeHelper` is injectable for tests.
|
|
72
|
+
*/
|
|
73
|
+
export function createTerminalModeSwitcher({ makeHelper = spawnHelper, now = Date.now } = {}) {
|
|
74
|
+
let helper = null;
|
|
75
|
+
let seq = 0;
|
|
76
|
+
const pending = new Map();
|
|
77
|
+
const failAll = error => {
|
|
78
|
+
for (const done of pending.values()) done({ ok: false, error });
|
|
79
|
+
pending.clear();
|
|
80
|
+
};
|
|
81
|
+
const kill = h => { try { h?.kill?.('SIGKILL'); } catch { /* already gone */ } };
|
|
82
|
+
const onProcessExit = () => kill(helper);
|
|
83
|
+
let exitHooked = false;
|
|
84
|
+
const ensure = () => {
|
|
85
|
+
if (helper) return helper;
|
|
86
|
+
const h = makeHelper();
|
|
87
|
+
h.unref?.(); // never keeps this process alive
|
|
88
|
+
h.channel?.unref?.();
|
|
89
|
+
h.on('message', ({ id, ok, error }) => {
|
|
90
|
+
const done = pending.get(id);
|
|
91
|
+
if (!done) return;
|
|
92
|
+
pending.delete(id);
|
|
93
|
+
done({ ok, error });
|
|
94
|
+
// Idle: nothing left for it to switch. No helper process lingers between switches.
|
|
95
|
+
if (!pending.size && helper === h) { helper = null; kill(h); }
|
|
96
|
+
});
|
|
97
|
+
h.on('error', err => { if (helper === h) { helper = null; failAll(err.message); } kill(h); });
|
|
98
|
+
h.on('exit', () => { if (helper === h) { helper = null; failAll('terminal helper exited'); } });
|
|
99
|
+
if (!exitHooked) { process.on('exit', onProcessExit); exitHooked = true; }
|
|
100
|
+
helper = h;
|
|
101
|
+
return h;
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
setRaw(raw, { toggle = false } = {}) {
|
|
105
|
+
const t0 = now();
|
|
106
|
+
return new Promise(resolve => {
|
|
107
|
+
const done = r => resolve({ ...r, ms: now() - t0 });
|
|
108
|
+
let h;
|
|
109
|
+
try { h = ensure(); } catch (err) { done({ ok: false, error: err.message }); return; }
|
|
110
|
+
const id = ++seq;
|
|
111
|
+
pending.set(id, done);
|
|
112
|
+
try { h.send({ id, raw: Boolean(raw), toggle: Boolean(toggle) }); } catch (err) { pending.delete(id); done({ ok: false, error: err.message }); }
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
/** Kill the helper NOW (exit paths call this first): a pending switch never lands. */
|
|
116
|
+
close() {
|
|
117
|
+
const h = helper;
|
|
118
|
+
helper = null;
|
|
119
|
+
failAll('closed');
|
|
120
|
+
kill(h);
|
|
121
|
+
if (exitHooked) { process.removeListener('exit', onProcessExit); exitHooked = false; }
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
package/src/tui.js
CHANGED
|
@@ -56,19 +56,83 @@ export function writeTerminalSync(s) {
|
|
|
56
56
|
* toggle waits until our own queued output has drained — the remaining wait is the
|
|
57
57
|
* kernel's ~1 KB — and a terminal that is not reading at all gets it on 'drain'.
|
|
58
58
|
* Resolves true when reclaimed now, false when deferred to 'drain'.
|
|
59
|
+
* 'drain' is emitted only after a write crossed the stream's high-water mark, so a
|
|
60
|
+
* deferred reclaim also polls the queue length and goes ahead once it is empty
|
|
61
|
+
* (2026-11-13: a frame below 16 KB never produced 'drain'). Fallback only: a worker
|
|
62
|
+
* with a terminal helper (terminal-mode.js) never switches on its own thread. `guard` is re-checked
|
|
63
|
+
* at reclaim time (a TUI stopped meanwhile must not be put back in raw mode);
|
|
64
|
+
* `onDeferredReclaim` runs once the deferred reclaim actually happened.
|
|
59
65
|
*/
|
|
60
|
-
export async function reclaimRawMode({
|
|
66
|
+
export async function reclaimRawMode({
|
|
67
|
+
stdin = process.stdin, stdout = process.stdout, flush, timeoutMs = 1500,
|
|
68
|
+
guard = () => true, onDeferredReclaim = () => {}, pollMs = 100,
|
|
69
|
+
} = {}) {
|
|
61
70
|
if (!stdin.isTTY) return true;
|
|
62
71
|
// OFF then ON: Node short-circuits setRawMode(true) while it believes isRaw is already
|
|
63
72
|
// true, and the reset happened in ANOTHER process (verified on a real pty 2026-08-07).
|
|
64
73
|
const reclaim = () => {
|
|
74
|
+
if (!guard()) return;
|
|
65
75
|
try { stdin.setRawMode(false); stdin.setRawMode(true); stdin.resume(); } catch { /* terminal gone */ }
|
|
66
76
|
};
|
|
67
77
|
if (!flush || await flush({ timeoutMs })) { reclaim(); return true; }
|
|
68
|
-
|
|
78
|
+
let settled = false;
|
|
79
|
+
const later = () => {
|
|
80
|
+
if (settled) return;
|
|
81
|
+
settled = true;
|
|
82
|
+
clearInterval(poll);
|
|
83
|
+
stdout.removeListener?.('drain', later);
|
|
84
|
+
reclaim();
|
|
85
|
+
try { onDeferredReclaim(); } catch { /* never */ }
|
|
86
|
+
};
|
|
87
|
+
// Only once our queue is EMPTY (polled, or 'drain'): a mere shrink — one whole write
|
|
88
|
+
// landed — fired the switch on the serving thread while a bursty terminal still held
|
|
89
|
+
// several bursts of ours, stalling the pool for each (red-team round 2). Our queue
|
|
90
|
+
// cannot grow without bound meanwhile: the dashboard stops writing at the high-water
|
|
91
|
+
// mark and owes one frame on 'drain', so a busy dashboard still reaches 'drain'.
|
|
92
|
+
const poll = setInterval(() => { if (stdout.writableLength === 0) later(); }, pollMs);
|
|
93
|
+
poll.unref?.();
|
|
94
|
+
stdout.once('drain', later);
|
|
69
95
|
return false;
|
|
70
96
|
}
|
|
71
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Call `cb` once, as soon as the terminal has taken everything queued for it (our
|
|
100
|
+
* write queue is empty) — polled, since 'drain' fires only after a write crossed the
|
|
101
|
+
* high-water mark. Returns a cancel function.
|
|
102
|
+
*/
|
|
103
|
+
export function whenTerminalCaughtUp(cb, { stdout = process.stdout, pollMs = 100 } = {}) {
|
|
104
|
+
let done = false;
|
|
105
|
+
const finish = () => {
|
|
106
|
+
if (done) return;
|
|
107
|
+
done = true;
|
|
108
|
+
clearInterval(poll);
|
|
109
|
+
stdout.removeListener?.('drain', onDrain);
|
|
110
|
+
cb();
|
|
111
|
+
};
|
|
112
|
+
const onDrain = () => { if (!stdout.writableLength) finish(); };
|
|
113
|
+
const poll = setInterval(onDrain, pollMs);
|
|
114
|
+
poll.unref?.();
|
|
115
|
+
stdout.on?.('drain', onDrain);
|
|
116
|
+
if (!stdout.writableLength) Promise.resolve().then(finish);
|
|
117
|
+
return () => { if (done) return; done = true; clearInterval(poll); stdout.removeListener?.('drain', onDrain); };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* reclaimRawMode + the update-window measurement (release B, B11): says how long the new
|
|
122
|
+
* worker took to take the terminal back, or that it was deferred to the terminal's drain
|
|
123
|
+
* (and how long that took when it finally happened). `now`/`log` are injectable for tests.
|
|
124
|
+
*/
|
|
125
|
+
export async function reclaimRawModeLogged({ stdout = process.stdout, log = m => console.log(`[Maxpool] ${m}`), now = Date.now, ...rest } = {}) {
|
|
126
|
+
const t0 = now();
|
|
127
|
+
const done = await reclaimRawMode({
|
|
128
|
+
stdout, ...rest,
|
|
129
|
+
onDeferredReclaim: () => log(`reload: terminal reclaimed after drain in ${now() - t0}ms`),
|
|
130
|
+
});
|
|
131
|
+
if (done) log(`reload: terminal reclaimed in ${now() - t0}ms`);
|
|
132
|
+
else log('reload: terminal reclaim deferred to drain');
|
|
133
|
+
return done;
|
|
134
|
+
}
|
|
135
|
+
|
|
72
136
|
const ANSI_RE = /\x1b\[[0-9;]*m/g;
|
|
73
137
|
const strip = s => s.replace(ANSI_RE, '');
|
|
74
138
|
const vw = s => strip(s).length;
|
|
@@ -579,12 +643,28 @@ export class TUI {
|
|
|
579
643
|
|
|
580
644
|
// ── lifecycle ──────────────────────────────────────
|
|
581
645
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
646
|
+
/** `rawWhenDrained` (a worker that is already serving — reload takeover, a respawn):
|
|
647
|
+
* enter raw mode only once the terminal has taken our first frame. setRawMode is
|
|
648
|
+
* tcsetattr(TCSADRAIN) and blocks the WHOLE process until the terminal has read
|
|
649
|
+
* everything queued; on a terminal that is not reading that is forever, and the port
|
|
650
|
+
* goes dead with it (2026-11-13). So the dashboard is drawn non-blocking first and the
|
|
651
|
+
* mode switch waits for the terminal (reclaimRawMode) — the keyboard, not the serving,
|
|
652
|
+
* is what a stalled terminal delays. A terminal that took the frame at once gets raw
|
|
653
|
+
* mode synchronously, so a broken one still falls back to plain logs (returns false). */
|
|
654
|
+
start({ rawWhenDrained = false, modeSwitcher = null } = {}) {
|
|
655
|
+
const rawError = err => `[Maxpool] TUI unavailable (${err.code || err.message}); continuing with plain logs.\n`;
|
|
656
|
+
if (rawWhenDrained) {
|
|
657
|
+
if (!process.stdin.isTTY || typeof process.stdin.setRawMode !== 'function') {
|
|
658
|
+
process.stderr.write(rawError({ message: 'stdin is not a terminal' }));
|
|
659
|
+
return false;
|
|
660
|
+
}
|
|
661
|
+
} else {
|
|
662
|
+
try {
|
|
663
|
+
process.stdin.setRawMode(true);
|
|
664
|
+
} catch (err) {
|
|
665
|
+
process.stderr.write(rawError(err));
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
588
668
|
}
|
|
589
669
|
|
|
590
670
|
this.running = true;
|
|
@@ -615,6 +695,34 @@ export class TUI {
|
|
|
615
695
|
this.frame = (this.frame + 1) % SPINNER.length;
|
|
616
696
|
this.requestRender();
|
|
617
697
|
}, 500);
|
|
698
|
+
if (rawWhenDrained && modeSwitcher) {
|
|
699
|
+
// Off this thread (terminal-mode.js): the pool keeps serving however long the
|
|
700
|
+
// terminal takes to read. Without a helper (no controlling terminal) fall back.
|
|
701
|
+
modeSwitcher.setRaw(true).then(r => {
|
|
702
|
+
if (r.ok) {
|
|
703
|
+
if (r.ms >= 100) appendEventLog(`[Maxpool] TUI: keyboard ready after the terminal caught up (${r.ms}ms)`);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
if (!this.running) return;
|
|
707
|
+
appendEventLog(`[Maxpool] TUI: terminal helper unavailable (${r.error}); keyboard waits for the terminal to read`);
|
|
708
|
+
reclaimRawMode({ flush: o => this.flushOutput(o), guard: () => this.running }).catch(() => {});
|
|
709
|
+
}).catch(() => {});
|
|
710
|
+
} else if (rawWhenDrained) {
|
|
711
|
+
if (!process.stdout.writableLength) {
|
|
712
|
+
// The terminal took the whole frame: it is reading, so the drain wait is short.
|
|
713
|
+
try {
|
|
714
|
+
process.stdin.setRawMode(true);
|
|
715
|
+
} catch (err) {
|
|
716
|
+
this.stop({ keepRaw: true });
|
|
717
|
+
console.error(rawError(err).trim());
|
|
718
|
+
return false;
|
|
719
|
+
}
|
|
720
|
+
} else {
|
|
721
|
+
reclaimRawMode({ flush: o => this.flushOutput(o), guard: () => this.running })
|
|
722
|
+
.then(now => { if (!now) appendEventLog('[Maxpool] TUI: keyboard waits for the terminal to read (raw mode deferred to drain)'); })
|
|
723
|
+
.catch(() => {});
|
|
724
|
+
}
|
|
725
|
+
}
|
|
618
726
|
return true;
|
|
619
727
|
}
|
|
620
728
|
|
|
@@ -735,7 +843,9 @@ export class TUI {
|
|
|
735
843
|
ttfbMs = r.firstByteAt - r.started;
|
|
736
844
|
}
|
|
737
845
|
const ttfbTag = ttfbMs != null ? ` ttfb=${(Math.max(0, ttfbMs) / 1000).toFixed(1)}s` : '';
|
|
738
|
-
|
|
846
|
+
// B8: ' ns' = a non-streaming request (strict false: the oauth relay carries no shape, so no tag).
|
|
847
|
+
const nsTag = info.stream === false ? ' ns' : '';
|
|
848
|
+
this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${nsTag}${provTag}${cvTag}${ttfbTag}`);
|
|
739
849
|
}
|
|
740
850
|
|
|
741
851
|
_addLog(msg) {
|