rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use std::time::Duration;

/// Bounds the wait for an exit signal; never the way a test syncs. Windows runs
/// `exit 0` through PowerShell, which took over 2s on a loaded windows-latest
/// runner, and neighbouring Windows tests have taken 17s, so the tripwire sits
/// well clear of both.
const EXIT_TRIPWIRE: Duration = Duration::from_secs(60);

/// Waits for exit through the host view, which, unlike `poll`, leaves the
/// result undelivered.
async fn exited_undelivered(manager: &ProcessManager, id: &str) {
    tokio::time::timeout(EXIT_TRIPWIRE, async {
        loop {
            // Subscribe before checking so an exit in between still wakes us.
            let exited = manager.notified_owned();
            let view = manager.host_view_from(id, /*cursor*/ 0).unwrap();
            if terminal(view.snapshot.state) {
                return;
            }
            exited.await;
        }
    })
    .await
    .expect("process did not become terminal");
}

fn rail_ids(manager: &ProcessManager) -> Vec<String> {
    manager
        .live_summaries()
        .into_iter()
        .map(|summary| summary.process_id)
        .collect()
}

// Covers: host peek must not mark a terminal process observed.
// Owner: process manager host view
#[tokio::test]
async fn host_view_does_not_mark_terminal_observed() {
    let manager = ProcessManager::new(ProcessLimits::default());
    let started = manager
        .start(SUCCESS_COMMAND.into(), std::path::Path::new("."), None)
        .await
        .unwrap();
    exited_undelivered(&manager, &started.process_id).await;
    assert!(manager.has_pending_notification());
    let view = manager
        .host_view_from(&started.process_id, /*cursor*/ 0)
        .unwrap();
    pretty_assertions::assert_eq!(view.snapshot.state, State::Exited);
    pretty_assertions::assert_eq!(view.snapshot.exit_code, Some(0));
    pretty_assertions::assert_eq!(view.quiet_seconds, None);
    assert!(manager.has_pending_notification());
}

// Covers: a finished, undelivered process stays on the rail under its own
// command, not the platform shell wrapper, with its exit code.
// Owner: process manager. Frozen elapsed time is covered with fixture instants
// in `manager_tests.rs`, since a real process's run time varies by runner.
#[tokio::test]
async fn live_summaries_keeps_terminal_rows_with_exit_code() {
    let manager = ProcessManager::new(ProcessLimits::default());
    let started = manager
        .start(SUCCESS_COMMAND.into(), std::path::Path::new("."), None)
        .await
        .unwrap();
    exited_undelivered(&manager, &started.process_id).await;

    let summaries = manager.live_summaries();
    pretty_assertions::assert_eq!(
        summaries
            .iter()
            .map(|summary| (
                summary.process_id.as_str(),
                summary.command.as_str(),
                summary.state,
                summary.quiet_seconds,
                summary.exit_code
            ))
            .collect::<Vec<_>>(),
        vec![(
            started.process_id.as_str(),
            SUCCESS_COMMAND,
            State::Exited,
            None,
            Some(0)
        )]
    );
}

// Covers: a finished row leaves the rail when its result is delivered, either
// as a notification or through an agent poll, not on a timer; a rolled-back
// notification puts it back.
// Owner: process manager host list
#[tokio::test]
async fn live_summaries_keep_terminal_rows_until_delivered() {
    let manager = ProcessManager::new(ProcessLimits::default());
    let notified = manager
        .start(SUCCESS_COMMAND.into(), std::path::Path::new("."), None)
        .await
        .unwrap();
    let polled = manager
        .start(SUCCESS_COMMAND.into(), std::path::Path::new("."), None)
        .await
        .unwrap();
    exited_undelivered(&manager, &notified.process_id).await;
    exited_undelivered(&manager, &polled.process_id).await;
    pretty_assertions::assert_eq!(
        rail_ids(&manager),
        vec![notified.process_id.clone(), polled.process_id.clone()]
    );

    manager
        .poll(&polled.process_id, None, Duration::ZERO)
        .await
        .unwrap();
    pretty_assertions::assert_eq!(rail_ids(&manager), vec![notified.process_id.clone()]);

    let delivered = manager.take_notifications();
    pretty_assertions::assert_eq!(rail_ids(&manager), Vec::<String>::new());

    manager.restore_notifications(&delivered);
    pretty_assertions::assert_eq!(rail_ids(&manager), vec![notified.process_id]);
}

// Covers: the rail reports seconds-since-output for live jobs, and None when
// nothing has been written yet.
// Owner: process manager
#[tokio::test]
async fn live_summaries_reports_quiet_seconds_only_after_output() {
    let manager = ProcessManager::new(ProcessLimits::default());
    let silent = manager
        .start(LONG_RUNNING_COMMAND.into(), std::path::Path::new("."), None)
        .await
        .unwrap();
    let noisy = manager
        .start(
            OUTPUT_THEN_SLEEP_COMMAND.into(),
            std::path::Path::new("."),
            None,
        )
        .await
        .unwrap();

    loop {
        let snapshot = manager
            .poll(&noisy.process_id, Some(0), Duration::from_secs(2))
            .await
            .unwrap();
        if snapshot
            .chunks
            .iter()
            .any(|chunk| chunk.text.contains("hello"))
        {
            break;
        }
        assert!(
            !terminal(snapshot.state),
            "noisy process exited before producing output"
        );
    }

    let summaries = manager.live_summaries();
    pretty_assertions::assert_eq!(
        summaries
            .iter()
            .map(|summary| summary.process_id.as_str())
            .collect::<Vec<_>>(),
        vec![silent.process_id.as_str(), noisy.process_id.as_str()]
    );
    pretty_assertions::assert_eq!(summaries[0].quiet_seconds, None);
    pretty_assertions::assert_eq!(summaries[0].exit_code, None);
    match summaries[1].quiet_seconds {
        Some(seconds) => assert!(seconds <= 2, "quiet_seconds={seconds}"),
        None => panic!("expected quiet_seconds after output"),
    }
    pretty_assertions::assert_eq!(summaries[1].exit_code, None);

    manager
        .stop(&silent.process_id, Duration::ZERO)
        .await
        .unwrap();
    manager
        .stop(&noisy.process_id, Duration::ZERO)
        .await
        .unwrap();
    eventually(&manager, &silent.process_id).await;
    eventually(&manager, &noisy.process_id).await;
}