@@ -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 ( ) ,
0 commit comments