Skip to main content

Module fuzzy_match

Module fuzzy_match 

Source
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):

  1. exact
  2. line_trimmed — strip leading/trailing ws per line
  3. whitespace_normalized — collapse runs of spaces/tabs
  4. indentation_flexible — lstrip per line (the most common LLM mistake)
  5. escape_normalized — \n/\t/\r escape sequences → real chars
  6. trimmed_boundary — trim only first and last lines
  7. unicode_normalized — smart quotes, em/en-dash, ellipsis, NBSP
  8. 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§

FuzzyMatch
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.