use crate::brain::agent::context::{AgentContext, COMPACTION_MARKER_PREFIX, CompactionScope};
use crate::brain::agent::service::AgentService;
use crate::brain::provider::{ContentBlock, Message};
const SUMMARY: &str = "## Summary\n\nThe user asked about token accounting.";
fn ctx_with_history() -> AgentContext {
let mut c = AgentContext::new(uuid::Uuid::nil(), 200_000);
for i in 0..6 {
c.add_message(Message::user(format!("turn {i}")));
c.add_message(Message::assistant(format!("reply {i}")));
}
c
}
fn text_of(msg: &Message) -> String {
msg.content
.iter()
.filter_map(|b| match b {
ContentBlock::Text { text } => Some(text.clone()),
_ => None,
})
.collect::<Vec<_>>()
.join(" ")
}
#[test]
fn the_marker_body_is_the_summary_verbatim() {
let mut c = ctx_with_history();
AgentService::apply_scoped_compaction_summary(&mut c, CompactionScope::FullWindow, SUMMARY);
let marker = text_of(&c.messages[0]);
assert!(marker.starts_with(COMPACTION_MARKER_PREFIX));
assert!(
marker.ends_with(SUMMARY),
"nothing may be appended after the summary: {marker}"
);
assert!(
!marker.contains("RECOVERED BRAIN CONTEXT"),
"brain files are in the system prompt every turn; welding them here \
duplicates them and desyncs the in-memory marker from the DB one"
);
assert!(
!marker.contains("No brain files found"),
"the absent-brain-files placeholder went with the weld"
);
}
#[test]
fn the_marker_costs_only_what_the_summary_costs() {
let mut c = ctx_with_history();
AgentService::apply_scoped_compaction_summary(&mut c, CompactionScope::FullWindow, SUMMARY);
let marker = text_of(&c.messages[0]);
assert!(
marker.len() < SUMMARY.len() + 512,
"marker is summary + one banner line, not summary + 64KB of brain files: \
{} bytes",
marker.len()
);
}
#[test]
fn every_scope_keeps_the_body_clean() {
for scope in [
CompactionScope::FullWindow,
CompactionScope::DeltaSinceMarker,
CompactionScope::SegmentConsolidation,
] {
let mut c = ctx_with_history();
AgentService::apply_scoped_compaction_summary(&mut c, scope, SUMMARY);
let welded = c
.messages
.iter()
.any(|m| text_of(m).contains("RECOVERED BRAIN CONTEXT"));
assert!(!welded, "{scope:?} welded brain files onto the marker");
}
}