feat: set hx to yolo in sandbox mode else auto mode
This commit is contained in:
1 parent
3f396b112a
commit
8ecbd504a2
6 files changed
+79
-18
No files matched your search
+2
-1
@@ -11,7 +11,8 @@ allow = ["read", "write", "edit", "bash:cargo *", "bash:git status"]
|
|||||||
read_roots = ["/work/shared-docs"] # extra readable dirs besides the worktree
|
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_*
|
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)
|
# 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`)
|
## Allowlist (`allow`)
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
hx = "hx"
|
hx = "hx"
|
||||||
# home = "/home/me/.hx-work" # own HOME, so the profile keeps its own ~/.hx/auth.json
|
# home = "/home/me/.hx-work" # own HOME, so the profile keeps its own ~/.hx/auth.json
|
||||||
# provider = "anthropic"
|
# provider = "anthropic"
|
||||||
sandbox = false
|
# Default: --sandbox --yolo when the platform has a sandbox (macOS, Linux with bwrap), else --auto.
|
||||||
# yolo = true # only together with sandbox = true
|
# 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.
|
# Shell command run in the worktree after hx; exit 0 = pass.
|
||||||
# gate = "cargo test"
|
# gate = "cargo test"
|
||||||
|
|||||||
@@ -67,6 +67,13 @@ impl Acp {
|
|||||||
.args(["serve", "--acp", "--cwd"])
|
.args(["serve", "--acp", "--cwd"])
|
||||||
.arg(worktree)
|
.arg(worktree)
|
||||||
.args(["--session", id]);
|
.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
|
// same cleared environment as the batch runner: orch's secrets never reach the agent
|
||||||
cmd.env_clear().envs(hx_env(std::env::vars(), p));
|
cmd.env_clear().envs(hx_env(std::env::vars(), p));
|
||||||
if let Some(h) = &p.home {
|
if let Some(h) = &p.home {
|
||||||
|
|||||||
+2
-2
@@ -454,7 +454,7 @@ fn process(
|
|||||||
telemetry: &Path,
|
telemetry: &Path,
|
||||||
) -> Result<state::Run> {
|
) -> Result<state::Run> {
|
||||||
let mut r = run::new_run(t, repo, profile)?;
|
let mut r = run::new_run(t, repo, profile)?;
|
||||||
if p.allow.is_none() && !p.sandbox {
|
if p.allow.is_none() && !p.sandboxed() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"warning: profile {profile} has no `allow` list and no sandbox: hx may use every tool"
|
"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);
|
let audit = |ev: &str, d: serde_json::Value| policy::audit(&policy::audit_path(), &id, ev, d);
|
||||||
audit(
|
audit(
|
||||||
"run_start",
|
"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)?;
|
run::execute(&mut r, p, t)?;
|
||||||
audit(
|
audit(
|
||||||
|
|||||||
+64
-7
@@ -33,6 +33,18 @@ fn add_allow_at(path: &std::path::Path, entry: &str) -> Result<bool> {
|
|||||||
Ok(true)
|
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)]
|
#[derive(Deserialize, Debug, Default, Clone)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct Budget {
|
pub struct Budget {
|
||||||
@@ -61,11 +73,11 @@ pub struct Profile {
|
|||||||
/// HOME for hx, so each profile keeps its own `~/.hx/auth.json`.
|
/// HOME for hx, so each profile keeps its own `~/.hx/auth.json`.
|
||||||
pub home: Option<PathBuf>,
|
pub home: Option<PathBuf>,
|
||||||
pub provider: Option<String>,
|
pub provider: Option<String>,
|
||||||
#[serde(default)]
|
/// Passes `--sandbox`. Unset: on when the platform has a sandbox (macOS, Linux with bwrap).
|
||||||
pub sandbox: bool,
|
pub sandbox: Option<bool>,
|
||||||
/// Passes `--yolo`. Only allowed together with `sandbox = true`.
|
/// Passes `--yolo`. Only allowed together with a sandbox. Unset: on whenever the sandbox is on.
|
||||||
#[serde(default)]
|
/// With neither set and no sandbox available hx runs with `--auto`.
|
||||||
pub yolo: bool,
|
pub yolo: Option<bool>,
|
||||||
/// Shell command run in the worktree after hx; exit 0 = pass. None = no gate.
|
/// Shell command run in the worktree after hx; exit 0 = pass. None = no gate.
|
||||||
pub gate: Option<String>,
|
pub gate: Option<String>,
|
||||||
/// Extra hx attempts after a red gate (default 2). 0 = fail straight to needs-human.
|
/// 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) {
|
if p.workers == 0 || p.max_concurrent == Some(0) {
|
||||||
bail!("workers and max_concurrent must be >= 1");
|
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");
|
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)
|
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> {
|
pub fn load(name: &str) -> Result<Self> {
|
||||||
if name.is_empty() || name.contains(['/', '.']) {
|
if name.is_empty() || name.contains(['/', '.']) {
|
||||||
bail!("bad profile name {name:?}");
|
bail!("bad profile name {name:?}");
|
||||||
@@ -214,10 +253,28 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rejects_yolo_without_sandbox_and_unknown_keys() {
|
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());
|
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]
|
#[test]
|
||||||
fn rejects_path_like_names() {
|
fn rejects_path_like_names() {
|
||||||
assert!(Profile::load("../x").is_err());
|
assert!(Profile::load("../x").is_err());
|
||||||
|
|||||||
+1
-6
@@ -275,12 +275,7 @@ fn run_hx(
|
|||||||
if let Some(s) = session {
|
if let Some(s) = session {
|
||||||
cmd.args(["--session", s]);
|
cmd.args(["--session", s]);
|
||||||
}
|
}
|
||||||
if p.sandbox {
|
cmd.args(p.hx_flags());
|
||||||
cmd.arg("--sandbox");
|
|
||||||
}
|
|
||||||
if p.yolo {
|
|
||||||
cmd.arg("--yolo");
|
|
||||||
}
|
|
||||||
if let Some(prov) = &p.provider {
|
if let Some(prov) = &p.provider {
|
||||||
cmd.args(["--provider", prov]);
|
cmd.args(["--provider", prov]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user