Skip to content

The agent that grows with you

fix(desktop): resolve the owning remote profile before a hint-less session read falls through to local

Tekniumb0af119963e3
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 591569b9870..ae12d1644e4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -250,6 +250,7 @@ import {
fetchPrimaryProfileSessions,
fetchRegistrySessionRows,
fetchRemoteProfileSessions,
+ findRemoteOwnerProfileForSession,
mergeProfileSessionWindow,
type RegistrySessionSource,
spliceRegistrySessionRows
@@ -13483,10 +13484,21 @@ async function interceptSessionRequestForRemote(request) {
// - global remote mode: ONE backend serves every profile via ?profile=, so
// route there and KEEP the profile param so it opens the right state.db.
if (/^\/api\/sessions\/[^/]+(\/messages)?$/.test(pathname)) {
- const profile = (searchParams.get('profile') || request.profile || '').trim()
+ let profile = (searchParams.get('profile') || request.profile || '').trim()
if (!profile) {
- return undefined
+ // No explicit owner hint (#85834). The list endpoints above already know
+ // which remote profile owns each row (remoteSessionList tags s.profile),
+ // but a caller without a hint used to fall straight through to the LOCAL
+ // backend and 404 on its state.db even though the session lives on a
+ // remote. Consult the same remote lists to find the owner; only fall
+ // through when the id is genuinely unknown remotely.
+ const sessionId = decodeURIComponent(pathname.split('/')[3] || '')
+ profile = (await remoteOwnerProfileForSession(sessionId)) || ''
+
+ if (!profile) {
+ return undefined
+ }
}
// Preserve every non-profile query param (limit/offset/order pagination —
@@ -13545,6 +13557,39 @@ async function remoteSessionList(profile, searchParams) {
return { ...(data as any), sessions: rowsOf(data) }
}
+// #85834: find which remote profile owns a session id when the caller gave no
+// profile hint (pure lookup lives in profile-session-routing.ts). Results are
+// memoized briefly so a burst of hint-less reads (transcript + messages)
+// costs one sweep across the configured remotes.
+const remoteOwnerBySessionId = new Map<string, { at: number; profile: null | string }>()
+const REMOTE_OWNER_CACHE_TTL_MS = 30_000
+
+async function remoteOwnerProfileForSession(sessionId: string) {
+ if (!sessionId) {
+ return null
+ }
+
+ const remoteProfiles = configuredRemoteProfileNames()
+
+ if (remoteProfiles.length === 0) {
+ return null
+ }
+
+ const cached = remoteOwnerBySessionId.get(sessionId)
+
+ if (cached && Date.now() - cached.at < REMOTE_OWNER_CACHE_TTL_MS) {
+ return cached.profile
+ }
+
+ const owner = await findRemoteOwnerProfileForSession(sessionId, remoteProfiles, (profile, params) =>
+ remoteSessionList(profile, params)
+ ).catch(() => null)
+
+ remoteOwnerBySessionId.set(sessionId, { at: Date.now(), profile: owner })
+
+ return owner
+}
+
// Resolve one /api/profiles/sessions slice with remote profiles spliced in —
// the same branch logic as the GET /api/profiles/sessions intercept, but always
// returns data (never `undefined`) so a batched caller can compose slices. A
diff --git a/apps/desktop/electron/profile-session-routing.test.ts b/apps/desktop/electron/profile-session-routing.test.ts
index 1c6b916c199..8cc530ff992 100644
--- a/apps/desktop/electron/profile-session-routing.test.ts
+++ b/apps/desktop/electron/profile-session-routing.test.ts
@@ -7,6 +7,7 @@ import {
fetchPrimaryProfileSessions,
fetchRegistrySessionRows,
fetchRemoteProfileSessions,
+ findRemoteOwnerProfileForSession,
mergeProfileSessionWindow,
spliceRegistrySessionRows
} from './profile-session-routing'
@@ -374,3 +375,43 @@ test('splice: registry rows dedupe by id and extend per-profile totals', () => {
assert.equal(totals.default, 2) // untagged registry row counts under default
assert.equal(totals.work, 1) // deduped row does not double-count
})
+
+test('finds the remote owner profile for a hint-less session read (#85834)', async () => {
+ const owner = await findRemoteOwnerProfileForSession('sess-remote', ['vps-a', 'vps-b'], async profile => {
+ if (profile === 'vps-b') {
+ return { sessions: [{ id: 'sess-remote' }] } as never
+ }
+
+ return { sessions: [{ id: 'other' }] } as never
+ })
+
+ assert.equal(owner, 'vps-b')
+})
+
+test('remote owner lookup matches a compression lineage root id too', async () => {
+ const owner = await findRemoteOwnerProfileForSession('root-1', ['vps-a'], async () => {
+ return { sessions: [{ id: 'tip-2', _lineage_root_id: 'root-1' }] } as never
+ })
+
+ assert.equal(owner, 'vps-a')
+})
+
+test('remote owner lookup returns null when no remote lists the id or remotes fail', async () => {
+ const missing = await findRemoteOwnerProfileForSession('sess-x', ['vps-a'], async () => {
+ return { sessions: [{ id: 'other' }] } as never
+ })
+
+ assert.equal(missing, null)
+
+ const dead = await findRemoteOwnerProfileForSession('sess-x', ['vps-a'], async () => {
+ throw new Error('remote unavailable')
+ })
+
+ assert.equal(dead, null)
+
+ const noRemotes = await findRemoteOwnerProfileForSession('sess-x', [], async () => {
+ throw new Error('never called')
+ })
+
+ assert.equal(noRemotes, null)
+})
diff --git a/apps/desktop/electron/profile-session-routing.ts b/apps/desktop/electron/profile-session-routing.ts
index e096b7324d2..c6694010219 100644
--- a/apps/desktop/electron/profile-session-routing.ts
+++ b/apps/desktop/electron/profile-session-routing.ts
@@ -374,3 +374,36 @@ export async function fetchRemoteProfileSessions(
offset: requestedOffset
}
}
+
+/**
+ * #85834: which remote profile owns `sessionId`, when a /api/sessions/{id}
+ * caller supplied no profile hint. Reads the same per-remote lists the list
+ * endpoints splice into the sidebar (each fetch is per-profile, so a hit IS
+ * the owner). Dead remotes contribute nothing; returns null when no remote
+ * lists the id — the intercept then falls through to the local backend
+ * exactly as before.
+ */
+export async function findRemoteOwnerProfileForSession(
+ sessionId: string,
+ remoteProfiles: readonly string[],
+ listForProfile: (profile: string, searchParams: URLSearchParams) => Promise<SessionListResponse | null>
+): Promise<null | string> {
+ if (!sessionId || remoteProfiles.length === 0) {
+ return null
+ }
+
+ const params = new URLSearchParams()
+ params.set('limit', '200')
+ params.set('offset', '0')
+
+ const matches = await Promise.all(
+ remoteProfiles.map(async profile => {
+ const list = await listForProfile(profile, params).catch(() => null)
+ const rows = Array.isArray(list?.sessions) ? (list.sessions as Array<Record<string, unknown>>) : []
+
+ return rows.some(row => row?.id === sessionId || row?._lineage_root_id === sessionId) ? profile : null
+ })
+ )
+
+ return matches.find(profile => profile !== null) ?? null
+}