Skip to main content

workyard_core/model/
task.rs

1use crate::Problem;
2use std::path::PathBuf;
3
4/// Progress of participant creation, distinct from task lifecycle.
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub enum Phase {
7	/// No participant work has begun.
8	NotStarted,
9	/// Git is refreshing the selected repository's refs.
10	Fetching,
11	/// Worktree creation has begun.
12	Creating,
13	/// Git has created the participant worktree.
14	WorktreeCreated,
15	/// Setup for this participant has settled.
16	Completed,
17}
18
19impl Phase {
20	/// Return the stable spelling used in records or output.
21	pub fn as_str(&self) -> &'static str {
22		match self {
23			Self::NotStarted => "not_started",
24			Self::Fetching => "fetching",
25			Self::Creating => "creating",
26			Self::WorktreeCreated => "worktree_created",
27			Self::Completed => "completed",
28		}
29	}
30}
31
32/// Last known relationship paths used as starting points for verification.
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct PlacementHint {
35	/// Path to the backing repository.
36	pub source: PathBuf,
37	/// Git administration directory shared by the repository's worktrees.
38	pub common_dir: PathBuf,
39	/// Location of the participant worktree.
40	pub destination: PathBuf,
41	/// Workspace root containing the Workyard marker.
42	pub workspace: PathBuf,
43	/// Native filesystem identity recorded for the worktree entry.
44	pub worktree_entry: Option<(u64, u64)>,
45}
46
47/// A repository's membership, branch choices and linked worktree in a task.
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct Participant {
50	/// Previous verified backing and worktree paths, used to check manually moved links.
51	pub placement: Option<PlacementHint>,
52	/// Project discovery roots associated with this participant.
53	pub registration_roots: Vec<PathBuf>,
54	/// Branch and fetch policy recorded for this participant.
55	pub creation: Option<super::creation::CreationOptions>,
56	/// Directory name of this participant inside the task.
57	pub name: String,
58	/// Path to the backing repository.
59	pub source: PathBuf,
60	/// Git administration directory shared by the repository's worktrees.
61	pub common_dir: PathBuf,
62	/// Identity used to distinguish backing repositories across path changes.
63	pub backing_identity: String,
64	/// Identity used to verify the linked worktree before applying changes.
65	pub worktree_identity: Option<String>,
66	/// Location of the participant worktree.
67	pub destination: PathBuf,
68	/// Remote selected for branch resolution or fetch.
69	pub remote: Option<String>,
70	/// Starting revision for a newly created branch.
71	pub from: Option<String>,
72	/// Step reached by this participant.
73	pub phase: Phase,
74}
75
76/// Whether a task is active or has been retired.
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum TaskLifecycle {
79	/// The task is managed as current work.
80	Active,
81	/// The task has been closed and retained in history.
82	Retired,
83}
84
85impl TaskLifecycle {
86	/// Return the stable spelling used in records or output.
87	pub fn as_str(self) -> &'static str {
88		match self {
89			Self::Active => "active",
90			Self::Retired => "retired",
91		}
92	}
93}
94
95/// Membership, placement and operation history for one task incarnation.
96#[derive(Clone, Debug)]
97pub struct Task {
98	/// Identifier distinguishing successive uses of the same task name.
99	pub generation: u64,
100	/// Removal or restoration evidence associated with this task.
101	pub removal: Option<super::removal::Removal>,
102	/// Branch name that identifies this task.
103	pub name: String,
104	/// Directory under which the task branch path is placed.
105	pub root: PathBuf,
106	/// Directory holding authoritative Workyard state.
107	pub state_dir: PathBuf,
108	/// Members associated with this task or operation.
109	pub participants: Vec<Participant>,
110	/// Allow reuse of an existing target branch.
111	pub checkout: bool,
112	/// Use locally available refs without fetching.
113	pub no_fetch: bool,
114	/// Current membership lifecycle of the task.
115	pub lifecycle: TaskLifecycle,
116	/// Invocation history tracked separately from task lifecycle.
117	pub operation: super::operation::Operation,
118	/// Diagnostic describing a failure or an unresolved condition.
119	pub problem: Option<Problem>,
120}
121
122impl Task {
123	/// A cancelled delivery can arrive after every participant was established.
124	pub(crate) fn preserve_settled_cancellation(&mut self) {
125		if self.operation.outcome == crate::OperationOutcome::Cancelled
126			&& self
127				.problem
128				.as_ref()
129				.is_some_and(|problem| problem.code == crate::ProblemCode::Cancelled)
130			&& self
131				.participants
132				.iter()
133				.all(|participant| participant.phase == Phase::Completed)
134		{
135			self.operation.outcome = crate::OperationOutcome::Succeeded;
136			self.problem = None;
137		}
138	}
139
140	/// Only a newly established worktree can publish its original discovery roots.
141	/// A later no-op recovery must respect an intervening explicit untrack.
142	pub fn admitted_roots(&self, previous: Option<&Self>) -> Vec<PathBuf> {
143		let established: std::collections::HashSet<_> = previous
144			.into_iter()
145			.flat_map(|task| &task.participants)
146			.filter(|p| p.worktree_identity.is_some())
147			.map(|p| &p.backing_identity)
148			.collect();
149		self.participants
150			.iter()
151			.filter(|p| p.worktree_identity.is_some() && !established.contains(&p.backing_identity))
152			.flat_map(|p| p.registration_roots.iter().cloned())
153			.collect()
154	}
155
156	/// Use the participant’s creation policy, falling back to the task defaults.
157	pub fn creation_options(&self, index: usize) -> super::creation::CreationOptions {
158		self.participants[index]
159			.creation
160			.unwrap_or(super::creation::CreationOptions {
161				checkout: self.checkout,
162				no_fetch: self.no_fetch,
163			})
164	}
165}