Skip to content

Commit ae4aa9a

Browse files
fix(server): only click timeouts carry a reason, and its warning comes first
A server browser timeout forwarded Playwright's raw message as its reason, so preview_open repeated it. The click diagnostic now travels in the error detail, leads with the possible-click warning, caps the target, and still names the target when the count is unavailable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fd26e06 commit ae4aa9a

4 files changed

Lines changed: 35 additions & 22 deletions

File tree

‎apps/server/src/mcp/PreviewAutomationBroker.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ it.effect("names the click target that matched nothing when the server browser t
349349
ok: false,
350350
error: {
351351
_tag: "PreviewAutomationTimeoutError",
352-
message:
353-
'No element matches ".definitely-no-such-t3-target" after the timeout. The click may have happened and removed its target or navigated; check the page before retrying, then take a fresh snapshot and use its locator.',
352+
message: "No element matches the target.",
353+
detail: { reason: 'No element matches ".definitely-no-such-t3-target".' },
354354
},
355355
}),
356356
).pipe(Effect.forkScoped);
@@ -365,8 +365,9 @@ it.effect("names the click target that matched nothing when the server browser t
365365
.pipe(Effect.flip);
366366

367367
expect(error._tag).toBe("PreviewAutomationTimeoutError");
368-
expect(error.message).toContain(".definitely-no-such-t3-target");
369-
expect(error.message).toMatch(/no (element|match)|matched no|not found/i);
368+
expect(error.message).toBe(
369+
'Preview automation click timed out after 15000ms. No element matches ".definitely-no-such-t3-target".',
370+
);
370371
}),
371372
),
372373
);

‎apps/server/src/mcp/PreviewAutomationBroker.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ const readRemoteTimeoutMs = (detail: unknown): number | undefined => {
205205
: undefined;
206206
};
207207

208-
const MAX_REASON_CHARS = 500;
208+
const readRemoteReason = (detail: unknown): string | undefined =>
209+
typeof detail === "object" &&
210+
detail !== null &&
211+
"reason" in detail &&
212+
typeof detail.reason === "string"
213+
? detail.reason
214+
: undefined;
209215

210-
const serverBrowserReason = (
211-
context: PreviewAutomationRequestErrorContext,
212-
error: NonNullable<PreviewAutomationResponse["error"]>,
213-
) =>
214-
context.clientId === SERVER_BROWSER_AUTOMATION_CLIENT_ID
215-
? { reason: error.message.slice(0, MAX_REASON_CHARS) }
216-
: {};
216+
const MAX_REASON_CHARS = 500;
217217

218218
const classifyResponseError = (
219219
context: PreviewAutomationRequestErrorContext,
@@ -263,10 +263,13 @@ const classifyResponseError = (
263263
});
264264
case "PreviewAutomationTimeoutError": {
265265
const remoteTimeoutMs = readRemoteTimeoutMs(error.detail);
266+
const remoteReason = readRemoteReason(error.detail);
266267
return new PreviewAutomationTimeoutError({
267268
...context,
268269
...remoteDiagnostics,
269-
...serverBrowserReason(context, error),
270+
...(context.clientId === SERVER_BROWSER_AUTOMATION_CLIENT_ID && remoteReason !== undefined
271+
? { reason: remoteReason.slice(0, MAX_REASON_CHARS) }
272+
: {}),
270273
...(remoteTimeoutMs === undefined
271274
? {}
272275
: { timeoutMs: Math.min(context.timeoutMs, remoteTimeoutMs) }),
@@ -348,7 +351,9 @@ const classifyResponseError = (
348351
return new PreviewAutomationExecutionError({
349352
...context,
350353
...remoteDiagnostics,
351-
...serverBrowserReason(context, error),
354+
...(context.clientId === SERVER_BROWSER_AUTOMATION_CLIENT_ID
355+
? { reason: error.message.slice(0, MAX_REASON_CHARS) }
356+
: {}),
352357
});
353358
}
354359
};

‎apps/server/src/preview/ServerBrowserPage.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ describe("server browser element refs", () => {
109109
const absent = await failure(".definitely-no-such-t3-target");
110110
expect(absent.tag).toBe("PreviewAutomationTimeoutError");
111111
expect(absent.message).toBe(
112-
'No element matches ".definitely-no-such-t3-target" after the timeout. The click may have happened and removed its target or navigated; check the page before retrying, then take a fresh snapshot and use its locator.',
112+
'The click may have happened and removed its target or navigated; check the page before retrying. No element matches ".definitely-no-such-t3-target" after the timeout; take a fresh snapshot and use its locator.',
113113
);
114114
expect(absent.cause).toMatchObject({ name: "TimeoutError" });
115115
const hidden = await failure("button");
116116
expect(hidden.tag).toBe("PreviewAutomationTimeoutError");
117117
expect(hidden.message).toBe(
118-
'"button" still matches an element after the timeout. The click may have happened; check the page before retrying.',
118+
'The click may have happened; check the page before retrying. "button" still matches an element after the timeout.',
119119
);
120120
});
121121

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const toOperationError = (
7373

7474
const DEFAULT_TIMEOUT_MS = 15_000;
7575
const TARGET_COUNT_MS = 250;
76+
const MAX_TARGET_CHARS = 200;
7677

7778
const pageRefs = new WeakMap<Page, { generation: string; refs: Map<string, string> }>();
7879
// A compact runtime namespace prevents old refs from aliasing after a server restart.
@@ -654,7 +655,10 @@ const namedTargetTimeout = async (
654655
cause: unknown,
655656
) => {
656657
if (!(cause instanceof Error && cause.name === "TimeoutError")) return cause;
657-
const target = JSON.stringify(input.locator ?? input.selector);
658+
const selector = JSON.stringify(input.locator ?? input.selector);
659+
// The warning leads and the target is capped, so a reply cut short still carries the warning.
660+
const target =
661+
selector.length > MAX_TARGET_CHARS ? `${selector.slice(0, MAX_TARGET_CHARS)}...` : selector;
658662
// The host's reply must still beat the broker's short grace after the operation timeout.
659663
let timer: ReturnType<typeof setTimeout> | undefined;
660664
const matches = await Promise.race([
@@ -663,13 +667,16 @@ const namedTargetTimeout = async (
663667
timer = setTimeout(() => resolve(undefined), TARGET_COUNT_MS);
664668
}),
665669
]).finally(() => clearTimeout(timer));
666-
if (matches === undefined) return cause;
670+
const reason =
671+
matches === undefined
672+
? `The click may have happened; check the page before retrying. Whether ${target} still matches an element is unknown.`
673+
: matches === 0
674+
? `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.`
675+
: `The click may have happened; check the page before retrying. ${target} still matches an element after the timeout.`;
667676
return new ServerBrowserOperationError(
668677
"PreviewAutomationTimeoutError",
669-
matches === 0
670-
? `No element matches ${target} after the timeout. The click may have happened and removed its target or navigated; check the page before retrying, then take a fresh snapshot and use its locator.`
671-
: `${target} still matches an element after the timeout. The click may have happened; check the page before retrying.`,
672-
undefined,
678+
reason,
679+
{ reason },
673680
{ cause },
674681
);
675682
};

0 commit comments

Comments
 (0)