runtimo-core 0.1.5

Agent-centric capability runtime with telemetry, process tracking, and crash recovery for persistent machines
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
//! Kill capability — terminate runaway processes by PID with full audit trail.
//!
//! Kills a process by PID with full telemetry capture and WAL logging.
//! Includes safety checks to prevent killing critical system processes.
//!
//! # PID Reuse Protection (FINDING #1)
//!
//! After sending a signal, the capability verifies the killed process is the
//! same one by comparing start times from `/proc/{pid}/stat` field 22. This
//! prevents PID reuse races where a new process inherits the killed PID.
//!
//! # Example
//!
//! ```rust,ignore
//! use runtimo_core::capabilities::Kill;
//! use runtimo_core::capability::{Capability, Context};
//! use serde_json::json;
//!
//! let cap = Kill;
//! let result = cap.execute(
//!     &json!({"pid": 12345}),
//!     &Context { dry_run: false, job_id: "test".into(), ..Default::default() }
//! ).unwrap();
//!
//! assert!(result.success);
//! ```

use crate::capability::{Capability, Context, Output};
use crate::processes::ProcessSnapshot;
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::Duration;

#[cfg(test)]
use std::process::Command;

/// Reads the process start time (field 22) from `/proc/{pid}/stat`.
///
/// Returns start time in clock ticks since boot. Used to detect PID reuse:
/// if a process is killed and a new process reuses the PID, the start time
/// will differ (FINDING #1).
fn get_process_start_time(pid: u32) -> Option<u64> {
    let stat_path = format!("/proc/{}/stat", pid);
    let content = std::fs::read_to_string(&stat_path).ok()?;
    let last_paren = content.rfind(')')?;
    let fields: Vec<&str> = content[last_paren + 2..].split_whitespace().collect();
    fields.get(19)?.parse::<u64>().ok()
}
fn get_process_start_time_retry(pid: u32) -> Option<u64> {
    for attempt in 0..3 {
        if attempt > 0 {
            std::thread::sleep(std::time::Duration::from_millis(10 * (1 << attempt)));
        }
        if let Some(start_time) = get_process_start_time(pid) {
            return Some(start_time);
        }
    }
    None
}

/// Reads the cgroup of a process from `/proc/{pid}/cgroup`.
///
/// Returns the cgroup path string, used to detect systemd-managed services.
fn get_process_cgroup(pid: u32) -> Option<String> {
    std::fs::read_to_string(format!("/proc/{}/cgroup", pid)).ok()
}

/// Checks if a cgroup path indicates a systemd-managed service.
fn is_systemd_service(cgroup: &str) -> bool {
    cgroup.contains("/system.slice/")
        || cgroup.contains("/init.scope")
        || cgroup.contains("systemd")
}

/// Protected PIDs that cannot be killed (safety guard).
/// Includes init, kthreadd, current process, parent, session leader,
/// process group leader, and systemd critical services (FINDING #2).
fn protected_pids() -> Vec<u32> {
    let mut pids = vec![1, 2];
    let self_pid = std::process::id();
    pids.push(self_pid);

    // Add parent process
    if let Ok(status) = std::fs::read_to_string(format!("/proc/{}/status", self_pid)) {
        if let Some(ppid_str) = status
            .lines()
            .find(|l| l.starts_with("PPid:"))
            .and_then(|l| l.split_whitespace().nth(1))
        {
            if let Ok(ppid) = ppid_str.parse::<u32>() {
                pids.push(ppid);
            }
        }
    }

    // Add session leader (FINDING #2)
    if let Ok(status) = std::fs::read_to_string(format!("/proc/{}/status", self_pid)) {
        if let Some(sid_str) = status
            .lines()
            .find(|l| l.starts_with("Sid:"))
            .and_then(|l| l.split_whitespace().nth(1))
        {
            if let Ok(sid) = sid_str.parse::<u32>() {
                if sid != 0 {
                    pids.push(sid);
                }
            }
        }
    }

    // Add process group leader (FINDING #2)
    if let Ok(status) = std::fs::read_to_string(format!("/proc/{}/status", self_pid)) {
        if let Some(pgid_str) = status
            .lines()
            .find(|l| l.starts_with("NSpgid:"))
            .and_then(|l| l.split_whitespace().nth(1))
        {
            if let Ok(pgid) = pgid_str.parse::<u32>() {
                if pgid != 0 {
                    pids.push(pgid);
                }
            }
        }
    }

    // Scan all running processes for systemd-critical services (FINDING #2)
    if let Ok(entries) = std::fs::read_dir("/proc") {
        for entry in entries.flatten() {
            if let Ok(name) = entry.file_name().into_string() {
                if let Ok(pid) = name.parse::<u32>() {
                    if let Some(cgroup) = get_process_cgroup(pid) {
                        if is_systemd_service(&cgroup) {
                            pids.push(pid);
                        }
                    }
                }
            }
        }
    }

    pids.sort();
    pids.dedup();
    pids
}

/// Arguments for the [`Kill`] capability.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KillArgs {
    /// Process ID to kill.
    pub pid: u32,
    /// Signal to send (default: 15 = SIGTERM). Must be valid POSIX: 1-31 or 64.
    pub signal: Option<i32>,
}

/// Capability that terminates a process by PID with full audit logging.
///
/// # Safety
///
/// This capability includes guards to prevent killing critical system processes.
/// Protected PIDs include: 1 (init), 2 (kthreadd).
///
/// # Security
///
/// All kill operations are logged to WAL for audit purposes.
pub struct Kill;

impl Capability for Kill {
    fn name(&self) -> &'static str {
        "Kill"
    }

    fn description(&self) -> &'static str {
        "Terminate a process by PID. Protects critical system processes (init, kthreadd, self). Supports custom signals."
    }

    /// Returns the JSON Schema for Kill arguments.
    ///
    /// Schema requires `"pid"` integer; `"signal"` is optional and restricted
    /// to valid POSIX signal values (1-31, 64) — FINDING #3.
    fn schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "pid": { "type": "integer", "minimum": 1 },
                "signal": {
                    "type": "integer",
                    "anyOf": [
                        { "minimum": 1, "maximum": 31 },
                        { "enum": [64] }
                    ]
                }
            },
            "required": ["pid"]
        })
    }

    fn validate(&self, args: &Value) -> Result<()> {
        let args: KillArgs = serde_json::from_value(args.clone())
            .map_err(|e| Error::SchemaValidationFailed(e.to_string()))?;

        // FINDING #3: Restrict signal to valid POSIX values (1-31, 64)
        if let Some(signal) = args.signal {
            if !(1..=31).contains(&signal) && signal != 64 {
                return Err(Error::SchemaValidationFailed(format!(
                    "Invalid signal {}: must be 1-31 or 64 (POSIX signals)",
                    signal
                )));
            }
        }

        Ok(())
    }

    fn execute(&self, args: &Value, ctx: &Context) -> Result<Output> {
        let args: KillArgs = serde_json::from_value(args.clone())
            .map_err(|e| Error::ExecutionFailed(e.to_string()))?;

        // Safety check: protected PIDs (init, kthreadd, self, parent)
        let protected = protected_pids();
        if protected.contains(&args.pid) {
            return Err(Error::ExecutionFailed(format!(
                "PID {} is a protected system process (protected: {:?})",
                args.pid, protected
            )));
        }

        // Respect dry_run — skip kill entirely
        if ctx.dry_run {
            // FINDING #20: Limit dry-run output to "would kill PID X", hide command/user info
            return Ok(Output {
                success: true,
                data: serde_json::json!({
                    "pid": args.pid,
                    "killed": false,
                    "dry_run": true,
                    "signal": args.signal.unwrap_or(15),
                }),
                message: Some(format!("DRY RUN: would kill PID {}", args.pid)),
            });
        }

        // Capture process snapshot before kill
        let process_before = ProcessSnapshot::capture();
        let process_exists = process_before.processes.iter().any(|p| p.pid == args.pid);

        if !process_exists {
            return Ok(Output {
                success: false,
                data: serde_json::json!({
                    "pid": args.pid,
                    "killed": false,
                    "reason": "Process not found"
                }),
                message: Some(format!("Process {} not found", args.pid)),
            });
        }

        // Get process info before killing
        let process_info: Option<(String, String)> = process_before
            .processes
            .iter()
            .find(|p| p.pid == args.pid)
            .map(|p| (p.command.clone(), p.user.clone()));

        // Record start time to detect PID reuse (FINDING #1)
let start_time_before = get_process_start_time_retry(args.pid);

        // Determine signal — default to SIGTERM (15) for graceful shutdown
        let signal = args.signal.unwrap_or(15);

        // Execute kill via libc for reliability (avoids shell/PATH issues)
        let kill_result = unsafe { libc::kill(args.pid as libc::pid_t, signal) };
        let success = kill_result == 0;
        let stderr_str = if !success {
            std::io::Error::last_os_error().to_string()
        } else {
            String::new()
        };

        // Delay to let process terminate and be removed from process table
        std::thread::sleep(Duration::from_millis(500));

        // Clear cache to ensure fresh snapshot (cached data would show pre-kill state)
        ProcessSnapshot::clear_cache();

        // Capture process snapshot after kill
        let process_after = ProcessSnapshot::capture();

        // Check if process still exists (zombies count as dead — they've been terminated)
        let process_still_exists = process_after
            .processes
            .iter()
            .any(|p| p.pid == args.pid && !p.stat.starts_with('Z'));
// Verify PID was not reused — check start time matches (FINDING #1)
let pid_reused = match (start_time_before, get_process_start_time_retry(args.pid)) {
    (Some(before_time), Some(after_time)) => before_time != after_time,
    (None, _) => false,
    (Some(_), None) => true,
};

        let killed_success = success && !process_still_exists && !pid_reused;

        let message = if killed_success {
            format!("Killed process {} (signal {})", args.pid, signal)
        } else if pid_reused {
            format!(
                "PID {} was reused by a different process (start time changed)",
                args.pid
            )
        } else if !success {
            format!("Failed to kill process {}: {}", args.pid, stderr_str)
        } else {
            format!("Process {} still exists after signal {}", args.pid, signal)
        };

        Ok(Output {
            success: killed_success,
            data: serde_json::json!({
                "pid": args.pid,
                "killed": killed_success,
                "signal": signal,
                "command": process_info.as_ref().map(|(cmd, _)| cmd),
                "user": process_info.as_ref().map(|(_, user)| user),
                "stderr": if !success { stderr_str.clone() } else { String::new() },
                "pid_reused": pid_reused,
                "process_before": {
                    "count": process_before.summary.total_processes,
                    "zombies": process_before.summary.zombie_count
                },
                "process_after": {
                    "count": process_after.summary.total_processes,
                    "zombies": process_after.summary.zombie_count
                }
            }),
            message: Some(message),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability::Capability;
    use std::thread;
    use std::time::Duration;

#[test]
fn test_kill_schema() {
    let cap = Kill;
    let _schema = cap.schema();
    // Retry function test
    // Test retry logic with existing process
let mut child = Command::new("sleep").arg("60").spawn().unwrap();
let pid = child.id();

let result = get_process_start_time_retry(pid);
assert!(result.is_some(), "Should read start time for running process");

child.kill().ok();
let _ = child.wait();

// Non-existent PID should return None after retries
let result = get_process_start_time_retry(999999);
assert!(result.is_none(), "Non-existent PID should return None");
    }

    #[test]
    fn test_kill_protected_pid() {
        let cap = Kill;
        // PID 1 is protected
        let result = cap.execute(
            &serde_json::json!({ "pid": 1 }),
            &Context {
                dry_run: false,
                job_id: "test".into(),
                working_dir: std::env::current_dir().unwrap(),
            },
        );

        // Should fail because PID 1 is protected
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("protected system process"));
    }

    #[test]
    fn test_kill_self_protected() {
        let cap = Kill;
        let self_pid = std::process::id();
        let result = cap.execute(
            &serde_json::json!({ "pid": self_pid }),
            &Context {
                dry_run: false,
                job_id: "test".into(),
                working_dir: std::env::current_dir().unwrap(),
            },
        );

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("protected"));
    }

    #[test]
    fn test_kill_nonexistent() {
        let cap = Kill;
        // Use a PID that's very unlikely to exist
        let result = cap
            .execute(
                &serde_json::json!({ "pid": 999999 }),
                &Context {
                    dry_run: false,
                    job_id: "test".into(),
                    working_dir: std::env::current_dir().unwrap(),
                },
            )
            .unwrap();

        assert!(!result.success);
        assert!(result.data["killed"].as_bool() == Some(false));
    }

    #[test]
    fn test_kill_dry_run() {
        let cap = Kill;
        // Use a real PID (self) but in dry_run mode — should NOT error as protected
        // because dry_run skips the actual kill but still checks protection
        // Actually, protection check runs before dry_run, so use a non-protected PID
        let result = cap
            .execute(
                &serde_json::json!({ "pid": 999998 }),
                &Context {
                    dry_run: true,
                    job_id: "test".into(),
                    working_dir: std::env::current_dir().unwrap(),
                },
            )
            .unwrap();

        assert!(result.success);
        assert!(result.data["dry_run"].as_bool() == Some(true));
        assert!(result.data["killed"].as_bool() == Some(false));
    }

    #[test]
    fn test_kill_actual_process() {
        // Start a long-running process (sleep)
        let mut child = Command::new("sleep").arg("60").spawn().unwrap();
        let pid = child.id();

        // Give it time to start
        thread::sleep(Duration::from_millis(100));

        // Verify process exists before kill
        let pre_check = Command::new("kill").arg("-0").arg(pid.to_string()).output();
        assert!(
            pre_check.unwrap().status.success(),
            "Process should exist before kill"
        );

        // Clear cache so kill sees fresh process list
        ProcessSnapshot::clear_cache();

        // Kill it via the capability using SIGKILL for reliability
        let cap = Kill;
        let result = cap
            .execute(
                &serde_json::json!({ "pid": pid, "signal": 9 }),
                &Context {
                    dry_run: false,
                    job_id: "test".into(),
                    working_dir: std::env::current_dir().unwrap(),
                },
            )
            .unwrap();

        // Kill should succeed — process becomes zombie until reaped
        assert!(
            result.data["killed"].as_bool() == Some(true),
            "Kill failed: {:?}",
            result.data
        );
        assert!(
            result.data["signal"].as_i64() == Some(9),
            "Should use SIGKILL"
        );

        // Reap the zombie so it disappears from process table
        let _ = child.wait();

        // Verify process is fully gone after reaping
        let post_check = Command::new("kill").arg("-0").arg(pid.to_string()).output();
        let still_alive = post_check.map(|o| o.status.success()).unwrap_or(false);
        assert!(
            !still_alive,
            "Process {} should be dead after kill and reap",
            pid
        );
    }

    #[test]
    fn test_get_process_start_time() {
        // Start a process and verify we can read its start time
        let mut child = Command::new("sleep").arg("60").spawn().unwrap();
        let pid = child.id();

        let start_time = get_process_start_time(pid);
        assert!(
            start_time.is_some(),
            "Should be able to read start time for running process"
        );

        // Verify start time is consistent (no PID reuse)
        let start_time2 = get_process_start_time(pid);
        assert_eq!(start_time, start_time2, "Start time should be stable");

        child.kill().ok();
        let _ = child.wait();
    }

    #[test]
    fn test_get_process_start_time_nonexistent() {
        let result = get_process_start_time(999999);
        assert!(result.is_none(), "Non-existent PID should return None");
    }

    #[test]
    fn test_signal_validation_rejects_negative() {
        // FINDING #3: negative signals should be rejected
        let cap = Kill;
        let result = cap.validate(&serde_json::json!({ "pid": 999998, "signal": -1 }));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid signal"));
    }

    #[test]
    fn test_signal_validation_rejects_zero() {
        // FINDING #3: signal 0 should be rejected
        let cap = Kill;
        let result = cap.validate(&serde_json::json!({ "pid": 999998, "signal": 0 }));
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid signal"));
    }

    #[test]
    fn test_signal_validation_rejects_out_of_range() {
        // FINDING #3: signal > 31 (except 64) should be rejected
        let cap = Kill;
        let result = cap.validate(&serde_json::json!({ "pid": 999998, "signal": 32 }));
        assert!(result.is_err());
    }

    #[test]
    fn test_signal_validation_accepts_valid_signals() {
        let cap = Kill;
        for sig in [1, 9, 15, 31, 64] {
            let result = cap.validate(&serde_json::json!({ "pid": 999998, "signal": sig }));
            assert!(result.is_ok(), "Signal {} should be valid", sig);
        }
    }

    #[test]
    fn test_dry_run_hides_process_info() {
        // FINDING #20: dry-run should NOT expose command or user info
        let cap = Kill;
        let result = cap
            .execute(
                &serde_json::json!({ "pid": 999998 }),
                &Context {
                    dry_run: true,
                    job_id: "test".into(),
                    working_dir: std::env::current_dir().unwrap(),
                },
            )
            .unwrap();

        assert!(result.success);
        assert!(result.data["dry_run"].as_bool() == Some(true));
        assert!(result.data.get("command").is_none(), "dry-run must not expose command");
        assert!(result.data.get("user").is_none(), "dry-run must not expose user");
        assert!(result.data.get("process_exists").is_none(), "dry-run must not expose process_exists");
    }

    #[test]
    fn test_protected_pids_includes_self_and_parent() {
        let protected = protected_pids();
        let self_pid = std::process::id();
        assert!(protected.contains(&1), "PID 1 should be protected");
        assert!(protected.contains(&2), "PID 2 should be protected");
        assert!(protected.contains(&self_pid), "self PID should be protected");
    }

#[test]
fn test_get_process_start_time_retry() {
    // Test retry logic with existing process
    let mut child = Command::new("sleep").arg("60").spawn().unwrap();
    let pid = child.id();

    let result = get_process_start_time_retry(pid);
    assert!(result.is_some(), "Should read start time for running process");

    child.kill().ok();
    let _ = child.wait();

    // Non-existent PID should return None after retries
    let result = get_process_start_time_retry(999999);
    assert!(result.is_none(), "Non-existent PID should return None");
}
}