diff --git a/apps/desktop/src/backend/DesktopBackendConfiguration.ts b/apps/desktop/src/backend/DesktopBackendConfiguration.ts index 82f0a30a40cd9..0d673392c8658 100644 --- a/apps/desktop/src/backend/DesktopBackendConfiguration.ts +++ b/apps/desktop/src/backend/DesktopBackendConfiguration.ts @@ -586,6 +586,7 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl input: SharedBootstrapInput & { readonly port: number; readonly distro: string | null; + readonly primary?: boolean; }, ): Effect.fn.Return< DesktopBackendManager.DesktopBackendStartConfig, @@ -611,6 +612,8 @@ const resolveWslStartConfig = Effect.fn("desktop.backendConfiguration.resolveWsl host: wslBindHost, desktopBootstrapToken: input.bootstrapToken, desktopBootstrapSecret: input.bootstrapSecret, + // wsl.exe drops the browser fds. Only the primary's tabs are drawn by this app. + ...(input.primary ? { desktopBrowserSocket: true } : {}), // PortSchema rejects 0 even when Tailscale Serve is disabled. tailscaleServeEnabled: false, tailscaleServePort: 443, @@ -816,6 +819,7 @@ export const make = Effect.gen(function* () { ...shared, port: backendExposure.port, distro: persistedSettings.wslDistro, + primary: true, }).pipe( Effect.provideService(DesktopEnvironment.DesktopEnvironment, environment), Effect.provideService(DesktopWslEnvironment.DesktopWslEnvironment, wslEnvironment), diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 8b4f154b5d4de..f6d23d7add2b6 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -32,6 +32,7 @@ import { waitForHttpReady as waitForHttpReadyShared } from "@t3tools/shared/http import * as DesktopObservability from "../app/DesktopObservability.ts"; import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts"; import * as DesktopBrowserHost from "../preview/DesktopBrowserHost.ts"; +import * as DesktopBrowserSocket from "../preview/DesktopBrowserSocket.ts"; import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts"; const INITIAL_RESTART_DELAY = Duration.millis(500); @@ -564,6 +565,20 @@ export const runBackendProcess = Effect.fn("runBackendProcess")(function* ( Effect.forkScoped, ); } + if ( + options.bootstrap.desktopBrowserSocket === true && + options.bootstrap.desktopBootstrapSecret !== undefined && + options.desktopBrowserStream !== undefined && + options.onDesktopBrowserCommand !== undefined + ) { + yield* DesktopBrowserSocket.run({ + httpBaseUrl: options.httpBaseUrl, + secret: options.bootstrap.desktopBootstrapSecret, + events: options.desktopBrowserStream, + onCommand: options.onDesktopBrowserCommand, + distro: options.runningDistro, + }).pipe(Effect.forkScoped); + } if (options.captureOutput) { const outputContext = { executablePath: options.executablePath, diff --git a/apps/desktop/src/preview/DesktopBrowserSocket.test.ts b/apps/desktop/src/preview/DesktopBrowserSocket.test.ts new file mode 100644 index 0000000000000..b5d17c9698928 --- /dev/null +++ b/apps/desktop/src/preview/DesktopBrowserSocket.test.ts @@ -0,0 +1,53 @@ +import { expect, it } from "vite-plus/test"; + +import { translateWslPaths } from "./DesktopBrowserSocket.ts"; + +const key = { threadId: "thread-1", tabId: "tab-1" }; +const cdp = (message: object) => + JSON.stringify({ type: "cdp", ...key, message: JSON.stringify(message) }); +const translated = (message: object) => + JSON.parse(JSON.parse(translateWslPaths(cdp(message), "Ubuntu")).message).params; + +it("translates the paths a WSL server sends to the Windows page, and nothing else", () => { + expect( + translated({ + id: 1, + method: "DOM.setFileInputFiles", + params: { + objectId: "1", + files: ["/home/alice/project/report.csv", "/mnt/c/Users/alice/a b.txt"], + }, + }), + ).toEqual({ + objectId: "1", + files: [ + "\\\\wsl.localhost\\Ubuntu\\home\\alice\\project\\report.csv", + "C:\\Users\\alice\\a b.txt", + ], + }); + expect( + translated({ + id: 2, + method: "Browser.setDownloadBehavior", + params: { + behavior: "allowAndName", + downloadPath: "/tmp/playwright-artifacts-1", + eventsEnabled: true, + }, + }), + ).toEqual({ + behavior: "allowAndName", + downloadPath: "\\\\wsl.localhost\\Ubuntu\\tmp\\playwright-artifacts-1", + eventsEnabled: true, + }); + + // A page may type either method's name; only the command itself is rewritten. + const typed = cdp({ + id: 3, + method: "Input.insertText", + params: { text: "DOM.setFileInputFiles /home" }, + }); + expect(translateWslPaths(typed, "Ubuntu")).toBe(typed); + const pointer = JSON.stringify({ type: "pointer", ...key, phase: "move", x: 1, y: 2 }); + expect(translateWslPaths(pointer, "Ubuntu")).toBe(pointer); +}); diff --git a/apps/desktop/src/preview/DesktopBrowserSocket.ts b/apps/desktop/src/preview/DesktopBrowserSocket.ts new file mode 100644 index 0000000000000..1ab013d726349 --- /dev/null +++ b/apps/desktop/src/preview/DesktopBrowserSocket.ts @@ -0,0 +1,126 @@ +/** Carries the desktop browser channel over a WebSocket for a backend whose launcher drops fds. */ +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import * as NodeSocket from "@effect/platform-node/NodeSocket"; +import { + DESKTOP_BROWSER_CHANNEL_PATH, + DESKTOP_BROWSER_CHANNEL_PING, + DesktopBrowserCommand, +} from "@t3tools/contracts"; +import { currentDesktopBootstrapToken } from "@t3tools/shared/desktopBootstrapToken"; +import * as Clock from "effect/Clock"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import * as Option from "effect/Option"; +import * as Schedule from "effect/Schedule"; +import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; +import * as Socket from "effect/socket/Socket"; + +import { wslLinuxPathToWindowsPath } from "../wsl/wslPathParsing.ts"; + +const REDIAL_INTERVAL = "1 second"; +// The server pings every 10 seconds; a socket silent for this long is dead without having closed. +const SILENCE_LIMIT = "30 seconds"; + +// Effect drops its listeners before closing a socket that is still connecting, and `ws` then +// emits an `error` nobody handles, which would exit the app while the backend boots. +const webSocketConstructor = Layer.effect( + Socket.WebSocketConstructor, + Effect.gen(function* () { + const make = yield* Socket.WebSocketConstructor; + return (url, options) => { + const socket = make(url, options); + socket.addEventListener("error", () => undefined); + return socket; + }; + }), +).pipe(Layer.provide(NodeSocket.layerWebSocketConstructor)); + +const commandCodec = Schema.fromJsonString(DesktopBrowserCommand); +const decodeCommand = Schema.decodeUnknownOption(commandCodec); +const encodeCommand = Schema.encodeSync(commandCodec); + +/** + * The server names files on its own filesystem: the ones to upload and the folder downloads land + * in. A WSL server's paths mean nothing to the Windows page until they are translated. + */ +export const translateWslPaths = (line: string, distro: string): string => { + // Most traffic is input and screencast acks; only these two commands carry paths. + if (!line.includes("DOM.setFileInputFiles") && !line.includes("Browser.setDownloadBehavior")) { + return line; + } + const command = Option.getOrUndefined(decodeCommand(line)); + if (command?.type !== "cdp") return line; + let message: { method?: unknown; params?: Record }; + try { + message = JSON.parse(command.message); + } catch { + return line; + } + const params = message.params; + if (typeof params !== "object" || params === null) return line; + const translate = (path: unknown) => + typeof path === "string" && path.startsWith("/") + ? wslLinuxPathToWindowsPath(distro, path) + : path; + if (message.method === "DOM.setFileInputFiles" && Array.isArray(params["files"])) { + params["files"] = params["files"].map(translate); + } else if (message.method === "Browser.setDownloadBehavior") { + params["downloadPath"] = translate(params["downloadPath"]); + } else { + return line; + } + return encodeCommand({ ...command, message: JSON.stringify(message) }); +}; + +/** Never returns: redials until interrupted, since the backend listens only once it has booted. */ +export const run = Effect.fn("DesktopBrowserSocket.run")(function* (options: { + readonly httpBaseUrl: URL; + readonly secret: string; + /** Subscribed anew on each dial, so the host announces its tabs to every connection. */ + readonly events: Stream.Stream; + readonly onCommand: (line: string) => Effect.Effect; + /** The WSL distro the backend runs in, when its filesystem is not this machine's. */ + readonly distro?: string | undefined; +}) { + const url = new URL(DESKTOP_BROWSER_CHANNEL_PATH, options.httpBaseUrl); + url.protocol = url.protocol === "https:" ? "wss:" : "ws:"; + const decoder = new TextDecoder(); + const dial = Effect.gen(function* () { + const token = yield* currentDesktopBootstrapToken( + options.secret, + yield* Clock.currentTimeMillis, + ); + const socket = yield* Socket.makeWebSocket(url.href, { + openTimeout: "10 seconds", + headers: { authorization: `Bearer ${token}` }, + }); + const writer = yield* socket.writer; + const reader = yield* socket.reader; + const commands = reader.pull.pipe( + Effect.timeout(SILENCE_LIMIT), + Effect.flatMap((frames) => + Effect.forEach( + frames, + (frame) => { + const line = typeof frame === "string" ? frame : decoder.decode(frame); + if (line === DESKTOP_BROWSER_CHANNEL_PING) return Effect.void; + return options.onCommand( + options.distro === undefined ? line : translateWslPaths(line, options.distro), + ); + }, + { discard: true }, + ), + ), + Effect.forever, + ); + const events = options.events.pipe( + Stream.runForEach((line) => writer.write(decoder.decode(line))), + ); + return yield* Effect.raceFirst(commands, events); + }).pipe(Effect.scoped, Effect.ignore); + return yield* dial.pipe( + Effect.repeat(Schedule.spaced(REDIAL_INTERVAL)), + Effect.provide(Layer.merge(webSocketConstructor, NodeCrypto.layer)), + ); +}); diff --git a/apps/desktop/src/wsl/wslPathParsing.ts b/apps/desktop/src/wsl/wslPathParsing.ts index 1df64f91dac0e..c2479799e7fa1 100644 --- a/apps/desktop/src/wsl/wslPathParsing.ts +++ b/apps/desktop/src/wsl/wslPathParsing.ts @@ -59,6 +59,13 @@ export function wslUncPathToLinuxPath(windowsPath: string): string | null { return `/${rest.split("\\").filter(Boolean).join("/")}`; } +/** Windows reaches drives WSL mounts by their letter, and everything else through the distro share. */ +export function wslLinuxPathToWindowsPath(distroName: string, linuxPath: string): string { + const drive = /^\/mnt\/([a-z])(?:\/(.*))?$/i.exec(linuxPath); + if (drive) return `${drive[1]!.toUpperCase()}:\\${(drive[2] ?? "").replaceAll("/", "\\")}`; + return `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`; +} + function resolveWslHomeUncPath(config: WslConfig, distros: readonly WslDistro[]): string | null { const distroName = config.distro ?? distros.find((distro) => distro.isDefault)?.name ?? null; return distroName ? `\\\\wsl.localhost\\${distroName}\\home` : null; diff --git a/apps/server/src/cli/config.ts b/apps/server/src/cli/config.ts index 25d6c6eb62dd6..955e1b2ac386e 100644 --- a/apps/server/src/cli/config.ts +++ b/apps/server/src/cli/config.ts @@ -392,6 +392,7 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct const desktopAppUpdate = bootstrap?.desktopAppUpdate; const desktopBrowserFd = bootstrap?.desktopBrowserFd; const desktopBrowserControlFd = bootstrap?.desktopBrowserControlFd; + const desktopBrowserSocket = bootstrap?.desktopBrowserSocket; const resourceMonitorPath = bootstrap?.resourceMonitorPath; const autoBootstrapProjectFromCwd = Option.getOrElse( resolveOptionPrecedence( @@ -526,6 +527,7 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct ...(desktopAppUpdate === undefined ? {} : { desktopAppUpdate }), desktopBrowserFd, desktopBrowserControlFd, + ...(desktopBrowserSocket === undefined ? {} : { desktopBrowserSocket }), resourceMonitorPath, autoBootstrapProjectFromCwd, logWebSocketEvents, diff --git a/apps/server/src/config.ts b/apps/server/src/config.ts index c707c912d672d..608f164458edc 100644 --- a/apps/server/src/config.ts +++ b/apps/server/src/config.ts @@ -104,6 +104,7 @@ export class ServerConfig extends Context.Service< readonly desktopAppUpdate?: boolean | undefined; readonly desktopBrowserFd?: number | undefined; readonly desktopBrowserControlFd?: number | undefined; + readonly desktopBrowserSocket?: boolean | undefined; readonly resourceMonitorPath?: string | undefined; readonly autoBootstrapProjectFromCwd: boolean; readonly logWebSocketEvents: boolean; diff --git a/apps/server/src/preview/DesktopBrowserChannel.test.ts b/apps/server/src/preview/DesktopBrowserChannel.test.ts index bf5cbc2d3c3ff..5627b572b26e7 100644 --- a/apps/server/src/preview/DesktopBrowserChannel.test.ts +++ b/apps/server/src/preview/DesktopBrowserChannel.test.ts @@ -1,11 +1,14 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import * as NodeStream from "@effect/platform-node/NodeStream"; import { expect, it } from "@effect/vitest"; +import * as Cause from "effect/Cause"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; +import * as TestClock from "effect/testing/TestClock"; +import * as Socket from "effect/socket/Socket"; import { vi } from "vite-plus/test"; import * as ServerConfig from "../config.ts"; @@ -111,3 +114,96 @@ it.live("closes a tab's relay connection when the desktop detaches the tab", () ), ), ); + +it.effect( + "a desktop without fds attaches its tabs over a socket, and loses them when it drops", + () => + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + yield* Effect.gen(function* () { + const channel = yield* DesktopBrowserChannel.DesktopBrowserChannel; + const key = { threadId: "thread-1", tabId: "tab-1" }; + const connect = Effect.gen(function* () { + const frames = yield* Queue.unbounded(); + const commands = yield* Queue.unbounded(); + const socket = Socket.make({ + reader: Effect.succeed({ + pull: Queue.take(frames).pipe( + Effect.map((frame) => [frame] as const), + Effect.mapError( + () => + new Socket.SocketError({ reason: new Socket.SocketCloseError({ code: 1006 }) }), + ), + ), + upgrade: () => Effect.void, + }), + writer: Effect.succeed({ + write: (chunk) => + typeof chunk === "string" + ? Effect.asVoid(Queue.offer(commands, chunk)) + : Effect.void, + writeAll: () => Effect.void, + }), + }); + const served = yield* channel.serve(socket).pipe(Effect.forkChild); + const send = (event: object) => Queue.offer(frames, JSON.stringify(event)); + return { frames, commands, served, send }; + }); + expect(channel.available).toBe(true); + + const first = yield* connect; + yield* first.send({ type: "attached", ...key }); + expect(yield* channel.awaitAttached(key, "5 seconds")).toBe(true); + // An idle socket still carries the ping a desktop tells a dead connection by. + yield* TestClock.adjust("10 seconds"); + expect(yield* Queue.take(first.commands)).toBe("ping"); + const turn = { type: "turn", threadId: key.threadId, active: true }; + yield* channel.turn(key.threadId, true); + expect(JSON.parse(yield* Queue.take(first.commands))).toEqual(turn); + // The desktop redials before the first socket's close is seen, while an endpoint is open. + const second = yield* Effect.scoped( + Effect.gen(function* () { + yield* channel.endpoint(key); + const detached = yield* channel.detached.pipe(Stream.runHead, Effect.forkChild); + const replacement = yield* connect; + yield* Fiber.join(detached); + expect(yield* channel.isAttached(key)).toBe(false); + yield* replacement.send({ type: "attached", ...key }); + expect(yield* channel.awaitAttached(key, "5 seconds")).toBe(true); + return replacement; + }), + ); + // Neither the old endpoint's release nor the first socket's late close reaches the new tab. + yield* Queue.end(first.frames); + yield* Fiber.join(first.served); + expect(yield* channel.isAttached(key)).toBe(true); + yield* channel.pointer(key, { phase: "move", x: 1, y: 2 }); + // A desktop that redialed forgot the running turn, so it hears of it again first. + expect(JSON.parse(yield* Queue.take(second.commands))).toEqual(turn); + expect(JSON.parse(yield* Queue.take(second.commands))).toEqual({ + type: "pointer", + ...key, + phase: "move", + x: 1, + y: 2, + }); + expect(yield* Queue.size(first.commands)).toBe(0); + + yield* Queue.end(second.frames); + yield* Fiber.join(second.served); + expect(yield* channel.isAttached(key)).toBe(false); + }).pipe( + Effect.provide( + DesktopBrowserChannel.layer.pipe( + Layer.provide(ServerConfig.layer({ ...config, desktopBrowserSocket: true })), + ), + ), + ); + }).pipe( + Effect.provide( + ServerConfig.layerTest(process.cwd(), { prefix: "desktop-browser-channel-" }).pipe( + Layer.provide(NodeServices.layer), + ), + ), + ), +); diff --git a/apps/server/src/preview/DesktopBrowserChannel.ts b/apps/server/src/preview/DesktopBrowserChannel.ts index 4da0efe3ce8f0..731d6b93c91f4 100644 --- a/apps/server/src/preview/DesktopBrowserChannel.ts +++ b/apps/server/src/preview/DesktopBrowserChannel.ts @@ -3,14 +3,20 @@ import * as NodeStream from "@effect/platform-node/NodeStream"; import * as NodeSocketServer from "@effect/platform-node/NodeSocketServer"; import { + DESKTOP_BROWSER_CHANNEL_PATH, + DESKTOP_BROWSER_CHANNEL_PING, DesktopBrowserCommand, DesktopBrowserEvent, type DesktopBrowserCommand as DesktopBrowserCommandType, + type DesktopBrowserEvent as DesktopBrowserEventType, type PreviewAutomationLoadError, } from "@t3tools/contracts"; +import { isValidDesktopBootstrapToken } from "@t3tools/shared/desktopBootstrapToken"; +import * as Clock from "effect/Clock"; import * as Context from "effect/Context"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as PubSub from "effect/PubSub"; @@ -19,6 +25,8 @@ import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; +import type * as Socket from "effect/socket/Socket"; +import * as Mac from "effect/crypto/Mac"; import * as Ndjson from "effect/encoding/Ndjson"; import * as NodeCrypto from "node:crypto"; import * as NodeNet from "node:net"; @@ -27,6 +35,7 @@ import * as ServerConfig from "../config.ts"; import { writeAllToFileDescriptor } from "../resourceTelemetry/DesktopTelemetryReceiver.ts"; const decodeEvent = Schema.decodeUnknownEffect(DesktopBrowserEvent); +const decodeEventLine = Schema.decodeUnknownEffect(Schema.fromJsonString(DesktopBrowserEvent)); const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(DesktopBrowserCommand)); export interface DesktopTabKey { @@ -34,6 +43,8 @@ export interface DesktopTabKey { readonly tabId: string; } +const PING_INTERVAL = "10 seconds"; + const keyOf = ({ threadId, tabId }: DesktopTabKey) => `${threadId}\u0000${tabId}`; export class DesktopBrowserChannel extends Context.Service< @@ -58,6 +69,8 @@ export class DesktopBrowserChannel extends Context.Service< ) => Effect.Effect; /** Never fails. A failed write means a broken channel; the next backend starts with no turns. */ readonly turn: (threadId: string, active: boolean) => Effect.Effect; + /** Carries the channel over a desktop's WebSocket until it closes. No-op with fds or no desktop. */ + readonly serve: (socket: Socket.Socket) => Effect.Effect; } >()("t3/preview/DesktopBrowserChannel") {} @@ -66,7 +79,7 @@ const make = Effect.gen(function* () { const inputFd = config.desktopBrowserFd; const controlFd = config.desktopBrowserControlFd; const changes = yield* PubSub.unbounded<{ key: DesktopTabKey; attached: boolean }>(); - const attachedTabs = new Set(); + const attachedTabs = new Map(); // Missing reports protect desktop tabs until the renderer explicitly marks them hidden. const shownTabs = new Map(); const shownReports = yield* PubSub.unbounded(); @@ -74,7 +87,9 @@ const make = Effect.gen(function* () { const inbound = new Map>(); const writeLock = yield* Semaphore.make(1); - if (inputFd === undefined || controlFd === undefined) { + const overSocket = config.desktopBrowserSocket === true; + const overFds = inputFd !== undefined && controlFd !== undefined; + if (!overSocket && !overFds) { return DesktopBrowserChannel.of({ available: false, awaitAttached: Effect.fn("DesktopBrowserChannel.awaitAttached")(function () { @@ -99,75 +114,150 @@ const make = Effect.gen(function* () { return Effect.succeed(false); }), loadFailure: () => undefined, + serve: Effect.fn("DesktopBrowserChannel.serve")(function () { + return Effect.void; + }), }); } - const command = (message: DesktopBrowserCommandType) => + // The desktop's socket once it dials in; commands sent before then have no tab to reach. + const activeTurns = new Set(); + let socketWrite: ((line: string) => Effect.Effect) | null = null; + const write = + (stillWanted: () => boolean) => + (line: string): Effect.Effect => { + if (!stillWanted()) return Effect.void; + return overFds + ? writeAllToFileDescriptor(controlFd, Buffer.from(`${line}\n`)) + : (socketWrite?.(line) ?? Effect.void); + }; + /** `stillWanted` is asked once the write's turn comes, since the connection can change in the wait. */ + const command = (message: DesktopBrowserCommandType, stillWanted: () => boolean = () => true) => writeLock.withPermits(1)( encodeCommand(message).pipe( - Effect.flatMap((line) => writeAllToFileDescriptor(controlFd, Buffer.from(`${line}\n`))), + Effect.flatMap(write(stillWanted)), Effect.catchCause((cause) => Effect.logWarning("desktop browser command failed", { cause }), ), ), ); - // Use cancellable socket reads; filesystem pipe reads keep Node alive after scope shutdown. - const readable = yield* Effect.acquireRelease( - Effect.sync(() => new NodeNet.Socket({ fd: inputFd, readable: true, writable: false })), - (stream) => Effect.sync(() => stream.destroy()), - ); - yield* NodeStream.fromReadable({ - evaluate: () => readable, - closeOnDone: true, - onError: (cause) => (cause instanceof Error ? cause : new Error(String(cause))), - }).pipe( - Stream.pipeThroughChannel(Ndjson.decode({ ignoreEmptyLines: true })), - Stream.mapEffect((value) => decodeEvent(value).pipe(Effect.option)), - Stream.runForEach((decoded) => { - if (Option.isNone(decoded)) return Effect.void; - const event = decoded.value; - const key = { threadId: event.threadId, tabId: event.tabId }; - const id = keyOf(key); - switch (event.type) { - case "cdp": { - const queue = inbound.get(id); - return queue ? Queue.offer(queue, event.message).pipe(Effect.asVoid) : Effect.void; - } - case "attached": - attachedTabs.add(id); - return PubSub.publish(changes, { key, attached: true }); - case "loadFailed": - loadFailures.set(id, { - url: event.url.slice(0, 2048), - code: event.code, - description: event.description, - }); - return Effect.void; - case "detached": { - attachedTabs.delete(id); - shownTabs.delete(id); - loadFailures.delete(id); - const queue = inbound.get(id); - return (queue ? Queue.shutdown(queue) : Effect.void).pipe( - Effect.andThen(PubSub.publish(changes, { key, attached: false })), - ); - } - case "shown": - shownTabs.set(id, event.shown); - return event.shown ? PubSub.publish(shownReports, id) : Effect.void; + const receive = (event: DesktopBrowserEventType): Effect.Effect => { + const key = { threadId: event.threadId, tabId: event.tabId }; + const id = keyOf(key); + switch (event.type) { + case "cdp": { + const queue = inbound.get(id); + return queue ? Queue.offer(queue, event.message).pipe(Effect.asVoid) : Effect.void; } - }), - Effect.catchCause((cause) => Effect.logWarning("desktop browser channel stopped", { cause })), - Effect.ensuring( - Effect.sync(() => { - shownTabs.clear(); - loadFailures.clear(); - }), - ), - Effect.forkScoped, + case "attached": + attachedTabs.set(id, key); + return PubSub.publish(changes, { key, attached: true }).pipe(Effect.asVoid); + case "loadFailed": + loadFailures.set(id, { + url: event.url.slice(0, 2048), + code: event.code, + description: event.description, + }); + return Effect.void; + case "detached": { + attachedTabs.delete(id); + shownTabs.delete(id); + loadFailures.delete(id); + const queue = inbound.get(id); + return (queue ? Queue.shutdown(queue) : Effect.void).pipe( + Effect.andThen(PubSub.publish(changes, { key, attached: false })), + Effect.asVoid, + ); + } + case "shown": + shownTabs.set(id, event.shown); + return event.shown ? PubSub.publish(shownReports, id).pipe(Effect.asVoid) : Effect.void; + } + }; + + // A connection's tabs go with it; the desktop announces them again on the one that replaces it. + const retireConnection = Effect.suspend(() => { + socketWrite = null; + return Effect.forEach( + [...attachedTabs.values()], + (key) => receive({ type: "detached", ...key }), + { discard: true }, + ); + }); + + const serve = Effect.fn("DesktopBrowserChannel.serve")( + function* (socket: Socket.Socket) { + if (!overSocket) return; + // Before the writer is published: a socket that never opens would hold a write forever. + const reader = yield* socket.reader; + const writer = yield* socket.writer; + const send = (line: string) => writer.write(line); + // A redial can arrive before the old socket's close is seen here. + yield* retireConnection; + socketWrite = send; + // Outside the write lock: a frame cannot interleave, and a dead socket must not hold commands. + yield* send(DESKTOP_BROWSER_CHANNEL_PING).pipe( + Effect.delay(PING_INTERVAL), + Effect.repeat({ while: () => socketWrite === send }), + Effect.ignore, + Effect.forkScoped, + ); + // The desktop forgets its turns when it dials. Its first frame shows that it has by now. + let turnsRestored = false; + const restoreTurns = Effect.suspend(() => { + if (turnsRestored) return Effect.void; + turnsRestored = true; + return Effect.forEach( + [...activeTurns], + (threadId) => + command( + { type: "turn", threadId, active: true }, + () => socketWrite === send && activeTurns.has(threadId), + ), + { discard: true }, + ); + }); + const decoder = new TextDecoder(); + yield* reader.pull.pipe( + Effect.flatMap((frames) => + Effect.forEach( + frames, + (frame) => + decodeEventLine(typeof frame === "string" ? frame : decoder.decode(frame)).pipe( + Effect.flatMap((event) => + Effect.andThen( + socketWrite === send ? restoreTurns : Effect.void, + Effect.suspend(() => (socketWrite === send ? receive(event) : Effect.void)), + ), + ), + Effect.ignore, + ), + { discard: true }, + ), + ), + Effect.forever, + Effect.ignore, + Effect.ensuring( + Effect.suspend(() => (socketWrite === send ? retireConnection : Effect.void)), + ), + ); + }, + Effect.scoped, + (effect) => Effect.ignore(effect), ); + if (overFds) { + yield* readDescriptor(inputFd, receive).pipe( + Effect.ensuring( + Effect.sync(() => { + shownTabs.clear(); + loadFailures.clear(); + }), + ), + Effect.forkScoped, + ); + } const endpoint = Effect.fn("DesktopBrowserChannel.endpoint")(function* (key: DesktopTabKey) { const id = keyOf(key); const secret = NodeCrypto.randomBytes(24).toString("base64url"); @@ -183,11 +273,13 @@ const make = Effect.gen(function* () { inbound.delete(id); return yield* Effect.die("The desktop tab detached before the server connected."); } + const connection = socketWrite; yield* Effect.addFinalizer(() => Effect.gen(function* () { if (inbound.get(id) === queue) inbound.delete(id); yield* Queue.shutdown(queue); - yield* command({ type: "release", ...key }); + // A desktop that redialed already dropped this relay; a release now would drop its new one. + yield* command({ type: "release", ...key }, () => socketWrite === connection); }), ); // The relay serves one Playwright connection; a second would see the first's sessions. @@ -279,12 +371,64 @@ const make = Effect.gen(function* () { loadFailure: (key) => loadFailures.get(keyOf(key)), endpoint, turn: Effect.fn("DesktopBrowserChannel.turn")(function (threadId, active) { + if (active) activeTurns.add(threadId); + else activeTurns.delete(threadId); return command({ type: "turn", threadId, active }); }), pointer: Effect.fn("DesktopBrowserChannel.pointer")(function (key, pointer) { return command({ type: "pointer", ...key, ...pointer }); }), + serve, }); }); +// Use cancellable socket reads; filesystem pipe reads keep Node alive after scope shutdown. +const readDescriptor = Effect.fn("DesktopBrowserChannel.readDescriptor")(function* ( + fd: number, + receive: (event: DesktopBrowserEventType) => Effect.Effect, +) { + const readable = yield* Effect.acquireRelease( + Effect.sync(() => new NodeNet.Socket({ fd, readable: true, writable: false })), + (stream) => Effect.sync(() => stream.destroy()), + ); + yield* NodeStream.fromReadable({ + evaluate: () => readable, + closeOnDone: true, + onError: (cause) => (cause instanceof Error ? cause : new Error(String(cause))), + }).pipe( + Stream.pipeThroughChannel(Ndjson.decode({ ignoreEmptyLines: true })), + Stream.mapEffect((value) => decodeEvent(value).pipe(Effect.option)), + Stream.runForEach(Option.match({ onNone: () => Effect.void, onSome: receive })), + Effect.catchCause((cause) => Effect.logWarning("desktop browser channel stopped", { cause })), + ); +}); + export const layer = Layer.effect(DesktopBrowserChannel, make); + +/** Only the desktop that started this server holds the secret its token derives from. */ +export const routeLayer = Layer.unwrap( + Effect.gen(function* () { + const config = yield* ServerConfig.ServerConfig; + const channel = yield* DesktopBrowserChannel; + const mac = yield* Mac.Mac; + const secret = config.desktopBootstrapSecret; + return HttpRouter.add( + "GET", + DESKTOP_BROWSER_CHANNEL_PATH, + Effect.gen(function* () { + const request = yield* HttpServerRequest.HttpServerRequest; + const token = /^Bearer (.+)$/.exec(request.headers.authorization ?? "")?.[1]; + const allowed = + config.desktopBrowserSocket === true && + secret !== undefined && + token !== undefined && + (yield* isValidDesktopBootstrapToken(secret, token, yield* Clock.currentTimeMillis).pipe( + Effect.provideService(Mac.Mac, mac), + )); + if (!allowed) return HttpServerResponse.empty({ status: 401 }); + yield* channel.serve(yield* request.upgrade); + return HttpServerResponse.empty(); + }), + ); + }), +); diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index f345bf6f150e6..ad6184f7c5dbe 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -535,6 +535,7 @@ const dependencies = Layer.mergeAll( }), ), pointer: () => Effect.void, + serve: () => Effect.void, turn: (threadId, active) => Effect.sync(() => desktopTurns.emit("turn", threadId, active)), }), PreviewAgentActivity.layer, diff --git a/apps/server/src/preview/ServerBrowserContexts.ts b/apps/server/src/preview/ServerBrowserContexts.ts index 670ca75eea195..37669997a7bfc 100644 --- a/apps/server/src/preview/ServerBrowserContexts.ts +++ b/apps/server/src/preview/ServerBrowserContexts.ts @@ -302,7 +302,7 @@ export class ServerBrowserContexts { async connectDesktopPage(endpoint: string) { const { chromium } = loadPlaywright(); - // Only a same-machine desktop backend gets the browser channel, so uploads can pass file paths. + // Uploads pass file paths: the desktop shares this filesystem, or translates them (WSL). const browser = await chromium.connectOverCDP(endpoint, { isLocal: true, timeout: 15_000 }); const page = browser.contexts().flatMap((context) => context.pages())[0]; if (!page) { diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 29e3143cbc638..a1b9d96313876 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -854,6 +854,7 @@ const layerMakeRoutes = Layer.mergeAll( GuestHttp.layer, DeviceHubProxy.layer, ServerBrowserStream.routeLayer, + DesktopBrowserChannel.routeLayer.pipe(Layer.provide(DesktopBrowserChannel.layer)), ServerHttp.layerStaticAndDevRoute, Ws.layer, speechStreamRouteLayer, diff --git a/packages/contracts/src/desktopBootstrap.ts b/packages/contracts/src/desktopBootstrap.ts index 5148aa2c1c82e..b1c7a0b5270ef 100644 --- a/packages/contracts/src/desktopBootstrap.ts +++ b/packages/contracts/src/desktopBootstrap.ts @@ -30,6 +30,8 @@ export const DesktopBackendBootstrap = Schema.Struct({ desktopAppUpdate: Schema.optionalKey(Schema.Boolean), desktopBrowserFd: Schema.optionalKey(PositiveInt), desktopBrowserControlFd: Schema.optionalKey(PositiveInt), + // wsl.exe drops the browser fds, so that desktop dials DESKTOP_BROWSER_CHANNEL_PATH instead. + desktopBrowserSocket: Schema.optionalKey(Schema.Boolean), resourceMonitorPath: Schema.optionalKey(TrimmedNonEmptyString), }); diff --git a/packages/contracts/src/desktopBrowser.ts b/packages/contracts/src/desktopBrowser.ts index 1fa572ca662ae..b17c8531c1ea2 100644 --- a/packages/contracts/src/desktopBrowser.ts +++ b/packages/contracts/src/desktopBrowser.ts @@ -2,6 +2,12 @@ import * as Schema from "effect/Schema"; import { TrimmedNonEmptyString } from "./baseSchemas.ts"; +/** Where a desktop whose backend launcher drops the browser fds dials the channel instead. */ +export const DESKTOP_BROWSER_CHANNEL_PATH = "/api/desktop-browser-channel"; + +/** Sent by the server over the socket while it is idle, so a desktop can tell a dead one from a quiet one. */ +export const DESKTOP_BROWSER_CHANNEL_PING = "ping"; + const TabKey = { threadId: TrimmedNonEmptyString, tabId: TrimmedNonEmptyString,