Chat list: order chat_session_changed pushes by a registry revision

operator_sessions gains a revision bumped by a trigger on every update; the
wire row carries it, and the app ignores a push (or a list read) older than
the row it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:34:16 +00:00
parent 9f13c93be6
commit fbc09528ec
6 changed files with 163 additions and 7 deletions
@@ -143,6 +143,10 @@ class ChatSessionsController extends ChangeNotifier {
/// Another device changed a chat's shared colour, star or title: the row
/// takes the server's values at once, with no re-list. Each field the
/// registry row omits is cleared, since omitted means unset on the wire.
/// The server sends these pushes concurrently, so they can arrive out of
/// order: one whose `revision` is older than the row held here is stale and
/// ignored (a row or push without a revision, from an older server, is
/// always applied).
///
/// A row naming `previous_id` is a chat another device moved to a fresh
/// session (`/new`): it replaces that row in place and [onSessionReplaced]
@@ -165,8 +169,14 @@ class ChatSessionsController extends ChangeNotifier {
if ('${row['id'] ?? ''}' == id)
() {
found = true;
if (chatSessionPushIsStale(row, changed)) return row;
final next = <String, dynamic>{...row};
for (final key in const ['color', 'starred', 'starred_at']) {
for (final key in const [
'color',
'starred',
'starred_at',
'revision',
]) {
if (changed.containsKey(key)) {
next[key] = changed[key];
} else {
@@ -404,8 +414,14 @@ class ChatSessionsController extends ChangeNotifier {
final runningRevision = _runningRevision;
final sessions = await repository.sessions();
if (generation != _generation) return;
// A push applied while this list was read can be newer than its row.
final held = {for (final row in _sessions) '${row['id'] ?? ''}': row};
_sessions = [
for (final row in sessions) _withActivity(row, runningRevision),
for (final row in sessions)
_withActivity(
newerChatSessionMetadata(row, held['${row['id'] ?? ''}']),
runningRevision,
),
];
_seedLocal();
_loading = false;
@@ -644,3 +660,41 @@ class ChatSessionsController extends ChangeNotifier {
super.dispose();
}
}
/// The registry revision a chat list row or push carries; 0 when absent.
int chatSessionRevision(Map<dynamic, dynamic> row) {
final value = row['revision'];
return value is num ? value.toInt() : 0;
}
/// Whether [pushed] is older than [held]: both carry a revision and the
/// push's is lower. A push is never stale against a row with no revision.
bool chatSessionPushIsStale(
Map<dynamic, dynamic> held,
Map<dynamic, dynamic> pushed,
) {
final incoming = chatSessionRevision(pushed);
return incoming > 0 && incoming < chatSessionRevision(held);
}
/// [fetched] with the shared metadata (colour, star, title, revision) of
/// [held] when [held] is newer: a list read that started before a push was
/// applied must not revert it.
JsonMap newerChatSessionMetadata(JsonMap fetched, JsonMap? held) {
if (held == null || !chatSessionPushIsStale(held, fetched)) return fetched;
final next = <String, dynamic>{...fetched};
for (final key in const [
'color',
'starred',
'starred_at',
'title',
'revision',
]) {
if (held.containsKey(key)) {
next[key] = held[key];
} else {
next.remove(key);
}
}
return next;
}