rho-coding-agent 3.8.0

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

use super::DaemonCompactTurn;
use crate::tui::{
    daemon_events::{Rest, TurnState},
    tests::test_app,
    Entry, QueuedPrompt, StreamKind,
};

// Covers: a replayed chunk that continues the message a running turn
// streams, as a reattach's tail brings, leaves the stream knowing what it
// said: a code block it opened stays open for the live text that follows,
// and the turn's answer still completes the message rather than
// mismatching it (#1818).
// Owner: daemon transcript (tail replay into a live stream)
#[test]
fn a_replayed_continuation_keeps_the_stream_in_step_with_the_message() {
    let mut app = test_app();
    app.streams.assistant_stream.push_delta("Here:\n");
    app.finish_streams();

    app.continue_stream_with_replayed(StreamKind::Assistant, "```rust\nlet a = 1;\n");

    let shown = match app.history.entries() {
        [Entry::Assistant(reply)] => reply.text.clone(),
        _ => String::new(),
    };
    let said = "Here:\n```rust\nlet a = 1;\n";
    assert_eq!(
        (
            app.streams.code_fence(StreamKind::Assistant).is_open(),
            app.streams.assistant_stream.emitted_text(),
            shown.as_str(),
        ),
        (true, said, said)
    );
}

// Covers: a reset replay during a running turn must not leave the turn's
// start pointing into the old transcript, which stamps the duration on an
// earlier turn's reply.
// Owner: daemon transcript (reset replay vs turn receipt)
#[tokio::test]
async fn reset_replay_during_a_turn_places_its_start_after_the_rebuilt_rows() {
    let mut app = test_app();
    app.insert_entry(&Entry::User("prompt".into()));
    app.show_daemon_turn(TurnState::Running).await;

    // The reset replay: the saved transcript, the running turn's prompt
    // last, then its running state.
    app.clear_daemon_transcript();
    app.insert_entry(&Entry::User("earlier".into()));
    app.insert_entry(&Entry::Assistant("earlier reply".into()));
    app.insert_entry(&Entry::User("prompt".into()));
    app.show_daemon_turn(TurnState::Running).await;
    // A turn that ends with no reply row of its own, as a tool-only one.
    app.show_daemon_turn(TurnState::Resting(Rest::Ready)).await;

    assert!(matches!(
        app.history.entries(),
        [
            Entry::User(_),
            Entry::Assistant(earlier),
            Entry::User(_),
            Entry::Assistant(receipt),
        ] if earlier.worked_for.is_none() && receipt.worked_for.is_some()
    ));
}

// Covers: a daemon `/compact` turn whose compaction failed sends the
// follow-up that waited for it, as a prompt "Compact, then send" parked; a
// cancelled compaction does not, and neither does a failed turn that was no
// compaction.
// Owner: daemon transcript (follow-ups at a turn's end). The fixture matrix
// cannot fail a compaction: a failed native compaction falls back to a text
// summary, and a conversation small enough for a PTY has none to write.
#[tokio::test]
async fn a_failed_compaction_sends_the_follow_up_that_waited_for_it() {
    let failed = || Rest::Failed {
        error: "compaction failed".into(),
    };
    let cases = [
        ("failed compaction", true, failed(), true),
        ("cancelled compaction", true, Rest::Interrupted, false),
        ("failed model turn", false, failed(), false),
    ];
    for (name, compaction, rest, sends) in cases {
        let mut app = test_app();
        app.show_daemon_turn(TurnState::Running).await;
        if compaction {
            app.daemon_compact_turn = Some(DaemonCompactTurn::default());
        }
        app.pending.push_follow_up(QueuedPrompt {
            prompt: "parked".into(),
            display_prompt: "parked".into(),
            paste_segments: Vec::new(),
            media: Vec::new(),
        });

        app.show_daemon_turn(TurnState::Resting(rest)).await;

        assert_eq!(app.start_follow_ups, sends, "{name}");
    }
}

/// Renders the transcript, runs every path check it queues, and returns how
/// many links its rows then carry.
fn linked_paths_after_checks(app: &mut crate::tui::App) -> usize {
    let settings = crate::tui::history_cache::HistoryRenderSettings {
        width: 80,
        max_tool_output_lines: 10,
        zen_mode: false,
        show_reasoning_output: true,
        theme_generation: 0,
        max_image_height: crate::tui::feed_image::DEFAULT_IMAGE_HEIGHT,
        path_icons: false,
    };
    let all = crate::tui::history_cache::HistoryLineSlice {
        start: 0,
        count: usize::MAX,
    };
    app.history.with_lines_and_images_mut(|cache, entries, _| {
        let mut links = cache.visible_links(entries, settings, all, &|_, _| Vec::new());
        while let Some(check) = cache.take_path_check() {
            cache.apply_path_check(check.run());
            links = cache.visible_links(entries, settings, all, &|_, _| Vec::new());
        }
        links.len()
    })
}

// Covers: a path a reply named before the file existed links once the turn
// that wrote the file ends, whether it completed, was interrupted, failed,
// or was a `/compact` turn.
// Owner: daemon transcript (turn end)
#[tokio::test]
async fn every_turn_end_rechecks_paths_found_missing() {
    let cases = [
        ("completed", false, Rest::Ready),
        ("compact", true, Rest::Ready),
        ("interrupted", false, Rest::Interrupted),
        (
            "failed",
            false,
            Rest::Failed {
                error: "boom".into(),
            },
        ),
    ];
    for (name, compact, rest) in cases {
        let workspace = tempfile::tempdir().expect("tempdir");
        let mut app = test_app();
        app.history.lines_mut().set_path_workspace(workspace.path());
        app.show_daemon_turn(TurnState::Running).await;
        if compact {
            app.daemon_compact_turn = Some(DaemonCompactTurn::default());
        }
        app.insert_entry(&Entry::Assistant("I will write src/new.rs next.".into()));
        assert_eq!(linked_paths_after_checks(&mut app), 0, "{name}: before");

        std::fs::create_dir_all(workspace.path().join("src")).expect("mkdir");
        std::fs::write(workspace.path().join("src/new.rs"), "").expect("write");
        app.show_daemon_turn(TurnState::Resting(rest)).await;

        assert_eq!(linked_paths_after_checks(&mut app), 1, "{name}: after");
    }
}