use anyhow::{anyhow, bail, Result};
use colored::Colorize;
use crate::followups;
use crate::utils;
pub fn run(path: &str) -> Result<()> {
let resolved = utils::resolve_project_root(path)
.ok_or_else(|| anyhow!("StrayMark not installed. Run 'straymark init' first."))?;
let project_root = &resolved.path;
let registry_path = followups::registry_path(project_root);
if !registry_path.exists() {
bail!(
"No follow-ups registry at {}.\n hint: run `straymark followups drift --scan-all --apply` to create and seed it (see STRAYMARK.md §16).",
registry_path.display()
);
}
let registry = followups::parse_registry(®istry_path)?;
for w in ®istry.warnings {
utils::warn(w);
}
let counters = followups::compute_counters(®istry);
let fm = followups::fm_apply_counters_and_v1(®istry.frontmatter_raw, &counters);
if fm == registry.frontmatter_raw {
println!(
"{} counters already in sync: {} open / {} suspected-closed / {} promoted (total {}).",
"OK".green().bold(),
counters.open,
counters.suspected_closed,
counters.promoted,
counters.total
);
return Ok(());
}
let was_v0 = registry.is_v0();
std::fs::write(
®istry_path,
followups::assemble(&fm, ®istry.body),
)?;
utils::success(&format!(
"Counters recomputed: {} open / {} suspected-closed / {} promoted (total {}).",
counters.open, counters.suspected_closed, counters.promoted, counters.total
));
if was_v0 {
utils::info("Registry upgraded to schema v1 (non-destructive — counters are now CLI-owned).");
}
Ok(())
}