1use std::collections::{BTreeMap, BTreeSet};
2
3use serde::{Deserialize, Serialize};
4use subc_config::{Digest, Generation, RecordedValue};
5
6pub const RECENT_HISTORY_LIMIT: usize = 20;
8pub type Snapshot = BTreeMap<String, RecordedValue>;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum IntegrityMode {
13 Sealed,
14 DigestOnly,
15}
16
17#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
18pub struct Tuple {
19 pub module: String,
20 pub project: Option<String>,
21}
22
23#[derive(Clone, Debug, Default, Serialize, Deserialize)]
24pub struct Versions {
25 pub active: Option<u64>,
26 pub recovery: Option<u64>,
27 pub pending: Option<u64>,
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
31#[serde(rename_all = "snake_case")]
32pub enum CommitKind {
33 Stamp,
34 Unstamp,
35 Approved,
36 Rollback,
37 Development,
38 DeclarationApproval,
39}
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42pub struct DeliverySnapshot {
43 pub daemon: Option<subc_protocol::config::ConfigLayerSnapshot>,
44 pub modules: BTreeMap<String, subc_protocol::config::ConfigView>,
45 pub declarations: BTreeMap<String, subc_protocol::config::ConfigDeclareRequest>,
46}
47
48#[derive(Clone, Debug, Serialize, Deserialize)]
49pub struct HistoryEntry {
50 pub generation: Generation,
51 pub kind: CommitKind,
52 pub approval_id: String,
53 pub outcome: String,
54 pub tuples: BTreeSet<Tuple>,
55 pub configuration: Snapshot,
57 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub delivery: Option<DeliverySnapshot>,
61}
62
63#[derive(Clone, Debug, Serialize, Deserialize)]
64pub struct PrunedGeneration {
65 pub generation: u64,
66 pub approval_channel: subc_config::ApprovalChannel,
67 pub plan_digest: Digest,
68 pub approval_id: String,
69 pub outcome: String,
70 pub pruned_at_ms: u64,
71}
72
73#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
74pub struct Proposal {
75 pub base_revision: u64,
76 pub snapshot_digest: Digest,
77 pub diff: BTreeMap<String, Option<RecordedValue>>,
78}
79
80#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
82pub struct ProgramRefusal {
83 pub module: String,
84 pub approved_base: Digest,
85 pub refused_identity: Digest,
86}
87
88pub const PROGRAM_REFUSAL_LIMIT: usize = 256;
89
90pub const DAEMON_MODULE: &str = "subc";
93
94#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
97pub struct RestartIntent {
98 pub generation: u64,
99 pub attempted: bool,
102}
103
104#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
107pub struct DaemonPointers {
108 pub active: u64,
113 pub awaiting_restart: Option<u64>,
117 pub restart: Option<RestartIntent>,
118}
119
120#[derive(Clone, Debug, Default, Serialize, Deserialize)]
123pub struct StoreBody {
124 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
126 pub approved_declarations: BTreeMap<String, BTreeMap<String, String>>,
127 #[serde(default, skip_serializing_if = "Vec::is_empty")]
128 pub program_refusals: Vec<ProgramRefusal>,
129 pub stamped: bool,
130 pub base_revision: u64,
131 pub last_generation: u64,
132 pub approved: Snapshot,
133 pub generations: BTreeMap<u64, HistoryEntry>,
134 pub versions: Vec<(Tuple, Versions)>,
135 pub pruned: BTreeMap<u64, PrunedGeneration>,
136 pub proposal: Option<Proposal>,
137 pub observed: Option<Snapshot>,
139 pub daemon: DaemonPointers,
140}
141
142fn requires_daemon_restart(base: Option<&HistoryEntry>, entry: &HistoryEntry) -> bool {
146 if entry
147 .generation
148 .changeset
149 .fields
150 .iter()
151 .any(|field| field.apply == subc_config::ApplyKind::DaemonRestart)
152 {
153 return true;
154 }
155 if entry
156 .delivery
157 .as_ref()
158 .is_none_or(|delivery| delivery.daemon.is_none())
159 {
160 return false;
162 }
163 let (Some(base), Some(next)) = (base.and_then(daemon_config), daemon_config(entry)) else {
164 return true;
166 };
167 base.port != next.port
168 || base.storage != next.storage
169 || base.admission_facts_carrier_module_id != next.admission_facts_carrier_module_id
170 || base.admission_facts_targets != next.admission_facts_targets
171 || base.scope_authority_owners != next.scope_authority_owners
172 || base.privileged_operator_prompt != next.privileged_operator_prompt
173}
174
175fn daemon_config(entry: &HistoryEntry) -> Option<crate::daemon_config::DaemonConfig> {
176 let layer = entry.delivery.as_ref()?.daemon.as_ref()?;
177 let doc = std::str::from_utf8(layer.bytes.as_deref()?).ok()?;
178 let path = std::path::Path::new(layer.path.as_deref()?);
179 crate::daemon_config::parse_with_activity(doc, path)
180 .ok()
181 .map(|(config, _)| config)
182}
183
184pub(crate) fn generation_failed(entry: &HistoryEntry) -> bool {
186 entry.outcome == "failed"
187 || !entry.generation.never_redeliver.is_empty()
188 || entry
189 .generation
190 .module_states
191 .values()
192 .any(|state| *state == subc_config::GenerationState::Failed)
193}
194
195fn bootable(entry: &HistoryEntry) -> bool {
196 !matches!(entry.kind, CommitKind::Development | CommitKind::Unstamp)
197 && !entry.generation.development
198 && entry
199 .delivery
200 .as_ref()
201 .is_some_and(|delivery| delivery.daemon.is_some())
202 && !generation_failed(entry)
203}
204
205impl StoreBody {
206 pub(super) fn awaits_restart(&self, entry: &HistoryEntry) -> bool {
209 let base = self
210 .active_daemon_generation()
211 .and_then(|id| self.generations.get(&id));
212 self.daemon.awaiting_restart.is_none() && requires_daemon_restart(base, entry)
213 }
214
215 pub(super) fn first_awaiting_restart_after(&self, active: u64) -> Option<u64> {
218 let base = self.generations.get(&active);
219 self.generations
220 .range(active.saturating_add(1)..)
221 .find(|(_, entry)| {
222 !matches!(entry.kind, CommitKind::Development | CommitKind::Unstamp)
223 && !entry.generation.development
224 && requires_daemon_restart(base, entry)
225 })
226 .map(|(&id, _)| id)
227 }
228
229 pub(crate) fn active_daemon_generation(&self) -> Option<u64> {
233 let limit = self.daemon.awaiting_restart.unwrap_or(u64::MAX);
234 self.generations
235 .range(..limit)
236 .rev()
237 .find(|(_, entry)| bootable(entry))
238 .map(|(&id, _)| id)
239 }
240
241 pub(crate) fn restart_candidate(&self) -> Option<u64> {
244 let candidate = self
245 .generations
246 .iter()
247 .rev()
248 .find(|(_, entry)| bootable(entry))
249 .map(|(&id, _)| id)?;
250 (Some(candidate) > self.active_daemon_generation()).then_some(candidate)
251 }
252
253 pub(crate) fn bootable_generation(&self, id: u64) -> bool {
254 self.generations.get(&id).is_some_and(bootable)
255 }
256
257 pub(super) fn protected_generations(&self) -> BTreeSet<u64> {
260 let mut keep = BTreeSet::new();
261 let mut placed_modules = BTreeSet::new();
265 for (&id, entry) in self.generations.iter().rev().take_while(|(_, entry)| {
266 matches!(
267 entry.kind,
268 CommitKind::Development | CommitKind::DeclarationApproval
269 )
270 }) {
271 if entry.kind != CommitKind::Development {
272 continue;
273 }
274 for module in entry.generation.changeset.launch_manifests.keys() {
275 if placed_modules.insert(module) {
276 keep.insert(id);
277 }
278 }
279 }
280 for (_, versions) in &self.versions {
281 keep.extend(
282 [versions.active, versions.recovery, versions.pending]
283 .into_iter()
284 .flatten(),
285 );
286 }
287 keep.extend(
291 [
292 self.daemon.restart.map(|intent| intent.generation),
293 self.active_daemon_generation(),
294 self.daemon.awaiting_restart,
295 ]
296 .into_iter()
297 .flatten()
298 .filter(|id| self.generations.contains_key(id)),
299 );
300 let mut daemon_versions = Vec::new();
301 for (&id, entry) in self.generations.iter().rev() {
302 if daemon_versions.len() == 2 {
303 break;
304 }
305 if matches!(
306 entry.kind,
307 CommitKind::Development | CommitKind::DeclarationApproval
308 ) || entry.generation.development
309 {
310 continue;
311 }
312 let Some(daemon) = entry
313 .delivery
314 .as_ref()
315 .and_then(|delivery| delivery.daemon.as_ref())
316 else {
317 continue;
318 };
319 let Some(input) = entry.generation.input_vector.inputs.get("subc.jsonc") else {
320 keep.insert(id);
323 continue;
324 };
325 let version = (input, &daemon.digest);
326 if !daemon_versions.contains(&version) {
327 daemon_versions.push(version);
328 keep.insert(id);
329 }
330 }
331 keep
332 }
333
334 pub(super) fn prune(&mut self, now_ms: u64) {
337 let protected = self.protected_generations();
338 let mut keep = protected.clone();
339 let tuples: BTreeSet<_> = self
340 .generations
341 .values()
342 .flat_map(|e| e.tuples.iter().cloned())
343 .collect();
344 for tuple in tuples.into_iter().map(Some).chain(std::iter::once(None)) {
345 let mut recent = 0;
346 for (&id, entry) in self.generations.iter().rev() {
347 let belongs = match &tuple {
348 Some(tuple) => entry.tuples.contains(tuple),
349 None => entry.tuples.is_empty(),
350 };
351 if !belongs || protected.contains(&id) {
352 continue;
353 }
354 if recent < RECENT_HISTORY_LIMIT {
355 keep.insert(id);
356 }
357 recent += 1;
358 }
359 }
360 let removed: Vec<_> = self
361 .generations
362 .keys()
363 .copied()
364 .filter(|id| !keep.contains(id))
365 .collect();
366 for id in removed {
367 self.prune_generation(id, now_ms);
368 }
369 }
370
371 pub(super) fn prune_oldest(&mut self, now_ms: u64) -> bool {
372 let protected = self.protected_generations();
373 let Some(id) = self
374 .generations
375 .keys()
376 .copied()
377 .find(|id| !protected.contains(id))
378 else {
379 return false;
380 };
381 self.prune_generation(id, now_ms);
382 true
383 }
384
385 fn prune_generation(&mut self, id: u64, now_ms: u64) {
386 if let Some(entry) = self.generations.remove(&id) {
387 self.pruned.insert(
388 id,
389 PrunedGeneration {
390 generation: id,
391 approval_channel: entry.generation.approval_channel,
392 plan_digest: entry.generation.plan_digest,
393 approval_id: entry.approval_id,
394 outcome: entry.outcome,
395 pruned_at_ms: now_ms,
396 },
397 );
398 }
399 }
400
401 pub(super) fn rebase_proposal(&mut self) -> Result<bool, super::StoreError> {
402 let old_diff = self
403 .proposal
404 .as_ref()
405 .map(|p| &p.diff)
406 .cloned()
407 .unwrap_or_default();
408 let Some(snapshot) = &self.observed else {
409 self.proposal = None;
410 return Ok(!old_diff.is_empty());
411 };
412 let keys: BTreeSet<_> = self.approved.keys().chain(snapshot.keys()).collect();
413 let diff: BTreeMap<_, _> = keys
414 .into_iter()
415 .filter(|key| self.approved.get(*key) != snapshot.get(*key))
416 .map(|key| (key.clone(), snapshot.get(key).cloned()))
417 .collect();
418 let changed = old_diff != diff;
419 self.proposal = if diff.is_empty() {
420 None
421 } else {
422 Some(Proposal {
423 base_revision: self.base_revision,
424 snapshot_digest: subc_config::digest(
425 subc_config::PURPOSE_SNAPSHOT,
426 &serde_json::to_value(snapshot).map_err(super::StoreError::serialization)?,
427 )
428 .map_err(super::StoreError::serialization)?,
429 diff,
430 })
431 };
432 Ok(changed)
433 }
434}