Skip to content

The agent that grows with you

fix(desktop): explain why a remote agent's localhost URL won't load in the browser pane

Brooklyn Nicholson3e9859beca68
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
index e036591604f..be6b2d95216 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
@@ -189,6 +189,97 @@ describe('PreviewPane console state', () => {
expect(webview.getAttribute('src')).toBe('http://localhost:5174')
})
+ // The webview always runs on THIS machine, so a remote agent's localhost is
+ // a different computer's localhost. The failure is honest but baffling
+ // without saying so.
+ it('explains a failed loopback URL when the agent is on a remote gateway', async () => {
+ $connection.set({ mode: 'remote' } as never)
+
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5173', url: 'http://localhost:5173' }}
+ />
+ )
+ })
+
+ const webview = rendered.container.querySelector('webview') as HTMLElement
+
+ await act(async () => {
+ webview.dispatchEvent(
+ Object.assign(new Event('did-fail-load'), {
+ errorCode: -102,
+ errorDescription: 'ERR_CONNECTION_REFUSED',
+ isMainFrame: true,
+ validatedURL: 'http://localhost:5173'
+ })
+ )
+ })
+
+ await waitFor(() => expect(rendered.container.textContent).toContain('machine running your agent'))
+ })
+
+ it('stays quiet about loopback when the gateway is local', async () => {
+ $connection.set({ mode: 'local' } as never)
+
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5173', url: 'http://localhost:5173' }}
+ />
+ )
+ })
+
+ const webview = rendered.container.querySelector('webview') as HTMLElement
+
+ await act(async () => {
+ webview.dispatchEvent(
+ Object.assign(new Event('did-fail-load'), {
+ errorCode: -102,
+ errorDescription: 'ERR_CONNECTION_REFUSED',
+ isMainFrame: true,
+ validatedURL: 'http://localhost:5173'
+ })
+ )
+ })
+
+ await waitFor(() => expect(rendered.container.textContent).toContain('ERR_CONNECTION_REFUSED'))
+ expect(rendered.container.textContent).not.toContain('machine running your agent')
+ })
+
+ // A public host fails for ordinary reasons; the remote-vs-local distinction
+ // has nothing to do with it.
+ it('stays quiet for a non-loopback host on a remote gateway', async () => {
+ $connection.set({ mode: 'remote' } as never)
+
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ target={{ kind: 'url', label: 'Preview', source: 'https://example.com', url: 'https://example.com' }}
+ />
+ )
+ })
+
+ const webview = rendered.container.querySelector('webview') as HTMLElement
+
+ await act(async () => {
+ webview.dispatchEvent(
+ Object.assign(new Event('did-fail-load'), {
+ errorCode: -105,
+ errorDescription: 'ERR_NAME_NOT_RESOLVED',
+ isMainFrame: true,
+ validatedURL: 'https://example.com'
+ })
+ )
+ })
+
+ await waitFor(() => expect(rendered.container.textContent).toContain('ERR_NAME_NOT_RESOLVED'))
+ expect(rendered.container.textContent).not.toContain('machine running your agent')
+ })
+
it('surfaces a rejected navigation as a load error', async () => {
let rendered!: ReturnType<typeof render>
await act(async () => {
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
index 8c30f38d31d..e8bb6626344 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
@@ -8,6 +8,7 @@ import { type Translations, useI18n } from '@/i18n'
import { isDesktopFsRemoteMode } from '@/lib/desktop-fs'
import { guardGuestPointers } from '@/lib/guest-pointer-guard'
import { openPreviewTargetInBrowser, remoteHtmlPreviewDocument } from '@/lib/local-preview'
+import { isRemoteGateway } from '@/lib/media'
import { rafCoalesce } from '@/lib/raf-coalesce'
import { cn } from '@/lib/utils'
import { notify, notifyError } from '@/store/notifications'
@@ -77,6 +78,30 @@ function loadErrorTitle(error: PreviewLoadErrorState, copy: Translations['previe
return copy.failedToLoad
}
+/** Loopback hosts — the address family that means "this machine", and so the
+ * one family whose meaning changes with WHICH machine is running the page. */
+const LOOPBACK_HOST_RE = /^(localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[?::1\]?)$/i
+
+/**
+ * True when this address can't mean what the agent meant.
+ *
+ * The `<webview>` always runs on the user's own machine, never on the gateway
+ * host. So against a remote gateway, an agent's `localhost:5173` resolves here
+ * — where that port is usually nothing, or worse, somebody else's service. The
+ * URL isn't wrong, it's just addressed to a different computer.
+ */
+function isRemoteLoopbackUrl(url: string): boolean {
+ if (!isRemoteGateway()) {
+ return false
+ }
+
+ try {
+ return LOOPBACK_HOST_RE.test(new URL(url).hostname)
+ } catch {
+ return false
+ }
+}
+
function isModuleMimeError(message: string): boolean {
const lower = message.toLowerCase()
@@ -115,6 +140,9 @@ function PreviewLoadError({
{error.code ? ` (${error.code})` : ''}
</a>
<div className="mt-1 text-[0.6875rem] text-muted-foreground/70">{error.description}</div>
+ {isRemoteLoopbackUrl(error.url) && (
+ <div className="mt-2 text-[0.6875rem] leading-relaxed text-muted-foreground/70">{copy.remoteLoopback}</div>
+ )}
</>
}
consoleHeight={consoleHeight}
diff --git a/apps/desktop/src/i18n/ar.ts b/apps/desktop/src/i18n/ar.ts
index 072b76d3375..06d7370adf8 100644
--- a/apps/desktop/src/i18n/ar.ts
+++ b/apps/desktop/src/i18n/ar.ts
@@ -2282,6 +2282,8 @@ export const ar = defineLocale({
web: {
appFailedToBoot: 'فشل إقلاع تطبيق المعاينة',
serverNotFound: 'الخادم غير موجود',
+ remoteLoopback:
+ 'يشير هذا العنوان إلى الجهاز الذي يشغّل الوكيل، وليس هذا الجهاز. تحمّل لوحة المتصفح الصفحات محليًا، لذا يحتاج خادم التطوير البعيد إلى إعادة توجيه منفذ أو اسم مضيف يمكن الوصول إليه.',
failedToLoad: 'فشل تحميل المعاينة',
tryAgain: 'إعادة المحاولة',
restarting: 'جار إعادة تشغيل Hermes...',
diff --git a/apps/desktop/src/i18n/en.ts b/apps/desktop/src/i18n/en.ts
index 11f147b19f0..d87a8be1a12 100644
--- a/apps/desktop/src/i18n/en.ts
+++ b/apps/desktop/src/i18n/en.ts
@@ -2875,6 +2875,8 @@ export const en: Translations = {
web: {
appFailedToBoot: 'Preview app failed to boot',
serverNotFound: 'Server not found',
+ remoteLoopback:
+ 'This address points at the machine running your agent, not this one. The browser pane loads pages locally, so a remote dev server needs a port forward or a reachable hostname.',
failedToLoad: 'Preview failed to load',
tryAgain: 'Try again',
restarting: 'Hermes is restarting...',
diff --git a/apps/desktop/src/i18n/ja.ts b/apps/desktop/src/i18n/ja.ts
index 60de409fc66..b9c7cf6d55e 100644
--- a/apps/desktop/src/i18n/ja.ts
+++ b/apps/desktop/src/i18n/ja.ts
@@ -2567,6 +2567,8 @@ export const ja = defineLocale({
web: {
appFailedToBoot: 'プレビューアプリの起動に失敗しました',
serverNotFound: 'サーバーが見つかりません',
+ remoteLoopback:
+ 'このアドレスはエージェントを実行しているマシンを指しており、このマシンではありません。ブラウザペインはページをローカルで読み込むため、リモートの開発サーバーにはポート転送か到達可能なホスト名が必要です。',
failedToLoad: 'プレビューの読み込みに失敗しました',
tryAgain: '再試行',
restarting: 'Hermes を再起動中...',
diff --git a/apps/desktop/src/i18n/types.ts b/apps/desktop/src/i18n/types.ts
index ef6b565fb14..6518c53cc8e 100644
--- a/apps/desktop/src/i18n/types.ts
+++ b/apps/desktop/src/i18n/types.ts
@@ -2451,6 +2451,7 @@ export interface Translations {
web: {
appFailedToBoot: string
serverNotFound: string
+ remoteLoopback: string
failedToLoad: string
tryAgain: string
restarting: string
diff --git a/apps/desktop/src/i18n/zh-hant.ts b/apps/desktop/src/i18n/zh-hant.ts
index 5e7b702ee2b..020b8180efb 100644
--- a/apps/desktop/src/i18n/zh-hant.ts
+++ b/apps/desktop/src/i18n/zh-hant.ts
@@ -2483,6 +2483,7 @@ export const zhHant = defineLocale({
web: {
appFailedToBoot: '預覽應用程式啟動失敗',
serverNotFound: '找不到伺服器',
+ remoteLoopback: '這個位址指向執行代理的那台機器,而不是本機。瀏覽器窗格會在本機載入頁面,因此遠端開發伺服器需要連接埠轉送或可連線的主機名稱。',
failedToLoad: '預覽載入失敗',
tryAgain: '重試',
restarting: 'Hermes 正在重新啟動...',
diff --git a/apps/desktop/src/i18n/zh.ts b/apps/desktop/src/i18n/zh.ts
index 897c18684d4..76873b2f6f5 100644
--- a/apps/desktop/src/i18n/zh.ts
+++ b/apps/desktop/src/i18n/zh.ts
@@ -3043,6 +3043,7 @@ export const zh: Translations = {
web: {
appFailedToBoot: '预览应用启动失败',
serverNotFound: '未找到服务器',
+ remoteLoopback: '该地址指向运行代理的那台机器,而不是本机。浏览器面板在本地加载页面,因此远程开发服务器需要端口转发或可访问的主机名。',
failedToLoad: '预览加载失败',
tryAgain: '重试',
restarting: 'Hermes 正在重启...',