use std::path::{Path, PathBuf};
pub fn agent_instance_lock(state_dir: &Path, agent_instance_hierarchy: &str) -> (PathBuf, String) {
let mut dir = state_dir.join("locks").join("agents").join("instances");
let mut segments = agent_instance_hierarchy.split('/').peekable();
let mut key = String::new();
while let Some(segment) = segments.next() {
if segments.peek().is_some() {
dir.push(segment);
} else {
key = segment.to_string();
}
}
(dir, key)
}
pub fn agent_tag_lock(state_dir: &Path, agent_tag: &str) -> (PathBuf, String) {
(
state_dir.join("locks").join("agents").join("tags"),
agent_tag.to_string(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn agent_instance_lock_splits_hierarchy() {
let state = Path::new("state");
let (dir, key) = agent_instance_lock(state, "root/child-1/leaf-2");
assert_eq!(
dir,
Path::new("state")
.join("locks")
.join("agents")
.join("instances")
.join("root")
.join("child-1"),
);
assert_eq!(key, "leaf-2");
let (dir, key) = agent_instance_lock(state, "UNKNOWN");
assert_eq!(
dir,
Path::new("state").join("locks").join("agents").join("instances"),
);
assert_eq!(key, "UNKNOWN");
}
}