rho-coding-agent 3.11.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 { .. } | MessageKind::Steer { .. } => {
            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)
    );
}

/// Covers: a steer reads as its content, text and images in order, the run
/// skips one its person withdrew, and its ticket says how far it got.
/// Owner: steers on the parent-message port.
#[test]
fn steers_carry_attachments_and_skip_withdrawn() {
    let image = PromptBlock::Image {
        mime_type: "image/png".to_owned(),
        data: "aGk=".to_owned(),
    };
    let text = PromptBlock::Text {
        text: "look".to_owned(),
    };
    let (handle, mut inbox) = message_channel();
    let mut tickets = Vec::new();
    for content in [
        vec![text.clone()],
        vec![text.clone(), image.clone()],
        vec![image.clone()],
    ] {
        let (message, ticket) = ParentMessage::steer(content, Vec::new(), String::new());
        handle.send_message(message).unwrap();
        tickets.push(ticket);
    }
    let withdrew = tickets[0].withdraw();
    // The run skips the withdrawn first steer.
    let text_and_image = inbox.take_next_or_close().map(|message| message.content());
    let progress_after_send = tickets[1].withdraw();
    let image_only = inbox.take_next_or_close().unwrap();
    let content = image_only.content();
    image_only.settle(Delivered::Steered);

    assert_eq!(
        (
            withdrew,
            text_and_image,
            progress_after_send,
            content,
            tickets[2].withdraw()
        ),
        (
            SteerProgress::Withdrawn,
            Some(vec![text, image.clone()]),
            SteerProgress::Sent,
            vec![image],
            SteerProgress::Applied,
        )
    );
}