tokenusage-cli 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/CHANGELOG.md +39 -19
  2. package/LICENSE +22 -22
  3. package/README.md +94 -94
  4. package/THIRD_PARTY_NOTICES.md +140 -140
  5. package/bin/tokenusage.js +69 -69
  6. package/package.json +59 -59
  7. package/src/cli.js +146 -134
  8. package/src/commands/device-login.js +205 -205
  9. package/src/commands/diagnostics.js +38 -38
  10. package/src/commands/doctor.js +110 -110
  11. package/src/commands/init.js +2484 -2436
  12. package/src/commands/pause.js +77 -0
  13. package/src/commands/sessions.js +29 -29
  14. package/src/commands/status.js +1758 -1747
  15. package/src/commands/sync.js +8048 -7934
  16. package/src/commands/uninstall.js +322 -312
  17. package/src/lib/account-usage-dedup.js +267 -267
  18. package/src/lib/antigravity-paths.js +48 -48
  19. package/src/lib/ark-agent-plan-limits.js +438 -438
  20. package/src/lib/ark-coding-plan-limits.js +537 -537
  21. package/src/lib/async-queue-reader.js +138 -138
  22. package/src/lib/atomic-json.js +27 -27
  23. package/src/lib/bearer-token.js +11 -11
  24. package/src/lib/categorizer-utils.js +232 -232
  25. package/src/lib/claude-categorizer.js +1343 -1343
  26. package/src/lib/claude-config.js +279 -279
  27. package/src/lib/claude-user-identity.js +56 -56
  28. package/src/lib/cli-ui.js +179 -179
  29. package/src/lib/cloud-account.js +541 -541
  30. package/src/lib/cloud-device-token.js +141 -141
  31. package/src/lib/cloud-sync-prefs.js +14 -14
  32. package/src/lib/cloud-usage-reset.js +115 -115
  33. package/src/lib/codex-config.js +595 -595
  34. package/src/lib/codex-context-breakdown.js +806 -806
  35. package/src/lib/codex-model-attribution.js +128 -128
  36. package/src/lib/codex-rollout-parser.js +1471 -1471
  37. package/src/lib/codex-service-tier.js +49 -49
  38. package/src/lib/codex-token-refresh.js +140 -140
  39. package/src/lib/codex-token-usage.js +195 -195
  40. package/src/lib/codex-usage-record.js +195 -195
  41. package/src/lib/collection-pause.js +77 -0
  42. package/src/lib/command-runner.js +313 -313
  43. package/src/lib/commandcode-limits.js +317 -317
  44. package/src/lib/context-health.js +124 -124
  45. package/src/lib/cursor-config.js +605 -605
  46. package/src/lib/cursor-store.js +1918 -1918
  47. package/src/lib/debug-flags.js +7 -7
  48. package/src/lib/devin-limits.js +249 -249
  49. package/src/lib/devin-usage.js +212 -212
  50. package/src/lib/diagnostics.js +383 -383
  51. package/src/lib/doctor.js +385 -385
  52. package/src/lib/dumate.js +95 -95
  53. package/src/lib/fs.js +455 -455
  54. package/src/lib/function-url.js +14 -14
  55. package/src/lib/gemini-config.js +291 -291
  56. package/src/lib/git-outcomes.js +243 -243
  57. package/src/lib/grok-context-breakdown.js +890 -890
  58. package/src/lib/grok-hook.js +360 -360
  59. package/src/lib/grok-limits.js +865 -865
  60. package/src/lib/grok-usage.js +98 -98
  61. package/src/lib/heatmap-compact.js +114 -114
  62. package/src/lib/ingest-sanitize.js +41 -41
  63. package/src/lib/init-flow.js +42 -42
  64. package/src/lib/install-resolver.js +170 -170
  65. package/src/lib/jsonl-lines.js +134 -134
  66. package/src/lib/kimi-model-repair.js +173 -173
  67. package/src/lib/kimi-profile.js +61 -61
  68. package/src/lib/kimi-session.js +216 -216
  69. package/src/lib/local-api.js +3588 -3588
  70. package/src/lib/machine-id.js +238 -238
  71. package/src/lib/multi-install-parser.js +113 -113
  72. package/src/lib/notify-command.js +38 -38
  73. package/src/lib/omp-hook.js +589 -589
  74. package/src/lib/openclaw-cli.js +104 -104
  75. package/src/lib/openclaw-hook.js +375 -375
  76. package/src/lib/openclaw-session-plugin.js +560 -560
  77. package/src/lib/opencode-config.js +121 -121
  78. package/src/lib/opencode-go-limits.js +807 -807
  79. package/src/lib/outcomes-engine.js +253 -253
  80. package/src/lib/passive-mode.js +219 -219
  81. package/src/lib/pet-packages.js +722 -722
  82. package/src/lib/pricing/curated-overrides.json +822 -822
  83. package/src/lib/pricing/index.js +379 -379
  84. package/src/lib/pricing/litellm-fetcher.js +172 -172
  85. package/src/lib/pricing/matcher.js +456 -456
  86. package/src/lib/pricing/seed-snapshot.json +1 -1
  87. package/src/lib/progress.js +76 -76
  88. package/src/lib/project-usage-purge.js +117 -117
  89. package/src/lib/prompt.js +24 -24
  90. package/src/lib/provider-status.js +104 -104
  91. package/src/lib/proxy-env.js +489 -489
  92. package/src/lib/proxy-settings.js +163 -163
  93. package/src/lib/qoder-cn-cli-limits.js +119 -119
  94. package/src/lib/qoder-limits.js +880 -880
  95. package/src/lib/quiet-warnings.js +26 -26
  96. package/src/lib/rollout.js +26005 -25966
  97. package/src/lib/runtime-config.js +9 -4
  98. package/src/lib/scan-roots.js +277 -277
  99. package/src/lib/scheduled-task.js +145 -0
  100. package/src/lib/sentry.js +231 -231
  101. package/src/lib/session-analytics.js +2972 -2972
  102. package/src/lib/session-performance.js +237 -237
  103. package/src/lib/session-upload.js +379 -379
  104. package/src/lib/skill-usage.js +267 -267
  105. package/src/lib/skills-manager.js +1591 -1591
  106. package/src/lib/source-metadata.js +48 -48
  107. package/src/lib/sqlite-reader.js +215 -215
  108. package/src/lib/star-cta.js +73 -73
  109. package/src/lib/subscription-manager.js +293 -293
  110. package/src/lib/subscriptions.js +508 -508
  111. package/src/lib/sync-report.js +138 -0
  112. package/src/lib/telemetry.js +193 -193
  113. package/src/lib/tracker-paths.js +16 -16
  114. package/src/lib/trae-cn-config.js +548 -548
  115. package/src/lib/trae-db.js +127 -127
  116. package/src/lib/trae-snapshot.js +230 -230
  117. package/src/lib/trae-sqlite.js +143 -143
  118. package/src/lib/trae-usage.js +121 -121
  119. package/src/lib/upload-queue.js +134 -134
  120. package/src/lib/upload-throttle.js +171 -171
  121. package/src/lib/usage-limits.js +4552 -4552
  122. package/src/lib/wrapped-aggregator.js +236 -236
  123. package/src/lib/wsl-probe.js +281 -281
  124. package/src/lib/zcode-limits.js +1172 -1172
  125. package/src/lib/zcode-reset-credits.js +66 -66
  126. package/test/audit-fixes.test.js +200 -0
  127. package/test/cli-smoke.test.js +53 -53
  128. package/test/collection-controls.test.js +181 -0
  129. package/test/ingest-sanitize.test.js +162 -162
  130. package/test/quiet-warnings.test.js +53 -53
  131. package/test/session-upload.test.js +427 -427
@@ -1,212 +1,212 @@
1
- "use strict";
2
-
3
- // Devin CLI (Cognition — devin.ai) local history projection.
4
- //
5
- // Devin persists chat history in a SQLite database at
6
- // `$XDG_DATA_HOME/devin/cli/sessions.db` (default
7
- // `~/.local/share/devin/cli/sessions.db`). Token usage lives on assistant
8
- // message nodes inside the `chat_message` JSON document. The SQL below is a
9
- // deliberately narrow projection: request identity, generation model, the
10
- // original generation timestamps, the numeric usage metrics, and the owning
11
- // session's `working_directory`/`created_at` for canonical ordering and local
12
- // project attribution. Message bodies, prompt text, node `metadata`,
13
- // `sessions.cogs_json`, and any credential-bearing state are never selected.
14
- //
15
- // Verified against devin CLI 3000.10.21 (611c1cba): replay/fork/compaction
16
- // produces multiple `message_nodes` rows that share one
17
- // `metadata.request_id` with identical metrics and original timestamps, so
18
- // `request_id` — never `row_id` or `(session_id, request_id)` — is the
19
- // canonical usage identity across the whole local install.
20
- //
21
- // Token components are disjoint: the DB's `metrics.input_tokens` already
22
- // excludes `cache_read_tokens` and `cache_creation_tokens`, so each reported
23
- // counter is added once and caches are never subtracted from input.
24
-
25
- const DEVIN_TABLE_PROBE_SQL =
26
- "SELECT name FROM sqlite_master WHERE type='table' AND name IN ('message_nodes','sessions')";
27
-
28
- const metricField = (jsonPath, alias) =>
29
- `CASE WHEN json_valid(n.chat_message) THEN json_extract(n.chat_message, '${jsonPath}') ELSE NULL END AS ${alias}`;
30
-
31
- // `sessions` is LEFT JOINed only for `working_directory` and `created_at`;
32
- // older databases may not have the table, in which case the projection runs
33
- // without it (project attribution and session-age ordering degrade to nulls).
34
- function devinUsageSql({ hasSessionsTable = true } = {}) {
35
- const join = hasSessionsTable ? "LEFT JOIN sessions s ON s.id = n.session_id" : "";
36
- const sessionCreatedAt = hasSessionsTable ? "s.created_at" : "NULL";
37
- const workingDirectory = hasSessionsTable ? "s.working_directory" : "NULL";
38
- return `
39
- SELECT
40
- n.row_id AS row_id,
41
- n.session_id AS session_id,
42
- ${sessionCreatedAt} AS session_created_at,
43
- ${workingDirectory} AS working_directory,
44
- ${metricField("$.metadata.request_id", "request_id")},
45
- ${metricField("$.metadata.generation_model", "generation_model")},
46
- ${metricField("$.metadata.started_generation_at", "started_generation_at")},
47
- ${metricField("$.metadata.created_at", "message_created_at")},
48
- ${metricField("$.metadata.metrics.input_tokens", "input_tokens")},
49
- ${metricField("$.metadata.metrics.output_tokens", "output_tokens")},
50
- ${metricField("$.metadata.metrics.cache_read_tokens", "cache_read_tokens")},
51
- ${metricField("$.metadata.metrics.cache_creation_tokens", "cache_creation_tokens")}
52
- FROM message_nodes n
53
- ${join}
54
- WHERE json_valid(n.chat_message)
55
- AND json_extract(n.chat_message, '$.role') = 'assistant'
56
- AND json_extract(n.chat_message, '$.metadata.request_id') IS NOT NULL
57
- ORDER BY n.row_id
58
- `.trim();
59
- }
60
-
61
- function normalizeText(value) {
62
- if (typeof value !== "string") return null;
63
- const trimmed = value.trim();
64
- return trimmed.length > 0 ? trimmed : null;
65
- }
66
-
67
- // Strict integer check: usage fields must be finite non-negative safe
68
- // integers. `null`/`undefined` cache counters are legal (no reported cache
69
- // contribution) — callers pass them through `optionalCount`; required
70
- // counters go through `requiredCount`.
71
- function toSafeCount(value) {
72
- const n = Number(value);
73
- return Number.isSafeInteger(n) && n >= 0 ? n : null;
74
- }
75
-
76
- function requiredCount(value) {
77
- // A missing/NULL required counter is incomplete usage, not zero — pending
78
- // assistant records carry no metrics block and must stay uncounted until
79
- // the writer finalizes them.
80
- if (value === null || value === undefined) return null;
81
- return toSafeCount(value);
82
- }
83
-
84
- function optionalCount(value) {
85
- if (value === null || value === undefined) return 0;
86
- return toSafeCount(value);
87
- }
88
-
89
- // RFC3339 timestamps arrive with both `Z` and `+00:00` spellings; Date.parse
90
- // normalizes both to the same instant. Returns epoch ms or 0 when missing or
91
- // unparsable.
92
- function parseDevinTimestampMs(value) {
93
- if (typeof value !== "string" || !value.trim()) return 0;
94
- const ms = Date.parse(value.trim());
95
- return Number.isFinite(ms) && ms > 0 ? ms : 0;
96
- }
97
-
98
- // session.created_at is Unix seconds; node row ordering uses ms.
99
- function epochSecondsToMs(value) {
100
- const n = Number(value);
101
- return Number.isFinite(n) && n > 0 ? Math.floor(n) * 1000 : null;
102
- }
103
-
104
- // Normalize one projected row into a canonical candidate, or null when the
105
- // record is pending/malformed: missing request identity, missing/invalid
106
- // numeric usage, or no usable original timestamp. Such rows are re-evaluated
107
- // on every scan, so a record that gains metrics or a timestamp later is
108
- // counted then.
109
- function normalizeDevinUsageRow(row) {
110
- const requestId = normalizeText(row?.request_id);
111
- if (!requestId) return null;
112
-
113
- const input = requiredCount(row?.input_tokens);
114
- const output = requiredCount(row?.output_tokens);
115
- const cacheRead = optionalCount(row?.cache_read_tokens);
116
- const cacheCreation = optionalCount(row?.cache_creation_tokens);
117
- if (input === null || output === null || cacheRead === null || cacheCreation === null) {
118
- return null;
119
- }
120
-
121
- // The original generation time decides the bucket; the node insertion time
122
- // (n.created_at) is clone/replay insertion time and is never a fallback.
123
- const tsMs =
124
- parseDevinTimestampMs(row?.started_generation_at) ||
125
- parseDevinTimestampMs(row?.message_created_at);
126
- if (!tsMs) return null;
127
-
128
- return {
129
- requestId,
130
- sessionId: normalizeText(row?.session_id) || "",
131
- rowId: toSafeCount(row?.row_id) ?? 0,
132
- sessionCreatedAtMs: epochSecondsToMs(row?.session_created_at),
133
- workingDirectory: normalizeText(row?.working_directory),
134
- model: normalizeText(row?.generation_model) || "unknown",
135
- tsMs,
136
- input,
137
- output,
138
- cacheRead,
139
- cacheCreation,
140
- };
141
- }
142
-
143
- // Pick the canonical retained record for one request_id. Copies created by
144
- // replay/fork/compaction carry identical original timestamps and metrics, so
145
- // the order is: earliest original generation time, then the earliest-created
146
- // owning session (a fork's copy lives in a younger session), then session id,
147
- // then newest row first so an in-place correction appended as a fresh node
148
- // inside the same session wins over the stale sibling.
149
- function compareCandidates(a, b) {
150
- if (a.tsMs !== b.tsMs) return a.tsMs - b.tsMs;
151
- const sessionA = a.sessionCreatedAtMs ?? Number.POSITIVE_INFINITY;
152
- const sessionB = b.sessionCreatedAtMs ?? Number.POSITIVE_INFINITY;
153
- if (sessionA !== sessionB) return sessionA - sessionB;
154
- if (a.sessionId !== b.sessionId) return a.sessionId < b.sessionId ? -1 : 1;
155
- return b.rowId - a.rowId;
156
- }
157
-
158
- // Build the deduplicated per-request event list. Conversation ownership and
159
- // session/project attribution are decided by the persisted request ledger in
160
- // parseDevinIncremental — the projection only reports each request's
161
- // best-evidence retained record; `conversation_count` stays 0 here and is
162
- // filled from ledger state at reconcile time.
163
- function buildDevinUsageEvents(rows) {
164
- const byRequest = new Map();
165
- let rowsSeen = 0;
166
- let rowsSkipped = 0;
167
- for (const row of Array.isArray(rows) ? rows : []) {
168
- rowsSeen += 1;
169
- const candidate = normalizeDevinUsageRow(row);
170
- if (!candidate) {
171
- rowsSkipped += 1;
172
- continue;
173
- }
174
- const list = byRequest.get(candidate.requestId);
175
- if (list) list.push(candidate);
176
- else byRequest.set(candidate.requestId, [candidate]);
177
- }
178
-
179
- const events = [];
180
- for (const [requestId, candidates] of byRequest) {
181
- candidates.sort(compareCandidates);
182
- const canonical = candidates[0];
183
- const total = canonical.input + canonical.output + canonical.cacheRead + canonical.cacheCreation;
184
- events.push({
185
- requestId,
186
- sessionId: canonical.sessionId,
187
- workingDirectory: canonical.workingDirectory,
188
- model: canonical.model,
189
- tsMs: canonical.tsMs,
190
- totals: {
191
- input_tokens: canonical.input,
192
- cached_input_tokens: canonical.cacheRead,
193
- cache_creation_input_tokens: canonical.cacheCreation,
194
- output_tokens: canonical.output,
195
- reasoning_output_tokens: 0,
196
- total_tokens: total,
197
- billable_total_tokens: total,
198
- total_cost_usd: 0,
199
- conversation_count: 0,
200
- },
201
- });
202
- }
203
- events.sort((a, b) => a.tsMs - b.tsMs || (a.requestId < b.requestId ? -1 : 1));
204
-
205
- return { events, rowsSeen, rowsSkipped };
206
- }
207
-
208
- module.exports = {
209
- DEVIN_TABLE_PROBE_SQL,
210
- devinUsageSql,
211
- buildDevinUsageEvents,
212
- };
1
+ "use strict";
2
+
3
+ // Devin CLI (Cognition — devin.ai) local history projection.
4
+ //
5
+ // Devin persists chat history in a SQLite database at
6
+ // `$XDG_DATA_HOME/devin/cli/sessions.db` (default
7
+ // `~/.local/share/devin/cli/sessions.db`). Token usage lives on assistant
8
+ // message nodes inside the `chat_message` JSON document. The SQL below is a
9
+ // deliberately narrow projection: request identity, generation model, the
10
+ // original generation timestamps, the numeric usage metrics, and the owning
11
+ // session's `working_directory`/`created_at` for canonical ordering and local
12
+ // project attribution. Message bodies, prompt text, node `metadata`,
13
+ // `sessions.cogs_json`, and any credential-bearing state are never selected.
14
+ //
15
+ // Verified against devin CLI 3000.10.21 (611c1cba): replay/fork/compaction
16
+ // produces multiple `message_nodes` rows that share one
17
+ // `metadata.request_id` with identical metrics and original timestamps, so
18
+ // `request_id` — never `row_id` or `(session_id, request_id)` — is the
19
+ // canonical usage identity across the whole local install.
20
+ //
21
+ // Token components are disjoint: the DB's `metrics.input_tokens` already
22
+ // excludes `cache_read_tokens` and `cache_creation_tokens`, so each reported
23
+ // counter is added once and caches are never subtracted from input.
24
+
25
+ const DEVIN_TABLE_PROBE_SQL =
26
+ "SELECT name FROM sqlite_master WHERE type='table' AND name IN ('message_nodes','sessions')";
27
+
28
+ const metricField = (jsonPath, alias) =>
29
+ `CASE WHEN json_valid(n.chat_message) THEN json_extract(n.chat_message, '${jsonPath}') ELSE NULL END AS ${alias}`;
30
+
31
+ // `sessions` is LEFT JOINed only for `working_directory` and `created_at`;
32
+ // older databases may not have the table, in which case the projection runs
33
+ // without it (project attribution and session-age ordering degrade to nulls).
34
+ function devinUsageSql({ hasSessionsTable = true } = {}) {
35
+ const join = hasSessionsTable ? "LEFT JOIN sessions s ON s.id = n.session_id" : "";
36
+ const sessionCreatedAt = hasSessionsTable ? "s.created_at" : "NULL";
37
+ const workingDirectory = hasSessionsTable ? "s.working_directory" : "NULL";
38
+ return `
39
+ SELECT
40
+ n.row_id AS row_id,
41
+ n.session_id AS session_id,
42
+ ${sessionCreatedAt} AS session_created_at,
43
+ ${workingDirectory} AS working_directory,
44
+ ${metricField("$.metadata.request_id", "request_id")},
45
+ ${metricField("$.metadata.generation_model", "generation_model")},
46
+ ${metricField("$.metadata.started_generation_at", "started_generation_at")},
47
+ ${metricField("$.metadata.created_at", "message_created_at")},
48
+ ${metricField("$.metadata.metrics.input_tokens", "input_tokens")},
49
+ ${metricField("$.metadata.metrics.output_tokens", "output_tokens")},
50
+ ${metricField("$.metadata.metrics.cache_read_tokens", "cache_read_tokens")},
51
+ ${metricField("$.metadata.metrics.cache_creation_tokens", "cache_creation_tokens")}
52
+ FROM message_nodes n
53
+ ${join}
54
+ WHERE json_valid(n.chat_message)
55
+ AND json_extract(n.chat_message, '$.role') = 'assistant'
56
+ AND json_extract(n.chat_message, '$.metadata.request_id') IS NOT NULL
57
+ ORDER BY n.row_id
58
+ `.trim();
59
+ }
60
+
61
+ function normalizeText(value) {
62
+ if (typeof value !== "string") return null;
63
+ const trimmed = value.trim();
64
+ return trimmed.length > 0 ? trimmed : null;
65
+ }
66
+
67
+ // Strict integer check: usage fields must be finite non-negative safe
68
+ // integers. `null`/`undefined` cache counters are legal (no reported cache
69
+ // contribution) — callers pass them through `optionalCount`; required
70
+ // counters go through `requiredCount`.
71
+ function toSafeCount(value) {
72
+ const n = Number(value);
73
+ return Number.isSafeInteger(n) && n >= 0 ? n : null;
74
+ }
75
+
76
+ function requiredCount(value) {
77
+ // A missing/NULL required counter is incomplete usage, not zero — pending
78
+ // assistant records carry no metrics block and must stay uncounted until
79
+ // the writer finalizes them.
80
+ if (value === null || value === undefined) return null;
81
+ return toSafeCount(value);
82
+ }
83
+
84
+ function optionalCount(value) {
85
+ if (value === null || value === undefined) return 0;
86
+ return toSafeCount(value);
87
+ }
88
+
89
+ // RFC3339 timestamps arrive with both `Z` and `+00:00` spellings; Date.parse
90
+ // normalizes both to the same instant. Returns epoch ms or 0 when missing or
91
+ // unparsable.
92
+ function parseDevinTimestampMs(value) {
93
+ if (typeof value !== "string" || !value.trim()) return 0;
94
+ const ms = Date.parse(value.trim());
95
+ return Number.isFinite(ms) && ms > 0 ? ms : 0;
96
+ }
97
+
98
+ // session.created_at is Unix seconds; node row ordering uses ms.
99
+ function epochSecondsToMs(value) {
100
+ const n = Number(value);
101
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) * 1000 : null;
102
+ }
103
+
104
+ // Normalize one projected row into a canonical candidate, or null when the
105
+ // record is pending/malformed: missing request identity, missing/invalid
106
+ // numeric usage, or no usable original timestamp. Such rows are re-evaluated
107
+ // on every scan, so a record that gains metrics or a timestamp later is
108
+ // counted then.
109
+ function normalizeDevinUsageRow(row) {
110
+ const requestId = normalizeText(row?.request_id);
111
+ if (!requestId) return null;
112
+
113
+ const input = requiredCount(row?.input_tokens);
114
+ const output = requiredCount(row?.output_tokens);
115
+ const cacheRead = optionalCount(row?.cache_read_tokens);
116
+ const cacheCreation = optionalCount(row?.cache_creation_tokens);
117
+ if (input === null || output === null || cacheRead === null || cacheCreation === null) {
118
+ return null;
119
+ }
120
+
121
+ // The original generation time decides the bucket; the node insertion time
122
+ // (n.created_at) is clone/replay insertion time and is never a fallback.
123
+ const tsMs =
124
+ parseDevinTimestampMs(row?.started_generation_at) ||
125
+ parseDevinTimestampMs(row?.message_created_at);
126
+ if (!tsMs) return null;
127
+
128
+ return {
129
+ requestId,
130
+ sessionId: normalizeText(row?.session_id) || "",
131
+ rowId: toSafeCount(row?.row_id) ?? 0,
132
+ sessionCreatedAtMs: epochSecondsToMs(row?.session_created_at),
133
+ workingDirectory: normalizeText(row?.working_directory),
134
+ model: normalizeText(row?.generation_model) || "unknown",
135
+ tsMs,
136
+ input,
137
+ output,
138
+ cacheRead,
139
+ cacheCreation,
140
+ };
141
+ }
142
+
143
+ // Pick the canonical retained record for one request_id. Copies created by
144
+ // replay/fork/compaction carry identical original timestamps and metrics, so
145
+ // the order is: earliest original generation time, then the earliest-created
146
+ // owning session (a fork's copy lives in a younger session), then session id,
147
+ // then newest row first so an in-place correction appended as a fresh node
148
+ // inside the same session wins over the stale sibling.
149
+ function compareCandidates(a, b) {
150
+ if (a.tsMs !== b.tsMs) return a.tsMs - b.tsMs;
151
+ const sessionA = a.sessionCreatedAtMs ?? Number.POSITIVE_INFINITY;
152
+ const sessionB = b.sessionCreatedAtMs ?? Number.POSITIVE_INFINITY;
153
+ if (sessionA !== sessionB) return sessionA - sessionB;
154
+ if (a.sessionId !== b.sessionId) return a.sessionId < b.sessionId ? -1 : 1;
155
+ return b.rowId - a.rowId;
156
+ }
157
+
158
+ // Build the deduplicated per-request event list. Conversation ownership and
159
+ // session/project attribution are decided by the persisted request ledger in
160
+ // parseDevinIncremental — the projection only reports each request's
161
+ // best-evidence retained record; `conversation_count` stays 0 here and is
162
+ // filled from ledger state at reconcile time.
163
+ function buildDevinUsageEvents(rows) {
164
+ const byRequest = new Map();
165
+ let rowsSeen = 0;
166
+ let rowsSkipped = 0;
167
+ for (const row of Array.isArray(rows) ? rows : []) {
168
+ rowsSeen += 1;
169
+ const candidate = normalizeDevinUsageRow(row);
170
+ if (!candidate) {
171
+ rowsSkipped += 1;
172
+ continue;
173
+ }
174
+ const list = byRequest.get(candidate.requestId);
175
+ if (list) list.push(candidate);
176
+ else byRequest.set(candidate.requestId, [candidate]);
177
+ }
178
+
179
+ const events = [];
180
+ for (const [requestId, candidates] of byRequest) {
181
+ candidates.sort(compareCandidates);
182
+ const canonical = candidates[0];
183
+ const total = canonical.input + canonical.output + canonical.cacheRead + canonical.cacheCreation;
184
+ events.push({
185
+ requestId,
186
+ sessionId: canonical.sessionId,
187
+ workingDirectory: canonical.workingDirectory,
188
+ model: canonical.model,
189
+ tsMs: canonical.tsMs,
190
+ totals: {
191
+ input_tokens: canonical.input,
192
+ cached_input_tokens: canonical.cacheRead,
193
+ cache_creation_input_tokens: canonical.cacheCreation,
194
+ output_tokens: canonical.output,
195
+ reasoning_output_tokens: 0,
196
+ total_tokens: total,
197
+ billable_total_tokens: total,
198
+ total_cost_usd: 0,
199
+ conversation_count: 0,
200
+ },
201
+ });
202
+ }
203
+ events.sort((a, b) => a.tsMs - b.tsMs || (a.requestId < b.requestId ? -1 : 1));
204
+
205
+ return { events, rowsSeen, rowsSkipped };
206
+ }
207
+
208
+ module.exports = {
209
+ DEVIN_TABLE_PROBE_SQL,
210
+ devinUsageSql,
211
+ buildDevinUsageEvents,
212
+ };