The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(preview): scope action responses to active session
fangliquanflq3c823f40b569
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.test.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.test.tsnew file mode 100644index 00000000000..5aee1b7b713--- /dev/null+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.test.ts@@ -0,0 +1,49 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++import { $gateway } from '@/store/gateway'++import { handleDesktopBridgeEvent } from './desktop-bridge'+import type { GatewayEventContext } from './types'++function previewActContext({+ explicitSid,+ isActiveEvent+}: {+ explicitSid: string+ isActiveEvent: boolean+}): GatewayEventContext {+ return {+ event: { session_id: explicitSid || undefined, type: 'preview.act.request' },+ explicitSid,+ isActiveEvent,+ payload: { action: 'elements', request_id: 'request-1' }+ } as GatewayEventContext+}++describe('preview action bridge routing', () => {+ afterEach(() => {+ $gateway.set(null)+ })++ it('leaves a scoped action request unanswered in a window showing another session', () => {+ const request = vi.fn()+ $gateway.set({ request } as never)++ expect(handleDesktopBridgeEvent(previewActContext({ explicitSid: 'session-a', isActiveEvent: false }))).toBe(true)+ expect(request).not.toHaveBeenCalled()+ })++ it('keeps the legacy fail-fast response for an unscoped inactive request', () => {+ const request = vi.fn()+ $gateway.set({ request } as never)++ expect(handleDesktopBridgeEvent(previewActContext({ explicitSid: '', isActiveEvent: false }))).toBe(true)+ expect(request).toHaveBeenCalledWith('preview.act.respond', {+ request_id: 'request-1',+ text: JSON.stringify({+ error: 'The in-app browser only takes actions in the session the user is looking at.',+ success: false+ })+ })+ })+})diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.tsindex 1372a1f36e4..a9d6b4fe739 100644--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.ts+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/desktop-bridge.ts@@ -45,7 +45,7 @@ const loadPreviewEngine = () => {* (terminal/preview/window), agent terminal streaming, pane reveal, and* message reactions. */export function handleDesktopBridgeEvent(ctx: GatewayEventContext): boolean {- const { event, payload, isActiveEvent } = ctx+ const { event, payload, explicitSid, isActiveEvent } = ctxif (event.type === 'terminal.read.request') {// read_terminal tool: serialize the renderer's xterm buffer and answer@@ -95,6 +95,13 @@ export function handleDesktopBridgeEvent(ctx: GatewayEventContext): boolean {const requestId = typeof payload?.request_id === 'string' ? payload.request_id : ''if (requestId) {+ // Every mounted desktop window can observe the same gateway event. A+ // scoped mismatch belongs to another window, so answering here would race+ // the owning window and could make this refusal win before its real result.+ if (explicitSid && !isActiveEvent) {+ return true+ }+const answer = (result: unknown) =>$gateway.get()?.request('preview.act.respond', {request_id: requestId,