tokenusage-cli 0.5.3 → 0.5.4
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 +20 -0
- package/package.json +1 -1
- package/src/commands/pause.js +2 -2
- package/src/commands/sync.js +13 -2
- package/src/lib/collection-pause.js +9 -6
- package/src/lib/fs.js +49 -6
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,26 @@
|
|
|
6
6
|
> 0.4.x 之前的条目为压缩摘要;细节与当时的验收记录见 `docs/cli-release.md`
|
|
7
7
|
> 与 `docs/archive/`。
|
|
8
8
|
|
|
9
|
+
## 0.5.4
|
|
10
|
+
|
|
11
|
+
2026-11-09。tracker 目录写入被拒时的可诊断性与降级。零契约变更,服务端无需同步升级;对账
|
|
12
|
+
基线(`queue.jsonl` last-wins)与队列文件格式不变。
|
|
13
|
+
|
|
14
|
+
- **原子写的临时文件创建加短重试**:Windows 上过滤驱动或上一轮释放的收尾窗口会让 `*.tmp`
|
|
15
|
+
创建偶发返回 `EPERM`/`EBUSY`;此前 `writeFileAtomic` 直接抛出,桌面端「立即同步」「暂停
|
|
16
|
+
采集」以一行裸 errno 中断。现在与 `sync.lock` 同策略(3 × 150 ms),耗尽后才原样上抛。
|
|
17
|
+
- **写入失败附带可读提示**:`EPERM`/`EACCES`/`EBUSY`/`EROFS` 时错误挂一行
|
|
18
|
+
`Hint: Cannot create files in <目录> (<code>) …`,点明「进程权限或完整性级别低于该目录」并
|
|
19
|
+
给出处理方向;`bin/tokenusage.js` 打印 `Hint:`,桌面端 Alert 直接可见。
|
|
20
|
+
- **`pause` 标志改走 `writeFileAtomic`**:原先手写 `<file>.<pid>.tmp`,没有任何保护;
|
|
21
|
+
`resume` 的 unlink 失败同样附带 hint。
|
|
22
|
+
- **skip 标记写入失败不再致命**:锁没拿到时若 `sync.skip.json` 写不进去,仍输出同义的
|
|
23
|
+
`Sync skipped: … (<code>)` 并以 exit 0 结束——标记缺失只影响可观测面,不会把一次「跳过」
|
|
24
|
+
升级成崩溃。
|
|
25
|
+
|
|
26
|
+
升级说明:无需任何动作,随下次计划任务自动生效;桌面端「立即同步 / 暂停」遇到这类拒绝时
|
|
27
|
+
从裸崩溃改为可读错误。
|
|
28
|
+
|
|
9
29
|
## 0.5.3
|
|
10
30
|
|
|
11
31
|
2026-11-09。同步锁稳健性 + 上游命令名残留清理。零契约变更,服务端无需同步升级;对账基线
|
package/package.json
CHANGED
package/src/commands/pause.js
CHANGED
|
@@ -25,7 +25,7 @@ async function cmdPause() {
|
|
|
25
25
|
const { trackerDir } = await resolveTrackerPaths({ home: os.homedir() });
|
|
26
26
|
const alreadyPaused = isCollectionPaused(trackerDir);
|
|
27
27
|
|
|
28
|
-
setCollectionPaused(trackerDir, true);
|
|
28
|
+
await setCollectionPaused(trackerDir, true);
|
|
29
29
|
const schedule = setAutoSyncEnabled(false);
|
|
30
30
|
|
|
31
31
|
process.stdout.write(
|
|
@@ -46,7 +46,7 @@ async function cmdResume() {
|
|
|
46
46
|
const { trackerDir } = await resolveTrackerPaths({ home: os.homedir() });
|
|
47
47
|
const wasPaused = isCollectionPaused(trackerDir);
|
|
48
48
|
|
|
49
|
-
setCollectionPaused(trackerDir, false);
|
|
49
|
+
await setCollectionPaused(trackerDir, false);
|
|
50
50
|
const schedule = setAutoSyncEnabled(true);
|
|
51
51
|
|
|
52
52
|
process.stdout.write(
|
package/src/commands/sync.js
CHANGED
|
@@ -693,8 +693,19 @@ async function cmdSync(argv, context = {}) {
|
|
|
693
693
|
const lock = await acquireSyncLock(lockPath, opts, lockWaitOptions);
|
|
694
694
|
if (!lock) {
|
|
695
695
|
// Warn on stderr so the notice reaches interactive and background runs
|
|
696
|
-
// alike without disturbing anything that parses sync's stdout.
|
|
697
|
-
|
|
696
|
+
// alike without disturbing anything that parses sync's stdout. The marker
|
|
697
|
+
// is diagnostics, not the skip itself: a failed write (Windows' transient
|
|
698
|
+
// EPERM on create) must not turn a skipped sync into a crash — the notice
|
|
699
|
+
// still reaches stderr, just without the marker.
|
|
700
|
+
let notice;
|
|
701
|
+
try {
|
|
702
|
+
notice = await recordSyncSkip(trackerDir, lockPath);
|
|
703
|
+
} catch (error) {
|
|
704
|
+
notice =
|
|
705
|
+
"Sync skipped: sync lock could not be acquired — " +
|
|
706
|
+
`failed to write ${SYNC_SKIP_MARKER} (${error?.code || error?.message || "unknown error"})\n`;
|
|
707
|
+
}
|
|
708
|
+
process.stderr.write(notice);
|
|
698
709
|
return;
|
|
699
710
|
}
|
|
700
711
|
let progress = null;
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
const fs = require("node:fs");
|
|
12
12
|
const path = require("node:path");
|
|
13
13
|
|
|
14
|
+
const { writeFileAtomic, attachWriteHint } = require("./fs");
|
|
15
|
+
|
|
14
16
|
const PAUSE_FILE = "pause.flag.json";
|
|
15
17
|
const RUN_REPORT_KEY = "runReport";
|
|
16
18
|
|
|
@@ -39,22 +41,23 @@ function readCollectionPause(trackerDir) {
|
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
// Async so the write goes through writeFileAtomic (same-directory tmp + rename,
|
|
45
|
+
// with its short retry for Windows' transient EPERM on create): the previous
|
|
46
|
+
// hand-rolled `${file}.${process.pid}.tmp` write aborted `tokenusage pause`
|
|
47
|
+
// with a bare errno instead of landing the flag.
|
|
48
|
+
async function setCollectionPaused(trackerDir, paused, { at = new Date().toISOString() } = {}) {
|
|
44
49
|
const file = pauseFilePath(trackerDir);
|
|
45
50
|
|
|
46
51
|
if (!paused) {
|
|
47
52
|
try {
|
|
48
53
|
fs.unlinkSync(file);
|
|
49
54
|
} catch (error) {
|
|
50
|
-
if (error?.code !== "ENOENT") throw error;
|
|
55
|
+
if (error?.code !== "ENOENT") throw attachWriteHint(error, file);
|
|
51
56
|
}
|
|
52
57
|
return;
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
fs.writeFileSync(tmp, `${JSON.stringify({ paused: true, at })}\n`, { mode: 0o600 });
|
|
57
|
-
fs.renameSync(tmp, file);
|
|
60
|
+
await writeFileAtomic(file, `${JSON.stringify({ paused: true, at })}\n`, { mode: 0o600 });
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
function isRunReportEnabled(trackerDir) {
|
package/src/lib/fs.js
CHANGED
|
@@ -16,11 +16,7 @@ async function writeFileAtomic(filePath, content, { mode } = {}) {
|
|
|
16
16
|
// private from creation instead of relying on a later chmod that a crash
|
|
17
17
|
// between write and chmod would skip.
|
|
18
18
|
try {
|
|
19
|
-
await
|
|
20
|
-
tmp,
|
|
21
|
-
content,
|
|
22
|
-
mode == null ? { encoding: "utf8" } : { encoding: "utf8", mode },
|
|
23
|
-
);
|
|
19
|
+
await writeTmpFile(tmp, content, mode);
|
|
24
20
|
await fs.rename(tmp, filePath);
|
|
25
21
|
} catch (err) {
|
|
26
22
|
try {
|
|
@@ -28,7 +24,7 @@ async function writeFileAtomic(filePath, content, { mode } = {}) {
|
|
|
28
24
|
} catch {
|
|
29
25
|
/* tmp may not exist if writeFile failed before creating it */
|
|
30
26
|
}
|
|
31
|
-
throw err;
|
|
27
|
+
throw attachWriteHint(err, filePath);
|
|
32
28
|
}
|
|
33
29
|
}
|
|
34
30
|
|
|
@@ -82,6 +78,52 @@ const TRANSITION_GUARD_ATTEMPTS = 1000;
|
|
|
82
78
|
// trouble must be survivable and queryable, not loud).
|
|
83
79
|
const LOCK_TRANSIENT_OPEN_RETRIES = 3;
|
|
84
80
|
const LOCK_TRANSIENT_OPEN_RETRY_MS = 150;
|
|
81
|
+
// The same Windows create-denial that the lock absorbs also hits plain atomic
|
|
82
|
+
// writes: `sync.skip.json.tmp…` and `pause.flag.json…tmp` surfaced a raw EPERM
|
|
83
|
+
// and aborted the command (desktop「立即同步」/「暂停采集」). Retry the tmp create
|
|
84
|
+
// briefly, then let the error out unchanged.
|
|
85
|
+
const WRITE_TRANSIENT_RETRIES = 3;
|
|
86
|
+
const WRITE_TRANSIENT_RETRY_MS = 150;
|
|
87
|
+
|
|
88
|
+
function isTransientCreateError(error) {
|
|
89
|
+
return error?.code === "EPERM" || error?.code === "EBUSY";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// A write failure is usually actionable in one specific way: the *folder*
|
|
93
|
+
// refused the create (permissions, or a process integrity level lower than the
|
|
94
|
+
// folder's label). Attach that as a one-line hint — bin/tokenusage.js prints
|
|
95
|
+
// `hint` after the error, and the desktop's actions card shows the same tail.
|
|
96
|
+
const WRITE_HINT_CODES = new Set(["EPERM", "EACCES", "EBUSY", "EROFS"]);
|
|
97
|
+
|
|
98
|
+
function attachWriteHint(error, filePath) {
|
|
99
|
+
if (!error || typeof error !== "object" || error.hint) return error;
|
|
100
|
+
if (!WRITE_HINT_CODES.has(error.code)) return error;
|
|
101
|
+
const dir = path.dirname(filePath);
|
|
102
|
+
error.hint =
|
|
103
|
+
`Cannot create files in ${dir} (${error.code}). ` +
|
|
104
|
+
"The process may run with less access or a lower integrity level than that folder " +
|
|
105
|
+
"(for example inside a sandbox, or started by a host process that runs at low integrity). " +
|
|
106
|
+
"Retry from a normal terminal, or grant this user write access to the folder.";
|
|
107
|
+
return error;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function writeTmpFile(tmp, content, mode) {
|
|
111
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
112
|
+
try {
|
|
113
|
+
await fs.writeFile(
|
|
114
|
+
tmp,
|
|
115
|
+
content,
|
|
116
|
+
mode == null ? { encoding: "utf8" } : { encoding: "utf8", mode },
|
|
117
|
+
);
|
|
118
|
+
return;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (attempt >= WRITE_TRANSIENT_RETRIES - 1 || !isTransientCreateError(error)) {
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
await new Promise((resolve) => setTimeout(resolve, WRITE_TRANSIENT_RETRY_MS));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
85
127
|
|
|
86
128
|
function parseLockOwner(raw) {
|
|
87
129
|
try {
|
|
@@ -488,6 +530,7 @@ async function updateJsonLocked(
|
|
|
488
530
|
module.exports = {
|
|
489
531
|
ensureDir,
|
|
490
532
|
writeFileAtomic,
|
|
533
|
+
attachWriteHint,
|
|
491
534
|
readJson,
|
|
492
535
|
readJsonStrict,
|
|
493
536
|
writeJson,
|