From 8ecbd504a279f3ed9379ca988b65219795ffb4e4 Mon Sep 17 00:00:00 2001 From: Johannes Kresner Date: Sun, 15 Nov 2026 16:12:54 +0100 Subject: [PATCH] feat: set hx to yolo in sandbox mode else auto mode --- docs/safety.md | 3 +- examples/profiles/example.toml | 5 ++- src/acp.rs | 7 ++++ src/main.rs | 4 +- src/profile.rs | 71 ++++++++++++++++++++++++++++++---- src/run.rs | 7 +--- 6 files changed, 79 insertions(+), 18 deletions(-) diff --git a/docs/safety.md b/docs/safety.md index 761c615..349c32e 100644 --- a/docs/safety.md +++ b/docs/safety.md @@ -11,7 +11,8 @@ allow = ["read", "write", "edit", "bash:cargo *", "bash:git status"] read_roots = ["/work/shared-docs"] # extra readable dirs besides the worktree env = ["GITEA_*"] # orch env vars hx may see (default: PATH HOME USER LOGNAME SHELL TERM LANG LC_* # TMPDIR CARGO_* RUST* NIX_*, plus HX_HOME only when `home` is unset) -sandbox = true # hx --sandbox (bwrap); required for yolo = true +sandbox = true # hx --sandbox (bwrap/seatbelt). Default: on when available (macOS, Linux with bwrap), + # together with --yolo (yolo = false opts out). Unsandboxed hx runs with --auto. ``` ## Allowlist (`allow`) diff --git a/examples/profiles/example.toml b/examples/profiles/example.toml index 3c2e939..de615ed 100644 --- a/examples/profiles/example.toml +++ b/examples/profiles/example.toml @@ -4,8 +4,9 @@ hx = "hx" # home = "/home/me/.hx-work" # own HOME, so the profile keeps its own ~/.hx/auth.json # provider = "anthropic" -sandbox = false -# yolo = true # only together with sandbox = true +# Default: --sandbox --yolo when the platform has a sandbox (macOS, Linux with bwrap), else --auto. +# sandbox = false # force unsandboxed (hx runs with --auto) +# yolo = false # sandbox without --yolo; yolo = true needs a sandbox # Shell command run in the worktree after hx; exit 0 = pass. # gate = "cargo test" diff --git a/src/acp.rs b/src/acp.rs index f0fae9c..caa3803 100644 --- a/src/acp.rs +++ b/src/acp.rs @@ -67,6 +67,13 @@ impl Acp { .args(["serve", "--acp", "--cwd"]) .arg(worktree) .args(["--session", id]); + // the TUI answers permission requests itself, so yolo only when the profile asks for it explicitly + let flags = p.hx_flags(); + cmd.args( + flags + .iter() + .filter(|f| **f != "--yolo" || p.yolo == Some(true)), + ); // same cleared environment as the batch runner: orch's secrets never reach the agent cmd.env_clear().envs(hx_env(std::env::vars(), p)); if let Some(h) = &p.home { diff --git a/src/main.rs b/src/main.rs index 97229c0..d00384d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -454,7 +454,7 @@ fn process( telemetry: &Path, ) -> Result { let mut r = run::new_run(t, repo, profile)?; - if p.allow.is_none() && !p.sandbox { + if p.allow.is_none() && !p.sandboxed() { eprintln!( "warning: profile {profile} has no `allow` list and no sandbox: hx may use every tool" ); @@ -463,7 +463,7 @@ fn process( let audit = |ev: &str, d: serde_json::Value| policy::audit(&policy::audit_path(), &id, ev, d); audit( "run_start", - serde_json::json!({"profile": profile, "repo": repo, "allow": p.allow, "sandbox": p.sandbox, "yolo": p.yolo, "task": t.id}), + serde_json::json!({"profile": profile, "repo": repo, "allow": p.allow, "sandbox": p.sandboxed(), "yolo": p.yolo_on(), "task": t.id}), ); run::execute(&mut r, p, t)?; audit( diff --git a/src/profile.rs b/src/profile.rs index ff7327a..9087637 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -33,6 +33,18 @@ fn add_allow_at(path: &std::path::Path, entry: &str) -> Result { Ok(true) } +/// macOS ships a sandbox (seatbelt); Linux needs `bwrap` on PATH; elsewhere none. +pub fn sandbox_available() -> bool { + if cfg!(target_os = "macos") { + return true; + } + cfg!(target_os = "linux") && has_bwrap(&std::env::var("PATH").unwrap_or_default()) +} + +fn has_bwrap(path: &str) -> bool { + std::env::split_paths(path).any(|d| d.join("bwrap").is_file()) +} + #[derive(Deserialize, Debug, Default, Clone)] #[serde(deny_unknown_fields)] pub struct Budget { @@ -61,11 +73,11 @@ pub struct Profile { /// HOME for hx, so each profile keeps its own `~/.hx/auth.json`. pub home: Option, pub provider: Option, - #[serde(default)] - pub sandbox: bool, - /// Passes `--yolo`. Only allowed together with `sandbox = true`. - #[serde(default)] - pub yolo: bool, + /// Passes `--sandbox`. Unset: on when the platform has a sandbox (macOS, Linux with bwrap). + pub sandbox: Option, + /// Passes `--yolo`. Only allowed together with a sandbox. Unset: on whenever the sandbox is on. + /// With neither set and no sandbox available hx runs with `--auto`. + pub yolo: Option, /// Shell command run in the worktree after hx; exit 0 = pass. None = no gate. pub gate: Option, /// Extra hx attempts after a red gate (default 2). 0 = fail straight to needs-human. @@ -123,12 +135,39 @@ impl Profile { if p.workers == 0 || p.max_concurrent == Some(0) { bail!("workers and max_concurrent must be >= 1"); } - if p.yolo && !p.sandbox { + if p.yolo == Some(true) && p.sandbox == Some(false) { bail!("yolo = true requires sandbox = true"); } + if p.yolo == Some(true) && p.sandbox.is_none() && !sandbox_available() { + bail!("yolo = true requires a sandbox, and none is available (Linux needs bwrap)"); + } Ok(p) } + /// Whether hx runs sandboxed: explicit setting, else whatever the platform offers. + pub fn sandboxed(&self) -> bool { + self.sandbox.unwrap_or_else(sandbox_available) + } + + /// Whether hx gets `--yolo`: explicit setting, else on with the sandbox. + pub fn yolo_on(&self) -> bool { + self.yolo.unwrap_or(self.sandboxed()) && self.sandboxed() + } + + /// Permission flags for hx: `--sandbox [--yolo]`, or `--auto` when unsandboxed. + pub fn hx_flags(&self) -> Vec<&'static str> { + let mut f = Vec::new(); + if self.sandboxed() { + f.push("--sandbox"); + if self.yolo_on() { + f.push("--yolo"); + } + } else { + f.push("--auto"); + } + f + } + pub fn load(name: &str) -> Result { if name.is_empty() || name.contains(['/', '.']) { bail!("bad profile name {name:?}"); @@ -214,10 +253,28 @@ mod tests { #[test] fn rejects_yolo_without_sandbox_and_unknown_keys() { - assert!(Profile::parse("yolo=true").is_err()); + assert!(Profile::parse("sandbox=false\nyolo=true").is_err()); assert!(Profile::parse("bogus=1").is_err()); } + #[test] + fn flags_follow_sandbox_setting() { + let f = |s| Profile::parse(s).unwrap().hx_flags(); + assert_eq!(f("sandbox=true"), ["--sandbox", "--yolo"]); + assert_eq!(f("sandbox=true\nyolo=false"), ["--sandbox"]); + assert_eq!(f("sandbox=false"), ["--auto"]); + } + + #[test] + fn bwrap_lookup_scans_path() { + let dir = std::env::temp_dir().join(format!("orch-bwrap-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + assert!(!has_bwrap(dir.to_str().unwrap())); + std::fs::write(dir.join("bwrap"), "").unwrap(); + assert!(has_bwrap(&format!("/nonexistent:{}", dir.display()))); + } + #[test] fn rejects_path_like_names() { assert!(Profile::load("../x").is_err()); diff --git a/src/run.rs b/src/run.rs index 2029e30..ef8f19f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -275,12 +275,7 @@ fn run_hx( if let Some(s) = session { cmd.args(["--session", s]); } - if p.sandbox { - cmd.arg("--sandbox"); - } - if p.yolo { - cmd.arg("--yolo"); - } + cmd.args(p.hx_flags()); if let Some(prov) = &p.provider { cmd.args(["--provider", prov]); }