Skip to main content

workyard_core/ports/
removal.rs

1use crate::{Control, Git, Participant, Problem, Repository};
2use std::{future::Future, path::PathBuf};
3
4/// Retained branch or commit history disclosed during unlinking.
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub enum RetainedHistory {
7	/// No unpushed commits were reported.
8	UpToDate,
9	/// Local commits have yet to reach the upstream.
10	Unpushed,
11	/// The local branch has no configured upstream.
12	NoUpstream,
13	/// The worktree is detached from a local branch.
14	Detached,
15}
16
17/// Git facts used to decide whether unlinking needs consent.
18#[derive(Clone, Debug)]
19pub struct RemovalObservation {
20	/// Identity of the worktree's Git administration entry.
21	pub administration_identity: String,
22	/// Native filesystem identity recorded for the worktree entry.
23	pub worktree_entry: (u64, u64),
24	/// Verified backing repository for this participant.
25	pub repository: Repository,
26	/// Location of the participant worktree.
27	pub destination: Option<PathBuf>,
28	/// Branch associated with the worktree, when known.
29	pub branch: Option<String>,
30	/// Local changes that could be discarded by unlinking.
31	pub dirty_paths: Vec<PathBuf>,
32	/// Content-sensitive token scoped to this inspection, never durable consent.
33	pub dirty_token: Vec<u8>,
34	/// Observed upstream relationship of the retained branch.
35	pub history: RetainedHistory,
36}
37
38impl RemovalObservation {
39	pub(crate) fn same_worktree(&self, other: &Self) -> bool {
40		self.destination == other.destination
41			&& self.administration_identity == other.administration_identity
42			&& self.worktree_entry == other.worktree_entry
43	}
44}
45
46/// Only a destructive command can leave behind an unconfirmed unlink writer.
47#[derive(Debug)]
48pub enum UnlinkFailure {
49	/// The unlink command had not started.
50	BeforeLaunch(Problem),
51	/// The unlink command was attempted.
52	Command(Problem),
53}
54
55impl From<Problem> for UnlinkFailure {
56	fn from(problem: Problem) -> Self {
57		Self::BeforeLaunch(problem)
58	}
59}
60
61/// Inspect, unlink and restore participants while preserving retained history.
62pub trait RemovalGit: Git {
63	/// Only for durable successful recreation evidence, never a failed add attempt.
64	fn reconcile_recreation(
65		&self,
66		participant: &Participant,
67		branch: &str,
68		control: &Control,
69	) -> impl Future<Output = Result<crate::RecoveryObservation, Problem>> + Send;
70
71	/// Verify a worktree recreated during removal recovery.
72	fn reconcile_restored(
73		&self,
74		participant: &Participant,
75		control: &Control,
76	) -> impl Future<Output = Result<crate::RecoveryObservation, Problem>> + Send;
77
78	/// Collect identity, history and local changes before unlinking.
79	fn inspect_removal(
80		&self,
81		participant: &Participant,
82		control: &Control,
83	) -> impl Future<Output = Result<RemovalObservation, Problem>> + Send;
84
85	/// Unlink the verified worktree under the selected removal policy.
86	fn remove_worktree(
87		&self,
88		observation: &RemovalObservation,
89		discard: bool,
90		control: &Control,
91		before_unlink: impl FnOnce() -> Result<(), Problem> + Send,
92	) -> impl Future<Output = Result<(), UnlinkFailure>> + Send;
93
94	/// Check whether the retained branch and destination permit restoration.
95	fn inspect_restoration(
96		&self,
97		participant: &Participant,
98		branch: &str,
99		control: &Control,
100	) -> impl Future<Output = Result<Repository, Problem>> + Send;
101}
102
103/// Kind of user consent requested before continuing.
104#[derive(Clone, Copy, Debug, PartialEq, Eq)]
105pub enum DecisionKind {
106	/// Confirm retirement of the entire task.
107	RemoveTask,
108	/// Confirm removal of the selected members.
109	RemoveParticipants,
110	/// Authorize discarding changes in the selected participants.
111	DiscardParticipants,
112	/// Authorize discarding changes during task retirement.
113	DiscardTask,
114	/// Authorize newly disclosed local file loss.
115	DiscardFiles,
116	/// Retained branch or commit history disclosed during unlinking.
117	RetainedHistory,
118	/// Acknowledge that restoration cannot reproduce discarded edits.
119	LostChanges,
120}
121
122/// Context supplied to a view when an operation needs consent.
123#[derive(Clone, Debug)]
124pub struct Decision {
125	/// Task selected for this operation.
126	pub task: Option<String>,
127	/// Classification used to interpret this record.
128	pub kind: DecisionKind,
129	/// Participant to which this observation or change applies.
130	pub participant: String,
131	/// Members associated with this task or operation.
132	pub participants: Vec<String>,
133	/// Human-readable explanation for the caller.
134	pub message: String,
135}
136
137/// Receives removal disclosures and requests for consent.
138pub trait RemovalObserver: crate::Observer {
139	/// Scope the disclosure to the task incarnation under mutation admission.
140	fn begin_disclosure(&self, _task: &crate::Task, _amendment: Option<&crate::AmendRequest>) {}
141
142	/// Exact approvals must also notice when previously disclosed changes have been saved.
143	fn requires_unchanged_disclosure(&self) -> bool {
144		false
145	}
146
147	/// Report removal facts before asking for a decision.
148	fn disclose(
149		&self,
150		_participant: &str,
151		_observation: &RemovalObservation,
152	) -> impl Future<Output = Result<(), Problem>> + Send {
153		async { Ok(()) }
154	}
155
156	/// Forget a completed target before any later discard decision.
157	fn removed(
158		&self,
159		task: &str,
160		participant: &str,
161	) -> impl Future<Output = Result<(), Problem>> + Send {
162		self.phase(task, participant, "removed")
163	}
164
165	/// Returns None when the interface cannot ask; never silently grants consent.
166	fn confirm(
167		&self,
168		_decision: Decision,
169	) -> impl Future<Output = Result<Option<bool>, Problem>> + Send {
170		async { Ok(None) }
171	}
172}
173
174/// Consent and recovery direction for removal.
175#[derive(Clone, Copy, Debug, Default)]
176pub struct RemovalOptions {
177	/// Accept routine confirmation.
178	pub yes: bool,
179	/// Authorize the disclosed discard and accept routine confirmation.
180	pub force: bool,
181	/// Resume in the restoration direction.
182	pub undo: bool,
183}