From 0a19ef229f211ff32b8df3e68a086382f5855580 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 24 Oct 2026 18:14:27 +0100 Subject: [PATCH] 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). --- .../conversation/chat_thread_controller.dart | 54 ++++- .../chat/conversation/chat_thread_view.dart | 55 ++++- .../conversation/chat_transcript_cache.dart | 71 ++++++ .../features/chat/domain/chat_transcript.dart | 23 ++ app/lib/features/chat/screen.dart | 14 ++ app/notes/chat-transcript-cache.md | 78 ++++++ .../chat/chat_transcript_reopen_test.dart | 171 +++++++++++++ .../chat_command_session_test.dart | 26 +- .../conversation/chat_thread_scroll_test.dart | 46 ++++ .../chat_transcript_cache_test.dart | 225 ++++++++++++++++++ app/tool/chat_reopen_probe.mjs | 123 ++++++++++ app/tool/runs_probe_server.mjs | 9 +- 12 files changed, 887 insertions(+), 8 deletions(-) create mode 100644 app/lib/features/chat/conversation/chat_transcript_cache.dart create mode 100644 app/notes/chat-transcript-cache.md create mode 100644 app/test/features/chat/chat_transcript_reopen_test.dart create mode 100644 app/test/features/chat/conversation/chat_transcript_cache_test.dart create mode 100644 app/tool/chat_reopen_probe.mjs diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index 828b271b..b5f7bf5c 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -7,6 +7,7 @@ import '../data/chat_repository.dart'; import '../domain/chat_sessions.dart'; import '../domain/chat_transcript.dart'; import 'chat_thread_cards.dart'; +import 'chat_transcript_cache.dart'; /// 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 /// 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 { - 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); } @@ -55,6 +75,28 @@ class ChatThreadController extends ChangeNotifier { bool _resyncing = 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? _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 get messages => _messages; /// Usage-only snapshots refresh Info, not the expensive transcript tree. @@ -167,6 +209,7 @@ class ChatThreadController extends ChangeNotifier { _running = false; _nativeTurnId = ''; _liveAssistantMessageId = null; + _authoritative = false; _notify(); await _attach(force: true); if (!_owned(token)) return; @@ -184,11 +227,17 @@ class ChatThreadController extends ChangeNotifier { .toList(); _cursor = '${page['nextCursor'] ?? ''}'; _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 { // A live frame may have arrived while the page was loading; merge it // over the held transcript instead of replacing it. _applyPage(mergeChatHistory({'messages': _messages}, page)); } + _untouchedCached = null; + _authoritative = true; _loaded = true; _loading = false; _error = null; @@ -255,6 +304,9 @@ class ChatThreadController extends ChangeNotifier { final current = _thread; final next = applyChatEvent(current, body); 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 []) .whereType() .map((message) => Map.from(message)) diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index f965d377..cc53e3e5 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -25,6 +25,7 @@ import 'chat_models.dart'; import 'chat_row_keys.dart'; import 'chat_scroll.dart'; import 'chat_thread_controller.dart'; +import 'chat_transcript_cache.dart'; import 'chat_permission_card_model.dart'; import 'chat_permission_group.dart'; import 'chat_transcript_card.dart'; @@ -53,6 +54,7 @@ class ChatThreadView extends StatefulWidget { this.newOperationId, this.attachmentPicker, this.scrollPositions, + this.transcripts, this.initialNotice, this.onCreatedSession, this.onSessionRenamed, @@ -107,6 +109,12 @@ class ChatThreadView extends StatefulWidget { /// the operator left it. Null keeps the view stateless across visits. 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 State createState() => _ChatThreadViewState(); } @@ -180,8 +188,40 @@ class _ChatThreadViewState extends State { /// counted against it for the jump control's arrival count. String? _newestAtRelease; - ChatThreadController _attach() => - ChatThreadController(widget.gateway, widget.sessionId)..load(); + ChatThreadController _attach() => ChatThreadController( + 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( widget.gateway, @@ -282,7 +322,7 @@ class _ChatThreadViewState extends State { openCreatedSession: onCreated ?? (created, notice) => _showCommandNotice(notice), onRenamed: widget.onSessionRenamed, - onClosed: widget.onSessionClosed, + onClosed: _sessionClosed, newOperationId: widget.newOperationId, ); } @@ -291,6 +331,7 @@ class _ChatThreadViewState extends State { void initState() { super.initState(); _savedOffset = widget.scrollPositions?.read(widget.sessionId); + _adoptCachedTranscript(); _seedCommandNotice(); } @@ -301,14 +342,19 @@ class _ChatThreadViewState extends State { oldWidget.sessionId != widget.sessionId) { // The conversation being left keeps the position the operator chose. _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(); _composer.dispose(); + _closed = false; _controller = _attach(); _composer = _attachComposer(); _rowKeys = ChatRowKeys(); _dismissals = ChatNoticeDismissals(); _reconnecting = false; - _preGapMessages = null; + _adoptCachedTranscript(); _syncGeneration++; _commandNotices.clear(); _seedCommandNotice(); @@ -355,6 +401,7 @@ class _ChatThreadViewState extends State { @override void dispose() { _persistPosition(); + _saveTranscript(widget.transcripts, widget.sessionId); _scroll.dispose(); _composer.dispose(); _controller.dispose(); diff --git a/app/lib/features/chat/conversation/chat_transcript_cache.dart b/app/lib/features/chat/conversation/chat_transcript_cache.dart new file mode 100644 index 00000000..d19ece24 --- /dev/null +++ b/app/lib/features/chat/conversation/chat_transcript_cache.dart @@ -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 messages, + this.cursor = '', + this.hasMore = false, + }) : messages = List.unmodifiable(messages); + + final List 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 _entries = + LinkedHashMap(); + int _messageCount = 0; + + /// The cached sessions, least recently used first. + List 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; + } +} diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 1bf2dca2..206d081d 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -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 held, + JsonMap page, + Set 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. /// diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index 6186982c..2bcc6bc2 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -11,6 +11,7 @@ import '../../core/api_gateway.dart'; import 'conversation/chat_models.dart'; import 'conversation/chat_scroll.dart'; import 'conversation/chat_thread_view.dart'; +import 'conversation/chat_transcript_cache.dart'; import 'data/chat_repository.dart'; import 'ui/sessions/chat_sessions_controller.dart'; import 'ui/sessions/chat_sessions_screen.dart'; @@ -72,6 +73,11 @@ class _ChatScreenState extends State { /// conversation view, so Back and a reopened chat return to the same place. 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 /// `/new` made. Leaving that conversation drops it, so a later reopen is an /// ordinary visit. @@ -171,6 +177,13 @@ class _ChatScreenState extends State { /// On a phone the open chat is a real page above the main screen. 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 void dispose() { _page.dispose(); @@ -189,6 +202,7 @@ class _ChatScreenState extends State { session: row, syncToken: widget.syncToken, scrollPositions: _scrollPositions, + transcripts: _transcripts, initialNotice: start != null && start.id == selected ? start.notice : null, diff --git a/app/notes/chat-transcript-cache.md b/app/notes/chat-transcript-cache.md new file mode 100644 index 00000000..9faaadfe --- /dev/null +++ b/app/notes/chat-transcript-cache.md @@ -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 `. + `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. diff --git a/app/test/features/chat/chat_transcript_reopen_test.dart b/app/test/features/chat/chat_transcript_reopen_test.dart new file mode 100644 index 00000000..9a639575 --- /dev/null +++ b/app/test/features/chat/chat_transcript_reopen_test.dart @@ -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 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 _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 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()); + }); +} diff --git a/app/test/features/chat/conversation/chat_command_session_test.dart b/app/test/features/chat/conversation/chat_command_session_test.dart index 9b5dc590..03a5f1aa 100644 --- a/app/test/features/chat/conversation/chat_command_session_test.dart +++ b/app/test/features/chat/conversation/chat_command_session_test.dart @@ -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/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_transcript_cache.dart'; import 'chat_composer_bar_fixtures.dart'; @@ -50,7 +51,11 @@ void main() { closed = 0; }); - Future pump(WidgetTester tester, {String directory = '/w'}) async { + Future pump( + WidgetTester tester, { + String directory = '/w', + ChatTranscriptCache? transcripts, + }) async { await tester.pumpWidget( materialHost( ChatThreadView( @@ -60,6 +65,7 @@ void main() { session: {'id': 's', 'title': 'Old', 'directory': directory}, onBack: () {}, drafts: MemoryChatDraftStore(), + transcripts: transcripts, newOperationId: () => 'op-1', onCreatedSession: (session, notice) { created = session; @@ -159,4 +165,22 @@ void main() { expect(closed, 1); 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']); + }); } diff --git a/app/test/features/chat/conversation/chat_thread_scroll_test.dart b/app/test/features/chat/conversation/chat_thread_scroll_test.dart index 9c2b40ed..7364c783 100644 --- a/app/test/features/chat/conversation/chat_thread_scroll_test.dart +++ b/app/test/features/chat/conversation/chat_thread_scroll_test.dart @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.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_thread_view.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_transcript_cache.dart'; import 'chat_thread_fixtures.dart'; @@ -211,6 +212,51 @@ void main() { 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', ( tester, ) async { diff --git a/app/test/features/chat/conversation/chat_transcript_cache_test.dart b/app/test/features/chat/conversation/chat_transcript_cache_test.dart new file mode 100644 index 00000000..280e9f98 --- /dev/null +++ b/app/test/features/chat/conversation/chat_transcript_cache_test.dart @@ -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 _ids(List 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), [ + 'u1', + 'a1', + 'u2', + 'live', + ]); + expect( + ((merged['messages'] as List)[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(); + 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); + }); + }); +} diff --git a/app/tool/chat_reopen_probe.mjs b/app/tool/chat_reopen_probe.mjs new file mode 100644 index 00000000..37abe1b1 --- /dev/null +++ b/app/tool/chat_reopen_probe.mjs @@ -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 +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(); +} diff --git a/app/tool/runs_probe_server.mjs b/app/tool/runs_probe_server.mjs index dc13a254..e6caed2f 100644 --- a/app/tool/runs_probe_server.mjs +++ b/app/tool/runs_probe_server.mjs @@ -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 * 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 connections = 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`, ); const connection = new Connection(socket, { - onMessage: (client, request) => { + onMessage: async (client, request) => { if (request.type === 'subscribe') { // The real server may not acknowledge `subscribe`; keep it silent so // 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') { 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({ id: request.id, type: 'chat_history', chat_history: {