Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(vocabulary): guard symbol-only headings from crashing init (#2101)
Akshat malik3c12cb098c12
diff --git a/packages/core/src/repowise/core/generation/concept_tree/vocabulary.py b/packages/core/src/repowise/core/generation/concept_tree/vocabulary.pyindex a736e2693..095028429 100644--- a/packages/core/src/repowise/core/generation/concept_tree/vocabulary.py+++ b/packages/core/src/repowise/core/generation/concept_tree/vocabulary.py@@ -188,6 +188,17 @@ def _is_useful(term: str) -> bool:words = term.split()if not (_MIN_TERM_WORDS <= len(words) <= _MAX_TERM_WORDS):return False+ # A term made entirely of separators (hyphens, underscores, spaces) contains+ # no alphanumeric characters. term_words() splits on [\s_\-]+ and filters+ # empty strings, so it returns [] for "---" or "___". Two things then break:+ # • term_words(c.term)[0] in extract_house_terms raises IndexError.+ # • phrase_pattern builds \b\b, which matches at every word boundary in the+ # repository, making the phantom term corroborated by every source file.+ # Rejecting here — before either site sees the term — fixes both at once.+ # \w is not enough: _ is a word character but is also in the word-gap+ # splitter, so "___" has \w characters and still produces an empty word list.+ if not any(c.isalnum() for c in term):+ return Falseif all(w.lower() in _STOPWORDS for w in words):return False# A heading that is entirely boilerplate ("Getting Started") says nothingdiff --git a/tests/unit/generation/test_vocabulary_house_terms.py b/tests/unit/generation/test_vocabulary_house_terms.pyindex b921f3a3f..4899802b0 100644--- a/tests/unit/generation/test_vocabulary_house_terms.py+++ b/tests/unit/generation/test_vocabulary_house_terms.py@@ -926,3 +926,108 @@ def test_excluding_scratch_does_not_cost_a_real_term(repo_with_scratch: Path) ->so it survives losing both leaked citations."""assert "Blast radius" in by_term(repo_with_scratch)+++# ---------------------------------------------------------------------------+# Symbol-only headings must not crash or produce phantom terms (issue #2097)+# ---------------------------------------------------------------------------+#+# A heading made entirely of hyphens, underscores, or symbols that normalise to+# spaces passes every existing _is_useful guard but causes term_words() to return+# []. Two things then break:+#+# • extract_house_terms builds `first_words` with term_words(c.term)[0], which+# raises IndexError on an empty list — the crash reported in #2097.+# • phrase_pattern builds \b\b from an empty word list, which matches at every+# word boundary in the repository, so the phantom term is "corroborated" by+# every source file and passes the code-frequency gate silently.+#+# The fix is in _is_useful: reject any term that contains no alphanumeric+# character. \w is not enough — _ is a word character but is also in the+# word-gap splitter, so "___" still produces an empty word list.+++ "heading",+ [+ "## ---", # the reported case+ "## ___", # underscores only — same crash; missed by a \w-only guard+ "## -_-", # mixed separators — same crash+ "## * --- *", # symbols normalise to spaces, leaving only dashes+ ],+)+def test_symbol_only_heading_does_not_crash_and_produces_no_term(+ heading: str, tmp_path: Path+) -> None:+ """A heading of only separators must produce no term and must not raise.++ Regression for #2097: term_words() returned [] for these headings and+ [0] on an empty list raised IndexError: list index out of range.+ """+ root = tmp_path / "repo"+ root.mkdir()+ (root / "README.md").write_text(+ f"{heading}\n\nSome content here.\n", encoding="utf-8"+ )+ # Must not raise — this was the crash.+ result = extract_house_terms(root)+ # No term whose text is purely separators should survive.+ for term in result:+ assert any(c.isalnum() for c in term.term), (+ f"Separator-only term {term.term!r} reached the output"+ )+++def test_markdown_table_border_does_not_become_a_phantom_term(+ tmp_path: Path,+) -> None:+ """A markdown table-border heading must not become a house term.++ '+------------------+-----------+' passes _is_useful today because '+' is+ not a word-gap character, so term.split() returns ['+', '+', '+'] — a+ non-empty word list that satisfies the length gate. The alphanumeric guard+ added for the crash fix catches this too: '+' contains no alphanumeric+ character, so the whole string is rejected cleanly rather than surviving as+ a phantom term whose first word is '+'.++ Reported by @nail alongside the '---' crash case (#2097).+ """+ root = tmp_path / "repo"+ root.mkdir()+ (root / "README.md").write_text(+ "+------------------+-----------+\n\nSome prose here.\n",+ encoding="utf-8",+ )+ result = extract_house_terms(root)+ for term in result:+ assert any(c.isalnum() for c in term.term), (+ f"Symbol-only term {term.term!r} reached the output"+ )+++def test_normal_heading_with_words_still_produces_a_term(+ tmp_path: Path,+) -> None:+ """A heading containing alphanumeric words must not be rejected by _is_useful.++ Every new test in the #2097 block asserts the *absence* of a bad term.+ An unconditionally-false _is_useful would satisfy all of them. This pins+ the positive case: '## Cache Layer' must still yield 'Cache Layer'.+ """+ root = tmp_path / "repo"+ root.mkdir()+ (root / "README.md").write_text(+ "## Cache Layer\n\nCache Layer stores pre-computed results.\n",+ encoding="utf-8",+ )+ src = root / "src"+ src.mkdir()+ # Spell the term in source so it passes the code-frequency gate.+ (src / "cache.py").write_text(+ '"""Cache Layer implementation."""\n', encoding="utf-8"+ )+ result = extract_house_terms(root)+ terms = [t.term for t in result]+ assert "Cache Layer" in terms, (+ "A normal two-word heading must survive _is_useful and reach the output"+ )