use super::AgentRegistry;
use crate::ipc::process::ProcessIdentity;
use std::collections::HashMap;
use std::path::PathBuf;
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
pub(crate) struct ProcessIdentityIndex {
#[serde(default)]
identities: HashMap<String, ProcessIdentity>,
}
impl ProcessIdentityIndex {
pub(crate) fn load() -> Self {
let Ok(path) = identity_index_path() else {
return Self::default();
};
std::fs::read_to_string(path)
.ok()
.and_then(|content| serde_json::from_str(&content).ok())
.unwrap_or_default()
}
pub(crate) fn get(&self, agent_id: &str) -> Option<&ProcessIdentity> {
self.identities.get(agent_id)
}
pub(crate) fn insert(&mut self, agent_id: &str, identity: &ProcessIdentity) -> bool {
self.identities
.insert(agent_id.to_string(), identity.clone())
.as_ref()
!= Some(identity)
}
pub(crate) fn retain_agents(&mut self, agents: &AgentRegistry) -> bool {
let before = self.identities.len();
self.identities.retain(|agent_id, _| {
agents
.agents
.iter()
.any(|agent| agent.agent_id == *agent_id)
});
self.identities.len() != before
}
pub(crate) fn save(&self) -> Result<(), String> {
let path = identity_index_path()?;
let json = serde_json::to_string_pretty(self).map_err(|error| error.to_string())?;
crate::config_io::write_atomic(&path, &json)
.map_err(|error| format!("persist process identity index {}: {error}", path.display()))
}
}
pub(super) fn agents_dir() -> Result<PathBuf, String> {
let dir = crate::core::data_dir::lean_ctx_data_dir()?;
Ok(dir.join("agents"))
}
fn identity_index_path() -> Result<PathBuf, String> {
Ok(agents_dir()?.join("process-identities.json"))
}
pub(super) fn create_agents_dir(dir: &std::path::Path) -> Result<(), String> {
std::fs::create_dir_all(dir)
.map_err(|error| format!("create agent registry directory {}: {error}", dir.display()))
}
pub(super) fn mutate_persistent<T>(
mutate: impl FnOnce(&mut AgentRegistry) -> T,
) -> Result<T, String> {
let dir = agents_dir()?;
create_agents_dir(&dir)?;
let _lock = FileLock::acquire(&dir.join("registry.lock"))?;
let path = dir.join("registry.json");
let mut registry = load_registry_file(&path)?.unwrap_or_default();
let result = mutate(&mut registry);
save_registry_file(&path, ®istry)?;
Ok(result)
}
pub(super) fn load_registry_file(path: &std::path::Path) -> Result<Option<AgentRegistry>, String> {
match std::fs::read_to_string(path) {
Ok(content) => serde_json::from_str(&content)
.map(Some)
.map_err(|error| format!("agent registry is corrupt at {}: {error}", path.display())),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(error) => Err(format!("read agent registry {}: {error}", path.display())),
}
}
pub(super) fn save_registry_file(
path: &std::path::Path,
registry: &AgentRegistry,
) -> Result<(), String> {
let json = serde_json::to_string_pretty(registry).map_err(|error| error.to_string())?;
crate::config_io::write_atomic(path, &json)
.map_err(|error| format!("persist agent registry {}: {error}", path.display()))
}
pub(super) fn generate_short_id() -> String {
uuid::Uuid::new_v4().simple().to_string()
}
pub(crate) struct FileLock {
file: std::fs::File,
}
impl FileLock {
pub(crate) fn acquire(path: &std::path::Path) -> Result<Self, String> {
use fs2::FileExt;
const TIMEOUT: std::time::Duration = std::time::Duration::from_millis(750);
const RETRY: std::time::Duration = std::time::Duration::from_millis(25);
let file = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(path)
.map_err(|error| format!("agent registry lock {}: {error}", path.display()))?;
let deadline = std::time::Instant::now() + TIMEOUT;
loop {
match file.try_lock_exclusive() {
Ok(()) => return Ok(Self { file }),
Err(error)
if crate::core::file_lock::is_contended(&error)
&& std::time::Instant::now() < deadline =>
{
std::thread::sleep(RETRY);
}
Err(error) if crate::core::file_lock::is_contended(&error) => {
return Err(format!(
"agent registry lock timed out after {}ms",
TIMEOUT.as_millis()
));
}
Err(error) => return Err(format!("agent registry lock: {error}")),
}
}
}
}
impl Drop for FileLock {
fn drop(&mut self) {
let _ = self.file.unlock();
}
}