Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Phone: a chat's read mark moves only to the last main-stream message on screen (#2461)

harness-for-autonomous[bot]6a7941103606
diff --git a/mobile/lib/swarm/swarm_conversation.dart b/mobile/lib/swarm/swarm_conversation.dart
index 323aefcba..8352e0f0e 100644
--- a/mobile/lib/swarm/swarm_conversation.dart
+++ b/mobile/lib/swarm/swarm_conversation.dart
@@ -152,18 +152,21 @@ class SwarmConversation extends ChangeNotifier implements SwarmFilesBox {
_markRead();
}
- /// Read through the newest message seen, while it is [shown]: [heldAt]
- /// once it is read, else the last.
+ /// Read through the newest message seen in the main stream, while it is
+ /// [shown]: [heldAt] once it is read, else the last. A reply only in its
+ /// thread is not on the chat's screen, so it never moves the mark (the
+ /// desktop's `_loadedMessageSequence`): a mark cannot move back.
void _markRead() {
if (!_shown) return;
final held = _heldAt;
- final at = held == null ? -1 : messages.lastIndexWhere((m) => m.id == held);
- final seen = held == null
- ? messages.lastOrNull
- : at < 0
- ? null
- : messages[at];
- if (seen != null) reader.readMarks?.markRead(id, seen.sequence);
+ final at = held == null
+ ? messages.length - 1
+ : messages.lastIndexWhere((m) => m.id == held);
+ for (var i = at; i >= 0; i--) {
+ if (!messages[i].inMainStream) continue;
+ reader.readMarks?.markRead(id, messages[i].sequence);
+ return;
+ }
}
/// What the last read or post could not do, as a sentence.
diff --git a/mobile/test/swarm/swarm_conversation_test.dart b/mobile/test/swarm/swarm_conversation_test.dart
index d2c5f1566..2ee5b2ff3 100644
--- a/mobile/test/swarm/swarm_conversation_test.dart
+++ b/mobile/test/swarm/swarm_conversation_test.dart
@@ -42,6 +42,7 @@ void main() {
List<String> agentIds = const ['agent-lead', 'agent-builder'],
String role = 'owner',
bool archived = false,
+ Map<String, int> readMarks = const {},
}) {
final swarm = FakeSwarm();
Map<String, dynamic> g1(int seq) => {
@@ -54,7 +55,7 @@ void main() {
'archived': archived,
};
swarm.routes['/api/super-tabs/$tab/parts'] = (_) => {
- ...firstLoad(groups: [g1(sequence)]),
+ ...firstLoad(groups: [g1(sequence)], derived: {'readMarks': readMarks}),
'members': [part(me, 1, person(me, role: role))],
};
swarm.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');
@@ -1379,6 +1380,110 @@ void main() {
expect(m.sent, isEmpty, reason: 'covered: it marked nothing');
});
+ test('an open chat whose newest message is a reply only in a thread is '
+ 'read through its newest message in the main stream, and a reply '
+ 'only in a thread arriving later marks nothing', () async {
+ final r = rig(sequence: 3, readMarks: const {'g1': 1});
+ final m = marked(r);
+ r.swarm.routes[messages] = (_) => {
+ 'messages': [
+ {...message('m3', 3, text: 'in the thread'), 'thread': 'm1'},
+ message('m2', 2),
+ {...message('m1', 1), 'thread': 'm1'},
+ ],
+ 'next': null,
+ };
+
+ final c = await open(r.reader);
+ await settle();
+ expect(c.messages.last.threadReply, isTrue, reason: 'loaded, last');
+ expect(m.sent, [2], reason: 'the thread reply is not on screen');
+ expect(m.marks.readThrough('g1'), 2);
+
+ r.swarm.routes['$messages?after=3'] = (_) => {
+ 'messages': [
+ {...message('m4', 4, text: 'in the thread'), 'thread': 'm1'},
+ ],
+ 'next': null,
+ };
+ moveOn(r.swarm, 4);
+ await r.reader.refresh();
+ await settle();
+
+ expect(c.messages.last.id, 'm4');
+ expect(m.sent, [2]);
+ expect(m.marks.readThrough('g1'), 2);
+ });
+
+ test('a chat read before, opened again with no network, is read through '
+ 'the newest main-stream message it kept', () async {
+ final r = rig(sequence: 3, readMarks: const {'g1': 1});
+ final m = marked(r);
+ r.swarm.routes[messages] = (_) => {
+ 'messages': [
+ {...message('m3', 3, text: 'in the thread'), 'thread': 'm1'},
+ message('m2', 2),
+ {...message('m1', 1), 'thread': 'm1'},
+ ],
+ 'next': null,
+ };
+ await r.reader.refresh();
+ var c = SwarmConversation(r.reader, 'g1', shown: false);
+ await c.load();
+ c.dispose();
+ await settle();
+ expect(m.sent, isEmpty);
+
+ // Offline: the kept page shows and is read; nothing reads on.
+ r.swarm.routes.remove(messages);
+ r.swarm.routes[read] = (_) => throw StateError('offline');
+ c = SwarmConversation(r.reader, 'g1');
+ addTearDown(c.dispose);
+ await settle();
+
+ expect(c.messages.last.id, 'm3');
+ expect(m.marks.readThrough('g1'), 2);
+ });
+
+ test('a chat opened at a reply only in a thread is read through the '
+ 'main-stream message before it', () async {
+ final r = rig(sequence: 4);
+ final m = marked(r);
+ r.swarm.routes[messages] = (_) => {
+ 'messages': [
+ message('m4', 4),
+ {...message('m3', 3, text: 'in the thread'), 'thread': 'm1'},
+ message('m2', 2),
+ {...message('m1', 1), 'thread': 'm1'},
+ ],
+ 'next': null,
+ };
+ await r.reader.refresh();
+ final c = SwarmConversation(r.reader, 'g1', heldAt: 'm3');
+ addTearDown(c.dispose);
+ await c.load();
+ await settle();
+
+ expect(m.sent, [2]);
+ });
+
+ test('a mark the server holds ahead of what the chat shows is neither '
+ 'sent back nor replaced', () async {
+ final r = rig(sequence: 3, readMarks: const {'g1': 5});
+ final m = marked(r);
+ r.swarm.routes[messages] = (_) => {
+ 'messages': [message('m3', 3), message('m2', 2)],
+ 'next': null,
+ };
+
+ await open(r.reader);
+ await settle();
+
+ expect(r.reader.snapshot!.readMarks['g1'], 5);
+ expect(m.sent, isEmpty);
+ expect(m.marks.readThrough('g1'), 5);
+ });
+
test('a Swarm without read marks marks nothing', () async {
final r = rig();
r.swarm.routes[messages] = (_) => {