The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: answer window-owned requests for sessions hosted in tiles
teknium14cde8f2ced10
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.tsindex a84f257c17e..81db01a7d8d 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@@ -1,6 +1,7 @@import { afterEach, describe, expect, it, vi } from 'vitest'import { createClientSessionState } from '@/lib/chat-runtime'+import { $sessionTiles } from '@/store/session-states'import { $toursEnabled } from '@/store/tours'import { handleServerRequest, previewSessionRoute } from './server-requests'@@ -70,6 +71,27 @@ describe('preview action request routing', () => {}})+ 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.+ $sessionTiles.set([{ runtimeId: 'session-a', storedSessionId: 'stored-a' } as never])++ try {+ const reads = ['preview.read', 'terminal.read', 'window.read'].map(method =>+ deliver(method, { session_id: 'session-a' }, 'session-b')+ )++ await new Promise(resolve => setTimeout(resolve, 0))++ for (const { handled, respond } of reads) {+ expect(handled).toBe(true)+ expect(respond).toHaveBeenCalledTimes(1)+ }+ } finally {+ $sessionTiles.set([])+ }+ })+it('fails fast for an unscoped request with no session in view', () => {const { respond } = deliver('preview.act', { action: 'elements' }, null)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 2f409d3d068..dd994c16459 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@@ -17,6 +17,7 @@ import {setVaultUnlockRequest} from '@/store/prompts'import { rememberServerRequest } from '@/store/server-requests'+import { $sessionTiles } from '@/store/session-states'import { requestScrollToBottom } from '@/store/thread-scroll'import { $toursEnabled } from '@/store/tours'@@ -59,19 +60,25 @@ type PreviewSessionRoute = 'ignore' | 'retry' | 'run'/*** Bridges answered from THIS window's panes (preview tab, xterm buffer, the- * native window below). Every attached window sees the request; one showing- * another session has no pane for it and its empty answer would win the race,- * so the tool reports "no preview tab / no terminal" while the owner's pane is- * open (#113348).+ * native window below, the tour overlay). Every attached window sees the+ * request; one not hosting the session has no pane for it and its empty answer+ * would win the race, so the tool reports "no preview tab / no terminal" while+ * the owner's pane is open (#113348).*/-const WINDOW_OWNED_REQUESTS = new Set(['preview.act', 'preview.read', 'terminal.read', 'window.read'])+const WINDOW_OWNED_REQUESTS = new Set(['preview.act', 'preview.read', 'terminal.read', 'window.read', 'tour'])++/** This window hosts the session: it is the primary view or an open session tile. */+export function windowHostsSession(sessionId: string, activeSessionId: null | string): boolean {+ return sessionId === activeSessionId || $sessionTiles.get().some(tile => tile.runtimeId === sessionId)+}/*** Panes are local to one desktop window, while gateway requests fan out* to every connected window. A scoped request may only be answered by the- * window showing its session. During reconnect, however, an open request can- * replay one event-loop turn before the resumed session becomes active; retry- * that one narrow race and otherwise leave the request for its owner.+ * window hosting its session (primary view or a tile). During reconnect,+ * however, an open request can replay one event-loop turn before the resumed+ * session becomes active; retry that one narrow race and otherwise leave the+ * request for its owner.*/export function previewSessionRoute({activeSessionId,@@ -82,7 +89,7 @@ export function previewSessionRoute({replayed: boolean | undefinedsessionId: string}): PreviewSessionRoute {- if (!sessionId || sessionId === activeSessionId) {+ if (!sessionId || windowHostsSession(sessionId, activeSessionId)) {return 'run'}@@ -316,16 +323,12 @@ const previewRead: Handler = ({ request }) => {)}-const previewAct: Handler = ({ isActiveSession, request, sessionId }) => {+const previewAct: Handler = ({ isActiveSession, request }) => {// drive_preview tool: click/type/scroll/press inside the guest page. Active- // session only: a background turn must never reach into the page the user is- // working in (desktop AGENTS.md: offer, don't hijack). Every mounted window can- // observe the same request; a scoped mismatch belongs to another window, so- // answering here would race the owner — stay silent.- if (sessionId && !isActiveSession) {- return- }-+ // session only: a background turn (including one in a tile this window hosts)+ // must never reach into the page the user is working in (desktop AGENTS.md:+ // offer, don't hijack). Window ownership is settled by WINDOW_OWNED_REQUESTS+ // before this runs, so a refusal here reaches the tool instead of stalling it.const p = request.paramsif (!isActiveSession) {@@ -369,13 +372,10 @@ const windowRead: Handler = ({ request }) => {)}-const tour: Handler = ({ isActiveSession, request, sessionId }) => {+const tour: Handler = ({ isActiveSession, request }) => {// tour tool: one guided-tour action via driver.js, app DOM or preview guest- // page. Active session only, same window-ownership rule as preview.act.- if (sessionId && !isActiveSession) {- return- }-+ // page. Active session only, same window-ownership rule as preview.act+ // (WINDOW_OWNED_REQUESTS).const p = request.paramsif (!$toursEnabled.get()) {