use std::sync::Arc;
use gadk_agent::{Agent, Mode};
use gadk_genai::{Content, ROLE_MODEL, ROLE_USER};
use gadk_session::Event;
use super::internal_llminternal_contents_processor::{
ctx_builder, ev, llm_agent, run_contents, text,
};
use crate::llm::LlmAgentConfig;
use crate::llm::mode::with_bound_mode;
use crate::llm::processors::contents::SINGLE_TURN_NUDGE;
fn has_text(contents: &[Content], t: &str) -> bool {
contents
.iter()
.flat_map(|c| c.parts.iter())
.any(|p| p.text == t)
}
fn history(agent_reply_author: &str) -> Vec<Event> {
vec![
ev("user", text("older turn", ROLE_USER)),
ev(agent_reply_author, text("older answer", ROLE_MODEL)),
ev("user", text("current turn", ROLE_USER)),
]
}
#[tokio::test]
async fn test_contents_request_processor_honours_resolved_mode_over_declaration() {
const AGENT_NAME: &str = "worker";
let cases = [
(
"single_turn placement drops history",
Mode::SingleTurn,
false,
),
("chat placement keeps history", Mode::Chat, true),
];
for (name, resolved, want_history) in cases {
let agent: Arc<dyn Agent> = llm_agent(LlmAgentConfig {
name: AGENT_NAME.into(),
..Default::default()
});
let ctx = ctx_builder(&agent, history(AGENT_NAME)).build_invocation();
let ctx = with_bound_mode(&ctx, AGENT_NAME, agent.agent_state(), resolved);
let got = run_contents(&ctx)
.await
.unwrap_or_else(|e| panic!("{name}: ContentsRequestProcessor: {e}"));
assert_eq!(
has_text(&got, "older turn"),
want_history,
"{name}: history present mismatch (contents: {got:?})"
);
}
}
#[tokio::test]
async fn test_contents_request_processor_bound_mode_is_scoped_to_its_agent() {
let agent = llm_agent(LlmAgentConfig {
name: "other".into(),
..Default::default()
});
let ctx = ctx_builder(&agent, history("other")).build_invocation();
let ctx = with_bound_mode(&ctx, "worker", agent.agent_state(), Mode::SingleTurn);
let got = run_contents(&ctx).await.unwrap();
assert!(
has_text(&got, "older turn"),
"a mode bound for another agent suppressed this agent's history; contents = {got:?}"
);
}
#[tokio::test]
async fn test_contents_request_processor_declared_single_turn_without_placement_keeps_history() {
let agent = llm_agent(LlmAgentConfig {
name: "worker".into(),
mode: Mode::SingleTurn,
..Default::default()
});
let ctx = ctx_builder(&agent, history("worker")).build_invocation();
let got = run_contents(&ctx)
.await
.unwrap_or_else(|e| panic!("ContentsRequestProcessor: {e}"));
assert!(
has_text(&got, "older turn"),
"declared single_turn outside a placement lost its history; contents = {got:?}"
);
}
#[tokio::test]
async fn test_contents_request_processor_resolved_single_turn_gets_the_nudge() {
const AGENT_NAME: &str = "worker";
let cases = [
("placement resolves it", Mode::Unset, true, true),
("declaration alone", Mode::SingleTurn, false, true),
("neither", Mode::Unset, false, false),
];
for (name, declared, bind, want) in cases {
let agent = llm_agent(LlmAgentConfig {
name: AGENT_NAME.into(),
mode: declared,
..Default::default()
});
let ctx = ctx_builder(&agent, vec![])
.isolation_scope("scope-1")
.user_content(text("do the thing", ROLE_USER))
.build_invocation();
let ctx = if bind {
with_bound_mode(&ctx, AGENT_NAME, agent.agent_state(), Mode::SingleTurn)
} else {
ctx
};
let got = run_contents(&ctx)
.await
.unwrap_or_else(|e| panic!("{name}: ContentsRequestProcessor: {e}"));
assert_eq!(
has_text(&got, SINGLE_TURN_NUDGE),
want,
"{name}: single-turn nudge present mismatch (contents: {got:?})"
);
}
}
#[tokio::test]
async fn test_contents_request_processor_a_foreign_single_turn_binding_does_not_hide_a_chat_agents_history()
{
let agent = llm_agent(LlmAgentConfig {
name: "worker".into(),
mode: Mode::Chat,
..Default::default()
});
let decoy = llm_agent(LlmAgentConfig {
name: "worker".into(),
..Default::default()
});
let ctx = ctx_builder(&agent, history("worker")).build_invocation();
let ctx = with_bound_mode(&ctx, "worker", decoy.agent_state(), Mode::SingleTurn);
let got = run_contents(&ctx)
.await
.unwrap_or_else(|e| panic!("ContentsRequestProcessor: {e}"));
assert!(
!got.is_empty(),
"no contents were built, so this test pins nothing"
);
assert!(
has_text(&got, "older turn"),
"a single_turn placement resolved for another agent hid this chat agent's history; contents = {got:?}"
);
}