agent-team-mail-core 0.44.8

Core library for agent-team-mail: file-based messaging for AI agent teams
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Canonical home directory resolution for ATM
//!
//! Provides a single source of truth for home directory resolution across all ATM crates.
//! This module ensures consistent behavior on all platforms (Linux, macOS, Windows) and
//! supports custom deployments and testing via the `ATM_HOME` environment variable.
//!
//! # Platform Behavior
//!
//! - **Linux/macOS**: `dirs::home_dir()` uses `$HOME` environment variable
//! - **Windows**: `dirs::home_dir()` uses Windows API (`SHGetKnownFolderPath`), which ignores
//!   both `HOME` and `USERPROFILE` environment variables
//!
//! # Precedence
//!
//! 1. `ATM_HOME` environment variable (if set and non-empty)
//! 2. `dirs::home_dir()` platform default
//!
//! # Usage
//!
//! ```
//! use agent_team_mail_core::home::get_home_dir;
//! use std::path::PathBuf;
//!
//! # fn example() -> anyhow::Result<()> {
//! let home = get_home_dir()?;
//! let config_dir = home.join(".config/atm");
//! # Ok(())
//! # }
//! # example().unwrap();
//! ```
//!
//! # Testing
//!
//! Integration tests MUST use `ATM_HOME` to override the home directory:
//!
//! ```ignore
//! use assert_cmd::Command;
//! use tempfile::TempDir;
//!
//! let temp_dir = TempDir::new().unwrap();
//! let mut cmd = Command::cargo_bin("atm").unwrap();
//! cmd.env("ATM_HOME", temp_dir.path());
//! ```

use anyhow::{Context, Result};
use std::path::{Path, PathBuf};

/// Get the home directory for ATM operations
///
/// This is the canonical home directory resolution function used by all ATM crates.
///
/// # Precedence
///
/// 1. `ATM_HOME` environment variable (if set and non-empty)
/// 2. `dirs::home_dir()` platform default
///
/// # Returns
///
/// Returns the home directory as a `PathBuf`. Trailing slashes are normalized.
///
/// # Errors
///
/// Returns an error if:
/// - `ATM_HOME` is not set AND
/// - Platform home directory cannot be determined via `dirs::home_dir()`
///
/// # Examples
///
/// ```
/// use agent_team_mail_core::home::get_home_dir;
///
/// # fn example() -> anyhow::Result<()> {
/// // Use platform default
/// let home = get_home_dir()?;
/// println!("Home: {}", home.display());
///
/// // Override with ATM_HOME (requires unsafe)
/// unsafe { std::env::set_var("ATM_HOME", "/custom/home") };
/// let custom_home = get_home_dir()?;
/// assert_eq!(custom_home.to_str().unwrap(), "/custom/home");
/// unsafe { std::env::remove_var("ATM_HOME") };
/// # Ok(())
/// # }
/// # example().unwrap();
/// ```
pub fn get_home_dir() -> Result<PathBuf> {
    // Check ATM_HOME first (useful for testing and custom deployments)
    if let Ok(home) = std::env::var("ATM_HOME") {
        let trimmed = home.trim();
        if !trimmed.is_empty() {
            // Normalize trailing slashes
            let path = PathBuf::from(trimmed);
            return Ok(path);
        }
    }

    // Fall back to platform default
    dirs::home_dir().context("Could not determine home directory")
}

/// Get the OS-level home directory, always bypassing `ATM_HOME`.
///
/// Unlike [`get_home_dir`], this function ignores the `ATM_HOME` environment
/// variable and returns the platform default home directory directly.
///
/// This is intentionally distinct from [`get_home_dir`] and is reserved for
/// locations that must be stable regardless of `ATM_HOME` — specifically the
/// daemon socket pointer file at `~/.config/atm/daemon-socket.path`, which
/// needs to be reachable by any CLI invocation even when `ATM_HOME` points to
/// a different directory.
///
/// # Errors
///
/// Returns an error if the platform cannot determine a home directory.
pub fn get_os_home_dir() -> Result<PathBuf> {
    dirs::home_dir().context("Could not determine OS home directory")
}

/// Return the canonical `{ATM_HOME}/.claude` root for ATM-managed state.
pub fn claude_root_dir() -> Result<PathBuf> {
    Ok(claude_root_dir_for(&get_home_dir()?))
}

/// Return the canonical `{ATM_HOME}/.claude` root for the provided home path.
pub fn claude_root_dir_for(home: &Path) -> PathBuf {
    home.join(".claude")
}

/// Return the canonical `{ATM_HOME}/.claude/teams` root for ATM team state.
pub fn teams_root_dir() -> Result<PathBuf> {
    Ok(teams_root_dir_for(&get_home_dir()?))
}

/// Return the canonical `{ATM_HOME}/.claude/teams` root for the provided home path.
pub fn teams_root_dir_for(home: &Path) -> PathBuf {
    claude_root_dir_for(home).join("teams")
}

/// Return the canonical team directory for the provided home and team name.
pub fn team_dir_for(home: &Path, team: &str) -> PathBuf {
    teams_root_dir_for(home).join(team)
}

/// Return the canonical team config path for the provided home and team name.
pub fn team_config_path_for(home: &Path, team: &str) -> PathBuf {
    team_dir_for(home, team).join("config.json")
}

/// Return the canonical inbox JSON path for the provided home, team, and agent.
pub fn inbox_path_for(home: &Path, team: &str, agent: &str) -> PathBuf {
    team_dir_for(home, team)
        .join("inboxes")
        .join(format!("{agent}.json"))
}

/// Return the canonical Claude settings path for the provided home.
pub fn claude_settings_path_for(home: &Path) -> PathBuf {
    claude_root_dir_for(home).join("settings.json")
}

/// Return the canonical Claude scripts directory for the provided home.
pub fn claude_scripts_dir_for(home: &Path) -> PathBuf {
    claude_root_dir_for(home).join("scripts")
}

/// Return the canonical Claude agents directory for the provided home.
pub fn claude_agents_dir_for(home: &Path) -> PathBuf {
    claude_root_dir_for(home).join("agents")
}

/// Return the canonical `{ATM_HOME}/.config/atm` directory for the provided home.
pub fn atm_config_dir_for(home: &Path) -> PathBuf {
    home.join(".config").join("atm")
}

/// Return the canonical agent session directory for the provided home.
pub fn sessions_dir_for(home: &Path) -> PathBuf {
    atm_config_dir_for(home).join("agent-sessions")
}
#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::env;

    #[test]
    #[serial]
    fn test_atm_home_set() {
        // Save and set
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "/custom/home") };

        let home = get_home_dir().unwrap();
        assert_eq!(home, PathBuf::from("/custom/home"));

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_not_set_uses_platform_default() {
        // Save and remove
        let original = env::var("ATM_HOME").ok();
        unsafe { env::remove_var("ATM_HOME") };

        let home = get_home_dir().unwrap();
        // Should match platform default
        assert_eq!(home, dirs::home_dir().unwrap());

        // Restore
        unsafe {
            if let Some(v) = original {
                env::set_var("ATM_HOME", v);
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_empty_string_uses_platform_default() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "") };

        let home = get_home_dir().unwrap();
        // Empty string should fall back to platform default
        assert_eq!(home, dirs::home_dir().unwrap());

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_whitespace_only_uses_platform_default() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "   ") };

        let home = get_home_dir().unwrap();
        // Whitespace-only should fall back to platform default
        assert_eq!(home, dirs::home_dir().unwrap());

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_with_trailing_slash() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "/custom/home/") };

        let home = get_home_dir().unwrap();
        // PathBuf normalizes trailing slashes automatically
        let expected = PathBuf::from("/custom/home/");
        assert_eq!(home, expected);

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_with_spaces_in_path() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "/path with spaces/home") };

        let home = get_home_dir().unwrap();
        assert_eq!(home, PathBuf::from("/path with spaces/home"));

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_relative_path() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "relative/path") };

        let home = get_home_dir().unwrap();
        assert_eq!(home, PathBuf::from("relative/path"));

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_atm_home_with_leading_trailing_whitespace() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "  /custom/home  ") };

        let home = get_home_dir().unwrap();
        // Should trim whitespace
        assert_eq!(home, PathBuf::from("/custom/home"));

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    #[serial]
    fn test_multiple_calls_consistent() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "/test/home") };

        let home1 = get_home_dir().unwrap();
        let home2 = get_home_dir().unwrap();
        assert_eq!(home1, home2);

        // Restore
        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }

    #[test]
    fn test_path_helpers_build_canonical_paths() {
        let home = PathBuf::from("test-home");

        assert_eq!(claude_root_dir_for(&home), home.join(".claude"));
        assert_eq!(
            teams_root_dir_for(&home),
            home.join(".claude").join("teams")
        );
        assert_eq!(
            team_dir_for(&home, "atm-dev"),
            home.join(".claude").join("teams").join("atm-dev")
        );
        assert_eq!(
            team_config_path_for(&home, "atm-dev"),
            home.join(".claude")
                .join("teams")
                .join("atm-dev")
                .join("config.json")
        );
        assert_eq!(
            inbox_path_for(&home, "atm-dev", "arch-ctm"),
            home.join(".claude")
                .join("teams")
                .join("atm-dev")
                .join("inboxes")
                .join("arch-ctm.json")
        );
        assert_eq!(
            claude_settings_path_for(&home),
            home.join(".claude").join("settings.json")
        );
        assert_eq!(
            claude_scripts_dir_for(&home),
            home.join(".claude").join("scripts")
        );
        assert_eq!(
            claude_agents_dir_for(&home),
            home.join(".claude").join("agents")
        );
        assert_eq!(atm_config_dir_for(&home), home.join(".config").join("atm"));
        assert_eq!(
            sessions_dir_for(&home),
            home.join(".config").join("atm").join("agent-sessions")
        );
    }

    #[test]
    #[serial]
    fn test_root_dir_helpers_respect_atm_home() {
        let original = env::var("ATM_HOME").ok();
        unsafe { env::set_var("ATM_HOME", "test-home") };

        assert_eq!(
            claude_root_dir().unwrap(),
            PathBuf::from("test-home/.claude")
        );
        assert_eq!(
            teams_root_dir().unwrap(),
            PathBuf::from("test-home/.claude/teams")
        );

        unsafe {
            match original {
                Some(v) => env::set_var("ATM_HOME", v),
                None => env::remove_var("ATM_HOME"),
            }
        }
    }
}