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 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 19:23:21 +00:00
parent 2f6c3b7f7e
commit 9bf080597a
2 changed files with 84 additions and 1 deletions
@@ -172,7 +172,13 @@ List<JsonMap> 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': <JsonMap>[],
});
@@ -341,3 +349,5 @@ String latestReplyModel(List<JsonMap> messages) {
}
return '';
}
int _nowMs() => DateTime.now().millisecondsSinceEpoch;
@@ -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': <JsonMap>[]},
_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 = <String, dynamic>{
'sessionId': 's',
'messages': <JsonMap>[
{
'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<String, dynamic>.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']);
});
}