use std::time::Duration;
use serde::{Deserialize, Serialize};
pub fn parse_top(out: &str) -> Vec<(String, String, String)> {
out.lines()
.skip(1)
.filter_map(|line| {
let mut it = line.split_whitespace();
let pid = it.next()?;
let ppid = it.next()?;
let args = remainder_after(line, pid, ppid)?;
Some((pid.to_string(), ppid.to_string(), args.to_string()))
})
.collect()
}
fn remainder_after<'a>(line: &'a str, pid: &str, ppid: &str) -> Option<&'a str> {
let after_pid = line.trim_start().strip_prefix(pid)?;
let after_ppid = after_pid.trim_start().strip_prefix(ppid)?;
let args = after_ppid.trim_start();
if args.is_empty() {
None
} else {
Some(args)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Run {
pub id: String,
pub state: String,
pub started: String,
pub argv: String,
}
pub fn parse_runs(ls: &str) -> Vec<Run> {
ls.lines().filter(|l| !l.trim().is_empty()).map(parse_run).collect()
}
fn parse_run(line: &str) -> Run {
let words: Vec<&str> = line.split(' ').collect();
let state_len = match words.get(1) {
Some(&"exited" | &"killed") => 2,
_ => 1,
};
let join = |from: usize, to: usize| words.get(from..to.min(words.len())).unwrap_or(&[]).join(" ");
Run {
id: words.first().copied().unwrap_or("").to_string(),
state: join(1, 1 + state_len),
started: words.get(1 + state_len).copied().unwrap_or("").to_string(),
argv: join(2 + state_len, words.len()),
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionCount {
pub running: usize,
pub detached: usize,
}
pub fn count_sessions(ls: &str) -> SessionCount {
let mut count = SessionCount::default();
for line in ls.lines() {
let mut words = line.split(' ').skip(1);
if words.next() != Some("running") {
continue;
}
count.running += 1;
if words.next() == Some("detached") {
count.detached += 1;
}
}
count
}
const RUN_LS_CAP: usize = 64 * 1024;
const RUN_LS_TIMEOUT: Duration = Duration::from_secs(5);
pub fn run_ls(container: &str) -> Option<String> {
helper_ls(container, "run")
}
pub fn session_ls(container: &str) -> Option<String> {
helper_ls(container, "session")
}
fn helper_ls(container: &str, noun: &str) -> Option<String> {
let backend = super::backend();
let mut cmd = std::process::Command::new(backend.bin());
cmd.args(["exec", container, crate::devsbd::BIN, noun, "ls"]).stdin(std::process::Stdio::null());
let out = super::bounded::run(&mut cmd, RUN_LS_CAP, RUN_LS_TIMEOUT).ok()?;
(out.status.success() && !out.truncated).then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_top_skips_header_and_keeps_arg_spacing() {
let out = concat!(
"PID PPID COMMAND\n",
"1 0 /sbin/init\n",
"42 1 node server.js --port 3000\n",
);
let rows = parse_top(out);
assert_eq!(
rows,
vec![
("1".into(), "0".into(), "/sbin/init".into()),
("42".into(), "1".into(), "node server.js --port 3000".into()),
],
);
}
#[test]
fn parse_top_skips_short_lines() {
let out = "PID PPID ARGS\n1 0\n2 0 sh\n";
let rows = parse_top(out);
assert_eq!(rows, vec![("2".into(), "0".into(), "sh".into())]);
}
fn run(id: &str, state: &str, started: &str, argv: &str) -> Run {
Run { id: id.into(), state: state.into(), started: started.into(), argv: argv.into() }
}
#[test]
fn parse_runs_splits_state_with_its_code() {
let ls = "1790000000-a1b2 exited 4 2026-09-21T14:13:20Z sh -c echo hi\n\
\n\
1790000001-0000 running 2026-09-21T14:13:21Z zidane -p fix it\n\
1790000002-ffff killed 9 2026-09-21T14:13:22Z sleep 9\n";
assert_eq!(
parse_runs(ls),
[
run("1790000000-a1b2", "exited 4", "2026-09-21T14:13:20Z", "sh -c echo hi"),
run("1790000001-0000", "running", "2026-09-21T14:13:21Z", "zidane -p fix it"),
run("1790000002-ffff", "killed 9", "2026-09-21T14:13:22Z", "sleep 9"),
]
);
assert!(parse_runs("").is_empty());
}
#[test]
fn parse_runs_keeps_short_lines_with_empty_fields() {
assert_eq!(parse_runs("x exited"), [run("x", "exited", "", "")]);
assert_eq!(parse_runs("x lost 2026"), [run("x", "lost", "2026", "")]);
assert_eq!(parse_runs("x"), [run("x", "", "", "")]);
}
#[test]
fn count_sessions_counts_running_and_detached() {
let ls = "1790000000-a1b2 exited 0 - 2026-09-21T14:13:20Z bash\n\
1790000001-0000 running attached 2026-09-21T14:13:21Z zidane\n\
\n\
1790000002-ffff running detached 2026-09-21T14:13:22Z claude\n\
1790000003-eeee lost - 2026-09-21T14:13:23Z sh\n\
1790000004-dddd running detached 2026-09-21T14:13:24Z\n\
1790000005-cccc running\n";
assert_eq!(count_sessions(ls), SessionCount { running: 4, detached: 2 });
assert_eq!(count_sessions(""), SessionCount::default());
assert_eq!(count_sessions("running detached"), SessionCount::default(), "state is the 2nd word");
}
}