fix: orchestrated runs honour yolo/auto and work inside the sandbox
- serve --acp applies --yolo / --auto (or `permissions` in config.toml) like the TUI; before, every edit and command went to the orchestrator. - claude no longer asks to run outside the sandbox. With the sandbox on it stays confined; only a stored `claude:unsandboxed` rule or --no-claude-sandbox lets it out, and yolo never does. - The sandbox reads the files the global git config includes and may stat $HOME and the directories above allowed paths and includeIf gitdir paths (metadata only). git died on both before. - hx mcp-serve logs a failed start to $TMPDIR/hx-mcp-serve.log; claude hides its stderr and only reports "mcp__hx__approve not found". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37ebf96108
commit
b1ca582d6b
3 files changed
+223
-78
No files matched your search
+100
-78
@@ -264,44 +264,91 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tool context for the claude sandbox question (set once the agent exists).
|
||||
/// Tool context of the agent, for the stored "run claude outside the sandbox" rule.
|
||||
static CLAUDE_CX: std::sync::OnceLock<ToolCtx> = std::sync::OnceLock::new();
|
||||
/// Answer to "run claude outside the sandbox?" for this process.
|
||||
static CLAUDE_OUTSIDE: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
|
||||
const OUTSIDE_RULE: &str = "claude:unsandboxed";
|
||||
|
||||
/// Allowed to leave the sandbox? A stored rule says yes; else the TUI asks (once per run, "always"
|
||||
/// is stored in `.hx/allow.json`); without a prompt hook (`-p`) the answer is no.
|
||||
async fn claude_outside() -> bool {
|
||||
if let Some(v) = *CLAUDE_OUTSIDE.lock().unwrap_or_else(|e| e.into_inner()) {
|
||||
return v;
|
||||
}
|
||||
let ok = match CLAUDE_CX.get() {
|
||||
Some(cx) if cx.state.permit.get().is_some() => {
|
||||
hx_core::tool::gate(
|
||||
cx,
|
||||
hx_core::permit::Request {
|
||||
tool: "claude".into(),
|
||||
detail: "run claude outside the sandbox (its tools then run unconfined)".into(),
|
||||
rule: OUTSIDE_RULE.into(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
Some(cx) => cx
|
||||
.state
|
||||
/// Leave the sandbox only on an explicit, stored rule (`claude:unsandboxed` in `.hx/allow.json`). Never
|
||||
/// asked and never implied by yolo: yolo is only safe because claude's tools stay confined.
|
||||
fn claude_outside() -> bool {
|
||||
CLAUDE_CX.get().is_some_and(|cx| {
|
||||
cx.state
|
||||
.allow
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.allows(&hx_core::permit::Request {
|
||||
tool: "claude".into(),
|
||||
detail: String::new(),
|
||||
rule: OUTSIDE_RULE.into(),
|
||||
}),
|
||||
None => false,
|
||||
.has_rule(OUTSIDE_RULE)
|
||||
})
|
||||
}
|
||||
|
||||
/// `hx mcp-serve`: lens/LSP, hx's MCP servers and (with a permission socket) `approve` for claude.
|
||||
async fn mcp_serve() -> Result<()> {
|
||||
let cwd = std::env::current_dir()?;
|
||||
let mgr = Arc::new(hx_lens::manager::Manager::new(
|
||||
cwd.clone(),
|
||||
hx_lens::manager::default_servers(),
|
||||
));
|
||||
let mut reg = hx_core::tool::Registry::default();
|
||||
reg.add(hx_lens::LspTool::new(mgr.clone()));
|
||||
let cx = ToolCtx {
|
||||
cwd: cwd.clone(),
|
||||
state: Default::default(),
|
||||
};
|
||||
*CLAUDE_OUTSIDE.lock().unwrap_or_else(|e| e.into_inner()) = Some(ok);
|
||||
ok
|
||||
let sock = std::env::var_os(hx_core::approve::ENV);
|
||||
if let Some(p) = &sock {
|
||||
reg.add(hx_core::approve::Approve(p.into()));
|
||||
// tools run by codemode ask hx for permission like any other
|
||||
let _ = cx
|
||||
.state
|
||||
.permit
|
||||
.set(hx_core::approve::remote_permit(p.into()));
|
||||
}
|
||||
// claude keeps its own tools; hx's MCP servers are added directly, or only through `codemode`
|
||||
let cfg = Config::load(&cwd)?;
|
||||
let mut servers =
|
||||
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?;
|
||||
// a server entry that is hx itself would spawn mcp-serve recursively
|
||||
servers.retain(|_, s| !s.args.iter().any(|a| a == "mcp-serve"));
|
||||
let hub = Arc::new(hx_mcp::Hub::start(servers).await);
|
||||
if std::env::var_os("HX_CODEMODE").is_some() {
|
||||
let mut all = hx_tools::registry();
|
||||
hx_tools::web::register(&mut all, web_search(&cfg));
|
||||
hub.register(&mut all);
|
||||
all.add(hx_lens::LspTool::new(mgr.clone()));
|
||||
reg.add(hx_tools::codemode::Codemode::new(&all));
|
||||
} else {
|
||||
hub.register(&mut reg);
|
||||
}
|
||||
hx_mcp::serve::serve(
|
||||
reg,
|
||||
cx,
|
||||
tokio::io::BufReader::new(tokio::io::stdin()),
|
||||
tokio::io::stdout(),
|
||||
)
|
||||
.await?;
|
||||
mgr.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Appends a line to `$TMPDIR/hx-mcp-serve.log` (writable inside the sandbox too); best effort.
|
||||
fn mcp_serve_log(line: &str) {
|
||||
use std::io::Write;
|
||||
let p = std::env::temp_dir().join("hx-mcp-serve.log");
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(p)
|
||||
{
|
||||
let cwd = std::env::current_dir().unwrap_or_default();
|
||||
let t = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs());
|
||||
let _ = writeln!(
|
||||
f,
|
||||
"{t} pid {} cwd {}: {line}",
|
||||
std::process::id(),
|
||||
cwd.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// What `claude` (and its `hx mcp-serve` child) need inside the sandbox: its binary and config,
|
||||
@@ -354,11 +401,11 @@ fn claude_extra(bin: &str) -> hx_core::sandbox::Extra {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `claude` command: sandboxed when the sandbox is on, unless the user let it out.
|
||||
/// The `claude` command: sandboxed when the sandbox is on, unless claude's sandbox is off or a rule lets it out.
|
||||
async fn claude_command(bin: String) -> tokio::process::Command {
|
||||
if hx_core::sandbox::active().is_none()
|
||||
|| !hx_core::sandbox::claude_sandboxed()
|
||||
|| claude_outside().await
|
||||
|| claude_outside()
|
||||
{
|
||||
return tokio::process::Command::new(bin);
|
||||
}
|
||||
@@ -817,51 +864,13 @@ async fn main() -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
Some(Cmd::McpServe) => {
|
||||
let cwd = std::env::current_dir()?;
|
||||
let mgr = Arc::new(hx_lens::manager::Manager::new(
|
||||
cwd.clone(),
|
||||
hx_lens::manager::default_servers(),
|
||||
));
|
||||
let mut reg = hx_core::tool::Registry::default();
|
||||
reg.add(hx_lens::LspTool::new(mgr.clone()));
|
||||
let cx = ToolCtx {
|
||||
cwd: cwd.clone(),
|
||||
state: Default::default(),
|
||||
};
|
||||
let sock = std::env::var_os(hx_core::approve::ENV);
|
||||
if let Some(p) = &sock {
|
||||
reg.add(hx_core::approve::Approve(p.into()));
|
||||
// tools run by codemode ask hx for permission like any other
|
||||
let _ = cx
|
||||
.state
|
||||
.permit
|
||||
.set(hx_core::approve::remote_permit(p.into()));
|
||||
// claude starts this and hides its stderr: a failed start shows up only as
|
||||
// "mcp__hx__approve not found", so the reason goes to a log file as well
|
||||
let r = mcp_serve().await;
|
||||
if let Err(e) = &r {
|
||||
mcp_serve_log(&format!("mcp-serve failed: {e:#}"));
|
||||
}
|
||||
// claude keeps its own tools; hx's MCP servers are added directly, or only through `codemode`
|
||||
let cfg = Config::load(&cwd)?;
|
||||
let mut servers =
|
||||
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?;
|
||||
// a server entry that is hx itself would spawn mcp-serve recursively
|
||||
servers.retain(|_, s| !s.args.iter().any(|a| a == "mcp-serve"));
|
||||
let hub = Arc::new(hx_mcp::Hub::start(servers).await);
|
||||
if std::env::var_os("HX_CODEMODE").is_some() {
|
||||
let mut all = hx_tools::registry();
|
||||
hx_tools::web::register(&mut all, web_search(&cfg));
|
||||
hub.register(&mut all);
|
||||
all.add(hx_lens::LspTool::new(mgr.clone()));
|
||||
reg.add(hx_tools::codemode::Codemode::new(&all));
|
||||
} else {
|
||||
hub.register(&mut reg);
|
||||
}
|
||||
hx_mcp::serve::serve(
|
||||
reg,
|
||||
cx,
|
||||
tokio::io::BufReader::new(tokio::io::stdin()),
|
||||
tokio::io::stdout(),
|
||||
)
|
||||
.await?;
|
||||
mgr.shutdown().await;
|
||||
return Ok(());
|
||||
return r;
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
@@ -1185,7 +1194,20 @@ async fn main() -> Result<()> {
|
||||
});
|
||||
}
|
||||
if let Some(id) = acp_id {
|
||||
// no TUI, no prompts: every permission question goes to the orchestrator
|
||||
// no TUI, no prompts: every permission question goes to the orchestrator, except what
|
||||
// --yolo / --auto (or `permissions` in config.toml) settle here, the same as in the TUI
|
||||
{
|
||||
let yolo = cli.yolo || cfg.permissions.as_deref() == Some("yolo");
|
||||
let auto = cli.auto || cfg.permissions.as_deref() == Some("auto");
|
||||
let mut a = agent
|
||||
.cx
|
||||
.state
|
||||
.allow
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
a.set_yolo(yolo);
|
||||
a.set_auto(auto && !yolo);
|
||||
}
|
||||
let opts = acp::Opts {
|
||||
session: id,
|
||||
cwd: cwd.clone(),
|
||||
|
||||
Reference in new issue
Block a user