forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan.rs
More file actions
350 lines (308 loc) · 11.1 KB
/
plan.rs
File metadata and controls
350 lines (308 loc) · 11.1 KB
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
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, HashMap, HashSet};
/// A swarm plan item.
///
/// This is intentionally separate from session todos: plan data is shared at the
/// server/swarm level, while todos remain session-local.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PlanItem {
pub content: String,
pub status: String,
pub priority: String,
pub id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub subsystem: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub file_scope: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub blocked_by: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub assigned_to: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PlanGraphSummary {
pub ready_ids: Vec<String>,
pub blocked_ids: Vec<String>,
pub active_ids: Vec<String>,
pub completed_ids: Vec<String>,
pub terminal_ids: Vec<String>,
pub unresolved_dependency_ids: Vec<String>,
pub cycle_ids: Vec<String>,
}
pub fn is_completed_status(status: &str) -> bool {
matches!(status, "completed" | "done")
}
pub fn is_terminal_status(status: &str) -> bool {
matches!(
status,
"completed" | "done" | "failed" | "stopped" | "crashed"
)
}
pub fn is_active_status(status: &str) -> bool {
matches!(status, "running" | "running_stale")
}
pub fn is_runnable_status(status: &str) -> bool {
matches!(status, "queued" | "ready" | "pending" | "todo")
}
pub fn priority_rank(priority: &str) -> u8 {
match priority {
"high" | "urgent" | "p0" => 0,
"medium" | "normal" | "p1" => 1,
"low" | "p2" => 2,
_ => 1,
}
}
pub fn completed_item_ids(items: &[PlanItem]) -> HashSet<String> {
items
.iter()
.filter(|item| is_completed_status(&item.status))
.map(|item| item.id.clone())
.collect()
}
pub fn unresolved_dependencies<'a>(
item: &'a PlanItem,
known_ids: &HashSet<&'a str>,
completed_ids: &HashSet<&str>,
) -> Vec<String> {
item.blocked_by
.iter()
.filter(|dep| known_ids.contains(dep.as_str()) && !completed_ids.contains(dep.as_str()))
.cloned()
.collect()
}
pub fn missing_dependencies<'a>(item: &'a PlanItem, known_ids: &HashSet<&'a str>) -> Vec<String> {
item.blocked_by
.iter()
.filter(|dep| !known_ids.contains(dep.as_str()))
.cloned()
.collect()
}
pub fn is_unblocked<'a>(
item: &'a PlanItem,
known_ids: &HashSet<&'a str>,
completed_ids: &HashSet<&str>,
) -> bool {
missing_dependencies(item, known_ids).is_empty()
&& unresolved_dependencies(item, known_ids, completed_ids).is_empty()
}
pub fn cycle_item_ids(items: &[PlanItem]) -> Vec<String> {
let item_ids: HashSet<&str> = items.iter().map(|item| item.id.as_str()).collect();
let mut indegree: HashMap<&str, usize> = HashMap::new();
let mut dependents: HashMap<&str, Vec<&str>> = HashMap::new();
for item in items {
indegree.entry(item.id.as_str()).or_insert(0);
}
for item in items {
for dependency in item
.blocked_by
.iter()
.filter(|dependency| item_ids.contains(dependency.as_str()))
{
*indegree.entry(item.id.as_str()).or_insert(0) += 1;
dependents
.entry(dependency.as_str())
.or_default()
.push(item.id.as_str());
}
}
let mut queue: Vec<&str> = indegree
.iter()
.filter_map(|(id, degree)| (*degree == 0).then_some(*id))
.collect();
let mut visited = HashSet::new();
while let Some(id) = queue.pop() {
if !visited.insert(id) {
continue;
}
if let Some(children) = dependents.get(id) {
for child in children {
if let Some(degree) = indegree.get_mut(child) {
*degree = degree.saturating_sub(1);
if *degree == 0 {
queue.push(child);
}
}
}
}
}
let mut cycle_ids: Vec<String> = indegree
.into_iter()
.filter_map(|(id, degree)| (degree > 0 && !visited.contains(id)).then_some(id.to_string()))
.collect();
cycle_ids.sort();
cycle_ids
}
pub fn summarize_plan_graph(items: &[PlanItem]) -> PlanGraphSummary {
let known_ids: HashSet<&str> = items.iter().map(|item| item.id.as_str()).collect();
let completed_ids = completed_item_ids(items);
let completed_refs: HashSet<&str> = completed_ids.iter().map(String::as_str).collect();
let cycle_ids = cycle_item_ids(items);
let cycle_set: HashSet<&str> = cycle_ids.iter().map(String::as_str).collect();
let mut ready_ids = Vec::new();
let mut blocked_ids = Vec::new();
let mut active_ids = Vec::new();
let mut completed = BTreeSet::new();
let mut terminal = BTreeSet::new();
let mut unresolved = BTreeSet::new();
for item in items {
let missing = missing_dependencies(item, &known_ids);
let unresolved_for_item = unresolved_dependencies(item, &known_ids, &completed_refs);
let is_cyclic = cycle_set.contains(item.id.as_str());
unresolved.extend(missing.iter().cloned());
if is_active_status(&item.status) {
active_ids.push(item.id.clone());
}
if is_completed_status(&item.status) {
completed.insert(item.id.clone());
}
if is_terminal_status(&item.status) {
terminal.insert(item.id.clone());
}
let has_dependency_blocker = !unresolved_for_item.is_empty() || is_cyclic;
if is_runnable_status(&item.status) && missing.is_empty() && !has_dependency_blocker {
ready_ids.push(item.id.clone());
} else if !is_terminal_status(&item.status)
&& !is_active_status(&item.status)
&& (!missing.is_empty() || has_dependency_blocker || item.status == "blocked")
{
blocked_ids.push(item.id.clone());
}
}
ready_ids.sort();
blocked_ids.sort();
active_ids.sort();
PlanGraphSummary {
ready_ids,
blocked_ids,
active_ids,
completed_ids: completed.into_iter().collect(),
terminal_ids: terminal.into_iter().collect(),
unresolved_dependency_ids: unresolved.into_iter().collect(),
cycle_ids,
}
}
pub fn next_runnable_item_ids(items: &[PlanItem], limit: Option<usize>) -> Vec<String> {
let ready_ids: HashSet<String> = summarize_plan_graph(items).ready_ids.into_iter().collect();
let mut ready_items: Vec<&PlanItem> = items
.iter()
.filter(|item| ready_ids.contains(&item.id))
.collect();
ready_items.sort_by(|left, right| {
priority_rank(&left.priority)
.cmp(&priority_rank(&right.priority))
.then_with(|| left.id.cmp(&right.id))
});
let iter = ready_items.into_iter().map(|item| item.id.clone());
match limit {
Some(limit) => iter.take(limit).collect(),
None => iter.collect(),
}
}
pub fn newly_ready_item_ids(before: &[PlanItem], after: &[PlanItem]) -> Vec<String> {
let before_ready: HashSet<String> =
summarize_plan_graph(before).ready_ids.into_iter().collect();
let mut after_ready = summarize_plan_graph(after).ready_ids;
after_ready.retain(|item_id| !before_ready.contains(item_id));
after_ready
}
#[cfg(test)]
mod tests {
use super::*;
fn item(id: &str, status: &str, blocked_by: &[&str]) -> PlanItem {
PlanItem {
id: id.to_string(),
content: id.to_string(),
status: status.to_string(),
priority: "high".to_string(),
subsystem: None,
file_scope: Vec::new(),
blocked_by: blocked_by.iter().map(|value| value.to_string()).collect(),
assigned_to: None,
}
}
#[test]
fn summarize_plan_graph_reports_ready_and_blocked_items() {
let items = vec![
item("a", "completed", &[]),
item("b", "queued", &["a"]),
item("c", "queued", &["b"]),
];
let summary = summarize_plan_graph(&items);
assert_eq!(summary.ready_ids, vec!["b".to_string()]);
assert_eq!(summary.blocked_ids, vec!["c".to_string()]);
assert_eq!(summary.completed_ids, vec!["a".to_string()]);
assert_eq!(summary.cycle_ids, Vec::<String>::new());
}
#[test]
fn summarize_plan_graph_reports_missing_dependencies() {
let items = vec![
item("a", "queued", &["missing-task"]),
item("b", "running", &[]),
];
let summary = summarize_plan_graph(&items);
assert_eq!(summary.ready_ids, Vec::<String>::new());
assert_eq!(summary.blocked_ids, vec!["a".to_string()]);
assert_eq!(summary.active_ids, vec!["b".to_string()]);
assert_eq!(
summary.unresolved_dependency_ids,
vec!["missing-task".to_string()]
);
}
#[test]
fn newly_ready_item_ids_reports_tasks_unblocked_by_completion() {
let before = vec![
item("setup", "running", &[]),
item("follow-up", "queued", &["setup"]),
item("later", "queued", &["follow-up"]),
];
let after = vec![
item("setup", "completed", &[]),
item("follow-up", "queued", &["setup"]),
item("later", "queued", &["follow-up"]),
];
assert_eq!(newly_ready_item_ids(&before, &after), vec!["follow-up"]);
}
#[test]
fn summarize_plan_graph_reports_cycles() {
let items = vec![
item("a", "queued", &["c"]),
item("b", "queued", &["a"]),
item("c", "queued", &["b"]),
];
let summary = summarize_plan_graph(&items);
assert_eq!(summary.ready_ids, Vec::<String>::new());
assert_eq!(
summary.blocked_ids,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
assert_eq!(
summary.cycle_ids,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
}
#[test]
fn status_helpers_match_runtime_expectations() {
assert!(is_completed_status("completed"));
assert!(is_terminal_status("failed"));
assert!(is_active_status("running_stale"));
assert!(is_runnable_status("queued"));
assert!(!is_terminal_status("queued"));
}
#[test]
fn next_runnable_items_prefers_higher_priority() {
let items = vec![
item("done", "completed", &[]),
item("b", "queued", &["done"]),
PlanItem {
priority: "low".to_string(),
..item("c", "queued", &["done"])
},
PlanItem {
priority: "high".to_string(),
..item("a", "queued", &["done"])
},
];
assert_eq!(next_runnable_item_ids(&items, None), vec!["a", "b", "c"]);
assert_eq!(next_runnable_item_ids(&items, Some(2)), vec!["a", "b"]);
}
}