mirror of
https://github.com/block/buzz.git
synced 2026-11-27 03:31:50 +00:00
Two fifth-round review findings on the Projects agent send path.
Cross-tenant in-flight sends (P1): the submit flow suspends across
managed-agent startup, DM open, and the send itself. A community switch
during any suspension does not cancel the callback — remounting only
removes the UI — so a stale callback could open a DM or publish the
captured tenant's content on the NEW tenant's relay. Fix: the caller
captures the community relay scope before its first await and passes it
as expected_relay_url through open_dm and send_channel_message. Each
command resolves its relay base exactly once, asserts the captured scope
against it (assert_expected_relay_scope, ws(s)→http(s) normalized), and
uses that same base for every side effect — thread-ref read, submit, and
metadata read. A mismatch fails closed ("active community changed before
the message was submitted; not sent") rather than publishing to the
wrong tenant. Absent scope preserves unscoped behavior for callers with
no tenant boundary. submit_event_with_created_at becomes
submit_event_at_created_at (explicit base) so the scope-checked base is
the one used at submit time — re-resolving there would reopen the race.
Same-second follow-up hiding (P2 of the pair): follow-up sends carried
no parentEventId, so a follow-up signed within the opener's second got a
random event id and roughly half sorted on the rejected side of the
same-second id tiebreak in isAtOrAfterConversationOpener — an immediate
follow-up could vanish. Fix: follow-ups now reply to the opener
(parentEventId = opener.eventId); the comparator always admits causal
e-tag replies, so visibility no longer depends on id luck.
Both submit handlers (ProjectAgentChatPanel, ProjectsAgentPromptPage)
route through a new pure orchestration function,
submitProjectAgentMessage, which never re-reads the scope after capture
and is unit-tested without React: switch-during-startup publishes
nothing to either tenant, switch-during-DM-open fails closed, the
captured scope rides every relay side effect, and follow-ups reference
the opener. The e2e bridge mirrors the backend check after its injected
delays so specs can drive a mid-flight community switch
deterministically.
File-size ratchet keeps its discipline: the scope check lives in
relay/scope.rs, resolve_thread_ref moves to
commands/messages/thread_ref.rs, sendChannelMessage moves to
shared/api/tauriMessages.ts (re-exported from tauri.ts), and OpenDmInput
moves next to openDm in tauriChannels.ts.
Co-authored-by: Thomas Petersen <thomasp@squareup.com>
Signed-off-by: Thomas Petersen <thomasp@squareup.com>
59 lines
1.8 KiB
Rust
59 lines
1.8 KiB
Rust
use nostr::EventId;
|
|
|
|
use crate::{app_state::AppState, events, relay::query_relay_at};
|
|
|
|
/// Fetch a parent event and extract the thread root from its NIP-10 e-tags.
|
|
///
|
|
/// Reads through the explicit `api_base_url` the calling command resolved —
|
|
/// never re-resolving the workspace override — so a mid-command community
|
|
/// switch cannot split one logical send across two relays.
|
|
pub(super) async fn resolve_thread_ref(
|
|
parent_event_id: &str,
|
|
state: &AppState,
|
|
api_base_url: &str,
|
|
) -> Result<events::ThreadRef, String> {
|
|
let parent_eid =
|
|
EventId::from_hex(parent_event_id).map_err(|e| format!("invalid parent event ID: {e}"))?;
|
|
|
|
let evs = query_relay_at(
|
|
state,
|
|
api_base_url,
|
|
&[serde_json::json!({
|
|
"ids": [parent_event_id],
|
|
"kinds": [9, 40002, 45001, 45003, buzz_core_pkg::kind::KIND_HUDDLE_STARTED],
|
|
"limit": 1
|
|
})],
|
|
)
|
|
.await?;
|
|
|
|
let parent = evs
|
|
.first()
|
|
.ok_or_else(|| "parent event not found".to_string())?;
|
|
|
|
// Walk tags looking for NIP-10 root/reply markers.
|
|
let (mut root, mut reply) = (None, None);
|
|
for tag in parent.tags.iter() {
|
|
let s = tag.as_slice();
|
|
if s.len() >= 4 && s[0] == "e" {
|
|
match s[3].as_str() {
|
|
"root" => root = Some(s[1].clone()),
|
|
"reply" => reply = Some(s[1].clone()),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
let root_hex = root.or(reply);
|
|
|
|
let root_eid = match root_hex {
|
|
Some(hex) if hex != parent_event_id => {
|
|
EventId::from_hex(&hex).map_err(|e| format!("invalid root event ID: {e}"))?
|
|
}
|
|
_ => parent_eid,
|
|
};
|
|
|
|
Ok(events::ThreadRef {
|
|
root_event_id: root_eid,
|
|
parent_event_id: parent_eid,
|
|
})
|
|
}
|