rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Where a request that reads a session without loading it finds the
//! session: `rho/session/observe`, `rho/session/history` and
//! `rho/session/tool_output`. Each takes what it can from where the
//! session is, as its own module says: a loaded session's feed, the stream
//! of a subagent or workflow agent node run under way that writes it, or
//! its saved file.

use std::sync::Arc;

use rho_sdk::SessionId;

use super::{child_feeds::RunStream, reopen::ReopenGuard, LoadedSession, Sessions};

/// Where a session is, for a request that reads it.
pub(super) enum Located<'a> {
    /// The daemon has it loaded.
    Loaded(Arc<LoadedSession>),
    /// The daemon has not loaded it. `reopening`, its reopen lock, is held,
    /// so no request loads it meanwhile, and a read of its file, which
    /// takes the lock as proof, does not find the file held by the daemon.
    /// `run` is the stream of the subagent or workflow agent node run under
    /// way that writes it, once the run opened it. While such a run's job is
    /// under way, [`Sessions::read_unloaded`] refuses the session as
    /// `SessionBusy`.
    Unloaded {
        run: Option<RunStream>,
        reopening: ReopenGuard<'a>,
    },
}

impl Sessions {
    /// Finds session `id`: loaded, or not, once no request loading it holds
    /// its reopen lock.
    pub(super) async fn locate(&self, id: &SessionId) -> Located<'_> {
        loop {
            let loaded = self.loaded().get(id).cloned();
            if let Some(session) = loaded {
                return Located::Loaded(session);
            }
            let reopening = self.reopening.lock(id).await;
            // A request that held the lock first may have loaded it.
            if self.loaded().contains_key(id) {
                continue;
            }
            return Located::Unloaded {
                run: self.subagents.feeds().run(id),
                reopening,
            };
        }
    }
}