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,216 +1,216 @@
1
- "use strict";
2
-
3
- const crypto = require("node:crypto");
4
- const fs = require("node:fs");
5
- const path = require("node:path");
6
- const { physicalJsonlRecords } = require("./jsonl-lines");
7
- const { resolveKimiDefaultModel } = require("./rollout");
8
- const { mergePerformance } = require("./session-performance");
9
-
10
- function readObject(file) {
11
- try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
12
- }
13
-
14
- function kimiSessionLocation(file) {
15
- const official = path.basename(path.dirname(path.dirname(file))) === "agents";
16
- const directory = official ? path.dirname(path.dirname(path.dirname(file))) : path.dirname(file);
17
- const id = path.basename(directory);
18
- const imported = official ? readObject(path.join(directory, "state.json"))?.custom : null;
19
- // The official migrator keeps the legacy UUID under ses_<UUID>. Group its
20
- // message-only mirror with the original wire, retaining any new usage tail.
21
- const identity = imported?.imported_from_kimi_cli && typeof imported.kimi_cli_session_id === "string"
22
- ? imported.kimi_cli_session_id : id.replace(/^(?:ses_|session_)(?=[0-9a-f-]{36}$)/i, "");
23
- return { directory, id, identity, official, main: !official || path.basename(path.dirname(file)) === "main" };
24
- }
25
-
26
- function kimiSessionDependencies(files) {
27
- return [...new Set(files.flatMap((file) => {
28
- const location = kimiSessionLocation(file);
29
- const root = path.dirname(path.dirname(path.dirname(location.directory)));
30
- return [path.join(location.directory, "state.json"), path.join(root, "config.toml"), path.join(root, "kimi.json")];
31
- }))];
32
- }
33
-
34
- function discoverKimiWireFiles(roots, incomplete) {
35
- const files = new Set();
36
- function walk(directory, depth = 0) {
37
- if (depth > 5) return;
38
- let entries;
39
- try { entries = fs.readdirSync(directory, { withFileTypes: true }); } catch (error) {
40
- if (error.code !== "ENOENT") incomplete.push({ path: directory, error: error.code || "unreadable" });
41
- return;
42
- }
43
- for (const entry of entries) {
44
- if (entry.name.startsWith(".")) continue;
45
- const file = path.join(directory, entry.name);
46
- if (entry.isDirectory()) walk(file, depth + 1);
47
- else if (entry.isFile() && entry.name === "wire.jsonl") files.add(file);
48
- }
49
- }
50
- for (const root of roots) walk(path.join(root, "sessions"));
51
- return [...files].sort();
52
- }
53
-
54
- const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Math.floor(Number(value))) : 0;
55
- function kimiUsage(usage, legacy) {
56
- if (!usage || typeof usage !== "object") return null;
57
- let input, cached, created, output;
58
- // Same column semantics as parseKimiIncremental / parseKimiCodeIncremental.
59
- if (legacy || usage.inputOther != null) {
60
- input = count(legacy ? usage.input_other : usage.inputOther);
61
- cached = count(legacy ? usage.input_cache_read : usage.inputCacheRead);
62
- created = count(legacy ? usage.input_cache_creation : usage.inputCacheCreation);
63
- output = count(usage.output);
64
- } else {
65
- created = count(usage.cache_creation_input_tokens);
66
- cached = count(usage.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens);
67
- input = Math.max(0, count(usage.input_tokens) - (usage.cache_read_input_tokens != null ? 0 : cached));
68
- output = count(usage.output_tokens);
69
- }
70
- return { input_tokens: input, cached_input_tokens: cached, cache_creation_input_tokens: created,
71
- output_tokens: output, reasoning_output_tokens: 0, total_tokens: input + cached + created + output };
72
- }
73
-
74
- const modelAlias = (value) => typeof value === "string" && value.trim() ? value.trim().split("/").pop() : null;
75
- const EDIT_TOOLS = new Set(["edit", "write", "multiedit", "notebookedit", "strreplacefile", "writefile", "apply_patch"]);
76
- const AGENT_TOOLS = new Set(["agent", "agentswarm", "task"]);
77
- const hash = (value) => crypto.createHash("sha256").update(value).digest("hex");
78
-
79
- // Reads both wire protocols into metadata only. A full inventory shares its
80
- // usage-ID set, as sync does, so copied/forked step records count only once.
81
- async function readKimiSessions(files) {
82
- const groups = new Map();
83
- for (const file of [...new Set(files)]) {
84
- const location = kimiSessionLocation(file);
85
- const group = groups.get(location.identity) || { identity: location.identity, files: [], createdAt: Infinity };
86
- const state = location.official ? readObject(path.join(location.directory, "state.json")) : {};
87
- group.createdAt = Math.min(group.createdAt, Number(state.createdAt) || Infinity);
88
- group.files.push({ file, ...location, state });
89
- groups.set(location.identity, group);
90
- }
91
- const seenUsage = new Set();
92
- const seenSignals = new Set();
93
- const rows = [];
94
- for (const group of [...groups.values()].sort((a, b) => a.createdAt - b.createdAt || a.identity.localeCompare(b.identity))) {
95
- const tokens = kimiUsage({}, true);
96
- const modelUsage = new Map();
97
- const buckets = new Map();
98
- const times = new Set();
99
- let turns = 0, edits = 0, retries = 0, agents = 0, errors = 0;
100
- let lastPrompt = null;
101
- let cwd = null;
102
- let resumeId = null;
103
- const modelsForEdits = new Map();
104
- // Legacy first preserves migrated user turns; context.append_message in
105
- // the imported wire is history, never another prompt or usage event.
106
- group.files.sort((a, b) => Number(a.official) - Number(b.official) || Number(b.main) - Number(a.main) || a.file.localeCompare(b.file));
107
- for (const location of group.files) {
108
- const root = path.dirname(path.dirname(path.dirname(location.directory)));
109
- let model = resolveKimiDefaultModel({ KIMI_HOME: root });
110
- if (location.official && location.main) {
111
- resumeId = location.id;
112
- if (typeof location.state.workDir === "string") cwd = location.state.workDir;
113
- }
114
- if (!location.official && !cwd) {
115
- const workspaceHash = path.basename(path.dirname(location.directory));
116
- const workDirs = readObject(path.join(root, "kimi.json"))?.work_dirs;
117
- const workspace = Array.isArray(workDirs) && workDirs.find((row) => typeof row?.path === "string"
118
- && crypto.createHash("md5").update(row.path).digest("hex") === workspaceHash);
119
- if (workspace) cwd = workspace.path;
120
- }
121
- let turnOpen = false, hadEdit = false, turnModel = model;
122
- const closeTurn = () => {
123
- if (hadEdit) { edits++; modelsForEdits.set(turnModel, (modelsForEdits.get(turnModel) || 0) + 1); }
124
- hadEdit = false; turnOpen = false;
125
- };
126
- const input = fs.createReadStream(location.file);
127
- for await (const { line, terminated } of physicalJsonlRecords(input)) {
128
- if (!terminated || !line) continue;
129
- let entry;
130
- try { entry = JSON.parse(line); } catch { continue; }
131
- if (!entry || typeof entry !== "object") continue;
132
- const message = entry.message && typeof entry.message === "object" ? entry.message : null;
133
- const event = entry.type === "context.append_loop_event" ? entry.event : entry;
134
- const timestampMs = Number(location.official ? entry.time ?? event?.time : entry.timestamp ?? message?.payload?.timestamp) * (location.official ? 1 : 1000);
135
- const timestamp = Number.isFinite(timestampMs) && timestampMs > 0 && timestampMs <= 8640000000000000
136
- ? new Date(timestampMs).toISOString() : null;
137
- if (timestamp) times.add(timestampMs);
138
- if (["config.update", "profile.bind", "llm.request"].includes(entry.type)) {
139
- model = modelAlias(entry.modelAlias || entry.model) || model;
140
- if (location.main && typeof (entry.cwd || entry.environmentDisclosure?.cwd) === "string") cwd = entry.cwd || entry.environmentDisclosure.cwd;
141
- }
142
- const isLegacyUsage = message?.type === "StatusUpdate";
143
- const usage = isLegacyUsage ? message.payload?.token_usage : event?.type === "step.end" ? event.usage : null;
144
- const usageId = isLegacyUsage ? message.payload?.message_id : event?.uuid;
145
- if (usage && typeof usageId === "string" && timestamp) {
146
- const ids = [isLegacyUsage ? `message:${usageId}` : `step:${usageId}`];
147
- if (!isLegacyUsage && typeof event.messageId === "string") ids.push(`message:${event.messageId}`);
148
- const delta = kimiUsage(usage, isLegacyUsage);
149
- if (delta.total_tokens > 0 && !ids.some((id) => seenUsage.has(id))) {
150
- for (const id of ids) seenUsage.add(id);
151
- const modelRow = modelUsage.get(model) || { model, ...kimiUsage({}, true), usage_events: 0 };
152
- const bucketTime = new Date(Math.floor(timestampMs / 60000) * 60000).toISOString();
153
- const key = `${model}\0${bucketTime}`;
154
- const bucket = buckets.get(key) || { model, timestamp: bucketTime, ...kimiUsage({}, true), usage_events: 0 };
155
- for (const field of Object.keys(delta)) { tokens[field] += delta[field]; modelRow[field] += delta[field]; bucket[field] += delta[field]; }
156
- modelRow.usage_events++; bucket.usage_events++;
157
- const streamMs = count(event.llmStreamDurationMs);
158
- const firstMs = Number(event.llmFirstTokenLatencyMs);
159
- const performance = {
160
- ...(delta.output_tokens >= 200 && streamMs >= 1000 && streamMs <= 3600000
161
- ? { estimated_output_tokens: delta.output_tokens, estimated_duration_ms: streamMs, estimated_request_count: 1 } : {}),
162
- ...(event.llmFirstTokenLatencyMs != null && Number.isFinite(firstMs) && firstMs >= 0
163
- ? { first_response_total_ms: firstMs, first_response_sample_count: 1 } : {}),
164
- };
165
- modelRow.performance = mergePerformance(modelRow.performance, performance);
166
- modelUsage.set(model, modelRow); buckets.set(key, bucket);
167
- turnModel = model;
168
- }
169
- }
170
- if (!location.main) continue;
171
- const signalId = hash(line);
172
- if (seenSignals.has(signalId)) continue;
173
- seenSignals.add(signalId);
174
- const prompt = message?.type === "TurnBegin" ? message.payload?.user_input
175
- : entry.type === "turn.prompt" && (!entry.origin?.kind || entry.origin.kind === "user") ? entry.input : null;
176
- if (prompt != null) {
177
- closeTurn(); turns++; turnOpen = true; turnModel = model;
178
- // Fingerprints live only for this scan; no text or hash is persisted.
179
- const fingerprint = hash(JSON.stringify(prompt));
180
- if (lastPrompt && fingerprint === lastPrompt) retries++;
181
- lastPrompt = fingerprint;
182
- }
183
- const tool = message?.type === "ToolCall" ? message.payload?.function?.name : event?.type === "tool.call" ? event.name : null;
184
- if (typeof tool === "string") {
185
- const name = tool.toLowerCase();
186
- if (turnOpen && EDIT_TOOLS.has(name)) hadEdit = true;
187
- if (AGENT_TOOLS.has(name)) agents++;
188
- }
189
- if (entry.type === "turn.step.retrying" || message?.type === "StepInterrupted") errors++;
190
- if (message?.type === "TurnEnd" || entry.type === "turn.ended") closeTurn();
191
- }
192
- closeTurn();
193
- }
194
- const orderedTimes = [...times].sort((a, b) => a - b);
195
- let activeMs = 0;
196
- for (let i = 1; i < orderedTimes.length; i++) {
197
- const gap = orderedTimes[i] - orderedTimes[i - 1];
198
- if (gap <= 30 * 60 * 1000) activeMs += gap;
199
- }
200
- const models = [...modelUsage.values()].map((row) => ({ ...row, edit_turns: modelsForEdits.get(row.model) || 0 }));
201
- if (!tokens.total_tokens && !turns) continue;
202
- rows.push({ identity: group.identity, files: group.files.map((row) => row.file), session_id: resumeId,
203
- title: null, project_ref: cwd, model: models.length === 1 ? models[0].model : "unknown", model_usage: models,
204
- usage_buckets: [...buckets.values()], tokens, turns, edit_turns: edits, retry_turns: retries,
205
- subagent_calls: agents, subagent_types: {}, error_count: errors,
206
- usage_events: models.reduce((sum, row) => sum + row.usage_events, 0),
207
- started_at: orderedTimes.length ? new Date(orderedTimes[0]).toISOString() : null,
208
- ended_at: orderedTimes.length ? new Date(orderedTimes.at(-1)).toISOString() : null, active_ms: activeMs,
209
- performance: mergePerformance(...models.map((row) => row.performance)),
210
- provenance: { source: "local-session-log", confidence: "observed", retry_confidence: "inferred", content_retained: false, usage: "kimi-wire-metadata" },
211
- });
212
- }
213
- return rows;
214
- }
215
-
216
- module.exports = { discoverKimiWireFiles, kimiSessionDependencies, readKimiSessions };
1
+ "use strict";
2
+
3
+ const crypto = require("node:crypto");
4
+ const fs = require("node:fs");
5
+ const path = require("node:path");
6
+ const { physicalJsonlRecords } = require("./jsonl-lines");
7
+ const { resolveKimiDefaultModel } = require("./rollout");
8
+ const { mergePerformance } = require("./session-performance");
9
+
10
+ function readObject(file) {
11
+ try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return {}; }
12
+ }
13
+
14
+ function kimiSessionLocation(file) {
15
+ const official = path.basename(path.dirname(path.dirname(file))) === "agents";
16
+ const directory = official ? path.dirname(path.dirname(path.dirname(file))) : path.dirname(file);
17
+ const id = path.basename(directory);
18
+ const imported = official ? readObject(path.join(directory, "state.json"))?.custom : null;
19
+ // The official migrator keeps the legacy UUID under ses_<UUID>. Group its
20
+ // message-only mirror with the original wire, retaining any new usage tail.
21
+ const identity = imported?.imported_from_kimi_cli && typeof imported.kimi_cli_session_id === "string"
22
+ ? imported.kimi_cli_session_id : id.replace(/^(?:ses_|session_)(?=[0-9a-f-]{36}$)/i, "");
23
+ return { directory, id, identity, official, main: !official || path.basename(path.dirname(file)) === "main" };
24
+ }
25
+
26
+ function kimiSessionDependencies(files) {
27
+ return [...new Set(files.flatMap((file) => {
28
+ const location = kimiSessionLocation(file);
29
+ const root = path.dirname(path.dirname(path.dirname(location.directory)));
30
+ return [path.join(location.directory, "state.json"), path.join(root, "config.toml"), path.join(root, "kimi.json")];
31
+ }))];
32
+ }
33
+
34
+ function discoverKimiWireFiles(roots, incomplete) {
35
+ const files = new Set();
36
+ function walk(directory, depth = 0) {
37
+ if (depth > 5) return;
38
+ let entries;
39
+ try { entries = fs.readdirSync(directory, { withFileTypes: true }); } catch (error) {
40
+ if (error.code !== "ENOENT") incomplete.push({ path: directory, error: error.code || "unreadable" });
41
+ return;
42
+ }
43
+ for (const entry of entries) {
44
+ if (entry.name.startsWith(".")) continue;
45
+ const file = path.join(directory, entry.name);
46
+ if (entry.isDirectory()) walk(file, depth + 1);
47
+ else if (entry.isFile() && entry.name === "wire.jsonl") files.add(file);
48
+ }
49
+ }
50
+ for (const root of roots) walk(path.join(root, "sessions"));
51
+ return [...files].sort();
52
+ }
53
+
54
+ const count = (value) => Number.isFinite(Number(value)) ? Math.max(0, Math.floor(Number(value))) : 0;
55
+ function kimiUsage(usage, legacy) {
56
+ if (!usage || typeof usage !== "object") return null;
57
+ let input, cached, created, output;
58
+ // Same column semantics as parseKimiIncremental / parseKimiCodeIncremental.
59
+ if (legacy || usage.inputOther != null) {
60
+ input = count(legacy ? usage.input_other : usage.inputOther);
61
+ cached = count(legacy ? usage.input_cache_read : usage.inputCacheRead);
62
+ created = count(legacy ? usage.input_cache_creation : usage.inputCacheCreation);
63
+ output = count(usage.output);
64
+ } else {
65
+ created = count(usage.cache_creation_input_tokens);
66
+ cached = count(usage.cache_read_input_tokens ?? usage.input_tokens_details?.cached_tokens);
67
+ input = Math.max(0, count(usage.input_tokens) - (usage.cache_read_input_tokens != null ? 0 : cached));
68
+ output = count(usage.output_tokens);
69
+ }
70
+ return { input_tokens: input, cached_input_tokens: cached, cache_creation_input_tokens: created,
71
+ output_tokens: output, reasoning_output_tokens: 0, total_tokens: input + cached + created + output };
72
+ }
73
+
74
+ const modelAlias = (value) => typeof value === "string" && value.trim() ? value.trim().split("/").pop() : null;
75
+ const EDIT_TOOLS = new Set(["edit", "write", "multiedit", "notebookedit", "strreplacefile", "writefile", "apply_patch"]);
76
+ const AGENT_TOOLS = new Set(["agent", "agentswarm", "task"]);
77
+ const hash = (value) => crypto.createHash("sha256").update(value).digest("hex");
78
+
79
+ // Reads both wire protocols into metadata only. A full inventory shares its
80
+ // usage-ID set, as sync does, so copied/forked step records count only once.
81
+ async function readKimiSessions(files) {
82
+ const groups = new Map();
83
+ for (const file of [...new Set(files)]) {
84
+ const location = kimiSessionLocation(file);
85
+ const group = groups.get(location.identity) || { identity: location.identity, files: [], createdAt: Infinity };
86
+ const state = location.official ? readObject(path.join(location.directory, "state.json")) : {};
87
+ group.createdAt = Math.min(group.createdAt, Number(state.createdAt) || Infinity);
88
+ group.files.push({ file, ...location, state });
89
+ groups.set(location.identity, group);
90
+ }
91
+ const seenUsage = new Set();
92
+ const seenSignals = new Set();
93
+ const rows = [];
94
+ for (const group of [...groups.values()].sort((a, b) => a.createdAt - b.createdAt || a.identity.localeCompare(b.identity))) {
95
+ const tokens = kimiUsage({}, true);
96
+ const modelUsage = new Map();
97
+ const buckets = new Map();
98
+ const times = new Set();
99
+ let turns = 0, edits = 0, retries = 0, agents = 0, errors = 0;
100
+ let lastPrompt = null;
101
+ let cwd = null;
102
+ let resumeId = null;
103
+ const modelsForEdits = new Map();
104
+ // Legacy first preserves migrated user turns; context.append_message in
105
+ // the imported wire is history, never another prompt or usage event.
106
+ group.files.sort((a, b) => Number(a.official) - Number(b.official) || Number(b.main) - Number(a.main) || a.file.localeCompare(b.file));
107
+ for (const location of group.files) {
108
+ const root = path.dirname(path.dirname(path.dirname(location.directory)));
109
+ let model = resolveKimiDefaultModel({ KIMI_HOME: root });
110
+ if (location.official && location.main) {
111
+ resumeId = location.id;
112
+ if (typeof location.state.workDir === "string") cwd = location.state.workDir;
113
+ }
114
+ if (!location.official && !cwd) {
115
+ const workspaceHash = path.basename(path.dirname(location.directory));
116
+ const workDirs = readObject(path.join(root, "kimi.json"))?.work_dirs;
117
+ const workspace = Array.isArray(workDirs) && workDirs.find((row) => typeof row?.path === "string"
118
+ && crypto.createHash("md5").update(row.path).digest("hex") === workspaceHash);
119
+ if (workspace) cwd = workspace.path;
120
+ }
121
+ let turnOpen = false, hadEdit = false, turnModel = model;
122
+ const closeTurn = () => {
123
+ if (hadEdit) { edits++; modelsForEdits.set(turnModel, (modelsForEdits.get(turnModel) || 0) + 1); }
124
+ hadEdit = false; turnOpen = false;
125
+ };
126
+ const input = fs.createReadStream(location.file);
127
+ for await (const { line, terminated } of physicalJsonlRecords(input)) {
128
+ if (!terminated || !line) continue;
129
+ let entry;
130
+ try { entry = JSON.parse(line); } catch { continue; }
131
+ if (!entry || typeof entry !== "object") continue;
132
+ const message = entry.message && typeof entry.message === "object" ? entry.message : null;
133
+ const event = entry.type === "context.append_loop_event" ? entry.event : entry;
134
+ const timestampMs = Number(location.official ? entry.time ?? event?.time : entry.timestamp ?? message?.payload?.timestamp) * (location.official ? 1 : 1000);
135
+ const timestamp = Number.isFinite(timestampMs) && timestampMs > 0 && timestampMs <= 8640000000000000
136
+ ? new Date(timestampMs).toISOString() : null;
137
+ if (timestamp) times.add(timestampMs);
138
+ if (["config.update", "profile.bind", "llm.request"].includes(entry.type)) {
139
+ model = modelAlias(entry.modelAlias || entry.model) || model;
140
+ if (location.main && typeof (entry.cwd || entry.environmentDisclosure?.cwd) === "string") cwd = entry.cwd || entry.environmentDisclosure.cwd;
141
+ }
142
+ const isLegacyUsage = message?.type === "StatusUpdate";
143
+ const usage = isLegacyUsage ? message.payload?.token_usage : event?.type === "step.end" ? event.usage : null;
144
+ const usageId = isLegacyUsage ? message.payload?.message_id : event?.uuid;
145
+ if (usage && typeof usageId === "string" && timestamp) {
146
+ const ids = [isLegacyUsage ? `message:${usageId}` : `step:${usageId}`];
147
+ if (!isLegacyUsage && typeof event.messageId === "string") ids.push(`message:${event.messageId}`);
148
+ const delta = kimiUsage(usage, isLegacyUsage);
149
+ if (delta.total_tokens > 0 && !ids.some((id) => seenUsage.has(id))) {
150
+ for (const id of ids) seenUsage.add(id);
151
+ const modelRow = modelUsage.get(model) || { model, ...kimiUsage({}, true), usage_events: 0 };
152
+ const bucketTime = new Date(Math.floor(timestampMs / 60000) * 60000).toISOString();
153
+ const key = `${model}\0${bucketTime}`;
154
+ const bucket = buckets.get(key) || { model, timestamp: bucketTime, ...kimiUsage({}, true), usage_events: 0 };
155
+ for (const field of Object.keys(delta)) { tokens[field] += delta[field]; modelRow[field] += delta[field]; bucket[field] += delta[field]; }
156
+ modelRow.usage_events++; bucket.usage_events++;
157
+ const streamMs = count(event.llmStreamDurationMs);
158
+ const firstMs = Number(event.llmFirstTokenLatencyMs);
159
+ const performance = {
160
+ ...(delta.output_tokens >= 200 && streamMs >= 1000 && streamMs <= 3600000
161
+ ? { estimated_output_tokens: delta.output_tokens, estimated_duration_ms: streamMs, estimated_request_count: 1 } : {}),
162
+ ...(event.llmFirstTokenLatencyMs != null && Number.isFinite(firstMs) && firstMs >= 0
163
+ ? { first_response_total_ms: firstMs, first_response_sample_count: 1 } : {}),
164
+ };
165
+ modelRow.performance = mergePerformance(modelRow.performance, performance);
166
+ modelUsage.set(model, modelRow); buckets.set(key, bucket);
167
+ turnModel = model;
168
+ }
169
+ }
170
+ if (!location.main) continue;
171
+ const signalId = hash(line);
172
+ if (seenSignals.has(signalId)) continue;
173
+ seenSignals.add(signalId);
174
+ const prompt = message?.type === "TurnBegin" ? message.payload?.user_input
175
+ : entry.type === "turn.prompt" && (!entry.origin?.kind || entry.origin.kind === "user") ? entry.input : null;
176
+ if (prompt != null) {
177
+ closeTurn(); turns++; turnOpen = true; turnModel = model;
178
+ // Fingerprints live only for this scan; no text or hash is persisted.
179
+ const fingerprint = hash(JSON.stringify(prompt));
180
+ if (lastPrompt && fingerprint === lastPrompt) retries++;
181
+ lastPrompt = fingerprint;
182
+ }
183
+ const tool = message?.type === "ToolCall" ? message.payload?.function?.name : event?.type === "tool.call" ? event.name : null;
184
+ if (typeof tool === "string") {
185
+ const name = tool.toLowerCase();
186
+ if (turnOpen && EDIT_TOOLS.has(name)) hadEdit = true;
187
+ if (AGENT_TOOLS.has(name)) agents++;
188
+ }
189
+ if (entry.type === "turn.step.retrying" || message?.type === "StepInterrupted") errors++;
190
+ if (message?.type === "TurnEnd" || entry.type === "turn.ended") closeTurn();
191
+ }
192
+ closeTurn();
193
+ }
194
+ const orderedTimes = [...times].sort((a, b) => a - b);
195
+ let activeMs = 0;
196
+ for (let i = 1; i < orderedTimes.length; i++) {
197
+ const gap = orderedTimes[i] - orderedTimes[i - 1];
198
+ if (gap <= 30 * 60 * 1000) activeMs += gap;
199
+ }
200
+ const models = [...modelUsage.values()].map((row) => ({ ...row, edit_turns: modelsForEdits.get(row.model) || 0 }));
201
+ if (!tokens.total_tokens && !turns) continue;
202
+ rows.push({ identity: group.identity, files: group.files.map((row) => row.file), session_id: resumeId,
203
+ title: null, project_ref: cwd, model: models.length === 1 ? models[0].model : "unknown", model_usage: models,
204
+ usage_buckets: [...buckets.values()], tokens, turns, edit_turns: edits, retry_turns: retries,
205
+ subagent_calls: agents, subagent_types: {}, error_count: errors,
206
+ usage_events: models.reduce((sum, row) => sum + row.usage_events, 0),
207
+ started_at: orderedTimes.length ? new Date(orderedTimes[0]).toISOString() : null,
208
+ ended_at: orderedTimes.length ? new Date(orderedTimes.at(-1)).toISOString() : null, active_ms: activeMs,
209
+ performance: mergePerformance(...models.map((row) => row.performance)),
210
+ provenance: { source: "local-session-log", confidence: "observed", retry_confidence: "inferred", content_retained: false, usage: "kimi-wire-metadata" },
211
+ });
212
+ }
213
+ return rows;
214
+ }
215
+
216
+ module.exports = { discoverKimiWireFiles, kimiSessionDependencies, readKimiSessions };