pub mod agents;
pub mod config_entries;
pub mod context;
pub mod hooks;
pub mod mcp;
pub mod skills;
pub mod variants;
pub mod visibility;
use std::path::Path;
use crate::config::AgentEmission;
use crate::diagnostic::DiagnosticCollector;
use crate::error::MarsError;
use crate::model::ReaderIr;
use crate::sync::{
SyncReport, SyncRequest,
apply::{ActionOutcome, ActionTaken},
apply_plan, build_target, check_frozen_gate, create_plan, finalize, sync_targets,
};
use crate::types::MarsContext;
pub fn compile(
ctx: &MarsContext,
ir: ReaderIr,
request: &SyncRequest,
diag: &mut DiagnosticCollector,
) -> Result<SyncReport, MarsError> {
let targeted = build_target(ctx, ir.resolved, ir.local_items, request, diag)?;
let planned = create_plan(ctx, targeted, request, diag)?;
if request.options.frozen {
check_frozen_gate(&planned)?;
}
let applied = apply_plan(ctx, planned, request)?;
let agent_surface_policy = agent_surface_policy(
applied
.planned
.targeted
.resolved
.loaded
.config
.settings
.agent_emission
.as_ref(),
ctx.meridian_managed,
);
let mars_dir = ctx.project_root.join(".mars");
reconcile_native_agent_surfaces(
agent_surface_policy,
&ctx.project_root,
&mars_dir,
&applied.applied.outcomes,
request.options.dry_run,
diag,
);
if matches!(agent_surface_policy, AgentSurfacePolicy::EmitAll) {
dual_surface_compile(&ctx.project_root, &mars_dir, request.options.dry_run, diag);
}
let config_entry_records =
config_entries::compile_config_entries(ctx, &applied, request.options.dry_run, diag);
let mut synced = sync_targets(ctx, applied, request, agent_surface_policy, diag);
synced.config_entries = config_entry_records;
finalize(ctx, synced, request, diag)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentSurfacePolicy {
EmitAll,
SuppressAll,
}
pub fn agent_surface_policy(
agent_emission: Option<&AgentEmission>,
meridian_managed: bool,
) -> AgentSurfacePolicy {
match agent_emission.unwrap_or(&AgentEmission::Auto) {
AgentEmission::Always => AgentSurfacePolicy::EmitAll,
AgentEmission::Never => AgentSurfacePolicy::SuppressAll,
AgentEmission::Auto if meridian_managed => AgentSurfacePolicy::SuppressAll,
AgentEmission::Auto => AgentSurfacePolicy::EmitAll,
}
}
pub fn suppress_agent_outcomes(outcomes: &[ActionOutcome]) -> Vec<ActionOutcome> {
outcomes
.iter()
.cloned()
.map(|mut outcome| {
if outcome.item_id.kind == crate::lock::ItemKind::Agent {
outcome.action = ActionTaken::Removed;
}
outcome
})
.collect()
}
fn reconcile_native_agent_surfaces(
policy: AgentSurfacePolicy,
project_root: &Path,
mars_dir: &Path,
outcomes: &[crate::sync::apply::ActionOutcome],
dry_run: bool,
diag: &mut DiagnosticCollector,
) {
use crate::lock::ItemKind;
if matches!(policy, AgentSurfacePolicy::SuppressAll) {
remove_current_native_agent_surfaces(project_root, mars_dir, dry_run, diag);
}
for outcome in outcomes {
if outcome.item_id.kind != ItemKind::Agent
|| !matches!(outcome.action, ActionTaken::Removed)
{
continue;
}
let agent_name = outcome.dest_path.item_name(ItemKind::Agent);
remove_native_agent_shapes(project_root, &agent_name, dry_run, diag);
}
}
fn remove_current_native_agent_surfaces(
project_root: &Path,
mars_dir: &Path,
dry_run: bool,
diag: &mut DiagnosticCollector,
) {
use crate::compiler::agents::parse_agent_content;
let agents_dir = mars_dir.join("agents");
let Ok(entries) = std::fs::read_dir(&agents_dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.extension().is_none_or(|ext| ext != "md") {
continue;
}
let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
diag.warn(
"native-agent-remove-read",
format!("could not read {}: {e}", path.display()),
);
continue;
}
};
let mut agent_diags = Vec::new();
let (profile, _fm) = match parse_agent_content(&content, &mut agent_diags) {
Ok(r) => r,
Err(e) => {
diag.warn(
"native-agent-remove-parse",
format!("could not parse {}: {e}", path.display()),
);
continue;
}
};
let agent_name = profile.name.as_deref().unwrap_or_else(|| {
path.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown")
});
remove_native_agent_shapes(project_root, agent_name, dry_run, diag);
}
}
fn remove_native_agent_shapes(
project_root: &Path,
agent_name: &str,
dry_run: bool,
diag: &mut DiagnosticCollector,
) {
use crate::compiler::agents::HarnessKind;
for harness in HarnessKind::all() {
let target = harness.target_dir();
for extension in ["md", "toml"] {
let native_path = project_root
.join(target)
.join("agents")
.join(format!("{agent_name}.{extension}"));
if !native_path.exists() && native_path.symlink_metadata().is_err() {
continue;
}
if dry_run {
continue;
}
if let Err(e) = crate::reconcile::fs_ops::safe_remove(&native_path) {
diag.warn(
"native-agent-remove",
format!("could not remove {}: {e}", native_path.display()),
);
}
}
}
}
fn dual_surface_compile(
project_root: &Path,
mars_dir: &Path,
dry_run: bool,
diag: &mut DiagnosticCollector,
) {
use crate::compiler::agents::HarnessKind;
use crate::compiler::agents::lower::lower_for_harness;
use crate::compiler::agents::parse_agent_content;
let agents_dir = mars_dir.join("agents");
let Ok(entries) = std::fs::read_dir(&agents_dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
let Some(ext) = path.extension() else {
continue;
};
if ext != "md" {
continue;
}
let content = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
diag.warn(
"dual-surface-read",
format!("could not read {}: {e}", path.display()),
);
continue;
}
};
let mut agent_diags = Vec::new();
let (profile, fm) = match parse_agent_content(&content, &mut agent_diags) {
Ok(r) => r,
Err(e) => {
diag.warn(
"dual-surface-parse",
format!("could not parse {}: {e}", path.display()),
);
continue;
}
};
let agent_name = profile.name.as_deref().unwrap_or_else(|| {
path.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown")
});
for d in &agent_diags {
if d.is_error() {
diag.warn(
"agent-schema-error",
format!("agent `{agent_name}`: {}", d.message()),
);
} else {
diag.warn(
"agent-schema-warning",
format!("agent `{agent_name}`: {}", d.message()),
);
}
}
let Some(harness) = &profile.harness else {
continue;
};
let body = fm.body().to_string();
let lowered = lower_for_harness(harness, &profile, &fm, &body);
for lf in &lowered.lossy_fields {
use crate::compiler::agents::lower::Lossiness;
match &lf.classification {
Lossiness::Dropped => {
if lf.field == "tools" || lf.field.starts_with("tools.") {
diag.warn(
"agent-field-dropped",
format!(
"agent `{agent_name}`: field `{}` dropped in {}",
lf.field, lf.target
),
);
}
}
Lossiness::MeridianOnly => {}
Lossiness::Approximate { note } => {
diag.warn(
"agent-field-approximate",
format!(
"agent `{agent_name}`: field `{}` approximately mapped in {} ({note})",
lf.field, lf.target
),
);
}
}
}
let harness_dir = project_root.join(harness.target_dir());
let native_agents_dir = harness_dir.join("agents");
let file_name = match harness {
HarnessKind::Codex => format!("{agent_name}.toml"),
_ => format!("{agent_name}.md"),
};
let native_path = native_agents_dir.join(&file_name);
if !dry_run {
if let Err(e) = std::fs::create_dir_all(&native_agents_dir) {
diag.warn(
"dual-surface-mkdir",
format!("could not create {}: {e}", native_agents_dir.display()),
);
continue;
}
if let Err(e) = crate::fs::atomic_write(&native_path, &lowered.bytes) {
diag.warn(
"dual-surface-write",
format!("could not write {}: {e}", native_path.display()),
);
}
}
}
}
#[cfg(test)]
mod skill_surface_tests {
use super::*;
use crate::compiler::agents::HarnessKind;
use crate::diagnostic::DiagnosticCollector;
use crate::lock::{ItemId, ItemKind};
use crate::sync::apply::{ActionOutcome, ActionTaken};
use crate::types::{DestPath, ItemName};
use tempfile::TempDir;
#[test]
fn native_agent_emission_defaults_to_standalone_auto() {
assert_eq!(
agent_surface_policy(None, false),
AgentSurfacePolicy::EmitAll
);
}
#[test]
fn native_agent_emission_auto_suppresses_meridian_managed() {
assert_eq!(
agent_surface_policy(Some(&AgentEmission::Auto), true),
AgentSurfacePolicy::SuppressAll
);
}
#[test]
fn native_agent_emission_always_ignores_meridian_managed() {
assert_eq!(
agent_surface_policy(Some(&AgentEmission::Always), true),
AgentSurfacePolicy::EmitAll
);
}
#[test]
fn native_agent_emission_never_suppresses_standalone() {
assert_eq!(
agent_surface_policy(Some(&AgentEmission::Never), false),
AgentSurfacePolicy::SuppressAll
);
}
fn agent_outcome(name: &str, action: ActionTaken) -> ActionOutcome {
ActionOutcome {
item_id: ItemId {
kind: ItemKind::Agent,
name: ItemName::from(name),
},
action,
dest_path: DestPath::from(format!("agents/{name}.md")),
source_name: "test-source".into(),
source_checksum: None,
installed_checksum: None,
}
}
#[test]
fn reconcile_emit_all_removes_native_shapes_for_removed_agents() {
let dir = TempDir::new().unwrap();
for harness in HarnessKind::all() {
let agents_dir = dir.path().join(harness.target_dir()).join("agents");
std::fs::create_dir_all(&agents_dir).unwrap();
std::fs::write(agents_dir.join("coder.md"), "# Old\n").unwrap();
std::fs::write(agents_dir.join("coder.toml"), "old = true\n").unwrap();
}
let mut diag = DiagnosticCollector::new();
reconcile_native_agent_surfaces(
AgentSurfacePolicy::EmitAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Removed)],
false,
&mut diag,
);
for harness in HarnessKind::all() {
assert!(
!dir.path()
.join(harness.target_dir())
.join("agents/coder.md")
.exists()
);
assert!(
!dir.path()
.join(harness.target_dir())
.join("agents/coder.toml")
.exists()
);
}
assert!(diag.drain().is_empty());
}
#[test]
fn reconcile_suppress_all_removes_native_shapes_for_current_agents() {
let dir = TempDir::new().unwrap();
let mars_agents = dir.path().join(".mars").join("agents");
std::fs::create_dir_all(&mars_agents).unwrap();
std::fs::write(
mars_agents.join("coder.md"),
"---\nname: coder\n---\n# Coder\n",
)
.unwrap();
for target in [".claude", ".codex", ".opencode"] {
let agents_dir = dir.path().join(target).join("agents");
std::fs::create_dir_all(&agents_dir).unwrap();
std::fs::write(agents_dir.join("coder.md"), "# Native\n").unwrap();
}
let mut diag = DiagnosticCollector::new();
reconcile_native_agent_surfaces(
AgentSurfacePolicy::SuppressAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Installed)],
false,
&mut diag,
);
for target in [".claude", ".codex", ".opencode"] {
assert!(
!dir.path().join(target).join("agents/coder.md").exists(),
"native agent should be removed under SuppressAll for target {target}"
);
}
}
#[test]
fn reconcile_emit_all_preserves_non_removed_agents() {
let dir = TempDir::new().unwrap();
let agents_dir = dir.path().join(".claude").join("agents");
std::fs::create_dir_all(&agents_dir).unwrap();
std::fs::write(agents_dir.join("coder.md"), "# Native\n").unwrap();
let mut diag = DiagnosticCollector::new();
reconcile_native_agent_surfaces(
AgentSurfacePolicy::EmitAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Installed)],
false,
&mut diag,
);
assert!(dir.path().join(".claude/agents/coder.md").exists());
}
#[test]
fn dual_surface_keeps_canonical_tools_for_opencode() {
let dir = TempDir::new().unwrap();
let mars_agents = dir.path().join(".mars").join("agents");
std::fs::create_dir_all(&mars_agents).unwrap();
let canonical = mars_agents.join("coder.md");
let content = "---\nname: coder\nharness: opencode\ntools:\n \"*\": deny\n bash: allow\n---\n# body\n";
std::fs::write(&canonical, content).unwrap();
let mut diag = DiagnosticCollector::new();
dual_surface_compile(dir.path(), &dir.path().join(".mars"), false, &mut diag);
let canonical_after = std::fs::read_to_string(&canonical).unwrap();
assert_eq!(canonical_after, content);
let native = std::fs::read_to_string(dir.path().join(".opencode/agents/coder.md")).unwrap();
assert!(
!native.contains("tools:"),
"opencode artifact must omit tools"
);
}
#[test]
fn dual_surface_emits_warning_for_dropped_tools_field() {
let dir = TempDir::new().unwrap();
let mars_agents = dir.path().join(".mars").join("agents");
std::fs::create_dir_all(&mars_agents).unwrap();
std::fs::write(
mars_agents.join("coder.md"),
"---\nname: coder\nharness: claude\ntools:\n read:\n \"*.env\": allow\n---\n# body\n",
)
.unwrap();
let mut diag = DiagnosticCollector::new();
dual_surface_compile(dir.path(), &dir.path().join(".mars"), true, &mut diag);
let diagnostics = diag.drain();
assert!(
diagnostics
.iter()
.any(|d| d.code == "agent-field-dropped" && d.message.contains("tools.read"))
);
}
}