tokenusage-cli 0.4.3 → 0.4.5
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/CHANGELOG.md +66 -0
- package/THIRD_PARTY_NOTICES.md +17 -2
- package/package.json +4 -4
- package/src/commands/init.js +8 -1
- package/src/commands/reconcile.js +17 -5
- package/src/commands/status.js +4 -0
- package/src/commands/sync.js +36 -1
- package/src/lib/cursor-config.js +5 -1
- package/src/lib/deepseek-limits.js +280 -0
- package/src/lib/deepseek-status.js +157 -0
- package/src/lib/doctor.js +50 -0
- package/src/lib/grok-limits.js +40 -1
- package/src/lib/ingest-sanitize.js +9 -0
- package/src/lib/pricing/curated-overrides.json +627 -0
- package/src/lib/pricing/index.js +16 -0
- package/src/lib/pricing-coverage.js +205 -0
- package/src/lib/project-key.js +23 -0
- package/src/lib/provider-api-key-store.js +111 -0
- package/src/lib/provider-status.js +14 -2
- package/src/lib/qoder-limits.js +4 -1
- package/src/lib/rollout.js +308 -58
- package/src/lib/scheduled-task.js +7 -1
- package/src/lib/session-analytics.js +52 -2
- package/src/lib/session-upload.js +25 -12
- package/src/lib/upload-queue.js +4 -1
- package/src/lib/usage-limits.js +536 -22
|
@@ -158,7 +158,13 @@ function writeHiddenRunner({
|
|
|
158
158
|
}) {
|
|
159
159
|
fs.mkdirSync(binDir, { recursive: true });
|
|
160
160
|
const vbsPath = vbsPathFor(binDir);
|
|
161
|
-
|
|
161
|
+
// wscript.exe reads a BOM-less .vbs with the system ANSI code page (GBK on
|
|
162
|
+
// Chinese Windows), so a UTF-8 path containing a non-ASCII username turns
|
|
163
|
+
// into mojibake and the hourly run cannot find node. UTF-16LE + BOM is what
|
|
164
|
+
// Windows Script Host expects; the XML definition already follows the same
|
|
165
|
+
// rule.
|
|
166
|
+
const body = buildHiddenRunnerVbs({ nodeExe, cliJs, fallbackCliJs });
|
|
167
|
+
fs.writeFileSync(vbsPath, Buffer.from(`\uFEFF${body}`, "utf16le"), { mode: 0o600 });
|
|
162
168
|
return vbsPath;
|
|
163
169
|
}
|
|
164
170
|
|
|
@@ -408,9 +408,51 @@ function isWorktreeRef(ref) {
|
|
|
408
408
|
return /[\\/](worktrees|subagents)[\\/]/.test(String(ref || ""));
|
|
409
409
|
}
|
|
410
410
|
|
|
411
|
+
const SUBAGENT_DIR = "subagents";
|
|
412
|
+
const WORKTREE_DIR = "worktrees";
|
|
413
|
+
const UUID_SEGMENT = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
414
|
+
|
|
415
|
+
// Throwaway per-agent checkouts (`<repo>/.claude/worktrees/<id>`) and subagent
|
|
416
|
+
// transcripts (`<session-dir>/subagents/`) belong to the project that owns
|
|
417
|
+
// them, not to the temp name at the tail of the path. Both are artefacts of
|
|
418
|
+
// how a tool lays out its state, so the project key steps up to the real
|
|
419
|
+
// project directory instead of surfacing `agent-<hash>` / `subagents` as a
|
|
420
|
+
// project of its own. Normal cwds and anything without those markers keep the
|
|
421
|
+
// historical basename behavior.
|
|
411
422
|
function projectKey(cwd, filePath) {
|
|
412
|
-
const
|
|
413
|
-
|
|
423
|
+
const raw = String(cwd || path.dirname(filePath || "")).replace(/[\\/]+$/, "");
|
|
424
|
+
const segments = raw.split(/[\\/]+/).filter(Boolean);
|
|
425
|
+
if (segments.length === 0) return "unknown";
|
|
426
|
+
return projectSegment(segments) || "unknown";
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function projectSegment(segments) {
|
|
430
|
+
const tail = segments[segments.length - 1];
|
|
431
|
+
|
|
432
|
+
const worktreeIndex = segments.lastIndexOf(WORKTREE_DIR);
|
|
433
|
+
if (worktreeIndex > 0 && worktreeIndex < segments.length - 1) {
|
|
434
|
+
const owner = segments[worktreeIndex - 1];
|
|
435
|
+
// Repo-local worktrees (`<repo>/.claude/worktrees/<id>`): the owner is the
|
|
436
|
+
// repo above the agent dot-directory.
|
|
437
|
+
if (owner === ".claude") {
|
|
438
|
+
const repo = segments[worktreeIndex - 2];
|
|
439
|
+
if (repo && !repo.startsWith(".")) return repo;
|
|
440
|
+
}
|
|
441
|
+
// `<project>/worktrees/<name>`: the project directly above the marker.
|
|
442
|
+
// A dot-directory there (e.g. `~/.codex/worktrees/<id>`) has no project
|
|
443
|
+
// above it, so keep the old tail behavior instead of guessing.
|
|
444
|
+
if (!owner.startsWith(".")) return owner;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (tail === SUBAGENT_DIR) {
|
|
448
|
+
// `<project>/<session-id>/subagents`: skip the session container too.
|
|
449
|
+
let index = segments.length - 2;
|
|
450
|
+
while (index >= 0 && UUID_SEGMENT.test(segments[index])) index -= 1;
|
|
451
|
+
const project = index >= 0 ? segments[index] : null;
|
|
452
|
+
if (project && !project.startsWith(".")) return project;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return tail;
|
|
414
456
|
}
|
|
415
457
|
|
|
416
458
|
function extractClaudePrompt(obj) {
|
|
@@ -1996,6 +2038,12 @@ function scanQoderSessionFile(filePath, source) {
|
|
|
1996
2038
|
if (typeof cwd === "string" && cwd.trim()) projectRef = cwd.trim();
|
|
1997
2039
|
continue;
|
|
1998
2040
|
}
|
|
2041
|
+
// Subagent transcripts under `<session>/subagents/` carry no workspace
|
|
2042
|
+
// record, but each record still names the workspace cwd — inherit it so the
|
|
2043
|
+
// project key stays the real project instead of the "subagents" directory.
|
|
2044
|
+
if (!projectRef && typeof record?.cwd === "string" && record.cwd.trim()) {
|
|
2045
|
+
projectRef = record.cwd.trim();
|
|
2046
|
+
}
|
|
1999
2047
|
if (record?.type !== "assistant" || record?.message?.role !== "assistant") continue;
|
|
2000
2048
|
const usage = record.message.usage;
|
|
2001
2049
|
if (!usage || typeof usage !== "object") continue;
|
|
@@ -3128,6 +3176,8 @@ module.exports = {
|
|
|
3128
3176
|
scanZcodeSessions,
|
|
3129
3177
|
scanOpencodeSessions,
|
|
3130
3178
|
scanKimiSessions,
|
|
3179
|
+
scanQoderSessionFile,
|
|
3180
|
+
projectKey,
|
|
3131
3181
|
buildSessionAnalytics,
|
|
3132
3182
|
summarizeSessions,
|
|
3133
3183
|
listSessionsForBrowser,
|
|
@@ -17,6 +17,7 @@ const path = require("node:path");
|
|
|
17
17
|
const crypto = require("node:crypto");
|
|
18
18
|
|
|
19
19
|
const { functionUrlFor, fetchFunctionResponse } = require("./function-url");
|
|
20
|
+
const { basenameKey } = require("./project-key");
|
|
20
21
|
|
|
21
22
|
const INGEST_SLUG = "token-usage-ingest";
|
|
22
23
|
const MAX_SESSIONS_PER_BATCH = 200;
|
|
@@ -66,17 +67,6 @@ function foldReasoningIntoOutput(tokens) {
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
function basenameKey(value) {
|
|
70
|
-
const raw = String(value || "").replace(/[\\/]+$/, "");
|
|
71
|
-
if (!raw || raw === "." || raw === "..") return null;
|
|
72
|
-
const base = raw.split(/[\\/]/).pop().trim();
|
|
73
|
-
if (!base || base === "." || base === "..") return null;
|
|
74
|
-
// Control characters are rejected server-side; strip instead of failing.
|
|
75
|
-
// eslint-disable-next-line no-control-regex
|
|
76
|
-
const clean = base.replace(/[\u0000-\u001f\u007f]/g, "").slice(0, 120);
|
|
77
|
-
return clean || null;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
70
|
function validSessionId(value) {
|
|
81
71
|
if (typeof value !== "string") return false;
|
|
82
72
|
const id = value.trim();
|
|
@@ -347,7 +337,30 @@ async function uploadSessions({
|
|
|
347
337
|
error.upserted = upserted;
|
|
348
338
|
throw error;
|
|
349
339
|
}
|
|
350
|
-
|
|
340
|
+
// A 2xx is not a confirmation: captive portals and reverse proxies answer
|
|
341
|
+
// with 200 HTML/empty bodies. Advancing the watermark on those would skip
|
|
342
|
+
// the batch forever, so require the contract's counter fields
|
|
343
|
+
// (docs/cli-contract.md §2.4) before recording progress.
|
|
344
|
+
const data = await res.json().catch(() => null);
|
|
345
|
+
const isCount = (value) =>
|
|
346
|
+
(typeof value === "number" || typeof value === "string") &&
|
|
347
|
+
value !== "" &&
|
|
348
|
+
Number.isFinite(Number(value));
|
|
349
|
+
|
|
350
|
+
if (
|
|
351
|
+
!data ||
|
|
352
|
+
typeof data !== "object" ||
|
|
353
|
+
!isCount(data.sessions_upserted) ||
|
|
354
|
+
!isCount(data.sessions_skipped)
|
|
355
|
+
) {
|
|
356
|
+
const error = new Error(
|
|
357
|
+
`sessions upload was not confirmed (HTTP ${res.status}): unexpected response body`,
|
|
358
|
+
);
|
|
359
|
+
error.status = res.status;
|
|
360
|
+
error.uploaded = uploaded;
|
|
361
|
+
error.upserted = upserted;
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
351
364
|
batches += 1;
|
|
352
365
|
uploaded += batch.length;
|
|
353
366
|
upserted += Number(data?.sessions_upserted || 0);
|
package/src/lib/upload-queue.js
CHANGED
|
@@ -56,7 +56,10 @@ function snapshotKey(bytes) {
|
|
|
56
56
|
if (Object.hasOwn(row, "kind") || typeof row.hour_start !== "string" || !Number.isFinite(Date.parse(row.hour_start))) return null;
|
|
57
57
|
const source = (typeof row.source === "string" ? row.source.trim().toLowerCase() : "") || "codex";
|
|
58
58
|
const model = (typeof row.model === "string" ? row.model.trim() : "") || "unknown";
|
|
59
|
-
|
|
59
|
+
// contract v2 (2026-11-03): project_key joins the last-wins identity;
|
|
60
|
+
// rows without it collapse onto the unattributed ('') bucket as before.
|
|
61
|
+
const project = typeof row.project_key === "string" ? row.project_key.trim() : "";
|
|
62
|
+
return { key: JSON.stringify(["hourly", source, model, project, row.hour_start]), verifiedMs: null };
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
// Caller holds sync.lock. Only rewrite pending snapshots; uploaded history and
|