looply-cli 0.5.39 → 0.5.43
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/dist/account-standing.d.ts +23 -0
- package/dist/account-standing.d.ts.map +1 -0
- package/dist/account-standing.js +41 -0
- package/dist/account-standing.js.map +1 -0
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +1 -0
- package/dist/bin.js.map +1 -1
- package/dist/branch-delivery-wiring.d.ts +19 -0
- package/dist/branch-delivery-wiring.d.ts.map +1 -1
- package/dist/branch-delivery-wiring.js +49 -26
- package/dist/branch-delivery-wiring.js.map +1 -1
- package/dist/checkout-deps.d.ts +6 -0
- package/dist/checkout-deps.d.ts.map +1 -1
- package/dist/checkout-deps.js +11 -0
- package/dist/checkout-deps.js.map +1 -1
- package/dist/checkout-leftovers.d.ts.map +1 -1
- package/dist/checkout-leftovers.js +28 -21
- package/dist/checkout-leftovers.js.map +1 -1
- package/dist/daemon-cmd.d.ts.map +1 -1
- package/dist/daemon-cmd.js +51 -1
- package/dist/daemon-cmd.js.map +1 -1
- package/dist/feature-report.d.ts.map +1 -1
- package/dist/feature-report.js +27 -4
- package/dist/feature-report.js.map +1 -1
- package/dist/gate-health.d.ts +7 -0
- package/dist/gate-health.d.ts.map +1 -1
- package/dist/gate-health.js +11 -1
- package/dist/gate-health.js.map +1 -1
- package/dist/goal-compile.d.ts.map +1 -1
- package/dist/goal-compile.js +3 -0
- package/dist/goal-compile.js.map +1 -1
- package/dist/goal-gap.d.ts +22 -1
- package/dist/goal-gap.d.ts.map +1 -1
- package/dist/goal-gap.js +65 -6
- package/dist/goal-gap.js.map +1 -1
- package/dist/grant-waiting-wiring.d.ts +16 -1
- package/dist/grant-waiting-wiring.d.ts.map +1 -1
- package/dist/grant-waiting-wiring.js +27 -1
- package/dist/grant-waiting-wiring.js.map +1 -1
- package/dist/heroku-cli.d.ts +13 -1
- package/dist/heroku-cli.d.ts.map +1 -1
- package/dist/heroku-cli.js +15 -0
- package/dist/heroku-cli.js.map +1 -1
- package/dist/machine-settings.d.ts +7 -0
- package/dist/machine-settings.d.ts.map +1 -1
- package/dist/machine-settings.js +27 -0
- package/dist/machine-settings.js.map +1 -1
- package/dist/provider-accounts.d.ts +5 -1
- package/dist/provider-accounts.d.ts.map +1 -1
- package/dist/provider-accounts.js +1 -0
- package/dist/provider-accounts.js.map +1 -1
- package/dist/qa-feedback.d.ts +110 -0
- package/dist/qa-feedback.d.ts.map +1 -0
- package/dist/qa-feedback.js +353 -0
- package/dist/qa-feedback.js.map +1 -0
- package/dist/run-cmd.d.ts.map +1 -1
- package/dist/run-cmd.js +43 -6
- package/dist/run-cmd.js.map +1 -1
- package/dist/self-update.d.ts +440 -0
- package/dist/self-update.d.ts.map +1 -0
- package/dist/self-update.js +1006 -0
- package/dist/self-update.js.map +1 -0
- package/dist/serve-cmd.d.ts +32 -1
- package/dist/serve-cmd.d.ts.map +1 -1
- package/dist/serve-cmd.js +162 -7
- package/dist/serve-cmd.js.map +1 -1
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +3 -0
- package/dist/serve.js.map +1 -1
- package/dist/server-client.d.ts +6 -0
- package/dist/server-client.d.ts.map +1 -1
- package/dist/server-client.js +4 -0
- package/dist/server-client.js.map +1 -1
- package/dist/setup-check-wiring.d.ts +31 -0
- package/dist/setup-check-wiring.d.ts.map +1 -0
- package/dist/setup-check-wiring.js +195 -0
- package/dist/setup-check-wiring.js.map +1 -0
- package/dist/setup-check.d.ts +222 -0
- package/dist/setup-check.d.ts.map +1 -0
- package/dist/setup-check.js +620 -0
- package/dist/setup-check.js.map +1 -0
- package/dist/signals.d.ts +13 -0
- package/dist/signals.d.ts.map +1 -1
- package/dist/signals.js +26 -1
- package/dist/signals.js.map +1 -1
- package/dist/ticket-attachments.d.ts +31 -1
- package/dist/ticket-attachments.d.ts.map +1 -1
- package/dist/ticket-attachments.js +232 -9
- package/dist/ticket-attachments.js.map +1 -1
- package/dist/watchdog-wiring.d.ts +40 -0
- package/dist/watchdog-wiring.d.ts.map +1 -0
- package/dist/watchdog-wiring.js +108 -0
- package/dist/watchdog-wiring.js.map +1 -0
- package/dist/watchdog.d.ts +107 -0
- package/dist/watchdog.d.ts.map +1 -0
- package/dist/watchdog.js +424 -0
- package/dist/watchdog.js.map +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,1006 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REINSTALL_HINT = exports.LAUNCHD_LABEL = exports.SYSTEMD_UNIT = exports.SelfUpdater = exports.ROLLBACK_RETRY_MAX_MS = exports.ROLLBACK_RETRY_BASE_MS = exports.SLIP_WAIT_MAX_MS = exports.CRASH_WINDOW_MS = exports.CRASH_STARTS = exports.HEALTHY_AFTER_MS = exports.AUTO_UPDATE_DEFAULT_ON = exports.EXIT_RESTART = exports.STATE_FILE = exports.RETRY_FAILED_AFTER_MS = exports.DAEMON_STOP_GRACE_MS = exports.STALE_RUN_MS = exports.DRAIN_WAIT_MAX_MS = exports.DRAIN_AFTER_MS = exports.CHECK_EVERY_MS = exports.SELF_UPDATE_TICK_MS = exports.PACKAGE = void 0;
|
|
4
|
+
exports.autoUpdateOff = autoUpdateOff;
|
|
5
|
+
exports.globalInstallOf = globalInstallOf;
|
|
6
|
+
exports.isNewer = isNewer;
|
|
7
|
+
exports.decideUpdate = decideUpdate;
|
|
8
|
+
exports.isLooplyRunCommand = isLooplyRunCommand;
|
|
9
|
+
exports.looplyCommandOf = looplyCommandOf;
|
|
10
|
+
exports.parseElapsedSeconds = parseElapsedSeconds;
|
|
11
|
+
exports.countLooplyRuns = countLooplyRuns;
|
|
12
|
+
exports.pendingRunSlots = pendingRunSlots;
|
|
13
|
+
exports.recordStart = recordStart;
|
|
14
|
+
exports.startupGuard = startupGuard;
|
|
15
|
+
exports.afterRollback = afterRollback;
|
|
16
|
+
exports.relaunchFailed = relaunchFailed;
|
|
17
|
+
exports.confirmServeStarted = confirmServeStarted;
|
|
18
|
+
exports.managedServe = managedServe;
|
|
19
|
+
exports.systemdUnitOf = systemdUnitOf;
|
|
20
|
+
exports.serviceManagerOf = serviceManagerOf;
|
|
21
|
+
exports.nodeServiceFacts = nodeServiceFacts;
|
|
22
|
+
exports.updateRegistry = updateRegistry;
|
|
23
|
+
exports.nodeSelfUpdateTask = nodeSelfUpdateTask;
|
|
24
|
+
exports.binaryVersion = binaryVersion;
|
|
25
|
+
exports.npmInstall = npmInstall;
|
|
26
|
+
exports.nodeRecordStart = nodeRecordStart;
|
|
27
|
+
exports.nodeStartupCheck = nodeStartupCheck;
|
|
28
|
+
exports.relaunchServe = relaunchServe;
|
|
29
|
+
const node_child_process_1 = require("node:child_process");
|
|
30
|
+
const node_fs_1 = require("node:fs");
|
|
31
|
+
const node_path_1 = require("node:path");
|
|
32
|
+
const core_1 = require("@looply/core");
|
|
33
|
+
const machine_settings_1 = require("./machine-settings");
|
|
34
|
+
const machine_run_slots_1 = require("./machine-run-slots");
|
|
35
|
+
const machine_store_1 = require("./machine-store");
|
|
36
|
+
const server_client_1 = require("./server-client");
|
|
37
|
+
const untrusted_exec_1 = require("./untrusted-exec");
|
|
38
|
+
/**
|
|
39
|
+
* EVERY PC MOVES ITSELF TO THE NEW looply-cli — `looply serve`'s self-update.
|
|
40
|
+
*
|
|
41
|
+
* Before this, a release reached a PC only when somebody ran a script on it
|
|
42
|
+
* that waited for a moment with no `looply run`, stopped serve and its daemons,
|
|
43
|
+
* ran `npm i -g looply-cli@X` and started serve again. Other people's PCs had
|
|
44
|
+
* no such script at all. This is that script, inside the product:
|
|
45
|
+
*
|
|
46
|
+
* 1. every 30 minutes the supervisor asks npm for looply-cli's `latest`, and
|
|
47
|
+
* the paired server whether that version is one it accepts
|
|
48
|
+
* (`GET /v1/cli-compat`: not newer than the server, not below its floor,
|
|
49
|
+
* not paused by the operator);
|
|
50
|
+
* 2. with an update decided, it looks every 15 seconds for a GAP — no run in
|
|
51
|
+
* flight anywhere on this machine (`runsInFlight`);
|
|
52
|
+
* 3. in the gap it asks its daemons to stop at their next idle point (so no
|
|
53
|
+
* new run can start), looks again, waits out any run that slipped in,
|
|
54
|
+
* ASKS THE WHOLE DECISION AGAIN, installs into the SAME global prefix it
|
|
55
|
+
* runs from, checks the installed binary's `--version`, and restarts serve
|
|
56
|
+
* — staying until the new serve has come up, and putting the previous
|
|
57
|
+
* version back if it does not.
|
|
58
|
+
*
|
|
59
|
+
* 🔴 A RUN IN FLIGHT IS NEVER INTERRUPTED. Nothing here stops a `looply run`.
|
|
60
|
+
* Daemons are stopped only in a gap, or — after six hours without one, and at
|
|
61
|
+
* most ONCE PER VERSION — to DRAIN: no new run starts, the ones in flight finish
|
|
62
|
+
* on their own (they are detached children with their own logs and survive
|
|
63
|
+
* their daemon), and only then is anything installed. A drain that has not
|
|
64
|
+
* emptied in three hours gives up and serve carries on as it was; that version
|
|
65
|
+
* then waits for a natural gap. A run going for over twelve hours is reported
|
|
66
|
+
* and not waited for: one hung run must not hold the PC hostage.
|
|
67
|
+
*
|
|
68
|
+
* ⚠️ OFF BY DEFAULT IN ITS FIRST RELEASE (`AUTO_UPDATE_DEFAULT_ON`). Per machine:
|
|
69
|
+
* `looply serve auto-update on|off` (`"autoUpdate"` in
|
|
70
|
+
* `~/.looply/machine-settings.json`), or `LOOPLY_AUTO_UPDATE=on|off` in serve's
|
|
71
|
+
* environment. The env var wins both ways.
|
|
72
|
+
*
|
|
73
|
+
* ⚠️ ONLY AN npm GLOBAL INSTALL IS UPDATED. A checkout's `dist/bin.js`, an npx
|
|
74
|
+
* cache or a project-local install is left alone, and the log says why.
|
|
75
|
+
*
|
|
76
|
+
* Every decision is one log line starting `[self-update]`, said when it
|
|
77
|
+
* changes rather than every half hour.
|
|
78
|
+
*/
|
|
79
|
+
exports.PACKAGE = 'looply-cli';
|
|
80
|
+
exports.SELF_UPDATE_TICK_MS = 15_000;
|
|
81
|
+
exports.CHECK_EVERY_MS = 30 * 60_000;
|
|
82
|
+
/** With an update waiting this long and still no gap, daemons are stopped so the runs drain. */
|
|
83
|
+
exports.DRAIN_AFTER_MS = 6 * 60 * 60_000;
|
|
84
|
+
/** Once draining, how long the runs in flight get before serve gives up and restarts as it was. */
|
|
85
|
+
exports.DRAIN_WAIT_MAX_MS = 3 * 60 * 60_000;
|
|
86
|
+
/** A run alive longer than this is reported as stale and does not hold an update back. */
|
|
87
|
+
exports.STALE_RUN_MS = 12 * 60 * 60_000;
|
|
88
|
+
/** How long a daemon gets to reach an idle point and stop by itself before it is killed. */
|
|
89
|
+
exports.DAEMON_STOP_GRACE_MS = 10 * 60_000;
|
|
90
|
+
/** A version whose install failed is not tried again for this long. */
|
|
91
|
+
exports.RETRY_FAILED_AFTER_MS = 6 * 60 * 60_000;
|
|
92
|
+
exports.STATE_FILE = 'self-update.json';
|
|
93
|
+
/** `looply serve` under systemd/launchd exits with this after updating, so the manager starts the new one. */
|
|
94
|
+
exports.EXIT_RESTART = 75;
|
|
95
|
+
const say = (line) => `[self-update] ${line}`;
|
|
96
|
+
/* ─────────────────────────────────────────────────────────────── opt-out ── */
|
|
97
|
+
/**
|
|
98
|
+
* 🔴 OFF BY DEFAULT IN ITS FIRST RELEASE. A PC updates itself only when its
|
|
99
|
+
* owner said so: `looply serve auto-update on` (`"autoUpdate": true` in
|
|
100
|
+
* `~/.looply/machine-settings.json`) or `LOOPLY_AUTO_UPDATE=on`. Once it has
|
|
101
|
+
* been proven end to end on one PC, the default turns on (docs/RELEASE.md).
|
|
102
|
+
*/
|
|
103
|
+
exports.AUTO_UPDATE_DEFAULT_ON = false;
|
|
104
|
+
/** Why automatic updates are off on this machine, or null when they are on. */
|
|
105
|
+
function autoUpdateOff(env, local) {
|
|
106
|
+
const flag = (env['LOOPLY_AUTO_UPDATE'] ?? '').trim();
|
|
107
|
+
if (/^(0|off|false|no)$/i.test(flag))
|
|
108
|
+
return `LOOPLY_AUTO_UPDATE=${flag}`;
|
|
109
|
+
if (/^(1|on|true|yes)$/i.test(flag))
|
|
110
|
+
return null;
|
|
111
|
+
let settings;
|
|
112
|
+
try {
|
|
113
|
+
settings = local();
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
// A file a person wrote and broke: their say is unknown, so nothing moves.
|
|
117
|
+
return `~/.looply/machine-settings.json cannot be read (${e.message})`;
|
|
118
|
+
}
|
|
119
|
+
if (settings.autoUpdate === false)
|
|
120
|
+
return '"autoUpdate": false in ~/.looply/machine-settings.json';
|
|
121
|
+
if (settings.autoUpdate === true)
|
|
122
|
+
return null;
|
|
123
|
+
return exports.AUTO_UPDATE_DEFAULT_ON ? null : 'off by default in this release; `looply serve auto-update on` turns it on';
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The npm global install `bin` belongs to, or null when it is not one.
|
|
127
|
+
*
|
|
128
|
+
* Global layouts: Windows `<prefix>\node_modules\looply-cli` with the shim
|
|
129
|
+
* `<prefix>\looply.cmd`; elsewhere `<prefix>/lib/node_modules/looply-cli` with
|
|
130
|
+
* `<prefix>/bin/looply`. The shim is what tells a global install from a
|
|
131
|
+
* project's own `node_modules`, which has the same folder shape.
|
|
132
|
+
*/
|
|
133
|
+
function globalInstallOf(bin, platform, fileExists) {
|
|
134
|
+
if (bin === null)
|
|
135
|
+
return null;
|
|
136
|
+
const path = platform === 'win32' ? node_path_1.win32 : node_path_1.posix;
|
|
137
|
+
const unified = bin.replace(/\\/g, '/');
|
|
138
|
+
const tail = `/node_modules/${exports.PACKAGE}/dist/bin.js`;
|
|
139
|
+
const tailAt = platform === 'win32' ? unified.toLowerCase().lastIndexOf(tail) : unified.lastIndexOf(tail);
|
|
140
|
+
if (tailAt < 0 || tailAt + tail.length !== unified.length)
|
|
141
|
+
return null;
|
|
142
|
+
if (/\/_npx\//i.test(unified))
|
|
143
|
+
return null;
|
|
144
|
+
const packageDir = path.dirname(path.dirname(bin));
|
|
145
|
+
const modules = path.dirname(packageDir);
|
|
146
|
+
const parent = path.dirname(modules);
|
|
147
|
+
if (platform === 'win32') {
|
|
148
|
+
return fileExists(path.join(parent, 'looply.cmd')) ? { prefix: parent, packageDir } : null;
|
|
149
|
+
}
|
|
150
|
+
if (path.basename(parent) !== 'lib')
|
|
151
|
+
return null;
|
|
152
|
+
const prefix = path.dirname(parent);
|
|
153
|
+
return fileExists(path.join(prefix, 'bin', 'looply')) ? { prefix, packageDir } : null;
|
|
154
|
+
}
|
|
155
|
+
/** Whether the published version is newer than this one — the only reason to ask the server. */
|
|
156
|
+
function isNewer(latest, current) {
|
|
157
|
+
const a = (0, core_1.parseReleaseVersion)(latest);
|
|
158
|
+
const b = (0, core_1.parseReleaseVersion)(current);
|
|
159
|
+
return a !== null && b !== null && (0, core_1.compareReleaseVersions)(a, b) > 0;
|
|
160
|
+
}
|
|
161
|
+
function decideUpdate(i) {
|
|
162
|
+
if (i.off !== null)
|
|
163
|
+
return { kind: 'stay', why: `automatic updates are off (${i.off})` };
|
|
164
|
+
if (i.install === null) {
|
|
165
|
+
return { kind: 'stay', why: 'this looply-cli is not an npm global install, so it is not updated in place' };
|
|
166
|
+
}
|
|
167
|
+
if ((0, core_1.parseReleaseVersion)(i.current) === null) {
|
|
168
|
+
return { kind: 'stay', why: `this looply-cli's own version (${i.current}) cannot be read` };
|
|
169
|
+
}
|
|
170
|
+
if (!i.latest.ok)
|
|
171
|
+
return { kind: 'stay', why: `npm could not be asked for the latest ${exports.PACKAGE} (${i.latest.error})` };
|
|
172
|
+
const latest = i.latest.version;
|
|
173
|
+
if (!isNewer(latest, i.current))
|
|
174
|
+
return { kind: 'stay', why: `up to date (${i.current})` };
|
|
175
|
+
if (i.compat === undefined || !i.compat.ok) {
|
|
176
|
+
const err = i.compat === undefined ? 'not asked' : i.compat.error;
|
|
177
|
+
return { kind: 'stay', why: `${latest} is published, but the server could not be asked whether it accepts it (${err})` };
|
|
178
|
+
}
|
|
179
|
+
const problem = (0, core_1.cliCompatProblem)(i.compat.value, latest);
|
|
180
|
+
if (problem !== null)
|
|
181
|
+
return { kind: 'stay', why: `${latest} is published, but ${problem}` };
|
|
182
|
+
if (i.last !== null && !i.last.ok && i.last.version === latest) {
|
|
183
|
+
const at = Date.parse(i.last.at);
|
|
184
|
+
if (Number.isFinite(at) && i.now - at < exports.RETRY_FAILED_AFTER_MS) {
|
|
185
|
+
const again = new Date(at + exports.RETRY_FAILED_AFTER_MS).toISOString();
|
|
186
|
+
return { kind: 'stay', why: `installing ${latest} failed at ${i.last.at} (${i.last.detail}); trying again after ${again}` };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return { kind: 'update', to: latest };
|
|
190
|
+
}
|
|
191
|
+
/* ──────────────────────────────────────────────────────── runs in flight ── */
|
|
192
|
+
/**
|
|
193
|
+
* Whether one process's command line is a `looply run`: this CLI's `bin.js`
|
|
194
|
+
* (an installed looply-cli, or exactly `ownBin`) with `run` as its command.
|
|
195
|
+
* The daemon launches runs as `node <bin.js> run …` (`run-launcher.ts`).
|
|
196
|
+
*/
|
|
197
|
+
function isLooplyRunCommand(commandLine, ownBin) {
|
|
198
|
+
return looplyCommandOf(commandLine, ownBin) === 'run';
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* The looply command a process runs (`run`, `serve`, `daemon`, `mcp`, …), or
|
|
202
|
+
* null when it is not this CLI at all.
|
|
203
|
+
*/
|
|
204
|
+
function looplyCommandOf(commandLine, ownBin) {
|
|
205
|
+
const line = commandLine.replace(/\\/g, '/');
|
|
206
|
+
const own = ownBin?.replace(/\\/g, '/').toLowerCase() ?? null;
|
|
207
|
+
const lower = line.toLowerCase();
|
|
208
|
+
let at = -1;
|
|
209
|
+
const m = /looply-cli\/dist\/bin\.js/i.exec(line);
|
|
210
|
+
if (m)
|
|
211
|
+
at = m.index + m[0].length;
|
|
212
|
+
else if (own !== null && lower.includes(own))
|
|
213
|
+
at = lower.indexOf(own) + own.length;
|
|
214
|
+
if (at < 0)
|
|
215
|
+
return null;
|
|
216
|
+
const args = line.slice(at).replace(/^"/, '').trim().split(/\s+/).map((a) => a.replace(/^"|"$/g, ''));
|
|
217
|
+
return args.find((a) => a !== '' && !a.startsWith('-')) ?? '(no command)';
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* How long a process has been running, from `ps -o etime` (`[[dd-]hh:]mm:ss`)
|
|
221
|
+
* or a plain number of seconds (the Windows listing below). Null when unreadable.
|
|
222
|
+
*/
|
|
223
|
+
function parseElapsedSeconds(text) {
|
|
224
|
+
const t = text.trim();
|
|
225
|
+
if (/^\d+$/.test(t))
|
|
226
|
+
return Number(t);
|
|
227
|
+
const m = /^(?:(\d+)-)?(?:(\d+):)?(\d+):(\d+)$/.exec(t);
|
|
228
|
+
if (!m)
|
|
229
|
+
return null;
|
|
230
|
+
return Number(m[1] ?? 0) * 86_400 + Number(m[2] ?? 0) * 3_600 + Number(m[3]) * 60 + Number(m[4]);
|
|
231
|
+
}
|
|
232
|
+
/** Commands that are this machine's own supervision, not something a person or an agent left running. */
|
|
233
|
+
const SUPERVISION = new Set(['run', 'serve', 'daemon']);
|
|
234
|
+
/**
|
|
235
|
+
* Runs in a listing of `<elapsed> <command line>` lines. A run older than
|
|
236
|
+
* `staleMs` is counted apart: a hung run must not hold every update back.
|
|
237
|
+
* A line whose age cannot be read is counted as fresh — the direction that
|
|
238
|
+
* cannot install under a live run.
|
|
239
|
+
*/
|
|
240
|
+
function countLooplyRuns(listing, ownBin, staleMs = exports.STALE_RUN_MS) {
|
|
241
|
+
let fresh = 0;
|
|
242
|
+
let stale = 0;
|
|
243
|
+
const others = [];
|
|
244
|
+
for (const raw of listing.split(/\r?\n/)) {
|
|
245
|
+
const line = raw.trim();
|
|
246
|
+
if (line === '')
|
|
247
|
+
continue;
|
|
248
|
+
const m = /^(\S+)\s+(.*)$/.exec(line);
|
|
249
|
+
const age = m ? parseElapsedSeconds(m[1]) : null;
|
|
250
|
+
const command = age === null ? line : m[2];
|
|
251
|
+
const what = looplyCommandOf(command, ownBin);
|
|
252
|
+
if (what === null)
|
|
253
|
+
continue;
|
|
254
|
+
if (what !== 'run') {
|
|
255
|
+
if (!SUPERVISION.has(what))
|
|
256
|
+
others.push(what);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (age !== null && age * 1000 > staleMs)
|
|
260
|
+
stale += 1;
|
|
261
|
+
else
|
|
262
|
+
fresh += 1;
|
|
263
|
+
}
|
|
264
|
+
return { fresh, stale, others };
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Slots claimed for a launch that has not spawned its run yet (`machine-run-slots.ts`):
|
|
268
|
+
* a run about to exist, which no process listing can show.
|
|
269
|
+
*/
|
|
270
|
+
function pendingRunSlots(dir, now, fs = nodeDirFs) {
|
|
271
|
+
let names;
|
|
272
|
+
try {
|
|
273
|
+
names = fs.list(dir).filter((n) => n.endsWith('.json'));
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
let n = 0;
|
|
279
|
+
for (const name of names) {
|
|
280
|
+
try {
|
|
281
|
+
const s = JSON.parse(fs.read((0, node_path_1.join)(dir, name)));
|
|
282
|
+
const at = typeof s.at === 'string' ? Date.parse(s.at) : NaN;
|
|
283
|
+
if (s.pending === true && Number.isFinite(at) && now - at <= machine_run_slots_1.PENDING_TTL_MS)
|
|
284
|
+
n += 1;
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
// A torn write is not a run.
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return n;
|
|
291
|
+
}
|
|
292
|
+
const nodeDirFs = { list: (d) => (0, node_fs_1.readdirSync)(d), read: (p) => (0, node_fs_1.readFileSync)(p, 'utf8') };
|
|
293
|
+
/** Serve on a new version that has stayed up this long is that version proven. */
|
|
294
|
+
exports.HEALTHY_AFTER_MS = 5 * 60_000;
|
|
295
|
+
/** This many starts on an unproven version within `CRASH_WINDOW_MS` is a crash loop. */
|
|
296
|
+
exports.CRASH_STARTS = 3;
|
|
297
|
+
exports.CRASH_WINDOW_MS = 10 * 60_000;
|
|
298
|
+
/** A run that slipped in while the daemons stopped, with the drain already spent, is waited for this long at most. */
|
|
299
|
+
exports.SLIP_WAIT_MAX_MS = 15 * 60_000;
|
|
300
|
+
/** A rollback that could not be done is tried again after this, doubling, up to `ROLLBACK_RETRY_MAX_MS`. */
|
|
301
|
+
exports.ROLLBACK_RETRY_BASE_MS = 5 * 60_000;
|
|
302
|
+
exports.ROLLBACK_RETRY_MAX_MS = 60 * 60_000;
|
|
303
|
+
const recentStarts = (starts, now) => starts.filter((t) => {
|
|
304
|
+
const at = Date.parse(t);
|
|
305
|
+
return Number.isFinite(at) && now - at <= exports.CRASH_WINDOW_MS;
|
|
306
|
+
});
|
|
307
|
+
/**
|
|
308
|
+
* Record that serve is starting on an unproven update — called as the FIRST
|
|
309
|
+
* thing serve does, before it reads anything or takes its lock, so a crash
|
|
310
|
+
* anywhere after this still counts. (A crash while loading the modules happens
|
|
311
|
+
* before any code runs; the `--version` check before the hand-over is what
|
|
312
|
+
* catches that one.) Null when there is nothing to record.
|
|
313
|
+
*/
|
|
314
|
+
function recordStart(state, current, now) {
|
|
315
|
+
const p = state.pendingVerify;
|
|
316
|
+
if (p === undefined || p.version !== current)
|
|
317
|
+
return null;
|
|
318
|
+
return { ...state, pendingVerify: { ...p, starts: [...recentStarts(p.starts, now), new Date(now).toISOString()] } };
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* What a serve that has its lock does about an unproven update. Pure.
|
|
322
|
+
*
|
|
323
|
+
* `CRASH_STARTS` starts within `CRASH_WINDOW_MS` means serve keeps dying on it
|
|
324
|
+
* — under systemd or launchd nobody else would notice — so the previous version
|
|
325
|
+
* goes back. A rollback that could not be done waits out its backoff.
|
|
326
|
+
*/
|
|
327
|
+
function startupGuard(state, current, now) {
|
|
328
|
+
const p = state.pendingVerify;
|
|
329
|
+
if (p === undefined || p.version !== current)
|
|
330
|
+
return { kind: 'nothing' };
|
|
331
|
+
const recent = recentStarts(p.starts, now);
|
|
332
|
+
if (recent.length < exports.CRASH_STARTS)
|
|
333
|
+
return { kind: 'nothing' };
|
|
334
|
+
const why = `serve started ${recent.length} times on ${current} within ${exports.CRASH_WINDOW_MS / 60_000} minutes`;
|
|
335
|
+
const retryAt = p.rollbackRetryAt === undefined ? NaN : Date.parse(p.rollbackRetryAt);
|
|
336
|
+
if (Number.isFinite(retryAt) && now < retryAt)
|
|
337
|
+
return { kind: 'wait', why: `${why}; putting ${p.previous} back is tried again at ${p.rollbackRetryAt}` };
|
|
338
|
+
return { kind: 'rollback', to: p.previous, why };
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* The state after a rollback attempt. Done: the update is over and recorded as
|
|
342
|
+
* failed, so the version waits `RETRY_FAILED_AFTER_MS`. Not done: the update is
|
|
343
|
+
* KEPT pending, with a backoff, so the next start tries again.
|
|
344
|
+
*/
|
|
345
|
+
function afterRollback(state, current, now, done, why) {
|
|
346
|
+
const at = new Date(now).toISOString();
|
|
347
|
+
const lastAttempt = { version: current, at, ok: false, detail: why };
|
|
348
|
+
const p = state.pendingVerify;
|
|
349
|
+
if (done || p === undefined) {
|
|
350
|
+
const { pendingVerify: _done, ...rest } = state;
|
|
351
|
+
return { ...rest, lastAttempt };
|
|
352
|
+
}
|
|
353
|
+
const tries = (p.rollbackTries ?? 0) + 1;
|
|
354
|
+
const wait = Math.min(exports.ROLLBACK_RETRY_BASE_MS * 2 ** (tries - 1), exports.ROLLBACK_RETRY_MAX_MS);
|
|
355
|
+
return { ...state, lastAttempt, pendingVerify: { ...p, rollbackTries: tries, rollbackRetryAt: new Date(now + wait).toISOString() } };
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* A relaunched serve that never came up: that version failed, whatever the
|
|
359
|
+
* rollback managed. Recorded so it is not tried again for `RETRY_FAILED_AFTER_MS`
|
|
360
|
+
* — without this, `lastAttempt` still said OK and the next check reinstalled it.
|
|
361
|
+
*/
|
|
362
|
+
function relaunchFailed(state, version, now, detail) {
|
|
363
|
+
const { pendingVerify: _done, ...rest } = state;
|
|
364
|
+
return { ...rest, lastAttempt: { version, at: new Date(now).toISOString(), ok: false, detail } };
|
|
365
|
+
}
|
|
366
|
+
class SelfUpdater {
|
|
367
|
+
deps;
|
|
368
|
+
target = null;
|
|
369
|
+
since = 0;
|
|
370
|
+
lastCheck = Number.NEGATIVE_INFINITY;
|
|
371
|
+
lastWhy = null;
|
|
372
|
+
waitingSaid = false;
|
|
373
|
+
drainSpentSaid = false;
|
|
374
|
+
staleSaid = 0;
|
|
375
|
+
finished = false;
|
|
376
|
+
constructor(deps) {
|
|
377
|
+
this.deps = deps;
|
|
378
|
+
}
|
|
379
|
+
/** The version an update is waiting for, if any. */
|
|
380
|
+
pending() {
|
|
381
|
+
return this.target;
|
|
382
|
+
}
|
|
383
|
+
async tick() {
|
|
384
|
+
if (this.finished)
|
|
385
|
+
return;
|
|
386
|
+
const d = this.deps;
|
|
387
|
+
const now = d.now();
|
|
388
|
+
this.prove(now);
|
|
389
|
+
if (now - this.lastCheck >= (d.checkEveryMs ?? exports.CHECK_EVERY_MS)) {
|
|
390
|
+
this.lastCheck = now;
|
|
391
|
+
await this.decide(now);
|
|
392
|
+
}
|
|
393
|
+
if (this.target === null)
|
|
394
|
+
return;
|
|
395
|
+
const off = d.offReason();
|
|
396
|
+
if (off !== null) {
|
|
397
|
+
d.log(say(`automatic updates were switched off (${off}); not updating to ${this.target}`));
|
|
398
|
+
this.lastWhy = `automatic updates are off (${off})`;
|
|
399
|
+
this.target = null;
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
const runs = await this.count();
|
|
403
|
+
if (runs.fresh === 0)
|
|
404
|
+
return this.apply(this.target, false);
|
|
405
|
+
if (now - this.since >= (d.drainAfterMs ?? exports.DRAIN_AFTER_MS)) {
|
|
406
|
+
if (d.readState().drained?.version !== this.target)
|
|
407
|
+
return this.apply(this.target, true);
|
|
408
|
+
if (!this.drainSpentSaid) {
|
|
409
|
+
d.log(say(`${this.target}: the one drain for this version is spent; waiting for a moment with no run in flight, however long that takes`));
|
|
410
|
+
this.drainSpentSaid = true;
|
|
411
|
+
}
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (!this.waitingSaid) {
|
|
415
|
+
d.log(say(`${this.target} waits for a moment with no run in flight (${runs.fresh} running now); no run is interrupted`));
|
|
416
|
+
this.waitingSaid = true;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/** Serve has stayed up on an unproven update long enough: it is proven, and no rollback will follow. */
|
|
420
|
+
prove(now) {
|
|
421
|
+
const d = this.deps;
|
|
422
|
+
const p = d.readState().pendingVerify;
|
|
423
|
+
if (p === undefined || p.version !== d.current || d.startedAt === undefined)
|
|
424
|
+
return;
|
|
425
|
+
if (now - d.startedAt < exports.HEALTHY_AFTER_MS)
|
|
426
|
+
return;
|
|
427
|
+
const { pendingVerify: _done, ...rest } = d.readState();
|
|
428
|
+
d.writeState(rest);
|
|
429
|
+
d.log(say(`${d.current} has run for ${exports.HEALTHY_AFTER_MS / 60_000} minutes since the update; it stays`));
|
|
430
|
+
}
|
|
431
|
+
/** Runs now, with any stale ones reported (once per count that changed). */
|
|
432
|
+
async count() {
|
|
433
|
+
const runs = await this.deps.runsInFlight();
|
|
434
|
+
if (runs.stale > 0 && runs.stale !== this.staleSaid) {
|
|
435
|
+
this.deps.log(say(`${runs.stale} run(s) have been going for over ${exports.STALE_RUN_MS / 3_600_000} hours; they do not hold the update back — look at them (\`looply status\` in their repository)`));
|
|
436
|
+
}
|
|
437
|
+
this.staleSaid = runs.stale;
|
|
438
|
+
return runs;
|
|
439
|
+
}
|
|
440
|
+
/** The full decision, asked afresh: npm, the server, the opt-out and the last attempt. */
|
|
441
|
+
async ask(now) {
|
|
442
|
+
const d = this.deps;
|
|
443
|
+
const off = d.offReason();
|
|
444
|
+
let latest;
|
|
445
|
+
let compat;
|
|
446
|
+
if (off === null && d.install !== null) {
|
|
447
|
+
try {
|
|
448
|
+
latest = { ok: true, version: await d.latest() };
|
|
449
|
+
}
|
|
450
|
+
catch (e) {
|
|
451
|
+
latest = { ok: false, error: e?.message ?? String(e) };
|
|
452
|
+
}
|
|
453
|
+
if (latest.ok && isNewer(latest.version, d.current)) {
|
|
454
|
+
try {
|
|
455
|
+
compat = { ok: true, value: await d.compat() };
|
|
456
|
+
}
|
|
457
|
+
catch (e) {
|
|
458
|
+
compat = { ok: false, error: e?.message ?? String(e) };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
latest = { ok: false, error: 'not asked' };
|
|
464
|
+
}
|
|
465
|
+
return decideUpdate({ current: d.current, off, install: d.install, latest, compat, last: d.readState().lastAttempt ?? null, now });
|
|
466
|
+
}
|
|
467
|
+
async decide(now) {
|
|
468
|
+
const d = this.deps;
|
|
469
|
+
const decision = await this.ask(now);
|
|
470
|
+
if (decision.kind === 'update') {
|
|
471
|
+
if (this.target !== decision.to) {
|
|
472
|
+
this.target = decision.to;
|
|
473
|
+
this.since = now;
|
|
474
|
+
this.waitingSaid = false;
|
|
475
|
+
this.drainSpentSaid = false;
|
|
476
|
+
d.log(say(`${exports.PACKAGE} ${decision.to} is published and the server accepts it (this PC: ${d.current}); updating in the first gap with no run in flight`));
|
|
477
|
+
}
|
|
478
|
+
this.lastWhy = null;
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
if (this.target !== null)
|
|
482
|
+
d.log(say(`no longer updating to ${this.target}: ${decision.why}`));
|
|
483
|
+
else if (decision.why !== this.lastWhy)
|
|
484
|
+
d.log(say(`staying on ${d.current}: ${decision.why}`));
|
|
485
|
+
this.target = null;
|
|
486
|
+
this.lastWhy = decision.why;
|
|
487
|
+
}
|
|
488
|
+
record(patch) {
|
|
489
|
+
this.deps.writeState({ ...this.deps.readState(), ...patch });
|
|
490
|
+
}
|
|
491
|
+
async apply(to, drain) {
|
|
492
|
+
const d = this.deps;
|
|
493
|
+
this.finished = true;
|
|
494
|
+
const unchanged = { expect: d.current };
|
|
495
|
+
if (drain) {
|
|
496
|
+
this.record({ drained: { version: to, at: new Date(d.now()).toISOString() } });
|
|
497
|
+
const hours = Math.round((d.drainAfterMs ?? exports.DRAIN_AFTER_MS) / 3_600_000);
|
|
498
|
+
d.log(say(`no gap in ${hours} hours; asking the daemons to stop so no new run starts — the runs in flight finish first, none is interrupted (once per version)`));
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
d.log(say(`updating ${d.current} → ${to}: no run in flight; asking the daemons to stop at their next idle point`));
|
|
502
|
+
}
|
|
503
|
+
await d.stopDaemons();
|
|
504
|
+
if (d.cancelled?.())
|
|
505
|
+
return;
|
|
506
|
+
let runs = await this.count();
|
|
507
|
+
if (runs.fresh > 0) {
|
|
508
|
+
// A run that slipped in while the daemons stopped. Waiting for it with the
|
|
509
|
+
// daemons down IS a drain, so it is recorded as one; with the drain already
|
|
510
|
+
// spent on this version, it gets a short wait, not another three hours.
|
|
511
|
+
const spent = !drain && d.readState().drained?.version === to;
|
|
512
|
+
if (!drain && !spent)
|
|
513
|
+
this.record({ drained: { version: to, at: new Date(d.now()).toISOString() } });
|
|
514
|
+
const waitMs = spent ? exports.SLIP_WAIT_MAX_MS : (d.drainWaitMaxMs ?? exports.DRAIN_WAIT_MAX_MS);
|
|
515
|
+
d.log(say(`${runs.fresh} run(s) in flight; waiting up to ${Math.round(waitMs / 60_000)} minutes for them to finish before installing`));
|
|
516
|
+
const giveUpAt = d.now() + waitMs;
|
|
517
|
+
while (runs.fresh > 0) {
|
|
518
|
+
if (d.now() >= giveUpAt) {
|
|
519
|
+
d.log(say(`not updating to ${to}: ${runs.fresh} run(s) still in flight after ${Math.round(waitMs / 60_000)} minutes. Restarting serve on ${d.current}; ${to} waits for a natural gap (no second drain for it)`));
|
|
520
|
+
d.restart(unchanged);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
await d.sleep(d.gapPollMs ?? exports.SELF_UPDATE_TICK_MS);
|
|
524
|
+
if (d.cancelled?.())
|
|
525
|
+
return;
|
|
526
|
+
runs = await this.count();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
// 🔴 ASKED AGAIN, NOW. A drain can take hours: in that time the operator may
|
|
530
|
+
// have paused the rollout, `latest` may have been moved back, or this PC
|
|
531
|
+
// switched off. What was true when the wait began decides nothing.
|
|
532
|
+
const again = await this.ask(d.now());
|
|
533
|
+
if (again.kind !== 'update' || again.to !== to) {
|
|
534
|
+
const why = again.kind === 'stay' ? again.why : `${again.to} is the one to install now`;
|
|
535
|
+
d.log(say(`not installing ${to} after all: ${why}; restarting serve on ${d.current}`));
|
|
536
|
+
d.restart(unchanged);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
const others = runs.others ?? [];
|
|
540
|
+
if (others.length > 0) {
|
|
541
|
+
const named = [...new Set(others)].map((c) => `${c} ×${others.filter((o) => o === c).length}`).join(', ');
|
|
542
|
+
d.log(say(`replacing the package under ${others.length} other live looply process(es) (${named}); they keep the code they loaded — restart them after the update`));
|
|
543
|
+
}
|
|
544
|
+
d.log(say(`installing ${exports.PACKAGE}@${to}`));
|
|
545
|
+
const result = await this.installAndCheck(to);
|
|
546
|
+
if (result.ok) {
|
|
547
|
+
const at = new Date(d.now()).toISOString();
|
|
548
|
+
this.record({
|
|
549
|
+
lastAttempt: { version: to, at, ok: true, detail: result.detail },
|
|
550
|
+
pendingVerify: { version: to, previous: d.current, at, starts: [] },
|
|
551
|
+
});
|
|
552
|
+
d.log(say(`installed ${to}, and it answers \`--version\` with ${to}; restarting serve on it`));
|
|
553
|
+
d.restart({ expect: to, rollbackTo: d.current });
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
this.record({ lastAttempt: { version: to, at: new Date(d.now()).toISOString(), ok: false, detail: result.detail } });
|
|
557
|
+
d.log(say(`installing ${to} failed — ${result.detail}; putting ${d.current} back`));
|
|
558
|
+
const back = await this.installAndCheck(d.current);
|
|
559
|
+
d.log(back.ok
|
|
560
|
+
? say(`${d.current} is back; restarting serve on it, and trying ${to} again after ${exports.RETRY_FAILED_AFTER_MS / 3_600_000} hours`)
|
|
561
|
+
: say(`🔴 could not put ${d.current} back either (${back.detail}); restarting serve on whatever is installed — this PC needs a look`));
|
|
562
|
+
d.restart(unchanged);
|
|
563
|
+
}
|
|
564
|
+
/** Install, then ask the installed binary what it is: npm's exit code is not the proof. */
|
|
565
|
+
async installAndCheck(version) {
|
|
566
|
+
const d = this.deps;
|
|
567
|
+
let r;
|
|
568
|
+
try {
|
|
569
|
+
r = await d.installVersion(version);
|
|
570
|
+
}
|
|
571
|
+
catch (e) {
|
|
572
|
+
r = { ok: false, detail: e?.message ?? String(e) };
|
|
573
|
+
}
|
|
574
|
+
if (!r.ok)
|
|
575
|
+
return r;
|
|
576
|
+
const says = await d.installedVersion().catch(() => null);
|
|
577
|
+
if (says !== version)
|
|
578
|
+
return { ok: false, detail: `npm finished, but the installed looply answers ${says ?? 'nothing'} to --version` };
|
|
579
|
+
return r;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
exports.SelfUpdater = SelfUpdater;
|
|
583
|
+
/**
|
|
584
|
+
* Whether the serve just spawned came up: it took the serve lock and is still
|
|
585
|
+
* alive `settleMs` later. A release that crashes at startup fails this, and the
|
|
586
|
+
* caller puts the previous version back instead of leaving the PC without a
|
|
587
|
+
* supervisor until the next login.
|
|
588
|
+
*/
|
|
589
|
+
async function confirmServeStarted(pid, deps, opts = {}) {
|
|
590
|
+
const deadline = deps.now() + (opts.timeoutMs ?? 90_000);
|
|
591
|
+
const poll = opts.pollMs ?? 1_000;
|
|
592
|
+
while (deps.now() < deadline) {
|
|
593
|
+
if (!deps.alive(pid))
|
|
594
|
+
return false;
|
|
595
|
+
if (deps.lockPid() === pid) {
|
|
596
|
+
await deps.sleep(opts.settleMs ?? 15_000);
|
|
597
|
+
return deps.alive(pid) && deps.lockPid() === pid;
|
|
598
|
+
}
|
|
599
|
+
await deps.sleep(poll);
|
|
600
|
+
}
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Whether serve runs under a service manager that restarts it.
|
|
605
|
+
*
|
|
606
|
+
* ⚠️ ONLY THE EXPLICIT FLAG. `INVOCATION_ID` and `XPC_SERVICE_NAME` are also set
|
|
607
|
+
* in GNOME terminals, VS Code and iTerm, where exiting 75 would leave nobody to
|
|
608
|
+
* start serve again. The service templates `looply serve install` prints set
|
|
609
|
+
* `LOOPLY_SERVE_MANAGED=1` (`serve.ts`).
|
|
610
|
+
*/
|
|
611
|
+
function managedServe(env) {
|
|
612
|
+
return /^(1|true|yes)$/i.test((env['LOOPLY_SERVE_MANAGED'] ?? '').trim());
|
|
613
|
+
}
|
|
614
|
+
/** The unit file name the systemd template `looply serve install` prints. */
|
|
615
|
+
exports.SYSTEMD_UNIT = 'looply-serve.service';
|
|
616
|
+
/** The launchd label the plist template uses. */
|
|
617
|
+
exports.LAUNCHD_LABEL = 'dev.looply.serve';
|
|
618
|
+
/**
|
|
619
|
+
* The systemd unit this process runs in, from `/proc/self/cgroup`: the unified
|
|
620
|
+
* `0::` line (cgroup v2, and the hybrid layout systemd also writes), else the
|
|
621
|
+
* `name=systemd` line of cgroup v1. Null when neither names a unit.
|
|
622
|
+
*/
|
|
623
|
+
function systemdUnitOf(cgroup) {
|
|
624
|
+
const lines = cgroup.split(/\r?\n/).filter((l) => l.trim() !== '');
|
|
625
|
+
const pathOf = (l) => l.split(':').slice(2).join(':');
|
|
626
|
+
const leaf = (p) => p.split('/').filter(Boolean).pop() ?? null;
|
|
627
|
+
const unified = lines.find((l) => l.startsWith('0::'));
|
|
628
|
+
const fromUnified = unified === undefined ? null : leaf(pathOf(unified));
|
|
629
|
+
if (fromUnified !== null)
|
|
630
|
+
return fromUnified;
|
|
631
|
+
const v1 = lines.find((l) => /^\d+:name=systemd:/.test(l));
|
|
632
|
+
return v1 === undefined ? null : leaf(pathOf(v1));
|
|
633
|
+
}
|
|
634
|
+
exports.REINSTALL_HINT = 'serve runs under a service manager whose unit predates LOOPLY_SERVE_MANAGED=1 — print a fresh one with `looply serve install` and replace yours';
|
|
635
|
+
/**
|
|
636
|
+
* Whether a service manager runs this serve, flag or no flag. Decided ONCE, at
|
|
637
|
+
* process start (`nodeServeDeps`), never at restart time: by then a serve that
|
|
638
|
+
* relaunched itself detached, or one run under `nohup`, has been re-parented,
|
|
639
|
+
* and reading the process tree then would call it managed and leave it dead.
|
|
640
|
+
*
|
|
641
|
+
* 🔴 A UNIT FROM BEFORE THE FLAG MUST STILL BE SEEN. Under systemd with the
|
|
642
|
+
* default KillMode, a serve that thought itself unmanaged would spawn its
|
|
643
|
+
* replacement, exit 0, and have systemd kill the whole cgroup — the
|
|
644
|
+
* replacement with it. So, without the flag:
|
|
645
|
+
* systemd `INVOCATION_ID` AND this process's own unit is `looply-serve.service`
|
|
646
|
+
* (cgroup v2, v1 or hybrid). Any other unit — a terminal's `.scope`,
|
|
647
|
+
* `gnome-terminal-server.service`, a `systemd-run` transient — is not ours.
|
|
648
|
+
* launchd `XPC_SERVICE_NAME` is our own label `dev.looply.serve`; nothing else.
|
|
649
|
+
* Either way it is treated as managed, and the hint says to reinstall the unit.
|
|
650
|
+
*/
|
|
651
|
+
function serviceManagerOf(env, platform, facts) {
|
|
652
|
+
if (managedServe(env))
|
|
653
|
+
return { managed: true, hint: null };
|
|
654
|
+
if (platform === 'linux' && (env['INVOCATION_ID'] ?? '') !== '' && facts.cgroup !== null) {
|
|
655
|
+
if (systemdUnitOf(facts.cgroup) === exports.SYSTEMD_UNIT)
|
|
656
|
+
return { managed: true, hint: exports.REINSTALL_HINT };
|
|
657
|
+
}
|
|
658
|
+
if (platform === 'darwin' && (env['XPC_SERVICE_NAME'] ?? '').trim() === exports.LAUNCHD_LABEL) {
|
|
659
|
+
return { managed: true, hint: exports.REINSTALL_HINT };
|
|
660
|
+
}
|
|
661
|
+
return { managed: false, hint: null };
|
|
662
|
+
}
|
|
663
|
+
/* istanbul ignore next — real /proc */
|
|
664
|
+
function nodeServiceFacts() {
|
|
665
|
+
let cgroup = null;
|
|
666
|
+
try {
|
|
667
|
+
cgroup = (0, node_fs_1.readFileSync)('/proc/self/cgroup', 'utf8');
|
|
668
|
+
}
|
|
669
|
+
catch {
|
|
670
|
+
cgroup = null;
|
|
671
|
+
}
|
|
672
|
+
return { cgroup };
|
|
673
|
+
}
|
|
674
|
+
function updateRegistry(env) {
|
|
675
|
+
return (env['LOOPLY_UPDATE_REGISTRY'] ?? 'https://registry.npmjs.org').trim().replace(/\/+$/, '');
|
|
676
|
+
}
|
|
677
|
+
/* istanbul ignore next — real network, processes and files; the decisions live above */
|
|
678
|
+
function nodeSelfUpdateTask(o, ctl) {
|
|
679
|
+
const install = globalInstallOf(o.bin, o.platform, node_fs_1.existsSync);
|
|
680
|
+
const stateFile = (0, node_path_1.join)(o.home, '.looply', exports.STATE_FILE);
|
|
681
|
+
const registry = updateRegistry(o.env);
|
|
682
|
+
let scanBroken = false;
|
|
683
|
+
const updater = new SelfUpdater({
|
|
684
|
+
current: o.current,
|
|
685
|
+
install,
|
|
686
|
+
startedAt: Date.now(),
|
|
687
|
+
now: () => Date.now(),
|
|
688
|
+
log: o.log,
|
|
689
|
+
offReason: () => autoUpdateOff(o.env, () => (0, machine_settings_1.readLocalMachineSettings)(o.home)),
|
|
690
|
+
latest: async () => {
|
|
691
|
+
const res = await fetch(`${registry}/${exports.PACKAGE}/latest`, {
|
|
692
|
+
headers: { accept: 'application/json' },
|
|
693
|
+
signal: AbortSignal.timeout(20_000),
|
|
694
|
+
});
|
|
695
|
+
if (!res.ok)
|
|
696
|
+
throw new Error(`npm answered ${res.status}`);
|
|
697
|
+
const v = (await res.json()).version;
|
|
698
|
+
if (typeof v !== 'string')
|
|
699
|
+
throw new Error('npm gave no version');
|
|
700
|
+
return v;
|
|
701
|
+
},
|
|
702
|
+
compat: async () => {
|
|
703
|
+
const identity = new machine_store_1.MachineStore((0, machine_store_1.machineFilePath)(o.home)).load();
|
|
704
|
+
if (identity === null)
|
|
705
|
+
throw new Error('this machine is not paired with a server');
|
|
706
|
+
return new server_client_1.ServerClient({ baseUrl: identity.serverUrl, token: identity.token }).cliCompat();
|
|
707
|
+
},
|
|
708
|
+
runsInFlight: async () => {
|
|
709
|
+
// The process table is the truth about runs that exist; a slot claimed for
|
|
710
|
+
// a launch that has not spawned yet is visible only in the slots. A bound
|
|
711
|
+
// slot is not trusted over the table: on Windows a dead run's pid is soon
|
|
712
|
+
// reused, and that slot would read as a run for hours.
|
|
713
|
+
try {
|
|
714
|
+
const procs = countLooplyRuns(await processListing(o.platform), o.bin);
|
|
715
|
+
scanBroken = false;
|
|
716
|
+
return { fresh: procs.fresh + pendingRunSlots((0, machine_run_slots_1.machineSlotsDir)(o.home), Date.now()), stale: procs.stale, others: procs.others ?? [] };
|
|
717
|
+
}
|
|
718
|
+
catch (e) {
|
|
719
|
+
if (!scanBroken)
|
|
720
|
+
o.log(say(`cannot list processes (${e.message}); counting the machine's run slots instead`));
|
|
721
|
+
scanBroken = true;
|
|
722
|
+
return { fresh: (0, machine_run_slots_1.machineRunSlots)(1, { home: o.home }).inUse(), stale: 0 };
|
|
723
|
+
}
|
|
724
|
+
},
|
|
725
|
+
stopDaemons: () => ctl.stopDaemons(),
|
|
726
|
+
installVersion: (v) => npmInstall(install, v, registry, o.platform, o.log),
|
|
727
|
+
installedVersion: () => binaryVersion(o.bin),
|
|
728
|
+
readState: () => {
|
|
729
|
+
try {
|
|
730
|
+
return JSON.parse((0, node_fs_1.readFileSync)(stateFile, 'utf8'));
|
|
731
|
+
}
|
|
732
|
+
catch {
|
|
733
|
+
return {};
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
writeState: (state) => {
|
|
737
|
+
try {
|
|
738
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(stateFile), { recursive: true });
|
|
739
|
+
const tmp = `${stateFile}.${process.pid}.tmp`;
|
|
740
|
+
(0, node_fs_1.writeFileSync)(tmp, JSON.stringify(state, null, 2) + '\n', 'utf8');
|
|
741
|
+
(0, node_fs_1.renameSync)(tmp, stateFile);
|
|
742
|
+
}
|
|
743
|
+
catch (e) {
|
|
744
|
+
o.log(say(`could not record the attempt in ${stateFile} (${e.message})`));
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
restart: (plan) => ctl.restart(plan),
|
|
748
|
+
cancelled: () => ctl.cancelled(),
|
|
749
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
750
|
+
});
|
|
751
|
+
return { name: 'self-update', everyMs: exports.SELF_UPDATE_TICK_MS, run: () => updater.tick() };
|
|
752
|
+
}
|
|
753
|
+
/** Every node process as `<elapsed> <command line>`, one per line. */
|
|
754
|
+
/* istanbul ignore next — real processes */
|
|
755
|
+
function processListing(platform) {
|
|
756
|
+
return new Promise((resolve, reject) => {
|
|
757
|
+
const [cmd, args] = platform === 'win32'
|
|
758
|
+
? [
|
|
759
|
+
(0, untrusted_exec_1.powershellPath)(),
|
|
760
|
+
[
|
|
761
|
+
'-NoProfile',
|
|
762
|
+
'-NonInteractive',
|
|
763
|
+
'-Command',
|
|
764
|
+
"$now = Get-Date; Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | ForEach-Object { '{0} {1}' -f [int]($now - $_.CreationDate).TotalSeconds, $_.CommandLine }",
|
|
765
|
+
],
|
|
766
|
+
]
|
|
767
|
+
: ['ps', ['-axo', 'etime=,args=']];
|
|
768
|
+
(0, node_child_process_1.execFile)(cmd, args, { windowsHide: true, timeout: 30_000, maxBuffer: 16 * 1024 * 1024 }, (err, stdout) => {
|
|
769
|
+
if (err)
|
|
770
|
+
reject(err);
|
|
771
|
+
else
|
|
772
|
+
resolve(stdout);
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
/** `node <bin> --version`, from a fresh process — the code now on disk, not the code this serve loaded. */
|
|
777
|
+
/* istanbul ignore next — real processes */
|
|
778
|
+
function binaryVersion(bin) {
|
|
779
|
+
if (bin === null)
|
|
780
|
+
return Promise.resolve(null);
|
|
781
|
+
return new Promise((resolve) => {
|
|
782
|
+
(0, node_child_process_1.execFile)(process.execPath, [bin, '--version'], { windowsHide: true, timeout: 60_000 }, (err, stdout) => {
|
|
783
|
+
resolve(err ? null : stdout.trim() || null);
|
|
784
|
+
});
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* `npm install --global --prefix <the prefix serve runs from> looply-cli@<v>
|
|
789
|
+
* --registry <the registry latest was read from>`, through the npm that ships
|
|
790
|
+
* beside this node — no shell and no PATH lookup.
|
|
791
|
+
*/
|
|
792
|
+
/* istanbul ignore next — real npm */
|
|
793
|
+
function npmInstall(install, version, registry, platform, log) {
|
|
794
|
+
const nodeDir = (0, node_path_1.dirname)(process.execPath);
|
|
795
|
+
const npmCli = [
|
|
796
|
+
(0, node_path_1.join)(nodeDir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
|
|
797
|
+
(0, node_path_1.join)(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
|
|
798
|
+
].find((p) => (0, node_fs_1.existsSync)(p));
|
|
799
|
+
const args = [
|
|
800
|
+
'install',
|
|
801
|
+
'--global',
|
|
802
|
+
'--prefix',
|
|
803
|
+
install.prefix,
|
|
804
|
+
'--registry',
|
|
805
|
+
`${registry}/`,
|
|
806
|
+
`${exports.PACKAGE}@${version}`,
|
|
807
|
+
'--no-fund',
|
|
808
|
+
'--no-audit',
|
|
809
|
+
'--loglevel=error',
|
|
810
|
+
];
|
|
811
|
+
return new Promise((resolve) => {
|
|
812
|
+
const child = npmCli !== undefined
|
|
813
|
+
? (0, node_child_process_1.spawn)(process.execPath, [npmCli, ...args], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
814
|
+
: (0, node_child_process_1.spawn)(platform === 'win32' ? 'npm.cmd' : 'npm', args, {
|
|
815
|
+
windowsHide: true,
|
|
816
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
817
|
+
shell: platform === 'win32',
|
|
818
|
+
});
|
|
819
|
+
const tail = [];
|
|
820
|
+
const keep = (d) => {
|
|
821
|
+
for (const l of d.toString('utf8').split(/\r?\n/))
|
|
822
|
+
if (l.trim() !== '')
|
|
823
|
+
tail.push(l.trim());
|
|
824
|
+
tail.splice(0, Math.max(0, tail.length - 5));
|
|
825
|
+
};
|
|
826
|
+
child.stdout?.on('data', keep);
|
|
827
|
+
child.stderr?.on('data', keep);
|
|
828
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), 15 * 60_000);
|
|
829
|
+
child.on('error', (e) => {
|
|
830
|
+
clearTimeout(timer);
|
|
831
|
+
resolve({ ok: false, detail: `npm could not start (${e.message})` });
|
|
832
|
+
});
|
|
833
|
+
child.on('close', (code) => {
|
|
834
|
+
clearTimeout(timer);
|
|
835
|
+
for (const l of tail)
|
|
836
|
+
log(say(`npm: ${l}`));
|
|
837
|
+
if (code === 0)
|
|
838
|
+
resolve({ ok: true, detail: `npm installed ${version}` });
|
|
839
|
+
else
|
|
840
|
+
resolve({ ok: false, detail: `npm exited with code ${code}` });
|
|
841
|
+
});
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
/* istanbul ignore next — real files */
|
|
845
|
+
function readStateFile(home) {
|
|
846
|
+
try {
|
|
847
|
+
return JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(home, '.looply', exports.STATE_FILE), 'utf8'));
|
|
848
|
+
}
|
|
849
|
+
catch {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
/* istanbul ignore next — real files */
|
|
854
|
+
function writeStateFile(home, state, log) {
|
|
855
|
+
const file = (0, node_path_1.join)(home, '.looply', exports.STATE_FILE);
|
|
856
|
+
try {
|
|
857
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(file), { recursive: true });
|
|
858
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
859
|
+
(0, node_fs_1.writeFileSync)(tmp, JSON.stringify(state, null, 2) + '\n', 'utf8');
|
|
860
|
+
(0, node_fs_1.renameSync)(tmp, file);
|
|
861
|
+
}
|
|
862
|
+
catch (e) {
|
|
863
|
+
log(say(`could not record in ${file} (${e.message})`));
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
/** The first thing serve does: count this start if it is on an unproven update (`recordStart`). */
|
|
867
|
+
/* istanbul ignore next — real files; the decision is recordStart */
|
|
868
|
+
function nodeRecordStart(o) {
|
|
869
|
+
const state = readStateFile(o.home);
|
|
870
|
+
if (state === null)
|
|
871
|
+
return;
|
|
872
|
+
const next = recordStart(state, o.current, Date.now());
|
|
873
|
+
if (next !== null)
|
|
874
|
+
writeStateFile(o.home, next, o.log);
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Serve holds its lock: is this an update that keeps crashing? On a crash loop
|
|
878
|
+
* it puts the previous version back and returns the plan to start serve on it.
|
|
879
|
+
* Null means carry on — including when the rollback could not be done, which is
|
|
880
|
+
* then tried again after a backoff (`afterRollback`).
|
|
881
|
+
*/
|
|
882
|
+
/* istanbul ignore next — real files and npm; the decisions are startupGuard and afterRollback */
|
|
883
|
+
async function nodeStartupCheck(o) {
|
|
884
|
+
const state = readStateFile(o.home);
|
|
885
|
+
if (state === null)
|
|
886
|
+
return null;
|
|
887
|
+
const verdict = startupGuard(state, o.current, Date.now());
|
|
888
|
+
if (verdict.kind === 'nothing')
|
|
889
|
+
return null;
|
|
890
|
+
if (verdict.kind === 'wait') {
|
|
891
|
+
o.log(say(`🔴 ${verdict.why}`));
|
|
892
|
+
return null;
|
|
893
|
+
}
|
|
894
|
+
o.log(say(`🔴 ${verdict.why}; putting ${verdict.to} back`));
|
|
895
|
+
const install = globalInstallOf(o.bin, o.platform, node_fs_1.existsSync);
|
|
896
|
+
const back = install === null
|
|
897
|
+
? { ok: false, detail: 'not a global install' }
|
|
898
|
+
: await npmInstall(install, verdict.to, updateRegistry(o.env), o.platform, o.log);
|
|
899
|
+
const says = back.ok ? await binaryVersion(o.bin) : null;
|
|
900
|
+
const done = says === verdict.to;
|
|
901
|
+
const after = afterRollback(state, o.current, Date.now(), done, verdict.why);
|
|
902
|
+
writeStateFile(o.home, after, o.log);
|
|
903
|
+
if (!done) {
|
|
904
|
+
o.log(say(`🔴 could not put ${verdict.to} back (${back.ok ? `the installed looply answers ${says ?? 'nothing'}` : back.detail}); trying again at ${after.pendingVerify?.rollbackRetryAt ?? 'the next start'}`));
|
|
905
|
+
return null;
|
|
906
|
+
}
|
|
907
|
+
return { said: say(`${verdict.to} is back after ${verdict.why}; starting serve on it`), plan: { expect: verdict.to } };
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Start the replacement supervisor once this one has let go of its lock, and
|
|
911
|
+
* stay until it has come up. If it does not, put `plan.rollbackTo` back and
|
|
912
|
+
* start that instead, so a release that crashes at startup does not leave the
|
|
913
|
+
* PC without a supervisor.
|
|
914
|
+
*
|
|
915
|
+
* Under a service manager (`LOOPLY_SERVE_MANAGED=1`) serve exits
|
|
916
|
+
* `EXIT_RESTART` and the manager starts the new one; the binary was already
|
|
917
|
+
* checked with `--version` before getting here.
|
|
918
|
+
*/
|
|
919
|
+
/* istanbul ignore next — real processes */
|
|
920
|
+
async function relaunchServe(o) {
|
|
921
|
+
const manager = o.manager;
|
|
922
|
+
if (manager.managed) {
|
|
923
|
+
return {
|
|
924
|
+
exitCode: exports.EXIT_RESTART,
|
|
925
|
+
said: `serve: exiting so the service manager starts the updated supervisor${manager.hint === null ? '' : ` (${manager.hint})`}`,
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
if (o.bin === null)
|
|
929
|
+
return { exitCode: 0, said: "serve: cannot find this CLI's bin.js to start again — start `looply serve` by hand" };
|
|
930
|
+
const start = () => {
|
|
931
|
+
let fd = null;
|
|
932
|
+
try {
|
|
933
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(o.log), { recursive: true });
|
|
934
|
+
fd = (0, node_fs_1.openSync)(o.log, 'a');
|
|
935
|
+
}
|
|
936
|
+
catch {
|
|
937
|
+
fd = null;
|
|
938
|
+
}
|
|
939
|
+
try {
|
|
940
|
+
const child = (0, node_child_process_1.spawn)(o.node, [o.bin, 'serve', '--no-color'], {
|
|
941
|
+
detached: true,
|
|
942
|
+
windowsHide: true,
|
|
943
|
+
stdio: fd === null ? 'ignore' : ['ignore', fd, fd],
|
|
944
|
+
});
|
|
945
|
+
child.unref();
|
|
946
|
+
return child.pid ?? null;
|
|
947
|
+
}
|
|
948
|
+
catch {
|
|
949
|
+
return null;
|
|
950
|
+
}
|
|
951
|
+
finally {
|
|
952
|
+
if (fd !== null) {
|
|
953
|
+
try {
|
|
954
|
+
(0, node_fs_1.closeSync)(fd);
|
|
955
|
+
}
|
|
956
|
+
catch {
|
|
957
|
+
/* the child holds its own copy */
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
const checks = {
|
|
963
|
+
lockPid: o.lockPid,
|
|
964
|
+
alive: (pid) => {
|
|
965
|
+
try {
|
|
966
|
+
process.kill(pid, 0);
|
|
967
|
+
return true;
|
|
968
|
+
}
|
|
969
|
+
catch (e) {
|
|
970
|
+
return e.code === 'EPERM';
|
|
971
|
+
}
|
|
972
|
+
},
|
|
973
|
+
now: () => Date.now(),
|
|
974
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
975
|
+
};
|
|
976
|
+
const pid = start();
|
|
977
|
+
if (pid !== null && (await confirmServeStarted(pid, checks))) {
|
|
978
|
+
return { exitCode: 0, said: `serve: the supervisor on ${o.plan.expect} is up (pid ${pid}), writing to ${o.log}` };
|
|
979
|
+
}
|
|
980
|
+
// One that is alive but never took the lock is hung: it goes, so the next one can.
|
|
981
|
+
if (pid !== null && checks.alive(pid)) {
|
|
982
|
+
try {
|
|
983
|
+
process.kill(pid);
|
|
984
|
+
}
|
|
985
|
+
catch {
|
|
986
|
+
/* gone already */
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
if (o.plan.rollbackTo === undefined) {
|
|
990
|
+
return { exitCode: 1, said: 'serve: the new supervisor did not come up — start `looply serve` by hand' };
|
|
991
|
+
}
|
|
992
|
+
// 🔴 RECORDED, so this version waits RETRY_FAILED_AFTER_MS like any failed
|
|
993
|
+
// install. Without it lastAttempt still said OK, and the next check put the
|
|
994
|
+
// same broken version back every half hour.
|
|
995
|
+
const state = readStateFile(o.home) ?? {};
|
|
996
|
+
writeStateFile(o.home, relaunchFailed(state, o.plan.expect, Date.now(), `serve on ${o.plan.expect} did not come up after the update`), o.say);
|
|
997
|
+
o.say(say(`🔴 serve on ${o.plan.expect} did not come up; putting ${o.plan.rollbackTo} back, and not trying ${o.plan.expect} again for ${exports.RETRY_FAILED_AFTER_MS / 3_600_000} hours`));
|
|
998
|
+
const install = globalInstallOf(o.bin, o.platform, node_fs_1.existsSync);
|
|
999
|
+
const back = install === null ? { ok: false, detail: 'not a global install' } : await npmInstall(install, o.plan.rollbackTo, updateRegistry(o.env), o.platform, o.say);
|
|
1000
|
+
const again = back.ok ? start() : null;
|
|
1001
|
+
if (again !== null && (await confirmServeStarted(again, checks))) {
|
|
1002
|
+
return { exitCode: 0, said: `serve: ${o.plan.expect} did not start; ${o.plan.rollbackTo} is back and its supervisor is up (pid ${again})` };
|
|
1003
|
+
}
|
|
1004
|
+
return { exitCode: 1, said: `serve: 🔴 neither ${o.plan.expect} nor ${o.plan.rollbackTo} came up (${back.detail}) — this PC needs a look` };
|
|
1005
|
+
}
|
|
1006
|
+
//# sourceMappingURL=self-update.js.map
|