@ouro.bot/cli 0.1.0-alpha.1403 → 0.1.0-alpha.1405

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.json CHANGED
@@ -1,6 +1,13 @@
1
1
  {
2
2
  "_note": "Each pull request writes its own change note under changes/; the release job on main gives each landed change its version and adds the notes here (scripts/release-version.cjs). On main this file stops at the last version set by hand; the published package has every release. Agents read this file directly via read_file to understand what changed between versions.",
3
3
  "versions": [
4
+ {
5
+ "version": "0.1.0-alpha.1405",
6
+ "changes": [
7
+ "The Ouro AX census runs ouro-ui v1.3.465's census, which adds the declared-tools walker for bundles that ship a tools.json; the harness's own counts don't change.",
8
+ "When the daemon has to SIGKILL a stuck worker, what that worker started goes too: sync's detached git commands and their ssh get SIGTERM, then SIGKILL after a second, so none of it runs on beside the replacement."
9
+ ]
10
+ },
4
11
  {
5
12
  "version": "0.1.0-alpha.1403",
6
13
  "changes": [
@@ -1,5 +1,5 @@
1
1
  {
2
- "runtimeVersion": "0.1.0-alpha.1403",
2
+ "runtimeVersion": "0.1.0-alpha.1405",
3
3
  "bundleSchemaVersion": 3,
4
4
  "lastUpdated": "2026-09-03T00:00:00.000Z"
5
5
  }
@@ -1,7 +1,7 @@
1
1
  <?xml version="1.0"?>
2
2
  <Container version="2">
3
3
  <Name>ouro-butler</Name>
4
- <Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.1403</Repository>
4
+ <Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.1405</Repository>
5
5
  <Registry>https://github.com/ourostack/ouroboros/pkgs/container/ouroboros-butler</Registry>
6
6
  <Network>host</Network>
7
7
  <Shell>sh</Shell>
@@ -39,6 +39,7 @@ const crypto_1 = require("crypto");
39
39
  const path = __importStar(require("path"));
40
40
  const identity_1 = require("../identity");
41
41
  const runtime_1 = require("../../nerves/runtime");
42
+ const process_tree_1 = require("./process-tree");
42
43
  function startOfHour(ms) {
43
44
  return ms - 60 * 60 * 1000;
44
45
  }
@@ -66,6 +67,9 @@ class DaemonProcessManager {
66
67
  initialBackoffMs;
67
68
  maxBackoffMs;
68
69
  spawnFn;
70
+ listProcessesFn;
71
+ killProcessFn;
72
+ reapGraceMs;
69
73
  now;
70
74
  setTimeoutFn;
71
75
  clearTimeoutFn;
@@ -153,6 +157,9 @@ class DaemonProcessManager {
153
157
  this.stopTimeoutMs = options.stopTimeoutMs ?? 4_000;
154
158
  this.killTimeoutMs = options.killTimeoutMs ?? 2_000;
155
159
  this.spawnFn = options.spawn ?? ((command, args, spawnOptions) => (0, child_process_1.spawn)(command, args, spawnOptions));
160
+ this.listProcessesFn = options.listProcesses ?? (options.spawn ? () => [] : process_tree_1.listProcesses);
161
+ this.killProcessFn = options.killProcess ?? (options.spawn ? () => { } : process_tree_1.sendSignal);
162
+ this.reapGraceMs = options.reapGraceMs ?? 1_000;
156
163
  this.now = options.now ?? (() => Date.now());
157
164
  this.setTimeoutFn = options.setTimeoutFn ?? ((cb, delay) => setTimeout(cb, delay));
158
165
  this.clearTimeoutFn = options.clearTimeoutFn ?? ((timer) => clearTimeout(timer));
@@ -483,7 +490,19 @@ class DaemonProcessManager {
483
490
  const child = state.process;
484
491
  const childPid = child.pid ?? null;
485
492
  const childPidLabel = childPid === null ? "unknown" : String(childPid);
486
- await new Promise((resolve, reject) => {
493
+ // Set when what the worker started is being reaped: the stop is not over until its SIGKILL pass has run, so a
494
+ // daemon that exits right after stopping, or a replacement starting at once, never races what is left.
495
+ let reaping = null;
496
+ try {
497
+ await this.signalWorkerToStop(agent, state, child, childPid, childPidLabel, (pass) => { reaping = pass; });
498
+ }
499
+ finally {
500
+ if (reaping)
501
+ await reaping;
502
+ }
503
+ }
504
+ signalWorkerToStop(agent, state, child, childPid, childPidLabel, onReaping) {
505
+ return new Promise((resolve, reject) => {
487
506
  let settled = false;
488
507
  let timeout = null;
489
508
  const finish = (error) => {
@@ -558,6 +577,9 @@ class DaemonProcessManager {
558
577
  return;
559
578
  // A worker stuck past its own shutdown grace (a turn that never ends, a blocked event loop) would otherwise
560
579
  // stay alive with no replacement. SIGKILL ends it; the replacement replays whatever it left pending.
580
+ // Read what the worker started before it dies: once it is gone its children belong to launchd, and sync's
581
+ // detached git groups (with their ssh) would outlive it.
582
+ const tree = childPid === null ? null : (0, process_tree_1.processTreeTargets)(childPid, this.listProcessesFn());
561
583
  let killAccepted = false;
562
584
  try {
563
585
  killAccepted = child.kill("SIGKILL");
@@ -565,6 +587,27 @@ class DaemonProcessManager {
565
587
  catch {
566
588
  killAccepted = false;
567
589
  }
590
+ if (killAccepted && tree) {
591
+ // SIGTERM first: a child that relays signals (sudo, to the ssh it runs) can only pass that one on. Whatever
592
+ // is still there after the grace gets SIGKILL.
593
+ const reaped = (0, process_tree_1.killProcessTree)(tree, childPid, this.killProcessFn, "SIGTERM");
594
+ if (reaped.groups + reaped.processes > 0) {
595
+ (0, runtime_1.emitNervesEvent)({
596
+ level: "warn",
597
+ component: "daemon",
598
+ event: "daemon.agent_stop_reaped_children",
599
+ message: "sent SIGTERM to what the stuck worker started, and SIGKILL after a grace, so none of it outlives the worker",
600
+ meta: { agent, pid: childPid, groups: reaped.groups, processes: reaped.processes, graceMs: this.reapGraceMs },
601
+ });
602
+ onReaping(new Promise((done) => {
603
+ this.setTimeoutFn(() => {
604
+ // Only what is still the worker's: a pid or group freed since the first read may belong to anyone.
605
+ (0, process_tree_1.killProcessTree)((0, process_tree_1.targetsStillInTree)(tree, this.listProcessesFn()), childPid, this.killProcessFn, "SIGKILL");
606
+ done();
607
+ }, this.reapGraceMs);
608
+ }));
609
+ }
610
+ }
568
611
  (0, runtime_1.emitNervesEvent)({
569
612
  level: "warn",
570
613
  component: "daemon",
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listProcesses = listProcesses;
4
+ exports.sendSignal = sendSignal;
5
+ exports.processTreeTargets = processTreeTargets;
6
+ exports.targetsStillInTree = targetsStillInTree;
7
+ exports.killProcessTree = killProcessTree;
8
+ const node_child_process_1 = require("node:child_process");
9
+ const runtime_1 = require("../../nerves/runtime");
10
+ /** The process table, from `ps`. Empty when it cannot be read. */
11
+ function listProcesses() {
12
+ let output;
13
+ try {
14
+ output = (0, node_child_process_1.execFileSync)("ps", ["-A", "-o", "pid=,ppid=,pgid="], { encoding: "utf-8", timeout: 2_000, killSignal: "SIGKILL" });
15
+ }
16
+ catch (error) {
17
+ (0, runtime_1.emitNervesEvent)({
18
+ level: "warn",
19
+ component: "daemon",
20
+ event: "daemon.process_table_unreadable",
21
+ message: "could not read the process table, so what a stopped worker started is not reaped",
22
+ meta: { error: String(error) },
23
+ });
24
+ return [];
25
+ }
26
+ return output.split("\n").flatMap((line) => {
27
+ const [pid, ppid, pgid] = line.trim().split(/\s+/).map(Number);
28
+ return Number.isInteger(pid) && Number.isInteger(ppid) && Number.isInteger(pgid) ? [{ pid: pid, ppid: ppid, pgid: pgid }] : [];
29
+ });
30
+ }
31
+ /** Sends a signal to a process, or to a process group when the pid is negative. Throws when it is gone. */
32
+ function sendSignal(pid, signal) {
33
+ process.kill(pid, signal);
34
+ }
35
+ /**
36
+ * The worker and everything below it. A group is killed whole only when its leader is in the tree, so the daemon's
37
+ * own group is never touched; the tree's members in other groups are killed one by one. Init (1) and pid 0 are
38
+ * never named: as a group, -1 means every process and 0 the caller's own group.
39
+ */
40
+ function processTreeTargets(rootPid, rows) {
41
+ if (rootPid <= 1)
42
+ return { groups: [], pids: [], members: [] };
43
+ const tree = new Set([rootPid]);
44
+ let grew = true;
45
+ while (grew) {
46
+ grew = false;
47
+ for (const row of rows) {
48
+ if (!tree.has(row.pid) && tree.has(row.ppid)) {
49
+ tree.add(row.pid);
50
+ grew = true;
51
+ }
52
+ }
53
+ }
54
+ const groupOf = new Map(rows.map((row) => [row.pid, row.pgid]));
55
+ const groups = new Set();
56
+ const pids = [];
57
+ for (const pid of tree) {
58
+ if (pid <= 1)
59
+ continue;
60
+ const pgid = groupOf.get(pid) ?? -1;
61
+ if (pgid > 1 && tree.has(pgid))
62
+ groups.add(pgid);
63
+ else
64
+ pids.push(pid);
65
+ }
66
+ return {
67
+ groups: [...groups].sort((a, b) => a - b),
68
+ pids: pids.sort((a, b) => a - b),
69
+ members: rows.filter((row) => tree.has(row.pid)),
70
+ };
71
+ }
72
+ /**
73
+ * The targets that are still what they were, from a fresh read of the table, so a later pass never signals a pid or
74
+ * group the system has since handed to something else. A group counts while it holds a member's child (git forks ssh
75
+ * after the first read), or a member that launchd took over when its parent died. A process counts while it keeps its
76
+ * group and its parent is a member or launchd. A pid reused within the grace by a child of launchd would still pass;
77
+ * macOS hands out pids in order, so that needs the whole pid range to wrap in about a second.
78
+ */
79
+ function targetsStillInTree(targets, rows) {
80
+ const memberPids = new Set(targets.members.map((member) => member.pid));
81
+ const groupOf = new Map(targets.members.map((member) => [member.pid, member.pgid]));
82
+ const stillOurs = (row) => memberPids.has(row.ppid) || (row.ppid === 1 && memberPids.has(row.pid));
83
+ const groups = targets.groups.filter((group) => rows.some((row) => row.pgid === group && stillOurs(row)));
84
+ const pids = targets.pids.filter((pid) => rows.some((row) => row.pid === pid && row.pgid === groupOf.get(pid) && (row.ppid === 1 || memberPids.has(row.ppid))));
85
+ return { groups, pids, members: targets.members };
86
+ }
87
+ /**
88
+ * Signal every target except `skipPid` (already signalled). A process that is already gone is not an error. Returns
89
+ * how many groups and processes were signalled.
90
+ */
91
+ function killProcessTree(targets, skipPid, kill, signal) {
92
+ const signalled = { groups: 0, processes: 0 };
93
+ const send = (pid) => {
94
+ try {
95
+ kill(pid, signal);
96
+ return true;
97
+ }
98
+ catch {
99
+ return false;
100
+ }
101
+ };
102
+ for (const group of targets.groups)
103
+ if (send(-group))
104
+ signalled.groups += 1;
105
+ for (const pid of targets.pids)
106
+ if (pid !== skipPid && send(pid))
107
+ signalled.processes += 1;
108
+ return signalled;
109
+ }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ouro.bot/cli",
3
- "version": "0.1.0-alpha.1403",
3
+ "version": "0.1.0-alpha.1405",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@ouro.bot/cli",
9
- "version": "0.1.0-alpha.1403",
9
+ "version": "0.1.0-alpha.1405",
10
10
  "bundleDependencies": [
11
11
  "@ouro.bot/friends"
12
12
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ouro.bot/cli",
3
- "version": "0.1.0-alpha.1403",
3
+ "version": "0.1.0-alpha.1405",
4
4
  "description": "Ouroboros, the agent harness: agents with durable memory that live where you talk (terminal, Teams, iMessage, mail, voice) and use real tools. Start with npx ouro.bot@latest.",
5
5
  "homepage": "https://ouroboros.bot/",
6
6
  "engines": {