1use std::collections::{HashMap, HashSet};
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct ProcNode {
20 pub pid: u32,
21 pub parent: Option<u32>,
22 pub is_agentmux: bool,
24 pub started_at_ms: Option<u64>,
29}
30
31impl ProcNode {
32 fn is_child_of(&self, parent: &ProcNode) -> bool {
36 if self.parent != Some(parent.pid) || parent.pid == self.pid {
37 return false;
38 }
39 match (parent.started_at_ms, self.started_at_ms) {
40 (Some(p), Some(c)) => p <= c,
41 _ => true,
42 }
43 }
44}
45
46pub fn own_instance_pids(nodes: &[ProcNode], self_pid: u32) -> HashSet<u32> {
62 let by_pid: HashMap<u32, &ProcNode> = nodes.iter().map(|n| (n.pid, n)).collect();
63 let Some(&start) = by_pid.get(&self_pid) else {
64 return HashSet::new();
65 };
66
67 let mut root = start;
68 let mut seen = HashSet::from([start.pid]);
69 while let Some(parent) = root.parent.and_then(|p| by_pid.get(&p).copied()) {
70 if !root.is_child_of(parent) || !parent.is_agentmux || !seen.insert(parent.pid) {
71 break;
72 }
73 root = parent;
74 }
75
76 let mut children: HashMap<u32, Vec<u32>> = HashMap::new();
77 for n in nodes {
78 if let Some(parent) = n.parent.and_then(|p| by_pid.get(&p)) {
80 if n.is_child_of(parent) {
81 children.entry(parent.pid).or_default().push(n.pid);
82 }
83 }
84 }
85 let mut own = HashSet::from([root.pid]);
86 let mut queue = vec![root.pid];
87 while let Some(pid) = queue.pop() {
88 let parent_is_agentmux = by_pid.get(&pid).is_some_and(|p| p.is_agentmux);
89 for &child in children.get(&pid).into_iter().flatten() {
90 let nested_root = !parent_is_agentmux && by_pid.get(&child).is_some_and(|c| c.is_agentmux);
92 if !nested_root && own.insert(child) {
93 queue.push(child);
94 }
95 }
96 }
97 own
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 fn n(pid: u32, parent: Option<u32>, is_agentmux: bool) -> ProcNode {
105 ProcNode { pid, parent, is_agentmux, started_at_ms: None }
106 }
107
108 fn started(mut node: ProcNode, at_ms: u64) -> ProcNode {
109 node.started_at_ms = Some(at_ms);
110 node
111 }
112
113 fn two_instances() -> Vec<ProcNode> {
116 vec![
117 n(1, None, false),
118 n(100, Some(1), true),
119 n(101, Some(100), true), n(102, Some(100), true), n(103, Some(102), true), n(200, Some(1), true),
123 n(201, Some(200), true), n(202, Some(200), true), n(203, Some(202), true), n(300, Some(201), false), ]
128 }
129
130 #[test]
131 fn two_instances_side_by_side_are_told_apart_from_srv() {
132 let t = two_instances();
133 assert_eq!(own_instance_pids(&t, 201), HashSet::from([200, 201, 202, 203, 300]));
134 assert_eq!(own_instance_pids(&t, 101), HashSet::from([100, 101, 102, 103]));
135 }
136
137 #[test]
138 fn an_agentmux_started_from_a_pane_of_this_one_is_its_own_instance() {
139 let mut t = two_instances();
142 t.extend([
143 n(301, Some(300), false),
144 n(400, Some(301), true),
145 n(401, Some(400), true),
146 n(402, Some(400), true),
147 ]);
148 let outer = own_instance_pids(&t, 201);
149 assert_eq!(outer, HashSet::from([200, 201, 202, 203, 300, 301]));
150 assert!(![400, 401, 402].iter().any(|p| outer.contains(p)), "the nested instance is not the outer one's");
151 assert_eq!(own_instance_pids(&t, 401), HashSet::from([400, 401, 402]));
153 }
154
155 #[test]
156 fn a_stale_parent_link_to_a_reused_pid_does_not_cross_into_another_instance() {
157 let t = vec![
162 started(n(400, None, true), 5_000), started(n(401, Some(400), true), 5_100), started(n(301, Some(400), true), 1_000), started(n(302, Some(301), true), 1_100),
166 ];
167 assert_eq!(own_instance_pids(&t, 301), HashSet::from([301, 302]));
170 assert_eq!(own_instance_pids(&t, 401), HashSet::from([400, 401]));
172 }
173
174 #[test]
175 fn a_parent_that_started_first_or_at_the_same_time_is_trusted() {
176 let t = vec![
177 started(n(10, None, true), 1_000),
178 started(n(11, Some(10), true), 1_000),
179 started(n(12, Some(11), true), 2_000),
180 ];
181 assert_eq!(own_instance_pids(&t, 12), HashSet::from([10, 11, 12]));
182 }
183
184 #[test]
185 fn the_same_tree_is_found_from_any_member() {
186 let t = two_instances();
187 assert_eq!(own_instance_pids(&t, 203), own_instance_pids(&t, 201));
188 assert_eq!(own_instance_pids(&t, 200), own_instance_pids(&t, 201));
189 }
190
191 #[test]
192 fn the_walk_up_stops_at_the_first_parent_that_isnt_agentmux() {
193 let t = vec![n(1, None, false), n(50, Some(1), false), n(60, Some(50), true), n(61, Some(60), true)];
195 let own = own_instance_pids(&t, 61);
196 assert_eq!(own, HashSet::from([60, 61]));
197 assert!(!own.contains(&50) && !own.contains(&1));
198 }
199
200 #[test]
201 fn srv_run_by_hand_is_its_own_instance() {
202 let t = vec![n(1, None, false), n(70, Some(1), true), n(71, Some(70), false)];
203 assert_eq!(own_instance_pids(&t, 70), HashSet::from([70, 71]));
204 }
205
206 #[test]
207 fn an_unknown_pid_or_a_missing_parent_is_handled() {
208 let t = two_instances();
209 assert!(own_instance_pids(&t, 9999).is_empty());
210 let orphan = vec![n(80, Some(7777), true), n(81, Some(80), true)];
212 assert_eq!(own_instance_pids(&orphan, 80), HashSet::from([80, 81]));
213 }
214
215 #[test]
216 fn stale_links_that_form_a_cycle_end() {
217 let t = vec![n(10, Some(11), true), n(11, Some(10), true), n(12, Some(11), true)];
219 let own = own_instance_pids(&t, 12);
220 assert!(own.contains(&12) && own.len() <= 3);
221 }
222}