agent-team-mail-core 0.9.0

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Inbox retention policy implementation
//!
//! Provides configurable retention policies to prevent unbounded inbox growth.
//! Supports age-based and count-based policies with archive or delete strategies.

use crate::config::{CleanupStrategy, RetentionConfig};
use crate::io::inbox::inbox_update;
use crate::schema::InboxMessage;
use anyhow::{Context, Result};
use chrono::{DateTime, Duration, Utc};
use std::fs;
use std::path::{Path, PathBuf};

/// Result of applying retention policy to an inbox
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetentionResult {
    /// Number of messages kept in inbox
    pub kept: usize,
    /// Number of messages removed from inbox
    pub removed: usize,
    /// Number of messages archived (subset of removed)
    pub archived: usize,
}

impl RetentionResult {
    /// Create a new retention result
    pub fn new(kept: usize, removed: usize, archived: usize) -> Self {
        Self {
            kept,
            removed,
            archived,
        }
    }
}

/// Apply retention policy to an inbox
///
/// Reads the inbox file, determines which messages should be removed based on
/// the configured policy (max age and/or max count), and either deletes them
/// or archives them based on the cleanup strategy.
///
/// # Arguments
///
/// * `inbox_path` - Path to the inbox.json file
/// * `team` - Team name (used for archive directory structure)
/// * `agent` - Agent name (used for archive directory structure)
/// * `policy` - Retention configuration to apply
/// * `dry_run` - If true, return what would be done without modifying files
///
/// # Returns
///
/// Returns `RetentionResult` with counts of kept, removed, and archived messages.
///
/// # Errors
///
/// Returns error if file operations fail or if duration parsing fails.
pub fn apply_retention(
    inbox_path: &Path,
    team: &str,
    agent: &str,
    policy: &RetentionConfig,
    dry_run: bool,
) -> Result<RetentionResult> {
    // If inbox doesn't exist, nothing to do
    if !inbox_path.exists() {
        return Ok(RetentionResult::new(0, 0, 0));
    }

    // Read current inbox
    let content = fs::read_to_string(inbox_path)
        .with_context(|| format!("Failed to read inbox at {}", inbox_path.display()))?;
    let messages: Vec<InboxMessage> = serde_json::from_str(&content)
        .with_context(|| format!("Failed to parse inbox at {}", inbox_path.display()))?;

    // If no retention policy configured, keep all messages
    if policy.max_age.is_none() && policy.max_count.is_none() {
        return Ok(RetentionResult::new(messages.len(), 0, 0));
    }

    let now = Utc::now();
    let max_age_duration = if let Some(ref age_str) = policy.max_age {
        Some(parse_duration(age_str)?)
    } else {
        None
    };

    // Determine which messages to keep
    let mut to_keep = Vec::new();
    let mut to_remove = Vec::new();

    for message in messages {
        let should_remove = should_remove_message(&message, &max_age_duration, now, policy.max_count, to_keep.len());

        if should_remove {
            to_remove.push(message);
        } else {
            to_keep.push(message);
        }
    }

    // If nothing to remove, we're done
    if to_remove.is_empty() {
        return Ok(RetentionResult::new(to_keep.len(), 0, 0));
    }

    // In dry-run mode, just return the counts
    if dry_run {
        let archived = if policy.strategy == CleanupStrategy::Archive {
            to_remove.len()
        } else {
            0
        };
        return Ok(RetentionResult::new(to_keep.len(), to_remove.len(), archived));
    }

    // Archive messages if configured
    let archived = if policy.strategy == CleanupStrategy::Archive {
        let archive_dir = determine_archive_dir(policy)?;
        archive_messages(&to_remove, team, agent, &archive_dir)?;
        to_remove.len()
    } else {
        0
    };

    // Update inbox with only the messages to keep
    inbox_update(inbox_path, team, agent, |messages| {
        messages.clear();
        messages.extend(to_keep.clone());
    })?;

    Ok(RetentionResult::new(to_keep.len(), to_remove.len(), archived))
}

/// Determine if a message should be removed based on retention policy
fn should_remove_message(
    message: &InboxMessage,
    max_age_duration: &Option<Duration>,
    now: DateTime<Utc>,
    max_count: Option<usize>,
    current_kept_count: usize,
) -> bool {
    // Check age-based policy
    if let Some(max_age) = max_age_duration
        && is_expired_by_age(message, max_age, now)
    {
        return true;
    }

    // Check count-based policy
    // We keep the newest messages up to max_count
    // Since messages are processed in order, once we've kept max_count messages,
    // all subsequent messages should be removed
    if let Some(max_count) = max_count
        && current_kept_count >= max_count
    {
        return true;
    }

    false
}

/// Check if a message exceeds the maximum age policy
fn is_expired_by_age(message: &InboxMessage, max_age: &Duration, now: DateTime<Utc>) -> bool {
    // Parse message timestamp
    if let Ok(msg_time) = DateTime::parse_from_rfc3339(&message.timestamp) {
        let msg_time_utc = msg_time.with_timezone(&Utc);
        let age = now.signed_duration_since(msg_time_utc);
        age > *max_age
    } else {
        // If we can't parse the timestamp, treat as expired (safer default)
        true
    }
}

/// Parse duration string into chrono::Duration
///
/// Supports formats like:
/// - "7d" -> 7 days
/// - "24h" -> 24 hours
/// - "30d" -> 30 days
/// - "168h" -> 168 hours (7 days)
pub fn parse_duration(s: &str) -> Result<Duration> {
    let s = s.trim();
    if s.is_empty() {
        anyhow::bail!("Empty duration string");
    }

    // Extract number and unit
    let (num_part, unit) = match s.find(|c: char| !c.is_ascii_digit()) {
        Some(idx) => (&s[..idx], &s[idx..]),
        None => anyhow::bail!("Duration must have a unit (h or d): {s}"),
    };

    let num: i64 = num_part.parse()
        .with_context(|| format!("Invalid number in duration: {s}"))?;

    match unit {
        "h" => Ok(Duration::hours(num)),
        "d" => Ok(Duration::days(num)),
        _ => anyhow::bail!("Unknown duration unit '{unit}'. Use 'h' for hours or 'd' for days"),
    }
}

/// Determine the archive directory from config or use default
fn determine_archive_dir(policy: &RetentionConfig) -> Result<PathBuf> {
    if let Some(ref dir_str) = policy.archive_dir {
        Ok(PathBuf::from(dir_str))
    } else {
        // Default: ~/.config/atm/archive/
        // When ATM_HOME is set, use it directly (test-friendly)
        if let Ok(atm_home) = std::env::var("ATM_HOME") {
            return Ok(PathBuf::from(atm_home).join("archive"));
        }
        let home = crate::home::get_home_dir()?;
        Ok(home.join(".config/atm/archive"))
    }
}

/// Archive messages to a timestamped archive file
///
/// Messages are archived to: `{archive_dir}/{team}/{agent}/archive-{timestamp}.json`
fn archive_messages(
    messages: &[InboxMessage],
    team: &str,
    agent: &str,
    archive_dir: &Path,
) -> Result<()> {
    // Create archive directory structure
    let team_agent_dir = archive_dir.join(team).join(agent);
    fs::create_dir_all(&team_agent_dir)
        .with_context(|| format!("Failed to create archive directory: {}", team_agent_dir.display()))?;

    // Create timestamped archive file
    let timestamp = Utc::now().format("%Y%m%d-%H%M%S");
    let archive_file = team_agent_dir.join(format!("archive-{timestamp}.json"));

    // Write messages to archive file
    let json = serde_json::to_string_pretty(messages)
        .context("Failed to serialize messages for archiving")?;
    fs::write(&archive_file, json)
        .with_context(|| format!("Failed to write archive file: {}", archive_file.display()))?;

    Ok(())
}

/// Result of cleaning report files
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CleanReportResult {
    /// Number of files deleted
    pub deleted_count: usize,
    /// Number of files skipped (not expired)
    pub skipped_count: usize,
}

impl CleanReportResult {
    /// Create a new clean report result
    pub fn new(deleted_count: usize, skipped_count: usize) -> Self {
        Self {
            deleted_count,
            skipped_count,
        }
    }
}

/// Clean old report files from the CI monitor report directory
///
/// Scans `report_dir` for `*.json` and `*.md` files older than `max_age`
/// and deletes them.
///
/// # Arguments
///
/// * `report_dir` - Directory containing report files
/// * `max_age` - Maximum age for report files (files older than this are deleted)
///
/// # Returns
///
/// Returns `CleanReportResult` with counts of deleted and skipped files.
///
/// # Errors
///
/// Returns error if directory operations fail or if file metadata cannot be read.
pub fn clean_report_files(report_dir: &Path, max_age: &Duration) -> Result<CleanReportResult> {
    // If directory doesn't exist, nothing to do
    if !report_dir.exists() {
        return Ok(CleanReportResult::new(0, 0));
    }

    let now = Utc::now();
    let mut deleted = 0;
    let mut skipped = 0;

    // Read directory entries
    let entries = fs::read_dir(report_dir)
        .with_context(|| format!("Failed to read report directory: {}", report_dir.display()))?;

    for entry in entries {
        let entry = entry.context("Failed to read directory entry")?;
        let path = entry.path();

        // Only process .json and .md files
        if let Some(ext) = path.extension() {
            let ext_str = ext.to_string_lossy();
            if ext_str != "json" && ext_str != "md" {
                continue;
            }
        } else {
            // No extension, skip
            continue;
        }

        // Get file metadata to check modification time
        let metadata = match fs::metadata(&path) {
            Ok(m) => m,
            Err(e) => {
                // File might have been deleted concurrently, just warn and continue
                tracing::warn!("Failed to get metadata for {}: {}", path.display(), e);
                continue;
            }
        };

        // Check if file is old enough to delete
        let modified = metadata
            .modified()
            .context("Failed to get file modification time")?;

        let modified_datetime: DateTime<Utc> = modified.into();
        let age = now.signed_duration_since(modified_datetime);

        if age > *max_age {
            // Delete the file
            match fs::remove_file(&path) {
                Ok(()) => {
                    deleted += 1;
                    tracing::debug!("Deleted old report file: {}", path.display());
                }
                Err(e) => {
                    // Log but don't fail the entire operation
                    tracing::warn!("Failed to delete report file {}: {}", path.display(), e);
                }
            }
        } else {
            skipped += 1;
        }
    }

    Ok(CleanReportResult::new(deleted, skipped))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_duration_hours() {
        let duration = parse_duration("24h").unwrap();
        assert_eq!(duration, Duration::hours(24));
    }

    #[test]
    fn test_parse_duration_days() {
        let duration = parse_duration("7d").unwrap();
        assert_eq!(duration, Duration::days(7));
    }

    #[test]
    fn test_parse_duration_large_values() {
        let duration = parse_duration("168h").unwrap();
        assert_eq!(duration, Duration::hours(168));

        let duration = parse_duration("30d").unwrap();
        assert_eq!(duration, Duration::days(30));
    }

    #[test]
    fn test_parse_duration_invalid() {
        assert!(parse_duration("").is_err());
        assert!(parse_duration("24").is_err());
        assert!(parse_duration("24m").is_err());
        assert!(parse_duration("abc").is_err());
    }

    #[test]
    fn test_is_expired_by_age() {
        let now = Utc::now();
        let max_age = Duration::days(7);

        // Message from 10 days ago (expired)
        let old_message = InboxMessage {
            from: "test".to_string(),
            text: "old message".to_string(),
            timestamp: (now - Duration::days(10)).to_rfc3339(),
            read: false,
            summary: None,
            message_id: None,
            unknown_fields: std::collections::HashMap::new(),
        };
        assert!(is_expired_by_age(&old_message, &max_age, now));

        // Message from 3 days ago (not expired)
        let recent_message = InboxMessage {
            from: "test".to_string(),
            text: "recent message".to_string(),
            timestamp: (now - Duration::days(3)).to_rfc3339(),
            read: false,
            summary: None,
            message_id: None,
            unknown_fields: std::collections::HashMap::new(),
        };
        assert!(!is_expired_by_age(&recent_message, &max_age, now));
    }

    #[test]
    fn test_retention_result() {
        let result = RetentionResult::new(10, 5, 5);
        assert_eq!(result.kept, 10);
        assert_eq!(result.removed, 5);
        assert_eq!(result.archived, 5);
    }

    #[test]
    fn test_clean_report_files_deletes_old_files() {
        use tempfile::TempDir;
        use std::fs::File;
        use std::io::Write;

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("reports");
        fs::create_dir_all(&report_dir).unwrap();

        // Create old report files
        let old_json = report_dir.join("old.json");
        let old_md = report_dir.join("old.md");
        File::create(&old_json).unwrap().write_all(b"{}").unwrap();
        File::create(&old_md).unwrap().write_all(b"# Report").unwrap();

        // Wait a bit to ensure files have a measurable age
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Clean files older than 50ms (both should be deleted)
        let max_age = Duration::milliseconds(50);
        let result = super::clean_report_files(&report_dir, &max_age).unwrap();

        assert_eq!(result.deleted_count, 2);
        assert_eq!(result.skipped_count, 0);
        assert!(!old_json.exists());
        assert!(!old_md.exists());
    }

    #[test]
    fn test_clean_report_files_skips_recent_files() {
        use tempfile::TempDir;
        use std::fs::File;
        use std::io::Write;

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("reports");
        fs::create_dir_all(&report_dir).unwrap();

        // Create recent report files
        let recent_json = report_dir.join("recent.json");
        let recent_md = report_dir.join("recent.md");
        File::create(&recent_json).unwrap().write_all(b"{}").unwrap();
        File::create(&recent_md).unwrap().write_all(b"# Report").unwrap();

        // Clean files older than 1 hour (both should be skipped)
        let max_age = Duration::hours(1);
        let result = super::clean_report_files(&report_dir, &max_age).unwrap();

        assert_eq!(result.deleted_count, 0);
        assert_eq!(result.skipped_count, 2);
        assert!(recent_json.exists());
        assert!(recent_md.exists());
    }

    #[test]
    fn test_clean_report_files_empty_directory() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("reports");
        fs::create_dir_all(&report_dir).unwrap();

        let max_age = Duration::hours(1);
        let result = super::clean_report_files(&report_dir, &max_age).unwrap();

        assert_eq!(result.deleted_count, 0);
        assert_eq!(result.skipped_count, 0);
    }

    #[test]
    fn test_clean_report_files_nonexistent_directory() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("nonexistent");

        let max_age = Duration::hours(1);
        let result = super::clean_report_files(&report_dir, &max_age).unwrap();

        assert_eq!(result.deleted_count, 0);
        assert_eq!(result.skipped_count, 0);
    }

    #[test]
    fn test_clean_report_files_only_targets_json_and_md() {
        use tempfile::TempDir;
        use std::fs::File;
        use std::io::Write;

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("reports");
        fs::create_dir_all(&report_dir).unwrap();

        // Create various file types
        let json_file = report_dir.join("report.json");
        let md_file = report_dir.join("report.md");
        let txt_file = report_dir.join("report.txt");
        let log_file = report_dir.join("report.log");

        File::create(&json_file).unwrap().write_all(b"{}").unwrap();
        File::create(&md_file).unwrap().write_all(b"# Report").unwrap();
        File::create(&txt_file).unwrap().write_all(b"text").unwrap();
        File::create(&log_file).unwrap().write_all(b"log").unwrap();

        // Wait for files to age
        std::thread::sleep(std::time::Duration::from_millis(100));

        // Clean files older than 50ms
        let max_age = Duration::milliseconds(50);
        let result = super::clean_report_files(&report_dir, &max_age).unwrap();

        // Only json and md should be deleted
        assert_eq!(result.deleted_count, 2);
        assert!(!json_file.exists());
        assert!(!md_file.exists());
        assert!(txt_file.exists()); // Should still exist
        assert!(log_file.exists()); // Should still exist
    }

    #[test]
    fn test_clean_report_result() {
        let result = CleanReportResult::new(5, 10);
        assert_eq!(result.deleted_count, 5);
        assert_eq!(result.skipped_count, 10);
    }

    #[test]
    fn test_parse_inbox_filename_simple() {
        // Simple agent name: "agent.json"
        let filename = "agent.json";
        let base = &filename[..filename.len() - 5];
        assert_eq!(base, "agent");
    }

    #[test]
    fn test_parse_inbox_filename_with_hostname() {
        // Bridge inbox: "agent.hostname.json"
        let filename = "agent.hostname.json";
        let base = &filename[..filename.len() - 5];
        assert_eq!(base, "agent.hostname");
        // Retention uses the entire base as agent name
    }

    #[test]
    fn test_parse_inbox_filename_dotted_agent() {
        // Agent name with dots: "dotted.agent.name.json"
        let filename = "dotted.agent.name.json";
        let base = &filename[..filename.len() - 5];
        assert_eq!(base, "dotted.agent.name");
        // Retention uses the entire base as agent name
    }

    #[test]
    fn test_parse_inbox_filename_dotted_agent_with_hostname() {
        // Bridge inbox with dotted agent: "dotted.agent.hostname.json"
        let filename = "dotted.agent.hostname.json";
        let base = &filename[..filename.len() - 5];
        assert_eq!(base, "dotted.agent.hostname");
        // Retention uses the entire base as agent name
    }

    #[test]
    fn test_retention_only_scans_inboxes_subdirectory() {
        use tempfile::TempDir;
        use std::fs::File;
        use std::io::Write;

        let temp_dir = TempDir::new().unwrap();
        let team_dir = temp_dir.path().join("test-team");
        let inboxes_dir = team_dir.join("inboxes");
        fs::create_dir_all(&inboxes_dir).unwrap();

        // Create inbox file in inboxes/ subdirectory
        let inbox_path = inboxes_dir.join("agent.json");
        let old_timestamp = (Utc::now() - Duration::days(10)).to_rfc3339();
        let inbox_data = serde_json::json!([
            {
                "from": "test",
                "text": "old message",
                "timestamp": old_timestamp,
                "read": false
            }
        ]);
        File::create(&inbox_path)
            .unwrap()
            .write_all(serde_json::to_string(&inbox_data).unwrap().as_bytes())
            .unwrap();

        // Create a file at team level (should NOT be processed)
        let config_path = team_dir.join("config.json");
        File::create(&config_path)
            .unwrap()
            .write_all(b"{\"name\":\"test-team\"}")
            .unwrap();

        // Apply retention with max age of 7 days (should remove the old message)
        let policy = RetentionConfig {
            max_age: Some("7d".to_string()),
            max_count: None,
            strategy: CleanupStrategy::Delete,
            archive_dir: None,
            enabled: true,
            interval_secs: 300,
        };

        let result = apply_retention(&inbox_path, "test-team", "agent", &policy, false).unwrap();
        assert_eq!(result.kept, 0);
        assert_eq!(result.removed, 1);

        // Verify config.json at team level is untouched
        assert!(config_path.exists());
        let config_content = fs::read_to_string(&config_path).unwrap();
        assert_eq!(config_content, "{\"name\":\"test-team\"}");
    }

    #[test]
    fn test_report_dir_lookup_resolves_ci_monitor_key() {
        use tempfile::TempDir;

        // This test verifies that the report_dir from ci_monitor config
        // is correctly used when passed to clean_report_files.
        // The key resolution happens in the daemon config parsing,
        // but this test documents the expected behavior.

        let temp_dir = TempDir::new().unwrap();
        let report_dir = temp_dir.path().join("ci-reports");
        fs::create_dir_all(&report_dir).unwrap();

        // Create a report file
        let report_path = report_dir.join("report.json");
        std::fs::File::create(&report_path).unwrap();

        // Clean with long max_age (file should be skipped)
        let max_age = Duration::hours(24);
        let result = clean_report_files(&report_dir, &max_age).unwrap();

        assert_eq!(result.deleted_count, 0);
        assert_eq!(result.skipped_count, 1);

        // Note: The actual config key resolution (ci_monitor vs ci-monitor)
        // is handled by TOML parsing in the daemon's config module,
        // which uses underscores as the canonical form.
    }
}