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

api/sessions.ts imported $connection from @/store/session to learn whether
an untagged read lands on the local pool. That closes a real module cycle
(api/sessions -> store/session -> session-unread-remote -> @/hermes ->
api/sessions), inert only because the read sat inside a function body, and
it breaks the "no store import" contract client.ts documents for exactly
this reason.

Publish the fact through the seam that already exists: store/session's
setConnection pushes local/remote into client.ts (setApiRequestLocalMode,
next to setApiRequestProfile / setApiRequestConnection), and
ambientOwnerConnectionId() answers "which backend serves an untagged
request" for the owner key. A null descriptor (reconnect blip) keeps the
last mode, matching rescopeConnectionScopedStores.
This commit is contained in:
kshitijk4poor
2026-09-11 11:47:36 +05:30
committed by kshitij
parent e52c97b8e7
commit 1affcb9386
4 changed files with 33 additions and 12 deletions
+19
View File
@@ -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.
*
@@ -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')
+3 -8
View File
@@ -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
+8
View File
@@ -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()))
}