rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use pretty_assertions::assert_eq;

/// The text of a parent's message.
fn text(message: ParentMessage) -> String {
    match message.0 {
        MessageKind::Parent(text) => text,
        MessageKind::Hosted { .. } => panic!("expected a parent's message"),
    }
}

/// Covers: seal stops new accepts while still delivering bodies already queued.
/// Owner: generic parent-message gate used before terminal stdin close.
#[tokio::test]
async fn seal_rejects_new_sends_and_drains_queued() {
    let (handle, mut inbox) = message_channel();
    handle.send("queued".into()).unwrap();
    inbox.seal();
    assert_eq!(
        handle.send("late".into()),
        Err(ParentMessageSendError::Closed)
    );
    assert_eq!(inbox.recv().await.map(text), Some("queued".to_owned()));
    assert_eq!(inbox.recv().await.map(text), None);
}

/// Covers: taking a follow-up keeps the port open for later messages, and only
/// an empty queue closes it; a full queue is a visible error, not a hang.
/// Owner: turn-boundary handoff on the parent-message port.
#[test]
fn take_next_or_close_only_closes_when_empty() {
    let (handle, mut inbox) = message_channel();
    for index in 0..PARENT_MESSAGE_QUEUE_CAPACITY {
        handle.send(format!("m{index}")).unwrap();
    }
    assert_eq!(
        handle.send("overflow".into()),
        Err(ParentMessageSendError::QueueFull)
    );

    assert_eq!(inbox.take_next_or_close().map(text), Some("m0".to_owned()));
    handle.send("after take".into()).unwrap();
    let mut rest = Vec::new();
    while let Some(message) = inbox.take_next_or_close() {
        rest.push(text(message));
    }
    let mut expected: Vec<String> = (1..PARENT_MESSAGE_QUEUE_CAPACITY)
        .map(|index| format!("m{index}"))
        .collect();
    expected.push("after take".into());
    assert_eq!(rest, expected);
    assert_eq!(
        handle.send("late".into()),
        Err(ParentMessageSendError::Closed)
    );
}

/// Covers: a hosted message tells its sender only once the run delivered
/// it, and one the run dropped undelivered tells it so, which puts a
/// daemon session's background results back for its next turn; the run
/// records the cards the host rendered, not a parent's card. Kills:
/// `settle` skipping the receipt (the daemon would redeliver a result the
/// agent already took), and hosted messages journaled as a parent's.
/// Owner: the parent-message port's delivery receipt, which a daemon ACP
/// session commits its background results on.
#[test]
fn hosted_messages_are_receipted_only_once_delivered() {
    let card = AttachmentEvent::Notice("process exited".into());
    let hosted = |shown| ParentMessage::hosted("exit".into(), shown, "not delivered".into());
    let (delivered, mut delivered_receipt) = hosted(vec![card.clone()]);
    let (dropped, mut dropped_receipt) = hosted(Vec::new());
    let before = delivered_receipt.try_recv().is_ok();

    let shown = delivered.settle(Delivered::Steered);
    drop(dropped);

    assert_eq!(
        (
            before,
            shown,
            delivered_receipt.try_recv().is_ok(),
            dropped_receipt.try_recv().is_ok()
        ),
        (false, vec![card], true, false)
    );
}