Skip to content

The agent that grows with you

fix(desktop): keep active Bot Chat profile when refreshing history

KoNit-K7a5a9108f889
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
index a574c24d8c2..fc6975231bf 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts
@@ -7,6 +7,7 @@ import { $changeEventsAvailable, notifySessionsChanged, resetLiveSync } from '@/
import {
$activeSessionId,
$selectedStoredSessionId,
+ _resetSessionOwnerHintsForTests,
setBusy,
setCronSessions,
setMessagingSessions,
@@ -177,6 +178,7 @@ afterEach(() => {
vi.restoreAllMocks()
clearAllSessionStates()
$sessionTiles.set([])
+ _resetSessionOwnerHintsForTests()
resetTypingActivityTracking()
})
@@ -648,6 +650,33 @@ describe('reconcileActiveTranscript', () => {
expect(fixture.updateSessionState).not.toHaveBeenCalled()
})
+ it('keeps the active named-profile owner when a visible default duplicate shares the stored id', async () => {
+ const S = ACTIVE_STORED_ID
+ const namedOwner = { connectionId: 'remote', mode: 'remote' as const, profile: 'omar' }
+
+ $activeSessionId.set(ACTIVE_RUNTIME_ID)
+ $selectedStoredSessionId.set(S)
+ setSessionOwnerHint(S, namedOwner)
+ // Bot Chat lives in a workspace tile (often hidden from $sessions) while a
+ // root-DB duplicate of the same stored id remains visible as `default`.
+ $sessionTiles.set([
+ {
+ storedSessionId: S,
+ runtimeId: ACTIVE_RUNTIME_ID,
+ ownerRoute: namedOwner
+ }
+ ])
+ setSessions([{ id: S, profile: 'default', source: 'desktop', connectionId: 'local' } as never])
+ const fixture = makeRefresh(resolveActiveTranscriptSession)
+ vi.mocked(getLatestSessionMessages).mockResolvedValue(transcript('named-profile answer') as never)
+
+ await fixture.refresh()
+
+ expect(getLatestSessionMessages).toHaveBeenCalledWith(S, expect.objectContaining({ profile: 'omar' }))
+ expect(getLatestSessionMessages).not.toHaveBeenCalledWith(S, 'default')
+ expect(getLatestSessionMessages).not.toHaveBeenCalledWith(S, expect.objectContaining({ profile: 'default' }))
+ })
+
it('uses the presentation profile when a hidden owner has no target profile', async () => {
const hiddenStoredSessionId = 'hidden-no-target'
setSessionOwnerHint(hiddenStoredSessionId, {
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
index d2ef0bc56ab..5bf6cbc9505 100644
--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts
@@ -39,9 +39,90 @@ interface ActiveTranscriptSession {
profile?: string | null
}
-/** Resolve an active transcript from visible rows or its unique hidden owner. */
-export function resolveActiveTranscriptSession(storedSessionId: string): ActiveTranscriptSession | undefined {
- const visible = ownerLookupSessionRows().find(session => sessionMatchesStoredId(session, storedSessionId))
+function ownerRouteProfile(owner: SessionProfileRoute): string {
+ return (owner.targetProfile ?? owner.profile).trim() || 'default'
+}
+
+function visibleRowMatchesOwner(
+ row: { connection_id?: null | string; profile?: null | string },
+ owner: SessionProfileRoute
+): boolean {
+ const rowProfile = (row.profile ?? '').trim() || 'default'
+
+ if (rowProfile !== ownerRouteProfile(owner)) {
+ return false
+ }
+
+ const rowConnection = (row.connection_id ?? '').trim()
+ const ownerConnection = owner.connectionId.trim()
+
+ if (!rowConnection || !ownerConnection) {
+ return true
+ }
+
+ return rowConnection === ownerConnection
+}
+
+/** Verified owner of the ACTIVE transcript: the tile bound to this stored
+ * (+ runtime) id, else a unique hint that the same tile corroborates.
+ * A unique hint alone is not verified — visible rows must still override a
+ * stale leftover hint. */
+function preferredActiveTranscriptOwner(
+ storedSessionId: string,
+ runtimeSessionId?: null | string
+): SessionProfileRoute | undefined {
+ const runtimeId = runtimeSessionId ?? $activeSessionId.get()
+ const tiles = $sessionTiles.get().filter(tile => tile.storedSessionId === storedSessionId)
+ const activeTile =
+ (runtimeId ? tiles.find(tile => tile.runtimeId === runtimeId) : undefined) ??
+ (tiles.length === 1 ? tiles[0] : undefined)
+
+ if (activeTile?.ownerRoute) {
+ return activeTile.ownerRoute
+ }
+
+ const uniqueHint = getSessionOwnerHint(storedSessionId)
+
+ return uniqueHint && activeTile ? uniqueHint : undefined
+}
+
+/** Profile/connection scope used to read an active transcript from storage. */
+export function profileScopeForTranscriptSession(stored: ActiveTranscriptSession | undefined): ProfileScope {
+ if (!stored) {
+ return undefined
+ }
+
+ if (stored.ownerRoute) {
+ return {
+ connectionId: stored.ownerRoute.connectionId,
+ profile: stored.ownerRoute.targetProfile ?? stored.ownerRoute.profile
+ }
+ }
+
+ return stored.profile
+}
+
+/** Resolve an active transcript from a verified owner, matching visible rows,
+ * or its unique hidden owner. Visible-first remains when no owner is verified
+ * so a stale hint cannot rewrite a viewed session row. */
+export function resolveActiveTranscriptSession(
+ storedSessionId: string,
+ runtimeSessionId?: null | string
+): ActiveTranscriptSession | undefined {
+ const visibleRows = ownerLookupSessionRows().filter(session => sessionMatchesStoredId(session, storedSessionId))
+ const verifiedOwner = preferredActiveTranscriptOwner(storedSessionId, runtimeSessionId)
+
+ if (verifiedOwner) {
+ const matchingVisible = visibleRows.find(row => visibleRowMatchesOwner(row, verifiedOwner))
+
+ if (matchingVisible) {
+ return { ownerRoute: verifiedOwner, profile: matchingVisible.profile }
+ }
+
+ return { ownerRoute: verifiedOwner, profile: verifiedOwner.profile }
+ }
+
+ const visible = visibleRows[0]
if (visible) {
return { profile: visible.profile }
@@ -232,12 +313,7 @@ export async function reconcileActiveTranscript({
requestSequenceRef.current = requestId
try {
- const profileScope: ProfileScope = stored.ownerRoute
- ? {
- connectionId: stored.ownerRoute.connectionId,
- profile: stored.ownerRoute.targetProfile ?? stored.ownerRoute.profile
- }
- : stored.profile
+ const profileScope: ProfileScope = profileScopeForTranscriptSession(stored)
const latest = await getLatestSessionMessages(storedSessionId, profileScope)
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index 097e0d10fd3..dafcca25fcd 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -152,6 +152,7 @@ import { UpdatesOverlay } from '../updates-overlay'
import { ContribWiringContext } from './context'
import {
+ profileScopeForTranscriptSession,
reconcileActiveTranscript,
resolveActiveTranscriptSession,
useBackgroundSync
@@ -484,7 +485,9 @@ export function ContribWiring({ children }: { children: ReactNode }) {
return
}
- const storedProfile = $sessions.get().find(session => sessionMatchesStoredId(session, storedSessionId))?.profile
+ const storedProfile = profileScopeForTranscriptSession(
+ resolveActiveTranscriptSession(storedSessionId, runtimeSessionId)
+ )
for (let index = 0; index < Math.max(1, attempts); index += 1) {
try {
@@ -533,7 +536,7 @@ export function ContribWiring({ children }: { children: ReactNode }) {
activeSessionIdRef,
busyRef,
requestSequenceRef: activeTranscriptRequestSequenceRef,
- resolveSession: resolveActiveTranscriptSession,
+ resolveSession: id => resolveActiveTranscriptSession(id, activeSessionIdRef.current),
selectedStoredSessionIdRef,
signatureRef: activeTranscriptSignatureRef,
updateSessionState