Skip to content

The agent that grows with you

fix(tools): only auto-correct escape drift when both strings are consistent

Elisa Martinez Abadbc26e5caa9ea
diff --git a/tools/escape_drift_autocorrect.py b/tools/escape_drift_autocorrect.py
index 80a61c8e0df..01af3d9f398 100644
--- a/tools/escape_drift_autocorrect.py
+++ b/tools/escape_drift_autocorrect.py
@@ -13,7 +13,11 @@ that deliberately sent genuinely-escaped content (a correctly backslash-doubled
regex/shell string, or a source file that legitimately contains the two-character sequence
``\\'``/``\\"``) would need the corrected form to coincidentally already exist at the same spot
in the file for either correction to apply, which the checks below rule out; a wrong guess
-simply fails verification and falls through to the existing reject-and-ask guard unchanged. An
+simply fails verification and falls through to the existing reject-and-ask guard unchanged.
+That verification covers ``old_string`` only: the lines ``new_string`` adds are not in the file,
+so each correction also requires ``new_string`` to be uniformly escaped (every backslash run
+even; every escaped quote escaped by exactly one backslash) and otherwise makes no correction,
+since un-escaping a backslash the model meant in new code would corrupt the file silently. An
earlier version of the backslash-doubling correction gated it to a model-name substring match;
that gate was removed because the substring check turned out to be unreliable in practice (the
resolved "model" value at the check site did not reliably match the serving model's real name),
@@ -37,48 +41,76 @@ _BACKSLASH_RUN_RE = re.compile(r"\\+")
def _halve_backslash_runs(s: str) -> str:
- """Replace every maximal run of backslashes with half its length (floor).
-
- Correctness does not depend on this being exactly right for every input -- the caller
- only accepts the result after verifying it appears verbatim in the actual file content,
- so a wrong guess is simply discarded, never applied.
- """
+ """Replace every maximal run of backslashes with half its length."""
return _BACKSLASH_RUN_RE.sub(lambda m: "\\" * (len(m.group(0)) // 2), s)
+def _all_backslash_runs_even(s: str) -> bool:
+ return all(len(run) % 2 == 0 for run in _BACKSLASH_RUN_RE.findall(s))
+
+
def maybe_correct_backslash_doubling(old_string: str, new_string: str, content: str) -> Tuple[str, str]:
"""Return a corrected ``(old_string, new_string)`` if a doubled-backslash wire artifact is
detected AND verified against the file's actual content; otherwise return them unchanged.
- Verification requirement: the halved ``old_string`` must appear verbatim in ``content``.
- This is what makes the correction safe to apply automatically -- a wrong guess simply
- fails verification and falls through to the existing reject-and-ask guard unchanged.
+ Two requirements, one per string. The halved ``old_string`` must appear verbatim in
+ ``content`` -- that verifies old_string. Nothing in the file can verify new_string (its
+ new lines are, by definition, not in the file yet), so it is checked structurally: an
+ extra escape doubles EVERY backslash, so every run in both strings must have even length.
+ An odd run (``print("done\\n")`` added alongside a doubled old_string) means that string
+ was not double-escaped; halving it would silently delete a real backslash, so the call is
+ left unchanged for the existing reject-and-ask guard.
"""
if "\\" not in old_string or old_string in content:
return old_string, new_string
+ if not (_all_backslash_runs_even(old_string) and _all_backslash_runs_even(new_string)):
+ return old_string, new_string
halved_old = _halve_backslash_runs(old_string)
- if halved_old == old_string or halved_old not in content:
+ if halved_old not in content:
return old_string, new_string
return halved_old, _halve_backslash_runs(new_string)
+_QUOTE_WITH_BACKSLASHES_RE = {q: re.compile(r"(\\*)" + q) for q in ("'", '"')}
+
+
def _strip_spurious_quote_escapes(s: str) -> str:
"""Un-escape a spurious backslash before a quote character: ``\\'`` -> ``'``, ``\\"`` -> ``"``."""
return s.replace("\\'", "'").replace('\\"', '"')
+def _quote_escapes_all_spurious(*strings: str) -> bool:
+ """True when every escaped quote in ``strings`` is attributable to the wire artifact.
+
+ The artifact escapes quotes mechanically, so a quote kind it touched is escaped at EVERY
+ occurrence across the whole call, by exactly one backslash. A backslash the model meant
+ (``"say \\"hi\\""`` in new code) went through the same extra escape and therefore arrives
+ as two or more backslashes before the quote, never one. So for each kind that appears as
+ ``\\q`` anywhere: every ``q`` in every string must be preceded by exactly one backslash.
+ A bare ``q`` beside a ``\\q`` (the kind was not drifted, so the ``\\q`` is real) or a longer
+ run (an intended escape) cannot be told apart from the artifact, so it refuses.
+ """
+ for quote, pattern in _QUOTE_WITH_BACKSLASHES_RE.items():
+ if not any("\\" + quote in s for s in strings):
+ continue
+ if any(len(m.group(1)) != 1 for s in strings for m in pattern.finditer(s)):
+ return False
+ return True
+
+
def maybe_correct_quote_escaping(old_string: str, new_string: str, content: str) -> Tuple[str, str]:
"""Return a corrected ``(old_string, new_string)`` if a spurious backslash-before-quote wire
artifact is detected AND verified against the file's actual content; otherwise unchanged.
Mirrors ``_detect_escape_drift``'s quote-suspect check: ``\\'`` or ``\\"`` present in BOTH
- old_string and new_string is the signature of a JSON-double-escaping artifact (an
- apostrophe/quote that picked up a spurious backslash in transit), not a literal escape the
- model intended. A source file legitimately containing the two-character sequence ``\\'``/
- ``\\"`` verbatim is the case this guards against; the verification step below (the stripped
- old_string must appear in content) is what makes stripping safe regardless.
+ old_string and new_string is the signature of an apostrophe/quote that picked up a spurious
+ backslash in transit, not a literal escape the model intended. The stripped old_string must
+ appear verbatim in content, which verifies old_string (and rules out a file that really
+ contains ``\\'``/``\\"``). new_string's added code is not in the file, so it is only
+ stripped when ``_quote_escapes_all_spurious`` holds for the whole call; otherwise the call
+ is left unchanged for the existing reject-and-ask guard.
"""
has_quote_suspect = (
("\\'" in old_string and "\\'" in new_string)
@@ -86,9 +118,11 @@ def maybe_correct_quote_escaping(old_string: str, new_string: str, content: str)
)
if not has_quote_suspect or old_string in content:
return old_string, new_string
+ if not _quote_escapes_all_spurious(old_string, new_string):
+ return old_string, new_string
stripped_old = _strip_spurious_quote_escapes(old_string)
- if stripped_old == old_string or stripped_old not in content:
+ if stripped_old not in content:
return old_string, new_string
return stripped_old, _strip_spurious_quote_escapes(new_string)