This repository has been archived on 2026-10-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
maestro/crates/maestro-registry/src/state.rs
T
naps62-yoloandClaude Opus 4.7 18bd385a2a
ci / rust (push) Successful in 48s
feat(maestro): registry HTTP API, cookie auth, JWT issuance, events WS
Adds the client-facing surface on the registry (NAP-215). Endpoints:

- GET /api/daemons — list connected daemons (with daemon_url)
- GET /api/sessions — flat session catalog
- POST /api/sessions — forwards CreateSession to chosen daemon, 504 on
  10s daemon timeout
- DELETE /api/sessions/:id — forwards KillSession
- GET /api/sessions/:id/connect-info — returns {daemon_url, jwt} with a
  5min HS256-signed attach JWT
- WS /api/events — snapshot + delta broadcast of catalog and presence
- GET / placeholder served until NAP-218 web bundle lands

Cookie auth middleware mints a long-lived httpOnly JWT cookie on first
request and reissues silently on missing/tampered cookies. Per spec the
perimeter is the operator's network — the cookie is a second factor.

Protocol additions: daemon_url on Hello, DaemonInfo, ConnectInfo,
RegistryEvent. Daemon config + RegistryClient now carry daemon_url.

Registry state now exposes an event broadcast channel (subscribe gives
a snapshot + tokio::sync::broadcast::Receiver) and helper lookups
daemon_for_session / daemon_url.

Tests: 11 reqwest-driven HTTP integration tests cover every endpoint,
the cookie flow, 504 timeout path, worktree forwarding, and events WS
propagation.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 17:08:41 +00:00

441 lines
15 KiB
Rust

//! In-memory registry state shared across all daemon connections.
//!
//! The state holds:
//!
//! - one [`DaemonEntry`] per connected daemon, keyed by `daemon_id`
//! - one entry in the session catalog per `(daemon_id, session_id)` pair
//! - one pending-reply oneshot per outstanding R→D RPC
//!
//! Concurrent access is guarded by a single `Mutex` — the registry handles a
//! handful of daemons and these critical sections are tiny.
use std::collections::{BTreeMap, HashMap};
use std::sync::Arc;
use std::time::Duration;
use maestro_protocol::{
CreateSessionRequest, DaemonInbound, DaemonInfo, ErrorInfo, RegistryEvent, ReplyTo, RpcResult,
Session,
};
use tokio::sync::{broadcast, mpsc, oneshot, Mutex};
use uuid::Uuid;
/// Capacity of the registry-wide broadcast channel that feeds
/// `WS /api/events` subscribers. Subscribers that fall this far behind get a
/// `Lagged` event and are expected to resync via `GET /api/sessions`.
const EVENT_CHANNEL_CAPACITY: usize = 256;
/// How long a daemon can go without sending us *any* message before it is
/// considered offline and its sessions are dropped from the catalog.
pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(30);
/// How long we wait for a `CreateSessionResult` / `KillSessionResult` after
/// sending the matching R→D request. Spec §"Wire protocol (D↔R)".
pub const RPC_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
#[error("daemon `{0}` is not connected")]
DaemonNotConnected(String),
#[error("daemon outbound channel closed before send")]
DaemonGone,
#[error("rpc reply channel closed before reply arrived")]
ReplyChannelClosed,
#[error("rpc timed out after {0:?}")]
Timeout(Duration),
}
/// Distinct error type for RPC failures bubbled up by the daemon. Separated
/// from [`RegistryError`] so callers can distinguish transport from daemon-
/// reported faults.
#[derive(Debug, thiserror::Error)]
#[error("daemon rpc failed: {0:?}")]
pub struct RpcError(pub ErrorInfo);
/// Public, read-only view of a single connected daemon. Convertible to the
/// protocol-level `DaemonInfo` for inclusion in HTTP responses and events.
#[derive(Debug, Clone)]
pub struct DaemonView {
pub daemon_id: String,
pub hostname: String,
pub version: String,
pub daemon_url: String,
}
impl From<DaemonView> for DaemonInfo {
fn from(v: DaemonView) -> Self {
Self {
daemon_id: v.daemon_id,
hostname: v.hostname,
version: v.version,
daemon_url: v.daemon_url,
}
}
}
/// Shared, cloneable registry handle.
#[derive(Debug, Clone)]
pub struct Registry {
inner: Arc<Mutex<State>>,
events: broadcast::Sender<RegistryEvent>,
}
#[derive(Debug, Default)]
struct State {
daemons: HashMap<String, DaemonEntry>,
/// `(daemon_id, session_id) -> Session`.
sessions: BTreeMap<(String, Uuid), Session>,
pending_create: HashMap<ReplyTo, oneshot::Sender<RpcResult<Session>>>,
pending_kill: HashMap<ReplyTo, oneshot::Sender<RpcResult<()>>>,
}
#[derive(Debug)]
struct DaemonEntry {
hostname: String,
version: String,
daemon_url: String,
outbound: mpsc::Sender<DaemonInbound>,
}
impl DaemonEntry {
fn to_view(&self, daemon_id: &str) -> DaemonView {
DaemonView {
daemon_id: daemon_id.to_string(),
hostname: self.hostname.clone(),
version: self.version.clone(),
daemon_url: self.daemon_url.clone(),
}
}
}
impl Registry {
pub fn new() -> Self {
let (events, _) = broadcast::channel(EVENT_CHANNEL_CAPACITY);
Self {
inner: Arc::new(Mutex::new(State::default())),
events,
}
}
/// Subscribe to the registry's broadcast event stream. The subscriber gets
/// a one-shot `Snapshot` of the current catalog first, then live deltas.
pub async fn subscribe(&self) -> (RegistryEvent, broadcast::Receiver<RegistryEvent>) {
let state = self.inner.lock().await;
let snapshot = RegistryEvent::Snapshot {
daemons: state
.daemons
.iter()
.map(|(id, e)| e.to_view(id).into())
.collect(),
sessions: state.sessions.values().cloned().collect(),
};
let rx = self.events.subscribe();
(snapshot, rx)
}
fn broadcast(&self, event: RegistryEvent) {
// Send returns Err only when there are no subscribers; that's not a
// bug, so swallow.
let _ = self.events.send(event);
}
// -----------------------------------------------------------------------
// Connection lifecycle
// -----------------------------------------------------------------------
/// Register a freshly-authenticated daemon. Replaces any previous entry
/// with the same `daemon_id` (and drops its sessions), matching the
/// spec's "re-registration restores catalog" expectation.
pub async fn register(
&self,
daemon_id: String,
hostname: String,
version: String,
daemon_url: String,
outbound: mpsc::Sender<DaemonInbound>,
) {
let mut state = self.inner.lock().await;
let removed_sessions = state.drop_daemon_sessions(&daemon_id);
state.daemons.insert(
daemon_id.clone(),
DaemonEntry {
hostname: hostname.clone(),
version: version.clone(),
daemon_url: daemon_url.clone(),
outbound,
},
);
drop(state);
for id in removed_sessions {
self.broadcast(RegistryEvent::SessionRemoved {
daemon_id: daemon_id.clone(),
id,
});
}
self.broadcast(RegistryEvent::DaemonAdded {
daemon: DaemonInfo {
daemon_id,
hostname,
version,
daemon_url,
},
});
}
/// Remove the daemon and any sessions it owned. Also fails any pending
/// RPCs that were sent to it.
pub async fn deregister(&self, daemon_id: &str) {
let removed_sessions;
let had_daemon;
{
let mut state = self.inner.lock().await;
had_daemon = state.daemons.remove(daemon_id).is_some();
removed_sessions = state.drop_daemon_sessions(daemon_id);
}
for id in removed_sessions {
self.broadcast(RegistryEvent::SessionRemoved {
daemon_id: daemon_id.to_string(),
id,
});
}
if had_daemon {
self.broadcast(RegistryEvent::DaemonRemoved {
daemon_id: daemon_id.to_string(),
});
}
}
// -----------------------------------------------------------------------
// Catalog mutations driven by inbound D→R messages
// -----------------------------------------------------------------------
pub async fn apply_snapshot(&self, daemon_id: &str, sessions: Vec<Session>) {
let (removed, added) = {
let mut state = self.inner.lock().await;
let removed = state.drop_daemon_sessions(daemon_id);
let mut added = Vec::with_capacity(sessions.len());
for s in sessions {
added.push(s.clone());
state.sessions.insert((daemon_id.to_string(), s.id), s);
}
(removed, added)
};
for id in removed {
self.broadcast(RegistryEvent::SessionRemoved {
daemon_id: daemon_id.to_string(),
id,
});
}
for s in added {
self.broadcast(RegistryEvent::SessionAdded { session: s });
}
}
pub async fn add_session(&self, daemon_id: &str, session: Session) {
{
let mut state = self.inner.lock().await;
state
.sessions
.insert((daemon_id.to_string(), session.id), session.clone());
}
self.broadcast(RegistryEvent::SessionAdded { session });
}
pub async fn remove_session(&self, daemon_id: &str, id: Uuid) {
let removed = {
let mut state = self.inner.lock().await;
state.sessions.remove(&(daemon_id.to_string(), id))
};
if removed.is_some() {
self.broadcast(RegistryEvent::SessionRemoved {
daemon_id: daemon_id.to_string(),
id,
});
}
}
pub async fn update_session(&self, daemon_id: &str, session: Session) {
{
let mut state = self.inner.lock().await;
state
.sessions
.insert((daemon_id.to_string(), session.id), session.clone());
}
self.broadcast(RegistryEvent::SessionUpdated { session });
}
/// Look up the daemon that owns the given session id.
pub async fn daemon_for_session(&self, session_id: Uuid) -> Option<String> {
let state = self.inner.lock().await;
state
.sessions
.iter()
.find(|((_, id), _)| *id == session_id)
.map(|((d, _), _)| d.clone())
}
/// Resolve a daemon's connect URL (the operator-advertised value from its
/// Hello frame).
pub async fn daemon_url(&self, daemon_id: &str) -> Option<String> {
let state = self.inner.lock().await;
state.daemons.get(daemon_id).map(|e| e.daemon_url.clone())
}
/// Resolve a CreateSession RPC reply. Returns true if a waiter was found.
pub async fn complete_create(&self, reply_to: ReplyTo, result: RpcResult<Session>) -> bool {
let mut state = self.inner.lock().await;
match state.pending_create.remove(&reply_to) {
Some(tx) => tx.send(result).is_ok(),
None => false,
}
}
/// Resolve a KillSession RPC reply. Returns true if a waiter was found.
pub async fn complete_kill(&self, reply_to: ReplyTo, result: RpcResult<()>) -> bool {
let mut state = self.inner.lock().await;
match state.pending_kill.remove(&reply_to) {
Some(tx) => tx.send(result).is_ok(),
None => false,
}
}
// -----------------------------------------------------------------------
// Outbound R→D requests (internal API for NAP-215)
// -----------------------------------------------------------------------
/// Forward a `CreateSession` to the named daemon and wait for the matching
/// `CreateSessionResult`. Returns the daemon-reported result on success.
pub async fn create_session(
&self,
daemon_id: &str,
request: CreateSessionRequest,
) -> Result<Result<Session, RpcError>, RegistryError> {
let reply_to = Uuid::new_v4();
let (tx, rx) = oneshot::channel();
let outbound = {
let mut state = self.inner.lock().await;
state.pending_create.insert(reply_to, tx);
state
.daemons
.get(daemon_id)
.ok_or_else(|| RegistryError::DaemonNotConnected(daemon_id.to_string()))?
.outbound
.clone()
};
let send_result = outbound
.send(DaemonInbound::CreateSession { reply_to, request })
.await;
if send_result.is_err() {
self.inner.lock().await.pending_create.remove(&reply_to);
return Err(RegistryError::DaemonGone);
}
await_rpc(rx, reply_to, |s| &mut s.pending_create, &self.inner)
.await
.map(rpc_into_result)
}
/// Forward a `KillSession` to the named daemon and wait for the matching
/// `KillSessionResult`.
pub async fn kill_session(
&self,
daemon_id: &str,
id: Uuid,
) -> Result<Result<(), RpcError>, RegistryError> {
let reply_to = Uuid::new_v4();
let (tx, rx) = oneshot::channel();
let outbound = {
let mut state = self.inner.lock().await;
state.pending_kill.insert(reply_to, tx);
state
.daemons
.get(daemon_id)
.ok_or_else(|| RegistryError::DaemonNotConnected(daemon_id.to_string()))?
.outbound
.clone()
};
let send_result = outbound
.send(DaemonInbound::KillSession { reply_to, id })
.await;
if send_result.is_err() {
self.inner.lock().await.pending_kill.remove(&reply_to);
return Err(RegistryError::DaemonGone);
}
await_rpc(rx, reply_to, |s| &mut s.pending_kill, &self.inner)
.await
.map(rpc_into_result)
}
// -----------------------------------------------------------------------
// Read-only views (mostly for tests)
// -----------------------------------------------------------------------
pub async fn daemons(&self) -> Vec<DaemonView> {
let state = self.inner.lock().await;
state.daemons.iter().map(|(id, e)| e.to_view(id)).collect()
}
pub async fn sessions(&self) -> Vec<Session> {
let state = self.inner.lock().await;
state.sessions.values().cloned().collect()
}
pub async fn sessions_for(&self, daemon_id: &str) -> Vec<Session> {
let state = self.inner.lock().await;
state
.sessions
.iter()
.filter(|((d, _), _)| d == daemon_id)
.map(|(_, s)| s.clone())
.collect()
}
}
impl Default for Registry {
fn default() -> Self {
Self::new()
}
}
impl State {
/// Removes every session belonging to `daemon_id`, returning their ids so
/// the caller can emit `SessionRemoved` events outside the lock.
fn drop_daemon_sessions(&mut self, daemon_id: &str) -> Vec<Uuid> {
let mut removed = Vec::new();
self.sessions.retain(|(d, id), _| {
if d.as_str() == daemon_id {
removed.push(*id);
false
} else {
true
}
});
removed
}
}
/// Await `rx` with [`RPC_TIMEOUT`]; on timeout / channel close, also clean up
/// the pending map so a late reply doesn't leak.
async fn await_rpc<T>(
rx: oneshot::Receiver<RpcResult<T>>,
reply_to: ReplyTo,
pick: impl FnOnce(&mut State) -> &mut HashMap<ReplyTo, oneshot::Sender<RpcResult<T>>>,
inner: &Arc<Mutex<State>>,
) -> Result<RpcResult<T>, RegistryError> {
match tokio::time::timeout(RPC_TIMEOUT, rx).await {
Ok(Ok(result)) => Ok(result),
Ok(Err(_)) => Err(RegistryError::ReplyChannelClosed),
Err(_) => {
let mut state = inner.lock().await;
pick(&mut state).remove(&reply_to);
Err(RegistryError::Timeout(RPC_TIMEOUT))
}
}
}
fn rpc_into_result<T>(rpc: RpcResult<T>) -> Result<T, RpcError> {
match rpc {
RpcResult::Ok { value } => Ok(value),
RpcResult::Err { error } => Err(RpcError(error)),
}
}