|
| 1 | +//! Structured audit events for agent lifecycle tracking. |
| 2 | +//! |
| 3 | +//! Audit events are emitted via `tracing::info!` with target |
| 4 | +//! `terraphim_spawner::audit`, making them easy to filter and forward |
| 5 | +//! to external logging systems. |
| 6 | +
|
| 7 | +use std::fmt; |
| 8 | + |
| 9 | +use terraphim_types::capability::ProcessId; |
| 10 | + |
| 11 | +/// Audit events for agent lifecycle operations. |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub enum AuditEvent { |
| 14 | + /// Agent process was spawned. |
| 15 | + AgentSpawned { |
| 16 | + process_id: ProcessId, |
| 17 | + provider_id: String, |
| 18 | + }, |
| 19 | + /// Agent process was terminated (graceful or forced). |
| 20 | + AgentTerminated { |
| 21 | + process_id: ProcessId, |
| 22 | + graceful: bool, |
| 23 | + }, |
| 24 | + /// Health check failed for an agent. |
| 25 | + HealthCheckFailed { |
| 26 | + process_id: ProcessId, |
| 27 | + reason: String, |
| 28 | + }, |
| 29 | + /// Agent was automatically restarted. |
| 30 | + AgentRestarted { process_id: ProcessId, attempt: u32 }, |
| 31 | + /// Resource limits were applied to a process. |
| 32 | + ResourceLimitApplied { |
| 33 | + process_id: ProcessId, |
| 34 | + limit_type: String, |
| 35 | + value: u64, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +impl fmt::Display for AuditEvent { |
| 40 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | + match self { |
| 42 | + AuditEvent::AgentSpawned { |
| 43 | + process_id, |
| 44 | + provider_id, |
| 45 | + } => { |
| 46 | + write!( |
| 47 | + f, |
| 48 | + "AgentSpawned(process={}, provider={})", |
| 49 | + process_id, provider_id |
| 50 | + ) |
| 51 | + } |
| 52 | + AuditEvent::AgentTerminated { |
| 53 | + process_id, |
| 54 | + graceful, |
| 55 | + } => { |
| 56 | + write!( |
| 57 | + f, |
| 58 | + "AgentTerminated(process={}, graceful={})", |
| 59 | + process_id, graceful |
| 60 | + ) |
| 61 | + } |
| 62 | + AuditEvent::HealthCheckFailed { process_id, reason } => { |
| 63 | + write!( |
| 64 | + f, |
| 65 | + "HealthCheckFailed(process={}, reason={})", |
| 66 | + process_id, reason |
| 67 | + ) |
| 68 | + } |
| 69 | + AuditEvent::AgentRestarted { |
| 70 | + process_id, |
| 71 | + attempt, |
| 72 | + } => { |
| 73 | + write!( |
| 74 | + f, |
| 75 | + "AgentRestarted(process={}, attempt={})", |
| 76 | + process_id, attempt |
| 77 | + ) |
| 78 | + } |
| 79 | + AuditEvent::ResourceLimitApplied { |
| 80 | + process_id, |
| 81 | + limit_type, |
| 82 | + value, |
| 83 | + } => { |
| 84 | + write!( |
| 85 | + f, |
| 86 | + "ResourceLimitApplied(process={}, type={}, value={})", |
| 87 | + process_id, limit_type, value |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_audit_event_display() { |
| 100 | + let pid = ProcessId::new(); |
| 101 | + |
| 102 | + let event = AuditEvent::AgentSpawned { |
| 103 | + process_id: pid, |
| 104 | + provider_id: "@codex".to_string(), |
| 105 | + }; |
| 106 | + let s = format!("{}", event); |
| 107 | + assert!(s.contains("AgentSpawned")); |
| 108 | + assert!(s.contains("@codex")); |
| 109 | + |
| 110 | + let event = AuditEvent::AgentTerminated { |
| 111 | + process_id: pid, |
| 112 | + graceful: true, |
| 113 | + }; |
| 114 | + let s = format!("{}", event); |
| 115 | + assert!(s.contains("AgentTerminated")); |
| 116 | + assert!(s.contains("graceful=true")); |
| 117 | + |
| 118 | + let event = AuditEvent::HealthCheckFailed { |
| 119 | + process_id: pid, |
| 120 | + reason: "timeout".to_string(), |
| 121 | + }; |
| 122 | + let s = format!("{}", event); |
| 123 | + assert!(s.contains("HealthCheckFailed")); |
| 124 | + assert!(s.contains("timeout")); |
| 125 | + |
| 126 | + let event = AuditEvent::AgentRestarted { |
| 127 | + process_id: pid, |
| 128 | + attempt: 2, |
| 129 | + }; |
| 130 | + let s = format!("{}", event); |
| 131 | + assert!(s.contains("AgentRestarted")); |
| 132 | + assert!(s.contains("attempt=2")); |
| 133 | + |
| 134 | + let event = AuditEvent::ResourceLimitApplied { |
| 135 | + process_id: pid, |
| 136 | + limit_type: "RLIMIT_AS".to_string(), |
| 137 | + value: 1073741824, |
| 138 | + }; |
| 139 | + let s = format!("{}", event); |
| 140 | + assert!(s.contains("ResourceLimitApplied")); |
| 141 | + assert!(s.contains("RLIMIT_AS")); |
| 142 | + } |
| 143 | +} |
0 commit comments