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(server): preview clicks fail within their timeout and say why (#34922)
Julius Marminge committed
Nov 13, 2026 at 10:21 UTC
445d32c2c3abdfac0a8aa070273529168efaac95
3 files changed
+45
-14
apps/server/src/preview/ServerBrowser.ts
+5
-1
@@ -2626,7 +2626,11 @@ const make = Effect.gen(function* () {
2626
return loadError ? { ...snapshot, url: loadError.url, loadError } : snapshot;
2627
}
2628
case "click": {
2629
- const clickInput = input as PreviewAutomationClickInput;
2629
+ // Time spent queued behind other work on this tab counts against the broker's deadline.
2630
+ const clickInput = {
2631
+ ...(input as PreviewAutomationClickInput),
2632
+ timeoutMs: Math.max(1, Math.floor(deadlineMs - performance.now())),
2633
+ };
2634
await recordAction(tab, "click", () =>
2635
ServerBrowserPage.click(tab.page, clickInput, pointerFor(tab)),
2636
);
apps/server/src/preview/ServerBrowserPage.test.ts
+13
-1
@@ -173,10 +173,22 @@ describe("server browser element refs", () => {
173
const hidden = await failure("button");
174
expect(hidden.tag).toBe("PreviewAutomationTimeoutError");
175
expect(hidden.message).toBe(
176
- 'The click may have happened; check the page before retrying. "button" still matches an element after the timeout.',
176
+ 'The click may have happened; check the page before retrying. "button" still matches an element after the timeout. Playwright last saw: element is not visible.',
177
);
178
});
179
180
+ it("gives a late target only the click's remaining time, not a fresh timeout", async () => {
181
+ await page.setContent(`<script>
182
+ setTimeout(() => document.body.insertAdjacentHTML("beforeend",
183
+ '<button id="save" disabled onclick="document.body.dataset.clicked = 1">Save</button>'), 1000);
184
+ setTimeout(() => document.querySelector("#save").disabled = false, 2500);
185
+ </script>`);
186
+ await expect(
187
+ ServerBrowserPage.click(page, { selector: "#save", timeoutMs: 2_000 }),
188
+ ).rejects.toThrow();
189
+ expect(await page.locator("body").getAttribute("data-clicked")).toBeNull();
190
+ });
191
+
192
it("does not retarget a removed ref to a replacement node", async () => {
193
await page.setContent("<button onclick=\"this.textContent='clicked'\">original</button>");
194
const locator = buttonLocator((await takeSnapshot()).accessibilityTree, "original");
apps/server/src/preview/ServerBrowserPage.ts
+27
-12
@@ -22,6 +22,7 @@ import { constVoid } from "effect/Function";
22
import * as Schema from "effect/Schema";
23
import type { CDPSession, ConsoleMessage, Frame, Locator, Page } from "playwright-core";
24
import * as NodeCrypto from "node:crypto";
25
+import * as NodeUtil from "node:util";
26
import { BrowserControlInterrupted } from "./SessionControl.ts";
27
28
const isBrowserControlInterrupted = Schema.is(BrowserControlInterrupted);
@@ -62,7 +63,7 @@ export const toOperationError = (
63
if (cause instanceof Error && cause.name === "TimeoutError") {
64
return new ServerBrowserOperationError({
65
tag: "PreviewAutomationTimeoutError",
65
- message: firstLine,
66
+ message: withActionabilityReason(firstLine, cause),
67
detail: timeoutMs === undefined ? undefined : { timeoutMs },
68
});
69
}
@@ -84,6 +85,17 @@ export const toOperationError = (
85
});
86
};
87
88
+/** Playwright puts why an element was not actionable in its call log, below the first line. */
89
+const withActionabilityReason = (message: string, cause: Error) => {
90
+ // Playwright dims call-log lines with ANSI escapes when colour is on.
91
+ const reason = NodeUtil.stripVTControlCharacters(cause.message)
92
+ .split("\n")
93
+ .map((line) => line.trim())
94
+ .findLast((line) => line.startsWith("- element is "))
95
+ ?.slice(2);
96
+ return reason === undefined ? message : `${message} Playwright last saw: ${reason}.`;
97
+};
98
+
99
const DEFAULT_TIMEOUT_MS = 15_000;
100
const TARGET_COUNT_MS = 250;
101
const MAX_TARGET_CHARS = 200;
@@ -168,11 +180,11 @@ const targetPoint = async (
180
page: Page,
181
locator: Locator | null,
182
input: { readonly x?: number | undefined; readonly y?: number | undefined },
171
- timeout: number,
183
+ timeout: () => number,
184
) => {
185
if (locator === null) return { x: input.x ?? 0, y: input.y ?? 0 };
174
- await scrollIntoView(locator, timeout);
175
- const box = await locator.boundingBox({ timeout });
186
+ await scrollIntoView(locator, timeout());
187
+ const box = await locator.boundingBox({ timeout: timeout() });
188
if (box) return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
189
const viewport = page.viewportSize();
190
return { x: (viewport?.width ?? 0) / 2, y: (viewport?.height ?? 0) / 2 };
@@ -711,10 +723,12 @@ export const click = async (
723
input: PreviewAutomationClickInput,
724
pointer: PointerReporter = noPointer,
725
): Promise<{ readonly x: number; readonly y: number }> => {
714
- const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
726
+ // One deadline across steps keeps the reply inside the broker's grace; Playwright reads 0 as none.
727
+ const deadline = Date.now() + (input.timeoutMs ?? DEFAULT_TIMEOUT_MS);
728
+ const remaining = () => Math.max(1, deadline - Date.now());
729
const locator = targetLocator(page, input);
730
try {
717
- return await clickTarget(page, locator, input, timeout, pointer);
731
+ return await clickTarget(page, locator, input, remaining, pointer);
732
} catch (cause) {
733
throw locator === null ? cause : await namedTargetTimeout(locator, input, cause);
734
}
@@ -750,10 +764,11 @@ const namedTargetTimeout = async (
764
: matches === 0
765
? `The click may have happened and removed its target or navigated; check the page before retrying. No element matches ${target} after the timeout; take a fresh snapshot and use its locator.`
766
: `The click may have happened; check the page before retrying. ${target} still matches an element after the timeout.`;
767
+ const message = withActionabilityReason(reason, cause);
768
return new ServerBrowserOperationError({
769
tag: "PreviewAutomationTimeoutError",
755
- message: reason,
756
- detail: { reason },
770
+ message,
771
+ detail: { reason: message },
772
cause,
773
});
774
};
@@ -762,7 +777,7 @@ const clickTarget = async (
777
page: Page,
778
locator: Locator | null,
779
input: PreviewAutomationClickInput,
765
- timeout: number,
780
+ timeout: () => number,
781
pointer: PointerReporter,
782
) => {
783
const point = await targetPoint(page, locator, input, timeout);
@@ -771,7 +786,7 @@ const clickTarget = async (
786
const clicked =
787
locator === null
788
? page.mouse.click(point.x, point.y, options)
774
- : locator.click({ ...options, timeout });
789
+ : locator.click({ ...options, timeout: timeout() });
790
let onDialog = constVoid;
791
const dialogOpened = new Promise<"dialog">((resolve) => {
792
onDialog = () => resolve("dialog");
@@ -852,7 +867,7 @@ export const hover = async (
867
) => {
868
const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
869
const locator = targetLocator(page, input);
855
- await pointer(await targetPoint(page, locator, input, timeout), "move");
870
+ await pointer(await targetPoint(page, locator, input, () => timeout), "move");
871
if (locator === null) {
872
await page.mouse.move(input.x ?? 0, input.y ?? 0);
873
return;
@@ -903,7 +918,7 @@ export const drag = async (
918
const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
919
const source = targetLocator(page, { locator: input.source })!;
920
const target = targetLocator(page, { locator: input.target })!;
906
- await pointer(await targetPoint(page, source, {}, timeout), "move");
921
+ await pointer(await targetPoint(page, source, {}, () => timeout), "move");
922
// Observe drag rejection immediately; an unhandled rejection during cursor movement exits the server.
923
// Chromium delivers a single move before dragstart; steps give the page mousemoves during the drag.
924
const dragFailure = source.dragTo(target, { timeout, steps: 5 }).then(