relay-companion 0.1.643 → 0.1.644
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.
|
@@ -5,28 +5,53 @@ const PROBATION_MS = 15 * 60_000, OBSERVATION_GAP_MS = 7 * 60_000;
|
|
|
5
5
|
const EMERGENCY_RETRY_MS = 15 * 60_000, RELEASE_COOLDOWN_MS = 30 * 60_000;
|
|
6
6
|
const read = file => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// A machine that is asleep, or only waking for a minute at a time with its
|
|
9
|
+
// lid closed, cannot download or verify anything; an update that fails then
|
|
10
|
+
// says nothing about the release. Sven's Mac, away for a weekend in
|
|
11
|
+
// 2026-11, failed 0.1.642 and 0.1.643 seven times that way and then refused
|
|
12
|
+
// 0.1.643 for 24 hours once he was back. So the update paths hold off while
|
|
13
|
+
// the machine is not properly awake (asleep()), and a failure the machine
|
|
14
|
+
// slept through is kept for the record but never counts towards the pause.
|
|
15
|
+
// Windows keeps its monotonic clock running through sleep, so there sleep
|
|
16
|
+
// cannot be told (sleep-clock.cjs) and nothing changes.
|
|
17
|
+
const machinePower = (root, now) => require("./sleep-clock.cjs").powerState({ heartbeat: read(path.join(root, "daemon.json")), now: now() });
|
|
18
|
+
|
|
19
|
+
function recoveryPolicy({ root = path.join(os.homedir(), ".relay", "recovery"), now = Date.now, power = () => machinePower(root, now) } = {}) {
|
|
20
|
+
const powerNow = () => { try { return power() || {}; } catch { return {}; } };
|
|
21
|
+
// Not properly awake: still resuming from a sleep, or in a run of brief wakes.
|
|
22
|
+
const asleep = () => ["resuming", "brief-wakes"].includes(powerNow().state);
|
|
23
|
+
// Did the machine sleep at any point since `since` (an attempt's start), or is it not properly awake now?
|
|
24
|
+
const sleptSince = since => {
|
|
25
|
+
const state = powerNow();
|
|
26
|
+
if (["resuming", "brief-wakes"].includes(state.state)) return true;
|
|
27
|
+
const wokeAt = Date.parse(state.lastWakeAt || "");
|
|
28
|
+
return Number.isFinite(since) && since > 0 && Number.isFinite(wokeAt) && wokeAt > since;
|
|
29
|
+
};
|
|
9
30
|
const directory = (channel, version) => {
|
|
10
31
|
if (!["stable", "dev", "staging"].includes(channel) || !/^\d+\.\d+\.\d+$/.test(version || "")) throw Error("invalid-release-policy-target");
|
|
11
32
|
return path.join(root, "release-health", channel, version);
|
|
12
33
|
};
|
|
13
|
-
const failure = (channel, version, { id = crypto.randomUUID(), reason = "runtime-not-healthy" } = {}) => {
|
|
34
|
+
const failure = (channel, version, { id = crypto.randomUUID(), reason = "runtime-not-healthy", since = null } = {}) => {
|
|
14
35
|
const dir = directory(channel, version), file = path.join(dir, 'failure-' + crypto.createHash("sha256").update(id).digest("hex") + '.json');
|
|
15
36
|
// Independent immutable event files avoid lost increments when the daemon,
|
|
16
37
|
// worker and watchdog report concurrently. A request ID deduplicates reports.
|
|
17
|
-
if (fs.existsSync(file)) return;
|
|
18
|
-
|
|
38
|
+
if (fs.existsSync(file)) return { slept: read(file)?.slept === true };
|
|
39
|
+
const slept = sleptSince(since);
|
|
40
|
+
atomicFile(file, JSON.stringify({ at: now(), reason: String(reason).slice(0, 300), ...(slept ? { slept: true } : {}) }));
|
|
41
|
+
// The machine slept: the release was not judged, so its probation stands.
|
|
42
|
+
if (slept) return { slept: true };
|
|
19
43
|
const probationFile = path.join(root, "probation.json");
|
|
20
44
|
if (read(probationFile)?.identity?.startsWith(`${channel}:${version}:`)) {
|
|
21
45
|
try { fs.unlinkSync(probationFile); } catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
22
46
|
}
|
|
47
|
+
return { slept: false };
|
|
23
48
|
};
|
|
24
49
|
const decision = (channel, version) => {
|
|
25
50
|
const dir = directory(channel, version), healthyAt = read(path.join(dir, "proven.json"))?.at ?? -1;
|
|
26
51
|
let names = [];
|
|
27
52
|
try { names = fs.readdirSync(dir); } catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
28
53
|
const events = names.filter(name => /^failure-[a-f0-9]{64}\.json$/.test(name)).sort().map(name => ({ ...read(path.join(dir, name)), name }))
|
|
29
|
-
.filter(event => Number.isFinite(event?.at) && event.at >= healthyAt);
|
|
54
|
+
.filter(event => Number.isFinite(event?.at) && event.at >= healthyAt && event.slept !== true);
|
|
30
55
|
const lastAt = events.reduce((last, event) => Math.max(last, event.at), 0);
|
|
31
56
|
const wait = Math.min(24 * 60 * 60_000, RELEASE_COOLDOWN_MS * 2 ** Math.min(Math.max(0, events.length - 1), 6));
|
|
32
57
|
// Anchor a clock correction once. Recomputing min(lastAt, now) on every
|
|
@@ -42,7 +67,7 @@ function recoveryPolicy({ root = path.join(os.homedir(), ".relay", "recovery"),
|
|
|
42
67
|
return { blocked: events.length > 0 && now() < retryAt, retryAt, failures: events.length };
|
|
43
68
|
};
|
|
44
69
|
return {
|
|
45
|
-
failure, decision,
|
|
70
|
+
failure, decision, asleep, sleptSince,
|
|
46
71
|
observe(channel, observed) {
|
|
47
72
|
const current = observed.current, at = now(), file = path.join(root, "probation.json");
|
|
48
73
|
// Only strong readiness probes supply an identity. Legacy process/heartbeat
|
|
@@ -223,6 +223,7 @@ async function recoverLocked({ homeDir = os.homedir(), env = process.env, now =
|
|
|
223
223
|
if (!config) return { ok: false, status: "configuration-unavailable" };
|
|
224
224
|
const channel = channelFrom(config, env);
|
|
225
225
|
const policy = policyFactory({ root: path.join(root, "recovery"), now });
|
|
226
|
+
const runStartedAt = now();
|
|
226
227
|
const previous = read(stateFile);
|
|
227
228
|
const runId = env.RELAY_RECOVERY_RUN_ID || crypto.randomUUID();
|
|
228
229
|
const diagnosticScope = diagnostics.configIdentity(homeDir)?.scope || null;
|
|
@@ -496,6 +497,9 @@ async function recoverLocked({ homeDir = os.homedir(), env = process.env, now =
|
|
|
496
497
|
if (quarantine.blocked) return status({ ok: true, status: "deferred-release-cooldown", desiredVersion, runtimeHealthy: runtimeVerified, runtimeAvailable: runtimeResponsive, retryAt: quarantine.retryAt, failures: quarantine.failures });
|
|
497
498
|
const busy = busyDecision(heartbeat, { homeDir, now: now() });
|
|
498
499
|
if (busy) return status({ ok: true, status: busy, desiredVersion, ...repairState });
|
|
500
|
+
// A sleeping or briefly waking machine cannot finish a download; wait
|
|
501
|
+
// until it is properly awake (recovery-policy.cjs).
|
|
502
|
+
if (policy.asleep?.()) return status({ ok: true, status: "deferred-asleep", desiredVersion, ...repairState });
|
|
499
503
|
// Never start a five-minute download to race an installer that is already
|
|
500
504
|
// running; the daemon's updater discovers the same release we just did.
|
|
501
505
|
const deferredBeforeDownload = deferToTransaction({ desiredVersion, ...repairState });
|
|
@@ -541,12 +545,17 @@ async function recoverLocked({ homeDir = os.homedir(), env = process.env, now =
|
|
|
541
545
|
const deferred = deferToTransaction({ desiredVersion, ...repairState });
|
|
542
546
|
if (deferred) return deferred;
|
|
543
547
|
if (workerLostLock(error)) return status({ ok: true, status: "deferred-update-in-flight", desiredVersion, ...repairState, runtimeHealthy: false });
|
|
544
|
-
policy.failure(channel, desiredVersion, { id: attemptId, reason: error.message });
|
|
545
|
-
policy.interrupt();
|
|
548
|
+
if (policy.failure(channel, desiredVersion, { id: attemptId, reason: error.message, since: activationAt })?.slept !== true) policy.interrupt();
|
|
546
549
|
throw error;
|
|
547
550
|
}
|
|
548
551
|
} catch (error) {
|
|
549
552
|
const activeStatus = read(stateFile);
|
|
553
|
+
// The machine slept during this run: the failure says nothing, so it
|
|
554
|
+
// neither counts nor delays the next run.
|
|
555
|
+
let slept = false;
|
|
556
|
+
try { slept = policy.sleptSince?.(runStartedAt) === true; } catch {}
|
|
557
|
+
if (slept) return status({ ok: true, status: "deferred-asleep", desiredVersion: activeStatus?.desiredVersion || null, version: current?.version,
|
|
558
|
+
lastError: String(error.message).slice(0, 300) });
|
|
550
559
|
const failures = previous && previous.desiredVersion === activeStatus?.desiredVersion ? (previous.failures || 0) + 1 : 1;
|
|
551
560
|
return status({ ok: false, status: "failed", desiredVersion: activeStatus?.desiredVersion || null, version: current?.version,
|
|
552
561
|
restarts: activeStatus?.restarts, staleSince: activeStatus?.staleSince,
|
package/package.json
CHANGED