Expand description
Multi-strategy fuzzy matching for edit_file.
LLMs frequently produce old_string arguments that don’t byte-exactly match
the file contents — wrong indentation, smart quotes pasted from a doc,
collapsed whitespace, literal \n instead of newlines, etc. This module
tries an ordered chain of increasingly-fuzzy strategies, returning the
first that matches uniquely. The caller learns which strategy fired so it
can surface that to the model (and the user).
Hermes reference: tools/fuzzy_match.py (originally from OpenCode).
Strategies (in order):
- exact
- line_trimmed — strip leading/trailing ws per line
- whitespace_normalized — collapse runs of spaces/tabs
- indentation_flexible — lstrip per line (the most common LLM mistake)
- escape_normalized —
\n/\t/\rescape sequences → real chars - trimmed_boundary — trim only first and last lines
- unicode_normalized — smart quotes, em/en-dash, ellipsis, NBSP
- block_anchor — first+last line anchor + middle similarity
context_aware (50% line similarity) is intentionally NOT ported — it’s
too loose and produces dangerous false positives.
Structs§
- Fuzzy
Match - Result of a fuzzy find/replace attempt.
Functions§
- fuzzy_
find_ and_ replace - Try the strategy chain. Returns Ok if exactly one match was found via some strategy. Returns Err with a human-readable explanation on no match, multi-match, or escape-drift.