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
@@ -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']);
});
}