Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(desktop): throttle hidden preview rendering (#7445)

t3-code[bot]fe281c540816
diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts
index 880e704f8..3bf6d6305 100644
--- a/apps/desktop/src/preview/Manager.test.ts
+++ b/apps/desktop/src/preview/Manager.test.ts
@@ -1797,6 +1797,216 @@ describe("PreviewManager", () => {
),
);
+ effectIt.effect("keeps window unthrottled until the final frame capture stops", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const setBackgroundThrottling = vi.fn();
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("recording-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ const webContentsById = new Map([
+ [41, makeTestPreviewWebContents(capturePage, 41)],
+ [42, makeTestPreviewWebContents(capturePage, 42)],
+ ]);
+ fromId.mockImplementation((id) =>
+ id === undefined ? null : (webContentsById.get(id) ?? null),
+ );
+
+ yield* manager.createTab("tab_capture_throttling_1");
+ yield* manager.createTab("tab_capture_throttling_2");
+ yield* manager.registerWebview("tab_capture_throttling_1", 41);
+ yield* manager.registerWebview("tab_capture_throttling_2", 42);
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: { setBackgroundThrottling },
+ } as never);
+
+ yield* manager.startRecording("tab_capture_throttling_1");
+ yield* manager.startRecording("tab_capture_throttling_2");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
+
+ yield* manager.stopRecording("tab_capture_throttling_1");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
+
+ yield* manager.stopRecording("tab_capture_throttling_2");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
+ }),
+ ),
+ );
+
+ effectIt.effect("does not commit failed starts and retries throttle restoration", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const setBackgroundThrottling = vi.fn<(enabled: boolean) => void>();
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("recording-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+
+ yield* manager.createTab("tab_capture_throttling_failure");
+ yield* manager.registerWebview("tab_capture_throttling_failure", 42);
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: { setBackgroundThrottling },
+ } as never);
+
+ setBackgroundThrottling.mockImplementationOnce(() => {
+ throw new Error("start throttling update failed");
+ });
+ const failedStart = yield* Effect.exit(
+ manager.startRecording("tab_capture_throttling_failure"),
+ );
+ expect(Exit.isFailure(failedStart)).toBe(true);
+
+ yield* manager.startRecording("tab_capture_throttling_failure");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false]]);
+
+ setBackgroundThrottling.mockImplementationOnce(() => {
+ throw new Error("stop throttling update failed");
+ });
+ yield* manager.stopRecording("tab_capture_throttling_failure");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false], [false], [true], [true]]);
+
+ yield* manager.startRecording("tab_capture_throttling_failure");
+ yield* manager.stopRecording("tab_capture_throttling_failure");
+ expect(setBackgroundThrottling.mock.calls).toEqual([
+ [false],
+ [false],
+ [true],
+ [true],
+ [false],
+ [true],
+ ]);
+ }),
+ ),
+ );
+
+ effectIt.effect("does not publish a replacement window when capture reconciliation fails", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const setBackgroundThrottling = vi.fn(() => {
+ throw new Error("replacement throttling update failed");
+ });
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("recording-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+
+ yield* manager.createTab("tab_capture_replacement_failure");
+ yield* manager.registerWebview("tab_capture_replacement_failure", 42);
+ yield* manager.startRecording("tab_capture_replacement_failure");
+
+ const failedReplacement = yield* Effect.exit(
+ manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: { setBackgroundThrottling },
+ } as never),
+ );
+ expect(Exit.isFailure(failedReplacement)).toBe(true);
+
+ yield* manager.stopRecording("tab_capture_replacement_failure");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
+ }),
+ ),
+ );
+
+ effectIt.effect("ignores close events from replaced main windows", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ let closeFirstWindow: (() => void) | undefined;
+ const firstWindowThrottling = vi.fn();
+ const replacementWindowThrottling = vi.fn();
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("recording-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+
+ yield* manager.createTab("tab_replaced_window_close");
+ yield* manager.registerWebview("tab_replaced_window_close", 42);
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn((event: string, listener: () => void) => {
+ if (event === "closed") closeFirstWindow = listener;
+ }),
+ webContents: { setBackgroundThrottling: firstWindowThrottling },
+ } as never);
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: { setBackgroundThrottling: replacementWindowThrottling },
+ } as never);
+
+ closeFirstWindow?.();
+ yield* manager.startRecording("tab_replaced_window_close");
+ expect(firstWindowThrottling).not.toHaveBeenCalled();
+ expect(replacementWindowThrottling.mock.calls).toEqual([[false]]);
+ yield* manager.stopRecording("tab_replaced_window_close");
+ expect(replacementWindowThrottling.mock.calls).toEqual([[false], [true]]);
+ }),
+ ),
+ );
+
+ effectIt.effect("releases frame capture when the main window closes", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ let closeMainWindow: (() => void) | undefined;
+ const firstWindowThrottling = vi.fn();
+ const replacementWindowThrottling = vi.fn();
+ const capturePage = vi.fn(async () => ({
+ toJPEG: () => Buffer.from("recording-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ }));
+ const webContentsById = new Map([
+ [42, makeTestPreviewWebContents(capturePage, 42)],
+ [43, makeTestPreviewWebContents(capturePage, 43)],
+ ]);
+ fromId.mockImplementation((id) =>
+ id === undefined ? null : (webContentsById.get(id) ?? null),
+ );
+
+ yield* manager.createTab("tab_window_close_recording");
+ yield* manager.createTab("tab_window_close_race");
+ yield* manager.registerWebview("tab_window_close_recording", 42);
+ yield* manager.registerWebview("tab_window_close_race", 43);
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn((event: string, listener: () => void) => {
+ if (event === "closed") closeMainWindow = listener;
+ }),
+ webContents: { setBackgroundThrottling: firstWindowThrottling },
+ } as never);
+ yield* manager.startRecording("tab_window_close_recording");
+ expect(firstWindowThrottling.mock.calls).toEqual([[false]]);
+
+ closeMainWindow?.();
+ const racedStart = yield* Effect.exit(manager.startRecording("tab_window_close_race"));
+ expect(Exit.isFailure(racedStart)).toBe(true);
+ if (Exit.isFailure(racedStart)) {
+ expect(Option.getOrThrow(Cause.findErrorOption(racedStart.cause))).toMatchObject({
+ _tag: "PreviewMainWindowClosedError",
+ tabId: "tab_window_close_race",
+ });
+ }
+ yield* Effect.yieldNow;
+ yield* Effect.yieldNow;
+
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: { setBackgroundThrottling: replacementWindowThrottling },
+ } as never);
+ expect(replacementWindowThrottling).not.toHaveBeenCalled();
+ }),
+ ),
+ );
+
effectIt.effect("captures hidden preview recordings independently for concurrent tabs", () =>
withManager((manager) =>
Effect.gen(function* () {
@@ -2106,6 +2316,8 @@ describe("PreviewManager", () => {
effectIt.effect("shares background frame capture between recording and picture-in-picture", () =>
withManager((manager) =>
Effect.gen(function* () {
+ const setBackgroundThrottling = vi.fn();
+ const mainWindowWebContents = { setBackgroundThrottling };
const jpeg = Buffer.from("shared-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
@@ -2113,6 +2325,7 @@ describe("PreviewManager", () => {
}));
fromId.mockReturnValue({
id: 42,
+ hostWebContents: mainWindowWebContents,
isDestroyed: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
@@ -2165,6 +2378,12 @@ describe("PreviewManager", () => {
const states: PreviewManager.PreviewTabState[] = [];
const recordingFrames: DesktopPreviewRecordingFrame[] = [];
+ yield* manager.setMainWindow({
+ isDestroyed: () => false,
+ once: vi.fn(),
+ webContents: mainWindowWebContents,
+ } as never);
+
yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
@@ -2179,6 +2398,7 @@ describe("PreviewManager", () => {
yield* manager.registerWebview("tab_pip", 42);
yield* manager.openPictureInPicture("tab_pip");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
expect(browserWindowConstructor).toHaveBeenCalledWith(
expect.objectContaining({
alwaysOnTop: true,
@@ -2224,6 +2444,7 @@ describe("PreviewManager", () => {
expect(recordingFrames).toHaveLength(1);
yield* manager.stopRecording("tab_pip");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
const framesBeforePictureInPictureOnlyTick = pictureInPictureSend.mock.calls.length;
yield* TestClock.adjust(100);
expect(capturePage).toHaveBeenCalledTimes(3);
@@ -2232,7 +2453,11 @@ describe("PreviewManager", () => {
);
expect(recordingFrames).toHaveLength(1);
+ setBackgroundThrottling.mockImplementationOnce(() => {
+ throw new Error("picture-in-picture throttling restore failed");
+ });
yield* manager.closePictureInPicture("tab_pip");
+ expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true], [true]]);
expect(pictureInPictureWindow.close).toHaveBeenCalledOnce();
expect(states.at(-1)?.pictureInPicture).toBe(false);
const capturesAfterClose = capturePage.mock.calls.length;
diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts
index abcd71a10..0d90e0175 100644
--- a/apps/desktop/src/preview/Manager.ts
+++ b/apps/desktop/src/preview/Manager.ts
@@ -526,6 +526,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
const pictureInPictureAspectRatiosRef = yield* Ref.make<ReadonlyMap<string, number>>(new Map());
const pictureInPictureMutationSemaphore = yield* Semaphore.make(1);
const closingTabIdsRef = yield* Ref.make<ReadonlySet<string>>(new Set());
+ let frameCaptureWindowOpen = true;
+ let currentMainWindow: BrowserWindow | undefined;
+ let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined;
const tabLifecycleLocks = new Map<
string,
{ readonly semaphore: Semaphore.Semaphore; users: number }
@@ -583,35 +586,67 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
),
);
});
+ const setWindowBackgroundThrottling = Effect.fnUntraced(function* (
+ window: BrowserWindow,
+ enabled: boolean,
+ ) {
+ if (window.isDestroyed()) return;
+ yield* attempt({ operation: "frameCapture.setBackgroundThrottling" }, () =>
+ window.webContents.setBackgroundThrottling(enabled),
+ );
+ });
+ const setFrameCaptureBackgroundThrottling = Effect.fnUntraced(function* (enabled: boolean) {
+ const mainWindow = yield* Ref.get(mainWindowRef);
+ if (Option.isNone(mainWindow)) return;
+ yield* setWindowBackgroundThrottling(mainWindow.value, enabled);
+ });
const stopFrameCapture = Effect.fn("PreviewManager.stopFrameCapture")(function* (
tabId: string,
consumer: FrameCaptureConsumer,
) {
- const captureScope = yield* SynchronizedRef.modify(frameCaptureSessionsRef, (sessions) => {
- const current = sessions.get(tabId);
- if (!current || !current.consumers.has(consumer)) {
- return [undefined, sessions] as const;
- }
- const consumers = new Set(current.consumers);
- consumers.delete(consumer);
- if (consumers.size > 0) {
- return [
- undefined,
- replaceMap(sessions, (copy) => {
- copy.set(tabId, { ...current, consumers });
- }),
- ] as const;
- }
- return [
- current.scope,
- replaceMap(sessions, (copy) => {
+ yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
+ Effect.gen(function* () {
+ const current = sessions.get(tabId);
+ if (!current || !current.consumers.has(consumer)) {
+ return [undefined, sessions] as const;
+ }
+ const consumers = new Set(current.consumers);
+ consumers.delete(consumer);
+ if (consumers.size > 0) {
+ return [
+ undefined,
+ replaceMap(sessions, (copy) => {
+ copy.set(tabId, { ...current, consumers });
+ }),
+ ] as const;
+ }
+ const remainingSessions = replaceMap(sessions, (copy) => {
copy.delete(tabId);
- }),
- ] as const;
+ });
+ if (remainingSessions.size === 0) {
+ yield* setFrameCaptureBackgroundThrottling(true).pipe(
+ Effect.retry({ times: 2 }),
+ Effect.catch((error) =>
+ Effect.logWarning("Failed to restore preview frame capture throttling.", { error }),
+ ),
+ );
+ }
+ return [current.scope, remainingSessions] as const;
+ }),
+ ).pipe(
+ Effect.flatMap((captureScope) =>
+ captureScope ? Scope.close(captureScope, Exit.void).pipe(Effect.ignore) : Effect.void,
+ ),
+ Effect.uninterruptible,
+ );
+ });
+
+ const stopAllRecordings = Effect.fn("PreviewManager.stopAllRecordings")(function* () {
+ const sessions = yield* SynchronizedRef.get(frameCaptureSessionsRef);
+ yield* Effect.forEach(sessions.keys(), (tabId) => stopFrameCapture(tabId, "recording"), {
+ concurrency: "unbounded",
+ discard: true,
});
- if (captureScope) {
- yield* Scope.close(captureScope, Exit.void).pipe(Effect.ignore);
- }
});
const deliverEvent = (
@@ -1691,10 +1726,32 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
const setMainWindow = Effect.fn("PreviewManager.setMainWindow")(function* (
window: BrowserWindow,
) {
- yield* Ref.set(mainWindowRef, Option.some(window));
- window.once("closed", () => {
- runFork(closeAllPictureInPicture());
- });
+ if (mainWindowCleanupFiber) {
+ yield* Fiber.join(mainWindowCleanupFiber);
+ mainWindowCleanupFiber = undefined;
+ }
+ yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
+ Effect.gen(function* () {
+ if (sessions.size > 0) {
+ yield* setWindowBackgroundThrottling(window, false);
+ }
+ yield* Ref.set(mainWindowRef, Option.some(window));
+ currentMainWindow = window;
+ frameCaptureWindowOpen = true;
+ window.once("closed", () => {
+ if (currentMainWindow !== window) return;
+ currentMainWindow = undefined;
+ frameCaptureWindowOpen = false;
+ mainWindowCleanupFiber = runFork(
+ Effect.all([closeAllPictureInPicture(), stopAllRecordings()], {
+ concurrency: "unbounded",
+ discard: true,
+ }).pipe(Effect.ignore),
+ );
+ });
+ return [undefined, sessions] as const;
+ }),
+ ).pipe(Effect.uninterruptible);
});
const createTabUnlocked = Effect.fn("PreviewManager.createTabUnlocked")(function* (
@@ -2597,6 +2654,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
);
const created = yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) => {
return Effect.gen(function* () {
+ if (!frameCaptureWindowOpen) {
+ return yield* new PreviewMainWindowClosedError({ tabId });
+ }
const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
if (!tab || (yield* Ref.get(closingTabIdsRef)).has(tabId)) {
return yield* new PreviewTabNotFoundError({ tabId });
@@ -2616,6 +2676,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
}),
] as const;
}
+ if (sessions.size === 0) {
+ yield* setFrameCaptureBackgroundThrottling(false);
+ }
const scope = yield* Scope.fork(parentScope, "sequential");
yield* Effect.forkIn(Effect.forever(captureNextFrame), scope);
return [
@@ -2628,7 +2691,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
}),
] as const;
});
- });
+ }).pipe(Effect.uninterruptible);
if (!created) return;
yield* capturePreviewFrame(tabId).pipe(
Effect.catch((error) =>
@@ -3724,6 +3787,15 @@ export class PreviewWebviewNotInitializedError extends Schema.TaggedErrorClass<P
}
}
+export class PreviewMainWindowClosedError extends Schema.TaggedErrorClass<PreviewMainWindowClosedError>()(
+ "PreviewMainWindowClosedError",
+ { tabId: Schema.String },
+) {
+ override get message(): string {
+ return `Cannot start preview frame capture while the main window is closed: ${this.tabId}`;
+ }
+}
+
export class PreviewOperationError extends Schema.TaggedErrorClass<PreviewOperationError>()(
"PreviewOperationError",
{
@@ -3930,6 +4002,7 @@ export const PreviewManagerError = Schema.Union([
PreviewTabNotFoundError,
PreviewWebContentsNotFoundError,
PreviewWebviewNotInitializedError,
+ PreviewMainWindowClosedError,
PreviewOperationError,
PreviewArtifactPathOutsideDirectoryError,
PreviewArtifactImageLoadError,
diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts
index 42ba818ac..cb7741f11 100644
--- a/apps/desktop/src/window/DesktopWindow.test.ts
+++ b/apps/desktop/src/window/DesktopWindow.test.ts
@@ -455,7 +455,7 @@ describe("DesktopWindow", () => {
assert.isUndefined(createdWindowOptions[0]?.x);
assert.isUndefined(createdWindowOptions[0]?.y);
assert.isTrue(createdWindowOptions[0]?.disableAutoHideCursor);
- assert.isFalse(createdWindowOptions[0]?.webPreferences?.backgroundThrottling);
+ assert.isUndefined(createdWindowOptions[0]?.webPreferences?.backgroundThrottling);
assert.deepEqual(fakeWindow.setAutoHideCursor.mock.calls, [[false]]);
assert.deepEqual(fakeWindow.loadURL.mock.calls[0], ["t3code-dev://app/"]);
assert.equal(fakeWindow.openDevTools.mock.calls.length, 1);
diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts
index 9018b9b92..9954875f8 100644
--- a/apps/desktop/src/window/DesktopWindow.ts
+++ b/apps/desktop/src/window/DesktopWindow.ts
@@ -359,7 +359,6 @@ export const make = Effect.gen(function* () {
...getWindowTitleBarOptions(shouldUseDarkColors, environment.platform),
webPreferences: {
preload: environment.preloadPath,
- backgroundThrottling: false,
contextIsolation: true,
nodeIntegration: false,
sandbox: true,