use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use ulid::Ulid;
pub const HARNESS_ENV: &str = "HIRD_HARNESS";
pub const PROJECT_ENV: &str = "HIRD_PROJECT";
pub const DB_ENV: &str = "HIRD_DB";
pub const ACTOR_CLI: &str = "cli";
pub const ACTOR_TUI: &str = "tui";
pub const ACTOR_WEB: &str = "web";
const HARNESS_MAX: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentId {
harness: OnceLock<String>,
session: String,
}
impl AgentId {
pub fn from_env() -> AgentId {
AgentId::new(
std::env::var(HARNESS_ENV).unwrap_or_default(),
short_session_id(),
)
}
pub fn new(harness: impl Into<String>, session: impl Into<String>) -> AgentId {
let id = AgentId {
harness: OnceLock::new(),
session: sanitize(&session.into()),
};
id.name_harness(&harness.into());
id
}
pub fn name_from_client(&self, client_name: &str) -> bool {
self.name_harness(client_name)
}
fn name_harness(&self, raw: &str) -> bool {
let mut name = sanitize(raw);
if name.is_empty() {
return false;
}
name.truncate(
name.char_indices()
.nth(HARNESS_MAX)
.map_or(name.len(), |(i, _)| i),
);
self.harness.set(name).is_ok()
}
pub fn harness(&self) -> &str {
self.harness.get().map_or("unknown", String::as_str)
}
pub fn session(&self) -> &str {
&self.session
}
pub fn as_actor(&self) -> String {
format!("{}:{}", self.harness(), self.session)
}
}
impl std::fmt::Display for AgentId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.harness(), self.session)
}
}
pub fn actor_harness(actor: &str) -> &str {
actor.split_once(':').map_or(actor, |(h, _)| h)
}
fn short_session_id() -> String {
let ulid = Ulid::generate().to_string().to_lowercase();
ulid[ulid.len() - 4..].to_string()
}
fn sanitize(raw: &str) -> String {
raw.trim()
.chars()
.filter(|c| !c.is_whitespace() && *c != ':' && *c != ',')
.collect()
}
pub fn resolve_project(cwd: &Path) -> String {
if let Ok(explicit) = std::env::var(PROJECT_ENV) {
let explicit = explicit.trim();
if !explicit.is_empty() {
return canonical(Path::new(explicit));
}
}
match git_toplevel(cwd) {
Some(root) => canonical(&root),
None => canonical(cwd),
}
}
fn git_toplevel(start: &Path) -> Option<PathBuf> {
let mut dir = Some(start);
while let Some(current) = dir {
if current.join(".git").exists() {
return Some(current.to_path_buf());
}
dir = current.parent();
}
None
}
pub fn canonical_project(path: &Path) -> String {
canonical(path)
}
fn canonical(path: &Path) -> String {
let resolved = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
resolved.to_string_lossy().into_owned()
}
pub fn default_db_path() -> PathBuf {
data_dir().join("hird").join("hird.db")
}
pub fn default_config_path() -> PathBuf {
config_dir().join("hird").join("config.toml")
}
fn data_dir() -> PathBuf {
non_empty_env("XDG_DATA_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home().join(".local").join("share"))
}
pub(crate) fn config_dir() -> PathBuf {
non_empty_env("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home().join(".config"))
}
pub fn home() -> PathBuf {
non_empty_env("HOME")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."))
}
fn non_empty_env(key: &str) -> Option<String> {
std::env::var(key).ok().filter(|v| !v.trim().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn agent_ids_render_as_harness_colon_session() {
let id = AgentId::new("claude-code", "af31");
assert_eq!(id.as_actor(), "claude-code:af31");
assert_eq!(id.to_string(), "claude-code:af31");
assert_eq!(id.harness(), "claude-code");
assert_eq!(id.session(), "af31");
}
#[test]
fn a_missing_harness_name_becomes_unknown() {
assert_eq!(AgentId::new("", "af31").harness(), "unknown");
assert_eq!(AgentId::new(" ", "af31").harness(), "unknown");
}
#[test]
fn a_nameless_identity_takes_the_name_the_client_gives_for_itself() {
let id = AgentId::new("", "af31");
assert!(id.name_from_client("codex-cli"));
assert_eq!(id.as_actor(), "codex-cli:af31");
}
#[test]
fn the_environment_outranks_whatever_the_client_calls_itself() {
let id = AgentId::new("claude-code", "af31");
assert!(!id.name_from_client("something-else"));
assert_eq!(id.harness(), "claude-code");
}
#[test]
fn the_first_client_name_is_the_one_that_sticks() {
let id = AgentId::new("", "af31");
assert!(id.name_from_client("codex-cli"));
assert!(!id.name_from_client("copilot"));
assert_eq!(id.harness(), "codex-cli");
}
#[test]
fn a_client_that_names_itself_nothing_leaves_the_identity_open() {
let id = AgentId::new("", "af31");
assert!(!id.name_from_client(" "));
assert_eq!(id.harness(), "unknown");
assert!(id.name_from_client("cursor"));
assert_eq!(id.harness(), "cursor");
}
#[test]
fn a_client_cannot_make_its_name_an_essay() {
let id = AgentId::new("", "af31");
id.name_from_client(&"x".repeat(500));
assert_eq!(id.harness().len(), HARNESS_MAX);
}
#[test]
fn a_client_name_cannot_forge_a_second_field_either() {
let id = AgentId::new("", "af31");
id.name_from_client("cla ude:code");
assert_eq!(id.as_actor(), "claudecode:af31");
}
#[test]
fn colons_and_whitespace_cannot_forge_a_second_field() {
let id = AgentId::new("cla ude:code", "af:31");
assert_eq!(id.as_actor(), "claudecode:af31");
}
#[test]
fn commas_cannot_forge_an_entry_in_the_recused_list() {
let id = AgentId::new("codex,claude-code", "af31");
assert_eq!(id.harness(), "codexclaude-code");
}
#[test]
fn session_ids_are_four_characters_and_differ() {
let a = short_session_id();
let b = short_session_id();
assert_eq!(a.len(), 4);
assert!(a.chars().all(|c| c.is_ascii_alphanumeric()));
assert!(a != b || short_session_id() != a);
}
#[test]
fn actor_harness_splits_agents_and_passes_humans_through() {
assert_eq!(actor_harness("claude-code:af31"), "claude-code");
assert_eq!(actor_harness("cli"), "cli");
assert_eq!(actor_harness("tui"), "tui");
}
#[test]
fn project_detection_finds_the_enclosing_git_root() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
std::fs::create_dir(root.join(".git")).unwrap();
let nested = root.join("crates").join("inner");
std::fs::create_dir_all(&nested).unwrap();
let found = git_toplevel(&nested).unwrap();
assert_eq!(
std::fs::canonicalize(found).unwrap(),
std::fs::canonicalize(root).unwrap()
);
}
#[test]
fn project_detection_falls_back_to_the_directory_itself() {
let dir = tempfile::tempdir().unwrap();
assert!(git_toplevel(dir.path()).is_none());
}
#[test]
fn worktree_git_files_count_as_a_root() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(".git"), "gitdir: /elsewhere").unwrap();
assert!(git_toplevel(dir.path()).is_some());
}
#[test]
fn default_paths_sit_under_the_hird_directory() {
assert!(default_db_path().ends_with("hird/hird.db"));
assert!(default_config_path().ends_with("hird/config.toml"));
}
}