bridex-worker 0.0.0-stage → 0.1.3

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.
Files changed (3) hide show
  1. package/README.md +521 -2
  2. package/dist/cli.js +10136 -0
  3. package/package.json +34 -3
package/README.md CHANGED
@@ -1,3 +1,522 @@
1
- # Temporary Holding Version
1
+ # bridex-worker
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Key your own machine into a [Bridex](https://bridex.app) instance as a
4
+ worker. The instance schedules heavy task runs onto it instead of executing
5
+ them in its own container: the worker keeps a warm clone of the project, cuts
6
+ a worktree for the task branch, runs the agent (Claude Agent SDK for `builtin`
7
+ agents, the agent's CLI for `adapter:cli:*`), pushes the branch, hands the
8
+ artifacts back and runs the task's background jobs on the same machine.
9
+
10
+ Unlike [bridex-satellite](../satellite), nothing on this machine is lent to
11
+ the run: the model key, git token and MCP secrets come from the instance per
12
+ run and live only in that run's environment, inside an OS jail that hides
13
+ your home folder.
14
+
15
+ No inbound connections: the worker holds one outbound WebSocket per
16
+ instance it serves. Works behind NAT. One process can serve several
17
+ instances at once over one pool of slots and one cache.
18
+
19
+ Needs Node 20 or later (22 adds the `node22` cap), git, bwrap on Linux and
20
+ WSL2 (Seatbelt is built into macOS), rclone for the `mount`/`sync` artifact
21
+ modes (optional), and the agent CLIs (`claude`, `codex`, …) on PATH for
22
+ `adapter:cli:*` agents. Plan runs (an agent's `plan_run_start`) also need
23
+ `loopai` on PATH, plus `claude` and/or `codex` for the providers the plan
24
+ uses: loopai starts those CLIs itself.
25
+
26
+ ## Join (once)
27
+
28
+ An admin issues a key in Settings → Workers → Issue key, then on the machine:
29
+
30
+ ```
31
+ npx bridex-worker@latest join https://your-instance.example --key <token>
32
+ ```
33
+
34
+ Without a key, `join <url>` asks an admin to approve this machine in the
35
+ dashboard (`--machine <name>` sets its name). Both write
36
+ `~/.bridex-worker/worker.yaml` (mode 0600; `BRIDEX_WORKER_HOME` moves it) and
37
+ run the readiness checks. `join` with another instance's URL adds it to the
38
+ file — this machine then serves both; the same URL again replaces its key.
39
+
40
+ On a fleet run from a control plane, register the machine in the CP console
41
+ instead (this needs a control plane with worker support — bridex-cp's
42
+ companion change, not released yet); it shows an id and a secret:
43
+
44
+ ```
45
+ npx bridex-worker@latest join-cp https://your-cp.example --id <worker-id> --key <secret>
46
+ ```
47
+
48
+ `join-cp` checks the pair against the CP and writes the `cp` block. The
49
+ instances are assigned in the CP console, not here: the worker asks the CP
50
+ every minute which instances it serves, connects to each with a short-lived
51
+ token the CP signs for it (renewed over the socket before it expires), and
52
+ reports its vitals, slots and per-instance state back every minute.
53
+ Unassigned, an instance is disconnected at the next poll; a machine the CP
54
+ refuses (403) three polls in a row closes every CP connection (with instance
55
+ keys left, it goes on serving those; with none, it stops). An instance
56
+ worker.yaml serves with a key stays on the key: the CP's assignment of it is
57
+ ignored.
58
+
59
+ `--remote-update` (anywhere after `join-cp`) also lets that control plane
60
+ update this machine's worker: it writes `remote_update: on` into the new `cp`
61
+ block and says what the machine still needs (see
62
+ [Remote update](#remote-update)). The consent belongs to that one control
63
+ plane: `join-cp` always writes a new `cp` block, so a later `join-cp` without
64
+ the flag turns it off again. The worker reads worker.yaml only when it
65
+ starts: a worker already running keeps the control plane and the consent it
66
+ started with, and that control plane can still update it, until it is
67
+ restarted. Restart the service after `join-cp` (see [Platforms](#platforms)).
68
+
69
+ ## Run
70
+
71
+ ```
72
+ npx bridex-worker@latest
73
+ ```
74
+
75
+ `run` is the same as no command; `version` (`--version`, `-v`) prints the
76
+ version. From the repository: `pnpm --filter bridex-worker build`, then
77
+ `node worker/dist/cli.js join …` (`doctor`, `run`) in place of
78
+ `npx bridex-worker@latest`.
79
+
80
+ Stop with Ctrl+C: runs in flight are stopped; the instance gives them up
81
+ after its grace and retries them (the next attempt continues from whatever
82
+ the task branch has on origin);
83
+ background jobs keep running and are picked up on the next start. A link
84
+ that drops reconnects on its own; runs keep going and the instance waits for
85
+ the machine up to its grace (120 s by default). A revoked key stops the
86
+ worker's link to that instance for good — issue a new key and join again,
87
+ or delete its entry from worker.yaml to stop serving it. Other instances it
88
+ serves are not affected; the worker stops once nothing is left to serve.
89
+
90
+ ## Check
91
+
92
+ ```
93
+ npx bridex-worker@latest doctor
94
+ ```
95
+
96
+ Each instance reachable and its key valid (one line per instance, with its
97
+ URL), the control plane reachable and how many instances it assigns (with
98
+ `cp` set), jail, detected caps, cache dir writable, artifacts mode and rclone
99
+ (per instance when there are several), and the platform caveats below. `✗`
100
+ is a failed check — `doctor` exits 1 and `join` does not offer to start; at
101
+ run time one on an instance or the CP ends only that link. `!` is a warning.
102
+
103
+ ## worker.yaml
104
+
105
+ One instance (what the first `join` writes):
106
+
107
+ ```yaml
108
+ instance: https://your-instance.example
109
+ machine_key: ${BRIDEX_WORKER_KEY} # ${ENV} works in URLs, keys (cp's too), cache_dir and store
110
+ ```
111
+
112
+ Several, and/or a control plane (at least one of the two):
113
+
114
+ ```yaml
115
+ instances:
116
+ - { url: https://inst-a.example, machine_key: "${KEY_A}" }
117
+ - { url: https://inst-b.example, machine_key: "${KEY_B}", max_slots: 2 }
118
+ cp: { url: https://cp.example, id: wk_7f3a, key: "${BRIDEX_CP_WORKER_KEY}" }
119
+ ```
120
+
121
+ The `cp` block takes one more key, written as a block:
122
+
123
+ ```yaml
124
+ cp:
125
+ url: https://cp.example
126
+ id: wk_7f3a
127
+ key: "${BRIDEX_CP_WORKER_KEY}"
128
+ remote_update: on # off when absent; `join-cp --remote-update` writes it
129
+ ```
130
+
131
+ `remote_update` (`on` / `off`, YAML `true` / `false` work too) lets this
132
+ control plane update the worker on this machine; see
133
+ [Remote update](#remote-update).
134
+
135
+ `instance:` and `instances:` together is an error, as is one URL listed
136
+ twice. `max_slots` caps how many of this machine's slots that instance may
137
+ take (a CP-assigned instance gets its cap from the CP). The rest is
138
+ machine-wide:
139
+
140
+ ```yaml
141
+ slots: auto # runs at once; auto = one per GB of memory, 2–8
142
+ caps: [game-tools] # added to the detected ones
143
+ cache_dir: ~/bridex-worker/cache
144
+ cache_gb: 60 # soft bound; least recently used checkouts go first
145
+ artifacts: auto # mount | sync | rest
146
+ jail: on # off must be written out
147
+ heartbeat_s: 5
148
+ codex_transport: auto # exec
149
+ ```
150
+
151
+ - **Caps** are detected at start: platform and arch, `node22`, `pnpm`, `uv`,
152
+ `chromium` (also Playwright's browser cache — runs get it read-only, with
153
+ `PLAYWRIGHT_BROWSERS_PATH` on it), `gpu`, `ffmpeg`, `blender`,
154
+ `dotnet`, `gdre`, `cli` and `cli:<preset>` for each agent CLI on PATH, `sdk`,
155
+ `plan` when `loopai` is on PATH, `rclone`. An agent's `requires:` and a
156
+ project's `requires:` must be among them for a run to come here. A plan run
157
+ needs `plan` and `cli:<preset>` for each provider its plan uses; install
158
+ `loopai` and restart the worker to advertise it.
159
+ - **Slots and load.** A grant is refused when every slot is taken, free memory
160
+ or the cache disk is below the reserve (3 GB), or the load per core is
161
+ above 1.5 — your own work on the machine comes first. Slots are shared by
162
+ every instance served: each sees the machine's free slots, capped by its
163
+ `max_slots`.
164
+ - **Cache.** `repos/` bare clones, `work/` one checkout per task, `templates/`
165
+ dependency templates (`node_modules`, `.venv`, Playwright's chromium)
166
+ prepared by a warm-up and copied into each checkout (reflink where the
167
+ filesystem has it). Over `cache_gb`, old checkouts are dropped; uncommitted
168
+ changes are saved as a patch into the task's artifacts first, and a checkout
169
+ with a live run or job is never touched. Then the copies of task folders
170
+ (`artifacts/`) whose checkout is gone, once the instance has every file of
171
+ one — the task's next run here downloads it again. Checkouts, artifacts
172
+ folders, run HOMEs and skills sit under a folder per instance (`work/<inst>/…`;
173
+ the one instance of a single-instance worker.yaml keeps the flat layout
174
+ for the life of the cache, so nothing moves when `join` adds a second one,
175
+ straight after an update too); `repos/` and `templates/` are shared — one
176
+ clone per repository, whichever instance asked. A cache from an earlier
177
+ worker release under a worker.yaml edited into a list by hand makes the
178
+ worker refuse to start and name the folder to clear.
179
+ - **codex_transport.** `auto` runs codex agents through `codex app-server`
180
+ when the run's `terminal` is `ask`, so codex can stop and ask for an
181
+ approval; `exec` always uses `codex exec --json`, which cannot.
182
+
183
+ ## Jail
184
+
185
+ `jail: on` (the default) needs bwrap on Linux and WSL2 (`apt install
186
+ bubblewrap`, unprivileged user namespaces enabled) or Seatbelt on macOS. With
187
+ no working jail the worker refuses to start — it never downgrades silently.
188
+ A jailed run sees none of your home folder and writes only its checkout, its
189
+ own HOME, the project's clone and the task's artifacts folder; the clone's
190
+ config and hooks and the checkout's `.git` pointer stay read-only, so nothing
191
+ the run plants there executes when the worker commits and pushes after it.
192
+ The network is not fenced: a run reaches your LAN like any program on the
193
+ machine.
194
+
195
+ `jail: off` runs with your user's rights and says so on every start.
196
+
197
+ ## Artifacts
198
+
199
+ - `rest` — through the instance's API. Always works.
200
+ - `mount` (Linux) — the instance's store bucket mounted with `rclone mount`
201
+ under the cache dir; each run binds only its task folder.
202
+ - `sync` (macOS) — `rclone copy` of the task folder down before and up after
203
+ the run.
204
+
205
+ `auto` picks `mount`/`sync` when rclone is installed, the instance has an
206
+ S3/R2 store bucket, and this machine has its own credentials for it, else
207
+ `rest`. The `store:` block belongs to the first instance of worker.yaml;
208
+ every other instance, and every instance the control plane assigns, uses
209
+ `rest`. The credentials are a `store:` block in worker.yaml or the
210
+ `BRIDEX_WORKER_R2_BUCKET`, `_ENDPOINT`, `_ACCESS_KEY_ID`,
211
+ `_SECRET_ACCESS_KEY` variables — never from the instance:
212
+
213
+ ```yaml
214
+ store:
215
+ bucket: ${BRIDEX_WORKER_R2_BUCKET}
216
+ endpoint: https://<account>.r2.cloudflarestorage.com
217
+ access_key_id: ${BRIDEX_WORKER_R2_ACCESS_KEY_ID}
218
+ secret_access_key: ${BRIDEX_WORKER_R2_SECRET_ACCESS_KEY}
219
+ ```
220
+
221
+ ## Platforms
222
+
223
+ - **macOS** — keep it awake: `sudo pmset -a sleep 0` (or `caffeinate -s`).
224
+ A sleeping Mac drops its runs; doctor warns. Run it under launchd with
225
+ `KeepAlive`, so a worker that ends (see
226
+ [When the worker gets stuck](#when-the-worker-gets-stuck)) is started
227
+ again. A LaunchAgent in `~/Library/LaunchAgents/app.bridex.worker.plist`
228
+ (absolute paths: launchd's `PATH` is `/usr/bin:/bin:/usr/sbin:/sbin`, so
229
+ give it the one the agent CLIs need):
230
+
231
+ ```xml
232
+ <?xml version="1.0" encoding="UTF-8"?>
233
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
234
+ <plist version="1.0">
235
+ <dict>
236
+ <key>Label</key><string>app.bridex.worker</string>
237
+ <key>ProgramArguments</key>
238
+ <array>
239
+ <string>/opt/homebrew/bin/node</string>
240
+ <string>/opt/homebrew/lib/node_modules/bridex-worker/dist/cli.js</string>
241
+ <string>run</string>
242
+ </array>
243
+ <key>EnvironmentVariables</key>
244
+ <dict>
245
+ <key>PATH</key><string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
246
+ </dict>
247
+ <key>KeepAlive</key><true/>
248
+ <key>StandardOutPath</key><string>/Users/you/Library/Logs/bridex-worker.log</string>
249
+ <key>StandardErrorPath</key><string>/Users/you/Library/Logs/bridex-worker.log</string>
250
+ </dict>
251
+ </plist>
252
+ ```
253
+
254
+ Load it with `launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/app.bridex.worker.plist`;
255
+ restart it (after an upgrade, say) with
256
+ `launchctl kickstart -k gui/$(id -u)/app.bridex.worker`; stop it with
257
+ `launchctl bootout gui/$(id -u)/app.bridex.worker` — Ctrl+C does not
258
+ reach it, and launchd starts a killed one again. `KeepAlive` also
259
+ restarts a worker that stopped for good (see [Run](#run)): a revoked one
260
+ is refused again on every start, so unload it or delete the instance's
261
+ entry from worker.yaml. This LaunchAgent starts the CLI that
262
+ `npm i -g bridex-worker` installed, under `KeepAlive`, so it already fits
263
+ [Remote update](#remote-update) (with Homebrew's node at
264
+ `/opt/homebrew`; adjust both paths to `npm prefix -g` on another node).
265
+ - **Linux** — run it as a service (systemd unit with `Restart=always`;
266
+ `docs/DEPLOYMENT.md` → Workers has one that starts `npx` and one that
267
+ starts the global CLI, the one remote update needs).
268
+ - **Windows** — native Windows has no jail and no bash toolchain: install the
269
+ worker inside WSL2 (Ubuntu with systemd), where it is a Linux worker with
270
+ bwrap.
271
+
272
+ ## Remote update
273
+
274
+ A control-plane admin can update the worker on this machine from the CP
275
+ console: the worker stops taking new runs, waits until the current ones are
276
+ over, installs the requested version with npm, exits, and its service
277
+ manager starts the new version. (The console button is bridex-cp's
278
+ companion change; without it the CP never asks.)
279
+
280
+ What the machine needs. Without all of these, the worker refuses every
281
+ request. It says why in its report to the CP, where the console shows it,
282
+ and never stops taking runs:
283
+
284
+ - `remote_update: on` in the `cp` block of worker.yaml (`join-cp
285
+ --remote-update`). Off by default: the install runs with your user's
286
+ rights, outside the jail.
287
+ - A control plane reached over `https://`.
288
+ - Not native Windows (WSL2 is Linux).
289
+ - A worker installed with `npm i -g bridex-worker` and started from there.
290
+ That means `bridex-worker`, or `node <prefix>/lib/node_modules/bridex-worker/dist/cli.js`,
291
+ not `npx` or a source checkout: npm would otherwise update a copy other
292
+ than the one running. The worker also has to find an npm whose
293
+ `npm root -g` names that same folder (the one in `<prefix>/bin`, or the
294
+ one next to the node that runs the worker), and the folder has to be
295
+ writable by the worker's user, as does `<prefix>/bin`, where npm replaces
296
+ the `bridex-worker` link (or the prefix, when it has no `bin` yet; a
297
+ `bin` that is a symlink to nothing is refused).
298
+ `<prefix>/lib/node_modules` has to be a
299
+ folder, not a symlink to one: the install would follow a link repointed
300
+ since. All of this is checked once, at start.
301
+ - A service manager that starts it again after it exits: launchd with
302
+ `KeepAlive` or systemd with `Restart=always` (see [Platforms](#platforms)).
303
+ A worker started in a terminal just ends after an update.
304
+
305
+ What happens, step by step. The worker asks the CP for its assignments every
306
+ minute, and each reply can carry a request `{ id, version }`:
307
+
308
+ 1. A request naming another version arrives. From the next heartbeat on,
309
+ every instance sees this machine with 0 free slots, and a grant that
310
+ still comes in is refused with the reason. Runs and background jobs
311
+ already going are not touched. The report to the CP says `draining`,
312
+ with how many runs (results not yet delivered to their instance count
313
+ too) and jobs are left.
314
+ 2. The drain ends when no run is executing, no run's result is still
315
+ waiting for its instance, and no job is running or settling. There is no
316
+ time limit. A long job, or a result whose instance stays away, holds it
317
+ for as long as that lasts. A result sent counts as waiting until the
318
+ instance answers the next keepalive ping on that connection (one every
319
+ 30 s, counted once the instance has welcomed the connection): until then
320
+ the connection may be half-open, and the result lost in it. A result
321
+ sent into a connection that went counts until it is sent again on the
322
+ next one and answered for the same way, or for the reconnect grace plus
323
+ 5 minutes after it was last sent or last named in a hello (an instance
324
+ that takes the hello waits for it again, even if its welcome is lost; a
325
+ hello the instance answers with an error does not count, nor one it
326
+ shows it read and let go: the worker pings right behind each hello, and
327
+ a pong that comes back with no welcome before it means the instance
328
+ ignored that hello, as one that cannot read this worker's hellos does; a
329
+ hello left unanswered with no such pong still counts, however often, as
330
+ the connection may have died with the welcome on it). A connection the
331
+ instance has not welcomed within 30 s of its hello is ended and opened
332
+ again, with a fresh hello. A result for an instance the machine
333
+ serves no more (unassigned, its key revoked or replaced) is not waited
334
+ for.
335
+ 3. Once nothing is left, the worker runs `npm i -g --prefix <prefix>
336
+ bridex-worker@<version>` (10 minutes at most), with the prefix the
337
+ start-up check found and, for npm's `env node`, the folders of the
338
+ worker's own node and of the node the service named put first in its
339
+ `PATH`, then asks the CLI in that prefix for its version
340
+ (with the worker's own node, or with the `node` on its `PATH` when that
341
+ one is gone from disk, as after a Homebrew upgrade of node).
342
+ Both are taken by real path, so a symlink repointed since the start (a
343
+ version manager's `current`) cannot move the install elsewhere. On a
344
+ machine with nothing to wait for, that is on the poll that brings the
345
+ request; otherwise on the first later poll that still carries it.
346
+ 4. If that prints the requested version, the worker also runs the path it
347
+ was started by (`/usr/local/bin/bridex-worker version`, say, or the CLI
348
+ file itself when that is what started it): the service manager starts
349
+ that path again. When the service named a node by its path (the
350
+ LaunchAgent above: `/opt/homebrew/bin/node <cli.js> run`), it hands the
351
+ path to that node, as the restart does. Otherwise it executes the path
352
+ directly, as the systemd unit does, so its execute bit and its `#!` line
353
+ count, with the `PATH` the worker was started with, so `env node` finds
354
+ the node the restart will find. If it prints the requested version too,
355
+ the worker
356
+ stops the way Ctrl+C does and exits with status 0. Its service manager
357
+ starts the new version.
358
+ That version sees the same request until the CP drops it, and ignores it,
359
+ because it names the version it runs. If a result came to be owed again
360
+ while npm ran (its connection turned out dead), or a job's end began its
361
+ upload, the worker stays held and exits as soon as nothing is left. It
362
+ looks every 5 s by itself, and on every poll whatever that poll carries:
363
+ the new version is on disk already, and the control plane has no say
364
+ any more (it may even have revoked the machine and stopped answering).
365
+
366
+ Npm uses the machine's own configuration (`~/.npmrc`, the global npmrc,
367
+ `npm_config_*` variables) for the registry, the proxy and the rest, as when
368
+ you install the worker by hand. The prefix is the exception: it is the one
369
+ the check found at start, so the install cannot go to another folder.
370
+
371
+ An assignments reply that came through an HTTP redirect carries no request:
372
+ the worker acts only on one from the control plane's own URL.
373
+
374
+ Cancel. Dropping the request in the CP while the worker drains puts it back
375
+ to taking runs at the next poll. A CP that revokes the machine does the
376
+ same. A request with another id replaces the one being drained. Once the
377
+ install has started it cannot be cancelled, and a worker that is left with
378
+ nothing to serve (the CP revoked it and it has no instance key) waits for
379
+ npm to finish before it exits. A machine held by an earlier
380
+ failed install that changed its installation (below) goes back to that
381
+ failure, still held, when the drain for a new request is cancelled.
382
+
383
+ When the install fails, the report says `failed` with npm's error, and this
384
+ process does not try that request id again (a request with a new id does).
385
+ What it leaves depends on what the installed CLI prints afterwards:
386
+
387
+ - It still prints the running version: nothing on disk changed, and the
388
+ worker takes runs again.
389
+ - It prints another version, or does not start: the installation on disk no
390
+ longer matches the running process, which loads files from it at run time
391
+ (the Agent SDK). The worker keeps refusing runs, and its report says the
392
+ installation changed. So it does when the install went well but the path
393
+ the worker was started by no longer leads to it (a version manager's
394
+ `current` repointed since the start) or cannot be executed: the service
395
+ manager would start another copy, or none. Repair it on the machine (`npm i -g
396
+ bridex-worker@<version>` by hand, then restart the service), or send a
397
+ request with a new id.
398
+
399
+ What it does not do:
400
+
401
+ - No rollback. A new version that fails at start is restarted by the service
402
+ manager in a loop and has to be fixed on the machine.
403
+ - No update for a worker joined with instance keys only (no control plane),
404
+ and none without a request from the CP.
405
+ - No pinning of npm's registry or other npm settings but the prefix (see above).
406
+
407
+ Known limits:
408
+
409
+ - Only the process that handled a request remembers it. A worker restarted
410
+ for another reason after a failed install tries the request again, unless
411
+ the CP has dropped it.
412
+ - A worker restarted in the middle of a drain learns of the request again
413
+ with the first poll after its start-up check has answered: the first
414
+ poll, which goes out at once, or the next one a minute later when the
415
+ check is slower. A grant that arrives before then is admitted and only
416
+ makes the drain longer.
417
+ - While the control plane cannot be reached, a draining worker stays
418
+ draining. It neither installs nor learns of a cancel, and takes no runs
419
+ until the control plane answers again.
420
+ - The states are reported once a minute. A drain and an install that both
421
+ end between two reports are seen only as the new version.
422
+ - Stopping the worker by hand while npm runs leaves npm running by itself.
423
+ What it installs is what the next start runs.
424
+ - The exit goes through `process.exit()`. On a wedged thread pool that does
425
+ not end the process, and the lifeline thread ends it within about 170 s
426
+ (see [When the worker gets stuck](#when-the-worker-gets-stuck)).
427
+
428
+ ## When the worker gets stuck
429
+
430
+ File work and DNS lookups run on Node's thread pool. If every thread of
431
+ that pool is blocked, the process stays alive but can open no new
432
+ connection: it stops reporting to the control plane, cannot reconnect to
433
+ its instances, and never exits on its own. A watchdog inside the worker
434
+ ends such a process, so its service manager starts a fresh one:
435
+
436
+ - Every 30 s it hands the pool a task that needs a free thread for
437
+ microseconds (`crypto.randomBytes`, no disk, no resolver). A task still
438
+ waiting after 120 s means the pool is wedged; a slower task that finishes
439
+ before that is fine.
440
+ - It then logs `the thread pool has run no task for <N> s: …`, saves the
441
+ thread stacks (macOS), stops the runs here and ends the process. All of
442
+ that takes at most 20 s, so a process whose pool wedges ends within
443
+ 170 s.
444
+ - A worker thread ends the process when its main thread is held (a log
445
+ line written to a stalled disk, the unmount of the store) and the
446
+ watchdog cannot act: before it fires, while the worker starts (the
447
+ thread is armed before the config is read and the first line logged),
448
+ or on the way out. It sends the SIGKILL itself no later than 170 s
449
+ after the main thread was held, or 20 s after the stuck line once the
450
+ watchdog has fired. While the worker starts, the scan of the jobs an
451
+ earlier process left puts it off at each job, so a long scan that makes
452
+ progress is not cut. That end logs nothing more, saves no stacks and
453
+ stops no runs here; their processes go with the worker's process group
454
+ (launchd) or its unit (systemd).
455
+ - A main thread that was held for a while and then went on can stretch
456
+ the 170 s. A stuck line that came late still gets its full 20 s for the
457
+ stacks and the stop. A probe that went out late gets its full 120 s and
458
+ the 20 s from when it went out. And the pool's answer to a probe that
459
+ the main thread got to late counts from when the main thread was due to
460
+ look at that probe, which can be up to 30 s after the pool ran it. A
461
+ probe found past 120 s whose looks came late waits as long again as
462
+ they did, added up, up to 30 s, before it ends the process: a process
463
+ that was stopped whole (SIGSTOP, a paused VM) stopped the pool with it,
464
+ and a probe that still waited behind other pool work gets the time to
465
+ run once it goes on.
466
+ - The runs here are cut, as on Ctrl-C; each reports that the worker went
467
+ away, so the instance retries it as it does a run whose machine dropped
468
+ off. Their background jobs are left running, and the next process takes
469
+ back those still alive.
470
+ - The process ends by SIGKILL (`process.exit()` does not end a process
471
+ whose pool is blocked). Nothing starts it again but its service manager:
472
+ launchd with `KeepAlive` or systemd with `Restart=always` (see
473
+ [Platforms](#platforms)).
474
+ - On macOS the stacks go to `~/.bridex-worker/diag/pool-stuck-<YYYYMMDD-HHMMSS>.txt`
475
+ (`$BRIDEX_WORKER_HOME/diag/` when that is set), taken with
476
+ `sample <pid> 3`, which gets 8 s; the 5 newest files are kept. The line
477
+ after the stuck line says where the file is, or why there is none. The
478
+ stacks of the `libuv-worker` threads show the call the pool is blocked
479
+ in — keep the file for the bug report. Linux leaves no stack file.
480
+
481
+ A connect to an instance that does not open within 15 s (a name lookup
482
+ that never returns, say, or a proxy that trickles its answer) is logged as
483
+ `connection failed: Opening handshake has timed out` and retried like any
484
+ other failed connect.
485
+
486
+ What the watchdog does not cover:
487
+
488
+ - A worker started in a terminal just ends («Killed: 9» in bash,
489
+ `zsh: killed` in zsh) with the stuck line as its last output; start it
490
+ again by hand. When the worker thread ended it (its main thread was
491
+ held), the processes of its runs are left running too, as no service
492
+ manager ends its process group: stop them by hand.
493
+ - A worker that loses only some of its pool threads goes on: the probe
494
+ passes while one thread is free.
495
+ - Ctrl-C and the other ordinary exits end the process with
496
+ `process.exit()`. If the pool is wedged at that moment the process can
497
+ hang there, and the watchdog cannot fire any more: end it with `kill -9`.
498
+ - When the worker thread ends a process whose main thread is held, a
499
+ run's helper that made a process group of its own and does not end with
500
+ its parent (a daemonized dev server, say) outlives it under launchd.
501
+ systemd's default `KillMode=control-group` ends it with the unit.
502
+
503
+ ## Release
504
+
505
+ `.github/workflows/worker-release.yml` publishes the package to npm. To
506
+ release: bump `version` in `worker/package.json` and `VERSION` in
507
+ `worker/src/version.ts` together (a test checks they match), merge to
508
+ `main`, then push a tag `worker-v<version>` on that commit
509
+ (`git tag worker-v0.1.2 && git push origin worker-v0.1.2`). The workflow
510
+ checks that the tag names the package's version, runs the typecheck and the
511
+ worker's tests, builds, packs and checks the tarball
512
+ (`worker/scripts/pack-check.sh`, also run on every pull request), and
513
+ publishes exactly that tarball with the `NPM_TOKEN` repository secret. npm
514
+ never overwrites a version, so a version already published makes the last
515
+ step fail. A prerelease goes under npm's `next` tag. A stable version goes
516
+ under `staged`; a second job waits until npm shows that version and its
517
+ tarball (npm answers a publish minutes before the registry shows it), then
518
+ points `latest` at the highest stable
519
+ version on npm, never below the one `latest` names already. That job runs
520
+ for one tag at a time and reads the registry afresh each time, so releases
521
+ that overlap (two tags pushed together, a backport) leave `latest` on the
522
+ newest stable version, whatever order they finish in.