pub async fn check_circular_dependency(
pool: &SqlitePool,
blocking_task_id: i64,
blocked_task_id: i64,
) -> Result<bool>Expand description
Check if adding a dependency would create a circular dependency.
This function implements a depth-first search using SQLite’s recursive CTE to detect cycles in the dependency graph.
§Algorithm
To check if we can add “blocked_task depends on blocking_task”:
- Start from blocking_task (the new prerequisite)
- Traverse what blocking_task depends on (its blocking tasks)
- If we ever reach blocked_task, adding this dependency would create a cycle
§Example
Existing: A depends on B (stored as: blocking=B, blocked=A) Trying to add: B depends on A (would be: blocking=A, blocked=B)
Check: Does A depend on B?
- Start from A (new blocking task)
- Find what A depends on: B
- We reached B (new blocked task) → Cycle detected!
§Performance
- Time complexity: O(V + E) where V is tasks and E is dependencies
- Expected: <10ms for graphs with 10,000 tasks
- Depth limit: 100 levels to prevent infinite loops
§Arguments
pool- Database connection poolblocking_task_id- ID of the task that must be completed firstblocked_task_id- ID of the task that depends on the blocking task
§Returns
Ok(true)if adding this dependency would create a cycleOk(false)if the dependency is safe to addErrif database query fails