tokenmaxxing 1.134.0 → 1.136.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/package.json +1 -1
- package/src/entries/supervisor.ts +6 -4
- package/src/lib/apikeys.ts +6 -0
- package/src/lib/compact.ts +11 -7
- package/src/lib/types.ts +6 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokenmaxxing",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.136.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",
|
|
@@ -5,7 +5,7 @@ import { maxBy, omit } from "es-toolkit";
|
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
import { claudePool, paths, storeDirFor } from "../lib/paths.ts";
|
|
7
7
|
import { CLAUDE_BIN, UNMANAGED_ENV, WRAP_DEPTH_ENV, resolveRealBin, wrapDepth } from "../lib/claudebin.ts";
|
|
8
|
-
import { apiKeyEnv, clearSessionCost, foldSessionCost, loadApiKeys, readApiKey, sendApiKey, usableApiKey, writeSessionCost } from "../lib/apikeys.ts";
|
|
8
|
+
import { apiKeyEnv, clearSessionCost, foldCompactionCost, foldSessionCost, loadApiKeys, readApiKey, sendApiKey, usableApiKey, writeSessionCost } from "../lib/apikeys.ts";
|
|
9
9
|
import { claude, pickLaunch, type LaunchTarget } from "../lib/claude.ts";
|
|
10
10
|
import { compactClaudeSession } from "../lib/compact.ts";
|
|
11
11
|
import { evaluateAndMaybeSwap, moveTarget } from "../lib/decide.ts";
|
|
@@ -19,7 +19,7 @@ import { saveTermios } from "../lib/tty.ts";
|
|
|
19
19
|
import { liveSessionId, loadSessionFlags, pruneStaleSessions, saveSessionFlags, writeRespawnMarker } from "../lib/sessions.ts";
|
|
20
20
|
import { loadAccounts, loadConfig, readJsonFile, releaseWaitClaim, writeUsage } from "../lib/state.ts";
|
|
21
21
|
import { gatedFamilies, modelFromFlag, parseStreamLine, scrubCredEnv } from "../lib/usage.ts";
|
|
22
|
-
import { CostLineSchema, JsonTextSchema, RespawnMarkerSchema, type Account, type ApiKey, type Config, type ModelInfo, type UsageWindows } from "../lib/types.ts";
|
|
22
|
+
import { CostLineSchema, JsonTextSchema, RespawnMarkerSchema, type Account, type ApiKey, type Config, type ModelInfo, type SessionCost, type UsageWindows } from "../lib/types.ts";
|
|
23
23
|
import { errorMessage, log } from "../lib/log.ts";
|
|
24
24
|
|
|
25
25
|
const SUBCOMMANDS = new Set([
|
|
@@ -297,9 +297,10 @@ function resolveLaunch(wanted: { accountId: string | null; apiKeyId: string | nu
|
|
|
297
297
|
return pickLaunch(now, model);
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
-
async function foldCost(sid: string, keyId: string | null, resumed: Set<string
|
|
300
|
+
async function foldCost(sid: string, keyId: string | null, resumed: Set<string>, compaction: SessionCost | null = null): Promise<void> {
|
|
301
301
|
try {
|
|
302
|
-
await
|
|
302
|
+
if (compaction) await foldCompactionCost(sid, compaction, keyId, resumed);
|
|
303
|
+
else await foldSessionCost(sid, keyId, resumed);
|
|
303
304
|
} catch (e) {
|
|
304
305
|
log("supervisor.cost_fold_failed", { err: errorMessage(e) });
|
|
305
306
|
}
|
|
@@ -683,6 +684,7 @@ export async function runSupervisor(argv: string[]): Promise<number> {
|
|
|
683
684
|
},
|
|
684
685
|
});
|
|
685
686
|
child = null;
|
|
687
|
+
await foldCost(sid, compactKey?.id ?? null, resumed, outcome.cost);
|
|
686
688
|
if (terminating) {
|
|
687
689
|
await releaseClaim();
|
|
688
690
|
return 143;
|
package/src/lib/apikeys.ts
CHANGED
|
@@ -121,6 +121,12 @@ export function clearSessionCost(sid: string): void {
|
|
|
121
121
|
rmSync(costFileFor(sid), { force: true });
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
export async function foldCompactionCost(sid: string, cost: SessionCost, keyId: string | null, resumed: Set<string>): Promise<void> {
|
|
125
|
+
if (!existsSync(claudePool.apiKeysJson)) return;
|
|
126
|
+
writeSessionCost(sid, cost);
|
|
127
|
+
await foldSessionCost(sid, keyId, new Set([...resumed, cost.sessionId]));
|
|
128
|
+
}
|
|
129
|
+
|
|
124
130
|
const BASELINE_RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
125
131
|
|
|
126
132
|
export async function foldSessionCost(sid: string, keyId: string | null, resumed: Set<string>): Promise<void> {
|
package/src/lib/compact.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { z } from "zod";
|
|
|
4
4
|
import pkg from "../../package.json" with { type: "json" };
|
|
5
5
|
import { errorMessage, log } from "./log.ts";
|
|
6
6
|
import { readLines } from "./proc.ts";
|
|
7
|
-
import { JsonTextSchema } from "./types.ts";
|
|
7
|
+
import { CostStateRowSchema, JsonTextSchema, type SessionCost } from "./types.ts";
|
|
8
8
|
|
|
9
9
|
export type CompactOutcome = { ok: true } | { ok: false; reason: string };
|
|
10
10
|
|
|
@@ -13,7 +13,9 @@ const PIPE_GRACE_MS = 2_000;
|
|
|
13
13
|
|
|
14
14
|
const CompactBoundarySchema = z.looseObject({ type: z.literal("system"), subtype: z.literal("compact_boundary") });
|
|
15
15
|
|
|
16
|
-
export
|
|
16
|
+
export type ClaudeCompactResult = CompactOutcome & { cost: SessionCost | null };
|
|
17
|
+
|
|
18
|
+
export async function compactClaudeSession(input: { real: string; sid: string; transcript: string; env: Record<string, string | undefined>; keyPipe: boolean; onSpawn: (child: Subprocess) => void }): Promise<ClaudeCompactResult> {
|
|
17
19
|
const offset = statSync(input.transcript).size;
|
|
18
20
|
const extra: "pipe"[] = input.keyPipe ? ["pipe"] : [];
|
|
19
21
|
const p = Bun.spawn([input.real, "-p", "--resume", input.sid, "/compact"], {
|
|
@@ -26,13 +28,15 @@ export async function compactClaudeSession(input: { real: string; sid: string; t
|
|
|
26
28
|
const reads = Promise.all([p.stdout.text(), p.stderr.text()]);
|
|
27
29
|
const settled = await Promise.race([reads, p.exited.then(() => Bun.sleep(PIPE_GRACE_MS)).then(() => null)]);
|
|
28
30
|
await p.exited;
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
const rows = (await Bun.file(input.transcript).slice(offset).text()).split("\n").map((line) => JsonTextSchema.safeParse(line).data);
|
|
32
|
+
const cost = rows.map((row) => CostStateRowSchema.safeParse(row).data).findLast((c) => c?.sessionId === input.sid) ?? null;
|
|
33
|
+
const done = (outcome: CompactOutcome): ClaudeCompactResult => ({ ...outcome, cost });
|
|
34
|
+
if (rows.some((row) => CompactBoundarySchema.safeParse(row).success)) return done({ ok: true });
|
|
35
|
+
if (settled === null) return done({ ok: false, reason: "output pipes still open after child exit (leaked descendant)" });
|
|
32
36
|
const [stdout, stderr] = settled;
|
|
33
|
-
if (p.exitCode === null) return { ok: false, reason: `killed after ${CLAUDE_COMPACT_KILL_MS / 1000}s` };
|
|
37
|
+
if (p.exitCode === null) return done({ ok: false, reason: `killed after ${CLAUDE_COMPACT_KILL_MS / 1000}s` });
|
|
34
38
|
const output = (stderr.trim() || stdout.trim()).slice(0, 200);
|
|
35
|
-
return { ok: false, reason: output ? `no compact boundary (exit ${p.exitCode}): ${output}` : `no compact boundary (exit ${p.exitCode})` };
|
|
39
|
+
return done({ ok: false, reason: output ? `no compact boundary (exit ${p.exitCode}): ${output}` : `no compact boundary (exit ${p.exitCode})` });
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
export const CODEX_COMPACT_KILL_MS = 180_000;
|
package/src/lib/types.ts
CHANGED
|
@@ -195,12 +195,16 @@ export const SessionCostSchema = z.object({ sessionId: z.string(), usd: z.number
|
|
|
195
195
|
export type SessionCost = z.infer<typeof SessionCostSchema>;
|
|
196
196
|
|
|
197
197
|
export const CostLineSchema = z
|
|
198
|
-
.looseObject({ session_id: z.string(), total_cost_usd: z.number().optional(), cost: z.looseObject({ total_cost_usd: z.number() }).nullable().optional() })
|
|
198
|
+
.looseObject({ session_id: z.string(), num_turns: z.number().optional(), total_cost_usd: z.number().optional(), cost: z.looseObject({ total_cost_usd: z.number() }).nullable().optional() })
|
|
199
199
|
.transform((line): SessionCost | null => {
|
|
200
200
|
const usd = line.total_cost_usd ?? line.cost?.total_cost_usd;
|
|
201
|
-
return usd == null ? null : { sessionId: line.session_id, usd };
|
|
201
|
+
return usd == null || (usd === 0 && line.num_turns === 0) ? null : { sessionId: line.session_id, usd };
|
|
202
202
|
});
|
|
203
203
|
|
|
204
|
+
export const CostStateRowSchema = z
|
|
205
|
+
.looseObject({ type: z.literal("cost-state"), sessionId: z.string(), totalCostUSD: z.number() })
|
|
206
|
+
.transform((row): SessionCost => ({ sessionId: row.sessionId, usd: row.totalCostUSD }));
|
|
207
|
+
|
|
204
208
|
export const EpochSecondsSchema = z.number().transform((seconds) => seconds * 1000);
|
|
205
209
|
|
|
206
210
|
export const InstantSchema = z.iso.datetime({ offset: true }).transform((iso) => Date.parse(iso));
|