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
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+
3
+ // Windows Task Scheduler registration for the hourly auto sync (0.2.0).
4
+ //
5
+ // init "takes over" the registration that 0.1.0 left to a manual runbook:
6
+ // the task action launches a VBS wrapper (GUI subsystem — no conhost flash
7
+ // on Interactive tasks), which runs `node <cli.js> sync` fully hidden and
8
+ // propagates the exit code via WScript.Quit. The CLI logs to a rolling
9
+ // <trackerDir>/logs/sync.log itself (autoLog sink), so the registration no
10
+ // longer needs a `>>` redirect — which also removes the cmd /c quoting
11
+ // pitfall from the manual setup. pause/resume toggle the task with
12
+ // /DISABLE //ENABLE so the hourly trigger stops firing while paused; the
13
+ // pause flag in collection-pause.js guards everything else (notify/retry/
14
+ // watch) cross-platform.
15
+ //
16
+ // Everything here is best-effort: a missing schtasks or an access error must
17
+ // never fail init/pause/resume — the pause flag alone already stops syncs.
18
+
19
+ const { execFileSync } = require("node:child_process");
20
+ const fs = require("node:fs");
21
+ const path = require("node:path");
22
+
23
+ const TASK_NAME = "TokenUsageSync";
24
+ const VBS_FILE = "run-sync-hidden.vbs";
25
+
26
+ function isWindows() {
27
+ return process.platform === "win32";
28
+ }
29
+
30
+ function vbsPathFor(binDir) {
31
+ return path.join(binDir, VBS_FILE);
32
+ }
33
+
34
+ /**
35
+ * The hidden-runner wrapper. Paths are quoted individually and cmd is NOT
36
+ * involved: wscript runs node directly, so the cmd /c outer-quote-stripping
37
+ * pitfall (paths with spaces) never applies.
38
+ */
39
+ function buildHiddenRunnerVbs({ nodeExe, cliJs }) {
40
+ return [
41
+ "' Token Usage — hidden sync runner.",
42
+ "' The scheduled task launches this script (GUI subsystem: no conhost",
43
+ "' flash); it runs the sync fully hidden and returns its exit code so the",
44
+ "' task's LastTaskResult stays meaningful. Sync output lands in the CLI's",
45
+ "' own rolling logs/sync.log — no shell redirect involved.",
46
+ "Dim shell",
47
+ "Set shell = CreateObject(\"WScript.Shell\")",
48
+ `WScript.Quit shell.Run("""${nodeExe}"" ""${cliJs}"" sync", 0, True)`,
49
+ "",
50
+ ].join("\n");
51
+ }
52
+
53
+ function writeHiddenRunner({ binDir, nodeExe = process.execPath, cliJs }) {
54
+ fs.mkdirSync(binDir, { recursive: true });
55
+ const vbsPath = vbsPathFor(binDir);
56
+ fs.writeFileSync(vbsPath, buildHiddenRunnerVbs({ nodeExe, cliJs }), { mode: 0o600 });
57
+ return vbsPath;
58
+ }
59
+
60
+ /**
61
+ * Register (idempotently — /F overwrites) the hourly hidden task. Returns
62
+ * {registered: true} or {registered: false, reason} instead of throwing.
63
+ */
64
+ function registerWindowsScheduledTask({ binDir, cliJs, nodeExe = process.execPath }) {
65
+ if (!isWindows()) return { registered: false, reason: "not_windows" };
66
+ try {
67
+ const vbsPath = writeHiddenRunner({ binDir, nodeExe, cliJs });
68
+ execFileSync(
69
+ "schtasks",
70
+ [
71
+ "/Create", "/F",
72
+ "/SC", "HOURLY", "/MO", "1",
73
+ "/TN", TASK_NAME,
74
+ "/TR", `wscript.exe "${vbsPath}"`,
75
+ ],
76
+ { stdio: "pipe", windowsHide: true },
77
+ );
78
+ return { registered: true, vbsPath };
79
+ } catch (error) {
80
+ return { registered: false, reason: String(error?.message || error).slice(0, 200) };
81
+ }
82
+ }
83
+
84
+ // Existence probe so pause/resume can report "no task" (expected on machines
85
+ // that never registered it) distinctly from a real toggle failure. schtasks
86
+ // output is localized; exit codes are not, so this uses /Query success only.
87
+ function windowsTaskExists() {
88
+ if (!isWindows()) return null;
89
+ try {
90
+ execFileSync("schtasks", ["/Query", "/TN", TASK_NAME], {
91
+ stdio: "pipe",
92
+ windowsHide: true,
93
+ });
94
+ return true;
95
+ } catch {
96
+ return false;
97
+ }
98
+ }
99
+
100
+ // Best-effort enable/disable of the hourly task (pause/resume). "Task
101
+ // missing" is an expected, non-fatal state (uninstalled task, non-Windows,
102
+ // never-registered installs) — the pause flag still guards every auto flavor.
103
+ function setWindowsTaskEnabled(enabled) {
104
+ if (!isWindows()) return { changed: false, reason: "not_windows" };
105
+ if (windowsTaskExists() === false) {
106
+ return { changed: false, reason: "task_missing" };
107
+ }
108
+ try {
109
+ execFileSync(
110
+ "schtasks",
111
+ ["/Change", "/TN", TASK_NAME, enabled ? "/ENABLE" : "/DISABLE"],
112
+ { stdio: "pipe", windowsHide: true },
113
+ );
114
+ return { changed: true };
115
+ } catch (error) {
116
+ return { changed: false, reason: "error", detail: String(error?.message || error).slice(0, 200) };
117
+ }
118
+ }
119
+
120
+ // uninstall teardown: delete the task (best-effort, idempotent).
121
+ function deleteWindowsScheduledTask() {
122
+ if (!isWindows()) return { deleted: false, reason: "not_windows" };
123
+ try {
124
+ execFileSync("schtasks", ["/Delete", "/F", "/TN", TASK_NAME], {
125
+ stdio: "pipe",
126
+ windowsHide: true,
127
+ });
128
+ return { deleted: true };
129
+ } catch (error) {
130
+ return { deleted: false, reason: String(error?.message || error).slice(0, 200) };
131
+ }
132
+ }
133
+
134
+ module.exports = {
135
+ TASK_NAME,
136
+ VBS_FILE,
137
+ isWindows,
138
+ vbsPathFor,
139
+ buildHiddenRunnerVbs,
140
+ writeHiddenRunner,
141
+ registerWindowsScheduledTask,
142
+ windowsTaskExists,
143
+ setWindowsTaskEnabled,
144
+ deleteWindowsScheduledTask,
145
+ };
package/src/lib/sentry.js CHANGED
@@ -1,231 +1,231 @@
1
- "use strict";
2
-
3
- /**
4
- * Optional Sentry error reporting for the Node CLI / EmbeddedServer runtime.
5
- *
6
- * Privacy contract (keep in sync with docs/PRIVACY.md and dashboard/src/lib/sentry.js):
7
- * - Off unless TOKENUSAGE_SENTRY_DSN (or SENTRY_DSN) is set.
8
- * - Honors the same telemetry opt-out as the daily heartbeat
9
- * (TOKENUSAGE_NO_TELEMETRY / DO_NOT_TRACK / config.json `"telemetry": false`).
10
- * - Fail-closed in the node test runner.
11
- * - Never intended to carry prompts, code, or provider secrets; beforeSend
12
- * scrubs common secret patterns from event payloads.
13
- */
14
-
15
- const path = require("node:path");
16
- const pkg = require("../../package.json");
17
- const { readJson } = require("./fs");
18
- const { isTelemetryDisabled } = require("./telemetry");
19
-
20
- const SECRET_PATTERN =
21
- /(?:api[_-]?key|access[_-]?token|refresh[_-]?token|cookie|secret|password|dsn)[=:\s]+["']?[^\s"'\\]{8,}/gi;
22
- const BEARER_PATTERN = /\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi;
23
- const JWT_PATTERN = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,}\.[A-Za-z0-9._-]{10,}\b/g;
24
-
25
- let initialized = false;
26
-
27
- function resolveDsn(env = process.env) {
28
- const raw = env?.TOKENUSAGE_SENTRY_DSN || env?.SENTRY_DSN || "";
29
- return typeof raw === "string" ? raw.trim() : "";
30
- }
31
-
32
- function resolveEnvironment(env = process.env) {
33
- const raw = typeof env?.TOKENUSAGE_SENTRY_ENVIRONMENT === "string"
34
- ? env.TOKENUSAGE_SENTRY_ENVIRONMENT.trim()
35
- : "";
36
- if (raw) return raw;
37
- if (env?.NODE_ENV === "production") return "production";
38
- return "development";
39
- }
40
-
41
- function resolveShell(env = process.env) {
42
- const raw = typeof env?.TOKENUSAGE_APP_SHELL === "string"
43
- ? env.TOKENUSAGE_APP_SHELL.trim().toLowerCase()
44
- : "";
45
- if (raw === "macos" || raw === "windows" || raw === "linux" || raw === "cli") return raw;
46
- return "cli";
47
- }
48
-
49
- function scrubString(value) {
50
- if (typeof value !== "string" || !value) return value;
51
- return value
52
- .replace(BEARER_PATTERN, "Bearer [Filtered]")
53
- .replace(JWT_PATTERN, "[Filtered]")
54
- .replace(SECRET_PATTERN, "[Filtered]");
55
- }
56
-
57
- function scrubValue(value, depth = 0) {
58
- if (depth > 6 || value == null) return value;
59
- if (typeof value === "string") return scrubString(value);
60
- if (Array.isArray(value)) return value.map((item) => scrubValue(item, depth + 1));
61
- if (typeof value === "object") {
62
- const out = {};
63
- for (const [key, nested] of Object.entries(value)) {
64
- const lower = key.toLowerCase();
65
- if (
66
- lower.includes("authorization") ||
67
- lower.includes("cookie") ||
68
- lower.includes("password") ||
69
- lower.includes("secret") ||
70
- lower.includes("token") ||
71
- lower.includes("api_key") ||
72
- lower.includes("apikey")
73
- ) {
74
- out[key] = "[Filtered]";
75
- } else {
76
- out[key] = scrubValue(nested, depth + 1);
77
- }
78
- }
79
- return out;
80
- }
81
- return value;
82
- }
83
-
84
- function truncateMaybe(value, max = 240) {
85
- if (typeof value !== "string") return value;
86
- if (value.length <= max) return value;
87
- return `${value.slice(0, max)}…[truncated]`;
88
- }
89
-
90
- function scrubEvent(event) {
91
- if (!event || typeof event !== "object") return event;
92
- const next = { ...event };
93
- if (next.message) next.message = truncateMaybe(scrubString(next.message));
94
- if (next.extra) next.extra = scrubValue(next.extra);
95
- if (next.contexts) next.contexts = scrubValue(next.contexts);
96
- if (next.request) next.request = scrubValue(next.request);
97
- // Console breadcrumbs can carry parser / JSON.parse excerpts of local logs
98
- // (prompt text). Drop them entirely; keep only non-console crumbs and scrub.
99
- if (Array.isArray(next.breadcrumbs)) {
100
- next.breadcrumbs = next.breadcrumbs
101
- .filter((crumb) => crumb && crumb.category !== "console")
102
- .map((crumb) => scrubValue(crumb));
103
- }
104
- if (next.exception?.values) {
105
- next.exception = {
106
- ...next.exception,
107
- values: next.exception.values.map((item) => ({
108
- ...item,
109
- value: truncateMaybe(scrubString(item?.value)),
110
- })),
111
- };
112
- }
113
- // Never attach IP / user PII by default.
114
- if (next.user) {
115
- next.user = { id: typeof next.user.id === "string" ? next.user.id : undefined };
116
- }
117
- return next;
118
- }
119
-
120
- /**
121
- * Initialise Sentry once for this process. Never throws.
122
- * @returns {Promise<{ enabled: boolean, reason?: string }>}
123
- */
124
- async function initSentry({
125
- env = process.env,
126
- trackerDir,
127
- version = pkg.version,
128
- } = {}) {
129
- try {
130
- if (initialized) return { enabled: true, reason: "already-initialized" };
131
- if (env?.NODE_TEST_CONTEXT !== undefined) {
132
- return { enabled: false, reason: "test-env" };
133
- }
134
-
135
- const dsn = resolveDsn(env);
136
- if (!dsn) return { enabled: false, reason: "no-dsn" };
137
-
138
- let config = {};
139
- if (trackerDir) {
140
- config = (await readJson(path.join(trackerDir, "config.json"))) || {};
141
- }
142
- if (isTelemetryDisabled({ env, config })) {
143
- return { enabled: false, reason: "opt-out" };
144
- }
145
-
146
- // Honor the same outbound proxy / fail-closed policy as the rest of the CLI.
147
- // Sentry's Node transport uses node:https (not undici), so pass proxy explicitly.
148
- const {
149
- readPersistedProxyConfig,
150
- resolveEffectiveProxySource,
151
- } = require("./proxy-env");
152
- const proxyConfig = readPersistedProxyConfig();
153
- const proxyResolved = resolveEffectiveProxySource({ env, proxyConfig });
154
- if (proxyResolved?.source === "blocked") {
155
- return { enabled: false, reason: "proxy-blocked" };
156
- }
157
-
158
- // Lazy require keeps unit tests that never call init from paying the
159
- // module graph cost at import time of unrelated modules.
160
- const Sentry = require("@sentry/node");
161
- Sentry.init({
162
- dsn,
163
- environment: resolveEnvironment(env),
164
- release: `tokenusage-cli@${String(version || "").trim() || "0.0.0"}`,
165
- sendDefaultPii: false,
166
- tracesSampleRate: 0,
167
- // Errors only: drop process-session + console; replace Http so request
168
- // sessions stay off (Http defaults sessions:true and bypasses beforeSend).
169
- integrations(defaults) {
170
- const filtered = defaults.filter((integration) => {
171
- const name = integration?.name || "";
172
- return name !== "ProcessSession" && name !== "Console" && name !== "Http";
173
- });
174
- filtered.push(Sentry.httpIntegration({ sessions: false }));
175
- return filtered;
176
- },
177
- transportOptions: proxyResolved?.proxyUrl
178
- ? { proxy: proxyResolved.proxyUrl }
179
- : undefined,
180
- beforeSend(event) {
181
- return scrubEvent(event);
182
- },
183
- initialScope: {
184
- tags: {
185
- shell: resolveShell(env),
186
- runtime: "node",
187
- },
188
- },
189
- });
190
- initialized = true;
191
- return { enabled: true };
192
- } catch {
193
- return { enabled: false, reason: "init-failed" };
194
- }
195
- }
196
-
197
- function captureException(error, context) {
198
- if (!initialized) return;
199
- try {
200
- const Sentry = require("@sentry/node");
201
- if (context) {
202
- Sentry.withScope((scope) => {
203
- if (context.tags) {
204
- for (const [key, value] of Object.entries(context.tags)) {
205
- scope.setTag(key, String(value));
206
- }
207
- }
208
- if (context.extra) scope.setExtras(scrubValue(context.extra));
209
- Sentry.captureException(error);
210
- });
211
- return;
212
- }
213
- Sentry.captureException(error);
214
- } catch {
215
- /* never break the CLI */
216
- }
217
- }
218
-
219
- module.exports = {
220
- initSentry,
221
- captureException,
222
- resolveDsn,
223
- resolveEnvironment,
224
- resolveShell,
225
- scrubEvent,
226
- scrubString,
227
- // test helpers
228
- _resetForTests() {
229
- initialized = false;
230
- },
231
- };
1
+ "use strict";
2
+
3
+ /**
4
+ * Optional Sentry error reporting for the Node CLI / EmbeddedServer runtime.
5
+ *
6
+ * Privacy contract (keep in sync with docs/PRIVACY.md and dashboard/src/lib/sentry.js):
7
+ * - Off unless TOKENUSAGE_SENTRY_DSN (or SENTRY_DSN) is set.
8
+ * - Honors the same telemetry opt-out as the daily heartbeat
9
+ * (TOKENUSAGE_NO_TELEMETRY / DO_NOT_TRACK / config.json `"telemetry": false`).
10
+ * - Fail-closed in the node test runner.
11
+ * - Never intended to carry prompts, code, or provider secrets; beforeSend
12
+ * scrubs common secret patterns from event payloads.
13
+ */
14
+
15
+ const path = require("node:path");
16
+ const pkg = require("../../package.json");
17
+ const { readJson } = require("./fs");
18
+ const { isTelemetryDisabled } = require("./telemetry");
19
+
20
+ const SECRET_PATTERN =
21
+ /(?:api[_-]?key|access[_-]?token|refresh[_-]?token|cookie|secret|password|dsn)[=:\s]+["']?[^\s"'\\]{8,}/gi;
22
+ const BEARER_PATTERN = /\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi;
23
+ const JWT_PATTERN = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,}\.[A-Za-z0-9._-]{10,}\b/g;
24
+
25
+ let initialized = false;
26
+
27
+ function resolveDsn(env = process.env) {
28
+ const raw = env?.TOKENUSAGE_SENTRY_DSN || env?.SENTRY_DSN || "";
29
+ return typeof raw === "string" ? raw.trim() : "";
30
+ }
31
+
32
+ function resolveEnvironment(env = process.env) {
33
+ const raw = typeof env?.TOKENUSAGE_SENTRY_ENVIRONMENT === "string"
34
+ ? env.TOKENUSAGE_SENTRY_ENVIRONMENT.trim()
35
+ : "";
36
+ if (raw) return raw;
37
+ if (env?.NODE_ENV === "production") return "production";
38
+ return "development";
39
+ }
40
+
41
+ function resolveShell(env = process.env) {
42
+ const raw = typeof env?.TOKENUSAGE_APP_SHELL === "string"
43
+ ? env.TOKENUSAGE_APP_SHELL.trim().toLowerCase()
44
+ : "";
45
+ if (raw === "macos" || raw === "windows" || raw === "linux" || raw === "cli") return raw;
46
+ return "cli";
47
+ }
48
+
49
+ function scrubString(value) {
50
+ if (typeof value !== "string" || !value) return value;
51
+ return value
52
+ .replace(BEARER_PATTERN, "Bearer [Filtered]")
53
+ .replace(JWT_PATTERN, "[Filtered]")
54
+ .replace(SECRET_PATTERN, "[Filtered]");
55
+ }
56
+
57
+ function scrubValue(value, depth = 0) {
58
+ if (depth > 6 || value == null) return value;
59
+ if (typeof value === "string") return scrubString(value);
60
+ if (Array.isArray(value)) return value.map((item) => scrubValue(item, depth + 1));
61
+ if (typeof value === "object") {
62
+ const out = {};
63
+ for (const [key, nested] of Object.entries(value)) {
64
+ const lower = key.toLowerCase();
65
+ if (
66
+ lower.includes("authorization") ||
67
+ lower.includes("cookie") ||
68
+ lower.includes("password") ||
69
+ lower.includes("secret") ||
70
+ lower.includes("token") ||
71
+ lower.includes("api_key") ||
72
+ lower.includes("apikey")
73
+ ) {
74
+ out[key] = "[Filtered]";
75
+ } else {
76
+ out[key] = scrubValue(nested, depth + 1);
77
+ }
78
+ }
79
+ return out;
80
+ }
81
+ return value;
82
+ }
83
+
84
+ function truncateMaybe(value, max = 240) {
85
+ if (typeof value !== "string") return value;
86
+ if (value.length <= max) return value;
87
+ return `${value.slice(0, max)}…[truncated]`;
88
+ }
89
+
90
+ function scrubEvent(event) {
91
+ if (!event || typeof event !== "object") return event;
92
+ const next = { ...event };
93
+ if (next.message) next.message = truncateMaybe(scrubString(next.message));
94
+ if (next.extra) next.extra = scrubValue(next.extra);
95
+ if (next.contexts) next.contexts = scrubValue(next.contexts);
96
+ if (next.request) next.request = scrubValue(next.request);
97
+ // Console breadcrumbs can carry parser / JSON.parse excerpts of local logs
98
+ // (prompt text). Drop them entirely; keep only non-console crumbs and scrub.
99
+ if (Array.isArray(next.breadcrumbs)) {
100
+ next.breadcrumbs = next.breadcrumbs
101
+ .filter((crumb) => crumb && crumb.category !== "console")
102
+ .map((crumb) => scrubValue(crumb));
103
+ }
104
+ if (next.exception?.values) {
105
+ next.exception = {
106
+ ...next.exception,
107
+ values: next.exception.values.map((item) => ({
108
+ ...item,
109
+ value: truncateMaybe(scrubString(item?.value)),
110
+ })),
111
+ };
112
+ }
113
+ // Never attach IP / user PII by default.
114
+ if (next.user) {
115
+ next.user = { id: typeof next.user.id === "string" ? next.user.id : undefined };
116
+ }
117
+ return next;
118
+ }
119
+
120
+ /**
121
+ * Initialise Sentry once for this process. Never throws.
122
+ * @returns {Promise<{ enabled: boolean, reason?: string }>}
123
+ */
124
+ async function initSentry({
125
+ env = process.env,
126
+ trackerDir,
127
+ version = pkg.version,
128
+ } = {}) {
129
+ try {
130
+ if (initialized) return { enabled: true, reason: "already-initialized" };
131
+ if (env?.NODE_TEST_CONTEXT !== undefined) {
132
+ return { enabled: false, reason: "test-env" };
133
+ }
134
+
135
+ const dsn = resolveDsn(env);
136
+ if (!dsn) return { enabled: false, reason: "no-dsn" };
137
+
138
+ let config = {};
139
+ if (trackerDir) {
140
+ config = (await readJson(path.join(trackerDir, "config.json"))) || {};
141
+ }
142
+ if (isTelemetryDisabled({ env, config })) {
143
+ return { enabled: false, reason: "opt-out" };
144
+ }
145
+
146
+ // Honor the same outbound proxy / fail-closed policy as the rest of the CLI.
147
+ // Sentry's Node transport uses node:https (not undici), so pass proxy explicitly.
148
+ const {
149
+ readPersistedProxyConfig,
150
+ resolveEffectiveProxySource,
151
+ } = require("./proxy-env");
152
+ const proxyConfig = readPersistedProxyConfig();
153
+ const proxyResolved = resolveEffectiveProxySource({ env, proxyConfig });
154
+ if (proxyResolved?.source === "blocked") {
155
+ return { enabled: false, reason: "proxy-blocked" };
156
+ }
157
+
158
+ // Lazy require keeps unit tests that never call init from paying the
159
+ // module graph cost at import time of unrelated modules.
160
+ const Sentry = require("@sentry/node");
161
+ Sentry.init({
162
+ dsn,
163
+ environment: resolveEnvironment(env),
164
+ release: `tokenusage-cli@${String(version || "").trim() || "0.0.0"}`,
165
+ sendDefaultPii: false,
166
+ tracesSampleRate: 0,
167
+ // Errors only: drop process-session + console; replace Http so request
168
+ // sessions stay off (Http defaults sessions:true and bypasses beforeSend).
169
+ integrations(defaults) {
170
+ const filtered = defaults.filter((integration) => {
171
+ const name = integration?.name || "";
172
+ return name !== "ProcessSession" && name !== "Console" && name !== "Http";
173
+ });
174
+ filtered.push(Sentry.httpIntegration({ sessions: false }));
175
+ return filtered;
176
+ },
177
+ transportOptions: proxyResolved?.proxyUrl
178
+ ? { proxy: proxyResolved.proxyUrl }
179
+ : undefined,
180
+ beforeSend(event) {
181
+ return scrubEvent(event);
182
+ },
183
+ initialScope: {
184
+ tags: {
185
+ shell: resolveShell(env),
186
+ runtime: "node",
187
+ },
188
+ },
189
+ });
190
+ initialized = true;
191
+ return { enabled: true };
192
+ } catch {
193
+ return { enabled: false, reason: "init-failed" };
194
+ }
195
+ }
196
+
197
+ function captureException(error, context) {
198
+ if (!initialized) return;
199
+ try {
200
+ const Sentry = require("@sentry/node");
201
+ if (context) {
202
+ Sentry.withScope((scope) => {
203
+ if (context.tags) {
204
+ for (const [key, value] of Object.entries(context.tags)) {
205
+ scope.setTag(key, String(value));
206
+ }
207
+ }
208
+ if (context.extra) scope.setExtras(scrubValue(context.extra));
209
+ Sentry.captureException(error);
210
+ });
211
+ return;
212
+ }
213
+ Sentry.captureException(error);
214
+ } catch {
215
+ /* never break the CLI */
216
+ }
217
+ }
218
+
219
+ module.exports = {
220
+ initSentry,
221
+ captureException,
222
+ resolveDsn,
223
+ resolveEnvironment,
224
+ resolveShell,
225
+ scrubEvent,
226
+ scrubString,
227
+ // test helpers
228
+ _resetForTests() {
229
+ initialized = false;
230
+ },
231
+ };