use openmoat_core::Policy;
use serde::Serialize;
use super::by;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Boundary {
pub by: String,
pub met: bool,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub gaps: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub mcp: Vec<String>,
}
impl Boundary {
pub fn of(
policy: &Policy,
system: Option<String>,
mcp: impl FnOnce() -> Vec<String>,
) -> Option<Self> {
let asked = policy.run_boundary_required()?;
let gaps: Vec<String> = system.into_iter().collect();
let mcp = mcp();
Some(Self {
by: by(&asked),
met: gaps.is_empty() && mcp.is_empty(),
gaps,
mcp,
})
}
pub fn describe(&self) -> String {
let head = format!(
"`openmoat run` must start with its full boundary ({})",
self.by
);
if self.met {
return format!("{head}: holds on this system");
}
let mut parts = Vec::new();
if !self.gaps.is_empty() {
parts.push(format!("it refuses to start: {}", self.gaps.join("; ")));
}
if !self.mcp.is_empty() {
parts.push(format!(
"MCP servers the agent starts itself, which `openmoat run` cannot refuse: {}",
self.mcp.join("; ")
));
}
format!("{head}: not met: {}", parts.join("; "))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn strict() -> Policy {
Policy::parse("version: 1\nrequire:\n run: full-boundary\n").unwrap()
}
#[test]
fn without_the_setting_nothing_is_reported_or_read() {
let policy = Policy::parse("version: 1\nrequire:\n protection: hook-only\n").unwrap();
let state = Boundary::of(&policy, Some("no sandbox".into()), || {
panic!("the MCP servers are not read without the setting")
});
assert_eq!(state, None);
}
#[test]
fn it_holds_when_nothing_is_missing() {
let state = Boundary::of(&strict(), None, Vec::new).unwrap();
assert!(state.met);
assert!(state.describe().contains("holds on this system"));
assert!(state.describe().contains("your policy"));
}
#[test]
fn a_system_gap_is_named_and_says_the_run_refuses() {
let state = Boundary::of(&strict(), Some("no bubblewrap".into()), Vec::new).unwrap();
assert!(!state.met);
assert!(state.describe().contains("no bubblewrap"), "{state:?}");
assert!(state.describe().contains("refuses to start"), "{state:?}");
}
#[test]
fn an_unprotected_server_fails_without_claiming_a_refusal() {
let state = Boundary::of(&strict(), None, || vec!["`gh`: unprotected".to_owned()]).unwrap();
assert!(!state.met);
let line = state.describe();
assert!(line.contains("`gh`: unprotected"), "{line}");
assert!(line.contains("cannot refuse"), "{line}");
assert!(!line.contains("it refuses to start"), "{line}");
}
}