infinity-daemon 0.2.0

The Infinity daemon: hosts durable agent sessions and serves local clients.
Documentation
pub mod client_handler;
pub mod config;
pub mod ids;
pub mod mcp_proxy;
pub mod memory_store;
pub mod migrate;
pub mod models;
pub mod rap_callback;
pub mod rap_servers;
pub mod remote;
pub mod session;
pub mod session_store;
pub mod set_title_tool;
pub mod web_assets;
pub mod ws_handler;

use infinity_protocol::socket_path;
use tokio::net::{TcpListener, UnixListener};
use tracing_subscriber::EnvFilter;

/// Line printed to stdout when `run_daemon` is called with
/// `announce_ready = true`, once initialization has succeeded and the daemon
/// is serving. A supervising process (the CLI auto-launch) races this line
/// against daemon exit to detect startup failures. It must remain the
/// daemon's only stdout output.
pub const DAEMON_READY_LINE: &str = "infinity-daemon ready";

/// Launch a fully-initialized session manager: bind the RAP callback
/// listener, build the [`session::SessionManager`] with the listener's
/// callback URL, and start serving callbacks into it. This is the shared
/// startup path for the daemon and the CLI's direct (daemonless) mode.
pub async fn launch_session_manager(
    state_dir: std::path::PathBuf,
) -> Result<session::SharedSessionManager, Box<dyn std::error::Error + Send + Sync>> {
    let bridge = infinity_rap_bridge::RapCallbackBridge::bind().await?;
    let session_manager =
        session::SessionManager::new(state_dir, bridge.callback_url().to_owned()).await?;
    Ok(rap_callback::serve_callbacks(bridge, session_manager))
}

/// Run the daemon until it receives a shutdown signal. When `announce_ready`
/// is true, prints [`DAEMON_READY_LINE`] to stdout after all initialization
/// (including the session manager and its model providers) has succeeded.
pub async fn run_daemon(
    announce_ready: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let state_dir = infinity_protocol::state_dir();
    std::fs::create_dir_all(&state_dir)?;

    let log_file = std::fs::File::create(state_dir.join("daemon.log"))?;
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .with_writer(std::sync::Mutex::new(log_file))
        .with_ansi(false)
        .init();

    let sock_path = socket_path();
    if sock_path.exists() {
        std::fs::remove_file(&sock_path)?;
    }

    let listener = UnixListener::bind(&sock_path)?; // TODO(shadaj): do we also need to lock the file?
    tracing::info!("daemon listening on {}", sock_path.display());

    let pid_path = infinity_protocol::pid_path();
    std::fs::write(&pid_path, std::process::id().to_string())?;

    let session_manager = launch_session_manager(infinity_protocol::state_dir())
        .await
        .map_err(|e| format!("Failed to launch session manager: {e}"))?;
    tracing::info!("shared callback server started");

    // Initialize remote daemon connections
    let remote_configs = remote::load_remotes_config();
    if !remote_configs.is_empty() {
        tracing::info!("loaded {} remote daemon config(s)", remote_configs.len());
        session_manager.lock().await.init_remotes(remote_configs);
    }

    // Start WebSocket server
    let ws_port: u16 = std::env::var("INFINITY_WS_PORT")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(8080);
    let ws_bind_addr = std::env::var("INFINITY_WS_BIND").unwrap_or_else(|_| "127.0.0.1".to_owned());
    let ws_listener = TcpListener::bind((&*ws_bind_addr, ws_port)).await?;
    tracing::info!("websocket server listening on {ws_bind_addr}:{ws_port}");

    let ws_accept = ws_handler::serve(ws_listener, session_manager.clone());

    let shutdown = async {
        let mut sigint = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())
            .expect("failed to register SIGINT handler");
        let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
            .expect("failed to register SIGTERM handler");
        tokio::select! {
            _ = sigint.recv() => {}
            _ = sigterm.recv() => {}
        }
        tracing::info!("received shutdown signal");
    };

    if announce_ready {
        println!("{DAEMON_READY_LINE}");
    }

    tokio::select! {
        _ = async {
            loop {
                match listener.accept().await {
                    Ok((stream, _)) => {
                        let mgr = session_manager.clone();
                        tokio::task::spawn_local(rap_protocol::log_panic("client_handler", client_handler::handle_client(stream, mgr)));
                    }
                    Err(e) => {
                        tracing::warn!("accept error: {e}");
                    }
                }
            }
        } => {}
        _ = ws_accept => {}
        _ = shutdown => {}
    }

    let _ = std::fs::remove_file(&sock_path);
    let _ = std::fs::remove_file(infinity_protocol::pid_path());

    // Stop every booted RAP server; sessions themselves need no teardown
    // (their state is already durable, and they resume on the next input
    // after the daemon restarts).
    let mgr = session_manager.lock().await;
    mgr.rap_manager.shutdown_all().await;

    tracing::info!("daemon shut down");
    Ok(())
}