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
refactor(generation): validate evidence files in one place (#1251)
Sergei Iakhnitskii38dd5543b45a
diff --git a/packages/core/src/repowise/core/generation/models.py b/packages/core/src/repowise/core/generation/models.pyindex 8a112450e..7317ca6fb 100644--- a/packages/core/src/repowise/core/generation/models.py+++ b/packages/core/src/repowise/core/generation/models.py@@ -68,6 +68,40 @@ def _source_evidence_page_keys() -> set[str]:}+def _normalize_evidence_files(+ raw_files: Mapping[str, Any], *, label: str+) -> dict[str, tuple[str, ...]]:+ """Validate a page-key -> paths mapping, framing errors with *label*.++ Shared by ``from_repo_config`` (label ``generation_context.files``, the key+ the user wrote) and ``__post_init__`` (label ``source_evidence_files``, the+ internal field for direct construction). The two labels are the whole point+ of the parameter: the same rules, reported against the surface the caller+ touched.++ Sharing one validator also unifies key normalization: both paths now+ ``str(...).strip()`` a key before the membership check. ``from_repo_config``+ always did; direct construction did not, so a padded key like+ ``" repo_overview "`` is now trimmed and accepted where it used to raise.+ """+ valid_page_keys = _source_evidence_page_keys()+ result: dict[str, tuple[str, ...]] = {}+ for raw_page_key, raw_paths in raw_files.items():+ page_key = str(raw_page_key).strip()+ if page_key not in valid_page_keys:+ raise ValueError(+ f"{label} keys must name repo_overview or a "+ "model-written onboarding slot; project_overview is configured "+ "as repo_overview"+ )+ if not isinstance(raw_paths, (list, tuple)) or not all(+ isinstance(path, str) and path.strip() for path in raw_paths+ ):+ raise ValueError(f"{label}.{page_key} must be a list of file paths")+ result[page_key] = tuple(path.strip() for path in raw_paths)+ return result++class _FrozenEvidenceFiles(Mapping[str, tuple[str, ...]]):"""Small immutable mapping that preserves the frozen config contract.@@ -322,24 +356,9 @@ class GenerationConfig:raw_files = raw_evidence.get("files", {})if not isinstance(raw_files, Mapping):raise ValueError("generation_context.files must be a mapping")- valid_page_keys = _source_evidence_page_keys()- evidence_files: dict[str, tuple[str, ...]] = {}- for raw_page_key, raw_paths in raw_files.items():- page_key = str(raw_page_key).strip()- if page_key not in valid_page_keys:- raise ValueError(- "generation_context.files keys must name repo_overview or a "- "model-written onboarding slot; project_overview is configured "- "as repo_overview"- )- if not isinstance(raw_paths, (list, tuple)) or not all(- isinstance(path, str) and path.strip() for path in raw_paths- ):- raise ValueError(- f"generation_context.files.{page_key} must be a list of file paths"- )- evidence_files[page_key] = tuple(path.strip() for path in raw_paths)- values["source_evidence_files"] = evidence_files+ values["source_evidence_files"] = _normalize_evidence_files(+ raw_files, label="generation_context.files"+ )values.update(overrides)return cls(**values)@@ -361,20 +380,9 @@ class GenerationConfig:raise ValueError("source_evidence_token_budget must be a non-negative integer")if not isinstance(self.source_evidence_files, Mapping):raise ValueError("source_evidence_files must be a mapping")- valid_page_keys = _source_evidence_page_keys()- evidence_files: dict[str, tuple[str, ...]] = {}- for page_key, paths in self.source_evidence_files.items():- if page_key not in valid_page_keys:- raise ValueError(- "source_evidence_files keys must name repo_overview or a "- "model-written onboarding slot; project_overview is configured "- "as repo_overview"- )- if not isinstance(paths, (list, tuple)) or not all(- isinstance(path, str) and path.strip() for path in paths- ):- raise ValueError(f"source_evidence_files.{page_key} must be a list of file paths")- evidence_files[page_key] = tuple(path.strip() for path in paths)+ evidence_files = _normalize_evidence_files(+ self.source_evidence_files, label="source_evidence_files"+ )object.__setattr__(self,"source_evidence_files",diff --git a/tests/unit/generation/test_models.py b/tests/unit/generation/test_models.pyindex 3247e8d94..5e4bd60f6 100644--- a/tests/unit/generation/test_models.py+++ b/tests/unit/generation/test_models.py@@ -279,6 +279,24 @@ def test_direct_generation_config_rejects_an_unconsumed_evidence_key() -> None:)+def test_direct_generation_config_frames_evidence_errors_with_the_field_name() -> None:+ # Direct construction reports against the internal field, not the config+ # key from_repo_config uses -- the two framings are deliberate, so pin both.+ with pytest.raises(ValueError, match=r"^source_evidence_files keys must name"):+ GenerationConfig(source_evidence_files={"module_page": ("README.md",)})+ with pytest.raises(ValueError, match=r"^source_evidence_files\.repo_overview must be a list"):+ GenerationConfig(source_evidence_files={"repo_overview": "README.md"})+++def test_direct_generation_config_normalizes_evidence_keys_like_the_config_loader() -> None:+ # Sharing one validator also aligns key normalization: direct construction+ # now strips keys before the membership check, as from_repo_config always+ # did. A padded key is accepted and stored trimmed, not rejected.+ config = GenerationConfig(source_evidence_files={" repo_overview ": ("README.md",)})++ assert config.source_evidence_files == {"repo_overview": ("README.md",)}++@pytest.mark.parametrize("value", [0, -1, True, 1.5, "not-a-number"])def test_generation_config_rejects_invalid_repo_max_tokens(value):with pytest.raises(ValueError, match="positive integer"):