diff --git a/backend/windmill-api/src/ai_session_tools/runs.rs b/backend/windmill-api/src/ai_session_tools/runs.rs index defbb773ed..9c4ad69b21 100644 --- a/backend/windmill-api/src/ai_session_tools/runs.rs +++ b/backend/windmill-api/src/ai_session_tools/runs.rs @@ -1846,11 +1846,6 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult { .unwrap_or(30) .clamp(1, 100); let mut query = format!("per_page={limit}"); - if set.is_some() { - // A flow's inline steps run at paths under the flow's: unfiltered, they would fill - // the page before the set's filter below drops them. - query.push_str("&job_kinds=script,flow"); - } for (param, arg) in [ ("script_path_exact", "path"), ("created_by", "created_by"), @@ -1865,29 +1860,30 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult { query.push_str(&format!("&{flag}={value}")); } } - let queries = match set - .as_ref() - .filter(|_| path.is_none()) - .and_then(ToolSetSelection::path_filters) - { - Some((exact, prefixes)) => [ - ("script_path_exact", exact), - ("script_path_start", prefixes), - ] - .into_iter() - .filter(|(_, paths)| !paths.is_empty()) - .map(|(param, paths)| format!("{query}&{param}={}", enc(&paths.join(",")))) - .collect(), - None => vec![query], + // A flow's inline steps run at paths under the flow's, so a set's queries name the kind too. + let queries: Vec = match (&set, path) { + (Some(_), Some(_)) => vec![format!("{query}&job_kinds=script,flow")], + (Some(set), None) => set + .run_queries() + .into_iter() + .map(|(kind, filter)| match filter { + Some((param, paths)) => { + format!("{query}&job_kinds={kind}&{param}={}", enc(&paths.join(","))) + } + None => format!("{query}&job_kinds={kind}"), + }) + .collect(), + (None, _) => vec![query], }; let mut jobs: Vec = Vec::new(); for query in queries { let page = ctx.get(&ctx.ws(&format!("jobs/list?{query}"))).await?; jobs.extend(page.as_array().into_iter().flatten().cloned()); } - // Newest first across both queries; ISO timestamps sort as text. + // Newest first across the queries; ISO timestamps sort as text. jobs.sort_by(|a, b| b["created_at"].as_str().cmp(&a["created_at"].as_str())); - jobs.dedup_by(|a, b| a["id"] == b["id"]); + let mut seen = std::collections::HashSet::new(); + jobs.retain(|job| seen.insert(job["id"].as_str().unwrap_or_default().to_string())); let runs: Vec = jobs .iter() // A prefix's `_` is a LIKE wildcard, and the kinds of a set's paths still need checking. diff --git a/backend/windmill-common/src/mcp_tool_sets.rs b/backend/windmill-common/src/mcp_tool_sets.rs index 5a5c7600e1..5ab9eb2f6b 100644 --- a/backend/windmill-common/src/mcp_tool_sets.rs +++ b/backend/windmill-common/src/mcp_tool_sets.rs @@ -72,23 +72,30 @@ impl ToolSetSelection { }) } - /// The set's exact paths and its subtrees' prefixes (`f/ops/`), to narrow a runs query before - /// `allows` filters it; `None` when the set reaches every path. Two lists, as the jobs API - /// ANDs its exact and prefix filters, and a prefix for an exact path would also match its - /// longer siblings (`f/a/x` starts `f/a/xy`). - pub fn path_filters(&self) -> Option<(Vec, Vec)> { - let all = self.scripts.iter().chain(&self.flows); - if all.clone().any(|p| p == "*") { - return None; + /// The runs queries covering the set, as `(job kind, path filter)`: a page of one query is + /// then rarely emptied by `allows` before it reaches the caller. Each kind gets its own, so a + /// scripts-only set is not crowded out by the folder's flow runs; its exact paths and subtree + /// prefixes (`f/ops/`) are queried apart, as the jobs API ANDs the two filters and a prefix + /// for an exact path would also match its longer siblings. `None`: every path of that kind. + pub fn run_queries(&self) -> Vec<(&'static str, Option<(&'static str, Vec)>)> { + let mut queries = Vec::new(); + for (kind, patterns) in [("script", &self.scripts), ("flow", &self.flows)] { + if patterns.iter().any(|p| p == "*") { + queries.push((kind, None)); + continue; + } + let (subtrees, exact): (Vec<&String>, Vec<&String>) = + patterns.iter().partition(|p| p.ends_with("/*")); + if !exact.is_empty() { + let exact = exact.into_iter().cloned().collect(); + queries.push((kind, Some(("script_path_exact", exact)))); + } + if !subtrees.is_empty() { + let prefixes = subtrees.iter().map(|p| p.trim_end_matches('*').to_string()); + queries.push((kind, Some(("script_path_start", prefixes.collect())))); + } } - let (subtrees, exact): (Vec<&String>, Vec<&String>) = all.partition(|p| p.ends_with("/*")); - Some(( - exact.into_iter().cloned().collect(), - subtrees - .into_iter() - .map(|p| p.trim_end_matches('*').to_string()) - .collect(), - )) + queries } } @@ -292,9 +299,17 @@ mod tests { assert!(!set.allows("flow", "f/a/x")); assert!(!set.allows("singlestepflow", "f/ops/check")); assert!(tool_set_selection(&["mcp:scripts:f/ops/*".to_string()]).is_none()); - // An exact path is not queried as a prefix, which would also match its longer siblings. - let (exact, prefixes) = set.path_filters().unwrap(); - assert_eq!(exact, ["f/a/x", "f/a/sync"]); - assert_eq!(prefixes, ["f/ops/"]); + // Each kind queries its own paths, and an exact path is never queried as a prefix. + let filter = |param: &'static str, paths: &[&str]| { + Some((param, paths.iter().map(|p| p.to_string()).collect())) + }; + assert_eq!( + set.run_queries(), + [ + ("script", filter("script_path_exact", &["f/a/x"])), + ("script", filter("script_path_start", &["f/ops/"])), + ("flow", filter("script_path_exact", &["f/a/sync"])), + ] + ); } }