Skip to content

The agent that grows with you

fix(desktop): route four remaining session_id RPCs to the session owner

Tekniumfd2197ba7882
diff --git a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx
index 75129f824b6..d1c9f1a2154 100644
--- a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx
+++ b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx
@@ -53,7 +53,13 @@ import {
sessionPinId
} from '@/store/session'
import { $sessionColorOverrides, setSessionColorOverride } from '@/store/session-color'
-import { $sessionStates, $sessionTiles, closeAllOpenSessionTiles } from '@/store/session-states'
+import { ambientRequestFor } from '@/store/session-gone-latch'
+import {
+ $sessionStates,
+ $sessionTiles,
+ closeAllOpenSessionTiles,
+ requestForOwnedSession
+} from '@/store/session-states'
import { ackStoredSessionId } from '@/store/session-unread'
import { canOpenSessionInTerminal, canOpenSessionWindow, openSessionInTerminal } from '@/store/windows'
@@ -125,10 +131,14 @@ export async function renameSessionPreferringRpc(
if (title && runtimeId && gateway) {
try {
- const result = await gateway.request<SessionTitleResponse>('session.title', {
- session_id: runtimeId,
- title
- })
+ // The runtime lives on its owner's backend; the ambient socket would
+ // refuse a secondary profile's session and drop us to REST.
+ const result = await requestForOwnedSession<SessionTitleResponse>(
+ runtimeId,
+ ambientRequestFor(gateway),
+ 'session.title',
+ { session_id: runtimeId, title }
+ )
return { title: result?.title ?? title }
} catch (err) {
diff --git a/apps/desktop/src/components/model-visibility-dialog.tsx b/apps/desktop/src/components/model-visibility-dialog.tsx
index 3e3f77e89b8..4fcec04333d 100644
--- a/apps/desktop/src/components/model-visibility-dialog.tsx
+++ b/apps/desktop/src/components/model-visibility-dialog.tsx
@@ -38,6 +38,24 @@ import {
toggleModelVisibility
} from '@/store/model-visibility'
import { $collapsedProviders, toggleCollapsedProvider } from '@/store/provider-collapse'
+import { ambientRequestFor } from '@/store/session-gone-latch'
+import { sessionOwnerIsKnown } from '@/store/session-owner-resolution'
+import { knownOwnerForSession, requestForOwnedSession } from '@/store/session-states'
+
+/** A session-scoped `model.options` goes to the session's OWNER: the ambient
+ * socket refuses a secondary profile's session_id and the dialog fell back to
+ * the launch profile's REST catalog. An owner nothing names yet keeps the
+ * ambient path (and its REST recovery) — this is a settings view, not a write. */
+function ownerModelOptionsRequest(gw: HermesGateway | undefined, sessionId: null | string | undefined) {
+ if (!gw || !sessionId || !sessionOwnerIsKnown(knownOwnerForSession(sessionId))) {
+ return undefined
+ }
+
+ const ambient = ambientRequestFor(gw)
+
+ return <T,>(method: string, params?: Record<string, unknown>) =>
+ requestForOwnedSession<T>(sessionId, ambient, method, params)
+}
interface ModelVisibilityDialogProps {
gw?: HermesGateway
@@ -67,7 +85,8 @@ export function ModelVisibilityDialog({
const modelOptions = useQuery({
queryKey: modelOptionsQueryKey(profile, sessionId, ownerConnectionId),
- queryFn: (): Promise<ModelOptionsResult> => requestModelOptions({ gateway: gw, profile, sessionId }),
+ queryFn: (): Promise<ModelOptionsResult> =>
+ requestModelOptions({ gateway: gw, profile, request: ownerModelOptionsRequest(gw, sessionId), sessionId }),
enabled: open
})
diff --git a/apps/desktop/src/lib/oneshot.ts b/apps/desktop/src/lib/oneshot.ts
index cfb41f26686..94b05cb75ba 100644
--- a/apps/desktop/src/lib/oneshot.ts
+++ b/apps/desktop/src/lib/oneshot.ts
@@ -1,5 +1,7 @@
import { $gateway } from '@/store/gateway'
import { $activeSessionId } from '@/store/session'
+import { ambientRequestFor } from '@/store/session-gone-latch'
+import { requestForOwnedSession } from '@/store/session-states'
// Shared client for one-off ("one-shot") LLM requests: a single stateless model
// call that runs OUTSIDE the conversation. It never appends to session history,
@@ -43,7 +45,10 @@ export async function requestOneShot(req: OneShotRequest): Promise<string> {
const sessionId = req.sessionId === undefined ? $activeSessionId.get() : req.sessionId
- const result = await gateway.request<{ text?: string }>('llm.oneshot', {
+ // A session-bound one-shot borrows that session's model, so it must reach the
+ // backend that OWNS the session: the ambient socket answers only for the
+ // launch profile and refuses a secondary profile's session_id.
+ const result = await requestForOwnedSession<{ text?: string }>(sessionId, ambientRequestFor(gateway), 'llm.oneshot', {
input: req.input,
instructions: req.instructions,
max_tokens: req.maxTokens,
diff --git a/apps/desktop/src/lib/session-owner-routed-rpcs.test.tsx b/apps/desktop/src/lib/session-owner-routed-rpcs.test.tsx
new file mode 100644
index 00000000000..f26e6249c38
--- /dev/null
+++ b/apps/desktop/src/lib/session-owner-routed-rpcs.test.tsx
@@ -0,0 +1,141 @@
+// Every Desktop RPC that carries a session_id must reach the backend that OWNS
+// that session. On a multiplexed local gateway the window's ambient socket is
+// the launch profile's authority, which refuses a secondary profile's session:
+// llm.oneshot and the session YOLO toggle failed outright, session.title fell
+// back to REST and model.options to the launch profile's catalog.
+import type { ModelOptionsResult } from '@hermes/shared'
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { cleanup, render, waitFor } from '@testing-library/react'
+import { atom } from 'nanostores'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { I18nProvider } from '@/i18n'
+import type * as GatewayStore from '@/store/gateway'
+import { _resetSessionOwnerHintsForTests, setSessionOwnerHint } from '@/store/session'
+import { $sessionStates } from '@/store/session-states'
+import { stubResizeObserver } from '@/test/jsdom'
+
+const mocks = vi.hoisted(() => {
+ // The ambient (launch-profile) socket: it refuses another profile's session.
+ const ambient = {
+ request: vi.fn(async () => {
+ throw new Error('not_found')
+ })
+ }
+
+ return { ambient, requestGatewayForAgent: vi.fn(), requestModelOptions: vi.fn() }
+})
+
+vi.mock('@/store/gateway', async importOriginal => ({
+ ...(await importOriginal<typeof GatewayStore>()),
+ $gateway: atom(mocks.ambient),
+ activeGateway: () => mocks.ambient,
+ requestGatewayForAgent: mocks.requestGatewayForAgent
+}))
+
+vi.mock('@/lib/model-options', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ requestModelOptions: mocks.requestModelOptions
+}))
+
+const { requestOneShot } = await import('./oneshot')
+const { setSessionYolo } = await import('./yolo-session')
+const { renameSessionPreferringRpc } = await import('@/app/chat/sidebar/session-actions-menu')
+const { ModelVisibilityDialog } = await import('@/components/model-visibility-dialog')
+
+stubResizeObserver()
+
+const RUNTIME = 'rt-research-1'
+const STORED = 'stored-research-1'
+const OWNER = { connectionId: 'local', profile: 'research' }
+
+beforeEach(() => {
+ mocks.requestGatewayForAgent.mockImplementation(async (_connection: string, _profile: string, method: string) =>
+ method === 'llm.oneshot'
+ ? { text: ' commit: research ' }
+ : method === 'config.set'
+ ? { value: '1' }
+ : method === 'session.title'
+ ? { title: 'Renamed on owner' }
+ : {}
+ )
+ mocks.requestModelOptions.mockResolvedValue({ providers: [] } satisfies ModelOptionsResult)
+ $sessionStates.set({ [RUNTIME]: { storedSessionId: STORED } as never })
+ setSessionOwnerHint(STORED, OWNER)
+})
+
+afterEach(() => {
+ cleanup()
+ _resetSessionOwnerHintsForTests()
+ $sessionStates.set({})
+ vi.clearAllMocks()
+})
+
+describe('session_id RPCs go to the session owner, not the ambient socket', () => {
+ it('llm.oneshot borrows the secondary-profile session model on its owner', async () => {
+ await expect(requestOneShot({ sessionId: RUNTIME, template: 'commit_message' })).resolves.toBe('commit: research')
+
+ expect(mocks.ambient.request).not.toHaveBeenCalled()
+ expect(mocks.requestGatewayForAgent).toHaveBeenCalledWith(
+ 'local',
+ 'research',
+ 'llm.oneshot',
+ expect.objectContaining({ session_id: RUNTIME, template: 'commit_message' })
+ )
+ })
+
+ it('the session YOLO toggle lands on the owner, even when handed the ambient requester', async () => {
+ const ambientRequest = vi.fn(async () => {
+ throw new Error('not_found')
+ })
+
+ await expect(setSessionYolo(ambientRequest as never, RUNTIME, true)).resolves.toBe(true)
+
+ expect(ambientRequest).not.toHaveBeenCalled()
+ expect(mocks.requestGatewayForAgent).toHaveBeenCalledWith('local', 'research', 'config.set', {
+ key: 'yolo',
+ session_id: RUNTIME,
+ value: '1'
+ })
+ })
+
+ it('session.title renames the live runtime on its owner instead of dropping to REST', async () => {
+ await expect(renameSessionPreferringRpc(STORED, 'Renamed')).resolves.toEqual({ title: 'Renamed on owner' })
+
+ expect(mocks.ambient.request).not.toHaveBeenCalled()
+ expect(mocks.requestGatewayForAgent).toHaveBeenCalledWith('local', 'research', 'session.title', {
+ session_id: RUNTIME,
+ title: 'Renamed'
+ })
+ })
+
+ it('Edit Models reads model.options for a secondary-profile session from its owner', async () => {
+ render(
+ <QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
+ <I18nProvider>
+ <ModelVisibilityDialog
+ gw={mocks.ambient as never}
+ onOpenChange={() => {}}
+ onOpenProviders={() => {}}
+ open
+ profile="default"
+ sessionId={RUNTIME}
+ />
+ </I18nProvider>
+ </QueryClientProvider>
+ )
+
+ await waitFor(() => expect(mocks.requestModelOptions).toHaveBeenCalled())
+
+ const { request } = mocks.requestModelOptions.mock.calls[0][0] as {
+ request?: (method: string, params?: Record<string, unknown>) => Promise<unknown>
+ }
+
+ expect(request).toBeTypeOf('function')
+ await request!('model.options', { session_id: RUNTIME })
+ expect(mocks.ambient.request).not.toHaveBeenCalled()
+ expect(mocks.requestGatewayForAgent).toHaveBeenCalledWith('local', 'research', 'model.options', {
+ session_id: RUNTIME
+ })
+ })
+})
diff --git a/apps/desktop/src/lib/yolo-session.ts b/apps/desktop/src/lib/yolo-session.ts
index 841cafdada7..237f2fbdeac 100644
--- a/apps/desktop/src/lib/yolo-session.ts
+++ b/apps/desktop/src/lib/yolo-session.ts
@@ -1,5 +1,6 @@
import { $gateway } from '@/store/gateway'
import { $activeSessionId, setYoloActive } from '@/store/session'
+import { requestForOwnedSession } from '@/store/session-states'
export type GatewayRequester = <T = unknown>(method: string, params?: Record<string, unknown>) => Promise<T>
@@ -7,13 +8,17 @@ export type GatewayRequester = <T = unknown>(method: string, params?: Record<str
* Toggle per-session YOLO (approval bypass) via gateway `config.set` — the same
* session-scoped flag as the TUI's Shift+Tab. It does NOT touch the global
* `approvals.mode` config, so CLI / TUI / cron behavior is unaffected.
+ *
+ * The flag lives on the session's OWNER, so the write is routed there;
+ * `requestGateway` is only the ambient fallback for a session the ambient
+ * gateway provably owns.
*/
export async function setSessionYolo(
requestGateway: GatewayRequester,
sessionId: string,
enabled: boolean
): Promise<boolean> {
- const result = await requestGateway<{ value?: string }>('config.set', {
+ const result = await requestForOwnedSession<{ value?: string }>(sessionId, requestGateway, 'config.set', {
key: 'yolo',
session_id: sessionId,
value: enabled ? '1' : '0'