use anyhow::Result;
use colored::Colorize;
use crate::cli::ConsolidateArgs;
use crate::store::{self, Store};
pub fn run(store: &Store, args: &ConsolidateArgs) -> Result<()> {
let branch = args
.branch
.as_deref()
.ok_or_else(|| anyhow::anyhow!("--branch is required"))?;
let branch_traces = store.load_branch_traces(branch)?;
if branch_traces.is_empty() {
println!("No traces found for branch '{branch}'");
return Ok(());
}
let mut meta_traces = store.load_meta_traces()?;
let existing_ids: std::collections::HashSet<String> =
meta_traces.iter().map(|t| t.id.clone()).collect();
let new_traces: Vec<_> = branch_traces
.into_iter()
.filter(|t| !existing_ids.contains(&t.id))
.collect();
let new_count = new_traces.len();
meta_traces.extend(new_traces);
let jsonl = store::traces_to_jsonl(&meta_traces)?;
store::write_to_ref(
&store.repo_root,
"refs/agentdiff/meta",
"traces.jsonl",
&jsonl,
&format!("agentdiff: consolidate {branch} ({new_count} traces)"),
)?;
let ref_name = store::branch_ref_name(branch);
if let Err(e) = store::delete_ref(&store.repo_root, &ref_name) {
eprintln!("{} could not delete local ref: {e}", "warn".yellow());
}
if args.push {
if let Err(e) = store::push_content_to_ref(
&store.repo_root,
"refs/agentdiff/meta",
"traces.jsonl",
&jsonl,
&format!("agentdiff: consolidate {branch} ({new_count} traces)"),
) {
let msg = e.to_string();
if !msg.contains("not a GitHub URL") {
eprintln!("{} could not push meta ref to GitHub: {e}", "warn".yellow());
}
} else if let Err(e) = store::delete_remote_ref(&store.repo_root, &ref_name) {
eprintln!("{} could not delete remote ref: {e}", "warn".yellow());
}
}
println!(
"{} Consolidated {} trace(s) from '{}' into agentdiff-meta ({} total)",
"ok".green(),
new_count,
branch,
meta_traces.len()
);
Ok(())
}