maxpool 1.24.66 → 1.24.68

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "maxpool",
3
- "version": "1.24.66",
3
+ "version": "1.24.68",
4
4
  "description": "Multi-account Claude Code proxy with adaptive, rate-aware load balancing across Claude accounts",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,8 +1,8 @@
1
1
  import { refreshAccessToken, isTokenExpiringSoon, modelFamily, tokenFingerprint } from './oauth.js';
2
- import { computeDuplicates, credentialFp } from './pool-link.js';
2
+ import { computeDuplicates, credentialFp, accountIdentity, remoteAccountIdentity } from './pool-link.js';
3
3
  import { appendEventLog } from './event-log.js';
4
4
  import { hourFloor } from './outside-usage.js';
5
- import { CapacityLedger, TANK_MIN_UTIL } from './capacity-ledger.js';
5
+ import { CapacityLedger, TANK_MIN_UTIL, mergeCycles, tankStatsFromCycles } from './capacity-ledger.js';
6
6
 
7
7
  // Nominal window length per kind — mirrors capacity-ledger's WINDOW_MS (kept here as a
8
8
  // local table so account-manager does not import a private constant).
@@ -386,6 +386,35 @@ function parseResetHeader(value) {
386
386
  * also logs once so a hand-edited config doesn't silently fail open (a NaN cap makes
387
387
  * every `util >= cap` comparison false = uncapped, with no error anywhere).
388
388
  */
389
+ const PLAN_CADENCE_MONTHS = { monthly: 1, quarterly: 3, annual: 12, annually: 12, yearly: 12 };
390
+
391
+ /** Optional per-account plan config: { tier, cadence, priceUsd, billingMonths }.
392
+ * Display-only (Capacity page Plan column + $/1M). Tolerant: anything malformed → null,
393
+ * never a guessed value. billingMonths defaults from the cadence word. */
394
+ export function sanitizePlan(plan) {
395
+ if (!plan || typeof plan !== 'object') return null;
396
+ const priceUsd = Number(plan.priceUsd);
397
+ const cadence = typeof plan.cadence === 'string' ? plan.cadence.toLowerCase() : null;
398
+ let billingMonths = Number(plan.billingMonths);
399
+ if (!(billingMonths > 0)) billingMonths = PLAN_CADENCE_MONTHS[cadence] || NaN;
400
+ const out = {
401
+ tier: typeof plan.tier === 'string' ? plan.tier.slice(0, 40) : null,
402
+ cadence,
403
+ priceUsd: priceUsd >= 0 && Number.isFinite(priceUsd) ? priceUsd : null,
404
+ billingMonths: billingMonths > 0 ? billingMonths : null,
405
+ };
406
+ return (out.tier || out.priceUsd != null) ? out : null;
407
+ }
408
+
409
+ /** Default plan of every Anthropic (OAuth) account when none is configured (owner 2026-11-06). */
410
+ export const DEFAULT_CLAUDE_PLAN = { tier: 'Claude Max', cadence: 'monthly', priceUsd: 200, billingMonths: 1 };
411
+
412
+ /** Monthly-normalized USD from a plan, or null when it cannot be derived. */
413
+ export function planMonthlyUSD(plan) {
414
+ const p = sanitizePlan(plan);
415
+ return (p && p.priceUsd != null && p.billingMonths) ? p.priceUsd / p.billingMonths : null;
416
+ }
417
+
389
418
  function _sanitizeCap(value, name, quiet = false) {
390
419
  if (value == null) return null;
391
420
  const n = Number(value);
@@ -516,6 +545,7 @@ export class AccountManager {
516
545
  capRampStart: _sanitizeRampStart(acct.capRampStart, acct.name),
517
546
  model: acct.model || null,
518
547
  modelMap: acct.modelMap || null,
548
+ plan: sanitizePlan(acct.plan),
519
549
  stripBetaHeaders: Boolean(acct.stripBetaHeaders),
520
550
  runtime: Boolean(acct.runtime),
521
551
  enabled: acct.enabled !== false,
@@ -3718,19 +3748,24 @@ export class AccountManager {
3718
3748
 
3719
3749
  monthlyPriceFor(account) {
3720
3750
  // A cloud mirror carries no credential, so this pool cannot read its subscription;
3721
- // the cloud pool reads it and exports it per row (remoteMonthlyPriceUSD).
3722
- if (account?.remote && account.remoteMonthlyPriceUSD != null) return account.remoteMonthlyPriceUSD;
3723
- if (account?.type === 'provider') return account.monthlyPriceUSD ?? null;
3751
+ // the cloud pool reads it and exports it per row (remotePlan / remoteMonthlyPriceUSD).
3752
+ if (account?.remote) {
3753
+ const rp = planMonthlyUSD(account.remotePlan);
3754
+ if (rp != null) return rp;
3755
+ if (account.remoteMonthlyPriceUSD != null) return account.remoteMonthlyPriceUSD;
3756
+ }
3757
+ // GLM: the live z.ai subscription value (renewPrice ÷ cycle months, from the prober)
3758
+ // is authoritative; a configured `plan` (receipts) is only the fallback.
3759
+ if (account?.type === 'provider') return account.monthlyPriceUSD ?? planMonthlyUSD(account?.plan);
3760
+ const pm = planMonthlyUSD(account?.plan);
3761
+ if (pm != null) return pm;
3724
3762
  const tbl = this.scheduler?.monthlyPriceUSD;
3725
3763
  if (tbl && typeof tbl === 'object') {
3726
3764
  const hit = tbl[account?.name] ?? tbl['*'];
3727
3765
  if (Number.isFinite(hit)) return hit;
3728
3766
  }
3729
- // Owner 2026-10-31: "Claude monthly price is fixed, it's 200 dollars per month."
3730
- // Anthropic has no per-account price API, so this is the default for every Claude
3731
- // login; scheduler.monthlyPriceUSD (name or '*') overrides it.
3732
- if (account?.type === 'oauth') return 200;
3733
- return null;
3767
+ // Owner 2026-11-06: every Anthropic account is monthly $200 — the default plan below.
3768
+ return account?.type === 'oauth' ? DEFAULT_CLAUDE_PLAN.priceUsd / DEFAULT_CLAUDE_PLAN.billingMonths : null;
3734
3769
  }
3735
3770
 
3736
3771
  applyResetCards(accountIndex, cards) {
@@ -3995,18 +4030,17 @@ export class AccountManager {
3995
4030
  capacityTank(accountIndex, window) {
3996
4031
  const a = this.accounts[accountIndex];
3997
4032
  if (!a) return null;
3998
- // A cloud mirror's history lives in TWO ledgers: this pool's, under the account's
3999
- // pre-migration name (months of cycles measured locally before it moved), and the
4000
- // cloud pool's own, exported per row as remoteTank. Mirror rows are named
4001
- // "<name> ☁", which matched neither — so every cloud row read "--" (2026-10-31).
4002
- if (a.remote) {
4003
- // Prefer whichever ledger holds MORE cycles: the cloud pool only started
4004
- // measuring at migration (2026-10-29), so for weeks its 0-2 cycles would
4005
- // otherwise override months of history measured here.
4006
- const cloud = a.remoteTank?.[window] || null;
4007
- const local = this.capacity.tankStats(a.remote.remoteName || a.name, window);
4008
- const best = (cloud && local) ? ((cloud.n || 0) >= (local.n || 0) ? cloud : local) : (cloud || local);
4009
- return best ? { ...best, source: 'cycles' } : null;
4033
+ // One account = one history, wherever it ran. A mirror/benched pair (same identity)
4034
+ // is the SAME account seen from two pools: union this pool's ledger cycles with the
4035
+ // cycles every linked pool exported — never "the ledger with more cycles wins".
4036
+ if (a.remote || a.poolBenchedBy) {
4037
+ const merged = this.capacityHistory(a, window);
4038
+ const t = tankStatsFromCycles(merged, window, {
4039
+ readFloor: this.capacity._readFloorOverride, ignoreCeiling: this.capacity._readFloorOverride != null });
4040
+ if (t) return { ...t, source: 'cycles' };
4041
+ // An older cloud's exported aggregate (remoteTank) is on the legacy input+output
4042
+ // axis: never shown, never blended.
4043
+ return null;
4010
4044
  }
4011
4045
  const measured = this.capacity.tankStats(a.name, window);
4012
4046
  if (measured) return { ...measured, source: 'cycles' };
@@ -4023,7 +4057,95 @@ export class AccountManager {
4023
4057
  };
4024
4058
  }
4025
4059
 
4026
- accrueCapacity(accountIndex, { input = 0, output = 0 } = {}) {
4060
+ /** Identity of a row: local → accountIdentity; mirror → the remote's uuid / key
4061
+ * fingerprint (same key shape). null = unresolvable (never merged by name). */
4062
+ capacityIdentity(a) {
4063
+ if (!a) return null;
4064
+ if (a.remote) return remoteAccountIdentity({ type: a.type, provider: a.provider, accountUuid: a.remoteUuid, keyFingerprint: a.remoteKeyFp });
4065
+ return accountIdentity(a);
4066
+ }
4067
+
4068
+ /** Every row of one identity (local rows + mirrors). An unresolvable row is alone. */
4069
+ capacityGroupOf(a) {
4070
+ const id = this.capacityIdentity(a);
4071
+ if (!id) return [a];
4072
+ return this.accounts.filter(x => this.capacityIdentity(x) === id);
4073
+ }
4074
+
4075
+ /** Merged closed-cycle history of an account's whole identity: this pool's ledger
4076
+ * (under the local name, and under a mirror's pre-migration remote name) plus every
4077
+ * linked pool's exported cycles. */
4078
+ capacityHistory(a, window) {
4079
+ const group = this.capacityGroupOf(a);
4080
+ const names = new Set();
4081
+ for (const x of group) names.add(x.remote ? (x.remote.remoteName || x.name) : x.name);
4082
+ let local = [];
4083
+ for (const n of names) local = mergeCycles(local, this.capacity.closedCycles(n, window));
4084
+ let remote = [];
4085
+ for (const x of group) {
4086
+ if (x.remote && Array.isArray(x.remoteCycles?.[window])) remote = mergeCycles(remote, x.remoteCycles[window]);
4087
+ }
4088
+ return mergeCycles(local, remote);
4089
+ }
4090
+
4091
+ /** Where an account's measurements live: 'cloud' | 'laptop' | 'both' (ledgers with
4092
+ * activity in the last 14 days, else where the live row routes). */
4093
+ capacityWhere(a, now = Date.now()) {
4094
+ const group = this.capacityGroupOf(a);
4095
+ const cutoff = now - 14 * 86400_000;
4096
+ let laptop = false, cloud = false;
4097
+ const hasMirror = group.some(x => x.remote && x.enabled !== false);
4098
+ for (const x of group) {
4099
+ if (x.remote) {
4100
+ const rc = x.remoteCycles || {};
4101
+ if ([...(rc.ses || []), ...(rc.wk || [])].some(c => c.endedAt >= cutoff)) cloud = true;
4102
+ const n = x.remote.remoteName || x.name;
4103
+ if (this._ledgerActiveSince(n, cutoff)) laptop = true;
4104
+ } else if (this._ledgerActiveSince(x.name, cutoff)) laptop = true;
4105
+ }
4106
+ if (hasMirror) cloud = true; // the live row routes through the cloud
4107
+ if (!laptop && !cloud) return group.some(x => !x.remote) ? 'laptop' : 'cloud';
4108
+ return laptop && cloud ? 'both' : (cloud ? 'cloud' : 'laptop');
4109
+ }
4110
+
4111
+ _ledgerActiveSince(name, cutoff) {
4112
+ for (const w of ['ses', 'wk']) {
4113
+ if (this.capacity.closedCycles(name, w).some(c => c.endedAt >= cutoff)) return true;
4114
+ if ((this.capacity.openCycle(name, w)?.lastAccrualAt || 0) >= cutoff) return true;
4115
+ }
4116
+ return false;
4117
+ }
4118
+
4119
+ /** Capacity-page rows: ONE per identity. Primary = the live authority (mirror when
4120
+ * present, else the local row). Rows with no resolvable identity stay separate and
4121
+ * are marked `unresolved` (never merged by name alone). */
4122
+ capacityRows(order, { hideDisabled = false } = {}) {
4123
+ const rows = [];
4124
+ const seen = new Set();
4125
+ for (const i of order) {
4126
+ const a = this.accounts[i];
4127
+ if (!a || seen.has(a.index)) continue;
4128
+ const id = this.capacityIdentity(a);
4129
+ const group = id ? this.capacityGroupOf(a) : [a];
4130
+ for (const x of group) seen.add(x.index);
4131
+ const visible = group.filter(x => !(hideDisabled && x.enabled === false));
4132
+ if (!visible.length) continue;
4133
+ const primary = visible.find(x => x.remote && x.enabled !== false) || visible.find(x => !x.remote) || visible[0];
4134
+ const localRow = group.find(x => !x.remote);
4135
+ const withPrice = [primary, ...group].find(x => x && this.monthlyPriceFor(x) != null) || primary;
4136
+ const withPlan = [primary, ...group].find(x => x && (x.plan || x.remotePlan)) || null;
4137
+ rows.push({
4138
+ index: primary.index, group: group.map(x => x.index), unresolved: !id,
4139
+ displayName: (localRow || primary).name.replace(/ ☁$/, ''),
4140
+ where: this.capacityWhere(primary),
4141
+ priceAccount: withPrice,
4142
+ plan: withPlan ? sanitizePlan(withPlan.plan || withPlan.remotePlan) : (primary.type === 'oauth' ? DEFAULT_CLAUDE_PLAN : null),
4143
+ });
4144
+ }
4145
+ return rows;
4146
+ }
4147
+
4148
+ accrueCapacity(accountIndex, { input = 0, output = 0, cacheRead = 0, cacheCreation = 0, model = null } = {}) {
4027
4149
  const account = this.accounts[accountIndex];
4028
4150
  if (!account) return;
4029
4151
  // A no-weekly plan (z.ai legacy TOKENS_LIMIT, weeklyAbsent) has no weekly window to
@@ -4032,7 +4154,7 @@ export class AccountManager {
4032
4154
  // window + day buckets (the 7d volume) are the real signals for such accounts.
4033
4155
  const windows = account.type === 'provider' && account.quota?.weeklyAbsent
4034
4156
  ? ['ses'] : ['ses', 'wk'];
4035
- this.capacity.accrue(account.name, { input, output }, undefined, windows);
4157
+ this.capacity.accrue(account.name, { input, output, cacheRead, cacheCreation, model }, undefined, windows);
4036
4158
  }
4037
4159
 
4038
4160
  /** The vendor's CURRENT fullness reading for a window — the tank numerator's
@@ -4486,6 +4608,7 @@ export class AccountManager {
4486
4608
  priority: Number.isFinite(acctData.priority) ? acctData.priority : 0,
4487
4609
  model: acctData.model || null,
4488
4610
  modelMap: acctData.modelMap || null,
4611
+ plan: sanitizePlan(acctData.plan),
4489
4612
  stripBetaHeaders: Boolean(acctData.stripBetaHeaders),
4490
4613
  runtime: Boolean(acctData.runtime),
4491
4614
  configSourced: Boolean(acctData.configSourced),
@@ -4944,6 +5067,9 @@ export class AccountManager {
4944
5067
  a.resetGrants = ra.resetGrants || null;
4945
5068
  a.remoteMonthlyPriceUSD = Number.isFinite(ra.monthlyPriceUSD) ? ra.monthlyPriceUSD : null;
4946
5069
  a.remoteTank = ra.tank || null;
5070
+ a.remoteKeyFp = ra.keyFingerprint || null;
5071
+ a.remoteCycles = ra.cycles || null;
5072
+ a.remotePlan = sanitizePlan(ra.plan);
4947
5073
  // Other teammates' live load on this cloud account (remote inFlight minus our
4948
5074
  // own leases) — so the balancer sees the account as busy as it really is.
4949
5075
  a._remoteOtherLoad = Math.max(0, (ra.inFlight || 0) - (a.inFlight || 0));
@@ -5012,6 +5138,14 @@ export class AccountManager {
5012
5138
  // (live-rehearsal 2026-10-29: status omitted accountUuid → no bench).
5013
5139
  accountUuid: a.type === 'oauth' ? (a.accountUuid || null) : null,
5014
5140
  keyFingerprint: (a.type === 'provider' && a.credential) ? credentialFp(a.credential) : null,
5141
+ // Plan config (display-only) + this pool's closed cycles per window so a linked
5142
+ // pool can UNION history instead of picking one ledger (bounded: 12 weekly /
5143
+ // 60 session).
5144
+ plan: a.remote ? null : (a.plan || null),
5145
+ cycles: a.remote ? null : {
5146
+ wk: this.capacity?.exportCycles(a.name, 'wk', 12) || [],
5147
+ ses: this.capacity?.exportCycles(a.name, 'ses', 60) || [],
5148
+ },
5015
5149
  upstream: a.upstream,
5016
5150
  profiles: a.profiles,
5017
5151
  priority: a.priority,