mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
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>
145 lines
4.2 KiB
Rust
145 lines
4.2 KiB
Rust
//! In-process observer bus for ACP session activity.
|
|
//!
|
|
//! This is intentionally process-local infrastructure: it lets the harness
|
|
//! collect raw ACP JSON-RPC activity and publish owner-scoped encrypted relay
|
|
//! frames without exposing a local HTTP port.
|
|
|
|
use std::{
|
|
collections::VecDeque,
|
|
sync::{
|
|
atomic::{AtomicU64, Ordering},
|
|
Arc, Mutex,
|
|
},
|
|
};
|
|
|
|
use serde::Serialize;
|
|
use tokio::sync::broadcast;
|
|
|
|
const OBSERVER_BUFFER_CAP: usize = 1_000;
|
|
|
|
/// Best-effort metadata attached to observer events.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct ObserverContext {
|
|
/// Buzz channel UUID for the current turn, when channel-scoped.
|
|
pub channel_id: Option<String>,
|
|
/// ACP session ID associated with the current turn, once known.
|
|
pub session_id: Option<String>,
|
|
/// Local UUID for one prompt turn.
|
|
pub turn_id: Option<String>,
|
|
}
|
|
|
|
/// Handle used by the harness to publish local observer events.
|
|
#[derive(Clone)]
|
|
pub struct ObserverHandle {
|
|
inner: Arc<ObserverInner>,
|
|
}
|
|
|
|
struct ObserverInner {
|
|
tx: broadcast::Sender<ObserverEvent>,
|
|
buffer: Mutex<VecDeque<ObserverEvent>>,
|
|
seq: AtomicU64,
|
|
}
|
|
|
|
fn new_observer_handle() -> ObserverHandle {
|
|
let (tx, _) = broadcast::channel(OBSERVER_BUFFER_CAP);
|
|
ObserverHandle {
|
|
inner: Arc::new(ObserverInner {
|
|
tx,
|
|
buffer: Mutex::new(VecDeque::with_capacity(OBSERVER_BUFFER_CAP)),
|
|
seq: AtomicU64::new(1),
|
|
}),
|
|
}
|
|
}
|
|
|
|
/// Event delivered through the in-process observer bus.
|
|
#[derive(Clone, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ObserverEvent {
|
|
/// Monotonic process-local sequence number.
|
|
pub seq: u64,
|
|
/// RFC3339 UTC timestamp.
|
|
pub timestamp: String,
|
|
/// Observer event kind, for example `acp_read` or `turn_started`.
|
|
pub kind: String,
|
|
/// Pool slot index for the agent process that emitted the event.
|
|
pub agent_index: Option<usize>,
|
|
/// Buzz channel UUID for channel-scoped events.
|
|
pub channel_id: Option<String>,
|
|
/// ACP session ID when known.
|
|
pub session_id: Option<String>,
|
|
/// Local UUID for one prompt turn.
|
|
pub turn_id: Option<String>,
|
|
/// Raw or semantic event payload.
|
|
pub payload: serde_json::Value,
|
|
}
|
|
|
|
impl ObserverHandle {
|
|
/// Create an in-process observer feed.
|
|
pub fn in_process() -> Self {
|
|
new_observer_handle()
|
|
}
|
|
|
|
/// Subscribe to live observer events.
|
|
pub fn subscribe(&self) -> broadcast::Receiver<ObserverEvent> {
|
|
self.inner.tx.subscribe()
|
|
}
|
|
|
|
/// Return the current replay buffer.
|
|
pub fn snapshot(&self) -> Vec<ObserverEvent> {
|
|
match self.inner.buffer.lock() {
|
|
Ok(buffer) => buffer.iter().cloned().collect(),
|
|
Err(error) => {
|
|
tracing::warn!(target: "observer", "observer replay buffer lock poisoned: {error}");
|
|
Vec::new()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Emit a local observer event.
|
|
pub fn emit(
|
|
&self,
|
|
kind: impl Into<String>,
|
|
agent_index: Option<usize>,
|
|
context: &ObserverContext,
|
|
payload: serde_json::Value,
|
|
) {
|
|
let event = ObserverEvent {
|
|
seq: self.inner.seq.fetch_add(1, Ordering::Relaxed),
|
|
timestamp: chrono::Utc::now().to_rfc3339(),
|
|
kind: kind.into(),
|
|
agent_index,
|
|
channel_id: context.channel_id.clone(),
|
|
session_id: context.session_id.clone(),
|
|
turn_id: context.turn_id.clone(),
|
|
payload,
|
|
};
|
|
|
|
match self.inner.buffer.lock() {
|
|
Ok(mut buffer) => {
|
|
if buffer.len() >= OBSERVER_BUFFER_CAP {
|
|
buffer.pop_front();
|
|
}
|
|
buffer.push_back(event.clone());
|
|
}
|
|
Err(error) => {
|
|
tracing::warn!(target: "observer", "observer replay buffer lock poisoned: {error}");
|
|
}
|
|
}
|
|
|
|
let _ = self.inner.tx.send(event);
|
|
}
|
|
}
|
|
|
|
/// Build observer context values from optional channel/session/turn IDs.
|
|
pub fn context_for(
|
|
channel_id: Option<uuid::Uuid>,
|
|
session_id: Option<String>,
|
|
turn_id: Option<String>,
|
|
) -> ObserverContext {
|
|
ObserverContext {
|
|
channel_id: channel_id.map(|id| id.to_string()),
|
|
session_id,
|
|
turn_id,
|
|
}
|
|
}
|