@edspencer/paddock 0.80.0 → 0.81.0

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 (97) hide show
  1. package/package.json +1 -1
  2. package/packages/server/dist/app.d.ts +7 -0
  3. package/packages/server/dist/app.js +1 -1
  4. package/packages/server/dist/auth.d.ts +3 -2
  5. package/packages/server/dist/auth.js +11 -0
  6. package/packages/server/dist/background-live.d.ts +17 -0
  7. package/packages/server/dist/background-live.js +24 -0
  8. package/packages/server/dist/busy.d.ts +96 -0
  9. package/packages/server/dist/busy.js +88 -0
  10. package/packages/server/dist/cli/args.d.ts +20 -4
  11. package/packages/server/dist/cli/args.js +120 -7
  12. package/packages/server/dist/cli/busy-command.d.ts +58 -0
  13. package/packages/server/dist/cli/busy-command.js +108 -0
  14. package/packages/server/dist/cli/busy.d.ts +69 -0
  15. package/packages/server/dist/cli/busy.js +150 -0
  16. package/packages/server/dist/cli/output.d.ts +52 -0
  17. package/packages/server/dist/cli/output.js +84 -0
  18. package/packages/server/dist/cli/paddock.js +16 -2
  19. package/packages/server/dist/cli/service/index.d.ts +12 -0
  20. package/packages/server/dist/cli/service/index.js +38 -4
  21. package/packages/server/dist/cli/service/launchd.d.ts +15 -1
  22. package/packages/server/dist/cli/service/launchd.js +116 -18
  23. package/packages/server/dist/herdctl.d.ts +19 -0
  24. package/packages/server/dist/herdctl.js +26 -0
  25. package/packages/server/dist/routes/meta.js +45 -0
  26. package/packages/server/dist/runtime-file.d.ts +26 -0
  27. package/packages/server/dist/runtime-file.js +91 -0
  28. package/packages/server/dist/start.d.ts +33 -0
  29. package/packages/server/dist/start.js +115 -8
  30. package/packages/server/dist/ws-context.d.ts +9 -0
  31. package/packages/server/dist/ws-turn.js +1 -0
  32. package/packages/server/package.json +1 -1
  33. package/packages/web/dist/assets/{DeleteProjectDialog-DHczLhwQ.js → DeleteProjectDialog-D8oIJ8HC.js} +1 -1
  34. package/packages/web/dist/assets/DiscoverPage-CQlzxsn4.js +2 -0
  35. package/packages/web/dist/assets/{DiscoverView-DtTOpTji.js → DiscoverView-o0KCr3aB.js} +1 -1
  36. package/packages/web/dist/assets/{InstanceConfigPage-BRoBh1C1.js → InstanceConfigPage-RSJz7oL3.js} +1 -1
  37. package/packages/web/dist/assets/{MarkdownRenderer-DJf8IJuk.js → MarkdownRenderer-39mafH8b.js} +1 -1
  38. package/packages/web/dist/assets/{ProjectRedirect-EioguUKW.js → ProjectRedirect-Bq7GVguY.js} +1 -1
  39. package/packages/web/dist/assets/{ProjectView-BiKnGfk6.js → ProjectView-BoBIru1g.js} +3 -3
  40. package/packages/web/dist/assets/{ProjectsGrid-D6kfaIal.js → ProjectsGrid-mJ4gqbr6.js} +1 -1
  41. package/packages/web/dist/assets/{RootHome-DpcyuV6e.js → RootHome-C0kwFyYp.js} +1 -1
  42. package/packages/web/dist/assets/{Surfaces-CHM29PhP.js → Surfaces-MkhXlB8z.js} +1 -1
  43. package/packages/web/dist/assets/{arc-fiqD05Hu.js → arc-DExlwAwO.js} +1 -1
  44. package/packages/web/dist/assets/{architectureDiagram-3BPJPVTR-BuuB2Nkf.js → architectureDiagram-3BPJPVTR-CZly3W79.js} +1 -1
  45. package/packages/web/dist/assets/{blockDiagram-GPEHLZMM-DQMBuzfi.js → blockDiagram-GPEHLZMM-AgES73i3.js} +1 -1
  46. package/packages/web/dist/assets/{c4Diagram-AAUBKEIU-BKN0UxCH.js → c4Diagram-AAUBKEIU-N9mD8Pfk.js} +1 -1
  47. package/packages/web/dist/assets/channel-ExsAG7qT.js +2 -0
  48. package/packages/web/dist/assets/{chunk-2J33WTMH-DW37EupX.js → chunk-2J33WTMH-Dsx_kfUl.js} +1 -1
  49. package/packages/web/dist/assets/{chunk-4BX2VUAB-Dyv7zV_c.js → chunk-4BX2VUAB-BQjqcjPj.js} +1 -1
  50. package/packages/web/dist/assets/{chunk-55IACEB6-DzMR0dOk.js → chunk-55IACEB6-CCxpYQpY.js} +1 -1
  51. package/packages/web/dist/assets/{chunk-727SXJPM-BdZ3Sor8.js → chunk-727SXJPM-DpFRdnUa.js} +1 -1
  52. package/packages/web/dist/assets/{chunk-AQP2D5EJ-B4i7_og0.js → chunk-AQP2D5EJ-D2BAlzoO.js} +1 -1
  53. package/packages/web/dist/assets/{chunk-FMBD7UC4-jcTr85it.js → chunk-FMBD7UC4-BFaMRzUF.js} +1 -1
  54. package/packages/web/dist/assets/{chunk-ND2GUHAM-fgMsadqB.js → chunk-ND2GUHAM-B9NGJ9xZ.js} +1 -1
  55. package/packages/web/dist/assets/{chunk-QZHKN3VN-bJNHYS1W.js → chunk-QZHKN3VN-CF1IHyMS.js} +1 -1
  56. package/packages/web/dist/assets/classDiagram-4FO5ZUOK-si7g9okq.js +2 -0
  57. package/packages/web/dist/assets/classDiagram-v2-Q7XG4LA2-si7g9okq.js +2 -0
  58. package/packages/web/dist/assets/{core-Bk4KFrmN.js → core-XjElsvQ7.js} +1 -1
  59. package/packages/web/dist/assets/{cose-bilkent-S5V4N54A-BIqCSlQy.js → cose-bilkent-S5V4N54A-CF972Flx.js} +1 -1
  60. package/packages/web/dist/assets/{dagre-BM42HDAG-6nyhtVQz.js → dagre-BM42HDAG-DsneI8YE.js} +1 -1
  61. package/packages/web/dist/assets/{diagram-2AECGRRQ-CjizfipR.js → diagram-2AECGRRQ-BHyrVzIk.js} +1 -1
  62. package/packages/web/dist/assets/{diagram-5GNKFQAL-DlgB5vWb.js → diagram-5GNKFQAL-Dj4q8xEi.js} +1 -1
  63. package/packages/web/dist/assets/{diagram-KO2AKTUF-CNC9qanV.js → diagram-KO2AKTUF-BHe3Puv_.js} +1 -1
  64. package/packages/web/dist/assets/{diagram-LMA3HP47-DSJ-s9lh.js → diagram-LMA3HP47-wzt6G2FE.js} +1 -1
  65. package/packages/web/dist/assets/{diagram-OG6HWLK6-C9FoHBDg.js → diagram-OG6HWLK6-CZMbMRJq.js} +1 -1
  66. package/packages/web/dist/assets/{documentTitle-Cj_AY97j.js → documentTitle-BwTIA8rI.js} +1 -1
  67. package/packages/web/dist/assets/{erDiagram-TEJ5UH35-z6gku4Yt.js → erDiagram-TEJ5UH35-Dtu14BL1.js} +1 -1
  68. package/packages/web/dist/assets/{flowDiagram-I6XJVG4X-Dl5c_FgU.js → flowDiagram-I6XJVG4X-T34ql9j0.js} +1 -1
  69. package/packages/web/dist/assets/{ganttDiagram-6RSMTGT7-CIscJ-Dt.js → ganttDiagram-6RSMTGT7-DQLOeCXK.js} +1 -1
  70. package/packages/web/dist/assets/{gitGraphDiagram-PVQCEYII-CvttOXM_.js → gitGraphDiagram-PVQCEYII-Bk11zea6.js} +1 -1
  71. package/packages/web/dist/assets/{index-DN82do6Z.js → index-CvaWErFv.js} +2 -2
  72. package/packages/web/dist/assets/{infoDiagram-5YYISTIA-dzWYdDJj.js → infoDiagram-5YYISTIA-CsE83lu0.js} +1 -1
  73. package/packages/web/dist/assets/{ishikawaDiagram-YF4QCWOH-8yX9k4bo.js → ishikawaDiagram-YF4QCWOH-DNVgA93n.js} +1 -1
  74. package/packages/web/dist/assets/{journeyDiagram-JHISSGLW-BmitzJDR.js → journeyDiagram-JHISSGLW-CLpSNjM3.js} +1 -1
  75. package/packages/web/dist/assets/{kanban-definition-UN3LZRKU-BZmIkfkW.js → kanban-definition-UN3LZRKU-BRzuXcKr.js} +1 -1
  76. package/packages/web/dist/assets/{linear-BBLXE4o7.js → linear-BxOawONt.js} +1 -1
  77. package/packages/web/dist/assets/{mermaid.core-mSKff4yu.js → mermaid.core-Dvs1QCcz.js} +4 -4
  78. package/packages/web/dist/assets/{mindmap-definition-RKZ34NQL-BWAUa4CP.js → mindmap-definition-RKZ34NQL-Dmt7pYwF.js} +1 -1
  79. package/packages/web/dist/assets/{pieDiagram-4H26LBE5-VmVdiL4C.js → pieDiagram-4H26LBE5-BHiZaJD-.js} +1 -1
  80. package/packages/web/dist/assets/{quadrantDiagram-W4KKPZXB-Dybfv6NU.js → quadrantDiagram-W4KKPZXB-U0A1noRE.js} +1 -1
  81. package/packages/web/dist/assets/{requirementDiagram-4Y6WPE33-BPnOvB7w.js → requirementDiagram-4Y6WPE33-CZLsr6yi.js} +1 -1
  82. package/packages/web/dist/assets/{sankeyDiagram-5OEKKPKP-YXIEmKw7.js → sankeyDiagram-5OEKKPKP-BTlLGILU.js} +1 -1
  83. package/packages/web/dist/assets/{sequenceDiagram-3UESZ5HK-DintIRUe.js → sequenceDiagram-3UESZ5HK-DpSS_Gb1.js} +1 -1
  84. package/packages/web/dist/assets/{stateDiagram-AJRCARHV-eMsjahWp.js → stateDiagram-AJRCARHV-D9srE-tV.js} +1 -1
  85. package/packages/web/dist/assets/stateDiagram-v2-BHNVJYJU-AxWqsDh-.js +2 -0
  86. package/packages/web/dist/assets/{timeline-definition-PNZ67QCA-as4zRCPn.js → timeline-definition-PNZ67QCA-BYQ5CKab.js} +1 -1
  87. package/packages/web/dist/assets/{vennDiagram-CIIHVFJN-CPzvUkM0.js → vennDiagram-CIIHVFJN-B4W2CtSV.js} +1 -1
  88. package/packages/web/dist/assets/{wardley-L42UT6IY-q6moXj5g.js → wardley-L42UT6IY--XUV7GvD.js} +1 -1
  89. package/packages/web/dist/assets/{wardleyDiagram-YWT4CUSO-BsEEb9Gm.js → wardleyDiagram-YWT4CUSO-Bl2VIEGY.js} +1 -1
  90. package/packages/web/dist/assets/{xychartDiagram-2RQKCTM6-DSgZlcZW.js → xychartDiagram-2RQKCTM6-Bj2H9Rcf.js} +1 -1
  91. package/packages/web/dist/index.html +1 -1
  92. package/packages/web/dist/sw.js +1 -1
  93. package/packages/web/dist/assets/DiscoverPage-BSGSzTEs.js +0 -2
  94. package/packages/web/dist/assets/channel-B7yh6Su_.js +0 -2
  95. package/packages/web/dist/assets/classDiagram-4FO5ZUOK-DIlhMPgi.js +0 -2
  96. package/packages/web/dist/assets/classDiagram-v2-Q7XG4LA2-DIlhMPgi.js +0 -2
  97. package/packages/web/dist/assets/stateDiagram-v2-BHNVJYJU-CpAOGzbO.js +0 -2
@@ -75,4 +75,18 @@ export declare function parseLaunchctlPrint(out: string): {
75
75
  running: boolean;
76
76
  pid?: string;
77
77
  };
78
- export declare function createLaunchdBackend(run: Runner, homeDir?: string): ServiceBackend;
78
+ /**
79
+ * Block this thread for `ms`.
80
+ *
81
+ * The backend's {@link Runner} is synchronous (`execFileSync`) and so is every
82
+ * `ServiceBackend` method, so the wait has to be too — making `install` async
83
+ * would ripple through the whole service CLI for a sleep. `Atomics.wait` on a
84
+ * throwaway buffer is the built-in way to do it; `setTimeout` cannot help here
85
+ * and spinning on `Date.now()` would burn a core.
86
+ *
87
+ * Injectable because the unit tests drive the retry path with a fake runner
88
+ * that fails every attempt: with the real sleep, pinning the retry count would
89
+ * cost three seconds per case.
90
+ */
91
+ export declare function sleepSync(ms: number): void;
92
+ export declare function createLaunchdBackend(run: Runner, homeDir?: string, sleep?: (ms: number) => void): ServiceBackend;
@@ -188,8 +188,107 @@ function target() {
188
188
  function domain() {
189
189
  return `gui/${process.getuid?.() ?? 0}`;
190
190
  }
191
- export function createLaunchdBackend(run, homeDir = os.homedir()) {
191
+ /**
192
+ * ## The bootout/bootstrap race (#984)
193
+ *
194
+ * `launchctl bootout` returns as soon as launchd has ACCEPTED the request, not
195
+ * once the job has gone. Every sequence below that boots out and then
196
+ * bootstraps the same label therefore races launchd's own teardown, and when it
197
+ * loses, `bootstrap` fails with:
198
+ *
199
+ * ```
200
+ * launchctl bootstrap failed (exit 5): Input/output error
201
+ * ```
202
+ *
203
+ * Exit 5 is `EIO`, which is launchd's generic "I cannot do that right now" and
204
+ * the classic symptom of this race. It is timing-dependent, so it passes
205
+ * locally and on a PR and then fails on a release: Paddock 0.80.0's first
206
+ * release run failed exactly here, in CI's real-launchd job, on
207
+ * `paddock service restart`.
208
+ *
209
+ * Both halves of the fix are needed, and neither is sufficient:
210
+ *
211
+ * - **Wait for the job to go.** After a bootout, poll `launchctl print` until
212
+ * it reports the label gone (a non-zero exit), up to {@link BOOTOUT_POLLS}
213
+ * times. That removes the race in the case we caused it.
214
+ * - **Retry the bootstrap.** A bootout we did NOT issue can still be in
215
+ * flight — `service start` after a separate process's `service stop`, or a
216
+ * login racing a logout — and there is nothing to poll for there, because
217
+ * from this process's point of view the job was already gone. So a
218
+ * bootstrap that fails with a race code is retried with a short backoff.
219
+ *
220
+ * Timing out the poll is not an error: the retry is the backstop, and a job
221
+ * that will not go is a real failure the bootstrap reports in its own words.
222
+ */
223
+ const BOOTOUT_POLLS = 40;
224
+ const BOOTOUT_POLL_MS = 250; // 40 × 250ms = 10s
225
+ const BOOTSTRAP_ATTEMPTS = 6;
226
+ const BOOTSTRAP_BACKOFF_MS = 500;
227
+ /**
228
+ * launchctl exits that mean "try again", as opposed to "no".
229
+ *
230
+ * `5` is `EIO`, what the release failure printed. `37` is launchd's "operation
231
+ * already in progress", which is the same race seen from the other side: the
232
+ * bootout is still being processed. Anything else — a malformed plist, a label
233
+ * genuinely already loaded, a permission problem — is reported, not retried,
234
+ * because retrying it only delays the message by three seconds.
235
+ */
236
+ const BOOTSTRAP_RACE_CODES = new Set([5, 37]);
237
+ /**
238
+ * Block this thread for `ms`.
239
+ *
240
+ * The backend's {@link Runner} is synchronous (`execFileSync`) and so is every
241
+ * `ServiceBackend` method, so the wait has to be too — making `install` async
242
+ * would ripple through the whole service CLI for a sleep. `Atomics.wait` on a
243
+ * throwaway buffer is the built-in way to do it; `setTimeout` cannot help here
244
+ * and spinning on `Date.now()` would burn a core.
245
+ *
246
+ * Injectable because the unit tests drive the retry path with a fake runner
247
+ * that fails every attempt: with the real sleep, pinning the retry count would
248
+ * cost three seconds per case.
249
+ */
250
+ export function sleepSync(ms) {
251
+ if (ms <= 0)
252
+ return;
253
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
254
+ }
255
+ export function createLaunchdBackend(run, homeDir = os.homedir(), sleep = sleepSync) {
192
256
  const file = plistPath(homeDir);
257
+ /**
258
+ * Boot the job out and wait until launchd agrees it has gone — see the race
259
+ * note above. The bootout's own result is deliberately not inspected: "there
260
+ * was no previous service" is the expected case on a first install, and a
261
+ * bootout that genuinely failed shows up as a job that is still there, which
262
+ * is what the poll below is looking at anyway.
263
+ */
264
+ const bootoutAndWait = () => {
265
+ run("launchctl", ["bootout", target()]);
266
+ // Counted, not timed: an injected no-op sleep has to terminate the loop
267
+ // too, and a wall-clock deadline with a no-op sleep is a ten-second spin.
268
+ for (let poll = 1; poll <= BOOTOUT_POLLS; poll++) {
269
+ // A non-zero `print` is launchd saying it has never heard of the label,
270
+ // which is precisely the state `bootstrap` needs.
271
+ if (run("launchctl", ["print", target()]).status !== 0)
272
+ return;
273
+ if (poll < BOOTOUT_POLLS)
274
+ sleep(BOOTOUT_POLL_MS);
275
+ }
276
+ };
277
+ /** Bootstrap the plist, retrying the race codes. Throws with launchctl's own words. */
278
+ const bootstrapOrThrow = () => {
279
+ for (let attempt = 1;; attempt++) {
280
+ // NOT `load`/`unload`: those are the legacy API, and on a modern macOS
281
+ // they are shims over this one that report success in cases where it
282
+ // reports the actual error.
283
+ const boot = run("launchctl", ["bootstrap", domain(), file]);
284
+ if (boot.status === 0)
285
+ return;
286
+ if (attempt >= BOOTSTRAP_ATTEMPTS || !BOOTSTRAP_RACE_CODES.has(boot.status ?? -1)) {
287
+ throw new Error(launchctlFailure("bootstrap", boot));
288
+ }
289
+ sleep(BOOTSTRAP_BACKOFF_MS);
290
+ }
291
+ };
193
292
  return {
194
293
  platform: "darwin",
195
294
  unitPath: file,
@@ -202,16 +301,12 @@ export function createLaunchdBackend(run, homeDir = os.homedir()) {
202
301
  fs.mkdirSync(path.dirname(spec.logPath), { recursive: true });
203
302
  fs.writeFileSync(file, renderPlist(spec), "utf8");
204
303
  // `bootstrap` fails outright if the label is already loaded, so a
205
- // reinstall has to unload first. A first install has nothing to unload,
206
- // and says so on stderr — ignored rather than reported, because "there
207
- // was no previous service" is the expected case, not a problem.
208
- run("launchctl", ["bootout", target()]);
209
- // NOT `load`/`unload`: those are the legacy API, and on a modern macOS
210
- // they are shims over this one that report success in cases where it
211
- // reports the actual error.
212
- const boot = run("launchctl", ["bootstrap", domain(), file]);
213
- if (boot.status !== 0)
214
- throw new Error(launchctlFailure("bootstrap", boot));
304
+ // reinstall has to unload first — and has to WAIT for that, which is what
305
+ // `bootoutAndWait` is for (see the race note). A first install has
306
+ // nothing to unload, and says so on stderr: ignored rather than reported,
307
+ // because "there was no previous service" is the expected case.
308
+ bootoutAndWait();
309
+ bootstrapOrThrow();
215
310
  // `RunAtLoad` should already have started it; `kickstart -k` makes that
216
311
  // true rather than probable, and is the same command a later restart uses.
217
312
  run("launchctl", ["kickstart", "-k", target()]);
@@ -230,11 +325,13 @@ export function createLaunchdBackend(run, homeDir = os.homedir()) {
230
325
  * Paddock is not merely idle, it is absent from the domain — and `start` on
231
326
  * a label launchd has never heard of fails. Bootstrapping is also what a
232
327
  * login does, so `service start` and the next login take the same path.
328
+ *
329
+ * No bootout to wait on here, but the retry in `bootstrapOrThrow` still
330
+ * earns its keep: the `stop` that booted it out may have been a different
331
+ * `paddock` process seconds ago, and launchd may not have finished with it.
233
332
  */
234
333
  start() {
235
- const boot = run("launchctl", ["bootstrap", domain(), file]);
236
- if (boot.status !== 0)
237
- throw new Error(launchctlFailure("bootstrap", boot));
334
+ bootstrapOrThrow();
238
335
  run("launchctl", ["kickstart", target()]);
239
336
  },
240
337
  /**
@@ -263,12 +360,13 @@ export function createLaunchdBackend(run, homeDir = os.homedir()) {
263
360
  * service that is stopped. This sequence is identical to `install`'s minus
264
361
  * writing the plist, which also gives `restart` the property the issue asks
265
362
  * for — the unit is re-read on the way up, so an edited plist takes effect.
363
+ *
364
+ * This is the sequence that failed Paddock 0.80.0's release run: bootout
365
+ * and bootstrap back to back, with nothing between them. See the race note.
266
366
  */
267
367
  restart() {
268
- run("launchctl", ["bootout", target()]);
269
- const boot = run("launchctl", ["bootstrap", domain(), file]);
270
- if (boot.status !== 0)
271
- throw new Error(launchctlFailure("bootstrap", boot));
368
+ bootoutAndWait();
369
+ bootstrapOrThrow();
272
370
  run("launchctl", ["kickstart", "-k", target()]);
273
371
  },
274
372
  status() {
@@ -371,6 +371,25 @@ export declare class HerdctlService {
371
371
  * error, returns `false` — the pre-#397 behaviour.
372
372
  */
373
373
  isSdkSessionLive(sessionId: string): boolean;
374
+ /**
375
+ * How many scheduler jobs are running right now, read from memory.
376
+ *
377
+ * This exists for `GET /api/busy` (CLI contract SPEC §4), which must read
378
+ * in-memory state and never walk the disk. The number the contract's ROADMAP
379
+ * names is `fleetStatus().counts.runningJobs` — but {@link fleetStatus} is
380
+ * built from `getAgentInfo()`, which re-reads the fleet state FILE on every
381
+ * call, and that re-parse has been this server's single largest CPU cost.
382
+ *
383
+ * The two are the same number. `counts.runningJobs` is
384
+ * `Σ scheduler.getRunningJobCount(agent.qualifiedName)` over the configured
385
+ * agents, and `getTotalRunningJobCount()` is the size of the same scheduler
386
+ * map keyed `<agent>/<schedule>` — one entry per running agent+schedule pair
387
+ * either way. So this reads the authority directly instead of a derived
388
+ * snapshot of it.
389
+ *
390
+ * Null-safe like {@link isSdkSessionLive}: no fleet or no scheduler means 0.
391
+ */
392
+ runningJobCount(): number;
374
393
  fleetStatus(): Promise<FleetStatus>;
375
394
  agents(): Promise<AgentInfo[]>;
376
395
  /**
@@ -528,6 +528,32 @@ export class HerdctlService {
528
528
  return false;
529
529
  }
530
530
  }
531
+ /**
532
+ * How many scheduler jobs are running right now, read from memory.
533
+ *
534
+ * This exists for `GET /api/busy` (CLI contract SPEC §4), which must read
535
+ * in-memory state and never walk the disk. The number the contract's ROADMAP
536
+ * names is `fleetStatus().counts.runningJobs` — but {@link fleetStatus} is
537
+ * built from `getAgentInfo()`, which re-reads the fleet state FILE on every
538
+ * call, and that re-parse has been this server's single largest CPU cost.
539
+ *
540
+ * The two are the same number. `counts.runningJobs` is
541
+ * `Σ scheduler.getRunningJobCount(agent.qualifiedName)` over the configured
542
+ * agents, and `getTotalRunningJobCount()` is the size of the same scheduler
543
+ * map keyed `<agent>/<schedule>` — one entry per running agent+schedule pair
544
+ * either way. So this reads the authority directly instead of a derived
545
+ * snapshot of it.
546
+ *
547
+ * Null-safe like {@link isSdkSessionLive}: no fleet or no scheduler means 0.
548
+ */
549
+ runningJobCount() {
550
+ try {
551
+ return this.fleet?.getScheduler()?.getTotalRunningJobCount() ?? 0;
552
+ }
553
+ catch {
554
+ return 0;
555
+ }
556
+ }
531
557
  async fleetStatus() {
532
558
  return this.manager.getFleetStatus();
533
559
  }
@@ -6,8 +6,14 @@ import { resolveModels, resolveDefaultModel } from "../models.js";
6
6
  import { sendProjectError } from "../route-errors.js";
7
7
  import { cspFor, parseRangeHeader } from "../http-bytes.js";
8
8
  import { PADDOCK_VERSION } from "../version.js";
9
+ import { collectBusy } from "../busy.js";
9
10
  export function registerMetaRoutes(app, ctx) {
10
11
  const { herdctl, transcriber, attachments, cfg } = ctx;
12
+ // Both optional for the same reason `turn-interlock.ts` takes an optional hub:
13
+ // a server built without the WS layer (several integration tests) has neither,
14
+ // and the honest answer for it is zero rather than a crash.
15
+ const hub = ctx.managementOpsContext?.hub;
16
+ const background = ctx.managementOpsContext?.background;
11
17
  // --- voice dictation (#voice): capability probe + transcription -------
12
18
  // The composer polls this to decide whether to show a mic button. `available`
13
19
  // is false on instances with dictation off (or a misconfigured remote).
@@ -128,6 +134,45 @@ export function registerMetaRoutes(app, ctx) {
128
134
  },
129
135
  },
130
136
  }, async () => ({ ok: true, version: PADDOCK_VERSION }));
137
+ // The contract's second server endpoint (SPEC §4, §8): is this instance busy?
138
+ //
139
+ // **Counts only, and no auth, and those two facts are the same fact.** There
140
+ // is nothing in the answer to protect — no chat title, no session id, no
141
+ // project name, just three integers and the version — which is why the route
142
+ // can sit beside `/api/health` in `HEALTH_PATHS` without widening what an
143
+ // unauthenticated caller can learn beyond "something is happening here".
144
+ // `reasons` (a line per piece of work, for a person) waits on SPEC §10's first
145
+ // question: how a CLI proves it is local, given that a reverse proxy on the
146
+ // same host also connects from loopback. Until that is designed, this answer
147
+ // is the whole answer for every caller — so nothing here may grow a string
148
+ // field without revisiting the auth decision above it.
149
+ //
150
+ // The callers that make the cheapness requirement real: `paddock service
151
+ // stop`, `paddock update`, and Watchtower's pre-update hook, which runs
152
+ // `paddock busy` before every image pull. `collectBusy` is synchronous and
153
+ // reads three in-memory structures — see busy.ts for why `job` does NOT come
154
+ // from `fleetStatus()`.
155
+ app.get("/api/busy", {
156
+ schema: {
157
+ tags: ["System"],
158
+ summary: "Is this instance busy?",
159
+ description: "The CLI contract's busy check (SPEC §4). Returns `{ busy, version, counts }`, where `counts` has `chat` (chat turns running, including fired triggers and cron fires), `background` (live sessions holding background work after their turn ended) and `job` (herdctl scheduler jobs). `busy` is true only for work a restart would lose or cut short. Counts only — never labels, ids or titles — so it needs no authentication. Reads in-memory state, never the disk.",
160
+ response: {
161
+ 200: {
162
+ description: "Whether work a restart would cut short is under way.",
163
+ type: "object",
164
+ additionalProperties: true,
165
+ },
166
+ },
167
+ },
168
+ }, async () => collectBusy({
169
+ runningChats: () => hub?.runningSessions().length ?? 0,
170
+ // Tasks in a session count only while its SDK session is still live —
171
+ // see `BackgroundRegistry.liveTaskCount`, which explains why a stale
172
+ // batch-mode entry must not wedge this at busy.
173
+ backgroundTasks: () => background?.liveTaskCount((id) => herdctl.isSdkSessionLive(id)) ?? 0,
174
+ runningJobs: () => herdctl.runningJobCount(),
175
+ }, PADDOCK_VERSION));
131
176
  // --- instance-wide settings (issue #385) --------------------------------
132
177
  // A top-level admin screen over the frozen instance config. GET reports every
133
178
  // surfaced field (value / default / editable / sensitive / env-shadow); PUT
@@ -0,0 +1,26 @@
1
+ export interface RuntimeInfo {
2
+ pid: number;
3
+ port: number;
4
+ /** Where the CLI reaches it, e.g. `http://127.0.0.1:3729`. */
5
+ url: string;
6
+ version: string;
7
+ /** ISO 8601. */
8
+ startedAt: string;
9
+ }
10
+ export declare const RUNTIME_FILE = "runtime.json";
11
+ export declare function runtimeFilePath(home: string): string;
12
+ /** Writes the file atomically (a temporary file, then a rename), readable only by this user. */
13
+ export declare function writeRuntimeFile(home: string, info: Omit<RuntimeInfo, "startedAt"> & {
14
+ startedAt?: string;
15
+ }): RuntimeInfo;
16
+ /** The file's contents, or null if it's missing or isn't a runtime file (a half-written or foreign file). */
17
+ export declare function readRuntimeFile(home: string): RuntimeInfo | null;
18
+ /**
19
+ * Removes the file, but only if it's still ours: a second instance may have started since and written its own.
20
+ * Call it on a clean exit. Returns whether it removed anything.
21
+ */
22
+ export declare function removeRuntimeFile(home: string, pid?: number): boolean;
23
+ /** Whether a process with this pid exists. EPERM means it exists but isn't ours, which still counts. */
24
+ export declare function isPidAlive(pid: number): boolean;
25
+ /** The runtime file, if the process it names is still alive. A crashed instance leaves a stale file behind. */
26
+ export declare function readLiveRuntimeFile(home: string): RuntimeInfo | null;
@@ -0,0 +1,91 @@
1
+ // From edspencer/cli-contract reference/runtime-file.ts @ SPEC 0.1; local changes: `node:fs` / `node:path` imports
2
+ // unchanged, nothing else — the module is Node built-ins only and Paddock's `<home>` is its data dir, so it needed
3
+ // no adaptation. Kept verbatim on purpose: wave C4 re-syncs it, and a divergence here would be a divergence in the
4
+ // one file every tool's `busy`, `service status` and `update` agree on.
5
+ //
6
+ // `<home>/runtime.json`: what a running instance tells its own CLI (SPEC §4, ROADMAP). `serve` writes it once it's
7
+ // listening and removes it on a clean exit; `busy`, `service status` and `update` read it to find the instance.
8
+ //
9
+ // It holds `{ pid, port, url, version, startedAt }`. A token for /api/busy will join it once SPEC §10's first question
10
+ // is settled; the file is written 0600 already, so adding one later changes nothing about who can read it.
11
+ // Node built-ins only.
12
+ import fs from "node:fs";
13
+ import path from "node:path";
14
+ export const RUNTIME_FILE = "runtime.json";
15
+ export function runtimeFilePath(home) {
16
+ return path.join(home, RUNTIME_FILE);
17
+ }
18
+ /** Writes the file atomically (a temporary file, then a rename), readable only by this user. */
19
+ export function writeRuntimeFile(home, info) {
20
+ const full = {
21
+ pid: info.pid,
22
+ port: info.port,
23
+ url: info.url,
24
+ version: info.version,
25
+ startedAt: info.startedAt ?? new Date().toISOString(),
26
+ };
27
+ fs.mkdirSync(home, { recursive: true });
28
+ const file = runtimeFilePath(home);
29
+ const tmp = `${file}.${process.pid}.tmp`;
30
+ fs.writeFileSync(tmp, `${JSON.stringify(full, null, 2)}\n`, { mode: 0o600 });
31
+ try {
32
+ fs.renameSync(tmp, file);
33
+ }
34
+ catch (err) {
35
+ fs.rmSync(tmp, { force: true });
36
+ throw err;
37
+ }
38
+ return full;
39
+ }
40
+ /** The file's contents, or null if it's missing or isn't a runtime file (a half-written or foreign file). */
41
+ export function readRuntimeFile(home) {
42
+ let raw;
43
+ try {
44
+ raw = fs.readFileSync(runtimeFilePath(home), "utf8");
45
+ }
46
+ catch {
47
+ return null;
48
+ }
49
+ try {
50
+ const v = JSON.parse(raw);
51
+ if (Number.isInteger(v.pid) &&
52
+ Number.isInteger(v.port) &&
53
+ typeof v.url === "string" &&
54
+ typeof v.version === "string" &&
55
+ typeof v.startedAt === "string") {
56
+ return v;
57
+ }
58
+ }
59
+ catch {
60
+ // fall through
61
+ }
62
+ return null;
63
+ }
64
+ /**
65
+ * Removes the file, but only if it's still ours: a second instance may have started since and written its own.
66
+ * Call it on a clean exit. Returns whether it removed anything.
67
+ */
68
+ export function removeRuntimeFile(home, pid = process.pid) {
69
+ const info = readRuntimeFile(home);
70
+ if (!info || info.pid !== pid)
71
+ return false;
72
+ fs.rmSync(runtimeFilePath(home), { force: true });
73
+ return true;
74
+ }
75
+ /** Whether a process with this pid exists. EPERM means it exists but isn't ours, which still counts. */
76
+ export function isPidAlive(pid) {
77
+ if (!Number.isInteger(pid) || pid <= 0)
78
+ return false;
79
+ try {
80
+ process.kill(pid, 0);
81
+ return true;
82
+ }
83
+ catch (err) {
84
+ return err.code === "EPERM";
85
+ }
86
+ }
87
+ /** The runtime file, if the process it names is still alive. A crashed instance leaves a stale file behind. */
88
+ export function readLiveRuntimeFile(home) {
89
+ const info = readRuntimeFile(home);
90
+ return info && isPidAlive(info.pid) ? info : null;
91
+ }
@@ -1 +1,34 @@
1
+ /**
2
+ * How long a clean shutdown gets before we exit anyway.
3
+ *
4
+ * The CLI contract requires the SIGTERM path to exit "within the unit's stop
5
+ * timeout" (SPEC §4, §8), and the tightest of those is **launchd's**: with no
6
+ * `ExitTimeOut` in the plist, a LaunchAgent gets 20 seconds before SIGKILL
7
+ * (systemd's user default is 90). So the budget is 20 seconds, and this sits
8
+ * inside it with room for the exit itself.
9
+ *
10
+ * The deadline matters because the two awaits below are not bounded by anything
11
+ * else. `app.close()` waits for in-flight requests and open WebSockets, and a
12
+ * browser holding `/ws` is the normal state of a running Paddock; `fleet.stop()`
13
+ * talks to subprocesses. Exceeding the budget is strictly worse than exiting at
14
+ * it: SIGKILL skips the rest of this function, so the runtime file would be left
15
+ * behind for the next instance's `busy` to find and the log would have no record
16
+ * of what was cut short.
17
+ */
18
+ export declare const SHUTDOWN_DEADLINE_MS = 15000;
19
+ /**
20
+ * The URL to record in `runtime.json` — the one a CLI on this machine can
21
+ * actually fetch, which is not always the one we bound.
22
+ *
23
+ * A wildcard bind is the normal case for a container or a dev box (`HOST=0.0.0.0`
24
+ * is what this repo's own `pm` wrapper injects), and `http://0.0.0.0:<port>` is
25
+ * not an address to connect TO. Node happens to resolve it to loopback on Linux
26
+ * and fails on some other stacks, so the file records loopback explicitly rather
27
+ * than relying on that. A real interface address is left alone: someone who
28
+ * bound `192.168.1.10` meant it, and that address reaches the server from here
29
+ * as well as from anywhere else.
30
+ *
31
+ * IPv6 gets brackets, because `new URL('http://::1:7233')` is not a URL.
32
+ */
33
+ export declare function runtimeUrl(host: string, port: number): string;
1
34
  export declare function start(): Promise<void>;
@@ -1,7 +1,8 @@
1
1
  /**
2
- * Process lifecycle for a Paddock server: bind the port and wire signal-driven
3
- * shutdown. Split out of `index.ts` so the `paddock` CLI (src/cli/paddock.ts)
4
- * and the plain `node dist/index.js` entrypoint share one implementation.
2
+ * Process lifecycle for a Paddock server: bind the port, publish
3
+ * `runtime.json`, and wire signal-driven shutdown. Split out of `index.ts` so
4
+ * the `paddock` CLI (src/cli/paddock.ts) and the plain `node dist/index.js`
5
+ * entrypoint share one implementation.
5
6
  *
6
7
  * Config is resolved INSIDE `buildApp()`, not at module load, so a caller may
7
8
  * still mutate `process.env` right up until this function is invoked. The CLI
@@ -9,8 +10,51 @@
9
10
  */
10
11
  import { buildApp } from "./app.js";
11
12
  import { startServiceLogRotation } from "./service-log.js";
13
+ import { removeRuntimeFile, writeRuntimeFile } from "./runtime-file.js";
14
+ import { PADDOCK_VERSION } from "./version.js";
15
+ /**
16
+ * How long a clean shutdown gets before we exit anyway.
17
+ *
18
+ * The CLI contract requires the SIGTERM path to exit "within the unit's stop
19
+ * timeout" (SPEC §4, §8), and the tightest of those is **launchd's**: with no
20
+ * `ExitTimeOut` in the plist, a LaunchAgent gets 20 seconds before SIGKILL
21
+ * (systemd's user default is 90). So the budget is 20 seconds, and this sits
22
+ * inside it with room for the exit itself.
23
+ *
24
+ * The deadline matters because the two awaits below are not bounded by anything
25
+ * else. `app.close()` waits for in-flight requests and open WebSockets, and a
26
+ * browser holding `/ws` is the normal state of a running Paddock; `fleet.stop()`
27
+ * talks to subprocesses. Exceeding the budget is strictly worse than exiting at
28
+ * it: SIGKILL skips the rest of this function, so the runtime file would be left
29
+ * behind for the next instance's `busy` to find and the log would have no record
30
+ * of what was cut short.
31
+ */
32
+ export const SHUTDOWN_DEADLINE_MS = 15_000;
33
+ /**
34
+ * The URL to record in `runtime.json` — the one a CLI on this machine can
35
+ * actually fetch, which is not always the one we bound.
36
+ *
37
+ * A wildcard bind is the normal case for a container or a dev box (`HOST=0.0.0.0`
38
+ * is what this repo's own `pm` wrapper injects), and `http://0.0.0.0:<port>` is
39
+ * not an address to connect TO. Node happens to resolve it to loopback on Linux
40
+ * and fails on some other stacks, so the file records loopback explicitly rather
41
+ * than relying on that. A real interface address is left alone: someone who
42
+ * bound `192.168.1.10` meant it, and that address reaches the server from here
43
+ * as well as from anywhere else.
44
+ *
45
+ * IPv6 gets brackets, because `new URL('http://::1:7233')` is not a URL.
46
+ */
47
+ export function runtimeUrl(host, port) {
48
+ const loopback = {
49
+ "0.0.0.0": "127.0.0.1",
50
+ "::": "::1",
51
+ "": "127.0.0.1",
52
+ };
53
+ const h = loopback[host] ?? host;
54
+ return `http://${h.includes(":") ? `[${h}]` : h}:${String(port)}`;
55
+ }
12
56
  export async function start() {
13
- const { app, cfg, close } = await buildApp();
57
+ const { app, cfg, close, hub } = await buildApp();
14
58
  // Nothing else rotates the service log: the supervisor appends to it through
15
59
  // its own descriptor, a `systemd --user` unit writing to a file gets no
16
60
  // journal rotation, and macOS has nothing that would (SPEC §7). A no-op on a
@@ -20,13 +64,76 @@ export async function start() {
20
64
  app.log.warn(message);
21
65
  },
22
66
  });
23
- const shutdown = async () => {
67
+ /**
68
+ * Exit on SIGTERM/SIGINT having said what was cut short (SPEC §4, §8).
69
+ *
70
+ * Three things are deliberate here.
71
+ *
72
+ * **It records rather than finishes.** A chat turn is a `claude` subprocess
73
+ * that can run for minutes; no 20-second stop timeout can wait for one, and
74
+ * `herdctl.stop()` ends the fleet with `waitForJobs: false` precisely because
75
+ * waiting is not an option. So the contract's other half applies — record the
76
+ * work under way — and the record is this `warn` line in the service log,
77
+ * which is the one artefact that survives the restart and which
78
+ * `paddock service logs` reads. What is NOT done, and should not be, is
79
+ * writing a note into the cut-short chat's transcript: `claude` owns that file
80
+ * and may append to it after we are gone, so a server-side write cannot be
81
+ * ordered against the subprocess's own and would be the resurrection race
82
+ * `turn-interlock.ts` exists to avoid.
83
+ *
84
+ * The reason a count is enough: `/api/busy` reports the same numbers BEFORE
85
+ * the stop, and `paddock service stop` refuses on them. The log line is for
86
+ * the race §4 admits — work that started between the check and the signal.
87
+ *
88
+ * **It is idempotent.** A supervisor that sends SIGTERM twice, or a Ctrl-C
89
+ * after a SIGTERM, used to re-enter `close()` concurrently: a second
90
+ * `app.close()` on a closing Fastify rejects, and a second `fleet.stop()`
91
+ * races the first. The guard makes the extra signals no-ops.
92
+ *
93
+ * **It exits 0.** The unit's `Restart=always` / `KeepAlive` means the
94
+ * supervisor brings Paddock back whatever the code; 0 is what makes a logout
95
+ * or a `service stop` read as a clean stop rather than a crash in the logs.
96
+ */
97
+ let shuttingDown = false;
98
+ const shutdown = async (signal) => {
99
+ if (shuttingDown)
100
+ return;
101
+ shuttingDown = true;
102
+ const running = hub.runningSessions().length;
103
+ app.log.warn(running > 0
104
+ ? `${signal}: shutting down with ${running} chat turn(s) in flight — they are being cut short. ` +
105
+ "Run `paddock busy` before a stop or an update to see this before it happens."
106
+ : `${signal}: shutting down, no chat turn in flight.`);
24
107
  stopRotation();
25
- await close();
108
+ // Published once we were listening; removed here so a `busy` or a
109
+ // `service status` run after this exit does not find a live-looking
110
+ // instance. Before the slow awaits, because this is the part that must land
111
+ // even if they overrun the deadline below.
112
+ removeRuntimeFile(cfg.dataDir);
113
+ // Bounded: see SHUTDOWN_DEADLINE_MS. `exit(0)` either way, so a hung
114
+ // `app.close()` cannot turn a stop into a SIGKILL.
115
+ await Promise.race([close(), new Promise((r) => setTimeout(r, SHUTDOWN_DEADLINE_MS))]).catch(() => undefined);
26
116
  process.exit(0);
27
117
  };
28
- process.on("SIGINT", shutdown);
29
- process.on("SIGTERM", shutdown);
118
+ process.on("SIGINT", () => void shutdown("SIGINT"));
119
+ process.on("SIGTERM", () => void shutdown("SIGTERM"));
30
120
  await app.listen({ port: cfg.port, host: cfg.host });
121
+ // `<dataDir>/runtime.json`, 0600, written only once we are actually listening
122
+ // (SPEC §4) — it is how `paddock busy`, and later `update`, find THIS instance
123
+ // without guessing a port. A failure to write it is logged and not fatal: the
124
+ // server is up and serving, and the one thing that stops working is a CLI
125
+ // finding it, which is better than refusing to run.
126
+ try {
127
+ writeRuntimeFile(cfg.dataDir, {
128
+ pid: process.pid,
129
+ port: cfg.port,
130
+ url: runtimeUrl(cfg.host, cfg.port),
131
+ version: PADDOCK_VERSION,
132
+ });
133
+ }
134
+ catch (err) {
135
+ app.log.warn(`could not write ${cfg.dataDir}/runtime.json (${err.message}) — ` +
136
+ "`paddock busy` will report this instance as not running");
137
+ }
31
138
  app.log.info(`paddock-server listening on http://${cfg.host}:${cfg.port}`);
32
139
  }
@@ -14,6 +14,7 @@ import type { SweepService } from "./sweep.js";
14
14
  import type { PaddockConfig } from "./config.js";
15
15
  import type { DriveMode } from "./models.js";
16
16
  import type { SessionHub } from "./session-hub.js";
17
+ import type { BackgroundRegistry } from "./background-live.js";
17
18
  import type { AttachmentStore } from "./attachments.js";
18
19
  import type { QueuedMessageStore } from "./queued-message.js";
19
20
  import type { ArchiveStore } from "./archive.js";
@@ -152,6 +153,14 @@ export type FireTrigger = (slug: string, triggerName: string) => Promise<string
152
153
  export interface ChatHandlerContext {
153
154
  deps: ChatHandlerDeps;
154
155
  hub: SessionHub;
156
+ /**
157
+ * The live background-work registry (#604). Here for the same reason `hub` is:
158
+ * it is per-server state owned by the WS layer that a NON-socket caller has to
159
+ * be able to read. `GET /api/busy` is that caller — a turn can end while its
160
+ * background tasks keep running, so the hub alone would report a session with
161
+ * a live sub-agent as idle (CLI contract SPEC §4).
162
+ */
163
+ background: BackgroundRegistry;
155
164
  startAgentTurn: StartAgentTurn;
156
165
  composePreloadedPrompt: ComposePreloadedPrompt;
157
166
  fireTrigger: FireTrigger;
@@ -85,6 +85,7 @@ export function makeTurnEngine(engine) {
85
85
  const selfMcpCtx = {
86
86
  deps,
87
87
  hub,
88
+ background,
88
89
  startAgentTurn,
89
90
  composePreloadedPrompt: triggers.composePreloadedPrompt,
90
91
  fireTrigger: triggers.fireTrigger,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paddock/server",
3
- "version": "0.80.0",
3
+ "version": "0.81.0",
4
4
  "private": true,
5
5
  "type": "module"
6
6
  }