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,
&applied.planned.targeted.resolved.loaded.old_lock,
request.options.dry_run,
diag,
);
let compiled_native_outputs = if matches!(agent_surface_policy, AgentSurfacePolicy::EmitAll) {
dual_surface_compile(
&ctx.project_root,
&mars_dir,
&applied.planned.targeted.resolved.loaded.old_lock,
request.options.force,
crate::surface_ownership::CollisionAdoptHint::SyncForce,
request.options.dry_run,
diag,
)
} else {
Vec::new()
};
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;
synced.compiled_native_outputs = compiled_native_outputs;
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],
old_lock: &crate::lock::LockFile,
dry_run: bool,
diag: &mut DiagnosticCollector,
) {
use crate::lock::ItemKind;
if matches!(policy, AgentSurfacePolicy::SuppressAll) {
remove_current_native_agent_surfaces(project_root, mars_dir, old_lock, 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, old_lock, dry_run, diag);
}
}
fn remove_current_native_agent_surfaces(
project_root: &Path,
mars_dir: &Path,
old_lock: &crate::lock::LockFile,
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, old_lock, dry_run, diag);
}
}
fn remove_native_agent_shapes(
project_root: &Path,
agent_name: &str,
old_lock: &crate::lock::LockFile,
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 dest_rel = format!("agents/{agent_name}.{extension}");
if !old_lock.contains_output(target, &dest_rel) {
continue;
}
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,
old_lock: &crate::lock::LockFile,
force: bool,
collision_hint: crate::surface_ownership::CollisionAdoptHint,
dry_run: bool,
diag: &mut DiagnosticCollector,
) -> Vec<(String, String, crate::types::ContentHash)> {
use crate::compiler::agents::HarnessKind;
use crate::compiler::agents::lower::lower_for_harness;
use crate::compiler::agents::parse_agent_content;
use crate::surface_ownership::{self, SurfaceCopyDecision};
let agents_dir = mars_dir.join("agents");
let Ok(entries) = std::fs::read_dir(&agents_dir) else {
return Vec::new();
};
let mut records = Vec::new();
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 | 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);
let dest_rel = format!("agents/{file_name}");
let target_dir = harness.target_dir();
let dest_exists = surface_ownership::target_dest_exists(&native_path);
match surface_ownership::copy_decision(old_lock, target_dir, &dest_rel, dest_exists, force)
{
SurfaceCopyDecision::SkipUnmanagedCollision => {
surface_ownership::warn_unmanaged_collision(
target_dir,
&dest_rel,
collision_hint,
diag,
);
continue;
}
SurfaceCopyDecision::Proceed => {
if dest_exists && force && !old_lock.contains_output(target_dir, &dest_rel) {
surface_ownership::warn_unmanaged_adopted(
target_dir,
&dest_rel,
collision_hint,
diag,
);
}
}
}
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()),
);
} else {
let checksum =
crate::types::ContentHash::from(crate::hash::hash_bytes(&lowered.bytes));
records.push((target_dir.to_string(), dest_rel, checksum));
}
}
}
records
}
#[cfg(test)]
mod skill_surface_tests {
use super::*;
use crate::compiler::agents::HarnessKind;
use crate::diagnostic::DiagnosticCollector;
use crate::lock::{ItemId, ItemKind, LockFile, LockedItemV2, OutputRecord};
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 lock_with_target_outputs(targets: &[&str], dest: &str, checksum: &str) -> LockFile {
let mut lock = LockFile::empty();
let outputs = targets
.iter()
.map(|target| OutputRecord {
target_root: (*target).to_string(),
dest_path: dest.into(),
installed_checksum: checksum.into(),
})
.collect();
lock.items.insert(
"agent/coder".to_string(),
LockedItemV2 {
source: "test".into(),
kind: ItemKind::Agent,
version: None,
source_checksum: "sha256:src".into(),
outputs,
},
);
lock
}
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 tracked_targets: Vec<&str> =
HarnessKind::all().iter().map(|h| h.target_dir()).collect();
let mut lock =
lock_with_target_outputs(&tracked_targets, "agents/coder.md", "sha256:coder");
for target in &tracked_targets {
lock.items
.get_mut("agent/coder")
.unwrap()
.outputs
.push(OutputRecord {
target_root: (*target).to_string(),
dest_path: "agents/coder.toml".into(),
installed_checksum: "sha256:coder-toml".into(),
});
}
let mut diag = DiagnosticCollector::new();
reconcile_native_agent_surfaces(
AgentSurfacePolicy::EmitAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Removed)],
&lock,
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();
let lock = lock_with_target_outputs(
&[".claude", ".codex", ".opencode"],
"agents/coder.md",
"sha256:coder",
);
reconcile_native_agent_surfaces(
AgentSurfacePolicy::SuppressAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Installed)],
&lock,
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_suppress_all_preserves_untracked_native_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();
let agents_dir = dir.path().join(".cursor").join("agents");
std::fs::create_dir_all(&agents_dir).unwrap();
std::fs::write(agents_dir.join("coder.md"), "# hand-written\n").unwrap();
let mut diag = DiagnosticCollector::new();
reconcile_native_agent_surfaces(
AgentSurfacePolicy::SuppressAll,
dir.path(),
&dir.path().join(".mars"),
&[agent_outcome("coder", ActionTaken::Installed)],
&LockFile::empty(),
false,
&mut diag,
);
assert!(dir.path().join(".cursor/agents/coder.md").exists());
}
#[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)],
&LockFile::empty(),
false,
&mut diag,
);
assert!(dir.path().join(".claude/agents/coder.md").exists());
}
}