pub fn create_engine_for_subgraph(
        &self,
        changed_packages: &HashSet<PackageName>,
    ) -> Engine<Built> {
        let entrypoint_indices: Vec<_> = changed_packages
            .iter()
            .flat_map(|pkg| self.package_tasks.get(pkg))
            .flatten()
            .collect();

        // We reverse the graph because we want the *dependents* of entrypoint tasks
        let mut reversed_graph = self.task_graph.clone();
        reversed_graph.reverse();

        // This is `O(V^3)`, so in theory a bottleneck. Running dijkstra's
        // algorithm for each entrypoint task could potentially be faster.
        let node_distances = petgraph::algo::floyd_warshall::floyd_warshall(&reversed_graph, |_| 1)
            .expect("no negative cycles");

        let new_graph = self.task_graph.filter_map(
            |node_idx, node| {
                if let TaskNode::Task(task) = &self.task_graph[node_idx] {
                    // We only want to include tasks that are not persistent
                    let def = self
                        .task_definitions
                        .get(task)
                        .expect("task should have definition");

                    if def.persistent && !def.interruptible {
                        return None;
                    }
                }
                // If the node is reachable from any of the entrypoint tasks, we include it
                entrypoint_indices
                    .iter()
                    .any(|idx| {
                        node_distances
                            .get(&(**idx, node_idx))
                            .is_some_and(|dist| *dist != i32::MAX)
                    })
                    .then_some(node.clone())
            },
            |_, _| Some(()),
        );

        let task_lookup: HashMap<_, _> = new_graph
            .node_indices()
            .filter_map(|index| {
                let task = new_graph
                    .node_weight(index)
                    .expect("node index should be present");
                match task {
                    TaskNode::Root => None,
                    TaskNode::Task(task) => Some((task.clone(), index)),
                }
            })
            .collect();

        Engine {
            marker: std::marker::PhantomData,
            root_index: self.root_index,
            task_graph: new_graph,
            task_lookup,
            task_definitions: self.task_definitions.clone(),
            task_locations: self.task_locations.clone(),
            package_tasks: self.package_tasks.clone(),
            // We've filtered out persistent tasks
            has_non_interruptible_tasks: false,
        }
    }
Creates an instance of `Engine` that only contains tasks that depend on
tasks from a given package. This is useful for watch mode, where we
need to re-run only a portion of the task graph.
crates/turborepo-lib/src/engine/mod.rs