Skip to content

Commit d092a33

Browse files
Merge remote-tracking branch 'origin/main' into test/live-to-testclock
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2 parents 6d16375 + b7a224c commit d092a33

154 files changed

Lines changed: 5175 additions & 908 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const makeTarget = (
1616
title: () => "Fixture",
1717
userAgent: () => "Electron",
1818
setDownloadDirectory,
19+
receivesScreencast: () => true,
1920
});
2021

2122
const makeRelay = (target: CdpRelayTarget, write: (raw: string) => void) =>

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface CdpRelayTarget {
99
owner?: string,
1010
) => Promise<unknown>;
1111
readonly sessionDetached?: (owner: string) => void;
12+
/** Whether a page session, by owner as `send` names it, gets the page's screencast frames. */
13+
readonly receivesScreencast: (owner: string | undefined) => boolean;
1214
/** The page's own CDP target id; Playwright keys the main frame by it. */
1315
readonly targetId: () => Promise<string>;
1416
readonly url: () => string;
@@ -260,8 +262,15 @@ export function createCdpRelayConnection(
260262
send({ method, params, sessionId });
261263
return;
262264
}
263-
for (const [id, kind] of sessions)
264-
if (kind === "page") send({ method, params, sessionId: id });
265+
for (const [id, kind] of sessions) {
266+
if (kind !== "page") continue;
267+
if (
268+
method === "Page.screencastFrame" &&
269+
!target.receivesScreencast(id === PAGE_SESSION_ID ? undefined : id)
270+
)
271+
continue;
272+
send({ method, params, sessionId: id });
273+
}
265274
},
266275
};
267276
}

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

Lines changed: 199 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
1616
const decodeCdpReply = Schema.decodeUnknownSync(
1717
Schema.fromJsonString(Schema.Struct({ id: Schema.Number })),
1818
);
19+
const decodeCdpMessage = Schema.decodeUnknownSync(
20+
Schema.fromJsonString(
21+
Schema.Struct({
22+
id: Schema.optional(Schema.Number),
23+
method: Schema.optional(Schema.String),
24+
sessionId: Schema.optional(Schema.String),
25+
}),
26+
),
27+
);
1928
const key = { threadId: "thread-1", tabId: "tab-1" };
2029
/** Sends a page command whose id the fake debugger records as it receives it. */
2130
const command = (
@@ -43,6 +52,8 @@ const makeDebuggee = (
4352
const pending: Array<() => void> = [];
4453
const focusChanges: Array<() => void> = [];
4554
const sent: Array<unknown> = [];
55+
const methods: Array<string> = [];
56+
const rejects: Array<() => void> = [];
4657
const sendWaiters: Array<() => void> = [];
4758
const focusEmulation: Array<boolean> = [];
4859
const debuggee = Object.assign(emitter, {
@@ -67,6 +78,8 @@ const makeDebuggee = (
6778
return;
6879
}
6980
sent.push(params?.id);
81+
methods.push(method);
82+
rejects.push(() => reject(new Error("Refused")));
7083
pending.push(() => resolve({ method }));
7184
sendWaiters.shift()?.();
7285
}),
@@ -94,6 +107,10 @@ const makeDebuggee = (
94107
webContents.emit("did-fail-load", {}, code, "Failed", "http://localhost/", true),
95108
/** Ids of the commands the debugger received, in order. */
96109
sent,
110+
/** Methods of the same commands, in order. */
111+
methods,
112+
/** Refuses the pending command the debugger received with this id. */
113+
refuse: (id: number) => rejects[sent.indexOf(id)]?.(),
97114
focusEmulation,
98115
nextSend: () => new Promise<void>((resolve) => sendWaiters.push(resolve)),
99116
release: () => pending.splice(0).forEach((resolve) => resolve()),
@@ -116,16 +133,24 @@ const takeEvents = (host: DesktopBrowserHost.DesktopBrowserHost["Service"], coun
116133
/**
117134
* Ids of the replies the host sends, in order. A screenshot's reply leaves after its turn
118135
* has passed on, so by then the next waiting screenshot, if any, has been sent. Subscribe
119-
* before sending commands: a new subscriber releases the tab's relay.
136+
* before sending commands: a new subscriber releases the tab's relay. Screencast frames
137+
* go to `frames` by the session they were sent to.
120138
*/
121-
const watchReplies = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =>
139+
const watchReplies = (
140+
host: DesktopBrowserHost.DesktopBrowserHost["Service"],
141+
frames: Array<string | undefined> = [],
142+
) =>
122143
Effect.gen(function* () {
123144
const replies = yield* Queue.unbounded<number>();
124145
yield* host.events.pipe(
125146
Stream.map((line) => decodeEvent(new TextDecoder().decode(line))),
126-
Stream.runForEach((event) =>
127-
event.type === "cdp" ? Queue.offer(replies, decodeCdpReply(event.message).id) : Effect.void,
128-
),
147+
Stream.runForEach((event) => {
148+
if (event.type !== "cdp") return Effect.void;
149+
const { id, method, sessionId } = decodeCdpMessage(event.message);
150+
if (id !== undefined) return Queue.offer(replies, id);
151+
if (method === "Page.screencastFrame") frames.push(sessionId);
152+
return Effect.void;
153+
}),
129154
Effect.forkScoped,
130155
);
131156
yield* Effect.yieldNow;
@@ -1040,6 +1065,175 @@ describe("DesktopBrowserHost", () => {
10401065
}).pipe(Effect.scoped),
10411066
);
10421067

1068+
it.effect("keeps a guest's one screencast running until its last streaming session stops", () =>
1069+
Effect.gen(function* () {
1070+
const host = yield* DesktopBrowserHost.make;
1071+
const debuggee = makeDebuggee();
1072+
host.attach(key, debuggee.tab);
1073+
const frames: Array<string | undefined> = [];
1074+
const replies = yield* watchReplies(host, frames);
1075+
const raw = (message: Record<string, unknown>) =>
1076+
host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
1077+
const nativeStops = () =>
1078+
debuggee.methods.filter((method) => method === "Page.stopScreencast").length;
1079+
1080+
// A viewer and a recording share Chromium's single screencast for the guest.
1081+
yield* raw({ id: 0, method: "Target.setAutoAttach", params: { autoAttach: true } });
1082+
expect(yield* Queue.take(replies)).toBe(0);
1083+
yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1084+
yield* raw({ id: 2, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1085+
expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([1, 2]);
1086+
yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
1087+
yield* raw({ id: 4, method: "Page.startScreencast", sessionId: "t3-preview-page-2" });
1088+
yield* settle;
1089+
debuggee.release();
1090+
yield* raw({ id: 5, method: "Page.stopScreencast", sessionId: "t3-preview-page-1" });
1091+
yield* settle;
1092+
expect(nativeStops(), "stopped the screencast another session still streams").toBe(0);
1093+
debuggee.emit("Page.screencastFrame", { data: "frame", sessionId: 1 });
1094+
yield* settle;
1095+
expect(frames, "sent a stopped or idle session the screencast's frames").toEqual([
1096+
"t3-preview-page-2",
1097+
]);
1098+
1099+
yield* raw({ id: 6, method: "Page.stopScreencast", sessionId: "t3-preview-page-2" });
1100+
yield* settle;
1101+
expect(nativeStops()).toBe(1);
1102+
}).pipe(Effect.scoped),
1103+
);
1104+
1105+
it.effect("keeps a session's screencast when another of its starts is refused", () =>
1106+
Effect.gen(function* () {
1107+
const host = yield* DesktopBrowserHost.make;
1108+
const debuggee = makeDebuggee();
1109+
host.attach(key, debuggee.tab);
1110+
const frames: Array<string | undefined> = [];
1111+
const replies = yield* watchReplies(host, frames);
1112+
const raw = (message: Record<string, unknown>) =>
1113+
host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
1114+
1115+
yield* raw({ id: 0, method: "Target.setAutoAttach", params: { autoAttach: true } });
1116+
expect(yield* Queue.take(replies)).toBe(0);
1117+
yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1118+
expect(yield* Queue.take(replies)).toBe(1);
1119+
yield* raw({
1120+
id: 2,
1121+
method: "Page.startScreencast",
1122+
params: { id: 2 },
1123+
sessionId: "t3-preview-page-1",
1124+
});
1125+
yield* raw({
1126+
id: 3,
1127+
method: "Page.startScreencast",
1128+
params: { id: 3 },
1129+
sessionId: "t3-preview-page-1",
1130+
});
1131+
yield* settle;
1132+
debuggee.refuse(2);
1133+
debuggee.release();
1134+
expect([yield* Queue.take(replies), yield* Queue.take(replies)].toSorted()).toEqual([2, 3]);
1135+
yield* settle;
1136+
expect(debuggee.methods).not.toContain("Page.stopScreencast");
1137+
debuggee.emit("Page.screencastFrame", { data: "frame", sessionId: 1 });
1138+
yield* settle;
1139+
expect(frames).toContain("t3-preview-page-1");
1140+
1141+
// A refused replacement leaves the session's running stream, shared with another session.
1142+
yield* raw({ id: 4, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1143+
expect(yield* Queue.take(replies)).toBe(4);
1144+
yield* raw({
1145+
id: 5,
1146+
method: "Page.startScreencast",
1147+
params: { id: 5 },
1148+
sessionId: "t3-preview-page-2",
1149+
});
1150+
yield* raw({
1151+
id: 6,
1152+
method: "Page.startScreencast",
1153+
params: { id: 6 },
1154+
sessionId: "t3-preview-page-1",
1155+
});
1156+
yield* settle;
1157+
debuggee.refuse(6);
1158+
debuggee.release();
1159+
expect([yield* Queue.take(replies), yield* Queue.take(replies)].toSorted()).toEqual([5, 6]);
1160+
yield* settle;
1161+
frames.length = 0;
1162+
debuggee.emit("Page.screencastFrame", { data: "frame", sessionId: 2 });
1163+
yield* settle;
1164+
expect(frames).toEqual(["t3-preview-page-1", "t3-preview-page-2"]);
1165+
}).pipe(Effect.scoped),
1166+
);
1167+
1168+
it.effect("stops a guest's screencast once its last streaming session detaches", () =>
1169+
Effect.gen(function* () {
1170+
const host = yield* DesktopBrowserHost.make;
1171+
const debuggee = makeDebuggee();
1172+
host.attach(key, debuggee.tab);
1173+
const replies = yield* watchReplies(host);
1174+
const raw = (message: Record<string, unknown>) =>
1175+
host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
1176+
const nativeStops = () =>
1177+
debuggee.methods.filter((method) => method === "Page.stopScreencast").length;
1178+
1179+
yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1180+
yield* raw({ id: 2, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1181+
expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([1, 2]);
1182+
yield* raw({ id: 3, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
1183+
yield* raw({ id: 4, method: "Page.startScreencast", sessionId: "t3-preview-page-2" });
1184+
yield* settle;
1185+
debuggee.release();
1186+
// Streamers go without stopping, such as viewers that closed.
1187+
yield* raw({
1188+
id: 5,
1189+
method: "Target.detachFromTarget",
1190+
params: { sessionId: "t3-preview-page-1" },
1191+
});
1192+
yield* settle;
1193+
expect(nativeStops(), "stopped the screencast another session still streams").toBe(0);
1194+
yield* raw({
1195+
id: 6,
1196+
method: "Target.detachFromTarget",
1197+
params: { sessionId: "t3-preview-page-2" },
1198+
});
1199+
const answered = [];
1200+
for (let reply = 0; reply < 4; reply++) answered.push(yield* Queue.take(replies));
1201+
expect(answered.toSorted()).toEqual([3, 4, 5, 6]);
1202+
yield* settle;
1203+
expect(nativeStops()).toBe(1);
1204+
}).pipe(Effect.scoped),
1205+
);
1206+
1207+
it.effect("stops a screencast whose start lands after its session detached", () =>
1208+
Effect.gen(function* () {
1209+
const host = yield* DesktopBrowserHost.make;
1210+
const debuggee = makeDebuggee({ deferFocus: true });
1211+
host.attach(key, debuggee.tab);
1212+
const replies = yield* watchReplies(host);
1213+
const raw = (message: Record<string, unknown>) =>
1214+
host.handleCommandLine(encodeJson({ type: "cdp", ...key, message: encodeJson(message) }));
1215+
1216+
yield* raw({ id: 1, method: "Target.attachToTarget", params: { targetId: "GUEST" } });
1217+
expect(yield* Queue.take(replies)).toBe(1);
1218+
// The start waits on focus emulation while the viewer closes.
1219+
yield* raw({ id: 2, method: "Page.startScreencast", sessionId: "t3-preview-page-1" });
1220+
yield* raw({
1221+
id: 3,
1222+
method: "Target.detachFromTarget",
1223+
params: { sessionId: "t3-preview-page-1" },
1224+
});
1225+
expect(yield* Queue.take(replies)).toBe(3);
1226+
debuggee.settleFocus();
1227+
yield* settle;
1228+
debuggee.release();
1229+
expect(yield* Queue.take(replies)).toBe(2);
1230+
yield* settle;
1231+
expect(debuggee.methods.at(-1), "left the late screencast running").toBe(
1232+
"Page.stopScreencast",
1233+
);
1234+
}).pipe(Effect.scoped),
1235+
);
1236+
10431237
it.effect("saves a server tab's download under its CDP guid where the server asked", () =>
10441238
Effect.gen(function* () {
10451239
const host = yield* DesktopBrowserHost.make;

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

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ interface AttachedTab {
7575
*/
7676
agentUsed: boolean;
7777
focusReleasing: boolean;
78-
/** The sessions streaming the guest; one stopping leaves the others' screencasts running. */
79-
readonly screencasts: Set<string | undefined>;
78+
/** The sessions sharing the guest's one Chromium screencast; it stops with the last of them. */
79+
readonly screencasts: Map<string | undefined, { started: boolean; pending: number }>;
8080
readonly onMessage: (
8181
event: Electron.Event,
8282
method: string,
@@ -289,9 +289,15 @@ export const make = Effect.gen(function* () {
289289
dropFocusHolds(tab);
290290
};
291291

292+
/** An owner that leaves without stopping would otherwise keep the screencast running unseen. */
293+
const leaveScreencast = (tab: AttachedTab, owner: string | undefined) => {
294+
if (!tab.screencasts.delete(owner) || tab.screencasts.size > 0) return;
295+
void tab.debuggee.debugger.sendCommand("Page.stopScreencast").catch(() => {});
296+
};
297+
292298
/** A detached session, such as a closed viewer, waits on nothing it sent. */
293299
const dropSessionHolds = (tab: AttachedTab, owner: string) => {
294-
tab.screencasts.delete(owner);
300+
leaveScreencast(tab, owner);
295301
for (const [pending, holder] of tab.focusPending) {
296302
if (holder === owner) tab.focusPending.delete(pending);
297303
}
@@ -330,13 +336,19 @@ export const make = Effect.gen(function* () {
330336
{
331337
send: (method, params, sessionId, owner) => {
332338
// A child session's screencast is not the page's, such as a late one on a detached session.
333-
if (sessionId === undefined && method === "Page.startScreencast") {
334-
tab.screencasts.add(owner);
339+
const stream =
340+
sessionId === undefined && method === "Page.startScreencast"
341+
? (tab.screencasts.get(owner) ?? { started: false, pending: 0 })
342+
: undefined;
343+
if (stream) {
344+
stream.pending += 1;
345+
tab.screencasts.set(owner, stream);
335346
if (owner !== undefined) streamingOwners.add(owner);
336347
syncPainting(tab);
337348
} else if (sessionId === undefined && method === "Page.stopScreencast") {
338349
tab.screencasts.delete(owner);
339350
syncPainting(tab);
351+
if (tab.screencasts.size > 0) return Promise.resolve({});
340352
}
341353
// A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
342354
if (
@@ -365,19 +377,42 @@ export const make = Effect.gen(function* () {
365377
held ? holdFocus(tab, owner, () => forward(current)) : forward(current),
366378
)
367379
: method === "Page.startScreencast"
368-
? holdFocus(tab, owner, forward).catch((cause: unknown) => {
369-
// A viewer can outlive a refused stream; nothing is streaming for it.
370-
if (sessionId === undefined && tab.relay === relay) tab.screencasts.delete(owner);
371-
releaseFocusWhenIdle(tab);
372-
syncPainting(tab);
373-
throw cause;
374-
})
380+
? holdFocus(tab, owner, forward).then(
381+
(result) => {
382+
if (stream) {
383+
stream.pending -= 1;
384+
stream.started = true;
385+
// A detach's stop can overtake a start still waiting on focus; nobody owns it now.
386+
if (tab.screencasts.get(owner) !== stream && tab.screencasts.size === 0) {
387+
void debuggee.sendCommand("Page.stopScreencast").catch(() => {});
388+
}
389+
}
390+
return result;
391+
},
392+
(cause: unknown) => {
393+
if (stream) stream.pending -= 1;
394+
// A viewer can outlive a refused start; its running or pending stream still holds it.
395+
if (
396+
stream &&
397+
!stream.started &&
398+
stream.pending === 0 &&
399+
tab.relay === relay &&
400+
tab.screencasts.get(owner) === stream
401+
) {
402+
leaveScreencast(tab, owner);
403+
}
404+
releaseFocusWhenIdle(tab);
405+
syncPainting(tab);
406+
throw cause;
407+
},
408+
)
375409
: holdFocus(tab, owner, forward);
376410
},
377411
sessionDetached: (owner) => {
378412
detachedOwners.add(owner);
379413
dropSessionHolds(tab, owner);
380414
},
415+
receivesScreencast: (owner) => tab.screencasts.has(owner),
381416
targetId: () =>
382417
debuggee
383418
.sendCommand("Target.getTargetInfo")
@@ -463,7 +498,7 @@ export const make = Effect.gen(function* () {
463498
painting: false,
464499
agentUsed: false,
465500
focusReleasing: false,
466-
screencasts: new Set(),
501+
screencasts: new Map(),
467502
onMessage: (_event, method, params, sessionId) => {
468503
if (method === "Page.downloadWillBegin") {
469504
const guid = (params as { guid?: unknown } | undefined)?.guid;

0 commit comments

Comments
 (0)