use super::*;
use crate::agents::manifest::{MANIFEST_FILE, ManifestEntry, Origin, checksum, manifest_lock_path};
use crate::agents::quarantine_receipt::shell_quote;
use std::collections::HashMap;
use tempfile::TempDir;
fn tm_agent(name: &str) -> String {
format!(
"---\nname: {name}\nrole: qa\ndescription: 'Composed by tm.'\nmodel: sonnet\n\
skills: [systematic-debugging]\ninitialPrompt: \"Begin.\"\n---\n\n\
# BASE-AGENT — Foundation for all trusty-mpm agents\n\n\
Root-level instructions composed into every deployed agent.\n\n# {name}\n\nBody.\n"
)
}
fn claude_mpm_agent(name: &str) -> String {
format!(
"---\nname: {name}\ndescription: \"Use this agent when…\"\nmodel: sonnet\n\
effort: balanced\nagent_type: qa\nversion: \"1.0.0\"\nskills:\n- code-review-standards\n\
initialPrompt: \"Begin.\"\n---\n# {name}\n\n**Inherits from**: BASE_AGENT.md\n"
)
}
fn roster(names: &[&str]) -> BTreeSet<String> {
names.iter().map(|n| (*n).to_owned()).collect()
}
struct Fixture {
_tmp: TempDir,
project: PathBuf,
tier: PathBuf,
backups: PathBuf,
}
impl Fixture {
fn new() -> Self {
let f = Self::without_git();
let out = std::process::Command::new("git")
.arg("-C")
.arg(&f.project)
.args(["init", "-q"])
.output()
.expect("git must be available to run the quarantine tests");
assert!(out.status.success(), "git init failed");
f
}
fn without_git() -> Self {
let tmp = TempDir::new().expect("tempdir");
let project = tmp.path().to_path_buf();
let tier = project.join(".claude").join("agents");
std::fs::create_dir_all(&tier).expect("create tier");
let backups = project.join(".trusty-mpm").join("agent-quarantine");
Self {
_tmp: tmp,
project,
tier,
backups,
}
}
fn write(&self, file_name: &str, content: &str) -> PathBuf {
let path = self.tier.join(file_name);
std::fs::write(&path, content).expect("write agent");
path
}
fn git_add(&self, rel: &str) {
let out = std::process::Command::new("git")
.arg("-C")
.arg(&self.project)
.args(["add", "--force", rel])
.output()
.expect("spawn git add");
assert!(out.status.success(), "git add {rel} failed");
}
fn ledger(&self, file_name: &str, content: &str, origin: Origin) {
let mut managed = HashMap::new();
managed.insert(
file_name.to_owned(),
ManifestEntry {
source_chain: vec![file_name.trim_end_matches(".md").to_owned()],
checksum: checksum(content),
deployed_at: "2026-08-03T00:00:00Z".to_owned(),
origin,
},
);
AgentManifest {
managed,
..AgentManifest::default()
}
.save(&self.tier)
.expect("save ledger");
}
fn sweep(&self, bundled: &BTreeSet<String>) -> Result<QuarantineReport, QuarantineError> {
quarantine_shadowing_agents(&self.tier, &self.backups, bundled, "run-1")
}
}
#[test]
fn quarantine_moves_an_untracked_trusty_mpm_shadow() {
let f = Fixture::new();
let content = tm_agent("qa");
let original = f.write("qa.md", &content);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
let moved = &report.moved[0];
assert_eq!(moved.name, "qa");
assert_eq!(moved.from, original);
assert_eq!(moved.to, f.tier.join("qa.md.disabled"));
assert!(!original.exists(), "the shadowing file must be gone");
assert!(moved.to.exists(), "the inert sibling must exist");
assert_eq!(
std::fs::read_to_string(&moved.to).expect("read moved"),
content,
"the moved file must be byte-identical"
);
assert_eq!(
std::fs::read_to_string(&moved.backup).expect("read backup"),
content,
"the backup must be byte-identical"
);
assert!(moved.backup.starts_with(&f.backups));
}
#[test]
fn a_quarantined_file_is_no_longer_an_agent_file() {
let f = Fixture::new();
f.write("qa.md", &tm_agent("qa"));
f.sweep(&roster(&["qa"])).expect("sweep");
assert!(!is_agent_file("qa.md.disabled"));
assert!(candidates(&f.tier).is_empty(), "nothing left to sweep");
}
#[test]
fn quarantine_follows_the_declared_name_not_the_filename() {
let f = Fixture::new();
f.write("helper.md", &tm_agent("rust-engineer"));
let report = f.sweep(&roster(&["rust-engineer"])).expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
assert_eq!(report.moved[0].name, "rust-engineer");
}
#[test]
fn quarantine_works_without_a_repository() {
let f = Fixture::without_git();
f.write("qa.md", &tm_agent("qa"));
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
}
#[test]
fn quarantine_never_moves_a_git_tracked_file() {
let f = Fixture::new();
let content = tm_agent("qa");
let path = f.write("qa.md", &content);
f.git_add(".claude/agents/qa.md");
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped.len(), 1);
assert_eq!(report.skipped[0].reason, SkipReason::GitTracked);
assert!(
path.exists(),
"a tracked file must be left exactly where it is"
);
assert_eq!(std::fs::read_to_string(&path).expect("read"), content);
assert!(
!f.tier.join("qa.md.disabled").exists(),
"no inert sibling may be created for a tracked file"
);
}
#[test]
fn quarantine_never_moves_a_claude_mpm_file_on_a_colliding_name() {
let f = Fixture::new();
let content = claude_mpm_agent("qa");
let path = f.write("qa.md", &content);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::ClaudeMpmSchema);
assert_eq!(std::fs::read_to_string(&path).expect("read"), content);
}
#[test]
fn quarantine_refuses_a_tracked_file_when_git_cannot_be_read() {
let f = Fixture::new();
let content = tm_agent("qa");
let original = f.write("qa.md", &content);
f.git_add(".claude/agents/qa.md");
let healthy = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(healthy.moved.is_empty(), "report: {healthy:?}");
assert_eq!(healthy.skipped[0].reason, SkipReason::GitTracked);
let out = std::process::Command::new("git")
.arg("-C")
.arg(&f.project)
.args(["config", "core.repositoryformatversion", "99"])
.output()
.expect("spawn git config");
assert!(out.status.success(), "could not break the repo");
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(
report.moved.is_empty(),
"a COMMITTED file must never move because git failed to answer: {report:?}"
);
assert_eq!(report.skipped[0].reason, SkipReason::VcsUnknown);
assert!(original.exists(), "the committed file must still be there");
assert_eq!(std::fs::read_to_string(&original).expect("read"), content);
assert!(!f.tier.join("qa.md.disabled").exists());
}
#[test]
#[cfg(unix)]
fn quarantine_refuses_a_tracked_file_when_the_git_dir_is_unreadable() {
use std::os::unix::fs::PermissionsExt;
struct ModeGuard(PathBuf);
impl Drop for ModeGuard {
fn drop(&mut self) {
let _ = std::fs::set_permissions(&self.0, std::fs::Permissions::from_mode(0o755));
}
}
let f = Fixture::new();
let content = tm_agent("qa");
let original = f.write("qa.md", &content);
f.git_add(".claude/agents/qa.md");
let healthy = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(healthy.moved.is_empty(), "report: {healthy:?}");
assert_eq!(healthy.skipped[0].reason, SkipReason::GitTracked);
let git_dir = f.project.join(".git");
let _guard = ModeGuard(git_dir.clone());
std::fs::set_permissions(&git_dir, std::fs::Permissions::from_mode(0o000)).expect("chmod .git");
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(
report.moved.is_empty(),
"a COMMITTED file must never move because git could not read .git: {report:?}"
);
assert_eq!(report.skipped[0].reason, SkipReason::VcsUnknown);
assert!(original.exists(), "the committed file must still be there");
assert_eq!(std::fs::read_to_string(&original).expect("read"), content);
assert!(!f.tier.join("qa.md.disabled").exists());
}
#[test]
fn quarantine_never_moves_a_minimal_hand_authored_file() {
let f = Fixture::new();
let content = "---\nname: qa\nrole: qa\ndescription: Ours.\nmodel: sonnet\n---\n\n\
# Our QA\n\nHand-written by the operator.\n";
let path = f.write("qa.md", content);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::UnrecognizedSchema);
assert_eq!(std::fs::read_to_string(&path).expect("read"), content);
assert!(!f.tier.join("qa.md.disabled").exists());
}
#[test]
fn quarantine_never_moves_a_hand_authored_file() {
let f = Fixture::new();
let path = f.write(
"qa.md",
"---\nname: qa\nrole: qa\ncolor: purple\n---\n\nOurs.\n",
);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::UnrecognizedSchema);
assert!(path.exists());
}
#[test]
fn quarantine_never_moves_a_user_owned_entry() {
let f = Fixture::new();
let content = tm_agent("qa");
let path = f.write("qa.md", &content);
f.ledger("qa.md", &content, Origin::User);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::UserOwned);
assert!(path.exists());
}
#[test]
fn quarantine_never_moves_a_custom_agent() {
let f = Fixture::new();
let path = f.write("acme.md", &tm_agent("acme-custom"));
let report = f.sweep(&roster(&["qa", "rust-engineer"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::NotShadowingBundled);
assert!(path.exists());
}
#[test]
fn quarantine_never_moves_a_stranded_framework_file() {
let f = Fixture::new();
let content = tm_agent("retired-agent");
let path = f.write("retired-agent.md", &content);
f.ledger("retired-agent.md", &content, Origin::Bundled);
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(report.moved.is_empty(), "report: {report:?}");
assert_eq!(report.skipped[0].reason, SkipReason::NotShadowingBundled);
assert!(path.exists());
}
#[test]
fn movable_only_when_all_four_gates_agree() {
let bundled = roster(&["qa"]);
let tm = tm_agent("qa");
let foreign = claude_mpm_agent("qa");
let untracked = TierOwnership::Untracked;
let free = VcsClaim::Unclaimed;
assert_eq!(refusal("qa", &tm, untracked, &bundled, free), None);
for (label, got, want) in [
(
"gate 1 / custom name",
refusal("acme", &tm, untracked, &bundled, free),
SkipReason::NotShadowingBundled,
),
(
"gate 1 / stranded",
refusal("acme", &tm, TierOwnership::FrameworkOwned, &bundled, free),
SkipReason::NotShadowingBundled,
),
(
"gate 2 / user-owned",
refusal("qa", &tm, TierOwnership::UserOwned, &bundled, free),
SkipReason::UserOwned,
),
(
"gate 3 / tracked",
refusal("qa", &tm, untracked, &bundled, VcsClaim::Claimed),
SkipReason::GitTracked,
),
(
"gate 3 / unknown",
refusal("qa", &tm, untracked, &bundled, VcsClaim::Unknown),
SkipReason::VcsUnknown,
),
(
"gate 4 / claude-mpm",
refusal("qa", &foreign, untracked, &bundled, free),
SkipReason::ClaudeMpmSchema,
),
(
"gate 4 / unrecognised",
refusal("qa", "not frontmatter", untracked, &bundled, free),
SkipReason::UnrecognizedSchema,
),
(
"empty roster",
refusal("qa", &tm, untracked, &BTreeSet::new(), free),
SkipReason::NotShadowingBundled,
),
] {
assert_eq!(got, Some(want), "{label}");
}
}
#[test]
fn quarantine_refuses_on_a_corrupt_ledger() {
let f = Fixture::new();
let path = f.write("qa.md", &tm_agent("qa"));
std::fs::write(f.tier.join(MANIFEST_FILE), "{ not json").expect("corrupt the ledger");
let err = f.sweep(&roster(&["qa"])).expect_err("must refuse");
assert!(matches!(err, QuarantineError::CorruptLedger(_)), "{err}");
assert!(path.exists(), "a refusal must touch nothing");
}
#[test]
fn quarantine_refuses_an_empty_roster() {
let f = Fixture::new();
let path = f.write("qa.md", &tm_agent("qa"));
let err = f.sweep(&BTreeSet::new()).expect_err("must refuse");
assert!(matches!(err, QuarantineError::EmptyRoster), "{err}");
assert!(path.exists());
}
#[test]
fn quarantine_missing_dir_is_a_noop() {
let tmp = TempDir::new().expect("tempdir");
let tier = tmp.path().join(".claude").join("agents");
let report =
quarantine_shadowing_agents(&tier, &tmp.path().join("bk"), &roster(&["qa"]), "run-1")
.expect("noop");
assert_eq!(report, QuarantineReport::default());
assert!(!tier.exists(), "the sweep must not create the tier");
}
#[test]
fn quarantine_blocks_while_the_ledger_lock_is_held() {
let f = Fixture::new();
f.write("qa.md", &tm_agent("qa"));
let lock_file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(manifest_lock_path(&f.tier))
.expect("open lock");
let mut lock = fd_lock::RwLock::new(lock_file);
let held = lock.try_write().expect("take the lock");
let (tx, rx) = std::sync::mpsc::channel();
let (tier, backups) = (f.tier.clone(), f.backups.clone());
let handle = std::thread::spawn(move || {
let out = quarantine_shadowing_agents(&tier, &backups, &roster(&["qa"]), "run-1");
tx.send(()).ok();
out
});
assert!(
rx.recv_timeout(std::time::Duration::from_millis(300))
.is_err(),
"the sweep must BLOCK while the ledger lock is held"
);
drop(held);
let report = handle.join().expect("join").expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
}
#[test]
fn a_partial_failure_still_reports_every_candidate() {
let f = Fixture::new();
let moves = f.write("qa.md", &tm_agent("qa"));
let blocked_content = tm_agent("ops");
let blocked = f.write("ops.md", &blocked_content);
let skipped = f.write("research.md", &claude_mpm_agent("research"));
std::fs::write(f.tier.join("ops.md.disabled"), "occupied").expect("occupy");
for n in 1..=MAX_COLLISION_ATTEMPTS {
std::fs::write(f.tier.join(format!("ops.md.disabled.{n}")), "occupied").expect("occupy");
}
let report = f.sweep(&roster(&["qa", "ops", "research"])).expect("sweep");
assert_eq!(report.examined(), 3, "report: {report:?}");
assert_eq!(report.moved.len(), 1);
assert_eq!(report.moved[0].name, "qa");
assert_eq!(report.failed.len(), 1);
assert_eq!(report.failed[0].name, "ops");
assert_eq!(report.failed[0].stage, FailStage::Rename);
assert_eq!(report.skipped.len(), 1);
assert_eq!(report.skipped[0].reason, SkipReason::ClaudeMpmSchema);
assert!(!moves.exists(), "the successful move still happened");
assert!(
blocked.exists(),
"the failed move left the original in place"
);
assert_eq!(
std::fs::read_to_string(&blocked).expect("read"),
blocked_content
);
assert!(skipped.exists());
let receipt_path = report.receipt.as_ref().expect("a receipt must be written");
let receipt = std::fs::read_to_string(receipt_path).expect("read receipt");
assert!(receipt.contains("## Moved (1)"), "{receipt}");
assert!(receipt.contains("## Failed (1)"), "{receipt}");
assert!(receipt.contains("## Skipped (1)"), "{receipt}");
for name in ["qa", "ops", "research"] {
assert!(receipt.contains(name), "receipt omits `{name}`:\n{receipt}");
}
assert!(receipt.contains("Examined: 3"), "{receipt}");
}
#[test]
fn a_failed_backup_leaves_the_original_in_place() {
let f = Fixture::new();
let content = tm_agent("qa");
let path = f.write("qa.md", &content);
std::fs::create_dir_all(f.backups.parent().expect("parent")).expect("mkdir");
std::fs::write(&f.backups, "not a directory").expect("occupy the backup root");
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert_eq!(report.failed.len(), 1, "report: {report:?}");
assert_eq!(report.failed[0].stage, FailStage::Backup);
assert!(report.moved.is_empty());
assert_eq!(std::fs::read_to_string(&path).expect("read"), content);
assert!(!f.tier.join("qa.md.disabled").exists());
assert!(report.receipt.is_none());
assert!(report.receipt_error.is_some(), "report: {report:?}");
}
#[test]
fn a_same_second_rerun_does_not_overwrite_the_first_receipt() {
let f = Fixture::new();
f.write("qa.md", &tm_agent("qa"));
let first = f.sweep(&roster(&["qa"])).expect("first sweep");
let first_receipt = first.receipt.clone().expect("receipt");
f.write("qa.md", &tm_agent("qa"));
let second = f.sweep(&roster(&["qa"])).expect("second sweep");
let second_receipt = second.receipt.clone().expect("receipt");
assert_ne!(first_receipt, second_receipt, "receipts must not collide");
assert!(first_receipt.exists(), "run 1's receipt must survive");
let body = std::fs::read_to_string(&first_receipt).expect("read");
assert!(
body.contains("## Moved (1)"),
"run 1's record intact:\n{body}"
);
}
#[test]
fn a_clean_tier_writes_no_receipt() {
let f = Fixture::new();
f.write("acme.md", &tm_agent("acme-custom"));
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert!(!report.wrote_anything());
assert!(report.receipt.is_none());
assert!(
!f.backups.exists(),
"a clean project must gain no quarantine directory"
);
}
#[test]
#[cfg(unix)]
fn restore_command_survives_a_hostile_filename() {
let f = Fixture::new();
let hostile = "it's $(touch PWNED-SUB) `touch PWNED-TICK`.md";
f.write(hostile, &tm_agent("qa"));
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
let moved = &report.moved[0];
let command = format!("mv {} {}", shell_quote(&moved.to), shell_quote(&moved.from));
let out = std::process::Command::new("sh")
.arg("-c")
.arg(&command)
.current_dir(&f.tier)
.output()
.expect("run the printed restore command");
assert!(
out.status.success(),
"restore failed: {}",
String::from_utf8_lossy(&out.stderr)
);
assert!(moved.from.exists(), "the file must be back where it was");
assert!(
!f.tier.join("PWNED-SUB").exists(),
"command substitution ran"
);
assert!(!f.tier.join("PWNED-TICK").exists(), "a backtick ran");
let receipt = std::fs::read_to_string(report.receipt.expect("receipt")).expect("read");
assert!(receipt.contains(&command), "receipt:\n{receipt}");
}
#[test]
fn a_second_run_does_not_overwrite_the_first_backup() {
let f = Fixture::new();
f.write("qa.md", &tm_agent("qa"));
let first = f.sweep(&roster(&["qa"])).expect("first sweep");
assert_eq!(first.moved.len(), 1);
f.write(
"qa.md",
"---\nname: qa\nrole: qa\n---\n\n\
# BASE-AGENT — Foundation for all trusty-mpm agents\n\nSecond copy.\n",
);
let second = f.sweep(&roster(&["qa"])).expect("second sweep");
assert_eq!(second.moved.len(), 1, "report: {second:?}");
assert_ne!(second.moved[0].backup, first.moved[0].backup);
assert_ne!(second.moved[0].to, first.moved[0].to);
assert!(first.moved[0].backup.exists(), "run 1's backup survives");
assert!(first.moved[0].to.exists(), "run 1's inert sibling survives");
assert!(
std::fs::read_to_string(&first.moved[0].backup)
.expect("read")
.contains("Composed by tm"),
"run 1's backup content must be unchanged"
);
}
#[test]
#[cfg(unix)]
fn a_dangling_symlink_is_not_free() {
let tmp = TempDir::new().expect("tempdir");
let link = tmp.path().join("qa.md.disabled");
std::os::unix::fs::symlink(tmp.path().join("nowhere"), &link).expect("symlink");
assert!(std::fs::metadata(&link).is_err(), "the link dangles");
let chosen = free_path(&link).expect("a free name");
assert_ne!(chosen, link, "a dangling symlink must count as occupied");
}
#[test]
#[cfg(unix)]
fn a_symlink_is_never_a_candidate() {
let f = Fixture::new();
let real = f.project.join("elsewhere.md");
std::fs::write(&real, tm_agent("qa")).expect("write");
std::os::unix::fs::symlink(&real, f.tier.join("qa.md")).expect("symlink");
let report = f.sweep(&roster(&["qa"])).expect("sweep");
assert_eq!(report.examined(), 0, "report: {report:?}");
assert!(real.exists(), "the symlink target must be untouched");
}
#[test]
fn never_deletes_on_any_path() {
for (label, source) in [
("quarantine.rs", include_str!("quarantine.rs")),
(
"quarantine_receipt.rs",
include_str!("quarantine_receipt.rs"),
),
("vcs_claim.rs", include_str!("vcs_claim.rs")),
("agent_schema.rs", include_str!("agent_schema.rs")),
] {
for needle in [
concat!("remove_", "file"),
concat!("remove_", "dir"),
concat!("remove_", "dir_all"),
concat!("set_", "len"),
concat!("File::", "create"),
] {
let calls: Vec<&str> = source
.lines()
.filter(|line| {
let t = line.trim_start();
!t.starts_with("//") && !t.starts_with("///") && line.contains(needle)
})
.collect();
assert!(
calls.is_empty(),
"{label} must never call `{needle}` — quarantine moves, it does not destroy. \
Offending lines: {calls:?}"
);
}
}
}
#[test]
fn a_sweep_destroys_no_content() {
let f = Fixture::new();
let staged: Vec<(&str, String)> = vec![
("qa.md", tm_agent("qa")),
("ops.md", claude_mpm_agent("ops")),
("acme.md", tm_agent("acme-custom")),
];
for (file_name, content) in &staged {
f.write(file_name, content);
}
let report = f.sweep(&roster(&["qa", "ops"])).expect("sweep");
assert_eq!(report.moved.len(), 1, "report: {report:?}");
for (_, content) in &staged {
let found = walk(&f.project).into_iter().any(|p| {
std::fs::read_to_string(&p)
.map(|c| &c == content)
.unwrap_or(false)
});
assert!(found, "content vanished from the project:\n{content}");
}
}
fn walk(root: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
let Ok(entries) = std::fs::read_dir(root) else {
return out;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
out.extend(walk(&path));
} else {
out.push(path);
}
}
out
}