Skip to content

Commit c73ff57

Browse files
committed
fix(mobile): resolve removal-aware roster heads for discovery
Signed-off-by: Logan Johnson <[email protected]>
1 parent 882f69c commit c73ff57

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎mobile/lib/shared/mentions/agent_authorization.dart‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,37 @@ Future<List<AgentDirectoryEntry>> readAgentAuthorization(
2222
}
2323
final authority = await session.fetchRelaySelf();
2424
check();
25-
final membership = await _membershipPages(
25+
var membership = await _membershipPages(
2626
session,
2727
authority,
2828
viewer,
2929
channelId,
3030
check,
3131
);
32+
if (channelId == null) {
33+
// #p discovers destinations, not authority: a replacement removing the
34+
// viewer cannot match that filter. Re-read each exact coordinate without
35+
// #p, and never retain a discovery snapshot if its head is absent.
36+
final destinations = membership
37+
.where((e) => e.kind == 39002 && e.pubkey == authority)
38+
.map((e) => e.getTagValue('d'))
39+
.whereType<String>()
40+
.toSet();
41+
if (destinations.length > 1000) {
42+
throw StateError('Membership destination budget exceeded');
43+
}
44+
membership = await _queryAgentFilters(session, [
45+
for (final destination in destinations)
46+
NostrFilter(
47+
kinds: const [39002],
48+
authors: [authority],
49+
tags: {
50+
'#d': [destination],
51+
},
52+
limit: 1,
53+
),
54+
], checkCurrent: check);
55+
}
3256
check();
3357
final runtime = await _queryAgentFilters(session, [
3458
for (final key in requestedKeys)

‎mobile/lib/shared/mentions/agent_identity_provider.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ part 'agent_authorization.dart';
1919
class AgentDirectoryEntry {
2020
final String pubkey;
2121
final String? displayName;
22+
/// Owner authenticated by the agent's verified NIP-OA kind:0 attestation,
23+
/// not a runtime ownership claim. Null means no authenticated owner here.
2224
final String? ownerPubkey;
2325
final String? respondTo;
2426
final List<String> respondToAllowlist;

‎mobile/test/shared/mentions/agent_authorization_test.dart‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22

33
import 'package:buzz/shared/mentions/agent_identity_provider.dart';
4+
import 'package:buzz/features/channels/mentions/mention_candidates.dart';
45
import 'package:buzz/shared/relay/relay.dart';
56
import 'package:flutter_test/flutter_test.dart';
67
import 'package:hooks_riverpod/hooks_riverpod.dart';
@@ -149,6 +150,57 @@ void main() {
149150
},
150151
);
151152

153+
test(
154+
'global discovery re-reads removal heads without viewer filter',
155+
() async {
156+
final old = signed(
157+
relay,
158+
39002,
159+
'',
160+
tags: [
161+
['d', 'room'],
162+
['p', owner.public],
163+
['p', agent.public, '', 'bot'],
164+
],
165+
);
166+
final removed = members(time: 101, includeViewer: false);
167+
final session = _ReadSession([runtime], relay.public);
168+
var removedNow = false;
169+
session.page = (filters) {
170+
final filter = filters.single;
171+
expect(filter.authors, [relay.public]);
172+
if (filter.tags.containsKey('#p')) {
173+
expect(filter.tags['#p'], [owner.public]);
174+
return [old]; // The removed head cannot match #p=viewer.
175+
}
176+
expect(filter.tags, {
177+
'#d': ['room'],
178+
});
179+
expect(filter.limit, 1);
180+
return [removedNow ? removed : old];
181+
};
182+
final before = await read(session, destination: null);
183+
expect(before.single.channelIds, ['room']);
184+
removedNow = true;
185+
final after = await read(
186+
_ReadSession([runtime], relay.public)..page = session.page,
187+
destination: null,
188+
);
189+
expect(after, isEmpty);
190+
expect(
191+
buildMentionCandidates(
192+
members: [],
193+
relayAgents: after,
194+
sharedChannelIds: {'room'},
195+
userCache: {},
196+
ownerByAgentPubkey: {},
197+
currentPubkey: owner.public,
198+
),
199+
isEmpty,
200+
);
201+
},
202+
);
203+
152204
test('same-second membership tie is deterministic', () async {
153205
final yes = members();
154206
final no = members(includeAgent: false);
@@ -197,6 +249,7 @@ void main() {
197249
final session = _ReadSession([owned, policy], relay.public);
198250
var calls = 0;
199251
session.page = (filters) {
252+
if (filters.first.tags.containsKey('#d')) return [first, last];
200253
calls++;
201254
if (calls == 1) return List.filled(500, first);
202255
expect(filters.single.until, first.createdAt);

0 commit comments

Comments
 (0)