refactor(ai): make the OpenCode transport a standard sandboxed transport
Some checks are pending
ci / test (push) Waiting to run

This commit is contained in:
Phoebus 2026-09-29 04:58:22 -07:00
parent 52dbdf4422
commit aeb46aceee
9 changed files with 203 additions and 37 deletions

View file

@ -5,6 +5,22 @@ this file is the curated, human-readable summary.
## Unreleased — Fast defaults with user-controlled source builds
- **OpenCode is now a standard transport (2026-09-29).** The OpenCode backend was
special-cased twice: it ran **outside** the model sandbox (a comment called it
"a different trust boundary") and its endpoint policy was stricter than the
direct provider's (plain `http` only to loopback). Both are now unified. The
OpenCode transport runs in the **same OS-level sandbox** as the direct
transport unless `--no-sandbox` is given; its curl calls inline the request
body into the hidden stdin config (the sandbox's `PrivateTmp` would hide a
temp file). A **network** OpenCode server works over `https` as before, and a
remote plain-`http` server is permitted with the explicit
`--opencode-insecure-http` opt-in (credentials/prompts travel in cleartext, so
it is off by default; `PHOEBUS_OPENCODE_INSECURE_HTTP=1` also enables it).
VM re-verification with the real host agent (`serial-boot-20260929T114319Z-c43c2a92`,
driver PASS=8, sandbox on by default): `list`/`search` valid plans,
constrained install gated before mutation, hostile instruction refused. Gates:
`cargo fmt`, `clippy --workspace --all-targets -- -D warnings`, `cargo test
--workspace` 845 passed / 0 failed.
- **Headless read-only probes can no longer block; real-agent VM re-verification
(2026-09-29).** Driving the powerful model (host `opencode` agent,
`deepseek-v4.1-flash`) with `sys` **inside the VM**, a `sys ask "search for

View file

@ -25,6 +25,16 @@ commit it. It should be short and current; use `git log` for detail.
call (`sys ask` with a real provider is still VM/live NotRun) but it closes
the "connected-model path only tested with fake transports" gap. Gates:
`cargo test --workspace` 841 passed / 0 failed.
- **OpenCode transport standardized (2026-09-29, VM-VERIFIED):** the OpenCode
backend no longer runs outside the model sandbox and no longer has a stricter
endpoint policy than the direct provider. It is sandboxed by default (curl
body inlined on stdin because `PrivateTmp` hides a temp file); `https` servers
on the network work as before, and remote plain `http` is allowed only with
`--opencode-insecure-http` / `PHOEBUS_OPENCODE_INSECURE_HTTP=1`. Re-verified in
the VM with the real host agent (`serial-boot-20260929T114319Z-c43c2a92`,
driver PASS=8). Next confidence work: VM-prove the approval-ledger/effect-binding
paths, source-conversion/tuned-kernel journeys (multi-hour), a physical-seat
desktop pass, and configure a CI runner for the release gates.
- **VM live AI (2026-09-29, VM-VERIFIED):** from inside the installed guest,
the host's real `opencode` agent was reached through the loopback tunnel and
`sys ask` produced a valid plan (`live-agent`), with a hostile instruction

View file

@ -100,6 +100,7 @@ Booting that recovery entry offline remains a manual gate.
| AI connected-model path over a real local stub | yes | yes (4 e2e cases: valid plan executes; request has no home path/credential; shell + smuggled key + malformed fail closed) | no (local stub, not a live model) | n/a |
| Live real-agent call (`opencode` backend) | yes | yes (`sys ask "list installed packages"` → valid plan + digest, exit 0; hostile shell/private-file instruction refused and downgraded; OpenCode tools disabled; no credential in the log) | yes — host direct (2026-09-28) and **from inside the installed VM through the loopback tunnel** (`serial-boot-20260929T102703Z-ffd1ec25`, `live-agent` + `live-agent-hostile-rejected`) with `opencode-go/deepseek-v4.1-flash` | n/a |
| Local model through the direct sandbox (VM) | yes | yes (real Qwen2.5-1.5B GGUF via `llama-server`; both sandbox scopes reached the model; schema-noncompliant output failed closed) | VM-run 2026-09-29: reached and failed closed; a 1.5B model is too weak for exact typed plans and its 4096-token context was exceeded by the correction turn — a capable model is required (a capable agent produces valid plans) | n/a |
| OpenCode transport parity (sandbox + network endpoint) | yes | yes (`CurlHttpClient::sandboxed` inlines the body under `PrivateTmp`; `--opencode-insecure-http` opt-in for remote plain http; endpoint-policy test) | yes (`serial-boot-20260929T114319Z-c43c2a92`: real host agent, OpenCode sandboxed by default, driver PASS=8) | n/a |
| Read-only host-root probes never block on a prompt | yes | yes (`CommandSpec::detached` runs host-root Flatpak in a new session via `setsid --wait`, no controlling terminal; field-preserving unit test) | yes (VM: real-agent `sys ask "search for firefox"` completed with zero auth prompts, `serial-boot-20260929T111941Z-2be30b89`) | n/a |
| Privilege derived only from the process identity | yes | yes (an environment variable cannot forge uid 0; the removed `PHOEBUS_ALLOW_UNPRIVILEGED_EXEC` regression test) | no | n/a |
| AI OS sandbox (systemd hardened unit) | yes | yes (argv + sandboxed-send; system properties for root, user-manager subset for non-root) | yes (`serial-boot-20260929T092147Z-6f88907a`: `AI_ROOT_RC=0` system sandbox, `AI_USER_RC=0` `--user` sandbox; previously the user path hung on a polkit `manage-units` prompt) | no |

View file

@ -71,6 +71,12 @@ the static `sys` built from HEAD; the base image was never modified.
`CommandSpec::detached()` fix the re-run completed with **zero auth prompts**.
The install investigation itself stopped on the model's own request-count /
formatting limits and failed closed (no `sys` defect).
- **OpenCode sandbox parity (`serial-boot-20260929T114319Z-c43c2a92`, driver
PASS=8):** re-ran the same four real-agent requests with the OpenCode transport
now sandboxed by default (`systemd-run --user` for the unprivileged `sys`).
All four passed with the sandbox enabled — `list`/`search` valid plans,
constrained install gated, hostile instruction refused — proving the OpenCode
transport works inside the same OS-level boundary as the direct provider.
## 2026-09-28 Headless dual-target golden install (Run 13) & detached boot proven

View file

@ -128,11 +128,23 @@ pub trait HttpClient {
/// file. Redirects are disabled and the protocol is restricted to http/https.
pub struct CurlHttpClient<'a> {
exec: &'a Exec,
sandboxed: bool,
}
impl<'a> CurlHttpClient<'a> {
pub fn new(exec: &'a Exec) -> Self {
Self { exec }
Self {
exec,
sandboxed: false,
}
}
/// Run the request inside the OS-level model sandbox (same boundary as the
/// direct provider transport). The body is inlined into the curl config on
/// stdin because the sandbox has a private `/tmp`.
pub fn sandboxed(mut self) -> Self {
self.sandboxed = true;
self
}
}
@ -142,6 +154,12 @@ impl HttpClient for CurlHttpClient<'_> {
fn request(&self, request: &HttpRequest) -> Result<HttpResponse, AiError> {
let mut temp: Option<tempfile::NamedTempFile> = None;
let data_line = if let Some(body) = &request.body {
if self.sandboxed {
// Inline the body (escaped) into the curl config: the sandbox's
// PrivateTmp would hide a temp file created here.
let escaped = body.replace('\\', "\\\\").replace('"', "\\\"");
format!("data = \"{escaped}\"\n")
} else {
let mut file = tempfile::Builder::new()
.prefix("phoebus-opencode.")
.tempfile()
@ -152,6 +170,7 @@ impl HttpClient for CurlHttpClient<'_> {
let line = format!("data = \"@{}\"\n", file.path().display());
temp = Some(file);
line
}
} else {
String::new()
};
@ -218,14 +237,23 @@ impl HttpClient for CurlHttpClient<'_> {
marker = STATUS_MARKER,
);
let out = self
.exec
let out = if self.sandboxed {
crate::sandbox::run(
self.exec,
"curl",
&["-sS".into(), "-K".into(), "-".into()],
config.into_bytes(),
)
.map_err(|e| AiError::Provider(format!("transport error: {e}")))?
} else {
self.exec
.run(
CommandSpec::new("ai", "OpenCode request", "curl")
.args(["-sS", "-K", "-"])
.stdin(config.into_bytes()),
)
.map_err(|e| AiError::Provider(format!("transport error: {e}")))?;
.map_err(|e| AiError::Provider(format!("transport error: {e}")))?
};
drop(temp);
let stdout = out.stdout_lossy();
@ -770,6 +798,8 @@ pub struct ExecOpenCodeTransport<'a> {
timeout: Duration,
max_body: u64,
resolve: Option<(String, u16, IpAddr)>,
sandboxed: bool,
allow_insecure_http: bool,
}
impl<'a> ExecOpenCodeTransport<'a> {
@ -789,9 +819,24 @@ impl<'a> ExecOpenCodeTransport<'a> {
timeout: Duration::from_secs(120),
max_body: 4 * 1024 * 1024,
resolve: None,
sandboxed: false,
allow_insecure_http: false,
}
}
/// Run the OpenCode requests inside the same OS-level sandbox as the direct
/// provider transport (DynamicUser/ProtectHome/... via `systemd-run`).
pub fn sandboxed(mut self) -> Self {
self.sandboxed = true;
self
}
/// Permit plain `http` to a non-loopback OpenCode server (explicit opt-in).
pub fn allow_insecure_http(mut self) -> Self {
self.allow_insecure_http = true;
self
}
pub fn with_auth(mut self, password: Option<SecretString>) -> Self {
self.auth = password;
self
@ -855,12 +900,17 @@ impl ModelTransport for ExecOpenCodeTransport<'_> {
// address, so the adapter cannot be pointed at a plain-http remote.
let endpoint = crate::opencode_server::Endpoint::parse(&self.base)
.map_err(|e| AiError::Rejected(format!("invalid OpenCode endpoint: {e}")))?;
let target = endpoint.checked_target()?;
let target = endpoint.checked_target_with(self.allow_insecure_http)?;
let resolve =
self.resolve
.clone()
.or(Some((endpoint.host.clone(), endpoint.port, target.ip())));
let http = CurlHttpClient::new(self.exec);
let http = if self.sandboxed {
http.sandboxed()
} else {
http
};
let mut client = OpenCodeClient::new(&http, &self.base, &self.provider, &self.model)
.with_auth(self.auth.clone())
.with_timeout(self.timeout)
@ -924,6 +974,44 @@ mod tests {
);
}
#[test]
fn a_sandboxed_request_runs_curl_under_systemd_run_with_the_body_inlined() {
if !crate::sandbox::available() {
eprintln!("systemd-run unavailable; sandboxed OpenCode transport not exercised here");
return;
}
let runner = RecordingRunner::new();
runner.set_response(
"systemd-run",
CommandOutput {
status: 0,
success: true,
stdout: b"\n__PHOEBUS_STATUS__200".to_vec(),
stderr: Vec::new(),
duration: std::time::Duration::from_millis(1),
},
);
let dir = tempfile::tempdir().unwrap().keep();
let logger = crate::log::Logger::open(dir.join("l.log"), Redactor::new(), false);
let exec = crate::exec::Exec::new(Box::new(runner.clone()), logger, false, &dir, &dir);
let http = CurlHttpClient::new(&exec).sandboxed();
let request = HttpRequest::post("http://127.0.0.1:4096/session", r#"{"x":"y"}"#);
let response = http.request(&request).unwrap();
assert_eq!(response.status, 200);
let spec = &runner.recorded()[0];
assert_eq!(spec.program, "systemd-run");
let stdin = String::from_utf8(spec.stdin.clone().unwrap()).unwrap();
assert!(
stdin.contains(r#"data = "{\"x\":\"y\"}""#),
"the body must be inlined into the config: {stdin}"
);
assert!(
!stdin.contains("phoebus-opencode."),
"no temp file may be used under PrivateTmp: {stdin}"
);
}
#[test]
fn a_bearer_token_is_redacted_too() {
let runner = RecordingRunner::new();

View file

@ -136,10 +136,19 @@ impl Endpoint {
/// Resolve the endpoint and enforce the transport policy.
///
/// Returns the address the connection must be pinned to. `http` requires
/// every resolved address to be loopback; `https` requires a valid
/// certificate (checked by curl, never disabled).
/// Returns the address the connection must be pinned to. Plain `http` must
/// resolve to loopback unless the caller explicitly opted in to an insecure
/// remote; `https` requires a valid certificate (checked by curl, never
/// disabled).
pub fn checked_target(&self) -> Result<SocketAddr, AiError> {
self.checked_target_with(false)
}
/// Like [`checked_target`], but permits plain `http` to a non-loopback host
/// when the operator explicitly opted in (`--opencode-insecure-http`). The
/// credential and prompts would then travel in cleartext, so the default is
/// strictly loopback.
pub fn checked_target_with(&self, allow_insecure_http: bool) -> Result<SocketAddr, AiError> {
let addrs: Vec<SocketAddr> = (self.host.as_str(), self.port)
.to_socket_addrs()
.map_err(|e| AiError::Provider(format!("cannot resolve {}: {e}", self.host)))?
@ -150,11 +159,12 @@ impl Endpoint {
self.host
)));
}
if self.scheme == Scheme::Http {
if self.scheme == Scheme::Http && !allow_insecure_http {
for addr in &addrs {
if !addr.ip().is_loopback() {
return Err(AiError::Rejected(format!(
"refusing plain http to {} ({}); use https or a loopback tunnel",
"refusing plain http to {} ({}); use https or a loopback tunnel, or opt \
in explicitly with --opencode-insecure-http",
self.host,
addr.ip()
)));
@ -535,6 +545,8 @@ mod tests {
let public = Endpoint::parse("http://93.184.216.34:80").unwrap();
let err = public.checked_target().unwrap_err().to_string();
assert!(err.contains("refusing plain http"), "{err}");
// An explicit opt-in permits a remote plain-http server.
assert!(public.checked_target_with(true).is_ok());
// https is allowed (certificate verification happens in the transport).
let secure = Endpoint::parse("https://93.184.216.34:443").unwrap();
assert!(secure.checked_target().is_ok());

View file

@ -30,6 +30,8 @@ pub struct AiOverrides {
pub opencode_provider: Option<String>,
pub opencode_model: Option<String>,
pub opencode_password_file: Option<PathBuf>,
/// Permit plain `http` to a non-loopback OpenCode server (explicit opt-in).
pub opencode_insecure_http: bool,
pub no_sandbox: bool,
/// When true, environment variables are consulted between the explicit
/// override and the installed configuration.
@ -49,6 +51,7 @@ pub struct ResolvedAi {
pub opencode_model: String,
pub api_key_file: Option<PathBuf>,
pub opencode_password_file: Option<PathBuf>,
pub opencode_insecure_http: bool,
pub no_sandbox: bool,
}
@ -183,6 +186,9 @@ pub fn resolve(_root: &Path, ov: &AiOverrides, conf: Option<&AiConfig>) -> Resol
opencode_model,
api_key_file,
opencode_password_file,
opencode_insecure_http: ov.opencode_insecure_http
|| env_var(ov.use_environment, "PHOEBUS_OPENCODE_INSECURE_HTTP").as_deref()
== Some("1"),
no_sandbox: ov.no_sandbox,
}
}
@ -236,15 +242,24 @@ impl ResolvedAi {
match self.backend {
AiBackend::OpenCode => {
let password = self.load_opencode_password()?;
Ok(Box::new(
ExecOpenCodeTransport::new(
// The OpenCode agent is a standard transport: it runs in the
// same OS-level sandbox as the direct provider unless the
// operator disabled it, and a network server is allowed (https,
// or plain http only with an explicit opt-in).
let mut transport = ExecOpenCodeTransport::new(
exec,
self.opencode_server.clone(),
self.opencode_provider.clone(),
self.opencode_model.clone(),
)
.with_auth(password),
))
.with_auth(password);
if self.opencode_insecure_http {
transport = transport.allow_insecure_http();
}
if !self.no_sandbox {
transport = transport.sandboxed();
}
Ok(Box::new(transport))
}
AiBackend::Direct => {
let key = self.load_direct_key(exec)?;
@ -300,6 +315,7 @@ mod tests {
opencode_model: String::new(),
api_key_file: key_file,
opencode_password_file: None,
opencode_insecure_http: false,
no_sandbox: true,
}
}
@ -351,6 +367,7 @@ mod tests {
opencode_provider: Some("opencode-go".into()),
opencode_model: Some("muse-spark-1.3-contributor".into()),
opencode_password_file: None,
opencode_insecure_http: false,
no_sandbox: true,
use_environment: false,
};

View file

@ -793,6 +793,15 @@ mod tests {
/// environment.
static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
/// Take the environment lock without cascading a panic from another test
/// into every waiter (a poisoned mutex would otherwise fail all three
/// env-dependent tests).
fn env_lock() -> std::sync::MutexGuard<'static, ()> {
ENV_LOCK
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
struct EnvGuard {
key: &'static str,
original: Option<std::ffi::OsString>,
@ -817,7 +826,7 @@ mod tests {
#[test]
fn personal_preferences_roundtrip_and_use_user_storage() {
let _env = ENV_LOCK.lock().unwrap();
let _env = env_lock();
let base = tempdir().unwrap();
let _guard = EnvGuard::set("PHOEBUS_PERSONAL_DIR", base.path().as_os_str());
@ -845,7 +854,7 @@ mod tests {
#[test]
fn a_failed_preference_save_is_reported_as_a_failure() {
let _env = ENV_LOCK.lock().unwrap();
let _env = env_lock();
let blocker = tempdir().unwrap();
let not_a_dir = blocker.path().join("file");
std::fs::write(&not_a_dir, "x").unwrap();
@ -865,7 +874,7 @@ mod tests {
#[test]
fn snooze_and_dismiss_persist_to_user_storage() {
let _env = ENV_LOCK.lock().unwrap();
let _env = env_lock();
let base = tempdir().unwrap();
let _guard = EnvGuard::set("PHOEBUS_PERSONAL_DIR", base.path().as_os_str());
@ -876,7 +885,8 @@ mod tests {
let mut snooze = request("proactive_snooze");
snooze.duration_seconds = Some(3600);
assert!(handle_request(&exec, root, &plans, snooze).ok);
let resp = handle_request(&exec, root, &plans, snooze);
assert!(resp.ok, "snooze failed: {:?}", resp.error);
let mut dismiss = request("proactive_dismiss");
dismiss.fingerprint = Some("reboot_required".into());

View file

@ -157,6 +157,11 @@ struct Cli {
/// Read the OpenCode server password from a mode-0600 file.
#[arg(long, value_name = "FILE")]
opencode_password_file: Option<PathBuf>,
/// Permit plain http to a non-loopback OpenCode server. Credentials and
/// prompts then travel in cleartext; prefer https or a loopback tunnel.
#[arg(long)]
opencode_insecure_http: bool,
}
struct SysConfirm {
@ -975,6 +980,7 @@ fn resolve_ai(cli: &Cli) -> phoebus_installer::ai_setup::ResolvedAi {
opencode_provider: cli.opencode_provider.clone(),
opencode_model: cli.opencode_model.clone(),
opencode_password_file: cli.opencode_password_file.clone(),
opencode_insecure_http: cli.opencode_insecure_http,
no_sandbox: cli.no_sandbox,
use_environment: true,
};