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
+13
View File
@@ -53,6 +53,7 @@ class AppSession extends ChangeNotifier {
bool _subscribed = false, _disposed = false;
bool _everConnected;
int _connectionEpoch = 0;
int _runsResyncs = 0;
RunSubscriptionState _subscription = RunSubscriptionState.pending;
String _subscriptionError = '';
String? _subscribeRequest;
@@ -79,6 +80,11 @@ class AppSession extends ChangeNotifier {
/// detail without remounting the screen or losing its selection.
int get connectionEpoch => _connectionEpoch;
/// Advances on every connected transition and whenever the server detached
/// this connection's run watch for falling behind (`runs_resync`). The Runs
/// screen keys its re-read with this: frames were discarded either way.
int get runsEpoch => _connectionEpoch + _runsResyncs;
/// Connects the shared socket. A missing endpoint is a configuration state,
/// so this is a no-op rather than a guessed endpoint.
Future<void> start() async {
@@ -140,6 +146,13 @@ class AppSession extends ChangeNotifier {
void _onFrame(JsonMap frame) {
if (_disposed) return;
if (frame['type'] == 'runs_resync') {
// The server dropped this watch because the connection fell behind and
// discarded the frames it still held: watch again and re-read the runs.
_runsResyncs++;
resubscribe();
return;
}
final id = frame['id'];
if (id is! String || id != _subscribeRequest) return;
switch (frame['type']) {
+1 -1
View File
@@ -103,7 +103,7 @@ class RunsDestination extends StatelessWidget {
initialStageKey: initialStageKey,
initialVisit: initialVisit,
initialDecide: initialDecide,
syncToken: session.connectionEpoch,
syncToken: session.runsEpoch,
listRefreshToken: listRefreshToken,
notifications: notifications,
attention: attention,
@@ -439,9 +439,10 @@ class ChatThreadController extends ChangeNotifier {
return;
}
// A delta a history page already included is not added again.
if (!_live.admit(body)) return;
final admitted = _live.admit(body);
if (admitted == null) return;
final current = _thread;
final next = applyChatEvent(current, body);
final next = applyChatEvent(current, admitted);
if (identical(next, current)) return;
// A frame for a cached message is fresh state the next read must not drop.
final changed = body['message_id'];
@@ -28,17 +28,21 @@ class ChatLiveFrames {
/// Stops recording for a read that will not be applied.
void abandon(List<JsonMap> frames) => _reads.remove(frames);
/// Whether [frame] should be folded into the held transcript, recording it
/// for every read in flight. A frame with no number is always folded.
bool admit(JsonMap frame) {
/// The part of [frame] to fold into the held transcript, recording it for
/// every read in flight, or null when a page already included all of it. A
/// frame with no number is always folded whole; a joined frame whose first
/// deltas were already included is trimmed to the rest.
JsonMap? admit(JsonMap frame) {
final seq = frame['seq'];
if (seq is! int) return true;
if (seq <= _included) return false;
if (seq is! int) return frame;
if (seq <= _included) return null;
final admitted = chatDeltasAfter(frame, _included);
if (admitted == null) return null;
_included = seq;
for (final read in _reads) {
read.add(frame);
read.add(admitted);
}
return true;
return admitted;
}
/// [messages] after applying [page]: frames recorded during the read that
@@ -65,9 +69,11 @@ class ChatLiveFrames {
};
}
var thread = <String, dynamic>{'messages': messages};
for (final frame in frames) {
final seq = frame['seq'];
for (final recorded in frames) {
final seq = recorded['seq'];
if (seq is! int || seq <= liveSeq) continue;
final frame = chatDeltasAfter(recorded, liveSeq);
if (frame == null) continue;
final messageId = frame['message_id'];
if (!replaced(messageId)) continue;
final part = frame['part'];
@@ -82,3 +88,35 @@ class ChatLiveFrames {
return chatMessageList(thread['messages']);
}
}
/// [frame] without the numbered deltas up to [included].
///
/// A server joins consecutive deltas of one part while a slow client's queue
/// is backed up: the joined frame's `seq_from`..`seq` names the deltas it holds
/// and `delta_lens` each one's length in UTF-16 code units. When a history page
/// already holds a prefix of them, only the rest is new. A plain frame (or one
/// wholly past [included]) is returned unchanged; null means nothing in it is
/// new.
JsonMap? chatDeltasAfter(JsonMap frame, int included) {
final seq = frame['seq'];
if (seq is! int) return frame;
if (seq <= included) return null;
final from = frame['seq_from'];
final lens = frame['delta_lens'];
if (from is! int || from > included || lens is! List) return frame;
final delta = frame['delta'];
if (delta is! String || lens.length != seq - from + 1) return frame;
var cut = 0;
for (var i = 0; i <= included - from; i++) {
final length = lens[i];
if (length is! int || length < 0) return frame;
cut += length;
}
if (cut >= delta.length) return null;
return {
...frame,
'delta': delta.substring(cut),
'seq_from': included + 1,
'delta_lens': lens.sublist(included - from + 1),
};
}