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.
Files changed (131) hide show
  1. package/CHANGELOG.md +46 -19
  2. package/LICENSE +22 -22
  3. package/README.md +123 -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 +210 -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
@@ -1,243 +1,243 @@
1
- "use strict";
2
-
3
- // Conservative, metadata-only Git attribution. A commit is attributed only
4
- // when exactly one local AI session overlaps its author timestamp.
5
-
6
- const { execFile } = require("node:child_process");
7
- const fs = require("node:fs");
8
- const fsp = require("node:fs/promises");
9
- const os = require("node:os");
10
- const path = require("node:path");
11
- const crypto = require("node:crypto");
12
-
13
- function resolveAutoOutcomesPath(home = os.homedir()) {
14
- return path.join(home, ".tokenusage", "tracker", "auto-outcomes.jsonl");
15
- }
16
-
17
- function readJsonl(filePath) {
18
- try { return fs.readFileSync(filePath, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)); }
19
- catch { return []; }
20
- }
21
-
22
- // Async on purpose: this runs inside the local API request path, where a
23
- // synchronous spawn would freeze every other endpoint for the duration of
24
- // each git invocation.
25
- function runGit(cwd, args) {
26
- return new Promise((resolve) => {
27
- execFile(
28
- "git",
29
- args,
30
- { cwd, encoding: "utf8", timeout: 15_000, maxBuffer: 32 * 1024 * 1024 },
31
- (error, stdout) => resolve(error ? "" : String(stdout).trim()),
32
- );
33
- });
34
- }
35
-
36
- async function repoRoot(cwd) {
37
- if (!cwd || !fs.existsSync(cwd)) return null;
38
- return (await runGit(cwd, ["rev-parse", "--show-toplevel"])) || null;
39
- }
40
-
41
- async function commitsForWindow(root, from, to) {
42
- const format = "%H%x1f%aI%x1f%P%x1f%s%x1e";
43
- const raw = await runGit(root, ["log", "--all", `--since=${from}`, `--until=${to}`, `--format=${format}`]);
44
- if (!raw) return [];
45
- return raw.split("\x1e").map((item) => item.trim()).filter(Boolean).map((item) => {
46
- const [sha, timestamp, parents, subject] = item.split("\x1f");
47
- return {
48
- sha,
49
- timestamp,
50
- parent_count: String(parents || "").split(/\s+/).filter(Boolean).length,
51
- reverted: /^revert\b/i.test(subject || ""),
52
- };
53
- }).filter((row) => row.sha && Number.isFinite(Date.parse(row.timestamp)));
54
- }
55
-
56
- async function writeAtomic(filePath, content) {
57
- await fsp.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
58
- const temp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
59
- await fsp.writeFile(temp, content, { encoding: "utf8", mode: 0o600 });
60
- await fsp.rename(temp, filePath);
61
- }
62
-
63
- // Attribution walks into the working directory of every recent session and
64
- // runs git there. On macOS each distinct protected location that gets touched
65
- // (~/Documents, ~/Downloads, another app's container) raises its own TCC
66
- // consent prompt, so probing must happen only when there is new work to
67
- // attribute — never on every dashboard poll.
68
- const GIT_PROBE_TTL_MS = 30 * 60 * 1000;
69
-
70
- // A data-only fingerprint of the sessions we would attribute. Computing it
71
- // touches no filesystem and spawns no git, so an unchanged fingerprint lets us
72
- // serve the cached sidecar without entering a single project directory.
73
- function sessionsFingerprint(sessions, maxAgeDays) {
74
- const hash = crypto.createHash("sha256");
75
- hash.update(`v1\0${maxAgeDays}\n`);
76
- const rows = sessions.map(
77
- (session) => `${session.project_ref}\0${session.session_hash}\0${session.started_at}\0${session.ended_at}`,
78
- );
79
- for (const row of rows.sort()) hash.update(`${row}\n`);
80
- return hash.digest("hex");
81
- }
82
-
83
- // The sessions attribution is allowed to look at. Filtering here — before the
84
- // fingerprint and before any filesystem access — keeps the cache signature and
85
- // the probe set in agreement, so a skipped directory is never entered and never
86
- // invalidates the cache either.
87
- function attributableSessions(sessions, cutoff, home) {
88
- const kept = [];
89
- for (const session of sessions || []) {
90
- if (!session?.project_ref || !session.started_at || !session.ended_at) continue;
91
- if (Date.parse(session.ended_at) < cutoff) continue;
92
- if (isTccProtectedPath(session.project_ref, home)) continue;
93
- kept.push(session);
94
- }
95
- return kept;
96
- }
97
-
98
- function gitAttributionDisabled() {
99
- return ["1", "true"].includes(String(process.env.TOKENUSAGE_DISABLE_GIT_ATTRIBUTION || "").toLowerCase());
100
- }
101
-
102
- // macOS gates these locations behind TCC: the first access to each one raises
103
- // its own "Token Usage would like to access data in …" consent dialog, and
104
- // ad-hoc signing resets that consent on every update, so the dialogs come back
105
- // each time. Attribution is an opt-in beta that walks into whatever directory a
106
- // session happened to run in — a token tracker has no business making the OS
107
- // ask for ~/Documents. Skip these by default; anyone who keeps repos there can
108
- // opt back in and answer the prompts knowingly.
109
- const TCC_PROTECTED_HOME_DIRS = ["Documents", "Downloads", "Desktop", "Movies", "Music", "Pictures", "Library"];
110
-
111
- function protectedProbeAllowed() {
112
- return ["1", "true"].includes(String(process.env.TOKENUSAGE_GIT_ATTRIBUTION_PROTECTED_DIRS || "").toLowerCase());
113
- }
114
-
115
- function isTccProtectedPath(target, home) {
116
- if (process.platform !== "darwin" || protectedProbeAllowed()) return false;
117
- const resolved = path.resolve(String(target || ""));
118
- if (resolved === "/Volumes" || resolved.startsWith("/Volumes/")) return true;
119
- const base = path.resolve(String(home || ""));
120
- if (resolved !== base && !resolved.startsWith(`${base}${path.sep}`)) return false;
121
- const [first] = path.relative(base, resolved).split(path.sep);
122
- return TCC_PROTECTED_HOME_DIRS.includes(first);
123
- }
124
-
125
- async function buildGitOutcomesInternal(sessions, { home = os.homedir(), force = false, maxAgeDays = 90 } = {}) {
126
- const outputPath = resolveAutoOutcomesPath(home);
127
- const metaPath = `${outputPath}.meta.json`;
128
- const cutoff = Date.now() - Math.max(1, Number(maxAgeDays) || 90) * 86400_000;
129
-
130
- // Escape hatch for anyone who would rather keep Token Usage out of their
131
- // project directories entirely. Serves whatever was already attributed.
132
- if (gitAttributionDisabled()) return readJsonl(outputPath);
133
-
134
- // Cheap gate, before any filesystem access: same sessions as last time and
135
- // the last probe is recent, so there is nothing new to attribute.
136
- const candidates = attributableSessions(sessions, cutoff, home);
137
- const sessionSignature = sessionsFingerprint(candidates, maxAgeDays);
138
- if (!force) {
139
- try {
140
- const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
141
- const age = Date.now() - Date.parse(meta.generated_at || "");
142
- if (
143
- meta.version === 2
144
- && meta.session_signature === sessionSignature
145
- && Number.isFinite(age)
146
- && age >= 0
147
- && age < GIT_PROBE_TTL_MS
148
- ) {
149
- return readJsonl(outputPath);
150
- }
151
- } catch { /* first run, or a v1 sidecar written before this gate existed */ }
152
- }
153
-
154
- const groups = new Map();
155
- const rootCache = new Map();
156
- for (const session of candidates) {
157
- let root = rootCache.get(session.project_ref);
158
- if (root === undefined) {
159
- root = await repoRoot(session.project_ref);
160
- rootCache.set(session.project_ref, root);
161
- }
162
- if (!root) continue;
163
- if (!groups.has(root)) groups.set(root, []);
164
- groups.get(root).push(session);
165
- }
166
-
167
- const signatureHash = crypto.createHash("sha256");
168
- for (const [root, repoSessions] of [...groups.entries()].sort(([a], [b]) => a.localeCompare(b))) {
169
- signatureHash.update(`${root}\0${await runGit(root, ["for-each-ref", "--format=%(objectname)"])}\n`);
170
- for (const session of repoSessions) signatureHash.update(`${session.session_hash}\0${session.ended_at}\n`);
171
- }
172
- const signature = signatureHash.digest("hex");
173
- if (!force) {
174
- try {
175
- const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
176
- // Git state is unchanged too — refresh the timestamp so the cheap gate
177
- // above can short-circuit the next poll instead of re-probing.
178
- if (meta.signature === signature) {
179
- await writeAtomic(metaPath, `${JSON.stringify({ ...meta, version: 2, session_signature: sessionSignature, generated_at: new Date().toISOString() })}\n`);
180
- return readJsonl(outputPath);
181
- }
182
- } catch { /* first run */ }
183
- }
184
-
185
- const outcomes = [];
186
- for (const [root, repoSessions] of groups) {
187
- const start = repoSessions.reduce((min, row) => !min || row.started_at < min ? row.started_at : min, "");
188
- const endMs = Math.max(...repoSessions.map((row) => Date.parse(row.ended_at))) + 60 * 60 * 1000;
189
- const commits = await commitsForWindow(root, start, new Date(endMs).toISOString());
190
- for (const commit of commits) {
191
- const commitMs = Date.parse(commit.timestamp);
192
- const candidates = repoSessions.filter((session) => {
193
- const sessionStart = Date.parse(session.started_at) - 10 * 60 * 1000;
194
- const sessionEnd = Date.parse(session.ended_at) + 60 * 60 * 1000;
195
- return commitMs >= sessionStart && commitMs <= sessionEnd;
196
- });
197
- if (candidates.length !== 1) continue;
198
- const session = candidates[0];
199
- outcomes.push({
200
- timestamp: commit.timestamp,
201
- model: session.model || "unknown",
202
- tool: session.source || "unknown",
203
- accepted: !commit.reverted,
204
- task_type: "git_commit",
205
- status: commit.reverted ? "reverted" : "committed",
206
- session_hash: session.session_hash,
207
- commit_hash: commit.sha.slice(0, 12),
208
- parent_count: commit.parent_count,
209
- confidence: "heuristic",
210
- methodology: "single-overlapping-session",
211
- });
212
- }
213
- }
214
- outcomes.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
215
- await writeAtomic(outputPath, outcomes.map((row) => JSON.stringify(row)).join("\n") + (outcomes.length ? "\n" : ""));
216
- await writeAtomic(metaPath, `${JSON.stringify({
217
- version: 2,
218
- signature,
219
- session_signature: sessionSignature,
220
- generated_at: new Date().toISOString(),
221
- max_age_days: maxAgeDays,
222
- })}\n`);
223
- return outcomes;
224
- }
225
-
226
- // Overlapping outcomes/session-insights requests must not spawn duplicate git
227
- // scans nor race the atomic sidecar write; share one build per home.
228
- const gitOutcomesBuilds = new Map();
229
-
230
- function buildGitOutcomes(sessions, options = {}) {
231
- const home = path.resolve(String(options.home || os.homedir()));
232
- const existing = gitOutcomesBuilds.get(home);
233
- if (existing) return existing;
234
- const promise = buildGitOutcomesInternal(sessions, { ...options, home });
235
- gitOutcomesBuilds.set(home, promise);
236
- const clear = () => {
237
- if (gitOutcomesBuilds.get(home) === promise) gitOutcomesBuilds.delete(home);
238
- };
239
- promise.then(clear, clear);
240
- return promise;
241
- }
242
-
243
- module.exports = { resolveAutoOutcomesPath, buildGitOutcomes, repoRoot, commitsForWindow };
1
+ "use strict";
2
+
3
+ // Conservative, metadata-only Git attribution. A commit is attributed only
4
+ // when exactly one local AI session overlaps its author timestamp.
5
+
6
+ const { execFile } = require("node:child_process");
7
+ const fs = require("node:fs");
8
+ const fsp = require("node:fs/promises");
9
+ const os = require("node:os");
10
+ const path = require("node:path");
11
+ const crypto = require("node:crypto");
12
+
13
+ function resolveAutoOutcomesPath(home = os.homedir()) {
14
+ return path.join(home, ".tokenusage", "tracker", "auto-outcomes.jsonl");
15
+ }
16
+
17
+ function readJsonl(filePath) {
18
+ try { return fs.readFileSync(filePath, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)); }
19
+ catch { return []; }
20
+ }
21
+
22
+ // Async on purpose: this runs inside the local API request path, where a
23
+ // synchronous spawn would freeze every other endpoint for the duration of
24
+ // each git invocation.
25
+ function runGit(cwd, args) {
26
+ return new Promise((resolve) => {
27
+ execFile(
28
+ "git",
29
+ args,
30
+ { cwd, encoding: "utf8", timeout: 15_000, maxBuffer: 32 * 1024 * 1024 },
31
+ (error, stdout) => resolve(error ? "" : String(stdout).trim()),
32
+ );
33
+ });
34
+ }
35
+
36
+ async function repoRoot(cwd) {
37
+ if (!cwd || !fs.existsSync(cwd)) return null;
38
+ return (await runGit(cwd, ["rev-parse", "--show-toplevel"])) || null;
39
+ }
40
+
41
+ async function commitsForWindow(root, from, to) {
42
+ const format = "%H%x1f%aI%x1f%P%x1f%s%x1e";
43
+ const raw = await runGit(root, ["log", "--all", `--since=${from}`, `--until=${to}`, `--format=${format}`]);
44
+ if (!raw) return [];
45
+ return raw.split("\x1e").map((item) => item.trim()).filter(Boolean).map((item) => {
46
+ const [sha, timestamp, parents, subject] = item.split("\x1f");
47
+ return {
48
+ sha,
49
+ timestamp,
50
+ parent_count: String(parents || "").split(/\s+/).filter(Boolean).length,
51
+ reverted: /^revert\b/i.test(subject || ""),
52
+ };
53
+ }).filter((row) => row.sha && Number.isFinite(Date.parse(row.timestamp)));
54
+ }
55
+
56
+ async function writeAtomic(filePath, content) {
57
+ await fsp.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
58
+ const temp = `${filePath}.${process.pid}.${Date.now()}.tmp`;
59
+ await fsp.writeFile(temp, content, { encoding: "utf8", mode: 0o600 });
60
+ await fsp.rename(temp, filePath);
61
+ }
62
+
63
+ // Attribution walks into the working directory of every recent session and
64
+ // runs git there. On macOS each distinct protected location that gets touched
65
+ // (~/Documents, ~/Downloads, another app's container) raises its own TCC
66
+ // consent prompt, so probing must happen only when there is new work to
67
+ // attribute — never on every dashboard poll.
68
+ const GIT_PROBE_TTL_MS = 30 * 60 * 1000;
69
+
70
+ // A data-only fingerprint of the sessions we would attribute. Computing it
71
+ // touches no filesystem and spawns no git, so an unchanged fingerprint lets us
72
+ // serve the cached sidecar without entering a single project directory.
73
+ function sessionsFingerprint(sessions, maxAgeDays) {
74
+ const hash = crypto.createHash("sha256");
75
+ hash.update(`v1\0${maxAgeDays}\n`);
76
+ const rows = sessions.map(
77
+ (session) => `${session.project_ref}\0${session.session_hash}\0${session.started_at}\0${session.ended_at}`,
78
+ );
79
+ for (const row of rows.sort()) hash.update(`${row}\n`);
80
+ return hash.digest("hex");
81
+ }
82
+
83
+ // The sessions attribution is allowed to look at. Filtering here — before the
84
+ // fingerprint and before any filesystem access — keeps the cache signature and
85
+ // the probe set in agreement, so a skipped directory is never entered and never
86
+ // invalidates the cache either.
87
+ function attributableSessions(sessions, cutoff, home) {
88
+ const kept = [];
89
+ for (const session of sessions || []) {
90
+ if (!session?.project_ref || !session.started_at || !session.ended_at) continue;
91
+ if (Date.parse(session.ended_at) < cutoff) continue;
92
+ if (isTccProtectedPath(session.project_ref, home)) continue;
93
+ kept.push(session);
94
+ }
95
+ return kept;
96
+ }
97
+
98
+ function gitAttributionDisabled() {
99
+ return ["1", "true"].includes(String(process.env.TOKENUSAGE_DISABLE_GIT_ATTRIBUTION || "").toLowerCase());
100
+ }
101
+
102
+ // macOS gates these locations behind TCC: the first access to each one raises
103
+ // its own "Token Usage would like to access data in …" consent dialog, and
104
+ // ad-hoc signing resets that consent on every update, so the dialogs come back
105
+ // each time. Attribution is an opt-in beta that walks into whatever directory a
106
+ // session happened to run in — a token tracker has no business making the OS
107
+ // ask for ~/Documents. Skip these by default; anyone who keeps repos there can
108
+ // opt back in and answer the prompts knowingly.
109
+ const TCC_PROTECTED_HOME_DIRS = ["Documents", "Downloads", "Desktop", "Movies", "Music", "Pictures", "Library"];
110
+
111
+ function protectedProbeAllowed() {
112
+ return ["1", "true"].includes(String(process.env.TOKENUSAGE_GIT_ATTRIBUTION_PROTECTED_DIRS || "").toLowerCase());
113
+ }
114
+
115
+ function isTccProtectedPath(target, home) {
116
+ if (process.platform !== "darwin" || protectedProbeAllowed()) return false;
117
+ const resolved = path.resolve(String(target || ""));
118
+ if (resolved === "/Volumes" || resolved.startsWith("/Volumes/")) return true;
119
+ const base = path.resolve(String(home || ""));
120
+ if (resolved !== base && !resolved.startsWith(`${base}${path.sep}`)) return false;
121
+ const [first] = path.relative(base, resolved).split(path.sep);
122
+ return TCC_PROTECTED_HOME_DIRS.includes(first);
123
+ }
124
+
125
+ async function buildGitOutcomesInternal(sessions, { home = os.homedir(), force = false, maxAgeDays = 90 } = {}) {
126
+ const outputPath = resolveAutoOutcomesPath(home);
127
+ const metaPath = `${outputPath}.meta.json`;
128
+ const cutoff = Date.now() - Math.max(1, Number(maxAgeDays) || 90) * 86400_000;
129
+
130
+ // Escape hatch for anyone who would rather keep Token Usage out of their
131
+ // project directories entirely. Serves whatever was already attributed.
132
+ if (gitAttributionDisabled()) return readJsonl(outputPath);
133
+
134
+ // Cheap gate, before any filesystem access: same sessions as last time and
135
+ // the last probe is recent, so there is nothing new to attribute.
136
+ const candidates = attributableSessions(sessions, cutoff, home);
137
+ const sessionSignature = sessionsFingerprint(candidates, maxAgeDays);
138
+ if (!force) {
139
+ try {
140
+ const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
141
+ const age = Date.now() - Date.parse(meta.generated_at || "");
142
+ if (
143
+ meta.version === 2
144
+ && meta.session_signature === sessionSignature
145
+ && Number.isFinite(age)
146
+ && age >= 0
147
+ && age < GIT_PROBE_TTL_MS
148
+ ) {
149
+ return readJsonl(outputPath);
150
+ }
151
+ } catch { /* first run, or a v1 sidecar written before this gate existed */ }
152
+ }
153
+
154
+ const groups = new Map();
155
+ const rootCache = new Map();
156
+ for (const session of candidates) {
157
+ let root = rootCache.get(session.project_ref);
158
+ if (root === undefined) {
159
+ root = await repoRoot(session.project_ref);
160
+ rootCache.set(session.project_ref, root);
161
+ }
162
+ if (!root) continue;
163
+ if (!groups.has(root)) groups.set(root, []);
164
+ groups.get(root).push(session);
165
+ }
166
+
167
+ const signatureHash = crypto.createHash("sha256");
168
+ for (const [root, repoSessions] of [...groups.entries()].sort(([a], [b]) => a.localeCompare(b))) {
169
+ signatureHash.update(`${root}\0${await runGit(root, ["for-each-ref", "--format=%(objectname)"])}\n`);
170
+ for (const session of repoSessions) signatureHash.update(`${session.session_hash}\0${session.ended_at}\n`);
171
+ }
172
+ const signature = signatureHash.digest("hex");
173
+ if (!force) {
174
+ try {
175
+ const meta = JSON.parse(fs.readFileSync(metaPath, "utf8"));
176
+ // Git state is unchanged too — refresh the timestamp so the cheap gate
177
+ // above can short-circuit the next poll instead of re-probing.
178
+ if (meta.signature === signature) {
179
+ await writeAtomic(metaPath, `${JSON.stringify({ ...meta, version: 2, session_signature: sessionSignature, generated_at: new Date().toISOString() })}\n`);
180
+ return readJsonl(outputPath);
181
+ }
182
+ } catch { /* first run */ }
183
+ }
184
+
185
+ const outcomes = [];
186
+ for (const [root, repoSessions] of groups) {
187
+ const start = repoSessions.reduce((min, row) => !min || row.started_at < min ? row.started_at : min, "");
188
+ const endMs = Math.max(...repoSessions.map((row) => Date.parse(row.ended_at))) + 60 * 60 * 1000;
189
+ const commits = await commitsForWindow(root, start, new Date(endMs).toISOString());
190
+ for (const commit of commits) {
191
+ const commitMs = Date.parse(commit.timestamp);
192
+ const candidates = repoSessions.filter((session) => {
193
+ const sessionStart = Date.parse(session.started_at) - 10 * 60 * 1000;
194
+ const sessionEnd = Date.parse(session.ended_at) + 60 * 60 * 1000;
195
+ return commitMs >= sessionStart && commitMs <= sessionEnd;
196
+ });
197
+ if (candidates.length !== 1) continue;
198
+ const session = candidates[0];
199
+ outcomes.push({
200
+ timestamp: commit.timestamp,
201
+ model: session.model || "unknown",
202
+ tool: session.source || "unknown",
203
+ accepted: !commit.reverted,
204
+ task_type: "git_commit",
205
+ status: commit.reverted ? "reverted" : "committed",
206
+ session_hash: session.session_hash,
207
+ commit_hash: commit.sha.slice(0, 12),
208
+ parent_count: commit.parent_count,
209
+ confidence: "heuristic",
210
+ methodology: "single-overlapping-session",
211
+ });
212
+ }
213
+ }
214
+ outcomes.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
215
+ await writeAtomic(outputPath, outcomes.map((row) => JSON.stringify(row)).join("\n") + (outcomes.length ? "\n" : ""));
216
+ await writeAtomic(metaPath, `${JSON.stringify({
217
+ version: 2,
218
+ signature,
219
+ session_signature: sessionSignature,
220
+ generated_at: new Date().toISOString(),
221
+ max_age_days: maxAgeDays,
222
+ })}\n`);
223
+ return outcomes;
224
+ }
225
+
226
+ // Overlapping outcomes/session-insights requests must not spawn duplicate git
227
+ // scans nor race the atomic sidecar write; share one build per home.
228
+ const gitOutcomesBuilds = new Map();
229
+
230
+ function buildGitOutcomes(sessions, options = {}) {
231
+ const home = path.resolve(String(options.home || os.homedir()));
232
+ const existing = gitOutcomesBuilds.get(home);
233
+ if (existing) return existing;
234
+ const promise = buildGitOutcomesInternal(sessions, { ...options, home });
235
+ gitOutcomesBuilds.set(home, promise);
236
+ const clear = () => {
237
+ if (gitOutcomesBuilds.get(home) === promise) gitOutcomesBuilds.delete(home);
238
+ };
239
+ promise.then(clear, clear);
240
+ return promise;
241
+ }
242
+
243
+ module.exports = { resolveAutoOutcomesPath, buildGitOutcomes, repoRoot, commitsForWindow };