Skip to content

Commit 8688c04

Browse files
wesbillmanPinky
andauthored
Rework first-launch community onboarding (#1936)
Signed-off-by: Wes <[email protected]> Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co>
1 parent e1865cb commit 8688c04

24 files changed

Lines changed: 1624 additions & 1091 deletions

‎Justfile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,28 @@ dev *ARGS: bootstrap _ensure-sidecar-stubs _ensure-migrations
412412
FEATURES=(); [[ -n "{{mesh}}" ]] && FEATURES=(--features mesh-llm)
413413
pnpm exec tauri dev ${FEATURES[@]+"${FEATURES[@]}"} --config "$BUZZ_TAURI_CONFIG" {{ARGS}}
414414
415+
# Run only the desktop app. No relay, database, Docker, migrations, or .env are needed.
416+
# The app opens normally and asks for a community before making a relay connection.
417+
desktop-standalone *ARGS: _ensure-sidecar-stubs
418+
#!/usr/bin/env bash
419+
set -euo pipefail
420+
export PATH="{{justfile_directory()}}/bin:$PATH"
421+
cargo build -p buzz-acp -p buzz-agent -p buzz-dev-mcp -p buzz-cli -p git-credential-nostr
422+
TARGET=$(rustc -vV | sed -n 's|host: ||p')
423+
TARGET_DIR=$(cargo metadata --format-version 1 --no-deps | node -p "JSON.parse(require('fs').readFileSync(0, 'utf8')).target_directory")
424+
for bin in buzz-acp buzz-agent buzz-dev-mcp git-credential-nostr buzz; do
425+
cp "${TARGET_DIR}/debug/${bin}" "desktop/src-tauri/binaries/${bin}-${TARGET}"
426+
done
427+
cd {{desktop_dir}}
428+
[[ -d node_modules ]] || pnpm install
429+
unset BUZZ_PRIVATE_KEY BUZZ_SHARE_IDENTITY
430+
source ../scripts/instance-env.sh
431+
INSTANCE_ID=$(node -e "console.log(JSON.parse(process.env.BUZZ_TAURI_CONFIG).identifier)")
432+
export BUZZ_DEV_KEYRING_SERVICE="buzz-desktop-dev.${BUZZ_INSTANCE_SLUG:-main}"
433+
trap '../scripts/cleanup-instance-agents.sh "$INSTANCE_ID" || true' EXIT
434+
echo "Starting standalone desktop on Vite port ${BUZZ_VITE_PORT}; no relay services were started"
435+
pnpm exec tauri dev --config "$BUZZ_TAURI_CONFIG" {{ARGS}}
436+
415437
# Run the desktop app against the internal staging relay (installs deps + builds agent tools automatically)
416438
staging *ARGS: bootstrap _ensure-sidecar-stubs
417439
#!/usr/bin/env bash

‎desktop/src-tauri/src/app_state.rs‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,9 @@ pub fn resolve_persisted_identity(app: &AppHandle, state: &AppState) -> Result<(
328328
Ok(())
329329
}
330330

331-
/// Service name for the desktop OS keyring. Shared by the human identity key
332-
/// and managed-agent keys (each addressed by a distinct key name within it).
333-
///
334-
/// Debug builds use a distinct service name so dev and production keyring
335-
/// entries never collide on the same machine.
336-
pub(crate) fn keyring_service() -> &'static str {
337-
if cfg!(debug_assertions) {
338-
"buzz-desktop-dev"
339-
} else {
340-
"buzz-desktop"
341-
}
342-
}
331+
#[path = "app_state_keyring.rs"]
332+
mod keyring_config;
333+
pub(crate) use keyring_config::keyring_service;
343334

344335
/// Keyring key name for the human identity nsec.
345336
const IDENTITY_KEY_NAME: &str = "identity";
@@ -855,7 +846,10 @@ pub(crate) fn persist_imported_identity(
855846

856847
/// Path of the migration-completed marker within `data_dir`.
857848
fn migration_marker_path(data_dir: &std::path::Path) -> std::path::PathBuf {
858-
data_dir.join(MIGRATION_MARKER_NAME)
849+
data_dir.join(keyring_config::migration_marker_name(
850+
keyring_service(),
851+
MIGRATION_MARKER_NAME,
852+
))
859853
}
860854

861855
/// Atomically write (and fsync) the migration-completed marker. The content is
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/// Service name for the desktop OS keyring. Debug builds default to a distinct
2+
/// service, while standalone worktree launches may request a scoped dev service.
3+
fn dev_keyring_service(configured: Option<String>) -> String {
4+
configured
5+
.filter(|service| service.starts_with("buzz-desktop-dev."))
6+
.unwrap_or_else(|| "buzz-desktop-dev".to_string())
7+
}
8+
9+
pub(crate) fn keyring_service() -> &'static str {
10+
if cfg!(debug_assertions) {
11+
static DEV_SERVICE: std::sync::OnceLock<String> = std::sync::OnceLock::new();
12+
DEV_SERVICE
13+
.get_or_init(|| dev_keyring_service(std::env::var("BUZZ_DEV_KEYRING_SERVICE").ok()))
14+
.as_str()
15+
} else {
16+
"buzz-desktop"
17+
}
18+
}
19+
20+
pub(super) fn migration_marker_name(service: &str, default_name: &str) -> String {
21+
if service == "buzz-desktop" || service == "buzz-desktop-dev" {
22+
default_name.to_string()
23+
} else {
24+
format!("identity.{service}.migrated")
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::{dev_keyring_service, migration_marker_name};
31+
32+
#[test]
33+
fn standalone_scope_must_remain_under_dev_service() {
34+
assert_eq!(
35+
dev_keyring_service(Some("buzz-desktop-dev.example".to_string())),
36+
"buzz-desktop-dev.example"
37+
);
38+
assert_eq!(
39+
dev_keyring_service(Some("buzz-desktop".to_string())),
40+
"buzz-desktop-dev"
41+
);
42+
}
43+
44+
#[test]
45+
fn standalone_scope_uses_its_own_migration_marker() {
46+
assert_eq!(
47+
migration_marker_name("buzz-desktop", "identity.migrated"),
48+
"identity.migrated"
49+
);
50+
assert_eq!(
51+
migration_marker_name("buzz-desktop-dev", "identity.migrated"),
52+
"identity.migrated"
53+
);
54+
assert_eq!(
55+
migration_marker_name("buzz-desktop-dev.example", "identity.migrated"),
56+
"identity.buzz-desktop-dev.example.migrated"
57+
);
58+
}
59+
}

‎desktop/src-tauri/src/deep_link.rs‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,85 @@
1+
use std::{collections::VecDeque, sync::Mutex};
2+
13
use serde::Serialize;
2-
use tauri::{Emitter, Manager};
4+
use tauri::{Emitter, Manager, State};
35
use url::Url;
46

57
use crate::nostr_bind;
68

9+
#[derive(Debug, Clone, PartialEq, Serialize)]
10+
#[serde(rename_all = "camelCase")]
11+
pub(crate) struct PendingCommunityDeepLink {
12+
id: String,
13+
kind: String,
14+
relay_url: String,
15+
code: Option<String>,
16+
}
17+
18+
#[derive(Default)]
19+
pub(crate) struct PendingCommunityDeepLinks(Mutex<VecDeque<PendingCommunityDeepLink>>);
20+
21+
impl PendingCommunityDeepLinks {
22+
fn enqueue(&self, pending: PendingCommunityDeepLink) {
23+
let mut queue = self.0.lock().expect("pending deep-link queue poisoned");
24+
if queue.iter().any(|item| {
25+
item.kind == pending.kind
26+
&& item.relay_url == pending.relay_url
27+
&& item.code == pending.code
28+
}) {
29+
return;
30+
}
31+
queue.push_back(pending);
32+
}
33+
34+
fn first(&self) -> Option<PendingCommunityDeepLink> {
35+
self.0
36+
.lock()
37+
.expect("pending deep-link queue poisoned")
38+
.front()
39+
.cloned()
40+
}
41+
42+
fn acknowledge(&self, id: &str) -> bool {
43+
let mut queue = self.0.lock().expect("pending deep-link queue poisoned");
44+
if queue.front().is_some_and(|item| item.id == id) {
45+
queue.pop_front();
46+
true
47+
} else {
48+
false
49+
}
50+
}
51+
}
52+
53+
#[tauri::command]
54+
pub(crate) fn take_pending_community_deep_link(
55+
pending: State<'_, PendingCommunityDeepLinks>,
56+
) -> Option<PendingCommunityDeepLink> {
57+
pending.first()
58+
}
59+
60+
#[tauri::command]
61+
pub(crate) fn acknowledge_pending_community_deep_link(
62+
id: String,
63+
pending: State<'_, PendingCommunityDeepLinks>,
64+
) -> bool {
65+
pending.acknowledge(&id)
66+
}
67+
68+
fn queue_community_deep_link(
69+
app: &tauri::AppHandle,
70+
kind: &str,
71+
relay_url: String,
72+
code: Option<String>,
73+
) {
74+
app.state::<PendingCommunityDeepLinks>()
75+
.enqueue(PendingCommunityDeepLink {
76+
id: uuid::Uuid::new_v4().to_string(),
77+
kind: kind.to_owned(),
78+
relay_url,
79+
code,
80+
});
81+
}
82+
783
fn activate_main_window(app: &tauri::AppHandle) {
884
let Some(window) = app.get_webview_window("main") else {
985
return;
@@ -226,6 +302,7 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
226302
}
227303
}
228304
activate_main_window(app);
305+
queue_community_deep_link(app, "connect", relay_url.clone(), None);
229306
let _ = app.emit("deep-link-connect", relay_url);
230307
}
231308
Some("join") => {
@@ -237,6 +314,9 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
237314
return;
238315
};
239316
activate_main_window(app);
317+
let relay_url = payload["relayUrl"].as_str().unwrap_or_default().to_owned();
318+
let code = payload["code"].as_str().map(str::to_owned);
319+
queue_community_deep_link(app, "join", relay_url, code);
240320
let _ = app.emit("deep-link-join", payload);
241321
}
242322
Some("message") => {
@@ -277,7 +357,39 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
277357
mod tests {
278358
use url::Url;
279359

280-
use super::{parse_join_deep_link, parse_message_deep_link, parse_nostr_bind_deep_link};
360+
use super::{
361+
parse_join_deep_link, parse_message_deep_link, parse_nostr_bind_deep_link,
362+
PendingCommunityDeepLink, PendingCommunityDeepLinks,
363+
};
364+
365+
fn pending(id: &str, relay_url: &str, code: Option<&str>) -> PendingCommunityDeepLink {
366+
PendingCommunityDeepLink {
367+
id: id.to_owned(),
368+
kind: if code.is_some() { "join" } else { "connect" }.to_owned(),
369+
relay_url: relay_url.to_owned(),
370+
code: code.map(str::to_owned),
371+
}
372+
}
373+
374+
#[test]
375+
fn pending_community_links_are_fifo_and_acknowledged_in_order() {
376+
let queue = PendingCommunityDeepLinks::default();
377+
queue.enqueue(pending("first", "wss://one.example", Some("one")));
378+
queue.enqueue(pending("second", "wss://two.example", Some("two")));
379+
assert_eq!(queue.first().unwrap().id, "first");
380+
assert!(!queue.acknowledge("second"));
381+
assert!(queue.acknowledge("first"));
382+
assert_eq!(queue.first().unwrap().id, "second");
383+
}
384+
385+
#[test]
386+
fn pending_community_links_dedupe_exact_intents() {
387+
let queue = PendingCommunityDeepLinks::default();
388+
queue.enqueue(pending("first", "wss://one.example", Some("one")));
389+
queue.enqueue(pending("duplicate", "wss://one.example", Some("one")));
390+
assert!(queue.acknowledge("first"));
391+
assert!(queue.first().is_none());
392+
}
281393

282394
fn valid_nostr_bind_url() -> Url {
283395
Url::parse(

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ use mesh_llm_stubs::*;
3535

3636
use app_state::{build_app_state, resolve_persisted_identity, AppState};
3737
use commands::*;
38-
use deep_link::handle_deep_link_url;
38+
use deep_link::{
39+
acknowledge_pending_community_deep_link, handle_deep_link_url,
40+
take_pending_community_deep_link, PendingCommunityDeepLinks,
41+
};
3942
use huddle::audio_output::{
4043
get_audio_output_device, list_audio_output_devices, set_audio_output_device,
4144
};
@@ -449,6 +452,7 @@ pub fn run() {
449452
});
450453
})
451454
.manage(build_app_state())
455+
.manage(PendingCommunityDeepLinks::default())
452456
.manage(commands::pairing::PairingHandle::new())
453457
.setup(move |app| {
454458
let app_handle = app.handle().clone();
@@ -739,6 +743,8 @@ pub fn run() {
739743
Ok(())
740744
})
741745
.invoke_handler(tauri::generate_handler![
746+
take_pending_community_deep_link,
747+
acknowledge_pending_community_deep_link,
742748
title_bar_double_click,
743749
get_identity,
744750
get_nsec,

‎desktop/src-tauri/src/managed_agents/storage.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ fn persist_agent_keys_with(store: &impl KeyStore, records: &mut [ManagedAgentRec
384384
/// after the service-name change.
385385
#[cfg(debug_assertions)]
386386
pub fn migrate_agent_keys_to_dev_service(app: &tauri::AppHandle) {
387-
if !cfg!(feature = "system-keyring") {
387+
if !cfg!(feature = "system-keyring") || keyring_service() != "buzz-desktop-dev" {
388388
return;
389389
}
390390

0 commit comments

Comments
 (0)