Chat: a reply that streams during a history read is shown whole

The thread controller records numbered (seq) deltas that arrive while the
newest page is read and folds back those the page does not include, onto its
live parts or parts it lacks; a delta the page already included is not added
again when it arrives late. The page's order now leads when a reply streamed
during the initial read, so the reply stays below its prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 15:46:54 +00:00
parent 2cdd524ecc
commit aeefe67631
4 changed files with 266 additions and 3 deletions
@@ -0,0 +1,152 @@
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 'chat_thread_fixtures.dart';
// A chat opened (or reconnected) while a reply streams used to show the reply
// with its first part cut off: history did not hold the block still streaming,
// and only the deltas after the read arrived. The newest page now carries the
// block's text so far and the number of the last delta it includes.
JsonMap seqFrame(String text, int seq, {String messageId = 'a'}) => {
...chatTextFrame(text, messageId: messageId),
'seq': seq,
};
JsonMap livePage(List<JsonMap> messages, int liveSeq, List<String> parts) {
final page = chatPage(messages);
final body = page['chat_history'] as JsonMap;
body['live_seq'] = liveSeq;
body['live_parts'] = parts;
return page;
}
void main() {
late FakeChatGateway gateway;
late ChatThreadController controller;
setUp(() {
gateway = FakeChatGateway();
controller = ChatThreadController(gateway, 's');
});
tearDown(() {
controller.dispose();
gateway.close();
});
List<Object?> texts() => [
for (final part in controller.messages.last['parts'] as List)
(part as Map)['text'],
];
test('opening mid-reply shows the reply from its beginning', () async {
final reply = Completer<JsonMap>();
gateway.history = (_) => reply.future;
final loading = controller.load();
await pumpEventQueue();
// Deltas arrive while the page is read: the first is in the page, the
// second came after the page's point.
gateway.emitEvent(seqFrame('part. ', 11));
gateway.emitEvent(seqFrame('And more.', 12));
await pumpEventQueue();
reply.complete(
livePage(
[
chatMessage('u', 'user'),
chatMessage(
'a',
'assistant',
parts: [
{'type': 'reasoning', 'id': 'r', 'text': 'Thinking.'},
{'type': 'text', 'id': 'p', 'text': 'The first part. '},
],
),
],
11,
['p'],
),
);
await loading;
expect(texts(), ['Thinking.', 'The first part. And more.']);
// A delta the page included can still be on its way: it is not repeated.
gateway.emitEvent(seqFrame('part. ', 11));
gateway.emitEvent(seqFrame(' Done.', 13));
await pumpEventQueue();
expect(texts(), ['Thinking.', 'The first part. And more. Done.']);
});
test(
'a resync mid-reply keeps the reply whole, including a new message',
() async {
gateway.history = (_) async => chatPage([chatMessage('u', 'user')]);
await controller.load();
gateway.emitEvent(seqFrame('The first', 1));
await pumpEventQueue();
final reply = Completer<JsonMap>();
gateway.history = (_) => reply.future;
final resync = controller.resync();
await pumpEventQueue();
gateway.emitEvent(seqFrame(' part.', 2));
gateway.emitEvent(seqFrame('Next', 3, messageId: 'b'));
await pumpEventQueue();
reply.complete(
livePage(
[
chatMessage('u', 'user'),
chatMessage(
'a',
'assistant',
parts: [
{'type': 'text', 'id': 'p', 'text': 'The first part.'},
],
),
],
2,
['p'],
),
);
await resync;
final ids = [for (final message in controller.messages) message['id']];
expect(ids, ['u', 'a', 'b']);
expect(
((controller.messages[1]['parts'] as List).single as Map)['text'],
'The first part.',
);
expect(texts(), ['Next']);
},
);
test('a part the page holds complete is not extended again', () async {
final reply = Completer<JsonMap>();
gateway.history = (_) => reply.future;
final loading = controller.load();
await pumpEventQueue();
gateway.emitEvent(seqFrame(' tail', 8));
await pumpEventQueue();
// The block completed and was written between the page's live point and
// the file read, so the page holds it whole rather than as live text.
reply.complete(
livePage(
[
chatMessage(
'a',
'assistant',
parts: [
{'type': 'text', 'id': 'p', 'text': 'Whole text tail'},
],
),
],
7,
const [],
),
);
await loading;
expect(texts(), ['Whole text tail']);
});
}