relay-companion 0.1.641 → 0.1.643
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.
|
@@ -52,7 +52,10 @@ function macPillLaunch({ homeDir, current, run }) {
|
|
|
52
52
|
try { record = mac.readRegistration(PILL_LABEL, { homeDir, run }); }
|
|
53
53
|
catch (error) {
|
|
54
54
|
// The first token only (ENOENT, invalid-service-plist, ...): never a path.
|
|
55
|
-
|
|
55
|
+
const code = String(error?.code || error?.message || "unknown").split(/[:\s]/)[0].slice(0, 40);
|
|
56
|
+
// A check that timed out learned nothing about the registration.
|
|
57
|
+
if (code === "check-timed-out") return null;
|
|
58
|
+
return { ok: false, reason: `registration-invalid: ${code}` };
|
|
56
59
|
}
|
|
57
60
|
if (record.releaseExecutable) {
|
|
58
61
|
const identity = require("./mac-background-identity.cjs");
|
|
@@ -111,8 +114,12 @@ function componentInventory(commands, activeVersion = null) {
|
|
|
111
114
|
const mixed = components.some(c => c.version && activeVersion && c.version !== activeVersion);
|
|
112
115
|
return { components: components.slice(0, 12), duplicates, mixed };
|
|
113
116
|
}
|
|
117
|
+
// Failures a sleeping machine produces on its own: the daemon cannot answer and
|
|
118
|
+
// checks time out while the computer is asleep or has only just woken.
|
|
119
|
+
const SLEEP_EXPLAINED = new Set(["daemon-not-responsive", "component-failed", "recovery-failed"]);
|
|
114
120
|
function collectInstallationHealth({ homeDir = os.homedir(), platform = process.platform, arch = process.arch,
|
|
115
|
-
osVersion = os.release(), commands, now = Date.now(), run = defaultRun, env = process.env
|
|
121
|
+
osVersion = os.release(), commands, now = Date.now(), run = defaultRun, env = process.env,
|
|
122
|
+
slept = require("./sleep-clock.cjs").sleptMs({ platform }) } = {}) {
|
|
116
123
|
const root = path.join(homeDir, ".relay"), current = read(path.join(root, "runtime", "current.json"));
|
|
117
124
|
const activeVersion = current?.active === true ? current.version : null;
|
|
118
125
|
const processCommands = commands || runtimeProcessCommands(platform);
|
|
@@ -144,13 +151,17 @@ function collectInstallationHealth({ homeDir = os.homedir(), platform = process.
|
|
|
144
151
|
else if (progressMatches && ["offline", "signed-out"].includes(progress.phase)) serviceHealth = { ...serviceHealth, state: "waiting", reason: progress.phase };
|
|
145
152
|
else if (monitorFailure || supervisor?.ok === false) serviceHealth = { ...serviceHealth, state: "degraded", reason: "recovery-failed" };
|
|
146
153
|
else if (!progressSupported) serviceHealth = { ...serviceHealth, state: "unverified", reason: "legacy-readiness" };
|
|
154
|
+
const power = require("./sleep-clock.cjs").powerState({ heartbeat, now, slept });
|
|
155
|
+
if (["resuming", "brief-wakes"].includes(power.state) && SLEEP_EXPLAINED.has(serviceHealth.reason)) {
|
|
156
|
+
serviceHealth = { ...serviceHealth, state: "waiting", reason: "resuming-from-sleep" };
|
|
157
|
+
}
|
|
147
158
|
const transport = (name) => {
|
|
148
159
|
const report = read(path.join(root, "transport-health", `${name}.json`));
|
|
149
160
|
return report?.at > 0 && now >= report.at ? new Date(report.at).toISOString() : null;
|
|
150
161
|
};
|
|
151
162
|
return {
|
|
152
163
|
installationId: installationId({ homeDir }), os: platform, osVersion: osVersion.slice(0, 80), arch,
|
|
153
|
-
...inventory, daemonResponsive, health: serviceHealth, pill,
|
|
164
|
+
...inventory, daemonResponsive, health: serviceHealth, pill, power,
|
|
154
165
|
recovery: supervisor || monitorFailure ? { status: supervisor?.status || monitor.state, version: supervisor?.launcherVersion || null,
|
|
155
166
|
checkedAt: supervisor?.checkedAt > 0 ? new Date(supervisor.checkedAt).toISOString() : null,
|
|
156
167
|
desiredVersion: supervisor?.desiredVersion || null,
|
|
@@ -174,6 +185,7 @@ function describeInstallationHealth(installation) {
|
|
|
174
185
|
"pill-missing": "Relay app is not running", "pill-stopped": "Relay app was quit",
|
|
175
186
|
"recovery-failed": "Recovery needs attention", "offline": "Waiting for Relay's connection", "signed-out": "Waiting for sign-in",
|
|
176
187
|
"runtime-not-active": "Runtime health has not been verified", "legacy-readiness": "Background service running; readiness not verified",
|
|
188
|
+
"resuming-from-sleep": "Computer is asleep or just woke; checks resume once it stays awake",
|
|
177
189
|
"ready": "Relay is running" })[reason] || "Runtime health has not been verified";
|
|
178
190
|
return message + (["restarting", "reactivating", "restoring-local", "downloading"].includes(installation?.recovery?.status) ? "; recovery in progress" : "");
|
|
179
191
|
}
|
|
@@ -22,6 +22,9 @@ function readRegistration(label, { homeDir = os.homedir(), run = defaultRun, fsI
|
|
|
22
22
|
const file = path.join(homeDir, "Library", "LaunchAgents", `${label}.plist`);
|
|
23
23
|
const bytes = contents ?? fsImpl.readFileSync(file, "utf8");
|
|
24
24
|
const decoded = run("/usr/bin/plutil", ["-convert", "json", "-o", "-", "--", "-"], { input: bytes });
|
|
25
|
+
// plutil killed by the timeout says nothing about the file: a sleeping Mac's
|
|
26
|
+
// brief wake ends mid-check (2026-11-20).
|
|
27
|
+
if (decoded?.error?.code === "ETIMEDOUT" || (decoded?.signal && decoded?.status === null)) throw Error(`check-timed-out: ${label}`);
|
|
25
28
|
if (!commandOk(decoded)) throw Error(`invalid-service-plist: ${label}`);
|
|
26
29
|
const plist = JSON.parse(decoded.stdout);
|
|
27
30
|
const args = plist.ProgramArguments;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// How long this machine has slept, told apart from a process that merely hung.
|
|
3
|
+
// Uptime (wall clock since boot) keeps counting while macOS or Linux sleeps;
|
|
4
|
+
// the monotonic clock behind process.hrtime does not. Their difference grows
|
|
5
|
+
// only while the machine sleeps, so a growth between two readings is sleep,
|
|
6
|
+
// while a hung process sees both clocks advance together. Windows' monotonic
|
|
7
|
+
// clock keeps counting through sleep, so there the difference never moves and
|
|
8
|
+
// sleep is not detected (reported as unsupported rather than guessed).
|
|
9
|
+
//
|
|
10
|
+
// Sven's Mac on 2026-11-20 woke for a minute every 16-18 minutes with its lid
|
|
11
|
+
// closed. Every health and update check started in one wake and timed out in
|
|
12
|
+
// the next, and the fleet showed "daemon not responding", an invalid pill
|
|
13
|
+
// registration and failed recovery for a machine that was simply asleep.
|
|
14
|
+
const os = require("node:os");
|
|
15
|
+
|
|
16
|
+
const SUPPORTED = new Set(["darwin", "linux"]);
|
|
17
|
+
// Ignore drift and scheduling noise; a real sleep is minutes long.
|
|
18
|
+
const SLEEP_GAP_MS = 30_000;
|
|
19
|
+
// Wakes this short and this frequent are a closed lid's maintenance wakes.
|
|
20
|
+
const BRIEF_WAKE_MS = 5 * 60_000;
|
|
21
|
+
const RECENT_WAKE_MS = 3 * 60_000;
|
|
22
|
+
const HISTORY_MS = 24 * 60 * 60_000;
|
|
23
|
+
const MAX_SLEEPS = 24;
|
|
24
|
+
|
|
25
|
+
/** Milliseconds slept since boot, up to a constant offset; null where it cannot be told. */
|
|
26
|
+
function sleptMs({ platform = process.platform, uptimeSeconds = os.uptime(), monotonicMs = Number(process.hrtime.bigint() / 1_000_000n) } = {}) {
|
|
27
|
+
if (!SUPPORTED.has(platform)) return null;
|
|
28
|
+
const value = Math.round(Number(uptimeSeconds) * 1000 - Number(monotonicMs));
|
|
29
|
+
return Number.isFinite(value) ? value : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Did the machine sleep between two sleptMs readings? */
|
|
33
|
+
function sleptBetween(before, after) {
|
|
34
|
+
return typeof before === "number" && typeof after === "number" && after - before > SLEEP_GAP_MS;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const isTime = value => Number.isFinite(value) && value > 0;
|
|
38
|
+
function validSleeps(list, now) {
|
|
39
|
+
return (Array.isArray(list) ? list : [])
|
|
40
|
+
.filter(item => isTime(item?.from) && isTime(item?.to) && item.from <= item.to && item.to <= now && now - item.to <= HISTORY_MS)
|
|
41
|
+
.map(item => ({ from: item.from, to: item.to }));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The heartbeat's sleep history after one tick: appends the gap since the last
|
|
46
|
+
* tick when the machine slept through it.
|
|
47
|
+
*/
|
|
48
|
+
function recordTick({ previous, at, slept }) {
|
|
49
|
+
const sleeps = validSleeps(previous?.sleeps, at);
|
|
50
|
+
if (isTime(previous?.at) && sleptBetween(previous?.sleptMs, slept)) sleeps.push({ from: previous.at, to: at });
|
|
51
|
+
return sleeps.slice(-MAX_SLEEPS);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* What the machine's power has been doing, from the daemon's heartbeat and a
|
|
56
|
+
* fresh clock reading. "resuming": it slept since the daemon last ticked, so
|
|
57
|
+
* the daemon has not run yet since waking. "brief-wakes": it keeps waking for a
|
|
58
|
+
* few minutes and sleeping again.
|
|
59
|
+
*/
|
|
60
|
+
function powerState({ heartbeat, now = Date.now(), slept = sleptMs() } = {}) {
|
|
61
|
+
const supported = slept !== null;
|
|
62
|
+
const sleeps = validSleeps(heartbeat?.sleeps, now);
|
|
63
|
+
const resuming = supported && isTime(heartbeat?.at) && heartbeat.at <= now && sleptBetween(heartbeat?.sleptMs, slept);
|
|
64
|
+
// The daemon has not ticked since: the sleep began after its last tick and
|
|
65
|
+
// ended when this machine's time stopped jumping, sleptSince ms later.
|
|
66
|
+
if (resuming) sleeps.push({ from: heartbeat.at, to: Math.min(now, heartbeat.at + (slept - heartbeat.sleptMs)) });
|
|
67
|
+
const lastHour = sleeps.filter(item => now - item.to <= 60 * 60_000);
|
|
68
|
+
// Every awake stretch between those sleeps, and the current one, was short.
|
|
69
|
+
const awake = lastHour.map((item, index) => (lastHour[index + 1]?.from ?? now) - item.to);
|
|
70
|
+
const briefWakes = lastHour.length >= 2 && awake.every(ms => ms < BRIEF_WAKE_MS);
|
|
71
|
+
const last = sleeps.at(-1);
|
|
72
|
+
const state = !supported ? "unknown"
|
|
73
|
+
: resuming || (last && now - last.to < RECENT_WAKE_MS) ? "resuming"
|
|
74
|
+
: briefWakes ? "brief-wakes" : "awake";
|
|
75
|
+
return {
|
|
76
|
+
sleepDetection: supported,
|
|
77
|
+
state,
|
|
78
|
+
lastSleepAt: last ? new Date(last.from).toISOString() : null,
|
|
79
|
+
lastWakeAt: last ? new Date(last.to).toISOString() : null,
|
|
80
|
+
sleepsLastHour: lastHour.length,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = { sleptMs, sleptBetween, recordTick, powerState, SLEEP_GAP_MS };
|
package/package.json
CHANGED