@devries-cameron20 / t3code Mirror · read-only / commits / 08930cafe3
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 22:15 UTC

fix(server): preview_open reports a tab it shows as visible (#26055)

Julius Marminge committed Nov 11, 2026 at 14:57 UTC 08930cafe3d074ac26c65a000de4e801a8c2c317
3 files changed +59 -2
apps/server/src/preview/ServerBrowser.test.ts
+27
@@ -2255,6 +2255,33 @@ it.live("agents read a human's tab with no arguments and act on it only while no
2255 ).pipe(Effect.provide(layer)),
2256 );
2257
2258 +it.live("preview_open reports a tab it shows as visible once the client attaches", () =>
2259 + Effect.scoped(
2260 + Effect.gen(function* () {
2261 + const browser = yield* ServerBrowser.ServerBrowser;
2262 + const broker = yield* Broker.PreviewAutomationBroker;
2263 + const manager = yield* Manager.PreviewManager;
2264 + const events = yield* manager.subscribeEvents;
2265 + yield* Effect.yieldNow;
2266 + // Like a real client, the stream attaches asynchronously after the reveal request arrives.
2267 + yield* Effect.gen(function* () {
2268 + let event;
2269 + do event = yield* PubSub.take(events);
2270 + while (event.type !== "navigated" || !event.snapshot.revealRequest);
2271 + yield* browser.attachViewer(viewerInput(event.tabId, false)).pipe(Effect.forkScoped);
2272 + }).pipe(Effect.forkScoped);
2273 +
2274 + const opened = yield* broker.invoke<PreviewAutomationStatus>({
2275 + scope,
2276 + operation: "open",
2277 + input: { reuseExistingTab: false, open: true },
2278 + });
2279 +
2280 + expect(opened.visible).toBe(true);
2281 + }),
2282 + ).pipe(Effect.provide(layer)),
2283 +);
2284 +
2285 it.live("a session's own tab stays its default while other sessions' tabs are listed", () =>
2286 Effect.scoped(
2287 Effect.gen(function* () {
apps/server/src/preview/ServerBrowser.ts
+27
@@ -106,10 +106,24 @@ const AGENT_CURSOR_MOVE_MS = 160;
106 const AGENT_CURSOR_CLICK_LEAD_MS = 40;
107 /** A dialog blocks page script, so a desktop size read gives up after this. */
108 const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second";
109 +// A revealed tab's client opens the panel and attaches its stream a few round trips later, remote ones included.
110 +const REVEAL_VIEWER_WAIT_MS = 1000;
111
112 const sleepUntil = (deadline: number) =>
113 new Promise<void>((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now())));
114
115 +const awaitViewer = async (tab: ServerTab, timeoutMs: number) => {
116 + const attached = (tab.viewerAttached ??= Promise.withResolvers());
117 + let timer: ReturnType<typeof setTimeout> | undefined;
118 + await Promise.race([
119 + attached.promise,
120 + new Promise<void>((resolve) => {
121 + timer = setTimeout(resolve, Math.max(0, timeoutMs));
122 + }),
123 + ]);
124 + clearTimeout(timer);
125 +};
126 +
127 const EDITABLE_AT_POINT_SCRIPT = `(x, y) => {
128 let element = document.elementFromPoint(x, y);
129 while (element && element.shadowRoot) {
@@ -303,6 +317,7 @@ interface ServerTab {
317 readonly createdAt: number;
318 usedAt: number;
319 readonly viewers: Set<ViewerState>;
320 + viewerAttached: PromiseWithResolvers<void> | null;
321 readonly consoleEntries: Array<PreviewAutomationConsoleEntry>;
322 readonly networkEntries: Array<PreviewAutomationNetworkEntry>;
323 readonly actionTimeline: Array<PreviewAutomationActionEvent>;
@@ -755,6 +770,7 @@ const make = Effect.gen(function* () {
770 if (tabs.get(key) !== tab) return;
771 tabs.delete(key);
772 tab.closing = true;
773 + tab.viewerAttached?.resolve();
774 clearAbortedNavigation(tab);
775 // A desktop page outlives the connection unless its session closed with it.
776 const end = tab.desktop && !closeSession && sessionOpen(tab) ? "reconnect" : "gone";
@@ -880,6 +896,7 @@ const make = Effect.gen(function* () {
896 createdAt: Date.now(),
897 usedAt: Date.now(),
898 viewers: new Set(),
899 + viewerAttached: null,
900 consoleEntries: [],
901 networkEntries: [],
902 actionTimeline: [],
@@ -2135,6 +2152,7 @@ const make = Effect.gen(function* () {
2152 });
2153 }
2154 const reveal = open.open ?? open.show;
2155 + let shown: Promise<void> | undefined;
2156 if (reveal !== false) {
2157 await runPromise(
2158 manager.requestReveal({
@@ -2143,7 +2161,14 @@ const make = Effect.gen(function* () {
2161 force: reveal === true,
2162 }),
2163 );
2164 + // Only an explicit show is sure to reach a client; desktop tabs are visible without a viewer.
2165 + if (reveal === true && tab.viewers.size === 0 && tab.desktop === null)
2166 + shown = awaitViewer(
2167 + tab,
2168 + Math.min(REVEAL_VIEWER_WAIT_MS, deadlineMs - performance.now()),
2169 + );
2170 }
2171 + await shown;
2172 return statusWithTitle(tab, request.agentSessionId);
2173 },
2174 deadlineMs,
@@ -2668,6 +2693,8 @@ const make = Effect.gen(function* () {
2693 yield* Effect.acquireRelease(
2694 Effect.sync(() => {
2695 tab.viewers.add(viewer);
2696 + tab.viewerAttached?.resolve();
2697 + tab.viewerAttached = null;
2698 reportLiveTabs();
2699 }),
2700 () =>
packages/contracts/src/previewAutomation.ts
+5 -2
@@ -71,6 +71,9 @@ export const PreviewAutomationProfile = Schema.Struct({
71 });
72 export type PreviewAutomationProfile = typeof PreviewAutomationProfile.Type;
73
74 +const VISIBLE_DESCRIPTION =
75 + "True while a client shows this tab: a streamed live view, or the desktop app's own browser panel.";
76 +
77 /** Why the tab shows the browser's error page instead of the page it tried to load. */
78 export const PreviewAutomationLoadError = Schema.Struct({
79 url: Schema.String,
@@ -81,7 +84,7 @@ export type PreviewAutomationLoadError = typeof PreviewAutomationLoadError.Type;
84
85 export const PreviewAutomationStatus = Schema.Struct({
86 available: Schema.Boolean,
84 - visible: Schema.Boolean,
87 + visible: Schema.Boolean.annotate({ description: VISIBLE_DESCRIPTION }),
88 tabId: Schema.NullOr(PreviewTabId),
89 url: Schema.NullOr(Schema.String),
90 title: Schema.NullOr(Schema.String),
@@ -128,7 +131,7 @@ export const PreviewAutomationStatus = Schema.Struct({
131 openerTabId: Schema.optional(PreviewTabId),
132 owner: Schema.optional(PreviewAutomationTabOwner),
133 ownedByCaller: Schema.optional(Schema.Boolean),
131 - visible: Schema.optional(Schema.Boolean),
134 + visible: Schema.optional(Schema.Boolean.annotate({ description: VISIBLE_DESCRIPTION })),
135 profileId: Schema.optional(BrowserProfileId),
136 }),
137 ),