@aiwayds/dsh-opentui 0.18.6 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/bin/dsh-opentui.mjs +54 -24
- package/bin/lib/launcher.mjs +29 -2
- package/dist/client/index.js +2625 -1280
- package/dist/client/index.js.map +16 -10
- package/dist/host-launch.mjs +1276 -0
- package/dist/host-launch.mjs.map +19 -0
- package/dist/scripts/idle-reaper.mjs +9 -3
- package/dist/watchdog.mjs +467 -0
- package/dist/watchdog.mjs.map +16 -0
- package/package.json +5 -1
- package/scripts/idle-reaper.mjs +9 -3
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
var __esm = (fn, res, err) => () => {
|
|
2
|
+
if (fn)
|
|
3
|
+
try {
|
|
4
|
+
res = fn(fn = 0);
|
|
5
|
+
} catch (e) {
|
|
6
|
+
err = [e];
|
|
7
|
+
}
|
|
8
|
+
if (err)
|
|
9
|
+
throw err[0];
|
|
10
|
+
return res;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/client/mux-frame.ts
|
|
14
|
+
var init_mux_frame = () => {};
|
|
15
|
+
|
|
16
|
+
// src/client/host-probe.ts
|
|
17
|
+
import WS from "ws";
|
|
18
|
+
var init_host_probe = __esm(() => {
|
|
19
|
+
init_mux_frame();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// src/client/append-system.ts
|
|
23
|
+
var APPEND_SYSTEM_PRESET_ID = "opentui-append-system", APPEND_SYSTEM_ROW_ID;
|
|
24
|
+
var init_append_system = __esm(() => {
|
|
25
|
+
APPEND_SYSTEM_ROW_ID = `preset-${APPEND_SYSTEM_PRESET_ID}`;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// src/client/safe-kill.ts
|
|
29
|
+
import { spawnSync } from "node:child_process";
|
|
30
|
+
function psStat(pid) {
|
|
31
|
+
try {
|
|
32
|
+
const result = spawnSync("ps", ["-p", String(pid), "-o", "stat="], { encoding: "utf8" });
|
|
33
|
+
if (result.error || result.status !== 0)
|
|
34
|
+
return null;
|
|
35
|
+
return (result.stdout ?? "").trim() || null;
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function statIsZombie(stat) {
|
|
41
|
+
return stat.startsWith("Z");
|
|
42
|
+
}
|
|
43
|
+
function defaultPidAlive(pid) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(pid, 0);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
return err.code === "EPERM";
|
|
48
|
+
}
|
|
49
|
+
const stat = psStat(pid);
|
|
50
|
+
if (stat !== null && statIsZombie(stat))
|
|
51
|
+
return false;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
function evidencesAgree(pid, registryPid, hooks) {
|
|
55
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
56
|
+
return false;
|
|
57
|
+
if (registryPid !== pid)
|
|
58
|
+
return false;
|
|
59
|
+
const cmd = hooks.cmdline(pid);
|
|
60
|
+
if (!cmd)
|
|
61
|
+
return false;
|
|
62
|
+
const profiles = [...cmd.matchAll(/--profile[= ]+([^\s]+)/g)].map((m) => m[1]);
|
|
63
|
+
if (profiles.length === 0 || profiles.some((p) => p !== HOST_PROFILE))
|
|
64
|
+
return false;
|
|
65
|
+
if (hooks.hasServerEnv(pid))
|
|
66
|
+
return true;
|
|
67
|
+
if (hooks.envReadable && !hooks.envReadable(pid)) {
|
|
68
|
+
hooks.onEnvFallback?.(pid);
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
async function waitGone(pid, budgetMs, alive, sleep, now) {
|
|
74
|
+
if (!alive(pid))
|
|
75
|
+
return true;
|
|
76
|
+
const deadline = now() + budgetMs;
|
|
77
|
+
const maxIterations = Math.max(1, Math.ceil(budgetMs / DEFAULT_EXIT_POLL_MS) + 8);
|
|
78
|
+
for (let i = 0;i < maxIterations && now() < deadline; i++) {
|
|
79
|
+
await sleep(DEFAULT_EXIT_POLL_MS);
|
|
80
|
+
if (!alive(pid))
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return !alive(pid);
|
|
84
|
+
}
|
|
85
|
+
async function safeStopHost(options) {
|
|
86
|
+
const {
|
|
87
|
+
pid,
|
|
88
|
+
registryPid,
|
|
89
|
+
hooks,
|
|
90
|
+
termGraceMs = DEFAULT_TERM_GRACE_MS,
|
|
91
|
+
killGraceMs = DEFAULT_KILL_GRACE_MS,
|
|
92
|
+
sleep = defaultSleep,
|
|
93
|
+
pidAlive = hooks.pidAlive ?? defaultPidAlive,
|
|
94
|
+
now = Date.now
|
|
95
|
+
} = options;
|
|
96
|
+
if (!evidencesAgree(pid, registryPid, hooks)) {
|
|
97
|
+
return { attempted: false, evidencesOk: false, signals: [], stopped: !pidAlive(pid) };
|
|
98
|
+
}
|
|
99
|
+
const signals = [];
|
|
100
|
+
hooks.kill(pid, "SIGTERM");
|
|
101
|
+
signals.push("SIGTERM");
|
|
102
|
+
if (await waitGone(pid, termGraceMs, pidAlive, sleep, now)) {
|
|
103
|
+
return { attempted: true, evidencesOk: true, signals, stopped: true };
|
|
104
|
+
}
|
|
105
|
+
hooks.kill(pid, "SIGKILL");
|
|
106
|
+
signals.push("SIGKILL");
|
|
107
|
+
const stopped = await waitGone(pid, killGraceMs, pidAlive, sleep, now);
|
|
108
|
+
return { attempted: true, evidencesOk: true, signals, stopped };
|
|
109
|
+
}
|
|
110
|
+
var SERVER_ENV_NAME = "DSH_OPENTUI_SERVER", SERVER_ENV_VALUE = "1", HOST_PROFILE = "opentui", DEFAULT_TERM_GRACE_MS = 5000, DEFAULT_KILL_GRACE_MS = 2000, DEFAULT_EXIT_POLL_MS = 50, psKillHooks, defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
111
|
+
var init_safe_kill = __esm(() => {
|
|
112
|
+
psKillHooks = {
|
|
113
|
+
cmdline(pid) {
|
|
114
|
+
try {
|
|
115
|
+
return spawnSync("ps", ["-p", String(pid), "-o", "command="], { encoding: "utf8" }).stdout?.trim() || null;
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
hasServerEnv(pid) {
|
|
121
|
+
try {
|
|
122
|
+
const out = spawnSync("ps", ["eww", "-p", String(pid), "-o", "command="], { encoding: "utf8" }).stdout ?? "";
|
|
123
|
+
return out.includes(`${SERVER_ENV_NAME}=${SERVER_ENV_VALUE}`);
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
envReadable(pid) {
|
|
129
|
+
try {
|
|
130
|
+
const result = spawnSync("ps", ["eww", "-p", String(pid), "-o", "command="], { encoding: "utf8" });
|
|
131
|
+
if (result.error || result.status !== null && result.status !== 0)
|
|
132
|
+
return false;
|
|
133
|
+
const out = (result.stdout ?? "").trim();
|
|
134
|
+
if (!out || out.includes("<defunct>"))
|
|
135
|
+
return false;
|
|
136
|
+
const stat = psStat(pid);
|
|
137
|
+
if (stat !== null && statIsZombie(stat))
|
|
138
|
+
return false;
|
|
139
|
+
return true;
|
|
140
|
+
} catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
onEnvFallback(pid) {
|
|
145
|
+
console.warn(`[safe-kill] ${SERVER_ENV_NAME} env unreadable for pid ${pid}; ` + `using registry + cmdline two-evidence fallback (no "dsh" match)`);
|
|
146
|
+
},
|
|
147
|
+
kill(pid, signal) {
|
|
148
|
+
try {
|
|
149
|
+
process.kill(pid, signal);
|
|
150
|
+
} catch {}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// src/client/lifecycle.ts
|
|
156
|
+
var DEFAULT_IDLE_EXIT_MS = 5000, DEFAULT_IDLE_EXIT_SEC, registry, pendingBoots, respawnHistory, RETRYABLE_MINT_ERRORS;
|
|
157
|
+
var init_lifecycle = __esm(() => {
|
|
158
|
+
init_host_probe();
|
|
159
|
+
init_append_system();
|
|
160
|
+
init_safe_kill();
|
|
161
|
+
DEFAULT_IDLE_EXIT_SEC = DEFAULT_IDLE_EXIT_MS / 1000;
|
|
162
|
+
registry = new Map;
|
|
163
|
+
pendingBoots = new Map;
|
|
164
|
+
respawnHistory = new Map;
|
|
165
|
+
RETRYABLE_MINT_ERRORS = new Set(["CredentialsFileMissingError", "CredentialsGrantMissingError"]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// src/client/host-registry.ts
|
|
169
|
+
import {
|
|
170
|
+
existsSync,
|
|
171
|
+
linkSync,
|
|
172
|
+
mkdirSync,
|
|
173
|
+
readFileSync,
|
|
174
|
+
renameSync,
|
|
175
|
+
unlinkSync,
|
|
176
|
+
writeFileSync
|
|
177
|
+
} from "node:fs";
|
|
178
|
+
import { join } from "node:path";
|
|
179
|
+
function registryPath(home, profile) {
|
|
180
|
+
return join(home, `opentui-host.${profile}.json`);
|
|
181
|
+
}
|
|
182
|
+
function isPlainObject(value) {
|
|
183
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
184
|
+
}
|
|
185
|
+
function isInteger(value) {
|
|
186
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
187
|
+
}
|
|
188
|
+
function isString(value) {
|
|
189
|
+
return typeof value === "string";
|
|
190
|
+
}
|
|
191
|
+
function nonEmpty(value) {
|
|
192
|
+
return isString(value) && value.trim() !== "";
|
|
193
|
+
}
|
|
194
|
+
function invalid(reason, raw) {
|
|
195
|
+
return { kind: "invalid", raw, reason };
|
|
196
|
+
}
|
|
197
|
+
function validateRegistry(raw) {
|
|
198
|
+
if (!isPlainObject(raw))
|
|
199
|
+
return invalid("registry is not an object", raw);
|
|
200
|
+
if (raw.version === REGISTRY_SCHEMA_VERSION) {
|
|
201
|
+
if (!nonEmpty(raw.profile))
|
|
202
|
+
return invalid("profile must be a non-empty string", raw);
|
|
203
|
+
if (!nonEmpty(raw.home))
|
|
204
|
+
return invalid("home must be a non-empty string", raw);
|
|
205
|
+
if (!isInteger(raw.pid) || raw.pid < 0)
|
|
206
|
+
return invalid("pid must be a non-negative integer", raw);
|
|
207
|
+
if (!isInteger(raw.port) || raw.port < 0 || raw.port > 65535)
|
|
208
|
+
return invalid("port must be an integer in [0, 65535]", raw);
|
|
209
|
+
if (!REGISTRY_PHASES.includes(raw.phase)) {
|
|
210
|
+
return invalid(`phase must be one of ${REGISTRY_PHASES.join("|")}`, raw);
|
|
211
|
+
}
|
|
212
|
+
if (!nonEmpty(raw.startedAt))
|
|
213
|
+
return invalid("startedAt must be a non-empty string", raw);
|
|
214
|
+
if (!nonEmpty(raw.updatedAt))
|
|
215
|
+
return invalid("updatedAt must be a non-empty string", raw);
|
|
216
|
+
if (!isString(raw.authority))
|
|
217
|
+
return invalid("authority must be a string", raw);
|
|
218
|
+
if (!Array.isArray(raw.clients))
|
|
219
|
+
return invalid("clients must be an array", raw);
|
|
220
|
+
const clients = [];
|
|
221
|
+
for (const [index, item] of raw.clients.entries()) {
|
|
222
|
+
if (!isPlainObject(item))
|
|
223
|
+
return invalid(`clients[${index}] must be an object`, raw);
|
|
224
|
+
if (!nonEmpty(item.id))
|
|
225
|
+
return invalid(`clients[${index}].id must be a non-empty string`, raw);
|
|
226
|
+
if (!isInteger(item.pid) || item.pid < 0)
|
|
227
|
+
return invalid(`clients[${index}].pid must be a non-negative integer`, raw);
|
|
228
|
+
if (!nonEmpty(item.lastSeen))
|
|
229
|
+
return invalid(`clients[${index}].lastSeen must be a non-empty string`, raw);
|
|
230
|
+
clients.push({ id: item.id, pid: item.pid, lastSeen: item.lastSeen });
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
kind: "v2",
|
|
234
|
+
value: {
|
|
235
|
+
version: 2,
|
|
236
|
+
profile: raw.profile,
|
|
237
|
+
home: raw.home,
|
|
238
|
+
pid: raw.pid,
|
|
239
|
+
port: raw.port,
|
|
240
|
+
phase: raw.phase,
|
|
241
|
+
startedAt: raw.startedAt,
|
|
242
|
+
updatedAt: raw.updatedAt,
|
|
243
|
+
authority: raw.authority,
|
|
244
|
+
clients
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (raw.version !== undefined)
|
|
249
|
+
return invalid(`unsupported registry version ${String(raw.version)}`, raw);
|
|
250
|
+
if (!isInteger(raw.pid) || !isInteger(raw.port)) {
|
|
251
|
+
return invalid("legacy registry is missing pid/port", raw);
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
kind: "legacy",
|
|
255
|
+
raw: {
|
|
256
|
+
pid: raw.pid,
|
|
257
|
+
port: raw.port,
|
|
258
|
+
...isString(raw.authority) ? { authority: raw.authority } : {},
|
|
259
|
+
...isString(raw.startedAt) ? { startedAt: raw.startedAt } : {},
|
|
260
|
+
...isString(raw.profile) ? { profile: raw.profile } : {},
|
|
261
|
+
...raw.booting === true ? { booting: true } : {}
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function readRegistryAt(path, fs = nodeRegistryFs) {
|
|
266
|
+
let text;
|
|
267
|
+
try {
|
|
268
|
+
text = fs.readFileSync(path, "utf8");
|
|
269
|
+
} catch {
|
|
270
|
+
return { kind: "missing" };
|
|
271
|
+
}
|
|
272
|
+
let raw;
|
|
273
|
+
try {
|
|
274
|
+
raw = JSON.parse(text);
|
|
275
|
+
} catch {
|
|
276
|
+
return invalid("registry file is not valid JSON", text);
|
|
277
|
+
}
|
|
278
|
+
return validateRegistry(raw);
|
|
279
|
+
}
|
|
280
|
+
function readRegistry(home, profile, fs = nodeRegistryFs) {
|
|
281
|
+
return readRegistryAt(registryPath(home, profile), fs);
|
|
282
|
+
}
|
|
283
|
+
var REGISTRY_SCHEMA_VERSION = 2, REGISTRY_PHASES, nodeRegistryFs;
|
|
284
|
+
var init_host_registry = __esm(() => {
|
|
285
|
+
init_lifecycle();
|
|
286
|
+
REGISTRY_PHASES = ["claim", "booting", "ready", "stopping"];
|
|
287
|
+
nodeRegistryFs = {
|
|
288
|
+
existsSync: (path) => existsSync(path),
|
|
289
|
+
mkdirSync: (path, options) => {
|
|
290
|
+
mkdirSync(path, options);
|
|
291
|
+
},
|
|
292
|
+
readFileSync: (path, encoding) => readFileSync(path, encoding),
|
|
293
|
+
writeFileSync: (path, data, options) => {
|
|
294
|
+
writeFileSync(path, data, options);
|
|
295
|
+
},
|
|
296
|
+
renameSync: (from, to) => renameSync(from, to),
|
|
297
|
+
linkSync: (from, to) => linkSync(from, to),
|
|
298
|
+
unlinkSync: (path) => unlinkSync(path)
|
|
299
|
+
};
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// src/client/client-lifecycle.ts
|
|
303
|
+
import {
|
|
304
|
+
existsSync as nodeExistsSync,
|
|
305
|
+
mkdirSync as nodeMkdirSync,
|
|
306
|
+
readFileSync as nodeReadFileSync,
|
|
307
|
+
readdirSync as nodeReaddirSync,
|
|
308
|
+
renameSync as nodeRenameSync,
|
|
309
|
+
unlinkSync as nodeUnlinkSync,
|
|
310
|
+
writeFileSync as nodeWriteFileSync
|
|
311
|
+
} from "node:fs";
|
|
312
|
+
import { join as join2 } from "node:path";
|
|
313
|
+
function clientsDir(home) {
|
|
314
|
+
return join2(home, CLIENTS_DIR_NAME);
|
|
315
|
+
}
|
|
316
|
+
function validateHeartbeat(raw) {
|
|
317
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
318
|
+
return null;
|
|
319
|
+
const r = raw;
|
|
320
|
+
if (typeof r.id !== "string" || r.id.trim() === "")
|
|
321
|
+
return null;
|
|
322
|
+
if (typeof r.pid !== "number" || !Number.isInteger(r.pid) || r.pid < 0)
|
|
323
|
+
return null;
|
|
324
|
+
if (typeof r.lastSeen !== "string" || r.lastSeen.trim() === "")
|
|
325
|
+
return null;
|
|
326
|
+
if (Number.isNaN(Date.parse(r.lastSeen)))
|
|
327
|
+
return null;
|
|
328
|
+
if (typeof r.workspace !== "string")
|
|
329
|
+
return null;
|
|
330
|
+
return { id: r.id, pid: r.pid, lastSeen: r.lastSeen, workspace: r.workspace };
|
|
331
|
+
}
|
|
332
|
+
function isFresh(hb, nowMs, ttlMs) {
|
|
333
|
+
const seen = Date.parse(hb.lastSeen);
|
|
334
|
+
if (Number.isNaN(seen))
|
|
335
|
+
return false;
|
|
336
|
+
const age = nowMs - seen;
|
|
337
|
+
return age >= 0 && age <= ttlMs;
|
|
338
|
+
}
|
|
339
|
+
function resolveGraceMs(explicitMs, env) {
|
|
340
|
+
if (explicitMs !== undefined && Number.isFinite(explicitMs))
|
|
341
|
+
return Math.max(0, Math.round(explicitMs));
|
|
342
|
+
const raw = (env ?? process.env)[ENV_IDLE_MS];
|
|
343
|
+
if (raw !== undefined && /^\d+$/.test(raw)) {
|
|
344
|
+
const parsed = Number(raw);
|
|
345
|
+
if (Number.isFinite(parsed))
|
|
346
|
+
return Math.round(parsed);
|
|
347
|
+
}
|
|
348
|
+
return DEFAULT_LAST_ONE_GRACE_MS;
|
|
349
|
+
}
|
|
350
|
+
function listHeartbeats(home, deps = {}) {
|
|
351
|
+
const fs = deps.fs ?? nodeLifecycleFs;
|
|
352
|
+
if (!fs.readdirSync)
|
|
353
|
+
return [];
|
|
354
|
+
let names;
|
|
355
|
+
try {
|
|
356
|
+
names = fs.readdirSync(clientsDir(home));
|
|
357
|
+
} catch {
|
|
358
|
+
return [];
|
|
359
|
+
}
|
|
360
|
+
const out = [];
|
|
361
|
+
for (const name of names) {
|
|
362
|
+
if (!name.endsWith(".json"))
|
|
363
|
+
continue;
|
|
364
|
+
try {
|
|
365
|
+
const raw = JSON.parse(fs.readFileSync(join2(clientsDir(home), name), "utf8"));
|
|
366
|
+
const hb = validateHeartbeat(raw);
|
|
367
|
+
if (hb)
|
|
368
|
+
out.push(hb);
|
|
369
|
+
} catch {}
|
|
370
|
+
}
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
function freshClients(home, deps = {}, ttlMs = DEFAULT_CLIENT_TTL_MS) {
|
|
374
|
+
const now = deps.now ?? Date.now;
|
|
375
|
+
const nowMs = now();
|
|
376
|
+
return listHeartbeats(home, deps).filter((hb) => isFresh(hb, nowMs, ttlMs));
|
|
377
|
+
}
|
|
378
|
+
function clearRegistryIfPid(home, profile, read, pid, fs) {
|
|
379
|
+
if (read.kind === "v2" && read.value.pid === pid) {
|
|
380
|
+
try {
|
|
381
|
+
fs.unlinkSync(registryPath(home, profile));
|
|
382
|
+
} catch {}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
async function runWatchdog(home, options) {
|
|
386
|
+
const profile = options.profile ?? "opentui";
|
|
387
|
+
const graceMs = resolveGraceMs(options.graceMs);
|
|
388
|
+
if (graceMs === 0)
|
|
389
|
+
return { reclaimed: false, reason: "resident" };
|
|
390
|
+
const pollMs = options.pollMs ?? DEFAULT_WATCHDOG_POLL_MS;
|
|
391
|
+
const sleep = options.sleep ?? defaultSleep2;
|
|
392
|
+
const alive = options.hooks.pidAlive ?? defaultPidAlive;
|
|
393
|
+
const fs = options.fs ?? nodeLifecycleFs;
|
|
394
|
+
for (let tick = 0;options.maxTicks === undefined || tick < options.maxTicks; tick++) {
|
|
395
|
+
const read = readRegistry(home, profile, fs);
|
|
396
|
+
if (read.kind !== "v2" || read.value.pid <= 0 || !alive(read.value.pid)) {
|
|
397
|
+
if (read.kind === "v2" && !alive(read.value.pid)) {
|
|
398
|
+
try {
|
|
399
|
+
fs.unlinkSync(registryPath(home, profile));
|
|
400
|
+
} catch {}
|
|
401
|
+
}
|
|
402
|
+
return { reclaimed: false, reason: "host-gone" };
|
|
403
|
+
}
|
|
404
|
+
const freshCount = freshClients(home, options, options.ttlMs).length;
|
|
405
|
+
options.onTick?.({ tick, freshCount, hostPid: read.value.pid });
|
|
406
|
+
if (freshCount > 0) {
|
|
407
|
+
await sleep(pollMs);
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
await sleep(graceMs);
|
|
411
|
+
const again = readRegistry(home, profile, fs);
|
|
412
|
+
if (again.kind !== "v2" || again.value.pid !== read.value.pid)
|
|
413
|
+
continue;
|
|
414
|
+
if (freshClients(home, options, options.ttlMs).length > 0)
|
|
415
|
+
continue;
|
|
416
|
+
if (!alive(again.value.pid))
|
|
417
|
+
continue;
|
|
418
|
+
const stop = await safeStopHost({
|
|
419
|
+
pid: again.value.pid,
|
|
420
|
+
registryPid: again.value.pid,
|
|
421
|
+
hooks: options.hooks,
|
|
422
|
+
termGraceMs: options.termGraceMs,
|
|
423
|
+
killGraceMs: options.killGraceMs,
|
|
424
|
+
sleep,
|
|
425
|
+
now: options.now,
|
|
426
|
+
pidAlive: alive
|
|
427
|
+
});
|
|
428
|
+
if (!stop.stopped) {
|
|
429
|
+
options.onEvidenceRefused?.({ pid: again.value.pid, evidencesOk: stop.evidencesOk });
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
clearRegistryIfPid(home, profile, again, again.value.pid, fs);
|
|
433
|
+
return { reclaimed: stop.evidencesOk, reason: "reclaimed", stop };
|
|
434
|
+
}
|
|
435
|
+
return { reclaimed: false, reason: "max-ticks" };
|
|
436
|
+
}
|
|
437
|
+
var CLIENTS_DIR_NAME = "opentui-clients", DEFAULT_CLIENT_TTL_MS = 15000, DEFAULT_LAST_ONE_GRACE_MS = 2000, ENV_IDLE_MS = "DSH_OPENTUI_HOST_IDLE_MS", DEFAULT_WATCHDOG_POLL_MS = 1000, ENV_WATCHDOG = "DSH_OPENTUI_WATCHDOG", ENV_WATCHDOG_HOME = "DSH_OPENTUI_WATCHDOG_HOME", ENV_WATCHDOG_PROFILE = "DSH_OPENTUI_WATCHDOG_PROFILE", nodeLifecycleFs, defaultSleep2 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
438
|
+
var init_client_lifecycle = __esm(() => {
|
|
439
|
+
init_host_registry();
|
|
440
|
+
init_safe_kill();
|
|
441
|
+
nodeLifecycleFs = {
|
|
442
|
+
existsSync: (p) => nodeExistsSync(p),
|
|
443
|
+
mkdirSync: (p, o) => nodeMkdirSync(p, o),
|
|
444
|
+
readFileSync: (p, e) => nodeReadFileSync(p, e),
|
|
445
|
+
writeFileSync: (p, d, o) => nodeWriteFileSync(p, d, o),
|
|
446
|
+
unlinkSync: (p) => nodeUnlinkSync(p),
|
|
447
|
+
readdirSync: (p) => nodeReaddirSync(p),
|
|
448
|
+
renameSync: (from, to) => nodeRenameSync(from, to)
|
|
449
|
+
};
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
// src/client/watchdog-cli.ts
|
|
453
|
+
init_client_lifecycle();
|
|
454
|
+
init_safe_kill();
|
|
455
|
+
var home = process.env[ENV_WATCHDOG_HOME];
|
|
456
|
+
var profile = process.env[ENV_WATCHDOG_PROFILE] || "opentui";
|
|
457
|
+
if (process.env[ENV_WATCHDOG] === "1" && home) {
|
|
458
|
+
runWatchdog(home, {
|
|
459
|
+
fs: nodeLifecycleFs,
|
|
460
|
+
profile,
|
|
461
|
+
hooks: psKillHooks,
|
|
462
|
+
onEvidenceRefused: ({ pid, evidencesOk }) => console.error(`[watchdog] reclaim refused for host pid ${pid} (evidencesOk=${evidencesOk}); ` + `registry record kept, retrying next tick`)
|
|
463
|
+
}).then(() => process.exit(0)).catch(() => process.exit(0));
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
//# debugId=252FD7B979E3851764756E2164756E21
|
|
467
|
+
//# sourceMappingURL=watchdog.mjs.map
|