Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): return flagged analyses in source order (#3294)

ump45nose5a0c37323eaf
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/facts.py b/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
index 8d4c6c9c5..307896f6f 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
@@ -223,6 +223,9 @@ class FileDataflow:
analysis = self._analysis_for(idx, fn_node)
if analysis is not None:
out.append(analysis)
+ # ``_functions()`` visits siblings last to first; restore source order.
+ # The sort is stable, so same-line functions keep collection order.
+ out.sort(key=lambda a: a.start_line)
return out
def analyses_covering(self, lines: Iterable[int]) -> list[FunctionAnalysis]:
diff --git a/tests/unit/health/test_dataflow_facts.py b/tests/unit/health/test_dataflow_facts.py
index 20193c795..9459d5ee0 100644
--- a/tests/unit/health/test_dataflow_facts.py
+++ b/tests/unit/health/test_dataflow_facts.py
@@ -249,6 +249,20 @@ def test_one_parse_when_promotion_and_extract_method_hit_same_file(tmp_path: Pat
assert calls["n"] == 1
+def test_flagged_analyses_are_in_source_order():
+ _require_python()
+
+ def fn(name: str) -> str:
+ body = "".join(f" if x == {i}:\n y = {i}\n" for i in range(10))
+ return f"def {name}(x):\n y = 0\n{body} return y\n\n"
+
+ source = (fn("first") + fn("second") + fn("third")).encode()
+ flagged = FileDataflow("a.py", "python", source=source).flagged_analyses()
+
+ assert [a.name for a in flagged] == ["first", "second", "third"]
+ assert [a.start_line for a in flagged] == sorted(a.start_line for a in flagged)
+
+
def test_cache_construction_does_not_touch_the_file(tmp_path: Path):
fd = FileDataflow(str(tmp_path / "missing.py"), "python")
# Nothing read or parsed yet; a consumer call on a missing file is silent.