tokenusage-cli 0.2.6 → 0.4.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 (53) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +52 -11
  3. package/bin/tokenusage.js +14 -2
  4. package/package.json +1 -2
  5. package/src/cli.js +325 -78
  6. package/src/commands/device-login.js +3 -3
  7. package/src/commands/init.js +343 -153
  8. package/src/commands/pause.js +11 -23
  9. package/src/commands/reconcile.js +485 -0
  10. package/src/commands/sessions.js +57 -14
  11. package/src/commands/status.js +5 -2
  12. package/src/commands/sync.js +77 -18
  13. package/src/commands/uninstall.js +6 -9
  14. package/src/commands/upload.js +147 -0
  15. package/src/lib/cli-errors.js +33 -0
  16. package/src/lib/cli-ui.js +93 -0
  17. package/src/lib/ingest-sanitize.js +9 -0
  18. package/src/lib/queue-row-normalize.js +55 -0
  19. package/src/lib/rollout.js +39 -2
  20. package/src/lib/scheduled-task.js +597 -75
  21. package/src/lib/session-analytics.js +171 -3
  22. package/src/lib/session-upload.js +6 -0
  23. package/src/lib/sync-report.js +8 -2
  24. package/src/lib/trae-db.js +6 -1
  25. package/src/lib/antigravity-paths.js +0 -48
  26. package/src/lib/async-queue-reader.js +0 -138
  27. package/src/lib/atomic-json.js +0 -27
  28. package/src/lib/bearer-token.js +0 -11
  29. package/src/lib/cloud-account.js +0 -541
  30. package/src/lib/codex-context-breakdown.js +0 -806
  31. package/src/lib/context-health.js +0 -124
  32. package/src/lib/grok-context-breakdown.js +0 -890
  33. package/src/lib/heatmap-compact.js +0 -114
  34. package/src/lib/local-api.js +0 -3574
  35. package/src/lib/outcomes-engine.js +0 -253
  36. package/src/lib/pet-packages.js +0 -722
  37. package/src/lib/skill-usage.js +0 -267
  38. package/src/lib/skills-manager.js +0 -1591
  39. package/src/lib/source-metadata.js +0 -48
  40. package/src/lib/subscription-manager.js +0 -293
  41. package/src/lib/wrapped-aggregator.js +0 -236
  42. package/test/audit-fixes.test.js +0 -249
  43. package/test/branding.test.js +0 -24
  44. package/test/cli-options.test.js +0 -43
  45. package/test/cli-smoke.test.js +0 -53
  46. package/test/cli-ui.test.js +0 -47
  47. package/test/collection-controls.test.js +0 -255
  48. package/test/ingest-sanitize.test.js +0 -162
  49. package/test/packaging.test.js +0 -107
  50. package/test/quiet-warnings.test.js +0 -53
  51. package/test/runtime-copy.test.js +0 -111
  52. package/test/session-upload.test.js +0 -427
  53. package/test/zstd-native.test.js +0 -36
@@ -1,253 +0,0 @@
1
- "use strict";
2
-
3
- // ---------------------------------------------------------------------------
4
- // Quality-per-dollar / Effective-Tokens engine (opt-in, sidecar)
5
- //
6
- // Token Usage answers "what did the tokens COST?". This optional layer adds
7
- // "what did they BUY?" by joining an opt-in, vendor-neutral `outcomes.jsonl`
8
- // sidecar to the existing token/$ rows by model + time.
9
- //
10
- // Design invariants (see GitHub #229):
11
- // 1. SIDECAR, never the hot path. This module only ever READS its own file
12
- // and the already-parsed queue rows. It never touches queue.jsonl, its
13
- // schema, or the sync path. A bug here cannot corrupt token/$ data.
14
- // 2. DEGRADES TO COST-ONLY. No outcomes file -> readOutcomesData returns []
15
- // and every join yields empty, so callers render exactly as today.
16
- // 3. METADATA-ONLY. sanitizeOutcome whitelists a tiny set of scalar fields
17
- // and drops everything else, so a mis-populated sidecar can NEVER surface
18
- // PR bodies, diffs, or message text into the dashboard.
19
- // ---------------------------------------------------------------------------
20
-
21
- const fs = require("node:fs");
22
- const os = require("node:os");
23
- const path = require("node:path");
24
- const { computeRowCost } = require("./pricing");
25
-
26
- function resolveOutcomesPath() {
27
- return path.join(os.homedir(), ".tokenusage", "tracker", "outcomes.jsonl");
28
- }
29
-
30
- function resolveAutoOutcomesPath() {
31
- return path.join(os.homedir(), ".tokenusage", "tracker", "auto-outcomes.jsonl");
32
- }
33
-
34
- // The ONLY fields ever lifted off an outcome record. Anything not on this list
35
- // — bodies, diffs, messages, arbitrary blobs — is dropped at read time. This is
36
- // the enforcement point for the metadata-only privacy invariant.
37
- function sanitizeOutcome(raw) {
38
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
39
- const timestamp = typeof raw.timestamp === "string" ? raw.timestamp.trim() : "";
40
- if (!timestamp) return null; // an outcome with no time can't be joined to $ rows
41
-
42
- // `accepted` is the gate signal: a merged PR / passed task. Strictly boolean
43
- // — anything other than literal true counts as not-accepted (rework).
44
- const accepted = raw.accepted === true;
45
-
46
- const model =
47
- typeof raw.model === "string" && raw.model.trim() ? raw.model.trim() : "unknown";
48
- // `tool` mirrors the queue row's `source` (claude, codex, cursor, …). Accept
49
- // either key so writers can use whichever they have on hand.
50
- const toolRaw =
51
- (typeof raw.tool === "string" && raw.tool.trim() && raw.tool) ||
52
- (typeof raw.source === "string" && raw.source.trim() && raw.source) ||
53
- "";
54
- const tool = toolRaw ? String(toolRaw).trim() : "unknown";
55
-
56
- const out = { timestamp, model, tool, accepted };
57
- // task_type is an optional, low-cardinality label (e.g. "feature", "bugfix").
58
- if (typeof raw.task_type === "string" && raw.task_type.trim()) {
59
- out.task_type = raw.task_type.trim();
60
- }
61
- for (const key of ["status", "session_hash", "commit_hash", "confidence", "methodology"]) {
62
- if (typeof raw[key] === "string" && raw[key].trim()) out[key] = raw[key].trim();
63
- }
64
- if (Number.isFinite(Number(raw.parent_count))) out.parent_count = Number(raw.parent_count);
65
- return out;
66
- }
67
-
68
- function readOutcomesData(outcomesPath) {
69
- let raw;
70
- try {
71
- raw = fs.readFileSync(outcomesPath, "utf8");
72
- } catch (e) {
73
- // ENOENT is the normal "user hasn't opted in" case — silent. Anything else
74
- // is surfaced once so a genuinely unreadable file isn't hidden forever.
75
- if (e && e.code !== "ENOENT") {
76
- console.error("[outcomes] read failed:", (e && e.message) || e);
77
- }
78
- return [];
79
- }
80
- const out = [];
81
- let malformed = 0;
82
- for (const line of raw.split("\n")) {
83
- const trimmed = line.trim();
84
- if (!trimmed) continue;
85
- let parsed;
86
- try {
87
- parsed = JSON.parse(trimmed);
88
- } catch {
89
- malformed += 1;
90
- continue;
91
- }
92
- const rec = sanitizeOutcome(parsed);
93
- if (rec) out.push(rec);
94
- else malformed += 1;
95
- }
96
- if (malformed > 0) {
97
- console.error(`[outcomes] skipped ${malformed} malformed/invalid line(s) in ${outcomesPath}`);
98
- }
99
- return out;
100
- }
101
-
102
- function readAllOutcomesData(paths = [resolveOutcomesPath(), resolveAutoOutcomesPath()]) {
103
- const dedup = new Map();
104
- for (const outcomePath of paths) {
105
- for (const row of readOutcomesData(outcomePath)) {
106
- const key = row.commit_hash
107
- ? `commit:${row.commit_hash}`
108
- : `${row.timestamp}|${row.model}|${row.tool}|${row.task_type || ""}`;
109
- dedup.set(key, row);
110
- }
111
- }
112
- return [...dedup.values()];
113
- }
114
-
115
- function dayOf(value) {
116
- return typeof value === "string" ? value.slice(0, 10) : "";
117
- }
118
-
119
- function inWindow(dayStr, from, to) {
120
- if (from && dayStr && dayStr < from) return false;
121
- if (to && dayStr && dayStr > to) return false;
122
- return true;
123
- }
124
-
125
- function bumpCost(map, key, cost, tokens) {
126
- let agg = map.get(key);
127
- if (!agg) {
128
- agg = { cost_usd: 0, total_tokens: 0 };
129
- map.set(key, agg);
130
- }
131
- agg.cost_usd += cost;
132
- agg.total_tokens += tokens;
133
- }
134
-
135
- function bumpOutcome(map, key, accepted) {
136
- let agg = map.get(key);
137
- if (!agg) {
138
- agg = { accepted: 0, outcomes: 0 };
139
- map.set(key, agg);
140
- }
141
- agg.outcomes += 1;
142
- if (accepted) agg.accepted += 1;
143
- }
144
-
145
- function combineRow(key, cost, out) {
146
- const cost_usd = cost ? cost.cost_usd : 0;
147
- const total_tokens = cost ? cost.total_tokens : 0;
148
- const accepted = out ? out.accepted : 0;
149
- const outcomes = out ? out.outcomes : 0;
150
- const acceptance_rate = outcomes > 0 ? accepted / outcomes : null;
151
- // quality per dollar = accepted, gate-passing outcomes ÷ dollars spent.
152
- // Null (not zero) when we can't form the ratio — no spend or no outcomes —
153
- // so the UI can distinguish "0 quality" from "not enough data".
154
- const quality_per_dollar = cost_usd > 0 && outcomes > 0 ? accepted / cost_usd : null;
155
- // Effective Tokens: the share of tokens that produced accepted work.
156
- const effective_tokens = acceptance_rate === null ? null : total_tokens * acceptance_rate;
157
- const effective_cost_usd = acceptance_rate === null ? null : cost_usd * acceptance_rate;
158
- return {
159
- key,
160
- cost_usd,
161
- total_tokens,
162
- accepted,
163
- outcomes,
164
- acceptance_rate,
165
- quality_per_dollar,
166
- effective_tokens,
167
- effective_cost_usd,
168
- };
169
- }
170
-
171
- function combine(costMap, outMap) {
172
- const keys = new Set([...costMap.keys(), ...outMap.keys()]);
173
- const rows = [];
174
- for (const key of keys) {
175
- rows.push(combineRow(key, costMap.get(key), outMap.get(key)));
176
- }
177
- // Highest quality-per-dollar first; rows without a ratio sink to the bottom.
178
- rows.sort((a, b) => {
179
- const av = a.quality_per_dollar;
180
- const bv = b.quality_per_dollar;
181
- if (av === null && bv === null) return b.outcomes - a.outcomes;
182
- if (av === null) return 1;
183
- if (bv === null) return -1;
184
- return bv - av;
185
- });
186
- return rows;
187
- }
188
-
189
- /**
190
- * Join outcomes to token/$ rows by model + tool, within an optional [from,to]
191
- * day window. Returns per-model and per-tool quality-per-dollar / ET, plus
192
- * totals. Pure: no I/O, no mutation of inputs.
193
- *
194
- * @param {Array} queueRows already-parsed queue rows ({ source, model, hour_start, …token fields })
195
- * @param {Array} outcomes sanitized outcome records (from readOutcomesData)
196
- * @param {{from?:string,to?:string}} [window]
197
- */
198
- function computeQualityPerDollar(queueRows, outcomes, { from = "", to = "" } = {}) {
199
- const modelCost = new Map();
200
- const toolCost = new Map();
201
- for (const row of queueRows || []) {
202
- if (!row || typeof row !== "object") continue; // tolerate a stray null/garbage line
203
- const day = dayOf(row.hour_start);
204
- if ((from || to) && !inWindow(day, from, to)) continue;
205
- const model = (row && row.model) || "unknown";
206
- const tool = (row && row.source) || "unknown";
207
- const cost = computeRowCost(row) || 0;
208
- const tokens = Number((row && row.total_tokens) || 0);
209
- bumpCost(modelCost, model, cost, tokens);
210
- bumpCost(toolCost, tool, cost, tokens);
211
- }
212
-
213
- const modelOut = new Map();
214
- const toolOut = new Map();
215
- for (const o of outcomes || []) {
216
- const day = dayOf(o && o.timestamp);
217
- if ((from || to) && !inWindow(day, from, to)) continue;
218
- bumpOutcome(modelOut, (o && o.model) || "unknown", !!(o && o.accepted));
219
- bumpOutcome(toolOut, (o && o.tool) || "unknown", !!(o && o.accepted));
220
- }
221
-
222
- const by_model = combine(modelCost, modelOut);
223
- const by_tool = combine(toolCost, toolOut);
224
-
225
- let totalCost = 0;
226
- let totalTokens = 0;
227
- for (const v of modelCost.values()) {
228
- totalCost += v.cost_usd;
229
- totalTokens += v.total_tokens;
230
- }
231
- let totalAccepted = 0;
232
- let totalOutcomes = 0;
233
- for (const v of modelOut.values()) {
234
- totalAccepted += v.accepted;
235
- totalOutcomes += v.outcomes;
236
- }
237
- const totals = combineRow(
238
- "__all__",
239
- { cost_usd: totalCost, total_tokens: totalTokens },
240
- { accepted: totalAccepted, outcomes: totalOutcomes },
241
- );
242
-
243
- return { available: outcomes && outcomes.length > 0, by_model, by_tool, totals };
244
- }
245
-
246
- module.exports = {
247
- resolveOutcomesPath,
248
- resolveAutoOutcomesPath,
249
- sanitizeOutcome,
250
- readOutcomesData,
251
- readAllOutcomesData,
252
- computeQualityPerDollar,
253
- };