mirror of
https://github.com/block/buzz.git
synced 2026-11-26 11:12:22 +00:00
## Summary - discover shared managed agents from authenticated relay directory records instead of treating channel membership as sufficient proof - publish and refresh access-policy changes immediately so running clients converge across machines without a restart or five-minute poll - route profile edits through the exact managed instance and stop/restart runtimes around access changes so unrelated edits cannot silently widen access - keep mention send-time revalidation and Block owner-only build enforcement fail closed - explain invalid custom provider/model configuration instead of leaving Save silently disabled ### Related issue Fixes #3204 ### Known residuals - a brand-new remote agent's first policy record can wait for the bounded directory poll when no authenticated directory coordinate exists yet; send-time mention revalidation remains fail closed - a failed remote-provider policy redeploy is recorded but cannot undeploy the older provider instance until the provider protocol gains the destructor tracked by #5570 ### Testing - full Desktop unit suite: 4,961 tests passed - focused profile editor Playwright workflow passed, including Customize access edits and prompt-only edits after tightening an instance - Desktop TypeScript, Biome formatting, file-size ratchet, Tauri checks, and pre-push suites passed - independently reviewed for authenticated directory trust, live subscription teardown, runtime revocation ordering, fail-open edit paths, and per-agent provider deployment serialization --------- Signed-off-by: Wes <wesbillman@users.noreply.github.com> Signed-off-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@buzz.block.builderlab.xyz> Co-authored-by: diegorumo <diegorumo@gmail.com> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@buzz.block.builderlab.xyz>
361 lines
15 KiB
Rust
361 lines
15 KiB
Rust
use super::*;
|
|
|
|
pub(crate) fn managed_agent_access_policy_changed(
|
|
current_mode: crate::managed_agents::RespondTo,
|
|
current_allowlist: &[String],
|
|
prospective_mode: crate::managed_agents::RespondTo,
|
|
prospective_allowlist: &[String],
|
|
) -> bool {
|
|
prospective_mode != current_mode
|
|
|| (prospective_mode == crate::managed_agents::RespondTo::Allowlist
|
|
&& prospective_allowlist != current_allowlist)
|
|
}
|
|
|
|
fn ensure_access_policy_change_supported(
|
|
record: &ManagedAgentRecord,
|
|
access_policy_changed: bool,
|
|
) -> Result<(), String> {
|
|
if access_policy_changed
|
|
&& record.backend != crate::managed_agents::BackendKind::Local
|
|
&& record.backend_agent_id.is_some()
|
|
{
|
|
return Err(
|
|
"Access cannot be changed while this provider-backed agent is deployed because the provider protocol has no explicit stop or revocation acknowledgement. Stop or recreate the provider agent first."
|
|
.to_string(),
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Flush a retained managed-agent policy, preserving any earlier profile error.
|
|
pub(crate) async fn flush_managed_agent_policy(
|
|
app: &AppHandle,
|
|
state: &AppState,
|
|
existing_error: Option<String>,
|
|
) -> Option<String> {
|
|
match crate::managed_agents::persona_events::flush_active_pending_events(app, state).await {
|
|
Ok(_) => existing_error,
|
|
Err(error) => Some(match existing_error {
|
|
Some(profile_error) => {
|
|
format!("{profile_error}; managed policy sync failed: {error}")
|
|
}
|
|
None => format!("managed policy sync failed: {error}"),
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Update mutable fields on an existing managed agent record.
|
|
///
|
|
/// Most runtime config changes take effect on the next agent spawn. Access
|
|
/// policy changes stop active local pairs before saving and restart those exact
|
|
/// pairs after the relay policy is flushed.
|
|
#[tauri::command]
|
|
pub async fn update_managed_agent(
|
|
input: UpdateManagedAgentRequest,
|
|
app: AppHandle,
|
|
state: State<'_, AppState>,
|
|
) -> Result<UpdateManagedAgentResponse, String> {
|
|
// Phase 1: local save (synchronous, under lock)
|
|
let (mut summary, sync_params, rollback, access_policy_changed, access_restart_relays) = {
|
|
let _store_guard = state
|
|
.managed_agents_store_lock
|
|
.lock()
|
|
.map_err(|e| e.to_string())?;
|
|
let mut records = load_managed_agents(&app)?;
|
|
let mut runtimes = state
|
|
.managed_agent_processes
|
|
.lock()
|
|
.map_err(|e| e.to_string())?;
|
|
let (_, exited_pubkeys) =
|
|
sync_managed_agent_processes(&mut records, &mut runtimes, ¤t_instance_id(&app));
|
|
for pubkey in &exited_pubkeys {
|
|
state.clear_agent_session_caches(pubkey);
|
|
}
|
|
|
|
let record = find_managed_agent_mut(&mut records, &input.pubkey)?;
|
|
let previous_record = record.clone();
|
|
|
|
let mut name_changed = false;
|
|
if let Some(name_update) = input.name {
|
|
let trimmed = name_update.trim().to_string();
|
|
if !trimmed.is_empty() && trimmed != record.name {
|
|
record.name = trimmed;
|
|
name_changed = true;
|
|
}
|
|
}
|
|
apply_model_provider_prompt_update(
|
|
record,
|
|
input.model,
|
|
input.provider,
|
|
input.system_prompt,
|
|
)?;
|
|
if let Some(parallelism) = input.parallelism {
|
|
record.parallelism = parallelism;
|
|
}
|
|
// turn_timeout_seconds is intentionally not applied here —
|
|
// BUZZ_ACP_TURN_TIMEOUT is deprecated and ignored by the harness.
|
|
// Use idle_timeout_seconds or max_turn_duration_seconds instead.
|
|
// Store the relay override exactly as supplied (trimmed). An explicit
|
|
// value pins the agent; empty falls back to the workspace relay at
|
|
// read-time. A name-only edit (relay_url == None) leaves the pin intact.
|
|
if let Some(relay_url) = input.relay_url {
|
|
record.relay_url = relay_url.trim().to_string();
|
|
}
|
|
if let Some(acp_command) = input.acp_command {
|
|
record.acp_command = acp_command;
|
|
}
|
|
// Harness edit: the persona's runtime is authoritative, so an explicit
|
|
// `agent_command_override` is persisted ONLY when the user picks a
|
|
// command that diverges from the persona, and the empty/whitespace
|
|
// "Inherit from persona" sentinel clears both the pin and the
|
|
// materialized record runtime. A name-only edit
|
|
// (`agent_command == None`) leaves the pin intact. `harness_override`
|
|
// threads the user's explicit intent — see `apply_agent_command_update`
|
|
// and `update_time_agent_command_override` for the full resolution
|
|
// rules.
|
|
if let Some(agent_command) = input.agent_command {
|
|
let personas = load_personas(&app).unwrap_or_default();
|
|
crate::managed_agents::apply_agent_command_update(
|
|
record,
|
|
&personas,
|
|
&agent_command,
|
|
input.harness_override,
|
|
);
|
|
}
|
|
if let Some(agent_args) = input.agent_args {
|
|
record.agent_args = agent_args;
|
|
}
|
|
// mcp_command is intentionally not applied here — the effective MCP
|
|
// command is always catalog-derived (known_acp_runtime at spawn time)
|
|
// and the per-record field is never read by the runtime.
|
|
if let Some(env_vars) = input.env_vars {
|
|
crate::managed_agents::validate_user_env_keys(&env_vars)?;
|
|
record.env_vars = env_vars;
|
|
}
|
|
|
|
// Native provider/model fields are authoritative. Keep the typed marker
|
|
// derived for new records while retaining legacy typed records for
|
|
// non-native providers.
|
|
if record.provider.as_deref() == Some(crate::managed_agents::RELAY_MESH_PROVIDER_ID) {
|
|
let model_ref = record
|
|
.model
|
|
.as_deref()
|
|
.map(str::trim)
|
|
.filter(|value| !value.is_empty())
|
|
.unwrap_or(crate::managed_agents::RELAY_MESH_AUTO_MODEL_ID)
|
|
.to_string();
|
|
record.model = Some(model_ref.clone());
|
|
record.relay_mesh = Some(crate::managed_agents::RelayMeshConfig { model_ref });
|
|
}
|
|
|
|
// Inbound author gate: merge patch onto current values, then validate
|
|
// the merged state. This lets a single update switch to Allowlist AND
|
|
// supply pubkeys atomically.
|
|
let prospective_mode = input.respond_to.unwrap_or(record.respond_to);
|
|
let prospective_allowlist = match input.respond_to_allowlist.as_ref() {
|
|
Some(list) => crate::managed_agents::validate_respond_to_allowlist(list)?,
|
|
None => record.respond_to_allowlist.clone(),
|
|
};
|
|
if prospective_mode == crate::managed_agents::RespondTo::Allowlist
|
|
&& prospective_allowlist.is_empty()
|
|
{
|
|
return Err(
|
|
"respond-to mode 'allowlist' requires at least one pubkey in the allowlist"
|
|
.to_string(),
|
|
);
|
|
}
|
|
let access_policy_changed = managed_agent_access_policy_changed(
|
|
record.respond_to,
|
|
&record.respond_to_allowlist,
|
|
prospective_mode,
|
|
&prospective_allowlist,
|
|
);
|
|
ensure_access_policy_change_supported(record, access_policy_changed)?;
|
|
|
|
// Revoke the currently running local gate before persisting or
|
|
// advertising the replacement policy. Keeping this inside the same
|
|
// store/process critical section prevents another command or a status
|
|
// refresh from observing a saved narrow policy while the old broad
|
|
// process is still alive. A stop failure aborts before mutation.
|
|
let mut access_restart_relays = Vec::new();
|
|
if access_policy_changed && record.backend == crate::managed_agents::BackendKind::Local {
|
|
access_restart_relays =
|
|
crate::managed_agents::managed_agent_runtime_keys(&runtimes, &record.pubkey)
|
|
.into_iter()
|
|
.map(|key| key.relay_url)
|
|
.collect();
|
|
if access_restart_relays.is_empty() && record.runtime_pid.is_some() {
|
|
access_restart_relays.push(crate::relay::effective_agent_relay_url(
|
|
&record.relay_url,
|
|
&relay_ws_url_with_override(&state),
|
|
));
|
|
}
|
|
if !access_restart_relays.is_empty() {
|
|
crate::managed_agents::stop_managed_agent_process(&app, record, &mut runtimes)?;
|
|
}
|
|
}
|
|
|
|
record.respond_to = prospective_mode;
|
|
// Preserve the persisted allowlist across mode toggles — only replace
|
|
// when the caller explicitly supplied a new list.
|
|
if input.respond_to_allowlist.is_some() {
|
|
record.respond_to_allowlist = prospective_allowlist;
|
|
}
|
|
|
|
record.updated_at = now_iso();
|
|
|
|
save_managed_agents(&app, &records)?;
|
|
|
|
let record = records
|
|
.iter()
|
|
.find(|r| r.pubkey == input.pubkey)
|
|
.ok_or_else(|| format!("agent {} not found", input.pubkey))?;
|
|
|
|
// Publish the edit to the relay. After-save, inside the lock, before
|
|
// any .await. The retention upsert hashes the opt-IN projection, so an
|
|
// update that touched only runtime/local fields is a no-op publish.
|
|
super::super::agents::retain_managed_agent_pending(&app, &state, record);
|
|
|
|
let sync_params = if name_changed {
|
|
let agent_keys = Keys::parse(&record.private_key_nsec)
|
|
.map_err(|e| format!("failed to parse agent keys: {e}"))?;
|
|
// Re-publish the renamed profile to the agent's effective relay:
|
|
// an explicit per-agent relay wins; empty falls back to workspace.
|
|
let relay_url = crate::relay::effective_agent_relay_url(
|
|
&record.relay_url,
|
|
&relay_ws_url_with_override(&state),
|
|
);
|
|
let display_name = record.name.clone();
|
|
// Avatar fallback derives from the EFFECTIVE harness (persona-wins),
|
|
// not the frozen snapshot, so an inherited harness picks the right
|
|
// default avatar.
|
|
let personas = load_personas(&app).unwrap_or_default();
|
|
let effective_command = crate::managed_agents::record_agent_command(record, &personas);
|
|
let avatar_url = record
|
|
.avatar_url
|
|
.clone()
|
|
.or_else(|| managed_agent_avatar_url(&effective_command));
|
|
let auth_tag = record.auth_tag.clone();
|
|
Some((agent_keys, relay_url, display_name, avatar_url, auth_tag))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let summary = {
|
|
let personas = load_personas(&app).unwrap_or_default();
|
|
build_managed_agent_summary(
|
|
&app,
|
|
record,
|
|
&runtimes,
|
|
&personas,
|
|
&crate::managed_agents::load_global_agent_config(&app).unwrap_or_default(),
|
|
)?
|
|
};
|
|
let rollback = name_changed
|
|
.then(|| AgentUpdateRollback::new(previous_record, record, access_policy_changed));
|
|
(
|
|
summary,
|
|
sync_params,
|
|
rollback,
|
|
access_policy_changed,
|
|
access_restart_relays,
|
|
)
|
|
}; // lock dropped here
|
|
|
|
try_regenerate_nest(&app);
|
|
|
|
// Phase 2: relay sync (async, outside lock). The owner-signed managed
|
|
// policy is security-sensitive: an access reduction must replace the old
|
|
// relay head before this command returns rather than waiting for the
|
|
// 30-second retention sweep. The flush remains durable/best-effort; rows a
|
|
// relay does not accept stay pending for the background retry.
|
|
let mut profile_sync_error =
|
|
crate::managed_agents::persona_events::flush_active_pending_events(&app, &state)
|
|
.await
|
|
.err()
|
|
.map(|error| format!("managed policy sync failed: {error}"));
|
|
if profile_sync_error.is_none()
|
|
&& crate::managed_agents::persona_events::active_pending_event(
|
|
&app,
|
|
&state,
|
|
buzz_core_pkg::kind::KIND_MANAGED_AGENT,
|
|
&summary.pubkey,
|
|
)?
|
|
{
|
|
profile_sync_error = Some(
|
|
"managed policy sync failed: relay did not accept the updated policy; retry queued"
|
|
.to_string(),
|
|
);
|
|
}
|
|
|
|
// A rename is committed only when profile sync succeeds; otherwise restore
|
|
// the complete pre-edit record so Desktop and the relay keep one
|
|
// authoritative name.
|
|
if let Some((agent_keys, relay_url, display_name, avatar_url, auth_tag)) = sync_params {
|
|
if let Err(sync_error) = sync_managed_agent_profile(
|
|
&state,
|
|
&relay_url,
|
|
&agent_keys,
|
|
&display_name,
|
|
avatar_url.as_deref(),
|
|
auth_tag.as_deref(),
|
|
)
|
|
.await
|
|
{
|
|
let rollback = rollback.ok_or_else(|| {
|
|
"missing local rollback state after relay profile sync failure".to_string()
|
|
})?;
|
|
rollback_failed_agent_update(&app, &state, &summary.pubkey, rollback)?;
|
|
let restart_suffix = if access_restart_relays.is_empty() {
|
|
String::new()
|
|
} else {
|
|
match super::super::agents::start_local_agent_pairs_with_preflight(
|
|
&app,
|
|
&state,
|
|
&summary.pubkey,
|
|
&access_restart_relays,
|
|
)
|
|
.await
|
|
{
|
|
Ok(_) => String::new(),
|
|
Err(error) => format!(
|
|
" The runtime also failed to restart with the kept access policy: {error}"
|
|
),
|
|
}
|
|
};
|
|
let rollback_message = if access_policy_changed {
|
|
"The access policy change was kept, but other edits were rolled back"
|
|
} else {
|
|
"No changes were saved"
|
|
};
|
|
return Err(format!(
|
|
"Agent rename failed because its relay profile could not be updated. {rollback_message}: {sync_error}.{restart_suffix}"
|
|
));
|
|
}
|
|
}
|
|
|
|
if !access_restart_relays.is_empty() {
|
|
summary = super::super::agents::start_local_agent_pairs_with_preflight(
|
|
&app,
|
|
&state,
|
|
&summary.pubkey,
|
|
&access_restart_relays,
|
|
)
|
|
.await
|
|
.map_err(|error| {
|
|
format!(
|
|
"Agent access was saved and published, but its runtime failed to restart with the new policy: {error}"
|
|
)
|
|
})?;
|
|
}
|
|
|
|
Ok(UpdateManagedAgentResponse {
|
|
agent: summary,
|
|
profile_sync_error: profile_sync_error.take(),
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "agent_models_update_tests.rs"]
|
|
mod tests;
|