@@ -8,6 +8,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
88import '../../shared/crypto/nip_oa.dart' ;
99import '../../shared/crypto/signed_event.dart' ;
1010import '../../shared/relay/relay.dart' ;
11+ import '../profile/user_cache_provider.dart' ;
1112
1213part 'agent_policy.dart' ;
1314part 'agent_authorization.dart' ;
@@ -92,28 +93,41 @@ final agentDirectoryProvider = FutureProvider<List<AgentDirectoryEntry>>((
9293 if (! current ()) return [];
9394 return readAgentAuthorization (
9495 session,
95- {...owned, ...events.map ((event) => event.pubkey)},
96+ {
97+ ...owned, ...events.map ((event) => event.pubkey),
98+ // Refresh previously authenticated owners even after runtime/policy
99+ // discovery stops returning them (for example across reconnect).
100+ for (final profile in ref.read (userCacheProvider).values)
101+ if (profile.ownerPubkey != null ) profile.pubkey,
102+ },
96103 viewer: viewer,
97104 isCurrent: current,
105+ resolveProfileOwners: (profiles) {
106+ if (! current ()) throw StateError ('Profile authority scope changed' );
107+ final cache = ref.read (userCacheProvider.notifier);
108+ for (final profile in profiles) {
109+ cache.cacheProfileEvent (profile);
110+ }
111+ return {
112+ for (final profile in ref.read (userCacheProvider).values)
113+ if (profile.ownerPubkey != null ) profile.pubkey: profile.ownerPubkey! ,
114+ };
115+ },
98116 );
99117});
100118
101119/// Verified NIP-OA owner pubkey per agent pubkey, from the agents' kind:0
102120/// profiles. An entry exists only when the `auth` tag verifies — mirrors
103121/// desktop's `profile_valid_oa_owner_pubkey` .
104122final agentOwnersProvider = FutureProvider <Map <String , String >>((ref) async {
105- final agents = await ref.watch (agentDirectoryProvider.future);
106- if (agents.isEmpty) return const {};
107- final session = ref.read (relaySessionProvider.notifier);
108- final events = await session.fetchHistory (
109- NostrFilters .profilesBatch ([for (final agent in agents) agent.pubkey]),
110- );
111- final owners = < String , String > {};
112- for (final event in latestProfileEvents (events).values) {
113- final owner = verifiedOaOwnerPubkey (event);
114- if (owner != null ) owners[event.pubkey.toLowerCase ()] = owner;
115- }
116- return owners;
123+ await ref.watch (agentDirectoryProvider.future);
124+ // The profile cache is the single ordered ownership source for lifecycle,
125+ // provenance and mention fallbacks; never re-query a parallel owner snapshot.
126+ final profiles = ref.watch (userCacheProvider);
127+ return {
128+ for (final profile in profiles.values)
129+ if (profile.ownerPubkey != null ) profile.pubkey: profile.ownerPubkey! ,
130+ };
117131});
118132
119133/// Pubkeys currently known to represent agents across the active relay.
0 commit comments