fix(desktop): Bot Mode chats no longer paint blank when switching between running bots

resolveStoredSession never probed the ACTIVE profile: the unscoped
/api/sessions GET routes to the PRIMARY backend (not the active
gateway's), and the cross-profile probe loop explicitly skipped the
active key. A hidden Bot Mode canonical chat — never present in the
sidebar cache — owned by the focused bot therefore resolved to
undefined on every switch. The transcript prefetch then went unscoped
to the primary backend, 404'd, and the thread painted empty until the
user opened the session explicitly via right-click → Sessions (which
seeds the cache with a profile-stamped row).

Probe the active profile first in the by-id ladder, so hidden and
uncached sessions on the focused profile resolve with ownership and the
prefetch routes to the owning backend.

Live-repro'd headless via CDP with 3 bot profiles mid-turn: before the
fix every focus-switch painted a blank thread ('Waking up <bot>…');
after, the full transcript paints. Reported by @tbkbossswaglord.
This commit is contained in:
Teknium
2026-08-19 02:19:44 -07:00
parent ac3d7ddae7
commit 1a19fedb5a
2 changed files with 41 additions and 8 deletions
@@ -62,14 +62,31 @@ describe('resolveStoredSession profile ownership', () => {
it('treats a profile-less cache hit as unresolved when multiple profiles exist', async () => {
$sessions.set([session({ id: 's1' })])
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockResolvedValueOnce(session({ id: 's1', profile: 'default' }))
const resolved = await resolveStoredSession('s1')
expect(resolved?.profile).toBe('default')
// rung 2 (bare) then rung 3 (stamped cross-profile probe)
// rung 2 (bare) then rung 3 (stamped probes: active profile first)
expect(mockGetSession).toHaveBeenNthCalledWith(1, 's1')
expect(mockGetSession).toHaveBeenNthCalledWith(2, 's1', 'default')
expect(mockGetSession).toHaveBeenNthCalledWith(2, 's1', 'meta')
expect(mockGetSession).toHaveBeenNthCalledWith(3, 's1', 'default')
})
it('probes the ACTIVE profile when the unscoped lookup 404s (hidden Bot Mode chat)', async () => {
// A hidden Bot Mode canonical chat is never in the sidebar cache, and the
// unscoped GET routes to the PRIMARY backend (404). The owner is the
// active profile itself — it must be probed, not skipped.
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockResolvedValueOnce(session({ id: 's1', message_count: 2 }))
const resolved = await resolveStoredSession('s1')
expect(resolved?.profile).toBe('meta')
expect(mockGetSession).toHaveBeenNthCalledWith(1, 's1')
expect(mockGetSession).toHaveBeenNthCalledWith(2, 's1', 'meta')
expect($sessions.get().find(s => s.id === 's1')?.profile).toBe('meta')
})
it('accepts a profile-less cache hit for single-profile users', async () => {
@@ -96,6 +113,7 @@ describe('resolveStoredSession profile ownership', () => {
// Per-profile remote override: Electron strips the desktop alias before
// forwarding, so the standalone backend stamps its backend-local root.
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockResolvedValueOnce(session({ id: 's1', profile: 'default' }))
$activeGatewayProfile.set('default')
$profiles.set(profiles('default', 'meta'))
@@ -107,6 +125,7 @@ describe('resolveStoredSession profile ownership', () => {
})
it('stamps the probed profile on a scoped hit from an older backend that omits it', async () => {
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockResolvedValueOnce(session({ id: 's1' }))
@@ -118,6 +137,7 @@ describe('resolveStoredSession profile ownership', () => {
})
it('resolveSessionProfile routes a default-profile session from a non-default gateway', async () => {
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))
mockGetSession.mockResolvedValueOnce(session({ id: 's1', profile: 'default' }))
@@ -1326,17 +1326,30 @@ export async function resolveStoredSession(storedSessionId: string): Promise<Ses
// Not on the active profile — fall through to the cross-profile probe.
}
// Multi-profile only: probe each other profile by id (still one cheap lookup
// Multi-profile only: probe each profile by id (still one cheap lookup
// each) rather than pulling every profile's recent sessions. The first hit
// carries its owning `profile`, which routes the resume to the right backend.
//
// The ACTIVE profile is probed too, and first. The unscoped rung above does
// NOT cover it: Electron routes a profile-less /api/sessions GET to the
// PRIMARY backend, not the active gateway's, so a session owned by the
// active non-primary profile 404s there. Skipping the active key then left
// it as the ONE profile never probed — a hidden Bot Mode chat (never in the
// sidebar cache) owned by the focused bot resolved to undefined on every
// switch, the transcript prefetch went unscoped to the primary, and the
// thread painted empty until an explicitly-profiled row (right-click →
// Sessions) seeded the cache.
const activeKey = normalizeProfileKey($activeGatewayProfile.get())
const otherProfiles = $profiles
.get()
.map(profile => normalizeProfileKey(profile.name))
.filter(key => key !== activeKey)
const probeProfiles = [
activeKey,
...$profiles
.get()
.map(profile => normalizeProfileKey(profile.name))
.filter(key => key !== activeKey)
]
for (const profile of otherProfiles) {
for (const profile of probeProfiles) {
try {
const session = await getSession(storedSessionId, profile)