fix(dedup): bound line-number digit run and never fold into a longer pointer

A leading digit run of 4,301+ digits made int() raise ValueError, so
dedup_blocks skipped dedup for the whole request. Bound the run at 18
digits; a longer run is not a line number.

A fold could also be longer than the span it replaced (40-char 3-line
span -> 43-char pointer). Fold only when the pointer is shorter.

Refs #4478 (items 1 and 2 only; items 3 and 4 stay open)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-10-26 15:43:49 +07:00
co-authored by Claude Sonnet 5.5
parent 3fef15d376
commit 7dc7cfc04e
2 changed files with 35 additions and 4 deletions
+8 -4
View File
@@ -63,7 +63,11 @@ MAX_ANCHOR_CANDIDATES = 16
# fold only on an EXACT match (delta 0) — the "prefer false negatives" posture.
# Real grep -n / sed -n / rg -n numbers never carry a leading zero, so the
# intended renumber-fold feature is unaffected.
_LINENO_RE = re.compile(r"^([1-9]\d*)(:|\t)(.*)$", re.DOTALL)
#
# ``\d{0,17}`` bounds the run at 18 digits: Python refuses ``int()`` on a run of
# 4,301+ digits (ValueError), which would abort dedup for the whole request. A
# longer run is not a real line number, so the line stays non-numbered.
_LINENO_RE = re.compile(r"^([1-9]\d{0,17})(:|\t)(.*)$", re.DOTALL)
def _num_and_key(line: str) -> tuple[int | None, str, str]:
@@ -264,9 +268,9 @@ def dedup_blocks(
if m is not None and m[0] >= min_lines:
span = lines[i : i + m[0]]
span_text = "\n".join(span)
if len(span_text) >= min_chars:
ref_turn = blocks[m[1]].turn
ptr = _pointer(span, ref_turn, m[3])
ptr = _pointer(span, blocks[m[1]].turn, m[3])
# Never fold when the pointer is not shorter than the span.
if len(span_text) >= min_chars and len(ptr) < len(span_text):
out.append(ptr)
# Folded span is NOT verbatim in this block's output:
# mark None so it can't seed a later contiguous match,
+27
View File
@@ -472,3 +472,30 @@ def test_apply_recoverable_default_and_true_still_fold():
true_out = _apply_with_recoverable(msgs, recoverable=True)
assert "[↑" in true_out[2]["content"] # explicit recoverable -> folds
def _five_numbered(digits):
return "\n".join("1" * digits + f":alpha line {i} of the file contents" for i in range(5))
def test_long_digit_run_does_not_disable_dedup():
# int() raises ValueError on 4,301+ digits; that must not skip dedup.
for digits in (10, 4300, 4301):
t = _five_numbered(digits)
out, stats = dedup_blocks([_blk(t, 1), _blk(t, 2)])
assert stats["spans_folded"] == 1, digits
assert out[1].text != t
def test_fold_never_longer_than_span():
# A 40-char 3-line span would fold into a 43-char pointer: leave it alone.
t = "a" * 21 + "\n" + "b" * 9 + "\n" + "c" * 8
assert len(t) == 40
out, stats = dedup_blocks([_blk(t, 1), _blk(t, 2)])
assert stats["spans_folded"] == 0
assert out[1].text == t
# A long span still folds.
long_t = _code("", 5)
out, stats = dedup_blocks([_blk(long_t, 1), _blk(long_t, 2)])
assert stats["spans_folded"] == 1
assert len(out[1].text) < len(long_t)