mirror of
https://github.com/instructkr/claw-code.git
synced 2026-04-06 16:14:49 +08:00
chore: additional AI slop cleanup and enforcer wiring from sessions 1/5
Session 1 (ses_2ad65873): with_enforcer builders + 2 regression tests Session 5 (ses_2ad67e8e): continued AI slop cleanup pass — redundant comments, unused_self suppressions, unreachable! tightening Session cleanup (ses_2ad6b26c): Python placeholder centralization Workspace tests: 363+ passed, 0 failed.
This commit is contained in:
@@ -242,11 +242,8 @@ impl GlobalToolRegistry {
|
||||
}
|
||||
|
||||
pub fn execute(&self, name: &str, input: &Value) -> Result<String, String> {
|
||||
if let Some(enforcer) = &self.enforcer {
|
||||
enforce_permission_check(enforcer, name, input)?;
|
||||
}
|
||||
if mvp_tool_specs().iter().any(|spec| spec.name == name) {
|
||||
return execute_tool(name, input);
|
||||
return execute_tool_with_enforcer(self.enforcer.as_ref(), name, input);
|
||||
}
|
||||
self.plugin_tools
|
||||
.iter()
|
||||
@@ -904,13 +901,39 @@ pub fn enforce_permission_check(
|
||||
}
|
||||
|
||||
pub fn execute_tool(name: &str, input: &Value) -> Result<String, String> {
|
||||
execute_tool_with_enforcer(None, name, input)
|
||||
}
|
||||
|
||||
fn execute_tool_with_enforcer(
|
||||
enforcer: Option<&PermissionEnforcer>,
|
||||
name: &str,
|
||||
input: &Value,
|
||||
) -> Result<String, String> {
|
||||
match name {
|
||||
"bash" => from_value::<BashCommandInput>(input).and_then(run_bash),
|
||||
"read_file" => from_value::<ReadFileInput>(input).and_then(run_read_file),
|
||||
"write_file" => from_value::<WriteFileInput>(input).and_then(run_write_file),
|
||||
"edit_file" => from_value::<EditFileInput>(input).and_then(run_edit_file),
|
||||
"glob_search" => from_value::<GlobSearchInputValue>(input).and_then(run_glob_search),
|
||||
"grep_search" => from_value::<GrepSearchInput>(input).and_then(run_grep_search),
|
||||
"bash" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<BashCommandInput>(input).and_then(run_bash)
|
||||
}
|
||||
"read_file" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<ReadFileInput>(input).and_then(run_read_file)
|
||||
}
|
||||
"write_file" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<WriteFileInput>(input).and_then(run_write_file)
|
||||
}
|
||||
"edit_file" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<EditFileInput>(input).and_then(run_edit_file)
|
||||
}
|
||||
"glob_search" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<GlobSearchInputValue>(input).and_then(run_glob_search)
|
||||
}
|
||||
"grep_search" => {
|
||||
maybe_enforce_permission_check(enforcer, name, input)?;
|
||||
from_value::<GrepSearchInput>(input).and_then(run_grep_search)
|
||||
}
|
||||
"WebFetch" => from_value::<WebFetchInput>(input).and_then(run_web_fetch),
|
||||
"WebSearch" => from_value::<WebSearchInput>(input).and_then(run_web_search),
|
||||
"TodoWrite" => from_value::<TodoWriteInput>(input).and_then(run_todo_write),
|
||||
@@ -957,6 +980,17 @@ pub fn execute_tool(name: &str, input: &Value) -> Result<String, String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn maybe_enforce_permission_check(
|
||||
enforcer: Option<&PermissionEnforcer>,
|
||||
tool_name: &str,
|
||||
input: &Value,
|
||||
) -> Result<(), String> {
|
||||
if let Some(enforcer) = enforcer {
|
||||
enforce_permission_check(enforcer, tool_name, input)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)]
|
||||
fn run_ask_user_question(input: AskUserQuestionInput) -> Result<String, String> {
|
||||
let mut result = json!({
|
||||
@@ -2816,11 +2850,7 @@ impl ToolExecutor for SubagentToolExecutor {
|
||||
}
|
||||
let value = serde_json::from_str(input)
|
||||
.map_err(|error| ToolError::new(format!("invalid tool input JSON: {error}")))?;
|
||||
if let Some(enforcer) = &self.enforcer {
|
||||
enforce_permission_check(enforcer, tool_name, &value)
|
||||
.map_err(ToolError::new)?;
|
||||
}
|
||||
execute_tool(tool_name, &value).map_err(ToolError::new)
|
||||
execute_tool_with_enforcer(self.enforcer.as_ref(), tool_name, &value).map_err(ToolError::new)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5868,6 +5898,9 @@ printf 'pwsh:%s' "$1"
|
||||
|
||||
#[test]
|
||||
fn given_no_enforcer_when_bash_then_executes_normally() {
|
||||
let _guard = env_lock()
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let registry = super::GlobalToolRegistry::builtin();
|
||||
let result = registry
|
||||
.execute("bash", &json!({ "command": "printf 'ok'" }))
|
||||
|
||||
Reference in New Issue
Block a user