use std::sync::Arc;
use gadk_agent::{Agent, AgentError, Loader, go_quote};
use gadk_platform::GoWrapped;
use gadk_session::compaction::Config;
use crate::runner::{Runner, RunnerConfig};
pub fn against_agents(
cfg: Option<&Config>,
loader: Option<&dyn Loader>,
base: &RunnerConfig,
) -> Result<(), AgentError> {
let Some(cfg) = cfg else {
return Ok(());
};
let Some(loader) = loader else {
return check(cfg, Vec::new(), base);
};
let agents = loader
.list_agents()
.into_iter()
.filter_map(|name| loader.load_agent(&name).ok().map(|a| (name, a)))
.collect();
check(cfg, agents, base)
}
pub fn against_root_agent(
cfg: Option<&Config>,
root: Option<Arc<dyn Agent>>,
base: &RunnerConfig,
) -> Result<(), AgentError> {
let Some(cfg) = cfg else {
return Ok(());
};
let agents = root
.map(|r| vec![(r.name().to_owned(), r)])
.unwrap_or_default();
check(cfg, agents, base)
}
fn check(
cfg: &Config,
agents: Vec<(String, Arc<dyn Agent>)>,
base: &RunnerConfig,
) -> Result<(), AgentError> {
cfg.validate()
.map_err(|e| AgentError::Other(Box::new(GoWrapped::new("invalid Compaction", e))))?;
for (name, agent) in agents {
let mut without = base.clone();
without.app_name = name.clone();
without.agent = agent;
without.compaction = None;
if Runner::new(without.clone()).is_err() {
continue;
}
let mut with = without;
with.compaction = Some(cfg.clone());
if let Err(e) = Runner::new(with) {
return Err(AgentError::Other(Box::new(GoWrapped::new(
format!("the Compaction config cannot serve app {}", go_quote(&name)),
e,
))));
}
}
Ok(())
}