From 6d4a4d3d475d3576e5b83fba12e4e4b304882e94 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 23:33:01 +0000 Subject: [PATCH 1/4] fix(mobile): taking control of an agent's browser tab no longer undoes itself Hiding the Take control row grows the viewer, which reconnects the stream to raise the frame cap. The server releases control with the old socket and never hands an agent-owned tab back, so the viewer now re-takes control on the new socket when it held control before. Fixes pingdotgg/t3code#40610 Co-Authored-By: Claude Opus 5.5 --- .../src/features/browser/preview-stream.browser.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/mobile/src/features/browser/preview-stream.browser.ts b/apps/mobile/src/features/browser/preview-stream.browser.ts index 748cecd982f93..ad16a1376144e 100644 --- a/apps/mobile/src/features/browser/preview-stream.browser.ts +++ b/apps/mobile/src/features/browser/preview-stream.browser.ts @@ -156,6 +156,8 @@ export function start(configuration: PreviewStreamConfiguration) { }; const connect = () => { + // The server releases control with the old socket, and agent tabs never hand it back. + let retake = interactive && control?.controller === "you"; if (client) reportStatus("connecting"); client?.stop(); control = null; @@ -205,6 +207,14 @@ export function start(configuration: PreviewStreamConfiguration) { onControl: (nextControl) => { const previous = control; control = nextControl; + if (retake && nextControl.controller !== "another-viewer") { + retake = false; + if (nextControl.controller !== "you" && nextControl.canOperate) { + clearInput(); + next.send({ type: "takeControl" }); + return; + } + } post({ type: "control", ...nextControl }); if (nextControl.controller === "you") agentCursor.style.opacity = "0"; if (nextControl.controller !== "you") clearInput(); From 59747676ce93a96867cd4a5acfca2db41aff88fd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 01:05:16 +0000 Subject: [PATCH 2/4] fix(mobile): no 'Another viewer has control' flash while re-taking control The replacement stream socket can see this viewer's old socket still holding control before the server releases it. Keep that state from the UI until the generation changes, so taking control shows no flash. Co-Authored-By: Claude Opus 5.5 --- .../features/browser/preview-stream.browser.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/mobile/src/features/browser/preview-stream.browser.ts b/apps/mobile/src/features/browser/preview-stream.browser.ts index ad16a1376144e..79b394664fec1 100644 --- a/apps/mobile/src/features/browser/preview-stream.browser.ts +++ b/apps/mobile/src/features/browser/preview-stream.browser.ts @@ -158,6 +158,7 @@ export function start(configuration: PreviewStreamConfiguration) { const connect = () => { // The server releases control with the old socket, and agent tabs never hand it back. let retake = interactive && control?.controller === "you"; + let retakeGeneration: number | null = null; if (client) reportStatus("connecting"); client?.stop(); control = null; @@ -207,13 +208,21 @@ export function start(configuration: PreviewStreamConfiguration) { onControl: (nextControl) => { const previous = control; control = nextControl; - if (retake && nextControl.controller !== "another-viewer") { - retake = false; - if (nextControl.controller !== "you" && nextControl.canOperate) { + if (retake) { + if (nextControl.controller === "another-viewer") { + // Our old socket until its release bumps the generation; showing it would flash. + retakeGeneration ??= nextControl.generation; + if (nextControl.generation === retakeGeneration) { + clearInput(); + return; + } + } else if (nextControl.controller !== "you" && nextControl.canOperate) { + retake = false; clearInput(); next.send({ type: "takeControl" }); return; } + retake = false; } post({ type: "control", ...nextControl }); if (nextControl.controller === "you") agentCursor.style.opacity = "0"; From 8e5ab4f04929ccc83d25f94c330ebb56a98493e4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 01:21:37 +0000 Subject: [PATCH 3/4] fix(mobile): show another viewer's control while re-taking it Only hide an another-viewer state at the generation this viewer held before reconnecting; a later generation means a real other viewer took control. Co-Authored-By: Claude Opus 5.5 --- .../browser/preview-stream.browser.ts | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/mobile/src/features/browser/preview-stream.browser.ts b/apps/mobile/src/features/browser/preview-stream.browser.ts index 79b394664fec1..7ecdb3b009781 100644 --- a/apps/mobile/src/features/browser/preview-stream.browser.ts +++ b/apps/mobile/src/features/browser/preview-stream.browser.ts @@ -157,8 +157,8 @@ export function start(configuration: PreviewStreamConfiguration) { const connect = () => { // The server releases control with the old socket, and agent tabs never hand it back. - let retake = interactive && control?.controller === "you"; - let retakeGeneration: number | null = null; + const heldGeneration = interactive && control?.controller === "you" ? control.generation : null; + let retake = heldGeneration !== null; if (client) reportStatus("connecting"); client?.stop(); control = null; @@ -209,14 +209,18 @@ export function start(configuration: PreviewStreamConfiguration) { const previous = control; control = nextControl; if (retake) { - if (nextControl.controller === "another-viewer") { - // Our old socket until its release bumps the generation; showing it would flash. - retakeGeneration ??= nextControl.generation; - if (nextControl.generation === retakeGeneration) { - clearInput(); - return; - } - } else if (nextControl.controller !== "you" && nextControl.canOperate) { + // At our held generation, the other viewer is our old socket; showing it would flash. + if ( + nextControl.controller === "another-viewer" && + nextControl.generation === heldGeneration + ) { + clearInput(); + return; + } + if ( + (nextControl.controller === "agent" || nextControl.controller === "unclaimed") && + nextControl.canOperate + ) { retake = false; clearInput(); next.send({ type: "takeControl" }); From ac934afca0c2ed12c3f2929ca5642060a5c86bec Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 01:37:40 +0000 Subject: [PATCH 4/4] fix(mobile): raise the frame cap in place instead of reconnecting while in control Replacing the stream socket to raise the frame cap released the viewer's control, and the previous re-take left a window in which the agent could act. The stream now accepts a maxSize message that restarts the viewer's screencast at the new cap on the same socket, and the mobile viewer uses it while it holds control. The re-take and its state suppression are removed. Fixes pingdotgg/t3code#40610 Co-Authored-By: Claude Opus 5.5 --- .../browser/preview-stream.browser.ts | 29 ++++--------------- apps/server/src/preview/ServerBrowser.test.ts | 24 +++++++++++++++ apps/server/src/preview/ServerBrowser.ts | 21 ++++++++++++-- .../server/src/preview/ServerBrowserStream.ts | 4 +-- .../src/preview/serverBrowserStream.ts | 14 +++++++-- 5 files changed, 62 insertions(+), 30 deletions(-) diff --git a/apps/mobile/src/features/browser/preview-stream.browser.ts b/apps/mobile/src/features/browser/preview-stream.browser.ts index 7ecdb3b009781..e0c88b26e8a8b 100644 --- a/apps/mobile/src/features/browser/preview-stream.browser.ts +++ b/apps/mobile/src/features/browser/preview-stream.browser.ts @@ -156,9 +156,6 @@ export function start(configuration: PreviewStreamConfiguration) { }; const connect = () => { - // The server releases control with the old socket, and agent tabs never hand it back. - const heldGeneration = interactive && control?.controller === "you" ? control.generation : null; - let retake = heldGeneration !== null; if (client) reportStatus("connecting"); client?.stop(); control = null; @@ -208,26 +205,6 @@ export function start(configuration: PreviewStreamConfiguration) { onControl: (nextControl) => { const previous = control; control = nextControl; - if (retake) { - // At our held generation, the other viewer is our old socket; showing it would flash. - if ( - nextControl.controller === "another-viewer" && - nextControl.generation === heldGeneration - ) { - clearInput(); - return; - } - if ( - (nextControl.controller === "agent" || nextControl.controller === "unclaimed") && - nextControl.canOperate - ) { - retake = false; - clearInput(); - next.send({ type: "takeControl" }); - return; - } - retake = false; - } post({ type: "control", ...nextControl }); if (nextControl.controller === "you") agentCursor.style.opacity = "0"; if (nextControl.controller !== "you") clearInput(); @@ -267,6 +244,12 @@ export function start(configuration: PreviewStreamConfiguration) { const height = Math.round((interactive ? next.height : side) * ratio); if (cap && cap.width >= width && cap.height >= height) return; cap = { width: Math.max(width, cap?.width ?? 0), height: Math.max(height, cap?.height ?? 0) }; + // A new socket would release control, and agent-owned tabs do not hand it back. + if ( + control?.controller === "you" && + send({ type: "maxSize", maxWidth: cap.width, maxHeight: cap.height }) + ) + return; connect(); }; const observer = new ResizeObserver(() => { diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index a15378ec3039c..a90b4335857d7 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -1596,6 +1596,30 @@ it.effect("keeps a tab's zoom when its viewer resizes", () => ).pipe(Effect.provide(layer)), ); +it.effect("raises a viewer's frame cap on its open stream without releasing control", () => + Effect.scoped( + Effect.gen(function* () { + const { browser, broker, tabId } = yield* ready; + const viewer = yield* browser.attachViewer(viewerInput(tabId, true)); + yield* viewer.input({ type: "takeControl" }); + const session = contexts[0]!.sessions.at(-1)!; + session.send.mockClear(); + yield* viewer.input({ type: "maxSize", maxWidth: 2064, maxHeight: 9000 }); + expect(session.send).toHaveBeenCalledWith( + "Page.startScreencast", + expect.objectContaining({ maxWidth: 2064, maxHeight: 4320 }), + ); + const status = yield* broker.invoke({ + scope, + tabId, + operation: "status", + input: {}, + }); + expect(status.control).toMatchObject({ owner: "human" }); + }), + ).pipe(Effect.provide(layer)), +); + it.effect( "streams to a read-only viewer without allowing takeover, input, or viewport changes", () => diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 26203fc1a63c8..d1f315d0e263f 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -115,6 +115,8 @@ const HOST_RECONNECT_DELAY = "1 second"; const DESKTOP_ATTACH_TIMEOUT = "10 seconds"; // A viewer still is best-effort; a capture Chromium never answers must not hold the capture lock. const VIEWER_STILL_TIMEOUT_MS = 2_000; +export const MAX_STREAM_WIDTH = 7680; +export const MAX_STREAM_HEIGHT = 4320; const VIEWER_OUTPUT_LIMIT = 64; const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const; const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024; @@ -3245,6 +3247,7 @@ const make = Effect.gen(function* () { const recentFrames: Array = []; let screencastParams = Promise.resolve(); let screencastScale = 1; + let maxSize = { width: input.maxWidth, height: input.maxHeight }; const startScreencast = (scale: number) => { screencastScale = scale; screencastParams = screencastParams.then(async () => { @@ -3254,8 +3257,8 @@ const make = Effect.gen(function* () { .send("Page.startScreencast", { format: "jpeg", quality: screencastScale < 1 ? Math.min(quality, SCREENCAST_MOTION_QUALITY) : quality, - maxWidth: Math.max(1, Math.round(input.maxWidth * screencastScale)), - maxHeight: Math.max(1, Math.round(input.maxHeight * screencastScale)), + maxWidth: Math.max(1, Math.round(maxSize.width * screencastScale)), + maxHeight: Math.max(1, Math.round(maxSize.height * screencastScale)), }) .catch(constVoid); }); @@ -3395,9 +3398,21 @@ const make = Effect.gen(function* () { output, input: (raw: unknown) => Effect.promise(async () => { - if (!viewer.canOperate) return; const message = asRecord(raw); if (!message) return; + // Raising the cap in place keeps this viewer's control, which a new socket would lose. + if (message.type === "maxSize") { + const width = num(message.maxWidth); + const height = num(message.maxHeight); + if (!(width > 0 && height > 0)) return; + maxSize = { + width: Math.min(Math.round(width), MAX_STREAM_WIDTH), + height: Math.min(Math.round(height), MAX_STREAM_HEIGHT), + }; + await startScreencast(screencastScale); + return; + } + if (!viewer.canOperate) return; try { if (message.type === "takeControl") { const taking = tab.control.take(viewer.id); diff --git a/apps/server/src/preview/ServerBrowserStream.ts b/apps/server/src/preview/ServerBrowserStream.ts index 649af3b31d0db..242338de9651f 100644 --- a/apps/server/src/preview/ServerBrowserStream.ts +++ b/apps/server/src/preview/ServerBrowserStream.ts @@ -88,8 +88,8 @@ const makeHandler = Effect.fnUntraced(function* (browser: ServerBrowser.ServerBr threadId, tabId, canOperate, - maxWidth: intParam(params, "maxWidth", 1280, 7680), - maxHeight: intParam(params, "maxHeight", 800, 4320), + maxWidth: intParam(params, "maxWidth", 1280, ServerBrowser.MAX_STREAM_WIDTH), + maxHeight: intParam(params, "maxHeight", 800, ServerBrowser.MAX_STREAM_HEIGHT), quality: intParam(params, "quality", DEFAULT_QUALITY, 100), }) .pipe( diff --git a/packages/client-runtime/src/preview/serverBrowserStream.ts b/packages/client-runtime/src/preview/serverBrowserStream.ts index 24d1cddd62fc8..7ce7801f52f54 100644 --- a/packages/client-runtime/src/preview/serverBrowserStream.ts +++ b/packages/client-runtime/src/preview/serverBrowserStream.ts @@ -94,6 +94,7 @@ export type PreviewStreamInput = } | { readonly type: "text"; readonly text: string } | { readonly type: "resize"; readonly width: number; readonly height: number } + | { readonly type: "maxSize"; readonly maxWidth: number; readonly maxHeight: number } | { readonly type: "navigate"; readonly url: string } | { readonly type: "history"; readonly delta: -1 | 1 } | { readonly type: "reload"; readonly ignoreCache?: boolean } @@ -224,7 +225,8 @@ export function createPreviewStreamClient( maxHeight: String(Math.max(1, Math.round(target.maxHeight))), }); if (target.interactive === false) query.set("interactive", "false"); - const url = withDeviceHubQuery(`${target.access.wsBase}/ws?${query.toString()}`, target.access); + const url = () => + withDeviceHubQuery(`${target.access.wsBase}/ws?${query.toString()}`, target.access); let stopped = false; let socket: WebSocket | null = null; let retryTimer: ReturnType | null = null; @@ -241,7 +243,7 @@ export function createPreviewStreamClient( const connect = () => { if (stopped) return; - const ws = new WebSocket(url); + const ws = new WebSocket(url()); ws.binaryType = "arraybuffer"; socket = ws; let opened = false; @@ -389,7 +391,15 @@ export function createPreviewStreamClient( return { send: (input) => { + if (input.type === "maxSize") { + query.set("maxWidth", String(Math.max(1, Math.round(input.maxWidth)))); + query.set("maxHeight", String(Math.max(1, Math.round(input.maxHeight)))); + } if (socket?.readyState !== WebSocket.OPEN) return false; + if (input.type === "maxSize") { + socket.send(JSON.stringify(input)); + return true; + } if (!control?.canOperate) return false; if (input.type !== "takeControl" && control.controller !== "you") return false; socket.send(JSON.stringify(input));