From ec1f7c5b0b2cd87a2b06d97e871594fd608f2a4c Mon Sep 17 00:00:00 2001 From: Phoebus Date: Fri, 25 Sep 2026 11:24:53 -0700 Subject: [PATCH] fix(sys): safe cleanup previews, exact Flatpak/Snap update transactions, and correct reverse dependencies --- .../crates/phoebus-core/src/effects.rs | 138 +++ .../crates/phoebus-executor/src/actions.rs | 553 ++++++++-- .../crates/phoebus-executor/src/nix.rs | 2 +- .../crates/phoebus-executor/src/query.rs | 962 ++++++++++++++---- .../crates/phoebus-installer/src/service.rs | 56 +- 5 files changed, 1421 insertions(+), 290 deletions(-) diff --git a/installer-rs/crates/phoebus-core/src/effects.rs b/installer-rs/crates/phoebus-core/src/effects.rs index e351c96..5bbab8e 100644 --- a/installer-rs/crates/phoebus-core/src/effects.rs +++ b/installer-rs/crates/phoebus-core/src/effects.rs @@ -63,6 +63,55 @@ pub struct CleanupEffect { pub unavailable: bool, } +/// A non-Portage package source whose update is a single, reviewable +/// transaction (Flatpak or Snap). Portage updates are represented as +/// [`PackageEffect`]s from `emerge --pretend` instead. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum UpdateSource { + Flatpak, + Snap, +} + +impl UpdateSource { + pub fn as_str(self) -> &'static str { + match self { + UpdateSource::Flatpak => "flatpak", + UpdateSource::Snap => "snap", + } + } +} + +/// One exact, reviewable update transaction for a non-Portage lane. +/// +/// Execution consumes these transactions rather than re-discovering updates, +/// so a changed revision, a newly appearing update, or a different installation +/// cannot be smuggled in after review. `exact` records whether the backend can +/// actually guarantee the reviewed identity and immutable revision; when it is +/// false the source must be refused rather than executed approximately. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct UpdateTransaction { + pub source: UpdateSource, + /// The immutable backend identity: a full Flatpak ref + /// (`app/org.x/x86_64/stable`) or a snap name. + pub identity: String, + /// Human-readable version before the update, when the backend reports it. + pub previous_version: Option, + /// Human-readable version after the update. + pub next_version: String, + /// Immutable revision before the update (Flatpak commit / snap revision). + pub previous_revision: Option, + /// Immutable revision after the update. + pub next_revision: Option, + /// Installation scope (Flatpak `system`/`user`; Snap tracking channel). + pub scope: Option, + /// Material changes the update pulls in beyond the primary identity (for + /// example an updated Flatpak runtime), or a confinement note. + pub related: Vec, + /// The backend can guarantee this exact identity and immutable revision. + pub exact: bool, +} + /// A fully resolved, human-reviewable plan. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] @@ -70,6 +119,10 @@ pub struct ResolvedPlan { pub effects: Vec, /// Destructive cache-cleanup intents, each bound to its reviewed dry run. pub cleanup: Vec, + /// Exact per-source update transactions (Flatpak/Snap), bound to approval + /// and consumed by execution. Freshly discovered updates are never a + /// substitute for these. + pub updates: Vec, pub download_bytes: Option, /// A recovery point that will exist before the change (snapshot id). pub recovery_point: Option, @@ -158,6 +211,31 @@ impl ResolvedPlan { } hasher.update(b"\0"); } + // Exact update transactions (identity + immutable revision) are part of + // the approved material change. + for txn in &self.updates { + hasher.update(b"update\0"); + hasher.update(txn.source.as_str().as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.identity.as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.previous_version.as_deref().unwrap_or("").as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.next_version.as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.previous_revision.as_deref().unwrap_or("").as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.next_revision.as_deref().unwrap_or("").as_bytes()); + hasher.update(b"\0"); + hasher.update(txn.scope.as_deref().unwrap_or("").as_bytes()); + hasher.update(b"\0"); + hasher.update([txn.exact as u8]); + for related in &txn.related { + hasher.update(related.as_bytes()); + hasher.update(b"\n"); + } + hasher.update(b"\0"); + } self.digest = format!("{:x}", hasher.finalize()); } @@ -214,6 +292,33 @@ impl ResolvedPlan { } } } + for txn in &self.updates { + let revisions = match (&txn.previous_revision, &txn.next_revision) { + (Some(prev), Some(next)) => format!(" [{prev} -> {next}]"), + (None, Some(next)) => format!(" [-> {next}]"), + _ => String::new(), + }; + let scope = txn + .scope + .as_deref() + .map(|s| format!(" ({s})")) + .unwrap_or_default(); + let exact_note = if txn.exact { + "" + } else { + " — NOT EXACTLY BINDABLE" + }; + out.push_str(&format!( + " [{}] {}{scope} {}{revisions} -> {}{exact_note}\n", + txn.source.as_str(), + txn.identity, + txn.previous_version.as_deref().unwrap_or("?"), + txn.next_version, + )); + for related in &txn.related { + out.push_str(&format!(" ~ {related}\n")); + } + } if let Some(bytes) = self.download_bytes { out.push_str(&format!(" download: {} MiB\n", bytes / (1024 * 1024))); } @@ -565,6 +670,39 @@ Total: 5 packages (2 upgrades, 2 new, 1 uninstall), Size of downloads: 10,240 Ki assert!(changed_materially(&a, &unavailable)); } + #[test] + fn update_transactions_are_part_of_the_digest_and_material_change() { + let txn = UpdateTransaction { + source: UpdateSource::Flatpak, + identity: "app/org.mozilla.firefox/x86_64/stable".into(), + previous_version: Some("128".into()), + next_version: "129".into(), + previous_revision: Some("OLD".into()), + next_revision: Some("NEW".into()), + scope: Some("system".into()), + related: Vec::new(), + exact: true, + }; + let mut a = ResolvedPlan::default(); + a.updates.push(txn.clone()); + a.recompute_digest(); + + // A different immutable revision with the *same* human version must be + // a material change. + let mut b = a.clone(); + b.updates[0].next_revision = Some("OTHER".into()); + b.recompute_digest(); + assert_ne!(a.digest, b.digest); + assert!(changed_materially(&a, &b)); + assert!(!approval_holds(&a, &b, &[])); + + // A loss of exactness is also material. + let mut c = a.clone(); + c.updates[0].exact = false; + c.recompute_digest(); + assert!(changed_materially(&a, &c)); + } + #[test] fn render_states_source_binary_and_consequences() { let plan = parse_emerge_pretend(PRETEND); diff --git a/installer-rs/crates/phoebus-executor/src/actions.rs b/installer-rs/crates/phoebus-executor/src/actions.rs index 4300e74..445eb02 100644 --- a/installer-rs/crates/phoebus-executor/src/actions.rs +++ b/installer-rs/crates/phoebus-executor/src/actions.rs @@ -8,7 +8,7 @@ use std::path::Path; use phoebus_core::action::SystemAction; use phoebus_core::effects::{ changed_materially, parse_emerge_pretend, CleanupEffect, EffectKind, EffectSource, - PackageEffect, ResolvedPlan, + PackageEffect, ResolvedPlan, UpdateSource, UpdateTransaction, }; use phoebus_core::selection::{Backend, Need}; use phoebus_core::transaction::TransactionOutcome; @@ -301,10 +301,74 @@ fn flatpak_available(exec: &Exec, root: &Path) -> bool { crate::query::flatpak_available(exec, root) } -/// Exact per-application Flatpak update effects. Each affected application is -/// an identity plus its planned new version, so approval cannot authorize an -/// unrestricted update-all. -fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result> { +/// Split a Flatpak `--columns=` row (tab-separated) or whitespace fallback. +fn flatpak_cols(line: &str) -> Vec { + if line.contains('\t') { + line.split('\t').map(|s| s.trim().to_string()).collect() + } else { + line.split_whitespace().map(|s| s.to_string()).collect() + } +} + +/// Parse `flatpak remote-ls --updates` and `flatpak list` output into exact, +/// immutable-revision update transactions. +/// +/// `updates` columns: `ref,application,version,branch,commit,origin,installation,type,arch`. +/// `installed` columns: `ref,application,version,branch,commit,installation,type,arch`. +/// A transaction is `exact` only when both the ref and the target commit are +/// present; otherwise the source must be refused rather than approximated. +pub(crate) fn parse_flatpak_transactions(updates: &str, installed: &str) -> Vec { + use std::collections::BTreeMap; + // ref -> (version, commit) + let mut current: BTreeMap, Option)> = BTreeMap::new(); + for line in installed.lines() { + if line.trim().is_empty() { + continue; + } + let cols = flatpak_cols(line); + let (Some(r), version, commit) = ( + cols.first().filter(|s| !s.is_empty()).cloned(), + cols.get(2).cloned(), + cols.get(4).cloned(), + ) else { + continue; + }; + current.insert(r, (version, commit)); + } + let mut txns = Vec::new(); + for line in updates.lines() { + if line.trim().is_empty() { + continue; + } + let cols = flatpak_cols(line); + let Some(identity) = cols.first().filter(|s| !s.is_empty()).cloned() else { + continue; + }; + let next_version = cols.get(2).cloned().unwrap_or_default(); + let next_revision = cols.get(4).filter(|s| !s.is_empty()).cloned(); + let scope = cols.get(6).filter(|s| !s.is_empty()).cloned(); + let previous = current.get(&identity); + txns.push(UpdateTransaction { + source: UpdateSource::Flatpak, + identity, + previous_version: previous.and_then(|(v, _)| v.clone()), + next_version, + previous_revision: previous.and_then(|(_, c)| c.clone()), + next_revision: next_revision.clone(), + scope, + related: Vec::new(), + exact: next_revision.is_some(), + }); + } + txns.sort_by(|a, b| a.identity.cmp(&b.identity)); + txns +} + +/// Resolve the exact Flatpak update transactions for the target. +pub(crate) fn flatpak_update_transactions( + exec: &Exec, + root: &Path, +) -> Result> { if !flatpak_available(exec, root) { return Err(ExecError::operation( "sys", @@ -322,7 +386,8 @@ fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result &[ "remote-ls".into(), "--updates".into(), - "--columns=application,version,branch".into(), + "--columns=ref,application,version,branch,commit,origin,installation,type,arch" + .into(), ], )) .map_err(|e| { @@ -342,49 +407,94 @@ fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result ), )); } - let mut current = std::collections::BTreeMap::new(); - if let Ok(list) = exec.runner.execute(&target_spec( - root, - "sys", - "resolve flatpak updates", - "flatpak", - &[ - "list".into(), - "--app".into(), - "--columns=application,version".into(), - ], - )) { - if list.success { - for line in list.stdout_lossy().lines() { - let mut parts = line.split_whitespace(); - if let (Some(id), Some(version)) = (parts.next(), parts.next()) { - current.insert(id.to_string(), version.to_string()); - } - } - } + let installed = exec + .runner + .execute(&target_spec( + root, + "sys", + "resolve flatpak updates", + "flatpak", + &[ + "list".into(), + "--columns=ref,application,version,branch,commit,installation,type,arch".into(), + ], + )) + .map_err(|e| { + ExecError::operation( + "sys", + "resolve flatpak updates", + format!("flatpak list could not run: {e}"), + ) + })?; + if !installed.success { + return Err(ExecError::operation( + "sys", + "resolve flatpak updates", + format!("flatpak list failed: {}", installed.stderr_lossy().trim()), + )); } - let mut effects = Vec::new(); - for line in updates.stdout_lossy().lines() { - let mut parts = line.split_whitespace(); - let (Some(id), Some(new_version)) = (parts.next(), parts.next()) else { - continue; - }; - if id.is_empty() || new_version.is_empty() { - continue; - } - effects.push(PackageEffect { - target: format!("{id}-{new_version}"), - previous: current.get(id).cloned(), - kind: EffectKind::Upgrade, - source: EffectSource::Binary, - }); - } - Ok(effects) + Ok(parse_flatpak_transactions( + &updates.stdout_lossy(), + &installed.stdout_lossy(), + )) } -/// Exact per-snap update effects. Snaps act on the running daemon, so an -/// alternate root is refused before any command is issued. -fn snap_update_effects(exec: &Exec, root: &Path) -> Result> { +/// Parse `snap refresh --list` and `snap list` output into exact update +/// transactions. Columns: `Name Version Rev Tracking Publisher Notes`; the +/// revision is the immutable identity and the tracking channel is the scope. +pub(crate) fn parse_snap_transactions(refresh: &str, installed: &str) -> Vec { + use std::collections::BTreeMap; + // name -> (version, revision, tracking, notes) + let mut current: BTreeMap, Option, Option, String)> = + BTreeMap::new(); + for line in installed.lines().skip(1) { + let cols: Vec<&str> = line.split_whitespace().collect(); + if cols.len() < 4 || cols[0].is_empty() { + continue; + } + current.insert( + cols[0].to_string(), + ( + Some(cols[1].to_string()), + Some(cols[2].to_string()), + Some(cols[3].to_string()), + cols.get(5..).map(|n| n.join(" ")).unwrap_or_default(), + ), + ); + } + let mut txns = Vec::new(); + for line in refresh.lines().skip(1) { + let cols: Vec<&str> = line.split_whitespace().collect(); + if cols.len() < 3 || cols[0].is_empty() { + continue; + } + let name = cols[0].to_string(); + let previous = current.get(&name); + let mut related = Vec::new(); + if let Some((_, _, _, notes)) = previous { + if !notes.trim().is_empty() { + related.push(format!("notes: {notes}")); + } + } + txns.push(UpdateTransaction { + source: UpdateSource::Snap, + identity: name, + previous_version: previous.and_then(|(v, _, _, _)| v.clone()), + next_version: cols[1].to_string(), + previous_revision: previous.and_then(|(_, r, _, _)| r.clone()), + next_revision: Some(cols[2].to_string()), + scope: Some(cols[3].to_string()), + related, + exact: !cols[2].is_empty(), + }); + } + txns.sort_by(|a, b| a.identity.cmp(&b.identity)); + txns +} + +/// Resolve the exact Snap update transactions. Snaps act on the running +/// daemon, so an alternate root is refused before any command is issued. +pub(crate) fn snap_update_transactions(exec: &Exec, root: &Path) -> Result> { if !is_host_root(root) { return Err(ExecError::operation( "sys", @@ -401,42 +511,45 @@ fn snap_update_effects(exec: &Exec, root: &Path) -> Result> { "snapd is not installed or active", )); } - let updates = exec.runner.execute( + let refresh = exec.runner.execute( &CommandSpec::new("sys", "resolve snap updates", "snap").args(["refresh", "--list"]), )?; - if !updates.success { + if !refresh.success { return Err(ExecError::operation( "sys", "resolve snap updates", format!( "snap refresh --list failed: {}", - updates.stderr_lossy().trim() + refresh.stderr_lossy().trim() ), )); } - let mut current = std::collections::BTreeMap::new(); - if let Ok(entries) = snap::list(exec) { - for entry in entries { - current.insert(entry.name, entry.version); - } + let installed = exec + .runner + .execute(&CommandSpec::new("sys", "resolve snap updates", "snap").args(["list"]))?; + if !installed.success { + return Err(ExecError::operation( + "sys", + "resolve snap updates", + format!("snap list failed: {}", installed.stderr_lossy().trim()), + )); } - let mut effects = Vec::new(); - for line in updates.stdout_lossy().lines().skip(1) { - let mut parts = line.split_whitespace(); - let (Some(name), Some(new_version)) = (parts.next(), parts.next()) else { - continue; - }; - if name.is_empty() || new_version.is_empty() { - continue; - } - effects.push(PackageEffect { - target: format!("{name}-{new_version}"), - previous: current.get(name).cloned(), + Ok(parse_snap_transactions( + &refresh.stdout_lossy(), + &installed.stdout_lossy(), + )) +} + +/// The human-readable `PackageEffect` summary for a set of update transactions. +fn update_effect_summary(txns: &[UpdateTransaction]) -> Vec { + txns.iter() + .map(|txn| PackageEffect { + target: format!("{}-{}", txn.identity, txn.next_version), + previous: txn.previous_version.clone(), kind: EffectKind::Upgrade, source: EffectSource::Binary, - }); - } - Ok(effects) + }) + .collect() } /// Resolve a reviewable, per-source update plan. Each lane contributes exact @@ -489,16 +602,22 @@ fn resolve_update_effects( } plan.consequences.extend(parsed.consequences); } else if lane == Backend::Flatpak { - match flatpak_update_effects(exec, root) { - Ok(effects) => plan.effects.extend(effects), + match flatpak_update_transactions(exec, root) { + Ok(txns) => { + plan.effects.extend(update_effect_summary(&txns)); + plan.updates.extend(txns); + } Err(e) if backend.is_none() => plan .consequences .push(format!("[flatpak] updates could not be resolved: {e}")), Err(e) => return Err(e), } } else if lane == Backend::Snap { - match snap_update_effects(exec, root) { - Ok(effects) => plan.effects.extend(effects), + match snap_update_transactions(exec, root) { + Ok(txns) => { + plan.effects.extend(update_effect_summary(&txns)); + plan.updates.extend(txns); + } Err(e) if backend.is_none() => plan .consequences .push(format!("[snap] updates could not be resolved: {e}")), @@ -534,18 +653,47 @@ fn resolve_update_effects( Ok(plan) } -/// The cache cleaners for a lane, as `(cleaner, dry-run args, destructive args)`. +/// The cache cleaners for a lane, as `(cleaner, preview args, destructive args)`. +/// +/// `eclean-dist`/`eclean-pkg` **delete by default**; only `--pretend` previews +/// (verified against the upstream `gentoolkit` CLI). The preview therefore +/// passes `--pretend` explicitly, and both the preview and the destructive run +/// use the *same* selection policy (`--deep`), so what is reviewed is exactly +/// what is removed. pub(crate) fn portage_cleaners(root: &Path) -> Vec<(&'static str, Vec, Vec)> { let mut cleaners = Vec::new(); - if crate::exec::tool_in_target(root, "eclean-dist") { - cleaners.push(("eclean-dist", vec![], vec!["-d".to_string()])); - } - if crate::exec::tool_in_target(root, "eclean-pkg") { - cleaners.push(("eclean-pkg", vec![], vec!["-d".to_string()])); + for cleaner in ["eclean-dist", "eclean-pkg"] { + if crate::exec::tool_in_target(root, cleaner) { + cleaners.push(( + cleaner, + vec!["--pretend".to_string(), "--deep".to_string()], + vec!["--deep".to_string()], + )); + } } cleaners } +/// Collect the items a cleaner reported, from *both* streams. +/// +/// The upstream tools do not guarantee which stream the reviewable list lands +/// on, so stdout and stderr are combined; only non-empty, trimmed lines become +/// bound items. +fn cleanup_items(output: &CommandOutput) -> Vec { + let mut text = output.stdout_lossy(); + if !output.stderr.is_empty() { + if !text.is_empty() && !text.ends_with('\n') { + text.push('\n'); + } + text.push_str(&output.stderr_lossy()); + } + text.lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + .map(str::to_string) + .collect() +} + /// Resolve a reviewable cache-cleanup plan from non-destructive dry runs. A /// cleaner that cannot run is recorded as unavailable, distinct from one that /// ran and found nothing. @@ -564,26 +712,19 @@ fn resolve_cleanup_effects( }; for lane in lanes { if lane.is_portage() { - for (cleaner, dry_args, _) in portage_cleaners(root) { + for (cleaner, preview_args, _) in portage_cleaners(root) { let out = exec.runner.execute(&target_spec( root, "sys", "resolve cache cleanup", cleaner, - &dry_args, + &preview_args, )); match out { Ok(output) if output.success => { - let items = output - .stdout_lossy() - .lines() - .map(str::trim) - .filter(|l| !l.is_empty()) - .map(str::to_string) - .collect(); plan.cleanup.push(CleanupEffect { cleaner: cleaner.to_string(), - items, + items: cleanup_items(&output), unavailable: false, }); } @@ -591,14 +732,14 @@ fn resolve_cleanup_effects( return Err(ExecError::operation( "sys", "resolve cache cleanup", - format!("{cleaner} dry run failed: {}", output.stderr_lossy().trim()), + format!("{cleaner} preview failed: {}", output.stderr_lossy().trim()), )); } Err(e) => { return Err(ExecError::operation( "sys", "resolve cache cleanup", - format!("{cleaner} dry run could not run: {e}"), + format!("{cleaner} preview could not run: {e}"), )); } } @@ -611,25 +752,21 @@ fn resolve_cleanup_effects( unavailable: true, }); } else { + // `nix-store --gc` deletes by default; the documented, + // non-destructive preview is `--print-dead` (the `--dry-run` + // global is not documented for the `--gc` operation). let out = exec.runner.execute(&target_spec( root, "sys", "resolve cache cleanup", "/nix/var/nix/profiles/default/bin/nix-store", - &["--gc".into(), "--dry-run".into()], + &["--gc".into(), "--print-dead".into()], )); match out { Ok(output) if output.success => { - let items = output - .stdout_lossy() - .lines() - .map(str::trim) - .filter(|l| !l.is_empty()) - .map(str::to_string) - .collect(); plan.cleanup.push(CleanupEffect { cleaner: "nix-store --gc".into(), - items, + items: cleanup_items(&output), unavailable: false, }); } @@ -637,14 +774,17 @@ fn resolve_cleanup_effects( return Err(ExecError::operation( "sys", "resolve cache cleanup", - format!("nix-store --gc failed: {}", output.stderr_lossy().trim()), + format!( + "nix-store --gc preview failed: {}", + output.stderr_lossy().trim() + ), )); } Err(e) => { return Err(ExecError::operation( "sys", "resolve cache cleanup", - format!("nix-store --gc could not run: {e}"), + format!("nix-store --gc preview could not run: {e}"), )); } } @@ -677,7 +817,7 @@ pub fn execute_checked( root: &Path, approved: Option<&ResolvedPlan>, ) -> Result { - if let Some(approved) = approved { + let current = if let Some(approved) = approved { let current = resolve_effects(exec, action, root)?; if changed_materially(approved, ¤t) { return Err(ExecError::operation( @@ -691,8 +831,11 @@ pub fn execute_checked( ), )); } - } - execute(exec, action, auth, root) + Some(current) + } else { + None + }; + execute_inner(exec, action, auth, root, current.as_ref()) } /// The result of running a reviewed multi-action plan. @@ -807,6 +950,21 @@ pub fn execute( action: &SystemAction, auth: Option<&DestructiveAuthorization>, root: &Path, +) -> Result { + execute_inner(exec, action, auth, root, None) +} + +/// Execute an action, optionally carrying the reviewed plan through to +/// execution. Actions whose mutation must equal the reviewed transaction +/// (Flatpak/Snap updates, cache cleanup) consume `resolved` instead of +/// re-discovering state; without it they refuse the mutation rather than +/// presenting approximate approval as exact. +pub fn execute_inner( + exec: &Exec, + action: &SystemAction, + auth: Option<&DestructiveAuthorization>, + root: &Path, + resolved: Option<&ResolvedPlan>, ) -> Result { // The execution boundary validates every action regardless of its source // (manual phrase, JSON, or AI plan), so an invalid value can never reach a @@ -1290,7 +1448,7 @@ pub fn execute( crate::query::check_updates(exec, root, *backend).render() } SystemAction::ApplyUpdates { backend } => { - crate::query::apply_updates(exec, root, *backend)? + crate::query::apply_updates(exec, root, *backend, resolved)? } SystemAction::ListPackages { backend, filter } => { crate::query::inventory(exec, root, *backend, filter.as_deref()).render() @@ -1303,7 +1461,9 @@ pub fn execute( crate::query::package_dependencies(exec, root, package, *reverse)? } SystemAction::Depclean => crate::query::depclean(exec, root)?, - SystemAction::CleanCache { backend } => crate::query::clean_cache(exec, root, *backend)?, + SystemAction::CleanCache { backend } => { + crate::query::clean_cache(exec, root, *backend, resolved)? + } SystemAction::CreateSnapshot { operation } => { let auth = auth.ok_or_else(|| ExecError::NotAuthorized(action.describe()))?; let source = root.to_string_lossy().to_string(); @@ -1960,6 +2120,185 @@ Total: 1 package (1 new), Size of downloads: 512 KiB assert_eq!(runner.recorded().len(), 1, "{:?}", runner.recorded()); } + fn install_fake_tool(root: &Path, name: &str) { + use std::os::unix::fs::PermissionsExt; + let bin = root.join("usr/bin"); + std::fs::create_dir_all(&bin).unwrap(); + let tool = bin.join(name); + std::fs::write(&tool, "#!/bin/sh\n").unwrap(); + std::fs::set_permissions(&tool, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + + #[test] + fn cleaner_preview_is_non_destructive_and_matches_execution_policy() { + let root = tempfile::tempdir().unwrap(); + install_fake_tool(root.path(), "eclean-dist"); + let cleaners = portage_cleaners(root.path()); + assert_eq!(cleaners.len(), 1); + let (_, preview, destructive) = &cleaners[0]; + // `eclean` deletes by default: the preview must be explicit. + assert!(preview.iter().any(|a| a == "--pretend"), "{preview:?}"); + assert!( + !destructive.iter().any(|a| a == "--pretend"), + "the destructive run must not pretend: {destructive:?}" + ); + // Same selection policy: the only difference is the preview flag. + let mut selection = preview.clone(); + selection.retain(|a| a != "--pretend"); + assert_eq!( + &selection, destructive, + "preview policy must match execution" + ); + } + + #[test] + fn resolve_cleanup_effects_never_runs_a_destructive_cleaner() { + let runner = RecordingRunner::new(); + runner.set_response( + "chroot", + CommandOutput { + status: 0, + success: true, + stdout: b"/var/cache/distfiles/old.tar.gz\n".to_vec(), + stderr: b"Here are the distfiles that would be deleted:\n".to_vec(), + duration: std::time::Duration::from_millis(1), + }, + ); + let exec = make_exec(runner.clone()); + let root = Path::new(&exec.root); + install_fake_tool(root, "eclean-dist"); + let plan = resolve_effects( + &exec, + &SystemAction::CleanCache { + backend: Some(Backend::PortageSource), + }, + root, + ) + .unwrap(); + // Items are taken from both streams and bound to the plan. + let items = &plan.cleanup[0].items; + assert!(items.iter().any(|i| i.contains("old.tar.gz")), "{items:?}"); + assert!( + items.iter().any(|i| i.contains("would be deleted")), + "{items:?}" + ); + // Every invocation was a preview; no destructive `eclean` ran. + for spec in runner.recorded() { + assert!( + spec.args.iter().any(|a| a == "--pretend"), + "planning must only preview: {spec:?}" + ); + } + } + + #[test] + fn clean_cache_revalidation_only_previews_and_aborts_on_drift() { + // Rank 1: reviewed preview (items X). + let approved_runner = RecordingRunner::new(); + approved_runner.set_response( + "chroot", + response("Here are the distfiles that would be deleted:\n/var/cache/old-a.tar.gz\n"), + ); + let approved_exec = make_exec(approved_runner); + let approved_root = Path::new(&approved_exec.root); + install_fake_tool(approved_root, "eclean-dist"); + let approved = resolve_effects( + &approved_exec, + &SystemAction::CleanCache { + backend: Some(Backend::PortageSource), + }, + approved_root, + ) + .unwrap(); + + // Execution-time re-resolution previews a *different* item set. + let runner = RecordingRunner::new(); + runner.set_response( + "chroot", + response("Here are the distfiles that would be deleted:\n/var/cache/old-b.tar.gz\n"), + ); + let exec = make_exec(runner.clone()); + let root = Path::new(&exec.root); + install_fake_tool(root, "eclean-dist"); + let err = execute_checked( + &exec, + &SystemAction::CleanCache { + backend: Some(Backend::PortageSource), + }, + Some(&auth()), + root, + Some(&approved), + ) + .unwrap_err(); + assert!(err.to_string().contains("plan changed"), "{err}"); + // Only the non-destructive preview ran; the destructive `--deep`-only + // invocation never did. + for spec in runner.recorded() { + assert!( + spec.args.iter().any(|a| a == "--pretend"), + "a destructive cleaner ran during revalidation: {spec:?}" + ); + } + } + + #[test] + fn clean_cache_without_authorization_runs_no_cleaner() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + let root = Path::new(&exec.root); + install_fake_tool(root, "eclean-dist"); + let err = execute( + &exec, + &SystemAction::CleanCache { + backend: Some(Backend::PortageSource), + }, + None, + root, + ) + .unwrap_err(); + assert!(matches!(err, ExecError::NotAuthorized(_))); + // A cancelled/unauthorized approval must not touch the cache. + assert!(runner.recorded().is_empty(), "{:?}", runner.recorded()); + } + + #[test] + fn nix_cleanup_preview_uses_the_documented_print_dead_and_not_gc() { + let runner = RecordingRunner::new(); + runner.set_response( + "chroot", + CommandOutput { + status: 0, + success: true, + stdout: b"/nix/store/dead-path-1\n".to_vec(), + stderr: Vec::new(), + duration: std::time::Duration::from_millis(1), + }, + ); + let exec = make_exec(runner.clone()); + let root = Path::new(&exec.root); + // Pretend Nix is installed by creating the pinned store path. + std::fs::create_dir_all(root.join(format!("nix/store/{}", crate::nix::NIX_STORE_NAME))) + .unwrap(); + let plan = resolve_effects( + &exec, + &SystemAction::CleanCache { + backend: Some(Backend::Nix), + }, + root, + ) + .unwrap(); + assert_eq!(plan.cleanup[0].cleaner, "nix-store --gc"); + assert!(!runner.recorded().is_empty()); + for spec in runner.recorded() { + let has_gc = spec.args.iter().any(|a| a == "--gc"); + let has_print_dead = spec.args.iter().any(|a| a == "--print-dead"); + assert!( + !has_gc || has_print_dead, + "a bare `--gc` deletes; preview must use --print-dead: {spec:?}" + ); + } + } + #[test] fn portage_apply_updates_previews_the_world_plan() { let runner = RecordingRunner::new(); diff --git a/installer-rs/crates/phoebus-executor/src/nix.rs b/installer-rs/crates/phoebus-executor/src/nix.rs index 0323426..550350c 100644 --- a/installer-rs/crates/phoebus-executor/src/nix.rs +++ b/installer-rs/crates/phoebus-executor/src/nix.rs @@ -19,7 +19,7 @@ pub const NIX_URL: &str = "https://releases.nixos.org/nix/nix-2.24.9/nix-2.24.9-x86_64-linux.tar.xz"; pub const NIX_SHA256: &str = "3c0779e4878d1289cf3fbb158ec5ea9bdf61dfb9b4efac6b3b0b6bec5ba4cf13"; const NIX_TARBALL_DIR: &str = "nix-2.24.9-x86_64-linux"; -const NIX_STORE_NAME: &str = "2nhrwv91g6ycpyxvhmvc0xs8p92wp4bk-nix-2.24.9"; +pub(crate) const NIX_STORE_NAME: &str = "2nhrwv91g6ycpyxvhmvc0xs8p92wp4bk-nix-2.24.9"; const NIX_BUILD_USERS: u32 = 8; fn nix_store_bin() -> String { diff --git a/installer-rs/crates/phoebus-executor/src/query.rs b/installer-rs/crates/phoebus-executor/src/query.rs index 48b9992..dcc529f 100644 --- a/installer-rs/crates/phoebus-executor/src/query.rs +++ b/installer-rs/crates/phoebus-executor/src/query.rs @@ -11,6 +11,7 @@ use std::path::Path; use phoebus_core::catalog::{ CatalogEntry, CatalogReport, PackageInfoMatch, PackageInfoReport, SourceReport, }; +use phoebus_core::effects::{ResolvedPlan, UpdateSource, UpdateTransaction}; use phoebus_core::selection::Backend; use phoebus_core::update::UnifiedUpdateOverview; @@ -812,8 +813,16 @@ pub fn package_dependencies( let mut lines: Vec = Vec::new(); if reverse { for record in &installed { + // A genuine self-reference is a package declaring a dependency on + // *itself*; it is not a reverse dependency of any other package. + // The dependent is `record`, so compare `record.atom` with the + // queried package — not the dependency target, which is exactly + // what must match. + if self_atom.as_deref() == Some(record.atom.as_str()) { + continue; + } for dep in declared_dependencies(record) { - if atom_matches(&dep.atom, package) && Some(&dep.atom) != self_atom.as_ref() { + if atom_matches(&dep.atom, package) { let conditional = dep .condition .as_deref() @@ -943,14 +952,30 @@ pub fn depclean(exec: &Exec, root: &Path) -> Result { )) } +/// The destructive arguments for a named Portage cleaner, if it is present. +fn destructive_args_for(root: &Path, cleaner: &str) -> Option> { + crate::actions::portage_cleaners(root) + .into_iter() + .find(|(name, _, _)| *name == cleaner) + .map(|(_, _, destructive)| destructive) +} + /// Remove unreferenced package caches / store paths. Never removes an /// installed package. /// -/// Every cleaner's outcome is preserved: a cleaner that ran and failed makes -/// the whole action fail (with the partial successes still reported), and a -/// missing cleaner is distinguished from one that ran and found nothing. A -/// cleanup that could not run at all is a failure, not a silent success. -pub fn clean_cache(exec: &Exec, root: &Path, filter: Option) -> Result { +/// When a reviewed plan is supplied, **only the cleaners and item sets that +/// were reviewed are executed**; the destructive invocation uses the same +/// selection policy as the preview. Every cleaner's outcome is preserved: a +/// cleaner that ran and failed makes the whole action fail (with partial +/// successes still reported), and a missing cleaner is distinguished from one +/// that ran and found nothing. A cleanup that could not run at all is a +/// failure, not a silent success. +pub fn clean_cache( + exec: &Exec, + root: &Path, + filter: Option, + approved: Option<&phoebus_core::effects::ResolvedPlan>, +) -> Result { let lanes: Vec = match filter { Some(backend) => vec![backend], None => vec![Backend::PortageSource, Backend::Nix], @@ -962,18 +987,33 @@ pub fn clean_cache(exec: &Exec, root: &Path, filter: Option) -> Result< for lane in lanes { if lane.is_portage() { + // The reviewed cleaner set, when present, is the only thing that may + // run. Otherwise fall back to every cleaner present in the target. + let reviewed: Vec = match approved { + Some(plan) => plan + .cleanup + .iter() + .filter(|c| !c.unavailable) + .map(|c| c.cleaner.clone()) + .collect(), + None => crate::actions::portage_cleaners(root) + .into_iter() + .map(|(name, _, _)| name.to_string()) + .collect(), + }; let mut attempted = false; - for (cleaner, _, destructive_args) in crate::actions::portage_cleaners(root) { + for cleaner in reviewed { + let Some(args) = destructive_args_for(root, &cleaner) else { + failed = true; + report.push_str(&format!( + " [portage] {cleaner} failed: the reviewed cleaner is no longer \ + available in the target\n" + )); + continue; + }; attempted = true; - match run_mut( - exec, - root, - cleaner, - &destructive_args - .iter() - .map(|s| s.as_str()) - .collect::>(), - ) { + let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); + match run_mut(exec, root, &cleaner, &refs) { Ok(()) => { completed += 1; report.push_str(&format!(" [portage] {cleaner} completed\n")); @@ -991,10 +1031,20 @@ pub fn clean_cache(exec: &Exec, root: &Path, filter: Option) -> Result< ); } } else if lane == Backend::Nix { - if !nix::installed(root) { - report.push_str(" [nix] unavailable: Nix is not installed\n"); - } else { - match nix::gc(exec, root) { + let reviewed = match approved { + Some(plan) => plan + .cleanup + .iter() + .find(|c| c.cleaner == "nix-store --gc") + .map(|c| !c.unavailable), + None => Some(nix::installed(root)), + }; + match reviewed { + None => report.push_str(" [nix] not executed: not part of the reviewed plan\n"), + Some(false) => { + report.push_str(" [nix] unavailable: Nix is not installed\n"); + } + Some(true) => match nix::gc(exec, root) { Ok(()) => { completed += 1; report.push_str( @@ -1005,7 +1055,7 @@ pub fn clean_cache(exec: &Exec, root: &Path, filter: Option) -> Result< failed = true; report.push_str(&format!(" [nix] cleanup failed: {e}\n")); } - } + }, } } else { return Err(ExecError::operation( @@ -1043,76 +1093,156 @@ pub fn check_updates(exec: &Exec, root: &Path, filter: Option) -> Unifi crate::update::check_unified_updates_filtered(exec, root, filter) } -/// The exact Flatpak applications with an available update. Each is updated by -/// ref so execution can never become an unrestricted update-all. -fn flatpak_update_ids(exec: &Exec, root: &Path) -> Result> { - let out = capture( - exec, - root, - "flatpak", - &[ - "remote-ls", - "--updates", - "--columns=application,version,branch", - ], - )?; - if !out.success { +/// Whether the reviewed plan recorded that a source could not be resolved. +fn source_resolution_failed(approved: &ResolvedPlan, tag: &str) -> bool { + approved + .consequences + .iter() + .any(|c| c.starts_with(&format!("[{tag}] updates could not be resolved"))) +} + +/// Revalidate an approved Flatpak transaction immediately before mutation. +/// The freshly resolved set must equal the approved set exactly (identity, +/// immutable commit, scope): a changed commit with an unchanged human version, +/// a newly appeared update, or a vanished one all fail. +fn revalidate_flatpak(exec: &Exec, root: &Path, approved: &[UpdateTransaction]) -> Result<()> { + let current = crate::actions::flatpak_update_transactions(exec, root)?; + if current != approved { return Err(ExecError::operation( "sys", "apply flatpak updates", format!( - "flatpak remote-ls failed: {}", - stderr_tail(&out.stderr_lossy()) + "the reviewed Flatpak transaction changed before execution ({} reviewed, {} now); \ + not applying it", + approved.len(), + current.len() ), )); } - let mut ids = Vec::new(); - for line in out.stdout_lossy().lines() { - if let Some(id) = line.split_whitespace().next() { - if !id.is_empty() { - ids.push(id.to_string()); - } - } - } - ids.sort(); - ids.dedup(); - Ok(ids) + Ok(()) } -/// The exact snaps with an available update. -fn snap_update_names(exec: &Exec) -> Result> { - let out = exec.runner.execute( - &CommandSpec::new("sys", "apply snap updates", "snap").args(["refresh", "--list"]), - )?; - if !out.success { +/// Revalidate an approved Snap transaction immediately before mutation. +fn revalidate_snap(exec: &Exec, root: &Path, approved: &[UpdateTransaction]) -> Result<()> { + let current = crate::actions::snap_update_transactions(exec, root)?; + if current != approved { return Err(ExecError::operation( "sys", "apply snap updates", format!( - "snap refresh --list failed: {}", - stderr_tail(&out.stderr_lossy()) + "the reviewed Snap transaction changed before execution ({} reviewed, {} now); \ + not applying it", + approved.len(), + current.len() ), )); } - let mut names = Vec::new(); - for line in out.stdout_lossy().lines().skip(1) { - if let Some(name) = line.split_whitespace().next() { - if !name.is_empty() { - names.push(name.to_string()); + Ok(()) +} + +/// Installed Flatpak commits keyed by ref. +fn installed_flatpak_commits( + exec: &Exec, + root: &Path, +) -> Result> { + let out = capture(exec, root, "flatpak", &["list", "--columns=ref,commit"])?; + if !out.success { + return Err(ExecError::operation( + "sys", + "verify flatpak updates", + format!("flatpak list failed: {}", stderr_tail(&out.stderr_lossy())), + )); + } + let mut map = std::collections::BTreeMap::new(); + for line in out.stdout_lossy().lines() { + if let Some((r, commit)) = line.split_once('\t').or_else(|| line.split_once(' ')) { + if !r.trim().is_empty() && !commit.trim().is_empty() { + map.insert(r.trim().to_string(), commit.trim().to_string()); } } } - names.sort(); - names.dedup(); - Ok(names) + Ok(map) +} + +fn execute_flatpak(exec: &Exec, root: &Path, txns: &[UpdateTransaction]) -> Result { + for txn in txns { + let mut args = vec!["update".to_string(), "-y".to_string()]; + if let Some(scope) = &txn.scope { + args.push(format!("--installation={scope}")); + } + args.push("--".to_string()); + args.push(txn.identity.clone()); + let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); + run_mut(exec, root, "flatpak", &refs)?; + } + // Post-execution verification: every reviewed ref must now be at the + // reviewed immutable commit. + let commits = installed_flatpak_commits(exec, root)?; + for txn in txns { + let actual = commits.get(&txn.identity); + if txn.next_revision.is_some() && actual != txn.next_revision.as_ref() { + return Err(ExecError::operation( + "sys", + "verify flatpak updates", + format!( + "{} did not reach the reviewed commit {} (now {:?})", + txn.identity, + txn.next_revision.as_deref().unwrap_or("?"), + actual + ), + )); + } + } + Ok(format!( + " [flatpak] updated {} reviewed ref(s) to their reviewed commits\n", + txns.len() + )) +} + +/// Installed Snap revisions keyed by name. +fn installed_snap_revisions(exec: &Exec) -> Result> { + let entries = snap::list(exec)?; + Ok(entries.into_iter().map(|e| (e.name, e.revision)).collect()) +} + +fn execute_snap(exec: &Exec, txns: &[UpdateTransaction]) -> Result { + for txn in txns { + snap::refresh(exec, Some(txn.identity.as_str()))?; + } + let revisions = installed_snap_revisions(exec)?; + for txn in txns { + let actual = revisions.get(&txn.identity); + if txn.next_revision.is_some() && actual != txn.next_revision.as_ref() { + return Err(ExecError::operation( + "sys", + "verify snap updates", + format!( + "{} did not reach the reviewed revision {} (now {:?})", + txn.identity, + txn.next_revision.as_deref().unwrap_or("?"), + actual + ), + )); + } + } + Ok(format!( + " [snap] refreshed {} reviewed snap(s) to their reviewed revisions\n", + txns.len() + )) } /// Apply updates across the supported lanes and report a truthful per-source -/// outcome. Each per-source update is constrained to the exact identities -/// resolved immediately before execution; a source that ran and failed is -/// reported as failed (with partial success preserved), and the whole action -/// then fails. Sources with no safe system-level transaction are refused. -pub fn apply_updates(exec: &Exec, root: &Path, filter: Option) -> Result { +/// outcome. Flatpak and Snap execute **exactly the reviewed transactions** +/// carried in `approved` (after revalidating each source immediately before its +/// mutation); freshly discovered updates are never used as replacement +/// approval. A source that ran and failed is reported as failed with partial +/// successes preserved, and the whole action then fails. +pub fn apply_updates( + exec: &Exec, + root: &Path, + filter: Option, + approved: Option<&ResolvedPlan>, +) -> Result { let lanes: Vec = match filter { Some(backend) => vec![backend], None => vec![Backend::PortageSource, Backend::Flatpak, Backend::Snap], @@ -1135,43 +1265,55 @@ pub fn apply_updates(exec: &Exec, root: &Path, filter: Option) -> Resul } } } else if lane == Backend::Flatpak { - if !flatpak_available(exec, root) { - if filter.is_some() { - failed = true; + let Some(plan) = approved else { + failed = true; + report.push_str( + " [flatpak] refused: no reviewed transaction; refusing to discover unreviewed\n", + ); + continue; + }; + let txns: Vec = plan + .updates + .iter() + .filter(|t| t.source == UpdateSource::Flatpak) + .cloned() + .collect(); + if txns.is_empty() { + if source_resolution_failed(plan, "flatpak") { report.push_str( - " [flatpak] failed: the Flatpak client is not available in the target\n", + " [flatpak] not executed: resolution failed at review; not re-discovered\n", ); } else { - report.push_str(" [flatpak] unavailable: the Flatpak client is not present\n"); + completed += 1; + report.push_str(" [flatpak] no updates were reviewed\n"); } continue; } - match flatpak_update_ids(exec, root) { - Ok(ids) if ids.is_empty() => { + if !flatpak_available(exec, root) { + failed = true; + report.push_str( + " [flatpak] failed: the reviewed transaction cannot run; the client is not \ + available in the target\n", + ); + continue; + } + if !txns.iter().all(|t| t.exact) { + failed = true; + report.push_str( + " [flatpak] refused: the reviewed transaction is not exactly bindable \ + (a ref or commit is missing)\n", + ); + continue; + } + if let Err(e) = revalidate_flatpak(exec, root, &txns) { + failed = true; + report.push_str(&format!(" [flatpak] failed: {e}\n")); + continue; + } + match execute_flatpak(exec, root, &txns) { + Ok(note) => { completed += 1; - report.push_str(" [flatpak] no updates available\n"); - } - Ok(ids) => { - let mut lane_failed = false; - for id in &ids { - match run_mut(exec, root, "flatpak", &["update", "-y", "--", id]) { - Ok(()) => {} - Err(e) => { - lane_failed = true; - report.push_str(&format!(" [flatpak] {id} failed: {e}\n")); - } - } - } - if lane_failed { - failed = true; - } else { - completed += 1; - report.push_str(&format!( - " [flatpak] updated {} reviewed application(s): {}\n", - ids.len(), - ids.join(", ") - )); - } + report.push_str(¬e); } Err(e) => { failed = true; @@ -1185,42 +1327,56 @@ pub fn apply_updates(exec: &Exec, root: &Path, filter: Option) -> Resul .push_str(" [snap] refused: Snap cannot be redirected to an alternate root\n"); continue; } - let usable = matches!(snap::status(exec), Ok(s) if s.usable()); - if !usable { - if filter.is_some() { - failed = true; - report.push_str(" [snap] failed: snapd is not installed or active\n"); + let Some(plan) = approved else { + failed = true; + report.push_str( + " [snap] refused: no reviewed transaction; refusing to discover unreviewed\n", + ); + continue; + }; + let txns: Vec = plan + .updates + .iter() + .filter(|t| t.source == UpdateSource::Snap) + .cloned() + .collect(); + if txns.is_empty() { + if source_resolution_failed(plan, "snap") { + report.push_str( + " [snap] not executed: resolution failed at review; not re-discovered\n", + ); } else { - report.push_str(" [snap] unavailable: snapd is not installed or active\n"); + completed += 1; + report.push_str(" [snap] no updates were reviewed\n"); } continue; } - match snap_update_names(exec) { - Ok(names) if names.is_empty() => { + let usable = matches!(snap::status(exec), Ok(s) if s.usable()); + if !usable { + failed = true; + report.push_str( + " [snap] failed: the reviewed transaction cannot run; snapd is not installed \ + or active\n", + ); + continue; + } + if !txns.iter().all(|t| t.exact) { + failed = true; + report.push_str( + " [snap] refused: the reviewed transaction is not exactly bindable \ + (a revision is missing)\n", + ); + continue; + } + if let Err(e) = revalidate_snap(exec, root, &txns) { + failed = true; + report.push_str(&format!(" [snap] failed: {e}\n")); + continue; + } + match execute_snap(exec, &txns) { + Ok(note) => { completed += 1; - report.push_str(" [snap] no updates available\n"); - } - Ok(names) => { - let mut lane_failed = false; - for name in &names { - match snap::refresh(exec, Some(name)) { - Ok(_) => {} - Err(e) => { - lane_failed = true; - report.push_str(&format!(" [snap] {name} failed: {e}\n")); - } - } - } - if lane_failed { - failed = true; - } else { - completed += 1; - report.push_str(&format!( - " [snap] refreshed {} reviewed snap(s): {}\n", - names.len(), - names.join(", ") - )); - } + report.push_str(¬e); } Err(e) => { failed = true; @@ -1491,8 +1647,13 @@ mod tests { // A temp dir with neither eclean-dist nor eclean-pkg in the target. let runner = RecordingRunner::new(); let exec = make_exec(runner); - let err = - clean_cache(&exec, Path::new(&exec.root), Some(Backend::PortageSource)).unwrap_err(); + let err = clean_cache( + &exec, + Path::new(&exec.root), + Some(Backend::PortageSource), + None, + ) + .unwrap_err(); let text = err.to_string(); assert!(text.contains("no cleaner ran"), "{text}"); // The unavailable tool is named distinctly from a failed tool. @@ -1545,7 +1706,7 @@ mod tests { root, logger_path, ); - let err = clean_cache(&exec2, root, Some(Backend::PortageSource)).unwrap_err(); + let err = clean_cache(&exec2, root, Some(Backend::PortageSource), None).unwrap_err(); let text = err.to_string(); assert!(text.contains("eclean-dist completed"), "{text}"); assert!(text.contains("eclean-pkg failed"), "{text}"); @@ -1557,7 +1718,8 @@ mod tests { fn apply_updates_refuses_a_lane_without_a_safe_transaction() { let runner = RecordingRunner::new(); let exec = make_exec(runner); - let err = apply_updates(&exec, Path::new(&exec.root), Some(Backend::AppImage)).unwrap_err(); + let err = + apply_updates(&exec, Path::new(&exec.root), Some(Backend::AppImage), None).unwrap_err(); let text = err.to_string(); assert!(text.contains("cannot be applied safely"), "{text}"); assert!(text.contains("pinned URL"), "{text}"); @@ -1568,8 +1730,13 @@ mod tests { let runner = RecordingRunner::new(); runner.set_response("chroot", fail("!!! emerge failed")); let exec = make_exec(runner); - let err = - apply_updates(&exec, Path::new(&exec.root), Some(Backend::PortageSource)).unwrap_err(); + let err = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::PortageSource), + None, + ) + .unwrap_err(); assert!(err.to_string().contains("[portage] failed")); } @@ -1591,57 +1758,428 @@ mod tests { assert!(plan.is_err()); } - #[test] - fn flatpak_update_effects_bind_exact_identities_and_versions() { - use crate::command::{CommandOutput, CommandSpec, ProcessRunner}; - struct Fp; - impl ProcessRunner for Fp { - fn execute(&self, spec: &CommandSpec) -> Result { - let ok = |s: &str| CommandOutput { - status: 0, - success: true, - stdout: s.as_bytes().to_vec(), - stderr: Vec::new(), - duration: std::time::Duration::from_millis(1), - }; - let flatpak = spec.args.iter().any(|a| a == "flatpak"); - if flatpak && spec.args.iter().any(|a| a == "--version") { - return Ok(ok("Flatpak 1.18\n")); - } - if flatpak && spec.args.iter().any(|a| a == "remote-ls") { - return Ok(ok( - "org.mozilla.firefox 129 stable\norg.gnome.Calc 46 stable\n", - )); - } - if flatpak && spec.args.iter().any(|a| a == "list") { - return Ok(ok("org.mozilla.firefox 128\norg.gnome.Calc 45\n")); - } - Ok(ok("")) + // --- Exact Flatpak/Snap update transactions --------------------------- + + fn ok_out(s: &str) -> crate::command::CommandOutput { + crate::command::CommandOutput { + status: 0, + success: true, + stdout: s.as_bytes().to_vec(), + stderr: Vec::new(), + duration: std::time::Duration::from_millis(1), + } + } + + /// One fake Flatpak ref update (`ref,app,version,branch,commit,origin,installation,type,arch`). + #[derive(Clone)] + struct FpUpdate { + reference: String, + app: String, + version: String, + commit: String, + installation: String, + kind: String, + } + + #[derive(Default)] + struct FpState { + /// ref -> (version, commit) + installed: std::collections::BTreeMap, + updates: Vec, + /// Apply the update to `installed` on `flatpak update`. + apply: bool, + /// A ref whose update command fails. + fail_ref: Option, + update_calls: Vec>, + } + + struct FpRunner { + state: std::sync::Mutex, + } + + impl FpRunner { + fn new(state: FpState) -> Self { + Self { + state: std::sync::Mutex::new(state), } } + fn set_updates(&self, updates: Vec) { + self.state.lock().unwrap().updates = updates; + } + fn update_calls(&self) -> Vec> { + self.state.lock().unwrap().update_calls.clone() + } + } + + impl crate::command::ProcessRunner for FpRunner { + fn execute(&self, spec: &crate::command::CommandSpec) -> Result { + let is_flatpak = spec.args.iter().any(|a| a == "flatpak"); + if !is_flatpak { + return Ok(ok_out("")); + } + if spec.args.iter().any(|a| a == "--version") { + return Ok(ok_out("Flatpak 1.18\n")); + } + if spec.args.iter().any(|a| a == "remote-ls") { + let state = self.state.lock().unwrap(); + let mut text = String::new(); + for u in &state.updates { + text.push_str(&format!( + "{}\t{}\t{}\tstable\t{}\tflathub\t{}\t{}\tx86_64\n", + u.reference, u.app, u.version, u.commit, u.installation, u.kind + )); + } + return Ok(ok_out(&text)); + } + if spec.args.iter().any(|a| a == "update") { + let mut state = self.state.lock().unwrap(); + let target = spec.args.last().cloned().unwrap_or_default(); + state.update_calls.push(spec.args.clone()); + if state.fail_ref.as_deref() == Some(target.as_str()) { + return Ok(crate::command::CommandOutput { + status: 1, + success: false, + stdout: Vec::new(), + stderr: b"update failed".to_vec(), + duration: std::time::Duration::from_millis(1), + }); + } + if state.apply { + let applied = state + .updates + .iter() + .find(|u| u.reference == target) + .map(|u| (u.version.clone(), u.commit.clone())); + if let Some((version, commit)) = applied { + state.installed.insert(target.clone(), (version, commit)); + } + } + return Ok(ok_out("updated\n")); + } + if spec.args.iter().any(|a| a == "list") { + let state = self.state.lock().unwrap(); + // Verification uses `--columns=ref,commit`. + if spec.args.iter().any(|a| a.contains("ref,commit")) { + let mut text = String::new(); + for (r, (_, commit)) in &state.installed { + text.push_str(&format!("{r}\t{commit}\n")); + } + return Ok(ok_out(&text)); + } + let mut text = String::new(); + for (r, (version, commit)) in &state.installed { + let kind = if r.starts_with("runtime/") { + "runtime" + } else { + "app" + }; + let app = r.split('/').nth(1).unwrap_or(""); + text.push_str(&format!( + "{r}\t{app}\t{version}\tstable\t{commit}\t{}\t{kind}\tx86_64\n", + "system" + )); + } + return Ok(ok_out(&text)); + } + Ok(ok_out("")) + } + } + + fn fp_exec(state: FpState) -> (Exec, std::sync::Arc) { let path = tempfile::tempdir().unwrap().keep(); let logger = crate::log::Logger::open(path.join("l.log"), phoebus_core::Redactor::new(), false); - let exec = Exec::new(Box::new(Fp), logger, false, path.join("root"), path); - let plan = crate::actions::resolve_effects( - &exec, + let runner = std::sync::Arc::new(FpRunner::new(state)); + // The runner must be shared; Exec owns a Box, so wrap an Arc-backed clone. + struct Shared(std::sync::Arc); + impl crate::command::ProcessRunner for Shared { + fn execute(&self, spec: &crate::command::CommandSpec) -> Result { + self.0.execute(spec) + } + } + let exec = Exec::new( + Box::new(Shared(runner.clone())), + logger, + false, + path.join("root"), + path, + ); + // `tool_in_target` needs a real flatpak binary in the target. + install_fake_tool(&exec.root, "flatpak"); + (exec, runner) + } + + fn fp_update(reference: &str, app: &str, version: &str, commit: &str) -> FpUpdate { + FpUpdate { + reference: reference.into(), + app: app.into(), + version: version.into(), + commit: commit.into(), + installation: "system".into(), + kind: if reference.starts_with("runtime/") { + "runtime".into() + } else { + "app".into() + }, + } + } + + fn resolve_flatpak_plan(exec: &Exec) -> ResolvedPlan { + crate::actions::resolve_effects( + exec, &phoebus_core::action::SystemAction::ApplyUpdates { backend: Some(Backend::Flatpak), }, &exec.root, ) - .unwrap(); - assert!(plan.established); - assert!( - plan.effects - .iter() - .any(|e| e.target == "org.mozilla.firefox-129" - && e.previous.as_deref() == Some("128")) + .unwrap() + } + + #[test] + fn flatpak_update_transactions_bind_full_refs_and_immutable_commits() { + let ref_firefox = "app/org.mozilla.firefox/x86_64/stable"; + let updates = format!( + "{ref_firefox}\torg.mozilla.firefox\t129\tstable\tNEWCOMMIT\tflathub\tsystem\tapp\tx86_64\n" ); - assert!(plan - .effects - .iter() - .any(|e| e.target == "org.gnome.Calc-46" && e.previous.as_deref() == Some("45"))); + let installed = format!( + "{ref_firefox}\torg.mozilla.firefox\t128\tstable\tOLDCOMMIT\tflathub\tsystem\tapp\tx86_64\n" + ); + let txns = crate::actions::parse_flatpak_transactions(&updates, &installed); + assert_eq!(txns.len(), 1); + let t = &txns[0]; + assert_eq!(t.identity, ref_firefox); + assert_eq!(t.previous_version.as_deref(), Some("128")); + assert_eq!(t.next_version, "129"); + assert_eq!(t.previous_revision.as_deref(), Some("OLDCOMMIT")); + assert_eq!(t.next_revision.as_deref(), Some("NEWCOMMIT")); + assert_eq!(t.scope.as_deref(), Some("system")); + assert!(t.exact); + } + + #[test] + fn snap_update_transactions_bind_revisions_and_scope() { + let refresh = "Name Version Rev Tracking Publisher Notes\n\ + hello 2.11 32 latest/stable canonical✓ -\n"; + let installed = "Name Version Rev Tracking Publisher Notes\n\ + hello 2.10 29 latest/stable canonical✓ -\n"; + let txns = crate::actions::parse_snap_transactions(refresh, installed); + assert_eq!(txns.len(), 1); + let t = &txns[0]; + assert_eq!(t.identity, "hello"); + assert_eq!(t.previous_version.as_deref(), Some("2.10")); + assert_eq!(t.next_version, "2.11"); + assert_eq!(t.previous_revision.as_deref(), Some("29")); + assert_eq!(t.next_revision.as_deref(), Some("32")); + assert_eq!(t.scope.as_deref(), Some("latest/stable")); + assert!(t.exact); + } + + #[test] + fn flatpak_update_execution_carries_the_reviewed_transaction() { + let ref_a = "app/org.mozilla.firefox/x86_64/stable"; + let ref_b = "runtime/org.gnome.Platform/x86_64/45"; + let mut state = FpState { + apply: true, + ..Default::default() + }; + state + .installed + .insert(ref_a.into(), ("128".into(), "OLD_A".into())); + state + .installed + .insert(ref_b.into(), ("45.1".into(), "OLD_B".into())); + let (exec, runner) = fp_exec(state); + runner.set_updates(vec![ + fp_update(ref_a, "org.mozilla.firefox", "129", "NEW_A"), + fp_update(ref_b, "org.gnome.Platform", "45.2", "NEW_B"), + ]); + let plan = resolve_flatpak_plan(&exec); + // The reviewed plan binds full refs, commits and the runtime. + assert_eq!(plan.updates.len(), 2); + assert!(plan.updates.iter().all(|t| t.exact)); + assert!(plan.updates.iter().any(|t| t.identity == ref_b)); + + let out = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::Flatpak), + Some(&plan), + ) + .unwrap(); + assert!(out.contains("updated 2 reviewed ref"), "{out}"); + let calls = runner.update_calls(); + assert_eq!(calls.len(), 2); + for call in &calls { + assert!(call.iter().any(|a| a == "-y")); + assert!(call.iter().any(|a| a == "--installation=system")); + assert!(call.iter().any(|a| a == "--")); + } + assert!(calls.iter().any(|c| c.last().unwrap() == ref_a)); + assert!(calls.iter().any(|c| c.last().unwrap() == ref_b)); + } + + #[test] + fn flatpak_update_refuses_a_changed_commit_with_unchanged_version() { + let reference = "app/org.mozilla.firefox/x86_64/stable"; + let mut state = FpState { + apply: true, + ..Default::default() + }; + state + .installed + .insert(reference.into(), ("128".into(), "OLD".into())); + let (exec, runner) = fp_exec(state); + runner.set_updates(vec![fp_update( + reference, + "org.mozilla.firefox", + "129", + "REVIEWED", + )]); + let plan = resolve_flatpak_plan(&exec); + // The remote silently republishes a different commit with the same + // human-readable version. + runner.set_updates(vec![fp_update( + reference, + "org.mozilla.firefox", + "129", + "DIFFERENT", + )]); + let err = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::Flatpak), + Some(&plan), + ) + .unwrap_err(); + assert!( + err.to_string().contains("changed before execution"), + "{err}" + ); + // No update command may have run for an unreviewed commit. + assert!(runner.update_calls().is_empty()); + } + + #[test] + fn flatpak_update_refuses_a_newly_appeared_update() { + let reference = "app/org.mozilla.firefox/x86_64/stable"; + let mut state = FpState { + apply: true, + ..Default::default() + }; + state + .installed + .insert(reference.into(), ("128".into(), "OLD".into())); + let (exec, runner) = fp_exec(state); + runner.set_updates(vec![fp_update( + reference, + "org.mozilla.firefox", + "129", + "NEW", + )]); + let plan = resolve_flatpak_plan(&exec); + // A second update appears after review. + runner.set_updates(vec![ + fp_update(reference, "org.mozilla.firefox", "129", "NEW"), + fp_update( + "app/org.gnome.Calc/x86_64/stable", + "org.gnome.Calc", + "46", + "CALC", + ), + ]); + let err = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::Flatpak), + Some(&plan), + ) + .unwrap_err(); + assert!( + err.to_string().contains("changed before execution"), + "{err}" + ); + assert!(runner.update_calls().is_empty()); + } + + #[test] + fn flatpak_update_verifies_the_resulting_state() { + let reference = "app/org.mozilla.firefox/x86_64/stable"; + let mut state = FpState { + // Do NOT apply: the installed commit never reaches the reviewed one. + apply: false, + ..Default::default() + }; + state + .installed + .insert(reference.into(), ("128".into(), "OLD".into())); + let (exec, runner) = fp_exec(state); + runner.set_updates(vec![fp_update( + reference, + "org.mozilla.firefox", + "129", + "NEW", + )]); + let plan = resolve_flatpak_plan(&exec); + let err = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::Flatpak), + Some(&plan), + ) + .unwrap_err(); + let text = err.to_string(); + assert!(text.contains("did not reach the reviewed commit"), "{text}"); + } + + #[test] + fn flatpak_execution_requires_a_reviewed_transaction() { + let (exec, runner) = fp_exec(FpState { + apply: true, + ..Default::default() + }); + // No approved plan is supplied: the source must refuse rather than + // discover and execute. + let err = + apply_updates(&exec, Path::new(&exec.root), Some(Backend::Flatpak), None).unwrap_err(); + assert!(err.to_string().contains("no reviewed transaction"), "{err}"); + assert!(runner.update_calls().is_empty()); + } + + #[test] + fn a_source_that_failed_resolution_is_not_re_discovered_at_execution() { + let (exec, runner) = fp_exec(FpState { + apply: true, + ..Default::default() + }); + runner.set_updates(vec![fp_update( + "app/org.mozilla.firefox/x86_64/stable", + "org.mozilla.firefox", + "129", + "NEW", + )]); + // The reviewed plan recorded that Flatpak could not be resolved and so + // carries no transaction. Even though updates are now available, they + // must not be executed. + let mut approved = ResolvedPlan { + established: true, + ..Default::default() + }; + approved + .consequences + .push("[flatpak] updates could not be resolved: offline".into()); + approved.recompute_digest(); + let err = apply_updates( + &exec, + Path::new(&exec.root), + Some(Backend::Flatpak), + Some(&approved), + ) + .unwrap_err(); + let text = err.to_string(); + assert!(text.contains("not re-discovered"), "{text}"); + // Most importantly, no update and no re-discovery happened: the runner + // recorded no update call. + assert!(runner.update_calls().is_empty()); } #[test] @@ -1668,7 +2206,8 @@ mod tests { fn apply_updates_never_runs_host_snap_for_an_alternate_root() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); - let err = apply_updates(&exec, Path::new(&exec.root), Some(Backend::Snap)).unwrap_err(); + let err = + apply_updates(&exec, Path::new(&exec.root), Some(Backend::Snap), None).unwrap_err(); assert!(err.to_string().contains("alternate root"), "{err}"); // Crucially, no host `snap` command was attempted. assert!( @@ -1735,6 +2274,14 @@ mod tests { ); } + fn install_vdb(root: &Path, atom: &str, version: &str, fields: &[(&str, &str)]) { + let dir = root.join(format!("var/db/pkg/{atom}-{version}")); + std::fs::create_dir_all(&dir).unwrap(); + for (name, value) in fields { + std::fs::write(dir.join(name), value).unwrap(); + } + } + #[test] fn package_dependencies_reverse_finds_installed_dependants() { let runner = RecordingRunner::new(); @@ -1752,6 +2299,67 @@ mod tests { assert!(empty.contains("no installed package depends on"), "{empty}"); } + #[test] + fn reverse_dependencies_include_an_installed_target() { + // Regression: the queried package is installed, and a *different* + // installed package depends on it. The old self-reference check compared + // the dependency target with the queried package and wrongly dropped + // exactly this valid match. + let runner = RecordingRunner::new(); + let exec = make_exec(runner); + let root = Path::new(&exec.root); + install_vdb(root, "dev-libs/libfoo", "1.2.3", &[]); + install_vdb( + root, + "app-misc/consumer", + "2.0", + &[("RDEPEND", ">=dev-libs/libfoo-1.2")], + ); + + // Full and bare names both find the real dependent. + for query in ["dev-libs/libfoo", "libfoo"] { + let out = package_dependencies(&exec, root, query, true).unwrap(); + assert!(out.contains("app-misc/consumer"), "{query} -> {out}"); + assert!(out.contains("dev-libs/libfoo"), "{query} -> {out}"); + } + } + + #[test] + fn reverse_dependencies_handle_conditionals_and_exclude_self_references() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner); + let root = Path::new(&exec.root); + install_vdb(root, "dev-libs/libfoo", "1.2.3", &[]); + install_vdb( + root, + "app-misc/consumer", + "2.0", + &[( + "RDEPEND", + "ssl? ( >=dev-libs/libfoo-1.2 ) !dev-libs/libfoo[old]", + )], + ); + // A genuine self-reference: libfoo declares a dependency on itself. + install_vdb( + root, + "dev-libs/libfoo", + "1.2.3", + &[("RDEPEND", ">=dev-libs/libfoo-1.2")], + ); + + let out = package_dependencies(&exec, root, "dev-libs/libfoo", true).unwrap(); + assert!(out.contains("from app-misc/consumer"), "{out}"); + assert!(out.contains("ssl?"), "condition preserved: {out}"); + assert!( + !out.contains("from dev-libs/libfoo"), + "a package declaring a dependency on itself is not a dependent: {out}" + ); + + // A negative: nothing depends on an unrelated installed package. + let none = package_dependencies(&exec, root, "app-misc/lonely", true).unwrap(); + assert!(none.contains("no installed package depends on"), "{none}"); + } + #[test] fn check_updates_can_be_restricted_to_one_source() { let runner = RecordingRunner::new(); diff --git a/installer-rs/crates/phoebus-installer/src/service.rs b/installer-rs/crates/phoebus-installer/src/service.rs index 79375a9..7137e9b 100644 --- a/installer-rs/crates/phoebus-installer/src/service.rs +++ b/installer-rs/crates/phoebus-installer/src/service.rs @@ -443,6 +443,7 @@ fn validate_effect_bindings(plan: &PreparedPlan, steps: &[ResolvedPlan]) -> Resu for step in steps { combined.effects.extend(step.effects.clone()); combined.cleanup.extend(step.cleanup.clone()); + combined.updates.extend(step.updates.clone()); combined.consequences.extend(step.consequences.clone()); } combined.recompute_digest(); @@ -1305,6 +1306,7 @@ pub fn plan_general( if eff.established { combined_effects.effects.extend(eff.effects.clone()); combined_effects.cleanup.extend(eff.cleanup.clone()); + combined_effects.updates.extend(eff.updates.clone()); combined_effects .consequences .extend(eff.consequences.clone()); @@ -1588,6 +1590,7 @@ fn revalidate_plan_state(exec: &Exec, root: &Path, plan: &PreparedPlan) -> Resul if eff.established { combined.effects.extend(eff.effects); combined.cleanup.extend(eff.cleanup); + combined.updates.extend(eff.updates); combined.consequences.extend(eff.consequences); } } @@ -1874,10 +1877,10 @@ fn execute_claimed( // `validate_prepared_plan` (install revalidation / installed-copy // checks); re-resolving them here would see an empty plan and falsely // report drift. - let mut current = ResolvedPlan::default(); + let mut current: Option = None; if actions::resolves_effects(action) { let reviewed = &reviewed_steps[idx]; - current = match actions::resolve_effects(exec, action, root) { + let resolved = match actions::resolve_effects(exec, action, root) { Ok(current) => current, Err(e) => { let detail = format!("effect resolution failed for `{described}`: {e}"); @@ -1889,7 +1892,7 @@ fn execute_claimed( continue; } }; - if !approval_holds(reviewed, ¤t, &introduced) { + if !approval_holds(reviewed, &resolved, &introduced) { let detail = format!( "the reviewed effects for `{described}` changed materially before execution" ); @@ -1900,9 +1903,17 @@ fn execute_claimed( drift = Some(format!("stale plan: {detail}")); continue; } + current = Some(resolved); } - match actions::execute(exec, action, auth, root) { + // Execution carries the *reviewed* transaction for actions that must + // not re-discover state (Flatpak/Snap updates, cache cleanup). + let carried = if actions::resolves_effects(action) { + Some(&reviewed_steps[idx]) + } else { + None + }; + match actions::execute_inner(exec, action, auth, root, carried) { Ok(result) => { if result.snapshot.is_some() { snapshot = result.snapshot; @@ -1911,7 +1922,9 @@ fn execute_claimed( completed.push((described, result.output)); // Packages this approved action installed may legitimately // appear in a later step's re-resolution. - introduced.extend(current.effects); + if let Some(current) = current { + introduced.extend(current.effects); + } } Err(e) => { let detail = e.to_string(); @@ -3735,6 +3748,7 @@ mod tests { for step in &steps { combined.effects.extend(step.effects.clone()); combined.cleanup.extend(step.cleanup.clone()); + combined.updates.extend(step.updates.clone()); combined.consequences.extend(step.consequences.clone()); } combined.recompute_digest(); @@ -4085,6 +4099,38 @@ mod tests { .expect("cleanup intents must aggregate into the bound plan"); } + #[test] + fn effect_binding_aggregates_update_transactions() { + let root = tempdir().unwrap(); + let mut step = ResolvedPlan { + established: true, + ..Default::default() + }; + step.updates.push(phoebus_core::effects::UpdateTransaction { + source: phoebus_core::effects::UpdateSource::Flatpak, + identity: "app/org.mozilla.firefox/x86_64/stable".into(), + previous_version: Some("128".into()), + next_version: "129".into(), + previous_revision: Some("OLD".into()), + next_revision: Some("NEW".into()), + scope: Some("system".into()), + related: Vec::new(), + exact: true, + }); + let plan = general_portage_plan( + "plan-updates", + vec![SystemAction::ApplyUpdates { + backend: Some(Backend::Flatpak), + }], + vec![step], + root.path(), + effective_uid(), + ); + let steps = reviewed_steps(&plan).unwrap(); + validate_effect_bindings(&plan, &steps) + .expect("update transactions must aggregate into the bound plan"); + } + #[test] fn constrain_action_rewrites_optional_backend_discovery_before_querying() { let discover = constrain_action(