tokenusage-cli 0.1.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/CHANGELOG.md +19 -0
- package/LICENSE +22 -0
- package/README.md +94 -0
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/bin/tokenusage.js +69 -0
- package/package.json +59 -0
- package/src/cli.js +134 -0
- package/src/commands/device-login.js +205 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +110 -0
- package/src/commands/init.js +2436 -0
- package/src/commands/sessions.js +29 -0
- package/src/commands/status.js +1747 -0
- package/src/commands/sync.js +7934 -0
- package/src/commands/uninstall.js +312 -0
- package/src/lib/account-usage-dedup.js +267 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/ark-agent-plan-limits.js +438 -0
- package/src/lib/ark-coding-plan-limits.js +537 -0
- package/src/lib/async-queue-reader.js +138 -0
- package/src/lib/atomic-json.js +27 -0
- package/src/lib/bearer-token.js +11 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1343 -0
- package/src/lib/claude-config.js +279 -0
- package/src/lib/claude-user-identity.js +56 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/cloud-account.js +541 -0
- package/src/lib/cloud-device-token.js +141 -0
- package/src/lib/cloud-sync-prefs.js +14 -0
- package/src/lib/cloud-usage-reset.js +115 -0
- package/src/lib/codex-config.js +595 -0
- package/src/lib/codex-context-breakdown.js +806 -0
- package/src/lib/codex-model-attribution.js +128 -0
- package/src/lib/codex-rollout-parser.js +1471 -0
- package/src/lib/codex-service-tier.js +49 -0
- package/src/lib/codex-token-refresh.js +140 -0
- package/src/lib/codex-token-usage.js +195 -0
- package/src/lib/codex-usage-record.js +195 -0
- package/src/lib/command-runner.js +313 -0
- package/src/lib/commandcode-limits.js +317 -0
- package/src/lib/context-health.js +124 -0
- package/src/lib/cursor-config.js +605 -0
- package/src/lib/cursor-store.js +1918 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/devin-limits.js +249 -0
- package/src/lib/devin-usage.js +212 -0
- package/src/lib/diagnostics.js +383 -0
- package/src/lib/doctor.js +385 -0
- package/src/lib/dumate.js +95 -0
- package/src/lib/fs.js +455 -0
- package/src/lib/function-url.js +14 -0
- package/src/lib/gemini-config.js +291 -0
- package/src/lib/git-outcomes.js +243 -0
- package/src/lib/grok-context-breakdown.js +890 -0
- package/src/lib/grok-hook.js +360 -0
- package/src/lib/grok-limits.js +865 -0
- package/src/lib/grok-usage.js +98 -0
- package/src/lib/heatmap-compact.js +114 -0
- package/src/lib/ingest-sanitize.js +41 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/install-resolver.js +170 -0
- package/src/lib/jsonl-lines.js +134 -0
- package/src/lib/kimi-model-repair.js +173 -0
- package/src/lib/kimi-profile.js +61 -0
- package/src/lib/kimi-session.js +216 -0
- package/src/lib/local-api.js +3588 -0
- package/src/lib/machine-id.js +238 -0
- package/src/lib/multi-install-parser.js +113 -0
- package/src/lib/notify-command.js +38 -0
- package/src/lib/omp-hook.js +589 -0
- package/src/lib/openclaw-cli.js +104 -0
- package/src/lib/openclaw-hook.js +375 -0
- package/src/lib/openclaw-session-plugin.js +560 -0
- package/src/lib/opencode-config.js +121 -0
- package/src/lib/opencode-go-limits.js +807 -0
- package/src/lib/outcomes-engine.js +253 -0
- package/src/lib/passive-mode.js +219 -0
- package/src/lib/pet-packages.js +722 -0
- package/src/lib/pricing/curated-overrides.json +822 -0
- package/src/lib/pricing/index.js +379 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +456 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +117 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/provider-status.js +104 -0
- package/src/lib/proxy-env.js +489 -0
- package/src/lib/proxy-settings.js +163 -0
- package/src/lib/qoder-cn-cli-limits.js +119 -0
- package/src/lib/qoder-limits.js +880 -0
- package/src/lib/quiet-warnings.js +26 -0
- package/src/lib/rollout.js +25966 -0
- package/src/lib/runtime-config.js +259 -0
- package/src/lib/scan-roots.js +277 -0
- package/src/lib/sentry.js +231 -0
- package/src/lib/session-analytics.js +2972 -0
- package/src/lib/session-performance.js +237 -0
- package/src/lib/session-upload.js +379 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1591 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +215 -0
- package/src/lib/star-cta.js +73 -0
- package/src/lib/subscription-manager.js +293 -0
- package/src/lib/subscriptions.js +508 -0
- package/src/lib/telemetry.js +193 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/trae-cn-config.js +548 -0
- package/src/lib/trae-db.js +127 -0
- package/src/lib/trae-snapshot.js +230 -0
- package/src/lib/trae-sqlite.js +143 -0
- package/src/lib/trae-usage.js +121 -0
- package/src/lib/upload-queue.js +134 -0
- package/src/lib/upload-throttle.js +171 -0
- package/src/lib/usage-limits.js +4552 -0
- package/src/lib/wrapped-aggregator.js +236 -0
- package/src/lib/wsl-probe.js +281 -0
- package/src/lib/zcode-limits.js +1172 -0
- package/src/lib/zcode-reset-credits.js +66 -0
- package/test/cli-smoke.test.js +53 -0
- package/test/ingest-sanitize.test.js +162 -0
- package/test/quiet-warnings.test.js +53 -0
- package/test/session-upload.test.js +427 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
const fs = require("node:fs/promises");
|
|
2
|
+
const { constants } = require("node:fs");
|
|
3
|
+
|
|
4
|
+
const { readJsonStrict } = require("./fs");
|
|
5
|
+
|
|
6
|
+
async function buildDoctorReport({
|
|
7
|
+
runtime = {},
|
|
8
|
+
diagnostics = null,
|
|
9
|
+
fetch = globalThis.fetch,
|
|
10
|
+
now = () => new Date(),
|
|
11
|
+
paths = {},
|
|
12
|
+
} = {}) {
|
|
13
|
+
const checks = [];
|
|
14
|
+
|
|
15
|
+
checks.push(...buildRuntimeChecks(runtime));
|
|
16
|
+
|
|
17
|
+
if (paths.trackerDir) {
|
|
18
|
+
checks.push(await checkTrackerDir(paths.trackerDir));
|
|
19
|
+
}
|
|
20
|
+
if (paths.configPath) {
|
|
21
|
+
checks.push(await checkConfigJson(paths.configPath));
|
|
22
|
+
}
|
|
23
|
+
if (paths.cliPath) {
|
|
24
|
+
checks.push(await checkCliEntrypoint(paths.cliPath));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
checks.push(await checkNetwork({ baseUrl: runtime?.baseUrl || null, fetch }));
|
|
28
|
+
|
|
29
|
+
if (diagnostics) {
|
|
30
|
+
checks.push(...buildDiagnosticsChecks(diagnostics));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const summary = summarizeChecks(checks);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
version: 1,
|
|
37
|
+
generated_at: now().toISOString(),
|
|
38
|
+
ok: summary.critical === 0,
|
|
39
|
+
summary,
|
|
40
|
+
checks,
|
|
41
|
+
diagnostics,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function buildRuntimeChecks(runtime = {}) {
|
|
46
|
+
const checks = [];
|
|
47
|
+
const baseUrl =
|
|
48
|
+
typeof runtime.baseUrl === "string" && runtime.baseUrl.trim() ? runtime.baseUrl.trim() : null;
|
|
49
|
+
const deviceToken =
|
|
50
|
+
typeof runtime.deviceToken === "string" && runtime.deviceToken.trim() ? "set" : "unset";
|
|
51
|
+
const dashboardUrl =
|
|
52
|
+
typeof runtime.dashboardUrl === "string" && runtime.dashboardUrl.trim()
|
|
53
|
+
? runtime.dashboardUrl.trim()
|
|
54
|
+
: null;
|
|
55
|
+
const httpTimeoutMs = Number.isFinite(Number(runtime.httpTimeoutMs))
|
|
56
|
+
? Number(runtime.httpTimeoutMs)
|
|
57
|
+
: null;
|
|
58
|
+
const debug = Boolean(runtime.debug);
|
|
59
|
+
const autoRetryNoSpawn = Boolean(runtime.autoRetryNoSpawn);
|
|
60
|
+
|
|
61
|
+
checks.push({
|
|
62
|
+
id: "runtime.base_url",
|
|
63
|
+
status: baseUrl ? "ok" : "fail",
|
|
64
|
+
detail: baseUrl ? "base_url set" : "base_url missing",
|
|
65
|
+
critical: false,
|
|
66
|
+
meta: {
|
|
67
|
+
base_url: baseUrl,
|
|
68
|
+
source: runtime?.sources?.baseUrl || null,
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
checks.push({
|
|
73
|
+
id: "runtime.device_token",
|
|
74
|
+
status: deviceToken === "set" ? "ok" : "warn",
|
|
75
|
+
detail: deviceToken === "set" ? "device token set" : "device token missing",
|
|
76
|
+
critical: false,
|
|
77
|
+
meta: {
|
|
78
|
+
device_token: deviceToken,
|
|
79
|
+
source: runtime?.sources?.deviceToken || null,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
checks.push({
|
|
84
|
+
id: "runtime.dashboard_url",
|
|
85
|
+
status: "ok",
|
|
86
|
+
detail: dashboardUrl ? "dashboard_url set" : "dashboard_url unset",
|
|
87
|
+
critical: false,
|
|
88
|
+
meta: {
|
|
89
|
+
dashboard_url: dashboardUrl,
|
|
90
|
+
source: runtime?.sources?.dashboardUrl || null,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
checks.push({
|
|
95
|
+
id: "runtime.http_timeout_ms",
|
|
96
|
+
status: "ok",
|
|
97
|
+
detail: "http timeout resolved",
|
|
98
|
+
critical: false,
|
|
99
|
+
meta: {
|
|
100
|
+
http_timeout_ms: httpTimeoutMs,
|
|
101
|
+
source: runtime?.sources?.httpTimeoutMs || null,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
checks.push({
|
|
106
|
+
id: "runtime.debug",
|
|
107
|
+
status: "ok",
|
|
108
|
+
detail: debug ? "debug enabled" : "debug disabled",
|
|
109
|
+
critical: false,
|
|
110
|
+
meta: {
|
|
111
|
+
debug,
|
|
112
|
+
source: runtime?.sources?.debug || null,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
checks.push({
|
|
117
|
+
id: "runtime.auto_retry_no_spawn",
|
|
118
|
+
status: "ok",
|
|
119
|
+
detail: autoRetryNoSpawn ? "auto retry spawn disabled" : "auto retry spawn enabled",
|
|
120
|
+
critical: false,
|
|
121
|
+
meta: {
|
|
122
|
+
auto_retry_no_spawn: autoRetryNoSpawn,
|
|
123
|
+
source: runtime?.sources?.autoRetryNoSpawn || null,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
return checks;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function checkTrackerDir(trackerDir) {
|
|
131
|
+
try {
|
|
132
|
+
const st = await fs.stat(trackerDir);
|
|
133
|
+
if (!st.isDirectory()) {
|
|
134
|
+
return {
|
|
135
|
+
id: "fs.tracker_dir",
|
|
136
|
+
status: "fail",
|
|
137
|
+
detail: "tracker dir is not a directory",
|
|
138
|
+
critical: true,
|
|
139
|
+
meta: { path: trackerDir },
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
await fs.access(trackerDir, constants.R_OK);
|
|
143
|
+
return {
|
|
144
|
+
id: "fs.tracker_dir",
|
|
145
|
+
status: "ok",
|
|
146
|
+
detail: "tracker dir readable",
|
|
147
|
+
critical: false,
|
|
148
|
+
meta: { path: trackerDir },
|
|
149
|
+
};
|
|
150
|
+
} catch (err) {
|
|
151
|
+
if (err && (err.code === "ENOENT" || err.code === "ENOTDIR")) {
|
|
152
|
+
return {
|
|
153
|
+
id: "fs.tracker_dir",
|
|
154
|
+
status: "warn",
|
|
155
|
+
detail: "tracker dir missing",
|
|
156
|
+
critical: false,
|
|
157
|
+
meta: { path: trackerDir },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (err && (err.code === "EACCES" || err.code === "EPERM")) {
|
|
161
|
+
return {
|
|
162
|
+
id: "fs.tracker_dir",
|
|
163
|
+
status: "fail",
|
|
164
|
+
detail: "tracker dir permission denied",
|
|
165
|
+
critical: true,
|
|
166
|
+
meta: { path: trackerDir, code: err.code },
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
id: "fs.tracker_dir",
|
|
171
|
+
status: "fail",
|
|
172
|
+
detail: "tracker dir error",
|
|
173
|
+
critical: true,
|
|
174
|
+
meta: { path: trackerDir, code: err?.code || "error" },
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function checkConfigJson(configPath) {
|
|
180
|
+
const res = await readJsonStrict(configPath);
|
|
181
|
+
if (res.status === "ok") {
|
|
182
|
+
return {
|
|
183
|
+
id: "fs.config_json",
|
|
184
|
+
status: "ok",
|
|
185
|
+
detail: "config.json readable",
|
|
186
|
+
critical: false,
|
|
187
|
+
meta: { path: configPath },
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
if (res.status === "missing") {
|
|
191
|
+
return {
|
|
192
|
+
id: "fs.config_json",
|
|
193
|
+
status: "warn",
|
|
194
|
+
detail: "config.json missing",
|
|
195
|
+
critical: false,
|
|
196
|
+
meta: { path: configPath },
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
if (res.status === "invalid") {
|
|
200
|
+
return {
|
|
201
|
+
id: "fs.config_json",
|
|
202
|
+
status: "fail",
|
|
203
|
+
detail: "config.json invalid",
|
|
204
|
+
critical: true,
|
|
205
|
+
meta: { path: configPath },
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
id: "fs.config_json",
|
|
210
|
+
status: "fail",
|
|
211
|
+
detail: "config.json read error",
|
|
212
|
+
critical: true,
|
|
213
|
+
meta: { path: configPath },
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function checkCliEntrypoint(cliPath) {
|
|
218
|
+
try {
|
|
219
|
+
const st = await fs.stat(cliPath);
|
|
220
|
+
if (!st.isFile()) {
|
|
221
|
+
return {
|
|
222
|
+
id: "cli.entrypoint",
|
|
223
|
+
status: "fail",
|
|
224
|
+
detail: "cli entrypoint is not a file",
|
|
225
|
+
critical: false,
|
|
226
|
+
meta: { path: cliPath },
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
await fs.access(cliPath, constants.R_OK);
|
|
230
|
+
if (process.platform !== "win32") {
|
|
231
|
+
await fs.access(cliPath, constants.X_OK);
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
id: "cli.entrypoint",
|
|
235
|
+
status: "ok",
|
|
236
|
+
detail: "cli entrypoint readable",
|
|
237
|
+
critical: false,
|
|
238
|
+
meta: { path: cliPath },
|
|
239
|
+
};
|
|
240
|
+
} catch (err) {
|
|
241
|
+
return {
|
|
242
|
+
id: "cli.entrypoint",
|
|
243
|
+
status: "fail",
|
|
244
|
+
detail: "cli entrypoint not accessible",
|
|
245
|
+
critical: false,
|
|
246
|
+
meta: { path: cliPath, code: err?.code || "error" },
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function checkNetwork({ baseUrl, fetch }) {
|
|
252
|
+
if (!baseUrl) {
|
|
253
|
+
return {
|
|
254
|
+
id: "network.base_url",
|
|
255
|
+
status: "warn",
|
|
256
|
+
detail: "base_url missing (skipped)",
|
|
257
|
+
critical: false,
|
|
258
|
+
meta: { base_url: null },
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const start = Date.now();
|
|
263
|
+
try {
|
|
264
|
+
if (typeof fetch !== "function") throw new Error("Missing fetch");
|
|
265
|
+
const res = await fetch(baseUrl, { method: "GET" });
|
|
266
|
+
const latency = Date.now() - start;
|
|
267
|
+
return {
|
|
268
|
+
id: "network.base_url",
|
|
269
|
+
status: "ok",
|
|
270
|
+
detail: `HTTP ${res.status} (reachable)`,
|
|
271
|
+
critical: false,
|
|
272
|
+
meta: {
|
|
273
|
+
status_code: res.status,
|
|
274
|
+
latency_ms: latency,
|
|
275
|
+
base_url: baseUrl,
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
} catch (err) {
|
|
279
|
+
const latency = Date.now() - start;
|
|
280
|
+
return {
|
|
281
|
+
id: "network.base_url",
|
|
282
|
+
status: "fail",
|
|
283
|
+
detail: "Network error",
|
|
284
|
+
critical: false,
|
|
285
|
+
meta: {
|
|
286
|
+
error: err?.message || String(err),
|
|
287
|
+
latency_ms: latency,
|
|
288
|
+
base_url: baseUrl,
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function buildDiagnosticsChecks(diagnostics) {
|
|
295
|
+
const checks = [];
|
|
296
|
+
const notify = diagnostics?.notify || {};
|
|
297
|
+
const notifyConfigured = Boolean(
|
|
298
|
+
notify.codex_notify_configured ||
|
|
299
|
+
notify.every_code_notify_configured ||
|
|
300
|
+
notify.claude_hook_configured ||
|
|
301
|
+
notify.gemini_hook_configured ||
|
|
302
|
+
notify.opencode_plugin_configured ||
|
|
303
|
+
notify.openclaw_hook_configured,
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
checks.push({
|
|
307
|
+
id: "notify.configured",
|
|
308
|
+
status: notifyConfigured ? "ok" : "warn",
|
|
309
|
+
detail: notifyConfigured ? "notify configured" : "notify not configured",
|
|
310
|
+
critical: false,
|
|
311
|
+
meta: { configured: notifyConfigured },
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const uploadError = diagnostics?.upload?.last_error || null;
|
|
315
|
+
checks.push({
|
|
316
|
+
id: "upload.last_error",
|
|
317
|
+
status: uploadError ? "warn" : "ok",
|
|
318
|
+
detail: uploadError ? "last upload error present" : "no upload errors",
|
|
319
|
+
critical: false,
|
|
320
|
+
meta: { last_error: uploadError ? uploadError.message || null : null },
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// Dual-install visibility (#306): surface resolved Kiro installs in the
|
|
324
|
+
// human-readable doctor output, not just the embedded diagnostics JSON.
|
|
325
|
+
const kiro = diagnostics?.kiro;
|
|
326
|
+
if (kiro && typeof kiro.wsl_mode === "string") {
|
|
327
|
+
const fmt = (present) => (present ? "yes" : "no");
|
|
328
|
+
const wslPart = kiro.wsl_installs
|
|
329
|
+
? `wsl ide=${fmt(kiro.wsl_installs.ide_present)} cli=${fmt(kiro.wsl_installs.cli_present)}`
|
|
330
|
+
: "wsl not detected";
|
|
331
|
+
checks.push({
|
|
332
|
+
id: "kiro.installs",
|
|
333
|
+
status: "ok",
|
|
334
|
+
detail: `native ide=${fmt(kiro.ide_present)} cli=${fmt(kiro.cli_present)}; ${wslPart} (mode ${kiro.wsl_mode})`,
|
|
335
|
+
critical: false,
|
|
336
|
+
meta: { wsl_mode: kiro.wsl_mode, wsl_installs: kiro.wsl_installs || null },
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Extra scan roots (#657): surface CODEX_HOME / CLAUDE_CONFIG_DIR / config
|
|
341
|
+
// roots in the human-readable report and warn when a configured root is not
|
|
342
|
+
// on disk — a silently missing root is the difference between "no usage"
|
|
343
|
+
// and "not scanned". Installs without extras get no additional line.
|
|
344
|
+
const scanRoots = diagnostics?.scan_roots;
|
|
345
|
+
if (scanRoots && typeof scanRoots === "object") {
|
|
346
|
+
const extras = [];
|
|
347
|
+
for (const provider of ["codex", "claude"]) {
|
|
348
|
+
for (const entry of Array.isArray(scanRoots[provider]) ? scanRoots[provider] : []) {
|
|
349
|
+
if (!entry || entry.origin === "native") continue;
|
|
350
|
+
extras.push({ provider, ...entry });
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (extras.length > 0) {
|
|
354
|
+
const missing = extras.filter((entry) => !entry.exists);
|
|
355
|
+
const describe = (list) => list
|
|
356
|
+
.map((entry) => `${entry.provider} ${entry.path}${entry.error ? ` (unreadable: ${entry.error})` : ""}`)
|
|
357
|
+
.join(", ");
|
|
358
|
+
checks.push({
|
|
359
|
+
id: "scan_roots.extra",
|
|
360
|
+
status: missing.length > 0 ? "warn" : "ok",
|
|
361
|
+
detail: missing.length > 0
|
|
362
|
+
? `${missing.length} of ${extras.length} extra scan root(s) unavailable: ${describe(missing)}`
|
|
363
|
+
: `${extras.length} extra scan root(s) present: ${describe(extras)}`,
|
|
364
|
+
critical: false,
|
|
365
|
+
meta: { roots: extras },
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return checks;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function summarizeChecks(checks = []) {
|
|
374
|
+
const summary = { ok: 0, warn: 0, fail: 0, critical: 0 };
|
|
375
|
+
for (const check of checks) {
|
|
376
|
+
if (!check || typeof check.status !== "string") continue;
|
|
377
|
+
if (check.status === "ok") summary.ok += 1;
|
|
378
|
+
else if (check.status === "warn") summary.warn += 1;
|
|
379
|
+
else if (check.status === "fail") summary.fail += 1;
|
|
380
|
+
if (check.status === "fail" && check.critical) summary.critical += 1;
|
|
381
|
+
}
|
|
382
|
+
return summary;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
module.exports = { buildDoctorReport };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const fs = require("node:fs");
|
|
2
|
+
const os = require("node:os");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const { expandHome } = require("./scan-roots");
|
|
5
|
+
const { readOpencodeDbMessages, parseOpencodeDbIncremental, normalizeOpencodeTokens } = require("./rollout");
|
|
6
|
+
|
|
7
|
+
function resolveDumateDbPaths({ home = os.homedir(), env = process.env, platform = process.platform } = {}) {
|
|
8
|
+
const override = expandHome(env.TOKENUSAGE_DUMATE_HOME, home);
|
|
9
|
+
const appdata = typeof env.APPDATA === "string" && env.APPDATA.trim()
|
|
10
|
+
? env.APPDATA.trim() : path.join(home, "AppData", "Roaming");
|
|
11
|
+
if (!override && platform !== "win32") return [];
|
|
12
|
+
const root = override || path.join(appdata, "qianfan-desktop-app", "qianfan_desk_xdg");
|
|
13
|
+
// The override accepts the data/opencode directory or an install's XDG root.
|
|
14
|
+
const candidates = [path.join(root, "opencode.db"), path.join(root, "data", "opencode", "opencode.db")];
|
|
15
|
+
try {
|
|
16
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
17
|
+
if (entry.isDirectory()) candidates.push(path.join(root, entry.name, "data", "opencode", "opencode.db"));
|
|
18
|
+
}
|
|
19
|
+
} catch (_e) { }
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
return candidates.sort().filter((candidate) => {
|
|
22
|
+
try {
|
|
23
|
+
if (!fs.statSync(candidate).isFile()) return false;
|
|
24
|
+
const realPath = fs.realpathSync(candidate);
|
|
25
|
+
if (seen.has(realPath)) return false;
|
|
26
|
+
seen.add(realPath);
|
|
27
|
+
return true;
|
|
28
|
+
} catch (_e) { return false; }
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function parseDumateIncremental({ dbPaths, ...options }) {
|
|
33
|
+
// Reinstalls can copy the same sessions into a new install-id directory.
|
|
34
|
+
// Share a message cursor across installations and choose the newest copy
|
|
35
|
+
// before parsing, so stale copies cannot roll back a completed turn.
|
|
36
|
+
const messages = new Map();
|
|
37
|
+
const includeZeroMessage = (message) => {
|
|
38
|
+
const provider = message.data?.providerID || message.data?.model?.providerID;
|
|
39
|
+
return String(provider || "").toLowerCase() === "qianfanpersonalquota";
|
|
40
|
+
};
|
|
41
|
+
for (const dbPath of dbPaths) {
|
|
42
|
+
for (const message of readOpencodeDbMessages(dbPath, {}, { propagateReadFailure: true, includeZeroMessage })) {
|
|
43
|
+
// DuMate 1.0.83 bundles OpenCode's import command without an import
|
|
44
|
+
// marker. Its own runtime uses QianfanPersonalQuota; other providers
|
|
45
|
+
// can be imported history already counted under Claude or OpenCode.
|
|
46
|
+
const provider = message.data?.providerID || message.data?.model?.providerID;
|
|
47
|
+
if (String(provider || "").toLowerCase() !== "qianfanpersonalquota") continue;
|
|
48
|
+
const id = message.data?.id || message.id;
|
|
49
|
+
const session = message.data?.sessionID || message.sessionID || "";
|
|
50
|
+
if (!id) continue;
|
|
51
|
+
const key = `${session}|${id}`;
|
|
52
|
+
if (Number(message.timeUpdated || 0) < Number(options.cursors?.dumate?.messages?.[key]?.sourceUpdatedAt || 0)) continue;
|
|
53
|
+
const previous = messages.get(key);
|
|
54
|
+
if (!previous || Number(message.timeUpdated || 0) > Number(previous.message.timeUpdated || 0)) {
|
|
55
|
+
messages.set(key, { message, dbPath });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const byDatabase = new Map();
|
|
60
|
+
for (const [key, { message, dbPath }] of messages) {
|
|
61
|
+
// DuMate's OpenAI-compatible adapter retains reasoning inside output.
|
|
62
|
+
// The shared parser expects separate columns, so subtract that subset once.
|
|
63
|
+
const usage = normalizeOpencodeTokens(message.data?.tokens);
|
|
64
|
+
// Zero copies participate in latest-copy selection so a stale install
|
|
65
|
+
// cannot resurrect usage. Only a previously counted message needs a
|
|
66
|
+
// queued retraction; a new empty message contributes nothing.
|
|
67
|
+
const previous = options.cursors?.dumate?.messages?.[key];
|
|
68
|
+
if (usage?.total_tokens === 0 && (!previous?.lastTotals || previous.dedupedForkCopy === true)) continue;
|
|
69
|
+
if (!byDatabase.has(dbPath)) byDatabase.set(dbPath, []);
|
|
70
|
+
const normalized = usage ? {
|
|
71
|
+
...message,
|
|
72
|
+
data: { ...message.data, tokens: { ...message.data.tokens,
|
|
73
|
+
output: Math.max(0, usage.output_tokens - usage.reasoning_output_tokens),
|
|
74
|
+
} },
|
|
75
|
+
} : message;
|
|
76
|
+
byDatabase.get(dbPath).push(normalized);
|
|
77
|
+
}
|
|
78
|
+
const result = { recordsProcessed: 0, eventsAggregated: 0, bucketsQueued: 0 };
|
|
79
|
+
for (const [dbPath, dbMessages] of byDatabase) {
|
|
80
|
+
const parsed = await parseOpencodeDbIncremental({
|
|
81
|
+
...options, dbPath, dbMessages, source: "dumate", cursorKey: "dumate",
|
|
82
|
+
});
|
|
83
|
+
for (const message of dbMessages) {
|
|
84
|
+
const key = `${message.data?.sessionID || message.sessionID || ""}|${message.data?.id || message.id}`;
|
|
85
|
+
const cursor = options.cursors?.dumate?.messages?.[key];
|
|
86
|
+
if (cursor) cursor.sourceUpdatedAt = Number(message.timeUpdated || 0);
|
|
87
|
+
}
|
|
88
|
+
result.recordsProcessed += parsed.messagesProcessed || 0;
|
|
89
|
+
result.eventsAggregated += parsed.eventsAggregated || 0;
|
|
90
|
+
result.bucketsQueued += parsed.bucketsQueued || 0;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { resolveDumateDbPaths, parseDumateIncremental };
|