A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
kernel: degrade read-only kernel venvs instead of raw EACCES (#3633)
Sebastian Müller01e022a3f4be
diff --git a/.changes/kernel-venv-readonly-degrade.md b/.changes/kernel-venv-readonly-degrade.mdnew file mode 100644index 0000000000..5cd0bde8e4--- /dev/null+++ b/.changes/kernel-venv-readonly-degrade.md@@ -0,0 +1,2 @@+- Kernel startup no longer fails on a raw EACCES when the kernel venv's directory is not writable by the current user: a venv that still runs the runtime yields its python with the missing Python skills disabled behind the unavailable-skill notice, and a venv that cannot run the runtime errors naming the venv and the `PRIME_AGENT_KERNEL_VENV` / `PRIME_AGENT_KERNEL_PYTHON` overrides.+diff --git a/crates/pa-core/src/kernel/bootstrap/dir_lock.rs b/crates/pa-core/src/kernel/bootstrap/dir_lock.rsindex 38b13da783..94d8fa3a59 100644--- a/crates/pa-core/src/kernel/bootstrap/dir_lock.rs+++ b/crates/pa-core/src/kernel/bootstrap/dir_lock.rs@@ -113,6 +113,20 @@ fn lock_missing_pid_is_stale(lock_path: &Path) -> bool {.is_ok_and(|age| age.as_millis() as u64 > BOOTSTRAP_LOCK_STALE_WITHOUT_PID_MS)}+/// Whether the bootstrap lock could not be created because the venv's directory+/// denies writes to this user (`EACCES`/`EROFS` on the lock candidate).+pub(crate) fn lock_creation_denied(error: &anyhow::Error) -> bool {+ error+ .root_cause()+ .downcast_ref::<std::io::Error>()+ .is_some_and(|io_error| {+ matches!(+ io_error.kind(),+ std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::ReadOnlyFilesystem+ )+ })+}+/// Serialize concurrent bootstraps across processes on the same venv.pub(crate) async fn acquire_bootstrap_lock(venv: &Path) -> anyhow::Result<impl Drop> {let lock_dir = venv.with_file_name(format!(diff --git a/crates/pa-core/src/kernel/bootstrap/mod.rs b/crates/pa-core/src/kernel/bootstrap/mod.rsindex 25ba0f52ad..7fe34e4236 100644--- a/crates/pa-core/src/kernel/bootstrap/mod.rs+++ b/crates/pa-core/src/kernel/bootstrap/mod.rs@@ -238,7 +238,13 @@ async fn ensure_kernel_venv(return Ok(python);}- let release_lock = acquire_bootstrap_lock(&venv).await?;+ let release_lock = match acquire_bootstrap_lock(&venv).await {+ Ok(release_lock) => release_lock,+ Err(error) if dir_lock::lock_creation_denied(&error) => {+ return unwritable_venv(options, &python, &venv, &runtime_identity, &error);+ }+ Err(error) => return Err(error),+ };let result = async {if kernel_ready(&python_str, &venv, &runtime_identity, python_skills) {return Ok(python);@@ -280,6 +286,38 @@ async fn ensure_kernel_venv(result.map_err(|error| format_bootstrap_failure(&error))}+/// The venv's directory denies writes to this user, so the bootstrap lock cannot be+/// created: a base-ready venv degrades the way a `PRIME_AGENT_KERNEL_PYTHON` override+/// does (uncovered skills disabled with a warning, nothing installed), and anything+/// less is a setup error naming the venv and both env overrides.+fn unwritable_venv(+ options: &EnsureKernelPythonOptions,+ python: &std::path::Path,+ venv: &std::path::Path,+ runtime_identity: &str,+ lock_error: &anyhow::Error,+) -> anyhow::Result<PathBuf> {+ let python_str = python.to_string_lossy().to_string();+ if !kernel_base_ready(&python_str, venv, runtime_identity) {+ return Err(anyhow!(+ "cannot set up the Python kernel runtime: the kernel venv at {} is not writable by \+ this user and does not hold a current runtime. Set PRIME_AGENT_KERNEL_VENV to a \+ writable directory, or set PRIME_AGENT_KERNEL_PYTHON to a Python with a current \+ prime-agent-runtime and the default Python packages installed. ({lock_error:#})",+ venv.display()+ ));+ }+ let missing = missing_python_skill_import_labels(&python_str, &options.python_skills);+ if !missing.is_empty() {+ options.report(&format!(+ "Warning: Python skills unavailable in {} and will be disabled: {}",+ venv.display(),+ missing.join(", ")+ ));+ }+ Ok(python.to_path_buf())+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/pa-core/src/kernel/bootstrap/venv/tests.rs b/crates/pa-core/src/kernel/bootstrap/venv/tests.rsindex 86d794c99c..5582311b9d 100644--- a/crates/pa-core/src/kernel/bootstrap/venv/tests.rs+++ b/crates/pa-core/src/kernel/bootstrap/venv/tests.rs@@ -1071,3 +1071,145 @@ async fn skill_sync_falls_back_to_per_skill_installs_on_batch_failure() {.collect::<Vec<_>>();assert_eq!(recorded, vec!["edit"], "only the healthy skill is recorded");}++/// Permission semantics need a non-root runner: `chmod` cannot deny root, so these+/// tests skip as root and the gate harness runs the binary via `runuser` as a+/// non-root user (a shared install dir readable but not writable, like the verifiers+/// harness's `agent_user` seats).+#[cfg(unix)]+fn skip_when_root() -> bool {+ if unsafe { libc::geteuid() } == 0 {+ eprintln!("skipped: run as a non-root user (chmod cannot deny root)");+ return true;+ }+ false+}++#[cfg(unix)]+fn set_mode(path: &Path, mode: u32) {+ use std::os::unix::fs::PermissionsExt;+ std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)).unwrap();+}++/// A base-ready venv whose directory cannot host the bootstrap lock (EACCES/EROFS on+/// the candidate) must degrade like a `PRIME_AGENT_KERNEL_PYTHON` override: the+/// uncovered skill is disabled with a warning and the venv python is returned, with+/// nothing installed into the read-only venv. Red on the raw EACCES before the fix.+#[cfg(unix)]+#[tokio::test]+async fn unwritable_venv_disables_uncovered_skills_instead_of_failing() {+ if skip_when_root() {+ return;+ }+ let dir = tempfile::tempdir().unwrap();+ let venv = dir.path().join("kernel-venv");+ let rlm = venv.join("lib/python3.11/site-packages/rlm");+ std::fs::create_dir_all(&rlm).unwrap();+ std::fs::write(rlm.join("__init__.py"), "x = 1\n").unwrap();+ // The fake interpreter passes the runtime and dill probes; only the skill import fails.+ let python = venv.join("bin/python");+ std::fs::create_dir_all(python.parent().unwrap()).unwrap();+ std::fs::write(+ &python,+ "#!/bin/sh\nif [ \"$2\" = \"import edit\" ]; then exit 1; fi\nexit 0\n",+ )+ .unwrap();+ set_mode(&python, 0o755);+ let skill_dir = dir.path().join("skills/edit");+ std::fs::create_dir_all(&skill_dir).unwrap();+ std::fs::write(+ skill_dir.join("pyproject.toml"),+ "[project]\nname = \"edit\"\n",+ )+ .unwrap();+ let runtime_source = require_runtime_source_dir().unwrap();+ let runtime_identity = hash_runtime_source(&runtime_source).unwrap();+ write_bootstrap_version(&venv, &runtime_identity, &[]).unwrap();++ let warnings = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));+ let mut options = EnsureKernelPythonOptions::default();+ let sink = warnings.clone();+ options.on_progress = Some(std::sync::Arc::new(move |message: &str| {+ sink.lock().unwrap().push(message.to_string());+ }));+ options.python_skills = vec![KernelPythonSkill {+ name: "edit".to_string(),+ import_name: "edit".to_string(),+ package_path: skill_dir.clone(),+ pyproject_path: skill_dir.join("pyproject.toml"),+ }];++ set_mode(&venv, 0o555);+ set_mode(dir.path(), 0o555);+ let outcome = super::super::ensure_kernel_venv(+ &options,+ &normalize_python_skills(&options.python_skills),+ venv.clone(),+ )+ .await;+ set_mode(dir.path(), 0o755);+ set_mode(&venv, 0o755);++ let resolved = outcome.expect("a base-ready venv must still yield its python");+ assert_eq!(resolved, python);+ let warnings = warnings.lock().unwrap();+ assert!(+ warnings.len() == 1+ && warnings[0].contains("Warning: Python skills unavailable in")+ && warnings[0].contains(&venv.display().to_string())+ && warnings[0].contains("edit (edit)"),+ "one warning naming the venv and the disabled skill: {warnings:?}"+ );+ assert!(+ !dir.path().join("kernel-venv.bootstrap.lock").exists(),+ "the read-only directory hosts no lock"+ );+ let version = read_bootstrap_version(&venv).expect("the version file is still readable");+ assert!(+ version.python_skills.as_ref().is_none_or(Vec::is_empty),+ "nothing was installed into the read-only venv: {version:?}"+ );+}++/// A venv that cannot host the lock AND cannot run the kernel is a setup error that+/// names the venv and both env overrides — not the raw candidate-file EACCES.+/// Red on the raw EACCES before the fix.+#[cfg(unix)]+#[tokio::test]+async fn unwritable_stale_venv_names_the_venv_and_both_env_overrides() {+ if skip_when_root() {+ return;+ }+ let dir = tempfile::tempdir().unwrap();+ let venv = dir.path().join("kernel-venv");+ let python = venv.join("bin/python");+ std::fs::create_dir_all(python.parent().unwrap()).unwrap();+ std::fs::write(&python, "#!/bin/sh\nexit 0\n").unwrap();+ set_mode(&python, 0o755);+ // A stale runtime identity: the base is not ready, so the venv needs a rebuild.+ write_bootstrap_version(&venv, "sha256:stale", &[]).unwrap();++ set_mode(&venv, 0o555);+ set_mode(dir.path(), 0o555);+ let error =+ super::super::ensure_kernel_venv(&EnsureKernelPythonOptions::default(), &[], venv.clone())+ .await+ .expect_err("an unusable venv in a read-only dir is a setup error");+ set_mode(dir.path(), 0o755);+ set_mode(&venv, 0o755);++ let message = error.to_string();+ assert!(+ message.contains(&venv.display().to_string()),+ "names the venv: {message}"+ );+ assert!(+ message.contains("PRIME_AGENT_KERNEL_VENV")+ && message.contains("PRIME_AGENT_KERNEL_PYTHON"),+ "names both env overrides: {message}"+ );+ assert!(+ !message.contains("candidate-"),+ "no raw lock-candidate path: {message}"+ );+}