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.

refactor(refactoring): move plan hydration into persistence and split the refactoring router (#3380)

Raghav Chamadiyaf9b7bc2d239f
diff --git a/docs/architecture/refactoring.md b/docs/architecture/refactoring.md
index 757b23532..258546b55 100644
--- a/docs/architecture/refactoring.md
+++ b/docs/architecture/refactoring.md
@@ -77,7 +77,6 @@ GET /api/repos/{repo_id}/refactoring/opportunities ?view= &scope= &fil
GET /api/repos/{repo_id}/refactoring/opportunities/{opportunity_id}
GET /api/repos/{repo_id}/refactoring/summary
GET /api/repos/{repo_id}/refactoring/targets ?refactoring_type= &min_confidence=
-GET /api/repos/{repo_id}/refactoring/targets/page ?limit= &offset= &refactoring_type=
GET /api/repos/{repo_id}/refactoring/{suggestion_id}
POST /api/repos/{repo_id}/refactoring/{suggestion_id}/generate-code
GET /api/repos/{repo_id}/refactoring/settings
diff --git a/docs/reference/API_REFERENCE.md b/docs/reference/API_REFERENCE.md
index bf166d208..b751b1b9b 100644
--- a/docs/reference/API_REFERENCE.md
+++ b/docs/reference/API_REFERENCE.md
@@ -416,11 +416,11 @@ repo (`routers/search.py`).
### 5.4 Pagination
-`limit`/`offset` is the convention (39 operations take `limit`, 19 take
+`limit`/`offset` is the convention (41 operations take `limit`, 18 take
`offset`), with a per-route maximum in the schema. The page-shaped responses
that need to tell a client whether more exists return an explicit
`{total, offset, has_more, next_offset}` instead, e.g.
-`GET /api/repos/{repo_id}/refactoring/targets/page`. `GET /api/pages` accepts a
+`GET /api/repos/{repo_id}/refactoring/opportunities`. `GET /api/pages` accepts a
`limit` up to 5000, which is the one route where a full listing is a realistic
request.
diff --git a/packages/api-client/src/refactoring.ts b/packages/api-client/src/refactoring.ts
index ac2401292..4bd010eb2 100644
--- a/packages/api-client/src/refactoring.ts
+++ b/packages/api-client/src/refactoring.ts
@@ -11,7 +11,6 @@ import type {
RefactoringOpportunityPage,
RefactoringOpportunityStatusUpdate,
RefactoringOrder,
- RefactoringPlanPage,
RefactoringPlan,
RefactoringPlanStatusUpdate,
RefactoringScope,
@@ -48,17 +47,6 @@ export interface RefactoringTargetsParams {
view?: "canonical" | "file_spread";
}
-export type RefactoringSort = "canonical" | "health" | "effort" | "blast" | "file";
-
-export interface RefactoringPageParams extends RefactoringTargetsParams {
- search?: string;
- confidence?: string;
- effort?: string;
- sort?: RefactoringSort;
- limit?: number;
- offset?: number;
-}
-
export async function getRefactoringTargets(
repoId: string,
params: RefactoringTargetsParams = {},
@@ -71,25 +59,6 @@ export async function getRefactoringTargets(
});
}
-/** Bounded server-filtered list for product surfaces. */
-export async function getRefactoringPlansPage(
- repoId: string,
- params: RefactoringPageParams = {},
-): Promise<RefactoringPlanPage> {
- return apiGet<RefactoringPlanPage>(`/api/repos/${repoId}/refactoring/targets/page`, {
- refactoring_type: params.refactoringType,
- min_confidence: params.minConfidence,
- file_path: params.filePath,
- view: params.view,
- search: params.search,
- confidence: params.confidence,
- effort: params.effort,
- sort: params.sort,
- limit: params.limit,
- offset: params.offset,
- });
-}
-
export async function getRefactoringPlan(
repoId: string,
suggestionId: string,
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/persist.py b/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
index eaf683474..c258bff61 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
@@ -150,11 +150,13 @@ def _load_recommendations(
from repowise.cli.helpers import get_db_url_for_repo, reconcile_schema_best_effort
from repowise.core.analysis.health.refactoring.recommendations import (
build_recommendations,
- hydrate_recommendations,
serialize_recommendations,
)
from repowise.core.persistence import create_engine, create_session_factory, get_session
from repowise.core.persistence.crud import get_repository_by_path
+ from repowise.core.persistence.crud.analysis.refactoring_recommendations import (
+ hydrate_recommendations,
+ )
async def _do() -> list[dict[str, Any]]:
url = get_db_url_for_repo(repo_path)
diff --git a/packages/core/src/repowise/core/analysis/health/impact_effort.py b/packages/core/src/repowise/core/analysis/health/impact_effort.py
index a79e70bd1..8e8daf3ba 100644
--- a/packages/core/src/repowise/core/analysis/health/impact_effort.py
+++ b/packages/core/src/repowise/core/analysis/health/impact_effort.py
@@ -12,18 +12,22 @@ filter or a new index never moves a file from one quadrant to another unless
the file itself changed.
Pure over plain rows (``health.rows.field``); the server reads and filters the
-rows, this module only places them.
+rows, this module only places them. :func:`work_queue_targets` builds the rows
+the plane and the health work queue share, one per file.
"""
from __future__ import annotations
-from collections.abc import Iterable, Mapping
+from collections.abc import Callable, Iterable, Mapping
from dataclasses import asdict, dataclass
from typing import Any, Literal
+from repowise.core.analysis.health.effort import EFFORT_WEIGHT, effort_bucket
+from repowise.core.analysis.health.models import primary_finding, split_by_origin
from repowise.core.analysis.health.queue.eligibility import MIN_WORTH
from repowise.core.analysis.health.queue.order import TIER_RANK
from repowise.core.analysis.health.rows import detail_map, field
+from repowise.core.analysis.health.suggestions import suggestion_for
#: The vertical midline, in lines to change. The work queue's Medium effort
#: ceiling (150 NLOC), which is also the median size of a file with findings on
@@ -165,13 +169,145 @@ def build_impact_effort(
)
+# Not ``aggregation.SEVERITY_ORDER``: there an unknown severity sorts last, here it counts as low.
+_SEVERITY_ORDER = {"low": 0, "medium": 1, "high": 2, "critical": 3}
+
+#: The work queue's orders over :func:`work_queue_targets` rows.
+WORK_QUEUE_SORTS: dict[str, Callable[[dict[str, Any]], Any]] = {
+ "impact_per_effort": lambda t: (-t["impact_per_effort"], -t["total_impact"]),
+ "total_impact": lambda t: -t["total_impact"],
+ # ``score`` clamps at 1.0, so on a real repo dozens of targets tie at the
+ # top and sorting on it alone returns them in dict-insertion order.
+ # ``total_impact`` is the same pre-clamp deduction magnitude the crud layer
+ # ranks metrics by — but summed over the findings that survived this
+ # request's finding-level filters (biomarker, severity, min_severity,
+ # dimension, status), so under a filter this ranks by the filtered depth
+ # rather than the file's full depth. That is what a filtered queue should
+ # do; it just means the order is not expected to match /health/files once
+ # a filter is on.
+ "score": lambda t: (t["score"], -t["total_impact"], t["file_path"]),
+ "finding_count": lambda t: -t["finding_count"],
+}
+
+
+def _finding_filter(
+ biomarker: str | None, severity: str | None, min_severity: str | None
+) -> Callable[[Any], bool]:
+ """One marker, then exact severities or else a severity floor."""
+ # An all-empty list (","; " ") means the caller selected nothing, not that
+ # nothing matches — falling through to ``min_severity`` keeps a stray
+ # serialization from silently emptying the queue behind a 200.
+ picked = {v.strip().lower() for v in (severity or "").split(",") if v.strip()}
+ floor = _SEVERITY_ORDER.get(min_severity, 0) if min_severity else None
+
+ def keep(f: Any) -> bool:
+ if biomarker and f.biomarker_type != biomarker:
+ return False
+ if picked:
+ return (f.severity or "").lower() in picked
+ if floor is not None:
+ return _SEVERITY_ORDER.get(f.severity, 0) >= floor
+ return True
+
+ return keep
+
+
+def _lead_finding(fs: list[Any]) -> Any:
+ primary = primary_finding(fs)
+ if primary is not None:
+ return primary
+ # Every finding here is advisory, so no cause accuses this file and the
+ # general queue never reaches this: advisory is excluded from it. A caller
+ # who filtered to an advisory marker did reach it, and the marker they
+ # asked for is the honest lead for the row.
+ return max(fs, key=lambda x: (_SEVERITY_ORDER.get(x.severity, 0), -(x.line_start or 0)))
+
+
+def _target_row(file_path: str, fs: list[Any], m: Any, bucket: str) -> dict:
+ primary = _lead_finding(fs)
+ # Impact, and therefore the ranking, counts only findings still open:
+ # ``score`` on this row was computed from open findings, and a file
+ # whose findings were all dismissed is not work to rank near the top.
+ open_fs = [x for x in fs if (getattr(x, "status", None) or "open") == "open"]
+ total_impact = round(sum(x.health_impact for x in open_fs), 3)
+ return {
+ "file_path": file_path,
+ "score": round(m.score, 2),
+ "nloc": m.nloc,
+ "module": m.module or None,
+ "is_test": bool(getattr(m, "is_test", False)),
+ "primary_biomarker": primary.biomarker_type,
+ "primary_severity": primary.severity,
+ "primary_reason": primary.reason,
+ "primary_function": primary.function_name,
+ "primary_line_start": primary.line_start,
+ "primary_line_end": primary.line_end,
+ "primary_suggestion": suggestion_for(primary.biomarker_type),
+ "primary_finding_id": primary.id,
+ "total_impact": total_impact,
+ "finding_count": len(fs),
+ "open_finding_count": len(open_fs),
+ "biomarkers": sorted({x.biomarker_type for x in fs}),
+ "effort_bucket": bucket,
+ "impact_per_effort": round(total_impact / EFFORT_WEIGHT[bucket], 3),
+ }
+
+
+def work_queue_targets(
+ metric_by_path: Mapping[str, Any],
+ findings: Iterable[Any],
+ *,
+ biomarker: str | None = None,
+ severity: str | None = None,
+ min_severity: str | None = None,
+ max_effort: str | None = None,
+ history: str = "exclude",
+) -> tuple[list[dict], int]:
+ """One row per file the finding filters keep, unsorted, and the history-only count.
+
+ *metric_by_path* holds only the files the caller's file-level filters keep.
+ ``history="exclude"`` leaves out a file whose only findings are history
+ markers, unless a marker was named.
+ """
+ keep = _finding_filter(biomarker, severity, min_severity)
+ by_file: dict[str, list[Any]] = {}
+ for f in findings:
+ if keep(f):
+ by_file.setdefault(f.file_path, []).append(f)
+ max_effort_rank = EFFORT_WEIGHT.get(max_effort or "", 99)
+
+ targets: list[dict] = []
+ history_only = 0
+ for file_path, fs in by_file.items():
+ m = metric_by_path.get(file_path)
+ # Absent means either filtered out above, or a file this reading
+ # cannot score: under ``code_shape``, a row with no recorded split.
+ # Ranking that on a stand-in 10.0 would put an unmeasured file at the
+ # top of a list ordered by how bad things are. The same holds for a
+ # file in a language health has no dialect for, stored with no score.
+ if m is None or m.score is None:
+ continue
+ # Naming a marker reaches it whatever its origin, as it reaches the
+ # zero-impact dimensions when the caller reads the findings.
+ if history == "exclude" and biomarker is None and not split_by_origin(fs)[0]:
+ history_only += 1
+ continue
+ bucket = effort_bucket(m.nloc)
+ if EFFORT_WEIGHT[bucket] > max_effort_rank:
+ continue
+ targets.append(_target_row(file_path, fs, m, bucket))
+ return targets, history_only
+
+
__all__ = [
"EFFORT_MIDLINE_LINES",
"GAIN_MIDLINE_POINTS",
"PLOT_CAP",
+ "WORK_QUEUE_SORTS",
"ImpactEffortPlane",
"ImpactEffortPoint",
"build_impact_effort",
"fix_first_marks",
"plan_lines",
+ "work_queue_targets",
]
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
index e81f9eea3..e5bdb51c7 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/recommendations.py
@@ -2,8 +2,8 @@
Detector suggestions and persisted ORM rows are deliberately small write-side
facts. This module is the one read-side owner that turns either shape into the
-recommendation contract consumed by REST, MCP, and CLI. Every database read is
-batched across the complete plan set; adding plans never adds SQL statements.
+recommendation contract consumed by REST, MCP, and CLI. The reads that feed it
+run in ``persistence/crud/analysis/refactoring_recommendations.py``.
"""
from __future__ import annotations
@@ -16,26 +16,17 @@ from collections.abc import Callable, Collection, Mapping, Sequence
from dataclasses import dataclass, field
from typing import Any, Literal
-from sqlalchemy.ext.asyncio import AsyncSession
-
-from repowise.core.analysis.health.effort import EFFORT_ORDER
from repowise.core.analysis.health.grading import TARGET_SCORE
from repowise.core.analysis.health.perf.ranking import _percentile_threshold
-from repowise.core.analysis.health.queue_rules import FilterRule, SortKeys
+from repowise.core.analysis.health.queue_rules import FilterRule
from repowise.core.analysis.health.refactoring_summary import STRUCTURAL_TYPES
from repowise.core.analysis.pr_blast import rank_tests_by_reach
-from repowise.core.analysis.test_collection import narrow_scopes
from repowise.core.analysis.test_reachability import (
DEFAULT_CALL_DEPTH,
MAX_TESTS_PER_TARGET,
ReachDistance,
ReachedBy,
- cached_test_files,
- imported_names_by_test,
rank_tests,
- reach_into_symbols,
- tests_matching_by_name,
- tests_reaching_by_tier,
)
from repowise.core.code_origin import ship_rank
from repowise.core.test_paths import is_test_support_path, names_test_for, paired_test_names
@@ -1016,8 +1007,6 @@ class Recommendation:
file_nloc: int
file_weighted_deficit: int
validation: ValidationPlan
- # The reads a rank-only pass made, so detailing a page reuses them.
- inputs: ValidationInputs | None = field(default=None, repr=False, compare=False)
# What other layers say about the target (:mod:`.annotations`), stored at
# finalize and served on plan detail only; ``None`` when never checked.
annotations: PlanAnnotations | None = field(default=None, repr=False, compare=False)
@@ -1146,29 +1135,14 @@ def stored_recommendation(row: Any) -> Recommendation | None:
)
-#: Each bucket's place in the shared effort scale, for the ``effort`` sort.
-EFFORT_RANK = {bucket: rank for rank, bucket in enumerate(EFFORT_ORDER)}
-#: Where a bucket outside the scale sorts: with ``L``.
-UNKNOWN_EFFORT = EFFORT_RANK["L"]
-
#: The plan list's filters, read in SQL on a ranked store
#: (``persistence.sql.rule_predicate``) and in memory on the live path
#: (``queue_rules.keep``), so the two cannot disagree.
PLAN_FILTERS = (
FilterRule("refactoring_types", "refactoring_type", "in", "set"),
FilterRule("file_path", "file_path", "eq", "set"),
- FilterRule("confidences", "confidence", "in", "truthy"),
- FilterRule("efforts", "effort_bucket", "in", "truthy"),
)
-#: The plan list's field sorts; every one but ``file`` breaks ties by the view
-#: order. ``effort`` reads :data:`EFFORT_RANK` and ``canonical`` is the view.
-PLAN_SORTS: dict[str, SortKeys] = {
- "health": (("impact_delta", True),),
- "blast": (("blast_size", True),),
- "file": (("file_path", False), ("target_symbol", False), ("id", False)),
-}
-
def plan_types(refactoring_type: str | None) -> tuple[str, ...] | None:
"""The plan types a list's ``refactoring_type`` names; ``structural`` is a lens."""
@@ -1177,22 +1151,6 @@ def plan_types(refactoring_type: str | None) -> tuple[str, ...] | None:
return (refactoring_type,) if refactoring_type else None
-def matches_search(suggestion: RefactoringSuggestion, query: str) -> bool:
- """Whether lower-cased *query* occurs in the plan's searchable text."""
- plan = suggestion.plan or {}
- haystack = " ".join(
- (
- suggestion.file_path,
- suggestion.target_symbol,
- suggestion.refactoring_type,
- suggestion.source_biomarker,
- str(plan.get("strategy") or ""),
- str(plan.get("intervention_symbol") or ""),
- )
- ).lower()
- return query in haystack
-
-
def _priority_components(
suggestion: RefactoringSuggestion,
*,
@@ -1315,217 +1273,6 @@ def apply_view(
return spread
-async def hydrate_recommendations(
- session: AsyncSession,
- repository_id: str,
- rows: Sequence[Any],
- *,
- metric_rows: Sequence[Any] | None = None,
- view: RecommendationView = "canonical",
- test_limit: int = DEFAULT_TEST_LIMIT,
- rank_only: bool = False,
- step_verify: bool = False,
-) -> list[Recommendation]:
- """Hydrate, enrich, validate, rank, and serialize-ready all *rows*.
-
- Query shape is constant in plan/test count: health metrics and graph metrics
- are bulk reads, measured coverage is one ``IN`` query, and inferred walks
- use their existing bounded level queries over the complete unanswered set.
-
- *rank_only* skips the symbol-level evidence that orders each plan's tests:
- rank and validation basis are unchanged, the test order falls back to name
- and directory. A paged surface ranks every row this way, then passes the
- rows it returns through :func:`detail_recommendations`.
-
- *step_verify* also validates each step of a multi-step plan
- (:func:`with_step_verify`). Finalize asks, so it is stored once; a live
- read never rebuilds it and serves the plan-level answer.
- """
- from repowise.core.persistence import crud
-
- if not rows:
- return []
- suggestions = [rehydrate_suggestion(row) for row in rows]
- metrics = (
- list(metric_rows)
- if metric_rows is not None
- else await crud.get_health_metrics(session, repository_id)
- )
- graph_metrics = await crud.get_graph_metrics(session, repository_id)
- centrality = {
- node_id: float(metric.get("in_degree") or 0.0) for node_id, metric in graph_metrics.items()
- }
- plans, inputs = await _validation_plans(
- session,
- repository_id,
- suggestions,
- test_limit=test_limit,
- detailed=not rank_only,
- in_degree=centrality,
- step_verify=step_verify and not rank_only,
- )
- recommendations = build_recommendations(
- suggestions,
- metric_by_path={metric.file_path: metric for metric in metrics},
- centrality=centrality,
- validations=dict(enumerate(plans)),
- )
- if rank_only:
- recommendations = [dataclasses.replace(item, inputs=inputs) for item in recommendations]
- return apply_view(recommendations, view)
-
-
-async def detail_recommendations(
- session: AsyncSession,
- repository_id: str,
- recommendations: Sequence[Recommendation],
- *,
- test_limit: int = DEFAULT_TEST_LIMIT,
-) -> list[Recommendation]:
- """*recommendations* with their tests ordered by full evidence, same order.
-
- Only the validation is rebuilt; rank, benefit and the rest come from the
- pass that ranked them, so a page detailed here matches a full hydration.
- """
- if not recommendations:
- return []
- suggestions = [item.suggestion for item in recommendations]
- shared = {id(item.inputs): item.inputs for item in recommendations}
- plans, _ = await _validation_plans(
- session,
- repository_id,
- suggestions,
- test_limit=test_limit,
- detailed=True,
- inputs=next(iter(shared.values())) if len(shared) == 1 else None,
- )
- out = []
- for item, plan in zip(recommendations, plans, strict=True):
- item.suggestion.validation = plan.as_dict()
- out.append(dataclasses.replace(item, validation=plan))
- return out
-
-
-async def _validation_plans(
- session: AsyncSession,
- repository_id: str,
- suggestions: Sequence[RefactoringSuggestion],
- *,
- test_limit: int,
- detailed: bool,
- inputs: ValidationInputs | None = None,
- in_degree: Mapping[str, float] | None = None,
- step_verify: bool = False,
-) -> tuple[list[ValidationPlan], ValidationInputs]:
- """One validation plan per suggestion, every read batched across the set.
-
- *inputs* from an earlier pass over a superset of these suggestions skips
- the coverage read and the reachability walk; only the symbol evidence is
- read again, and only for these suggestions.
- """
- target_files = sorted(
- {path for suggestion in suggestions for path in affected_files(suggestion)}
- )
- if inputs is None:
- inputs = await _validation_inputs(
- session, repository_id, suggestions, target_files, in_degree=in_degree
- )
- evidence = (
- await _validation_evidence(
- session, repository_id, suggestions, target_files, inputs.test_files, inputs.evidence
- )
- if detailed
- else inputs.evidence
- )
- plans = [
- build_validation_plan(
- suggestion,
- inputs.measured,
- inputs.inferred,
- test_limit=test_limit,
- evidence=evidence,
- order_tests=detailed,
- )
- for suggestion in suggestions
- ]
- if step_verify:
- plans = [
- with_step_verify(
- plan,
- suggestion,
- inputs.measured,
- inputs.inferred,
- test_limit=test_limit,
- evidence=evidence,
- )
- for plan, suggestion in zip(plans, suggestions, strict=True)
- ]
- return plans, inputs
-
-
-async def _validation_inputs(
- session: AsyncSession,
- repository_id: str,
- suggestions: Sequence[RefactoringSuggestion],
- target_files: Sequence[str],
- *,
- in_degree: Mapping[str, float] | None = None,
-) -> ValidationInputs:
- """Measured coverage and the tiered reachability walk for every plan's files.
-
- *in_degree* is the stored fan-in a caller already read; without it, read here.
- """
- from repowise.core.persistence import crud
- from repowise.core.persistence.crud.analysis.coverage_map import tests_covering_files
-
- measured = await tests_covering_files(session, repository_id, set(target_files))
-
- # A measured row only answers a target when it intersects the plan's line
- # range (if one exists). Seed inference once with every file that remains
- # unanswered for at least one plan.
- unanswered: set[str] = set()
- for suggestion in suggestions:
- for file_path, lines in _line_ranges(suggestion).items():
- if not _measured_labels(measured.get(file_path, []), lines):
- unanswered.add(file_path)
- test_files = await cached_test_files(session, repository_id)
- if in_degree is None:
- in_degree = {
- node: float(metric.get("in_degree") or 0.0)
- for node, metric in (await crud.get_graph_metrics(session, repository_id)).items()
- }
- # A test that reaches the file only through a hub tests the hub; it is not
- # listed, and with nothing else the plan says no test reaches it.
- hubs = hub_files(in_degree, test_files)
- inferred = (
- await tests_reaching_by_tier(
- session, repository_id, sorted(unanswered), test_files=test_files, avoid=hubs
- )
- if unanswered
- else {}
- )
- unreached = sorted(unanswered - inferred.keys())
- if unreached:
- inferred.update(tests_matching_by_name(unreached, test_files))
- walked = {path: reached.all_tests or tuple(reached.tests) for path, reached in inferred.items()}
- narrowed = await narrow_scopes(session, repository_id, walked, test_files)
- inferred = {
- path: _expand_scopes(path, reached, narrowed[path]) for path, reached in inferred.items()
- }
- hub_targets = [path for path in target_files if path in hubs]
- evidence = await _validation_evidence(
- session,
- repository_id,
- [item for item in suggestions if hubs.intersection(affected_files(item))],
- hub_targets,
- test_files,
- ValidationEvidence(hubs=hubs, siblings=_siblings(in_degree)),
- )
- return ValidationInputs(
- measured=measured, inferred=inferred, test_files=test_files, evidence=evidence
- )
-
-
def _expand_scopes(path: str, reached: ReachedBy, expanded: list[str]) -> ReachedBy:
"""*reached* with a conftest or test package it stopped at replaced by *expanded*.
@@ -1555,50 +1302,6 @@ def _expand_scopes(path: str, reached: ReachedBy, expanded: list[str]) -> Reache
)
-async def _validation_evidence(
- session: AsyncSession,
- repository_id: str,
- suggestions: Sequence[RefactoringSuggestion],
- target_files: Sequence[str],
- test_files: set[str],
- base: ValidationEvidence,
-) -> ValidationEvidence:
- """Symbol spans, symbol-level reach and test imports for every plan at once.
-
- Bounded reads over the whole plan set (the symbol walk is one ``IN`` query
- per hop), so adding plans adds no statements.
- """
- from sqlalchemy import select
-
- from repowise.core.persistence.models import GraphNode
-
- if not target_files:
- return base
- rows = await session.execute(
- select(GraphNode.file_path, GraphNode.node_id, GraphNode.start_line, GraphNode.end_line)
- .where(GraphNode.repository_id == repository_id)
- .where(GraphNode.node_type == "symbol")
- .where(GraphNode.file_path.in_(list(target_files)))
- )
- spans: dict[str, list[SymbolSpan]] = {}
- for file_path, node_id, start, end in rows:
- spans.setdefault(file_path, []).append((node_id, int(start or 0), int(end or 0)))
- symbol_ids = {
- symbol
- for suggestion in suggestions
- for file_path, lines in _line_ranges(suggestion).items()
- for symbol in target_symbol_ids(suggestion, file_path, lines, spans.get(file_path, ()))
- }
- return dataclasses.replace(
- base,
- symbols=spans,
- symbol_reach=await reach_into_symbols(
- session, repository_id, symbol_ids, test_files, avoid=base.hubs
- ),
- imports=await imported_names_by_test(session, repository_id, target_files, test_files),
- )
-
-
def serialize_recommendations(
recommendations: Sequence[Recommendation],
) -> list[dict[str, Any]]:
@@ -1609,10 +1312,7 @@ __all__ = [
"CONFIDENCE_RISK",
"DEFAULT_TEST_LIMIT",
"EFFORT_COST",
- "EFFORT_RANK",
"PLAN_FILTERS",
- "PLAN_SORTS",
- "UNKNOWN_EFFORT",
"Recommendation",
"RecommendationView",
"ValidationEvidence",
@@ -1626,12 +1326,9 @@ __all__ = [
"build_recommendations",
"build_validation_plan",
"canonical_order",
- "detail_recommendations",
"detector_native_benefit",
"enrich_blast_radius",
"hub_files",
- "hydrate_recommendations",
- "matches_search",
"plan_types",
"priority_score",
"rehydrate_suggestion",
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
index 16dfaed58..957461ef0 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py
@@ -53,7 +53,6 @@ from repowise.core.analysis.health.refactoring.models import (
)
from repowise.core.analysis.health.refactoring.recommendations import (
DEFAULT_TEST_LIMIT,
- _validation_inputs,
build_validation_plan,
)
from repowise.core.analysis.health.rows import detail_map
@@ -72,6 +71,7 @@ from ...models import (
)
from ...read_snapshots import decode_or_none, read_snapshot, refresh_snapshot, snapshot_key
from ...sql import json_text
+from .refactoring_recommendations import validation_inputs
#: Files read for plan-less finding items, by open code-shape deduction.
#: Ceiling: a file past this rank never becomes a finding item. The queue
@@ -468,7 +468,7 @@ async def _finding_validator(
if not spans:
return lambda *_: None
suggestions = [_span_suggestion(p, f.function_name, s, e) for (p, s, e), f in spans.items()]
- inputs = await _validation_inputs(
+ inputs = await validation_inputs(
session, repo_id, suggestions, sorted({s.file_path for s in suggestions})
)
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
index 92da7cf8c..8527e5487 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
@@ -359,10 +359,8 @@ async def _plan_facts(
Serving one plan cannot afford the test-reachability walk, and without this
it fell back to an empty profile that read as "no tests" for every plan.
"""
- from ....analysis.health.refactoring.recommendations import (
- hydrate_recommendations,
- steps_with_verify,
- )
+ from ....analysis.health.refactoring.recommendations import steps_with_verify
+ from .refactoring_recommendations import hydrate_recommendations
if not rows:
return {}
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
index 461e22c13..cc6055e0d 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring.py
@@ -17,11 +17,8 @@ from typing import Any
from sqlalchemy import (
Select,
- String,
- Text,
bindparam,
case,
- collate,
func,
or_,
select,
@@ -31,7 +28,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from ....analysis.finding_registry import excluded_types
from ...models import RefactoringSuggestion, _new_uuid, _now_utc
-from ...sql import LIKE_ESCAPE, escape_like, rule_predicate
+from ...sql import rule_predicate
from .._shared import _BATCH_SIZE, _finding_file_path
# The finding-triage vocabulary, shared with health findings so Code Health has
@@ -566,86 +563,23 @@ def _base_order(view: str, predicates: list[Any]) -> tuple[Select[Any], tuple[An
return query, (spread.c.spread_round, spread.c.first)
-def _sort_keys(sort: str, base: tuple[Any, ...], *, byte_order: bool) -> tuple[Any, ...]:
- """A named sort from the shared table, ties broken by the view order.
-
- *byte_order* collates text as Python compares it; SQLite's default already
- does, and it has no ``"C"`` collation to name.
- """
- from ....analysis.health.refactoring.recommendations import (
- EFFORT_RANK,
- PLAN_SORTS,
- UNKNOWN_EFFORT,
- )
-
- if sort == "effort":
- bucket = case(EFFORT_RANK, value=RefactoringSuggestion.effort_bucket, else_=UNKNOWN_EFFORT)
- return (bucket, *base)
- keys = PLAN_SORTS.get(sort)
- if keys is None:
- return base
- ordered = []
- for name, descending in keys:
- column = getattr(RefactoringSuggestion, name)
- if byte_order and isinstance(column.type, String | Text):
- column = collate(column, "C")
- ordered.append(column.desc() if descending else column.asc())
- return (*ordered, *base)
-
-
-def _search_prefilter(search: str) -> list[Any]:
- """Every word of *search* in some searchable column: a superset of the
- exact match, which :func:`matches_search` then decides.
-
- A word ``plan_json`` may store escaped (non-ASCII, a quote, a backslash),
- or SQLite cannot case-fold (non-ASCII), is left to the exact match alone.
- """
- columns = (
- RefactoringSuggestion.file_path,
- RefactoringSuggestion.target_symbol,
- RefactoringSuggestion.refactoring_type,
- RefactoringSuggestion.source_biomarker,
- RefactoringSuggestion.plan_json,
- )
- return [
- or_(
- *(
- func.lower(column).like(f"%{escape_like(word)}%", escape=LIKE_ESCAPE)
- for column in columns
- )
- )
- for word in search.split()
- if word.isascii() and '"' not in word and "\\" not in word
- ]
-
-
async def ranked_refactoring_suggestions(
session: AsyncSession,
repository_id: str,
*,
min_confidence: str | None = None,
filters: Mapping[str, Any] | None = None,
- search: str = "",
- sort: str = "canonical",
view: str = "canonical",
- limit: int | None = None,
- offset: int = 0,
-) -> tuple[list[RefactoringSuggestion], int] | None:
- """One page of open plans in persisted rank order, and its total.
+) -> list[RefactoringSuggestion] | None:
+ """The open plans in persisted rank order.
*filters* are the params of the shared ``PLAN_FILTERS``. They narrow after
the view is dealt, as the in-memory path does, so a filtered
- ``file_spread`` keeps the order the unfiltered one had. A search reads
- every row its SQL prefilter keeps, so its cost is bounded only by that
- prefilter. ``None`` when an open plan was not ranked by the last
- finalize: the caller ranks live.
+ ``file_spread`` keeps the order the unfiltered one had. ``None`` when an
+ open plan was not ranked by the last finalize: the caller ranks live.
"""
from ....analysis.health.queue_rules import active_filters
- from ....analysis.health.refactoring.recommendations import (
- PLAN_FILTERS,
- matches_search,
- rehydrate_suggestion,
- )
+ from ....analysis.health.refactoring.recommendations import PLAN_FILTERS
base = _suggestion_filters(
repository_id,
@@ -666,26 +600,8 @@ async def ranked_refactoring_suggestions(
rule_predicate(RefactoringSuggestion, rule, value)
for rule, value in active_filters(PLAN_FILTERS, filters or {})
]
- byte_order = session.get_bind().dialect.name == "postgresql"
- query = query.where(*narrowing, *_search_prefilter(search)).order_by(
- *_sort_keys(sort, order, byte_order=byte_order)
- )
- if search:
- # The prefilter bounds the read; the exact match is the shared one.
- rows = [
- row
- for row in (await session.execute(query)).scalars().all()
- if matches_search(rehydrate_suggestion(row), search)
- ]
- end = None if limit is None else offset + limit
- return rows[offset:end], len(rows)
- total = int(
- (await session.execute(select(func.count()).select_from(query.subquery()))).scalar_one()
- )
- query = query.offset(offset)
- if limit is not None:
- query = query.limit(limit)
- return list((await session.execute(query)).scalars().all()), total
+ query = query.where(*narrowing).order_by(*order)
+ return list((await session.execute(query)).scalars().all())
async def summarize_open_plans(
@@ -719,3 +635,21 @@ async def summarize_open_plans(
)
)
return summarize_plans(rows.all())
+
+
+async def refactoring_suggestions_by_public_id(
+ session: AsyncSession, repository_id: str, public_ids: list[str]
+) -> list[RefactoringSuggestion]:
+ """The plans named by *public_ids*, in that order, in one indexed query."""
+ if not public_ids:
+ return []
+ rows = (
+ await session.execute(
+ select(RefactoringSuggestion).where(
+ RefactoringSuggestion.repository_id == repository_id,
+ RefactoringSuggestion.public_id.in_(public_ids),
+ )
+ )
+ ).scalars()
+ by_id = {row.public_id: row for row in rows}
+ return [by_id[pid] for pid in public_ids if pid in by_id]
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
index 601beb4d2..3b6f6ecdc 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_opportunities.py
@@ -283,8 +283,8 @@ async def _rank_live_plans(session: AsyncSession, repository_id: str) -> list[An
import dataclasses
from ....analysis.health.refactoring.annotations import annotate, load_annotation_facts
- from ....analysis.health.refactoring.recommendations import hydrate_recommendations
from .refactoring import shown_plan_predicate, store_plan_ranks
+ from .refactoring_recommendations import hydrate_recommendations
rows = (
await session.execute(
@@ -846,11 +846,45 @@ async def refactoring_facet_counts(
return fold_facets(rows.all(), selection)
+async def owning_refactoring_opportunity(
+ session: AsyncSession, repository_id: str, public_id: str | None, file_path: str
+) -> RefactoringOpportunity | None:
+ """The open opportunity holding plan *public_id*, found through its file.
+
+ One indexed lookup on ``(repository_id, status, file_path)``: a plan
+ belongs to at most one file's opportunity, so the file narrows it to a
+ single candidate and the step list confirms it.
+ """
+ from ....analysis.health.refactoring.identity import REFACTORING_MODEL_VERSION
+ from ....analysis.health.rows import detail_map
+
+ if not public_id:
+ return None
+ rows = (
+ await session.execute(
+ select(RefactoringOpportunity)
+ .where(
+ RefactoringOpportunity.repository_id == repository_id,
+ RefactoringOpportunity.status == "open",
+ RefactoringOpportunity.refactoring_model_version == REFACTORING_MODEL_VERSION,
+ RefactoringOpportunity.file_path == file_path,
+ )
+ .limit(5)
+ )
+ ).scalars()
+ for row in rows:
+ steps = detail_map(row).get("steps") or []
+ if any(step.get("plan_id") == public_id for step in steps):
+ return row
+ return None
+
+
__all__ = [
"finalize_refactoring_opportunities",
"get_refactoring_opportunity",
"get_refactoring_summary",
"list_refactoring_opportunities",
+ "owning_refactoring_opportunity",
"refactoring_facet_counts",
"refactoring_opportunities_by_id",
"refactoring_reason_counts",
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py
new file mode 100644
index 000000000..f77b9d597
--- /dev/null
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/refactoring_recommendations.py
@@ -0,0 +1,285 @@
+"""Plan recommendations read from the store: rank, validate and order every plan.
+
+The ranking and validation rules are pure and live in
+``analysis/health/refactoring/recommendations.py``; this module runs the reads
+they need. Every read is batched across the complete plan set, so adding plans
+never adds SQL statements.
+"""
+
+from __future__ import annotations
+
+import dataclasses
+from collections.abc import Mapping, Sequence
+from typing import Any
+
+from sqlalchemy import or_, select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
+from repowise.core.analysis.health.refactoring.recommendations import (
+ DEFAULT_TEST_LIMIT,
+ Recommendation,
+ RecommendationView,
+ SymbolSpan,
+ ValidationEvidence,
+ ValidationInputs,
+ ValidationPlan,
+ _expand_scopes,
+ _line_ranges,
+ _measured_labels,
+ _siblings,
+ affected_files,
+ apply_view,
+ build_recommendations,
+ build_validation_plan,
+ hub_files,
+ rehydrate_suggestion,
+ target_symbol_ids,
+ with_step_verify,
+)
+from repowise.core.analysis.test_collection import narrow_scopes
+from repowise.core.analysis.test_reachability import (
+ cached_test_files,
+ imported_names_by_test,
+ reach_into_symbols,
+ tests_matching_by_name,
+ tests_reaching_by_tier,
+)
+
+from ...models import GraphMetric, GraphNode, HealthFileMetric
+from ..graph import get_graph_metrics
+from .coverage_map import tests_covering_files
+from .health import get_health_metrics
+
+
+async def hydrate_recommendations(
+ session: AsyncSession,
+ repository_id: str,
+ rows: Sequence[Any],
+ *,
+ metric_rows: Sequence[Any] | None = None,
+ view: RecommendationView = "canonical",
+ test_limit: int = DEFAULT_TEST_LIMIT,
+ step_verify: bool = False,
+) -> list[Recommendation]:
+ """Hydrate, enrich, validate, rank, and serialize-ready all *rows*.
+
+ Query shape is constant in plan/test count: health metrics and graph metrics
+ are bulk reads, measured coverage is one ``IN`` query, and inferred walks
+ use their existing bounded level queries over the complete unanswered set.
+
+ *step_verify* also validates each step of a multi-step plan
+ (:func:`with_step_verify`). Finalize asks, so it is stored once; a live
+ read never rebuilds it and serves the plan-level answer.
+ """
+ if not rows:
+ return []
+ suggestions = [rehydrate_suggestion(row) for row in rows]
+ metrics = (
+ list(metric_rows)
+ if metric_rows is not None
+ else await get_health_metrics(session, repository_id)
+ )
+ graph_metrics = await get_graph_metrics(session, repository_id)
+ centrality = {
+ node_id: float(metric.get("in_degree") or 0.0) for node_id, metric in graph_metrics.items()
+ }
+ plans = await _validation_plans(
+ session,
+ repository_id,
+ suggestions,
+ test_limit=test_limit,
+ in_degree=centrality,
+ step_verify=step_verify,
+ )
+ recommendations = build_recommendations(
+ suggestions,
+ metric_by_path={metric.file_path: metric for metric in metrics},
+ centrality=centrality,
+ validations=dict(enumerate(plans)),
+ )
+ return apply_view(recommendations, view)
+
+
+async def _validation_plans(
+ session: AsyncSession,
+ repository_id: str,
+ suggestions: Sequence[RefactoringSuggestion],
+ *,
+ test_limit: int,
+ in_degree: Mapping[str, float] | None = None,
+ step_verify: bool = False,
+) -> list[ValidationPlan]:
+ """One validation plan per suggestion, every read batched across the set."""
+ target_files = sorted(
+ {path for suggestion in suggestions for path in affected_files(suggestion)}
+ )
+ inputs = await validation_inputs(
+ session, repository_id, suggestions, target_files, in_degree=in_degree
+ )
+ evidence = await _validation_evidence(
+ session, repository_id, suggestions, target_files, inputs.test_files, inputs.evidence
+ )
+ plans = [
+ build_validation_plan(
+ suggestion,
+ inputs.measured,
+ inputs.inferred,
+ test_limit=test_limit,
+ evidence=evidence,
+ )
+ for suggestion in suggestions
+ ]
+ if step_verify:
+ plans = [
+ with_step_verify(
+ plan,
+ suggestion,
+ inputs.measured,
+ inputs.inferred,
+ test_limit=test_limit,
+ evidence=evidence,
+ )
+ for plan, suggestion in zip(plans, suggestions, strict=True)
+ ]
+ return plans
+
+
+async def validation_inputs(
+ session: AsyncSession,
+ repository_id: str,
+ suggestions: Sequence[RefactoringSuggestion],
+ target_files: Sequence[str],
+ *,
+ in_degree: Mapping[str, float] | None = None,
+) -> ValidationInputs:
+ """Measured coverage and the tiered reachability walk for every plan's files.
+
+ *in_degree* is the stored fan-in a caller already read; without it, read here.
+ """
+ measured = await tests_covering_files(session, repository_id, set(target_files))
+
+ # A measured row only answers a target when it intersects the plan's line
+ # range (if one exists). Seed inference once with every file that remains
+ # unanswered for at least one plan.
+ unanswered: set[str] = set()
+ for suggestion in suggestions:
+ for file_path, lines in _line_ranges(suggestion).items():
+ if not _measured_labels(measured.get(file_path, []), lines):
+ unanswered.add(file_path)
+ test_files = await cached_test_files(session, repository_id)
+ if in_degree is None:
+ in_degree = {
+ node: float(metric.get("in_degree") or 0.0)
+ for node, metric in (await get_graph_metrics(session, repository_id)).items()
+ }
+ # A test that reaches the file only through a hub tests the hub; it is not
+ # listed, and with nothing else the plan says no test reaches it.
+ hubs = hub_files(in_degree, test_files)
+ inferred = (
+ await tests_reaching_by_tier(
+ session, repository_id, sorted(unanswered), test_files=test_files, avoid=hubs
+ )
+ if unanswered
+ else {}
+ )
+ unreached = sorted(unanswered - inferred.keys())
+ if unreached:
+ inferred.update(tests_matching_by_name(unreached, test_files))
+ walked = {path: reached.all_tests or tuple(reached.tests) for path, reached in inferred.items()}
+ narrowed = await narrow_scopes(session, repository_id, walked, test_files)
+ inferred = {
+ path: _expand_scopes(path, reached, narrowed[path]) for path, reached in inferred.items()
+ }
+ hub_targets = [path for path in target_files if path in hubs]
+ evidence = await _validation_evidence(
+ session,
+ repository_id,
+ [item for item in suggestions if hubs.intersection(affected_files(item))],
+ hub_targets,
+ test_files,
+ ValidationEvidence(hubs=hubs, siblings=_siblings(in_degree)),
+ )
+ return ValidationInputs(
+ measured=measured, inferred=inferred, test_files=test_files, evidence=evidence
+ )
+
+
+async def _validation_evidence(
+ session: AsyncSession,
+ repository_id: str,
+ suggestions: Sequence[RefactoringSuggestion],
+ target_files: Sequence[str],
+ test_files: set[str],
+ base: ValidationEvidence,
+) -> ValidationEvidence:
+ """Symbol spans, symbol-level reach and test imports for every plan at once.
+
+ Bounded reads over the whole plan set (the symbol walk is one ``IN`` query
+ per hop), so adding plans adds no statements.
+ """
+ if not target_files:
+ return base
+ rows = await session.execute(
+ select(GraphNode.file_path, GraphNode.node_id, GraphNode.start_line, GraphNode.end_line)
+ .where(GraphNode.repository_id == repository_id)
+ .where(GraphNode.node_type == "symbol")
+ .where(GraphNode.file_path.in_(list(target_files)))
+ )
+ spans: dict[str, list[SymbolSpan]] = {}
+ for file_path, node_id, start, end in rows:
+ spans.setdefault(file_path, []).append((node_id, int(start or 0), int(end or 0)))
+ symbol_ids = {
+ symbol
+ for suggestion in suggestions
+ for file_path, lines in _line_ranges(suggestion).items()
+ for symbol in target_symbol_ids(suggestion, file_path, lines, spans.get(file_path, ()))
+ }
+ return dataclasses.replace(
+ base,
+ symbols=spans,
+ symbol_reach=await reach_into_symbols(
+ session, repository_id, symbol_ids, test_files, avoid=base.hubs
+ ),
+ imports=await imported_names_by_test(session, repository_id, target_files, test_files),
+ )
+
+
+async def plan_rank_inputs(
+ session: AsyncSession, repository_id: str, file_path: str
+) -> tuple[dict[str, Any], dict[str, float]]:
+ """The two rank inputs for one file, as seeks rather than repo reads.
+
+ ``build_recommendations`` wants a metric per path and an in-degree per
+ node. Serving one plan used to load every metric row and every graph
+ metric in the repository to supply them for a single file.
+ """
+ metric = (
+ await session.execute(
+ select(HealthFileMetric).where(
+ HealthFileMetric.repository_id == repository_id,
+ HealthFileMetric.file_path == file_path,
+ )
+ )
+ ).scalar_one_or_none()
+ rows = (
+ await session.execute(
+ select(GraphMetric.node_id, GraphMetric.in_degree).where(
+ GraphMetric.repository_id == repository_id,
+ or_(
+ GraphMetric.node_id == file_path,
+ # The separator matters: a bare ``f"{file_path}%"`` also
+ # matches a sibling whose name extends this one, so
+ # ``Component.ts`` would absorb ``Component.tsx``.
+ GraphMetric.node_id.like(f"{file_path}::%"),
+ ),
+ )
+ )
+ ).all()
+ return (
+ {metric.file_path: metric} if metric is not None else {},
+ {node_id: float(in_degree or 0.0) for node_id, in_degree in rows},
+ )
+
+
+__all__ = ["hydrate_recommendations", "plan_rank_inputs", "validation_inputs"]
diff --git a/packages/server/src/repowise/server/routers/_local_git.py b/packages/server/src/repowise/server/routers/_local_git.py
index e03a4d4a8..031fc32c9 100644
--- a/packages/server/src/repowise/server/routers/_local_git.py
+++ b/packages/server/src/repowise/server/routers/_local_git.py
@@ -1,9 +1,10 @@
-"""A repository's local checkout, for routes that run git against it."""
+"""A repository's local checkout, for routes that run git or read its ``.repowise``."""
from __future__ import annotations
import os
import subprocess
+from pathlib import Path
from fastapi import Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
@@ -24,6 +25,21 @@ async def resolve_local_repo(
return repo
+async def local_repo_path(session: AsyncSession, repo_id: str) -> Path:
+ """The repo's on-disk checkout, or a 404: a local-``serve`` capability."""
+ # Unlike ``resolve_local_repo``, the 404 says which of the two is missing;
+ # the settings routes have always answered with these two messages.
+ repo = await crud.get_repository(session, repo_id)
+ if repo is None or not repo.local_path:
+ raise HTTPException(status_code=404, detail=f"repository not found: {repo_id}")
+ repo_path = Path(repo.local_path)
+ if not repo_path.exists():
+ raise HTTPException(
+ status_code=404, detail="repository checkout not accessible on this server"
+ )
+ return repo_path
+
+
def revision_exists(repo_path: str, rev: str) -> bool:
# Reject option-shaped input outright; git refuses ref names starting
# with "-", so this loses no legitimate revision and keeps user input
diff --git a/packages/server/src/repowise/server/routers/code_health/__init__.py b/packages/server/src/repowise/server/routers/code_health/__init__.py
index 84b2840a2..e1bb0f721 100644
--- a/packages/server/src/repowise/server/routers/code_health/__init__.py
+++ b/packages/server/src/repowise/server/routers/code_health/__init__.py
@@ -32,7 +32,6 @@ from ._router import router
from .badge import _badge_fields, _render_badge_svg # noqa: F401
from .breakdown import _finding_base_deduction, _score_breakdown_from_findings # noqa: F401
from .overview_routes import _resolve_last_indexed_at # noqa: F401
-from .refactoring_routes import _SORT_KEYS # noqa: F401
# re-export helpers imported by routers/files.py + tests (keep old import path working)
from .serializers import ( # noqa: F401
diff --git a/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py b/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py
index 97c879111..f01fa60c9 100644
--- a/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py
+++ b/packages/server/src/repowise/server/routers/code_health/refactoring_routes.py
@@ -6,19 +6,20 @@ not structured refactoring plans.
from __future__ import annotations
-from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from fastapi import Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession
-from repowise.core.analysis.health.effort import EFFORT_WEIGHT, effort_bucket
-from repowise.core.analysis.health.impact_effort import build_impact_effort, fix_first_marks
-from repowise.core.analysis.health.models import primary_finding, split_by_origin
+from repowise.core.analysis.health.impact_effort import (
+ WORK_QUEUE_SORTS,
+ build_impact_effort,
+ fix_first_marks,
+ work_queue_targets,
+)
from repowise.core.analysis.health.scope import parse_scope
from repowise.core.analysis.health.scoring import ZERO_IMPACT_DIMENSIONS
-from repowise.core.analysis.health.suggestions import suggestion_for as _suggestion_for
from repowise.core.persistence import crud
from repowise.core.persistence.crud.analysis.fix_first import load_fix_first
from repowise.server.deps import get_db_session
@@ -30,24 +31,6 @@ from .file_filters import FAILING_DESCRIPTION, hotspot_paths, metric_filter
from .scope import ScopeQuery, narrow
from .statuses import STATUS_FILTER_DESCRIPTION, parse_status_filter
-_SEVERITY_ORDER = {"low": 0, "medium": 1, "high": 2, "critical": 3}
-
-_SORT_KEYS = {
- "impact_per_effort": lambda t: (-t["impact_per_effort"], -t["total_impact"]),
- "total_impact": lambda t: -t["total_impact"],
- # ``score`` clamps at 1.0, so on a real repo dozens of targets tie at the
- # top and sorting on it alone returns them in dict-insertion order.
- # ``total_impact`` is the same pre-clamp deduction magnitude the crud layer
- # ranks metrics by — but summed over the findings that survived this
- # request's finding-level filters (biomarker, severity, min_severity,
- # dimension, status), so under a filter this ranks by the filtered depth
- # rather than the file's full depth. That is what a filtered queue should
- # do; it just means the order is not expected to match /health/files once
- # a filter is on.
- "score": lambda t: (t["score"], -t["total_impact"], t["file_path"]),
- "finding_count": lambda t: -t["finding_count"],
-}
-
@dataclass(frozen=True, slots=True)
class QueueFilters:
@@ -137,81 +120,6 @@ async def _load_queue_rows(
return {m.file_path: m for m in metrics if keep_metric(m)}, findings
-def _finding_filter(q: QueueFilters) -> Callable[[Any], bool]:
- """One marker, then exact severities or else a severity floor."""
- # An all-empty list (","; " ") means the caller selected nothing, not that
- # nothing matches — falling through to ``min_severity`` keeps a stray
- # serialization from silently emptying the queue behind a 200.
- picked = {v.strip().lower() for v in (q.severity or "").split(",") if v.strip()}
- floor = _SEVERITY_ORDER.get(q.min_severity, 0) if q.min_severity else None
-
- def keep(f: Any) -> bool:
- if q.biomarker and f.biomarker_type != q.biomarker:
- return False
- if picked:
- return (f.severity or "").lower() in picked
- if floor is not None:
- return _SEVERITY_ORDER.get(f.severity, 0) >= floor
- return True
-
- return keep
-
-
-def _group_by_file(findings: list[Any], keep: Callable[[Any], bool]) -> dict[str, list[Any]]:
- by_file: dict[str, list[Any]] = {}
- for f in findings:
- if keep(f):
- by_file.setdefault(f.file_path, []).append(f)
- return by_file
-
-
-def _is_history_only(fs: list[Any], q: QueueFilters, history: str) -> bool:
- # Naming a marker reaches it whatever its origin, as with the zero-impact
- # dimensions in ``_load_queue_rows``.
- return history == "exclude" and q.biomarker is None and not split_by_origin(fs)[0]
-
-
-def _lead_finding(fs: list[Any]) -> Any:
- primary = primary_finding(fs)
- if primary is not None:
- return primary
- # Every finding here is advisory, so no cause accuses this file and the
- # general queue never reaches this: advisory is excluded from it. A caller
- # who filtered to an advisory marker did reach it, and the marker they
- # asked for is the honest lead for the row.
- return max(fs, key=lambda x: (_SEVERITY_ORDER.get(x.severity, 0), -(x.line_start or 0)))
-
-
-def _target_row(file_path: str, fs: list[Any], m: Any, bucket: str) -> dict:
- primary = _lead_finding(fs)
- # Impact, and therefore the ranking, counts only findings still open:
- # ``score`` on this row was computed from open findings, and a file
- # whose findings were all dismissed is not work to rank near the top.
- open_fs = [x for x in fs if (getattr(x, "status", None) or "open") == "open"]
- total_impact = round(sum(x.health_impact for x in open_fs), 3)
- return {
- "file_path": file_path,
- "score": round(m.score, 2),
- "nloc": m.nloc,
- "module": m.module or None,
- "is_test": bool(getattr(m, "is_test", False)),
- "primary_biomarker": primary.biomarker_type,
- "primary_severity": primary.severity,
- "primary_reason": primary.reason,
- "primary_function": primary.function_name,
- "primary_line_start": primary.line_start,
- "primary_line_end": primary.line_end,
- "primary_suggestion": _suggestion_for(primary.biomarker_type),
- "primary_finding_id": primary.id,
- "total_impact": total_impact,
- "finding_count": len(fs),
- "open_finding_count": len(open_fs),
- "biomarkers": sorted({x.biomarker_type for x in fs}),
- "effort_bucket": bucket,
- "impact_per_effort": round(total_impact / EFFORT_WEIGHT[bucket], 3),
- }
-
-
async def _queue_targets(
session: AsyncSession, repo_id: str, q: QueueFilters, *, history: str
) -> tuple[list[dict], int]:
@@ -221,28 +129,15 @@ async def _queue_targets(
raise HTTPException(status_code=404, detail="Repository not found")
metric_by_path, findings = await _load_queue_rows(session, repo_id, q)
- by_file = _group_by_file(findings, _finding_filter(q))
- max_effort_rank = EFFORT_WEIGHT.get(q.max_effort or "", 99)
-
- targets: list[dict] = []
- history_only = 0
- for file_path, fs in by_file.items():
- m = metric_by_path.get(file_path)
- # Absent means either filtered out above, or a file this reading
- # cannot score: under ``code_shape``, a row with no recorded split.
- # Ranking that on a stand-in 10.0 would put an unmeasured file at the
- # top of a list ordered by how bad things are. The same holds for a
- # file in a language health has no dialect for, stored with no score.
- if m is None or m.score is None:
- continue
- if _is_history_only(fs, q, history):
- history_only += 1
- continue
- bucket = effort_bucket(m.nloc)
- if EFFORT_WEIGHT[bucket] > max_effort_rank:
- continue
- targets.append(_target_row(file_path, fs, m, bucket))
- return targets, history_only
+ return work_queue_targets(
+ metric_by_path,
+ findings,
+ biomarker=q.biomarker,
+ severity=q.severity,
+ min_severity=q.min_severity,
+ max_effort=q.max_effort,
+ history=history,
+ )
_HISTORY_QUERY = Query(
@@ -281,7 +176,7 @@ async def health_work_queue(
``GET /health/findings?file_path=``.
"""
targets, history_only = await _queue_targets(session, repo_id, filters, history=history)
- targets.sort(key=_SORT_KEYS[sort])
+ targets.sort(key=WORK_QUEUE_SORTS[sort])
# Both counts, because the view lists files but triages findings: "50 of
# 812 files" alone leaves the size of the work unsaid.
return {
diff --git a/packages/server/src/repowise/server/routers/decisions.py b/packages/server/src/repowise/server/routers/decisions.py
index d4fa6f76d..f3cdb72b6 100644
--- a/packages/server/src/repowise/server/routers/decisions.py
+++ b/packages/server/src/repowise/server/routers/decisions.py
@@ -19,6 +19,7 @@ from repowise.core.analysis.decisions.lifecycle import (
from repowise.core.persistence import crud, decision_graph
from repowise.core.persistence.models import DecisionEvidence
from repowise.server.deps import get_db_session, verify_api_key
+from repowise.server.routers._local_git import local_repo_path
from repowise.server.schemas import (
DecisionCodeEdge,
DecisionCountsResponse,
@@ -349,18 +350,6 @@ async def get_decision_graph(
# ---------------------------------------------------------------------------
-async def _local_repo_path(session: AsyncSession, repo_id: str) -> Path:
- repo = await crud.get_repository(session, repo_id)
- if repo is None or not repo.local_path:
- raise HTTPException(status_code=404, detail=f"repository not found: {repo_id}")
- repo_path = Path(repo.local_path)
- if not repo_path.exists():
- raise HTTPException(
- status_code=404, detail="repository checkout not accessible on this server"
- )
- return repo_path
-
-
def _settings_payload(repo_path: Path, resolution) -> DecisionSettings:
from repowise.core.analysis.decisions.policy_store import policy_etag
@@ -459,7 +448,7 @@ async def get_decision_settings(
session: AsyncSession = Depends(get_db_session),
) -> DecisionSettings:
"""The resolved decision capture policy and source registry."""
- repo_path = await _local_repo_path(session, repo_id)
+ repo_path = await local_repo_path(session, repo_id)
return _settings_payload(repo_path, _load_policy_or_400(repo_path))
@@ -479,7 +468,7 @@ async def update_decision_settings(
"""
from repowise.core.analysis.decisions.policy_store import PolicyConflictError, write_policy
- repo_path = await _local_repo_path(session, repo_id)
+ repo_path = await local_repo_path(session, repo_id)
policy = _apply_settings_update(_load_policy_or_400(repo_path).policy, body)
try:
resolution = write_policy(repo_path, policy, expected_etag=body.etag)
diff --git a/packages/server/src/repowise/server/routers/refactoring.py b/packages/server/src/repowise/server/routers/refactoring.py
index 6e2a517fb..6962463e5 100644
--- a/packages/server/src/repowise/server/routers/refactoring.py
+++ b/packages/server/src/repowise/server/routers/refactoring.py
@@ -7,16 +7,19 @@ web tab, CLI, and MCP share priority components and ordering. Centrality is
leverage; a larger change surface raises cost and risk rather than benefit.
No on-disk work happens here, so this works on hosted backends without a
-checkout — the same property the C4 endpoints rely on.
+checkout — the same property the C4 endpoints rely on. The exceptions are the
+code-generation settings and code generation itself, which need the checkout.
+
+Routes only adapt: reads and ranking are ``services/refactoring_health.py``,
+the settings ``services/refactoring_settings.py``, the shapes
+``schemas/refactoring.py``.
"""
from __future__ import annotations
-from pathlib import Path
from typing import Any, Literal
from fastapi import APIRouter, Depends, HTTPException, Query
-from pydantic import BaseModel, Field
from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.agent_prompts import Flavor, render_opportunity, render_plan
@@ -30,15 +33,25 @@ from repowise.core.analysis.health.refactoring.serving import (
from repowise.core.persistence import crud
from repowise.core.persistence.crud.analysis.refactoring import ALLOWED_STATUSES
from repowise.server.deps import get_db_session, verify_api_key
+from repowise.server.routers._local_git import local_repo_path
from repowise.server.schemas import (
+ GenerateCodeRequest,
+ GenerateCodeResponse,
RefactoringOpportunitiesResponse,
RefactoringOpportunityDetailResponse,
RefactoringOpportunityStatusResponse,
+ RefactoringOpportunityStatusUpdate,
+ RefactoringPlanDetailResponse,
RefactoringPlanStatusResponse,
RefactoringRollupResponse,
+ RefactoringSettings,
+ RefactoringSettingsUpdate,
+ RefactoringStatusUpdate,
+ RefactoringTargetsResponse,
)
from repowise.server.schemas.agent_prompts import AgentPromptResponse
from repowise.server.services.refactoring_health import PlanListQuery, RefactoringHealthService
+from repowise.server.services.refactoring_settings import refactoring_settings, save_llm_enabled
_STEPS_PER_ROW = 3
"""Steps carried on a queue row; the detail call pages the rest."""
@@ -54,117 +67,6 @@ router = APIRouter(
)
-# ---------------------------------------------------------------------------
-# Response shapes (kept local — these surface only the refactoring layer)
-# ---------------------------------------------------------------------------
-
-
-class RefactoringPlanResponse(BaseModel):
- """One ranked refactoring plan, with its open ``plan`` / ``evidence`` /
- ``blast_radius`` dicts re-hydrated from the persisted ``*_json`` columns."""
-
- id: str
- refactoring_type: str
- file_path: str
- target_symbol: str
- line_start: int | None = None
- line_end: int | None = None
- plan: dict[str, Any] = Field(default_factory=dict)
- evidence: dict[str, Any] = Field(default_factory=dict)
- impact_delta: float = 0.0
- effort_bucket: str = ""
- blast_radius: dict[str, Any] = Field(default_factory=dict)
- confidence: str = "medium"
- source_biomarker: str = ""
- benefit: float = 0.0
- leverage: float = 0.0
- cost: float = 0.0
- risk: float = 0.0
- # The unified-rank score (higher = surface sooner). Carried so the tab can
- # plot/sort without recomputing the blend client-side.
- rank_score: float = 0.0
- # Two plot-ready figures, served rather than derived client-side.
- #
- # `dependents` is the file's in-degree — the same centrality the rank reads,
- # so every type reports it the same way. The blast-radius dict was the only
- # other source and it carries the count under `file_count`, `dependents_count`
- # or `callers` depending on which detector wrote it, which is how one file
- # ended up reporting two different dependent counts from two of its plans.
- #
- # Both default to 0 rather than being optional: a repo with no graph metrics
- # or no health pass yet is a real state, and 0 reads as "not measured" in the
- # same place a missing field would have.
- dependents: int = 0
- file_nloc: int = 0
- file_weighted_deficit: int = 0
- validation: dict[str, Any] = Field(default_factory=dict)
-
-
-class PlanRiskResponse(BaseModel):
- kind: str
- text: str
- ref: str | None = None
-
-
-class RefactoringPlanDetailResponse(RefactoringPlanResponse):
- """One plan read alone: what other layers say about its target. Lists stay
- on :class:`RefactoringPlanResponse` and never carry these; both are absent
- on a plan that was never checked."""
-
- governed_by: list[str] | None = None
- risks: list[PlanRiskResponse] | None = None
- # Only with ``include=recipe``: the plan as preconditions, steps and
- # postconditions an agent applies (``refactoring.recipe``).
- recipe: dict[str, Any] | None = None
-
-
-class RefactoringTypeCount(BaseModel):
- type: str
- count: int
-
-
-class RefactoringSummary(BaseModel):
- total: int
- by_type: list[RefactoringTypeCount]
- files_total: int | None = None
- structural_total: int | None = None
- design_total: int | None = None
- performance_total: int | None = None
- small_effort_total: int | None = None
- health_recovery_total: int | None = None
- negligible_health_total: int | None = None
- best_health_gain: float | None = None
-
-
-class RefactoringTargetsResponse(BaseModel):
- summary: RefactoringSummary
- plans: list[RefactoringPlanResponse]
-
-
-class RefactoringPlanPageResponse(BaseModel):
- """Bounded product page; the legacy targets response remains unpaged."""
-
- items: list[RefactoringPlanResponse]
- total: int
- has_more: bool
- next_offset: int | None
- summary: RefactoringSummary
- structural_leads: list[RefactoringPlanResponse]
-
-
-# ---------------------------------------------------------------------------
-# Row → dataclass → response adapters
-# ---------------------------------------------------------------------------
-
-
-def _to_response(data: dict[str, Any]) -> RefactoringPlanResponse:
- return RefactoringPlanResponse(**data)
-
-
-def _csv_values(value: str | None) -> frozenset[str]:
- return frozenset(part.strip() for part in (value or "").split(",") if part.strip())
-
-
# ---------------------------------------------------------------------------
# Endpoints — declare the static `targets` path before the dynamic id path so
# FastAPI matches it first.
@@ -204,94 +106,6 @@ async def get_refactoring_targets(
return RefactoringTargetsResponse(**body)
[email protected]("/{repo_id}/refactoring/targets/page", response_model=RefactoringPlanPageResponse)
-async def get_refactoring_plan_page(
- repo_id: str,
- refactoring_type: str | None = Query(None),
- min_confidence: str | None = Query(None, description="Compatibility confidence floor"),
- confidence: str | None = Query(None, description="Comma-separated exact confidence values"),
- effort: str | None = Query(None, description="Comma-separated effort buckets"),
- file_path: str | None = Query(None),
- search: str | None = Query(None, max_length=200),
- sort: Literal["canonical", "health", "effort", "blast", "file"] = Query("canonical"),
- view: Literal["canonical", "file_spread"] = Query("canonical"),
- limit: int = Query(60, ge=1, le=100),
- offset: int = Query(0, ge=0),
- session: AsyncSession = Depends(get_db_session),
-) -> RefactoringPlanPageResponse:
- """Bounded list with server-owned filters and deterministic ordering.
-
- Reads the rank, factors and validation the finalizer stored: filters,
- order and paging are SQL, so the cost follows the page, not the plan count.
- """
- body = await _service(session, repo_id).ranked_plan_page(
- PlanListQuery(
- refactoring_type=refactoring_type,
- min_confidence=min_confidence,
- confidences=_csv_values(confidence),
- efforts=_csv_values(effort),
- file_path=file_path,
- search=(search or "").strip().lower(),
- sort=sort,
- view=view,
- limit=limit,
- offset=offset,
- )
- )
- return RefactoringPlanPageResponse(**body)
-
-
-# ---------------------------------------------------------------------------
-# Code-gen settings — read/write the refactoring.llm config block. Declared
-# before the dynamic /{suggestion_id} GET so the static `settings` path wins.
-# ---------------------------------------------------------------------------
-
-
-class RefactoringSettings(BaseModel):
- """The code-generation switch plus the model it will use.
-
- ``provider`` / ``model`` are read-only: they come from the same resolver
- chat uses, so the user configures a model once. Never carries a key.
- """
-
- enabled: bool = False
- provider: str | None = None
- model: str | None = None
-
-
-class RefactoringSettingsUpdate(BaseModel):
- """The one writable field, ``refactoring.llm.enabled``."""
-
- enabled: bool
-
-
-def _read_refactoring_settings(
- config: dict[str, Any], repo_id: str, repo_path: Path
-) -> RefactoringSettings:
- """The switch from ``config`` plus the model chat would build, or none."""
- from repowise.core.analysis.health.refactoring.llm import llm_enrichment_enabled
- from repowise.server.provider_config import get_configured_active_provider
-
- provider, model = get_configured_active_provider(repo_id=repo_id, repo_path=repo_path)
- return RefactoringSettings(
- enabled=llm_enrichment_enabled(config), provider=provider, model=model
- )
-
-
-async def _local_repo_path(session: AsyncSession, repo_id: str) -> Path:
- """The repo's on-disk checkout, or a 404 — code-gen settings are a
- local-``serve`` capability (they live in the repo's ``.repowise``)."""
- repo = await crud.get_repository(session, repo_id)
- if repo is None or not repo.local_path:
- raise HTTPException(status_code=404, detail=f"repository not found: {repo_id}")
- repo_path = Path(repo.local_path)
- if not repo_path.exists():
- raise HTTPException(
- status_code=404, detail="repository checkout not accessible on this server"
- )
- return repo_path
-
-
# ---------------------------------------------------------------------------
# Composed opportunities. Thin adapters: filtering, ordering, paging, facets
# and detail all live in ``services/refactoring_health.py``, which the MCP
@@ -447,12 +261,6 @@ async def get_refactoring_opportunity_prompt(
return AgentPromptResponse(flavor=flavor, text=text)
-class RefactoringOpportunityStatusUpdate(BaseModel):
- """The finding-triage vocabulary, applied to a whole opportunity."""
-
- status: str = Field(..., description="open | acknowledged | resolved | false_positive")
-
-
@router.patch(
"/{repo_id}/refactoring/opportunities/{opportunity_id}/status",
response_model=RefactoringOpportunityStatusResponse,
@@ -499,6 +307,10 @@ async def update_refactoring_opportunity_state(
}
+# Code-gen settings: the refactoring.llm config block. Declared before the
+# dynamic /{suggestion_id} GET so the static `settings` path wins.
+
+
@router.get("/{repo_id}/refactoring/settings", response_model=RefactoringSettings)
async def get_refactoring_settings(
repo_id: str,
@@ -507,8 +319,10 @@ async def get_refactoring_settings(
"""Whether code generation is on for the repo, and the provider/model it uses."""
from repowise.core.repo_config import load_repo_config
- repo_path = await _local_repo_path(session, repo_id)
- return _read_refactoring_settings(load_repo_config(repo_path), repo_id, repo_path)
+ repo_path = await local_repo_path(session, repo_id)
+ return RefactoringSettings(
+ **refactoring_settings(load_repo_config(repo_path), repo_id, repo_path)
+ )
@router.put("/{repo_id}/refactoring/settings", response_model=RefactoringSettings)
@@ -517,27 +331,10 @@ async def update_refactoring_settings(
body: RefactoringSettingsUpdate,
session: AsyncSession = Depends(get_db_session),
) -> RefactoringSettings:
- """Write ``refactoring.llm.enabled`` to the repo's ``.repowise/config.yaml``.
-
- Round-trips through the loaded config so unrelated keys are preserved.
- """
- from repowise.core.repo_config import load_repo_config, save_repo_config
-
- repo_path = await _local_repo_path(session, repo_id)
- config = load_repo_config(repo_path)
-
- refactoring = config.get("refactoring")
- if not isinstance(refactoring, dict):
- refactoring = {}
- config["refactoring"] = refactoring
- llm = refactoring.get("llm")
- if not isinstance(llm, dict):
- llm = {}
- refactoring["llm"] = llm
- llm["enabled"] = body.enabled
-
- save_repo_config(repo_path, config)
- return _read_refactoring_settings(config, repo_id, repo_path)
+ """Write ``refactoring.llm.enabled`` to the repo's ``.repowise/config.yaml``."""
+ repo_path = await local_repo_path(session, repo_id)
+ config = save_llm_enabled(repo_path, body.enabled)
+ return RefactoringSettings(**refactoring_settings(config, repo_id, repo_path))
@router.get(
@@ -596,12 +393,6 @@ async def get_refactoring_plan_prompt(
return AgentPromptResponse(flavor=flavor, text=text)
-class RefactoringStatusUpdate(BaseModel):
- """Same shape and vocabulary as health finding triage — one triage system."""
-
- status: str = Field(..., description="open | acknowledged | resolved | false_positive")
-
-
@router.patch(
"/{repo_id}/refactoring/{suggestion_id}/status",
response_model=RefactoringPlanStatusResponse,
@@ -639,31 +430,6 @@ async def update_refactoring_plan_status(
# ---------------------------------------------------------------------------
-class GenerateCodeRequest(BaseModel):
- """Optional per-call provider/model overrides, as chat accepts."""
-
- provider: str | None = None
- model: str | None = None
-
-
-class GenerateCodeResponse(BaseModel):
- """Generated refactored code + diff for one plan, with the self-check."""
-
- suggestion_id: str | None = None
- refactoring_type: str
- file_path: str
- target_symbol: str
- content: str
- diff: str
- provider: str
- model: str
- cached: bool
- input_tokens: int
- output_tokens: int
- validation: dict[str, Any] = Field(default_factory=dict)
- spans: list[dict[str, Any]] = Field(default_factory=list)
-
-
@router.post(
"/{repo_id}/refactoring/{suggestion_id}/generate-code",
response_model=GenerateCodeResponse,
@@ -689,16 +455,7 @@ async def generate_refactoring_code(
from repowise.core.repo_config import load_repo_config
from repowise.server.provider_config import get_chat_provider_instance
- repo = await crud.get_repository(session, repo_id)
- if repo is None or not repo.local_path:
- raise HTTPException(status_code=404, detail=f"repository not found: {repo_id}")
- repo_path = Path(repo.local_path)
- if not repo_path.exists():
- raise HTTPException(
- status_code=404,
- detail="repository checkout not accessible on this server",
- )
-
+ repo_path = await local_repo_path(session, repo_id)
if not llm_enrichment_enabled(load_repo_config(repo_path)):
raise HTTPException(
status_code=403,
diff --git a/packages/server/src/repowise/server/schemas/__init__.py b/packages/server/src/repowise/server/schemas/__init__.py
index 42b5d28a0..3ed7f6773 100644
--- a/packages/server/src/repowise/server/schemas/__init__.py
+++ b/packages/server/src/repowise/server/schemas/__init__.py
@@ -235,12 +235,24 @@ if TYPE_CHECKING:
PatchCoverageTestHint,
)
from .refactoring import (
+ GenerateCodeRequest,
+ GenerateCodeResponse,
+ PlanRiskResponse,
RefactoringHiddenCounts,
RefactoringOpportunitiesResponse,
RefactoringOpportunityDetailResponse,
RefactoringOpportunityStatusResponse,
+ RefactoringOpportunityStatusUpdate,
+ RefactoringPlanDetailResponse,
+ RefactoringPlanResponse,
RefactoringPlanStatusResponse,
RefactoringRollupResponse,
+ RefactoringSettings,
+ RefactoringSettingsUpdate,
+ RefactoringStatusUpdate,
+ RefactoringSummary,
+ RefactoringTargetsResponse,
+ RefactoringTypeCount,
)
from .repository import (
JobAcceptedResponse,
@@ -525,12 +537,24 @@ _EXPORTS: dict[str, tuple[str, ...]] = {
"PatchCoverageTestHint",
),
"refactoring": (
+ "GenerateCodeRequest",
+ "GenerateCodeResponse",
+ "PlanRiskResponse",
"RefactoringHiddenCounts",
"RefactoringOpportunitiesResponse",
"RefactoringOpportunityDetailResponse",
"RefactoringOpportunityStatusResponse",
+ "RefactoringOpportunityStatusUpdate",
+ "RefactoringPlanDetailResponse",
+ "RefactoringPlanResponse",
"RefactoringPlanStatusResponse",
"RefactoringRollupResponse",
+ "RefactoringSettings",
+ "RefactoringSettingsUpdate",
+ "RefactoringStatusUpdate",
+ "RefactoringSummary",
+ "RefactoringTargetsResponse",
+ "RefactoringTypeCount",
),
"repository": (
"JobAcceptedResponse",
@@ -733,6 +757,8 @@ __all__ = [
"FileTrendPointResponse",
"FixHistoryFileResponse",
"FixHistoryResponse",
+ "GenerateCodeRequest",
+ "GenerateCodeResponse",
"GitMetadataResponse",
"GitSummaryResponse",
"GoverningDecisionRef",
@@ -791,6 +817,7 @@ __all__ = [
"PatchCoverageRisky",
"PatchCoverageScope",
"PatchCoverageTestHint",
+ "PlanRiskResponse",
"PopulationBreakdown",
"ProviderEntry",
"ProviderStatusResponse",
@@ -799,8 +826,17 @@ __all__ = [
"RefactoringOpportunitiesResponse",
"RefactoringOpportunityDetailResponse",
"RefactoringOpportunityStatusResponse",
+ "RefactoringOpportunityStatusUpdate",
+ "RefactoringPlanDetailResponse",
+ "RefactoringPlanResponse",
"RefactoringPlanStatusResponse",
"RefactoringRollupResponse",
+ "RefactoringSettings",
+ "RefactoringSettingsUpdate",
+ "RefactoringStatusUpdate",
+ "RefactoringSummary",
+ "RefactoringTargetsResponse",
+ "RefactoringTypeCount",
"RelatedWorkFile",
"RelatedWorkItem",
"RelatedWorkLens",
diff --git a/packages/server/src/repowise/server/schemas/refactoring.py b/packages/server/src/repowise/server/schemas/refactoring.py
index 47b2468cc..1af59daf2 100644
--- a/packages/server/src/repowise/server/schemas/refactoring.py
+++ b/packages/server/src/repowise/server/schemas/refactoring.py
@@ -11,7 +11,7 @@ from __future__ import annotations
from typing import Any, Literal
-from pydantic import BaseModel, ConfigDict
+from pydantic import BaseModel, ConfigDict, Field
from repowise.core.analysis.health.queue.counts import QueueCounts
@@ -100,3 +100,143 @@ class RefactoringOpportunityDetailResponse(BaseModel):
plans: list[dict[str, Any]] | None = None
#: Present when a step names a symbol an earlier step moves.
ordering_note: str | None = None
+
+
+# Plan-level shapes: the plan lists, one plan, triage bodies and code generation.
+
+
+class RefactoringPlanResponse(BaseModel):
+ """One ranked refactoring plan, with its open ``plan`` / ``evidence`` /
+ ``blast_radius`` dicts re-hydrated from the persisted ``*_json`` columns."""
+
+ id: str
+ refactoring_type: str
+ file_path: str
+ target_symbol: str
+ line_start: int | None = None
+ line_end: int | None = None
+ plan: dict[str, Any] = Field(default_factory=dict)
+ evidence: dict[str, Any] = Field(default_factory=dict)
+ impact_delta: float = 0.0
+ effort_bucket: str = ""
+ blast_radius: dict[str, Any] = Field(default_factory=dict)
+ confidence: str = "medium"
+ source_biomarker: str = ""
+ benefit: float = 0.0
+ leverage: float = 0.0
+ cost: float = 0.0
+ risk: float = 0.0
+ # The unified-rank score (higher = surface sooner). Carried so the tab can
+ # plot/sort without recomputing the blend client-side.
+ rank_score: float = 0.0
+ # Two plot-ready figures, served rather than derived client-side.
+ #
+ # `dependents` is the file's in-degree — the same centrality the rank reads,
+ # so every type reports it the same way. The blast-radius dict was the only
+ # other source and it carries the count under `file_count`, `dependents_count`
+ # or `callers` depending on which detector wrote it, which is how one file
+ # ended up reporting two different dependent counts from two of its plans.
+ #
+ # Both default to 0 rather than being optional: a repo with no graph metrics
+ # or no health pass yet is a real state, and 0 reads as "not measured" in the
+ # same place a missing field would have.
+ dependents: int = 0
+ file_nloc: int = 0
+ file_weighted_deficit: int = 0
+ validation: dict[str, Any] = Field(default_factory=dict)
+
+
+class PlanRiskResponse(BaseModel):
+ kind: str
+ text: str
+ ref: str | None = None
+
+
+class RefactoringPlanDetailResponse(RefactoringPlanResponse):
+ """One plan read alone: what other layers say about its target. Lists stay
+ on :class:`RefactoringPlanResponse` and never carry these; both are absent
+ on a plan that was never checked."""
+
+ governed_by: list[str] | None = None
+ risks: list[PlanRiskResponse] | None = None
+ # Only with ``include=recipe``: the plan as preconditions, steps and
+ # postconditions an agent applies (``refactoring.recipe``).
+ recipe: dict[str, Any] | None = None
+
+
+class RefactoringTypeCount(BaseModel):
+ type: str
+ count: int
+
+
+class RefactoringSummary(BaseModel):
+ total: int
+ by_type: list[RefactoringTypeCount]
+ files_total: int | None = None
+ structural_total: int | None = None
+ design_total: int | None = None
+ performance_total: int | None = None
+ small_effort_total: int | None = None
+ health_recovery_total: int | None = None
+ negligible_health_total: int | None = None
+ best_health_gain: float | None = None
+
+
+class RefactoringTargetsResponse(BaseModel):
+ summary: RefactoringSummary
+ plans: list[RefactoringPlanResponse]
+
+
+class RefactoringSettings(BaseModel):
+ """The code-generation switch plus the model it will use.
+
+ ``provider`` / ``model`` are read-only: they come from the same resolver
+ chat uses, so the user configures a model once. Never carries a key.
+ """
+
+ enabled: bool = False
+ provider: str | None = None
+ model: str | None = None
+
+
+class RefactoringSettingsUpdate(BaseModel):
+ """The one writable field, ``refactoring.llm.enabled``."""
+
+ enabled: bool
+
+
+class RefactoringOpportunityStatusUpdate(BaseModel):
+ """The finding-triage vocabulary, applied to a whole opportunity."""
+
+ status: str = Field(..., description="open | acknowledged | resolved | false_positive")
+
+
+class RefactoringStatusUpdate(BaseModel):
+ """Same shape and vocabulary as health finding triage — one triage system."""
+
+ status: str = Field(..., description="open | acknowledged | resolved | false_positive")
+
+
+class GenerateCodeRequest(BaseModel):
+ """Optional per-call provider/model overrides, as chat accepts."""
+
+ provider: str | None = None
+ model: str | None = None
+
+
+class GenerateCodeResponse(BaseModel):
+ """Generated refactored code + diff for one plan, with the self-check."""
+
+ suggestion_id: str | None = None
+ refactoring_type: str
+ file_path: str
+ target_symbol: str
+ content: str
+ diff: str
+ provider: str
+ model: str
+ cached: bool
+ input_tokens: int
+ output_tokens: int
+ validation: dict[str, Any] = Field(default_factory=dict)
+ spans: list[dict[str, Any]] = Field(default_factory=list)
diff --git a/packages/server/src/repowise/server/services/refactoring_health.py b/packages/server/src/repowise/server/services/refactoring_health.py
index e8b688742..13d8dc78c 100644
--- a/packages/server/src/repowise/server/services/refactoring_health.py
+++ b/packages/server/src/repowise/server/services/refactoring_health.py
@@ -21,22 +21,12 @@ from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Literal
-from sqlalchemy import or_, select
from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.health.queue.counts import NOT_JUDGED
-from repowise.core.analysis.health.queue_rules import keep, sort_key
-from repowise.core.analysis.health.refactoring.identity import REFACTORING_MODEL_VERSION
+from repowise.core.analysis.health.queue_rules import keep
from repowise.core.analysis.health.refactoring.recommendations import (
- EFFORT_RANK,
PLAN_FILTERS,
- PLAN_SORTS,
- UNKNOWN_EFFORT,
- apply_view,
- blast_size,
- detail_recommendations,
- hydrate_recommendations,
- matches_search,
plan_types,
stored_recommendation,
)
@@ -58,18 +48,23 @@ from repowise.core.persistence.crud.analysis.queue_counts import any_unjudged, u
from repowise.core.persistence.crud.analysis.refactoring import (
get_refactoring_suggestions,
ranked_refactoring_suggestions,
+ refactoring_suggestions_by_public_id,
summarize_open_plans,
)
from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
get_refactoring_opportunity,
get_refactoring_summary,
list_refactoring_opportunities,
+ owning_refactoring_opportunity,
refactoring_facet_counts,
refactoring_opportunities_by_id,
refactoring_reason_counts,
refactoring_step_counts,
)
-from repowise.core.persistence.models import RefactoringOpportunity, RefactoringSuggestion
+from repowise.core.persistence.crud.analysis.refactoring_recommendations import (
+ hydrate_recommendations,
+ plan_rank_inputs,
+)
def _hidden(left_out: dict[str, int]) -> dict[str, Any]:
@@ -148,68 +143,19 @@ def _plan_window(
@dataclass(frozen=True, slots=True)
class PlanListQuery:
- """One plan-list request: the filters, search, order and page."""
+ """One plan-list request: the filters and the view."""
refactoring_type: str | None = None
min_confidence: str | None = None
- confidences: frozenset[str] = frozenset()
- efforts: frozenset[str] = frozenset()
file_path: str | None = None
- search: str = ""
- sort: str = "canonical"
view: Literal["canonical", "file_spread"] = "canonical"
- limit: int | None = None
- offset: int = 0
def filters(self) -> dict[str, Any]:
"""The params ``PLAN_FILTERS`` reads, on either path."""
return {
"refactoring_types": plan_types(self.refactoring_type),
"file_path": self.file_path,
- "confidences": self.confidences,
- "efforts": self.efforts,
- }
-
-
-#: Structural plans a plan page leads with.
-_STRUCTURAL_LEADS = 12
-
-
-def _live_sorted(items: list[Any], sort: str) -> list[Any]:
- """*items*, in view order, under a named sort from the shared table."""
- position = {id(item): index for index, item in enumerate(items)}
-
- def key(item: Any) -> tuple[Any, ...]:
- suggestion = item.suggestion
- if sort == "effort":
- bucket = EFFORT_RANK.get(suggestion.effort_bucket, UNKNOWN_EFFORT)
- return (bucket, position[id(item)])
- row = {
- "impact_delta": suggestion.impact_delta,
- "blast_size": blast_size(suggestion),
- "file_path": suggestion.file_path,
- "target_symbol": suggestion.target_symbol,
- "id": item.id,
}
- return (*sort_key(PLAN_SORTS[sort], row), position[id(item)])
-
- if sort != "effort" and sort not in PLAN_SORTS:
- return items
- return sorted(items, key=key)
-
-
-def _plan_page_payload(
- items: list[Any], total: int, offset: int, summary: dict[str, Any], leads: list[Any]
-) -> dict[str, Any]:
- next_offset = offset + len(items) if offset + len(items) < total else None
- return {
- "items": [item.as_dict() for item in items],
- "total": total,
- "has_more": next_offset is not None,
- "next_offset": next_offset,
- "summary": summary,
- "structural_leads": [item.as_dict() for item in leads],
- }
class RefactoringHealthService:
@@ -423,7 +369,12 @@ class RefactoringHealthService:
payload["lead_finding_ids"] = list(details.get("lead_finding_ids") or [])
payload["next_actions"] = next_actions(row, page)
if with_plans and page:
- payload["plans"] = await self._plans_for([s["plan_id"] for s in page])
+ payload["plans"] = [
+ plan_payload(row)
+ for row in await refactoring_suggestions_by_public_id(
+ self._session, self._repository_id, [s["plan_id"] for s in page]
+ )
+ ]
# Ordered steps carry ``relocated_by``; a surface that renders them must
# say the symbol has to be located again before the step is applied.
if any(step.get("relocated_by") for step in page):
@@ -445,7 +396,9 @@ class RefactoringHealthService:
row = await get_refactoring_suggestion(self._session, self._repository_id, plan_id)
if row is None:
return {"resolved": False, "plan_id": plan_id, "reason": "unknown_plan_id"}
- owner = await self._owning_opportunity(row.public_id, row.file_path)
+ owner = await owning_refactoring_opportunity(
+ self._session, self._repository_id, row.public_id, row.file_path
+ )
payload = (await self.plan_recommendation(row, owner=owner)).detail_dict()
payload["id"] = row.public_id or row.id
payload["status"] = row.status
@@ -461,11 +414,11 @@ class RefactoringHealthService:
}
return result
- # -- plan lists ---------------------------------------------------------
+ # -- plan list ----------------------------------------------------------
#
- # Each list reads the rank, factors and validation the finalizer stored, so
- # its cost follows the page. A store with an open plan the finalizer did
- # not rank is ranked per request instead. Remove that live path once every
+ # The list reads the rank, factors and validation the finalizer stored. A
+ # store with an open plan the finalizer did not rank is ranked per request
+ # instead. Remove that live path once every
# served store has been indexed by a version that writes ranks.
async def ranked_plans(self, query: PlanListQuery) -> dict[str, Any]:
@@ -485,7 +438,7 @@ class RefactoringHealthService:
chips = await summarize_open_plans(
self._session, self._repository_id, min_confidence=query.min_confidence
)
- plans = [stored_recommendation(row).as_dict() for row in stored[0]]
+ plans = [stored_recommendation(row).as_dict() for row in stored]
else:
rows = await get_refactoring_suggestions(
self._session, self._repository_id, min_confidence=query.min_confidence
@@ -498,77 +451,6 @@ class RefactoringHealthService:
plans = [item.as_dict() for item in ranked]
return {"summary": {"total": chips["total"], "by_type": chips["by_type"]}, "plans": plans}
- async def ranked_plan_page(self, query: PlanListQuery) -> dict[str, Any]:
- """One page of open plans, its total, the chips and the structural leads."""
- stored = await ranked_refactoring_suggestions(
- self._session,
- self._repository_id,
- min_confidence=query.min_confidence,
- filters=query.filters(),
- search=query.search,
- sort=query.sort,
- view=query.view,
- limit=query.limit,
- offset=query.offset,
- )
- if stored is None:
- return await self._live_plan_page(query)
- rows, total = stored
- leads = await ranked_refactoring_suggestions(
- self._session,
- self._repository_id,
- min_confidence=query.min_confidence,
- filters={"refactoring_types": plan_types("structural")},
- limit=_STRUCTURAL_LEADS,
- )
- return _plan_page_payload(
- [stored_recommendation(row) for row in rows],
- total,
- query.offset,
- await summarize_open_plans(
- self._session, self._repository_id, min_confidence=query.min_confidence
- ),
- [stored_recommendation(row) for row in (leads[0] if leads else [])],
- )
-
- async def _live_plan_page(self, query: PlanListQuery) -> dict[str, Any]:
- """The page ranked per request. One batched ranking pass over the open
- plans; the symbol-level evidence that orders each plan's tests is read
- only for the rows this response returns."""
- rows = await get_refactoring_suggestions(
- self._session, self._repository_id, min_confidence=query.min_confidence
- )
- canonical = await hydrate_recommendations(
- self._session, self._repository_id, rows, view="canonical", rank_only=True
- )
- structural = plan_types("structural") or ()
- leads = [item for item in canonical if item.suggestion.refactoring_type in structural]
- leads = leads[:_STRUCTURAL_LEADS]
- filters = query.filters()
- ordered = _live_sorted(
- [
- item
- for item in apply_view(canonical, query.view)
- if keep(PLAN_FILTERS, item.suggestion, filters)
- and (not query.search or matches_search(item.suggestion, query.search))
- ],
- query.sort,
- )
- end = None if query.limit is None else query.offset + query.limit
- page = ordered[query.offset : end]
- shown = list({id(item): item for item in [*page, *leads]}.values())
- detailed = {
- id(item.suggestion): item
- for item in await detail_recommendations(self._session, self._repository_id, shown)
- }
- return _plan_page_payload(
- [detailed[id(item.suggestion)] for item in page],
- len(ordered),
- query.offset,
- summarize_plans(item.suggestion for item in canonical),
- [detailed[id(item.suggestion)] for item in leads],
- )
-
async def plan_recommendation(self, row: Any, *, owner: Any = None) -> Any:
"""One stored plan as a ranked recommendation, without the repository.
@@ -589,8 +471,12 @@ class RefactoringHealthService:
if stored is not None:
return stored
if owner is None:
- owner = await self._owning_opportunity(row.public_id, row.file_path)
- metric_by_path, centrality = await self._rank_inputs(row.file_path)
+ owner = await owning_refactoring_opportunity(
+ self._session, self._repository_id, row.public_id, row.file_path
+ )
+ metric_by_path, centrality = await plan_rank_inputs(
+ self._session, self._repository_id, row.file_path
+ )
return build_recommendations(
[rehydrate_suggestion(row)],
metric_by_path=metric_by_path,
@@ -623,94 +509,5 @@ class RefactoringHealthService:
profile = (detail_map(owner).get("plan") or {}).get("validation") if owner else None
return validation_from_profile(profile) if profile else None
- async def _rank_inputs(self, file_path: str) -> tuple[dict[str, Any], dict[str, float]]:
- """The two rank inputs for one file, as seeks rather than repo reads.
-
- ``build_recommendations`` wants a metric per path and an in-degree per
- node. Serving one plan used to load every metric row and every graph
- metric in the repository to supply them for a single file.
- """
- from repowise.core.persistence.models import GraphMetric, HealthFileMetric
-
- metric = (
- await self._session.execute(
- select(HealthFileMetric).where(
- HealthFileMetric.repository_id == self._repository_id,
- HealthFileMetric.file_path == file_path,
- )
- )
- ).scalar_one_or_none()
- rows = (
- await self._session.execute(
- select(GraphMetric.node_id, GraphMetric.in_degree).where(
- GraphMetric.repository_id == self._repository_id,
- or_(
- GraphMetric.node_id == file_path,
- # The separator matters: a bare ``f"{file_path}%"`` also
- # matches a sibling whose name extends this one, so
- # ``Component.ts`` would absorb ``Component.tsx``.
- GraphMetric.node_id.like(f"{file_path}::%"),
- ),
- )
- )
- ).all()
- return (
- {metric.file_path: metric} if metric is not None else {},
- {node_id: float(in_degree or 0.0) for node_id, in_degree in rows},
- )
-
- # -- internals --------------------------------------------------------
-
- async def _owning_opportunity(self, public_id: str | None, file_path: str) -> Any | None:
- """The opportunity holding this plan, found through its file.
-
- One indexed lookup on ``(repository_id, status, file_path)``: a plan
- belongs to at most one file's opportunity, so the file narrows it to a
- single candidate and the step list confirms it.
- """
- if not public_id:
- return None
- rows = list(
- (
- await self._session.execute(
- select(RefactoringOpportunity)
- .where(
- RefactoringOpportunity.repository_id == self._repository_id,
- RefactoringOpportunity.status == "open",
- RefactoringOpportunity.refactoring_model_version
- == REFACTORING_MODEL_VERSION,
- RefactoringOpportunity.file_path == file_path,
- )
- .limit(5)
- )
- )
- .scalars()
- .all()
- )
- for row in rows:
- steps = detail_map(row).get("steps") or []
- if any(step.get("plan_id") == public_id for step in steps):
- return row
- return None
-
- async def _plans_for(self, plan_ids: list[str]) -> list[dict[str, Any]]:
- """The member plans' payloads, in one indexed query over the page."""
- if not plan_ids:
- return []
- rows = list(
- (
- await self._session.execute(
- select(RefactoringSuggestion).where(
- RefactoringSuggestion.repository_id == self._repository_id,
- RefactoringSuggestion.public_id.in_(plan_ids),
- )
- )
- )
- .scalars()
- .all()
- )
- by_id = {row.public_id: row for row in rows}
- return [plan_payload(by_id[pid]) for pid in plan_ids if pid in by_id]
-
__all__ = ["RefactoringHealthService", "RefactoringPage", "RefactoringPlanPage"]
diff --git a/packages/server/src/repowise/server/services/refactoring_settings.py b/packages/server/src/repowise/server/services/refactoring_settings.py
new file mode 100644
index 000000000..8d42745ed
--- /dev/null
+++ b/packages/server/src/repowise/server/services/refactoring_settings.py
@@ -0,0 +1,44 @@
+"""The refactoring code-generation switch, kept in the repo's ``.repowise/config.yaml``.
+
+The provider and model are read-only: they come from the same resolver chat
+uses, so the user configures a model once. Never carries a key.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+from typing import Any
+
+
+def refactoring_settings(config: dict[str, Any], repo_id: str, repo_path: Path) -> dict[str, Any]:
+ """The switch from *config* plus the model chat would build, or none."""
+ from repowise.core.analysis.health.refactoring.llm import llm_enrichment_enabled
+ from repowise.server.provider_config import get_configured_active_provider
+
+ provider, model = get_configured_active_provider(repo_id=repo_id, repo_path=repo_path)
+ return {"enabled": llm_enrichment_enabled(config), "provider": provider, "model": model}
+
+
+def save_llm_enabled(repo_path: Path, enabled: bool) -> dict[str, Any]:
+ """Write ``refactoring.llm.enabled`` and return the saved config.
+
+ Round-trips through the loaded config so unrelated keys are preserved.
+ """
+ from repowise.core.repo_config import load_repo_config, save_repo_config
+
+ config = load_repo_config(repo_path)
+ refactoring = config.get("refactoring")
+ if not isinstance(refactoring, dict):
+ refactoring = {}
+ config["refactoring"] = refactoring
+ llm = refactoring.get("llm")
+ if not isinstance(llm, dict):
+ llm = {}
+ refactoring["llm"] = llm
+ llm["enabled"] = enabled
+
+ save_repo_config(repo_path, config)
+ return config
+
+
+__all__ = ["refactoring_settings", "save_llm_enabled"]
diff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.ts
index 01030f9af..5541c7cbc 100644
--- a/packages/types/src/generated/http.ts
+++ b/packages/types/src/generated/http.ts
@@ -2794,16 +2794,6 @@ export interface RefactoringPlanDetailResponse {
recipe?: Record<string, unknown> | null;
}
-/** Bounded product page; the legacy targets response remains unpaged. */
-export interface RefactoringPlanPageResponse {
- items: RefactoringPlanResponse[];
- total: number;
- has_more: boolean;
- next_offset: number | null;
- summary: RefactoringSummary;
- structural_leads: RefactoringPlanResponse[];
-}
-
/**
* One ranked refactoring plan, with its open ``plan`` / ``evidence`` /
* ``blast_radius`` dicts re-hydrated from the persisted ``*_json`` columns.
diff --git a/packages/types/src/refactoring.ts b/packages/types/src/refactoring.ts
index 522186344..1f5bd5d6b 100644
--- a/packages/types/src/refactoring.ts
+++ b/packages/types/src/refactoring.ts
@@ -2,7 +2,6 @@
import type { ActionCommand } from "./actions.js";
import type { FixExclusion } from "./fix-first.js";
-import type { Paginated } from "./pagination.js";
import type { QueueCounts } from "./queue.js";
export type RefactoringType =
@@ -241,13 +240,6 @@ export interface RefactoringTargets {
plans: RefactoringPlan[];
}
-/** Bounded product list. The legacy unpaged RefactoringTargets path remains. */
-export interface RefactoringPlanPage extends Paginated<RefactoringPlan> {
- summary: RefactoringSummary;
- /** Bounded canonical structural head used by the existing Start here section. */
- structural_leads: RefactoringPlan[];
-}
-
export interface GeneratedSpan {
file: string;
line_start: number;
diff --git a/tests/unit/health/test_recommendation_surface_parity.py b/tests/unit/health/test_recommendation_surface_parity.py
index 3531cc158..8c71a702c 100644
--- a/tests/unit/health/test_recommendation_surface_parity.py
+++ b/tests/unit/health/test_recommendation_surface_parity.py
@@ -8,7 +8,7 @@ from repowise.cli.commands.health_cmd.refactoring_targets import _render_refacto
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
from repowise.core.analysis.health.refactoring.recommendations import build_recommendations
from repowise.server.mcp_server.tool_health import _serialize_refactoring
-from repowise.server.routers.refactoring import _to_response
+from repowise.server.schemas import RefactoringPlanResponse
def _plan(target: str, file_path: str) -> RefactoringSuggestion:
@@ -36,7 +36,7 @@ def test_golden_surface_fields_and_order_are_identical(fixtures_dir, capsys) ->
[_plan("Beta.run", "src/beta.py"), _plan("Alpha.run", "src/alpha.py")]
)
- rest = [_to_response(item.as_dict()).model_dump() for item in recommendations]
+ rest = [RefactoringPlanResponse(**item.as_dict()).model_dump() for item in recommendations]
mcp = [_serialize_refactoring(item) for item in recommendations]
_render_refactoring_targets([], [], mcp, fmt="json")
cli = json.loads(capsys.readouterr().out)["refactoring_plans"]
diff --git a/tests/unit/health/test_recommendations.py b/tests/unit/health/test_recommendations.py
index 341b12333..1dcdab5f7 100644
--- a/tests/unit/health/test_recommendations.py
+++ b/tests/unit/health/test_recommendations.py
@@ -18,7 +18,6 @@ from repowise.core.analysis.health.refactoring.recommendations import (
build_validation_plan,
detector_native_benefit,
hub_files,
- hydrate_recommendations,
rehydrate_suggestion,
steps_with_verify,
stored_recommendation,
@@ -27,6 +26,9 @@ from repowise.core.analysis.health.refactoring.recommendations import (
)
from repowise.core.analysis.test_reachability import ReachDistance, ReachedBy, clear_test_map_cache
from repowise.core.persistence.crud import save_test_coverage
+from repowise.core.persistence.crud.analysis.refactoring_recommendations import (
+ hydrate_recommendations,
+)
from repowise.core.persistence.database import init_db
from tests.unit.persistence.helpers import insert_repo
diff --git a/tests/unit/persistence/test_test_reachability_walk.py b/tests/unit/persistence/test_test_reachability_walk.py
index e716c509f..c1a8b130d 100644
--- a/tests/unit/persistence/test_test_reachability_walk.py
+++ b/tests/unit/persistence/test_test_reachability_walk.py
@@ -334,12 +334,10 @@ async def test_imported_names_are_read_per_test_and_file(async_session):
assert found == {"src/walk.py": {"tests/test_walk.py": frozenset({"walk"})}}
-async def test_a_detailed_page_matches_a_full_hydration(async_session):
- """Paged surfaces rank every plan without symbol evidence and detail only
- the rows they return; those rows must serialize exactly as before."""
+async def test_hydration_orders_a_plans_tests_by_the_symbol_they_call(async_session):
+ """A test calling the changed symbol leads one that calls only a neighbour."""
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
- from repowise.core.analysis.health.refactoring.recommendations import (
- detail_recommendations,
+ from repowise.core.persistence.crud.analysis.refactoring_recommendations import (
hydrate_recommendations,
)
@@ -393,15 +391,9 @@ async def test_a_detailed_page_matches_a_full_hydration(async_session):
]
full = await hydrate_recommendations(async_session, repo.id, plans())
- ranked = await hydrate_recommendations(async_session, repo.id, plans(), rank_only=True)
- detailed = await detail_recommendations(async_session, repo.id, ranked)
- assert [item.as_dict() for item in detailed] == [item.as_dict() for item in full]
assert full[0].validation.tests == ["tests/test_walk.py", "tests/test_bystander.py"]
assert full[0].validation.reasons["tests/test_walk.py"] == "calls walk"
- # The rank pass orders nothing it will not serve.
- assert ranked[0].validation.reasons == {}
- assert ranked[0].rank_score == full[0].rank_score
async def test_a_seed_that_calls_another_seed_does_not_shorten_its_distance(async_session):
@@ -559,9 +551,9 @@ async def _hub_repo(session, *, tests=(), edges=()):
return repo
-async def _util_validations(session, repo_id, *, rank_only=False):
+async def _util_validations(session, repo_id):
from repowise.core.analysis.health.refactoring.models import RefactoringSuggestion
- from repowise.core.analysis.health.refactoring.recommendations import (
+ from repowise.core.persistence.crud.analysis.refactoring_recommendations import (
hydrate_recommendations,
)
@@ -582,7 +574,7 @@ async def _util_validations(session, repo_id, *, rank_only=False):
)
for symbol, start, end in (("walk", 1, 20), ("lonely", 50, 60))
]
- items = await hydrate_recommendations(session, repo_id, plans, rank_only=rank_only)
+ items = await hydrate_recommendations(session, repo_id, plans)
return {item.suggestion.target_symbol: item for item in items}
@@ -602,17 +594,6 @@ async def test_a_hub_plan_lists_only_tests_reaching_the_changed_symbol(async_ses
assert lonely.prerequisite == (
"No test reaches this; add a characterization test for `lonely` before the edit."
)
- # The rank-only pass a paged list makes narrows the same way.
- ranked = await _util_validations(async_session, repo.id, rank_only=True)
- for symbol, item in full.items():
- fast = ranked[symbol]
- assert (fast.validation.basis, fast.validation.total, fast.validation.prerequisite) == (
- item.validation.basis,
- item.validation.total,
- item.validation.prerequisite,
- )
- assert set(fast.validation.tests) == set(item.validation.tests)
- assert fast.risk == item.risk
async def test_a_hub_import_the_graph_cannot_tie_to_a_symbol_still_counts(async_session):
diff --git a/tests/unit/server/test_health_breakdown.py b/tests/unit/server/test_health_breakdown.py
index c16876ab5..4304bff49 100644
--- a/tests/unit/server/test_health_breakdown.py
+++ b/tests/unit/server/test_health_breakdown.py
@@ -10,10 +10,10 @@ from __future__ import annotations
from types import SimpleNamespace
+from repowise.core.analysis.health.impact_effort import WORK_QUEUE_SORTS
from repowise.core.analysis.health.models import Severity
from repowise.core.analysis.health.scoring import biomarker_weight, severity_deduction
from repowise.server.routers.code_health import (
- _SORT_KEYS,
_finding_base_deduction,
_leads_by_file,
_primary_and_magnitude,
@@ -44,7 +44,7 @@ def test_worst_score_sort_breaks_floored_ties_by_impact() -> None:
_target("m.py", 1.0, 5.0),
_target("ok.py", 6.0, 40.0),
]
- targets.sort(key=_SORT_KEYS["score"])
+ targets.sort(key=WORK_QUEUE_SORTS["score"])
assert [t["file_path"] for t in targets] == ["z.py", "m.py", "a.py", "ok.py"]
@@ -52,7 +52,7 @@ def test_worst_score_sort_is_a_total_order() -> None:
"""Equal score and equal impact still order deterministically, so a
``limit`` boundary does not shuffle between requests."""
targets = [_target("b.py", 1.0, 3.0), _target("a.py", 1.0, 3.0)]
- targets.sort(key=_SORT_KEYS["score"])
+ targets.sort(key=WORK_QUEUE_SORTS["score"])
assert [t["file_path"] for t in targets] == ["a.py", "b.py"]
diff --git a/tests/unit/server/test_refactoring.py b/tests/unit/server/test_refactoring.py
index 9accb6d93..03204ceb5 100644
--- a/tests/unit/server/test_refactoring.py
+++ b/tests/unit/server/test_refactoring.py
@@ -7,9 +7,7 @@ from pathlib import Path
import pytest
from httpx import AsyncClient
-from sqlalchemy import event
-from repowise.core.analysis.test_reachability import clear_test_map_cache
from repowise.core.persistence import (
batch_upsert_graph_edges,
batch_upsert_graph_nodes,
@@ -230,98 +228,7 @@ async def test_type_filter_narrows_plans_but_keeps_summary(client: AsyncClient,
assert body["summary"]["total"] == 4
-async def test_paged_targets_are_bounded_deterministic_and_server_filtered(
- client: AsyncClient, app
-) -> None:
- repo_id = await _seed(client, app)
- legacy = (await client.get(f"/api/repos/{repo_id}/refactoring/targets")).json()
- first = (
- await client.get(f"/api/repos/{repo_id}/refactoring/targets/page", params={"limit": 2})
- ).json()
- second = (
- await client.get(
- f"/api/repos/{repo_id}/refactoring/targets/page",
- params={"limit": 2, "offset": first["next_offset"]},
- )
- ).json()
-
- assert len(first["items"]) == 2
- assert first["total"] == 4
- assert first["has_more"] is True
- assert [item["id"] for item in first["items"] + second["items"]] == [
- item["id"] for item in legacy["plans"]
- ]
-
- filtered = (
- await client.get(
- f"/api/repos/{repo_id}/refactoring/targets/page",
- params={
- "search": "helper.do_work",
- "confidence": "medium",
- "effort": "S",
- "sort": "file",
- },
- )
- ).json()
- assert filtered["total"] == 1
- assert filtered["items"][0]["refactoring_type"] == "move_method"
- assert filtered["summary"]["total"] == 4
-
-
-async def test_paged_targets_query_count_is_constant_as_payload_grows(
- client: AsyncClient, app, test_engine
-) -> None:
- repo_id = await _seed(client, app)
-
- async def request_count(limit: int) -> int:
- # Each request starts from a cold test-map cache, so the count measures
- # the payload, not whether the previous request warmed it.
- clear_test_map_cache()
- statements: list[str] = []
-
- def record(*args) -> None:
- statements.append(str(args[2]))
-
- event.listen(test_engine.sync_engine, "before_cursor_execute", record)
- try:
- response = await client.get(
- f"/api/repos/{repo_id}/refactoring/targets/page", params={"limit": limit}
- )
- assert response.status_code == 200
- finally:
- event.remove(test_engine.sync_engine, "before_cursor_execute", record)
- return len(statements)
-
- small = await request_count(1)
- async with app.state.session_factory() as session:
- await crud.save_refactoring_suggestions(
- session,
- repo_id,
- [
- {
- "refactoring_type": "extract_method",
- "file_path": f"pkg/many_{index}.py",
- "target_symbol": f"work_{index}",
- "line_start": 1,
- "line_end": 30,
- "plan": {"suggested_name": f"part_{index}"},
- "evidence": {"slice_nloc": 12, "ccn_removed": 2},
- "impact_delta": 0.2,
- "effort_bucket": "S",
- "blast_radius": {"files": [f"pkg/many_{index}.py"], "file_count": 1},
- "confidence": "high",
- "source_biomarker": "high_complexity",
- }
- for index in range(80)
- ],
- )
- await session.commit()
-
- large = await request_count(60)
- assert large == small
-
-
-async def test_paged_targets_filter_performance_plans_without_redefining_rank(
+async def test_targets_filter_performance_plans_without_redefining_rank(
client: AsyncClient, app
) -> None:
repo_id = await _seed(client, app)
@@ -350,18 +257,18 @@ async def test_paged_targets_filter_performance_plans_without_redefining_rank(
)
await session.commit()
- all_page = (await client.get(f"/api/repos/{repo_id}/refactoring/targets/page")).json()
+ all_plans = (await client.get(f"/api/repos/{repo_id}/refactoring/targets")).json()
performance = (
await client.get(
- f"/api/repos/{repo_id}/refactoring/targets/page",
+ f"/api/repos/{repo_id}/refactoring/targets",
params={"refactoring_type": "performance_fix"},
)
).json()
expected = next(
- item for item in all_page["items"] if item["refactoring_type"] == "performance_fix"
+ item for item in all_plans["plans"] if item["refactoring_type"] == "performance_fix"
)
- assert performance["items"] == [expected]
- assert performance["summary"]["performance_total"] == 1
+ assert performance["plans"] == [expected]
+ assert {"type": "performance_fix", "count": 1} in performance["summary"]["by_type"]
assert expected["impact_delta"] == 0.0
assert expected["benefit"] > 0
diff --git a/tests/unit/server/test_refactoring_plan_rank.py b/tests/unit/server/test_refactoring_plan_rank.py
index 9fcec7796..d7c565443 100644
--- a/tests/unit/server/test_refactoring_plan_rank.py
+++ b/tests/unit/server/test_refactoring_plan_rank.py
@@ -71,17 +71,8 @@ _QUERIES = [
("targets", {"view": "file_spread"}),
("targets", {"refactoring_type": "extract_method"}),
("targets", {"min_confidence": "medium", "file_path": "pkg/leaf.py"}),
- ("targets/page", {}),
- ("targets/page", {"limit": 2, "offset": 1}),
- ("targets/page", {"view": "file_spread", "refactoring_type": "structural"}),
- ("targets/page", {"sort": "health"}),
- ("targets/page", {"sort": "effort", "view": "file_spread"}),
- ("targets/page", {"sort": "blast"}),
- ("targets/page", {"sort": "file", "limit": 3}),
- ("targets/page", {"search": "helper.do_work"}),
- ("targets/page", {"search": "parsing loop"}),
- ("targets/page", {"confidence": "medium,high", "effort": "S,M", "view": "file_spread"}),
- ("targets/page", {"min_confidence": "medium", "limit": 1, "offset": 2}),
+ ("targets", {"view": "file_spread", "refactoring_type": "structural"}),
+ ("targets", {"min_confidence": "medium", "view": "file_spread"}),
]
@@ -128,10 +119,9 @@ def _no_live_ranking(monkeypatch: pytest.MonkeyPatch) -> None:
raise AssertionError("a ranked store must not be ranked per request")
monkeypatch.setattr(service, "hydrate_recommendations", refuse)
- monkeypatch.setattr(service, "detail_recommendations", refuse)
# The one-plan rebuild from seeks is the fallback too.
monkeypatch.setattr(recommendations, "build_recommendations", refuse)
- monkeypatch.setattr(service.RefactoringHealthService, "_rank_inputs", refuse)
+ monkeypatch.setattr(service, "plan_rank_inputs", refuse)
async def test_stored_rank_answers_every_query_as_live_ranking_did(
@@ -145,11 +135,7 @@ async def test_stored_rank_answers_every_query_as_live_ranking_did(
live = await _answers(client, repo_id)
assert stored == live
- # The rank order is the canonical one: a non-empty default page leads with
- # the highest score among production plans.
- page = stored[4]
- assert page["total"] == 7
- assert [item["id"] for item in page["items"]] == [plan["id"] for plan in stored[0]["plans"]]
+ assert len(stored[0]["plans"]) == 7
async def test_a_plan_finalize_did_not_rank_sends_the_list_live(
@@ -233,63 +219,15 @@ async def test_resolving_a_ranked_plan_drops_it_and_keeps_the_rest_in_order(
client: AsyncClient, app, monkeypatch: pytest.MonkeyPatch
) -> None:
repo_id = await _seed_ranked(client, app)
- before = (await client.get(f"/api/repos/{repo_id}/refactoring/targets/page")).json()["items"]
+ before = (await client.get(f"/api/repos/{repo_id}/refactoring/targets")).json()["plans"]
gone = before[1]["id"]
patched = await client.patch(
f"/api/repos/{repo_id}/refactoring/{gone}/status", json={"status": "resolved"}
)
assert patched.status_code == 200
_no_live_ranking(monkeypatch)
- after = (await client.get(f"/api/repos/{repo_id}/refactoring/targets/page")).json()
- assert [item["id"] for item in after["items"]] == [
- item["id"] for item in before if item["id"] != gone
- ]
- assert after["total"] == len(before) - 1
-
-
-async def test_search_matches_what_the_live_match_accepts(
- client: AsyncClient, app, monkeypatch: pytest.MonkeyPatch
-) -> None:
- """Words the SQL prefilter cannot judge (JSON-escaped, or beyond ASCII case
- folding) still find their plans, and LIKE wildcards are literal."""
- repo_id = await _seed(client, app)
- async with app.state.session_factory() as session:
- for symbol, strategy in (
- ("parse_a_b", "keep 100% of rows"),
- ("parse_axb", "keep 1000 rows"),
- ("Über_load", "inline the Übergabe step"),
- ):
- session.add(
- RefactoringSuggestion(
- **_refactoring_row_kwargs(
- {**_EXTRA[0], "target_symbol": symbol, "plan": {"strategy": strategy}},
- repo_id,
- )
- )
- )
- await session.flush()
- await crud.finalize_refactoring_opportunities(session, repo_id)
- await session.commit()
-
- async def found(word: str) -> list[str]:
- body = (
- await client.get(
- f"/api/repos/{repo_id}/refactoring/targets/page", params={"search": word}
- )
- ).json()
- return sorted(item["target_symbol"] for item in body["items"])
-
- with monkeypatch.context() as patch:
- _no_live_ranking(patch)
- stored = {word: await found(word) for word in ("a_b", "100%", "übergabe", "über_load")}
- assert stored == {
- "a_b": ["parse_a_b"],
- "100%": ["parse_a_b"],
- "übergabe": ["Über_load"],
- "über_load": ["Über_load"],
- }
- await _unrank(app, repo_id)
- assert {word: await found(word) for word in stored} == stored
+ after = (await client.get(f"/api/repos/{repo_id}/refactoring/targets")).json()["plans"]
+ assert [item["id"] for item in after] == [item["id"] for item in before if item["id"] != gone]
async def test_finalize_ranks_only_live_plans(client: AsyncClient, app) -> None:
diff --git a/tests/unit/server/test_refactoring_summary_golden.py b/tests/unit/server/test_refactoring_summary_golden.py
index a85b4282a..912320455 100644
--- a/tests/unit/server/test_refactoring_summary_golden.py
+++ b/tests/unit/server/test_refactoring_summary_golden.py
@@ -10,7 +10,7 @@ from types import SimpleNamespace
from repowise.core.analysis.health.refactoring.recommendations import plan_types
from repowise.core.analysis.health.refactoring_summary import summarize_plans
-from repowise.server.routers.refactoring import RefactoringSummary
+from repowise.server.schemas import RefactoringSummary
def _rec(kind: str, path: str, effort: str, impact: float) -> SimpleNamespace: