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:
@@ -7,6 +7,7 @@ import '../data/chat_repository.dart';
|
|||||||
import '../domain/chat_sessions.dart';
|
import '../domain/chat_sessions.dart';
|
||||||
import '../domain/chat_transcript.dart';
|
import '../domain/chat_transcript.dart';
|
||||||
import 'chat_thread_cards.dart';
|
import 'chat_thread_cards.dart';
|
||||||
|
import 'chat_transcript_cache.dart';
|
||||||
|
|
||||||
/// One open conversation's transcript and live turn state.
|
/// One open conversation's transcript and live turn state.
|
||||||
///
|
///
|
||||||
@@ -20,8 +21,27 @@ import 'chat_thread_cards.dart';
|
|||||||
///
|
///
|
||||||
/// Approval requests and question forms are recorded cards rather than
|
/// Approval requests and question forms are recorded cards rather than
|
||||||
/// messages; [ChatThreadCards] owns their frames, answers and send gates.
|
/// messages; [ChatThreadCards] owns their frames, answers and send gates.
|
||||||
|
///
|
||||||
|
/// A [cached] transcript from an earlier visit is shown at once instead of a
|
||||||
|
/// loading state, but it is only a starting point: [load] still subscribes and
|
||||||
|
/// reads, and the newest page replaces every cached message no live frame has
|
||||||
|
/// changed since (see [refreshCachedChatHistory]).
|
||||||
class ChatThreadController extends ChangeNotifier {
|
class ChatThreadController extends ChangeNotifier {
|
||||||
ChatThreadController(this.gateway, this.sessionId) {
|
ChatThreadController(
|
||||||
|
this.gateway,
|
||||||
|
this.sessionId, {
|
||||||
|
ChatTranscriptSnapshot? cached,
|
||||||
|
}) {
|
||||||
|
if (cached != null) {
|
||||||
|
_messages = cached.messages;
|
||||||
|
_cursor = cached.cursor;
|
||||||
|
_hasMore = cached.hasMore;
|
||||||
|
_loaded = true;
|
||||||
|
_untouchedCached = {
|
||||||
|
for (final message in cached.messages)
|
||||||
|
if (message['id'] != null) message['id'] as Object,
|
||||||
|
};
|
||||||
|
}
|
||||||
_subscription = gateway.frames.listen(_onFrame);
|
_subscription = gateway.frames.listen(_onFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +75,28 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
bool _resyncing = false;
|
bool _resyncing = false;
|
||||||
bool _resyncAgain = false;
|
bool _resyncAgain = false;
|
||||||
|
|
||||||
|
/// Ids of cached messages that no frame or read has confirmed yet; null once
|
||||||
|
/// a read replaced the cached transcript, or when there was none.
|
||||||
|
Set<Object>? _untouchedCached;
|
||||||
|
|
||||||
|
/// True while the held transcript came from a successful read on the current
|
||||||
|
/// connection; a socket gap clears it until the repair's read succeeds.
|
||||||
|
bool _authoritative = false;
|
||||||
|
|
||||||
|
/// Whether this visit opened on a cached transcript that no read has
|
||||||
|
/// replaced yet.
|
||||||
|
bool get showingCached => _untouchedCached != null;
|
||||||
|
|
||||||
|
/// The transcript to keep for a later visit, or null when the held one is
|
||||||
|
/// not backed by a successful read on the current connection.
|
||||||
|
ChatTranscriptSnapshot? get snapshot => _authoritative
|
||||||
|
? ChatTranscriptSnapshot(
|
||||||
|
messages: _messages,
|
||||||
|
cursor: _cursor,
|
||||||
|
hasMore: _hasMore,
|
||||||
|
)
|
||||||
|
: null;
|
||||||
|
|
||||||
List<JsonMap> get messages => _messages;
|
List<JsonMap> get messages => _messages;
|
||||||
|
|
||||||
/// Usage-only snapshots refresh Info, not the expensive transcript tree.
|
/// Usage-only snapshots refresh Info, not the expensive transcript tree.
|
||||||
@@ -167,6 +209,7 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
_running = false;
|
_running = false;
|
||||||
_nativeTurnId = '';
|
_nativeTurnId = '';
|
||||||
_liveAssistantMessageId = null;
|
_liveAssistantMessageId = null;
|
||||||
|
_authoritative = false;
|
||||||
_notify();
|
_notify();
|
||||||
await _attach(force: true);
|
await _attach(force: true);
|
||||||
if (!_owned(token)) return;
|
if (!_owned(token)) return;
|
||||||
@@ -184,11 +227,17 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
.toList();
|
.toList();
|
||||||
_cursor = '${page['nextCursor'] ?? ''}';
|
_cursor = '${page['nextCursor'] ?? ''}';
|
||||||
_hasMore = page['hasMore'] == true;
|
_hasMore = page['hasMore'] == true;
|
||||||
|
} else if (_untouchedCached case final untouched?) {
|
||||||
|
// The cached transcript was a placeholder: the page decides what it
|
||||||
|
// still holds, and live arrivals since reopening are kept after it.
|
||||||
|
_applyPage(refreshCachedChatHistory(_messages, page, untouched));
|
||||||
} else {
|
} else {
|
||||||
// A live frame may have arrived while the page was loading; merge it
|
// A live frame may have arrived while the page was loading; merge it
|
||||||
// over the held transcript instead of replacing it.
|
// over the held transcript instead of replacing it.
|
||||||
_applyPage(mergeChatHistory({'messages': _messages}, page));
|
_applyPage(mergeChatHistory({'messages': _messages}, page));
|
||||||
}
|
}
|
||||||
|
_untouchedCached = null;
|
||||||
|
_authoritative = true;
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
_loading = false;
|
_loading = false;
|
||||||
_error = null;
|
_error = null;
|
||||||
@@ -255,6 +304,9 @@ class ChatThreadController extends ChangeNotifier {
|
|||||||
final current = _thread;
|
final current = _thread;
|
||||||
final next = applyChatEvent(current, body);
|
final next = applyChatEvent(current, body);
|
||||||
if (identical(next, current)) return;
|
if (identical(next, current)) return;
|
||||||
|
// A frame for a cached message is fresh state the next read must not drop.
|
||||||
|
final changed = body['message_id'];
|
||||||
|
if (changed != null) _untouchedCached?.remove(changed);
|
||||||
_messages = (next['messages'] as List? ?? const [])
|
_messages = (next['messages'] as List? ?? const [])
|
||||||
.whereType<Map>()
|
.whereType<Map>()
|
||||||
.map((message) => Map<String, dynamic>.from(message))
|
.map((message) => Map<String, dynamic>.from(message))
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import 'chat_models.dart';
|
|||||||
import 'chat_row_keys.dart';
|
import 'chat_row_keys.dart';
|
||||||
import 'chat_scroll.dart';
|
import 'chat_scroll.dart';
|
||||||
import 'chat_thread_controller.dart';
|
import 'chat_thread_controller.dart';
|
||||||
|
import 'chat_transcript_cache.dart';
|
||||||
import 'chat_permission_card_model.dart';
|
import 'chat_permission_card_model.dart';
|
||||||
import 'chat_permission_group.dart';
|
import 'chat_permission_group.dart';
|
||||||
import 'chat_transcript_card.dart';
|
import 'chat_transcript_card.dart';
|
||||||
@@ -53,6 +54,7 @@ class ChatThreadView extends StatefulWidget {
|
|||||||
this.newOperationId,
|
this.newOperationId,
|
||||||
this.attachmentPicker,
|
this.attachmentPicker,
|
||||||
this.scrollPositions,
|
this.scrollPositions,
|
||||||
|
this.transcripts,
|
||||||
this.initialNotice,
|
this.initialNotice,
|
||||||
this.onCreatedSession,
|
this.onCreatedSession,
|
||||||
this.onSessionRenamed,
|
this.onSessionRenamed,
|
||||||
@@ -107,6 +109,12 @@ class ChatThreadView extends StatefulWidget {
|
|||||||
/// the operator left it. Null keeps the view stateless across visits.
|
/// the operator left it. Null keeps the view stateless across visits.
|
||||||
final ChatScrollStore? scrollPositions;
|
final ChatScrollStore? scrollPositions;
|
||||||
|
|
||||||
|
/// The destination's recently left transcripts. A conversation found here
|
||||||
|
/// opens on that transcript instead of a loading state while its newest page
|
||||||
|
/// is read; leaving writes back the transcript the last successful read
|
||||||
|
/// produced. Null opens every visit from the server alone.
|
||||||
|
final ChatTranscriptCache? transcripts;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ChatThreadView> createState() => _ChatThreadViewState();
|
State<ChatThreadView> createState() => _ChatThreadViewState();
|
||||||
}
|
}
|
||||||
@@ -180,8 +188,40 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
/// counted against it for the jump control's arrival count.
|
/// counted against it for the jump control's arrival count.
|
||||||
String? _newestAtRelease;
|
String? _newestAtRelease;
|
||||||
|
|
||||||
ChatThreadController _attach() =>
|
ChatThreadController _attach() => ChatThreadController(
|
||||||
ChatThreadController(widget.gateway, widget.sessionId)..load();
|
widget.gateway,
|
||||||
|
widget.sessionId,
|
||||||
|
cached: widget.transcripts?.read(widget.sessionId),
|
||||||
|
)..load();
|
||||||
|
|
||||||
|
/// `/close` finished this conversation; its transcript is not kept.
|
||||||
|
bool _closed = false;
|
||||||
|
|
||||||
|
/// A cached transcript is from before this visit's subscription, like one
|
||||||
|
/// held across a socket gap: no running turn or compaction is drawn from it
|
||||||
|
/// until a read or frame replaces it.
|
||||||
|
void _adoptCachedTranscript() {
|
||||||
|
_preGapMessages = _controller.showingCached ? _controller.messages : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Keeps the transcript being left for the next visit. Only one backed by a
|
||||||
|
/// successful read on the current connection is written; otherwise the
|
||||||
|
/// earlier entry stays, and every visit re-reads anyway.
|
||||||
|
void _saveTranscript(ChatTranscriptCache? cache, String sessionId) {
|
||||||
|
if (cache == null) return;
|
||||||
|
if (_closed) {
|
||||||
|
cache.remove(sessionId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final snapshot = _controller.snapshot;
|
||||||
|
if (snapshot != null) cache.write(sessionId, snapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _sessionClosed() {
|
||||||
|
_closed = true;
|
||||||
|
widget.transcripts?.remove(widget.sessionId);
|
||||||
|
widget.onSessionClosed?.call();
|
||||||
|
}
|
||||||
|
|
||||||
ChatComposerController _attachComposer() => ChatComposerController(
|
ChatComposerController _attachComposer() => ChatComposerController(
|
||||||
widget.gateway,
|
widget.gateway,
|
||||||
@@ -282,7 +322,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
openCreatedSession:
|
openCreatedSession:
|
||||||
onCreated ?? (created, notice) => _showCommandNotice(notice),
|
onCreated ?? (created, notice) => _showCommandNotice(notice),
|
||||||
onRenamed: widget.onSessionRenamed,
|
onRenamed: widget.onSessionRenamed,
|
||||||
onClosed: widget.onSessionClosed,
|
onClosed: _sessionClosed,
|
||||||
newOperationId: widget.newOperationId,
|
newOperationId: widget.newOperationId,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -291,6 +331,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
_savedOffset = widget.scrollPositions?.read(widget.sessionId);
|
||||||
|
_adoptCachedTranscript();
|
||||||
_seedCommandNotice();
|
_seedCommandNotice();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,14 +342,19 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
oldWidget.sessionId != widget.sessionId) {
|
oldWidget.sessionId != widget.sessionId) {
|
||||||
// The conversation being left keeps the position the operator chose.
|
// The conversation being left keeps the position the operator chose.
|
||||||
_persistPosition(oldWidget.sessionId);
|
_persistPosition(oldWidget.sessionId);
|
||||||
|
// A transcript from another server is not this one's to keep.
|
||||||
|
if (oldWidget.gateway == widget.gateway) {
|
||||||
|
_saveTranscript(oldWidget.transcripts, oldWidget.sessionId);
|
||||||
|
}
|
||||||
_controller.dispose();
|
_controller.dispose();
|
||||||
_composer.dispose();
|
_composer.dispose();
|
||||||
|
_closed = false;
|
||||||
_controller = _attach();
|
_controller = _attach();
|
||||||
_composer = _attachComposer();
|
_composer = _attachComposer();
|
||||||
_rowKeys = ChatRowKeys();
|
_rowKeys = ChatRowKeys();
|
||||||
_dismissals = ChatNoticeDismissals();
|
_dismissals = ChatNoticeDismissals();
|
||||||
_reconnecting = false;
|
_reconnecting = false;
|
||||||
_preGapMessages = null;
|
_adoptCachedTranscript();
|
||||||
_syncGeneration++;
|
_syncGeneration++;
|
||||||
_commandNotices.clear();
|
_commandNotices.clear();
|
||||||
_seedCommandNotice();
|
_seedCommandNotice();
|
||||||
@@ -355,6 +401,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_persistPosition();
|
_persistPosition();
|
||||||
|
_saveTranscript(widget.transcripts, widget.sessionId);
|
||||||
_scroll.dispose();
|
_scroll.dispose();
|
||||||
_composer.dispose();
|
_composer.dispose();
|
||||||
_controller.dispose();
|
_controller.dispose();
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import '../../../core/api_gateway.dart';
|
||||||
|
|
||||||
|
/// The newest transcript one conversation view held when it was left.
|
||||||
|
///
|
||||||
|
/// It is a starting point for the next visit, never a final state: a reopened
|
||||||
|
/// conversation still subscribes and reads its newest page, which replaces it.
|
||||||
|
class ChatTranscriptSnapshot {
|
||||||
|
ChatTranscriptSnapshot({
|
||||||
|
required List<JsonMap> messages,
|
||||||
|
this.cursor = '',
|
||||||
|
this.hasMore = false,
|
||||||
|
}) : messages = List.unmodifiable(messages);
|
||||||
|
|
||||||
|
final List<JsonMap> messages;
|
||||||
|
|
||||||
|
/// The server's opaque older-page cursor that matched [messages].
|
||||||
|
final String cursor;
|
||||||
|
final bool hasMore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transcripts of recently left conversations, kept in memory by the chat
|
||||||
|
/// destination for the rest of the app session, like its scroll positions.
|
||||||
|
///
|
||||||
|
/// Bounded twice: at most [maxSessions] conversations and [maxMessages] held
|
||||||
|
/// messages in total, evicting the least recently used first. A transcript
|
||||||
|
/// larger than [maxMessages] on its own is not kept. Nothing is written to
|
||||||
|
/// disk, so a restarted app opens every conversation from the server.
|
||||||
|
class ChatTranscriptCache {
|
||||||
|
ChatTranscriptCache({this.maxSessions = 16, this.maxMessages = 4000});
|
||||||
|
|
||||||
|
final int maxSessions;
|
||||||
|
final int maxMessages;
|
||||||
|
|
||||||
|
/// Least recently used first.
|
||||||
|
final LinkedHashMap<String, ChatTranscriptSnapshot> _entries =
|
||||||
|
LinkedHashMap();
|
||||||
|
int _messageCount = 0;
|
||||||
|
|
||||||
|
/// The cached sessions, least recently used first.
|
||||||
|
List<String> get sessionIds => List.unmodifiable(_entries.keys);
|
||||||
|
int get messageCount => _messageCount;
|
||||||
|
|
||||||
|
/// The transcript [sessionId] was left with, marking it recently used.
|
||||||
|
ChatTranscriptSnapshot? read(String sessionId) {
|
||||||
|
final snapshot = _entries.remove(sessionId);
|
||||||
|
if (snapshot != null) _entries[sessionId] = snapshot;
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(String sessionId, ChatTranscriptSnapshot snapshot) {
|
||||||
|
remove(sessionId);
|
||||||
|
if (snapshot.messages.length > maxMessages) return;
|
||||||
|
_entries[sessionId] = snapshot;
|
||||||
|
_messageCount += snapshot.messages.length;
|
||||||
|
while (_entries.length > maxSessions || _messageCount > maxMessages) {
|
||||||
|
remove(_entries.keys.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(String sessionId) {
|
||||||
|
final removed = _entries.remove(sessionId);
|
||||||
|
if (removed != null) _messageCount -= removed.messages.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
_entries.clear();
|
||||||
|
_messageCount = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
|
/// Consecutive assistant messages form one answer turn. All part fields (also
|
||||||
/// future/unknown protocol types) are preserved in original order.
|
/// future/unknown protocol types) are preserved in original order.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import '../../core/api_gateway.dart';
|
|||||||
import 'conversation/chat_models.dart';
|
import 'conversation/chat_models.dart';
|
||||||
import 'conversation/chat_scroll.dart';
|
import 'conversation/chat_scroll.dart';
|
||||||
import 'conversation/chat_thread_view.dart';
|
import 'conversation/chat_thread_view.dart';
|
||||||
|
import 'conversation/chat_transcript_cache.dart';
|
||||||
import 'data/chat_repository.dart';
|
import 'data/chat_repository.dart';
|
||||||
import 'ui/sessions/chat_sessions_controller.dart';
|
import 'ui/sessions/chat_sessions_controller.dart';
|
||||||
import 'ui/sessions/chat_sessions_screen.dart';
|
import 'ui/sessions/chat_sessions_screen.dart';
|
||||||
@@ -72,6 +73,11 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
/// conversation view, so Back and a reopened chat return to the same place.
|
/// conversation view, so Back and a reopened chat return to the same place.
|
||||||
final ChatScrollStore _scrollPositions = MemoryChatScrollStore();
|
final ChatScrollStore _scrollPositions = MemoryChatScrollStore();
|
||||||
|
|
||||||
|
/// Transcripts of recently left chats for this visit, so a reopened chat
|
||||||
|
/// shows its last transcript at once while it re-subscribes and re-reads.
|
||||||
|
/// Bounded and in memory only; see [ChatTranscriptCache].
|
||||||
|
final ChatTranscriptCache _transcripts = ChatTranscriptCache();
|
||||||
|
|
||||||
/// The line a just-created conversation opens on, tied to the selection
|
/// The line a just-created conversation opens on, tied to the selection
|
||||||
/// `/new` made. Leaving that conversation drops it, so a later reopen is an
|
/// `/new` made. Leaving that conversation drops it, so a later reopen is an
|
||||||
/// ordinary visit.
|
/// ordinary visit.
|
||||||
@@ -171,6 +177,13 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
/// On a phone the open chat is a real page above the main screen.
|
/// On a phone the open chat is a real page above the main screen.
|
||||||
final DetailPageHost _page = DetailPageHost();
|
final DetailPageHost _page = DetailPageHost();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(covariant ChatScreen oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
// Transcripts read through another gateway belong to that server.
|
||||||
|
if (oldWidget.gateway != widget.gateway) _transcripts.clear();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_page.dispose();
|
_page.dispose();
|
||||||
@@ -189,6 +202,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
|||||||
session: row,
|
session: row,
|
||||||
syncToken: widget.syncToken,
|
syncToken: widget.syncToken,
|
||||||
scrollPositions: _scrollPositions,
|
scrollPositions: _scrollPositions,
|
||||||
|
transcripts: _transcripts,
|
||||||
initialNotice: start != null && start.id == selected
|
initialNotice: start != null && start.id == selected
|
||||||
? start.notice
|
? start.notice
|
||||||
: null,
|
: null,
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
# Chat transcript cache
|
||||||
|
|
||||||
|
Reported problem: going list -> chat -> Back -> same or another chat reloads the
|
||||||
|
whole conversation for a few seconds each time.
|
||||||
|
|
||||||
|
## What it does
|
||||||
|
|
||||||
|
`ChatScreen` owns a `ChatTranscriptCache` (`lib/features/chat/conversation/chat_transcript_cache.dart`)
|
||||||
|
next to its scroll store. It is in memory only and lasts as long as the
|
||||||
|
destination. It is bounded to 16 conversations and 4,000 messages in total.
|
||||||
|
The least recently used entry is evicted first, and a transcript too large for
|
||||||
|
the whole bound is not kept.
|
||||||
|
|
||||||
|
- Leaving a conversation (phone page pop, wide pane switch, destination teardown)
|
||||||
|
writes the controller's transcript, cursor and `has_more`. It is written only
|
||||||
|
if a read on the current connection succeeded. After a socket gap that has not
|
||||||
|
been repaired, or after a refused read, the earlier entry is kept unchanged.
|
||||||
|
- Reopening seeds `ChatThreadController` with that entry, so there is no
|
||||||
|
loading state and the saved scroll position is applied to it immediately. The
|
||||||
|
controller still runs `chat_subscribe` and then `chat_messages`.
|
||||||
|
- `refreshCachedChatHistory` merges the newest page. The page decides which
|
||||||
|
cached messages still exist and supplies its copies and cursor. Messages that
|
||||||
|
a live frame or local send touched during the read are kept after the page,
|
||||||
|
so no frame is dropped. Until a read or frame replaces the cached transcript,
|
||||||
|
the view treats it like a pre-gap transcript: it shows no running turn or
|
||||||
|
compaction state drawn from it.
|
||||||
|
- `/close` removes the entry and the closed view never writes it back. A
|
||||||
|
gateway change clears the cache. Rename and model switch do not affect the
|
||||||
|
transcript: their row data stays in `_openedSessions` and `_acceptedModels`.
|
||||||
|
A socket replacement needs no invalidation, because every visit re-reads.
|
||||||
|
|
||||||
|
## Measured cause of the delay
|
||||||
|
|
||||||
|
`tool/chat_reopen_probe.mjs` serves the built app against the loopback fixture
|
||||||
|
(100 user/assistant pairs, 390x844, Android UA). `HISTORY_DELAY_MS` holds every
|
||||||
|
`chat_messages` reply to stand in for a slow server read. These are controlled
|
||||||
|
samples, not device timings.
|
||||||
|
|
||||||
|
| 1.5 s read delay | Reopen, before | Reopen, after |
|
||||||
|
| --- | ---: | ---: |
|
||||||
|
| 1x CPU: transcript in semantics (visits 2/3) | 1,661 / 1,641 ms | 550 / 414 ms |
|
||||||
|
| 4x CPU: frame 250 ms after tap | loading spinner | cached transcript |
|
||||||
|
|
||||||
|
Two costs add up:
|
||||||
|
|
||||||
|
1. The history read gates the first paint. Every visit waits for it, and the
|
||||||
|
real server projects the native transcript with tool state. The cache
|
||||||
|
removes this cost from reopen.
|
||||||
|
2. Rendering is itself slow for a long transcript. Opening at the end of the
|
||||||
|
top-anchored lazy `ListView` builds and lays out every row above the end
|
||||||
|
(101 first builds of the markdown view for 100 turns, with only 2 kept).
|
||||||
|
The probe recorded one main-thread long task per open of about 250-390 ms at
|
||||||
|
1x CPU and about 1-3 s at 4x CPU. Markdown parsing is not the cost: parsing
|
||||||
|
100 answers takes about 5 ms. **The cache does not remove this cost.** With
|
||||||
|
the cache it happens when the page is pushed instead of when the reply
|
||||||
|
arrives.
|
||||||
|
|
||||||
|
The real fix for (2) is a bottom-anchored transcript, for example a
|
||||||
|
`CustomScrollView` with a center sliver at the newest row. Opening would then
|
||||||
|
lay out only the visible tail. That rewrites follow, restore, older-page and
|
||||||
|
jump-control geometry, which about 66 chat geometry assertions cover, and it
|
||||||
|
changes how a short chat is aligned. It is left as a separate item rather than
|
||||||
|
mixed into this one.
|
||||||
|
|
||||||
|
## Evidence
|
||||||
|
|
||||||
|
- Probe: `HISTORY_DELAY_MS=1500 PROBE_PLATFORM=android CPU_RATE=4 node tool/chat_reopen_probe.mjs <web build> <out>`.
|
||||||
|
`runs_probe_server.mjs` gained an optional `chatHistoryDelayMs`/`onChatHistory`.
|
||||||
|
Its default replies immediately, as before.
|
||||||
|
- Captures: `build/reopen-before-d1500/visit-3-early.png` (spinner) and
|
||||||
|
`build/reopen-after-d1500/visit-3-early.png` (cached transcript), plus
|
||||||
|
`result.json` in both directories and in `build/reopen-{before,after}-cpu1/`.
|
||||||
|
- Tests: `test/features/chat/conversation/chat_transcript_cache_test.dart`
|
||||||
|
(bounds, merge, seeded controller, live frame during the read, refused read,
|
||||||
|
gap), `test/features/chat/chat_transcript_reopen_test.dart` (phone page and
|
||||||
|
wide pane), a cached restore in `chat_thread_scroll_test.dart`, and `/close`
|
||||||
|
in `chat_command_session_test.dart`.
|
||||||
|
- Not verified on a device. No APK, no deployment.
|
||||||
@@ -0,0 +1,171 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/screen.dart';
|
||||||
|
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||||
|
import 'package:pipelines_app/shell/destination.dart';
|
||||||
|
|
||||||
|
import '../../native_back.dart';
|
||||||
|
import 'conversation/chat_thread_fixtures.dart';
|
||||||
|
|
||||||
|
class _Gateway extends FakeChatGateway {
|
||||||
|
@override
|
||||||
|
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||||
|
if (type == 'chat_sessions') {
|
||||||
|
return Future.value({
|
||||||
|
'chat_sessions': [
|
||||||
|
{'id': 'a', 'title': 'First chat'},
|
||||||
|
{'id': 'b', 'title': 'Second chat'},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (type == 'chat_shells') return Future.value({'chat_shells': []});
|
||||||
|
return super.call(type, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get historyReads =>
|
||||||
|
calls.where((call) => call.type == 'chat_messages').length;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonMap _user(String id, String text) => chatMessage(
|
||||||
|
id,
|
||||||
|
'user',
|
||||||
|
parts: [
|
||||||
|
{'type': 'text', 'text': text},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
/// A few frames, without settling: the phone pushes its page after a frame,
|
||||||
|
/// and a pending read's indicator would never settle.
|
||||||
|
Future<void> _frames(WidgetTester tester) async {
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump();
|
||||||
|
await tester.pump(const Duration(milliseconds: 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
Finder _shows(String text) => find.textContaining(text, findRichText: true);
|
||||||
|
|
||||||
|
Future<_Gateway> _pumpShell(WidgetTester tester, Size size) async {
|
||||||
|
SharedPreferences.setMockInitialValues({});
|
||||||
|
tester.view.physicalSize = size;
|
||||||
|
tester.view.devicePixelRatio = 1;
|
||||||
|
addTearDown(tester.view.reset);
|
||||||
|
final gateway = _Gateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
home: AdaptiveShell(
|
||||||
|
destination: AppDestination.chat,
|
||||||
|
destinationBodies: {
|
||||||
|
AppDestination.chat: (_) => ChatScreen(gateway: gateway),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
return gateway;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
for (final (label, size) in [
|
||||||
|
('phone page', const Size(390, 844)),
|
||||||
|
('wide pane', const Size(1280, 800)),
|
||||||
|
]) {
|
||||||
|
testWidgets('a reopened chat shows its last transcript at once and '
|
||||||
|
'refreshes it from the server ($label)', (tester) async {
|
||||||
|
final gateway = await _pumpShell(tester, size);
|
||||||
|
final phone = size.width < 600;
|
||||||
|
|
||||||
|
Future<void> leave() async {
|
||||||
|
if (phone) {
|
||||||
|
await tester.tap(findNativeBack());
|
||||||
|
} else {
|
||||||
|
gateway.history = (_) async => chatPage(const []);
|
||||||
|
await tester.tap(_shows('Second chat').first);
|
||||||
|
}
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(find.byKey(const ValueKey('chat-thread-a')), findsNothing);
|
||||||
|
gateway.history = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First visit: nothing is cached, so the read is awaited.
|
||||||
|
await tester.tap(_shows('First chat').first);
|
||||||
|
await _frames(tester);
|
||||||
|
expect(find.byKey(const Key('chat-thread-loading')), findsOneWidget);
|
||||||
|
gateway.historyRequests.last.complete(
|
||||||
|
chatPage([_user('u1', 'Earlier question'), _user('gone', 'Removed')]),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_shows('Earlier question'), findsOneWidget);
|
||||||
|
await leave();
|
||||||
|
|
||||||
|
// Reopen: the cached transcript is drawn before the read replies, and
|
||||||
|
// the conversation still re-subscribes and re-reads.
|
||||||
|
final reads = gateway.historyReads;
|
||||||
|
final subscribes = gateway.calls
|
||||||
|
.where((call) => call.type == 'chat_subscribe')
|
||||||
|
.length;
|
||||||
|
await tester.tap(_shows('First chat').first);
|
||||||
|
await _frames(tester);
|
||||||
|
expect(find.byKey(const ValueKey('chat-thread-a')), findsOneWidget);
|
||||||
|
expect(find.byKey(const Key('chat-thread-loading')), findsNothing);
|
||||||
|
expect(_shows('Earlier question'), findsOneWidget);
|
||||||
|
expect(_shows('Removed'), findsOneWidget);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(gateway.historyReads, reads + 1);
|
||||||
|
expect(
|
||||||
|
gateway.calls.where((call) => call.type == 'chat_subscribe').length,
|
||||||
|
subscribes + 1,
|
||||||
|
);
|
||||||
|
|
||||||
|
// A live frame during the read is kept through the reply.
|
||||||
|
gateway.emitEvent(
|
||||||
|
chatTextFrame('Live reply while reopening', session: 'a'),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_shows('Live reply while reopening'), findsOneWidget);
|
||||||
|
|
||||||
|
// The server's reply replaces the cached copy: a message it no longer
|
||||||
|
// holds leaves, a new one arrives, the live frame stays.
|
||||||
|
gateway.historyRequests.last.complete(
|
||||||
|
chatPage([
|
||||||
|
_user('u1', 'Earlier question'),
|
||||||
|
_user('u2', 'Sent from another client'),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_shows('Removed'), findsNothing);
|
||||||
|
expect(_shows('Sent from another client'), findsOneWidget);
|
||||||
|
expect(_shows('Live reply while reopening'), findsOneWidget);
|
||||||
|
|
||||||
|
// The refreshed transcript is what the next visit opens on.
|
||||||
|
await leave();
|
||||||
|
await tester.tap(_shows('First chat').first);
|
||||||
|
await _frames(tester);
|
||||||
|
expect(find.byKey(const Key('chat-thread-loading')), findsNothing);
|
||||||
|
expect(_shows('Sent from another client'), findsOneWidget);
|
||||||
|
expect(_shows('Removed'), findsNothing);
|
||||||
|
|
||||||
|
await tester.pumpWidget(const SizedBox.shrink());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgets('a chat whose read failed is not cached for the next visit', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final gateway = await _pumpShell(tester, const Size(390, 844));
|
||||||
|
gateway.history = (_) async => throw StateError('refused');
|
||||||
|
await tester.tap(_shows('First chat').first);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.tap(findNativeBack());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
|
gateway.history = null;
|
||||||
|
await tester.tap(_shows('First chat').first);
|
||||||
|
await _frames(tester);
|
||||||
|
expect(find.byKey(const Key('chat-thread-loading')), findsOneWidget);
|
||||||
|
|
||||||
|
await tester.pumpWidget(const SizedBox.shrink());
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import 'package:pipelines_app/core/api_gateway.dart';
|
|||||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart';
|
||||||
|
|
||||||
import 'chat_composer_bar_fixtures.dart';
|
import 'chat_composer_bar_fixtures.dart';
|
||||||
|
|
||||||
@@ -50,7 +51,11 @@ void main() {
|
|||||||
closed = 0;
|
closed = 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
Future<void> pump(WidgetTester tester, {String directory = '/w'}) async {
|
Future<void> pump(
|
||||||
|
WidgetTester tester, {
|
||||||
|
String directory = '/w',
|
||||||
|
ChatTranscriptCache? transcripts,
|
||||||
|
}) async {
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
materialHost(
|
materialHost(
|
||||||
ChatThreadView(
|
ChatThreadView(
|
||||||
@@ -60,6 +65,7 @@ void main() {
|
|||||||
session: {'id': 's', 'title': 'Old', 'directory': directory},
|
session: {'id': 's', 'title': 'Old', 'directory': directory},
|
||||||
onBack: () {},
|
onBack: () {},
|
||||||
drafts: MemoryChatDraftStore(),
|
drafts: MemoryChatDraftStore(),
|
||||||
|
transcripts: transcripts,
|
||||||
newOperationId: () => 'op-1',
|
newOperationId: () => 'op-1',
|
||||||
onCreatedSession: (session, notice) {
|
onCreatedSession: (session, notice) {
|
||||||
created = session;
|
created = session;
|
||||||
@@ -159,4 +165,22 @@ void main() {
|
|||||||
expect(closed, 1);
|
expect(closed, 1);
|
||||||
expect(find.text('chat is locked'), findsOneWidget);
|
expect(find.text('chat is locked'), findsOneWidget);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('/close forgets the cached transcript, also after leaving', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final transcripts = ChatTranscriptCache()
|
||||||
|
..write('s', ChatTranscriptSnapshot(messages: const []))
|
||||||
|
..write('other', ChatTranscriptSnapshot(messages: const []));
|
||||||
|
await pump(tester, transcripts: transcripts);
|
||||||
|
await run(tester, '/close');
|
||||||
|
await tester.tap(find.text('Close chat'));
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(closed, 1);
|
||||||
|
expect(transcripts.sessionIds, ['other']);
|
||||||
|
|
||||||
|
// Leaving the closed conversation does not write it back.
|
||||||
|
await tester.pumpWidget(const SizedBox.shrink());
|
||||||
|
expect(transcripts.sessionIds, ['other']);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
|
|||||||
import 'package:pipelines_app/core/api_gateway.dart';
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
|
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
|
||||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart';
|
||||||
|
|
||||||
import 'chat_thread_fixtures.dart';
|
import 'chat_thread_fixtures.dart';
|
||||||
|
|
||||||
@@ -211,6 +212,51 @@ void main() {
|
|||||||
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('a cached reopen restores the position before the read replies', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
final gateway = FakeChatGateway();
|
||||||
|
addTearDown(gateway.close);
|
||||||
|
gateway.history = (_) async => tallPage();
|
||||||
|
final positions = MemoryChatScrollStore();
|
||||||
|
final transcripts = ChatTranscriptCache();
|
||||||
|
Widget thread() => host(
|
||||||
|
ChatThreadView(
|
||||||
|
gateway: gateway,
|
||||||
|
sessionId: 's',
|
||||||
|
scrollPositions: positions,
|
||||||
|
transcripts: transcripts,
|
||||||
|
onBack: () {},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(thread());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
await tester.drag(
|
||||||
|
find.byKey(const Key('chat-thread-s')),
|
||||||
|
const Offset(0, 360),
|
||||||
|
);
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final saved = transcriptScroll(tester).position.pixels;
|
||||||
|
await tester.pumpWidget(host(const SizedBox()));
|
||||||
|
expect(transcripts.sessionIds, ['s']);
|
||||||
|
|
||||||
|
// The reopened read stays pending: the position is restored on the
|
||||||
|
// cached transcript alone.
|
||||||
|
gateway.history = null;
|
||||||
|
await tester.pumpWidget(thread());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
final reopened = transcriptScroll(tester);
|
||||||
|
expect(find.byKey(const Key('chat-thread-loading')), findsNothing);
|
||||||
|
expect(reopened.position.pixels, closeTo(saved, 1));
|
||||||
|
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
|
||||||
|
|
||||||
|
// The same transcript from the server keeps the operator's position.
|
||||||
|
gateway.historyRequests.last.complete(tallPage());
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(reopened.position.pixels, closeTo(saved, 1));
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('a conversation that fits its viewport offers no control', (
|
testWidgets('a conversation that fits its viewport offers no control', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
|
|||||||
@@ -0,0 +1,225 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||||
|
|
||||||
|
import 'chat_thread_fixtures.dart';
|
||||||
|
|
||||||
|
JsonMap _text(String id, String role, String text) => chatMessage(
|
||||||
|
id,
|
||||||
|
role,
|
||||||
|
parts: [
|
||||||
|
{'type': 'text', 'id': 'p-$id', 'text': text},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
ChatTranscriptSnapshot _snapshot(int count, {String prefix = 'm'}) =>
|
||||||
|
ChatTranscriptSnapshot(
|
||||||
|
messages: [
|
||||||
|
for (var i = 0; i < count; i++) chatMessage('$prefix$i', 'user'),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
List<Object?> _ids(List<JsonMap> messages) => [
|
||||||
|
for (final message in messages) message['id'],
|
||||||
|
];
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('ChatTranscriptCache', () {
|
||||||
|
test('keeps the most recently used sessions within the session bound', () {
|
||||||
|
final cache = ChatTranscriptCache(maxSessions: 3, maxMessages: 100);
|
||||||
|
for (final id in ['a', 'b', 'c']) {
|
||||||
|
cache.write(id, _snapshot(1));
|
||||||
|
}
|
||||||
|
// Reading marks a conversation recently used, so it outlives b.
|
||||||
|
expect(cache.read('a'), isNotNull);
|
||||||
|
cache.write('d', _snapshot(1));
|
||||||
|
|
||||||
|
expect(cache.sessionIds, ['c', 'a', 'd']);
|
||||||
|
expect(cache.read('b'), isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('evicts the least recently used until the message bound holds', () {
|
||||||
|
final cache = ChatTranscriptCache(maxSessions: 10, maxMessages: 10);
|
||||||
|
cache.write('a', _snapshot(4));
|
||||||
|
cache.write('b', _snapshot(4));
|
||||||
|
cache.write('c', _snapshot(4));
|
||||||
|
|
||||||
|
expect(cache.sessionIds, ['b', 'c']);
|
||||||
|
expect(cache.messageCount, 8);
|
||||||
|
|
||||||
|
// Rewriting one conversation replaces its count instead of adding to it.
|
||||||
|
cache.write('b', _snapshot(2));
|
||||||
|
expect(cache.messageCount, 6);
|
||||||
|
expect(cache.sessionIds, ['c', 'b']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('never keeps a transcript larger than the whole bound', () {
|
||||||
|
final cache = ChatTranscriptCache(maxSessions: 10, maxMessages: 10);
|
||||||
|
cache.write('a', _snapshot(3));
|
||||||
|
cache.write('a', _snapshot(11));
|
||||||
|
|
||||||
|
expect(cache.read('a'), isNull);
|
||||||
|
expect(cache.messageCount, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('remove and clear forget transcripts and their counts', () {
|
||||||
|
final cache = ChatTranscriptCache();
|
||||||
|
cache.write('a', _snapshot(2));
|
||||||
|
cache.write('b', _snapshot(3));
|
||||||
|
cache.remove('a');
|
||||||
|
expect(cache.sessionIds, ['b']);
|
||||||
|
expect(cache.messageCount, 3);
|
||||||
|
cache.clear();
|
||||||
|
expect(cache.sessionIds, isEmpty);
|
||||||
|
expect(cache.messageCount, 0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('refreshCachedChatHistory lets the page decide and keeps live '
|
||||||
|
'arrivals', () {
|
||||||
|
final held = [
|
||||||
|
_text('gone', 'user', 'deleted on the server'),
|
||||||
|
_text('u1', 'user', 'cached question'),
|
||||||
|
_text('a1', 'assistant', 'cached stale answer'),
|
||||||
|
_text('live', 'assistant', 'arrived after reopening'),
|
||||||
|
];
|
||||||
|
final page = {
|
||||||
|
'messages': [
|
||||||
|
_text('u1', 'user', 'cached question'),
|
||||||
|
_text('a1', 'assistant', 'authoritative answer'),
|
||||||
|
_text('u2', 'user', 'sent from another client'),
|
||||||
|
],
|
||||||
|
'nextCursor': 'next',
|
||||||
|
'hasMore': true,
|
||||||
|
};
|
||||||
|
|
||||||
|
final merged = refreshCachedChatHistory(held, page, {'gone', 'u1', 'a1'});
|
||||||
|
|
||||||
|
expect(_ids(merged['messages'] as List<JsonMap>), [
|
||||||
|
'u1',
|
||||||
|
'a1',
|
||||||
|
'u2',
|
||||||
|
'live',
|
||||||
|
]);
|
||||||
|
expect(
|
||||||
|
((merged['messages'] as List<JsonMap>)[1]['parts'] as List)
|
||||||
|
.single['text'],
|
||||||
|
'authoritative answer',
|
||||||
|
);
|
||||||
|
expect(merged['nextCursor'], 'next');
|
||||||
|
expect(merged['hasMore'], isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('a controller reopened on a cached transcript', () {
|
||||||
|
late FakeChatGateway gateway;
|
||||||
|
late ChatThreadController controller;
|
||||||
|
final cached = ChatTranscriptSnapshot(
|
||||||
|
messages: [
|
||||||
|
_text('u1', 'user', 'question'),
|
||||||
|
_text('a', 'assistant', 'cached answer'),
|
||||||
|
],
|
||||||
|
cursor: 'cached-cursor',
|
||||||
|
hasMore: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
gateway = FakeChatGateway();
|
||||||
|
controller = ChatThreadController(gateway, 's', cached: cached);
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
controller.dispose();
|
||||||
|
gateway.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('shows the cached transcript before the read, then re-subscribes '
|
||||||
|
'and lets the newest page replace it', () async {
|
||||||
|
expect(controller.loaded, isTrue);
|
||||||
|
expect(controller.showingCached, isTrue);
|
||||||
|
expect(_ids(controller.messages), ['u1', 'a']);
|
||||||
|
// A cached transcript is not backed by a read on this connection.
|
||||||
|
expect(controller.snapshot, isNull);
|
||||||
|
|
||||||
|
final load = controller.load();
|
||||||
|
await pumpEventQueue();
|
||||||
|
expect(gateway.calls.map((call) => call.type), [
|
||||||
|
'chat_subscribe',
|
||||||
|
'chat_messages',
|
||||||
|
]);
|
||||||
|
expect(controller.loading, isTrue);
|
||||||
|
expect(_ids(controller.messages), ['u1', 'a']);
|
||||||
|
|
||||||
|
gateway.historyRequests.single.complete(
|
||||||
|
chatPage([
|
||||||
|
_text('a', 'assistant', 'fresh answer'),
|
||||||
|
_text('u2', 'user', 'newer question'),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await load;
|
||||||
|
|
||||||
|
expect(controller.showingCached, isFalse);
|
||||||
|
expect(controller.loading, isFalse);
|
||||||
|
expect(_ids(controller.messages), ['a', 'u2']);
|
||||||
|
expect(
|
||||||
|
(controller.messages.first['parts'] as List).single['text'],
|
||||||
|
'fresh answer',
|
||||||
|
);
|
||||||
|
expect(controller.cursor, '');
|
||||||
|
expect(controller.hasMore, isFalse);
|
||||||
|
expect(_ids(controller.snapshot!.messages), ['a', 'u2']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('keeps a live frame that arrives while the read is pending', () async {
|
||||||
|
final load = controller.load();
|
||||||
|
await pumpEventQueue();
|
||||||
|
gateway.emitEvent(chatTextFrame('streamed reply', messageId: 'live'));
|
||||||
|
await pumpEventQueue();
|
||||||
|
expect(_ids(controller.messages), ['u1', 'a', 'live']);
|
||||||
|
|
||||||
|
gateway.historyRequests.single.complete(
|
||||||
|
chatPage([
|
||||||
|
_text('u1', 'user', 'question'),
|
||||||
|
_text('a', 'assistant', 'cached answer'),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await load;
|
||||||
|
|
||||||
|
expect(_ids(controller.messages), ['u1', 'a', 'live']);
|
||||||
|
expect(
|
||||||
|
(controller.messages.last['parts'] as List).single['text'],
|
||||||
|
'streamed reply',
|
||||||
|
);
|
||||||
|
expect(controller.running, isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a refused read keeps the cached transcript with its error and '
|
||||||
|
'offers nothing to cache', () async {
|
||||||
|
gateway.history = (_) async => throw StateError('offline');
|
||||||
|
await controller.load();
|
||||||
|
|
||||||
|
expect(controller.error, contains('offline'));
|
||||||
|
expect(_ids(controller.messages), ['u1', 'a']);
|
||||||
|
expect(controller.snapshot, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a socket gap withdraws the snapshot until the repair read', () async {
|
||||||
|
gateway.history = (_) async => chatPage([_text('a', 'assistant', 'x')]);
|
||||||
|
await controller.load();
|
||||||
|
expect(controller.snapshot, isNotNull);
|
||||||
|
|
||||||
|
final repaired = Completer<JsonMap>();
|
||||||
|
gateway.history = (_) => repaired.future;
|
||||||
|
final resync = controller.resync();
|
||||||
|
await pumpEventQueue();
|
||||||
|
expect(controller.snapshot, isNull);
|
||||||
|
|
||||||
|
repaired.complete(chatPage([_text('a', 'assistant', 'y')]));
|
||||||
|
await resync;
|
||||||
|
expect(controller.snapshot, isNotNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
// Measures list -> chat -> Back -> same chat against a populated loopback
|
||||||
|
// fixture (never operator data). It separates the history read from rendering:
|
||||||
|
// HISTORY_DELAY_MS holds every `chat_messages` reply, and each visit records
|
||||||
|
// when the reply was sent and when the transcript's text first reached the
|
||||||
|
// semantics tree.
|
||||||
|
// HISTORY_DELAY_MS=1500 CHAT_PAIRS=100 node tool/chat_reopen_probe.mjs <web-build> <out>
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
|
||||||
|
import { once } from 'node:events';
|
||||||
|
import { tmpdir } from 'node:os';
|
||||||
|
import { join, resolve } from 'node:path';
|
||||||
|
import { performance } from 'node:perf_hooks';
|
||||||
|
import { startFixture } from './runs_probe_server.mjs';
|
||||||
|
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
|
||||||
|
import { axNodes, waitFor, capture } from './pipelines_probe_cdp_input.mjs';
|
||||||
|
|
||||||
|
const pairs = Number(process.env.CHAT_PAIRS ?? 100);
|
||||||
|
const width = Number(process.env.PROBE_WIDTH ?? 390);
|
||||||
|
const visits = Number(process.env.VISITS ?? 3);
|
||||||
|
const historyDelayMs = Number(process.env.HISTORY_DELAY_MS ?? 0);
|
||||||
|
const text = '# Recorded answer\n\n' +
|
||||||
|
Array.from({ length: 12 }, (_, i) => `Paragraph ${i}: This is recorded fixture content with **formatting**, an [inert link](https://example.test), and enough text to wrap naturally on a phone.\n`).join('\n') +
|
||||||
|
'\n```dart\n' + Array.from({ length: 25 }, (_, i) => `final value${i} = ${i};`).join('\n') + '\n```\n';
|
||||||
|
const messages = Array.from({ length: pairs }, (_, i) => [
|
||||||
|
{ id: `u${i}`, role: 'user', parts: [{ type: 'text', text: `Recorded question ${i}` }] },
|
||||||
|
{ id: `a${i}`, role: 'assistant', parts: [{ id: `p${i}`, type: 'text', text }] },
|
||||||
|
]).flat();
|
||||||
|
const output = resolve(process.argv[3] ?? 'build/chat-reopen');
|
||||||
|
await mkdir(output, { recursive: true });
|
||||||
|
const historyEvents = [];
|
||||||
|
const fixture = await startFixture({
|
||||||
|
root: resolve(process.argv[2]), chatMessages: messages, chatHistoryDelayMs: historyDelayMs,
|
||||||
|
onChatHistory: (phase) => historyEvents.push({ phase, at: performance.now() }),
|
||||||
|
});
|
||||||
|
const profile = await mkdtemp(join(tmpdir(), 'chat-reopen-'));
|
||||||
|
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank');
|
||||||
|
const log = { pairs, width, historyDelayMs, cpuRate: Number(process.env.CPU_RATE ?? 1), host: process.env.PROBE_PLATFORM ?? 'cupertino', visits: [], checks: [], captures: [] };
|
||||||
|
let c;
|
||||||
|
|
||||||
|
// Transcript text is in the semantics DOM once the view has painted it; the
|
||||||
|
// marker never appears in the list, the header or a loading indicator.
|
||||||
|
const TRANSCRIPT_VISIBLE = `(() => {
|
||||||
|
const host = document.querySelector('flt-semantics-host');
|
||||||
|
if (!host) return false;
|
||||||
|
if (host.textContent.includes('Paragraph')) return true;
|
||||||
|
return [...host.querySelectorAll('[aria-label]')].some(e => e.getAttribute('aria-label').includes('Paragraph'));
|
||||||
|
})()`;
|
||||||
|
|
||||||
|
async function tapNode(node) {
|
||||||
|
const { model } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId });
|
||||||
|
const [x1, y1, , , x3, y3] = model.content;
|
||||||
|
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
|
||||||
|
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl);
|
||||||
|
await c.send('Page.enable');
|
||||||
|
if (log.host === 'android') await c.send('Emulation.setUserAgentOverride', {
|
||||||
|
userAgent: 'Mozilla/5.0 (Linux; Android 16; BrowserFixture) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Mobile Safari/537.36', platform: 'Linux armv8l',
|
||||||
|
});
|
||||||
|
await c.send('Emulation.setDeviceMetricsOverride', { width, height: 844, deviceScaleFactor: 1, mobile: true });
|
||||||
|
await c.send('Emulation.setCPUThrottlingRate', { rate: log.cpuRate });
|
||||||
|
await c.send('Page.navigate', { url: fixture.url + '/chat' });
|
||||||
|
assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000));
|
||||||
|
const rect = await c.evaluate("(() => { const r = document.querySelector('flt-semantics-placeholder').getBoundingClientRect(); return {x:(r.left+r.right)/2,y:(r.top+r.bottom)/2}; })()");
|
||||||
|
await c.send('Input.dispatchMouseEvent', { type: 'mousePressed', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
|
||||||
|
await c.send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: rect.x, y: rect.y, button: 'left', clickCount: 1 });
|
||||||
|
for (let visit = 0; visit < visits; visit++) {
|
||||||
|
const row = await waitFor(async () => (await axNodes(c)).find(n => n.name?.value?.startsWith('Probe conversation') && n.backendDOMNodeId && ['group', 'button'].includes(n.role?.value)), 30000);
|
||||||
|
assert.ok(row, 'chat list row');
|
||||||
|
const firstEvent = historyEvents.length;
|
||||||
|
// The page's own clock times the visit; the in-page frame loop sees the
|
||||||
|
// first frame whose semantics carry transcript text.
|
||||||
|
// Long tasks and frame gaps show whether the main thread is busy
|
||||||
|
// rendering, independently of when the semantics tree catches up.
|
||||||
|
await c.evaluate(`(() => {
|
||||||
|
const open = window.__open = {started: performance.now(), frames: [], longTasks: []};
|
||||||
|
new PerformanceObserver(list => { for (const e of list.getEntries()) if (e.startTime >= open.started) open.longTasks.push({start: e.startTime - open.started, duration: e.duration}); }).observe({entryTypes: ['longtask']});
|
||||||
|
const frame = t => {
|
||||||
|
open.frames.push(t);
|
||||||
|
if (!open.visible && ${TRANSCRIPT_VISIBLE}) open.visible = performance.now();
|
||||||
|
if (t - open.started < 6000) requestAnimationFrame(frame);
|
||||||
|
};
|
||||||
|
requestAnimationFrame(frame);
|
||||||
|
})()`);
|
||||||
|
const tapped = performance.now();
|
||||||
|
await tapNode(row);
|
||||||
|
await sleep(Number(process.env.EARLY_CAPTURE_MS ?? 250));
|
||||||
|
await capture(c, output, log, `visit-${visit + 1}-early`);
|
||||||
|
assert.ok(await waitFor(() => c.evaluate('Boolean(window.__open.visible)'), 45000), 'transcript visible');
|
||||||
|
await sleep(Math.max(0, 6500 - (performance.now() - tapped)));
|
||||||
|
const open = await c.evaluate('window.__open');
|
||||||
|
const reply = historyEvents.slice(firstEvent).find(e => e.phase === 'reply');
|
||||||
|
log.visits.push({
|
||||||
|
visit: visit + 1,
|
||||||
|
transcriptVisibleMs: Math.round(open.visible - open.started),
|
||||||
|
historyReplySentMs: reply ? Math.round(reply.at - tapped) : null,
|
||||||
|
historyRequests: historyEvents.slice(firstEvent).filter(e => e.phase === 'request').length,
|
||||||
|
longTaskTotalMs: Math.round(open.longTasks.reduce((sum, task) => sum + task.duration, 0)),
|
||||||
|
longestTaskMs: Math.round(Math.max(0, ...open.longTasks.map(task => task.duration))),
|
||||||
|
maxFrameGapMs: Math.round(Math.max(0, ...open.frames.slice(1).map((t, i) => t - open.frames[i]))),
|
||||||
|
});
|
||||||
|
await capture(c, output, log, `visit-${visit + 1}-settled`);
|
||||||
|
const back = await waitFor(async () => {
|
||||||
|
const nodes = (await axNodes(c)).filter(n => /^Back(?:\s|$)/.test(n.name?.value ?? '') && n.backendDOMNodeId);
|
||||||
|
return nodes.find(n => n.role?.value === 'button') ?? nodes[0];
|
||||||
|
}, 30000);
|
||||||
|
assert.ok(back, 'native Back control');
|
||||||
|
await tapNode(back);
|
||||||
|
assert.ok(await waitFor(() => c.evaluate("location.pathname === '/chat'"), 30000), 'back to list');
|
||||||
|
await sleep(900);
|
||||||
|
}
|
||||||
|
log.checks.push('every visit painted the transcript');
|
||||||
|
console.log(JSON.stringify(log, null, 2));
|
||||||
|
} catch (error) {
|
||||||
|
if (c) await capture(c, output, log, 'failure');
|
||||||
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await writeFile(join(output, 'result.json'), JSON.stringify(log, null, 2));
|
||||||
|
c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited;
|
||||||
|
await fixture.close();
|
||||||
|
}
|
||||||
@@ -310,7 +310,7 @@ async function staticFile(base, pathname, req, res) {
|
|||||||
* It serves the built probe bundle and upgrades /connect on the same origin, so
|
* It serves the built probe bundle and upgrades /connect on the same origin, so
|
||||||
* the normal web composition infers this server from Uri.base.
|
* the normal web composition infers this server from Uri.base.
|
||||||
*/
|
*/
|
||||||
export async function startFixture({ root, runs = RUNS, runDetail, files = {}, chatMessages }) {
|
export async function startFixture({ root, runs = RUNS, runDetail, files = {}, chatMessages, chatHistoryDelayMs = 0, onChatHistory }) {
|
||||||
const base = await realpath(root);
|
const base = await realpath(root);
|
||||||
const connections = new Set();
|
const connections = new Set();
|
||||||
const sockets = new Set();
|
const sockets = new Set();
|
||||||
@@ -440,7 +440,7 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {}, c
|
|||||||
`Sec-WebSocket-Accept: ${accept(req.headers['sec-websocket-key'])}\r\n\r\n`,
|
`Sec-WebSocket-Accept: ${accept(req.headers['sec-websocket-key'])}\r\n\r\n`,
|
||||||
);
|
);
|
||||||
const connection = new Connection(socket, {
|
const connection = new Connection(socket, {
|
||||||
onMessage: (client, request) => {
|
onMessage: async (client, request) => {
|
||||||
if (request.type === 'subscribe') {
|
if (request.type === 'subscribe') {
|
||||||
// The real server may not acknowledge `subscribe`; keep it silent so
|
// The real server may not acknowledge `subscribe`; keep it silent so
|
||||||
// the app's send-only path is exercised.
|
// the app's send-only path is exercised.
|
||||||
@@ -561,6 +561,11 @@ export async function startFixture({ root, runs = RUNS, runDetail, files = {}, c
|
|||||||
}
|
}
|
||||||
if (request.type === 'chat_messages') {
|
if (request.type === 'chat_messages') {
|
||||||
chatHistory++;
|
chatHistory++;
|
||||||
|
onChatHistory?.('request');
|
||||||
|
// Optional loopback latency for a probe that separates the history
|
||||||
|
// read from rendering; the default replies immediately as before.
|
||||||
|
if (chatHistoryDelayMs > 0) await new Promise((r) => setTimeout(r, chatHistoryDelayMs));
|
||||||
|
onChatHistory?.('reply');
|
||||||
client.send({
|
client.send({
|
||||||
id: request.id, type: 'chat_history',
|
id: request.id, type: 'chat_history',
|
||||||
chat_history: {
|
chat_history: {
|
||||||
|
|||||||
Reference in New Issue
Block a user