Agent Zero AI framework
text_editor: route file I/O through RFC in development
linuztxca65fb9a5238
diff --git a/plugins/text_editor/helpers/file_ops.py b/plugins/text_editor/helpers/file_ops.pyindex a5b5b8b95..a1dc959b9 100644--- a/plugins/text_editor/helpers/file_ops.py+++ b/plugins/text_editor/helpers/file_ops.py@@ -1,31 +1,46 @@"""-Pure file operations for the text_editor plugin.--No agent/tool dependencies — only stdlib + tokens helper.+File operations for the text_editor plugin.++Split into two layers:+ - _impl functions in python/helpers/rfc_text_editor.py: pure filesystem+ I/O with only stdlib deps. These run inside the Docker container+ when routed via RFC.+ - Public async functions here: orchestrate _impl calls through+ runtime.call_development_function and apply token budgeting+ (which requires tiktoken, a framework-only dependency)."""-import os-import shutil-import tempfilefrom dataclasses import dataclass-from python.helpers import tokens--_BINARY_PEEK = 8192-+from python.helpers import tokens, runtime+from python.helpers.rfc_text_editor import (+ is_binary_impl as _is_binary_impl,+ read_file_raw_impl as _read_file_raw_impl,+ write_file_impl as _write_file_impl,+ apply_patch_impl as _apply_patch_impl,+ file_info_impl as _file_info_impl,+)# ------------------------------------------------------------------# Binary detection# -------------------------------------------------------------------def is_binary(path: str) -> bool:+async def is_binary(path: str) -> bool:"""Detect binary file by checking for null bytes."""- try:- with open(path, "rb") as f:- chunk = f.read(_BINARY_PEEK)- return b"\x00" in chunk- except OSError:- return False+ return await runtime.call_development_function(_is_binary_impl, path)+++# ------------------------------------------------------------------+# File metadata+# ------------------------------------------------------------------++async def file_info(path: str) -> dict:+ """+ Get file metadata from the container.++ Returns dict with: exists, is_file, realpath, expanded, mtime.+ """+ return await runtime.call_development_function(_file_info_impl, path)# ------------------------------------------------------------------@@ -40,7 +55,7 @@ class ReadResult:error: str = ""-def read_file(+async def read_file(path: str,line_from: int = 1,line_to: int | None = None,@@ -55,21 +70,15 @@ def read_file(line_from and line_to are both inclusive.None line_to defaults to line_from + default_line_count - 1."""- path = os.path.expanduser(path)-- if not os.path.isfile(path):- return ReadResult(error="file not found")+ # I/O happens in the container via RFC; token budgeting stays host-side.+ raw = await runtime.call_development_function(_read_file_raw_impl, path)- if is_binary(path):- return ReadResult(error="file appears binary, use terminal instead")+ if raw["error"]:+ return ReadResult(error=raw["error"])- try:- with open(path, "r", encoding="utf-8", errors="replace") as f:- all_lines = f.readlines()- except OSError as exc:- return ReadResult(error=str(exc))+ all_lines = raw["lines"]+ total_lines = raw["total_lines"]- total_lines = len(all_lines)line_from = max(line_from, 1)if line_to is None:line_to = line_from + default_line_count - 1@@ -139,22 +148,16 @@ class WriteResult:error: str = ""-def write_file(path: str, content: str | None) -> WriteResult:+async def write_file(path: str, content: str | None) -> WriteResult:"""Create or overwrite a file."""if content is None:content = ""- path = os.path.expanduser(path)- try:- os.makedirs(os.path.dirname(path) or ".", exist_ok=True)- with open(path, "w", encoding="utf-8") as f:- f.write(content)- except OSError as exc:- return WriteResult(error=str(exc))-- total = content.count("\n") + (- 1 if content and not content.endswith("\n") else 0+ result = await runtime.call_development_function(+ _write_file_impl, path, content)- return WriteResult(total_lines=total)+ if result["error"]:+ return WriteResult(error=result["error"])+ return WriteResult(total_lines=result["total_lines"])# ------------------------------------------------------------------@@ -221,7 +224,7 @@ def validate_edits(edits: list | None) -> tuple[list[dict], str]:return parsed, ""-def apply_patch(path: str, edits: list[dict]) -> int:+async def apply_patch(path: str, edits: list[dict]) -> int:"""Apply sorted, validated edits by streaming to a temp file.@@ -229,75 +232,20 @@ def apply_patch(path: str, edits: list[dict]) -> int:Inserts have 'insert': True.Returns total line count after patching."""- # Ensure content always ends with newline to prevent line merging- for e in edits:- if e["content"] and not e["content"].endswith("\n"):- e["content"] += "\n"+ result = await runtime.call_development_function(+ _apply_patch_impl, path, edits+ )+ if result["error"]:+ raise Exception(result["error"])+ return result["total_lines"]- dir_name = os.path.dirname(path) or "."- fd, tmp_path = tempfile.mkstemp(dir=dir_name, suffix=".tmp")- try:- with (- open(path, "r", encoding="utf-8", errors="replace") as src,- os.fdopen(fd, "w", encoding="utf-8") as dst,- ):- edit_idx = 0- line_no = 1 # 1-based- total_written = 0-- for raw_line in src:- # Process all inserts targeting this line first- while (- edit_idx < len(edits)- and edits[edit_idx]["insert"]- and edits[edit_idx]["from"] == line_no- ):- edit = edits[edit_idx]- if edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(edit["content"])- edit_idx += 1-- # Check if current line falls in a replace/delete range- if edit_idx < len(edits) and not edits[edit_idx]["insert"]:- edit = edits[edit_idx]- if edit["from"] <= line_no <= edit["to"]:- # Write replacement content once at range start- if line_no == edit["from"] and edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(- edit["content"]- )- # Skip original line; advance edit at range end- if line_no == edit["to"]:- edit_idx += 1- line_no += 1- continue-- dst.write(raw_line)- total_written += 1- line_no += 1-- # Remaining edits past end of file- while edit_idx < len(edits):- edit = edits[edit_idx]- if edit["content"]:- dst.write(edit["content"])- total_written += _count_content_lines(edit["content"])- edit_idx += 1-- shutil.move(tmp_path, path)- return total_written- except Exception:- if os.path.exists(tmp_path):- os.unlink(tmp_path)- raise---def patch_file(path: str, edits: list | None) -> PatchResult:++async def patch_file(path: str, edits: list | None) -> PatchResult:"""Validate and apply edits to a file."""- path = os.path.expanduser(path)- if not os.path.isfile(path):+ info = await runtime.call_development_function(+ _file_info_impl, path+ )+ if not info["is_file"]:return PatchResult(error="file not found")parsed, err = validate_edits(edits)@@ -305,18 +253,8 @@ def patch_file(path: str, edits: list | None) -> PatchResult:return PatchResult(error=err)try:- total = apply_patch(path, parsed)+ total = await apply_patch(info["expanded"], parsed)except Exception as exc:return PatchResult(error=str(exc))return PatchResult(total_lines=total, edit_count=len(parsed))---# -------------------------------------------------------------------# Internal-# --------------------------------------------------------------------def _count_content_lines(content: str) -> int:- return content.count("\n") + (- 1 if content and not content.endswith("\n") else 0- )diff --git a/plugins/text_editor/tools/text_editor.py b/plugins/text_editor/tools/text_editor.pyindex 5892de605..e5d07a120 100644--- a/plugins/text_editor/tools/text_editor.py+++ b/plugins/text_editor/tools/text_editor.py@@ -1,5 +1,3 @@-import os-from python.helpers.tool import Tool, Responsefrom python.helpers.extension import call_extensionsfrom python.helpers import plugins@@ -8,6 +6,7 @@ from plugins.text_editor.helpers.file_ops import (write_file,validate_edits,apply_patch,+ file_info,)# Key used in agent.data to store file state for patch validation@@ -42,7 +41,7 @@ class TextEditor(Tool):raw_to = kwargs.get("line_to")line_to = int(raw_to) if raw_to is not None else None- result = read_file(+ result = await read_file(path,line_from=line_from,line_to=line_to,@@ -54,7 +53,8 @@ class TextEditor(Tool):if result.error:return self._error("read", path, result.error)- _record_mtime(self.agent, os.path.expanduser(path), result.total_lines)+ info = await file_info(path)+ await _record_mtime(self.agent, info, result.total_lines)# Extension pointext_data = {"content": result.content, "warnings": result.warnings}@@ -64,7 +64,7 @@ class TextEditor(Tool):msg = self.agent.read_prompt("fw.text_editor.read_ok.md",- path=os.path.expanduser(path),+ path=info["expanded"],total_lines=str(result.total_lines),warnings=ext_data["warnings"],content=ext_data["content"],@@ -84,7 +84,7 @@ class TextEditor(Tool):"text_editor_write_before", agent=self.agent, data=ext_data)- result = write_file(ext_data["path"], ext_data["content"])+ result = await write_file(ext_data["path"], ext_data["content"])if result.error:return self._error("write", path, result.error)@@ -95,12 +95,12 @@ class TextEditor(Tool):data={"path": path, "total_lines": result.total_lines},)- expanded = os.path.expanduser(path)- _record_mtime(self.agent, expanded, result.total_lines)+ info = await file_info(path)+ await _record_mtime(self.agent, info, result.total_lines)cfg = _get_config(self.agent)- read_result = read_file(- expanded,+ read_result = await read_file(+ info["expanded"],line_from=1,line_to=result.total_lines,max_line_tokens=cfg["max_line_tokens"],@@ -109,7 +109,7 @@ class TextEditor(Tool):msg = self.agent.read_prompt("fw.text_editor.write_ok.md",- path=expanded,+ path=info["expanded"],total_lines=str(result.total_lines),content=read_result.content,)@@ -122,15 +122,16 @@ class TextEditor(Tool):if not path:return self._error("patch", path, "path is required")- expanded = os.path.expanduser(path)- if not os.path.isfile(expanded):+ info = await file_info(path)+ if not info["is_file"]:return self._error("patch", path, "file not found")- stale_err = _check_mtime(self.agent, expanded)+ expanded = info["expanded"]++ stale_err = _check_mtime(self.agent, info)if stale_err:return self._error("patch", path, stale_err)-parsed, err = validate_edits(edits)if err:return self._error("patch", path, err)@@ -142,7 +143,7 @@ class TextEditor(Tool):)try:- total_lines = apply_patch(ext_data["path"], ext_data["edits"])+ total_lines = await apply_patch(ext_data["path"], ext_data["edits"])except Exception as exc:return self._error("patch", path, str(exc))@@ -152,9 +153,11 @@ class TextEditor(Tool):data={"path": expanded, "total_lines": total_lines},)- _apply_patch_post(self.agent, expanded, total_lines, ext_data["edits"])+ # Refresh file info after patch for updated mtime+ post_info = await file_info(expanded)+ _apply_patch_post(self.agent, post_info, total_lines, ext_data["edits"])- patch_content = _read_patch_region(+ patch_content = await _read_patch_region(expanded, ext_data["edits"], total_lines, _get_config(self.agent))@@ -181,7 +184,7 @@ class TextEditor(Tool):# Standalone helpers# -------------------------------------------------------------------def _read_patch_region(+async def _read_patch_region(path: str, edits: list[dict], total_lines: int, cfg: dict) -> str:if not edits:@@ -200,7 +203,7 @@ def _read_patch_region(max_to = max(e["to"] for e in edits)end_line = max_to + added - removed + 3- result = read_file(+ result = await read_file(path,line_from=max(min_from - 1, 1),line_to=min(end_line, total_lines),@@ -210,21 +213,20 @@ def _read_patch_region(return result.content-def _record_mtime(agent, path: str, total_lines: int):+async def _record_mtime(agent, info: dict, total_lines: int):+ """Record mtime using file_info dict from the container."""mtimes = agent.data.setdefault(_MTIME_KEY, {})- try:- mtimes[os.path.realpath(path)] = {- "mtime": os.path.getmtime(path),+ if info["mtime"] is not None:+ mtimes[info["realpath"]] = {+ "mtime": info["mtime"],"total_lines": total_lines,}- except OSError:- pass-def _clear_mtime(agent, path: str):+def _clear_mtime(agent, info: dict):mtimes = agent.data.get(_MTIME_KEY)if mtimes is not None:- mtimes.pop(os.path.realpath(path), None)+ mtimes.pop(info["realpath"], None)def _count_content_lines(content: str) -> int:@@ -244,16 +246,16 @@ def _all_edits_in_place(edits: list[dict]) -> bool:return True-def _apply_patch_post(agent, path: str, new_total: int, edits: list[dict]):-+def _apply_patch_post(agent, info: dict, new_total: int, edits: list[dict]):+ """Update mtime cache after a patch, using file_info from the container."""if not _all_edits_in_place(edits):- _clear_mtime(agent, path)+ _clear_mtime(agent, info)returnmtimes = agent.data.get(_MTIME_KEY)if mtimes is None:return- real = os.path.realpath(path)+ real = info["realpath"]stored = mtimes.get(real)if not isinstance(stored, dict) or "total_lines" not in stored:mtimes.pop(real, None)@@ -261,36 +263,36 @@ def _apply_patch_post(agent, path: str, new_total: int, edits: list[dict]):if new_total != stored["total_lines"]:mtimes.pop(real, None)return- try:+ if info["mtime"] is not None:mtimes[real] = {- "mtime": os.path.getmtime(path),+ "mtime": info["mtime"],"total_lines": new_total,}- except OSError:+ else:mtimes.pop(real, None)-def _check_mtime(agent, path: str) -> str:+def _check_mtime(agent, info: dict) -> str:+ """Check if the file has been modified since last read, using file_info."""mtimes = agent.data.get(_MTIME_KEY, {})- real = os.path.realpath(path)+ real = info["realpath"]if real not in mtimes:return agent.read_prompt(- "fw.text_editor.patch_need_read.md", path=path+ "fw.text_editor.patch_need_read.md", path=info["expanded"])stored = mtimes[real]mtime = stored.get("mtime") if isinstance(stored, dict) else storedif mtime is None:mtimes.pop(real, None)return agent.read_prompt(- "fw.text_editor.patch_need_read.md", path=path+ "fw.text_editor.patch_need_read.md", path=info["expanded"])- try:- current = os.path.getmtime(path)- except OSError:+ current = info["mtime"]+ if current is None:return ""if current != mtime:return agent.read_prompt(- "fw.text_editor.patch_stale_read.md", path=path+ "fw.text_editor.patch_stale_read.md", path=info["expanded"])return ""diff --git a/python/helpers/rfc_text_editor.py b/python/helpers/rfc_text_editor.pynew file mode 100644index 000000000..c21b2cbd7--- /dev/null+++ b/python/helpers/rfc_text_editor.py@@ -0,0 +1,210 @@+"""+RFC-routable filesystem operations for the text_editor plugin.++These functions run inside the Docker container when invoked via+runtime.call_development_function. They use only stdlib — no+framework dependencies (no tiktoken, no agent context, etc.).++All args and return values must be JSON-serializable.+"""++import os+import shutil+import tempfile++_BINARY_PEEK = 8192+++# ------------------------------------------------------------------+# Internal+# ------------------------------------------------------------------++def _count_content_lines(content: str) -> int:+ return content.count("\n") + (+ 1 if content and not content.endswith("\n") else 0+ )+++# ------------------------------------------------------------------+# Binary detection+# ------------------------------------------------------------------++def is_binary_impl(path: str) -> bool:+ """Check for null bytes in the first 8 KiB."""+ try:+ with open(path, "rb") as f:+ chunk = f.read(_BINARY_PEEK)+ return b"\x00" in chunk+ except OSError:+ return False+++# ------------------------------------------------------------------+# Read+# ------------------------------------------------------------------++def read_file_raw_impl(path: str) -> dict:+ """+ Read a text file and return raw lines plus metadata.++ Returns dict with:+ - lines: list[str] (raw lines including newlines)+ - total_lines: int+ - error: str (empty on success)+ """+ path = os.path.expanduser(path)++ if not os.path.isfile(path):+ return {"lines": [], "total_lines": 0, "error": "file not found"}++ # Binary check inline to avoid extra RFC round-trip+ try:+ with open(path, "rb") as f:+ chunk = f.read(_BINARY_PEEK)+ if b"\x00" in chunk:+ return {"lines": [], "total_lines": 0,+ "error": "file appears binary, use terminal instead"}+ except OSError:+ pass++ try:+ with open(path, "r", encoding="utf-8", errors="replace") as f:+ all_lines = f.readlines()+ except OSError as exc:+ return {"lines": [], "total_lines": 0, "error": str(exc)}++ return {"lines": all_lines, "total_lines": len(all_lines), "error": ""}+++# ------------------------------------------------------------------+# Write+# ------------------------------------------------------------------++def write_file_impl(path: str, content: str) -> dict:+ """+ Write content to a file, creating parent dirs as needed.++ Returns dict with:+ - total_lines: int+ - error: str (empty on success)+ """+ path = os.path.expanduser(path)+ try:+ os.makedirs(os.path.dirname(path) or ".", exist_ok=True)+ with open(path, "w", encoding="utf-8") as f:+ f.write(content)+ except OSError as exc:+ return {"total_lines": 0, "error": str(exc)}++ total = content.count("\n") + (+ 1 if content and not content.endswith("\n") else 0+ )+ return {"total_lines": total, "error": ""}+++# ------------------------------------------------------------------+# Patch+# ------------------------------------------------------------------++def apply_patch_impl(path: str, edits: list) -> dict:+ """+ Apply sorted, validated edits to a file.++ Returns dict with:+ - total_lines: int+ - error: str (empty on success)+ """+ path = os.path.expanduser(path)++ # Ensure content always ends with newline to prevent line merging+ for e in edits:+ if e["content"] and not e["content"].endswith("\n"):+ e["content"] += "\n"++ dir_name = os.path.dirname(path) or "."+ fd, tmp_path = tempfile.mkstemp(dir=dir_name, suffix=".tmp")+ try:+ with (+ open(path, "r", encoding="utf-8", errors="replace") as src,+ os.fdopen(fd, "w", encoding="utf-8") as dst,+ ):+ edit_idx = 0+ line_no = 1+ total_written = 0++ for raw_line in src:+ while (+ edit_idx < len(edits)+ and edits[edit_idx]["insert"]+ and edits[edit_idx]["from"] == line_no+ ):+ edit = edits[edit_idx]+ if edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(edit["content"])+ edit_idx += 1++ if edit_idx < len(edits) and not edits[edit_idx]["insert"]:+ edit = edits[edit_idx]+ if edit["from"] <= line_no <= edit["to"]:+ if line_no == edit["from"] and edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(+ edit["content"]+ )+ if line_no == edit["to"]:+ edit_idx += 1+ line_no += 1+ continue++ dst.write(raw_line)+ total_written += 1+ line_no += 1++ while edit_idx < len(edits):+ edit = edits[edit_idx]+ if edit["content"]:+ dst.write(edit["content"])+ total_written += _count_content_lines(edit["content"])+ edit_idx += 1++ shutil.move(tmp_path, path)+ return {"total_lines": total_written, "error": ""}+ except Exception as exc:+ if os.path.exists(tmp_path):+ os.unlink(tmp_path)+ return {"total_lines": 0, "error": str(exc)}+++# ------------------------------------------------------------------+# File info+# ------------------------------------------------------------------++def file_info_impl(path: str) -> dict:+ """+ Return file metadata needed by the host for mtime tracking.++ Returns dict with:+ - exists: bool+ - is_file: bool+ - realpath: str+ - expanded: str (expanduser result)+ - mtime: float | None+ """+ path = os.path.expanduser(path)+ rp = os.path.realpath(path)+ exists = os.path.exists(path)+ is_file = os.path.isfile(path)+ mtime = None+ if exists:+ try:+ mtime = os.path.getmtime(path)+ except OSError:+ pass+ return {+ "exists": exists,+ "is_file": is_file,+ "realpath": rp,+ "expanded": path,+ "mtime": mtime,+ }