rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What a child session is to its parent, and the policy that follows:
//! what titles it and what tells the host it stopped.

use agent_client_protocol::v2::schema as v2;
use rho_protocol::{BackgroundStatus, JobState};

use super::{background::subagent_idle, child_jobs::job_ended};
use crate::app::acp::Foreground;

/// What a child is to its parent, which says what titles it and what
/// tells the host it stopped.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ChildKind {
    /// A subagent: the parent's background list titles it and says how
    /// its run ended.
    Subagent,
    /// A session the parent started with the `sessions` tool, titled as
    /// its own session is. It outlives its turns, so it is idle when its
    /// session is.
    Started,
}

impl ChildKind {
    /// Whether the daemon's job list says how its work ended once its
    /// relay stops: a subagent's run ends, a started session does not.
    pub(super) fn learns_end_from_jobs(self) -> bool {
        match self {
            Self::Subagent => true,
            Self::Started => false,
        }
    }

    /// Whether a link to it follows it again once its relay stopped: a
    /// started session that unloaded may be prompted again, while a
    /// subagent's run is over.
    pub(super) fn follows_again(self) -> bool {
        match self {
            Self::Subagent => false,
            Self::Started => true,
        }
    }

    /// Whether the host is asked its approvals and questions under the
    /// child: a started session waits for a person, while a subagent's
    /// questions are its parent's requests, put to the host under the
    /// child from there.
    pub(super) fn asks_host(self) -> bool {
        match self {
            Self::Subagent => false,
            Self::Started => true,
        }
    }

    /// Whether its own session's title is the parent's title for it: a
    /// subagent's is its run's, which the background list carries.
    pub(super) fn mirrors_title(self) -> bool {
        match self {
            Self::Subagent => false,
            Self::Started => true,
        }
    }

    /// The `idle` the host is told when an observe finds the child in
    /// `state`, which no turn's end will tell: a started session that is
    /// idle, with how its last turn ended, or no stop reason when it ran
    /// none (`JobState::Idle`). A subagent's turn end tells it.
    pub(super) fn idle_at_observe(self, state: JobState) -> Option<v2::StateUpdate> {
        match (self, Foreground::of(state)) {
            (Self::Started, Foreground::Idle) => Some(subagent_idle(
                job_ended(state).unwrap_or(BackgroundStatus::Unknown),
            )),
            (Self::Started, Foreground::Running | Foreground::RequiresAction)
            | (Self::Subagent, _) => None,
        }
    }
}