Skip to content

Commit b0a3243

Browse files
committed
Make moderation discovery retries convergent
Signed-off-by: kenny lopez <[email protected]>
1 parent 896378c commit b0a3243

4 files changed

Lines changed: 167 additions & 28 deletions

File tree

‎crates/buzz-relay/src/handlers/moderation_notices.rs‎

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ pub async fn send_moderation_notice(
118118
)
119119
.await?
120120
{
121+
// A prior attempt may have committed the notice before a
122+
// transient discovery failure. Duplicate delivery is still a
123+
// content no-op, but retry the idempotent discovery generation so
124+
// the channel eventually has a complete 39000/39001/39002 set.
125+
emit_group_discovery_events(tenant, state, existing_dm.id).await?;
121126
return Ok(());
122127
}
123128
}
@@ -146,20 +151,12 @@ pub async fn send_moderation_notice(
146151
.increment(1);
147152
}
148153

149-
// 2. Ensure the relay's "{host} Moderation" kind:0 profile exists, and 3.
150-
// the DM's kind:39000 discovery (with `hidden` / `t=dm` / `p`). Both are
151-
// replaceable events, so we emit them on EVERY send rather than gating on
152-
// first creation: if discovery failed on the first delivery (it is
153-
// `?`-propagated), a `was_created`-gated retry would skip it forever and
154-
// leave the thread permanently undiscoverable — a notice delivered into a
155-
// channel no client can render. Notices are rare; unconditional re-emit is
156-
// cheap and `replace_addressable_event` makes it idempotent.
154+
// 2. Ensure the relay's "{host} Moderation" kind:0 profile exists.
157155
if let Err(e) = publish_moderation_profile(tenant, state, &relay_pubkey_hex).await {
158156
warn!(error = %e, "moderation profile publish failed (continuing)");
159157
}
160-
emit_group_discovery_events(tenant, state, dm_channel_id).await?;
161158

162-
// 4. Insert the relay-signed kind:9 notice with `h=<dm_channel_id>` and a
159+
// 3. Insert the relay-signed kind:9 notice with `h=<dm_channel_id>` and a
163160
// `moderation_source` tag naming the source row id (idempotency +
164161
// client linking).
165162
let tags = vec![
@@ -197,6 +194,14 @@ pub async fn send_moderation_notice(
197194
let kind_u32 = event_kind_u32(&stored.event);
198195
dispatch_persistent_event(tenant, state, &stored, kind_u32, &relay_pubkey_hex, None).await;
199196

197+
// 4. Publish the idempotent discovery generation after the notice is
198+
// durable. The roster is established first inside the helper, so a roster
199+
// failure cannot leave metadata/admin heads without kind 39002. Propagate
200+
// any remaining failure after the notice commit; an idempotent caller retry
201+
// takes the duplicate path above and repairs discovery without reinserting
202+
// or resurfacing the notice.
203+
emit_group_discovery_events(tenant, state, dm_channel_id).await?;
204+
200205
Ok(())
201206
}
202207

@@ -546,6 +551,20 @@ mod tests {
546551
hidden_after_failure,
547552
"failed notice insertion must not expose stale moderation content"
548553
);
554+
let discovery_after_failure: i64 = sqlx::query_scalar(
555+
"SELECT COUNT(*) FROM events \
556+
WHERE community_id = $1 AND channel_id = $2 \
557+
AND kind IN (39000, 39001, 39002) AND deleted_at IS NULL",
558+
)
559+
.bind(tenant.community().as_uuid())
560+
.bind(preexisting_dm.id)
561+
.fetch_one(&pool)
562+
.await
563+
.expect("count discovery after failed notice");
564+
assert_eq!(
565+
discovery_after_failure, 0,
566+
"a rejected notice must not publish a partial discovery generation"
567+
);
549568
sqlx::raw_sql(
550569
"DROP TRIGGER fail_moderation_notice_insert ON events; \
551570
DROP FUNCTION fail_moderation_notice_insert();",
@@ -554,9 +573,72 @@ mod tests {
554573
.await
555574
.expect("remove notice failure injection");
556575

576+
sqlx::raw_sql(
577+
"CREATE FUNCTION fail_moderation_metadata_insert() RETURNS trigger \
578+
LANGUAGE plpgsql AS $$ BEGIN \
579+
IF NEW.kind = 39000 THEN RAISE EXCEPTION 'injected discovery failure'; END IF; \
580+
RETURN NEW; \
581+
END $$; \
582+
CREATE TRIGGER fail_moderation_metadata_insert \
583+
BEFORE INSERT ON events FOR EACH ROW \
584+
EXECUTE FUNCTION fail_moderation_metadata_insert();",
585+
)
586+
.execute(&pool)
587+
.await
588+
.expect("install discovery failure injection");
589+
send_moderation_notice(&tenant, &state, recipient_bytes.as_slice(), notice.clone())
590+
.await
591+
.expect_err("post-commit discovery failure must request a retry");
592+
let partial_discovery_kinds: Vec<i32> = sqlx::query_scalar(
593+
"SELECT kind FROM events \
594+
WHERE community_id = $1 AND channel_id = $2 \
595+
AND kind IN (39000, 39001, 39002) AND deleted_at IS NULL \
596+
ORDER BY kind",
597+
)
598+
.bind(tenant.community().as_uuid())
599+
.bind(preexisting_dm.id)
600+
.fetch_all(&pool)
601+
.await
602+
.expect("read roster-first discovery state");
603+
assert_eq!(
604+
partial_discovery_kinds,
605+
vec![39002],
606+
"metadata failure must not strand 39000/39001 without a roster"
607+
);
608+
let notice_count_after_discovery_failure: i64 = sqlx::query_scalar(
609+
"SELECT COUNT(*) FROM events \
610+
WHERE community_id = $1 AND channel_id = $2 AND kind = 9 \
611+
AND deleted_at IS NULL",
612+
)
613+
.bind(tenant.community().as_uuid())
614+
.bind(preexisting_dm.id)
615+
.fetch_one(&pool)
616+
.await
617+
.expect("count durable notice after discovery failure");
618+
assert_eq!(notice_count_after_discovery_failure, 1);
619+
sqlx::raw_sql(
620+
"DROP TRIGGER fail_moderation_metadata_insert ON events; \
621+
DROP FUNCTION fail_moderation_metadata_insert();",
622+
)
623+
.execute(&pool)
624+
.await
625+
.expect("remove discovery failure injection");
626+
557627
send_moderation_notice(&tenant, &state, recipient_bytes.as_slice(), notice.clone())
558628
.await
559-
.expect("deliver notice");
629+
.expect("duplicate retry repairs discovery");
630+
let discovery_kinds: Vec<i32> = sqlx::query_scalar(
631+
"SELECT kind FROM events \
632+
WHERE community_id = $1 AND channel_id = $2 \
633+
AND kind IN (39000, 39001, 39002) AND deleted_at IS NULL \
634+
ORDER BY kind",
635+
)
636+
.bind(tenant.community().as_uuid())
637+
.bind(preexisting_dm.id)
638+
.fetch_all(&pool)
639+
.await
640+
.expect("read converged moderation discovery");
641+
assert_eq!(discovery_kinds, vec![39000, 39001, 39002]);
560642

561643
let participant_hash = buzz_db::dm::compute_participant_hash(&[
562644
recipient_bytes.as_slice(),

‎crates/buzz-relay/src/handlers/side_effects.rs‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,11 +1112,25 @@ pub async fn emit_group_discovery_events(
11121112
channel_id: Uuid,
11131113
) -> anyhow::Result<()> {
11141114
let channel = state.db.get_channel(tenant.community(), channel_id).await?;
1115-
let members = state.db.get_members(tenant.community(), channel_id).await?;
1116-
11171115
let relay_pubkey_hex = hex::encode(state.relay_keypair.public_key().to_bytes());
11181116
let group_id = channel_id.to_string();
11191117

1118+
// Establish the authoritative roster first, from one membership snapshot
1119+
// held behind the writer lock. Metadata/admin discovery is emitted only
1120+
// after that transaction commits, so a roster validation failure cannot
1121+
// strand a partial 39000/39001 generation without kind 39002. Keeping the
1122+
// captured members for all three events also prevents cross-query drift.
1123+
let relay_pubkey = state.relay_keypair.public_key().to_bytes();
1124+
let mut member_snapshot = state
1125+
.db
1126+
.lock_member_snapshot(tenant.community(), channel_id, &relay_pubkey)
1127+
.await?;
1128+
let members = member_snapshot.members.clone();
1129+
let stored_members =
1130+
store_group_members_event(tenant, state, channel_id, &mut member_snapshot).await?;
1131+
member_snapshot.release().await?;
1132+
dispatch_group_members_event(tenant, state, stored_members, &relay_pubkey_hex).await;
1133+
11201134
{
11211135
let mut tags: Vec<Tag> = vec![Tag::parse(["d", &group_id])?];
11221136
tags.push(Tag::parse(["name", &channel.name])?);
@@ -1200,19 +1214,6 @@ pub async fn emit_group_discovery_events(
12001214
.await?;
12011215
}
12021216

1203-
// Re-capture membership behind the writer lock immediately before the
1204-
// authoritative 39002 replacement. Metadata/admin snapshots retain their
1205-
// existing behavior; only membership publication needs this freshness fence.
1206-
let relay_pubkey = state.relay_keypair.public_key().to_bytes();
1207-
let mut member_snapshot = state
1208-
.db
1209-
.lock_member_snapshot(tenant.community(), channel_id, &relay_pubkey)
1210-
.await?;
1211-
let stored_members =
1212-
store_group_members_event(tenant, state, channel_id, &mut member_snapshot).await?;
1213-
member_snapshot.release().await?;
1214-
dispatch_group_members_event(tenant, state, stored_members, &relay_pubkey_hex).await;
1215-
12161217
Ok(())
12171218
}
12181219

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
4040
void Function()? _unsubscribeDmVisibility;
4141
String? _dmVisibilityRelayBaseUrl;
4242
String? _dmVisibilityPubkey;
43+
String? _latestDmVisibilityEventId;
4344
Future<void> _liveSubscriptionQueue = Future.value();
4445
List<Channel> _desiredLiveChannels = const [];
4546
Set<String> _desiredLiveChannelIds = const {};
@@ -83,6 +84,7 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
8384
_memberSnapshotRelayBaseUrl = relayBaseUrl;
8485
_memberSnapshotPubkey = pubkey;
8586
_memberSnapshotsByChannelId = const {};
87+
_latestDmVisibilityEventId = null;
8688
}
8789
final connected = Completer<void>();
8890
final sessionState = ref.read(relaySessionProvider);
@@ -464,13 +466,17 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
464466
) async {
465467
try {
466468
final events = await session.fetchHistory(NostrFilters.hiddenDms(myPk));
467-
if (events.isEmpty) return const {};
469+
if (events.isEmpty) {
470+
_latestDmVisibilityEventId = null;
471+
return const {};
472+
}
468473
NostrEvent latest = events.first;
469474
for (final event in events.skip(1)) {
470475
if (event.createdAt > latest.createdAt) {
471476
latest = event;
472477
}
473478
}
479+
_latestDmVisibilityEventId = latest.id;
474480
return {
475481
for (final tag in latest.tags)
476482
if (tag.length >= 2 && tag[0] == 'h') tag[1],
@@ -601,7 +607,9 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
601607
tags: {
602608
'#p': [myPk],
603609
},
604-
limit: 0,
610+
// Replay the latest snapshot before EOSE so a publication between
611+
// the history fetch and this subscription cannot be missed.
612+
limit: 1,
605613
),
606614
_handleDmVisibilityEvent,
607615
);
@@ -808,6 +816,8 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
808816

809817
void _handleDmVisibilityEvent(NostrEvent event) {
810818
if (event.kind != EventKind.dmVisibility) return;
819+
if (_latestDmVisibilityEventId == event.id) return;
820+
_latestDmVisibilityEventId = event.id;
811821
unawaited(refresh());
812822
}
813823

@@ -949,6 +959,7 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
949959
_unsubscribeDmVisibility = null;
950960
_dmVisibilityRelayBaseUrl = null;
951961
_dmVisibilityPubkey = null;
962+
_latestDmVisibilityEventId = null;
952963
_backstopTimer?.cancel();
953964
_backstopTimer = null;
954965
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ void main() {
466466
);
467467
expect(session.dmVisibilitySubscribeFilters, hasLength(1));
468468
expect(session.dmVisibilitySubscribeFilters.single.tags['#p'], [myPk]);
469+
expect(session.dmVisibilitySubscribeFilters.single.limit, 1);
469470

470471
final visibleSnapshot = _hiddenDms(const [], pubkey: myPk);
471472
session.hiddenDmEvents = [visibleSnapshot];
@@ -485,6 +486,45 @@ void main() {
485486
expect(session.dmVisibilitySubscribeFilters, hasLength(1));
486487
});
487488

489+
test(
490+
'visibility subscription replays a snapshot published during setup',
491+
() async {
492+
final session = _FakeRelaySession(
493+
memberships: [
494+
_membership(_channelA, myPk),
495+
_membership(_channelB, myPk),
496+
],
497+
metadata: [
498+
_meta(id: _channelA, name: 'Alice', channelType: 'dm'),
499+
_meta(id: _channelB, name: 'Bob', channelType: 'dm'),
500+
],
501+
hiddenDmEvents: [
502+
_hiddenDms([_channelA], pubkey: myPk),
503+
],
504+
)..pauseNextSubscribe();
505+
final container = _buildContainer(session: session);
506+
addTearDown(container.dispose);
507+
508+
final initialLoad = container.read(channelsProvider.future);
509+
await session.nextSubscribeStarted;
510+
session.hiddenDmEvents = [_hiddenDms(const [], pubkey: myPk)];
511+
session.resumePausedSubscribe();
512+
513+
await initialLoad;
514+
await _waitUntil(
515+
() =>
516+
container
517+
.read(channelsProvider)
518+
.value
519+
?.map((channel) => channel.id)
520+
.toSet()
521+
.containsAll({_channelA, _channelB}) ==
522+
true,
523+
);
524+
expect(session.activeChannels, {_channelA, _channelB});
525+
},
526+
);
527+
488528
test(
489529
'archived kind:39000 metadata sets Channel.isArchived (covers TTL auto-archive)',
490530
() async {
@@ -926,6 +966,11 @@ class _FakeRelaySession extends RelaySessionNotifier {
926966
}
927967
final subscriptionKey = ++_nextSubscriptionKey;
928968
_subscriptions[subscriptionKey] = (filter, onEvent);
969+
if (filter.kinds.contains(EventKind.dmVisibility) && filter.limit > 0) {
970+
for (final event in hiddenDmEvents.take(filter.limit)) {
971+
onEvent(event);
972+
}
973+
}
929974
return () {
930975
final subscription = _subscriptions.remove(subscriptionKey);
931976
if (subscription == null) return;

0 commit comments

Comments
 (0)