Skip to content

Commit a01c5bd

Browse files
committed
Merge remote-tracking branch 'upstream/main' into feat/thread-workspace-split-panes
# Conflicts: # apps/web/src/components/ChatView.tsx
2 parents a1b8f8d + 20363c3 commit a01c5bd

323 files changed

Lines changed: 17473 additions & 2066 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.pnpm-store/v11/index.db‎

-8 KB
Binary file not shown.

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Cause from "effect/Cause";
2+
import * as Duration from "effect/Duration";
23
import * as Effect from "effect/Effect";
34
import * as Option from "effect/Option";
45
import * as Ref from "effect/Ref";
@@ -142,6 +143,20 @@ const handleFatalStartupError = Effect.fn("desktop.startup.handleFatalStartupErr
142143
const fatalStartupCause = <E>(stage: string, cause: Cause.Cause<E>) =>
143144
handleFatalStartupError(stage, Cause.pretty(cause)).pipe(Effect.andThen(Effect.failCause(cause)));
144145

146+
export const stopAllPoolInstances = Effect.fn("desktop.app.stopAllPoolInstances")(
147+
function* (): Effect.fn.Return<void, never, DesktopBackendPool.DesktopBackendPool> {
148+
// Stop every backend in the pool with a timeout to guarantee the quit
149+
// path makes progress even if a backend hangs during teardown.
150+
const pool = yield* DesktopBackendPool.DesktopBackendPool;
151+
const instances = yield* pool.list;
152+
yield* Effect.forEach(
153+
instances,
154+
(instance) => instance.stop({ timeout: Duration.seconds(5) }),
155+
{ concurrency: "unbounded" },
156+
);
157+
},
158+
);
159+
145160
const bootstrap = Effect.gen(function* () {
146161
const pool = yield* DesktopBackendPool.DesktopBackendPool;
147162
const primaryBackend = yield* pool.primary;
@@ -313,18 +328,12 @@ const scopedProgram = Effect.scoped(
313328
const shutdown = yield* DesktopShutdown.DesktopShutdown;
314329

315330
yield* Effect.addFinalizer(() =>
316-
Effect.gen(function* () {
317-
const pool = yield* DesktopBackendPool.DesktopBackendPool;
318-
// Stop every backend in the pool, not just the primary. The
319-
// electronApp.quit() path can race ahead of the layer-scope
320-
// cascade, so leaving the WSL instance for its parent scope
321-
// finalizer means it gets hard-killed by the OS instead of
322-
// receiving SIGTERM + grace. Stops run concurrently.
323-
const instances = yield* pool.list;
324-
yield* Effect.forEach(instances, (instance) => instance.stop(), {
325-
concurrency: "unbounded",
326-
});
327-
}).pipe(Effect.ensuring(shutdown.markComplete)),
331+
// Stop every backend in the pool, not just the primary. The
332+
// electronApp.quit() path can race ahead of the layer-scope
333+
// cascade, so leaving the WSL instance for its parent scope
334+
// finalizer means it gets hard-killed by the OS instead of
335+
// receiving SIGTERM + grace.
336+
stopAllPoolInstances().pipe(Effect.ensuring(shutdown.markComplete)),
328337
);
329338

330339
yield* startup;

‎apps/desktop/src/app/DesktopConnectionCatalogStore.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ const migrateSavedEnvironmentRecords = Effect.fn(
372372
profiles,
373373
credentials,
374374
remoteDpopTokens: [],
375+
disabledEnvironmentIds: [],
375376
};
376377
});
377378

‎apps/desktop/src/backend/DesktopBackendManager.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstab
2323
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
2424

2525
import * as DesktopBackendManager from "./DesktopBackendManager.ts";
26+
import * as DesktopApp from "../app/DesktopApp.ts";
27+
import * as DesktopBackendPool from "./DesktopBackendPool.ts";
2628
import * as DesktopObservability from "../app/DesktopObservability.ts";
2729
import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
2830
import * as DesktopWslEnvironment from "../wsl/DesktopWslEnvironment.ts";
@@ -1503,4 +1505,76 @@ describe("DesktopBackendManager", () => {
15031505
}).pipe(Effect.provide(TestClock.layer())),
15041506
),
15051507
);
1508+
1509+
it.effect("stopAllPoolInstances bounds the quit finalizer when backends hang", () =>
1510+
Effect.scoped(
1511+
Effect.gen(function* () {
1512+
// Each backend's process-scope finalizer reports when it starts and
1513+
// when it finishes, keyed by instance name, so the test can prove
1514+
// both backends reached each milestone instead of inferring it from
1515+
// a shared flag or a clock advance.
1516+
const teardownStarted = yield* Queue.unbounded<string>();
1517+
const teardownFinished = yield* Queue.unbounded<string>();
1518+
const allowTeardown = yield* Deferred.make<void>();
1519+
1520+
const makeInstance = (name: string) =>
1521+
makeTestInstance({
1522+
spawnerLayer: Layer.succeed(
1523+
ChildProcessSpawner.ChildProcessSpawner,
1524+
ChildProcessSpawner.make(() =>
1525+
Effect.gen(function* () {
1526+
const scope = yield* Scope.Scope;
1527+
yield* Scope.addFinalizer(
1528+
scope,
1529+
Queue.offer(teardownStarted, name).pipe(
1530+
Effect.andThen(Deferred.await(allowTeardown)),
1531+
Effect.andThen(Queue.offer(teardownFinished, name)),
1532+
Effect.asVoid,
1533+
),
1534+
);
1535+
return makeProcess({ exitCode: Effect.never });
1536+
}),
1537+
),
1538+
),
1539+
httpClientLayer: httpClientLayer(() => Effect.never),
1540+
});
1541+
1542+
const instance1 = yield* makeInstance("instance1");
1543+
const instance2 = yield* makeInstance("instance2");
1544+
1545+
yield* instance1.start;
1546+
yield* instance2.start;
1547+
1548+
const mockPool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
1549+
list: Effect.succeed([instance1, instance2]),
1550+
get: () => Effect.succeed(Option.none()),
1551+
primary: Effect.die(new Error("primary not implemented")),
1552+
register: () => Effect.die(new Error("register not implemented")),
1553+
unregister: () => Effect.die(new Error("unregister not implemented")),
1554+
});
1555+
1556+
// Mirror the quit path: register stopAllPoolInstances as a scope
1557+
// finalizer and let the scope close run it, rather than calling it
1558+
// as an ordinary interruptible effect.
1559+
const quitFiber = yield* Effect.scoped(
1560+
Effect.addFinalizer(() => DesktopApp.stopAllPoolInstances()),
1561+
).pipe(Effect.provide(mockPool), Effect.forkChild);
1562+
1563+
const started = yield* Queue.takeN(teardownStarted, 2);
1564+
assert.deepEqual(started.toSorted(), ["instance1", "instance2"]);
1565+
1566+
// Both backends are now hung in teardown. Advancing past the 5s
1567+
// budget must let the quit finalizer return without them.
1568+
yield* TestClock.adjust(Duration.seconds(5));
1569+
yield* Fiber.join(quitFiber);
1570+
assert.equal(yield* Queue.size(teardownFinished), 0);
1571+
1572+
// The timed-out closes keep running in the background and finish
1573+
// once the backends unblock.
1574+
yield* Deferred.succeed(allowTeardown, undefined);
1575+
const finished = yield* Queue.takeN(teardownFinished, 2);
1576+
assert.deepEqual(finished.toSorted(), ["instance1", "instance2"]);
1577+
}).pipe(Effect.provide(TestClock.layer())),
1578+
),
1579+
);
15061580
});

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
openExternal,
4141
openSystemSettings,
4242
checkSystemPermission,
43+
pasteAsText,
4344
probeRemoteEditors,
4445
pickFolder,
4546
pickProjectFavicon,
@@ -124,6 +125,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
124125
yield* ipc.handle(openExternal);
125126
yield* ipc.handle(openSystemSettings);
126127
yield* ipc.handle(checkSystemPermission);
128+
yield* ipc.handle(pasteAsText);
127129
yield* ipc.handle(probeRemoteEditors);
128130
yield* ipc.handle(getUpdateState);
129131
yield* ipc.handle(setUpdateChannel);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
77
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
88
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
99
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
10+
export const PASTE_AS_TEXT_CHANNEL = "desktop:paste-as-text";
1011
export const SNAP_SHOT_EVENT_CHANNEL = "desktop:snap-shot-event";
1112
export const QUIT_SHORTCUT_CHANNEL = "desktop:quit-shortcut";
1213
export const GET_WINDOW_FULLSCREEN_STATE_CHANNEL = "desktop:get-window-fullscreen-state";

‎apps/desktop/src/ipc/methods/window.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ import { vi } from "vite-plus/test";
66

77
import type * as Electron from "electron";
88

9+
const { focusedWebContents, ownerWindow } = vi.hoisted(() => ({
10+
focusedWebContents: vi.fn(),
11+
ownerWindow: vi.fn(),
12+
}));
13+
vi.mock("electron", () => ({
14+
webContents: { getFocusedWebContents: focusedWebContents },
15+
BrowserWindow: { fromWebContents: ownerWindow },
16+
}));
17+
918
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
1019
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
1120
import * as ElectronDialog from "../../electron/ElectronDialog.ts";
1221
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
1322
import {
1423
getLocalEnvironmentBootstraps,
1524
getWindowFullscreenState,
25+
pasteAsText,
1626
pickProjectFavicon,
1727
} from "./window.ts";
1828

@@ -153,6 +163,50 @@ describe("getWindowFullscreenState", () => {
153163
});
154164
});
155165

166+
describe("pasteAsText", () => {
167+
it.effect(
168+
"pastes into the focused guest only after the main renderer acknowledges the menu action",
169+
() => {
170+
const paste = vi.fn();
171+
const mainPaste = vi.fn();
172+
const window = {
173+
webContents: { id: 42, paste: mainPaste },
174+
isDestroyed: () => false,
175+
} as unknown as Electron.BrowserWindow;
176+
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => false });
177+
ownerWindow.mockReturnValue(window);
178+
179+
return Effect.gen(function* () {
180+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
181+
assert.equal(paste.mock.calls.length, 1);
182+
assert.equal(mainPaste.mock.calls.length, 0);
183+
184+
yield* pasteAsText.handler(undefined, { sender: { id: 99 } });
185+
assert.equal(paste.mock.calls.length, 1);
186+
ownerWindow.mockReturnValue({}); // A focused PiP/other BrowserWindow.
187+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
188+
assert.equal(paste.mock.calls.length, 1);
189+
ownerWindow.mockReturnValue(null); // Detached contents.
190+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
191+
assert.equal(paste.mock.calls.length, 1);
192+
ownerWindow.mockReturnValue(window);
193+
focusedWebContents.mockReturnValue({ paste, isDestroyed: () => true });
194+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
195+
assert.equal(paste.mock.calls.length, 1);
196+
focusedWebContents.mockReturnValue(null);
197+
yield* pasteAsText.handler(undefined, { sender: { id: 42 } });
198+
assert.equal(paste.mock.calls.length, 1);
199+
}).pipe(
200+
Effect.provide(
201+
Layer.mock(ElectronWindow.ElectronWindow)({
202+
main: Effect.succeed(Option.some(window)),
203+
}),
204+
),
205+
);
206+
},
207+
);
208+
});
209+
156210
describe("pickProjectFavicon", () => {
157211
it.effect("opens a single-image picker from the project directory", () =>
158212
Effect.gen(function* () {

‎apps/desktop/src/ipc/methods/window.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,32 @@ export const probeRemoteEditors = DesktopIpc.makeIpcMethod({
345345
}),
346346
});
347347

348+
export const pasteAsText = DesktopIpc.makeIpcMethod({
349+
channel: IpcChannels.PASTE_AS_TEXT_CHANNEL,
350+
payload: Schema.Undefined,
351+
result: Schema.Void,
352+
handler: Effect.fn("desktop.ipc.window.pasteAsText")(function* (_input, event) {
353+
const electronWindow = yield* ElectronWindow.ElectronWindow;
354+
const window = yield* electronWindow.main;
355+
if (
356+
event === undefined ||
357+
Option.isNone(window) ||
358+
window.value.isDestroyed() ||
359+
window.value.webContents.id !== event.sender.id
360+
) {
361+
return;
362+
}
363+
const focused = Electron.webContents.getFocusedWebContents();
364+
if (
365+
focused &&
366+
!focused.isDestroyed() &&
367+
Electron.BrowserWindow.fromWebContents(focused) === window.value
368+
) {
369+
focused.paste();
370+
}
371+
}),
372+
});
373+
348374
/** Theme files are a few KB; anything larger returns empty text and lets the
349375
* renderer reject it by size without the contents ever crossing the bridge. */
350376
const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;

‎apps/desktop/src/preload.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
157157
openSystemSettings: (pane: string) =>
158158
ipcRenderer.invoke(IpcChannels.OPEN_SYSTEM_SETTINGS_CHANNEL, pane),
159159
probeRemoteEditors: () => ipcRenderer.invoke(IpcChannels.PROBE_REMOTE_EDITORS_CHANNEL, undefined),
160+
pasteAsText: () => ipcRenderer.invoke(IpcChannels.PASTE_AS_TEXT_CHANNEL, undefined),
160161
onMenuAction: (listener) => {
161162
const wrappedListener = (_event: Electron.IpcRendererEvent, action: unknown) => {
162163
if (typeof action !== "string") return;

‎apps/desktop/src/preview/BrowserSession.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { fromPartition, sessions } = vi.hoisted(() => ({
1313
{
1414
readonly clearCache: ReturnType<typeof vi.fn>;
1515
readonly clearStorageData: ReturnType<typeof vi.fn>;
16-
readonly getUserAgent: ReturnType<typeof vi.fn>;
16+
readonly getUserAgent: ReturnType<typeof vi.fn<() => string>>;
1717
readonly setPermissionRequestHandler: ReturnType<typeof vi.fn>;
1818
readonly setPermissionCheckHandler: ReturnType<typeof vi.fn>;
1919
readonly setUserAgent: ReturnType<typeof vi.fn>;
@@ -102,6 +102,44 @@ describe("BrowserSession", () => {
102102
}).pipe(Effect.provide(layer)),
103103
);
104104

105+
// A rewritten session UA — any variant, even ones that keep the Electron
106+
// token — makes Cloudflare Turnstile loop with error 600010 (#5002), so
107+
// the guest must end up with Electron's native User-Agent. The mock applies
108+
// setUserAgent calls, so this fails on any reintroduced rewrite while still
109+
// permitting a harmless re-set of the unchanged native string.
110+
it.effect("keeps the guest's effective User-Agent equal to Electron's native one", () =>
111+
Effect.gen(function* () {
112+
// Electron's real UA shape: app token, then Chrome, then Electron, then
113+
// Safari — the token order and casing matter to any strip regex.
114+
const nativeUserAgent =
115+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) T3Code(Alpha)/0.0.33 Chrome/146.0.7680.216 Electron/41.5.0 Safari/537.36";
116+
fromPartition.mockReset();
117+
fromPartition.mockImplementation((partition: string) => {
118+
let userAgent = nativeUserAgent;
119+
const browserSession = {
120+
clearCache: vi.fn(() => Promise.resolve()),
121+
clearStorageData: vi.fn(() => Promise.resolve()),
122+
getUserAgent: vi.fn(() => userAgent),
123+
setPermissionRequestHandler: vi.fn(),
124+
setPermissionCheckHandler: vi.fn(),
125+
setUserAgent: vi.fn((next: string) => {
126+
userAgent = next;
127+
}),
128+
};
129+
sessions.set(partition, browserSession);
130+
return browserSession;
131+
});
132+
133+
const browserSessions = yield* BrowserSession.BrowserSession;
134+
const partition = yield* browserSessions.getPartition("scope-a");
135+
yield* browserSessions.getSession("scope-a");
136+
137+
const browserSession = sessions.get(partition);
138+
assert.isDefined(browserSession);
139+
assert.strictEqual(browserSession.getUserAgent(), nativeUserAgent);
140+
}).pipe(Effect.provide(layer)),
141+
);
142+
105143
it.effect("grants clipboard-sanitized-write through both the request and check handlers", () =>
106144
Effect.gen(function* () {
107145
const browserSessions = yield* BrowserSession.BrowserSession;

0 commit comments

Comments
 (0)