use agent_loadout_core::apply::import;
use agent_loadout_core::apply::ledger::Ledger;
use agent_loadout_core::apply::render::DesiredState;
use agent_loadout_core::apply::{
self, Action, ApplyReport, ApplyRequest, Decision, OnConflict, PlanOptions,
};
use agent_loadout_core::model::applied::ApplyTrigger;
use agent_loadout_core::store::Operation;
use agent_loadout_core::{CoreError, ErrorCode};
use clap::Args;
use serde_json::json;
use super::{Context, JSON_SCHEMA, apply_exit_code, print_json, print_report, report_json};
#[derive(Debug, Args)]
pub struct AdoptArgs {
#[arg(value_name = "CLIENT/NAME", required_unless_present = "all_identical")]
entries: Vec<String>,
#[arg(long)]
all_identical: bool,
#[arg(long)]
theirs: bool,
}
pub fn run(ctx: &Context, args: &AdoptArgs) -> Result<u8, CoreError> {
let resolution = if args.theirs {
OnConflict::Import
} else {
OnConflict::Overwrite
};
let mut decisions = Vec::new();
for entry in &args.entries {
let Some((client, name)) = entry
.split_once('/')
.filter(|(c, n)| !c.is_empty() && !n.is_empty())
else {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("{entry:?} is not CLIENT/NAME"),
));
};
decisions.push(Decision {
client: client.to_owned(),
name: name.to_owned(),
resolution,
});
}
let resumed = agent_loadout_core::eject::resume(ctx.fs(), &ctx.home)?;
let undo = |err: CoreError| {
let _ = agent_loadout_core::eject::unresume(ctx.fs(), &ctx.home, &resumed);
err
};
let store = ctx.open_store_for_change().map_err(undo)?;
let ax = ctx.apply_env();
let committed = store
.transaction(Operation::new("entry.adopt", "adopt"), |tx| {
let mut adopted = Vec::new();
if args.all_identical {
let desired = DesiredState::load(tx, Some(&tx.machine().id.clone()))?;
let ledger = Ledger::load(ax.fs, &ctx.home.state_dir())?;
let (candidates, _) = import::scan(&ax, &desired, &ledger, None)?;
adopted = import::stage_adopt_identical(tx, &ax, &desired, &candidates)?;
}
let request = ApplyRequest {
options: PlanOptions {
clients: None,
on_conflict: OnConflict::Ask,
decisions: decisions.clone(),
..PlanOptions::default()
},
trigger: ApplyTrigger::Review,
};
let report = apply::apply_in(tx, &ax, &request)?;
let mut named: Vec<String> = adopted.clone();
named.extend(args.entries.iter().cloned());
let drift = if args.theirs { " drift" } else { "" };
tx.set_summary(format!("adopt: {}{drift}", named.join(", ")));
Ok((adopted, report))
})
.map_err(undo)?;
let (adopted, report) = committed.value;
let commit = committed.commit;
let untouched = untouched(&report, &decisions);
if ctx.json {
print_json(&json!({
"schema": JSON_SCHEMA,
"identical": adopted,
"not_adopted": untouched,
"apply": report_json(&report, commit.as_ref()),
}));
} else {
for entry in &adopted {
println!("adopted {entry} (identical)");
}
for entry in &untouched {
eprintln!("warning: {entry} needs no decision or does not exist; nothing adopted");
}
print_report(&report, commit.as_ref());
}
Ok(apply_exit_code(&report))
}
fn untouched(report: &ApplyReport, decisions: &[Decision]) -> Vec<String> {
decisions
.iter()
.filter(|decision| {
let acted = report.targets.iter().flat_map(|t| &t.entries).any(|entry| {
entry.client == Some(decision.client.as_str())
&& entry.name == decision.name
&& (!matches!(entry.action, Action::None | Action::Skip)
|| entry
.server_id
.as_ref()
.is_some_and(|id| report.imported.contains(id)))
});
!acted
})
.map(|decision| format!("{}/{}", decision.client, decision.name))
.collect()
}