Skip to content

The agent that grows with you

fix(desktop): retain messaging totals per profile

David Dudok de Wit1f24b3ed2ec3
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsx
index ed41b199026..5166c968062 100644
--- a/apps/desktop/src/app/chat/sidebar/index.tsx
+++ b/apps/desktop/src/app/chat/sidebar/index.tsx
@@ -78,6 +78,8 @@ import {
$profiles,
$profileScope,
ALL_PROFILES,
+ messagingProfileFor,
+ messagingTotalsKey,
normalizeProfileKey
} from '@/store/profile'
import {
@@ -396,6 +398,7 @@ export function ChatSidebar({
// profile while scope is still ALL (persisted), the rail is hidden and they'd
// otherwise be stuck in the grouped view with no way out.
const showAllProfiles = multiProfile && profileScope === ALL_PROFILES
+ const messagingProfile = messagingProfileFor(profileScope)
const agentOrderIds = useStore($sidebarSessionOrderIds)
const agentOrderManual = useStore($sidebarSessionOrderManual)
const workspaceOrderIds = useStore($sidebarWorkspaceOrderIds)
@@ -1202,7 +1205,7 @@ export function ChatSidebar({
return [...bySource.entries()]
.map(([sourceId, list]) => {
const ordered = [...list].sort((a, b) => sessionTime(b) - sessionTime(a))
- const known = messagingPlatformTotals[sourceId]
+ const known = messagingPlatformTotals[messagingTotalsKey(messagingProfile, sourceId)]
const unpinnedKnown = known == null ? null : Math.max(0, known - (pinnedBySource.get(sourceId) ?? 0))
const total = Math.max(ordered.length, unpinnedKnown ?? 0)
@@ -1218,7 +1221,7 @@ export function ChatSidebar({
}
})
.sort((a, b) => sessionTime(b.sessions[0]) - sessionTime(a.sessions[0]))
- }, [visibleMessagingSessions, messagingPlatformTotals, messagingTruncated, isPinnedSession])
+ }, [visibleMessagingSessions, messagingPlatformTotals, messagingTruncated, isPinnedSession, messagingProfile])
// Grouping by profile: one collapsible group per profile, color on the header
// (not on every row). Default profile floats to the top, the rest alpha.
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
index 42333099a57..ac8104d4780 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.test.tsx
@@ -354,6 +354,44 @@ describe('messaging profile scope', () => {
expect(listAllProfileSessions.mock.calls[0][4]).toBe('work')
expect($messagingSessions.get().map(s => s.id)).toEqual(['m1', 'm2'])
+ expect($messagingPlatformTotals.get()).toEqual({ 'work:signal': 2 })
+ })
+
+ it('keeps resolved platform totals separate across profile switches', async () => {
+ listAllProfileSessions.mockResolvedValue({
+ sessions: [row('work-signal', { profile: 'work', source: 'signal' })],
+ total: 42
+ })
+
+ const { rerender, result } = renderHook(({ profileScope }) => useSessionListActions({ profileScope }), {
+ initialProps: { profileScope: 'work' }
+ })
+
+ await act(async () => {
+ await result.current.loadMoreMessagingForPlatform('signal')
+ })
+
+ expect($messagingPlatformTotals.get()).toEqual({ 'work:signal': 42 })
+
+ rerender({ profileScope: 'personal' })
+
+ expect($messagingPlatformTotals.get()['personal:signal']).toBeUndefined()
+ expect($messagingPlatformTotals.get()['work:signal']).toBe(42)
+
+ listAllProfileSessions.mockResolvedValue({
+ sessions: [row('personal-signal', { profile: 'personal', source: 'signal' })],
+ total: 3
+ })
+
+ await act(async () => {
+ await result.current.loadMoreMessagingForPlatform('signal')
+ })
+
+ expect($messagingPlatformTotals.get()).toEqual({ 'personal:signal': 3, 'work:signal': 42 })
+
+ rerender({ profileScope: 'work' })
+
+ expect($messagingPlatformTotals.get()['work:signal']).toBe(42)
})
it('ignores an in-flight response after the active profile changes', async () => {
@@ -386,9 +424,9 @@ describe('messaging profile scope', () => {
expect($messagingSessions.get().map(session => session.id)).toEqual(['personal-message'])
})
- it('does not let a callback captured before a profile switch clear current totals', async () => {
+ it('does not let a callback captured before a profile switch disturb current totals', async () => {
listAllProfileSessions.mockResolvedValue({ sessions: [], total: 0 })
- setMessagingPlatformTotals({ signal: 12 })
+ setMessagingPlatformTotals({ 'work:signal': 12 })
const { rerender, result } = renderHook(({ profileScope }) => useSessionListActions({ profileScope }), {
initialProps: { profileScope: 'work' }
@@ -403,6 +441,6 @@ describe('messaging profile scope', () => {
})
expect(listAllProfileSessions).not.toHaveBeenCalled()
- expect($messagingPlatformTotals.get()).toEqual({ signal: 12 })
+ expect($messagingPlatformTotals.get()).toEqual({ 'work:signal': 12 })
})
})
diff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
index ecea0021c97..471d5098b71 100644
--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts
@@ -17,7 +17,7 @@ import {
SIDEBAR_FILTERED_PAGE_SIZE,
SIDEBAR_SESSIONS_PAGE_SIZE
} from '@/store/layout'
-import { ALL_PROFILES, normalizeProfileKey } from '@/store/profile'
+import { messagingProfileFor, messagingTotalsKey, normalizeProfileKey } from '@/store/profile'
import { $removedSessionIds } from '@/store/projects'
import {
$messagingSessions,
@@ -96,9 +96,6 @@ interface UseSessionListActionsArgs {
profileScope: string
}
-const sessionProfileForScope = (profileScope: string): string =>
- profileScope === ALL_PROFILES ? 'all' : normalizeProfileKey(profileScope)
-
/** Owns the sidebar's session-list fetching + paging: recents, cron runs/jobs,
* and the per-platform messaging slices. Returns the callbacks the controller
* wires into the sidebar and refresh effects. */
@@ -114,17 +111,16 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
// the sidebar's profile scope; otherwise a remote-profile switch leaks one
// instance's Telegram/Signal sections into another instance's workspace.
const refreshMessagingSessions = useCallback(async () => {
- const sessionProfile = sessionProfileForScope(profileScope)
+ const sessionProfile = messagingProfileFor(profileScope)
// A callback captured before a profile switch may still be queued by an
- // event subscription. Do not let it start a request or clear current totals.
- if (sessionProfileForScope(profileScopeRef.current) !== sessionProfile) {
+ // event subscription. Do not let it start a request against the old scope.
+ if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {
return
}
const requestId = refreshMessagingSessionsRequestRef.current + 1
refreshMessagingSessionsRequestRef.current = requestId
- setMessagingPlatformTotals({})
try {
const result = await listAllProfileSessions(MESSAGING_SECTION_LIMIT, 1, 'exclude', 'recent', sessionProfile, {
@@ -133,7 +129,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
if (
refreshMessagingSessionsRequestRef.current !== requestId ||
- sessionProfileForScope(profileScopeRef.current) !== sessionProfile
+ messagingProfileFor(profileScopeRef.current) !== sessionProfile
) {
return
}
@@ -156,9 +152,9 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
// every other platform's rows untouched. Resolves the platform's exact total.
const loadMoreMessagingForPlatform = useCallback(
async (platform: string) => {
- const sessionProfile = sessionProfileForScope(profileScope)
+ const sessionProfile = messagingProfileFor(profileScope)
- if (sessionProfileForScope(profileScopeRef.current) !== sessionProfile) {
+ if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {
return
}
@@ -177,7 +173,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
{ source: platform }
)
- if (sessionProfileForScope(profileScopeRef.current) !== sessionProfile) {
+ if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {
return
}
@@ -189,7 +185,9 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
])
const total = result.total ?? incoming.length
- setMessagingPlatformTotals(prev => ({ ...prev, [platform]: Math.max(total, incoming.length) }))
+ const totalsKey = messagingTotalsKey(sessionProfile, platform)
+
+ setMessagingPlatformTotals(prev => ({ ...prev, [totalsKey]: Math.max(total, incoming.length) }))
},
[profileScope]
)
@@ -203,7 +201,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
// own jobs; ALL_PROFILES keeps the unified view.
const refreshCronJobs = useCallback(async () => {
try {
- await refreshCronJobsStore(sessionProfileForScope(profileScope))
+ await refreshCronJobsStore(messagingProfileFor(profileScope))
} catch {
// Non-fatal: the cron section just keeps its last-known jobs.
}
@@ -235,7 +233,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
// with few recent sessions isn't windowed out of the cross-profile
// recency page and never inherits another profile's cron or messaging
// sections. ALL_PROFILES remains the explicit unified view.
- const sessionProfile = sessionProfileForScope(profileScope)
+ const sessionProfile = messagingProfileFor(profileScope)
// Batched: one request opens each profile DB once and returns all three
// source-scoped slices, instead of three separate listAllProfileSessions
diff --git a/apps/desktop/src/store/profile.ts b/apps/desktop/src/store/profile.ts
index 0d38269479f..d0fd760b509 100644
--- a/apps/desktop/src/store/profile.ts
+++ b/apps/desktop/src/store/profile.ts
@@ -315,6 +315,14 @@ export async function ensureGatewayProfile(profile: string | null | undefined):
export const ALL_PROFILES = '__all__'
+// Messaging totals belong to the Desktop profile route as well as the source.
+// Shared by the fetcher and sidebar so neither can read another profile's count.
+export const messagingProfileFor = (profileScope: string): string =>
+ profileScope === ALL_PROFILES ? 'all' : normalizeProfileKey(profileScope)
+
+export const messagingTotalsKey = (messagingProfile: string, sourceId: string): string =>
+ `${messagingProfile}:${sourceId}`
+
const SHOW_ALL_PROFILES_STORAGE_KEY = 'hermes.desktop.showAllProfiles'
// Opt-in unified view. When false, scope follows the live gateway profile, so