The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep the agent's read_preview / drive_preview inside the session's tabs
teknium18e737698bcc0
diff --git a/apps/desktop/src/app/chat/right-rail/preview-input.ts b/apps/desktop/src/app/chat/right-rail/preview-input.tsindex 7b6ed80260f..f7c023b7c98 100644--- a/apps/desktop/src/app/chat/right-rail/preview-input.ts+++ b/apps/desktop/src/app/chat/right-rail/preview-input.ts@@ -20,7 +20,7 @@*/import { $rightRailActiveTabId } from '@/store/layout'-import { $previewTabs } from '@/store/preview'+import { previewTabsFor } from '@/store/preview'/** The subset of Electron's input events the agent needs to drive a page. */export type PreviewInputEvent =@@ -61,7 +61,7 @@ export function registerPreviewInput(tabId: string, handle: PreviewInputHandle):/** The ACTIVE preview tab's input channel. Null = nothing real to drive, and* the caller falls back to synthesizing events inside the page. */export function activePreviewInput(): PreviewInputHandle | null {- const tabs = $previewTabs.get()+ const tabs = previewTabsFor()const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0]return (tab && handles.get(tab.id)) || nulldiff --git a/apps/desktop/src/app/chat/right-rail/preview-nav.ts b/apps/desktop/src/app/chat/right-rail/preview-nav.tsindex dfe5b08dc7c..6e455fded0c 100644--- a/apps/desktop/src/app/chat/right-rail/preview-nav.ts+++ b/apps/desktop/src/app/chat/right-rail/preview-nav.ts@@ -11,7 +11,7 @@import { isElementInHiddenPane } from '@/components/pane-shell/pane-visibility'import { $rightRailActiveTabId } from '@/store/layout'-import { $previewTabs } from '@/store/preview'+import { previewTabsFor } from '@/store/preview'/** Marks a live browser pane so a gesture can find the one holding focus. */export const PREVIEW_BROWSER_ATTR = 'data-preview-browser'@@ -38,7 +38,7 @@ export function registerPreviewNav(tabId: string, handle: PreviewNavHandle): ()/** The ACTIVE preview tab's commands, for callers with no focus to key off —* the agent's drive_preview, which runs while focus is in the composer. */export function activePreviewNav(): PreviewNavHandle | null {- const tabs = $previewTabs.get()+ const tabs = previewTabsFor()const tab = tabs.find(t => t.id === $rightRailActiveTabId.get()) ?? tabs[0]return (tab && handles.get(tab.id)) || nulldiff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.test.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.test.tsindex e59dc0e18b8..042541fcc58 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@@ -4,10 +4,18 @@ import { group, split } from '@/components/pane-shell/tree/model'import { $layoutTree, noteActiveTreeGroup, noteHoveredTreeGroup } from '@/components/pane-shell/tree/store'import { $rightRailActiveTabId, selectRightRailTab } from '@/store/layout'import { $previewTabs, closeRightRail, openPreview, type PreviewTarget } from '@/store/preview'+import { $selectedStoredSessionId } from '@/store/session'import { watchPreviewTiles } from '../preview-tile'-import { PREVIEW_READ_MAX_CHARS, readActivePreview, registerPreviewPageReader } from './preview-reader'+import { activePreviewInput, registerPreviewInput } from './preview-input'+import { activePreviewNav, registerPreviewNav } from './preview-nav'+import {+ PREVIEW_READ_MAX_CHARS,+ readActivePreview,+ registerPreviewPageReader,+ resolveActivePreviewTab+} from './preview-reader'function urlTarget(url: string): PreviewTarget {return { kind: 'url', label: 'Browser', source: url, url }@@ -199,6 +207,45 @@ describe('readActivePreview (read_preview tool)', () => {})})+describe('agent preview reads stay inside the session (#73890)', () => {+ beforeEach(() => {+ closeRightRail()+ window.localStorage.clear()+ noteActiveTreeGroup(null)+ noteHoveredTreeGroup(null)+ $layoutTree.set(null)+ $selectedStoredSessionId.set(null)+ })++ it('never answers with, or lists, another session hidden tab', async () => {+ $selectedStoredSessionId.set('sess-a')+ openPreview(fileTarget('/work/secret-a.txt'))+ const hidden = $previewTabs.get()[0]!.id+ const unbindNav = registerPreviewNav(hidden, { back: () => {}, forward: () => {}, reload: () => {} })+ const unbindInput = registerPreviewInput(hidden, { focus: () => {}, send: () => {} })++ try {+ $selectedStoredSessionId.set('sess-b')+ openPreview(fileTarget('/work/b1.txt'))+ openPreview(fileTarget('/work/b2.txt'))+ // No visible selection: the fallback must stay in sess-b's drawer.+ selectRightRailTab(null)++ expect(resolveActivePreviewTab()?.target.path).toMatch(/^\/work\/b/)+ expect(activePreviewNav()).toBeNull()+ expect(activePreviewInput()).toBeNull()++ const read = await readActivePreview()++ expect(read?.path).toMatch(/^\/work\/b/)+ expect(JSON.stringify(read)).not.toContain('secret-a')+ } finally {+ unbindNav()+ unbindInput()+ }+ })+})+describe('follow() does not overwrite an explicit open in another group', () => {beforeEach(() => {closeRightRail()diff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.tsindex 76c380eb524..f47ff0aedfe 100644--- a/apps/desktop/src/app/chat/right-rail/preview-reader.ts+++ b/apps/desktop/src/app/chat/right-rail/preview-reader.ts@@ -16,7 +16,7 @@import { findGroup } from '@/components/pane-shell/tree/model'import { $activeTreeGroup, $hoveredTreeGroup, $layoutTree } from '@/components/pane-shell/tree/store'import { $rightRailActiveTabId } from '@/store/layout'-import { $previewTabs, type PreviewTab } from '@/store/preview'+import { type PreviewTab, previewTabsFor } from '@/store/preview'import { explicitOpenBlocksZone, PREVIEW_TILE_PREFIX } from '@/store/preview-explicit'import { nudgeOverlay } from './preview-nudge'@@ -121,7 +121,7 @@ function openTabInGroup(groupId: null | string, tabs: PreviewTab[]): null | Prev* an explicit open living in a different group — that is follow()'s clobber,* not a look.*/-export function resolveActivePreviewTab(tabs: PreviewTab[] = $previewTabs.get()): null | PreviewTab {+export function resolveActivePreviewTab(tabs: PreviewTab[] = previewTabsFor()): null | PreviewTab {if (tabs.length === 0) {return null}@@ -165,9 +165,14 @@ function withMultiNote(note: string | undefined, multi: boolean): string | undefreturn note ? `${note} ${extra}` : extra}-/** Read the preview the user is looking at. Null only when no tab is open at all. */-export async function readActivePreview(opts: PreviewReadOptions = {}): Promise<null | PreviewReadResult> {- const tabs = $previewTabs.get()+/** Read the preview the user is looking at, among the tabs `sessionId` (the+ * requesting session; default the focused one) can see. Null only when that+ * session has no tab open at all. */+export async function readActivePreview(+ opts: PreviewReadOptions = {},+ sessionId?: null | string+): Promise<null | PreviewReadResult> {+ const tabs = previewTabsFor(sessionId)const tab = resolveActivePreviewTab(tabs)if (!tab) {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.tsindex 483d358d170..5e5656907a1 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@@ -30,7 +30,7 @@ import {} from '@/store/prompts'import { rememberServerRequest } from '@/store/server-requests'import { $selectedStoredSessionId, $sessions, lineageAliases, sessionMatchesStoredId } from '@/store/session'-import { $sessionStates, $sessionTiles } from '@/store/session-states'+import { $sessionStates, $sessionTiles, storedSessionIdForRuntimeId } from '@/store/session-states'import { requestScrollToBottom } from '@/store/thread-scroll'import { $toursEnabled } from '@/store/tours'@@ -482,17 +482,19 @@ const terminalRead: Handler = ({ request }) => {answerValue(request, readActiveTerminal({ count: num(request.params.count), start: num(request.params.start) }))}-const previewRead: Handler = ({ request }) => {+const previewRead: Handler = ({ request, sessionId }) => {// read_preview tool: the active preview tab's page text is async. Empty = nothing open.// The window that passes the session gate may be the chat window while the// live webview lives in the popped-out Browser renderer — forward there// first; a null (no pop-out answered) falls back to the legacy local read.+ // A local read sees only the tabs the requesting session can see.const opts = { count: num(request.params.count), start: num(request.params.start) }+ const owner = (sessionId && storedSessionIdForRuntimeId(sessionId)) || undefinedvoid (async () => {const result = hasLivePreviewSurface()- ? await readActivePreview(opts)- : ((await requestPopoutPreviewRead(opts)) ?? (await readActivePreview(opts)))+ ? await readActivePreview(opts, owner)+ : ((await requestPopoutPreviewRead(opts)) ?? (await readActivePreview(opts, owner)))answerValue(request, result)})()diff --git a/apps/desktop/src/store/preview.ts b/apps/desktop/src/store/preview.tsindex 67220b90d57..22cdef9387a 100644--- a/apps/desktop/src/store/preview.ts+++ b/apps/desktop/src/store/preview.ts@@ -14,7 +14,7 @@ import { $rightRailActiveTabId, type RightRailTabId, selectRightRailTab } from 'import { clearExplicitPreviewOpen, noteExplicitPreviewOpen, PREVIEW_TILE_PREFIX } from './preview-explicit'import { normalizeProfileKey } from './profile'import { $focusedStoredSessionId } from './session-focus'-import { canOpenBrowserWindow, openBrowserInNewWindow } from './windows'+import { canOpenBrowserWindow, isBrowserWindow, openBrowserInNewWindow } from './windows'/*** PREVIEW RAIL — one list of tabs, one way in.@@ -392,6 +392,14 @@ function tabsVisibleTo(tabs: readonly PreviewTab[], sessionId: null | string): Preturn tabs.filter(tab => tabVisibleTo(tab, sessionId, rotated))}+/** The tabs an agent tool acting for `sessionId` (default: the focused+ * session) may read or drive: never another session's hidden tab. A+ * popped-out Browser renderer holds only the tab it shows, so there every+ * tab is its own. */+export function previewTabsFor(sessionId: null | string = $focusedStoredSessionId.get()): PreviewTab[] {+ return isBrowserWindow() ? $previewTabs.get() : tabsVisibleTo($previewTabs.get(), sessionId)+}+/** Tabs the FOCUSED session sees. The layout-tree mirror renders only these,* so a session switch swaps the drawer; hidden tabs stay in `$previewTabs`. */export const $visiblePreviewTabs = computed(