Skip to content

Commit b8be6da

Browse files
authored
fix(preview): report navigation timeout accurately (pingdotgg#24356)
1 parent cd2b681 commit b8be6da

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ function remoteDetailKind(detail: unknown): RemoteDetailKind {
205205
}
206206
}
207207

208+
const readRemoteTimeoutMs = (detail: unknown): number | undefined => {
209+
if (typeof detail !== "object" || detail === null || !("timeoutMs" in detail)) return undefined;
210+
const timeoutMs = detail.timeoutMs;
211+
return typeof timeoutMs === "number" && Number.isInteger(timeoutMs) && timeoutMs > 0
212+
? timeoutMs
213+
: undefined;
214+
};
215+
208216
const MAX_REASON_CHARS = 500;
209217

210218
const classifyResponseError = (
@@ -253,11 +261,16 @@ const classifyResponseError = (
253261
...context,
254262
...remoteDiagnostics,
255263
});
256-
case "PreviewAutomationTimeoutError":
264+
case "PreviewAutomationTimeoutError": {
265+
const remoteTimeoutMs = readRemoteTimeoutMs(error.detail);
257266
return new PreviewAutomationTimeoutError({
258267
...context,
259268
...remoteDiagnostics,
269+
...(remoteTimeoutMs === undefined
270+
? {}
271+
: { timeoutMs: Math.min(context.timeoutMs, remoteTimeoutMs) }),
260272
});
273+
}
261274
case "PreviewAutomationControlInterruptedError": {
262275
const reason = decodeControlReason(error.detail);
263276
return new PreviewAutomationControlInterruptedError({

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,37 @@ beforeEach(() => {
379379
desktopConnections.length = 0;
380380
});
381381

382+
it.live("preview_open reports its navigation timeout instead of the request budget", () =>
383+
Effect.scoped(
384+
Effect.gen(function* () {
385+
const { broker } = yield* ready;
386+
initialNavigation = async () => {
387+
throw Object.assign(new Error("page.goto: Timeout 15000ms exceeded."), {
388+
name: "TimeoutError",
389+
});
390+
};
391+
392+
const error = yield* broker
393+
.invoke<PreviewAutomationStatus>({
394+
scope,
395+
operation: "open",
396+
input: {
397+
url: "http://localhost:1337/",
398+
reuseExistingTab: false,
399+
show: false,
400+
},
401+
})
402+
.pipe(Effect.flip);
403+
404+
expect(error).toMatchObject({
405+
_tag: "PreviewAutomationTimeoutError",
406+
timeoutMs: 15_000,
407+
});
408+
expect(error.message).toBe("Preview automation open timed out after 15000ms.");
409+
}),
410+
).pipe(Effect.provide(layer)),
411+
);
412+
382413
it.live.each(["desktop" as const, undefined])(
383414
"explicit desktop targets ($0 runtime) fail without creating a server tab",
384415
(runtime) =>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ const make = Effect.gen(function* () {
902902
(desktop || adopted) && page.url() === snapshot.navStatus.url
903903
? page.reload(options)
904904
: page.goto(snapshot.navStatus.url, options)
905-
).then(constVoid);
905+
).then(constVoid, (cause: unknown) => {
906+
throw ServerBrowserPage.toOperationError(cause, NAVIGATION_TIMEOUT_MS);
907+
});
906908

907909
if (tab.control.agentId !== null) tab.control.track(tab.initialNavigation);
908910
else void tab.initialNavigation.catch(constVoid);
@@ -1458,7 +1460,7 @@ const make = Effect.gen(function* () {
14581460
`Navigation to ${url} failed: ${ServerBrowserPage.parseNetError(message).description}`,
14591461
);
14601462
}
1461-
throw cause;
1463+
throw ServerBrowserPage.toOperationError(cause, timeout);
14621464
});
14631465
};
14641466

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ export class ServerBrowserOperationError extends Error {
4040
}
4141
}
4242

43-
export const toOperationError = (cause: unknown): ServerBrowserOperationError => {
43+
export const toOperationError = (
44+
cause: unknown,
45+
timeoutMs?: number,
46+
): ServerBrowserOperationError => {
4447
if (cause instanceof BrowserControlInterrupted)
4548
return new ServerBrowserOperationError(
4649
"PreviewAutomationControlInterruptedError",
@@ -51,7 +54,11 @@ export const toOperationError = (cause: unknown): ServerBrowserOperationError =>
5154
const message = cause instanceof Error ? cause.message : String(cause);
5255
const firstLine = message.split("\n")[0] ?? message;
5356
if (cause instanceof Error && cause.name === "TimeoutError") {
54-
return new ServerBrowserOperationError("PreviewAutomationTimeoutError", firstLine);
57+
return new ServerBrowserOperationError(
58+
"PreviewAutomationTimeoutError",
59+
firstLine,
60+
timeoutMs === undefined ? undefined : { timeoutMs },
61+
);
5562
}
5663
if (
5764
/while parsing selector|Unknown engine|Unexpected token|strict mode violation/i.test(message)

0 commit comments

Comments
 (0)