Skip to content

Commit f8ebee0

Browse files
loganjLarryclaude
committed
Drop the one-shot member/bot roster reconcile scan
Old rosters keep their stored label until the channel's next membership change republishes them. Both labels grant the same permissions, and the Huddle clients (#8544, #8545) recognize agents from managed-agent records, so a stale label needs no operator step. This removes BUZZ_RECONCILE_BOT_ROSTERS, its scan query and the env-flag rename. 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 a4311bb commit f8ebee0

4 files changed

Lines changed: 21 additions & 212 deletions

File tree

‎crates/buzz-db/src/store/channel_members.rs‎

Lines changed: 2 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,117 +1700,6 @@ pub async fn list_large_channel_rosters_needing_reconciliation(
17001700
.collect()
17011701
}
17021702

1703-
/// A channel whose live relay-authored kind:39002 roster shows an active
1704-
/// ordinary member with the other label (`member` instead of `bot`, or the
1705-
/// reverse) than [`member_role_label`] gives.
1706-
#[derive(Debug, Clone, PartialEq, Eq)]
1707-
pub struct StaleRoleRoster {
1708-
/// Community that owns the channel.
1709-
pub community_id: CommunityId,
1710-
/// Canonical host for the owning community.
1711-
pub host: String,
1712-
/// Channel whose roster must be republished.
1713-
pub channel_id: Uuid,
1714-
}
1715-
1716-
/// Returns active channels whose live roster shows an ordinary member with
1717-
/// the wrong `member`/`bot` label.
1718-
///
1719-
/// Such a roster exists when it was published before this release (older
1720-
/// relays published the stored role), or when a relay stopped between
1721-
/// recording an agent owner and republishing that agent's rosters. Only two
1722-
/// kinds of channel can disagree, so only they are read: channels with an
1723-
/// active ordinary member that has a recorded agent owner, and channels whose
1724-
/// roster names someone `bot` (found through the `tags` GIN index). Only
1725-
/// active communities are scanned, as in
1726-
/// [`list_large_channel_rosters_needing_reconciliation`].
1727-
///
1728-
/// This is an internal cross-community maintenance read for the one-shot
1729-
/// `BUZZ_RECONCILE_BOT_ROSTERS` startup step. Callers must preserve the
1730-
/// returned community id when rewriting discovery state.
1731-
pub async fn list_channel_rosters_with_stale_ordinary_roles(
1732-
pool: &PgPool,
1733-
relay_pubkey: &[u8],
1734-
) -> Result<Vec<StaleRoleRoster>> {
1735-
let mut connection = crate::observability::acquire_writer(
1736-
pool,
1737-
crate::observability::WriterOperation::Maintenance,
1738-
)
1739-
.await?;
1740-
let rows = sqlx::query(concat!(
1741-
r#"
1742-
WITH candidates AS (
1743-
SELECT cm.community_id, cm.channel_id
1744-
FROM channel_members cm
1745-
JOIN users u
1746-
ON u.community_id = cm.community_id
1747-
AND u.pubkey = cm.pubkey
1748-
AND u.agent_owner_pubkey IS NOT NULL
1749-
WHERE cm.role IN ('member', 'bot') AND cm.removed_at IS NULL
1750-
UNION
1751-
SELECT roster.community_id, roster.channel_id
1752-
FROM events roster
1753-
WHERE roster.kind = 39002
1754-
AND roster.pubkey = $1
1755-
AND roster.deleted_at IS NULL
1756-
AND roster.channel_id IS NOT NULL
1757-
AND roster.tags @> '[["bot"]]'::jsonb
1758-
)
1759-
SELECT candidate.community_id, community.host, candidate.channel_id
1760-
FROM candidates candidate
1761-
JOIN channels ch
1762-
ON ch.community_id = candidate.community_id
1763-
AND ch.id = candidate.channel_id
1764-
AND ch.deleted_at IS NULL
1765-
JOIN communities community
1766-
ON community.id = candidate.community_id
1767-
AND community.archived_at IS NULL
1768-
AND community.deleted_at IS NULL
1769-
AND community.deletion_state = 'active'
1770-
JOIN LATERAL (
1771-
SELECT roster.tags
1772-
FROM events roster
1773-
WHERE roster.community_id = candidate.community_id
1774-
AND roster.channel_id = candidate.channel_id
1775-
AND roster.kind = 39002
1776-
AND roster.pubkey = $1
1777-
AND roster.deleted_at IS NULL
1778-
ORDER BY roster.created_at DESC, roster.id ASC
1779-
LIMIT 1
1780-
) live_roster ON true
1781-
WHERE EXISTS (
1782-
SELECT 1
1783-
FROM channel_members cm
1784-
JOIN jsonb_array_elements(live_roster.tags) tag
1785-
ON tag->>0 = 'p'
1786-
AND tag->>1 = encode(cm.pubkey, 'hex')
1787-
WHERE cm.community_id = candidate.community_id
1788-
AND cm.channel_id = candidate.channel_id
1789-
AND cm.removed_at IS NULL
1790-
AND cm.role IN ('member', 'bot')
1791-
AND tag->>3 IN ('member', 'bot')
1792-
AND tag->>3 <> "#,
1793-
member_role_label!(),
1794-
r#"
1795-
)
1796-
ORDER BY candidate.community_id, candidate.channel_id
1797-
"#
1798-
))
1799-
.bind(relay_pubkey)
1800-
.fetch_all(&mut *connection)
1801-
.await?;
1802-
1803-
rows.into_iter()
1804-
.map(|row| {
1805-
Ok(StaleRoleRoster {
1806-
community_id: CommunityId::from_uuid(row.try_get("community_id")?),
1807-
host: row.try_get("host")?,
1808-
channel_id: row.try_get("channel_id")?,
1809-
})
1810-
})
1811-
.collect()
1812-
}
1813-
18141703
/// The channels where `pubkey` is an active ordinary member (`member` or
18151704
/// `bot`). Recording an agent owner changes the label these rows show, so
18161705
/// their rosters are republished.
@@ -2484,19 +2373,6 @@ impl Db {
24842373
.await
24852374
}
24862375

2487-
/// Returns active-channel rosters that show an ordinary member with the
2488-
/// wrong `member`/`bot` label.
2489-
#[datastore_span(
2490-
name = "list_channel_rosters_with_stale_ordinary_roles",
2491-
system = "postgresql"
2492-
)]
2493-
pub async fn list_channel_rosters_with_stale_ordinary_roles(
2494-
&self,
2495-
relay_pubkey: &[u8],
2496-
) -> Result<Vec<StaleRoleRoster>> {
2497-
list_channel_rosters_with_stale_ordinary_roles(&self.pool, relay_pubkey).await
2498-
}
2499-
25002376
/// Returns the channels where `pubkey` is an active ordinary member.
25012377
#[datastore_span(name = "list_ordinary_member_channels", system = "postgresql")]
25022378
pub async fn list_ordinary_member_channels(
@@ -5609,11 +5485,9 @@ mod postgres_tests {
56095485
/// The roster check treats `member` and `bot` as one role, so an owner
56105486
/// recorded between roster capture and insert does not refuse the
56115487
/// publication; the background republish then replaces the stale label.
5612-
/// The one-shot scan reports a roster with a stale label until then.
56135488
///
5614-
/// Mutations: compare labels exactly in `validate_channel_roster_snapshot`
5615-
/// → the captured roster is refused → RED; compare stored roles in the
5616-
/// scan → the stale roster is not reported → RED.
5489+
/// Mutation: compare labels exactly in `validate_channel_roster_snapshot`
5490+
/// → the captured roster is refused → RED.
56175491
#[tokio::test]
56185492
#[ignore = "requires Postgres"]
56195493
async fn owner_record_during_roster_publication_converges() {
@@ -5667,13 +5541,6 @@ mod postgres_tests {
56675541
.await
56685542
.expect("read live roster")
56695543
};
5670-
let stale = || async {
5671-
list_channel_rosters_with_stale_ordinary_roles(&pool, &relay_pubkey)
5672-
.await
5673-
.expect("list stale rosters")
5674-
.iter()
5675-
.any(|c| c.channel_id == channel)
5676-
};
56775544
let at = nostr::Timestamp::now().as_secs();
56785545

56795546
let mut snapshot = db
@@ -5699,7 +5566,6 @@ mod postgres_tests {
56995566
);
57005567
snapshot.release().await.expect("commit captured roster");
57015568
assert_eq!(agent_label(&live_tags().await).as_deref(), Some("member"));
5702-
assert!(stale().await, "the scan must report the stale label");
57035569

57045570
// The background republish after the owner record.
57055571
let mut snapshot = db
@@ -5713,7 +5579,6 @@ mod postgres_tests {
57135579
.expect("republish");
57145580
snapshot.release().await.expect("commit republish");
57155581
assert_eq!(agent_label(&live_tags().await).as_deref(), Some("bot"));
5716-
assert!(!stale().await);
57175582
}
57185583

57195584
/// The republish after an owner record lists the agent's active ordinary

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

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,36 +3793,6 @@ pub async fn reconcile_large_channel_member_snapshots(
37933793
Ok(summary)
37943794
}
37953795

3796-
/// Republish rosters that show an ordinary member with the wrong
3797-
/// `member`/`bot` label: rosters published by relays from before the label
3798-
/// was computed, or left stale when a relay stopped between recording an
3799-
/// agent owner and republishing that agent's rosters.
3800-
///
3801-
/// A one-shot operator step, run at startup only when
3802-
/// `BUZZ_RECONCILE_BOT_ROSTERS` is set. Converged deployments find no
3803-
/// candidates, so it only reads.
3804-
pub async fn reconcile_ordinary_role_rosters(
3805-
state: &Arc<AppState>,
3806-
) -> anyhow::Result<ReconcileSummary> {
3807-
let relay_pubkey = state.relay_keypair.public_key();
3808-
let candidates = state
3809-
.db
3810-
.list_channel_rosters_with_stale_ordinary_roles(&relay_pubkey.to_bytes())
3811-
.await?;
3812-
let summary = republish_roster_candidates(
3813-
state,
3814-
candidates
3815-
.into_iter()
3816-
.map(|candidate| (candidate.community_id, candidate.host, candidate.channel_id))
3817-
.collect(),
3818-
"ordinary role roster reconciliation failed",
3819-
)
3820-
.await;
3821-
metrics::counter!("buzz_channel_role_roster_reconciliations_total")
3822-
.increment(summary.repaired as u64);
3823-
Ok(summary)
3824-
}
3825-
38263796
/// Republish each `(community, host, channel)` roster under its membership
38273797
/// lock, counting failures instead of stopping. Each roster first takes a
38283798
/// relay-wide republish permit, so concurrent callers share a small bound on
@@ -3886,8 +3856,7 @@ async fn republish_roster_candidates(
38863856
/// wait: first AUTH must finish inside its own deadline. A failure is logged
38873857
/// and counted and does not stop the other channels. A roster left stale (for
38883858
/// example by a relay that stopped first) shows only the wrong label, which
3889-
/// has the same permissions; the next membership change or a
3890-
/// `BUZZ_RECONCILE_BOT_ROSTERS` startup repairs it.
3859+
/// has the same permissions; the next membership change repairs it.
38913860
pub fn spawn_agent_roster_republish(tenant: &TenantContext, state: Arc<AppState>, agent: Vec<u8>) {
38923861
let (community_id, host) = (tenant.community(), tenant.host().to_owned());
38933862
tokio::spawn(async move {

‎crates/buzz-relay/src/main.rs‎

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ use buzz_relay::telemetry;
3030
use buzz_workflow::WorkflowEngine;
3131
use tokio_util::sync::CancellationToken;
3232

33-
/// Whether an opt-in boolean environment flag is on: `true`, `1`, `yes` or
34-
/// `on`, in any case. Unset, empty and every other value mean off.
35-
fn env_flag_enabled(value: Option<&str>) -> bool {
33+
fn buzz_auto_migrate_enabled(value: Option<&str>) -> bool {
3634
value.map(str::trim).is_some_and(|value| {
3735
matches!(
3836
value.to_ascii_lowercase().as_str(),
@@ -547,7 +545,8 @@ async fn run_relay_main(boot: BootTracker) -> anyhow::Result<()> {
547545
info!("Postgres connected");
548546
}
549547

550-
let auto_migrate = env_flag_enabled(std::env::var("BUZZ_AUTO_MIGRATE").ok().as_deref());
548+
let auto_migrate =
549+
buzz_auto_migrate_enabled(std::env::var("BUZZ_AUTO_MIGRATE").ok().as_deref());
551550
if auto_migrate {
552551
let step = StepTimer::start(StartupStep::DbMigrate);
553552
db.migrate().await.map_err(|e| {
@@ -1150,30 +1149,6 @@ async fn run_relay_main(boot: BootTracker) -> anyhow::Result<()> {
11501149
}
11511150
step.finish();
11521151

1153-
// One-shot operator step after upgrading to computed `member`/`bot`
1154-
// labels: republish rosters that older relays published with the stored
1155-
// label. Off by default so no replica scans at every startup; both labels
1156-
// have the same permissions, so a stale one is only cosmetic until then.
1157-
if env_flag_enabled(std::env::var("BUZZ_RECONCILE_BOT_ROSTERS").ok().as_deref()) {
1158-
let role_roster_state = Arc::clone(&state);
1159-
tokio::spawn(async move {
1160-
match buzz_relay::handlers::side_effects::reconcile_ordinary_role_rosters(
1161-
&role_roster_state,
1162-
)
1163-
.await
1164-
{
1165-
Ok(summary) => info!(
1166-
count = summary.repaired,
1167-
failed = summary.failed,
1168-
"member/bot channel rosters reconciled"
1169-
),
1170-
Err(error) => {
1171-
tracing::warn!(%error, "member/bot channel roster reconciliation failed")
1172-
}
1173-
}
1174-
});
1175-
}
1176-
11771152
// Emit kind:39000/39002 discovery events for channels that exist in the DB
11781153
// but don't have corresponding events (e.g. seeded via direct SQL inserts).
11791154
// Only runs when BUZZ_RECONCILE_CHANNELS=true (dev/CI environments).
@@ -3138,8 +3113,8 @@ mod tests {
31383113
use uuid::Uuid;
31393114

31403115
use super::{
3141-
apply_activity_refresh, apply_stock_refresh, connect_audit_pool, counted_partition_audit,
3142-
dropped_in_memory_keys, emit_cached_fleet_usage_metrics, env_flag_enabled,
3116+
apply_activity_refresh, apply_stock_refresh, buzz_auto_migrate_enabled, connect_audit_pool,
3117+
counted_partition_audit, dropped_in_memory_keys, emit_cached_fleet_usage_metrics,
31433118
idle_timeout_secs, jwks_next_retry_after_failed_refresh, nip_fi_jwks_refresh_loop,
31443119
record_legacy_usage_collection, refresh_due_fleet_usage,
31453120
refresh_legacy_active_gauge_recency, relay_keypair_from_config,
@@ -3508,18 +3483,18 @@ mod tests {
35083483
}
35093484

35103485
#[test]
3511-
fn env_flags_are_opt_in() {
3512-
assert!(!env_flag_enabled(None));
3513-
assert!(!env_flag_enabled(Some("")));
3514-
assert!(!env_flag_enabled(Some("false")));
3515-
assert!(!env_flag_enabled(Some("0")));
3516-
assert!(!env_flag_enabled(Some("no")));
3517-
3518-
assert!(env_flag_enabled(Some("true")));
3519-
assert!(env_flag_enabled(Some("TRUE")));
3520-
assert!(env_flag_enabled(Some(" 1 ")));
3521-
assert!(env_flag_enabled(Some("yes")));
3522-
assert!(env_flag_enabled(Some("on")));
3486+
fn buzz_auto_migrate_is_opt_in() {
3487+
assert!(!buzz_auto_migrate_enabled(None));
3488+
assert!(!buzz_auto_migrate_enabled(Some("")));
3489+
assert!(!buzz_auto_migrate_enabled(Some("false")));
3490+
assert!(!buzz_auto_migrate_enabled(Some("0")));
3491+
assert!(!buzz_auto_migrate_enabled(Some("no")));
3492+
3493+
assert!(buzz_auto_migrate_enabled(Some("true")));
3494+
assert!(buzz_auto_migrate_enabled(Some("TRUE")));
3495+
assert!(buzz_auto_migrate_enabled(Some(" 1 ")));
3496+
assert!(buzz_auto_migrate_enabled(Some("yes")));
3497+
assert!(buzz_auto_migrate_enabled(Some("on")));
35233498
}
35243499

35253500
#[test]

‎deploy/charts/buzz/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ default so long-lived WebSocket connections have time to drain.
616616

617617
Schema migrations are embedded in the relay binary via `sqlx::migrate!` and run at startup, gated by `BUZZ_AUTO_MIGRATE` (default `true`). Multiple replicas race-safely behind a Postgres advisory lock. `helm upgrade` is the entire upgrade procedure.
618618

619-
Migration 0032 is a hard compatibility boundary for relay versions that publish repaired channel rosters. Older relays verify its roster-fence trigger before opening listeners and refuse to start if the trigger is missing or inert. Newer relays run the same check in every roster writer and do not require the trigger. Migration 0074 drops the trigger, so every relay must run a version without the startup check (#8611 or later) before 0074 is applied; an older relay then refuses to start. Relays from the 0074 release on show a computed `member`/`bot` label in rosters that the trigger would refuse, so they refuse to start while the trigger is installed. Apply 0074 before rolling them out. After that rollout, start one relay once with `BUZZ_RECONCILE_BOT_ROSTERS=1` (`true`, `yes` and `on` also work; any other value leaves it off) to republish rosters that older relays published with the stored label. Confirm that it logs `member/bot channel rosters reconciled`; a warning instead means the repair did not run, so start it again. Apply migrations before rolling the relay; for large installations, prefer a controlled `buzz-admin migrate` job with PostgreSQL lock monitoring before the code rollout.
619+
Migration 0032 is a hard compatibility boundary for relay versions that publish repaired channel rosters. Older relays verify its roster-fence trigger before opening listeners and refuse to start if the trigger is missing or inert. Newer relays run the same check in every roster writer and do not require the trigger. Migration 0074 drops the trigger, so every relay must run a version without the startup check (#8611 or later) before 0074 is applied; an older relay then refuses to start. Relays from the 0074 release on show a computed `member`/`bot` label in rosters that the trigger would refuse, so they refuse to start while the trigger is installed. Apply 0074 before rolling them out. A roster that an older relay published keeps its stored label until the channel's next membership change republishes it. Both labels have the same permissions. Apply migrations before rolling the relay; for large installations, prefer a controlled `buzz-admin migrate` job with PostgreSQL lock monitoring before the code rollout.
620620

621621
If you prefer decoupling migrations from serving, set `migrate.autoMigrate=false`. **In that mode the chart does not run migrations for you** — you own running `buzz-admin migrate` (separate Pod / one-shot Job) against the database before every `helm install` / `helm upgrade`. Readiness probes only verify DB connectivity, not schema freshness, so a pod will appear healthy against an unmigrated schema and fail under load. A pre-upgrade Helm Job for this is on the chart roadmap; the values knob `migrate.preUpgradeJob.enabled` is reserved.
622622

0 commit comments

Comments
 (0)