Skip to content

The agent that grows with you

feat(desktop): count unread sessions from the shared status map

Brooklyn Nicholson508b3cf65d8d
diff --git a/apps/desktop/src/store/session-dot-state.test.ts b/apps/desktop/src/store/session-dot-state.test.ts
index 1661c3ba093..81f20f9b0b0 100644
--- a/apps/desktop/src/store/session-dot-state.test.ts
+++ b/apps/desktop/src/store/session-dot-state.test.ts
@@ -4,7 +4,7 @@ import { createClientSessionState } from '@/lib/chat-runtime'
import type { SessionInfo } from '@/types/hermes'
import { $sessions, $unreadFinishedSessionIds, setSessions } from './session'
-import { $delegatingSessionIds, $sessionDotStateById, hasLiveTurn, showsRunningArc } from './session-dot-state'
+import { $delegatingSessionIds, $sessionDotStateById, hasLiveTurn, showsRunningArc, unreadSessionCount } from './session-dot-state'
import { clearAllSessionStates, publishSessionState } from './session-states'
import { $unreadWriteGuard } from './session-unread-remote'
import { $subagentsBySession, type SubagentProgress } from './subagents'
@@ -148,3 +148,20 @@ describe('persisted unread (backend watermark)', () => {
expect($sessionDotStateById.get()['s1'] ?? 'idle').not.toBe('unread')
})
})
+
+describe('unreadSessionCount', () => {
+ it('counts listed unread rows and skips archived', () => {
+ expect(
+ unreadSessionCount({ a: 'unread', b: 'working', c: 'unread' }, [
+ { id: 'a' },
+ { id: 'b' },
+ { archived: true, id: 'c' },
+ { id: 'missing' }
+ ])
+ ).toBe(1)
+ })
+
+ it('does not count alias keys that are not listed rows', () => {
+ expect(unreadSessionCount({ tip: 'unread', root: 'unread' }, [{ id: 'tip' }])).toBe(1)
+ })
+})
diff --git a/apps/desktop/src/store/session-dot-state.ts b/apps/desktop/src/store/session-dot-state.ts
index f6f30169ff4..58c0f8182c6 100644
--- a/apps/desktop/src/store/session-dot-state.ts
+++ b/apps/desktop/src/store/session-dot-state.ts
@@ -28,7 +28,7 @@ import { computed } from 'nanostores'
import { stableArray, stableRecord } from '@/lib/stable-array'
import { $backgroundRunningSessionIds } from './composer-status'
-import { $sessions, $unreadFinishedSessionIds, lineageAliases } from './session'
+import { $cronSessions, $messagingSessions, $sessions, $unreadFinishedSessionIds, lineageAliases } from './session'
import {
$attentionSessionIds,
$draftSessionIds,
@@ -182,3 +182,27 @@ export const $sessionDotStateById = computed(
return (dotStates = stableRecord(dotStates, next))
}
)
+
+/** Listed, non-archived rows whose resolved status is unread. Alias keys in
+ * `$sessionDotStateById` are ignored unless they are themselves a listed row. */
+export function unreadSessionCount(
+ byId: Readonly<Record<string, SessionDotState>>,
+ ...lists: Array<readonly { archived?: boolean; id: string }[]>
+): number {
+ let n = 0
+
+ for (const rows of lists) {
+ for (const row of rows) {
+ if (!row.archived && byId[row.id] === 'unread') {
+ n++
+ }
+ }
+ }
+
+ return n
+}
+
+export const $unreadSessionCount = computed(
+ [$sessionDotStateById, $sessions, $cronSessions, $messagingSessions],
+ (byId, sessions, cron, messaging) => unreadSessionCount(byId, sessions, cron, messaging)
+)