rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::{
    io::Read,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
};

use super::{read_from, READ_BYTES};

/// A terminal that never stops printing, counting its reads.
struct Flood(Arc<AtomicUsize>);

impl Read for Flood {
    fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
        self.0.fetch_add(1, Ordering::SeqCst);
        buffer.fill(b'y');
        Ok(buffer.len())
    }
}

// Covers: the reader of a flooding shell reads no further ahead of what
// was taken from it than the read it hands over and the one it holds, so
// while the client does not read, the shell's output waits in its
// terminal and not in the daemon.
// Owner: daemon host shell terminal.
#[tokio::test]
async fn the_reader_reads_only_as_fast_as_its_reads_are_taken() {
    let reads = Arc::new(AtomicUsize::new(0));
    let stopped = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let mut printed = read_from(Box::new(Flood(Arc::clone(&reads))), stopped).unwrap();

    for taken in 1..=200 {
        let read = printed.recv().await.unwrap();
        assert_eq!(read.len(), READ_BYTES);
        let ahead = reads.load(Ordering::SeqCst);
        // One read waits to be taken; the reader holds the next.
        assert!(ahead <= taken + 2, "{ahead} reads with {taken} taken");
        tokio::task::yield_now().await;
    }
}