tokenusage-cli 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -19
- package/LICENSE +22 -22
- package/README.md +123 -94
- package/THIRD_PARTY_NOTICES.md +140 -140
- package/bin/tokenusage.js +69 -69
- package/package.json +59 -59
- package/src/cli.js +146 -134
- package/src/commands/device-login.js +205 -205
- package/src/commands/diagnostics.js +38 -38
- package/src/commands/doctor.js +110 -110
- package/src/commands/init.js +2484 -2436
- package/src/commands/pause.js +77 -0
- package/src/commands/sessions.js +29 -29
- package/src/commands/status.js +1758 -1747
- package/src/commands/sync.js +8048 -7934
- package/src/commands/uninstall.js +322 -312
- package/src/lib/account-usage-dedup.js +267 -267
- package/src/lib/antigravity-paths.js +48 -48
- package/src/lib/ark-agent-plan-limits.js +438 -438
- package/src/lib/ark-coding-plan-limits.js +537 -537
- package/src/lib/async-queue-reader.js +138 -138
- package/src/lib/atomic-json.js +27 -27
- package/src/lib/bearer-token.js +11 -11
- package/src/lib/categorizer-utils.js +232 -232
- package/src/lib/claude-categorizer.js +1343 -1343
- package/src/lib/claude-config.js +279 -279
- package/src/lib/claude-user-identity.js +56 -56
- package/src/lib/cli-ui.js +179 -179
- package/src/lib/cloud-account.js +541 -541
- package/src/lib/cloud-device-token.js +141 -141
- package/src/lib/cloud-sync-prefs.js +14 -14
- package/src/lib/cloud-usage-reset.js +115 -115
- package/src/lib/codex-config.js +595 -595
- package/src/lib/codex-context-breakdown.js +806 -806
- package/src/lib/codex-model-attribution.js +128 -128
- package/src/lib/codex-rollout-parser.js +1471 -1471
- package/src/lib/codex-service-tier.js +49 -49
- package/src/lib/codex-token-refresh.js +140 -140
- package/src/lib/codex-token-usage.js +195 -195
- package/src/lib/codex-usage-record.js +195 -195
- package/src/lib/collection-pause.js +77 -0
- package/src/lib/command-runner.js +313 -313
- package/src/lib/commandcode-limits.js +317 -317
- package/src/lib/context-health.js +124 -124
- package/src/lib/cursor-config.js +605 -605
- package/src/lib/cursor-store.js +1918 -1918
- package/src/lib/debug-flags.js +7 -7
- package/src/lib/devin-limits.js +249 -249
- package/src/lib/devin-usage.js +212 -212
- package/src/lib/diagnostics.js +383 -383
- package/src/lib/doctor.js +385 -385
- package/src/lib/dumate.js +95 -95
- package/src/lib/fs.js +455 -455
- package/src/lib/function-url.js +14 -14
- package/src/lib/gemini-config.js +291 -291
- package/src/lib/git-outcomes.js +243 -243
- package/src/lib/grok-context-breakdown.js +890 -890
- package/src/lib/grok-hook.js +360 -360
- package/src/lib/grok-limits.js +865 -865
- package/src/lib/grok-usage.js +98 -98
- package/src/lib/heatmap-compact.js +114 -114
- package/src/lib/ingest-sanitize.js +41 -41
- package/src/lib/init-flow.js +42 -42
- package/src/lib/install-resolver.js +170 -170
- package/src/lib/jsonl-lines.js +134 -134
- package/src/lib/kimi-model-repair.js +173 -173
- package/src/lib/kimi-profile.js +61 -61
- package/src/lib/kimi-session.js +216 -216
- package/src/lib/local-api.js +3588 -3588
- package/src/lib/machine-id.js +238 -238
- package/src/lib/multi-install-parser.js +113 -113
- package/src/lib/notify-command.js +38 -38
- package/src/lib/omp-hook.js +589 -589
- package/src/lib/openclaw-cli.js +104 -104
- package/src/lib/openclaw-hook.js +375 -375
- package/src/lib/openclaw-session-plugin.js +560 -560
- package/src/lib/opencode-config.js +121 -121
- package/src/lib/opencode-go-limits.js +807 -807
- package/src/lib/outcomes-engine.js +253 -253
- package/src/lib/passive-mode.js +219 -219
- package/src/lib/pet-packages.js +722 -722
- package/src/lib/pricing/curated-overrides.json +822 -822
- package/src/lib/pricing/index.js +379 -379
- package/src/lib/pricing/litellm-fetcher.js +172 -172
- package/src/lib/pricing/matcher.js +456 -456
- package/src/lib/pricing/seed-snapshot.json +1 -1
- package/src/lib/progress.js +76 -76
- package/src/lib/project-usage-purge.js +117 -117
- package/src/lib/prompt.js +24 -24
- package/src/lib/provider-status.js +104 -104
- package/src/lib/proxy-env.js +489 -489
- package/src/lib/proxy-settings.js +163 -163
- package/src/lib/qoder-cn-cli-limits.js +119 -119
- package/src/lib/qoder-limits.js +880 -880
- package/src/lib/quiet-warnings.js +26 -26
- package/src/lib/rollout.js +26005 -25966
- package/src/lib/runtime-config.js +9 -4
- package/src/lib/scan-roots.js +277 -277
- package/src/lib/scheduled-task.js +145 -0
- package/src/lib/sentry.js +231 -231
- package/src/lib/session-analytics.js +2972 -2972
- package/src/lib/session-performance.js +237 -237
- package/src/lib/session-upload.js +379 -379
- package/src/lib/skill-usage.js +267 -267
- package/src/lib/skills-manager.js +1591 -1591
- package/src/lib/source-metadata.js +48 -48
- package/src/lib/sqlite-reader.js +215 -215
- package/src/lib/star-cta.js +73 -73
- package/src/lib/subscription-manager.js +293 -293
- package/src/lib/subscriptions.js +508 -508
- package/src/lib/sync-report.js +138 -0
- package/src/lib/telemetry.js +193 -193
- package/src/lib/tracker-paths.js +16 -16
- package/src/lib/trae-cn-config.js +548 -548
- package/src/lib/trae-db.js +127 -127
- package/src/lib/trae-snapshot.js +230 -230
- package/src/lib/trae-sqlite.js +143 -143
- package/src/lib/trae-usage.js +121 -121
- package/src/lib/upload-queue.js +134 -134
- package/src/lib/upload-throttle.js +171 -171
- package/src/lib/usage-limits.js +4552 -4552
- package/src/lib/wrapped-aggregator.js +236 -236
- package/src/lib/wsl-probe.js +281 -281
- package/src/lib/zcode-limits.js +1172 -1172
- package/src/lib/zcode-reset-credits.js +66 -66
- package/test/audit-fixes.test.js +200 -0
- package/test/cli-smoke.test.js +53 -53
- package/test/collection-controls.test.js +210 -0
- package/test/ingest-sanitize.test.js +162 -162
- package/test/quiet-warnings.test.js +53 -53
- package/test/session-upload.test.js +427 -427
|
@@ -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
|
+
};
|