tokenusage-cli 0.4.7 → 0.5.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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,42 @@
6
6
  > 0.4.x 之前的条目为压缩摘要;细节与当时的验收记录见 `docs/cli-release.md`
7
7
  > 与 `docs/archive/`。
8
8
 
9
+ ## 0.5.1
10
+
11
+ 2026-11-06。修复 Windows 计划任务在非提升权限安装下的静默降级 + `doctor` 新增计划任务自检。
12
+ 零契约变更,服务端无需同步升级。
13
+
14
+ - **修复 Windows 计划任务注册**:`<LogonTrigger>` 缺少 `<UserId>` 时 `schtasks` 按「任意用户」解析,
15
+ 非提升权限的 `/Create /XML` 直接返回 `Access is denied`,整份定义被丢弃、退回纯 `-SC HOURLY`
16
+ ——丢掉 `StartWhenAvailable` 睡眠后补跑、登录补采、失败重试与无限运行时长。现在登录触发写入
17
+ 当前用户;注册失败时先重试「无登录触发」的完整定义(保留补跑/重试/无限时长),再不行才退回
18
+ 纯每小时,摘要按原因区分 `logon_trigger_rejected` 与 `xml_rejected`。
19
+ - **`doctor` 新增 `schedule.registered` 自检**:读取已注册的计划任务(Windows 用
20
+ `schtasks /Query /XML`,macOS/Linux 检查 launchd plist / systemd timer),发现降级定义或未注册
21
+ 直接告警并提示重跑 `tokenusage init`;检查项从 16 项增至 17 项。
22
+ - **升级提示**:0.5.0 在 Windows 上装过的机器升级后重跑一次 `tokenusage init --yes`,让计划任务
23
+ 换成带当前用户的完整定义,再用 `doctor` 确认。
24
+
25
+ ## 0.5.0
26
+
27
+ 2026-11-06。定价数据刷新 + WorkBuddy 项目归属 + 仓库地址上报(契约 v3)。服务端需先部署项目仓库
28
+ 接口(`token_project_repos` / `ingest` 的 `projects` 字段);旧服务端下仓库上报按「未支持」跳过,
29
+ 其余功能不受影响。
30
+
31
+ - **定价数据重建**:两份 `seed-snapshot.json`(web + CLI)从 LiteLLM 上游重新生成
32
+ (`generated_at` 2026-05-26 → 2026-11-06,2284 → 3930 个模型:新增 1996 / 删除 350 /
33
+ 调价 264)。费用在读取时现算,历史费用会随新价目变化。
34
+ - **WorkBuddy 项目归属**:项目 pass 与总量 pass 使用同一模型表达式,项目桶因此能与小时桶
35
+ 同键拆行(项目行 + 未归因余量)。状态新增 `projectAttributionVersion: 2`:升级后第一次
36
+ 同步只重扫 WorkBuddy 的项目游标并丢弃无 model 的旧项目桶,总量游标与对账基线不变,
37
+ 历史由项目 pass 自行回填。
38
+ - **仓库地址上报(契约 v3)**:`ingest` 新增可选 `projects` 数组。CLI 只上报 github.com /
39
+ gitee.com 的规范地址,按 `project.repos.state.json` 的已确认状态差量上报;`null` 清除
40
+ 本设备此前上报的值;未确认(旧服务端的 400 / 404 / 405)视为未支持,条目留待下次同步。
41
+ 上报失败只记日志,不影响本次同步结果。
42
+ - **文档修正**:契约 §2.4 的归因覆盖表(WorkBuddy 已覆盖、`dsh` 未覆盖);`/projects`
43
+ 口径说明不再引用过时的 77%;部署指南补充 0.5.0 的收益说明。
44
+
9
45
  ## 0.4.7
10
46
 
11
47
  2026-11-05。CLI 输出面批次,零契约变更,服务端无需同步升级:命令自报设备身份 + 采集结果摘要补全。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenusage-cli",
3
- "version": "0.4.7",
3
+ "version": "0.5.1",
4
4
  "description": "Self-hosted AI coding token usage collector for the Token Usage dashboard.",
5
5
  "keywords": [
6
6
  "token-usage",
@@ -8,6 +8,7 @@ const { resolveRuntimeConfig } = require("../lib/runtime-config");
8
8
  const { resolveDeviceTokenPresence } = require("../lib/cloud-device-token");
9
9
  const { resolveLocalDeviceIdentity } = require("../lib/device-identity");
10
10
  const { buildDoctorReport } = require("../lib/doctor");
11
+ const { inspectAutoSyncSchedule } = require("../lib/scheduled-task");
11
12
 
12
13
  async function cmdDoctor(argv = []) {
13
14
  const opts = parseArgs(argv);
@@ -39,12 +40,26 @@ async function cmdDoctor(argv = []) {
39
40
  const diagnostics = await collectTrackerDiagnostics({ home });
40
41
  const cliPath = process.argv[1] ? path.resolve(process.argv[1]) : null;
41
42
  const deviceIdentity = resolveLocalDeviceIdentity({ config, home });
43
+ // Never let a scheduler probe failure take the whole report down: a broken
44
+ // schedule is exactly the state doctor should be able to describe.
45
+ let schedule = null;
46
+ try {
47
+ schedule = inspectAutoSyncSchedule({ home });
48
+ } catch (error) {
49
+ schedule = {
50
+ platform: process.platform,
51
+ label: "Scheduled sync",
52
+ state: "missing",
53
+ detail: String(error?.message || error),
54
+ };
55
+ }
42
56
 
43
57
  const report = await buildDoctorReport({
44
58
  runtime,
45
59
  diagnostics,
46
60
  fetch: globalThis.fetch,
47
61
  deviceIdentity,
62
+ schedule,
48
63
  paths: {
49
64
  trackerDir,
50
65
  configPath,
@@ -3933,6 +3933,35 @@ async function cmdSync(argv, context = {}) {
3933
3933
  }
3934
3934
  }
3935
3935
 
3936
+ // Detected project repositories (docs/cli-contract.md §2.4 `projects`,
3937
+ // contract v3). Best-effort like sessions: repo metadata never fails a run.
3938
+ // A backend that predates v3 answers the projects-only request with 400 or
3939
+ // 404/405; those entries simply stay pending until the server is upgraded.
3940
+ if (canUpload() && runtime.deviceToken && runtime.baseUrl &&
3941
+ (!isBackgroundLightweightSync || opts.publishAccount)) {
3942
+ try {
3943
+ const repoResult = await uploadProjectRepos({
3944
+ baseUrl: runtime.baseUrl,
3945
+ anonKey: runtime.anonKey,
3946
+ deviceToken: runtime.deviceToken,
3947
+ projects: cursors?.projectHourly?.projects || {},
3948
+ statePath: path.join(trackerDir, "project.repos.state.json"),
3949
+ canUpload,
3950
+ });
3951
+ if (repoResult.uploaded > 0 && opts.auto) {
3952
+ autoLog(`Project repos: ${repoResult.uploaded} updated`);
3953
+ }
3954
+ } catch (repoError) {
3955
+ // Auxiliary metadata: log, leave the confirmed state untouched so the
3956
+ // next sync retries, and keep the run's outcome unchanged.
3957
+ autoLog(`Project repo upload error: ${repoError?.message || repoError}`, {
3958
+ phase: "project-repos",
3959
+ error: String(repoError?.message || repoError),
3960
+ status: Number.isInteger(repoError?.status) ? repoError.status : null,
3961
+ });
3962
+ }
3963
+ }
3964
+
3936
3965
  const afterState = (await readJson(queueStatePath)) || { offset: 0 };
3937
3966
  const queueSize = await safeStatSize(queuePath);
3938
3967
  // Only the main queue is uploaded by drainQueueToCloud. project.queue.jsonl
@@ -4714,6 +4743,7 @@ const AUTO_RETRY_MAX_DELAY_MS = 2 * 60 * 60 * 1000;
4714
4743
  const INGEST_SLUG = "token-usage-ingest";
4715
4744
  const { buildSessionAnalytics } = require("../lib/session-analytics");
4716
4745
  const { uploadSessions } = require("../lib/session-upload");
4746
+ const { uploadProjectRepos } = require("../lib/project-repos");
4717
4747
  const MAX_INGEST_BUCKETS = 500;
4718
4748
 
4719
4749
  async function drainQueueToCloud({ baseUrl, anonKey, deviceToken, queuePath, queueStatePath, maxBatches = 5, batchSize = 200, maxDurationMs = UPLOAD_BUDGET_MS, canUpload = () => true }) {
package/src/lib/doctor.js CHANGED
@@ -11,6 +11,7 @@ async function buildDoctorReport({
11
11
  fetch = globalThis.fetch,
12
12
  now = () => new Date(),
13
13
  deviceIdentity = null,
14
+ schedule = null,
14
15
  paths = {},
15
16
  } = {}) {
16
17
  const checks = [];
@@ -29,6 +30,9 @@ async function buildDoctorReport({
29
30
  if (paths.cliPath) {
30
31
  checks.push(await checkCliEntrypoint(paths.cliPath));
31
32
  }
33
+ if (schedule) {
34
+ checks.push(buildScheduleCheck(schedule));
35
+ }
32
36
 
33
37
  checks.push(await checkNetwork({ baseUrl: runtime?.baseUrl || null, fetch }));
34
38
 
@@ -161,6 +165,57 @@ function buildDeviceIdentityChecks(deviceIdentity) {
161
165
  return checks;
162
166
  }
163
167
 
168
+ // 0.5.0 registered the plain hourly task on every non-elevated Windows
169
+ // install and only mentioned it once, in the `init` summary. A later `doctor`
170
+ // has to name the loss (no catch-up after sleep, no logon pass, no retry) or
171
+ // the only symptom is silently missing hours.
172
+ function buildScheduleCheck(schedule = {}) {
173
+ const label = schedule.label || "Scheduled sync";
174
+ const meta = {
175
+ platform: schedule.platform || null,
176
+ label: schedule.label || null,
177
+ state: schedule.state || null,
178
+ enhanced: schedule.enhanced ?? null,
179
+ };
180
+ if (schedule.state === "unsupported") {
181
+ return {
182
+ id: "schedule.registered",
183
+ status: "ok",
184
+ critical: false,
185
+ detail: `${label}: unsupported platform (the pause flag covers manual runs)`,
186
+ meta,
187
+ };
188
+ }
189
+ if (schedule.state === "missing") {
190
+ const why = schedule.detail ? ` — ${schedule.detail}` : "";
191
+ return {
192
+ id: "schedule.registered",
193
+ status: "warn",
194
+ critical: false,
195
+ detail: `${label} not registered${why}; run \`tokenusage init\` for hourly collection`,
196
+ meta,
197
+ };
198
+ }
199
+ if (schedule.enhanced) {
200
+ return {
201
+ id: "schedule.registered",
202
+ status: "ok",
203
+ critical: false,
204
+ detail: `${label}: ${schedule.detail || "registered"}`,
205
+ meta,
206
+ };
207
+ }
208
+ return {
209
+ id: "schedule.registered",
210
+ status: "warn",
211
+ critical: false,
212
+ detail:
213
+ `${label} is ${schedule.detail || "a reduced definition"}; ` +
214
+ "re-run `tokenusage init` to restore catch-up/retry (0.5.1 and later register the full definition)",
215
+ meta: { ...meta, missing: schedule.missing || [] },
216
+ };
217
+ }
218
+
164
219
  async function checkTrackerDir(trackerDir) {
165
220
  try {
166
221
  const st = await fs.stat(trackerDir);