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

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

[codex] Fix preview automation edge cases (#3561)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 25, 2026 at 20:08 UTC e9ed70c5b935d8653c887c6a2f5c1f93013579d4
7 files changed +128 -8
apps/desktop/src/preview/Manager.test.ts
+2
@@ -1,4 +1,5 @@
1 import { it as effectIt } from "@effect/vitest";
2 +import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
3 import * as Cause from "effect/Cause";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
@@ -91,6 +92,7 @@ const layer = PreviewManager.layer.pipe(
92 Layer.provideMerge(environmentLayer),
93 Layer.provideMerge(fileSystemLayer),
94 Layer.provideMerge(Path.layer),
95 + Layer.provideMerge(Layer.succeed(HostProcessPlatform, "linux")),
96 );
97 const encodePreviewManagerError = Schema.encodeSync(PreviewManager.PreviewManagerError);
98
apps/desktop/src/preview/Manager.ts
+5 -1
@@ -25,6 +25,7 @@ import type {
25 PreviewAutomationTypeInput,
26 PreviewAutomationWaitForInput,
27 } from "@t3tools/contracts";
28 +import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
29 import { normalizePreviewUrl } from "@t3tools/shared/preview";
30 import {
31 type BrowserWindow,
@@ -379,6 +380,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
380 artifactDirectory: string,
381 ) {
382 const fileSystem = yield* FileSystem.FileSystem;
383 + const hostPlatform = yield* HostProcessPlatform;
384 const path = yield* Path.Path;
385 const parentScope = yield* Scope.Scope;
386 const context = yield* Effect.context<never>();
@@ -2226,7 +2228,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
2228 sendCleanup: SendCommand,
2229 ) {
2230 yield* prepareAutomationInput(send, false);
2229 - const keySequence = makePreviewAutomationKeySequence(input);
2231 + const keySequence = makePreviewAutomationKeySequence(input, {
2232 + isMac: hostPlatform === "darwin",
2233 + });
2234 const previouslyFocused = yield* attempt(
2235 { operation: "automationPress.getFocusedWebContents", tabId, webContentsId: wc.id },
2236 () => webContents.getFocusedWebContents(),
apps/desktop/src/preview/PreviewKeyboard.test.ts
+14 -1
@@ -42,7 +42,9 @@ describe("preview keyboard packets", () => {
42 });
43
44 it("suppresses text and uses raw key-down for shortcuts", () => {
45 - expect(makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown).toEqual({
45 + expect(
46 + makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }, { isMac: true }).keyDown,
47 + ).toEqual({
48 type: "rawKeyDown",
49 key: "a",
50 code: "KeyA",
@@ -50,9 +52,20 @@ describe("preview keyboard packets", () => {
52 windowsVirtualKeyCode: 65,
53 location: 0,
54 isKeypad: false,
55 + commands: ["selectAll"],
56 });
57 });
58
59 + it("maps common macOS editing shortcuts without changing other platforms", () => {
60 + expect(
61 + makePreviewAutomationKeySequence({ key: "z", modifiers: ["Shift", "Meta"] }, { isMac: true })
62 + .keyDown.commands,
63 + ).toEqual(["redo"]);
64 + expect(
65 + makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown,
66 + ).not.toHaveProperty("commands");
67 + });
68 +
69 it("resolves shifted printable keys to their browser values", () => {
70 const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
71 expect(sequence.keyDown).toMatchObject({
apps/desktop/src/preview/PreviewKeyboard.ts
+40
@@ -20,6 +20,7 @@ export interface PreviewAutomationKeyEvent {
20 readonly isKeypad: boolean;
21 readonly text?: string;
22 readonly unmodifiedText?: string;
23 + readonly commands?: ReadonlyArray<string>;
24 }
25
26 export interface PreviewAutomationKeySequence {
@@ -79,6 +80,42 @@ const PRINTABLE_KEYS: ReadonlyArray<KeyDefinition> = [
80 { code: "Slash", key: "/", shiftedKey: "?", keyCode: 191 },
81 ];
82
83 +/**
84 + * Chromium does not infer macOS editing commands from synthetic Meta chords.
85 + * Keep the common browser editing/navigation shortcuts explicit so dispatched
86 + * key events behave like their physical-key equivalents.
87 + */
88 +const MAC_EDITING_COMMANDS: Readonly<Record<string, string>> = {
89 + "Meta+Backspace": "deleteToBeginningOfLine",
90 + "Meta+ArrowUp": "moveToBeginningOfDocument",
91 + "Meta+ArrowDown": "moveToEndOfDocument",
92 + "Meta+ArrowLeft": "moveToLeftEndOfLine",
93 + "Meta+ArrowRight": "moveToRightEndOfLine",
94 + "Shift+Meta+ArrowUp": "moveToBeginningOfDocumentAndModifySelection",
95 + "Shift+Meta+ArrowDown": "moveToEndOfDocumentAndModifySelection",
96 + "Shift+Meta+ArrowLeft": "moveToLeftEndOfLineAndModifySelection",
97 + "Shift+Meta+ArrowRight": "moveToRightEndOfLineAndModifySelection",
98 + "Meta+KeyA": "selectAll",
99 + "Meta+KeyC": "copy",
100 + "Meta+KeyX": "cut",
101 + "Meta+KeyV": "paste",
102 + "Meta+KeyZ": "undo",
103 + "Shift+Meta+KeyZ": "redo",
104 +};
105 +const SHORTCUT_MODIFIER_ORDER = ["Shift", "Control", "Alt", "Meta"] as const;
106 +
107 +const macEditingCommands = (
108 + code: string,
109 + modifiers: PreviewAutomationPressInput["modifiers"],
110 +): ReadonlyArray<string> => {
111 + const shortcut = [
112 + ...SHORTCUT_MODIFIER_ORDER.filter((modifier) => modifiers?.includes(modifier)),
113 + code,
114 + ].join("+");
115 + const command = MAC_EDITING_COMMANDS[shortcut];
116 + return command ? [command] : [];
117 +};
118 +
119 const modifierMask = (modifiers: PreviewAutomationPressInput["modifiers"]): number =>
120 (modifiers ?? []).reduce((value, modifier) => {
121 switch (modifier) {
@@ -136,12 +173,14 @@ function resolveKeyDefinition(input: PreviewAutomationPressInput): KeyDefinition
173 */
174 export function makePreviewAutomationKeySequence(
175 input: PreviewAutomationPressInput,
176 + options?: { readonly isMac?: boolean },
177 ): PreviewAutomationKeySequence {
178 const definition = resolveKeyDefinition(input);
179 const modifiers = modifierMask(input.modifiers);
180 const suppressText = input.modifiers?.some((modifier) => modifier !== "Shift") ?? false;
181 const text = suppressText ? "" : (definition.text ?? "");
182 const location = definition.location ?? 0;
183 + const commands = options?.isMac ? macEditingCommands(definition.code, input.modifiers) : [];
184 const shared = {
185 key: definition.key,
186 code: definition.code,
@@ -156,6 +195,7 @@ export function makePreviewAutomationKeySequence(
195 type: text ? "keyDown" : "rawKeyDown",
196 ...shared,
197 ...(text ? { text, unmodifiedText: text } : {}),
198 + ...(commands.length > 0 ? { commands } : {}),
199 },
200 keyUp: { type: "keyUp", ...shared },
201 signal: { kind: "key", key: definition.key, code: definition.code },
apps/web/src/components/preview/PreviewAutomationHosts.tsx
+13 -6
@@ -51,6 +51,7 @@ import {
51 PreviewAutomationTargetUnavailableError,
52 PreviewAutomationViewportTimeoutError,
53 } from "./previewAutomationErrors";
54 +import { previewAutomationOpenNeedsOverlay } from "./previewAutomationOpenReadiness";
55 import { createPreviewAutomationRequestConsumerAtom } from "./previewAutomationRequestConsumer";
56 import { createPreviewAutomationClientId } from "./previewAutomationClientId";
57 import {
@@ -333,6 +334,9 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
334 const input = request.input as PreviewAutomationOpenInput;
335 let activeTabId =
336 (input.reuseExistingTab ?? true) ? (state.snapshot?.tabId ?? null) : null;
337 + let activeSnapshot = activeTabId
338 + ? (state.sessions[activeTabId] ?? state.snapshot ?? undefined)
339 + : undefined;
340 const reusedExistingTab = activeTabId !== null;
341 tabId = activeTabId;
342 if (!activeTabId) {
@@ -349,17 +353,20 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
353 const snapshot = result.value;
354 applyPreviewServerSnapshot(threadRef, snapshot);
355 activeTabId = snapshot.tabId;
356 + activeSnapshot = snapshot;
357 tabId = activeTabId;
358 }
359 if (input.show ?? true) {
360 useRightPanelStore.getState().openBrowser(threadRef, activeTabId);
361 }
357 - await waitForDesktopOverlay(
358 - threadRef,
359 - request.requestId,
360 - activeTabId,
361 - request.timeoutMs,
362 - );
362 + if (activeSnapshot && previewAutomationOpenNeedsOverlay(input, activeSnapshot)) {
363 + await waitForDesktopOverlay(
364 + threadRef,
365 + request.requestId,
366 + activeTabId,
367 + request.timeoutMs,
368 + );
369 + }
370 if (reusedExistingTab && input.url && previewBridge) {
371 const resolution = resolveBrowserNavigationTarget(environmentId, {
372 kind: "url",
apps/web/src/components/preview/previewAutomationOpenReadiness.test.ts new
+46
@@ -0,0 +1,46 @@
1 +import type { PreviewAutomationOpenInput, PreviewSessionSnapshot } from "@t3tools/contracts";
2 +import { describe, expect, it } from "vite-plus/test";
3 +
4 +import { previewAutomationOpenNeedsOverlay } from "./previewAutomationOpenReadiness";
5 +
6 +const snapshot = (navStatus: PreviewSessionSnapshot["navStatus"]): PreviewSessionSnapshot => ({
7 + threadId: "thread-1",
8 + tabId: "tab-1",
9 + navStatus,
10 + canGoBack: false,
11 + canGoForward: false,
12 + updatedAt: "2026-06-26T00:00:00.000Z",
13 +});
14 +
15 +describe("preview automation open readiness", () => {
16 + it("does not wait for a desktop overlay when opening an empty tab", () => {
17 + expect(
18 + previewAutomationOpenNeedsOverlay(
19 + {} as PreviewAutomationOpenInput,
20 + snapshot({ _tag: "Idle" }),
21 + ),
22 + ).toBe(false);
23 + });
24 +
25 + it("waits when an empty tab is immediately given a URL", () => {
26 + expect(
27 + previewAutomationOpenNeedsOverlay(
28 + { url: "https://example.com" } as PreviewAutomationOpenInput,
29 + snapshot({ _tag: "Idle" }),
30 + ),
31 + ).toBe(true);
32 + });
33 +
34 + it("waits for existing tabs that already have rendered content", () => {
35 + expect(
36 + previewAutomationOpenNeedsOverlay(
37 + {} as PreviewAutomationOpenInput,
38 + snapshot({
39 + _tag: "Success",
40 + url: "https://example.com/",
41 + title: "Example",
42 + }),
43 + ),
44 + ).toBe(true);
45 + });
46 +});
apps/web/src/components/preview/previewAutomationOpenReadiness.ts new
+8
@@ -0,0 +1,8 @@
1 +import type { PreviewAutomationOpenInput, PreviewSessionSnapshot } from "@t3tools/contracts";
2 +
3 +export function previewAutomationOpenNeedsOverlay(
4 + input: PreviewAutomationOpenInput,
5 + snapshot: PreviewSessionSnapshot,
6 +): boolean {
7 + return input.url !== undefined || snapshot.navStatus._tag !== "Idle";
8 +}