Chat and runs: tell a slow client to resync instead of dropping it silently; join queued deltas

A chat subscriber whose queue fills is detached and its writer ends the
stream with a resync frame (the app re-attaches and re-reads history); a run
subscriber gets a final runs_resync and the app subscribes again and
re-reads the Runs list and open detail. Consecutive text/reasoning deltas of
one part are joined in a chat subscriber's queue; joined numbered frames carry
seq_from and delta_lens so the app trims exactly the prefix a history page
already holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:39:38 +00:00
parent e649eced10
commit c316bc9bc8
20 changed files with 603 additions and 80 deletions
@@ -140,6 +140,37 @@ void main() {
expect(session.subscriptionError, contains('too many run subscriptions'));
});
test('runs_resync watches again and advances the runs epoch', () async {
// The server detached this connection's run watch because it fell behind
// and discarded the frames it held; without a new subscribe the Runs
// list and detail froze until the next reconnect.
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final session = AppSession(
endpoint: Uri.parse('ws://example.test/connect'),
client: client,
);
addTearDown(session.dispose);
await session.start();
await flush();
final subscribe =
jsonDecode(channel.outgoing.single as String) as Map<String, dynamic>;
channel.incoming.add(
jsonEncode({'id': subscribe['id'], 'type': 'subscribe'}),
);
await flush();
final epoch = session.runsEpoch;
final connection = session.connectionEpoch;
channel.incoming.add(jsonEncode({'type': 'runs_resync'}));
await flush();
expect(types(channel), ['subscribe', 'subscribe']);
expect(session.runsEpoch, greaterThan(epoch));
expect(session.connectionEpoch, connection);
expect(session.subscription, RunSubscriptionState.pending);
});
test('dispose drops the watch and releases the client', () async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
@@ -0,0 +1,67 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_live_frames.dart';
/// A frame the server joined from deltas 10..12 ("héllo ", "wörld ", "😀!")
/// while this client's queue was backed up.
Map<String, dynamic> joined() => {
'session_id': 's',
'message_id': 'm',
'kind': 'text',
'part': {'type': 'text', 'id': 'm-text-0'},
'delta': 'héllo wörld 😀!',
'seq_from': 10,
'seq': 12,
'delta_lens': [6, 6, 3],
};
void main() {
test('a joined frame drops exactly the deltas a page already holds', () {
expect(chatDeltasAfter(joined(), 9)?['delta'], 'héllo wörld 😀!');
final rest = chatDeltasAfter(joined(), 10)!;
expect(rest['delta'], 'wörld 😀!');
expect(rest['seq_from'], 11);
expect(rest['delta_lens'], [6, 3]);
expect(chatDeltasAfter(joined(), 11)?['delta'], '😀!');
expect(chatDeltasAfter(joined(), 12), isNull);
});
test('a plain numbered delta is folded whole or not at all', () {
final plain = {'kind': 'text', 'delta': 'x', 'seq': 5};
expect(chatDeltasAfter(plain, 4), same(plain));
expect(chatDeltasAfter(plain, 5), isNull);
});
test('admit trims a joined frame overlapping what was included', () {
final live = ChatLiveFrames();
final first = {'kind': 'text', 'delta': 'héllo ', 'seq': 10};
expect(live.admit(first), same(first));
// The rest arrives joined with the delta already shown.
expect(live.admit(joined())?['delta'], 'wörld 😀!');
expect(live.admit(joined()), isNull);
});
test('settle folds back only the part of a joined frame past the page', () {
final live = ChatLiveFrames();
final read = live.beginRead();
live.admit(joined());
final page = {
'liveSeq': 11,
'liveParts': ['m-text-0'],
'messages': [
{
'id': 'm',
'role': 'assistant',
'parts': [
{'type': 'text', 'id': 'm-text-0', 'text': 'héllo wörld '},
],
},
],
};
final messages = live.settle(read, page, [
for (final message in page['messages'] as List)
Map<String, dynamic>.from(message as Map),
], replaced: (_) => true);
final parts = messages.single['parts'] as List;
expect((parts.single as Map)['text'], 'héllo wörld 😀!');
});
}