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, 22:15 UTC
fix(desktop): a hidden preview tab no longer keeps the display awake after a screenshot (#25978)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 11, 2026 at 12:00 UTC
731c87334c75705ee85cd657957ee93c5b946ba8
2 files changed
+63
-43
apps/desktop/src/preview/DesktopBrowserHost.test.ts
+25
-14
@@ -607,20 +607,21 @@ describe("DesktopBrowserHost", () => {
607
params: { sessionId: "t3-preview-page-1" },
608
});
609
expect(yield* Queue.take(replies)).toBe(3);
610
- expect(painting).toEqual([true, false]);
610
+ expect(painting).toEqual([true]);
611
612
// The agent's screenshot waits its turn; painting lets the abandoned one finish first.
613
yield* command(host, 4, "Page.captureScreenshot");
614
yield* settle;
615
- expect(painting).toEqual([true, false, true]);
615
+ expect(painting).toEqual([true]);
616
debuggee.release();
617
expect(yield* Queue.take(replies)).toBe(2);
618
yield* settle;
619
expect(debuggee.sent).toEqual([undefined, 4]);
620
+ expect(painting).toEqual([true]);
621
debuggee.release();
622
expect(yield* Queue.take(replies)).toBe(4);
623
yield* settle;
623
- expect(painting).toEqual([true, false, true, false]);
624
+ expect(painting).toEqual([true, false]);
625
}).pipe(Effect.scoped),
626
);
627
@@ -645,14 +646,19 @@ describe("DesktopBrowserHost", () => {
646
expect(yield* Queue.take(replies)).toBe(2);
647
yield* raw({ id: 3, method: "Page.captureScreenshot", sessionId: "t3-preview-page-1" });
648
yield* settle;
648
- expect(painting).toEqual([true, false, true]);
649
+ expect(painting).toEqual([true]);
650
yield* raw({
651
id: 4,
652
method: "Target.detachFromTarget",
653
params: { sessionId: "t3-preview-page-1" },
654
});
655
expect(yield* Queue.take(replies)).toBe(4);
655
- expect(painting).toEqual([true, false, true, false]);
656
+
657
+ // The agent's answers late; the viewer's is dropped unsent, so nothing is left to paint.
658
+ debuggee.release();
659
+ yield* settle;
660
+ expect(debuggee.sent).toEqual([1]);
661
+ expect(painting).toEqual([true, false]);
662
yield* TestClock.adjust("1 minute");
663
expect(debuggee.focusEmulation).toEqual([true, false]);
664
}).pipe(Effect.scoped),
@@ -682,7 +688,7 @@ describe("DesktopBrowserHost", () => {
688
}).pipe(Effect.scoped),
689
);
690
685
- it.effect("stops painting for a screenshot still queued when the agent's turn ends", () =>
691
+ it.effect("holds no focus for a screenshot still queued when the agent's turn ends", () =>
692
Effect.gen(function* () {
693
const host = yield* DesktopBrowserHost.make;
694
const debuggee = makeDebuggee();
@@ -697,15 +703,18 @@ describe("DesktopBrowserHost", () => {
703
yield* settle;
704
yield* turn(false);
705
yield* settle;
700
- expect(painting).toEqual([true, false]);
706
+ expect(painting).toEqual([true]);
707
702
- // The first answers late; the second still goes in turn, but holds nothing.
708
+ // The first answers late; the second still goes in turn, painting but holding no focus.
709
debuggee.releaseFirst();
710
yield* settle;
711
expect(debuggee.sent).toEqual([1, 2]);
706
- expect(painting).toEqual([true, false]);
712
yield* TestClock.adjust("1 minute");
713
expect(debuggee.focusEmulation).toEqual([true, false]);
714
+ expect(painting).toEqual([true]);
715
+ debuggee.release();
716
+ yield* settle;
717
+ expect(painting).toEqual([true, false]);
718
}).pipe(Effect.scoped),
719
);
720
@@ -952,7 +961,7 @@ describe("DesktopBrowserHost", () => {
961
}).pipe(Effect.scoped),
962
);
963
955
- it.effect("stops painting for a screenshot Chromium never answers", () =>
964
+ it.effect("stops painting for a screenshot Chromium never answers once its document goes", () =>
965
Effect.gen(function* () {
966
const host = yield* DesktopBrowserHost.make;
967
const debuggee = makeDebuggee();
@@ -961,32 +970,34 @@ describe("DesktopBrowserHost", () => {
970
const turn = (active: boolean) =>
971
host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));
972
964
- // The server gave up on it, and the agent's turn ends.
973
+ // The server gave up on it, and the agent's turn ends; Chromium still holds the capture.
974
yield* turn(true);
975
yield* command(host, 1, "Page.captureScreenshot");
976
yield* settle;
977
yield* turn(false);
978
yield* settle;
970
- expect(painting).toEqual([true, false]);
979
+ expect(painting).toEqual([true]);
980
981
// The page loads a new document, which also frees the screenshot turn.
982
debuggee.navigate();
983
+ expect(painting).toEqual([true, false]);
984
yield* command(host, 2, "Page.captureScreenshot");
985
yield* settle;
986
debuggee.navigate();
987
yield* settle;
988
expect(painting).toEqual([true, false, true, false]);
989
980
- // The server releases the relay.
990
+ // The server releases the relay; Chromium still holds the capture.
991
yield* command(host, 3, "Page.captureScreenshot");
992
yield* settle;
993
yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
994
yield* settle;
985
- expect(painting).toEqual([true, false, true, false, true, false]);
995
+ expect(painting).toEqual([true, false, true, false, true]);
996
997
// The guest detaches; a replacement starts with no screenshot pending.
998
const replacement = makeDebuggee();
999
host.attach(key, replacement.tab);
1000
+ expect(painting).toEqual([true, false, true, false, true, false]);
1001
yield* command(host, 4, "Page.captureScreenshot");
1002
yield* settle;
1003
host.detach(key);
apps/desktop/src/preview/DesktopBrowserHost.ts
+38
-29
@@ -49,7 +49,7 @@ interface AttachedTab {
49
readonly debuggee: DesktopBrowserTabDebugger;
50
relay: CdpRelayConnection | null;
51
screenshotTurn: object | null;
52
- /** Each holds focus and painting while `held` and whoever queued it still waits on it. */
52
+ /** Each keeps the guest painting while whoever queued it still waits, and focused while `held`. */
53
readonly waitingScreenshots: Array<{
54
readonly start: () => void;
55
readonly live: () => boolean;
@@ -62,10 +62,9 @@ interface AttachedTab {
62
focusChange: Promise<void> | null;
63
focusUses: number;
64
/** Each pending command, by the extra session waiting on it; the main connection's have none. */
65
- readonly focusPending: Map<
66
- object,
67
- { readonly owner: string | undefined; readonly paints: boolean }
68
- >;
65
+ readonly focusPending: Map<object, string | undefined>;
66
+ /** Screenshots Chromium was sent and has not answered; each keeps its wake lock until then. */
67
+ readonly capturing: Set<object>;
68
/** Whether the client keeps the guest painting, as published on `painting`. */
69
painting: boolean;
70
/**
@@ -150,7 +149,7 @@ export const make = Effect.gen(function* () {
149
tab: AttachedTab,
150
relay: CdpRelayConnection,
151
ownerLive: () => boolean,
153
- /** Sends only while `current` holds; holds focus and painting only while `held`. */
152
+ /** Sends only while `current` holds; holds focus only while `held`. */
153
capture: (current: () => boolean, held: boolean) => Promise<unknown>,
154
) =>
155
new Promise<unknown>((resolve, reject) => {
@@ -238,38 +237,42 @@ export const make = Effect.gen(function* () {
237
const active =
238
tabs.get(keyOf(tab.key)) === tab &&
239
(tab.screencasts.size > 0 ||
241
- waitingHolds(tab) ||
242
- [...tab.focusPending.values()].some((hold) => hold.paints));
240
+ tab.capturing.size > 0 ||
241
+ tab.waitingScreenshots.some((waiting) => waiting.live()));
242
if (tab.painting === active) return;
243
tab.painting = active;
244
runFork(PubSub.publish(painting, { key: tab.key, active }));
245
};
246
247
/** A command may wait on the page's next frame, so the guest stays focused until it settles. */
249
- const holdFocus = <A>(
250
- tab: AttachedTab,
251
- method: string,
252
- owner: string | undefined,
253
- work: () => Promise<A>,
254
- ) => {
248
+ const holdFocus = <A>(tab: AttachedTab, owner: string | undefined, work: () => Promise<A>) => {
249
const pending = {};
256
- tab.focusPending.set(pending, { owner, paints: method === "Page.captureScreenshot" });
250
+ tab.focusPending.set(pending, owner);
251
tab.focusUses += 1;
252
setFocusEmulation(tab, true);
253
releaseFocusWhenIdle(tab);
260
- syncPainting(tab);
254
return work().finally(() => {
255
if (!tab.focusPending.delete(pending)) return;
256
tab.focusUses += 1;
257
releaseFocusWhenIdle(tab);
258
+ });
259
+ };
260
+
261
+ /** Chromium answers a capture, and frees its wake lock, only once the guest paints a frame. */
262
+ const paintUntilAnswered = <A>(tab: AttachedTab, send: () => Promise<A>) => {
263
+ const sent = {};
264
+ tab.capturing.add(sent);
265
+ syncPainting(tab);
266
+ return send().finally(() => {
267
+ tab.capturing.delete(sent);
268
syncPainting(tab);
269
});
270
};
271
272
/**
273
* Chromium can leave a command unanswered, so the screenshot guard's release points
271
- * also end every hold: a released relay has no one waiting, and a new document answers
272
- * nothing pending from the old one. A released relay's screencast has no viewer to stop it.
274
+ * also end every focus hold: a released relay has no one waiting, and a new document
275
+ * answers nothing pending from the old one. A released relay's screencast has no viewer.
276
*/
277
const dropFocusHolds = (tab: AttachedTab) => {
278
tab.focusPending.clear();
@@ -287,8 +290,8 @@ export const make = Effect.gen(function* () {
290
/** A detached session, such as a closed viewer, waits on nothing it sent. */
291
const dropSessionHolds = (tab: AttachedTab, owner: string) => {
292
tab.screencasts.delete(owner);
290
- for (const [pending, hold] of tab.focusPending) {
291
- if (hold.owner === owner) tab.focusPending.delete(pending);
293
+ for (const [pending, holder] of tab.focusPending) {
294
+ if (holder === owner) tab.focusPending.delete(pending);
295
}
296
releaseFocusWhenIdle(tab);
297
syncPainting(tab);
@@ -296,7 +299,7 @@ export const make = Effect.gen(function* () {
299
300
/**
301
* A command the server stopped waiting on, such as a timed-out screenshot or evaluation,
299
- * may never settle; the turn's end ends every hold. The agent also sends on its own extra
302
+ * may never settle; the turn's end ends every focus hold. The agent also sends on its own extra
303
* session, so this cannot spare other sessions; a viewer's screencast still holds.
304
*/
305
const endAgentTurn = (threadId: string) => {
@@ -304,7 +307,7 @@ export const make = Effect.gen(function* () {
307
for (const tab of tabs.values()) {
308
if (tab.key.threadId !== threadId) continue;
309
tab.agentUsed = false;
307
- // Still sent in turn, so the queue keeps moving, but no longer kept painting.
310
+ // Still sent in turn, so the queue keeps moving, but no longer kept focused.
311
for (const waiting of tab.waitingScreenshots) waiting.held = false;
312
dropFocusHolds(tab);
313
}
@@ -325,8 +328,10 @@ export const make = Effect.gen(function* () {
328
if (sessionId === undefined && method === "Page.startScreencast") {
329
tab.screencasts.add(owner);
330
if (owner !== undefined) streamingOwners.add(owner);
331
+ syncPainting(tab);
332
} else if (sessionId === undefined && method === "Page.stopScreencast") {
333
tab.screencasts.delete(owner);
334
+ syncPainting(tab);
335
}
336
// A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
337
if (
@@ -339,9 +344,11 @@ export const make = Effect.gen(function* () {
344
const forward = (current?: () => boolean) => {
345
const change = tab.focusChange;
346
const run = () =>
342
- tab.relay === relay && (current === undefined || current())
343
- ? send(method, params, sessionId)
344
- : Promise.reject(new Error("The page this screenshot was for has moved on."));
347
+ tab.relay !== relay || (current !== undefined && !current())
348
+ ? Promise.reject(new Error("The page this screenshot was for has moved on."))
349
+ : current === undefined
350
+ ? send(method, params, sessionId)
351
+ : paintUntilAnswered(tab, () => send(method, params, sessionId));
352
return change === null ? run() : change.then(run);
353
};
354
return method === "Page.captureScreenshot"
@@ -350,17 +357,17 @@ export const make = Effect.gen(function* () {
357
relay,
358
() => owner === undefined || !detachedOwners.has(owner),
359
(current, held) =>
353
- held ? holdFocus(tab, method, owner, () => forward(current)) : forward(current),
360
+ held ? holdFocus(tab, owner, () => forward(current)) : forward(current),
361
)
362
: method === "Page.startScreencast"
356
- ? holdFocus(tab, method, owner, forward).catch((cause: unknown) => {
363
+ ? holdFocus(tab, owner, forward).catch((cause: unknown) => {
364
// A viewer can outlive a refused stream; nothing is streaming for it.
365
if (sessionId === undefined && tab.relay === relay) tab.screencasts.delete(owner);
366
releaseFocusWhenIdle(tab);
367
syncPainting(tab);
368
throw cause;
369
})
363
- : holdFocus(tab, method, owner, forward);
370
+ : holdFocus(tab, owner, forward);
371
},
372
sessionDetached: (owner) => {
373
detachedOwners.add(owner);
@@ -447,6 +454,7 @@ export const make = Effect.gen(function* () {
454
focusChange: null,
455
focusUses: 0,
456
focusPending: new Map(),
457
+ capturing: new Set(),
458
painting: false,
459
agentUsed: false,
460
focusReleasing: false,
@@ -461,7 +469,8 @@ export const make = Effect.gen(function* () {
469
// A screenshot pending when the page loads a new document, a reload included, can
470
// stay unanswered. A crashed page lands here once it reloads, well after its renderer died.
471
onNewDocument: () => {
464
- // Before the next screenshot starts, so its own hold survives.
472
+ // Before the next screenshot starts, so its own holds survive.
473
+ tab.capturing.clear();
474
dropFocusHolds(tab);
475
if (tab.screenshotTurn !== null) nextScreenshot(tab);
476
},