orchbee 0.1.0-beta.1 → 0.1.0-beta.10

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 (2) hide show
  1. package/lib/launcher.mjs +126 -9
  2. package/package.json +2 -2
package/lib/launcher.mjs CHANGED
@@ -2,7 +2,8 @@
2
2
  // sub-package; on each start this finds it, installs it into the per-user store through the carried
3
3
  // program's own `install --no-path` when the store has no active version or a lower one, and then runs
4
4
  // the store's active program in place of itself. The store's pointer `active.json` alone names the
5
- // active version. Nothing runs at install time.
5
+ // active version. On Windows, where automatic updates run, it follows the relaunch protocol of
6
+ // `apps/orchbee-launcher` with the same rule. Nothing runs at install time.
6
7
  //
7
8
  // The launcher is plain JavaScript that runs on the person's own Node, so it uses only Node built-ins
8
9
  // available since Node 18 and reads nothing beyond its own package, the sub-package, and the pointer.
@@ -25,6 +26,25 @@ const maximumPointer = 64 * 1024;
25
26
  const busyWaitMilliseconds = 600_000;
26
27
  const busyRetryMilliseconds = 250;
27
28
 
29
+ // The relaunch protocol, the same as `apps/orchbee-launcher/src/relaunch.rs`: the variable carries the
30
+ // launcher's process id, and a Client uses the statuses below only when that process is its parent.
31
+ const relaunchVariable = 'ORCHBEE_RELAUNCH_V1';
32
+ const relaunchStatus = 75;
33
+ // A Client that could not join its Session; without the protocol it ends as a startup failure.
34
+ const joinFailedStatus = 76;
35
+ const startupFailureStatus = 1;
36
+ // Rust's status for a panic that unwinds out of `main`; no CLI exit code uses it.
37
+ const panicStatus = 101;
38
+ // Error-severity NTSTATUS values end a process that did not choose to end; STATUS_CONTROL_C_EXIT is a
39
+ // console Ctrl+C, Ctrl+Break or close.
40
+ const errorSeverity = 0xc0000000;
41
+ const consoleExitStatus = 0xc000013a;
42
+ const relaunchLimit = 3;
43
+ const relaunchWindowMilliseconds = 10_000;
44
+ // The release-build half of the local runtime's automatic-update switch, `IN_RELEASE_BUILDS` in
45
+ // `crates/local-runtime/src/automatic_updates.rs`: the change that turns one on turns on both.
46
+ const automaticUpdatesInReleaseBuilds = false;
47
+
28
48
  const platformNames = { win32: 'Windows', darwin: 'macOS', linux: 'Linux' };
29
49
  const architectureNames = { x64: 'x64', arm64: 'ARM64' };
30
50
 
@@ -256,6 +276,57 @@ function resolveProgram({ root, programName, carriedProgram, carriedVersion, tex
256
276
  }
257
277
  }
258
278
 
279
+ /**
280
+ * Whether automatic-update behaviors run for this stem, as the local runtime's switch answers for the
281
+ * same build: always for a development or test stem, and for the release stem once the switch is on.
282
+ */
283
+ export function automaticUpdatesLive(stem) {
284
+ return (
285
+ stem.startsWith('ob-dev-') || stem.startsWith('ob-test-') || automaticUpdatesInReleaseBuilds
286
+ );
287
+ }
288
+
289
+ /**
290
+ * How the protocol reads a Client's exit `code`, or null when no Client started: `relaunch`, `failed`
291
+ * for a failed start or join, or `normal` for any other end, CLI failures included.
292
+ */
293
+ export function judgeClientExit(code) {
294
+ if (code === null) return 'failed';
295
+ if (code === relaunchStatus) return 'relaunch';
296
+ if (code === joinFailedStatus || code === panicStatus) return 'failed';
297
+ // Node reports a Windows status unsigned; either sign reads as the same 32-bit value.
298
+ const status = code >>> 0;
299
+ return status >= errorSeverity && status !== consoleExitStatus ? 'failed' : 'normal';
300
+ }
301
+
302
+ /**
303
+ * The step after a start (`first`, `relaunch`, or `return` to the version a failed relaunch
304
+ * replaced) ended with `code`, given the times of the earlier relaunches: `{ step: 'end', status }`,
305
+ * `{ step: 'unavailable' }`, `{ step: 'relaunch' }`, or `{ step: 'return' }`. A relaunched Client that
306
+ * fails to start or to join returns to the one it replaced; a fourth relaunch inside ten seconds is
307
+ * refused and ends as a startup failure, so no status outside the CLI exit codes reaches the caller.
308
+ */
309
+ export function nextStart(start, code, relaunches, now) {
310
+ const verdict = judgeClientExit(code);
311
+ if (verdict === 'relaunch') {
312
+ const recent = relaunches.filter((at) => now - at < relaunchWindowMilliseconds).length;
313
+ return recent < relaunchLimit
314
+ ? { step: 'relaunch' }
315
+ : { step: 'end', status: startupFailureStatus };
316
+ }
317
+ if (verdict === 'failed' && start === 'relaunch') return { step: 'return' };
318
+ if (code === null) return { step: 'unavailable' };
319
+ return { step: 'end', status: code === joinFailedStatus ? startupFailureStatus : code };
320
+ }
321
+
322
+ /** Leaves with a program's exit status, or by the same signal, so the caller sees what it saw. */
323
+ function leave(code, signal) {
324
+ if (code !== null) process.exit(code);
325
+ process.removeAllListeners(signal);
326
+ process.kill(process.pid, signal);
327
+ process.exit(128 + (constants.signals[signal] ?? 0));
328
+ }
329
+
259
330
  /**
260
331
  * Runs `program` with this process's arguments, standard handles and environment and leaves with its
261
332
  * exit status. Console signals (Ctrl+C, Ctrl+Break, close) and terminal signals reach the program
@@ -265,13 +336,57 @@ function run(program, text) {
265
336
  const child = spawn(program, process.argv.slice(2), { stdio: 'inherit' });
266
337
  for (const signal of forwardedSignals) process.on(signal, () => child.kill(signal));
267
338
  child.on('error', (error) => fail(text.startFailed(program, error.code ?? error.message)));
268
- child.on('exit', (code, signal) => {
269
- if (code !== null) process.exit(code);
270
- // Leave by the same signal, so the caller sees what the program saw.
271
- process.removeAllListeners(signal);
272
- process.kill(process.pid, signal);
273
- process.exit(128 + (constants.signals[signal] ?? 0));
274
- });
339
+ child.on('exit', leave);
340
+ }
341
+
342
+ /**
343
+ * `run` under the relaunch protocol, on Windows only: each start receives this process's id in the
344
+ * protocol variable, and `nextStart` decides after each one. A relaunch reads the pointer again; a
345
+ * pointer that names no version counts as a relaunched Client that did not start.
346
+ */
347
+ function runWithRelaunch(program, root, programName, text) {
348
+ const environment = { ...process.env, [relaunchVariable]: String(process.pid) };
349
+ const relaunches = [];
350
+ let replaced = null;
351
+ function decide(current, start, code, error) {
352
+ const now = performance.now();
353
+ const next = nextStart(start, code, relaunches, now);
354
+ if (next.step === 'end') process.exit(next.status);
355
+ if (next.step === 'unavailable')
356
+ fail(text.startFailed(current ?? '-', error?.code ?? error?.message ?? 'ENOENT'));
357
+ if (next.step === 'return') {
358
+ startClient(replaced, 'return');
359
+ return;
360
+ }
361
+ if (relaunches.length === relaunchLimit) relaunches.shift();
362
+ relaunches.push(now);
363
+ replaced = current;
364
+ const active = readPointer(root);
365
+ startClient(
366
+ active === null ? null : path.join(root, 'versions', active, programName),
367
+ 'relaunch',
368
+ );
369
+ }
370
+ function startClient(current, start) {
371
+ if (current === null) {
372
+ decide(current, start, null, null);
373
+ return;
374
+ }
375
+ const child = spawn(current, process.argv.slice(2), { stdio: 'inherit', env: environment });
376
+ let settled = false;
377
+ child.on('error', (error) => {
378
+ if (settled) return;
379
+ settled = true;
380
+ decide(current, start, null, error);
381
+ });
382
+ child.on('exit', (code, signal) => {
383
+ if (settled) return;
384
+ settled = true;
385
+ if (code === null) leave(code, signal);
386
+ else decide(current, start, code, null);
387
+ });
388
+ }
389
+ startClient(program, 'first');
275
390
  }
276
391
 
277
392
  // Signals the console or terminal delivers to every process attached to it: the program receives
@@ -317,5 +432,7 @@ export function main() {
317
432
  carriedVersion: carried.version,
318
433
  text,
319
434
  });
320
- run(program, text);
435
+ if (process.platform === 'win32' && automaticUpdatesLive(stem))
436
+ runWithRelaunch(program, storeRoot(stem, process.platform, process.env), programName, text);
437
+ else run(program, text);
321
438
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orchbee",
3
- "version": "0.1.0-beta.1",
3
+ "version": "0.1.0-beta.10",
4
4
  "description": "A terminal that runs and orchestrates AI agents",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -18,7 +18,7 @@
18
18
  "node": ">=18"
19
19
  },
20
20
  "optionalDependencies": {
21
- "@orchbee/cli-win32-x64": "0.1.0-beta.1"
21
+ "@orchbee/cli-win32-x64": "0.1.0-beta.10"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public",