refactor(tools): unify edit summaries with shared fast diffs
Return compact, workspace-relative summaries from edit_file and apply_patch. Compute one RapidFuzz line alignment per file and reuse it for summary counts and WebUI unified diffs, checking snapshots before reuse. Summarize batched edits by their final file state. Warn only when a recorded file version changed, without asking for a read after an edit has already succeeded. Preserve existing edit errors. Validate summary and activity reuse, repetitive files, diff formatting, snapshot changes, file creation, and read-state behavior.
This commit is contained in:
1 parent
3a725b35a7
commit
1c3c68262f
13 files changed
+346
-170
No files matched your search
@@ -46,7 +46,7 @@ class TestDeleteLineCleanup:
|
||||
content = f"{prefix}{indent}line2\nline3\n"
|
||||
f.write_bytes(content.replace("\n", newline).encode("utf-8"))
|
||||
result = await tool.execute(path=str(f), old_text=f"{indent}line2", new_text="")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
# Should not leave a blank line where line2 was
|
||||
assert f.read_bytes() == f"{prefix}line3\n".replace("\n", newline).encode("utf-8")
|
||||
|
||||
@@ -61,7 +61,7 @@ class TestDeleteLineCleanup:
|
||||
content = f"{prefix}x = 1{old_text}\ny = 2\n"
|
||||
f.write_bytes(content.replace("\n", newline).encode("utf-8"))
|
||||
result = await tool.execute(path=str(f), old_text=old_text, new_text="")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
expected = f"{prefix}x = 1\ny = 2\n"
|
||||
assert f.read_bytes() == expected.replace("\n", newline).encode("utf-8")
|
||||
|
||||
@@ -76,7 +76,7 @@ class TestDeleteLineCleanup:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text=" # obsolete", new_text="", replace_all=True,
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
expected = "x = 1\ny = 2\nz = 3\n"
|
||||
assert f.read_bytes() == expected.replace("\n", newline).encode("utf-8")
|
||||
|
||||
@@ -85,7 +85,7 @@ class TestDeleteLineCleanup:
|
||||
f = tmp_path / "a.py"
|
||||
f.write_text("line1\nline2\nline3\n", encoding="utf-8")
|
||||
result = await tool.execute(path=str(f), old_text="line2\n", new_text="")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text() == "line1\nline3\n"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -94,7 +94,7 @@ class TestDeleteLineCleanup:
|
||||
f = tmp_path / "a.py"
|
||||
f.write_text("hello world here\n", encoding="utf-8")
|
||||
result = await tool.execute(path=str(f), old_text="world ", new_text="")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text() == "hello here\n"
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ class TestQuoteStylePreservation:
|
||||
old_text='message = "hello"',
|
||||
new_text='message = "goodbye"',
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text(encoding="utf-8") == 'message = “goodbye”\n'
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -131,7 +131,7 @@ class TestQuoteStylePreservation:
|
||||
old_text="it's fine",
|
||||
new_text="it's better",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text(encoding="utf-8") == "it’s better\n"
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ class TestIndentationPreservation:
|
||||
old_text="def foo():\n pass",
|
||||
new_text="def bar():\n return 1",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text(encoding="utf-8") == (
|
||||
"if True:\n"
|
||||
" def bar():\n"
|
||||
@@ -238,7 +238,7 @@ class TestAdvancedReplaceAll:
|
||||
new_text="def bar():\n return 1",
|
||||
replace_all=True,
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text(encoding="utf-8") == (
|
||||
"if a:\n"
|
||||
" def bar():\n"
|
||||
@@ -257,7 +257,7 @@ class TestAdvancedReplaceAll:
|
||||
old_text='message = "hello"',
|
||||
new_text='message = "goodbye"',
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text(encoding="utf-8") == " message = “goodbye”\n"
|
||||
|
||||
|
||||
@@ -280,7 +280,7 @@ class TestTrailingWhitespaceStrip:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text="x = 1", new_text="x = 2 \ny = 3 ",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
content = f.read_text()
|
||||
assert "x = 2\ny = 3\n" == content
|
||||
|
||||
@@ -292,7 +292,7 @@ class TestTrailingWhitespaceStrip:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text="# Title", new_text="# Title \nSubtitle ",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
content = f.read_text()
|
||||
# Trailing spaces should be preserved for markdown
|
||||
assert "Title " in content
|
||||
@@ -361,6 +361,6 @@ class TestStaleDetectionContentFallback:
|
||||
os.utime(f, (stat.st_atime, stat.st_mtime + 10))
|
||||
|
||||
result = await edit_tool.execute(path=str(f), old_text="world", new_text="earth")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
# Should NOT warn about modification since content is the same
|
||||
assert "modified" not in result.lower()
|
||||
@@ -1,9 +1,12 @@
|
||||
"""Tests for EditFileTool enhancements: read-before-edit tracking, path suggestions,
|
||||
notebook JSON editing, and create-file semantics."""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.agent.tools import file_state
|
||||
from nanobot.agent.tools.apply_patch import ApplyPatchTool
|
||||
from nanobot.agent.tools.filesystem import EditFileTool, ReadFileTool
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -23,7 +26,7 @@ def _clear_file_state():
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestEditReadTracking:
|
||||
"""edit_file should warn when file hasn't been read first."""
|
||||
"""edit_file warns only when a recorded file version has changed."""
|
||||
|
||||
@pytest.fixture()
|
||||
def file_states(self):
|
||||
@@ -38,13 +41,12 @@ class TestEditReadTracking:
|
||||
return EditFileTool(workspace=tmp_path, file_states=file_states)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_edit_warns_if_file_not_read_first(self, edit_tool, tmp_path):
|
||||
async def test_edit_does_not_warn_without_read_record(self, edit_tool, tmp_path):
|
||||
f = tmp_path / "a.py"
|
||||
f.write_text("hello world", encoding="utf-8")
|
||||
result = await edit_tool.execute(path=str(f), old_text="world", new_text="earth")
|
||||
# Should still succeed but include a warning
|
||||
assert "Successfully" in result
|
||||
assert "not been read" in result.lower() or "warning" in result.lower()
|
||||
assert result == "Patch applied:\n- update a.py (+1/-1)"
|
||||
assert f.read_text() == "hello earth"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_edit_succeeds_cleanly_after_read(self, read_tool, edit_tool, tmp_path):
|
||||
@@ -52,21 +54,30 @@ class TestEditReadTracking:
|
||||
f.write_text("hello world", encoding="utf-8")
|
||||
await read_tool.execute(path=str(f))
|
||||
result = await edit_tool.execute(path=str(f), old_text="world", new_text="earth")
|
||||
assert "Successfully" in result
|
||||
# No warning when file was read first
|
||||
assert "not been read" not in result.lower()
|
||||
assert result == "Patch applied:\n- update a.py (+1/-1)"
|
||||
assert f.read_text() == "hello earth"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_edit_warns_if_file_modified_since_read(self, read_tool, edit_tool, tmp_path):
|
||||
@pytest.mark.parametrize("reread", [False, True])
|
||||
async def test_edit_after_file_modified_since_read(
|
||||
self, read_tool, edit_tool, tmp_path, reread,
|
||||
):
|
||||
f = tmp_path / "a.py"
|
||||
f.write_text("hello world", encoding="utf-8")
|
||||
await read_tool.execute(path=str(f))
|
||||
# External modification
|
||||
mtime = f.stat().st_mtime
|
||||
f.write_text("hello universe", encoding="utf-8")
|
||||
os.utime(f, (mtime + 2, mtime + 2))
|
||||
if reread:
|
||||
assert "hello universe" in await read_tool.execute(path=str(f))
|
||||
result = await edit_tool.execute(path=str(f), old_text="universe", new_text="earth")
|
||||
assert "Successfully" in result
|
||||
assert "modified" in result.lower() or "warning" in result.lower()
|
||||
summary = "Patch applied:\n- update a.py (+1/-1)"
|
||||
assert result == (
|
||||
summary if reread
|
||||
else "Warning: file was modified since the last recorded read or write.\n" + summary
|
||||
)
|
||||
assert f.read_text() == "hello earth"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -84,7 +95,7 @@ class TestEditCreateFile:
|
||||
async def test_create_new_file_with_empty_old_text(self, tool, tmp_path):
|
||||
f = tmp_path / "subdir" / "new.py"
|
||||
result = await tool.execute(path=str(f), old_text="", new_text="print('hi')")
|
||||
assert "created" in result.lower() or "Successfully" in result
|
||||
assert result == "Patch applied:\n- add subdir/new.py (+1/-0)"
|
||||
assert f.exists()
|
||||
assert f.read_text() == "print('hi')"
|
||||
|
||||
@@ -102,7 +113,7 @@ class TestEditCreateFile:
|
||||
f = tmp_path / "empty.py"
|
||||
f.write_text("", encoding="utf-8")
|
||||
result = await tool.execute(path=str(f), old_text="", new_text="print('hi')")
|
||||
assert "Successfully" in result
|
||||
assert result == "Patch applied:\n- update empty.py (+1/-0)"
|
||||
assert f.read_text() == "print('hi')"
|
||||
|
||||
|
||||
@@ -126,7 +137,7 @@ class TestEditIpynbFiles:
|
||||
old_text='"cells": []',
|
||||
new_text='"cells": [{"cell_type": "markdown", "source": "hi"}]',
|
||||
)
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert '"source": "hi"' in f.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
@@ -158,3 +169,18 @@ class TestEditPathSuggestion:
|
||||
path=str(tmp_path / "nonexistent.py"), old_text="a", new_text="b",
|
||||
)
|
||||
assert "Error" in result
|
||||
|
||||
|
||||
async def test_edit_summary_matches_apply_patch(tmp_path):
|
||||
states = file_state.FileStates()
|
||||
target = tmp_path / "config.toml"
|
||||
target.write_bytes(b"a = 1\n")
|
||||
edited = await EditFileTool(workspace=tmp_path, file_states=states).execute(
|
||||
path="config.toml", old_text="1", new_text="2",
|
||||
)
|
||||
target.write_bytes(b"a = 1\n")
|
||||
patched = await ApplyPatchTool(workspace=tmp_path, file_states=states).execute(
|
||||
edits=[{"path": "config.toml", "action": "replace", "old_text": "1", "new_text": "2"}],
|
||||
)
|
||||
assert patched == "Patch applied:\n- update config.toml (+1/-1)"
|
||||
assert edited == patched
|
||||
@@ -32,7 +32,7 @@ def test_edit_file_can_select_occurrence(tmp_path):
|
||||
occurrence=2,
|
||||
))
|
||||
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert target.read_text() == "one\nsame\ntwo\nchanged\n"
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ def test_edit_file_expected_replacements_allows_replace_all_when_count_matches(t
|
||||
expected_replacements=2,
|
||||
))
|
||||
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert target.read_text() == "changed\nchanged\n"
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ def test_edit_file_line_hint_selects_matching_occurrence(tmp_path):
|
||||
line_hint=4,
|
||||
))
|
||||
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert target.read_text() == "one\nsame\ntwo\nchanged\n"
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ def test_edit_file_line_hint_can_cover_multiline_match(tmp_path):
|
||||
line_hint=3,
|
||||
))
|
||||
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert target.read_text() == "before\nstart\nchanged\nend\nafter\n"
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ def test_edit_file_can_edit_ipynb_as_json(tmp_path):
|
||||
new_text='"cells": [{"cell_type": "markdown", "source": "hi"}]',
|
||||
))
|
||||
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert '"source": "hi"' in target.read_text()
|
||||
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ class TestEditFileTool:
|
||||
f = tmp_path / "a.py"
|
||||
f.write_text("hello world", encoding="utf-8")
|
||||
result = await tool.execute(path=str(f), old_text="world", new_text="earth")
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text() == "hello earth"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -150,7 +150,7 @@ class TestEditFileTool:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text="line1\nline2", new_text="LINE1\nLINE2",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
raw = f.read_bytes()
|
||||
assert b"LINE1" in raw
|
||||
# CRLF line endings should be preserved throughout the file
|
||||
@@ -163,7 +163,7 @@ class TestEditFileTool:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text="def foo():\n pass", new_text="def bar():\n return 1",
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert "bar" in f.read_text()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -180,7 +180,7 @@ class TestEditFileTool:
|
||||
result = await tool.execute(
|
||||
path=str(f), old_text="foo", new_text="baz", replace_all=True,
|
||||
)
|
||||
assert "Successfully" in result
|
||||
assert "Patch applied:" in result
|
||||
assert f.read_text() == "baz bar baz bar baz"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -476,5 +476,5 @@ class TestWorkspaceRestriction:
|
||||
old_text="before",
|
||||
new_text="after",
|
||||
)
|
||||
assert "Successfully edited" in result
|
||||
assert "Patch applied:" in result
|
||||
assert target.read_text(encoding="utf-8") == "after\n"
|
||||
Reference in new issue
Block a user