Skip to main content

workyard_core/model/
recovery.rs

1use crate::{Repository, Task};
2use std::{collections::BTreeMap, path::PathBuf};
3
4/// Task reconstructed from the saved document and independent evidence.
5#[derive(Clone, Debug)]
6pub struct RecoveryRecord {
7	/// Last published task document, distinct from independent effect evidence.
8	pub recorded: Option<Task>,
9	/// Existing formats may need a supported migration even when Git is unchanged.
10	pub needs_publication: bool,
11	/// Task selected for this operation.
12	pub task: Task,
13	/// Participant index associated with unresolved effect evidence.
14	pub pending: Option<usize>,
15	/// Current locations of admission roots whose recorded workspace moved.
16	pub relocated_project_roots: BTreeMap<PathBuf, PathBuf>,
17}
18
19/// Facts obtained from Git before resuming a participant.
20#[derive(Clone, Debug)]
21pub struct RecoveryObservation {
22	/// Verified backing repository for this participant.
23	pub repository: Repository,
24	/// Location of the participant worktree.
25	pub destination: Option<PathBuf>,
26	/// Whether an unfinished repository operation was observed.
27	pub operation_in_progress: bool,
28}
29
30/// The invocation result is separate from preserved task operation history.
31pub struct RecoveryResult {
32	/// Newly established project roots eligible for catalog registration.
33	pub admitted_roots: Vec<PathBuf>,
34	/// Invocation history tracked separately from task lifecycle.
35	pub operation: crate::Operation,
36	/// Diagnostic describing a failure or an unresolved condition.
37	pub problem: Option<crate::Problem>,
38	/// Task selected for this operation.
39	pub task: Task,
40	/// Location of the task record when available.
41	pub record: PathBuf,
42	/// Whether inspection found no outstanding work to resume.
43	pub nothing_to_recover: bool,
44}
45
46impl RecoveryRecord {
47	/// New revisions can publish current registration paths. Creation evidence
48	/// keeps its original selections for comparison against the immutable admission.
49	pub(crate) fn into_amendment_task(mut self) -> Task {
50		for participant in &mut self.task.participants {
51			locate_roots(
52				&mut participant.registration_roots,
53				&self.relocated_project_roots,
54			);
55		}
56		self.task
57	}
58
59	pub(crate) fn newly_admitted_roots(&self, task: &Task) -> Vec<PathBuf> {
60		let mut roots = task.admitted_roots(Some(&self.task));
61		locate_roots(&mut roots, &self.relocated_project_roots);
62		roots
63	}
64
65	pub(crate) fn writer_problem(&self, index: usize) -> crate::Problem {
66		let participant = &self.task.participants[index];
67		let operation = match self.task.operation.continues.or(self.task.operation.kind) {
68			Some(crate::OperationKind::Remove) => "the removal operation",
69			_ => match participant.phase {
70				crate::Phase::Creating => "worktree creation",
71				crate::Phase::Fetching => "the fetch",
72				_ => "the task operation",
73			},
74		};
75		crate::Problem::unconfirmed_git_exit(format_args!(
76			"{operation} for participant {:?} at {:?}",
77			participant.name, participant.destination
78		))
79	}
80
81	/// Effect snapshots may retain older operation provenance after final publication.
82	/// Compare reconciled resource facts, not that stale invocation outcome.
83	pub(crate) fn matches_published(&self, task: &Task) -> bool {
84		!self.needs_publication
85			&& self.recorded.as_ref().is_some_and(|recorded| {
86				recorded.problem.is_none()
87					&& recorded.operation.outcome == crate::OperationOutcome::Succeeded
88					&& recorded.participants == task.participants
89					&& recorded.lifecycle == task.lifecycle
90					&& recorded.removal == task.removal
91			})
92	}
93}
94
95fn locate_roots(roots: &mut [PathBuf], locations: &BTreeMap<PathBuf, PathBuf>) {
96	for root in roots {
97		if let Some(located) = locations.get(root) {
98			root.clone_from(located);
99		}
100	}
101}