rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Observing sessions (`rho/session/observe`): following a session's events
//! as an attach does, for a client that only reads it, such as the `read`
//! and `wait` tools of `rho mcp serve` (#1672). Observing never loads a
//! session. One the daemon has loaded is followed as an attach follows it,
//! keeping the environment it runs with. One a subagent or workflow agent
//! node run is writing is followed through the run's stream, from when the
//! run opens it until the run ends (#1668; see `child_feeds`). A saved one it has not loaded is
//! opened only to be read, starting none of its MCP servers, OpenAPI
//! sources, or hooks, replayed as its file keeps it with how its last turn
//! ended, and shut down again; the client is then detached as
//! [`DetachReason::NotLoaded`]. A reopen would start the session's servers
//! with the reader's environment, at the reader's nesting depth rather
//! than the session's own. A saved session another engine ran, a subagent
//! run on an external agent runtime, replays what the run showed instead
//! (see `engine_replay`).

use rho_protocol::{RpcError, SessionAttachAnswer, SessionObserveQuery, Subscription};

use super::{
    attach_response, engine_replay,
    locate::Located,
    reopen::{parse_session, ReopenGuard},
    unloaded::Unloaded,
    ResumeFrom, Sessions,
};

impl Sessions {
    /// Subscribes a client to a session's events without loading it: a
    /// loaded session's as [`Self::attach`] does, else a replay of what the
    /// session saved, after which the client is detached.
    pub(in crate::app::daemon_host) async fn observe(
        &self,
        query: SessionObserveQuery,
    ) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
        self.refuse_when_stopping()?;
        let id = parse_session(&query.params.session)?;
        let after = query.params.after.as_ref();
        let from = ResumeFrom {
            after,
            entry: query.after_entry.as_ref(),
            window: query.window,
            deferral: query.defer_tool_output,
        };
        loop {
            match self.locate(&id).await {
                Located::Loaded(session) => {
                    // Under the turn lock and marked in use, as an attach,
                    // so the idle unload cannot take it meanwhile. Taken
                    // already: looked up again, which no longer finds it
                    // loaded.
                    let Some(_turn) = session.enter() else {
                        continue;
                    };
                    return Ok(attach_response(
                        &session.workspace,
                        session.feed.attach(from),
                    ));
                }
                // A subagent run under way writes it: its stream is the run's.
                Located::Unloaded { run: Some(run), .. } => return Ok(run.attach(from)),
                Located::Unloaded {
                    run: None,
                    reopening,
                } => return self.observe_unloaded(from, &reopening).await,
            }
        }
    }

    /// The replay of the saved session whose reopen lock `reopening` holds,
    /// which the daemon has not loaded, as a client resuming from `from`
    /// gets it, then nothing: the session is read and shut down again (see
    /// [`Self::read_unloaded`]).
    async fn observe_unloaded(
        &self,
        from: ResumeFrom<'_>,
        reopening: &ReopenGuard<'_>,
    ) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
        let id = reopening.id();
        match self.read_unloaded(reopening).await? {
            Unloaded::Engine {
                workspace,
                engine,
                run,
            } => engine_replay::read(id, &workspace, *engine, from.after, run.as_deref()).await,
            Unloaded::Saved { workspace, feed } => {
                Ok(attach_response(&workspace, feed.attach(from)))
            }
        }
    }
}