magi-code 0.77.1

Repository-aware CLI coding agent for terminal work
Documentation
use super::*;

#[test]
fn conversation_replay_rejects_oversized_jsonl_without_materializing_all_events() {
    let temp = TempDir::new().unwrap();
    let manager = crate::sessions::SessionManager::new(temp.path().join("sessions"));
    let session = manager.open("safe").unwrap();
    let event = SessionEvent::new(
        "user_input",
        session.id().to_string(),
        temp.path().to_path_buf(),
        json!({"text":"x".repeat(512)}),
    );
    fs::create_dir_all(session.path().parent().unwrap()).unwrap();
    fs::write(
        session.path(),
        format!("{}\n", serde_json::to_string(&event).unwrap()),
    )
    .unwrap();
    crate::sessions::secure_test_session_root(session.path().parent().unwrap());

    let error = build_conversation_replay_with_limits(
        Some(&session),
        ConversationReplayLimits {
            max_lines: 100,
            max_bytes: 128,
        },
    )
    .unwrap_err()
    .to_string();

    assert!(
        error.contains("session replay exceeded bounded history budget"),
        "{error}"
    );
    assert!(error.contains("start /new"), "{error}");
}

#[test]
fn context_cache_material_has_stable_prefix_when_history_appends() {
    let prefix_items = vec![ProviderConversationItem::Message(ChatMessage::user("one"))];
    let mut appended_items = prefix_items.clone();
    appended_items.push(ProviderConversationItem::Message(ChatMessage::assistant(
        "two",
    )));
    let prefix = conversation_cache_material("p", "m", "sys", &prefix_items);
    let appended = conversation_cache_material("p", "m", "sys", &appended_items);
    assert!(appended.starts_with(&prefix));
    assert_ne!(prefix, appended);
}

#[test]
#[ignore = "diagnostic-only optimization measurement; run explicitly with --ignored --nocapture"]
fn optimization_harness_measures_large_replay_and_cache_material() {
    // Large count is fixture data for optimization measurement, not a runtime ceiling.
    let temp = TempDir::new().unwrap();
    let manager = crate::sessions::SessionManager::new(temp.path().join("sessions"));
    let session = manager.create().unwrap();
    let tool_pair_count = 1_500;
    for index in 0..tool_pair_count {
        session
            .append(&SessionEvent::new(
                "tool_call",
                session.id().to_string(),
                temp.path().to_path_buf(),
                json!({
                    "id": format!("call_{index}"),
                    "name": "read",
                    "arguments": {"path": format!("fixtures/{index}.txt")}
                }),
            ))
            .unwrap();
        session
            .append(&SessionEvent::new(
                "tool_result",
                session.id().to_string(),
                temp.path().to_path_buf(),
                json!({
                    "call_id": format!("call_{index}"),
                    "result": {
                        "tool_name": "read",
                        "success": true,
                        "content": format!("file contents {index}")
                    }
                }),
            ))
            .unwrap();
    }

    let replay_started = std::time::Instant::now();
    let replay = build_conversation_replay(Some(&session)).unwrap();
    let replay_elapsed = replay_started.elapsed();
    let material_started = std::time::Instant::now();
    let material = conversation_cache_material("p", "m", "sys", &replay.items);
    let material_elapsed = material_started.elapsed();

    println!(
        "optimization_harness context_replay tool_pairs={} items={} material_bytes={} replay_elapsed={replay_elapsed:?} material_elapsed={material_elapsed:?}",
        tool_pair_count,
        replay.items.len(),
        material.len()
    );
    assert_eq!(replay.items.len(), tool_pair_count * 2);
    assert!(material.contains(&format!("call_{}", tool_pair_count - 1)));
}