use crate::mcp::{McpRef, McpSegment};
use crate::tool_names::{Capability, ParsedToolName, is_mcp_wire_token};
use crate::tool_policy::ToolRule;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Relation {
Equal,
Covers,
CoveredBy,
Disjoint,
Unknown,
}
#[derive(Debug, Clone, Copy)]
pub struct ToolKey<'a> {
pub capability: &'a Capability,
pub pattern: Option<&'a str>,
}
impl<'a> From<&'a ToolRule> for ToolKey<'a> {
fn from(rule: &'a ToolRule) -> Self {
Self {
capability: &rule.capability,
pattern: rule.pattern.as_deref(),
}
}
}
impl<'a> From<&'a ParsedToolName> for ToolKey<'a> {
fn from(name: &'a ParsedToolName) -> Self {
Self {
capability: &name.capability,
pattern: name.pattern.as_deref(),
}
}
}
pub fn capability_covers(a: &str, b: &str) -> bool {
matches!(
(a, b),
("edit", "write" | "notebook") | ("web", "web_search" | "web_fetch")
)
}
pub fn relation(a: ToolKey<'_>, b: ToolKey<'_>) -> Relation {
if a.capability == b.capability && a.pattern == b.pattern {
return Relation::Equal;
}
match (a.capability, b.capability) {
(Capability::Any, _) | (_, Capability::Any) => Relation::Unknown,
(Capability::Mcp(x), Capability::Mcp(y)) => mcp_relation(x, y),
(Capability::Mcp(_), Capability::Unknown(name))
| (Capability::Unknown(name), Capability::Mcp(_)) => {
if is_mcp_wire_token(name) {
Relation::Unknown
} else {
Relation::Disjoint
}
}
(Capability::Mcp(_), _) | (_, Capability::Mcp(_)) => Relation::Disjoint,
(Capability::Unknown(x), Capability::Unknown(y))
if x == y || (is_mcp_wire_token(x) && is_mcp_wire_token(y)) =>
{
Relation::Unknown
}
(Capability::Unknown(_), _) | (_, Capability::Unknown(_)) => Relation::Disjoint,
(Capability::Builtin(x), Capability::Builtin(y)) => builtin_relation(x, y, a, b),
}
}
fn builtin_relation(x: &str, y: &str, a: ToolKey<'_>, b: ToolKey<'_>) -> Relation {
if x == y {
return match (a.pattern, b.pattern) {
(None, Some(_)) => Relation::Covers,
(Some(_), None) => Relation::CoveredBy,
(Some(p), Some(q)) if x == "bash" => bash_relation(p, q),
(Some(p), Some(q)) if x == "skill" && is_plain(p) && is_plain(q) => Relation::Disjoint,
_ => Relation::Unknown,
};
}
if capability_covers(x, y) {
return if a.pattern.is_none() {
Relation::Covers
} else {
Relation::Unknown
};
}
if capability_covers(y, x) {
return if b.pattern.is_none() {
Relation::CoveredBy
} else {
Relation::Unknown
};
}
Relation::Disjoint
}
enum BashPattern<'a> {
Literal(&'a str),
Prefix(&'a str),
Other,
}
fn is_plain(text: &str) -> bool {
!text.contains(['*', '?', '\\', '\n', '\r'])
}
fn bash_pattern(pattern: &str) -> BashPattern<'_> {
let wildcards = pattern.matches('*').count();
let literal = pattern.strip_suffix('*').unwrap_or(pattern);
let plain = is_plain(literal);
match (wildcards, pattern.ends_with('*'), plain) {
(0, _, true) => BashPattern::Literal(pattern),
(1, true, true) => BashPattern::Prefix(literal),
_ => BashPattern::Other,
}
}
fn prefix_matches(prefix: &str, command: &str) -> bool {
command.starts_with(prefix) || prefix.strip_suffix(' ') == Some(command)
}
fn bash_relation(p: &str, q: &str) -> Relation {
match (bash_pattern(p), bash_pattern(q)) {
(BashPattern::Literal(_), BashPattern::Literal(_)) => Relation::Disjoint,
(BashPattern::Prefix(prefix), BashPattern::Literal(command)) => {
if prefix_matches(prefix, command) {
Relation::Covers
} else {
Relation::Disjoint
}
}
(BashPattern::Literal(command), BashPattern::Prefix(prefix)) => {
if prefix_matches(prefix, command) {
Relation::CoveredBy
} else {
Relation::Disjoint
}
}
(BashPattern::Prefix(x), BashPattern::Prefix(y)) => {
if y.starts_with(x) {
Relation::Covers
} else if x.starts_with(y) {
Relation::CoveredBy
} else {
Relation::Disjoint
}
}
_ => Relation::Unknown,
}
}
fn mcp_relation(a: &McpRef, b: &McpRef) -> Relation {
let server = segment_relation(&a.server, &b.server);
let tool = segment_relation(&a.tool, &b.tool);
match (server, tool) {
(Relation::Disjoint, _) | (_, Relation::Disjoint) => Relation::Disjoint,
(Relation::Equal, other) | (other, Relation::Equal) => other,
(x, y) if x == y => x,
_ => Relation::Unknown,
}
}
fn segment_relation(a: &McpSegment, b: &McpSegment) -> Relation {
match (a, b) {
(McpSegment::Any, McpSegment::Any) => Relation::Equal,
(McpSegment::Any, McpSegment::Named(_)) => Relation::Covers,
(McpSegment::Named(_), McpSegment::Any) => Relation::CoveredBy,
(McpSegment::Named(x), McpSegment::Named(y)) if x == y => Relation::Equal,
(McpSegment::Named(_), McpSegment::Named(_)) => Relation::Disjoint,
}
}
#[cfg(test)]
mod tests {
use super::Relation::*;
use super::*;
use crate::tool_names::parse_mars_tool_name;
fn rel(a: &str, b: &str) -> Relation {
let (a, b) = (
parse_mars_tool_name(a).unwrap(),
parse_mars_tool_name(b).unwrap(),
);
relation((&a).into(), (&b).into())
}
fn flipped(relation: Relation) -> Relation {
match relation {
Covers => CoveredBy,
CoveredBy => Covers,
other => other,
}
}
#[test]
fn relations() {
let rows: &[(&str, &str, Relation)] = &[
("bash", "shell", Equal),
("bash(git push:*)", "bash(git push *)", Equal),
("read(src/**)", "read(src/**)", Equal),
("mcp(github)", "mcp(github/*)", Equal),
("edit", "write", Covers),
("edit", "notebook", Covers),
("web", "web_search", Covers),
("web", "web_fetch", Covers),
("write", "notebook", Disjoint),
("web_search", "web_fetch", Disjoint),
("edit", "web", Disjoint),
("bash", "read", Disjoint),
("grep", "read", Disjoint),
("edit", "bash(git *)", Disjoint),
("skill(init)", "agent(init)", Disjoint),
("bash", "bash(git *)", Covers),
("read", "read(src/**)", Covers),
("edit", "write(src/**)", Covers),
("web", "web_fetch(domain:example.com)", Covers),
("write(src/**)", "edit", CoveredBy),
("edit(src/**)", "write", Unknown),
("edit(src/**)", "write(src/**)", Unknown),
("web_fetch(domain:a.com)", "web", CoveredBy),
("bash(git *)", "bash(git push *)", Covers),
("bash(git *)", "bash(git push)", Covers),
("bash(git *)", "bash(git)", Covers),
("bash(git *)", "bash(gitk)", Disjoint),
("bash(git *)", "bash(gitk *)", Disjoint),
("bash(git*)", "bash(gitk *)", Covers),
("bash(git*)", "bash(git *)", Covers),
("bash(git*)", "bash(git)", Covers),
("bash(*)", "bash(git push *)", Covers),
("bash(git push *)", "bash(git pull *)", Disjoint),
("bash(git push *)", "bash(git status)", Disjoint),
("bash(git status)", "bash(git status -s)", Disjoint),
("bash(git status)", "bash(git status)", Equal),
("bash(git push *)", "bash(git push -f *)", Covers),
("bash(git push -f *)", "bash(git push *)", CoveredBy),
("bash(pkill -f *)", "bash(pkill -f)", Covers),
("bash(git * main)", "bash(git push main)", Unknown),
("bash(git *)", "bash(git * main)", Unknown),
("bash(git **)", "bash(git push *)", Unknown),
("bash(*git)", "bash(git)", Unknown),
("bash(git ?*)", "bash(git a *)", Unknown),
("bash(git push?)", "bash(git push1)", Unknown),
("bash(git push?)", "bash(git push *)", Unknown),
("bash(git *)", "bash(git\\ push)", Unknown),
("bash(git\\ *)", "bash(git\\ push *)", Unknown),
("read(src/**)", "read(src/lib/**)", Unknown),
("read(./src/*)", "read(src/*)", Unknown),
("read(~/x)", "read(/home/x)", Unknown),
("read(src/a)", "read(src/b)", Unknown),
("write(src/**)", "write(docs/**)", Unknown),
("grep(src/**)", "grep(src/a/**)", Unknown),
("skill(init)", "skill(review)", Disjoint),
("skill(init)", "skill(re*)", Unknown),
(
"web_fetch(domain:a.com)",
"web_fetch(domain:b.com)",
Unknown,
),
("mcp(github)", "mcp(github/create_issue)", Covers),
("mcp(github/*)", "mcp(github/create_issue)", Covers),
("mcp(*/*)", "mcp(github)", Covers),
("mcp(*/*)", "mcp(github/create_issue)", Covers),
("mcp(*/create_issue)", "mcp(github/create_issue)", Covers),
(
"mcp(github/create_issue)",
"mcp(github/delete_repo)",
Disjoint,
),
("mcp(github)", "mcp(gitlab)", Disjoint),
("mcp(github)", "mcp(gitlab/x)", Disjoint),
("mcp(*/create_issue)", "mcp(github/*)", Unknown),
("mcp(github/Create)", "mcp(github/create)", Disjoint),
("mcp(github)", "bash", Disjoint),
("mcp(github)", "bash(git *)", Disjoint),
("mcp(*/*)", "edit", Disjoint),
("*", "bash", Unknown),
("*", "mcp(github)", Unknown),
("*", "*", Equal),
("mystery", "mystery", Equal),
("mystery", "mystery(x)", Unknown),
("mystery", "other", Disjoint),
("mystery", "bash", Disjoint),
("mystery", "mcp(github)", Disjoint),
("mcp__github__create_issue", "mcp(github)", Unknown),
(
"mcp__github__create_issue",
"mcp__github__create_issue",
Equal,
),
("mcp__github__a", "mcp__github__b", Unknown),
];
for (a, b, expected) in rows {
assert_eq!(rel(a, b), *expected, "{a} against {b}");
assert_eq!(rel(b, a), flipped(*expected), "{b} against {a}");
}
}
}