1use camino::{Utf8Path, Utf8PathBuf};
15use serde::Serialize;
16
17use crate::adapters::history::History;
18use crate::candidate::Ownership;
19use crate::error::AppError;
20
21pub const SCHEMA: &str = "sdd.stage-compare/1";
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
26#[serde(rename_all = "kebab-case")]
27pub enum State {
28 Equal,
30 Absent,
32 Differs,
34 Retired,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
40#[serde(rename_all = "kebab-case")]
41pub enum Provenance {
42 Attributed,
44 Reduced,
46 Unknown,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
52#[serde(rename_all = "kebab-case")]
53pub enum RowOwnership {
54 Managed,
56 Integration,
58 Adopted,
60}
61
62impl RowOwnership {
63 const fn landing(self) -> Ownership {
64 match self {
65 Self::Managed => Ownership::Managed,
66 Self::Integration => Ownership::Integration,
67 Self::Adopted => Ownership::Adopted,
68 }
69 }
70
71 #[must_use]
73 pub const fn as_str(self) -> &'static str {
74 match self {
75 Self::Managed => "managed",
76 Self::Integration => "integration",
77 Self::Adopted => "adopted",
78 }
79 }
80}
81
82#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
84pub struct Row {
85 pub path: String,
87 pub state: State,
89 pub ownership: RowOwnership,
91 pub provenance: Option<Provenance>,
93 pub vouched: Option<bool>,
97 pub replaceable: bool,
101}
102
103#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
105pub struct HistoryState {
106 pub available: bool,
108 pub reason: Option<String>,
110}
111
112#[derive(Debug, Clone, Serialize)]
114pub struct Report {
115 pub schema: &'static str,
117 pub root: Utf8PathBuf,
119 pub target: Utf8PathBuf,
121 pub history: HistoryState,
123 pub rows: Vec<Row>,
125 pub refused_packages: Vec<String>,
128}
129
130pub fn compare(root: &Utf8Path, target: &Utf8Path) -> Result<Report, AppError> {
139 let choices = super::read_choices(root, target)?;
140 let replaceable = crate::services::upgrader::replaceable(&choices)?;
141 let target = choices.target.clone();
142 let recorded = crate::services::installer::recorded(&target);
143 let refused_packages = crate::landing::apply::refused_skill_packages(&target, &recorded);
144 let (history, history_state) = match History::open(&target) {
145 Ok(history) => (
146 Some(history),
147 HistoryState {
148 available: true,
149 reason: None,
150 },
151 ),
152 Err(missing) => (
153 None,
154 HistoryState {
155 available: false,
156 reason: Some(missing.to_string()),
157 },
158 ),
159 };
160 let touches = |path: &Utf8Path| history.as_ref().is_some_and(|held| held.touches(path));
161 let provenance = |vouched: bool, path: &Utf8Path| match (vouched, touches(path)) {
162 (true, true) => Provenance::Attributed,
163 (false, false) => Provenance::Unknown,
164 _ => Provenance::Reduced,
165 };
166
167 let mut rows = Vec::new();
168 let mut staged_paths = Vec::new();
169 for artifact in &choices.artifacts {
170 let ownership = match artifact.ownership.as_str() {
171 "managed" => RowOwnership::Managed,
172 "integration" => RowOwnership::Integration,
173 "adopted" => RowOwnership::Adopted,
174 _ => continue,
177 };
178 let path = Utf8PathBuf::from(&artifact.path);
179 staged_paths.push(artifact.path.clone());
180 let staged = std::fs::read(root.join(super::ARTIFACTS_DIR).join(&path))?;
181 let Ok(held) = std::fs::read(target.join(&path)) else {
182 rows.push(Row {
183 path: artifact.path.clone(),
184 state: State::Absent,
185 ownership,
186 provenance: None,
187 vouched: None,
188 replaceable: replaceable.contains(&artifact.path),
189 });
190 continue;
191 };
192 let vouched = crate::landing::apply::vouches(&recorded, &path, ownership.landing(), &held);
193 let equal = match ownership {
194 RowOwnership::Integration => region(&path, &held) == region(&path, &staged),
197 RowOwnership::Managed | RowOwnership::Adopted => held == staged,
198 };
199 rows.push(Row {
200 path: artifact.path.clone(),
201 state: if equal { State::Equal } else { State::Differs },
202 ownership,
203 provenance: (!equal).then(|| provenance(vouched, &path)),
204 vouched: Some(vouched),
205 replaceable: replaceable.contains(&artifact.path),
206 });
207 }
208 for (destination, _) in &recorded.managed {
209 if staged_paths.contains(destination) {
210 continue;
211 }
212 let path = Utf8PathBuf::from(destination);
213 let (provenance, vouched) = match std::fs::read(target.join(&path)) {
218 Ok(held) => {
219 let vouched = crate::landing::apply::vouches_retired(&recorded, destination, &held);
220 (Some(provenance(vouched, &path)), Some(vouched))
221 }
222 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
223 (Some(provenance(false, &path)), None)
224 }
225 Err(_) => (Some(Provenance::Unknown), None),
226 };
227 rows.push(Row {
228 path: destination.clone(),
229 state: State::Retired,
230 ownership: RowOwnership::Managed,
231 provenance,
232 vouched,
233 replaceable: replaceable.contains(destination),
234 });
235 }
236 Ok(Report {
237 schema: SCHEMA,
238 root: root.to_owned(),
239 target,
240 history: history_state,
241 rows,
242 refused_packages,
243 })
244}
245
246fn region(path: &Utf8Path, bytes: &[u8]) -> Option<crate::domain::ownership::Sha256> {
248 use crate::domain::marker;
249
250 let text = std::str::from_utf8(bytes).ok()?;
251 if path == crate::domain::paths::HOOKS_CONFIG_PATH {
252 marker::block_hash(text)
253 } else {
254 marker::block_hash_with(text, marker::AGENTS_BEGIN, marker::AGENTS_END)
255 }
256}