One function

fuzzy_find_text in zhayujie/CowAgent

The author described this change as fix(edit): preserve file indentation when oldText is unindented. It counts as a record because the check below fails on the code as it stood at a94f4e3c1 and passes on 93162d2f1, with nothing else changed between the two runs.

Fix saved2026-07-07
Sharing licenceMIT · LICENSE
Change size+15 8

What the code was meant to do, written into the code itself as a docstring

Find text in content, try exact match first, then fuzzy match :param content: Content to search in :param old_text: Text to find :return: Match result

The change

2828 # with collapsed indentation (every untouched line got reformatted).
2929 stripped = old_text.strip('\n')
3030 if stripped.strip():
31+ source_lines = stripped.split('\n')
3132 line_patterns = []
32- for line in stripped.split('\n'):
33+ for i, line in enumerate(source_lines):
3334 tokens = line.split()
34- if tokens:
35- # Tolerate flexible indentation and any run of blanks between
36- # tokens, matching the leniency of the old normalized compare.
37- line_patterns.append(
38- r'[ \t]*' + r'[ \t]+'.join(re.escape(tok) for tok in tokens) + r'[ \t]*'
39- )
40- else:
35+ if not tokens:
4136 line_patterns.append(r'[ \t]*')
37+ continue
38+ # Tolerate any run of blanks between tokens.
39+ core = r'[ \t]+'.join(re.escape(tok) for tok in tokens)
40+ # First-line leading whitespace is folded into the match only when
41+ # old_text itself was indented here; otherwise it stays OUTSIDE the
42+ # match so a no-indent old_text preserves (does not swallow and drop)
43+ # the file's existing indentation -- mirroring an exact substring
44+ # match. Inner lines always tolerate indentation: it sits inside the
45+ # matched region and is re-supplied by new_text.
46+ if i > 0 or line[:1] in (' ', '\t'):
47+ core = r'[ \t]*' + core
48+ line_patterns.append(core + r'[ \t]*')
4249 pattern = '\n'.join(line_patterns)
4350 match = re.search(pattern, content)
4451 if match:

The check that tells the two apart

failpass·tests/test_edit_tool.py::TestEditFuzzyPreservesWhitespace::test_fuzzy_match_with_unindented_oldtext_preserves_file_indent

Check file tests/test_edit_tool.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before ita94f4e3c186fd94081c7a9432fe787db767d4568
Broken version dated2026-07-07
Moduleagent.tools.utils.diff
Units changedfuzzy_find_text
Fingerprintc85a1196cfc9b535
Checked2026-08-17 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.