@parcha/pi-browser 0.1.0-beta.80 → 0.1.0-beta.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.1.0-beta.82 (2026-12-02)
6
+
7
+ - A run's own process no longer chooses where the server connects for its browser viewer. The runner makes the viewer's listening socket (on loopback, or on its `AGENTRUN_RUNNER_LIVE_HOST` address) and hands it to the run's process on fd 5 (`AGENTRUN_VIEWER_FD`); each hand-over names that socket's port and the runner's own host, and the process supplies only the session's tag, the viewer's path id and its token. Before, the process named the port and host itself, so code in a run could point the server's viewer check and WebSocket proxy at another service on the runner's loopback or at another listed host. <!-- changes/viewer-listener-from-runner.md -->
8
+ - The server still dials only loopback, or the hosts and IPv4 blocks in `AGENTRUN_RUNNER_HOSTS`. A row that names any other host is now refused in words on the server's log (`live_host_refused`), once per host, instead of reading as a viewer that does not answer.
9
+ - pi-browser's `createLiveView` takes `listen: { fd, host }`: it serves on a listening socket its parent made and handed down, and the viewer's link names `host` and that socket's port. The process listens on that socket once: a later live view given the same `fd` (a run's pass rebuilt after a fault) takes it over, on the same port, and the earlier view's links answer 404.
10
+ - Live view of browser and desktop computers works as before, on one machine and with runners on other hosts. Desktop and phone viewers were already registered by the runner's own listener.
11
+
5
12
  ## 0.1.0-beta.80 (2026-12-01)
6
13
 
7
14
  **Breaking:**
@@ -10,10 +10,16 @@ export type LiveViewOptions = {
10
10
  baseUrl?: string;
11
11
  /** With no `baseUrl`: the address its own server listens on, and the host the viewer's link names, for a host whose
12
12
  * viewer is reached from another machine (its own proxy, never a person's browser directly). Absent: loopback for
13
- * both. A host that listens off loopback gives `admit` too. */
13
+ * both. A host that listens off loopback gives `admit` too. `fd`, in place of `bind`: a socket already listening that
14
+ * this process was handed (its parent made it, and knows its address), served as it is; the link names `host` and
15
+ * that socket's own port. The process listens on it once: a later live view given the same `fd` takes it over from
16
+ * the one before (whose links then answer 404), and a close leaves it listening, answering 404, for the next. */
14
17
  listen?: {
15
18
  readonly bind: string;
16
19
  readonly host: string;
20
+ } | {
21
+ readonly fd: number;
22
+ readonly host: string;
17
23
  };
18
24
  /** Asked of every page request and upgrade before anything else is read of it: false is a 403. The host's own check
19
25
  * that the request comes from its own proxy (a signed header, say). Absent: every request is asked only what it is
@@ -19,6 +19,32 @@ const MAX_QUEUED_INPUT = 32;
19
19
  /** A viewer whose messages the protocol does not know: one is dropped with a notice, and past this many within the window the connection is closed. */
20
20
  const MAX_BAD_MESSAGES = 20;
21
21
  const BAD_WINDOW_MS = 10_000;
22
+ const handedSockets = new Map();
23
+ const handedSocket = (fd) => {
24
+ const known = handedSockets.get(fd);
25
+ if (known)
26
+ return known;
27
+ const made = new Promise((resolve, reject) => {
28
+ let at;
29
+ const s = createServer((req, res) => { if (!at?.route?.handle(req, res)) {
30
+ res.writeHead(404, { "Content-Length": 0 });
31
+ res.end();
32
+ } });
33
+ s.on("upgrade", (req, socket, head) => { if (!at?.route?.upgrade(req, socket, head))
34
+ socket.destroy(); });
35
+ s.once("error", reject);
36
+ s.listen({ fd }, () => {
37
+ s.unref();
38
+ at = { server: s, port: s.address().port, route: null };
39
+ resolve(at);
40
+ });
41
+ });
42
+ handedSockets.set(fd, made);
43
+ // A socket that could not be listened on is asked again by the next view, not refused from memory.
44
+ made.catch(() => { if (handedSockets.get(fd) === made)
45
+ handedSockets.delete(fd); });
46
+ return made;
47
+ };
22
48
  export function createLiveView(options) {
23
49
  const takeOver = options.readOnly ? undefined : options.takeOver;
24
50
  const helloTimeoutMs = options.helloTimeoutMs ?? 5_000;
@@ -29,6 +55,8 @@ export function createLiveView(options) {
29
55
  let origin = mounted?.origin ?? null;
30
56
  const prefix = mounted ? mounted.pathname.replace(/\/+$/, "") : "";
31
57
  let server = null;
58
+ /** The handed socket this view took (`listen.fd`), while it holds it. */
59
+ let handed = null;
32
60
  let starting = null;
33
61
  let closed = false;
34
62
  /** The opens waiting for the server, by tag: a `release` or a `close` that comes meanwhile marks them cancelled, and each open takes its own mark off when it ends, so
@@ -425,9 +453,22 @@ export function createLiveView(options) {
425
453
  viewer.gate.setViewing(false);
426
454
  unused(entry);
427
455
  };
456
+ const routes = { handle: (req, res) => handle(req, res), upgrade: (req, socket, head) => upgrade(req, socket, head) };
428
457
  const ensureServer = () => {
429
458
  if (mounted || origin)
430
459
  return Promise.resolve();
460
+ const given = options.listen;
461
+ if (given && "fd" in given) {
462
+ starting ??= handedSocket(given.fd).then((socket) => {
463
+ // A close that came while the socket was being listened on leaves it to the next view.
464
+ if (closed)
465
+ return;
466
+ socket.route = routes;
467
+ handed = socket;
468
+ origin = `http://${given.host}:${socket.port}`;
469
+ });
470
+ return starting;
471
+ }
431
472
  starting ??= new Promise((resolve, reject) => {
432
473
  const s = createServer((req, res) => { if (!handle(req, res)) {
433
474
  res.writeHead(404, { "Content-Length": 0 });
@@ -438,7 +479,7 @@ export function createLiveView(options) {
438
479
  s.once("error", reject);
439
480
  // Loopback unless the host names another address (`listen`, with its `admit`): the host that wants more mounts the
440
481
  // handler on its own server.
441
- const at = options.listen ?? { bind: LOOPBACK_HOST, host: LOOPBACK_HOST };
482
+ const at = given ?? { bind: LOOPBACK_HOST, host: LOOPBACK_HOST };
442
483
  s.listen(0, at.bind, () => { s.unref(); server = s; origin = `http://${at.host}:${s.address().port}`; resolve(); });
443
484
  });
444
485
  return starting;
@@ -530,6 +571,13 @@ export function createLiveView(options) {
530
571
  await starting.catch(() => undefined);
531
572
  for (const tag of [...byTag.keys()])
532
573
  await release(tag);
574
+ // A handed socket stays listening for the process's next view; this view's requests on it go, if it still holds it.
575
+ const socket = handed;
576
+ handed = null;
577
+ if (socket && socket.route === routes) {
578
+ socket.route = null;
579
+ socket.server.closeAllConnections();
580
+ }
533
581
  const s = server;
534
582
  server = null;
535
583
  if (s)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/pi-browser",
3
- "version": "0.1.0-beta.80",
3
+ "version": "0.1.0-beta.82",
4
4
  "description": "Your agent gets its own browser, and you can take over and hand it back.",
5
5
  "keywords": [
6
6
  "pi-durable",