use std::path::PathBuf;
fn crate_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn discover_agent_manifests() -> Vec<(String, PathBuf)> {
let agents_dir = crate_root().join("agents");
let mut manifests = Vec::new();
if let Ok(entries) = std::fs::read_dir(&agents_dir) {
for entry in entries.flatten() {
let manifest = entry.path().join("agent.leviath");
if manifest.exists() {
let name = entry.file_name().to_string_lossy().to_string();
manifests.push((name, manifest));
}
}
}
manifests.sort_by(|a, b| a.0.cmp(&b.0));
manifests
}
#[test]
fn all_builtin_agents_parse_successfully() {
let manifests = discover_agent_manifests();
assert!(
!manifests.is_empty(),
"No agent manifests found - check agents/ directory"
);
for (name, path) in &manifests {
let content = std::fs::read_to_string(path)
.unwrap_or_else(|e| panic!("Failed to read {}: {}", path.display(), e));
let blueprint = leviath_core::manifest::parse_manifest(&content)
.unwrap_or_else(|e| panic!("Failed to parse agent '{}': {}", name, e));
assert!(
!blueprint.name.is_empty(),
"Agent '{}' has empty name",
name
);
assert!(
!blueprint.stages.is_empty(),
"Agent '{}' has no stages",
name
);
}
}
#[test]
fn all_builtin_agents_validate() {
let manifests = discover_agent_manifests();
for (name, path) in &manifests {
let content = std::fs::read_to_string(path).unwrap();
let blueprint = leviath_core::manifest::parse_manifest(&content).unwrap();
blueprint
.validate()
.unwrap_or_else(|e| panic!("Agent '{}' failed validation: {:?}", name, e));
}
}
#[test]
fn all_builtin_agents_have_valid_entry_stage() {
let manifests = discover_agent_manifests();
for (name, path) in &manifests {
let content = std::fs::read_to_string(path).unwrap();
let blueprint = leviath_core::manifest::parse_manifest(&content).unwrap();
let entry = blueprint.resolve_entry_stage_name();
assert!(
blueprint.find_stage(&entry).is_some(),
"Agent '{}' entry stage '{}' not found in stages",
name,
entry
);
}
}
#[test]
fn all_builtin_agents_transition_targets_exist() {
let manifests = discover_agent_manifests();
for (name, path) in &manifests {
let content = std::fs::read_to_string(path).unwrap();
let blueprint = leviath_core::manifest::parse_manifest(&content).unwrap();
for stage in &blueprint.stages {
if let Some(ref transitions) = stage.transitions {
for target_name in transitions.keys() {
assert!(
blueprint.find_stage(target_name).is_some(),
"Agent '{}', stage '{}': transition target '{}' not found",
name,
stage.name,
target_name
);
}
}
}
}
}
#[test]
fn specific_agent_coder_has_expected_structure() {
let path = crate_root().join("agents/coder/agent.leviath");
let content = std::fs::read_to_string(&path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
assert_eq!(bp.name, "coder");
assert!(bp.stages.len() >= 2);
assert!(bp.find_stage("plan").is_some());
assert!(bp.find_stage("implement").is_some());
let plan = bp.find_stage("plan").unwrap();
assert!(plan.transitions.is_some());
}
#[test]
fn agent_written_required_regions_have_a_stage_that_can_write_them() {
use leviath_core::layout::RegionSeed;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
let agent_written: Vec<&str> = bp
.context_layout
.regions
.iter()
.filter(|r| r.required)
.filter(|r| !matches!(r.seed, Some(RegionSeed::CallerInput { .. })))
.map(|r| r.name.as_str())
.collect();
if agent_written.is_empty() {
continue;
}
let writers: Vec<&str> = bp
.stages
.iter()
.filter(|s| {
s.available_tools
.iter()
.any(|t| t == "context_write" || t == "context_append")
})
.map(|s| s.name.as_str())
.collect();
assert!(
!writers.is_empty(),
"agent '{name}' marks {agent_written:?} required but no stage has \
context_write/context_append - the required-region gate is a no-op \
and the region silently stays empty"
);
}
}
#[test]
fn required_regions_are_not_also_seeded_from_the_environment() {
use leviath_core::layout::RegionSeed;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
for region in bp.context_layout.regions.iter().filter(|r| r.required) {
let environmental = matches!(
region.seed,
Some(
RegionSeed::Files { .. } | RegionSeed::Glob { .. } | RegionSeed::Command { .. }
)
);
assert!(
!environmental,
"agent '{name}' region '{}' is required AND seeded from the \
environment - a missing file or failing command would fail the \
spawn outright",
region.name
);
}
}
}
#[test]
fn specific_agent_researcher_has_graph_transitions() {
let path = crate_root().join("agents/researcher/agent.leviath");
let content = std::fs::read_to_string(&path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
assert_eq!(bp.name, "researcher");
let has_transitions = bp.stages.iter().any(|s| s.transitions.is_some());
assert!(
has_transitions || bp.stages.len() > 1,
"Researcher agent should have transitions or multiple stages"
);
}
#[test]
fn all_builtin_agents_have_sound_context_layout() {
use leviath_core::RegionKind;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
let regions = &bp.context_layout.regions;
let conv = regions.iter().find(|r| r.name == "conversation");
assert!(
matches!(
conv.map(|r| &r.kind),
Some(RegionKind::SlidingWindow { .. })
),
"agent '{name}' must declare an explicit `conversation` sliding_window region"
);
let sliding: std::collections::HashSet<&str> = regions
.iter()
.filter(|r| matches!(r.kind, RegionKind::SlidingWindow { .. }))
.map(|r| r.name.as_str())
.collect();
for stage in &bp.stages {
if let Some(routing) = &stage.tool_result_routing {
let mut targets = vec![routing.default_region.as_str()];
targets.extend(routing.tool_overrides.values().map(String::as_str));
for t in targets {
assert!(
t == "conversation" || !sliding.contains(t),
"agent '{name}' stage '{}' routes tool results to non-conversation \
sliding_window region '{t}' (would desync tool_result from tool_use)",
stage.name
);
}
}
}
let hist_sources: std::collections::HashSet<&str> = regions
.iter()
.filter_map(|r| match &r.kind {
RegionKind::CompactHistory { source_region } => Some(source_region.as_str()),
_ => None,
})
.collect();
for r in regions {
if matches!(r.kind, RegionKind::Compacting { .. }) {
assert!(
hist_sources.contains(r.name.as_str()),
"agent '{name}': compacting region '{}' has no paired compact_history region",
r.name
);
}
}
}
}
#[test]
fn all_builtin_stuck_edges_are_armed_and_bounded() {
use leviath_core::TransitionCondition;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
for stage in &bp.stages {
let Some(transitions) = &stage.transitions else {
continue;
};
for (target, edge) in transitions {
if edge.condition != TransitionCondition::Stuck {
continue;
}
assert!(
edge.stuck.is_some_and(|c| c.is_armed()),
"agent '{name}' stage '{}': stuck edge → '{target}' has no threshold, \
so it could never fire",
stage.name
);
assert!(
bp.find_stage(target)
.is_some_and(|s| s.max_revisits.is_some()),
"agent '{name}' stage '{}': stuck edge → '{target}' is unbounded - \
'{target}' needs max_revisits or the two can ping-pong all run",
stage.name
);
}
}
}
}
#[test]
fn builtin_error_edges_have_a_pinned_error_report_region() {
use leviath_core::{RegionKind, TransitionCondition};
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
let error_targets: std::collections::BTreeSet<&str> = bp
.stages
.iter()
.filter_map(|s| s.transitions.as_ref())
.flatten()
.filter(|(_, e)| e.condition == TransitionCondition::Error)
.map(|(target, _)| target.as_str())
.collect();
if error_targets.is_empty() {
continue; }
let pinned: Vec<&str> = bp
.context_layout
.regions
.iter()
.filter(|r| matches!(r.kind, RegionKind::Pinned))
.map(|r| r.name.as_str())
.collect();
assert!(
pinned.contains(&"error_report"),
"agent '{name}' has error edges but no pinned `error_report` region - \
the runtime's error/iteration-cap notes would land in the evictable \
`conversation` window instead"
);
assert_ne!(
pinned.first(),
Some(&"error_report"),
"agent '{name}': `error_report` is the FIRST pinned region, so stage \
instructions would be injected into it (apply_stage_context targets \
the first pinned region) - declare it after the other pinned regions"
);
for target in error_targets {
let stage = bp
.find_stage(target)
.unwrap_or_else(|| panic!("agent '{name}': error edge → unknown stage '{target}'"));
assert!(
stage
.config
.get("system_prompt")
.and_then(|v| v.as_str())
.is_some_and(|p| p.contains("error_report")),
"agent '{name}' stage '{target}' is an error-edge target but its \
system prompt never mentions `error_report` - the model won't \
know where the runtime put the error text"
);
}
}
}
#[test]
fn branching_stages_explain_how_to_choose() {
use leviath_core::TransitionCondition;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let bp = leviath_core::manifest::parse_manifest(&content).unwrap();
for stage in &bp.stages {
let Some(transitions) = &stage.transitions else {
continue;
};
let choosable: Vec<&str> = transitions
.values()
.filter(|e| {
matches!(
e.condition,
TransitionCondition::Always | TransitionCondition::LlmChoice
)
})
.map(|e| e.target.as_str())
.collect();
if choosable.len() < 2 {
continue;
}
let unlabeled: Vec<&str> = transitions
.values()
.filter(|e| {
matches!(
e.condition,
TransitionCondition::Always | TransitionCondition::LlmChoice
) && e.hint.is_none()
})
.map(|e| e.target.as_str())
.collect();
assert!(
stage.transition_prompt.is_some() || unlabeled.is_empty(),
"agent '{name}' stage '{}' branches to {choosable:?} but has no \
transition_prompt, and {unlabeled:?} carry no hint either - the \
model is left to guess which branch to take",
stage.name
);
}
}
}
#[test]
fn builtin_required_tools_are_offered_and_belong_to_an_interactive_stage() {
use leviath_core::blueprint::StageMode;
for (name, path) in &discover_agent_manifests() {
let content = std::fs::read_to_string(path).unwrap();
let blueprint = leviath_core::manifest::parse_manifest(&content).unwrap();
for stage in &blueprint.stages {
for tool in &stage.required_tools {
assert!(
stage.available_tools.contains(tool),
"agent '{name}' stage '{}' keeps '{tool}' through an unattended run \
but never offers it",
stage.name
);
}
assert!(
stage.required_tools.is_empty()
|| matches!(stage.mode, StageMode::InteractivePoints { .. }),
"agent '{name}' stage '{}' holds {:?} for a person, but declares no \
interaction point - an unattended run would park there with nobody \
watching",
stage.name,
stage.required_tools
);
}
}
}
#[test]
fn the_bundled_agents_exist_and_are_discoverable() {
let manifests = discover_agent_manifests();
assert!(
!manifests.is_empty(),
"the binary ships no agents; build.rs found no agents/ directory"
);
}
#[test]
fn a_stage_that_can_fan_out_offers_no_shortcut_past_it() {
use leviath_core::blueprint::{StageMode, TransitionCondition};
let manifests = discover_agent_manifests();
let mut checked = 0;
for (name, path) in &manifests {
let content = std::fs::read_to_string(path).unwrap();
let blueprint = leviath_core::manifest::parse_manifest(&content).unwrap();
let is_fan_out = |target: &str| {
blueprint
.stages
.iter()
.any(|s| s.name == target && matches!(s.mode, StageMode::FanOut { .. }))
};
let is_output = |target: &str| {
blueprint
.stages
.iter()
.any(|s| s.name == target && matches!(s.mode, StageMode::Output))
};
for stage in &blueprint.stages {
let Some(edges) = &stage.transitions else {
continue;
};
if !edges.values().any(|e| is_fan_out(&e.target)) {
continue;
}
checked += 1;
let shortcuts: Vec<&str> = edges
.values()
.filter(|e| {
is_output(&e.target)
&& matches!(
e.condition,
TransitionCondition::Always | TransitionCondition::LlmChoice
)
})
.map(|e| e.target.as_str())
.collect();
assert!(
shortcuts.is_empty(),
"{name}: stage '{}' can fan out, but also offers the model a \
plain edge straight to {shortcuts:?}. Gate it with \
condition = \"dead_end\" so it is taken only when nothing \
else can be.",
stage.name
);
}
}
assert!(
checked > 0,
"no bundled agent has a stage that can fan out, so this proves nothing"
);
}