Skip to content

The agent that grows with you

perf(file-ops): eliminate redundant subprocess calls in write_file and V4A patch path

阿泥豆cb3e8e9fb1ce
diff --git a/tests/tools/test_patch_parser.py b/tests/tools/test_patch_parser.py
index 78a405ae5b4..6623ec25bbc 100644
--- a/tests/tools/test_patch_parser.py
+++ b/tests/tools/test_patch_parser.py
@@ -215,7 +215,7 @@ class TestApplyUpdate:
error=None,
)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.written = content
return SimpleNamespace(error=None)
@@ -261,7 +261,7 @@ class TestAdditionOnlyHunks:
content="def main():\n pass\n",
error=None,
)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.written = content
return SimpleNamespace(error=None)
@@ -289,7 +289,7 @@ class TestAdditionOnlyHunks:
content="existing = True\n",
error=None,
)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.written = content
return SimpleNamespace(error=None)
@@ -326,7 +326,7 @@ class TestReadFileRaw:
written = None
def read_file_raw(self, path):
return SimpleNamespace(content=file_content, error=None)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.written = content
return SimpleNamespace(error=None)
@@ -360,7 +360,7 @@ class TestReadFileRaw:
written = None
def read_file_raw(self, path):
return SimpleNamespace(content=file_content, error=None)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.written = content
return SimpleNamespace(error=None)
@@ -403,7 +403,7 @@ class TestValidationPhase:
return SimpleNamespace(content=None, error=f"File not found: {path}")
return SimpleNamespace(content=content, error=None)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
written[path] = content
return SimpleNamespace(error=None)
@@ -569,7 +569,7 @@ class TestV4ALspDiagnosticsPropagation:
)
class FakeFileOps:
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
return SimpleNamespace(error=None, lsp_diagnostics=diag_block)
def _check_lint(self, path):
@@ -603,7 +603,7 @@ class TestV4ALspDiagnosticsPropagation:
def read_file_raw(self, path):
return SimpleNamespace(content="ctx\nold\nctx\n", error=None)
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
return SimpleNamespace(error=None, lsp_diagnostics=diag_block)
def _check_lint(self, path):
@@ -621,7 +621,7 @@ class TestV4ALspDiagnosticsPropagation:
ops = self._build_ops_writing("foo.py", "x = 1\n")
class FakeFileOps:
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
# lsp_diagnostics omitted entirely (older WriteResult shape).
return SimpleNamespace(error=None)
@@ -654,7 +654,7 @@ class TestV4ALspDiagnosticsPropagation:
}
class FakeFileOps:
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
return SimpleNamespace(error=None, lsp_diagnostics=per_file[path])
def _check_lint(self, path):
@@ -679,7 +679,7 @@ class _DictFileOps:
return SimpleNamespace(content=self.files[path], error=None)
return SimpleNamespace(content="", error="file not found")
- def write_file(self, path, content):
+ def write_file(self, path, content, pre_content=None):
self.files[path] = content
return SimpleNamespace(error=None)
diff --git a/tools/file_operations.py b/tools/file_operations.py
index f6a2deacaba..5b9ea997ebf 100644
--- a/tools/file_operations.py
+++ b/tools/file_operations.py
@@ -465,7 +465,8 @@ class FileOperations(ABC):
...
@abstractmethod
- def write_file(self, path: str, content: str) -> WriteResult:
+ def write_file(self, path: str, content: str,
+ pre_content: Optional[str] = None) -> WriteResult:
"""Write content to a file, creating directories as needed."""
...
@@ -1012,6 +1013,9 @@ class ShellFileOperations(FileOperations):
``.hermes-tmp`` file next to the user's data, and the original file
is left untouched. Content rides stdin so there is no ARG_MAX limit.
+ ``mkdir -p`` for the parent directory is folded into this script
+ (one fewer subprocess vs. a separate ``mkdir -p`` call).
+
Returns an :class:`ExecuteResult`; ``exit_code == 0`` means the file
was swapped into place atomically. A non-zero exit means nothing was
renamed and the original (if any) is intact.
@@ -1025,6 +1029,9 @@ class ShellFileOperations(FileOperations):
tmpl = self._escape_shell_arg(".hermes-tmp.XXXXXX")
# One shell script, fully quoted. Notes:
+ # - `mkdir -p "$d"` is folded in here so the parent directory is
+ # created in the same subprocess that writes the temp file —
+ # saves one entire subprocess spawn vs. a separate mkdir call.
# - `mktemp` lands the temp in the target's own dir (-p) so `mv` is
# same-FS atomic; we fall back to a PID-stamped name if the
# backend lacks mktemp (rare; busybox/macOS/Linux all ship it).
@@ -1058,6 +1065,11 @@ class ShellFileOperations(FileOperations):
'rt="$(readlink -f "$t" 2>/dev/null || realpath "$t" 2>/dev/null || true)"; '
'[ -n "$rt" ] && { t="$rt"; d="$(dirname "$t")"; }; '
"fi; "
+ # Create the parent dir in the SAME subprocess that writes the
+ # temp file (one fewer exec vs. a separate mkdir call). Runs
+ # AFTER symlink resolution so a resolved target's directory is
+ # the one created/confirmed.
+ 'mkdir -p "$d"; '
'tmp="$(mktemp -p "$d" ' + tmpl + ' 2>/dev/null '
'|| mktemp "$d/.hermes-tmp.$$.XXXXXX" 2>/dev/null '
'|| { tmp="$d/.hermes-tmp.$$"; : > "$tmp" && echo "$tmp"; })"; '
@@ -1400,7 +1412,8 @@ class ShellFileOperations(FileOperations):
# WRITE Implementation
# =========================================================================
- def write_file(self, path: str, content: str) -> WriteResult:
+ def write_file(self, path: str, content: str,
+ pre_content: Optional[str] = None) -> WriteResult:
"""
Write content to a file, creating parent directories as needed.
@@ -1427,6 +1440,11 @@ class ShellFileOperations(FileOperations):
Args:
path: File path to write
content: Content to write
+ pre_content: Pre-edit file content if the caller already has it
+ (e.g. patch_replace read the file for fuzzy matching).
+ When provided, skips a redundant ``cat`` subprocess to
+ re-read the file for lint baseline / line-ending / BOM
+ detection. When None, reads from disk as before.
Returns:
WriteResult with bytes written, lint summary, or error.
@@ -1492,17 +1510,21 @@ class ShellFileOperations(FileOperations):
# the UNION of in-process lint coverage and LSP coverage. For
# extensions outside both sets (binaries, opaque formats),
# skipping the read keeps the hot path fast.
- pre_content: Optional[str] = None
want_pre = ext in LINTERS_INPROC or self._lsp_handles_extension(ext)
if want_pre:
- # Best-effort read; failure (file missing, permission) leaves
- # pre_content as None which makes both downstream consumers
- # degrade gracefully (lint reports all errors; LSP skips the
- # shift map).
- read_cmd = f"cat {self._escape_shell_arg(path)} 2>/dev/null"
- read_result = self._exec(read_cmd)
- if read_result.exit_code == 0 and read_result.stdout:
- pre_content = read_result.stdout
+ if pre_content is not None:
+ # Caller already has file content (e.g. patch_replace read it
+ # for fuzzy matching) — reuse directly, skip redundant cat.
+ pass
+ else:
+ # Best-effort read; failure (file missing, permission) leaves
+ # pre_content as None which makes both downstream consumers
+ # degrade gracefully (lint reports all errors; LSP skips the
+ # shift map).
+ read_cmd = f"cat {self._escape_shell_arg(path)} 2>/dev/null"
+ read_result = self._exec(read_cmd)
+ if read_result.exit_code == 0 and read_result.stdout:
+ pre_content = read_result.stdout
# ── Line-ending preservation (Roo Code pattern) ──────────────
# If the file existed with CRLF endings and the agent's content
@@ -1534,15 +1556,13 @@ class ShellFileOperations(FileOperations):
# rather than an external IDE.
self._snapshot_lsp_baseline(path)
- # Create parent directories
+ # Write atomically. ``mkdir -p`` is folded into _atomic_write
+ # (one fewer subprocess vs. a separate mkdir call). Report
+ # dirs_created as True when the parent wasn't obviously present;
+ # we don't stat to avoid an extra syscall — if the mkdir succeeds
+ # or was already there, _atomic_write handles it.
parent = os.path.dirname(path)
- dirs_created = False
-
- if parent:
- mkdir_cmd = f"mkdir -p {self._escape_shell_arg(parent)}"
- mkdir_result = self._exec(mkdir_cmd)
- if mkdir_result.exit_code == 0:
- dirs_created = True
+ dirs_created = bool(parent)
# Write atomically: stream into a temp file in the SAME directory,
# then ``mv`` it over the target. The rename is atomic on POSIX
@@ -1564,14 +1584,10 @@ class ShellFileOperations(FileOperations):
if write_result.exit_code != 0:
return WriteResult(error=f"Failed to write file: {write_result.stdout}")
- # Get bytes written (wc -c is POSIX, works on Linux + macOS)
- stat_cmd = f"wc -c < {self._escape_shell_arg(path)} 2>/dev/null"
- stat_result = self._exec(stat_cmd)
-
- try:
- bytes_written = int(stat_result.stdout.strip())
- except ValueError:
- bytes_written = len(content.encode('utf-8'))
+ # Get bytes written — compute from the content we just wrote
+ # (len(content.encode('utf-8')) matches wc -c for UTF-8) instead
+ # of spawning a ``wc -c`` subprocess.
+ bytes_written = len(content.encode('utf-8'))
# Post-write content verification (cheap, one shell call): compare
# the on-disk sha256 to the intended content's hash. Production
@@ -1659,6 +1675,9 @@ class ShellFileOperations(FileOperations):
return PatchResult(error=f"Failed to read file: {path}")
content = read_result.stdout
+ # Preserve raw content (including BOM) for write_file's pre_content
+ # so write_file can detect/restore BOM correctly.
+ raw_content = content
# Strip a leading UTF-8 BOM before matching so the fuzzy matcher and
# the diff operate on clean content (a phantom U+FEFF before line 1
# defeats an exact first-line match). write_file restores the BOM on
@@ -1711,8 +1730,11 @@ class ShellFileOperations(FileOperations):
if file_ending:
new_content = _normalize_line_endings(new_content, file_ending)
- # Write back
- write_result = self.write_file(path, new_content)
+ # Write back — pass pre_content (original read, with BOM) to avoid
+ # a redundant cat subprocess inside write_file. Must be the raw
+ # content (before _strip_bom) so write_file can detect/restore BOM.
+ write_result = self.write_file(path, new_content,
+ pre_content=raw_content)
if write_result.error:
return PatchResult(error=f"Failed to write changes: {write_result.error}")
diff --git a/tools/patch_parser.py b/tools/patch_parser.py
index 271538904ec..1813dca6c39 100644
--- a/tools/patch_parser.py
+++ b/tools/patch_parser.py
@@ -432,6 +432,7 @@ def apply_v4a_operations(operations: List[PatchOperation],
# ``PatchResult.lsp_diagnostics`` aggregation below.
lsp_blocks: List[str] = []
errors = []
+ lint_results = {}
for op in operations:
try:
@@ -442,6 +443,8 @@ def apply_v4a_operations(operations: List[PatchOperation],
all_diffs.append(result[1])
if result[2]:
lsp_blocks.append(result[2])
+ if result[3]:
+ lint_results[op.file_path] = result[3]
else:
errors.append(f"Failed to add {op.file_path}: {result[1]}")
@@ -468,18 +471,18 @@ def apply_v4a_operations(operations: List[PatchOperation],
all_diffs.append(result[1])
if result[2]:
lsp_blocks.append(result[2])
+ if result[3]:
+ lint_results[op.file_path] = result[3]
else:
errors.append(f"Failed to update {op.file_path}: {result[1]}")
except Exception as e:
errors.append(f"Error processing {op.file_path}: {str(e)}")
- # Run lint on all modified/created files
- lint_results = {}
- for f in files_modified + files_created:
- if hasattr(file_ops, '_check_lint'):
- lint_result = file_ops._check_lint(f)
- lint_results[f] = lint_result.to_dict()
+ # Lint results were collected from write_file's internal _check_lint_delta
+ # via the four-tuple return of _apply_add / _apply_update — zero extra
+ # subprocess calls vs. the old approach of re-reading each file with a
+ # bare _check_lint(f) that lacked post_content context.
combined_diff = '\n'.join(all_diffs)
@@ -514,14 +517,16 @@ def apply_v4a_operations(operations: List[PatchOperation],
)
-def _apply_add(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optional[str]]:
+def _apply_add(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optional[str], Optional[dict]]:
"""Apply an add file operation.
- Returns ``(success, diff_or_error, lsp_diagnostics)``. The third
- element carries the formatted ``<diagnostics>`` block from
+ Returns ``(success, diff_or_error, lsp_diagnostics, lint_result)``.
+ The third element carries the formatted ``<diagnostics>`` block from
:class:`WriteResult.lsp_diagnostics` so V4A patches can surface
- semantic diagnostics from the LSP layer — without this, the LSP
- tier would silently swallow them on the V4A code path.
+ semantic diagnostics from the LSP layer. The fourth element carries
+ the ``WriteResult.lint`` dict (syntax check result) so V4A patches
+ can propagate lint to ``PatchResult.lint`` without a redundant
+ ``_check_lint`` re-read — write_file already ran the check internally.
"""
# Extract content from hunks (all + lines)
content_lines = []
@@ -532,14 +537,15 @@ def _apply_add(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optional[s
content = '\n'.join(content_lines)
+ # _apply_add creates a new file, no pre_content to pass
result = file_ops.write_file(op.file_path, content)
if result.error:
- return False, result.error, None
-
+ return False, result.error, None, None
+
diff = f"--- /dev/null\n+++ b/{op.file_path}\n"
diff += '\n'.join(f"+{line}" for line in content_lines)
-
- return True, diff, getattr(result, "lsp_diagnostics", None)
+
+ return True, diff, getattr(result, "lsp_diagnostics", None), getattr(result, "lint", None)
def _apply_delete(op: PatchOperation, file_ops: Any) -> Tuple[bool, str]:
@@ -573,11 +579,11 @@ def _apply_move(op: PatchOperation, file_ops: Any) -> Tuple[bool, str]:
return True, diff
-def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optional[str]]:
+def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optional[str], Optional[dict]]:
"""Apply an update file operation.
- Returns ``(success, diff_or_error, lsp_diagnostics)`` — see
- :func:`_apply_add` for the rationale on the third element.
+ Returns ``(success, diff_or_error, lsp_diagnostics, lint_result)`` — see
+ :func:`_apply_add` for the rationale on the third and fourth elements.
"""
# Deferred import: breaks the patch_parser ↔ fuzzy_match circular dependency
from tools.fuzzy_match import fuzzy_find_and_replace
@@ -586,7 +592,7 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optiona
read_result = file_ops.read_file_raw(op.file_path)
if read_result.error:
- return False, f"Cannot read file: {read_result.error}", None
+ return False, f"Cannot read file: {read_result.error}", None, None
current_content = read_result.content
@@ -650,7 +656,7 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optiona
err_msg += format_no_match_hint(error, 0, search_pattern, new_content)
except Exception:
pass
- return False, err_msg, None
+ return False, err_msg, None, None
else:
# Addition-only hunk (no context or removed lines).
# Insert at the location indicated by the context hint, or at end of file.
@@ -664,7 +670,7 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optiona
return False, (
f"Addition-only hunk: context hint '{hunk.context_hint}' is ambiguous "
f"({occurrences} occurrences) — provide a more unique hint"
- ), None
+ ), None, None
else:
hint_pos = new_content.find(hunk.context_hint)
# Insert after the line containing the context hint
@@ -676,10 +682,12 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optiona
else:
new_content = new_content.rstrip('\n') + '\n' + insert_text + '\n'
- # Write new content
- write_result = file_ops.write_file(op.file_path, new_content)
+ # Write new content — pass current_content (already read above) to avoid
+ # a redundant cat subprocess inside write_file.
+ write_result = file_ops.write_file(op.file_path, new_content,
+ pre_content=current_content)
if write_result.error:
- return False, write_result.error, None
+ return False, write_result.error, None, None
# Generate diff
diff_lines = difflib.unified_diff(
@@ -690,4 +698,4 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> Tuple[bool, str, Optiona
)
diff = ''.join(diff_lines)
- return True, diff, getattr(write_result, "lsp_diagnostics", None)
+ return True, diff, getattr(write_result, "lsp_diagnostics", None), getattr(write_result, "lint", None)