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(core): split crud/analysis into per-entity modules (#726)
Raghav Chamadiya06d860e628f1
diff --git a/packages/core/src/repowise/core/persistence/crud/_shared.py b/packages/core/src/repowise/core/persistence/crud/_shared.pyindex 41dee5d4f..695887334 100644--- a/packages/core/src/repowise/core/persistence/crud/_shared.py+++ b/packages/core/src/repowise/core/persistence/crud/_shared.py@@ -22,6 +22,13 @@ _VALID_JOB_STATUSES = frozenset(_BATCH_SIZE = 500 # max rows per INSERT to stay under SQLite's parameter limit+def _finding_file_path(finding: Any) -> str | None:+ """Read ``file_path`` from a dataclass-like finding or a plain dict."""+ if isinstance(finding, dict):+ return finding.get("file_path")+ return getattr(finding, "file_path", None)++def _parse_dt(ts: str) -> datetime:"""Parse an ISO-8601 UTC string to a timezone-aware datetime."""ts = ts.replace("Z", "+00:00")diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/__init__.py b/packages/core/src/repowise/core/persistence/crud/analysis/__init__.pynew file mode 100644index 000000000..e605ffd09--- /dev/null+++ b/packages/core/src/repowise/core/persistence/crud/analysis/__init__.py@@ -0,0 +1,74 @@+"""CRUD operations for the analysis domain (repowise persistence layer).++Split out of the former monolithic ``crud.py`` and later from the single+``analysis.py`` module into per-entity submodules. ``crud/__init__.py``+re-exports every public name via ``from .analysis import *``, so existing+imports are unaffected.+"""++from __future__ import annotations++from . import coverage, dead_code, health, refactoring # noqa: F401+from .coverage import (+ get_coverage_summary,+ load_coverage_for_repo,+ save_coverage_files,+)+from .dead_code import (+ get_dead_code_findings,+ get_dead_code_summary,+ save_dead_code_findings,+ update_dead_code_status,+ upsert_dead_code_findings,+)+from .health import (+ HEALTH_SNAPSHOT_RETENTION,+ get_file_language_map,+ get_health_findings,+ get_health_metrics,+ get_health_summary,+ get_perf_coverage,+ list_health_snapshots,+ replace_governance_findings,+ save_health_findings,+ save_health_metrics,+ save_health_snapshot,+ update_health_finding_status,+ upsert_health_findings,+ upsert_health_metrics,+)+from .refactoring import (+ get_refactoring_suggestion,+ get_refactoring_suggestions,+ save_refactoring_suggestions,+ upsert_refactoring_suggestions,+)++__all__ = [+ "HEALTH_SNAPSHOT_RETENTION",+ "get_coverage_summary",+ "get_dead_code_findings",+ "get_dead_code_summary",+ "get_file_language_map",+ "get_health_findings",+ "get_health_metrics",+ "get_health_summary",+ "get_perf_coverage",+ "get_refactoring_suggestion",+ "get_refactoring_suggestions",+ "list_health_snapshots",+ "load_coverage_for_repo",+ "replace_governance_findings",+ "save_coverage_files",+ "save_dead_code_findings",+ "save_health_findings",+ "save_health_metrics",+ "save_health_snapshot",+ "save_refactoring_suggestions",+ "update_dead_code_status",+ "update_health_finding_status",+ "upsert_dead_code_findings",+ "upsert_health_findings",+ "upsert_health_metrics",+ "upsert_refactoring_suggestions",+]diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/coverage.py b/packages/core/src/repowise/core/persistence/crud/analysis/coverage.pynew file mode 100644index 000000000..16d1d5b7b--- /dev/null+++ b/packages/core/src/repowise/core/persistence/crud/analysis/coverage.py@@ -0,0 +1,134 @@+"""CRUD operations for coverage files (repowise persistence layer)."""++from __future__ import annotations++import json+from typing import Any++from sqlalchemy import select+from sqlalchemy.ext.asyncio import AsyncSession++from ...models import CoverageFile, _new_uuid+from .._shared import _BATCH_SIZE+++async def save_coverage_files(+ session: AsyncSession,+ repository_id: str,+ files: list[Any],+ *,+ source_format: str,+ ingested_commit_sha: str | None = None,+) -> None:+ """Replace coverage rows for *repository_id* with *files*.++ Mirrors the delete-then-insert pattern used by the health writers.+ *files* is a list of ``FileCoverage`` dataclasses (or dicts with the+ same shape).+ """+ existing = await session.execute(+ select(CoverageFile).where(CoverageFile.repository_id == repository_id)+ )+ for row in existing.scalars().all():+ await session.delete(row)+ await session.flush()++ for i in range(0, len(files), _BATCH_SIZE):+ batch = files[i : i + _BATCH_SIZE]+ for f in batch:+ if hasattr(f, "file_path"):+ data = {+ "file_path": f.file_path,+ "line_coverage_pct": float(f.line_coverage_pct),+ "branch_coverage_pct": (+ float(f.branch_coverage_pct) if f.branch_coverage_pct is not None else None+ ),+ "covered_lines_json": json.dumps(list(f.covered_lines or [])),+ "total_coverable_lines": int(f.total_coverable_lines or 0),+ }+ else:+ data = dict(f)+ if "covered_lines" in data:+ data["covered_lines_json"] = json.dumps(list(data.pop("covered_lines") or []))++ session.add(+ CoverageFile(+ id=_new_uuid(),+ repository_id=repository_id,+ source_format=source_format,+ ingested_commit_sha=ingested_commit_sha,+ **{+ k: v+ for k, v in data.items()+ if k+ not in (+ "id",+ "repository_id",+ "source_format",+ "ingested_commit_sha",+ )+ and hasattr(CoverageFile, k)+ },+ )+ )+ await session.flush()+++async def load_coverage_for_repo(+ session: AsyncSession,+ repository_id: str,+ *,+ file_paths: list[str] | None = None,+) -> list[CoverageFile]:+ q = select(CoverageFile).where(CoverageFile.repository_id == repository_id)+ if file_paths is not None:+ q = q.where(CoverageFile.file_path.in_(file_paths))+ result = await session.execute(q)+ return list(result.scalars().all())+++async def get_coverage_summary(+ session: AsyncSession,+ repository_id: str,+) -> dict[str, Any]:+ """Repo-level coverage aggregate. Returns an empty shape when no rows."""+ rows = await load_coverage_for_repo(session, repository_id)+ if not rows:+ return {+ "file_count": 0,+ "covered_lines": 0,+ "total_lines": 0,+ "line_coverage_pct": None,+ "branch_coverage_pct": None,+ "source_format": None,+ "ingested_at": None,+ "ingested_commit_sha": None,+ }+ covered = 0+ total = 0+ branch_pcts: list[float] = []+ branch_weights: list[int] = []+ for r in rows:+ covered += round(r.line_coverage_pct / 100.0 * r.total_coverable_lines)+ total += r.total_coverable_lines+ if r.branch_coverage_pct is not None:+ branch_pcts.append(r.branch_coverage_pct)+ branch_weights.append(max(r.total_coverable_lines, 1))+ line_pct = (covered / total * 100.0) if total else 0.0+ branch_pct: float | None+ if branch_pcts:+ wsum = sum(branch_weights)+ branch_pct = sum(p * w for p, w in zip(branch_pcts, branch_weights, strict=True)) / wsum+ else:+ branch_pct = None+ latest = max(rows, key=lambda r: r.ingested_at)+ return {+ "file_count": len(rows),+ "covered_lines": covered,+ "total_lines": total,+ "line_coverage_pct": round(line_pct, 2),+ "branch_coverage_pct": round(branch_pct, 2) if branch_pct is not None else None,+ "source_format": latest.source_format,+ "ingested_at": latest.ingested_at,+ "ingested_commit_sha": latest.ingested_commit_sha,+ }diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/dead_code.py b/packages/core/src/repowise/core/persistence/crud/analysis/dead_code.pynew file mode 100644index 000000000..03dd40230--- /dev/null+++ b/packages/core/src/repowise/core/persistence/crud/analysis/dead_code.py@@ -0,0 +1,196 @@+"""CRUD operations for dead-code findings (repowise persistence layer)."""++from __future__ import annotations++import json+from typing import Any++from sqlalchemy import select+from sqlalchemy.ext.asyncio import AsyncSession++from repowise.core.analysis.dead_code.risk_factors import effective_safe_to_delete++from ...models import DeadCodeFinding, _new_uuid+from .._shared import _BATCH_SIZE, _finding_file_path+++def _dead_code_row_kwargs(finding: Any, repository_id: str) -> dict:+ """Normalize a DeadCodeFindingData-like object or plain dict into kwargs+ for the ``DeadCodeFinding`` ORM row."""+ if hasattr(finding, "kind"):+ data = {+ "kind": str(finding.kind.value)+ if hasattr(finding.kind, "value")+ else str(finding.kind),+ "file_path": finding.file_path,+ "symbol_name": finding.symbol_name,+ "symbol_kind": finding.symbol_kind,+ "confidence": finding.confidence,+ "reason": finding.reason,+ "last_commit_at": finding.last_commit_at,+ "commit_count_90d": finding.commit_count_90d,+ "lines": finding.lines,+ "start_line": finding.start_line,+ "end_line": finding.end_line,+ "package": finding.package,+ "evidence_json": json.dumps(finding.evidence if hasattr(finding, "evidence") else []),+ "safe_to_delete": finding.safe_to_delete,+ "primary_owner": finding.primary_owner,+ "age_days": finding.age_days,+ }+ else:+ data = dict(finding)+ if "evidence" in data:+ data["evidence_json"] = json.dumps(data.pop("evidence"))++ return {+ "id": _new_uuid(),+ "repository_id": repository_id,+ **{+ k: v+ for k, v in data.items()+ if k not in ("id", "repository_id") and hasattr(DeadCodeFinding, k)+ },+ }+++async def save_dead_code_findings(+ session: AsyncSession,+ repository_id: str,+ findings: list[dict],+) -> None:+ """Persist dead code findings, replacing any existing open findings for the repo."""+ # Delete existing open findings for this repo before saving new ones+ existing = await session.execute(+ select(DeadCodeFinding).where(+ DeadCodeFinding.repository_id == repository_id,+ DeadCodeFinding.status == "open",+ )+ )+ for row in existing.scalars().all():+ await session.delete(row)++ for i in range(0, len(findings), _BATCH_SIZE):+ batch = findings[i : i + _BATCH_SIZE]+ for finding in batch:+ session.add(DeadCodeFinding(**_dead_code_row_kwargs(finding, repository_id)))+ await session.flush()+++async def upsert_dead_code_findings(+ session: AsyncSession,+ repository_id: str,+ findings: list[Any],+ *,+ file_paths: list[str],+) -> None:+ """Replace open dead-code findings **only for the given file paths**.++ Used by the incremental ``repowise update`` path so unchanged files keep+ their findings instead of being wiped on every partial re-index. Callers+ must pass the full set of *changed* file paths (not just paths that+ produced findings) so a changed-but-now-clean file has its stale findings+ removed.+ """+ if not file_paths:+ return+ allowed = set(file_paths)+ existing = await session.execute(+ select(DeadCodeFinding).where(+ DeadCodeFinding.repository_id == repository_id,+ DeadCodeFinding.status == "open",+ DeadCodeFinding.file_path.in_(file_paths),+ )+ )+ for row in existing.scalars().all():+ await session.delete(row)+ await session.flush()++ # Insert only within the replaced scope (delete is scoped to file_paths).+ scoped = [f for f in findings if _finding_file_path(f) in allowed]+ for i in range(0, len(scoped), _BATCH_SIZE):+ batch = scoped[i : i + _BATCH_SIZE]+ for finding in batch:+ session.add(DeadCodeFinding(**_dead_code_row_kwargs(finding, repository_id)))+ await session.flush()+++async def get_dead_code_findings(+ session: AsyncSession,+ repository_id: str,+ *,+ kind: str | None = None,+ min_confidence: float = 0.0,+ status: str = "open",+) -> list[DeadCodeFinding]:+ """Return dead code findings filtered by kind, confidence, and status."""+ q = select(DeadCodeFinding).where(+ DeadCodeFinding.repository_id == repository_id,+ DeadCodeFinding.status == status,+ DeadCodeFinding.confidence >= min_confidence,+ )+ if kind is not None:+ q = q.where(DeadCodeFinding.kind == kind)+ q = q.order_by(DeadCodeFinding.confidence.desc())+ result = await session.execute(q)+ return list(result.scalars().all())+++async def update_dead_code_status(+ session: AsyncSession,+ finding_id: str,+ status: str,+ note: str | None = None,+) -> DeadCodeFinding | None:+ """Update the status (and optional note) of a dead code finding."""+ finding = await session.get(DeadCodeFinding, finding_id)+ if finding is None:+ return None+ finding.status = status+ if note is not None:+ finding.note = note+ await session.flush()+ return finding+++async def get_dead_code_summary(session: AsyncSession, repository_id: str) -> dict:+ """Return aggregate dead code statistics."""+ result = await session.execute(+ select(DeadCodeFinding).where(+ DeadCodeFinding.repository_id == repository_id,+ DeadCodeFinding.status == "open",+ )+ )+ findings = list(result.scalars().all())++ summary: dict[str, int] = {"high": 0, "medium": 0, "low": 0}+ total_lines = 0+ by_kind: dict[str, int] = {}++ for f in findings:+ if f.confidence >= 0.7:+ summary["high"] += 1+ elif f.confidence >= 0.4:+ summary["medium"] += 1+ else:+ summary["low"] += 1+ total_lines += f.lines+ by_kind[f.kind] = by_kind.get(f.kind, 0) + 1++ # Re-derive effective safety from confidence + path risk factors rather+ # than trusting the persisted boolean alone, so findings written before the+ # risk-factor logic existed (or in a config/bootstrap/database/environment+ # file the allowlist missed) are not counted as deletion-ready.+ deletable_lines = sum(+ f.lines+ for f in findings+ if effective_safe_to_delete(f.confidence, f.file_path, f.safe_to_delete)+ )++ return {+ "total_findings": len(findings),+ "confidence_summary": summary,+ "deletable_lines": deletable_lines,+ "total_lines": total_lines,+ "by_kind": by_kind,+ }diff --git a/packages/core/src/repowise/core/persistence/crud/analysis.py b/packages/core/src/repowise/core/persistence/crud/analysis/health.pysimilarity index 58%rename from packages/core/src/repowise/core/persistence/crud/analysis.pyrename to packages/core/src/repowise/core/persistence/crud/analysis/health.pyindex 8804a52b4..2bed2a4f7 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/health.py@@ -1,8 +1,5 @@-"""CRUD operations for the analysis domain (repowise persistence layer).--Split out of the former monolithic ``crud.py``; ``crud/__init__.py`` re-exports-every public name, so existing imports are unaffected.-"""+"""CRUD operations for code-health findings, metrics, and snapshots+(repowise persistence layer)."""from __future__ import annotations@@ -15,221 +12,18 @@ from sqlalchemy import or_, selectfrom sqlalchemy.ext.asyncio import AsyncSessionif TYPE_CHECKING:- from ...analysis.health.perf.coverage import PerfCoverage--from repowise.core.analysis.dead_code.risk_factors import effective_safe_to_delete+ from ....analysis.health.perf.coverage import PerfCoverage-from ..models import (- CoverageFile,- DeadCodeFinding,+from ...models import (GraphNode,HealthFileMetric,HealthFinding,HealthSnapshot,- RefactoringSuggestion,Repository,_new_uuid,_now_utc,)-from ._shared import _BATCH_SIZE--# ----------------------------------------------------------------------------# DeadCodeFinding CRUD-# ------------------------------------------------------------------------------def _finding_file_path(finding: Any) -> str | None:- """Read ``file_path`` from a dataclass-like finding or a plain dict."""- if isinstance(finding, dict):- return finding.get("file_path")- return getattr(finding, "file_path", None)---def _dead_code_row_kwargs(finding: Any, repository_id: str) -> dict:- """Normalize a DeadCodeFindingData-like object or plain dict into kwargs- for the ``DeadCodeFinding`` ORM row."""- if hasattr(finding, "kind"):- data = {- "kind": str(finding.kind.value)- if hasattr(finding.kind, "value")- else str(finding.kind),- "file_path": finding.file_path,- "symbol_name": finding.symbol_name,- "symbol_kind": finding.symbol_kind,- "confidence": finding.confidence,- "reason": finding.reason,- "last_commit_at": finding.last_commit_at,- "commit_count_90d": finding.commit_count_90d,- "lines": finding.lines,- "start_line": finding.start_line,- "end_line": finding.end_line,- "package": finding.package,- "evidence_json": json.dumps(finding.evidence if hasattr(finding, "evidence") else []),- "safe_to_delete": finding.safe_to_delete,- "primary_owner": finding.primary_owner,- "age_days": finding.age_days,- }- else:- data = dict(finding)- if "evidence" in data:- data["evidence_json"] = json.dumps(data.pop("evidence"))-- return {- "id": _new_uuid(),- "repository_id": repository_id,- **{- k: v- for k, v in data.items()- if k not in ("id", "repository_id") and hasattr(DeadCodeFinding, k)- },- }---async def save_dead_code_findings(- session: AsyncSession,- repository_id: str,- findings: list[dict],-) -> None:- """Persist dead code findings, replacing any existing open findings for the repo."""- # Delete existing open findings for this repo before saving new ones- existing = await session.execute(- select(DeadCodeFinding).where(- DeadCodeFinding.repository_id == repository_id,- DeadCodeFinding.status == "open",- )- )- for row in existing.scalars().all():- await session.delete(row)-- for i in range(0, len(findings), _BATCH_SIZE):- batch = findings[i : i + _BATCH_SIZE]- for finding in batch:- session.add(DeadCodeFinding(**_dead_code_row_kwargs(finding, repository_id)))- await session.flush()---async def upsert_dead_code_findings(- session: AsyncSession,- repository_id: str,- findings: list[Any],- *,- file_paths: list[str],-) -> None:- """Replace open dead-code findings **only for the given file paths**.-- Used by the incremental ``repowise update`` path so unchanged files keep- their findings instead of being wiped on every partial re-index. Callers- must pass the full set of *changed* file paths (not just paths that- produced findings) so a changed-but-now-clean file has its stale findings- removed.- """- if not file_paths:- return- allowed = set(file_paths)- existing = await session.execute(- select(DeadCodeFinding).where(- DeadCodeFinding.repository_id == repository_id,- DeadCodeFinding.status == "open",- DeadCodeFinding.file_path.in_(file_paths),- )- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-- # Insert only within the replaced scope (delete is scoped to file_paths).- scoped = [f for f in findings if _finding_file_path(f) in allowed]- for i in range(0, len(scoped), _BATCH_SIZE):- batch = scoped[i : i + _BATCH_SIZE]- for finding in batch:- session.add(DeadCodeFinding(**_dead_code_row_kwargs(finding, repository_id)))- await session.flush()---async def get_dead_code_findings(- session: AsyncSession,- repository_id: str,- *,- kind: str | None = None,- min_confidence: float = 0.0,- status: str = "open",-) -> list[DeadCodeFinding]:- """Return dead code findings filtered by kind, confidence, and status."""- q = select(DeadCodeFinding).where(- DeadCodeFinding.repository_id == repository_id,- DeadCodeFinding.status == status,- DeadCodeFinding.confidence >= min_confidence,- )- if kind is not None:- q = q.where(DeadCodeFinding.kind == kind)- q = q.order_by(DeadCodeFinding.confidence.desc())- result = await session.execute(q)- return list(result.scalars().all())---async def update_dead_code_status(- session: AsyncSession,- finding_id: str,- status: str,- note: str | None = None,-) -> DeadCodeFinding | None:- """Update the status (and optional note) of a dead code finding."""- finding = await session.get(DeadCodeFinding, finding_id)- if finding is None:- return None- finding.status = status- if note is not None:- finding.note = note- await session.flush()- return finding---async def get_dead_code_summary(session: AsyncSession, repository_id: str) -> dict:- """Return aggregate dead code statistics."""- result = await session.execute(- select(DeadCodeFinding).where(- DeadCodeFinding.repository_id == repository_id,- DeadCodeFinding.status == "open",- )- )- findings = list(result.scalars().all())-- summary: dict[str, int] = {"high": 0, "medium": 0, "low": 0}- total_lines = 0- by_kind: dict[str, int] = {}-- for f in findings:- if f.confidence >= 0.7:- summary["high"] += 1- elif f.confidence >= 0.4:- summary["medium"] += 1- else:- summary["low"] += 1- total_lines += f.lines- by_kind[f.kind] = by_kind.get(f.kind, 0) + 1-- # Re-derive effective safety from confidence + path risk factors rather- # than trusting the persisted boolean alone, so findings written before the- # risk-factor logic existed (or in a config/bootstrap/database/environment- # file the allowlist missed) are not counted as deletion-ready.- deletable_lines = sum(- f.lines- for f in findings- if effective_safe_to_delete(f.confidence, f.file_path, f.safe_to_delete)- )-- return {- "total_findings": len(findings),- "confidence_summary": summary,- "deletable_lines": deletable_lines,- "total_lines": total_lines,- "by_kind": by_kind,- }---# ----------------------------------------------------------------------------# Code Health CRUD-# ---------------------------------------------------------------------------+from .._shared import _BATCH_SIZEasync def save_health_findings(@@ -546,7 +340,7 @@ async def get_perf_coverage(session: AsyncSession, repository_id: str) -> PerfCo"""How much of the analyzed code the performance pass was able to run on."""# Imported lazily to keep the persistence layer free of an analysis-layer# import at module load (and to avoid a circular import).- from ...analysis.health.perf.coverage import coverage_for_metrics+ from ....analysis.health.perf.coverage import coverage_for_metricsmetrics = await get_health_metrics(session, repository_id)lang_by_path = await get_file_language_map(session, repository_id)@@ -635,7 +429,7 @@ async def get_health_summary(session: AsyncSession, repository_id: str) -> dict:# perf-unsupported language the aggregate perf average is meaningless, so we# surface how much of the analyzed code a detector actually ran on, plus a# findings-per-10K-LOC density over the *covered* lines (not the whole repo).- from ...analysis.health.perf.coverage import coverage_for_metrics+ from ....analysis.health.perf.coverage import coverage_for_metricslang_by_path = await get_file_language_map(session, repository_id)coverage = coverage_for_metrics(metrics, lang_by_path)@@ -880,288 +674,3 @@ async def upsert_health_metrics())await session.flush()---# ----------------------------------------------------------------------------# Refactoring suggestion CRUD-# ------------------------------------------------------------------------------def _refactoring_row_kwargs(suggestion: Any, repository_id: str) -> dict:- """Normalize a ``RefactoringSuggestion`` dataclass or a plain dict into- kwargs for the ORM row (folding the open ``plan`` / ``evidence`` /- ``blast_radius`` dicts into their ``*_json`` columns)."""- if hasattr(suggestion, "refactoring_type"):- data = {- "refactoring_type": suggestion.refactoring_type,- "file_path": suggestion.file_path,- "target_symbol": suggestion.target_symbol,- "line_start": suggestion.line_start,- "line_end": suggestion.line_end,- "plan_json": json.dumps(suggestion.plan or {}),- "evidence_json": json.dumps(suggestion.evidence or {}),- "impact_delta": float(suggestion.impact_delta),- "effort_bucket": suggestion.effort_bucket,- "blast_radius_json": json.dumps(suggestion.blast_radius or {}),- "confidence": suggestion.confidence,- "source_biomarker": suggestion.source_biomarker,- }- else:- data = dict(suggestion)- for key in ("plan", "evidence", "blast_radius"):- if key in data:- data[f"{key}_json"] = json.dumps(data.pop(key) or {})-- return {- "id": _new_uuid(),- "repository_id": repository_id,- **{- k: v- for k, v in data.items()- if k not in ("id", "repository_id") and hasattr(RefactoringSuggestion, k)- },- }---async def save_refactoring_suggestions(- session: AsyncSession,- repository_id: str,- suggestions: list[Any],-) -> None:- """Replace open refactoring suggestions for *repository_id*.-- Delete-then-insert, mirroring ``save_health_findings``. Accepts- ``RefactoringSuggestion`` dataclasses or plain dicts.- """- existing = await session.execute(- select(RefactoringSuggestion).where(- RefactoringSuggestion.repository_id == repository_id,- RefactoringSuggestion.status == "open",- )- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-- for i in range(0, len(suggestions), _BATCH_SIZE):- batch = suggestions[i : i + _BATCH_SIZE]- for s in batch:- session.add(RefactoringSuggestion(**_refactoring_row_kwargs(s, repository_id)))- await session.flush()---async def upsert_refactoring_suggestions(- session: AsyncSession,- repository_id: str,- suggestions: list[Any],- *,- file_paths: list[str],-) -> None:- """Replace open suggestions **only for the given file paths**.-- The incremental ``repowise update`` sibling of- ``save_refactoring_suggestions``: unchanged files keep their suggestions.- Pass the full set of *changed* paths (not just those that produced a- suggestion) so a changed-but-now-clean file is cleared.- """- if not file_paths:- return- allowed = set(file_paths)- existing = await session.execute(- select(RefactoringSuggestion).where(- RefactoringSuggestion.repository_id == repository_id,- RefactoringSuggestion.status == "open",- RefactoringSuggestion.file_path.in_(file_paths),- )- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-- scoped = [s for s in suggestions if _finding_file_path(s) in allowed]- for i in range(0, len(scoped), _BATCH_SIZE):- batch = scoped[i : i + _BATCH_SIZE]- for s in batch:- session.add(RefactoringSuggestion(**_refactoring_row_kwargs(s, repository_id)))- await session.flush()---async def get_refactoring_suggestion(- session: AsyncSession,- repository_id: str,- suggestion_id: str,-) -> RefactoringSuggestion | None:- """Return one refactoring suggestion by id, scoped to *repository_id*.-- Powers the web tab's plan-detail drill-down (and any deep link to a single- plan). Returns ``None`` when the id is unknown or belongs to another repo.- """- result = await session.execute(- select(RefactoringSuggestion).where(- RefactoringSuggestion.repository_id == repository_id,- RefactoringSuggestion.id == suggestion_id,- )- )- return result.scalar_one_or_none()---async def get_refactoring_suggestions(- session: AsyncSession,- repository_id: str,- *,- refactoring_type: str | None = None,- file_paths: list[str] | None = None,- min_confidence: str | None = None,- status: str = "open",-) -> list[RefactoringSuggestion]:- """Return refactoring suggestions, highest recovered impact first."""- q = select(RefactoringSuggestion).where(- RefactoringSuggestion.repository_id == repository_id,- RefactoringSuggestion.status == status,- )- if refactoring_type is not None:- q = q.where(RefactoringSuggestion.refactoring_type == refactoring_type)- if file_paths is not None:- q = q.where(RefactoringSuggestion.file_path.in_(file_paths))- if min_confidence is not None:- order = {"low": 0, "medium": 1, "high": 2}- threshold = order.get(min_confidence, 0)- allowed = [k for k, v in order.items() if v >= threshold]- q = q.where(RefactoringSuggestion.confidence.in_(allowed))- # Secondary keys (file_path, target_symbol) make the read order stable for- # ties — notably the common 0.0 no-finding case — so it matches the- # detector's own deterministic ordering rather than DB row order.- q = q.order_by(- RefactoringSuggestion.impact_delta.desc(),- RefactoringSuggestion.file_path.asc(),- RefactoringSuggestion.target_symbol.asc(),- )- result = await session.execute(q)- return list(result.scalars().all())---# ----------------------------------------------------------------------------# Coverage CRUD-# ------------------------------------------------------------------------------async def save_coverage_files(- session: AsyncSession,- repository_id: str,- files: list[Any],- *,- source_format: str,- ingested_commit_sha: str | None = None,-) -> None:- """Replace coverage rows for *repository_id* with *files*.-- Mirrors the delete-then-insert pattern used by the health writers.- *files* is a list of ``FileCoverage`` dataclasses (or dicts with the- same shape).- """- existing = await session.execute(- select(CoverageFile).where(CoverageFile.repository_id == repository_id)- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-- for i in range(0, len(files), _BATCH_SIZE):- batch = files[i : i + _BATCH_SIZE]- for f in batch:- if hasattr(f, "file_path"):- data = {- "file_path": f.file_path,- "line_coverage_pct": float(f.line_coverage_pct),- "branch_coverage_pct": (- float(f.branch_coverage_pct) if f.branch_coverage_pct is not None else None- ),- "covered_lines_json": json.dumps(list(f.covered_lines or [])),- "total_coverable_lines": int(f.total_coverable_lines or 0),- }- else:- data = dict(f)- if "covered_lines" in data:- data["covered_lines_json"] = json.dumps(list(data.pop("covered_lines") or []))-- session.add(- CoverageFile(- id=_new_uuid(),- repository_id=repository_id,- source_format=source_format,- ingested_commit_sha=ingested_commit_sha,- **{- k: v- for k, v in data.items()- if k- not in (- "id",- "repository_id",- "source_format",- "ingested_commit_sha",- )- and hasattr(CoverageFile, k)- },- )- )- await session.flush()---async def load_coverage_for_repo(- session: AsyncSession,- repository_id: str,- *,- file_paths: list[str] | None = None,-) -> list[CoverageFile]:- q = select(CoverageFile).where(CoverageFile.repository_id == repository_id)- if file_paths is not None:- q = q.where(CoverageFile.file_path.in_(file_paths))- result = await session.execute(q)- return list(result.scalars().all())---async def get_coverage_summary(- session: AsyncSession,- repository_id: str,-) -> dict[str, Any]:- """Repo-level coverage aggregate. Returns an empty shape when no rows."""- rows = await load_coverage_for_repo(session, repository_id)- if not rows:- return {- "file_count": 0,- "covered_lines": 0,- "total_lines": 0,- "line_coverage_pct": None,- "branch_coverage_pct": None,- "source_format": None,- "ingested_at": None,- "ingested_commit_sha": None,- }- covered = 0- total = 0- branch_pcts: list[float] = []- branch_weights: list[int] = []- for r in rows:- covered += round(r.line_coverage_pct / 100.0 * r.total_coverable_lines)- total += r.total_coverable_lines- if r.branch_coverage_pct is not None:- branch_pcts.append(r.branch_coverage_pct)- branch_weights.append(max(r.total_coverable_lines, 1))- line_pct = (covered / total * 100.0) if total else 0.0- branch_pct: float | None- if branch_pcts:- wsum = sum(branch_weights)- branch_pct = sum(p * w for p, w in zip(branch_pcts, branch_weights, strict=True)) / wsum- else:- branch_pct = None- latest = max(rows, key=lambda r: r.ingested_at)- return {- "file_count": len(rows),- "covered_lines": covered,- "total_lines": total,- "line_coverage_pct": round(line_pct, 2),- "branch_coverage_pct": round(branch_pct, 2) if branch_pct is not None else None,- "source_format": latest.source_format,- "ingested_at": latest.ingested_at,- "ingested_commit_sha": latest.ingested_commit_sha,- }diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.pynew file mode 100644index 000000000..5c55271f2--- /dev/null+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py@@ -0,0 +1,165 @@+"""CRUD operations for refactoring suggestions (repowise persistence layer)."""++from __future__ import annotations++import json+from typing import Any++from sqlalchemy import select+from sqlalchemy.ext.asyncio import AsyncSession++from ...models import RefactoringSuggestion, _new_uuid+from .._shared import _BATCH_SIZE, _finding_file_path+++def _refactoring_row_kwargs(suggestion: Any, repository_id: str) -> dict:+ """Normalize a ``RefactoringSuggestion`` dataclass or a plain dict into+ kwargs for the ORM row (folding the open ``plan`` / ``evidence`` /+ ``blast_radius`` dicts into their ``*_json`` columns)."""+ if hasattr(suggestion, "refactoring_type"):+ data = {+ "refactoring_type": suggestion.refactoring_type,+ "file_path": suggestion.file_path,+ "target_symbol": suggestion.target_symbol,+ "line_start": suggestion.line_start,+ "line_end": suggestion.line_end,+ "plan_json": json.dumps(suggestion.plan or {}),+ "evidence_json": json.dumps(suggestion.evidence or {}),+ "impact_delta": float(suggestion.impact_delta),+ "effort_bucket": suggestion.effort_bucket,+ "blast_radius_json": json.dumps(suggestion.blast_radius or {}),+ "confidence": suggestion.confidence,+ "source_biomarker": suggestion.source_biomarker,+ }+ else:+ data = dict(suggestion)+ for key in ("plan", "evidence", "blast_radius"):+ if key in data:+ data[f"{key}_json"] = json.dumps(data.pop(key) or {})++ return {+ "id": _new_uuid(),+ "repository_id": repository_id,+ **{+ k: v+ for k, v in data.items()+ if k not in ("id", "repository_id") and hasattr(RefactoringSuggestion, k)+ },+ }+++async def save_refactoring_suggestions(+ session: AsyncSession,+ repository_id: str,+ suggestions: list[Any],+) -> None:+ """Replace open refactoring suggestions for *repository_id*.++ Delete-then-insert, mirroring ``save_health_findings``. Accepts+ ``RefactoringSuggestion`` dataclasses or plain dicts.+ """+ existing = await session.execute(+ select(RefactoringSuggestion).where(+ RefactoringSuggestion.repository_id == repository_id,+ RefactoringSuggestion.status == "open",+ )+ )+ for row in existing.scalars().all():+ await session.delete(row)+ await session.flush()++ for i in range(0, len(suggestions), _BATCH_SIZE):+ batch = suggestions[i : i + _BATCH_SIZE]+ for s in batch:+ session.add(RefactoringSuggestion(**_refactoring_row_kwargs(s, repository_id)))+ await session.flush()+++async def upsert_refactoring_suggestions(+ session: AsyncSession,+ repository_id: str,+ suggestions: list[Any],+ *,+ file_paths: list[str],+) -> None:+ """Replace open suggestions **only for the given file paths**.++ The incremental ``repowise update`` sibling of+ ``save_refactoring_suggestions``: unchanged files keep their suggestions.+ Pass the full set of *changed* paths (not just those that produced a+ suggestion) so a changed-but-now-clean file is cleared.+ """+ if not file_paths:+ return+ allowed = set(file_paths)+ existing = await session.execute(+ select(RefactoringSuggestion).where(+ RefactoringSuggestion.repository_id == repository_id,+ RefactoringSuggestion.status == "open",+ RefactoringSuggestion.file_path.in_(file_paths),+ )+ )+ for row in existing.scalars().all():+ await session.delete(row)+ await session.flush()++ scoped = [s for s in suggestions if _finding_file_path(s) in allowed]+ for i in range(0, len(scoped), _BATCH_SIZE):+ batch = scoped[i : i + _BATCH_SIZE]+ for s in batch:+ session.add(RefactoringSuggestion(**_refactoring_row_kwargs(s, repository_id)))+ await session.flush()+++async def get_refactoring_suggestion(+ session: AsyncSession,+ repository_id: str,+ suggestion_id: str,+) -> RefactoringSuggestion | None:+ """Return one refactoring suggestion by id, scoped to *repository_id*.++ Powers the web tab's plan-detail drill-down (and any deep link to a single+ plan). Returns ``None`` when the id is unknown or belongs to another repo.+ """+ result = await session.execute(+ select(RefactoringSuggestion).where(+ RefactoringSuggestion.repository_id == repository_id,+ RefactoringSuggestion.id == suggestion_id,+ )+ )+ return result.scalar_one_or_none()+++async def get_refactoring_suggestions(+ session: AsyncSession,+ repository_id: str,+ *,+ refactoring_type: str | None = None,+ file_paths: list[str] | None = None,+ min_confidence: str | None = None,+ status: str = "open",+) -> list[RefactoringSuggestion]:+ """Return refactoring suggestions, highest recovered impact first."""+ q = select(RefactoringSuggestion).where(+ RefactoringSuggestion.repository_id == repository_id,+ RefactoringSuggestion.status == status,+ )+ if refactoring_type is not None:+ q = q.where(RefactoringSuggestion.refactoring_type == refactoring_type)+ if file_paths is not None:+ q = q.where(RefactoringSuggestion.file_path.in_(file_paths))+ if min_confidence is not None:+ order = {"low": 0, "medium": 1, "high": 2}+ threshold = order.get(min_confidence, 0)+ allowed = [k for k, v in order.items() if v >= threshold]+ q = q.where(RefactoringSuggestion.confidence.in_(allowed))+ # Secondary keys (file_path, target_symbol) make the read order stable for+ # ties — notably the common 0.0 no-finding case — so it matches the+ # detector's own deterministic ordering rather than DB row order.+ q = q.order_by(+ RefactoringSuggestion.impact_delta.desc(),+ RefactoringSuggestion.file_path.asc(),+ RefactoringSuggestion.target_symbol.asc(),+ )+ result = await session.execute(q)+ return list(result.scalars().all())