Skip to content

The agent that grows with you

fix(desktop): retry replayed preview owner routing

KoNit-K4db8521f2519
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.ts
index 2497dfd408c..72da5742a3b 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
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { createClientSessionState } from '@/lib/chat-runtime'
import { $toursEnabled } from '@/store/tours'
-import { handleServerRequest } from './server-requests'
+import { handleServerRequest, previewSessionRoute } from './server-requests'
import type { ServerRequestContext } from './server-requests'
const deps = {
@@ -42,6 +42,20 @@ describe('connection request routing', () => {
})
describe('preview action request routing', () => {
+ it('waits one turn for a replayed scoped request while the active-session binding resumes', () => {
+ expect(previewSessionRoute({ replayed: true, sessionId: 'session-a', activeSessionId: null })).toBe('retry')
+ expect(previewSessionRoute({ replayed: true, sessionId: 'session-a', activeSessionId: 'session-a' })).toBe('run')
+ })
+
+ it('leaves scoped preview requests for another window unanswered', () => {
+ expect(previewSessionRoute({ replayed: false, sessionId: 'session-a', activeSessionId: 'session-b' })).toBe('ignore')
+ expect(previewSessionRoute({ replayed: true, sessionId: 'session-a', activeSessionId: 'session-b' })).toBe('ignore')
+ })
+
+ it('keeps unscoped preview requests on the existing fail-fast path', () => {
+ expect(previewSessionRoute({ replayed: true, sessionId: '', activeSessionId: null })).toBe('run')
+ })
+
it('leaves a scoped action request unanswered in a window showing another session', () => {
const { handled, respond, fail } = deliver('preview.act', { action: 'elements', session_id: 'session-a' }, 'session-b')
@@ -50,6 +64,14 @@ describe('preview action request routing', () => {
expect(fail).not.toHaveBeenCalled()
})
+ it('leaves a scoped read request unanswered in a window showing another session', async () => {
+ const { respond } = deliver('preview.read', { session_id: 'session-a' }, 'session-b')
+
+ await Promise.resolve()
+
+ expect(respond).not.toHaveBeenCalled()
+ })
+
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.ts
index 886cd5cc9cf..50c4ae63755 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
@@ -55,6 +55,31 @@ export interface ServerRequestContext {
type Handler = (ctx: ServerRequestContext) => void
+type PreviewSessionRoute = 'ignore' | 'retry' | 'run'
+
+/**
+ * Preview 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.
+ */
+export function previewSessionRoute({
+ activeSessionId,
+ replayed,
+ sessionId
+}: {
+ activeSessionId: null | string
+ replayed: boolean | undefined
+ sessionId: string
+}): PreviewSessionRoute {
+ if (!sessionId || sessionId === activeSessionId) {
+ return 'run'
+ }
+
+ return replayed && !activeSessionId ? 'retry' : 'ignore'
+}
+
const markNeedsInput = (ctx: ServerRequestContext) => {
if (ctx.sessionId) {
ctx.deps.updateSessionState(ctx.sessionId, state => ({ ...state, needsInput: true }))
@@ -409,6 +434,27 @@ export function handleServerRequest(
const sessionId = str(request.params.session_id)
+ if (request.method === 'preview.act' || request.method === 'preview.read') {
+ const route = previewSessionRoute({ activeSessionId, replayed: request.replayed, sessionId })
+
+ if (route === 'ignore') {
+ return true
+ }
+
+ if (route === 'retry') {
+ // Re-read the ref instead of capturing activeSessionId: session resume
+ // publishes its binding synchronously between this replay and the next
+ // turn. A second miss deliberately stays silent for another window.
+ setTimeout(() => {
+ if (previewSessionRoute({ activeSessionId: deps.activeSessionIdRef.current, replayed: false, sessionId }) === 'run') {
+ handler({ deps, request, sessionId, isActiveSession: true })
+ }
+ }, 0)
+
+ return true
+ }
+ }
+
handler({ deps, request, sessionId, isActiveSession: Boolean(sessionId) && sessionId === activeSessionId })
return true