use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::cli::harness::HarnessId;
use crate::cli::setup::InstallationType;
use crate::domain::ControlStateDir;
pub const OPERATOR_SETUP_PROFILE_FILE: &str = "operator-setup.json";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthPosture {
#[serde(rename = "loopback-no-key")]
LoopbackNoKey,
#[serde(rename = "network-keyed")]
NetworkKeyed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OperatorSetupProfile {
pub installation_type: InstallationType,
pub port: u16,
pub auth_posture: AuthPosture,
pub harnesses: Vec<String>,
pub updated_ms: i64,
}
impl OperatorSetupProfile {
pub fn new(
installation_type: InstallationType,
port: u16,
auth_posture: AuthPosture,
harnesses: &[HarnessId],
updated_ms: i64,
) -> Self {
Self {
installation_type,
port,
auth_posture,
harnesses: harnesses.iter().map(|h| h.slug().to_string()).collect(),
updated_ms,
}
}
pub fn path(control_state_dir: &ControlStateDir) -> PathBuf {
crate::paths::control_state_path(control_state_dir, OPERATOR_SETUP_PROFILE_FILE)
}
pub fn load(control_state_dir: &ControlStateDir) -> Option<Self> {
let text = std::fs::read_to_string(Self::path(control_state_dir)).ok()?;
serde_json::from_str(&text).ok()
}
pub fn save(&self, control_state_dir: &ControlStateDir) -> std::io::Result<()> {
let path = Self::path(control_state_dir);
crate::paths::ensure_control_state_dir(control_state_dir)?;
let dir = control_state_dir.as_path();
let json = serde_json::to_string_pretty(self).map_err(std::io::Error::other)?;
atomic_write_in(dir, &path, json.as_bytes())
}
}
fn atomic_write_in(dir: &Path, path: &Path, content: &[u8]) -> std::io::Result<()> {
use std::io::Write;
let mut tmp = tempfile::NamedTempFile::new_in(dir)?;
tmp.write_all(content)?;
tmp.flush()?;
tmp.as_file().sync_all()?;
tmp.persist(path).map_err(|e| e.error)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn control_state(root: &Path) -> ControlStateDir {
ControlStateDir::new(root.join("control"))
}
fn sample() -> OperatorSetupProfile {
OperatorSetupProfile::new(
InstallationType::Both,
8787,
AuthPosture::LoopbackNoKey,
&[HarnessId::ClaudeCode, HarnessId::Cursor],
1_750_000_000_000,
)
}
#[test]
fn load_missing_returns_none() {
let dir = tempfile::tempdir().unwrap();
let control = control_state(dir.path());
assert_eq!(OperatorSetupProfile::load(&control), None);
}
#[test]
fn save_then_load_roundtrips() {
let dir = tempfile::tempdir().unwrap();
let control = control_state(dir.path());
let profile = sample();
profile.save(&control).expect("save profile");
let loaded = OperatorSetupProfile::load(&control).expect("load saved profile");
assert_eq!(loaded, profile);
assert_eq!(loaded.harnesses, vec!["claude", "cursor"]);
assert_eq!(loaded.auth_posture, AuthPosture::LoopbackNoKey);
}
#[test]
fn malformed_file_returns_none() {
let dir = tempfile::tempdir().unwrap();
let control = control_state(dir.path());
crate::paths::ensure_control_state_dir(&control).unwrap();
std::fs::write(
OperatorSetupProfile::path(&control),
b"{ this is not valid json",
)
.unwrap();
assert_eq!(OperatorSetupProfile::load(&control), None);
}
#[test]
fn network_keyed_posture_persists_without_key_bytes() {
let dir = tempfile::tempdir().unwrap();
let control = control_state(dir.path());
let profile = OperatorSetupProfile::new(
InstallationType::Server,
9000,
AuthPosture::NetworkKeyed,
&[HarnessId::Codex],
1,
);
profile.save(&control).expect("save");
let text = std::fs::read_to_string(OperatorSetupProfile::path(&control)).unwrap();
assert!(text.contains("network-keyed"));
let loaded = OperatorSetupProfile::load(&control).expect("load");
assert_eq!(loaded.auth_posture, AuthPosture::NetworkKeyed);
}
}