Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
),
]

# Layout filler: characters that repeat because something is being *aligned*, not because
# context is being displaced — aligned table cells, ASCII boxes, horizontal rules, padded
# columns built by UI code. Box drawing is U+2500-U+257F.
_MP2_LAYOUT_FILLER = re.compile(r"^[\s\-=_*.|:+~\u2500-\u257F]+$")

# Above this width the run stops being plausible formatting: nobody reads a table row or an
# ASCII box 400 characters wide, so a repetition that long is reported even if it is filler.
_MP2_LAYOUT_MAX_RUN = 400

# MP2: Context Window Stuffing — filling context to displace content
MP2_PATTERNS = [
(r"(.{2,20}?)\1{20,}", 0.8),
Expand Down Expand Up @@ -186,6 +195,14 @@ def ctx(start: int) -> str:
non_ws_chars = set(captured) - {" ", "\t", "\n", "\r"}
if len(non_ws_chars) <= 1 and not any(c in captured for c in (" ", "\t")):
continue
# Same reasoning as the guard above, extended to the case it misses: a unit made
# only of whitespace and alignment characters repeats because a table, a box or a
# column is being drawn. Runs wider than a readable line are still reported.
if (
_MP2_LAYOUT_FILLER.match(captured)
and len(match.group(0)) < _MP2_LAYOUT_MAX_RUN
):
continue
line_num = get_line_number(content, match.start())
findings.append(
AnalyzerFinding(
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_patterns_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,34 @@ def test_mp2_repeated_pattern(self) -> None:
def test_mp2_separator_not_flagged(self) -> None:
assert not any(f.rule_id == "MP2" for f in mp_mod.analyze("=" * 80, "test.md", "markdown"))

def test_mp2_aligned_markdown_table_is_layout(self) -> None:
# An aligned table repeats spaces and dashes because cells are being lined up, not
# because context is being displaced.
content = (
"| campo | quando | significato |\n"
"| ---------- | ----------------- | ---------------------------------- |\n"
"| id | sempre | identificatore stabile del record |\n"
"| aggiornato | dopo ogni scrittura | timestamp dell'ultima modifica |\n"
)
assert not any(f.rule_id == "MP2" for f in mp_mod.analyze(content, "docs/tabella.md", "markdown"))

def test_mp2_ascii_box_is_layout(self) -> None:
content = "│ └───────────────────────────────────────────────────────┘ │\n"
assert not any(f.rule_id == "MP2" for f in mp_mod.analyze(content, "README.md", "markdown"))

def test_mp2_padded_column_in_code_is_layout(self) -> None:
content = 'lines.append(f"{Colors.DIM}|{Colors.RESET} ")\n'
assert not any(f.rule_id == "MP2" for f in mp_mod.analyze(content, "src/ui.py", "python"))

def test_mp2_whitespace_run_beyond_layout_width_is_still_stuffing(self) -> None:
# Layout stops being a plausible explanation well before this width.
content = "testo" + " " * 800 + "altro testo\n"
assert any(f.rule_id == "MP2" for f in mp_mod.analyze(content, "test.md", "markdown"))

def test_mp2_repeated_content_is_still_stuffing(self) -> None:
# The unit repeated here is content, not alignment: unchanged behaviour.
assert any(f.rule_id == "MP2" for f in mp_mod.analyze("lorem " * 60, "test.md", "markdown"))

@pytest.mark.parametrize(
"content",
[
Expand Down