rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Attaching to a session (`rho/session/attach`): following its events,
//! loading it first if the daemon has not, and continuing a turn the
//! restart that ran this daemon interrupted.

use std::sync::Arc;

use rho_protocol::{RpcError, SessionAttachAnswer, SessionAttachQuery, Subscription, Surfaces};

use super::{
    attach_response, grant, host_prompt, open::OpenWith, reopen::FirstTurn, restart_resume, setup,
    turn::TurnTask, ResumeFrom, Sessions,
};

impl Sessions {
    /// Subscribes a client to a session's events, resuming after its cursor
    /// when the session's feed still can, and continues the session's turn
    /// if the restart that ran this daemon interrupted it.
    pub(in crate::app::daemon_host) async fn attach(
        &self,
        query: SessionAttachQuery,
    ) -> Result<(SessionAttachAnswer, Subscription), RpcError> {
        self.refuse_when_stopping()?;
        let SessionAttachQuery {
            params,
            after_entry,
            window,
            defer_tool_output,
            ..
        } = query;
        // Taken under the turn lock a prompt starts its turn under, so an
        // attach never sees the last turn's end once the job list says the
        // next turn runs, and the idle unload never takes a session a client
        // just attached to.
        // Applied only if this attach reopens the session: one the daemon
        // has loaded keeps the environment and setup it opened with.
        // Its grant is its environment's: an attach names none.
        let draws = params.setup.surfaces;
        // Applied to a loaded session too, from its next turn.
        let prompt = params
            .setup
            .system_prompt
            .as_ref()
            .map(setup::host_prompt)
            .transpose()?;
        let with = OpenWith::under(
            grant::asked(/*named*/ None, &params.environment)?,
            &params.environment,
            params.setup,
        )?;
        // A turn the restart interrupted continues first, so the client
        // attaches to the turn that continues it.
        let (session, (resumed, attached)) = self
            .enter_session(
                &params.session,
                &with,
                FirstTurn::Daemon,
                |session, turn| {
                    let resumed = self.resume_interrupted(session, turn);
                    // Read from every attach, not only one that opens the
                    // session: a `ui` call waits for the user only while a
                    // client that draws surfaces follows the session.
                    let viewer = match draws {
                        Surfaces::Unsupported => None,
                        Surfaces::Drawn => session
                            .background
                            .surfaces()
                            .map(|surfaces| surfaces.viewer()),
                    };
                    Ok((
                        resumed,
                        session.feed.attach_viewer(
                            ResumeFrom {
                                after: params.after.as_ref(),
                                entry: after_entry.as_ref(),
                                window,
                                deferral: defer_tool_output,
                            },
                            viewer,
                        ),
                    ))
                },
            )
            .await?;
        if let Some(prompt) = prompt {
            host_prompt::apply_when_free(&session, prompt, self.stopping.clone());
        }
        if let Some(turn) = resumed {
            // Spawned before anything awaits, as a prompt's turn is.
            TurnTask {
                session: Arc::clone(&session),
                turn,
                stopping: self.stopping.clone(),
                pull_requests: self.pull_requests.service().clone(),
            }
            .spawn_daemon(restart_resume::resume_turn());
        }
        Ok(attach_response(&session.workspace, attached))
    }
}