feat: set hx to yolo in sandbox mode else auto mode
build / version (push) Successful in 11s
build / build (aarch64-apple-darwin) (push) Successful in 5m13s
build / build (x86_64-unknown-linux-musl) (push) Successful in 4m21s
build / release (push) Successful in 20s

This commit is contained in:
johannes committed 2026-11-15 16:12:54 +01:00
1 parent 3f396b112a
commit 8ecbd504a2
6 files changed
+79 -18

No files matched your search

+64 -7
View File
@@ -33,6 +33,18 @@ fn add_allow_at(path: &std::path::Path, entry: &str) -> Result<bool> {
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<PathBuf>,
pub provider: Option<String>,
#[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<bool>,
/// 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<bool>,
/// Shell command run in the worktree after hx; exit 0 = pass. None = no gate.
pub gate: Option<String>,
/// 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<Self> {
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());