rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! What the daemon's job list says of a session's children: how a
//! subagent's run ended, and which sessions a session started.

use std::collections::HashMap;

use rho_protocol::{BackgroundStatus, ChildRole, JobId, JobKind, JobState, JobSummary, SessionId};

/// A session another session started, as the job list records it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct StartedChild {
    pub(super) session: SessionId,
    /// Its title, once it has one.
    pub(super) title: Option<String>,
}

/// The sessions the session `parent` started, as `jobs` lists them: those
/// its own turns started, and those its subagents and workflow runs did,
/// which the daemon saves as `parent`'s too.
pub(super) fn started_by(jobs: &[JobSummary], parent: &SessionId) -> Vec<StartedChild> {
    let by_id: HashMap<&JobId, &JobSummary> = jobs.iter().map(|job| (&job.id, job)).collect();
    jobs.iter()
        .filter_map(|job| {
            let child = job.child.as_ref()?;
            let session = job.session.as_ref()?;
            let started = child.role == ChildRole::Started && starter(&by_id, job)? == parent;
            started.then(|| StartedChild {
                session: session.clone(),
                title: child.title.clone(),
            })
        })
        .collect()
}

/// The session whose turn started `job`: its parent job's, through the
/// subagent and workflow runs between them.
fn starter<'a>(by_id: &HashMap<&JobId, &'a JobSummary>, job: &JobSummary) -> Option<&'a SessionId> {
    let mut parent = by_id.get(job.parent.as_ref()?)?;
    // A chain holds each job at most once, so this bounds a cycle.
    for _ in 0..by_id.len() {
        match parent.kind {
            JobKind::Session => return parent.session.as_ref(),
            JobKind::Subagent | JobKind::Workflow => {
                parent = by_id.get(parent.parent.as_ref()?)?;
            }
            // A kind a newer daemon sends.
            _ => return None,
        }
    }
    None
}

/// How the subagent run that wrote `session` ended, as `jobs` says, once it
/// did.
pub(super) fn run_ended(jobs: &[JobSummary], session: &SessionId) -> Option<BackgroundStatus> {
    let job = jobs
        .iter()
        .rev()
        .find(|job| job.kind == JobKind::Subagent && job.session.as_ref() == Some(session))?;
    job_ended(job.state)
}

/// How a job in `state` ended, once it did.
pub(super) fn job_ended(state: JobState) -> Option<BackgroundStatus> {
    match state {
        JobState::Completed => Some(BackgroundStatus::Completed),
        JobState::Failed | JobState::TimedOut => Some(BackgroundStatus::Failed),
        JobState::Cancelled => Some(BackgroundStatus::Cancelled),
        JobState::Interrupted | JobState::Unknown => Some(BackgroundStatus::Unknown),
        JobState::Idle | JobState::Queued | JobState::Running | JobState::WaitingForInput => None,
        // A state a newer daemon sends.
        _ => Some(BackgroundStatus::Unknown),
    }
}

#[cfg(test)]
#[path = "child_jobs_tests.rs"]
mod tests;