fix: reserve the run tools' names, query a set's exact and subtree paths apart

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-13 18:15:01 +01:00
co-authored by Claude Opus 5.5
parent 492736044e
commit 7981ac8f1e
3 changed files with 49 additions and 22 deletions
@@ -1851,13 +1851,6 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult {
// the page before the set's filter below drops them.
query.push_str("&job_kinds=script,flow");
}
if let Some(prefixes) = set
.as_ref()
.filter(|_| path.is_none())
.and_then(ToolSetSelection::path_prefixes)
{
query.push_str(&format!("&script_path_start={}", enc(&prefixes.join(","))));
}
for (param, arg) in [
("script_path_exact", "path"),
("created_by", "created_by"),
@@ -1872,13 +1865,34 @@ async fn list_runs(ctx: &Ctx<'_>, args: &Value) -> ToolResult {
query.push_str(&format!("&{flag}={value}"));
}
}
let jobs = ctx.get(&ctx.ws(&format!("jobs/list?{query}"))).await?;
let runs: Vec<Value> = jobs
.as_array()
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()
.flatten()
// The prefixes match more than the set's patterns: `f/a/x` also starts `f/a/xy`.
.filter(|(_, paths)| !paths.is_empty())
.map(|(param, paths)| format!("{query}&{param}={}", enc(&paths.join(","))))
.collect(),
None => vec![query],
};
let mut jobs: Vec<Value> = 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.
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 runs: Vec<Value> = jobs
.iter()
// A prefix's `_` is a LIKE wildcard, and the kinds of a set's paths still need checking.
.filter(|job| set.as_ref().is_none_or(|set| in_set(set, job)))
.take(limit as usize)
.map(|job| Value::Object(summarize_run(job)))
.collect();
Ok(pretty(&Value::Array(runs)))
+16 -6
View File
@@ -72,17 +72,23 @@ impl ToolSetSelection {
})
}
/// Path prefixes covering the whole set, to narrow a runs query before `allows` filters it;
/// `None` when the set reaches every path.
pub fn path_prefixes(&self) -> Option<Vec<String>> {
/// 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<String>, Vec<String>)> {
let all = self.scripts.iter().chain(&self.flows);
if all.clone().any(|p| p == "*") {
return None;
}
Some(
all.map(|p| p.strip_suffix('*').unwrap_or(p).to_string())
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(),
)
))
}
}
@@ -286,5 +292,9 @@ 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/"]);
}
}
+7 -4
View File
@@ -177,7 +177,8 @@ pub(super) struct McpContext<A> {
}
/// The session's run tools a tool set with run history serves; their handlers confine them to
/// the set's runs.
/// the set's runs. Reserved for every token, so a runnable keeps one name whether or not a set
/// turns run history on.
const RUN_HISTORY_TOOLS: &[&str] = &["list_runs", "get_run"];
impl<B: McpBackend> Runner<B> {
@@ -690,8 +691,6 @@ impl<B: McpBackend> ServerHandler for Runner<B> {
.list_tools_single(&auth, &workspace_id, &scope_config, read_only)
.await?;
if run_history {
list.tools
.retain(|t| !RUN_HISTORY_TOOLS.contains(&t.name.as_ref()));
list.tools.extend(
self.backend
.session_tools(SessionAccess::ReadOnly)
@@ -852,7 +851,11 @@ impl<B: McpBackend> Runner<B> {
.filter(|(t, p)| !scope_config.granular || scope_config.is_allowed(t, p))
.collect();
let endpoint_tools = self.backend.all_endpoint_tools();
let reserved: HashSet<&str> = endpoint_tools.iter().map(|t| t.name.as_ref()).collect();
let reserved: HashSet<&str> = endpoint_tools
.iter()
.map(|t| t.name.as_ref())
.chain(RUN_HISTORY_TOOLS.iter().copied())
.collect();
let pairs: Vec<(&str, &str)> = items.iter().map(|(t, p)| (*t, p.as_str())).collect();
let names = assign_tool_names(&pairs, &reserved);
Ok(items