@devries-cameron20 / t3code Mirror · read-only / commits / 4a6cd8defa
Fork 0

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 23, 00:30 UTC

fix(preview): release original browser downloads after saving copies (#18435)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Alex Southwell committed Oct 20, 2026 at 00:28 UTC 4a6cd8defaa7f97179aef70621a09ce394a81ba0
2 files changed +133 -55
apps/server/src/preview/ServerBrowser.test.ts
+131 -55
@@ -1,4 +1,6 @@
1 import * as NodeEvents from "node:events";
2 +// @effect-diagnostics-next-line nodeBuiltinImport:off - Playwright download mocks perform real file operations outside the Effect runtime.
3 +import * as NodeFSP from "node:fs/promises";
4
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import { it } from "@effect/vitest";
@@ -1034,6 +1036,15 @@ it.live("a tab opened to a file the browser cannot show reports the file to down
1036 const written = Promise.withResolvers<void>();
1037 page.emit("download", {
1038 failure: async () => null,
1039 + delete: async () => {
1040 + // A new navigation during cleanup must not erase the settled download status.
1041 + page.emit("request", {
1042 + url: () => "https://example.com/next",
1043 + method: () => "GET",
1044 + isNavigationRequest: () => true,
1045 + frame: () => page,
1046 + });
1047 + },
1048 saveAs: (path: string) => {
1049 Queue.offerUnsafe(saved, path);
1050 return written.promise;
@@ -1044,9 +1055,23 @@ it.live("a tab opened to a file the browser cannot show reports the file to down
1055 const fs = yield* FileSystem.FileSystem;
1056 yield* fs.writeFileString(yield* Queue.take(saved), "%PDF");
1057 written.resolve();
1047 - let status = yield* PubSub.take(events);
1048 - while (status.type !== "failed") status = yield* PubSub.take(events);
1049 - expect(status).toMatchObject({ url: pdf, download: { fileName: "paper.pdf" } });
1058 + const received = [];
1059 + let status;
1060 + do {
1061 + status = yield* PubSub.take(events);
1062 + received.push(status);
1063 + } while (
1064 + status.type !== "navigated" ||
1065 + status.snapshot.navStatus._tag !== "Loading" ||
1066 + status.snapshot.navStatus.url !== "https://example.com/next"
1067 + );
1068 + expect(received).toContainEqual(
1069 + expect.objectContaining({
1070 + type: "failed",
1071 + url: pdf,
1072 + download: { id: expect.any(String), fileName: "paper.pdf" },
1073 + }),
1074 + );
1075 expect((yield* Queue.clear(viewer.output)).some((item) => item._tag === "download")).toBe(
1076 false,
1077 );
@@ -1078,6 +1103,7 @@ it.live("a file a blank tab opened is offered to download if the tab moves on wh
1103 const written = Promise.withResolvers<void>();
1104 page.emit("download", {
1105 failure: async () => null,
1106 + delete: async () => {},
1107 saveAs: (path: string) => {
1108 Queue.offerUnsafe(saved, path);
1109 return written.promise;
@@ -1120,6 +1146,7 @@ it.live("a download from a superseded navigation leaves the newer navigation loa
1146 const written = Promise.withResolvers<void>();
1147 page.emit("download", {
1148 failure: async () => null,
1149 + delete: async () => {},
1150 saveAs: (path: string) => {
1151 Queue.offerUnsafe(saved, path);
1152 return written.promise;
@@ -1321,58 +1348,107 @@ it.live("copy keys reach the viewer from a frame in its own process", () =>
1348 ).pipe(Effect.provide(layer)),
1349 );
1350
1324 -it.live("a page download is saved, offered to the controller, and listed for the agent", () =>
1325 - Effect.scoped(
1326 - Effect.gen(function* () {
1327 - const { browser, broker, tabId } = yield* ready;
1328 - const page = contexts[0]!.page;
1329 - const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1330 - yield* viewer.input({ type: "takeControl" });
1331 - yield* Queue.clear(viewer.output);
1332 - const saved = yield* Queue.unbounded<string>();
1333 - const written = Promise.withResolvers<void>();
1334 - page.emit("download", {
1335 - failure: async () => null,
1336 - saveAs: (path: string) => {
1337 - Queue.offerUnsafe(saved, path);
1338 - return written.promise;
1339 - },
1340 - suggestedFilename: () => "report.csv",
1341 - url: () => "blob:http://localhost:5173/1",
1342 - });
1343 - const fs = yield* FileSystem.FileSystem;
1344 - const path = yield* Queue.take(saved);
1345 - yield* fs.writeFileString(path, "a,b");
1346 - written.resolve();
1347 - let offered = yield* Queue.take(viewer.output);
1348 - while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
1349 - expect(offered).toMatchObject({ _tag: "download", fileName: "report.csv", sizeBytes: 3 });
1350 - const file = yield* browser.openDownload({
1351 - threadId: scope.thread.threadId,
1352 - tabId,
1353 - downloadId: offered.id,
1354 - });
1355 - expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
1356 - const status = yield* broker.invoke<PreviewAutomationStatus>({
1357 - scope,
1358 - tabId,
1359 - operation: "status",
1360 - input: {},
1361 - });
1362 - expect(status.downloads).toEqual([
1363 - expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1364 - ]);
1365 - expect(
1366 - Option.isNone(
1367 - yield* browser.openDownload({
1368 - threadId: scope.thread.threadId,
1369 - tabId,
1370 - downloadId: "guess",
1371 - }),
1372 - ),
1373 - ).toBe(true);
1374 - }),
1375 - ).pipe(Effect.provide(layer)),
1351 +it.live.each([false, true])(
1352 + "a page download is saved, offered to the controller, and listed for the agent (delete fails: %s)",
1353 + (deleteFails) =>
1354 + Effect.scoped(
1355 + Effect.gen(function* () {
1356 + const { browser, broker, tabId } = yield* ready;
1357 + const page = contexts[0]!.page;
1358 + const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
1359 + yield* viewer.input({ type: "takeControl" });
1360 + yield* Queue.clear(viewer.output);
1361 + const fs = yield* FileSystem.FileSystem;
1362 + const original = `${yield* fs.makeTempDirectoryScoped()}/chromium-download`;
1363 + yield* fs.writeFileString(original, "a,b");
1364 + const released = Promise.withResolvers<void>();
1365 + page.emit("download", {
1366 + failure: async () => null,
1367 + delete: async () => {
1368 + try {
1369 + if (deleteFails) throw new Error("context closed");
1370 + await NodeFSP.rm(original);
1371 + } finally {
1372 + released.resolve();
1373 + }
1374 + },
1375 + saveAs: (path: string) => NodeFSP.copyFile(original, path),
1376 + suggestedFilename: () => "report.csv",
1377 + url: () => "blob:http://localhost:5173/1",
1378 + });
1379 + let offered = yield* Queue.take(viewer.output);
1380 + while (offered._tag !== "download") offered = yield* Queue.take(viewer.output);
1381 + expect(offered).toMatchObject({ _tag: "download", fileName: "report.csv", sizeBytes: 3 });
1382 + yield* Effect.promise(() => released.promise);
1383 + expect(yield* fs.exists(original)).toBe(deleteFails);
1384 + const file = yield* browser.openDownload({
1385 + threadId: scope.thread.threadId,
1386 + tabId,
1387 + downloadId: offered.id,
1388 + });
1389 + expect(Option.isSome(file) && file.value.fileName).toBe("report.csv");
1390 + expect(yield* fs.readFileString(Option.getOrThrow(file).path)).toBe("a,b");
1391 + const status = yield* broker.invoke<PreviewAutomationStatus>({
1392 + scope,
1393 + tabId,
1394 + operation: "status",
1395 + input: {},
1396 + });
1397 + expect(status.downloads).toEqual([
1398 + expect.objectContaining({ fileName: "report.csv", sizeBytes: 3 }),
1399 + ]);
1400 + expect(
1401 + Option.isNone(
1402 + yield* browser.openDownload({
1403 + threadId: scope.thread.threadId,
1404 + tabId,
1405 + downloadId: "guess",
1406 + }),
1407 + ),
1408 + ).toBe(true);
1409 + }),
1410 + ).pipe(Effect.provide(layer)),
1411 +);
1412 +
1413 +it.live.each(["save failed", "oversized"])(
1414 + "a rejected download releases the original and keeps no saved copy (%s)",
1415 + (outcome) =>
1416 + Effect.scoped(
1417 + Effect.gen(function* () {
1418 + const { broker, tabId } = yield* ready;
1419 + const fs = yield* FileSystem.FileSystem;
1420 + const original = `${yield* fs.makeTempDirectoryScoped()}/chromium-download`;
1421 + yield* fs.writeFileString(original, "a,b");
1422 + let savedPath: string | undefined;
1423 + const released = Promise.withResolvers<void>();
1424 + contexts[0]!.page.emit("download", {
1425 + failure: async () => null,
1426 + delete: async () => {
1427 + await NodeFSP.rm(original);
1428 + released.resolve();
1429 + },
1430 + saveAs: async (path: string) => {
1431 + savedPath = path;
1432 + await NodeFSP.copyFile(original, path);
1433 + if (outcome === "save failed") throw new Error("save interrupted");
1434 + // A sparse file exercises the size limit without writing a GiB of data.
1435 + await NodeFSP.truncate(path, 1024 ** 3 + 1);
1436 + },
1437 + suggestedFilename: () => "report.csv",
1438 + url: () => "blob:http://localhost:5173/1",
1439 + });
1440 + yield* Effect.promise(() => released.promise);
1441 + expect(yield* fs.exists(original)).toBe(false);
1442 + if (savedPath !== undefined) expect(yield* fs.exists(savedPath)).toBe(false);
1443 + const status = yield* broker.invoke<PreviewAutomationStatus>({
1444 + scope,
1445 + tabId,
1446 + operation: "status",
1447 + input: {},
1448 + });
1449 + expect(status.downloads).toEqual([]);
1450 + }),
1451 + ).pipe(Effect.provide(layer)),
1452 );
1453
1454 it.live("a page's file picker goes to the controller and takes its uploaded files", () =>
apps/server/src/preview/ServerBrowser.ts
+2
@@ -985,6 +985,8 @@ const make = Effect.gen(function* () {
985 if (navigation !== null && !tab.closing) {
986 settleAbortedNavigation(tab, navigation, download.url(), offered);
987 }
988 + // saveAs copies the artifact; cached profile contexts otherwise retain the original.
989 + await download.delete().catch(constVoid);
990 }
991 };
992