@ouro.bot/cli 0.1.0-alpha.855 → 0.1.0-alpha.856

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,12 @@
1
1
  {
2
2
  "_note": "This changelog is maintained as part of the PR/version-bump workflow. Agent-curated, not auto-generated. Agents read this file directly via read_file to understand what changed between versions.",
3
3
  "versions": [
4
+ {
5
+ "version": "0.1.0-alpha.856",
6
+ "changes": [
7
+ "The daemon no longer freezes for minutes after an upgrade. Mailbox watched the whole agent bundle with Node's JavaScript recursive watcher on Linux, which blocked the event loop for about 7 s at every boot and for about 150 s when a host-side chown touched every bundle file, long enough to fail the container healthcheck and get the Butler restarted. Mailbox now watches directories without blocking: async discovery, event callbacks that never touch the filesystem, a catch-up report for new directories, a 3 s cap on event debouncing, and a warning event when watches run out."
8
+ ]
9
+ },
4
10
  {
5
11
  "version": "0.1.0-alpha.855",
6
12
  "changes": [
@@ -1,5 +1,5 @@
1
1
  {
2
- "runtimeVersion": "0.1.0-alpha.855",
2
+ "runtimeVersion": "0.1.0-alpha.856",
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.855</Repository>
4
+ <Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.856</Repository>
5
5
  <Registry>https://github.com/ourostack/ouroboros/pkgs/container/ouroboros-butler</Registry>
6
6
  <Network>host</Network>
7
7
  <Shell>sh</Shell>
@@ -37,9 +37,13 @@ exports.createSseBroadcaster = createSseBroadcaster;
37
37
  exports.createStateChangedBroadcast = createStateChangedBroadcast;
38
38
  exports.createBundleWatcher = createBundleWatcher;
39
39
  const fs = __importStar(require("fs"));
40
+ const path = __importStar(require("path"));
41
+ const runtime_1 = require("../../nerves/runtime");
40
42
  const DEFAULT_BUNDLE_WATCHER_DEPS = {
43
+ platform: process.platform,
41
44
  existsSync: fs.existsSync,
42
- watch: fs.watch,
45
+ watch: (targetPath, options, listener) => fs.watch(targetPath, options, listener),
46
+ readdir: (targetPath) => fs.promises.readdir(targetPath, { withFileTypes: true }),
43
47
  setTimeout,
44
48
  clearTimeout,
45
49
  };
@@ -81,49 +85,188 @@ function createStateChangedBroadcast(sse) {
81
85
  sse.broadcast("state-changed", { at: new Date().toISOString() });
82
86
  };
83
87
  }
88
+ const BUNDLE_WATCH_DEBOUNCE_MS = 500;
89
+ // A continuous event stream must not postpone onChange forever.
90
+ const BUNDLE_WATCH_MAX_WAIT_MS = 3_000;
91
+ const BUNDLE_WATCH_DEGRADED_LOG_INTERVAL_MS = 60_000;
92
+ const BUNDLE_WATCH_SKIPPED_DIRECTORIES = new Set([".git", "node_modules"]);
93
+ function closeQuietly(handle) {
94
+ try {
95
+ handle.close();
96
+ }
97
+ catch {
98
+ // Already closed.
99
+ }
100
+ }
101
+ /**
102
+ * Report "something under the bundles root changed" to Mailbox, debounced.
103
+ *
104
+ * On Linux, Node implements `fs.watch(root, { recursive: true })` in JavaScript: it stats and
105
+ * inotify-watches every file in the tree before returning (7-10 s of blocked event loop for a
106
+ * 12k-file bundle), and it answers every file event on a directory by re-reading that whole
107
+ * directory. A host-side `chown -R` of a bundle holding directories of ~3.5k receipts therefore
108
+ * keeps the daemon's event loop busy for ~150 s, long enough to fail the container healthcheck.
109
+ *
110
+ * So on Linux we watch each directory (not each file) non-recursively. The tree is discovered
111
+ * with async readdir, and event callbacks never touch the filesystem: they only schedule a
112
+ * debounced onChange, plus, for create/delete/rename events, a debounced async re-read of that
113
+ * one directory to pick up added or removed subdirectories. macOS and Windows have a native
114
+ * recursive watcher that is cheap, so they keep it. Symlinked directories are not followed, and
115
+ * .git and node_modules are not watched.
116
+ */
84
117
  function createBundleWatcher(bundlesRoot, onChange, deps = DEFAULT_BUNDLE_WATCHER_DEPS) {
85
- const watchers = [];
118
+ const watchers = new Map();
119
+ const pendingDirectories = new Set();
86
120
  let debounceTimer = null;
87
- const debounceMs = 500;
121
+ let rescanTimer = null;
122
+ let maxWaitTimer = null;
123
+ let rescanRunning = false;
124
+ let stopped = false;
125
+ let resourceExhausted = false;
126
+ let lastDegradedLogAt = Number.NEGATIVE_INFINITY;
127
+ function flushOnChange() {
128
+ if (debounceTimer)
129
+ deps.clearTimeout(debounceTimer);
130
+ if (maxWaitTimer)
131
+ deps.clearTimeout(maxWaitTimer);
132
+ debounceTimer = null;
133
+ maxWaitTimer = null;
134
+ onChange();
135
+ }
88
136
  function debouncedOnChange() {
89
137
  if (debounceTimer)
90
138
  deps.clearTimeout(debounceTimer);
91
- debounceTimer = deps.setTimeout(onChange, debounceMs);
139
+ debounceTimer = deps.setTimeout(flushOnChange, BUNDLE_WATCH_DEBOUNCE_MS);
140
+ if (!maxWaitTimer)
141
+ maxWaitTimer = deps.setTimeout(flushOnChange, BUNDLE_WATCH_MAX_WAIT_MS);
92
142
  }
93
- try {
94
- if (deps.existsSync(bundlesRoot)) {
95
- const watcher = deps.watch(bundlesRoot, { recursive: true }, debouncedOnChange);
96
- watcher.on?.("error", () => {
97
- try {
98
- watcher.close();
99
- }
100
- catch {
101
- // Already closed.
102
- }
103
- const index = watchers.indexOf(watcher);
104
- if (index >= 0)
105
- watchers.splice(index, 1);
143
+ function reportWatchDegraded(targetPath, error) {
144
+ const code = error?.code;
145
+ if (code === "ENOSPC" || code === "EMFILE")
146
+ resourceExhausted = true;
147
+ const now = Date.now();
148
+ if (now - lastDegradedLogAt < BUNDLE_WATCH_DEGRADED_LOG_INTERVAL_MS)
149
+ return;
150
+ lastDegradedLogAt = now;
151
+ const reason = typeof code === "string" ? code : error instanceof Error ? error.message : String(error);
152
+ (0, runtime_1.emitNervesEvent)({
153
+ level: "warn",
154
+ component: "daemon",
155
+ event: "daemon.mailbox_watch_degraded",
156
+ message: `mailbox watch degraded: ${reason} at ${targetPath}`,
157
+ meta: { path: targetPath, reason },
158
+ });
159
+ }
160
+ function watchPath(targetPath, recursive, listener) {
161
+ try {
162
+ const handle = deps.watch(targetPath, { recursive }, listener);
163
+ handle.on?.("error", (error) => {
164
+ closeQuietly(handle);
165
+ if (watchers.get(targetPath) === handle)
166
+ watchers.delete(targetPath);
167
+ if (stopped)
168
+ return;
169
+ reportWatchDegraded(targetPath, error);
106
170
  debouncedOnChange();
107
171
  });
108
- watchers.push(watcher);
172
+ watchers.set(targetPath, handle);
173
+ return true;
174
+ }
175
+ catch (error) {
176
+ // Watching is best-effort; manual broadcasts still keep Mailbox usable.
177
+ reportWatchDegraded(targetPath, error);
178
+ return false;
109
179
  }
110
180
  }
111
- catch {
112
- // Watching is best-effort; manual broadcasts still keep Mailbox usable.
181
+ function unwatchTree(directory) {
182
+ for (const [watched, handle] of watchers) {
183
+ if (watched === directory || watched.startsWith(`${directory}${path.sep}`)) {
184
+ closeQuietly(handle);
185
+ watchers.delete(watched);
186
+ }
187
+ }
188
+ }
189
+ function scheduleRescan(directory) {
190
+ pendingDirectories.add(directory);
191
+ if (rescanTimer)
192
+ return;
193
+ rescanTimer = deps.setTimeout(() => {
194
+ rescanTimer = null;
195
+ void drainRescans();
196
+ }, BUNDLE_WATCH_DEBOUNCE_MS);
197
+ }
198
+ async function drainRescans() {
199
+ if (rescanRunning)
200
+ return;
201
+ rescanRunning = true;
202
+ try {
203
+ while (!stopped && pendingDirectories.size > 0) {
204
+ const [directory] = pendingDirectories;
205
+ pendingDirectories.delete(directory);
206
+ await reconcileDirectory(directory, true);
207
+ }
208
+ }
209
+ finally {
210
+ rescanRunning = false;
211
+ }
212
+ }
213
+ function watchDirectory(directory) {
214
+ return watchPath(directory, false, (eventType) => {
215
+ debouncedOnChange();
216
+ if (eventType === "rename")
217
+ scheduleRescan(directory);
218
+ });
219
+ }
220
+ // catchUp is true for rescans after boot: files written into a directory before its watch existed
221
+ // produced no event, so a newly watched directory reports one change itself.
222
+ async function reconcileDirectory(directory, catchUp) {
223
+ let entries;
224
+ try {
225
+ entries = await deps.readdir(directory);
226
+ }
227
+ catch {
228
+ unwatchTree(directory);
229
+ return;
230
+ }
231
+ if (stopped)
232
+ return;
233
+ const subdirectories = new Set(entries.filter((entry) => entry.isDirectory() && !BUNDLE_WATCH_SKIPPED_DIRECTORIES.has(entry.name)).map((entry) => path.join(directory, entry.name)));
234
+ for (const watched of [...watchers.keys()]) {
235
+ if (path.dirname(watched) === directory && !subdirectories.has(watched))
236
+ unwatchTree(watched);
237
+ }
238
+ for (const subdirectory of subdirectories) {
239
+ if (stopped || resourceExhausted)
240
+ return;
241
+ if (watchers.has(subdirectory) || !watchDirectory(subdirectory))
242
+ continue;
243
+ if (catchUp)
244
+ debouncedOnChange();
245
+ await reconcileDirectory(subdirectory, catchUp);
246
+ }
247
+ }
248
+ if (deps.existsSync(bundlesRoot)) {
249
+ if (deps.platform === "linux") {
250
+ if (watchDirectory(bundlesRoot))
251
+ void reconcileDirectory(bundlesRoot, false);
252
+ }
253
+ else {
254
+ watchPath(bundlesRoot, true, debouncedOnChange);
255
+ }
113
256
  }
114
257
  return {
115
258
  stop() {
259
+ stopped = true;
116
260
  if (debounceTimer)
117
261
  deps.clearTimeout(debounceTimer);
118
- for (const watcher of watchers) {
119
- try {
120
- watcher.close();
121
- }
122
- catch {
123
- // Already closed.
124
- }
125
- }
126
- watchers.length = 0;
262
+ if (maxWaitTimer)
263
+ deps.clearTimeout(maxWaitTimer);
264
+ if (rescanTimer)
265
+ deps.clearTimeout(rescanTimer);
266
+ pendingDirectories.clear();
267
+ for (const handle of watchers.values())
268
+ closeQuietly(handle);
269
+ watchers.clear();
127
270
  },
128
271
  };
129
272
  }
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@ouro.bot/cli",
3
- "version": "0.1.0-alpha.855",
3
+ "version": "0.1.0-alpha.856",
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.855",
9
+ "version": "0.1.0-alpha.856",
10
10
  "dependencies": {
11
11
  "@anthropic-ai/sdk": "^0.78.0",
12
12
  "@azure/identity": "^4.13.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ouro.bot/cli",
3
- "version": "0.1.0-alpha.855",
3
+ "version": "0.1.0-alpha.856",
4
4
  "engines": {
5
5
  "node": ">=22"
6
6
  },