Skip to content

Commit f1c7a8d

Browse files
fix(desktop): a navigated preview tab keeps the server tab it answers for
Navigating rebuilt the tab's state without its server tab, so once a user loaded a URL the Manager could no longer match the host's painting, pointer, or detach events to it. A hidden guest the agent then captured never painted, its screenshot timed out, and it stayed captured at 20Hz. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 550e8a8 commit f1c7a8d

2 files changed

Lines changed: 57 additions & 48 deletions

File tree

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

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,55 +1712,63 @@ describe("PreviewManager", () => {
17121712
}).pipe(Effect.provide(managerLayer()), Effect.scoped),
17131713
);
17141714

1715-
effectIt.effect("asks the client to paint a server tab while the agent captures it", () =>
1716-
Effect.gen(function* () {
1717-
const host = yield* DesktopBrowserHost.DesktopBrowserHost;
1718-
const serverTab = { threadId: "thread-1", tabId: "server-tab-1" };
1719-
const contents = Object.assign(
1720-
makeTestPreviewWebContents(async () => {
1721-
throw new Error("unused");
1722-
}, 42),
1723-
{ isDevToolsOpened: () => false, getUserAgent: () => "Electron" },
1724-
);
1725-
const screenshot = Promise.withResolvers<unknown>();
1726-
contents.debugger.sendCommand = vi.fn((method: string) =>
1727-
method === "Page.captureScreenshot" ? screenshot.promise : Promise.resolve(undefined),
1728-
) as unknown as typeof contents.debugger.sendCommand;
1729-
fromId.mockReturnValue(contents);
1730-
const manager = yield* PreviewManager.PreviewManager;
1731-
const capturing = yield* Queue.unbounded<boolean>();
1732-
yield* manager.subscribeStateChanges((_tabId, state) =>
1733-
Queue.offer(capturing, state.capturing).pipe(Effect.asVoid),
1734-
);
1735-
const attached = yield* Deferred.make<void>();
1736-
yield* host.events.pipe(
1737-
Stream.runForEach((line) =>
1738-
new TextDecoder().decode(line).includes('"type":"attached"')
1739-
? Deferred.succeed(attached, undefined)
1740-
: Effect.void,
1741-
),
1742-
Effect.forkScoped,
1743-
);
1744-
yield* manager.createTab("tab_1", { serverTab });
1745-
yield* manager.registerWebview("tab_1", 42);
1746-
yield* Deferred.await(attached);
1747-
yield* Queue.clear(capturing);
1748-
1749-
yield* host.handleCommandLine(
1750-
JSON.stringify({
1751-
type: "cdp",
1752-
...serverTab,
1753-
message: JSON.stringify({
1754-
id: 1,
1755-
method: "Page.captureScreenshot",
1756-
sessionId: "t3-preview-page",
1715+
effectIt.effect(
1716+
"asks the client to paint a navigated server tab while the agent captures it",
1717+
() =>
1718+
Effect.gen(function* () {
1719+
const host = yield* DesktopBrowserHost.DesktopBrowserHost;
1720+
const serverTab = { threadId: "thread-1", tabId: "server-tab-1" };
1721+
const contents = Object.assign(
1722+
makeTestPreviewWebContents(async () => {
1723+
throw new Error("unused");
1724+
}, 42),
1725+
{
1726+
isDevToolsOpened: () => false,
1727+
getUserAgent: () => "Electron",
1728+
loadURL: vi.fn(async () => undefined),
1729+
},
1730+
);
1731+
const screenshot = Promise.withResolvers<unknown>();
1732+
contents.debugger.sendCommand = vi.fn((method: string) =>
1733+
method === "Page.captureScreenshot" ? screenshot.promise : Promise.resolve(undefined),
1734+
) as unknown as typeof contents.debugger.sendCommand;
1735+
fromId.mockReturnValue(contents);
1736+
const manager = yield* PreviewManager.PreviewManager;
1737+
const capturing = yield* Queue.unbounded<boolean>();
1738+
yield* manager.subscribeStateChanges((_tabId, state) =>
1739+
Queue.offer(capturing, state.capturing).pipe(Effect.asVoid),
1740+
);
1741+
const attached = yield* Deferred.make<void>();
1742+
yield* host.events.pipe(
1743+
Stream.runForEach((line) =>
1744+
new TextDecoder().decode(line).includes('"type":"attached"')
1745+
? Deferred.succeed(attached, undefined)
1746+
: Effect.void,
1747+
),
1748+
Effect.forkScoped,
1749+
);
1750+
yield* manager.createTab("tab_1", { serverTab });
1751+
yield* manager.registerWebview("tab_1", 42);
1752+
yield* Deferred.await(attached);
1753+
// Navigating rebuilds the tab's state, which must keep the server tab it answers for.
1754+
yield* manager.navigate("tab_1", "https://example.com/next");
1755+
yield* Queue.clear(capturing);
1756+
1757+
yield* host.handleCommandLine(
1758+
JSON.stringify({
1759+
type: "cdp",
1760+
...serverTab,
1761+
message: JSON.stringify({
1762+
id: 1,
1763+
method: "Page.captureScreenshot",
1764+
sessionId: "t3-preview-page",
1765+
}),
17571766
}),
1758-
}),
1759-
);
1760-
expect(yield* Queue.take(capturing)).toBe(true);
1761-
screenshot.resolve({ data: "" });
1762-
expect(yield* Queue.take(capturing)).toBe(false);
1763-
}).pipe(Effect.provide(managerLayer()), Effect.scoped),
1767+
);
1768+
expect(yield* Queue.take(capturing)).toBe(true);
1769+
screenshot.resolve({ data: "" });
1770+
expect(yield* Queue.take(capturing)).toBe(false);
1771+
}).pipe(Effect.provide(managerLayer()), Effect.scoped),
17641772
);
17651773

17661774
effectIt.effect("discards a screenshot that resolves after its guest is replaced", () =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
19511951
controller: current?.controller ?? "none",
19521952
capturing: current?.capturing ?? false,
19531953
...(current?.favicon ? { favicon: current.favicon } : {}),
1954+
...(current?.serverTab ? { serverTab: current.serverTab } : {}),
19541955
updatedAt,
19551956
};
19561957
return [

0 commit comments

Comments
 (0)