The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stop renderer OOM from session.info heartbeat churn on $sessionStates
webtecnica571b75792c26
diff --git a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts b/apps/desktop/src/app/session/hooks/use-session-state-cache.tsindex 1b55e9c6915..952c90be799 100644--- a/apps/desktop/src/app/session/hooks/use-session-state-cache.ts+++ b/apps/desktop/src/app/session/hooks/use-session-state-cache.ts@@ -7,8 +7,10 @@ import { createClientSessionState } from '@/lib/chat-runtime'import { persistInFlightTurnState } from '@/lib/inflight-turn-journal'import { setMutableRef } from '@/lib/mutable-ref'import {+ $activeSessionId,$busy,$messages,+ setActiveSessionStoredIdRotation,setCurrentFastMode,setCurrentModel,setCurrentPersonality,@@ -108,10 +110,21 @@ export function useSessionStateCache({// rotates (e.g. auto-compression forks a continuation). Leaving the// stale key lets getRuntimeIdForStoredSession resolve the old stored id// to this runtime, which the compression route-follow logic relies on- // being absent. The rotation signal itself is emitted centrally from- // handleTransition (session-states.ts) off the published diff.+ // being absent. The rotation signal was previously emitted centrally+ // from handleTransition (session-states.ts), but updateSessionState+ // now skips publishSessionState (and thus handleTransition) when the+ // updater is a no-op — fire it here so the route-follow effect still+ // tracks compression without needing a dummy state write.if (existing.storedSessionId && existing.storedSessionId !== storedSessionId) {runtimeIdByStoredSessionIdRef.current.delete(existing.storedSessionId)++ if (sessionId === $activeSessionId.get()) {+ setActiveSessionStoredIdRotation({+ nextStoredSessionId: storedSessionId,+ previousStoredSessionId: existing.storedSessionId,+ runtimeSessionId: sessionId+ })+ }}if (storedSessionId) {@@ -268,7 +281,22 @@ export function useSessionStateCache({storedSessionId?: string | null) => {const previous = ensureSessionState(sessionId, storedSessionId)- const next = updater({ ...previous, messages: previous.messages })+ // Give the updater the raw previous state so it can return the same+ // reference when nothing changed (the caller sees a no-op). Previously+ // the param was always a fresh spread, so every call looked like a+ // change — including periodic ~1/s session.info heartbeats that churn+ // $sessionStates and its computed atoms on every tick.+ const next = updater(previous)++ // If the updater returned the same reference, nothing changed for this+ // session — skip the store write, publishSessionState, and view sync.+ // The cache entry was already updated by ensureSessionState (if+ // storedSessionId rotated); the caller gets its return value from the+ // cache, so stale reads don't regress.+ if (next === previous) {+ return previous+ }+sessionStateByRuntimeIdRef.current.set(sessionId, next)// Crash-survivable turn progress: journal the running turn's visible// tail (throttled localStorage write; cleared the moment the turndiff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.tsindex 949d56e140f..53ad8ee89a7 100644--- a/apps/desktop/src/store/session-states.ts+++ b/apps/desktop/src/store/session-states.ts@@ -184,10 +184,25 @@ function handleTransition(previous: ClientSessionState | null, next: ClientSessi/** Publish one session's state. Automatically fires transition side-effects* (watchdog arm/disarm, settle grace, unread marker, compression id rotation)* by diffing previous vs next — callers never need to manually call a- * transition handler. */+ * transition handler.+ *+ * Skips the publish when the new state is identical to the existing one+ * (same reference) to avoid churning `$sessionStates` on periodic+ * `session.info` heartbeats that carry no change — otherwise every ~1/s+ * heartbeat creates a new Record spread, triggering computed atoms+ * ($workingSessionIds, $attentionSessionIds) and their subscribers+ * unnecessarily. The runtime-id→state cache (sessionStateByRuntimeIdRef)+ * is updated independently by the caller, so the visual path stays live+ * without the store churn. */export function publishSessionState(runtimeId: string, state: ClientSessionState) {- const prev = $sessionStates.get()[runtimeId] ?? null- $sessionStates.set({ ...$sessionStates.get(), [runtimeId]: state })+ const current = $sessionStates.get()+ const prev = current[runtimeId] ?? null++ if (prev === state) {+ return+ }++ $sessionStates.set({ ...current, [runtimeId]: state })handleTransition(prev, state, runtimeId)}