统一公共工具并移除 workspace 专家链路。

将文件/命令/Git/检索能力统一迁入 public,去除重复工具与 local_ 命名前缀,并重命名底层路径守卫模块为 path_guard 以提升语义清晰度。

Made-with: Cursor
This commit is contained in:
oliver 2026-05-01 00:38:31 +08:00
parent 75aec2abe4
commit d0a66d190d
21 changed files with 326 additions and 1012 deletions

View file

@ -5,10 +5,10 @@ from pathlib import Path
from oclaw.runtime.tools.catalog import default_registry
from oclaw.runtime.tools.local_sdk.adapter import LocalAdapter
from oclaw.runtime.tools.public.local_edit_file_tool import local_edit_file_tool
from oclaw.runtime.tools.public.local_read_file_tool import local_read_file_tool
from oclaw.runtime.tools.public.local_run_command_tool import local_run_command_tool
from oclaw.runtime.tools.public.local_write_file_tool import local_write_file_tool
from oclaw.runtime.tools.public.edit_file_tool import edit_file_tool
from oclaw.runtime.tools.public.run_command_tool import run_command_tool
from oclaw.runtime.tools.public.read_file_tool import read_file_tool
from oclaw.runtime.tools.public.write_file_tool import write_file_tool
from oclaw.runtime.tools.public_registry import clear_public_tool_cache
from oclaw.runtime.tools.public.list_directory_tool import list_directory_tool
from oclaw.runtime.tools.public.search_files_tool import search_files_tool
@ -21,15 +21,15 @@ from oclaw.runtime.tools.public.set_env_tool import set_env_tool
def test_local_public_read_tool_visible_by_default() -> None:
clear_public_tool_cache()
names = [t.name for t in default_registry(expert="network_ops+memory", specialist="ops").list()]
assert "local_read_file" in names
assert "read_file" in names
assert "list_directory" in names
assert "search_files" in names
assert "get_cwd" in names
assert "get_env" in names
assert "list_processes" in names
assert "local_run_command" not in names
assert "local_write_file" not in names
assert "local_edit_file" not in names
assert "run_command" not in names
assert "write_file" not in names
assert "edit_file" not in names
assert "mkdir" not in names
assert "delete_file" not in names
assert "move_file" not in names
@ -42,15 +42,15 @@ def test_local_public_high_risk_tools_visible_when_enabled(monkeypatch) -> None:
clear_public_tool_cache()
monkeypatch.setenv("AIA_PUBLIC_TOOLS_ALLOW_HIGH", "1")
names = [t.name for t in default_registry(expert="network_ops+memory", specialist="ops").list()]
assert "local_read_file" in names
assert "read_file" in names
assert "list_directory" in names
assert "search_files" in names
assert "get_cwd" in names
assert "get_env" in names
assert "list_processes" in names
assert "local_run_command" in names
assert "local_write_file" in names
assert "local_edit_file" in names
assert "run_command" in names
assert "write_file" in names
assert "edit_file" in names
assert "mkdir" in names
assert "delete_file" in names
assert "move_file" in names
@ -71,13 +71,13 @@ def test_local_adapter_backend_roundtrip(tmp_path: Path, monkeypatch) -> None:
assert "hi" in str(out2.get("stdout") or "").lower()
def test_local_run_command_tool_handler(monkeypatch) -> None:
def test_run_command_tool_handler(monkeypatch) -> None:
class _Adapter:
def run_command(self, *, command: str, cwd: str | None = None, timeout: int = 30):
return {"ok": True, "command": command, "cwd": cwd, "timeout": timeout}
monkeypatch.setattr("oclaw.runtime.tools.public.local_run_command_tool.get_local_adapter", lambda: _Adapter())
spec = local_run_command_tool()
monkeypatch.setattr("oclaw.runtime.tools.public.run_command_tool.get_local_adapter", lambda: _Adapter())
spec = run_command_tool()
out = spec.handler({"command": "echo hi", "cwd": "repo", "timeout": 9})
assert out.get("ok") is True
assert out.get("command") == "echo hi"
@ -85,35 +85,7 @@ def test_local_run_command_tool_handler(monkeypatch) -> None:
assert out.get("timeout") == 9
def test_local_read_file_tool_handler(monkeypatch) -> None:
class _Adapter:
def read_file(self, *, path: str, start_line: int | None = None, end_line: int | None = None):
return {"ok": True, "path": path, "start_line": start_line, "end_line": end_line}
monkeypatch.setattr("oclaw.runtime.tools.public.local_read_file_tool.get_local_adapter", lambda: _Adapter())
spec = local_read_file_tool()
out = spec.handler({"path": "a.py", "start_line": 2, "end_line": 5})
assert out.get("ok") is True
assert out.get("path") == "a.py"
assert out.get("start_line") == 2
assert out.get("end_line") == 5
def test_local_write_file_tool_handler(monkeypatch) -> None:
class _Adapter:
def write_file(self, *, path: str, content: str, mode: str = "overwrite"):
return {"ok": True, "path": path, "content": content, "mode": mode}
monkeypatch.setattr("oclaw.runtime.tools.public.local_write_file_tool.get_local_adapter", lambda: _Adapter())
spec = local_write_file_tool()
out = spec.handler({"path": "a.py", "content": "x=1", "mode": "append"})
assert out.get("ok") is True
assert out.get("path") == "a.py"
assert out.get("content") == "x=1"
assert out.get("mode") == "append"
def test_local_edit_file_tool_handler(monkeypatch) -> None:
def test_edit_file_tool_handler(monkeypatch) -> None:
class _Adapter:
def edit_file(
self,
@ -135,8 +107,8 @@ def test_local_edit_file_tool_handler(monkeypatch) -> None:
"replacement": replacement,
}
monkeypatch.setattr("oclaw.runtime.tools.public.local_edit_file_tool.get_local_adapter", lambda: _Adapter())
spec = local_edit_file_tool()
monkeypatch.setattr("oclaw.runtime.tools.public.edit_file_tool.get_local_adapter", lambda: _Adapter())
spec = edit_file_tool()
out = spec.handler({"path": "a.py", "search": "foo", "replace": "bar"})
assert out.get("ok") is True
assert out.get("path") == "a.py"
@ -145,26 +117,26 @@ def test_local_edit_file_tool_handler(monkeypatch) -> None:
def test_local_tool_integration_roundtrip(monkeypatch) -> None:
run_spec = local_run_command_tool()
read_spec = local_read_file_tool()
write_spec = local_write_file_tool()
edit_spec = local_edit_file_tool()
run_spec = run_command_tool()
read_spec = read_file_tool()
write_spec = write_file_tool()
edit_spec = edit_file_tool()
tmpdir = Path(tempfile.mkdtemp(prefix="local_it_"))
monkeypatch.setenv("OPS_WORKSPACE_ROOT", str(tmpdir))
target = tmpdir / "it_sample.txt"
target_rel = "data/workspace/it_sample.txt"
out_write = write_spec.handler({"path": str(target), "content": "line1\nline2\n", "mode": "overwrite"})
out_write = write_spec.handler({"path": "it_sample.txt", "content": "line1\nline2\n", "mode": "overwrite"})
assert out_write.get("ok") is True, out_write
out_read_before = read_spec.handler({"path": str(target), "start_line": 1, "end_line": 10})
out_read_before = read_spec.handler({"path": target_rel, "offset": 1, "limit": 10})
assert out_read_before.get("ok") is True, out_read_before
assert "line2" in str(out_read_before.get("content") or "")
out_edit = edit_spec.handler({"path": str(target), "search": "line2", "replace": "line2_edited"})
out_edit = edit_spec.handler({"path": target_rel, "search": "line2", "replace": "line2_edited"})
assert out_edit.get("ok") is True, out_edit
out_read_after = read_spec.handler({"path": str(target), "start_line": 1, "end_line": 10})
out_read_after = read_spec.handler({"path": target_rel, "offset": 1, "limit": 10})
assert out_read_after.get("ok") is True, out_read_after
assert "line2_edited" in str(out_read_after.get("content") or "")
@ -203,3 +175,21 @@ def test_p1_p2_read_tools_smoke(tmp_path: Path, monkeypatch) -> None:
assert out_get1.get("ok") is True
assert out_get1.get("value") == "y"
def test_run_command_does_not_follow_cd_state(tmp_path: Path, monkeypatch) -> None:
monkeypatch.setenv("OPS_WORKSPACE_ROOT", str(tmp_path))
(tmp_path / "subdir").mkdir(parents=True, exist_ok=True)
(tmp_path / "subdir" / "echo_dir.py").write_text(
"import os\nprint(os.path.basename(os.getcwd()))\n",
encoding="utf-8",
)
adapter = LocalAdapter()
out_cd = adapter.cd(cwd="subdir")
assert out_cd.get("ok") is True
out_run = adapter.run_command(command='python -c "import os; print(os.path.basename(os.getcwd()))"', timeout=20)
assert out_run.get("ok") is True, out_run
# If run_command follows cd state this would be "subdir"; we expect workspace root name instead.
assert str(out_run.get("cwd") or "").replace("\\", "/").rstrip("/").endswith(str(tmp_path.name))