use serde_json::{json, Value};
use crate::protocol::Response;
pub const TOOL_DISABLED_CODE: &str = "tool_disabled";
pub fn canonical_tool_name(call_name: &str) -> Option<&'static str> {
let bare = call_name.strip_prefix("aft_").unwrap_or(call_name);
Some(match bare {
"read" => "read",
"write" => "write",
"edit" => "edit",
"apply_patch" => "apply_patch",
"grep" => "grep",
"glob" => "glob",
"bash" => "bash",
"powershell" => "powershell",
"bash_status" => "bash_status",
"bash_kill" => "bash_kill",
"bash_write" => "bash_write",
"bash_watch" => "bash_watch",
"ast_search" | "ast_grep_search" => "ast_grep_search",
"ast_replace" | "ast_grep_replace" => "ast_grep_replace",
"callgraph" => "aft_callgraph",
"conflicts" => "aft_conflicts",
"delete" => "aft_delete",
"import" => "aft_import",
"inspect" => "aft_inspect",
"move" => "aft_move",
"outline" => "aft_outline",
"safety" => "aft_safety",
"search" => "aft_search",
"zoom" => "aft_zoom",
_ => return None,
})
}
pub fn gating_name(call_name: &str, arguments: &Value) -> Option<&'static str> {
let canonical = canonical_tool_name(call_name)?;
let bare = call_name.strip_prefix("aft_").unwrap_or(call_name);
if crate::host_tool_names::is_native_plumbing_call(bare, arguments) {
return None;
}
Some(canonical)
}
pub fn refusal(
request_id: &str,
call_name: &str,
arguments: &Value,
disabled: &[String],
) -> Option<Response> {
let tool = gating_name(call_name, arguments)?;
refusal_for_tool(request_id, call_name, tool, disabled)
}
pub fn refusal_by_name(request_id: &str, call_name: &str, disabled: &[String]) -> Option<Response> {
refusal_for_tool(
request_id,
call_name,
canonical_tool_name(call_name)?,
disabled,
)
}
fn refusal_for_tool(
request_id: &str,
call_name: &str,
tool: &str,
disabled: &[String],
) -> Option<Response> {
if !disabled.iter().any(|name| name == tool) {
return None;
}
let called_as = if call_name == tool {
String::new()
} else {
format!(" (called as `{call_name}`)")
};
let message = format!(
"{TOOL_DISABLED_CODE} {tool}: `{tool}`{called_as} is disabled by `disabled_tools` in aft.jsonc. \
To enable it, remove \"{tool}\" from `disabled_tools` in ~/.config/cortexkit/aft.jsonc \
(or the project's .cortexkit/aft.jsonc), or run `npx @cortexkit/aft setup`, then restart \
the host so the session reconnects."
);
Some(Response::error_with_data(
request_id,
TOOL_DISABLED_CODE,
message,
json!({ "tool": tool }),
))
}
pub fn catalog_keeps(catalog_name: &str, disabled: &[String]) -> bool {
canonical_tool_name(catalog_name).is_none_or(|tool| !disabled.iter().any(|name| name == tool))
}
#[cfg(test)]
mod tests {
use super::*;
fn disabled(names: &[&str]) -> Vec<String> {
names.iter().map(|name| (*name).to_string()).collect()
}
#[test]
fn every_catalog_and_legacy_name_maps_to_a_canonical_tool() {
for (call, canonical) in [
("read", "read"),
("aft_read", "read"),
("write", "write"),
("aft_write", "write"),
("edit", "edit"),
("aft_edit", "edit"),
("apply_patch", "apply_patch"),
("aft_apply_patch", "apply_patch"),
("grep", "grep"),
("aft_grep", "grep"),
("glob", "glob"),
("aft_glob", "glob"),
("bash", "bash"),
("aft_bash", "bash"),
("delete", "aft_delete"),
("aft_delete", "aft_delete"),
("move", "aft_move"),
("aft_move", "aft_move"),
("inspect", "aft_inspect"),
("aft_inspect", "aft_inspect"),
("ast_search", "ast_grep_search"),
("ast_grep_search", "ast_grep_search"),
("ast_replace", "ast_grep_replace"),
("outline", "aft_outline"),
("zoom", "aft_zoom"),
("search", "aft_search"),
("safety", "aft_safety"),
("import", "aft_import"),
("callgraph", "aft_callgraph"),
("conflicts", "aft_conflicts"),
("bash_status", "bash_status"),
("bash_kill", "bash_kill"),
("bash_write", "bash_write"),
] {
assert_eq!(canonical_tool_name(call), Some(canonical), "{call}");
assert!(
crate::feature_config::is_known_tool(canonical),
"{canonical} must be a canonical tool name"
);
}
for (alias, canonical) in crate::feature_config::LEGACY_TOOL_ALIASES {
assert_eq!(canonical_tool_name(alias), Some(canonical));
}
for canonical in crate::feature_config::CANONICAL_TOOLS {
assert_eq!(canonical_tool_name(canonical), Some(canonical));
}
}
#[test]
fn plumbing_is_never_gated() {
for name in [
"bash_drain_completions",
"bash_ack_completions",
"undo_preview",
"checkpoint_paths",
"safety_purge_paths",
"hashline_preflight",
"inspect_tier2_run",
"status",
] {
assert_eq!(gating_name(name, &json!({})), None, "{name}");
}
assert_eq!(gating_name("bash_status", &json!({ "task_id": "t" })), None);
assert_eq!(
gating_name("bash_status", &json!({ "taskId": "t" })),
Some("bash_status")
);
}
#[test]
fn bash_and_powershell_are_independent() {
let args = json!({ "command": "true" });
assert!(refusal("r", "powershell", &args, &disabled(&["bash"])).is_none());
assert!(refusal("r", "bash", &args, &disabled(&["powershell"])).is_none());
assert!(refusal("r", "powershell", &args, &disabled(&["powershell"])).is_some());
assert!(refusal(
"r",
"bash_kill",
&json!({ "taskId": "t" }),
&disabled(&["bash"])
)
.is_none());
assert!(catalog_keeps("powershell", &disabled(&["bash"])));
assert!(!catalog_keeps("bash", &disabled(&["bash"])));
}
#[test]
fn refusal_is_named_and_says_how_to_enable() {
let refusal = refusal(
"r1",
"aft_delete",
&json!({ "files": ["x"] }),
&disabled(&["aft_delete"]),
)
.expect("disabled tool is refused");
assert!(!refusal.success);
assert_eq!(refusal.data["code"], TOOL_DISABLED_CODE);
assert_eq!(refusal.data["tool"], "aft_delete");
let message = refusal.data["message"].as_str().unwrap();
assert!(
message.starts_with("tool_disabled aft_delete:"),
"{message}"
);
assert!(message.contains("aft.jsonc"));
assert!(message.contains("remove \"aft_delete\" from `disabled_tools`"));
assert!(message.contains("npx @cortexkit/aft setup"));
}
#[test]
fn catalog_filter_drops_disabled_names_only() {
let off = disabled(&["aft_delete", "read"]);
assert!(!catalog_keeps("delete", &off));
assert!(!catalog_keeps("read", &off));
assert!(catalog_keeps("move", &off));
assert!(catalog_keeps("status", &off));
}
}