Skip to main content

workyard_application/actions/recover/
contract.rs

1use serde::Serialize;
2use trine::NativePath;
3use workyard_core::{OperationKind, Problem, Task};
4
5pub use crate::contracts::{OperationReport, ParticipantOutcome};
6use crate::contracts::{ProblemOutcome, removal};
7
8/// Result of reconciling a single task and its participants.
9#[derive(Serialize)]
10pub struct RecoverOutcome {
11	#[serde(skip)]
12	pub(crate) explicit_workspace: Option<NativePath>,
13	/// Failure to record a project root after participant work succeeded.
14	#[serde(skip_serializing_if = "Option::is_none")]
15	pub registration_problem: Option<ProblemOutcome>,
16	/// Verified no-op; task/evidence history was preserved.
17	pub nothing_to_recover: bool,
18	/// User-facing health classification for the observed task.
19	#[serde(skip_serializing_if = "Option::is_none")]
20	pub condition: Option<crate::contracts::Condition>,
21	/// Whether the invocation entered the mutation phase.
22	#[serde(skip_serializing_if = "Option::is_none")]
23	pub mutation_started: Option<bool>,
24	/// Removal or restoration evidence associated with this task.
25	#[serde(flatten)]
26	pub removal: removal::RemovalReport,
27	/// Invocation history tracked separately from task lifecycle.
28	pub operation: OperationReport,
29	/// None when this invocation could not establish task membership.
30	pub lifecycle: Option<&'static str>,
31	/// Task selected for this operation.
32	pub task: String,
33	/// Directory containing the task workspaces.
34	pub tasks_root: Option<NativePath>,
35	/// Directory holding authoritative Workyard state.
36	pub state_dir: Option<NativePath>,
37	/// Location of the task record when available.
38	pub record: Option<NativePath>,
39	/// Members associated with this task or operation.
40	pub participants: Vec<ParticipantOutcome>,
41	/// Changes reported for individual participants.
42	#[serde(skip_serializing_if = "Vec::is_empty")]
43	pub changes: Vec<crate::contracts::ParticipantChange>,
44	/// Diagnostic describing a failure or an unresolved condition.
45	#[serde(skip_serializing_if = "Option::is_none")]
46	pub problem: Option<ProblemOutcome>,
47}
48
49impl RecoverOutcome {
50	/// Present a removal or restoration result with its participant evidence.
51	pub fn from_removal(result: workyard_core::RemovalResult) -> Self {
52		let condition = removal::condition(&result);
53		let nothing_to_recover = result.nothing_to_recover;
54		let mut outcome = Self::from_task(result.task, result.record);
55		outcome.nothing_to_recover = nothing_to_recover;
56		outcome.operation = result.operation.into();
57		outcome.problem = result.problem.map(Into::into);
58		outcome.condition = Some(condition);
59		outcome.mutation_started = Some(result.mutation_started);
60
61		if !result.mutation_started && result.inspected {
62			removal::mark_present_after_inspection(&mut outcome.participants);
63		}
64
65		outcome
66	}
67
68	/// Build an invocation outcome for a request rejected before effects.
69	pub fn rejected(task: String, problem: Problem) -> Self {
70		Self {
71			changes: Vec::new(),
72			registration_problem: None,
73			nothing_to_recover: false,
74			condition: None,
75			mutation_started: None,
76			removal: Default::default(),
77			operation: OperationReport::rejected(OperationKind::Recover, &problem),
78			explicit_workspace: None,
79			lifecycle: None,
80			task,
81			tasks_root: None,
82			state_dir: None,
83			record: None,
84			participants: Vec::new(),
85			problem: Some(problem.into()),
86		}
87	}
88
89	/// Present the recorded task and its participants.
90	pub fn from_task(task: Task, record: std::path::PathBuf) -> Self {
91		let participants = removal::participants(task.participants, task.removal.as_ref());
92
93		Self {
94			changes: Vec::new(),
95			registration_problem: None,
96			nothing_to_recover: false,
97			condition: None,
98			mutation_started: None,
99			removal: Default::default(),
100			operation: task.operation.into(),
101			explicit_workspace: None,
102			lifecycle: Some(task.lifecycle.as_str()),
103			task: task.name,
104			tasks_root: Some(NativePath::new(task.root).expect("validated root path")),
105			state_dir: Some(NativePath::new(task.state_dir).expect("validated state path")),
106			record: Some(NativePath::new(record).expect("validated filesystem path")),
107			participants,
108			problem: task.problem.map(Into::into),
109		}
110	}
111}
112
113/// Result of either named task recovery or workspace-wide reconciliation.
114#[derive(Serialize)]
115#[serde(untagged)]
116pub enum RecoveryOutcome {
117	/// Result of reconciling one named task.
118	Task(Box<RecoverOutcome>),
119	/// Result of reconciling workspace placement.
120	Workspace(super::workspace::WorkspaceRecovery),
121}