Skip to content

The agent that grows with you

fix(desktop): pin /messages/around reads to the session owner and trim the pin seam (#125372, salvage #125414)

teknium1e55c1cb8799f
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index dc1870db322..2f6c614e1fa 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -226,29 +226,22 @@ export function getApiRequestConnection(): null | string {
return $apiRequestScope.get().connectionId
}
-// ── Session-owner reads: the connection a session-scoped REST read must ride ─
+// ── Session-owner pin for session-scoped REST reads (#125372) ──────────────
//
-// A session-scoped read (detail / messages / timeline) only means anything on
-// the backend that OWNS the session's row (#125372). The renderer already
-// knows that owner (tile route → persisted hint → connection-tagged row,
-// resolved by store/session-states knownOwnerForSession), but read helpers
-// dispatched with the WINDOW's ambient connection scope — so with a registered
-// remote that exposes a same-named profile, the wrong backend answered
-// 404 "Session not found" in both directions. The store pushes its resolver
-// through setSessionOwnerResolver (same seam pattern as setApiRequestProfile:
-// no store import here, which would close a module cycle) and the session read
-// helpers pin the resolved connection.
+// A read of /api/sessions/{id}[/messages|/timeline|/messages/around] only
+// means anything on the backend that OWNS the row. The store already resolves
+// that owner (knownOwnerForSession: tile route → persisted hint → tagged row)
+// and pushes the resolver here — same no-store-import seam as
+// setApiRequestProfile — so read helpers stop riding the WINDOW's ambient
+// connection onto a host that answers 404 "Session not found".
export interface SessionReadOwnerRoute {
connectionId?: null | string
- mode?: string
profile?: null | string
targetProfile?: null | string
}
-export type SessionReadOwner = string | SessionReadOwnerRoute
-
-export type SessionOwnerResolver = (sessionId: string) => SessionReadOwner | null | undefined
+export type SessionOwnerResolver = (sessionId: string) => SessionReadOwnerRoute | null | string | undefined
let _sessionOwnerResolver: SessionOwnerResolver | null = null
@@ -256,17 +249,12 @@ export function setSessionOwnerResolver(resolver: SessionOwnerResolver | null):
_sessionOwnerResolver = resolver
}
-function sessionOwnerForRead(sessionId: string): SessionReadOwner | null | undefined {
- return _sessionOwnerResolver?.(sessionId)
-}
-
/** The connection pin a session-scoped READ must carry for `id`, or {} when
- * the ambient scope is already right. An explicit (connection, profile)
- * object scope short-circuits: the caller has already routed the request. An
- * exact owner route pins its connection — 'local' INCLUDED, the sanctioned
- * way back to this device when the window's primary is a remote registry
- * source — plus, when the caller named no profile, the route's backend-facing
- * profile: the answering host resolves ?profile= against ITS OWN profiles. */
+ * the caller already pinned a connection, no exact owner is known, or the
+ * owner is the ambient connection. Pins 'local' too (the only way back to
+ * this device when the primary is a remote registry source). When the caller
+ * named no profile, the owner's backend-facing profile rides along: the
+ * answering host resolves ?profile= against ITS OWN profiles. */
export function sessionReadOwnerPin(
id: string,
profile?: ProfileScope
@@ -275,25 +263,20 @@ export function sessionReadOwnerPin(
return {}
}
- const owner = sessionOwnerForRead(id)
+ const owner = _sessionOwnerResolver?.(id)
- if (!owner || typeof owner === 'string' || !('connectionId' in owner)) {
+ if (!owner || typeof owner === 'string') {
return {}
}
const connectionId = String(owner.connectionId ?? '').trim()
- // No exact connection in the route → no pin (ambient behavior, never guess).
- if (!connectionId) {
+ if (!connectionId || connectionId === (ambientOwnerConnectionId() ?? 'local')) {
return {}
}
const ownerProfile = String(owner.targetProfile || owner.profile || '').trim()
- if (connectionId === (ambientOwnerConnectionId() ?? 'local')) {
- return {}
- }
-
return {
connectionId,
...(profile == null && ownerProfile ? { profile: ownerProfile } : {})
diff --git a/apps/desktop/src/api/sessions.test.ts b/apps/desktop/src/api/sessions.test.ts
index 19c65562030..4717a5447ed 100644
--- a/apps/desktop/src/api/sessions.test.ts
+++ b/apps/desktop/src/api/sessions.test.ts
@@ -20,7 +20,6 @@ const client = await import('./client')
const {
deleteSession,
getSession,
- getSessionMessages,
getLatestSessionMessages,
setSessionArchived,
setSessionPinnedRemote,
@@ -278,17 +277,6 @@ describe('session reads pin the owner connection (#125372)', () => {
})
})
- it('leaves ambient reads untouched when no owner is known', async () => {
- vi.mocked(client.sessionReadOwnerPin).mockReturnValue({})
-
- await getSessionMessages('sess-x')
-
- expect(hermesApi.mock.calls[0][0]).toMatchObject({
- path: '/api/sessions/sess-x/messages'
- })
- expect(hermesApi.mock.calls[0][0]).not.toHaveProperty('connectionId')
- })
-
it('keeps an explicit (connection, profile) pin authoritative in getLatestSessionMessages', async () => {
// Explicit caller pin: the real capabilityScoped resolves it; the owner
// lookup must not override it.
diff --git a/apps/desktop/src/app/chat/history-window.ts b/apps/desktop/src/app/chat/history-window.ts
index e5ebf0fac2f..880ee4345f9 100644
--- a/apps/desktop/src/app/chat/history-window.ts
+++ b/apps/desktop/src/app/chat/history-window.ts
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
-import { capabilityScoped, hermesApi, type ProfileScope } from '@/api/client'
+import { capabilityScoped, hermesApi, type ProfileScope, sessionReadOwnerPin } from '@/api/client'
import {
cachedTimelineIndex,
previousPromptRowId,
@@ -36,7 +36,15 @@ export async function fetchHistoryWindow(
signal: AbortSignal
): Promise<HistoryPage> {
signal.throwIfAborted()
- const route = capabilityScoped(scope)
+
+ // Owner connection pin (#125372): an around-read for a session owned by
+ // another registry connection must read THAT host, not the ambient one.
+ const route = {
+ ...capabilityScoped(scope),
+ ...(typeof scope === 'object' && scope?.connectionId === 'local' ? { connectionId: 'local' } : {}),
+ ...sessionReadOwnerPin(storedId, scope)
+ }
+
const query = new URLSearchParams({ row_id: String(rowId), limit: String(HISTORY_WINDOW_LIMIT) })
if (route.profile) {
@@ -48,7 +56,6 @@ export async function fetchHistoryWindow(
// it does not pretend to cancel backend I/O or fall back to a full transcript.
const response = await hermesApi<HistoryWindowResponse>({
...route,
- ...(typeof scope === 'object' && scope?.connectionId === 'local' ? { connectionId: 'local' } : {}),
method: 'GET',
path: `/api/sessions/${encodeURIComponent(storedId)}/messages/around?${query}`
})
diff --git a/apps/desktop/src/components/assistant-ui/thread/use-timeline-history.test.tsx b/apps/desktop/src/components/assistant-ui/thread/use-timeline-history.test.tsx
index 41f2cd24413..0d982e47359 100644
--- a/apps/desktop/src/components/assistant-ui/thread/use-timeline-history.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/use-timeline-history.test.tsx
@@ -11,7 +11,11 @@ const { api, view } = vi.hoisted(() => ({
}
}))
-vi.mock('@/api/client', () => ({ capabilityScoped: (scope: object) => scope, hermesApi: api }))
+vi.mock('@/api/client', () => ({
+ capabilityScoped: (scope: object) => scope,
+ hermesApi: api,
+ sessionReadOwnerPin: () => ({})
+}))
vi.mock('@/app/chat/session-view', () => ({ useSessionView: () => view }))
vi.mock('@/store/profile', () => ({ $activeGatewayProfile: atom('default') }))
vi.mock('@/store/session', () => ({ $connection: atom({ mode: 'local' }), getSessionOwnerHint: () => undefined }))