use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const DEFAULT_SHELL_TOOL: &str = "bash";
pub const DEFAULT_PATCH_TOOL: &str = "apply_patch";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HostToolNames {
pub bash: String,
pub apply_patch: String,
}
impl Default for HostToolNames {
fn default() -> Self {
Self {
bash: DEFAULT_SHELL_TOOL.to_string(),
apply_patch: DEFAULT_PATCH_TOOL.to_string(),
}
}
}
fn valid_tool_name(name: &str) -> bool {
let mut chars = name.chars();
let Some(first) = chars.next() else {
return false;
};
name.len() <= 64
&& first.is_ascii_alphabetic()
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}
pub const HOST_TOOL_NAMES_FIELD: &str = "host_tool_names";
impl HostToolNames {
pub fn from_request_field(value: Option<&Value>) -> Result<Self, String> {
let mut names = Self::default();
let Some(value) = value else {
return Ok(names);
};
let Some(object) = value.as_object() else {
return Err("host_tool_names must be an object".to_string());
};
for (key, entry) in object {
let slot = match key.as_str() {
"bash" => &mut names.bash,
"apply_patch" => &mut names.apply_patch,
other => {
return Err(format!(
"host_tool_names has unknown key `{other}`; expected `bash` or `apply_patch`"
));
}
};
let Some(name) = entry.as_str() else {
return Err(format!("host_tool_names.{key} must be a string"));
};
if !valid_tool_name(name) {
return Err(format!(
"host_tool_names.{key} is not a valid tool name: {name:?}"
));
}
*slot = name.to_string();
}
Ok(names)
}
pub fn shell(&self) -> &str {
&self.bash
}
pub fn patch(&self) -> &str {
&self.apply_patch
}
pub fn name_patch_tool_in(&self, text: &str) -> String {
if self.apply_patch == DEFAULT_PATCH_TOOL {
return text.to_string();
}
text.replace(
&format!("use {DEFAULT_PATCH_TOOL}"),
&format!("use {}", self.apply_patch),
)
}
}
pub fn supports_tool(bare_name: &str) -> bool {
matches!(
bare_name,
"bash"
| "powershell"
| "status"
| "read"
| "write"
| "edit"
| "apply_patch"
| "grep"
| "glob"
| "search"
| "outline"
| "zoom"
| "inspect"
| "callgraph"
| "conflicts"
| "ast_search"
| "ast_replace"
| "delete"
| "move"
| "import"
| "safety"
| "bash_status"
| "bash_kill"
| "bash_write"
| "bash_watch"
)
}
pub fn is_subc_native_plumbing_tool(name: &str) -> bool {
matches!(
name,
"bash_abort_inflight"
| "bash_status"
| "bash_drain_completions"
| "bash_ack_completions"
| "undo_preview"
| "checkpoint_paths"
| "safety_purge_paths"
| "bash_kill"
| "bash_write"
| "bash_notify"
| "bash_unnotify"
| "bash_wait_detach"
| "bash_regex_match"
| "bash_artifact_owned"
| "bash_owned_tasks"
| "inspect_tier2_run"
| "hashline_preflight"
)
}
pub fn is_native_plumbing_call(name: &str, arguments: &Value) -> bool {
if !is_subc_native_plumbing_tool(name) {
return false;
}
if !supports_tool(name) {
return true;
}
let has_catalog_task_id = |value: &Value| value.get("taskId").is_some();
let agent_spelled =
has_catalog_task_id(arguments) || arguments.get("params").is_some_and(has_catalog_task_id);
!agent_spelled
}
use subc_protocol::manifest::ExecutionMode;
pub fn tool_execution_mode(name: &str) -> ExecutionMode {
match crate::tool_gate::canonical_tool_name(name).unwrap_or(name) {
"bash" | "powershell" | "write" | "edit" | "apply_patch" | "ast_grep_replace"
| "aft_delete" | "aft_move" | "aft_import" | "aft_safety" | "bash_kill" | "bash_write" => {
ExecutionMode::Mutating
}
_ => ExecutionMode::Pure,
}
}
pub fn tool_call_is_mutating(name: &str, args: &Value) -> bool {
match crate::tool_gate::canonical_tool_name(name) {
Some("aft_safety") if matches!(args["op"].as_str(), Some("history" | "list")) => false,
Some("ast_grep_replace") if args["dryRun"].as_bool() == Some(true) => false,
_ => tool_execution_mode(name) == ExecutionMode::Mutating,
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn absent_parameter_keeps_the_default_names() {
let names = HostToolNames::from_request_field(None).unwrap();
assert_eq!(names.shell(), "bash");
assert_eq!(names.patch(), "apply_patch");
}
#[test]
fn opencode_2_names_are_accepted() {
let names = HostToolNames::from_request_field(Some(
&json!({"bash": "shell", "apply_patch": "patch"}),
))
.unwrap();
assert_eq!(names.shell(), "shell");
assert_eq!(names.patch(), "patch");
}
#[test]
fn a_missing_key_keeps_that_tools_default() {
let names = HostToolNames::from_request_field(Some(&json!({"bash": "shell"}))).unwrap();
assert_eq!(names.shell(), "shell");
assert_eq!(names.patch(), "apply_patch");
}
#[test]
fn malformed_parameters_are_refused() {
for bad in [
json!("shell"),
json!({"shell": "shell"}),
json!({"bash": 1}),
json!({"bash": ""}),
json!({"bash": "has space"}),
json!({"bash": "9lives"}),
] {
assert!(
HostToolNames::from_request_field(Some(&bad)).is_err(),
"{bad} should be refused"
);
}
}
fn request(params: serde_json::Value) -> crate::protocol::RawRequest {
serde_json::from_value(params).expect("raw request")
}
#[test]
fn a_request_reads_its_own_envelope_field() {
let names = json!({"bash": "shell", "apply_patch": "patch"});
let top = request(json!({"id": "1", "command": "bash", "host_tool_names": names}));
assert_eq!(top.host_tool_names().shell(), "shell");
assert!(top.sent_host_tool_names().is_some());
let nested =
request(json!({"id": "2", "command": "bash", "params": {"host_tool_names": names}}));
assert_eq!(nested.host_tool_names().patch(), "patch");
let absent = request(json!({"id": "3", "command": "bash"}));
assert_eq!(absent.host_tool_names(), HostToolNames::default());
assert!(absent.sent_host_tool_names().is_none());
let bad = request(json!({"id": "4", "command": "bash", "host_tool_names": "shell"}));
assert_eq!(bad.host_tool_names(), HostToolNames::default());
}
#[test]
fn patch_steering_is_renamed_only_for_a_non_default_name() {
let steer = "enable backups or use apply_patch for this destructive change";
assert_eq!(HostToolNames::default().name_patch_tool_in(steer), steer);
let v2 = HostToolNames {
bash: "shell".into(),
apply_patch: "patch".into(),
};
assert_eq!(
v2.name_patch_tool_in(steer),
"enable backups or use patch for this destructive change"
);
}
}