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,1343 +1,1343 @@
1
- const { claudeUserIdentity, collectClaudeForkAliases } = require("./claude-user-identity");
2
- // Claude Code "Context Breakdown" categorizer.
3
- //
4
- // Reads ~/.claude/projects/**/*.jsonl and splits each assistant message's
5
- // usage into seven semantic buckets, mirroring (approximately) the Claude
6
- // Code in-CLI /context view but as a historical aggregate. Computes on
7
- // demand — no queue schema changes, no parser changes, no sync changes.
8
- //
9
- // Why these seven and not the screenshot's eight: the raw system prompt
10
- // (which contains tools schema, skills, rules, MCP descriptions) is sent
11
- // once per session as a 1h-ephemeral cache prefix and is never logged
12
- // verbatim in the jsonl. So at the token-accounting layer those four are
13
- // indistinguishable — they all collapse into `system_prefix`. UI says so.
14
- const fssync = require("node:fs");
15
- const os = require("node:os");
16
- const path = require("node:path");
17
- const crypto = require("node:crypto");
18
- const readline = require("node:readline");
19
-
20
- const {
21
- categorizeTool,
22
- inferExecCommandKind,
23
- sanitizeCommandSignature,
24
- getExecutableName,
25
- emptyTotals,
26
- addInto,
27
- roundTotals,
28
- buildExecStatsEntry,
29
- allocateByLargestRemainder,
30
- } = require("./categorizer-utils");
31
- const { claudeMessageDedupKey } = require("./rollout");
32
-
33
- const CATEGORY_KEYS = [
34
- "system_prefix",
35
- "conversation_history",
36
- "user_input",
37
- "tool_calls",
38
- "subagents",
39
- "reasoning",
40
- "assistant_response",
41
- ];
42
-
43
- const SUBAGENT_TOOL_NAMES = new Set(["Agent", "Task"]);
44
-
45
- function emptyToolBreakdown() {
46
- return {
47
- total_calls: 0,
48
- tools: [],
49
- };
50
- }
51
-
52
- function emptyCategoryMap() {
53
- const out = {};
54
- for (const key of CATEGORY_KEYS) out[key] = emptyTotals();
55
- return out;
56
- }
57
-
58
- function toNonNegativeNumber(value) {
59
- const n = Number(value || 0);
60
- return Number.isFinite(n) ? Math.max(0, n) : 0;
61
- }
62
-
63
- function readClaudeUsageTotals(usage) {
64
- if (!usage || typeof usage !== "object") return null;
65
- const input_tokens = toNonNegativeNumber(usage.input_tokens);
66
- const cached_input_tokens = toNonNegativeNumber(usage.cache_read_input_tokens);
67
- const cache_creation_input_tokens = toNonNegativeNumber(usage.cache_creation_input_tokens);
68
- const output_tokens = toNonNegativeNumber(usage.output_tokens);
69
- const reasoning_output_tokens = toNonNegativeNumber(usage.reasoning_output_tokens);
70
- const total_tokens =
71
- input_tokens + cached_input_tokens + cache_creation_input_tokens + output_tokens;
72
-
73
- return {
74
- input_tokens,
75
- cached_input_tokens,
76
- cache_creation_input_tokens,
77
- output_tokens,
78
- reasoning_output_tokens,
79
- total_tokens,
80
- };
81
- }
82
-
83
- function hasNonZeroClaudeUsage(usage) {
84
- const totals = readClaudeUsageTotals(usage);
85
- if (!totals) return false;
86
- return (
87
- totals.input_tokens +
88
- totals.cached_input_tokens +
89
- totals.cache_creation_input_tokens +
90
- totals.output_tokens +
91
- totals.reasoning_output_tokens >
92
- 0
93
- );
94
- }
95
-
96
- function extractExecCommands(content) {
97
- const commands = [];
98
- for (const block of Array.isArray(content) ? content : []) {
99
- if (!block || typeof block !== "object") continue;
100
- if (block.type !== "tool_use") continue;
101
- if (block.name !== "Bash" && block.name !== "exec_command") continue;
102
- const input = block.input || {};
103
- const command =
104
- typeof input.command === "string" ? input.command
105
- : typeof input.cmd === "string" ? input.cmd
106
- : "";
107
- if (command.trim()) commands.push(command.trim());
108
- }
109
- return commands;
110
- }
111
-
112
- function ensureExecRow(map, key) {
113
- const safeKey = key || "unknown";
114
- if (!map.has(safeKey)) map.set(safeKey, { name: safeKey, ...buildExecStatsEntry() });
115
- return map.get(safeKey);
116
- }
117
-
118
- function recordExecCommandUsage(execLedger, commands, totals) {
119
- if (!execLedger || !Array.isArray(commands) || commands.length === 0) return;
120
- const perCommandRows = new Map();
121
- for (const command of commands) {
122
- if (!perCommandRows.has(command)) perCommandRows.set(command, { calls: 0 });
123
- perCommandRows.get(command).calls += 1;
124
- }
125
- const totalsByCommand = allocateTotalsAcrossRows(totals, perCommandRows);
126
-
127
- for (const [command, row] of perCommandRows.entries()) {
128
- const commandTotals = totalsByCommand.get(command) || {};
129
- const kind = inferExecCommandKind(command);
130
- const executable = getExecutableName(command);
131
- const signature = sanitizeCommandSignature(command);
132
- const exitKey = "unknown:unknown";
133
-
134
- const targets = [
135
- [execLedger.by_type, kind],
136
- [execLedger.by_executable, executable],
137
- [execLedger.by_command, signature],
138
- [execLedger.by_exit, exitKey],
139
- ];
140
- for (const [map, key] of targets) {
141
- const target = ensureExecRow(map, key);
142
- target.calls += Math.max(1, Number(row.calls || 0));
143
- addInto(target.totals, commandTotals);
144
- }
145
- execLedger.total_calls += Math.max(1, Number(row.calls || 0));
146
- }
147
- }
148
-
149
- function allocateTotalsAcrossRows(totals, rows) {
150
- const entries = Array.from(rows?.entries?.() || []);
151
- if (entries.length === 0) return new Map();
152
- const weights = {};
153
- const order = entries.map(([name]) => name).sort();
154
- for (const name of order) {
155
- const row = rows.get(name) || {};
156
- weights[name] = Math.max(0, Number(row.output_tokens || row.calls || 0));
157
- }
158
- if (order.every((name) => !weights[name])) {
159
- for (const name of order) weights[name] = 1;
160
- }
161
- const out = new Map();
162
- for (const name of order) out.set(name, emptyTotals());
163
- for (const key of [
164
- "input_tokens",
165
- "cached_input_tokens",
166
- "cache_creation_input_tokens",
167
- "output_tokens",
168
- "reasoning_output_tokens",
169
- "total_tokens",
170
- ]) {
171
- const alloc = allocateByLargestRemainder(Math.max(0, Number(totals?.[key] || 0)), weights, order);
172
- for (const name of order) {
173
- out.get(name)[key] = alloc[name] || 0;
174
- }
175
- }
176
- return out;
177
- }
178
-
179
- function extractSkillNames(content) {
180
- const names = [];
181
- for (const block of Array.isArray(content) ? content : []) {
182
- if (!block || typeof block !== "object") continue;
183
- if (block.type !== "tool_use" || block.name !== "Skill") continue;
184
- const skill = typeof block.input?.skill === "string" ? block.input.skill.trim() : "";
185
- if (skill) names.push(skill);
186
- }
187
- return names;
188
- }
189
-
190
- function recordSkillUsage(skillLedger, skillNames, totals) {
191
- if (!skillLedger || !Array.isArray(skillNames) || skillNames.length === 0) return;
192
- const perMessageRows = new Map();
193
- for (const name of skillNames) {
194
- if (!perMessageRows.has(name)) perMessageRows.set(name, { calls: 0 });
195
- perMessageRows.get(name).calls += 1;
196
- }
197
- const totalsByName = allocateTotalsAcrossRows(totals, perMessageRows);
198
- for (const [name, row] of perMessageRows.entries()) {
199
- if (!skillLedger.by_name.has(name)) {
200
- skillLedger.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
201
- }
202
- const target = skillLedger.by_name.get(name);
203
- target.calls += row.calls || 0;
204
- addInto(target.totals, totalsByName.get(name) || {});
205
- }
206
- skillLedger.total_calls += skillNames.length;
207
- }
208
-
209
- function computeOutputTokenBreakdown(usage, content) {
210
- const total = Math.max(0, Number(usage.output_tokens || 0));
211
- const reasoningExplicit = Math.max(0, Number(usage.reasoning_output_tokens || 0));
212
- const blocks = Array.isArray(content) ? content : [];
213
-
214
- if (total === 0) {
215
- return {
216
- bucket_tokens: { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: 0 },
217
- tool_calls: { total_calls: 0, by_name: new Map() },
218
- subagents: { total_calls: 0, by_name: new Map() },
219
- };
220
- }
221
-
222
- const bucketChars = { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: 0 };
223
- const toolCallChars = new Map();
224
- const subagentChars = new Map();
225
- const toolCallCounts = new Map();
226
- const subagentCounts = new Map();
227
-
228
- let totalChars = 0;
229
- for (const block of blocks) {
230
- if (!block || typeof block !== "object") continue;
231
- const type = block.type;
232
- let chars = 0;
233
-
234
- if (type === "thinking") {
235
- chars = String(block.thinking || block.text || "").length || 1;
236
- bucketChars.reasoning += chars;
237
- } else if (type === "text") {
238
- chars = String(block.text || "").length || 1;
239
- bucketChars.assistant_response += chars;
240
- } else if (type === "tool_use") {
241
- const inputJson = block.input ? JSON.stringify(block.input) : "";
242
- chars = (block.name || "").length + inputJson.length + 1;
243
- if (SUBAGENT_TOOL_NAMES.has(block.name)) {
244
- bucketChars.subagents += chars;
245
- subagentChars.set(block.name, (subagentChars.get(block.name) || 0) + chars);
246
- subagentCounts.set(block.name, (subagentCounts.get(block.name) || 0) + 1);
247
- } else {
248
- bucketChars.tool_calls += chars;
249
- toolCallChars.set(block.name, (toolCallChars.get(block.name) || 0) + chars);
250
- toolCallCounts.set(block.name, (toolCallCounts.get(block.name) || 0) + 1);
251
- }
252
- } else {
253
- continue;
254
- }
255
-
256
- totalChars += chars;
257
- }
258
-
259
- if (totalChars === 0) {
260
- return {
261
- bucket_tokens: { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: total },
262
- tool_calls: { total_calls: 0, by_name: new Map() },
263
- subagents: { total_calls: 0, by_name: new Map() },
264
- };
265
- }
266
-
267
- const explicitReasoning = reasoningExplicit > 0 ? Math.min(reasoningExplicit, total) : 0;
268
- const nonReasoningOutput = total - explicitReasoning;
269
-
270
- const allocChars = { ...bucketChars };
271
- let allocTotalChars = totalChars;
272
- if (explicitReasoning > 0) {
273
- allocTotalChars -= allocChars.reasoning;
274
- allocChars.reasoning = 0;
275
- }
276
-
277
- const order = ["reasoning", "tool_calls", "subagents", "assistant_response"];
278
- const prorated = allocateByLargestRemainder(Math.max(0, nonReasoningOutput), allocChars, order);
279
-
280
- const bucketTokens = {
281
- reasoning: explicitReasoning + (prorated.reasoning || 0),
282
- tool_calls: prorated.tool_calls || 0,
283
- subagents: prorated.subagents || 0,
284
- assistant_response: prorated.assistant_response || 0,
285
- };
286
-
287
- if (allocTotalChars <= 0 && nonReasoningOutput > 0) {
288
- bucketTokens.reasoning = explicitReasoning;
289
- bucketTokens.tool_calls = 0;
290
- bucketTokens.subagents = 0;
291
- bucketTokens.assistant_response = nonReasoningOutput;
292
- }
293
-
294
- const toolTokensByName = new Map();
295
- if (bucketTokens.tool_calls > 0 && toolCallChars.size > 0) {
296
- const keys = [...toolCallChars.keys()].sort();
297
- const weights = {};
298
- for (const k of keys) weights[k] = toolCallChars.get(k) || 0;
299
- const alloc = allocateByLargestRemainder(bucketTokens.tool_calls, weights, keys);
300
- for (const k of keys) toolTokensByName.set(k, alloc[k] || 0);
301
- }
302
-
303
- const subagentTokensByName = new Map();
304
- if (bucketTokens.subagents > 0 && subagentChars.size > 0) {
305
- const keys = [...subagentChars.keys()].sort();
306
- const weights = {};
307
- for (const k of keys) weights[k] = subagentChars.get(k) || 0;
308
- const alloc = allocateByLargestRemainder(bucketTokens.subagents, weights, keys);
309
- for (const k of keys) subagentTokensByName.set(k, alloc[k] || 0);
310
- }
311
-
312
- return {
313
- bucket_tokens: bucketTokens,
314
- tool_calls: {
315
- total_calls: [...toolCallCounts.values()].reduce((a, b) => a + b, 0),
316
- by_name: new Map(
317
- [...toolCallCounts.entries()].map(([name, calls]) => [
318
- name,
319
- { name, calls, output_tokens: toolTokensByName.get(name) || 0 },
320
- ]),
321
- ),
322
- },
323
- subagents: {
324
- total_calls: [...subagentCounts.values()].reduce((a, b) => a + b, 0),
325
- by_name: new Map(
326
- [...subagentCounts.entries()].map(([name, calls]) => [
327
- name,
328
- { name, calls, output_tokens: subagentTokensByName.get(name) || 0 },
329
- ]),
330
- ),
331
- },
332
- };
333
- }
334
-
335
- function defaultClaudeProjectsDir() {
336
- return path.join(os.homedir(), ".claude", "projects");
337
- }
338
-
339
- function listSessionFiles(rootDir) {
340
- const out = [];
341
- const stack = [rootDir];
342
- while (stack.length > 0) {
343
- const dir = stack.pop();
344
- let entries;
345
- try {
346
- entries = fssync.readdirSync(dir, { withFileTypes: true });
347
- } catch (_e) {
348
- continue;
349
- }
350
- for (const entry of entries) {
351
- const fp = path.join(dir, entry.name);
352
- if (entry.isDirectory()) {
353
- stack.push(fp);
354
- } else if (entry.isFile() && entry.name.endsWith(".jsonl")) {
355
- out.push(fp);
356
- }
357
- }
358
- }
359
- return out;
360
- }
361
-
362
- // Distribute one assistant message's output tokens across categories by the
363
- // character-length ratio of each content block. Thinking goes to reasoning,
364
- // tool_use(Agent|Task) → subagents, tool_use(other) → tool_calls, text →
365
- // assistant_response. If reasoning_output_tokens is reported separately, use
366
- // that exact figure for reasoning instead of pro-rating.
367
- function splitOutputByContent(usage, content, breakdown) {
368
- const out = computeOutputTokenBreakdown(usage, content);
369
- for (const key of ["reasoning", "tool_calls", "subagents", "assistant_response"]) {
370
- const tok = out.bucket_tokens[key] || 0;
371
- if (tok === 0) continue;
372
- breakdown[key].output_tokens += tok;
373
- breakdown[key].total_tokens += tok;
374
- if (key === "reasoning") breakdown[key].reasoning_output_tokens += tok;
375
- }
376
- }
377
-
378
- // Per-session state lets us pick out the *first* meaningful cache_creation
379
- // chunk and call that the system_prefix. Subsequent cache_creations are
380
- // incremental — we attribute them to conversation_history.
381
- function classifyOneMessage(obj, sessionState, breakdown, toolLedger = null, skillLedger = null, execLedger = null) {
382
- const usage = obj?.message?.usage;
383
- if (!usage || typeof usage !== "object") return;
384
-
385
- const cacheCreate = Math.max(0, Number(usage.cache_creation_input_tokens || 0));
386
- const cacheRead = Math.max(0, Number(usage.cache_read_input_tokens || 0));
387
- const inputNonCached = Math.max(0, Number(usage.input_tokens || 0));
388
- const output = Math.max(0, Number(usage.output_tokens || 0));
389
-
390
- // input_tokens (pure non-cached) → user_input
391
- if (inputNonCached > 0) {
392
- breakdown.user_input.input_tokens += inputNonCached;
393
- breakdown.user_input.total_tokens += inputNonCached;
394
- }
395
-
396
- // cache_read_input_tokens → conversation_history (replaying earlier turns)
397
- if (cacheRead > 0) {
398
- breakdown.conversation_history.cached_input_tokens += cacheRead;
399
- breakdown.conversation_history.total_tokens += cacheRead;
400
- }
401
-
402
- // cache_creation_input_tokens: first big block of a session = system_prefix,
403
- // everything after = incremental conversation history.
404
- if (cacheCreate > 0) {
405
- if (!sessionState.systemPrefixSeen) {
406
- breakdown.system_prefix.cache_creation_input_tokens += cacheCreate;
407
- breakdown.system_prefix.total_tokens += cacheCreate;
408
- sessionState.systemPrefixSeen = true;
409
- } else {
410
- breakdown.conversation_history.cache_creation_input_tokens += cacheCreate;
411
- breakdown.conversation_history.total_tokens += cacheCreate;
412
- }
413
- }
414
-
415
- recordSkillUsage(
416
- skillLedger,
417
- extractSkillNames(obj?.message?.content),
418
- {
419
- input_tokens: inputNonCached,
420
- cached_input_tokens: cacheRead,
421
- cache_creation_input_tokens: cacheCreate,
422
- output_tokens: output,
423
- reasoning_output_tokens: 0,
424
- total_tokens: inputNonCached + cacheRead + cacheCreate + output,
425
- },
426
- );
427
-
428
- // Split output across reasoning / tool_calls / subagents / assistant_response.
429
- if (output > 0) {
430
- const out = computeOutputTokenBreakdown(
431
- { output_tokens: output, reasoning_output_tokens: usage.reasoning_output_tokens },
432
- obj?.message?.content,
433
- );
434
- for (const key of ["reasoning", "tool_calls", "subagents", "assistant_response"]) {
435
- const tok = out.bucket_tokens[key] || 0;
436
- if (tok === 0) continue;
437
- breakdown[key].output_tokens += tok;
438
- breakdown[key].total_tokens += tok;
439
- if (key === "reasoning") breakdown[key].reasoning_output_tokens += tok;
440
- }
441
-
442
- recordExecCommandUsage(
443
- execLedger,
444
- extractExecCommands(obj?.message?.content),
445
- {
446
- input_tokens: inputNonCached,
447
- cached_input_tokens: cacheRead,
448
- cache_creation_input_tokens: cacheCreate,
449
- output_tokens: out.bucket_tokens.tool_calls || 0,
450
- reasoning_output_tokens: 0,
451
- total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.tool_calls || 0),
452
- },
453
- );
454
-
455
- if (toolLedger) {
456
- const ledgerTotals = {
457
- input_tokens: inputNonCached,
458
- cached_input_tokens: cacheRead,
459
- cache_creation_input_tokens: cacheCreate,
460
- output_tokens: out.bucket_tokens.tool_calls || 0,
461
- reasoning_output_tokens: 0,
462
- total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.tool_calls || 0),
463
- };
464
- const toolTotalsByName = allocateTotalsAcrossRows(ledgerTotals, out.tool_calls.by_name);
465
- for (const [name, row] of out.tool_calls.by_name.entries()) {
466
- if (!toolLedger.tool_calls.by_name.has(name)) {
467
- toolLedger.tool_calls.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
468
- }
469
- const target = toolLedger.tool_calls.by_name.get(name);
470
- target.calls += row.calls || 0;
471
- addInto(target.totals, toolTotalsByName.get(name) || {});
472
- }
473
- toolLedger.tool_calls.total_calls += out.tool_calls.total_calls || 0;
474
-
475
- const subagentTotals = {
476
- input_tokens: inputNonCached,
477
- cached_input_tokens: cacheRead,
478
- cache_creation_input_tokens: cacheCreate,
479
- output_tokens: out.bucket_tokens.subagents || 0,
480
- reasoning_output_tokens: 0,
481
- total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.subagents || 0),
482
- };
483
- const subagentTotalsByName = allocateTotalsAcrossRows(subagentTotals, out.subagents.by_name);
484
- for (const [name, row] of out.subagents.by_name.entries()) {
485
- if (!toolLedger.subagents.by_name.has(name)) {
486
- toolLedger.subagents.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
487
- }
488
- const target = toolLedger.subagents.by_name.get(name);
489
- target.calls += row.calls || 0;
490
- addInto(target.totals, subagentTotalsByName.get(name) || {});
491
- }
492
- toolLedger.subagents.total_calls += out.subagents.total_calls || 0;
493
- }
494
- }
495
- }
496
-
497
- // Read one session jsonl streaming, in timestamp range, dedup by msgId+reqId.
498
- async function categorizeSessionFile(filePath, { fromIso, toIso, seenHashes }, breakdown, toolLedger = null, skillLedger = null, execLedger = null) {
499
- let stream;
500
- try {
501
- stream = fssync.createReadStream(filePath, { encoding: "utf8" });
502
- } catch (_e) {
503
- return 0;
504
- }
505
- const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
506
- const sessionState = { systemPrefixSeen: false };
507
- let counted = 0;
508
-
509
- for await (const line of rl) {
510
- if (!line || !line.includes('"usage"')) continue;
511
- let obj;
512
- try {
513
- obj = JSON.parse(line);
514
- } catch (_e) {
515
- continue;
516
- }
517
- const ts = typeof obj?.timestamp === "string" ? obj.timestamp : null;
518
- if (!ts) continue;
519
- if (fromIso && ts < fromIso) continue;
520
- if (toIso && ts > toIso) continue;
521
-
522
- const hash = claudeMessageDedupKey(obj);
523
- if (hash && seenHashes.has(hash)) continue;
524
-
525
- // Defer the hash-add until we've confirmed this entry carries non-zero
526
- // usage. Mirrors the same fix in rollout.js parseClaudeFile. Without it,
527
- // a zero-token streaming snapshot (e.g. Mimo's pre-response thinking
528
- // entry that shares the final response's message.id) would claim the
529
- // bare-msgId dedup key — Mimo has no requestId, so claudeMessageDedupKey
530
- // falls back to bare msgId — and pre-empt the real token-bearing entry,
531
- // collapsing Mimo into a tiny fraction of its real total.
532
- const usage = obj?.message?.usage;
533
- if (!hasNonZeroClaudeUsage(usage)) continue;
534
-
535
- if (hash) seenHashes.add(hash);
536
-
537
- classifyOneMessage(obj, sessionState, breakdown, toolLedger, skillLedger, execLedger);
538
- counted += 1;
539
- }
540
- rl.close();
541
- stream.close?.();
542
- return counted;
543
- }
544
-
545
- // Convert a YYYY-MM-DD day key (already in the user's tz from the API call)
546
- // into an inclusive ISO range. We still match against UTC timestamps in the
547
- // jsonl, so we widen by ±14h to be safe across timezones — totals are
548
- // post-filtered against the queue's authoritative UTC totals anyway, this
549
- // view is approximate by design.
550
- function dayKeyToIsoBounds(from, to) {
551
- if (!from && !to) return { fromIso: null, toIso: null };
552
- const fromDate = from ? new Date(`${from}T00:00:00Z`) : null;
553
- const toDate = to ? new Date(`${to}T23:59:59Z`) : null;
554
- if (fromDate && Number.isFinite(fromDate.getTime())) {
555
- fromDate.setUTCHours(fromDate.getUTCHours() - 14);
556
- }
557
- if (toDate && Number.isFinite(toDate.getTime())) {
558
- toDate.setUTCHours(toDate.getUTCHours() + 14);
559
- }
560
- return {
561
- fromIso: fromDate ? fromDate.toISOString() : null,
562
- toIso: toDate ? toDate.toISOString() : null,
563
- };
564
- }
565
-
566
- // Cache: keyed on (rootDir|from|to|files.length|maxMtime). The key already
567
- // changes the moment any session file is touched, so a long TTL is safe — we
568
- // don't need a short window as a "safety net". Mirror the in-memory cache
569
- // to disk so a fresh tracker process (e.g. menu bar app restart) doesn't pay
570
- // the cold-scan cost again.
571
- const CACHE = new Map();
572
- const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6h
573
- const CACHE_SCHEMA_VERSION = "skills-exec-v3";
574
- const DISK_CACHE_DIR = path.join(os.homedir(), ".tokenusage", "cache", "claude-categorizer");
575
-
576
- function cacheKeyHash(key) {
577
- return crypto.createHash("sha1").update(key).digest("hex").slice(0, 32);
578
- }
579
-
580
- function readDiskCache(key) {
581
- try {
582
- const fp = path.join(DISK_CACHE_DIR, `${cacheKeyHash(key)}.json`);
583
- const raw = fssync.readFileSync(fp, "utf8");
584
- const obj = JSON.parse(raw);
585
- if (!obj || obj.schemaVersion !== CACHE_SCHEMA_VERSION) return null;
586
- if (Date.now() - Number(obj.at || 0) >= CACHE_TTL_MS) return null;
587
- return obj.value;
588
- } catch (_e) {
589
- return null;
590
- }
591
- }
592
-
593
- function writeDiskCache(key, value) {
594
- try {
595
- fssync.mkdirSync(DISK_CACHE_DIR, { recursive: true });
596
- const fp = path.join(DISK_CACHE_DIR, `${cacheKeyHash(key)}.json`);
597
- const payload = { schemaVersion: CACHE_SCHEMA_VERSION, at: Date.now(), value };
598
- fssync.writeFileSync(fp, JSON.stringify(payload));
599
- // Bound on-disk size; categorizer is cheap to recompute when miss.
600
- try {
601
- const entries = fssync.readdirSync(DISK_CACHE_DIR)
602
- .filter((n) => n.endsWith(".json"))
603
- .map((n) => {
604
- const p = path.join(DISK_CACHE_DIR, n);
605
- let mtime = 0;
606
- try { mtime = fssync.statSync(p).mtimeMs; } catch (_e) {}
607
- return { p, mtime };
608
- })
609
- .sort((a, b) => b.mtime - a.mtime);
610
- for (const e of entries.slice(16)) {
611
- try { fssync.unlinkSync(e.p); } catch (_e) {}
612
- }
613
- } catch (_e) {}
614
- } catch (_e) {}
615
- }
616
-
617
- function maxMtimeMs(files) {
618
- let max = 0;
619
- for (const fp of files) {
620
- try {
621
- const st = fssync.statSync(fp);
622
- if (st.mtimeMs > max) max = st.mtimeMs;
623
- } catch (_e) {}
624
- }
625
- return max;
626
- }
627
-
628
- async function computeClaudeCategoryBreakdown({ from = null, to = null, rootDir = null, projectDir = null } = {}) {
629
- const root = rootDir || defaultClaudeProjectsDir();
630
- let files = [];
631
- try {
632
- files = listSessionFiles(root);
633
- } catch (_e) {
634
- return {
635
- source: "claude",
636
- scope: "supported",
637
- totals: emptyTotals(),
638
- categories: CATEGORY_KEYS.map((key) => ({
639
- key,
640
- totals: emptyTotals(),
641
- percent: 0,
642
- })),
643
- session_count: 0,
644
- message_count: 0,
645
- };
646
- }
647
-
648
- const cacheKey = `${CACHE_SCHEMA_VERSION}|${root}|${from || ""}|${to || ""}|${files.length}|${maxMtimeMs(files)}`;
649
- const cached = CACHE.get(cacheKey);
650
- if (cached && Date.now() - cached.at < CACHE_TTL_MS) {
651
- return cached.value;
652
- }
653
- const onDisk = readDiskCache(cacheKey);
654
- if (onDisk) {
655
- CACHE.set(cacheKey, { at: Date.now(), value: onDisk });
656
- return onDisk;
657
- }
658
-
659
- const { fromIso, toIso } = dayKeyToIsoBounds(from, to);
660
- const breakdown = emptyCategoryMap();
661
- const seenHashes = new Set();
662
- let messageCount = 0;
663
- let sessionCount = 0;
664
- const toolLedger = {
665
- tool_calls: { total_calls: 0, by_name: new Map() },
666
- subagents: { total_calls: 0, by_name: new Map() },
667
- };
668
- const skillLedger = { total_calls: 0, by_name: new Map() };
669
- const execLedger = {
670
- total_calls: 0,
671
- by_type: new Map(),
672
- by_executable: new Map(),
673
- by_command: new Map(),
674
- by_exit: new Map(),
675
- };
676
-
677
- // Process files with bounded parallelism. CPU-bound (JSON.parse per line)
678
- // limits the win, but overlapping the per-file fs.open + first-block read
679
- // I/O behind the previous file's parsing still shaves ~15% off cold scans.
680
- // `seenHashes`/`breakdown`/ledgers are mutated only inside synchronous
681
- // sections of `classifyOneMessage`; `for await` in `categorizeSessionFile`
682
- // only yields between lines, so workers can't tear shared state.
683
- const SCAN_CONCURRENCY = 4;
684
- let cursor = 0;
685
- async function worker() {
686
- while (cursor < files.length) {
687
- const idx = cursor++;
688
- if (idx >= files.length) return;
689
- const counted = await categorizeSessionFile(
690
- files[idx],
691
- { fromIso, toIso, seenHashes },
692
- breakdown,
693
- toolLedger,
694
- skillLedger,
695
- execLedger,
696
- );
697
- if (counted > 0) sessionCount += 1;
698
- messageCount += counted;
699
- }
700
- }
701
- await Promise.all(Array.from({ length: SCAN_CONCURRENCY }, () => worker()));
702
-
703
- const totals = emptyTotals();
704
- for (const key of CATEGORY_KEYS) addInto(totals, breakdown[key]);
705
-
706
- const result = {
707
- source: "claude",
708
- scope: "supported",
709
- totals,
710
- categories: CATEGORY_KEYS.map((key) => {
711
- const t = breakdown[key];
712
- const percent = totals.total_tokens > 0
713
- ? Number(((t.total_tokens / totals.total_tokens) * 100).toFixed(2))
714
- : 0;
715
- return { key, totals: t, percent };
716
- }),
717
- session_count: sessionCount,
718
- message_count: messageCount,
719
- message_breakdown: buildMessageBreakdown(breakdown),
720
- tool_calls_breakdown: buildToolCallsBreakdown(toolLedger),
721
- skills_breakdown: buildSkillsBreakdown(skillLedger),
722
- exec_command_breakdown: buildExecCommandBreakdown(execLedger),
723
- configured_resources: getConfiguredResources({ projectDir }),
724
- };
725
-
726
- CACHE.set(cacheKey, { at: Date.now(), value: result });
727
- writeDiskCache(cacheKey, result);
728
- // Bound cache size — categorizer is cheap to recompute, no point hoarding.
729
- while (CACHE.size > 32) {
730
- const oldest = [...CACHE.entries()].sort((a, b) => a[1].at - b[1].at)[0];
731
- if (oldest) CACHE.delete(oldest[0]);
732
- }
733
- return result;
734
- }
735
-
736
- function buildToolCallsBreakdown(toolLedger) {
737
- if (!toolLedger || !toolLedger.tool_calls || !toolLedger.subagents) {
738
- return {
739
- tool_calls: emptyToolBreakdown(),
740
- subagents: emptyToolBreakdown(),
741
- };
742
- }
743
-
744
- function mapToSortedRows(map) {
745
- const rows = Array.from(map.values()).map((row) => ({
746
- name: row.name,
747
- calls: row.calls,
748
- totals: row.totals || {
749
- ...emptyTotals(),
750
- output_tokens: row.output_tokens || 0,
751
- total_tokens: row.total_tokens || row.output_tokens || 0,
752
- },
753
- }));
754
- rows.sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
755
- return rows;
756
- }
757
-
758
- const toolCallsRows = mapToSortedRows(toolLedger.tool_calls.by_name || new Map());
759
- const subagentRows = mapToSortedRows(toolLedger.subagents.by_name || new Map());
760
-
761
- function groupIntoCategories(rows) {
762
- const byCategory = new Map(); // name -> {name,calls,totals,tools:[]}
763
- for (const row of rows) {
764
- const toolName = String(row?.name || "");
765
- if (!toolName) continue;
766
- const cat = categorizeTool(toolName);
767
- if (!byCategory.has(cat)) {
768
- byCategory.set(cat, {
769
- name: cat,
770
- calls: 0,
771
- totals: emptyTotals(),
772
- tools: [],
773
- });
774
- }
775
- const bucket = byCategory.get(cat);
776
- bucket.calls += Number(row.calls || 0);
777
- addInto(bucket.totals, row.totals || {});
778
- bucket.tools.push({
779
- name: toolName,
780
- calls: Number(row.calls || 0),
781
- totals: row.totals || emptyTotals(),
782
- });
783
- }
784
- const categories = Array.from(byCategory.values())
785
- .map((c) => ({
786
- name: c.name,
787
- calls: Math.round(c.calls || 0),
788
- totals: {
789
- input_tokens: Math.round(c.totals.input_tokens || 0),
790
- cached_input_tokens: Math.round(c.totals.cached_input_tokens || 0),
791
- cache_creation_input_tokens: Math.round(c.totals.cache_creation_input_tokens || 0),
792
- output_tokens: Math.round(c.totals.output_tokens || 0),
793
- reasoning_output_tokens: Math.round(c.totals.reasoning_output_tokens || 0),
794
- total_tokens: Math.round(c.totals.total_tokens || 0),
795
- },
796
- tools: c.tools
797
- .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0))
798
- .map((t) => ({
799
- name: t.name,
800
- calls: Math.round(t.calls || 0),
801
- totals: {
802
- input_tokens: Math.round(t.totals.input_tokens || 0),
803
- cached_input_tokens: Math.round(t.totals.cached_input_tokens || 0),
804
- cache_creation_input_tokens: Math.round(t.totals.cache_creation_input_tokens || 0),
805
- output_tokens: Math.round(t.totals.output_tokens || 0),
806
- reasoning_output_tokens: Math.round(t.totals.reasoning_output_tokens || 0),
807
- total_tokens: Math.round(t.totals.total_tokens || 0),
808
- },
809
- })),
810
- }))
811
- .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
812
- return categories;
813
- }
814
-
815
- return {
816
- tool_calls: {
817
- total_calls: toolLedger.tool_calls.total_calls || 0,
818
- tools: toolCallsRows,
819
- categories: groupIntoCategories(toolCallsRows),
820
- },
821
- subagents: {
822
- total_calls: toolLedger.subagents.total_calls || 0,
823
- tools: subagentRows,
824
- categories: groupIntoCategories(subagentRows),
825
- },
826
- privacy: {
827
- includes_inputs: false,
828
- note: "Aggregated tool names only; tool inputs are never recorded.",
829
- },
830
- };
831
- }
832
-
833
- function buildSkillsBreakdown(skillLedger) {
834
- const rows = Array.from(skillLedger?.by_name?.values?.() || [])
835
- .map((row) => ({
836
- name: row.name,
837
- calls: Math.round(row.calls || 0),
838
- totals: roundTotals(row.totals || emptyTotals()),
839
- }))
840
- .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
841
-
842
- return {
843
- total_calls: Math.round(skillLedger?.total_calls || 0),
844
- skills: rows,
845
- privacy: {
846
- includes_inputs: false,
847
- note: "Aggregated skill names only; skill inputs are never returned.",
848
- },
849
- };
850
- }
851
-
852
- function serializeExecRows(map) {
853
- return Array.from(map?.values?.() || [])
854
- .map((row) => ({
855
- name: row.name,
856
- calls: Math.round(row.calls || 0),
857
- failures: Math.round(row.failures || 0),
858
- duration_ms: Math.round(row.duration_ms || 0),
859
- max_duration_ms: Math.round(row.max_duration_ms || 0),
860
- output_chars: Math.round(row.output_chars || 0),
861
- output_lines: Math.round(row.output_lines || 0),
862
- totals: roundTotals(row.totals || emptyTotals()),
863
- }))
864
- .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
865
- }
866
-
867
- function buildExecCommandBreakdown(execLedger) {
868
- return {
869
- total_calls: Math.round(execLedger?.total_calls || 0),
870
- by_type: serializeExecRows(execLedger?.by_type),
871
- by_executable: serializeExecRows(execLedger?.by_executable),
872
- by_command: serializeExecRows(execLedger?.by_command),
873
- by_duration: [],
874
- by_output: [],
875
- by_exit: serializeExecRows(execLedger?.by_exit),
876
- privacy: {
877
- includes_commands: false,
878
- note: "Claude Bash commands are grouped into sanitized signatures; raw commands are not returned.",
879
- },
880
- };
881
- }
882
-
883
- function buildMessageBreakdown(breakdown) {
884
- const rows = [
885
- {
886
- key: "user_input",
887
- name: "User input",
888
- totals: breakdown.user_input || emptyTotals(),
889
- },
890
- {
891
- key: "conversation_history",
892
- name: "Conversation history",
893
- totals: breakdown.conversation_history || emptyTotals(),
894
- },
895
- {
896
- key: "assistant_response",
897
- name: "Assistant response",
898
- totals: breakdown.assistant_response || emptyTotals(),
899
- },
900
- ];
901
-
902
- return {
903
- categories: rows
904
- .map((row) => ({
905
- key: row.key,
906
- name: row.name,
907
- totals: {
908
- input_tokens: Math.round(row.totals.input_tokens || 0),
909
- cached_input_tokens: Math.round(row.totals.cached_input_tokens || 0),
910
- cache_creation_input_tokens: Math.round(row.totals.cache_creation_input_tokens || 0),
911
- output_tokens: Math.round(row.totals.output_tokens || 0),
912
- reasoning_output_tokens: Math.round(row.totals.reasoning_output_tokens || 0),
913
- total_tokens: Math.round(row.totals.total_tokens || 0),
914
- },
915
- }))
916
- .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0)),
917
- privacy: {
918
- includes_content: false,
919
- note: "Aggregated message token categories only; prompt and assistant text are never returned.",
920
- },
921
- };
922
- }
923
-
924
- // Lightweight on-disk count of static resources Claude Code's /context UI
925
- // also surfaces (Skills, MCP servers, Memory files, Custom agents). These are
926
- // counts of what's *installed*, not historical token usage — the same way
927
- // /context shows "MCP tools 0 (115)" with the install count in parens. Lets
928
- // the dashboard match that vocabulary even though token-level separation
929
- // from the system prompt isn't possible from the rollout logs alone.
930
- function countDirEntries(dir, predicate) {
931
- let entries;
932
- try {
933
- entries = fssync.readdirSync(dir, { withFileTypes: true });
934
- } catch (_e) {
935
- return 0;
936
- }
937
- return entries.filter(predicate).length;
938
- }
939
-
940
- function fileExists(fp) {
941
- try {
942
- return fssync.statSync(fp).isFile();
943
- } catch (_e) {
944
- return false;
945
- }
946
- }
947
-
948
- function safeReadJson(fp) {
949
- try {
950
- return JSON.parse(fssync.readFileSync(fp, "utf8"));
951
- } catch (_e) {
952
- return null;
953
- }
954
- }
955
-
956
- // Walk @./path imports recursively. Claude Code expands @file references
957
- // inside CLAUDE.md into separate memory entries; /context counts them.
958
- function collectMemoryImports(filePath, seen) {
959
- if (!filePath || seen.has(filePath)) return;
960
- seen.add(filePath);
961
- let raw;
962
- try {
963
- raw = fssync.readFileSync(filePath, "utf8");
964
- } catch (_e) {
965
- return;
966
- }
967
- const dir = path.dirname(filePath);
968
- // Match `@path/to/file.md` (CC's import syntax), but skip `@user@host` and
969
- // `email@host` patterns by requiring a path-like suffix.
970
- const re = /(?:^|\s)@([./~][^\s)]+\.md)\b/g;
971
- let m;
972
- while ((m = re.exec(raw)) !== null) {
973
- let target = m[1];
974
- if (target.startsWith("~")) target = path.join(os.homedir(), target.slice(1).replace(/^\//, ""));
975
- else if (!path.isAbsolute(target)) target = path.resolve(dir, target);
976
- if (fileExists(target)) collectMemoryImports(target, seen);
977
- }
978
- }
979
-
980
- function findLatestPluginVersionDir(pluginCacheRoot) {
981
- let entries;
982
- try {
983
- entries = fssync.readdirSync(pluginCacheRoot, { withFileTypes: true });
984
- } catch (_e) {
985
- return null;
986
- }
987
- const dirs = entries.filter((e) => e.isDirectory()).map((e) => e.name);
988
- if (dirs.length === 0) return null;
989
- // Pick highest semver-ish lex sort fallback. CC keeps the active version
990
- // path under the plugin's cache; if multiple versions linger, the lexically
991
- // largest is usually the latest installed.
992
- dirs.sort();
993
- return path.join(pluginCacheRoot, dirs[dirs.length - 1]);
994
- }
995
-
996
- function countSkillsInDir(rootDir) {
997
- // Walk subdirs once looking for SKILL.md / skill.md.
998
- let count = 0;
999
- const stack = [rootDir];
1000
- while (stack.length > 0) {
1001
- const dir = stack.pop();
1002
- let entries;
1003
- try {
1004
- entries = fssync.readdirSync(dir, { withFileTypes: true });
1005
- } catch (_e) {
1006
- continue;
1007
- }
1008
- for (const e of entries) {
1009
- if (!e.isDirectory()) continue;
1010
- const sub = path.join(dir, e.name);
1011
- if (fileExists(path.join(sub, "SKILL.md")) || fileExists(path.join(sub, "skill.md"))) {
1012
- count += 1;
1013
- } else {
1014
- stack.push(sub);
1015
- }
1016
- }
1017
- }
1018
- return count;
1019
- }
1020
-
1021
- function countAgentMarkdowns(rootDir) {
1022
- let count = 0;
1023
- const stack = [rootDir];
1024
- while (stack.length > 0) {
1025
- const dir = stack.pop();
1026
- let entries;
1027
- try {
1028
- entries = fssync.readdirSync(dir, { withFileTypes: true });
1029
- } catch (_e) {
1030
- continue;
1031
- }
1032
- for (const e of entries) {
1033
- const fp = path.join(dir, e.name);
1034
- if (e.isDirectory()) stack.push(fp);
1035
- else if (e.isFile() && e.name.endsWith(".md")) count += 1;
1036
- }
1037
- }
1038
- return count;
1039
- }
1040
-
1041
- function listEnabledPlugins() {
1042
- const home = os.homedir();
1043
- // settings.local.json overrides settings.json (CC's normal precedence).
1044
- const baseMap = safeReadJson(path.join(home, ".claude", "settings.json"))?.enabledPlugins || {};
1045
- const localMap = safeReadJson(path.join(home, ".claude", "settings.local.json"))?.enabledPlugins || {};
1046
- const merged = { ...baseMap, ...localMap };
1047
- return Object.entries(merged)
1048
- .filter(([, enabled]) => enabled === true)
1049
- .map(([key]) => key);
1050
- }
1051
-
1052
- function getConfiguredResources({ projectDir = null } = {}) {
1053
- const home = os.homedir();
1054
- const claudeRoot = path.join(home, ".claude");
1055
- const cacheRoot = path.join(claudeRoot, "plugins", "cache");
1056
-
1057
- // --- Skills ------------------------------------------------------------
1058
- let skillsCount = countSkillsInDir(path.join(claudeRoot, "skills"));
1059
- if (projectDir) {
1060
- skillsCount += countSkillsInDir(path.join(projectDir, ".claude", "skills"));
1061
- }
1062
-
1063
- // --- Custom agents -----------------------------------------------------
1064
- let agentsCount = countAgentMarkdowns(path.join(claudeRoot, "agents"));
1065
- if (projectDir) {
1066
- agentsCount += countAgentMarkdowns(path.join(projectDir, ".claude", "agents"));
1067
- }
1068
-
1069
- // --- MCP servers -------------------------------------------------------
1070
- // Primary: ~/.claude.json (single dot-json — CC's main config), NOT
1071
- // ~/.claude/settings.json (which holds GUI toggles, not MCP).
1072
- let mcpCount = 0;
1073
- const claudeJson = safeReadJson(path.join(home, ".claude.json"));
1074
- if (claudeJson?.mcpServers && typeof claudeJson.mcpServers === "object") {
1075
- mcpCount += Object.keys(claudeJson.mcpServers).length;
1076
- }
1077
- if (projectDir) {
1078
- const projectMcp = safeReadJson(path.join(projectDir, ".mcp.json"));
1079
- if (projectMcp?.mcpServers && typeof projectMcp.mcpServers === "object") {
1080
- mcpCount += Object.keys(projectMcp.mcpServers).length;
1081
- }
1082
- }
1083
-
1084
- // --- Plugin contributions (enabled plugins only) -----------------------
1085
- // Plugin caches live at ~/.claude/plugins/cache/<owner>/<plugin>/<version>/
1086
- // and contribute skills, agents, and mcpServers (declared in plugin.json).
1087
- for (const pluginKey of listEnabledPlugins()) {
1088
- // pluginKey is "name@marketplace" (e.g., "claude-mem@thedotmack").
1089
- const [name, marketplace] = pluginKey.split("@");
1090
- if (!name || !marketplace) continue;
1091
- const pluginRoot = path.join(cacheRoot, marketplace, name);
1092
- const versionDir = findLatestPluginVersionDir(pluginRoot);
1093
- if (!versionDir) continue;
1094
- skillsCount += countSkillsInDir(path.join(versionDir, "skills"));
1095
- agentsCount += countAgentMarkdowns(path.join(versionDir, "agents"));
1096
- const pluginManifest = safeReadJson(path.join(versionDir, ".claude-plugin", "plugin.json"));
1097
- if (pluginManifest?.mcpServers && typeof pluginManifest.mcpServers === "object") {
1098
- mcpCount += Object.keys(pluginManifest.mcpServers).length;
1099
- }
1100
- }
1101
-
1102
- // --- Memory files (CLAUDE.md + transitive @-imports) -------------------
1103
- const memorySeen = new Set();
1104
- const userMd = path.join(claudeRoot, "CLAUDE.md");
1105
- const homeMd = path.join(home, "CLAUDE.md");
1106
- if (fileExists(userMd)) collectMemoryImports(userMd, memorySeen);
1107
- if (fileExists(homeMd) && fssync.statSync(homeMd).size > 0) collectMemoryImports(homeMd, memorySeen);
1108
- // Walk up from projectDir to find the closest CLAUDE.md (CC walks up too).
1109
- // Handles dev servers running from a subdir (e.g. vite from dashboard/).
1110
- if (projectDir) {
1111
- let cursor = projectDir;
1112
- for (let i = 0; i < 8; i++) {
1113
- const candidate = path.join(cursor, "CLAUDE.md");
1114
- if (fileExists(candidate)) {
1115
- collectMemoryImports(candidate, memorySeen);
1116
- break;
1117
- }
1118
- const parent = path.dirname(cursor);
1119
- if (parent === cursor) break;
1120
- cursor = parent;
1121
- }
1122
- }
1123
-
1124
- return {
1125
- skills_count: skillsCount,
1126
- custom_agents_count: agentsCount,
1127
- memory_files_count: memorySeen.size,
1128
- mcp_servers_count: mcpCount,
1129
- };
1130
- }
1131
-
1132
- function unsupportedSourcePayload(source) {
1133
- return {
1134
- source,
1135
- scope: "unsupported",
1136
- totals: emptyTotals(),
1137
- categories: CATEGORY_KEYS.map((key) => ({
1138
- key,
1139
- totals: emptyTotals(),
1140
- percent: 0,
1141
- })),
1142
- session_count: 0,
1143
- message_count: 0,
1144
- };
1145
- }
1146
-
1147
- // ---------------------------------------------------------------------------
1148
- // Ground-truth bucket aggregator for queue.jsonl repair.
1149
- //
1150
- // `sync.js` historically used a stateful incremental pipeline (cursor offsets
1151
- // + persisted hash set) and the `reincludeClaudeMemObserverFiles` migration
1152
- // shipped 3 versions, each of which reset the hash set and re-read observer
1153
- // jsonls. Result: queue.jsonl ended up with ~+40% extra Claude tokens that
1154
- // never actually existed.
1155
- //
1156
- // This function is the source-of-truth replacement: scan every Claude jsonl,
1157
- // dedup messages by (msgId, requestId) globally — same algorithm ccusage
1158
- // uses — and emit one record per (model, hour_start) bucket. Callers (sync's
1159
- // repair migration) write these as authoritative rows to queue.jsonl,
1160
- // overwriting whatever was there for source=claude.
1161
- // ---------------------------------------------------------------------------
1162
-
1163
- function bucketAccumulator() {
1164
- return {
1165
- input_tokens: 0,
1166
- cached_input_tokens: 0,
1167
- cache_creation_input_tokens: 0,
1168
- output_tokens: 0,
1169
- reasoning_output_tokens: 0,
1170
- total_tokens: 0,
1171
- conversation_count: 0,
1172
- };
1173
- }
1174
-
1175
- function toUtcHalfHourStart(ts) {
1176
- const dt = new Date(ts);
1177
- if (!Number.isFinite(dt.getTime())) return null;
1178
- const minutes = dt.getUTCMinutes();
1179
- const halfMinute = minutes >= 30 ? 30 : 0;
1180
- return new Date(
1181
- Date.UTC(
1182
- dt.getUTCFullYear(),
1183
- dt.getUTCMonth(),
1184
- dt.getUTCDate(),
1185
- dt.getUTCHours(),
1186
- halfMinute,
1187
- 0,
1188
- 0,
1189
- ),
1190
- ).toISOString();
1191
- }
1192
-
1193
- async function computeClaudeGroundTruthBuckets({ rootDir = null, rootDirs = null } = {}) {
1194
- // Multi-root (#307): a Windows host may scan a native ~/.claude/projects
1195
- // plus a WSL install over \\wsl$. `rootDirs` wins over the legacy single
1196
- // `rootDir`; duplicated session files synced between environments collapse
1197
- // via the msgId+reqId dedup below.
1198
- const roots = Array.isArray(rootDirs) && rootDirs.length > 0
1199
- ? rootDirs
1200
- : [rootDir || defaultClaudeProjectsDir()];
1201
- const files = [];
1202
- const seenFiles = new Set();
1203
- for (const root of roots) {
1204
- for (const fp of listSessionFiles(root)) {
1205
- if (!seenFiles.has(fp)) {
1206
- seenFiles.add(fp);
1207
- files.push(fp);
1208
- }
1209
- }
1210
- }
1211
- const buckets = new Map(); // `${model}|${hourStart}` → totals
1212
- const seenHashes = new Set();
1213
- const userMessageBuckets = new Map(); // for conversation_count tracking
1214
- const forkAliases = await collectClaudeForkAliases(files);
1215
-
1216
- for (const fp of files) {
1217
- let stream;
1218
- try {
1219
- stream = fssync.createReadStream(fp, { encoding: "utf8" });
1220
- } catch (_e) {
1221
- continue;
1222
- }
1223
- const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
1224
- // Separator-agnostic: WSL roots are UNC paths where path.join produces
1225
- // backslashes (mirrors the same check in rollout.js parseClaudeFile).
1226
- const isMainSession = !/[\\/]subagents[\\/]/.test(fp);
1227
-
1228
- for await (const line of rl) {
1229
- if (!line) continue;
1230
-
1231
- // Conversation count = main-session user messages with text content
1232
- // (matches what parseClaudeFile in rollout.js does).
1233
- if (isMainSession && line.includes('"type":"user"')) {
1234
- let userObj;
1235
- try {
1236
- userObj = JSON.parse(line);
1237
- } catch (_e) {
1238
- /* skip */
1239
- }
1240
- if (userObj?.type === "user") {
1241
- const content = userObj?.message?.content;
1242
- const hasText =
1243
- typeof content === "string" ||
1244
- (Array.isArray(content) && content.some((b) => b?.type === "text"));
1245
- if (hasText) {
1246
- const userKey = claudeUserIdentity(userObj, forkAliases);
1247
- if (!userKey || !seenHashes.has(userKey)) {
1248
- if (userKey) seenHashes.add(userKey);
1249
- const ts = typeof userObj?.timestamp === "string" ? userObj.timestamp : null;
1250
- const hourStart = ts ? toUtcHalfHourStart(ts) : null;
1251
- if (hourStart) {
1252
- const k = `unknown|${hourStart}`;
1253
- userMessageBuckets.set(k, (userMessageBuckets.get(k) || 0) + 1);
1254
- }
1255
- }
1256
- }
1257
- }
1258
- }
1259
-
1260
- if (!line.includes('"usage"')) continue;
1261
- let obj;
1262
- try {
1263
- obj = JSON.parse(line);
1264
- } catch (_e) {
1265
- continue;
1266
- }
1267
- const usage = obj?.message?.usage;
1268
- const totals = readClaudeUsageTotals(usage);
1269
- if (!totals || !hasNonZeroClaudeUsage(usage)) continue;
1270
-
1271
- const hash = claudeMessageDedupKey(obj);
1272
- if (hash) {
1273
- if (seenHashes.has(hash)) continue;
1274
- seenHashes.add(hash);
1275
- }
1276
-
1277
- const model = (obj?.message?.model || obj?.model || "unknown").trim() || "unknown";
1278
- const ts = typeof obj?.timestamp === "string" ? obj.timestamp : null;
1279
- const hourStart = ts ? toUtcHalfHourStart(ts) : null;
1280
- if (!hourStart) continue;
1281
-
1282
- const key = `${model}|${hourStart}`;
1283
- let acc = buckets.get(key);
1284
- if (!acc) {
1285
- acc = bucketAccumulator();
1286
- buckets.set(key, acc);
1287
- }
1288
- acc.input_tokens += totals.input_tokens;
1289
- acc.cached_input_tokens += totals.cached_input_tokens;
1290
- acc.cache_creation_input_tokens += totals.cache_creation_input_tokens;
1291
- acc.output_tokens += totals.output_tokens;
1292
- acc.reasoning_output_tokens += totals.reasoning_output_tokens;
1293
- acc.total_tokens += totals.total_tokens;
1294
- }
1295
- rl.close();
1296
- stream.close?.();
1297
- }
1298
-
1299
- // Stitch user-message conversation counts onto the unknown-model bucket
1300
- // for the same hour (matches rollout.js behavior — user messages are
1301
- // counted under DEFAULT_MODEL because they have no model field).
1302
- for (const [key, count] of userMessageBuckets) {
1303
- let acc = buckets.get(key);
1304
- if (!acc) {
1305
- acc = bucketAccumulator();
1306
- buckets.set(key, acc);
1307
- }
1308
- acc.conversation_count += count;
1309
- }
1310
-
1311
- const out = [];
1312
- for (const [key, totals] of buckets) {
1313
- const sep = key.indexOf("|");
1314
- const model = key.slice(0, sep);
1315
- const hourStart = key.slice(sep + 1);
1316
- out.push({
1317
- source: "claude",
1318
- model,
1319
- hour_start: hourStart,
1320
- ...totals,
1321
- billable_total_tokens: totals.total_tokens,
1322
- });
1323
- }
1324
- return {
1325
- rows: out,
1326
- seenHashes: Array.from(seenHashes),
1327
- forkAliases,
1328
- fileList: files,
1329
- };
1330
- }
1331
-
1332
- module.exports = {
1333
- CATEGORY_KEYS,
1334
- computeClaudeCategoryBreakdown,
1335
- computeClaudeGroundTruthBuckets,
1336
- unsupportedSourcePayload,
1337
- getConfiguredResources,
1338
- // Exported for tests
1339
- splitOutputByContent,
1340
- classifyOneMessage,
1341
- emptyTotals,
1342
- emptyCategoryMap,
1343
- };
1
+ const { claudeUserIdentity, collectClaudeForkAliases } = require("./claude-user-identity");
2
+ // Claude Code "Context Breakdown" categorizer.
3
+ //
4
+ // Reads ~/.claude/projects/**/*.jsonl and splits each assistant message's
5
+ // usage into seven semantic buckets, mirroring (approximately) the Claude
6
+ // Code in-CLI /context view but as a historical aggregate. Computes on
7
+ // demand — no queue schema changes, no parser changes, no sync changes.
8
+ //
9
+ // Why these seven and not the screenshot's eight: the raw system prompt
10
+ // (which contains tools schema, skills, rules, MCP descriptions) is sent
11
+ // once per session as a 1h-ephemeral cache prefix and is never logged
12
+ // verbatim in the jsonl. So at the token-accounting layer those four are
13
+ // indistinguishable — they all collapse into `system_prefix`. UI says so.
14
+ const fssync = require("node:fs");
15
+ const os = require("node:os");
16
+ const path = require("node:path");
17
+ const crypto = require("node:crypto");
18
+ const readline = require("node:readline");
19
+
20
+ const {
21
+ categorizeTool,
22
+ inferExecCommandKind,
23
+ sanitizeCommandSignature,
24
+ getExecutableName,
25
+ emptyTotals,
26
+ addInto,
27
+ roundTotals,
28
+ buildExecStatsEntry,
29
+ allocateByLargestRemainder,
30
+ } = require("./categorizer-utils");
31
+ const { claudeMessageDedupKey } = require("./rollout");
32
+
33
+ const CATEGORY_KEYS = [
34
+ "system_prefix",
35
+ "conversation_history",
36
+ "user_input",
37
+ "tool_calls",
38
+ "subagents",
39
+ "reasoning",
40
+ "assistant_response",
41
+ ];
42
+
43
+ const SUBAGENT_TOOL_NAMES = new Set(["Agent", "Task"]);
44
+
45
+ function emptyToolBreakdown() {
46
+ return {
47
+ total_calls: 0,
48
+ tools: [],
49
+ };
50
+ }
51
+
52
+ function emptyCategoryMap() {
53
+ const out = {};
54
+ for (const key of CATEGORY_KEYS) out[key] = emptyTotals();
55
+ return out;
56
+ }
57
+
58
+ function toNonNegativeNumber(value) {
59
+ const n = Number(value || 0);
60
+ return Number.isFinite(n) ? Math.max(0, n) : 0;
61
+ }
62
+
63
+ function readClaudeUsageTotals(usage) {
64
+ if (!usage || typeof usage !== "object") return null;
65
+ const input_tokens = toNonNegativeNumber(usage.input_tokens);
66
+ const cached_input_tokens = toNonNegativeNumber(usage.cache_read_input_tokens);
67
+ const cache_creation_input_tokens = toNonNegativeNumber(usage.cache_creation_input_tokens);
68
+ const output_tokens = toNonNegativeNumber(usage.output_tokens);
69
+ const reasoning_output_tokens = toNonNegativeNumber(usage.reasoning_output_tokens);
70
+ const total_tokens =
71
+ input_tokens + cached_input_tokens + cache_creation_input_tokens + output_tokens;
72
+
73
+ return {
74
+ input_tokens,
75
+ cached_input_tokens,
76
+ cache_creation_input_tokens,
77
+ output_tokens,
78
+ reasoning_output_tokens,
79
+ total_tokens,
80
+ };
81
+ }
82
+
83
+ function hasNonZeroClaudeUsage(usage) {
84
+ const totals = readClaudeUsageTotals(usage);
85
+ if (!totals) return false;
86
+ return (
87
+ totals.input_tokens +
88
+ totals.cached_input_tokens +
89
+ totals.cache_creation_input_tokens +
90
+ totals.output_tokens +
91
+ totals.reasoning_output_tokens >
92
+ 0
93
+ );
94
+ }
95
+
96
+ function extractExecCommands(content) {
97
+ const commands = [];
98
+ for (const block of Array.isArray(content) ? content : []) {
99
+ if (!block || typeof block !== "object") continue;
100
+ if (block.type !== "tool_use") continue;
101
+ if (block.name !== "Bash" && block.name !== "exec_command") continue;
102
+ const input = block.input || {};
103
+ const command =
104
+ typeof input.command === "string" ? input.command
105
+ : typeof input.cmd === "string" ? input.cmd
106
+ : "";
107
+ if (command.trim()) commands.push(command.trim());
108
+ }
109
+ return commands;
110
+ }
111
+
112
+ function ensureExecRow(map, key) {
113
+ const safeKey = key || "unknown";
114
+ if (!map.has(safeKey)) map.set(safeKey, { name: safeKey, ...buildExecStatsEntry() });
115
+ return map.get(safeKey);
116
+ }
117
+
118
+ function recordExecCommandUsage(execLedger, commands, totals) {
119
+ if (!execLedger || !Array.isArray(commands) || commands.length === 0) return;
120
+ const perCommandRows = new Map();
121
+ for (const command of commands) {
122
+ if (!perCommandRows.has(command)) perCommandRows.set(command, { calls: 0 });
123
+ perCommandRows.get(command).calls += 1;
124
+ }
125
+ const totalsByCommand = allocateTotalsAcrossRows(totals, perCommandRows);
126
+
127
+ for (const [command, row] of perCommandRows.entries()) {
128
+ const commandTotals = totalsByCommand.get(command) || {};
129
+ const kind = inferExecCommandKind(command);
130
+ const executable = getExecutableName(command);
131
+ const signature = sanitizeCommandSignature(command);
132
+ const exitKey = "unknown:unknown";
133
+
134
+ const targets = [
135
+ [execLedger.by_type, kind],
136
+ [execLedger.by_executable, executable],
137
+ [execLedger.by_command, signature],
138
+ [execLedger.by_exit, exitKey],
139
+ ];
140
+ for (const [map, key] of targets) {
141
+ const target = ensureExecRow(map, key);
142
+ target.calls += Math.max(1, Number(row.calls || 0));
143
+ addInto(target.totals, commandTotals);
144
+ }
145
+ execLedger.total_calls += Math.max(1, Number(row.calls || 0));
146
+ }
147
+ }
148
+
149
+ function allocateTotalsAcrossRows(totals, rows) {
150
+ const entries = Array.from(rows?.entries?.() || []);
151
+ if (entries.length === 0) return new Map();
152
+ const weights = {};
153
+ const order = entries.map(([name]) => name).sort();
154
+ for (const name of order) {
155
+ const row = rows.get(name) || {};
156
+ weights[name] = Math.max(0, Number(row.output_tokens || row.calls || 0));
157
+ }
158
+ if (order.every((name) => !weights[name])) {
159
+ for (const name of order) weights[name] = 1;
160
+ }
161
+ const out = new Map();
162
+ for (const name of order) out.set(name, emptyTotals());
163
+ for (const key of [
164
+ "input_tokens",
165
+ "cached_input_tokens",
166
+ "cache_creation_input_tokens",
167
+ "output_tokens",
168
+ "reasoning_output_tokens",
169
+ "total_tokens",
170
+ ]) {
171
+ const alloc = allocateByLargestRemainder(Math.max(0, Number(totals?.[key] || 0)), weights, order);
172
+ for (const name of order) {
173
+ out.get(name)[key] = alloc[name] || 0;
174
+ }
175
+ }
176
+ return out;
177
+ }
178
+
179
+ function extractSkillNames(content) {
180
+ const names = [];
181
+ for (const block of Array.isArray(content) ? content : []) {
182
+ if (!block || typeof block !== "object") continue;
183
+ if (block.type !== "tool_use" || block.name !== "Skill") continue;
184
+ const skill = typeof block.input?.skill === "string" ? block.input.skill.trim() : "";
185
+ if (skill) names.push(skill);
186
+ }
187
+ return names;
188
+ }
189
+
190
+ function recordSkillUsage(skillLedger, skillNames, totals) {
191
+ if (!skillLedger || !Array.isArray(skillNames) || skillNames.length === 0) return;
192
+ const perMessageRows = new Map();
193
+ for (const name of skillNames) {
194
+ if (!perMessageRows.has(name)) perMessageRows.set(name, { calls: 0 });
195
+ perMessageRows.get(name).calls += 1;
196
+ }
197
+ const totalsByName = allocateTotalsAcrossRows(totals, perMessageRows);
198
+ for (const [name, row] of perMessageRows.entries()) {
199
+ if (!skillLedger.by_name.has(name)) {
200
+ skillLedger.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
201
+ }
202
+ const target = skillLedger.by_name.get(name);
203
+ target.calls += row.calls || 0;
204
+ addInto(target.totals, totalsByName.get(name) || {});
205
+ }
206
+ skillLedger.total_calls += skillNames.length;
207
+ }
208
+
209
+ function computeOutputTokenBreakdown(usage, content) {
210
+ const total = Math.max(0, Number(usage.output_tokens || 0));
211
+ const reasoningExplicit = Math.max(0, Number(usage.reasoning_output_tokens || 0));
212
+ const blocks = Array.isArray(content) ? content : [];
213
+
214
+ if (total === 0) {
215
+ return {
216
+ bucket_tokens: { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: 0 },
217
+ tool_calls: { total_calls: 0, by_name: new Map() },
218
+ subagents: { total_calls: 0, by_name: new Map() },
219
+ };
220
+ }
221
+
222
+ const bucketChars = { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: 0 };
223
+ const toolCallChars = new Map();
224
+ const subagentChars = new Map();
225
+ const toolCallCounts = new Map();
226
+ const subagentCounts = new Map();
227
+
228
+ let totalChars = 0;
229
+ for (const block of blocks) {
230
+ if (!block || typeof block !== "object") continue;
231
+ const type = block.type;
232
+ let chars = 0;
233
+
234
+ if (type === "thinking") {
235
+ chars = String(block.thinking || block.text || "").length || 1;
236
+ bucketChars.reasoning += chars;
237
+ } else if (type === "text") {
238
+ chars = String(block.text || "").length || 1;
239
+ bucketChars.assistant_response += chars;
240
+ } else if (type === "tool_use") {
241
+ const inputJson = block.input ? JSON.stringify(block.input) : "";
242
+ chars = (block.name || "").length + inputJson.length + 1;
243
+ if (SUBAGENT_TOOL_NAMES.has(block.name)) {
244
+ bucketChars.subagents += chars;
245
+ subagentChars.set(block.name, (subagentChars.get(block.name) || 0) + chars);
246
+ subagentCounts.set(block.name, (subagentCounts.get(block.name) || 0) + 1);
247
+ } else {
248
+ bucketChars.tool_calls += chars;
249
+ toolCallChars.set(block.name, (toolCallChars.get(block.name) || 0) + chars);
250
+ toolCallCounts.set(block.name, (toolCallCounts.get(block.name) || 0) + 1);
251
+ }
252
+ } else {
253
+ continue;
254
+ }
255
+
256
+ totalChars += chars;
257
+ }
258
+
259
+ if (totalChars === 0) {
260
+ return {
261
+ bucket_tokens: { reasoning: 0, tool_calls: 0, subagents: 0, assistant_response: total },
262
+ tool_calls: { total_calls: 0, by_name: new Map() },
263
+ subagents: { total_calls: 0, by_name: new Map() },
264
+ };
265
+ }
266
+
267
+ const explicitReasoning = reasoningExplicit > 0 ? Math.min(reasoningExplicit, total) : 0;
268
+ const nonReasoningOutput = total - explicitReasoning;
269
+
270
+ const allocChars = { ...bucketChars };
271
+ let allocTotalChars = totalChars;
272
+ if (explicitReasoning > 0) {
273
+ allocTotalChars -= allocChars.reasoning;
274
+ allocChars.reasoning = 0;
275
+ }
276
+
277
+ const order = ["reasoning", "tool_calls", "subagents", "assistant_response"];
278
+ const prorated = allocateByLargestRemainder(Math.max(0, nonReasoningOutput), allocChars, order);
279
+
280
+ const bucketTokens = {
281
+ reasoning: explicitReasoning + (prorated.reasoning || 0),
282
+ tool_calls: prorated.tool_calls || 0,
283
+ subagents: prorated.subagents || 0,
284
+ assistant_response: prorated.assistant_response || 0,
285
+ };
286
+
287
+ if (allocTotalChars <= 0 && nonReasoningOutput > 0) {
288
+ bucketTokens.reasoning = explicitReasoning;
289
+ bucketTokens.tool_calls = 0;
290
+ bucketTokens.subagents = 0;
291
+ bucketTokens.assistant_response = nonReasoningOutput;
292
+ }
293
+
294
+ const toolTokensByName = new Map();
295
+ if (bucketTokens.tool_calls > 0 && toolCallChars.size > 0) {
296
+ const keys = [...toolCallChars.keys()].sort();
297
+ const weights = {};
298
+ for (const k of keys) weights[k] = toolCallChars.get(k) || 0;
299
+ const alloc = allocateByLargestRemainder(bucketTokens.tool_calls, weights, keys);
300
+ for (const k of keys) toolTokensByName.set(k, alloc[k] || 0);
301
+ }
302
+
303
+ const subagentTokensByName = new Map();
304
+ if (bucketTokens.subagents > 0 && subagentChars.size > 0) {
305
+ const keys = [...subagentChars.keys()].sort();
306
+ const weights = {};
307
+ for (const k of keys) weights[k] = subagentChars.get(k) || 0;
308
+ const alloc = allocateByLargestRemainder(bucketTokens.subagents, weights, keys);
309
+ for (const k of keys) subagentTokensByName.set(k, alloc[k] || 0);
310
+ }
311
+
312
+ return {
313
+ bucket_tokens: bucketTokens,
314
+ tool_calls: {
315
+ total_calls: [...toolCallCounts.values()].reduce((a, b) => a + b, 0),
316
+ by_name: new Map(
317
+ [...toolCallCounts.entries()].map(([name, calls]) => [
318
+ name,
319
+ { name, calls, output_tokens: toolTokensByName.get(name) || 0 },
320
+ ]),
321
+ ),
322
+ },
323
+ subagents: {
324
+ total_calls: [...subagentCounts.values()].reduce((a, b) => a + b, 0),
325
+ by_name: new Map(
326
+ [...subagentCounts.entries()].map(([name, calls]) => [
327
+ name,
328
+ { name, calls, output_tokens: subagentTokensByName.get(name) || 0 },
329
+ ]),
330
+ ),
331
+ },
332
+ };
333
+ }
334
+
335
+ function defaultClaudeProjectsDir() {
336
+ return path.join(os.homedir(), ".claude", "projects");
337
+ }
338
+
339
+ function listSessionFiles(rootDir) {
340
+ const out = [];
341
+ const stack = [rootDir];
342
+ while (stack.length > 0) {
343
+ const dir = stack.pop();
344
+ let entries;
345
+ try {
346
+ entries = fssync.readdirSync(dir, { withFileTypes: true });
347
+ } catch (_e) {
348
+ continue;
349
+ }
350
+ for (const entry of entries) {
351
+ const fp = path.join(dir, entry.name);
352
+ if (entry.isDirectory()) {
353
+ stack.push(fp);
354
+ } else if (entry.isFile() && entry.name.endsWith(".jsonl")) {
355
+ out.push(fp);
356
+ }
357
+ }
358
+ }
359
+ return out;
360
+ }
361
+
362
+ // Distribute one assistant message's output tokens across categories by the
363
+ // character-length ratio of each content block. Thinking goes to reasoning,
364
+ // tool_use(Agent|Task) → subagents, tool_use(other) → tool_calls, text →
365
+ // assistant_response. If reasoning_output_tokens is reported separately, use
366
+ // that exact figure for reasoning instead of pro-rating.
367
+ function splitOutputByContent(usage, content, breakdown) {
368
+ const out = computeOutputTokenBreakdown(usage, content);
369
+ for (const key of ["reasoning", "tool_calls", "subagents", "assistant_response"]) {
370
+ const tok = out.bucket_tokens[key] || 0;
371
+ if (tok === 0) continue;
372
+ breakdown[key].output_tokens += tok;
373
+ breakdown[key].total_tokens += tok;
374
+ if (key === "reasoning") breakdown[key].reasoning_output_tokens += tok;
375
+ }
376
+ }
377
+
378
+ // Per-session state lets us pick out the *first* meaningful cache_creation
379
+ // chunk and call that the system_prefix. Subsequent cache_creations are
380
+ // incremental — we attribute them to conversation_history.
381
+ function classifyOneMessage(obj, sessionState, breakdown, toolLedger = null, skillLedger = null, execLedger = null) {
382
+ const usage = obj?.message?.usage;
383
+ if (!usage || typeof usage !== "object") return;
384
+
385
+ const cacheCreate = Math.max(0, Number(usage.cache_creation_input_tokens || 0));
386
+ const cacheRead = Math.max(0, Number(usage.cache_read_input_tokens || 0));
387
+ const inputNonCached = Math.max(0, Number(usage.input_tokens || 0));
388
+ const output = Math.max(0, Number(usage.output_tokens || 0));
389
+
390
+ // input_tokens (pure non-cached) → user_input
391
+ if (inputNonCached > 0) {
392
+ breakdown.user_input.input_tokens += inputNonCached;
393
+ breakdown.user_input.total_tokens += inputNonCached;
394
+ }
395
+
396
+ // cache_read_input_tokens → conversation_history (replaying earlier turns)
397
+ if (cacheRead > 0) {
398
+ breakdown.conversation_history.cached_input_tokens += cacheRead;
399
+ breakdown.conversation_history.total_tokens += cacheRead;
400
+ }
401
+
402
+ // cache_creation_input_tokens: first big block of a session = system_prefix,
403
+ // everything after = incremental conversation history.
404
+ if (cacheCreate > 0) {
405
+ if (!sessionState.systemPrefixSeen) {
406
+ breakdown.system_prefix.cache_creation_input_tokens += cacheCreate;
407
+ breakdown.system_prefix.total_tokens += cacheCreate;
408
+ sessionState.systemPrefixSeen = true;
409
+ } else {
410
+ breakdown.conversation_history.cache_creation_input_tokens += cacheCreate;
411
+ breakdown.conversation_history.total_tokens += cacheCreate;
412
+ }
413
+ }
414
+
415
+ recordSkillUsage(
416
+ skillLedger,
417
+ extractSkillNames(obj?.message?.content),
418
+ {
419
+ input_tokens: inputNonCached,
420
+ cached_input_tokens: cacheRead,
421
+ cache_creation_input_tokens: cacheCreate,
422
+ output_tokens: output,
423
+ reasoning_output_tokens: 0,
424
+ total_tokens: inputNonCached + cacheRead + cacheCreate + output,
425
+ },
426
+ );
427
+
428
+ // Split output across reasoning / tool_calls / subagents / assistant_response.
429
+ if (output > 0) {
430
+ const out = computeOutputTokenBreakdown(
431
+ { output_tokens: output, reasoning_output_tokens: usage.reasoning_output_tokens },
432
+ obj?.message?.content,
433
+ );
434
+ for (const key of ["reasoning", "tool_calls", "subagents", "assistant_response"]) {
435
+ const tok = out.bucket_tokens[key] || 0;
436
+ if (tok === 0) continue;
437
+ breakdown[key].output_tokens += tok;
438
+ breakdown[key].total_tokens += tok;
439
+ if (key === "reasoning") breakdown[key].reasoning_output_tokens += tok;
440
+ }
441
+
442
+ recordExecCommandUsage(
443
+ execLedger,
444
+ extractExecCommands(obj?.message?.content),
445
+ {
446
+ input_tokens: inputNonCached,
447
+ cached_input_tokens: cacheRead,
448
+ cache_creation_input_tokens: cacheCreate,
449
+ output_tokens: out.bucket_tokens.tool_calls || 0,
450
+ reasoning_output_tokens: 0,
451
+ total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.tool_calls || 0),
452
+ },
453
+ );
454
+
455
+ if (toolLedger) {
456
+ const ledgerTotals = {
457
+ input_tokens: inputNonCached,
458
+ cached_input_tokens: cacheRead,
459
+ cache_creation_input_tokens: cacheCreate,
460
+ output_tokens: out.bucket_tokens.tool_calls || 0,
461
+ reasoning_output_tokens: 0,
462
+ total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.tool_calls || 0),
463
+ };
464
+ const toolTotalsByName = allocateTotalsAcrossRows(ledgerTotals, out.tool_calls.by_name);
465
+ for (const [name, row] of out.tool_calls.by_name.entries()) {
466
+ if (!toolLedger.tool_calls.by_name.has(name)) {
467
+ toolLedger.tool_calls.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
468
+ }
469
+ const target = toolLedger.tool_calls.by_name.get(name);
470
+ target.calls += row.calls || 0;
471
+ addInto(target.totals, toolTotalsByName.get(name) || {});
472
+ }
473
+ toolLedger.tool_calls.total_calls += out.tool_calls.total_calls || 0;
474
+
475
+ const subagentTotals = {
476
+ input_tokens: inputNonCached,
477
+ cached_input_tokens: cacheRead,
478
+ cache_creation_input_tokens: cacheCreate,
479
+ output_tokens: out.bucket_tokens.subagents || 0,
480
+ reasoning_output_tokens: 0,
481
+ total_tokens: inputNonCached + cacheRead + cacheCreate + (out.bucket_tokens.subagents || 0),
482
+ };
483
+ const subagentTotalsByName = allocateTotalsAcrossRows(subagentTotals, out.subagents.by_name);
484
+ for (const [name, row] of out.subagents.by_name.entries()) {
485
+ if (!toolLedger.subagents.by_name.has(name)) {
486
+ toolLedger.subagents.by_name.set(name, { name, calls: 0, totals: emptyTotals() });
487
+ }
488
+ const target = toolLedger.subagents.by_name.get(name);
489
+ target.calls += row.calls || 0;
490
+ addInto(target.totals, subagentTotalsByName.get(name) || {});
491
+ }
492
+ toolLedger.subagents.total_calls += out.subagents.total_calls || 0;
493
+ }
494
+ }
495
+ }
496
+
497
+ // Read one session jsonl streaming, in timestamp range, dedup by msgId+reqId.
498
+ async function categorizeSessionFile(filePath, { fromIso, toIso, seenHashes }, breakdown, toolLedger = null, skillLedger = null, execLedger = null) {
499
+ let stream;
500
+ try {
501
+ stream = fssync.createReadStream(filePath, { encoding: "utf8" });
502
+ } catch (_e) {
503
+ return 0;
504
+ }
505
+ const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
506
+ const sessionState = { systemPrefixSeen: false };
507
+ let counted = 0;
508
+
509
+ for await (const line of rl) {
510
+ if (!line || !line.includes('"usage"')) continue;
511
+ let obj;
512
+ try {
513
+ obj = JSON.parse(line);
514
+ } catch (_e) {
515
+ continue;
516
+ }
517
+ const ts = typeof obj?.timestamp === "string" ? obj.timestamp : null;
518
+ if (!ts) continue;
519
+ if (fromIso && ts < fromIso) continue;
520
+ if (toIso && ts > toIso) continue;
521
+
522
+ const hash = claudeMessageDedupKey(obj);
523
+ if (hash && seenHashes.has(hash)) continue;
524
+
525
+ // Defer the hash-add until we've confirmed this entry carries non-zero
526
+ // usage. Mirrors the same fix in rollout.js parseClaudeFile. Without it,
527
+ // a zero-token streaming snapshot (e.g. Mimo's pre-response thinking
528
+ // entry that shares the final response's message.id) would claim the
529
+ // bare-msgId dedup key — Mimo has no requestId, so claudeMessageDedupKey
530
+ // falls back to bare msgId — and pre-empt the real token-bearing entry,
531
+ // collapsing Mimo into a tiny fraction of its real total.
532
+ const usage = obj?.message?.usage;
533
+ if (!hasNonZeroClaudeUsage(usage)) continue;
534
+
535
+ if (hash) seenHashes.add(hash);
536
+
537
+ classifyOneMessage(obj, sessionState, breakdown, toolLedger, skillLedger, execLedger);
538
+ counted += 1;
539
+ }
540
+ rl.close();
541
+ stream.close?.();
542
+ return counted;
543
+ }
544
+
545
+ // Convert a YYYY-MM-DD day key (already in the user's tz from the API call)
546
+ // into an inclusive ISO range. We still match against UTC timestamps in the
547
+ // jsonl, so we widen by ±14h to be safe across timezones — totals are
548
+ // post-filtered against the queue's authoritative UTC totals anyway, this
549
+ // view is approximate by design.
550
+ function dayKeyToIsoBounds(from, to) {
551
+ if (!from && !to) return { fromIso: null, toIso: null };
552
+ const fromDate = from ? new Date(`${from}T00:00:00Z`) : null;
553
+ const toDate = to ? new Date(`${to}T23:59:59Z`) : null;
554
+ if (fromDate && Number.isFinite(fromDate.getTime())) {
555
+ fromDate.setUTCHours(fromDate.getUTCHours() - 14);
556
+ }
557
+ if (toDate && Number.isFinite(toDate.getTime())) {
558
+ toDate.setUTCHours(toDate.getUTCHours() + 14);
559
+ }
560
+ return {
561
+ fromIso: fromDate ? fromDate.toISOString() : null,
562
+ toIso: toDate ? toDate.toISOString() : null,
563
+ };
564
+ }
565
+
566
+ // Cache: keyed on (rootDir|from|to|files.length|maxMtime). The key already
567
+ // changes the moment any session file is touched, so a long TTL is safe — we
568
+ // don't need a short window as a "safety net". Mirror the in-memory cache
569
+ // to disk so a fresh tracker process (e.g. menu bar app restart) doesn't pay
570
+ // the cold-scan cost again.
571
+ const CACHE = new Map();
572
+ const CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6h
573
+ const CACHE_SCHEMA_VERSION = "skills-exec-v3";
574
+ const DISK_CACHE_DIR = path.join(os.homedir(), ".tokenusage", "cache", "claude-categorizer");
575
+
576
+ function cacheKeyHash(key) {
577
+ return crypto.createHash("sha1").update(key).digest("hex").slice(0, 32);
578
+ }
579
+
580
+ function readDiskCache(key) {
581
+ try {
582
+ const fp = path.join(DISK_CACHE_DIR, `${cacheKeyHash(key)}.json`);
583
+ const raw = fssync.readFileSync(fp, "utf8");
584
+ const obj = JSON.parse(raw);
585
+ if (!obj || obj.schemaVersion !== CACHE_SCHEMA_VERSION) return null;
586
+ if (Date.now() - Number(obj.at || 0) >= CACHE_TTL_MS) return null;
587
+ return obj.value;
588
+ } catch (_e) {
589
+ return null;
590
+ }
591
+ }
592
+
593
+ function writeDiskCache(key, value) {
594
+ try {
595
+ fssync.mkdirSync(DISK_CACHE_DIR, { recursive: true });
596
+ const fp = path.join(DISK_CACHE_DIR, `${cacheKeyHash(key)}.json`);
597
+ const payload = { schemaVersion: CACHE_SCHEMA_VERSION, at: Date.now(), value };
598
+ fssync.writeFileSync(fp, JSON.stringify(payload));
599
+ // Bound on-disk size; categorizer is cheap to recompute when miss.
600
+ try {
601
+ const entries = fssync.readdirSync(DISK_CACHE_DIR)
602
+ .filter((n) => n.endsWith(".json"))
603
+ .map((n) => {
604
+ const p = path.join(DISK_CACHE_DIR, n);
605
+ let mtime = 0;
606
+ try { mtime = fssync.statSync(p).mtimeMs; } catch (_e) {}
607
+ return { p, mtime };
608
+ })
609
+ .sort((a, b) => b.mtime - a.mtime);
610
+ for (const e of entries.slice(16)) {
611
+ try { fssync.unlinkSync(e.p); } catch (_e) {}
612
+ }
613
+ } catch (_e) {}
614
+ } catch (_e) {}
615
+ }
616
+
617
+ function maxMtimeMs(files) {
618
+ let max = 0;
619
+ for (const fp of files) {
620
+ try {
621
+ const st = fssync.statSync(fp);
622
+ if (st.mtimeMs > max) max = st.mtimeMs;
623
+ } catch (_e) {}
624
+ }
625
+ return max;
626
+ }
627
+
628
+ async function computeClaudeCategoryBreakdown({ from = null, to = null, rootDir = null, projectDir = null } = {}) {
629
+ const root = rootDir || defaultClaudeProjectsDir();
630
+ let files = [];
631
+ try {
632
+ files = listSessionFiles(root);
633
+ } catch (_e) {
634
+ return {
635
+ source: "claude",
636
+ scope: "supported",
637
+ totals: emptyTotals(),
638
+ categories: CATEGORY_KEYS.map((key) => ({
639
+ key,
640
+ totals: emptyTotals(),
641
+ percent: 0,
642
+ })),
643
+ session_count: 0,
644
+ message_count: 0,
645
+ };
646
+ }
647
+
648
+ const cacheKey = `${CACHE_SCHEMA_VERSION}|${root}|${from || ""}|${to || ""}|${files.length}|${maxMtimeMs(files)}`;
649
+ const cached = CACHE.get(cacheKey);
650
+ if (cached && Date.now() - cached.at < CACHE_TTL_MS) {
651
+ return cached.value;
652
+ }
653
+ const onDisk = readDiskCache(cacheKey);
654
+ if (onDisk) {
655
+ CACHE.set(cacheKey, { at: Date.now(), value: onDisk });
656
+ return onDisk;
657
+ }
658
+
659
+ const { fromIso, toIso } = dayKeyToIsoBounds(from, to);
660
+ const breakdown = emptyCategoryMap();
661
+ const seenHashes = new Set();
662
+ let messageCount = 0;
663
+ let sessionCount = 0;
664
+ const toolLedger = {
665
+ tool_calls: { total_calls: 0, by_name: new Map() },
666
+ subagents: { total_calls: 0, by_name: new Map() },
667
+ };
668
+ const skillLedger = { total_calls: 0, by_name: new Map() };
669
+ const execLedger = {
670
+ total_calls: 0,
671
+ by_type: new Map(),
672
+ by_executable: new Map(),
673
+ by_command: new Map(),
674
+ by_exit: new Map(),
675
+ };
676
+
677
+ // Process files with bounded parallelism. CPU-bound (JSON.parse per line)
678
+ // limits the win, but overlapping the per-file fs.open + first-block read
679
+ // I/O behind the previous file's parsing still shaves ~15% off cold scans.
680
+ // `seenHashes`/`breakdown`/ledgers are mutated only inside synchronous
681
+ // sections of `classifyOneMessage`; `for await` in `categorizeSessionFile`
682
+ // only yields between lines, so workers can't tear shared state.
683
+ const SCAN_CONCURRENCY = 4;
684
+ let cursor = 0;
685
+ async function worker() {
686
+ while (cursor < files.length) {
687
+ const idx = cursor++;
688
+ if (idx >= files.length) return;
689
+ const counted = await categorizeSessionFile(
690
+ files[idx],
691
+ { fromIso, toIso, seenHashes },
692
+ breakdown,
693
+ toolLedger,
694
+ skillLedger,
695
+ execLedger,
696
+ );
697
+ if (counted > 0) sessionCount += 1;
698
+ messageCount += counted;
699
+ }
700
+ }
701
+ await Promise.all(Array.from({ length: SCAN_CONCURRENCY }, () => worker()));
702
+
703
+ const totals = emptyTotals();
704
+ for (const key of CATEGORY_KEYS) addInto(totals, breakdown[key]);
705
+
706
+ const result = {
707
+ source: "claude",
708
+ scope: "supported",
709
+ totals,
710
+ categories: CATEGORY_KEYS.map((key) => {
711
+ const t = breakdown[key];
712
+ const percent = totals.total_tokens > 0
713
+ ? Number(((t.total_tokens / totals.total_tokens) * 100).toFixed(2))
714
+ : 0;
715
+ return { key, totals: t, percent };
716
+ }),
717
+ session_count: sessionCount,
718
+ message_count: messageCount,
719
+ message_breakdown: buildMessageBreakdown(breakdown),
720
+ tool_calls_breakdown: buildToolCallsBreakdown(toolLedger),
721
+ skills_breakdown: buildSkillsBreakdown(skillLedger),
722
+ exec_command_breakdown: buildExecCommandBreakdown(execLedger),
723
+ configured_resources: getConfiguredResources({ projectDir }),
724
+ };
725
+
726
+ CACHE.set(cacheKey, { at: Date.now(), value: result });
727
+ writeDiskCache(cacheKey, result);
728
+ // Bound cache size — categorizer is cheap to recompute, no point hoarding.
729
+ while (CACHE.size > 32) {
730
+ const oldest = [...CACHE.entries()].sort((a, b) => a[1].at - b[1].at)[0];
731
+ if (oldest) CACHE.delete(oldest[0]);
732
+ }
733
+ return result;
734
+ }
735
+
736
+ function buildToolCallsBreakdown(toolLedger) {
737
+ if (!toolLedger || !toolLedger.tool_calls || !toolLedger.subagents) {
738
+ return {
739
+ tool_calls: emptyToolBreakdown(),
740
+ subagents: emptyToolBreakdown(),
741
+ };
742
+ }
743
+
744
+ function mapToSortedRows(map) {
745
+ const rows = Array.from(map.values()).map((row) => ({
746
+ name: row.name,
747
+ calls: row.calls,
748
+ totals: row.totals || {
749
+ ...emptyTotals(),
750
+ output_tokens: row.output_tokens || 0,
751
+ total_tokens: row.total_tokens || row.output_tokens || 0,
752
+ },
753
+ }));
754
+ rows.sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
755
+ return rows;
756
+ }
757
+
758
+ const toolCallsRows = mapToSortedRows(toolLedger.tool_calls.by_name || new Map());
759
+ const subagentRows = mapToSortedRows(toolLedger.subagents.by_name || new Map());
760
+
761
+ function groupIntoCategories(rows) {
762
+ const byCategory = new Map(); // name -> {name,calls,totals,tools:[]}
763
+ for (const row of rows) {
764
+ const toolName = String(row?.name || "");
765
+ if (!toolName) continue;
766
+ const cat = categorizeTool(toolName);
767
+ if (!byCategory.has(cat)) {
768
+ byCategory.set(cat, {
769
+ name: cat,
770
+ calls: 0,
771
+ totals: emptyTotals(),
772
+ tools: [],
773
+ });
774
+ }
775
+ const bucket = byCategory.get(cat);
776
+ bucket.calls += Number(row.calls || 0);
777
+ addInto(bucket.totals, row.totals || {});
778
+ bucket.tools.push({
779
+ name: toolName,
780
+ calls: Number(row.calls || 0),
781
+ totals: row.totals || emptyTotals(),
782
+ });
783
+ }
784
+ const categories = Array.from(byCategory.values())
785
+ .map((c) => ({
786
+ name: c.name,
787
+ calls: Math.round(c.calls || 0),
788
+ totals: {
789
+ input_tokens: Math.round(c.totals.input_tokens || 0),
790
+ cached_input_tokens: Math.round(c.totals.cached_input_tokens || 0),
791
+ cache_creation_input_tokens: Math.round(c.totals.cache_creation_input_tokens || 0),
792
+ output_tokens: Math.round(c.totals.output_tokens || 0),
793
+ reasoning_output_tokens: Math.round(c.totals.reasoning_output_tokens || 0),
794
+ total_tokens: Math.round(c.totals.total_tokens || 0),
795
+ },
796
+ tools: c.tools
797
+ .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0))
798
+ .map((t) => ({
799
+ name: t.name,
800
+ calls: Math.round(t.calls || 0),
801
+ totals: {
802
+ input_tokens: Math.round(t.totals.input_tokens || 0),
803
+ cached_input_tokens: Math.round(t.totals.cached_input_tokens || 0),
804
+ cache_creation_input_tokens: Math.round(t.totals.cache_creation_input_tokens || 0),
805
+ output_tokens: Math.round(t.totals.output_tokens || 0),
806
+ reasoning_output_tokens: Math.round(t.totals.reasoning_output_tokens || 0),
807
+ total_tokens: Math.round(t.totals.total_tokens || 0),
808
+ },
809
+ })),
810
+ }))
811
+ .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
812
+ return categories;
813
+ }
814
+
815
+ return {
816
+ tool_calls: {
817
+ total_calls: toolLedger.tool_calls.total_calls || 0,
818
+ tools: toolCallsRows,
819
+ categories: groupIntoCategories(toolCallsRows),
820
+ },
821
+ subagents: {
822
+ total_calls: toolLedger.subagents.total_calls || 0,
823
+ tools: subagentRows,
824
+ categories: groupIntoCategories(subagentRows),
825
+ },
826
+ privacy: {
827
+ includes_inputs: false,
828
+ note: "Aggregated tool names only; tool inputs are never recorded.",
829
+ },
830
+ };
831
+ }
832
+
833
+ function buildSkillsBreakdown(skillLedger) {
834
+ const rows = Array.from(skillLedger?.by_name?.values?.() || [])
835
+ .map((row) => ({
836
+ name: row.name,
837
+ calls: Math.round(row.calls || 0),
838
+ totals: roundTotals(row.totals || emptyTotals()),
839
+ }))
840
+ .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
841
+
842
+ return {
843
+ total_calls: Math.round(skillLedger?.total_calls || 0),
844
+ skills: rows,
845
+ privacy: {
846
+ includes_inputs: false,
847
+ note: "Aggregated skill names only; skill inputs are never returned.",
848
+ },
849
+ };
850
+ }
851
+
852
+ function serializeExecRows(map) {
853
+ return Array.from(map?.values?.() || [])
854
+ .map((row) => ({
855
+ name: row.name,
856
+ calls: Math.round(row.calls || 0),
857
+ failures: Math.round(row.failures || 0),
858
+ duration_ms: Math.round(row.duration_ms || 0),
859
+ max_duration_ms: Math.round(row.max_duration_ms || 0),
860
+ output_chars: Math.round(row.output_chars || 0),
861
+ output_lines: Math.round(row.output_lines || 0),
862
+ totals: roundTotals(row.totals || emptyTotals()),
863
+ }))
864
+ .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0));
865
+ }
866
+
867
+ function buildExecCommandBreakdown(execLedger) {
868
+ return {
869
+ total_calls: Math.round(execLedger?.total_calls || 0),
870
+ by_type: serializeExecRows(execLedger?.by_type),
871
+ by_executable: serializeExecRows(execLedger?.by_executable),
872
+ by_command: serializeExecRows(execLedger?.by_command),
873
+ by_duration: [],
874
+ by_output: [],
875
+ by_exit: serializeExecRows(execLedger?.by_exit),
876
+ privacy: {
877
+ includes_commands: false,
878
+ note: "Claude Bash commands are grouped into sanitized signatures; raw commands are not returned.",
879
+ },
880
+ };
881
+ }
882
+
883
+ function buildMessageBreakdown(breakdown) {
884
+ const rows = [
885
+ {
886
+ key: "user_input",
887
+ name: "User input",
888
+ totals: breakdown.user_input || emptyTotals(),
889
+ },
890
+ {
891
+ key: "conversation_history",
892
+ name: "Conversation history",
893
+ totals: breakdown.conversation_history || emptyTotals(),
894
+ },
895
+ {
896
+ key: "assistant_response",
897
+ name: "Assistant response",
898
+ totals: breakdown.assistant_response || emptyTotals(),
899
+ },
900
+ ];
901
+
902
+ return {
903
+ categories: rows
904
+ .map((row) => ({
905
+ key: row.key,
906
+ name: row.name,
907
+ totals: {
908
+ input_tokens: Math.round(row.totals.input_tokens || 0),
909
+ cached_input_tokens: Math.round(row.totals.cached_input_tokens || 0),
910
+ cache_creation_input_tokens: Math.round(row.totals.cache_creation_input_tokens || 0),
911
+ output_tokens: Math.round(row.totals.output_tokens || 0),
912
+ reasoning_output_tokens: Math.round(row.totals.reasoning_output_tokens || 0),
913
+ total_tokens: Math.round(row.totals.total_tokens || 0),
914
+ },
915
+ }))
916
+ .sort((a, b) => (b.totals?.total_tokens || 0) - (a.totals?.total_tokens || 0)),
917
+ privacy: {
918
+ includes_content: false,
919
+ note: "Aggregated message token categories only; prompt and assistant text are never returned.",
920
+ },
921
+ };
922
+ }
923
+
924
+ // Lightweight on-disk count of static resources Claude Code's /context UI
925
+ // also surfaces (Skills, MCP servers, Memory files, Custom agents). These are
926
+ // counts of what's *installed*, not historical token usage — the same way
927
+ // /context shows "MCP tools 0 (115)" with the install count in parens. Lets
928
+ // the dashboard match that vocabulary even though token-level separation
929
+ // from the system prompt isn't possible from the rollout logs alone.
930
+ function countDirEntries(dir, predicate) {
931
+ let entries;
932
+ try {
933
+ entries = fssync.readdirSync(dir, { withFileTypes: true });
934
+ } catch (_e) {
935
+ return 0;
936
+ }
937
+ return entries.filter(predicate).length;
938
+ }
939
+
940
+ function fileExists(fp) {
941
+ try {
942
+ return fssync.statSync(fp).isFile();
943
+ } catch (_e) {
944
+ return false;
945
+ }
946
+ }
947
+
948
+ function safeReadJson(fp) {
949
+ try {
950
+ return JSON.parse(fssync.readFileSync(fp, "utf8"));
951
+ } catch (_e) {
952
+ return null;
953
+ }
954
+ }
955
+
956
+ // Walk @./path imports recursively. Claude Code expands @file references
957
+ // inside CLAUDE.md into separate memory entries; /context counts them.
958
+ function collectMemoryImports(filePath, seen) {
959
+ if (!filePath || seen.has(filePath)) return;
960
+ seen.add(filePath);
961
+ let raw;
962
+ try {
963
+ raw = fssync.readFileSync(filePath, "utf8");
964
+ } catch (_e) {
965
+ return;
966
+ }
967
+ const dir = path.dirname(filePath);
968
+ // Match `@path/to/file.md` (CC's import syntax), but skip `@user@host` and
969
+ // `email@host` patterns by requiring a path-like suffix.
970
+ const re = /(?:^|\s)@([./~][^\s)]+\.md)\b/g;
971
+ let m;
972
+ while ((m = re.exec(raw)) !== null) {
973
+ let target = m[1];
974
+ if (target.startsWith("~")) target = path.join(os.homedir(), target.slice(1).replace(/^\//, ""));
975
+ else if (!path.isAbsolute(target)) target = path.resolve(dir, target);
976
+ if (fileExists(target)) collectMemoryImports(target, seen);
977
+ }
978
+ }
979
+
980
+ function findLatestPluginVersionDir(pluginCacheRoot) {
981
+ let entries;
982
+ try {
983
+ entries = fssync.readdirSync(pluginCacheRoot, { withFileTypes: true });
984
+ } catch (_e) {
985
+ return null;
986
+ }
987
+ const dirs = entries.filter((e) => e.isDirectory()).map((e) => e.name);
988
+ if (dirs.length === 0) return null;
989
+ // Pick highest semver-ish lex sort fallback. CC keeps the active version
990
+ // path under the plugin's cache; if multiple versions linger, the lexically
991
+ // largest is usually the latest installed.
992
+ dirs.sort();
993
+ return path.join(pluginCacheRoot, dirs[dirs.length - 1]);
994
+ }
995
+
996
+ function countSkillsInDir(rootDir) {
997
+ // Walk subdirs once looking for SKILL.md / skill.md.
998
+ let count = 0;
999
+ const stack = [rootDir];
1000
+ while (stack.length > 0) {
1001
+ const dir = stack.pop();
1002
+ let entries;
1003
+ try {
1004
+ entries = fssync.readdirSync(dir, { withFileTypes: true });
1005
+ } catch (_e) {
1006
+ continue;
1007
+ }
1008
+ for (const e of entries) {
1009
+ if (!e.isDirectory()) continue;
1010
+ const sub = path.join(dir, e.name);
1011
+ if (fileExists(path.join(sub, "SKILL.md")) || fileExists(path.join(sub, "skill.md"))) {
1012
+ count += 1;
1013
+ } else {
1014
+ stack.push(sub);
1015
+ }
1016
+ }
1017
+ }
1018
+ return count;
1019
+ }
1020
+
1021
+ function countAgentMarkdowns(rootDir) {
1022
+ let count = 0;
1023
+ const stack = [rootDir];
1024
+ while (stack.length > 0) {
1025
+ const dir = stack.pop();
1026
+ let entries;
1027
+ try {
1028
+ entries = fssync.readdirSync(dir, { withFileTypes: true });
1029
+ } catch (_e) {
1030
+ continue;
1031
+ }
1032
+ for (const e of entries) {
1033
+ const fp = path.join(dir, e.name);
1034
+ if (e.isDirectory()) stack.push(fp);
1035
+ else if (e.isFile() && e.name.endsWith(".md")) count += 1;
1036
+ }
1037
+ }
1038
+ return count;
1039
+ }
1040
+
1041
+ function listEnabledPlugins() {
1042
+ const home = os.homedir();
1043
+ // settings.local.json overrides settings.json (CC's normal precedence).
1044
+ const baseMap = safeReadJson(path.join(home, ".claude", "settings.json"))?.enabledPlugins || {};
1045
+ const localMap = safeReadJson(path.join(home, ".claude", "settings.local.json"))?.enabledPlugins || {};
1046
+ const merged = { ...baseMap, ...localMap };
1047
+ return Object.entries(merged)
1048
+ .filter(([, enabled]) => enabled === true)
1049
+ .map(([key]) => key);
1050
+ }
1051
+
1052
+ function getConfiguredResources({ projectDir = null } = {}) {
1053
+ const home = os.homedir();
1054
+ const claudeRoot = path.join(home, ".claude");
1055
+ const cacheRoot = path.join(claudeRoot, "plugins", "cache");
1056
+
1057
+ // --- Skills ------------------------------------------------------------
1058
+ let skillsCount = countSkillsInDir(path.join(claudeRoot, "skills"));
1059
+ if (projectDir) {
1060
+ skillsCount += countSkillsInDir(path.join(projectDir, ".claude", "skills"));
1061
+ }
1062
+
1063
+ // --- Custom agents -----------------------------------------------------
1064
+ let agentsCount = countAgentMarkdowns(path.join(claudeRoot, "agents"));
1065
+ if (projectDir) {
1066
+ agentsCount += countAgentMarkdowns(path.join(projectDir, ".claude", "agents"));
1067
+ }
1068
+
1069
+ // --- MCP servers -------------------------------------------------------
1070
+ // Primary: ~/.claude.json (single dot-json — CC's main config), NOT
1071
+ // ~/.claude/settings.json (which holds GUI toggles, not MCP).
1072
+ let mcpCount = 0;
1073
+ const claudeJson = safeReadJson(path.join(home, ".claude.json"));
1074
+ if (claudeJson?.mcpServers && typeof claudeJson.mcpServers === "object") {
1075
+ mcpCount += Object.keys(claudeJson.mcpServers).length;
1076
+ }
1077
+ if (projectDir) {
1078
+ const projectMcp = safeReadJson(path.join(projectDir, ".mcp.json"));
1079
+ if (projectMcp?.mcpServers && typeof projectMcp.mcpServers === "object") {
1080
+ mcpCount += Object.keys(projectMcp.mcpServers).length;
1081
+ }
1082
+ }
1083
+
1084
+ // --- Plugin contributions (enabled plugins only) -----------------------
1085
+ // Plugin caches live at ~/.claude/plugins/cache/<owner>/<plugin>/<version>/
1086
+ // and contribute skills, agents, and mcpServers (declared in plugin.json).
1087
+ for (const pluginKey of listEnabledPlugins()) {
1088
+ // pluginKey is "name@marketplace" (e.g., "claude-mem@thedotmack").
1089
+ const [name, marketplace] = pluginKey.split("@");
1090
+ if (!name || !marketplace) continue;
1091
+ const pluginRoot = path.join(cacheRoot, marketplace, name);
1092
+ const versionDir = findLatestPluginVersionDir(pluginRoot);
1093
+ if (!versionDir) continue;
1094
+ skillsCount += countSkillsInDir(path.join(versionDir, "skills"));
1095
+ agentsCount += countAgentMarkdowns(path.join(versionDir, "agents"));
1096
+ const pluginManifest = safeReadJson(path.join(versionDir, ".claude-plugin", "plugin.json"));
1097
+ if (pluginManifest?.mcpServers && typeof pluginManifest.mcpServers === "object") {
1098
+ mcpCount += Object.keys(pluginManifest.mcpServers).length;
1099
+ }
1100
+ }
1101
+
1102
+ // --- Memory files (CLAUDE.md + transitive @-imports) -------------------
1103
+ const memorySeen = new Set();
1104
+ const userMd = path.join(claudeRoot, "CLAUDE.md");
1105
+ const homeMd = path.join(home, "CLAUDE.md");
1106
+ if (fileExists(userMd)) collectMemoryImports(userMd, memorySeen);
1107
+ if (fileExists(homeMd) && fssync.statSync(homeMd).size > 0) collectMemoryImports(homeMd, memorySeen);
1108
+ // Walk up from projectDir to find the closest CLAUDE.md (CC walks up too).
1109
+ // Handles dev servers running from a subdir (e.g. vite from dashboard/).
1110
+ if (projectDir) {
1111
+ let cursor = projectDir;
1112
+ for (let i = 0; i < 8; i++) {
1113
+ const candidate = path.join(cursor, "CLAUDE.md");
1114
+ if (fileExists(candidate)) {
1115
+ collectMemoryImports(candidate, memorySeen);
1116
+ break;
1117
+ }
1118
+ const parent = path.dirname(cursor);
1119
+ if (parent === cursor) break;
1120
+ cursor = parent;
1121
+ }
1122
+ }
1123
+
1124
+ return {
1125
+ skills_count: skillsCount,
1126
+ custom_agents_count: agentsCount,
1127
+ memory_files_count: memorySeen.size,
1128
+ mcp_servers_count: mcpCount,
1129
+ };
1130
+ }
1131
+
1132
+ function unsupportedSourcePayload(source) {
1133
+ return {
1134
+ source,
1135
+ scope: "unsupported",
1136
+ totals: emptyTotals(),
1137
+ categories: CATEGORY_KEYS.map((key) => ({
1138
+ key,
1139
+ totals: emptyTotals(),
1140
+ percent: 0,
1141
+ })),
1142
+ session_count: 0,
1143
+ message_count: 0,
1144
+ };
1145
+ }
1146
+
1147
+ // ---------------------------------------------------------------------------
1148
+ // Ground-truth bucket aggregator for queue.jsonl repair.
1149
+ //
1150
+ // `sync.js` historically used a stateful incremental pipeline (cursor offsets
1151
+ // + persisted hash set) and the `reincludeClaudeMemObserverFiles` migration
1152
+ // shipped 3 versions, each of which reset the hash set and re-read observer
1153
+ // jsonls. Result: queue.jsonl ended up with ~+40% extra Claude tokens that
1154
+ // never actually existed.
1155
+ //
1156
+ // This function is the source-of-truth replacement: scan every Claude jsonl,
1157
+ // dedup messages by (msgId, requestId) globally — same algorithm ccusage
1158
+ // uses — and emit one record per (model, hour_start) bucket. Callers (sync's
1159
+ // repair migration) write these as authoritative rows to queue.jsonl,
1160
+ // overwriting whatever was there for source=claude.
1161
+ // ---------------------------------------------------------------------------
1162
+
1163
+ function bucketAccumulator() {
1164
+ return {
1165
+ input_tokens: 0,
1166
+ cached_input_tokens: 0,
1167
+ cache_creation_input_tokens: 0,
1168
+ output_tokens: 0,
1169
+ reasoning_output_tokens: 0,
1170
+ total_tokens: 0,
1171
+ conversation_count: 0,
1172
+ };
1173
+ }
1174
+
1175
+ function toUtcHalfHourStart(ts) {
1176
+ const dt = new Date(ts);
1177
+ if (!Number.isFinite(dt.getTime())) return null;
1178
+ const minutes = dt.getUTCMinutes();
1179
+ const halfMinute = minutes >= 30 ? 30 : 0;
1180
+ return new Date(
1181
+ Date.UTC(
1182
+ dt.getUTCFullYear(),
1183
+ dt.getUTCMonth(),
1184
+ dt.getUTCDate(),
1185
+ dt.getUTCHours(),
1186
+ halfMinute,
1187
+ 0,
1188
+ 0,
1189
+ ),
1190
+ ).toISOString();
1191
+ }
1192
+
1193
+ async function computeClaudeGroundTruthBuckets({ rootDir = null, rootDirs = null } = {}) {
1194
+ // Multi-root (#307): a Windows host may scan a native ~/.claude/projects
1195
+ // plus a WSL install over \\wsl$. `rootDirs` wins over the legacy single
1196
+ // `rootDir`; duplicated session files synced between environments collapse
1197
+ // via the msgId+reqId dedup below.
1198
+ const roots = Array.isArray(rootDirs) && rootDirs.length > 0
1199
+ ? rootDirs
1200
+ : [rootDir || defaultClaudeProjectsDir()];
1201
+ const files = [];
1202
+ const seenFiles = new Set();
1203
+ for (const root of roots) {
1204
+ for (const fp of listSessionFiles(root)) {
1205
+ if (!seenFiles.has(fp)) {
1206
+ seenFiles.add(fp);
1207
+ files.push(fp);
1208
+ }
1209
+ }
1210
+ }
1211
+ const buckets = new Map(); // `${model}|${hourStart}` → totals
1212
+ const seenHashes = new Set();
1213
+ const userMessageBuckets = new Map(); // for conversation_count tracking
1214
+ const forkAliases = await collectClaudeForkAliases(files);
1215
+
1216
+ for (const fp of files) {
1217
+ let stream;
1218
+ try {
1219
+ stream = fssync.createReadStream(fp, { encoding: "utf8" });
1220
+ } catch (_e) {
1221
+ continue;
1222
+ }
1223
+ const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
1224
+ // Separator-agnostic: WSL roots are UNC paths where path.join produces
1225
+ // backslashes (mirrors the same check in rollout.js parseClaudeFile).
1226
+ const isMainSession = !/[\\/]subagents[\\/]/.test(fp);
1227
+
1228
+ for await (const line of rl) {
1229
+ if (!line) continue;
1230
+
1231
+ // Conversation count = main-session user messages with text content
1232
+ // (matches what parseClaudeFile in rollout.js does).
1233
+ if (isMainSession && line.includes('"type":"user"')) {
1234
+ let userObj;
1235
+ try {
1236
+ userObj = JSON.parse(line);
1237
+ } catch (_e) {
1238
+ /* skip */
1239
+ }
1240
+ if (userObj?.type === "user") {
1241
+ const content = userObj?.message?.content;
1242
+ const hasText =
1243
+ typeof content === "string" ||
1244
+ (Array.isArray(content) && content.some((b) => b?.type === "text"));
1245
+ if (hasText) {
1246
+ const userKey = claudeUserIdentity(userObj, forkAliases);
1247
+ if (!userKey || !seenHashes.has(userKey)) {
1248
+ if (userKey) seenHashes.add(userKey);
1249
+ const ts = typeof userObj?.timestamp === "string" ? userObj.timestamp : null;
1250
+ const hourStart = ts ? toUtcHalfHourStart(ts) : null;
1251
+ if (hourStart) {
1252
+ const k = `unknown|${hourStart}`;
1253
+ userMessageBuckets.set(k, (userMessageBuckets.get(k) || 0) + 1);
1254
+ }
1255
+ }
1256
+ }
1257
+ }
1258
+ }
1259
+
1260
+ if (!line.includes('"usage"')) continue;
1261
+ let obj;
1262
+ try {
1263
+ obj = JSON.parse(line);
1264
+ } catch (_e) {
1265
+ continue;
1266
+ }
1267
+ const usage = obj?.message?.usage;
1268
+ const totals = readClaudeUsageTotals(usage);
1269
+ if (!totals || !hasNonZeroClaudeUsage(usage)) continue;
1270
+
1271
+ const hash = claudeMessageDedupKey(obj);
1272
+ if (hash) {
1273
+ if (seenHashes.has(hash)) continue;
1274
+ seenHashes.add(hash);
1275
+ }
1276
+
1277
+ const model = (obj?.message?.model || obj?.model || "unknown").trim() || "unknown";
1278
+ const ts = typeof obj?.timestamp === "string" ? obj.timestamp : null;
1279
+ const hourStart = ts ? toUtcHalfHourStart(ts) : null;
1280
+ if (!hourStart) continue;
1281
+
1282
+ const key = `${model}|${hourStart}`;
1283
+ let acc = buckets.get(key);
1284
+ if (!acc) {
1285
+ acc = bucketAccumulator();
1286
+ buckets.set(key, acc);
1287
+ }
1288
+ acc.input_tokens += totals.input_tokens;
1289
+ acc.cached_input_tokens += totals.cached_input_tokens;
1290
+ acc.cache_creation_input_tokens += totals.cache_creation_input_tokens;
1291
+ acc.output_tokens += totals.output_tokens;
1292
+ acc.reasoning_output_tokens += totals.reasoning_output_tokens;
1293
+ acc.total_tokens += totals.total_tokens;
1294
+ }
1295
+ rl.close();
1296
+ stream.close?.();
1297
+ }
1298
+
1299
+ // Stitch user-message conversation counts onto the unknown-model bucket
1300
+ // for the same hour (matches rollout.js behavior — user messages are
1301
+ // counted under DEFAULT_MODEL because they have no model field).
1302
+ for (const [key, count] of userMessageBuckets) {
1303
+ let acc = buckets.get(key);
1304
+ if (!acc) {
1305
+ acc = bucketAccumulator();
1306
+ buckets.set(key, acc);
1307
+ }
1308
+ acc.conversation_count += count;
1309
+ }
1310
+
1311
+ const out = [];
1312
+ for (const [key, totals] of buckets) {
1313
+ const sep = key.indexOf("|");
1314
+ const model = key.slice(0, sep);
1315
+ const hourStart = key.slice(sep + 1);
1316
+ out.push({
1317
+ source: "claude",
1318
+ model,
1319
+ hour_start: hourStart,
1320
+ ...totals,
1321
+ billable_total_tokens: totals.total_tokens,
1322
+ });
1323
+ }
1324
+ return {
1325
+ rows: out,
1326
+ seenHashes: Array.from(seenHashes),
1327
+ forkAliases,
1328
+ fileList: files,
1329
+ };
1330
+ }
1331
+
1332
+ module.exports = {
1333
+ CATEGORY_KEYS,
1334
+ computeClaudeCategoryBreakdown,
1335
+ computeClaudeGroundTruthBuckets,
1336
+ unsupportedSourcePayload,
1337
+ getConfiguredResources,
1338
+ // Exported for tests
1339
+ splitOutputByContent,
1340
+ classifyOneMessage,
1341
+ emptyTotals,
1342
+ emptyCategoryMap,
1343
+ };