tokenusage-cli 0.3.0 → 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 (47) hide show
  1. package/CHANGELOG.md +50 -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/sync.js +69 -15
  12. package/src/commands/uninstall.js +6 -9
  13. package/src/commands/upload.js +147 -0
  14. package/src/lib/cli-errors.js +33 -0
  15. package/src/lib/cli-ui.js +93 -0
  16. package/src/lib/queue-row-normalize.js +55 -0
  17. package/src/lib/scheduled-task.js +597 -75
  18. package/src/lib/antigravity-paths.js +0 -48
  19. package/src/lib/async-queue-reader.js +0 -138
  20. package/src/lib/atomic-json.js +0 -27
  21. package/src/lib/bearer-token.js +0 -11
  22. package/src/lib/cloud-account.js +0 -541
  23. package/src/lib/codex-context-breakdown.js +0 -806
  24. package/src/lib/context-health.js +0 -124
  25. package/src/lib/grok-context-breakdown.js +0 -890
  26. package/src/lib/heatmap-compact.js +0 -114
  27. package/src/lib/local-api.js +0 -3574
  28. package/src/lib/outcomes-engine.js +0 -253
  29. package/src/lib/pet-packages.js +0 -722
  30. package/src/lib/skill-usage.js +0 -267
  31. package/src/lib/skills-manager.js +0 -1591
  32. package/src/lib/source-metadata.js +0 -48
  33. package/src/lib/subscription-manager.js +0 -293
  34. package/src/lib/wrapped-aggregator.js +0 -236
  35. package/test/audit-fixes.test.js +0 -249
  36. package/test/branding.test.js +0 -24
  37. package/test/cli-options.test.js +0 -43
  38. package/test/cli-smoke.test.js +0 -53
  39. package/test/cli-ui.test.js +0 -47
  40. package/test/collection-controls.test.js +0 -255
  41. package/test/ingest-sanitize.test.js +0 -162
  42. package/test/packaging.test.js +0 -107
  43. package/test/quiet-warnings.test.js +0 -53
  44. package/test/runtime-copy.test.js +0 -111
  45. package/test/session-upload.test.js +0 -427
  46. package/test/trae-db.test.js +0 -67
  47. package/test/zstd-native.test.js +0 -36
@@ -1,48 +0,0 @@
1
- const fs = require("node:fs");
2
- const os = require("node:os");
3
- const path = require("node:path");
4
-
5
- const MAIN_APP_SUBDIR = "antigravity";
6
- const IDE_SUBDIR = "antigravity-ide";
7
-
8
- function geminiHome(env) {
9
- return path.join(env.HOME || env.USERPROFILE || os.homedir(), ".gemini");
10
- }
11
-
12
- function trimmed(value) {
13
- return typeof value === "string" ? value.trim() : "";
14
- }
15
-
16
- function existsDir(dirPath) {
17
- try {
18
- return fs.statSync(dirPath).isDirectory();
19
- } catch (_e) {
20
- return false;
21
- }
22
- }
23
-
24
- function resolveAntigravitySkillDirs(env = process.env) {
25
- const override = trimmed(env.TOKENUSAGE_ANTIGRAVITY_HOME);
26
- if (override) return [path.join(override, "skills")];
27
-
28
- const home = geminiHome(env);
29
- const mainSkills = path.join(home, MAIN_APP_SUBDIR, "skills");
30
- const ideSkills = path.join(home, IDE_SUBDIR, "skills");
31
- const mainParent = path.join(home, MAIN_APP_SUBDIR);
32
- const ideParent = path.join(home, IDE_SUBDIR);
33
-
34
- const dirs = [];
35
- if (existsDir(mainParent)) dirs.push(mainSkills);
36
- if (existsDir(ideParent)) dirs.push(ideSkills);
37
-
38
- // Neither Antigravity install present: fall back to the main-app path so
39
- // targetList still surfaces a stable path string and the user can install
40
- // skills now and run Antigravity later. The empty parent will be created on
41
- // demand by ensureDir; cleanup happens naturally via uninstallSkill.
42
- if (dirs.length > 0) return dirs;
43
- return [mainSkills];
44
- }
45
-
46
- module.exports = {
47
- resolveAntigravitySkillDirs,
48
- };
@@ -1,138 +0,0 @@
1
- const fs = require("node:fs/promises");
2
- const { createHash } = require("node:crypto");
3
- const { StringDecoder } = require("node:string_decoder");
4
- const { setImmediate: yieldToRequests } = require("node:timers/promises");
5
-
6
- const CHUNK_BYTES = 64 * 1024;
7
-
8
- function signature(stat) {
9
- return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
10
- }
11
-
12
- // One in-flight read per path shares the dashboard's request fan-out. Parsing
13
- // yields between bounded chunks so a cold/replaced queue cannot monopolize the
14
- // HTTP event loop. Keep committed newline-terminated rows separate from a tail
15
- // that a concurrent writer may still be completing.
16
- function createAsyncQueueReader({ parseRow, label, maxEntries = 4 }) {
17
- const cache = new Map();
18
- const pending = new Map();
19
-
20
- async function hashPrefix(file, length) {
21
- const hash = createHash("sha256");
22
- const buffer = Buffer.allocUnsafe(CHUNK_BYTES);
23
- let position = 0;
24
- while (position < length) {
25
- const { bytesRead } = await file.read(buffer, 0, Math.min(CHUNK_BYTES, length - position), position);
26
- if (!bytesRead) throw new Error("Queue truncated during validation");
27
- hash.update(buffer.subarray(0, bytesRead));
28
- position += bytesRead;
29
- await yieldToRequests();
30
- }
31
- return hash;
32
- }
33
-
34
- async function load(queuePath) {
35
- const cached = cache.get(queuePath);
36
- let file;
37
- try {
38
- file = await fs.open(queuePath, "r");
39
- // Stat the opened file, rather than a path which can be atomically replaced
40
- // between stat and open. Its signature always describes the bytes we read.
41
- const stat = await file.stat({ bigint: true });
42
- const nextSignature = signature(stat);
43
- const identity = `${stat.dev}:${stat.ino}`;
44
- const size = Number(stat.size);
45
- if (cached?.signature === nextSignature) {
46
- // Windows can reuse mtime/ctime for same-size writes anywhere in the
47
- // file. Validate every byte before reusing the parsed snapshot.
48
- const hash = await hashPrefix(file, size);
49
- if (hash.digest("hex") === cached.digest) return cached.rows;
50
- }
51
- let offset = 0;
52
- let seen = new Map();
53
- let hash = createHash("sha256");
54
- if (cached && cached.identity === identity && size > cached.size && cached.consumedBytes > 0) {
55
- // Growth can include an interior rewrite, so verify the entire old
56
- // committed prefix before parsing only the new tail.
57
- const prefixHash = await hashPrefix(file, cached.consumedBytes);
58
- if (prefixHash.copy().digest("hex") === cached.committedDigest) {
59
- offset = cached.consumedBytes;
60
- seen = cached.seen;
61
- hash = prefixHash;
62
- }
63
- }
64
- let committedHash = hash.copy();
65
-
66
- const changes = new Map();
67
- let malformed = 0;
68
- function parse(line, target) {
69
- if (!line.trim()) return;
70
- try {
71
- const entry = parseRow(JSON.parse(line));
72
- if (entry) target.set(entry.key, entry.row);
73
- } catch { malformed++; }
74
- }
75
-
76
- const decoder = new StringDecoder("utf8");
77
- const buffer = Buffer.allocUnsafe(CHUNK_BYTES);
78
- let remainder = "";
79
- let position = offset;
80
- let consumedBytes = offset;
81
- while (position < size) {
82
- const length = Math.min(CHUNK_BYTES, size - position);
83
- const { bytesRead } = await file.read(buffer, 0, length, position);
84
- if (!bytesRead) throw new Error("Queue truncated during read");
85
- const newline = buffer.lastIndexOf(0x0a, bytesRead - 1);
86
- const chunk = buffer.subarray(0, bytesRead);
87
- if (newline >= 0) {
88
- consumedBytes = position + newline + 1;
89
- committedHash = hash.copy().update(chunk.subarray(0, newline + 1));
90
- }
91
- hash.update(chunk);
92
- position += bytesRead;
93
- remainder += decoder.write(chunk);
94
- let start = 0;
95
- let end;
96
- while ((end = remainder.indexOf("\n", start)) !== -1) {
97
- parse(remainder.slice(start, end), changes);
98
- start = end + 1;
99
- }
100
- remainder = remainder.slice(start);
101
- await yieldToRequests();
102
- }
103
- remainder += decoder.end();
104
- for (const [key, row] of changes) seen.set(key, row);
105
- const tail = new Map();
106
- parse(remainder, tail);
107
- // Legacy files without a final newline are readable, but their provisional
108
- // key never enters the committed map (completion may change that key).
109
- const rows = tail.size ? Array.from(new Map([...seen, ...tail]).values()) : Array.from(seen.values());
110
- cache.delete(queuePath);
111
- cache.set(queuePath, {
112
- signature: nextSignature, identity, size, consumedBytes,
113
- digest: hash.digest("hex"), committedDigest: committedHash.digest("hex"), seen, rows,
114
- });
115
- while (cache.size > maxEntries) cache.delete(cache.keys().next().value);
116
- if (malformed) console.error(`[LocalAPI] ${label}: skipped ${malformed} malformed line(s)`);
117
- return rows;
118
- } catch (error) {
119
- if (error.code === "ENOENT") {
120
- cache.delete(queuePath);
121
- return [];
122
- }
123
- console.error(`[LocalAPI] ${label}: failed to read queue:`, error.message);
124
- return cached?.rows || [];
125
- } finally {
126
- if (file) await file.close();
127
- }
128
- }
129
-
130
- return function read(queuePath) {
131
- if (pending.has(queuePath)) return pending.get(queuePath);
132
- const task = load(queuePath).finally(() => pending.delete(queuePath));
133
- pending.set(queuePath, task);
134
- return task;
135
- };
136
- }
137
-
138
- module.exports = { createAsyncQueueReader };
@@ -1,27 +0,0 @@
1
- const fs = require("node:fs");
2
- const path = require("node:path");
3
- const crypto = require("node:crypto");
4
-
5
- function writeJsonAtomicSync(filePath, value) {
6
- const contents = JSON.stringify(value, null, 2) + "\n";
7
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
8
- const tmp = `${filePath}.tmp.${crypto.randomUUID()}`;
9
- let fd;
10
- let created = false;
11
- try {
12
- fd = fs.openSync(tmp, "wx", 0o600);
13
- created = true;
14
- fs.writeFileSync(fd, contents, "utf8");
15
- fs.closeSync(fd);
16
- fd = undefined;
17
- // Replace the final directory entry rather than following a link that
18
- // appeared after the caller read or checked the previous configuration.
19
- fs.renameSync(tmp, filePath);
20
- created = false;
21
- } finally {
22
- if (fd !== undefined) fs.closeSync(fd);
23
- if (created) fs.unlinkSync(tmp);
24
- }
25
- }
26
-
27
- module.exports = { writeJsonAtomicSync };
@@ -1,11 +0,0 @@
1
- function parseBearerToken(authorization) {
2
- // Match only the prefix so an all-whitespace header cannot make two
3
- // overlapping quantifiers repeatedly backtrack over the same characters.
4
- const value = String(authorization || "");
5
- const prefix = /^Bearer[ \t]+/i.exec(value);
6
- if (!prefix) return null;
7
- const token = value.slice(prefix[0].length);
8
- return token && !/[\r\n]/.test(token) ? token : null;
9
- }
10
-
11
- module.exports = { parseBearerToken };