Files
buzz/crates/buzz-acp/src/lib.rs
Will Pfleger d99ad131f1 refactor: rename sprout backend to buzz (#958)
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <wpfleger@block.xyz>
Signed-off-by: Will Pfleger <wpfleger@squareup.com>
Signed-off-by: Will Pfleger <wpfleger96@gmail.com>
Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub16v54tttfqacx9ycvc3k0ut0npj564ahcuajzy6qjvh57ntmsf4uq4806j2 <d32955ad69077062930cc46cfe2df30ca9aaf6f8e76422681265e9e9af704d78@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Will Pfleger <wpfleger96@gmail.com>
2026-06-10 19:29:51 -04:00

2947 lines
121 KiB
Rust

#![deny(unsafe_code)]
mod acp;
mod config;
mod engram_fetch;
mod filter;
mod observer;
mod pool;
mod queue;
mod relay;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::Duration;
use acp::{AcpClient, EnvVar, McpServer};
use anyhow::Result;
use buzz_core::kind::{
KIND_MEMBER_ADDED_NOTIFICATION, KIND_MEMBER_REMOVED_NOTIFICATION, KIND_STREAM_MESSAGE,
KIND_STREAM_REMINDER, KIND_WORKFLOW_APPROVAL_REQUESTED,
};
use buzz_core::observer::{
decrypt_observer_payload, encrypt_observer_payload, OBSERVER_FRAME_TELEMETRY,
};
use clap::Parser;
use config::{Config, DedupMode, ModelsArgs, MultipleEventHandling, RespondTo, SubscribeMode};
use filter::SubscriptionRule;
use futures_util::FutureExt;
use nostr::{PublicKey, ToBech32};
use pool::{
AgentPool, CancelMode, OwnedAgent, PromptContext, PromptOutcome, PromptResult, PromptSource,
SessionState,
};
use queue::{prepend_base_prompt, EventQueue, QueuedEvent, ThreadTags};
use relay::{HarnessRelay, RelayEventPublisher};
use tokio::sync::{mpsc, watch};
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
// ── Subcommand dispatch ───────────────────────────────────────────────────────
/// Check if argv[1] matches a subcommand name, before any clap parsing.
///
/// This avoids clap rejecting harness flags (like `--private-key`) that aren't
/// declared on the subcommand's `Parser`. The `models` path has its own
/// `ModelsArgs` parser; the default path uses the existing `CliArgs`.
///
/// **Constraint**: subcommand must be argv[1] — flags before the subcommand
/// name (e.g., `buzz-acp --verbose models`) are not supported.
fn is_subcommand(name: &str) -> bool {
std::env::args().nth(1).map(|a| a == name).unwrap_or(false)
}
/// Timeout for the `buzz-acp models` subcommand (spawn + init + session/new).
const MODELS_TIMEOUT: Duration = Duration::from_secs(10);
// ── Presence helper ───────────────────────────────────────────────────────────
/// Publish a kind:20001 presence update event via the WebSocket connection.
///
/// Ephemeral kinds (20000-29999) are rejected by the HTTP bridge, so presence
/// updates must be routed through the WS path.
///
/// Content is a bare status string (`"online"`, `"away"`, `"offline"`) matching
/// the desktop client's format. The relay stores this in Redis and synthesizes
/// it back on presence queries.
async fn publish_presence(
publisher: &relay::RelayEventPublisher,
keys: &nostr::Keys,
status: &str,
) -> Result<(), relay::RelayError> {
use buzz_core::kind::KIND_PRESENCE_UPDATE;
use nostr::{EventBuilder, Kind};
let event = EventBuilder::new(Kind::Custom(KIND_PRESENCE_UPDATE as u16), status)
.tags([])
.sign_with_keys(keys)
.map_err(|e| relay::RelayError::Http(format!("presence sign error: {e}")))?;
publisher.publish_event(event).await?;
Ok(())
}
// ── Owner resolution ──────────────────────────────────────────────────────────
/// Resolve the agent's owner pubkey at startup.
///
/// Priority:
/// 1. `BUZZ_AUTH_TAG` env var — NIP-OA attestation signed by the owner.
/// Verified against the agent's own pubkey to extract the owner pubkey.
/// 2. `--agent-owner` CLI flag / `BUZZ_ACP_AGENT_OWNER` env var.
fn resolve_agent_owner(config: &Config) -> Option<String> {
// Try BUZZ_AUTH_TAG first (NIP-OA attestation).
if let Ok(auth_tag) = std::env::var("BUZZ_AUTH_TAG") {
if !auth_tag.is_empty() {
let agent_pk = config.keys.public_key();
match buzz_sdk::nip_oa::verify_auth_tag(&auth_tag, &agent_pk) {
Ok(owner_pk) => {
let owner_hex = owner_pk.to_hex().to_ascii_lowercase();
tracing::info!("owner resolved from BUZZ_AUTH_TAG: {owner_hex}");
return Some(owner_hex);
}
Err(e) => {
tracing::warn!("BUZZ_AUTH_TAG verification failed: {e} — falling back");
}
}
}
}
// Fall back to --agent-owner config.
config.agent_owner.clone()
}
// ── Owner cache ───────────────────────────────────────────────────────────────
/// Cache for the agent's owner pubkey.
///
/// Owner is now provided via `--agent-owner` config flag (no REST lookup).
/// Cache for the agent's owner pubkey + sibling lookups.
///
/// Siblings are other agents whose NIP-OA auth tag proves the same owner.
/// Lookup results are cached for the process lifetime (attestations are immutable).
struct OwnerCache {
pubkey: Option<String>,
/// author_hex → is_sibling (true = same owner, false = not)
siblings: std::sync::Mutex<HashMap<String, bool>>,
}
impl OwnerCache {
fn new(initial: Option<String>) -> Self {
Self {
pubkey: initial,
siblings: std::sync::Mutex::new(HashMap::new()),
}
}
/// Return the cached owner pubkey.
fn get(&self) -> Option<&str> {
self.pubkey.as_deref()
}
/// Check if author is a known sibling (cached result).
fn is_known_sibling(&self, author: &str) -> Option<bool> {
self.siblings.lock().ok()?.get(author).copied()
}
/// Cache a sibling lookup result.
fn cache_sibling(&self, author: String, is_sibling: bool) {
if let Ok(mut map) = self.siblings.lock() {
// Cap at 256 entries to prevent unbounded growth.
if map.len() >= 256 {
map.clear();
}
map.insert(author, is_sibling);
}
}
}
/// Check if `author` is the owner OR a sibling (same owner via NIP-OA).
///
/// For unknown authors, queries their kind:0 profile to extract the NIP-OA
/// auth tag and verify the owner matches. Result is cached.
async fn is_owner_or_sibling(
author: &str,
owner_cache: &OwnerCache,
rest_client: &relay::RestClient,
) -> bool {
let my_owner = match owner_cache.get() {
Some(o) => o,
None => return false, // no owner configured — fail closed
};
// Direct owner check.
if author == my_owner {
return true;
}
// Check sibling cache.
if let Some(cached) = owner_cache.is_known_sibling(author) {
return cached;
}
// Query the author's kind:0 profile to check for NIP-OA auth tag.
let is_sibling = check_sibling_via_profile(author, my_owner, rest_client).await;
owner_cache.cache_sibling(author.to_string(), is_sibling);
is_sibling
}
/// Query an author's kind:0 profile and check if their NIP-OA auth tag
/// proves the same owner as us.
async fn check_sibling_via_profile(
author: &str,
expected_owner: &str,
rest_client: &relay::RestClient,
) -> bool {
let filter = nostr::Filter::new()
.kind(nostr::Kind::Metadata)
.author(match nostr::PublicKey::from_hex(author) {
Ok(pk) => pk,
Err(_) => return false,
})
.limit(1);
let resp = match tokio::time::timeout(Duration::from_millis(2000), rest_client.query(&[filter]))
.await
{
Ok(Ok(v)) => v,
_ => return false, // timeout or error — fail closed
};
// Look for an "auth" tag in the profile event.
let events = match resp.as_array() {
Some(arr) => arr,
None => return false,
};
let event = match events.first() {
Some(e) => e,
None => return false,
};
let tags = match event.get("tags").and_then(|t| t.as_array()) {
Some(t) => t,
None => return false,
};
// Find ["auth", owner_pk, conditions, sig] and verify the Schnorr signature.
// Don't trust the relay — verify ourselves.
let agent_pk = match nostr::PublicKey::from_hex(author) {
Ok(pk) => pk,
Err(_) => return false,
};
for tag in tags {
let parts = match tag.as_array() {
Some(p) if p.len() >= 4 => p,
_ => continue,
};
if parts[0].as_str() != Some("auth") {
continue;
}
let tag_owner = match parts[1].as_str() {
Some(o) => o,
None => continue,
};
// Only verify if the owner field matches ours.
if !tag_owner.eq_ignore_ascii_case(expected_owner) {
continue;
}
// Cryptographically verify the NIP-OA attestation signature.
let tag_json = serde_json::to_string(tag).unwrap_or_default();
match buzz_sdk::nip_oa::verify_auth_tag(&tag_json, &agent_pk) {
Ok(_) => {
tracing::debug!(author, expected_owner, "sibling verified via NIP-OA");
return true;
}
Err(e) => {
tracing::debug!(author, "NIP-OA auth tag verification failed: {e}");
}
}
}
false
}
fn spawn_relay_observer_publisher(
observer: observer::ObserverHandle,
publisher: RelayEventPublisher,
keys: nostr::Keys,
agent_pubkey_hex: String,
owner_pubkey_hex: String,
owner_pubkey: PublicKey,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut coalescer = ObserverChunkCoalescer::default();
for event in observer.snapshot() {
for event in coalescer.ingest(event) {
publish_relay_observer_event(
&publisher,
&keys,
&agent_pubkey_hex,
&owner_pubkey_hex,
&owner_pubkey,
event,
)
.await;
}
}
let mut rx = observer.subscribe();
let mut flush_interval = tokio::time::interval(std::time::Duration::from_millis(500));
flush_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
tokio::select! {
result = rx.recv() => {
match result {
Ok(event) => {
for event in coalescer.ingest(event) {
publish_relay_observer_event(
&publisher, &keys, &agent_pubkey_hex,
&owner_pubkey_hex, &owner_pubkey, event,
).await;
}
}
Err(tokio::sync::broadcast::error::RecvError::Lagged(count)) => {
for event in coalescer.flush() {
publish_relay_observer_event(
&publisher, &keys, &agent_pubkey_hex,
&owner_pubkey_hex, &owner_pubkey, event,
).await;
}
tracing::warn!(dropped = count, "relay observer publisher lagged");
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
for event in coalescer.flush() {
publish_relay_observer_event(
&publisher, &keys, &agent_pubkey_hex,
&owner_pubkey_hex, &owner_pubkey, event,
).await;
}
break;
}
}
}
_ = flush_interval.tick() => {
// Periodic flush ensures live streaming even during continuous chunk delivery.
for event in coalescer.flush() {
publish_relay_observer_event(
&publisher, &keys, &agent_pubkey_hex,
&owner_pubkey_hex, &owner_pubkey, event,
).await;
}
}
}
}
})
}
#[derive(Default)]
struct ObserverChunkCoalescer {
pending: Vec<PendingObserverChunk>,
}
struct PendingObserverChunk {
key: ObserverChunkKey,
event: observer::ObserverEvent,
text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ObserverChunkKey {
update_type: String,
message_id: Option<String>,
channel_id: Option<String>,
session_id: Option<String>,
turn_id: Option<String>,
agent_index: Option<usize>,
}
/// Flush coalesced chunks before they exceed the NIP-44 plaintext limit (65,535 bytes).
/// Leave headroom for the JSON envelope wrapping the text.
const OBSERVER_CHUNK_MAX_TEXT_BYTES: usize = 60_000;
impl ObserverChunkCoalescer {
fn ingest(&mut self, event: observer::ObserverEvent) -> Vec<observer::ObserverEvent> {
let Some((key, text)) = observer_chunk_key_and_text(&event) else {
let mut events = self.flush();
events.push(event);
return events;
};
if let Some(pending) = self.pending.iter_mut().find(|pending| pending.key == key) {
// Flush before appending if this would exceed the plaintext size limit.
if pending.text.len() + text.len() >= OBSERVER_CHUNK_MAX_TEXT_BYTES {
let events = self.flush();
// Start a new pending entry with the current chunk.
self.pending.push(PendingObserverChunk { key, event, text });
return events;
}
pending.text.push_str(&text);
pending.event.seq = event.seq;
pending.event.timestamp = event.timestamp;
return Vec::new();
}
self.pending.push(PendingObserverChunk { key, event, text });
Vec::new()
}
fn flush(&mut self) -> Vec<observer::ObserverEvent> {
self.pending
.drain(..)
.map(|mut pending| {
set_observer_chunk_text(&mut pending.event.payload, pending.text);
pending.event
})
.collect()
}
}
fn observer_chunk_key_and_text(
event: &observer::ObserverEvent,
) -> Option<(ObserverChunkKey, String)> {
let update = event.payload.get("params")?.get("update")?;
let update_type = update.get("sessionUpdate")?.as_str()?;
if !matches!(
update_type,
"agent_message_chunk" | "user_message_chunk" | "agent_thought_chunk"
) {
return None;
}
let text = update.get("content")?.get("text")?.as_str()?.to_string();
let message_id = update
.get("messageId")
.and_then(|value| value.as_str())
.map(ToOwned::to_owned);
Some((
ObserverChunkKey {
update_type: update_type.to_string(),
message_id,
channel_id: event.channel_id.clone(),
session_id: event.session_id.clone(),
turn_id: event.turn_id.clone(),
agent_index: event.agent_index,
},
text,
))
}
fn set_observer_chunk_text(payload: &mut serde_json::Value, text: String) {
let Some(content) = payload
.get_mut("params")
.and_then(|params| params.get_mut("update"))
.and_then(|update| update.get_mut("content"))
else {
return;
};
if let Some(content_object) = content.as_object_mut() {
content_object.insert("text".to_string(), serde_json::Value::String(text));
}
}
async fn publish_relay_observer_event(
publisher: &RelayEventPublisher,
keys: &nostr::Keys,
agent_pubkey_hex: &str,
owner_pubkey_hex: &str,
owner_pubkey: &PublicKey,
event: observer::ObserverEvent,
) {
let encrypted = match encrypt_observer_payload(keys, owner_pubkey, &event) {
Ok(encrypted) => encrypted,
Err(error) => {
tracing::warn!("failed to encrypt relay observer event: {error}");
return;
}
};
let builder = match buzz_sdk::build_agent_observer_frame(
owner_pubkey_hex,
agent_pubkey_hex,
OBSERVER_FRAME_TELEMETRY,
&encrypted,
) {
Ok(builder) => builder,
Err(error) => {
tracing::warn!("failed to build relay observer event: {error}");
return;
}
};
let signed = match builder.sign_with_keys(keys) {
Ok(event) => event,
Err(error) => {
tracing::warn!("failed to sign relay observer event: {error}");
return;
}
};
if let Err(error) = publisher.publish_event(signed).await {
tracing::warn!("relay observer event dropped: {error}");
}
}
/// Maximum age (seconds) for an observer control frame to be considered fresh.
const OBSERVER_CONTROL_FRESHNESS_SECS: i64 = 300;
fn handle_relay_observer_control_event(
keys: &nostr::Keys,
event: nostr::Event,
pool: &mut AgentPool,
observer: Option<&observer::ObserverHandle>,
owner_pubkey_hex: &str,
) {
// Defense-in-depth: verify signature even though the relay already checked.
if let Err(e) = buzz_core::verify_event(&event) {
tracing::warn!(error = %e, "observer control frame failed signature verification");
return;
}
// Defense-in-depth: verify the sender is the resolved owner.
if event.pubkey.to_hex() != owner_pubkey_hex {
tracing::warn!(
sender = %event.pubkey,
expected = %owner_pubkey_hex,
"observer control frame from non-owner — dropping"
);
return;
}
// Freshness: reject stale/replayed frames outside ±5 minute window.
let now = chrono::Utc::now().timestamp();
let event_ts = event.created_at.as_secs() as i64;
if (event_ts - now).unsigned_abs() > OBSERVER_CONTROL_FRESHNESS_SECS as u64 {
tracing::warn!(
event_ts,
now,
"observer control frame outside freshness window — dropping"
);
return;
}
let payload = match decrypt_observer_payload::<serde_json::Value>(keys, &event) {
Ok(payload) => payload,
Err(error) => {
tracing::warn!("failed to decrypt observer control frame: {error}");
return;
}
};
let command_type = payload.get("type").and_then(|value| value.as_str());
if command_type != Some("cancel_turn") {
tracing::debug!(payload = %payload, "ignoring unknown observer control frame");
return;
}
let Some(channel_id) = payload
.get("channelId")
.and_then(|value| value.as_str())
.and_then(|value| value.parse::<Uuid>().ok())
else {
tracing::warn!("observer cancel_turn control frame missing valid channelId");
return;
};
let fired = cancel_in_flight_task(pool, channel_id, CancelMode::Stop);
let status = if fired { "sent" } else { "no_active_turn" };
if let Some(observer) = observer {
observer.emit(
"control_result",
None,
&observer::ObserverContext {
channel_id: Some(channel_id.to_string()),
session_id: None,
turn_id: None,
},
serde_json::json!({
"type": "cancel_turn",
"status": status,
}),
);
}
}
/// Maximum crashes in a 60-second window before a slot's circuit opens.
const CIRCUIT_BREAKER_THRESHOLD: usize = 3;
/// Window for circuit-breaker crash counting.
const CIRCUIT_BREAKER_WINDOW: Duration = Duration::from_secs(60);
/// Cooldown before a tripped circuit breaker allows a probe respawn.
const CIRCUIT_BREAKER_COOLDOWN: Duration = Duration::from_secs(300); // 5 minutes
/// Base backoff delay for respawn (doubles per recent crash, capped at 30s).
const RESPAWN_BASE_DELAY: Duration = Duration::from_secs(1);
/// Maximum respawn backoff delay.
const RESPAWN_MAX_DELAY: Duration = Duration::from_secs(30);
/// Per-slot circuit breaker state.
///
/// `crash_times` holds timestamps of recent crashes within `CIRCUIT_BREAKER_WINDOW`.
/// `open_until` is set when the threshold is hit; the circuit stays open until that
/// instant, then allows one probe respawn (half-open). If the probe crashes, the
/// circuit re-opens for another `CIRCUIT_BREAKER_COOLDOWN` period.
///
/// All state transitions go through methods on this struct — callers never
/// manipulate `crash_times` or `open_until` directly.
struct SlotCircuit {
crash_times: Vec<std::time::Instant>,
open_until: Option<std::time::Instant>,
/// True while a background respawn/refill task is in flight for this slot.
/// Prevents duplicate spawns from maintenance ticks that fire before the
/// previous spawn_and_init completes.
respawn_in_flight: bool,
}
/// Result of [`SlotCircuit::record_crash`].
enum CrashVerdict {
/// Respawn is allowed after sleeping for this duration (jittered backoff).
Respawn(Duration),
/// Circuit is open — do not respawn.
CircuitOpen,
/// Circuit was open but cooldown has elapsed — one probe respawn is allowed
/// (no backoff sleep). If the probe crashes, the next `record_crash` will
/// immediately re-open the circuit.
HalfOpenProbe,
}
impl SlotCircuit {
/// Record a crash and decide whether to respawn.
///
/// This is the **single canonical path** for all crash → respawn decisions.
/// Called by `respawn_agent_into`, `recover_panicked_agent`, and slot refill.
fn record_crash(&mut self) -> CrashVerdict {
let now = std::time::Instant::now();
// Half-open: cooldown elapsed → allow one probe.
if let Some(open_until) = self.open_until {
if now >= open_until {
// Pre-seed crash_times to threshold-1 so that if the probe
// itself crashes on the *next* call, the threshold is hit
// immediately and the circuit re-opens. This implements a
// "prove stability for one full window" policy.
self.crash_times.clear();
for _ in 0..(CIRCUIT_BREAKER_THRESHOLD - 1) {
self.crash_times.push(now);
}
self.open_until = None;
return CrashVerdict::HalfOpenProbe;
} else {
return CrashVerdict::CircuitOpen;
}
}
// Record this crash and prune old entries.
self.crash_times.push(now);
self.crash_times
.retain(|&t| now.duration_since(t) < CIRCUIT_BREAKER_WINDOW);
let recent = self.crash_times.len();
if recent >= CIRCUIT_BREAKER_THRESHOLD {
self.open_until = Some(now + CIRCUIT_BREAKER_COOLDOWN);
return CrashVerdict::CircuitOpen;
}
// Exponential backoff: 1s * 2^(recent-1), capped at 30s, with ±20% jitter.
let base = RESPAWN_BASE_DELAY.saturating_mul(1u32 << (recent - 1).min(5));
let capped = base.min(RESPAWN_MAX_DELAY);
let jitter = (std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.subsec_nanos() as f64)
/ 1_000_000_000.0; // 0.0..1.0
let factor = 0.8 + jitter * 0.4; // 0.8..1.2
CrashVerdict::Respawn(capped.mul_f64(factor))
}
/// Mark a spawn failure — opens the circuit so the slot isn't retried
/// on every heartbeat tick. Uses fresh `Instant::now()` so spawn latency
/// doesn't shorten the effective cooldown.
fn mark_spawn_failed(&mut self) {
self.open_until = Some(std::time::Instant::now() + CIRCUIT_BREAKER_COOLDOWN);
}
/// Check if an empty slot can be refilled. Unlike `record_crash`, this
/// does NOT record a new crash — it only checks whether the circuit
/// allows a respawn attempt.
///
/// Returns `true` if respawn is allowed. For half-open probes, pre-seeds
/// crash_times so the next crash re-opens immediately. For normal refills
/// (no circuit was ever opened), crash history is preserved so the breaker
/// can still trip if the refilled agent crashes quickly.
fn can_refill(&mut self) -> bool {
let now = std::time::Instant::now();
match self.open_until {
Some(open_until) => {
if now >= open_until {
// Half-open probe: pre-seed crash_times.
self.crash_times.clear();
for _ in 0..(CIRCUIT_BREAKER_THRESHOLD - 1) {
self.crash_times.push(now);
}
self.open_until = None;
true
} else {
false // cooldown not elapsed
}
}
None => true, // no circuit open — normal refill, preserve crash history
}
}
}
/// True if any slot has a respawn task in flight. Used to prevent premature
/// "all agents dead" exits — a respawning agent may succeed in seconds.
fn any_respawn_in_flight(crash_history: &[SlotCircuit]) -> bool {
crash_history.iter().any(|s| s.respawn_in_flight)
}
/// Result of a background respawn task.
struct RespawnResult {
index: usize,
result: Result<AcpClient>,
}
/// RAII guard that ensures a `RespawnResult` is sent even if the task panics.
/// Without this, a panicked respawn task would leave `respawn_in_flight = true`
/// permanently, silently losing the slot forever.
struct RespawnGuard {
index: usize,
tx: mpsc::Sender<RespawnResult>,
sent: bool,
}
impl RespawnGuard {
fn new(index: usize, tx: mpsc::Sender<RespawnResult>) -> Self {
Self {
index,
tx,
sent: false,
}
}
/// Send the result and disarm the guard. Uses `try_send` (sync) so there
/// is no await boundary between marking `sent` and actually enqueueing —
/// cancellation cannot slip between the two.
fn send(mut self, result: Result<AcpClient>) {
// Invariant: try_send succeeds because the channel capacity equals the
// slot count, and respawn_in_flight guarantees at most one outstanding
// result per slot. If this ever fails, the channel sizing or the
// respawn_in_flight guard has drifted — that's a bug, not a transient.
match self.tx.try_send(RespawnResult {
index: self.index,
result,
}) {
Ok(()) => self.sent = true,
Err(e) => {
tracing::error!(
agent = self.index,
"respawn result channel full or closed: {e}"
);
// Drop will fire and send a failure result as fallback.
}
}
}
}
impl Drop for RespawnGuard {
fn drop(&mut self) {
if !self.sent {
tracing::error!(
agent = self.index,
"respawn task exited without sending result — sending failure"
);
// Best-effort: try_send in Drop (can't await).
let _ = self.tx.try_send(RespawnResult {
index: self.index,
result: Err(anyhow::anyhow!("respawn task panicked or was cancelled")),
});
}
}
}
// ── Finding #16: propagate_legacy_env_vars before tokio runtime ───────────────
//
// Sync env-var propagation must run before the tokio runtime starts so that
// any child processes inherit the correct environment. This must happen in the
// sync entry point — `std::env::set_var` is only safe before tokio spawns
// worker threads (Rust 2024 edition safety requirement).
pub fn run() -> Result<()> {
config::propagate_legacy_env_vars();
tokio_main()
}
#[tokio::main]
async fn tokio_main() -> Result<()> {
// Install the ring crypto provider for rustls (required for wss:// connections).
rustls::crypto::ring::default_provider()
.install_default()
.expect("failed to install rustls crypto provider");
// ── Subcommand dispatch — before Config::from_cli() or any harness setup ──
if is_subcommand("models") {
// Strip the "models" token so clap doesn't reject it as a positional.
// Keeps argv[0] (binary name) and passes everything after "models".
let filtered: Vec<String> = std::env::args()
.enumerate()
.filter(|(i, _)| *i != 1)
.map(|(_, a)| a)
.collect();
let args = ModelsArgs::parse_from(&filtered);
return run_models(args).await;
}
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("buzz_acp=info")),
)
.compact()
.init();
let mut config = Config::from_cli().map_err(|e| anyhow::anyhow!("configuration error: {e}"))?;
tracing::info!("buzz-acp starting: {}", config.summary());
let observer = config
.relay_observer
.then(observer::ObserverHandle::in_process);
if let Some(handle) = &observer {
handle.emit(
"harness_started",
None,
&observer::ObserverContext::default(),
serde_json::json!({
"relayUrl": config.relay_url,
"agentCommand": config.agent_command,
"agentArgs": config.agent_args,
"parallelism": config.agents,
"relayObserver": config.relay_observer,
}),
);
}
// ── Step 1: Spawn N ACP agent subprocesses and initialize ─────────────────
//
// Finding #10: one agent failing to start must not kill the whole pool.
// We attempt each spawn under a 60-second timeout; failures are logged and
// skipped. If ALL agents fail we return an error. A partial pool is valid —
// the harness continues with reduced capacity and logs a warning.
let mut agent_slots: Vec<Option<OwnedAgent>> = Vec::with_capacity(config.agents as usize);
for i in 0..config.agents as usize {
// Spawn OUTSIDE the timeout so we always own the child for cleanup.
// This matches the run_models pattern and prevents zombie leaks on
// init timeout (the cancelled future would drop the AcpClient via
// Drop which is best-effort only).
let spawn_result = AcpClient::spawn(
&config.agent_command,
&config.agent_args,
&config.persona_env_vars,
)
.await;
match spawn_result {
Ok(mut acp) => {
acp.set_observer(observer.clone(), i);
match tokio::time::timeout(Duration::from_secs(60), acp.initialize()).await {
Ok(Ok(init_result)) => {
tracing::info!(agent = i, "agent initialized: {init_result}");
acp.observe(
"agent_initialized",
serde_json::json!({
"agentIndex": i,
"initializeResult": init_result,
}),
);
agent_slots.push(Some(OwnedAgent {
index: i,
acp,
state: SessionState::default(),
model_capabilities: None,
desired_model: config.model.clone(),
}));
}
Ok(Err(e)) => {
tracing::error!(agent = i, "agent initialize failed: {e}");
acp.shutdown().await;
agent_slots.push(None);
}
Err(_) => {
tracing::error!(agent = i, "agent timed out during init (60s)");
acp.shutdown().await;
agent_slots.push(None);
}
}
}
Err(e) => {
tracing::error!(agent = i, "agent failed to spawn: {e}");
agent_slots.push(None);
}
}
}
let live_count = agent_slots.iter().filter(|s| s.is_some()).count();
if live_count == 0 {
return Err(anyhow::anyhow!(
"all {} agents failed to start — cannot continue",
config.agents
));
}
if live_count < config.agents as usize {
tracing::warn!(
"started {}/{} agents — continuing with reduced pool",
live_count,
config.agents
);
}
tracing::info!("agent_pool_ready agents={}", live_count);
let mut pool = AgentPool::from_slots(agent_slots);
// ── Step 2: Connect to Buzz relay ──────────────────────────────────────
//
// Finding #22: capture a startup watermark BEFORE connecting to the relay.
// This timestamp is used for membership notification replay (via
// startup_watermark) and as the initial subscribe_since for channels
// discovered at startup. The Subscribe handler falls back to
// subscribe_since when last_seen is None, closing the blind spot
// between "agents ready" and "first REQ sent".
let startup_watermark: u64 = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let pubkey_hex = config.keys.public_key().to_hex();
// Parse BUZZ_AUTH_TAG into a nostr::Tag for NIP-OA relay membership delegation.
let relay_auth_tag: Option<nostr::Tag> = std::env::var("BUZZ_AUTH_TAG")
.ok()
.filter(|s| !s.is_empty())
.and_then(|s| buzz_sdk::nip_oa::parse_auth_tag(&s).ok());
let mut relay =
HarnessRelay::connect(&config.relay_url, &config.keys, &pubkey_hex, relay_auth_tag)
.await
.map_err(|e| anyhow::anyhow!("relay connect error: {e}"))?;
// Finding #22: tell the relay background task the watermark so it can use
// `since = watermark - 5s` on the first REQ instead of `since=now`.
// Best-effort: a failure here is non-fatal (we just lose the startup window
// protection, which is the same as the pre-fix behaviour).
if let Err(e) = relay.set_startup_watermark(startup_watermark).await {
tracing::warn!("failed to set startup watermark: {e}");
}
tracing::info!("connected to relay at {}", config.relay_url);
// ── Step 2b: Subscribe to membership notifications ────────────────────────
relay
.subscribe_membership_notifications()
.await
.map_err(|e| anyhow::anyhow!("membership notification subscribe error: {e}"))?;
tracing::info!("subscribed to membership notifications");
// ── Step 2c: Set initial presence ─────────────────────────────────────────
let presence_publisher = relay.event_publisher();
let presence_keys = config.keys.clone();
if config.presence_enabled {
match publish_presence(&presence_publisher, &presence_keys, "online").await {
Ok(_) => tracing::info!("presence set to online"),
Err(e) => tracing::warn!("failed to set initial presence: {e}"),
}
}
// ── Step 2d: Resolve agent owner ────────────────────────────────────────
// Priority: BUZZ_AUTH_TAG (NIP-OA attestation) → --agent-owner flag.
let startup_owner: Option<String> = resolve_agent_owner(&config);
if let Some(ref owner) = startup_owner {
tracing::info!("agent owner: {owner}");
} else {
tracing::info!("no agent owner configured");
}
// Warn if owner-dependent mode but no owner resolved yet.
if startup_owner.is_none() {
match &config.respond_to {
RespondTo::OwnerOnly => {
tracing::warn!(
"respond-to=owner-only but no owner is set — all events will be \
dropped. Set BUZZ_AUTH_TAG or --agent-owner, or use --respond-to=anyone."
);
}
RespondTo::Allowlist => {
tracing::warn!(
"respond-to=allowlist but no owner is set — allowlisted pubkeys \
will still be accepted, but owner-based matching is unavailable \
until owner is resolved."
);
}
_ => {} // anyone/nobody don't depend on owner
}
}
let owner_cache = OwnerCache::new(startup_owner.clone());
let mut relay_observer_control_rx = None;
let mut relay_observer_publisher_task = None;
if config.relay_observer {
if let (Some(observer), Some(owner_pubkey_hex)) =
(observer.clone(), owner_cache.pubkey.clone())
{
match PublicKey::from_hex(&owner_pubkey_hex) {
Ok(owner_pubkey) => {
relay_observer_publisher_task = Some(spawn_relay_observer_publisher(
observer,
relay.event_publisher(),
config.keys.clone(),
pubkey_hex.clone(),
owner_pubkey_hex,
owner_pubkey,
));
relay
.subscribe_observer_controls()
.await
.map_err(|e| anyhow::anyhow!("observer control subscribe error: {e}"))?;
relay_observer_control_rx = relay.take_observer_control_rx();
tracing::info!("relay observer enabled");
}
Err(error) => {
tracing::warn!("relay observer disabled: invalid owner pubkey: {error}");
}
}
} else {
tracing::warn!(
"relay observer requested but no agent owner was resolved at startup; \
observer frames will not be published"
);
}
}
// ── Step 3: Discover channels and build subscription rules ────────────────
let channel_info_map = relay
.discover_channels()
.await
.map_err(|e| anyhow::anyhow!("channel discovery error: {e}"))?;
tracing::info!("discovered {} channel(s)", channel_info_map.len());
let channel_ids: Vec<Uuid> = channel_info_map.keys().copied().collect();
let rules: Vec<SubscriptionRule> = match config.subscribe_mode {
SubscribeMode::Mentions => {
vec![SubscriptionRule {
name: "mentions".into(),
channels: filter::ChannelScope::All("all".into()),
kinds: config.kinds_override.clone().unwrap_or_else(|| {
vec![
KIND_STREAM_MESSAGE,
KIND_WORKFLOW_APPROVAL_REQUESTED,
KIND_STREAM_REMINDER,
]
}),
require_mention: !config.no_mention_filter,
filter: None,
compiled_filter: None,
consecutive_timeouts: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
prompt_tag: Some("@mention".into()),
}]
}
SubscribeMode::All => {
vec![SubscriptionRule {
name: "all".into(),
channels: filter::ChannelScope::All("all".into()),
kinds: config.kinds_override.clone().unwrap_or_default(),
require_mention: false,
filter: None,
compiled_filter: None,
consecutive_timeouts: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
prompt_tag: Some("all".into()),
}]
}
SubscribeMode::Config => {
// load_rules() already warns if the config file has zero rules.
config::load_rules(&config.config_path)?
}
};
// ── Step 4: Subscribe to channels ────────────────────────────────────────
let channel_filters = config::resolve_channel_filters(&config, &channel_ids, &rules);
if channel_filters.is_empty() {
tracing::warn!("no channel subscriptions resolved — agent will sit idle");
}
for (channel_id, filter) in &channel_filters {
if let Err(e) = relay.subscribe_channel(*channel_id, filter.clone()).await {
tracing::warn!("failed to subscribe to channel {channel_id}: {e}");
} else {
tracing::info!("subscribed to channel {channel_id}");
}
}
// ── Step 5: Build shared prompt context ──────────────────────────────────
let dedup_mode = config.dedup_mode;
let mut queue = EventQueue::new(dedup_mode);
let base_prompt_content = config.base_prompt_content.take();
let ctx = Arc::new(PromptContext {
mcp_servers: build_mcp_servers(&config),
initial_message: config.initial_message.clone(),
idle_timeout: Duration::from_secs(config.idle_timeout_secs),
max_turn_duration: Duration::from_secs(config.max_turn_duration_secs),
dedup_mode: config.dedup_mode,
system_prompt: config.system_prompt.clone(),
base_prompt: if config.no_base_prompt {
None
} else if let Some(content) = base_prompt_content {
Some(Box::leak(content.into_boxed_str()))
} else {
Some(include_str!("base_prompt.md"))
},
heartbeat_prompt: config.heartbeat_prompt.clone(),
cwd: std::env::current_dir()
.unwrap_or_else(|_| std::path::PathBuf::from("/"))
.to_string_lossy()
.to_string(),
rest_client: relay.rest_client(),
channel_info: channel_info_map,
context_message_limit: config.context_message_limit,
max_turns_per_session: config.max_turns_per_session,
permission_mode: config.permission_mode,
agent_keys: config.keys.clone(),
agent_owner_pubkey: startup_owner
.as_deref()
.and_then(|hex| nostr::PublicKey::from_hex(hex).ok()),
memory_enabled: config.memory_enabled,
});
if !config.memory_enabled {
tracing::info!(
target: "engram::core",
"NIP-AE core memory injection disabled (re-enable by removing --no-memory / BUZZ_ACP_NO_MEMORY)"
);
}
// ── Step 6: Heartbeat timer ───────────────────────────────────────────────
let mut heartbeat = if config.heartbeat_interval_secs > 0 {
let interval = Duration::from_secs(config.heartbeat_interval_secs);
Some(tokio::time::interval_at(
tokio::time::Instant::now() + interval,
interval,
))
} else {
None
};
let mut heartbeat_in_flight = false;
// ── Step 6b: Presence heartbeat timer (refreshes 90s TTL every 60s) ───────
let mut presence_heartbeat = if config.presence_enabled {
let interval = Duration::from_secs(60);
Some(tokio::time::interval_at(
tokio::time::Instant::now() + interval,
interval,
))
} else {
None
};
// ── Step 6c: Typing refresh timer (re-publishes kind:20002 every 3s) ──────
let mut typing_refresh = if config.typing_enabled {
let interval = Duration::from_secs(3);
Some(tokio::time::interval_at(
tokio::time::Instant::now() + interval,
interval,
))
} else {
None
};
let mut typing_channels: HashMap<Uuid, ThreadTags> = HashMap::new();
let mut presence_task: Option<tokio::task::JoinHandle<()>> = None;
// ── Step 6d: Maintenance (slot refill + queue compaction) ────────────────
// Runs at the TOP of every loop iteration via Instant check — cannot be
// starved by the biased select. Slot refill spawns background tasks so
// spawn_and_init never blocks the main loop.
let maintenance_interval = Duration::from_secs(30);
let mut last_maintenance = std::time::Instant::now();
// Channel for background respawn tasks to return completed agents.
// Bounded to agent count — at most one respawn per slot in flight.
let (respawn_tx, mut respawn_rx) = mpsc::channel::<RespawnResult>(config.agents as usize);
// JoinSet for respawn tasks so shutdown can abort them.
let mut respawn_tasks: tokio::task::JoinSet<()> = tokio::task::JoinSet::new();
// ── Step 7: Shutdown signal ───────────────────────────────────────────────
let (shutdown_tx, mut shutdown_rx) = watch::channel(());
let tx = shutdown_tx.clone();
tokio::spawn(async move {
tokio::signal::ctrl_c().await.ok();
let _ = tx.send(());
});
#[cfg(unix)]
{
let tx = shutdown_tx.clone();
tokio::spawn(async move {
use tokio::signal::unix::{signal, SignalKind};
let mut sigterm = signal(SignalKind::terminate()).expect("SIGTERM handler");
sigterm.recv().await;
let _ = tx.send(());
});
}
// Track the newest membership notification timestamp per channel.
// On reconnect the relay replays events newest-first, so the first event
// per channel is authoritative. Any later event with ts < newest is stale.
// Exact duplicates (same event ID) are caught by seen_membership_ids.
//
// Uses strict `<` (not `<=`) so that legitimate live events at the same
// second are both processed. The seen_membership_ids set handles exact
// replays that share the same timestamp.
let mut membership_newest_ts: HashMap<Uuid, u64> = HashMap::new();
// Two-generation dedup for membership event replays (bounded, no amnesia).
// Rotates at 1000 entries instead of clearing the entire set at 2000.
let mut seen_membership_current: HashSet<String> = HashSet::new();
let mut seen_membership_previous: HashSet<String> = HashSet::new();
// Channels the agent has been removed from. When a checked-out agent is
// returned to the pool, its sessions for these channels are stripped, and
// failed/panicked batches for these channels are dropped instead of requeued.
//
// Cleared on re-add (KIND_MEMBER_ADDED_NOTIFICATION) so re-joined channels
// regain session affinity.
//
// Known limitation: if a batch is in-flight when the channel is removed AND
// re-added before the batch returns, the stale batch may be requeued. This
// is acceptable because: (a) the agent is a member again and has access,
// (b) the events are from the agent's authorized history, (c) the window
// is extremely narrow (membership changes are rare, prompt turns are seconds),
// and (d) fixing this would require per-channel epoch tracking on TaskMeta
// and PromptResult — significant complexity for a benign edge case. If strict
// causal invalidation is needed, add a monotonic epoch counter per channel
// and capture it in TaskMeta at dispatch time.
let mut removed_channels: HashSet<Uuid> = HashSet::new();
// ── Finding #14: Per-slot crash history for circuit breaker ───────────────
//
// One SlotCircuit per agent slot. crash_times entries are pruned to the last
// CIRCUIT_BREAKER_WINDOW on each respawn attempt. The Vec is indexed by
// agent slot index, so it must be sized to the configured pool capacity
// (not the live count, which may be smaller after partial startup).
let mut crash_history: Vec<SlotCircuit> = (0..config.agents as usize)
.map(|_| SlotCircuit {
crash_times: Vec::new(),
open_until: None,
respawn_in_flight: false,
})
.collect();
// ── Step 8: Main orchestration loop ──────────────────────────────────────
//
// Branches 1 & 2 both need to borrow `pool`, but they access different
// fields (result_rx vs join_set). We use `rx_and_join_set()` to split the
// borrow, yielding a typed enum so the outer code can dispatch cleanly.
enum PoolEvent {
Result(Box<PromptResult>),
Panic(tokio::task::JoinError),
}
loop {
// ── Maintenance (runs at loop top — cannot be starved by biased select) ──
if last_maintenance.elapsed() >= maintenance_interval {
last_maintenance = std::time::Instant::now();
queue.compact_expired_state();
// Slot refill: spawn background tasks for empty slots whose
// circuit breaker allows it. spawn_and_init runs off the main
// loop so it never blocks event processing.
for (idx, slot) in crash_history.iter_mut().enumerate() {
if pool.slot_alive(idx) || slot.respawn_in_flight {
continue;
}
if !slot.can_refill() {
continue;
}
slot.respawn_in_flight = true;
tracing::info!(agent = idx, "slot refill: spawning background respawn");
let cmd = config.agent_command.clone();
let args = config.agent_args.clone();
let env = config.persona_env_vars.clone();
let observer = observer.clone();
let guard = RespawnGuard::new(idx, respawn_tx.clone());
respawn_tasks.spawn(async move {
let result = spawn_and_init(&cmd, &args, &env, idx, observer).await;
guard.send(result);
});
}
// Flush requeued batches whose retry_after has expired. Without
// this, a batch requeued during crash recovery can sit idle
// indefinitely on quiet channels — dispatch_pending is only
// called on relay events or pool results, neither of which
// arrive when the channel is silent.
if queue.has_flushable_work() {
for (channel_id, thread_tags) in dispatch_pending(&mut pool, &mut queue, &ctx) {
typing_channels.insert(channel_id, thread_tags);
}
}
}
// ── Collect completed background respawns (non-blocking) ─────────────
let mut respawn_collected = false;
while let Ok(rr) = respawn_rx.try_recv() {
crash_history[rr.index].respawn_in_flight = false;
match rr.result {
Ok(acp) => {
let agent = OwnedAgent {
index: rr.index,
acp,
state: SessionState::default(),
model_capabilities: None,
desired_model: config.model.clone(),
};
pool.return_agent(agent);
tracing::info!(agent = rr.index, "respawn complete");
respawn_collected = true;
}
Err(e) => {
crash_history[rr.index].mark_spawn_failed();
tracing::warn!(agent = rr.index, "respawn failed: {e} — circuit re-opened");
}
}
}
// Flush requeued events that were waiting for a live agent. Without
// this, batches requeued during crash recovery sit idle until the
// next relay event arrives — which can be minutes on quiet channels.
if respawn_collected {
for (channel_id, thread_tags) in dispatch_pending(&mut pool, &mut queue, &ctx) {
typing_channels.insert(channel_id, thread_tags);
}
}
// Borrow result_rx and join_set simultaneously via split-borrow helper.
let pool_event: Option<PoolEvent> = {
let (result_rx, join_set) = pool.rx_and_join_set();
tokio::select! {
biased;
// Finding #24: recv() returning None means all senders dropped
// (pool was torn down). Break cleanly instead of panicking.
r = result_rx.recv() => match r {
Some(result) => Some(PoolEvent::Result(Box::new(result))),
None => {
tracing::info!("result channel closed — exiting main loop");
break;
}
},
// Guard: join_next() returns None immediately when JoinSet is
// empty, which would cause a tight spin. Only poll when there
// are in-flight tasks.
Some(Err(e)) = join_set.join_next(), if !join_set.is_empty() => {
Some(PoolEvent::Panic(e))
}
control_event = async {
match relay_observer_control_rx.as_mut() {
Some(rx) => rx.recv().await,
None => std::future::pending().await,
}
} => {
let _ = result_rx;
match control_event {
Some(event) => {
if let Some(ref owner_hex) = owner_cache.pubkey {
handle_relay_observer_control_event(&config.keys, event, &mut pool, observer.as_ref(), owner_hex);
} else {
tracing::warn!("observer control frame received but no owner resolved — dropping");
}
}
None => {
relay_observer_control_rx = None;
tracing::warn!("relay observer control channel closed");
}
}
None
}
// Remaining branches don't touch pool — evaluated when pool is idle.
buzz_event = relay.next_event() => {
let _ = result_rx; // end split borrow before relay handling
match buzz_event {
Some(buzz_event) => {
let kind_u32 = buzz_event.event.kind.as_u16() as u32;
// ── Membership notification handling ──────────────
if kind_u32 == KIND_MEMBER_ADDED_NOTIFICATION
|| kind_u32 == KIND_MEMBER_REMOVED_NOTIFICATION
{
let ch = buzz_event.channel_id;
let ts = buzz_event.event.created_at.as_secs();
let eid = buzz_event.event.id.to_hex();
// Two-layer membership dedup:
//
// 1. Exact duplicate rejection (seen_membership_ids):
// Catches the same event replayed on reconnect.
//
// 2. Timestamp watermark (membership_newest_ts):
// Uses strict `<` so that older events from reconnect
// replay are dropped, but legitimate live events at the
// same second are both processed. This is safe because
// exact duplicates are already caught by layer 1.
//
// Why not `<=`? That would suppress legitimate live
// add→remove (or remove→add) sequences in the same second,
// leaving the harness in the wrong membership state.
// Two-generation dedup: check both sets before inserting.
if seen_membership_current.contains(&eid)
|| seen_membership_previous.contains(&eid)
{
tracing::debug!(
channel_id = %ch,
kind = kind_u32,
"skipping duplicate membership notification (same event_id)"
);
continue;
}
seen_membership_current.insert(eid);
// Rotate at 1000: current → previous, no amnesia window.
if seen_membership_current.len() >= 1000 {
seen_membership_previous =
std::mem::take(&mut seen_membership_current);
}
if let Some(&newest) = membership_newest_ts.get(&ch) {
if ts < newest {
tracing::debug!(
channel_id = %ch,
kind = kind_u32,
ts,
newest,
"skipping stale membership notification (older than newest)"
);
continue;
}
}
membership_newest_ts.insert(ch, ts);
if kind_u32 == KIND_MEMBER_ADDED_NOTIFICATION {
// Clear removal tracking so sessions are not
// stripped for a legitimately re-added channel.
removed_channels.remove(&ch);
if let Some(filter) = config::resolve_dynamic_channel_filter(&config, ch, &rules) {
tracing::info!(channel_id = %ch, "membership notification: subscribing to new channel");
if let Err(e) = relay.subscribe_channel_from(ch, filter, Some(ts)).await {
tracing::warn!("failed to subscribe to new channel {ch}: {e}");
}
} else {
tracing::debug!(channel_id = %ch, "membership notification: no matching rules — skipping");
}
} else {
tracing::info!(channel_id = %ch, "membership notification: unsubscribing from channel");
if let Err(e) = relay.unsubscribe_channel(ch).await {
tracing::warn!("failed to unsubscribe from channel {ch}: {e}");
}
// Drain queued events and invalidate sessions for the
// removed channel. Events already in-flight will
// complete normally (the relay may reject actions if
// the agent lost access).
let drained_ids = queue.drain_channel(ch);
let invalidated = pool.invalidate_channel_sessions(ch);
// Track removed channels so checked-out agents get
// their sessions stripped when they return to the pool.
removed_channels.insert(ch);
typing_channels.remove(&ch);
// Best-effort: clean up 👀 on drained events.
// Note: the relay revokes membership before
// emitting the notification, so this DELETE may
// 403 on non-open channels. Stale 👀 in that
// case is a known limitation — fix belongs in
// the relay (clean up bot reactions on removal).
if !drained_ids.is_empty() {
let rc = ctx.rest_client.clone();
let ids = drained_ids.clone();
tokio::spawn(async move {
for eid in &ids {
pool::reaction_remove(&rc, eid, "👀").await;
}
});
}
if !drained_ids.is_empty() || invalidated > 0 {
tracing::info!(
channel_id = %ch,
drained = drained_ids.len(),
invalidated,
"cleaned up after membership removal"
);
}
}
continue;
}
// ── End membership notification handling ──────────
if config.ignore_self && buzz_event.event.pubkey.to_hex() == pubkey_hex {
tracing::debug!(channel_id = %buzz_event.channel_id, "dropping self-authored event");
continue;
}
// ── Shutdown command handling ─────────────────────
// Check: kind:9, content "!shutdown", from owner, mentions THIS agent.
let is_shutdown = kind_u32 == KIND_STREAM_MESSAGE
&& buzz_event.event.content.trim() == "!shutdown"
&& buzz_event.event.tags.iter().any(|t| {
t.as_slice().first().map(|s| s.as_str()) == Some("p")
&& t.as_slice().get(1).map(|s| s.as_str()) == Some(pubkey_hex.as_str())
});
if is_shutdown {
let owner = owner_cache.get();
if let Some(owner) = owner {
if buzz_event.event.pubkey.to_hex() == *owner {
tracing::info!(
channel_id = %buzz_event.channel_id,
sender = %buzz_event.event.pubkey.to_hex(),
"shutdown command from owner — exiting gracefully"
);
let _ = shutdown_tx.send(());
continue;
}
}
// Not from owner — fall through to normal prompt handling.
// Don't drop it — it's a regular message that happens to
// contain "!shutdown" from a non-owner.
}
// ── End shutdown command handling ──────────────────
// ── Cancel command handling ──────────────────────
// Mirrors !shutdown: kind:9, content "!cancel", from
// owner, mentions THIS agent. Must be BEFORE
// queue.push() — the event content is moved by push.
//
// Mode-independent: !cancel fires regardless of
// --multiple-event-handling. It is explicit user
// intent, not an automatic policy decision.
let is_cancel = kind_u32 == KIND_STREAM_MESSAGE
&& buzz_event.event.content.trim() == "!cancel"
&& buzz_event.event.tags.iter().any(|t| {
t.as_slice().first().map(|s| s.as_str()) == Some("p")
&& t.as_slice().get(1).map(|s| s.as_str()) == Some(pubkey_hex.as_str())
});
if is_cancel {
if let Some(owner) = owner_cache.get() {
if buzz_event.event.pubkey.to_hex() == *owner {
let fired = cancel_in_flight_task(&mut pool, buzz_event.channel_id, CancelMode::Stop);
if !fired {
tracing::warn!(
channel_id = %buzz_event.channel_id,
"!cancel received but no in-flight task — no-op"
);
}
continue; // consume event — do NOT push to queue
}
}
// Not from owner — fall through to normal prompt handling.
}
// ── End cancel command handling ───────────────────
// ── Inbound author gate ──────────────────────────
// Coarse security policy: drop events from disallowed
// authors before they reach subscription rules or the
// agent. Must be AFTER !shutdown (owner can always
// shut down regardless of gate mode).
//
// OwnerOnly also accepts events from "siblings" —
// pubkeys whose agent_owner_pubkey matches this
// agent's owner (e.g. other bots launched by the
// same human). Allowlist is unchanged: owner +
// explicit pubkey list only.
{
let author = buzz_event.event.pubkey.to_hex();
let allowed = match &config.respond_to {
RespondTo::Anyone => true,
RespondTo::Nobody => false,
RespondTo::OwnerOnly => {
is_owner_or_sibling(&author, &owner_cache, &ctx.rest_client).await
}
RespondTo::Allowlist => {
let owner = owner_cache.get();
config.respond_to_allowlist.contains(&author)
|| owner == Some(author.as_str())
}
};
if !allowed {
tracing::debug!(
channel_id = %buzz_event.channel_id,
author = %buzz_event.event.pubkey.to_hex(),
mode = %config.respond_to,
"inbound author gate — dropping event"
);
continue;
}
}
// ── End inbound author gate ──────────────────────
let matched = filter::match_event(&buzz_event.event, buzz_event.channel_id, &rules, &pubkey_hex).await;
let prompt_tag = match matched {
Some(m) => m.prompt_tag,
None => {
tracing::debug!(channel_id = %buzz_event.channel_id, kind = buzz_event.event.kind.as_u16(), "event matched no rule — dropping");
continue;
}
};
// Capture author pubkey before queue.push() moves
// buzz_event.event (needed for mode gate below).
let author_hex = buzz_event.event.pubkey.to_hex();
let event_id_hex = buzz_event.event.id.to_hex();
let accepted = queue.push(QueuedEvent {
channel_id: buzz_event.channel_id,
event: buzz_event.event,
received_at: std::time::Instant::now(),
prompt_tag,
});
// 👀 — immediate "seen" reaction, only if the event
// was actually queued (not dropped by DedupMode::Drop).
// Fire-and-forget: on rare fast-failure paths the
// guard's cleanup may race with this add, leaving a
// cosmetic stale 👀. Acceptable — see ReactionGuard docs.
if accepted {
let rc = ctx.rest_client.clone();
tokio::spawn(async move {
pool::reaction_add(&rc, &event_id_hex, "👀").await;
});
}
// ── Multiple-event-handling mode gate ─────────────
// Event is already queued. If mode requires it AND
// the channel has an in-flight task, fire cancel.
if accepted && queue.is_channel_in_flight(buzz_event.channel_id) {
let should_cancel = match config.multiple_event_handling {
MultipleEventHandling::Queue => false,
MultipleEventHandling::Interrupt => true,
MultipleEventHandling::OwnerInterrupt => {
match owner_cache.get() {
Some(o) => author_hex == *o,
None => false,
}
}
};
if should_cancel {
cancel_in_flight_task(&mut pool, buzz_event.channel_id, CancelMode::Interrupt);
}
}
// ── End mode gate ────────────────────────────────
for (channel_id, thread_tags) in
dispatch_pending(&mut pool, &mut queue, &ctx)
{
typing_channels.insert(channel_id, thread_tags);
}
}
None => {
tracing::warn!("relay event stream ended — requesting reconnect");
if let Err(e) = relay.reconnect().await {
tracing::error!("relay background task is gone: {e} — exiting");
tokio::time::sleep(Duration::from_secs(1)).await;
break;
}
}
}
None
}
_ = async {
match heartbeat.as_mut() {
Some(hb) => hb.tick().await,
None => std::future::pending().await,
}
} => {
let _ = result_rx;
if queue.has_flushable_work() {
tracing::debug!("heartbeat_skipped_events");
for (channel_id, thread_tags) in
dispatch_pending(&mut pool, &mut queue, &ctx)
{
typing_channels.insert(channel_id, thread_tags);
}
} else if pool.any_idle() {
dispatch_heartbeat(&mut pool, &ctx, &mut heartbeat_in_flight);
} else {
tracing::debug!("heartbeat_skipped_busy");
}
None
}
_ = async {
match presence_heartbeat.as_mut() {
Some(t) => t.tick().await,
None => std::future::pending().await,
}
} => {
let _ = result_rx;
// Abort previous heartbeat if still in flight (prevents race on shutdown).
if let Some(h) = presence_task.take() {
h.abort();
}
let pp = presence_publisher.clone();
let pk = presence_keys.clone();
presence_task = Some(tokio::spawn(async move {
if let Err(e) = publish_presence(&pp, &pk, "online").await {
tracing::warn!("presence heartbeat failed: {e}");
}
}));
None
}
_ = async {
match typing_refresh.as_mut() {
Some(t) => t.tick().await,
None => std::future::pending().await,
}
} => {
let _ = result_rx;
// Use try_publish (non-blocking) for typing indicators —
// they're ephemeral and must not block the main loop during
// relay reconnection (#35).
for (&ch, thread_tags) in &typing_channels {
if let Ok(event) = relay.build_typing_event(
ch,
thread_tags.root_event_id.as_deref(),
thread_tags.parent_event_id.as_deref(),
) {
if let Err(e) = relay.try_publish_event(event) {
tracing::debug!("typing indicator dropped for {ch}: {e}");
}
}
}
None
}
_ = shutdown_rx.changed() => {
tracing::info!("shutting down");
break;
}
}
};
match pool_event {
Some(PoolEvent::Result(result)) => {
// Stop typing indicator for the completed channel.
if let PromptSource::Channel(ch) = &result.source {
typing_channels.remove(ch);
}
if handle_prompt_result(
&mut pool,
&mut queue,
&config,
*result,
&mut heartbeat_in_flight,
&removed_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
observer.clone(),
&relay,
) == LoopAction::Exit
{
break;
}
if drain_ready_join_results(
&mut pool,
&mut queue,
&config,
&mut heartbeat_in_flight,
&removed_channels,
&mut typing_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
observer.clone(),
) == LoopAction::Exit
{
break;
}
for (channel_id, thread_tags) in dispatch_pending(&mut pool, &mut queue, &ctx) {
typing_channels.insert(channel_id, thread_tags);
}
}
Some(PoolEvent::Panic(join_error)) => {
tracing::error!("agent task panicked: {join_error}");
recover_panicked_agent(
&mut pool,
&mut queue,
&config,
join_error,
&mut heartbeat_in_flight,
&removed_channels,
&mut typing_channels,
&mut crash_history,
&respawn_tx,
&mut respawn_tasks,
observer.clone(),
);
if pool.live_count() == 0 && !any_respawn_in_flight(&crash_history) {
tracing::error!("all agents dead — exiting");
break;
}
for (channel_id, thread_tags) in dispatch_pending(&mut pool, &mut queue, &ctx) {
typing_channels.insert(channel_id, thread_tags);
}
}
None => {} // relay/heartbeat/shutdown branches handled inline above
}
}
// ── Shutdown sequence ─────────────────────────────────────────────────────
tracing::info!("shutdown: waiting for in-flight prompts");
// 30 s is generous for in-flight prompts to be cancelled; using
// max_turn_duration here would cause Ctrl+C to hang for up to an hour.
let grace = Duration::from_secs(30);
// Best-effort drain of both join_set and result_rx during the grace period.
// Tasks that finish normally send their OwnedAgent through result_rx — we
// explicitly shut them down here to reap child processes. If the grace
// period expires, remaining tasks are aborted and fall back to
// AcpClient::Drop (start_kill + try_wait — best-effort, not guaranteed).
let (rx_ref, js_ref) = pool.rx_and_join_set();
let shutdown_result = tokio::time::timeout(grace, async {
loop {
tokio::select! {
result = js_ref.join_next() => {
match result {
Some(Err(e)) => tracing::warn!("task error during shutdown: {e}"),
Some(Ok(())) => {}
None => break, // join_set empty
}
}
maybe_result = rx_ref.recv() => {
if let Some(mut pr) = maybe_result {
let idx = pr.agent.index;
pr.agent.acp.shutdown().await;
tracing::debug!(agent = idx, "reaped checked-out agent on shutdown");
}
// If None, channel closed — tasks are done.
}
}
}
})
.await;
if shutdown_result.is_err() {
tracing::warn!("grace period expired, aborting remaining tasks");
pool.join_set.shutdown().await;
}
// Drain any remaining results that arrived after join_set drained but
// before tasks were aborted.
while let Ok(mut pr) = pool.result_rx_try_recv() {
let idx = pr.agent.index;
pr.agent.acp.shutdown().await;
tracing::debug!(agent = idx, "reaped late-arriving agent on shutdown");
}
// Explicitly shut down idle agents still sitting in their slots.
for slot in pool.agents_mut().iter_mut() {
if let Some(agent) = slot.take() {
let idx = agent.index;
let mut acp = agent.acp;
acp.shutdown().await;
tracing::debug!(agent = idx, "reaped idle agent on shutdown");
}
}
drop(pool);
// Abort any in-flight respawn tasks. They may be sleeping in backoff or
// running spawn_and_init — either way, we don't want them spawning new
// children after the main loop has exited. RespawnGuard::Drop sends a
// failure result for aborted tasks, so respawn_in_flight is cleared.
respawn_tasks.shutdown().await;
// Drain any respawn results that completed before the abort. Explicitly
// shut down returned agents instead of relying on AcpClient::Drop.
while let Ok(rr) = respawn_rx.try_recv() {
if let Ok(mut acp) = rr.result {
acp.shutdown().await;
tracing::debug!(agent = rr.index, "reaped respawned agent on shutdown");
}
}
// Cancel any in-flight presence heartbeat before sending offline.
if let Some(h) = presence_task.take() {
h.abort();
}
// Best-effort: set presence to offline before exiting.
if config.presence_enabled {
match tokio::time::timeout(
Duration::from_secs(2),
publish_presence(&presence_publisher, &presence_keys, "offline"),
)
.await
{
Ok(Ok(_)) => tracing::info!("presence set to offline"),
Ok(Err(e)) => tracing::warn!("failed to set offline presence: {e}"),
Err(_) => tracing::warn!("offline presence timed out"),
}
}
if let Some(handle) = relay_observer_publisher_task.take() {
handle.abort();
}
// Graceful relay shutdown — sends WebSocket close frame and waits up to 5s
// for the background task to finish, rather than aborting immediately (#40).
relay.shutdown().await;
tracing::info!("buzz-acp stopped");
Ok(())
}
// ── Loop control ──────────────────────────────────────────────────────────────
#[derive(PartialEq)]
enum LoopAction {
Continue,
Exit,
}
// ── cancel_in_flight_task ─────────────────────────────────────────────────────
/// Send a cancel signal to the in-flight task for `channel_id`.
/// Returns `true` if a signal was sent, `false` if no in-flight task was found.
fn cancel_in_flight_task(pool: &mut AgentPool, channel_id: uuid::Uuid, mode: CancelMode) -> bool {
let entry = pool
.task_map_mut()
.values_mut()
.find(|m| m.channel_id == Some(channel_id));
if let Some(meta) = entry {
if let Some(tx) = meta.cancel_tx.take() {
let _ = tx.send(mode);
tracing::info!(channel = %channel_id, "cancel signal sent to in-flight task");
return true;
}
}
false
}
// ── dispatch_pending ──────────────────────────────────────────────────────────
/// Flush queued work to available agents.
fn dispatch_pending(
pool: &mut AgentPool,
queue: &mut EventQueue,
ctx: &Arc<PromptContext>,
) -> Vec<(Uuid, ThreadTags)> {
let mut dispatched_channels = Vec::new();
loop {
let batch = match queue.flush_next() {
Some(b) => b,
None => break,
};
let channel_id = batch.channel_id;
let typing_scope = batch
.events
.last()
.map(|event| queue::parse_thread_tags(&event.event))
.unwrap_or_default();
let affinity_hit = pool.has_session_for(channel_id);
let agent = match pool.try_claim(Some(channel_id)) {
Some(a) => a,
None => {
let pending = queue.pending_channels();
tracing::debug!(pending_channels = pending, "pool_exhausted");
queue.requeue_preserve_timestamps(batch);
queue.mark_complete(channel_id);
break;
}
};
tracing::debug!(agent = agent.index, channel = %channel_id, affinity_hit, "agent_claimed");
let recoverable_batch = match ctx.dedup_mode {
DedupMode::Queue => Some(batch.clone()),
DedupMode::Drop => None,
};
let result_tx = pool.result_tx();
let ctx_clone = Arc::clone(ctx);
let agent_index = agent.index;
// Prompt text is now built inside run_prompt_task (needs async for
// context fetching). Pass None for prompt_text; batch carries the data.
let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<CancelMode>();
let abort_handle = pool.join_set.spawn(async move {
pool::run_prompt_task(
agent,
Some(batch),
None,
ctx_clone,
result_tx,
Some(cancel_rx),
)
.await;
});
pool.task_map_mut().insert(
abort_handle.id(),
pool::TaskMeta {
agent_index,
channel_id: Some(channel_id),
recoverable_batch,
cancel_tx: Some(cancel_tx),
},
);
dispatched_channels.push((channel_id, typing_scope));
}
tracing::debug!(
dispatched = dispatched_channels.len(),
queue_depth = queue.pending_channels(),
"dispatch_pending"
);
dispatched_channels
}
// ── handle_prompt_result ──────────────────────────────────────────────────────
#[allow(clippy::too_many_arguments)]
fn handle_prompt_result(
pool: &mut AgentPool,
queue: &mut EventQueue,
config: &Config,
mut result: PromptResult,
heartbeat_in_flight: &mut bool,
removed_channels: &HashSet<Uuid>,
crash_history: &mut [SlotCircuit],
respawn_tx: &mpsc::Sender<RespawnResult>,
respawn_tasks: &mut tokio::task::JoinSet<()>,
observer: Option<observer::ObserverHandle>,
relay: &HarnessRelay,
) -> LoopAction {
let before = pool.task_map().len();
let agent_index = result.agent.index;
pool.task_map_mut()
.retain(|_, meta| meta.agent_index != agent_index);
debug_assert_eq!(before, pool.task_map().len() + 1);
// Extract thread root from the batch before it's consumed by requeue.
// Used by death notices to thread the message into the original conversation.
let thread_root: Option<String> = result
.batch
.as_ref()
.and_then(|b| b.events.first())
.map(|e| queue::parse_thread_tags(&e.event))
.and_then(|tags| tags.root_event_id);
// Requeue BEFORE mark_complete: requeue() sets retry_after with a future
// deadline, and mark_complete() checks for it to decide whether to preserve
// retry_counts. If mark_complete runs first, retry_counts is cleared and
// every retry starts at attempt 1 — defeating exponential backoff and
// dead-letter protection.
if let Some(batch) = result.batch {
// Don't requeue batches for channels the agent was removed from —
// those events are stale and should be silently dropped.
if !removed_channels.contains(&batch.channel_id) {
if matches!(result.outcome, PromptOutcome::Cancelled) {
// Cancel re-prompt: store as cancelled events so flush_next()
// merges them into the next FlushBatch.cancelled_events,
// enabling the annotated merged-prompt format.
queue.requeue_as_cancelled(batch);
} else {
queue.requeue(batch);
}
} else {
tracing::debug!(
channel_id = %batch.channel_id,
events = batch.events.len(),
"dropping failed batch for removed channel"
);
}
}
match &result.source {
PromptSource::Channel(ch) => queue.mark_complete(*ch),
PromptSource::Heartbeat => *heartbeat_in_flight = false,
}
// Strip sessions for channels the agent was removed from while this
// agent was checked out. This covers the gap where invalidate_channel_sessions
// only touches idle agents.
for ch in removed_channels {
result.agent.state.invalidate_channel(ch);
}
let outcome_label = match &result.outcome {
PromptOutcome::Ok(_) => "ok",
PromptOutcome::Error(_) => "error",
PromptOutcome::Timeout => "timeout",
PromptOutcome::AgentExited => "exited",
PromptOutcome::Cancelled => "cancelled",
};
let agent_index = result.agent.index;
let channel_id = match &result.source {
PromptSource::Channel(ch) => Some(*ch),
PromptSource::Heartbeat => None,
};
let emit_turn_error = |error_msg: &str| {
if let Some(ref observer) = observer {
observer.emit(
"turn_error",
Some(agent_index),
&observer::context_for(channel_id, None, None),
serde_json::json!({
"outcome": outcome_label,
"error": error_msg,
}),
);
}
};
match result.outcome {
// Successful prompt — return agent to pool.
PromptOutcome::Ok(_) => {
tracing::debug!(
agent = agent_index,
outcome = outcome_label,
"agent_returned"
);
pool.return_agent(result.agent);
}
// Fatal outcomes: the agent subprocess is dead or poisoned — respawn it.
PromptOutcome::AgentExited | PromptOutcome::Timeout => {
tracing::warn!(
agent = agent_index,
outcome = outcome_label,
"agent_returned — respawning"
);
let death_message = match outcome_label {
"exited" => "Agent process exited unexpectedly",
_ => "Agent session timed out due to inactivity",
};
emit_turn_error(death_message);
// Post a visible death notice to the channel so humans know why
// the agent went silent.
if let Some(ch) = channel_id {
relay.publish_death_notice(ch, death_message, thread_root.as_deref());
}
let index = result.agent.index;
let slot_history = &mut crash_history[index];
if !spawn_respawn_task(
result.agent,
config,
slot_history,
respawn_tx,
respawn_tasks,
observer.clone(),
) {
// Circuit open — slot stays empty until maintenance refill.
if pool.live_count() == 0 && !any_respawn_in_flight(crash_history) {
tracing::error!("all agents dead — exiting");
return LoopAction::Exit;
}
}
}
// Errors fall into two categories:
//
// 1. Transport-class (Io, WriteTimeout, Timeout, Protocol): the stdio
// pipe may be corrupted or the agent desynchronized. These are fatal
// to the agent regardless of whether they occurred during session
// creation or an active prompt — respawn unconditionally.
//
// 2. Application-class (IdleTimeout, HardTimeout, Json): the pipe is
// intact but the prompt failed. Return the agent to the pool so it
// can be reused for the next event.
// Intentional cancel — agent is healthy, return it to the pool.
// No respawn, no retry penalty. The cancelled batch was already stored
// via requeue_as_cancelled() above and will be merged into the next
// FlushBatch by flush_next().
PromptOutcome::Cancelled => {
tracing::debug!(
agent = agent_index,
outcome = outcome_label,
"agent_returned (cancelled)"
);
pool.return_agent(result.agent);
}
PromptOutcome::Error(ref e) => {
let is_transport_error = matches!(
e,
acp::AcpError::Io(_)
| acp::AcpError::WriteTimeout(_)
| acp::AcpError::Timeout(_)
| acp::AcpError::Protocol(_)
);
if is_transport_error {
tracing::warn!(
agent = agent_index,
outcome = outcome_label,
error = %e,
"transport/protocol error — respawning agent"
);
emit_turn_error(&e.to_string());
// Post a visible death notice for transport errors too.
if let Some(ch) = channel_id {
relay.publish_death_notice(
ch,
"Agent connection lost (transport error)",
thread_root.as_deref(),
);
}
let index = result.agent.index;
let slot_history = &mut crash_history[index];
if !spawn_respawn_task(
result.agent,
config,
slot_history,
respawn_tx,
respawn_tasks,
observer,
) && pool.live_count() == 0
&& !any_respawn_in_flight(crash_history)
{
tracing::error!("all agents dead — exiting");
return LoopAction::Exit;
}
} else {
tracing::warn!(
agent = agent_index,
outcome = outcome_label,
error = %e,
"agent_returned (application error — pipe intact)"
);
emit_turn_error(&e.to_string());
pool.return_agent(result.agent);
}
}
}
LoopAction::Continue
}
// ── recover_panicked_agent ────────────────────────────────────────────────────
#[allow(clippy::too_many_arguments)]
fn recover_panicked_agent(
pool: &mut AgentPool,
queue: &mut EventQueue,
config: &Config,
join_error: tokio::task::JoinError,
heartbeat_in_flight: &mut bool,
removed_channels: &HashSet<Uuid>,
typing_channels: &mut HashMap<Uuid, ThreadTags>,
crash_history: &mut [SlotCircuit],
respawn_tx: &mpsc::Sender<RespawnResult>,
respawn_tasks: &mut tokio::task::JoinSet<()>,
observer: Option<observer::ObserverHandle>,
) {
let task_id = join_error.id();
let Some(meta) = pool.task_map_mut().remove(&task_id) else {
tracing::error!("panic for unknown task {task_id:?} — bug");
return;
};
let i = meta.agent_index;
// Requeue BEFORE mark_complete (same rationale as handle_prompt_result).
if let Some(batch) = meta.recoverable_batch {
if let Some(ch) = meta.channel_id {
if !removed_channels.contains(&ch) {
queue.requeue(batch);
tracing::warn!("requeued batch for panicked agent {i}");
} else {
tracing::debug!(
channel_id = %ch,
"dropping panicked batch for removed channel"
);
}
}
}
if let Some(ch) = meta.channel_id {
queue.mark_complete(ch);
typing_channels.remove(&ch);
tracing::warn!("cleared wedged in-flight channel {ch} from panicked agent {i}");
} else {
*heartbeat_in_flight = false;
tracing::warn!("cleared wedged heartbeat_in_flight from panicked agent {i}");
}
if let Some(ref observer) = observer {
observer.emit(
"agent_panic",
Some(i),
&observer::context_for(meta.channel_id, None, None),
serde_json::json!({
"outcome": "panic",
"error": format!("Agent task panicked: {join_error}"),
}),
);
}
// Panics count as crashes for the circuit breaker.
// The panicked task already dropped the AcpClient, so we just need to
// check the circuit and spawn a fresh agent in the background.
let slot = &mut crash_history[i];
let delay = match slot.record_crash() {
CrashVerdict::CircuitOpen => {
tracing::error!(agent = i, "circuit open after panic — not respawning");
return;
}
CrashVerdict::HalfOpenProbe => {
tracing::info!(agent = i, "circuit half-open — probe respawn after panic");
Duration::ZERO
}
CrashVerdict::Respawn(d) => {
tracing::info!(
agent = i,
delay_ms = d.as_millis(),
"respawn backoff after panic"
);
d
}
};
// Spawn respawn work off the main loop.
slot.respawn_in_flight = true;
let cmd = config.agent_command.clone();
let args = config.agent_args.clone();
let env = config.persona_env_vars.clone();
let guard = RespawnGuard::new(i, respawn_tx.clone());
respawn_tasks.spawn(async move {
if !delay.is_zero() {
tokio::time::sleep(delay).await;
}
let result = spawn_and_init(&cmd, &args, &env, i, observer).await;
guard.send(result);
});
}
// ── drain_ready_join_results ──────────────────────────────────────────────────
#[allow(clippy::too_many_arguments)]
fn drain_ready_join_results(
pool: &mut AgentPool,
queue: &mut EventQueue,
config: &Config,
heartbeat_in_flight: &mut bool,
removed_channels: &HashSet<Uuid>,
typing_channels: &mut HashMap<Uuid, ThreadTags>,
crash_history: &mut [SlotCircuit],
respawn_tx: &mpsc::Sender<RespawnResult>,
respawn_tasks: &mut tokio::task::JoinSet<()>,
observer: Option<observer::ObserverHandle>,
) -> LoopAction {
while let Some(Some(join_result)) = pool.join_set.join_next().now_or_never() {
if let Err(join_error) = join_result {
tracing::error!("agent task panicked: {join_error}");
recover_panicked_agent(
pool,
queue,
config,
join_error,
heartbeat_in_flight,
removed_channels,
typing_channels,
crash_history,
respawn_tx,
respawn_tasks,
observer.clone(),
);
if pool.live_count() == 0 && !any_respawn_in_flight(crash_history) {
return LoopAction::Exit;
}
}
}
LoopAction::Continue
}
// ── dispatch_heartbeat ────────────────────────────────────────────────────────
fn dispatch_heartbeat(
pool: &mut AgentPool,
ctx: &Arc<PromptContext>,
heartbeat_in_flight: &mut bool,
) {
if *heartbeat_in_flight {
return;
}
let agent = match pool.try_claim(None) {
Some(a) => a,
None => return,
};
let prompt_text = ctx
.heartbeat_prompt
.clone()
.unwrap_or_else(default_heartbeat_prompt);
let prompt_text = match ctx.base_prompt {
Some(bp) => prepend_base_prompt(bp, &prompt_text),
None => prompt_text,
};
let result_tx = pool.result_tx();
let ctx_clone = Arc::clone(ctx);
let agent_index = agent.index;
let abort_handle = pool.join_set.spawn(async move {
pool::run_prompt_task(agent, None, Some(prompt_text), ctx_clone, result_tx, None).await;
});
pool.task_map_mut().insert(
abort_handle.id(),
pool::TaskMeta {
agent_index,
channel_id: None,
recoverable_batch: None,
cancel_tx: None,
},
);
*heartbeat_in_flight = true;
tracing::info!(agent = agent_index, "heartbeat_fired");
}
// ── default_heartbeat_prompt ──────────────────────────────────────────────────
fn default_heartbeat_prompt() -> String {
let now = chrono::Utc::now().to_rfc3339();
format!(
"[System: Heartbeat]\nTime: {now}\n\n\
You have been awakened for a routine heartbeat. You have NO incoming messages or\n\
active channel context for this turn.\n\n\
Your tasks:\n\
1. Run `buzz feed get --types needs_action` to check for pending workflow approvals or\n\
high-priority requests addressed to you.\n\
2. Run `buzz feed get --types mentions` to check for unanswered @mentions.\n\
3. If you find actionable items, address them using the appropriate CLI commands\n\
(e.g., `buzz workflows approve --token <UUID>`, `buzz messages send`,\n\
`buzz messages send --reply-to <event-id>`).\n\
4. If there are no pending actions or mentions, end your turn immediately.\n\n\
Do not run `buzz channels list` or `buzz messages search` unless you have a specific reason.\n\
Do not invent work — only act on items surfaced by the feed commands."
)
}
// ── respawn_agent_into ────────────────────────────────────────────────────────
/// Spawn a background respawn task for a crashed agent slot.
///
/// Does the circuit breaker check synchronously (non-blocking), then spawns
/// the actual shutdown + backoff + spawn_and_init work into a background task.
/// The result comes back through `respawn_tx` so the main loop stays responsive.
///
/// Returns `true` if a respawn task was spawned, `false` if the circuit is open.
fn spawn_respawn_task(
old_agent: OwnedAgent,
config: &Config,
slot: &mut SlotCircuit,
respawn_tx: &mpsc::Sender<RespawnResult>,
respawn_tasks: &mut tokio::task::JoinSet<()>,
observer: Option<observer::ObserverHandle>,
) -> bool {
let index = old_agent.index;
// Circuit breaker: record crash, decide whether to respawn.
let delay = match slot.record_crash() {
CrashVerdict::CircuitOpen => {
tracing::error!(agent = index, "circuit open — not respawning");
return false;
}
CrashVerdict::HalfOpenProbe => {
tracing::info!(agent = index, "circuit half-open — probe respawn");
Duration::ZERO
}
CrashVerdict::Respawn(d) => {
tracing::info!(agent = index, delay_ms = d.as_millis(), "respawn backoff");
d
}
};
slot.respawn_in_flight = true;
// Spawn the actual work (shutdown + sleep + spawn + init) off the main loop.
let cmd = config.agent_command.clone();
let args = config.agent_args.clone();
let env = config.persona_env_vars.clone();
let guard = RespawnGuard::new(index, respawn_tx.clone());
respawn_tasks.spawn(async move {
// Shutdown old agent (reap child, prevent zombie).
let mut agent = old_agent;
agent.acp.shutdown().await;
drop(agent);
if !delay.is_zero() {
tokio::time::sleep(delay).await;
}
let result = spawn_and_init(&cmd, &args, &env, index, observer).await;
guard.send(result);
});
true
}
// ── spawn_and_init ────────────────────────────────────────────────────────────
/// Spawn an agent subprocess and run the MCP `initialize` handshake.
///
/// Takes owned args so it can run in a background `tokio::spawn` task without
/// borrowing `Config`. All respawn/refill paths use this.
async fn spawn_and_init(
command: &str,
args: &[String],
extra_env: &[(String, String)],
agent_index: usize,
observer: Option<observer::ObserverHandle>,
) -> Result<AcpClient> {
let mut acp = AcpClient::spawn(command, args, extra_env)
.await
.map_err(|e| anyhow::anyhow!("failed to spawn agent: {e}"))?;
acp.set_observer(observer, agent_index);
match acp.initialize().await {
Ok(init_result) => {
tracing::info!("agent initialized: {init_result}");
acp.observe(
"agent_initialized",
serde_json::json!({
"agentIndex": agent_index,
"initializeResult": init_result,
}),
);
Ok(acp)
}
Err(e) => {
// Explicitly shut down the spawned child to prevent zombie/leak.
// Drop only does start_kill + try_wait (best-effort); shutdown()
// does start_kill + bounded wait (guaranteed reap).
acp.shutdown().await;
Err(anyhow::anyhow!("agent initialize failed: {e}"))
}
}
}
// ── build_mcp_servers ─────────────────────────────────────────────────────────
// ── run_models ─────────────────────────────────────────────────────────────────
/// `buzz-acp models` — spawn an agent, query its available models, exit.
///
/// Flow: spawn → initialize → session/new → print models → shutdown.
/// No relay connection, no MCP servers, no subscriptions. ~2-5s total.
async fn run_models(args: ModelsArgs) -> Result<()> {
use acp::{extract_model_config_options, extract_model_state};
let agent_args = config::normalize_agent_args(&args.agent_command, args.agent_args);
let cwd = std::env::current_dir()
.unwrap_or_else(|_| std::path::PathBuf::from("/"))
.to_string_lossy()
.to_string();
// Spawn outside the timeout so we always own the child for cleanup.
// `models` subcommand doesn't use persona packs — no extra env.
let mut client = match AcpClient::spawn(&args.agent_command, &agent_args, &[]).await {
Ok(c) => c,
Err(e) => {
eprintln!("error: failed to spawn agent: {e}");
std::process::exit(1);
}
};
// Initialize + session/new under a timeout. Client is owned above,
// so shutdown() runs on all paths (success, error, timeout).
let protocol_result = tokio::time::timeout(MODELS_TIMEOUT, async {
let init = client.initialize().await?;
let session = client.session_new_full(&cwd, vec![]).await?;
Ok::<_, acp::AcpError>((init, session))
})
.await;
let (init_result, session_resp) = match protocol_result {
Ok(Ok(tuple)) => tuple,
Ok(Err(e)) => {
client.shutdown().await;
eprintln!("error: agent communication failed: {e}");
std::process::exit(1);
}
Err(_) => {
client.shutdown().await;
eprintln!("error: agent timed out ({MODELS_TIMEOUT:?})");
std::process::exit(1);
}
};
// Extract agent info from initialize response.
// ACP spec uses "serverInfo" (MCP heritage); some agents may use "agentInfo".
let info_obj = init_result
.get("serverInfo")
.or_else(|| init_result.get("agentInfo"));
let agent_name = info_obj
.and_then(|ai| ai.get("name"))
.and_then(|v| v.as_str())
.unwrap_or("unknown");
let agent_version = info_obj
.and_then(|ai| ai.get("version"))
.and_then(|v| v.as_str())
.unwrap_or("unknown");
// Extract model info from session/new response.
let config_options = extract_model_config_options(&session_resp.raw);
let model_state = extract_model_state(&session_resp.raw);
if args.json {
// Structured JSON output — consumed by Phase 3 `get_agent_models`.
let output = serde_json::json!({
"agent": {
"name": agent_name,
"version": agent_version,
},
"stable": {
"configOptions": config_options,
},
"unstable": model_state.as_ref().map(|ms| serde_json::json!({
"currentModelId": ms.get("currentModelId"),
"availableModels": ms.get("availableModels"),
})),
});
println!("{}", serde_json::to_string_pretty(&output)?);
} else {
// Human-readable output.
println!("Agent: {} v{}", agent_name, agent_version);
println!();
let mut has_models = false;
if !config_options.is_empty() {
println!("Models (stable configOptions):");
for opt in &config_options {
let config_id = opt.get("configId").and_then(|v| v.as_str()).unwrap_or("?");
let display = opt
.get("displayName")
.and_then(|v| v.as_str())
.unwrap_or(config_id);
println!(" {display} (configId: {config_id})");
if let Some(options) = opt.get("options").and_then(|v| v.as_array()) {
for o in options {
let val = o.get("value").and_then(|v| v.as_str()).unwrap_or("?");
let name = o.get("displayName").and_then(|v| v.as_str()).unwrap_or(val);
println!(" - {name} (value: {val})");
}
}
}
has_models = true;
}
if let Some(ref ms) = model_state {
let current = ms
.get("currentModelId")
.and_then(|v| v.as_str())
.unwrap_or("(none)");
println!("Models (unstable SessionModelState):");
println!(" Current: {current}");
if let Some(available) = ms.get("availableModels").and_then(|v| v.as_array()) {
println!(" Available:");
for m in available {
let id = m.get("modelId").and_then(|v| v.as_str()).unwrap_or("?");
let name = m.get("name").and_then(|v| v.as_str()).unwrap_or(id);
let desc = m.get("description").and_then(|v| v.as_str()).unwrap_or("");
if desc.is_empty() {
println!(" - {name} (id: {id})");
} else {
println!(" - {name} (id: {id}) — {desc}");
}
}
}
has_models = true;
}
if !has_models {
println!("No model information available from this agent.");
}
}
client.shutdown().await;
Ok(())
}
fn build_mcp_servers(config: &Config) -> Vec<McpServer> {
if config.mcp_command.is_empty() {
return vec![];
}
vec![McpServer {
name: std::path::Path::new(&config.mcp_command)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("mcp")
.to_string(),
command: config.mcp_command.clone(),
args: vec![],
env: {
let mut env = vec![
EnvVar {
name: "BUZZ_RELAY_URL".into(),
value: config.relay_url.clone(),
},
EnvVar {
name: "BUZZ_PRIVATE_KEY".into(),
// bech32 encoding of a valid secret key is infallible.
// Panic here is correct: injecting a bogus secret would cause
// delayed, hard-to-diagnose agent failures downstream.
value: config
.keys
.secret_key()
.to_bech32()
.expect("secret key bech32 encoding should never fail"),
},
];
// Forward BUZZ_AUTH_TAG (NIP-OA owner attestation credential)
// so the MCP server can attach it to every signed event.
if let Ok(auth_tag) = std::env::var("BUZZ_AUTH_TAG") {
if !auth_tag.is_empty() {
env.push(EnvVar {
name: "BUZZ_AUTH_TAG".into(),
value: auth_tag,
});
}
}
env
},
}]
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod owner_cache_tests {
use super::*;
#[test]
fn new_with_some_caches_immediately() {
let cache = OwnerCache::new(Some("abcd".into()));
assert_eq!(cache.get(), Some("abcd"));
}
#[test]
fn new_with_none_returns_none() {
let cache = OwnerCache::new(None);
assert!(cache.get().is_none());
}
#[test]
fn get_returns_cached_value() {
let cache = OwnerCache::new(Some("ab".repeat(32)));
assert_eq!(cache.get(), Some("ab".repeat(32)).as_deref());
}
}
#[cfg(test)]
mod observer_chunk_coalescer_tests {
use super::*;
fn chunk_event(
seq: u64,
update_type: &str,
message_id: &str,
text: &str,
) -> observer::ObserverEvent {
observer::ObserverEvent {
seq,
timestamp: format!("2026-04-29T04:00:0{seq}Z"),
kind: "acp_read".to_string(),
agent_index: Some(0),
channel_id: Some("channel-1".to_string()),
session_id: Some("session-1".to_string()),
turn_id: Some("turn-1".to_string()),
payload: serde_json::json!({
"jsonrpc": "2.0",
"method": "session/update",
"params": {
"sessionId": "session-1",
"update": {
"sessionUpdate": update_type,
"messageId": message_id,
"content": {
"type": "text",
"text": text,
},
},
},
}),
}
}
fn non_chunk_event(seq: u64) -> observer::ObserverEvent {
observer::ObserverEvent {
seq,
timestamp: format!("2026-04-29T04:00:0{seq}Z"),
kind: "turn_started".to_string(),
agent_index: Some(0),
channel_id: Some("channel-1".to_string()),
session_id: Some("session-1".to_string()),
turn_id: Some("turn-1".to_string()),
payload: serde_json::json!({ "type": "turn_started" }),
}
}
fn chunk_text(event: &observer::ObserverEvent) -> &str {
event.payload["params"]["update"]["content"]["text"]
.as_str()
.expect("chunk text")
}
#[test]
fn coalesces_chunks_until_non_chunk_event() {
let mut coalescer = ObserverChunkCoalescer::default();
assert!(coalescer
.ingest(chunk_event(1, "agent_message_chunk", "message-1", "hello "))
.is_empty());
assert!(coalescer
.ingest(chunk_event(2, "agent_message_chunk", "message-1", "world"))
.is_empty());
let events = coalescer.ingest(non_chunk_event(3));
assert_eq!(events.len(), 2);
assert_eq!(events[0].seq, 2);
assert_eq!(chunk_text(&events[0]), "hello world");
assert_eq!(events[1].kind, "turn_started");
}
#[test]
fn keeps_independent_chunk_streams_separate() {
let mut coalescer = ObserverChunkCoalescer::default();
assert!(coalescer
.ingest(chunk_event(1, "agent_message_chunk", "message-1", "answer"))
.is_empty());
assert!(coalescer
.ingest(chunk_event(
2,
"agent_thought_chunk",
"thought-1",
"thinking"
))
.is_empty());
let events = coalescer.flush();
assert_eq!(events.len(), 2);
assert_eq!(chunk_text(&events[0]), "answer");
assert_eq!(chunk_text(&events[1]), "thinking");
}
}
#[cfg(test)]
mod build_mcp_servers_tests {
use super::*;
use std::sync::Mutex;
/// Env-var-touching tests must run serially — env vars are process-global.
static ENV_LOCK: Mutex<()> = Mutex::new(());
fn test_config() -> Config {
Config {
keys: nostr::Keys::generate(),
relay_url: "ws://localhost:3000".into(),
agent_command: "goose".into(),
agent_args: vec!["acp".into()],
mcp_command: "test-mcp-server".into(),
idle_timeout_secs: config::DEFAULT_IDLE_TIMEOUT_SECS,
max_turn_duration_secs: 3600,
agents: 1,
heartbeat_interval_secs: 0,
heartbeat_prompt: None,
system_prompt: None,
initial_message: None,
subscribe_mode: config::SubscribeMode::All,
dedup_mode: config::DedupMode::Queue,
multiple_event_handling: config::MultipleEventHandling::Queue,
ignore_self: true,
kinds_override: None,
channels_override: None,
no_mention_filter: false,
config_path: std::path::PathBuf::from("./buzz-acp.toml"),
context_message_limit: 12,
max_turns_per_session: 0,
presence_enabled: true,
typing_enabled: true,
memory_enabled: false,
model: None,
permission_mode: config::PermissionMode::BypassPermissions,
respond_to: config::RespondTo::Anyone,
respond_to_allowlist: std::collections::HashSet::new(),
persona_env_vars: vec![],
relay_observer: false,
agent_owner: None,
no_base_prompt: false,
base_prompt_content: None,
}
}
#[test]
fn session_new_mcp_server_has_required_fields() {
let config = test_config();
let servers = build_mcp_servers(&config);
assert_eq!(servers.len(), 1);
let server = &servers[0];
assert_eq!(server.name, "test-mcp-server");
let names: Vec<&str> = server.env.iter().map(|e| e.name.as_str()).collect();
assert!(
names.contains(&"BUZZ_RELAY_URL"),
"missing BUZZ_RELAY_URL; got {names:?}"
);
assert!(
names.contains(&"BUZZ_PRIVATE_KEY"),
"missing BUZZ_PRIVATE_KEY; got {names:?}"
);
}
#[test]
fn session_new_mcp_server_forwards_buzz_auth_tag() {
let _guard = ENV_LOCK.lock().unwrap();
std::env::set_var("BUZZ_AUTH_TAG", "test-attestation-tag");
let config = test_config();
let servers = build_mcp_servers(&config);
std::env::remove_var("BUZZ_AUTH_TAG");
let server = &servers[0];
let auth_tag_env = server.env.iter().find(|e| e.name == "BUZZ_AUTH_TAG");
assert!(
auth_tag_env.is_some(),
"BUZZ_AUTH_TAG should be forwarded when set"
);
assert_eq!(auth_tag_env.unwrap().value, "test-attestation-tag");
}
#[test]
fn session_new_mcp_server_skips_empty_buzz_auth_tag() {
let _guard = ENV_LOCK.lock().unwrap();
std::env::set_var("BUZZ_AUTH_TAG", "");
let config = test_config();
let servers = build_mcp_servers(&config);
std::env::remove_var("BUZZ_AUTH_TAG");
let server = &servers[0];
let has_auth_tag = server.env.iter().any(|e| e.name == "BUZZ_AUTH_TAG");
assert!(!has_auth_tag, "empty BUZZ_AUTH_TAG should not be forwarded");
}
#[test]
fn empty_mcp_command_returns_no_servers() {
let mut config = test_config();
config.mcp_command = "".into();
let servers = build_mcp_servers(&config);
assert!(
servers.is_empty(),
"empty mcp_command should produce no MCP servers"
);
}
#[test]
fn absolute_path_mcp_command_uses_file_stem_as_name() {
let mut config = test_config();
config.mcp_command = "/opt/bin/my-mcp-server".into();
let servers = build_mcp_servers(&config);
assert_eq!(servers.len(), 1);
assert_eq!(servers[0].name, "my-mcp-server");
}
#[test]
fn mcp_command_with_no_stem_falls_back_to_mcp() {
// Path::new("").file_stem() returns None — exercises the unwrap_or("mcp") path.
let mut config = test_config();
config.mcp_command = "".into();
// Empty command returns no servers; test the stem logic directly.
assert_eq!(
std::path::Path::new("")
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("mcp"),
"mcp"
);
// Confirm a non-empty command with no stem (e.g. just a dot) also falls back.
config.mcp_command = ".".into();
let servers = build_mcp_servers(&config);
assert_eq!(servers.len(), 1);
assert_eq!(
servers[0].name, "mcp",
"Path::new(\".\").file_stem() is None — should fall back to \"mcp\""
);
}
}