@devries-cameron20 / t3code Mirror · read-only / commits / 8f0b729faf
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 22, 14:30 UTC

fix(preview): agent and browser panel show the same page after a late desktop attach (#22877)

When the server falls back to headless Chromium, it marks the tab headless, so the desktop streams that page instead of mounting a second one. Fixes #16901 Fixes #22882 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 27, 2026 at 15:29 UTC 8f0b729faf214ad05597d558eaab22b29a8589d7
6 files changed +71 -4
apps/server/src/preview/Manager.ts
+33
@@ -47,6 +47,10 @@ export class PreviewManager extends Context.Service<
47 readonly reportStatus: (
48 input: PreviewReportStatusInput & { readonly serverControlled?: boolean },
49 ) => Effect.Effect<void, PreviewError>;
50 + /** Records that a server tab runs headless. It stays headless until it closes. */
51 + readonly markHeadless: (
52 + input: PreviewCloseInput & { readonly tabId: string },
53 + ) => Effect.Effect<void, PreviewError>;
54 readonly requestReveal: (
55 input: PreviewCloseInput & { readonly tabId: string; readonly force: boolean },
56 ) => Effect.Effect<void, PreviewError>;
@@ -307,6 +311,34 @@ export const make = Effect.gen(function* PreviewManagerMake() {
311 );
312 });
313
314 + const markHeadless: PreviewManager["Service"]["markHeadless"] = Effect.fn(
315 + "PreviewManager.markHeadless",
316 + )(function* (input) {
317 + return yield* mutateExistingSession(
318 + input.threadId,
319 + input.tabId,
320 + Effect.fn("PreviewManager.markSessionHeadless")(function* (session) {
321 + if (session.snapshot.headless) return { next: session, emit: null, result: undefined };
322 + const snapshot: PreviewSessionSnapshot = {
323 + ...session.snapshot,
324 + headless: true,
325 + updatedAt: yield* currentIsoTimestamp,
326 + };
327 + return {
328 + next: { ...session, snapshot },
329 + emit: {
330 + type: "resized",
331 + threadId: session.threadId,
332 + tabId: session.tabId,
333 + createdAt: snapshot.updatedAt,
334 + snapshot,
335 + },
336 + result: undefined,
337 + };
338 + }),
339 + );
340 + });
341 +
342 const resize: PreviewManager["Service"]["resize"] = Effect.fn("PreviewManager.resize")(
343 function* (input) {
344 return yield* mutateExistingSession(
@@ -469,6 +501,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
501 requestReveal,
502 navigate,
503 reportStatus,
504 + markHeadless,
505 resize,
506 adjust,
507 refresh,
apps/server/src/preview/ServerBrowser.test.ts
+21
@@ -473,6 +473,27 @@ it.live.each(["desktop" as const, undefined])(
473 ).pipe(Effect.provide(layer)),
474 );
475
476 +it.effect("a tab the desktop does not draw is marked headless, so the desktop streams it", () =>
477 + Effect.scoped(
478 + Effect.gen(function* () {
479 + const { broker, tabId } = yield* ready;
480 + const manager = yield* Manager.PreviewManager;
481 + desktopRendersNext = true;
482 + const desktopTab = yield* broker.invoke<PreviewAutomationStatus>({
483 + scope,
484 + operation: "open",
485 + input: { reuseExistingTab: false, show: false },
486 + });
487 + const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
488 + const headlessOf = (id: string | null) =>
489 + sessions.find((session) => session.tabId === id)?.headless;
490 +
491 + expect(headlessOf(tabId)).toBe(true);
492 + expect(headlessOf(desktopTab.tabId)).toBeUndefined();
493 + }),
494 + ).pipe(Effect.provide(layer)),
495 +);
496 +
497 it.live.each([
498 { method: "goto" as const, message: { type: "navigate", url: "http://10.255.255.1/" } },
499 { method: "goBack" as const, message: { type: "history", delta: -1 } },
apps/server/src/preview/ServerBrowser.ts
+9 -1
@@ -677,7 +677,7 @@ const make = Effect.gen(function* () {
677 };
678
679 const desktopRenders = (snapshot: PreviewSessionSnapshot) =>
680 - desktopChannel.available
680 + desktopChannel.available && !snapshot.headless
681 ? runPromise(
682 desktopChannel.awaitAttached(
683 { threadId: snapshot.threadId, tabId: snapshot.tabId },
@@ -715,6 +715,14 @@ const make = Effect.gen(function* () {
715 adopted === undefined && (await desktopRenders(snapshot))
716 ? await connectDesktop(snapshot)
717 : null;
718 + // Otherwise a desktop that attaches late shows its own page, and the agent drives another.
719 + if (!desktop && desktopChannel.available) {
720 + await runPromise(
721 + manager
722 + .markHeadless({ threadId: ThreadId.make(snapshot.threadId), tabId: snapshot.tabId })
723 + .pipe(Effect.ignore),
724 + );
725 + }
726 const isolatedContext =
727 adopted === undefined &&
728 desktop === null &&
apps/web/src/browser/previewRuntime.ts
+3 -2
@@ -44,11 +44,12 @@ export function usePreviewAvailable(environmentId: EnvironmentId | null): boolea
44 export function rendersServerTabNatively(
45 environmentId: EnvironmentId,
46 primaryEnvironmentId: EnvironmentId | null,
47 - snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
47 + snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined,
48 ): boolean {
49 return (
50 isElectron &&
51 snapshot?.runtime === "server" &&
52 + !snapshot.headless &&
53 primaryEnvironmentId !== null &&
54 environmentId === primaryEnvironmentId
55 );
@@ -56,7 +57,7 @@ export function rendersServerTabNatively(
57
58 export function useRendersServerTabNatively(
59 environmentId: EnvironmentId,
59 - snapshot: Pick<PreviewSessionSnapshot, "runtime"> | null | undefined,
60 + snapshot: Pick<PreviewSessionSnapshot, "runtime" | "headless"> | null | undefined,
61 ): boolean {
62 return rendersServerTabNatively(environmentId, useAtomValue(primaryEnvironmentIdAtom), snapshot);
63 }
apps/web/src/components/preview/PreviewView.tsx
+3 -1
@@ -202,7 +202,9 @@ export function PreviewView({
202 const [serverControlledTabId, setServerControlledTabId] = useState<string | null>(null);
203 const serverInputDisabled = isServerTab && serverControlledTabId !== runtimeTabId;
204 const [serverFrameTabId, setServerFrameTabId] = useState<string | null>(null);
205 - const desktopOverlay = tabId ? (previewState.desktopByTabId[tabId] ?? null) : null;
205 + // A desktop page that mounted before its tab went headless leaves its stale state here.
206 + const desktopOverlay =
207 + tabId && !isServerTab ? (previewState.desktopByTabId[tabId] ?? null) : null;
208 const layoutZoomFactor = useBrowserLayoutZoomFactor(desktopOverlay?.zoomFactor ?? 1);
209 const navStatus = snapshot?.navStatus ?? { _tag: "Idle" as const };
210 const url = navStatus._tag === "Idle" ? "" : navStatus.url;
packages/contracts/src/preview.ts
+2
@@ -160,6 +160,8 @@ export const PreviewSessionSnapshot = Schema.Struct({
160 /** Fixed at open; Electron cannot change a webview partition after attach without losing page state. */
161 profileId: Schema.optional(BrowserProfileId),
162 runtime: Schema.optional(PreviewRuntime),
163 + /** The server runs this tab in headless Chromium, so the desktop streams it like other clients. */
164 + headless: Schema.optional(Schema.Boolean),
165 automationOwner: Schema.optional(Schema.String),
166 reveal: Schema.optional(Schema.Boolean),
167 revealRequest: Schema.optional(Schema.Struct({ id: Schema.String, force: Schema.Boolean })),