rig-core 0.47.0

An opinionated library for building LLM powered applications.
Documentation
use super::*;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Counter(u32);
impl ContextValue for Counter {
    const KEY: &'static str = "test.counter";
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct RequestId(String);
impl ContextValue for RequestId {
    const KEY: &'static str = "test.request_id";
}

// Two shapes that deliberately share a key: the fixture for "the slot holds
// something that is not a `T`".
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct A(u32);
impl ContextValue for A {
    const KEY: &'static str = "k";
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct B {
    name: String,
}
impl ContextValue for B {
    const KEY: &'static str = "k";
}

#[test]
fn context_separates_inbound_and_result_values() {
    let mut context = ToolContext::new();
    context.insert(Counter(42)).unwrap();
    context
        .insert_result(RequestId("request-1".to_string()))
        .unwrap();
    assert_eq!(context.get::<Counter>().unwrap(), Some(Counter(42)));
    assert_eq!(
        context.result::<RequestId>().unwrap(),
        Some(RequestId("request-1".to_string()))
    );

    let next = context.for_dispatch();
    assert_eq!(next.get::<Counter>().unwrap(), Some(Counter(42)));
    assert_eq!(next.result::<RequestId>().unwrap(), None);
}

#[test]
fn context_round_trips_through_serde() {
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct Session {
        id: String,
    }
    impl ContextValue for Session {
        const KEY: &'static str = "test.session";
    }
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    struct Seq(u64);
    impl ContextValue for Seq {
        const KEY: &'static str = "test.seq";
    }
    let mut context = ToolContext::new();
    context
        .insert(Session {
            id: "abc".to_string(),
        })
        .unwrap();
    context.insert_result(Seq(7)).unwrap();

    let json = serde_json::to_string(&context).unwrap();
    let back: ToolContext = serde_json::from_str(&json).unwrap();
    assert_eq!(back, context);
    assert_eq!(
        back.get::<Session>().unwrap(),
        Some(Session {
            id: "abc".to_string()
        })
    );
    assert_eq!(back.result::<Seq>().unwrap(), Some(Seq(7)));

    let empty: ToolContext = serde_json::from_str("{}").unwrap();
    assert!(empty.is_empty());
    assert_eq!(serde_json::to_string(&ToolContext::new()).unwrap(), "{}");
}

#[test]
fn decode_failure_keeps_the_serde_error_as_its_source() {
    let mut context = ToolContext::new();
    context
        .inbound
        .insert(Counter::KEY.to_string(), serde_json::json!("not a number"));
    let error = context.require::<Counter>().unwrap_err();
    let source = std::error::Error::source(&error)
        .and_then(|source| source.downcast_ref::<serde_json::Error>())
        .expect("decode failure should expose the serde error");
    assert!(source.is_data());
}

#[test]
fn insert_replaces_an_undecodable_displaced_value_and_returns_none() {
    let mut context = ToolContext::new();
    context.insert(A(1)).unwrap();
    assert_eq!(
        context
            .insert(B {
                name: "b".to_string()
            })
            .unwrap(),
        None
    );
    assert_eq!(
        context.get::<B>().unwrap(),
        Some(B {
            name: "b".to_string()
        })
    );
}

/// What a conversation keeps of a call's facts: no old file text, and past
/// the budget the largest values left out while a small one stays.
#[test]
fn kept_facts_are_without_old_text_and_bounded() {
    use crate::tool::{
        ExitStatus, FileChange, FileRead, FilesChanged, FilesRead, Hunk, HunkLine, Lines,
        content_hash,
    };
    const MAX: usize = 256 * 1024;

    let mut context = ToolContext::new();
    let read = FileRead::new("/work/a.rs", content_hash(b""));
    let hunk = Hunk {
        old: Lines { start: 1, count: 1 },
        new: Lines { start: 1, count: 1 },
        lines: vec![
            HunkLine::Removed("old\n".to_owned()),
            HunkLine::Added("new\n".to_owned()),
        ],
    };
    let change = FileChange::modified("a.rs", 1, 1)
        .with_hunks(vec![hunk])
        .with_before_text("x".repeat(1024 * 1024));
    let inserted = [
        context.insert_result(ExitStatus::Exited(3)).is_ok(),
        context.insert_result(FilesRead(vec![read])).is_ok(),
        context.insert_result(FilesChanged(vec![change])).is_ok(),
    ];
    assert_eq!(inserted, [true; 3]);
    let kept = context.result_context().kept(MAX);
    let paths = kept.get::<FilesRead>().ok().flatten().unwrap_or_default();
    assert_eq!(paths.paths().collect::<Vec<_>>(), ["/work/a.rs"]);
    let changed = kept
        .get::<FilesChanged>()
        .ok()
        .flatten()
        .unwrap_or_default();
    let change = changed.0.first();
    assert_eq!(
        change.map(|change| change.before_text.is_none()),
        Some(true)
    );
    assert_eq!(change.map(|change| change.hunks.len()), Some(1));

    let huge = FileRead::new("x".repeat(MAX), content_hash(b""));
    assert!(context.insert_result(FilesRead(vec![huge])).is_ok());
    let kept = context.result_context().kept(MAX);
    assert_eq!(kept.get::<FilesRead>().ok().flatten(), None);
    assert_eq!(
        kept.get::<ExitStatus>().ok().flatten(),
        Some(ExitStatus::Exited(3))
    );

    // At the edge, the key's quotes and the map's braces count too.
    let mut edge = ToolContext::new();
    let fill = MAX - RequestId::KEY.len() - 2;
    assert!(edge.insert_result(RequestId("x".repeat(fill))).is_ok());
    let kept = edge.result_context().kept(MAX);
    let size = serde_json::to_vec(&kept).map_or(usize::MAX, |json| json.len());
    assert!(size <= MAX, "{size}");
}

/// A budget below an empty map's `{}` keeps nothing and still encodes to
/// those 2 bytes; a budget of exactly 2 keeps nothing either.
#[test]
fn kept_facts_under_the_empty_map_floor_are_empty() {
    use crate::tool::ExitStatus;

    let mut context = ToolContext::new();
    assert!(context.insert_result(ExitStatus::Exited(0)).is_ok());
    let sizes: Vec<(bool, usize)> = [0, 1, 2]
        .into_iter()
        .map(|budget| {
            let kept = context.result_context().kept(budget);
            let size = serde_json::to_vec(&kept).map_or(usize::MAX, |json| json.len());
            (kept.is_empty(), size)
        })
        .collect();
    assert_eq!(sizes, [(true, 2); 3]);
}