maxpool 1.24.92 → 1.24.94
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 +155 -4
- package/src/claude-reset-grants.js +8 -0
- package/src/event-log.js +79 -1
- package/src/index.js +325 -149
- package/src/prober.js +51 -9
- package/src/reload-protocol.js +86 -2
- package/src/reset-policy.js +94 -9
- package/src/terminal-mode.js +124 -0
- package/src/tui.js +106 -15
package/src/tui.js
CHANGED
|
@@ -56,19 +56,67 @@ 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
|
+
|
|
72
120
|
/**
|
|
73
121
|
* reclaimRawMode + the update-window measurement (release B, B11): says how long the new
|
|
74
122
|
* worker took to take the terminal back, or that it was deferred to the terminal's drain
|
|
@@ -76,13 +124,12 @@ export async function reclaimRawMode({ stdin = process.stdin, stdout = process.s
|
|
|
76
124
|
*/
|
|
77
125
|
export async function reclaimRawModeLogged({ stdout = process.stdout, log = m => console.log(`[Maxpool] ${m}`), now = Date.now, ...rest } = {}) {
|
|
78
126
|
const t0 = now();
|
|
79
|
-
const done = await reclaimRawMode({
|
|
80
|
-
|
|
81
|
-
log(`reload: terminal reclaimed in ${now() - t0}ms`)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
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');
|
|
86
133
|
return done;
|
|
87
134
|
}
|
|
88
135
|
|
|
@@ -596,12 +643,28 @@ export class TUI {
|
|
|
596
643
|
|
|
597
644
|
// ── lifecycle ──────────────────────────────────────
|
|
598
645
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|
+
}
|
|
605
668
|
}
|
|
606
669
|
|
|
607
670
|
this.running = true;
|
|
@@ -632,6 +695,34 @@ export class TUI {
|
|
|
632
695
|
this.frame = (this.frame + 1) % SPINNER.length;
|
|
633
696
|
this.requestRender();
|
|
634
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
|
+
}
|
|
635
726
|
return true;
|
|
636
727
|
}
|
|
637
728
|
|