Skip to content

The agent that grows with you

fix(desktop): Webapp preview reads say the page is unreadable; read_window_below refuses cleanly

Teknium974feef5e9e7
diff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.test.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.test.ts
index 4dc2ddd6ce9..35923018082 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-reader.test.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-reader.test.ts
@@ -111,6 +111,28 @@ describe('readActivePreview (read_preview tool)', () => {
})
})
+ it('says a browser-hosted URL tab is unreadable instead of promising a retry', async () => {
+ // The Webapp renders URLs in a sandboxed iframe: no page reader can ever
+ // register, so "retry in a moment" would loop the agent forever.
+ document.documentElement.dataset.hermesDesktopHost = 'browser'
+
+ try {
+ openPreview(urlTarget('https://example.com'))
+
+ const result = await readActivePreview()
+
+ expect(result).toMatchObject({ kind: 'url', surface: 'iframe', text: '', url: 'https://example.com' })
+ expect(result?.note).toContain('web_extract')
+ expect(result?.note).not.toContain('retry')
+
+ openPreview(fileTarget('/work/notes.md'))
+
+ expect(await readActivePreview()).not.toHaveProperty('surface')
+ } finally {
+ delete document.documentElement.dataset.hermesDesktopHost
+ }
+ })
+
it('answers a file tab with its identity and points at read_file', async () => {
openPreview(fileTarget('/work/notes.md'))
diff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.ts
index ef867d69c20..403037ad2d9 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-reader.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-reader.ts
@@ -10,9 +10,12 @@
* a registered reader answers with the live page's text;
* a tab with no reader (a file peek, an artifact) still answers with its
* identity and a note pointing the agent at the tool that reads that content
- * directly (read_file / the conversation's artifact).
+ * directly (read_file / the conversation's artifact). A browser-hosted
+ * Desktop renders URLs in a sandboxed iframe it can never read, so its URL
+ * tabs answer `surface: 'iframe'` with a note naming web_extract instead.
*/
+import { hostHas } from '@/lib/host-capabilities'
import { type PreviewTab, previewTabsFor } from '@/store/preview'
import type { PreviewOwner } from '@/store/preview-ownership'
@@ -41,6 +44,9 @@ export interface PreviewReadResult {
note?: string
path?: string
start: number
+ /** Set to 'iframe' when the page renders in the browser-hosted Desktop's
+ * sandboxed iframe, whose text this renderer cannot read. */
+ surface?: 'iframe'
/** Open preview tabs, set when more than one is mounted. */
tabs?: PreviewReadTabSummary[]
text: string
@@ -58,6 +64,11 @@ interface PreviewPage {
type PageReader = () => Promise<PreviewPage>
+/** A browser host mounts URL previews in a sandboxed (opaque-origin) iframe:
+ * no page reader can ever register, so waiting would never help. */
+const IFRAME_UNREADABLE_NOTE =
+ "Page text isn't readable in browser-hosted Desktop: the page renders in a sandboxed cross-origin iframe. Read this URL with web_extract or browser_exec instead."
+
/** Default + hard cap on one read — a page's innerText can be megabytes, and
* this crosses the gateway into model context. Page with start/count. */
export const PREVIEW_READ_MAX_CHARS = 24_000
@@ -158,15 +169,19 @@ export async function readActivePreview(
}
}
- // No live webview behind the tab (a file peek, an artifact, or a page still
- // booting): answer with the tab's identity so the agent knows what's on
- // screen and which of its own tools reads the content directly.
+ // No live webview behind the tab (a file peek, an artifact, a page still
+ // booting, or a browser host's iframe): answer with the tab's identity so the
+ // agent knows what's on screen and which of its own tools reads the content.
+ const iframe = target.kind === 'url' && !hostHas('webviewPreview')
+
const identity =
target.kind === 'file'
? 'File preview — read the file itself with read_file.'
: target.kind === 'artifact'
? 'Generated artifact — its content is in the conversation that produced it.'
- : 'The page has not finished loading — retry in a moment.'
+ : iframe
+ ? IFRAME_UNREADABLE_NOTE
+ : 'The page has not finished loading — retry in a moment.'
return windowText(
{
@@ -174,6 +189,7 @@ export async function readActivePreview(
kind: target.kind,
note: withMultiNote(identity, multi),
path: target.path,
+ ...(iframe ? { surface: 'iframe' as const } : {}),
title: target.label,
url: target.url
},
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
index 41367d7eadc..ac73fd191c0 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
@@ -172,6 +172,25 @@ describe('preview action request routing', () => {
expect(respond).not.toHaveBeenCalled()
})
+ it('refuses window.read in a browser tab with an error the tool relays', async () => {
+ // A browser tab cannot enumerate OS windows: answer {error, platform} at
+ // once rather than an empty value the tool reports as "did not answer".
+ document.documentElement.dataset.hermesDesktopHost = 'browser'
+
+ try {
+ const { respond } = deliver('window.read', {}, null)
+
+ await Promise.resolve()
+
+ expect(JSON.parse(respond.mock.calls[0][0].value)).toMatchObject({
+ error: expect.stringContaining('desktop app') as string,
+ platform: 'browser'
+ })
+ } finally {
+ delete document.documentElement.dataset.hermesDesktopHost
+ }
+ })
+
it("answers pane reads for a session hosted in one of this window's tiles", async () => {
// The tile session is not the active one, but this window hosts it: its
// panes are here, so an 'ignore' would stall the tool until its deadline.
@@ -397,6 +416,19 @@ describe('preview pop-out forwarding', () => {
expect(requestPopoutPreviewRead).toHaveBeenCalledWith({}, sessionAOwner)
expect(respond.mock.calls[0][0].value).toBe('')
})
+
+ it('reads locally in a browser tab, which has no pop-out to wait on', async () => {
+ document.documentElement.dataset.hermesDesktopHost = 'browser'
+
+ try {
+ const { respond } = deliver('preview.read', { session_id: 'session-a' }, 'session-a')
+
+ await vi.waitFor(() => expect(respond).toHaveBeenCalledTimes(1))
+ expect(requestPopoutPreviewRead).not.toHaveBeenCalled()
+ } finally {
+ delete document.documentElement.dataset.hermesDesktopHost
+ }
+ })
})
describe('preview requests act for the session that asked (#73890)', () => {
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
index 208ce75cb29..e87b0454aa4 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
@@ -15,6 +15,7 @@ import { readActiveTerminal } from '@/app/right-sidebar/terminal/buffer'
import { pendingClarifyToolPayload } from '@/app/session/hooks/use-session-actions/restore-pending-clarify'
import { translateNow } from '@/i18n'
import { restorePendingClarifyToolCall } from '@/lib/chat-messages'
+import { hostHas } from '@/lib/host-capabilities'
import type { PreviewActAction } from '@/lib/preview-act/act-in-page'
import type { TourAction, TourStep } from '@/lib/tour'
import { type AppearanceValues, applyAppearance, THEME_MODES } from '@/store/appearance-apply'
@@ -528,8 +529,10 @@ const previewRead: Handler = ({ request, sessionId }) => {
const opts = { count: num(request.params.count), start: num(request.params.start) }
const owner = previewOwnerFor(sessionId)
+ // A browser host has no pop-out and no readable surface to forward to, so
+ // asking would only wait out the pop-out timeout before the same answer.
void (async () => {
- const result = hasLivePreviewSurface(owner)
+ const result = hasLivePreviewSurface(owner) || !hostHas('webviewPreview')
? await readActivePreview(opts, owner)
: ((await requestPopoutPreviewRead(opts, owner)) ?? (await readActivePreview(opts, owner)))
@@ -617,7 +620,21 @@ const previewAct: Handler = ({ deps, isActiveSession, request, sessionId }) => {
})()
}
+/** read_window_below's `{error, platform}` answer on a browser host: the
+ * agent relays it instead of retrying a call that can never succeed. */
+const BROWSER_WINDOW_READ_REFUSAL = {
+ error:
+ 'read_window_below needs the Hermes desktop app: this session is in a browser tab, which cannot see the OS windows around it.',
+ platform: 'browser'
+}
+
const windowRead: Handler = ({ request }) => {
+ if (!hostHas('osWindowEnumeration')) {
+ answerValue(request, BROWSER_WINDOW_READ_REFUSAL)
+
+ return
+ }
+
// read_window_below tool: main owns native window enumeration. Empty =
// unavailable (older shell without the handler, Wayland, …) — without an
// answer the tool would stall its full 30s deadline.
diff --git a/apps/desktop/src/lib/host-capabilities.ts b/apps/desktop/src/lib/host-capabilities.ts
index c7fd4c3982b..df5eb12b516 100644
--- a/apps/desktop/src/lib/host-capabilities.ts
+++ b/apps/desktop/src/lib/host-capabilities.ts
@@ -39,6 +39,8 @@ export const HOST_CAPABILITIES = Object.freeze({
nativeFolderPicker: 'native',
/** Host files are read through the Electron bridge, not the gateway's REST routes. */
nativeHostFiles: 'native',
+ /** Main can enumerate the OS windows around this one (read_window_below); a browser tab cannot. */
+ osWindowEnumeration: 'native',
/** Preferences are stored per serving origin (and per profile) in this tab. */
originScopedPrefs: 'browser',
/** The profile rail shows its squares (drag, hold-to-recolor) instead of always condensing. */