From 2f1fd26a9f0a48471daf5b5c4dd6275ff1117329 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:48:06 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20task=20unblocking=20to?= =?UTF-8?q?=20fix=20N+1=20query=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: undivisible <136312656+undivisible@users.noreply.github.com> --- src/swarm/team.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/swarm/team.rs b/src/swarm/team.rs index f2d33043..17c902ed 100644 --- a/src/swarm/team.rs +++ b/src/swarm/team.rs @@ -102,11 +102,26 @@ impl TeamManager { &self, blocked_by: &[String], ignore_task_id: Option<&str>, + prefetched_blockers: Option<&std::collections::HashMap>, ) -> Result { if blocked_by.is_empty() { return Ok(false); } + if let Some(map) = prefetched_blockers { + for b_id in blocked_by { + if Some(b_id.as_str()) == ignore_task_id { + continue; + } + if let Some(blocker) = map.get(b_id) { + if blocker.status != "done" { + return Ok(true); + } + } + } + return Ok(false); + } + let blockers = self.storage.get_team_tasks(blocked_by).await?; for blocker in blockers { if Some(blocker.task_id.as_str()) == ignore_task_id { @@ -177,7 +192,7 @@ impl TeamManager { // Check if task is blocked if task.status == "blocked" && self - .has_unresolved_dependencies(&task.blocked_by, None) + .has_unresolved_dependencies(&task.blocked_by, None, None) .await? { bail!("Task '{}' is blocked by incomplete dependencies", task_id); @@ -198,10 +213,37 @@ impl TeamManager { .ok_or_else(|| anyhow::anyhow!("Task '{}' not found after completion", task_id))?; let blocked = self.storage.get_blocked_tasks(&task.team_id).await?; + + // Find all unique blockers for all blocked tasks, excluding the one just completed + let mut all_blocker_ids = std::collections::HashSet::new(); + for bt in &blocked { + if bt.blocked_by.contains(&task_id.to_string()) { + for blocker_id in &bt.blocked_by { + if blocker_id != task_id { + all_blocker_ids.insert(blocker_id.clone()); + } + } + } + } + + let all_blocker_ids: Vec = all_blocker_ids.into_iter().collect(); + // pre-fetch them + let all_blockers = if all_blocker_ids.is_empty() { + Vec::new() + } else { + self.storage.get_team_tasks(&all_blocker_ids).await? + }; + + // build a fast lookup map + let blocker_map: std::collections::HashMap = all_blockers + .into_iter() + .map(|b| (b.task_id.clone(), b)) + .collect(); + for bt in blocked { if bt.blocked_by.contains(&task_id.to_string()) && !self - .has_unresolved_dependencies(&bt.blocked_by, Some(task_id)) + .has_unresolved_dependencies(&bt.blocked_by, Some(task_id), Some(&blocker_map)) .await? { // Unblock the task (move from blocked to pending)