@devries-cameron20 / t3code-1 / commits / b23f94e4a1
Fork 0

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

fix(server): preview resize and status report the real size of desktop tabs (#32854)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Entrpi committed Nov 13, 2026 at 14:32 UTC b23f94e4a1684ba28b12357d6eeb756faefc5500
3 files changed +207 -11
apps/server/src/preview/ServerBrowser.test.ts
+129
@@ -14,6 +14,7 @@ import {
14 ProviderInstanceId,
15 ThreadId,
16 type PreviewAutomationLoadError,
17 + type PreviewAutomationResizeResult,
18 type PreviewAutomationSnapshot,
19 type PreviewAutomationStatus,
20 } from "@t3tools/contracts";
@@ -2405,6 +2406,134 @@ it.effect(
2406 ).pipe(Effect.provide(layer)),
2407 );
2408
2409 +it.effect("a desktop tab reports the size the desktop draws after a resize", () =>
2410 + Effect.scoped(
2411 + Effect.gen(function* () {
2412 + const { broker } = yield* ready;
2413 + const manager = yield* Manager.PreviewManager;
2414 + const failReads = () => Promise.reject(new Error("Cannot find context with specified id"));
2415 + desktopSizeAnswer = failReads;
2416 + desktopRendersNext = true;
2417 + const opened = yield* broker.invoke<PreviewAutomationStatus>({
2418 + scope,
2419 + operation: "open",
2420 + input: { reuseExistingTab: false, show: false },
2421 + });
2422 + const tabId = PreviewTabId.make(opened.tabId!);
2423 + const { context } = desktopConnections.at(-1)!;
2424 + const cdp = context.sessions[0]!;
2425 + const events = yield* manager.subscribeEvents;
2426 + const status = broker
2427 + .invoke<PreviewAutomationStatus>({ scope, tabId, operation: "status", input: {} })
2428 + .pipe(Effect.map((result) => result.viewport));
2429 + // The desktop draws the new size once the session update reaches it.
2430 + const resize = (input: Record<string, unknown>, width: number, height: number) =>
2431 + Effect.gen(function* () {
2432 + const result = yield* broker
2433 + .invoke<PreviewAutomationResizeResult>({ scope, tabId, operation: "resize", input })
2434 + .pipe(Effect.forkScoped);
2435 + let event = yield* PubSub.take(events);
2436 + while (event.type !== "resized" || event.tabId !== tabId) {
2437 + event = yield* PubSub.take(events);
2438 + }
2439 + Object.assign(context.cssViewport, { width, height });
2440 + yield* Effect.promise(() => cdp.emitAsync("Page.frameResized"));
2441 + return (yield* Fiber.join(result)).viewport;
2442 + });
2443 +
2444 + // A size no read measured after the resize is unknown, never a default or the last one.
2445 + expect(
2446 + yield* resize({ mode: "freeform", width: 900, height: 600 }, 900, 600),
2447 + ).toBeUndefined();
2448 + desktopSizeAnswer = null;
2449 + expect(yield* resize({ mode: "freeform", width: 1280, height: 760 }, 1280, 760)).toEqual({
2450 + width: 1280,
2451 + height: 760,
2452 + });
2453 + desktopSizeAnswer = failReads;
2454 + expect(yield* status).toEqual({ width: 1280, height: 760 });
2455 + expect(
2456 + yield* resize({ mode: "freeform", width: 1000, height: 700 }, 1000, 700),
2457 + ).toBeUndefined();
2458 + desktopSizeAnswer = null;
2459 + // A page can keep another size than the fixed one asked for; both report the page's.
2460 + expect(yield* resize({ mode: "freeform", width: 844, height: 390 }, 560, 760)).toEqual({
2461 + width: 560,
2462 + height: 760,
2463 + });
2464 + expect(yield* status).toEqual({ width: 560, height: 760 });
2465 + expect(yield* resize({ mode: "fill" }, 1234, 700)).toEqual({ width: 1234, height: 700 });
2466 + // No page resize or load since that read, so status answers without reading again.
2467 + let reads = 0;
2468 + desktopSizeAnswer = async () => {
2469 + reads += 1;
2470 + return { result: { value: { width: 1, height: 1 } } };
2471 + };
2472 + expect(yield* status).toEqual({ width: 1234, height: 700 });
2473 + expect(reads).toBe(0);
2474 + // A status during a read waits for it rather than answer the size before it.
2475 + const pending = yield* Queue.unbounded<PromiseWithResolvers<Record<string, unknown>>>();
2476 + const heldAnswer = () => {
2477 + const read = Promise.withResolvers<Record<string, unknown>>();
2478 + Queue.offerUnsafe(pending, read);
2479 + return read.promise;
2480 + };
2481 + desktopSizeAnswer = heldAnswer;
2482 + const answer = (width: number, height: number) => ({ result: { value: { width, height } } });
2483 + const refill = broker
2484 + .invoke<PreviewAutomationResizeResult>({
2485 + scope,
2486 + tabId,
2487 + operation: "resize",
2488 + input: { mode: "fill" },
2489 + })
2490 + .pipe(
2491 + Effect.map((result) => result.viewport),
2492 + Effect.forkScoped,
2493 + );
2494 + // Status reads the URL just before it picks the size.
2495 + const url = context.page.url();
2496 + using urlRead = vi.spyOn(context.page, "url");
2497 + const startStatus = Effect.gen(function* () {
2498 + const started = Promise.withResolvers<void>();
2499 + urlRead.mockImplementationOnce(() => {
2500 + started.resolve();
2501 + return url;
2502 + });
2503 + const fiber = yield* Effect.forkScoped(status);
2504 + yield* Effect.promise(() => started.promise);
2505 + return fiber;
2506 + });
2507 + const filled = yield* refill;
2508 + const read = yield* Queue.take(pending);
2509 + const during = yield* startStatus;
2510 + read.resolve(answer(1100, 650));
2511 + expect(yield* Fiber.join(during)).toEqual({ width: 1100, height: 650 });
2512 + expect(yield* Fiber.join(filled)).toEqual({ width: 1100, height: 650 });
2513 + // A page resize during that read makes its answer old: status reads again, and the
2514 + // resize result has no size.
2515 + const outdated = yield* refill;
2516 + const oldRead = yield* Queue.take(pending);
2517 + const later = yield* startStatus;
2518 + yield* Effect.promise(() => cdp.emitAsync("Page.frameResized"));
2519 + desktopSizeAnswer = async () => answer(1000, 600);
2520 + oldRead.resolve(answer(1100, 650));
2521 + expect(yield* Fiber.join(later)).toEqual({ width: 1000, height: 600 });
2522 + expect(yield* Fiber.join(outdated)).toBeUndefined();
2523 + // So does a load during that read, which may have measured the page before it.
2524 + desktopSizeAnswer = heldAnswer;
2525 + const reloaded = yield* refill;
2526 + const pageRead = yield* Queue.take(pending);
2527 + const navigated = yield* startStatus;
2528 + yield* Effect.promise(() => cdp.emitAsync("Page.loadEventFired"));
2529 + desktopSizeAnswer = async () => answer(900, 560);
2530 + pageRead.resolve(answer(1000, 600));
2531 + expect(yield* Fiber.join(navigated)).toEqual({ width: 900, height: 560 });
2532 + expect(yield* Fiber.join(reloaded)).toBeUndefined();
2533 + }),
2534 + ).pipe(Effect.provide(layer)),
2535 +);
2536 +
2537 it.effect("a stalled desktop viewer still gets its size, control and reconnect", () =>
2538 Effect.scoped(
2539 Effect.gen(function* () {
apps/server/src/preview/ServerBrowser.ts
+75 -10
@@ -120,6 +120,8 @@ const AGENT_CURSOR_MOVE_MS = 160;
120 const AGENT_CURSOR_CLICK_LEAD_MS = 40;
121 /** A dialog blocks page script, so a desktop size read gives up after this. */
122 const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second";
123 +/** A desktop page that keeps its size draws nothing on a resize, so the wait gives up. */
124 +const DESKTOP_RESIZE_WAIT_MS = 1000;
125 // A revealed tab's client opens the panel and attaches its stream a few round trips later, remote ones included.
126 const REVEAL_VIEWER_WAIT_MS = 1000;
127
@@ -363,6 +365,12 @@ interface ServerTab {
365 desktopViewportRead: Promise<ServerTab["desktopViewport"]>;
366 /** A read queued behind `desktopViewportRead`, shared by every caller that waits for it. */
367 desktopViewportNext: Promise<ServerTab["desktopViewport"]> | null;
368 + /** A page resize or load since the last read started; status measures again. */
369 + desktopViewportStale: boolean;
370 + /** Counts page resizes, loads and dialog closes, so a caller can tell one landed during a read. */
371 + desktopViewportChanges: number;
372 + /** Settles at the desktop page's next main-frame resize. */
373 + desktopResize: PromiseWithResolvers<void>;
374 loading: boolean;
375 loadError: PreviewAutomationLoadError | undefined;
376 closing: boolean;
@@ -679,7 +687,7 @@ const make = Effect.gen(function* () {
687 };
688
689 // Desktop pages have no Playwright viewport; an isolated world keeps page script
682 - // from overriding the CSS size, and failed reads preserve the last usable size.
690 + // from overriding the CSS size. A failed read answers null and keeps the last usable size.
691 const readDesktopViewport = async (tab: ServerTab) => {
692 const readSize = async () => {
693 const { frameTree } = await tab.cdp.send("Page.getFrameTree");
@@ -704,20 +712,26 @@ const make = Effect.gen(function* () {
712 const width = size?.width;
713 const height = size?.height;
714 if (typeof width === "number" && width > 0 && typeof height === "number" && height > 0)
707 - tab.desktopViewport = { width, height };
708 - return tab.desktopViewport;
715 + return (tab.desktopViewport = { width, height });
716 + tab.desktopViewportStale = true;
717 + return null;
718 };
719
720 // Reads run one at a time, and callers that arrive during a read share the next one.
721 const measureDesktopViewport = (tab: ServerTab) =>
722 (tab.desktopViewportNext ??= tab.desktopViewportRead.then(() => {
723 tab.desktopViewportNext = null;
724 + tab.desktopViewportStale = false;
725 return (tab.desktopViewportRead = readDesktopViewport(tab));
726 }));
727
728 const layoutSize = async (tab: ServerTab) => {
729 // A dialog blocks page script; its close measures again.
720 - if (tab.desktop) return tab.dialog ? tab.desktopViewport : measureDesktopViewport(tab);
730 + if (tab.desktop) {
731 + return tab.dialog
732 + ? tab.desktopViewport
733 + : ((await measureDesktopViewport(tab)) ?? tab.desktopViewport);
734 + }
735 const size = tab.page.viewportSize();
736 if (!size) return size;
737 return {
@@ -726,6 +740,15 @@ const make = Effect.gen(function* () {
740 };
741 };
742
743 + // Page resizes and loads mark the size stale; only then does status read the page. One
744 + // during the read it waits for can leave that answer old, so it reads once more.
745 + const desktopStatusViewport = async (tab: ServerTab) => {
746 + const changes = tab.desktopViewportChanges;
747 + await (tab.desktopViewportStale || tab.dialog ? layoutSize(tab) : tab.desktopViewportRead);
748 + if (tab.desktopViewportChanges !== changes) await layoutSize(tab);
749 + return tab.desktopViewport;
750 + };
751 +
752 const applyZoom = async (tab: ServerTab) => {
753 if (tab.desktop) return;
754 const size = tab.page.viewportSize();
@@ -975,6 +998,9 @@ const make = Effect.gen(function* () {
998 desktopViewport: null,
999 desktopViewportRead: Promise.resolve(null),
1000 desktopViewportNext: null,
1001 + desktopViewportStale: true,
1002 + desktopViewportChanges: 0,
1003 + desktopResize: Promise.withResolvers<void>(),
1004 loading: false,
1005 loadError: undefined,
1006 closing: false,
@@ -993,9 +1019,16 @@ const make = Effect.gen(function* () {
1019 if (desktop) {
1020 // The desktop resizes and zooms its webview itself; viewers map input with the new size.
1021 const remeasure = () => {
1022 + tab.desktopViewportStale = true;
1023 + tab.desktopViewportChanges += 1;
1024 if (tab.viewers.size > 0) return broadcastViewport(tab).catch(constVoid);
1025 };
998 - cdp.on("Page.frameResized", remeasure);
1026 + cdp.on("Page.frameResized", () => {
1027 + const drawn = tab.desktopResize;
1028 + tab.desktopResize = Promise.withResolvers();
1029 + drawn.resolve();
1030 + return remeasure();
1031 + });
1032 // A read a navigation broke has a fresh page to measure.
1033 cdp.on("Page.loadEventFired", remeasure);
1034 // Closing a dialog resizes nothing, and the host can close it without us.
@@ -1804,7 +1837,7 @@ const make = Effect.gen(function* () {
1837 };
1838 }
1839 const url = ServerBrowserPage.withoutDataPayload(tab.page.url());
1807 - const viewport = tab.desktop ? await layoutSize(tab) : tab.page.viewportSize();
1840 + const viewport = tab.desktop ? await desktopStatusViewport(tab) : tab.page.viewportSize();
1841 const loadError = loadErrorOf(tab);
1842 const status = {
1843 available: true,
@@ -2420,7 +2453,7 @@ const make = Effect.gen(function* () {
2453 message: "A browser dialog is pending. Read preview_status.",
2454 reason: "dialogPending",
2455 });
2423 - return executeTabOperation(tab, request);
2456 + return executeTabOperation(tab, request, deadlineMs);
2457 });
2458 }
2459 return tab.control.agent(
@@ -2436,7 +2469,7 @@ const make = Effect.gen(function* () {
2469 tab.agentOperating = !READ_OPERATIONS.has(request.operation);
2470 updateFileChooserInterception(tab);
2471 try {
2439 - return await executeTabOperation(tab, request);
2472 + return await executeTabOperation(tab, request, deadlineMs);
2473 } finally {
2474 tab.agentOperating = false;
2475 updateFileChooserInterception(tab);
@@ -2447,7 +2480,11 @@ const make = Effect.gen(function* () {
2480 );
2481 };
2482
2450 - const executeTabOperation = async (tab: ServerTab, request: PreviewAutomationRequest) => {
2483 + const executeTabOperation = async (
2484 + tab: ServerTab,
2485 + request: PreviewAutomationRequest,
2486 + deadlineMs: number,
2487 + ) => {
2488 const input = request.input;
2489 switch (request.operation) {
2490 case "navigate": {
@@ -2464,14 +2501,40 @@ const make = Effect.gen(function* () {
2501 }
2502 case "resize": {
2503 const setting = resolvePreviewViewport(input as PreviewAutomationResizeInput);
2504 + const from = fixedViewportSize(tab.setting);
2505 + const to = fixedViewportSize(setting);
2506 + const drawn = tab.desktopResize.promise;
2507 await runPromise(
2508 manager.resize({ threadId: tab.threadId, tabId: tab.tabId, viewport: setting }),
2509 );
2510 await applySetting(tab, setting);
2511 + if (!tab.desktop) {
2512 + return {
2513 + tabId: tab.tabId,
2514 + setting,
2515 + viewport: tab.page.viewportSize() ?? UNATTACHED_FILL_VIEWPORT,
2516 + };
2517 + }
2518 + const changed = from?.width !== to?.width || from?.height !== to?.height;
2519 + const remainingMs = () => Math.max(0, deadlineMs - performance.now());
2520 + const within = <A>(promise: Promise<A>, ms: number) =>
2521 + runPromise(
2522 + Effect.promise(() => promise).pipe(
2523 + Effect.timeoutOption(ms),
2524 + Effect.map(Option.getOrNull),
2525 + ),
2526 + );
2527 + // The desktop draws the new size after the session update; its page resize says it has.
2528 + // Half the time left keeps room for the read, which runs even if nothing was drawn,
2529 + // because a page can keep another size than the one asked for.
2530 + if (changed) await within(drawn, Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2));
2531 + const changes = tab.desktopViewportChanges;
2532 + const measured = await within(measureDesktopViewport(tab), remainingMs());
2533 + // Only a read after the draw, with no page resize or load during it, measures this resize.
2534 return {
2535 tabId: tab.tabId,
2536 setting,
2474 - viewport: tab.page.viewportSize() ?? UNATTACHED_FILL_VIEWPORT,
2537 + ...(measured && tab.desktopViewportChanges === changes ? { viewport: measured } : {}),
2538 };
2539 }
2540 case "setColorScheme": {
@@ -2592,6 +2655,8 @@ const make = Effect.gen(function* () {
2655 return;
2656 }
2657 if (event.type === "resized") {
2658 + // Desktop tabs only record the setting, so status need not wait behind an agent action.
2659 + if (tab.desktop) tab.setting = event.snapshot.viewport ?? FILL_PREVIEW_VIEWPORT;
2660 await tab.control
2661 .system(async () => {
2662 if (
packages/contracts/src/previewAutomation.ts
+3 -1
@@ -262,7 +262,9 @@ export type PreviewAutomationResizeInput = typeof PreviewAutomationResizeInput.T
262 export const PreviewAutomationResizeResult = Schema.Struct({
263 tabId: PreviewTabId,
264 setting: PreviewViewportSetting,
265 - viewport: PreviewRenderedViewportSize,
265 + viewport: Schema.optional(PreviewRenderedViewportSize).annotate({
266 + description: "Viewport size after the resize. Omitted when it could not be measured.",
267 + }),
268 });
269 export type PreviewAutomationResizeResult = typeof PreviewAutomationResizeResult.Type;
270