tokenusage-cli 0.2.2 → 0.2.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 +64 -0
- package/README.md +3 -1
- package/THIRD_PARTY_NOTICES.md +4 -128
- package/package.json +2 -2
- package/src/cli.js +26 -12
- package/src/commands/init.js +294 -49
- package/src/commands/sync.js +44 -16
- package/src/commands/uninstall.js +3 -1
- package/src/lib/command-runner.js +3 -0
- package/src/lib/doctor.js +70 -0
- package/src/lib/local-api.js +5 -19
- 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/passive-mode.js +1 -1
- package/src/lib/runtime-entry.js +35 -0
- package/src/lib/scheduled-task.js +55 -13
- package/src/lib/sentry.js +3 -3
- package/src/lib/telemetry-pref.js +28 -0
- package/test/audit-fixes.test.js +19 -1
- package/test/branding.test.js +24 -0
- package/test/cli-options.test.js +43 -0
- package/test/collection-controls.test.js +41 -4
- package/test/packaging.test.js +83 -0
- package/test/runtime-copy.test.js +111 -0
- package/src/lib/telemetry.js +0 -193
package/src/commands/init.js
CHANGED
|
@@ -84,7 +84,8 @@ const {
|
|
|
84
84
|
} = require("../lib/rollout");
|
|
85
85
|
const { resolveRuntimeConfig, DEFAULT_BASE_URL, DEFAULT_DASHBOARD_URL, normalizeInstanceBaseUrl, assertInsforgeRuntime,
|
|
86
86
|
clearDeviceIdentity, resetInstanceState } = require("../lib/runtime-config");
|
|
87
|
-
const { registerWindowsScheduledTask, TASK_NAME } = require("../lib/scheduled-task");
|
|
87
|
+
const { registerWindowsScheduledTask, TASK_NAME, cliEntryPath } = require("../lib/scheduled-task");
|
|
88
|
+
const { runtimeReadyMarkerPath } = require("../lib/runtime-entry");
|
|
88
89
|
const {
|
|
89
90
|
BOLD,
|
|
90
91
|
DIM,
|
|
@@ -105,12 +106,12 @@ const ASCII_LOGO = [
|
|
|
105
106
|
" ██║ ██║ ██║██╔═██╗ ██╔══╝ ██║╚██╗██║",
|
|
106
107
|
" ██║ ╚██████╔╝██║ ██╗███████╗██║ ╚████║",
|
|
107
108
|
" ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝",
|
|
108
|
-
"
|
|
109
|
-
"
|
|
110
|
-
"
|
|
111
|
-
"
|
|
112
|
-
"
|
|
113
|
-
"
|
|
109
|
+
" ██╗ ██╗███████╗ █████╗ ██████╗ ███████╗",
|
|
110
|
+
" ██║ ██║██╔════╝██╔══██╗██╔════╝ ██╔════╝",
|
|
111
|
+
" ██║ ██║███████╗███████║██║ ███╗█████╗",
|
|
112
|
+
" ██║ ██║╚════██║██╔══██║██║ ██║██╔══╝",
|
|
113
|
+
" ╚██████╔╝███████║██║ ██║╚██████╔╝███████╗",
|
|
114
|
+
" ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝",
|
|
114
115
|
].join("\n");
|
|
115
116
|
|
|
116
117
|
const DIVIDER = "----------------------------------------------";
|
|
@@ -290,7 +291,7 @@ function renderWelcome(dashboardUrl = DEFAULT_DASHBOARD_URL) {
|
|
|
290
291
|
[
|
|
291
292
|
ASCII_LOGO,
|
|
292
293
|
"",
|
|
293
|
-
`${BOLD}Token
|
|
294
|
+
`${BOLD}Token Usage${RESET} ${color("Local-first usage across " + providerCount + " AI CLIs", DIM)}`,
|
|
294
295
|
DIVIDER,
|
|
295
296
|
`${CYAN}Nothing leaves your machine — token counts only, never prompts or responses.${RESET}`,
|
|
296
297
|
DIVIDER,
|
|
@@ -388,7 +389,7 @@ async function runSetup({
|
|
|
388
389
|
const installedAt = existingConfig?.installedAt || new Date().toISOString();
|
|
389
390
|
let pendingBrowserAuth = false;
|
|
390
391
|
|
|
391
|
-
await installLocalTrackerApp({ appDir });
|
|
392
|
+
const runtimeCopy = await installLocalTrackerApp({ appDir });
|
|
392
393
|
|
|
393
394
|
const existingPlainConfig =
|
|
394
395
|
existingConfig && typeof existingConfig === "object" && !Array.isArray(existingConfig)
|
|
@@ -443,6 +444,17 @@ async function runSetup({
|
|
|
443
444
|
dryRun: Boolean(opts.dryRun),
|
|
444
445
|
});
|
|
445
446
|
|
|
447
|
+
if (runtimeCopy.ok === false) {
|
|
448
|
+
// Never silent: a missing runtime copy degrades collection to the installed
|
|
449
|
+
// package/npx, so it must show up in the report (and on stderr, since the
|
|
450
|
+
// report line is dimmed as "skipped").
|
|
451
|
+
const detail = describeRuntimeCopyFailure(runtimeCopy);
|
|
452
|
+
process.stderr.write(
|
|
453
|
+
`Runtime copy skipped (${detail}); notify and the scheduled task will use the installed CLI.\n`,
|
|
454
|
+
);
|
|
455
|
+
summary.push({ label: "Runtime copy", status: "skipped", detail });
|
|
456
|
+
}
|
|
457
|
+
|
|
446
458
|
// Windows scheduled task takeover (0.2.0): hourly auto sync behind a hidden
|
|
447
459
|
// VBS runner — no conhost flash, no shell redirect (sync self-logs to the
|
|
448
460
|
// rolling logs/sync.log). Best-effort; pause/resume toggle this task.
|
|
@@ -453,10 +465,16 @@ async function runSetup({
|
|
|
453
465
|
detail: "Skipped (--no-task); auto syncs stay manual",
|
|
454
466
|
});
|
|
455
467
|
} else {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
468
|
+
// The VBS targets the package's bin entry (scheduled-task.js owns that
|
|
469
|
+
// path): pointing it at src/cli.js makes the hidden task a no-op. The
|
|
470
|
+
// runtime copy refresh below is also the task's fallback, so an nvm switch
|
|
471
|
+
// (which drops the version-specific npm prefix) does not kill collection —
|
|
472
|
+
// but only a copy with its readiness marker counts as usable.
|
|
473
|
+
const fallbackCliJs =
|
|
474
|
+
runtimeCopy.ok === true && fssync.existsSync(runtimeReadyMarkerPath(appDir))
|
|
475
|
+
? trackerBinPath
|
|
476
|
+
: undefined;
|
|
477
|
+
const taskOutcome = registerWindowsScheduledTask({ binDir, fallbackCliJs });
|
|
460
478
|
summary.push({
|
|
461
479
|
label: "Scheduled task",
|
|
462
480
|
status: taskOutcome.registered ? "installed" : "skipped",
|
|
@@ -1521,13 +1539,14 @@ function normalizePlatform(value) {
|
|
|
1521
1539
|
return "unknown";
|
|
1522
1540
|
}
|
|
1523
1541
|
|
|
1524
|
-
function buildNotifyHandler({ trackerDir, packageName }) {
|
|
1542
|
+
function buildNotifyHandler({ trackerDir, packageName, packageEntry = cliEntryPath() }) {
|
|
1525
1543
|
// Keep this file dependency-free: Node built-ins only.
|
|
1526
1544
|
// It must never block Codex; it spawns sync in the background and exits 0.
|
|
1527
1545
|
const queueSignalPath = path.join(trackerDir, "notify.signal");
|
|
1528
1546
|
const originalPath = path.join(trackerDir, "codex_notify_original.json");
|
|
1529
1547
|
const fallbackPkg = packageName || "tokenusage-cli";
|
|
1530
1548
|
const trackerBinPath = path.join(trackerDir, "app", "bin", "tracker.js");
|
|
1549
|
+
const runtimeReadyPath = runtimeReadyMarkerPath(path.join(trackerDir, "app"));
|
|
1531
1550
|
|
|
1532
1551
|
return `#!/usr/bin/env node
|
|
1533
1552
|
'use strict';
|
|
@@ -1560,7 +1579,8 @@ const codexOriginalPath = ${JSON.stringify(originalPath)};
|
|
|
1560
1579
|
const acodeOriginalPath = ${JSON.stringify(path.join(trackerDir, "acode_notify_original.json"))};
|
|
1561
1580
|
const codeOriginalPath = ${JSON.stringify(path.join(trackerDir, "code_notify_original.json"))};
|
|
1562
1581
|
const trackerBinPath = ${JSON.stringify(trackerBinPath)};
|
|
1563
|
-
|
|
1582
|
+
const runtimeReadyPath = ${JSON.stringify(runtimeReadyPath)};
|
|
1583
|
+
const packageEntryPath = ${JSON.stringify(packageEntry || null)};
|
|
1564
1584
|
const fallbackPkg = ${JSON.stringify(fallbackPkg)};
|
|
1565
1585
|
const selfPath = path.resolve(__filename);
|
|
1566
1586
|
const home = os.homedir();
|
|
@@ -1603,14 +1623,20 @@ try {
|
|
|
1603
1623
|
try { last = Number(fs.readFileSync(throttlePath, 'utf8')) || 0; } catch (_) {}
|
|
1604
1624
|
if (now - last > 20_000) {
|
|
1605
1625
|
try { fs.writeFileSync(throttlePath, String(now), 'utf8'); } catch (_) {}
|
|
1606
|
-
|
|
1607
|
-
|
|
1626
|
+
// Pick the first usable launcher: the init-copied runtime (self-contained,
|
|
1627
|
+
// survives an nvm switch — but only with its readiness marker), then the
|
|
1628
|
+
// installed package entry, then npx as the last resort. A half-copied
|
|
1629
|
+
// runtime must never be spawned: it dies with MODULE_NOT_FOUND in an
|
|
1630
|
+
// ignored stdio stream, which looks exactly like a successful sync.
|
|
1631
|
+
const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);
|
|
1632
|
+
const hasPackageEntry = Boolean(packageEntryPath) && fs.existsSync(packageEntryPath);
|
|
1608
1633
|
const syncArgs = ['sync', '--auto', '--from-notify', '--source', source];
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1634
|
+
const cmd = hasLocalRuntime
|
|
1635
|
+
? [process.execPath, trackerBinPath, ...syncArgs]
|
|
1636
|
+
: hasPackageEntry
|
|
1637
|
+
? [process.execPath, packageEntryPath, ...syncArgs]
|
|
1638
|
+
: ['npx', '--yes', fallbackPkg, ...syncArgs];
|
|
1639
|
+
spawnDetached(cmd);
|
|
1614
1640
|
}
|
|
1615
1641
|
} catch (_) {}
|
|
1616
1642
|
|
|
@@ -2342,43 +2368,158 @@ async function isDir(p) {
|
|
|
2342
2368
|
}
|
|
2343
2369
|
}
|
|
2344
2370
|
|
|
2371
|
+
// The only modules the runtime copy must carry — the CLI's declared deps.
|
|
2372
|
+
const PACKAGE_DEPENDENCIES = Object.keys(
|
|
2373
|
+
require(path.resolve(__dirname, "../../package.json")).dependencies || {},
|
|
2374
|
+
);
|
|
2375
|
+
|
|
2345
2376
|
async function installLocalTrackerApp({ appDir }) {
|
|
2346
2377
|
// Test-only escape hatch: skip the ~10 MB runtime copy (src + dashboard/dist +
|
|
2347
2378
|
// node_modules) when a test only exercises hook/config/notify wiring. Copying
|
|
2348
2379
|
// the bundle per test case dominates the init test suite's wall time. Tests
|
|
2349
2380
|
// that assert on the copied runtime simply leave this unset.
|
|
2350
|
-
if (process.env.TOKENUSAGE_SKIP_LOCAL_RUNTIME_COPY === "1") return;
|
|
2351
|
-
// Copy the current package's runtime (bin + src) into ~/.tokenusage so
|
|
2381
|
+
if (process.env.TOKENUSAGE_SKIP_LOCAL_RUNTIME_COPY === "1") return { ok: true, skipped: true };
|
|
2382
|
+
// Copy the current package's runtime (bin + src + deps) into ~/.tokenusage so
|
|
2383
|
+
// notify can run sync without npx and without npm's global prefix.
|
|
2352
2384
|
const packageRoot = path.resolve(__dirname, "../..");
|
|
2385
|
+
|
|
2386
|
+
// When running from the installed local runtime (or when appDir is symlinked
|
|
2387
|
+
// to this package), source and destination resolve to the same place. Do not
|
|
2388
|
+
// delete appDir in that case.
|
|
2389
|
+
if (await pathsPointToSameLocation(packageRoot, appDir)) {
|
|
2390
|
+
return { ok: true, reused: true };
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
// Build into a staging dir first: a failed copy must never destroy a working
|
|
2394
|
+
// runtime (or leave a half one behind that hooks would spawn and die in).
|
|
2395
|
+
const stagingDir = `${appDir}.new`;
|
|
2396
|
+
await fs.rm(stagingDir, { recursive: true, force: true, maxRetries: 3 }).catch(() => {});
|
|
2397
|
+
const built = await buildRuntimeCopy({ packageRoot, appDir: stagingDir });
|
|
2398
|
+
if (!built.ok) {
|
|
2399
|
+
await fs.rm(stagingDir, { recursive: true, force: true, maxRetries: 3 }).catch(() => {});
|
|
2400
|
+
return built;
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
await fs.rm(appDir, { recursive: true, force: true, maxRetries: 3 }).catch(() => {});
|
|
2404
|
+
try {
|
|
2405
|
+
await fs.rename(stagingDir, appDir);
|
|
2406
|
+
} catch (_err) {
|
|
2407
|
+
await fs.rm(stagingDir, { recursive: true, force: true, maxRetries: 3 }).catch(() => {});
|
|
2408
|
+
return { ok: false, missing: [], reason: "swap-failed" };
|
|
2409
|
+
}
|
|
2410
|
+
|
|
2411
|
+
// The marker is the contract every spawn point checks: only a copy that
|
|
2412
|
+
// booted gets one.
|
|
2413
|
+
const version = require(path.resolve(__dirname, "../../package.json")).version;
|
|
2414
|
+
await fs
|
|
2415
|
+
.writeFile(
|
|
2416
|
+
runtimeReadyMarkerPath(appDir),
|
|
2417
|
+
JSON.stringify({ version, installedAt: new Date().toISOString() }, null, 2),
|
|
2418
|
+
"utf8",
|
|
2419
|
+
)
|
|
2420
|
+
.catch(() => {});
|
|
2421
|
+
return { ok: true };
|
|
2422
|
+
}
|
|
2423
|
+
|
|
2424
|
+
// Assemble bin + src + the dependency closure in `appDir` and prove it boots.
|
|
2425
|
+
// Returns {ok:true} / {ok:false, missing, reason}.
|
|
2426
|
+
async function buildRuntimeCopy({ packageRoot, appDir }) {
|
|
2353
2427
|
const srcFrom = path.join(packageRoot, "src");
|
|
2354
2428
|
const binFrom = path.join(packageRoot, "bin", "tokenusage.js");
|
|
2355
2429
|
const packageJsonFrom = path.join(packageRoot, "package.json");
|
|
2356
2430
|
const nodeModulesFrom = path.join(packageRoot, "node_modules");
|
|
2357
2431
|
const dashboardDistFrom = path.join(packageRoot, "dashboard", "dist");
|
|
2358
2432
|
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2433
|
+
try {
|
|
2434
|
+
await ensureDir(appDir);
|
|
2435
|
+
await fs.cp(srcFrom, path.join(appDir, "src"), { recursive: true });
|
|
2436
|
+
await ensureDir(path.join(appDir, "bin"));
|
|
2437
|
+
const binTo = path.join(appDir, "bin", "tracker.js");
|
|
2438
|
+
await fs.copyFile(binFrom, binTo);
|
|
2439
|
+
await fs.chmod(binTo, 0o755).catch(() => {});
|
|
2440
|
+
await fs.copyFile(packageJsonFrom, path.join(appDir, "package.json")).catch(() => {});
|
|
2441
|
+
if (await isDir(dashboardDistFrom)) {
|
|
2442
|
+
await fs.cp(dashboardDistFrom, path.join(appDir, "dashboard", "dist"), { recursive: true });
|
|
2443
|
+
}
|
|
2444
|
+
} catch (err) {
|
|
2445
|
+
return { ok: false, missing: [], reason: `copy-failed: ${err?.message || err}` };
|
|
2363
2446
|
}
|
|
2364
2447
|
|
|
2365
|
-
const
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2448
|
+
const deps = await copyRuntimeDependencies({
|
|
2449
|
+
from: nodeModulesFrom,
|
|
2450
|
+
to: path.join(appDir, "node_modules"),
|
|
2451
|
+
});
|
|
2452
|
+
if (!deps.ok) return { ok: false, missing: deps.missing, reason: "missing-dependencies" };
|
|
2370
2453
|
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2454
|
+
const boot = await probeRuntimeCopy(appDir);
|
|
2455
|
+
if (!boot.ok) return { ok: false, missing: boot.missing, reason: boot.reason };
|
|
2456
|
+
return { ok: true };
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
// Boot the staged copy with the current Node: `require` every declared
|
|
2460
|
+
// dependency (which loads their transitive graph too) and print the version
|
|
2461
|
+
// through the real CLI entry. Only a copy that survives both gets a marker.
|
|
2462
|
+
function probeRuntimeCopy(appDir) {
|
|
2463
|
+
const trackerBinPath = path.join(appDir, "bin", "tracker.js");
|
|
2464
|
+
const script = [
|
|
2465
|
+
"const names = " + JSON.stringify(PACKAGE_DEPENDENCIES) + ";",
|
|
2466
|
+
"const missing = [];",
|
|
2467
|
+
"for (const name of names) {",
|
|
2468
|
+
" try { require(name); } catch (err) {",
|
|
2469
|
+
" if (err && (err.code === 'MODULE_NOT_FOUND' || err.code === 'ERR_MODULE_NOT_FOUND')) {",
|
|
2470
|
+
" missing.push(name);",
|
|
2471
|
+
" }",
|
|
2472
|
+
" }",
|
|
2473
|
+
"}",
|
|
2474
|
+
"if (missing.length > 0) { console.error(missing.join(',')); process.exit(3); }",
|
|
2475
|
+
].join("\n");
|
|
2476
|
+
const env = { ...process.env };
|
|
2477
|
+
// The probe must not trigger telemetry or hook spawning.
|
|
2478
|
+
env.TOKENUSAGE_NO_TELEMETRY = "1";
|
|
2479
|
+
try {
|
|
2480
|
+
const probe = cp.spawnSync(process.execPath, ["-e", script], {
|
|
2481
|
+
cwd: appDir,
|
|
2482
|
+
env,
|
|
2483
|
+
encoding: "utf8",
|
|
2484
|
+
windowsHide: true,
|
|
2485
|
+
timeout: 60_000,
|
|
2486
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
2487
|
+
});
|
|
2488
|
+
if (probe.status !== 0) {
|
|
2489
|
+
const reported = String(probe.stderr || "").trim();
|
|
2490
|
+
const missing = reported
|
|
2491
|
+
.split(",")
|
|
2492
|
+
.map((name) => name.trim())
|
|
2493
|
+
.filter(Boolean);
|
|
2494
|
+
return { ok: false, missing, reason: "dependency-probe-failed" };
|
|
2495
|
+
}
|
|
2496
|
+
const boot = cp.spawnSync(process.execPath, [trackerBinPath, "--version"], {
|
|
2497
|
+
cwd: appDir,
|
|
2498
|
+
env,
|
|
2499
|
+
encoding: "utf8",
|
|
2500
|
+
windowsHide: true,
|
|
2501
|
+
timeout: 60_000,
|
|
2502
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
2503
|
+
});
|
|
2504
|
+
if (boot.status !== 0) {
|
|
2505
|
+
return {
|
|
2506
|
+
ok: false,
|
|
2507
|
+
missing: [],
|
|
2508
|
+
reason: `boot-failed: ${String(boot.stderr || "").trim().split("\n")[0] || `exit ${boot.status}`}`,
|
|
2509
|
+
};
|
|
2510
|
+
}
|
|
2511
|
+
return { ok: true };
|
|
2512
|
+
} catch (err) {
|
|
2513
|
+
return { ok: false, missing: [], reason: `probe-error: ${err?.message || err}` };
|
|
2380
2514
|
}
|
|
2381
|
-
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
function describeRuntimeCopyFailure(result) {
|
|
2518
|
+
const missing = Array.isArray(result?.missing) ? result.missing.filter(Boolean) : [];
|
|
2519
|
+
const reason = result?.reason ? String(result.reason) : "incomplete copy";
|
|
2520
|
+
if (missing.length === 0) return reason;
|
|
2521
|
+
const preview = missing.slice(0, 3).join(", ");
|
|
2522
|
+
return `${reason}: ${preview}${missing.length > 3 ? " …" : ""}`;
|
|
2382
2523
|
}
|
|
2383
2524
|
|
|
2384
2525
|
async function pathsPointToSameLocation(a, b) {
|
|
@@ -2411,7 +2552,12 @@ async function runFirstSyncAndRead({ trackerBinPath, trackerDir, packageName })
|
|
|
2411
2552
|
}
|
|
2412
2553
|
const fallbackPkg = packageName || "tokenusage-cli";
|
|
2413
2554
|
const argv = ["sync", "--auto", "--drain"];
|
|
2414
|
-
|
|
2555
|
+
// Same readiness contract as notify: a runtime copy without its marker is a
|
|
2556
|
+
// half copy — spawn the installed package instead of dying silently.
|
|
2557
|
+
const hasLocalRuntime =
|
|
2558
|
+
typeof trackerBinPath === "string" &&
|
|
2559
|
+
fssync.existsSync(trackerBinPath) &&
|
|
2560
|
+
fssync.existsSync(runtimeReadyMarkerPath(path.join(trackerDir, "app")));
|
|
2415
2561
|
const cmd = hasLocalRuntime
|
|
2416
2562
|
? [process.execPath, trackerBinPath, ...argv]
|
|
2417
2563
|
: ["npx", "--yes", fallbackPkg, ...argv];
|
|
@@ -2431,6 +2577,7 @@ async function runFirstSyncAndRead({ trackerBinPath, trackerDir, packageName })
|
|
|
2431
2577
|
// detached so we can let it keep running past our timeout — the user
|
|
2432
2578
|
// still gets data later via dashboard auto-refresh.
|
|
2433
2579
|
detached: true,
|
|
2580
|
+
windowsHide: true,
|
|
2434
2581
|
stdio: "ignore",
|
|
2435
2582
|
env: process.env,
|
|
2436
2583
|
});
|
|
@@ -2486,19 +2633,117 @@ function readFirstSyncTotals(trackerDir) {
|
|
|
2486
2633
|
return { totalTokens, sources: Array.from(sources) };
|
|
2487
2634
|
}
|
|
2488
2635
|
|
|
2636
|
+
// Copy the CLI's dependency closure into `to` as a flat node_modules tree.
|
|
2637
|
+
//
|
|
2638
|
+
// npm installs the CLI's deps flat (real directories), so copying them is
|
|
2639
|
+
// enough. pnpm links them as junctions into a shared store: copying the links
|
|
2640
|
+
// yields a runtime that breaks as soon as that store entry moves (nvm switch),
|
|
2641
|
+
// and NOT following them yields a runtime with no deps at all. So walk the
|
|
2642
|
+
// declared dependency graph and copy each package's *real* directory instead —
|
|
2643
|
+
// that flattens both layouts into something self-contained.
|
|
2489
2644
|
async function copyRuntimeDependencies({ from, to }) {
|
|
2645
|
+
if (!(await isDir(from))) return { ok: false, missing: PACKAGE_DEPENDENCIES };
|
|
2646
|
+
|
|
2647
|
+
const { packages } = collectRuntimePackages(from);
|
|
2490
2648
|
try {
|
|
2491
|
-
|
|
2492
|
-
|
|
2649
|
+
await ensureDir(to);
|
|
2650
|
+
for (const [name, dir] of packages) {
|
|
2651
|
+
await fs.cp(dir, path.join(to, name), {
|
|
2652
|
+
recursive: true,
|
|
2653
|
+
dereference: true,
|
|
2654
|
+
filter: skipNestedNodeModules(dir),
|
|
2655
|
+
});
|
|
2656
|
+
}
|
|
2493
2657
|
} catch (_e) {
|
|
2494
|
-
return;
|
|
2658
|
+
return { ok: false, missing: await missingRuntimeDependencies(to) };
|
|
2659
|
+
}
|
|
2660
|
+
const missing = await missingRuntimeDependencies(to);
|
|
2661
|
+
return { ok: missing.length === 0, missing };
|
|
2662
|
+
}
|
|
2663
|
+
|
|
2664
|
+
// Breadth-first walk of the declared dependency graph. Follows the same lookup
|
|
2665
|
+
// Node does for each package — its own node_modules, then the node_modules next
|
|
2666
|
+
// to it (pnpm: `.pnpm/<id>/node_modules`, npm: the flat root) — so nothing from
|
|
2667
|
+
// the hoisted store leaks in. Optional deps are followed when present but their
|
|
2668
|
+
// absence is not an error.
|
|
2669
|
+
function collectRuntimePackages(nodeModulesRoot) {
|
|
2670
|
+
const packages = new Map();
|
|
2671
|
+
const unresolved = [];
|
|
2672
|
+
const queue = PACKAGE_DEPENDENCIES.map((name) => ({ name, from: null, optional: false }));
|
|
2673
|
+
while (queue.length > 0) {
|
|
2674
|
+
const item = queue.shift();
|
|
2675
|
+
if (packages.has(item.name)) continue;
|
|
2676
|
+
const dir =
|
|
2677
|
+
item.from === null
|
|
2678
|
+
? realDirSync(path.join(nodeModulesRoot, item.name))
|
|
2679
|
+
: resolveDependencyPackage(item.from, item.name);
|
|
2680
|
+
const manifest = dir ? readPackageJsonSync(dir) : null;
|
|
2681
|
+
if (!dir || !manifest) {
|
|
2682
|
+
if (!item.optional) unresolved.push(item.name);
|
|
2683
|
+
continue;
|
|
2684
|
+
}
|
|
2685
|
+
packages.set(item.name, dir);
|
|
2686
|
+
const required = Object.keys(manifest.dependencies || {});
|
|
2687
|
+
for (const dep of required) queue.push({ name: dep, from: dir, optional: false });
|
|
2688
|
+
for (const dep of Object.keys(manifest.optionalDependencies || {})) {
|
|
2689
|
+
queue.push({ name: dep, from: dir, optional: true });
|
|
2690
|
+
}
|
|
2495
2691
|
}
|
|
2692
|
+
return { packages, unresolved };
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
function resolveDependencyPackage(packageDir, name) {
|
|
2696
|
+
const parentNodeModules = name.startsWith("@")
|
|
2697
|
+
? path.dirname(path.dirname(packageDir))
|
|
2698
|
+
: path.dirname(packageDir);
|
|
2699
|
+
return (
|
|
2700
|
+
realDirSync(path.join(packageDir, "node_modules", name)) ||
|
|
2701
|
+
realDirSync(path.join(parentNodeModules, name))
|
|
2702
|
+
);
|
|
2703
|
+
}
|
|
2704
|
+
|
|
2705
|
+
// Packages that ship their own node_modules keep it linked to pnpm's shared
|
|
2706
|
+
// store; dereferencing those links would drag the entire workspace dependency
|
|
2707
|
+
// graph into the runtime copy.
|
|
2708
|
+
function skipNestedNodeModules(from) {
|
|
2709
|
+
return (src) => {
|
|
2710
|
+
const rel = path.relative(from, src);
|
|
2711
|
+
return !rel || !rel.split(path.sep).includes("node_modules");
|
|
2712
|
+
};
|
|
2713
|
+
}
|
|
2496
2714
|
|
|
2715
|
+
function realDirSync(p) {
|
|
2716
|
+
if (!isDirSync(p)) return null;
|
|
2497
2717
|
try {
|
|
2498
|
-
|
|
2718
|
+
const real = fssync.realpathSync(p);
|
|
2719
|
+
return isDirSync(real) ? real : null;
|
|
2499
2720
|
} catch (_e) {
|
|
2500
|
-
|
|
2721
|
+
return null;
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
function isDirSync(p) {
|
|
2726
|
+
try {
|
|
2727
|
+
return fssync.statSync(p).isDirectory();
|
|
2728
|
+
} catch (_e) {
|
|
2729
|
+
return false;
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
function readPackageJsonSync(dir) {
|
|
2734
|
+
try {
|
|
2735
|
+
return JSON.parse(fssync.readFileSync(path.join(dir, "package.json"), "utf8"));
|
|
2736
|
+
} catch (_e) {
|
|
2737
|
+
return null;
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
async function missingRuntimeDependencies(nodeModulesDir) {
|
|
2742
|
+
const missing = [];
|
|
2743
|
+
for (const name of PACKAGE_DEPENDENCIES) {
|
|
2744
|
+
if (!(await isDir(path.join(nodeModulesDir, name)))) missing.push(name);
|
|
2501
2745
|
}
|
|
2746
|
+
return missing;
|
|
2502
2747
|
}
|
|
2503
2748
|
|
|
2504
2749
|
function isDebugEnabled() {
|
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");
|
|
@@ -174,7 +176,6 @@ const AUTO_UPLOAD_CONFIG = {
|
|
|
174
176
|
maxBatchesLarge: 100,
|
|
175
177
|
};
|
|
176
178
|
const UPLOAD_BUDGET_MS = 90_000;
|
|
177
|
-
const { maybeSendHeartbeat } = require("../lib/telemetry");
|
|
178
179
|
const {
|
|
179
180
|
isCursorInstalled,
|
|
180
181
|
extractCursorSessionToken,
|
|
@@ -638,7 +639,10 @@ async function cmdSync(argv, context = {}) {
|
|
|
638
639
|
// try/finally so an auxiliary diagnostic never strands the sync lock.
|
|
639
640
|
await clearSyncSkip(trackerDir);
|
|
640
641
|
progress = !opts.auto ? createProgress({ stream: process.stdout }) : null;
|
|
641
|
-
|
|
642
|
+
// Every auto-flavored run (scheduled task, hooks, retry, watch) self-logs:
|
|
643
|
+
// a hidden TTY-less task has no other trail, so --from-scheduled must not
|
|
644
|
+
// fall through to the interactive stdout summary below.
|
|
645
|
+
if (autoishSync) {
|
|
642
646
|
autoLog("Auto sync started — scanning local provider sessions…");
|
|
643
647
|
}
|
|
644
648
|
const configPath = path.join(trackerDir, "config.json");
|
|
@@ -3878,7 +3882,7 @@ async function cmdSync(argv, context = {}) {
|
|
|
3878
3882
|
}
|
|
3879
3883
|
}
|
|
3880
3884
|
|
|
3881
|
-
if (!
|
|
3885
|
+
if (!autoishSync) {
|
|
3882
3886
|
const codexRecordOnlyWarning = formatRecordOnlyWarning(countRecordOnlyFiles(cursors));
|
|
3883
3887
|
process.stdout.write(
|
|
3884
3888
|
[
|
|
@@ -3907,13 +3911,6 @@ async function cmdSync(argv, context = {}) {
|
|
|
3907
3911
|
);
|
|
3908
3912
|
}
|
|
3909
3913
|
|
|
3910
|
-
// Anonymous daily heartbeat (shared 24h throttle with serve — see
|
|
3911
|
-
// src/lib/telemetry.js). Awaited because hook-spawned sync processes exit
|
|
3912
|
-
// right after this function returns, which would kill an in-flight
|
|
3913
|
-
// request; the throttle makes it a network no-op on all but the first
|
|
3914
|
-
// sync of the day, and maybeSendHeartbeat never throws.
|
|
3915
|
-
await maybeSendHeartbeat({ trackerDir });
|
|
3916
|
-
|
|
3917
3914
|
// §2.11 run record (empty runs included) — the last thing a run does,
|
|
3918
3915
|
// strictly best-effort: failures only reach the local log, never the
|
|
3919
3916
|
// ingest outcome above or this process's exit code.
|
|
@@ -4463,7 +4460,9 @@ async function scheduleAutoRetry({
|
|
|
4463
4460
|
|
|
4464
4461
|
spawnAutoRetryProcess({
|
|
4465
4462
|
retryPath,
|
|
4466
|
-
trackerBinPath:
|
|
4463
|
+
trackerBinPath: runtimeBinPath(trackerDir),
|
|
4464
|
+
runtimeReadyPath: runtimeReadyMarkerPath(path.join(trackerDir, "app")),
|
|
4465
|
+
packageEntryPath: cliEntryPath(),
|
|
4467
4466
|
fallbackPkg: "tokenusage-cli",
|
|
4468
4467
|
delayMs,
|
|
4469
4468
|
});
|
|
@@ -4489,8 +4488,22 @@ async function clearAutoRetry(trackerDir) {
|
|
|
4489
4488
|
await fs.unlink(retryPath).catch(() => {});
|
|
4490
4489
|
}
|
|
4491
4490
|
|
|
4492
|
-
function spawnAutoRetryProcess({
|
|
4493
|
-
|
|
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
|
+
});
|
|
4494
4507
|
try {
|
|
4495
4508
|
const child = cp.spawn(process.execPath, ["-e", script], {
|
|
4496
4509
|
detached: true,
|
|
@@ -4505,13 +4518,22 @@ function spawnAutoRetryProcess({ retryPath, trackerBinPath, fallbackPkg, delayMs
|
|
|
4505
4518
|
} catch (_e) {}
|
|
4506
4519
|
}
|
|
4507
4520
|
|
|
4508
|
-
function buildAutoRetryScript({
|
|
4521
|
+
function buildAutoRetryScript({
|
|
4522
|
+
retryPath,
|
|
4523
|
+
trackerBinPath,
|
|
4524
|
+
runtimeReadyPath,
|
|
4525
|
+
packageEntryPath,
|
|
4526
|
+
fallbackPkg,
|
|
4527
|
+
delayMs,
|
|
4528
|
+
}) {
|
|
4509
4529
|
return (
|
|
4510
4530
|
`'use strict';\n` +
|
|
4511
4531
|
`const fs = require('node:fs');\n` +
|
|
4512
4532
|
`const cp = require('node:child_process');\n` +
|
|
4513
4533
|
`const retryPath = ${JSON.stringify(retryPath)};\n` +
|
|
4514
4534
|
`const trackerBinPath = ${JSON.stringify(trackerBinPath)};\n` +
|
|
4535
|
+
`const runtimeReadyPath = ${JSON.stringify(runtimeReadyPath)};\n` +
|
|
4536
|
+
`const packageEntryPath = ${JSON.stringify(packageEntryPath || null)};\n` +
|
|
4515
4537
|
`const fallbackPkg = ${JSON.stringify(fallbackPkg)};\n` +
|
|
4516
4538
|
`const delayMs = ${Math.max(0, Math.floor(delayMs || 0))};\n` +
|
|
4517
4539
|
`setTimeout(() => {\n` +
|
|
@@ -4527,9 +4549,15 @@ function buildAutoRetryScript({ retryPath, trackerBinPath, fallbackPkg, delayMs
|
|
|
4527
4549
|
` if (payload && typeof payload.syncSource === 'string' && payload.syncSource.trim()) {\n` +
|
|
4528
4550
|
` argv.push('--source', payload.syncSource.trim());\n` +
|
|
4529
4551
|
` }\n` +
|
|
4530
|
-
`
|
|
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` +
|
|
4531
4557
|
` ? [process.execPath, trackerBinPath, ...argv]\n` +
|
|
4532
|
-
` :
|
|
4558
|
+
` : hasPackageEntry\n` +
|
|
4559
|
+
` ? [process.execPath, packageEntryPath, ...argv]\n` +
|
|
4560
|
+
` : ['npx', '--yes', fallbackPkg, ...argv];\n` +
|
|
4533
4561
|
` try {\n` +
|
|
4534
4562
|
` const child = cp.spawn(cmd[0], cmd.slice(1), { detached: true, windowsHide: true, stdio: 'ignore', env: process.env });\n` +
|
|
4535
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
|
|
@@ -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"],
|