From 8e980564cb3b1a05ecd8a5682ba6d6fce8446a95 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 24 Oct 2026 02:04:44 +0100 Subject: [PATCH] fix(chat): keep streamed reasoning live across queued follow-ups --- .../conversation/chat_thread_controller.dart | 8 ++++ .../chat/conversation/chat_thread_view.dart | 1 + .../features/chat/domain/chat_transcript.dart | 17 +++++--- .../chat_composer_bar_state_test.dart | 41 +++++++++++++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index fe5a84ba..d83d54c5 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -43,6 +43,7 @@ class ChatThreadController extends ChangeNotifier { bool _hasMore = false; String _cursor = ''; String _nativeTurnId = ''; + String? _liveAssistantMessageId; JsonMap? _usage; String? _error; @@ -55,6 +56,7 @@ class ChatThreadController extends ChangeNotifier { List get messages => _messages; bool get running => _running; + String? get liveAssistantMessageId => _liveAssistantMessageId; bool get loaded => _loaded; bool get loading => _loading; bool get hasMore => _hasMore; @@ -160,6 +162,7 @@ class ChatThreadController extends ChangeNotifier { // keep claiming to run until the re-subscribe proves otherwise. _running = false; _nativeTurnId = ''; + _liveAssistantMessageId = null; _notify(); await _attach(force: true); if (!_owned(token)) return; @@ -253,6 +256,11 @@ class ChatThreadController extends ChangeNotifier { .map((message) => Map.from(message)) .toList(); _running = next['running'] == true; + if (!_running) { + _liveAssistantMessageId = null; + } else if (const ['text', 'reasoning', 'tool'].contains(body['kind'])) { + _liveAssistantMessageId = body['message_id'] as String?; + } _nativeTurnId = '${next['nativeTurnId'] ?? ''}'; _notify(); } diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 2fde91b5..c35b670a 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -591,6 +591,7 @@ class _ChatThreadViewState extends State { messages, trailing: cards, running: _controller.running && !_reconnecting && !_staleTranscript, + liveMessageId: _controller.liveAssistantMessageId, ); // A transcript held from before a socket gap may still say a compaction is // running even when the repair's read is refused. That running state is diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 9977237f..1bf2dca2 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -54,6 +54,7 @@ List transcriptRows( List messages, { List trailing = const [], bool running = false, + String? liveMessageId, }) { final rows = []; String? key; @@ -114,14 +115,18 @@ List transcriptRows( } } flush(); - // Only the newest assistant turn can be live. A running step before the - // first assistant delta must not reopen reasoning from an earlier reply. - if (running && messages.isNotEmpty && messages.last['role'] == 'assistant') { + // Prefer the assistant identity that actually streamed: a queued follow-up + // can be later in the transcript while this same reply is still arriving. + // Before a first delta, do not reopen an older reply behind a new user turn. + final streamingId = + liveMessageId ?? + (messages.isNotEmpty && messages.last['role'] == 'assistant' + ? '${messages.last['id'] ?? ''}' + : null); + if (running && streamingId != null) { for (final row in rows.reversed) { if (row['kind'] == 'assistant' && - (row['messageIds'] as List).contains( - '${messages.last['id'] ?? ''}', - )) { + (row['messageIds'] as List).contains(streamingId)) { row['streaming'] = true; break; } diff --git a/app/test/features/chat/conversation/chat_composer_bar_state_test.dart b/app/test/features/chat/conversation/chat_composer_bar_state_test.dart index fb05eff0..0b89327b 100644 --- a/app/test/features/chat/conversation/chat_composer_bar_state_test.dart +++ b/app/test/features/chat/conversation/chat_composer_bar_state_test.dart @@ -112,6 +112,47 @@ void main() { }); for (final cupertino in [false, true]) { + testWidgets('reasoning first arriving after a queued follow-up opens live ' + '($cupertino)', (tester) async { + gateway.prompt = (_, _) async => {'chat_queued': true}; + await tester.pumpWidget( + composerThread( + gateway, + cupertino: cupertino, + newOperationId: nextOperation, + ), + ); + await tester.pumpAndSettle(); + gateway.emitEvent({ + 'session_id': 's', + 'message_id': 'live', + 'kind': 'text', + 'part': {'id': 'live-text-0', 'type': 'text'}, + 'delta': 'Working answer', + }); + await tester.pumpAndSettle(); + await tester.enterText( + find.byKey(const Key('chat-composer-field')), + 'Continue', + ); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-composer-primary'))); + await tester.pumpAndSettle(); + gateway.emitEvent({ + 'session_id': 's', + 'message_id': 'live', + 'kind': 'reasoning', + 'part': {'id': 'live-reasoning-0', 'type': 'reasoning'}, + 'delta': 'Provided reasoning after the follow-up', + }); + await tester.pumpAndSettle(); + expect( + find.text('Provided reasoning after the follow-up'), + findsOneWidget, + ); + expect(gateway.interruptBodies, isEmpty); + }); + testWidgets( 'running send waits only for its acknowledgement and keeps newer draft ' '(${cupertino ? 'cupertino' : 'material'})',