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: New messages line and Jump to latest in Buddy chats; Jump to latest in threads (#2265)

0xken1543aa9d4cc5c4
diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dart
index 59a5c5488..80cf7d098 100644
--- a/mobile/lib/swarm/swarm_conversation_page.dart
+++ b/mobile/lib/swarm/swarm_conversation_page.dart
@@ -136,7 +136,8 @@ class SwarmConversationPage extends StatefulWidget {
/// above the first message the person had not read as it opened
/// ([swarmFirstUnread]), placed once, the chat opening at it when it is
/// above the screen; and Jump to latest while they read more than a
- /// screen back.
+ /// screen back. The person's own Buddy chat has both too, in the project's
+ /// Buddy tab and on its own page.
final bool homeC;
@override
@@ -204,8 +205,21 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
/// says how many. Null at the foot.
final _held = ValueNotifier<String?>(null);
- /// Swarm Home C: the person reads more than a screen back, where Jump to
- /// latest is offered.
+ /// Whether this chat has the New messages line and Jump to latest: Swarm
+ /// Home C's project chat and the person's own Buddy chat, known as the chat
+ /// opens. Not a work chat.
+ bool _unreadLine = false;
+
+ /// Whether this chat's Swarm has not been read yet, so whose Buddy chat it
+ /// is is not known: [_unreadLine] waits for the first read that works, and
+ /// nothing is read until then, as until the line is placed.
+ bool _ownerUnknown = false;
+
+ /// The chat's first read is done, so its line can be placed.
+ bool _firstRead = false;
+
+ /// The person reads more than a screen back, where Jump to latest is
+ /// offered.
final _farBack = ValueNotifier<bool>(false);
/// Each entry's row as last built, with what it was built from ([_row]).
@@ -288,6 +302,12 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
return;
}
session.inbox.removeListener(_attach);
+ _ownerUnknown =
+ !widget.homeC && widget.buddy == null && reader.snapshot == null;
+ _unreadLine =
+ widget.homeC ||
+ widget.buddy != null ||
+ swarmIsOwnBuddyChat(reader, widget.conversationId);
// Set before what the chat kept is shown, so it is read only as far as
// it is seen: opened at a message, through it until the person goes
// down to the foot; with a New messages line to place, nothing until it
@@ -299,7 +319,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
heldAt:
widget.messageId ??
_held.value ??
- (widget.homeC && !_lineDecided ? _undecided : null),
+ ((_unreadLine || _ownerUnknown) && !_lineDecided ? _undecided : null),
);
c.composer.addListener(_drafted);
if (widget.mention case final agentId?) c.mention(agentId);
@@ -309,8 +329,11 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
_watch();
unawaited(
c.load().then((_) {
+ // A chat left behind may finish reading after its replacement began.
+ if (!mounted || _conversation != c) return;
+ _firstRead = true;
_placeLine(c);
- if (!mounted || _conversation != c || _noticeHandled) return;
+ if (_noticeHandled) return;
if (widget.messageId case final messageId?) {
_noticeHandled = true;
unawaited(_jumpTo(c, messageId));
@@ -330,7 +353,11 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
/// the screen opens the chat at it ([_openAtLine]), unless a notice opened
/// it at a message.
void _placeLine(SwarmConversation c) {
- if (!mounted || _conversation != c || _lineDecided || !widget.homeC) {
+ if (!mounted || _conversation != c || _lineDecided || _ownerUnknown) {
+ return;
+ }
+ if (!_unreadLine) {
+ if (c.heldAt == _undecided) c.heldAt = null;
return;
}
final marks = c.reader.readMarks;
@@ -434,6 +461,11 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
void _see() {
final session = _session, c = _conversation;
if (session == null || c == null) return;
+ if (_ownerUnknown && c.reader.snapshot != null) {
+ _ownerUnknown = false;
+ _unreadLine = swarmIsOwnBuddyChat(c.reader, widget.conversationId);
+ if (_firstRead) _placeLine(c);
+ }
final view = _view.value = _ChatView.of(session, c, _view.value);
_seeBuddy(c, view.buddyProject);
final decisionId = widget.decisionId;
@@ -481,6 +513,9 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
_madeFrom = null;
_lineAt = null;
_lineDecided = false;
+ _ownerUnknown = false;
+ _firstRead = false;
+ _farBack.value = false;
_stopAwaitingMarks();
if (_watching) session?.unwatch(widget.swarmId);
_watching = false;
@@ -554,7 +589,7 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
_nearTop();
if (!_scroll.hasClients) return;
final back = _scroll.offset > _readingBack;
- if (widget.homeC) {
+ if (_unreadLine) {
_farBack.value = _scroll.offset > _scroll.position.viewportDimension;
}
// At the foot all is seen; reading back, only what was seen at the foot
@@ -1026,9 +1061,10 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
child: ListenableBuilder(
listenable: Listenable.merge([c, _held, _farBack]),
builder: (context, _) => switch (_waiting(c)) {
- 0 when !_farBack.value => const SizedBox.shrink(),
+ 0 when !_farBack.value || c.state == SwarmChatState.lost =>
+ const SizedBox.shrink(),
final count => Center(
- child: _NewPill(count: count, onTap: _toFoot),
+ child: SwarmNewPill(count: count, onTap: _toFoot),
),
},
),
@@ -2238,9 +2274,9 @@ class _Day extends StatelessWidget {
/// How many messages arrived below while the person read back — or, with
/// none ([count] 0), Jump to latest while they read far back (Swarm Home C);
-/// a tap goes down to the foot.
-class _NewPill extends StatelessWidget {
- const _NewPill({required this.count, required this.onTap});
+/// a tap goes down to the foot. The chat's and a thread's.
+class SwarmNewPill extends StatelessWidget {
+ const SwarmNewPill({super.key, required this.count, required this.onTap});
static const double _height = 36;
diff --git a/mobile/lib/swarm/swarm_thread_page.dart b/mobile/lib/swarm/swarm_thread_page.dart
index 7b94ae8dd..6adc4308f 100644
--- a/mobile/lib/swarm/swarm_thread_page.dart
+++ b/mobile/lib/swarm/swarm_thread_page.dart
@@ -72,6 +72,10 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
final _focus = FocusNode(debugLabel: 'Swarm thread reply');
final _scroll = ScrollController();
+ /// The latest reply is more than a screen below, where Jump to latest is
+ /// offered.
+ final _farBack = ValueNotifier<bool>(false);
+
/// The message whose actions are open (Copy), by a tap on it.
String? _acting;
@@ -110,6 +114,7 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
_thread.composer.removeListener(_drafted);
_thread.dispose();
_attachNote.dispose();
+ _farBack.dispose();
_text.dispose();
_focus.dispose();
_scroll.dispose();
@@ -150,6 +155,32 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
}
}
+ /// Whether the foot is more than a screen below ([_farBack]), on every
+ /// scroll and whenever the thread's length changes.
+ bool _measured(Notification n) {
+ final ScrollMetrics? m = switch (n) {
+ ScrollMetricsNotification() => n.metrics,
+ ScrollUpdateNotification() => n.metrics,
+ _ => null,
+ };
+ if (m != null) {
+ _farBack.value = m.maxScrollExtent - m.pixels > m.viewportDimension;
+ }
+ return false;
+ }
+
+ /// Back down to the latest reply.
+ void _toFoot() {
+ if (!_scroll.hasClients) return;
+ unawaited(
+ _scroll.animateTo(
+ _scroll.position.maxScrollExtent,
+ duration: const Duration(milliseconds: 250),
+ curve: Curves.easeOut,
+ ),
+ );
+ }
+
/// Sends the reply, at the foot of the thread where it shows. Once the
/// backend took it the keyboard goes down, unless the next is being typed.
Future<void> _send() async {
@@ -179,9 +210,33 @@ class _SwarmThreadPageState extends State<SwarmThreadPage> {
children: [
_header(context, tty),
Expanded(
- child: ListenableBuilder(
- listenable: _thread,
- builder: (context, _) => _body(tty),
+ child: Stack(
+ children: [
+ Positioned.fill(
+ child: NotificationListener<Notification>(
+ onNotification: _measured,
+ child: ListenableBuilder(
+ listenable: _thread,
+ builder: (context, _) => _body(tty),
+ ),
+ ),
+ ),
+ Positioned(
+ left: 0,
+ right: 0,
+ bottom: 8,
+ child: ListenableBuilder(
+ listenable: Listenable.merge([_thread, _farBack]),
+ builder: (context, _) =>
+ _farBack.value &&
+ _thread.state == SwarmChatState.loaded
+ ? Center(
+ child: SwarmNewPill(count: 0, onTap: _toFoot),
+ )
+ : const SizedBox.shrink(),
+ ),
+ ),
+ ],
),
),
ListenableBuilder(
diff --git a/mobile/test/swarm/swarm_buddy_unread_line_test.dart b/mobile/test/swarm/swarm_buddy_unread_line_test.dart
new file mode 100644
index 000000000..6752e5e5d
--- /dev/null
+++ b/mobile/test/swarm/swarm_buddy_unread_line_test.dart
@@ -0,0 +1,507 @@
+import 'dart:async';
+
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/notify/system_notices.dart';
+import 'package:harness_mobile/settings/experimental_features.dart';
+import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;
+import 'package:harness_mobile/swarm/swarm_conversation_page.dart';
+import 'package:harness_mobile/swarm/swarm_project_page.dart';
+import 'package:harness_mobile/swarm/swarm_request.dart';
+import 'package:harness_mobile/swarm/swarm_session.dart';
+
+import '../voice_fakes.dart';
+import 'swarm_fixture.dart';
+
+/// The New messages line and Jump to latest of Swarm Home C's project chat
+/// (#2259) in the person's own Buddy chat too (Tony, 2026-10-30): above the
+/// first message from someone else the person had not read as the chat
+/// opened, placed once, the chat opening at it; and Jump to latest while
+/// they read more than a screen back. Work chats have neither.
+void main() {
+ const chat = 'buddy-me';
+ const messages = '/api/super-tabs/$tab/conversations/$chat/messages';
+ final line = find.byKey(const ValueKey('swarm-new-messages-line'));
+ final jump = find.byKey(const ValueKey('swarm-jump-latest'));
+
+ Map<String, dynamic> said(
+ int sequence, {
+ String author = 'agent-wren',
+ String kind = 'agent',
+ String? text,
+ String conversation = chat,
+ }) => {
+ 'id': 'msg-$sequence',
+ 'conversationId': conversation,
+ 'text': text ?? 'Update $sequence.',
+ 'sequence': sequence,
+ 'author': {'kind': kind, 'id': author, 'name': author},
+ 'attachments': <Object>[],
+ 'reactions': <Object>[],
+ 'createdAt': DateTime.utc(2026, 10, 29, 1, sequence).toIso8601String(),
+ };
+
+ /// The person's Buddy chat holding [page], read through [mark] on some
+ /// device (the server's mark, #2131); [older] ones come before them.
+ FakeSwarm backend(
+ List<Map<String, dynamic>> page, {
+ required int mark,
+ bool older = false,
+ }) {
+ final fake = FakeSwarm();
+ fake.routes['/api/super-tabs/list'] = (_) => {
+ 'tabs': [
+ {'id': tab, 'name': 'Phone Swarm', 'ownerId': me},
+ ],
+ 'next': null,
+ };
+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => {
+ ...firstLoad(
+ groups: [
+ groupData(
+ 'g1',
+ title: 'Fix login',
+ number: 7,
+ lastMessageSequence: 3,
+ ),
+ buddyChatData(
+ chat,
+ agentIds: ['agent-wren'],
+ lastMessageSequence: page.isEmpty
+ ? 0
+ : page.last['sequence'] as int,
+ ),
+ ],
+ agents: [
+ agent('agent-lead', name: 'Lead', displayName: 'River'),
+ buddyData('agent-wren', displayName: 'Wren'),
+ ],
+ derived: {
+ 'readMarks': {chat: mark, 'g1': 3},
+ },
+ ),
+ 'members': [part(me, 1, person(me))],
+ };
+ fake.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');
+ fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {
+ 'decisions': <Object>[],
+ };
+ fake.routes['/api/super-tabs/$tab/mentions?state=waiting'] = (_) => {
+ 'mentions': <Object>[],
+ };
+ fake.routes[messages] = (_) => {
+ 'messages': page.reversed.toList(),
+ 'next': older
+ ? {'sequence': page.first['sequence'], 'id': 'msg-older'}
+ : null,
+ };
+ fake.routes['/api/super-tabs/$tab/conversations/g1/messages'] = (_) => {
+ 'messages': [for (var i = 1; i <= 40; i++) said(i, conversation: 'g1')]
+ .reversed
+ .toList(),
+ 'next': null,
+ };
+ return fake;
+ }
+
+ Future<SwarmHost> pump(
+ WidgetTester tester,
+ FakeSwarm fake, {
+ bool settle = true,
+ ValueNotifier<String?>? signedIn,
+ Widget home = const SwarmConversationPage(
+ swarmId: tab,
+ conversationId: chat,
+ ),
+ }) async {
+ final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
+ ..value = {PhoneExperiment.swarm};
+ final account = signedIn ?? ValueNotifier<String?>(me);
+ final host = SwarmHost(
+ accountId: () => account.value,
+ account: account,
+ request: fake.request,
+ upload: refuseSwarmUpload,
+ notices: SilentSystemNotices(),
+ features: features,
+ readMarks: MemoryKeyValueStore(),
+ )..foreground = false;
+ addTearDown(host.dispose);
+ await tester.pumpWidget(
+ MaterialApp(
+ theme: grid.buildAppTheme(brightness: Brightness.dark),
+ builder: (context, child) => SwarmScope(notifier: host, child: child!),
+ home: home,
+ ),
+ );
+ if (settle) {
+ await tester.pumpAndSettle();
+ } else {
+ for (var i = 0; i < 10; i++) {
+ await tester.pump(const Duration(milliseconds: 50));
+ }
+ }
+ return host;
+ }
+
+ /// [fake]'s Swarm read failing until the returned function is called, which
+ /// reads it again.
+ Future<void> Function() swarmDownAt(
+ WidgetTester tester,
+ FakeSwarm fake,
+ SwarmHost Function() host,
+ ) {
+ final parts = '/api/super-tabs/$tab/parts';
+ final answer = fake.routes[parts]!;
+ var down = true;
+ fake.routes[parts] = (body) {
+ if (down) {
+ throw const SuperTabFailure(
+ 'INTERNAL',
+ 'Swarm temporarily unavailable.',
+ );
+ }
+ return answer(body);
+ };
+ return () async {
+ down = false;
+ await host().session!.inbox.reader(tab)!.refresh();
+ await tester.pumpAndSettle();
+ };
+ }
+
+ /// The read marks sent for [conversation], in order.
+ List<Object?> marksSent(FakeSwarm fake, [String conversation = chat]) => [
+ for (final call in fake.calls)
+ if (call.method == 'POST' &&
+ call.path == '/api/super-tabs/$tab/conversations/$conversation/read')
+ call.body?['sequence'],
+ ];
+
+ bool onScreen(WidgetTester tester, Finder f) {
+ if (f.evaluate().isEmpty) return false;
+ final list = tester.getRect(find.byKey(const ValueKey('swarm-messages')));
+ final row = tester.getRect(f);
+ return row.top >= list.top && row.bottom <= list.bottom;
+ }
+
+ List<Map<String, dynamic>> forty([String conversation = chat]) => [
+ for (var i = 1; i <= 40; i++) said(i, conversation: conversation),
+ ];
+
+ group('the New messages line', () {
+ testWidgets('sits above the first message from someone else the person '
+ 'had not read, and says so in words', (tester) async {
+ await pump(
+ tester,
+ backend([for (var i = 1; i <= 4; i++) said(i)], mark: 2),
+ );
+
+ expect(line, findsOneWidget);
+ expect(find.text('New messages'), findsOneWidget);
+ expect(tester.getSemantics(line).label, 'New messages');
+ final y = tester.getTopLeft(line).dy;
+ expect(tester.getTopLeft(find.text('Update 2.')).dy, lessThan(y));
+ expect(tester.getTopLeft(find.text('Update 3.')).dy, greaterThan(y));
+ });
+
+ testWidgets("passes over the person's own messages", (tester) async {
+ await pump(
+ tester,
+ backend([
+ said(1),
+ said(2),
+ said(3, author: me, kind: 'person', text: 'On it.'),
+ ], mark: 1),
+ );
+
+ expect(line, findsNothing);
+ });
+
+ testWidgets('a message from an agent that has the person\'s id is still '
+ 'from someone else', (tester) async {
+ await pump(
+ tester,
+ backend([said(1), said(2, author: me, kind: 'agent')], mark: 1),
+ );
+
+ expect(line, findsOneWidget);
+ });
+
+ testWidgets('a chat read through its end has none', (tester) async {
+ await pump(tester, backend([said(1), said(2)], mark: 2));
+
+ expect(line, findsNothing);
+ });
+
+ testWidgets('above the screen, the chat opens at it, and what is below '
+ 'is read only once the person goes down to it', (tester) async {
+ final fake = backend(forty(), mark: 5);
+ await pump(tester, fake);
+
+ expect(onScreen(tester, line), isTrue, reason: 'opened at the line');
+ expect(onScreen(tester, find.text('Update 6.')), isTrue);
+ expect(onScreen(tester, find.text('Update 40.')), isFalse);
+ expect(
+ marksSent(fake).where((s) => (s! as int) > 5),
+ isEmpty,
+ reason: 'nothing past the line is read yet',
+ );
+
+ await tester.fling(
+ find.byKey(const ValueKey('swarm-messages')),
+ const Offset(0, -6000),
+ 4000,
+ );
+ await tester.pumpAndSettle();
+
+ expect(onScreen(tester, find.text('Update 40.')), isTrue);
+ expect(marksSent(fake).last, 40);
+ });
+
+ testWidgets('is also on the Buddy tab of a project', (tester) async {
+ await pump(
+ tester,
+ backend([for (var i = 1; i <= 4; i++) said(i)], mark: 2),
+ home: const SwarmProjectPage(swarmId: tab, projectId: project),
+ );
+ await tester.tap(find.byKey(const ValueKey('swarm-project-tab-buddy')));
+ await tester.pumpAndSettle();
+
+ expect(line, findsOneWidget);
+ expect(find.text('Update 4.'), findsOneWidget);
+ });
+
+ testWidgets('a work chat has none, nor Jump to latest', (tester) async {
+ await pump(
+ tester,
+ backend([said(1)], mark: 1),
+ home: const SwarmConversationPage(swarmId: tab, conversationId: 'g1'),
+ );
+ expect(line, findsNothing);
+ await tester.drag(
+ find.byKey(const ValueKey('swarm-messages')),
+ const Offset(0, 1500),
+ );
+ await tester.pumpAndSettle();
+
+ expect(onScreen(tester, find.text('Update 40.')), isFalse);
+ expect(jump, findsNothing);
+ });
+ });
+
+ group('Jump to latest', () {
+ testWidgets('reading more than a screen back it takes the person to the '
+ 'foot; a little way back, none', (tester) async {
+ await pump(tester, backend(forty(), mark: 40));
+ expect(line, findsNothing);
+ expect(jump, findsNothing, reason: 'at the foot');
+
+ final list = find.byKey(const ValueKey('swarm-messages'));
+ await tester.drag(list, const Offset(0, 120));
+ await tester.pumpAndSettle();
+ expect(jump, findsNothing, reason: 'not a screen back');
+
+ await tester.drag(list, const Offset(0, 1500));
+ await tester.pumpAndSettle();
+ expect(onScreen(tester, find.text('Update 40.')), isFalse);
+ expect(jump, findsOneWidget);
+ expect(find.text('Jump to latest'), findsOneWidget);
+ expect(tester.getSemantics(jump).label, 'Jump to latest');
+
+ await tester.tap(jump);
+ await tester.pumpAndSettle();
+ expect(onScreen(tester, find.text('Update 40.')), isTrue);
+ expect(jump, findsNothing);
+ });
+ });
+
+ group('its privacy edges', () {
+ testWidgets("someone else's Buddy chat, which the Swarm does not list "
+ 'for this person, is refused: no line, no name, nothing of it', (
+ tester,
+ ) async {
+ final fake = backend(forty(), mark: 5);
+ fake.routes['/api/super-tabs/$tab/conversations/buddy-dee/messages'] =
+ (_) => {
+ 'messages': [said(1, conversation: 'buddy-dee')],
+ 'next': null,
+ };
+ await pump(
+ tester,
+ fake,
+ home: const SwarmConversationPage(
+ swarmId: tab,
+ conversationId: 'buddy-dee',
+ ),
+ );
+
+ expect(line, findsNothing);
+ expect(jump, findsNothing);
+ expect(find.text('Update 1.'), findsNothing);
+ expect(find.textContaining('person-dee'), findsNothing);
+ });
+
+ testWidgets('access taken away while it is open: the line and the '
+ 'messages go', (tester) async {
+ final fake = backend(forty(), mark: 5);
+ final host = await pump(tester, fake);
+ expect(line, findsOneWidget);
+
+ const refused = SuperTabFailure(
+ 'FORBIDDEN',
+ 'You cannot read this Swarm.',
+ statusCode: 403,
+ );
+ fake.routes['/api/super-tabs/$tab/changes'] = (_) => throw refused;
+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => throw refused;
+ unawaited(host.session!.inbox.reader(tab)!.refresh());
+ await tester.pumpAndSettle();
+
+ expect(line, findsNothing);
+ expect(jump, findsNothing);
+ expect(find.text('Update 6.'), findsNothing);
+ });
+
+ testWidgets('a first read that fails says so with no Loading left and no '
+ "line, and is read again once, at the Swarm's next read", (
+ tester,
+ ) async {
+ final fake = backend([for (var i = 1; i <= 4; i++) said(i)], mark: 2);
+ final answer = fake.routes[messages]!;
+ fake.routes[messages] = (_) =>
+ throw const SuperTabFailure('INTERNAL', 'Messages are down.');
+ final host = await pump(tester, fake);
+ int reads() => fake.paths.where((p) => p.startsWith(messages)).length;
+
+ expect(find.text('Messages are down.'), findsOneWidget);
+ expect(find.byKey(const ValueKey('swarm-chat-skeleton')), findsNothing);
+ expect(line, findsNothing);
+ final opened = reads();
+ await tester.pump(const Duration(minutes: 1));
+ expect(reads(), opened, reason: 'not again on its own, in a loop');
+
+ fake.routes[messages] = answer;
+ await host.session!.inbox.reader(tab)!.refresh();
+ await tester.pumpAndSettle();
+
+ expect(reads(), opened + 1);
+ expect(find.text('Update 4.'), findsOneWidget);
+ expect(find.text('Messages are down.'), findsNothing);
+ });
+ });
+
+ group('the Swarm read fails first, then recovers', () {
+ testWidgets('a Buddy chat then gets Jump to latest', (tester) async {
+ final fake = backend(forty(), mark: 40);
+ late SwarmHost host;
+ final recover = swarmDownAt(tester, fake, () => host);
+ host = await pump(tester, fake, settle: false);
+ expect(host.session!.inbox.reader(tab)!.snapshot, isNull);
+
+ await recover();
+ expect(find.text('Update 40.'), findsOneWidget);
+ await tester.drag(
+ find.byKey(const ValueKey('swarm-messages')),
+ const Offset(0, 1500),
+ );
+ await tester.pumpAndSettle();
+
+ expect(onScreen(tester, find.text('Update 40.')), isFalse);
+ expect(jump, findsOneWidget);
+ });
+
+ testWidgets('and the New messages line, with nothing read before it', (
+ tester,
+ ) async {
+ final fake = backend([for (var i = 1; i <= 4; i++) said(i)], mark: 2);
+ late SwarmHost host;
+ final recover = swarmDownAt(tester, fake, () => host);
+ host = await pump(tester, fake, settle: false);
+ expect(marksSent(fake), isEmpty);
+
+ await recover();
+
+ expect(line, findsOneWidget);
+ final y = tester.getTopLeft(line).dy;
+ expect(tester.getTopLeft(find.text('Update 2.')).dy, lessThan(y));
+ expect(tester.getTopLeft(find.text('Update 3.')).dy, greaterThan(y));
+ });
+
+ testWidgets('a work chat still has neither', (tester) async {
+ final fake = backend([said(1)], mark: 1);
+ late SwarmHost host;
+ final recover = swarmDownAt(tester, fake, () => host);
+ host = await pump(
+ tester,
+ fake,
+ settle: false,
+ home: const SwarmConversationPage(swarmId: tab, conversationId: 'g1'),
+ );
+
+ await recover();
+ await tester.drag(
+ find.byKey(const ValueKey('swarm-messages')),
+ const Offset(0, 1500),
+ );
+ await tester.pumpAndSettle();
+
+ expect(line, findsNothing);
+ expect(onScreen(tester, find.text('Update 40.')), isFalse);
+ expect(jump, findsNothing);
+ expect(marksSent(fake, 'g1'), isNotEmpty, reason: 'read, once known');
+ });
+
+ testWidgets('a chat left behind, still reading, does not mark the new '
+ 'one read: its line is placed from its own read', (tester) async {
+ final fake = backend([for (var i = 1; i <= 4; i++) said(i)], mark: 2);
+ final asked = <Completer<Object?>>[];
+ final page = fake.routes[messages]!;
+ fake.routes[messages] = (body) {
+ asked.add(Completer<Object?>());
+ return asked.last.future;
+ };
+ final parts = '/api/super-tabs/$tab/parts';
+ final answer = fake.routes[parts]!;
+ var down = false;
+ fake.routes[parts] = (body) {
+ if (down) {
+ throw const SuperTabFailure('INTERNAL', 'Swarm temporarily down.');
+ }
+ return answer(body);
+ };
+ final signedIn = ValueNotifier<String?>(me);
+ final host = await pump(tester, fake, settle: false, signedIn: signedIn);
+ expect(asked, hasLength(1), reason: 'the first session is reading');
+
+ down = true;
+ signedIn.value = null;
+ await tester.pump();
+ // A session made while the app is in front reads its Swarms at once.
+ host.foreground = true;
+ signedIn.value = me;
+ for (var i = 0; i < 10; i++) {
+ await tester.pump(const Duration(milliseconds: 50));
+ }
+ host.foreground = false;
+ final reader = host.session!.inbox.reader(tab)!;
+ expect(
+ reader.snapshot,
+ isNull,
+ reason: "the new session's Swarm is down",
+ );
+ expect(asked, hasLength(2), reason: 'and its chat is reading too');
+
+ asked[0].complete(page(null));
+ await tester.pump();
+ down = false;
+ await reader.refresh();
+ await tester.pump();
+ asked[1].complete(page(null));
+ await tester.pumpAndSettle();
+
+ expect(find.text('Update 4.'), findsOneWidget);
+ expect(line, findsOneWidget);
+ });
+ });
+}
diff --git a/mobile/test/swarm/swarm_thread_jump_test.dart b/mobile/test/swarm/swarm_thread_jump_test.dart
new file mode 100644
index 000000000..f6ba51ad6
--- /dev/null
+++ b/mobile/test/swarm/swarm_thread_jump_test.dart
@@ -0,0 +1,215 @@
+import 'package:flutter/material.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/notify/system_notices.dart';
+import 'package:harness_mobile/settings/experimental_features.dart';
+import 'package:harness_mobile/shared/theme/app_theme.dart' as grid;
+import 'package:harness_mobile/swarm/swarm_conversation_page.dart';
+import 'package:harness_mobile/swarm/swarm_request.dart';
+import 'package:harness_mobile/swarm/swarm_session.dart';
+
+import '../voice_fakes.dart';
+import 'swarm_fixture.dart';
+
+/// Jump to latest in a thread (Tony, 2026-10-30), as the chat has it: shown
+/// while the latest reply is more than a screen below. The New messages line
+/// is left out: the server keeps a read mark per chat, none per thread
+/// (Desktop's thread read state is its own, kept on that machine).
+void main() {
+ const messages = '/api/super-tabs/$tab/conversations/g1/messages';
+ const thread = '$messages?thread=m1';
+ final jump = find.byKey(const ValueKey('swarm-jump-latest'));
+ final list = find.byKey(const ValueKey('swarm-thread-messages'));
+
+ /// Message [sequence] of g1: the root is m1; every other is a reply in its
+ /// thread, only there.
+ Map<String, dynamic> said(
+ int sequence, {
+ String author = 'agent-builder',
+ String kind = 'agent',
+ }) => {
+ 'id': 'm$sequence',
+ 'conversationId': 'g1',
+ 'text': sequence == 1 ? 'The question.' : 'Reply $sequence.',
+ 'sequence': sequence,
+ 'author': {'kind': kind, 'id': author, 'name': author},
+ 'attachments': <Object>[],
+ 'reactions': <Object>[],
+ 'thread': 'm1',
+ if (sequence > 1) 'replyTo': 'm1',
+ 'createdAt': DateTime.utc(2026, 10, 29, 1, sequence).toIso8601String(),
+ };
+
+ Map<String, dynamic> lineOf(int replies, int last) => {
+ 'id': 'm1',
+ 'replies': replies,
+ 'lastSequence': last,
+ 'lastAt': DateTime.utc(2026, 10, 29, 2).toIso8601String(),
+ 'authors': [
+ {'kind': 'agent', 'id': 'agent-builder', 'name': 'agent-builder'},
+ ],
+ 'people': <String>[],
+ };
+
+ /// g1 whose thread under m1 holds [replies] (oldest first), the chat read
+ /// through [mark] on some device (the server's mark, #2131). With [older],
+ /// the thread has replies before those read.
+ FakeSwarm backend(
+ List<Map<String, dynamic>> replies, {
+ required int mark,
+ bool older = false,
+ }) {
+ final last = replies.isEmpty ? 1 : replies.last['sequence'] as int;
+ final fake = FakeSwarm();
+ fake.routes['/api/super-tabs/list'] = (_) => {
+ 'tabs': [
+ {'id': tab, 'name': 'Phone Swarm', 'ownerId': me},
+ ],
+ 'next': null,
+ };
+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => {
+ ...firstLoad(
+ groups: [
+ groupData(
+ 'g1',
+ title: 'Fix login',
+ agentIds: ['agent-lead', 'agent-builder'],
+ lastMessageSequence: last,
+ ),
+ ],
+ derived: {
+ 'readMarks': {'g1': mark},
+ },
+ ),
+ 'members': [part(me, 1, person(me))],
+ };
+ fake.routes['/api/super-tabs/$tab/changes'] = (_) => changes(cursor: 'c2');
+ fake.routes['/api/super-tabs/$tab/decisions?state=open'] = (_) => {
+ 'decisions': <Object>[],
+ };
+ fake.routes['/api/super-tabs/$tab/mentions?state=waiting'] = (_) => {
+ 'mentions': <Object>[],
+ };
+ fake.routes[messages] = (_) => {
+ 'messages': [said(1)],
+ 'next': null,
+ 'threads': [lineOf(replies.length, last)],
+ };
+ fake.routes[thread] = (_) => {
+ 'messages': [...replies.reversed, said(1)],
+ 'next': older
+ ? {'sequence': replies.first['sequence'], 'id': 'm-older'}
+ : null,
+ 'threads': [lineOf(replies.length, last)],
+ };
+ return fake;
+ }
+
+ /// The thread under m1 of g1 on screen.
+ Future<SwarmHost> open(WidgetTester tester, FakeSwarm fake) async {
+ final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
+ ..value = {PhoneExperiment.swarm};
+ final account = ValueNotifier<String?>(me);
+ final host = SwarmHost(
+ accountId: () => account.value,
+ account: account,
+ request: fake.request,
+ upload: refuseSwarmUpload,
+ notices: SilentSystemNotices(),
+ features: features,
+ readMarks: MemoryKeyValueStore(),
+ )..foreground = false;
+ addTearDown(host.dispose);
+ await tester.pumpWidget(
+ MaterialApp(
+ theme: grid.buildAppTheme(brightness: Brightness.dark),
+ builder: (context, child) => SwarmScope(notifier: host, child: child!),
+ home: const SwarmConversationPage(swarmId: tab, conversationId: 'g1'),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.tap(find.byKey(const ValueKey('swarm-thread-line-m1')));
+ await tester.pumpAndSettle();
+ return host;
+ }
+
+ List<Map<String, dynamic>> replies(int last) => [
+ for (var i = 2; i <= last; i++) said(i),
+ ];
+
+ bool onScreen(WidgetTester tester, Finder f) {
+ if (f.evaluate().isEmpty) return false;
+ final view = tester.getRect(list);
+ final row = tester.getRect(f);
+ return row.top >= view.top && row.bottom <= view.bottom;
+ }
+
+ group('Jump to latest', () {
+ testWidgets('while the latest is more than a screen below it takes the '
+ 'person to the foot; near the foot, none', (tester) async {
+ await open(tester, backend(replies(40), mark: 40));
+ expect(onScreen(tester, find.text('Reply 40.')), isFalse);
+ expect(jump, findsOneWidget, reason: 'opened at the top of a long one');
+ expect(find.text('Jump to latest'), findsOneWidget);
+ expect(tester.getSemantics(jump).label, 'Jump to latest');
+
+ await tester.tap(jump);
+ await tester.pumpAndSettle();
+ expect(onScreen(tester, find.text('Reply 40.')), isTrue);
+ expect(jump, findsNothing);
+
+ await tester.drag(list, const Offset(0, 120));
+ await tester.pumpAndSettle();
+ expect(jump, findsNothing, reason: 'not a screen from the foot');
+
+ await tester.drag(list, const Offset(0, 1500));
+ await tester.pumpAndSettle();
+ expect(jump, findsOneWidget);
+ });
+
+ testWidgets('a thread that fits one screen has none', (tester) async {
+ await open(tester, backend(replies(4), mark: 4));
+
+ expect(jump, findsNothing);
+ });
+ });
+
+ group('its privacy edges', () {
+ testWidgets('access taken away while it is open: the replies and Jump '
+ 'to latest go', (tester) async {
+ final fake = backend(replies(40), mark: 5);
+ final host = await open(tester, fake);
+ expect(jump, findsOneWidget);
+
+ host.session!.inbox.reader(tab)!.revoke();
+ await tester.pumpAndSettle();
+
+ expect(jump, findsNothing);
+ expect(find.text('Reply 6.'), findsNothing);
+ expect(find.text('The question.'), findsNothing);
+ });
+
+ testWidgets('a read that fails says so with Try again, no Loading left; '
+ 'once read, the thread shows', (tester) async {
+ final fake = backend(replies(5), mark: 2);
+ final answer = fake.routes[thread]!;
+ fake.routes[thread] = (_) =>
+ throw const SuperTabFailure('INTERNAL', 'Thread is down.');
+ await open(tester, fake);
+ int reads() => fake.paths.where((p) => p == thread).length;
+
+ expect(find.text('Thread is down.'), findsOneWidget);
+ expect(jump, findsNothing);
+ final opened = reads();
+ await tester.pump(const Duration(minutes: 1));
+ expect(reads(), opened, reason: 'not again on its own, in a loop');
+
+ fake.routes[thread] = answer;
+ await tester.tap(find.byKey(const ValueKey('swarm-thread-retry')));
+ await tester.pumpAndSettle();
+
+ expect(reads(), opened + 1);
+ expect(find.text('Reply 5.'), findsOneWidget);
+ expect(find.text('Thread is down.'), findsNothing);
+ });
+ });
+}