refactor(files): observe authorized edit targets by capability
This commit is contained in:
1 parent
436ba1617c
commit
f24383771d
5 files changed
+54
-55
No files matched your search
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
from typing import Any
|
||||
|
||||
from nanobot.agent.hook import (
|
||||
AgentHook,
|
||||
@@ -46,15 +46,14 @@ class FileEditActivityHook(AgentHook):
|
||||
tool: Any,
|
||||
params: Any,
|
||||
) -> None:
|
||||
if self._publish is None or not isinstance(params, dict):
|
||||
if self._publish is None:
|
||||
return
|
||||
typed_params = cast(dict[str, Any], params)
|
||||
trackers = prepare_file_edit_trackers(
|
||||
call_id=tool_call.id,
|
||||
tool_name=tool_call.name,
|
||||
tool=tool,
|
||||
workspace=self._workspace,
|
||||
params=typed_params,
|
||||
params=params,
|
||||
)
|
||||
if not trackers:
|
||||
return
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator, Mapping
|
||||
from typing import Any, cast
|
||||
|
||||
from nanobot.agent.tools.base import ToolResult, tool_parameters
|
||||
@@ -19,7 +20,9 @@ class _PatchError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _validate_patch_path(path: str) -> str:
|
||||
def _validate_patch_path(path: object) -> str:
|
||||
if not isinstance(path, str):
|
||||
raise _PatchError("path required for edit")
|
||||
normalized = path.strip()
|
||||
if not normalized:
|
||||
raise _PatchError("patch path cannot be empty")
|
||||
@@ -88,6 +91,17 @@ class ApplyPatchTool(_WritableFsTool):
|
||||
"Use edit_file only for small exact replacements on a single file."
|
||||
)
|
||||
|
||||
def _file_edit_paths(self, params: Mapping[str, object]) -> Iterator[str]:
|
||||
edits = params.get("edits")
|
||||
if params.get("dry_run") is True or not isinstance(edits, list):
|
||||
return
|
||||
for edit in cast(list[object], edits):
|
||||
if isinstance(edit, dict):
|
||||
try:
|
||||
yield _validate_patch_path(cast(dict[str, object], edit).get("path"))
|
||||
except _PatchError:
|
||||
continue
|
||||
|
||||
async def execute(
|
||||
self,
|
||||
edits: list[object] | None = None,
|
||||
@@ -106,14 +120,11 @@ class ApplyPatchTool(_WritableFsTool):
|
||||
if not isinstance(edit_value, dict):
|
||||
raise _PatchError("each edit must be an object")
|
||||
edit = cast(dict[str, Any], edit_value)
|
||||
raw_path = edit.get("path")
|
||||
if not isinstance(raw_path, str):
|
||||
raise _PatchError("path required for edit")
|
||||
path = _validate_patch_path(raw_path)
|
||||
path = _validate_patch_path(edit.get("path"))
|
||||
action = edit.get("action")
|
||||
if not isinstance(action, str):
|
||||
raise _PatchError(f"action required for edit: {path}")
|
||||
source = self.file_edit_target(path)
|
||||
source = self._files.edit_target(path)
|
||||
|
||||
if action == "add":
|
||||
new_text = edit.get("new_text")
|
||||
|
||||
@@ -4,6 +4,7 @@ import codecs
|
||||
import difflib
|
||||
import hashlib
|
||||
import mimetypes
|
||||
from collections.abc import Iterator, Mapping
|
||||
from copy import deepcopy
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
@@ -100,8 +101,17 @@ class _FsTool(Tool):
|
||||
class _WritableFsTool(_FsTool):
|
||||
"""Expose the same write target to execution and edit activity tracking."""
|
||||
|
||||
def file_edit_target(self, path: str) -> WritableFile:
|
||||
return self._files.edit_target(path)
|
||||
def _file_edit_paths(self, params: Mapping[str, object]) -> Iterator[str]:
|
||||
path = params.get("path")
|
||||
if isinstance(path, str) and path.strip():
|
||||
yield path
|
||||
|
||||
def file_edit_targets(self, params: Mapping[str, object]) -> Iterator[WritableFile]:
|
||||
for path in self._file_edit_paths(params):
|
||||
try:
|
||||
yield self._files.edit_target(path)
|
||||
except (OSError, RuntimeError, ValueError):
|
||||
continue # Execution reports the error; activity must not read this target.
|
||||
|
||||
@property
|
||||
def file_edit_workspace(self) -> Path | None:
|
||||
@@ -598,7 +608,7 @@ class WriteFileTool(_WritableFsTool):
|
||||
raise ValueError("Unknown path")
|
||||
if content is None:
|
||||
raise ValueError("Unknown content")
|
||||
target = self.file_edit_target(path)
|
||||
target = self._files.edit_target(path)
|
||||
target.write(content, create_parents=True)
|
||||
self._file_states.record_write(target.path)
|
||||
return f"Successfully wrote {len(content)} characters to {target.path}"
|
||||
@@ -950,7 +960,7 @@ class EditFileTool(_WritableFsTool):
|
||||
if expected_replacements is not None and expected_replacements < 1:
|
||||
return ToolResult.error("Error: expected_replacements must be >= 1.")
|
||||
|
||||
target = self.file_edit_target(path)
|
||||
target = self._files.edit_target(path)
|
||||
fp = target.path
|
||||
file_exists = target.exists()
|
||||
if file_exists and old_text == new_text:
|
||||
|
||||
Reference in new issue
Block a user