The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(tools): cover escape-drift auto-correct and its refusals
Elisa Martinez Abad4c56e9da57e4
diff --git a/tests/tools/test_escape_drift_autocorrect.py b/tests/tools/test_escape_drift_autocorrect.pynew file mode 100644index 00000000000..80e436f4944--- /dev/null+++ b/tests/tools/test_escape_drift_autocorrect.py@@ -0,0 +1,90 @@+"""Tests for escape-drift auto-correction on ``patch`` (tools/escape_drift_autocorrect.py).++Contract: a call whose arguments were escaped one extra time on the wire is un-escaped only when+old_string is verified against the file AND new_string is uniformly escaped; a backslash the+model meant in new code is never removed, and ``note`` reports exactly when a correction fired.+"""++import json++import pytest++from tools.escape_drift_autocorrect import maybe_correct_escape_drift+++def workdir(tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))+ return tmp_path+++def _patch_tool(**kwargs):+ from tools.file_tools import patch_tool+ return json.loads(patch_tool(**kwargs))+++# (file content, drifted old_string, new_string that also adds a legitimately escaped line)+_MIXED_CALLS = {+ "backslash_new_line_single_escaped": (+ 'pat = re.compile(r"\\d+")\n',+ r'pat = re.compile(r"\\d+")',+ 'pat = re.compile(r"\\\\d+")\nprint("done\\n")',+ ),+ "quote_new_line_really_escaped": (+ "x = 'it'\n",+ r"x = \'it\'",+ r"x = \'it\'" + "\n" + r'y = "say \"hi\""',+ ),+}++[email protected]("content,old,new", _MIXED_CALLS.values(), ids=_MIXED_CALLS.keys())+def test_mixed_escaping_is_not_corrected(content, old, new):+ assert maybe_correct_escape_drift(old, new, content) == (old, new, None)+++class TestPatchEndToEnd:+ def test_backslash_mixed_call_rejected_not_mangled(self, workdir):+ # Two runs so the existing doubling guard has a non-trivial signal and rejects.+ f = workdir / "a.py"+ original = 'pat = re.compile(r"\\d+")\nsep = r"a\\b"\n'+ f.write_text(original)+ r = _patch_tool(path=str(f), old_string=r'pat = re.compile(r"\\d+")' + "\n" + r'sep = r"a\\b"',+ new_string=r'pat = re.compile(r"\\w+")' + "\n" + r'sep = r"a\\b"' + "\n" + r'print("done\n")',+ task_id="t-drift")+ assert r.get("success") is not True+ assert f.read_text() == original++ def test_quote_mixed_call_rejected_not_mangled(self, workdir):+ content, old, new = _MIXED_CALLS["quote_new_line_really_escaped"]+ f = workdir / "b.py"+ f.write_text(content)+ r = _patch_tool(path=str(f), old_string=old, new_string=new, task_id="t-drift")+ assert r.get("success") is not True+ assert f.read_text() == content++ @pytest.mark.parametrize("content,old,new,expected", [+ ('pat = re.compile(r"\\d+")\n', r'pat = re.compile(r"\\d+")', r'pat = re.compile(r"\\w+")',+ 'pat = re.compile(r"\\w+")\n'),+ ("x = 'it'\n", r"x = \'it\'", r"x = \'its\'" + "\n" + r"y = \'ok\'",+ "x = 'its'\ny = 'ok'\n"),+ ], ids=["backslash_doubled", "quote_escaped"])+ def test_uniformly_drifted_call_is_corrected_with_note(self, workdir, content, old, new, expected):+ f = workdir / "c.py"+ f.write_text(content)+ r = _patch_tool(path=str(f), old_string=old, new_string=new, task_id="t-drift")+ assert r["success"] is True+ assert "escape-drift auto-corrected" in r["note"]+ assert f.read_text() == expected++ @pytest.mark.parametrize("content,old,new", [+ ('s = "a\\\\b"\n', r's = "a\\b"', r's = "a\\c"'),+ ("s = 'it\\'s'\n", r"s = 'it\'s'", r"s = 'it\'s ok'"),+ ], ids=["real_double_backslash", "real_escaped_quote"])+ def test_file_really_containing_escapes_is_left_alone(self, workdir, content, old, new):+ f = workdir / "d.py"+ f.write_text(content)+ r = _patch_tool(path=str(f), old_string=old, new_string=new, task_id="t-drift")+ assert r["success"] is True+ assert r.get("note") is None+ assert f.read_text() == content.replace(old, new)