use std::path::{Path, PathBuf};
use crate::adapters::adapter;
use crate::apply::exec::{ApplyReport, ApplyRequest, apply_in, swap_file};
use crate::apply::ledger::{EntryKind, Ledger};
use crate::apply::plan::{PlanOptions, read_target};
use crate::apply::{ApplyEnv, backup};
use crate::error::{CoreError, ErrorCode};
use crate::formats::Document;
use crate::model::TomlDocument;
use crate::model::applied::ApplyTrigger;
use crate::model::machine::MachineOverlay;
use crate::secrets::catalog::overlay_path;
use crate::store::diff::trailer_entities;
use crate::store::merge::Files;
use crate::store::{
CommitId, LOCK_TIMEOUT, Machine, Operation, Store, StoreLock, StoreRead, entity_paths,
};
const SHARED_MANIFEST: &str = "project.toml";
const LOCAL_MANIFEST: &str = "project.local.toml";
#[derive(Debug, Clone)]
pub struct RestoreRequest {
pub target: CommitId,
pub entities: Vec<String>,
pub confirmed: bool,
pub apply: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ManifestWrite {
pub project: String,
pub relative: String,
pub path: PathBuf,
pub shared: bool,
}
#[derive(Debug, Clone)]
pub struct RestorePlan {
pub target: CommitId,
pub head: CommitId,
pub files: Vec<String>,
pub manifests: Vec<ManifestWrite>,
pub unplaced: Vec<String>,
}
impl RestorePlan {
pub fn needs_confirmation(&self) -> bool {
self.manifests.iter().any(|manifest| manifest.shared)
}
}
#[derive(Debug)]
pub struct RestoreReport {
pub plan: RestorePlan,
pub tag: Option<String>,
pub commit: Option<CommitId>,
pub written: Vec<PathBuf>,
pub apply: Option<ApplyReport>,
}
fn restorable(file: &str, machine_id: &str) -> bool {
match file.strip_prefix("machines/") {
Some(name) => name == format!("{machine_id}.toml"),
None => true,
}
}
pub fn prepare(
store: &Store<'_>,
machine_id: &str,
request: &RestoreRequest,
) -> Result<RestorePlan, CoreError> {
let mut selected: Vec<String> = Vec::new();
for entity in &request.entities {
let paths = entity_paths(entity);
if paths.is_empty() {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!(
"unknown entity {entity:?}; expected server/<id>, skill/<id>, project/<id>, \
machine/<id>, secrets, clients, settings or sources"
),
));
}
if entity
.strip_prefix("machine/")
.is_some_and(|id| id != machine_id)
{
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("{entity} belongs to another machine; only that machine writes its files"),
));
}
selected.extend(paths);
}
let head = store.head()?;
let files: Vec<String> = store
.changed_files(&head, &request.target)?
.into_iter()
.filter(|file| restorable(file, machine_id))
.filter(|file| {
selected.is_empty()
|| selected
.iter()
.any(|path| file == path || file.starts_with(&format!("{path}/")))
})
.collect();
let target = store.revision(&request.target)?;
let overlay_file = overlay_path(machine_id)?;
let overlay_text = if files.contains(&overlay_file) {
target.read_file(&overlay_file)?
} else {
store.read_file(&overlay_file)?
};
let overlay = overlay_text
.and_then(|text| TomlDocument::<MachineOverlay>::parse(&text).ok())
.map(TomlDocument::into_value);
let mut manifests = Vec::new();
let mut unplaced = Vec::new();
for file in &files {
let Some((project, name)) = manifest_parts(file) else {
continue;
};
let Some(bytes) = target.read_bytes(file)? else {
continue;
};
let folder = overlay
.as_ref()
.and_then(|overlay| overlay.projects.as_ref())
.and_then(|projects| projects.get(project))
.map(|project| PathBuf::from(&project.path))
.filter(|folder| folder.is_absolute() && store.fs.is_dir(folder));
let Some(folder) = folder else {
unplaced.push(file.clone());
continue;
};
let path = crate::layering::manifest::manifest_dir(store.fs, &folder).join(name);
if read_target(store.fs, &path)?.as_deref() == Some(bytes.as_slice()) {
continue;
}
manifests.push(ManifestWrite {
project: project.to_owned(),
relative: file.clone(),
path,
shared: name == SHARED_MANIFEST,
});
}
Ok(RestorePlan {
target: request.target.clone(),
head,
files,
manifests,
unplaced,
})
}
fn manifest_parts(file: &str) -> Option<(&str, &str)> {
let rest = file.strip_prefix("projects/")?;
let (id, name) = rest.split_once('/')?;
matches!(name, SHARED_MANIFEST | LOCAL_MANIFEST).then_some((id, name))
}
pub fn restore(
store: &Store<'_>,
ax: &ApplyEnv<'_>,
request: &RestoreRequest,
) -> Result<RestoreReport, CoreError> {
let summary = format!("restore: {}", request.target.short());
restore_as(store, ax, request, "store.restore", summary, None)
}
pub(crate) fn restore_as(
store: &Store<'_>,
ax: &ApplyEnv<'_>,
request: &RestoreRequest,
op: &str,
summary: String,
expected_head: Option<&CommitId>,
) -> Result<RestoreReport, CoreError> {
let machine = Machine::load_or_create(ax.env, ax.fs, &ax.home.state_dir())?;
let plan = prepare(store, &machine.id, request)?;
if expected_head.is_some_and(|head| *head != plan.head) {
return Err(CoreError::new(
ErrorCode::FileChangedDuringApply,
"the store changed since the pending changes were read; review them again",
));
}
if plan.needs_confirmation() && !request.confirmed {
let files: Vec<String> = plan
.manifests
.iter()
.filter(|manifest| manifest.shared)
.map(|manifest| manifest.path.display().to_string())
.collect();
return Err(CoreError::new(
ErrorCode::ConfirmationRequired,
format!(
"the restore writes shared project manifests a team may use ({}); confirm to \
write them back",
files.join(", ")
),
));
}
if plan.files.is_empty() {
return Ok(RestoreReport {
plan,
tag: None,
commit: None,
written: Vec::new(),
apply: None,
});
}
let mut operation = Operation::new(op, summary);
for entity in trailer_entities(&plan.files) {
operation = operation.entity(entity);
}
let committed = store.transaction(operation, |tx| {
if tx.store().head()? != plan.head {
return Err(CoreError::new(
ErrorCode::FileChangedDuringApply,
"the store changed while the restore was being prepared; run it again",
));
}
let source = tx.store().revision(&plan.target)?;
let mut contents = Vec::with_capacity(plan.files.len());
for file in &plan.files {
contents.push((file.clone(), source.read_bytes(file)?));
}
drop(source);
for (file, content) in contents {
match content {
Some(bytes) => tx.write(&file, bytes)?,
None => tx.remove(&file)?,
}
}
if request.apply {
let request = ApplyRequest {
options: PlanOptions::default(),
trigger: ApplyTrigger::Restore,
};
apply_in(tx, ax, &request).map(Some)
} else {
Ok(None)
}
})?;
let tag = store.create_auto_tag_at("pre-restore", &plan.head)?;
let target = store.revision(&plan.target)?;
let mut written = Vec::new();
for manifest in &plan.manifests {
let Some(bytes) = target.read_bytes(&manifest.relative)? else {
continue;
};
crate::apply::write_atomic(ax.fs, &manifest.path, &bytes)?;
written.push(manifest.path.clone());
}
Ok(RestoreReport {
plan,
tag: Some(tag),
commit: committed.commit,
written,
apply: committed.value,
})
}
pub fn restore_tree(
store: &Store<'_>,
ax: &ApplyEnv<'_>,
files: &Files,
label: &str,
apply: bool,
) -> Result<RestoreReport, CoreError> {
let machine = Machine::load_or_create(ax.env, ax.fs, &ax.home.state_dir())?;
let head = store.head()?;
let current = store.files_at(&head)?;
let changed: Vec<String> = current
.keys()
.chain(files.keys())
.filter(|file| restorable(file, &machine.id))
.filter(|file| current.get(*file) != files.get(*file))
.cloned()
.collect::<std::collections::BTreeSet<_>>()
.into_iter()
.collect();
let plan = RestorePlan {
target: head.clone(),
head: head.clone(),
files: changed,
manifests: Vec::new(),
unplaced: Vec::new(),
};
if plan.files.is_empty() {
return Ok(RestoreReport {
plan,
tag: None,
commit: None,
written: Vec::new(),
apply: None,
});
}
let mut operation = Operation::new("store.restore", format!("restore: from {label}"));
for entity in trailer_entities(&plan.files) {
operation = operation.entity(entity);
}
let committed = store.transaction(operation, |tx| {
if tx.store().head()? != plan.head {
return Err(CoreError::new(
ErrorCode::FileChangedDuringApply,
"the store changed while the restore was being prepared; run it again",
));
}
for file in &plan.files {
match files.get(file) {
Some(bytes) => tx.write(file, bytes.clone())?,
None => tx.remove(file)?,
}
}
if apply {
let request = ApplyRequest {
options: PlanOptions::default(),
trigger: ApplyTrigger::Restore,
};
apply_in(tx, ax, &request).map(Some)
} else {
Ok(None)
}
})?;
let tag = store.create_auto_tag_at("pre-restore", &plan.head)?;
Ok(RestoreReport {
plan,
tag: Some(tag),
commit: committed.commit,
written: Vec::new(),
apply: committed.value,
})
}
#[derive(Debug)]
pub enum FileOutcome {
Restored { previous: Option<PathBuf> },
Unchanged,
Failed(CoreError),
Removed,
}
#[derive(Debug)]
pub struct FileRestore {
pub file: PathBuf,
pub outcome: FileOutcome,
}
pub fn restore_files(
ax: &ApplyEnv<'_>,
run: &str,
only: Option<&Path>,
) -> Result<Vec<FileRestore>, CoreError> {
let fs = ax.fs;
let state_dir = ax.home.state_dir();
let _lock = StoreLock::acquire(fs, &state_dir, LOCK_TIMEOUT)?;
let apply_id = run;
let removed: Vec<PathBuf> = backup::removed_backups(fs, &state_dir, run_apply_id(run))
.unwrap_or_default()
.into_iter()
.filter(|file| only.is_none_or(|only| same_path(ax, file, only)))
.collect();
let run = match find_run(ax, run) {
Ok(run) => Some(run),
Err(err) if err.code() == ErrorCode::NotFound && !removed.is_empty() => None,
Err(err) => return Err(err),
};
let entries = match &run {
Some(run) => fs.read_dir(run).map_err(|err| {
CoreError::new(ErrorCode::IoError, format!("cannot list backups: {err}")).with_path(run)
})?,
None => Vec::new(),
};
let mut files: Vec<(PathBuf, PathBuf)> = entries
.into_iter()
.filter_map(|backup| {
let name = backup.file_name()?.to_str()?.to_owned();
let original = PathBuf::from(backup::decode_path(&name)?);
Some((original, backup))
})
.filter(|(original, _)| only.is_none_or(|only| same_path(ax, original, only)))
.filter(|(original, _)| !removed.iter().any(|file| same_path(ax, original, file)))
.collect();
files.sort();
if files.is_empty() && removed.is_empty() {
return Err(CoreError::new(
ErrorCode::NotFound,
match only {
Some(only) => format!("apply {apply_id} has no backup of {}", only.display()),
None => format!("apply {apply_id} has no file backups"),
},
));
}
let mut results: Vec<FileRestore> = removed
.into_iter()
.map(|file| FileRestore {
file,
outcome: FileOutcome::Removed,
})
.collect();
let Some(run) = run else {
return Ok(results);
};
let run_id = run
.file_name()
.and_then(|name| name.to_str())
.and_then(|name| name.split_once('-'))
.map_or(apply_id, |(_, id)| id);
let base = run_id
.strip_prefix("restore-")
.and_then(|rest| rest.split('-').next())
.unwrap_or(run_id);
let undo_id = format!(
"restore-{base}-{}",
crate::model::ids::new_ulid(ax.env.now())?
);
let undo_dir = backup::run_dir(ax.home, ax.env.now(), &undo_id);
let mut ledger = Ledger::load(fs, &state_dir)?;
for (file, saved) in files {
let outcome = restore_one(ax, &undo_dir, &file, &saved).and_then(|outcome| {
if matches!(outcome, FileOutcome::Restored { .. }) {
refresh_ledger(ax, &mut ledger, &file)?;
ledger.save(fs, &state_dir)?;
}
Ok(outcome)
});
let outcome = outcome.unwrap_or_else(|err| FileOutcome::Failed(err.with_path(&file)));
results.push(FileRestore { file, outcome });
}
Ok(results)
}
fn run_apply_id(run: &str) -> &str {
match run.get(..16).and_then(backup::parse_timestamp) {
Some(_) if run.as_bytes().get(16) == Some(&b'-') => &run[17..],
_ => run,
}
}
fn find_run(ax: &ApplyEnv<'_>, run: &str) -> Result<PathBuf, CoreError> {
let root = ax.home.backups_dir().join(backup::FILES_DIR);
let runs = if ax.fs.is_dir(&root) {
ax.fs.read_dir(&root).map_err(|err| {
CoreError::new(ErrorCode::IoError, format!("cannot list backups: {err}"))
.with_path(&root)
})?
} else {
Vec::new()
};
let runs: Vec<(String, PathBuf)> = runs
.into_iter()
.filter(|dir| ax.fs.is_dir(dir))
.filter_map(|dir| Some((dir.file_name()?.to_str()?.to_owned(), dir)))
.collect();
if let Some((_, dir)) = runs.iter().find(|(name, _)| name == run) {
return Ok(dir.clone());
}
runs.into_iter()
.filter(|(name, _)| {
name.split_once('-')
.is_some_and(|(_, id)| id.eq_ignore_ascii_case(run))
})
.max_by(|a, b| a.0.cmp(&b.0))
.map(|(_, dir)| dir)
.ok_or_else(|| {
CoreError::new(
ErrorCode::NotFound,
format!("no file backups of {run:?} on this machine"),
)
})
}
fn same_path(ax: &ApplyEnv<'_>, a: &Path, b: &Path) -> bool {
match ax.env.os() {
crate::env::Os::Windows => a
.to_string_lossy()
.eq_ignore_ascii_case(&b.to_string_lossy()),
_ => a == b,
}
}
fn restore_one(
ax: &ApplyEnv<'_>,
undo_dir: &Path,
file: &Path,
saved: &Path,
) -> Result<FileOutcome, CoreError> {
let fs = ax.fs;
let bytes = fs.read(saved).map_err(|err| {
CoreError::new(ErrorCode::IoError, format!("cannot read backup: {err}")).with_path(saved)
})?;
let current = read_target(fs, file)?;
if current.as_deref() == Some(bytes.as_slice()) {
return Ok(FileOutcome::Unchanged);
}
let previous = match ¤t {
Some(current) => Some(backup::save(fs, ax.home, undo_dir, file, current)?),
None => None,
};
if !swap_file(fs, file, current.as_deref(), &bytes)? {
return Err(CoreError::new(
ErrorCode::FileChangedDuringApply,
"the file changed while it was being restored; nothing was written",
));
}
Ok(FileOutcome::Restored { previous })
}
fn refresh_ledger(ax: &ApplyEnv<'_>, ledger: &mut Ledger, file: &Path) -> Result<(), CoreError> {
let name = file.to_string_lossy().into_owned();
let bytes = read_target(ax.fs, file)?;
let owned: Vec<_> = ledger
.entries
.iter()
.filter(|entry| entry.file == name && entry.kind == EntryKind::McpEntry)
.cloned()
.collect();
for entry in owned {
let present = match &bytes {
None => false,
Some(bytes) => {
let Some(format) = adapter(&entry.client)
.and_then(|spec| spec.mcp.as_ref())
.map(|mcp| mcp.format)
else {
continue;
};
let Ok(doc) = Document::parse(bytes, format) else {
continue;
};
let keys = pointer_keys(&entry.container);
let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
!matches!(doc.get(&keys, &entry.key), Ok(None))
}
};
if !present {
ledger.remove(&entry.slot(), &entry.key);
}
}
Ok(())
}
fn pointer_keys(pointer: &str) -> Vec<String> {
pointer
.split('/')
.skip(1)
.map(|key| key.replace("~1", "/").replace("~0", "~"))
.collect()
}