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
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// DeepSeek service-status probe.
|
|
2
|
+
//
|
|
3
|
+
// status.deepseek.com is a Flashcat(快猫)-hosted Next.js app. Unlike Statuspage.io
|
|
4
|
+
// it exposes no public JSON API and sends no CORS headers, so a browser cannot
|
|
5
|
+
// read it directly. The page state is, however, inlined in the server render as
|
|
6
|
+
// React Server Component "flight" data: the client script pushes JSON chunks via
|
|
7
|
+
// self.__next_f.push([1, "<json string>"]). We concatenate those chunks and read
|
|
8
|
+
// the single machine-readable signal — the live `active_changes` array.
|
|
9
|
+
//
|
|
10
|
+
// Semantics (Flashcat change status -> Statuspage.io indicator):
|
|
11
|
+
// operational -> none
|
|
12
|
+
// degraded / degraded_performance / maintenance -> minor
|
|
13
|
+
// partial_outage -> major
|
|
14
|
+
// major_outage / full_outage -> critical
|
|
15
|
+
// An empty active_changes array means everything is operational. Anything we
|
|
16
|
+
// cannot parse throws, so the caller falls back instead of inventing "all good".
|
|
17
|
+
|
|
18
|
+
const DEEPSEEK_STATUS_URL = "https://status.deepseek.com/";
|
|
19
|
+
|
|
20
|
+
const RSC_PUSH_RE = /self\.__next_f\.push\(\[1,("(?:[^"\\]|\\.)*")\]\)/g;
|
|
21
|
+
|
|
22
|
+
const CHANGE_STATUS_TO_INDICATOR = {
|
|
23
|
+
operational: "none",
|
|
24
|
+
degraded: "minor",
|
|
25
|
+
degraded_performance: "minor",
|
|
26
|
+
maintenance: "minor",
|
|
27
|
+
under_maintenance: "minor",
|
|
28
|
+
partial_outage: "major",
|
|
29
|
+
major_outage: "critical",
|
|
30
|
+
full_outage: "critical",
|
|
31
|
+
outage: "critical",
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const INDICATOR_RANK = { none: 0, minor: 1, major: 2, critical: 3 };
|
|
35
|
+
|
|
36
|
+
function extractFlight(html) {
|
|
37
|
+
if (typeof html !== "string" || html.length === 0) {
|
|
38
|
+
throw new Error("DeepSeek status: expected an HTML document");
|
|
39
|
+
}
|
|
40
|
+
let flight = "";
|
|
41
|
+
let chunks = 0;
|
|
42
|
+
RSC_PUSH_RE.lastIndex = 0;
|
|
43
|
+
let match;
|
|
44
|
+
while ((match = RSC_PUSH_RE.exec(html))) {
|
|
45
|
+
try {
|
|
46
|
+
flight += JSON.parse(match[1]);
|
|
47
|
+
} catch (_error) {
|
|
48
|
+
throw new Error("DeepSeek status: malformed RSC chunk");
|
|
49
|
+
}
|
|
50
|
+
chunks += 1;
|
|
51
|
+
}
|
|
52
|
+
if (chunks === 0) {
|
|
53
|
+
throw new Error("DeepSeek status: no RSC data found");
|
|
54
|
+
}
|
|
55
|
+
return flight;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Return the balanced JSON array that starts at `start` (a '[' in flight).
|
|
59
|
+
function sliceJsonArray(flight, start) {
|
|
60
|
+
let depth = 0;
|
|
61
|
+
let inString = false;
|
|
62
|
+
let escaped = false;
|
|
63
|
+
for (let i = start; i < flight.length; i += 1) {
|
|
64
|
+
const ch = flight[i];
|
|
65
|
+
if (inString) {
|
|
66
|
+
if (escaped) escaped = false;
|
|
67
|
+
else if (ch === "\\") escaped = true;
|
|
68
|
+
else if (ch === '"') inString = false;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (ch === '"') inString = true;
|
|
72
|
+
else if (ch === "[") depth += 1;
|
|
73
|
+
else if (ch === "]") {
|
|
74
|
+
depth -= 1;
|
|
75
|
+
if (depth === 0) return flight.slice(start, i + 1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
throw new Error("DeepSeek status: unterminated active_changes array");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function readUpdatedAt(flight) {
|
|
82
|
+
const match = /"initialDataUpdatedAt":(\d{10,})/.exec(flight);
|
|
83
|
+
if (!match) return undefined;
|
|
84
|
+
const ms = Number(match[1]);
|
|
85
|
+
return Number.isFinite(ms) ? new Date(ms).toISOString() : undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Parse the status.deepseek.com HTML into a Statuspage.io-shaped payload:
|
|
90
|
+
* { status: { indicator, description }, page: { updated_at } }
|
|
91
|
+
* Throws when the page structure is not understood, so callers fall back
|
|
92
|
+
* instead of reporting a false "operational".
|
|
93
|
+
*/
|
|
94
|
+
function parseDeepSeekStatusPage(html) {
|
|
95
|
+
const flight = extractFlight(html);
|
|
96
|
+
const marker = '"active_changes":';
|
|
97
|
+
const markerIndex = flight.indexOf(marker);
|
|
98
|
+
if (markerIndex === -1) {
|
|
99
|
+
throw new Error("DeepSeek status: active_changes not found");
|
|
100
|
+
}
|
|
101
|
+
// The value must be an array. If upstream ever reports null / an object /
|
|
102
|
+
// anything else, refuse to guess a healthier-looking array further along the
|
|
103
|
+
// document — throwing keeps the caller on its last-good / "unknown" path
|
|
104
|
+
// instead of inventing a false "operational".
|
|
105
|
+
let arrayStart = markerIndex + marker.length;
|
|
106
|
+
while (arrayStart < flight.length && /\s/.test(flight[arrayStart])) arrayStart += 1;
|
|
107
|
+
if (flight[arrayStart] !== "[") {
|
|
108
|
+
throw new Error("DeepSeek status: active_changes is not an array");
|
|
109
|
+
}
|
|
110
|
+
let changes;
|
|
111
|
+
try {
|
|
112
|
+
changes = JSON.parse(sliceJsonArray(flight, arrayStart));
|
|
113
|
+
} catch (error) {
|
|
114
|
+
throw new Error("DeepSeek status: cannot parse active_changes (" + error.message + ")");
|
|
115
|
+
}
|
|
116
|
+
if (!Array.isArray(changes)) {
|
|
117
|
+
throw new Error("DeepSeek status: active_changes is not an array");
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const updatedAt = readUpdatedAt(flight);
|
|
121
|
+
if (changes.length === 0) {
|
|
122
|
+
return { status: { indicator: "none", description: "" }, page: { updated_at: updatedAt } };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let worstIndicator = "none";
|
|
126
|
+
let worstRank = -1;
|
|
127
|
+
let descriptions = [];
|
|
128
|
+
for (const change of changes) {
|
|
129
|
+
if (!change || typeof change !== "object") continue;
|
|
130
|
+
const rawStatus = String(change.status || change.change_status || change.impact || "").toLowerCase();
|
|
131
|
+
const indicator = CHANGE_STATUS_TO_INDICATOR[rawStatus] || "minor";
|
|
132
|
+
const rank = INDICATOR_RANK[indicator];
|
|
133
|
+
const description = typeof change.name === "string" ? change.name
|
|
134
|
+
: typeof change.title === "string" ? change.title
|
|
135
|
+
: typeof change.description === "string" ? change.description
|
|
136
|
+
: "";
|
|
137
|
+
if (rank > worstRank) {
|
|
138
|
+
worstRank = rank;
|
|
139
|
+
worstIndicator = indicator;
|
|
140
|
+
descriptions = description ? [description] : [];
|
|
141
|
+
} else if (rank === worstRank && description) {
|
|
142
|
+
descriptions.push(description);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// A non-empty array with no readable change is still "something is wrong".
|
|
146
|
+
if (worstRank < 0) worstIndicator = "minor";
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
status: { indicator: worstIndicator, description: descriptions.join("; ") },
|
|
150
|
+
page: { updated_at: updatedAt },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = {
|
|
155
|
+
parseDeepSeekStatusPage,
|
|
156
|
+
DEEPSEEK_STATUS_URL,
|
|
157
|
+
};
|
package/src/lib/doctor.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const fs = require("node:fs/promises");
|
|
2
|
+
const path = require("node:path");
|
|
2
3
|
const { constants } = require("node:fs");
|
|
3
4
|
|
|
4
5
|
const { readJsonStrict } = require("./fs");
|
|
6
|
+
const { collectPricingCoverage } = require("./pricing-coverage");
|
|
5
7
|
|
|
6
8
|
async function buildDoctorReport({
|
|
7
9
|
runtime = {},
|
|
@@ -17,6 +19,7 @@ async function buildDoctorReport({
|
|
|
17
19
|
if (paths.trackerDir) {
|
|
18
20
|
checks.push(await checkTrackerDir(paths.trackerDir));
|
|
19
21
|
checks.push(await checkRuntimeCopy(paths.trackerDir));
|
|
22
|
+
checks.push(await checkPricingCoverage(paths.trackerDir));
|
|
20
23
|
}
|
|
21
24
|
if (paths.configPath) {
|
|
22
25
|
checks.push(await checkConfigJson(paths.configPath));
|
|
@@ -177,6 +180,53 @@ async function checkTrackerDir(trackerDir) {
|
|
|
177
180
|
}
|
|
178
181
|
}
|
|
179
182
|
|
|
183
|
+
// B3 (docs/upstream-comparison-plan.md §2): pricing coverage of the local
|
|
184
|
+
// last-wins queue over the trailing 7 days, plus the C2 unknown-alignment
|
|
185
|
+
// dry-run preview. Informational only — never critical, never writes.
|
|
186
|
+
async function checkPricingCoverage(trackerDir) {
|
|
187
|
+
const queuePath = path.join(trackerDir, "queue.jsonl");
|
|
188
|
+
try {
|
|
189
|
+
const report = await collectPricingCoverage({ queuePath });
|
|
190
|
+
const { coverage, unknown_alignment_preview: alignment } = report;
|
|
191
|
+
if (coverage.buckets === 0) {
|
|
192
|
+
return {
|
|
193
|
+
id: "pricing.coverage",
|
|
194
|
+
status: "ok",
|
|
195
|
+
critical: false,
|
|
196
|
+
detail: `no local buckets in the last ${report.window.days}d`,
|
|
197
|
+
meta: report,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
const parts = [
|
|
201
|
+
`last ${report.window.days}d: ${(coverage.token_coverage * 100).toFixed(1)}% of tokens priced ` +
|
|
202
|
+
`(${coverage.covered_buckets}/${coverage.buckets} buckets covered)`,
|
|
203
|
+
];
|
|
204
|
+
const top = coverage.top_unpriced[0];
|
|
205
|
+
if (top) {
|
|
206
|
+
parts.push(`top unpriced: ${top.model} (${top.sources.join("/")}, ${top.total_tokens} tokens)`);
|
|
207
|
+
}
|
|
208
|
+
if (alignment.alignable_buckets > 0) {
|
|
209
|
+
parts.push(
|
|
210
|
+
`unknown dry-run: ${alignment.alignable_buckets} bucket(s) / ${alignment.alignable_tokens} token(s) would re-attribute to a dominant known model`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
id: "pricing.coverage",
|
|
215
|
+
status: coverage.token_coverage >= 0.95 ? "ok" : "warn",
|
|
216
|
+
critical: false,
|
|
217
|
+
detail: parts.join("; "),
|
|
218
|
+
meta: report,
|
|
219
|
+
};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
return {
|
|
222
|
+
id: "pricing.coverage",
|
|
223
|
+
status: "warn",
|
|
224
|
+
critical: false,
|
|
225
|
+
detail: `coverage check failed: ${error?.message || error}`,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
180
230
|
// The init-copied runtime (`<trackerDir>/app`) is what notify hooks and the
|
|
181
231
|
// hidden task prefer; without its readiness marker they fall back to the
|
|
182
232
|
// installed CLI. Surface that state instead of leaving it invisible.
|
package/src/lib/grok-limits.js
CHANGED
|
@@ -264,6 +264,29 @@ function grokEntryAccessToken(entry) {
|
|
|
264
264
|
return entry && typeof entry.key === "string" ? entry.key.trim() : "";
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
+
// auth.x.ai issues JWT access tokens (RFC 9068), so the OIDC `sub` claim is a
|
|
268
|
+
// non-secret, locally derivable account id — the cache-partition key for the
|
|
269
|
+
// limits disk-cache fallback. An opaque token yields null (fail-closed).
|
|
270
|
+
function grokTokenSubject(token) {
|
|
271
|
+
if (typeof token !== "string" || !token.trim()) return null;
|
|
272
|
+
try {
|
|
273
|
+
const parts = token.split(".");
|
|
274
|
+
if (parts.length !== 3) return null;
|
|
275
|
+
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
276
|
+
const subject = payload?.sub;
|
|
277
|
+
return typeof subject === "string" && subject.trim() ? subject.trim() : null;
|
|
278
|
+
} catch (_error) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Errors that tell the user to re-login. They are actionable and must surface
|
|
284
|
+
// instead of being masked by the stale disk-cache fallback (opencode-go
|
|
285
|
+
// auth_error precedent).
|
|
286
|
+
function isGrokAuthErrorCode(error) {
|
|
287
|
+
return error?.code === "GROK_AUTH_REQUIRED" || error?.code === "GROK_REAUTH_REQUIRED";
|
|
288
|
+
}
|
|
289
|
+
|
|
267
290
|
/**
|
|
268
291
|
* Public xAI OIDC client refresh (auth method "none").
|
|
269
292
|
* Matches Grok Build CLI's refresh against https://auth.x.ai/oauth2/token.
|
|
@@ -775,6 +798,10 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
|
|
|
775
798
|
return {
|
|
776
799
|
configured: true,
|
|
777
800
|
error: error?.message || "Unknown error",
|
|
801
|
+
// A rejected refresh is actionable (re-login) and must not be hidden
|
|
802
|
+
// behind the stale disk-cache fallback. Internal marker — stripped
|
|
803
|
+
// before the payload is served.
|
|
804
|
+
...(isGrokAuthErrorCode(error) ? { auth_error: true } : {}),
|
|
778
805
|
};
|
|
779
806
|
}
|
|
780
807
|
|
|
@@ -785,13 +812,16 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
|
|
|
785
812
|
return {
|
|
786
813
|
configured: true,
|
|
787
814
|
error: resolved.error?.message || grokAuthError().message,
|
|
815
|
+
...(isGrokAuthErrorCode(resolved.error) ? { auth_error: true } : {}),
|
|
788
816
|
};
|
|
789
817
|
}
|
|
790
818
|
|
|
791
819
|
const startedAtMs = Date.now();
|
|
820
|
+
// Hoisted so the catch below can still derive the cache identity from the
|
|
821
|
+
// token the failing attempt used.
|
|
822
|
+
let accessToken = resolved.accessToken;
|
|
792
823
|
try {
|
|
793
824
|
let body;
|
|
794
|
-
let accessToken = resolved.accessToken;
|
|
795
825
|
try {
|
|
796
826
|
body = await fetchGrokBilling(resolved.accessToken, { fetchImpl, env, timeoutMs });
|
|
797
827
|
} catch (error) {
|
|
@@ -830,11 +860,19 @@ async function fetchGrokLimits({ home, env, fetchImpl = fetch, timeoutMs, nowMs
|
|
|
830
860
|
configured: true,
|
|
831
861
|
error: null,
|
|
832
862
|
...limits,
|
|
863
|
+
// Internal cache-partition key (stripped before the payload is served).
|
|
864
|
+
// An opaque token yields null → the cache write/read is refused, so an
|
|
865
|
+
// unattributable login can never pin another account's bars (no token
|
|
866
|
+
// fingerprint fallback: credentials must not enter even a label hash —
|
|
867
|
+
// CodeQL js/insufficient-password-hash precedent).
|
|
868
|
+
cache_identity: grokTokenSubject(accessToken),
|
|
833
869
|
};
|
|
834
870
|
} catch (error) {
|
|
835
871
|
return {
|
|
836
872
|
configured: true,
|
|
837
873
|
error: error?.message || "Unknown error",
|
|
874
|
+
...(isGrokAuthErrorCode(error) ? { auth_error: true } : {}),
|
|
875
|
+
cache_identity: grokTokenSubject(accessToken),
|
|
838
876
|
};
|
|
839
877
|
}
|
|
840
878
|
}
|
|
@@ -848,6 +886,7 @@ module.exports = {
|
|
|
848
886
|
loadGrokAuthEntry,
|
|
849
887
|
readGrokAccessToken,
|
|
850
888
|
isGrokAccessTokenExpired,
|
|
889
|
+
grokTokenSubject,
|
|
851
890
|
resolveGrokOidcClientId,
|
|
852
891
|
resolveGrokOidcIssuer,
|
|
853
892
|
resolveGrokTokenEndpoint,
|
|
@@ -44,6 +44,15 @@ function sanitizeBucketForUpload(bucket) {
|
|
|
44
44
|
} else {
|
|
45
45
|
delete sanitized.credits;
|
|
46
46
|
}
|
|
47
|
+
// project_key (contract v2, 2026-11-03): optional pass-through, string only
|
|
48
|
+
// (anything else is a producer bug — drop the field, not the row); the
|
|
49
|
+
// server applies the basename/control-char cleanup. Trimmed-empty is
|
|
50
|
+
// dropped so unattributed rows keep the old wire shape.
|
|
51
|
+
if (typeof bucket.project_key === "string" && bucket.project_key.trim()) {
|
|
52
|
+
sanitized.project_key = bucket.project_key;
|
|
53
|
+
} else {
|
|
54
|
+
delete sanitized.project_key;
|
|
55
|
+
}
|
|
47
56
|
return sanitized;
|
|
48
57
|
}
|
|
49
58
|
|