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(init): make --test-run actually limit generation to top 10 files (#1525)

Felix-Ayush3d15d2f2edbe
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
index ea59672a7..2bdc82dc5 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
@@ -265,6 +265,7 @@ def _run_generation_phase(
skip_infra: bool,
embedder_name_resolved: str,
resume: bool,
+ test_run: bool,
) -> tuple[bool, bool]:
"""Run the LLM generation phase for a single-repo init.
@@ -392,6 +393,7 @@ def _run_generation_phase(
embedder_name_resolved=embedder_name_resolved,
resume=resume,
verbose=True,
+ test_run=test_run,
)
return False, False
@@ -1405,6 +1407,7 @@ def init_command(
# file. Resuming against it would skip the entire model run and
# say nothing, so a template wiki is never a run to continue.
resume=resume and _prior_docs_mode != "deterministic",
+ test_run=test_run,
)
if gen_stop:
return
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/generation.py b/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
index d0bae6092..486255217 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/generation.py
@@ -258,6 +258,7 @@ def run_repo_generation(
embedder_name_resolved: str,
resume: bool,
verbose: bool,
+ test_run: bool = False,
) -> list[Any]:
"""Generate wiki pages for one repo and enrich its knowledge graph.
@@ -357,6 +358,7 @@ def run_repo_generation(
if getattr(result, "knowledge_graph_result", None) is not None
else None
),
+ test_run=test_run,
)
)
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
index 1cd7a4b67..8e6fe75c5 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
@@ -164,6 +164,7 @@ def _run_workspace_generation(
embedder_name_resolved=embedder_name_resolved,
resume=resume,
verbose=False,
+ test_run=test_run,
)
diff --git a/packages/core/src/repowise/core/pipeline/orchestrator.py b/packages/core/src/repowise/core/pipeline/orchestrator.py
index a76d6a806..aca4cc77a 100644
--- a/packages/core/src/repowise/core/pipeline/orchestrator.py
+++ b/packages/core/src/repowise/core/pipeline/orchestrator.py
@@ -33,7 +33,7 @@ from repowise.core.pipeline.progress import (
)
from repowise.core.registry import HookProgressCallback
-from .phases._common import _phase_done
+from .phases._common import TEST_RUN_FILE_LIMIT, _phase_done, limit_to_top_pagerank
from .phases.analysis import (
_run_dead_code_analysis,
_run_decision_extraction,
@@ -447,19 +447,12 @@ async def run_pipeline(
f"→ Git: {git_summary.files_indexed:,} files indexed{_hotspot_msg}",
)
- # Test-run: limit to top 10 files by PageRank
+ # Test-run: limit to top 10 files by PageRank (shared helper with
+ # ``run_generation`` so the two paths cannot drift).
if test_run and generate_docs:
- try:
- import networkx as nx
-
- ranks = nx.pagerank(graph_builder.graph())
- except Exception:
- ranks = {}
- parsed_files = sorted(
- parsed_files,
- key=lambda pf: ranks.get(pf.file_info.path, 0),
- reverse=True,
- )[:10]
+ parsed_files = limit_to_top_pagerank(
+ parsed_files, graph_builder, n=TEST_RUN_FILE_LIMIT
+ )
if progress:
progress.on_message("warning", f"Test run: limiting to {len(parsed_files)} files")
diff --git a/packages/core/src/repowise/core/pipeline/phases/_common.py b/packages/core/src/repowise/core/pipeline/phases/_common.py
index 8cdfb4dcd..f512b5e5b 100644
--- a/packages/core/src/repowise/core/pipeline/phases/_common.py
+++ b/packages/core/src/repowise/core/pipeline/phases/_common.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import contextlib
+from typing import Any
from repowise.core.pipeline.progress import ProgressCallback
@@ -17,3 +18,31 @@ def _phase_done(progress: ProgressCallback | None, phase: str) -> None:
if callable(fn):
with contextlib.suppress(Exception):
fn(phase)
+
+
+def limit_to_top_pagerank(
+ parsed_files: list[Any],
+ graph_builder: Any,
+ n: int = 10,
+) -> list[Any]:
+ """Return the *n* highest-PageRank files from *parsed_files*.
+
+ Shared by the orchestrator's in-pipeline generation path and
+ ``run_generation`` (init's separate generation phase), so ``--test-run``
+ cannot drift between two copies of the same truncation.
+ """
+ try:
+ import networkx as nx
+
+ ranks = nx.pagerank(graph_builder.graph())
+ except Exception:
+ ranks = {}
+ return sorted(
+ parsed_files,
+ key=lambda pf: ranks.get(pf.file_info.path, 0),
+ reverse=True,
+ )[:n]
+
+
+#: Default ``--test-run`` file cap (highest-PageRank files).
+TEST_RUN_FILE_LIMIT = 10
diff --git a/packages/core/src/repowise/core/pipeline/phases/generation.py b/packages/core/src/repowise/core/pipeline/phases/generation.py
index 4d2f9bb53..595219cdb 100644
--- a/packages/core/src/repowise/core/pipeline/phases/generation.py
+++ b/packages/core/src/repowise/core/pipeline/phases/generation.py
@@ -16,7 +16,7 @@ from repowise.core.cost_estimator.estimator import STRUCTURAL_PAGE_TYPES
from repowise.core.generation.models import count_stub_fallbacks
from repowise.core.pipeline.progress import ProgressCallback
-from ._common import _phase_done
+from ._common import TEST_RUN_FILE_LIMIT, _phase_done, limit_to_top_pagerank
logger = structlog.get_logger(__name__)
@@ -52,6 +52,7 @@ async def run_generation(
kg_data: dict | None = None,
only_page_ids: set[str] | None = None,
preserved_page_ids: set[str] | None = None,
+ test_run: bool = False,
) -> list[Any]:
"""Run LLM-powered page generation.
@@ -68,6 +69,10 @@ async def run_generation(
``preserved_page_ids`` is an out-parameter filled by a ``resume`` run with
the ids it skipped because a prior run already wrote them. Persistence
needs it to keep those pages out of the stale sweep.
+
+ ``test_run`` limits generation to the top 10 files by PageRank, so a quick
+ validation run can exercise the whole generation path without paying for a
+ full index's worth of pages.
"""
from repowise.core.generation import (
ContextAssembler,
@@ -90,6 +95,18 @@ async def run_generation(
config = replace(base_config, max_concurrency=concurrency)
assembler = ContextAssembler(config)
+ # Test-run: limit to top 10 files by PageRank for a fast validation run.
+ # Applied here rather than only in the orchestrator so it works whether the
+ # pipeline ran with generate_docs=True or generation happened in a later,
+ # separate phase (init's generate_docs=False flow) — the flag's documented
+ # purpose is to cap the *generation* work, and this is where that happens.
+ if test_run:
+ parsed_files = limit_to_top_pagerank(
+ parsed_files, graph_builder, n=TEST_RUN_FILE_LIMIT
+ )
+ if progress:
+ progress.on_message("warning", f"Test run: limiting to {len(parsed_files)} files")
+
# Resolve embedder and vector store
embedder_impl = embedder if embedder is not None else KeylessEmbedder()
diff --git a/tests/unit/pipeline/test_run_generation_test_run.py b/tests/unit/pipeline/test_run_generation_test_run.py
new file mode 100644
index 000000000..47589dcd8
--- /dev/null
+++ b/tests/unit/pipeline/test_run_generation_test_run.py
@@ -0,0 +1,104 @@
+"""``run_generation`` honours ``test_run`` by limiting the file set.
+
+Regression anchor for #1505: ``--test-run`` was forwarded end-to-end but never
+consumed, because the orchestrator's truncation branch was gated on
+``test_run and generate_docs`` and init always runs the pipeline with
+``generate_docs=False``, delegating generation to a later phase. The top-10
+PageRank cap belongs in the generation entry itself, so it works regardless of
+how the pipeline got there.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from repowise.core.pipeline.phases.generation import run_generation
+
+
+class _StubGenerator:
+ """Records the parsed_files it was asked to generate, then returns pages."""
+
+ def __init__(self) -> None:
+ self.parsed_files: list | None = None
+
+ async def generate_all(self, parsed_files, *args, **kwargs) -> list:
+ self.parsed_files = list(parsed_files)
+ return []
+
+
[email protected](autouse=True)
+def _stub_generator(monkeypatch: pytest.MonkeyPatch) -> _StubGenerator:
+ stub = _StubGenerator()
+ monkeypatch.setattr(
+ "repowise.core.generation.PageGenerator",
+ lambda *a, **k: stub,
+ )
+ return stub
+
+
+def _parsed_files(n: int) -> list:
+ files = []
+ for i in range(n):
+ files.append(SimpleNamespace(file_info=SimpleNamespace(path=f"pkg/mod_{i}.py")))
+ return files
+
+
+def _graph_builder_for(paths: list[str]):
+ """A bare GraphBuilder whose graph has a node per file (pagerank on a
+ graph with no edges is uniform, so ranking never drops any file before
+ the cap)."""
+ import networkx as nx
+
+ from repowise.core.ingestion.graph import builder as _builder_mod
+
+ g = nx.DiGraph()
+ for p in paths:
+ g.add_node(p)
+ b = object.__new__(_builder_mod.GraphBuilder)
+ b._graph = g
+ return b
+
+
+async def test_test_run_limits_generation_to_top_10_files(
+ _stub_generator: _StubGenerator, tmp_path: pytest.TempPathFactory
+) -> None:
+ paths = [f"pkg/mod_{i}.py" for i in range(15)]
+ await run_generation(
+ repo_path=tmp_path,
+ parsed_files=_parsed_files(15),
+ source_map={},
+ graph_builder=_graph_builder_for(paths),
+ repo_structure=SimpleNamespace(),
+ git_meta_map={},
+ llm_client=SimpleNamespace(),
+ embedder=None,
+ vector_store=None,
+ concurrency=1,
+ progress=None,
+ test_run=True,
+ )
+ assert _stub_generator.parsed_files is not None
+ assert len(_stub_generator.parsed_files) == 10
+
+
+async def test_no_test_run_generates_every_file(
+ _stub_generator: _StubGenerator, tmp_path: pytest.TempPathFactory
+) -> None:
+ paths = [f"pkg/mod_{i}.py" for i in range(15)]
+ await run_generation(
+ repo_path=tmp_path,
+ parsed_files=_parsed_files(15),
+ source_map={},
+ graph_builder=_graph_builder_for(paths),
+ repo_structure=SimpleNamespace(),
+ git_meta_map={},
+ llm_client=SimpleNamespace(),
+ embedder=None,
+ vector_store=None,
+ concurrency=1,
+ progress=None,
+ )
+ assert _stub_generator.parsed_files is not None
+ assert len(_stub_generator.parsed_files) == 15