@devries-cameron20 / t3code Mirror · read-only / commits / e7c375d6ed
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 22, 08:15 UTC

fix(desktop): keep preview download settings out of app exports (#25428)

Co-authored-by: maria-rcks <maria@kuuro.net>

Isaac Mayes committed Nov 1, 2026 at 14:57 UTC e7c375d6ed8f4ed273dc41dfe5372f877fee8663
4 files changed +121 -13
apps/desktop/src/preview/CdpRelay.test.ts
+100 -7
@@ -171,7 +171,7 @@ describe("CDP relay extra sessions", () => {
171 });
172
173 describe("CDP relay downloads", () => {
174 - it("applies the server's download directory and reports downloads on the root session", async () => {
174 + it("captures preview downloads without changing the default browser context", async () => {
175 const send = vi.fn(async () => ({}));
176 const directories: Array<string | null> = [];
177 const written: Array<Record<string, unknown>> = [];
@@ -194,17 +194,110 @@ describe("CDP relay downloads", () => {
194 );
195 await settle();
196 expect(directories).toEqual(["/srv/artifacts"]);
197 - expect(send).toHaveBeenCalledWith(
198 - "Browser.setDownloadBehavior",
199 - { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: true },
200 - undefined,
201 - );
197 + expect(send.mock.calls).toEqual([
198 + ["Page.setDownloadBehavior", { behavior: "default" }, undefined],
199 + ]);
200 written.length = 0;
203 - relay.event("Browser.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
201 + relay.event("Page.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
202 + relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
203 expect(written).toEqual([
204 { method: "Browser.downloadWillBegin", params: { guid: "g1", suggestedFilename: "r.csv" } },
205 + { method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
206 ]);
207 });
208 +
209 + it("keeps denial scoped to the preview and restores Electron's save handling when allowed", async () => {
210 + const send = vi.fn(async () => ({}));
211 + const directories: Array<string | null> = [];
212 + const relay = makeRelay(
213 + makeTarget(send, (directory) => directories.push(directory)),
214 + () => {},
215 + );
216 + for (const [id, behavior] of ["deny", "allowAndName"].entries()) {
217 + relay.receive(
218 + JSON.stringify({
219 + id,
220 + method: "Browser.setDownloadBehavior",
221 + params: { behavior, downloadPath: "/srv/artifacts" },
222 + }),
223 + );
224 + await settle();
225 + }
226 + expect(directories).toEqual([null, "/srv/artifacts"]);
227 + expect(send.mock.calls).toEqual([
228 + ["Page.setDownloadBehavior", { behavior: "deny" }, undefined],
229 + ["Page.setDownloadBehavior", { behavior: "default" }, undefined],
230 + ]);
231 + });
232 +
233 + it("does not publish download events when the connection has disabled them", async () => {
234 + const written: Array<Record<string, unknown>> = [];
235 + const relay = makeRelay(makeTarget(vi.fn(async () => ({}))), (raw) =>
236 + written.push(JSON.parse(raw)),
237 + );
238 + relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
239 + relay.receive(
240 + JSON.stringify({
241 + id: 2,
242 + method: "Browser.setDownloadBehavior",
243 + params: { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: false },
244 + }),
245 + );
246 + await settle();
247 + written.length = 0;
248 + relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
249 + relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
250 + expect(written).toEqual([]);
251 + });
252 +
253 + it.each([false, true])(
254 + "keeps the previous download configuration (events enabled: %s) when configuration fails",
255 + async (enabled) => {
256 + const send = vi
257 + .fn(async () => ({}))
258 + .mockResolvedValueOnce({})
259 + .mockRejectedValueOnce(new Error("Page command rejected"));
260 + const written: Array<Record<string, unknown>> = [];
261 + let directory: string | null = null;
262 + const relay = makeRelay(
263 + makeTarget(send, (value) => {
264 + directory = value;
265 + }),
266 + (raw) => written.push(JSON.parse(raw)),
267 + );
268 + relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
269 + for (const [id, eventsEnabled] of [enabled, !enabled].entries()) {
270 + relay.receive(
271 + JSON.stringify({
272 + id: id + 2,
273 + method: "Browser.setDownloadBehavior",
274 + params: {
275 + behavior: id === 1 && enabled ? "deny" : "allowAndName",
276 + downloadPath: id === 0 ? "/srv/artifacts" : "/srv/new-artifacts",
277 + eventsEnabled,
278 + },
279 + }),
280 + );
281 + await settle();
282 + }
283 + expect(written).toContainEqual({
284 + id: 3,
285 + error: { code: -32000, message: "Page command rejected" },
286 + });
287 + expect(directory).toBe("/srv/artifacts");
288 + written.length = 0;
289 + relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
290 + relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
291 + expect(written).toEqual(
292 + enabled
293 + ? [
294 + { method: "Browser.downloadWillBegin", params: { guid: "g1" } },
295 + { method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
296 + ]
297 + : [],
298 + );
299 + },
300 + );
301 });
302
303 describe("CDP relay payload budget", () => {
apps/desktop/src/preview/CdpRelay.ts
+11 -3
@@ -98,6 +98,7 @@ export function createCdpRelayConnection(
98 close();
99 };
100 let attached = false;
101 + let downloadEventsEnabled = false;
102 /** Relay-created page sessions share one real debugger; child frame sessions retain their own ids. */
103 const sessions = new Map<string, "browser" | "page">([[PAGE_SESSION_ID, "page"]]);
104 let sessionSequence = 0;
@@ -122,9 +123,15 @@ export function createCdpRelayConnection(
123 case "Browser.setDownloadBehavior": {
124 const directory = command.params?.["downloadPath"];
125 const allowed = command.params?.["behavior"] !== "deny" && typeof directory === "string";
126 + // Browser-scoped behavior targets Electron's app session; preview paths belong to will-download.
127 + const result = await target.send(
128 + "Page.setDownloadBehavior",
129 + { behavior: command.params?.["behavior"] === "deny" ? "deny" : "default" },
130 + undefined,
131 + );
132 target.setDownloadDirectory(allowed ? directory : null);
126 - const { browserContextId: _ignored, ...params } = command.params ?? {};
127 - return target.send("Browser.setDownloadBehavior", params, undefined);
133 + downloadEventsEnabled = command.params?.["eventsEnabled"] === true;
134 + return result;
135 }
136 case "Browser.getVersion":
137 return {
@@ -232,9 +239,10 @@ export function createCdpRelayConnection(
239 },
240 event: (method, params, sessionId) => {
241 if (!attached) return;
242 + if (method.startsWith("Page.download")) method = method.replace("Page.", "Browser.");
243 // Download events are the browser's; Playwright listens on its root session.
244 if (method.startsWith("Browser.download")) {
237 - send({ method, params });
245 + if (downloadEventsEnabled) send({ method, params });
246 return;
247 }
248 // Electron reports the page's own events with an empty session id.
apps/desktop/src/preview/DesktopBrowserHost.test.ts
+9 -2
@@ -917,12 +917,14 @@ describe("DesktopBrowserHost", () => {
917 const host = yield* DesktopBrowserHost.make;
918 const debuggee = makeDebuggee();
919 host.attach(key, debuggee.tab);
920 + const replies = yield* watchReplies(host);
921 const paths: Array<string> = [];
922 const item = {
923 setSavePath: (path: string) => void paths.push(path),
924 } as unknown as Electron.DownloadItem;
925
926 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
927 + const sent = debuggee.nextSend();
928 yield* host.handleCommandLine(
929 encodeJson({
930 type: "cdp",
@@ -934,10 +936,15 @@ describe("DesktopBrowserHost", () => {
936 }),
937 }),
938 );
937 - debuggee.emit("Browser.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
939 + yield* Effect.promise(() => sent);
940 + debuggee.release();
941 + expect(yield* Queue.take(replies)).toBe(1);
942 + debuggee.emit("Page.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
943 + expect(host.placeDownload(makeDebuggee().tab.webContents, item)).toBe(false);
944 expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
945 expect(paths).toEqual(["/srv/downloads/guid-1"]);
940 - }),
946 + expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
947 + }).pipe(Effect.scoped),
948 );
949
950 it.effect("tells a download the person clicked from one the agent's input started", () =>
apps/desktop/src/preview/DesktopBrowserHost.ts
+1 -1
@@ -438,7 +438,7 @@ export const make = Effect.gen(function* () {
438 focusReleasing: false,
439 screencasts: new Set(),
440 onMessage: (_event, method, params, sessionId) => {
441 - if (method === "Browser.downloadWillBegin") {
441 + if (method === "Page.downloadWillBegin") {
442 const guid = (params as { guid?: unknown } | undefined)?.guid;
443 tab.pendingDownloadGuid = typeof guid === "string" ? guid : null;
444 }