Skip to content

The agent that grows with you

fix(desktop): close preview tabs by live browser URL

doojiang61a44e971ee8
diff --git a/apps/desktop/src/app/session/hooks/preview-open.test.tsx b/apps/desktop/src/app/session/hooks/preview-open.test.tsx
index 9dba7d68d59..2cc37d717c5 100644
--- a/apps/desktop/src/app/session/hooks/preview-open.test.tsx
+++ b/apps/desktop/src/app/session/hooks/preview-open.test.tsx
@@ -4,7 +4,16 @@ import { useEffect } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { assistantTextPart, type ChatMessage } from '@/lib/chat-messages'
-import { $previewTabs, $previewTarget, closeRightRail, type PreviewTarget } from '@/store/preview'
+import {
+ $browserPages,
+ $previewTabs,
+ $previewTarget,
+ closeRightRail,
+ markBrowserTabPopped,
+ noteBrowserPage,
+ openPreview,
+ type PreviewTarget
+} from '@/store/preview'
import { $activeSessionId, $currentCwd, $messages, $selectedStoredSessionId } from '@/store/session'
import { usePreviewRouting } from './use-preview-routing'
@@ -19,6 +28,10 @@ function fileTarget(path: string): PreviewTarget {
return { kind: 'file', label: path, path, previewKind: 'html', source: path, url: `file://${path}` }
}
+function urlTarget(url: string): PreviewTarget {
+ return { kind: 'url', label: url, source: url, url }
+}
+
let handleEvent: (event: GatewayEvent) => void = () => undefined
function Harness() {
@@ -61,6 +74,7 @@ describe('preview routing', () => {
$activeSessionId.set(RUNTIME_SESSION_ID)
$currentCwd.set('/work')
$messages.set([])
+ $browserPages.set({})
closeRightRail()
window.localStorage.clear()
@@ -73,6 +87,7 @@ describe('preview routing', () => {
afterEach(() => {
cleanup()
$messages.set([])
+ $browserPages.set({})
closeRightRail()
$activeSessionId.set(null)
$selectedStoredSessionId.set(null)
@@ -233,6 +248,42 @@ describe('preview routing', () => {
expect($previewTarget.get()?.path).toBe('/tmp/keep.html')
})
+ it('closes a Browser tab by the page it navigated to', async () => {
+ render(<Harness />)
+ openPreview(urlTarget('https://example.com/start'))
+ const tabId = $previewTabs.get()[0].id
+
+ noteBrowserPage(tabId, {
+ title: 'Dashboard',
+ url: 'https://example.com/dashboard'
+ })
+
+ await emitPreviewClose('https://example.com/dashboard')
+
+ expect($previewTabs.get()).toHaveLength(0)
+ expect(window.localStorage.getItem('hermes.desktop.previewTabs.v2')).toBeNull()
+ })
+
+ it('does not remove a popped Browser tab through the persisted-url fallback', async () => {
+ render(<Harness />)
+ openPreview(urlTarget('https://example.com/start'))
+ const tabId = $previewTabs.get()[0].id
+
+ noteBrowserPage(tabId, {
+ title: 'Dashboard',
+ url: 'https://example.com/dashboard'
+ })
+ markBrowserTabPopped(tabId, true)
+
+ try {
+ await emitPreviewClose('https://example.com/start')
+
+ expect($previewTabs.get().map(tab => tab.id)).toEqual([tabId])
+ } finally {
+ markBrowserTabPopped(tabId, false)
+ }
+ })
+
it('ignores a close from a session that is not the one on screen', async () => {
render(<Harness />)
diff --git a/apps/desktop/src/app/session/hooks/use-preview-routing.ts b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
index f4913a267e8..9dc369c7465 100644
--- a/apps/desktop/src/app/session/hooks/use-preview-routing.ts
+++ b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
@@ -7,7 +7,8 @@ import { reachablePreviewUrl } from '@/lib/preview-reach'
import {
$previewTabs,
beginPreviewServerRestart,
- closePreviewMatching,
+ closeBrowserPreviewMatchingLiveUrl,
+ closeDockedPreviewMatching,
closeRightRail,
completePreviewServerRestart,
openPreview,
@@ -126,10 +127,6 @@ export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestG
return
}
- if (closePreviewMatching(target)) {
- return
- }
-
void normalizeOrLocalPreviewTarget(target, $currentCwd.get() || currentCwd || undefined).then(
async resolved => {
const candidates = [target]
@@ -142,7 +139,9 @@ export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestG
}
}
- closePreviewMatching(...candidates)
+ if (!closeBrowserPreviewMatchingLiveUrl(...candidates)) {
+ closeDockedPreviewMatching(...candidates)
+ }
}
)
diff --git a/apps/desktop/src/store/preview.test.ts b/apps/desktop/src/store/preview.test.ts
index 3c1ec234ac1..ab2289ab98d 100644
--- a/apps/desktop/src/store/preview.test.ts
+++ b/apps/desktop/src/store/preview.test.ts
@@ -2,17 +2,20 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { $rightRailActiveTabId, selectRightRailTab } from './layout'
import {
+ $browserPages,
$previewServerRestart,
$previewServerRestartStatus,
$previewTabs,
$previewTarget,
beginPreviewServerRestart,
+ closeBrowserPreviewMatchingLiveUrl,
closePreviewForSource,
closePreviewMatching,
closeRightRail,
closeRightRailTab,
commitBrowserTabLocation,
newBrowserTab,
+ noteBrowserPage,
openPreview,
previewTabId,
type PreviewTarget,
@@ -35,12 +38,14 @@ function artifactTarget(id: string): PreviewTarget {
describe('preview store', () => {
beforeEach(() => {
+ $browserPages.set({})
$previewServerRestart.set(null)
closeRightRail()
window.localStorage.clear()
})
afterEach(() => {
+ $browserPages.set({})
$previewServerRestart.set(null)
closeRightRail()
window.localStorage.clear()
@@ -263,6 +268,36 @@ describe('preview store', () => {
expect($previewTabs.get()).toHaveLength(0)
})
+ it('closes a Browser tab by its live navigated URL and removes it from persistence', () => {
+ openPreview(urlTarget('https://example.com'))
+ const tabId = $previewTabs.get()[0].id
+
+ noteBrowserPage(tabId, {
+ title: 'Dashboard',
+ url: 'https://example.com/dashboard'
+ })
+
+ expect(closeBrowserPreviewMatchingLiveUrl('https://example.com/dashboard')).toBe(true)
+ expect($previewTabs.get()).toHaveLength(0)
+ expect($browserPages.get()[tabId]).toBeUndefined()
+ expect(window.localStorage.getItem('hermes.desktop.previewTabs.v2')).toBeNull()
+ })
+
+ it('prefers the tab currently showing a URL over one that navigated away from it', () => {
+ openPreview(urlTarget('https://example.com'))
+ const first = $previewTabs.get()[0].id
+ noteBrowserPage(first, { title: 'Elsewhere', url: 'https://elsewhere.example/' })
+
+ newBrowserTab()
+ openPreview(urlTarget('https://example.com'))
+ const second = $previewTabs.get()[1].id
+ noteBrowserPage(second, { title: 'Example', url: 'https://example.com/' })
+
+ expect(closeBrowserPreviewMatchingLiveUrl('https://example.com')).toBe(true)
+ expect($previewTabs.get().map(tab => tab.id)).toEqual([first])
+ expect($browserPages.get()[first]?.url).toBe('https://elsewhere.example/')
+ })
+
it('does not wipe the rail on an empty or unknown close query', () => {
openPreview(fileTarget('/work/keep.html'))
diff --git a/apps/desktop/src/store/preview.ts b/apps/desktop/src/store/preview.ts
index dbfe22a3a64..b4e30dcf8d7 100644
--- a/apps/desktop/src/store/preview.ts
+++ b/apps/desktop/src/store/preview.ts
@@ -662,6 +662,7 @@ export function closeRightRailTab(tabId: string) {
const next = current.filter(tab => tab.id !== tabId)
+ forgetBrowserPage(tabId)
$previewTabs.set(next)
if ($rightRailActiveTabId.get() === tabId) {
@@ -686,17 +687,69 @@ export function closePreviewForSource(source: string): boolean {
return closePreviewMatching(source)
}
-/** Close the first tab whose source, url, or label matches any candidate.
- * Empty candidates are a no-op so a missed match cannot wipe the rail —
- * closing the whole pane is `closeRightRail`. */
-export function closePreviewMatching(...candidates: string[]): boolean {
+/** Close the first docked Browser tab whose current page URL matches.
+ * Browsers keep navigation state outside their persisted target so matching
+ * only target.url misses redirects and in-page navigation. */
+export function closeBrowserPreviewMatchingLiveUrl(...candidates: string[]): boolean {
+ const queries = new Set(
+ candidates
+ .map(value => {
+ try {
+ const url = new URL(value.trim())
+
+ return url.protocol === 'http:' || url.protocol === 'https:' ? url.href : ''
+ } catch {
+ return ''
+ }
+ })
+ .filter(Boolean)
+ )
+
+ if (queries.size === 0) {
+ return false
+ }
+
+ const pages = $browserPages.get()
+ const popped = $poppedBrowserTabIds.get()
+ const tabs = $previewTabs.get()
+ const activeId = $rightRailActiveTabId.get()
+ const ordered = [...tabs.filter(tab => tab.id === activeId), ...tabs.filter(tab => tab.id !== activeId)]
+
+ const tab = ordered.find(item => {
+ if (item.target.kind !== 'url' || popped.has(item.id)) {
+ return false
+ }
+
+ const liveUrl = pages[item.id]?.url
+
+ if (!liveUrl) {
+ return false
+ }
+
+ try {
+ return queries.has(new URL(liveUrl).href)
+ } catch {
+ return false
+ }
+ })
+
+ if (!tab) {
+ return false
+ }
+
+ closeRightRailTab(tab.id)
+
+ return true
+}
+
+function closePreviewMatchingTabs(tabs: PreviewTab[], candidates: string[]): boolean {
const queries = [...new Set(candidates.map(value => value.trim()).filter(Boolean))]
if (queries.length === 0) {
return false
}
- const tab = $previewTabs.get().find(item => {
+ const tab = tabs.find(item => {
const fields = [item.target.source, item.target.url, item.target.label]
return queries.some(query => fields.includes(query))
@@ -711,6 +764,22 @@ export function closePreviewMatching(...candidates: string[]): boolean {
return true
}
+/** Close the first tab whose source, url, or label matches any candidate.
+ * Empty candidates are a no-op so a missed match cannot wipe the rail —
+ * closing the whole pane is `closeRightRail`. */
+export function closePreviewMatching(...candidates: string[]): boolean {
+ return closePreviewMatchingTabs($previewTabs.get(), candidates)
+}
+
+/** Agent-driven close is scoped to the docked rail; an independent Browser
+ * window owns popped tabs and must not lose its backing state here. */
+export function closeDockedPreviewMatching(...candidates: string[]): boolean {
+ const popped = $poppedBrowserTabIds.get()
+ const docked = $previewTabs.get().filter(tab => !popped.has(tab.id))
+
+ return closePreviewMatchingTabs(docked, candidates)
+}
+
/** Artifact tabs can't outlive the registry they read from, so clearing it
* closes them. File and URL tabs re-read from their source and are left alone. */
export function closeArtifactPreviewTabs() {