escape_drift_autocorrect.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """Conservative correction of escape drift before shared fuzzy matching.
  2. Corrections require the corrected ``old_string`` to appear verbatim in the file.
  3. That evidence cannot establish the intended escaping of newly added code: deliberate
  4. double backslashes look the same as a serialization artifact. Backslash correction
  5. therefore halves only ``old_string`` and requires a backslash-free ``new_string``.
  6. An otherwise verified doubled anchor with replacement backslashes raises an explicit
  7. error; callers must reject it before fuzzy matching can apply the ambiguous text.
  8. Quote correction retains its separate consistency checks. Successful corrections
  9. return a note for the tool result; already-exact anchors are left unchanged.
  10. """
  11. from __future__ import annotations
  12. import re
  13. from typing import Optional
  14. _BACKSLASH_RUN_RE = re.compile(r"\\+")
  15. class AmbiguousEscapeDriftError(ValueError):
  16. """The replacement's intentional backslashes cannot be distinguished from drift."""
  17. def _halve_backslash_runs(s: str) -> str:
  18. """Replace every maximal run of backslashes with half its length."""
  19. return _BACKSLASH_RUN_RE.sub(lambda m: "\\" * (len(m.group(0)) // 2), s)
  20. def _all_backslash_runs_even(s: str) -> bool:
  21. return all(len(run) % 2 == 0 for run in _BACKSLASH_RUN_RE.findall(s))
  22. def maybe_correct_backslash_doubling(old_string: str, new_string: str, content: str) -> tuple[str, str]:
  23. """Halve a verified doubled anchor only when the replacement has no backslashes.
  24. Every old_string run must be even and its halved form must occur in the file.
  25. Replacement backslashes are ambiguous regardless of run length, so raise
  26. ``AmbiguousEscapeDriftError`` rather than let fuzzy matching apply them.
  27. """
  28. if "\\" not in old_string or old_string in content:
  29. return old_string, new_string
  30. if not _all_backslash_runs_even(old_string):
  31. return old_string, new_string
  32. halved_old = _halve_backslash_runs(old_string)
  33. if halved_old not in content:
  34. return old_string, new_string
  35. if "\\" in new_string:
  36. raise AmbiguousEscapeDriftError(
  37. "Escape-drift detected: halving old_string's backslashes matches the file, "
  38. "but correcting new_string could remove intentional backslashes. "
  39. "Re-read the file and resend old_string/new_string with their intended escaping."
  40. )
  41. return halved_old, new_string
  42. _QUOTE_WITH_BACKSLASHES_RE = {q: re.compile(r"(\\*)" + q) for q in ("'", '"')}
  43. def _strip_spurious_quote_escapes(s: str) -> str:
  44. """Un-escape a spurious backslash before a quote character: ``\\'`` -> ``'``, ``\\"`` -> ``"``."""
  45. return s.replace("\\'", "'").replace('\\"', '"')
  46. def _quote_escapes_all_spurious(*strings: str) -> bool:
  47. """True when every escaped quote in ``strings`` is attributable to the wire artifact.
  48. The artifact escapes quotes mechanically, so a quote kind it touched is escaped at EVERY
  49. occurrence across the whole call, by exactly one backslash. A backslash the model meant
  50. (``"say \\"hi\\""`` in new code) went through the same extra escape and therefore arrives
  51. as two or more backslashes before the quote, never one. So for each kind that appears as
  52. ``\\q`` anywhere: every ``q`` in every string must be preceded by exactly one backslash.
  53. A bare ``q`` beside a ``\\q`` (the kind was not drifted, so the ``\\q`` is real) or a longer
  54. run (an intended escape) cannot be told apart from the artifact, so it refuses.
  55. """
  56. for quote, pattern in _QUOTE_WITH_BACKSLASHES_RE.items():
  57. if not any("\\" + quote in s for s in strings):
  58. continue
  59. if any(len(m.group(1)) != 1 for s in strings for m in pattern.finditer(s)):
  60. return False
  61. return True
  62. def maybe_correct_quote_escaping(old_string: str, new_string: str, content: str) -> tuple[str, str]:
  63. """Return a corrected ``(old_string, new_string)`` if a spurious backslash-before-quote wire
  64. artifact is detected AND verified against the file's actual content; otherwise unchanged.
  65. Mirrors ``_detect_escape_drift``'s quote-suspect check: ``\\'`` or ``\\"`` present in BOTH
  66. old_string and new_string is the signature of an apostrophe/quote that picked up a spurious
  67. backslash in transit, not a literal escape the model intended. The stripped old_string must
  68. appear verbatim in content, which verifies old_string (and rules out a file that really
  69. contains ``\\'``/``\\"``). new_string's added code is not in the file, so it is only
  70. stripped when ``_quote_escapes_all_spurious`` holds for the whole call; otherwise the call
  71. is left unchanged for the existing reject-and-ask guard. Raises ``AmbiguousEscapeDriftError``
  72. when new_string escapes a quote kind that old_string does not.
  73. """
  74. has_quote_suspect = (
  75. ("\\'" in old_string and "\\'" in new_string)
  76. or ('\\"' in old_string and '\\"' in new_string)
  77. )
  78. if not has_quote_suspect or old_string in content:
  79. return old_string, new_string
  80. if not _quote_escapes_all_spurious(old_string, new_string):
  81. return old_string, new_string
  82. stripped_old = _strip_spurious_quote_escapes(old_string)
  83. if stripped_old not in content:
  84. return old_string, new_string
  85. # Only old_string is checked against the file, so only the quote kinds it escapes are
  86. # proven drift. A kind escaped only in new_string may be an escape the model meant.
  87. unproven = [q for q in ("'", '"') if "\\" + q in new_string and "\\" + q not in old_string]
  88. if unproven:
  89. shown = " and ".join("\\" + q for q in unproven)
  90. raise AmbiguousEscapeDriftError(
  91. "Escape-drift detected: removing the spurious quote escapes in old_string matches "
  92. f"the file, but new_string also contains {shown}, which old_string gives no evidence "
  93. "for. Re-read the file and resend old_string/new_string with their intended escaping."
  94. )
  95. return stripped_old, _strip_spurious_quote_escapes(new_string)
  96. def maybe_correct_escape_drift(old_string: str, new_string: str, content: str) -> tuple[str, str, Optional[str]]:
  97. """Apply whichever escape-drift auto-correction (if any) is verified against the file's
  98. actual content and report which one fired.
  99. Tries quote escaping, then backslash correction of the anchor only. Raises
  100. ``AmbiguousEscapeDriftError`` for a verified doubled anchor whose replacement
  101. contains backslashes, or a verified quote correction whose replacement escapes a
  102. quote kind the anchor does not; the caller must return an error without matching.
  103. Returns ``(corrected_old, corrected_new, note)``: ``note`` is a human-readable description
  104. of the correction that was applied, or ``None`` if neither fired. The caller attaches
  105. ``note`` to the successful result (e.g. ``PatchResult.note``) so a corrected call stays
  106. distinguishable from one whose arguments never needed correction.
  107. """
  108. corrected_old, corrected_new = maybe_correct_quote_escaping(old_string, new_string, content)
  109. if corrected_old != old_string:
  110. return corrected_old, corrected_new, (
  111. "escape-drift auto-corrected: stripped a spurious backslash before a quote/apostrophe "
  112. "in old_string/new_string (a JSON-double-escaping wire artifact) before matching."
  113. )
  114. corrected_old, corrected_new = maybe_correct_backslash_doubling(old_string, new_string, content)
  115. if corrected_old != old_string:
  116. return corrected_old, corrected_new, (
  117. "escape-drift auto-corrected: halved doubled backslash runs in old_string "
  118. "before matching; new_string has no backslashes and was left unchanged."
  119. )
  120. return old_string, new_string, None