rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use rho_protocol::PROTOCOL_VERSION;
use tempfile::TempDir;

use super::*;

fn prepared_paths(root: &TempDir) -> DaemonPaths {
    let paths = DaemonPaths::resolve(root.path(), |_| None).unwrap();
    paths.prepare_runtime_dir().unwrap();
    paths
}

// Covers: a second daemon is refused while the first holds the lock and the
// error names the running daemon; the lock frees when the holder goes away.
// Owner: daemon single-instance lock.
#[test]
fn second_acquire_names_the_running_daemon_until_the_first_releases() {
    let root = TempDir::new().unwrap();
    let paths = prepared_paths(&root);
    let first = acquire(&paths).unwrap();
    let record = DaemonRecord {
        pid: 4242,
        version: "9.9.9".into(),
        protocol: PROTOCOL_VERSION,
        started_at: 1,
        socket: paths.socket.clone(),
    };
    write_record(&paths, &record).unwrap();

    let error = acquire(&paths).unwrap_err();

    let LockError::AlreadyRunning { record: reported } = error else {
        panic!("expected AlreadyRunning");
    };
    assert_eq!(reported, Some(record));
    assert!(is_held(&paths).unwrap());

    drop(first);
    acquire(&paths).unwrap();
}

// Covers: a daemon's lock is released the moment it is dropped, even
// while a fork shares its open file description (a clone of the file stands
// in for the fork's), so the next daemon takes it at once.
// Kills: the lock left to close without unlocking (`acquire` then waits
// out `FORK_HOLD_WAIT` and refuses).
// Owner: daemon single-instance lock.
#[test]
fn a_dropped_lock_is_free_while_a_fork_still_shares_its_file() {
    let root = TempDir::new().unwrap();
    let paths = prepared_paths(&root);
    let first = acquire(&paths).unwrap();
    let forked = first.as_ref().try_clone().unwrap();

    drop(first);
    let taken = acquire(&paths);

    drop(forked);
    assert!(taken.is_ok(), "{taken:?}");
}

// Covers: taking the lock waits out a contention as long as a probe or a
// fork may hold it (see `acquire`), and no longer: a lock still held then
// is another daemon's, and other errors are not waited on.
// Kills: no retry (a probe's hold reads as a running daemon), a retry with
// no end, errors other than contention retried.
// Owner: daemon single-instance lock.
#[test]
fn a_lock_held_for_a_moment_is_taken_once_it_frees() {
    let contended = || io::Error::from(io::ErrorKind::WouldBlock);
    let denied = || io::Error::from(io::ErrorKind::PermissionDenied);
    let never_free = 10_000;
    // Its name, the tries' results until it frees, how taking it ends,
    // and whether the tries made are right.
    type Case = (
        &'static str,
        Vec<io::Error>,
        Option<io::ErrorKind>,
        fn(usize) -> bool,
    );
    let cases: [Case; 3] = [
        (
            "free after two contended tries",
            vec![contended(), contended()],
            None,
            |tries| tries == 3,
        ),
        (
            "never free",
            std::iter::repeat_with(contended).take(never_free).collect(),
            Some(io::ErrorKind::WouldBlock),
            |tries| (2..10_000).contains(&tries),
        ),
        (
            "another error",
            vec![denied()],
            Some(io::ErrorKind::PermissionDenied),
            |tries| tries == 1,
        ),
    ];
    for (name, errors, expected, tried) in cases {
        let mut errors = errors.into_iter();
        let mut tries = 0;
        let taken = past_forks(|| {
            tries += 1;
            errors.next().map_or(Ok(()), Err)
        });
        assert_eq!(
            (taken.err().map(|error| error.kind()), tried(tries)),
            (expected, true),
            "{name}: {tries} tries"
        );
    }
}