use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::core::hive::HiveError;
use crate::core::hive::state::{HiveStore, load_or_quarantine, lock_state, write_private};
use lean_ctx_protocol::UtcTimestamp;
use lean_ctx_protocol::hive::bus::{BusFrameV1, ControlAction};
use lean_ctx_protocol::hive::lease::LeaseState;
const CONTROL_DIR: &str = "bus";
const CONTROL_FILE: &str = "control.json";
const CONTROL_VERSION: u32 = 1;
pub const ALL_AGENTS: &str = "*";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ControlEntry {
pub action: ControlAction,
pub control_id: String,
pub reason: String,
pub issued_by: String,
pub seq: u64,
pub server_time: UtcTimestamp,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TargetControl {
#[serde(default)]
pub mode: Option<ControlEntry>,
#[serde(default)]
pub vetoes: BTreeMap<String, ControlEntry>,
#[serde(default)]
pub last_seq: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ControlState {
pub version: u32,
#[serde(default)]
pub instance_id: Option<String>,
#[serde(default)]
pub targets: BTreeMap<String, TargetControl>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rebuild_pending: Option<UtcTimestamp>,
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub rebuilt_for: BTreeSet<(String, String)>,
}
fn stamp_now() -> UtcTimestamp {
UtcTimestamp::new(chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string())
.expect("a formatted UTC time is a valid timestamp")
}
const REBUILD_REASON: &str =
"the local control state was damaged; waiting for Hive to resend the controls";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refusal {
pub target: String,
pub entry: ControlEntry,
}
impl Refusal {
pub fn message(&self) -> String {
let who = if self.target == ALL_AGENTS {
"every agent on this machine".to_string()
} else {
format!("agent {}", self.target)
};
let what = match self.entry.action {
ControlAction::Stop => "stopped: end the current task",
ControlAction::Hold => "on hold until resumed",
ControlAction::Veto => "vetoed",
ControlAction::Resume => "resumed",
};
format!(
"Hive control: {who} is {what} (by {}: {}). Read-only tools still work.",
self.entry.issued_by, self.entry.reason
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActiveControl {
pub target_agent: Option<String>,
pub action: ControlAction,
pub target_ref: Option<String>,
pub reason: String,
pub issued_by: String,
pub since: UtcTimestamp,
}
impl ControlState {
fn read(data_dir: &Path) -> Result<Self, Option<UtcTimestamp>> {
let path = data_dir.join(CONTROL_DIR).join(CONTROL_FILE);
let changed = || {
let at: chrono::DateTime<chrono::Utc> =
std::fs::metadata(&path).ok()?.modified().ok()?.into();
UtcTimestamp::new(at.format("%Y-%m-%dT%H:%M:%SZ").to_string()).ok()
};
match std::fs::read(&path) {
Ok(bytes) => serde_json::from_slice::<Self>(&bytes)
.ok()
.filter(|state| state.version == CONTROL_VERSION)
.ok_or_else(changed),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
Err(_) => Err(changed()),
}
}
pub fn load(data_dir: &Path) -> Result<Self, HiveError> {
load_or_quarantine(
&data_dir.join(CONTROL_DIR).join(CONTROL_FILE),
|state: &Self| state.version == CONTROL_VERSION,
)
.map(Option::unwrap_or_default)
}
pub fn save(&self, data_dir: &Path) -> Result<(), HiveError> {
let mut bytes =
serde_json::to_vec_pretty(self).map_err(|e| HiveError::Local(e.to_string()))?;
bytes.push(b'\n');
write_private(&data_dir.join(CONTROL_DIR), CONTROL_FILE, &bytes)
}
pub fn apply(&mut self, instance_id: Option<&str>, frame: &BusFrameV1) -> bool {
let BusFrameV1::Control {
control_id,
target_agent,
target_ref,
action,
reason,
issued_by,
seq,
server_time,
} = frame
else {
return false;
};
if self.instance_id.as_deref() != instance_id {
self.targets.clear();
self.instance_id = instance_id.map(str::to_string);
}
self.version = CONTROL_VERSION;
let target = self
.targets
.entry(target_agent.clone().unwrap_or_else(|| ALL_AGENTS.into()))
.or_default();
if *seq <= target.last_seq {
return false;
}
target.last_seq = *seq;
let entry = ControlEntry {
action: *action,
control_id: control_id.clone(),
reason: reason.clone(),
issued_by: issued_by.clone(),
seq: *seq,
server_time: server_time.clone(),
};
match (action, target_ref) {
(ControlAction::Hold | ControlAction::Stop, _) => target.mode = Some(entry),
(ControlAction::Veto, Some(reference)) => {
target.vetoes.insert(reference.clone(), entry);
}
(ControlAction::Veto, None) => {}
(ControlAction::Resume, Some(reference)) => {
target.vetoes.remove(reference);
}
(ControlAction::Resume, None) => target.mode = None,
}
true
}
fn targets_for<'a>(
&'a self,
agent_id: Option<&'a str>,
) -> impl Iterator<Item = (&'a str, &'a TargetControl)> {
std::iter::once(ALL_AGENTS)
.chain(agent_id.filter(|id| *id != ALL_AGENTS))
.filter_map(|name| self.targets.get(name).map(|t| (name, t)))
}
fn pending(&self) -> Option<Refusal> {
self.rebuild_pending.clone().map(|since| Refusal {
target: ALL_AGENTS.into(),
entry: ControlEntry {
action: ControlAction::Hold,
control_id: "local".into(),
reason: REBUILD_REASON.into(),
issued_by: "lean-ctx".into(),
seq: 0,
server_time: since,
},
})
}
fn pending_for(&self, agent_id: Option<&str>) -> Option<Refusal> {
if agent_id.is_some_and(|id| self.rebuilt_for.iter().any(|(agent, _)| agent == id)) {
return None;
}
self.pending()
}
pub fn admission(&self, agent_id: Option<&str>) -> Option<Refusal> {
self.targets_for(agent_id)
.filter_map(|(name, target)| {
target.mode.as_ref().map(|entry| Refusal {
target: name.to_string(),
entry: entry.clone(),
})
})
.chain(self.pending_for(agent_id))
.max_by_key(|refusal| {
(
refusal.entry.action == ControlAction::Stop,
refusal.entry.seq,
)
})
}
pub fn vetoed(&self, agent_id: Option<&str>, target_ref: &str) -> Option<Refusal> {
self.targets_for(agent_id)
.filter_map(|(name, target)| {
target.vetoes.get(target_ref).map(|entry| Refusal {
target: name.to_string(),
entry: entry.clone(),
})
})
.max_by_key(|refusal| refusal.entry.seq)
.or_else(|| self.pending_for(agent_id))
}
pub fn active(&self) -> Vec<ActiveControl> {
let mut out: Vec<ActiveControl> = self
.pending()
.map(|p| ActiveControl {
target_agent: None,
action: p.entry.action,
target_ref: None,
reason: p.entry.reason,
issued_by: p.entry.issued_by,
since: p.entry.server_time,
})
.into_iter()
.collect();
for (name, target) in &self.targets {
let target_agent = (name != ALL_AGENTS).then(|| name.clone());
let view = |entry: &ControlEntry, target_ref: Option<&String>| ActiveControl {
target_agent: target_agent.clone(),
action: entry.action,
target_ref: target_ref.cloned(),
reason: entry.reason.clone(),
issued_by: entry.issued_by.clone(),
since: entry.server_time.clone(),
};
if let Some(entry) = &target.mode {
out.push(view(entry, None));
}
for (reference, entry) in &target.vetoes {
out.push(view(entry, Some(reference)));
}
}
out
}
}
fn load_for(store: &HiveStore) -> Result<ControlState, HiveError> {
match ControlState::load(store.data_dir()) {
Ok(state) => Ok(state),
Err(damaged) => {
tracing::warn!("hive bus controls: {damaged}");
let state = ControlState {
version: CONTROL_VERSION,
instance_id: store.instance()?.map(|i| i.instance_id),
targets: BTreeMap::new(),
rebuild_pending: Some(stamp_now()),
rebuilt_for: BTreeSet::new(),
};
state.save(store.data_dir())?;
Ok(state)
}
}
}
fn mutate<T>(
store: &HiveStore,
change: impl FnOnce(&mut ControlState) -> (bool, T),
) -> Result<T, HiveError> {
let _lock = lock_state(&store.data_dir().join(CONTROL_DIR), CONTROL_FILE)?;
let mut state = load_for(store)?;
let (changed, out) = change(&mut state);
if changed {
state.save(store.data_dir())?;
}
Ok(out)
}
pub fn record(store: &HiveStore, frame: &BusFrameV1) -> Result<bool, HiveError> {
let instance = store.instance()?.map(|i| i.instance_id);
mutate(store, |state| {
let changed = state.apply(instance.as_deref(), frame);
(changed, changed)
})
}
pub fn rebuilt(
store: &HiveStore,
confirmed: &BTreeSet<(String, String)>,
projects: &BTreeSet<String>,
) -> Result<bool, HiveError> {
mutate(store, |state| {
if state.rebuild_pending.is_none() {
return (false, false);
}
let before = state.rebuilt_for.len();
state.rebuilt_for.extend(confirmed.iter().cloned());
let covered: BTreeSet<&str> = state.rebuilt_for.iter().map(|(_, p)| p.as_str()).collect();
if !projects.iter().all(|p| covered.contains(p.as_str())) {
return (state.rebuilt_for.len() != before, false);
}
state.rebuild_pending = None;
state.rebuilt_for.clear();
(true, true)
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Inactive {
NotEnrolled,
LeaseLapsed,
}
impl Inactive {
pub fn status_line(self) -> &'static str {
match self {
Self::NotEnrolled => "controls inactive: not enrolled",
Self::LeaseLapsed => "controls inactive: lease lapsed",
}
}
}
fn lease_lapsed(store: &HiveStore, local_now: chrono::DateTime<chrono::Utc>) -> bool {
let record = store
.read_config::<crate::core::hive::state::LeaseRecord>(crate::core::hive::state::LEASE_FILE)
.ok()
.flatten();
let Some(record) = record else {
return true;
};
let lease = serde_json::from_value::<lean_ctx_protocol::hive::lease::HiveLeaseEnvelopeV1>(
record.envelope,
)
.ok()
.and_then(|envelope| {
serde_json::from_value::<lean_ctx_protocol::hive::lease::HiveLeaseV1>(envelope.lease).ok()
});
let Some(lease) = lease else {
return true;
};
let service = (local_now + chrono::Duration::seconds(record.offset_seconds)).fixed_offset();
!matches!(
lease.state_at(service),
Ok(LeaseState::Valid | LeaseState::Grace)
)
}
pub fn current_at(
store: &HiveStore,
local_now: chrono::DateTime<chrono::Utc>,
) -> (ControlState, Option<Inactive>) {
if !store
.data_dir()
.join(CONTROL_DIR)
.join(CONTROL_FILE)
.exists()
{
return (ControlState::default(), None);
}
let state = ControlState::read(store.data_dir()).unwrap_or_else(|since| ControlState {
version: CONTROL_VERSION,
instance_id: store.instance().ok().flatten().map(|i| i.instance_id),
targets: BTreeMap::new(),
rebuild_pending: Some(since.unwrap_or_else(stamp_now)),
rebuilt_for: BTreeSet::new(),
});
if state.targets.is_empty() && state.rebuild_pending.is_none() {
return (state, None);
}
let enrolled = store.instance().ok().flatten().map(|i| i.instance_id);
if state.instance_id.is_none() || state.instance_id != enrolled {
return (state, Some(Inactive::NotEnrolled));
}
if lease_lapsed(store, local_now) {
return (state, Some(Inactive::LeaseLapsed));
}
(state, None)
}
pub fn current(store: &HiveStore) -> ControlState {
match current_at(store, chrono::Utc::now()) {
(state, None) => state,
(_, Some(_)) => ControlState::default(),
}
}
pub fn admission(store: &HiveStore, agent_id: Option<&str>) -> Option<Refusal> {
current(store).admission(agent_id)
}
pub fn vetoed(store: &HiveStore, agent_id: Option<&str>, target_ref: &str) -> Option<Refusal> {
current(store).vetoed(agent_id, target_ref)
}
pub fn active(store: &HiveStore) -> Vec<ActiveControl> {
current(store).active()
}
pub fn status_at(
store: &HiveStore,
local_now: chrono::DateTime<chrono::Utc>,
) -> (Vec<ActiveControl>, Option<Inactive>) {
match current_at(store, local_now) {
(state, None) => (state.active(), None),
(state, Some(reason)) => (Vec::new(), (!state.active().is_empty()).then_some(reason)),
}
}
impl From<Inactive> for lean_ctx_protocol::hive::status::HiveBusControlsInactiveV1 {
fn from(reason: Inactive) -> Self {
match reason {
Inactive::NotEnrolled => Self::NotEnrolled,
Inactive::LeaseLapsed => Self::LeaseLapsed,
}
}
}
impl From<ActiveControl> for lean_ctx_protocol::hive::status::HiveStatusBusControlV1 {
fn from(c: ActiveControl) -> Self {
use lean_ctx_protocol::hive::status::HiveBusControlActionV1 as Action;
Self {
target_agent: c.target_agent,
action: match c.action {
ControlAction::Hold => Action::Hold,
ControlAction::Stop => Action::Stop,
ControlAction::Veto => Action::Veto,
ControlAction::Resume => Action::Unknown,
},
target_ref: c.target_ref,
reason: c.reason,
issued_by: c.issued_by,
since: c.since,
}
}
}
pub fn status_view(
store: &HiveStore,
local_now: chrono::DateTime<chrono::Utc>,
) -> (
Vec<lean_ctx_protocol::hive::status::HiveStatusBusControlV1>,
Option<lean_ctx_protocol::hive::status::HiveBusControlsInactiveV1>,
) {
let (active, inactive) = status_at(store, local_now);
(
active.into_iter().map(Into::into).collect(),
inactive.map(Into::into),
)
}
fn exempt(tool: &str) -> bool {
crate::tool_defs::READONLY_TOOL_NAMES.contains(&tool)
|| matches!(tool, "ctx_agent" | "ctx_session")
}
pub fn tool_refusal(agent_id: Option<&str>, tool: &str) -> Option<String> {
if exempt(tool) {
return None;
}
let store = HiveStore::open().ok()?;
admission(&store, agent_id).map(|refusal| refusal.message())
}
pub fn veto_refusal(agent_id: Option<&str>, refs: &[&str]) -> Option<String> {
let store = HiveStore::open().ok()?;
let state = current(&store);
refs.iter()
.find_map(|reference| state.vetoed(agent_id, reference))
.map(|refusal| refusal.message())
.or_else(|| {
refs.iter()
.find_map(|reference| super::veto::vetoed_v2(&store, reference))
})
}
#[cfg(test)]
pub(crate) fn enroll_for_tests(store: &HiveStore, hours_after_issue: i64) -> String {
use crate::core::hive::state::{INSTANCE_FILE, InstanceRecord, LEASE_FILE, LeaseRecord};
let envelope: serde_json::Value = serde_json::from_str(include_str!(
"../../../../crates/lean-ctx-protocol/tests/fixtures/hive/lease_envelope.response.json"
))
.unwrap();
let lease = &envelope["lease"];
let instance_id = lease["instance_id"].as_str().unwrap().to_string();
let issued = chrono::DateTime::parse_from_rfc3339(lease["issued_at"].as_str().unwrap())
.unwrap()
.with_timezone(&chrono::Utc);
let offset = (issued + chrono::Duration::hours(hours_after_issue)) - chrono::Utc::now();
store
.write_config(
INSTANCE_FILE,
&InstanceRecord {
instance_id: instance_id.clone(),
org_id: lease["org_id"].as_str().unwrap().into(),
org_name: "Acme AG".into(),
label: "test-box".into(),
url: "https://hive.example".into(),
org_salt_id: "salt".into(),
enrolled_at: "2026-10-20T12:00:00Z".into(),
web_url: None,
org_kind: None,
company_name: None,
},
)
.unwrap();
store
.write_config(
LEASE_FILE,
&LeaseRecord {
envelope: envelope.clone(),
offset_seconds: offset.num_seconds(),
received_at: "2026-10-20T12:00:00Z".into(),
},
)
.unwrap();
instance_id
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_lapsed_lease_voids_controls_and_status_says_so() {
let dir = tempfile::tempdir().unwrap();
let store = HiveStore::at(dir.path().join("c"), dir.path().join("d"));
enroll_for_tests(&store, 1);
assert!(record(&store, &frame(1, None, ControlAction::Stop, None)).unwrap());
assert_eq!(
admission(&store, Some("a")).unwrap().entry.action,
ControlAction::Stop
);
enroll_for_tests(&store, 30);
assert!(admission(&store, Some("a")).is_some());
let (active_now, inactive) = status_at(&store, chrono::Utc::now());
assert_eq!((active_now.len(), inactive), (1, None));
enroll_for_tests(&store, 24 * 8);
assert!(admission(&store, Some("a")).is_none());
let (active_now, inactive) = status_at(&store, chrono::Utc::now());
assert!(active_now.is_empty());
assert_eq!(inactive, Some(Inactive::LeaseLapsed));
assert_eq!(
inactive.unwrap().status_line(),
"controls inactive: lease lapsed"
);
std::fs::remove_file(dir.path().join("c/lease.json")).unwrap();
assert!(admission(&store, Some("a")).is_none());
}
#[test]
fn damaged_or_newer_controls_hold_every_agent_until_hive_replays() {
let dir = tempfile::tempdir().unwrap();
let store = HiveStore::at(dir.path().join("c"), dir.path().join("d"));
enroll_for_tests(&store, 1);
assert!(record(&store, &frame(1, None, ControlAction::Stop, None)).unwrap());
let path = dir.path().join("d/bus/control.json");
for broken in [&b"{ not json"[..], br#"{"version": 2, "targets": {}}"#] {
std::fs::write(&path, broken).unwrap();
let refusal = admission(&store, Some("a")).expect("fails closed");
assert_eq!(refusal.entry.reason, REBUILD_REASON);
assert!(admission(&store, None).is_some());
assert!(vetoed(&store, Some("a"), "lease_1").is_some());
let (shown, inactive) = status_at(&store, chrono::Utc::now());
assert_eq!((shown.len(), inactive), (1, None), "status names it");
assert!(record(&store, &frame(9, Some("b"), ControlAction::Hold, None)).unwrap());
assert!(admission(&store, Some("a")).is_some(), "still pending");
let pair = |agent: &str, project: &str| (agent.to_string(), project.to_string());
let projects = BTreeSet::from(["p1".to_string(), "p2".to_string()]);
let first = BTreeSet::from([pair("a", "p1")]);
assert!(!rebuilt(&store, &first, &projects).unwrap(), "p2 not yet");
assert!(
admission(&store, Some("a")).is_none(),
"a's replay is confirmed"
);
assert!(vetoed(&store, Some("a"), "lease_1").is_none());
assert!(admission(&store, Some("c")).is_some(), "c's project is not");
assert!(admission(&store, None).is_some());
let later = BTreeSet::from([pair("c", "p2")]);
assert!(rebuilt(&store, &later, &projects).unwrap());
assert!(!rebuilt(&store, &later, &projects).unwrap(), "once");
assert!(admission(&store, Some("c")).is_none());
assert!(admission(&store, None).is_none());
assert!(admission(&store, Some("b")).is_some(), "the replayed hold");
}
assert!(dir.path().join("d/bus/control.json.damaged-1").exists());
assert!(dir.path().join("d/bus/control.json.damaged-2").exists());
}
fn frame(
seq: u64,
target_agent: Option<&str>,
action: ControlAction,
target_ref: Option<&str>,
) -> BusFrameV1 {
BusFrameV1::Control {
control_id: format!("ctl_{seq}"),
target_agent: target_agent.map(str::to_string),
target_ref: target_ref.map(str::to_string),
action,
reason: "Owner review.".into(),
issued_by: "admin@acme".into(),
seq,
server_time: UtcTimestamp::new("2026-10-21T12:00:00Z").unwrap(),
}
}
const I: Option<&str> = Some("inst_1");
#[test]
fn hold_and_stop_refuse_until_resume_and_stop_wins() {
let mut state = ControlState::default();
assert!(state.apply(I, &frame(1, Some("claude-a"), ControlAction::Hold, None)));
assert_eq!(
state.admission(Some("claude-a")).unwrap().entry.action,
ControlAction::Hold
);
assert!(state.admission(Some("claude-b")).is_none());
assert!(state.admission(None).is_none());
assert!(state.apply(I, &frame(2, None, ControlAction::Stop, None)));
let refusal = state.admission(Some("claude-a")).unwrap();
assert_eq!(refusal.entry.action, ControlAction::Stop, "stop beats hold");
assert_eq!(refusal.target, ALL_AGENTS);
assert!(
state.admission(None).is_some(),
"unknown callers are bound by *"
);
assert!(state.apply(I, &frame(3, Some("claude-a"), ControlAction::Resume, None)));
assert_eq!(
state.admission(Some("claude-a")).unwrap().target,
ALL_AGENTS
);
assert!(state.apply(I, &frame(4, None, ControlAction::Resume, None)));
assert!(state.admission(Some("claude-a")).is_none());
}
#[test]
fn replayed_or_older_frames_never_undo_a_newer_control() {
let mut state = ControlState::default();
assert!(state.apply(I, &frame(10, None, ControlAction::Resume, None)));
assert!(state.apply(I, &frame(11, None, ControlAction::Hold, None)));
assert!(!state.apply(I, &frame(10, None, ControlAction::Resume, None)));
assert!(!state.apply(I, &frame(11, None, ControlAction::Hold, None)));
assert!(state.admission(Some("x")).is_some());
}
#[test]
fn a_veto_names_one_lease_and_does_not_replace_a_hold() {
let mut state = ControlState::default();
state.apply(I, &frame(1, None, ControlAction::Hold, None));
state.apply(I, &frame(2, None, ControlAction::Veto, Some("lease_7")));
assert!(
state.admission(Some("a")).is_some(),
"hold survives the veto"
);
assert!(state.vetoed(Some("a"), "lease_7").is_some());
assert!(state.vetoed(Some("a"), "lease_8").is_none());
state.apply(I, &frame(3, Some("a"), ControlAction::Veto, None));
assert!(state.vetoed(Some("a"), "").is_none());
state.apply(I, &frame(4, None, ControlAction::Resume, Some("lease_7")));
assert!(state.vetoed(Some("a"), "lease_7").is_none());
assert!(
state.admission(Some("a")).is_some(),
"resume of a veto keeps the hold"
);
}
#[test]
fn active_lists_modes_and_vetoes_for_status() {
let mut state = ControlState::default();
state.apply(I, &frame(1, Some("codex-9"), ControlAction::Stop, None));
state.apply(I, &frame(2, None, ControlAction::Veto, Some("handoff_1")));
let active = state.active();
assert_eq!(active.len(), 2);
assert_eq!(active[0].target_agent, None);
assert_eq!(active[0].target_ref.as_deref(), Some("handoff_1"));
assert_eq!(active[1].target_agent.as_deref(), Some("codex-9"));
assert_eq!(active[1].action, ControlAction::Stop);
assert_eq!(active[1].since.as_str(), "2026-10-21T12:00:00Z");
}
#[test]
fn controls_belong_to_the_enrollment_and_persist_privately() {
let dir = tempfile::tempdir().unwrap();
let store = HiveStore::at(dir.path().join("c"), dir.path().join("d"));
assert!(record(&store, &frame(1, None, ControlAction::Stop, None)).unwrap());
assert!(admission(&store, Some("a")).is_none());
assert!(active(&store).is_empty());
assert_eq!(
status_at(&store, chrono::Utc::now()),
(Vec::new(), Some(Inactive::NotEnrolled))
);
let path = dir.path().join("d/bus/control.json");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode, 0o600);
}
std::fs::write(&path, br#"{"*":{"t":"control","action":"stop"}}"#).unwrap();
assert!(ControlState::load(&dir.path().join("d")).is_err());
assert!(dir.path().join("d/bus/control.json.damaged-1").exists());
let mut state = ControlState::default();
state.apply(Some("inst_old"), &frame(5, None, ControlAction::Stop, None));
state.apply(
Some("inst_new"),
&frame(1, Some("a"), ControlAction::Hold, None),
);
assert_eq!(state.instance_id.as_deref(), Some("inst_new"));
assert_eq!(
state.admission(Some("a")).unwrap().entry.action,
ControlAction::Hold
);
assert!(state.admission(Some("b")).is_none());
}
#[test]
fn read_only_tools_the_bus_and_the_session_stay_available() {
assert!(exempt("ctx_read") && exempt("ctx_search") && exempt("ctx_agent"));
assert!(exempt("ctx_session"));
assert!(!exempt("ctx_edit") && !exempt("ctx_shell") && !exempt("ctx_patch"));
}
#[test]
fn the_refusal_message_names_the_issuer_and_reason_but_no_ids() {
let mut state = ControlState::default();
state.apply(I, &frame(1, None, ControlAction::Stop, None));
let message = state.admission(Some("a")).unwrap().message();
assert!(message.contains("stopped"), "{message}");
assert!(message.contains("admin@acme") && message.contains("Owner review."));
assert!(!message.contains("ctl_1"));
}
}