Problem
Realtime-buffer cleanup on session switch is held together by a timer and a dead effect:
use-realtime-chat-history.ts:445-448 — the primary cleanup effect's first statement is return // disabled; the entire body below (with a TODO about "message appears then disappears") is unreachable dead code.
:510-530 — the only remaining cleanup is setTimeout(..., 5000) in an effect-cleanup function guarded by activeSessionKeyRef.current === effectiveSessionKey. Rapid switching (A→B→A within 5s) can cancel/misfire the timer: session A's realtime buffer can merge into the wrong view or never clear → unbounded realtimeMessages Map growth over a long session.
:429-433 — mergedMessages useMemo intentionally omits the realtime set from deps (eslint-disable) and keys on lastEventAt as a proxy. Buffer clears that don't bump lastEventAt leave just-removed realtime messages visible until the next event. Works today by coincidence; an implicit invariant a future change will break.
Fix
Deterministic clearing keyed on committed session change: when effectiveSessionKey changes, synchronously clear the previous key's buffer (after capturing any recovery message). Delete the disabled effect. Add the subscribed realtimeMessages value to the merge memo deps (it is already subscribed at :388) instead of the lastEventAt proxy.
Found in chat-area audit 2026-06-11.
Problem
Realtime-buffer cleanup on session switch is held together by a timer and a dead effect:
use-realtime-chat-history.ts:445-448— the primary cleanup effect's first statement isreturn // disabled; the entire body below (with a TODO about "message appears then disappears") is unreachable dead code.:510-530— the only remaining cleanup issetTimeout(..., 5000)in an effect-cleanup function guarded byactiveSessionKeyRef.current === effectiveSessionKey. Rapid switching (A→B→A within 5s) can cancel/misfire the timer: session A's realtime buffer can merge into the wrong view or never clear → unboundedrealtimeMessagesMap growth over a long session.:429-433—mergedMessagesuseMemo intentionally omits the realtime set from deps (eslint-disable) and keys onlastEventAtas a proxy. Buffer clears that don't bumplastEventAtleave just-removed realtime messages visible until the next event. Works today by coincidence; an implicit invariant a future change will break.Fix
Deterministic clearing keyed on committed session change: when
effectiveSessionKeychanges, synchronously clear the previous key's buffer (after capturing any recovery message). Delete the disabled effect. Add the subscribedrealtimeMessagesvalue to the merge memo deps (it is already subscribed at:388) instead of thelastEventAtproxy.Found in chat-area audit 2026-06-11.