veerdiff 0.1.0

Semantic diff engine for LLM and agent execution traces — find exactly where two runs veer apart.
Documentation
//! End-to-end library tests over fixture traces.

use std::path::PathBuf;

use veerdiff::config::Config;
use veerdiff::import::{Format, import_str};
use veerdiff::normalize::Normalizer;
use veerdiff::{Trace, diff};

fn fixture(name: &str) -> Trace {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push("tests/fixtures");
    path.push(name);
    let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {path:?}: {e}"));
    import_str(&text, Format::Veer).unwrap()
}

#[test]
fn reordered_json_keys_do_not_count_as_a_change() {
    // run_a and run_b differ only in: search args key order (noise), the
    // reserved restaurant (Osteria -> Kappo), the final message, and a volatile
    // request_id in metadata.
    let a = fixture("agent_run_a.json");
    let b = fixture("agent_run_b.json");

    let cfg = Config {
        normalizer: Normalizer {
            // Drop the volatile request id so it does not show as a change.
            drop_meta: vec!["request_id".into()],
            ..Normalizer::default()
        },
        ..Config::default()
    };

    let d = diff(&a, &b, &cfg);

    // The search_restaurants call has only reordered keys -> equal.
    // Changes: the `reserve` args.restaurant and the final assistant message.
    assert_eq!(d.stats.changed, 2, "ops: {:#?}", d.ops);
    assert_eq!(d.stats.inserted, 0);
    assert_eq!(d.stats.deleted, 0);
    assert_eq!(d.stats.equal, 3);

    // First divergence is the `reserve` tool call at index 3.
    assert_eq!(d.first_divergence, Some(3));
}

#[test]
fn keeping_the_request_id_surfaces_it_as_a_change() {
    let a = fixture("agent_run_a.json");
    let b = fixture("agent_run_b.json");

    // Default config keeps meta; the request_id now differs -> the tool_result
    // becomes a change too.
    let d = diff(&a, &b, &Config::default());
    assert_eq!(d.stats.changed, 3);
    let changed_paths: Vec<&str> = d
        .ops
        .iter()
        .filter_map(|op| match op {
            veerdiff::Op::Change { fields, .. } => Some(fields),
            _ => None,
        })
        .flatten()
        .map(|f| f.path.as_str())
        .collect();
    assert!(changed_paths.contains(&"meta.request_id"));
}

#[test]
fn identical_trace_diffs_to_nothing() {
    let a = fixture("agent_run_a.json");
    let d = diff(&a, &a, &Config::default());
    assert!(d.is_identical());
    assert_eq!(d.similarity, 1.0);
}