Skip to main content

workyard_core/
amend.rs

1//! Amend existing membership without treating ordinary invocation failure as task damage.
2mod addition;
3mod admission;
4mod completion;
5mod execution;
6mod existing;
7mod intent;
8mod movement;
9mod plan;
10mod removal;
11mod replacement;
12
13pub use intent::AmendRecovery;
14use intent::Intent;
15
16use crate::{
17	AmendGit, AmendRequest, AmendResult, AmendState, AmendmentChange, Control, Operation,
18	OperationKind, OperationOutcome, Problem, RemovalObserver, Task,
19};
20
21struct Amendment<'a, G, S: AmendState, O> {
22	git: &'a G,
23	store: &'a S,
24	guard: &'a S::Admission,
25	control: &'a Control,
26	observer: &'a O,
27	request: &'a AmendRequest,
28	targets: &'a [crate::RemovalTarget],
29	checkpoint: Option<S::Checkpoint>,
30	changed: bool,
31	uncertain: bool,
32	changes: Vec<AmendmentChange>,
33}
34
35/// Apply the requested membership changes to a managed task.
36pub async fn amend(
37	request: AmendRequest,
38	git: &impl AmendGit,
39	control: &Control,
40	observer: &impl RemovalObserver,
41	store: &impl AmendState,
42) -> Result<AmendResult, Problem> {
43	admission::validate_selection(&request)?;
44	let name = request.task.clone();
45	run(&name, Intent::Apply(request), git, control, observer, store).await
46}
47
48/// Resume the recorded amendment using its saved intent.
49pub async fn recover_amend(
50	request: AmendRequest,
51	git: &impl AmendGit,
52	control: &Control,
53	observer: &impl RemovalObserver,
54	store: &impl AmendState,
55) -> Result<AmendResult, Problem> {
56	admission::validate_selection(&request)?;
57	let scope = AmendRecovery::WithRemovalOptions(request.removal_options);
58	recover_amend_with_scope(&request.task, scope, git, control, observer, store).await
59}
60
61/// Resume an amendment under the caller's recovery policy.
62pub async fn recover_amend_with_scope(
63	name: &str,
64	scope: AmendRecovery,
65	git: &impl AmendGit,
66	control: &Control,
67	observer: &impl RemovalObserver,
68	store: &impl AmendState,
69) -> Result<AmendResult, Problem> {
70	run(name, Intent::Resume(scope), git, control, observer, store).await
71}
72
73async fn run(
74	name: &str,
75	intent: Intent,
76	git: &impl AmendGit,
77	control: &Control,
78	observer: &impl RemovalObserver,
79	store: &impl AmendState,
80) -> Result<AmendResult, Problem> {
81	crate::validate_task(name)?;
82	control.check()?;
83	let guard = store.admit(name)?;
84	let record = store.load(&guard, name)?;
85	let recovering = matches!(intent, Intent::Resume(_));
86	admission::validate(&record)?;
87	let mut request = intent.resolve(&record, store, &guard)?;
88
89	let restore_record = recovering && completion::needs_publication(&record, &request);
90	let original = record.into_amendment_task();
91	if recovering {
92		admission::refresh_request(&mut request, &original, git, control).await?;
93	}
94
95	let plan = plan::plan(&request, &original, recovering, git, store, &guard, control).await?;
96	// Persist resolved native locators and effective names so recovery is independent of CWD.
97	if !recovering {
98		request.repositories = plan
99			.changes
100			.iter()
101			.map(|change| crate::RepositoryInput {
102				registration_roots: change.participant.registration_roots.clone(),
103				expected_identity: Some(change.repository.identity.clone()),
104				path: change.repository.source.clone(),
105				name: Some(change.name.clone()),
106				remote: change.repository.remote.clone(),
107				from: change.participant.from.clone(),
108			})
109			.collect();
110	}
111
112	let mut task = original.clone();
113	task.removal = None;
114	task.operation = Operation {
115		kind: Some(OperationKind::Amend),
116		continues: None,
117		outcome: OperationOutcome::Pending,
118	};
119	let mut run = Amendment {
120		git,
121		store,
122		guard: &guard,
123		control,
124		observer,
125		request: &request,
126		targets: &plan.targets,
127		checkpoint: None,
128		changed: false,
129		uncertain: false,
130		changes: Vec::new(),
131	};
132	let result = run.execute(&mut task, plan.changes, restore_record).await;
133	if result.is_ok() && restore_record {
134		run.begin(&task)?;
135		run.changed = true;
136		run.publish(&task, 0)?;
137	}
138	run.finish(task, original, result, recovering)
139}
140
141impl<G: AmendGit, S: AmendState, O: RemovalObserver> Amendment<'_, G, S, O> {
142	fn begin(&mut self, task: &Task) -> Result<(), Problem> {
143		self.control.check()?;
144		self.store.verify()?;
145		if self.checkpoint.is_none() {
146			let checkpoint =
147				self.store
148					.begin_amend(self.guard, task, self.request, self.targets)?;
149			self.checkpoint = Some(checkpoint);
150		}
151		Ok(())
152	}
153
154	fn publish(&self, task: &Task, index: usize) -> Result<(), Problem> {
155		self.store
156			.record_effect(self.guard, task, index, crate::EffectState::Settled)?;
157		self.store.save(self.guard, task)
158	}
159
160	async fn report(
161		&mut self,
162		task: &Task,
163		index: usize,
164		kind: crate::AmendmentChangeKind,
165	) -> Result<(), Problem> {
166		self.report_named(task, &task.participants[index].name, kind)
167			.await
168	}
169
170	async fn report_named(
171		&mut self,
172		task: &Task,
173		participant: &str,
174		kind: crate::AmendmentChangeKind,
175	) -> Result<(), Problem> {
176		self.changes.push(AmendmentChange {
177			participant: participant.into(),
178			kind,
179		});
180		self.observer
181			.phase(&task.name, participant, kind.as_str())
182			.await
183	}
184}