perf(chat): instant channel re-entry — per-channel message cache + persistent WebSocket#273
Conversation
…nt WS First channel entry (and every switch) paid a full cold reload: messages were wiped and re-fetched, a brand-new WebSocket was handshaken per channel, and a subscribe-ack race could re-fetch the whole first page a second time. Discord/Telegram-style fixes, all client-side (the gateway query path is already keyset-paginated and batched): - chatCache.ts: per-channel in-memory LRU cache (messages + members). Re-entering a channel renders the cached window instantly, then a since_seq catch-up merges anything missed. Seeds are trimmed to the newest 100 rows (hasMore flips back on) and streaming flags cleared. - useChatRealtime: the browser socket is now a module-level singleton shared across channel switches — switching sends unsubscribe/subscribe on the live connection instead of paying TCP/WS + auth per switch. Late frames are dropped by channel_id; an idle socket closes after a grace period; logout closes it immediately. - ChannelView: the subscribe ack no longer races the initial history load into a duplicate since_seq=0 page fetch — the catch-up defers until the real seq cursor exists. Async responses landing after a channel switch are discarded instead of merged into the wrong channel. - MessageList: a channel switch jumps straight to the bottom (the old auto-scroll only fired on message-count growth, which a cache seed doesn't trigger). Verified against the kind cluster with headless Chrome: warm re-entry renders in ~140ms with zero full-page refetches and zero new WS connections across A→B→A→B switches; live delivery on the reused socket still works after subscription churn. vitest 83/83. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The iOS client already had the other two web fixes by construction (one app-wide socket with subscribe/unsubscribe per channel; catchUp guarded by loadedOnce/highestSeq), but ChatsHomeView rebuilt ChatView — and with it a fresh ChatModel — on every switch via .id(channelId), so each entry cold-reloaded 50 messages + members. - ChatModelStore (new): LRU (12 channels) of ChatModel instances hung off AppModel, cleared on sign-out. The open channel is always the most recently touched entry, so it can't be evicted. - ChatView now receives the cached model instead of constructing one; the view still remounts per channel, the model survives. - ChatModel: warm loadInitial path renders the in-memory history immediately, scrolls to bottom, marks read, refreshes members in the background, and heals via the existing since_seq catchUp (in-flight guarded — the subscribe ack requests the same catch-up). detach() trims retained history to the newest 100 rows (hasMoreBefore flips back on) so re-entry render cost stays bounded. channel DTO refreshes on reuse so renames still show. xcodebuild (Debug, iOS Simulator) succeeds. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Added the iOS counterpart in 520b1b2. iOS already had two of the three web fixes by construction — a single app-wide socket with per-channel subscribe/unsubscribe, and a Now |
…eviction Two platform-convention upgrades on top of the in-memory ChatModel cache: - MessageStore (new, SwiftData): every visited channel's newest 200 finalized messages persist across launches as full-DTO JSON blobs (schema-stable while the DTO evolves) keyed by unique msg_id, plus a per-channel hasMoreBefore flag so upward pagination works straight off a cold start. Cold loadInitial renders the persisted window first and the network refresh lands on top; if >1 page arrived while offline (fresh page not contiguous with the cached window) only the fresh page is kept so pagination stays gap-free. Writes are debounced (1 s) off upsert/loadOlder and flushed on detach; deleted messages are removed from disk so they can't resurrect; the store is wiped on sign-out. MessageDto becomes Codable (nested types already were). Gotcha found by a macOS SwiftData harness driving the real store: a trim fetch with fetchOffset run BEFORE saving saw every pending insert as overflow and deleted the batch it was meant to keep (SwiftData does not apply offset windows reliably to unsaved objects). save() now persists first, then trims by slicing in code. - ChatModelStore responds to UIApplication.didReceiveMemoryWarning by shedding every cached model except the open channel's — safe to be aggressive now that evicted history reloads from disk, not network. Verified: harness passes round-trip/ordering, unique upsert, 200-row trim + hasMoreBefore, in-flight row skipping, delete, channel isolation, removeAll, and cross-instance relaunch persistence. xcodebuild (Debug, iOS Simulator) succeeds. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
1b90be3 adds the two iOS platform-convention upgrades: Offline-first persistence (SwiftData) — new Memory-pressure eviction — Notable bug caught by a macOS harness driving the real store: SwiftData does not apply |
Conflict in apps/ios/Sources/State/ChatModel.swift: #274 (iOS @-mention picker) built its `mentionPool` inside the members-fetch block of loadInitial, which this branch had extracted into refreshMembers(). Resolved by keeping this branch's loadInitial (offline-first seed + contiguity check) and moving #274's mentionPool construction into refreshMembers(). Strictly better than either side: refreshMembers() also runs on warm re-entry, so a member who joined while the user was away now appears in the mention picker without a cold reload — #274's version only built the pool on a cold load. DTOs.swift, ChatView.swift and ComposerView.swift auto-merged. xcodebuild (Debug, iOS Simulator) succeeds; SwiftData harness and frontend vitest (83/83) still pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Problem
Opening a channel was slow, and every switch paid the full cost again:
ChannelViewwipedmessagesand cold-fetched 50 rows on every entry, even re-entering a channel viewed seconds earlier.channelId, so each switch tore down the socket and paid TCP/WS +auth+subscribe(2 RTT) again.since_seq=0and re-fetched a full page the load was already fetching.The backend needed no changes —
list_channel_messagesis already keyset-paginated with batched hydration (3 queries/page) and the right indexes.Changes (Discord/Telegram-style, frontend only)
chatCache.ts(new) — per-channel in-memory LRU cache (messages + members, 30 channels). Re-entry renders the cached window instantly, then asince_seqcatch-up merges anything missed. Seeds trim to the newest 100 rows (hasMoreflips back on) and clear interrupted streaming flags.useChatRealtime.ts— the browser socket is now a module-level singleton shared across switches: switching sendsunsubscribe/subscribeon the live connection. Late frames for a left channel are dropped bychannel_id; an idle socket closes after 30s; logout closes it immediately. Public API and exports unchanged.ChannelView.tsx— warm path seeds from cache and catch-ups incrementally; the subscribe-ack race is fixed (catch-up defers until the real seq cursor exists); every async response is guarded so a reply landing after a channel switch is discarded instead of merged into the wrong channel's state/cache.MessageList.tsx— a channel switch jumps straight to the bottom of the new timeline.Verification
Headless Chrome against the kind cluster (Vite → in-cluster gateway):
since_seqdelta — 0 full-page refetches.vitest83/83 (incl. newchatCachetests: LRU eviction, seed trimming, streaming-flag clearing);tscclean except the known PdfViewer baseline.🤖 Generated with Claude Code