tokenusage-cli 0.1.0 → 0.2.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.
Files changed (131) hide show
  1. package/CHANGELOG.md +39 -19
  2. package/LICENSE +22 -22
  3. package/README.md +94 -94
  4. package/THIRD_PARTY_NOTICES.md +140 -140
  5. package/bin/tokenusage.js +69 -69
  6. package/package.json +59 -59
  7. package/src/cli.js +146 -134
  8. package/src/commands/device-login.js +205 -205
  9. package/src/commands/diagnostics.js +38 -38
  10. package/src/commands/doctor.js +110 -110
  11. package/src/commands/init.js +2484 -2436
  12. package/src/commands/pause.js +77 -0
  13. package/src/commands/sessions.js +29 -29
  14. package/src/commands/status.js +1758 -1747
  15. package/src/commands/sync.js +8048 -7934
  16. package/src/commands/uninstall.js +322 -312
  17. package/src/lib/account-usage-dedup.js +267 -267
  18. package/src/lib/antigravity-paths.js +48 -48
  19. package/src/lib/ark-agent-plan-limits.js +438 -438
  20. package/src/lib/ark-coding-plan-limits.js +537 -537
  21. package/src/lib/async-queue-reader.js +138 -138
  22. package/src/lib/atomic-json.js +27 -27
  23. package/src/lib/bearer-token.js +11 -11
  24. package/src/lib/categorizer-utils.js +232 -232
  25. package/src/lib/claude-categorizer.js +1343 -1343
  26. package/src/lib/claude-config.js +279 -279
  27. package/src/lib/claude-user-identity.js +56 -56
  28. package/src/lib/cli-ui.js +179 -179
  29. package/src/lib/cloud-account.js +541 -541
  30. package/src/lib/cloud-device-token.js +141 -141
  31. package/src/lib/cloud-sync-prefs.js +14 -14
  32. package/src/lib/cloud-usage-reset.js +115 -115
  33. package/src/lib/codex-config.js +595 -595
  34. package/src/lib/codex-context-breakdown.js +806 -806
  35. package/src/lib/codex-model-attribution.js +128 -128
  36. package/src/lib/codex-rollout-parser.js +1471 -1471
  37. package/src/lib/codex-service-tier.js +49 -49
  38. package/src/lib/codex-token-refresh.js +140 -140
  39. package/src/lib/codex-token-usage.js +195 -195
  40. package/src/lib/codex-usage-record.js +195 -195
  41. package/src/lib/collection-pause.js +77 -0
  42. package/src/lib/command-runner.js +313 -313
  43. package/src/lib/commandcode-limits.js +317 -317
  44. package/src/lib/context-health.js +124 -124
  45. package/src/lib/cursor-config.js +605 -605
  46. package/src/lib/cursor-store.js +1918 -1918
  47. package/src/lib/debug-flags.js +7 -7
  48. package/src/lib/devin-limits.js +249 -249
  49. package/src/lib/devin-usage.js +212 -212
  50. package/src/lib/diagnostics.js +383 -383
  51. package/src/lib/doctor.js +385 -385
  52. package/src/lib/dumate.js +95 -95
  53. package/src/lib/fs.js +455 -455
  54. package/src/lib/function-url.js +14 -14
  55. package/src/lib/gemini-config.js +291 -291
  56. package/src/lib/git-outcomes.js +243 -243
  57. package/src/lib/grok-context-breakdown.js +890 -890
  58. package/src/lib/grok-hook.js +360 -360
  59. package/src/lib/grok-limits.js +865 -865
  60. package/src/lib/grok-usage.js +98 -98
  61. package/src/lib/heatmap-compact.js +114 -114
  62. package/src/lib/ingest-sanitize.js +41 -41
  63. package/src/lib/init-flow.js +42 -42
  64. package/src/lib/install-resolver.js +170 -170
  65. package/src/lib/jsonl-lines.js +134 -134
  66. package/src/lib/kimi-model-repair.js +173 -173
  67. package/src/lib/kimi-profile.js +61 -61
  68. package/src/lib/kimi-session.js +216 -216
  69. package/src/lib/local-api.js +3588 -3588
  70. package/src/lib/machine-id.js +238 -238
  71. package/src/lib/multi-install-parser.js +113 -113
  72. package/src/lib/notify-command.js +38 -38
  73. package/src/lib/omp-hook.js +589 -589
  74. package/src/lib/openclaw-cli.js +104 -104
  75. package/src/lib/openclaw-hook.js +375 -375
  76. package/src/lib/openclaw-session-plugin.js +560 -560
  77. package/src/lib/opencode-config.js +121 -121
  78. package/src/lib/opencode-go-limits.js +807 -807
  79. package/src/lib/outcomes-engine.js +253 -253
  80. package/src/lib/passive-mode.js +219 -219
  81. package/src/lib/pet-packages.js +722 -722
  82. package/src/lib/pricing/curated-overrides.json +822 -822
  83. package/src/lib/pricing/index.js +379 -379
  84. package/src/lib/pricing/litellm-fetcher.js +172 -172
  85. package/src/lib/pricing/matcher.js +456 -456
  86. package/src/lib/pricing/seed-snapshot.json +1 -1
  87. package/src/lib/progress.js +76 -76
  88. package/src/lib/project-usage-purge.js +117 -117
  89. package/src/lib/prompt.js +24 -24
  90. package/src/lib/provider-status.js +104 -104
  91. package/src/lib/proxy-env.js +489 -489
  92. package/src/lib/proxy-settings.js +163 -163
  93. package/src/lib/qoder-cn-cli-limits.js +119 -119
  94. package/src/lib/qoder-limits.js +880 -880
  95. package/src/lib/quiet-warnings.js +26 -26
  96. package/src/lib/rollout.js +26005 -25966
  97. package/src/lib/runtime-config.js +9 -4
  98. package/src/lib/scan-roots.js +277 -277
  99. package/src/lib/scheduled-task.js +145 -0
  100. package/src/lib/sentry.js +231 -231
  101. package/src/lib/session-analytics.js +2972 -2972
  102. package/src/lib/session-performance.js +237 -237
  103. package/src/lib/session-upload.js +379 -379
  104. package/src/lib/skill-usage.js +267 -267
  105. package/src/lib/skills-manager.js +1591 -1591
  106. package/src/lib/source-metadata.js +48 -48
  107. package/src/lib/sqlite-reader.js +215 -215
  108. package/src/lib/star-cta.js +73 -73
  109. package/src/lib/subscription-manager.js +293 -293
  110. package/src/lib/subscriptions.js +508 -508
  111. package/src/lib/sync-report.js +138 -0
  112. package/src/lib/telemetry.js +193 -193
  113. package/src/lib/tracker-paths.js +16 -16
  114. package/src/lib/trae-cn-config.js +548 -548
  115. package/src/lib/trae-db.js +127 -127
  116. package/src/lib/trae-snapshot.js +230 -230
  117. package/src/lib/trae-sqlite.js +143 -143
  118. package/src/lib/trae-usage.js +121 -121
  119. package/src/lib/upload-queue.js +134 -134
  120. package/src/lib/upload-throttle.js +171 -171
  121. package/src/lib/usage-limits.js +4552 -4552
  122. package/src/lib/wrapped-aggregator.js +236 -236
  123. package/src/lib/wsl-probe.js +281 -281
  124. package/src/lib/zcode-limits.js +1172 -1172
  125. package/src/lib/zcode-reset-credits.js +66 -66
  126. package/test/audit-fixes.test.js +200 -0
  127. package/test/cli-smoke.test.js +53 -53
  128. package/test/collection-controls.test.js +181 -0
  129. package/test/ingest-sanitize.test.js +162 -162
  130. package/test/quiet-warnings.test.js +53 -53
  131. package/test/session-upload.test.js +427 -427
package/src/lib/doctor.js CHANGED
@@ -1,385 +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 };
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 };