tokenmaxxing 1.110.0 → 1.111.0
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/DESIGN.md +1 -1
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/cli/serve.ts +2 -2
- package/src/cli/status.ts +11 -7
- package/src/lib/decide.ts +5 -5
- package/src/lib/picker.ts +37 -15
- package/src/lib/sample.ts +4 -4
- package/src/lib/types.ts +15 -4
package/DESIGN.md
CHANGED
|
@@ -30,7 +30,7 @@ It is a process manager only - spawn with inherited stdio (stdin through a relay
|
|
|
30
30
|
- A `claude` **supervisor** on your PATH ahead of the real binary (`~/.config/tokenmaxxing/bin/claude`) - you invoke it identically; a `claude` shell function or alias would shadow it, and `doctor` warns about one.
|
|
31
31
|
- Five `~/.claude/settings.json` entries (merged - other settings keys are preserved, but the `statusLine` slot is taken over): the tokenmaxxing `statusLine` renderer (native since 2026-07-11; it also tees usage), a `subagentStatusLine` (per-subagent rows in the agents panel), a `Stop` hook, a `StopFailure` hook, a `SessionStart` hook.
|
|
32
32
|
- **`~/.config/tokenmaxxing/`** - the single home for config and state:
|
|
33
|
-
- `config.json` - SPARSE overrides only (thresholds.session/weekly, claudeBin/codexBin/grokBin/opencodeBin/piBin pins, policy.*); defaults apply at read time, `xx config` prints the path and the effective values, and edits happen in an editor.
|
|
33
|
+
- `config.json` - SPARSE overrides only (thresholds.session/weekly/accounts, claudeBin/codexBin/grokBin/opencodeBin/piBin pins, policy.*); defaults apply at read time, `xx config` prints the path and the effective values, and edits happen in an editor.
|
|
34
34
|
- `accounts.json` - non-secret index `{id, label, email, tier, addedAt, windows, lastUsageAt, lastProbeAt, probeFails, storeFails, usageRetryAt, enforcedUntil, needsReauth, oauthAccount}` (each cached window carries its name, duration, reset, and sample time; the same shape serves the Codex pool as `codex-accounts.json`). Neither index names an active account: the schema is `{version, accounts}`, and an older file that still carries `activeId` loads and drops the key on the next save.
|
|
35
35
|
- `stores/<uuid8>/` - one Claude Code credential store per pooled account (see below).
|
|
36
36
|
- `codex-stores/<uuid8>/` - one Codex credential store per pooled account: its `auth.json`, with everything else symlinked to the shared `~/.codex` so sessions, history, and config stay shared and `codex resume` works across stores.
|
package/README.md
CHANGED
|
@@ -112,7 +112,7 @@ See [How switching decides](docs/content/docs/switching.mdx) for the policy and
|
|
|
112
112
|
}
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
`thresholds.session` and `thresholds.weekly` are the two bars, one number each (an array `thresholds.session`, the former ladder, fails config loading with a message naming the field); `projectionMargin` is a fixed safety margin subtracted from the session bar only (default 3), so a large turn is less likely to blow past the 5-hour bar between checks; the weekly bar takes no margin because one turn cannot overshoot a week; `switchModels` names the models whose per-model cap triggers a move; `usagePollTtlMs` is the shortest sample interval, the one an account near a bar gets, for a seat's own sample, for `status`, and for the accounts each check tick samples; `maxWaitMs` bounds the depleted-pool countdown - a reset further out than this does not pause the session (no respawn marker is written and the session simply keeps hitting its limit until an account recovers); `checkIntervalMs` is the periodic check tick (default 60s), which `init` writes into the timer - re-run `tokenmaxxing init` after changing it so the timer unit picks up the new tick. `claudeBin`, `codexBin`, `grokBin`, `opencodeBin`, and `piBin` pin the real binaries; `init` writes them, and `TOKENMAXXING_CLAUDE_BIN`, `TOKENMAXXING_CODEX_BIN`, `TOKENMAXXING_GROK_BIN`, `TOKENMAXXING_OPENCODE_BIN`, and `TOKENMAXXING_PI_BIN` override them for one process, except that `init` pins the binary it resolved into `config.json`.
|
|
115
|
+
`thresholds.session` and `thresholds.weekly` are the two bars, one number each (an array `thresholds.session`, the former ladder, fails config loading with a message naming the field); `projectionMargin` is a fixed safety margin subtracted from the session bar only (default 3), so a large turn is less likely to blow past the 5-hour bar between checks; the weekly bar takes no margin because one turn cannot overshoot a week; `thresholds.accounts` gives one account its own pair, keyed by label (`{"shared-seat": {"session": 75, "weekly": 75}}`), and `policy.accountReleaseMs` (30 minutes for the session window, 5 hours for a weekly window by default) lifts each of its windows to the higher of its own and the global bar that close to the window's reset; `switchModels` names the models whose per-model cap triggers a move; `usagePollTtlMs` is the shortest sample interval, the one an account near a bar gets, for a seat's own sample, for `status`, and for the accounts each check tick samples; `maxWaitMs` bounds the depleted-pool countdown - a reset further out than this does not pause the session (no respawn marker is written and the session simply keeps hitting its limit until an account recovers); `checkIntervalMs` is the periodic check tick (default 60s), which `init` writes into the timer - re-run `tokenmaxxing init` after changing it so the timer unit picks up the new tick. `claudeBin`, `codexBin`, `grokBin`, `opencodeBin`, and `piBin` pin the real binaries; `init` writes them, and `TOKENMAXXING_CLAUDE_BIN`, `TOKENMAXXING_CODEX_BIN`, `TOKENMAXXING_GROK_BIN`, `TOKENMAXXING_OPENCODE_BIN`, and `TOKENMAXXING_PI_BIN` override them for one process, except that `init` pins the binary it resolved into `config.json`.
|
|
116
116
|
|
|
117
117
|
State lives in `~/.config/tokenmaxxing/`; outside it the install touches Claude Code's `settings.json`, codex's `hooks.json`, the timer and usage hub units, and your shell rc. Each account's credential store is `stores/<uuid8>/`, which Claude Code reads and refreshes as its own: on macOS the credential is a login-keychain item keyed by the store path (never plaintext on disk), on Linux a 0600 `.credentials.json` inside the store (the same plaintext model claude itself uses for `~/.claude/.credentials.json`).
|
|
118
118
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokenmaxxing",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.111.0",
|
|
4
4
|
"description": "Automatic Claude Code account switching: pool multiple accounts, start each session on its own account, and resume a session on a fresher account when quota fills.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/cli/serve.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type { Provider } from "../lib/provider.ts";
|
|
|
14
14
|
import { foldTee } from "../lib/sample.ts";
|
|
15
15
|
import { loadAccounts, loadConfig } from "../lib/state.ts";
|
|
16
16
|
import { OAUTH_USAGE_URL } from "../lib/usage.ts";
|
|
17
|
-
import type { Account,
|
|
17
|
+
import type { Account, Bars, Config } from "../lib/types.ts";
|
|
18
18
|
import { c, emitError } from "./render.ts";
|
|
19
19
|
import { usageReport, type UsageReport, type WindowReport } from "./status.ts";
|
|
20
20
|
|
|
@@ -24,7 +24,7 @@ const STALE_AFTER_MS = 2 * 60 * 60_000;
|
|
|
24
24
|
type HubPool = {
|
|
25
25
|
provider: Provider;
|
|
26
26
|
usageUrl: string;
|
|
27
|
-
fold: (account: Account, thresholds:
|
|
27
|
+
fold: (account: Account, thresholds: Bars) => void;
|
|
28
28
|
read: (account: Account, cfg: Config, now: number) => Promise<Account>;
|
|
29
29
|
body: (usage: UsageReport, account: Account) => unknown;
|
|
30
30
|
};
|
package/src/cli/status.ts
CHANGED
|
@@ -6,10 +6,10 @@ import { opencodeGo } from "../lib/opencodego.ts";
|
|
|
6
6
|
import { loadAccounts, loadConfig, saveAccounts } from "../lib/state.ts";
|
|
7
7
|
import { withLock } from "../lib/lock.ts";
|
|
8
8
|
import { codexPool, grokPool, opencodeGoPool } from "../lib/paths.ts";
|
|
9
|
-
import { earliestReset, gatedWindows, landWindows, isExhausted, isSessionWindow, limitWindows, liveUsed, nextWeeklyReset, sessionWindow, thresholdBars, weeklyWindow } from "../lib/picker.ts";
|
|
9
|
+
import { barFor, earliestReset, gatedWindows, landWindows, isExhausted, isSessionWindow, limitWindows, liveUsed, nextWeeklyReset, sessionWindow, thresholdBars, weeklyWindow } from "../lib/picker.ts";
|
|
10
10
|
import type { Provider, SampleReport } from "../lib/provider.ts";
|
|
11
11
|
import { bar, c, count, emitJson, fmtAgo } from "./render.ts";
|
|
12
|
-
import type { Account, Config, Thresholds, Window } from "../lib/types.ts";
|
|
12
|
+
import type { Account, Bars, Config, Thresholds, Window } from "../lib/types.ts";
|
|
13
13
|
|
|
14
14
|
export type WindowReport = { usedPercentage: number; resetsAt: number | null; windowSeconds: number | null };
|
|
15
15
|
|
|
@@ -24,6 +24,7 @@ type StatusAccount = {
|
|
|
24
24
|
sessions: number;
|
|
25
25
|
needsReauth: boolean;
|
|
26
26
|
exhausted: boolean;
|
|
27
|
+
thresholds: Thresholds | null;
|
|
27
28
|
usage: UsageReport | null;
|
|
28
29
|
usageAt: number | null;
|
|
29
30
|
limitsAt: number | null;
|
|
@@ -60,7 +61,7 @@ export function usageReport(a: Account, now: number): UsageReport | null {
|
|
|
60
61
|
};
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
function gatedNote(accounts: Account[], families: string[] | null,
|
|
64
|
+
function gatedNote(accounts: Account[], families: string[] | null, bars: Bars, now: number): string | null {
|
|
64
65
|
const groups = new Map<string, { carriers: number; capped: number }>();
|
|
65
66
|
for (const a of accounts) {
|
|
66
67
|
if (a.needsReauth === true) continue;
|
|
@@ -68,7 +69,7 @@ function gatedNote(accounts: Account[], families: string[] | null, weeklyBar: nu
|
|
|
68
69
|
const key = (w.name ?? "").toLowerCase();
|
|
69
70
|
const g = groups.get(key) ?? { carriers: 0, capped: 0 };
|
|
70
71
|
g.carriers++;
|
|
71
|
-
if (liveUsed(w, now) >=
|
|
72
|
+
if (liveUsed(w, now) >= barFor(a, w, bars, now)) g.capped++;
|
|
72
73
|
groups.set(key, g);
|
|
73
74
|
}
|
|
74
75
|
}
|
|
@@ -77,7 +78,7 @@ function gatedNote(accounts: Account[], families: string[] | null, weeklyBar: nu
|
|
|
77
78
|
const headroom = accounts.filter((a) => {
|
|
78
79
|
if (a.needsReauth === true) return false;
|
|
79
80
|
const w = weeklyWindow(a);
|
|
80
|
-
return w != null && liveUsed(w, now) <
|
|
81
|
+
return w != null && liveUsed(w, now) < barFor(a, w, bars, now);
|
|
81
82
|
}).length;
|
|
82
83
|
if (headroom === 0) return null;
|
|
83
84
|
return `every ${capped.join(", ")} cap is at the weekly bar, but ${count({ n: headroom, noun: "account" })} still ha${headroom === 1 ? "s" : "ve"} weekly aggregate headroom for other models`;
|
|
@@ -130,6 +131,7 @@ async function collect(p: Provider, cfg: Config, now: number, cached: boolean):
|
|
|
130
131
|
|
|
131
132
|
const present = p.presence();
|
|
132
133
|
const ctx = { now, thresholds: bars, currentId: null, families: p.gatedFamilies(cfg), seats: null };
|
|
134
|
+
const own = new Map(Object.entries(cfg.thresholds.accounts));
|
|
133
135
|
const ordered = sortBy(idx.accounts, [(a) => (a.needsReauth ? 1 : 0), (a) => earliestReset(a, now)]);
|
|
134
136
|
const accounts = ordered.map((a): StatusAccount => {
|
|
135
137
|
const limits = limitWindows(a);
|
|
@@ -142,6 +144,7 @@ async function collect(p: Provider, cfg: Config, now: number, cached: boolean):
|
|
|
142
144
|
sessions: present.get(a.id) ?? 0,
|
|
143
145
|
needsReauth: a.needsReauth === true,
|
|
144
146
|
exhausted: isExhausted(a, ctx),
|
|
147
|
+
thresholds: own.get(a.label) ?? null,
|
|
145
148
|
usage: usageReport(a, now),
|
|
146
149
|
usageAt: a.lastUsageAt ?? null,
|
|
147
150
|
limitsAt: limits.length > 0 ? Math.max(...limits.map((w) => w.sampledAt)) : null,
|
|
@@ -150,9 +153,9 @@ async function collect(p: Provider, cfg: Config, now: number, cached: boolean):
|
|
|
150
153
|
});
|
|
151
154
|
return {
|
|
152
155
|
thresholds: { session: cfg.thresholds.session, weekly: cfg.thresholds.weekly },
|
|
153
|
-
bars,
|
|
156
|
+
bars: { session: bars.session, weekly: bars.weekly },
|
|
154
157
|
projectionMargin: cfg.policy.projectionMargin,
|
|
155
|
-
gatedNote: gatedNote(idx.accounts, ctx.families, bars
|
|
158
|
+
gatedNote: gatedNote(idx.accounts, ctx.families, bars, now),
|
|
156
159
|
accounts,
|
|
157
160
|
};
|
|
158
161
|
}
|
|
@@ -218,6 +221,7 @@ function card(p: Provider, a: StatusAccount, now: number, staleAfterMs: number):
|
|
|
218
221
|
for (const w of a.usage.limits) lines.push(usageRow(p.windowLabel(w.name), w));
|
|
219
222
|
}
|
|
220
223
|
const notes: Note[] = [];
|
|
224
|
+
if (a.thresholds) notes.push({ paint: c.dim, text: `thresholds 5h ${a.thresholds.session}% weekly ${a.thresholds.weekly}%` });
|
|
221
225
|
if (a.sample.ok && a.sample.source === "statusline") {
|
|
222
226
|
const stale = a.usageAt == null || now - a.usageAt > staleAfterMs;
|
|
223
227
|
const age = a.usageAt != null ? fmtAgo(a.usageAt, now) : "age unknown";
|
package/src/lib/decide.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { countBy } from "es-toolkit";
|
|
|
2
2
|
import { CLAUDE_COMPACT_KILL_MS } from "./compact.ts";
|
|
3
3
|
import { withLock } from "./lock.ts";
|
|
4
4
|
import { loadAccounts, loadConfig, liveWaitClaims, releaseWaitClaim, replaceWaitClaim, saveAccounts } from "./state.ts";
|
|
5
|
-
import { isExhausted, landWindows, limitWindows,
|
|
5
|
+
import { isExhausted, landWindows, limitWindows, nextWeeklyReset, pickBest, pickWaitTarget, screensUntilReset, sessionWindow, thresholdBars, weeklyWindow, type PickCtx } from "./picker.ts";
|
|
6
6
|
import { familyTokens } from "./usage.ts";
|
|
7
7
|
import { errorMessage, log } from "./log.ts";
|
|
8
8
|
import type { Observation, Provider } from "./provider.ts";
|
|
9
|
-
import type { Account, EnforcedLimit } from "./types.ts";
|
|
9
|
+
import type { Account, Bars, EnforcedLimit } from "./types.ts";
|
|
10
10
|
|
|
11
11
|
export type SwapDecision = { swapped: boolean; account: Account | null; reason: string; waitUntil?: number };
|
|
12
12
|
|
|
@@ -31,10 +31,10 @@ function isOver(account: Account | undefined, observed: Observation | null, ctx:
|
|
|
31
31
|
return isExhausted({ ...account, windows: observed.windows }, ctx);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
function modelKindCovered(limit: EnforcedLimit, account: Account, now: number,
|
|
34
|
+
function modelKindCovered(limit: EnforcedLimit, account: Account, now: number, bars: Bars): boolean {
|
|
35
35
|
if (limit.kind !== "model" || limit.family == null) return false;
|
|
36
36
|
const family = limit.family;
|
|
37
|
-
return limitWindows(account).some((w) => familyTokens(w.name ?? "").includes(family) &&
|
|
37
|
+
return limitWindows(account).some((w) => familyTokens(w.name ?? "").includes(family) && screensUntilReset(account, w, bars, now));
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
function enforcedWall(limit: EnforcedLimit, account: Account, now: number): number {
|
|
@@ -86,7 +86,7 @@ export async function evaluateAndMaybeSwap(p: Provider, now = Date.now(), canRes
|
|
|
86
86
|
let stamped = false;
|
|
87
87
|
const credits = enforced?.kind === "credits";
|
|
88
88
|
if (enforced && origin && !credits) {
|
|
89
|
-
const covered = modelKindCovered(enforced, origin, now, bars
|
|
89
|
+
const covered = modelKindCovered(enforced, origin, now, bars);
|
|
90
90
|
if (!covered || prior) {
|
|
91
91
|
origin.enforcedUntil = Math.max(origin.enforcedUntil ?? 0, enforcedWall(enforced, origin, now));
|
|
92
92
|
stamped = true;
|
package/src/lib/picker.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { maxBy, minBy, sortBy } from "es-toolkit";
|
|
2
2
|
import { familyTokens } from "./usage.ts";
|
|
3
|
-
import type { Account, Config, Thresholds, Window } from "./types.ts";
|
|
3
|
+
import type { Account, Bars, Config, Thresholds, Window } from "./types.ts";
|
|
4
4
|
|
|
5
|
-
export function thresholdBars(cfg: Config):
|
|
5
|
+
export function thresholdBars(cfg: Config): Bars {
|
|
6
|
+
const bars = (t: Thresholds): Thresholds => ({ session: t.session - cfg.policy.projectionMargin, weekly: t.weekly });
|
|
6
7
|
return {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
...bars(cfg.thresholds),
|
|
9
|
+
accounts: new Map(Object.entries(cfg.thresholds.accounts).map(([label, t]) => [label, bars(t)])),
|
|
10
|
+
accountReleaseMs: cfg.policy.accountReleaseMs,
|
|
9
11
|
};
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export type PickCtx = {
|
|
13
15
|
now: number;
|
|
14
|
-
thresholds:
|
|
16
|
+
thresholds: Bars;
|
|
15
17
|
currentId: string | null;
|
|
16
18
|
families: string[] | null;
|
|
17
19
|
seats: Map<string, number> | null;
|
|
@@ -49,20 +51,40 @@ export function liveUsed(w: Window, now: number): number {
|
|
|
49
51
|
return w.usedPercentage;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
return isSessionWindow(w) ?
|
|
54
|
+
function releaseMs(w: Window, bars: Bars): number {
|
|
55
|
+
return isSessionWindow(w) ? bars.accountReleaseMs.session : bars.accountReleaseMs.weekly;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
export function barFor(a: Account, w: Window, bars: Bars, now: number): number {
|
|
59
|
+
const pick = (t: Thresholds) => (isSessionWindow(w) ? t.session : t.weekly);
|
|
60
|
+
const own = bars.accounts.get(a.label);
|
|
61
|
+
if (own == null) return pick(bars);
|
|
62
|
+
const reset = windowResetAt(w, now);
|
|
63
|
+
const released = reset != null && reset > now && reset - now <= releaseMs(w, bars);
|
|
64
|
+
return released ? Math.max(pick(own), pick(bars)) : pick(own);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function windowEnd(w: Window): number {
|
|
68
|
+
return w.resetsAt ?? w.sampledAt + (w.windowSeconds ?? 0) * 1000;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function blockedUntil(a: Account, w: Window, bars: Bars, now: number): number {
|
|
72
|
+
const used = liveUsed(w, now);
|
|
73
|
+
if (used < barFor(a, w, bars, now)) return 0;
|
|
74
|
+
const reset = windowEnd(w);
|
|
75
|
+
const release = reset - releaseMs(w, bars);
|
|
76
|
+
return release > now && used < barFor(a, w, bars, release) ? release : reset;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function screensUntilReset(a: Account, w: Window, bars: Bars, now: number): boolean {
|
|
80
|
+
const reset = windowEnd(w);
|
|
81
|
+
return reset > now && blockedUntil(a, w, bars, now) === reset;
|
|
60
82
|
}
|
|
61
83
|
|
|
62
84
|
function blockingUntil(a: Account, ctx: PickCtx): number[] {
|
|
63
85
|
return [
|
|
64
|
-
...a.windows.filter((w) => w.name == null).map((w) => blockedUntil(
|
|
65
|
-
...gatedWindows(a, ctx.families).map((w) => blockedUntil(
|
|
86
|
+
...a.windows.filter((w) => w.name == null).map((w) => blockedUntil(a, w, ctx.thresholds, ctx.now)),
|
|
87
|
+
...gatedWindows(a, ctx.families).map((w) => blockedUntil(a, w, ctx.thresholds, ctx.now)),
|
|
66
88
|
...(a.enforcedUntil != null ? [a.enforcedUntil] : []),
|
|
67
89
|
];
|
|
68
90
|
}
|
|
@@ -71,7 +93,7 @@ export function isExhausted(a: Account, ctx: PickCtx): boolean {
|
|
|
71
93
|
return blockingUntil(a, ctx).some((t) => t > ctx.now);
|
|
72
94
|
}
|
|
73
95
|
|
|
74
|
-
export function landWindows(a: Account, windows: Window[], at: number, thresholds:
|
|
96
|
+
export function landWindows(a: Account, windows: Window[], at: number, thresholds: Bars): void {
|
|
75
97
|
const ctx: PickCtx = { now: at, thresholds, currentId: null, families: [], seats: null };
|
|
76
98
|
const blocked = a.enforcedUntil != null && isExhausted({ ...a, enforcedUntil: undefined }, ctx);
|
|
77
99
|
a.windows = windows;
|
|
@@ -114,7 +136,7 @@ export function pacePressure(a: Account, ctx: PickCtx): number {
|
|
|
114
136
|
export function seatHeadroom(a: Account, ctx: PickCtx): number {
|
|
115
137
|
const session = sessionWindow(a);
|
|
116
138
|
if (session == null) return Number.NEGATIVE_INFINITY;
|
|
117
|
-
return (ctx.thresholds.
|
|
139
|
+
return (barFor(a, session, ctx.thresholds, ctx.now) - liveUsed(session, ctx.now)) / ((ctx.seats?.get(a.id) ?? 0) + 1);
|
|
118
140
|
}
|
|
119
141
|
|
|
120
142
|
const swapPreference = (ctx: PickCtx) => [
|
package/src/lib/sample.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { seatCounts } from "./presence.ts";
|
|
|
12
12
|
import type { Observation } from "./provider.ts";
|
|
13
13
|
import { loadAccounts, loadUsageSnapshot, saveAccounts } from "./state.ts";
|
|
14
14
|
import { fetchUsageDirect, mergeWindows, scrubCredEnv, windowsOf } from "./usage.ts";
|
|
15
|
-
import type { Account,
|
|
15
|
+
import type { Account, Bars, Config, UsageWindows, Window } from "./types.ts";
|
|
16
16
|
|
|
17
17
|
export type SampleOutcome = { ok: true; usage: UsageWindows; via: "get" } | { ok: false; reason: string; retryAt?: number };
|
|
18
18
|
|
|
@@ -25,10 +25,10 @@ export function sampleIntervalMs(account: Account, cfg: Config, now: number): nu
|
|
|
25
25
|
const bars = thresholdBars(cfg);
|
|
26
26
|
const aggregates = account.windows.filter((w) => w.name == null);
|
|
27
27
|
if (aggregates.length === 0) return floor;
|
|
28
|
-
const blocked = (w: Window) => liveUsed(w, now) >= barFor(w, bars);
|
|
28
|
+
const blocked = (w: Window) => liveUsed(w, now) >= barFor(account, w, bars, now);
|
|
29
29
|
if (aggregates.some(blocked)) return Math.max(floor, SAMPLE_INTERVAL_MAX_MS);
|
|
30
30
|
const open = [...aggregates, ...gatedWindows(account, cfg.policy.switchModels).filter((w) => !blocked(w))];
|
|
31
|
-
const spare = Math.min(...open.map((w) => 1 - liveUsed(w, now) / barFor(w, bars)));
|
|
31
|
+
const spare = Math.min(...open.map((w) => 1 - liveUsed(w, now) / barFor(account, w, bars, now)));
|
|
32
32
|
return Math.max(floor, SAMPLE_INTERVAL_MAX_MS * spare);
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -48,7 +48,7 @@ export function teeObservation(account: Account): Observation | null {
|
|
|
48
48
|
return { windows: mergeWindows(aggregate, account.windows), at };
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export function foldTee(account: Account, thresholds:
|
|
51
|
+
export function foldTee(account: Account, thresholds: Bars): boolean {
|
|
52
52
|
const observed = teeObservation(account);
|
|
53
53
|
if (!observed || (account.lastUsageAt != null && observed.at <= account.lastUsageAt)) return false;
|
|
54
54
|
landWindows(account, observed.windows, observed.at, thresholds);
|
package/src/lib/types.ts
CHANGED
|
@@ -92,12 +92,17 @@ export type EnforcedLimit = {
|
|
|
92
92
|
|
|
93
93
|
export type Thresholds = { session: number; weekly: number };
|
|
94
94
|
|
|
95
|
+
export type Bars = Thresholds & { accounts: Map<string, Thresholds>; accountReleaseMs: Thresholds };
|
|
96
|
+
|
|
97
|
+
const PercentSchema = z.number().min(0).max(100);
|
|
98
|
+
|
|
95
99
|
export const ConfigSchema = z
|
|
96
100
|
.object({
|
|
97
101
|
thresholds: z
|
|
98
102
|
.object({
|
|
99
|
-
session:
|
|
100
|
-
weekly:
|
|
103
|
+
session: PercentSchema.default(90),
|
|
104
|
+
weekly: PercentSchema.default(98),
|
|
105
|
+
accounts: z.record(z.string(), z.object({ session: PercentSchema, weekly: PercentSchema })).default({}),
|
|
101
106
|
})
|
|
102
107
|
.prefault({}),
|
|
103
108
|
claudeBin: z.string().default(""),
|
|
@@ -115,6 +120,12 @@ export const ConfigSchema = z
|
|
|
115
120
|
usagePollTtlMs: z.number().int().positive().default(90_000),
|
|
116
121
|
maxWaitMs: z.number().int().positive().default(3_600_000),
|
|
117
122
|
checkIntervalMs: z.number().int().min(10_000).default(60_000),
|
|
123
|
+
accountReleaseMs: z
|
|
124
|
+
.object({
|
|
125
|
+
session: z.number().int().min(0).default(1_800_000),
|
|
126
|
+
weekly: z.number().int().min(0).default(18_000_000),
|
|
127
|
+
})
|
|
128
|
+
.prefault({}),
|
|
118
129
|
})
|
|
119
130
|
.prefault({}),
|
|
120
131
|
hub: z
|
|
@@ -123,9 +134,9 @@ export const ConfigSchema = z
|
|
|
123
134
|
})
|
|
124
135
|
.prefault({}),
|
|
125
136
|
})
|
|
126
|
-
.refine((cfg) => cfg.policy.projectionMargin <
|
|
137
|
+
.refine((cfg) => [cfg.thresholds, ...Object.values(cfg.thresholds.accounts)].every((t) => cfg.policy.projectionMargin < t.session), {
|
|
127
138
|
path: ["policy", "projectionMargin"],
|
|
128
|
-
message: "must be strictly below
|
|
139
|
+
message: "must be strictly below every session threshold (the session bar would hit zero and every account it covers would read as exhausted)",
|
|
129
140
|
});
|
|
130
141
|
export type Config = z.infer<typeof ConfigSchema>;
|
|
131
142
|
|