benignswarm 0.12.85 → 0.12.86

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.
@@ -3,7 +3,7 @@ import { accessSync, constants, existsSync, readFileSync, readdirSync, realpathS
3
3
  import { delimiter, join } from "node:path";
4
4
  import { bool } from "../args.js";
5
5
  import { CliError } from "../errors.js";
6
- import { binVersion, ensureInstall, latestPublished, lingerEnabled, readInstall, restartWatcher, watcherServices } from "../install.js";
6
+ import { binVersion, ensureInstall, formatWatcherRestartReport, latestPublished, lingerEnabled, readInstall, restartWatcherServices, watcherServices } from "../install.js";
7
7
  import { CLI_VERSION } from "../version.js";
8
8
  import { refreshInstalledHooks } from "../harness.js";
9
9
  /**
@@ -26,24 +26,25 @@ export async function install(p, out) {
26
26
  lines.push(`(was ${prev.bin}; run b9s doctor to find hooks and watchers still pointing there)`);
27
27
  const restarted = [];
28
28
  const elsewhere = [];
29
+ const notStarted = [];
29
30
  if (!bool(p.flags, "no-restart")) {
31
+ const matching = [];
30
32
  for (const w of watcherServices()) {
31
33
  const program = w.args?.[0];
32
34
  if (program !== rec.bin) {
33
35
  elsewhere.push(`${w.label} runs ${program ?? "(unreadable definition)"}`);
34
36
  continue;
35
37
  }
36
- if (restartWatcher(w))
37
- restarted.push(w.label);
38
- else
39
- elsewhere.push(`${w.label} is not loaded (b9s doctor shows how to load it)`);
38
+ matching.push(w);
40
39
  }
40
+ const report = await restartWatcherServices(matching);
41
+ restarted.push(...report.restarted);
42
+ lines.push(...formatWatcherRestartReport(report));
43
+ notStarted.push(...report.not_started.map((failure) => `${failure.label}: ${failure.reason}`));
41
44
  }
42
- if (restarted.length)
43
- lines.push(`Restarted ${restarted.length} watcher${restarted.length === 1 ? "" : "s"} on it: ${restarted.map((l) => l.replace("org.benignswarm.watch.", "")).join(", ")}`);
44
45
  if (elsewhere.length)
45
46
  lines.push(`Not restarted:\n ${elsewhere.join("\n ")}`);
46
- out.print(lines.join("\n"), { install: rec, restarted, not_restarted: elsewhere });
47
+ out.print(lines.join("\n"), { install: rec, restarted, not_restarted: [...elsewhere, ...notStarted] });
47
48
  }
48
49
  /**
49
50
  * b9s doctor every b9s on this machine and who uses which: the recorded install, copies on
@@ -113,6 +113,9 @@ export async function watch(p, out) {
113
113
  stoppingCode = code;
114
114
  releaseWatchLock?.();
115
115
  signalStop();
116
+ // The harness command may be awaiting I/O that never settles. The lock is released first,
117
+ // then the watcher exits with the service's signal code; detached turns are separate groups.
118
+ process.exit(code);
116
119
  };
117
120
  if (!once) {
118
121
  // Only one long-running watcher per identity on this machine; a second one exits here.
package/dist/help.js CHANGED
@@ -137,9 +137,10 @@ Agent commands
137
137
  --service (launchd on macOS, systemd on Linux; --launchd is the same). --turn runs each turn
138
138
  itself (--harness codex|claude-code --session <id>); set up by b9s start.
139
139
  Codex: b9s watch --exec "codex queue --thread <id> --message {summary}" --service
140
- b9s update the same as install --latest: upgrade this machine's b9s and restart its watchers
141
- b9s install [<version> | --latest] make or upgrade this machine's one b9s (join does it once) and restart
142
- the watchers on it; hooks, repo instructions and watchers all run its path
140
+ b9s update the same as install --latest: upgrade this machine's b9s, restart watchers
141
+ without waiting on one stop, and report any that did not come up
142
+ b9s install [<version> | --latest] make or upgrade this machine's one b9s (join does it once), restart watchers
143
+ without waiting on one stop, and report any that did not come up
143
144
  b9s doctor every b9s on this machine and who runs which (PATH, npx caches, this repo's
144
145
  hook and instructions, each watcher's plist versus what launchd runs), with fixes
145
146
  b9s codex hooks | bind <thread-id> | sync install b9s's Codex hooks (~/.codex/hooks.json) and say which thread is you
package/dist/install.js CHANGED
@@ -260,6 +260,84 @@ export function restartWatcher(s) {
260
260
  return restartLaunchAgent(s.label);
261
261
  return spawnSync("systemctl", ["--user", "restart", `${s.label}.service`], { stdio: "ignore" }).status === 0;
262
262
  }
263
+ /** Restart request accepted; on systemd the stop/start job runs without blocking this process. */
264
+ export function requestWatcherRestart(s) {
265
+ if (s.kind === "launchd")
266
+ return restartLaunchAgent(s.label);
267
+ return spawnSync("systemctl", ["--user", "--no-block", "restart", `${s.label}.service`], { stdio: "ignore" }).status === 0;
268
+ }
269
+ function watcherPid(service) {
270
+ if (service.kind === "systemd") {
271
+ const r = spawnSync("systemctl", ["--user", "show", "--property=MainPID", "--value", `${service.label}.service`], { encoding: "utf8" });
272
+ const pid = Number((r.stdout ?? "").trim());
273
+ return r.status === 0 && Number.isInteger(pid) && pid > 0 ? pid : null;
274
+ }
275
+ const output = spawnSync("launchctl", ["list", service.label], { encoding: "utf8" }).stdout ?? "";
276
+ const pid = Number(/"PID" = (\d+);/.exec(output)?.[1]);
277
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
278
+ }
279
+ const watcherRestartDriver = {
280
+ pid: watcherPid,
281
+ active: serviceActive,
282
+ request: requestWatcherRestart,
283
+ now: Date.now,
284
+ wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
285
+ };
286
+ /**
287
+ * Queue every restart before polling any service. A single slow stop therefore cannot keep later
288
+ * watchers on the old version. A changed MainPID/PID proves that each previously-running watcher
289
+ * came back; previously-stopped services count once they become active.
290
+ */
291
+ export async function restartWatcherServices(services, driver = watcherRestartDriver, timeoutMs = 5_000, pollMs = 100) {
292
+ const pending = new Map();
293
+ const notStarted = [];
294
+ for (const service of services) {
295
+ const previousPid = driver.pid(service);
296
+ let accepted = false;
297
+ try {
298
+ accepted = driver.request(service);
299
+ }
300
+ catch { /* reported below */ }
301
+ if (accepted)
302
+ pending.set(service.label, { service, previousPid });
303
+ else
304
+ notStarted.push({ label: service.label, reason: "restart request failed" });
305
+ }
306
+ const restarted = [];
307
+ const deadline = driver.now() + timeoutMs;
308
+ while (pending.size && driver.now() < deadline) {
309
+ for (const [label, entry] of pending) {
310
+ let running = false;
311
+ try {
312
+ const pid = driver.pid(entry.service);
313
+ running = driver.active(entry.service) && (entry.previousPid === null || (pid !== null && pid !== entry.previousPid));
314
+ }
315
+ catch { /* retry while there is time */ }
316
+ if (running) {
317
+ restarted.push(label);
318
+ pending.delete(label);
319
+ }
320
+ }
321
+ if (pending.size)
322
+ await driver.wait(Math.min(pollMs, Math.max(0, deadline - driver.now())));
323
+ }
324
+ for (const label of pending.keys())
325
+ notStarted.push({ label, reason: `did not come up within ${timeoutMs >= 1000 ? `${timeoutMs / 1000} s` : `${timeoutMs} ms`}` });
326
+ return { restarted, not_started: notStarted };
327
+ }
328
+ /** Lines for the install/update output and its structured JSON equivalent. */
329
+ export function formatWatcherRestartReport(report) {
330
+ const short = (label) => label.replace(WATCH_LABEL_PREFIX, "");
331
+ const total = report.restarted.length + report.not_started.length;
332
+ const completed = report.restarted.length;
333
+ const heading = completed === total
334
+ ? `Restarted ${completed} watcher${completed === 1 ? "" : "s"}`
335
+ : `Restarted ${completed} of ${total} watcher${total === 1 ? "" : "s"}`;
336
+ const lines = [`${heading}${completed ? `: ${report.restarted.map(short).join(", ")}` : "."}`];
337
+ if (report.not_started.length)
338
+ lines.push(`Watchers that did not come up:\n ${report.not_started.map((failure) => `${short(failure.label)}: ${failure.reason}`).join("\n ")}`);
339
+ return lines;
340
+ }
263
341
  /** Whether this user's systemd services run while nobody is logged in (null when it cannot be told). */
264
342
  export function lingerEnabled() {
265
343
  const r = spawnSync("loginctl", ["show-user", userInfo().username, "-p", "Linger", "--value"], { encoding: "utf8" });
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.85";
1
+ export const CLI_VERSION = "0.12.86";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.85",
3
+ "version": "0.12.86",
4
4
  "description": "b9s: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {