Skip to content

Commit 35fc447

Browse files
loganjLarryclaude
committed
Republish an agent's rosters when an invite claim records its owner
Since #8237, a v1 or v2 invite claim can record the claimant's agent owner. The claim disconnected the agent's unowned sockets but did not republish its rosters, so an agent already in channels kept showing `member` there until the next membership change. A later AUTH finds the owner already recorded and does not republish either. Both claim formats now do what first AUTH does after the claim commits: disconnect the unowned sockets and start the background roster republish. 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 62290cf commit 35fc447

2 files changed

Lines changed: 134 additions & 2 deletions

File tree

‎crates/buzz-relay/src/api/invites.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,23 @@ async fn mint_invite_inner(
413413
})))
414414
}
415415

416+
/// A claim just recorded the claimant's agent owner, after its transaction
417+
/// committed. Do what first AUTH does when it records an owner: make sockets
418+
/// opened without the owner reconnect with it, and republish the claimant's
419+
/// rosters in the background, because rosters show `bot` for an owned agent.
420+
fn owner_recorded_by_claim(
421+
state: &Arc<AppState>,
422+
tenant: &buzz_core::TenantContext,
423+
claimant: &nostr::PublicKey,
424+
) {
425+
state.disconnect_unowned_agent_clusterwide(tenant, claimant.as_bytes());
426+
crate::handlers::side_effects::spawn_agent_roster_republish(
427+
tenant,
428+
Arc::clone(state),
429+
claimant.to_bytes().to_vec(),
430+
);
431+
}
432+
416433
/// Claim an invite code — `POST /api/invites/claim`, NIP-98 signed by the
417434
/// *joining* pubkey. Exempt from the relay-membership gate by design.
418435
///
@@ -480,7 +497,7 @@ pub async fn claim_invite(
480497
metrics::counter!("buzz_users_created_total", "community" => tenant.host().to_owned())
481498
.increment(write.outcome.users_created);
482499
if write.outcome.owner_link_created {
483-
state.disconnect_unowned_agent_clusterwide(&tenant, pubkey.as_bytes());
500+
owner_recorded_by_claim(&state, &tenant, &pubkey);
484501
}
485502
fan_out_nip43_membership_list(&tenant, &state, write.snapshot).await;
486503
return match write.outcome.outcome {
@@ -575,7 +592,7 @@ pub async fn claim_invite(
575592
metrics::counter!("buzz_users_created_total", "community" => tenant.host().to_owned())
576593
.increment(write.outcome.users_created);
577594
if write.outcome.owner_link_created {
578-
state.disconnect_unowned_agent_clusterwide(&tenant, pubkey.as_bytes());
595+
owner_recorded_by_claim(&state, &tenant, &pubkey);
579596
}
580597
let was_inserted = write.outcome.inserted;
581598
fan_out_nip43_membership_list(&tenant, &state, write.snapshot).await;

‎crates/buzz-relay/src/api/invites/postgres_tests/owner_admission_tests.rs‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,119 @@ mod postgres_tests {
240240
assert_eq!(users(&state, community).await, before);
241241
}
242242
}
243+
244+
/// A claim that records the agent's owner makes its ordinary rows show
245+
/// `bot`, so the rosters of channels it is already in must be republished,
246+
/// as on first AUTH. No membership change happens in those channels.
247+
///
248+
/// Mutation: remove the republish from `owner_recorded_by_claim`, or call
249+
/// that helper from only one claim format → that format's rosters keep
250+
/// `member` → RED.
251+
#[tokio::test]
252+
#[ignore = "requires Postgres"]
253+
async fn owner_recorded_by_v1_and_v2_claims_republishes_the_agents_rosters() {
254+
use buzz_core::channel::{ChannelType, ChannelVisibility, MemberRole};
255+
256+
let (state, host, community, issuer) = fixture().await;
257+
let v2 = mint_code(
258+
state.clone(),
259+
&host,
260+
&issuer,
261+
serde_json::json!({"max_uses":1}),
262+
)
263+
.await;
264+
let key = derive_invite_key(&state.relay_keypair);
265+
let (v1, _) = crate::invite_token::mint_invite(&key, community, 3600);
266+
for code in [v1, v2] {
267+
let (owner, agent) = (Keys::generate(), Keys::generate());
268+
let agent_bytes = agent.public_key().to_bytes();
269+
state
270+
.db
271+
.ensure_user(community, &agent_bytes)
272+
.await
273+
.expect("seed agent user");
274+
let mut channels = Vec::new();
275+
for name in ["claim-roster-a", "claim-roster-b"] {
276+
let channel = state
277+
.db
278+
.create_channel(
279+
community,
280+
name,
281+
ChannelType::Stream,
282+
ChannelVisibility::Open,
283+
None,
284+
&issuer.public_key().to_bytes(),
285+
None,
286+
)
287+
.await
288+
.expect("create channel")
289+
.id;
290+
state
291+
.db
292+
.add_member(
293+
community,
294+
channel,
295+
&agent_bytes,
296+
MemberRole::Member,
297+
Some(&issuer.public_key().to_bytes()),
298+
)
299+
.await
300+
.expect("add unowned agent");
301+
channels.push(channel);
302+
}
303+
304+
let tag = buzz_sdk::nip_oa::compute_auth_tag(&owner, &agent.public_key(), "").unwrap();
305+
let response = post_with_owner_tag(
306+
state.clone(),
307+
&host,
308+
"/api/invites/claim",
309+
&agent,
310+
serde_json::json!({"code":code}).to_string(),
311+
&tag,
312+
)
313+
.await;
314+
assert_eq!(response.status(), StatusCode::OK);
315+
assert!(state
316+
.db
317+
.is_agent_owner(
318+
community,
319+
agent.public_key().as_bytes(),
320+
owner.public_key().as_bytes()
321+
)
322+
.await
323+
.unwrap());
324+
325+
let agent_hex = agent.public_key().to_hex();
326+
let deadline = std::time::Instant::now() + Duration::from_secs(10);
327+
for channel in channels {
328+
loop {
329+
let live: Option<serde_json::Value> = sqlx::query_scalar(
330+
"SELECT tags FROM events WHERE community_id = $1 AND channel_id = $2 \
331+
AND kind = 39002 AND deleted_at IS NULL",
332+
)
333+
.bind(community.as_uuid())
334+
.bind(channel)
335+
.fetch_optional(state.db.pool())
336+
.await
337+
.expect("read live roster");
338+
let shows_bot =
339+
live.as_ref()
340+
.and_then(|tags| tags.as_array())
341+
.is_some_and(|tags| {
342+
tags.iter().any(|tag| {
343+
tag[0] == "p" && tag[1] == agent_hex.as_str() && tag[3] == "bot"
344+
})
345+
});
346+
if shows_bot {
347+
break;
348+
}
349+
assert!(
350+
std::time::Instant::now() < deadline,
351+
"code {code}: the roster must show the claimant as bot: {live:?}"
352+
);
353+
tokio::time::sleep(Duration::from_millis(50)).await;
354+
}
355+
}
356+
}
357+
}
243358
}

0 commit comments

Comments
 (0)