Skip to content

Commit 0b7bddf

Browse files
klopez4212Terp
andcommitted
End agent DM Huddles when the last human device leaves
Retain direct-message membership while other humans remain so transcript history stays readable. Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz> Signed-off-by: kenny lopez <[email protected]>
1 parent e3a9bf5 commit 0b7bddf

2 files changed

Lines changed: 114 additions & 99 deletions

File tree

‎mobile/lib/features/channels/mobile_huddle_controller.dart‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ final class _HuddleAdmissionToken {
9292
final int epoch;
9393
final String backingChannelId;
9494

95-
/// Membership is shared by all of an owner's devices. A snapshot cannot
96-
/// prove that another admission did not arrive during teardown. In DMs,
97-
/// leave audio only; the relay ends the room after its last peer leaves.
98-
final bool relayOwnsCleanup;
95+
/// DM membership also grants access to the saved transcript after hanging up.
96+
final bool preserveMembership;
9997

10098
const _HuddleAdmissionToken(
10199
this.epoch,
102100
this.backingChannelId, {
103-
this.relayOwnsCleanup = false,
101+
this.preserveMembership = false,
104102
});
105103
}
106104

@@ -212,7 +210,7 @@ final class MobileHuddleController extends Notifier<bool> {
212210
_admissionToken = _HuddleAdmissionToken(
213211
admissionEpoch,
214212
backingChannelId,
215-
relayOwnsCleanup: _isDirectCall(parentChannelId),
213+
preserveMembership: _isDirectCall(parentChannelId),
216214
);
217215
} catch (_) {
218216
if (admissionEpoch == _admissionEpoch) {
@@ -251,7 +249,7 @@ final class MobileHuddleController extends Notifier<bool> {
251249
final admissionEpoch = ++_admissionEpoch;
252250
_latestAdmissionTargetBackingChannelId = ephemeralChannelId;
253251
final currentPubkey = ref.read(currentPubkeyProvider);
254-
final relayOwnsCleanup = _isDirectCall(parentChannelId);
252+
final preserveMembership = _isDirectCall(parentChannelId);
255253
try {
256254
await ref
257255
.read(huddleSessionProvider.notifier)
@@ -277,7 +275,7 @@ final class MobileHuddleController extends Notifier<bool> {
277275
_admissionToken = _HuddleAdmissionToken(
278276
admissionEpoch,
279277
ephemeralChannelId,
280-
relayOwnsCleanup: relayOwnsCleanup,
278+
preserveMembership: preserveMembership,
281279
);
282280
}
283281
}
@@ -365,7 +363,6 @@ final class MobileHuddleController extends Notifier<bool> {
365363
final humanCount =
366364
!session.wasAdmitted ||
367365
backingChannelId == null ||
368-
admissionToken?.relayOwnsCleanup == true ||
369366
_hasOtherOwnerAdmission(session)
370367
? null
371368
: ref
@@ -406,7 +403,7 @@ final class MobileHuddleController extends Notifier<bool> {
406403
final backingChannelId = session.ephemeralChannelId;
407404
final admissionToken = session.wasAdmitted ? _admissionToken : null;
408405
if (admissionToken == null || backingChannelId == null) return;
409-
if (admissionToken.relayOwnsCleanup || _hasOtherOwnerAdmission(session)) {
406+
if (_hasOtherOwnerAdmission(session)) {
410407
_admissionToken = null;
411408
return;
412409
}
@@ -469,6 +466,7 @@ final class MobileHuddleController extends Notifier<bool> {
469466
return;
470467
}
471468

469+
if (admissionToken.preserveMembership) return;
472470
try {
473471
await actions.leaveChannel(backingChannelId);
474472
} catch (_) {

‎mobile/test/features/channels/channel_detail_page_test.dart‎

Lines changed: 106 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -10549,103 +10549,120 @@ void main() {
1054910549
},
1055010550
);
1055110551

10552-
testWidgets(
10553-
'Codex DM hangup leaves cleanup to relay with a one-owner snapshot',
10554-
(tester) async {
10555-
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
10556-
final media = _HuddleTestMedia();
10557-
final transport = _HuddleTestTransport(
10558-
peers: const {2: HuddlePeer(pubkey: 'self', peerIndex: 2, epoch: 0)},
10559-
);
10560-
final relaySession = _ReconnectingRelaySession();
10561-
String? leftChannelId;
10562-
String? archivedChannelId;
10563-
10564-
await tester.pumpWidget(
10565-
_buildTestable(
10566-
channel: Channel(
10567-
id: _channelId,
10568-
name: 'Codex',
10569-
description: '',
10570-
channelType: 'dm',
10571-
visibility: 'private',
10572-
createdBy: 'self',
10573-
createdAt: DateTime(2025),
10574-
memberCount: 2,
10575-
participants: const ['Self', 'Agent'],
10576-
participantPubkeys: const ['self', 'agent'],
10577-
isMember: true,
10578-
),
10579-
voiceAgents: const {'agent'},
10580-
huddleHumanCountLoader: (_) async => 1,
10581-
messages: [
10582-
_huddleMsg(
10583-
id: 'last-human-call-leave',
10584-
kind: EventKind.huddleStarted,
10585-
pubkey: 'desktop',
10586-
createdAt: now,
10587-
),
10588-
],
10589-
users: const {
10590-
'desktop': UserProfile(pubkey: 'desktop'),
10591-
'agent': UserProfile(pubkey: 'agent', displayName: 'Pollen'),
10592-
'self': UserProfile(pubkey: 'self'),
10552+
for (final scenario in [
10553+
(anotherDevice: false, otherHuman: false),
10554+
(anotherDevice: true, otherHuman: false),
10555+
(anotherDevice: false, otherHuman: true),
10556+
]) {
10557+
final anotherDevice = scenario.anotherDevice;
10558+
final shouldEnd = !anotherDevice && !scenario.otherHuman;
10559+
testWidgets(
10560+
'Codex DM hangup ends only after the last human device: $scenario',
10561+
(tester) async {
10562+
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
10563+
final media = _HuddleTestMedia();
10564+
final transport = _HuddleTestTransport(
10565+
peers: {
10566+
2: const HuddlePeer(pubkey: 'self', peerIndex: 2, epoch: 0),
10567+
if (anotherDevice)
10568+
3: const HuddlePeer(pubkey: 'self', peerIndex: 3, epoch: 0),
1059310569
},
10594-
relayConfigNotifier: _HuddleRelayConfigNotifier(),
10595-
relaySessionNotifier: relaySession,
10596-
huddleCurrentPubkey: 'self',
10597-
huddleMembers: [
10598-
ChannelMember(
10599-
pubkey: 'self',
10600-
role: 'member',
10601-
joinedAt: DateTime(2025),
10570+
);
10571+
final relaySession = _ReconnectingRelaySession();
10572+
String? leftChannelId;
10573+
String? archivedChannelId;
10574+
10575+
await tester.pumpWidget(
10576+
_buildTestable(
10577+
channel: Channel(
10578+
id: _channelId,
10579+
name: 'Codex',
10580+
description: '',
10581+
channelType: 'dm',
10582+
visibility: 'private',
10583+
createdBy: 'self',
10584+
createdAt: DateTime(2025),
10585+
memberCount: 2,
10586+
participants: const ['Self', 'Agent'],
10587+
participantPubkeys: const ['self', 'agent'],
10588+
isMember: true,
1060210589
),
10603-
ChannelMember(
10604-
pubkey: 'agent',
10605-
role: 'bot',
10606-
joinedAt: DateTime(2025),
10590+
voiceAgents: scenario.otherHuman ? const {} : const {'agent'},
10591+
huddleHumanCountLoader: (_) async => scenario.otherHuman ? 2 : 1,
10592+
messages: [
10593+
_huddleMsg(
10594+
id: 'last-human-call-leave',
10595+
kind: EventKind.huddleStarted,
10596+
pubkey: 'desktop',
10597+
createdAt: now,
10598+
),
10599+
],
10600+
users: const {
10601+
'desktop': UserProfile(pubkey: 'desktop'),
10602+
'agent': UserProfile(pubkey: 'agent', displayName: 'Pollen'),
10603+
'self': UserProfile(pubkey: 'self'),
10604+
},
10605+
relayConfigNotifier: _HuddleRelayConfigNotifier(),
10606+
relaySessionNotifier: relaySession,
10607+
huddleCurrentPubkey: 'self',
10608+
huddleMembers: [
10609+
ChannelMember(
10610+
pubkey: 'self',
10611+
role: 'member',
10612+
joinedAt: DateTime(2025),
10613+
),
10614+
ChannelMember(
10615+
pubkey: 'agent',
10616+
role: scenario.otherHuman ? 'member' : 'bot',
10617+
joinedAt: DateTime(2025),
10618+
),
10619+
],
10620+
huddleMediaFactory: () => media,
10621+
huddleTransportFactory: (_) => transport,
10622+
createChannelActions: (ref) => _FakeChannelActions(
10623+
ref,
10624+
onLeaveChannel: (channelId) async => leftChannelId = channelId,
10625+
onArchiveChannel: (channelId) async =>
10626+
archivedChannelId = channelId,
1060710627
),
10608-
],
10609-
huddleMediaFactory: () => media,
10610-
huddleTransportFactory: (_) => transport,
10611-
createChannelActions: (ref) => _FakeChannelActions(
10612-
ref,
10613-
onLeaveChannel: (channelId) async => leftChannelId = channelId,
10614-
onArchiveChannel: (channelId) async =>
10615-
archivedChannelId = channelId,
1061610628
),
10617-
),
10618-
);
10619-
await tester.pumpAndSettle();
10629+
);
10630+
await tester.pumpAndSettle();
1062010631

10621-
await tester.tap(find.widgetWithText(FilledButton, 'Join'));
10622-
await tester.pumpAndSettle();
10623-
relaySession.connect();
10624-
await tester.pump();
10625-
await tester.tap(find.byKey(const ValueKey('huddle-leave')));
10626-
await tester.pump();
10627-
for (
10628-
var attempt = 0;
10629-
attempt < 100 && archivedChannelId == null;
10630-
attempt++
10631-
) {
10632+
await tester.tap(find.widgetWithText(FilledButton, 'Join'));
10633+
await tester.pumpAndSettle();
10634+
relaySession.connect();
1063210635
await tester.pump();
10633-
}
10634-
for (var attempt = 0; attempt < 20; attempt++) {
10636+
await tester.tap(find.byKey(const ValueKey('huddle-leave')));
10637+
await tester.pump();
10638+
for (
10639+
var attempt = 0;
10640+
attempt < 100 && archivedChannelId == null;
10641+
attempt++
10642+
) {
10643+
await tester.pump();
10644+
}
10645+
for (var attempt = 0; attempt < 20; attempt++) {
10646+
await tester.pump();
10647+
}
10648+
await tester.pump(const Duration(milliseconds: 300));
10649+
await tester.pump(const Duration(milliseconds: 300));
1063510650
await tester.pump();
10636-
}
10637-
await tester.pump(const Duration(milliseconds: 300));
10638-
await tester.pump(const Duration(milliseconds: 300));
10639-
await tester.pump();
1064010651

10641-
expect(leftChannelId, isNull);
10642-
expect(archivedChannelId, isNull);
10643-
expect(
10644-
relaySession.publishedKinds,
10645-
isNot(contains(EventKind.huddleEnded)),
10646-
);
10647-
},
10648-
);
10652+
expect(leftChannelId, isNull);
10653+
expect(
10654+
archivedChannelId,
10655+
shouldEnd ? '8d764100-fd8f-44cf-9c98-6d8fbd739b8c' : isNull,
10656+
);
10657+
expect(
10658+
relaySession.publishedKinds,
10659+
shouldEnd
10660+
? contains(EventKind.huddleEnded)
10661+
: isNot(contains(EventKind.huddleEnded)),
10662+
);
10663+
},
10664+
);
10665+
}
1064910666

1065010667
testWidgets('top-right call end auto-ends for the last human', (
1065110668
tester,

0 commit comments

Comments
 (0)