Skip to content

Commit 7fed734

Browse files
committed
fix(desktop): preserve capture errors and compose activity effects
1 parent 05dd391 commit 7fed734

5 files changed

Lines changed: 159 additions & 116 deletions

File tree

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ const makeGuest = (id = 41) => {
4444
nativeStarted,
4545
screenshotStarted,
4646
tab: {
47-
withCaptureActivity: (capture: () => Promise<unknown>) => capture(),
47+
withCaptureActivity: <A, E>(capture: Effect.Effect<A, E>) => capture,
4848
webContents: {
4949
id,
5050
capturePage,
51-
isDestroyed: () => false,
51+
isDestroyed: vi.fn(() => false),
5252
getURL: () => "http://localhost/",
5353
getTitle: () => "Fixture",
5454
getUserAgent: () => "Electron",
@@ -141,6 +141,45 @@ describe("desktop agent screenshot rendering lease", () => {
141141
}).pipe(Effect.scoped),
142142
);
143143

144+
it.effect("reports a destroyed guest with one capture failure prefix", () =>
145+
Effect.gen(function* () {
146+
const { guest, leases, replies, command, acknowledge } = yield* setup;
147+
yield* command(1);
148+
vi.mocked(guest.tab.webContents.isDestroyed).mockReturnValue(true);
149+
const active = yield* acknowledge;
150+
expect((yield* Queue.take(replies)).error?.message).toBe(
151+
"Desktop browser screenshot failed: the browser tab changed during capture.",
152+
);
153+
expect(yield* Queue.take(leases)).toEqual({ ...active, active: false });
154+
expect(guest.capturePage).not.toHaveBeenCalled();
155+
expect(guest.screenshot).not.toHaveBeenCalled();
156+
}).pipe(Effect.scoped),
157+
);
158+
159+
it.effect.each(["native", "cdp"] as const)(
160+
"reports a stable capture error when %s fails and allows a retry",
161+
(failedStage) =>
162+
Effect.gen(function* () {
163+
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);
170+
yield* command(1);
171+
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 }));
176+
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" } });
180+
}).pipe(Effect.scoped),
181+
);
182+
144183
it.effect.each(["native", "cdp"] as const)(
145184
"bounds %s waiting without accumulating captures or blocking DOM commands",
146185
(stalledStage) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const makeDebuggee = () => {
3737
};
3838
return {
3939
tab: {
40-
withCaptureActivity: (capture: () => Promise<unknown>) => capture(),
40+
withCaptureActivity: <A, E>(capture: Effect.Effect<A, E>) => capture,
4141
webContents: webContents as unknown as Electron.WebContents,
4242
debugger: debuggee as unknown as Electron.Debugger,
4343
},

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

Lines changed: 96 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as Schema from "effect/Schema";
2727
import * as Stream from "effect/Stream";
2828

2929
import { createCdpRelayConnection, type CdpRelayConnection } from "./CdpRelay.ts";
30+
import type { PreviewManagerError } from "./Manager.ts";
3031

3132
const encodeEvent = Schema.encodeSync(Schema.fromJsonString(DesktopBrowserEvent));
3233
const decodeCommand = Schema.decodeUnknownOption(Schema.fromJsonString(DesktopBrowserCommand));
@@ -42,21 +43,22 @@ export interface DesktopBrowserTabDebugger {
4243
readonly webContents: Electron.WebContents;
4344
readonly debugger: Electron.Debugger;
4445
/** Shares the manager's recording/PiP throttling lease while preparing a still frame. */
45-
readonly withCaptureActivity: (
46-
capture: () => Promise<unknown>,
47-
signal: AbortSignal,
48-
) => Promise<unknown>;
46+
readonly withCaptureActivity: <A, E>(
47+
capture: Effect.Effect<A, E>,
48+
) => Effect.Effect<A, E | PreviewManagerError>;
4949
}
5050

5151
class DesktopBrowserCaptureError extends Schema.TaggedError<DesktopBrowserCaptureError>()(
5252
"DesktopBrowserCaptureError",
53-
{ reason: Schema.String },
53+
{ reason: Schema.String, cause: Schema.optional(Schema.Defect()) },
5454
) {
5555
override get message(): string {
5656
return `Desktop browser screenshot failed: ${this.reason}`;
5757
}
5858
}
5959

60+
const isDesktopBrowserCaptureError = Schema.is(DesktopBrowserCaptureError);
61+
6062
const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`;
6163

6264
interface AttachedTab {
@@ -140,105 +142,102 @@ export const make = Effect.gen(function* () {
140142
}
141143
};
142144
try {
143-
return await tab.debuggee.withCaptureActivity(
144-
() =>
145-
runPromise(
146-
Effect.acquireUseRelease(
145+
return await runPromise(
146+
tab.debuggee.withCaptureActivity(
147+
Effect.acquireUseRelease(
148+
Effect.gen(function* () {
149+
const ready = yield* Deferred.make<void>();
150+
const requestId = NodeCrypto.randomUUID();
151+
pendingCaptures.set(requestId, ready);
152+
return { requestId, ready, webContentsId: webContents.id };
153+
}),
154+
({ requestId, ready, webContentsId }) =>
147155
Effect.gen(function* () {
148-
const ready = yield* Deferred.make<void>();
149-
const requestId = NodeCrypto.randomUUID();
150-
pendingCaptures.set(requestId, ready);
151-
return { requestId, ready, webContentsId: webContents.id };
152-
}),
153-
({ requestId, ready, webContentsId }) =>
154-
Effect.gen(function* () {
155-
yield* PubSub.publish(captureRequests, {
156-
requestId,
157-
webContentsId,
158-
active: true,
159-
});
160-
yield* Deferred.await(ready).pipe(
161-
Effect.timeoutOrElse({
162-
duration: "2 seconds",
163-
orElse: () =>
164-
Effect.fail(
165-
new DesktopBrowserCaptureError({
166-
reason:
167-
"the browser surface did not become paintable within 2 seconds.",
168-
}),
169-
),
170-
}),
171-
);
172-
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;
156+
yield* PubSub.publish(captureRequests, {
157+
requestId,
158+
webContentsId,
159+
active: true,
160+
});
161+
yield* Deferred.await(ready).pipe(
162+
Effect.timeoutOrElse({
163+
duration: "2 seconds",
164+
orElse: () =>
165+
Effect.fail(
166+
new DesktopBrowserCaptureError({
167+
reason: "the browser surface did not become paintable within 2 seconds.",
168+
}),
169+
),
170+
}),
171+
);
172+
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;
184+
});
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 () => {
190+
await webContents.capturePage(undefined, {
191+
stayHidden: true,
192+
stayAwake: false,
184193
});
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 () => {
194+
requireCurrent(signal);
195+
if (!screenshotSettled) {
190196
await webContents.capturePage(undefined, {
191197
stayHidden: true,
192198
stayAwake: false,
193199
});
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-
catch: (cause) =>
211-
new DesktopBrowserCaptureError({
212-
reason:
213-
cause instanceof Error
214-
? cause.message
215-
: "the compositor could not capture a frame.",
216-
}),
217-
}).pipe(
218-
Effect.timeoutOrElse({
219-
duration: "8 seconds",
220-
orElse: () =>
221-
Effect.fail(
222-
new DesktopBrowserCaptureError({
223-
reason: "the compositor did not supply a screenshot within 8 seconds.",
224-
}),
225-
),
226-
}),
227-
);
228-
}),
229-
({ requestId, webContentsId }) =>
230-
Effect.gen(function* () {
231-
pendingCaptures.delete(requestId);
232-
yield* PubSub.publish(captureRequests, {
233-
requestId,
234-
webContentsId,
235-
active: false,
236-
});
237-
}),
238-
),
239-
{ signal: relaySignal },
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+
catch: (cause) =>
211+
isDesktopBrowserCaptureError(cause)
212+
? cause
213+
: new DesktopBrowserCaptureError({
214+
reason: "the compositor could not capture a frame.",
215+
cause,
216+
}),
217+
}).pipe(
218+
Effect.timeoutOrElse({
219+
duration: "8 seconds",
220+
orElse: () =>
221+
Effect.fail(
222+
new DesktopBrowserCaptureError({
223+
reason: "the compositor did not supply a screenshot within 8 seconds.",
224+
}),
225+
),
226+
}),
227+
);
228+
}),
229+
({ requestId, webContentsId }) =>
230+
Effect.gen(function* () {
231+
pendingCaptures.delete(requestId);
232+
yield* PubSub.publish(captureRequests, {
233+
requestId,
234+
webContentsId,
235+
active: false,
236+
});
237+
}),
240238
),
241-
relaySignal,
239+
),
240+
{ signal: relaySignal },
242241
);
243242
} finally {
244243
const release = () => {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,9 +2791,9 @@ describe("PreviewManager", () => {
27912791
),
27922792
);
27932793

2794-
effectIt.effect(
2795-
"shares agent screenshot throttling with a recording and restores it after timeout",
2796-
() =>
2794+
effectIt.effect.each(["timeout", "release"] as const)(
2795+
"shares agent screenshot throttling with a recording and restores it after %s",
2796+
(endCapture) =>
27972797
Effect.gen(function* () {
27982798
const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost;
27992799
const manager = yield* PreviewManager.PreviewManager;
@@ -2824,7 +2824,10 @@ describe("PreviewManager", () => {
28242824
yield* manager.createTab("capture", { serverTab });
28252825
yield* manager.registerWebview("capture", 42);
28262826
expect(yield* Queue.take(lines)).toContain('"attached"');
2827-
const setBackgroundThrottling = vi.fn();
2827+
const restored = Promise.withResolvers<void>();
2828+
const setBackgroundThrottling = vi.fn((enabled: boolean) => {
2829+
if (enabled) restored.resolve();
2830+
});
28282831
yield* manager.setMainWindow({
28292832
isDestroyed: () => false,
28302833
once: vi.fn(),
@@ -2852,8 +2855,13 @@ describe("PreviewManager", () => {
28522855
yield* manager.stopRecording("capture");
28532856
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
28542857
expect(contents.setBackgroundThrottling.mock.calls).toEqual([[false]]);
2855-
yield* TestClock.adjust("8 seconds");
2856-
expect(yield* Queue.take(lines)).toContain("within 8 seconds");
2858+
if (endCapture === "timeout") {
2859+
yield* TestClock.adjust("8 seconds");
2860+
expect(yield* Queue.take(lines)).toContain("within 8 seconds");
2861+
} else {
2862+
yield* browserHost.handleCommandLine(JSON.stringify({ type: "release", ...serverTab }));
2863+
}
2864+
yield* Effect.promise(() => restored.promise);
28572865
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
28582866
expect(contents.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
28592867
pending.resolve(image);

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
556556
const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost;
557557
const context = yield* Effect.context<never>();
558558
const runFork = Effect.runForkWith(context);
559-
const runPromise = Effect.runPromiseWith(context);
560559
const resolvedArtifactDirectory = path.resolve(artifactDirectory);
561560

562561
let forwardedShortcuts: ReadonlyArray<PreviewForwardedShortcut> = [];
@@ -2381,17 +2380,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23812380
browserHost.attach(afterAttach.serverTab, {
23822381
webContents: wc,
23832382
debugger: control.debugger,
2384-
withCaptureActivity: (capture, signal) => {
2385-
const consumer = Symbol("agent-screenshot");
2386-
return runPromise(
2387-
Effect.acquireUseRelease(
2383+
withCaptureActivity: (capture) =>
2384+
Effect.suspend(() => {
2385+
const consumer = Symbol("agent-screenshot");
2386+
return Effect.acquireUseRelease(
23882387
startFrameCapture(tabId, consumer),
2389-
() => Effect.promise((_signal) => capture()),
2388+
() => capture,
23902389
() => stopFrameCapture(tabId, consumer),
2391-
),
2392-
{ signal },
2393-
);
2394-
},
2390+
);
2391+
}),
23952392
});
23962393
}
23972394
if (afterAttach.colorScheme !== "system") {

0 commit comments

Comments
 (0)