vtcode-llm 0.188.0

LLM provider abstraction, client implementations, and streaming for VT Code
Documentation
//! GPT-6.2 system-prompt relocation on native and compatible Responses routes.

use super::*;

fn gpt62_request(messages: Vec<provider::Message>) -> provider::LLMRequest {
    provider::LLMRequest {
        messages: messages.into(),
        model: models::openai::GPT_6_2_SOL.to_string(),
        stream: true,
        ..Default::default()
    }
}

fn compatible_gpt62_context() -> ResponsesRequestContext<'static> {
    let mut ctx = cache_ctx();
    ctx.is_responses_api_model = false;
    ctx.include_explicit_cache_breakpoints = false;
    ctx.supports_prompt_cache_options = false;
    ctx
}

#[test]
fn gpt62_compatible_responses_moves_system_prompt_without_breakpoint() {
    let mut request = gpt62_request(vec![
        provider::Message::user("first question".to_string()),
        provider::Message::user("follow-up".to_string()),
    ]);
    request.system_prompt = Some(std::sync::Arc::from(system_with_dynamics()));

    let payload = build_responses_request(&request, &compatible_gpt62_context()).expect("compatible request builds");

    assert_eq!(payload.pointer("/input/0/role"), Some(&json!("developer")));
    let anchor = payload
        .pointer("/input/0/content/0/text")
        .and_then(Value::as_str)
        .expect("anchor text");
    assert!(anchor.contains("stable instructions"));
    assert!(!anchor.contains("[Harness Limits]"), "volatile sections must stay out of the anchor");
    assert!(
        payload.pointer("/input/0/content/0/prompt_cache_breakpoint").is_none(),
        "compatible endpoints reject explicit breakpoints"
    );
    assert!(!payload.to_string().contains("prompt_cache_breakpoint"));
    assert!(
        payload.get("instructions").is_none(),
        "GPT-6.2 bills top-level instructions uncached on every request"
    );
    let input = payload.get("input").and_then(Value::as_array).expect("input");
    assert_eq!(
        input.last().and_then(|item| item.get("role")),
        Some(&json!("user")),
        "compatible reminder keeps the user-role shape"
    );
    assert!(payload.pointer("/prompt_cache_options/mode").is_none());
}

#[test]
fn gpt62_compatible_developer_anchor_is_byte_stable_across_turns() {
    let first = {
        let mut request = gpt62_request(vec![provider::Message::user("one".to_string())]);
        request.system_prompt = Some(std::sync::Arc::from(system_with_dynamics()));
        request
    };
    let second = {
        let mut request = gpt62_request(vec![
            provider::Message::user("one".to_string()),
            provider::Message::user("two".to_string()),
        ]);
        request.system_prompt = Some(std::sync::Arc::from(system_with_dynamics()));
        request
    };
    let ctx = compatible_gpt62_context();

    let a = build_responses_request(&first, &ctx).expect("first turn builds");
    let b = build_responses_request(&second, &ctx).expect("second turn builds");

    assert_eq!(a.pointer("/input/0"), b.pointer("/input/0"), "developer anchor must repeat verbatim");
    let a_input = a.get("input").and_then(Value::as_array).expect("input");
    let b_input = b.get("input").and_then(Value::as_array).expect("input");
    assert_eq!(a_input.len() + 1, b_input.len(), "the new turn is the only added item");
    assert_eq!(b_input[1], a_input[1], "earlier history stays byte-stable");
    assert_eq!(b_input[2]["role"], json!("user"));
    assert!(a.get("instructions").is_none() && b.get("instructions").is_none());
}

#[test]
fn gpt6_compatible_responses_keeps_legacy_instructions_shape() {
    let mut request = gpt62_request(vec![provider::Message::user("question".to_string())]);
    request.model = models::openai::GPT_6_SOL.to_string();
    request.system_prompt = Some(std::sync::Arc::from("stable instructions"));
    let mut ctx = compatible_gpt62_context();
    ctx.relocate_instructions_to_input = false;

    let payload = build_responses_request(&request, &ctx).expect("GPT-6 compatible request builds");

    assert!(
        payload
            .get("instructions")
            .and_then(Value::as_str)
            .is_some_and(|text| text.contains("stable instructions")),
        "only GPT-6.2+ changes the instruction wire shape"
    );
    assert_eq!(payload.pointer("/input/0/role"), Some(&json!("user")));
}

#[test]
fn gpt62_native_keeps_explicit_instruction_anchor() {
    let mut request = gpt62_request(vec![provider::Message::user("question".to_string())]);
    request.system_prompt = Some(std::sync::Arc::from("stable instructions"));

    let payload = build_responses_request(&request, &cache_ctx()).expect("native request builds");

    assert_eq!(payload.pointer("/input/0/role"), Some(&json!("developer")));
    assert_eq!(payload.pointer("/input/0/content/0/prompt_cache_breakpoint/mode"), Some(&json!("explicit")));
    assert!(payload.get("instructions").is_none());
}

#[test]
fn gpt62_compatible_route_with_native_model_id_omits_breakpoints() {
    let mut request = gpt62_request(vec![provider::Message::user("question".to_string())]);
    request.system_prompt = Some(std::sync::Arc::from("stable instructions"));
    let mut ctx = compatible_gpt62_context();
    ctx.is_responses_api_model = true;

    let payload = build_responses_request(&request, &ctx).expect("request builds");

    assert_eq!(payload.pointer("/input/0/role"), Some(&json!("developer")));
    assert!(payload.pointer("/input/0/content/0/prompt_cache_breakpoint").is_none());
    assert!(!payload.to_string().contains("prompt_cache_breakpoint"));
}

#[test]
fn system_prompt_without_a_user_turn_is_rejected_before_wire() {
    let mut request = gpt62_request(Vec::new());
    request.system_prompt = Some(std::sync::Arc::from("stable instructions"));

    let error = build_responses_request(&request, &cache_ctx()).expect_err("no user turn must fail locally");

    assert!(matches!(error, provider::LLMError::InvalidRequest { .. }));
    assert!(error.to_string().contains("No messages provided for Responses API"));
}