@devries-cameron20 / t3code-1 / commits / a72481fb4c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): preserve concurrent downloads with the same name (#29308)

Lakshmi Tanmay committed Nov 9, 2026 at 20:01 UTC a72481fb4c3e056357b308e05e2865460cdf38df
2 files changed +114 -1
apps/desktop/src/preview/Manager.test.ts
+100
@@ -1,5 +1,6 @@
1 import { it as effectIt } from "@effect/vitest";
2 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 +import * as NodeServices from "@effect/platform-node/NodeServices";
4 import type {
5 DesktopPreviewRecordingInputEvent,
6 PreviewAnnotationPayload,
@@ -7,6 +8,7 @@ import type {
8 } from "@t3tools/contracts";
9 import * as HostProcess from "@t3tools/shared/HostProcess";
10 import { parseKeybindingShortcut } from "@t3tools/shared/keybindings";
11 +import { app, shell } from "electron";
12 import * as Cause from "effect/Cause";
13 import * as Deferred from "effect/Deferred";
14 import * as Effect from "effect/Effect";
@@ -169,6 +171,7 @@ const {
171 }));
172
173 vi.mock("electron", () => ({
174 + app: { getPath: vi.fn() },
175 BrowserWindow: browserWindowConstructor,
176 // oxlint-disable-next-line typescript/no-extraneous-class -- This constructor-only mock implements the browser ClipboardItem API.
177 ClipboardItem: class {
@@ -571,6 +574,103 @@ describe("PreviewManager", () => {
574 webviewSend.mockClear();
575 });
576
577 + effectIt.effect.each([false, true])(
578 + "preserves simultaneous same-name downloads with an existing destination: %s",
579 + (existing) =>
580 + Effect.gen(function* () {
581 + const fs = yield* FileSystem.FileSystem;
582 + const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-download-test-" });
583 + const downloads = `${dir}/downloads`;
584 + yield* fs.makeDirectory(downloads);
585 + using _downloads = vi.spyOn(app, "getPath").mockReturnValue(downloads);
586 + if (existing) yield* fs.writeFileString(`${downloads}/report.txt`, "existing download");
587 + const sources = [`${dir}/first`, `${dir}/second`];
588 + yield* fs.writeFileString(sources[0]!, "first download");
589 + yield* fs.writeFileString(sources[1]!, "second download");
590 + const checked = yield* Deferred.make<void>();
591 + const finished = makeChangeSignal();
592 + let probes = 0;
593 + let completions = 0;
594 + using _reveal = vi.spyOn(shell, "showItemInFolder").mockImplementation(() => {
595 + completions += 1;
596 + finished.notify();
597 + });
598 + const raceTarget = `${downloads}/${existing ? "report (1).txt" : "report.txt"}`;
599 + const exists = (target: string) =>
600 + Effect.gen(function* () {
601 + const found = yield* fs.exists(target);
602 + if (target === raceTarget) {
603 + if (++probes === 2) yield* Deferred.succeed(checked, undefined);
604 + yield* Deferred.await(checked);
605 + }
606 + return found;
607 + });
608 + const manager = yield* PreviewManager.make.pipe(
609 + Effect.provideService(FileSystem.FileSystem, { ...fs, exists }),
610 + Effect.provide(
611 + Layer.mergeAll(
612 + layerEnvironment,
613 + layerBrowserSession,
614 + Layer.mock(DesktopBrowserHost.DesktopBrowserHost)({
615 + attach: () => {},
616 + detach: () => {},
617 + setShown: () => {},
618 + humanStartedDownload: () => true,
619 + placeDownload: () => true,
620 + }),
621 + Layer.mock(PreviewPasskeys.PreviewPasskeys)({
622 + bridgeEnabled: false,
623 + installSessionHandlers: () => {},
624 + attachGuest: () => () => {},
625 + }),
626 + Layer.mock(DesktopRendererHistory.DesktopRendererHistory)({}),
627 + Layer.succeed(HostProcess.Platform, "linux"),
628 + ),
629 + ),
630 + );
631 + yield* manager.getBrowserSession("downloads");
632 + const listener = previewSession.on.mock.calls[0]![1] as (
633 + event: unknown,
634 + item: {
635 + once: (event: string, done: (event: unknown, state: string) => void) => void;
636 + getFilename: () => string;
637 + getSavePath: () => string;
638 + },
639 + source: unknown,
640 + ) => void;
641 + for (const source of sources) {
642 + listener(
643 + {},
644 + {
645 + once: (_event, done) => done({}, "completed"),
646 + getFilename: () => "report.txt",
647 + getSavePath: () => source,
648 + },
649 + {},
650 + );
651 + }
652 + yield* finished.until(() => completions === 2);
653 + const names = yield* fs.readDirectory(downloads);
654 + expect(names.toSorted()).toEqual(
655 + existing
656 + ? ["report (1).txt", "report (2).txt", "report.txt"]
657 + : ["report (1).txt", "report.txt"],
658 + );
659 + const contents = yield* Effect.forEach(names, (name) =>
660 + fs.readFileString(`${downloads}/${name}`),
661 + );
662 + expect(contents.toSorted()).toEqual(
663 + (existing
664 + ? ["existing download", "first download", "second download"]
665 + : ["first download", "second download"]
666 + ).toSorted(),
667 + );
668 + if (existing) {
669 + expect(yield* fs.readFileString(`${downloads}/report.txt`)).toBe("existing download");
670 + }
671 + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Effect.scoped),
672 + );
673 +
674 effectIt.effect.each([
675 ["linux", "ctrl+alt+[", "[", "BracketLeft", false],
676 ["win32", "ctrl+alt+8", "[", "Digit8", false],
apps/desktop/src/preview/Manager.ts
+14 -1
@@ -1,3 +1,5 @@
1 +// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.copyFile cannot request an exclusive copy.
2 +import * as NodeFSP from "node:fs/promises";
3 import * as NodeZlib from "node:zlib";
4 import {
5 DesktopPreviewAnnotationVoiceEventSchema,
@@ -3686,7 +3688,18 @@ export const make = Effect.gen(function* PreviewManagerMake() {
3688 attempt === 0 ? `${stem}${extension}` : `${stem} (${attempt})${extension}`,
3689 );
3690 if (yield* fileSystem.exists(target)) continue;
3689 - yield* fileSystem.copyFile(source, target);
3691 + const copied = yield* Effect.tryPromise({
3692 + try: () => NodeFSP.copyFile(source, target, NodeFSP.constants.COPYFILE_EXCL),
3693 + catch: (cause) => new PreviewOperationError({ operation: "copyToDownloads", cause }),
3694 + }).pipe(
3695 + Effect.as(true),
3696 + Effect.catchIf(
3697 + (error) =>
3698 + error.cause instanceof Error && "code" in error.cause && error.cause.code === "EEXIST",
3699 + () => Effect.succeed(false),
3700 + ),
3701 + );
3702 + if (!copied) continue;
3703 shell.showItemInFolder(target);
3704 return;
3705 }