App: keep a left chat's cached transcript following its live frames

A left conversation stays subscribed on the server for the rest of the
connection, but the cached transcript was frozen when it was left. A reply
that arrived in the background only appeared after the reopened view's
subscribe and newest-page read (~1-2s over the network on large chats).
The cache now folds the session's chat events into the kept transcript,
so the reply is drawn the moment the chat opens; the page still replaces it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 14:43:52 +00:00
parent 61c7eee2b0
commit cf4579ab15
4 changed files with 211 additions and 11 deletions
@@ -246,6 +246,34 @@ void main() {
expect(reopened.position.pixels, closeTo(saved, 1));
});
testWidgets('a left conversation keeps folding its replies into the cache', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => tallPage();
final transcripts = ChatTranscriptCache();
await tester.pumpWidget(
host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
transcripts: transcripts,
onBack: () {},
),
),
);
await tester.pumpAndSettle();
await tester.pumpWidget(host(const SizedBox()));
// The session stays subscribed, so its reply still arrives.
gateway.emitEvent(chatTextFrame('reply while away', messageId: 'late'));
await tester.pump();
final held = transcripts.read('s')!.messages;
expect(held.last['id'], 'late');
expect((held.last['parts'] as List).single['text'], 'reply while away');
});
testWidgets('a conversation that fits its viewport offers no control', (
tester,
) async {
@@ -79,6 +79,103 @@ void main() {
});
});
group('a transcript left while its session stays subscribed', () {
late FakeChatGateway gateway;
setUp(() => gateway = FakeChatGateway());
tearDown(() => gateway.close());
ChatTranscriptSnapshot left() => ChatTranscriptSnapshot(
messages: [
_text('u1', 'user', 'question'),
_text('a1', 'assistant', 'first answer'),
],
cursor: 'older',
hasMore: true,
);
test('folds the replies that arrive in the background', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
cache.write('other', _snapshot(1));
gateway.emitEvent(chatTextFrame('new ', messageId: 'a2'));
gateway.emitEvent(chatTextFrame('reply', messageId: 'a2'));
// Another session's frame and a non-event envelope change nothing.
gateway.emitEvent(chatTextFrame('elsewhere', session: 'x'));
gateway.emitRaw({'type': 'chat_queue', 'chat_queue': {}});
await pumpEventQueue();
final held = cache.read('s')!;
expect(_ids(held.messages), ['u1', 'a1', 'a2']);
expect((held.messages.last['parts'] as List).single['text'], 'new reply');
expect(held.cursor, 'older');
expect(held.hasMore, isTrue);
expect(cache.messageCount, 4);
});
test('stops following once replaced, removed or cleared', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
cache.write('s', left());
gateway.emitEvent(chatTextFrame('ignored', messageId: 'a2'));
await pumpEventQueue();
expect(_ids(cache.read('s')!.messages), ['u1', 'a1']);
cache.write('s', left(), frames: gateway.frames);
cache.remove('s');
cache.write('t', left(), frames: gateway.frames);
cache.clear();
gateway.emitEvent(chatTextFrame('ignored', messageId: 'a2'));
await pumpEventQueue();
expect(cache.sessionIds, isEmpty);
expect(cache.messageCount, 0);
});
test('a growing transcript keeps the message bound', () async {
final cache = ChatTranscriptCache(maxMessages: 4);
cache.write('old', _snapshot(2, prefix: 'o'));
cache.write('s', left(), frames: gateway.frames);
gateway.emitEvent(chatTextFrame('reply', messageId: 'a2'));
await pumpEventQueue();
expect(cache.sessionIds, ['s']);
expect(cache.messageCount, 3);
});
test('a reopened controller shows the background reply before its read '
'returns, and the page still replaces it', () async {
final cache = ChatTranscriptCache();
cache.write('s', left(), frames: gateway.frames);
gateway.emitEvent(chatTextFrame('background reply', messageId: 'a2'));
await pumpEventQueue();
final controller = ChatThreadController(
gateway,
's',
cached: cache.read('s'),
);
addTearDown(controller.dispose);
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
final load = controller.load();
await pumpEventQueue();
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
gateway.historyRequests.single.complete(
chatPage([
_text('u1', 'user', 'question'),
_text('a1', 'assistant', 'first answer'),
_text('a2', 'assistant', 'recorded reply'),
]),
);
await load;
expect(_ids(controller.messages), ['u1', 'a1', 'a2']);
expect(
(controller.messages.last['parts'] as List).single['text'],
'recorded reply',
);
});
});
test('refreshCachedChatHistory lets the page decide and keeps live '
'arrivals', () {
final held = [