relay-companion 0.1.646 → 0.1.648
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.
|
@@ -19,10 +19,10 @@ function createDaemonProgress({ packageRoot, version, homeDir = os.homedir(), pi
|
|
|
19
19
|
// The last failure of each part, as a closed code (see diagnostics.cjs
|
|
20
20
|
// classifyFailure) and the job it happened in. Kept after the part
|
|
21
21
|
// recovers, so the fleet can see what last went wrong and when.
|
|
22
|
-
componentError(name, { stage, code, at }) {
|
|
22
|
+
componentError(name, { stage, code, at, waitingOn = null }) {
|
|
23
23
|
const previous = state.componentErrors?.[name];
|
|
24
24
|
if (previous && previous.stage === stage && previous.code === code && at - previous.at < 60_000) return;
|
|
25
|
-
state = { ...state, componentErrors: { ...(state.componentErrors || {}), [name]: { stage, code, at } } };
|
|
25
|
+
state = { ...state, componentErrors: { ...(state.componentErrors || {}), [name]: { stage, code, at, ...(waitingOn ? { waitingOn } : {}) } } };
|
|
26
26
|
save();
|
|
27
27
|
},
|
|
28
28
|
snapshot() { return { ...state, components: { ...state.components }, componentErrors: { ...(state.componentErrors || {}) } }; },
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
"topics",
|
|
27
27
|
"tasks",
|
|
28
28
|
"process-query",
|
|
29
|
-
"report"
|
|
29
|
+
"report",
|
|
30
|
+
"connection",
|
|
31
|
+
"event-loop"
|
|
30
32
|
],
|
|
31
33
|
"outcomes": [
|
|
32
34
|
"started",
|
|
@@ -102,7 +104,11 @@
|
|
|
102
104
|
"server-refused",
|
|
103
105
|
"component-stalled",
|
|
104
106
|
"worker-out-of-memory",
|
|
105
|
-
"collection-cancelled"
|
|
107
|
+
"collection-cancelled",
|
|
108
|
+
"connection-refused",
|
|
109
|
+
"connection-reset",
|
|
110
|
+
"tls-failed",
|
|
111
|
+
"event-loop-blocked"
|
|
106
112
|
],
|
|
107
113
|
"runtimeStates": [
|
|
108
114
|
"active",
|
|
@@ -157,7 +157,8 @@ function collectInstallationHealth({ homeDir = os.homedir(), platform = process.
|
|
|
157
157
|
.filter(([name, item]) => LABEL.test(name) && LABEL.test(item?.stage || "") && LABEL.test(item?.code || "")
|
|
158
158
|
&& Number.isFinite(item.at) && item.at <= now && now - item.at < 24 * 60 * 60_000)
|
|
159
159
|
.sort((a, b) => b[1].at - a[1].at).slice(0, 4)
|
|
160
|
-
.map(([name, item]) => ({ component: name, stage: item.stage, code: item.code, at: new Date(item.at).toISOString()
|
|
160
|
+
.map(([name, item]) => ({ component: name, stage: item.stage, code: item.code, at: new Date(item.at).toISOString(),
|
|
161
|
+
...(typeof item.waitingOn === "string" && /^[A-Z]{1,7} \/[a-z0-9:/-]{0,60}$/.test(item.waitingOn) ? { waitingOn: item.waitingOn } : {}) })) : [];
|
|
161
162
|
let serviceHealth = { state: "healthy", reason: "ready", components: unhealthyComponents, ...(errors.length ? { errors } : {}) };
|
|
162
163
|
if (!activeVersion) serviceHealth = { ...serviceHealth, state: "unverified", reason: "runtime-not-active" };
|
|
163
164
|
else if (!live?.daemon) serviceHealth = { ...serviceHealth, state: "failed", reason: "daemon-missing" };
|
|
@@ -171,7 +172,7 @@ function collectInstallationHealth({ homeDir = os.homedir(), platform = process.
|
|
|
171
172
|
else if (progressMatches && ["offline", "signed-out"].includes(progress.phase)) serviceHealth = { ...serviceHealth, state: "waiting", reason: progress.phase };
|
|
172
173
|
else if (monitorFailure || supervisor?.ok === false) serviceHealth = { ...serviceHealth, state: "degraded", reason: "recovery-failed" };
|
|
173
174
|
else if (!progressSupported) serviceHealth = { ...serviceHealth, state: "unverified", reason: "legacy-readiness" };
|
|
174
|
-
const power = require("./sleep-clock.cjs").powerState({ heartbeat, now, slept });
|
|
175
|
+
const power = require("./sleep-clock.cjs").powerState({ heartbeat, now, slept, source: require("./sleep-clock.cjs").sleepSource(platform) });
|
|
175
176
|
if (["resuming", "brief-wakes"].includes(power.state) && SLEEP_EXPLAINED.has(serviceHealth.reason)) {
|
|
176
177
|
serviceHealth = { ...serviceHealth, state: "waiting", reason: "resuming-from-sleep" };
|
|
177
178
|
}
|
|
@@ -179,9 +180,11 @@ function collectInstallationHealth({ homeDir = os.homedir(), platform = process.
|
|
|
179
180
|
const report = read(path.join(root, "transport-health", `${name}.json`));
|
|
180
181
|
return report?.at > 0 && now >= report.at ? new Date(report.at).toISOString() : null;
|
|
181
182
|
};
|
|
183
|
+
const { connection, loop } = heartbeatDiagnosis(heartbeat);
|
|
182
184
|
return {
|
|
183
185
|
installationId: installationId({ homeDir }), os: platform, osVersion: osVersion.slice(0, 80), arch,
|
|
184
186
|
...inventory, daemonResponsive, health: serviceHealth, pill, power,
|
|
187
|
+
...(connection ? { connection } : {}), ...(loop ? { loop } : {}),
|
|
185
188
|
recovery: supervisor || monitorFailure ? { status: supervisor?.status || monitor.state, version: supervisor?.launcherVersion || null,
|
|
186
189
|
checkedAt: supervisor?.checkedAt > 0 ? new Date(supervisor.checkedAt).toISOString() : null,
|
|
187
190
|
desiredVersion: supervisor?.desiredVersion || null,
|
|
@@ -193,6 +196,35 @@ function collectInstallationHealth({ homeDir = os.homedir(), platform = process.
|
|
|
193
196
|
transports: { mcpLastUsedAt: transport("mcp"), httpsLastUsedAt: transport("https") },
|
|
194
197
|
};
|
|
195
198
|
}
|
|
199
|
+
// The daemon's connection summary and event-loop lag, as its last heartbeat
|
|
200
|
+
// recorded them (src/recovery-health.js, src/connection-health.js). asOf is
|
|
201
|
+
// the heartbeat's own time: a stale one is the daemon not ticking, and is
|
|
202
|
+
// shown as such rather than as current numbers. Only bounded values pass.
|
|
203
|
+
function heartbeatDiagnosis(heartbeat) {
|
|
204
|
+
const iso = value => (typeof value === "string" && Number.isFinite(Date.parse(value)) ? new Date(value).toISOString() : null);
|
|
205
|
+
const count = value => (Number.isSafeInteger(value) && value >= 0 ? Math.min(value, 1_000_000) : null);
|
|
206
|
+
const label = value => (typeof value === "string" && /^[A-Z]{1,7} \/[a-z0-9:/-]{0,60}$/.test(value) ? value : null);
|
|
207
|
+
const code = value => (typeof value === "string" && /^[a-z0-9-]{1,40}$/.test(value) ? value : null);
|
|
208
|
+
const asOf = Number.isFinite(heartbeat?.at) && heartbeat.at > 0 ? new Date(heartbeat.at).toISOString() : null;
|
|
209
|
+
const raw = heartbeat?.connection;
|
|
210
|
+
const connection = raw && typeof raw === "object" ? {
|
|
211
|
+
asOf,
|
|
212
|
+
lastSuccessAt: iso(raw.lastSuccessAt), failingSince: iso(raw.failingSince), lastFailureAt: iso(raw.lastFailureAt),
|
|
213
|
+
lastFailureCode: code(raw.lastFailureCode),
|
|
214
|
+
window: raw.window && typeof raw.window === "object" ? {
|
|
215
|
+
minutes: count(raw.window.minutes) ?? 15, ok: count(raw.window.ok) ?? 0, serverError: count(raw.window.serverError) ?? 0,
|
|
216
|
+
failed: count(raw.window.failed) ?? 0, slow: count(raw.window.slow) ?? 0, p50Ms: count(raw.window.p50Ms), p95Ms: count(raw.window.p95Ms),
|
|
217
|
+
} : null,
|
|
218
|
+
waiting: (Array.isArray(raw.waiting) ? raw.waiting : []).slice(0, 3).flatMap(item => {
|
|
219
|
+
const name = label(item?.label);
|
|
220
|
+
return name ? [{ label: name, component: code(item?.component), waitingMs: count(item?.waitingMs) ?? 0 }] : [];
|
|
221
|
+
}),
|
|
222
|
+
} : null;
|
|
223
|
+
const loopRaw = heartbeat?.loop;
|
|
224
|
+
const loop = loopRaw && typeof loopRaw === "object" ? { asOf, maxGapMs: count(loopRaw.maxGapMs), gapsOver10s: count(loopRaw.gapsOver10s) ?? 0, lastGapAt: iso(loopRaw.lastGapAt) } : null;
|
|
225
|
+
return { connection, loop };
|
|
226
|
+
}
|
|
227
|
+
|
|
196
228
|
function recordTransport(name, { homeDir = os.homedir(), now = Date.now() } = {}) {
|
|
197
229
|
if (!["mcp", "https"].includes(name)) return;
|
|
198
230
|
const root = path.join(homeDir, ".relay", "transport-health");
|
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// How long this machine has slept, told apart from a process that merely hung.
|
|
3
|
-
//
|
|
4
|
-
// the
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
3
|
+
// A "sleep reading" is a number that stays put while the machine is awake and
|
|
4
|
+
// grows by at least the length of every sleep, so a growth between two
|
|
5
|
+
// readings is sleep, while a hung process sees it unchanged.
|
|
6
|
+
//
|
|
7
|
+
// Linux ("uptime"): uptime (wall clock since boot) keeps counting through a
|
|
8
|
+
// suspend; the monotonic clock behind process.hrtime does not. Their
|
|
9
|
+
// difference is the reading.
|
|
10
|
+
//
|
|
11
|
+
// macOS ("waketime"): Node's monotonic clock there keeps counting through
|
|
12
|
+
// sleep, so the uptime difference never moves. Sven's Mac slept from 18:56 to
|
|
13
|
+
// 09:41 on 2026-11-24/25 and its heartbeat recorded no sleep at all. The
|
|
14
|
+
// reading is instead the kernel's last wake time (sysctl kern.waketime), which
|
|
15
|
+
// Power Nap's dark wakes also move. Spawning sysctl every 5 s would be waste:
|
|
16
|
+
// a sleep always leaves a gap between two calls in one process (timers do not
|
|
17
|
+
// fire while asleep), so the kernel is asked again only after such a gap.
|
|
18
|
+
//
|
|
19
|
+
// Windows' monotonic clock also counts through sleep, so sleep is not
|
|
20
|
+
// detected there (reported as unsupported rather than guessed).
|
|
9
21
|
//
|
|
10
22
|
// Sven's Mac on 2026-11-20 woke for a minute every 16-18 minutes with its lid
|
|
11
23
|
// closed. Every health and update check started in one wake and timed out in
|
|
@@ -13,7 +25,13 @@
|
|
|
13
25
|
// registration and failed recovery for a machine that was simply asleep.
|
|
14
26
|
const os = require("node:os");
|
|
15
27
|
|
|
16
|
-
const
|
|
28
|
+
const { execFileSync } = require("node:child_process");
|
|
29
|
+
|
|
30
|
+
const SOURCES = { darwin: "waketime", linux: "uptime" };
|
|
31
|
+
// Calls closer together than this cannot have a sleep that matters between them.
|
|
32
|
+
const REREAD_GAP_MS = 20_000;
|
|
33
|
+
// A failed kernel read is retried after this long, not on every tick.
|
|
34
|
+
const FAILED_READ_RETRY_MS = 60_000;
|
|
17
35
|
// Ignore drift and scheduling noise; a real sleep is minutes long.
|
|
18
36
|
const SLEEP_GAP_MS = 30_000;
|
|
19
37
|
// Wakes this short and this frequent are a closed lid's maintenance wakes.
|
|
@@ -22,10 +40,42 @@ const RECENT_WAKE_MS = 3 * 60_000;
|
|
|
22
40
|
const HISTORY_MS = 24 * 60 * 60_000;
|
|
23
41
|
const MAX_SLEEPS = 24;
|
|
24
42
|
|
|
25
|
-
/**
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
/** Which sleep reading this platform has, or null. */
|
|
44
|
+
function sleepSource(platform = process.platform) {
|
|
45
|
+
return SOURCES[platform] || null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const readKernelWake = () => execFileSync("/usr/sbin/sysctl", ["-n", "kern.waketime"],
|
|
49
|
+
{ encoding: "utf8", timeout: 2000, windowsHide: true, stdio: ["ignore", "pipe", "ignore"] });
|
|
50
|
+
let wakeCache = null; // { calledAt, readAt, value }
|
|
51
|
+
|
|
52
|
+
/** "{ sec = 1764060070, usec = 742196 } Tue Nov 25 …" as epoch milliseconds (0: not woken since boot). */
|
|
53
|
+
function parseKernelTime(text) {
|
|
54
|
+
const match = String(text || "").match(/\bsec\s*=\s*(\d+)\s*,\s*usec\s*=\s*(\d+)/);
|
|
55
|
+
return match ? Number(match[1]) * 1000 + Math.floor(Number(match[2]) / 1000) : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The kernel's last wake time on macOS, asked again only when a sleep could have happened since the last call. */
|
|
59
|
+
function lastWakeMs({ now = Date.now(), read = readKernelWake } = {}) {
|
|
60
|
+
const cached = wakeCache;
|
|
61
|
+
if (cached && now >= cached.calledAt && (cached.value === null
|
|
62
|
+
? now - cached.readAt < FAILED_READ_RETRY_MS
|
|
63
|
+
: now - cached.calledAt < REREAD_GAP_MS)) {
|
|
64
|
+
cached.calledAt = now;
|
|
65
|
+
return cached.value;
|
|
66
|
+
}
|
|
67
|
+
let value = null;
|
|
68
|
+
try { value = parseKernelTime(read()); } catch {}
|
|
69
|
+
wakeCache = { calledAt: now, readAt: now, value };
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** This platform's sleep reading (see above); null where sleep cannot be told. */
|
|
74
|
+
function sleptMs({ platform = process.platform, uptimeSeconds, monotonicMs, now, readWake } = {}) {
|
|
75
|
+
const source = sleepSource(platform);
|
|
76
|
+
if (source === "waketime") return lastWakeMs({ now, read: readWake });
|
|
77
|
+
if (source !== "uptime") return null;
|
|
78
|
+
const value = Math.round(Number(uptimeSeconds ?? os.uptime()) * 1000 - Number(monotonicMs ?? Number(process.hrtime.bigint() / 1_000_000n)));
|
|
29
79
|
return Number.isFinite(value) ? value : null;
|
|
30
80
|
}
|
|
31
81
|
|
|
@@ -57,13 +107,20 @@ function recordTick({ previous, at, slept }) {
|
|
|
57
107
|
* the daemon has not run yet since waking. "brief-wakes": it keeps waking for a
|
|
58
108
|
* few minutes and sleeping again.
|
|
59
109
|
*/
|
|
60
|
-
function powerState({ heartbeat, now = Date.now(), slept = sleptMs() } = {}) {
|
|
110
|
+
function powerState({ heartbeat, now = Date.now(), slept = sleptMs(), source = sleepSource() } = {}) {
|
|
61
111
|
const supported = slept !== null;
|
|
62
112
|
const sleeps = validSleeps(heartbeat?.sleeps, now);
|
|
63
|
-
|
|
113
|
+
// Readings of two different kinds (a heartbeat written before an update
|
|
114
|
+
// changed the kind) cannot be compared.
|
|
115
|
+
const comparable = !heartbeat?.sleepSource || !source || heartbeat.sleepSource === source;
|
|
116
|
+
const resuming = supported && comparable && isTime(heartbeat?.at) && heartbeat.at <= now && sleptBetween(heartbeat?.sleptMs, slept);
|
|
64
117
|
// The daemon has not ticked since: the sleep began after its last tick and
|
|
65
|
-
// ended
|
|
66
|
-
|
|
118
|
+
// ended at the kernel's wake time, or, from the uptime clocks, when this
|
|
119
|
+
// machine's time stopped jumping, sleptSince ms later.
|
|
120
|
+
if (resuming) {
|
|
121
|
+
const woke = source === "waketime" ? Math.max(heartbeat.at, slept) : heartbeat.at + (slept - heartbeat.sleptMs);
|
|
122
|
+
sleeps.push({ from: heartbeat.at, to: Math.min(now, woke) });
|
|
123
|
+
}
|
|
67
124
|
const lastHour = sleeps.filter(item => now - item.to <= 60 * 60_000);
|
|
68
125
|
// Every awake stretch between those sleeps, and the current one, was short.
|
|
69
126
|
const awake = lastHour.map((item, index) => (lastHour[index + 1]?.from ?? now) - item.to);
|
|
@@ -81,4 +138,7 @@ function powerState({ heartbeat, now = Date.now(), slept = sleptMs() } = {}) {
|
|
|
81
138
|
};
|
|
82
139
|
}
|
|
83
140
|
|
|
84
|
-
|
|
141
|
+
/** Test seam: forget the cached kernel wake time. */
|
|
142
|
+
function resetSleepClock() { wakeCache = null; }
|
|
143
|
+
|
|
144
|
+
module.exports = { sleptMs, sleepSource, sleptBetween, recordTick, powerState, parseKernelTime, resetSleepClock, SLEEP_GAP_MS };
|
package/package.json
CHANGED