maxpool 1.24.81 → 1.24.83

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.
@@ -42,8 +42,11 @@
42
42
  * merely hours away. Dying-value redeems (R2) are exempt: an expiring card's
43
43
  * value cannot survive a wait.
44
44
  */
45
+ import { appliesTo as defaultAppliesTo } from './claude-reset-grants.js';
46
+ import { CLAIM_FRESH_MS } from './reset-timing.js';
47
+
45
48
  export const POLICY = {
46
- wallWeekly: 0.90, // weekly at 90% = at the wall
49
+ wallWeekly: 0.90, // weekly at 90% = at the wall (z.ai cards; UNCHANGED — pre-mortem #22)
47
50
  wallSes: 0.95, // 5h at 95% = at the wall
48
51
  dyingWindowMs: 24 * 3600 * 1000, // redeem-don't-save horizon
49
52
  dyingMinUtilWeekly: 0.30, // below this a dying weekly refill buys too little
@@ -52,6 +55,18 @@ export const POLICY = {
52
55
  freeRefillImmediate: 0.98, // R6: ...unless this full — never block the owner
53
56
  };
54
57
 
58
+ // Claude cedar_ember claims read their OWN wall (pre-mortem #22): 0.99, deliberately
59
+ // NOT POLICY.wallWeekly (0.90) which z.ai shares — moving the shared constant would
60
+ // redeem GLM cards at a different point. Claude benches at 0.999 (weeklyExhausted)
61
+ // and tiers critical at 0.95; 0.99 is "the weekly is effectively spent" for a claim.
62
+ export const CLAUDE_WALL_WEEKLY = 0.99;
63
+
64
+ // Claim freshness bound (C1, pre-mortem #5): the usage reading the claim decides on
65
+ // must be THIS fresh (≤ 5 min) — a reading taken before this sweep's probe (up to
66
+ // ~36 min stale after failed probes) would spend a reset right after a free refill.
67
+ // The value lives in the shared timing leaf; re-exported for existing callers.
68
+ export { CLAIM_FRESH_MS };
69
+
55
70
  function msLeft(t, now) { return t == null ? Infinity : t - now; }
56
71
 
57
72
  /** Decide the z.ai action. Returns {kind:'card', card, resetType} | null. */
@@ -97,34 +112,124 @@ export function decideZai(account, cards, now = Date.now()) {
97
112
  return null;
98
113
  }
99
114
 
100
- /** Decide the Claude action. Returns {kind:'grant', grant} | null. */
101
- export function decideClaude(account, listing, now = Date.now()) {
102
- if (account?.enabled === false) return null; // R0
103
- if (!listing || listing.error || listing.eligible === false) return null;
104
- if (listing.cooldownUntil != null && listing.cooldownUntil > now) return null;
105
- const grants = (listing.grants || [])
106
- .filter(g => g.usableNow && !g.paused && !g.expired)
107
- .sort((a, b) => (a.endsAt ?? Infinity) - (b.endsAt ?? Infinity));
108
- // The server names the next grant it would spend; honoring it avoids surprises.
109
- const chosen = grants.find(g => listing.nextGrantId == null || g.id === listing.nextGrantId) || grants[0];
110
- if (!chosen) return null;
115
+ /** Decide the Claude action. Returns {kind:'grant', grant, scope, reason} | null.
116
+ * Thin wrapper over decideClaudeWithReason for callers that only need the action. */
117
+ export function decideClaude(account, listing, now = Date.now(), opts = {}) {
118
+ return decideClaudeWithReason(account, listing, now, opts).action;
119
+ }
120
+
121
+ /** The ONE probe-send-time used to order a reading against a claim (finding 1):
122
+ * the SEND time of the last successful probe when the prober recorded it,
123
+ * else its apply time. A probe sent before a claim carries the pre-claim
124
+ * reading even when it lands after the claim. */
125
+ export function probeSentAt(q) {
126
+ return q?.lastProbeOkSentAt ?? q?.lastProbeOkAt ?? null;
127
+ }
111
128
 
129
+ /** Decide the Claude action AND say why when there is none.
130
+ * Returns { action: {kind:'grant', grant, scope, giveBack?, reason} | null,
131
+ * reason: string | null } — `reason` names the rejection when action
132
+ * is null (C5: the skip log prints exactly this), or the claim reason.
133
+ *
134
+ * C1 (amended, 2026-11-10): the claim is decided per grant by appliesTo (D1),
135
+ * NEVER by the server's nextGrantId order alone (pre-mortem #8: a Fable 429 could
136
+ * pick the general grant through that order). Freshness gates (pre-mortem #5):
137
+ * lastProbeOkAt within CLAIM_FRESH_MS, unified7dReset in the future (a window
138
+ * that just rolled means 0% used — don't spend on it), and a probe SENT after
139
+ * any claim we already made (no second claim for one wall).
140
+ * D4: a dying-value claim (not at the wall) waits when the free refill of the
141
+ * limit it clears is < 24h away; a wall claim never waits (limit effectively full).
142
+ * Session-only grants are NEVER claimed (owner 2026-10-30). */
143
+ export function decideClaudeWithReason(account, listing, now = Date.now(), opts = {}) {
144
+ const skip = reason => ({ action: null, reason });
145
+ if (account?.enabled === false) return skip('account disabled'); // R0
146
+ if (!listing) return skip('no listing');
147
+ // Only a FRESH listing may claim.
148
+ if (listing.error) return skip('listing failed');
149
+ if (listing.stale) return skip('listing stale');
150
+ if (listing.eligible === false) return skip('not eligible');
151
+ if (listing.cooldownUntil != null && listing.cooldownUntil > now) return skip('cooldown');
112
152
  const q = account.quota || {};
113
- const wkUtil = q.unified7d ?? null;
114
- const clearsWk = chosen.clears?.includes('seven_day');
115
153
 
116
- // Owner, 2026-10-30: "never spend resets on session limits, only on weekly."
117
- // A grant that clears ONLY the session window is never spent, at wall or dying.
118
- if (!clearsWk) return null;
154
+ // FRESHNESS (pre-mortem #5): decide on THIS sweep's probe, never an aging one.
155
+ // `freshNow` defaults to the real clock; tests may inject a frozen one.
156
+ const freshNow = opts.now ?? now;
157
+ const lastProbeOkAt = q.lastProbeOkAt ?? null;
158
+ if (lastProbeOkAt == null || freshNow - lastProbeOkAt > CLAIM_FRESH_MS) return skip('probe not fresh');
159
+ // A weekly window that already rolled over is empty — a claim right now buys
160
+ // nothing (spent right after the free refill, at 0% used).
161
+ if (q.unified7dReset != null && q.unified7dReset <= freshNow) return skip('weekly window elapsed');
162
+ // No second claim for one wall: a probe SENT after the last claim must land first.
163
+ const lastClaimAt = account.lastClaimAt ?? null;
164
+ if (lastClaimAt != null && probeSentAt(q) <= lastClaimAt) return skip('already claimed for this wall');
119
165
 
120
- const atWall = wkUtil != null && wkUtil >= POLICY.wallWeekly;
166
+ // Per-grant applicability (D1 via appliesTo), soonest-expiring first among
167
+ // SAME-scope grants. Grants with unknown scope (appliesTo []) are never chosen.
168
+ const held = (listing.grants || []).filter(g => g && !g.paused && !g.expired
169
+ && (g.endsAt == null || g.endsAt > now) && (g.resetsLeft ?? 1) > 0);
170
+ if (!held.length) return skip('no grant held');
171
+ const grants = held
172
+ .filter(g => g.usableNow !== false && !g.blocking && !g.unavailableReason)
173
+ .sort((a, b) => (a.endsAt ?? Infinity) - (b.endsAt ?? Infinity));
174
+ if (!grants.length) return skip('grant not usable');
175
+
176
+ const appliesTo = opts.appliesTo || defaultAppliesTo;
177
+ const wkUtil = q.unified7d ?? null;
178
+ const weeklyWall = wkUtil != null && wkUtil >= CLAUDE_WALL_WEEKLY;
179
+ // D4: the free refill of a limit < 24h away makes a not-at-the-wall spend redundant.
180
+ const refillSoon = resetAt => resetAt != null && msLeft(resetAt, now) < POLICY.freeRefillWaitMs;
181
+ let reason = null;
182
+ const note = r => { if (reason == null) reason = r; };
121
183
 
122
- // use_requires_limit: some grants may only fire AT the wall (server-enforced).
123
- const dying = msLeft(chosen.endsAt, now) < POLICY.dyingWindowMs
124
- && wkUtil != null && wkUtil >= POLICY.dyingMinUtilWeekly; // weekly window only (owner 2026-10-30)
125
- // A requires-limit grant cannot be redeemed pre-wall even for dying value.
126
- if (atWall || (dying && !chosen.requiresLimit)) {
127
- return { kind: 'grant', grant: chosen, reason: atWall ? 'at wall' : 'dying value' };
184
+ // GENERAL (clears the general weekly, or gives_back_weekly): claim at the wall.
185
+ // "Weekly reached" is the fresh unified7d reading ONLY — never the generic
186
+ // `rejected` status (that conflates the 5h window and per-model caps; pre-mortem #8).
187
+ const general = grants.filter(g => appliesTo(g).includes('general'));
188
+ for (const g of general) {
189
+ const giveBack = g.givesBackWeekly === true && !(Array.isArray(g.clears) && g.clears.includes('seven_day'));
190
+ if (weeklyWall) {
191
+ // A give-back-only grant (no seven_day in clears) returns only what the model
192
+ // used — the claim snapshots unified7d so the next probe measures the drop.
193
+ return { action: { kind: 'grant', grant: g, scope: 'general', giveBack, reason: 'weekly at wall' }, reason: 'weekly at wall' };
194
+ }
195
+ const dying = msLeft(g.endsAt, now) < POLICY.dyingWindowMs
196
+ && wkUtil != null && wkUtil >= POLICY.dyingMinUtilWeekly; // weekly window only (owner 2026-10-30)
197
+ if (!dying) continue;
198
+ if (g.requiresLimit) { note('requires limit'); continue; }
199
+ if (refillSoon(q.unified7dReset)) { note('free refill <24h away'); continue; }
200
+ return { action: { kind: 'grant', grant: g, scope: 'general', giveBack, reason: 'dying value' }, reason: 'dying value' };
128
201
  }
129
- return null;
202
+ if (weeklyWall && !general.length) note('general not held');
203
+
204
+ // MODEL-SCOPED (D3): claim when THAT model's probe-measured weekly bucket is at
205
+ // its wall. `reactive` entries (a 429's authoritative-high mark) do NOT count —
206
+ // only the probe's own measurement (pre-mortem #3/#8: a scoped 429 classified by
207
+ // guesswork must not spend a reset). D4 dying-value is evaluated per bucket
208
+ // BEFORE the below-the-wall skip (it applies exactly there).
209
+ const scopedFams = new Set();
210
+ for (const g of grants) for (const s of appliesTo(g)) if (s !== 'general' && s !== 'session') scopedFams.add(s);
211
+ for (const fam of scopedFams) {
212
+ const e = q.scopedWeekly?.[fam];
213
+ const measured = e && e.reactive !== true && e.utilization != null;
214
+ if (!measured) continue;
215
+ const cand = grants.find(g => appliesTo(g).includes(fam));
216
+ if (!cand) continue;
217
+ if (e.utilization >= CLAUDE_WALL_WEEKLY) {
218
+ return { action: { kind: 'grant', grant: cand, scope: fam, reason: `${fam} weekly at wall` }, reason: `${fam} weekly at wall` };
219
+ }
220
+ const dying = msLeft(cand.endsAt, now) < POLICY.dyingWindowMs
221
+ && e.utilization >= POLICY.dyingMinUtilWeekly;
222
+ if (!dying) continue;
223
+ if (cand.requiresLimit) { note('requires limit'); continue; }
224
+ if (refillSoon(e.resetAt)) { note('free refill <24h away'); continue; }
225
+ return { action: { kind: 'grant', grant: cand, scope: fam, reason: 'dying value' }, reason: 'dying value' };
226
+ }
227
+ // A scoped bucket measured at the wall with no grant for it.
228
+ for (const [fam, e] of Object.entries(q.scopedWeekly || {})) {
229
+ if (e && e.reactive !== true && e.utilization != null && e.utilization >= CLAUDE_WALL_WEEKLY
230
+ && !grants.some(g => appliesTo(g).includes(fam))) note(`${fam} not held`);
231
+ }
232
+
233
+ // Session-only grants: NEVER claimed (owner 2026-10-30) — falls through.
234
+ return skip(reason ?? 'below wall');
130
235
  }
@@ -0,0 +1,27 @@
1
+ // Reset-machinery timing constants — ONE leaf module (judge note, 2026-11-10: the
2
+ // 5*60_000 literal had spread across account-manager.js, prober.js and tui.js).
3
+ // Leaf by construction: imports nothing, so every consumer can share it.
4
+
5
+ // A2: grants are listed at most this often per account (the call shares the
6
+ // per-IP rate-limited /api/oauth/usage endpoint with the usage probe).
7
+ export const GRANTS_EVERY_MS = 5 * 60_000;
8
+ // A stale/errored listing retries at this shorter cadence so a transient blank
9
+ // recovers fast.
10
+ export const GRANTS_RETRY_MS = 30_000;
11
+ // C3: the cap lift survives listing failures this long before ending.
12
+ export const LIFT_GRACE_MS = 10 * 60_000;
13
+ // C2: a stored listing may serve a reactive claim up to this age.
14
+ export const CLAIM_STALE_LISTING_MAX_MS = 6 * 3600_000;
15
+ // C6: a FAILED Claude claim backs off per (account, grantId) on these steps,
16
+ // capped at CLAIM_BACKOFF_CAP_MS (matches the z.ai card-redeem schedule).
17
+ export const CLAIM_BACKOFF_STEPS_MS = [15 * 60_000, 30 * 60_000, 60 * 60_000];
18
+ export const CLAIM_BACKOFF_CAP_MS = 6 * 3600_000;
19
+ // B3: a listing older than LISTING_FRESH_MULT × GRANTS_EVERY_MS reads not-fresh
20
+ // in the TUI and cannot start a cap lift (C3).
21
+ export const LISTING_FRESH_MULT = 3;
22
+ export const LISTING_FRESH_MS = LISTING_FRESH_MULT * GRANTS_EVERY_MS;
23
+ // C1: the usage reading a claim decides on must be this fresh.
24
+ export const CLAIM_FRESH_MS = 5 * 60_000;
25
+ // C2: a reactive claim runs off the request path and gives up after this long,
26
+ // so a hung profile GET / POST can never hold the claim lock for minutes.
27
+ export const REACTIVE_CLAIM_TIMEOUT_MS = 10_000;
package/src/server.js CHANGED
@@ -473,6 +473,8 @@ export function createProxyServer(accountManager, config, hooks = {}) {
473
473
  return;
474
474
  }
475
475
 
476
+ markFirstByte(res, () => hooks.onRequestFirstByte?.(reqId));
477
+
476
478
  // The request is now TRACKED (onRequestStart accepted → tui.active +
477
479
  // restartController.activeRequests both hold it). EVERYTHING that can throw —
478
480
  // including body buffering — must run inside the try whose finally fires
@@ -572,6 +574,11 @@ export function createProxyServer(accountManager, config, hooks = {}) {
572
574
  // belonged to, so a turn's account is exact instead of a timestamp guess.
573
575
  conversation: requestInfo?.conversationHash || null,
574
576
  sessionKey: headerValue(req.headers, 'x-maxpool-session') || null,
577
+ // D (2026-11-10): the first REAL upstream byte (the fetch resolve), for
578
+ // the TUI's ttfb= tag. Prefer this over the client-write stamp: a queued
579
+ // request's heartbeat commits writes LONG before upstream answers.
580
+ firstUpstreamByteAt: requestInfo?._firstUpstreamByteAt ?? null,
581
+ upstreamForwardStartAt: requestInfo?._upstreamForwardStartAt ?? null,
575
582
  });
576
583
  }
577
584
  } catch (err) {
@@ -1240,6 +1247,10 @@ async function forwardRequest(
1240
1247
  const streamGraceTimer = armStreamForwardGrace(res, requestInfo, queueConfig, accountManager);
1241
1248
  try {
1242
1249
  let upstreamRes;
1250
+ // D (finding 5): THIS attempt's upstream forward start. ttfb is measured from
1251
+ // here — never from the request's arrival, which includes queue wait and the
1252
+ // earlier attempts of a failover.
1253
+ const upstreamForwardStartAt = Date.now();
1243
1254
  try {
1244
1255
  upstreamRes = await fetch(upstreamUrl, {
1245
1256
  method,
@@ -1254,6 +1265,14 @@ async function forwardRequest(
1254
1265
  // a re-forward (failover) re-arms for the REMAINING time to the shared deadline.
1255
1266
  if (streamGraceTimer) clearTimeout(streamGraceTimer);
1256
1267
  }
1268
+ // D (2026-11-10, finding 5): the upstream ANSWERED (fetch resolved — a failed
1269
+ // or timed-out fetch threw above and never reaches here). Stamped on EVERY
1270
+ // attempt together with that attempt's forward start, so after a failover the
1271
+ // pair describes the attempt that actually served (a quick 429 then a slow
1272
+ // second account reports the slow one). The queue heartbeat never stamps it.
1273
+ // Log-only: no setting reads it.
1274
+ requestInfo._upstreamForwardStartAt = upstreamForwardStartAt;
1275
+ requestInfo._firstUpstreamByteAt = Date.now();
1257
1276
  // Response arrived — the pre-response leak window is over. Stop guarding for
1258
1277
  // client-disconnect via abort (streamResponse handles mid-stream disconnects).
1259
1278
  res.off('close', onClientClose);
@@ -1438,6 +1457,27 @@ async function forwardRequest(
1438
1457
  fingerprint: rateLimit.scope === 'unknown' ? rateLimit.fingerprint : null,
1439
1458
  modelScope: rateLimit.modelScope || null,
1440
1459
  });
1460
+ // C2 REACTIVE CLAIM (2026-11-10): a 429 on the serving lease claims a held
1461
+ // reset AT ONCE, not after a full prober sweep. Only on the lease's LOCAL
1462
+ // account holding the credential (never a mirror row — its 429 is the
1463
+ // cloud's, and the cloud claims on its own side). A weekly-CLASS 429 (the
1464
+ // 7d header itself ≥ the wall) claims a general grant; a scoped 429 claims
1465
+ // only if the probe MEASURED that model's bucket at the wall. Uses the
1466
+ // current token; a 401 defers to the sweep.
1467
+ // FIRE-AND-FORGET (finding 4): the claim (profile GET + POST) runs OFF the
1468
+ // user's request — this request fails over at once; the claim unbenches the
1469
+ // account for the NEXT request when it lands. Never awaited here.
1470
+ if (accountManager.tryReactiveClaim && !account.remote) {
1471
+ const weeklyHdr = Number(rateLimitHeaders['anthropic-ratelimit-unified-7d-utilization']);
1472
+ const weeklyWall = Number.isFinite(weeklyHdr) && weeklyHdr >= 0.999;
1473
+ const kind = weeklyWall ? 'weekly' : (rateLimit.modelScope || null);
1474
+ if (kind) {
1475
+ try {
1476
+ Promise.resolve(accountManager.tryReactiveClaim(account, { kind, weeklyUtil: weeklyHdr }))
1477
+ .catch(() => { /* best-effort; the sweep retries */ });
1478
+ } catch { /* a synchronous throw must not break the failover below */ }
1479
+ }
1480
+ }
1441
1481
  accountManager.releaseAccount(lease, { status: 429, error: rateLimit.modelScope ? 'model_rate_limited' : 'rate_limited' });
1442
1482
 
1443
1483
  if (logDir) {
@@ -4045,6 +4085,28 @@ function prepareRuntimeProviders(accountManager, headers) {
4045
4085
  }
4046
4086
  }
4047
4087
 
4088
+ /**
4089
+ * Call `onFirst` once, the first time anything is flushed to the client (headers or
4090
+ * body). This is the moment a waiting client first sees life: Claude Code reports
4091
+ * "Waiting for API response" when nothing arrives for ~20s, so the request log carries
4092
+ * time-to-first-byte separately from the total duration. Pure observer: the wrapped
4093
+ * methods behave identically, and a throwing callback never affects the response.
4094
+ */
4095
+ export function markFirstByte(res, onFirst) {
4096
+ let fired = false;
4097
+ const fire = () => { if (fired) return; fired = true; try { onFirst(); } catch { /* observer only */ } };
4098
+ for (const m of ['write', 'end', 'flushHeaders']) {
4099
+ const orig = res[m];
4100
+ if (typeof orig !== 'function') continue;
4101
+ res[m] = function (...args) {
4102
+ // end() with no body and write('') send no bytes of their own, but end() still
4103
+ // flushes the headers, so any call to these three means bytes are on the way.
4104
+ fire();
4105
+ return orig.apply(this, args);
4106
+ };
4107
+ }
4108
+ }
4109
+
4048
4110
  function headerValue(headers, name) {
4049
4111
  const lname = name.toLowerCase();
4050
4112
  let value = headers[lname];
package/src/tui.js CHANGED
@@ -5,6 +5,9 @@ import { fetchProfile, loginOAuth, tokenFingerprint, isLoginCancelled } from './
5
5
  import { appendEventLog, setConsoleStdoutSuppressed } from './event-log.js';
6
6
  import { sendLoginToPool } from './cloud-login.js';
7
7
  import { describeLinkError } from './pool-link.js';
8
+ import { normalizeRemoteResets } from './account-manager.js';
9
+ import { appliesTo } from './claude-reset-grants.js';
10
+ import { LISTING_FRESH_MS } from './reset-timing.js';
8
11
 
9
12
  // ── ANSI helpers ─────────────────────────────────────────────
10
13
 
@@ -571,6 +574,12 @@ export class TUI {
571
574
  if (r) r.account = info.account;
572
575
  }
573
576
 
577
+ /** First response byte reached the client (server.js markFirstByte). */
578
+ onRequestFirstByte(id) {
579
+ const r = this.active.get(id);
580
+ if (r && r.firstByteAt == null) r.firstByteAt = Date.now();
581
+ }
582
+
574
583
  onRequestEnd(id, info) {
575
584
  const r = this.active.get(id);
576
585
  this.active.delete(id);
@@ -593,7 +602,19 @@ export class TUI {
593
602
  const provTag = (isProvider && r?.sessionKey) ? ` [sess ${String(r.sessionKey).slice(0, 8)}]` : '';
594
603
  const cv = info.conversation || r?.conversation;
595
604
  const cvTag = cv ? ` [cv ${cv.slice(0, 8)} ${String(info.sessionKey || r?.sessionKey || '').slice(0, 8)}]` : '';
596
- this._addLog(`${who}${info.method} ${info.path} → ${acct} (${info.status}, ${dur}s)${provTag}${cvTag}`);
605
+ // ttfb LAST: the UPSTREAM wait of the attempt that served — its fetch resolve
606
+ // minus its own forward start (D, 2026-11-10). Never measured from arrival:
607
+ // that would add queue wait and earlier failover attempts. Falls back to the
608
+ // client-write stamp from arrival when no upstream pair exists (a request that
609
+ // never reached upstream, the link path). Omitted when no byte was ever sent.
610
+ let ttfbMs = null;
611
+ if (info.firstUpstreamByteAt != null && info.upstreamForwardStartAt != null) {
612
+ ttfbMs = info.firstUpstreamByteAt - info.upstreamForwardStartAt;
613
+ } else if (r?.firstByteAt != null) {
614
+ ttfbMs = r.firstByteAt - r.started;
615
+ }
616
+ 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}`);
597
618
  }
598
619
 
599
620
  _addLog(msg) {
@@ -2287,12 +2308,33 @@ export class TUI {
2287
2308
  const live = a?.poolBenchedBy ? this.am.remoteRowFor?.(a) : null;
2288
2309
  if (live) {
2289
2310
  const r = live.row;
2311
+ // B2 (2026-11-10): the overlay also carries the cloud twin's RESETS via the
2312
+ // same normalizeRemoteResets the mirror rows use. When the twin's snapshot is
2313
+ // MISSING the reset fields (one failed link poll — the "2 → gone → back"
2314
+ // flicker through a new path), keep the PREVIOUS overlay marked not-fresh,
2315
+ // never 0.
2316
+ let resetsOverlay;
2317
+ if (r.resetGrants != null || r.resetCards != null || r.capLift != null) {
2318
+ const nr = normalizeRemoteResets(r);
2319
+ resetsOverlay = { resetGrants: nr.resetGrants, resetCards: nr.resetCards, capLift: nr.capLift };
2320
+ } else {
2321
+ const prev = a._twinResetsOverlay || {};
2322
+ resetsOverlay = {
2323
+ resetGrants: prev.resetGrants ? { ...prev.resetGrants, stale: true } : null,
2324
+ resetCards: prev.resetCards ? { ...prev.resetCards, stale: true } : null,
2325
+ capLift: prev.capLift ?? null,
2326
+ };
2327
+ }
2328
+ a._twinResetsOverlay = resetsOverlay; // remember for the next frame's fallback
2290
2329
  a = Object.assign(Object.create(Object.getPrototypeOf(a)), a, {
2291
2330
  quota: { ...a.quota, ...(r.quota || {}) },
2292
2331
  inFlight: r.inFlight ?? 0,
2293
2332
  completedRequests: r.completedRequests ?? a.completedRequests,
2294
2333
  refreshDead: Boolean(r.refreshDead),
2295
2334
  rateLimitedUntil: r.rateLimitedUntil ? Date.parse(r.rateLimitedUntil) : null,
2335
+ resetGrants: resetsOverlay.resetGrants,
2336
+ resetCards: resetsOverlay.resetCards,
2337
+ capLift: resetsOverlay.capLift,
2296
2338
  });
2297
2339
  }
2298
2340
 
@@ -2428,41 +2470,82 @@ export class TUI {
2428
2470
  // column showing 0 or the count). z.ai cards + Claude cedar_ember grants,
2429
2471
  // not-yet-expired, on ENABLED accounts only: a disabled account's resets are
2430
2472
  // not ours to spend, and showing a count there would imply otherwise.
2473
+ //
2474
+ // B3 grammar (2026-11-10), rendered width EXACTLY RESETS_W (6):
2475
+ // general n~Xd e.g. 2~119d
2476
+ // scoped nF~Xd e.g. 2F~4d (model-scoped, e.g. Fable)
2477
+ // mixed g+sF e.g. 1+2F
2478
+ // unknown n? scope unknown — count shown, never claimed
2479
+ // not-fresh = gray + trailing '?' (stale listing, dead account, old check)
2480
+ // Count = SUM of resetsLeft over spendable grants + live cards (not entries).
2481
+ // Overflow order: drop the suffix first, then collapse to total+'*'.
2482
+ // Expiry is checked LIVE here: grants endsAt>now, cards !expired &&
2483
+ // (expiresAt==null || expiresAt>now) — a frozen `expired` flag never keeps a
2484
+ // dead reset on screen (pre-mortem #18).
2431
2485
  let resetsCell;
2432
2486
  {
2433
- let n = 0;
2487
+ const now = Date.now();
2488
+ let general = 0; // resetsLeft of general-weekly grants + live cards
2489
+ let scoped = 0; // resetsLeft of model-scoped grants (tagged F)
2490
+ let unknown = 0; // resetsLeft of scope-unknown grants
2491
+ let soonest = Infinity;
2434
2492
  if (a.enabled !== false) {
2493
+ const liveCard = c => c && !c.expired && (c.expiresAt == null || c.expiresAt > now);
2435
2494
  const rc = a.resetCards;
2436
- if (rc) n += (rc.fiveHour || []).filter(c => !c.expired).length
2437
- + (rc.weekly || []).filter(c => !c.expired).length;
2438
- const rg = a.resetGrants;
2439
- if (rg?.eligible) n += (rg.grants || []).filter(g => g.usableNow && !g.expired).length;
2440
- }
2441
- // Expiry rides with the count when it's tight: "1~2d" = 1 reset, expires in ~2 days.
2442
- // Claude grants end mid-Oct (weeks out) — a day-count would be noise; only the
2443
- // SOONEST-expiring card under 7 days gets a suffix, so the column stays narrow
2444
- // and the number it carries still answers "use it or lose it?".
2445
- let suffix = '';
2446
- if (n > 0) {
2447
- let soonest = Infinity;
2448
- const rc = a.resetCards;
2449
- if (rc) for (const c of [...(rc.fiveHour || []), ...(rc.weekly || [])]) {
2450
- if (!c.expired && c.expiresAt != null && c.expiresAt < soonest) soonest = c.expiresAt;
2495
+ if (rc) {
2496
+ for (const c of [...(rc.fiveHour || []), ...(rc.weekly || [])]) {
2497
+ if (liveCard(c)) { general += 1; if (c.expiresAt != null && c.expiresAt < soonest) soonest = c.expiresAt; }
2498
+ }
2451
2499
  }
2452
2500
  const rg = a.resetGrants;
2453
- if (rg?.eligible) for (const g of (rg.grants || [])) {
2454
- if (g.usableNow && !g.expired && g.endsAt != null && g.endsAt < soonest) soonest = g.endsAt;
2455
- }
2456
- // Expiry is ALWAYS shown when a reset exists (owner 2026-10-01: "what is the
2457
- // proper way to communicate if we have a reset, and when does it expire" —
2458
- // both facts, always). Hours under 24h, days at or above.
2459
- const days = soonest === Infinity ? null : (soonest - Date.now()) / 86400_000;
2460
- if (days != null) {
2461
- suffix = days < 1 ? '~' + Math.max(1, Math.round(days * 24)) + 'h'
2462
- : '~' + Math.max(1, Math.round(days)) + 'd';
2501
+ if (rg?.eligible !== false && rg) {
2502
+ for (const g of (rg.grants || [])) {
2503
+ if (!g || g.usableNow === false || g.paused || g.expired) continue;
2504
+ if (g.endsAt != null && g.endsAt <= now) continue; // live expiry
2505
+ // The ONE shared scope answer (A1) — display reads the same
2506
+ // classification as claim and lift.
2507
+ const scopes = appliesTo(g);
2508
+ const hasGeneral = scopes.includes('general');
2509
+ const hasScoped = scopes.some(x => x !== 'general' && x !== 'session');
2510
+ const left = Math.max(0, Math.floor(g.resetsLeft ?? 1));
2511
+ if (hasGeneral) general += left;
2512
+ else if (hasScoped) scoped += left;
2513
+ else unknown += left;
2514
+ if (g.endsAt != null && g.endsAt < soonest) soonest = g.endsAt;
2515
+ }
2463
2516
  }
2464
2517
  }
2465
- resetsCell = rpad(n > 0 ? green(String(n) + suffix) : gray('0'), RESETS_W);
2518
+ const total = general + scoped + unknown;
2519
+ // Not-fresh (pre-mortem #15): stale listing, a dead account, or a check older
2520
+ // than 3x the grants cadence. An account that stopped being probed never gets
2521
+ // re-listed, so checkedAt alone would show a green count forever.
2522
+ const freshGrants = a.resetGrants && a.resetGrants.stale !== true
2523
+ && (a.resetGrants.checkedAt == null || now - a.resetGrants.checkedAt <= LISTING_FRESH_MS);
2524
+ const freshCards = !a.resetCards || (a.resetCards.stale !== true
2525
+ && (a.resetCards.checkedAt == null || now - a.resetCards.checkedAt <= LISTING_FRESH_MS));
2526
+ const notFresh = total > 0 && (!freshGrants || !freshCards || a.refreshDead || a.subscriptionGone);
2527
+
2528
+ // Core text per the grammar.
2529
+ let core;
2530
+ if (total === 0) core = '0';
2531
+ else if (unknown > 0 && general === 0 && scoped === 0) core = `${unknown}?`;
2532
+ else if (general > 0 && scoped > 0) core = `${general}+${scoped}F`;
2533
+ else if (scoped > 0) core = `${scoped}F`;
2534
+ else core = `${general}`;
2535
+
2536
+ // Expiry suffix (owner 2026-10-01: always show when a reset exists).
2537
+ let suffix = '';
2538
+ if (total > 0 && soonest !== Infinity) {
2539
+ const days = (soonest - now) / 86400_000;
2540
+ suffix = days < 1 ? `~${Math.max(1, Math.round(days * 24))}h` : `~${Math.max(1, Math.round(days))}d`;
2541
+ }
2542
+
2543
+ // Fit to EXACTLY RESETS_W: full form, then drop the suffix, then total+'*'.
2544
+ let text = core + suffix;
2545
+ if (text.length > RESETS_W) text = core;
2546
+ if (text.length > RESETS_W) text = `${total}*`.slice(0, RESETS_W);
2547
+ if (notFresh) text = (text + '?').length <= RESETS_W ? text + '?' : `${total}?`.slice(0, RESETS_W);
2548
+ resetsCell = rpad(total > 0 && !notFresh ? green(text) : gray(text), RESETS_W);
2466
2549
  }
2467
2550
 
2468
2551
  if (a.type === 'provider') {
@@ -60,7 +60,10 @@ function normalizeCards(arr, now = Date.now()) {
60
60
  }
61
61
 
62
62
  /** List this account's available reset cards. Zero-spend read.
63
- * Returns { fiveHour: [], weekly: [] } | { error }. */
63
+ * Returns { fiveHour: [], weekly: [] } | { error }.
64
+ * DRIFT (A4, 2026-11-10): a 200 whose expected arrays are missing, or whose raw
65
+ * entries all vanish in normalization (renamed field), is reported as an ERROR —
66
+ * never as an empty "good" list that overwrites the last known cards. */
64
67
  export async function listResetCards(token, { signal, fetchImpl = fetch } = {}) {
65
68
  if (!token) return { error: 'no token' };
66
69
  try {
@@ -74,11 +77,22 @@ export async function listResetCards(token, { signal, fetchImpl = fetch } = {})
74
77
  return { error: `bad_envelope${data?.code != null ? ' code=' + data.code : ''}` };
75
78
  }
76
79
  const now = Date.now();
77
- return {
78
- fiveHour: normalizeCards(data.data.fiveHourResets, now),
79
- weekly: normalizeCards(data.data.weekResets, now),
80
- source: 'zai',
81
- };
80
+ const fiveHour = normalizeCards(data.data.fiveHourResets, now);
81
+ const weekly = normalizeCards(data.data.weekResets, now);
82
+ // Drift guard: the raw arrays are the expected shape; if either is missing, or
83
+ // raw entries exist but none survived normalization, the schema moved. An
84
+ // `available:false` entry is LEGITIMATELY dropped by normalizeCards — only
85
+ // entries that claim to be available count toward the drift check.
86
+ const rawFive = Array.isArray(data.data.fiveHourResets) ? data.data.fiveHourResets : null;
87
+ const rawWeek = Array.isArray(data.data.weekResets) ? data.data.weekResets : null;
88
+ if (rawFive == null || rawWeek == null) {
89
+ return { error: 'drift: expected arrays missing' };
90
+ }
91
+ const availableRaw = [...rawFive, ...rawWeek].filter(c => c && c.available !== false).length;
92
+ if (availableRaw > 0 && fiveHour.length + weekly.length === 0) {
93
+ return { error: `drift: ${availableRaw} raw entr${availableRaw === 1 ? 'y' : 'ies'}, 0 kept` };
94
+ }
95
+ return { fiveHour, weekly, source: 'zai' };
82
96
  } catch (err) {
83
97
  return { error: err.message || String(err) };
84
98
  }