Skip to content

Commit 6da3508

Browse files
committed
fix(preview): avoid restoring focus after user takeover
Prevent preview automation from reclaiming a stale composer or native renderer focus after the user takes control during an awaited press. Report native focus disposition and interruptions through the typed IPC contract, and cover renderer, desktop, and response serialization behavior with focused tests. Model: GPT-5 Harness: Codex CLI
1 parent c88499a commit 6da3508

10 files changed

Lines changed: 760 additions & 59 deletions

File tree

‎apps/desktop/src/ipc/methods/preview.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,42 @@ describe("preview IPC methods", () => {
170170
).toThrow();
171171
});
172172
});
173+
174+
describe("automationPressResult", () => {
175+
effectIt.effect("returns an interrupted outcome for human control takeover", () =>
176+
Effect.gen(function* () {
177+
const error = new PreviewManager.PreviewAutomationControlInterruptedError({
178+
operation: "press",
179+
tabId: "tab-1",
180+
webContentsId: 42,
181+
});
182+
183+
expect(yield* PreviewIpc.automationPressResult(Effect.fail(error))).toEqual({
184+
status: "interrupted",
185+
});
186+
}),
187+
);
188+
189+
effectIt.effect("returns a completed outcome after a successful press", () =>
190+
Effect.gen(function* () {
191+
expect(yield* PreviewIpc.automationPressResult(Effect.succeed("restored"))).toEqual({
192+
status: "completed",
193+
focusDisposition: "restored",
194+
});
195+
}),
196+
);
197+
198+
effectIt.effect("keeps unexpected failures as rejected effects", () =>
199+
Effect.gen(function* () {
200+
const error = new PreviewManager.PreviewWebContentsNotFoundError({
201+
tabId: "tab-1",
202+
webContentsId: 42,
203+
});
204+
const exit = yield* Effect.exit(PreviewIpc.automationPressResult(Effect.fail(error)));
205+
206+
expect(Exit.isFailure(exit)).toBe(true);
207+
if (Exit.isSuccess(exit)) return;
208+
expect(Cause.squash(exit.cause)).toBe(error);
209+
}),
210+
);
211+
});

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DesktopPreviewAutomationClickInputSchema,
55
DesktopPreviewAutomationEvaluateInputSchema,
66
DesktopPreviewAutomationPressInputSchema,
7+
DesktopPreviewAutomationPressResultSchema,
78
DesktopPreviewAutomationScrollInputSchema,
89
DesktopPreviewAutomationStatusSchema,
910
DesktopPreviewAutomationTypeInputSchema,
@@ -23,6 +24,8 @@ import {
2324
DesktopPreviewCreateTabInputSchema,
2425
DesktopPreviewTabInputSchema,
2526
DesktopPreviewWebviewConfigSchema,
27+
type DesktopPreviewAutomationPressFocusDisposition,
28+
type DesktopPreviewAutomationPressResult,
2629
PreviewAnnotationSubmissionResultSchema,
2730
PreviewAutomationSnapshot,
2831
DEFAULT_BROWSER_PROFILE_ID,
@@ -420,13 +423,28 @@ export const automationType = DesktopIpc.makeIpcMethod({
420423
export const automationPress = DesktopIpc.makeIpcMethod({
421424
channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
422425
payload: DesktopPreviewAutomationPressInputSchema,
423-
result: Schema.Void,
426+
result: DesktopPreviewAutomationPressResultSchema,
424427
handler: Effect.fn("desktop.ipc.preview.automationPress")(function* ({ tabId, input }) {
425428
const manager = yield* PreviewManager.PreviewManager;
426-
yield* manager.automationPress(tabId, input);
429+
return yield* automationPressResult(manager.automationPress(tabId, input));
427430
}),
428431
});
429432

433+
export const automationPressResult = (
434+
effect: Effect.Effect<
435+
DesktopPreviewAutomationPressFocusDisposition,
436+
PreviewManager.PreviewManagerError
437+
>,
438+
): Effect.Effect<DesktopPreviewAutomationPressResult, PreviewManager.PreviewManagerError> =>
439+
effect.pipe(
440+
Effect.map((focusDisposition) => ({ status: "completed" as const, focusDisposition })),
441+
Effect.catch((error: PreviewManager.PreviewManagerError) =>
442+
PreviewManager.isPreviewAutomationControlInterruptedError(error)
443+
? Effect.succeed({ status: "interrupted" as const })
444+
: Effect.fail(error),
445+
),
446+
);
447+
430448
export const automationScroll = DesktopIpc.makeIpcMethod({
431449
channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
432450
payload: DesktopPreviewAutomationScrollInputSchema,

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ const {
116116
createFromPath,
117117
fromId,
118118
getFocusedWebContents,
119+
getFocusedWindow,
119120
mkdir,
120121
showItemInFolder,
121122
webviewSend,
@@ -126,6 +127,9 @@ const {
126127
createFromPath: vi.fn((): { readonly isEmpty: () => boolean } => ({ isEmpty: () => false })),
127128
fromId: vi.fn<(_id?: number) => Electron.WebContents | null>((_id?: number) => null),
128129
getFocusedWebContents: vi.fn(() => null),
130+
getFocusedWindow: vi.fn<() => Electron.BrowserWindow | null>(
131+
() => ({}) as Electron.BrowserWindow,
132+
),
129133
mkdir: vi.fn((_path: string) => undefined),
130134
showItemInFolder: vi.fn(),
131135
webviewSend: vi.fn(),
@@ -134,7 +138,7 @@ const {
134138
}));
135139

136140
vi.mock("electron", () => ({
137-
BrowserWindow: browserWindowConstructor,
141+
BrowserWindow: Object.assign(browserWindowConstructor, { getFocusedWindow }),
138142
clipboard: {
139143
writeImage,
140144
},
@@ -466,6 +470,8 @@ describe("PreviewManager", () => {
466470
fromId.mockClear();
467471
getFocusedWebContents.mockReset();
468472
getFocusedWebContents.mockReturnValue(null);
473+
getFocusedWindow.mockReset();
474+
getFocusedWindow.mockReturnValue({} as Electron.BrowserWindow);
469475
mkdir.mockClear();
470476
writeFile.mockClear();
471477
showItemInFolder.mockClear();
@@ -3503,6 +3509,7 @@ describe("PreviewManager", () => {
35033509
withManager((manager) =>
35043510
Effect.gen(function* () {
35053511
let failKeyDown = false;
3512+
let interruptPress = false;
35063513
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
35073514
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
35083515
if (
@@ -3518,22 +3525,26 @@ describe("PreviewManager", () => {
35183525
) {
35193526
humanInput?.(
35203527
{},
3521-
{
3522-
kind: "key",
3523-
key: params["key"],
3524-
code: params["code"] ?? "Digit1",
3525-
},
3528+
interruptPress
3529+
? { kind: "pointer", x: 400, y: 300, button: 0 }
3530+
: {
3531+
kind: "key",
3532+
key: params["key"],
3533+
code: params["code"] ?? "Digit1",
3534+
},
35263535
);
35273536
}
35283537
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
35293538
});
35303539
const restoreFocus = vi.fn();
35313540
const focus = vi.fn();
3532-
getFocusedWebContents.mockReturnValue({
3541+
const previousFocused = {
35333542
id: 7,
35343543
isDestroyed: () => false,
35353544
focus: restoreFocus,
3536-
} as never);
3545+
} as never;
3546+
const focusedGuest = { id: 42, isDestroyed: () => false } as never;
3547+
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
35373548
fromId.mockReturnValue({
35383549
id: 42,
35393550
isDestroyed: () => false,
@@ -3571,7 +3582,7 @@ describe("PreviewManager", () => {
35713582
yield* manager.registerWebview("tab_input", 42);
35723583
yield* manager.automationType("tab_input", { text: "hello", clear: true });
35733584
yield* manager.automationType("tab_input", { text: "", clear: true });
3574-
yield* manager.automationPress("tab_input", { key: "x" });
3585+
expect(yield* manager.automationPress("tab_input", { key: "x" })).toBe("restored");
35753586

35763587
const calls = sendCommand.mock.calls;
35773588
const methods = calls.map(([method]) => method);
@@ -3630,6 +3641,8 @@ describe("PreviewManager", () => {
36303641
expect(sendCommand).toHaveBeenCalledWith("Input.setIgnoreInputEvents", { ignore: false });
36313642

36323643
sendCommand.mockClear();
3644+
getFocusedWebContents.mockReset();
3645+
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
36333646
failKeyDown = true;
36343647
const failedPress = yield* Effect.exit(manager.automationPress("tab_input", { key: "y" }));
36353648

@@ -3655,6 +3668,8 @@ describe("PreviewManager", () => {
36553668
).toHaveLength(1);
36563669

36573670
sendCommand.mockClear();
3671+
getFocusedWebContents.mockReset();
3672+
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
36583673
failKeyDown = false;
36593674
yield* manager.automationPress("tab_input", { key: "!" });
36603675
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
@@ -3669,6 +3684,40 @@ describe("PreviewManager", () => {
36693684
unmodifiedText: "!",
36703685
});
36713686
expect(restoreFocus).toHaveBeenCalledTimes(3);
3687+
3688+
sendCommand.mockClear();
3689+
getFocusedWebContents.mockReset();
3690+
getFocusedWebContents
3691+
.mockReturnValueOnce({ id: 7, isDestroyed: () => false, focus: restoreFocus } as never)
3692+
.mockReturnValueOnce(null);
3693+
getFocusedWindow.mockReturnValue(null);
3694+
expect(yield* manager.automationPress("tab_input", { key: "?" })).toBe("preserved");
3695+
expect(restoreFocus).toHaveBeenCalledTimes(3);
3696+
3697+
getFocusedWebContents.mockReset();
3698+
getFocusedWebContents
3699+
.mockReturnValueOnce({ id: 7, isDestroyed: () => false, focus: restoreFocus } as never)
3700+
.mockReturnValueOnce({ id: 43, isDestroyed: () => false } as never);
3701+
getFocusedWindow.mockReturnValue({} as Electron.BrowserWindow);
3702+
expect(yield* manager.automationPress("tab_input", { key: "/" })).toBe("preserved");
3703+
expect(restoreFocus).toHaveBeenCalledTimes(3);
3704+
3705+
sendCommand.mockClear();
3706+
getFocusedWebContents.mockReset();
3707+
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
3708+
interruptPress = true;
3709+
const interruptedPress = yield* Effect.exit(
3710+
manager.automationPress("tab_input", { key: "=" }),
3711+
);
3712+
expect(Exit.isFailure(interruptedPress)).toBe(true);
3713+
if (Exit.isSuccess(interruptedPress)) return;
3714+
expect(Option.getOrThrow(Cause.findErrorOption(interruptedPress.cause))).toMatchObject({
3715+
_tag: "PreviewAutomationControlInterruptedError",
3716+
operation: "press",
3717+
tabId: "tab_input",
3718+
webContentsId: 42,
3719+
});
3720+
expect(restoreFocus).toHaveBeenCalledTimes(3);
36723721
}),
36733722
),
36743723
);

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER } from "@t3tools/contracts";
99
import type {
1010
DesktopPreviewAnnotationTheme,
1111
DesktopPreviewAutomationStatus,
12+
DesktopPreviewAutomationPressFocusDisposition,
1213
DesktopPreviewColorScheme,
1314
DesktopPreviewFavicon,
1415
DesktopPreviewPointerEvent,
@@ -3866,49 +3867,84 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
38663867
{ operation: "automationPress.getFocusedWebContents", tabId, webContentsId: wc.id },
38673868
() => webContents.getFocusedWebContents(),
38683869
);
3870+
let focusDisposition: DesktopPreviewAutomationPressFocusDisposition = "preserved";
38693871
let keyDownAttempted = false;
3870-
const releaseInput = Effect.gen(function* () {
3871-
if (keyDownAttempted) {
3872-
yield* sendCleanup("Input.dispatchKeyEvent", keySequence.keyUp).pipe(Effect.ignore);
3873-
}
3874-
yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(
3875-
Effect.ignore,
3876-
);
3877-
if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
3878-
yield* attempt(
3872+
const releaseInput = (restoreFocus: boolean) =>
3873+
Effect.gen(function* () {
3874+
if (keyDownAttempted) {
3875+
yield* sendCleanup("Input.dispatchKeyEvent", keySequence.keyUp).pipe(Effect.ignore);
3876+
}
3877+
yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(
3878+
Effect.ignore,
3879+
);
3880+
const focusedWebContents = yield* attempt(
38793881
{
3880-
operation: "automationPress.restoreFocusedWebContents",
3882+
operation: "automationPress.getCurrentFocusedWebContents",
38813883
tabId,
3882-
webContentsId: previouslyFocused.id,
3884+
webContentsId: wc.id,
38833885
},
3884-
() => previouslyFocused.focus(),
3885-
).pipe(Effect.ignore);
3886-
}
3887-
});
3886+
() => webContents.getFocusedWebContents(),
3887+
).pipe(Effect.orElseSucceed(() => null));
3888+
const focusedWindow = yield* attempt(
3889+
{ operation: "automationPress.getFocusedWindow", tabId, webContentsId: wc.id },
3890+
() => BrowserWindow.getFocusedWindow(),
3891+
).pipe(Effect.orElseSucceed(() => null));
3892+
const automationOwnsFocus = focusedWindow !== null && focusedWebContents?.id === wc.id;
3893+
if (
3894+
restoreFocus &&
3895+
automationOwnsFocus &&
3896+
previouslyFocused &&
3897+
previouslyFocused.id !== wc.id &&
3898+
!previouslyFocused.isDestroyed()
3899+
) {
3900+
const restored = yield* attempt(
3901+
{
3902+
operation: "automationPress.restoreFocusedWebContents",
3903+
tabId,
3904+
webContentsId: previouslyFocused.id,
3905+
},
3906+
() => previouslyFocused.focus(),
3907+
).pipe(
3908+
Effect.as(true),
3909+
Effect.orElseSucceed(() => false),
3910+
);
3911+
if (restored) focusDisposition = "restored";
3912+
}
3913+
});
38883914

38893915
// Focus the guest WebContents itself, not its containing BrowserWindow. This
38903916
// activates native keyboard behavior for hidden/background previews without
38913917
// changing which thread is mounted in the UI. Restore the previous renderer
38923918
// after dispatch so automation never leaves the app's input focus behind.
3893-
yield* Effect.gen(function* () {
3894-
yield* attempt(
3895-
{ operation: "automationPress.focusWebContents", tabId, webContentsId: wc.id },
3896-
() => wc.focus(),
3919+
const pressExit = yield* Effect.exit(
3920+
Effect.gen(function* () {
3921+
yield* attempt(
3922+
{ operation: "automationPress.focusWebContents", tabId, webContentsId: wc.id },
3923+
() => wc.focus(),
3924+
);
3925+
yield* send("Page.bringToFront");
3926+
yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });
3927+
yield* expectAgentInput(tabId, keySequence.signal);
3928+
keyDownAttempted = true;
3929+
yield* send("Input.dispatchKeyEvent", keySequence.keyDown);
3930+
}),
3931+
);
3932+
const interrupted =
3933+
pressExit._tag === "Failure" &&
3934+
isPreviewAutomationControlInterruptedError(
3935+
Option.getOrNull(Cause.findErrorOption(pressExit.cause)),
38973936
);
3898-
yield* send("Page.bringToFront");
3899-
yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });
3900-
yield* expectAgentInput(tabId, keySequence.signal);
3901-
keyDownAttempted = true;
3902-
yield* send("Input.dispatchKeyEvent", keySequence.keyDown);
3903-
}).pipe(Effect.ensuring(releaseInput));
3937+
yield* releaseInput(!interrupted);
3938+
if (pressExit._tag === "Failure") return yield* Effect.failCause(pressExit.cause);
3939+
return focusDisposition;
39043940
});
39053941

39063942
const automationPress = Effect.fn("PreviewManager.automationPress")(function* (
39073943
tabId: string,
39083944
input: PreviewAutomationPressInput,
39093945
) {
39103946
const wc = yield* requireWebContents(tabId);
3911-
yield* withControlSession(tabId, wc, "press", (send, sendCleanup) =>
3947+
return yield* withControlSession(tabId, wc, "press", (send, sendCleanup) =>
39123948
performAutomationPress(tabId, wc, input, send, sendCleanup),
39133949
);
39143950
});
@@ -4558,7 +4594,7 @@ export class PreviewManager extends Context.Service<
45584594
readonly automationPress: (
45594595
tabId: string,
45604596
input: PreviewAutomationPressInput,
4561-
) => Effect.Effect<void, PreviewManagerError>;
4597+
) => Effect.Effect<DesktopPreviewAutomationPressFocusDisposition, PreviewManagerError>;
45624598
readonly automationScroll: (
45634599
tabId: string,
45644600
input: PreviewAutomationScrollInput,

0 commit comments

Comments
 (0)