rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::path::PathBuf;

use pretty_assertions::assert_eq;
use rho_protocol::{
    self as wire, BackgroundStatus, ChildInfo, ChildRole, JobId, JobKind, JobState, JobSummary,
};

use super::{run_ended, started_by, StartedChild};

fn job(id: &str, kind: JobKind, session: &str, state: JobState) -> JobSummary {
    JobSummary {
        id: JobId::new(id),
        kind,
        state,
        workspace: PathBuf::from("/work"),
        parent: None,
        session: Some(wire::SessionId::new(session)),
        run: None,
        workflow_run: None,
        result_pending: false,
        created_at: 0,
        updated_at: 0,
        error: None,
        queue: None,
        live: None,
        grant: None,
        grant_limit: None,
        worktree: None,
        worktree_stale: false,
        child: None,
    }
}

// Covers: a child whose relay stopped without hearing how its run ended,
// as one never listed, ends as the latest subagent job that wrote its
// session ended, not as a session job of the same id; a run still under
// way, or one the daemon knows nothing of, says nothing yet.
// Owner: ACP v2 child session state
#[test]
fn a_childs_end_is_its_runs_job_state() {
    use JobKind::{Session, Subagent};
    use JobState::{Cancelled, Completed, Failed, Interrupted, Running, TimedOut};
    let cases = [
        (
            "a completed run",
            vec![job("j", Subagent, "c", Completed)],
            Some(BackgroundStatus::Completed),
        ),
        (
            "a run that timed out failed",
            vec![job("j", Subagent, "c", TimedOut)],
            Some(BackgroundStatus::Failed),
        ),
        (
            "a cancelled run",
            vec![job("j", Subagent, "c", Cancelled)],
            Some(BackgroundStatus::Cancelled),
        ),
        (
            "an interrupted run ended unseen",
            vec![job("j", Subagent, "c", Interrupted)],
            Some(BackgroundStatus::Unknown),
        ),
        (
            "a run under way has not ended",
            vec![job("j", Subagent, "c", Running)],
            None,
        ),
        (
            "the latest run of the session counts, and no session job",
            vec![
                job("old", Subagent, "c", Failed),
                job("new", Subagent, "c", Completed),
                job("turns", Session, "c", Cancelled),
            ],
            Some(BackgroundStatus::Completed),
        ),
        (
            "another session's run says nothing",
            vec![job("j", Subagent, "other", Completed)],
            None,
        ),
    ];
    for (case, jobs, expected) in cases {
        assert_eq!(
            run_ended(&jobs, &wire::SessionId::new("c")),
            expected,
            "{case}"
        );
    }
}

/// Job `id`, a child of job `parent` in `role`, writing `session`.
fn child(
    id: &str,
    parent: &str,
    role: ChildRole,
    session: &str,
    title: Option<&str>,
) -> JobSummary {
    let info = ChildInfo::new(role);
    JobSummary {
        parent: Some(JobId::new(parent)),
        child: Some(match title {
            Some(title) => info.titled(title),
            None => info,
        }),
        ..job(id, JobKind::Session, session, JobState::Idle)
    }
}

// Covers: a resume finds the sessions its session started through the
// session's own job, with each one's title, untitled ones too, and those
// its subagents started, which the daemon saves as its own; the subagents
// it delegated, which its background list announces, and the sessions
// another session started, are not among them.
// Owner: ACP v2 child sessions on resume
#[test]
fn a_resume_finds_the_sessions_its_session_started() {
    let jobs = [
        job(
            "parent-job",
            JobKind::Session,
            "parent",
            JobState::Completed,
        ),
        job("other-job", JobKind::Session, "other", JobState::Completed),
        child(
            "a",
            "parent-job",
            ChildRole::Started,
            "titled",
            Some("Fix the build"),
        ),
        child("b", "parent-job", ChildRole::Started, "untitled", None),
        JobSummary {
            kind: JobKind::Subagent,
            ..child(
                "c",
                "parent-job",
                ChildRole::Delegated,
                "delegated",
                Some("task"),
            )
        },
        child(
            "d",
            "other-job",
            ChildRole::Started,
            "elsewhere",
            Some("Not ours"),
        ),
        child("e", "c", ChildRole::Started, "nested", None),
    ];

    assert_eq!(
        started_by(&jobs, &wire::SessionId::new("parent")),
        vec![
            StartedChild {
                session: wire::SessionId::new("titled"),
                title: Some("Fix the build".to_owned()),
            },
            StartedChild {
                session: wire::SessionId::new("untitled"),
                title: None,
            },
            StartedChild {
                session: wire::SessionId::new("nested"),
                title: None,
            },
        ]
    );
}