TypeScript
fix(server): a preview_open whose first load fails no longer leaves a hidden tab behind (#29865)
Alex Southwell8ca8fddbeba8
diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.tsindex 7111b17eb5b..90b1fc44072 100644--- a/apps/server/src/preview/ServerBrowser.test.ts+++ b/apps/server/src/preview/ServerBrowser.test.ts@@ -481,7 +481,8 @@ it.live("tells the desktop once per thread when its agent turns start and end",it.live("preview_open reports its navigation timeout instead of the request budget", () =>Effect.scoped(Effect.gen(function* () {- const { broker } = yield* ready;+ const { broker, tabId } = yield* ready;+ const manager = yield* Manager.PreviewManager;initialNavigation = async () => {throw Object.assign(new Error("page.goto: Timeout 15000ms exceeded."), {name: "TimeoutError",@@ -505,6 +506,54 @@ it.live("preview_open reports its navigation timeout instead of the request budgtimeoutMs: 15_000,});expect(error.message).toBe("Preview automation open timed out after 15000ms.");+ // The failed open returned no tabId, so its tab must not linger and block later opens.+ const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });+ expect(sessions.map((session) => session.tabId)).toEqual([tabId]);+ expect(contexts[1]!.page.close).toHaveBeenCalled();+ }),+ ).pipe(Effect.provide(layer)),+);++it.live("an initial download stays available after preview_open reports the navigation error", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const { broker, tabId } = yield* ready;+ const manager = yield* Manager.PreviewManager;+ const released = Promise.withResolvers<void>();+ initialNavigation = async () => {+ contexts[1]!.page.emit("download", {+ failure: async () => null,+ delete: async () => released.resolve(),+ saveAs: (path: string) => NodeFSP.writeFile(path, "a,b"),+ suggestedFilename: () => "report.csv",+ url: () => "http://localhost:1337/report.csv",+ });+ throw new Error("page.goto: Download is starting");+ };+ const error = yield* broker+ .invoke<PreviewAutomationStatus>({+ scope,+ operation: "open",+ input: { url: "http://localhost:1337/report.csv", reuseExistingTab: false, open: false },+ })+ .pipe(Effect.flip);+ expect(error.message).toContain("Download is starting");+ yield* Effect.promise(() => released.promise);+ const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });+ expect(sessions).toHaveLength(2);+ const downloadTabId = PreviewTabId.make(sessions.find((tab) => tab.tabId !== tabId)!.tabId);+ const status = yield* broker.invoke<PreviewAutomationStatus>({+ scope,+ tabId: downloadTabId,+ operation: "status",+ input: {},+ });+ expect(status.downloads).toEqual([+ expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),+ ]);+ expect(yield* (yield* FileSystem.FileSystem).readFileString(status.downloads![0]!.path)).toBe(+ "a,b",+ );}),).pipe(Effect.provide(layer)),);diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.tsindex 6c147bbf9fe..5206e67ddad 100644--- a/apps/server/src/preview/ServerBrowser.ts+++ b/apps/server/src/preview/ServerBrowser.ts@@ -2013,7 +2013,16 @@ const make = Effect.gen(function* () {if (existing) {if (url) await navigate(tab, url, "load", navigationTimeout);} else {- await tab.initialNavigation;+ // A failed open returns no tabId; a leftover hidden tab would block later reuse.+ await tab.initialNavigation?.catch(async (cause: unknown) => {+ // Playwright rejects downloads too; their tab must survive to save the file.+ if (cause instanceof Error && cause.message.includes("Download is starting")) {+ throw cause;+ }+ await runPromise(manager.close({ threadId: tab.threadId, tabId: tab.tabId }));+ dropTab(tab, false);+ throw cause;+ });}const reveal = open.open ?? open.show;if (reveal !== false) {