Repository navigation
fix(desktop): WSL-only browser tabs keep Annotate and Screenshot #38539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a1b7da4
f4a916f
d5e3f9e
722b1b2
cdcd669
d841b26
d77fc8b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string, unknown> }; | ||
| 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<Uint8Array>; | ||
| readonly onCommand: (line: string) => Effect.Effect<void>; | ||
| /** 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)), | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the distro’s actual drive mounts. If the distro mounts
|
||
| if (drive) return `${drive[1]!.toUpperCase()}:\\${(drive[2] ?? "").replaceAll("/", "\\")}`; | ||
| return `\\\\wsl.localhost\\${distroName}${linuxPath.replaceAll("/", "\\")}`; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not translate a literal backslash into a directory separator. The Linux paths
|
||
| } | ||
|
|
||
| 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string, Cause.Done>(); | ||
| const commands = yield* Queue.unbounded<string>(); | ||
| 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"); | ||
|
Comment on lines
+158
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert that the idle heartbeat repeats. This assertion passes if the server sends one ping at ten seconds and never sends another. With that regression, an idle desktop redials 30 seconds after the first ping. Extend this case to advance through a second interval and consume a second ping before sending any browser commands. As per coding guidelines, “Extend before you add.” As per path instructions, “Test at the lowest layer that fails on
|
||
| 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), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore active turns after a socket redial.
Each redial subscribes
eventsagain.announceAllthen clearsagentTurnsinDesktopBrowserHost, even when the backend has not restarted.ServerBrowserforwards only turn transitions. It does not resend a turn that is already active. After the redial, the idle release atDesktopBrowserHost.ts:238no longer finds the turn. It can release focus between steps while the turn still runs.After each redial, the host must hold every thread whose turn is active on the server. Apply later transitions after that state is restored. Clear the set only when the backend restarts.