tokenusage-cli 0.2.5 → 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 +40 -0
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/bin/tokenusage.js +0 -6
- package/package.json +1 -2
- package/src/cli.js +0 -10
- package/src/commands/init.js +0 -2
- package/src/commands/status.js +5 -2
- package/src/commands/sync.js +8 -3
- package/src/lib/ingest-sanitize.js +9 -0
- package/src/lib/rollout.js +39 -2
- package/src/lib/session-analytics.js +171 -3
- package/src/lib/session-upload.js +6 -0
- package/src/lib/sync-report.js +8 -2
- package/src/lib/trae-db.js +6 -1
- package/test/audit-fixes.test.js +2 -2
- package/test/packaging.test.js +30 -6
- package/test/trae-db.test.js +67 -0
- package/src/lib/sentry.js +0 -231
- package/src/lib/telemetry-pref.js +0 -28
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,46 @@
|
|
|
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
|
+
|
|
26
|
+
## 0.2.6
|
|
27
|
+
|
|
28
|
+
移除从未接线的错误上报 SDK,安装体积与运行时副本明显变小;采集行为不变。
|
|
29
|
+
|
|
30
|
+
- 删除 `@sentry/node` 与 `src/lib/sentry.js`、`src/lib/telemetry-pref.js`:
|
|
31
|
+
CLI 只在启动时 init 了 Sentry,`captureException` 没有任何调用点,实际从不上报;
|
|
32
|
+
但它在运行时副本里占 44 MB(67.5 MB 的约 65%),全局安装还带入
|
|
33
|
+
`@sentry/bundler-plugins` 的构建期依赖链(安装总量 93.1 MB 中约 59 MB)。
|
|
34
|
+
移除后运行时副本 22.9 MB(实测 462 个文件)、干净 prefix 安装 23 MB
|
|
35
|
+
(此前 93.1 MB),且不再有第三方上报入口。
|
|
36
|
+
- `bin/tokenusage.js` 未捕获异常的兜底里还留着一个 `require('../src/lib/sentry')`
|
|
37
|
+
(包在 try/catch 里,删模块后永不生效的残留);一并删除,守卫测试改为同时扫描
|
|
38
|
+
`src/` 与 `bin/`。
|
|
39
|
+
- README 隐私边界同步:CLI 不做匿名心跳、也不做任何第三方错误上报。
|
|
40
|
+
- `THIRD_PARTY_NOTICES.md` 的运行时依赖示例去掉 `@sentry/node`。
|
|
41
|
+
- 新增 `test/packaging.test.js` 守卫:`@sentry/*` 不得重新出现在依赖或源码中;
|
|
42
|
+
运行时副本回归测试改用 `yauzl` 的传递依赖 `pend` 验证扁平闭包复制。
|
|
43
|
+
- 未随本版改动:macOS/Linux 原生调度、`init` 逐工具集成选择(见
|
|
44
|
+
`docs/cli-release.md` §四,等真机验证/形态确认后再做)。
|
|
45
|
+
|
|
6
46
|
## 0.2.5
|
|
7
47
|
|
|
8
48
|
0.2.4 验收后的小修:去掉安装警告、修正初始化重复文案,并补 Windows CI。
|
package/README.md
CHANGED
|
@@ -103,7 +103,7 @@ CLI 未配对也能正常用,只是**降级为“只采集、不上报”**:
|
|
|
103
103
|
|
|
104
104
|
**永不上传**:prompt / 助手正文 / 工具参数 / 命令输出 / diff / 会话标题 / 本地绝对路径 / resume 命令。测试(`test/session-upload.test.js`)用字段白名单锁定该边界。
|
|
105
105
|
|
|
106
|
-
CLI
|
|
106
|
+
CLI 不发送匿名安装心跳,也不做任何第三方错误上报:上游的 `tokenusage-telemetry` 端点在自托管后端从未实现(0.2.3 移除);从未接线的 Sentry SDK 也已在 0.2.6 删除。除你自己配对的实例外,CLI 不向任何第三方发送数据。
|
|
107
107
|
|
|
108
108
|
## 数据目录
|
|
109
109
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -11,6 +11,6 @@ upstream repository and were then modified for the token-usage contract
|
|
|
11
11
|
(`docs/cli-contract.md` in the repository root). The upstream MIT license text
|
|
12
12
|
and copyright notice are preserved in this package's `LICENSE` file.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
本包不含其他第三方资产:运行时依赖(如 `undici`)的许可随
|
|
15
15
|
`node_modules` 一同分发;品牌图标、字体与定价快照属于 Web 端,登记在仓库根目录的
|
|
16
16
|
`THIRD_PARTY_NOTICES.md`。
|
package/bin/tokenusage.js
CHANGED
|
@@ -43,12 +43,6 @@ if (applyResult && applyResult.unprotected === true) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
run(argv).catch((err) => {
|
|
46
|
-
try {
|
|
47
|
-
const { captureException } = require('../src/lib/sentry');
|
|
48
|
-
captureException(err, { tags: { surface: 'cli-unhandled' } });
|
|
49
|
-
} catch {
|
|
50
|
-
/* ignore */
|
|
51
|
-
}
|
|
52
46
|
console.error(err?.stack || String(err));
|
|
53
47
|
if (debug) {
|
|
54
48
|
if (typeof err?.status === 'number') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tokenusage-cli",
|
|
3
|
-
"version": "0.
|
|
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",
|
|
@@ -43,7 +43,6 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@journeyapps/wa-sqlite": "2.0.7",
|
|
46
|
-
"@sentry/node": "^11.8.0",
|
|
47
46
|
"smol-toml": "^1.9.0",
|
|
48
47
|
"undici": "8.12.2",
|
|
49
48
|
"yauzl": "^3.4.0"
|
package/src/cli.js
CHANGED
|
@@ -9,16 +9,6 @@ const { cmdSessions } = require("./commands/sessions");
|
|
|
9
9
|
const { cmdPause, cmdResume } = require("./commands/pause");
|
|
10
10
|
|
|
11
11
|
async function run(argv) {
|
|
12
|
-
// Best-effort Sentry init (no-op without DSN / when telemetry opted out).
|
|
13
|
-
try {
|
|
14
|
-
const { initSentry } = require("./lib/sentry");
|
|
15
|
-
const { resolveTrackerPaths } = require("./lib/tracker-paths");
|
|
16
|
-
const { trackerDir } = await resolveTrackerPaths().catch(() => ({ trackerDir: null }));
|
|
17
|
-
await initSentry({ trackerDir });
|
|
18
|
-
} catch {
|
|
19
|
-
/* never block CLI startup on telemetry */
|
|
20
|
-
}
|
|
21
|
-
|
|
22
12
|
const [command, ...rest] = argv;
|
|
23
13
|
|
|
24
14
|
// No args → short help (the upstream local dashboard was removed in this
|
package/src/commands/init.js
CHANGED
|
@@ -2474,8 +2474,6 @@ function probeRuntimeCopy(appDir) {
|
|
|
2474
2474
|
"if (missing.length > 0) { console.error(missing.join(',')); process.exit(3); }",
|
|
2475
2475
|
].join("\n");
|
|
2476
2476
|
const env = { ...process.env };
|
|
2477
|
-
// The probe must not trigger telemetry or hook spawning.
|
|
2478
|
-
env.TOKENUSAGE_NO_TELEMETRY = "1";
|
|
2479
2477
|
try {
|
|
2480
2478
|
const probe = cp.spawnSync(process.execPath, ["-e", script], {
|
|
2481
2479
|
cwd: appDir,
|
package/src/commands/status.js
CHANGED
|
@@ -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
|
|
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
|
package/src/commands/sync.js
CHANGED
|
@@ -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
|
|
package/src/lib/rollout.js
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/lib/sync-report.js
CHANGED
|
@@ -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
|
-
|
|
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,
|
package/src/lib/trae-db.js
CHANGED
|
@@ -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
|
-
|
|
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
|
package/test/audit-fixes.test.js
CHANGED
|
@@ -234,8 +234,8 @@ test("init's runtime copy works from the published layout (bin/tokenusage.js)",
|
|
|
234
234
|
"declared dependency copied as a real directory",
|
|
235
235
|
);
|
|
236
236
|
assert.ok(
|
|
237
|
-
fs.existsSync(path.join(appDir, "node_modules", "
|
|
238
|
-
"transitive dependency of
|
|
237
|
+
fs.existsSync(path.join(appDir, "node_modules", "pend")),
|
|
238
|
+
"transitive dependency of yauzl copied too (flattened closure)",
|
|
239
239
|
);
|
|
240
240
|
// The marker is what every spawn point probes: without it hooks fall back
|
|
241
241
|
// to the installed CLI instead of spawning a half copy.
|
package/test/packaging.test.js
CHANGED
|
@@ -10,6 +10,7 @@ const { test } = require("node:test");
|
|
|
10
10
|
|
|
11
11
|
const PACKAGE_ROOT = path.join(__dirname, "..");
|
|
12
12
|
const SRC = path.join(PACKAGE_ROOT, "src");
|
|
13
|
+
const BIN = path.join(PACKAGE_ROOT, "bin");
|
|
13
14
|
|
|
14
15
|
function readSource(...segments) {
|
|
15
16
|
return fs.readFileSync(path.join(PACKAGE_ROOT, ...segments), "utf8");
|
|
@@ -35,12 +36,35 @@ test("local-api spawns the package bin entry, not the missing bin/tracker.js", (
|
|
|
35
36
|
);
|
|
36
37
|
});
|
|
37
38
|
|
|
38
|
-
test("
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
test("the unused Sentry error-reporting SDK stays out of the package", () => {
|
|
40
|
+
// 0.2.6 removed @sentry/node: it was only initialized (captureException had no
|
|
41
|
+
// call sites) and shipped 44 MB into the runtime copy plus a build-time
|
|
42
|
+
// bundler-plugin chain into every npm install. Keep it out unless the
|
|
43
|
+
// reporting path is actually wired and budgeted again.
|
|
44
|
+
const pkg = JSON.parse(readSource("package.json"));
|
|
45
|
+
const declared = { ...(pkg.dependencies || {}), ...(pkg.optionalDependencies || {}) };
|
|
46
|
+
assert.ok(
|
|
47
|
+
!Object.keys(declared).some((name) => name === "@sentry/node" || name.startsWith("@sentry/")),
|
|
48
|
+
"@sentry/* must not be a declared dependency",
|
|
49
|
+
);
|
|
50
|
+
assert.ok(
|
|
51
|
+
!fs.existsSync(path.join(SRC, "lib", "sentry.js")),
|
|
52
|
+
"the inert sentry bootstrap must stay deleted",
|
|
53
|
+
);
|
|
54
|
+
assert.ok(
|
|
55
|
+
!fs.existsSync(path.join(SRC, "lib", "telemetry-pref.js")),
|
|
56
|
+
"the opt-out helper only existed to gate Sentry",
|
|
57
|
+
);
|
|
58
|
+
const roots = [SRC, BIN];
|
|
59
|
+
for (const root of roots) {
|
|
60
|
+
const sourceFiles = fs
|
|
61
|
+
.readdirSync(root, { recursive: true })
|
|
62
|
+
.filter((rel) => String(rel).endsWith(".js"));
|
|
63
|
+
for (const rel of sourceFiles) {
|
|
64
|
+
const source = fs.readFileSync(path.join(root, String(rel)), "utf8");
|
|
65
|
+
assert.ok(!source.includes("@sentry/"), `${rel} must not require the removed SDK`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
44
68
|
});
|
|
45
69
|
|
|
46
70
|
test("the unported install heartbeat stays removed", () => {
|
|
@@ -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
|
+
});
|
package/src/lib/sentry.js
DELETED
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Optional Sentry error reporting for the Node CLI / EmbeddedServer runtime.
|
|
5
|
-
*
|
|
6
|
-
* Privacy contract (keep in sync with README "隐私边界"):
|
|
7
|
-
* - Off unless TOKENUSAGE_SENTRY_DSN (or SENTRY_DSN) is set.
|
|
8
|
-
* - Honors the shared telemetry opt-out
|
|
9
|
-
* (TOKENUSAGE_NO_TELEMETRY / DO_NOT_TRACK / config.json `"telemetry": false`).
|
|
10
|
-
* - Fail-closed in the node test runner.
|
|
11
|
-
* - Never intended to carry prompts, code, or provider secrets; beforeSend
|
|
12
|
-
* scrubs common secret patterns from event payloads.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
const path = require("node:path");
|
|
16
|
-
const pkg = require("../../package.json");
|
|
17
|
-
const { readJson } = require("./fs");
|
|
18
|
-
const { isTelemetryDisabled } = require("./telemetry-pref");
|
|
19
|
-
|
|
20
|
-
const SECRET_PATTERN =
|
|
21
|
-
/(?:api[_-]?key|access[_-]?token|refresh[_-]?token|cookie|secret|password|dsn)[=:\s]+["']?[^\s"'\\]{8,}/gi;
|
|
22
|
-
const BEARER_PATTERN = /\bBearer\s+[A-Za-z0-9._~+/=-]{8,}/gi;
|
|
23
|
-
const JWT_PATTERN = /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9._-]{10,}\.[A-Za-z0-9._-]{10,}\b/g;
|
|
24
|
-
|
|
25
|
-
let initialized = false;
|
|
26
|
-
|
|
27
|
-
function resolveDsn(env = process.env) {
|
|
28
|
-
const raw = env?.TOKENUSAGE_SENTRY_DSN || env?.SENTRY_DSN || "";
|
|
29
|
-
return typeof raw === "string" ? raw.trim() : "";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function resolveEnvironment(env = process.env) {
|
|
33
|
-
const raw = typeof env?.TOKENUSAGE_SENTRY_ENVIRONMENT === "string"
|
|
34
|
-
? env.TOKENUSAGE_SENTRY_ENVIRONMENT.trim()
|
|
35
|
-
: "";
|
|
36
|
-
if (raw) return raw;
|
|
37
|
-
if (env?.NODE_ENV === "production") return "production";
|
|
38
|
-
return "development";
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function resolveShell(env = process.env) {
|
|
42
|
-
const raw = typeof env?.TOKENUSAGE_APP_SHELL === "string"
|
|
43
|
-
? env.TOKENUSAGE_APP_SHELL.trim().toLowerCase()
|
|
44
|
-
: "";
|
|
45
|
-
if (raw === "macos" || raw === "windows" || raw === "linux" || raw === "cli") return raw;
|
|
46
|
-
return "cli";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function scrubString(value) {
|
|
50
|
-
if (typeof value !== "string" || !value) return value;
|
|
51
|
-
return value
|
|
52
|
-
.replace(BEARER_PATTERN, "Bearer [Filtered]")
|
|
53
|
-
.replace(JWT_PATTERN, "[Filtered]")
|
|
54
|
-
.replace(SECRET_PATTERN, "[Filtered]");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function scrubValue(value, depth = 0) {
|
|
58
|
-
if (depth > 6 || value == null) return value;
|
|
59
|
-
if (typeof value === "string") return scrubString(value);
|
|
60
|
-
if (Array.isArray(value)) return value.map((item) => scrubValue(item, depth + 1));
|
|
61
|
-
if (typeof value === "object") {
|
|
62
|
-
const out = {};
|
|
63
|
-
for (const [key, nested] of Object.entries(value)) {
|
|
64
|
-
const lower = key.toLowerCase();
|
|
65
|
-
if (
|
|
66
|
-
lower.includes("authorization") ||
|
|
67
|
-
lower.includes("cookie") ||
|
|
68
|
-
lower.includes("password") ||
|
|
69
|
-
lower.includes("secret") ||
|
|
70
|
-
lower.includes("token") ||
|
|
71
|
-
lower.includes("api_key") ||
|
|
72
|
-
lower.includes("apikey")
|
|
73
|
-
) {
|
|
74
|
-
out[key] = "[Filtered]";
|
|
75
|
-
} else {
|
|
76
|
-
out[key] = scrubValue(nested, depth + 1);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return out;
|
|
80
|
-
}
|
|
81
|
-
return value;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function truncateMaybe(value, max = 240) {
|
|
85
|
-
if (typeof value !== "string") return value;
|
|
86
|
-
if (value.length <= max) return value;
|
|
87
|
-
return `${value.slice(0, max)}…[truncated]`;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function scrubEvent(event) {
|
|
91
|
-
if (!event || typeof event !== "object") return event;
|
|
92
|
-
const next = { ...event };
|
|
93
|
-
if (next.message) next.message = truncateMaybe(scrubString(next.message));
|
|
94
|
-
if (next.extra) next.extra = scrubValue(next.extra);
|
|
95
|
-
if (next.contexts) next.contexts = scrubValue(next.contexts);
|
|
96
|
-
if (next.request) next.request = scrubValue(next.request);
|
|
97
|
-
// Console breadcrumbs can carry parser / JSON.parse excerpts of local logs
|
|
98
|
-
// (prompt text). Drop them entirely; keep only non-console crumbs and scrub.
|
|
99
|
-
if (Array.isArray(next.breadcrumbs)) {
|
|
100
|
-
next.breadcrumbs = next.breadcrumbs
|
|
101
|
-
.filter((crumb) => crumb && crumb.category !== "console")
|
|
102
|
-
.map((crumb) => scrubValue(crumb));
|
|
103
|
-
}
|
|
104
|
-
if (next.exception?.values) {
|
|
105
|
-
next.exception = {
|
|
106
|
-
...next.exception,
|
|
107
|
-
values: next.exception.values.map((item) => ({
|
|
108
|
-
...item,
|
|
109
|
-
value: truncateMaybe(scrubString(item?.value)),
|
|
110
|
-
})),
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
// Never attach IP / user PII by default.
|
|
114
|
-
if (next.user) {
|
|
115
|
-
next.user = { id: typeof next.user.id === "string" ? next.user.id : undefined };
|
|
116
|
-
}
|
|
117
|
-
return next;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Initialise Sentry once for this process. Never throws.
|
|
122
|
-
* @returns {Promise<{ enabled: boolean, reason?: string }>}
|
|
123
|
-
*/
|
|
124
|
-
async function initSentry({
|
|
125
|
-
env = process.env,
|
|
126
|
-
trackerDir,
|
|
127
|
-
version = pkg.version,
|
|
128
|
-
} = {}) {
|
|
129
|
-
try {
|
|
130
|
-
if (initialized) return { enabled: true, reason: "already-initialized" };
|
|
131
|
-
if (env?.NODE_TEST_CONTEXT !== undefined) {
|
|
132
|
-
return { enabled: false, reason: "test-env" };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const dsn = resolveDsn(env);
|
|
136
|
-
if (!dsn) return { enabled: false, reason: "no-dsn" };
|
|
137
|
-
|
|
138
|
-
let config = {};
|
|
139
|
-
if (trackerDir) {
|
|
140
|
-
config = (await readJson(path.join(trackerDir, "config.json"))) || {};
|
|
141
|
-
}
|
|
142
|
-
if (isTelemetryDisabled({ env, config })) {
|
|
143
|
-
return { enabled: false, reason: "opt-out" };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Honor the same outbound proxy / fail-closed policy as the rest of the CLI.
|
|
147
|
-
// Sentry's Node transport uses node:https (not undici), so pass proxy explicitly.
|
|
148
|
-
const {
|
|
149
|
-
readPersistedProxyConfig,
|
|
150
|
-
resolveEffectiveProxySource,
|
|
151
|
-
} = require("./proxy-env");
|
|
152
|
-
const proxyConfig = readPersistedProxyConfig();
|
|
153
|
-
const proxyResolved = resolveEffectiveProxySource({ env, proxyConfig });
|
|
154
|
-
if (proxyResolved?.source === "blocked") {
|
|
155
|
-
return { enabled: false, reason: "proxy-blocked" };
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// Lazy require keeps unit tests that never call init from paying the
|
|
159
|
-
// module graph cost at import time of unrelated modules.
|
|
160
|
-
const Sentry = require("@sentry/node");
|
|
161
|
-
Sentry.init({
|
|
162
|
-
dsn,
|
|
163
|
-
environment: resolveEnvironment(env),
|
|
164
|
-
release: `tokenusage-cli@${String(version || "").trim() || "0.0.0"}`,
|
|
165
|
-
sendDefaultPii: false,
|
|
166
|
-
tracesSampleRate: 0,
|
|
167
|
-
// Errors only: drop process-session + console; replace Http so request
|
|
168
|
-
// sessions stay off (Http defaults sessions:true and bypasses beforeSend).
|
|
169
|
-
integrations(defaults) {
|
|
170
|
-
const filtered = defaults.filter((integration) => {
|
|
171
|
-
const name = integration?.name || "";
|
|
172
|
-
return name !== "ProcessSession" && name !== "Console" && name !== "Http";
|
|
173
|
-
});
|
|
174
|
-
filtered.push(Sentry.httpIntegration({ sessions: false }));
|
|
175
|
-
return filtered;
|
|
176
|
-
},
|
|
177
|
-
transportOptions: proxyResolved?.proxyUrl
|
|
178
|
-
? { proxy: proxyResolved.proxyUrl }
|
|
179
|
-
: undefined,
|
|
180
|
-
beforeSend(event) {
|
|
181
|
-
return scrubEvent(event);
|
|
182
|
-
},
|
|
183
|
-
initialScope: {
|
|
184
|
-
tags: {
|
|
185
|
-
shell: resolveShell(env),
|
|
186
|
-
runtime: "node",
|
|
187
|
-
},
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
initialized = true;
|
|
191
|
-
return { enabled: true };
|
|
192
|
-
} catch {
|
|
193
|
-
return { enabled: false, reason: "init-failed" };
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function captureException(error, context) {
|
|
198
|
-
if (!initialized) return;
|
|
199
|
-
try {
|
|
200
|
-
const Sentry = require("@sentry/node");
|
|
201
|
-
if (context) {
|
|
202
|
-
Sentry.withScope((scope) => {
|
|
203
|
-
if (context.tags) {
|
|
204
|
-
for (const [key, value] of Object.entries(context.tags)) {
|
|
205
|
-
scope.setTag(key, String(value));
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
if (context.extra) scope.setExtras(scrubValue(context.extra));
|
|
209
|
-
Sentry.captureException(error);
|
|
210
|
-
});
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
Sentry.captureException(error);
|
|
214
|
-
} catch {
|
|
215
|
-
/* never break the CLI */
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = {
|
|
220
|
-
initSentry,
|
|
221
|
-
captureException,
|
|
222
|
-
resolveDsn,
|
|
223
|
-
resolveEnvironment,
|
|
224
|
-
resolveShell,
|
|
225
|
-
scrubEvent,
|
|
226
|
-
scrubString,
|
|
227
|
-
// test helpers
|
|
228
|
-
_resetForTests() {
|
|
229
|
-
initialized = false;
|
|
230
|
-
},
|
|
231
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Outbound telemetry opt-out, shared by the optional Sentry error reporting.
|
|
5
|
-
*
|
|
6
|
-
* 0.2.3 removed the upstream anonymous install heartbeat: this backend never
|
|
7
|
-
* implemented `/api/functions/tokenusage-telemetry`, so every sync only earned
|
|
8
|
-
* a 404. Sentry remains strictly opt-in (needs a DSN) and honors the same
|
|
9
|
-
* opt-out switches:
|
|
10
|
-
* - TOKENUSAGE_NO_TELEMETRY=1
|
|
11
|
-
* - DO_NOT_TRACK (the standard)
|
|
12
|
-
* - `"telemetry": false` in config.json
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
function isTruthyFlag(value) {
|
|
16
|
-
if (typeof value !== "string") return false;
|
|
17
|
-
const v = value.trim().toLowerCase();
|
|
18
|
-
return v === "1" || v === "true";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function isTelemetryDisabled({ env = process.env, config = {} } = {}) {
|
|
22
|
-
if (isTruthyFlag(env?.TOKENUSAGE_NO_TELEMETRY)) return true;
|
|
23
|
-
if (isTruthyFlag(env?.DO_NOT_TRACK)) return true;
|
|
24
|
-
if (config && config.telemetry === false) return true;
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
module.exports = { isTelemetryDisabled };
|