tokenusage-cli 0.2.6 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,26 @@
3
3
  本包(`tokenusage-cli`)的用户可见变更记录。发版时在顶部新增一节,
4
4
  `## <版本号>` 行会被 release workflow 取作 GitHub Release 正文。
5
5
 
6
+ ## 0.3.0
7
+
8
+ 新增三个采集来源/维度,修复 Windows 下 Qoder 会话的发现与口径问题。
9
+
10
+ - **Trae CN 本地库接入**:`resolveTraeDbPaths` 新增 `Trae CN` 安装目录
11
+ (与 `Trae` / `TRAE SOLO` 同 schema,共享应用密钥直接解密)。此前国内版
12
+ 安装的整个本地用量库被静默漏扫。
13
+ - **Qoder credits 维度**:Qoder 新版会话日志是 credits-only(token 全零),
14
+ 现按桶记录 `credits`(独立维度,不并入 token 列与成本);会话级 credits
15
+ 随 sessions 上报;sync-run 遥测新增 `credits_ingested`。
16
+ - **Qoder 会话进入 Sessions**:新增 qoder / qoder-cn 会话清单读取器
17
+ (一文件 = 一会话),并应用与桶解析器一致的 billable 门槛——
18
+ `billable:false` 行是未扣费理论值(实测占总量 81%),不计入消耗。
19
+ `SIDECAR_VERSION` 随之重建一次 sidecar。
20
+ - 模型口径:读侧按 model 小写折叠分组(大小写漂移不再拆行),展示名取
21
+ 组内 token 份额最大的写法。
22
+ - 采集期修复:会话清单缓存签名纳入 qoder 文件(否则永远复用旧 sidecar);
23
+ `listRolloutFilesDeep` 为异步且只认 `rollout-*` 文件名,qoder 改用专用
24
+ `.jsonl` 深遍历。
25
+
6
26
  ## 0.2.6
7
27
 
8
28
  移除从未接线的错误上报 SDK,安装体积与运行时副本明显变小;采集行为不变。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenusage-cli",
3
- "version": "0.2.6",
3
+ "version": "0.3.0",
4
4
  "description": "Self-hosted AI coding token usage collector for the Token Usage dashboard.",
5
5
  "keywords": [
6
6
  "token-usage",
@@ -844,6 +844,9 @@ async function cmdStatus(argv = []) {
844
844
 
845
845
  const { resolveTraeDbPaths } = require("../lib/trae-db");
846
846
  const traeDbPaths = resolveTraeDbPaths(process.env);
847
+ const traeDbAppDirs = [...new Set(traeDbPaths.map(
848
+ (dbPath) => path.basename(path.resolve(dbPath, "..", "..", "..")),
849
+ ))];
847
850
  const traeStoragePath = resolveTraeStoragePath(process.env);
848
851
  const traeInstalled = Boolean(traeStoragePath || traeDbPaths.length);
849
852
  // Render path for the entitlement snapshot: read it straight from the
@@ -1232,7 +1235,7 @@ async function cmdStatus(argv = []) {
1232
1235
  trae: traeInstalled
1233
1236
  ? {
1234
1237
  installed: true,
1235
- detail: traeDbPaths[0] || traeStoragePath,
1238
+ detail: traeDbPaths.join(", ") || traeStoragePath,
1236
1239
  usage_databases: traeDbPaths.length,
1237
1240
  usage_key_source: process.env.TOKENUSAGE_TRAE_SQLCIPHER_KEY?.trim() ? "environment" : "application",
1238
1241
  ...(traeEntitlement ? { entitlement: traeEntitlement } : {}),
@@ -1453,7 +1456,7 @@ async function cmdStatus(argv = []) {
1453
1456
  : null,
1454
1457
  traeInstalled
1455
1458
  ? traeDbPaths.length
1456
- ? `- TRAE: local usage reader, ${traeDbPaths.length} database(s) (shared application key; optional key override)`
1459
+ ? `- TRAE: local usage reader, ${traeDbPaths.length} database(s): ${traeDbAppDirs.join(", ")} (shared application key; optional key override)`
1457
1460
  : `- TRAE: plan info only, no local usage database found (${traeStoragePath})`
1458
1461
  : null,
1459
1462
  traeEntitlement
@@ -3795,7 +3795,7 @@ async function cmdSync(argv, context = {}) {
3795
3795
  // semantics). A failed upload reports the run as partial — data stays
3796
3796
  // queued locally for the next pass.
3797
3797
  if (syncRun && uploadAttempted) {
3798
- syncRun.noteUpload({ inserted: uploadResult.inserted, tokens: uploadResult.tokens || 0 });
3798
+ syncRun.noteUpload({ inserted: uploadResult.inserted, tokens: uploadResult.tokens || 0, credits: uploadResult.credits || 0 });
3799
3799
  if (uploadError) syncRun.setOutcome({ result: "partial", errorCode: "upload_failed" });
3800
3800
  }
3801
3801
 
@@ -4596,12 +4596,13 @@ async function drainQueueToCloud({ baseUrl, anonKey, deviceToken, queuePath, que
4596
4596
  let skipped = 0;
4597
4597
  let batches = 0;
4598
4598
  let tokensIngested = 0;
4599
+ let creditsIngested = 0;
4599
4600
  let cloudAccess = null;
4600
4601
  const uploadId = crypto.randomUUID();
4601
4602
  const deadline = Date.now() + maxDurationMs;
4602
4603
  let batchBudget = Math.min(maxBatches, 100);
4603
4604
  let requestSignal = null;
4604
- const resultSoFar = () => ({ inserted, skipped, batches, tokens: tokensIngested, ...(cloudAccess?.next_allowed_at ? { cloudAccess } : {}) });
4605
+ const resultSoFar = () => ({ inserted, skipped, batches, tokens: tokensIngested, credits: creditsIngested, ...(cloudAccess?.next_allowed_at ? { cloudAccess } : {}) });
4605
4606
 
4606
4607
  const queueSize = await safeStatSize(queuePath);
4607
4608
  const limit = Math.min(Math.max(1, Math.floor(Number(batchSize || 200))), MAX_INGEST_BUCKETS);
@@ -4672,9 +4673,13 @@ async function drainQueueToCloud({ baseUrl, anonKey, deviceToken, queuePath, que
4672
4673
  skipped += Number(data?.skipped || 0);
4673
4674
  // Client-side §2.11 telemetry: the total_tokens of the buckets this
4674
4675
  // request carried (a larger replay may last-wins-override them later;
4675
- // this figure never participates in reconciliation).
4676
+ // this figure never participates in reconciliation). Credits sum the
4677
+ // same way — the display-only dimension (docs/sources-capture-audit.md
4678
+ // §2) — rounded to 6 decimals to keep float noise out of the ledger.
4676
4679
  for (const bucketRow of result.buckets) {
4677
4680
  tokensIngested += Number(bucketRow?.total_tokens) || 0;
4681
+ creditsIngested =
4682
+ Math.round((creditsIngested + (Number(bucketRow?.credits) || 0)) * 1e6) / 1e6;
4678
4683
  }
4679
4684
  batches += 1;
4680
4685
 
@@ -35,6 +35,15 @@ function sanitizeBucketForUpload(bucket) {
35
35
  if (bucket.conversation_count !== undefined) sanitized.conversation_count = nonNegativeInteger(bucket.conversation_count);
36
36
  if (bucket.conversations !== undefined) sanitized.conversations = nonNegativeInteger(bucket.conversations);
37
37
  sanitized.total_cost_usd = bucket.total_cost_usd == null ? 0 : nonNegativeNumber(bucket.total_cost_usd);
38
+ // Credits (docs/cli-contract.md §2.4, 2026-10-31): optional float, dropped
39
+ // entirely when zero/invalid so rows without a credit-based provider keep
40
+ // their previous wire shape.
41
+ const credits = Number(bucket.credits);
42
+ if (Number.isFinite(credits) && credits > 0) {
43
+ sanitized.credits = Math.round(credits * 1e6) / 1e6;
44
+ } else {
45
+ delete sanitized.credits;
46
+ }
38
47
  return sanitized;
39
48
  }
40
49
 
@@ -3684,6 +3684,7 @@ async function enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets })
3684
3684
  total_cost_usd: totals.total_cost_usd || 0,
3685
3685
  usage_precision: usagePrecision || undefined,
3686
3686
  ...prioritySubsetOf(totals),
3687
+ ...creditsSubsetOf(totals),
3687
3688
  conversation_count: totals.conversation_count,
3688
3689
  }),
3689
3690
  );
@@ -3765,6 +3766,7 @@ async function enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets })
3765
3766
  total_cost_usd: unknownBucket.totals.total_cost_usd || 0,
3766
3767
  usage_precision: usagePrecision || undefined,
3767
3768
  ...prioritySubsetOf(unknownBucket.totals),
3769
+ ...creditsSubsetOf(unknownBucket.totals),
3768
3770
  conversation_count: unknownBucket.totals.conversation_count,
3769
3771
  }),
3770
3772
  );
@@ -3814,6 +3816,7 @@ async function enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets })
3814
3816
  billable_total_tokens: group.totals.billable_total_tokens ?? group.totals.total_tokens,
3815
3817
  total_cost_usd: group.totals.total_cost_usd || 0,
3816
3818
  ...prioritySubsetOf(group.totals),
3819
+ ...creditsSubsetOf(group.totals),
3817
3820
  conversation_count: group.totals.conversation_count,
3818
3821
  }),
3819
3822
  );
@@ -4405,6 +4408,7 @@ function initTotals() {
4405
4408
  billable_total_tokens: 0,
4406
4409
  total_cost_usd: 0,
4407
4410
  conversation_count: 0,
4411
+ credits: 0,
4408
4412
  };
4409
4413
  }
4410
4414
 
@@ -4440,6 +4444,14 @@ function prioritySubsetOf(totals) {
4440
4444
  return subset;
4441
4445
  }
4442
4446
 
4447
+ // Vendor credits (docs/sources-capture-audit.md §2) serialize only when
4448
+ // positive, so buckets that never see a credit-based provider keep their
4449
+ // previously persisted queuedKey and queue rows unchanged.
4450
+ function creditsSubsetOf(totals) {
4451
+ const value = Number(totals?.credits) || 0;
4452
+ return value > 0 ? { credits: value } : {};
4453
+ }
4454
+
4443
4455
  function addTotals(target, delta) {
4444
4456
  for (const [field] of PRIORITY_SUBSET_FIELDS) {
4445
4457
  const value = Number(delta?.[field]) || 0;
@@ -4456,6 +4468,9 @@ function addTotals(target, delta) {
4456
4468
  ((target.total_cost_usd || 0) + (delta.total_cost_usd || 0)) * USD_TICKS_PER_USD,
4457
4469
  ) / USD_TICKS_PER_USD;
4458
4470
  target.conversation_count += delta.conversation_count || 0;
4471
+ // Credits are a display-only dimension: summed like cost but never part of
4472
+ // any token column or sum check (docs/sources-capture-audit.md §2).
4473
+ target.credits = Math.round(((target.credits || 0) + (delta.credits || 0)) * 1e6) / 1e6;
4459
4474
  }
4460
4475
 
4461
4476
  // Signed-delta defense (audit fix): OpenCode corrections subtract a prior
@@ -4473,6 +4488,7 @@ function clampTotalsNonNegative(totals) {
4473
4488
  "total_tokens",
4474
4489
  "billable_total_tokens",
4475
4490
  "conversation_count",
4491
+ "credits",
4476
4492
  ]) {
4477
4493
  if (totals[key] != null) totals[key] = Math.max(0, totals[key]);
4478
4494
  }
@@ -4516,6 +4532,10 @@ function subtractTotals(target, totals) {
4516
4532
  0,
4517
4533
  target.conversation_count - (totals.conversation_count || 0),
4518
4534
  );
4535
+ target.credits = Math.max(
4536
+ 0,
4537
+ Math.round(((target.credits || 0) - (totals.credits || 0)) * 1e6) / 1e6,
4538
+ );
4519
4539
  }
4520
4540
 
4521
4541
  function totalsKey(totals) {
@@ -4533,7 +4553,9 @@ function totalsKey(totals) {
4533
4553
  // Appended only when a priority subset exists, so buckets that never see the
4534
4554
  // tier keep their previously persisted queuedKey and are not re-enqueued.
4535
4555
  const priority = PRIORITY_SUBSET_FIELDS.map(([field]) => Number(totals[field]) || 0);
4536
- return priority.some((value) => value > 0) ? `${base}|p:${priority.join(",")}` : base;
4556
+ const key = priority.some((value) => value > 0) ? `${base}|p:${priority.join(",")}` : base;
4557
+ const credits = Number(totals.credits) || 0;
4558
+ return credits > 0 ? `${key}|c:${credits}` : key;
4537
4559
  }
4538
4560
 
4539
4561
  function toUtcHalfHourStart(ts) {
@@ -6889,9 +6911,18 @@ function qoderNewTimestampMs(record) {
6889
6911
  || 0;
6890
6912
  }
6891
6913
 
6914
+ // Vendor credits (Qoder) ride along on token rows and are the only counter on
6915
+ // credits-only rows. They are a display-only dimension
6916
+ // (docs/sources-capture-audit.md §2): summed for visibility, never converted
6917
+ // to tokens and never merged into any token column.
6918
+ function qoderUsageCredits(usage) {
6919
+ const credits = Number(usage?.credits ?? usage?.original_credits ?? 0);
6920
+ return Number.isFinite(credits) && credits > 0 ? Math.round(credits * 1e6) / 1e6 : 0;
6921
+ }
6922
+
6892
6923
  function normalizeQoderNewTokens(usage) {
6893
6924
  if (!usage || typeof usage !== "object") return null;
6894
- const credits = Number(usage.credits ?? usage.original_credits ?? 0);
6925
+ const credits = qoderUsageCredits(usage);
6895
6926
  let input = Number(usage.input_tokens ?? 0);
6896
6927
  let cached = Number(usage.cache_read_input_tokens ?? usage.cached_tokens ?? 0);
6897
6928
  let cacheCreation = Number(usage.cache_creation_input_tokens ?? 0);
@@ -7024,9 +7055,14 @@ async function parseQoderNewIncremental({
7024
7055
  total_tokens: base.total_tokens,
7025
7056
  billable_total_tokens: base.billable_total_tokens,
7026
7057
  total_cost_usd: 0,
7058
+ credits: base.credits || 0,
7027
7059
  usage_precision: base.usage_precision || undefined,
7028
7060
  conversation_count: 1,
7029
7061
  } : {
7062
+ // Credit-only usage: the token delta stays zero (no first-party
7063
+ // credit→token rate), but the vendor credits are now recorded for
7064
+ // display instead of being dropped (2026-10-31 P1,
7065
+ // docs/sources-capture-plan.md §3).
7030
7066
  input_tokens: 0,
7031
7067
  cached_input_tokens: 0,
7032
7068
  cache_creation_input_tokens: 0,
@@ -7035,6 +7071,7 @@ async function parseQoderNewIncremental({
7035
7071
  total_tokens: 0,
7036
7072
  billable_total_tokens: 0,
7037
7073
  total_cost_usd: 0,
7074
+ credits: qoderUsageCredits(usage),
7038
7075
  conversation_count: 1,
7039
7076
  };
7040
7077
  let projectKey = null;
@@ -87,7 +87,11 @@ const {
87
87
  // v23 excludes database updates and user records from request durations.
88
88
  // v24 keeps OpenCode usage dates at event time and fork history on its ancestor.
89
89
  // v25 adds Kimi wire sessions, including official/legacy migration dedup.
90
- const SIDECAR_VERSION = 25;
90
+ // 26: qoder session rows now apply the same billable gate as the token-side
91
+ // bucket parser — billable:false rows carry un-billed "would-be" credits
92
+ // (observed: 3718 of 4591 credits on this machine) and must not inflate the
93
+ // session view. Bumped so existing sidecars rebuild once.
94
+ const SIDECAR_VERSION = 26;
91
95
  const EDIT_TOOLS = new Set([
92
96
  "apply_patch",
93
97
  "edit",
@@ -1935,6 +1939,122 @@ async function scanKimiSessions(filePaths) {
1935
1939
  }));
1936
1940
  }
1937
1941
 
1942
+ // Qoder's flat per-session JSONL: one file per session id, assistant messages
1943
+ // carry Anthropic-style usage — credits-only on current builds (2026-10-31).
1944
+ // Sessions sum both dimensions; the credits figure rides the row as
1945
+ // display-only telemetry and never feeds the token identity.
1946
+ function clampQoderCounter(value) {
1947
+ const parsed = Number(value);
1948
+ return Number.isFinite(parsed) && parsed > 0 ? Math.trunc(parsed) : 0;
1949
+ }
1950
+
1951
+ function qoderTimestampMs(record) {
1952
+ const raw = record?.timestamp ?? record?.message?.timestamp;
1953
+ if (typeof raw === "number" && Number.isFinite(raw)) {
1954
+ return raw < 1e12 ? Math.trunc(raw * 1000) : Math.trunc(raw);
1955
+ }
1956
+ if (typeof raw === "string") {
1957
+ const parsed = Date.parse(raw);
1958
+ if (Number.isFinite(parsed)) return parsed;
1959
+ const numeric = Number(raw);
1960
+ if (Number.isFinite(numeric) && raw.trim() !== "") {
1961
+ return numeric < 1e12 ? Math.trunc(numeric * 1000) : Math.trunc(numeric);
1962
+ }
1963
+ }
1964
+ return null;
1965
+ }
1966
+
1967
+ function scanQoderSessionFile(filePath, source) {
1968
+ let lines;
1969
+ try {
1970
+ lines = fs.readFileSync(filePath, "utf8").split("\n");
1971
+ } catch (error) {
1972
+ if (isRecoverableSessionReadError(error)) throw error;
1973
+ throw error;
1974
+ }
1975
+ const sessionId = path.basename(filePath).replace(/\.jsonl$/i, "");
1976
+ if (!sessionId) throw new Error("qoder session file without an id");
1977
+
1978
+ let projectRef = null;
1979
+ let startedMs = null;
1980
+ let endedMs = null;
1981
+ let credits = 0;
1982
+ const tokens = {
1983
+ input_tokens: 0,
1984
+ cached_input_tokens: 0,
1985
+ cache_creation_input_tokens: 0,
1986
+ output_tokens: 0,
1987
+ reasoning_output_tokens: 0,
1988
+ };
1989
+
1990
+ for (const line of lines) {
1991
+ if (!line.trim()) continue;
1992
+ let record;
1993
+ try { record = JSON.parse(line); } catch { continue; }
1994
+ if (record?.type === "workspace-directories") {
1995
+ const cwd = Array.isArray(record.directories) ? record.directories[0] : null;
1996
+ if (typeof cwd === "string" && cwd.trim()) projectRef = cwd.trim();
1997
+ continue;
1998
+ }
1999
+ if (record?.type !== "assistant" || record?.message?.role !== "assistant") continue;
2000
+ const usage = record.message.usage;
2001
+ if (!usage || typeof usage !== "object") continue;
2002
+ const timestamp = qoderTimestampMs(record);
2003
+ if (timestamp == null) continue;
2004
+
2005
+ // Same gate as the bucket parser (normalizeQoderNewTokens + its caller):
2006
+ // a billable:false row with no tokens is un-billed internal activity —
2007
+ // neither conversation nor credits — and must not reach either view.
2008
+ const rowTokens = {
2009
+ input_tokens: clampQoderCounter(usage.input_tokens),
2010
+ cached_input_tokens: clampQoderCounter(usage.cache_read_input_tokens ?? usage.cached_tokens),
2011
+ cache_creation_input_tokens: clampQoderCounter(usage.cache_creation_input_tokens),
2012
+ output_tokens: clampQoderCounter(usage.output_tokens),
2013
+ reasoning_output_tokens: 0,
2014
+ };
2015
+ const hasTokens =
2016
+ rowTokens.input_tokens > 0 || rowTokens.cached_input_tokens > 0 ||
2017
+ rowTokens.cache_creation_input_tokens > 0 || rowTokens.output_tokens > 0;
2018
+ const isBillable = usage.billable !== false;
2019
+ if (!hasTokens && !isBillable) continue;
2020
+
2021
+ tokens.input_tokens += rowTokens.input_tokens;
2022
+ tokens.cached_input_tokens += rowTokens.cached_input_tokens;
2023
+ tokens.cache_creation_input_tokens += rowTokens.cache_creation_input_tokens;
2024
+ tokens.output_tokens += rowTokens.output_tokens;
2025
+ tokens.reasoning_output_tokens += rowTokens.reasoning_output_tokens;
2026
+ const creditValue = Number(usage.credits ?? usage.original_credits ?? 0);
2027
+ credits = Math.round((credits + (Number.isFinite(creditValue) && creditValue > 0 ? creditValue : 0)) * 1e6) / 1e6;
2028
+
2029
+ if (startedMs == null || timestamp < startedMs) startedMs = timestamp;
2030
+ if (endedMs == null || timestamp > endedMs) endedMs = timestamp;
2031
+ }
2032
+
2033
+ if (endedMs == null) return null; // no timestamped usage in this file
2034
+
2035
+ const totalTokens =
2036
+ tokens.input_tokens + tokens.cached_input_tokens +
2037
+ tokens.cache_creation_input_tokens + tokens.output_tokens +
2038
+ tokens.reasoning_output_tokens;
2039
+
2040
+ return finalizeRecord({
2041
+ version: SIDECAR_VERSION,
2042
+ session_hash: sessionHash(source, sessionId),
2043
+ session_id: sessionId,
2044
+ source,
2045
+ ...sessionStorageUsage([filePath]),
2046
+ project_key: projectKey(projectRef, filePath),
2047
+ project_ref: projectRef || null,
2048
+ tokens,
2049
+ total_tokens: totalTokens,
2050
+ credits,
2051
+ started_at: new Date(startedMs).toISOString(),
2052
+ ended_at: new Date(endedMs).toISOString(),
2053
+ active_ms: Math.max(0, endedMs - startedMs),
2054
+ provenance: { source: "local-session-log", confidence: "observed", retry_confidence: "inferred", content_retained: false },
2055
+ });
2056
+ }
2057
+
1938
2058
  async function discoverSessionFiles(home, env = process.env, deps = {}) {
1939
2059
  const grokHome = resolveGrokHome(home);
1940
2060
  // config.scanRoots (#657): read from the tracker config unless the caller
@@ -2027,7 +2147,42 @@ async function discoverSessionFiles(home, env = process.env, deps = {}) {
2027
2147
  return roots;
2028
2148
  });
2029
2149
  const kimi = discoverKimiWireFiles(dedupeDirsByRealpath(kimiRoots), incomplete);
2030
- return { claude, codex: groupCodexFiles([...codex, ...archived]), grok, zcode, opencode, kimi, incomplete };
2150
+ // Qoder's flat per-session JSONL (one file = one session id) carries the
2151
+ // same usage shape as the token side — including credits-only rows, so the
2152
+ // session rows sum credits alongside tokens (2026-10-31). listRolloutFilesDeep
2153
+ // is codex-specific (rollout-* names), so qoder walks its own tree.
2154
+ const qoder = (await Promise.all(
2155
+ [
2156
+ ...providerRoots(home, ".qoder", env, deps).map((root) => ({ root, source: "qoder" })),
2157
+ ...providerRoots(home, ".qoder-cn", env, deps).map((root) => ({ root, source: "qoder-cn" })),
2158
+ ].map(async ({ root, source }) =>
2159
+ (await listQoderSessionFiles(path.join(root, "projects")))
2160
+ .map((filePath) => ({ filePath, source })),
2161
+ ),
2162
+ )).flat();
2163
+ return { claude, codex: groupCodexFiles([...codex, ...archived]), grok, zcode, opencode, kimi, qoder, incomplete };
2164
+ }
2165
+
2166
+ // Qoder session files: `<projects>/<slug>/<sessionId>.jsonl` — any .jsonl is
2167
+ // a session transcript, so the walk is generic (unlike codex's rollout-* rule).
2168
+ async function listQoderSessionFiles(projectsDir) {
2169
+ const out = [];
2170
+ async function walk(dir) {
2171
+ let entries;
2172
+ try {
2173
+ entries = await fsp.readdir(dir, { withFileTypes: true });
2174
+ } catch {
2175
+ return; // absent or unreadable: an empty result, reported as such upstream
2176
+ }
2177
+ for (const entry of entries) {
2178
+ const p = path.join(dir, entry.name);
2179
+ if (entry.isDirectory()) await walk(p);
2180
+ else if (entry.isFile() && entry.name.endsWith(".jsonl")) out.push(p);
2181
+ }
2182
+ }
2183
+ await walk(projectsDir);
2184
+ out.sort((a, b) => a.localeCompare(b));
2185
+ return out;
2031
2186
  }
2032
2187
 
2033
2188
  // Warn once per unreadable directory per process: the dashboard polls this
@@ -2215,6 +2370,7 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2215
2370
  ...discovered.grok.map(grokSignalsPathFor),
2216
2371
  // db + its WAL sidecars: the trio is one logical file for change detection.
2217
2372
  ...[...(discovered.zcode || []), ...(discovered.opencode || [])].flatMap((dbPath) => [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]),
2373
+ ...discovered.qoder.map(({ filePath }) => filePath),
2218
2374
  ]);
2219
2375
  if (!force && !retryZcodeScan && !retryOpencodeScan && !retryKimiScan && previousMeta?.version === SIDECAR_VERSION && previousMeta.signature === signature) {
2220
2376
  await writeAtomic(metaPath, `${JSON.stringify({ ...previousMeta, checked_at: new Date().toISOString() })}\n`);
@@ -2239,6 +2395,11 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2239
2395
  ...(discovered.zcode.length ? [{ source: "zcode", filePath: discovered.zcode, scan: scanZcodeSessions }] : []),
2240
2396
  ...(discovered.opencode.length ? [{ source: "opencode", filePath: discovered.opencode, scan: scanOpencodeSessions }] : []),
2241
2397
  ...(discovered.kimi.length ? [{ source: "kimi", filePath: discovered.kimi, scan: scanKimiSessions }] : []),
2398
+ ...discovered.qoder.map(({ filePath, source }) => ({
2399
+ source,
2400
+ filePath,
2401
+ scan: (file) => scanQoderSessionFile(file, source),
2402
+ })),
2242
2403
  ];
2243
2404
  // Files we could not turn into a row (permission denied, half-written line,
2244
2405
  // vanished mid-scan). Swallowing these silently made sessions disappear with
@@ -2247,6 +2408,7 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2247
2408
  let incompleteZcodeScan = false;
2248
2409
  let incompleteOpencodeScan = false;
2249
2410
  let incompleteKimiScan = false;
2411
+ let incompleteQoderScan = false;
2250
2412
  for (const entry of entries) {
2251
2413
  const cacheKey = sessionFileCacheKey(entry.source, entry.filePath);
2252
2414
  const statKey = analyticsEntryStatKey(entry.source, entry.filePath);
@@ -2255,6 +2417,7 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2255
2417
  if (entry.source === "zcode") incompleteZcodeScan = true;
2256
2418
  if (entry.source === "opencode") incompleteOpencodeScan = true;
2257
2419
  if (entry.source === "kimi") incompleteKimiScan = true;
2420
+ if (entry.source === "qoder" || entry.source === "qoder-cn") incompleteQoderScan = true;
2258
2421
  continue;
2259
2422
  }
2260
2423
  let rows = null;
@@ -2280,6 +2443,7 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2280
2443
  if (entry.source === "zcode") incompleteZcodeScan = true;
2281
2444
  if (entry.source === "opencode") incompleteOpencodeScan = true;
2282
2445
  if (entry.source === "kimi") incompleteKimiScan = true;
2446
+ if (entry.source === "qoder" || entry.source === "qoder-cn") incompleteQoderScan = true;
2283
2447
  if (!process.env.NODE_TEST_CONTEXT) {
2284
2448
  console.warn(`[session-analytics] skipped ${entry.source} session file: ${error?.message || error}`);
2285
2449
  }
@@ -2317,10 +2481,14 @@ async function buildSessionAnalyticsInternal({ home = os.homedir(), force = fals
2317
2481
  await writeAtomic(incompleteKimiPath, "1\n");
2318
2482
  sessions.push(...readSidecar(sidecarPath).filter((row) => row.source === "kimi" && row.version === SIDECAR_VERSION));
2319
2483
  }
2484
+ if (incompleteQoderScan) {
2485
+ sessions.push(...readSidecar(sidecarPath).filter((row) =>
2486
+ (row.source === "qoder" || row.source === "qoder-cn") && row.version === SIDECAR_VERSION));
2487
+ }
2320
2488
  sessions.sort((a, b) => String(b.ended_at || "").localeCompare(String(a.ended_at || "")));
2321
2489
  // A failed database scan is an incomplete inventory. Keep the last complete
2322
2490
  // snapshot and signature so an unchanged database is retried next build.
2323
- if (incompleteDirs.length === 0 && !incompleteZcodeScan && !incompleteOpencodeScan && !incompleteKimiScan) {
2491
+ if (incompleteDirs.length === 0 && !incompleteZcodeScan && !incompleteOpencodeScan && !incompleteKimiScan && !incompleteQoderScan) {
2324
2492
  const content = sessions.map(serializeSessionRecord).join("\n") + (sessions.length ? "\n" : "");
2325
2493
  await writeAtomic(sidecarPath, content);
2326
2494
  const generatedAt = new Date().toISOString();
@@ -186,6 +186,12 @@ function mapSessionRow(row) {
186
186
  project_key: basenameKey(row.project_key ?? row.project_ref),
187
187
  ...tokens,
188
188
  billable_total_tokens: null,
189
+ // Session-level credits (docs/cli-contract.md §2.4, 2026-10-31): optional
190
+ // display-only telemetry; old backends ignore it, so it rides only when
191
+ // the sidecar carried a positive figure.
192
+ ...(Number(row.credits) > 0
193
+ ? { credits: Math.round(Number(row.credits) * 1e6) / 1e6 }
194
+ : {}),
189
195
  ...(models.length > 0 ? { models } : {}),
190
196
  };
191
197
  }
@@ -49,6 +49,7 @@ function createSyncRunTelemetry({
49
49
  let parsedFiles = 0;
50
50
  let bucketsUploaded = 0;
51
51
  let tokensIngested = 0;
52
+ let creditsIngested = 0;
52
53
  let sessionsUploaded = 0;
53
54
  let sessionsSkipped = 0;
54
55
  let queueBytes = null;
@@ -64,10 +65,14 @@ function createSyncRunTelemetry({
64
65
  },
65
66
  // Server-acked drain result: buckets placed into successful ingest
66
67
  // requests plus their total_tokens sum (client-side telemetry semantics
67
- // per §2.11 — a larger replay may override these buckets later).
68
- noteUpload({ inserted = 0, tokens = 0 } = {}) {
68
+ // per §2.11 — a larger replay may override these buckets later). Credits
69
+ // (docs/sources-capture-audit.md §2) sum identically as display-only
70
+ // telemetry.
71
+ noteUpload({ inserted = 0, tokens = 0, credits = 0 } = {}) {
69
72
  bucketsUploaded += Math.max(0, Number(inserted) || 0);
70
73
  tokensIngested += Math.max(0, Number(tokens) || 0);
74
+ creditsIngested =
75
+ Math.round((creditsIngested + Math.max(0, Number(credits) || 0)) * 1e6) / 1e6;
71
76
  },
72
77
  noteSessions({ upserted = 0, skipped = 0 } = {}) {
73
78
  sessionsUploaded += Math.max(0, Number(upserted) || 0);
@@ -91,6 +96,7 @@ function createSyncRunTelemetry({
91
96
  parsed_files: parsedFiles,
92
97
  buckets_uploaded: bucketsUploaded,
93
98
  tokens_ingested: tokensIngested,
99
+ ...(creditsIngested > 0 ? { credits_ingested: creditsIngested } : {}),
94
100
  sessions_uploaded: sessionsUploaded,
95
101
  sessions_skipped: sessionsSkipped,
96
102
  queue_bytes: queueBytes,
@@ -21,7 +21,12 @@ function resolveTraeDbPaths(env = process.env, { platform = process.platform, ho
21
21
  : platform === "darwin"
22
22
  ? path.join(home, "Library", "Application Support")
23
23
  : env.XDG_CONFIG_HOME || path.join(home, ".config");
24
- const roots = customHome ? [customHome] : [path.join(parent, "Trae"), path.join(parent, "TRAE SOLO")];
24
+ // The CN IDE build installs as "Trae CN" with the same ModularData schema;
25
+ // its database joins the same source ledger as a separate store (per-store
26
+ // turn identity in parseTraeIncremental), so no separate source id is needed.
27
+ const roots = customHome
28
+ ? [customHome]
29
+ : [path.join(parent, "Trae"), path.join(parent, "TRAE SOLO"), path.join(parent, "Trae CN")];
25
30
  const result = [];
26
31
  for (const root of roots) {
27
32
  // A decrypted sibling may be an old export. Only an explicit DB override
@@ -0,0 +1,67 @@
1
+ const test = require("node:test");
2
+ const assert = require("node:assert/strict");
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+
7
+ const { resolveTraeDbPaths } = require("../src/lib/trae-db");
8
+
9
+ function makeStoreDb(parent, module = "ai-agent") {
10
+ const dir = path.join(parent, "ModularData", module);
11
+ fs.mkdirSync(dir, { recursive: true });
12
+ const file = path.join(dir, "database.db");
13
+ fs.writeFileSync(file, "");
14
+ return file;
15
+ }
16
+
17
+ test("resolveTraeDbPaths discovers the international, SOLO, and CN stores on win32", (t) => {
18
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), "trae-db-home-"));
19
+ const appdata = fs.mkdtempSync(path.join(os.tmpdir(), "trae-db-app-"));
20
+ t.after(() => {
21
+ fs.rmSync(home, { recursive: true, force: true });
22
+ fs.rmSync(appdata, { recursive: true, force: true });
23
+ });
24
+ const international = makeStoreDb(path.join(appdata, "Trae"));
25
+ const solo = makeStoreDb(path.join(appdata, "TRAE SOLO"));
26
+ const cn = makeStoreDb(path.join(appdata, "Trae CN"));
27
+
28
+ const paths = resolveTraeDbPaths({ APPDATA: appdata }, { platform: "win32", home });
29
+
30
+ assert.deepEqual(paths, [international, solo, cn]);
31
+ });
32
+
33
+ test("resolveTraeDbPaths prefers the ai-agent module over ai-chat", (t) => {
34
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), "trae-db-home-"));
35
+ const appdata = fs.mkdtempSync(path.join(os.tmpdir(), "trae-db-app-"));
36
+ t.after(() => {
37
+ fs.rmSync(home, { recursive: true, force: true });
38
+ fs.rmSync(appdata, { recursive: true, force: true });
39
+ });
40
+ makeStoreDb(path.join(appdata, "Trae CN"), "ai-chat");
41
+ const agent = makeStoreDb(path.join(appdata, "Trae CN"), "ai-agent");
42
+
43
+ const paths = resolveTraeDbPaths({ APPDATA: appdata }, { platform: "win32", home });
44
+
45
+ assert.deepEqual(paths, [agent]);
46
+ });
47
+
48
+ test("resolveTraeDbPaths discovers the CN store on darwin", (t) => {
49
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), "trae-db-home-"));
50
+ t.after(() => fs.rmSync(home, { recursive: true, force: true }));
51
+ const cn = makeStoreDb(path.join(home, "Library", "Application Support", "Trae CN"));
52
+
53
+ const paths = resolveTraeDbPaths({}, { platform: "darwin", home });
54
+
55
+ assert.deepEqual(paths, [cn]);
56
+ });
57
+
58
+ test("resolveTraeDbPaths honors an explicit DB override over discovery", () => {
59
+ const override = path.join(os.tmpdir(), "trae-db-explicit", "database.db");
60
+
61
+ const paths = resolveTraeDbPaths(
62
+ { TOKENUSAGE_TRAE_DB: override },
63
+ { platform: "win32", home: os.tmpdir() },
64
+ );
65
+
66
+ assert.deepEqual(paths, [path.resolve(override)]);
67
+ });