bridex-worker 0.0.0-stage → 0.1.2

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