Skip to main content

magi/
blockers.rs

1//! Dependency inventory for `blocked` tasks.
2//!
3//! [`crate::daemon::resolve_blockers`] frees a task when its dependency is
4//! `done` (or its question answered) and quarantines it when the dependency no
5//! longer exists. It has nothing to say about a dependency that is alive but
6//! *stuck*: a `held` task never becomes `done` on its own, so everything
7//! waiting on it waits forever, and nothing shows why. [`Inventory`] answers
8//! that from one snapshot of the queue and the questions:
9//!
10//! - [`Inventory::waits_on`] - what a blocked task waits on and each
11//!   dependency's state, following a chain of blocked dependencies
12//!   (`4135 (blocked → 9db7 held)`), for `magi task list` and the web UI.
13//! - [`Inventory::stuck_roots`] - the tasks nothing in the loop will ever run
14//!   that a blocked task is frozen behind. [`crate::triage`] asks about each
15//!   such root once, naming everything it freezes.
16//!
17//! A blocked task is *stuck* when it has at least one unresolved dependency
18//! and every one of them is either `held` or itself a stuck blocked task.
19//! Anything that can still make progress - a queued, running or failed
20//! (retried) task, an open question, a dependency that no longer exists (which
21//! `resolve_blockers` quarantines) - makes it not stuck. A dependency cycle
22//! terminates: its members are stuck, and the smallest id in the cycle is its
23//! root.
24
25use std::collections::{BTreeMap, BTreeSet};
26
27use crate::ask::{Question, QuestionStatus};
28use crate::queue::{Task, TaskStatus};
29
30/// How many hops [`Inventory::waits_on`] follows before it stops describing.
31const CHAIN_DEPTH: usize = 4;
32
33/// One snapshot of the tasks and questions a `blocked_by` id can name.
34#[derive(Debug, Clone, Default)]
35pub struct Inventory {
36    tasks: BTreeMap<String, Task>,
37    questions: BTreeMap<String, QuestionStatus>,
38}
39
40enum Dep<'a> {
41    Task(&'a Task),
42    Question(QuestionStatus),
43    Missing,
44}
45
46type Roots = Option<BTreeSet<String>>;
47
48/// The last dash-separated part of an id: what `Task::short` shows.
49fn short(id: &str) -> &str {
50    id.split('-').next_back().unwrap_or(id)
51}
52
53impl Inventory {
54    /// Build the snapshot. Callers take `queue.list()` and `questions.list()`
55    /// once, so a listing over many tasks never rescans the disk per task.
56    pub fn new(tasks: Vec<Task>, questions: &[Question]) -> Self {
57        Self {
58            tasks: tasks.into_iter().map(|t| (t.id.clone(), t)).collect(),
59            questions: questions.iter().map(|q| (q.id.clone(), q.status)).collect(),
60        }
61    }
62
63    fn dep(&self, id: &str) -> Dep<'_> {
64        if let Some(t) = self.tasks.get(id) {
65            Dep::Task(t)
66        } else if let Some(s) = self.questions.get(id) {
67            Dep::Question(*s)
68        } else {
69            Dep::Missing
70        }
71    }
72
73    /// A dependency that has nothing left to wait for.
74    fn resolved(&self, id: &str) -> bool {
75        match self.dep(id) {
76            Dep::Task(t) => t.status == TaskStatus::Done,
77            Dep::Question(s) => s == QuestionStatus::Answered,
78            Dep::Missing => false,
79        }
80    }
81
82    fn walk(
83        &self,
84        id: &str,
85        path: &mut Vec<String>,
86        memo: &mut BTreeMap<String, Roots>,
87    ) -> (Roots, bool) {
88        if let Some(v) = memo.get(id) {
89            return (v.clone(), false);
90        }
91        let Some(task) = self.tasks.get(id) else {
92            return (None, false);
93        };
94        path.push(id.to_owned());
95        let mut roots = BTreeSet::new();
96        let mut moving = false;
97        let mut cyclic = false;
98        for b in &task.blocked_by {
99            match self.dep(b) {
100                Dep::Task(t) => match t.status {
101                    TaskStatus::Done => {}
102                    TaskStatus::Held => {
103                        roots.insert(b.clone());
104                    }
105                    TaskStatus::Blocked => {
106                        if let Some(pos) = path.iter().position(|p| p == b) {
107                            // A cycle: its smallest id speaks for all of it, so
108                            // every entry point names the same root.
109                            if let Some(min) = path[pos..].iter().min() {
110                                roots.insert(min.clone());
111                            }
112                            cyclic = true;
113                        } else {
114                            let (sub, c) = self.walk(b, path, memo);
115                            cyclic |= c;
116                            match sub {
117                                Some(r) => roots.extend(r),
118                                None => moving = true,
119                            }
120                        }
121                    }
122                    TaskStatus::Queued | TaskStatus::Running | TaskStatus::Failed => {
123                        moving = true;
124                    }
125                },
126                Dep::Question(QuestionStatus::Answered) => {}
127                Dep::Question(_) | Dep::Missing => moving = true,
128            }
129        }
130        path.pop();
131        let verdict = (!moving && !roots.is_empty()).then_some(roots);
132        if !cyclic {
133            memo.insert(id.to_owned(), verdict.clone());
134        }
135        (verdict, cyclic)
136    }
137
138    /// Every stuck `blocked` task, with the root ids it is frozen behind.
139    pub fn stuck(&self) -> BTreeMap<String, BTreeSet<String>> {
140        let mut memo = BTreeMap::new();
141        let mut out = BTreeMap::new();
142        for (id, t) in &self.tasks {
143            if t.status != TaskStatus::Blocked {
144                continue;
145            }
146            let (verdict, _) = self.walk(id, &mut Vec::new(), &mut memo);
147            if let Some(roots) = verdict {
148                out.insert(id.clone(), roots);
149            }
150        }
151        out
152    }
153
154    /// The roots `task` is frozen behind; empty when it is not stuck.
155    pub fn stuck_roots(&self, task: &Task) -> BTreeSet<String> {
156        if task.status != TaskStatus::Blocked {
157            return BTreeSet::new();
158        }
159        self.walk(&task.id, &mut Vec::new(), &mut BTreeMap::new())
160            .0
161            .unwrap_or_default()
162    }
163
164    /// Each dependency of `task` with its state, e.g. `9db7 (held)` or
165    /// `4135 (blocked → 9db7 held)`. Empty unless `task` is `blocked`.
166    pub fn waits_on(&self, task: &Task) -> Vec<String> {
167        if task.status != TaskStatus::Blocked {
168            return Vec::new();
169        }
170        task.blocked_by
171            .iter()
172            .map(|b| {
173                let chain = self.chain(b, &mut vec![task.id.clone()]);
174                format!("{} ({chain})", short(b))
175            })
176            .collect()
177    }
178
179    fn chain(&self, id: &str, seen: &mut Vec<String>) -> String {
180        match self.dep(id) {
181            Dep::Missing => "missing".to_owned(),
182            Dep::Question(s) => format!("question {}", s.as_str()),
183            Dep::Task(t) => {
184                let mut out = t.status.as_str().to_owned();
185                if t.status != TaskStatus::Blocked {
186                    return out;
187                }
188                if seen.iter().any(|s| s == id) {
189                    return format!("{out}, cycle");
190                }
191                if seen.len() > CHAIN_DEPTH {
192                    return format!("{out} → …");
193                }
194                seen.push(id.to_owned());
195                if let Some(next) = t.blocked_by.iter().find(|b| !self.resolved(b)) {
196                    out.push_str(&format!(" → {} {}", short(next), self.chain(next, seen)));
197                }
198                out
199            }
200        }
201    }
202
203    /// The task with this id, if it is one.
204    pub fn task(&self, id: &str) -> Option<&Task> {
205        self.tasks.get(id)
206    }
207}
208
209#[cfg(test)]
210mod tests {
211    use super::*;
212    use crate::queue::Source;
213    use std::path::PathBuf;
214
215    fn t(id: &str, status: TaskStatus, blocked_by: &[&str]) -> Task {
216        let mut t = Task::new(
217            id.to_owned(),
218            "x".to_owned(),
219            PathBuf::from("/repo"),
220            Source::Human,
221        );
222        t.id = format!("2026-{id}");
223        t.status = status;
224        t.blocked_by = blocked_by.iter().map(|b| format!("2026-{b}")).collect();
225        t
226    }
227
228    fn inv(tasks: Vec<Task>) -> Inventory {
229        Inventory::new(tasks, &[])
230    }
231
232    #[test]
233    fn a_chain_reads_at_a_glance_and_its_root_is_the_held_task() {
234        let i = inv(vec![
235            t("9db7", TaskStatus::Held, &[]),
236            t("4135", TaskStatus::Blocked, &["9db7"]),
237            t("6081", TaskStatus::Blocked, &["4135"]),
238        ]);
239        let six = i.task("2026-6081").unwrap();
240        assert_eq!(i.waits_on(six), ["4135 (blocked → 9db7 held)"]);
241        let four = i.task("2026-4135").unwrap();
242        assert_eq!(i.waits_on(four), ["9db7 (held)"]);
243        let stuck = i.stuck();
244        assert_eq!(stuck.len(), 2);
245        assert!(stuck.values().all(|r| r.iter().eq(["2026-9db7"].iter())));
246    }
247
248    #[test]
249    fn a_dependency_that_can_still_run_is_not_stuck() {
250        let i = inv(vec![
251            t("aaaa", TaskStatus::Queued, &[]),
252            t("bbbb", TaskStatus::Held, &[]),
253            t("cccc", TaskStatus::Blocked, &["aaaa", "bbbb"]),
254            t("dddd", TaskStatus::Blocked, &["missing1"]),
255            t("eeee", TaskStatus::Blocked, &["cccc"]),
256        ]);
257        assert!(i.stuck().is_empty(), "{:?}", i.stuck());
258    }
259
260    #[test]
261    fn a_done_dependency_does_not_hide_a_held_one() {
262        let i = inv(vec![
263            t("aaaa", TaskStatus::Done, &[]),
264            t("bbbb", TaskStatus::Held, &[]),
265            t("cccc", TaskStatus::Blocked, &["aaaa", "bbbb"]),
266        ]);
267        assert_eq!(i.stuck().len(), 1);
268    }
269
270    #[test]
271    fn a_cycle_terminates_and_names_its_smallest_id_from_every_entry() {
272        let i = inv(vec![
273            t("bbbb", TaskStatus::Blocked, &["cccc"]),
274            t("cccc", TaskStatus::Blocked, &["aaaa"]),
275            t("aaaa", TaskStatus::Blocked, &["bbbb"]),
276            t("dddd", TaskStatus::Blocked, &["cccc"]),
277        ]);
278        let stuck = i.stuck();
279        assert_eq!(stuck.len(), 4);
280        for roots in stuck.values() {
281            assert!(roots.iter().eq(["2026-aaaa"].iter()), "{roots:?}");
282        }
283        let d = i.task("2026-dddd").unwrap();
284        assert!(i.waits_on(d)[0].contains("cycle"), "{:?}", i.waits_on(d));
285    }
286
287    #[test]
288    fn a_self_dependency_is_a_stuck_cycle_of_one() {
289        let i = inv(vec![t("aaaa", TaskStatus::Blocked, &["aaaa"])]);
290        assert_eq!(i.stuck().len(), 1);
291    }
292
293    #[test]
294    fn an_open_question_keeps_a_task_alive_and_an_answered_one_resolves() {
295        let mut open = Question::new(
296            "r".into(),
297            "n".into(),
298            "s".into(),
299            "?".into(),
300            String::new(),
301            vec![],
302        );
303        open.id = "2026-qqqq".into();
304        let mut answered = open.clone();
305        answered.id = "2026-rrrr".into();
306        answered.status = QuestionStatus::Answered;
307        let tasks = vec![
308            t("bbbb", TaskStatus::Held, &[]),
309            t("cccc", TaskStatus::Blocked, &["bbbb", "qqqq"]),
310            t("dddd", TaskStatus::Blocked, &["bbbb", "rrrr"]),
311        ];
312        let i = Inventory::new(tasks, &[open, answered]);
313        let stuck = i.stuck();
314        assert!(!stuck.contains_key("2026-cccc"));
315        assert!(stuck.contains_key("2026-dddd"));
316        let c = i.task("2026-cccc").unwrap();
317        assert_eq!(i.waits_on(c)[1], "qqqq (question open)");
318    }
319}