tokenusage-cli 0.1.0 → 0.2.1
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 +46 -19
- package/LICENSE +22 -22
- package/README.md +123 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +210 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
|
@@ -1,173 +1,173 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("node:fs/promises");
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const crypto = require("node:crypto");
|
|
6
|
-
|
|
7
|
-
const KIMI_MODEL_REPAIR_KEY = "kimiModelAttribution_2026_10";
|
|
8
|
-
const TOKEN_FIELDS = ["input_tokens", "cached_input_tokens", "cache_creation_input_tokens", "output_tokens", "reasoning_output_tokens", "total_tokens"];
|
|
9
|
-
const keyOf = (row) => row?.source === "kimi" && typeof row.model === "string" && typeof row.hour_start === "string"
|
|
10
|
-
? `kimi|${row.model}|${row.hour_start}` : null;
|
|
11
|
-
const digest = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex");
|
|
12
|
-
|
|
13
|
-
async function readBytes(file) {
|
|
14
|
-
try { return await fs.readFile(file); } catch (error) { if (error.code === "ENOENT") return Buffer.alloc(0); throw error; }
|
|
15
|
-
}
|
|
16
|
-
async function readJson(file) {
|
|
17
|
-
const bytes = await readBytes(file);
|
|
18
|
-
return bytes.length ? JSON.parse(bytes.toString("utf8")) : null;
|
|
19
|
-
}
|
|
20
|
-
function latestRows(bytes) {
|
|
21
|
-
const rows = new Map();
|
|
22
|
-
for (const line of bytes.toString("utf8").split("\n")) {
|
|
23
|
-
let row;
|
|
24
|
-
try { row = JSON.parse(line); } catch { continue; }
|
|
25
|
-
const key = keyOf(row);
|
|
26
|
-
if (key) rows.set(key, row);
|
|
27
|
-
}
|
|
28
|
-
return rows;
|
|
29
|
-
}
|
|
30
|
-
function sumsByHour(rows) {
|
|
31
|
-
const totals = new Map();
|
|
32
|
-
for (const row of rows.values()) {
|
|
33
|
-
const total = totals.get(row.hour_start) || Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0]));
|
|
34
|
-
for (const field of TOKEN_FIELDS) {
|
|
35
|
-
const count = Number(row[field] || 0);
|
|
36
|
-
if (!Number.isSafeInteger(count) || count < 0) throw new Error("Kimi repair encountered invalid token totals");
|
|
37
|
-
total[field] += count;
|
|
38
|
-
}
|
|
39
|
-
if (TOKEN_FIELDS.some((field) => total[field] !== 0)) totals.set(row.hour_start, total);
|
|
40
|
-
}
|
|
41
|
-
return totals;
|
|
42
|
-
}
|
|
43
|
-
function sameTotals(before, after) {
|
|
44
|
-
const left = sumsByHour(before), right = sumsByHour(after);
|
|
45
|
-
return left.size === right.size && [...left].every(([hour, total]) => right.has(hour)
|
|
46
|
-
&& TOKEN_FIELDS.every((field) => total[field] === right.get(hour)[field]));
|
|
47
|
-
}
|
|
48
|
-
function sameSnapshot(left, right) {
|
|
49
|
-
return left && right && [...TOKEN_FIELDS, "conversation_count", "billable_total_tokens"]
|
|
50
|
-
.every((field) => Number(left[field] || 0) === Number(right[field] || 0));
|
|
51
|
-
}
|
|
52
|
-
async function fileSignature(file) {
|
|
53
|
-
const handle = await fs.open(file, "r");
|
|
54
|
-
try {
|
|
55
|
-
const stat = await handle.stat();
|
|
56
|
-
if (!stat.isFile()) throw new Error("Kimi repair source is not a file");
|
|
57
|
-
return { size: stat.size, ino: stat.ino, mtimeMs: stat.mtimeMs };
|
|
58
|
-
} finally { await handle.close(); }
|
|
59
|
-
}
|
|
60
|
-
async function writeJournal(file, journal) {
|
|
61
|
-
const temp = `${file}.${crypto.randomUUID()}.tmp`;
|
|
62
|
-
try {
|
|
63
|
-
await fs.writeFile(temp, JSON.stringify(journal), { mode: 0o600, flag: "wx" });
|
|
64
|
-
await fs.rename(temp, file);
|
|
65
|
-
} finally { await fs.rm(temp, { force: true }); }
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Caller holds sync.lock and commits cursors after this returns, before upload.
|
|
69
|
-
// Only append to queue.jsonl; the caller compacts its pending suffix with
|
|
70
|
-
// minPendingBytes:0 after cursor commit, preserving uploaded bytes and offset.
|
|
71
|
-
// Parsers are injected to keep rollout.js independent of its migration helper.
|
|
72
|
-
async function repairKimiModelAttribution({ cursors, queuePath, queueStatePath, legacyFiles = [], officialFiles = [], parseLegacy, parseOfficial, env = process.env }) {
|
|
73
|
-
const journalPath = `${queuePath}.kimi-model-repair.json`;
|
|
74
|
-
const migrations = cursors.migrations || {};
|
|
75
|
-
const applied = migrations[KIMI_MODEL_REPAIR_KEY];
|
|
76
|
-
if (applied?.status === "applied" || applied?.status === "noop") {
|
|
77
|
-
await fs.rm(journalPath, { force: true });
|
|
78
|
-
return { repaired: false, pendingCompaction: applied.pendingCompaction === true };
|
|
79
|
-
}
|
|
80
|
-
let journal = await readJson(journalPath);
|
|
81
|
-
if (!journal) {
|
|
82
|
-
const rawQueue = await readBytes(queuePath);
|
|
83
|
-
const live = latestRows(rawQueue);
|
|
84
|
-
for (const [key, bucket] of Object.entries(cursors.hourly?.buckets || {})) {
|
|
85
|
-
if (!key.startsWith("kimi|") || live.has(key)) continue;
|
|
86
|
-
const [, model, hour_start] = key.split("|");
|
|
87
|
-
live.set(key, { source: "kimi", model, hour_start, ...bucket.totals });
|
|
88
|
-
}
|
|
89
|
-
if (![...live.values()].some((row) => Number(row.total_tokens) > 0)) {
|
|
90
|
-
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = { status: "noop", appliedAt: new Date().toISOString() };
|
|
91
|
-
return { repaired: false, pendingCompaction: false };
|
|
92
|
-
}
|
|
93
|
-
const files = [...new Set([...legacyFiles, ...officialFiles])];
|
|
94
|
-
const discovered = new Set(files);
|
|
95
|
-
const previousFiles = Object.entries(cursors.kimi?.fileOffsets || {}).concat(Object.entries(cursors.kimiCode?.fileOffsets || {}));
|
|
96
|
-
if (!officialFiles.length || previousFiles.some(([file]) => !discovered.has(file))) {
|
|
97
|
-
return { repaired: false, pendingCompaction: false, deferred: "missing-source" };
|
|
98
|
-
}
|
|
99
|
-
const signatures = new Map();
|
|
100
|
-
try {
|
|
101
|
-
for (const file of files) signatures.set(file, await fileSignature(file));
|
|
102
|
-
if (previousFiles.some(([file, prior]) => signatures.get(file).size < Number(prior.size || 0))) {
|
|
103
|
-
return { repaired: false, pendingCompaction: false, deferred: "truncated-source" };
|
|
104
|
-
}
|
|
105
|
-
} catch {
|
|
106
|
-
return { repaired: false, pendingCompaction: false, deferred: "unreadable-source" };
|
|
107
|
-
}
|
|
108
|
-
const scratch = await fs.mkdtemp(path.join(path.dirname(queuePath), ".kimi-model-repair-"));
|
|
109
|
-
const rebuilt = { version: 1, hourly: { version: 3, buckets: {}, groupQueued: {} } };
|
|
110
|
-
let corrected;
|
|
111
|
-
try {
|
|
112
|
-
const scratchQueue = path.join(scratch, "queue.jsonl");
|
|
113
|
-
await parseLegacy({ wireFiles: legacyFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
114
|
-
await parseOfficial({ wireFiles: officialFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
115
|
-
corrected = latestRows(await readBytes(scratchQueue));
|
|
116
|
-
for (const file of files) {
|
|
117
|
-
if (JSON.stringify(await fileSignature(file)) !== JSON.stringify(signatures.get(file))) {
|
|
118
|
-
return { repaired: false, pendingCompaction: false, deferred: "source-changed" };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
} finally { await fs.rm(scratch, { recursive: true, force: true }); }
|
|
122
|
-
// Missing/corrupt history or newly appended usage must first settle through
|
|
123
|
-
// ordinary sync. A model-only repair may never change per-hour token totals.
|
|
124
|
-
if (!sameTotals(live, corrected)) return { repaired: false, pendingCompaction: false, deferred: "totals-not-conserved" };
|
|
125
|
-
const currentQueue = await readBytes(queuePath);
|
|
126
|
-
if (!currentQueue.equals(rawQueue)) return { repaired: false, pendingCompaction: false, deferred: "queue-changed" };
|
|
127
|
-
const state = await readJson(queueStatePath) || {};
|
|
128
|
-
const offset = Number(state.offset || 0);
|
|
129
|
-
if (!Number.isSafeInteger(offset) || offset < 0 || offset > rawQueue.length || (offset > 0 && rawQueue[offset - 1] !== 10)) {
|
|
130
|
-
return { repaired: false, pendingCompaction: false, deferred: "invalid-upload-offset" };
|
|
131
|
-
}
|
|
132
|
-
const corrections = [];
|
|
133
|
-
for (const [key, old] of live) {
|
|
134
|
-
if (!corrected.has(key)) corrections.push({ source: "kimi", model: old.model, hour_start: old.hour_start,
|
|
135
|
-
...Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0])), billable_total_tokens: 0, conversation_count: 0 });
|
|
136
|
-
}
|
|
137
|
-
for (const [key, row] of corrected) if (!sameSnapshot(live.get(key), row)) corrections.push(row);
|
|
138
|
-
journal = { version: 1, uploadedOffset: offset, uploadedPrefixSha256: digest(rawQueue.subarray(0, offset)),
|
|
139
|
-
rows: corrections, kimi: rebuilt.kimi || {}, kimiCode: rebuilt.kimiCode || {}, hourly: rebuilt.hourly,
|
|
140
|
-
preparedAt: new Date().toISOString() };
|
|
141
|
-
await writeJournal(journalPath, journal);
|
|
142
|
-
}
|
|
143
|
-
const rawQueue = await readBytes(queuePath);
|
|
144
|
-
const state = await readJson(queueStatePath) || {};
|
|
145
|
-
if (journal.version !== 1 || !Number.isSafeInteger(journal.uploadedOffset) || journal.uploadedOffset < 0
|
|
146
|
-
|| rawQueue.length < journal.uploadedOffset || Number(state.offset || 0) < journal.uploadedOffset
|
|
147
|
-
|| digest(rawQueue.subarray(0, journal.uploadedOffset)) !== journal.uploadedPrefixSha256) {
|
|
148
|
-
return { repaired: false, pendingCompaction: false, deferred: "uploaded-prefix-changed" };
|
|
149
|
-
}
|
|
150
|
-
if (journal.rows.length) {
|
|
151
|
-
// A prior append can have stopped halfway through a JSON line. Preserve
|
|
152
|
-
// those bytes, terminate that incomplete record, then append complete LWW
|
|
153
|
-
// snapshots again. The journal bypasses conservation checks on partial work.
|
|
154
|
-
const separator = rawQueue.length && rawQueue.at(-1) !== 10 ? "\n" : "";
|
|
155
|
-
await fs.appendFile(queuePath, separator + journal.rows.map(JSON.stringify).join("\n") + "\n");
|
|
156
|
-
}
|
|
157
|
-
const hourly = (cursors.hourly ||= { version: 3, buckets: {}, groupQueued: {} });
|
|
158
|
-
for (const field of ["buckets", "groupQueued"]) {
|
|
159
|
-
hourly[field] ||= {};
|
|
160
|
-
for (const key of Object.keys(hourly[field])) if (key.startsWith("kimi|")) delete hourly[field][key];
|
|
161
|
-
for (const [key, value] of Object.entries(journal.hourly?.[field] || {})) if (key.startsWith("kimi|")) hourly[field][key] = value;
|
|
162
|
-
}
|
|
163
|
-
cursors.kimi = journal.kimi;
|
|
164
|
-
cursors.kimiCode = journal.kimiCode;
|
|
165
|
-
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = {
|
|
166
|
-
status: "applied", appliedAt: new Date().toISOString(), correctedBuckets: journal.rows.length,
|
|
167
|
-
pendingCompaction: journal.rows.length > 0,
|
|
168
|
-
};
|
|
169
|
-
// Leave the journal until the next call observes the persisted marker.
|
|
170
|
-
return { repaired: true, pendingCompaction: journal.rows.length > 0 };
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
module.exports = { KIMI_MODEL_REPAIR_KEY, repairKimiModelAttribution };
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs/promises");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const crypto = require("node:crypto");
|
|
6
|
+
|
|
7
|
+
const KIMI_MODEL_REPAIR_KEY = "kimiModelAttribution_2026_10";
|
|
8
|
+
const TOKEN_FIELDS = ["input_tokens", "cached_input_tokens", "cache_creation_input_tokens", "output_tokens", "reasoning_output_tokens", "total_tokens"];
|
|
9
|
+
const keyOf = (row) => row?.source === "kimi" && typeof row.model === "string" && typeof row.hour_start === "string"
|
|
10
|
+
? `kimi|${row.model}|${row.hour_start}` : null;
|
|
11
|
+
const digest = (bytes) => crypto.createHash("sha256").update(bytes).digest("hex");
|
|
12
|
+
|
|
13
|
+
async function readBytes(file) {
|
|
14
|
+
try { return await fs.readFile(file); } catch (error) { if (error.code === "ENOENT") return Buffer.alloc(0); throw error; }
|
|
15
|
+
}
|
|
16
|
+
async function readJson(file) {
|
|
17
|
+
const bytes = await readBytes(file);
|
|
18
|
+
return bytes.length ? JSON.parse(bytes.toString("utf8")) : null;
|
|
19
|
+
}
|
|
20
|
+
function latestRows(bytes) {
|
|
21
|
+
const rows = new Map();
|
|
22
|
+
for (const line of bytes.toString("utf8").split("\n")) {
|
|
23
|
+
let row;
|
|
24
|
+
try { row = JSON.parse(line); } catch { continue; }
|
|
25
|
+
const key = keyOf(row);
|
|
26
|
+
if (key) rows.set(key, row);
|
|
27
|
+
}
|
|
28
|
+
return rows;
|
|
29
|
+
}
|
|
30
|
+
function sumsByHour(rows) {
|
|
31
|
+
const totals = new Map();
|
|
32
|
+
for (const row of rows.values()) {
|
|
33
|
+
const total = totals.get(row.hour_start) || Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0]));
|
|
34
|
+
for (const field of TOKEN_FIELDS) {
|
|
35
|
+
const count = Number(row[field] || 0);
|
|
36
|
+
if (!Number.isSafeInteger(count) || count < 0) throw new Error("Kimi repair encountered invalid token totals");
|
|
37
|
+
total[field] += count;
|
|
38
|
+
}
|
|
39
|
+
if (TOKEN_FIELDS.some((field) => total[field] !== 0)) totals.set(row.hour_start, total);
|
|
40
|
+
}
|
|
41
|
+
return totals;
|
|
42
|
+
}
|
|
43
|
+
function sameTotals(before, after) {
|
|
44
|
+
const left = sumsByHour(before), right = sumsByHour(after);
|
|
45
|
+
return left.size === right.size && [...left].every(([hour, total]) => right.has(hour)
|
|
46
|
+
&& TOKEN_FIELDS.every((field) => total[field] === right.get(hour)[field]));
|
|
47
|
+
}
|
|
48
|
+
function sameSnapshot(left, right) {
|
|
49
|
+
return left && right && [...TOKEN_FIELDS, "conversation_count", "billable_total_tokens"]
|
|
50
|
+
.every((field) => Number(left[field] || 0) === Number(right[field] || 0));
|
|
51
|
+
}
|
|
52
|
+
async function fileSignature(file) {
|
|
53
|
+
const handle = await fs.open(file, "r");
|
|
54
|
+
try {
|
|
55
|
+
const stat = await handle.stat();
|
|
56
|
+
if (!stat.isFile()) throw new Error("Kimi repair source is not a file");
|
|
57
|
+
return { size: stat.size, ino: stat.ino, mtimeMs: stat.mtimeMs };
|
|
58
|
+
} finally { await handle.close(); }
|
|
59
|
+
}
|
|
60
|
+
async function writeJournal(file, journal) {
|
|
61
|
+
const temp = `${file}.${crypto.randomUUID()}.tmp`;
|
|
62
|
+
try {
|
|
63
|
+
await fs.writeFile(temp, JSON.stringify(journal), { mode: 0o600, flag: "wx" });
|
|
64
|
+
await fs.rename(temp, file);
|
|
65
|
+
} finally { await fs.rm(temp, { force: true }); }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Caller holds sync.lock and commits cursors after this returns, before upload.
|
|
69
|
+
// Only append to queue.jsonl; the caller compacts its pending suffix with
|
|
70
|
+
// minPendingBytes:0 after cursor commit, preserving uploaded bytes and offset.
|
|
71
|
+
// Parsers are injected to keep rollout.js independent of its migration helper.
|
|
72
|
+
async function repairKimiModelAttribution({ cursors, queuePath, queueStatePath, legacyFiles = [], officialFiles = [], parseLegacy, parseOfficial, env = process.env }) {
|
|
73
|
+
const journalPath = `${queuePath}.kimi-model-repair.json`;
|
|
74
|
+
const migrations = cursors.migrations || {};
|
|
75
|
+
const applied = migrations[KIMI_MODEL_REPAIR_KEY];
|
|
76
|
+
if (applied?.status === "applied" || applied?.status === "noop") {
|
|
77
|
+
await fs.rm(journalPath, { force: true });
|
|
78
|
+
return { repaired: false, pendingCompaction: applied.pendingCompaction === true };
|
|
79
|
+
}
|
|
80
|
+
let journal = await readJson(journalPath);
|
|
81
|
+
if (!journal) {
|
|
82
|
+
const rawQueue = await readBytes(queuePath);
|
|
83
|
+
const live = latestRows(rawQueue);
|
|
84
|
+
for (const [key, bucket] of Object.entries(cursors.hourly?.buckets || {})) {
|
|
85
|
+
if (!key.startsWith("kimi|") || live.has(key)) continue;
|
|
86
|
+
const [, model, hour_start] = key.split("|");
|
|
87
|
+
live.set(key, { source: "kimi", model, hour_start, ...bucket.totals });
|
|
88
|
+
}
|
|
89
|
+
if (![...live.values()].some((row) => Number(row.total_tokens) > 0)) {
|
|
90
|
+
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = { status: "noop", appliedAt: new Date().toISOString() };
|
|
91
|
+
return { repaired: false, pendingCompaction: false };
|
|
92
|
+
}
|
|
93
|
+
const files = [...new Set([...legacyFiles, ...officialFiles])];
|
|
94
|
+
const discovered = new Set(files);
|
|
95
|
+
const previousFiles = Object.entries(cursors.kimi?.fileOffsets || {}).concat(Object.entries(cursors.kimiCode?.fileOffsets || {}));
|
|
96
|
+
if (!officialFiles.length || previousFiles.some(([file]) => !discovered.has(file))) {
|
|
97
|
+
return { repaired: false, pendingCompaction: false, deferred: "missing-source" };
|
|
98
|
+
}
|
|
99
|
+
const signatures = new Map();
|
|
100
|
+
try {
|
|
101
|
+
for (const file of files) signatures.set(file, await fileSignature(file));
|
|
102
|
+
if (previousFiles.some(([file, prior]) => signatures.get(file).size < Number(prior.size || 0))) {
|
|
103
|
+
return { repaired: false, pendingCompaction: false, deferred: "truncated-source" };
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return { repaired: false, pendingCompaction: false, deferred: "unreadable-source" };
|
|
107
|
+
}
|
|
108
|
+
const scratch = await fs.mkdtemp(path.join(path.dirname(queuePath), ".kimi-model-repair-"));
|
|
109
|
+
const rebuilt = { version: 1, hourly: { version: 3, buckets: {}, groupQueued: {} } };
|
|
110
|
+
let corrected;
|
|
111
|
+
try {
|
|
112
|
+
const scratchQueue = path.join(scratch, "queue.jsonl");
|
|
113
|
+
await parseLegacy({ wireFiles: legacyFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
114
|
+
await parseOfficial({ wireFiles: officialFiles, cursors: rebuilt, queuePath: scratchQueue, env });
|
|
115
|
+
corrected = latestRows(await readBytes(scratchQueue));
|
|
116
|
+
for (const file of files) {
|
|
117
|
+
if (JSON.stringify(await fileSignature(file)) !== JSON.stringify(signatures.get(file))) {
|
|
118
|
+
return { repaired: false, pendingCompaction: false, deferred: "source-changed" };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
} finally { await fs.rm(scratch, { recursive: true, force: true }); }
|
|
122
|
+
// Missing/corrupt history or newly appended usage must first settle through
|
|
123
|
+
// ordinary sync. A model-only repair may never change per-hour token totals.
|
|
124
|
+
if (!sameTotals(live, corrected)) return { repaired: false, pendingCompaction: false, deferred: "totals-not-conserved" };
|
|
125
|
+
const currentQueue = await readBytes(queuePath);
|
|
126
|
+
if (!currentQueue.equals(rawQueue)) return { repaired: false, pendingCompaction: false, deferred: "queue-changed" };
|
|
127
|
+
const state = await readJson(queueStatePath) || {};
|
|
128
|
+
const offset = Number(state.offset || 0);
|
|
129
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset > rawQueue.length || (offset > 0 && rawQueue[offset - 1] !== 10)) {
|
|
130
|
+
return { repaired: false, pendingCompaction: false, deferred: "invalid-upload-offset" };
|
|
131
|
+
}
|
|
132
|
+
const corrections = [];
|
|
133
|
+
for (const [key, old] of live) {
|
|
134
|
+
if (!corrected.has(key)) corrections.push({ source: "kimi", model: old.model, hour_start: old.hour_start,
|
|
135
|
+
...Object.fromEntries(TOKEN_FIELDS.map((field) => [field, 0])), billable_total_tokens: 0, conversation_count: 0 });
|
|
136
|
+
}
|
|
137
|
+
for (const [key, row] of corrected) if (!sameSnapshot(live.get(key), row)) corrections.push(row);
|
|
138
|
+
journal = { version: 1, uploadedOffset: offset, uploadedPrefixSha256: digest(rawQueue.subarray(0, offset)),
|
|
139
|
+
rows: corrections, kimi: rebuilt.kimi || {}, kimiCode: rebuilt.kimiCode || {}, hourly: rebuilt.hourly,
|
|
140
|
+
preparedAt: new Date().toISOString() };
|
|
141
|
+
await writeJournal(journalPath, journal);
|
|
142
|
+
}
|
|
143
|
+
const rawQueue = await readBytes(queuePath);
|
|
144
|
+
const state = await readJson(queueStatePath) || {};
|
|
145
|
+
if (journal.version !== 1 || !Number.isSafeInteger(journal.uploadedOffset) || journal.uploadedOffset < 0
|
|
146
|
+
|| rawQueue.length < journal.uploadedOffset || Number(state.offset || 0) < journal.uploadedOffset
|
|
147
|
+
|| digest(rawQueue.subarray(0, journal.uploadedOffset)) !== journal.uploadedPrefixSha256) {
|
|
148
|
+
return { repaired: false, pendingCompaction: false, deferred: "uploaded-prefix-changed" };
|
|
149
|
+
}
|
|
150
|
+
if (journal.rows.length) {
|
|
151
|
+
// A prior append can have stopped halfway through a JSON line. Preserve
|
|
152
|
+
// those bytes, terminate that incomplete record, then append complete LWW
|
|
153
|
+
// snapshots again. The journal bypasses conservation checks on partial work.
|
|
154
|
+
const separator = rawQueue.length && rawQueue.at(-1) !== 10 ? "\n" : "";
|
|
155
|
+
await fs.appendFile(queuePath, separator + journal.rows.map(JSON.stringify).join("\n") + "\n");
|
|
156
|
+
}
|
|
157
|
+
const hourly = (cursors.hourly ||= { version: 3, buckets: {}, groupQueued: {} });
|
|
158
|
+
for (const field of ["buckets", "groupQueued"]) {
|
|
159
|
+
hourly[field] ||= {};
|
|
160
|
+
for (const key of Object.keys(hourly[field])) if (key.startsWith("kimi|")) delete hourly[field][key];
|
|
161
|
+
for (const [key, value] of Object.entries(journal.hourly?.[field] || {})) if (key.startsWith("kimi|")) hourly[field][key] = value;
|
|
162
|
+
}
|
|
163
|
+
cursors.kimi = journal.kimi;
|
|
164
|
+
cursors.kimiCode = journal.kimiCode;
|
|
165
|
+
(cursors.migrations ||= {})[KIMI_MODEL_REPAIR_KEY] = {
|
|
166
|
+
status: "applied", appliedAt: new Date().toISOString(), correctedBuckets: journal.rows.length,
|
|
167
|
+
pendingCompaction: journal.rows.length > 0,
|
|
168
|
+
};
|
|
169
|
+
// Leave the journal until the next call observes the persisted marker.
|
|
170
|
+
return { repaired: true, pendingCompaction: journal.rows.length > 0 };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
module.exports = { KIMI_MODEL_REPAIR_KEY, repairKimiModelAttribution };
|
package/src/lib/kimi-profile.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("node:fs");
|
|
4
|
-
const os = require("node:os");
|
|
5
|
-
const path = require("node:path");
|
|
6
|
-
const { parse } = require("smol-toml");
|
|
7
|
-
const { expandHome } = require("./scan-roots");
|
|
8
|
-
|
|
9
|
-
const MAINLAND = { oauthHost: "https://auth.kimi.com", baseUrl: "https://api.kimi.com/coding/v1" };
|
|
10
|
-
const GLOBAL = { oauthHost: "https://auth.kimi.ai", baseUrl: "https://api.kimi.ai/coding/v1" };
|
|
11
|
-
|
|
12
|
-
function endpoint(value, fallback) {
|
|
13
|
-
return typeof value === "string" ? value.trim().replace(/\/+$/, "") : fallback;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// The two supported official endpoint pairs have stable upstream credential
|
|
17
|
-
// slots. The global suffix is SHA-256(JSON.stringify({ oauthHost, baseUrl }))
|
|
18
|
-
// truncated to 16 hex characters, matching managed-kimi-code.ts. Keeping the
|
|
19
|
-
// filenames constant also prevents remote configuration from shaping paths.
|
|
20
|
-
function credentialKey(oauthHost, baseUrl) {
|
|
21
|
-
return oauthHost === GLOBAL.oauthHost && baseUrl === GLOBAL.baseUrl
|
|
22
|
-
? "kimi-code-env-0e4f99c69cc27850" : "kimi-code";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function resolveKimiProfile({ home, env = process.env } = {}) {
|
|
26
|
-
const base = home || os.homedir();
|
|
27
|
-
const explicit = expandHome(env?.KIMI_HOME, base);
|
|
28
|
-
const explicitCode = expandHome(env?.KIMI_CODE_HOME, base);
|
|
29
|
-
const codeHome = explicit || explicitCode || path.join(base, ".kimi-code");
|
|
30
|
-
const codeConfig = path.join(codeHome, "config.toml");
|
|
31
|
-
let provider = null;
|
|
32
|
-
let error = null;
|
|
33
|
-
if (fs.existsSync(codeConfig)) {
|
|
34
|
-
try {
|
|
35
|
-
provider = parse(fs.readFileSync(codeConfig, "utf8"))?.providers?.["managed:kimi-code"] || null;
|
|
36
|
-
} catch {
|
|
37
|
-
error = "Could not parse Kimi Code config.toml";
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
let hasCodeLogin = false;
|
|
41
|
-
try {
|
|
42
|
-
hasCodeLogin = Boolean(JSON.parse(fs.readFileSync(path.join(codeHome, "credentials", "kimi-code.json"), "utf8"))?.access_token);
|
|
43
|
-
} catch { /* No default-slot login. */ }
|
|
44
|
-
const hasEnvOverride = env?.KIMI_CODE_BASE_URL != null || env?.KIMI_CODE_OAUTH_HOST != null || env?.KIMI_OAUTH_HOST != null;
|
|
45
|
-
const kimiHome = explicit || explicitCode || provider || error || hasCodeLogin || hasEnvOverride
|
|
46
|
-
? codeHome : path.join(base, ".kimi");
|
|
47
|
-
const oauthHost = endpoint(hasEnvOverride ? env?.KIMI_CODE_OAUTH_HOST ?? env?.KIMI_OAUTH_HOST : provider?.oauth?.oauth_host, MAINLAND.oauthHost);
|
|
48
|
-
const baseUrl = endpoint(env?.KIMI_CODE_BASE_URL ?? provider?.base_url, MAINLAND.baseUrl);
|
|
49
|
-
if (![MAINLAND, GLOBAL].some((region) => region.oauthHost === oauthHost && region.baseUrl === baseUrl)) {
|
|
50
|
-
error = "Unsupported Kimi quota endpoints; configure a matching official mainland or global profile";
|
|
51
|
-
}
|
|
52
|
-
if (provider?.oauth?.storage && provider.oauth.storage !== "file") {
|
|
53
|
-
error = "Kimi quota supports file credentials; the configured credential storage is unsupported";
|
|
54
|
-
}
|
|
55
|
-
// Derive the endpoint-scoped slot, just as upstream does when a stored key
|
|
56
|
-
// mismatches the endpoints. Never read an unrelated slot or legacy login.
|
|
57
|
-
const credsPath = path.join(kimiHome, "credentials", `${credentialKey(oauthHost, baseUrl)}.json`);
|
|
58
|
-
return { home: kimiHome, credsPath, oauthHost, baseUrl, error, configured: fs.existsSync(path.join(kimiHome, "config.toml")) };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
module.exports = { resolveKimiProfile };
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const os = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { parse } = require("smol-toml");
|
|
7
|
+
const { expandHome } = require("./scan-roots");
|
|
8
|
+
|
|
9
|
+
const MAINLAND = { oauthHost: "https://auth.kimi.com", baseUrl: "https://api.kimi.com/coding/v1" };
|
|
10
|
+
const GLOBAL = { oauthHost: "https://auth.kimi.ai", baseUrl: "https://api.kimi.ai/coding/v1" };
|
|
11
|
+
|
|
12
|
+
function endpoint(value, fallback) {
|
|
13
|
+
return typeof value === "string" ? value.trim().replace(/\/+$/, "") : fallback;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The two supported official endpoint pairs have stable upstream credential
|
|
17
|
+
// slots. The global suffix is SHA-256(JSON.stringify({ oauthHost, baseUrl }))
|
|
18
|
+
// truncated to 16 hex characters, matching managed-kimi-code.ts. Keeping the
|
|
19
|
+
// filenames constant also prevents remote configuration from shaping paths.
|
|
20
|
+
function credentialKey(oauthHost, baseUrl) {
|
|
21
|
+
return oauthHost === GLOBAL.oauthHost && baseUrl === GLOBAL.baseUrl
|
|
22
|
+
? "kimi-code-env-0e4f99c69cc27850" : "kimi-code";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolveKimiProfile({ home, env = process.env } = {}) {
|
|
26
|
+
const base = home || os.homedir();
|
|
27
|
+
const explicit = expandHome(env?.KIMI_HOME, base);
|
|
28
|
+
const explicitCode = expandHome(env?.KIMI_CODE_HOME, base);
|
|
29
|
+
const codeHome = explicit || explicitCode || path.join(base, ".kimi-code");
|
|
30
|
+
const codeConfig = path.join(codeHome, "config.toml");
|
|
31
|
+
let provider = null;
|
|
32
|
+
let error = null;
|
|
33
|
+
if (fs.existsSync(codeConfig)) {
|
|
34
|
+
try {
|
|
35
|
+
provider = parse(fs.readFileSync(codeConfig, "utf8"))?.providers?.["managed:kimi-code"] || null;
|
|
36
|
+
} catch {
|
|
37
|
+
error = "Could not parse Kimi Code config.toml";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let hasCodeLogin = false;
|
|
41
|
+
try {
|
|
42
|
+
hasCodeLogin = Boolean(JSON.parse(fs.readFileSync(path.join(codeHome, "credentials", "kimi-code.json"), "utf8"))?.access_token);
|
|
43
|
+
} catch { /* No default-slot login. */ }
|
|
44
|
+
const hasEnvOverride = env?.KIMI_CODE_BASE_URL != null || env?.KIMI_CODE_OAUTH_HOST != null || env?.KIMI_OAUTH_HOST != null;
|
|
45
|
+
const kimiHome = explicit || explicitCode || provider || error || hasCodeLogin || hasEnvOverride
|
|
46
|
+
? codeHome : path.join(base, ".kimi");
|
|
47
|
+
const oauthHost = endpoint(hasEnvOverride ? env?.KIMI_CODE_OAUTH_HOST ?? env?.KIMI_OAUTH_HOST : provider?.oauth?.oauth_host, MAINLAND.oauthHost);
|
|
48
|
+
const baseUrl = endpoint(env?.KIMI_CODE_BASE_URL ?? provider?.base_url, MAINLAND.baseUrl);
|
|
49
|
+
if (![MAINLAND, GLOBAL].some((region) => region.oauthHost === oauthHost && region.baseUrl === baseUrl)) {
|
|
50
|
+
error = "Unsupported Kimi quota endpoints; configure a matching official mainland or global profile";
|
|
51
|
+
}
|
|
52
|
+
if (provider?.oauth?.storage && provider.oauth.storage !== "file") {
|
|
53
|
+
error = "Kimi quota supports file credentials; the configured credential storage is unsupported";
|
|
54
|
+
}
|
|
55
|
+
// Derive the endpoint-scoped slot, just as upstream does when a stored key
|
|
56
|
+
// mismatches the endpoints. Never read an unrelated slot or legacy login.
|
|
57
|
+
const credsPath = path.join(kimiHome, "credentials", `${credentialKey(oauthHost, baseUrl)}.json`);
|
|
58
|
+
return { home: kimiHome, credsPath, oauthHost, baseUrl, error, configured: fs.existsSync(path.join(kimiHome, "config.toml")) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { resolveKimiProfile };
|