tokenusage-cli 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +16 -2
- package/package.json +1 -3
- package/src/cli.js +15 -9
- package/src/commands/init.js +288 -42
- package/src/commands/sync.js +39 -6
- package/src/commands/uninstall.js +3 -1
- package/src/lib/cli-ui.js +7 -1
- package/src/lib/command-runner.js +3 -0
- package/src/lib/doctor.js +70 -0
- package/src/lib/local-api.js +3 -0
- package/src/lib/omp-hook.js +2 -0
- package/src/lib/openclaw-hook.js +20 -7
- package/src/lib/openclaw-session-plugin.js +20 -6
- package/src/lib/rollout.js +19 -22
- package/src/lib/runtime-entry.js +35 -0
- package/src/lib/scheduled-task.js +47 -13
- package/test/audit-fixes.test.js +19 -1
- package/test/cli-options.test.js +11 -0
- package/test/cli-ui.test.js +47 -0
- package/test/collection-controls.test.js +27 -3
- package/test/packaging.test.js +29 -0
- package/test/runtime-copy.test.js +111 -0
- package/test/zstd-native.test.js +36 -0
package/src/commands/sync.js
CHANGED
|
@@ -8,6 +8,8 @@ const { functionUrlFor, fetchFunctionResponse } = require("../lib/function-url")
|
|
|
8
8
|
const { cloudUsageResetBlocksUpload, finishCloudDeviceUsageReset } = require("../lib/cloud-usage-reset");
|
|
9
9
|
const { KIMI_MODEL_REPAIR_KEY, repairKimiModelAttribution } = require("../lib/kimi-model-repair");
|
|
10
10
|
const { readSqliteJsonRows } = require("../lib/sqlite-reader");
|
|
11
|
+
const { runtimeReadyMarkerPath, runtimeBinPath } = require("../lib/runtime-entry");
|
|
12
|
+
const { cliEntryPath } = require("../lib/scheduled-task");
|
|
11
13
|
|
|
12
14
|
const { resolveInstallPaths, resolveZcodeNativeDbPath, resolveMimoNativeDbPath, ensureFlatCursor } = require("../lib/install-resolver");
|
|
13
15
|
const { multiInstallParse, mergeBothFileSources } = require("../lib/multi-install-parser");
|
|
@@ -4458,7 +4460,9 @@ async function scheduleAutoRetry({
|
|
|
4458
4460
|
|
|
4459
4461
|
spawnAutoRetryProcess({
|
|
4460
4462
|
retryPath,
|
|
4461
|
-
trackerBinPath:
|
|
4463
|
+
trackerBinPath: runtimeBinPath(trackerDir),
|
|
4464
|
+
runtimeReadyPath: runtimeReadyMarkerPath(path.join(trackerDir, "app")),
|
|
4465
|
+
packageEntryPath: cliEntryPath(),
|
|
4462
4466
|
fallbackPkg: "tokenusage-cli",
|
|
4463
4467
|
delayMs,
|
|
4464
4468
|
});
|
|
@@ -4484,8 +4488,22 @@ async function clearAutoRetry(trackerDir) {
|
|
|
4484
4488
|
await fs.unlink(retryPath).catch(() => {});
|
|
4485
4489
|
}
|
|
4486
4490
|
|
|
4487
|
-
function spawnAutoRetryProcess({
|
|
4488
|
-
|
|
4491
|
+
function spawnAutoRetryProcess({
|
|
4492
|
+
retryPath,
|
|
4493
|
+
trackerBinPath,
|
|
4494
|
+
runtimeReadyPath,
|
|
4495
|
+
packageEntryPath,
|
|
4496
|
+
fallbackPkg,
|
|
4497
|
+
delayMs,
|
|
4498
|
+
}) {
|
|
4499
|
+
const script = buildAutoRetryScript({
|
|
4500
|
+
retryPath,
|
|
4501
|
+
trackerBinPath,
|
|
4502
|
+
runtimeReadyPath,
|
|
4503
|
+
packageEntryPath,
|
|
4504
|
+
fallbackPkg,
|
|
4505
|
+
delayMs,
|
|
4506
|
+
});
|
|
4489
4507
|
try {
|
|
4490
4508
|
const child = cp.spawn(process.execPath, ["-e", script], {
|
|
4491
4509
|
detached: true,
|
|
@@ -4500,13 +4518,22 @@ function spawnAutoRetryProcess({ retryPath, trackerBinPath, fallbackPkg, delayMs
|
|
|
4500
4518
|
} catch (_e) {}
|
|
4501
4519
|
}
|
|
4502
4520
|
|
|
4503
|
-
function buildAutoRetryScript({
|
|
4521
|
+
function buildAutoRetryScript({
|
|
4522
|
+
retryPath,
|
|
4523
|
+
trackerBinPath,
|
|
4524
|
+
runtimeReadyPath,
|
|
4525
|
+
packageEntryPath,
|
|
4526
|
+
fallbackPkg,
|
|
4527
|
+
delayMs,
|
|
4528
|
+
}) {
|
|
4504
4529
|
return (
|
|
4505
4530
|
`'use strict';\n` +
|
|
4506
4531
|
`const fs = require('node:fs');\n` +
|
|
4507
4532
|
`const cp = require('node:child_process');\n` +
|
|
4508
4533
|
`const retryPath = ${JSON.stringify(retryPath)};\n` +
|
|
4509
4534
|
`const trackerBinPath = ${JSON.stringify(trackerBinPath)};\n` +
|
|
4535
|
+
`const runtimeReadyPath = ${JSON.stringify(runtimeReadyPath)};\n` +
|
|
4536
|
+
`const packageEntryPath = ${JSON.stringify(packageEntryPath || null)};\n` +
|
|
4510
4537
|
`const fallbackPkg = ${JSON.stringify(fallbackPkg)};\n` +
|
|
4511
4538
|
`const delayMs = ${Math.max(0, Math.floor(delayMs || 0))};\n` +
|
|
4512
4539
|
`setTimeout(() => {\n` +
|
|
@@ -4522,9 +4549,15 @@ function buildAutoRetryScript({ retryPath, trackerBinPath, fallbackPkg, delayMs
|
|
|
4522
4549
|
` if (payload && typeof payload.syncSource === 'string' && payload.syncSource.trim()) {\n` +
|
|
4523
4550
|
` argv.push('--source', payload.syncSource.trim());\n` +
|
|
4524
4551
|
` }\n` +
|
|
4525
|
-
`
|
|
4552
|
+
` // Same readiness contract as notify: a copy without its marker is half\n` +
|
|
4553
|
+
` // copied, so retry through the installed package instead.\n` +
|
|
4554
|
+
` const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);\n` +
|
|
4555
|
+
` const hasPackageEntry = Boolean(packageEntryPath) && fs.existsSync(packageEntryPath);\n` +
|
|
4556
|
+
` const cmd = hasLocalRuntime\n` +
|
|
4526
4557
|
` ? [process.execPath, trackerBinPath, ...argv]\n` +
|
|
4527
|
-
` :
|
|
4558
|
+
` : hasPackageEntry\n` +
|
|
4559
|
+
` ? [process.execPath, packageEntryPath, ...argv]\n` +
|
|
4560
|
+
` : ['npx', '--yes', fallbackPkg, ...argv];\n` +
|
|
4528
4561
|
` try {\n` +
|
|
4529
4562
|
` const child = cp.spawn(cmd[0], cmd.slice(1), { detached: true, windowsHide: true, stdio: 'ignore', env: process.env });\n` +
|
|
4530
4563
|
` child.unref();\n` +
|
|
@@ -143,8 +143,10 @@ async function cmdUninstall(argv) {
|
|
|
143
143
|
await fs.unlink(path.join(binDir, "run-sync-hidden.vbs")).catch(() => {});
|
|
144
144
|
const taskDelete = deleteWindowsScheduledTask();
|
|
145
145
|
|
|
146
|
-
// Remove local app runtime (installed by init for notify-driven sync)
|
|
146
|
+
// Remove local app runtime (installed by init for notify-driven sync) and any
|
|
147
|
+
// staging directory left behind by an interrupted runtime copy.
|
|
147
148
|
await fs.rm(path.join(trackerDir, "app"), { recursive: true, force: true }).catch(() => {});
|
|
149
|
+
await fs.rm(path.join(trackerDir, "app.new"), { recursive: true, force: true }).catch(() => {});
|
|
148
150
|
|
|
149
151
|
// Deliberately NOT removed by --purge: the machine-identity seed lets a
|
|
150
152
|
// reinstall reuse the same cloud device row instead of double-counting the
|
package/src/lib/cli-ui.js
CHANGED
|
@@ -155,7 +155,13 @@ function createSpinner({ text, intervalMs = 80 }) {
|
|
|
155
155
|
function formatSummaryLine({ label, status, detail }) {
|
|
156
156
|
const isSuccess = status === "updated" || status === "set" || status === "installed";
|
|
157
157
|
const bullet = isSuccess ? color("*", GREEN) : "o";
|
|
158
|
-
const statusLabel = isSuccess
|
|
158
|
+
const statusLabel = isSuccess
|
|
159
|
+
? detail || status
|
|
160
|
+
: !detail
|
|
161
|
+
? "Skipped"
|
|
162
|
+
: /^Skipped\b/i.test(detail)
|
|
163
|
+
? detail
|
|
164
|
+
: `Skipped - ${detail}`;
|
|
159
165
|
const line = ` ${bullet} ${label.padEnd(22)} [${statusLabel}]`;
|
|
160
166
|
return isSuccess ? line : color(line, DIM);
|
|
161
167
|
}
|
|
@@ -64,6 +64,9 @@ function runCommand(commandRunner, command, args, options = {}) {
|
|
|
64
64
|
try {
|
|
65
65
|
child = cp.spawn(shellCommand, args, {
|
|
66
66
|
...spawnOptions,
|
|
67
|
+
// Detection/probe commands pipe their output; never let Windows pop a
|
|
68
|
+
// console window when the CLI itself runs from a GUI host.
|
|
69
|
+
windowsHide: spawnOptions.windowsHide ?? true,
|
|
67
70
|
detached: useProcessGroup || spawnOptions.detached,
|
|
68
71
|
shell: useShell,
|
|
69
72
|
stdio: [input === undefined ? "ignore" : "pipe", "pipe", "pipe"],
|
package/src/lib/doctor.js
CHANGED
|
@@ -16,6 +16,7 @@ async function buildDoctorReport({
|
|
|
16
16
|
|
|
17
17
|
if (paths.trackerDir) {
|
|
18
18
|
checks.push(await checkTrackerDir(paths.trackerDir));
|
|
19
|
+
checks.push(await checkRuntimeCopy(paths.trackerDir));
|
|
19
20
|
}
|
|
20
21
|
if (paths.configPath) {
|
|
21
22
|
checks.push(await checkConfigJson(paths.configPath));
|
|
@@ -176,6 +177,75 @@ async function checkTrackerDir(trackerDir) {
|
|
|
176
177
|
}
|
|
177
178
|
}
|
|
178
179
|
|
|
180
|
+
// The init-copied runtime (`<trackerDir>/app`) is what notify hooks and the
|
|
181
|
+
// hidden task prefer; without its readiness marker they fall back to the
|
|
182
|
+
// installed CLI. Surface that state instead of leaving it invisible.
|
|
183
|
+
async function checkRuntimeCopy(trackerDir) {
|
|
184
|
+
const { runtimeAppDir, runtimeBinPath, runtimeReadyMarkerPath } = require("./runtime-entry");
|
|
185
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
186
|
+
const binPath = runtimeBinPath(trackerDir);
|
|
187
|
+
const markerPath = runtimeReadyMarkerPath(appDir);
|
|
188
|
+
|
|
189
|
+
if (!(await isDirectory(appDir))) {
|
|
190
|
+
return {
|
|
191
|
+
id: "runtime.copy",
|
|
192
|
+
status: "ok",
|
|
193
|
+
detail: "no runtime copy (hooks use the installed CLI)",
|
|
194
|
+
critical: false,
|
|
195
|
+
meta: { path: appDir },
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const marker = await readJsonStrict(markerPath);
|
|
200
|
+
const cliVersion = require("../../package.json").version;
|
|
201
|
+
if (!(await isFile(binPath)) || marker.status !== "ok") {
|
|
202
|
+
return {
|
|
203
|
+
id: "runtime.copy",
|
|
204
|
+
status: "warn",
|
|
205
|
+
detail: "runtime copy incomplete; re-run `tokenusage init` (hooks currently use the installed CLI)",
|
|
206
|
+
critical: false,
|
|
207
|
+
meta: { path: appDir, marker: marker.status },
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const version = marker.value && typeof marker.value.version === "string" ? marker.value.version : null;
|
|
211
|
+
if (version && version !== cliVersion) {
|
|
212
|
+
return {
|
|
213
|
+
id: "runtime.copy",
|
|
214
|
+
status: "warn",
|
|
215
|
+
detail: `runtime copy v${version} is older than the CLI v${cliVersion}; re-run \`tokenusage init\``,
|
|
216
|
+
critical: false,
|
|
217
|
+
meta: { path: appDir, version, cli_version: cliVersion },
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
id: "runtime.copy",
|
|
222
|
+
status: "ok",
|
|
223
|
+
detail: `runtime copy ready${version ? ` (v${version})` : ""}`,
|
|
224
|
+
critical: false,
|
|
225
|
+
meta: {
|
|
226
|
+
path: appDir,
|
|
227
|
+
version,
|
|
228
|
+
installed_at: marker.value && typeof marker.value.installedAt === "string" ? marker.value.installedAt : null,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async function isDirectory(p) {
|
|
234
|
+
try {
|
|
235
|
+
return (await fs.stat(p)).isDirectory();
|
|
236
|
+
} catch (_err) {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function isFile(p) {
|
|
242
|
+
try {
|
|
243
|
+
return (await fs.stat(p)).isFile();
|
|
244
|
+
} catch (_err) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
179
249
|
async function checkConfigJson(configPath) {
|
|
180
250
|
const res = await readJsonStrict(configPath);
|
|
181
251
|
if (res.status === "ok") {
|
package/src/lib/local-api.js
CHANGED
|
@@ -761,6 +761,9 @@ function runSyncCommand(extraEnv = {}, opts = {}) {
|
|
|
761
761
|
} : {}),
|
|
762
762
|
TOKENUSAGE_LOCAL_SYNC_DEVICE_TOKEN: extraEnv.TOKENUSAGE_DEVICE_TOKEN || "",
|
|
763
763
|
},
|
|
764
|
+
// The API can be hosted by a GUI process (desktop shell): keep the piped
|
|
765
|
+
// sync child console-less on Windows.
|
|
766
|
+
windowsHide: true,
|
|
764
767
|
stdio: ["ignore", "pipe", "pipe"],
|
|
765
768
|
});
|
|
766
769
|
const cancel = () => child.kill("SIGTERM");
|
package/src/lib/omp-hook.js
CHANGED
|
@@ -116,6 +116,8 @@ function spawnNotify() {
|
|
|
116
116
|
try {
|
|
117
117
|
const child = spawn(process.execPath, [notifyPath, "--source=" + SOURCE], {
|
|
118
118
|
detached: true,
|
|
119
|
+
// A GUI-hosted oh-my-pi would otherwise flash a console window per turn.
|
|
120
|
+
windowsHide: true,
|
|
119
121
|
stdio: "ignore",
|
|
120
122
|
env: process.env,
|
|
121
123
|
});
|
package/src/lib/openclaw-hook.js
CHANGED
|
@@ -3,6 +3,8 @@ const path = require("node:path");
|
|
|
3
3
|
const fs = require("node:fs/promises");
|
|
4
4
|
const fssync = require("node:fs");
|
|
5
5
|
const { runOpenclawCli } = require("./openclaw-cli");
|
|
6
|
+
const { cliEntryPath } = require("./scheduled-task");
|
|
7
|
+
const { runtimeReadyMarkerPath } = require("./runtime-entry");
|
|
6
8
|
|
|
7
9
|
const OPENCLAW_HOOK_NAME = "tokenusage-openclaw-sync";
|
|
8
10
|
const OPENCLAW_HOOK_DIRNAME = "openclaw-hook";
|
|
@@ -237,8 +239,14 @@ Triggers non-blocking 'tokenusage sync --auto --from-openclaw' runs when OpenCla
|
|
|
237
239
|
`;
|
|
238
240
|
}
|
|
239
241
|
|
|
240
|
-
function buildHookHandler({
|
|
242
|
+
function buildHookHandler({
|
|
243
|
+
trackerDir,
|
|
244
|
+
packageName = "tokenusage-cli",
|
|
245
|
+
openclawHome,
|
|
246
|
+
packageEntry = cliEntryPath(),
|
|
247
|
+
}) {
|
|
241
248
|
const trackerBinPath = path.join(trackerDir, "app", "bin", "tracker.js");
|
|
249
|
+
const runtimeReadyPath = runtimeReadyMarkerPath(path.join(trackerDir, "app"));
|
|
242
250
|
const fallbackPkg = packageName || "tokenusage-cli";
|
|
243
251
|
const safeOpenclawHome = openclawHome || path.join(os.homedir(), ".openclaw");
|
|
244
252
|
|
|
@@ -249,10 +257,11 @@ function buildHookHandler({ trackerDir, packageName = "tokenusage-cli", openclaw
|
|
|
249
257
|
`const cp = require('node:child_process');\n` +
|
|
250
258
|
`const trackerDir = ${JSON.stringify(trackerDir)};\n` +
|
|
251
259
|
`const trackerBinPath = ${JSON.stringify(trackerBinPath)};\n` +
|
|
260
|
+
`const runtimeReadyPath = ${JSON.stringify(runtimeReadyPath)};\n` +
|
|
261
|
+
`const packageEntryPath = ${JSON.stringify(packageEntry || null)};\n` +
|
|
252
262
|
`const fallbackPkg = ${JSON.stringify(fallbackPkg)};\n` +
|
|
253
263
|
`const openclawHome = ${JSON.stringify(safeOpenclawHome)};\n` +
|
|
254
264
|
`const throttlePath = path.join(trackerDir, 'openclaw.sync.throttle');\n` +
|
|
255
|
-
`const depsMarkerPath = path.join(trackerDir, 'app', 'bin', 'tracker.js');\n` +
|
|
256
265
|
`const THROTTLE_MS = 15_000;\n` +
|
|
257
266
|
`\n` +
|
|
258
267
|
`module.exports = async function handler(event) {\n` +
|
|
@@ -295,13 +304,17 @@ function buildHookHandler({ trackerDir, packageName = "tokenusage-cli", openclaw
|
|
|
295
304
|
` if (prevModel) env.TOKENUSAGE_OPENCLAW_PREV_MODEL = prevModel;\n` +
|
|
296
305
|
` if (prevUpdatedAt) env.TOKENUSAGE_OPENCLAW_PREV_UPDATED_AT = prevUpdatedAt;\n` +
|
|
297
306
|
`\n` +
|
|
298
|
-
`
|
|
299
|
-
`
|
|
300
|
-
` const
|
|
307
|
+
` // First usable launcher: init-copied runtime (only with its readiness\n` +
|
|
308
|
+
` // marker), then the installed package entry, then npx.\n` +
|
|
309
|
+
` const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);\n` +
|
|
310
|
+
` const hasPackageEntry = Boolean(packageEntryPath) && fs.existsSync(packageEntryPath);\n` +
|
|
311
|
+
` const cmd = hasLocalRuntime\n` +
|
|
301
312
|
` ? [process.execPath, trackerBinPath, 'sync', '--auto', '--from-openclaw']\n` +
|
|
302
|
-
` :
|
|
313
|
+
` : hasPackageEntry\n` +
|
|
314
|
+
` ? [process.execPath, packageEntryPath, 'sync', '--auto', '--from-openclaw']\n` +
|
|
315
|
+
` : ['npx', '--yes', fallbackPkg, 'sync', '--auto', '--from-openclaw'];\n` +
|
|
303
316
|
`\n` +
|
|
304
|
-
` const child = cp.spawn(cmd[0], cmd.slice(1), { detached: true, stdio: 'ignore', env });\n` +
|
|
317
|
+
` const child = cp.spawn(cmd[0], cmd.slice(1), { detached: true, windowsHide: true, stdio: 'ignore', env });\n` +
|
|
305
318
|
` child.unref();\n` +
|
|
306
319
|
` } catch (_) {}\n` +
|
|
307
320
|
`};\n` +
|
|
@@ -3,6 +3,8 @@ const path = require("node:path");
|
|
|
3
3
|
const fs = require("node:fs/promises");
|
|
4
4
|
const fssync = require("node:fs");
|
|
5
5
|
const { runOpenclawCli } = require("./openclaw-cli");
|
|
6
|
+
const { cliEntryPath } = require("./scheduled-task");
|
|
7
|
+
const { runtimeReadyMarkerPath } = require("./runtime-entry");
|
|
6
8
|
|
|
7
9
|
const OPENCLAW_SESSION_PLUGIN_ID = "openclaw-session-sync";
|
|
8
10
|
const OPENCLAW_SESSION_PLUGIN_DIRNAME = "openclaw-plugin";
|
|
@@ -344,8 +346,14 @@ function buildSessionPluginMeta() {
|
|
|
344
346
|
)}\n`;
|
|
345
347
|
}
|
|
346
348
|
|
|
347
|
-
function buildSessionPluginIndex({
|
|
349
|
+
function buildSessionPluginIndex({
|
|
350
|
+
trackerDir,
|
|
351
|
+
packageName = "tokenusage-cli",
|
|
352
|
+
openclawHome,
|
|
353
|
+
packageEntry = cliEntryPath(),
|
|
354
|
+
}) {
|
|
348
355
|
const trackerBinPath = path.join(trackerDir, "app", "bin", "tracker.js");
|
|
356
|
+
const runtimeReadyPath = runtimeReadyMarkerPath(path.join(trackerDir, "app"));
|
|
349
357
|
const fallbackPkg = packageName || "tokenusage-cli";
|
|
350
358
|
const safeOpenclawHome = openclawHome || path.join(os.homedir(), ".openclaw");
|
|
351
359
|
|
|
@@ -356,9 +364,10 @@ function buildSessionPluginIndex({ trackerDir, packageName = "tokenusage-cli", o
|
|
|
356
364
|
`\n` +
|
|
357
365
|
`const trackerDir = ${JSON.stringify(trackerDir)};\n` +
|
|
358
366
|
`const trackerBinPath = ${JSON.stringify(trackerBinPath)};\n` +
|
|
367
|
+
`const runtimeReadyPath = ${JSON.stringify(runtimeReadyPath)};\n` +
|
|
368
|
+
`const packageEntryPath = ${JSON.stringify(packageEntry || null)};\n` +
|
|
359
369
|
`const fallbackPkg = ${JSON.stringify(fallbackPkg)};\n` +
|
|
360
370
|
`const openclawHome = ${JSON.stringify(safeOpenclawHome)};\n` +
|
|
361
|
-
`const depsMarkerPath = path.join(trackerDir, 'app', 'bin', 'tracker.js');\n` +
|
|
362
371
|
`const triggerStatePath = path.join(trackerDir, 'openclaw.session-sync.trigger-state.json');\n` +
|
|
363
372
|
`const SESSION_TRIGGER_THROTTLE_MS = 15_000;\n` +
|
|
364
373
|
`\n` +
|
|
@@ -405,14 +414,19 @@ function buildSessionPluginIndex({ trackerDir, packageName = "tokenusage-cli", o
|
|
|
405
414
|
`}\n` +
|
|
406
415
|
`\n` +
|
|
407
416
|
`function spawnSync({ args, env = {} }) {\n` +
|
|
408
|
-
`
|
|
409
|
-
`
|
|
417
|
+
` // First usable launcher: init-copied runtime (only with its readiness\n` +
|
|
418
|
+
` // marker), then the installed package entry, then npx.\n` +
|
|
419
|
+
` const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);\n` +
|
|
420
|
+
` const hasPackageEntry = Boolean(packageEntryPath) && fs.existsSync(packageEntryPath);\n` +
|
|
410
421
|
` const argv = Array.isArray(args) && args.length > 0 ? args : ['sync', '--auto'];\n` +
|
|
411
|
-
` const cmd = hasLocalRuntime
|
|
422
|
+
` const cmd = hasLocalRuntime\n` +
|
|
412
423
|
` ? [process.execPath, trackerBinPath, ...argv]\n` +
|
|
413
|
-
` :
|
|
424
|
+
` : hasPackageEntry\n` +
|
|
425
|
+
` ? [process.execPath, packageEntryPath, ...argv]\n` +
|
|
426
|
+
` : ['npx', '--yes', fallbackPkg, ...argv];\n` +
|
|
414
427
|
` const child = cp.spawn(cmd[0], cmd.slice(1), {\n` +
|
|
415
428
|
` detached: true,\n` +
|
|
429
|
+
` windowsHide: true,\n` +
|
|
416
430
|
` stdio: 'ignore',\n` +
|
|
417
431
|
` env: { ...process.env, ...env }\n` +
|
|
418
432
|
` });\n` +
|
package/src/lib/rollout.js
CHANGED
|
@@ -190,8 +190,8 @@ function isRolloutFileName(name) {
|
|
|
190
190
|
// Compressed rollout archives cannot be tailed by byte offset: the cursor
|
|
191
191
|
// stores compressed bytes while the parser consumes decompressed text. Treat
|
|
192
192
|
// them as content-addressed snapshots — one parse per (inode, size) version —
|
|
193
|
-
// reusing the DSH session's
|
|
194
|
-
//
|
|
193
|
+
// reusing the DSH session's built-in zlib zstd machinery, including its
|
|
194
|
+
// multi-frame handling. Returns
|
|
195
195
|
// null for a missing/non-file path; an empty archive yields no lines (the
|
|
196
196
|
// same outcome as an empty uncompressed rollout).
|
|
197
197
|
const CODEX_ROLLOUT_COMPRESSED_PATTERN = /^rollout-.*\.jsonl\.zstd?$/;
|
|
@@ -13174,10 +13174,18 @@ function resolveZedDbPath(env = process.env) {
|
|
|
13174
13174
|
return path.join(xdg, "zed", "threads", "threads.db");
|
|
13175
13175
|
}
|
|
13176
13176
|
|
|
13177
|
+
function decompressBuiltinZstd(data) {
|
|
13178
|
+
const zlib = require("node:zlib");
|
|
13179
|
+
if (typeof zlib.zstdDecompressSync !== "function") {
|
|
13180
|
+
throw new Error("built-in zstd is unavailable; Node.js >= 22.19 is required");
|
|
13181
|
+
}
|
|
13182
|
+
return Buffer.from(zlib.zstdDecompressSync(data));
|
|
13183
|
+
}
|
|
13184
|
+
|
|
13177
13185
|
// Decode a row's BLOB payload into UTF-8 JSON text. Zed marks zstd-compressed
|
|
13178
13186
|
// blobs with data_type="zstd"; older / smaller threads use data_type="json"
|
|
13179
|
-
// and store the bytes verbatim. Node
|
|
13180
|
-
//
|
|
13187
|
+
// and store the bytes verbatim. Node >=22.19 already ships zstd; the package
|
|
13188
|
+
// engine floor guarantees it. Cap decoded size to mirror tokscale's safety net.
|
|
13181
13189
|
async function decodeZedThreadBlob({ dataType, data }) {
|
|
13182
13190
|
const type = (dataType || "").trim().toLowerCase();
|
|
13183
13191
|
if (type === "json") {
|
|
@@ -13187,11 +13195,7 @@ async function decodeZedThreadBlob({ dataType, data }) {
|
|
|
13187
13195
|
return data.toString("utf8");
|
|
13188
13196
|
}
|
|
13189
13197
|
if (type === "zstd") {
|
|
13190
|
-
const
|
|
13191
|
-
const out =
|
|
13192
|
-
typeof zlib.zstdDecompressSync === "function"
|
|
13193
|
-
? zlib.zstdDecompressSync(data)
|
|
13194
|
-
: Buffer.from(await require("@mongodb-js/zstd").decompress(data));
|
|
13198
|
+
const out = decompressBuiltinZstd(data);
|
|
13195
13199
|
if (out.length > MAX_ZED_THREAD_JSON_BYTES) {
|
|
13196
13200
|
throw new Error(`decoded zstd blob exceeds ${MAX_ZED_THREAD_JSON_BYTES} bytes`);
|
|
13197
13201
|
}
|
|
@@ -23870,8 +23874,8 @@ async function parseTraeCnApiIncremental({
|
|
|
23870
23874
|
// Zstd artifacts are a concatenated-frame container: a single decompress call
|
|
23871
23875
|
// decodes only the first frame (silently dropping every event line), so we
|
|
23872
23876
|
// identify the frame boundaries and decode each frame independently —
|
|
23873
|
-
// built-in zlib zstd
|
|
23874
|
-
//
|
|
23877
|
+
// with the built-in Node zlib zstd decoder, enforcing a cumulative plaintext
|
|
23878
|
+
// bound as we go.
|
|
23875
23879
|
// Dedup is a per-file `lastSeq` watermark — seq
|
|
23876
23880
|
// is monotonic within an append-only session log, so a grow re-reads the file
|
|
23877
23881
|
// and skips everything at or below the watermark. Torn tails never advance the
|
|
@@ -24119,18 +24123,11 @@ function inspectDshZstdFrames(data, maxOutputBytes = DSH_SESSION_TEXT_MAX_BYTES)
|
|
|
24119
24123
|
};
|
|
24120
24124
|
}
|
|
24121
24125
|
|
|
24122
|
-
// Decode one already-isolated frame
|
|
24123
|
-
//
|
|
24124
|
-
//
|
|
24125
|
-
// would throw — silently skipping every compressed session (issue #465). The
|
|
24126
|
-
// MongoDB binding stays as the fallback for Node 20 CLI installs, where npm
|
|
24127
|
-
// runs install scripts normally.
|
|
24126
|
+
// Decode one already-isolated frame with Node's built-in zstd. The previous
|
|
24127
|
+
// @mongodb-js/zstd fallback was only reachable on Node 20; the package engine
|
|
24128
|
+
// floor is now >=22.19, so a missing built-in decoder is a hard error.
|
|
24128
24129
|
async function decodeDshZstdFrame(frameBytes) {
|
|
24129
|
-
|
|
24130
|
-
if (typeof zlib.zstdDecompressSync === "function") {
|
|
24131
|
-
return zlib.zstdDecompressSync(frameBytes);
|
|
24132
|
-
}
|
|
24133
|
-
return Buffer.from(await require("@mongodb-js/zstd").decompress(frameBytes));
|
|
24130
|
+
return decompressBuiltinZstd(frameBytes);
|
|
24134
24131
|
}
|
|
24135
24132
|
|
|
24136
24133
|
// Decode one independent Harness append frame at a time; per-frame decoding
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Shared layout constants for the init-copied runtime (`<trackerDir>/app`).
|
|
4
|
+
//
|
|
5
|
+
// init copies the CLI (bin + src + deps) into the data dir so notify hooks and
|
|
6
|
+
// the scheduled task can run `sync` without npx and without depending on npm's
|
|
7
|
+
// global prefix (nvm-windows makes that prefix version-specific). The copy is
|
|
8
|
+
// only usable when `.runtime-ready` exists next to it: every spawn point probes
|
|
9
|
+
// both the launcher and the marker, so a half-copied runtime (missing deps)
|
|
10
|
+
// degrades to the installed package instead of dying silently in an ignored
|
|
11
|
+
// stdio stream — which looks exactly like "collection is running" while
|
|
12
|
+
// nothing is collected.
|
|
13
|
+
|
|
14
|
+
const path = require("node:path");
|
|
15
|
+
|
|
16
|
+
const RUNTIME_READY_MARKER = ".runtime-ready";
|
|
17
|
+
|
|
18
|
+
function runtimeAppDir(trackerDir) {
|
|
19
|
+
return path.join(trackerDir, "app");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function runtimeBinPath(trackerDir) {
|
|
23
|
+
return path.join(runtimeAppDir(trackerDir), "bin", "tracker.js");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function runtimeReadyMarkerPath(appDir) {
|
|
27
|
+
return path.join(appDir, RUNTIME_READY_MARKER);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
RUNTIME_READY_MARKER,
|
|
32
|
+
runtimeAppDir,
|
|
33
|
+
runtimeBinPath,
|
|
34
|
+
runtimeReadyMarkerPath,
|
|
35
|
+
};
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
// init "takes over" the registration that 0.1.0 left to a manual runbook:
|
|
6
6
|
// the task action launches a VBS wrapper (GUI subsystem — no conhost flash
|
|
7
|
-
// on Interactive tasks), which runs `node <
|
|
7
|
+
// on Interactive tasks), which runs `node <bin entry> sync` fully hidden and
|
|
8
8
|
// propagates the exit code via WScript.Quit. The CLI logs to a rolling
|
|
9
9
|
// <trackerDir>/logs/sync.log itself (autoLog sink), so the registration no
|
|
10
10
|
// longer needs a `>>` redirect — which also removes the cmd /c quoting
|
|
@@ -39,31 +39,60 @@ function cliEntryPath() {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
* The hidden-runner wrapper. Paths are quoted individually
|
|
43
|
-
*
|
|
44
|
-
* pitfall (paths with
|
|
42
|
+
* The hidden-runner wrapper. Paths are quoted individually (via Chr(34), so
|
|
43
|
+
* the VBS has no quote-escaping puzzles) and cmd is NOT involved: wscript
|
|
44
|
+
* runs node directly, so the cmd /c outer-quote-stripping pitfall (paths with
|
|
45
|
+
* spaces) never applies.
|
|
46
|
+
*
|
|
47
|
+
* `fallbackCliJs` is the init-copied runtime (trackerDir/app/bin/tracker.js).
|
|
48
|
+
* The primary path lives in npm's global prefix, which nvm-windows makes
|
|
49
|
+
* version-specific: after `nvm use` the package is gone from that prefix and
|
|
50
|
+
* an unconditional launch would fail every hour with "file not found". The
|
|
51
|
+
* fallback keeps collecting (with the last init-copied build) instead.
|
|
45
52
|
*/
|
|
46
|
-
function buildHiddenRunnerVbs({ nodeExe, cliJs }) {
|
|
53
|
+
function buildHiddenRunnerVbs({ nodeExe, cliJs, fallbackCliJs }) {
|
|
47
54
|
// --from-scheduled: the run reports trigger=scheduled, self-logs via autoLog
|
|
48
55
|
// and uploads without consulting the cloud-sync preference — the registered
|
|
49
56
|
// task is the default collection path, not an opt-in flavor.
|
|
50
|
-
|
|
57
|
+
const lines = [
|
|
51
58
|
"' Token Usage — hidden sync runner.",
|
|
52
59
|
"' The scheduled task launches this script (GUI subsystem: no conhost",
|
|
53
60
|
"' flash); it runs the sync fully hidden and returns its exit code so the",
|
|
54
61
|
"' task's LastTaskResult stays meaningful. Sync output lands in the CLI's",
|
|
55
62
|
"' own rolling logs/sync.log — no shell redirect involved.",
|
|
56
|
-
"Dim shell",
|
|
63
|
+
"Dim shell, fso, nodeExe, cliJs, cmd, q",
|
|
57
64
|
"Set shell = CreateObject(\"WScript.Shell\")",
|
|
58
|
-
|
|
65
|
+
"Set fso = CreateObject(\"Scripting.FileSystemObject\")",
|
|
66
|
+
"q = Chr(34)",
|
|
67
|
+
`nodeExe = "${nodeExe}"`,
|
|
68
|
+
`cliJs = "${cliJs}"`,
|
|
69
|
+
];
|
|
70
|
+
if (fallbackCliJs) {
|
|
71
|
+
lines.push(
|
|
72
|
+
"' npm's global prefix is version-specific under nvm-windows, so the",
|
|
73
|
+
"' path above disappears after `nvm use`. init also copies a",
|
|
74
|
+
"' self-contained runtime into the data dir; fall back to it rather than",
|
|
75
|
+
"' failing the hourly run with \"file not found\".",
|
|
76
|
+
`If Not fso.FileExists(cliJs) Then cliJs = "${fallbackCliJs}"`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
lines.push(
|
|
80
|
+
"cmd = q & nodeExe & q & \" \" & q & cliJs & q & \" sync --from-scheduled\"",
|
|
81
|
+
"WScript.Quit shell.Run(cmd, 0, True)",
|
|
59
82
|
"",
|
|
60
|
-
|
|
83
|
+
);
|
|
84
|
+
return lines.join("\n");
|
|
61
85
|
}
|
|
62
86
|
|
|
63
|
-
function writeHiddenRunner({
|
|
87
|
+
function writeHiddenRunner({
|
|
88
|
+
binDir,
|
|
89
|
+
nodeExe = process.execPath,
|
|
90
|
+
cliJs = cliEntryPath(),
|
|
91
|
+
fallbackCliJs,
|
|
92
|
+
}) {
|
|
64
93
|
fs.mkdirSync(binDir, { recursive: true });
|
|
65
94
|
const vbsPath = vbsPathFor(binDir);
|
|
66
|
-
fs.writeFileSync(vbsPath, buildHiddenRunnerVbs({ nodeExe, cliJs }), { mode: 0o600 });
|
|
95
|
+
fs.writeFileSync(vbsPath, buildHiddenRunnerVbs({ nodeExe, cliJs, fallbackCliJs }), { mode: 0o600 });
|
|
67
96
|
return vbsPath;
|
|
68
97
|
}
|
|
69
98
|
|
|
@@ -71,10 +100,15 @@ function writeHiddenRunner({ binDir, nodeExe = process.execPath, cliJs = cliEntr
|
|
|
71
100
|
* Register (idempotently — /F overwrites) the hourly hidden task. Returns
|
|
72
101
|
* {registered: true} or {registered: false, reason} instead of throwing.
|
|
73
102
|
*/
|
|
74
|
-
function registerWindowsScheduledTask({
|
|
103
|
+
function registerWindowsScheduledTask({
|
|
104
|
+
binDir,
|
|
105
|
+
cliJs = cliEntryPath(),
|
|
106
|
+
nodeExe = process.execPath,
|
|
107
|
+
fallbackCliJs,
|
|
108
|
+
}) {
|
|
75
109
|
if (!isWindows()) return { registered: false, reason: "not_windows" };
|
|
76
110
|
try {
|
|
77
|
-
const vbsPath = writeHiddenRunner({ binDir, nodeExe, cliJs });
|
|
111
|
+
const vbsPath = writeHiddenRunner({ binDir, nodeExe, cliJs, fallbackCliJs });
|
|
78
112
|
execFileSync(
|
|
79
113
|
"schtasks",
|
|
80
114
|
[
|
package/test/audit-fixes.test.js
CHANGED
|
@@ -221,10 +221,28 @@ test("init's runtime copy works from the published layout (bin/tokenusage.js)",
|
|
|
221
221
|
const home = fs.mkdtempSync(path.join(os.tmpdir(), "tu-init-copy-"));
|
|
222
222
|
const appDir = path.join(home, "tracker", "app");
|
|
223
223
|
try {
|
|
224
|
-
await installLocalTrackerApp({ appDir });
|
|
224
|
+
const result = await installLocalTrackerApp({ appDir });
|
|
225
|
+
assert.equal(result.ok, true, `installation reported a failure: ${JSON.stringify(result)}`);
|
|
225
226
|
assert.ok(fs.existsSync(path.join(appDir, "bin", "tracker.js")), "app/bin/tracker.js copied");
|
|
226
227
|
assert.ok(fs.existsSync(path.join(appDir, "src", "cli.js")), "app/src copied");
|
|
227
228
|
assert.ok(fs.existsSync(path.join(appDir, "package.json")), "app/package.json copied");
|
|
229
|
+
// 0.2.4: the copy must be self-contained. npm links nothing, but pnpm links
|
|
230
|
+
// deps as junctions into a shared store — copying those links produced a
|
|
231
|
+
// runtime that died with MODULE_NOT_FOUND (one node_modules entry, no deps).
|
|
232
|
+
assert.ok(
|
|
233
|
+
fs.existsSync(path.join(appDir, "node_modules", "smol-toml")),
|
|
234
|
+
"declared dependency copied as a real directory",
|
|
235
|
+
);
|
|
236
|
+
assert.ok(
|
|
237
|
+
fs.existsSync(path.join(appDir, "node_modules", "@sentry", "core")),
|
|
238
|
+
"transitive dependency of @sentry/node copied too (flattened closure)",
|
|
239
|
+
);
|
|
240
|
+
// The marker is what every spawn point probes: without it hooks fall back
|
|
241
|
+
// to the installed CLI instead of spawning a half copy.
|
|
242
|
+
const markerPath = path.join(appDir, ".runtime-ready");
|
|
243
|
+
assert.ok(fs.existsSync(markerPath), ".runtime-ready written only after the copy booted");
|
|
244
|
+
const marker = JSON.parse(fs.readFileSync(markerPath, "utf8"));
|
|
245
|
+
assert.equal(marker.version, require("../package.json").version);
|
|
228
246
|
} finally {
|
|
229
247
|
fs.rmSync(home, { recursive: true, force: true });
|
|
230
248
|
}
|
package/test/cli-options.test.js
CHANGED
|
@@ -16,6 +16,17 @@ test("a valid --interval is adopted and removed from the sync args", () => {
|
|
|
16
16
|
});
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
+
test("--interval=<ms> works too and never leaks into the sync args", () => {
|
|
20
|
+
assert.deepEqual(resolveWatchInterval(["--watch", "--interval=300000", "--no-sessions"]), {
|
|
21
|
+
args: ["--no-sessions"],
|
|
22
|
+
intervalMs: 300_000,
|
|
23
|
+
});
|
|
24
|
+
assert.deepEqual(resolveWatchInterval(["--interval=1000"]), {
|
|
25
|
+
args: [],
|
|
26
|
+
intervalMs: FALLBACK_MS,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
19
30
|
test("a sub-floor or missing --interval falls back to 15 min but is still removed", () => {
|
|
20
31
|
for (const rest of [["--watch", "--interval", "1000"], ["--interval", "0"], ["--watch", "--interval"], ["--interval", "abc"]]) {
|
|
21
32
|
const parsed = resolveWatchInterval(rest);
|