Skip to content

Commit 6bd0894

Browse files
loganjLarryclaude
committed
fix(mobile): name saved archived agents apart; skip names for idle Solo rows
A saved agent switch or a resumed new chat can still point to an agent that is now archived. Its recovery notice, Resume/Discard dialog and draft header now label it as an agent against the live agents, so it never reads exactly like a live namesake. Solo rows no longer build a name context while no agent is working in them. Working rows read one shared context instead of each copying the profile cache. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent fb9c8fc commit 6bd0894

7 files changed

Lines changed: 213 additions & 35 deletions

File tree

‎mobile/lib/features/channels/me/me_chat_assistant.dart‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,6 @@ useMeChatAssistant(
9797
? ref.watch(userCacheProvider)[selected] ??
9898
ref.read(userCacheProvider.notifier).get(selected)
9999
: null;
100-
// The owner's inventory name outranks the agent's self-published profile.
101-
final names = watchMeAssistantNames(ref, agents);
102-
final name = agents.any((agent) => agent.pubkey == selected)
103-
? names.labelFor(selected!)
104-
: profile?.displayName;
105-
final avatarInitial = name?.trim().isNotEmpty == true
106-
? name!.trim()[0].toUpperCase()
107-
: profile?.initial ?? '?';
108-
String agentName(String pubkey) {
109-
final candidates = [
110-
agents.any((a) => a.pubkey == pubkey) ? names.labelFor(pubkey) : null,
111-
ref.read(userCacheProvider)[pubkey]?.displayName,
112-
];
113-
return candidates
114-
.whereType<String>()
115-
.where((name) => name.trim().isNotEmpty)
116-
.firstOrNull ??
117-
(pubkey.length > 12
118-
? '${pubkey.substring(0, 8)}…${pubkey.substring(pubkey.length - 4)}'
119-
: pubkey);
120-
}
121-
122100
MeAgentChange? pending;
123101
if (sync != null) {
124102
try {
@@ -127,6 +105,31 @@ useMeChatAssistant(
127105
// The action surfaces an unreadable journal without crashing the chat.
128106
}
129107
}
108+
// The owner's inventory name outranks the agent's self-published profile.
109+
final names = watchMeAssistantNames(
110+
ref,
111+
agents,
112+
saved: [selected, pending?.agent],
113+
);
114+
// A known agent outside the inventory, such as an archived one, is labeled
115+
// against the live agents so it never reads as a live namesake.
116+
bool named(String pubkey) =>
117+
agents.any((agent) => agent.pubkey == pubkey) ||
118+
ref.read(userCacheProvider)[pubkey]?.displayName?.trim().isNotEmpty ==
119+
true;
120+
final name = selected != null && named(selected)
121+
? names.labelFor(selected)
122+
: profile?.displayName;
123+
final avatarInitial = name?.trim().isNotEmpty == true
124+
? name!.trim()[0].toUpperCase()
125+
: profile?.initial ?? '?';
126+
String agentName(String pubkey) {
127+
if (named(pubkey)) return names.labelFor(pubkey);
128+
return pubkey.length > 12
129+
? '${pubkey.substring(0, 8)}…${pubkey.substring(pubkey.length - 4)}'
130+
: pubkey;
131+
}
132+
130133
final pendingName = pending == null ? null : agentName(pending.agent);
131134
final label = pendingName != null
132135
? 'Pending agent change: $pendingName'

‎mobile/lib/features/channels/me/me_chat_draft.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ class _MeChatDraft extends HookConsumerWidget {
2727
final agent = choices
2828
.where((agent) => agent.pubkey == selected)
2929
.firstOrNull;
30-
final names = watchMeAssistantNames(ref, choices);
30+
final names = watchMeAssistantNames(ref, choices, saved: [selected]);
3131
// The composer keeps the plain name; only what the owner reads is labeled.
3232
final plainName =
3333
agent?.displayName ??
3434
profile?.label ??
3535
(selected == null ? 'Choose an agent' : 'Agent');
36-
final name = agent == null ? plainName : names.labelFor(agent.pubkey);
36+
// A saved choice that is no longer offered, such as an archived agent, is
37+
// labeled against the live agents so it never reads as a live namesake.
38+
final name =
39+
agent != null || profile?.displayName?.trim().isNotEmpty == true
40+
? names.labelFor(selected!)
41+
: plainName;
3742
final avatarInitial = name.isEmpty ? 'A' : name.characters.first;
3843
final scopeChanged = originalScope != (sync.community, sync.pubkey);
3944
final frozen =
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:hooks_riverpod/hooks_riverpod.dart';
22

3-
import '../../../shared/identity_names/identity_names_provider.dart';
43
import '../../../shared/me/me_assistant_names.dart';
54
import '../../../shared/me/me_assistants.dart';
65
import '../channel_typing_provider.dart';
@@ -12,15 +11,14 @@ final meWorkingAgentsProvider = Provider.autoDispose
1211
.family<List<String>, String>((ref, channelId) {
1312
final agents = ref.watch(meAssistantsProvider).value ?? [];
1413
if (agents.isEmpty) return const [];
15-
final names = meAssistantNames(
16-
ref.watch(identityNameSourcesProvider),
17-
agents,
18-
);
1914
final typing = ref.watch(channelTypingProvider(channelId));
2015
final active = typing.map((entry) => entry.pubkey.toLowerCase()).toSet();
21-
return [
16+
final working = [
2217
for (final agent in agents)
23-
if (active.contains(agent.pubkey.toLowerCase()))
24-
names.labelFor(agent.pubkey),
18+
if (active.contains(agent.pubkey.toLowerCase())) agent.pubkey,
2519
];
20+
// An idle chat reads no names at all.
21+
if (working.isEmpty) return const [];
22+
final names = ref.watch(meAssistantNamesProvider);
23+
return [for (final pubkey in working) names.labelFor(pubkey)];
2624
});

‎mobile/lib/shared/me/me_assistant_names.dart‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import '../identity_names/identity_names.dart';
44
import '../identity_names/identity_names_provider.dart';
55
import '../mentions/agent_identity_provider.dart';
66
import '../profile/user_profile.dart';
7+
import 'me_assistants.dart';
78

89
/// [base] with the owner's inventory name ahead of each agent's self-published
910
/// profile name, as Solo has always shown it. Verified names still win, and
@@ -55,16 +56,28 @@ IdentityNames meAssistantNames(
5556
).scope(keys, agentPubkeys: keys.toSet());
5657
}
5758

59+
/// [meAssistantNames] for the live inventory, built once for every reader so
60+
/// each Solo row does not copy the profile cache again.
61+
final meAssistantNamesProvider = Provider.autoDispose<IdentityNames>(
62+
(ref) => meAssistantNames(
63+
ref.watch(identityNameSourcesProvider),
64+
ref.watch(meAssistantsProvider).value ?? const [],
65+
),
66+
);
67+
5868
/// [meAssistantNames] for a widget, loading the profiles it still lacks.
69+
/// [saved] are agents a saved choice still names that Solo no longer offers,
70+
/// such as an archived agent: they are labeled as agents against [agents].
5971
IdentityNames watchMeAssistantNames(
6072
WidgetRef ref,
61-
List<AgentDirectoryEntry> agents,
62-
) {
73+
List<AgentDirectoryEntry> agents, {
74+
Iterable<String?> saved = const [],
75+
}) {
6376
final keys = [for (final agent in agents) agent.pubkey];
6477
return watchIdentityNames(
6578
ref,
6679
keys,
67-
agentPubkeys: keys.toSet(),
80+
agentPubkeys: {...keys, ...saved.nonNulls},
6881
sources: meAssistantNameSources(
6982
ref.watch(identityNameSourcesProvider),
7083
agents,

‎mobile/test/features/channels/me/me_chat_assistant_test.dart‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,5 +525,26 @@ void main() {
525525
await tester.tap(find.byKey(const ValueKey('me-assistant-button')));
526526
await tester.pumpAndSettle();
527527
expect(pickerLabels(), ['Atlas']);
528+
await tester.tapAt(Offset.zero);
529+
await tester.pumpAndSettle();
530+
531+
// A saved switch to the archived agent still names it apart from its live
532+
// namesake, where the owner decides whether to finish it.
533+
await MeAgentChange.create(sync, 'chat', first).save(sync, 'chat');
534+
ProviderScope.containerOf(
535+
tester.element(find.byType(_Harness)),
536+
).invalidate(meAssistantsProvider);
537+
await tester.pumpAndSettle();
538+
final pending = find.textContaining('Finish choosing ');
539+
final archivedName = tester
540+
.widget<Text>(pending)
541+
.data!
542+
.substring('Finish choosing '.length);
543+
expect(archivedName, startsWith('Atlas'));
544+
expect(archivedName, isNot('Atlas'));
545+
expect(headerLabel(), 'Pending agent change: $archivedName');
546+
await tester.tap(pending);
547+
await tester.pumpAndSettle();
548+
expect(find.textContaining('Your choice of $archivedName'), findsOneWidget);
528549
});
529550
}

‎mobile/test/features/channels/me/me_page_test.dart‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ import 'package:buzz/shared/me/me_groups.dart';
2323
import 'package:buzz/shared/me/me_sync.dart';
2424
import 'package:buzz/shared/theme/theme.dart';
2525
import 'package:buzz/shared/widgets/app_list.dart';
26+
import 'package:buzz/shared/profile/user_cache_provider.dart';
27+
import 'package:buzz/shared/profile/user_profile.dart';
28+
29+
class _CachedProfiles extends UserCacheNotifier {
30+
_CachedProfiles(this.profiles);
31+
final Map<String, UserProfile> profiles;
32+
@override
33+
Map<String, UserProfile> build() => profiles;
34+
}
2635

2736
class _UnavailableRelay extends RelaySessionNotifier {
2837
@override
@@ -501,6 +510,69 @@ void main() {
501510
expect(pickerLabels(), ['Atlas']);
502511
});
503512

513+
testWidgets('a resumed chat for an archived agent names it apart', (
514+
tester,
515+
) async {
516+
SharedPreferences.setMockInitialValues({});
517+
final prefs = await SharedPreferences.getInstance();
518+
final action = ValueNotifier<VoidCallback?>(null);
519+
addTearDown(action.dispose);
520+
final sync = MeSync(
521+
_UnavailableRelay(),
522+
RelayConfig(
523+
baseUrl: 'https://relay.test',
524+
nsec: nostr.Keys.generate().nsec,
525+
),
526+
prefs: prefs,
527+
);
528+
final archived = 'a1' * 32;
529+
final live = 'b2' * 32;
530+
await MeChatCreation('same-id', 'Unfinished chat', archived).save(sync);
531+
await tester.pumpWidget(
532+
ProviderScope(
533+
overrides: [
534+
savedPrefsProvider.overrideWithValue(prefs),
535+
defaultAgentProvider.overrideWith((ref) => Stream.value(null)),
536+
meSyncProvider.overrideWithValue(sync),
537+
meGroupsProvider.overrideWith(
538+
(ref) => Stream.value(const MeGroups()),
539+
),
540+
channelsProvider.overrideWith(FakeChannels.new),
541+
meAssistantsProvider.overrideWith(
542+
(ref) async => [
543+
AgentDirectoryEntry(pubkey: live, displayName: 'Atlas'),
544+
],
545+
),
546+
// The archived agent's profile stays in the client's cache.
547+
userCacheProvider.overrideWith(
548+
() => _CachedProfiles({
549+
archived: UserProfile(pubkey: archived, displayName: 'Atlas'),
550+
}),
551+
),
552+
agentDirectoryProvider.overrideWith((ref) async => []),
553+
agentOwnersProvider.overrideWith((ref) async => {}),
554+
customEmojiListProvider.overrideWithValue([]),
555+
],
556+
child: MaterialApp(
557+
theme: AppTheme.light(),
558+
home: MePage(newChatAction: action),
559+
),
560+
),
561+
);
562+
await tester.pumpAndSettle();
563+
action.value!();
564+
await tester.pumpAndSettle();
565+
await tester.tap(find.text('Resume'));
566+
await tester.pumpAndSettle();
567+
final tooltip = tester
568+
.widgetList<IconButton>(find.byType(IconButton))
569+
.map((button) => button.tooltip)
570+
.whereType<String>()
571+
.singleWhere((label) => label.startsWith('Change agent: '));
572+
expect(tooltip, startsWith('Change agent: Atlas'));
573+
expect(tooltip, isNot('Change agent: Atlas'));
574+
});
575+
504576
for (final resume in [true, false]) {
505577
testWidgets(
506578
'new chat resumes a named creation in the draft: resume=$resume',
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'package:buzz/features/channels/channel_typing_provider.dart';
2+
import 'package:buzz/features/channels/me/me_working_provider.dart';
3+
import 'package:buzz/shared/identity_names/identity_names.dart';
4+
import 'package:buzz/shared/identity_names/identity_names_provider.dart';
5+
import 'package:buzz/shared/me/me_assistant_names.dart';
6+
import 'package:buzz/shared/me/me_assistants.dart';
7+
import 'package:buzz/shared/mentions/agent_identity_provider.dart';
8+
import 'package:flutter_test/flutter_test.dart';
9+
import 'package:hooks_riverpod/hooks_riverpod.dart';
10+
11+
class _Typing extends ChannelTypingNotifier {
12+
_Typing(super.channelId);
13+
@override
14+
List<TypingEntry> build() => [];
15+
void show(List<String> pubkeys) => state = [
16+
for (final pubkey in pubkeys)
17+
TypingEntry(pubkey: pubkey, expiresAtMs: 999999),
18+
];
19+
}
20+
21+
void main() {
22+
test('idle Solo rows build no names; working namesakes read apart', () async {
23+
final first = 'a1' * 32;
24+
final second = 'b2' * 32;
25+
final chats = ['one', 'two'];
26+
final typing = {for (final id in chats) id: _Typing(id)};
27+
var nameReads = 0;
28+
final container = ProviderContainer(
29+
overrides: [
30+
meAssistantsProvider.overrideWith(
31+
(ref) async => [
32+
for (final key in [first, second])
33+
AgentDirectoryEntry(pubkey: key, displayName: 'Atlas'),
34+
],
35+
),
36+
identityNameSourcesProvider.overrideWith((ref) {
37+
nameReads++;
38+
return IdentityNameSources.empty;
39+
}),
40+
for (final id in chats)
41+
channelTypingProvider(id).overrideWith(() => typing[id]!),
42+
],
43+
);
44+
addTearDown(container.dispose);
45+
await container.read(meAssistantsProvider.future);
46+
final rows = {
47+
for (final id in chats)
48+
id: container.listen(meWorkingAgentsProvider(id), (_, _) {}),
49+
};
50+
51+
// Nobody is working: no row copies the profile cache into a name context.
52+
expect([for (final row in rows.values) row.read()], [isEmpty, isEmpty]);
53+
expect(nameReads, 0);
54+
expect(container.exists(meAssistantNamesProvider), isFalse);
55+
56+
typing['one']!.show([first]);
57+
typing['two']!.show([first, second]);
58+
final one = rows['one']!.read();
59+
final two = rows['two']!.read();
60+
expect(one, hasLength(1));
61+
expect(two, hasLength(2));
62+
expect(two.toSet(), hasLength(2), reason: 'Namesakes must differ');
63+
expect(two.first, one.single, reason: 'One agent reads alike everywhere');
64+
expect(two, everyElement(startsWith('Atlas')));
65+
});
66+
}

0 commit comments

Comments
 (0)