Skip to content

The agent that grows with you

refactor(desktop): resolve the ambient tail owner through the api/client seam

kshitijk4poor1affcb93862b
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index fc98817b358..b61f0d33b4c 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -86,6 +86,25 @@ export function connectionScoped(): { connectionId?: string } {
return _apiConnectionId ? { connectionId: _apiConnectionId } : {}
}
+// Whether the window's primary connection is the local pool. Pushed from
+// store/session's setConnection (same no-store-import contract as _apiProfile)
+// so api/ helpers can name the backend an UNTAGGED request lands on without
+// importing the heavy session store — which would close a module cycle
+// through @/hermes.
+let _apiLocalMode = false
+
+export function setApiRequestLocalMode(local: boolean): void {
+ _apiLocalMode = local
+}
+
+/** The connection an ambient (untagged) request is served by: the registry
+ * tag when one is active, else `'local'` for the local pool. Identity only —
+ * never send this as a request pin (an explicit `'local'` bypasses Electron's
+ * legacy per-profile remote overrides). */
+export function ambientOwnerConnectionId(): string | undefined {
+ return _apiConnectionId ?? (_apiLocalMode ? 'local' : undefined)
+}
+
/** Send a REST request to the renderer's active registry source. Request-level
* routing may override the active source for an explicitly-owned resource.
*
diff --git a/apps/desktop/src/api/session-transcript-paging.test.ts b/apps/desktop/src/api/session-transcript-paging.test.ts
index 3f620742fe8..e3bbbf2c30f 100644
--- a/apps/desktop/src/api/session-transcript-paging.test.ts
+++ b/apps/desktop/src/api/session-transcript-paging.test.ts
@@ -6,11 +6,10 @@ import {
transcriptBackfillAvailable
} from '@/app/chat/transcript-backfill'
import { toChatMessages } from '@/lib/chat-messages'
-import { $connection } from '@/store/session'
import { $transcriptTailBySessionId, transcriptTailState } from '@/store/transcript-tail'
import type { SessionMessagesResponse } from '@/types/hermes'
-import { setApiRequestConnection, setApiRequestProfile } from './client'
+import { setApiRequestConnection, setApiRequestLocalMode, setApiRequestProfile } from './client'
import { getLatestSessionMessages, LATEST_SESSION_MESSAGES_LIMIT } from './sessions'
// Zero timestamps use the conversion-time clock for IDs; keep fixtures stable.
@@ -31,11 +30,11 @@ describe('session transcript pagination ownership', () => {
$transcriptTailBySessionId.set({})
Object.defineProperty(window, 'hermesDesktop', { configurable: true, value: { api } })
setApiRequestProfile('default')
- $connection.set({ mode: 'local' } as NonNullable<ReturnType<typeof $connection.get>>)
+ setApiRequestLocalMode(true)
})
afterEach(() => {
- $connection.set(null)
+ setApiRequestLocalMode(false)
setApiRequestConnection(null)
setApiRequestProfile(null)
Reflect.deleteProperty(window, 'hermesDesktop')
diff --git a/apps/desktop/src/api/sessions.ts b/apps/desktop/src/api/sessions.ts
index 2ebbf445bf1..7387bb27341 100644
--- a/apps/desktop/src/api/sessions.ts
+++ b/apps/desktop/src/api/sessions.ts
@@ -1,7 +1,6 @@
import { isMissingRestEndpoint } from '@/lib/gateway-rpc'
import { maybeBackfillLegacySessionOwners } from '@/lib/legacy-session-owner-backfill'
import { stampRowsWithOwningConnection } from '@/lib/session-owner-stamp'
-import { $connection } from '@/store/session'
import { recordTranscriptTail } from '@/store/transcript-tail'
import type {
PaginatedSessions,
@@ -12,6 +11,7 @@ import type {
} from '@/types/hermes'
import {
+ ambientOwnerConnectionId,
capabilityScoped,
connectionScoped,
getApiRequestConnection,
@@ -459,13 +459,8 @@ export function getLatestSessionMessages(
// duplicate tail entries and "Show earlier" cannot resolve the loaded tail.
// Capture before awaiting: the active gateway may change during the read.
const tailScope = { ...connectionScoped(), ...sessionScoped(profile) }
- const ownerScope = { ...tailScope }
-
- // Normalize only the lookup key: a local request pin would bypass Electron's
- // legacy per-profile remote overrides. Backfill must retain the original route.
- if (!ownerScope.connectionId && $connection.get()?.mode === 'local') {
- ownerScope.connectionId = 'local'
- }
+ // Normalize only the lookup key; backfill must replay the original route.
+ const ownerScope = { ...tailScope, connectionId: tailScope.connectionId || ambientOwnerConnectionId() }
// includeCompacted: durable display history must include rows preserved by
// in-place compaction (active=0, compacted=1); without them the transcript
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index 5ce4c9e6205..11ac2d963b3 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -1,6 +1,7 @@
import type { ConnectionState } from '@hermes/shared'
import { atom, computed } from 'nanostores'
+import { setApiRequestLocalMode } from '@/api/client'
import { lastVisibleMessageIsUser } from '@/app/chat/thread-loading'
import type { ContextSuggestion } from '@/app/types'
import type { HermesConnection } from '@/global'
@@ -1176,6 +1177,13 @@ export const setConnection = (next: Updater<HermesConnection | null>) => {
// keeps the current scope.
rescopeConnectionScopedStores($connection.get())
syncCronModelImpactConnection($connection.get())
+
+ const mode = $connection.get()?.mode
+
+ if (mode) {
+ setApiRequestLocalMode(mode === 'local')
+ }
+
rescopeComposerSelection(composerScopeForConnection($connection.get()))
}