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
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Packaging guards: what `npm pack` ships must actually boot. 0.1.x shipped
|
|
2
|
+
// without bin/tracker.js (installer still pointed at the pre-fork name) and
|
|
3
|
+
// local-api's dormant local server still resolved that missing path. The
|
|
4
|
+
// upstream install-count heartbeat was also never ported to this backend.
|
|
5
|
+
|
|
6
|
+
const assert = require("node:assert/strict");
|
|
7
|
+
const fs = require("node:fs");
|
|
8
|
+
const path = require("node:path");
|
|
9
|
+
const { test } = require("node:test");
|
|
10
|
+
|
|
11
|
+
const PACKAGE_ROOT = path.join(__dirname, "..");
|
|
12
|
+
const SRC = path.join(PACKAGE_ROOT, "src");
|
|
13
|
+
|
|
14
|
+
function readSource(...segments) {
|
|
15
|
+
return fs.readFileSync(path.join(PACKAGE_ROOT, ...segments), "utf8");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
test("package bin points at the CLI entry that actually ships", () => {
|
|
19
|
+
const pkg = JSON.parse(readSource("package.json"));
|
|
20
|
+
assert.equal(pkg.bin.tokenusage, "bin/tokenusage.js");
|
|
21
|
+
assert.ok(
|
|
22
|
+
fs.existsSync(path.join(PACKAGE_ROOT, pkg.bin.tokenusage)),
|
|
23
|
+
"the declared bin file must exist in the tarball",
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("local-api spawns the package bin entry, not the missing bin/tracker.js", () => {
|
|
28
|
+
const source = readSource("src", "lib", "local-api.js");
|
|
29
|
+
assert.ok(!source.includes('"../../bin/tracker.js"'), "bin/tracker.js never existed after the fork");
|
|
30
|
+
const match = source.match(/TRACKER_BIN = path\.resolve\(__dirname, "([^"]+)"\)/);
|
|
31
|
+
assert.ok(match, "TRACKER_BIN must stay a single resolved path");
|
|
32
|
+
assert.ok(
|
|
33
|
+
fs.existsSync(path.resolve(path.join(SRC, "lib"), match[1])),
|
|
34
|
+
`local-api spawn target does not resolve: ${match[1]}`,
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("telemetry opt-out flags still gate the optional Sentry reporting", () => {
|
|
39
|
+
const { isTelemetryDisabled } = require("../src/lib/telemetry-pref");
|
|
40
|
+
assert.equal(isTelemetryDisabled({ env: {}, config: {} }), false);
|
|
41
|
+
assert.equal(isTelemetryDisabled({ env: { TOKENUSAGE_NO_TELEMETRY: "1" }, config: {} }), true);
|
|
42
|
+
assert.equal(isTelemetryDisabled({ env: { DO_NOT_TRACK: "true" }, config: {} }), true);
|
|
43
|
+
assert.equal(isTelemetryDisabled({ env: {}, config: { telemetry: false } }), true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("the unported install heartbeat stays removed", () => {
|
|
47
|
+
assert.ok(
|
|
48
|
+
!fs.existsSync(path.join(SRC, "lib", "telemetry.js")),
|
|
49
|
+
"the backend has no /api/functions/tokenusage-telemetry route, so nothing may post to it",
|
|
50
|
+
);
|
|
51
|
+
const sync = readSource("src", "commands", "sync.js");
|
|
52
|
+
assert.ok(!sync.includes("maybeSendHeartbeat"));
|
|
53
|
+
assert.ok(!sync.includes('require("../lib/telemetry")'));
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("every auto-sync spawn point gates on the runtime-copy readiness marker", () => {
|
|
57
|
+
// 0.2.4: a runtime copy without its marker is a half copy (missing deps) and
|
|
58
|
+
// dies with MODULE_NOT_FOUND inside an ignored stdio stream — collection
|
|
59
|
+
// looks alive while nothing is collected. Each generated/hidden launcher must
|
|
60
|
+
// check the marker before choosing it.
|
|
61
|
+
const init = readSource("src", "commands", "init.js");
|
|
62
|
+
assert.ok(init.includes("runtimeReadyMarkerPath"), "init.js must write and probe the marker");
|
|
63
|
+
|
|
64
|
+
const notify = readSource("src", "commands", "init.js");
|
|
65
|
+
assert.ok(
|
|
66
|
+
notify.includes("const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);"),
|
|
67
|
+
"the notify handler must require both the launcher and the marker",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
for (const file of [["src", "lib", "openclaw-hook.js"], ["src", "lib", "openclaw-session-plugin.js"]]) {
|
|
71
|
+
const source = readSource(...file);
|
|
72
|
+
assert.ok(
|
|
73
|
+
source.includes("runtimeReadyPath") && source.includes("packageEntryPath"),
|
|
74
|
+
`${file.join("/")} must bake the marker + installed-package fallback`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const sync = readSource("src", "commands", "sync.js");
|
|
79
|
+
assert.ok(
|
|
80
|
+
sync.includes("runtimeReadyPath") && sync.includes("packageEntryPath"),
|
|
81
|
+
"the auto-retry script must use the same launcher selection",
|
|
82
|
+
);
|
|
83
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The init-copied runtime (`<trackerDir>/app`) is the launcher behind notify
|
|
4
|
+
// hooks, the hidden scheduled task and the auto-retry child. 0.2.4 made its
|
|
5
|
+
// readiness marker the single gate: a copy that never booted must never be
|
|
6
|
+
// spawned (it dies with MODULE_NOT_FOUND inside an ignored stdio stream, which
|
|
7
|
+
// is indistinguishable from "collection is running"), and `doctor` must say so.
|
|
8
|
+
|
|
9
|
+
const assert = require("node:assert/strict");
|
|
10
|
+
const fs = require("node:fs");
|
|
11
|
+
const os = require("node:os");
|
|
12
|
+
const path = require("node:path");
|
|
13
|
+
const { test } = require("node:test");
|
|
14
|
+
|
|
15
|
+
const { buildNotifyHandler } = require("../src/commands/init");
|
|
16
|
+
const { buildDoctorReport } = require("../src/lib/doctor");
|
|
17
|
+
const { runtimeAppDir, runtimeBinPath, runtimeReadyMarkerPath, RUNTIME_READY_MARKER } = require("../src/lib/runtime-entry");
|
|
18
|
+
|
|
19
|
+
const CLI_VERSION = require("../package.json").version;
|
|
20
|
+
|
|
21
|
+
async function withTrackerDir(fn) {
|
|
22
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), "tu-runtime-copy-"));
|
|
23
|
+
const trackerDir = path.join(home, "tracker");
|
|
24
|
+
fs.mkdirSync(trackerDir, { recursive: true });
|
|
25
|
+
try {
|
|
26
|
+
return await fn({ home, trackerDir });
|
|
27
|
+
} finally {
|
|
28
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function runtimeCopyCheck(trackerDir) {
|
|
33
|
+
const report = await buildDoctorReport({
|
|
34
|
+
runtime: { baseUrl: "https://example.test", deviceToken: "present", dashboardUrl: "https://example.test" },
|
|
35
|
+
diagnostics: null,
|
|
36
|
+
fetch: async () => ({ ok: true, status: 200 }),
|
|
37
|
+
paths: { trackerDir, configPath: path.join(trackerDir, "config.json") },
|
|
38
|
+
});
|
|
39
|
+
return report.checks.find((check) => check.id === "runtime.copy");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("runtime-entry: the marker lives next to the copy, not inside bin/", () => {
|
|
43
|
+
const trackerDir = path.join("home", ".tokenusage", "tracker");
|
|
44
|
+
assert.equal(runtimeAppDir(trackerDir), path.join(trackerDir, "app"));
|
|
45
|
+
assert.equal(runtimeBinPath(trackerDir), path.join(trackerDir, "app", "bin", "tracker.js"));
|
|
46
|
+
assert.equal(
|
|
47
|
+
runtimeReadyMarkerPath(runtimeAppDir(trackerDir)),
|
|
48
|
+
path.join(trackerDir, "app", RUNTIME_READY_MARKER),
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("doctor: no runtime copy is fine (hooks use the installed CLI)", async () => {
|
|
53
|
+
const check = await withTrackerDir(({ trackerDir }) => runtimeCopyCheck(trackerDir));
|
|
54
|
+
assert.equal(check.status, "ok");
|
|
55
|
+
assert.match(check.detail, /installed CLI/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("doctor: a copy without its marker is reported as incomplete", async () => {
|
|
59
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
60
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
61
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
62
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// half copy\n");
|
|
63
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
64
|
+
assert.equal(check.status, "warn");
|
|
65
|
+
assert.match(check.detail, /incomplete/);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("doctor: a ready copy reports its version", async () => {
|
|
70
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
71
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
72
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
73
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// copy\n");
|
|
74
|
+
fs.writeFileSync(
|
|
75
|
+
runtimeReadyMarkerPath(appDir),
|
|
76
|
+
JSON.stringify({ version: CLI_VERSION, installedAt: "2026-10-28T00:00:00.000Z" }),
|
|
77
|
+
);
|
|
78
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
79
|
+
assert.equal(check.status, "ok");
|
|
80
|
+
assert.match(check.detail, new RegExp(`v${CLI_VERSION.replace(/\./g, "\\.")}`));
|
|
81
|
+
assert.equal(check.meta.version, CLI_VERSION);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("doctor: a copy from an older CLI is flagged for a re-run", async () => {
|
|
86
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
87
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
88
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
89
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// copy\n");
|
|
90
|
+
fs.writeFileSync(runtimeReadyMarkerPath(appDir), JSON.stringify({ version: "0.0.1" }));
|
|
91
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
92
|
+
assert.equal(check.status, "warn");
|
|
93
|
+
assert.match(check.detail, /older than the CLI/);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("notify handler: marker + installed-package fallback baked into the template", () => {
|
|
98
|
+
const source = buildNotifyHandler({ trackerDir: path.join("C:", "home", ".tokenusage", "tracker") });
|
|
99
|
+
assert.ok(source.includes("runtimeReadyPath"), "the template must probe the readiness marker");
|
|
100
|
+
assert.ok(source.includes("packageEntryPath"), "the template must carry the installed CLI entry");
|
|
101
|
+
assert.ok(
|
|
102
|
+
source.includes("const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);"),
|
|
103
|
+
"both the launcher and the marker are required",
|
|
104
|
+
);
|
|
105
|
+
assert.ok(
|
|
106
|
+
source.includes(": hasPackageEntry\n ? [process.execPath, packageEntryPath, ...syncArgs]"),
|
|
107
|
+
"the middle tier runs the installed package without npx",
|
|
108
|
+
);
|
|
109
|
+
// The generated file must stay dependency-free: only node built-ins.
|
|
110
|
+
assert.ok(!/require\(['"](?!node:)/.test(source), "notify.cjs must only require node: built-ins");
|
|
111
|
+
});
|
package/src/lib/telemetry.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Anonymous daily heartbeat for counting active installs.
|
|
5
|
-
*
|
|
6
|
-
* Privacy contract (documented in README "Privacy" section — keep in sync):
|
|
7
|
-
* the payload carries exactly four fields — a one-way sha256 hash of the
|
|
8
|
-
* machine id, the app version, `process.platform`, and the app shell
|
|
9
|
-
* (cli / macos / windows / linux). No token counts, no model names, no usernames,
|
|
10
|
-
* no paths. Opt out with TOKENUSAGE_NO_TELEMETRY=1, the DO_NOT_TRACK
|
|
11
|
-
* standard, or `"telemetry": false` in config.json.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
const path = require("node:path");
|
|
15
|
-
const crypto = require("node:crypto");
|
|
16
|
-
|
|
17
|
-
const pkg = require("../../package.json");
|
|
18
|
-
const { readJson, writeJson } = require("./fs");
|
|
19
|
-
const { getOrCreateMachineId } = require("./machine-id");
|
|
20
|
-
const { resolveRuntimeConfig } = require("./runtime-config");
|
|
21
|
-
const { functionUrlFor, fetchFunctionResponse } = require("./function-url");
|
|
22
|
-
const {
|
|
23
|
-
decideAutoUpload,
|
|
24
|
-
recordUploadSuccess,
|
|
25
|
-
recordUploadFailure,
|
|
26
|
-
} = require("./upload-throttle");
|
|
27
|
-
|
|
28
|
-
const HEARTBEAT_FUNCTION_SLUG = "tokenusage-telemetry";
|
|
29
|
-
const HEARTBEAT_STATE_FILE = "telemetry.heartbeat.json";
|
|
30
|
-
const HEARTBEAT_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
31
|
-
// The hash namespace is versioned so the anonymous id can be rotated fleet-wide
|
|
32
|
-
// by bumping the string. sha256(namespace + machineId) cannot be joined back to
|
|
33
|
-
// the cloud device rows (those store the machineId itself).
|
|
34
|
-
const MACHINE_HASH_NAMESPACE = "tokenusage-telemetry-v1:";
|
|
35
|
-
// Generous because some networks need 3-4s just for the TLS handshake to the
|
|
36
|
-
// cloud host; sync awaits this at most once per day so the worst-case stall
|
|
37
|
-
// is bounded and rare.
|
|
38
|
-
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
39
|
-
const VALID_SHELLS = new Set(["cli", "macos", "windows", "linux"]);
|
|
40
|
-
|
|
41
|
-
const THROTTLE_CONFIG = {
|
|
42
|
-
intervalMs: HEARTBEAT_INTERVAL_MS,
|
|
43
|
-
jitterMsMax: 60_000,
|
|
44
|
-
backoffInitialMs: 60 * 60_000, // failures retry hourly at most
|
|
45
|
-
backoffMaxMs: HEARTBEAT_INTERVAL_MS,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
function isTelemetryDisabled({ env = process.env, config = {} } = {}) {
|
|
49
|
-
if (isTruthyFlag(env?.TOKENUSAGE_NO_TELEMETRY)) return true;
|
|
50
|
-
if (isTruthyFlag(env?.DO_NOT_TRACK)) return true;
|
|
51
|
-
if (config && config.telemetry === false) return true;
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isTruthyFlag(value) {
|
|
56
|
-
if (typeof value !== "string") return false;
|
|
57
|
-
const v = value.trim().toLowerCase();
|
|
58
|
-
return v === "1" || v === "true";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function hashMachineId(machineId) {
|
|
62
|
-
return crypto
|
|
63
|
-
.createHash("sha256")
|
|
64
|
-
.update(`${MACHINE_HASH_NAMESPACE}${machineId}`)
|
|
65
|
-
.digest("hex");
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function resolveShell(env = process.env) {
|
|
69
|
-
const raw = typeof env?.TOKENUSAGE_APP_SHELL === "string"
|
|
70
|
-
? env.TOKENUSAGE_APP_SHELL.trim().toLowerCase()
|
|
71
|
-
: "";
|
|
72
|
-
return VALID_SHELLS.has(raw) ? raw : "cli";
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function buildHeartbeatPayload({ machineId, version = pkg.version, platform = process.platform, env = process.env }) {
|
|
76
|
-
return {
|
|
77
|
-
machine_hash: hashMachineId(machineId),
|
|
78
|
-
app_version: String(version || "").slice(0, 32),
|
|
79
|
-
platform: String(platform || "unknown").slice(0, 16),
|
|
80
|
-
shell: resolveShell(env),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Send the daily heartbeat if allowed. Never throws; every failure path is a
|
|
86
|
-
* silent no-op so serve/sync flow is never affected. Returns a small status
|
|
87
|
-
* object for tests and debug logging.
|
|
88
|
-
*/
|
|
89
|
-
async function maybeSendHeartbeat({
|
|
90
|
-
trackerDir,
|
|
91
|
-
fetchImpl = fetch,
|
|
92
|
-
nowMs = Date.now(),
|
|
93
|
-
env = process.env,
|
|
94
|
-
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
95
|
-
version,
|
|
96
|
-
} = {}) {
|
|
97
|
-
try {
|
|
98
|
-
if (!trackerDir) return { sent: false, reason: "no-tracker-dir" };
|
|
99
|
-
|
|
100
|
-
// Never phone home from the node test runner: many tests drive cmdSync /
|
|
101
|
-
// cmdServe directly from sandboxed temp homes, and their fresh throttle
|
|
102
|
-
// state would let a real POST through to production on every run. Tests
|
|
103
|
-
// that exercise this module inject their own env without this marker.
|
|
104
|
-
if (env?.NODE_TEST_CONTEXT !== undefined) {
|
|
105
|
-
return { sent: false, reason: "test-env" };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const configPath = path.join(trackerDir, "config.json");
|
|
109
|
-
const config = (await readJson(configPath)) || {};
|
|
110
|
-
if (isTelemetryDisabled({ env, config })) {
|
|
111
|
-
return { sent: false, reason: "opt-out" };
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const statePath = path.join(trackerDir, HEARTBEAT_STATE_FILE);
|
|
115
|
-
const state = await readJson(statePath);
|
|
116
|
-
const decision = decideAutoUpload({
|
|
117
|
-
nowMs,
|
|
118
|
-
pendingBytes: 1, // heartbeats always have "one ping pending"
|
|
119
|
-
state,
|
|
120
|
-
config: THROTTLE_CONFIG,
|
|
121
|
-
});
|
|
122
|
-
if (!decision.allowed) {
|
|
123
|
-
return { sent: false, reason: "throttled", blockedUntilMs: decision.blockedUntilMs };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const machineId = getOrCreateMachineId(path.join(trackerDir, "queue.jsonl"));
|
|
127
|
-
if (!machineId) return { sent: false, reason: "no-machine-id" };
|
|
128
|
-
|
|
129
|
-
const runtime = resolveRuntimeConfig({ config, env });
|
|
130
|
-
const payload = buildHeartbeatPayload({ machineId, version, env });
|
|
131
|
-
|
|
132
|
-
const controller = new AbortController();
|
|
133
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
134
|
-
let response;
|
|
135
|
-
try {
|
|
136
|
-
response = await fetchFunctionResponse(functionUrlFor(runtime.baseUrl, HEARTBEAT_FUNCTION_SLUG), {
|
|
137
|
-
method: "POST",
|
|
138
|
-
headers: {
|
|
139
|
-
"Content-Type": "application/json",
|
|
140
|
-
apikey: runtime.anonKey,
|
|
141
|
-
},
|
|
142
|
-
body: JSON.stringify(payload),
|
|
143
|
-
signal: controller.signal,
|
|
144
|
-
}, fetchImpl);
|
|
145
|
-
} finally {
|
|
146
|
-
clearTimeout(timer);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (!response || !response.ok) {
|
|
150
|
-
const failedState = recordUploadFailure({
|
|
151
|
-
nowMs,
|
|
152
|
-
state,
|
|
153
|
-
error: { message: `heartbeat http ${response?.status || "error"}`, status: response?.status },
|
|
154
|
-
config: THROTTLE_CONFIG,
|
|
155
|
-
});
|
|
156
|
-
await writeJson(statePath, failedState);
|
|
157
|
-
return { sent: false, reason: "http-error", status: response?.status };
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const nextState = recordUploadSuccess({ nowMs, state, config: THROTTLE_CONFIG });
|
|
161
|
-
await writeJson(statePath, nextState);
|
|
162
|
-
return { sent: true, reason: "sent" };
|
|
163
|
-
} catch (e) {
|
|
164
|
-
// Includes abort/network errors: record backoff if we can, stay silent.
|
|
165
|
-
try {
|
|
166
|
-
const statePath = path.join(trackerDir, HEARTBEAT_STATE_FILE);
|
|
167
|
-
const state = await readJson(statePath);
|
|
168
|
-
await writeJson(
|
|
169
|
-
statePath,
|
|
170
|
-
recordUploadFailure({
|
|
171
|
-
nowMs,
|
|
172
|
-
state,
|
|
173
|
-
error: { message: String(e?.message || e) },
|
|
174
|
-
config: THROTTLE_CONFIG,
|
|
175
|
-
}),
|
|
176
|
-
);
|
|
177
|
-
} catch {
|
|
178
|
-
// Even state persistence failed — nothing else to do.
|
|
179
|
-
}
|
|
180
|
-
return { sent: false, reason: "error" };
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
module.exports = {
|
|
185
|
-
HEARTBEAT_FUNCTION_SLUG,
|
|
186
|
-
HEARTBEAT_STATE_FILE,
|
|
187
|
-
HEARTBEAT_INTERVAL_MS,
|
|
188
|
-
isTelemetryDisabled,
|
|
189
|
-
hashMachineId,
|
|
190
|
-
resolveShell,
|
|
191
|
-
buildHeartbeatPayload,
|
|
192
|
-
maybeSendHeartbeat,
|
|
193
|
-
};
|