@parcha/pi-browser 0.1.0-beta.42 → 0.1.0-beta.44

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,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.1.0-beta.44 (2026-11-17)
6
+
7
+ - An agent whose computer is a browser can record its runs: `computer: {"kind": "browser", ..., "record": true}` makes a video of each browser session's page, `recordings/<computer>.mp4` with one file per session, under the same caps, pause and stop rules as a desktop's or a phone's, listed in the run read's `recordings` and served by the files route. A browser with `record: true` was refused with 422 before.
8
+ - The video is the live view's own CDP screencast of the session, shared with a person's viewer (one screencast per session, never a second); each frame is stamped as it comes, so a page that sits still does not shorten the video.
9
+ - `@parcha/pi-browser`'s live view takes a host's watch of a session's frames, with or without a viewer: `liveView.watch(tag, { frame, ended })`. A viewer that joins a stream already running is sent the page, any dialog and the newest frame.
10
+
5
11
  ## 0.1.0-beta.39 (2026-11-16)
6
12
 
7
13
  - A template eval case can fake the web: `web: unavailable` in its `case.yaml` fails every `web_search` with the words a rate-limited free search answers with and every `web_fetch` as a page that never answers, and `web: <fixture file>` answers each search from the first answer whose `query` (a regular expression) matches and each page from the answer for its exact URL, else the fixture's default. No call reaches the network, the calls show in the run's timeline and the graders' tool calls, so `tool_used` counts them, and the fixture is checked whole before any run.
package/README.md CHANGED
@@ -145,7 +145,9 @@ token is 256 random bits in the fragment, so it reaches no server log, proxy or
145
145
  nothing is accepted before it. The upgrade's `Origin` must be the viewer's own origin (behind a proxy, pass the upgrade through with `Origin` intact).
146
146
  There is one authenticated viewer per session (a second is refused `viewer_in_use`); the token, the stream and the viewer's socket end at the session's release.
147
147
  The page is served with `Referrer-Policy: no-referrer`, `default-src 'self'` and `frame-ancestors` set to `hostOrigin`. The viewer may send a page only to
148
- http(s) addresses. Frames are held one at a time and acknowledged after they were sent; none is written anywhere.
148
+ http(s) addresses. Frames are held one at a time and acknowledged after they were sent; the live view writes none anywhere. A host may watch a
149
+ session's frames itself, with or without a viewer (`liveView.watch(tag, { frame, ended })`, which AgentRun's run video records): the viewer and the
150
+ watchers share the session's one screencast.
149
151
 
150
152
  Every browser tool is a model call on its session's gate (`drivingGates`, shared by default between the live view and the toolset, so you pass nothing). To use your own
151
153
  registry, pass the same one to both sides: `const gates = createDrivingGates({ idleMs: 5_000 }); createLiveView({ hostOrigin, gates }); createBrowserExtension({ ..., gates });`.
@@ -1,4 +1,4 @@
1
- export { createLiveView, type LiveViewHost, type LiveViewOptions, type TakeAnswer } from "./server.js";
1
+ export { createLiveView, type LiveViewHost, type LiveViewOptions, type TakeAnswer, type Watcher } from "./server.js";
2
2
  export { liveViewProvider } from "./provider.js";
3
3
  export * from "./protocol.js";
4
4
  export { createDrivingGates, drivingGates, personDrivingFailure, PERSON_DRIVING, withModelCall, type DrivingOptions } from "../core/driving.js";
@@ -140,7 +140,7 @@ export declare function decodeFrame(message: Uint8Array): {
140
140
  /** What stream.ts gives the server: one session's page, shown and driven over its browser's DevTools socket. */
141
141
  export type StreamHandlers = {
142
142
  /** The latest frame only. `ack` tells the browser to send the next: call it after the frame was sent to the viewer, or at once to drop it
143
- * (the viewer is slow). No frame is kept after the handler returns, and none is ever written anywhere. */
143
+ * (the viewer is slow). No frame is kept after the handler returns, and the stream writes none anywhere. */
144
144
  frame(jpeg: Buffer, page: {
145
145
  w: number;
146
146
  h: number;
@@ -71,6 +71,11 @@ export type LiveViewHost = {
71
71
  known(tag: string): LiveView | null;
72
72
  /** True while a person is driving this session (viewer input within the gate's idle window). */
73
73
  driving(tag: string): boolean;
74
+ /** The host watches the session's screen with or without a viewer (serve's run video): one screencast per session, the
75
+ * viewer's and the watchers' alike, never a second. Each frame reaches `watcher.frame` and is acked by the viewer's send,
76
+ * or at once with none; `ended` once if the browser's socket ends unasked (a release ends the watch with no call). The
77
+ * answer stops watching; null when the session has no view. */
78
+ watch(tag: string, watcher: Watcher): (() => void) | null;
74
79
  /** The token is dead, the viewer's socket closed, the stream and the gate ended. Idempotent. */
75
80
  release(tag: string): Promise<void>;
76
81
  /** What the host holds, for diagnostics and tests: the sessions with a viewer URL and the opens in flight. Neither grows with the sessions it once released. */
@@ -81,4 +86,12 @@ export type LiveViewHost = {
81
86
  /** Releases every session and stops the server it started, if any. */
82
87
  close(): Promise<void>;
83
88
  };
89
+ /** A host's watch of a session's frames (LiveViewHost.watch). */
90
+ export type Watcher = {
91
+ frame(jpeg: Buffer, page: {
92
+ w: number;
93
+ h: number;
94
+ }): void;
95
+ ended(): void;
96
+ };
84
97
  export declare function createLiveView(options: LiveViewOptions): LiveViewHost;
@@ -136,26 +136,10 @@ export function createLiveView(options) {
136
136
  if (entry.viewer)
137
137
  return refuse(conn, "viewer_in_use");
138
138
  let viewer;
139
+ const running = entry.stream !== null;
139
140
  try {
140
141
  const gate = (options.gates ?? drivingGates)(entry.tag);
141
- const stream = (options.stream ?? openStream)(entry.cdpUrl, {
142
- frame(jpeg, page, ack) {
143
- if (conn.closed || conn.buffered > SLOW_VIEWER_BYTES) {
144
- ack();
145
- return;
146
- }
147
- if (!conn.send(encodeFrame(jpeg, page.w, page.h), ack))
148
- ack();
149
- },
150
- page: (page) => send(conn, { type: "page", url: page.url, title: page.title }),
151
- noPage: () => send(conn, { type: "no_page" }),
152
- dialog: (dialog) => send(conn, dialog ? { type: "dialog", ...dialog } : { type: "dialog_closed" }),
153
- closed: () => { if (entry.viewer?.conn === conn) {
154
- send(conn, { type: "ended", reason: "browser_closed" });
155
- conn.close(CLOSE_CODES.browser_closed, "browser_closed");
156
- } },
157
- });
158
- viewer = { conn, stream, gate, unsubscribe: gate.onChange((drive) => send(conn, { type: "status", drive })), chain: Promise.resolve(), bad: [], queued: 0, tail: null };
142
+ viewer = { conn, stream: streamOf(entry), gate, unsubscribe: gate.onChange((drive) => send(conn, { type: "status", drive })), chain: Promise.resolve(), bad: [], queued: 0, tail: null };
159
143
  }
160
144
  catch {
161
145
  conn.close(1011, "unavailable");
@@ -169,6 +153,75 @@ export function createLiveView(options) {
169
153
  else if (!options.readOnly)
170
154
  viewer.gate.setViewing(true);
171
155
  send(conn, { type: "ready", drive: viewer.gate.state(), ...(options.readOnly ? { readOnly: true } : takeOver ? { takeOver: true, held: isHolding(viewer.gate) } : {}) });
156
+ // Joining a watcher's stream: what it last said, its newest frame too (already acked).
157
+ if (!running)
158
+ return;
159
+ const { page, dialog, frame } = entry.shown;
160
+ for (const said of [page, dialog])
161
+ if (said)
162
+ send(conn, said);
163
+ if (frame)
164
+ conn.send(encodeFrame(frame.jpeg, frame.page.w, frame.page.h));
165
+ };
166
+ /** The session's one stream, opened at its first viewer or watcher: each frame to every watcher, then to the viewer,
167
+ * whose send acks it (or at once, with no viewer or a slow one); the rest to the viewer alone. Kept in `shown` while it is
168
+ * open, for a viewer that joins later. */
169
+ const streamOf = (entry) => (entry.stream ??= (options.stream ?? openStream)(entry.cdpUrl, {
170
+ frame(jpeg, page, ack) {
171
+ for (const watcher of entry.watchers)
172
+ try {
173
+ watcher.frame(jpeg, page);
174
+ }
175
+ catch { /* a watcher's failure never holds the frame's ack */ }
176
+ entry.shown.frame = { jpeg, page };
177
+ const conn = entry.viewer?.conn;
178
+ if (!conn || conn.closed || conn.buffered > SLOW_VIEWER_BYTES) {
179
+ ack();
180
+ return;
181
+ }
182
+ if (!conn.send(encodeFrame(jpeg, page.w, page.h), ack))
183
+ ack();
184
+ },
185
+ page: (page) => tell(entry, "page", { type: "page", url: page.url, title: page.title }),
186
+ noPage: () => {
187
+ // The closed tab's picture goes with it: a viewer that joins now is told no page, and shown none.
188
+ delete entry.shown.frame;
189
+ tell(entry, "page", { type: "no_page" });
190
+ },
191
+ dialog: (dialog) => tell(entry, "dialog", dialog ? { type: "dialog", ...dialog } : { type: "dialog_closed" }),
192
+ closed: () => {
193
+ const conn = entry.viewer?.conn;
194
+ entry.stream = null;
195
+ entry.shown = {};
196
+ if (conn) {
197
+ send(conn, { type: "ended", reason: "browser_closed" });
198
+ conn.close(CLOSE_CODES.browser_closed, "browser_closed");
199
+ }
200
+ const watchers = [...entry.watchers];
201
+ entry.watchers.clear();
202
+ for (const watcher of watchers)
203
+ try {
204
+ watcher.ended();
205
+ }
206
+ catch { /* each watcher is told */ }
207
+ },
208
+ }));
209
+ const tell = (entry, key, message) => {
210
+ entry.shown[key] = message;
211
+ if (entry.viewer)
212
+ send(entry.viewer.conn, message);
213
+ };
214
+ /** The stream ends once neither a viewer nor a watcher has it. */
215
+ const unused = (entry) => {
216
+ if (entry.viewer || entry.watchers.size > 0 || !entry.stream)
217
+ return;
218
+ const stream = entry.stream;
219
+ entry.stream = null;
220
+ entry.shown = {};
221
+ try {
222
+ stream.close();
223
+ }
224
+ catch { /* the stream is gone either way */ }
172
225
  };
173
226
  const onViewerMessage = (entry, viewer, data, binary) => {
174
227
  const message = binary || typeof data !== "string" ? null : parseClientMessage(data);
@@ -370,10 +423,7 @@ export function createLiveView(options) {
370
423
  viewer.gate.setPresent?.(false);
371
424
  else if (!options.readOnly)
372
425
  viewer.gate.setViewing(false);
373
- try {
374
- viewer.stream.close();
375
- }
376
- catch { /* the stream is gone either way */ }
426
+ unused(entry);
377
427
  };
378
428
  const ensureServer = () => {
379
429
  if (mounted || origin)
@@ -409,6 +459,9 @@ export function createLiveView(options) {
409
459
  entry.pending.clear();
410
460
  const viewer = entry.viewer;
411
461
  endViewer(entry);
462
+ // Its watchers stop with no call: the host that released it ends what it watches for.
463
+ entry.watchers.clear();
464
+ unused(entry);
412
465
  // The gate is the session's, so it ends with it even when no viewer ever took it.
413
466
  (options.gates ?? drivingGates)(tag).end();
414
467
  void viewer;
@@ -440,7 +493,7 @@ export function createLiveView(options) {
440
493
  const raced = byTag.get(tag);
441
494
  if (raced?.view)
442
495
  return raced.view;
443
- const entry = { tag, id: newViewerId(), token: newToken(), cdpUrl, view: null, released: false, viewer: null, pending: new Set() };
496
+ const entry = { tag, id: newViewerId(), token: newToken(), cdpUrl, view: null, released: false, viewer: null, pending: new Set(), stream: null, watchers: new Set(), shown: {} };
444
497
  const url = `${origin}${prefix}/v/${entry.id}#t=${entry.token}`;
445
498
  entry.view = { fullscreen: url, framed: url, pages: [] };
446
499
  byTag.set(tag, entry);
@@ -450,6 +503,21 @@ export function createLiveView(options) {
450
503
  known: (tag) => { const entry = byTag.get(tag); return entry && !entry.released ? entry.view : null; },
451
504
  driving: (tag) => { const entry = byTag.get(tag); if (!entry)
452
505
  return false; const gate = (options.gates ?? drivingGates)(tag); return isHeld(gate) || (!!entry.viewer && gate.state() === "person"); },
506
+ watch(tag, watcher) {
507
+ const entry = byTag.get(tag);
508
+ if (!entry || entry.released)
509
+ return null;
510
+ entry.watchers.add(watcher);
511
+ try {
512
+ streamOf(entry);
513
+ }
514
+ catch {
515
+ entry.watchers.delete(watcher);
516
+ return null;
517
+ }
518
+ return () => { if (entry.watchers.delete(watcher))
519
+ unused(entry); };
520
+ },
453
521
  tracked: () => ({ sessions: byTag.size, opening: [...opening.values()].reduce((n, marks) => n + marks.size, 0) }),
454
522
  release,
455
523
  async close() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/pi-browser",
3
- "version": "0.1.0-beta.42",
3
+ "version": "0.1.0-beta.44",
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",