Show a reopened chat's last transcript while it refreshes

ChatScreen keeps a bounded in-memory cache of recently left transcripts
(16 chats / 4000 messages, LRU). A reopened chat is seeded from it with no
loading state, still re-subscribes and re-reads, and the newest page replaces
the cached copy while live frames received during the read are kept. /close
removes the entry; only transcripts backed by a successful read on the
current connection are written.

Adds tool/chat_reopen_probe.mjs and an optional history delay in the loopback
fixture. Measurement shows a second cost the cache does not remove: opening at
the end of the top-anchored transcript lays out every row (notes).
This commit is contained in:
2026-10-24 18:14:27 +01:00
parent ef575898f2
commit 0a19ef229f
12 changed files with 887 additions and 8 deletions
@@ -32,6 +32,29 @@ JsonMap mergeChatHistory(JsonMap previous, JsonMap page, {bool older = false}) {
};
}
/// Replaces a transcript that opened from this visit's cached copy with the
/// newest authoritative page.
///
/// [held] is what the view shows: the cached messages plus anything a live
/// frame or a local send added or changed since the conversation reopened.
/// [untouchedCached] names the cached messages nothing has changed since; they
/// are not evidence of the current state, so the page alone decides whether
/// they still exist. The page's messages lead in its order with its copies
/// winning, then live arrivals the page does not hold yet follow in their own
/// order, so a frame received during the read is never dropped.
JsonMap refreshCachedChatHistory(
List<JsonMap> held,
JsonMap page,
Set<Object> untouchedCached,
) {
final live = [
for (final message in held)
if (message['id'] == null || !untouchedCached.contains(message['id']))
message,
];
return mergeChatHistory({'messages': live}, page, older: true);
}
/// Consecutive assistant messages form one answer turn. All part fields (also
/// future/unknown protocol types) are preserved in original order.
///