runtimo-core 0.5.1

Agent-centric capability runtime with telemetry, process tracking, and crash recovery
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
//! Process Execution Awareness — What's running and consuming resources.
//!
//! Tracks processes, resource consumption, and execution context.
//! Captures a snapshot via `ps` with explicit format, computes summaries
//! (total CPU%, memory%, zombie count), and identifies top consumers.
//!
//! # Example
//!
//! ```rust,ignore
//! use runtimo_core::ProcessSnapshot;
//!
//! let snap = ProcessSnapshot::capture();
//! println!("Processes: {}", snap.summary.total_processes);
//! println!("Zombies: {}", snap.summary.zombie_count);
//!
//! for proc in snap.top_by_cpu(5) {
//!     println!("{}: {:.1}% CPU", proc.command, proc.cpu_percent);
//! }
//! ```

use crate::cmd::run_cmd;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;

static PROCESS_CACHE: Mutex<Option<(ProcessSnapshot, std::time::Instant)>> = Mutex::new(None);
const CACHE_TTL_SECS: u64 = 30;

/// Process list snapshot at a point in time.
///
/// Contains the raw process list, a computed summary, and a timestamp.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct ProcessSnapshot {
    /// Unix timestamp (seconds) when the snapshot was taken.
    pub timestamp: u64,
    /// Individual process records parsed from `ps -eo`.
    pub processes: Vec<ProcessInfo>,
    /// Aggregated summary statistics.
    pub summary: ProcessSummary,
}

/// Information about a single running process.
///
/// Parsed from one line of `ps -eo` output.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct ProcessInfo {
    /// Process ID.
    pub pid: u32,
    /// Parent Process ID (PPID) for lineage tracking.
    pub ppid: u32,
    /// Owning user name.
    pub user: String,
    /// CPU usage percentage.
    pub cpu_percent: f32,
    /// Memory usage percentage.
    pub mem_percent: f32,
    /// Virtual memory size in kilobytes (KB).
    pub vsz: u64,
    /// Resident set size in kilobytes (KB).
    pub rss: u64,
    /// Process state string (e.g. `"S"`, `"R"`, `"Z"`).
    pub stat: String,
    /// Start time of the process.
    pub start_time: String,
    /// Elapsed running time.
    pub elapsed: String,
    /// Full command line.
    pub command: String,
}

/// Aggregated summary of a process snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::exhaustive_structs)]
pub struct ProcessSummary {
    /// Total number of processes.
    pub total_processes: usize,
    /// Sum of all process CPU percentages.
    pub total_cpu_percent: f32,
    /// Sum of all process memory percentages.
    pub total_mem_percent: f32,
    /// Command name of the top CPU consumer.
    pub top_cpu_consumer: Option<String>,
    /// Command name of the top memory consumer.
    pub top_mem_consumer: Option<String>,
    /// Number of zombie (`Z` state) processes.
    pub zombie_count: usize,
}

impl ProcessSnapshot {
    /// Captures a full process snapshot via `ps` with explicit format.
    ///
    /// Results are cached for 30 seconds to avoid re-parsing on
    /// repeated calls within the same execution window.
    pub fn capture() -> Self {
        let now = std::time::Instant::now();
        {
            // Handle poison error by recovering from the poisoned state
            let cache = PROCESS_CACHE.lock().unwrap_or_else(|e| e.into_inner());
            if let Some((cached, instant)) = cache.as_ref() {
                if now.duration_since(*instant).as_secs() < CACHE_TTL_SECS {
                    return cached.clone();
                }
            }
        }

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_secs());

        let mut processes = Vec::new();
        // Use ps with explicit format to get PPID: PID,PPID,USER,CPU,MEM,VSZ,RSS,STAT,START,TIME,COMMAND
        // This gives us parent process ID for lineage tracking
        let ps_output =
            run_cmd("ps -eo pid,ppid,user,%cpu,%mem,vsz,rss,stat,start,time,comm --no-headers");

        for line in ps_output.lines() {
            if let Some(proc) = parse_ps_line(line) {
                processes.push(proc);
            }
        }

        let summary = ProcessSummary::compute(&processes);

        let snapshot = Self {
            timestamp,
            processes,
            summary,
        };

        // Handle poison error by recovering from the poisoned state
        let mut cache = PROCESS_CACHE.lock().unwrap_or_else(|e| e.into_inner());
        *cache = Some((snapshot.clone(), now));
        snapshot
    }

    /// Returns all zombie processes with their PID, command, and PPID.
    ///
    /// Zombies are defunct child processes whose parent has not yet called
    /// `waitpid(2)`. They consume no resources but each occupies a PID slot.
    #[must_use]
    pub fn zombies(&self) -> Vec<&ProcessInfo> {
        self.processes
            .iter()
            .filter(|p| p.stat.starts_with('Z'))
            .collect()
    }

    /// Clears the process snapshot cache.
    ///
    /// Use before capturing an after-kill snapshot to ensure fresh data.
    pub fn clear_cache() {
        let mut cache = PROCESS_CACHE.lock().unwrap_or_else(|e| e.into_inner());
        *cache = None;
    }

    /// Returns the top `n` processes by CPU usage.
    #[must_use]
    pub fn top_by_cpu(&self, n: usize) -> Vec<&ProcessInfo> {
        let mut procs: Vec<_> = self.processes.iter().collect();
        procs.sort_by(|a, b| {
            b.cpu_percent
                .partial_cmp(&a.cpu_percent)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        procs.into_iter().take(n).collect()
    }

    /// Returns the top `n` processes by memory usage.
    #[must_use]
    pub fn top_by_mem(&self, n: usize) -> Vec<&ProcessInfo> {
        let mut procs: Vec<_> = self.processes.iter().collect();
        procs.sort_by(|a, b| {
            b.mem_percent
                .partial_cmp(&a.mem_percent)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        procs.into_iter().take(n).collect()
    }

    /// Prints a human-readable process report to stdout.
    #[allow(clippy::arithmetic_side_effects)] // i+1 for human-readable display numbering
    pub fn print_report(&self) {
        println!("\n{}", "=".repeat(80));
        println!(" PROCESS SNAPSHOT [{}]", self.timestamp);
        println!("{}", "=".repeat(80));

        println!("\n--- SUMMARY ---");
        println!(" Total Processes: {}", self.summary.total_processes);
        println!(" Total CPU: {:.1}%", self.summary.total_cpu_percent);
        println!(" Total Memory: {:.1}%", self.summary.total_mem_percent);
        println!(" Zombies: {}", self.summary.zombie_count);

        if let Some(ref top_cpu) = self.summary.top_cpu_consumer {
            println!(
                " Top CPU: {} ({:.1}%)",
                top_cpu,
                self.processes
                    .iter()
                    .find(|p| p.command == *top_cpu)
                    .map_or(0.0, |p| p.cpu_percent)
            );
        }

        if let Some(ref top_mem) = self.summary.top_mem_consumer {
            println!(
                " Top Memory: {} ({:.1}%)",
                top_mem,
                self.processes
                    .iter()
                    .find(|p| p.command == *top_mem)
                    .map_or(0.0, |p| p.mem_percent)
            );
        }

        println!("\n--- TOP 10 BY CPU ---");
        for (i, proc) in self.top_by_cpu(10).iter().enumerate() {
            println!(
                "{:2}. {:6} {:6} {:5.1} {:5.1} {:8} {:8} {:?} {}",
                i + 1,
                proc.pid,
                proc.user,
                proc.cpu_percent,
                proc.mem_percent,
                format_size(proc.vsz),
                format_size(proc.rss),
                proc.stat,
                truncate(&proc.command, 50)
            );
        }

        println!("\n--- TOP 10 BY MEMORY ---");
        for (i, proc) in self.top_by_mem(10).iter().enumerate() {
            println!(
                "{:2}. {:6} {:6} {:5.1} {:5.1} {:8} {:8} {:?} {}",
                i + 1,
                proc.pid,
                proc.user,
                proc.cpu_percent,
                proc.mem_percent,
                format_size(proc.vsz),
                format_size(proc.rss),
                proc.stat,
                truncate(&proc.command, 50)
            );
        }

        println!("\n{}", "=".repeat(80));
    }
}

/// Parses a single line of process output into a [`ProcessInfo`].
///
/// Expected format: PID PPID USER %CPU %MEM VSZ RSS STAT START TIME COMMAND
/// Returns `None` if the line has fewer than 10 whitespace-separated fields.
#[allow(clippy::indexing_slicing, clippy::arithmetic_side_effects)]
fn parse_ps_line(line: &str) -> Option<ProcessInfo> {
    let parts: Vec<&str> = line.split_whitespace().collect();
    if parts.len() < 10 {
        return None;
    }

    let pid = parts[0].parse().ok()?;
    let ppid = parts[1].parse().ok()?;
    let user = parts[2].to_string();
    let cpu_percent = parts[3].parse().unwrap_or(0.0);
    let mem_percent = parts[4].parse().unwrap_or(0.0);
    let vsz: u64 = parts[5].parse().unwrap_or(0);
    let rss: u64 = parts[6].parse().unwrap_or(0);
    let stat = parts[7].to_string();
    let start_time = parts[8].to_string();
    let elapsed = parts[9].to_string();
    let command = parts.get(10..).map(|s| s.join(" ")).unwrap_or_default();

    Some(ProcessInfo {
        pid,
        ppid,
        user,
        cpu_percent,
        mem_percent,
        vsz,
        rss,
        stat,
        start_time,
        elapsed,
        command,
    })
}

impl ProcessSummary {
    fn compute(processes: &[ProcessInfo]) -> Self {
        let total_processes = processes.len();
        let total_cpu_percent: f32 = processes.iter().map(|p| p.cpu_percent).sum();
        let total_mem_percent: f32 = processes.iter().map(|p| p.mem_percent).sum();

        let top_cpu_consumer = processes
            .iter()
            .max_by(|a, b| {
                a.cpu_percent
                    .partial_cmp(&b.cpu_percent)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|p| p.command.clone());

        let top_mem_consumer = processes
            .iter()
            .max_by(|a, b| {
                a.mem_percent
                    .partial_cmp(&b.mem_percent)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|p| p.command.clone());

        let zombie_count = processes.iter().filter(|p| p.stat.starts_with('Z')).count();

        Self {
            total_processes,
            total_cpu_percent,
            total_mem_percent,
            top_cpu_consumer,
            top_mem_consumer,
            zombie_count,
        }
    }
}

/// Formats a size in kilobytes as a human-readable string (K/M/G).
#[allow(clippy::cast_precision_loss)]
fn format_size(kb: u64) -> String {
    if kb >= 1024 * 1024 {
        format!("{:.1}G", kb as f64 / (1024.0 * 1024.0))
    } else if kb >= 1024 {
        format!("{:.1}M", kb as f64 / 1024.0)
    } else {
        format!("{}K", kb)
    }
}

/// Truncates a string to `max_len` characters, appending `"..."` if truncated.
///
/// Uses `char_indices()` for safe UTF-8 boundary slicing — never panics on
/// multi-byte characters.
fn truncate(s: &str, max_len: usize) -> String {
    if s.chars().count() > max_len {
        let end = max_len.saturating_sub(3);
        let byte_end = s.char_indices().nth(end).map_or(s.len(), |(i, _)| i);
        format!("{}...", &s[..byte_end])
    } else {
        s.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_process_snapshot() {
        let snapshot = ProcessSnapshot::capture();
        assert!(!snapshot.processes.is_empty());
        assert!(snapshot.summary.total_processes > 0);
    }

    #[test]
    fn test_truncate_ascii() {
        assert_eq!(truncate("hello world", 8), "hello...");
        assert_eq!(truncate("short", 10), "short");
    }

    #[test]
    fn test_truncate_multibyte_utf8() {
        // This must NOT panic — the old code panicked on multi-byte boundaries
        let cjk = "你好世界这是一个很长的命令行参数"; // 15 CJK chars
        let result = truncate(cjk, 8);
        assert!(result.ends_with("..."));
        // Should not panic and should be valid UTF-8
        assert!(result.is_char_boundary(result.len()));
    }

    #[test]
    fn test_format_size() {
        // format_size expects KB input
        assert_eq!(format_size(0), "0K");
        assert_eq!(format_size(512), "512K");
        assert_eq!(format_size(1024), "1.0M");
        assert_eq!(format_size(1024 * 1024), "1.0G");
        assert_eq!(format_size(1024 * 512), "512.0M");
        assert_eq!(format_size(1024 * 1024 * 2), "2.0G");
    }

    #[test]
    fn test_process_vsz_rss_in_kb() {
        let snap = ProcessSnapshot::capture();
        // Every process should have vsz/rss as reasonable KB values
        // (not multiplied by 1024 — that was the old bug)
        for p in &snap.processes {
            // vsz can be very large on 64-bit systems (virtual memory is cheap)
            // but should not exceed 1PB (1024*1024*1024 KB)
            assert!(
                p.vsz < 1_000_000_000,
                "vsz={}KB is unreasonably large for {}",
                p.vsz,
                p.command
            );
            // rss is physical memory — should be under 100GB for any single process
            assert!(
                p.rss < 100_000_000,
                "rss={}KB is unreasonably large for {}",
                p.rss,
                p.command
            );
        }
    }
}