1mod completion;
3pub(crate) mod consent;
4mod restoration;
5mod retirement;
6mod verification;
7
8use crate::{
9 Control, EffectState, Operation, OperationKind, OperationOutcome, Problem, ProblemCode,
10 RecoveryState, Removal, RemovalDirection, RemovalGit, RemovalObserver, RemovalOptions,
11 RemovalResult, Task, TaskLifecycle,
12};
13
14use completion::PriorState;
15
16struct RemovalRun<'a, G, S: RecoveryState, O> {
17 git: &'a G,
18 store: &'a S,
19 guard: &'a S::Admission,
20 control: &'a Control,
21 observer: &'a O,
22 options: RemovalOptions,
23 publication_attempted: bool,
24 inspected: bool,
25}
26
27enum RemovalIntent {
28 Remove(RemovalOptions),
29 ForwardOnly,
30 Recover(RemovalOptions),
31}
32
33impl RemovalIntent {
34 fn options(&self) -> RemovalOptions {
35 match self {
36 Self::Remove(options) | Self::Recover(options) => *options,
37 Self::ForwardOnly => RemovalOptions::default(),
38 }
39 }
40}
41
42pub async fn remove(
44 name: &str,
45 options: RemovalOptions,
46 git: &impl RemovalGit,
47 control: &Control,
48 observer: &impl RemovalObserver,
49 store: &impl RecoveryState,
50) -> Result<RemovalResult, Problem> {
51 run(
52 name,
53 RemovalIntent::Remove(options),
54 git,
55 control,
56 observer,
57 store,
58 )
59 .await
60}
61
62pub async fn retire(
64 name: &str,
65 git: &impl RemovalGit,
66 control: &Control,
67 observer: &impl RemovalObserver,
68 store: &impl RecoveryState,
69) -> Result<RemovalResult, Problem> {
70 run(
71 name,
72 RemovalIntent::ForwardOnly,
73 git,
74 control,
75 observer,
76 store,
77 )
78 .await
79}
80
81pub async fn recover_removal(
83 name: &str,
84 options: RemovalOptions,
85 git: &impl RemovalGit,
86 control: &Control,
87 observer: &impl RemovalObserver,
88 store: &impl RecoveryState,
89) -> Result<RemovalResult, Problem> {
90 run(
91 name,
92 RemovalIntent::Recover(options),
93 git,
94 control,
95 observer,
96 store,
97 )
98 .await
99}
100
101async fn run(
102 name: &str,
103 intent: RemovalIntent,
104 git: &impl RemovalGit,
105 control: &Control,
106 observer: &impl RemovalObserver,
107 store: &impl RecoveryState,
108) -> Result<RemovalResult, Problem> {
109 crate::validate_task(name)?;
110 control.check()?;
111 let guard = store.admit(name)?;
112 let record = store.load(&guard, name)?;
113 if let Some(index) = record.pending {
114 return Err(record.writer_problem(index));
115 }
116
117 let options = intent.options();
118 let recovering = matches!(intent, RemovalIntent::Recover(_));
119 if recovering && record.task.removal.is_none() {
120 return Err(Problem::new(
121 ProblemCode::InvalidInput,
122 "This task has no whole-task removal to resume.",
123 ));
124 }
125
126 let prior = PriorState::from_record(&record);
127 let mut task = record.task.clone();
128 restoration::retain_recorded_identity(&record, &mut task);
129 let path = store.record_path(&guard);
130 if recovering
131 && !(options.undo && task.lifecycle == TaskLifecycle::Retired)
132 && verification::unchanged(&record, &mut task, git, control).await?
133 {
134 control.check()?;
135 store.verify()?;
136 return Ok(prior.nothing_to_recover(path));
137 }
138
139 if task.lifecycle == TaskLifecycle::Retired {
140 if options.undo {
141 return Err(Problem::new(
142 ProblemCode::InvalidInput,
143 "This task is already retired; undo applies only to incomplete removal.",
144 )
145 .remedy("Use workyard checkout to create fresh membership on the retained branches."));
146 }
147 if recovering {
148 task.operation = task.operation.recovery();
149 }
150 task.operation.outcome = OperationOutcome::Succeeded;
151 if recovering {
152 task.problem = None;
153 store.save(&guard, &task)?;
154 }
155 return Ok(RemovalResult {
156 nothing_to_recover: false,
157 operation: task.operation,
158 problem: None,
159 task,
160 record: path,
161 mutation_started: recovering,
162 inspected: false,
163 record_available: prior.record_available,
164 needs_attention: prior.needs_attention,
165 });
166 }
167
168 if options.undo && task.removal.is_none() {
169 return Err(Problem::new(
170 ProblemCode::InvalidInput,
171 "This task has no removal to undo.",
172 ));
173 }
174
175 if !recovering
176 && task
177 .removal
178 .as_ref()
179 .is_some_and(Removal::restoration_complete)
180 {
181 task.removal = None;
182 }
183
184 if matches!(intent, RemovalIntent::ForwardOnly)
185 && task
186 .removal
187 .as_ref()
188 .is_some_and(|r| r.direction == RemovalDirection::Restore)
189 {
190 return Err(Problem::new(
191 ProblemCode::InvalidInput,
192 "This task has a pending removal restoration.",
193 )
194 .remedy("Finish restoration through CLI or RPC recovery before removing the task."));
195 }
196
197 task.operation = Operation {
198 kind: Some(if recovering {
199 OperationKind::Recover
200 } else {
201 OperationKind::Remove
202 }),
203 continues: recovering.then_some(OperationKind::Remove),
204 outcome: OperationOutcome::Pending,
205 };
206 task.problem = None;
207
208 let mut run = RemovalRun {
209 git,
210 store,
211 guard: &guard,
212 control,
213 observer,
214 options,
215 publication_attempted: false,
216 inspected: false,
217 };
218 let result = run.execute(&mut task).await;
219 Ok(run.finish(task, result, prior, path))
220}
221
222impl<G: RemovalGit, S: RecoveryState, O: RemovalObserver> RemovalRun<'_, G, S, O> {
223 async fn execute(&mut self, task: &mut Task) -> Result<(), Problem> {
224 if self.options.undo
225 || task
226 .removal
227 .as_ref()
228 .is_some_and(|r| r.direction == RemovalDirection::Restore)
229 {
230 return self.restore(task).await;
231 }
232
233 self.retire(task).await
234 }
235
236 fn publish(&mut self, task: &Task, index: usize) -> Result<(), Problem> {
237 self.publication_attempted = true;
238 self.store
239 .record_effect(self.guard, task, index, EffectState::Settled)?;
240 self.store.save(self.guard, task)
241 }
242}