The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): answer window.read.request with the window below
Brooklyn Nicholsonf22ae72921b1
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 60bc3a019ed..7aa17d8f9ad 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -209,6 +209,7 @@ import {import { formatBlockerMessage, formatProbeFailedMessage, scanVenvBlockers } from './venv-blocker-scan'import { fetchMarketplaceThemes, searchMarketplaceThemes } from './vscode-marketplace'import { createWakeIndicatorWindowController } from './wake-indicator-window'+import { readWindowBelow } from './window-below'import {computeWindowOptions,debounce,@@ -10339,6 +10340,26 @@ ipcMain.handle('hermes:requestMicrophoneAccess', async () => {return systemPreferences.askForMediaAccess('microphone')})+// read_window_below tool: which OS window is directly underneath this one.+// Metadata only (app, title, bounds) — never pixels. On macOS, other apps'+// window titles are gated behind the Screen Recording permission; pass titles+// through only when it is ALREADY granted, and never prompt for it here.+ipcMain.handle('hermes:window:readBelow', async event => {+ const win = BrowserWindow.fromWebContents(event.sender)++ if (!win || win.isDestroyed()) {+ return null+ }++ const titlesAvailable = IS_MAC+ ? systemPreferences.getMediaAccessStatus?.('screen') === 'granted'+ : true+ const [x, y] = win.getPosition()+ const [width, height] = win.getSize()++ return readWindowBelow(process.pid, { x, y, width, height }, titlesAvailable)+})+// Re-route remote-profile session requests to the owning remote backend. Returns// `undefined` when not interceptable (caller takes the normal local path), else// the response. Reads tag the profile as ?profile=<name>; mutations carry it indiff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.tsindex fbce8197951..1695a1ccb81 100644--- a/apps/desktop/electron/preload.ts+++ b/apps/desktop/electron/preload.ts@@ -131,6 +131,7 @@ contextBridge.exposeInMainWorld('hermesDesktop', {api: request => ipcRenderer.invoke('hermes:api', request),notify: payload => ipcRenderer.invoke('hermes:notify', payload),requestMicrophoneAccess: () => ipcRenderer.invoke('hermes:requestMicrophoneAccess'),+ readWindowBelow: () => ipcRenderer.invoke('hermes:window:readBelow'),readFileDataUrl: filePath => ipcRenderer.invoke('hermes:readFileDataUrl', filePath),readFileDataUrlForAttach: filePath => ipcRenderer.invoke('hermes:readFileDataUrlForAttach', filePath),dataUrlReadMax: {diff --git a/apps/desktop/electron/window-below.test.ts b/apps/desktop/electron/window-below.test.tsnew file mode 100644index 00000000000..3f0ce33e3f7--- /dev/null+++ b/apps/desktop/electron/window-below.test.ts@@ -0,0 +1,111 @@+import { describe, expect, it } from 'vitest'++import { pickWindowBelow, type EnumeratedWindow } from './window-below'++const win = (+ pid: number,+ x = 0,+ y = 0,+ width = 800,+ height = 600,+ app = `app-${pid}`+): EnumeratedWindow => ({+ app,+ bounds: { x, y, width, height },+ id: pid * 10,+ pid,+ title: `${app} window`+})++const SELF_PID = 42+const SELF_BOUNDS = { x: 100, y: 100, width: 800, height: 600 }++describe('pickWindowBelow', () => {+ it('picks the first overlapping window behind ours in z-order', () => {+ const chrome = win(1, 120, 120)+ const spotify = win(2, 130, 130)+ const { below, frontmost } = pickWindowBelow(+ [win(SELF_PID, 100, 100), chrome, spotify],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBe(chrome)+ expect(frontmost).toBe(chrome)+ })++ it('skips windows behind ours that do not overlap', () => {+ const elsewhere = win(1, 5000, 5000)+ const covered = win(2, 200, 200)+ const { below } = pickWindowBelow(+ [win(SELF_PID, 100, 100), elsewhere, covered],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBe(covered)+ })++ it('skips our own other windows (same pid) while walking down', () => {+ const secondHermesWindow = win(SELF_PID, 150, 150)+ const target = win(7, 160, 160)+ const { below } = pickWindowBelow(+ [win(SELF_PID, 100, 100), secondHermesWindow, target],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBe(target)+ })++ it('reports frontmost even when nothing overlaps', () => {+ const elsewhere = win(1, 5000, 5000)+ const { below, frontmost } = pickWindowBelow(+ [win(SELF_PID, 100, 100), elsewhere],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBeNull()+ expect(frontmost).toBe(elsewhere)+ })++ it('windows in front of ours are never "below", even overlapping', () => {+ const inFront = win(3, 110, 110)+ const behind = win(4, 120, 120)+ const { below, frontmost } = pickWindowBelow(+ [inFront, win(SELF_PID, 100, 100), behind],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBe(behind)+ expect(frontmost).toBe(inFront)+ })++ it('falls back to overlap-only when our own window is not in the list', () => {+ // macOS omits windows the enumerator cannot see; still answer usefully.+ const chrome = win(1, 120, 120)+ const { below } = pickWindowBelow([chrome], SELF_PID, SELF_BOUNDS)++ expect(below).toBe(chrome)+ })++ it('returns nulls for an empty enumeration', () => {+ const { below, frontmost } = pickWindowBelow([], SELF_PID, SELF_BOUNDS)++ expect(below).toBeNull()+ expect(frontmost).toBeNull()+ })++ it('edge-adjacent bounds do not count as overlap', () => {+ const adjacent = win(1, 900, 100) // starts exactly at our right edge+ const { below } = pickWindowBelow(+ [win(SELF_PID, 100, 100), adjacent],+ SELF_PID,+ SELF_BOUNDS+ )++ expect(below).toBeNull()+ })+})diff --git a/apps/desktop/electron/window-below.ts b/apps/desktop/electron/window-below.tsnew file mode 100644index 00000000000..648895d8653--- /dev/null+++ b/apps/desktop/electron/window-below.ts@@ -0,0 +1,137 @@+// window-below.ts — which OS window sits directly underneath a Hermes window.+//+// Backs the desktop-gated `read_window_below` tool: the renderer receives+// `window.read.request` from the gateway, asks main over IPC, and answers+// with this module's serialized result. Enumeration uses `get-windows`+// (front-to-back z-order on macOS/Windows/Linux-X11); the picking logic is a+// pure function so the OS-specific part stays a thin provider.+//+// Privacy contract (matches the tool schema): metadata only — app, title,+// bounds. Never pixels. On macOS, window titles require the Screen Recording+// permission; we pass titles through only when that permission is ALREADY+// granted and never trigger the prompt for it.++export interface EnumeratedWindow {+ app: string+ bounds: { x: number; y: number; width: number; height: number }+ id: number+ pid: number+ title: string+}++export interface WindowBelowResult {+ frontmost: { app: string; title: string } | null+ note?: string+ platform: string+ window: {+ app: string+ bounds: { x: number; y: number; width: number; height: number }+ id: number+ title: string+ } | null+}++const overlaps = (+ a: EnumeratedWindow['bounds'],+ b: EnumeratedWindow['bounds']+): boolean =>+ a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height++/**+ * Pick the window directly underneath ours from a front-to-back window list.+ *+ * Walks past every window owned by our own process (all Hermes windows share+ * the main process pid), then takes the first other-process window whose+ * bounds overlap ours — "underneath" means visually behind, not merely next+ * in z-order on some other display. `frontmost` is the first other-process+ * window regardless of overlap: the app the user was last working in.+ */+export function pickWindowBelow(+ windows: EnumeratedWindow[],+ selfPid: number,+ selfBounds: EnumeratedWindow['bounds']+): { below: EnumeratedWindow | null; frontmost: EnumeratedWindow | null } {+ const others = windows.filter(w => w.pid !== selfPid)+ const frontmost = others[0] ?? null++ const selfIndex = windows.findIndex(w => w.pid === selfPid)+ const behind = selfIndex === -1 ? others : windows.slice(selfIndex + 1)+ const below = behind.find(w => w.pid !== selfPid && overlaps(w.bounds, selfBounds)) ?? null++ return { below, frontmost }+}++type GetWindowsModule = typeof import('get-windows')++let getWindowsModule: Promise<GetWindowsModule> | null = null++const loadGetWindows = (): Promise<GetWindowsModule> => {+ getWindowsModule ??= import('get-windows')+ return getWindowsModule+}++/**+ * Enumerate windows and serialize the one underneath `selfBounds`.+ *+ * `titlesAvailable` is the macOS Screen Recording grant (pass true on other+ * platforms, where titles are free). Returns null only when enumeration+ * itself is unavailable (Wayland, missing xprop, addon load failure) — the+ * caller turns that into an empty tool answer.+ */+export async function readWindowBelow(+ selfPid: number,+ selfBounds: EnumeratedWindow['bounds'],+ titlesAvailable: boolean+): Promise<WindowBelowResult | null> {+ let raw+ try {+ const { openWindows } = await loadGetWindows()+ raw = await openWindows(+ process.platform === 'darwin'+ ? { accessibilityPermission: false, screenRecordingPermission: titlesAvailable }+ : undefined+ )+ } catch {+ return null+ }++ if (!Array.isArray(raw)) return null++ // get-windows documents openWindows() as front-to-back, and macOS/Windows+ // honor that (CGWindowList / EnumWindows order). Its lib/linux.js, however,+ // iterates `_NET_CLIENT_LIST_STACKING` in raw xprop order, which EWMH+ // defines as bottom-to-top — so the Linux list arrives back-to-front and+ // must be reversed to match. (Verified against get-windows 9.3.0.)+ const ordered = process.platform === 'linux' ? [...raw].reverse() : raw++ const windows: EnumeratedWindow[] = ordered.map(w => ({+ app: w.owner?.name ?? '',+ bounds: {+ x: w.bounds?.x ?? 0,+ y: w.bounds?.y ?? 0,+ width: w.bounds?.width ?? 0,+ height: w.bounds?.height ?? 0+ },+ id: w.id ?? 0,+ pid: w.owner?.processId ?? 0,+ title: w.title ?? ''+ }))++ const { below, frontmost } = pickWindowBelow(windows, selfPid, selfBounds)++ const result: WindowBelowResult = {+ frontmost: frontmost ? { app: frontmost.app, title: frontmost.title } : null,+ platform: process.platform,+ window: below+ ? { app: below.app, bounds: below.bounds, id: below.id, title: below.title }+ : null+ }++ if (process.platform === 'darwin' && !titlesAvailable) {+ result.note =+ 'Window titles are hidden: macOS reveals other apps\u2019 titles only with the ' ++ 'Screen Recording permission, which Hermes does not request for this.'+ }++ return result+}diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.tsindex b0d6a0f57de..2089766568a 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts@@ -1079,6 +1079,21 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {})})}+ } else if (event.type === 'window.read.request') {+ // read_window_below tool: main owns native window enumeration, so ask+ // it over IPC and answer. Empty text = unavailable (no bridge, or+ // enumeration unsupported on this system e.g. Wayland).+ const requestId = typeof payload?.request_id === 'string' ? payload.request_id : ''++ if (requestId) {+ const read = window.hermesDesktop?.readWindowBelow+ void Promise.resolve(read ? read() : null).then(result => {+ void $gateway.get()?.request('window.read.respond', {+ request_id: requestId,+ text: result ? JSON.stringify(result) : ''+ })+ })+ }} else if (event.type === 'agent.terminal.output') {// Live chunk from a background process → its read-only agent terminal tab.writeAgentTerminalChunk(payload?.process_id ?? '', payload?.chunk ?? '')diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.tsindex d33d17f682c..1ff99e37551 100644--- a/apps/desktop/src/global.d.ts+++ b/apps/desktop/src/global.d.ts@@ -132,6 +132,18 @@ declare global {api: <T>(request: HermesApiRequest) => Promise<T>notify: (payload: HermesNotification) => Promise<boolean>requestMicrophoneAccess: () => Promise<boolean>+ /** read_window_below tool: metadata for the OS window directly underneath this one (never pixels). */+ readWindowBelow?: () => Promise<{+ frontmost: { app: string; title: string } | null+ note?: string+ platform: string+ window: {+ app: string+ bounds: { height: number; width: number; x: number; y: number }+ id: number+ title: string+ } | null+ } | null>readFileDataUrl: (filePath: string) => Promise<string>/** Remote non-image attach: higher dedicated cap than preview/Settings default. */readFileDataUrlForAttach?: (filePath: string) => Promise<string>