| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use crate::id_types::{TaskDependencyId, TaskId}; |
| 14 |
use chrono::{DateTime, Utc}; |
| 15 |
use serde::{Deserialize, Serialize}; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const GOINGSON_TASK_DEPENDENCY_NS: uuid::Uuid = uuid::Uuid::from_bytes([ |
| 20 |
0x84, 0x1b, 0x2e, 0xc5, 0x7f, 0x36, 0x5a, 0x90, 0xb4, 0x28, 0x1d, 0x6f, 0x39, 0xa7, 0xc0, 0x1e, |
| 21 |
]); |
| 22 |
|
| 23 |
|
| 24 |
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 25 |
#[serde(rename_all = "camelCase")] |
| 26 |
pub struct TaskDependency { |
| 27 |
|
| 28 |
pub id: TaskDependencyId, |
| 29 |
|
| 30 |
pub blocked_id: TaskId, |
| 31 |
|
| 32 |
pub blocker_id: TaskId, |
| 33 |
|
| 34 |
pub created_at: DateTime<Utc>, |
| 35 |
} |
| 36 |
|
| 37 |
impl TaskDependency { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub fn deterministic_id(blocker_id: TaskId, blocked_id: TaskId) -> TaskDependencyId { |
| 46 |
let key = format!("{blocker_id}:{blocked_id}"); |
| 47 |
TaskDependencyId::from(uuid::Uuid::new_v5( |
| 48 |
&GOINGSON_TASK_DEPENDENCY_NS, |
| 49 |
key.as_bytes(), |
| 50 |
)) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 57 |
#[serde(rename_all = "camelCase")] |
| 58 |
pub struct LinkedTaskRef { |
| 59 |
|
| 60 |
pub id: TaskId, |
| 61 |
|
| 62 |
pub title: String, |
| 63 |
|
| 64 |
pub status: super::TaskStatus, |
| 65 |
|
| 66 |
|
| 67 |
pub project_name: Option<String>, |
| 68 |
} |
| 69 |
|
| 70 |
impl LinkedTaskRef { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub fn is_satisfied(&self) -> bool { |
| 79 |
matches!( |
| 80 |
self.status, |
| 81 |
super::TaskStatus::Completed | super::TaskStatus::Deleted |
| 82 |
) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] |
| 89 |
#[serde(rename_all = "camelCase")] |
| 90 |
pub struct GraphPosition { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub block_depth: u32, |
| 97 |
|
| 98 |
|
| 99 |
pub unblocks_count: u32, |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
pub in_cycle: bool, |
| 107 |
} |
| 108 |
|
| 109 |
impl GraphPosition { |
| 110 |
|
| 111 |
pub fn is_blocked(&self) -> bool { |
| 112 |
self.block_depth > 0 |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
pub fn is_ready(&self) -> bool { |
| 117 |
self.block_depth == 0 |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 125 |
#[serde(rename_all = "camelCase")] |
| 126 |
pub struct TaskGraph { |
| 127 |
|
| 128 |
pub nodes: Vec<TaskGraphNode>, |
| 129 |
|
| 130 |
pub edges: Vec<TaskDependency>, |
| 131 |
|
| 132 |
pub cycles: Vec<Vec<TaskId>>, |
| 133 |
} |
| 134 |
|
| 135 |
impl TaskGraph { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn ready(&self) -> Vec<&TaskGraphNode> { |
| 142 |
let mut ready: Vec<&TaskGraphNode> = self |
| 143 |
.nodes |
| 144 |
.iter() |
| 145 |
.filter(|n| n.position.is_ready()) |
| 146 |
.collect(); |
| 147 |
ready.sort_by_key(|n| std::cmp::Reverse(n.position.unblocks_count)); |
| 148 |
ready |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
pub fn is_acyclic(&self) -> bool { |
| 153 |
self.cycles.is_empty() |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 159 |
#[serde(rename_all = "camelCase")] |
| 160 |
pub struct TaskGraphNode { |
| 161 |
|
| 162 |
pub id: TaskId, |
| 163 |
|
| 164 |
pub title: String, |
| 165 |
|
| 166 |
pub status: super::TaskStatus, |
| 167 |
|
| 168 |
pub priority: super::Priority, |
| 169 |
|
| 170 |
pub project_id: Option<crate::id_types::ProjectId>, |
| 171 |
|
| 172 |
pub position: GraphPosition, |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 190 |
#[serde(rename_all = "camelCase")] |
| 191 |
pub struct PlanGate { |
| 192 |
|
| 193 |
|
| 194 |
pub after: Vec<LinkedTaskRef>, |
| 195 |
|
| 196 |
|
| 197 |
pub unlocked_by_plan: bool, |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
pub out_of_order: bool, |
| 206 |
} |
| 207 |
|
| 208 |
impl PlanGate { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
pub fn lead_blocker(&self) -> Option<&LinkedTaskRef> { |
| 214 |
self.after.first() |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 224 |
pub enum DependencyRejection { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
WouldCycle { path: Vec<TaskId> }, |
| 229 |
|
| 230 |
SelfEdge { task_id: TaskId }, |
| 231 |
} |
| 232 |
|
| 233 |
impl std::fmt::Display for DependencyRejection { |
| 234 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 235 |
match self { |
| 236 |
Self::WouldCycle { path } => { |
| 237 |
let chain: Vec<String> = path.iter().map(ToString::to_string).collect(); |
| 238 |
write!( |
| 239 |
f, |
| 240 |
"that edge would close a dependency cycle: {} already depends on it", |
| 241 |
chain.join(" -> ") |
| 242 |
) |
| 243 |
} |
| 244 |
Self::SelfEdge { task_id } => write!(f, "task {task_id} cannot block itself"), |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
impl std::error::Error for DependencyRejection {} |
| 250 |
|
| 251 |
#[cfg(test)] |
| 252 |
mod tests { |
| 253 |
use super::*; |
| 254 |
use crate::models::{Priority, TaskStatus}; |
| 255 |
|
| 256 |
fn node(title: &str, depth: u32, unblocks: u32) -> TaskGraphNode { |
| 257 |
TaskGraphNode { |
| 258 |
id: TaskId::new(), |
| 259 |
title: title.to_string(), |
| 260 |
status: TaskStatus::Pending, |
| 261 |
priority: Priority::Medium, |
| 262 |
project_id: None, |
| 263 |
position: GraphPosition { |
| 264 |
block_depth: depth, |
| 265 |
unblocks_count: unblocks, |
| 266 |
in_cycle: false, |
| 267 |
}, |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
fn edge_id_is_stable_content_derived_and_directional() { |
| 273 |
let a = TaskId::new(); |
| 274 |
let b = TaskId::new(); |
| 275 |
assert_eq!( |
| 276 |
TaskDependency::deterministic_id(a, b), |
| 277 |
TaskDependency::deterministic_id(a, b), |
| 278 |
"same pair must yield the same id so two devices converge" |
| 279 |
); |
| 280 |
assert_ne!( |
| 281 |
TaskDependency::deterministic_id(a, b), |
| 282 |
TaskDependency::deterministic_id(b, a), |
| 283 |
"the reverse edge is a different claim and must be a different row" |
| 284 |
); |
| 285 |
assert_eq!( |
| 286 |
TaskDependency::deterministic_id(a, b) |
| 287 |
.as_uuid() |
| 288 |
.get_version_num(), |
| 289 |
5 |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn a_position_is_blocked_exactly_when_it_has_depth() { |
| 295 |
let ready = GraphPosition::default(); |
| 296 |
assert!(ready.is_ready()); |
| 297 |
assert!(!ready.is_blocked()); |
| 298 |
|
| 299 |
let waiting = GraphPosition { |
| 300 |
block_depth: 1, |
| 301 |
..Default::default() |
| 302 |
}; |
| 303 |
assert!(waiting.is_blocked()); |
| 304 |
assert!(!waiting.is_ready()); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn completed_and_deleted_blockers_stop_gating_but_nothing_else_does() { |
| 309 |
let mut r = LinkedTaskRef { |
| 310 |
id: TaskId::new(), |
| 311 |
title: "blocker".into(), |
| 312 |
status: TaskStatus::Pending, |
| 313 |
project_name: None, |
| 314 |
}; |
| 315 |
assert!(!r.is_satisfied()); |
| 316 |
r.status = TaskStatus::Started; |
| 317 |
assert!(!r.is_satisfied(), "started is not finished"); |
| 318 |
r.status = TaskStatus::Completed; |
| 319 |
assert!(r.is_satisfied()); |
| 320 |
r.status = TaskStatus::Deleted; |
| 321 |
assert!( |
| 322 |
r.is_satisfied(), |
| 323 |
"a deleted blocker would otherwise strand its dependents forever" |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn ready_returns_only_startable_nodes_most_unblocking_first() { |
| 329 |
let graph = TaskGraph { |
| 330 |
nodes: vec![ |
| 331 |
node("frees one", 0, 1), |
| 332 |
node("waiting", 2, 9), |
| 333 |
node("frees four", 0, 4), |
| 334 |
node("frees none", 0, 0), |
| 335 |
], |
| 336 |
edges: vec![], |
| 337 |
cycles: vec![], |
| 338 |
}; |
| 339 |
let ready: Vec<&str> = graph.ready().iter().map(|n| n.title.as_str()).collect(); |
| 340 |
assert_eq!( |
| 341 |
ready, |
| 342 |
vec!["frees four", "frees one", "frees none"], |
| 343 |
"blocked nodes are excluded however much they would free" |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
#[test] |
| 348 |
fn a_graph_with_a_cycle_is_not_acyclic() { |
| 349 |
let mut graph = TaskGraph::default(); |
| 350 |
assert!(graph.is_acyclic(), "an empty graph is trivially a DAG"); |
| 351 |
graph.cycles.push(vec![TaskId::new(), TaskId::new()]); |
| 352 |
assert!(!graph.is_acyclic()); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn a_cycle_rejection_names_the_path_already_in_the_way() { |
| 357 |
let a = TaskId::new(); |
| 358 |
let b = TaskId::new(); |
| 359 |
let msg = DependencyRejection::WouldCycle { path: vec![a, b] }.to_string(); |
| 360 |
assert!(msg.contains(&a.to_string()) && msg.contains(&b.to_string())); |
| 361 |
assert!(msg.contains("->"), "the path reads as a chain"); |
| 362 |
} |
| 363 |
} |
| 364 |
|