A desktop coding agent with free access to frontier models.
TypeScript
fix: keep browser running in background
ogsamrat618e373d6e56
diff --git a/apps/desktop/electron/background-rendering.mjs b/apps/desktop/electron/background-rendering.mjsnew file mode 100644index 00000000..d14063a8--- /dev/null+++ b/apps/desktop/electron/background-rendering.mjs@@ -0,0 +1,31 @@+// Chromium switches that keep pages rendering while the app is behind other+// windows. Windows' native occlusion tracking marks a covered window hidden+// and stops its frames even when backgroundThrottling is off, so the built-in+// browser stalls (no animation frames, observers or screenshots) until the+// user comes back to the app.++export const WINDOWS_OCCLUSION_FEATURE = "CalculateNativeWinOcclusion";++/** Add features to a comma-separated --disable-features value without duplicates. */+export function mergeFeatureList(existing, features) {+ const list = String(existing ?? "")+ .split(",")+ .map((item) => item.trim())+ .filter(Boolean);+ for (const feature of features) {+ if (!list.includes(feature)) list.push(feature);+ }+ return list.join(",");+}++/**+ * Apply the switches before `ready`. `commandLine` is `app.commandLine`.+ * Merges with a --disable-features value set earlier (launch arguments), since+ * Chromium reads only one value for the switch.+ */+export function applyBackgroundRenderingSwitches(commandLine, platform = process.platform) {+ if (platform !== "win32") return null;+ const value = mergeFeatureList(commandLine.getSwitchValue("disable-features"), [WINDOWS_OCCLUSION_FEATURE]);+ commandLine.appendSwitch("disable-features", value);+ return value;+}diff --git a/apps/desktop/electron/background-rendering.test.mjs b/apps/desktop/electron/background-rendering.test.mjsnew file mode 100644index 00000000..9c36dfca--- /dev/null+++ b/apps/desktop/electron/background-rendering.test.mjs@@ -0,0 +1,42 @@+import assert from "node:assert/strict";+import test from "node:test";++import { applyBackgroundRenderingSwitches, mergeFeatureList, WINDOWS_OCCLUSION_FEATURE } from "./background-rendering.mjs";++function fakeCommandLine(initial = {}) {+ const switches = new Map(Object.entries(initial));+ return {+ switches,+ getSwitchValue(name) { return switches.get(name) ?? ""; },+ appendSwitch(name, value) { switches.set(name, value); },+ };+}++test("merging keeps existing features, trims blanks and skips duplicates", () => {+ assert.equal(mergeFeatureList("", ["A"]), "A");+ assert.equal(mergeFeatureList(undefined, ["A"]), "A");+ assert.equal(mergeFeatureList(" X , ,Y", ["A", "X"]), "X,Y,A");+ assert.equal(mergeFeatureList("A", ["A"]), "A");+});++test("Windows turns off native occlusion so covered windows keep rendering", () => {+ const commandLine = fakeCommandLine();+ assert.equal(applyBackgroundRenderingSwitches(commandLine, "win32"), WINDOWS_OCCLUSION_FEATURE);+ assert.equal(commandLine.switches.get("disable-features"), "CalculateNativeWinOcclusion");+});++test("Windows keeps features disabled by launch arguments", () => {+ const commandLine = fakeCommandLine({ "disable-features": "SomethingElse" });+ applyBackgroundRenderingSwitches(commandLine, "win32");+ assert.equal(commandLine.switches.get("disable-features"), "SomethingElse,CalculateNativeWinOcclusion");+});++test("other platforms leave the command line alone", () => {+ /** @type {NodeJS.Platform[]} */+ const platforms = ["darwin", "linux"];+ for (const platform of platforms) {+ const commandLine = fakeCommandLine();+ assert.equal(applyBackgroundRenderingSwitches(commandLine, platform), null);+ assert.equal(commandLine.switches.size, 0);+ }+});diff --git a/apps/desktop/electron/browser-panel.mjs b/apps/desktop/electron/browser-panel.mjsindex 481d7c3a..c8104b1f 100644--- a/apps/desktop/electron/browser-panel.mjs+++ b/apps/desktop/electron/browser-panel.mjs@@ -140,6 +140,9 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, checonst suspendedTabs = new Map();const registry = createBrowserTabRegistry();let browserViewVisible = false;+ // The main window is minimized or hidden: nothing of it reaches the screen,+ // and Chromium stops producing frames for every view attached to it.+ let mainWindowConcealed = false;let backgroundWindow = null;// Last browser panel bounds reported by the renderer, in renderer CSS pixels.// Converted to window device-independent pixels at every setBounds call.@@ -1127,10 +1130,30 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, che});}+ /**+ * Whether the main window can show the browser panel right now: the panel+ * is open with real bounds and the window is neither minimized nor hidden.+ */+ function panelOnScreen() {+ return browserViewVisible && !mainWindowConcealed && !!window()+ && !!lastBrowserBounds && lastBrowserBounds.width > 0 && lastBrowserBounds.height > 0;+ }++ /**+ * The visible conversation's active tab may be driven by an agent while the+ * panel is closed or the window is minimized. A detached view, or one in a+ * minimized window, produces no frames: animation frames, observers and+ * screenshots stall until the user comes back. Park that tab in the hidden+ * host instead, like a background conversation's tab.+ */+ function parksActiveTab(tab) {+ return !panelOnScreen() && tab.tabId === registry.onScreenTabId() && !tab.suspending;+ }+/** Re-evaluate every tab after the on-screen conversation changed. */function applySurfacing() {for (const tab of browserTabs.values()) {- if (registry.surfacingFor(tab.tabId) === "background") enterBackgroundMode(tab);+ if (registry.surfacingFor(tab.tabId) === "background" || parksActiveTab(tab)) enterBackgroundMode(tab);else exitBackgroundMode(tab);}releaseEmptyBackgroundWindow();@@ -1220,8 +1243,13 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, chefunction attachActiveBrowserView() {const mainWindow = window();- if (!mainWindow || !browserViewVisible) return;- if (!lastBrowserBounds || lastBrowserBounds.width <= 0 || lastBrowserBounds.height <= 0) return;+ if (!mainWindow) return;+ if (!panelOnScreen()) {+ // Off screen: keep the active tab live in the hidden host (applySurfacing).+ detachIdleBrowserViews(getActiveBrowserView());+ applySurfacing();+ return;+ }const tab = getBrowserTab();if (!tab) { detachIdleBrowserViews(); return; }exitBackgroundMode(tab);@@ -1232,6 +1260,7 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, cheif (!mainWindow.contentView.children.includes(tab.view)) {mainWindow.contentView.addChildView(tab.view);}+ releaseEmptyBackgroundWindow();}function selectBrowserTab(tabId) {@@ -1245,6 +1274,8 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, cheif (previousView && previousView !== tab.view && !tabForView(previousView)?.background) {detachBrowserView(previousView);}+ // A previously parked active tab leaves the hidden host.+ applySurfacing();attachActiveBrowserView();}sendBrowserState();@@ -1460,7 +1491,22 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, chehideMenuOverlay();browserViewVisible = false;if (!window()) return;- detachIdleBrowserViews();+ attachActiveBrowserView();+ }++ /**+ * The main window was minimized, hidden, restored or shown. While nothing+ * of it is on screen, the displayed tab moves to the hidden host so an+ * agent driving it keeps a live, paintable page; it returns on restore.+ */+ function setMainWindowConcealed(concealed) {+ const next = concealed === true;+ if (next === mainWindowConcealed) return;+ mainWindowConcealed = next;+ if (next) hideMenuOverlay();+ applySurfacing();+ attachActiveBrowserView();+ sendBrowserState();}function destroyBrowserView() {@@ -1632,6 +1678,7 @@ export function createBrowserPanel({ getWindow, remoteDebugPort, onDeepLink, chereturn {destroy: destroyBrowserView,+ setMainWindowConcealed,isMainWindowAllowedNavigation,setMainWindowEntryUrl,allowMainWindowInternalUrl,diff --git a/apps/desktop/electron/browser-panel.test.mjs b/apps/desktop/electron/browser-panel.test.mjsindex a55049f8..c9a9b34f 100644--- a/apps/desktop/electron/browser-panel.test.mjs+++ b/apps/desktop/electron/browser-panel.test.mjs@@ -258,6 +258,7 @@ test("showing the panel sizes the active tab and resets viewport emulation leftinvoke("omnirush:browser:createTab", "https://example.com");assert.equal(onScreen(), null, "a tab created while the panel is hidden stays off screen");await flush();+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 1, "it keeps painting in the hidden host");invoke("omnirush:browser:show", PANEL_BOUNDS);await flush();@@ -265,7 +266,9 @@ test("showing the panel sizes the active tab and resets viewport emulation leftconst view = onScreen();assert.ok(view, "the active tab is attached to the window");assert.deepEqual(view.getBounds(), PANEL_BOUNDS);- assert.deepEqual(commands(view), RESET_SEQUENCE);+ // Leaving the hidden host clears the parked viewport, which is the reset.+ assert.deepEqual(commands(view), [...BACKGROUND_SEQUENCE, ...FOREGROUND_SEQUENCE]);+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 0, "the hidden host is released");assert.equal(view.webContents.debugger.isAttached(), false, "the temporary debugger session is released");});@@ -489,6 +492,9 @@ test("failed navigation rolls back its allocation while another owner's page surawait assert.rejects(opening, { code: "browser_operation_failed" });invoke("omnirush:browser:setVisibleSession", "A");assert.deepEqual(invoke("omnirush:browser:state").tabs, before.tabs);+ // B's empty panel closed; A's active tab stays live in the hidden host until A's panel shows again.+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 1);+ invoke("omnirush:browser:show", PANEL_BOUNDS, "A");assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 0);assert.equal(views()[1].webContents.isDestroyed(), true);assert.equal(views()[0].webContents.isDestroyed(), false);@@ -1443,3 +1449,52 @@ test("the main window only shows the app document and pages the main process loaassert.equal(panel.isMainWindowAllowedNavigation("http://localhost:5173/#/settings"), true);assert.equal(panel.isMainWindowAllowedNavigation("http://localhost:5174/"), false);});++test("closing the panel keeps the active tab painting in the hidden host and showing it again restores it", async () => {+ const { invoke, onScreen, children, commands } = createPanel();+ invoke("omnirush:browser:show", PANEL_BOUNDS, "A");+ invoke("omnirush:browser:createTab", "https://one.example", "A");+ const view = onScreen();+ assert.ok(view);+ await flush();+ commands(view).length = 0;++ invoke("omnirush:browser:hide");+ await flush();+ assert.equal(children.includes(view), false, "nothing paints over the app");+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 1);+ assert.deepEqual(view.getBounds(), { x: 0, y: 0, width: 1280, height: 800 });+ assert.deepEqual(commands(view), BACKGROUND_SEQUENCE, "the page keeps a real viewport and focus while unseen");++ invoke("omnirush:browser:show", PANEL_BOUNDS, "A");+ await flush();+ assert.equal(onScreen(), view);+ assert.deepEqual(view.getBounds(), PANEL_BOUNDS);+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 0);+});++test("minimizing the window parks the displayed tab and restoring brings it back", async () => {+ const { invoke, onScreen, children, panel } = createPanel();+ invoke("omnirush:browser:show", PANEL_BOUNDS, "A");+ invoke("omnirush:browser:createTab", "https://one.example", "A");+ const other = invoke("omnirush:browser:createTab", "https://two.example", "A");+ const view = onScreen();+ await flush();++ panel.setMainWindowConcealed(true);+ await flush();+ assert.equal(onScreen(), null, "a minimized window holds no browser view");+ assert.equal(children.includes(view), false);+ const state = invoke("omnirush:browser:state");+ assert.equal(state.backgroundWindowCount, 1, "only the displayed tab is kept live");+ assert.equal(state.activeTabId, other.tabId, "the selection is unchanged");++ panel.setMainWindowConcealed(true);+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 1, "repeated events are idempotent");++ panel.setMainWindowConcealed(false);+ await flush();+ assert.equal(onScreen(), view);+ assert.deepEqual(view.getBounds(), PANEL_BOUNDS);+ assert.equal(invoke("omnirush:browser:state").backgroundWindowCount, 0);+});diff --git a/apps/desktop/electron/main.mjs b/apps/desktop/electron/main.mjsindex b92ed4b0..11a91c3f 100644--- a/apps/desktop/electron/main.mjs+++ b/apps/desktop/electron/main.mjs@@ -41,6 +41,7 @@ import { createApplicationMenu } from "./app-menu.mjs";import { applyBrandAppName } from "./brand-app-name.mjs";import { createBrowserLoginSync } from "./browser-login-sync.mjs";import { createBrowserPanel } from "./browser-panel.mjs";+import { applyBackgroundRenderingSwitches } from "./background-rendering.mjs";import { createWorkspaceStore } from "./workspace-store.mjs";import {START_REPORT_FILE,@@ -1088,6 +1089,8 @@ if (extraLaunchArgs) {}}}+// Keep the built-in browser rendering while the app is behind other windows.+applyBackgroundRenderingSwitches(app.commandLine);// Linux: Chromium falls back to its insecure basic_text store when it cannot// name the desktop (Hyprland, sway, i3, ...), even with gnome-keyring,// KeePassXC or KWallet on the session bus. Point it at that keyring before@@ -3143,6 +3146,18 @@ async function createMainWindow() {});});+ // Minimized or hidden windows produce no frames; the built-in browser parks+ // its displayed tab in a hidden host meanwhile so agent work keeps going.+ const syncBrowserWindowPresence = () => {+ const win = mainWindow;+ if (!win || win.isDestroyed()) return;+ browserPanel.setMainWindowConcealed(win.isMinimized() || !win.isVisible());+ };+ mainWindow.on("minimize", syncBrowserWindowPresence);+ mainWindow.on("restore", syncBrowserWindowPresence);+ mainWindow.on("hide", syncBrowserWindowPresence);+ mainWindow.on("show", syncBrowserWindowPresence);+mainWindow.on("closed", () => {browserPanel.destroy();mainWindow = null;diff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex 99c611b2..c36bb5c1 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -27,7 +27,7 @@"prepare:sidecar": "node ./scripts/prepare-sidecar.mjs","pretest": "pnpm --filter @omnirush/headless-threads build","test:omnirush-account": "node --test electron/omnirush-account.test.mjs",- "test": "node --test scripts/prepare-server-constants.test.mjs scripts/verify-update-manifest.test.mjs electron/after-pack-paths.test.mjs electron/agent-context-diagnostics-fetch.test.mjs electron/automation-runner.test.mjs electron/binary-transfer.test.mjs electron/blank-slate-profile.test.mjs electron/blank-slate-profile-isolation.test.mjs electron/brand-app-name.test.mjs electron/brand-icon-darwin.test.mjs electron/brand-icon-windows.test.mjs electron/browser-login-sync.test.mjs electron/browser-panel.test.mjs electron/connect-link.test.mjs electron/desktop-distribution.test.mjs electron/dev-profile.test.mjs electron/electron-builder-config.test.mjs electron/linux-desktop-integration.test.mjs electron/linux-password-store.test.mjs electron/logs-folder.test.mjs electron/media-permissions.test.mjs electron/no-bare-external-fetch.test.mjs electron/nuke.test.mjs electron/omnirush-account.test.mjs electron/omnirush-ui-mcp.test.mjs electron/open-external.test.mjs electron/open-local-path.test.mjs electron/process-resilience.test.mjs electron/runtime.test.mjs electron/runtime-ca.test.mjs electron/system-ca-trust.test.mjs electron/external-fetch.test.mjs electron/runtime-chain-repair.test.mjs electron/sentry.test.mjs electron/server-dependency-mirror.test.mjs electron/skill-folder.test.mjs electron/secure-vault-key.test.mjs electron/stdio-errors.test.mjs electron/terminal-sessions.test.mjs electron/ui-control-server.test.mjs electron/updater.test.mjs electron/update-gate.test.mjs electron/quality.test.mjs electron/buildathon.test.mjs electron/build-variant.test.mjs electron/placements-desktop.test.mjs electron/feedback-desktop.test.mjs electron/turn-guard.test.mjs electron/keep-awake.test.mjs electron/computer-use-answers.test.mjs electron/remote-workspace.test.mjs electron/workspace-archive.test.mjs electron/workspace-store.test.mjs electron/start-report.test.mjs electron/start-reset.test.mjs electron/start-store-check.test.mjs electron/app-request-secret.test.mjs",+ "test": "node --test scripts/prepare-server-constants.test.mjs scripts/verify-update-manifest.test.mjs electron/after-pack-paths.test.mjs electron/agent-context-diagnostics-fetch.test.mjs electron/automation-runner.test.mjs electron/binary-transfer.test.mjs electron/blank-slate-profile.test.mjs electron/blank-slate-profile-isolation.test.mjs electron/brand-app-name.test.mjs electron/brand-icon-darwin.test.mjs electron/brand-icon-windows.test.mjs electron/browser-login-sync.test.mjs electron/browser-panel.test.mjs electron/background-rendering.test.mjs electron/connect-link.test.mjs electron/desktop-distribution.test.mjs electron/dev-profile.test.mjs electron/electron-builder-config.test.mjs electron/linux-desktop-integration.test.mjs electron/linux-password-store.test.mjs electron/logs-folder.test.mjs electron/media-permissions.test.mjs electron/no-bare-external-fetch.test.mjs electron/nuke.test.mjs electron/omnirush-account.test.mjs electron/omnirush-ui-mcp.test.mjs electron/open-external.test.mjs electron/open-local-path.test.mjs electron/process-resilience.test.mjs electron/runtime.test.mjs electron/runtime-ca.test.mjs electron/system-ca-trust.test.mjs electron/external-fetch.test.mjs electron/runtime-chain-repair.test.mjs electron/sentry.test.mjs electron/server-dependency-mirror.test.mjs electron/skill-folder.test.mjs electron/secure-vault-key.test.mjs electron/stdio-errors.test.mjs electron/terminal-sessions.test.mjs electron/ui-control-server.test.mjs electron/updater.test.mjs electron/update-gate.test.mjs electron/quality.test.mjs electron/buildathon.test.mjs electron/build-variant.test.mjs electron/placements-desktop.test.mjs electron/feedback-desktop.test.mjs electron/turn-guard.test.mjs electron/keep-awake.test.mjs electron/computer-use-answers.test.mjs electron/remote-workspace.test.mjs electron/workspace-archive.test.mjs electron/workspace-store.test.mjs electron/start-report.test.mjs electron/start-reset.test.mjs electron/start-store-check.test.mjs electron/app-request-secret.test.mjs","test:core": "node --test scripts/prepare-server-constants.test.mjs electron/workspace-store.test.mjs electron/workspace-archive.test.mjs electron/connect-link.test.mjs electron/open-external.test.mjs electron/open-local-path.test.mjs electron/secure-vault-key.test.mjs electron/process-resilience.test.mjs electron/stdio-errors.test.mjs electron/runtime-ca.test.mjs electron/system-ca-trust.test.mjs electron/external-fetch.test.mjs electron/runtime-chain-repair.test.mjs electron/automation-runner.test.mjs","pretest:core": "pnpm --filter @omnirush/headless-threads build"},