From 4679f935e3f02ec2a25406c80b9f173ee52c9765 Mon Sep 17 00:00:00 2001 From: Phoebus Date: Tue, 22 Sep 2026 07:30:38 -0700 Subject: [PATCH] fix(sys): harden the administration broker and complete its command surface --- docs/CHANGELOG.md | 48 ++ docs/HANDOFF.md | 76 +- docs/ROADMAP.md | 2 +- docs/STATUS.md | 27 +- docs/VM-BOOT-VERIFICATION.md | 8 + .../crates/phoebus-ai/src/assistant.rs | 9 +- .../crates/phoebus-core/src/action.rs | 240 ++++++- .../crates/phoebus-core/src/effects.rs | 29 + .../crates/phoebus-core/src/transaction.rs | 126 +++- .../crates/phoebus-executor/src/actions.rs | 662 ++++++++++++++++-- .../crates/phoebus-executor/src/portage.rs | 4 +- .../crates/phoebus-installer/src/bin/sys.rs | 104 ++- .../crates/phoebus-installer/tests/sys_cli.rs | 59 ++ 13 files changed, 1239 insertions(+), 155 deletions(-) create mode 100644 installer-rs/crates/phoebus-installer/tests/sys_cli.rs diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index fd67700..55f54c3 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,54 @@ this file is the curated, human-readable summary. Format: Keep a Changelog. Dates are UTC. +## 2026-09-22 — `sys` administration hardening + +Correctness work on the deterministic `sys` broker and the assistant path that +shares it. Every change is covered by unit tests (368 Rust tests pass); the +destructive rollback VM acceptance remains unverified (see below). + +### Fixed +- **Alternate-root execution now targets the requested system.** Package + execution, service enable/disable and Flatpak operations previously ignored + `--root` and ran against the host, while `emerge --pretend` previews already + ran in the target. All of them now go through one `target_spec`/chroot path, + and operations that cannot be confined to a non-running root (service status, + start, stop, restart) are **rejected before any command is built**. + `systemctl --root` is used for offline enable/disable. Regression tests prove + preview and execution both emit a chroot confined to the same root, and that + a rejected operation invokes nothing. +- **Validation at the execution boundary.** `parse_natural` emitted actions + that were never validated, so a manual `sys install --color=n` could pass an + option through. `parse_natural` now validates its own output, `resolve_effects` + and `execute` validate every action before touching a command, and + option-like operands (leading `-`) are rejected for services, packages and + search. `emerge`/`flatpak` operands are passed after an explicit `--` + separator. Manual, JSON and AI entry paths are all covered. +- **`sys ask` reports failure truthfully.** A failing action now stops the plan, + records the completed/failed/unattempted actions, and exits nonzero instead of + returning success. +- **Complete transaction history.** Ordinary mutating `sys` commands are + recorded (previously only assistant plans were), the protective snapshot id + created by the generic high-risk path is preserved and recorded, and a + recording failure is surfaced instead of being swallowed. +- **Reliable package approval.** `emerge --pretend` output is now classified as + *established* (a real plan, including a genuine `0 packages`) or + *unestablished*; `sys` refuses to proceed when the effects cannot be + established rather than treating unparseable output as "nothing to do". A + material change between review and execution still aborts. + +### Added +- Typed service lifecycle actions `start_service`, `stop_service`, + `restart_service`, and ordinary commands for the existing bounded verbs: + `sys flatpak install|remove|list`, `sys nix prepare …`, `sys snapshot [op]`, + `sys start|stop|restart `. The AI prompt exposes the new verbs, so the + manual and AI interfaces share one validated execution path. +- `phoebus-executor::actions::execute_plan` runs a reviewed multi-action plan, + stopping at the first failure and marking the rest unattempted, so the + assistant and any multi-step caller report partial failure consistently. +- CLI integration tests (`tests/sys_cli.rs`) cover the manual and JSON entry + paths and exit codes. + ## Unreleased ### Added diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md index 4a7a7a9..9a3c6cf 100644 --- a/docs/HANDOFF.md +++ b/docs/HANDOFF.md @@ -3,36 +3,37 @@ Rolling note between work sessions. **Update this file before you finish**, then commit it. It should be short and current; use `git log` for detail. -- **Last updated:** this session (2026-09-22), OpenCode adapter. -- **Current focus:** Implemented a real OpenCode agent-server adapter - (`phoebus-executor::opencode_client`) that speaks the session/message protocol - over curl and implements `ModelTransport`, wired into canonical config - (`ai.backend = "opencode"`), the GUI ("OpenCode agent" + Test connection), - the installer CLI (`--ai-backend opencode`, `--opencode-test`) and `sys ask`. - The mandatory-AI gate now requires a validated interaction with invalidated - state. The probe/transport were hardened (loopback/TLS policy, DNS pinning, - status checks, auth), and the GUI harness gained exact-session and - frontend-state assertions. +- **Last updated:** this session (2026-09-22), `sys` administration hardening. +- **Current focus:** Hardened `sys` into a reliable administration tool. + (1) All package/service/Flatpak execution now honours `--root` through one + `target_spec`/chroot path (offline `systemctl --root` for enable/disable), + and runtime-only service operations are rejected before any command when an + alternate root is requested. (2) Every action is validated at the execution + boundary (`parse_natural` now validates its own output); option-like operands + are rejected and operands are passed after `--`. (3) `sys ask` stops at the + first failure, records completed/failed/unattempted actions, and exits + nonzero. (4) Ordinary mutating commands are recorded, the generic protective + snapshot id is preserved, and recording failures are visible. (5) A genuinely + empty package plan is distinguished from unparseable emerge output, which is + refused. (6) Added typed `start/stop/restart_service` and ordinary commands + for Flatpak, Nix preparation and snapshots. - **Last verified (this session):** - - 336 Rust tests + 16 harness tests green; fmt/clippy clean. - - Real inference through `opencode 1.18.32` with `deepseek-v4-flash`; - `--opencode-test` → `{"phoebus":"ok"}`; the GUI bridge `ai_check` applied 8 - reviewed changes; `sys ask` proposed and ran a read-only action. - - Auth: none → `authentication_required`, wrong → `authentication_rejected`, - correct → `model_catalog` + successful inference. Plain http to a LAN host - → `insecure_transport`, exit 1, no request sent. - - 7 real-HTTP integration tests (status, chunked multibyte, oversize, - redirect, disconnect, auth) and 2 env-gated live tests. - - GUI acceptance in a disposable VM: `gui-driver.py opencode` passed 17/17 - (selected OpenCode, entered the endpoint/model through the UI, real model - response + validated proposal); `gui-driver.py gui` passed 25/25. - ISO `sha256 4db98db3…`, static binary `sha256 72c2dcd0…`. -- **Not yet verified this session:** the full graphical **installation** through - OpenCode past the AI gate (disk/account/review/ERASE/install/detached boot), - and a separate physical computer. The pinned manifest is `v1.18.31` while the - tested binary is `1.18.32`. On this server use `deepseek-flash`; the model - must match the server's catalog (the GUI default `deepseek-chat` does not - exist on it). + - 368 Rust tests green (including 3 new CLI integration tests) + 16 harness + tests; `cargo fmt --all -- --check` and `cargo clippy --workspace + --all-targets` clean (exit 0). Static `phoebus-installer`/`sys` build. + - Unit/integration evidence for all six fixes: preview and execution both + chroot to the same alternate root; runtime service ops reject an alternate + root without invoking anything; invalid actions are refused by the manual, + JSON and AI parsers and at the executor; `execute_plan` marks later actions + unattempted after a failure; unparseable emerge output is refused while a + real `0 packages` plan is accepted; a material plan change aborts before + the mutation. +- **Not yet verified this session:** the destructive **rollback VM + acceptance** (`sys undo`). See the blocker below — rollback stays explicitly + unverified. The OpenCode live tests are environment-gated and were skipped + (no `PHOEBUS_OPENCODE_LIVE_*`), and the full graphical install past the AI + gate and a separate physical computer remain unverified from the previous + session. - **Public repo:** (remote `origin`, SSH port 2222). @@ -46,9 +47,13 @@ commit it. It should be short and current; use `git log` for detail. 2. **Installed Mango desktop**: install the default profile in a graphical VM and verify the panel/launcher/terminal/audio/network/lock/idle, the dwl fallback, and the welcome/installer absence on the installed system. -3. **Rollback VM acceptance**: deliberately break an update, run `sys undo`, - reboot detached, verify `/var/db/pkg` matches `/`, then boot the recovery - entry with no network or model (ADR 0007). +3. **Rollback VM acceptance (BLOCKED this session)**: deliberately break an + update, run `sys undo`, reboot detached, verify `/var/db/pkg` matches `/`, + then boot the recovery entry with no network or model (ADR 0007). Needed: + (a) free the >=20 GiB `make doctor` wants for the harness (the root fs has + ~12 GiB free); (b) an automated driver for the break-update/undo/recovery + sequence — `appliance-driver.py` only implements `install`/`boot`. Until + both exist, `sys undo` stays unverified. 4. **Narrow the live broker privilege**: replace the live-only `NOPASSWD: ALL` rule for the GUI bridge with the smallest practical command-scoped rule once a safe sudoers form is verified (ADR 0011 context). @@ -66,8 +71,11 @@ commit it. It should be short and current; use `git log` for detail. verified as a blocked prerequisite and with a fixture transport. - **Installed** Mango (GURU + compiled Quickshell/DMS) is not VM-verified; nor is the dwl fallback session. -- Rollback and the AI sandbox are implemented and unit-tested but not - VM-verified. NVIDIA is not verified. +- **Rollback (`sys undo`) is implemented and unit-tested but NOT VM-verified + (BLOCKED).** Exact blockers: `make doctor` reports ~13 GiB free versus the + >=20 GiB the harness wants (root fs ~12 GiB free), and the harness has no + automated break-update/undo/recovery-boot mode. The AI sandbox is likewise + implemented/unit-tested but not VM-verified. NVIDIA is not verified. - A **separate physical computer** for the OpenCode transport test is not available (BLOCKED); a forwarded-hop + unauthenticated-LAN-refusal host-local test was done instead. diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index e7fc626..2aeecd1 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -16,7 +16,7 @@ more interesting. Status vocabulary: **Done**, **Partial**, **Next**, **Later**. | 9 | Background update preparation | Partial | distfile fetch VM-verified; `--buildpkgonly` skipped while deps need merging (recorded Ready); activation explicit | | 10 | Snapshots / rollback | Partial | snapshot sets + history VM-verified on Btrfs; coordinated rollback, `sys undo`, recovery entry, retention and free-space gate implemented + unit-tested; destructive VM acceptance test pending | | 11 | AI privilege and privacy architecture | Partial | typed actions + enforced privacy boundary + guided proposal review + OS-level sandbox (systemd-run hardening) + approval-bound resolved effects; live call/sandbox not VM-verified | -| 12 | `sys` operations | Partial | history, enable/disable, inspect, install, prepare-updates VM-verified; `undo`, `undo status`, `ask` added (unit-tested, VM gate pending) | +| 12 | `sys` operations | Partial | history, enable/disable, inspect, install, prepare-updates VM-verified; target-root consistency, execution-boundary validation, truthful `ask` failure, full transaction outcomes, empty-vs-unestablished plans, and start/stop/restart + flatpak/nix/snapshot commands added (unit/integration-tested; VM gate pending) | | 13 | Kernel optimization policy | Later | conservative fragments only; SAFE/OPTIMIZED/AGGRESSIVE selection exists | | 14 | ISO / release pipeline | Partial | manifest/verify/assemble-plan tooling; release gates, atomic publish and signing helpers added; no CI runner has executed a gate | | 15 | Recovery environment | Later | recovery BLS entry generated; snapshot-based recovery entry written at install; full recovery image not built | diff --git a/docs/STATUS.md b/docs/STATUS.md index 359d1d7..0213c62 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -84,9 +84,13 @@ still not VM-verified. | AI privacy boundary (facts only) | yes | yes | no | n/a | | AI OS sandbox (systemd hardened unit) | yes | yes | no | no | | Installed assistant (`sys ask`) | yes | yes | fixture path only (no live call) | n/a | -| Resolved package effects + approval binding | yes | yes | no | n/a | -| Transaction log | yes | yes | no | no | +| Resolved package effects + approval binding | yes | yes (empty vs unestablished) | no | n/a | +| Transaction log (outcomes, protective snapshots) | yes | yes | no | no | | Flatpak/Nix bounded typed actions | yes | yes | no | no | +| `sys` consistent target-root execution | yes | yes | no | no | +| `sys` validation at the execution boundary | yes | yes | no | n/a | +| `sys ask` truthful failure (nonzero, stop, record) | yes | yes | no | n/a | +| Service start/stop/restart + flatpak/nix/snapshot commands | yes | yes | no | no | | Desktop session helpers (menu/lock/idle) | yes | yes | functional test (stubs) | no | | Mango + DankMaterialShell profile | yes | yes | live graphical VM yes; installed no | no | | Mango is the default desktop | yes | yes | live graphical VM yes; installed install pending | no | @@ -123,13 +127,18 @@ still not VM-verified. - **NVIDIA** is architecturally allowed but not verified on hardware. - **UKI** is deferred; the dracut UKI generator did not embed the kernel command line, so BLS is used (and verified). -- **Rollback** is implemented and unit-tested end to end at the command/text - layer (snapshot sets, root/var generation, fstab and BLS rewriting, kernel - selection, interruption resume, retention, free-space gate), but the - destructive VM acceptance test (deliberately broken update -> `sys undo` -> - detached reboot -> package-state check -> recovery with no network/model) has - not been run. `sys undo` is present; treat VM verification as the gate before - relying on it. +- **Rollback is NOT VM-verified.** It is implemented and unit-tested end to end + at the command/text layer (snapshot sets, root/var generation, fstab and BLS + rewriting, kernel selection, interruption resume, retention, free-space + gate), but the destructive acceptance test (deliberately broken update -> + `sys undo` -> detached reboot -> package-state check -> recovery with no + network/model) was **not run in this session**. Blockers: (1) `make doctor` + reports only ~13 GiB free versus the >=20 GiB the VM harness wants, and the + root filesystem has ~12 GiB free; (2) the committed appliance/serial harness + only implements `install` and `boot` modes — there is no automated driver for + the break-update / `sys undo` / recovery-boot sequence, which is documented as + an interactive procedure. `sys undo` is present; treat VM verification as the + gate before relying on it. - **Mango + DankMaterialShell** is the default desktop (`desktop.profile = "mango"`, Flatpak on). It is implemented with Mango 0.17.2, DMS (pinned release), Quickshell 0.3.1 and scenefx 0.5 from the GURU diff --git a/docs/VM-BOOT-VERIFICATION.md b/docs/VM-BOOT-VERIFICATION.md index 0a75b60..2c4aeb9 100644 --- a/docs/VM-BOOT-VERIFICATION.md +++ b/docs/VM-BOOT-VERIFICATION.md @@ -399,6 +399,14 @@ Not yet verified for this baseline: `sys undo`, a deliberately broken update, a detached rollback reboot, and recovery with no network/model (the rollback acceptance gate). +**2026-09-22 update — rollback acceptance still NOT run.** A session of `sys` +administration hardening did not run the destructive acceptance test. The +blockers are unchanged and exact: `make doctor` reports ~13 GiB free versus the +>=20 GiB the VM harness requires (the root filesystem has ~12 GiB free), and the +committed harness (`appliance-driver.py`, `serial-driver.py`) implements only +`install` and `boot`, with no automated break-update / `sys undo` / +recovery-boot mode. `sys undo` therefore remains **unverified**, not passing. + ## Mango + DankMaterialShell verification (IN PROGRESS) ### Live ISO — graphical VM (verified 2026-09-21) diff --git a/installer-rs/crates/phoebus-ai/src/assistant.rs b/installer-rs/crates/phoebus-ai/src/assistant.rs index 4906ab7..1440348 100644 --- a/installer-rs/crates/phoebus-ai/src/assistant.rs +++ b/installer-rs/crates/phoebus-ai/src/assistant.rs @@ -66,7 +66,8 @@ pub fn action_tag(action: &SystemAction) -> String { /// there is no shell or file action. pub const ASSISTANT_PROMPT: &str = "\ You are the Phoebus OS system assistant. You can ONLY propose actions from this \ -closed set: inspect_service, enable_service, disable_service, inspect_package, \ +closed set: inspect_service, enable_service, disable_service, start_service, \ +stop_service, restart_service, inspect_package, \ install_package, remove_package, search_packages, flatpak_install, flatpak_remove, \ flatpak_list, nix_prepare, create_snapshot, undo, rollback_status, history, \ update_system, prepare_updates, inspect_updates. @@ -270,6 +271,12 @@ mod tests { fn invalid_values_are_rejected() { let text = r#"{"version":1,"summary":"x","actions":[{"action":"install_package","atom":"bad atom"}],"rationale":[{"action":"install_package","reason":"x"}]}"#; assert!(parse_plan(text).is_err()); + // The AI path rejects option-like operands exactly like the manual and + // JSON paths, so a model cannot smuggle a command-line option. + let option = r#"{"version":1,"summary":"x","actions":[{"action":"install_package","atom":"-C"}],"rationale":[{"action":"install_package","reason":"x"}]}"#; + assert!(parse_plan(option).is_err()); + let service = r#"{"version":1,"summary":"x","actions":[{"action":"start_service","name":"--now"}],"rationale":[{"action":"start_service","reason":"x"}]}"#; + assert!(parse_plan(service).is_err()); } #[test] diff --git a/installer-rs/crates/phoebus-core/src/action.rs b/installer-rs/crates/phoebus-core/src/action.rs index f3cb955..0bf3484 100644 --- a/installer-rs/crates/phoebus-core/src/action.rs +++ b/installer-rs/crates/phoebus-core/src/action.rs @@ -35,6 +35,18 @@ pub enum SystemAction { DisableService { name: String, }, + /// Start a service now (runtime, not boot persistence). + StartService { + name: String, + }, + /// Stop a running service now. + StopService { + name: String, + }, + /// Restart a running service now. + RestartService { + name: String, + }, InspectPackage { atom: String, }, @@ -84,6 +96,9 @@ impl SystemAction { SystemAction::DisableService { name } => { format!("disable service {name} at boot") } + SystemAction::StartService { name } => format!("start service {name}"), + SystemAction::StopService { name } => format!("stop service {name}"), + SystemAction::RestartService { name } => format!("restart service {name}"), SystemAction::InspectPackage { atom } => format!("inspect package {atom}"), SystemAction::InstallPackage { atom } => format!("install package {atom}"), SystemAction::RemovePackage { atom } => format!("remove package {atom}"), @@ -130,6 +145,9 @@ impl SystemAction { SystemAction::PrepareUpdates => Risk::Medium, SystemAction::EnableService { .. } | SystemAction::DisableService { .. } + | SystemAction::StartService { .. } + | SystemAction::StopService { .. } + | SystemAction::RestartService { .. } | SystemAction::CreateSnapshot { .. } | SystemAction::FlatpakInstall { .. } | SystemAction::FlatpakRemove { .. } @@ -160,13 +178,19 @@ impl SystemAction { let valid_token = |s: &str| { !s.is_empty() && s.len() <= 128 + // A leading dash would be parsed by the target command as an + // option, so it is never a service/unit name. + && !s.starts_with('-') && s.chars() .all(|c| c.is_ascii_alphanumeric() || "@._+-".contains(c)) }; match self { SystemAction::InspectService { name } | SystemAction::EnableService { name } - | SystemAction::DisableService { name } => { + | SystemAction::DisableService { name } + | SystemAction::StartService { name } + | SystemAction::StopService { name } + | SystemAction::RestartService { name } => { if valid_token(name) { Ok(()) } else { @@ -241,6 +265,11 @@ pub fn valid_package_spec(spec: &str) -> bool { if spec.is_empty() || spec.len() > 128 { return false; } + // A leading dash would be consumed by `emerge` as an option (e.g. + // `--color=n` or `-C`), so it is rejected before any command is built. + if spec.starts_with('-') { + return false; + } let mut parts = spec.split('/'); let Some(first) = parts.next() else { return false; @@ -251,7 +280,10 @@ pub fn valid_package_spec(spec: &str) -> bool { match parts.next() { None => true, Some(second) => { - !second.is_empty() && second.chars().all(is_atom_char) && parts.next().is_none() + !second.is_empty() + && !second.starts_with('-') + && second.chars().all(is_atom_char) + && parts.next().is_none() } } } @@ -260,6 +292,8 @@ pub fn valid_package_spec(spec: &str) -> bool { pub fn valid_search_query(query: &str) -> bool { !query.is_empty() && query.len() <= 128 + // Reject a leading dash so a query cannot become an option. + && !query.starts_with('-') && query .chars() .all(|c| c.is_ascii_alphanumeric() || " ._+@/-".contains(c)) @@ -291,11 +325,29 @@ pub fn valid_nix_attribute(value: &str) -> bool { && !value.contains(':') } +/// Strip a case-insensitive ASCII prefix, returning the remainder. Used where +/// the operand after the verb is case-sensitive (Flatpak app ids). +fn strip_prefix_ascii_ci<'a>(text: &'a str, prefix: &str) -> Option<&'a str> { + // `get` (not `split_at`) so a prefix length that lands inside a multibyte + // character is simply "no match" rather than a panic. + let head = text.get(..prefix.len())?; + head.eq_ignore_ascii_case(prefix) + .then(|| &text[prefix.len()..]) +} + /// A small deterministic parser for common `sys` phrases. /// +/// The parser validates its own output, so the deterministic (manual) entry +/// path cannot emit an invalid action — exactly like the JSON and AI paths. /// Free-form requests that do not match are left to the AI path /// (`--action-file`), which still produces the same typed schema. pub fn parse_natural(text: &str) -> Option { + let action = parse_natural_raw(text)?; + action.validate().ok()?; + Some(action) +} + +fn parse_natural_raw(text: &str) -> Option { // Snapshot ids are case-sensitive (`@-20260101T000000-...`), so parse the // undo form from the original text before the general lowercasing. let raw = text.trim(); @@ -329,6 +381,21 @@ pub fn parse_natural(text: &str) -> Option { return Some(SystemAction::DisableService { name }); } } + if let Some(name) = rest("start ") { + if !name.is_empty() { + return Some(SystemAction::StartService { name }); + } + } + if let Some(name) = rest("stop ") { + if !name.is_empty() { + return Some(SystemAction::StopService { name }); + } + } + if let Some(name) = rest("restart ") { + if !name.is_empty() { + return Some(SystemAction::RestartService { name }); + } + } if let Some(atom) = rest("install ").or_else(|| rest("add ")) { if !atom.is_empty() { return Some(SystemAction::InstallPackage { atom }); @@ -352,6 +419,62 @@ pub fn parse_natural(text: &str) -> Option { return Some(SystemAction::InspectService { name }); } } + + // Flatpak verbs. App ids are case-sensitive (`org.chromium.Chromium`), so + // the operands are read from the original text, not the lowercased copy. + if let Some(rest) = strip_prefix_ascii_ci(raw, "flatpak ") { + let rest = rest.trim(); + if let Some(id) = strip_prefix_ascii_ci(rest, "install ") { + let id = id.trim(); + if !id.is_empty() { + return Some(SystemAction::FlatpakInstall { + app_id: id.to_string(), + }); + } + } + if let Some(id) = strip_prefix_ascii_ci(rest, "remove ") + .or_else(|| strip_prefix_ascii_ci(rest, "uninstall ")) + { + let id = id.trim(); + if !id.is_empty() { + return Some(SystemAction::FlatpakRemove { + app_id: id.to_string(), + }); + } + } + if rest.eq_ignore_ascii_case("list") || rest.eq_ignore_ascii_case("apps") { + return Some(SystemAction::FlatpakList); + } + } + if t == "flatpaks" { + return Some(SystemAction::FlatpakList); + } + + // Nix development packages. + if let Some(packages) = rest("nix prepare ").or_else(|| rest("nix realise ")) { + let packages: Vec = packages.split_whitespace().map(str::to_string).collect(); + if !packages.is_empty() { + return Some(SystemAction::NixPrepare { packages }); + } + } + if let Some(packages) = rest("nix install ") { + let packages: Vec = packages.split_whitespace().map(str::to_string).collect(); + if !packages.is_empty() { + return Some(SystemAction::NixPrepare { packages }); + } + } + + // Protective snapshot. + if t == "snapshot" || t.starts_with("snapshot ") { + let operation = t.strip_prefix("snapshot").unwrap_or("").trim(); + let operation = if operation.is_empty() { + "manual snapshot".to_string() + } else { + operation.to_string() + }; + return Some(SystemAction::CreateSnapshot { operation }); + } + if let Some(id) = rest("undo ").or_else(|| rest("rollback ")) { if id == "status" { return Some(SystemAction::RollbackStatus); @@ -552,4 +675,117 @@ mod tests { assert!(valid_flatpak_app_id("com.spotify.Client")); assert!(!valid_flatpak_app_id("com/spotify")); } + + #[test] + fn service_lifecycle_actions_are_typed_and_medium_risk() { + for (json, expected) in [ + ( + r#"{"action":"start_service","name":"sshd"}"#, + SystemAction::StartService { + name: "sshd".into(), + }, + ), + ( + r#"{"action":"stop_service","name":"sshd"}"#, + SystemAction::StopService { + name: "sshd".into(), + }, + ), + ( + r#"{"action":"restart_service","name":"sshd.service"}"#, + SystemAction::RestartService { + name: "sshd.service".into(), + }, + ), + ] { + let action = parse_action(json).unwrap(); + assert_eq!(action, expected); + assert!(action.is_mutating()); + assert_eq!(action.risk(), Risk::Medium); + } + assert!(parse_action(r#"{"action":"start_service","name":"--now"}"#).is_err()); + assert_eq!( + parse_natural("restart sshd"), + Some(SystemAction::RestartService { + name: "sshd".into() + }) + ); + assert_eq!( + parse_natural("stop bluetooth"), + Some(SystemAction::StopService { + name: "bluetooth".into() + }) + ); + } + + /// The manual (natural), JSON and AI entry paths all reject an option-like + /// operand, so no entry path can smuggle an option into a command. + #[test] + fn option_like_operands_are_rejected_on_every_entry_path() { + // Manual path: the deterministic parser validates its own output. + assert_eq!(parse_natural("install --color=n"), None); + assert_eq!(parse_natural("enable --now"), None); + assert_eq!(parse_natural("search -C"), None); + assert_eq!(parse_natural("start --force"), None); + + // JSON path. + assert!(parse_action(r#"{"action":"install_package","atom":"--color=n"}"#).is_err()); + assert!(parse_action(r#"{"action":"enable_service","name":"--now"}"#).is_err()); + assert!(parse_action(r#"{"action":"search_packages","query":"-C"}"#).is_err()); + assert!(parse_action(r#"{"action":"install_package","atom":"app/--evil"}"#).is_err()); + + // The AI plan parser runs this same `validate` (covered in + // `phoebus-ai::assistant::tests`). + } + + #[test] + fn case_insensitive_prefix_never_panics_on_multibyte() { + assert_eq!( + strip_prefix_ascii_ci("FLATPAK install x", "flatpak "), + Some("install x") + ); + assert_eq!(strip_prefix_ascii_ci("notaflatpak", "flatpak "), None); + // The split point lands inside a multibyte character: no panic. + assert_eq!(strip_prefix_ascii_ci("aaa€€", "flatpak "), None); + assert_eq!(parse_natural("aaa€€ install x"), None); + } + + #[test] + fn natural_flatpak_nix_and_snapshot_phrases() { + // Flatpak app ids are case-sensitive and must survive parsing. + assert_eq!( + parse_natural("flatpak install org.chromium.Chromium"), + Some(SystemAction::FlatpakInstall { + app_id: "org.chromium.Chromium".into() + }) + ); + assert_eq!( + parse_natural("flatpak remove org.chromium.Chromium"), + Some(SystemAction::FlatpakRemove { + app_id: "org.chromium.Chromium".into() + }) + ); + assert_eq!( + parse_natural("flatpak list"), + Some(SystemAction::FlatpakList) + ); + assert_eq!( + parse_natural("nix prepare nodejs python312"), + Some(SystemAction::NixPrepare { + packages: vec!["nodejs".into(), "python312".into()] + }) + ); + assert_eq!( + parse_natural("snapshot before update"), + Some(SystemAction::CreateSnapshot { + operation: "before update".into() + }) + ); + assert_eq!( + parse_natural("snapshot"), + Some(SystemAction::CreateSnapshot { + operation: "manual snapshot".into() + }) + ); + } } diff --git a/installer-rs/crates/phoebus-core/src/effects.rs b/installer-rs/crates/phoebus-core/src/effects.rs index 5cec74e..b37bf48 100644 --- a/installer-rs/crates/phoebus-core/src/effects.rs +++ b/installer-rs/crates/phoebus-core/src/effects.rs @@ -59,6 +59,12 @@ pub struct ResolvedPlan { pub consequences: Vec, /// Stable digest of the effect list, used to bind approval to the review. pub digest: String, + /// Whether the effect set was actually established from the package + /// manager. A genuinely empty plan (`0 packages`) is `true` with no + /// effects; output that could not be recognised is `false`, and callers + /// must refuse to proceed rather than treat it as "nothing to do". + #[serde(default)] + pub established: bool, } impl ResolvedPlan { @@ -276,6 +282,12 @@ pub fn parse_emerge_pretend(text: &str) -> ResolvedPlan { source, }); } + // Distinguish a real "0 packages" plan from output we could not parse. A + // Portage pretend run always prints a recognisable header or totals line. + plan.established = !plan.effects.is_empty() + || text.contains("These are the packages that would be merged") + || text.contains("Calculating dependencies") + || text.lines().any(|l| l.trim_start().starts_with("Total:")); plan.download_bytes = parse_download_bytes(text); if plan.removals() > 0 { plan.consequences @@ -368,12 +380,29 @@ Total: 5 packages (2 upgrades, 2 new, 1 uninstall), Size of downloads: 10,240 Ki fn empty_output_is_a_no_change_plan() { let plan = parse_emerge_pretend("Calculating dependencies... done!\n\nTotal: 0 packages\n"); assert!(plan.effects.is_empty()); + assert!(plan.established, "a real 0-package plan is established"); assert_eq!(plan.additions(), 0); assert!(plan.render().contains("no package changes")); // The digest is still defined. assert_eq!(plan.digest.len(), 64); } + #[test] + fn unparseable_output_is_not_an_empty_plan() { + // A genuinely empty plan and output we cannot parse must be + // distinguishable: the latter has `established == false`. + let garbage = parse_emerge_pretend("something went sideways\nno totals here\n"); + assert!(garbage.effects.is_empty()); + assert!( + !garbage.established, + "unrecognised output must not masquerade as 0 packages" + ); + + let real = parse_emerge_pretend(PRETEND); + assert!(real.established); + assert!(!real.effects.is_empty()); + } + #[test] fn render_states_source_binary_and_consequences() { let plan = parse_emerge_pretend(PRETEND); diff --git a/installer-rs/crates/phoebus-core/src/transaction.rs b/installer-rs/crates/phoebus-core/src/transaction.rs index 58d1804..e189180 100644 --- a/installer-rs/crates/phoebus-core/src/transaction.rs +++ b/installer-rs/crates/phoebus-core/src/transaction.rs @@ -18,7 +18,7 @@ pub struct TransactionRecord { pub origin: String, /// A short human-readable summary. pub summary: String, - /// The validated typed actions, rendered. + /// The validated typed actions that were attempted, rendered. pub actions: Vec, /// `ok` or a failure description. pub result: String, @@ -26,6 +26,54 @@ pub struct TransactionRecord { pub snapshot: Option, /// The reviewed plan digest, when effects were resolved. pub digest: Option, + /// Per-action outcomes in execution order: what completed, what failed and + /// what was never attempted (because an earlier action aborted the plan). + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub outcomes: Vec, +} + +/// The status of one action within a recorded transaction. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ActionStatus { + Completed, + Failed, + Unattempted, +} + +/// One action's outcome. `detail` carries the failure reason for `failed`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct TransactionOutcome { + pub action: String, + pub status: ActionStatus, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub detail: Option, +} + +impl TransactionOutcome { + pub fn completed(action: impl Into) -> Self { + Self { + action: action.into(), + status: ActionStatus::Completed, + detail: None, + } + } + + pub fn failed(action: impl Into, detail: impl Into) -> Self { + Self { + action: action.into(), + status: ActionStatus::Failed, + detail: Some(detail.into()), + } + } + + pub fn unattempted(action: impl Into) -> Self { + Self { + action: action.into(), + status: ActionStatus::Unattempted, + detail: None, + } + } } impl Default for TransactionRecord { @@ -38,10 +86,28 @@ impl Default for TransactionRecord { result: "ok".into(), snapshot: None, digest: None, + outcomes: Vec::new(), } } } +impl TransactionRecord { + /// `(completed, failed, unattempted)` counts for the recorded outcomes. + pub fn outcome_counts(&self) -> (usize, usize, usize) { + let mut completed = 0; + let mut failed = 0; + let mut unattempted = 0; + for outcome in &self.outcomes { + match outcome.status { + ActionStatus::Completed => completed += 1, + ActionStatus::Failed => failed += 1, + ActionStatus::Unattempted => unattempted += 1, + } + } + (completed, failed, unattempted) + } +} + #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct TransactionLog { @@ -92,7 +158,20 @@ impl TransactionLog { if let Some(snap) = &r.snapshot { out.push_str(&format!(" [snapshot {snap}]")); } + if !r.outcomes.is_empty() { + let (done, failed, skipped) = r.outcome_counts(); + out.push_str(&format!( + " [{done} completed, {failed} failed, {skipped} unattempted]" + )); + } out.push('\n'); + for outcome in &r.outcomes { + out.push_str(&format!(" - {:?} {}", outcome.status, outcome.action)); + if let Some(detail) = &outcome.detail { + out.push_str(&format!(": {detail}")); + } + out.push('\n'); + } } out } @@ -115,6 +194,7 @@ mod tests { result: "ok".into(), snapshot: Some("@-x".into()), digest: Some("abc".into()), + outcomes: Vec::new(), }); log.save(&path).unwrap(); let loaded = TransactionLog::load(&path).unwrap(); @@ -135,6 +215,50 @@ mod tests { assert!(TransactionLog::load(&path).is_err()); } + #[test] + fn outcomes_roundtrip_and_render() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("transactions.toml"); + let mut log = TransactionLog::default(); + log.append(TransactionRecord { + created_unix: 20, + origin: "assistant".into(), + summary: "install two".into(), + actions: vec!["install package a".into(), "install package b".into()], + result: "failed: boom".into(), + snapshot: Some("@-snap".into()), + digest: None, + outcomes: vec![ + TransactionOutcome::completed("install package a"), + TransactionOutcome::failed("install package b", "boom"), + TransactionOutcome::unattempted("enable service c"), + ], + }); + log.save(&path).unwrap(); + let loaded = TransactionLog::load(&path).unwrap(); + assert_eq!(loaded, log); + assert_eq!(loaded.records[0].outcome_counts(), (1, 1, 1)); + let text = loaded.render(); + assert!(text.contains("1 completed, 1 failed, 1 unattempted")); + assert!(text.contains("Unattempted")); + } + + #[test] + fn records_without_outcomes_still_load() { + // Backward compatibility: an older log has no `outcomes` key. + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("old.toml"); + std::fs::write( + &path, + "[[records]]\ncreated_unix = 1\norigin = \"sys\"\nsummary = \"x\"\nresult = \"ok\"\n", + ) + .unwrap(); + let log = TransactionLog::load(&path).unwrap(); + assert_eq!(log.records.len(), 1); + assert!(log.records[0].outcomes.is_empty()); + assert_eq!(log.records[0].outcome_counts(), (0, 0, 0)); + } + #[test] fn log_is_bounded() { let mut log = TransactionLog::default(); diff --git a/installer-rs/crates/phoebus-executor/src/actions.rs b/installer-rs/crates/phoebus-executor/src/actions.rs index 154eb4b..69d877b 100644 --- a/installer-rs/crates/phoebus-executor/src/actions.rs +++ b/installer-rs/crates/phoebus-executor/src/actions.rs @@ -7,11 +7,13 @@ use std::path::Path; use phoebus_core::action::SystemAction; use phoebus_core::effects::{changed_materially, parse_emerge_pretend, ResolvedPlan}; +use phoebus_core::transaction::TransactionOutcome; use crate::auth::DestructiveAuthorization; -use crate::command::CommandSpec; +use crate::command::{CommandOutput, CommandSpec}; use crate::error::{ExecError, Result}; use crate::exec::Exec; +use crate::portage; use crate::snapshot; #[derive(Debug)] @@ -33,17 +35,105 @@ pub fn preview(action: &SystemAction) -> String { out } +/// Whether the command acts on the running system (`/`) rather than a mounted +/// alternate target. +fn is_host_root(root: &Path) -> bool { + root == Path::new("/") +} + +/// Build a command that acts on the intended system: directly for the running +/// root, or inside the target chroot for an alternate root. This is the single +/// place package, service and Flatpak commands are rooted, so preview and +/// execution always target the same system. +fn target_spec( + root: &Path, + stage: &str, + operation: &str, + program: &str, + args: &[String], +) -> CommandSpec { + if is_host_root(root) { + CommandSpec::new(stage, operation, program).args(args.iter().cloned()) + } else { + portage::chroot_spec(stage, operation, root, program, args) + } +} + +/// Refuse an operation that genuinely needs the running system (e.g. service +/// status, start, stop, restart) when an alternate root was requested. This +/// runs before any command is built, so nothing is invoked against the host. +fn require_host_root(root: &Path, operation: &str) -> Result<()> { + if is_host_root(root) { + Ok(()) + } else { + Err(ExecError::operation( + "sys", + operation, + format!( + "cannot target the alternate root {}; this operation needs the \ + running system", + root.display() + ), + )) + } +} + +/// Run a mutating command against the intended system. +fn run_in_target( + exec: &Exec, + root: &Path, + stage: &str, + operation: &str, + program: &str, + args: &[String], +) -> Result { + exec.run(target_spec(root, stage, operation, program, args)) +} + +/// Capture a read-only command against the intended system. Output is returned +/// regardless of exit status (inspection commands report state, not failure). +fn capture_in_target( + exec: &Exec, + root: &Path, + stage: &str, + operation: &str, + program: &str, + args: &[String], +) -> Result { + exec.runner + .execute(&target_spec(root, stage, operation, program, args)) +} + +/// `systemctl` for boot enable/disable. On the host it acts on PID 1; on an +/// alternate root it uses `--root`, which only writes symlinks under the +/// target, so the host's service state is never touched. +fn systemctl_spec(root: &Path, operation: &str, verb: &str, name: &str) -> CommandSpec { + if is_host_root(root) { + CommandSpec::new("sys", operation, "systemctl").args([verb, name]) + } else { + CommandSpec::new("sys", operation, "systemctl") + .arg("--root") + .arg(root.to_string_lossy().to_string()) + .arg(verb) + .arg(name) + } +} + /// The Portage arguments that produce a resolved pretend plan for an action, /// or `None` when the action is not a Portage transaction. fn pretend_args(action: &SystemAction) -> Option> { match action { - SystemAction::InstallPackage { atom } => { - Some(vec!["--pretend".into(), "--color=n".into(), atom.clone()]) - } + SystemAction::InstallPackage { atom } => Some(vec![ + "--pretend".into(), + "--color=n".into(), + "--".into(), + atom.clone(), + ]), SystemAction::RemovePackage { atom } => Some(vec![ "--pretend".into(), "--unmerge".into(), "--color=n".into(), + "--".into(), atom.clone(), ]), SystemAction::UpdateSystem => Some(vec![ @@ -52,6 +142,7 @@ fn pretend_args(action: &SystemAction) -> Option> { "--deep".into(), "--newuse".into(), "--color=n".into(), + "--".into(), "@world".into(), ]), _ => None, @@ -61,17 +152,25 @@ fn pretend_args(action: &SystemAction) -> Option> { /// Resolve exactly what a package action will do. Non-Portage actions yield an /// empty plan. A plan's digest can be bound to an approval. pub fn resolve_effects(exec: &Exec, action: &SystemAction, root: &Path) -> Result { + action + .validate() + .map_err(|e| ExecError::operation("sys", "validate action", e))?; let Some(args) = pretend_args(action) else { return Ok(ResolvedPlan::default()); }; - let out = if root == Path::new("/") { - exec.run_capture(CommandSpec::new("sys", "resolve effects", "emerge").args(args))? - } else { - let mut chroot_args = vec![root.to_string_lossy().to_string(), "emerge".to_string()]; - chroot_args.extend(args); - exec.run_capture(CommandSpec::new("sys", "resolve effects", "chroot").args(chroot_args))? - }; + // Same rooting as execution, so the reviewed plan describes the action that + // will actually run. + let spec = target_spec(root, "sys", "resolve effects", "emerge", &args); + let out = exec.run_capture(spec)?; let mut plan = parse_emerge_pretend(&out.stdout_lossy()); + if !plan.established { + return Err(ExecError::operation( + "sys", + "resolve effects", + "could not establish the package plan from emerge --pretend; \ + refusing to proceed unreviewed", + )); + } if root.join(snapshot::SNAPSHOT_DIR).is_dir() { plan.recovery_point = Some("a protective snapshot before the change".into()); } @@ -105,12 +204,75 @@ pub fn execute_checked( execute(exec, action, auth, root) } +/// The result of running a reviewed multi-action plan. +#[derive(Debug, Default)] +pub struct PlanOutcome { + /// `(action, output)` for actions that completed, in execution order. + pub completed: Vec<(String, String)>, + /// `(action, error)` for the action that failed, if any. + pub failure: Option<(String, String)>, + /// Every action's status in order, including the ones never attempted + /// because an earlier action aborted the plan. + pub outcomes: Vec, + /// The most recent protective snapshot reported by an action. + pub snapshot: Option, +} + +/// Execute a reviewed plan action by action, stopping at the first failure and +/// recording later actions as unattempted. This is the single execution path +/// shared by the assistant and any multi-step caller, so partial failure is +/// reported truthfully rather than as success. +pub fn execute_plan( + exec: &Exec, + steps: &[(SystemAction, ResolvedPlan)], + auth: Option<&DestructiveAuthorization>, + root: &Path, +) -> PlanOutcome { + let mut outcome = PlanOutcome::default(); + for (action, resolved) in steps { + let described = action.describe(); + if outcome.failure.is_some() { + outcome + .outcomes + .push(TransactionOutcome::unattempted(described)); + continue; + } + match execute_checked(exec, action, auth, root, Some(resolved)) { + Ok(result) => { + if result.snapshot.is_some() { + outcome.snapshot = result.snapshot; + } + outcome + .outcomes + .push(TransactionOutcome::completed(described.clone())); + outcome.completed.push((described, result.output)); + } + Err(e) => { + let detail = e.to_string(); + outcome.outcomes.push(TransactionOutcome::failed( + described.clone(), + detail.clone(), + )); + outcome.failure = Some((described, detail)); + } + } + } + outcome +} + pub fn execute( exec: &Exec, action: &SystemAction, auth: Option<&DestructiveAuthorization>, root: &Path, ) -> 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 + // command. + action + .validate() + .map_err(|e| ExecError::operation("sys", "validate action", e))?; + if action.is_mutating() && auth.is_none() { return Err(ExecError::NotAuthorized(action.describe())); } @@ -136,14 +298,22 @@ pub fn execute( }); } + let mut snapshot_id = None; + // A protective snapshot must exist before a high-risk change. If the // snapshots subvolume is present but the snapshot cannot be created, fail // closed rather than proceed without a recovery point. A system without a // `.snapshots` subvolume (e.g. a non-Btrfs root) is a warning, not a block. + // The snapshot source is the target root itself, so an alternate root + // snapshots the target, never the host. if action.risk() == phoebus_core::action::Risk::High && !exec.dry_run { if root.join(snapshot::SNAPSHOT_DIR).is_dir() { let auth = auth.ok_or_else(|| ExecError::NotAuthorized(action.describe()))?; - snapshot::create_and_record(exec, auth, root, "/", &action.describe())?; + let source = root.to_string_lossy().to_string(); + let record = + snapshot::create_and_record(exec, auth, root, &source, &action.describe())?; + // Preserve the recovery point so callers can record it. + snapshot_id = Some(record.id); } else { exec.logger.warn(format!( "no {}/ subvolume; proceeding without a protective snapshot", @@ -152,82 +322,147 @@ pub fn execute( } } - let mut snapshot_id = None; let output = match action { SystemAction::InspectService { name } => { - let out = exec.runner.execute( - &CommandSpec::new("sys", "inspect service", "systemctl").args([ - "status", - name, - "--no-pager", - ]), + require_host_root(root, "inspect service")?; + let out = capture_in_target( + exec, + root, + "sys", + "inspect service", + "systemctl", + &["status".into(), name.clone(), "--no-pager".into()], )?; format!("{}\n{}", out.stdout_lossy(), out.stderr_lossy()) } SystemAction::EnableService { name } => { - exec.run( - CommandSpec::new("sys", "enable service", "systemctl").args(["enable", name]), - )?; + let spec = systemctl_spec(root, "enable service", "enable", name); + exec.run(spec)?; format!("enabled {name}") } SystemAction::DisableService { name } => { - exec.run( - CommandSpec::new("sys", "disable service", "systemctl").args(["disable", name]), - )?; + let spec = systemctl_spec(root, "disable service", "disable", name); + exec.run(spec)?; format!("disabled {name}") } + SystemAction::StartService { name } => { + require_host_root(root, "start service")?; + let spec = systemctl_spec(root, "start service", "start", name); + exec.run(spec)?; + format!("started {name}") + } + SystemAction::StopService { name } => { + require_host_root(root, "stop service")?; + let spec = systemctl_spec(root, "stop service", "stop", name); + exec.run(spec)?; + format!("stopped {name}") + } + SystemAction::RestartService { name } => { + require_host_root(root, "restart service")?; + let spec = systemctl_spec(root, "restart service", "restart", name); + exec.run(spec)?; + format!("restarted {name}") + } SystemAction::InspectPackage { atom } => { - let out = exec.runner.execute( - &CommandSpec::new("sys", "inspect package", "emerge").args([ - "--pretend", - "--quiet", - atom, - ]), + let out = capture_in_target( + exec, + root, + "sys", + "inspect package", + "emerge", + &[ + "--pretend".into(), + "--quiet".into(), + "--".into(), + atom.clone(), + ], )?; format!("{}\n{}", out.stdout_lossy(), out.stderr_lossy()) } SystemAction::InstallPackage { atom } => { // Portage resolves a bare name (`vim`) or a full atom. - exec.run(CommandSpec::new("sys", "install package", "emerge").args(["--ask=n", atom]))?; + run_in_target( + exec, + root, + "sys", + "install package", + "emerge", + &["--ask=n".into(), "--".into(), atom.clone()], + )?; format!("installed {atom} (Portage)") } SystemAction::RemovePackage { atom } => { - exec.run(CommandSpec::new("sys", "remove package", "emerge").args([ - "--unmerge", - "--ask=n", - atom, - ]))?; + run_in_target( + exec, + root, + "sys", + "remove package", + "emerge", + &[ + "--unmerge".into(), + "--ask=n".into(), + "--".into(), + atom.clone(), + ], + )?; format!("removed {atom} (Portage)") } SystemAction::SearchPackages { query } => { - let out = exec.runner.execute( - &CommandSpec::new("sys", "search packages", "emerge") - .args(["--search", "--quiet", query]), + let out = capture_in_target( + exec, + root, + "sys", + "search packages", + "emerge", + &[ + "--search".into(), + "--quiet".into(), + "--".into(), + query.clone(), + ], )?; format!("{}\n{}", out.stdout_lossy(), out.stderr_lossy()) } SystemAction::FlatpakInstall { app_id } => { - exec.run( - CommandSpec::new("sys", "install flatpak", "flatpak") - .args(["install", "-y", "flathub", app_id]), + run_in_target( + exec, + root, + "sys", + "install flatpak", + "flatpak", + &[ + "install".into(), + "-y".into(), + "--".into(), + "flathub".into(), + app_id.clone(), + ], )?; format!("installed Flatpak app {app_id}") } SystemAction::FlatpakRemove { app_id } => { - exec.run(CommandSpec::new("sys", "remove flatpak", "flatpak").args([ - "uninstall", - "-y", - app_id, - ]))?; + run_in_target( + exec, + root, + "sys", + "remove flatpak", + "flatpak", + &["uninstall".into(), "-y".into(), "--".into(), app_id.clone()], + )?; format!("removed Flatpak app {app_id}") } SystemAction::FlatpakList => { - let out = exec.runner.execute( - &CommandSpec::new("sys", "list flatpaks", "flatpak").args([ - "list", - "--app", - "--columns=application,version,branch", - ]), + let out = capture_in_target( + exec, + root, + "sys", + "list flatpaks", + "flatpak", + &[ + "list".into(), + "--app".into(), + "--columns=application,version,branch".into(), + ], )?; let text = format!("{}\n{}", out.stdout_lossy(), out.stderr_lossy()); if text.trim().is_empty() { @@ -241,15 +476,26 @@ pub fn execute( format!("realised Nix development packages: {}", prepared.join(", ")) } SystemAction::UpdateSystem => { - exec.run( - CommandSpec::new("sys", "update system", "emerge") - .args(["--update", "--deep", "--newuse", "@world"]), + run_in_target( + exec, + root, + "sys", + "update system", + "emerge", + &[ + "--update".into(), + "--deep".into(), + "--newuse".into(), + "--".into(), + "@world".into(), + ], )?; "system update complete".to_string() } SystemAction::CreateSnapshot { operation } => { - let auth = auth.expect("mutating action has auth"); - let record = snapshot::create_and_record(exec, auth, root, "/", operation)?; + let auth = auth.ok_or_else(|| ExecError::NotAuthorized(action.describe()))?; + let source = root.to_string_lossy().to_string(); + let record = snapshot::create_and_record(exec, auth, root, &source, operation)?; snapshot_id = Some(record.id.clone()); format!("snapshot created: {}", record.id) } @@ -311,7 +557,27 @@ mod tests { use super::*; use crate::command::RecordingRunner; use crate::log::Logger; + use phoebus_core::transaction::ActionStatus; use phoebus_core::Redactor; + use std::time::Duration; + + const PRETEND_VIM: &str = "\ +These are the packages that would be merged, in order: + +Calculating dependencies... done! +[ebuild N ] app-editors/vim-9.1.0 + +Total: 1 package (1 new), Size of downloads: 1,024 KiB +"; + + const PRETEND_WGET: &str = "\ +These are the packages that would be merged, in order: + +Calculating dependencies... done! +[ebuild N ] net-misc/wget-1.24.5 + +Total: 1 package (1 new), Size of downloads: 512 KiB +"; fn make_exec(runner: RecordingRunner) -> Exec { let path = tempfile::tempdir().unwrap().keep(); @@ -319,6 +585,27 @@ mod tests { Exec::new(Box::new(runner), logger, false, path.join("root"), path) } + fn auth() -> DestructiveAuthorization { + crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap() + } + + fn response(stdout: &str) -> CommandOutput { + CommandOutput { + status: 0, + success: true, + stdout: stdout.as_bytes().to_vec(), + stderr: Vec::new(), + duration: Duration::from_millis(1), + } + } + + fn emerge_calls(specs: &[CommandSpec]) -> Vec<&CommandSpec> { + specs + .iter() + .filter(|s| s.program == "emerge" || s.args.iter().any(|a| a == "emerge")) + .collect() + } + #[test] fn mutating_action_requires_authorization() { let runner = RecordingRunner::new(); @@ -345,7 +632,7 @@ mod tests { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); std::fs::create_dir_all(exec.root.join(snapshot::SNAPSHOT_DIR)).unwrap(); - let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); + let auth = auth(); execute( &exec, &SystemAction::UpdateSystem, @@ -353,25 +640,46 @@ mod tests { Path::new(&exec.root), ) .unwrap(); - let programs = runner.programs(); - let btrfs = programs + let specs = runner.recorded(); + let btrfs = specs .iter() - .position(|p| p == "btrfs") + .position(|s| s.program == "btrfs") .expect("snapshot"); - let emerge = programs.iter().position(|p| p == "emerge").expect("emerge"); + let emerge = specs + .iter() + .position(|s| s.program == "emerge" || s.args.iter().any(|a| a == "emerge")) + .expect("emerge"); assert!( btrfs < emerge, - "protective snapshot must precede the mutation: {programs:?}" + "protective snapshot must precede the mutation: {:?}", + runner.programs() ); } + #[test] + fn protective_snapshot_id_is_preserved_in_the_outcome() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + std::fs::create_dir_all(exec.root.join(snapshot::SNAPSHOT_DIR)).unwrap(); + let auth = auth(); + let outcome = execute( + &exec, + &SystemAction::UpdateSystem, + Some(&auth), + Path::new(&exec.root), + ) + .unwrap(); + // The generic protective-snapshot path must report its id, not drop it. + assert!(outcome.snapshot.is_some(), "protective snapshot id lost"); + } + #[test] fn high_risk_action_aborts_when_protective_snapshot_fails() { let runner = RecordingRunner::new(); runner.fail("btrfs", "snapshot failed"); let exec = make_exec(runner.clone()); std::fs::create_dir_all(exec.root.join(snapshot::SNAPSHOT_DIR)).unwrap(); - let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); + let auth = auth(); let err = execute( &exec, &SystemAction::UpdateSystem, @@ -402,13 +710,15 @@ mod tests { fn remove_package_builds_unmerge() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); - let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); + let auth = auth(); let action = SystemAction::RemovePackage { atom: "vim".into() }; execute(&exec, &action, Some(&auth), Path::new(&exec.root)).unwrap(); - let spec = &runner.recorded()[0]; - assert_eq!(spec.program, "emerge"); + let recorded = runner.recorded(); + let spec = emerge_calls(&recorded)[0]; assert!(spec.args.contains(&"--unmerge".to_string())); assert!(spec.args.contains(&"vim".to_string())); + // The operand follows the option terminator. + assert!(spec.args.windows(2).any(|w| w[0] == "--" && w[1] == "vim")); } #[test] @@ -419,26 +729,232 @@ mod tests { query: "firefox".into(), }; execute(&exec, &action, None, Path::new(&exec.root)).unwrap(); - let spec = &runner.recorded()[0]; - assert_eq!(spec.program, "emerge"); + let recorded = runner.recorded(); + let spec = emerge_calls(&recorded)[0]; assert!(spec.args.contains(&"--search".to_string())); assert!(spec.args.contains(&"firefox".to_string())); + assert!(spec + .args + .windows(2) + .any(|w| w[0] == "--" && w[1] == "firefox")); } #[test] fn enable_builds_separate_arguments() { let runner = RecordingRunner::new(); let exec = make_exec(runner.clone()); - let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap(); let action = SystemAction::EnableService { name: "sshd".into(), }; - execute(&exec, &action, Some(&auth), Path::new("/")).unwrap(); + execute(&exec, &action, Some(&auth()), Path::new("/")).unwrap(); let spec = &runner.recorded()[0]; assert_eq!(spec.program, "systemctl"); assert_eq!(spec.args, vec!["enable", "sshd"]); } + #[test] + fn service_lifecycle_runs_on_the_host() { + for (action, verb) in [ + ( + SystemAction::StartService { + name: "sshd".into(), + }, + "start", + ), + ( + SystemAction::StopService { + name: "sshd".into(), + }, + "stop", + ), + ( + SystemAction::RestartService { + name: "sshd".into(), + }, + "restart", + ), + ] { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + execute(&exec, &action, Some(&auth()), Path::new("/")).unwrap(); + let spec = &runner.recorded()[0]; + assert_eq!(spec.program, "systemctl"); + assert_eq!(spec.args, vec![verb.to_string(), "sshd".to_string()]); + } + } + + #[test] + fn service_enable_on_an_alternate_root_uses_root_flag() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + let root = Path::new("/tmp/phoebus-alt"); + execute( + &exec, + &SystemAction::EnableService { + name: "sshd".into(), + }, + Some(&auth()), + root, + ) + .unwrap(); + let spec = &runner.recorded()[0]; + assert_eq!(spec.program, "systemctl"); + assert_eq!( + spec.args, + vec!["--root", "/tmp/phoebus-alt", "enable", "sshd"] + ); + } + + #[test] + fn runtime_service_operations_reject_an_alternate_root() { + let root = Path::new("/tmp/phoebus-alt"); + for action in [ + SystemAction::InspectService { + name: "sshd".into(), + }, + SystemAction::StartService { + name: "sshd".into(), + }, + SystemAction::StopService { + name: "sshd".into(), + }, + SystemAction::RestartService { + name: "sshd".into(), + }, + ] { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + let err = execute(&exec, &action, Some(&auth()), root).unwrap_err(); + assert!( + err.to_string().contains("cannot target the alternate root"), + "got {err}" + ); + // Nothing may be invoked against the host. + assert!(runner.recorded().is_empty(), "{:?}", runner.programs()); + } + } + + #[test] + fn preview_and_execution_use_the_same_alternate_root() { + let runner = RecordingRunner::new(); + runner.set_response("chroot", response(PRETEND_VIM)); + let exec = make_exec(runner.clone()); + let root = Path::new("/mnt/phoebus-target"); + let action = SystemAction::InstallPackage { + atom: "app-editors/vim".into(), + }; + let plan = resolve_effects(&exec, &action, root).unwrap(); + assert!(!plan.effects.is_empty()); + let outcome = execute_checked(&exec, &action, Some(&auth()), root, Some(&plan)).unwrap(); + assert!(outcome.output.contains("installed")); + + let recorded = runner.recorded(); + let calls = emerge_calls(&recorded); + assert!(calls.len() >= 2, "preview + execution: {:?}", calls); + for spec in calls { + assert_eq!(spec.program, "chroot", "execution escaped the target"); + assert_eq!(spec.args[0], "/mnt/phoebus-target"); + } + } + + #[test] + fn execution_validates_before_any_command() { + let runner = RecordingRunner::new(); + let exec = make_exec(runner.clone()); + let action = SystemAction::InstallPackage { + atom: "--color=n".into(), + }; + // The read path refuses too. + assert!(resolve_effects(&exec, &action, Path::new("/")).is_err()); + let err = execute(&exec, &action, Some(&auth()), Path::new("/")).unwrap_err(); + assert!(err.to_string().contains("invalid"), "got {err}"); + assert!(runner.recorded().is_empty()); + } + + #[test] + fn resolve_effects_refuses_unparseable_output() { + let runner = RecordingRunner::new(); + runner.set_response("emerge", response("something went sideways\n")); + let exec = make_exec(runner.clone()); + let action = SystemAction::InstallPackage { + atom: "app-editors/vim".into(), + }; + let err = resolve_effects(&exec, &action, Path::new("/")).unwrap_err(); + assert!(err.to_string().contains("could not establish"), "got {err}"); + } + + #[test] + fn resolve_effects_accepts_a_genuinely_empty_plan() { + let runner = RecordingRunner::new(); + runner.set_response( + "emerge", + response("Calculating dependencies... done!\n\nTotal: 0 packages\n"), + ); + let exec = make_exec(runner.clone()); + let action = SystemAction::InstallPackage { + atom: "app-editors/vim".into(), + }; + let plan = resolve_effects(&exec, &action, Path::new("/")).unwrap(); + assert!(plan.effects.is_empty()); + assert!(plan.established); + } + + #[test] + fn a_material_change_invalidates_the_reviewed_plan() { + let runner = RecordingRunner::new(); + runner.set_response("emerge", response(PRETEND_WGET)); + let exec = make_exec(runner.clone()); + let approved = phoebus_core::effects::parse_emerge_pretend(PRETEND_VIM); + let action = SystemAction::InstallPackage { + atom: "net-misc/wget".into(), + }; + let err = execute_checked( + &exec, + &action, + Some(&auth()), + Path::new("/"), + Some(&approved), + ) + .unwrap_err(); + assert!(err.to_string().contains("plan changed"), "got {err}"); + // The unapproved mutation must not run. + assert!(!runner + .recorded() + .iter() + .any(|s| s.args.iter().any(|a| a == "--ask=n"))); + } + + #[test] + fn plan_stops_at_the_first_failure_and_marks_the_rest_unattempted() { + let runner = RecordingRunner::new(); + runner.fail("emerge", "boom"); + let exec = make_exec(runner.clone()); + let steps = vec![ + (SystemAction::History, ResolvedPlan::default()), + ( + SystemAction::InstallPackage { + atom: "app-editors/vim".into(), + }, + ResolvedPlan::default(), + ), + ( + SystemAction::EnableService { + name: "sshd".into(), + }, + ResolvedPlan::default(), + ), + ]; + let run = execute_plan(&exec, &steps, Some(&auth()), Path::new("/")); + assert_eq!(run.completed.len(), 1); + assert!(run.failure.is_some()); + assert_eq!(run.outcomes.len(), 3); + assert_eq!(run.outcomes[0].status, ActionStatus::Completed); + assert_eq!(run.outcomes[1].status, ActionStatus::Failed); + assert_eq!(run.outcomes[2].status, ActionStatus::Unattempted); + // The third action must never reach the system. + assert!(!runner.programs().iter().any(|p| p == "systemctl")); + } + #[test] fn undo_requires_authorization() { let runner = RecordingRunner::new(); diff --git a/installer-rs/crates/phoebus-executor/src/portage.rs b/installer-rs/crates/phoebus-executor/src/portage.rs index 1e11361..5a430e2 100644 --- a/installer-rs/crates/phoebus-executor/src/portage.rs +++ b/installer-rs/crates/phoebus-executor/src/portage.rs @@ -87,7 +87,9 @@ pub fn select_profile(exec: &Exec, config: &InstallConfig) -> Result<()> { Ok(()) } -fn chroot_spec( +/// Build a `chroot env -i ... ` command. Shared with the `sys` +/// action broker so preview and execution target the same root. +pub(crate) fn chroot_spec( stage: &str, operation: &str, root: &Path, diff --git a/installer-rs/crates/phoebus-installer/src/bin/sys.rs b/installer-rs/crates/phoebus-installer/src/bin/sys.rs index c9dff15..4a88129 100644 --- a/installer-rs/crates/phoebus-installer/src/bin/sys.rs +++ b/installer-rs/crates/phoebus-installer/src/bin/sys.rs @@ -6,14 +6,14 @@ //! `--action-file`; it never receives a shell or the sudo password. use std::io::Write; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::process::ExitCode; use clap::Parser; use phoebus_ai::assistant::{complete_plan, AssistantPlan}; use phoebus_ai::provider::{ModelTransport, ProviderKind, SystemFacts}; use phoebus_core::action::{parse_action, parse_natural, SystemAction}; -use phoebus_core::transaction::TransactionRecord; +use phoebus_core::transaction::{TransactionOutcome, TransactionRecord}; use phoebus_core::{AiBackend, InstallConfig, Redactor}; use phoebus_executor::opencode_client::ExecOpenCodeTransport; use phoebus_executor::{ @@ -114,6 +114,26 @@ impl Confirmation for SysConfirm { } } +fn now_unix() -> u64 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0) +} + +/// Append a transaction and make a recording failure visible instead of +/// silently claiming success. The system change (if any) has already happened, +/// so the message says so explicitly. +fn record_transaction( + exec: &Exec, + root: &Path, + record: TransactionRecord, +) -> std::result::Result<(), Box> { + transaction::append(exec, root, record).map_err(|e| { + format!("the action completed but its transaction could not be recorded: {e}").into() + }) +} + fn effective_uid() -> u32 { std::process::Command::new("id") .arg("-u") @@ -222,6 +242,23 @@ fn run() -> std::result::Result<(), Box> { println!("protective snapshot: {id}"); } println!("{}", outcome.output); + + // Ordinary mutating commands are part of the same audited history as + // assistant actions, including their protective snapshot and outcome. + if action.is_mutating() { + let described = action.describe(); + let record = TransactionRecord { + created_unix: now_unix(), + origin: "sys".into(), + summary: described.clone(), + actions: vec![described.clone()], + result: "ok".into(), + snapshot: outcome.snapshot.clone(), + digest: approved.as_ref().map(|p| p.digest.clone()), + outcomes: vec![TransactionOutcome::completed(described)], + }; + record_transaction(&exec, &cli.root, record)?; + } Ok(()) } @@ -443,44 +480,45 @@ fn run_ask( None }; - let mut snapshot = None; - let mut result = String::from("ok"); - let mut executed = Vec::new(); - for (action, resolved) in plan.actions.iter().zip(approved.iter()) { - match actions::execute_checked(exec, action, auth.as_ref(), &cli.root, Some(resolved)) { - Ok(outcome) => { - if let Some(id) = &outcome.snapshot { - snapshot = Some(id.clone()); - } - executed.push(action.describe()); - println!("{}", outcome.output); - } - Err(e) => { - result = format!("failed: {e}"); - executed.push(action.describe()); - eprintln!("action failed: {e}"); - break; - } - } + // The reviewed plans and actions are executed through the shared, + // failure-aware plan runner: it stops at the first failure and records the + // remaining actions as unattempted. + let digest = approved + .iter() + .find(|p| !p.digest.is_empty()) + .map(|p| p.digest.clone()); + let steps: Vec<(SystemAction, phoebus_core::effects::ResolvedPlan)> = + plan.actions.iter().cloned().zip(approved).collect(); + let run = actions::execute_plan(exec, &steps, auth.as_ref(), &cli.root); + for (_, output) in &run.completed { + println!("{output}"); + } + let mut attempted: Vec = run.completed.iter().map(|(a, _)| a.clone()).collect(); + if let Some((action, detail)) = &run.failure { + eprintln!("action failed: {action}: {detail}"); + attempted.push(action.clone()); } + let result = match &run.failure { + None => "ok".to_string(), + Some((action, detail)) => format!("failed: {action}: {detail}"), + }; let record = TransactionRecord { - created_unix: std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_secs()) - .unwrap_or(0), + created_unix: now_unix(), origin: "assistant".into(), summary: plan.summary.clone(), - actions: executed, + actions: attempted, result, - snapshot, - digest: approved - .iter() - .find(|p| !p.digest.is_empty()) - .map(|p| p.digest.clone()), + snapshot: run.snapshot, + digest, + outcomes: run.outcomes, }; - if let Err(e) = transaction::append(exec, &cli.root, record) { - eprintln!("could not record the transaction: {e}"); + record_transaction(exec, &cli.root, record)?; + + // A plan that did not fully apply is a failure: nonzero exit, so scripts + // and the GUI cannot mistake it for success. + if let Some((action, detail)) = run.failure { + return Err(format!("the assistant plan did not complete: {action}: {detail}").into()); } Ok(()) } diff --git a/installer-rs/crates/phoebus-installer/tests/sys_cli.rs b/installer-rs/crates/phoebus-installer/tests/sys_cli.rs new file mode 100644 index 0000000..40a2fd3 --- /dev/null +++ b/installer-rs/crates/phoebus-installer/tests/sys_cli.rs @@ -0,0 +1,59 @@ +//! CLI-level regression tests for the `sys` entry paths. +//! +//! These exercise the real binary so the deterministic (manual) and JSON paths +//! are proven to fail closed on invalid input and to exit nonzero on failure. +//! They never mutate the system: the invalid cases are refused before any +//! command is built, and the read-only case targets a temporary root. + +use std::process::Command; + +fn sys() -> Command { + Command::new(env!("CARGO_BIN_EXE_sys")) +} + +#[test] +fn natural_option_like_operand_is_rejected() { + // `--` stops clap option parsing, so the operand reaches the parser intact. + let out = sys() + .args(["--", "install", "--color=n"]) + .output() + .expect("run sys"); + assert!(!out.status.success()); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!( + stderr.contains("could not parse") || stderr.contains("invalid"), + "stderr: {stderr}" + ); +} + +#[test] +fn json_option_like_operand_is_rejected() { + let out = sys() + .args(["--action", r#"{"action":"install_package","atom":"-C"}"#]) + .output() + .expect("run sys"); + assert!(!out.status.success()); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!(stderr.contains("invalid"), "stderr: {stderr}"); +} + +#[test] +fn read_only_history_succeeds_against_a_temporary_root() { + let root = tempfile::tempdir().expect("temp root"); + let out = sys() + .arg("--root") + .arg(root.path()) + .arg("history") + .output() + .expect("run sys"); + assert!( + out.status.success(), + "stderr: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stdout = String::from_utf8_lossy(&out.stdout); + assert!( + stdout.contains("no snapshot history") || stdout.contains("snapshots:"), + "stdout: {stdout}" + ); +}