The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): drop the write_file rewrite warning
emozillab2835584b7c4
diff --git a/tests/tools/test_file_tools_live.py b/tests/tools/test_file_tools_live.pyindex 5603c55d048..9252b607a24 100644--- a/tests/tools/test_file_tools_live.py+++ b/tests/tools/test_file_tools_live.py@@ -128,23 +128,6 @@ class TestWriteFile:assert result.bytes_written == len(SIMPLE_CONTENT.encode())assert Path(path).read_text() == SIMPLE_CONTENT- # .py so write_file probes the previous content (lint coverage) for the rewrite check.- def test_large_rewrite_of_existing_file_warns_with_replaced_line_count(self, ops, tmp_path):- path = tmp_path / "big.py"- path.write_text("".join(f"x_{i} = {i}\n" for i in range(40)))- rewritten = "".join(f"x_{i} = {i + 1000 if i < 30 else i}\n" for i in range(40))- result = ops.write_file(str(path), rewritten)- assert result.error is None- assert result.warning is not None- assert "30 of 40 previous lines" in result.warning-- def test_small_edit_of_existing_file_has_no_warning(self, ops, tmp_path):- path = tmp_path / "small.py"- path.write_text("".join(f"x_{i} = {i}\n" for i in range(40)))- result = ops.write_file(str(path), path.read_text().replace("x_7 = 7", "x_7 = 70"))- assert result.error is None- assert result.warning is None-# ── patch_replace ────────────────────────────────────────────────────────diff --git a/tools/file_operations.py b/tools/file_operations.pyindex bd22b810946..1a93283f8c9 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -1518,11 +1518,6 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):# Pre-content is read only for extensions in the UNION of in-process lint and# LSP coverage (keeps the hot path fast for binaries).want_pre = ext in LINTERS_INPROC or self._lsp_handles_extension(ext)- # A caller-supplied pre_content (e.g. patch_replace, which already computes and- # returns its own diff+boundary note) already has a structural signal — do not- # duplicate it below. Only the direct write_file entrypoint, which never had the- # prior content in hand, is missing one.- caller_supplied_pre_content = pre_content is not Nonehas_bom, pre_content, original_ending = self._probe_write_target(path, pre_content, want_pre)if ext == ".json":refused = _refuse_introduced_json_constant(path, content, pre_content)@@ -1556,32 +1551,10 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):lsp_diagnostics: Optional[str] = Noneif lint_result.success or lint_result.skipped:lsp_diagnostics = self._maybe_lsp_diagnostics(path, pre_content=pre_content, post_content=content) or None- # write_file has no structural check of model-authored content against the file's own- # prior content — unlike patch_replace, whose diff+boundary note give the caller a- # concrete way to notice a bad edit. When the pre-edit content happens to already be in- # hand (probed above for lint/LSP, not re-fetched here), surface a compact change-size- # summary so an unexpectedly large rewrite of an existing file is visible rather than- # silent — information only, no auto-fix or content change.- rewrite_warning: Optional[str] = None- if not caller_supplied_pre_content and pre_content:- pre_lines = pre_content.splitlines()- opcodes = difflib.SequenceMatcher(None, pre_lines, content.splitlines()).get_opcodes()- # Count previous lines removed or replaced (pure insertions leave prior content- # intact), so the count is bounded by the previous line count and the warning- # fires on rewriting at least a quarter of the old file (min 10 lines).- replaced = sum(i2 - i1 for tag, i1, i2, _, _ in opcodes if tag in ("replace", "delete"))- if replaced >= max(10, len(pre_lines) // 4):- rewrite_warning = (- f"write_file replaced an existing file with no structural check against its "- f"prior content: {replaced} of {len(pre_lines)} previous lines were removed or "- "replaced. If this was meant to be a small edit, re-read the file to confirm "- "the rewrite is correct — a hand-retyped file has no diff/fuzzy-match safety net."- )return WriteResult(bytes_written=len(content_bytes), dirs_created=dirs_created, verified=content_verified,_content_sha256=hashlib.sha256(content_bytes).hexdigest(),- lint=lint_result.to_dict() if lint_result else None, lsp_diagnostics=lsp_diagnostics,- warning=rewrite_warning)+ lint=lint_result.to_dict() if lint_result else None, lsp_diagnostics=lsp_diagnostics)# --- PATCH (replace mode) -----------------------------------------------