The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(desktop): one preview viewer or recording stopping no longer freezes the others (#31051)
Julius Marminge committed
Nov 12, 2026 at 19:38 UTC
be03b93bac9b5f21ee1210e6e6f9faf5a551361a
4 files changed
+259
-20
apps/desktop/src/preview/CdpRelay.test.ts
+1
@@ -16,6 +16,7 @@ const makeTarget = (
16
title: () => "Fixture",
17
userAgent: () => "Electron",
18
setDownloadDirectory,
19
+ receivesScreencast: () => true,
20
});
21
22
const makeRelay = (target: CdpRelayTarget, write: (raw: string) => void) =>
apps/desktop/src/preview/CdpRelay.ts
+11
-2
@@ -9,6 +9,8 @@ export interface CdpRelayTarget {
9
owner?: string,
10
) => Promise<unknown>;
11
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;
14
/** The page's own CDP target id; Playwright keys the main frame by it. */
15
readonly targetId: () => Promise<string>;
16
readonly url: () => string;
@@ -260,8 +262,15 @@ export function createCdpRelayConnection(
262
send({ method, params, sessionId });
263
return;
264
}
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
+ }
274
},
275
};
276
}
apps/desktop/src/preview/DesktopBrowserHost.test.ts
+199
-5
@@ -16,6 +16,15 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
16
const decodeCdpReply = Schema.decodeUnknownSync(
17
Schema.fromJsonString(Schema.Struct({ id: Schema.Number })),
18
);
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
+);
28
const key = { threadId: "thread-1", tabId: "tab-1" };
29
/** Sends a page command whose id the fake debugger records as it receives it. */
30
const command = (
@@ -43,6 +52,8 @@ const makeDebuggee = (
52
const pending: Array<() => void> = [];
53
const focusChanges: Array<() => void> = [];
54
const sent: Array<unknown> = [];
55
+ const methods: Array<string> = [];
56
+ const rejects: Array<() => void> = [];
57
const sendWaiters: Array<() => void> = [];
58
const focusEmulation: Array<boolean> = [];
59
const debuggee = Object.assign(emitter, {
@@ -67,6 +78,8 @@ const makeDebuggee = (
78
return;
79
}
80
sent.push(params?.id);
81
+ methods.push(method);
82
+ rejects.push(() => reject(new Error("Refused")));
83
pending.push(() => resolve({ method }));
84
sendWaiters.shift()?.();
85
}),
@@ -94,6 +107,10 @@ const makeDebuggee = (
107
webContents.emit("did-fail-load", {}, code, "Failed", "http://localhost/", true),
108
/** Ids of the commands the debugger received, in order. */
109
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)]?.(),
114
focusEmulation,
115
nextSend: () => new Promise<void>((resolve) => sendWaiters.push(resolve)),
116
release: () => pending.splice(0).forEach((resolve) => resolve()),
@@ -116,16 +133,24 @@ const takeEvents = (host: DesktopBrowserHost.DesktopBrowserHost["Service"], coun
133
/**
134
* Ids of the replies the host sends, in order. A screenshot's reply leaves after its turn
135
* 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.
138
*/
121
-const watchReplies = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =>
139
+const watchReplies = (
140
+ host: DesktopBrowserHost.DesktopBrowserHost["Service"],
141
+ frames: Array<string | undefined> = [],
142
+) =>
143
Effect.gen(function* () {
144
const replies = yield* Queue.unbounded<number>();
145
yield* host.events.pipe(
146
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
+ }),
154
Effect.forkScoped,
155
);
156
yield* Effect.yieldNow;
@@ -1040,6 +1065,175 @@ describe("DesktopBrowserHost", () => {
1065
}).pipe(Effect.scoped),
1066
);
1067
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
+
1237
it.effect("saves a server tab's download under its CDP guid where the server asked", () =>
1238
Effect.gen(function* () {
1239
const host = yield* DesktopBrowserHost.make;
apps/desktop/src/preview/DesktopBrowserHost.ts
+48
-13
@@ -75,8 +75,8 @@ interface AttachedTab {
75
*/
76
agentUsed: boolean;
77
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 }>;
80
readonly onMessage: (
81
event: Electron.Event,
82
method: string,
@@ -289,9 +289,15 @@ export const make = Effect.gen(function* () {
289
dropFocusHolds(tab);
290
};
291
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
+
298
/** A detached session, such as a closed viewer, waits on nothing it sent. */
299
const dropSessionHolds = (tab: AttachedTab, owner: string) => {
294
- tab.screencasts.delete(owner);
300
+ leaveScreencast(tab, owner);
301
for (const [pending, holder] of tab.focusPending) {
302
if (holder === owner) tab.focusPending.delete(pending);
303
}
@@ -330,13 +336,19 @@ export const make = Effect.gen(function* () {
336
{
337
send: (method, params, sessionId, owner) => {
338
// 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);
346
if (owner !== undefined) streamingOwners.add(owner);
347
syncPainting(tab);
348
} else if (sessionId === undefined && method === "Page.stopScreencast") {
349
tab.screencasts.delete(owner);
350
syncPainting(tab);
351
+ if (tab.screencasts.size > 0) return Promise.resolve({});
352
}
353
// A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
354
if (
@@ -365,19 +377,42 @@ export const make = Effect.gen(function* () {
377
held ? holdFocus(tab, owner, () => forward(current)) : forward(current),
378
)
379
: 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
+ )
409
: holdFocus(tab, owner, forward);
410
},
411
sessionDetached: (owner) => {
412
detachedOwners.add(owner);
413
dropSessionHolds(tab, owner);
414
},
415
+ receivesScreencast: (owner) => tab.screencasts.has(owner),
416
targetId: () =>
417
debuggee
418
.sendCommand("Target.getTargetInfo")
@@ -463,7 +498,7 @@ export const make = Effect.gen(function* () {
498
painting: false,
499
agentUsed: false,
500
focusReleasing: false,
466
- screencasts: new Set(),
501
+ screencasts: new Map(),
502
onMessage: (_event, method, params, sessionId) => {
503
if (method === "Page.downloadWillBegin") {
504
const guid = (params as { guid?: unknown } | undefined)?.guid;