intent-engine 0.11.1

A command-line database service for tracking strategic intent, tasks, and events
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
//! Intent-Engine Logging System
//!
//! Provides structured logging with configurable levels and output formats.
//! Uses tracing crate for structured logging with spans and events.

use std::io::{self, IsTerminal};
use tracing::Level;
use tracing_subscriber::{
    fmt::{self, format::FmtSpan},
    layer::SubscriberExt,
    util::SubscriberInitExt,
    EnvFilter, Layer, Registry,
};

/// Logging configuration options
#[derive(Debug, Clone)]
pub struct LoggingConfig {
    /// Minimum log level to output
    pub level: Level,
    /// Enable colored output
    pub color: bool,
    /// Show timestamps
    pub show_timestamps: bool,
    /// Show target/module name
    pub show_target: bool,
    /// Enable JSON format for machine parsing
    pub json_format: bool,
    /// Enable span events for tracing
    pub enable_spans: bool,
    /// Output to file instead of stdout (for daemon mode)
    pub file_output: Option<std::path::PathBuf>,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: Level::INFO,
            color: true,
            show_timestamps: false,
            show_target: false,
            json_format: false,
            enable_spans: false,
            file_output: None,
        }
    }
}

impl LoggingConfig {
    /// Create config for different application modes
    pub fn for_mode(mode: ApplicationMode) -> Self {
        match mode {
            ApplicationMode::McpServer => Self {
                level: Level::DEBUG,
                color: false, // MCP output should be clean
                show_timestamps: true,
                show_target: true,
                json_format: true,   // Machine-readable for MCP
                enable_spans: false, // Avoid noise in JSON-RPC
                file_output: None,
            },
            ApplicationMode::Dashboard => Self {
                level: Level::INFO,
                color: false, // Background service
                show_timestamps: true,
                show_target: true,
                json_format: false,
                enable_spans: true, // Good for debugging dashboard
                file_output: None,
            },
            ApplicationMode::Cli => Self {
                level: Level::INFO,
                color: true,
                show_timestamps: false,
                show_target: false,
                json_format: false,
                enable_spans: false,
                file_output: None,
            },
            ApplicationMode::Test => Self {
                level: Level::DEBUG,
                color: false,
                show_timestamps: true,
                show_target: true,
                json_format: false,
                enable_spans: true,
                file_output: None,
            },
        }
    }

    /// Create config from CLI arguments
    pub fn from_args(quiet: bool, verbose: bool, json: bool) -> Self {
        let level = if verbose {
            Level::DEBUG
        } else if quiet {
            Level::ERROR
        } else {
            Level::INFO
        };

        Self {
            level,
            color: !quiet && !json && std::io::stdout().is_terminal(),
            show_timestamps: verbose || json,
            show_target: verbose,
            json_format: json,
            enable_spans: verbose,
            file_output: None,
        }
    }
}

/// Application modes with different logging requirements
#[derive(Debug, Clone, Copy)]
pub enum ApplicationMode {
    /// MCP server mode - clean, structured output
    McpServer,
    /// Dashboard server mode - detailed for debugging
    Dashboard,
    /// CLI mode - user-friendly output
    Cli,
    /// Test mode - maximum detail for testing
    Test,
}

/// Initialize the logging system
///
/// Note: For production use on Linux/Unix, consider using `logrotate` for log rotation.
/// See `docs/deployment/logrotate.conf` for configuration example.
/// The built-in daily rotation is provided as a fallback for Windows or simple deployments.
pub fn init_logging(config: LoggingConfig) -> io::Result<()> {
    let env_filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(format!("intent_engine={}", config.level)));

    let registry = Registry::default().with(env_filter);

    if let Some(log_file) = config.file_output {
        let log_dir = log_file
            .parent()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid log file path"))?;

        let file_name = log_file
            .file_name()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid log file name"))?;

        // Create log directory if it doesn't exist
        std::fs::create_dir_all(log_dir)?;

        // Use daily rotation (recommended to configure logrotate on Linux)
        let file_appender = tracing_appender::rolling::daily(log_dir, file_name);

        if config.json_format {
            let json_layer = tracing_subscriber::fmt::layer()
                .json()
                .with_current_span(config.enable_spans)
                .with_span_events(FmtSpan::CLOSE)
                .with_writer(file_appender);
            json_layer.with_subscriber(registry).init();
        } else {
            let fmt_layer = fmt::layer()
                .with_target(config.show_target)
                .with_level(true)
                .with_ansi(false)
                .with_writer(file_appender);

            if config.show_timestamps {
                fmt_layer
                    .with_timer(fmt::time::ChronoUtc::rfc_3339())
                    .with_subscriber(registry)
                    .init();
            } else {
                fmt_layer.with_subscriber(registry).init();
            }
        }
    } else if config.json_format {
        let json_layer = tracing_subscriber::fmt::layer()
            .json()
            .with_current_span(config.enable_spans)
            .with_span_events(FmtSpan::CLOSE)
            .with_writer(io::stdout);
        json_layer.with_subscriber(registry).init();
    } else {
        let fmt_layer = fmt::layer()
            .with_target(config.show_target)
            .with_level(true)
            .with_ansi(config.color)
            .with_writer(io::stdout);

        if config.show_timestamps {
            fmt_layer
                .with_timer(fmt::time::ChronoUtc::rfc_3339())
                .with_subscriber(registry)
                .init();
        } else {
            fmt_layer.with_subscriber(registry).init();
        }
    }

    Ok(())
}

/// Clean up old log files based on retention policy
///
/// Scans the log directory and removes files older than the specified retention period.
/// Only removes files matching the pattern `.log.YYYY-MM-DD` (rotated log files).
///
/// # Arguments
/// * `log_dir` - Directory containing log files
/// * `retention_days` - Number of days to retain logs (default: 7)
///
/// # Example
/// ```no_run
/// use std::path::Path;
/// use intent_engine::logging::cleanup_old_logs;
///
/// let log_dir = Path::new("/home/user/.intent-engine/logs");
/// cleanup_old_logs(log_dir, 7).ok();
/// ```
pub fn cleanup_old_logs(log_dir: &std::path::Path, retention_days: u32) -> io::Result<()> {
    use std::fs;
    use std::time::SystemTime;

    if !log_dir.exists() {
        return Ok(()); // Nothing to clean if directory doesn't exist
    }

    let now = SystemTime::now();
    let retention_duration = std::time::Duration::from_secs(retention_days as u64 * 24 * 60 * 60);

    let mut cleaned_count = 0;
    let mut cleaned_size: u64 = 0;

    for entry in fs::read_dir(log_dir)? {
        let entry = entry?;
        let path = entry.path();

        // Only process rotated log files (containing .log. followed by a date)
        // Examples: dashboard.log.2025-11-22, mcp-server.log.2025-11-21
        let path_str = path.to_string_lossy();
        if !path_str.contains(".log.") || !path.is_file() {
            continue;
        }

        let metadata = entry.metadata()?;
        let modified = metadata.modified()?;

        if let Ok(age) = now.duration_since(modified) {
            if age > retention_duration {
                let size = metadata.len();
                match fs::remove_file(&path) {
                    Ok(_) => {
                        cleaned_count += 1;
                        cleaned_size += size;
                        tracing::info!(
                            "Cleaned up old log file: {} (age: {} days, size: {} bytes)",
                            path.display(),
                            age.as_secs() / 86400,
                            size
                        );
                    },
                    Err(e) => {
                        tracing::warn!(path = %path.display(), error = %e, "Failed to remove old log file");
                    },
                }
            }
        }
    }

    if cleaned_count > 0 {
        tracing::info!(
            "Log cleanup completed: removed {} files, freed {} bytes",
            cleaned_count,
            cleaned_size
        );
    }

    Ok(())
}

/// Get log file path for a given application mode
pub fn log_file_path(mode: ApplicationMode) -> std::path::PathBuf {
    let home = dirs::home_dir().expect("Failed to get home directory");
    let log_dir = home.join(".intent-engine").join("logs");

    // Create log directory if it doesn't exist
    std::fs::create_dir_all(&log_dir).ok();

    match mode {
        ApplicationMode::Dashboard => log_dir.join("dashboard.log"),
        ApplicationMode::McpServer => log_dir.join("mcp-server.log"),
        ApplicationMode::Cli => log_dir.join("cli.log"),
        ApplicationMode::Test => log_dir.join("test.log"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::time::SystemTime;
    use tempfile::TempDir;

    // ========== LoggingConfig tests ==========

    #[test]
    fn test_logging_config_default() {
        let config = LoggingConfig::default();

        assert_eq!(config.level, Level::INFO);
        assert!(config.color);
        assert!(!config.show_timestamps);
        assert!(!config.show_target);
        assert!(!config.json_format);
        assert!(!config.enable_spans);
        assert!(config.file_output.is_none());
    }

    #[test]
    fn test_logging_config_for_mode_mcp_server() {
        let config = LoggingConfig::for_mode(ApplicationMode::McpServer);

        assert_eq!(config.level, Level::DEBUG);
        assert!(!config.color); // MCP should be clean
        assert!(config.show_timestamps);
        assert!(config.show_target);
        assert!(config.json_format); // Machine-readable
        assert!(!config.enable_spans); // Avoid noise
        assert!(config.file_output.is_none());
    }

    #[test]
    fn test_logging_config_for_mode_dashboard() {
        let config = LoggingConfig::for_mode(ApplicationMode::Dashboard);

        assert_eq!(config.level, Level::INFO);
        assert!(!config.color); // Background service
        assert!(config.show_timestamps);
        assert!(config.show_target);
        assert!(!config.json_format);
        assert!(config.enable_spans); // Good for debugging
        assert!(config.file_output.is_none());
    }

    #[test]
    fn test_logging_config_for_mode_cli() {
        let config = LoggingConfig::for_mode(ApplicationMode::Cli);

        assert_eq!(config.level, Level::INFO);
        assert!(config.color); // User-friendly
        assert!(!config.show_timestamps);
        assert!(!config.show_target);
        assert!(!config.json_format);
        assert!(!config.enable_spans);
        assert!(config.file_output.is_none());
    }

    #[test]
    fn test_logging_config_for_mode_test() {
        let config = LoggingConfig::for_mode(ApplicationMode::Test);

        assert_eq!(config.level, Level::DEBUG);
        assert!(!config.color);
        assert!(config.show_timestamps);
        assert!(config.show_target);
        assert!(!config.json_format);
        assert!(config.enable_spans); // Maximum detail
        assert!(config.file_output.is_none());
    }

    #[test]
    fn test_logging_config_from_args_verbose() {
        let config = LoggingConfig::from_args(false, true, false);

        assert_eq!(config.level, Level::DEBUG);
        assert!(config.show_timestamps);
        assert!(config.show_target);
        assert!(!config.json_format);
        assert!(config.enable_spans);
    }

    #[test]
    fn test_logging_config_from_args_quiet() {
        let config = LoggingConfig::from_args(true, false, false);

        assert_eq!(config.level, Level::ERROR);
        assert!(!config.color); // Quiet mode disables color
        assert!(!config.show_timestamps); // Quiet mode, no verbose
        assert!(!config.show_target);
    }

    #[test]
    fn test_logging_config_from_args_json() {
        let config = LoggingConfig::from_args(false, false, true);

        assert_eq!(config.level, Level::INFO);
        assert!(!config.color); // JSON disables color
        assert!(config.show_timestamps); // JSON enables timestamps
        assert!(config.json_format);
    }

    #[test]
    fn test_logging_config_from_args_normal() {
        let config = LoggingConfig::from_args(false, false, false);

        assert_eq!(config.level, Level::INFO);
        assert!(!config.show_timestamps);
        assert!(!config.show_target);
        assert!(!config.json_format);
        assert!(!config.enable_spans);
    }

    // ========== log_file_path tests ==========

    #[test]
    fn test_log_file_path_dashboard() {
        let path = log_file_path(ApplicationMode::Dashboard);
        assert!(path.to_string_lossy().ends_with("dashboard.log"));
        assert!(path.to_string_lossy().contains(".intent-engine"));
        assert!(path.to_string_lossy().contains("logs"));
    }

    #[test]
    fn test_log_file_path_mcp_server() {
        let path = log_file_path(ApplicationMode::McpServer);
        assert!(path.to_string_lossy().ends_with("mcp-server.log"));
    }

    #[test]
    fn test_log_file_path_cli() {
        let path = log_file_path(ApplicationMode::Cli);
        assert!(path.to_string_lossy().ends_with("cli.log"));
    }

    #[test]
    fn test_log_file_path_test() {
        let path = log_file_path(ApplicationMode::Test);
        assert!(path.to_string_lossy().ends_with("test.log"));
    }

    // ========== cleanup_old_logs tests ==========

    #[test]
    fn test_cleanup_old_logs_nonexistent_dir() {
        let temp = TempDir::new().unwrap();
        let nonexistent = temp.path().join("nonexistent");

        // Should not error on non-existent directory
        let result = cleanup_old_logs(&nonexistent, 7);
        assert!(result.is_ok());
    }

    #[test]
    fn test_cleanup_old_logs_empty_dir() {
        let temp = TempDir::new().unwrap();

        let result = cleanup_old_logs(temp.path(), 7);
        assert!(result.is_ok());
    }

    #[test]
    fn test_cleanup_old_logs_keeps_current_logs() {
        let temp = TempDir::new().unwrap();

        // Create a current log file (not rotated)
        let current_log = temp.path().join("dashboard.log");
        fs::write(&current_log, "current log data").unwrap();

        // Should not remove current log file (no .log.DATE pattern)
        cleanup_old_logs(temp.path(), 0).unwrap();

        assert!(current_log.exists());
    }

    #[test]
    fn test_cleanup_old_logs_removes_old_rotated_files() {
        let temp = TempDir::new().unwrap();

        // Create an old rotated log file
        let old_log = temp.path().join("dashboard.log.2020-01-01");
        fs::write(&old_log, "old log data").unwrap();

        // Set modification time to 10 days ago
        let ten_days_ago = SystemTime::now()
            .checked_sub(std::time::Duration::from_secs(10 * 24 * 60 * 60))
            .unwrap();
        filetime::set_file_mtime(&old_log, filetime::FileTime::from_system_time(ten_days_ago))
            .unwrap();

        // Clean up logs older than 7 days
        cleanup_old_logs(temp.path(), 7).unwrap();

        // Old file should be removed
        assert!(!old_log.exists());
    }

    #[test]
    fn test_cleanup_old_logs_keeps_recent_rotated_files() {
        let temp = TempDir::new().unwrap();

        // Create a recent rotated log file
        let recent_log = temp.path().join("mcp-server.log.2025-11-25");
        fs::write(&recent_log, "recent log data").unwrap();

        // Set modification time to 3 days ago
        let three_days_ago = SystemTime::now()
            .checked_sub(std::time::Duration::from_secs(3 * 24 * 60 * 60))
            .unwrap();
        filetime::set_file_mtime(
            &recent_log,
            filetime::FileTime::from_system_time(three_days_ago),
        )
        .unwrap();

        // Clean up logs older than 7 days
        cleanup_old_logs(temp.path(), 7).unwrap();

        // Recent file should be kept
        assert!(recent_log.exists());
    }

    #[test]
    fn test_cleanup_old_logs_ignores_non_log_files() {
        let temp = TempDir::new().unwrap();

        // Create a non-log file that's old
        let old_file = temp.path().join("config.json");
        fs::write(&old_file, "{}").unwrap();

        let ten_days_ago = SystemTime::now()
            .checked_sub(std::time::Duration::from_secs(10 * 24 * 60 * 60))
            .unwrap();
        filetime::set_file_mtime(
            &old_file,
            filetime::FileTime::from_system_time(ten_days_ago),
        )
        .unwrap();

        // Should not remove non-log files
        cleanup_old_logs(temp.path(), 7).unwrap();

        assert!(old_file.exists());
    }

    #[test]
    fn test_cleanup_old_logs_ignores_subdirectories() {
        let temp = TempDir::new().unwrap();

        // Create a subdirectory with log-like name
        let subdir = temp.path().join("archive.log.2020-01-01");
        fs::create_dir(&subdir).unwrap();

        // Should not try to remove directories
        let result = cleanup_old_logs(temp.path(), 7);
        assert!(result.is_ok());
        assert!(subdir.exists());
    }
}