1use camino::{Utf8Path, Utf8PathBuf};
16use serde::Serialize;
17
18use crate::adapters::history::History;
19use crate::candidate::Ownership;
20use crate::error::AppError;
21
22pub const SCHEMA: &str = "sdd.stage-compare/2";
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
27#[serde(rename_all = "kebab-case")]
28pub enum State {
29 Equal,
31 Absent,
33 Differs,
35 Retired,
38 Dropped,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
46#[serde(rename_all = "kebab-case")]
47pub enum Provenance {
48 Attributed,
50 Reduced,
52 Unknown,
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
58#[serde(rename_all = "kebab-case")]
59pub enum RowOwnership {
60 Managed,
62 Integration,
64 Adopted,
66}
67
68impl RowOwnership {
69 const fn landing(self) -> Ownership {
70 match self {
71 Self::Managed => Ownership::Managed,
72 Self::Integration => Ownership::Integration,
73 Self::Adopted => Ownership::Adopted,
74 }
75 }
76
77 #[must_use]
79 pub const fn as_str(self) -> &'static str {
80 match self {
81 Self::Managed => "managed",
82 Self::Integration => "integration",
83 Self::Adopted => "adopted",
84 }
85 }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
90pub struct Row {
91 pub path: String,
93 pub state: State,
95 pub ownership: RowOwnership,
97 pub provenance: Option<Provenance>,
100 pub vouched: Option<bool>,
104 pub replaceable: bool,
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
112pub struct HistoryState {
113 pub available: bool,
115 pub reason: Option<String>,
117}
118
119#[derive(Debug, Clone, Serialize)]
121pub struct Report {
122 pub schema: &'static str,
124 pub root: Utf8PathBuf,
126 pub target: Utf8PathBuf,
128 pub history: HistoryState,
130 pub rows: Vec<Row>,
133 pub refused_packages: Vec<String>,
136}
137
138pub fn compare(root: &Utf8Path, target: &Utf8Path) -> Result<Report, AppError> {
147 let choices = super::read_choices(root, target)?;
148 let replaceable = crate::services::upgrader::replaceable(&choices)?;
149 let target = choices.target.clone();
150 let recorded = crate::services::installer::recorded(&target);
151 let refused_packages = crate::landing::apply::refused_skill_packages(&target, &recorded);
152 let (history, history_state) = match History::open(&target) {
153 Ok(history) => (
154 Some(history),
155 HistoryState {
156 available: true,
157 reason: None,
158 },
159 ),
160 Err(missing) => (
161 None,
162 HistoryState {
163 available: false,
164 reason: Some(missing.to_string()),
165 },
166 ),
167 };
168 let touches = |path: &Utf8Path| history.as_ref().is_some_and(|held| held.touches(path));
169 let provenance = |vouched: bool, path: &Utf8Path| match (vouched, touches(path)) {
170 (true, true) => Provenance::Attributed,
171 (false, false) => Provenance::Unknown,
172 _ => Provenance::Reduced,
173 };
174
175 let mut rows = Vec::new();
176 let mut staged_paths = Vec::new();
177 for artifact in &choices.artifacts {
178 let ownership = match artifact.ownership.as_str() {
179 "managed" => RowOwnership::Managed,
180 "integration" => RowOwnership::Integration,
181 "adopted" => RowOwnership::Adopted,
182 _ => continue,
185 };
186 let path = Utf8PathBuf::from(&artifact.path);
187 staged_paths.push(artifact.path.clone());
188 let staged = std::fs::read(root.join(super::ARTIFACTS_DIR).join(&path))?;
189 let Ok(held) = std::fs::read(target.join(&path)) else {
190 rows.push(Row {
191 path: artifact.path.clone(),
192 state: State::Absent,
193 ownership,
194 provenance: None,
195 vouched: None,
196 replaceable: replaceable.contains(&artifact.path),
197 });
198 continue;
199 };
200 let vouched = crate::landing::apply::vouches(&recorded, &path, ownership.landing(), &held);
201 let equal = match ownership {
202 RowOwnership::Integration => region(&path, &held) == region(&path, &staged),
205 RowOwnership::Managed | RowOwnership::Adopted => held == staged,
206 };
207 rows.push(Row {
208 path: artifact.path.clone(),
209 state: if equal { State::Equal } else { State::Differs },
210 ownership,
211 provenance: (!equal).then(|| provenance(vouched, &path)),
212 vouched: Some(vouched),
213 replaceable: replaceable.contains(&artifact.path),
214 });
215 }
216 let landing: Vec<&str> = staged_paths.iter().map(String::as_str).collect();
219 let departures = crate::landing::apply::departures_from(&target, &landing, &recorded);
220 let retired = departures
221 .retired
222 .into_iter()
223 .map(|(destination, _)| (destination, State::Retired, true));
224 let dropped = departures
225 .dropped
226 .into_iter()
227 .map(|destination| (destination, State::Dropped, false));
228 for (destination, state, vouched) in retired.chain(dropped) {
229 let path = Utf8PathBuf::from(&destination);
230 rows.push(Row {
231 provenance: Some(provenance(vouched, &path)),
232 vouched: Some(vouched),
233 replaceable: replaceable.contains(&destination),
234 path: destination,
235 state,
236 ownership: RowOwnership::Managed,
237 });
238 }
239 Ok(Report {
240 schema: SCHEMA,
241 root: root.to_owned(),
242 target,
243 history: history_state,
244 rows,
245 refused_packages,
246 })
247}
248
249fn region(path: &Utf8Path, bytes: &[u8]) -> Option<crate::domain::ownership::Sha256> {
251 use crate::domain::marker;
252
253 let text = std::str::from_utf8(bytes).ok()?;
254 if path == crate::domain::paths::HOOKS_CONFIG_PATH {
255 marker::block_hash(text)
256 } else {
257 marker::block_hash_with(text, marker::AGENTS_BEGIN, marker::AGENTS_END)
258 }
259}