use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
pub const DEFAULT_ENGINE: &str = "agix";
pub const DEFAULT_PROJECT_DIR: &str = ".agix";
pub const DEFAULT_STATE_DIR: &str = ".agix";
pub const FOREIGN_CONFIG_ROOTS: &[&str] = &[DEFAULT_PROJECT_DIR, CLAUDE_DIR];
pub const CLAUDE_DIR: &str = ".claude";
pub const CLAUDE_CREDENTIALS: &str = ".claude/.credentials.json";
pub const AGENTS_DIR: &str = ".agents";
pub const PROJECT_SKILLS_DIR: &str = ".agents/skills";
pub const DEFAULT_PRODUCT_NAME: &str = "Agix";
pub const DEFAULT_PRODUCT_URL: &str = "https://github.com/harnixai/agix";
pub const GIT_WORKTREES_DIR: &str = "worktrees";
pub const DEFAULT_WORKTREES_DIR: &str = ".wkts";
pub mod entry {
pub const SETTINGS: &str = "settings.json";
pub const SETTINGS_LOCAL: &str = "settings.local.json";
pub const MCP_LOCAL: &str = "mcp.local.json";
pub const SCENE_FILE: &str = "scene.json";
pub const SCENE_PROMPT: &str = "prompt.md";
pub const SCHEDULED_TASKS: &str = "scheduled_tasks.json";
pub const IMPORTED_MARKER: &str = ".imported.json";
pub const SKILLS: &str = "skills";
pub const AGENTS: &str = "agents";
pub const HOOKS: &str = "hooks";
pub const COMMANDS: &str = "commands";
pub const RULES: &str = "rules";
pub const OUTPUT_STYLES: &str = "output-styles";
pub const PLUGINS: &str = "plugins";
pub const CACHE: &str = "cache";
pub const SCENES: &str = "scenes";
pub const MCP: &str = "mcp";
pub const OAUTH: &str = "oauth";
pub const POLICY: &str = "policy";
pub const SCRIPTS: &str = "scripts";
pub const MEMORY: &str = "memory";
pub const SCRATCHPAD: &str = "scratchpad";
pub const TMP: &str = "tmp";
pub const TEAMS: &str = "teams";
pub const TELEMETRY: &str = "telemetry";
pub const TOOL_RESULTS: &str = "tool-results";
pub const STATE: &str = "state";
pub const HISTORY: &str = "history";
pub const SESSIONS: &str = "sessions";
pub const PROJECTS: &str = "projects";
pub const RECORDINGS: &str = "recordings";
pub const QUOTA: &str = "quota";
pub const DAEMON: &str = "daemon";
pub const INSTANCES: &str = "instances.d";
pub const AUTH: &str = "auth";
pub const GROUPS: &str = "groups";
pub const CRASHES: &str = "crashes";
pub const MANAGED_SETTINGS: &str = "managed-settings.json";
pub const MCP_POLICY: &str = "mcp-policy.json";
}
pub const SETTINGS_FILE: &str = entry::SETTINGS;
pub const SETTINGS_LOCAL_FILE: &str = entry::SETTINGS_LOCAL;
pub const STATE_ROOT_WORKING_DIRS: &[&str] = &[STATE_ROOT_SCRATCHPAD, entry::MEMORY];
pub const STATE_ROOT_SCRATCHPAD: &str = entry::SCRATCHPAD;
pub const STATE_ROOT_SESSION_TMP: &str = entry::TMP;
pub const ENGINE_CONFIG_FILES: &[&str] =
&[entry::SETTINGS, entry::SETTINGS_LOCAL, entry::MCP_LOCAL];
pub const ENGINE_CONFIG_DIRS: &[&str] = &[
entry::MCP,
entry::HOOKS,
entry::COMMANDS,
entry::AGENTS,
entry::SKILLS,
entry::PLUGINS,
entry::RULES,
entry::SCENES,
entry::POLICY,
entry::SCRIPTS,
entry::OUTPUT_STYLES,
entry::TOOL_RESULTS,
];
pub const PROJECT_ENGINE_CONFIG_DIRS: &[&str] = &[PROJECT_SKILLS_DIR];
pub const STATE_ROOT_SCENE_FILES: &[&str] =
&[entry::SETTINGS, entry::SCENE_FILE, entry::SCENE_PROMPT];
pub const LOCAL_CONFIG_FILES: &[&str] = &[entry::SETTINGS_LOCAL, entry::MCP_LOCAL];
pub const PROJECT_STATE_ENTRIES: &[&str] = &[
entry::MEMORY,
entry::SCRATCHPAD,
entry::TEAMS,
entry::TELEMETRY,
entry::TOOL_RESULTS,
entry::IMPORTED_MARKER,
];
pub const PROJECT_STATE_ENGINE_DIRS: &[&str] = &[entry::TOOL_RESULTS, entry::HISTORY];
pub const STATE_RESERVED_NAMES: &[&str] = &[
entry::MEMORY,
entry::SCRATCHPAD,
entry::TEAMS,
entry::TELEMETRY,
entry::TOOL_RESULTS,
entry::HISTORY,
"imported",
entry::SESSIONS,
entry::CACHE,
entry::TMP,
];
pub const OTHER_TOOLS_DIRS: &[&str] = &[
super::REPOSITORY_CONFIG_DIR,
AGENTS_DIR,
CLAUDE_DIR,
".codex",
".cursor",
".github",
".vscode",
".idea",
];
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{name}` cannot name the project directory: {reason}")]
pub struct LayoutError {
pub name: String,
pub reason: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{name}` cannot name the worktrees directory: {reason}")]
pub struct WorktreesDirError {
pub name: String,
pub reason: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{name}` cannot name the engine: {reason}")]
pub struct EngineNameError {
pub name: String,
pub reason: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{name}` cannot name the product: {reason}")]
pub struct ProductNameError {
pub name: String,
pub reason: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Layout {
engine: Arc<str>,
project_dir: Arc<str>,
product: Arc<str>,
worktrees: Arc<str>,
}
impl Default for Layout {
fn default() -> Self {
Self {
engine: Arc::from(DEFAULT_ENGINE),
project_dir: Arc::from(DEFAULT_PROJECT_DIR),
product: Arc::from(DEFAULT_PRODUCT_NAME),
worktrees: Arc::from(DEFAULT_WORKTREES_DIR),
}
}
}
fn not_hidden_component(
name: &str,
allowed: fn(char) -> bool,
charset: &'static str,
) -> Option<&'static str> {
if name.is_empty() {
Some("it is empty")
} else if name.len() > 64 {
Some("it is longer than 64 characters")
} else if !name.chars().all(allowed) {
Some(charset)
} else if name == "." || name == ".." {
Some("it is not a directory of its own")
} else if !name.starts_with('.') {
Some("it must start with `.`, as a directory a project does not use for its own files")
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConfigRoot<'a> {
pub name: &'a str,
pub own: bool,
}
impl Layout {
pub fn new(project_dir: &str) -> Result<Self, LayoutError> {
Self::default().with_project_dir(project_dir)
}
pub fn with_project_dir(self, name: &str) -> Result<Self, LayoutError> {
match self.refuse_project_dir(name) {
Some(reason) => Err(LayoutError {
name: name.to_string(),
reason,
}),
None => Ok(Self {
project_dir: Arc::from(name),
..self
}),
}
}
fn refuse_project_dir(&self, name: &str) -> Option<&'static str> {
let lowercase = |c: char| c.is_ascii_lowercase() || c.is_ascii_digit() || "._-".contains(c);
not_hidden_component(
name,
lowercase,
"only lowercase letters, digits, `.`, `_` and `-` are allowed",
)
.or_else(|| {
OTHER_TOOLS_DIRS
.contains(&name)
.then_some("that directory belongs to another tool")
})
.or_else(|| {
(name == DEFAULT_WORKTREES_DIR || super::fold_case(&self.worktrees) == name)
.then_some("that is where the engine's worktrees go")
})
}
pub fn with_engine(self, name: &str) -> Result<Self, EngineNameError> {
let refuse = |reason| {
Err(EngineNameError {
name: name.to_string(),
reason,
})
};
if name.len() > 16 {
return refuse("it is longer than 16 characters");
}
if let Err(reason) = crate::interface::script::check_global_name(name) {
return refuse(reason);
}
let project_dir = format!(".{name}");
if self.refuse_project_dir(&project_dir).is_some() {
return refuse("its project directory would be another tool's, or the worktrees'");
}
Ok(Self {
engine: Arc::from(name),
project_dir: Arc::from(project_dir),
..self
})
}
pub fn with_worktrees_dir(self, name: &str) -> Result<Self, WorktreesDirError> {
let ascii = |c: char| c.is_ascii_alphanumeric() || "._-".contains(c);
let folded = super::fold_case(name);
let reason = not_hidden_component(
name,
ascii,
"only ASCII letters, digits, `.`, `_` and `-` are allowed",
)
.or_else(|| {
self.config_roots()
.any(|root| root.name == folded)
.then_some("that directory is a configuration root")
})
.or_else(|| {
(OTHER_TOOLS_DIRS.contains(&folded.as_str())
|| folded.starts_with(super::REPOSITORY_CONFIG_DIR))
.then_some("that directory belongs to another tool")
});
match reason {
Some(reason) => Err(WorktreesDirError {
name: name.to_string(),
reason,
}),
None => Ok(Self {
worktrees: Arc::from(name),
..self
}),
}
}
pub fn with_product_name(self, name: &str) -> Result<Self, ProductNameError> {
let refuse = |reason| {
Err(ProductNameError {
name: name.to_string(),
reason,
})
};
let mut chars = name.chars();
match chars.next() {
None => return refuse("it is empty"),
Some(c) if !c.is_ascii_alphabetic() => {
return refuse("it must start with an ASCII letter")
}
Some(_) => {}
}
if name.len() > 32 {
return refuse("it is longer than 32 characters");
}
if !chars.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') {
return refuse("only ASCII letters, digits, `-` and `_` are allowed");
}
if STATE_RESERVED_NAMES.contains(&name.to_ascii_lowercase().as_str()) {
return refuse(
"the engine keeps its own state under that name in a project's `state/`",
);
}
Ok(Self {
product: Arc::from(name),
..self
})
}
pub fn engine(&self) -> &str {
&self.engine
}
pub fn state_dir_names(&self) -> impl Iterator<Item = String> {
let own = format!(".{}", self.engine);
let agixd = (own != DEFAULT_STATE_DIR).then(|| DEFAULT_STATE_DIR.to_string());
std::iter::once(own).chain(agixd)
}
pub fn project_dir_name(&self) -> &str {
&self.project_dir
}
pub fn worktrees_dir_name(&self) -> &str {
&self.worktrees
}
pub fn product_name(&self) -> &str {
&self.product
}
pub fn product_id(&self) -> String {
self.product.to_ascii_lowercase()
}
pub fn user_agent(&self) -> String {
format!("{}/{}", self.product_id(), env!("CARGO_PKG_VERSION"))
}
pub fn worktree_branch_prefix(&self) -> String {
format!("{}/worktree-", self.product_id())
}
pub fn quarantine_prefix(&self) -> String {
format!(".git.{}-quarantine-", self.product_id())
}
pub fn machine_dir(&self) -> PathBuf {
#[cfg(target_os = "macos")]
{
PathBuf::from("/Library/Application Support").join(self.product_name())
}
#[cfg(windows)]
{
#[expect(
clippy::disallowed_methods,
reason = "where Windows keeps machine-wide program data is a fact about this machine"
)]
let root = std::env::var_os("ProgramData")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\ProgramData"));
root.join(self.product_name())
}
#[cfg(not(any(target_os = "macos", windows)))]
{
PathBuf::from("/etc").join(self.product_id())
}
}
pub fn managed_settings_file(&self) -> PathBuf {
self.machine_dir().join(entry::MANAGED_SETTINGS)
}
pub fn mcp_policy_file(&self) -> PathBuf {
self.machine_dir().join(entry::MCP_POLICY)
}
pub fn attribution(&self) -> String {
if &*self.product == DEFAULT_PRODUCT_NAME {
format!("[{DEFAULT_PRODUCT_NAME}]({DEFAULT_PRODUCT_URL})")
} else {
self.product.to_string()
}
}
pub fn project_dir(&self, project_root: &Path) -> PathBuf {
project_root.join(&*self.project_dir)
}
pub fn worktrees_dir(&self, repo_root: &Path) -> PathBuf {
repo_root.join(&*self.worktrees)
}
pub fn worktrees_dir_names(&self) -> impl Iterator<Item = &str> {
let own: &str = &self.worktrees;
std::iter::once(own).chain((own != DEFAULT_WORKTREES_DIR).then_some(DEFAULT_WORKTREES_DIR))
}
pub fn project_state_dir(&self, project_root: &Path) -> PathBuf {
self.project_dir(project_root).join(entry::STATE)
}
pub fn host_state_dir(&self, project_root: &Path) -> PathBuf {
self.project_state_dir(project_root).join(self.product_id())
}
pub fn teams_dir(&self, dir: &Path) -> PathBuf {
self.project_state_dir(dir).join(entry::TEAMS)
}
pub fn gitignore_entry(&self) -> String {
format!("{}/", self.worktrees)
}
pub fn config_roots(&self) -> impl Iterator<Item = ConfigRoot<'_>> {
let own: &str = &self.project_dir;
std::iter::once(ConfigRoot {
name: own,
own: true,
})
.chain(
FOREIGN_CONFIG_ROOTS
.iter()
.filter(move |name| **name != own)
.map(|name| ConfigRoot { name, own: false }),
)
}
pub fn engine_dir_names(&self) -> impl Iterator<Item = &str> {
let own: &str = &self.project_dir;
std::iter::once(own).chain((own != DEFAULT_PROJECT_DIR).then_some(DEFAULT_PROJECT_DIR))
}
pub fn is_own_dir_component(&self, component: &str) -> bool {
super::fold_case(component) == *self.project_dir
}
pub fn main_checkout_copies(&self, path: &Path) -> Vec<PathBuf> {
let mut copies = Vec::new();
let mut current: Vec<Component<'_>> = path.components().collect();
while let Some(at) = current.windows(2).rposition(|w| self.opens_a_worktree(w)) {
current.drain(at..at + 2);
copies.push(current.iter().collect());
}
copies
}
pub fn worktree_of(&self, path: &Path) -> Option<PathBuf> {
let parts: Vec<Component<'_>> = path.components().collect();
let at = parts.windows(2).position(|w| self.opens_a_worktree(w))?;
Some(parts.get(..at + 2)?.iter().collect())
}
fn opens_a_worktree(&self, pair: &[Component<'_>]) -> bool {
let [Component::Normal(dir), Component::Normal(_)] = pair else {
return false;
};
let dir = super::fold_case(&dir.to_string_lossy());
self.worktrees_dir_names()
.any(|name| super::fold_case(name) == dir)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct StateRoot(PathBuf);
impl StateRoot {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self(root.into())
}
pub fn path(&self) -> &Path {
&self.0
}
pub fn into_path(self) -> PathBuf {
self.0
}
pub fn settings_file(&self) -> PathBuf {
self.0.join(entry::SETTINGS)
}
pub fn settings_local_file(&self) -> PathBuf {
self.0.join(entry::SETTINGS_LOCAL)
}
pub fn scenes_dir(&self) -> PathBuf {
self.0.join(entry::SCENES)
}
pub fn scene_dir(&self, scene: &str) -> PathBuf {
self.scenes_dir().join(scene)
}
pub fn scheduled_tasks_file(&self, scene: &str) -> PathBuf {
self.scene_dir(scene).join(entry::SCHEDULED_TASKS)
}
pub fn plugins_dir(&self) -> PathBuf {
self.0.join(entry::PLUGINS)
}
pub fn plugin_cache_dir(&self) -> PathBuf {
self.plugins_dir().join(entry::CACHE)
}
pub fn tmp_dir(&self) -> PathBuf {
self.0.join(entry::TMP)
}
pub fn projects_dir(&self) -> PathBuf {
self.0.join(entry::PROJECTS)
}
pub fn sessions_dir(&self) -> PathBuf {
self.0.join(entry::SESSIONS)
}
pub fn recordings_dir(&self) -> PathBuf {
self.0.join(entry::RECORDINGS)
}
pub fn daemon_dir(&self) -> PathBuf {
self.0.join(entry::DAEMON)
}
pub fn instances_dir(&self) -> PathBuf {
self.daemon_dir().join(entry::INSTANCES)
}
pub fn auth_dir(&self) -> PathBuf {
self.daemon_dir().join(entry::AUTH)
}
pub fn groups_dir(&self) -> PathBuf {
self.daemon_dir().join(entry::GROUPS)
}
pub fn groups_file(&self, instance: &str) -> PathBuf {
self.groups_dir().join(format!("{instance}.json"))
}
pub fn crashes_dir(&self) -> PathBuf {
self.daemon_dir().join(entry::CRASHES)
}
}
impl AsRef<Path> for StateRoot {
fn as_ref(&self) -> &Path {
&self.0
}
}
#[cfg(test)]
pub(crate) fn test_layouts() -> [Layout; 2] {
[
Layout::default(),
Layout::new(".codix")
.unwrap()
.with_worktrees_dir(".trees")
.unwrap(),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_name_is_one_hidden_lowercase_component_of_no_other_tool() {
for good in [".agix", ".codix", ".agent-state", ".x_1.d"] {
assert_eq!(Layout::new(good).unwrap().project_dir_name(), good);
}
for bad in [
"",
".",
"..",
"a/b",
"../x",
".Agix",
".git",
".agents",
".claude",
"a b",
"a\\b",
"x$y",
"src",
"docs",
"node_modules",
"agent-state",
".github",
".vscode",
".idea",
".codex",
".cursor",
] {
assert!(Layout::new(bad).is_err(), "{bad:?}");
}
assert!(Layout::new(&"a".repeat(65)).is_err());
assert!(Layout::new(DEFAULT_WORKTREES_DIR).is_err());
assert_eq!(Layout::default().project_dir_name(), DEFAULT_PROJECT_DIR);
}
#[test]
fn a_worktrees_dir_is_one_hidden_component_of_no_configuration_root() {
for good in [
".wkts",
".trees",
".Trees",
".work_trees-2",
&format!(".{}", "a".repeat(63)),
] {
let layout = Layout::default().with_worktrees_dir(good).unwrap();
assert_eq!(layout.worktrees_dir_name(), good);
assert_eq!(layout.project_dir_name(), DEFAULT_PROJECT_DIR);
}
let codix = Layout::new(".codix").unwrap();
for bad in [
"",
".",
"..",
"wkts",
"a/b",
".a/b",
"../x",
".a b",
".a\\b",
".x$y",
".tr\u{e9}es",
".git",
".GIT",
".gitignore",
".gitmodules",
".GitAttributes",
".git-blame-ignore-revs",
".gitlab",
".agix",
".AGIX",
".claude",
".Claude",
".agents",
".github",
".vscode",
".idea",
".codex",
".cursor",
&format!(".{}", "a".repeat(64)),
] {
assert!(
Layout::default().with_worktrees_dir(bad).is_err(),
"{bad:?}"
);
assert!(codix.clone().with_worktrees_dir(bad).is_err(), "{bad:?}");
}
assert!(codix.clone().with_worktrees_dir(".codix").is_err());
assert!(codix.clone().with_worktrees_dir(".Codix").is_err());
assert!(Layout::default().with_worktrees_dir(".codix").is_ok());
assert_eq!(
Layout::default().worktrees_dir_name(),
DEFAULT_WORKTREES_DIR
);
let named = Layout::new(".codix")
.unwrap()
.with_product_name("Codix")
.unwrap()
.with_worktrees_dir(".trees")
.unwrap();
assert_eq!(
(named.project_dir_name(), named.product_name()),
(".codix", "Codix")
);
assert_eq!(
named
.with_product_name("Other")
.unwrap()
.worktrees_dir_name(),
".trees"
);
}
#[test]
fn a_product_name_is_a_letter_then_letters_digits_dashes_underscores() {
for good in ["Agix", "Codix", "x", "My-Agent_2", &"A".repeat(32)] {
let layout = Layout::default().with_product_name(good).unwrap();
assert_eq!(layout.product_name(), good);
assert_eq!(layout.project_dir_name(), DEFAULT_PROJECT_DIR);
}
for bad in [
"",
"1x",
"-x",
"a b",
"a/b",
"a.b",
"a..b",
"a@b",
"Ägix",
&"A".repeat(33),
] {
assert!(Layout::default().with_product_name(bad).is_err(), "{bad:?}");
}
assert_eq!(Layout::default().product_name(), DEFAULT_PRODUCT_NAME);
}
#[test]
fn a_product_id_is_none_of_the_names_the_engine_keeps_in_state() {
for reserved in STATE_RESERVED_NAMES
.iter()
.filter(|n| !n.starts_with('.') && !n.contains('-'))
{
let capitalised = format!("{}{}", reserved[..1].to_uppercase(), &reserved[1..]);
for name in [reserved.to_string(), capitalised, reserved.to_uppercase()] {
assert!(
Layout::default().with_product_name(&name).is_err(),
"{name}"
);
}
}
for name in ["Tool-Results", "History", "Imported"] {
assert!(Layout::default().with_product_name(name).is_err(), "{name}");
}
for entry in PROJECT_STATE_ENTRIES
.iter()
.chain(PROJECT_STATE_ENGINE_DIRS)
{
let id = entry.trim_start_matches('.').trim_end_matches(".json");
assert!(STATE_RESERVED_NAMES.contains(&id), "{entry}");
}
assert!(!STATE_RESERVED_NAMES.contains(&DEFAULT_PRODUCT_NAME.to_ascii_lowercase().as_str()));
let codix = Layout::default().with_product_name("Codix").unwrap();
assert_eq!(
codix.host_state_dir(Path::new("/repo")),
Path::new("/repo/.agix/state/codix")
);
}
#[test]
fn an_engine_name_is_a_lowercase_script_global_and_names_the_project_directory() {
let agix = Layout::default();
assert_eq!(agix.engine(), DEFAULT_ENGINE);
assert_eq!(format!(".{DEFAULT_ENGINE}"), DEFAULT_PROJECT_DIR);
assert_eq!(agix.state_dir_names().collect::<Vec<_>>(), [".agix"]);
for good in ["foo", "x", "codix2", &"a".repeat(16)] {
let layout = Layout::default().with_engine(good).unwrap();
assert_eq!(layout.engine(), good);
assert_eq!(layout.project_dir_name(), format!(".{good}"));
assert_eq!(layout.product_name(), DEFAULT_PRODUCT_NAME);
}
let foo = Layout::default().with_engine("foo").unwrap();
assert_eq!(foo.state_dir_names().collect::<Vec<_>>(), [".foo", ".agix"]);
assert_eq!(
foo.config_roots().map(|r| r.name).collect::<Vec<_>>(),
[".foo", ".agix", ".claude"]
);
let named = foo.with_project_dir(".codix").unwrap();
assert_eq!(
(named.engine(), named.project_dir_name()),
("foo", ".codix")
);
for bad in [
"",
"Foo",
"1x",
"a-b",
"a_b",
"a.b",
"f\u{f6}o",
"class",
"new",
"tools",
"console",
"escape",
"constructor",
"git",
"claude",
"codex",
"wkts",
&"a".repeat(17),
] {
assert!(Layout::default().with_engine(bad).is_err(), "{bad:?}");
}
let trees = Layout::default().with_worktrees_dir(".foo").unwrap();
assert!(trees.clone().with_engine("foo").is_err());
assert!(trees.with_project_dir(".foo").is_err());
}
#[test]
fn what_a_product_name_names_follows_it() {
let codix = Layout::new(".codix")
.unwrap()
.with_product_name("Codix")
.unwrap();
assert_eq!(codix.project_dir_name(), ".codix");
assert_eq!(codix.product_id(), "codix");
assert_eq!(
codix.user_agent(),
format!("codix/{}", env!("CARGO_PKG_VERSION"))
);
assert_eq!(codix.worktree_branch_prefix(), "codix/worktree-");
assert_eq!(codix.quarantine_prefix(), ".git.codix-quarantine-");
assert_eq!(codix.attribution(), "Codix");
let agix = Layout::default();
assert_eq!(agix.worktree_branch_prefix(), "agix/worktree-");
assert_eq!(agix.quarantine_prefix(), ".git.agix-quarantine-");
assert_eq!(
agix.user_agent(),
format!("agix/{}", env!("CARGO_PKG_VERSION"))
);
assert_eq!(
agix.attribution(),
"[Agix](https://github.com/harnixai/agix)"
);
for layout in [&agix, &codix] {
assert!(layout.machine_dir().is_absolute());
assert_eq!(
layout.managed_settings_file(),
layout.machine_dir().join("managed-settings.json")
);
assert_eq!(
layout.mcp_policy_file(),
layout.machine_dir().join("mcp-policy.json")
);
}
assert!(codix
.machine_dir()
.to_string_lossy()
.to_lowercase()
.ends_with("codix"));
assert!(!codix
.machine_dir()
.to_string_lossy()
.to_lowercase()
.contains("agix"));
}
#[test]
fn its_own_root_comes_first_and_the_foreign_ones_follow_without_it() {
let roots = |l: &Layout| -> Vec<(String, bool)> {
l.config_roots()
.map(|r| (r.name.to_string(), r.own))
.collect()
};
assert_eq!(
roots(&Layout::default()),
[(".agix".into(), true), (".claude".into(), false)]
);
assert_eq!(
roots(&Layout::new(".codix").unwrap()),
[
(".codix".into(), true),
(".agix".into(), false),
(".claude".into(), false)
]
);
assert_eq!(
Layout::new(".codix")
.unwrap()
.engine_dir_names()
.collect::<Vec<_>>(),
[".codix", ".agix"]
);
assert_eq!(
Layout::default().engine_dir_names().collect::<Vec<_>>(),
[".agix"]
);
}
#[test]
fn the_derived_paths_follow_the_name() {
for layout in test_layouts() {
let name = layout.project_dir_name().to_string();
let worktrees = layout.worktrees_dir_name().to_string();
let root = Path::new("/repo");
assert_eq!(layout.project_dir(root), root.join(&name));
assert_eq!(layout.worktrees_dir(root), root.join(&worktrees));
assert_eq!(
layout.project_state_dir(root),
root.join(&name).join("state")
);
assert_eq!(layout.teams_dir(root), root.join(&name).join("state/teams"));
assert_eq!(
layout.host_state_dir(root),
root.join(&name).join("state").join(layout.product_id())
);
assert_eq!(layout.gitignore_entry(), format!("{worktrees}/"));
assert!(layout.is_own_dir_component(&name.to_uppercase()));
}
assert_eq!(
Layout::default().worktrees_dir(Path::new("/repo")),
Path::new("/repo/.wkts")
);
assert_eq!(Layout::default().gitignore_entry(), ".wkts/");
assert_eq!(
Layout::default().worktrees_dir_names().collect::<Vec<_>>(),
[".wkts"]
);
assert_eq!(
Layout::default()
.with_worktrees_dir(".trees")
.unwrap()
.worktrees_dir_names()
.collect::<Vec<_>>(),
[".trees", ".wkts"]
);
}
#[test]
fn a_path_in_a_worktree_has_its_main_checkout_copies() {
for layout in test_layouts() {
let wk = layout.worktrees_dir_name();
let nested = format!("/repo/{wk}/a/{wk}/b/src/x.rs");
assert_eq!(
layout.main_checkout_copies(Path::new(&nested)),
[
PathBuf::from(format!("/repo/{wk}/a/src/x.rs")),
PathBuf::from("/repo/src/x.rs")
]
);
assert_eq!(
layout.worktree_of(Path::new(&nested)),
Some(PathBuf::from(format!("/repo/{wk}/a")))
);
let upper = format!("/repo/{}/a/src/x.rs", wk.to_uppercase());
assert_eq!(
layout.main_checkout_copies(Path::new(&upper)),
[PathBuf::from("/repo/src/x.rs")]
);
assert_eq!(
layout.worktree_of(Path::new(&upper)),
Some(PathBuf::from(format!("/repo/{}/a", wk.to_uppercase())))
);
assert!(layout
.main_checkout_copies(Path::new(&format!("/repo/{wk}")))
.is_empty());
assert_eq!(layout.worktree_of(Path::new("/repo/src/x.rs")), None);
assert_eq!(
layout.main_checkout_copies(Path::new(&format!("/repo/{wk}/wt"))),
[PathBuf::from("/repo")]
);
let own = layout.project_dir_name();
for not_one in [
format!("/repo/{own}/sessions/s/x"),
format!("/repo/{own}/worktrees/a/x"),
"/repo/src/a".to_string(),
"/repo/wkts/a/x".to_string(),
] {
assert!(
layout.main_checkout_copies(Path::new(¬_one)).is_empty(),
"{not_one}"
);
assert_eq!(layout.worktree_of(Path::new(¬_one)), None, "{not_one}");
}
}
let trees = Layout::new(".codix")
.unwrap()
.with_worktrees_dir(".trees")
.unwrap();
assert_eq!(
trees.main_checkout_copies(Path::new("/repo/.wkts/a/.trees/b/x")),
[PathBuf::from("/repo/.wkts/a/x"), PathBuf::from("/repo/x")],
"a worktree made under the default directory is still an alias of the main checkout"
);
assert_eq!(
trees.worktree_of(Path::new("/repo/.wkts/a/x")),
Some(PathBuf::from("/repo/.wkts/a"))
);
assert!(Layout::default()
.main_checkout_copies(Path::new("/repo/.trees/a/x"))
.is_empty());
}
#[test]
fn the_state_roots_entries_are_under_it() {
let root = StateRoot::new("/state");
assert_eq!(root.scene_dir("code"), Path::new("/state/scenes/code"));
assert_eq!(
root.scheduled_tasks_file("code"),
Path::new("/state/scenes/code/scheduled_tasks.json")
);
assert_eq!(
root.groups_file("desktop"),
Path::new("/state/daemon/groups/desktop.json")
);
assert_eq!(root.instances_dir(), Path::new("/state/daemon/instances.d"));
assert_eq!(root.plugin_cache_dir(), Path::new("/state/plugins/cache"));
}
}