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 voice take ends when the person can no longer post there (#2478)

0xken15c2db9512d3e1
diff --git a/mobile/lib/swarm/swarm_conversation_page.dart b/mobile/lib/swarm/swarm_conversation_page.dart
index 104e4ab92..e7f571663 100644
--- a/mobile/lib/swarm/swarm_conversation_page.dart
+++ b/mobile/lib/swarm/swarm_conversation_page.dart
@@ -349,8 +349,10 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
);
c.composer.addListener(_drafted);
c.addListener(_chatMoved);
+ c.addListener(_guardVoice);
if (widget.mention case final agentId?) c.mention(agentId);
reader.addListener(_see);
+ reader.addListener(_guardVoice);
session.inbox.addListener(_see);
_see();
_watch();
@@ -422,6 +424,17 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
/// A read came in: the line, if a failed read left it to place; the notice's
/// message, once a read has brought what to look for it in; and what is
/// offered of the line, since the list changed.
+ /// A take goes with the right to post here, as in a thread: the chat lost,
+ /// archived, or the person no longer a writer. Its words land nowhere
+ /// ([SwarmVoice.hide]). Nothing while the chat is still being read.
+ void _guardVoice() {
+ final c = _conversation;
+ if (c == null || c.reader.snapshot == null && !c.reader.accessRevoked) {
+ return;
+ }
+ if (c.state == SwarmChatState.lost || !c.canPost) _voice?.hide();
+ }
+
void _chatMoved() {
final c = _conversation;
if (c == null || !_firstRead) return;
@@ -631,13 +644,18 @@ class _SwarmConversationPageState extends State<SwarmConversationPage>
void _close() {
final session = _session;
+ // Signed out, another account, or Swarm turned off: the take goes with
+ // the chat it was for.
+ _voice?.hide();
session?.inbox
?..removeListener(_attach)
..removeListener(_see);
if (_conversation case final c?) {
c.composer.removeListener(_drafted);
c.removeListener(_chatMoved);
+ c.removeListener(_guardVoice);
c.reader.removeListener(_see);
+ c.reader.removeListener(_guardVoice);
c.dispose();
}
if (_buddy != widget.buddy) _buddy?.dispose();
diff --git a/mobile/test/swarm/swarm_chat_voice_test.dart b/mobile/test/swarm/swarm_chat_voice_test.dart
index 64c8ec0f6..7c3a3ee87 100644
--- a/mobile/test/swarm/swarm_chat_voice_test.dart
+++ b/mobile/test/swarm/swarm_chat_voice_test.dart
@@ -10,6 +10,7 @@ import 'package:harness_mobile/phone/voice_recorder.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';
@@ -75,12 +76,15 @@ void main() {
fake.routes[commands] = (_) => {'result': <String, dynamic>{}};
});
+ /// Who is signed in, in the last [pump].
+ late ValueNotifier<String?> account;
+
/// Chat g1 on screen, its composer's mic heard through [recorder] and
/// [stt] — or with no voice input at all.
- Future<void> pump(WidgetTester tester, {bool voice = true}) async {
+ Future<SwarmHost> pump(WidgetTester tester, {bool voice = true}) async {
final features = ExperimentalFeaturesStore(storage: MemoryKeyValueStore())
..value = {PhoneExperiment.swarm};
- final account = ValueNotifier<String?>(me);
+ account = ValueNotifier<String?>(me);
final host = SwarmHost(
accountId: () => account.value,
account: account,
@@ -104,6 +108,7 @@ void main() {
),
);
await tester.pumpAndSettle();
+ return host;
}
Finder key(String value) => find.byKey(ValueKey(value));
@@ -683,6 +688,101 @@ void main() {
expect(key('swarm-mic'), findsNothing);
expect(key('swarm-send'), findsOneWidget);
});
+
+ // The composer goes with the right to post, and its mic with it: a take
+ // must not go on recording, or land its words, once the person can no
+ // longer post here (as a thread's reply box already guards).
+ group('access lost mid-take', () {
+ const refused = SuperTabFailure('FORBIDDEN', 'Not yours.', statusCode: 403);
+
+ /// The Swarm read again: refused, or with the person a [role].
+ Future<void> reread(
+ WidgetTester tester,
+ SwarmHost host, {
+ bool lost = false,
+ String? role,
+ }) async {
+ if (lost) {
+ fake.routes['/api/super-tabs/$tab/changes'] = (_) => throw refused;
+ fake.routes['/api/super-tabs/$tab/parts'] = (_) => throw refused;
+ } else {
+ final parts = fake.routes['/api/super-tabs/$tab/parts']!;
+ fake.routes['/api/super-tabs/$tab/parts'] = (body) => {
+ ...(parts(body) as Map<String, dynamic>),
+ 'members': [part(me, 2, person(me, role: role!))],
+ };
+ fake.routes['/api/super-tabs/$tab/changes'] = (_) => {'reload': true};
+ }
+ await host.session!.inbox.reader(tab)!.refresh();
+ await frames(tester);
+ }
+
+ testWidgets('signed out: the microphone closes, nothing is transcribed '
+ 'or sent', (tester) async {
+ await pump(tester);
+ stt.replies.add('after sign out');
+ await tapMic(tester);
+ expect(recorder.recording, isTrue);
+
+ account.value = null;
+ await frames(tester);
+
+ expect(recorder.recording, isFalse);
+ expect(stt.calls, isEmpty);
+ expect(fake.commands, isEmpty);
+ });
+
+ testWidgets('made read-only: the take goes with the composer', (
+ tester,
+ ) async {
+ final host = await pump(tester);
+ await tapMic(tester);
+ expect(recorder.recording, isTrue);
+
+ await reread(tester, host, role: 'viewer');
+
+ expect(key('swarm-read-only'), findsOneWidget);
+ expect(key('swarm-mic'), findsNothing);
+ expect(recorder.recording, isFalse);
+ expect(stt.calls, isEmpty);
+ expect(fake.commands, isEmpty);
+ });
+
+ testWidgets('the Swarm lost: the microphone closes', (tester) async {
+ final host = await pump(tester);
+ await tapMic(tester);
+ expect(recorder.recording, isTrue);
+
+ await reread(tester, host, lost: true);
+
+ expect(recorder.recording, isFalse);
+ expect(stt.calls, isEmpty);
+ });
+
+ testWidgets('words transcribed after the person was made read-only land '
+ 'nowhere: not sent, and not kept in the draft', (tester) async {
+ final host = await pump(tester);
+ stt.pending = Completer<String>();
+ await tapMic(tester);
+ recorder.meter.value = 0.8;
+ await tester.pump(const Duration(milliseconds: 500));
+ await tapMic(tester);
+ expect(stt.calls, hasLength(1), reason: 'the take is being transcribed');
+
+ await reread(tester, host, role: 'viewer');
+ stt.pending!.complete('too late');
+ await tester.pumpAndSettle();
+
+ expect(fake.commands, isEmpty);
+ expect(find.text('too late'), findsNothing);
+
+ // Nor kept in the draft for when the person may post again.
+ await reread(tester, host, role: 'owner');
+ await tester.pumpAndSettle();
+ expect(key('swarm-mic'), findsOneWidget);
+ expect(box(tester), isEmpty);
+ });
+ });
}
/// A microphone that also says how loud it is, for the mic's bars.