use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SkipReason {
NotShadowingBundled,
UserOwned,
GitTracked,
VcsUnknown,
ClaudeMpmSchema,
UnrecognizedSchema,
}
impl SkipReason {
pub fn explain(self) -> &'static str {
match self {
Self::NotShadowingBundled => "does not shadow a bundled agent name",
Self::UserOwned => "the ownership ledger records this as the operator's",
Self::GitTracked => "git tracks this file — the project is claiming it",
Self::VcsUnknown => "git could not be consulted; a claim cannot be ruled out",
Self::ClaudeMpmSchema => "claude-mpm's frontmatter schema — another project's file",
Self::UnrecognizedSchema => "frontmatter matches no known deploy shape",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FailStage {
Backup,
Verify,
Rename,
}
impl FailStage {
pub fn explain(self) -> &'static str {
match self {
Self::Backup => "backup copy failed; the original was NOT moved",
Self::Verify => {
"the backup did not read back byte-identical; the original was NOT moved"
}
Self::Rename => {
"the rename failed; the verified backup survives and the original is untouched"
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuarantinedAgent {
pub name: String,
pub from: PathBuf,
pub to: PathBuf,
pub backup: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkippedAgent {
pub name: String,
pub path: PathBuf,
pub reason: SkipReason,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FailedAgent {
pub name: String,
pub path: PathBuf,
pub stage: FailStage,
pub detail: String,
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct QuarantineReport {
pub moved: Vec<QuarantinedAgent>,
pub skipped: Vec<SkippedAgent>,
pub failed: Vec<FailedAgent>,
pub receipt: Option<PathBuf>,
pub receipt_error: Option<String>,
}
impl QuarantineReport {
pub fn wrote_anything(&self) -> bool {
!self.moved.is_empty() || !self.failed.is_empty()
}
pub fn examined(&self) -> usize {
self.moved.len() + self.skipped.len() + self.failed.len()
}
}
pub fn shell_quote(path: &Path) -> String {
format!("'{}'", path.display().to_string().replace('\'', r"'\''"))
}
pub fn render_receipt(report: &QuarantineReport, tier_dir: &Path, run_id: &str) -> String {
let mut out = format!(
"# trusty-mpm agent quarantine — {run_id}\n\n\
Tier: {}\n\
Examined: {}\n\n\
Nothing here was deleted. Every moved file has a byte-identical backup\n\
and an inert `.md.disabled` sibling; either one restores it.\n\n",
tier_dir.display(),
report.examined()
);
out.push_str(&format!("## Moved ({})\n\n", report.moved.len()));
for m in &report.moved {
out.push_str(&format!(
"- `{}` — shadowed a bundled agent name\n - was: {}\n - now: {}\n - backup: {}\n - restore: mv {} {}\n",
m.name,
m.from.display(),
m.to.display(),
m.backup.display(),
shell_quote(&m.to),
shell_quote(&m.from),
));
}
out.push_str(&format!("\n## Failed ({})\n\n", report.failed.len()));
for f in &report.failed {
out.push_str(&format!(
"- `{}` — {}\n - path: {}\n - {}\n - detail: {}\n",
f.name,
match f.stage {
FailStage::Backup => "backup",
FailStage::Verify => "verify",
FailStage::Rename => "rename",
},
f.path.display(),
f.stage.explain(),
f.detail,
));
}
out.push_str(&format!("\n## Skipped ({})\n\n", report.skipped.len()));
for s in &report.skipped {
out.push_str(&format!(
"- `{}` — {}\n - path: {}\n",
s.name,
s.reason.explain(),
s.path.display(),
));
}
out
}