Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions desktop/src-tauri/src/commands/agent_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,12 @@ async fn restart_single_agent_after_install(
if record.backend != BackendKind::Local {
return Err(format!("agent {pubkey_owned} is no longer a local agent"));
}
let runtime_keys =
crate::managed_agents::managed_agent_runtime_keys(&runtimes, &pubkey_owned);
if runtime_keys.is_empty() {
// Collect the restart dial targets (each pair's configured connection
// URL, not the canonical key spelling) BEFORE the stop below drops the
// pairs — and their URLs — from the runtimes map.
let restart_targets =
crate::managed_agents::managed_agent_restart_targets(&runtimes, &pubkey_owned);
if restart_targets.is_empty() {
return Err(format!(
"agent {pubkey_owned} no longer has a live pair runtime after sync"
));
Expand Down Expand Up @@ -606,12 +609,12 @@ async fn restart_single_agent_after_install(
stop_managed_agent_process(&app_for_stop, record_mut, &mut runtimes)?;
save_managed_agents(&app_for_stop, &records)?;

Ok(runtime_keys)
Ok(restart_targets)
})
.await;

let runtime_keys = match stop_result {
Ok(Ok(runtime_keys)) => runtime_keys,
let relay_urls = match stop_result {
Ok(Ok(restart_targets)) => restart_targets,
Ok(Err(e)) => {
eprintln!("buzz-desktop: install_acp_runtime: skipping restart of {pubkey}: {e}");
return InstallRestartOutcome::Skipped;
Expand All @@ -624,7 +627,6 @@ async fn restart_single_agent_after_install(
}
};

let relay_urls: Vec<_> = runtime_keys.into_iter().map(|key| key.relay_url).collect();
let state = app.state::<AppState>();
match super::agents::start_local_agent_pairs_with_preflight(app, &state, pubkey, &relay_urls)
.await
Expand Down
16 changes: 9 additions & 7 deletions desktop/src-tauri/src/commands/global_agent_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ async fn restart_local_agent_on_config_change(
if record.backend != BackendKind::Local {
return Err(format!("agent {pubkey_owned} is no longer a local agent"));
}
let runtime_keys =
crate::managed_agents::managed_agent_runtime_keys(&runtimes, &pubkey_owned);
if runtime_keys.is_empty() {
// Collect the restart dial targets (each pair's configured connection
// URL, not the canonical key spelling) BEFORE the stop below drops the
// pairs — and their URLs — from the runtimes map.
let restart_targets =
crate::managed_agents::managed_agent_restart_targets(&runtimes, &pubkey_owned);
if restart_targets.is_empty() {
return Err(format!(
"agent {pubkey_owned} no longer has a live pair runtime after sync"
));
Expand Down Expand Up @@ -327,12 +330,12 @@ async fn restart_local_agent_on_config_change(
stop_managed_agent_process(&app_for_stop, record_mut, &mut runtimes)?;
save_managed_agents(&app_for_stop, &records)?;

Ok(runtime_keys)
Ok(restart_targets)
})
.await;

let runtime_keys = match stop_result {
Ok(Ok(runtime_keys)) => runtime_keys,
let relay_urls = match stop_result {
Ok(Ok(restart_targets)) => restart_targets,
Ok(Err(e)) => {
eprintln!("buzz-desktop: set_global_agent_config: skipping restart of {pubkey}: {e}");
return RestartOutcome::Skipped;
Expand All @@ -345,7 +348,6 @@ async fn restart_local_agent_on_config_change(
}
};

let relay_urls: Vec<_> = runtime_keys.into_iter().map(|key| key.relay_url).collect();
use tauri::Manager;
let state = app.state::<AppState>();
match super::agents::start_local_agent_pairs_with_preflight(app, &state, pubkey, &relay_urls)
Expand Down
2 changes: 2 additions & 0 deletions desktop/src-tauri/src/managed_agents/process_lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn taskkill_tree(pid: u32) -> Result<(), String> {
pub fn finish_spawn(
child: std::process::Child,
log_path: std::path::PathBuf,
connect_relay_url: String,
spawn_config: super::spawn_snapshot::SpawnConfigSnapshot,
setup_mode: bool,
adapter_availability: Option<super::AcpAvailabilityStatus>,
Expand All @@ -149,6 +150,7 @@ pub fn finish_spawn(
super::ManagedAgentProcess {
child,
log_path,
connect_relay_url,
spawn_config,
setup_mode,
adapter_availability,
Expand Down
7 changes: 6 additions & 1 deletion desktop/src-tauri/src/managed_agents/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pub async fn restore_managed_agents_on_launch(
spawn_agent_child(
app,
record,
&key.relay_url,
&relay_url,
true,
owner_hex_ref,
)
Expand Down Expand Up @@ -390,6 +390,11 @@ pub async fn restore_managed_agents_on_launch(
pid: process.child.id(),
desktop_instance_id: super::current_instance_id(app),
started_at: now.clone(),
// Phase B stamped the dial URL onto the process at spawn;
// reading it back here (not recomputing from the record)
// keeps the receipt truthful even if the workspace relay
// changed between phases.
connect_relay_url: Some(process.connect_relay_url.clone()),
};
if let Err(error) = super::write_agent_runtime_receipt(app, &receipt) {
let _ = super::terminate_process(process.child.id());
Expand Down
29 changes: 23 additions & 6 deletions desktop/src-tauri/src/managed_agents/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) use metadata::{
};

mod stop;
pub(crate) use stop::managed_agent_runtime_keys;
pub(crate) use stop::{managed_agent_restart_targets, managed_agent_runtime_keys};
pub use stop::{stop_managed_agent_process, stop_managed_agent_workspace_pair};

mod sweep;
Expand Down Expand Up @@ -395,6 +395,10 @@ pub(crate) fn configure_runtime_cli(
}
}

fn connection_relay_url(configured_relay_url: &str) -> String {
configured_relay_url.trim().to_string()
}

/// Spawn an agent process without holding any locks on records or runtimes.
/// Returns the child process and log path on success. The caller is responsible
/// for updating `ManagedAgentRecord` fields and inserting into the runtimes map.
Expand Down Expand Up @@ -496,9 +500,10 @@ pub fn spawn_agent_child(
.map(|p| p.display().to_string())
.unwrap_or_else(|| effective_command.clone());

// The caller supplies the explicit canonical pair relay. This is the only
// relay this child may connect to, regardless of the record/workspace default.
let effective_relay_url = runtime_key.relay_url.clone();
// The canonical relay is only the pair identity. Preserve the configured
// authority for the network connection because relay communities are
// host-derived (`localhost` and `127.0.0.1` can be distinct tenants).
let effective_relay_url = connection_relay_url(relay_url);

// Augment PATH for DMG launches so child processes can find:
// - bundled CLI via ~/.local/bin symlink
Expand Down Expand Up @@ -852,7 +857,11 @@ pub fn spawn_agent_child(
super::spawn_snapshot::SpawnConfigInputs {
record,
descriptor: &descriptor,
relay_url: &effective_relay_url,
// Snapshot the canonical key relay, not the connection URL: the
// restart-drift check recomputes the prospective snapshot with
// `key.relay_url`, and the two must agree even when the configured
// spelling differs from the canonical one.
relay_url: &runtime_key.relay_url,
team_instructions: team_instructions.as_deref(),
system_prompt: effective_prompt.as_deref(),
model: effective_model.as_deref(),
Expand Down Expand Up @@ -907,6 +916,7 @@ pub fn spawn_agent_child(
return Ok(super::process_lifecycle::finish_spawn(
child,
log_path,
effective_relay_url,
spawn_config,
spawned_setup_mode,
spawned_adapter_availability,
Expand All @@ -917,6 +927,9 @@ pub fn spawn_agent_child(
Ok(crate::managed_agents::ManagedAgentProcess {
child,
log_path,
// The trimmed value that actually went into the child's
// BUZZ_RELAY_URL above, not the raw `relay_url` parameter.
connect_relay_url: effective_relay_url,
spawn_config,
setup_mode: spawned_setup_mode,
adapter_availability: spawned_adapter_availability,
Expand Down Expand Up @@ -964,13 +977,17 @@ pub fn start_managed_agent_process(
// Scalar PIDs are migration-only and never establish pair liveness.
record.runtime_pid = None;

let mut process = spawn_agent_child(app, record, &key.relay_url, false, owner_hex)?;
let mut process = spawn_agent_child(app, record, &relay_url, false, owner_hex)?;
let now = now_iso();
let receipt = super::ManagedAgentRuntimeReceipt {
key: key.clone(),
pid: process.child.id(),
desktop_instance_id: current_instance_id(app),
started_at: now.clone(),
// The URL the child actually dialed, not the local `relay_url`
// binding — same string by construction here, but reading it off the
// process keeps every receipt site identical to the spawn.
connect_relay_url: Some(process.connect_relay_url.clone()),
};
if let Err(error) = super::write_agent_runtime_receipt(app, &receipt) {
let _ = terminate_process(process.child.id());
Expand Down
9 changes: 9 additions & 0 deletions desktop/src-tauri/src/managed_agents/runtime/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,16 @@ pub(crate) fn valid_agent_runtime_receipt_with(
else {
return false;
};
// A stored connection URL must belong to this pair: canonicalizing it has
// to reproduce the receipt's own key. Absent is fine (pre-field receipts);
// present-but-foreign or unparseable is a corrupt receipt, not a fallback.
let connect_url_matches_key = match receipt.connect_relay_url.as_deref() {
None => true,
Some(connect_url) => ManagedAgentRuntimeKey::new(receipt.key.pubkey.clone(), connect_url)
.is_ok_and(|from_connect| from_connect == receipt.key),
};
canonical == receipt.key
&& connect_url_matches_key
&& path.file_name().and_then(|name| name.to_str())
== Some(&format!("{}.json", receipt.key.runtime_id()))
&& receipt.desktop_instance_id == instance_id
Expand Down
17 changes: 17 additions & 0 deletions desktop/src-tauri/src/managed_agents/runtime/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ pub(crate) fn managed_agent_runtime_keys<T>(
.collect()
}

/// The relay URLs to dial when restarting `pubkey`'s live pairs: each pair's
/// stamped connection URL. Restart paths must use this instead of
/// `key.relay_url` — the canonical spelling folds loopback hosts, and on a
/// host-scoped multi-tenant relay that lands the child in the wrong (empty)
/// community. Collect before stopping: stopping removes the pair, and with it
/// the only in-memory copy of the configured spelling.
pub(crate) fn managed_agent_restart_targets(
runtimes: &HashMap<ManagedAgentRuntimeKey, ManagedAgentPairRuntime>,
pubkey: &str,
) -> Vec<String> {
runtimes
.iter()
.filter(|(key, _)| key.pubkey.eq_ignore_ascii_case(pubkey))
.map(|(_, runtime)| runtime.connect_relay_url.clone())
.collect()
}

#[cfg(test)]
pub(crate) fn managed_agent_runtime_relay_urls<T>(
runtimes: &HashMap<ManagedAgentRuntimeKey, T>,
Expand Down
Loading
Loading