benignswarm 0.12.107 → 0.12.108

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.
@@ -1,4 +1,4 @@
1
- import { readdirSync, readlinkSync, realpathSync, readFileSync } from "node:fs";
1
+ import { readdirSync, readlinkSync, realpathSync, readFileSync, statSync } from "node:fs";
2
2
  import { spawnSync } from "node:child_process";
3
3
  import { isAbsolute, relative, sep } from "node:path";
4
4
  const run = (command, args) => {
@@ -269,6 +269,57 @@ function linuxListenersForPid(pid) {
269
269
  }
270
270
  return [...new Set(out)];
271
271
  }
272
+ function readLinuxProcessOrigin(pid, ppid) {
273
+ let parentUid = null;
274
+ let cgroup = null;
275
+ try {
276
+ parentUid = statSync(`/proc/${ppid}`).uid;
277
+ }
278
+ catch { /* unknown stays unknown */ }
279
+ try {
280
+ cgroup = readFileSync(`/proc/${pid}/cgroup`, "utf8").trim();
281
+ }
282
+ catch { /* unknown stays unknown */ }
283
+ return { parentPid: ppid, parentUid, cgroup };
284
+ }
285
+ /** True when the process no longer runs: /proc/<pid> is gone, or its state is Z (zombie) or X (dead). */
286
+ function linuxProcessEnded(pid) {
287
+ let stat;
288
+ try {
289
+ stat = readFileSync(`/proc/${pid}/stat`, "utf8");
290
+ }
291
+ catch (error) {
292
+ return isDisappearedProcess(error);
293
+ }
294
+ const state = stat.slice(stat.lastIndexOf(")") + 2, stat.lastIndexOf(")") + 3);
295
+ return state === "Z" || state === "X";
296
+ }
297
+ /** User services a host runs for its user that are allowed to stay uninspected. Named, never by pattern. */
298
+ const UNINSPECTABLE_USER_SERVICES = new Set(["gpg-agent.service", "ssh-agent.service"]);
299
+ /**
300
+ * Why an uninspectable same-user process may be left out of a worktree scan, or null when it must fail
301
+ * the scan. The operator's rule (WU #472, 2026-11-17) allows exactly three exclusions:
302
+ * - a child of a root-owned process other than PID 1: login and session leaders (sshd-session).
303
+ * - a member of the user manager's init.scope: systemd --user and sd-pam.
304
+ * - a member of a named user service in UNINSPECTABLE_USER_SERVICES.
305
+ * Linux cannot prove where a non-dumpable process works, so these are a policy, not a proof. Accepted
306
+ * residual risk: a root-owned supervisor or subreaper that adopts or launches same-user work inside a
307
+ * checkout would pass the first rule; BenignSwarm hosts run no such supervisor for agent work.
308
+ * Unknown facts, PID 1 parents, transient run-* units and org.benignswarm.* units all return null.
309
+ */
310
+ export function uninspectableExclusion(o) {
311
+ if (o.parentUid === 0 && o.parentPid > 1)
312
+ return "child of a root-owned daemon (login session leader)";
313
+ const unified = o.cgroup?.split("\n").find((l) => l.startsWith("0::"))?.slice(3) ?? null;
314
+ if (!unified)
315
+ return null;
316
+ if (/\/user@\d+\.service\/init\.scope$/.test(unified))
317
+ return "the user's systemd manager";
318
+ const unit = /\/user@\d+\.service\/app\.slice\/([^/]+\.service)$/.exec(unified)?.[1];
319
+ if (unit && UNINSPECTABLE_USER_SERVICES.has(unit))
320
+ return `user service ${unit}`;
321
+ return null;
322
+ }
272
323
  function readLinuxProcessPaths(rows, worktree) {
273
324
  const cwd = new Map();
274
325
  const listeners = new Map();
@@ -278,8 +329,21 @@ function readLinuxProcessPaths(rows, worktree) {
278
329
  cwd.set(row.pid, readlinkSync(`/proc/${row.pid}/cwd`));
279
330
  }
280
331
  catch (error) {
281
- if (!isDisappearedProcess(error))
282
- throw new Error(`cannot inspect working directory for process ${row.pid}: ${error.message}`);
332
+ if (isDisappearedProcess(error))
333
+ continue;
334
+ // The rows are this user's own processes (ps -u). The kernel denies /proc/<pid>/cwd (EACCES) for a
335
+ // non-dumpable process even to its own user. Non-dumpable is a setting any process may take, so it
336
+ // proves nothing about where the process works. Only the exclusions in uninspectableExclusion (the
337
+ // operator's rule) are left out; any other uninspectable process fails the scan (WU #472).
338
+ if (error.code === "EACCES") {
339
+ const excluded = uninspectableExclusion(readLinuxProcessOrigin(row.pid, row.parent_pid));
340
+ if (excluded)
341
+ continue;
342
+ // A process that exited while being scanned (gone, or a zombie awaiting its parent) is not working anywhere.
343
+ if (linuxProcessEnded(row.pid))
344
+ continue;
345
+ }
346
+ throw new Error(`cannot inspect working directory for process ${row.pid}: ${error.message}`);
283
347
  }
284
348
  }
285
349
  for (const row of rows) {
@@ -298,7 +362,13 @@ export function scanWorktreeProcesses(worktree, scope, runner = run, sampledAt =
298
362
  const uid = process.getuid?.();
299
363
  if (uid === undefined)
300
364
  throw new Error(`host process inspection is unsupported on ${process.platform}`);
301
- const ps = runner("ps", ["-U", String(uid), "-ww", "-axo", "pid=,ppid=,lstart=,command="]);
365
+ // Only this user's processes. On Linux (procps) "-U uid -ax" is a union that lists every process on the
366
+ // host, and reading another user's /proc/<pid>/cwd fails with EACCES, which made every scan throw and
367
+ // every stopped session's turn "unknown" (WU #472); "-u uid" alone selects by effective user there.
368
+ // BSD ps on macOS needs -ax for processes without a terminal, and -U already restricts it to the user.
369
+ const ps = runner("ps", process.platform === "linux"
370
+ ? ["-u", String(uid), "-ww", "-o", "pid=,ppid=,lstart=,command="]
371
+ : ["-U", String(uid), "-ww", "-axo", "pid=,ppid=,lstart=,command="]);
302
372
  if (ps.status !== 0)
303
373
  throw new Error(`could not inspect the host process table: ${ps.stderr.trim() || `ps exited ${ps.status}`}`);
304
374
  const rows = parsePsProcessRows(ps.stdout);
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.107";
1
+ export const CLI_VERSION = "0.12.108";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.107",
3
+ "version": "0.12.108",
4
4
  "description": "b9s: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {