The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): route approval.respond through the session's owner, not the ambient socket
Teknium09047ec69cf5
diff --git a/apps/desktop/src/components/assistant-ui/tool/approval.tsx b/apps/desktop/src/components/assistant-ui/tool/approval.tsxindex f511fb0afbe..66731664ef1 100644--- a/apps/desktop/src/components/assistant-ui/tool/approval.tsx+++ b/apps/desktop/src/components/assistant-ui/tool/approval.tsx@@ -28,6 +28,7 @@ import {sessionApprovalInlineVisible,sessionApprovalRequest} from '@/store/prompts'+import { requestForOwnedSession } from '@/store/session-states'import type { ToolPart } from './fallback-model'@@ -143,11 +144,22 @@ const ApprovalBar: FC<{ request: ApprovalRequest; surface: 'floating' | 'inline'setSubmitting(choice)try {- await gateway.request<{ resolved?: boolean }>('approval.respond', {- choice,- request_id: request.requestId,- session_id: request.sessionId ?? undefined- })+ // Route through the session's OWNER (tile route → known profile);+ // ambient only when no owner is known. The ambient socket follows+ // foreground focus, and for a cross-profile session it points at a+ // backend that never held this approval (#91684 client half).+ await requestForOwnedSession<{ resolved?: boolean }>(+ request.sessionId,+ // Bound (not wrapped) so the ambient fallback keeps the exact+ // 2-arg call shape gateway.request callers assert on.+ gateway.request.bind(gateway) as typeof gateway.request,+ 'approval.respond',+ {+ choice,+ request_id: request.requestId,+ session_id: request.sessionId ?? undefined+ }+ )triggerHaptic(choice === 'deny' ? 'cancel' : 'submit')clearApprovalRequest(request.sessionId, request.requestId)void replayPendingApproval(gateway, request.sessionId).catch(() => undefined)diff --git a/apps/desktop/src/store/native-notifications.ts b/apps/desktop/src/store/native-notifications.tsindex 3e7e1c8ce78..09edafc2bbd 100644--- a/apps/desktop/src/store/native-notifications.ts+++ b/apps/desktop/src/store/native-notifications.ts@@ -7,6 +7,7 @@ import { $gateway } from './gateway'import { withinNativeNotifyBaseline } from './notify-baseline'import { clearApprovalRequest } from './prompts'import { $activeSessionId } from './session'+import { requestForOwnedSession } from './session-states'export type { HermesOpenTarget }@@ -359,7 +360,18 @@ export async function respondToApprovalAction(sessionId: null | string, actionId}try {- await gateway.request('approval.respond', { choice, session_id: sessionId ?? undefined })+ // Route through the session's OWNER (tile route → known profile); the+ // ambient socket follows foreground focus and, for a background approval+ // raised by a cross-profile session, points at a backend that never held+ // the approval (#91684 client half). Ambient only when no owner is known.+ await requestForOwnedSession(+ sessionId,+ // Bound (not wrapped) so the ambient fallback keeps the exact 2-arg+ // call shape gateway.request callers assert on.+ gateway.request.bind(gateway) as typeof gateway.request,+ 'approval.respond',+ { choice, session_id: sessionId ?? undefined }+ )clearApprovalRequest(sessionId)} catch {// Leave the prompt parked so the user can still resolve it in-app.diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.tsindex 8cf8435a7d0..7210771070c 100644--- a/apps/desktop/src/store/session-states.test.ts+++ b/apps/desktop/src/store/session-states.test.ts@@ -4,7 +4,7 @@ import type { ClientSessionState } from '@/app/types'import { findGroupOfPane, group, split } from '@/components/pane-shell/tree/model'import { $layoutTree } from '@/components/pane-shell/tree/store'import { $activeGatewayProfile } from '@/store/profile'-import { $selectedStoredSessionId } from '@/store/session'+import { $selectedStoredSessionId, setSessions } from '@/store/session'import type { SessionTile } from '@/store/session-states'import {$sessionStates,@@ -12,12 +12,14 @@ import {blankDraftTile,focusedSessionNeedsRoute,focusOpenSession,+ knownOwnerForSession,markSelectionRestore,nextSessionTileForWorkspace,openSessionTile,orderTilesByTree,patchSessionTile,releaseSessionTranscript,+ requestForOwnedSession,resetTileRuntimeBindings,selectionHomesToWorkspace,type SessionTileDelegate,@@ -575,3 +577,53 @@ describe('sessionTileOwnerRoute', () => {expect(sessionTileOwnerRoute('missing')).toBeUndefined()})})++describe('knownOwnerForSession / requestForOwnedSession (#91684 client half)', () => {+ beforeEach(() => {+ // Earlier suites leave profile-scoped tile buckets behind; pin the profile+ // BEFORE seeding tiles so the bucket subscriber cannot clobber the seed.+ $activeGatewayProfile.set('default')+ $sessionTiles.set([])+ })+ afterEach(() => {+ $sessionTiles.set([])+ setSessions([])+ })++ it('resolves the tile owner route first, translating a runtime id to its stored id', () => {+ $sessionTiles.set([+ {+ ownerRoute: { connectionId: 'conn-a', profile: 'work' },+ runtimeId: 'rt-1',+ storedSessionId: 'stored-1'+ }+ ])++ expect(knownOwnerForSession('rt-1')).toEqual({ connectionId: 'conn-a', profile: 'work' })+ expect(knownOwnerForSession('stored-1')).toEqual({ connectionId: 'conn-a', profile: 'work' })+ })++ it('falls back to the session row profile when no tile route exists', () => {+ setSessions([{ id: 'stored-2', profile: 'loki' } as never])++ expect(knownOwnerForSession('stored-2')).toBe('loki')+ })++ it('returns undefined (ambient) when no owner is known, and for null ids', () => {+ expect(knownOwnerForSession('unknown-session')).toBeUndefined()+ expect(knownOwnerForSession(null)).toBeUndefined()+ expect(knownOwnerForSession(undefined)).toBeUndefined()+ })++ it('requestForOwnedSession dispatches ambient with the exact 2-arg shape when no owner is known', async () => {+ const ambient = vi.fn(async (method: string, params?: Record<string, unknown>) => ({ method, params }))++ await requestForOwnedSession('unknown-session', ambient as never, 'approval.respond', {+ choice: 'once',+ session_id: 'unknown-session'+ })++ expect(ambient).toHaveBeenCalledTimes(1)+ expect(ambient.mock.calls[0]).toEqual(['approval.respond', { choice: 'once', session_id: 'unknown-session' }])+ })+})diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.tsindex fd8961e54a8..1711bc898ab 100644--- a/apps/desktop/src/store/session-states.ts+++ b/apps/desktop/src/store/session-states.ts@@ -42,13 +42,14 @@ import {$selectedStoredSessionId,$sessions,clearReadBaseline,+ knownSessionProfile,lineageAliases,markSessionRead,sessionMatchesStoredId,setActiveSessionStoredIdRotation,setSessions} from './session'-import type { SessionProfileRoute } from './session-request-router'+import { requestForSessionProfile, type SessionOwnerScope, type SessionProfileRoute } from './session-request-router'import { ackStoredSessionId, markSessionUnreadFinished } from './session-unread'import { isSecondaryWindow } from './windows'@@ -738,6 +739,45 @@ export function sessionTileOwnerRoute(storedSessionId: string): SessionProfileRoreturn $sessionTiles.get().find(tile => tile.storedSessionId === storedSessionId)?.ownerRoute}+/**+ * Sync owner resolution for a session id that may be a RUNTIME or a STORED id.+ * Tile route first (exact connectionId+profile, survives relaunch), then the+ * known session profile (row or open-time hint). Returns undefined when no+ * owner is known — the caller falls back to ambient, never to "active".+ */+export function knownOwnerForSession(sessionId: null | string | undefined): SessionOwnerScope {+ if (!sessionId) {+ return undefined+ }++ const storedSessionId = storedSessionIdForRuntimeId(sessionId) ?? sessionId++ return sessionTileOwnerRoute(storedSessionId) ?? knownSessionProfile($sessions.get(), storedSessionId)+}++/**+ * Dispatch a session-scoped RPC through the OWNER of `sessionId` (tile route →+ * known profile), falling back to the ambient dispatcher only when no owner is+ * known. This is the client half of #91684: approval.respond (and siblings)+ * sent on the ambient socket land on whatever backend is active, which for a+ * cross-profile session is a backend that never held the approval.+ */+export function requestForOwnedSession<T>(+ sessionId: null | string | undefined,+ ambientRequest: <R>(+ method: string,+ params?: Record<string, unknown>,+ timeoutMs?: number,+ signal?: AbortSignal+ ) => Promise<R>,+ method: string,+ params: Record<string, unknown> = {},+ timeoutMs?: number,+ signal?: AbortSignal+): Promise<T> {+ return requestForSessionProfile<T>(knownOwnerForSession(sessionId), ambientRequest, method, params, timeoutMs, signal)+}+/** Resolve a session id THAT MAY BE A RUNTIME ID to the stored id its tile* keys on. Session-scoped RPC params carry the runtime id, while tile owner* routes (and everything else durable) key on the stored id — so routing an