Skip to content

Commit 7aa29c6

Browse files
antsokclaude
andcommitted
Python: Strip the whole line terminator before matching in file_access_grep
Ports the fix for the same defect found by review on the .NET side (microsoft#7671). _search_file_content removed only the trailing "\n" before matching, so on a CRLF file the pattern was applied to text such as "beta match\r" and an end-anchored pattern like "match$" failed even though the line's text is exactly "beta match". The terminator is not part of the line's text, so it is stripped in full now. The per-line offset had to move with it: it advanced by len(scanned) + 1, which was only correct while scanned still carried the "\r". It now advances by len(line), whose terminator is already included, keeping the snippet anchored at the match. Also drops a stale claim in _split_lines_keepends' docstring, which still said it reproduced _search_file_content's content.split("\n") — that dependency now runs the other way round. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d97afaa commit 7aa29c6

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎python/packages/core/agent_framework/_harness/_file_access.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ def _apply_replace(content: str, old_string: str, new_string: str, replace_all:
235235
def _split_lines_keepends(content: str) -> list[str]:
236236
r"""Split ``content`` into lines on ``\n`` only, keeping the terminator attached.
237237
238-
Splits solely on ``\n`` (a trailing ``\r`` stays as line content), reproducing
239-
:func:`_search_file_content`'s ``content.split("\n")`` enumeration exactly, so a
240-
``line_number`` obtained from ``grep`` always targets the same line here and stays
241-
in range. This means the result has ``len(content.split("\n"))`` elements: a
238+
This is the single definition of a line shared by ``grep``, ``read_lines`` and
239+
``replace_lines``, so a ``line_number`` obtained from one always targets the same
240+
line in the others and stays in range. Splitting solely on ``\n`` (a trailing
241+
``\r`` stays attached to the line) means the result has
242242
trailing ``\n`` yields a final empty (editable) line, and empty content yields a
243243
single empty line. ``"".join(...)`` reproduces ``content`` verbatim.
244244
"""
@@ -510,7 +510,8 @@ def _search_file_content(file_name: str, content: str, regex: re.Pattern[str]) -
510510
Lines are split by :func:`_split_lines_keepends` and reported verbatim, terminator
511511
included, so a match can be fed straight back to ``replace_lines`` as a ``new_line``
512512
without losing a ``\r\n``. The pattern is matched against the line without its
513-
trailing ``\n``, so ``^`` and ``$`` anchor to the line as before. A snippet of up to
513+
trailing terminator, so ``$`` anchors to the end of the line's text even on a CRLF
514+
file. A snippet of up to
514515
``±_SEARCH_SNIPPET_RADIUS`` characters around the first match is included. Returns
515516
``None`` when no lines match.
516517
"""
@@ -520,7 +521,7 @@ def _search_file_content(file_name: str, content: str, regex: re.Pattern[str]) -
520521
line_start_offset = 0
521522

522523
for line_number, line in enumerate(lines, start=1):
523-
scanned = line.removesuffix("\n")
524+
scanned = line.removesuffix("\n").removesuffix("\r")
524525
match = regex.search(scanned)
525526
if match is not None:
526527
matching_lines.append(FileSearchMatch(line_number=line_number, line=line))
@@ -529,8 +530,8 @@ def _search_file_content(file_name: str, content: str, regex: re.Pattern[str]) -
529530
snippet_start = max(0, char_index - _SEARCH_SNIPPET_RADIUS)
530531
snippet_end = min(len(content), char_index + (match.end() - match.start()) + _SEARCH_SNIPPET_RADIUS)
531532
first_snippet = content[snippet_start:snippet_end]
532-
# Advance past this line and the implied '\n' separator.
533-
line_start_offset += len(scanned) + 1
533+
# Advance past this line; its terminator is already part of its length.
534+
line_start_offset += len(line)
534535

535536
if not matching_lines:
536537
return None

‎python/packages/core/tests/core/test_harness_file_access.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from agent_framework._filesystem import is_link_or_reparse_point
3232
from agent_framework._harness import _file_access as _file_access_module
3333
from agent_framework._harness._file_access import (
34+
_SEARCH_SNIPPET_RADIUS,
3435
DEFAULT_FILE_ACCESS_INSTRUCTIONS,
3536
DEFAULT_FILE_ACCESS_SOURCE_ID,
3637
_matches_glob,
@@ -1325,6 +1326,41 @@ async def test_file_access_grep_reports_lines_verbatim(chat_client_base: Support
13251326
assert _text((await read.invoke(arguments={"file_name": "a.txt"}))[0]) == "alpha\r\nBETA\r\n"
13261327

13271328

1329+
async def test_file_access_grep_end_anchored_pattern_matches_crlf_line(
1330+
chat_client_base: SupportsChatGetResponse,
1331+
) -> None:
1332+
r"""``$`` anchors to the end of the line's text, not to the ``\r`` of a CRLF terminator."""
1333+
tools = await _prepare_access_tools(chat_client_base)
1334+
save = _tool_by_name(tools, "file_access_write")
1335+
grep = _tool_by_name(tools, "file_access_grep")
1336+
1337+
await save.invoke(arguments={"file_name": "a.txt", "content": "alpha\r\nbeta match\r\n", "overwrite": True})
1338+
found = await grep.invoke(arguments={"regex_pattern": "match$", "glob_pattern": "a.txt"})
1339+
1340+
hits = json.loads(_text(found[0]))[0]["matching_lines"]
1341+
assert [hit["line_number"] for hit in hits] == [2]
1342+
assert hits[0]["line"] == "beta match\r\n"
1343+
1344+
1345+
async def test_file_access_grep_snippet_is_anchored_at_the_match(
1346+
chat_client_base: SupportsChatGetResponse,
1347+
) -> None:
1348+
"""The per-line offset must count the terminator, or every snippet drifts."""
1349+
tools = await _prepare_access_tools(chat_client_base)
1350+
save = _tool_by_name(tools, "file_access_write")
1351+
grep = _tool_by_name(tools, "file_access_grep")
1352+
1353+
# The first line is long enough that the snippet window is not clamped to the start of the file.
1354+
first_line = f"{'x' * 60}\r\n"
1355+
content = f"{first_line}needle\r\n"
1356+
await save.invoke(arguments={"file_name": "a.txt", "content": content, "overwrite": True})
1357+
found = await grep.invoke(arguments={"regex_pattern": "needle", "glob_pattern": "a.txt"})
1358+
1359+
# The match starts right after the first line, so the window opens _SEARCH_SNIPPET_RADIUS before it.
1360+
snippet = json.loads(_text(found[0]))[0]["snippet"]
1361+
assert snippet == content[len(first_line) - _SEARCH_SNIPPET_RADIUS :]
1362+
1363+
13281364
async def test_file_access_grep_line_numbers_are_editable(chat_client_base: SupportsChatGetResponse) -> None:
13291365
"""A ``line_number`` returned by ``file_access_grep`` must be in range for ``replace_lines``.
13301366

0 commit comments

Comments
 (0)