Skip to content

The agent that grows with you

fix(desktop): the agent avatar cache is bounded, so a long session cannot pin image bytes (Refs #77311)

finn763f4e085e9567e
diff --git a/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx b/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
index 638f8d3acc2..44592dab994 100644
--- a/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/agent-delivery.tsx
@@ -111,7 +111,7 @@ const NOTICE_CLASS =
'flex max-w-[min(86%,44rem)] flex-col gap-0.5 self-center px-2 py-0.5 text-[0.6875rem] leading-5 text-muted-foreground/60'
const AgentGlyph: FC<{ handle: string }> = ({ handle }) => {
- const [avatar, setAvatar] = useState<null | string>(() => agentAvatarCache.get(handle.toLowerCase()) ?? null)
+ const [avatar, setAvatar] = useState<null | string>(() => agentAvatarCache.get(handle.toLowerCase())?.url ?? null)
useEffect(() => {
let live = true
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
index 678083a8ef1..7f970fc2161 100644
--- a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
@@ -15,6 +15,7 @@ import { useResizeObserver } from '@/hooks/use-resize-observer'
import { useI18n } from '@/i18n'
import { triggerHaptic } from '@/lib/haptics'
import { StopFilled } from '@/lib/icons'
+import { LruCache } from '@/lib/lru-cache'
import { cn } from '@/lib/utils'
import { $gateway } from '@/store/gateway'
import { notifyThreadEditOpen } from '@/store/thread-scroll'
@@ -86,12 +87,15 @@ export const AGENT_MESSAGE_RE =
/^(?:Message from (?:πŸ€–\s*)?([^:\n(]{1,64}?)(?:\s*\(@([a-z0-9][a-z0-9_-]{0,63})(?:@[a-zA-Z0-9][a-zA-Z0-9_-]{0,63})?\))?:\s*|\[Message from agent '([^']{1,64})'\]\s*)([\s\S]*)$/u
// sender handle -> avatar data URL. Module-level so a chat full of notices
-// from one bot resolves once. Hits are cached for the window's lifetime;
-// misses only briefly (30s) β€” an avatar can appear at any moment (bot just
+// from one bot resolves once. Bounded LRU: handles are parsed out of message
+// text (unbounded distinct senders over a long session list) and hits hold
+// base64 avatar data URLs, so an unbounded map pins image bytes for the
+// window's lifetime. Eviction only costs a refetch.
+// Misses expire after 30s β€” an avatar can appear at any moment (bot just
// created, art backfill still running), and a permanent negative cache
// froze the πŸ€– glyph until an app restart.
-export const agentAvatarCache = new Map<string, null | string>()
-const agentAvatarMissAt = new Map<string, number>()
+const AGENT_AVATAR_CACHE_MAX = 128
+export const agentAvatarCache = new LruCache<string, { at: number; url: null | string }>(AGENT_AVATAR_CACHE_MAX)
const AVATAR_MISS_TTL_MS = 30_000
const agentAvatarInflight = new Map<string, Promise<null | string>>()
@@ -102,19 +106,17 @@ export async function resolveAgentAvatar(handle: string): Promise<null | string>
return null
}
- if (agentAvatarCache.has(key)) {
- const hit = agentAvatarCache.get(key) ?? null
+ const hit = agentAvatarCache.get(key)
- if (hit !== null) {
- return hit
+ if (hit) {
+ if (hit.url !== null) {
+ return hit.url
}
// Negative entry: honor it only within the TTL, then re-probe.
- if (Date.now() - (agentAvatarMissAt.get(key) ?? 0) < AVATAR_MISS_TTL_MS) {
+ if (Date.now() - hit.at < AVATAR_MISS_TTL_MS) {
return null
}
-
- agentAvatarCache.delete(key)
}
const inflight = agentAvatarInflight.get(key)
@@ -164,11 +166,7 @@ export async function resolveAgentAvatar(handle: string): Promise<null | string>
agentAvatarInflight.set(key, run)
const out = await run
- agentAvatarCache.set(key, out)
-
- if (out === null) {
- agentAvatarMissAt.set(key, Date.now())
- }
+ agentAvatarCache.set(key, { at: Date.now(), url: out })
return out
}
@@ -178,7 +176,7 @@ const AgentMessageNote: FC<{ text: string }> = ({ text }) => {
const sender = (match?.[1] || match?.[3] || 'agent').trim()
const handle = (match?.[2] || match?.[3] || sender).trim()
const body = (match?.[4] || '').trim()
- const [avatar, setAvatar] = useState<null | string>(() => agentAvatarCache.get(handle.toLowerCase()) ?? null)
+ const [avatar, setAvatar] = useState<null | string>(() => agentAvatarCache.get(handle.toLowerCase())?.url ?? null)
useEffect(() => {
let live = true