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/package.json
CHANGED
package/src/account-manager.js
CHANGED
|
@@ -4,11 +4,11 @@ import { appendEventLog } from './event-log.js';
|
|
|
4
4
|
import { hourFloor } from './outside-usage.js';
|
|
5
5
|
import { CapacityLedger, TANK_MIN_UTIL, mergeCycles, tankStatsFromCycles } from './capacity-ledger.js';
|
|
6
6
|
// A1: the ONE shared grant-scope answer (display, routing, claim and lift all read it).
|
|
7
|
-
import { appliesTo, forgetClaimOrg } from './claude-reset-grants.js';
|
|
8
|
-
import { POLICY, CLAUDE_WALL_WEEKLY, probeSentAt } from './reset-policy.js';
|
|
7
|
+
import { appliesTo, clearsGeneralWeekly, forgetClaimOrg } from './claude-reset-grants.js';
|
|
8
|
+
import { POLICY, CLAUDE_WALL_WEEKLY, probeSentAt, wallClaimHeld, HOLD_POOL_HAS_ROOM } from './reset-policy.js';
|
|
9
9
|
import {
|
|
10
10
|
LISTING_FRESH_MS, LIFT_GRACE_MS, CLAIM_STALE_LISTING_MAX_MS, CLAIM_BACKOFF_STEPS_MS,
|
|
11
|
-
CLAIM_BACKOFF_CAP_MS, REACTIVE_CLAIM_TIMEOUT_MS,
|
|
11
|
+
CLAIM_BACKOFF_CAP_MS, REACTIVE_CLAIM_TIMEOUT_MS, CLAIM_FRESH_MS,
|
|
12
12
|
} from './reset-timing.js';
|
|
13
13
|
|
|
14
14
|
// Review r2 A: consecutive request-scoped faults before they count against the account.
|
|
@@ -3449,6 +3449,110 @@ export class AccountManager {
|
|
|
3449
3449
|
* - ramp: ease a just-recovered account back in instead of slamming it
|
|
3450
3450
|
* - failures: direct per-account backoff after errors
|
|
3451
3451
|
*/
|
|
3452
|
+
/** R7 (owner, 2026-11-13 — task-2026-11-13-maxpool-hold-resets-when-pool-has-room):
|
|
3453
|
+
* how many OTHER accounts in THIS pool have room to absorb the load while the
|
|
3454
|
+
* GENERAL weekly wall claim waits for the account's free weekly refill (< 24h
|
|
3455
|
+
* out). General only (red-team r3): model walls are never held, so there is no
|
|
3456
|
+
* per-model count. The number
|
|
3457
|
+
* feeds decideClaudeWithReason's hold gate (opts.peersWithRoom), so it must be
|
|
3458
|
+
* computed LIVE from current account state — the hold re-evaluates every sweep.
|
|
3459
|
+
* Conservative per peer: only a probe-FRESH (≤ CLAIM_FRESH_MS) reading counts,
|
|
3460
|
+
* every disqualifier means "cannot confirm room" (claim, never hold on a guess):
|
|
3461
|
+
* - serves the SAME traffic (red-team r3 #4, r4 #5): counted PER routing profile
|
|
3462
|
+
* self serves, by the router's own _matchesProfile, and the THINNEST profile
|
|
3463
|
+
* decides — every kind of traffic the walled account carries needs its own
|
|
3464
|
+
* roomy peers (a peer serving only 'all' takes none of its 'claude' requests).
|
|
3465
|
+
* A self that serves no profile has no confirmed room (0).
|
|
3466
|
+
* - routable NOW: enabled, not throttled/error/exhausted, not pool-benched
|
|
3467
|
+
* (another process owns it), not a remote mirror (routed by its holder pool),
|
|
3468
|
+
* not auth/subscription-dead.
|
|
3469
|
+
* - weekly util (unified7d) leaves ≥ POLICY.holdPeerWeeklyRoom to the peer's
|
|
3470
|
+
* weekly ceiling — its effective usage cap, else 1.0 (uncapped: below the 0.65
|
|
3471
|
+
* `soft` tier the router already spreads load at). Red-team #4: room is
|
|
3472
|
+
* measured to the CAP, so a 0.70 cap at 0.64 is not room.
|
|
3473
|
+
* - 5h util (unified5h) leaves ≥ POLICY.holdPeerSessionRoom to the peer's
|
|
3474
|
+
* session bench (_sessionBenchThreshold: its 5h cap, else the 0.90 switch
|
|
3475
|
+
* threshold) — a session-benched account absorbs nothing.
|
|
3476
|
+
* - a DIFFERENT identity (accountUuid/credential fingerprint) — not this same
|
|
3477
|
+
* account under a second name; peers sharing one capacityIdentity (two local
|
|
3478
|
+
* names for one subscription) count ONCE (red-team #6).
|
|
3479
|
+
* - every ACTIVE model bucket the peer reports also has ≥ holdPeerWeeklyRoom
|
|
3480
|
+
* left (red-team #1) — the walled account serves no model at all, so a peer
|
|
3481
|
+
* whose Fable is exhausted/full (or 429-marked, or unreadable) would leave
|
|
3482
|
+
* Fable with no route for up to a day. An inactive bucket binds nothing (the
|
|
3483
|
+
* router's _scopedExhausted rule).
|
|
3484
|
+
* - a RESOLVABLE identity (red-team r2 #4): a peer whose capacityIdentity is
|
|
3485
|
+
* null is unknown capacity and is not counted; one equal to self's is self
|
|
3486
|
+
* (or self under a second name). Self itself is excluded by the same check —
|
|
3487
|
+
* its identity is either null or its own.
|
|
3488
|
+
* Returns a plain integer (the minimum over self's profiles); the POLICY decides
|
|
3489
|
+
* sufficiency (holdMinPeers × holders). */
|
|
3490
|
+
countPeersWithRoom(self, now = Date.now()) {
|
|
3491
|
+
const selfId = this.capacityIdentity(self);
|
|
3492
|
+
const selfProfiles = self?.profiles || ['claude', 'all'];
|
|
3493
|
+
const roomLeft = (util, ceiling, room) => Number.isFinite(util) && util <= ceiling - room;
|
|
3494
|
+
const roomy = [];
|
|
3495
|
+
for (const p of this.accounts) {
|
|
3496
|
+
const id = this.capacityIdentity(p);
|
|
3497
|
+
if (id == null || id === selfId) continue;
|
|
3498
|
+
if (p.type !== 'oauth' || p.enabled === false) continue;
|
|
3499
|
+
if (p.remote || p.poolBenchedBy) continue;
|
|
3500
|
+
if (p.refreshDead || p.subscriptionGone) continue;
|
|
3501
|
+
if (p.status === 'throttled' || p.status === 'error' || p.status === 'exhausted') continue;
|
|
3502
|
+
if (p.cooldownUntil && p.cooldownUntil > now) continue;
|
|
3503
|
+
const q = p.quota || {};
|
|
3504
|
+
// Probe-fresh only: a peer whose reading predates this sweep's decision is
|
|
3505
|
+
// unknown capacity, and unknown capacity must not hold a claim.
|
|
3506
|
+
const fresh = q.lastProbeOkAt != null && now - q.lastProbeOkAt <= CLAIM_FRESH_MS;
|
|
3507
|
+
if (!fresh) continue;
|
|
3508
|
+
const wkCeiling = this._effectiveCap(p, 'wk', now) ?? 1;
|
|
3509
|
+
if (!roomLeft(q.unified7d, wkCeiling, POLICY.holdPeerWeeklyRoom)) continue;
|
|
3510
|
+
if (!roomLeft(q.unified5h, this._sessionBenchThreshold(p, now), POLICY.holdPeerSessionRoom)) continue;
|
|
3511
|
+
const buckets = Object.values(q.scopedWeekly || {}).filter(e => e && e.isActive !== false);
|
|
3512
|
+
if (buckets.some(e => !roomLeft(e.utilization, 1, POLICY.holdPeerWeeklyRoom))) continue;
|
|
3513
|
+
roomy.push({ p, id });
|
|
3514
|
+
}
|
|
3515
|
+
if (!selfProfiles.length) return 0;
|
|
3516
|
+
return Math.min(...selfProfiles.map(pr =>
|
|
3517
|
+
new Set(roomy.filter(({ p }) => this._matchesProfile(p, pr)).map(({ id }) => id)).size));
|
|
3518
|
+
}
|
|
3519
|
+
|
|
3520
|
+
// ── R7 hold register (red-team r2 #1/#2) ──────────────────────────────────
|
|
3521
|
+
// account._wallHold = { at, refillAt } is stamped whenever either claim path HOLDS the
|
|
3522
|
+
// GENERAL weekly wall (the only wall R7 holds — red-team r3). It is live only
|
|
3523
|
+
// while the hold still describes reality — see isHoldingWall — so a general
|
|
3524
|
+
// claim, the refill or leaving the wall retires it without any clearing code.
|
|
3525
|
+
|
|
3526
|
+
/** Record that `account`'s general weekly wall claim was held, and WHICH refill
|
|
3527
|
+
* it waits for (red-team r4 #4). */
|
|
3528
|
+
noteWallHold(account, now = Date.now()) {
|
|
3529
|
+
if (account) account._wallHold = { at: now, refillAt: account.quota?.unified7dReset ?? null };
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
/** Is `account` holding its general weekly wall RIGHT NOW? A stamped hold that no
|
|
3533
|
+
* claim CLEARING the general weekly has superseded (lastFullGeneralClaimAt before
|
|
3534
|
+
* the stamp — a Fable claim refills nothing the hold waits on, red-team r3 #3; a
|
|
3535
|
+
* give-back claim retires it only through the live reading below, r5 #1), on an
|
|
3536
|
+
* enabled, live (r5 #2), locally-owned account still at the wall with its refill
|
|
3537
|
+
* still ahead — and that refill is still the one it waited for (a later window is
|
|
3538
|
+
* a new wall, not this hold; holdRefillSlackMs absorbs the two sources' reading
|
|
3539
|
+
* precision). */
|
|
3540
|
+
isHoldingWall(account, now = Date.now()) {
|
|
3541
|
+
const h = account?._wallHold;
|
|
3542
|
+
if (!h || account.enabled === false || account.poolBenchedBy) return false;
|
|
3543
|
+
if (account.refreshDead || account.subscriptionGone) return false; // a dead account serves nothing to hold for
|
|
3544
|
+
if (account.lastFullGeneralClaimAt != null && account.lastFullGeneralClaimAt >= h.at) return false;
|
|
3545
|
+
const q = account.quota || {};
|
|
3546
|
+
if (!(q.unified7dReset <= h.refillAt + POLICY.holdRefillSlackMs)) return false;
|
|
3547
|
+
return Number.isFinite(q.unified7d) && q.unified7d >= CLAUDE_WALL_WEEKLY && q.unified7dReset > now;
|
|
3548
|
+
}
|
|
3549
|
+
|
|
3550
|
+
/** The walls a hold must budget for: every OTHER account holding now, plus
|
|
3551
|
+
* `self` (the wall being decided). Feeds wallClaimHeld's holdersInPool. */
|
|
3552
|
+
countHoldingWalls(self, now = Date.now()) {
|
|
3553
|
+
return 1 + this.accounts.filter(a => a !== self && this.isHoldingWall(a, now)).length;
|
|
3554
|
+
}
|
|
3555
|
+
|
|
3452
3556
|
/**
|
|
3453
3557
|
* Does this account hold a banked reset that can actually lift its BINDING limit?
|
|
3454
3558
|
* Claude grants: usable, not paused, unexpired. z.ai cards: unexpired five-hour cards
|
|
@@ -5716,6 +5820,14 @@ export class AccountManager {
|
|
|
5716
5820
|
// count instead of nothing; restoreQuotaState marks it stale.
|
|
5717
5821
|
if (a.resetGrants) out.resetGrants = { ...a.resetGrants, grants: (a.resetGrants.grants || []).map(g => ({ ...g })) };
|
|
5718
5822
|
if (a.resetCards) out.resetCards = { ...a.resetCards, fiveHour: (a.resetCards.fiveHour || []).map(c => ({ ...c })), weekly: (a.resetCards.weekly || []).map(c => ({ ...c })) };
|
|
5823
|
+
// R7 HOLD REGISTER survives restart (red-team r5 #3): the hold and the claim
|
|
5824
|
+
// stamps that retire it / guard one claim per wall. isHoldingWall re-checks the
|
|
5825
|
+
// restored hold against the live wall and refill, so a stale one self-retires.
|
|
5826
|
+
// Unset fields are undefined and drop out of the JSON state file; a hold is
|
|
5827
|
+
// only ever replaced whole (noteWallHold), so sharing the reference is safe.
|
|
5828
|
+
out.wallHold = a._wallHold;
|
|
5829
|
+
out.lastGeneralClaimAt = a.lastGeneralClaimAt;
|
|
5830
|
+
out.lastFullGeneralClaimAt = a.lastFullGeneralClaimAt;
|
|
5719
5831
|
return out;
|
|
5720
5832
|
});
|
|
5721
5833
|
}
|
|
@@ -5762,6 +5874,11 @@ export class AccountManager {
|
|
|
5762
5874
|
if (match.resetCards && !account.resetCards) {
|
|
5763
5875
|
account.resetCards = { ...match.resetCards, stale: true };
|
|
5764
5876
|
}
|
|
5877
|
+
// R7 hold register (r5 #3). Boot-time only (index.js), so nothing live is
|
|
5878
|
+
// overwritten; an absent field restores as undefined, i.e. never stamped.
|
|
5879
|
+
account._wallHold = match.wallHold;
|
|
5880
|
+
account.lastGeneralClaimAt = match.lastGeneralClaimAt;
|
|
5881
|
+
account.lastFullGeneralClaimAt = match.lastFullGeneralClaimAt;
|
|
5765
5882
|
// OUTSIDE-USAGE restored: keep holding R7 and force the next sweep to
|
|
5766
5883
|
// re-verify immediately (fresh poolHourly can only re-prove the leak).
|
|
5767
5884
|
if (match.outsideUsage?.outside) {
|
|
@@ -6061,12 +6178,40 @@ export class AccountManager {
|
|
|
6061
6178
|
if (!account.credential || account.type !== 'oauth') return false;
|
|
6062
6179
|
// C6 (finding 1): ONE claim per wall — after a claim, only a probe SENT after
|
|
6063
6180
|
// it may show the wall again. Repeated 429s on the same wall never re-claim.
|
|
6064
|
-
|
|
6181
|
+
// The general wall counts only claims that cleared it (red-team r4 #2): after a
|
|
6182
|
+
// Fable claim under the hold, a dark probe can't move past the account-wide
|
|
6183
|
+
// stamp, and the rescue / weekly 429 must still reach the general reset.
|
|
6184
|
+
const lastClaimAt = (kind === 'weekly' ? account.lastGeneralClaimAt : account.lastClaimAt) ?? null;
|
|
6065
6185
|
if (lastClaimAt != null && !((probeSentAt(account.quota) ?? -Infinity) > lastClaimAt)) return false;
|
|
6066
6186
|
// No grant (or a scoped 429 without a probe-measured bucket) → the next sweep
|
|
6067
6187
|
// decides on its own fresh reading.
|
|
6068
6188
|
const grant = this._reactiveClaimGrant(account, kind, now);
|
|
6069
6189
|
if (!grant) return false;
|
|
6190
|
+
// R7 (owner, 2026-11-13): a wall 429 claims a held reset only when the free
|
|
6191
|
+
// refill of the limit it clears ISN'T imminent-with-pool-room — the same
|
|
6192
|
+
// wallClaimHeld rule the sweep uses, so the fast path cannot spend a reset the
|
|
6193
|
+
// sweep deliberately holds. Evaluated DIRECTLY here, not via the sweep's full
|
|
6194
|
+
// decision (red-team #3): at the wall the account's own usage probe and listing
|
|
6195
|
+
// are routinely stale (the usage endpoint 429s), and the sweep's freshness skips
|
|
6196
|
+
// would read as "not held" and fall through to a claim. The hold needs no fresh
|
|
6197
|
+
// reading of THIS account: the refill stamp is kept current by every response's
|
|
6198
|
+
// rate-limit headers (updateQuota), the grant end comes from the stored listing,
|
|
6199
|
+
// and capacity comes from the PEERS' own fresh probes (countPeersWithRoom). A
|
|
6200
|
+
// grant dying before (or within 24h after) the refill never waits. The budget
|
|
6201
|
+
// counts every wall held in this pool right now (r2 #2) and the hold is
|
|
6202
|
+
// registered (r2 #1) so the sweep's rescue can find it again.
|
|
6203
|
+
// GENERAL ONLY (red-team r3): a model (Fable) 429 claims exactly as before R7.
|
|
6204
|
+
if (kind === 'weekly' && wallClaimHeld(account.quota?.unified7dReset, grant, now,
|
|
6205
|
+
{ peersWithRoom: () => this.countPeersWithRoom(account, now),
|
|
6206
|
+
holdersInPool: () => this.countHoldingWalls(account, now),
|
|
6207
|
+
listingCheckedAt: account.resetGrants?.checkedAt })) {
|
|
6208
|
+
// Log once per hold EPISODE: the sweep's rescue re-evaluates a held wall every
|
|
6209
|
+
// failed tick, and a line per tick would bury the console.
|
|
6210
|
+
const already = this.isHoldingWall(account, now);
|
|
6211
|
+
this.noteWallHold(account, now);
|
|
6212
|
+
if (!already) console.log(`[reset-grant] ${account.name}: at wall, claim held (${HOLD_POOL_HAS_ROOM})`);
|
|
6213
|
+
return false;
|
|
6214
|
+
}
|
|
6070
6215
|
// C6: shared backoff per (account, grantId).
|
|
6071
6216
|
const key = `${account.name}:${grant.id}`;
|
|
6072
6217
|
const until = this._claimRetryUntil?.get(key);
|
|
@@ -6084,6 +6229,12 @@ export class AccountManager {
|
|
|
6084
6229
|
if (r?.ok) {
|
|
6085
6230
|
this.noteClaimSuccess(account.name, grant.id);
|
|
6086
6231
|
account.lastClaimAt = Date.now();
|
|
6232
|
+
// A spent grant that applies to the general weekly — whatever the 429 kind —
|
|
6233
|
+
// counts as the general wall's claim (r3 #3, r4 #3: one claim per wall). Only
|
|
6234
|
+
// one that CLEARS it retires the hold (r5 #1): a give-back returns just what
|
|
6235
|
+
// the model used, and the live reading retires the hold once a probe sees it.
|
|
6236
|
+
if (appliesTo(grant).includes('general')) account.lastGeneralClaimAt = account.lastClaimAt;
|
|
6237
|
+
if (clearsGeneralWeekly(grant)) account.lastFullGeneralClaimAt = account.lastClaimAt;
|
|
6087
6238
|
this.noteGrantClaimed(account, grant.id);
|
|
6088
6239
|
// Clear the wall this claim refills.
|
|
6089
6240
|
if (kind !== 'weekly' && account.quota?.scopedWeekly?.[kind]) delete account.quota.scopedWeekly[kind];
|
|
@@ -80,6 +80,14 @@ export function appliesTo(grant) {
|
|
|
80
80
|
return [...out];
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/** Does spending this grant REFILL the general weekly outright (clears seven_day)?
|
|
84
|
+
* A give-back-only grant also applies to 'general' (appliesTo), but it returns
|
|
85
|
+
* only what one model used — whether that leaves the general wall is known only
|
|
86
|
+
* once a probe measures the drop (red-team r5 #1). */
|
|
87
|
+
export function clearsGeneralWeekly(grant) {
|
|
88
|
+
return Array.isArray(grant?.clears) && grant.clears.includes('seven_day');
|
|
89
|
+
}
|
|
90
|
+
|
|
83
91
|
// Process-wide: the first listing logs the raw grant KEY SET (keys only, never
|
|
84
92
|
// values) once, so a schema drift is visible without per-sweep spam. Set by
|
|
85
93
|
// listResetGrants on the first grants-bearing response.
|
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 (
|
|
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
|
}
|