Skip to content

Commit 7aaff44

Browse files
committed
fix(desktop): return screenshots independently of native warmup
1 parent 7fed734 commit 7aaff44

4 files changed

Lines changed: 150 additions & 81 deletions

File tree

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

Lines changed: 80 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -156,73 +156,109 @@ describe("desktop agent screenshot rendering lease", () => {
156156
}).pipe(Effect.scoped),
157157
);
158158

159-
it.effect.each(["native", "cdp"] as const)(
160-
"reports a stable capture error when %s fails and allows a retry",
161-
(failedStage) =>
159+
it.effect.each(["reject", "throw"] as const)(
160+
"returns the CDP screenshot even if native warmup fails with %s",
161+
(failure) =>
162162
Effect.gen(function* () {
163163
const { guest, leases, replies, command, acknowledge } = yield* setup;
164-
const cause = new Error("internal compositor failure detail");
165-
const captureFailed = vi.fn<(error: unknown) => void>();
166-
guest.tab.withCaptureActivity = (capture) =>
167-
capture.pipe(Effect.tapError((error) => Effect.sync(() => captureFailed(error))));
168-
if (failedStage === "native") guest.capturePage.mockRejectedValueOnce(cause);
169-
else guest.screenshot.mockRejectedValueOnce(cause);
164+
const cause = new Error("native unavailable");
165+
if (failure === "reject") guest.capturePage.mockRejectedValueOnce(cause);
166+
else
167+
guest.capturePage.mockImplementationOnce(() => {
168+
throw cause;
169+
});
170170
yield* command(1);
171171
const active = yield* acknowledge;
172-
expect((yield* Queue.take(replies)).error?.message).toBe(
173-
"Desktop browser screenshot failed: the compositor could not capture a frame.",
174-
);
175-
expect(captureFailed).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ cause }));
172+
expect(yield* Queue.take(replies)).toEqual({ id: 1, result: { data: "encoded-by-cdp" } });
176173
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
177-
yield* command(2);
178-
yield* acknowledge;
179-
expect(yield* Queue.take(replies)).toEqual({ id: 2, result: { data: "encoded-by-cdp" } });
180174
}).pipe(Effect.scoped),
181175
);
182176

183-
it.effect.each(["native", "cdp"] as const)(
184-
"bounds %s waiting without accumulating captures or blocking DOM commands",
185-
(stalledStage) =>
177+
it.effect("preserves the CDP failure cause and allows a retry", () =>
178+
Effect.gen(function* () {
179+
const { guest, leases, replies, command, acknowledge } = yield* setup;
180+
const cause = new Error("internal compositor failure detail");
181+
const captureFailed = vi.fn<(error: unknown) => void>();
182+
guest.tab.withCaptureActivity = (capture) =>
183+
capture.pipe(Effect.tapError((error) => Effect.sync(() => captureFailed(error))));
184+
guest.screenshot.mockRejectedValueOnce(cause);
185+
yield* command(1);
186+
const active = yield* acknowledge;
187+
expect((yield* Queue.take(replies)).error?.message).toBe(
188+
"Desktop browser screenshot failed: the compositor could not capture a frame.",
189+
);
190+
expect(captureFailed).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ cause }));
191+
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
192+
yield* command(2);
193+
yield* acknowledge;
194+
expect(yield* Queue.take(replies)).toEqual({ id: 2, result: { data: "encoded-by-cdp" } });
195+
}).pipe(Effect.scoped),
196+
);
197+
198+
it.effect.each(["success", "failure"] as const)(
199+
"returns CDP %s while retaining the slot for pending native work",
200+
(outcome) =>
186201
Effect.gen(function* () {
187202
const { guest, leases, replies, command, acknowledge } = yield* setup;
188-
const pending = Promise.withResolvers<{ data: string }>();
189-
if (stalledStage === "native")
190-
guest.capturePage.mockImplementationOnce(() => {
191-
guest.nativeStarted.resolve();
192-
return pending.promise;
193-
});
194-
else
195-
guest.screenshot.mockImplementationOnce(() => {
196-
guest.screenshotStarted.resolve();
197-
return pending.promise;
198-
});
203+
const pending = Promise.withResolvers<object>();
204+
guest.capturePage.mockImplementationOnce(() => pending.promise);
205+
if (outcome === "failure") guest.screenshot.mockRejectedValueOnce(new Error("CDP failed"));
199206
yield* command(1);
200207
const active = yield* acknowledge;
201-
yield* Effect.promise(
202-
() => (stalledStage === "native" ? guest.nativeStarted : guest.screenshotStarted).promise,
203-
);
204-
yield* TestClock.adjust("8 seconds");
205-
expect((yield* Queue.take(replies)).error?.message).toContain("within 8 seconds");
208+
const reply = yield* Queue.take(replies);
209+
if (outcome === "success")
210+
expect(reply).toEqual({ id: 1, result: { data: "encoded-by-cdp" } });
211+
else
212+
expect(reply.error?.message).toBe(
213+
"Desktop browser screenshot failed: the compositor could not capture a frame.",
214+
);
206215
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
207216
yield* command(2);
208217
expect((yield* Queue.take(replies)).error?.message).toContain(
209218
"previous capture is still pending",
210219
);
211220
yield* command(3, "DOM.enable");
212221
expect((yield* Queue.take(replies)).id).toBe(3);
213-
expect(guest.capturePage).toHaveBeenCalledTimes(stalledStage === "cdp" ? 2 : 1);
214-
pending.resolve({ data: "late-image" });
222+
pending.resolve({});
215223
yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));
216-
expect(guest.screenshot).toHaveBeenCalledTimes(1);
224+
expect(guest.capturePage).toHaveBeenCalledTimes(1);
217225
yield* command(4);
218226
yield* acknowledge;
219227
expect(yield* Queue.take(replies)).toEqual({ id: 4, result: { data: "encoded-by-cdp" } });
220-
expect(guest.capturePage.mock.calls.length).toBeLessThanOrEqual(
221-
stalledStage === "cdp" ? 4 : 3,
222-
);
223228
}).pipe(Effect.scoped),
224229
);
225230

231+
it.effect("bounds CDP waiting without accumulating captures or blocking DOM commands", () =>
232+
Effect.gen(function* () {
233+
const { guest, leases, replies, command, acknowledge } = yield* setup;
234+
const pending = Promise.withResolvers<{ data: string }>();
235+
guest.screenshot.mockImplementationOnce(() => {
236+
guest.screenshotStarted.resolve();
237+
return pending.promise;
238+
});
239+
yield* command(1);
240+
const active = yield* acknowledge;
241+
yield* Effect.promise(() => guest.screenshotStarted.promise);
242+
yield* TestClock.adjust("8 seconds");
243+
expect((yield* Queue.take(replies)).error?.message).toContain("within 8 seconds");
244+
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
245+
yield* command(2);
246+
expect((yield* Queue.take(replies)).error?.message).toContain(
247+
"previous capture is still pending",
248+
);
249+
yield* command(3, "DOM.enable");
250+
expect((yield* Queue.take(replies)).id).toBe(3);
251+
expect(guest.capturePage).toHaveBeenCalledTimes(2);
252+
pending.resolve({ data: "late-image" });
253+
yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));
254+
expect(guest.screenshot).toHaveBeenCalledTimes(1);
255+
yield* command(4);
256+
yield* acknowledge;
257+
expect(yield* Queue.take(replies)).toEqual({ id: 4, result: { data: "encoded-by-cdp" } });
258+
expect(guest.capturePage.mock.calls.length).toBeLessThanOrEqual(4);
259+
}).pipe(Effect.scoped),
260+
);
261+
226262
it.effect("retains the pending CDP slot when native capture throws synchronously", () =>
227263
Effect.gen(function* () {
228264
const { guest, leases, replies, command, acknowledge } = yield* setup;
@@ -258,7 +294,8 @@ describe("desktop agent screenshot rendering lease", () => {
258294
(lifecycle) =>
259295
Effect.gen(function* () {
260296
const { host, guest, leases, replies, command, acknowledge } = yield* setup;
261-
const pending = Promise.withResolvers<object>();
297+
const pending = Promise.withResolvers<{ data: string }>();
298+
guest.screenshot.mockImplementationOnce(() => pending.promise);
262299
guest.capturePage.mockImplementationOnce(() => {
263300
guest.nativeStarted.resolve();
264301
return pending.promise;
@@ -272,7 +309,7 @@ describe("desktop agent screenshot rendering lease", () => {
272309
else host.attach(key, makeGuest(42).tab);
273310
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
274311
if (lifecycle === "detach") host.attach(key, guest.tab);
275-
pending.resolve({});
312+
pending.resolve({ data: "discarded" });
276313
yield* command(2, "DOM.enable");
277314
expect((yield* Queue.take(replies)).id).toBe(2);
278315
yield* Effect.promise(() => new Promise((resolve) => setImmediate(resolve)));

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,20 @@ export interface DesktopBrowserTabDebugger {
5050

5151
class DesktopBrowserCaptureError extends Schema.TaggedError<DesktopBrowserCaptureError>()(
5252
"DesktopBrowserCaptureError",
53-
{ reason: Schema.String, cause: Schema.optional(Schema.Defect()) },
53+
{
54+
reason: Schema.Literals(["pending", "changed", "paint-timeout", "capture-timeout", "failed"]),
55+
cause: Schema.optional(Schema.Defect()),
56+
},
5457
) {
5558
override get message(): string {
56-
return `Desktop browser screenshot failed: ${this.reason}`;
59+
const details = {
60+
pending: "a previous capture is still pending.",
61+
changed: "the browser tab changed during capture.",
62+
"paint-timeout": "the browser surface did not become paintable within 2 seconds.",
63+
"capture-timeout": "the compositor did not supply a screenshot within 8 seconds.",
64+
failed: "the compositor could not capture a frame.",
65+
};
66+
return `Desktop browser screenshot failed: ${details[this.reason]}`;
5767
}
5868
}
5969

@@ -131,14 +141,14 @@ export const make = Effect.gen(function* () {
131141
) => {
132142
const { webContents, debugger: debuggee } = tab.debuggee;
133143
if (capturing.has(webContents)) {
134-
throw new DesktopBrowserCaptureError({ reason: "a previous capture is still pending." });
144+
throw new DesktopBrowserCaptureError({ reason: "pending" });
135145
}
136146
capturing.add(webContents);
137147
let pending: Promise<unknown> | undefined;
138148
const requireCurrent = (signal: AbortSignal) => {
139149
signal.throwIfAborted();
140150
if (tabs.get(keyOf(tab.key)) !== tab || webContents.isDestroyed()) {
141-
throw new DesktopBrowserCaptureError({ reason: "the browser tab changed during capture." });
151+
throw new DesktopBrowserCaptureError({ reason: "changed" });
142152
}
143153
};
144154
try {
@@ -164,54 +174,52 @@ export const make = Effect.gen(function* () {
164174
orElse: () =>
165175
Effect.fail(
166176
new DesktopBrowserCaptureError({
167-
reason: "the browser surface did not become paintable within 2 seconds.",
177+
reason: "paint-timeout",
168178
}),
169179
),
170180
}),
171181
);
172182
return yield* Effect.tryPromise({
173-
try: (signal) => {
174-
pending = (async () => {
175-
requireCurrent(signal);
176-
// CDP may resize the surface for a scaled clip. Request its
177-
// screenshot first, then produce an initial frame and, if
178-
// CDP is still waiting, a frame after that capture setup.
179-
let screenshotSettled = false;
180-
const screenshotRequest = Promise.resolve()
181-
.then(() => debuggee.sendCommand("Page.captureScreenshot", parameters))
182-
.finally(() => {
183-
screenshotSettled = true;
183+
try: async (signal) => {
184+
requireCurrent(signal);
185+
// CDP may resize the surface for a scaled clip. Request its
186+
// screenshot first, then produce an initial frame and, if
187+
// CDP is still waiting, a frame after that capture setup.
188+
let screenshotSettled = false;
189+
const screenshotRequest = Promise.resolve()
190+
.then(() => debuggee.sendCommand("Page.captureScreenshot", parameters))
191+
.finally(() => {
192+
screenshotSettled = true;
193+
});
194+
// Neither API is cancellable. Retain the slot until both
195+
// settle, including when one rejects or the caller times out.
196+
pending = Promise.allSettled([
197+
screenshotRequest,
198+
Promise.resolve().then(async () => {
199+
await webContents.capturePage(undefined, {
200+
stayHidden: true,
201+
stayAwake: false,
184202
});
185-
// Neither API is cancellable. Retain the slot until both
186-
// settle, including when one rejects or the caller times out.
187-
const [screenshot, warmup] = await Promise.allSettled([
188-
screenshotRequest,
189-
Promise.resolve().then(async () => {
203+
requireCurrent(signal);
204+
if (!screenshotSettled) {
190205
await webContents.capturePage(undefined, {
191206
stayHidden: true,
192207
stayAwake: false,
193208
});
194-
requireCurrent(signal);
195-
if (!screenshotSettled) {
196-
await webContents.capturePage(undefined, {
197-
stayHidden: true,
198-
stayAwake: false,
199-
});
200-
}
201-
}),
202-
]);
203-
requireCurrent(signal);
204-
if (screenshot.status === "rejected") throw screenshot.reason;
205-
if (warmup.status === "rejected") throw warmup.reason;
206-
return screenshot.value;
207-
})();
208-
return pending;
209+
}
210+
}),
211+
]);
212+
// Native capture only requests paint; its unused image must
213+
// not delay or replace CDP's screenshot result.
214+
const screenshot = await screenshotRequest;
215+
requireCurrent(signal);
216+
return screenshot;
209217
},
210218
catch: (cause) =>
211219
isDesktopBrowserCaptureError(cause)
212220
? cause
213221
: new DesktopBrowserCaptureError({
214-
reason: "the compositor could not capture a frame.",
222+
reason: "failed",
215223
cause,
216224
}),
217225
}).pipe(
@@ -220,7 +228,7 @@ export const make = Effect.gen(function* () {
220228
orElse: () =>
221229
Effect.fail(
222230
new DesktopBrowserCaptureError({
223-
reason: "the compositor did not supply a screenshot within 8 seconds.",
231+
reason: "capture-timeout",
224232
}),
225233
),
226234
}),

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,6 +2836,12 @@ describe("PreviewManager", () => {
28362836
yield* manager.startRecording("capture");
28372837
const started = Promise.withResolvers<void>();
28382838
const pending = Promise.withResolvers<typeof image>();
2839+
const pendingScreenshot = Promise.withResolvers<unknown>();
2840+
vi.mocked(contents.debugger.sendCommand).mockImplementation((method) =>
2841+
method === "Page.captureScreenshot"
2842+
? pendingScreenshot.promise
2843+
: Promise.resolve(undefined),
2844+
);
28392845
capturePage.mockImplementationOnce(() => {
28402846
started.resolve();
28412847
return pending.promise;
@@ -2865,6 +2871,7 @@ describe("PreviewManager", () => {
28652871
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
28662872
expect(contents.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
28672873
pending.resolve(image);
2874+
pendingScreenshot.resolve({ data: "discarded" });
28682875
}).pipe(Effect.provide(managerLayer()), Effect.scoped),
28692876
);
28702877

‎apps/web/src/browser/HostedBrowserWebview.test.tsx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ describe("HostedBrowserWebview settings hydration", () => {
147147
});
148148
await send("other-guest", true, 42);
149149
expect(useBrowserSurfaceStore.getState().activityByTabId[runtimeTabId]).toBeUndefined();
150+
let releaseOtherActivity: () => void = () => undefined;
151+
await act(() => {
152+
releaseOtherActivity = useBrowserSurfaceStore.getState().acquireActivity(runtimeTabId);
153+
});
154+
await send("cancelled", true);
155+
expect(useBrowserSurfaceStore.getState().activityByTabId[runtimeTabId]).toBe(2);
156+
await act(() => {
157+
for (const frame of frames.splice(0)) frame(1);
158+
});
159+
await send("cancelled", false);
160+
await act(() => {
161+
for (const frame of frames.splice(0)) frame(2);
162+
});
163+
expect(acknowledgeCapture).not.toHaveBeenCalled();
164+
// Cancelling a screenshot must preserve another consumer's activity lease.
165+
expect(useBrowserSurfaceStore.getState().activityByTabId[runtimeTabId]).toBe(1);
166+
await act(releaseOtherActivity);
150167
await send("first", true);
151168
await send("second", true);
152169
await send("first", true); // Duplicate IPC delivery must not leak a lease.

0 commit comments

Comments
 (0)