tokenusage-cli 0.2.4 → 0.2.5

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,22 @@
3
3
  本包(`tokenusage-cli`)的用户可见变更记录。发版时在顶部新增一节,
4
4
  `## <版本号>` 行会被 release workflow 取作 GitHub Release 正文。
5
5
 
6
+ ## 0.2.5
7
+
8
+ 0.2.4 验收后的小修:去掉安装警告、修正初始化重复文案,并补 Windows CI。
9
+
10
+ - 移除 `@mongodb-js/zstd` 与两处 Node 20 zstd 回落分支:包 `engines` 已是
11
+ `>=22.19`,`node:zlib` 自带 zstd。`npm i -g` 不再出现 `prebuild-install`
12
+ 弃用警告,运行时副本也少一个 native 依赖。
13
+ - 修 `init` 跳过行重复文案:detail 已以 `Skipped` 开头时不再叠加
14
+ `Skipped - ` 前缀(此前显示 `Skipped - Skipped (--no-integrations)`)。
15
+ - 顺手去掉 `--dry-run` 收尾的重复 `Dry run complete` 行,只保留
16
+ `Run init without --dry-run to apply changes.`。
17
+ - README 补齐 `init` 的 `--yes` / `--dry-run` / `--no-open` / `--link-code`
18
+ 参数说明。
19
+ - CI 新增 `windows-latest` job:跑 CLI 测试 + `init --dry-run` 烟测并断言
20
+ 输出不含 `Skipped - Skipped`,避免 VBS / 入口类回归只靠发版前人工验收。
21
+
6
22
  ## 0.2.4
7
23
 
8
24
  计划任务抗 Node 版本切换 + 自检一次性收口的采集可靠性修复(发 0.2.3 后立即补;
package/README.md CHANGED
@@ -30,7 +30,7 @@ tokenusage sync --drain # 首次/积压补传(更大的批处理预算)
30
30
 
31
31
  | 命令 | 说明 |
32
32
  |---|---|
33
- | `tokenusage init` | 本地初始化:扫描根配置、各工具 hook 安装、注册每小时计划任务(Windows 隐藏窗口;`--no-task` 跳过任务;`--run-report=off` 关闭运行记录上报;`--no-integrations` 不装任何 hook/notify/插件,只做纯定时采集)。不含配对 |
33
+ | `tokenusage init` | 本地初始化:扫描根配置、各工具 hook 安装、注册每小时计划任务(Windows 隐藏窗口)。`--no-task` 跳过任务;`--run-report=off` 关闭运行记录上报;`--no-integrations` 不装任何 hook/notify/插件,只做纯定时采集;`--yes` 非交互;`--dry-run` 只预览不落盘;`--no-open` 不打开浏览器;`--link-code <code>` 用已有配对码。不含配对 |
34
34
  | `tokenusage device-login` | 设备流配对:输出 `user_code`,在站点 `/device` 页批准后本机拿到设备令牌 |
35
35
  | `tokenusage sync` | 解析本地 AI 工具日志 → 上报小时桶 + 会话元数据 |
36
36
  | `tokenusage sync --watch` | 常驻模式,默认每 15 分钟增量一轮(`--interval <ms>` 可调,≥60000) |
@@ -107,7 +107,21 @@ CLI 也不发送匿名安装心跳:上游的 `tokenusage-telemetry` 端点在
107
107
 
108
108
  ## 数据目录
109
109
 
110
- `~/.tokenusage/`(`0700` 目录 + `0600` 文件):`tracker/config.json`(配对与运行配置)、上传队列(`queue.jsonl` + offset)、会话分析 sidecar、`session-upload-watermarks.json`(增量水位)、`tracker/logs/sync.log`(自动采集滚动日志)、暂停标志。与旧版 tokentracker CLI 的 `~/.tokentracker/` 互不干扰,迁移期两套 CLI 可在同一台机器共存。
110
+ `~/.tokenusage/`(`0700` 目录 + `0600` 文件):`tracker/config.json`(配对与运行配置)、`tracker/app/`(自包含运行时副本,`.runtime-ready` 记录版本)、上传队列(`queue.jsonl` + offset)、会话分析 sidecar、`session-upload-watermarks.json`(增量水位)、`tracker/logs/sync.log`(自动采集滚动日志)、暂停标志。与旧版 tokentracker CLI 的 `~/.tokentracker/` 互不干扰,迁移期两套 CLI 可在同一台机器共存。
111
+
112
+ ## 自包含运行时副本(0.2.4+)
113
+
114
+ init 会把当前 CLI 及其依赖闭包复制到 `~/.tokenusage/tracker/app`,并写入
115
+ `.runtime-ready`(版本 + 安装时间)。计划任务 / notify / hook 优先启动已安装包;
116
+ 包路径失效时(典型是 nvm-windows 切换 Node 版本后全局前缀变化)自动回落到副本,
117
+ 小时任务不再 `file not found`。副本先复制到 `app.new` 暂存,`require` 全部直接
118
+ 依赖且 `--version` 实跑通过后才替换并写标记;没有标记的副本不会被任何自动采集
119
+ 入口使用。
120
+
121
+ 升级 CLI 或切换 Node 版本后重跑一次
122
+ `tokenusage init --yes --no-integrations --no-open` 刷新副本;`tokenusage doctor`
123
+ 的 `runtime.copy` 检查会报告副本缺失、缺标记或版本落后,`uninstall --purge`
124
+ 连同数据目录一并清理。
111
125
 
112
126
  ## provider 支持面
113
127
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenusage-cli",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Self-hosted AI coding token usage collector for the Token Usage dashboard.",
5
5
  "keywords": [
6
6
  "token-usage",
@@ -43,7 +43,6 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@journeyapps/wa-sqlite": "2.0.7",
46
- "@mongodb-js/zstd": "^2.0.1",
47
46
  "@sentry/node": "^11.8.0",
48
47
  "smol-toml": "^1.9.0",
49
48
  "undici": "8.12.2",
@@ -51,7 +50,6 @@
51
50
  },
52
51
  "pnpm": {
53
52
  "onlyBuiltDependencies": [
54
- "@mongodb-js/zstd",
55
53
  "@journeyapps/wa-sqlite",
56
54
  "esbuild"
57
55
  ]
@@ -332,7 +332,7 @@ function renderAccountNotLinked({ context } = {}) {
332
332
  process.stdout.write(
333
333
  [
334
334
  "",
335
- "Dry run complete. Run init without --dry-run to apply changes.",
335
+ "Run init without --dry-run to apply changes.",
336
336
  "",
337
337
  ].join("\n"),
338
338
  );
package/src/lib/cli-ui.js CHANGED
@@ -155,7 +155,13 @@ function createSpinner({ text, intervalMs = 80 }) {
155
155
  function formatSummaryLine({ label, status, detail }) {
156
156
  const isSuccess = status === "updated" || status === "set" || status === "installed";
157
157
  const bullet = isSuccess ? color("*", GREEN) : "o";
158
- const statusLabel = isSuccess ? detail || status : detail ? `Skipped - ${detail}` : "Skipped";
158
+ const statusLabel = isSuccess
159
+ ? detail || status
160
+ : !detail
161
+ ? "Skipped"
162
+ : /^Skipped\b/i.test(detail)
163
+ ? detail
164
+ : `Skipped - ${detail}`;
159
165
  const line = ` ${bullet} ${label.padEnd(22)} [${statusLabel}]`;
160
166
  return isSuccess ? line : color(line, DIM);
161
167
  }
@@ -190,8 +190,8 @@ function isRolloutFileName(name) {
190
190
  // Compressed rollout archives cannot be tailed by byte offset: the cursor
191
191
  // stores compressed bytes while the parser consumes decompressed text. Treat
192
192
  // them as content-addressed snapshots — one parse per (inode, size) version —
193
- // reusing the DSH session's zstd machinery (built-in zlib first,
194
- // `@mongodb-js/zstd` fallback) including its multi-frame handling. Returns
193
+ // reusing the DSH session's built-in zlib zstd machinery, including its
194
+ // multi-frame handling. Returns
195
195
  // null for a missing/non-file path; an empty archive yields no lines (the
196
196
  // same outcome as an empty uncompressed rollout).
197
197
  const CODEX_ROLLOUT_COMPRESSED_PATTERN = /^rollout-.*\.jsonl\.zstd?$/;
@@ -13174,10 +13174,18 @@ function resolveZedDbPath(env = process.env) {
13174
13174
  return path.join(xdg, "zed", "threads", "threads.db");
13175
13175
  }
13176
13176
 
13177
+ function decompressBuiltinZstd(data) {
13178
+ const zlib = require("node:zlib");
13179
+ if (typeof zlib.zstdDecompressSync !== "function") {
13180
+ throw new Error("built-in zstd is unavailable; Node.js >= 22.19 is required");
13181
+ }
13182
+ return Buffer.from(zlib.zstdDecompressSync(data));
13183
+ }
13184
+
13177
13185
  // Decode a row's BLOB payload into UTF-8 JSON text. Zed marks zstd-compressed
13178
13186
  // blobs with data_type="zstd"; older / smaller threads use data_type="json"
13179
- // and store the bytes verbatim. Node 24+ has native zstd; Node 20 needs the
13180
- // @mongodb-js/zstd fallback. Cap decoded size to mirror tokscale's safety net.
13187
+ // and store the bytes verbatim. Node >=22.19 already ships zstd; the package
13188
+ // engine floor guarantees it. Cap decoded size to mirror tokscale's safety net.
13181
13189
  async function decodeZedThreadBlob({ dataType, data }) {
13182
13190
  const type = (dataType || "").trim().toLowerCase();
13183
13191
  if (type === "json") {
@@ -13187,11 +13195,7 @@ async function decodeZedThreadBlob({ dataType, data }) {
13187
13195
  return data.toString("utf8");
13188
13196
  }
13189
13197
  if (type === "zstd") {
13190
- const zlib = require("node:zlib");
13191
- const out =
13192
- typeof zlib.zstdDecompressSync === "function"
13193
- ? zlib.zstdDecompressSync(data)
13194
- : Buffer.from(await require("@mongodb-js/zstd").decompress(data));
13198
+ const out = decompressBuiltinZstd(data);
13195
13199
  if (out.length > MAX_ZED_THREAD_JSON_BYTES) {
13196
13200
  throw new Error(`decoded zstd blob exceeds ${MAX_ZED_THREAD_JSON_BYTES} bytes`);
13197
13201
  }
@@ -23870,8 +23874,8 @@ async function parseTraeCnApiIncremental({
23870
23874
  // Zstd artifacts are a concatenated-frame container: a single decompress call
23871
23875
  // decodes only the first frame (silently dropping every event line), so we
23872
23876
  // identify the frame boundaries and decode each frame independently —
23873
- // built-in zlib zstd first, `@mongodb-js/zstd` as the Node 20 fallback —
23874
- // enforcing a cumulative plaintext bound as we go.
23877
+ // with the built-in Node zlib zstd decoder, enforcing a cumulative plaintext
23878
+ // bound as we go.
23875
23879
  // Dedup is a per-file `lastSeq` watermark — seq
23876
23880
  // is monotonic within an append-only session log, so a grow re-reads the file
23877
23881
  // and skips everything at or below the watermark. Torn tails never advance the
@@ -24119,18 +24123,11 @@ function inspectDshZstdFrames(data, maxOutputBytes = DSH_SESSION_TEXT_MAX_BYTES)
24119
24123
  };
24120
24124
  }
24121
24125
 
24122
- // Decode one already-isolated frame. Prefer Node's built-in zstd (22.15+):
24123
- // the desktop bundles install dependencies with --ignore-scripts, so
24124
- // @mongodb-js/zstd ships without its native binding there and the require()
24125
- // would throw — silently skipping every compressed session (issue #465). The
24126
- // MongoDB binding stays as the fallback for Node 20 CLI installs, where npm
24127
- // runs install scripts normally.
24126
+ // Decode one already-isolated frame with Node's built-in zstd. The previous
24127
+ // @mongodb-js/zstd fallback was only reachable on Node 20; the package engine
24128
+ // floor is now >=22.19, so a missing built-in decoder is a hard error.
24128
24129
  async function decodeDshZstdFrame(frameBytes) {
24129
- const zlib = require("node:zlib");
24130
- if (typeof zlib.zstdDecompressSync === "function") {
24131
- return zlib.zstdDecompressSync(frameBytes);
24132
- }
24133
- return Buffer.from(await require("@mongodb-js/zstd").decompress(frameBytes));
24130
+ return decompressBuiltinZstd(frameBytes);
24134
24131
  }
24135
24132
 
24136
24133
  // Decode one independent Harness append frame at a time; per-frame decoding
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ // init summary rows: skipped call-sites already include a "Skipped (...)"
4
+ // detail, while formatSummaryLine added its own "Skipped - " prefix. The
5
+ // result was "Skipped - Skipped (--no-integrations)" (0.2.4 acceptance).
6
+ // Keep the double prefix out without changing the success rows.
7
+
8
+ const assert = require("node:assert/strict");
9
+ const { test } = require("node:test");
10
+
11
+ const { formatSummaryLine } = require("../src/lib/cli-ui");
12
+
13
+ test("a detail that already starts with Skipped is not prefixed twice", () => {
14
+ const line = formatSummaryLine({
15
+ label: "Integrations",
16
+ status: "skipped",
17
+ detail: "Skipped (--no-integrations); tool configs stay untouched",
18
+ });
19
+ assert.ok(line.includes("[Skipped (--no-integrations); tool configs stay untouched]"), line);
20
+ assert.ok(!line.includes("Skipped - Skipped"), line);
21
+ });
22
+
23
+ test("a plain skipped detail still gets the Skipped - prefix", () => {
24
+ const line = formatSummaryLine({
25
+ label: "Runtime copy",
26
+ status: "skipped",
27
+ detail: "not installed",
28
+ });
29
+ assert.ok(line.includes("[Skipped - not installed]"), line);
30
+ });
31
+
32
+ test("skipped rows without a detail keep the bare label", () => {
33
+ const line = formatSummaryLine({
34
+ label: "Scheduled task",
35
+ status: "skipped",
36
+ });
37
+ assert.ok(line.includes("[Skipped]"), line);
38
+ });
39
+
40
+ test("success rows keep their detail text untouched", () => {
41
+ const line = formatSummaryLine({
42
+ label: "Claude Code",
43
+ status: "installed",
44
+ detail: "hook -> ~/.claude/settings.json",
45
+ });
46
+ assert.ok(line.includes("[hook -> ~/.claude/settings.json]"), line);
47
+ });
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ // 0.2.5 dropped @mongodb-js/zstd: the package engine floor is >=22.19, so
4
+ // node:zlib can always decode zstd. These tests exercise the two real decode
5
+ // paths (Zed thread blobs and concatenated DeepSeek Harness frames) so a
6
+ // future dependency/engine change cannot silently remove zstd support.
7
+
8
+ const assert = require("node:assert/strict");
9
+ const zlib = require("node:zlib");
10
+ const { test } = require("node:test");
11
+
12
+ const { decodeDshZstd, decodeZedThreadBlob } = require("../src/lib/rollout");
13
+
14
+ test("the supported Node runtime has a built-in zstd decoder", () => {
15
+ assert.equal(typeof zlib.zstdDecompressSync, "function");
16
+ });
17
+
18
+ test("Zed zstd thread blobs decode through the built-in decoder", async () => {
19
+ const text = JSON.stringify({ tokens: { input: 10, output: 4 }, model: "zed-test" });
20
+ const decoded = await decodeZedThreadBlob({
21
+ dataType: "zstd",
22
+ data: zlib.zstdCompressSync(Buffer.from(text)),
23
+ });
24
+ assert.equal(decoded, text);
25
+ });
26
+
27
+ test("concatenated DeepSeek Harness zstd frames are decoded one by one", async () => {
28
+ const first = "{\"seq\":1}\n";
29
+ const second = "{\"seq\":2}\n";
30
+ const payload = Buffer.concat([
31
+ zlib.zstdCompressSync(Buffer.from(first)),
32
+ zlib.zstdCompressSync(Buffer.from(second)),
33
+ ]);
34
+ const decoded = await decodeDshZstd(payload, { maxOutputBytes: 4096 });
35
+ assert.equal(decoded.toString("utf8"), first + second);
36
+ });