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

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

fix(server): preview_open reports a desktop tab it reveals as visible (#38066)

Julius Marminge committed Nov 17, 2026 at 04:10 UTC ecde5791c12f60437a85045001c65f182bee6edd
4 files changed +96 -9
apps/server/src/preview/DesktopBrowserChannel.test.ts
+3 -2
@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
2 import * as NodeStream from "@effect/platform-node/NodeStream";
3 import { expect, it } from "@effect/vitest";
4 import * as Effect from "effect/Effect";
5 +import * as Fiber from "effect/Fiber";
6 import * as Layer from "effect/Layer";
7 import * as Queue from "effect/Queue";
8 import * as Stream from "effect/Stream";
@@ -38,9 +39,9 @@ it.effect("protects unknown desktop visibility until an explicit hidden report a
39 yield* channel.awaitAttached({ threadId: key.threadId, tabId: "sentinel-1" }, "5 seconds");
40 expect(channel.isShown(key)).toBe(false);
41
42 + const shown = yield* channel.awaitShown(key, "5 seconds").pipe(Effect.forkChild);
43 yield* send({ type: "shown", ...key, shown: true });
42 - yield* send({ type: "attached", threadId: key.threadId, tabId: "sentinel-2" });
43 - yield* channel.awaitAttached({ threadId: key.threadId, tabId: "sentinel-2" }, "5 seconds");
44 + expect(yield* Fiber.join(shown)).toBe(true);
45 expect(channel.isShown(key)).toBe(true);
46
47 yield* send({ type: "shown", ...key, shown: false });
apps/server/src/preview/DesktopBrowserChannel.ts
+25 -2
@@ -47,6 +47,8 @@ export class DesktopBrowserChannel extends Context.Service<
47 readonly isAttached: (key: DesktopTabKey) => Effect.Effect<boolean>;
48 /** Whether a desktop window draws the tab on screen, which no streamed viewer counts. */
49 readonly isShown: (key: DesktopTabKey) => boolean;
50 + /** Resolves true once the tab is shown, or false on timeout. */
51 + readonly awaitShown: (key: DesktopTabKey, timeout: Duration.Input) => Effect.Effect<boolean>;
52 /** A load failure the desktop saw, possibly before the server connected to the tab. */
53 readonly loadFailure: (key: DesktopTabKey) => PreviewAutomationLoadError | undefined;
54 readonly endpoint: (key: DesktopTabKey) => Effect.Effect<string, never, Scope.Scope>;
@@ -67,6 +69,7 @@ const make = Effect.gen(function* () {
69 const attachedTabs = new Set<string>();
70 // Missing reports protect desktop tabs until the renderer explicitly marks them hidden.
71 const shownTabs = new Map<string, boolean>();
72 + const shownReports = yield* PubSub.unbounded<string>();
73 const loadFailures = new Map<string, PreviewAutomationLoadError>();
74 const inbound = new Map<string, Queue.Queue<string>>();
75 const writeLock = yield* Semaphore.make(1);
@@ -92,6 +95,9 @@ const make = Effect.gen(function* () {
95 return Effect.void;
96 }),
97 isShown: () => false,
98 + awaitShown: Effect.fn("DesktopBrowserChannel.awaitShown")(function () {
99 + return Effect.succeed(false);
100 + }),
101 loadFailure: () => undefined,
102 });
103 }
@@ -149,7 +155,7 @@ const make = Effect.gen(function* () {
155 }
156 case "shown":
157 shownTabs.set(id, event.shown);
152 - return Effect.void;
158 + return event.shown ? PubSub.publish(shownReports, id) : Effect.void;
159 }
160 }),
161 Effect.catchCause((cause) => Effect.logWarning("desktop browser channel stopped", { cause })),
@@ -224,6 +230,8 @@ const make = Effect.gen(function* () {
230 return `ws://127.0.0.1:${address.port}/${secret}`;
231 });
232
233 + const isShown = (key: DesktopTabKey) => shownTabs.get(keyOf(key)) ?? true;
234 +
235 return DesktopBrowserChannel.of({
236 available: true,
237 awaitAttached: Effect.fn("DesktopBrowserChannel.awaitAttached")(function (key, timeout) {
@@ -252,7 +260,22 @@ const make = Effect.gen(function* () {
260 isAttached: Effect.fn("DesktopBrowserChannel.isAttached")(function (key) {
261 return Effect.sync(() => attachedTabs.has(keyOf(key)));
262 }),
255 - isShown: (key) => shownTabs.get(keyOf(key)) ?? true,
263 + isShown,
264 + awaitShown: Effect.fn("DesktopBrowserChannel.awaitShown")(function (key, timeout) {
265 + return Effect.scoped(
266 + Effect.gen(function* () {
267 + const subscription = yield* PubSub.subscribe(shownReports);
268 + if (isShown(key)) return true;
269 + return yield* Stream.fromSubscription(subscription).pipe(
270 + Stream.filter((id) => id === keyOf(key)),
271 + Stream.runHead,
272 + Effect.map(Option.isSome),
273 + Effect.timeoutOption(timeout),
274 + Effect.map((result) => Option.getOrElse(result, () => false)),
275 + );
276 + }),
277 + );
278 + }),
279 loadFailure: (key) => loadFailures.get(keyOf(key)),
280 endpoint,
281 turn: Effect.fn("DesktopBrowserChannel.turn")(function (threadId, active) {
apps/server/src/preview/ServerBrowser.test.ts
+56 -1
@@ -421,7 +421,13 @@ const desktopRenders = (tabId: string) => {
421 /** When set, a desktop that has not attached a tab waits for it the way the real channel does. */
422 let desktopAttachesLate = false;
423 let desktopAttachWaiting = Promise.withResolvers<void>();
424 -const shownDesktopTabs = new Set<string>();
424 +const shownDesktopTabs = new (class extends Set<string> {
425 + override add(tabId: string) {
426 + super.add(tabId);
427 + desktopDetaches.emit("shown", tabId);
428 + return this;
429 + }
430 +})();
431 const releasedDesktopTabs: Array<string> = [];
432 const desktopLoadFailures = new Map<string, PreviewAutomationLoadError>();
433 const desktopTurns = new NodeEvents.EventEmitter();
@@ -509,6 +515,16 @@ const dependencies = Layer.mergeAll(
515 ),
516 isAttached: (key) => Effect.sync(() => desktopRenders(key.tabId)),
517 isShown: (key) => shownDesktopTabs.has(key.tabId),
518 + awaitShown: (key, timeout) =>
519 + shownDesktopTabs.has(key.tabId)
520 + ? Effect.succeed(true)
521 + : Effect.callback<boolean>((resume) => {
522 + const onShown = (tabId: string) => {
523 + if (tabId === key.tabId) resume(Effect.succeed(true));
524 + };
525 + desktopDetaches.on("shown", onShown);
526 + return Effect.sync(() => desktopDetaches.off("shown", onShown));
527 + }).pipe(Effect.timeoutOrElse({ duration: timeout, orElse: () => Effect.succeed(false) })),
528 loadFailure: (key) => desktopLoadFailures.get(key.tabId),
529 endpoint: (key) =>
530 Effect.acquireRelease(Effect.succeed(`ws://desktop/${key.tabId}`), () =>
@@ -3303,6 +3319,45 @@ it.effect("preview_open reports a tab it shows as visible once the client attach
3319 ).pipe(Effect.provide(layer)),
3320 );
3321
3322 +it.effect("preview_open reports a hidden desktop tab it reveals as visible", () =>
3323 + Effect.scoped(
3324 + Effect.gen(function* () {
3325 + const broker = yield* Broker.PreviewAutomationBroker;
3326 + const manager = yield* Manager.PreviewManager;
3327 + yield* ServerBrowser.ServerBrowser;
3328 + const events = yield* manager.subscribeEvents;
3329 + yield* Effect.yieldNow;
3330 + desktopRendersNext = true;
3331 + const created = yield* broker.invoke<PreviewAutomationStatus>({
3332 + scope,
3333 + operation: "open",
3334 + input: { reuseExistingTab: false, open: false },
3335 + });
3336 + const tabId = PreviewTabId.make(created.tabId!);
3337 + expect(created.visible).toBe(false);
3338 + // Like the desktop renderer, the docked panel reports the tab shown after the reveal arrives.
3339 + yield* Effect.gen(function* () {
3340 + let event;
3341 + do event = yield* PubSub.take(events);
3342 + while (event.type !== "navigated" || !event.snapshot.revealRequest);
3343 + // The report crosses the client, the desktop main process and its pipe, so it lands a task later.
3344 + yield* Effect.yieldNow;
3345 + yield* Effect.sync(() => shownDesktopTabs.add(tabId));
3346 + }).pipe(Effect.forkScoped);
3347 +
3348 + const opened = yield* broker.invoke<PreviewAutomationStatus>({
3349 + scope,
3350 + tabId,
3351 + operation: "open",
3352 + input: { open: true },
3353 + });
3354 +
3355 + expect(opened.visible).toBe(true);
3356 + expect(opened.tabs).toEqual([expect.objectContaining({ tabId, visible: true })]);
3357 + }),
3358 + ).pipe(Effect.provide(layer)),
3359 +);
3360 +
3361 it.effect("a session's own tab stays its default while other sessions' tabs are listed", () =>
3362 Effect.scoped(
3363 Effect.gen(function* () {
apps/server/src/preview/ServerBrowser.ts
+12 -4
@@ -128,6 +128,8 @@ const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second";
128 const DESKTOP_RESIZE_WAIT_MS = 1000;
129 // A revealed tab's client opens the panel and attaches its stream a few round trips later, remote ones included.
130 const REVEAL_VIEWER_WAIT_MS = 1000;
131 +// A desktop panel's shown report waits on the renderer's layout, which a busy machine can push past a second.
132 +const REVEAL_DESKTOP_SHOWN_WAIT_MS = 3000;
133
134 const sleepUntil = (deadline: number) =>
135 new Promise<void>((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now())));
@@ -2649,11 +2651,17 @@ const make = Effect.gen(function* () {
2651 }),
2652 );
2653 // Only an explicit show is sure to reach a client; desktop tabs are visible without a viewer.
2654 + const remainingMs = deadlineMs - performance.now();
2655 if (reveal === true && tab.viewers.size === 0 && tab.desktop === null)
2653 - shown = awaitViewer(
2654 - tab,
2655 - Math.min(REVEAL_VIEWER_WAIT_MS, deadlineMs - performance.now()),
2656 - );
2656 + shown = awaitViewer(tab, Math.min(REVEAL_VIEWER_WAIT_MS, remainingMs));
2657 + // A desktop panel reports the revealed tab shown a few hops after the reveal.
2658 + else if (reveal === true && !isVisible(tab))
2659 + shown = runPromise(
2660 + desktopChannel.awaitShown(
2661 + tab,
2662 + Math.max(0, Math.min(REVEAL_DESKTOP_SHOWN_WAIT_MS, remainingMs)),
2663 + ),
2664 + ).then(constVoid);
2665 }
2666 await shown;
2667 return statusWithTitle(tab, request.agentSessionId);