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 super::*;

/// The daemon at a check: what updates leave the watch to do, whether its
/// executable was replaced by a probed rho, whether it is idle, whether an
/// update starts while the probe runs, and how often the watch probed and
/// claimed it idle.
#[derive(Debug, Default)]
struct AtCheck {
    follow: Follow,
    replaced: bool,
    idle: bool,
    update_starts_while_probing: bool,
    probes: u32,
    claims: u32,
}

impl Watched for AtCheck {
    fn follow(&self) -> Follow {
        self.follow
    }

    async fn replaced(&mut self) -> bool {
        self.probes += 1;
        if self.update_starts_while_probing {
            self.follow = Follow::Held;
        }
        self.replaced
    }

    // As `Updates::commit`: nothing once updates moved on from `follow`.
    fn commit(&mut self, follow: Follow, once_idle: bool) -> bool {
        if follow != self.follow {
            return false;
        }
        if !once_idle {
            return true;
        }
        self.claims += 1;
        self.idle
    }
}

// Covers: what the upgrade watch begins at a check for each way an update
// left it. A held update suppresses even a replaced executable on an idle
// daemon, so a restart never cuts off the client before it heard the
// answer; a settled update starts the move it asked for, at once or once
// idle. A restart, even one an update asked for now, begins only onto a rho
// the watch probed at the daemon's path, never onto an unproven file. An
// update that starts while the probe runs stops the move. The watch claims
// the daemon idle, which closes its job registry to new turns, only when
// the move follows the claim.
// Owner: daemon upgrade watch.
#[tokio::test]
async fn each_check_begins_the_move_the_update_left_it() {
    let once_idle = |once_idle| Follow::Replace { once_idle };
    let restart = Some(Begin::Restart);
    let replace = Some(Begin::Replace);
    // follow, replaced, idle, an update starts while probing: begins,
    // probes, claims.
    let cases = [
        ((Follow::Held, true, true, false), (None, 0, 0)),
        ((Follow::Watch, false, true, false), (None, 1, 0)),
        ((Follow::Watch, true, false, false), (None, 1, 1)),
        ((Follow::Watch, true, true, false), (restart, 1, 1)),
        ((Follow::Watch, true, true, true), (None, 1, 0)),
        ((Follow::RestartNow, false, false, false), (None, 1, 0)),
        ((Follow::RestartNow, true, false, false), (restart, 1, 0)),
        ((Follow::RestartNow, true, true, true), (None, 1, 0)),
        ((once_idle(false), false, false, false), (replace, 0, 0)),
        ((once_idle(true), false, false, false), (None, 0, 1)),
        ((once_idle(true), false, true, false), (replace, 0, 1)),
    ];
    for ((follow, replaced, idle, update_starts_while_probing), expected) in cases {
        let case = format!("{follow:?}, replaced {replaced}, idle {idle}, update starts {update_starts_while_probing}");
        let mut at = AtCheck {
            follow,
            replaced,
            idle,
            update_starts_while_probing,
            ..AtCheck::default()
        };

        let began = at_check(&mut at).await.map(|(begin, began_for)| {
            assert_eq!(began_for, follow, "{case}");
            begin
        });

        assert_eq!((began, at.probes, at.claims), expected, "{case}");
    }
}