From 9bf080597a00b428617f56e19624061acd575188 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 22 Nov 2026 19:23:21 +0000 Subject: [PATCH] Place items shown mid-turn inside the running turn Live chat events name a message by id and carry no time, so messages the client created from them had no created_at. A running turn then had no timed rows, and an item the agent showed mid-turn (show_file, show_diff) was placed after the last timed row: above the prompt and the whole running reply, usually off screen, until a reload brought the server's times. Messages created from live events are now stamped with their arrival; a history read still replaces them with the server's times. Co-Authored-By: Claude Opus 5.5 --- .../features/chat/domain/chat_transcript.dart | 12 ++- .../chat_live_shown_placement_test.dart | 73 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 app/test/features/chat/domain/chat_live_shown_placement_test.dart diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index f9a1a721..83bc75bc 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -172,7 +172,13 @@ List chatMessageList(Object? messages) => [ /// Fold a live chat frame into an immutable thread snapshot. Text and reasoning /// deltas append by part id, tool snapshots merge without dropping prior fields, /// and step frames update turn activity. Frames for another session are ignored. -JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { +/// A live event names a message by id and carries no time, so a message this +/// creates is stamped with when it arrived ([now], epoch ms). Without one, a +/// running turn has no timed rows and an item the agent shows mid-turn +/// (show_file, show_diff) is placed above the whole turn; a later history +/// read replaces the stamp with the server's own time. +JsonMap applyChatEvent(JsonMap thread, JsonMap frame, {int Function()? now}) { + int arrived() => (now ?? _nowMs)(); final session = thread['sessionId']; if (session != null && frame['session_id'] != null && @@ -203,6 +209,7 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { 'id': id, 'session_id': frame['session_id'], 'role': 'user', + 'created_at': arrived(), 'client': frame['client'], 'operation_id': frame['operation_id'], 'parts': [ @@ -229,6 +236,7 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { 'id': messageId, 'session_id': frame['session_id'], 'role': 'assistant', + 'created_at': arrived(), 'kind': ?injected, 'parts': [], }); @@ -341,3 +349,5 @@ String latestReplyModel(List messages) { } return ''; } + +int _nowMs() => DateTime.now().millisecondsSinceEpoch; diff --git a/app/test/features/chat/domain/chat_live_shown_placement_test.dart b/app/test/features/chat/domain/chat_live_shown_placement_test.dart new file mode 100644 index 00000000..4788e9ec --- /dev/null +++ b/app/test/features/chat/domain/chat_live_shown_placement_test.dart @@ -0,0 +1,73 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; + +JsonMap _event(String kind, String messageId, String partId, String text) => { + 'session_id': 's', + 'kind': kind, + 'message_id': messageId, + 'delta': text, + 'part': {'id': partId, 'type': kind}, +}; + +void main() { + test('a message created from a live event is stamped with its arrival', () { + final thread = applyChatEvent( + {'sessionId': 's', 'messages': []}, + _event('text', 'm1', 'p1', 'hello'), + now: () => 1234, + ); + final messages = thread['messages'] as List; + expect((messages.single as Map)['created_at'], 1234); + }); + + test('an item shown mid-turn lands in the running turn, not above it', () { + // An earlier, finished turn from history (server times). + var thread = { + 'sessionId': 's', + 'messages': [ + { + 'id': 'old', + 'role': 'assistant', + 'created_at': 1000, + 'parts': [ + {'id': 'op', 'type': 'text', 'text': 'Earlier answer'}, + ], + }, + ], + }; + // The running turn arrives only as live events: a prompt, a first + // answer message, then (after the tool call) a second one. + thread = applyChatEvent(thread, { + 'session_id': 's', + 'kind': 'user', + 'message_id': 'u1', + 'part': {'text': 'show me the diff'}, + }, now: () => 2000); + thread = applyChatEvent( + thread, + _event('text', 'a1', 'a1p', 'Let me show it.'), + now: () => 2100, + ); + thread = applyChatEvent( + thread, + _event('text', 'a2', 'a2p', 'Here is the summary.'), + now: () => 2300, + ); + final rows = transcriptRows([ + for (final m in thread['messages'] as List) Map.from(m), + ]); + final placed = withChatShownItems(rows, [ + {'id': 'd', 'kind': 'diff', 'created_at': 2200}, + ], hasMore: false); + final order = [ + for (final row in placed) + row['kind'] == chatShownRowKind ? 'card' : '${row['kind']}', + ]; + // The two live answer messages form one turn row. Without arrival + // stamps the card went after the last timed row, the earlier answer: + // above the prompt and the whole running turn. + expect(order, ['assistant', 'user', 'assistant', 'card']); + }); +}