diff --git a/SERVICE.md b/SERVICE.md index 0c67f53..0f6e037 100644 --- a/SERVICE.md +++ b/SERVICE.md @@ -24,14 +24,16 @@ and, paradoxically, could keep old objects pinned forever. - `archipelago cleanup plan [--json]` is read-only and inventories deployment roots, overlay-selected/source layer commits, Host Extension bases and active-context - plan targets. It lists service refs that look unreferenced but explicitly marks - them **retained** until a history/readiness policy exists. + plan targets. Absent optional commit metadata is tolerated (older images and local + compositions do not carry every key). It lists the refs that apply would expire. - `archipelago cleanup apply` takes the shared host mutation lock, refuses live/no-booted/ pending states, establishes keep refs for the complete protected commit graph and invokes only `ostree prune --refs-only`. It does **not** run `admin cleanup` or delete - deployments; it removes only its own `archipelago/keep/*` refs for commits no longer - named by a deployment, active plan or retained intent (those deployments were already - retired by OSTree finalize-staged retention). Service/history refs stay untouched. + deployments. It expires its own `archipelago/keep/*` refs and superseded + `service/bases|overlays|overlay-builds` refs whose commits are no longer named by a + deployment, active plan or retained intent — those deployments were already retired + by OSTree finalize-staged retention, and the refs would otherwise pin their objects + forever. `stable/*` refs, plans and generation history are untouched. The record stores protected commit identities and the before/after repo size. This safely removes objects already unreachable from every ref; it cannot yet reclaim commits kept alive by obsolete deployment/history refs. diff --git a/src/gc.rs b/src/gc.rs index c8721c1..4b53e11 100644 --- a/src/gc.rs +++ b/src/gc.rs @@ -4,11 +4,12 @@ //! so it only removes objects that were written but never committed. `--refs-only` //! collects objects unreachable from refs. This module makes deployments and the //! commits they reference explicit GC roots with `archipelago/keep/` -//! refs. It deliberately does not run `ostree admin cleanup` or delete service, -//! plan, generation or old keep refs: destructive retention requires the -//! deployment-bound readiness records mandated by D-046/D-047, which are not yet -//! implemented. The timer therefore only collects objects already unreachable from -//! every retained ref. +//! refs. It deliberately does not run `ostree admin cleanup` or delete deployments: +//! destructive retention requires the deployment-bound readiness records mandated by +//! D-046/D-047, which are not yet implemented. It only expires its own keep refs and +//! superseded service composition refs whose commits are no longer named by a +//! deployment, plan or retained intent; `stable/*`, plans and history stay. The +//! timer therefore collects objects already unreachable from every retained ref. use serde_json::Value; use sha2::{Digest, Sha256}; use std::{collections::BTreeSet, fs, io::Write, os::unix::fs::{MetadataExt, OpenOptionsExt}, path::Path, process::Command, time::{SystemTime, UNIX_EPOCH}}; @@ -115,9 +116,10 @@ fn stale_keep_refs(refs: &[(String, String)], protected: &BTreeSet) -> V .cloned().collect() } -/// Candidate service refs not directly referenced by a deployment. This is an -/// informational list only: plans/history may still refer to them, so GC must not -/// delete these refs until their durable retention policy exists. +/// Service refs whose commits are not part of any retained deployment, active plan +/// or retained overlay/extension intent. They are local composition inputs: the +/// public repository and the recorded manifests can reproduce them, so they must not +/// keep superseded images alive in the sysroot object store. pub(crate) fn stale_service_refs(refs: &[(String, String)], desired: &BTreeSet) -> Vec<(String, String)> { refs.iter().filter(|(name, _)| SERVICE_PREFIXES.iter().any(|prefix| name.starts_with(prefix))) .filter(|(_, target)| !desired.contains(target)) @@ -286,7 +288,7 @@ pub fn plan(as_json: bool) -> Result<(), String> { "keep_refs_to_create": create.iter().map(|(name, _)| name).collect::>(), "old_keep_refs_retained_pending_readiness": stale_keep.iter().map(|(name, target)| serde_json::json!({ "ref": name, "commit": target})).collect::>(), - "service_refs_retained_pending_history_policy": stale.iter().map(|(name, target)| serde_json::json!({ + "superseded_service_refs": stale.iter().map(|(name, target)| serde_json::json!({ "ref": name, "commit": target})).collect::>(), "repository_bytes": repository_bytes(), "guard": match &guard { Err(error) => Some(error.clone()), Ok(_) => None }, @@ -304,10 +306,10 @@ pub fn plan(as_json: bool) -> Result<(), String> { println!("Protected commits: {}", protected.len()); println!("Keep refs to create: {}", create.len()); if !stale_keep.is_empty() { - println!("Old keep refs retained until readiness-bound cleanup: {}", stale_keep.len()); + println!("Old keep refs to expire: {}", stale_keep.len()); } if !stale.is_empty() { - println!("Composition refs retained until history/readiness policy is available:"); + println!("Superseded composition refs to expire:"); for (name, target) in &stale { println!(" {name} {target}"); } } if let Err(error) = guard { @@ -318,10 +320,11 @@ pub fn plan(as_json: bool) -> Result<(), String> { Ok(()) } -/// Apply guarded collection of objects already unreachable from any ref. Deployment -/// retirement is success-gated by D-046/D-047, and that readiness record producer -/// is not implemented yet; therefore this command never calls `admin cleanup` or -/// removes keep/service/history refs. +/// Apply guarded object collection. It never retires deployments (that stays with +/// OSTree finalize-staged retention and D-047 readiness evidence) and never touches +/// `stable/*` refs or retained plans/intent. It expires only this service's own +/// protection refs for commits no longer named by any retained deployment, plan or +/// intent, then collects objects no ref can reach. pub fn apply() -> Result<(), String> { require_host()?; let _lock = lock()?; @@ -342,6 +345,9 @@ pub fn apply() -> Result<(), String> { for (name, _) in stale_keep_refs(&refs, &protected) { repository(&["refs", "--delete", &name])?; } + for (name, _) in stale_service_refs(&refs, &protected) { + repository(&["refs", "--delete", &name])?; + } for commit in &protected { repository(&["ls", commit, "/"])?; } @@ -415,7 +421,19 @@ fn ref_names(refs: &[(String, String)]) -> Vec { } fn metadata_commit(commit: &str, key: &str) -> Result, String> { - let raw = ostree(&["show", &format!("--print-metadata-key={key}"), commit])?; + let key_argument = format!("--print-metadata-key={key}"); + let output = Command::new("timeout") + .args(["--kill-after=15", "120", "ostree", "show", &key_argument, commit]) + .env("LC_ALL", "C").output() + .map_err(|error| format!("ostree show: {error}"))?; + if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + // Images built before the key existed, and locally composed commits, simply + // do not carry every metadata field. That is not a collection failure. + if stderr.to_ascii_lowercase().contains("no such metadata key") { return Ok(None); } + return Err(format!("ostree show {key_argument} {commit}: {}", stderr.trim())); + } + let raw = String::from_utf8(output.stdout).map_err(|error| error.to_string())?; let value = raw.trim().trim_matches('\''); if value.is_empty() { return Ok(None); } if !digest(value) { return Err(format!("invalid {key} commit metadata in {commit}")); } @@ -498,7 +516,7 @@ mod tests { } #[test] - fn service_refs_are_only_candidates_until_history_policy_exists() { + fn service_refs_outside_the_protected_graph_are_expired() { let target = "d".repeat(64); let refs = vec![ ("service/bases/".to_owned() + &target, target.clone()), @@ -512,6 +530,9 @@ mod tests { assert_eq!(stale.len(), 2); assert!(stale.iter().all(|(name, _)| name != "stable/core")); assert!(stale.iter().all(|(name, _)| !name.starts_with(&format!("{KEEP}/")))); + // A service ref that is the current base of a retained deployment survives. + assert!(stale_service_refs(&refs, &[target.clone(), "a".repeat(64), "e".repeat(64), + "f".repeat(64)].into_iter().collect()).is_empty()); } #[test]