#![cfg(feature = "testutil")]
use std::collections::HashSet;
use std::sync::Arc;
use gadk_agent::{BeforeModelCallback, Context, ToolError};
use gadk_genai::Content;
use gadk_session::compaction::{Config, LlmSummarizer, LlmSummarizerConfig, content};
use gadk_session::{Event, GetRequest};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::compaction::apply::has_usable_summary;
use crate::llm::LlmAgentConfig;
use crate::llm::agent as llm_agent;
use crate::testutil::{TestAgentRunner, collect_text_parts, gemini_test_model};
use crate::tool::{FunctionTool, FunctionToolConfig};
pub(super) const COMPACTION_MODEL_NAME: &str = "gemini-3.5-flash";
pub(super) fn cassette(test: &str) -> String {
format!(
"{}/tests/testdata/agent/llmagent/testdata/{test}.httprr",
env!("CARGO_MANIFEST_DIR")
)
}
pub(super) async fn compaction_model(test: &str) -> Arc<dyn gadk_model::Llm> {
gemini_test_model(COMPACTION_MODEL_NAME, cassette(test)).await
}
pub(super) fn prompt_has_function_traffic(contents: &[Content]) -> bool {
contents
.iter()
.flat_map(|c| c.parts.iter())
.any(|p| p.function_call.is_some() || p.function_response.is_some())
}
fn call_key(id: &str, name: &str) -> String {
if !id.is_empty() {
format!("id:{id}")
} else {
format!("name:{name}")
}
}
pub(super) fn assert_no_orphan_function_responses(prompt_idx: usize, contents: &[Content]) {
let mut seen_calls = HashSet::new();
for c in contents {
for part in &c.parts {
if let Some(fc) = &part.function_call {
seen_calls.insert(call_key(&fc.id, &fc.name));
}
if let Some(fr) = &part.function_response {
assert!(
seen_calls.contains(&call_key(&fr.id, &fr.name)),
"prompt {prompt_idx} carries a function response {:?} with no preceding call",
fr.name
);
}
}
}
}
pub(super) async fn session_events_for(r: &TestAgentRunner) -> Vec<Event> {
r.session_service()
.get(GetRequest {
app_name: "test_app".into(),
user_id: "test_user".into(),
session_id: r.session_id().to_string(),
..Default::default()
})
.await
.unwrap_or_else(|e| panic!("session Get() error = {e}"))
.events()
}
pub(super) fn text_of(c: Option<&Content>) -> String {
c.map(|c| c.parts.iter().map(|p| p.text.as_str()).collect::<String>())
.unwrap_or_default()
}
fn prompt_text_of(contents: &[Content]) -> String {
let mut b = String::new();
for c in contents {
for p in &c.parts {
if !p.text.is_empty() {
b.push_str(&format!("[{}] {}\n", c.role, p.text));
} else if let Some(fc) = &p.function_call {
b.push_str(&format!("[{}] CALL {}\n", c.role, fc.name));
} else if let Some(fr) = &p.function_response {
b.push_str(&format!("[{}] RESPONSE {}\n", c.role, fr.name));
}
}
}
b
}
fn session_holds_text(events: &[Event], want: &str) -> bool {
events
.iter()
.filter_map(content)
.flat_map(|c| c.parts.iter())
.any(|p| p.text.contains(want))
}
fn prompt_mentions_call_id(contents: &[Content], id: &str) -> bool {
contents.iter().flat_map(|c| c.parts.iter()).any(|p| {
p.function_call.as_ref().is_some_and(|fc| fc.id == id)
|| p.function_response.as_ref().is_some_and(|fr| fr.id == id)
})
}
#[derive(Deserialize, schemars::JsonSchema)]
struct CityArgs {
city: String,
}
#[derive(Serialize, schemars::JsonSchema)]
struct WeatherResult {
weather: String,
}
#[tokio::test]
async fn test_compaction_e2_e() {
let model = compaction_model("TestCompactionE2E").await;
let prompts: Arc<Mutex<Vec<Vec<Content>>>> = Arc::default();
let capture: BeforeModelCallback = {
let prompts = prompts.clone();
Arc::new(move |_ctx: Context, req: gadk_model::LlmRequest| {
prompts.lock().push(req.contents.clone());
Box::pin(async move { (req, Ok(None)) })
})
};
let weather = FunctionTool::new(
FunctionToolConfig {
input_schema: Some(json!({
"additionalProperties": false,
"properties": {"city": {"description": "the city to look up", "type": "string"}},
"required": ["city"],
"type": "object"
})),
output_schema: Some(json!({
"additionalProperties": false,
"properties": {"weather": {"type": "string"}},
"required": ["weather"],
"type": "object"
})),
..FunctionToolConfig::new("get_weather", "Returns the weather in a city.")
},
|_ctx: Context, args: CityArgs| async move {
Ok::<_, ToolError>(WeatherResult {
weather: format!("sunny in {}", args.city),
})
},
)
.unwrap();
let a = llm_agent::new(LlmAgentConfig {
name: "compaction_agent".into(),
description: "agent used to exercise context compaction".into(),
model: Some(model.clone()),
instruction: "You are a concise assistant. Answer in one short sentence.".into(),
tools: vec![weather],
before_model_callbacks: vec![capture],
disallow_transfer_to_parent: true,
disallow_transfer_to_peers: true,
..Default::default()
})
.unwrap();
let summarizer = LlmSummarizer::new(LlmSummarizerConfig {
model: Some(model.clone()),
..Default::default()
})
.unwrap();
let r = TestAgentRunner::with_compaction(
a,
Config {
compaction_interval: 2,
summarizer: Some(Arc::new(summarizer)),
..Default::default()
},
)
.await;
let turns = [
"What is the weather in Zurich?",
"My favourite colour is teal, remember that.",
"What was my favourite colour again?",
"Now check the weather in Oslo.",
];
let mut answers: Vec<Vec<String>> = vec![Vec::new(); turns.len()];
let mut run_err = None;
for (i, turn) in turns.iter().enumerate() {
match collect_text_parts(r.run(turn)).await {
Ok(a) => answers[i] = a,
Err(e) => {
run_err = Some((i, e));
break;
}
}
}
let captured = prompts.lock().clone();
for (i, p) in captured.iter().enumerate() {
assert_no_orphan_function_responses(i, p);
}
if let Some((i, e)) = run_err {
panic!("turn {} ({:?}) failed: {e}", i + 1, turns[i]);
}
let events = session_events_for(&r).await;
let summaries: Vec<&Event> = events.iter().filter(|e| has_usable_summary(e)).collect();
assert!(
!summaries.is_empty(),
"no compaction event after {} turns, so this test exercised nothing",
turns.len()
);
let covered = summaries[0].actions.compaction.as_ref().unwrap();
let summary_text = text_of(covered.compacted_content.as_ref());
assert!(
!summary_text.trim().is_empty(),
"the stored summary is empty"
);
assert!(
captured.len() >= turns.len(),
"captured {} prompts, want at least {}",
captured.len(),
turns.len()
);
let last = captured.last().unwrap();
let final_text = prompt_text_of(last);
assert!(
final_text.contains(summary_text.trim()),
"final prompt does not contain the stored summary.\nsummary:\n{summary_text}\n\nprompt:\n{final_text}"
);
assert!(
!final_text.contains(turns[0]),
"final prompt still contains the compacted first turn {:?}:\n{final_text}",
turns[0]
);
assert!(
final_text.contains(turns[turns.len() - 1]),
"final prompt is missing the current turn {:?}:\n{final_text}",
turns[turns.len() - 1]
);
assert!(
!final_text.contains(turns[1]),
"final prompt still contains the compacted second turn {:?}:\n{final_text}",
turns[1]
);
assert!(
events.len() >= turns.len(),
"the session holds {} events for {} turns, so history was deleted rather than compacted",
events.len(),
turns.len()
);
for want in turns {
assert!(
session_holds_text(&events, want),
"turn {want:?} is no longer in the session: compaction must leave history intact"
);
}
let recall = answers[2].join(" ").to_lowercase();
assert!(
recall.contains("teal"),
"the model could not recall the color from the summary alone; answer was {recall:?}"
);
let outside_summary = final_text.replace(summary_text.trim(), "");
let mut checked = 0;
for ev in &events {
if ev.actions.compaction.is_some()
|| ev.timestamp < covered.start_timestamp
|| ev.timestamp > covered.end_timestamp
{
continue;
}
let Some(c) = content(ev) else { continue };
for part in &c.parts {
let text = part.text.trim();
if !text.is_empty() {
checked += 1;
assert!(
!outside_summary.contains(text),
"event {:?} is covered by the summary but its text is still in the final prompt: {:?}",
ev.id,
part.text
);
}
if let Some(fc) = part.function_call.as_ref().filter(|fc| !fc.id.is_empty()) {
checked += 1;
assert!(
!prompt_mentions_call_id(last, &fc.id),
"event {:?} is covered but its function call {:?} is still in the final prompt",
ev.id,
fc.id
);
}
if let Some(fr) = part
.function_response
.as_ref()
.filter(|fr| !fr.id.is_empty())
{
checked += 1;
assert!(
!prompt_mentions_call_id(last, &fr.id),
"event {:?} is covered but its function response {:?} is still in the final prompt",
ev.id,
fr.id
);
}
}
}
assert!(
checked >= 4,
"the covered-range sweep made {checked} comparisons, want at least 4: it is not examining what it claims to"
);
let with_summary_and_tools = captured
.iter()
.filter(|p| {
prompt_has_function_traffic(p) && prompt_text_of(p).contains(summary_text.trim())
})
.count();
assert!(
with_summary_and_tools > 0,
"no recorded prompt carries both a summary and function traffic, so pairing across a summary was never exercised"
);
}