maxpool 1.24.95 → 1.24.96

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,4 +1,5 @@
1
1
  import { createInterface } from 'node:readline';
2
+ import { spawn } from 'node:child_process';
2
3
  import { readFileSync, writeSync } from 'node:fs';
3
4
  import { isatty } from 'node:tty';
4
5
  import { homedir } from 'node:os';
@@ -6,9 +7,13 @@ import { fetchProfile, loginOAuth, tokenFingerprint, isLoginCancelled, sameUser,
6
7
  import { appendEventLog, setConsoleStdoutSuppressed } from './event-log.js';
7
8
  import { sendLoginToPool } from './cloud-login.js';
8
9
  import { describeLinkError } from './pool-link.js';
9
- import { normalizeRemoteResets } from './account-manager.js';
10
+ import { normalizeRemoteResets, resolveStickyBusyBudget } from './account-manager.js';
10
11
  import { appliesTo } from './claude-reset-grants.js';
11
12
  import { LISTING_FRESH_MS } from './reset-timing.js';
13
+ import { cacheUsageLabel, cacheUsageState } from './usage-label.js';
14
+
15
+ // Running as the background service (tools/pool-service): set by its launchd plist.
16
+ const serviceMode = () => process.env.MAXPOOL_SERVICE === '1';
12
17
 
13
18
  // ── ANSI helpers ─────────────────────────────────────────────
14
19
 
@@ -27,6 +32,8 @@ const red = s => fg(31, s);
27
32
  const cyan = s => fg(36, s);
28
33
  const gray = s => fg(90, s);
29
34
  const dimUnderline = s => `${ESC}2;4m${s}${RESET}`;
35
+ // The cache figures of a request line, by whether the cache held (#642 port, 2026-11-15).
36
+ const CACHE_PAINT = { hit: green, partial: yellow, miss: red, none: dim };
30
37
 
31
38
  // Terminal output budget (2026-11-12: the pool froze for up to 60s inside a synchronous
32
39
  // TTY write() — a full-screen redraw per log line, 4.9 GB in 12h49m). Frames are
@@ -140,14 +147,16 @@ const vw = s => strip(s).length;
140
147
  // ── Accounts-table columns ───────────────────────────────────
141
148
  // Fixed column widths shared by the header row (acctHeader) AND every data row, so
142
149
  // the header labels stay aligned with the columns they name. The Account/Provider/
143
- // Status/Rst/Quota start offsets (4/25/35/49/56) are pure functions of these widths +
150
+ // Status/MRst/ARst/Quota start offsets are pure functions of these widths +
144
151
  // the 4-col row prefix, independent of the quota-bar width.
145
152
  const NAME_W = 20; // a.name.slice(0, NAME_W).padEnd(NAME_W) — fits a full email like 2solarmax@gmail.com (19)
146
153
  const WHERE_W = 5; // 'Local' / 'Cloud' — where the account is provisioned
147
154
  const PROVIDER_W = 9; // providerLabel(a).padEnd(PROVIDER_W) — fits "Anthropic"
148
155
  const STATUS_W = 13; // rpad(status, STATUS_W) — fits "throttled 59s"
149
- const RESETS_W = 13; // 'Rst' column — FIXED width (fits '1+3Fable~118d': count AND expiry, owner 2026-11-11); a variable
150
- // width ('0' vs '1~21d') shifted every quota bar per row.
156
+ const MRST_W = 11; // 'MRst' column (model resets) — FIXED width (fits '2Fable~118d': count AND expiry, owner 2026-11-11);
157
+ // a variable width ('0' vs '1~21d') shifted every quota bar per row.
158
+ const ARST_W = 7; // 'ARst' column (account-level resets) — FIXED width (fits '1~118d?'); owner 2026-11-15: model and
159
+ // account resets in ONE mixed cell read as confusing, so they are two aligned columns.
151
160
  const ROW_PREFIX = ' '; // ' ' + sel(1) + cur(1) + ' ' — 4 cols before the name
152
161
 
153
162
  // Human provider name for the accounts-table "Provider" column. account.provider
@@ -183,7 +192,8 @@ function acctHeader(W) {
183
192
  + 'Where'.padEnd(WHERE_W) + ' '
184
193
  + 'Provider'.padEnd(PROVIDER_W) + ' '
185
194
  + 'Status'.padEnd(STATUS_W) + ' '
186
- + 'Rst'.padEnd(RESETS_W) + ' '
195
+ + 'MRst'.padEnd(MRST_W) + ' '
196
+ + 'ARst'.padEnd(ARST_W) + ' '
187
197
  + quota;
188
198
  }
189
199
 
@@ -196,18 +206,18 @@ function lpadVis(s, w) {
196
206
  /**
197
207
  * Which quota bars fit a terminal W columns wide, and how wide each bar is. The
198
208
  * space before the first bar comes from the column widths above, never from a
199
- * hard-coded number (the old number went stale when the Rst column was added).
209
+ * hard-coded number (the old number went stale when the reset columns were added).
200
210
  * Each extra bar costs its label gap (" Fbl " or " Wk ") plus one bar width.
201
211
  * Under 70 columns only the session bar shows, 5 to 20 wide; from 70 the weekly
202
212
  * bar joins it, both 5 to 20 wide. The Fable bar joins only once all three can be
203
- * 9 wide (112 columns), the narrowest bar that still shows "100% 2d5h", and three
204
- * bars stop growing at 11 wide, so the row never passes column 118 and the tags
213
+ * 9 wide (118 columns), the narrowest bar that still shows "100% 2d5h", and three
214
+ * bars stop growing at 11 wide, so the row never passes column 124 and the tags
205
215
  * after the bars (cap, prefer, staleness) keep the room they have with two bars.
206
216
  * Narrower terminals keep today's two bars plus the "Fable NN%" tag.
207
217
  * When no account reports a session window, the session bar is left out: the
208
- * weekly bar always shows and the Fable bar joins it from 97 columns.
218
+ * weekly bar always shows and the Fable bar joins it from 103 columns.
209
219
  */
210
- const BAR_START = ROW_PREFIX.length + NAME_W + 1 + WHERE_W + 1 + PROVIDER_W + 1 + STATUS_W + 1 + RESETS_W + 1 + 'Ses '.length;
220
+ const BAR_START = ROW_PREFIX.length + NAME_W + 1 + WHERE_W + 1 + PROVIDER_W + 1 + STATUS_W + 1 + MRST_W + 1 + ARST_W + 1 + 'Ses '.length;
211
221
  const BAR_GAP = ' Wk '.length;
212
222
  const BAR_MIN_W = 5;
213
223
  const BAR_MAX_W = 20;
@@ -258,6 +268,11 @@ function truncate(s, w) {
258
268
  }
259
269
 
260
270
  /** Fit a line to exactly w columns: truncate if too long, pad if too short. */
271
+ /** Truncate only when too wide (no padding) — for header lines. */
272
+ function truncLine(s, w) {
273
+ return vw(s) > w ? truncate(s, w) : s;
274
+ }
275
+
261
276
  function fitLine(s, w) {
262
277
  const v = vw(s);
263
278
  if (v > w) return truncate(s, w);
@@ -361,10 +376,19 @@ function loadText(load) {
361
376
  const hourReq = load?.last1h?.requests || 0;
362
377
  const failed = load?.last15m?.failed || 0;
363
378
  const linkFailed = load?.last15m?.linkFailed || 0;
364
- if (!inflight && !hourReq && !failed && !linkFailed) return '';
379
+ const bounced = load?.last15m?.bounced || 0;
380
+ const eff = load?.last15m?.efficiency;
381
+ // Dim below the volume floors (too few turns to mean much). A cloud row's figure is the
382
+ // serving pool's own, team-wide on that account — tagged (team).
383
+ const cacheTxt = eff && eff.hit != null ? `cache ${Math.round(eff.hit * 100)}%` : null;
384
+ const cache = cacheTxt
385
+ ? `${eff.lowVolume ? dim(cacheTxt) : cacheTxt}${load.last15m.team ? dim(' (team)') : ''}` : null;
386
+ if (!inflight && !hourReq && !failed && !linkFailed && !bounced && !cache) return '';
365
387
  const parts = [];
366
388
  if (inflight) parts.push(`${inflight} live`);
367
389
  if (hourReq) parts.push(`${hourReq} req/hr`);
390
+ if (cache) parts.push(cache);
391
+ if (bounced) parts.push(dim(`${bounced} bounced`));
368
392
  if (failed) parts.push(red(`${failed} failed`));
369
393
  if (linkFailed) parts.push(yellow(`${linkFailed} link drop${linkFailed === 1 ? '' : 's'}`));
370
394
  return parts.join(' · ');
@@ -590,7 +614,9 @@ function emptyBar(label, w = 10) {
590
614
  */
591
615
  function scopedWeeklyBar(quota, family, w) {
592
616
  const e = quota?.scopedWeekly?.[family];
593
- if (!e || e.isActive === false || e.utilization == null) return emptyBar('—', w);
617
+ // is_active=false still carries a REAL reading (percent + reset days out): the
618
+ // limit exists and counts. Only a MISSING reading dashes.
619
+ if (!e || e.utilization == null) return emptyBar('—', w);
594
620
  return bar(e.utilization, w, e.resetAt);
595
621
  }
596
622
 
@@ -646,13 +672,18 @@ export function decideLoginTarget({ mirror, links, existing, accountUuid }) {
646
672
  }
647
673
 
648
674
  export class TUI {
649
- constructor({ accountManager, config, saveConfig, syncAccounts, onQuit, onRestart }) {
675
+ constructor({ accountManager, config, saveConfig, syncAccounts, onQuit, onRestart, spawnDetached }) {
650
676
  this.am = accountManager;
677
+ // Runs the tmux detach for q in service mode; a seam so tests never start tmux.
678
+ this._spawnDetached = spawnDetached || ((cmd, args) => {
679
+ try { spawn(cmd, args, { stdio: 'ignore', detached: true }).on('error', () => {}).unref(); } catch { /* no tmux */ }
680
+ });
651
681
  this.config = config;
652
682
  this.saveConfig = saveConfig;
653
683
  this.syncAccounts = syncAccounts;
654
684
  this.onQuit = onQuit;
655
685
  this.onRestart = onRestart;
686
+ this._startedAt = Date.now(); // the efficiency line's "since restart" window
656
687
  // Set by index.js after construction (a deferred closure, not a constructor literal —
657
688
  // avoids the const TDZ on applyUpdateIfReady, which is defined after `new TUI`).
658
689
  this.checkNow = null;
@@ -898,7 +929,13 @@ export class TUI {
898
929
  const ttfbTag = ttfbMs != null ? ` ttfb=${(Math.max(0, ttfbMs) / 1000).toFixed(1)}s` : '';
899
930
  // B8: ' ns' = a non-streaming request (strict false: the oauth relay carries no shape, so no tag).
900
931
  const nsTag = info.stream === false ? ' ns' : '';
901
- this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${nsTag}${provTag}${cvTag}${ttfbTag}`);
932
+ // The served attempt's cache figures (~25B, well inside the 480B line cap): whether
933
+ // the cache held is the one thing a request line could not say (#642 port).
934
+ const cacheLabel = cacheUsageLabel(info.cacheUsage);
935
+ const cacheTag = cacheLabel ? ` ${CACHE_PAINT[cacheUsageState(info.cacheUsage)](cacheLabel)}` : '';
936
+ // ttfb= stays the LAST field — the request-line contract (reset-count-stale-and-ttfb
937
+ // test 13); the cache tag goes just before it.
938
+ this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${nsTag}${provTag}${cvTag}${cacheTag}${ttfbTag}`);
902
939
  }
903
940
 
904
941
  _addLog(msg) {
@@ -972,8 +1009,19 @@ export class TUI {
972
1009
  this.forceRender();
973
1010
  }
974
1011
 
1012
+ /** Close every viewer of the service dashboard (viewers are view-only); the pool
1013
+ * keeps running. `-s pool` detaches all clients of the session, deterministically. */
1014
+ detachView() {
1015
+ const sock = process.env.MAXPOOL_TMUX_SOCK;
1016
+ this._spawnDetached('tmux', [...(sock ? ['-S', sock] : []), 'detach-client', '-s', 'pool']);
1017
+ }
1018
+
975
1019
  _keyNormal(k) {
976
- if (k === 'q') {
1020
+ if (k === 'q' && serviceMode()) {
1021
+ // Under the background service the pool lives in a tmux pane launchd owns:
1022
+ // stopping it here would only make launchd cold-restart it. q closes the view.
1023
+ this.detachView();
1024
+ } else if (k === 'q') {
977
1025
  this._confirm(
978
1026
  'Stop Maxpool?',
979
1027
  'New requests will stop; active requests will drain before the server exits.',
@@ -1129,6 +1177,71 @@ export class TUI {
1129
1177
  return ` Routing ${cyan(routing)}${xpText}`;
1130
1178
  }
1131
1179
 
1180
+ /** EFFICIENCY line (2026-11-15), directly under Routing: is the prompt cache holding,
1181
+ * and what are moves costing — Claude and GLM never blended. null with no traffic.
1182
+ * Colours are WARN-only proposals until a one-day baseline exists; a red names the
1183
+ * cause and the account. */
1184
+ _efficiencyLine(W = 80, now = Date.now()) {
1185
+ const eff = this.am.efficiencyOverall?.('last15m', now);
1186
+ if (!eff) return null;
1187
+ const { claude: c, glm: g } = eff;
1188
+ const served = c.served + g.served;
1189
+ const bounced = c.bounced + g.bounced;
1190
+ if (served + bounced === 0) return null;
1191
+ const sinceMs = now - (this._startedAt ?? now);
1192
+ const since = sinceMs < 15 * 60_000 ? dim(` since restart ${Math.max(1, Math.round(sinceMs / 60_000))}m`) : '';
1193
+ if (c.lowVolume && g.lowVolume) return truncLine(` Cache ${dim('— (little traffic)')}${since}`, W);
1194
+ const pctTxt = x => `${Math.round(x * 100)}%`;
1195
+ const parts = [];
1196
+ if (c.hit != null) parts.push(`Claude ${c.lowVolume ? dim(pctTxt(c.hit)) : pctTxt(c.hit)}`);
1197
+ if (g.hit != null) parts.push(`GLM ${g.lowVolume ? dim(pctTxt(g.hit)) : pctTxt(g.hit)}`);
1198
+ const miss = c.missTokens + g.missTokens;
1199
+ parts.push(`miss ${miss >= 1e6 ? `${(miss / 1e6).toFixed(1)}M` : `${Math.round(miss / 1000)}k`} tok`);
1200
+ const cause = k => c.movedByCause[k] + g.movedByCause[k];
1201
+ const per100 = n => (served ? Math.round(n * 100 / served) : 0);
1202
+ parts.push(`moves ${per100(c.moved + g.moved)}/100 (busy ${cause('busy')} · balance ${cause('balance')} · wall ${cause('wall')}${cause('link') ? ` · link ${cause('link')}` : ''}${cause('other') ? ` · other ${cause('other')}` : ''})`);
1203
+ const bounceShare = bounced / (served + bounced);
1204
+ if (bounced) parts.push(`bounced ${pctTxt(bounceShare)}`);
1205
+ const waited = c.waited + g.waited;
1206
+ if (waited) parts.push(`waited ${waited} (avg ${((c.waitMs + g.waitMs) / waited / 1000).toFixed(1)}s)`);
1207
+
1208
+ // RED — the first cause that applies, named with the account behind it.
1209
+ const forced = cause('busy') + cause('wall') + cause('link');
1210
+ const wtm = c.waitThenMoved + g.waitThenMoved;
1211
+ let red_ = null;
1212
+ if (!c.lowVolume && c.hit != null && c.hit < 0.5) red_ = `Claude ${pctTxt(c.hit)} on ${this._worstCacheAccount() || '?'}`;
1213
+ else if (per100(forced) > 2) red_ = `${per100(forced)} forced moves/100 — most refusals on ${this._mostBouncedAccount(now) || '?'}`;
1214
+ else if (bounceShare > 0.1) red_ = `${pctTxt(bounceShare)} bounced — most on ${this._mostBouncedAccount(now) || '?'}`;
1215
+ else if (wtm * 100 / Math.max(1, served) > 0.5) red_ = `${wtm} waits still moved — on ${this._mostBouncedAccount(now) || '?'}`;
1216
+ if (red_) return truncLine(` ${red('Cache')} ${red(`⚠ ${red_}`)} ${dim('·')} ${parts.join(' · ')} ${dim('[15m]')}${since}`, W);
1217
+ // YELLOW — Claude below 70%, drifting 15+ points under its hour, or waits are common.
1218
+ const hour = this.am.efficiencyOverall('last1h', now).claude;
1219
+ const drift = !c.lowVolume && !hour.lowVolume && c.hit != null && hour.hit != null && hour.hit - c.hit >= 0.15;
1220
+ const warn = (!c.lowVolume && c.hit != null && c.hit < 0.7) || drift || waited > 0.25 * served;
1221
+ return truncLine(` ${warn ? yellow('Cache') : green('Cache')} ${parts.join(' · ')} ${dim('[15m]')}${since}`, W);
1222
+ }
1223
+
1224
+ /** The Claude row with the lowest cache share in the window (enough volume only). */
1225
+ _worstCacheAccount() {
1226
+ let worst = null;
1227
+ for (const a of this.am.accounts) {
1228
+ if (a.enabled === false || a.type === 'provider') continue;
1229
+ const e = this._accountLoad(a)?.last15m?.efficiency;
1230
+ if (e && !e.lowVolume && e.hit != null && (!worst || e.hit < worst.hit)) worst = { name: a.name, hit: e.hit };
1231
+ }
1232
+ return worst?.name || null;
1233
+ }
1234
+
1235
+ /** The row with the most pinned refusals (bounces) seen by this pool in the window. */
1236
+ _mostBouncedAccount(now = Date.now()) {
1237
+ let best = null;
1238
+ for (const a of this.am.accounts) {
1239
+ const n = this.am._loadSummary(a, 15 * 60_000, now).bounced;
1240
+ if (n > 0 && (!best || n > best.n)) best = { name: a.name, n };
1241
+ }
1242
+ return best?.name || null;
1243
+ }
1244
+
1132
1245
  /** One line per unreachable team pool, so a lost connection reads as ONE cause
1133
1246
  * instead of every cloud row turning red. '' lines are skipped by the caller. */
1134
1247
  _cloudLinkLines(W = 80, now = Date.now()) {
@@ -1450,6 +1563,10 @@ export class TUI {
1450
1563
  // mode — under balance/prefer-* the mode itself controls eligibility. Still
1451
1564
  // safe to set (it'll apply if you switch back to sticky).
1452
1565
  this._cycleProviderClaudeFallback(k === 'g' ? 'zai' : 'kimi');
1566
+ } else if (k === 'b' || k === 'B') {
1567
+ // STICKY BUSY WAIT (2026-11-15): how long a conversation waits for its own busy
1568
+ // account before moving (a move re-writes its whole prompt cache).
1569
+ this._cycleStickyBusyBudget();
1453
1570
  } else if (k === 'w' || k === 'W') {
1454
1571
  // WEEKLY-AWARE SCORING (2026-09-16): fold weekly utilization into the routing
1455
1572
  // score, not just the 5h session. Without it, an account at 89% weekly with a
@@ -1570,6 +1687,42 @@ export class TUI {
1570
1687
  : 'Weekly-aware routing: OFF — score sees only the 5h session window again');
1571
1688
  }
1572
1689
 
1690
+ static STICKY_BUSY_STEPS = [15_000, 30_000, 60_000, 120_000, 0];
1691
+
1692
+ _stickyBusyLabel() {
1693
+ // The value IN FORCE — the server's own resolver, so an env override shows as such.
1694
+ const { ms, fromEnv } = resolveStickyBusyBudget(this.am.scheduler);
1695
+ const txt = ms > 0 ? `${Math.round(ms / 1000)}s` : 'off';
1696
+ return fromEnv ? `${txt} (env)` : txt;
1697
+ }
1698
+
1699
+ /** Cycle the busy-account wait. Live: the server reads it per request, so the next
1700
+ * request uses the new value; saved so it survives a restart. */
1701
+ async _cycleStickyBusyBudget() {
1702
+ if (resolveStickyBusyBudget(this.am.scheduler).fromEnv) {
1703
+ // Saving a setting the server ignores would make the screen lie about the wait.
1704
+ this._addLog('Wait for busy account is pinned by MAXPOOL_STICKY_BUSY_BUDGET_MS — unset it to change it here');
1705
+ return;
1706
+ }
1707
+ const steps = TUI.STICKY_BUSY_STEPS;
1708
+ const cur = this.am.scheduler.stickyBusyBudgetMs ?? 60_000;
1709
+ const next = steps[(steps.indexOf(cur) + 1) % steps.length];
1710
+ this.am.scheduler.stickyBusyBudgetMs = next;
1711
+ const prevSched = this.config.scheduler;
1712
+ this.config.scheduler = { ...(prevSched || {}), stickyBusyBudgetMs: next };
1713
+ try {
1714
+ await this.saveConfig(this.config);
1715
+ } catch (e) {
1716
+ this.am.scheduler.stickyBusyBudgetMs = cur;
1717
+ this.config.scheduler = prevSched;
1718
+ this._addLog(`Could not save: ${e.message}`);
1719
+ return;
1720
+ }
1721
+ this._addLog(next > 0
1722
+ ? `Wait for busy account: up to ${Math.round(next / 1000)}s before a conversation moves to another account`
1723
+ : 'Wait for busy account: off — a busy account moves the conversation at once');
1724
+ }
1725
+
1573
1726
  async _cycleRoutingMode() {
1574
1727
  const modes = TUI.ROUTING_MODES;
1575
1728
  const cur = this.am.scheduler?.routingMode || 'sticky';
@@ -2502,6 +2655,8 @@ export class TUI {
2502
2655
  if (provReq > 0) xpText += ` ${dim('·')} ${yellow(`providers: ${provReq} req/15m`)}`;
2503
2656
  }
2504
2657
  lines.push(this._routingLine(routing, xpText));
2658
+ const effLine = this._efficiencyLine(W);
2659
+ if (effLine != null) lines.push(effLine);
2505
2660
  lines.push(...this._cloudLinkLines(W));
2506
2661
  const queuedCount = this.am.queueState?.waiting?.length || 0;
2507
2662
  // Throttle and WAITING are different things and used to share one line labelled
@@ -2868,17 +3023,16 @@ export class TUI {
2868
3023
  // Widened from 10 to fit "throttled 59s" so the quota bars stay column-aligned.
2869
3024
  status = rpad(status, STATUS_W);
2870
3025
 
2871
- // Rst column — banked limit resets (owner request 2026-09-30: a separate
3026
+ // MRst/ARst columns — banked limit resets (owner request 2026-09-30: a separate
2872
3027
  // column showing 0 or the count). z.ai cards + Claude cedar_ember grants,
2873
3028
  // not-yet-expired, on ENABLED accounts only: a disabled account's resets are
2874
3029
  // not ours to spend, and showing a count there would imply otherwise.
2875
3030
  //
2876
- // B3 grammar (2026-11-10), rendered width EXACTLY RESETS_W (6):
2877
- // general n~Xd e.g. 2~119d
2878
- // scoped nF~Xd e.g. 2F~4d (model-scoped, e.g. Fable)
2879
- // mixed g+sF e.g. 1+2F
2880
- // unknown n? scope unknown — count shown, never claimed
2881
- // not-fresh = gray + trailing '?' (stale listing, dead account, old check)
3031
+ // B3 grammar (2026-11-10), split into two cells 2026-11-15:
3032
+ // MRst (width MRST_W) model-scoped nFable~Xd e.g. 2Fable~114d ('-' when none)
3033
+ // ARst (width ARST_W) account-level n~Xd e.g. 1~29d ('0' when none)
3034
+ // unknown n? scope unknown (ARst only) — count shown, never claimed
3035
+ // not-fresh = gray + trailing '?' when it fits (stale listing, dead account, old check)
2882
3036
  // Count = SUM of resetsLeft over spendable grants + live cards (not entries).
2883
3037
  // Overflow order: drop the suffix first, then collapse to total+'*'.
2884
3038
  // Expiry is checked LIVE here: grants endsAt>now, cards !expired &&
@@ -2890,13 +3044,14 @@ export class TUI {
2890
3044
  let general = 0; // resetsLeft of general-weekly grants + live cards
2891
3045
  let scoped = 0; // resetsLeft of model-scoped grants (tagged F)
2892
3046
  let unknown = 0; // resetsLeft of scope-unknown grants
2893
- let soonest = Infinity;
3047
+ let soonestG = Infinity; // soonest expiry among account-level (general/unknown) resets
3048
+ let soonestS = Infinity; // soonest expiry among model-scoped resets
2894
3049
  if (a.enabled !== false) {
2895
3050
  const liveCard = c => c && !c.expired && (c.expiresAt == null || c.expiresAt > now);
2896
3051
  const rc = a.resetCards;
2897
3052
  if (rc) {
2898
3053
  for (const c of [...(rc.fiveHour || []), ...(rc.weekly || [])]) {
2899
- if (liveCard(c)) { general += 1; if (c.expiresAt != null && c.expiresAt < soonest) soonest = c.expiresAt; }
3054
+ if (liveCard(c)) { general += 1; if (c.expiresAt != null && c.expiresAt < soonestG) soonestG = c.expiresAt; }
2900
3055
  }
2901
3056
  }
2902
3057
  const rg = a.resetGrants;
@@ -2918,11 +3073,15 @@ export class TUI {
2918
3073
  const clearsWeekly = (Array.isArray(g.clears) ? g.clears : []).includes('seven_day');
2919
3074
  const hasScoped = scopes.some(x => x !== 'general' && x !== 'session');
2920
3075
  const left = Math.max(0, Math.floor(g.resetsLeft ?? 1));
3076
+ let isScoped = false;
2921
3077
  if (clearsWeekly) general += left;
2922
- else if (hasScoped) scoped += left;
3078
+ else if (hasScoped) { scoped += left; isScoped = true; }
2923
3079
  else if (scopes.includes('general')) general += left;
2924
3080
  else unknown += left;
2925
- if (g.endsAt != null && g.endsAt < soonest) soonest = g.endsAt;
3081
+ if (g.endsAt != null) {
3082
+ if (isScoped) { if (g.endsAt < soonestS) soonestS = g.endsAt; }
3083
+ else if (g.endsAt < soonestG) soonestG = g.endsAt;
3084
+ }
2926
3085
  }
2927
3086
  }
2928
3087
  }
@@ -2936,38 +3095,37 @@ export class TUI {
2936
3095
  && (a.resetCards.checkedAt == null || now - a.resetCards.checkedAt <= LISTING_FRESH_MS));
2937
3096
  const notFresh = total > 0 && (!freshGrants || !freshCards || a.refreshDead || a.subscriptionGone);
2938
3097
 
2939
- // Core text per the grammar.
2940
- let core;
2941
- if (total === 0) core = '0';
2942
- else if (unknown > 0 && general === 0 && scoped === 0) core = `${unknown}?`;
2943
- else if (general > 0 && scoped > 0) core = `${general}+${scoped}F`;
2944
- else if (scoped > 0) core = `${scoped}F`;
2945
- else core = `${general}`;
2946
-
2947
- // Expiry suffix (owner 2026-10-01: always show when a reset exists).
2948
- let suffix = '';
2949
- if (total > 0 && soonest !== Infinity) {
3098
+ // Two cells (owner 2026-11-15): MRst = model-scoped resets, ARst = account-level.
3099
+ // Each cell has its OWN count, expiry and fit; the staleness flag is shared.
3100
+ const expirySuffix = (n, soonest) => {
3101
+ if (!(n > 0) || soonest === Infinity) return '';
2950
3102
  const days = (soonest - now) / 86400_000;
2951
- suffix = days < 1 ? `~${Math.max(1, Math.round(days * 24))}h` : `~${Math.max(1, Math.round(days))}d`;
2952
- }
2953
-
2954
- // Fit to EXACTLY RESETS_W: full form, then drop the suffix, then total+'*'.
2955
- // OWNER READABILITY (2026-11-10: "What does the letter F mean? I have no
2956
- // idea"): a bare F is undecodable shorthand — the 2026-08-27 no-glossary
2957
- // rule says the cell says what it means in words. Model-scoped resets spell
2958
- // the model: '2Fable' / '1Opus'. RESETS_W grows to fit '99Fable' (8) so the
2959
- // word is never truncated into a mystery tag again; plain counts keep the
2960
- // compact n~Xd form.
2961
- const coreFable = core.replace(/F\b/, 'Fable');
2962
- let text = coreFable + suffix;
2963
- if (text.length > RESETS_W) text = coreFable;
2964
- if (text.length > RESETS_W) text = `${total}*`.slice(0, RESETS_W);
2965
- // Not-fresh: append '?' only when it fits. When it does not ('1+2Fable' is
2966
- // already 8), keep the breakdown and let the gray colour carry staleness —
2967
- // collapsing to '3?' made the cell flip 1+2Fable → 3? → 1+2Fable across every
2968
- // self-update (owner 2026-11-10: "the number of resets keeps changing").
2969
- if (notFresh && (text + '?').length <= RESETS_W) text += '?';
2970
- resetsCell = rpad(total > 0 && !notFresh ? green(text) : gray(text), RESETS_W);
3103
+ return days < 1 ? `~${Math.max(1, Math.round(days * 24))}h` : `~${Math.max(1, Math.round(days))}d`;
3104
+ };
3105
+ // Fit to EXACTLY w: full form, then drop the suffix, then n+'*'. Not-fresh:
3106
+ // append '?' only when it fits; otherwise gray carries the staleness (a
3107
+ // '1+2Fable' -> '3?' collapse flipped the count on every self-update,
3108
+ // owner 2026-11-10).
3109
+ const renderCell = (core, suffix, n, w, sticky) => {
3110
+ let text = core + suffix;
3111
+ if (text.length > w) text = core;
3112
+ if (text.length > w) text = `${n}*`.slice(0, w);
3113
+ if (notFresh && n > 0 && !sticky && (text + '?').length <= w) text += '?';
3114
+ return rpad(n > 0 && !notFresh ? green(text) : gray(text), w);
3115
+ };
3116
+ // MRst: model-scoped grants spell the model ('2Fable'), never a bare F
3117
+ // (owner 2026-11-10: "What does the letter F mean?"). Nothing -> a dim dash.
3118
+ const mCell = scoped > 0
3119
+ ? renderCell(`${scoped}Fable`, expirySuffix(scoped, soonestS), scoped, MRST_W, false)
3120
+ : rpad(gray('-'), MRST_W);
3121
+ // ARst: account-level count; scope-unknown grants show 'n?' when they are all there is.
3122
+ const unknownOnly = unknown > 0 && general === 0;
3123
+ const aN = unknownOnly ? unknown : general;
3124
+ const aCore = unknownOnly ? `${unknown}?` : `${general}`;
3125
+ const aCell = aN > 0
3126
+ ? renderCell(aCore, expirySuffix(aN, soonestG), aN, ARST_W, unknownOnly)
3127
+ : rpad(gray('0'), ARST_W);
3128
+ resetsCell = mCell + ' ' + aCell;
2971
3129
  }
2972
3130
 
2973
3131
  if (a.type === 'provider') {
@@ -3028,7 +3186,7 @@ export class TUI {
3028
3186
  const reserveFloor = this.am.scheduler?.weeklyReserveThreshold ?? 0.85;
3029
3187
  const scopedTags = [];
3030
3188
  for (const [fam, e] of Object.entries(q.scopedWeekly || {})) {
3031
- if (!e || e.isActive === false || e.utilization == null) continue;
3189
+ if (!e || e.utilization == null) continue;
3032
3190
  if (e.utilization < reserveFloor) continue;
3033
3191
  const Fam = fam.charAt(0).toUpperCase() + fam.slice(1);
3034
3192
  scopedTags.push(e.utilization >= exhaustedFloor
@@ -3199,13 +3357,25 @@ export class TUI {
3199
3357
  if (account.load) return account.load;
3200
3358
  if (!this.am?._loadSummary) return null;
3201
3359
  const now = Date.now();
3360
+ const base = w => {
3361
+ const load = this.am._loadSummary(account, w, now);
3362
+ const key = w === 15 * 60_000 ? 'last15m' : 'last1h';
3363
+ // A cloud row merges both pools' halves (rowEfficiencySummary): cache + served
3364
+ // from the serving pool's export (team-wide on that account), bounces / waits /
3365
+ // moves from this Mac's own events — the parts the cloud cannot see.
3366
+ const team = Boolean(account.remote && account._remoteLoad?.[key]?.efficiency);
3367
+ const efficiency = this.am.rowEfficiencySummary
3368
+ ? this.am.rowEfficiencySummary(account, key, now)
3369
+ : this.am._efficiencySummary(account, w, now);
3370
+ return team ? { ...load, efficiency, team: true } : { ...load, efficiency };
3371
+ };
3202
3372
  return {
3203
3373
  current: {
3204
3374
  inFlight: account.inFlight,
3205
3375
  activeWeight: account.activeWeight,
3206
3376
  },
3207
- last15m: this.am._loadSummary(account, 15 * 60 * 1000, now),
3208
- last1h: this.am._loadSummary(account, 60 * 60 * 1000, now),
3377
+ last15m: base(15 * 60 * 1000),
3378
+ last1h: base(60 * 60 * 1000),
3209
3379
  };
3210
3380
  }
3211
3381
 
@@ -3445,6 +3615,7 @@ export class TUI {
3445
3615
  if (hasPeak) {
3446
3616
  out.push(` ${bold('c')} GLM peak cap ${dim('— GLM accounts only, in GLM peak hours. Not Claude.')}`);
3447
3617
  }
3618
+ out.push(` ${bold('b')} Wait for busy account ${cyan(this._stickyBusyLabel())}`);
3448
3619
  // Where the per-account cap actually is.
3449
3620
  out.push(` ${dim('Usage cap for ONE account:')} ${bold('Esc')} ${dim('then')} ${bold('a')} ${dim('→')} ${bold('u')} ${dim('(Claude or GLM)')}`);
3450
3621
  const capped = this.am.accounts.filter(a => a.capUtilization != null && a.enabled !== false);
@@ -3457,7 +3628,7 @@ export class TUI {
3457
3628
  _renderFooter() {
3458
3629
  switch (this.mode) {
3459
3630
  case 'normal':
3460
- return ` ${bold('a')} Accounts ${bold('m')} Routing ${bold('y')} Capacity ${bold('p')} People ${bold('h')} Hide disabled ${dim('│')} ${bold('u')} Updates ${bold('r')} Restart ${bold('q')} Stop server`;
3631
+ return ` ${bold('a')} Accounts ${bold('m')} Routing ${bold('y')} Capacity ${bold('p')} People ${bold('h')} Hide disabled ${dim('│')} ${bold('u')} Updates ${bold('r')} Restart ${bold('q')} ${serviceMode() ? 'Close view' : 'Stop server'}`;
3461
3632
  case 'capacity': {
3462
3633
  const other = this.capacityWindow === 'wk' ? '5h session' : 'weekly (7d)';
3463
3634
  return ` ${bold('w')} Show ${cyan(other)} ${bold('Esc')} Back`;
@@ -0,0 +1,50 @@
1
+ // The cache figures of one response, as the tail of an activity line:
2
+ // `cache r180k w3k in1k` = tokens read from the prompt cache, written to it, and
3
+ // sent uncached. Whether the cache held is the one thing a request line did not
4
+ // say, and these three numbers decide it (#642, design by @kyjin).
5
+
6
+ // A figure that is not a count reads as zero rather than as a number nobody sent.
7
+ const count = (/** @type {any} */ n) => (Number.isFinite(n) && n > 0 ? Math.round(n) : 0);
8
+
9
+ // 1000 and up in whole k, smaller counts as they are.
10
+ const k = (/** @type {any} */ n) => {
11
+ const c = count(n);
12
+ return c >= 1000 ? `${Math.round(c / 1000)}k` : String(c);
13
+ };
14
+
15
+ // Share of the prompt read from the cache at or above which it held, and
16
+ // below which it missed; in between is partial.
17
+ const HIT_SHARE = 0.9;
18
+ const MISS_SHARE = 0.5;
19
+
20
+ // A prompt this big that neither read nor wrote the cache MISSED it — a cold GLM
21
+ // turn (z.ai reports no cache writes) must read MISS, not 'none' (2026-11-15).
22
+ const COLD_MISS_INPUT = 2000;
23
+
24
+ /**
25
+ * Whether the cache held: the share of the prompt that was read from it,
26
+ * r / (r + w + in). A small response that neither read nor wrote the cache is not
27
+ * a cached request and has no state to judge; a big one is a miss.
28
+ * @param {Record<string, any>|null|undefined} usage the response's usage object
29
+ * @returns {'hit'|'partial'|'miss'|'none'}
30
+ */
31
+ export function cacheUsageState(usage) {
32
+ if (!usage || typeof usage !== 'object') return 'none';
33
+ const r = count(usage.cache_read_input_tokens);
34
+ const w = count(usage.cache_creation_input_tokens);
35
+ if (r === 0 && w === 0) return count(usage.input_tokens) >= COLD_MISS_INPUT ? 'miss' : 'none';
36
+ const ratio = r / (r + w + count(usage.input_tokens));
37
+ return ratio >= HIT_SHARE ? 'hit' : ratio >= MISS_SHARE ? 'partial' : 'miss';
38
+ }
39
+
40
+ /**
41
+ * A miss ends with MISS: the TUI also paints it red, but its log file and the
42
+ * headless log carry no colour, so the word is what is left there to grep.
43
+ * @param {Record<string, any>|null|undefined} usage the response's usage object
44
+ * @returns {string} '' when the response carried no usage
45
+ */
46
+ export function cacheUsageLabel(usage) {
47
+ if (!usage || typeof usage !== 'object' || !Object.keys(usage).length) return '';
48
+ const label = `cache r${k(usage.cache_read_input_tokens)} w${k(usage.cache_creation_input_tokens)} in${k(usage.input_tokens)}`;
49
+ return cacheUsageState(usage) === 'miss' ? `${label} MISS` : label;
50
+ }