Skip to content

[frontend] P1 bug: session-switch mid-stream cleanup is a disabled effect + 5s timer race; merge memo keyed on lastEventAt proxy #220

Description

@Interstellar-code

Problem

Realtime-buffer cleanup on session switch is held together by a timer and a dead effect:

  1. 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.
  2. :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.
  3. :429-433mergedMessages 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions