The best way to control Claude Code, Codex and more from any computer or phone

main
ts 3,547 lines 136 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off globalDate:off globalTimers:off - Playwright callbacks run outside the Effect runtime.
2 // Screencasts ignore emulated device scale; real 2x keeps captures sharp.
3 // --disable-gpu uses cheaper software compositing while preserving SwiftShader WebGL.
4 import {
5 BrowserProfile,
6 BrowserProfileId,
7 BUILT_IN_BROWSER_PROFILES,
8 DEFAULT_BROWSER_PROFILE_ID,
9 DEFAULT_BROWSER_RECORDING_FRAME_RATE,
10 FILL_PREVIEW_VIEWPORT,
11 findBrowserProfile,
12 INCOGNITO_BROWSER_PROFILE_ID,
13 isBuiltInBrowserProfileId,
14 resolveBrowserProfiles,
15 type PreviewReportProfilesInput,
16 type PreviewReportStatusInput,
17 PREVIEW_AUTOMATION_SERVER_OPERATIONS,
18 PreviewViewportSetting as PreviewViewportSettingSchema,
19 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
20 type PreviewAutomationActionEvent,
21 type PreviewAutomationClickInput,
22 type PreviewAutomationDialogInput,
23 type PreviewAutomationConsoleEntry,
24 type PreviewAutomationDragInput,
25 type PreviewAutomationEvaluateInput,
26 type PreviewAutomationHoverInput,
27 type PreviewAutomationLoadError,
28 type PreviewAutomationNavigateInput,
29 type PreviewAutomationNetworkEntry,
30 type PreviewAutomationOpenInput,
31 type PreviewAutomationPressInput,
32 type PreviewAutomationRequest,
33 type PreviewAutomationResizeInput,
34 type PreviewAutomationScrollInput,
35 type PreviewAutomationSelectInput,
36 type PreviewAutomationSetColorSchemeInput,
37 type PreviewAutomationSnapshotInput,
38 type PreviewAutomationStatus,
39 type PreviewAutomationTypeInput,
40 type PreviewAutomationUploadInput,
41 type PreviewAutomationWaitForInput,
42 PreviewClearProfileError,
43 type PreviewEvent,
44 type PreviewNavStatus,
45 type PreviewSessionSnapshot,
46 type PreviewViewportSetting,
47 ThreadId,
48 SERVER_BROWSER_AUTOMATION_CLIENT_ID,
49 type PreviewAppearancePreference,
50 } from "@t3tools/contracts";
51 import { AgentScope, agentScopeLauncherScript } from "@t3tools/shared/AgentScope";
52 import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
53 import * as HostProcess from "@t3tools/shared/HostProcess";
54 import { normalizePreviewUrl } from "@t3tools/shared/preview";
55 import { resolvePreviewViewport } from "@t3tools/shared/previewViewport";
56 import * as NodeCrypto from "node:crypto";
57 import * as NodeFSP from "node:fs/promises";
58 import * as NodePath from "node:path";
59 import * as NodeUtil from "node:util";
60 import * as Context from "effect/Context";
61 import * as Effect from "effect/Effect";
62 import * as FileSystem from "effect/FileSystem";
63 import { constVoid } from "effect/Function";
64 import * as Layer from "effect/Layer";
65 import * as Option from "effect/Option";
66 import type * as Path from "effect/Path";
67 import * as Queue from "effect/Queue";
68 import * as Schedule from "effect/Schedule";
69 import * as Schema from "effect/Schema";
70 import * as Semaphore from "effect/Semaphore";
71 import * as Exit from "effect/Exit";
72 import * as Scope from "effect/Scope";
73 import * as Stream from "effect/Stream";
74 import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
75 import type {
76 BrowserContext,
77 CDPSession,
78 Dialog,
79 Download,
80 FileChooser,
81 Frame,
82 Page,
83 } from "playwright-core";
84
85 import { PENDING_ATTACHMENT_THREAD_SEGMENT } from "../attachmentStore.ts";
86 import * as ServerConfig from "../config.ts";
87 import { resolveRootCliCommand } from "../cli/invocation.ts";
88 import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
89 import * as PreviewAutomationBroker from "../mcp/PreviewAutomationBroker.ts";
90 import * as DesktopBrowserChannel from "./DesktopBrowserChannel.ts";
91 import * as PreviewAgentActivity from "./PreviewAgentActivity.ts";
92 import * as PreviewManager from "./Manager.ts";
93 import * as ServerBrowserPage from "./ServerBrowserPage.ts";
94 import * as PreviewBrowser from "./PreviewBrowser.ts";
95 import * as PreviewBrowserHost from "./PreviewBrowserHost.ts";
96 import { presentAsChrome, ServerBrowserContexts } from "./ServerBrowserContexts.ts";
97 import { BrowserControlInterrupted, SessionControl } from "./SessionControl.ts";
98 import { installPdfDocumentNavigation, isPdfAssetUrl } from "./PdfDocumentNavigation.ts";
99
100 const SERVER_HOST_CLIENT_ID = SERVER_BROWSER_AUTOMATION_CLIENT_ID;
101 // Each client's report is capped, but the saved catalog is their union, so it has no cap.
102 const SavedProfilesJson = Schema.fromJsonString(
103 Schema.Struct({ profiles: Schema.Array(BrowserProfile), defaultProfileId: BrowserProfileId }),
104 );
105 const decodeSavedProfiles = Schema.decodeUnknownEffect(SavedProfilesJson);
106 const encodeSavedProfiles = Schema.encodeSync(SavedProfilesJson);
107 const RENDER_SCALE = 2;
108 // Chromium allows three unacked frames, so 100 ms pacing caps viewers near 30 fps.
109 const SCREENCAST_ACK_PACE_MS = 100;
110 const SCREENCAST_SETTLE_MS = 200;
111 const SCREENCAST_MOTION_FRAMES = 4;
112 const SCREENCAST_MOTION_WINDOW_MS = 300;
113 const SCREENCAST_MOTION_QUALITY = 50;
114 const HOST_RECONNECT_DELAY = "1 second";
115 const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
116 // A viewer still is best-effort; a capture Chromium never answers must not hold the capture lock.
117 const VIEWER_STILL_TIMEOUT_MS = 2_000;
118 const VIEWER_OUTPUT_LIMIT = 64;
119 const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const;
120 const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024;
121 const RECORDING_DOWNLOAD_TIMEOUT_MS = 5_000;
122 const decodeViewportSetting = Schema.decodeUnknownSync(PreviewViewportSettingSchema);
123 const AGENT_CURSOR_MOVE_MS = 160;
124 const AGENT_CURSOR_CLICK_LEAD_MS = 40;
125 /** A dialog blocks page script, so a desktop size read gives up after this. */
126 const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second";
127 /** A desktop page that keeps its size draws nothing on a resize, so the wait gives up. */
128 const DESKTOP_RESIZE_WAIT_MS = 1000;
129 /** Answered only once the guest paints a frame; one pixel keeps it cheap. */
130 const DESKTOP_PAINT_CAPTURE = {
131 format: "jpeg",
132 quality: 1,
133 clip: { x: 0, y: 0, width: 1, height: 1, scale: 1 },
134 } as const;
135 // A revealed tab's client opens the panel and attaches its stream a few round trips later, remote ones included.
136 const REVEAL_VIEWER_WAIT_MS = 1000;
137 // A desktop panel's shown report waits on the renderer's layout, which a busy machine can push past a second.
138 const REVEAL_DESKTOP_SHOWN_WAIT_MS = 3000;
139
140 const sleepUntil = (deadline: number) =>
141 new Promise<void>((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now())));
142
143 const awaitViewer = async (tab: ServerTab, timeoutMs: number) => {
144 const attached = (tab.viewerAttached ??= Promise.withResolvers());
145 let timer: ReturnType<typeof setTimeout> | undefined;
146 await Promise.race([
147 attached.promise,
148 new Promise<void>((resolve) => {
149 timer = setTimeout(resolve, Math.max(0, timeoutMs));
150 }),
151 ]);
152 clearTimeout(timer);
153 };
154
155 const EDITABLE_AT_POINT_SCRIPT = `(x, y) => {
156 let element = document.elementFromPoint(x, y);
157 while (element && element.shadowRoot) {
158 const inner = element.shadowRoot.elementFromPoint(x, y);
159 if (!inner || inner === element) break;
160 element = inner;
161 }
162 if (element && element.tagName === "LABEL" && element.control) element = element.control;
163 if (!element) return false;
164 if (element.tagName === "IFRAME" || element.tagName === "FRAME") return true;
165 if (element.isContentEditable) return true;
166 if (element.tagName === "TEXTAREA") return !element.disabled && !element.readOnly;
167 if (element.tagName !== "INPUT") return false;
168 const nonText = ["button", "checkbox", "color", "file", "hidden", "image", "radio", "range", "reset", "submit"];
169 return !nonText.includes(element.type) && !element.disabled && !element.readOnly;
170 }`;
171 const UNATTACHED_FILL_VIEWPORT = { width: 1280, height: 800 } as const;
172 const NAVIGATION_TIMEOUT_MS = 15_000;
173 // Status reads run in the renderer; a busy page must not hold a status past the broker's grace.
174 const STATUS_READ_MS = 250;
175 const VIEWER_NAVIGATION_OPTIONS = { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS } as const;
176 const ACTION_TIMELINE_LIMIT = 50;
177
178 export class ServerBrowserTabNotFoundError extends Schema.TaggedError<ServerBrowserTabNotFoundError>()(
179 "ServerBrowserTabNotFoundError",
180 { threadId: Schema.String, tabId: Schema.String },
181 ) {
182 override get message(): string {
183 return "The server preview tab does not exist.";
184 }
185 }
186
187 const isTabNotFound = Schema.is(ServerBrowserTabNotFoundError);
188 const isHostSetupError = Schema.is(
189 Schema.Union([
190 PreviewBrowserHost.PreviewBrowserSandboxError,
191 PreviewBrowserHost.PreviewBrowserLibrariesError,
192 PreviewBrowserHost.PreviewBrowserRootError,
193 ]),
194 );
195
196 class ServerBrowserDownloadsCleanupError extends Schema.TaggedError<ServerBrowserDownloadsCleanupError>()(
197 "ServerBrowserDownloadsCleanupError",
198 { cause: Schema.Defect() },
199 ) {
200 override get message(): string {
201 return "The server preview could not clear earlier downloads.";
202 }
203 }
204
205 export class ServerBrowserLaunchError extends Schema.TaggedError<ServerBrowserLaunchError>()(
206 "ServerBrowserLaunchError",
207 { cause: Schema.Defect() },
208 ) {
209 override get message(): string {
210 return "The server preview browser could not start.";
211 }
212 }
213
214 export type ServerBrowserViewerOutput =
215 | {
216 readonly _tag: "frame";
217 readonly data: Uint8Array;
218 readonly ack: Effect.Effect<void>;
219 }
220 | { readonly _tag: "viewport"; readonly width: number; readonly height: number }
221 | {
222 readonly _tag: "control";
223 readonly canOperate: boolean;
224 readonly controller: "agent" | "you" | "another-viewer" | "unclaimed";
225 readonly generation: number;
226 readonly dialog: {
227 readonly type: string;
228 readonly message: string;
229 readonly defaultValue: string;
230 } | null;
231 }
232 | {
233 readonly _tag: "probe";
234 readonly x: number;
235 readonly y: number;
236 readonly editable: boolean;
237 }
238 | { readonly _tag: "clipboard"; readonly text: string }
239 | {
240 readonly _tag: "pointer";
241 readonly phase: "move" | "click";
242 readonly x: number;
243 readonly y: number;
244 readonly sequence: number;
245 }
246 | {
247 readonly _tag: "fileChooser";
248 readonly id: string;
249 readonly multiple: boolean;
250 readonly accept: string;
251 }
252 | { readonly _tag: "fileChooserClosed"; readonly id: string }
253 | { readonly _tag: "popup"; readonly tabId: string }
254 | {
255 readonly _tag: "download";
256 readonly id: string;
257 readonly fileName: string;
258 readonly sizeBytes: number;
259 }
260 | { readonly _tag: "gone" }
261 | { readonly _tag: "reconnect" };
262
263 export interface ServerBrowserViewer {
264 readonly output: Queue.Dequeue<ServerBrowserViewerOutput>;
265 readonly input: (message: unknown) => Effect.Effect<void>;
266 }
267
268 export type ProfileReporter = (input: PreviewReportProfilesInput) => Effect.Effect<void>;
269
270 export class ServerBrowser extends Context.Service<
271 ServerBrowser,
272 {
273 readonly attachViewer: (input: {
274 readonly threadId: string;
275 readonly tabId: string;
276 readonly maxWidth: number;
277 readonly maxHeight: number;
278 readonly quality: number;
279 readonly canOperate: boolean;
280 }) => Effect.Effect<
281 ServerBrowserViewer,
282 ServerBrowserTabNotFoundError | ServerBrowserLaunchError,
283 Scope.Scope
284 >;
285
286 readonly answerFileChooser: (input: {
287 readonly threadId: string;
288 readonly tabId: string;
289 readonly chooserId: string;
290 readonly files: ReadonlyArray<{
291 readonly name: string;
292 readonly mimeType: string;
293 readonly buffer: Buffer;
294 }>;
295 }) => Effect.Effect<boolean>;
296 readonly openDownload: (input: {
297 readonly threadId: string;
298 readonly tabId: string;
299 readonly downloadId: string;
300 }) => Effect.Effect<Option.Option<{ readonly path: string; readonly fileName: string }>>;
301
302 readonly clearProfile: (profileId: string) => Effect.Effect<void, PreviewClearProfileError>;
303
304 readonly profileReporter: Effect.Effect<ProfileReporter, never, Scope.Scope>;
305 }
306 >()("t3/preview/ServerBrowser") {}
307
308 interface ViewerState {
309 readonly id: string;
310 readonly canOperate: boolean;
311 readonly pressedKeys: Map<string, { key: string; code: string }>;
312 readonly pressedButtons: Map<"left" | "middle" | "right", { x: number; y: number }>;
313 readonly push: (output: ServerBrowserViewerOutput) => void;
314 readonly pause: () => Promise<void>;
315 readonly resume: () => Promise<void>;
316 scrolledAt: number;
317
318 requestedSize: { width: number; height: number; order: number } | null;
319 }
320
321 interface EncoderWindow {
322 __t3Recorder: {
323 cursor(x: number, y: number, click: boolean): void;
324 frame(data: string, width: number): Promise<boolean>;
325 stop(): Promise<{ mimeType: string | null; count: number; bytes: number }>;
326 chunk(index: number): Promise<string>;
327 download(): void;
328 };
329 }
330
331 interface Recording {
332 readonly encoder: Page;
333 readonly session: CDPSession;
334 readonly startedAt: string;
335 readonly framesInFlight: Set<Promise<void>>;
336 }
337
338 interface ServerTab {
339 readonly threadId: ThreadId;
340 readonly tabId: string;
341 readonly page: Page;
342 readonly cdp: CDPSession;
343 readonly frameSessions: Map<Frame, CDPSession>;
344 /** Copy reads run in key order, outside the input queue. */
345 copyReads: Promise<void>;
346 /** When the last copy or cut key was acknowledged; the next one starts after it. */
347 copyKeyedUntil: number;
348 readonly createdAt: number;
349 usedAt: number;
350 readonly viewers: Set<ViewerState>;
351 viewerAttached: PromiseWithResolvers<void> | null;
352 readonly consoleEntries: Array<PreviewAutomationConsoleEntry>;
353 readonly networkEntries: Array<PreviewAutomationNetworkEntry>;
354 readonly actionTimeline: Array<PreviewAutomationActionEvent>;
355 readonly control: SessionControl;
356 agentOperating: boolean;
357 fileChooserIntercepted: boolean;
358 readonly onFileChooser: (chooser: FileChooser) => void;
359
360 readonly isolatedContext: boolean;
361
362 readonly desktop: { readonly close: () => Promise<void> } | null;
363 readonly profileId: string | undefined;
364 readonly openerTabId: string | undefined;
365
366 readonly downloads: Array<ServerDownload>;
367 fileChooser: {
368 readonly id: string;
369 readonly chooser: FileChooser;
370 readonly accept: string;
371 readonly offeredTo: string | null;
372 } | null;
373 dialog: Dialog | null;
374 setting: PreviewViewportSetting;
375 colorScheme: PreviewAppearancePreference;
376 zoomFactor: number;
377 /** Last usable CSS size measured on the desktop. */
378 desktopViewport: { readonly width: number; readonly height: number } | null;
379 desktopViewportRead: Promise<ServerTab["desktopViewport"]>;
380 /** A read queued behind `desktopViewportRead`, shared by every caller that waits for it. */
381 desktopViewportNext: Promise<ServerTab["desktopViewport"]> | null;
382 /** A page resize or load since the last read started; status measures again. */
383 desktopViewportStale: boolean;
384 /** Counts page resizes, loads and dialog closes, so a caller can tell one landed during a read. */
385 desktopViewportChanges: number;
386 /** Settles at the desktop page's next main-frame resize. */
387 desktopResize: PromiseWithResolvers<void>;
388 loading: boolean;
389 loadError: PreviewAutomationLoadError | undefined;
390 /** The renderer crash this page died of, or the one the page it replaced died of. */
391 crash: PreviewAutomationLoadError | undefined;
392 /** What Reload loads after a crash; the reported URL is shortened and drops data payloads. */
393 crashedUrl: string | undefined;
394 closing: boolean;
395 recording: Recording | null;
396 initialNavigation: Promise<void> | null;
397 pendingInitialRequestUrl: string | null;
398 navigationGeneration: number;
399 stoppedGeneration: number | null;
400 /** The reported title a watch in the main document's isolated world waits to change from. */
401 titleWatch: { readonly contextId: number; readonly title: string } | null;
402 lastReport: PreviewReportStatusInput | null;
403
404 abortedNavigation: {
405 readonly url: string;
406 readonly generation: number;
407 readonly timer: ReturnType<typeof setTimeout>;
408 } | null;
409
410 recordingStart: Promise<Recording> | null;
411 captureLock: Promise<void>;
412 capturing: number;
413 }
414
415 interface ServerDownload {
416 readonly id: string;
417 readonly fileName: string;
418 readonly path: string;
419 readonly sizeBytes: number;
420 readonly url: string;
421 readonly completedAt: string;
422 }
423
424 /** Read-only operations remain available during human control; evaluate requires control because scripts can mutate the page. */
425 const READ_OPERATIONS: ReadonlySet<PreviewAutomationRequest["operation"]> = new Set([
426 "status",
427 "snapshot",
428 "waitFor",
429 ]);
430
431 const AGENT_TAB_LIMIT = 8;
432 const SERVER_TAB_LIMIT = 32;
433 const AGENT_TAB_IDLE_MS = 30 * 60 * 1000;
434 const IDLE_SWEEP_INTERVAL = "1 minute";
435
436 const DOWNLOAD_LIMIT = 20;
437 const ABORTED_NAVIGATION_DOWNLOAD_WAIT_MS = 5_000;
438 const PAGE_CLOSE_GRACE_MS = 5_000;
439 const DOWNLOAD_MAX_BYTES = 1024 * 1024 * 1024;
440
441 const tabKey = (threadId: string, tabId: string) => `${threadId}\u0000${tabId}`;
442
443 const pushBounded = <A>(
444 buffer: Array<A>,
445 entry: A,
446 limit = ServerBrowserPage.DIAGNOSTIC_BUFFER_LIMIT,
447 ) => {
448 buffer.push(entry);
449 if (buffer.length > limit) buffer.splice(0, buffer.length - limit);
450 };
451
452 const viewportSettingsEqual = (left: PreviewViewportSetting, right: PreviewViewportSetting) =>
453 left._tag === right._tag &&
454 (left._tag === "fill" ||
455 (right._tag !== "fill" && left.width === right.width && left.height === right.height));
456
457 const fixedViewportSize = (setting: PreviewViewportSetting) =>
458 setting._tag === "fill" ? null : { width: setting.width, height: setting.height };
459
460 const asRecord = (value: unknown): Record<string, unknown> | null =>
461 typeof value === "object" && value !== null ? (value as Record<string, unknown>) : null;
462
463 const num = (value: unknown, fallback = 0) =>
464 typeof value === "number" && Number.isFinite(value) ? value : fallback;
465
466 const modifiersOf = (message: Record<string, unknown>) => {
467 const value = num(message.modifiers);
468 return Number.isInteger(value) && value >= 0 && value < 16 ? value : 0;
469 };
470
471 // Linux/headless Chromium needs commands for Cmd editing shortcuts and Ctrl copy/cut.
472 const editingCommand = (key: string, modifiers: number) => {
473 const modifier = modifiers & 0b0111;
474 if (modifier !== 2 && modifier !== 4) return null;
475 const lower = key.toLowerCase();
476 const command =
477 lower === "c"
478 ? "copy"
479 : lower === "x"
480 ? "cut"
481 : modifier !== 4
482 ? null
483 : lower === "a"
484 ? "selectAll"
485 : lower === "z"
486 ? modifiers & 8
487 ? "redo"
488 : "undo"
489 : null;
490 return command ? { commands: [command] } : null;
491 };
492
493 const TITLE_LIMIT = 512;
494 const TITLE_WORLD = "t3-preview-title";
495 // Observe root replacement without subscribing to body subtree mutations.
496 const titleChange = (reported: string) => `new Promise((resolve) => {
497 const changed = () => document.title.slice(0, ${TITLE_LIMIT}) !== ${JSON.stringify(reported)};
498 if (changed()) return resolve();
499 let head;
500 let root;
501 let title;
502 const observer = new MutationObserver(() => {
503 if (changed()) {
504 observer.disconnect();
505 resolve();
506 } else if (head !== document.head || root !== document.documentElement || title !== document.querySelector('title')) {
507 observe();
508 }
509 });
510 const observe = () => {
511 observer.disconnect();
512 observer.observe(document, { childList: true });
513 root = document.documentElement;
514 head = document.head;
515 title = document.querySelector('title');
516 if (root) observer.observe(root, { childList: true });
517 if (head) observer.observe(head, { subtree: true, childList: true, characterData: true });
518 if (title && !head?.contains(title)) {
519 observer.observe(title, { subtree: true, childList: true, characterData: true });
520 for (let ancestor = title.parentElement; ancestor; ancestor = ancestor.parentElement) {
521 observer.observe(ancestor, { childList: true });
522 }
523 }
524 };
525 observe();
526 })`;
527
528 const CLIPBOARD_TEXT_LIMIT = 1024 * 1024;
529 const CLIPBOARD_WORLD = "t3-preview-clipboard";
530 // Chromium shares its clipboard across contexts; capture real copy/cut keys in a page-inaccessible world instead.
531 const CLIPBOARD_SCRIPT = `(() => {
532 let keying = false;
533 // One entry per copy or cut key, oldest first, so a later key cannot overwrite an unread copy.
534 const copies = [];
535 // The same Ctrl or Cmd + C or X that editingCommand sends as copy or cut.
536 addEventListener("keydown", (event) => {
537 if (!event.isTrusted || event.altKey || event.ctrlKey === event.metaKey) return;
538 const key = event.key.toLowerCase();
539 if (key !== "c" && key !== "x") return;
540 keying = true;
541 copies.push({ at: Date.now(), text: "" });
542 setTimeout(() => { keying = false; });
543 }, true);
544 const selection = () => {
545 let field = document.activeElement;
546 while (field?.shadowRoot?.activeElement) field = field.shadowRoot.activeElement;
547 if ((field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement) && field.selectionStart !== null)
548 return field.value.slice(field.selectionStart, field.selectionEnd ?? field.selectionStart);
549 return String(document.getSelection() ?? "");
550 };
551 const record = (text) => {
552 const copy = copies.at(-1);
553 if (copy) copy.text = String(text ?? "").slice(0, ${CLIPBOARD_TEXT_LIMIT});
554 };
555 // Capturing at window runs before page handlers can stop the event, and
556 // before cut removes the selection.
557 const snapshot = (event) => {
558 if (keying && event.isTrusted) record(selection());
559 };
560 // Bubbling back to window picks up text a page handler set instead.
561 const replace = (event) => {
562 if (keying && event.isTrusted && event.defaultPrevented) record(event.clipboardData?.getData("text/plain"));
563 };
564 for (const type of ["copy", "cut"]) {
565 addEventListener(type, snapshot, true);
566 addEventListener(type, replace);
567 }
568 // A key's copy was keyed between its dispatch and acknowledgement; older copies are stale.
569 globalThis.takeCopy = (since, until) => {
570 while (copies.length > 0 && copies[0].at < since) copies.shift();
571 return copies[0] && copies[0].at <= until ? copies.shift().text : "";
572 };
573 })();`;
574
575 const make = Effect.gen(function* () {
576 const config = yield* ServerConfig.ServerConfig;
577 const host = { platform: yield* HostProcess.Platform, arch: yield* HostProcess.Architecture };
578 const manager = yield* PreviewManager.PreviewManager;
579 const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
580 const environment = yield* ServerEnvironment.ServerEnvironment;
581 const previewBrowser = yield* PreviewBrowser.PreviewBrowser;
582 const desktopChannel = yield* DesktopBrowserChannel.DesktopBrowserChannel;
583 const agentTurns = yield* (yield* PreviewAgentActivity.PreviewAgentActivity).changes;
584 const runFork = Effect.runForkWith(yield* Effect.context<never>());
585 const runPromise = Effect.runPromiseWith(yield* Effect.context<never>());
586 const runSyncExit = Effect.runSyncExitWith(yield* Effect.context<never>());
587 const launchServices = yield* Effect.context<ChildProcessSpawner.ChildProcessSpawner>();
588 // sudo resets the environment, so the command has to name this server's home.
589 const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND, [
590 "--base-dir",
591 config.baseDir,
592 ]);
593 const agentScope = yield* AgentScope;
594 const launchersDir = NodePath.join(config.stateDir, "server-browser", "launchers");
595
596 const tabs = new Map<string, ServerTab>();
597 const pendingTabs = new Map<
598 string,
599 { readonly agentId: string | null; promise?: Promise<ServerTab> }
600 >();
601
602 const closedPendingTabs = new Set<string>();
603 const adoptedPages = new Map<string, { readonly page: Page; readonly openerTabId: string }>();
604 const closedSessions = new Set<string>();
605 let hostConnectionId: string | null = null;
606 let viewerResizeOrder = 0;
607
608 const contexts = new ServerBrowserContexts({
609 profilesDir: NodePath.join(config.stateDir, "server-browser", "profiles"),
610 idleBrowserMs: 5 * 60_000,
611 executable: () => runPromise(previewBrowser.executable),
612 chrome: { version: PreviewBrowser.VERSION, platform: host.platform },
613 // An agent scope keeps Chrome out of the server's memory protection and OOM score.
614 launcher: async (executable) => {
615 const launch = await runPromise(
616 agentScope.wrap({ command: executable, args: [], name: "preview-browser" }),
617 );
618 if (launch.command === executable) return undefined;
619 await NodeFSP.mkdir(launchersDir, { recursive: true });
620 const launcher = NodePath.join(launchersDir, NodeCrypto.randomUUID());
621 await NodeFSP.writeFile(launcher, agentScopeLauncherScript(launch), { mode: 0o700 });
622 return { path: launcher, release: () => NodeFSP.rm(launcher, { force: true }) };
623 },
624 // Playwright's launch error drops Chrome's own output; its sandbox note survives.
625 diagnose: (executable, cause) =>
626 Effect.runPromiseWith(launchServices)(
627 PreviewBrowserHost.diagnoseLaunchFailure({
628 executable,
629 setupCommand,
630 output: /sandboxing failed/i.test(String(cause))
631 ? PreviewBrowserHost.NO_SANDBOX_SIGNATURE
632 : "",
633 }),
634 ),
635 onContextClose: (context) => {
636 for (const tab of tabs.values()) {
637 if (tab.page.context() === context) dropTab(tab, true);
638 }
639 },
640 });
641
642 // Playwright cannot reload a crashed page, so a fresh page takes over its tab.
643 const reopenCrashedTab = async (tab: ServerTab) => {
644 if (tab.desktop) return dropTab(tab, true);
645 const key = tabKey(tab.threadId, tab.tabId);
646 if (tabs.get(key) !== tab) return;
647 const crash = {
648 url: ServerBrowserPage.withoutDataPayload(tab.page.url()).slice(0, 2048),
649 code: -2,
650 description: "Page crashed",
651 };
652 tab.crash = crash;
653 const reopening = (async () => {
654 // The session must name the crash before a new page reads it, or it would load the URL again.
655 await runPromise(
656 manager
657 .reportStatus({
658 threadId: tab.threadId,
659 tabId: tab.tabId,
660 serverControlled: true,
661 navStatus: { _tag: "LoadFailed", title: "", ...crash },
662 canGoBack: false,
663 canGoForward: false,
664 })
665 .pipe(Effect.ignore),
666 );
667 dropTab(tab, false, true);
668 // A context that is still closing would take the new page down with it.
669 if (tab.isolatedContext) await tab.page.context().close().catch(constVoid);
670 const { sessions } = await runPromise(manager.list({ threadId: tab.threadId }));
671 const snapshot = sessions.find(
672 (session) => session.tabId === tab.tabId && session.runtime === "server",
673 );
674 if (!snapshot) {
675 throw new ServerBrowserTabNotFoundError({ threadId: tab.threadId, tabId: tab.tabId });
676 }
677 return createTab(snapshot, tab.page.url());
678 })().finally(() => {
679 pendingTabs.delete(key);
680 closedPendingTabs.delete(key);
681 });
682 // Reserved before the dead page leaves, so a lookup in between waits for its replacement.
683 pendingTabs.set(key, { agentId: tab.control.agentId, promise: reopening });
684 await reopening.catch(constVoid);
685 };
686
687 const dialogStatus = (tab: ServerTab) =>
688 tab.dialog
689 ? {
690 type: tab.dialog.type(),
691 message: tab.dialog.message().slice(0, 4000),
692 defaultValue: tab.dialog.defaultValue().slice(0, 4000),
693 }
694 : null;
695
696 const updateFileChooserInterception = (tab: ServerTab) => {
697 // Only agents and streamed controllers can answer intercepted uploads; native desktop views use the OS picker.
698 const intercept = !tab.desktop || tab.agentOperating || tab.control.controller !== null;
699 if (intercept === tab.fileChooserIntercepted) return;
700 tab.fileChooserIntercepted = intercept;
701 if (intercept) tab.page.on("filechooser", tab.onFileChooser);
702 else tab.page.off("filechooser", tab.onFileChooser);
703 };
704
705 const broadcastControl = (tab: ServerTab) => {
706 updateFileChooserInterception(tab);
707 for (const viewer of tab.viewers)
708 viewer.push({
709 _tag: "control",
710 canOperate: viewer.canOperate,
711 controller:
712 tab.control.controller === viewer.id
713 ? "you"
714 : tab.control.controller !== null
715 ? "another-viewer"
716 : tab.control.agentId !== null
717 ? "agent"
718 : "unclaimed",
719 generation: tab.control.generation,
720 dialog: dialogStatus(tab),
721 });
722 };
723
724 const resolveDialog = async (tab: ServerTab, input: PreviewAutomationDialogInput) => {
725 const dialog = tab.dialog;
726 if (!dialog) throw new Error("No dialog is pending.");
727 if (input.accept) await dialog.accept(input.promptText);
728 else await dialog.dismiss();
729 if (tab.dialog === dialog) tab.dialog = null;
730 ServerBrowserPage.invalidateRefs(tab.page);
731 broadcastControl(tab);
732 };
733
734 const report = (tab: ServerTab, navStatus: PreviewNavStatus) => {
735 const generation = tab.navigationGeneration;
736 const loadError = tab.loadError;
737 void tab.cdp
738 .send("Page.getNavigationHistory")
739 .catch(() => null)
740 .then((history) => {
741 // A status the dead page still had in flight must not replace the crash.
742 if (tab.crash) return;
743 if (
744 navStatus._tag === "Success" &&
745 (tab.navigationGeneration !== generation || tab.loadError !== loadError)
746 )
747 return;
748 const index = history?.currentIndex ?? 0;
749 const count = history?.entries.length ?? 0;
750 const input = {
751 threadId: tab.threadId,
752 tabId: tab.tabId,
753 serverControlled: true,
754 navStatus,
755 canGoBack: index > 0,
756 canGoForward: index < count - 1,
757 };
758 if (NodeUtil.isDeepStrictEqual(tab.lastReport, input)) return;
759 tab.lastReport = input;
760 runFork(manager.reportStatus(input).pipe(Effect.ignore));
761 });
762 };
763
764 const reportLoaded = async (tab: ServerTab) => {
765 const url = tab.page.url();
766 // Chromium's error page loads after `requestfailed` and must not clear LoadFailed.
767 if (url === "about:blank" || url.startsWith("chrome-error://")) return;
768 // A navigation that starts and fails during the title read leaves the URL unchanged.
769 const generation = tab.navigationGeneration;
770 const loadError = tab.loadError;
771 const title = (await tab.page.title().catch(() => "")).slice(0, TITLE_LIMIT);
772 if (
773 tab.navigationGeneration !== generation ||
774 tab.loadError !== loadError ||
775 tab.loading ||
776 tab.page.url() !== url
777 )
778 return;
779 report(tab, {
780 _tag: "Success",
781 url: ServerBrowserPage.withoutDataPayload(url).slice(0, 2048),
782 title,
783 });
784 void watchTitle(tab, title);
785 };
786
787 // A title change fires no page event, so one watch per document reports the next one.
788 const watchTitle = async (tab: ServerTab, title: string) => {
789 // Chromium keeps one world per name and document, so a new document gets a new context.
790 const world = await tab.cdp
791 .send("Page.getFrameTree")
792 .then(({ frameTree }) =>
793 tab.cdp.send("Page.createIsolatedWorld", {
794 frameId: frameTree.frame.id,
795 worldName: TITLE_WORLD,
796 }),
797 )
798 .catch(() => null);
799 if (!world) return;
800 const watch = { contextId: world.executionContextId, title };
801 if (tab.titleWatch?.contextId === watch.contextId && tab.titleWatch.title === title) return;
802 tab.titleWatch = watch;
803 const changed = await tab.cdp
804 .send("Runtime.evaluate", {
805 contextId: watch.contextId,
806 expression: titleChange(title),
807 awaitPromise: true,
808 })
809 .then(
810 ({ exceptionDetails }) => exceptionDetails === undefined,
811 () => false,
812 );
813 if (tab.titleWatch !== watch) return;
814 tab.titleWatch = null;
815 // A dropped tab's page can still change its title while it unloads.
816 if (changed && !tab.closing && !tab.loadError) await reportLoaded(tab);
817 };
818
819 const reportLiveTabs = () => {
820 const connectionId = hostConnectionId;
821 if (connectionId === null) return;
822 runFork(
823 environment.getEnvironmentId.pipe(
824 Effect.flatMap((environmentId) =>
825 broker.focusHost({
826 clientId: SERVER_HOST_CLIENT_ID,
827 environmentId,
828 connectionId,
829 focused: true,
830 liveTabs: [...tabs.values()].map((tab) => ({
831 threadId: tab.threadId,
832 tabId: tab.tabId,
833 visible: tab.viewers.size > 0,
834 })),
835 }),
836 ),
837 ),
838 );
839 };
840
841 // Desktop pages have no Playwright viewport; an isolated world keeps page script
842 // from overriding the CSS size. A failed read answers null and keeps the last usable size.
843 const readDesktopViewport = async (tab: ServerTab) => {
844 const readSize = async () => {
845 const { frameTree } = await tab.cdp.send("Page.getFrameTree");
846 const { executionContextId } = await tab.cdp.send("Page.createIsolatedWorld", {
847 frameId: frameTree.frame.id,
848 worldName: "t3-preview-viewport",
849 });
850 const { result } = await tab.cdp.send("Runtime.evaluate", {
851 expression: "({ width: innerWidth, height: innerHeight })",
852 contextId: executionContextId,
853 returnByValue: true,
854 });
855 return asRecord(result.value);
856 };
857 const size = await runPromise(
858 // A navigation can destroy the isolated world mid-read.
859 Effect.promise(() => readSize().catch(() => null)).pipe(
860 Effect.timeoutOption(DESKTOP_VIEWPORT_READ_TIMEOUT),
861 Effect.map(Option.getOrNull),
862 ),
863 );
864 const width = size?.width;
865 const height = size?.height;
866 if (typeof width === "number" && width > 0 && typeof height === "number" && height > 0)
867 return (tab.desktopViewport = { width, height });
868 tab.desktopViewportStale = true;
869 return null;
870 };
871
872 // Reads run one at a time, and callers that arrive during a read share the next one.
873 const measureDesktopViewport = (tab: ServerTab) =>
874 (tab.desktopViewportNext ??= tab.desktopViewportRead.then(() => {
875 tab.desktopViewportNext = null;
876 tab.desktopViewportStale = false;
877 return (tab.desktopViewportRead = readDesktopViewport(tab));
878 }));
879
880 const layoutSize = async (tab: ServerTab) => {
881 // A dialog blocks page script; its close measures again.
882 if (tab.desktop) {
883 return tab.dialog
884 ? tab.desktopViewport
885 : ((await measureDesktopViewport(tab)) ?? tab.desktopViewport);
886 }
887 const size = tab.page.viewportSize();
888 if (!size) return size;
889 return {
890 width: Math.max(1, Math.round(size.width / tab.zoomFactor)),
891 height: Math.max(1, Math.round(size.height / tab.zoomFactor)),
892 };
893 };
894
895 // Page resizes and loads mark the size stale; only then does status read the page. One
896 // during the read it waits for can leave that answer old, so it reads once more.
897 const desktopStatusViewport = async (tab: ServerTab) => {
898 const changes = tab.desktopViewportChanges;
899 await (tab.desktopViewportStale || tab.dialog ? layoutSize(tab) : tab.desktopViewportRead);
900 if (tab.desktopViewportChanges !== changes) await layoutSize(tab);
901 return tab.desktopViewport;
902 };
903
904 const applyZoom = async (tab: ServerTab) => {
905 if (tab.desktop) return;
906 const size = tab.page.viewportSize();
907 if (!size) return;
908 if (tab.zoomFactor === 1) {
909 await tab.page.setViewportSize(size);
910 return;
911 }
912 const layout = (await layoutSize(tab))!;
913 await tab.cdp.send("Emulation.setDeviceMetricsOverride", {
914 width: layout.width,
915 height: layout.height,
916 deviceScaleFactor: RENDER_SCALE * tab.zoomFactor,
917 mobile: false,
918 });
919 };
920
921 const broadcastViewport = async (tab: ServerTab) => {
922 const size = await layoutSize(tab);
923 if (!size || tab.closing) return;
924 for (const viewer of tab.viewers) viewer.push({ _tag: "viewport", ...size });
925 };
926
927 const applySetting = async (tab: ServerTab, setting: PreviewViewportSetting) => {
928 tab.setting = setting;
929
930 if (tab.desktop) return;
931 const size =
932 fixedViewportSize(setting) ??
933 [...tab.viewers]
934 .map((viewer) => viewer.requestedSize)
935 .filter((requested) => requested !== null)
936 .sort((left, right) => right.order - left.order)[0] ??
937 UNATTACHED_FILL_VIEWPORT;
938 await tab.page.setViewportSize({ width: size.width, height: size.height });
939 await applyZoom(tab);
940 await broadcastViewport(tab);
941 };
942
943 const applyRendering = async (tab: ServerTab, snapshot: PreviewSessionSnapshot) => {
944 const colorScheme = snapshot.colorScheme ?? "system";
945 if (colorScheme !== tab.colorScheme) {
946 tab.colorScheme = colorScheme;
947 await tab.page.emulateMedia({ colorScheme: colorScheme === "system" ? null : colorScheme });
948 }
949 const zoomFactor = snapshot.zoomFactor ?? 1;
950 if (zoomFactor !== tab.zoomFactor && !tab.desktop) {
951 tab.zoomFactor = zoomFactor;
952 await applyZoom(tab);
953 await broadcastViewport(tab);
954 }
955 };
956
957 const sessionOpen = (tab: ServerTab) => !closedSessions.has(tabKey(tab.threadId, tab.tabId));
958
959 // Closing the session makes the desktop destroy its webview, which drops an unload still in
960 // progress, and `runBeforeUnload` resolves before it runs. So wait for the page's `pagehide`.
961 const unloadDesktopPage = async (page: Page) => {
962 const unloaded = Promise.withResolvers<void>();
963 const timeout = setTimeout(unloaded.resolve, PAGE_CLOSE_GRACE_MS);
964 const binding = `__t3PreviewUnloaded${NodeCrypto.randomUUID().replaceAll("-", "")}`;
965 void (async () => {
966 await page.exposeFunction(binding, unloaded.resolve);
967 await page.evaluate((name) => {
968 const target = globalThis as unknown as {
969 addEventListener: (type: string, listener: () => void) => void;
970 };
971 target.addEventListener("pagehide", () =>
972 Reflect.apply(Reflect.get(target, name), target, []),
973 );
974 }, binding);
975 await page.close({ runBeforeUnload: true });
976 })().catch(unloaded.resolve);
977 await unloaded.promise;
978 clearTimeout(timeout);
979 };
980
981 const unloadingTabs = new WeakMap<ServerTab, Promise<void>>();
982 yield* manager.registerBeforeClose((snapshots) =>
983 Effect.promise(async () => {
984 await Promise.all(
985 snapshots.map((snapshot) => {
986 const key = tabKey(snapshot.threadId, snapshot.tabId);
987 const tab = tabs.get(key);
988 if (!tab) {
989 const opening = pendingTabs.get(key);
990 if (!opening) return;
991 closedPendingTabs.add(key);
992 if (!opening.promise) return;
993 const settled = Promise.withResolvers<void>();
994 const timeout = setTimeout(settled.resolve, PAGE_CLOSE_GRACE_MS);
995 void opening.promise.finally(settled.resolve).catch(constVoid);
996 return settled.promise.finally(() => clearTimeout(timeout));
997 }
998 void tab.control.close().catch(constVoid);
999 if (!tab.desktop || tab.dialog !== null) return;
1000 const pending = unloadingTabs.get(tab);
1001 if (pending) return pending;
1002 tab.closing = true;
1003 const unloading = unloadDesktopPage(tab.page);
1004 unloadingTabs.set(tab, unloading);
1005 return unloading;
1006 }),
1007 );
1008 }),
1009 );
1010
1011 const dropTab = (tab: ServerTab, closeSession: boolean, reopening = false) => {
1012 const key = tabKey(tab.threadId, tab.tabId);
1013 if (tabs.get(key) !== tab) return;
1014 tabs.delete(key);
1015 tab.closing = true;
1016 tab.viewerAttached?.resolve();
1017 clearAbortedNavigation(tab);
1018 // A desktop page outlives the connection unless its session closed with it.
1019 const end =
1020 (tab.desktop || reopening) && !closeSession && sessionOpen(tab) ? "reconnect" : "gone";
1021 for (const viewer of tab.viewers) viewer.push({ _tag: end });
1022 void tab.control.close().catch(constVoid);
1023 if (tab.desktop) void tab.desktop.close().catch(constVoid);
1024 else {
1025 const page = tab.page;
1026 // `runBeforeUnload` returns before the page closes, so closing an isolated context
1027 // must wait for it. A pending dialog would block `beforeunload` from running at all.
1028 const closed = page.isClosed()
1029 ? Promise.resolve()
1030 : new Promise<void>((resolve) => page.once("close", () => resolve()));
1031 // A renderer that never finishes unloading would outlive its tab, so force it closed.
1032 const forceClose = setTimeout(() => void page.close().catch(constVoid), PAGE_CLOSE_GRACE_MS);
1033 void closed.then(() => clearTimeout(forceClose));
1034 void page
1035 // A crashed page never answers `beforeunload`, so that close would not finish.
1036 .close({ runBeforeUnload: tab.dialog === null && tab.crash === undefined })
1037 .catch(() => page.close())
1038 .catch(constVoid);
1039 if (tab.isolatedContext) void closed.then(() => page.context().close()).catch(constVoid);
1040 }
1041 void tab.recording?.encoder.close().catch(constVoid);
1042 void NodeFSP.rm(downloadDir(tab), { recursive: true, force: true }).catch(constVoid);
1043 reportLiveTabs();
1044 if (closeSession) {
1045 runFork(manager.close({ threadId: tab.threadId, tabId: tab.tabId }).pipe(Effect.ignore));
1046 }
1047 };
1048
1049 const desktopRenders = (snapshot: PreviewSessionSnapshot) =>
1050 desktopChannel.available && !snapshot.headless
1051 ? runPromise(
1052 Effect.gen(function* () {
1053 const key = { threadId: snapshot.threadId, tabId: snapshot.tabId };
1054 // A reloaded desktop mounts a thread's tabs again only once an event for it arrives,
1055 // and a renderer still reloading misses events, so repeat it until the wait ends.
1056 if (!(yield* desktopChannel.isAttached(key))) {
1057 yield* manager
1058 .announce({ threadId: ThreadId.make(key.threadId), tabId: key.tabId })
1059 .pipe(Effect.ignore, Effect.repeat(Schedule.spaced("1 second")), Effect.forkScoped);
1060 }
1061 return yield* desktopChannel.awaitAttached(key, DESKTOP_ATTACH_TIMEOUT);
1062 }).pipe(Effect.scoped),
1063 )
1064 : Promise.resolve(false);
1065
1066 const connectDesktop = async (snapshot: PreviewSessionSnapshot) => {
1067 const scope = await runPromise(Scope.make());
1068 try {
1069 const endpoint = await runPromise(
1070 desktopChannel
1071 .endpoint({ threadId: snapshot.threadId, tabId: snapshot.tabId })
1072 .pipe(Scope.provide(scope)),
1073 );
1074 const connected = await contexts.connectDesktopPage(endpoint);
1075 return {
1076 page: connected.page,
1077 close: async () => {
1078 await connected.browser.close().catch(constVoid);
1079 await runPromise(Scope.close(scope, Exit.void));
1080 },
1081 };
1082 } catch (cause) {
1083 await runPromise(Scope.close(scope, Exit.void));
1084 throw cause;
1085 }
1086 };
1087
1088 const createTab = async (
1089 snapshot: PreviewSessionSnapshot,
1090 crashedUrl?: string,
1091 ): Promise<ServerTab> => {
1092 const opened: Array<() => Promise<void>> = [];
1093 try {
1094 return await setUpTab(snapshot, opened, crashedUrl);
1095 } catch (cause) {
1096 // An unpublished tab is in no list, so nothing else would ever close it.
1097 for (const close of opened.reverse()) await close().catch(constVoid);
1098 throw cause;
1099 }
1100 };
1101
1102 const setUpTab = async (
1103 snapshot: PreviewSessionSnapshot,
1104 opened: Array<() => Promise<void>>,
1105 crashedUrl: string | undefined,
1106 ): Promise<ServerTab> => {
1107 const adopted = adoptedPages.get(tabKey(snapshot.threadId, snapshot.tabId));
1108 adoptedPages.delete(tabKey(snapshot.threadId, snapshot.tabId));
1109 const desktop =
1110 adopted === undefined && (await desktopRenders(snapshot))
1111 ? await connectDesktop(snapshot)
1112 : null;
1113 if (desktop) opened.push(desktop.close);
1114 // Attaching over CDP emulates a light scheme; a tab on System follows the desktop's.
1115 if (desktop) await desktop.page.emulateMedia({ colorScheme: null });
1116 // Otherwise a desktop that attaches late, or has no channel to draw it, shows its own page.
1117 if (!desktop) {
1118 await runPromise(
1119 manager
1120 .markHeadless({ threadId: ThreadId.make(snapshot.threadId), tabId: snapshot.tabId })
1121 .pipe(Effect.ignore),
1122 );
1123 }
1124 const isolatedContext =
1125 adopted === undefined &&
1126 desktop === null &&
1127 ((snapshot.automationOwner !== undefined && snapshot.profileId === undefined) ||
1128 snapshot.profileId === INCOGNITO_BROWSER_PROFILE_ID);
1129 const context =
1130 adopted?.page.context() ??
1131 desktop?.page.context() ??
1132 (await contexts.contextFor(
1133 snapshot.profileId ?? "default",
1134 isolatedContext ? tabKey(snapshot.threadId, snapshot.tabId) : undefined,
1135 ));
1136 if (isolatedContext) opened.push(() => context.close());
1137 if (adopted?.page.isClosed()) throw new Error("The popup closed before it opened.");
1138 if (!desktop) await prepareContext(context);
1139 const page = adopted?.page ?? desktop?.page ?? (await context.newPage());
1140 if (!desktop) opened.push(() => page.close());
1141 const cdp = await context.newCDPSession(page);
1142 await installPdfDocumentNavigation(cdp);
1143 // The desktop's page is already a normal browser. A popup's first request
1144 // precedes this override, so its Sec-CH-UA brands still name HeadlessChrome.
1145 if (!desktop) await presentAsChrome(cdp, host);
1146 if (!desktop) await recordCopies(cdp);
1147 page.setDefaultTimeout(NAVIGATION_TIMEOUT_MS);
1148 page.setDefaultNavigationTimeout(NAVIGATION_TIMEOUT_MS);
1149 const control = new SessionControl(snapshot.automationOwner ?? null, () =>
1150 ServerBrowserPage.invalidateRefs(page),
1151 );
1152 let pendingFileChooserOffer = Promise.resolve();
1153 const tab: ServerTab = {
1154 threadId: ThreadId.make(snapshot.threadId),
1155 tabId: snapshot.tabId,
1156 page,
1157 cdp,
1158 frameSessions: new Map(),
1159 copyReads: Promise.resolve(),
1160 copyKeyedUntil: 0,
1161 createdAt: Date.now(),
1162 usedAt: Date.now(),
1163 viewers: new Set(),
1164 viewerAttached: null,
1165 consoleEntries: [],
1166 networkEntries: [],
1167 actionTimeline: [],
1168 control,
1169 agentOperating: false,
1170 fileChooserIntercepted: false,
1171 onFileChooser: (chooser) => {
1172 pendingFileChooserOffer = pendingFileChooserOffer
1173 .then(() => offerFileChooser(tab, chooser))
1174 .catch(constVoid);
1175 },
1176 isolatedContext,
1177 desktop: desktop === null ? null : { close: desktop.close },
1178 profileId: snapshot.profileId,
1179 openerTabId: adopted?.openerTabId,
1180 downloads: [],
1181 fileChooser: null,
1182 dialog: null,
1183 setting: snapshot.viewport ?? FILL_PREVIEW_VIEWPORT,
1184 colorScheme: "system",
1185 zoomFactor: 1,
1186 desktopViewport: null,
1187 desktopViewportRead: Promise.resolve(null),
1188 desktopViewportNext: null,
1189 desktopViewportStale: true,
1190 desktopViewportChanges: 0,
1191 desktopResize: Promise.withResolvers<void>(),
1192 loading: false,
1193 loadError: undefined,
1194 // The page a crash left behind is blank; the session still names what crashed.
1195 crash:
1196 !desktop && !adopted && snapshot.navStatus._tag === "LoadFailed"
1197 ? {
1198 url: snapshot.navStatus.url,
1199 code: snapshot.navStatus.code,
1200 description: snapshot.navStatus.description,
1201 }
1202 : undefined,
1203 crashedUrl,
1204 closing: false,
1205 recording: null,
1206 recordingStart: null,
1207 initialNavigation: null,
1208 pendingInitialRequestUrl: null,
1209 navigationGeneration: 0,
1210 stoppedGeneration: null,
1211 titleWatch: null,
1212 lastReport: null,
1213 abortedNavigation: null,
1214 captureLock: Promise.resolve(),
1215 capturing: 0,
1216 };
1217 if (!desktop) {
1218 await page.setViewportSize(fixedViewportSize(tab.setting) ?? UNATTACHED_FILL_VIEWPORT);
1219 }
1220 await applyRendering(tab, snapshot);
1221 if (desktop) {
1222 // The desktop resizes and zooms its webview itself; viewers map input with the new size.
1223 const remeasure = () => {
1224 tab.desktopViewportStale = true;
1225 tab.desktopViewportChanges += 1;
1226 if (tab.viewers.size > 0) return broadcastViewport(tab).catch(constVoid);
1227 };
1228 cdp.on("Page.frameResized", () => {
1229 const drawn = tab.desktopResize;
1230 tab.desktopResize = Promise.withResolvers();
1231 drawn.resolve();
1232 return remeasure();
1233 });
1234 // A read a navigation broke has a fresh page to measure.
1235 cdp.on("Page.loadEventFired", remeasure);
1236 // Closing a dialog resizes nothing, and the host can close it without us.
1237 cdp.on("Page.javascriptDialogClosed", () => {
1238 tab.dialog = null;
1239 return remeasure();
1240 });
1241 await cdp.send("Page.enable");
1242 }
1243 // Playwright's `request.frame()` throws for a request issued before its frame exists, and a
1244 // throw from a page listener is uncaught, so such a request is not a main-frame navigation.
1245 const isMainNavigation = (request: { isNavigationRequest(): boolean; frame(): unknown }) => {
1246 try {
1247 return request.isNavigationRequest() && request.frame() === page.mainFrame();
1248 } catch {
1249 return false;
1250 }
1251 };
1252 const navigationGenerations = new WeakMap<object, number>();
1253 page.on("request", (request) => {
1254 if (!isMainNavigation(request)) return;
1255 // Playwright can deliver the first request after Stop has already settled its navigation.
1256 const stoppedInitialRequest =
1257 request.url() === tab.pendingInitialRequestUrl &&
1258 tab.stoppedGeneration === tab.navigationGeneration;
1259 tab.pendingInitialRequestUrl = null;
1260 navigationGenerations.set(request, ++tab.navigationGeneration);
1261 clearAbortedNavigation(tab);
1262 tab.loading = true;
1263 tab.loadError = undefined;
1264 tab.crash = undefined;
1265 tab.crashedUrl = undefined;
1266 if (stoppedInitialRequest) {
1267 tab.stoppedGeneration = tab.navigationGeneration;
1268 settleStoppedNavigation(tab, tab.navigationGeneration);
1269 return;
1270 }
1271 report(tab, {
1272 _tag: "Loading",
1273 url: ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048),
1274 title: "",
1275 });
1276 });
1277 page.on("load", () => {
1278 tab.pendingInitialRequestUrl = null;
1279 tab.loading = false;
1280 void reportLoaded(tab);
1281 });
1282 page.on("framenavigated", (frame) => {
1283 const open = tab.fileChooser;
1284 if (open) {
1285 void isFileChooserCurrent(open.chooser).then((current) => {
1286 if (!current && tab.fileChooser?.chooser === open.chooser) closeFileChooser(tab);
1287 });
1288 }
1289 // Same-document navigations (SPA routes) fire no load event.
1290 if (frame === page.mainFrame() && !tab.loading) void reportLoaded(tab);
1291 if (!desktop) void recordFrameCopies(tab, frame);
1292 });
1293 // An adopted popup's frames may have loaded before the tab listened.
1294 if (adopted) for (const frame of page.frames()) void recordFrameCopies(tab, frame);
1295 page.on("requestfailed", (request) => {
1296 const errorText = request.failure()?.errorText ?? "";
1297 pushBounded(tab.networkEntries, {
1298 url: ServerBrowserPage.withoutDataPayload(request.url()),
1299 method: request.method(),
1300 status: null,
1301 failed: true,
1302 errorText,
1303 timestamp: new Date().toISOString(),
1304 });
1305 if (!isMainNavigation(request)) return;
1306 // Newer navigations own status; untracked initial popup requests yield to any tracked navigation.
1307 const generation = navigationGenerations.get(request) ?? 0;
1308 if (generation !== tab.navigationGeneration) return;
1309 // An aborted navigation is usually a download; its `download` event settles the status.
1310 if (errorText.includes("ERR_ABORTED")) {
1311 if (tab.stoppedGeneration === generation) {
1312 settleStoppedNavigation(tab, generation);
1313 return;
1314 }
1315 const url = request.url();
1316 const navigationGeneration = tab.navigationGeneration;
1317 clearAbortedNavigation(tab);
1318 tab.abortedNavigation = {
1319 url,
1320 generation: navigationGeneration,
1321 timer: setTimeout(() => {
1322 if (tab.abortedNavigation?.generation !== navigationGeneration) return;
1323 tab.abortedNavigation = null;
1324 if (!tab.closing) settleAbortedNavigation(tab, navigationGeneration, url, undefined);
1325 }, ABORTED_NAVIGATION_DOWNLOAD_WAIT_MS),
1326 };
1327 return;
1328 }
1329 tab.loading = false;
1330 const { code, description } = ServerBrowserPage.parseNetError(errorText);
1331 const url = ServerBrowserPage.withoutDataPayload(request.url()).slice(0, 2048);
1332 tab.loadError = { url, code, description };
1333 report(tab, { _tag: "LoadFailed", url, title: "", code, description });
1334 });
1335 page.on("response", (response) => {
1336 pushBounded(tab.networkEntries, {
1337 url: ServerBrowserPage.withoutDataPayload(response.url()),
1338 method: response.request().method(),
1339 status: response.status(),
1340 failed: false,
1341 timestamp: new Date().toISOString(),
1342 });
1343 });
1344 page.on("console", (message) => {
1345 pushBounded(tab.consoleEntries, {
1346 level: message.type(),
1347 text: ServerBrowserPage.consoleText(message).slice(0, 2_000),
1348 timestamp: new Date().toISOString(),
1349 });
1350 });
1351 page.on("dialog", (dialog) => {
1352 // Closing is already decided, so an unsaved-changes guard must not keep the page open.
1353 if (tab.closing && dialog.type() === "beforeunload") {
1354 void dialog.accept().catch(constVoid);
1355 return;
1356 }
1357 tab.dialog = dialog;
1358 broadcastControl(tab);
1359 });
1360 page.on("download", (download) => void saveDownload(tab, download));
1361 updateFileChooserInterception(tab);
1362 // Popups become tabs and keep `window.opener`, so sign-in popups can report back.
1363 page.on("popup", (popup) => void adoptPopup(tab, popup));
1364 const key = tabKey(tab.threadId, tab.tabId);
1365 if (closedPendingTabs.delete(key)) {
1366 if (desktop) {
1367 tab.closing = true;
1368 await unloadDesktopPage(page).catch(constVoid);
1369 }
1370 await control.close().catch(constVoid);
1371 throw new ServerBrowserTabNotFoundError({ threadId: tab.threadId, tabId: tab.tabId });
1372 }
1373 page.on("close", () => dropTab(tab, true));
1374 page.on("crash", () => reopenCrashedTab(tab));
1375 tabs.set(key, tab);
1376 reportLiveTabs();
1377 // A desktop page navigates on its own and can finish loading before these listeners attach.
1378 if (desktop) void page.waitForLoadState("load").then(() => reportLoaded(tab), constVoid);
1379 // Existing desktop and popup PDFs can start loading before interception attaches.
1380 if (
1381 snapshot.navStatus._tag === "Loading" &&
1382 ((!adopted && !desktop) || isPdfAssetUrl(snapshot.navStatus.url))
1383 ) {
1384 tab.loading = true;
1385 const initialUrl = URL.parse(snapshot.navStatus.url);
1386 if (initialUrl) initialUrl.hash = "";
1387 tab.pendingInitialRequestUrl = initialUrl?.href ?? null;
1388 const options = { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS } as const;
1389 tab.initialNavigation = (
1390 (desktop || adopted) && page.url() === snapshot.navStatus.url
1391 ? page.reload(options)
1392 : page.goto(snapshot.navStatus.url, options)
1393 ).then(constVoid, (cause: unknown) => {
1394 throw ServerBrowserPage.toOperationError(cause, NAVIGATION_TIMEOUT_MS);
1395 });
1396
1397 if (tab.control.agentId !== null) tab.control.track(tab.initialNavigation);
1398 else void tab.initialNavigation.catch(constVoid);
1399 }
1400 return tab;
1401 };
1402
1403 const downloadsRoot = NodePath.join(config.stateDir, "server-browser", "downloads");
1404 // Saved downloads are indexed in memory, so an earlier run's files can never be opened again.
1405 yield* Effect.tryPromise({
1406 try: () => NodeFSP.rm(downloadsRoot, { recursive: true, force: true }),
1407 catch: (cause) => new ServerBrowserDownloadsCleanupError({ cause }),
1408 }).pipe(
1409 Effect.catch((cause) =>
1410 Effect.logWarning("server preview could not clear earlier downloads", { cause }),
1411 ),
1412 );
1413 // Tab ids are opaque server strings; hashing keeps them out of the path.
1414 const downloadDir = (tab: ServerTab) =>
1415 NodePath.join(
1416 downloadsRoot,
1417 NodeCrypto.createHash("sha256").update(tabKey(tab.threadId, tab.tabId)).digest("hex"),
1418 );
1419
1420 const clearAbortedNavigation = (tab: ServerTab) => {
1421 if (tab.abortedNavigation !== null) clearTimeout(tab.abortedNavigation.timer);
1422 tab.abortedNavigation = null;
1423 };
1424
1425 const settleStoppedNavigation = (tab: ServerTab, generation: number) => {
1426 if (tab.navigationGeneration !== generation || !tab.loading) return;
1427 clearAbortedNavigation(tab);
1428 tab.loading = false;
1429 if (tab.page.url() === "about:blank") report(tab, { _tag: "Idle" });
1430 else void reportLoaded(tab);
1431 };
1432
1433 const settleAbortedNavigation = (
1434 tab: ServerTab,
1435 generation: number,
1436 url: string,
1437 offered: { readonly id: string; readonly fileName: string } | undefined,
1438 ) => {
1439 if (tab.navigationGeneration !== generation) return;
1440 tab.loading = false;
1441 if (tab.page.url() !== "about:blank") {
1442 void reportLoaded(tab);
1443 return;
1444 }
1445 report(tab, {
1446 _tag: "LoadFailed",
1447 url: ServerBrowserPage.withoutDataPayload(url).slice(0, 2048),
1448 title: "",
1449 code: -3,
1450 description: "ERR_ABORTED",
1451 ...(offered === undefined ? {} : { download: offered }),
1452 });
1453 };
1454
1455 const saveDownload = async (tab: ServerTab, download: Download) => {
1456 const id = NodeCrypto.randomUUID();
1457 const path = NodePath.join(downloadDir(tab), id);
1458 const navigation =
1459 download.url() === tab.abortedNavigation?.url ? tab.abortedNavigation.generation : null;
1460 if (navigation !== null) clearAbortedNavigation(tab);
1461 let offered: { readonly id: string; readonly fileName: string } | undefined;
1462 try {
1463 if (await download.failure()) return;
1464 await NodeFSP.mkdir(downloadDir(tab), { recursive: true, mode: 0o700 });
1465 // mkdir leaves the mode of a folder that already exists, such as one an older build made.
1466 await NodeFSP.chmod(downloadDir(tab), 0o700);
1467 await download.saveAs(path);
1468 // saveAs copies with the process umask; a download should stay readable by its owner only.
1469 await NodeFSP.chmod(path, 0o600);
1470 const { size } = await NodeFSP.stat(path);
1471 if (tab.closing || size > DOWNLOAD_MAX_BYTES) {
1472 await NodeFSP.rm(path, { force: true });
1473 return;
1474 }
1475 const saved: ServerDownload = {
1476 id,
1477 fileName: download.suggestedFilename().slice(0, 255) || "download",
1478 path,
1479 sizeBytes: size,
1480 url: ServerBrowserPage.withoutDataPayload(download.url()).slice(0, 2048),
1481 completedAt: new Date().toISOString(),
1482 };
1483 tab.downloads.push(saved);
1484 for (const evicted of tab.downloads.splice(0, tab.downloads.length - DOWNLOAD_LIMIT)) {
1485 await NodeFSP.rm(evicted.path, { force: true }).catch(constVoid);
1486 }
1487 offered = { id, fileName: saved.fileName };
1488 const shownInTab =
1489 navigation === tab.navigationGeneration && tab.page.url() === "about:blank";
1490 if (shownInTab) return;
1491 const controller = [...tab.viewers].find((viewer) => viewer.id === tab.control.controller);
1492 controller?.push({
1493 _tag: "download",
1494 id,
1495 fileName: saved.fileName,
1496 sizeBytes: saved.sizeBytes,
1497 });
1498 } catch (cause) {
1499 await NodeFSP.rm(path, { force: true }).catch(constVoid);
1500 runFork(Effect.logWarning("server preview download failed", { cause }));
1501 } finally {
1502 if (navigation !== null && !tab.closing) {
1503 settleAbortedNavigation(tab, navigation, download.url(), offered);
1504 }
1505 // saveAs copies the artifact; cached profile contexts otherwise retain the original.
1506 await download.delete().catch(constVoid);
1507 }
1508 };
1509
1510 const fileChooserMessage = (tab: ServerTab): ServerBrowserViewerOutput | null =>
1511 tab.fileChooser
1512 ? {
1513 _tag: "fileChooser",
1514 id: tab.fileChooser.id,
1515 multiple: tab.fileChooser.chooser.isMultiple(),
1516 accept: tab.fileChooser.accept,
1517 }
1518 : null;
1519
1520 // Not isConnected: upload helpers often click an input never added to the page. The handle's
1521 // context is destroyed with its document, so a replaced document rejects.
1522 const isFileChooserCurrent = (chooser: FileChooser) =>
1523 chooser
1524 .element()
1525 .evaluate(
1526 (element) =>
1527 element.ownerDocument === (globalThis as unknown as { document: unknown }).document,
1528 )
1529 .catch(() => false);
1530
1531 const offerFileChooser = async (tab: ServerTab, chooser: FileChooser) => {
1532 const previous = tab.fileChooser;
1533 const accept =
1534 (await chooser
1535 .element()
1536 .getAttribute("accept")
1537 .catch(() => null)) ?? "";
1538 if (!(await isFileChooserCurrent(chooser))) return;
1539 if (previous) closeFileChooser(tab);
1540 tab.fileChooser = {
1541 id: NodeCrypto.randomUUID(),
1542 chooser,
1543 accept: accept.slice(0, 1024),
1544 offeredTo: null,
1545 };
1546 pushFileChooser(tab);
1547 };
1548
1549 const pushFileChooser = (tab: ServerTab) => {
1550 const open = tab.fileChooser;
1551 const controller = [...tab.viewers].find((viewer) => viewer.id === tab.control.controller);
1552 if (!open || !controller) return;
1553 if (open.offeredTo !== controller.id) {
1554 tab.fileChooser = { ...open, id: NodeCrypto.randomUUID(), offeredTo: controller.id };
1555 if (open.offeredTo !== null)
1556 for (const viewer of tab.viewers) viewer.push({ _tag: "fileChooserClosed", id: open.id });
1557 }
1558 const message = fileChooserMessage(tab);
1559 if (message) controller.push(message);
1560 };
1561
1562 const closeFileChooser = (tab: ServerTab) => {
1563 const open = tab.fileChooser;
1564 if (!open) return;
1565 tab.fileChooser = null;
1566 for (const viewer of tab.viewers) viewer.push({ _tag: "fileChooserClosed", id: open.id });
1567 };
1568
1569 const setChooserFiles = async (
1570 input: Parameters<ServerBrowser["Service"]["answerFileChooser"]>[0],
1571 ) => {
1572 const tab = tabs.get(tabKey(input.threadId, input.tabId));
1573 const open = tab?.fileChooser;
1574 if (
1575 !tab ||
1576 !open ||
1577 open.id !== input.chooserId ||
1578 open.offeredTo === null ||
1579 open.offeredTo !== tab.control.controller
1580 )
1581 return false;
1582 if (input.files.length > 0) {
1583 await open.chooser.setFiles(
1584 open.chooser.isMultiple() ? [...input.files] : input.files.slice(0, 1),
1585 );
1586 }
1587 if (tab.fileChooser === open) closeFileChooser(tab);
1588 return true;
1589 };
1590 const uploadFiles = async (tab: ServerTab, input: PreviewAutomationUploadInput) => {
1591 const relative = input.paths.find((path) => !NodePath.isAbsolute(path));
1592 if (relative !== undefined)
1593 throw new ServerBrowserPage.ServerBrowserOperationError({
1594 tag: "PreviewAutomationExecutionError",
1595 message: `Upload paths must be absolute: ${relative}`,
1596 });
1597 if (await ServerBrowserPage.setInputFiles(tab.page, input)) return undefined;
1598 const open = tab.fileChooser;
1599 if (!open)
1600 throw new ServerBrowserPage.ServerBrowserOperationError({
1601 tag: "PreviewAutomationExecutionError",
1602 message:
1603 "No file picker is open. Click the page's upload control first, or pass the file input's locator.",
1604 });
1605 if (input.paths.length > 1 && !open.chooser.isMultiple())
1606 throw new ServerBrowserPage.ServerBrowserOperationError({
1607 tag: "PreviewAutomationExecutionError",
1608 message: "This file picker accepts one file.",
1609 });
1610 if (input.paths.length > 0)
1611 await open.chooser.setFiles([...input.paths], {
1612 timeout: input.timeoutMs ?? NAVIGATION_TIMEOUT_MS,
1613 });
1614 if (tab.fileChooser === open) closeFileChooser(tab);
1615 return undefined;
1616 };
1617
1618 const answerFileChooser: ServerBrowser["Service"]["answerFileChooser"] = (input) =>
1619 Effect.promise(() => setChooserFiles(input).catch(() => false));
1620
1621 const openDownload: ServerBrowser["Service"]["openDownload"] = (input) =>
1622 Effect.sync(() => {
1623 const download = tabs
1624 .get(tabKey(input.threadId, input.tabId))
1625 ?.downloads.find((candidate) => candidate.id === input.downloadId);
1626 return download === undefined
1627 ? Option.none()
1628 : Option.some({ path: download.path, fileName: download.fileName });
1629 });
1630
1631 const preparedContexts = new WeakSet<BrowserContext>();
1632 const prepareContext = async (context: BrowserContext) => {
1633 if (preparedContexts.has(context)) return;
1634 preparedContexts.add(context);
1635 await context.grantPermissions(["clipboard-write"]);
1636 };
1637
1638 const recordCopies = async (cdp: CDPSession) => {
1639 // Scripts for new documents run only while this session's Page domain is on.
1640 await cdp.send("Page.enable");
1641 await cdp.send("Page.addScriptToEvaluateOnNewDocument", {
1642 source: CLIPBOARD_SCRIPT,
1643 worldName: CLIPBOARD_WORLD,
1644 runImmediately: true,
1645 });
1646 };
1647
1648 /** Out-of-process frames need their own copy-recording session until target closure. */
1649 const recordFrameCopies = async (tab: ServerTab, frame: Frame) => {
1650 if (frame === tab.page.mainFrame() || tab.frameSessions.has(frame)) return;
1651
1652 const cdp = await tab.page
1653 .context()
1654 .newCDPSession(frame)
1655 .catch(() => null);
1656 if (!cdp) return;
1657 if (tab.frameSessions.has(frame) || frame.isDetached()) {
1658 await cdp.detach().catch(constVoid);
1659 return;
1660 }
1661 tab.frameSessions.set(frame, cdp);
1662 cdp.on("close", () => {
1663 if (tab.frameSessions.get(frame) !== cdp) return;
1664 tab.frameSessions.delete(frame);
1665
1666 if (!frame.isDetached()) void recordFrameCopies(tab, frame);
1667 });
1668 await recordCopies(cdp).catch(constVoid);
1669 };
1670
1671 const takeSessionCopies = async (cdp: CDPSession, since: number, until: number) => {
1672 const tree = await cdp.send("Page.getFrameTree").catch(() => null);
1673 if (!tree) return [];
1674 const frameIds = (node: typeof tree.frameTree): Array<string> => [
1675 node.frame.id,
1676 ...(node.childFrames ?? []).flatMap(frameIds),
1677 ];
1678 return Promise.all(
1679 frameIds(tree.frameTree).map((frameId) =>
1680 cdp
1681 .send("Page.createIsolatedWorld", { frameId, worldName: CLIPBOARD_WORLD })
1682 .then(({ executionContextId }) =>
1683 cdp.send("Runtime.evaluate", {
1684 contextId: executionContextId,
1685 expression: `globalThis.takeCopy?.(${since}, ${until})`,
1686 returnByValue: true,
1687 }),
1688 )
1689 .then(
1690 ({ result }) => (typeof result.value === "string" ? result.value : ""),
1691 () => "",
1692 ),
1693 ),
1694 );
1695 };
1696
1697 const takeCopiedText = async (tab: ServerTab, since: number, until: number) => {
1698 const copies = await Promise.all(
1699 [tab.cdp, ...tab.frameSessions.values()].map((cdp) => takeSessionCopies(cdp, since, until)),
1700 );
1701 return copies.flat().find((text) => text.length > 0);
1702 };
1703
1704 const adoptPopup = async (opener: ServerTab, popup: Page) => {
1705 if (opener.closing) {
1706 await popup.close().catch(constVoid);
1707 return;
1708 }
1709 const url = popup.url();
1710
1711 const controller = [...opener.viewers].find(
1712 (viewer) => viewer.id === opener.control.controller,
1713 );
1714 await runPromise(
1715 openTab({
1716 threadId: opener.threadId,
1717 ...(/^https?:/i.test(url) ? { url } : {}),
1718 runtime: "server",
1719 ...(opener.profileId === undefined ? {} : { profileId: opener.profileId }),
1720 colorScheme: opener.colorScheme,
1721 ...(opener.control.agentId === null
1722 ? {}
1723 : { automationOwner: opener.control.agentId, reveal: false }),
1724 beforePublish: (snapshot) =>
1725 adoptedPages.set(tabKey(snapshot.threadId, snapshot.tabId), {
1726 page: popup,
1727 openerTabId: opener.tabId,
1728 }),
1729 }),
1730 ).then(
1731 (tab) => {
1732 if (opener.control.agentId === null) controller?.push({ _tag: "popup", tabId: tab.tabId });
1733 },
1734 async () => {
1735 await popup.close().catch(constVoid);
1736 },
1737 );
1738 };
1739
1740 const ensureTab = (snapshot: PreviewSessionSnapshot): Promise<ServerTab> => {
1741 const key = tabKey(snapshot.threadId, snapshot.tabId);
1742 const pending = pendingTabs.get(key);
1743 if (pending?.promise) return pending.promise;
1744 const existing = tabs.get(key);
1745 if (existing) return Promise.resolve(existing);
1746 const opening = createTab(snapshot)
1747 .catch((cause: unknown) => {
1748 runFork(
1749 Effect.logWarning(
1750 isHostSetupError(cause) ? cause.message : "server preview tab failed to start",
1751 { cause },
1752 ),
1753 );
1754 throw cause;
1755 })
1756 .finally(() => {
1757 pendingTabs.delete(key);
1758 closedPendingTabs.delete(key);
1759 });
1760 pendingTabs.set(key, { agentId: snapshot.automationOwner ?? null, promise: opening });
1761 return opening;
1762 };
1763
1764 const findTab = Effect.fn("ServerBrowser.findTab")(function* (threadId: string, tabId: string) {
1765 const existing = tabs.get(tabKey(threadId, tabId));
1766 if (existing) return existing;
1767 const { sessions } = yield* manager.list({ threadId: ThreadId.make(threadId) });
1768 const snapshot = sessions.find(
1769 (session) => session.tabId === tabId && session.runtime === "server",
1770 );
1771 if (!snapshot) return yield* new ServerBrowserTabNotFoundError({ threadId, tabId });
1772 return yield* Effect.tryPromise({
1773 try: () => ensureTab(snapshot),
1774 catch: (cause) => (isTabNotFound(cause) ? cause : new ServerBrowserLaunchError({ cause })),
1775 });
1776 });
1777
1778 const openingReservations = () =>
1779 [...pendingTabs.entries()].filter(([key]) => !tabs.has(key)).map(([, pending]) => pending);
1780 const serverAtTabLimit = () => tabs.size + openingReservations().length >= SERVER_TAB_LIMIT;
1781 const agentAtTabLimit = (agentSessionId: string) =>
1782 [...tabs.values()].filter((tab) => tab.control.agentId === agentSessionId).length +
1783 openingReservations().filter((pending) => pending.agentId === agentSessionId).length >=
1784 AGENT_TAB_LIMIT;
1785 const atTabLimit = (agentSessionId: string) =>
1786 serverAtTabLimit() || agentAtTabLimit(agentSessionId);
1787
1788 const assertTabCapacity = (agentSessionId: string) => {
1789 if (atTabLimit(agentSessionId))
1790 throw new BrowserControlInterrupted({
1791 message: `Too many server browser tabs are open (${AGENT_TAB_LIMIT} per agent session, ${SERVER_TAB_LIMIT} per server). Close one with t3_preview_close or reuse one from preview_status tabs.`,
1792 reason: "tabLimit",
1793 });
1794 };
1795
1796 // The desktop draws its tabs in its own webview, so they never get a streamed viewer.
1797 const isVisible = (tab: ServerTab) =>
1798 tab.viewers.size > 0 || (tab.desktop !== null && desktopChannel.isShown(tab));
1799
1800 const reclaimingTabs = new Set<ServerTab>();
1801 const idleAgentTabs = () => {
1802 const cutoff = Date.now() - AGENT_TAB_IDLE_MS;
1803 return [...tabs.values()].filter(
1804 (tab) =>
1805 tab.control.agentId !== null &&
1806 !reclaimingTabs.has(tab) &&
1807 !isVisible(tab) &&
1808 tab.usedAt < cutoff,
1809 );
1810 };
1811
1812 // The desktop app renders every server tab, so idle desktop agent tabs would otherwise
1813 // hold the server's budget forever. Hidden ones give up their slots, longest idle first,
1814 // only when an agent needs one and has room for it.
1815 const freeIdleAgentSlot = async (agentSessionId: string) => {
1816 if (!serverAtTabLimit() || agentAtTabLimit(agentSessionId)) return;
1817 const longestIdle = idleAgentTabs().sort((left, right) => left.usedAt - right.usedAt)[0];
1818 if (longestIdle) {
1819 reclaimingTabs.add(longestIdle);
1820 try {
1821 await runPromise(
1822 manager.close({ threadId: longestIdle.threadId, tabId: longestIdle.tabId }),
1823 );
1824 dropTab(longestIdle, false);
1825 } finally {
1826 reclaimingTabs.delete(longestIdle);
1827 }
1828 }
1829 };
1830
1831 // A Playwright launch must settle before its capacity can be released.
1832 const openTab = Effect.fn("ServerBrowser.openTab")(function* (
1833 input: Parameters<typeof manager.open>[0],
1834 options: { readonly reclaimIdleSlot?: boolean } = {},
1835 ) {
1836 let key: string = NodeCrypto.randomUUID();
1837 const reservation = { agentId: input.automationOwner ?? null };
1838 return yield* Effect.acquireUseRelease(
1839 Effect.tryPromise({
1840 try: async () => {
1841 if (input.automationOwner !== undefined) {
1842 while (
1843 options.reclaimIdleSlot &&
1844 serverAtTabLimit() &&
1845 !agentAtTabLimit(input.automationOwner) &&
1846 idleAgentTabs().length > 0
1847 ) {
1848 await freeIdleAgentSlot(input.automationOwner);
1849 }
1850 assertTabCapacity(input.automationOwner);
1851 }
1852 pendingTabs.set(key, reservation);
1853 },
1854 catch: ServerBrowserPage.toOperationError,
1855 }),
1856 () =>
1857 manager
1858 .open({
1859 ...input,
1860 beforePublish: (snapshot) => {
1861 pendingTabs.delete(key);
1862 key = tabKey(snapshot.threadId, snapshot.tabId);
1863 pendingTabs.set(key, reservation);
1864 input.beforePublish?.(snapshot);
1865 },
1866 })
1867 .pipe(
1868 Effect.flatMap((snapshot) =>
1869 Effect.tryPromise({
1870 try: () => ensureTab(snapshot),
1871 catch: ServerBrowserPage.toOperationError,
1872 }).pipe(
1873 Effect.onError(() =>
1874 manager
1875 .close({ threadId: ThreadId.make(snapshot.threadId), tabId: snapshot.tabId })
1876 .pipe(Effect.ignore),
1877 ),
1878 ),
1879 ),
1880 ),
1881 () =>
1882 Effect.sync(() => {
1883 pendingTabs.delete(key);
1884 }),
1885 );
1886 }, Effect.uninterruptible);
1887
1888 // Desktop browsing bypasses streamed viewers and server input tracking,
1889 // so these idle signals only apply to headless tabs.
1890 const closeIdleAgentTabs = () => {
1891 for (const tab of idleAgentTabs()) if (tab.desktop === null) dropTab(tab, true);
1892 };
1893
1894 const latestThreadTab = (threadId: string, agentSessionId?: string) => {
1895 const threadTabs = [...tabs.values()].filter((tab) => tab.threadId === threadId);
1896 return (
1897 threadTabs
1898 .filter((tab) => tab.control.agentId === agentSessionId)
1899 .sort((left, right) => right.createdAt - left.createdAt)[0] ??
1900 threadTabs.sort(
1901 (left, right) =>
1902 Number(isVisible(right)) - Number(isVisible(left)) || right.usedAt - left.usedAt,
1903 )[0]
1904 );
1905 };
1906
1907 const tabOwner = (tab: ServerTab) =>
1908 tab.control.controller !== null
1909 ? ("human" as const)
1910 : tab.control.agentId !== null
1911 ? ("agent" as const)
1912 : ("unclaimed" as const);
1913
1914 const resolveReportedProfiles = (input: PreviewReportProfilesInput) => {
1915 const profiles = resolveBrowserProfiles(input.profiles);
1916 return {
1917 profiles,
1918 defaultProfileId:
1919 findBrowserProfile(profiles, input.defaultProfileId)?.id ?? DEFAULT_BROWSER_PROFILE_ID,
1920 };
1921 };
1922
1923 // Clients report profiles only when they connect, so the catalog at the last report is
1924 // kept on disk for agents and scheduled tasks that open a profile after a restart.
1925 const savedProfilesPath = NodePath.join(
1926 config.stateDir,
1927 "server-browser",
1928 "reported-profiles.json",
1929 );
1930 const fileServices = yield* Effect.context<FileSystem.FileSystem | Path.Path>();
1931 const saveProfilesLock = yield* Semaphore.make(1);
1932 let savedProfiles = yield* (yield* FileSystem.FileSystem).readFileString(savedProfilesPath).pipe(
1933 Effect.tapError((cause) =>
1934 cause.reason._tag === "NotFound"
1935 ? Effect.void
1936 : Effect.logWarning("failed to read saved browser profiles", {
1937 path: savedProfilesPath,
1938 cause,
1939 }),
1940 ),
1941 Effect.orElseSucceed(() => ""),
1942 );
1943 const reportsByClient = new Map<number, PreviewReportProfilesInput>();
1944 let nextClient = 0;
1945 let reportedProfiles: {
1946 readonly profiles: ReadonlyArray<BrowserProfile>;
1947 readonly defaultProfileId: string;
1948 } | null = yield* decodeSavedProfiles(savedProfiles).pipe(
1949 Effect.map(resolveReportedProfiles),
1950 Effect.tapError((cause) =>
1951 savedProfiles === ""
1952 ? Effect.void
1953 : Effect.logWarning("ignoring invalid saved browser profiles", {
1954 path: savedProfilesPath,
1955 cause,
1956 }),
1957 ),
1958 Effect.orElseSucceed(() => null),
1959 );
1960 // Only reports change what is saved, so the order clients disconnect in at shutdown,
1961 // even while a save waits for the lock, cannot shrink the saved catalog.
1962 let catalogToSave = reportedProfiles;
1963 // Agent recordings encode here, so they need the client's Browser recording frame rate. The
1964 // last report outlives the client, which lets agents record while no client is connected.
1965 let recordingFrameRate = DEFAULT_BROWSER_RECORDING_FRAME_RATE;
1966
1967 // Each client reports only the profiles in its own settings, so the catalog is
1968 // their union while they stay connected. The last catalog stays usable after
1969 // every client disconnects, as a single report did before.
1970 const rebuildReportedProfiles = () => {
1971 const byId = new Map<string, BrowserProfile>();
1972 let latest: PreviewReportProfilesInput | undefined;
1973 for (const report of reportsByClient.values()) {
1974 for (const profile of report.profiles) byId.set(profile.id, profile);
1975 latest = report;
1976 }
1977 if (latest === undefined) return;
1978 reportedProfiles = resolveReportedProfiles({
1979 profiles: [...byId.values()],
1980 defaultProfileId: latest.defaultProfileId,
1981 });
1982 };
1983
1984 const saveReportedProfiles = Effect.fn("ServerBrowser.saveReportedProfiles")(function* () {
1985 if (catalogToSave === null) return;
1986 const contents = encodeSavedProfiles({
1987 profiles: catalogToSave.profiles.filter((profile) => !isBuiltInBrowserProfileId(profile.id)),
1988 defaultProfileId: catalogToSave.defaultProfileId,
1989 });
1990 if (contents === savedProfiles) return;
1991 yield* writeFileStringAtomically({ filePath: savedProfilesPath, contents }).pipe(
1992 Effect.provideContext(fileServices),
1993 Effect.tap(() =>
1994 Effect.sync(() => {
1995 savedProfiles = contents;
1996 }),
1997 ),
1998 Effect.catch((cause) =>
1999 Effect.logWarning("failed to save reported browser profiles", { cause }),
2000 ),
2001 );
2002 }, saveProfilesLock.withPermits(1));
2003
2004 const profileReporter: ServerBrowser["Service"]["profileReporter"] = Effect.acquireRelease(
2005 Effect.sync(() => nextClient++),
2006 (client) =>
2007 Effect.sync(() => {
2008 reportsByClient.delete(client);
2009 rebuildReportedProfiles();
2010 }),
2011 ).pipe(
2012 Effect.map(
2013 (client): ProfileReporter =>
2014 (input) =>
2015 Effect.sync(() => {
2016 if (input.recordingFrameRate !== undefined)
2017 recordingFrameRate = input.recordingFrameRate;
2018 reportsByClient.delete(client);
2019 reportsByClient.set(client, input);
2020 rebuildReportedProfiles();
2021 catalogToSave = reportedProfiles;
2022 }).pipe(
2023 Effect.andThen(saveReportedProfiles),
2024 // A disconnect, even while an earlier save holds the lock, must not drop this
2025 // report's save or leave the saved-content cache stale.
2026 Effect.uninterruptible,
2027 ),
2028 ),
2029 );
2030
2031 const profileStatus = () => {
2032 const profiles = reportedProfiles?.profiles ?? BUILT_IN_BROWSER_PROFILES;
2033 return {
2034 profiles: profiles.map((profile) => ({
2035 id: profile.id,
2036 name: profile.name,
2037 ...(profile.kind === "incognito" ? { incognito: true } : {}),
2038 })),
2039 ...(reportedProfiles ? { defaultProfileId: reportedProfiles.defaultProfileId } : {}),
2040 };
2041 };
2042
2043 const resolveOpenProfile = (requested: string | undefined): string | undefined => {
2044 if (requested === undefined) return reportedProfiles?.defaultProfileId;
2045 const profiles = reportedProfiles?.profiles ?? BUILT_IN_BROWSER_PROFILES;
2046 const wanted = requested.toLowerCase();
2047 const match =
2048 findBrowserProfile(profiles, requested) ??
2049 profiles.find((profile) => profile.name.toLowerCase() === wanted);
2050 if (match) return match.id;
2051 throw new ServerBrowserPage.ServerBrowserOperationError({
2052 tag: "PreviewAutomationUnknownProfileError",
2053 message: `No browser profile is named "${requested}". Use one of: ${profiles
2054 .map((profile) => `${profile.name} (id ${profile.id})`)
2055 .join(", ")}.`,
2056 });
2057 };
2058
2059 const loadErrorOf = (tab: ServerTab): PreviewAutomationLoadError | undefined => {
2060 if (tab.crash) return tab.crash;
2061 if (!tab.page.url().startsWith("chrome-error://")) return undefined;
2062 // A desktop failure from before CDP attached is superseded by any navigation we observe.
2063 if (tab.loadError || !tab.desktop || tab.navigationGeneration > 0) return tab.loadError;
2064 const reported = desktopChannel.loadFailure({ threadId: tab.threadId, tabId: tab.tabId });
2065 return reported && { ...reported, url: ServerBrowserPage.withoutDataPayload(reported.url) };
2066 };
2067
2068 const assertNavigationSucceeded = (tab: ServerTab) => {
2069 const failure = loadErrorOf(tab);
2070 if (failure) {
2071 const diagnosticUrl = URL.parse(failure.url);
2072 if (diagnosticUrl) {
2073 diagnosticUrl.username = "";
2074 diagnosticUrl.password = "";
2075 diagnosticUrl.search = "";
2076 diagnosticUrl.hash = "";
2077 }
2078 const url = diagnosticUrl
2079 ? ServerBrowserPage.withoutDataPayload(diagnosticUrl.href)
2080 : "the requested page";
2081 throw new ServerBrowserPage.ServerBrowserOperationError({
2082 tag: "PreviewAutomationExecutionError",
2083 message: `Navigation to ${url} failed: ${failure.description}`,
2084 detail: { ...failure, url },
2085 });
2086 }
2087 };
2088
2089 const withinStatusDeadline = <A>(read: Promise<A>, late: () => A): Promise<A> => {
2090 let timer: ReturnType<typeof setTimeout> | undefined;
2091 const deadline = new Promise<A>((resolve) => {
2092 timer = setTimeout(() => resolve(late()), STATUS_READ_MS);
2093 });
2094 return Promise.race([read, deadline]).finally(() => clearTimeout(timer));
2095 };
2096
2097 const statusWithTitle = async (
2098 tab: ServerTab | undefined,
2099 agentSessionId?: string,
2100 ): Promise<PreviewAutomationStatus> => {
2101 if (!tab) {
2102 return {
2103 available: false,
2104 visible: false,
2105 tabId: null,
2106 url: null,
2107 title: null,
2108 loading: false,
2109 ...profileStatus(),
2110 };
2111 }
2112 const url = ServerBrowserPage.withoutDataPayload(tab.page.url());
2113 const loadError = loadErrorOf(tab);
2114 const statusUrl = loadError?.url ?? (url === "about:blank" ? null : url);
2115 const [viewport, title] = await Promise.all([
2116 // A late measurement still lands in desktopViewport for the next status.
2117 tab.desktop
2118 ? withinStatusDeadline(desktopStatusViewport(tab), () => tab.desktopViewport)
2119 : tab.page.viewportSize(),
2120 statusUrl === null || tab.dialog
2121 ? null
2122 : withinStatusDeadline(
2123 tab.page.title().catch(() => ""),
2124 () => "",
2125 ).then((title) => title || null),
2126 ]);
2127 return {
2128 available: true,
2129 visible: isVisible(tab),
2130 tabId: tab.tabId,
2131 url: statusUrl,
2132 title,
2133 loading: tab.loading,
2134 ...(loadError ? { loadError } : {}),
2135 control: {
2136 owner: tabOwner(tab),
2137 ownedByCaller: tab.control.agentId === agentSessionId,
2138 generation: tab.control.generation,
2139 },
2140 dialog: dialogStatus(tab),
2141 fileChooser: tab.fileChooser
2142 ? { multiple: tab.fileChooser.chooser.isMultiple(), accept: tab.fileChooser.accept }
2143 : null,
2144 viewportSetting: tab.setting,
2145 ...(viewport ? { viewport } : {}),
2146 tabs: [...tabs.values()]
2147 .filter((candidate) => candidate.threadId === tab.threadId)
2148 .map((candidate) => ({
2149 tabId: candidate.tabId,
2150 url:
2151 candidate.page.url() === "about:blank"
2152 ? null
2153 : (loadErrorOf(candidate)?.url ??
2154 ServerBrowserPage.withoutDataPayload(candidate.page.url())),
2155 ...(candidate.openerTabId === undefined ? {} : { openerTabId: candidate.openerTabId }),
2156 owner: tabOwner(candidate),
2157 ownedByCaller: candidate.control.agentId === agentSessionId,
2158 visible: isVisible(candidate),
2159 ...(candidate.profileId === undefined ? {} : { profileId: candidate.profileId }),
2160 })),
2161 ...profileStatus(),
2162 downloads: tab.downloads.map(({ fileName, path, sizeBytes, url, completedAt }) => ({
2163 fileName,
2164 path,
2165 sizeBytes,
2166 url,
2167 completedAt,
2168 })),
2169 };
2170 };
2171
2172 const resolveNavigationUrl = (input: PreviewAutomationNavigateInput) => {
2173 if (input.url !== undefined) return normalizePreviewUrl(input.url);
2174 const target = input.target!;
2175 if (target.kind === "url") return normalizePreviewUrl(target.url);
2176
2177 const path = target.path ?? "";
2178 return `${target.protocol ?? "http"}://localhost:${target.port}${path.startsWith("/") || path === "" ? path : `/${path}`}`;
2179 };
2180
2181 /** A pending dialog also leaves the probe unanswered, but its script is waiting, not stuck. */
2182 const leaveBusyPage = (tab: ServerTab, cdp: CDPSession, budgetMs?: number) =>
2183 tab.dialog ? Promise.resolve() : ServerBrowserPage.stopUnresponsiveScript(cdp, budgetMs);
2184
2185 const navigate = async (
2186 tab: ServerTab,
2187 url: string,
2188 readiness: "load" | "domContentLoaded" | "none",
2189 timeout: number,
2190 ) => {
2191 tab.pendingInitialRequestUrl = null;
2192 const startedAt = performance.now();
2193 const remaining = () => Math.max(1, timeout - (performance.now() - startedAt));
2194 await leaveBusyPage(tab, tab.cdp, timeout / 2);
2195 const navigation = tab.page.goto(url, { timeout: remaining(), waitUntil: "commit" });
2196 if (readiness === "none") {
2197 tab.control.track(navigation);
2198 return;
2199 }
2200 // Once the target commits, a stalled subresource only delays readiness; report the loading tab.
2201 const ready = () =>
2202 tab.page
2203 .waitForLoadState(readiness === "domContentLoaded" ? "domcontentloaded" : "load", {
2204 timeout: remaining(),
2205 })
2206 .catch((cause: unknown) => {
2207 if (!(cause instanceof Error && cause.name === "TimeoutError")) throw cause;
2208 });
2209 await navigation.then(ready).catch((cause: unknown) => {
2210 const message = cause instanceof Error ? cause.message : String(cause);
2211 if (message.includes("Download is starting")) {
2212 throw new ServerBrowserPage.ServerBrowserOperationError({
2213 tag: "PreviewAutomationExecutionError",
2214 message: `${url} is a file this browser cannot show, so it was downloaded instead. preview_status lists it under downloads.`,
2215 });
2216 }
2217 if (/ERR_[A-Z_]+/.test(message)) {
2218 throw new ServerBrowserPage.ServerBrowserOperationError({
2219 tag: "PreviewAutomationExecutionError",
2220 message: `Navigation to ${url} failed: ${ServerBrowserPage.parseNetError(message).description}`,
2221 });
2222 }
2223 throw ServerBrowserPage.toOperationError(cause, timeout);
2224 });
2225 assertNavigationSucceeded(tab);
2226 };
2227
2228 const withCaptureLock = <A>(tab: ServerTab, operation: () => Promise<A>): Promise<A> => {
2229 const run = tab.captureLock.then(() => {
2230 if (tab.closing) throw new Error("The preview tab closed.");
2231 return operation();
2232 });
2233 tab.captureLock = run.then(constVoid, constVoid);
2234 return run;
2235 };
2236
2237 const startRecording = (tab: ServerTab): Promise<Recording> => {
2238 const started = withCaptureLock(tab, async () => {
2239 if (tab.recording) return tab.recording;
2240 const encoder = await contexts.scratchPage();
2241 let session: CDPSession | null = null;
2242 try {
2243 await encoder.evaluate(ServerBrowserPage.recordingEncoderScript(recordingFrameRate));
2244 // Seed idle pages before returning so an immediate stop has a frame to encode.
2245 const firstFrame = await ServerBrowserPage.captureViewport(tab.cdp, {
2246 format: "jpeg",
2247 quality: RECORDING_SCREENCAST.quality,
2248 });
2249 await encoder.evaluate(
2250 ([data, width]) =>
2251 (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
2252 [firstFrame, tab.page.viewportSize()?.width ?? UNATTACHED_FILL_VIEWPORT.width] as const,
2253 );
2254 const opened = await tab.page.context().newCDPSession(tab.page);
2255 session = opened;
2256 const framesInFlight = new Set<Promise<void>>();
2257 // Chrome withholds frames while acks are outstanding, so ack on arrival and let a
2258 // slow encoder skip to the newest frame instead of throttling the capture.
2259 let pendingFrame: readonly [string, number] | null = null;
2260 const deliverPending = async () => {
2261 while (pendingFrame) {
2262 const next = pendingFrame;
2263 pendingFrame = null;
2264 const accepted = await encoder
2265 .evaluate(
2266 ([data, width]) =>
2267 (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
2268 next,
2269 )
2270 .catch(() => true);
2271 if (!accepted) {
2272 pendingFrame = null;
2273 await opened.send("Page.stopScreencast").catch(constVoid);
2274 }
2275 }
2276 };
2277 opened.on("Page.screencastFrame", (frame) => {
2278 void opened
2279 .send("Page.screencastFrameAck", { sessionId: frame.sessionId })
2280 .catch(constVoid);
2281 const cssWidth = tab.page.viewportSize()?.width ?? frame.metadata.deviceWidth;
2282 const delivering = framesInFlight.size > 0;
2283 pendingFrame = [frame.data, cssWidth];
2284 if (delivering) return;
2285 const delivered: Promise<void> = deliverPending().finally(() =>
2286 framesInFlight.delete(delivered),
2287 );
2288 framesInFlight.add(delivered);
2289 });
2290 await opened.send("Page.startScreencast", RECORDING_SCREENCAST);
2291 if (tab.closing) throw new Error("The tab closed while the recording started.");
2292 const recording: Recording = {
2293 encoder,
2294 session: opened,
2295 startedAt: new Date().toISOString(),
2296 framesInFlight,
2297 };
2298 tab.recording = recording;
2299 return recording;
2300 } catch (cause) {
2301 await encoder.close().catch(constVoid);
2302 await session?.detach().catch(constVoid);
2303 throw cause;
2304 }
2305 }).finally(() => {
2306 if (tab.recordingStart === started) tab.recordingStart = null;
2307 });
2308 tab.recordingStart = started;
2309 return started;
2310 };
2311
2312 const withScreencastsPaused = <A>(tab: ServerTab, capture: () => Promise<A>): Promise<A> =>
2313 withCaptureLock(tab, async () => {
2314 tab.capturing += 1;
2315 const recording = tab.recording;
2316 try {
2317 await Promise.all([
2318 ...[...tab.viewers].map((viewer) => viewer.pause()),
2319 recording?.session.send("Page.stopScreencast").catch(constVoid),
2320 ]);
2321 return await capture();
2322 } finally {
2323 tab.capturing -= 1;
2324
2325 await Promise.all([
2326 ...[...tab.viewers].map((viewer) => viewer.resume()),
2327 recording && tab.recording === recording
2328 ? recording.session.send("Page.startScreencast", RECORDING_SCREENCAST).catch(constVoid)
2329 : undefined,
2330 ]);
2331 }
2332 });
2333
2334 const stopRecording = (tab: ServerTab) =>
2335 withCaptureLock(tab, async () => {
2336 const recording = tab.recording;
2337 if (!recording) {
2338 throw new ServerBrowserPage.ServerBrowserOperationError({
2339 tag: "PreviewAutomationRecordingNotActiveError",
2340 message: "No recording is active for this tab.",
2341 });
2342 }
2343 try {
2344 await recording.session.send("Page.stopScreencast").catch(constVoid);
2345 await Promise.all(recording.framesInFlight);
2346 await recording.session.detach().catch(constVoid);
2347 // A screencast can miss the last paint when the page goes idle.
2348 if (!tab.closing && !tab.dialog) {
2349 const finalFrame = await ServerBrowserPage.captureFinalRecordingFrame(
2350 tab.cdp,
2351 RECORDING_SCREENCAST.quality,
2352 );
2353 if (finalFrame !== undefined) {
2354 await recording.encoder
2355 .evaluate(
2356 ([data, width]) =>
2357 (globalThis as unknown as EncoderWindow).__t3Recorder.frame(data, width),
2358 [
2359 finalFrame,
2360 tab.page.viewportSize()?.width ?? UNATTACHED_FILL_VIEWPORT.width,
2361 ] as const,
2362 )
2363 .catch(constVoid);
2364 }
2365 }
2366 const { mimeType, count, bytes } = await recording.encoder.evaluate(() =>
2367 (globalThis as unknown as EncoderWindow).__t3Recorder.stop(),
2368 );
2369 // Checked before the transfer so an oversized video never lands in this process.
2370 if (bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
2371 throw new ServerBrowserPage.ServerBrowserOperationError({
2372 tag: "PreviewAutomationRecordingTooLargeError",
2373 message: "The recording is larger than the attachment limit.",
2374 });
2375 }
2376 if (!mimeType || bytes === 0) {
2377 throw new ServerBrowserPage.ServerBrowserOperationError({
2378 tag: "PreviewAutomationExecutionError",
2379 message: "The recording captured no frames.",
2380 });
2381 }
2382 const extension = mimeType.startsWith("video/mp4") ? "mp4" : "webm";
2383 const pendingId = `${PENDING_ATTACHMENT_THREAD_SEGMENT}-${NodeCrypto.randomUUID()}-${extension}`;
2384 const path = NodePath.join(config.attachmentsDir, `${pendingId}.${extension}`);
2385 await NodeFSP.mkdir(config.attachmentsDir, { recursive: true });
2386 if (bytes >= RECORDING_BINARY_EXPORT_MIN_BYTES) {
2387 const stagedPath = `${path}.partial`;
2388 try {
2389 const [download] = await Promise.all([
2390 recording.encoder.waitForEvent("download", {
2391 timeout: RECORDING_DOWNLOAD_TIMEOUT_MS,
2392 }),
2393 recording.encoder.evaluate(() =>
2394 (globalThis as unknown as EncoderWindow).__t3Recorder.download(),
2395 ),
2396 ]);
2397 await download.saveAs(stagedPath);
2398 if ((await NodeFSP.stat(stagedPath)).size !== bytes) {
2399 throw new Error("The recording download is incomplete.");
2400 }
2401 await NodeFSP.rename(stagedPath, path);
2402 } finally {
2403 await NodeFSP.rm(stagedPath, { force: true }).catch(constVoid);
2404 }
2405 } else {
2406 const chunks: Array<Buffer> = [];
2407 for (let index = 0; index < count; index += 1) {
2408 const chunk = await recording.encoder.evaluate(
2409 (chunkIndex) =>
2410 (globalThis as unknown as EncoderWindow).__t3Recorder.chunk(chunkIndex),
2411 index,
2412 );
2413 chunks.push(Buffer.from(chunk, "base64"));
2414 }
2415 const data = Buffer.concat(chunks);
2416 if (data.byteLength !== bytes) throw new Error("The recording transfer is incomplete.");
2417 await NodeFSP.writeFile(path, data);
2418 }
2419 return {
2420 id: pendingId,
2421 tabId: tab.tabId,
2422 path,
2423 mimeType: mimeType.split(";")[0]!,
2424 sizeBytes: bytes,
2425 createdAt: new Date().toISOString(),
2426 uploadedAttachmentId: pendingId,
2427 };
2428 } finally {
2429 await recording.encoder.close().catch(constVoid);
2430 tab.recording = null;
2431 }
2432 });
2433
2434 let pointerSequence = 0;
2435 const pointerFor =
2436 (tab: ServerTab): ServerBrowserPage.PointerReporter =>
2437 async ({ x, y }, phase) => {
2438 const encoder = tab.recording?.encoder;
2439
2440 if (tab.viewers.size === 0 && !encoder && !tab.desktop) return;
2441 const show = (next: "move" | "click") => {
2442 const sequence = ++pointerSequence;
2443 for (const viewer of tab.viewers)
2444 viewer.push({ _tag: "pointer", phase: next, x, y, sequence });
2445 if (tab.desktop)
2446 runFork(
2447 desktopChannel.pointer(
2448 { threadId: tab.threadId, tabId: tab.tabId },
2449 { phase: next, x, y },
2450 ),
2451 );
2452 void encoder
2453 ?.evaluate(
2454 ([px, py, click]) =>
2455 (globalThis as unknown as EncoderWindow).__t3Recorder?.cursor(px, py, click),
2456 [x, y, next === "click"] as const,
2457 )
2458 .catch(constVoid);
2459 };
2460 show("move");
2461 await sleepUntil(Date.now() + AGENT_CURSOR_MOVE_MS);
2462 if (phase !== "click") return;
2463 show("click");
2464 await sleepUntil(Date.now() + AGENT_CURSOR_CLICK_LEAD_MS);
2465 };
2466
2467 const recordAction = <A>(tab: ServerTab, action: string, run: () => Promise<A>): Promise<A> => {
2468 const event: {
2469 -readonly [K in keyof PreviewAutomationActionEvent]: PreviewAutomationActionEvent[K];
2470 } = {
2471 id: NodeCrypto.randomUUID(),
2472 action,
2473 status: "running",
2474 startedAt: new Date().toISOString(),
2475 };
2476 pushBounded(tab.actionTimeline, event, ACTION_TIMELINE_LIMIT);
2477 return run().then(
2478 (result) => {
2479 event.status = "succeeded";
2480 event.completedAt = new Date().toISOString();
2481 return result;
2482 },
2483 (cause: unknown) => {
2484 event.status = "failed";
2485 event.completedAt = new Date().toISOString();
2486 event.error = cause instanceof Error ? cause.message.split("\n")[0] : String(cause);
2487 throw cause;
2488 },
2489 );
2490 };
2491
2492 const requireTab = async (request: PreviewAutomationRequest) => {
2493 if (!request.agentSessionId)
2494 throw new BrowserControlInterrupted({
2495 message: "The agent session is missing. Reconnect the provider.",
2496 });
2497 const owned = [...tabs.values()].filter(
2498 (tab) => tab.threadId === request.threadId && tab.control.agentId === request.agentSessionId,
2499 );
2500 if (!request.tabIdExplicit && owned.length > 1)
2501 throw new BrowserControlInterrupted({
2502 message:
2503 "Multiple tabs belong to this agent session. Pass a tabId from preview_open or preview_status tabs.",
2504 reason: "tabRequired",
2505 });
2506 const tab =
2507 request.tabId === undefined
2508 ? latestThreadTab(request.threadId, request.agentSessionId)
2509 : await runPromise(
2510 findTab(request.threadId, request.tabId).pipe(Effect.orElseSucceed(constVoid)),
2511 );
2512 if (!tab) {
2513 throw new ServerBrowserPage.ServerBrowserOperationError({
2514 tag: "PreviewAutomationTabNotFoundError",
2515 message: "No server preview tab is open for this thread. Call preview_open first.",
2516 });
2517 }
2518 if (!READ_OPERATIONS.has(request.operation) && !tab.control.agentMayAct(request.agentSessionId))
2519 throw new BrowserControlInterrupted({
2520 message:
2521 "This tab belongs to another agent session. You can read it, but open your own tab to act.",
2522 reason: "agentMismatch",
2523 });
2524 return tab;
2525 };
2526
2527 const markUsed = (request: PreviewAutomationRequest) => {
2528 const now = Date.now();
2529 for (const tab of tabs.values()) {
2530 if (
2531 tab.threadId === request.threadId &&
2532 (tab.control.agentId === request.agentSessionId || tab.tabId === request.tabId)
2533 )
2534 tab.usedAt = now;
2535 }
2536 };
2537
2538 const runOperation = async (
2539 request: PreviewAutomationRequest,
2540 deadlineMs: number,
2541 ): Promise<unknown> => {
2542 const input = request.input;
2543 switch (request.operation) {
2544 case "status":
2545 return statusWithTitle(
2546 request.tabId === undefined
2547 ? latestThreadTab(request.threadId, request.agentSessionId)
2548 : tabs.get(tabKey(request.threadId, request.tabId)),
2549 request.agentSessionId,
2550 );
2551 case "open": {
2552 if (!request.agentSessionId)
2553 throw new BrowserControlInterrupted({
2554 message: "The agent session is missing. Reconnect the provider.",
2555 });
2556 const open = input as PreviewAutomationOpenInput;
2557 const url = open.url === undefined ? undefined : normalizePreviewUrl(open.url);
2558 const reuse = open.reuseExistingTab ?? true;
2559 if (
2560 reuse &&
2561 !request.tabIdExplicit &&
2562 [...tabs.values()].filter(
2563 (tab) =>
2564 tab.threadId === request.threadId && tab.control.agentId === request.agentSessionId,
2565 ).length > 1
2566 )
2567 throw new BrowserControlInterrupted({
2568 message: "Multiple tabs are open. Pass tabId or reuseExistingTab=false.",
2569 reason: "tabRequired",
2570 });
2571
2572 const found =
2573 reuse && request.tabId !== undefined
2574 ? await runPromise(
2575 findTab(request.threadId, request.tabId).pipe(
2576 Effect.catchTags({
2577 ServerBrowserTabNotFoundError: () => Effect.void,
2578 }),
2579 ),
2580 )
2581 : undefined;
2582 if (reuse && request.tabIdExplicit && !found) {
2583 const { sessions } = await runPromise(
2584 manager.list({ threadId: ThreadId.make(request.threadId) }),
2585 );
2586 const desktop = sessions.some(
2587 (session) => session.tabId === request.tabId && session.runtime !== "server",
2588 );
2589 throw new ServerBrowserPage.ServerBrowserOperationError({
2590 tag: desktop ? "PreviewAutomationExecutionError" : "PreviewAutomationTabNotFoundError",
2591 message: desktop
2592 ? "This tab belongs to the desktop runtime. This environment's server browser cannot control desktop tabs. Omit tabId to open a server tab instead."
2593 : "The requested server preview tab does not exist.",
2594 });
2595 }
2596 // Reusing another session's tab requires explicit tabId; reading it grants no implicit takeover.
2597 const existing =
2598 found && (request.tabIdExplicit || found.control.agentId === request.agentSessionId)
2599 ? found
2600 : undefined;
2601 const navigationTimeout = Math.min(request.timeoutMs, NAVIGATION_TIMEOUT_MS);
2602 const profileId = existing ? undefined : resolveOpenProfile(open.profileId);
2603 if (!existing) closeIdleAgentTabs();
2604 const tab =
2605 existing ??
2606 (await runPromise(
2607 openTab(
2608 {
2609 threadId: request.threadId,
2610 ...(url ? { url } : {}),
2611 ...(profileId === undefined ? {} : { profileId }),
2612 runtime: "server",
2613 reveal: false,
2614 automationOwner: request.agentSessionId,
2615 },
2616 { reclaimIdleSlot: true },
2617 ),
2618 ));
2619 if (existing?.dialog)
2620 throw new BrowserControlInterrupted({
2621 message:
2622 "A browser dialog is pending. Read preview_status and use preview_dialog first.",
2623 reason: "dialogPending",
2624 });
2625 return tab.control.agent(
2626 request.agentSessionId,
2627 async () => {
2628 if (tab.dialog)
2629 throw new BrowserControlInterrupted({
2630 message:
2631 "A browser dialog is pending. Read preview_status and use preview_dialog first.",
2632 reason: "dialogPending",
2633 });
2634 if (existing) {
2635 if (url) await navigate(tab, url, "load", navigationTimeout);
2636 } else {
2637 // A failed open returns no tabId; a leftover hidden tab would block later reuse.
2638 await Promise.resolve(tab.initialNavigation)
2639 .then(async () => {
2640 if (url) {
2641 await tab.page
2642 .waitForLoadState("load", { timeout: navigationTimeout })
2643 .catch(constVoid);
2644 }
2645 assertNavigationSucceeded(tab);
2646 })
2647 .catch(async (cause: unknown) => {
2648 // Playwright rejects downloads too; their tab must survive to save the file.
2649 if (cause instanceof Error && cause.message.includes("Download is starting")) {
2650 throw cause;
2651 }
2652 await runPromise(manager.close({ threadId: tab.threadId, tabId: tab.tabId }));
2653 dropTab(tab, false);
2654 throw cause;
2655 });
2656 }
2657 const reveal = open.open ?? open.show;
2658 let shown: Promise<void> | undefined;
2659 if (reveal !== false) {
2660 await runPromise(
2661 manager.requestReveal({
2662 threadId: tab.threadId,
2663 tabId: tab.tabId,
2664 force: reveal === true,
2665 }),
2666 );
2667 // Only an explicit show is sure to reach a client; desktop tabs are visible without a viewer.
2668 const remainingMs = deadlineMs - performance.now();
2669 if (reveal === true && tab.viewers.size === 0 && tab.desktop === null)
2670 shown = awaitViewer(tab, Math.min(REVEAL_VIEWER_WAIT_MS, remainingMs));
2671 // A desktop panel reports the revealed tab shown a few hops after the reveal.
2672 else if (reveal === true && !isVisible(tab))
2673 shown = runPromise(
2674 desktopChannel.awaitShown(
2675 tab,
2676 Math.max(0, Math.min(REVEAL_DESKTOP_SHOWN_WAIT_MS, remainingMs)),
2677 ),
2678 ).then(constVoid);
2679 }
2680 await shown;
2681 return statusWithTitle(tab, request.agentSessionId);
2682 },
2683 deadlineMs,
2684 );
2685 }
2686 case "recordingStop": {
2687 if (
2688 !request.tabIdExplicit &&
2689 [...tabs.values()].filter(
2690 (candidate) =>
2691 candidate.threadId === request.threadId &&
2692 candidate.control.agentId === request.agentSessionId,
2693 ).length > 1
2694 )
2695 throw new BrowserControlInterrupted({
2696 message:
2697 "Multiple tabs belong to this agent session. Pass a tabId from preview_open or preview_status tabs.",
2698 reason: "tabRequired",
2699 });
2700 const recordings = [...tabs.values()].filter(
2701 (candidate) =>
2702 candidate.threadId === request.threadId &&
2703 candidate.control.agentMayAct(request.agentSessionId ?? "") &&
2704 (candidate.recording || candidate.recordingStart),
2705 );
2706 const targetTabId =
2707 request.tabId ?? latestThreadTab(request.threadId, request.agentSessionId)?.tabId;
2708 const tab =
2709 recordings.find((candidate) => candidate.tabId === targetTabId) ??
2710 (!request.tabIdExplicit && recordings.length === 1 ? recordings[0] : undefined);
2711 if (!tab) {
2712 throw new ServerBrowserPage.ServerBrowserOperationError({
2713 tag: "PreviewAutomationRecordingNotActiveError",
2714 message: "No recording is active for this thread.",
2715 });
2716 }
2717 // A stop cannot act twice (a retry finds no recording);
2718 // dropping a late one would leave capture running.
2719 return tab.control.agent(request.agentSessionId ?? "", () => stopRecording(tab));
2720 }
2721 }
2722 const tab = await requireTab(request);
2723 // Closing must unblock an action waiting on a dialog, without queueing behind it.
2724 if (request.operation === "close") {
2725 if (tab.control.agentId === null)
2726 throw new BrowserControlInterrupted({
2727 message: "This tab has no agent owner. Ask the user to close it in the browser panel.",
2728 reason: "unclaimedTab",
2729 });
2730 if (tab.control.controller !== null)
2731 throw new BrowserControlInterrupted({
2732 message: "A human controls this tab.",
2733 reason: "humanControl",
2734 });
2735 await runPromise(manager.close({ threadId: tab.threadId, tabId: tab.tabId }));
2736 dropTab(tab, false);
2737 return {};
2738 }
2739 // Resolve dialogs outside the queue, with ownership checked, to unblock waiting clicks.
2740 if (request.operation === "dialog") {
2741 if (tab.control.controller !== null)
2742 throw new BrowserControlInterrupted({
2743 message: "A human controls this tab.",
2744 reason: "humanControl",
2745 });
2746 await resolveDialog(tab, input as PreviewAutomationDialogInput);
2747 return statusWithTitle(tab, request.agentSessionId);
2748 }
2749 const agentSessionId = request.agentSessionId!;
2750
2751 if (
2752 READ_OPERATIONS.has(request.operation) &&
2753 tab.control.agentId !== agentSessionId &&
2754 !tab.control.agentCanActNow(agentSessionId)
2755 ) {
2756 return tab.control.observe(async () => {
2757 if (tab.dialog)
2758 throw new BrowserControlInterrupted({
2759 message: "A browser dialog is pending. Read preview_status.",
2760 reason: "dialogPending",
2761 });
2762 return executeTabOperation(tab, request, deadlineMs);
2763 });
2764 }
2765 return tab.control.agent(
2766 agentSessionId,
2767 async () => {
2768 if (tab.dialog)
2769 throw new BrowserControlInterrupted({
2770 message:
2771 "A browser dialog is pending. Read preview_status and use preview_dialog first.",
2772 reason: "dialogPending",
2773 });
2774 const generation = tab.control.generation;
2775 tab.agentOperating = !READ_OPERATIONS.has(request.operation);
2776 updateFileChooserInterception(tab);
2777 try {
2778 return await executeTabOperation(tab, request, deadlineMs);
2779 } finally {
2780 tab.agentOperating = false;
2781 updateFileChooserInterception(tab);
2782 if (generation !== tab.control.generation) ServerBrowserPage.invalidateRefs(tab.page);
2783 }
2784 },
2785 deadlineMs,
2786 );
2787 };
2788
2789 const executeTabOperation = async (
2790 tab: ServerTab,
2791 request: PreviewAutomationRequest,
2792 deadlineMs: number,
2793 ) => {
2794 const input = request.input;
2795 switch (request.operation) {
2796 case "navigate": {
2797 const navigateInput = input as PreviewAutomationNavigateInput;
2798 await recordAction(tab, "navigate", () =>
2799 navigate(
2800 tab,
2801 resolveNavigationUrl(navigateInput),
2802 navigateInput.readiness ?? "load",
2803 navigateInput.timeoutMs ?? request.timeoutMs,
2804 ),
2805 );
2806 return statusWithTitle(tab, request.agentSessionId);
2807 }
2808 case "resize": {
2809 const setting = resolvePreviewViewport(input as PreviewAutomationResizeInput);
2810 const from = fixedViewportSize(tab.setting);
2811 const to = fixedViewportSize(setting);
2812 const drawn = tab.desktopResize.promise;
2813 await runPromise(
2814 manager.resize({ threadId: tab.threadId, tabId: tab.tabId, viewport: setting }),
2815 );
2816 await applySetting(tab, setting);
2817 if (!tab.desktop) {
2818 return {
2819 tabId: tab.tabId,
2820 setting,
2821 viewport: tab.page.viewportSize() ?? UNATTACHED_FILL_VIEWPORT,
2822 };
2823 }
2824 const changed = from?.width !== to?.width || from?.height !== to?.height;
2825 const remainingMs = () => Math.max(0, deadlineMs - performance.now());
2826 const within = <A>(promise: Promise<A>, ms: number) =>
2827 runPromise(
2828 Effect.promise(() => promise).pipe(
2829 Effect.timeoutOption(ms),
2830 Effect.map(Option.getOrNull),
2831 ),
2832 );
2833 // The desktop draws the new size after the session update; its page resize says it has.
2834 // Half the time left keeps room for the read, which runs even if nothing was drawn,
2835 // because a page can keep another size than the one asked for.
2836 if (changed) {
2837 // Electron resizes no hidden guest it does not paint; a pending capture makes the desktop paint it.
2838 let waiting = true;
2839 void (async () => {
2840 while (waiting && !tab.closing) {
2841 await tab.cdp.send("Page.captureScreenshot", DESKTOP_PAINT_CAPTURE);
2842 }
2843 })().catch(constVoid);
2844 try {
2845 await within(drawn, Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2));
2846 } finally {
2847 waiting = false;
2848 }
2849 }
2850 const changes = tab.desktopViewportChanges;
2851 const measured = await within(measureDesktopViewport(tab), remainingMs());
2852 // Only a read after the draw, with no page resize or load during it, measures this resize.
2853 return {
2854 tabId: tab.tabId,
2855 setting,
2856 ...(measured && tab.desktopViewportChanges === changes ? { viewport: measured } : {}),
2857 };
2858 }
2859 case "setColorScheme": {
2860 const { colorScheme } = input as PreviewAutomationSetColorSchemeInput;
2861 const snapshot = await runPromise(
2862 manager.adjust({ threadId: tab.threadId, tabId: tab.tabId, colorScheme }),
2863 );
2864 await applyRendering(tab, snapshot);
2865 return { tabId: tab.tabId, colorScheme };
2866 }
2867 case "snapshot": {
2868 const snapshotInput = input as PreviewAutomationSnapshotInput | undefined;
2869 const rendering = {
2870 ...tab,
2871 renderScale: tab.desktop ? "display" : RENDER_SCALE,
2872 fullScreenshot: snapshotInput?.fullScreenshot,
2873 } as const;
2874 const snapshot = await withScreencastsPaused(tab, () =>
2875 snapshotInput?.pixelsRequired === false
2876 ? ServerBrowserPage.snapshotMetadata(rendering)
2877 : ServerBrowserPage.snapshot(rendering),
2878 );
2879 const loadError = loadErrorOf(tab);
2880 return loadError ? { ...snapshot, url: loadError.url, loadError } : snapshot;
2881 }
2882 case "click": {
2883 // Time spent queued behind other work on this tab counts against the broker's deadline.
2884 const clickInput = {
2885 ...(input as PreviewAutomationClickInput),
2886 timeoutMs: Math.max(1, Math.floor(deadlineMs - performance.now())),
2887 };
2888 await recordAction(tab, "click", () =>
2889 ServerBrowserPage.click(tab.page, clickInput, pointerFor(tab)),
2890 );
2891 return undefined;
2892 }
2893 case "hover":
2894 return recordAction(tab, "hover", () =>
2895 ServerBrowserPage.hover(tab.page, input as PreviewAutomationHoverInput, pointerFor(tab)),
2896 );
2897 case "select":
2898 return recordAction(tab, "select", () =>
2899 ServerBrowserPage.select(tab.page, input as PreviewAutomationSelectInput),
2900 );
2901 case "drag":
2902 return recordAction(tab, "drag", () =>
2903 ServerBrowserPage.drag(tab.page, input as PreviewAutomationDragInput, pointerFor(tab)),
2904 );
2905 case "upload":
2906 return recordAction(tab, "upload", () =>
2907 uploadFiles(tab, input as PreviewAutomationUploadInput),
2908 );
2909 case "type":
2910 return recordAction(tab, "type", () =>
2911 ServerBrowserPage.type(tab.page, input as PreviewAutomationTypeInput),
2912 );
2913 case "press":
2914 return recordAction(tab, "press", () =>
2915 ServerBrowserPage.press(tab.page, input as PreviewAutomationPressInput),
2916 );
2917 case "scroll":
2918 return recordAction(tab, "scroll", () =>
2919 ServerBrowserPage.scroll(tab.page, input as PreviewAutomationScrollInput),
2920 );
2921 case "evaluate":
2922 return ServerBrowserPage.evaluate(
2923 tab.cdp,
2924 input as PreviewAutomationEvaluateInput,
2925 request.timeoutMs,
2926 );
2927 case "waitFor":
2928 return ServerBrowserPage.waitFor(tab.page, input as PreviewAutomationWaitForInput);
2929 case "recordingStart": {
2930 const recording = await startRecording(tab);
2931 return { tabId: tab.tabId, recording: true, startedAt: recording.startedAt };
2932 }
2933 }
2934 };
2935
2936 const handleRequest = (connectionId: string, request: PreviewAutomationRequest) =>
2937 Effect.tryPromise({
2938 try: () =>
2939 runOperation(request, performance.now() + request.timeoutMs).finally(() =>
2940 markUsed(request),
2941 ),
2942 catch: ServerBrowserPage.toOperationError,
2943 }).pipe(
2944 Effect.match({
2945 onSuccess: (result) => ({ ok: true as const, result }),
2946 onFailure: (error) => ({
2947 ok: false as const,
2948 error: {
2949 _tag: error.tag,
2950 message: error.message,
2951 ...(error.detail === undefined ? {} : { detail: error.detail }),
2952 },
2953 }),
2954 }),
2955 Effect.flatMap((outcome) =>
2956 broker.respond({
2957 clientId: SERVER_HOST_CLIENT_ID,
2958 connectionId,
2959 requestId: request.requestId,
2960 ...outcome,
2961 }),
2962 ),
2963 Effect.ignore,
2964 );
2965
2966 const mirrorManagerEvent = (event: PreviewEvent) =>
2967 Effect.promise(async () => {
2968 if (event.type === "opened" && event.snapshot.runtime === "server") {
2969 const key = tabKey(event.snapshot.threadId, event.snapshot.tabId);
2970 if (event.snapshot.automationOwner === undefined || pendingTabs.has(key) || tabs.has(key))
2971 await ensureTab(event.snapshot).catch(constVoid);
2972 return;
2973 }
2974 const key = tabKey(event.threadId, event.tabId);
2975 const tab = tabs.get(key);
2976 if (event.type === "closed" && !tab && pendingTabs.has(key)) closedPendingTabs.add(key);
2977 if (!tab) return;
2978 if (event.type === "closed") {
2979 closedSessions.add(key);
2980 dropTab(tab, false);
2981 closedSessions.delete(key);
2982 return;
2983 }
2984 if (event.type === "resized") {
2985 // Desktop tabs only record the setting, so status need not wait behind an agent action.
2986 if (tab.desktop) tab.setting = event.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT;
2987 await tab.control
2988 .system(async () => {
2989 if (
2990 !viewportSettingsEqual(event.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT, tab.setting)
2991 ) {
2992 await applySetting(tab, event.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT);
2993 }
2994 await applyRendering(tab, event.snapshot);
2995 const request = event.request;
2996 if (request?.clear === "cookies") await tab.cdp.send("Network.clearBrowserCookies");
2997 if (request?.clear === "cache") await tab.cdp.send("Network.clearBrowserCache");
2998 if (request?.hardReload) {
2999 tab.pendingInitialRequestUrl = null;
3000 await leaveBusyPage(tab, tab.cdp);
3001 await tab.cdp.send("Page.reload", { ignoreCache: true });
3002 }
3003 })
3004 .catch((cause: unknown) =>
3005 runFork(Effect.logWarning("server preview could not apply a tab setting", { cause })),
3006 );
3007 }
3008 });
3009
3010 const releaseViewerInput = async (viewer: ViewerState, session: CDPSession) => {
3011 for (const { key, code } of viewer.pressedKeys.values()) {
3012 await session.send("Input.dispatchKeyEvent", { type: "keyUp", key, code }).catch(constVoid);
3013 }
3014 viewer.pressedKeys.clear();
3015 for (const [button, point] of viewer.pressedButtons) {
3016 await session
3017 .send("Input.dispatchMouseEvent", {
3018 type: "mouseReleased",
3019 button,
3020 ...point,
3021 buttons: 0,
3022 clickCount: 1,
3023 })
3024 .catch(constVoid);
3025 }
3026 viewer.pressedButtons.clear();
3027 viewer.requestedSize = null;
3028 };
3029
3030 const dispatchViewerInput = async (
3031 tab: ServerTab,
3032 session: CDPSession,
3033 viewer: ViewerState,
3034 raw: unknown,
3035 ) => {
3036 const message = asRecord(raw);
3037 if (!message) return;
3038 const modifiers = modifiersOf(message);
3039 switch (message.type) {
3040 case "mouse": {
3041 const action = message.action;
3042 const type =
3043 action === "down" ? "mousePressed" : action === "up" ? "mouseReleased" : "mouseMoved";
3044 const button = ["none", "left", "middle", "right"].includes(String(message.button))
3045 ? (message.button as "none" | "left" | "middle" | "right")
3046 : "none";
3047 await session.send("Input.dispatchMouseEvent", {
3048 type,
3049 x: num(message.x),
3050 y: num(message.y),
3051 button,
3052 buttons: num(message.buttons),
3053 clickCount: num(message.clickCount, type === "mouseMoved" ? 0 : 1),
3054 modifiers,
3055 });
3056 if (button !== "none") {
3057 if (action === "down")
3058 viewer.pressedButtons.set(button, { x: num(message.x), y: num(message.y) });
3059 else if (action === "up") viewer.pressedButtons.delete(button);
3060 }
3061 return;
3062 }
3063 case "wheel":
3064 viewer.scrolledAt = Date.now();
3065 await session.send("Input.dispatchMouseEvent", {
3066 type: "mouseWheel",
3067 x: num(message.x),
3068 y: num(message.y),
3069 deltaX: num(message.deltaX),
3070 deltaY: num(message.deltaY),
3071 modifiers,
3072 });
3073 return;
3074 case "key": {
3075 const key = typeof message.key === "string" ? message.key : "";
3076 const code = typeof message.code === "string" ? message.code : "";
3077 const text = typeof message.text === "string" ? message.text : undefined;
3078 if (message.action === "up") {
3079 await session.send("Input.dispatchKeyEvent", { type: "keyUp", key, code, modifiers });
3080 viewer.pressedKeys.delete(code || key);
3081 return;
3082 }
3083 const command = editingCommand(key, modifiers);
3084 const copying =
3085 !tab.desktop && (command?.commands[0] === "copy" || command?.commands[0] === "cut");
3086
3087 // Copy keys get disjoint millisecond windows so each frame's copy belongs to one key.
3088 if (copying)
3089 while (Date.now() <= tab.copyKeyedUntil) await sleepUntil(tab.copyKeyedUntil + 1);
3090 const since = Date.now();
3091 await session.send("Input.dispatchKeyEvent", {
3092 type: text ? "keyDown" : "rawKeyDown",
3093 key,
3094 code,
3095 modifiers,
3096 ...(text ? { text, unmodifiedText: text } : {}),
3097 ...command,
3098 windowsVirtualKeyCode: num(
3099 message.keyCode,
3100 key.length === 1 ? key.toUpperCase().charCodeAt(0) : 0,
3101 ),
3102 });
3103 viewer.pressedKeys.set(code || key, { key, code });
3104 if (!copying) return;
3105 const until = Date.now();
3106 tab.copyKeyedUntil = until;
3107 // A busy frame answers late; its read must not hold later input.
3108 const read = tab.copyReads.then(async () => {
3109 const copied = await takeCopiedText(tab, since, until);
3110 if (copied) viewer.push({ _tag: "clipboard", text: copied });
3111 });
3112 tab.copyReads = read;
3113 return () => read;
3114 }
3115 case "text":
3116 if (typeof message.text === "string" && message.text.length > 0) {
3117 await session.send("Input.insertText", { text: message.text.slice(0, 10_000) });
3118 }
3119 return;
3120 case "resize": {
3121 const width = Math.min(Math.round(num(message.width)), 3840);
3122 const height = Math.min(Math.round(num(message.height)), 2160);
3123 if (width < 100 || height < 100) return;
3124 viewer.requestedSize = { width, height, order: ++viewerResizeOrder };
3125 if (tab.setting._tag !== "fill" || tab.desktop) return;
3126 const current = tab.page.viewportSize();
3127 if (current?.width === width && current.height === height) return;
3128 await tab.page.setViewportSize({ width, height });
3129 await applyZoom(tab);
3130 await broadcastViewport(tab);
3131 return;
3132 }
3133 case "viewport": {
3134 const setting = decodeViewportSetting(message.setting);
3135 await runPromise(
3136 manager.resize({ threadId: tab.threadId, tabId: tab.tabId, viewport: setting }),
3137 );
3138 await applySetting(tab, setting);
3139 return;
3140 }
3141 // A stalled navigation must not block a corrected URL or further viewer input; only the
3142 // bounded wait for a page script that never yields comes first.
3143 case "navigate":
3144 if (typeof message.url === "string") {
3145 const url = normalizePreviewUrl(message.url);
3146 tab.pendingInitialRequestUrl = null;
3147 await leaveBusyPage(tab, session);
3148 tab.control.trackUntilHandoff(tab.page.goto(url, VIEWER_NAVIGATION_OPTIONS));
3149 }
3150 return;
3151 case "history":
3152 tab.pendingInitialRequestUrl = null;
3153 await leaveBusyPage(tab, session);
3154 tab.control.trackUntilHandoff(
3155 num(message.delta) < 0
3156 ? tab.page.goBack(VIEWER_NAVIGATION_OPTIONS)
3157 : tab.page.goForward(VIEWER_NAVIGATION_OPTIONS),
3158 );
3159 return;
3160 case "stopLoading": {
3161 const generation = tab.navigationGeneration;
3162 tab.stoppedGeneration = generation;
3163 await session.send("Page.stopLoading");
3164 // Stopping a committed document need not emit Playwright's load or requestfailed events.
3165 settleStoppedNavigation(tab, generation);
3166 return;
3167 }
3168 case "reload":
3169 tab.pendingInitialRequestUrl = null;
3170 if (tab.crash) {
3171 tab.control.trackUntilHandoff(
3172 tab.page.goto(tab.crashedUrl ?? tab.crash.url, VIEWER_NAVIGATION_OPTIONS),
3173 );
3174 return;
3175 }
3176 await leaveBusyPage(tab, session);
3177 if (message.ignoreCache === true) {
3178 await session.send("Page.reload", { ignoreCache: true });
3179 return;
3180 }
3181 tab.control.trackUntilHandoff(tab.page.reload(VIEWER_NAVIGATION_OPTIONS));
3182 return;
3183 case "probe": {
3184 const x = num(message.x);
3185 const y = num(message.y);
3186 const result = await session.send("Runtime.evaluate", {
3187 expression: `(${EDITABLE_AT_POINT_SCRIPT})(${x}, ${y})`,
3188 returnByValue: true,
3189 });
3190 viewer.push({ _tag: "probe", x, y, editable: result.result.value === true });
3191 return;
3192 }
3193 }
3194 };
3195
3196 const attachViewer: ServerBrowser["Service"]["attachViewer"] = Effect.fn(
3197 "ServerBrowser.attachViewer",
3198 )(function* (input) {
3199 const tab = yield* findTab(input.threadId, input.tabId);
3200 const output = yield* Queue.make<ServerBrowserViewerOutput>({
3201 capacity: VIEWER_OUTPUT_LIMIT,
3202 strategy: "dropping",
3203 });
3204 const session = yield* Effect.acquireRelease(
3205 Effect.tryPromise({
3206 try: () => tab.page.context().newCDPSession(tab.page),
3207 catch: (cause) => new ServerBrowserLaunchError({ cause }),
3208 }),
3209 (session) =>
3210 Effect.promise(() =>
3211 session
3212 .send("Page.stopScreencast")
3213 .catch(constVoid)
3214 .then(() => session.detach())
3215 .catch(constVoid),
3216 ),
3217 );
3218 const quality = Math.min(100, Math.max(1, Math.round(input.quality)));
3219 // Chromium may drop the final frame during a burst; send a still when it settles.
3220 let framesInFlight = 0;
3221 let mayHaveDropped = false;
3222 let motion = false;
3223 const recentFrames: Array<number> = [];
3224 let screencastParams = Promise.resolve();
3225 let screencastScale = 1;
3226 const startScreencast = (scale: number) => {
3227 screencastScale = scale;
3228 screencastParams = screencastParams.then(async () => {
3229 if (tab.capturing > 0) return;
3230 await session.send("Page.stopScreencast").catch(constVoid);
3231 await session
3232 .send("Page.startScreencast", {
3233 format: "jpeg",
3234 quality: screencastScale < 1 ? Math.min(quality, SCREENCAST_MOTION_QUALITY) : quality,
3235 maxWidth: Math.max(1, Math.round(input.maxWidth * screencastScale)),
3236 maxHeight: Math.max(1, Math.round(input.maxHeight * screencastScale)),
3237 })
3238 .catch(constVoid);
3239 });
3240 return screencastParams;
3241 };
3242 const viewer: ViewerState = {
3243 id: NodeCrypto.randomUUID(),
3244 canOperate: input.canOperate,
3245 pressedKeys: new Map(),
3246 pressedButtons: new Map(),
3247 push: (next) => {
3248 // Dropped frames must still release Chromium.
3249 if (Queue.offerUnsafe(output, next)) return;
3250 if (next._tag === "frame") runFork(next.ack);
3251 // State a stalled viewer cannot miss compacts its backlog. It runs
3252 // synchronously so an older compaction can never land after a newer one.
3253 else if (["gone", "reconnect", "control", "viewport", "fileChooser"].includes(next._tag)) {
3254 const dropped = runSyncExit(Queue.clear(output));
3255 if (dropped._tag === "Failure") return;
3256 const items = [...dropped.value, next];
3257 // Without its latest size and control the viewer cannot map input at all.
3258 const state = (["control", "viewport"] as const).flatMap(
3259 (tag) => items.findLast((item) => item._tag === tag) ?? [],
3260 );
3261 // Everything else is one-time; if it no longer fits, the oldest goes first.
3262 const rest = items.filter(
3263 (item) => item._tag !== "frame" && item._tag !== "control" && item._tag !== "viewport",
3264 );
3265 for (const item of [...rest.slice(state.length - VIEWER_OUTPUT_LIMIT), ...state])
3266 Queue.offerUnsafe(output, item);
3267 for (const item of dropped.value) if (item._tag === "frame") runFork(item.ack);
3268 }
3269 },
3270 pause: () => {
3271 screencastParams = screencastParams.then(() =>
3272 session.send("Page.stopScreencast").then(constVoid, constVoid),
3273 );
3274 return screencastParams;
3275 },
3276 resume: () => startScreencast(screencastScale),
3277 scrolledAt: 0,
3278 requestedSize: null,
3279 };
3280 yield* Effect.acquireRelease(
3281 Effect.sync(() => {
3282 tab.viewers.add(viewer);
3283 tab.viewerAttached?.resolve();
3284 tab.viewerAttached = null;
3285 reportLiveTabs();
3286 }),
3287 () =>
3288 Effect.promise(async () => {
3289 await tab.control.disconnect(viewer.id, () => releaseViewerInput(viewer, session));
3290 tab.viewers.delete(viewer);
3291 broadcastControl(tab);
3292 reportLiveTabs();
3293 }),
3294 );
3295 if (input.canOperate && tab.control.agentId === null && tab.control.controller === null) {
3296 yield* Effect.promise(() => tab.control.take(viewer.id));
3297 }
3298 broadcastControl(tab);
3299 pushFileChooser(tab);
3300 const pushStill = async () => {
3301 const data = await withCaptureLock(tab, async () => {
3302 let timer: ReturnType<typeof setTimeout> | undefined;
3303 try {
3304 return await Promise.race([
3305 ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }),
3306 new Promise<null>((resolve) => {
3307 timer = setTimeout(() => resolve(null), VIEWER_STILL_TIMEOUT_MS);
3308 }),
3309 ]);
3310 } finally {
3311 clearTimeout(timer);
3312 }
3313 }).catch(() => null);
3314 if (data) viewer.push({ _tag: "frame", data: Buffer.from(data, "base64"), ack: Effect.void });
3315 };
3316 let settleTimer: ReturnType<typeof setTimeout> | null = null;
3317 yield* Effect.addFinalizer(() =>
3318 Effect.sync(() => {
3319 if (settleTimer !== null) clearTimeout(settleTimer);
3320 }),
3321 );
3322 let screencastStarted = false;
3323 session.on("Page.screencastFrame", (frame) => {
3324 screencastStarted = true;
3325 if (framesInFlight > 0) mayHaveDropped = true;
3326 framesInFlight += 1;
3327 const arrivedAt = Date.now();
3328 recentFrames.push(arrivedAt);
3329 while (recentFrames[0]! < arrivedAt - SCREENCAST_MOTION_WINDOW_MS) recentFrames.shift();
3330 if (
3331 !motion &&
3332 recentFrames.length >= SCREENCAST_MOTION_FRAMES &&
3333 arrivedAt - viewer.scrolledAt < SCREENCAST_MOTION_WINDOW_MS
3334 ) {
3335 motion = true;
3336 void startScreencast(0.5);
3337 } else if (motion && arrivedAt - viewer.scrolledAt > SCREENCAST_SETTLE_MS) {
3338 motion = false;
3339 recentFrames.length = 0;
3340 void startScreencast(1);
3341 }
3342 if (settleTimer !== null) clearTimeout(settleTimer);
3343 settleTimer = setTimeout(() => {
3344 settleTimer = null;
3345 if (!motion && !mayHaveDropped) return;
3346 mayHaveDropped = false;
3347 if (motion) {
3348 motion = false;
3349 recentFrames.length = 0;
3350 void startScreencast(1);
3351 }
3352 void pushStill();
3353 }, SCREENCAST_SETTLE_MS);
3354 viewer.push({
3355 _tag: "frame",
3356 data: Buffer.from(frame.data, "base64"),
3357 ack: Effect.promise(() =>
3358 sleepUntil(arrivedAt + SCREENCAST_ACK_PACE_MS).then(() => {
3359 framesInFlight -= 1;
3360 return session
3361 .send("Page.screencastFrameAck", { sessionId: frame.sessionId })
3362 .catch(constVoid);
3363 }),
3364 ),
3365 });
3366 });
3367 void broadcastViewport(tab).catch(constVoid);
3368 yield* Effect.promise(() => startScreencast(1));
3369 // An idle page does not repaint for a new screencast, so the viewer
3370 // starts from a still unless a live frame beat it.
3371 if (!screencastStarted) yield* Effect.forkScoped(Effect.promise(pushStill));
3372 return {
3373 output,
3374 input: (raw: unknown) =>
3375 Effect.promise(async () => {
3376 if (!viewer.canOperate) return;
3377 const message = asRecord(raw);
3378 if (!message) return;
3379 try {
3380 if (message.type === "takeControl") {
3381 const taking = tab.control.take(viewer.id);
3382 broadcastControl(tab);
3383 await taking;
3384 pushFileChooser(tab);
3385 } else if (message.type === "releaseControl") {
3386 const releasing = tab.control.release(viewer.id, () =>
3387 releaseViewerInput(viewer, session),
3388 );
3389 broadcastControl(tab);
3390 await releasing;
3391 } else if (
3392 message.type === "dialog" &&
3393 tab.control.controller === viewer.id &&
3394 typeof message.accept === "boolean"
3395 ) {
3396 await resolveDialog(tab, {
3397 accept: message.accept,
3398 ...(typeof message.promptText === "string"
3399 ? { promptText: message.promptText }
3400 : {}),
3401 });
3402 } else {
3403 const dispatch = async (input: Record<string, unknown>) => {
3404 await dispatchViewerInput(tab, session, viewer, input);
3405 };
3406 if (
3407 message.type === "mouse" &&
3408 message.action === "move" &&
3409 message.button === "none" &&
3410 message.buttons === 0
3411 ) {
3412 await tab.control.humanLatest(
3413 viewer.id,
3414 `hover:${modifiersOf(message)}`,
3415 message,
3416 dispatch,
3417 );
3418 } else if (message.type === "wheel") {
3419 // A reversal stays separate so scrolling clamped at an edge behaves as if unmerged.
3420 await tab.control.humanLatest(
3421 viewer.id,
3422 `wheel:${num(message.x)}:${num(message.y)}:${modifiersOf(message)}:${Math.sign(num(message.deltaX))}:${Math.sign(num(message.deltaY))}`,
3423 message,
3424 dispatch,
3425 (waiting, next) => ({
3426 ...next,
3427 deltaX: num(waiting.deltaX) + num(next.deltaX),
3428 deltaY: num(waiting.deltaY) + num(next.deltaY),
3429 }),
3430 );
3431 } else {
3432 const copyRead = await tab.control.human(viewer.id, () =>
3433 dispatchViewerInput(tab, session, viewer, message),
3434 );
3435 await copyRead?.();
3436 }
3437 }
3438 } catch {
3439 broadcastControl(tab);
3440 }
3441 }),
3442 } satisfies ServerBrowserViewer;
3443 });
3444
3445 // Subscribe before forking: a forked stream subscribes only once it runs, so a tab opened
3446 // right after startup would publish its `opened` event to no one and never load.
3447 const managerEvents = yield* manager.subscribeEvents;
3448 yield* Stream.fromSubscription(managerEvents).pipe(
3449 Stream.runForEach(mirrorManagerEvent),
3450 Effect.forkScoped,
3451 );
3452 if (
3453 !PreviewBrowserHost.sandboxDisabled(yield* HostProcess.Environment) &&
3454 (yield* PreviewBrowserHost.sandboxBlocked)
3455 ) {
3456 yield* Effect.logWarning(
3457 `This host blocks the sandbox T3's browser runs in, so browser tabs and HTML previews will not start. Run \`${setupCommand}\` once to allow it.`,
3458 );
3459 }
3460
3461 yield* desktopChannel.detached.pipe(
3462 Stream.runForEach((key) =>
3463 Effect.sync(() => {
3464 const tab = tabs.get(tabKey(key.threadId, key.tabId));
3465 if (tab?.desktop) dropTab(tab, false);
3466 }),
3467 ),
3468 Effect.forkScoped,
3469 );
3470 // The desktop keeps the tabs an agent uses focused until its turn ends, not just per command.
3471 yield* Stream.fromSubscription(agentTurns).pipe(
3472 Stream.runForEach((change) => desktopChannel.turn(change.threadId, change.active)),
3473 Effect.forkScoped,
3474 );
3475 // Reconnect returned pages immediately so all clients keep receiving URL and title changes.
3476 yield* desktopChannel.attached.pipe(
3477 Stream.runForEach((key) =>
3478 Effect.gen(function* () {
3479 if (tabs.has(tabKey(key.threadId, key.tabId))) return;
3480 const { sessions } = yield* manager.list({ threadId: ThreadId.make(key.threadId) });
3481 const snapshot = sessions.find(
3482 (session) => session.tabId === key.tabId && session.runtime === "server",
3483 );
3484 if (snapshot) yield* Effect.promise(() => ensureTab(snapshot).catch(constVoid));
3485 }).pipe(Effect.ignore),
3486 ),
3487 Effect.forkScoped,
3488 );
3489 yield* Effect.sync(closeIdleAgentTabs).pipe(
3490 Effect.repeat(Schedule.spaced(IDLE_SWEEP_INTERVAL)),
3491 Effect.forkScoped,
3492 );
3493 const environmentId = yield* environment.getEnvironmentId;
3494 const hostSession = broker
3495 .connect(
3496 {
3497 clientId: SERVER_HOST_CLIENT_ID,
3498 environmentId,
3499 supportedOperations: [...PREVIEW_AUTOMATION_SERVER_OPERATIONS],
3500 },
3501 { preferred: true },
3502 )
3503 .pipe(
3504 Effect.flatMap((events) =>
3505 events.pipe(
3506 Stream.runForEach((event) => {
3507 if (event.type === "connected") {
3508 hostConnectionId = event.connectionId;
3509 return Effect.sync(reportLiveTabs);
3510 }
3511 return handleRequest(event.connectionId, event.request).pipe(
3512 Effect.forkScoped,
3513 Effect.asVoid,
3514 );
3515 }),
3516 ),
3517 ),
3518 );
3519 // The broker disconnects timed-out hosts, including slow first installs. Reconnect.
3520 yield* hostSession.pipe(
3521 Effect.exit,
3522 Effect.andThen(Effect.sleep(HOST_RECONNECT_DELAY)),
3523 Effect.forever,
3524 Effect.forkScoped,
3525 );
3526 yield* Effect.addFinalizer(() =>
3527 Effect.promise(async () => {
3528 await contexts.close();
3529 }),
3530 );
3531
3532 const clearProfile = (profileId: string) =>
3533 Effect.tryPromise({
3534 try: () => contexts.clearProfile(profileId),
3535 catch: (cause) => new PreviewClearProfileError({ profileId, cause }),
3536 });
3537
3538 return ServerBrowser.of({
3539 attachViewer,
3540 clearProfile,
3541 profileReporter,
3542 openDownload,
3543 answerFileChooser,
3544 });
3545 });
3546
3547 export const layer = Layer.effect(ServerBrowser, make);