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.

feat(health): store each unit's queue verdict and report one count object (#3354)

Raghav Chamadiyae7d8c03a4382
diff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.md
index 42d2308d8..b6a5d3c62 100644
--- a/docs/layers/CODE_HEALTH.md
+++ b/docs/layers/CODE_HEALTH.md
@@ -27,7 +27,7 @@ The dashboard (`repowise serve`) has a Code Health page per repository at
`/repos/<id>/code-health`.
```
-Fix first (3 of 41 eligible; tests, tooling and history-only files left out)
+Fix first (3 of 41 eligible items, 12 due; 230 in the inventory, tests, tooling and history-only files left out)
1. Extract the retry branch from parse_config now, effort M, +1.4 health
src/config/loader.py:212
CCN 47 and nesting 6 in a file changed 19 times this quarter.
@@ -117,6 +117,11 @@ places while another kind has work worth doing.
| `next` | Worth doing, needs judgment |
| `later` | Real but can wait; the reason is shown |
+Findings, performance causes, refactoring plans and Fix first items are counted the
+same way everywhere: open in the inventory, in scope, eligible, due (tier `now` or
+`next`) and shown, with every exclusion named by its reason. The numbers on the
+dashboard, in `get_health` and in `repowise health --format json` (`queue_counts`) agree.
+
### Do next
`repowise next` prints the cross-layer list the dashboard calls **Do next** and
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.md
index 146b12324..ccc63c891 100644
--- a/docs/layers/REFACTORING.md
+++ b/docs/layers/REFACTORING.md
@@ -116,7 +116,9 @@ in the code anchors one. A Split File or Extract Class plan with an unnamed grou
is held as `needs_design`, like a cycle: it is not a step and never reaches Fix
first. A plan led by Move Method or Extract Class stays in the full list but is
kept out of Fix first and the default scope, counted as not yet audited, until
-that kind passes a per-language review. Code only moves within a language family,
+that kind passes a per-language review. When such a step follows another lead, the
+Fix first item leaves it out and says how many steps it held back; the full plan
+keeps them. Code only moves within a language family,
so Java, Kotlin and Scala are separate. A callback is named after the call it is
passed to. An Extract Method helper is named from a stage label, a banner comment,
or `compute_<value>` when the span has no outside effects. A name already taken
diff --git a/docs/reference/MCP_RESPONSE_FIELDS.md b/docs/reference/MCP_RESPONSE_FIELDS.md
index 7984a147d..b7b9caa4d 100644
--- a/docs/reference/MCP_RESPONSE_FIELDS.md
+++ b/docs/reference/MCP_RESPONSE_FIELDS.md
@@ -334,6 +334,8 @@ One ranked queue across refactoring, performance and code-shape work, the same o
| `totals` | `candidates`, `eligible`, `shown`, `excluded` counted by reason (`gated_off`: a constant flag in the same file switches the function off; `unreachable`: sure dead code, delete it), and `dormant`: distinct functions kept out as `gated_off` |
| `by_improves`, `basis`, `model_version`, `detail_call` | Rollup, basis, and the call that opens the lead in full |
+Findings, performance causes and refactoring plans carry the same count object when their list is named in `only`: `top_findings_counts`, `performance_opportunities_counts`, `refactoring_plans_counts`. The CLI and dashboard report the same numbers.
+
Items are ordered by value first, then tier. History markers (churn, ownership, co-change, prior fixes) never create or lead an item; they ride on an item under `context`, and history-only files count under `totals.excluded.history_only`. `fix_first` always describes production code, whatever `scope` says; `limit` caps the items, never which leads.
### Dashboard and metric rows
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/command.py b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
index 767470214..eacfea9c0 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/command.py
@@ -42,6 +42,7 @@ from .codegen import _generate_refactoring_code
from .persist import (
_load_fix_first,
_load_persisted_coverage_map,
+ _load_queue_counts,
_load_recommendations,
_load_stored_report,
_persist_health,
@@ -337,6 +338,9 @@ def health_command(
"kpis": report.kpis,
"scope": parse_scope(scope),
"counts": parse_counts(counts),
+ # Findings, causes, plans and Fix first items, as every
+ # surface counts them; ``shown`` is 0, nothing is listed.
+ "queue_counts": _load_queue_counts(repo_path),
"metrics": [
{
"file_path": m.file_path,
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 9774d5920..eaf683474 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/persist.py
@@ -52,21 +52,43 @@ def _load_persisted_coverage_map(repo_path: object) -> dict[str, dict]:
return {}
-def _load_fix_first(repo_path: object, *, limit: int) -> Any:
- """The stored Fix-first queue, or ``None`` when the store cannot answer.
+def _load_fix_first(repo_path: object, *, limit: int) -> tuple[Any, Any] | None:
+ """The stored Fix-first head and the items' stored counts, or ``None``
+ when the store cannot answer.
Best-effort like the coverage read: a missing repo row or an older store
leaves the report without the section; the report itself still prints.
"""
from repowise.cli.helpers import repo_index_session
from repowise.core.persistence.crud.analysis.fix_first import load_fix_first
+ from repowise.core.persistence.crud.analysis.queue_counts import unit_counts
async def _do() -> Any:
async with repo_index_session(Path(str(repo_path))) as opened:
if opened is None:
return None
session, repo_id = opened
- return await load_fix_first(session, repo_id, limit=limit)
+ queue = await load_fix_first(session, repo_id, limit=limit)
+ counts = await unit_counts(session, repo_id, "items", shown=len(queue.items))
+ return queue, counts
+
+ try:
+ return run_async(_do())
+ except Exception:
+ return None
+
+
+def _load_queue_counts(repo_path: object) -> dict[str, Any] | None:
+ """Every queue unit's stored counts, by noun; ``None`` when the store cannot answer."""
+ from repowise.cli.helpers import repo_index_session
+ from repowise.core.persistence.crud.analysis.queue_counts import all_unit_counts
+
+ async def _do() -> Any:
+ async with repo_index_session(Path(str(repo_path))) as opened:
+ if opened is None:
+ return None
+ session, repo_id = opened
+ return await all_unit_counts(session, repo_id)
try:
return run_async(_do())
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py b/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
index 11f143186..37f55e12d 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/refactoring_targets.py
@@ -155,6 +155,7 @@ def _render_stored_refactoring_targets(
"summary": summary,
"total": page.total,
"hidden": page.hidden,
+ "counts": page.counts,
"details": details,
"metrics": metrics,
"findings": findings,
@@ -171,11 +172,14 @@ def _render_stored_refactoring_targets(
analyzed_commit = stored["summary"].get("analyzed_commit")
if fmt == "table":
commit = f" at {analyzed_commit[:7]}" if analyzed_commit else ""
- hidden = (stored["hidden"] or {}).get("total") or 0
+ counts = stored["counts"]
+ # Each number names its level, so "of N" never mixes the eligible
+ # queue with the inventory.
scope = (
- f"{stored['total']} worth doing ({hidden} more in the full inventory)"
+ f"{stored['total']:,} eligible plans ({counts['inventory']:,} open, "
+ f"{counts['due']:,} due now or next)"
if stored["hidden"] is not None
- else f"{stored['total']} open opportunities"
+ else f"{stored['total']:,} open plans ({counts['eligible']:,} eligible)"
)
console.print(
f"[dim]Read from the index{commit}. Showing {len(rows)} of {scope}. "
@@ -193,7 +197,9 @@ def _render_stored_refactoring_targets(
"source": "index",
"analyzed_commit": analyzed_commit,
"opportunities_total": stored["total"],
+ # Restated by ``counts.excluded``; kept for scripts, remove next major.
"opportunities_hidden": stored["hidden"],
+ "counts": stored["counts"],
"targets": targets,
"refactoring_opportunities": rows,
"refactoring_plans": [_list_row(plan) for plan in plans],
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd/summary.py b/packages/cli/src/repowise/cli/commands/health_cmd/summary.py
index 96f95e85a..2d412c35d 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd/summary.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd/summary.py
@@ -11,21 +11,24 @@ from typing import Any
from repowise.cli.helpers import console
-def _render_fix_first(queue: Any) -> None:
- """What to fix first, as core ranks it: title, where, why, effort, how to verify."""
+def _render_fix_first(loaded: tuple[Any, Any] | None) -> None:
+ """What to fix first, as core ranks it: title, where, why, effort, how to
+ verify. ``loaded`` is the queue's head and the items' stored counts."""
from rich.markup import escape
- if queue is None:
+ if loaded is None:
return
+ queue, counts = loaded
if not queue.items:
console.print("\n[bold]Fix first[/bold]: nothing eligible in the stored analysis.")
return
- totals = queue.totals
+ shown = queue.items
console.print(
- f"\n[bold]Fix first[/bold] [dim]({totals.shown} of {totals.eligible} eligible; "
- "tests, tooling and history-only files left out)[/dim]"
+ f"\n[bold]Fix first[/bold] [dim]({counts.shown} of {counts.eligible:,} eligible items, "
+ f"{counts.due:,} due; {counts.inventory:,} in the inventory, tests, tooling and "
+ "history-only files left out)[/dim]"
)
- for item in queue.items:
+ for item in shown:
target = item.target
where = target.file_path + (f":{target.line_start}" if target.line_start else "")
console.print(
diff --git a/packages/core/alembic/versions/0103_queue_verdicts.py b/packages/core/alembic/versions/0103_queue_verdicts.py
new file mode 100644
index 000000000..f66e12f47
--- /dev/null
+++ b/packages/core/alembic/versions/0103_queue_verdicts.py
@@ -0,0 +1,66 @@
+"""Persist each unit's default-queue verdict: eligible, reason, value, tier.
+
+Findings, performance opportunities and refactoring opportunities are judged
+once per index by ``analysis.health.queue``, so default lists filter and
+count on a column instead of re-deriving the rule per request. NULL means the
+unit was not judged yet; the next index writes it.
+
+Local SQLite stores get the columns from ``init_db``'s additive reconciler;
+this migration covers managed Postgres.
+
+Revision ID: 0103
+Revises: 0102
+Create Date: 2026-10-10
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers
+revision: str = "0103"
+down_revision: str | None = "0102"
+branch_labels: str | Sequence[str] | None = None
+depends_on: str | Sequence[str] | None = None
+
+_TABLES = ("health_findings", "performance_opportunities", "refactoring_opportunities")
+_INDEXES = (
+ (
+ "ix_performance_opportunities_repo_status_queue",
+ "performance_opportunities",
+ ["repository_id", "status", "queue_eligible", "rank_position"],
+ ),
+ (
+ "ix_refactoring_opportunities_repo_status_eligible",
+ "refactoring_opportunities",
+ ["repository_id", "status", "queue_eligible", "queue_position"],
+ ),
+ (
+ "ix_health_findings_repo_status_queue",
+ "health_findings",
+ ["repository_id", "status", "queue_eligible"],
+ ),
+)
+
+
+def upgrade() -> None:
+ for table in _TABLES:
+ op.add_column(table, sa.Column("queue_eligible", sa.Boolean(), nullable=True))
+ op.add_column(table, sa.Column("queue_reason", sa.String(32), nullable=True))
+ op.add_column(table, sa.Column("queue_value", sa.Integer(), nullable=True))
+ op.add_column(table, sa.Column("queue_tier", sa.String(8), nullable=True))
+ for name, table, columns in _INDEXES:
+ op.create_index(name, table, columns)
+
+
+def downgrade() -> None:
+ for name, table, _columns in _INDEXES:
+ op.drop_index(name, table_name=table)
+ # Batch mode rebuilds the table on SQLite, which cannot drop a column in place.
+ for table in _TABLES:
+ with op.batch_alter_table(table) as batch:
+ for column in ("queue_tier", "queue_value", "queue_reason", "queue_eligible"):
+ batch.drop_column(column)
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/build.py b/packages/core/src/repowise/core/analysis/health/fix_first/build.py
index a364c2f06..27379721a 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/build.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/build.py
@@ -59,9 +59,11 @@ from typing import Any
from repowise.core.analysis.finding_registry import excluded_types
from repowise.core.analysis.health.complexity.dispatch import DISPATCH_SHARE
from repowise.core.analysis.health.models import primary_finding, split_by_origin
+from repowise.core.analysis.health.queue.counts import COVERED_BY_PLAN, NOT_FILE_LEAD, Judgement
from repowise.core.analysis.health.queue.eligibility import (
DEAD_CONFIDENCE,
DEFAULT_QUEUE_CONTEXTS,
+ UNAUDITED_KINDS,
DeadSpan,
Tally,
Verdict,
@@ -75,6 +77,8 @@ from repowise.core.analysis.health.queue.eligibility import (
)
from repowise.core.analysis.health.queue.order import LEVEL_RANK, order
from repowise.core.analysis.health.queue.value import (
+ perf_confidence,
+ perf_ready,
perf_value,
shape_value,
size_value,
@@ -592,6 +596,7 @@ def _refactor_unit(
) -> _Unit:
path = field(row, "file_path")
lead = steps[0]
+ steps, held_back = _audited_steps(steps)
lead_type = lead.get("refactoring_type") or field(row, "lead_refactoring_type") or ""
sym = text.short_symbol(lead.get("target_symbol")) or text.basename(path)
marker = field(row, "lead_biomarker") or (
@@ -654,6 +659,7 @@ def _refactor_unit(
FixFact("health recoverable", f"+{gain:.1f}", "inferred"),
*([FixFact("size", size_text)] if size_text else []),
FixFact("steps", f"{len(steps)} ({mechanical_n} mechanical)"),
+ *([FixFact("steps held back", text.held_back(held_back))] if held_back else []),
*files.common_facts(path, dependents),
]
nloc = files.nloc(path)
@@ -733,6 +739,13 @@ def _refactor_unit(
)
+def _audited_steps(steps: list[Mapping[str, Any]]) -> tuple[list[Mapping[str, Any]], int]:
+ """The steps an item may carry, and how many it held back: steps of a
+ kind not yet audited stay in the full plan, never in the item."""
+ kept = [s for s in steps if s.get("refactoring_type") not in UNAUDITED_KINDS]
+ return kept, len(steps) - len(kept)
+
+
def _refactor_measure(
kind: str, sym: str, path: str, step: Mapping[str, Any], plan: Any, files: _Files
) -> str | None:
@@ -800,9 +813,6 @@ def _perf_unit(
mechanical = bool(plan_steps) and all(
s.get("applicability") == "mechanical" for s in plan_steps
)
- ready = (
- field(lead, "actionability_state") == "plan_ready" or field(lead, "fix_safety") == "proven"
- )
effort = plan.get("effort_bucket")
def fields() -> dict[str, Any]:
@@ -917,14 +927,13 @@ def _perf_unit(
),
}
- confidence = facets.get("actionability_confidence") or "low"
- confidence = confidence if confidence in LEVEL_RANK else "low"
+ confidence = perf_confidence(facets)
return _finish(
kind="perf_fix",
may_lead=details.get("may_lead") is not False,
source_id=f"{path}::{symbol or path}",
value=perf_value(lead, facets),
- ready=ready or mechanical,
+ ready=perf_ready(lead, plan),
score=_num(field(lead, "rank_score")),
confidence=confidence,
effort=effort or "M",
@@ -945,18 +954,49 @@ def _perf_unit(
# --- findings with no plan ----------------------------------------------------------
+@dataclass(frozen=True, slots=True)
+class _FindingRank:
+ """What a finding's value and tier read: one rule for the item and the stored judgement."""
+
+ shape: dict[str, int]
+ hot: bool
+ cloned: bool
+ low: str | None
+ value: int
+ cold_value: int
+
+ @property
+ def tier(self) -> str:
+ return tier(self.cold_value, "medium", False, self.low)[0]
+
+
+def _finding_rank(finding: Any, files: _Files) -> _FindingRank:
+ path = field(finding, "file_path")
+ marker = field(finding, "biomarker_type") or ""
+ impact = _num(field(finding, "health_impact"))
+ hot = files.hot(path)
+ shape = files.shape(path, field(finding, "function_name"))
+ cloned = marker in SIZE_MARKERS and files.cloned(path, shape)
+ return _FindingRank(
+ shape,
+ hot,
+ cloned,
+ low_priority(marker, shape, error_kind=detail_map(finding).get("kind")),
+ shape_value(impact, shape, cloned, hot=hot),
+ shape_value(impact, shape, cloned, hot=False),
+ )
+
+
def _finding_unit(lead: Any, files: _Files, first: FixStep, validate: Validate | None) -> _Unit:
path = field(lead, "file_path")
marker = field(lead, "biomarker_type") or ""
function = field(lead, "function_name")
impact = _num(field(lead, "health_impact"))
- hot = files.hot(path)
public_id = field(lead, "public_id")
dimension = biomarker_dimension(marker)
- shape = files.shape(path, function)
+ rank = _finding_rank(lead, files)
+ shape, hot, cloned, low = rank.shape, rank.hot, rank.cloned, rank.low
size = size_value(shape, hot)
- cloned = marker in SIZE_MARKERS and files.cloned(path, shape)
- low = low_priority(marker, shape, error_kind=detail_map(lead).get("kind"))
def fields() -> dict[str, Any]:
where = function or text.basename(path)
@@ -1012,8 +1052,8 @@ def _finding_unit(lead: Any, files: _Files, first: FixStep, validate: Validate |
return _finish(
kind="finding",
source_id=public_id or f"{path}::{marker}::{function or ''}",
- value=shape_value(impact, shape, cloned, hot=hot),
- cold_value=shape_value(impact, shape, cloned, hot=False),
+ value=rank.value,
+ cold_value=rank.cold_value,
ready=False,
score=impact,
confidence="medium",
@@ -1077,6 +1117,104 @@ def _basis(metrics: Rows) -> dict[str, str | None]:
}
+def _shown_findings(findings: Iterable[Any]) -> dict[str, list[Any]]:
+ """Open code-health findings that score, of a shown type, by file."""
+ hidden = excluded_types()
+ by_file: dict[str, list[Any]] = defaultdict(list)
+ for f in findings:
+ if (
+ _num(field(f, "health_impact")) > 0
+ and field(f, "biomarker_type") not in hidden
+ and field(f, "dimension") != "performance"
+ and _open(f)
+ ):
+ by_file[field(f, "file_path")].append(f)
+ return by_file
+
+
+def _prepare(
+ metrics: list[Any],
+ findings: Iterable[Any],
+ plans: list[Any],
+ dead_code: Rows,
+ hot_cuts: tuple[float, float] | None,
+) -> tuple[dict[str, tuple[list[Any], list[Any]]], _Files]:
+ """Findings split into (code shape, history) by file, and the file facts."""
+ split = {path: split_by_origin(rows) for path, rows in _shown_findings(findings).items()}
+ files = _Files(
+ metrics,
+ {p: hist for p, (_shape, hist) in split.items() if hist},
+ _by_function(f for shape, _hist in split.values() for f in shape),
+ hot_cuts,
+ _clone_spans(plans),
+ _extractions(plans),
+ _dead_spans(dead_code),
+ )
+ return split, files
+
+
+def judge_findings(
+ *,
+ metrics: Rows = (),
+ findings: Rows = (),
+ plans: Rows = (),
+ dead_code: Rows = (),
+ hot_cuts: tuple[float, float] | None = None,
+ planned_files: Iterable[str] = (),
+) -> dict[Any, Judgement]:
+ """Each open code-health finding's place in the queue, keyed by its ``id``.
+
+ The rule Fix first applies to a file's findings: a file whose plan became
+ an item is that plan's (``covered_by_plan``); then the file's scope, then
+ history-only markers, then :func:`finding_verdict`; of a file's eligible
+ findings only the one that leads its item stays eligible (``not_file_lead``),
+ valued and tiered as that item. ``planned_files`` are the files of the
+ eligible refactoring plans. Pass every open finding of the files judged, so
+ a function's shape is whole.
+ """
+ metrics = list(metrics)
+ split, files = _prepare(metrics, findings, list(plans), dead_code, hot_cuts)
+ planned = set(planned_files)
+ out: dict[Any, Judgement] = {}
+ for path, (shape, history) in split.items():
+ if path in planned:
+ out.update(dict.fromkeys((field(f, "id") for f in (*shape, *history)), _COVERED))
+ continue
+ scope = path_verdict(path, files.is_test(path), None, files.origin(path))
+ history_verdict = scope if not scope.eligible else Verdict("history_only")
+ for f in history:
+ out[field(f, "id")] = Judgement.of(history_verdict)
+ out.update(_judge_file_shape(shape, scope, files))
+ return out
+
+
+_COVERED = Judgement(COVERED_BY_PLAN)
+_NOT_LEAD = Judgement(NOT_FILE_LEAD)
+
+
+def _judge_file_shape(shape: list[Any], scope: Verdict, files: _Files) -> dict[Any, Judgement]:
+ """One file's code-shape findings: only the lead of the eligible ones is eligible."""
+ if not scope.eligible:
+ return {field(f, "id"): Judgement.of(scope) for f in shape}
+
+ def first_step(finding: Any) -> bool:
+ return files.first_step(finding) is not None
+
+ verdicts = {id(f): finding_verdict(f, files, first_step) for f in shape}
+ lead = primary_finding([f for f in shape if verdicts[id(f)].eligible])
+ out: dict[Any, Judgement] = {}
+ for f in shape:
+ verdict = verdicts[id(f)]
+ if not verdict.eligible:
+ out[field(f, "id")] = Judgement.of(verdict)
+ elif f is not lead:
+ out[field(f, "id")] = _NOT_LEAD
+ else:
+ rank = _finding_rank(f, files)
+ out[field(f, "id")] = Judgement(None, rank.value, rank.tier)
+ return out
+
+
def build_fix_first(
*,
metrics: Rows = (),
@@ -1092,6 +1230,7 @@ def build_fix_first(
hot_cuts: tuple[float, float] | None = None,
symbol_lines: Mapping[str, int] | None = None,
validate: Validate | None = None,
+ judged: dict[str, Judgement] | None = None,
) -> FixFirstQueue:
"""One ranked queue of what to fix, from stored rows (shapes in the module docstring).
@@ -1107,33 +1246,13 @@ def build_fix_first(
is the validation profile (``basis``, ``via``, ``total``, ``tests``,
``commands``) of a finding with no plan, read lazily for the items shown;
without it such an item's Verify stays unknown. ``dead_code`` rows make
- the units they cover ``unreachable``.
+ the units they cover ``unreachable``. ``judged`` is filled with every open
+ refactoring opportunity's judgement, by id, for the index to store.
"""
metrics = list(metrics)
- findings = list(findings)
keep_tests = scope == "all"
- hidden = excluded_types()
-
- by_file: dict[str, list[Any]] = defaultdict(list)
- for f in findings:
- if (
- _num(field(f, "health_impact")) > 0
- and field(f, "biomarker_type") not in hidden
- and field(f, "dimension") != "performance"
- and _open(f)
- ):
- by_file[field(f, "file_path")].append(f)
- split = {path: split_by_origin(rows) for path, rows in by_file.items()}
plans = list(plans)
- files = _Files(
- metrics,
- {p: hist for p, (_shape, hist) in split.items() if hist},
- _by_function(f for shape, _hist in split.values() for f in shape),
- hot_cuts,
- _clone_spans(plans),
- _extractions(plans),
- _dead_spans(dead_code),
- )
+ split, files = _prepare(metrics, findings, plans, dead_code, hot_cuts)
tally = Tally(FIX_EXCLUSIONS)
def exclude(verdict: Verdict, path: str, symbol: str | None = None) -> bool:
@@ -1154,7 +1273,6 @@ def build_fix_first(
units: list[_Unit] = []
planned_files: set[str] = set()
- refactoring_reasons: dict[str, str | None] = {}
for row in sorted(
(r for r in refactoring if _open(r)),
key=lambda r: (field(r, "rank_position") or 0, field(r, "opportunity_id")),
@@ -1167,10 +1285,14 @@ def build_fix_first(
if verdict.eligible:
verdict = refactor_verdict(gain, steps, files, path, concrete)
exclude(verdict, path, steps[0].get("target_symbol") if steps else None)
- refactoring_reasons[field(row, "opportunity_id")] = verdict.reason
if not verdict.eligible:
+ if judged is not None:
+ judged[field(row, "opportunity_id")] = Judgement(verdict.reason)
continue
- units.append(_refactor_unit(row, details, steps, gain, plan_rows, files))
+ unit = _refactor_unit(row, details, steps, gain, plan_rows, files)
+ units.append(unit)
+ if judged is not None:
+ judged[field(row, "opportunity_id")] = Judgement(None, unit.value, unit.tier)
# Only a plan that became an item speaks for the file's findings; an
# excluded one leaves them to compete on their own.
planned_files.add(path)
@@ -1240,7 +1362,6 @@ def build_fix_first(
),
by_improves={k: by_improves.get(k, 0) for k in FIX_IMPROVES},
basis=dict(basis) if basis is not None else _basis(metrics),
- refactoring_reasons=refactoring_reasons,
)
@@ -1253,4 +1374,5 @@ __all__ = [
"build_fix_first",
"hot_cut",
"hot_cut_offset",
+ "judge_findings",
]
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/model.py b/packages/core/src/repowise/core/analysis/health/fix_first/model.py
index 1011c836a..4e8100bed 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/model.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/model.py
@@ -10,7 +10,7 @@ import hashlib
from dataclasses import asdict, dataclass, field
from typing import Any, Literal, get_args
-from repowise.core.analysis.health.queue.eligibility import SCOPE_EXCLUSIONS
+from repowise.core.analysis.health.queue.counts import QueueCounts, counts_of
from repowise.core.analysis.health.queue.order import DUE_TIERS, Tier
from repowise.core.analysis.next_call import ActionCommand
@@ -234,10 +234,6 @@ class FixFirstQueue:
basis: dict[str, str | None] = field(
default_factory=lambda: {"analyzed_commit": None, "health_analyzed_at": None}
)
- #: Every open refactoring opportunity the builder read, by id: ``None``
- #: when it is eligible, else the exclusion that kept it out. The
- #: refactoring view's default scope reads it; never on the wire.
- refactoring_reasons: dict[str, str | None] = field(default_factory=dict)
@property
def lead(self) -> FixItem | None:
@@ -257,24 +253,12 @@ class FixFirstQueue:
"basis": dict(self.basis),
}
- def counts(self, shown: int) -> dict[str, Any]:
- """The five-level count vocabulary, on the full queue (``limit=None``).
-
- ``inventory`` every unit the builder considered; ``in_scope`` those not
- excluded by :data:`SCOPE_EXCLUSIONS`; ``eligible`` those that became an
- item; ``due`` eligible items in a :data:`DUE_TIERS` tier; ``shown`` what
- the caller emits; ``excluded`` every non-zero exclusion, by reason.
- """
- excluded = {reason: n for reason, n in self.totals.excluded.items() if n}
- out_of_scope = sum(n for reason, n in excluded.items() if reason in SCOPE_EXCLUSIONS)
- return {
- "inventory": self.totals.candidates,
- "in_scope": self.totals.candidates - out_of_scope,
- "eligible": self.totals.eligible,
- "due": sum(1 for item in self.items if item.tier in DUE_TIERS),
- "shown": shown,
- "excluded": excluded,
- }
+ def counts(self, shown: int) -> QueueCounts:
+ """The count vocabulary (``queue.counts``) on the full queue
+ (``limit=None``): ``inventory`` every unit the builder considered,
+ ``due`` the items in a :data:`DUE_TIERS` tier."""
+ due = sum(1 for item in self.items if item.tier in DUE_TIERS)
+ return counts_of(self.totals.eligible, due, self.totals.excluded, shown)
__all__ = [
diff --git a/packages/core/src/repowise/core/analysis/health/fix_first/text.py b/packages/core/src/repowise/core/analysis/health/fix_first/text.py
index 922c05b11..6cb155e50 100644
--- a/packages/core/src/repowise/core/analysis/health/fix_first/text.py
+++ b/packages/core/src/repowise/core/analysis/health/fix_first/text.py
@@ -135,6 +135,11 @@ def plural(n: int, noun: str) -> str:
return f"{n:,} {noun}" if n == 1 else f"{n:,} {noun}s"
+def held_back(n: int) -> str:
+ """Steps of a refactoring kind not yet audited, kept out of the item."""
+ return f"{n} (move or extract-class, not yet audited; in the full plan)"
+
+
def short_symbol(symbol: str | None) -> str | None:
"""``path/to/x.py::Cls.method`` reads as ``Cls.method``."""
return symbol.rsplit("::", 1)[-1] if symbol else None
diff --git a/packages/core/src/repowise/core/analysis/health/perf/serving.py b/packages/core/src/repowise/core/analysis/health/perf/serving.py
index 4e827bad3..f24e81255 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/serving.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/serving.py
@@ -122,6 +122,8 @@ FILTERS: tuple[FilterRule, ...] = (
FilterRule("proofs", "cost_proof", "in", "set"),
FilterRule("roles", "execution_role", "in", "set"),
FilterRule("file_paths", "file_path", "in", "set"),
+ # The default queue is the stored judgement (``queue.counts.perf_judgement``).
+ FilterRule("queue_eligible", "queue_eligible", "is", "set"),
)
#: Facet name, the field it counts, and the filter parameter it is
@@ -232,17 +234,54 @@ class PerformanceQuery:
return alias if alias is not None else frozenset({self.context})
@property
- def actionabilities(self) -> frozenset[str]:
- """The set the queue is filtered to: one explicit state, or the default queue states (plan_ready, advisory)."""
+ def default_queue(self) -> bool:
+ """Whether the caller named no context, state, proof or role: the
+ default queue, read from each cause's stored judgement."""
+ return (
+ self.context == DEFAULT_CONTEXT
+ and self.actionability is None
+ and self.proof is None
+ and self.role is None
+ )
+
+ @property
+ def queue_eligible(self) -> bool | None:
+ return True if self.default_queue else None
+
+ def store_filters(self, *, judged: bool = True) -> dict[str, Any]:
+ """The store's filter arguments. On a store whose causes are not all
+ judged (``judged=False``), the default queue reads its rule live:
+ the default states and proof instead of the stored verdict."""
+ live = self.default_queue and not judged
+ return {
+ "contexts": self.contexts,
+ "boundary": self.boundary,
+ "confidence": self.confidence,
+ "actionabilities": DEFAULT_ACTIONABILITIES if live else self.actionabilities,
+ "proofs": DEFAULT_QUEUE_PROOFS if live else self.proofs,
+ "roles": QUEUE_ROLES if live else self.roles,
+ "queue_eligible": None if live else self.queue_eligible,
+ "file_paths": self.file_paths,
+ }
+
+ @property
+ def actionabilities(self) -> frozenset[str] | None:
+ """One explicit state; else, under another context, the default
+ queue states (plan_ready, advisory). ``None`` for the default queue."""
+ if self.default_queue:
+ return None
if self.actionability is None:
return DEFAULT_ACTIONABILITIES
return frozenset({self.actionability})
@property
- def proofs(self) -> frozenset[str]:
- """``unproven`` when asked for; else the measured causes, so every
- surface lists what the default queue counts. The ``proof`` facet says
- how many unproven ones sit one filter away."""
+ def proofs(self) -> frozenset[str] | None:
+ """``unproven`` when asked for; else, under another context or state,
+ the measured causes, so every surface lists what the default queue
+ counts. ``None`` for the default queue. The ``proof`` facet says how
+ many unproven ones sit one filter away."""
+ if self.default_queue:
+ return None
if self.proof is None:
return DEFAULT_QUEUE_PROOFS
return frozenset({self.proof})
@@ -251,7 +290,10 @@ class PerformanceQuery:
def roles(self) -> frozenset[str] | None:
"""One role when asked for, every role under ``all``; else the roles
the default queue holds, so startup, CLI, tooling and test loops sit
- one filter away and the ``role`` facet counts them."""
+ one filter away and the ``role`` facet counts them. ``None`` for the
+ default queue, whose stored verdict already holds the role rule."""
+ if self.default_queue:
+ return None
if self.role is None:
return QUEUE_ROLES
return None if self.role == "all" else frozenset({self.role})
diff --git a/packages/core/src/repowise/core/analysis/health/queue/counts.py b/packages/core/src/repowise/core/analysis/health/queue/counts.py
new file mode 100644
index 000000000..8a658d1dc
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/queue/counts.py
@@ -0,0 +1,131 @@
+"""The one count vocabulary every queue surface reports, and a unit's stored judgement.
+
+Each unit (findings, performance causes, refactoring plans, Fix first items)
+counts at five levels: ``inventory`` every open unit; ``in_scope`` those whose
+code is in scope (not test, tooling, vendored, generated or docs); ``eligible``
+those a default queue holds; ``due`` eligible units in a tier worth scheduling
+now; ``shown`` what the response carries. ``excluded`` names every reason a
+unit is out, so the levels add up.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Iterable, Mapping
+from dataclasses import dataclass, field
+from typing import Any
+
+from .eligibility import SCOPE_EXCLUSIONS, Verdict, perf_low_priority, perf_queue_verdict
+from .order import DUE_TIERS
+from .value import perf_confidence, perf_ready, perf_value, tier
+
+#: Counted for a row stored before units were judged; the next index judges it.
+NOT_JUDGED = "not_judged"
+#: A finding in a file whose refactoring plan is the item: the plan speaks for it.
+COVERED_BY_PLAN = "covered_by_plan"
+#: An eligible finding that another finding in its file leads: one item per file.
+NOT_FILE_LEAD = "not_file_lead"
+#: A row the queue builder did not read this run (for example an opportunity
+#: of an older model): judged out so it never reads as unjudged.
+NOT_IN_QUEUE = "not_in_queue"
+#: Reasons a stored judgement can carry beyond ``eligibility.REASONS``.
+STORED_REASONS: tuple[str, ...] = (NOT_JUDGED, COVERED_BY_PLAN, NOT_FILE_LEAD, NOT_IN_QUEUE)
+
+
+@dataclass(frozen=True, slots=True)
+class Judgement:
+ """A unit's verdict as stored on its row: value and tier only when eligible."""
+
+ reason: str | None = None
+ value: int | None = None
+ tier: str | None = None
+
+ @classmethod
+ def of(cls, verdict: Verdict, value: int | None = None, tier: str | None = None) -> Judgement:
+ if not verdict.eligible:
+ return cls(verdict.reason)
+ return cls(None, value, tier)
+
+ def columns(self) -> dict[str, Any]:
+ """The ``queue_*`` columns this judgement writes."""
+ return {
+ "queue_eligible": self.reason is None,
+ "queue_reason": self.reason,
+ "queue_value": self.value,
+ "queue_tier": self.tier,
+ }
+
+
+def perf_judgement(row: Any, facets: Mapping[str, Any], plan: Mapping[str, Any]) -> Judgement:
+ """A cause's place in the performance default queue, valued and tiered as
+ Fix first values and tiers it when the cause leads its item."""
+ verdict = perf_queue_verdict(row)
+ if not verdict.eligible:
+ return Judgement(verdict.reason)
+ value = perf_value(row, facets)
+ level, _why = tier(value, perf_confidence(facets), perf_ready(row, plan), perf_low_priority(row))
+ return Judgement(None, value, level)
+
+
+@dataclass(frozen=True, slots=True)
+class QueueCounts:
+ """One unit's counts at the five levels, and the reasons for what is out."""
+
+ inventory: int = 0
+ in_scope: int = 0
+ eligible: int = 0
+ due: int = 0
+ shown: int = 0
+ excluded: dict[str, int] = field(default_factory=dict)
+
+ def as_dict(self) -> dict[str, Any]:
+ return {
+ "inventory": self.inventory,
+ "in_scope": self.in_scope,
+ "eligible": self.eligible,
+ "due": self.due,
+ "shown": self.shown,
+ "excluded": dict(self.excluded),
+ }
+
+
+def queue_counts(groups: Iterable[tuple[str | None, str | None, int]], shown: int) -> QueueCounts:
+ """Fold ``(reason, tier, n)`` groups (``reason`` ``None`` for an eligible
+ unit) into the counts; ``shown`` is the caller's."""
+ eligible = due = 0
+ excluded: dict[str, int] = {}
+ for reason, level, n in groups:
+ if reason is None:
+ eligible += n
+ due += n if level in DUE_TIERS else 0
+ else:
+ excluded[reason] = excluded.get(reason, 0) + n
+ return counts_of(eligible, due, excluded, shown)
+
+
+def counts_of(eligible: int, due: int, excluded: Mapping[str, int], shown: int) -> QueueCounts:
+ """The counts from what a queue holds and what it left out, by reason."""
+ kept = {reason: n for reason, n in excluded.items() if n}
+ inventory = eligible + sum(kept.values())
+ out_of_scope = sum(n for reason, n in kept.items() if reason in SCOPE_EXCLUSIONS)
+ return QueueCounts(
+ inventory=inventory,
+ in_scope=inventory - out_of_scope,
+ eligible=eligible,
+ due=due,
+ shown=shown,
+ excluded=dict(sorted(kept.items(), key=lambda i: (-i[1], i[0]))),
+ )
+
+
+__all__ = [
+ "COVERED_BY_PLAN",
+ "NOT_FILE_LEAD",
+ "NOT_IN_QUEUE",
+ "NOT_JUDGED",
+ "STORED_REASONS",
+ "Judgement",
+ "QueueCounts",
+ "counts_of",
+ "perf_judgement",
+ "queue_counts",
+]
diff --git a/packages/core/src/repowise/core/analysis/health/queue/value.py b/packages/core/src/repowise/core/analysis/health/queue/value.py
index 3d2d39d63..eadb35820 100644
--- a/packages/core/src/repowise/core/analysis/health/queue/value.py
+++ b/packages/core/src/repowise/core/analysis/health/queue/value.py
@@ -65,6 +65,21 @@ def perf_value(row: Any, facets: Mapping[str, Any]) -> int:
return value
+def perf_confidence(facets: Mapping[str, Any]) -> str:
+ """How sure the strategy is; ``low`` when it was not recorded."""
+ level = facets.get("actionability_confidence") or "low"
+ return level if level in LEVEL_RANK else "low"
+
+
+def perf_ready(row: Any, plan: Mapping[str, Any]) -> bool:
+ """Whether a cause's fix is safe to start: a ready or proven strategy,
+ or a stored plan whose every step is mechanical."""
+ if field(row, "actionability_state") == "plan_ready" or field(row, "fix_safety") == "proven":
+ return True
+ steps = plan.get("steps") or []
+ return bool(steps) and all(s.get("applicability") == "mechanical" for s in steps)
+
+
def tier(value: int, confidence: str, ready: bool, low: str | None) -> tuple[str, str]:
"""The tier and its reason, from a value that leaves the hot-file bonus
out: history orders items within a tier but never lifts one. A problem
@@ -84,6 +99,8 @@ __all__ = [
"SIZE_SEVERE",
"VALUE_MAX",
"gain_value",
+ "perf_confidence",
+ "perf_ready",
"perf_value",
"shape_value",
"size_value",
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/serving.py b/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
index e188b7194..e0e0ae911 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/serving.py
@@ -43,8 +43,8 @@ DEFAULT_VIEW = "diversified"
# Which open opportunities a queue lists:
#
# - ``fix_first`` the default for a repository-wide open queue: only the
-# opportunities Fix first would take (its eligibility and exclusion rules,
-# read from the same builder), with the rest counted by reason.
+# opportunities Fix first would take (the judgement the index stored on each
+# row), with the rest counted by reason.
# - ``all`` the full inventory. The default when the caller names files (a
# file surface asks for its own work) or lists a triaged status, which Fix
# first never reads.
@@ -90,8 +90,9 @@ CANONICAL_ORDERS = tuple(SORTS)
#: In the order the store has always emitted its predicates.
FILTERS: tuple[FilterRule, ...] = (
FilterRule("status", "status", "eq", "always"),
- # A scope resolved in Python (Fix first's eligible set).
FilterRule("opportunity_ids", "opportunity_id", "in", "set"),
+ # The ``fix_first`` scope: the judgement stored at index time.
+ FilterRule("queue_eligible", "queue_eligible", "is", "set"),
# A list is how the board's "Structural" tab asks for four types at once.
FilterRule("lead_types", "lead_refactoring_type", "in", "truthy"),
FilterRule("confidence", "confidence", "eq", "set"),
@@ -105,14 +106,17 @@ FILTERS: tuple[FilterRule, ...] = (
FilterRule("addresses_primary", "addresses_primary_problem", "is", "set"),
)
-#: Facet name and the field it counts. No facet is cross-filtered: the counts
-#: are over the status and scope alone.
+#: Facet name, the field it counts, and the filter that narrows it. Each facet
+#: is counted under every other filter, so choosing a type keeps the other
+#: types' counts.
FACETS: tuple[Facet, ...] = (
- ("lead_type", "lead_refactoring_type", None),
- ("effort", "effort_bucket", None),
- ("confidence", "confidence", None),
+ ("lead_type", "lead_refactoring_type", "lead_types"),
+ ("effort", "effort_bucket", "effort"),
+ ("confidence", "confidence", "confidence"),
)
FACET_FIELDS = tuple(column for _, column, _ in FACETS)
+#: The filters a facet cross-filters by, applied after grouping, not in SQL.
+FACET_PARAMS = frozenset(param for _, _, param in FACETS if param)
def sort_keys(order: str | None) -> SortKeys:
@@ -130,20 +134,31 @@ def row_sort_key(row: Any, order: str | None = None) -> tuple[Any, ...]:
return queue_rules.sort_key(sort_keys(order), row)
-def fold_facets(groups: Iterable[Sequence[Any]]) -> dict[str, dict[str, int]]:
- """Fold ``(*facet values, count)`` groups into per-facet counts; a NULL is skipped."""
- return queue_rules.fold_facets(groups, FACETS)
+def split_facet_params(params: Mapping[str, Any]) -> tuple[dict[str, Any], dict[str, Any]]:
+ """``(row filters, facet selection)``: the filters every count applies,
+ and the ones each facet applies to the others only."""
+ rows = {k: v for k, v in params.items() if k not in FACET_PARAMS}
+ return rows, {k: v for k, v in params.items() if k in FACET_PARAMS}
-def facet_counts(
- rows: Iterable[Any], *, status: str = "open", opportunity_ids: Sequence[str] | None = None
+def fold_facets(
+ groups: Iterable[Sequence[Any]], selection: Mapping[str, Any] | None = None
) -> dict[str, dict[str, int]]:
- """Counts for every facet over the rows a status (and scope) selects."""
- params = {"status": status, "opportunity_ids": opportunity_ids}
+ """Fold ``(*facet values, count)`` groups into per-facet counts, each
+ cross-filtered by the *selection* of the others; a NULL is skipped."""
+ return queue_rules.fold_facets(groups, FACETS, rules=FILTERS, selection=selection)
+
+
+def facet_counts(rows: Iterable[Any], **params: Any) -> dict[str, dict[str, int]]:
+ """Counts for every facet over *rows*, under the queue filters in *params*."""
+ row_params, selection = split_facet_params({"status": "open", **params})
return fold_facets(
- (*(field(row, column) for column in FACET_FIELDS), 1)
- for row in rows
- if keep(row, params)
+ (
+ (*(field(row, column) for column in FACET_FIELDS), 1)
+ for row in rows
+ if keep(row, row_params)
+ ),
+ selection,
)
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 1344f50fe..a465cfb75 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
@@ -25,11 +25,11 @@ reads one row instead of building.
from __future__ import annotations
from collections import OrderedDict, namedtuple
-from collections.abc import Callable
+from collections.abc import Callable, Iterable
from dataclasses import asdict, replace
from typing import Any
-from sqlalchemy import and_, case, func, or_, select
+from sqlalchemy import and_, case, func, or_, select, true, update
from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.finding_registry import excluded_types
@@ -43,7 +43,9 @@ from repowise.core.analysis.health.fix_first.build import (
DEAD_CONFIDENCE,
hot_cut,
hot_cut_offset,
+ judge_findings,
)
+from repowise.core.analysis.health.queue.counts import NOT_IN_QUEUE, Judgement
from repowise.core.analysis.health.queue.eligibility import DEFAULT_QUEUE_STATES, MIN_WORTH
from repowise.core.analysis.health.refactoring.identity import REFACTORING_MODEL_VERSION
from repowise.core.analysis.health.refactoring.models import (
@@ -87,9 +89,11 @@ def _plain(result: Any) -> list[Any]:
return [shape(*r) for r in rows]
-async def _metrics(session: AsyncSession, repo_id: str, paths: set[str]) -> list[Any]:
- if not paths:
+async def _metrics(session: AsyncSession, repo_id: str, paths: set[str] | None) -> list[Any]:
+ """The named files' rows; ``None`` reads every file."""
+ if paths is not None and not paths:
return []
+ scoped = HealthFileMetric.file_path.in_(paths) if paths is not None else true()
return _plain(
await session.execute(
select(
@@ -112,9 +116,7 @@ async def _metrics(session: AsyncSession, repo_id: str, paths: set[str]) -> list
(GraphMetric.repository_id == HealthFileMetric.repository_id)
& (GraphMetric.node_id == HealthFileMetric.file_path),
)
- .where(
- HealthFileMetric.repository_id == repo_id, HealthFileMetric.file_path.in_(paths)
- )
+ .where(HealthFileMetric.repository_id == repo_id, scoped)
)
)
@@ -169,7 +171,7 @@ async def _basis(session: AsyncSession, repo_id: str) -> dict[str, str | None]:
return {"analyzed_commit": row.analyzed_commit, "health_analyzed_at": row.updated_at.isoformat()}
-def _eligible_findings(repo_id: str) -> Any:
+def queue_findings(repo_id: str) -> Any:
"""Open, scoring, code-health findings a surface may show."""
f = HealthFinding
return and_(
@@ -200,7 +202,7 @@ async def _finding_files(session: AsyncSession, repo_id: str) -> tuple[set[str],
func.max(f.biomarker_type).label("marker"),
func.max(f.health_impact).label("impact"),
)
- .where(_eligible_findings(repo_id))
+ .where(queue_findings(repo_id))
.group_by(f.file_path)
)
).all()
@@ -241,7 +243,7 @@ async def _findings(
# History numbers become plain context sentences.
f.details_json,
).where(
- _eligible_findings(repo_id),
+ queue_findings(repo_id),
or_(
f.file_path.in_(full),
and_(
@@ -272,11 +274,7 @@ async def _refactoring(session: AsyncSession, repo_id: str) -> list[Any]:
o.status,
case((o.recoverable_health >= MIN_WORTH, o.details_json)).label("details_json"),
)
- .where(
- o.repository_id == repo_id,
- o.status == "open",
- o.refactoring_model_version == REFACTORING_MODEL_VERSION,
- )
+ .where(open_opportunities(repo_id))
.order_by(o.rank_position)
)
)
@@ -372,20 +370,20 @@ def _steps(refactoring: list[Any]) -> list[dict[str, Any]]:
async def _plans(
- session: AsyncSession, repo_id: str, steps: list[dict[str, Any]], files: set[str]
+ session: AsyncSession, repo_id: str, steps: list[dict[str, Any]], files: set[str] | None
) -> list[Any]:
"""The plans those steps name (span, signature, evidence), the open
Extract Method plans in ``files``, where a finding with no plan of its own
takes its first concrete step from, and every open Extract Helper plan,
whose occurrences say where verified duplicates sit (a plan is stored at
- one anchor file and names every site, so it is not filtered by file)."""
+ one anchor file and names every site, so it is not filtered by file).
+ ``files=None`` reads the Extract Method plans of every file."""
ids = {s.get("plan_id") for s in steps if s.get("plan_id")}
s = RefactoringSuggestion
named = s.public_id.in_(ids) if ids else None
+ extract = and_(s.refactoring_type == "extract_method", s.status == "open")
extractions = (
- and_(s.refactoring_type == "extract_method", s.status == "open", s.file_path.in_(files))
- if files
- else None
+ extract if files is None else and_(extract, s.file_path.in_(files)) if files else None
)
helpers = and_(s.refactoring_type == "extract_helper", s.status == "open")
wanted = [c for c in (named, extractions, helpers) if c is not None]
@@ -528,22 +526,157 @@ async def _stamp(session: AsyncSession, repo_id: str) -> tuple[Any, ...]:
#: The stored production queue's ``read_snapshots.kind``.
SNAPSHOT_KIND = "fix_first"
_KEY = snapshot_key(SNAPSHOT_KIND, FIX_FIRST_MODEL_VERSION)
+#: Its count vocabulary, stored beside it.
+COUNTS_KIND = "fix_first_counts"
+_COUNTS_KEY = snapshot_key(COUNTS_KIND, FIX_FIRST_MODEL_VERSION)
async def write_fix_first_snapshot(session: AsyncSession, repository_id: str) -> bool:
- """Store the whole production queue, tests resolved, for the next reader.
+ """Store the whole production queue, tests resolved, for the next reader,
+ and each refactoring opportunity's and finding's judgement on its row.
The whole queue rather than its head: an id lookup, a page past the
- first, the counts and the refactoring list's exclusion reasons are all
- read from it. The writer of the stores calls this once they are final.
- Returns whether it wrote (a row still current writes nothing).
+ first and the counts are all read from it. The writer of the stores calls
+ this once they are final. Returns whether it wrote (a row still current
+ writes nothing, unless a unit on it was never judged).
"""
+ built: dict[str, Any] = {}
+
async def build() -> dict[str, Any]:
- full = await _build(session, repository_id, limit=None, scope="production", item_id=None)
+ judged: dict[str, Judgement] = {}
+ full = await _build(
+ session, repository_id, limit=None, scope="production", item_id=None, judged=judged
+ )
+ await _store_judgements(session, repository_id, judged)
+ built["counts"] = full.counts(0).as_dict()
return asdict(full)
- return await refresh_snapshot(session, repository_id, SNAPSHOT_KIND, _KEY, build)
+ async def counts() -> dict[str, Any]:
+ if "counts" not in built:
+ full = await load_fix_first(session, repository_id, limit=None)
+ built["counts"] = full.counts(0).as_dict()
+ return built["counts"]
+
+ wrote = await refresh_snapshot(
+ session,
+ repository_id,
+ SNAPSHOT_KIND,
+ _KEY,
+ build,
+ force=await _unjudged(session, repository_id),
+ )
+ # The items' counts apart, so counting never decodes or builds the queue.
+ await refresh_snapshot(session, repository_id, COUNTS_KIND, _COUNTS_KEY, counts, force=wrote)
+ return wrote
+
+
+async def stored_item_counts(session: AsyncSession, repository_id: str) -> dict[str, Any] | None:
+ """The stored production queue's counts (``shown`` 0); ``None`` when not stored."""
+ payload = await read_snapshot(session, repository_id, COUNTS_KIND, _COUNTS_KEY)
+ return payload if isinstance(payload, dict) else None
+
+
+def open_opportunities(repo_id: str) -> Any:
+ o = RefactoringOpportunity
+ return and_(
+ o.repository_id == repo_id,
+ o.status == "open",
+ o.refactoring_model_version == REFACTORING_MODEL_VERSION,
+ )
+
+
+async def unjudged(session: AsyncSession, model: Any, population: Any) -> bool:
+ """Whether a row of ``population`` has no stored judgement: a store
+ written before the columns, or one whose judging write did not run."""
+ found = await session.execute(
+ select(model.id).where(population, model.queue_eligible.is_(None)).limit(1)
+ )
+ return found.first() is not None
+
+
+async def _unjudged(session: AsyncSession, repo_id: str) -> bool:
+ return await unjudged(
+ session, RefactoringOpportunity, open_opportunities(repo_id)
+ ) or await unjudged(session, HealthFinding, queue_findings(repo_id))
+
+
+async def _store_judgements(
+ session: AsyncSession, repo_id: str, refactoring: dict[str, Judgement]
+) -> None:
+ """Write each open refactoring opportunity's and finding's judgement,
+ only where it changed: an update run rewrites what moved.
+
+ Ceiling: findings are judged over every file, so metrics, findings and
+ Extract Method plans are read again here rather than reused from the
+ queue build, which reads only its candidate files. Upgrade path: one read
+ of the whole population shared by both, once the queue build drops its
+ ``FINDING_FILES`` ceiling.
+ """
+ o = RefactoringOpportunity
+ stored = (
+ await session.execute(
+ select(o.id, o.opportunity_id, o.file_path, *_queue_columns(o)).where(
+ open_opportunities(repo_id)
+ )
+ )
+ ).all()
+ await _write_changed(session, o, ((r, refactoring.get(r.opportunity_id)) for r in stored))
+ eligible = {i for i, j in refactoring.items() if j.reason is None}
+ planned = {r.file_path for r in stored if r.opportunity_id in eligible}
+ findings = await _judged_findings(session, repo_id)
+ paths = {f.file_path for f in findings}
+ judged = judge_findings(
+ metrics=await _metrics(session, repo_id, None) if paths else [],
+ findings=findings,
+ plans=await _plans(session, repo_id, [], None) if paths else [],
+ dead_code=await _dead_code(session, repo_id),
+ hot_cuts=await _hot_cuts(session, repo_id),
+ planned_files=planned,
+ )
+ await _write_changed(session, HealthFinding, ((f, judged.get(f.id)) for f in findings))
+
+
+_NOT_IN_QUEUE = Judgement(NOT_IN_QUEUE)
+
+
+def _queue_columns(model: Any) -> tuple[Any, ...]:
+ return (model.queue_eligible, model.queue_reason, model.queue_value, model.queue_tier)
+
+
+async def _write_changed(
+ session: AsyncSession, model: Any, rows: Iterable[tuple[Any, Judgement | None]]
+) -> None:
+ changed = []
+ for row, judgement in rows:
+ # Not read by the builder this run: judged out, so it settles.
+ values = (judgement or _NOT_IN_QUEUE).columns()
+ if any(getattr(row, name) != value for name, value in values.items()):
+ changed.append({"id": row.id, **values})
+ if changed:
+ await session.execute(update(model), changed)
+
+
+async def _judged_findings(session: AsyncSession, repo_id: str) -> list[Any]:
+ """Every finding the queue judges, with what judging reads and its stored judgement."""
+ f = HealthFinding
+ return _plain(
+ await session.execute(
+ select(
+ f.id,
+ f.file_path,
+ f.biomarker_type,
+ f.function_name,
+ f.line_start,
+ f.line_end,
+ f.health_impact,
+ f.dimension,
+ f.status,
+ f.details_json,
+ *_queue_columns(f),
+ ).where(queue_findings(repo_id))
+ )
+ )
async def _stored(session: AsyncSession, repo_id: str, scope: str) -> FixFirstQueue | None:
@@ -640,6 +773,7 @@ async def _build(
scope: str,
item_id: str | None,
verify: bool = True,
+ judged: dict[str, Judgement] | None = None,
) -> FixFirstQueue:
refactoring = _decoded(await _refactoring(session, repository_id))
performance = _decoded(await _performance(session, repository_id))
@@ -668,15 +802,18 @@ async def _build(
hot_cuts=await _hot_cuts(session, repository_id),
symbol_lines=await _symbol_lines(session, repository_id, performance),
validate=await _finding_validator(session, repository_id, findings) if verify else None,
+ judged=judged,
)
__all__ = [
"CACHE_SIZE",
+ "COUNTS_KIND",
"FINDING_FILES",
"SNAPSHOT_KIND",
"clear_fix_first_cache",
"load_fix_first",
"queue_view",
+ "stored_item_counts",
"write_fix_first_snapshot",
]
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 ff105be64..92da7cf8c 100644
--- a/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/performance.py
@@ -104,10 +104,11 @@ def _row_kwargs(
plan: dict[str, Any] | None = None,
) -> dict[str, Any]:
from ....analysis.health.perf.serving import intervention_file
+ from ....analysis.health.queue.counts import perf_judgement
from ....analysis.health.queue.eligibility import queue_proof
fix = opportunity.fix
- return {
+ row = {
"opportunity_id": opportunity.opportunity_id,
"performance_model_version": opportunity.performance_model_version,
"status": "open",
@@ -132,6 +133,13 @@ def _row_kwargs(
"details_json": json.dumps(opportunity_details(opportunity, plan), separators=(",", ":")),
"analyzed_commit": analyzed_commit,
}
+ # Judged on the row as stored, which Fix first reads the same way.
+ judged = {
+ **row,
+ "facets": opportunity.facets,
+ "actionability_reason": opportunity.actionability_reason,
+ }
+ return {**row, **perf_judgement(judged, opportunity.facets, plan or {}).columns()}
def _summary_payload(
@@ -508,6 +516,7 @@ async def list_performance_opportunities(
actionabilities: frozenset[str] | None = None,
proofs: frozenset[str] | None = None,
roles: frozenset[str] | None = None,
+ queue_eligible: bool | None = None,
file_paths: tuple[str, ...] | None = None,
sort: str = "rank",
limit: int = 20,
@@ -528,6 +537,7 @@ async def list_performance_opportunities(
actionabilities=actionabilities,
proofs=proofs,
roles=roles,
+ queue_eligible=queue_eligible,
file_paths=file_paths,
)
total = int(
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/queue_counts.py b/packages/core/src/repowise/core/persistence/crud/analysis/queue_counts.py
new file mode 100644
index 000000000..9154ffb86
--- /dev/null
+++ b/packages/core/src/repowise/core/persistence/crud/analysis/queue_counts.py
@@ -0,0 +1,107 @@
+"""The count vocabulary of each queue unit, read from the stored judgements.
+
+Findings, performance causes and refactoring plans each carry their judgement
+(``models.QueueVerdict``), so their counts are one grouped read. Fix first
+items count from the stored queue. Every surface reads these, so a number
+means the same thing in the CLI, MCP, REST and the web UI.
+"""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+from typing import Any, Literal, get_args
+
+from sqlalchemy import func, select, true
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from repowise.core.analysis.health.perf.opportunities import PERFORMANCE_MODEL_VERSION
+from repowise.core.analysis.health.queue.counts import NOT_JUDGED, QueueCounts, queue_counts
+
+from ...models import HealthFinding, PerformanceOpportunity, RefactoringOpportunity
+from .fix_first import (
+ load_fix_first,
+ open_opportunities,
+ queue_findings,
+ stored_item_counts,
+ unjudged,
+)
+
+Unit = Literal["findings", "causes", "plans", "items"]
+#: One noun per unit, in the order surfaces list them.
+UNITS: tuple[Unit, ...] = get_args(Unit)
+#: The units whose rows carry a stored judgement.
+JudgedUnit = Literal["findings", "causes", "plans"]
+
+
+def _population(unit: str, repo_id: str) -> tuple[Any, Any]:
+ if unit == "findings":
+ return HealthFinding, queue_findings(repo_id)
+ if unit == "causes":
+ p = PerformanceOpportunity
+ return p, (
+ (p.repository_id == repo_id)
+ & (p.status == "open")
+ & (p.performance_model_version == PERFORMANCE_MODEL_VERSION)
+ )
+ return RefactoringOpportunity, open_opportunities(repo_id)
+
+
+async def any_unjudged(session: AsyncSession, repo_id: str, unit: JudgedUnit) -> bool:
+ """Whether an open unit has no stored judgement yet. A default queue
+ then reads its rule live rather than filtering on the column."""
+ return await unjudged(session, *_population(unit, repo_id))
+
+
+async def unit_counts(
+ session: AsyncSession,
+ repo_id: str,
+ unit: Unit,
+ *,
+ shown: int = 0,
+ file_paths: Sequence[str] | None = None,
+) -> QueueCounts:
+ """``unit``'s counts over its open inventory, in ``file_paths`` when named.
+
+ List filters (type, effort, context) never narrow these: the counts say
+ where a list sits in the whole queue, and ``shown`` is what it carries.
+ Fix first items are counted over the whole repository only: their
+ exclusions carry no file, so ``file_paths`` is refused for them.
+ """
+ if unit == "items":
+ if file_paths is not None:
+ raise ValueError("Fix first items are counted repository-wide only")
+ stored = await stored_item_counts(session, repo_id)
+ if stored is not None:
+ return QueueCounts(**{**stored, "shown": shown})
+ # Ceiling: a store with no counts row (written before it existed, or
+ # its snapshot write failed) builds the queue once; the next index
+ # stores the counts.
+ return (await load_fix_first(session, repo_id, limit=None)).counts(shown)
+ model, population = _population(unit, repo_id)
+ scoped = model.file_path.in_(list(file_paths)) if file_paths is not None else true()
+ rows = await session.execute(
+ select(model.queue_eligible, model.queue_reason, model.queue_tier, func.count())
+ .where(population, scoped)
+ .group_by(model.queue_eligible, model.queue_reason, model.queue_tier)
+ )
+ return queue_counts(
+ (
+ (NOT_JUDGED if eligible is None else None if eligible else reason, tier, n)
+ for eligible, reason, tier, n in rows.all()
+ ),
+ shown,
+ )
+
+
+async def all_unit_counts(
+ session: AsyncSession, repo_id: str, *, shown: dict[str, int] | None = None
+) -> dict[str, dict[str, Any]]:
+ """Every unit's counts, by noun, for a surface that reports them together."""
+ out: dict[str, dict[str, Any]] = {}
+ for unit in UNITS:
+ counts = await unit_counts(session, repo_id, unit, shown=(shown or {}).get(unit, 0))
+ out[unit] = counts.as_dict()
+ return out
+
+
+__all__ = ["UNITS", "JudgedUnit", "Unit", "all_unit_counts", "any_unjudged", "unit_counts"]
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 832474427..601beb4d2 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
@@ -213,7 +213,8 @@ def _summary_payload(
plan_ids: set[str],
design_plan_ids: set[str],
) -> dict[str, Any]:
- """The rollup. ``plans_total`` is the plan inventory behind it, split into
+ """The rollup of open opportunities; ``by_status`` also counts the ones
+ picked up. ``plans_total`` is the plan inventory behind it, split into
steps, evidence and plans in no opportunity, so the plan and opportunity
counts reconcile. ``design_total`` names the grouping plans held out of the
steps for an unnamed group; they sit in the evidence or unattached share."""
@@ -226,11 +227,13 @@ def _summary_payload(
mechanical = judgment = 0
addresses_primary = not_primary = unknown_primary = 0
for item in opportunities:
+ state = statuses.get(item.opportunity_id, "open")
+ by_status[state] = by_status.get(state, 0) + 1
+ open_items = _open_items(opportunities, statuses)
+ for item in open_items:
by_type[item.lead_refactoring_type] = by_type.get(item.lead_refactoring_type, 0) + 1
by_effort[item.effort_bucket] = by_effort.get(item.effort_bucket, 0) + 1
by_confidence[item.confidence] = by_confidence.get(item.confidence, 0) + 1
- state = statuses.get(item.opportunity_id, "open")
- by_status[state] = by_status.get(state, 0) + 1
mechanical += item.mechanical_steps
judgment += item.judgment_steps
if item.addresses_primary_problem is True:
@@ -240,13 +243,13 @@ def _summary_payload(
else:
unknown_primary += 1
return {
- "opportunities_total": len(opportunities),
- "files_total": len({item.file_path for item in opportunities}),
+ "opportunities_total": len(open_items),
+ "files_total": len({item.file_path for item in open_items}),
"steps_total": mechanical + judgment,
"mechanical_steps_total": mechanical,
"judgment_steps_total": judgment,
"plans_total": len(plan_ids),
- "evidence_total": sum(len(item.evidence) for item in opportunities),
+ "evidence_total": sum(len(item.evidence) for item in open_items),
"unattached_plans_total": len(plan_ids - claimed_plan_ids(opportunities)),
"design_total": len(design_plan_ids & plan_ids),
"by_lead_type": by_type,
@@ -262,6 +265,12 @@ def _summary_payload(
}
+def _open_items(
+ opportunities: list[OpportunityModel], statuses: dict[str, str]
+) -> list[OpportunityModel]:
+ return [o for o in opportunities if statuses.get(o.opportunity_id, "open") == "open"]
+
+
async def _rank_live_plans(session: AsyncSession, repository_id: str) -> list[Any]:
"""Rank, validate and annotate every live plan the lists can show, and store it.
@@ -576,7 +585,7 @@ async def _write_summary(
row = RefactoringSummary(repository_id=repository_id)
session.add(row)
row.refactoring_model_version = REFACTORING_MODEL_VERSION
- row.opportunities_total = len(opportunities)
+ row.opportunities_total = len(_open_items(opportunities, statuses))
row.summary_json = payload
row.analyzed_commit = analyzed_commit
row.updated_at = _now_utc()
@@ -622,6 +631,7 @@ async def list_refactoring_opportunities(
mechanical_only: bool = False,
addresses_primary: bool | None = None,
opportunity_ids: list[str] | None = None,
+ queue_eligible: bool | None = None,
order: str | None = None,
limit: int = 20,
offset: int = 0,
@@ -641,6 +651,7 @@ async def list_refactoring_opportunities(
mechanical_only=mechanical_only,
addresses_primary=addresses_primary,
opportunity_ids=opportunity_ids,
+ queue_eligible=queue_eligible,
)
total = int(
(
@@ -693,35 +704,24 @@ async def refactoring_opportunities_by_id(
return {row.opportunity_id: row for row in rows.scalars().all()}
-async def refactoring_opportunity_ids(
- session: AsyncSession,
- repository_id: str,
- *,
- status: str = "open",
- lead_types: list[str] | None = None,
- confidence: str | None = None,
- effort: str | None = None,
- file_paths: list[str] | None = None,
- path_contains: str | None = None,
- path_prefix: str | None = None,
- mechanical_only: bool = False,
- addresses_primary: bool | None = None,
-) -> list[str]:
- """The ids the same filters match, unpaged: one narrow column."""
- predicates = _opportunity_filters(
- repository_id,
- status=status,
- lead_types=lead_types,
- confidence=confidence,
- effort=effort,
- file_paths=file_paths,
- path_contains=path_contains,
- path_prefix=path_prefix,
- mechanical_only=mechanical_only,
- addresses_primary=addresses_primary,
+async def refactoring_reason_counts(
+ session: AsyncSession, repository_id: str, *, status: str = "open", **filters: Any
+) -> dict[str | None, int]:
+ """How many rows the filters match, by stored queue reason (``None`` for
+ an eligible row): one grouped read, exact at any size."""
+ from ....analysis.health.queue.counts import NOT_JUDGED
+
+ o = RefactoringOpportunity
+ rows = await session.execute(
+ select(o.queue_eligible, o.queue_reason, func.count())
+ .where(*_opportunity_filters(repository_id, status=status, **filters))
+ .group_by(o.queue_eligible, o.queue_reason)
)
- rows = await session.execute(select(RefactoringOpportunity.opportunity_id).where(*predicates))
- return [opportunity_id for (opportunity_id,) in rows.all()]
+ out: dict[str | None, int] = {}
+ for eligible, reason, n in rows.all():
+ key = NOT_JUDGED if eligible is None else None if eligible else reason
+ out[key] = out.get(key, 0) + int(n)
+ return out
async def get_refactoring_opportunity(
@@ -822,31 +822,28 @@ async def get_refactoring_summary(
async def refactoring_facet_counts(
- session: AsyncSession,
- repository_id: str,
- *,
- status: str = "open",
- opportunity_ids: list[str] | None = None,
+ session: AsyncSession, repository_id: str, *, status: str = "open", **filters: Any
) -> dict[str, dict[str, int]]:
- """Counts for every facet dimension, in one statement.
+ """Counts for every facet dimension, in one statement, under the list's
+ filters: each facet counts under every filter but its own.
Grouped by all three dimensions at once and folded here rather than one
statement each, so adding a facet never adds a round trip.
- ``opportunity_ids`` narrows the counts to a scope the list applies too.
"""
- from ....analysis.health.refactoring.serving import FACET_FIELDS, fold_facets
+ from ....analysis.health.refactoring.serving import (
+ FACET_FIELDS,
+ fold_facets,
+ split_facet_params,
+ )
+ row_filters, selection = split_facet_params({"status": status, **filters})
columns = tuple(getattr(RefactoringOpportunity, name) for name in FACET_FIELDS)
rows = await session.execute(
select(*columns, func.count())
- .where(
- *_opportunity_filters(
- repository_id, status=status, opportunity_ids=opportunity_ids
- )
- )
+ .where(*_opportunity_filters(repository_id, **row_filters))
.group_by(*columns)
)
- return fold_facets(rows.all())
+ return fold_facets(rows.all(), selection)
__all__ = [
@@ -856,7 +853,7 @@ __all__ = [
"list_refactoring_opportunities",
"refactoring_facet_counts",
"refactoring_opportunities_by_id",
- "refactoring_opportunity_ids",
+ "refactoring_reason_counts",
"refactoring_step_counts",
"update_refactoring_opportunity_status",
]
diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.py
index 4f89932fd..cbad84066 100644
--- a/packages/core/src/repowise/core/persistence/models.py
+++ b/packages/core/src/repowise/core/persistence/models.py
@@ -1748,7 +1748,23 @@ class DocDriftFinding(Base):
first_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
-class HealthFinding(Base):
+class QueueVerdict:
+ """A unit's place in the default queue, judged once at index time by
+ ``analysis.health.queue`` so lists filter and count it in SQL.
+
+ ``queue_eligible`` is NULL until the first judgement (a store written
+ before the columns), ``False`` with the one ``queue_reason`` that kept the
+ unit out, or ``True``; ``queue_value`` (0-4) and ``queue_tier`` are set on
+ eligible units only.
+ """
+
+ queue_eligible: Mapped[bool | None] = mapped_column(Boolean, nullable=True)
+ queue_reason: Mapped[str | None] = mapped_column(String(32), nullable=True)
+ queue_value: Mapped[int | None] = mapped_column(Integer, nullable=True)
+ queue_tier: Mapped[str | None] = mapped_column(String(8), nullable=True)
+
+
+class HealthFinding(QueueVerdict, Base):
"""One biomarker hit produced by the code-health analyzer."""
__tablename__ = "health_findings"
@@ -1805,6 +1821,8 @@ class HealthFinding(Base):
"dimension",
"opportunity_id",
),
+ # Counting and the unjudged check group by the stored verdict.
+ Index("ix_health_findings_repo_status_queue", "repository_id", "status", "queue_eligible"),
)
@@ -1913,7 +1931,7 @@ class RefactoringSuggestion(Base):
)
-class PerformanceOpportunity(Base):
+class PerformanceOpportunity(QueueVerdict, Base):
"""One causal performance opportunity, materialized for serving.
The queue used to be rebuilt from every open performance finding on every
@@ -2011,6 +2029,13 @@ class PerformanceOpportunity(Base):
"rank_position",
),
Index("ix_performance_opportunities_repo_status_path", "repository_id", "status", "file_path"),
+ Index(
+ "ix_performance_opportunities_repo_status_queue",
+ "repository_id",
+ "status",
+ "queue_eligible",
+ "rank_position",
+ ),
)
@@ -2042,7 +2067,7 @@ class PerformanceSummary(Base):
)
-class RefactoringOpportunity(Base):
+class RefactoringOpportunity(QueueVerdict, Base):
"""One file's composed refactoring work, materialized for serving.
Composition folds a file's plans into one ordered, precondition-aware
@@ -2135,6 +2160,15 @@ class RefactoringOpportunity(Base):
"status",
"file_path",
),
+ # The ``fix_first`` scope's page, in queue order; its reason counts
+ # read the same (repository, status) prefix.
+ Index(
+ "ix_refactoring_opportunities_repo_status_eligible",
+ "repository_id",
+ "status",
+ "queue_eligible",
+ "queue_position",
+ ),
)
diff --git a/packages/core/src/repowise/core/persistence/read_snapshots.py b/packages/core/src/repowise/core/persistence/read_snapshots.py
index 49797e725..68605a753 100644
--- a/packages/core/src/repowise/core/persistence/read_snapshots.py
+++ b/packages/core/src/repowise/core/persistence/read_snapshots.py
@@ -197,18 +197,21 @@ async def refresh_snapshot(
kind: str,
key: str,
build: Callable[[], Awaitable[Any]],
+ *,
+ force: bool = False,
) -> bool:
"""Store ``await build()`` (JSON-ready) as the ``kind`` row under ``key``,
unless the row already holds that key and this transaction has written
- no input (such a row would be dropped at commit). Returns whether it
- wrote.
+ no input (such a row would be dropped at commit). ``force`` builds
+ anyway, for a builder that also writes what the stores lack. Returns
+ whether it wrote.
Two writers racing on one row (two updates of one store) can fail on the
primary key; the caller treats any failure as "no snapshot", which a
reader answers by building live.
"""
row = await session.get(ReadSnapshot, (repo_id, kind))
- if row is not None and row.key == key and not stale(session.sync_session):
+ if row is not None and row.key == key and not force and not stale(session.sync_session):
return False
text = json.dumps(await build(), ensure_ascii=False)
if row is None:
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py b/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
index 988c6d301..0813ceedc 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/dashboard.py
@@ -73,6 +73,7 @@ def build_dashboard(
"top_findings",
),
"top_findings_total": findings.findings_total,
+ **({"top_findings_counts": data.finding_counts} if data.finding_counts else {}),
# The test half of the same ranked set, kept out of the production list.
"test_findings": pager.bound(
[
@@ -117,8 +118,11 @@ def _fix_first_block(data: HealthData, req: HealthRequest, pager: Pager) -> dict
block = page.as_dict(compact=True)
for item in block["items"]:
item.pop("next_call", None)
+ # Zero reasons say nothing an agent reads; ``counts.excluded`` omits them too.
+ totals = block["totals"]
+ totals["excluded"] = {reason: n for reason, n in totals["excluded"].items() if n}
block["lead"] = full.lead.compact() if full.lead is not None else None
- block["counts"] = full.counts(shown=len(page.items))
+ block["counts"] = full.counts(shown=len(page.items)).as_dict()
block["items_total"] = full.totals.eligible
block["cursor"] = req.fix_first_cursor
if full.lead is not None:
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/loading.py b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
index b718573cc..3b40711b1 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/loading.py
@@ -37,6 +37,7 @@ from repowise.core.persistence.crud import (
load_coverage_history,
)
from repowise.core.persistence.crud.analysis.fix_first import load_fix_first, queue_view
+from repowise.core.persistence.crud.analysis.queue_counts import unit_counts
from repowise.core.persistence.models import HealthFileMetric
from repowise.server.mcp_server._helpers import filter_rows_by_attr
from repowise.server.mcp_server.tool_health.findings import (
@@ -94,6 +95,8 @@ class HealthData:
fix_first: FixFirstQueue | None = None
#: The whole queue the page was cut from: its lead and its counts.
fix_first_full: FixFirstQueue | None = None
+ #: The findings' count vocabulary, read only when ``top_findings`` is named.
+ finding_counts: dict[str, Any] | None = None
# Dashboard only: the worst-first test files, ranked apart from
# ``metric_rows`` so a test never heads the production worklist.
test_metric_rows: list[HealthFileMetric] = field(default_factory=list)
@@ -165,6 +168,9 @@ async def load_health_data(
pop, metric_rows, findings, req, data.test_metric_rows
)
data.fix_first, data.fix_first_full = await _read_fix_first(session, repository, pop, req)
+ if "top_findings" in req.only_set and not pop.nothing_resolved:
+ counts = await unit_counts(session, repository.id, "findings", file_paths=pop.target_paths)
+ data.finding_counts = counts.as_dict()
return data
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/paging.py b/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
index 9460fa464..360c8c80d 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/paging.py
@@ -40,6 +40,10 @@ _PAGED_COLLECTIONS = frozenset(
_QUEUE_COLLECTIONS = frozenset({"refactoring_opportunities", "performance_opportunities"})
_QUEUE_PAGE = 6
+#: Collections of one unit each whose ``<key>_counts.shown`` is the rows that
+#: arrived. A plan page's rows are steps, so its count stays the opportunities.
+_COUNTED_COLLECTIONS = ("top_findings", "performance_opportunities")
+
#: A collection's fixed page, for naming a cut below ``limit`` a cap.
_COLLECTION_CAPS = {"refactoring_plans": PLANS_PAGE_CAP, "performance_opportunities": _QUEUE_PAGE}
@@ -165,6 +169,10 @@ def _settle_pages(result: dict[str, Any], call: Mapping[str, Any]) -> None:
limit=req.fix_first_cap,
)
_restate(result, pager, req, "fix_first")
+ for key in _COUNTED_COLLECTIONS:
+ counts = result.get(f"{key}_counts")
+ if isinstance(counts, dict) and isinstance(result.get(key), list):
+ counts["shown"] = len(result[key])
plans = result.get("refactoring_plans")
if isinstance(plans, list) and "refactoring_plans_total" in result:
total = int(result["refactoring_plans_total"] or 0)
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/pillars.py b/packages/server/src/repowise/server/mcp_server/tool_health/pillars.py
index 33341a55d..26b5af68b 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/pillars.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/pillars.py
@@ -220,6 +220,7 @@ def _render_performance(
result["performance_opportunities"] = page.items
result["performance_opportunities_total"] = page.total
result["performance_opportunities_emitted"] = len(page.items)
+ result["performance_opportunities_counts"] = page.counts
if page.next_offset is not None:
pager.recoveries["performance_opportunities"] = (
page.next_offset,
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/plans.py b/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
index 2132a5dfd..424ed3cbc 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/plans.py
@@ -33,6 +33,7 @@ def _render_plans(
result["refactoring_plans_opportunities_total"] = page.opportunities_total
if page.hidden is not None:
result["refactoring_plans_hidden"] = page.hidden
+ result["refactoring_plans_counts"] = page.counts
pager.note_page(
"refactoring_plans",
start=req.cursor,
diff --git a/packages/server/src/repowise/server/mcp_server/tool_health/projection.py b/packages/server/src/repowise/server/mcp_server/tool_health/projection.py
index 899a90c3f..abf1c9206 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_health/projection.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_health/projection.py
@@ -101,6 +101,7 @@ def _project(
f"{k}_reduced_reason",
f"{k}_scope",
f"{k}_hidden",
+ f"{k}_counts",
)
}
)
diff --git a/packages/server/src/repowise/server/routers/code_health/fix_first_routes.py b/packages/server/src/repowise/server/routers/code_health/fix_first_routes.py
index 183d6ca1f..ec10ec8e1 100644
--- a/packages/server/src/repowise/server/routers/code_health/fix_first_routes.py
+++ b/packages/server/src/repowise/server/routers/code_health/fix_first_routes.py
@@ -15,8 +15,9 @@ from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.agent_prompts import Flavor, render_fix_item
from repowise.core.analysis.health.fix_first import FIX_SCOPES, FixItem
from repowise.core.analysis.health.fix_first.model import FixTotals
+from repowise.core.analysis.health.queue.counts import QueueCounts
from repowise.core.persistence import crud
-from repowise.core.persistence.crud.analysis.fix_first import load_fix_first
+from repowise.core.persistence.crud.analysis.fix_first import load_fix_first, queue_view
from repowise.core.persistence.models import Repository
from repowise.server.deps import get_db_session
from repowise.server.schemas.agent_prompts import AgentPromptResponse
@@ -33,6 +34,8 @@ class FixFirstQueueResponse(BaseModel):
by_improves: dict[str, int]
model_version: int
basis: dict[str, str | None]
+ #: The items' count vocabulary; ``totals`` stays for clients that read it.
+ counts: QueueCounts
def _scope(scope: str) -> str:
@@ -62,8 +65,9 @@ async def get_fix_first(
session: AsyncSession = Depends(get_db_session),
) -> dict[str, Any]:
"""The ranked queue, its lead, and how many units each rule excluded."""
- queue = await load_fix_first(session, repo_id, limit=limit, scope=_scope(scope))
- return queue.as_dict()
+ full = await load_fix_first(session, repo_id, limit=None, scope=_scope(scope))
+ queue = queue_view(full, limit=limit)
+ return {**queue.as_dict(), "counts": full.counts(len(queue.items)).as_dict()}
@router.get("/api/repos/{repo_id}/health/fix-first/{fix_id}", response_model=FixItem)
diff --git a/packages/server/src/repowise/server/routers/code_health/overview_routes.py b/packages/server/src/repowise/server/routers/code_health/overview_routes.py
index 657d26671..44d064b28 100644
--- a/packages/server/src/repowise/server/routers/code_health/overview_routes.py
+++ b/packages/server/src/repowise/server/routers/code_health/overview_routes.py
@@ -18,6 +18,7 @@ from repowise.core.analysis.health.grading import distribution as health_distrib
from repowise.core.analysis.health.ranking import deduction_by_path
from repowise.core.analysis.health.scoring import ZERO_IMPACT_DIMENSIONS, hotspot_health
from repowise.core.persistence import crud
+from repowise.core.persistence.crud.analysis.queue_counts import all_unit_counts
from repowise.server.deps import get_db_session
from repowise.server.mcp_server._meta import resolve_indexed_commit
@@ -144,6 +145,12 @@ async def health_overview(
"top_findings": top_findings,
"modules": module_rollups(metrics, deductions),
"biomarkers": biomarker_breakdown(findings),
+ # Every queue unit's count vocabulary, over the stored judgements: the
+ # same numbers the lists, MCP and the CLI report, whatever scope or
+ # counts this page projects.
+ "queue_counts": await all_unit_counts(
+ session, repo_id, shown={"findings": len(top_findings)}
+ ),
"meta": {
"last_indexed_at": last_indexed_at,
# Prefer state.json's last_sync_commit over a possibly-stale DB row
diff --git a/packages/server/src/repowise/server/routers/code_health/performance_routes.py b/packages/server/src/repowise/server/routers/code_health/performance_routes.py
index 71fcb959c..1e4c75e5c 100644
--- a/packages/server/src/repowise/server/routers/code_health/performance_routes.py
+++ b/packages/server/src/repowise/server/routers/code_health/performance_routes.py
@@ -120,6 +120,7 @@ async def list_performance_opportunities(
"next_offset": page.next_offset,
"facets": page.facets,
"summary": page.summary,
+ "counts": page.counts,
**({"ignored_arguments": ignored} if ignored else {}),
}
diff --git a/packages/server/src/repowise/server/routers/overview.py b/packages/server/src/repowise/server/routers/overview.py
index d242fa89b..41d9c7433 100644
--- a/packages/server/src/repowise/server/routers/overview.py
+++ b/packages/server/src/repowise/server/routers/overview.py
@@ -25,6 +25,7 @@ from repowise.core.analysis.health.aggregation import (
)
from repowise.core.analysis.health.scoring import hotspot_health
from repowise.core.persistence import crud
+from repowise.core.persistence.crud.analysis.queue_counts import unit_counts
from repowise.core.persistence.models import (
DeadCodeFinding,
GenerationJob,
@@ -563,6 +564,9 @@ async def overview_summary(
"worst_performer_path": health_summary.get("worst_performer_path"),
"worst_performer_score": health_summary.get("worst_performer_score"),
"open_findings": health_summary.get("open_findings", 0),
+ # The findings' count vocabulary, which the Code Health page and the
+ # agent surfaces report too; ``open_findings`` stays for older readers.
+ "finding_counts": (await unit_counts(session, repo_id, "findings")).as_dict(),
# The two co-equal pillars surfaced alongside the defect headline.
"maintainability_average": health_summary.get("maintainability_average"),
"performance_average": health_summary.get("performance_average"),
diff --git a/packages/server/src/repowise/server/routers/refactoring.py b/packages/server/src/repowise/server/routers/refactoring.py
index 4381c01a8..f259fc4ce 100644
--- a/packages/server/src/repowise/server/routers/refactoring.py
+++ b/packages/server/src/repowise/server/routers/refactoring.py
@@ -368,7 +368,10 @@ async def get_refactoring_opportunities(
"facets": page.facets,
"summary": page.summary,
"scope": page.scope,
+ "counts": page.counts,
}
+ # ``hidden`` restates ``counts.excluded`` for the board; remove once the
+ # web reads ``counts`` (UI adoption).
if page.hidden is not None:
body["hidden"] = page.hidden
if ignored:
diff --git a/packages/server/src/repowise/server/schemas/refactoring.py b/packages/server/src/repowise/server/schemas/refactoring.py
index d9fb52974..47b2468cc 100644
--- a/packages/server/src/repowise/server/schemas/refactoring.py
+++ b/packages/server/src/repowise/server/schemas/refactoring.py
@@ -13,6 +13,8 @@ from typing import Any, Literal
from pydantic import BaseModel, ConfigDict
+from repowise.core.analysis.health.queue.counts import QueueCounts
+
class RefactoringHiddenCounts(BaseModel):
"""What the ``fix_first`` scope leaves out of a page's filtered set."""
@@ -41,6 +43,8 @@ class RefactoringOpportunitiesResponse(BaseModel):
scope: Literal["fix_first", "all"] = "all"
#: Under ``fix_first``, the rest of the filtered set, by reason.
hidden: RefactoringHiddenCounts | None = None
+ #: The plans' count vocabulary over the open inventory (``queue.counts``).
+ counts: QueueCounts | None = None
class RefactoringRollupResponse(BaseModel):
diff --git a/packages/server/src/repowise/server/services/performance_health.py b/packages/server/src/repowise/server/services/performance_health.py
index 08aab49df..5861396a3 100644
--- a/packages/server/src/repowise/server/services/performance_health.py
+++ b/packages/server/src/repowise/server/services/performance_health.py
@@ -44,6 +44,7 @@ from repowise.core.persistence.crud import (
list_performance_opportunities,
performance_facet_counts,
)
+from repowise.core.persistence.crud.analysis.queue_counts import any_unjudged, unit_counts
from ..mcp_server._references import refactoring_plan_id
@@ -57,6 +58,8 @@ class PerformancePage:
facets: dict[str, list[dict[str, Any]]]
summary: dict[str, Any]
ignored_arguments: dict[str, str] = field(default_factory=dict)
+ #: The causes' count vocabulary (``queue.counts``) in the query's files.
+ counts: dict[str, Any] = field(default_factory=dict)
class PerformanceHealthService:
@@ -89,16 +92,14 @@ class PerformanceHealthService:
filter control needs. It stays a single indexed aggregate over a table
that already holds one row per cause rather than one per observation.
"""
+ judged = not (
+ query.default_queue
+ and await any_unjudged(self._session, self._repository_id, "causes")
+ )
rows, total = await list_performance_opportunities(
self._session,
self._repository_id,
- contexts=query.contexts,
- boundary=query.boundary,
- confidence=query.confidence,
- actionabilities=query.actionabilities,
- proofs=query.proofs,
- roles=query.roles,
- file_paths=query.file_paths,
+ **query.store_filters(judged=judged),
sort=query.sort,
limit=query.limit,
offset=query.offset,
@@ -126,6 +127,15 @@ class PerformanceHealthService:
next_offset=emitted if emitted < total else None,
facets=await self._facets(query) if with_facets else {},
summary=await self.summary(query.contexts) if with_summary else {},
+ counts=(
+ await unit_counts(
+ self._session,
+ self._repository_id,
+ "causes",
+ shown=len(items),
+ file_paths=query.file_paths,
+ )
+ ).as_dict(),
)
async def _evidence_for(
diff --git a/packages/server/src/repowise/server/services/refactoring_health.py b/packages/server/src/repowise/server/services/refactoring_health.py
index 494ec4e02..e8b688742 100644
--- a/packages/server/src/repowise/server/services/refactoring_health.py
+++ b/packages/server/src/repowise/server/services/refactoring_health.py
@@ -24,7 +24,7 @@ from typing import Any, Literal
from sqlalchemy import or_, select
from sqlalchemy.ext.asyncio import AsyncSession
-from repowise.core.analysis.health.queue.eligibility import Tally, Verdict
+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.refactoring.recommendations import (
@@ -54,6 +54,7 @@ from repowise.core.analysis.health.refactoring.serving import (
from repowise.core.analysis.health.refactoring_summary import summarize_plans
from repowise.core.analysis.health.rows import detail_map, json_field
from repowise.core.persistence.crud.analysis.fix_first import load_fix_first
+from repowise.core.persistence.crud.analysis.queue_counts import any_unjudged, unit_counts
from repowise.core.persistence.crud.analysis.refactoring import (
get_refactoring_suggestions,
ranked_refactoring_suggestions,
@@ -65,12 +66,19 @@ from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
list_refactoring_opportunities,
refactoring_facet_counts,
refactoring_opportunities_by_id,
- refactoring_opportunity_ids,
+ refactoring_reason_counts,
refactoring_step_counts,
)
from repowise.core.persistence.models import RefactoringOpportunity, RefactoringSuggestion
+def _hidden(left_out: dict[str, int]) -> dict[str, Any]:
+ return {
+ "total": sum(left_out.values()),
+ "by_reason": dict(sorted(left_out.items(), key=lambda i: (-i[1], i[0]))),
+ }
+
+
@dataclass(frozen=True, slots=True)
class RefactoringPage:
items: list[dict[str, Any]]
@@ -84,6 +92,8 @@ class RefactoringPage:
#: Under ``fix_first``: the opportunities the same filters match that Fix
#: first leaves out, ``{"total": n, "by_reason": {reason: n}}``.
hidden: dict[str, Any] | None = None
+ #: The plans' count vocabulary (``queue.counts``) in the query's files.
+ counts: dict[str, Any] = field(default_factory=dict)
@dataclass(frozen=True, slots=True)
@@ -98,6 +108,7 @@ class RefactoringPlanPage:
next_offset: int
scope: str = "all"
hidden: dict[str, Any] | None = None
+ counts: dict[str, Any] = field(default_factory=dict)
def _filters(query: RefactoringQuery) -> dict[str, Any]:
@@ -233,13 +244,11 @@ class RefactoringHealthService:
write) says which opportunities it takes, and one id-only read of the
filtered set counts what it leaves out, by reason.
"""
- filters = _filters(query)
- shown_ids, hidden = await self._scope(query, filters)
+ filters, hidden = await self._scope(query, _filters(query))
rows, total = await list_refactoring_opportunities(
self._session,
self._repository_id,
**filters,
- opportunity_ids=shown_ids,
order=query.resolved_order,
limit=query.limit,
offset=query.offset,
@@ -255,21 +264,16 @@ class RefactoringHealthService:
offset=query.offset,
next_offset=next_offset if next_offset < total else None,
facets=(
- # Scoped to the status being listed. Facets counting the open
- # set while the list shows the resolved one would put a badge on
- # a tab that returns nothing.
- await refactoring_facet_counts(
- self._session,
- self._repository_id,
- status=query.status,
- opportunity_ids=shown_ids,
- )
+ # Under the list's status, scope and filters, so a badge never
+ # counts rows its tab would not return.
+ await refactoring_facet_counts(self._session, self._repository_id, **filters)
if with_facets
else {}
),
summary=await self.summary() if with_summary else None,
scope=query.scope,
hidden=hidden,
+ counts=await self._counts(query, len(items)),
)
async def plan_page(self, query: RefactoringQuery) -> RefactoringPlanPage:
@@ -280,13 +284,11 @@ class RefactoringHealthService:
of step counts places the page; only the opportunities it touches are
decoded.
"""
- filters = _filters(query)
- shown_ids, hidden = await self._scope(query, filters)
+ filters, hidden = await self._scope(query, _filters(query))
counts = await refactoring_step_counts(
self._session,
self._repository_id,
**filters,
- opportunity_ids=shown_ids,
order=query.resolved_order,
)
window = _plan_window(counts, query.offset, query.limit)
@@ -307,39 +309,50 @@ class RefactoringHealthService:
next_offset=query.offset + sum(stop - start for start, stop in window.values()),
scope=query.scope,
hidden=hidden,
+ counts=await self._counts(query, len(window)),
)
async def _scope(
self, query: RefactoringQuery, filters: dict[str, Any]
- ) -> tuple[list[str] | None, dict[str, Any] | None]:
- """The ids a ``fix_first`` scope keeps and what it hides; ``all`` keeps every row."""
+ ) -> tuple[dict[str, Any], dict[str, Any] | None]:
+ """The filters a scope reads, and under ``fix_first`` what the same
+ filters match that Fix first leaves out, by the reason stored on each
+ row at index time. ``all`` keeps every row."""
if query.scope != "fix_first":
- return None, None
- return await self._fix_first_scope(filters)
-
- async def _fix_first_scope(
+ return filters, None
+ if await any_unjudged(self._session, self._repository_id, "plans"):
+ return await self._live_fix_first_scope(filters)
+ by_reason = await refactoring_reason_counts(self._session, self._repository_id, **filters)
+ left_out = {reason: n for reason, n in by_reason.items() if reason is not None}
+ return {**filters, "queue_eligible": True}, _hidden(left_out)
+
+ async def _live_fix_first_scope(
self, filters: dict[str, Any]
- ) -> tuple[list[str], dict[str, Any]]:
- """The filtered ids Fix first takes, and what it leaves out by reason.
+ ) -> tuple[dict[str, Any], dict[str, Any]]:
+ """The ``fix_first`` scope on a store whose plans are not all judged:
+ the ids the live Fix first queue takes, the rest counted as not judged.
- Ceiling: the shown ids go back to the page read as an ``IN`` list, one
- entry per open opportunity Fix first takes (94 on this repository).
- Upgrade path: store the eligibility on the opportunity row at index
- time and filter on the column.
+ Ceiling: the ids go back as an ``IN`` list; the next index judges every
+ row and the stored verdict replaces this.
"""
- queue = await load_fix_first(self._session, self._repository_id, limit=0, verify=False)
- reasons = queue.refactoring_reasons
- matched = await refactoring_opportunity_ids(
- self._session, self._repository_id, **filters
+ queue = await load_fix_first(self._session, self._repository_id, limit=None, verify=False)
+ ids = sorted({i.source.opportunity_id for i in queue.items if i.kind == "refactor"})
+ scoped = {**filters, "opportunity_ids": ids}
+ repo = self._repository_id
+ matched = sum((await refactoring_reason_counts(self._session, repo, **filters)).values())
+ kept = sum((await refactoring_reason_counts(self._session, repo, **scoped)).values())
+ return scoped, _hidden({NOT_JUDGED: matched - kept} if matched > kept else {})
+
+ async def _counts(self, query: RefactoringQuery, shown: int) -> dict[str, Any]:
+ """The plans' counts in the query's files: where the page sits in the queue."""
+ counts = await unit_counts(
+ self._session,
+ self._repository_id,
+ "plans",
+ shown=shown,
+ file_paths=query.file_paths,
)
- # The queue is keyed on the stores' newest write, so it has read every
- # open id; one written between the two reads is in neither count.
- shown = [i for i in matched if i in reasons and reasons[i] is None]
- left_out = Tally()
- for i in matched:
- if reason := reasons.get(i):
- left_out.add(Verdict(reason))
- return shown, {"total": left_out.total, "by_reason": left_out.by_reason()}
+ return counts.as_dict()
# -- headline ---------------------------------------------------------
diff --git a/packages/types/package.json b/packages/types/package.json
index 7c1265e92..c8bcf2b23 100644
--- a/packages/types/package.json
+++ b/packages/types/package.json
@@ -36,6 +36,7 @@
"./stats": "./src/stats.ts",
"./refactoring": "./src/refactoring.ts",
"./fix-first": "./src/fix-first.ts",
+ "./queue": "./src/queue.ts",
"./agent-prompts": "./src/agent-prompts.ts",
"./serve-lock": "./src/serve-lock.ts",
"./generated/http": "./src/generated/http.ts"
diff --git a/packages/types/src/fix-first.ts b/packages/types/src/fix-first.ts
index 69bf04994..2b177a402 100644
--- a/packages/types/src/fix-first.ts
+++ b/packages/types/src/fix-first.ts
@@ -7,6 +7,7 @@
*/
import type { ActionCommand } from "./actions.js";
+import type { QueueCounts } from "./queue.js";
export type FixTier = "now" | "next" | "later";
@@ -125,20 +126,8 @@ export interface FixFirstTotals {
dormant: number;
}
-/**
- * The five-level count vocabulary (`FixFirstQueue.counts` in core), read on the
- * whole queue: every unit considered, those in scope (not excluded for where
- * the code lives), those that became an item, those due (tier now or next),
- * and those this response shows; `excluded` holds the non-zero exclusions.
- */
-export interface FixFirstCounts {
- inventory: number;
- in_scope: number;
- eligible: number;
- due: number;
- shown: number;
- excluded: Partial<Record<FixExclusion, number>>;
-}
+/** The count vocabulary (`QueueCounts`) read on the whole Fix first queue. */
+export type FixFirstCounts = QueueCounts;
export interface FixFirstQueue<Item = FixItem> {
items: Item[];
@@ -149,4 +138,6 @@ export interface FixFirstQueue<Item = FixItem> {
by_improves: Record<FixImproves, number>;
model_version: number;
basis: { analyzed_commit: string | null; health_analyzed_at: string | null };
+ /** The items' counts; `totals` stays for older readers. */
+ counts?: QueueCounts;
}
diff --git a/packages/types/src/generated/http.ts b/packages/types/src/generated/http.ts
index 1261afb03..0bd93bbda 100644
--- a/packages/types/src/generated/http.ts
+++ b/packages/types/src/generated/http.ts
@@ -9,6 +9,12 @@
// `?` mirrors the schema's `required` list, which states what a request may
// omit. A response field with a server-side default is still always sent.
+/**
+ * A way to see more: the MCP call an agent makes, the CLI line a person runs.
+ *
+ * ``tool`` and ``arguments`` are the structured call; ``mcp`` is that call
+ * rendered by :func:`render_call`. Build one with :meth:`call`.
+ */
export interface ActionCommand {
purpose: string;
mcp?: string | null;
@@ -1571,6 +1577,7 @@ export interface FixFirstQueueResponse {
by_improves: Record<string, number>;
model_version: number;
basis: Record<string, string | null>;
+ counts: QueueCounts;
}
export interface FixGain {
@@ -2685,6 +2692,16 @@ export interface PublishResponse {
repo?: string | null;
}
+/** One unit's counts at the five levels, and the reasons for what is out. */
+export interface QueueCounts {
+ inventory?: number;
+ in_scope?: number;
+ eligible?: number;
+ due?: number;
+ shown?: number;
+ excluded?: Record<string, number>;
+}
+
/** What the ``fix_first`` scope leaves out of a page's filtered set. */
export interface RefactoringHiddenCounts {
total?: number;
@@ -2703,6 +2720,7 @@ export interface RefactoringOpportunitiesResponse {
ignored_arguments?: Record<string, string> | null;
scope?: "fix_first" | "all";
hidden?: RefactoringHiddenCounts | null;
+ counts?: QueueCounts | null;
}
/**
diff --git a/packages/types/src/health.ts b/packages/types/src/health.ts
index dfe5539d3..e3bbedd13 100644
--- a/packages/types/src/health.ts
+++ b/packages/types/src/health.ts
@@ -18,6 +18,7 @@
import type { C4IoKind } from "./external-systems.js";
import type { CoverageHistoryPoint, CoverageSummary } from "./generated/http.js";
import type { Paginated } from "./pagination.js";
+import type { QueueCounts, QueueCountsByUnit } from "./queue.js";
import type {
StepClassification,
StepVerify,
@@ -689,6 +690,8 @@ export interface PerformanceOpportunityPage extends Paginated<PerformanceOpportu
facets: PerformanceFacets;
/** Filter values the server did not recognize, named rather than dropped. */
ignored_arguments?: Record<string, string>;
+ /** The causes' count vocabulary over the open inventory in the queried files. */
+ counts?: QueueCounts;
}
export interface HealthModuleRow {
@@ -784,6 +787,8 @@ export interface HealthOverviewResponse {
top_findings: HealthFinding[];
modules?: HealthModuleRow[];
biomarkers?: BiomarkerBreakdownRow[];
+ /** Every queue unit's counts, unprojected by the page's scope or counts. */
+ queue_counts?: QueueCountsByUnit;
meta?: {
last_indexed_at: string | null;
head_commit: string | null;
diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts
index 2ac2a1401..c72e4a0dc 100644
--- a/packages/types/src/index.ts
+++ b/packages/types/src/index.ts
@@ -35,5 +35,6 @@ export * from "./coupling.js";
export * from "./stats.js";
export * from "./refactoring.js";
export * from "./fix-first.js";
+export * from "./queue.js";
export * from "./agent-prompts.js";
export * from "./serve-lock.js";
diff --git a/packages/types/src/overview.ts b/packages/types/src/overview.ts
index b671750c2..ae0891d56 100644
--- a/packages/types/src/overview.ts
+++ b/packages/types/src/overview.ts
@@ -6,6 +6,7 @@
import type { Hotspot } from "./git.js";
import type { DefectAccuracy, HealthSeverity } from "./health.js";
+import type { QueueCounts } from "./queue.js";
export interface OverviewRepoMeta {
id: string;
@@ -84,6 +85,8 @@ export interface OverviewHealth {
worst_performer_path: string | null;
worst_performer_score: number | null;
open_findings: number;
+ /** The findings' count vocabulary; the same numbers the Code Health page shows. */
+ finding_counts?: QueueCounts;
/** Co-equal maintainability pillar headline (NLOC-weighted). `null` when no
* file carries a maintainability score yet. */
maintainability_average?: number | null;
diff --git a/packages/types/src/queue.ts b/packages/types/src/queue.ts
new file mode 100644
index 000000000..00bfaeeee
--- /dev/null
+++ b/packages/types/src/queue.ts
@@ -0,0 +1,60 @@
+/**
+ * The one count vocabulary every code-health queue reports
+ * (`analysis/health/queue/counts.py` in core), for findings, performance
+ * causes, refactoring plans and Fix first items alike.
+ */
+
+/**
+ * Why a unit is out of a default queue (`queue.eligibility.Reason`), plus the
+ * stored-judgement reasons (`queue.counts.STORED_REASONS`): `not_judged` (stored
+ * before units were judged), `covered_by_plan`, `not_file_lead`, `not_in_queue`.
+ */
+export type QueueReason =
+ | "test"
+ | "tooling"
+ | "unknown"
+ | "generated"
+ | "expected"
+ | "no_strategy"
+ | "no_plan"
+ | "below_min_worth"
+ | "history_only"
+ | "vendored"
+ | "docs_example"
+ | "deprecated"
+ | "gated_off"
+ | "cold_path"
+ | "unreachable"
+ | "inherent_dispatch"
+ | "small_function"
+ | "no_concrete_step"
+ | "low_value_kind"
+ | "kind_unaudited"
+ | "unmeasured_cost"
+ | "cold_role"
+ | "background_unproven"
+ | "not_judged"
+ | "covered_by_plan"
+ | "not_file_lead"
+ | "not_in_queue";
+
+/** One noun per unit: findings (observations), causes (performance), plans (refactoring), items (Fix first). */
+export type QueueUnit = "findings" | "causes" | "plans" | "items";
+
+/**
+ * `inventory` every open unit; `in_scope` those whose code is in scope (not
+ * test, tooling, vendored, generated or docs); `eligible` those a default
+ * queue holds; `due` eligible units in tier now or next; `shown` what the
+ * response carries; `excluded` the non-zero reasons, so the levels add up.
+ */
+export interface QueueCounts {
+ inventory: number;
+ in_scope: number;
+ eligible: number;
+ due: number;
+ shown: number;
+ excluded: Partial<Record<QueueReason, number>>;
+}
+
+/** Every unit's counts, by noun (`queue_counts` on the health overview and `repowise health --format json`). */
+export type QueueCountsByUnit = Record<QueueUnit, QueueCounts>;
diff --git a/packages/types/src/refactoring.ts b/packages/types/src/refactoring.ts
index dfffb254a..62c810261 100644
--- a/packages/types/src/refactoring.ts
+++ b/packages/types/src/refactoring.ts
@@ -3,6 +3,7 @@
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 =
| "extract_class"
@@ -497,6 +498,8 @@ export interface RefactoringOpportunityPage {
scope?: RefactoringScope;
/** Under ``fix_first`` only. */
hidden?: RefactoringHiddenCounts;
+ /** The plans' count vocabulary over the open inventory in the queried files. */
+ counts?: QueueCounts;
}
/** ``GET /api/repos/{repo_id}/refactoring/summary``. */
diff --git a/packages/ui/src/health/performance-view.tsx b/packages/ui/src/health/performance-view.tsx
index 87ca5180b..90b238f41 100644
--- a/packages/ui/src/health/performance-view.tsx
+++ b/packages/ui/src/health/performance-view.tsx
@@ -212,7 +212,11 @@ export function PerformanceView({
<PageLede
label="Causal opportunities"
value={summary.total.toLocaleString()}
- unit="ranked causes, not repeated observations"
+ unit={
+ data.counts
+ ? `ranked causes, not repeated observations; ${data.counts.eligible.toLocaleString()} in the default queue, ${data.counts.inventory.toLocaleString()} in the repository`
+ : "ranked causes, not repeated observations"
+ }
layout="beside"
figureFooter={
<p className="text-caption text-[var(--color-text-tertiary)]">
diff --git a/packages/ui/src/overview/overview-model.ts b/packages/ui/src/overview/overview-model.ts
index 3e1b38ae2..2a7d2bea5 100644
--- a/packages/ui/src/overview/overview-model.ts
+++ b/packages/ui/src/overview/overview-model.ts
@@ -337,7 +337,7 @@ export function buildExplore(
key: "code-health",
label: "Code health",
href: `${r.base}/code-health`,
- description: `Per-file scores, ${formatNumber(health.open_findings)} open findings, coverage and refactoring targets`,
+ description: `Per-file scores, ${formatNumber(health.finding_counts?.inventory ?? health.open_findings)} open findings, coverage and refactoring targets`,
},
{
key: "knowledge-graph",
diff --git a/packages/ui/src/refactoring/refactoring-board.tsx b/packages/ui/src/refactoring/refactoring-board.tsx
index 6ad18cacd..482b469fb 100644
--- a/packages/ui/src/refactoring/refactoring-board.tsx
+++ b/packages/ui/src/refactoring/refactoring-board.tsx
@@ -59,6 +59,7 @@ import type {
RefactoringOrder,
RefactoringScope,
} from "@repowise-dev/types/refactoring";
+import type { QueueCounts } from "@repowise-dev/types/queue";
const PAGE_SIZE = 60;
@@ -91,6 +92,8 @@ export interface RefactoringBoardServerState {
appliedScope?: RefactoringScope | undefined;
/** Under `fix_first`, what the filtered set leaves out, by reason. */
hidden?: RefactoringHiddenCounts | null | undefined;
+ /** The plans' count vocabulary, so the lede and the tabs name their level. */
+ counts?: QueueCounts | null | undefined;
total: number;
offset: number;
nextOffset: number | null;
@@ -160,7 +163,10 @@ export function RefactoringBoard({
const rollupTotal =
summary && summary.status === "available" ? summary.opportunities_total : null;
// Empty only when the inventory is: the default scope can list none of it.
- const inventory = rollupTotal ?? serverState.total + (serverState.hidden?.total ?? 0);
+ const inventory =
+ serverState.counts?.inventory ??
+ rollupTotal ??
+ serverState.total + (serverState.hidden?.total ?? 0);
const filtersActive =
serverState.query.trim() !== "" ||
serverState.effort !== null ||
@@ -204,6 +210,7 @@ export function RefactoringBoard({
{showLede ? (
<RefactoringLede
summary={summary}
+ counts={serverState.counts}
indexedFileCount={indexedFileCount}
facets={facets}
quickWinsActive={quickWinsActive}
diff --git a/packages/ui/src/refactoring/refactoring-lede.tsx b/packages/ui/src/refactoring/refactoring-lede.tsx
index e38857b16..d1dbbb7d5 100644
--- a/packages/ui/src/refactoring/refactoring-lede.tsx
+++ b/packages/ui/src/refactoring/refactoring-lede.tsx
@@ -18,6 +18,7 @@ import type {
RefactoringOpportunityPage,
RefactoringOpportunityRollup,
} from "@repowise-dev/types/refactoring";
+import type { QueueCounts } from "@repowise-dev/types/queue";
/** The page's facet counts, by facet then value. */
export type RefactoringFacets = RefactoringOpportunityPage["facets"];
@@ -25,6 +26,8 @@ export type RefactoringFacets = RefactoringOpportunityPage["facets"];
export interface RefactoringLedeProps {
/** The repository rollup. Absent or unavailable and the lede does not render. */
summary?: RefactoringOpportunityRollup | null | undefined;
+ /** The plans' count vocabulary; names how many of the open ones are worth doing now. */
+ counts?: QueueCounts | null | undefined;
/** Files indexed, for the all-clear line. */
indexedFileCount?: number | undefined;
/** The page's facet counts. Absent and the chips do not render. */
@@ -49,6 +52,7 @@ function plural(n: number, one: string, many: string): string {
export function RefactoringLede({
summary,
+ counts,
indexedFileCount,
facets,
quickWinsActive = false,
@@ -97,7 +101,12 @@ export function RefactoringLede({
<PageLede
label="Open opportunities"
value={formatNumber(total)}
- unit={files ? `one per file, across ${plural(files, "file", "files")}` : undefined}
+ unit={
+ files
+ ? `one per file, across ${plural(files, "file", "files")}` +
+ (counts ? `; ${formatNumber(counts.eligible)} worth doing first` : "")
+ : undefined
+ }
action={action}
>
<p>
diff --git a/packages/ui/vitest.config.ts b/packages/ui/vitest.config.ts
index 1cb67b8c0..631b4b5ba 100644
--- a/packages/ui/vitest.config.ts
+++ b/packages/ui/vitest.config.ts
@@ -41,6 +41,7 @@ export default defineConfig({
"@repowise-dev/types/overview": path.resolve(__dirname, "../types/src/overview.ts"),
"@repowise-dev/types/actions": path.resolve(__dirname, "../types/src/actions.ts"),
"@repowise-dev/types/fix-first": path.resolve(__dirname, "../types/src/fix-first.ts"),
+ "@repowise-dev/types/queue": path.resolve(__dirname, "../types/src/queue.ts"),
"@repowise-dev/types/agent-prompts": path.resolve(
__dirname,
"../types/src/agent-prompts.ts",
diff --git a/packages/web/src/app/repos/[id]/code-health/page.tsx b/packages/web/src/app/repos/[id]/code-health/page.tsx
index ea037fb08..55b7f1e6c 100644
--- a/packages/web/src/app/repos/[id]/code-health/page.tsx
+++ b/packages/web/src/app/repos/[id]/code-health/page.tsx
@@ -431,6 +431,7 @@ export default function CodeHealthPage() {
// it here would put a bigger number on the tab than the tab can show.
if (overview) {
badges.findings =
+ overview.queue_counts?.findings.inventory ??
overview.summary.open_findings - (overview.summary.performance_findings ?? 0);
}
if (deadCode) badges["dead-code"] = deadCode.total_findings;
diff --git a/packages/web/src/app/repos/[id]/refactoring/page.tsx b/packages/web/src/app/repos/[id]/refactoring/page.tsx
index 47902b6e1..51589d684 100644
--- a/packages/web/src/app/repos/[id]/refactoring/page.tsx
+++ b/packages/web/src/app/repos/[id]/refactoring/page.tsx
@@ -275,6 +275,7 @@ export default function RefactoringPage({ params }: { params: Promise<{ id: stri
scope,
appliedScope: data?.scope ?? "all",
hidden: data?.hidden ?? null,
+ counts: data?.counts ?? null,
total: data?.total ?? 0,
offset,
nextOffset: data?.next_offset ?? null,
diff --git a/tests/unit/cli/test_health_cmd_controls.py b/tests/unit/cli/test_health_cmd_controls.py
index c80697b3a..8bb27df09 100644
--- a/tests/unit/cli/test_health_cmd_controls.py
+++ b/tests/unit/cli/test_health_cmd_controls.py
@@ -123,7 +123,7 @@ def test_the_report_leads_with_fix_first() -> None:
)
console = Console(file=io.StringIO(), width=400, force_terminal=False)
with patch("repowise.cli.commands.health_cmd.summary.console", console):
- _render_fix_first(queue)
+ _render_fix_first((queue, queue.counts(len(queue.items))))
_render_fix_first(None)
out = console.file.getvalue()
assert out.startswith("\nFix first (3 of 3 eligible")
diff --git a/tests/unit/cli/test_read_snapshot_refresh.py b/tests/unit/cli/test_read_snapshot_refresh.py
index 2682ff1a8..dc245cbd5 100644
--- a/tests/unit/cli/test_read_snapshot_refresh.py
+++ b/tests/unit/cli/test_read_snapshot_refresh.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
+import json
import sqlite3
import subprocess
from pathlib import Path
@@ -19,6 +20,12 @@ def _kinds(repo: Path) -> list[str]:
return sorted(r[0] for r in db.execute("SELECT kind FROM read_snapshots"))
+def _payload(repo: Path, kind: str) -> dict:
+ with sqlite3.connect(repo / ".repowise" / "wiki.db") as db:
+ row = db.execute("SELECT payload_json FROM read_snapshots WHERE kind = ?", (kind,)).fetchone()
+ return json.loads(row[0])
+
+
def test_index_writes_and_update_restores_the_snapshots(tmp_path: Path) -> None:
from repowise.core.pipeline.full_index import index_repo_full
@@ -33,12 +40,15 @@ def test_index_writes_and_update_restores_the_snapshots(tmp_path: Path) -> None:
(repo / ".repowise").mkdir()
asyncio.run(index_repo_full(repo))
- assert _kinds(repo) == ["actions", "fix_first"]
+ assert _kinds(repo) == ["actions", "fix_first", "fix_first_counts"]
+ counts = _payload(repo, "fix_first_counts")
with sqlite3.connect(repo / ".repowise" / "wiki.db") as db:
db.execute("DELETE FROM read_snapshots")
refresh_read_snapshots(repo)
- assert _kinds(repo) == ["actions", "fix_first"]
+ assert _kinds(repo) == ["actions", "fix_first", "fix_first_counts"]
+ # The restored item counts are the ones the index stored.
+ assert _payload(repo, "fix_first_counts") == counts
def test_every_update_outcome_ends_in_one_helper() -> None:
diff --git a/tests/unit/cli/test_refactoring_targets_stored.py b/tests/unit/cli/test_refactoring_targets_stored.py
index 49567e7be..08940410f 100644
--- a/tests/unit/cli/test_refactoring_targets_stored.py
+++ b/tests/unit/cli/test_refactoring_targets_stored.py
@@ -71,6 +71,7 @@ async def _store(repo_path: Path, paths: list[str] = _PATHS) -> list[str]:
init_db,
upsert_repository,
)
+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots
from repowise.server.services.refactoring_health import RefactoringHealthService
db_path = repo_path / ".repowise" / "wiki.db"
@@ -90,6 +91,7 @@ async def _store(repo_path: Path, paths: list[str] = _PATHS) -> list[str]:
await crud.finalize_refactoring_opportunities(
session, repo.id, analyzed_commit="c" * 40
)
+ await write_read_snapshots(session, repo.id)
await session.commit()
page = await RefactoringHealthService(session, repo.id, "repo").page(
parse_query(limit=20)[0]
diff --git a/tests/unit/health/test_fix_first_build.py b/tests/unit/health/test_fix_first_build.py
index acfa02e95..8c758d093 100644
--- a/tests/unit/health/test_fix_first_build.py
+++ b/tests/unit/health/test_fix_first_build.py
@@ -216,16 +216,20 @@ def test_value_leads_tier_and_a_huge_function_says_so() -> None:
)
-def test_every_open_refactoring_opportunity_carries_its_reason() -> None:
- queue = _build(limit=0)
+def test_every_open_refactoring_opportunity_is_judged() -> None:
+ judged: dict = {}
+ queue = _build(limit=0, judged=judged)
assert queue.items == ()
- assert queue.refactoring_reasons == {
+ assert {k: j.reason for k, j in judged.items()} == {
"refop2_core": None,
"refop2_small": "below_min_worth",
"refop2_test": "test",
}
- # Off the wire: the reasons are a read-model input, not part of the queue.
- assert "refactoring_reasons" not in queue.as_dict()
+ # An eligible plan is valued and tiered as its item is; an excluded one is not.
+ full = _build(limit=None)
+ item = next(i for i in full.items if i.source.opportunity_id == "refop2_core")
+ assert judged["refop2_core"].tier == item.tier and judged["refop2_core"].value is not None
+ assert judged["refop2_small"].value is None and judged["refop2_small"].tier is None
def test_a_finding_item_takes_its_tests_from_the_validate_callback() -> None:
diff --git a/tests/unit/health/test_gated_off.py b/tests/unit/health/test_gated_off.py
index 51a8b9d0a..0795087c5 100644
--- a/tests/unit/health/test_gated_off.py
+++ b/tests/unit/health/test_gated_off.py
@@ -214,10 +214,14 @@ def test_a_gated_function_is_no_item_and_counts_as_dormant() -> None:
def test_a_plan_on_a_gated_function_leaves_the_refactoring_default_scope() -> None:
+ judged: dict = {}
queue = build_fix_first(
- metrics=METRICS[:1], findings=[_fix_finding(gated_off=True)], refactoring=REFACTORING[:1]
+ metrics=METRICS[:1],
+ findings=[_fix_finding(gated_off=True)],
+ refactoring=REFACTORING[:1],
+ judged=judged,
)
- assert queue.refactoring_reasons["refop2_core"] == "gated_off"
+ assert judged["refop2_core"].reason == "gated_off"
# The plan and the finding it leaves behind sit in one function.
assert queue.totals.excluded["gated_off"] == 2 and queue.totals.dormant == 1
@@ -293,12 +297,13 @@ def _dead(**over) -> dict:
],
)
def test_sure_dead_code_makes_its_target_unreachable(dead: dict) -> None:
+ judged: dict = {}
queue = build_fix_first(
metrics=METRICS[:1], findings=[_fix_finding()], refactoring=REFACTORING[:1],
- dead_code=[dead],
+ dead_code=[dead], judged=judged,
)
assert queue.items == ()
- assert queue.refactoring_reasons["refop2_core"] == "unreachable"
+ assert judged["refop2_core"].reason == "unreachable"
assert queue.totals.excluded["unreachable"] == 2 and queue.totals.dormant == 0
diff --git a/tests/unit/health/test_perf_serving.py b/tests/unit/health/test_perf_serving.py
index 49844ae60..7da0eb38a 100644
--- a/tests/unit/health/test_perf_serving.py
+++ b/tests/unit/health/test_perf_serving.py
@@ -60,7 +60,10 @@ def test_parse_query_reports_what_it_discards() -> None:
def test_query_resolves_contexts_and_default_queue() -> None:
assert parse_query(context="all")[0].contexts is None
assert parse_query(context="production_tooling")[0].contexts == {"production", "tooling"}
- assert parse_query()[0].actionabilities == {"plan_ready", "advisory"}
+ # The default queue is the stored judgement; any other context keeps its states.
+ assert parse_query()[0].queue_eligible is True and parse_query()[0].actionabilities is None
+ assert parse_query(context="all")[0].actionabilities == {"plan_ready", "advisory"}
+ assert parse_query(context="all")[0].queue_eligible is None
assert parse_query(actionability="expected")[0].actionabilities == {"expected"}
assert parse_query(boundary="none")[0].boundary == "none"
@@ -175,6 +178,12 @@ def _seed_rows(repository_id: str) -> list[Any]:
status=seed.get("status", "open"),
rank_position=i,
details_json="{}",
+ # The default queue's rule, as the index judges and stores it.
+ queue_eligible=(
+ seed["execution_context"] == "production"
+ and seed["actionability_state"] in ("plan_ready", "advisory")
+ and seed["cost_proof"] == "proven"
+ ),
**{k: v for k, v in seed.items() if k != "status"},
)
for i, seed in enumerate(_SEEDS)
@@ -185,7 +194,7 @@ def _as_mapping(row: Any) -> dict[str, Any]:
return {
column: getattr(row, column)
for column in (
- "opportunity_id", "status", "rank_position", *_SEEDS[0],
+ "opportunity_id", "status", "rank_position", "queue_eligible", *_SEEDS[0],
)
}
@@ -235,6 +244,7 @@ async def test_keep_and_sort_agree_with_the_store(store, args, sort) -> None:
actionabilities=query.actionabilities,
proofs=query.proofs,
roles=query.roles,
+ queue_eligible=query.queue_eligible,
file_paths=query.file_paths,
sort=query.sort,
limit=query.limit,
@@ -292,3 +302,18 @@ async def test_facet_counts_agree_with_the_store(store, args) -> None:
assert expected == _reference_facets(grouped, query)
for rows in _both_shapes(repository_id):
assert facet_counts(rows, query) == expected
+
+
+async def test_an_unjudged_store_reads_the_default_queue_rule_live(store) -> None:
+ from sqlalchemy import update
+
+ from repowise.core.persistence.models import PerformanceOpportunity
+ from repowise.server.services.performance_health import PerformanceHealthService
+
+ session, repository_id = store
+ service = PerformanceHealthService(session, repository_id, "repo")
+ judged = [i["opportunity_id"] for i in (await service.page(parse_query()[0])).items]
+ await session.execute(update(PerformanceOpportunity).values(queue_eligible=None))
+ await session.commit()
+ live = [i["opportunity_id"] for i in (await service.page(parse_query()[0])).items]
+ assert live == judged == ["perf_0"]
diff --git a/tests/unit/health/test_queue_counts.py b/tests/unit/health/test_queue_counts.py
new file mode 100644
index 000000000..b75f68a24
--- /dev/null
+++ b/tests/unit/health/test_queue_counts.py
@@ -0,0 +1,120 @@
+"""The count vocabulary, the judgements the index stores, and held-back steps."""
+
+from __future__ import annotations
+
+import copy
+
+from repowise.core.analysis.health.fix_first import build_fix_first
+from repowise.core.analysis.health.fix_first.build import judge_findings
+from repowise.core.analysis.health.queue.counts import (
+ COVERED_BY_PLAN,
+ NOT_JUDGED,
+ counts_of,
+ perf_judgement,
+ queue_counts,
+)
+from repowise.core.analysis.health.queue.eligibility import perf_queue_verdict
+from tests.unit.health.fix_first_rows import (
+ FINDINGS,
+ METRICS,
+ PERFORMANCE,
+ PLANS,
+ REFACTORING,
+ _step,
+)
+
+
+def test_the_levels_add_up_and_scope_reasons_leave_in_scope() -> None:
+ counts = queue_counts(
+ [(None, "now", 2), (None, "later", 3), ("test", None, 4), ("below_min_worth", None, 1),
+ (NOT_JUDGED, None, 1)],
+ shown=2,
+ )
+ assert counts.as_dict() == {
+ "inventory": 11,
+ "in_scope": 7,
+ "eligible": 5,
+ "due": 2,
+ "shown": 2,
+ "excluded": {"test": 4, "below_min_worth": 1, "not_judged": 1},
+ }
+ excluded = {"test": 4, "below_min_worth": 1, "not_judged": 1, "tooling": 0}
+ assert counts_of(5, 2, excluded, 2) == counts
+
+
+def test_every_open_finding_is_judged_as_fix_first_judges_its_file() -> None:
+ findings = [{**f, "id": f["public_id"]} for f in FINDINGS]
+ judged = judge_findings(metrics=METRICS, findings=findings, plans=PLANS)
+ assert judged["finding_t1"].reason == "test"
+ assert judged["finding_h1"].reason == "history_only"
+ assert judged["finding_h2"].reason == "history_only"
+ # Resolved findings are no unit of the queue.
+ assert "finding_r1" not in judged
+ # An eligible finding carries the tier its item would.
+ queue = build_fix_first(metrics=METRICS, findings=FINDINGS, limit=None)
+ item = next(i for i in queue.items if i.target.file_path == "src/plain.py")
+ assert judged["finding_n1"].reason is None and judged["finding_n1"].tier == item.tier
+ # A file whose plan is an item: the plan speaks for its findings.
+ covered = judge_findings(
+ metrics=METRICS, findings=findings, plans=PLANS, planned_files={"src/core.py"}
+ )
+ assert covered["finding_c1"].reason == COVERED_BY_PLAN == covered["finding_h1"].reason
+
+
+def test_only_a_files_lead_finding_is_eligible() -> None:
+ second = {**FINDINGS[2], "public_id": "finding_n2", "function_name": "walk2",
+ "health_impact": 0.2, "line_start": 50, "line_end": 80}
+ findings = [{**f, "id": f["public_id"]} for f in (*FINDINGS, second)]
+ judged = judge_findings(metrics=METRICS, findings=findings, plans=PLANS)
+ assert judged["finding_n1"].reason is None
+ assert judged["finding_n2"].reason == "not_file_lead"
+
+
+def test_a_cause_is_judged_by_the_default_queue_rule() -> None:
+ for row in PERFORMANCE:
+ facets = row["details"]["facets"]
+ judgement = perf_judgement(row, facets, row["details"]["plan"])
+ assert judgement.reason == perf_queue_verdict(row).reason
+ assert (judgement.tier is None) == (judgement.reason is not None)
+
+
+def test_a_causes_verdict_follows_its_execution_role() -> None:
+ """Finalize judges each cause from its row as written, role included, so
+ a restamped role is judged again on the same write."""
+ from repowise.core.analysis.health.queue.eligibility import QUEUE_ROLES
+
+ row = PERFORMANCE[0]
+ hot = sorted(QUEUE_ROLES - {"scheduled_job"})[0]
+ for role, reason in ((hot, None), ("cli", "cold_role")):
+ facets = {**row["details"]["facets"], "execution_role": role}
+ judged = {**row, "execution_role": role, "details": {**row["details"], "facets": facets}}
+ assert perf_judgement(judged, facets, row["details"]["plan"]).reason == reason
+
+
+def test_unaudited_steps_stay_in_the_plan_not_the_item() -> None:
+ """A plan led by an audited kind keeps its opportunity in Fix first, but a
+ Move Method or Extract Class step behind the lead is held back."""
+ refactoring = copy.deepcopy(REFACTORING[:1])
+ steps = refactoring[0]["details"]["steps"]
+ steps.append(_step("refac2_move", "move_method", "run", 0.4, mechanical=False))
+ steps.append(_step("refac2_class", "extract_class", "Core", 0.4, mechanical=False))
+ judged: dict = {}
+ queue = build_fix_first(
+ metrics=METRICS, findings=FINDINGS, refactoring=refactoring, plans=PLANS,
+ limit=None, judged=judged,
+ )
+ item = next(i for i in queue.items if i.kind == "refactor")
+ plain: dict = {}
+ before = build_fix_first(
+ metrics=METRICS, findings=FINDINGS, refactoring=REFACTORING[:1], plans=PLANS,
+ limit=None, judged=plain,
+ )
+ # The opportunity, its id and its verdict are unchanged by the held-back steps.
+ assert item.id == next(i for i in before.items if i.kind == "refactor").id
+ assert judged["refop2_core"] == plain["refop2_core"]
+ assert judged["refop2_core"].reason is None
+ assert item.action.steps_total == 2
+ assert {s.order for s in item.action.steps} == {1, 2}
+ assert ("steps held back", "2 (move or extract-class, not yet audited; in the full plan)") in {
+ (f.label, f.value) for f in item.facts
+ }
diff --git a/tests/unit/health/test_refactoring_serving.py b/tests/unit/health/test_refactoring_serving.py
index 5ec3c5fb6..3362e9254 100644
--- a/tests/unit/health/test_refactoring_serving.py
+++ b/tests/unit/health/test_refactoring_serving.py
@@ -263,6 +263,8 @@ _FILTERS: list[dict[str, Any]] = [
{"opportunity_ids": ["refop_0", "refop_2", "refop_5"]},
{"opportunity_ids": []},
{"lead_types": ["extract_method"], "mechanical_only": True, "path_prefix": "pkg_a/"},
+ {"queue_eligible": True},
+ {"queue_eligible": True, "effort": "S"},
]
@@ -283,6 +285,9 @@ def _seed_rows(repository_id: str) -> list[Any]:
evidence_total=0,
affected_files_total=1,
details_json="{}",
+ # Every other row judged eligible, as the index stores it.
+ queue_eligible=i % 2 == 0,
+ queue_reason=None if i % 2 == 0 else "below_min_worth",
**seed,
)
for i, seed in enumerate(_SEEDS)
@@ -293,7 +298,7 @@ def _as_mapping(row: Any) -> dict[str, Any]:
return {column: getattr(row, column) for column in (
"opportunity_id", "status", "file_path", "lead_refactoring_type", "confidence",
"effort_bucket", "mechanical_steps", "addresses_primary_problem", "rank_position",
- "queue_position", "recoverable_health", "step_count",
+ "queue_position", "recoverable_health", "step_count", "queue_eligible",
)}
@@ -324,7 +329,7 @@ async def store(tmp_path: Path):
async def test_keep_and_sort_agree_with_the_store(store, params, order) -> None:
from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
list_refactoring_opportunities,
- refactoring_opportunity_ids,
+ refactoring_reason_counts,
)
session, repository_id = store
@@ -339,11 +344,9 @@ async def test_keep_and_sort_agree_with_the_store(store, params, order) -> None:
)
assert [field(row, "opportunity_id") for row in kept] == expected
assert total == len(expected)
- unpaged = await refactoring_opportunity_ids(
- session, repository_id, **{k: v for k, v in params.items() if k != "opportunity_ids"}
- )
- if "opportunity_ids" not in params:
- assert sorted(unpaged) == sorted(expected)
+ by_reason = await refactoring_reason_counts(session, repository_id, **params)
+ assert sum(by_reason.values()) == len(expected)
+ assert by_reason.get(None, 0) == sum(1 for r in stored if r.queue_eligible)
def test_keep_reads_a_parsed_query() -> None:
@@ -353,19 +356,41 @@ def test_keep_reads_a_parsed_query() -> None:
@pytest.mark.parametrize("status", ["open", "acknowledged", "resolved"])
[email protected]("ids", [None, ["refop_0", "refop_4"], []])
-async def test_facet_counts_agree_with_the_store(store, status, ids) -> None:
+ "params",
+ [
+ {},
+ {"opportunity_ids": ["refop_0", "refop_4"]},
+ {"opportunity_ids": []},
+ {"queue_eligible": True},
+ {"path_contains": "mod", "effort": "S"},
+ {"lead_types": ["extract_method"], "mechanical_only": True},
+ ],
+)
+async def test_facet_counts_agree_with_the_store(store, status, params) -> None:
from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
refactoring_facet_counts,
)
session, repository_id = store
- expected = await refactoring_facet_counts(
- session, repository_id, status=status, opportunity_ids=ids
- )
- assert facet_counts(_seed_rows(repository_id), status=status, opportunity_ids=ids) == expected
+ expected = await refactoring_facet_counts(session, repository_id, status=status, **params)
+ assert facet_counts(_seed_rows(repository_id), status=status, **params) == expected
assert (
- facet_counts([_as_mapping(r) for r in _seed_rows(repository_id)], status=status,
- opportunity_ids=ids)
+ facet_counts([_as_mapping(r) for r in _seed_rows(repository_id)], status=status, **params)
== expected
)
+
+
+async def test_facets_follow_every_filter_but_their_own(store) -> None:
+ """A search narrows every facet; a chosen type keeps the other types' counts."""
+ from repowise.core.persistence.crud.analysis.refactoring_opportunities import (
+ refactoring_facet_counts,
+ )
+
+ session, repository_id = store
+ facets = await refactoring_facet_counts(
+ session, repository_id, path_contains="other/", lead_types=["extract_method"]
+ )
+ # Only other/v.py is open under other/; its type counts though it is the one chosen.
+ assert facets["lead_type"] == {"extract_method": 1}
+ assert facets["effort"] == {"S": 1}
diff --git a/tests/unit/persistence/test_read_snapshots.py b/tests/unit/persistence/test_read_snapshots.py
index fdab1bb40..c03fa8c7f 100644
--- a/tests/unit/persistence/test_read_snapshots.py
+++ b/tests/unit/persistence/test_read_snapshots.py
@@ -41,11 +41,11 @@ def _wire(value) -> str:
return json.dumps(value, sort_keys=False, default=str)
-async def _live(session, rid: str) -> tuple[str, str, dict]:
+async def _live(session, rid: str) -> tuple[str, str]:
clear_fix_first_cache()
view = await load_actions_view(session, rid, now=NOW)
queue = await load_fix_first(session, rid, limit=None)
- return _wire(view), _wire(queue.as_dict()), dict(queue.refactoring_reasons)
+ return _wire(view), _wire(queue.as_dict())
async def _snapshot_rows(session) -> int:
@@ -71,7 +71,7 @@ async def test_persist_writes_both_snapshots_once(async_session) -> None:
rid = await _seed(async_session)
await write_read_snapshots(async_session, rid)
kinds = (await async_session.execute(select(ReadSnapshot.kind))).scalars().all()
- assert sorted(kinds) == ["actions", "fix_first"]
+ assert sorted(kinds) == ["actions", "fix_first", "fix_first_counts"]
# Nothing moved since, so a second pass writes nothing.
assert not await fix_first_loader.write_fix_first_snapshot(async_session, rid)
assert not await actions_loader.write_actions_snapshot(async_session, rid)
@@ -81,14 +81,13 @@ async def test_a_fresh_snapshot_is_served_and_equals_a_live_build(
async_session, no_live_build
) -> None:
rid = await _seed(async_session)
- view, queue, reasons = await _live(async_session, rid)
+ view, queue = await _live(async_session, rid)
await write_read_snapshots(async_session, rid)
await async_session.commit()
no_live_build()
served = await load_fix_first(async_session, rid, limit=None)
assert _wire(served.as_dict()) == queue
- assert served.refactoring_reasons == reasons
assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view
# Every limit and id is a slice of the stored queue.
lead = served.items[0]
@@ -151,12 +150,12 @@ async def test_a_write_to_an_input_drops_the_snapshots(async_session, change) ->
rid = await _seed(async_session)
await write_read_snapshots(async_session, rid)
await async_session.commit()
- assert await _snapshot_rows(async_session) == 2
+ assert await _snapshot_rows(async_session) == 3
await change(async_session, rid)
await async_session.commit()
assert await _snapshot_rows(async_session) == 0
- view, queue, _ = await _live(async_session, rid)
+ view, queue = await _live(async_session, rid)
assert _wire((await load_fix_first(async_session, rid, limit=None)).as_dict()) == queue
assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view
@@ -167,7 +166,7 @@ async def test_a_dropped_snapshot_would_have_been_wrong(async_session) -> None:
await _triage(async_session, rid)
await _new_secret(async_session, rid)
await async_session.commit()
- view, queue, _ = await _live(async_session, rid)
+ view, queue = await _live(async_session, rid)
assert view != before[0] and queue != before[1]
@@ -181,7 +180,7 @@ async def test_writes_elsewhere_keep_the_snapshots(async_session) -> None:
async with async_session.begin_nested():
await async_session.execute(select(HealthFinding.id)) # a read in a savepoint
await async_session.commit()
- assert await _snapshot_rows(async_session) == 2
+ assert await _snapshot_rows(async_session) == 3
async def test_writes_before_the_snapshot_in_one_session_keep_it(async_session) -> None:
@@ -189,7 +188,7 @@ async def test_writes_before_the_snapshot_in_one_session_keep_it(async_session)
await _new_secret(async_session, rid)
await write_read_snapshots(async_session, rid)
await async_session.commit()
- assert await _snapshot_rows(async_session) == 2
+ assert await _snapshot_rows(async_session) == 3
async def _stored_payload(session, kind: str) -> dict:
@@ -210,7 +209,7 @@ async def _replace_payload(session, kind: str, payload: dict) -> None:
@pytest.mark.parametrize("kind", ["fix_first", "actions"])
async def test_a_payload_an_older_build_wrote_is_a_miss(async_session, kind) -> None:
rid = await _seed(async_session)
- view, queue, _ = await _live(async_session, rid)
+ view, queue = await _live(async_session, rid)
await write_read_snapshots(async_session, rid)
payload = await _stored_payload(async_session, kind)
if kind == "fix_first":
@@ -248,7 +247,7 @@ def test_decode_inverts_asdict_over_every_item_kind() -> None:
performance=PERFORMANCE, plans=PLANS, limit=None,
validate=lambda *_: profile)
assert {i.kind for i in queue.items} == set(FIX_KINDS)
- assert any(i.verify.tests for i in queue.items) and queue.refactoring_reasons
+ assert any(i.verify.tests for i in queue.items)
stored = json.loads(json.dumps(asdict(queue)))
assert decode_or_none(FixFirstQueue, stored) == queue
@@ -283,8 +282,8 @@ async def test_a_rewrite_in_the_writing_session_replaces_a_current_row(async_ses
await _triage(async_session, rid) # an input write, not yet committed
await write_read_snapshots(async_session, rid)
await async_session.commit()
- assert await _snapshot_rows(async_session) == 2
- view, queue, _ = await _live(async_session, rid)
+ assert await _snapshot_rows(async_session) == 3
+ view, queue = await _live(async_session, rid)
clear_fix_first_cache()
stored = await load_fix_first(async_session, rid, limit=None)
assert _wire(stored.as_dict()) == queue
@@ -328,12 +327,12 @@ async def test_one_sessions_write_does_not_mark_another(async_session, session_f
await reader.execute(select(HealthFinding.id))
await reader.commit()
await other.rollback()
- assert await _snapshot_rows(async_session) == 2
+ assert await _snapshot_rows(async_session) == 3
async def test_a_corrupt_payload_is_a_miss(async_session) -> None:
rid = await _seed(async_session)
- view, queue, _ = await _live(async_session, rid)
+ view, queue = await _live(async_session, rid)
await write_read_snapshots(async_session, rid)
for kind in ("fix_first", "actions"):
row = await async_session.get(ReadSnapshot, (rid, kind))
diff --git a/tests/unit/server/mcp/test_health.py b/tests/unit/server/mcp/test_health.py
index c1b871657..8b5241c6c 100644
--- a/tests/unit/server/mcp/test_health.py
+++ b/tests/unit/server/mcp/test_health.py
@@ -1041,6 +1041,7 @@ async def test_include_names_work_as_only_aliases(setup_mcp, health_data, alias,
"refactoring_plans_status",
"refactoring_plans_scope",
"refactoring_plans_hidden",
+ "refactoring_plans_counts",
"refactoring_plans_opportunities_total",
}
assert set(result) - {"mode", "targets", "_meta"} <= allowed
@@ -1667,7 +1668,7 @@ def test_fix_first_counts_split_scope_from_worth():
excluded = dict.fromkeys(("test", "tooling", "below_min_worth", "vendored"), 0)
excluded.update(test=4, tooling=1, below_min_worth=3)
queue = FixFirstQueue(totals=FixTotals(candidates=10, eligible=2, shown=2, excluded=excluded))
- assert queue.counts(shown=1) == {
+ assert queue.counts(shown=1).as_dict() == {
"inventory": 10,
"in_scope": 5,
"eligible": 2,
diff --git a/tests/unit/server/test_refactoring_opportunities.py b/tests/unit/server/test_refactoring_opportunities.py
index 60a50e590..80a5a68fd 100644
--- a/tests/unit/server/test_refactoring_opportunities.py
+++ b/tests/unit/server/test_refactoring_opportunities.py
@@ -79,6 +79,14 @@ def _finding(
}
+async def _finalize(session: Any, repo_id: str, **kwargs: Any) -> None:
+ """Compose the opportunities, then judge them, as an index run does."""
+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots
+
+ await crud.finalize_refactoring_opportunities(session, repo_id, **kwargs)
+ await write_read_snapshots(session, repo_id)
+
+
async def _seed(client: AsyncClient, app, *, files: int = 8) -> str:
"""A repository whose plans compose into one opportunity per file."""
repo_id = await _repo(client)
@@ -94,7 +102,7 @@ async def _seed(client: AsyncClient, app, *, files: int = 8) -> str:
repo_id,
[_plan(path, f"sym{i}", impact_delta=float(files - i)) for i, path in enumerate(paths)],
)
- await crud.finalize_refactoring_opportunities(session, repo_id, analyzed_commit="c" * 40)
+ await _finalize(session, repo_id, analyzed_commit="c" * 40)
await session.commit()
return repo_id
@@ -140,7 +148,7 @@ async def test_addresses_primary_problem_is_unknown_without_a_lead(client, app):
repo_id = await _repo(client)
async with app.state.session_factory() as session:
await crud.save_refactoring_suggestions(session, repo_id, [_plan("pkg/x.py", "s")])
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
body = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
assert body["items"][0]["addresses_primary_problem"] is None
@@ -154,7 +162,7 @@ async def test_an_opportunity_nobody_composes_resolves_rather_than_vanishing(cli
async with app.state.session_factory() as session:
# A run that detects nothing resolves every plan, so nothing composes.
await crud.save_refactoring_suggestions(session, repo_id, [])
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
after = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
assert after["total"] == 0
@@ -177,7 +185,7 @@ async def test_lifecycle_rolls_up_from_the_member_plans(client, app):
plan_id = detail["steps"][0]["plan_id"]
async with app.state.session_factory() as session:
await crud.update_refactoring_suggestion_status(session, repo_id, plan_id, "acknowledged")
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
refreshed = (
await client.get(
@@ -247,7 +255,7 @@ async def test_a_model_bump_retires_the_older_models_open_opportunities(client,
Its plans were already resolved by the bump, so an open opportunity folded
from them names work nobody can act on, and it doubled every count.
"""
- from repowise.core.persistence.crud.analysis.fix_first import load_fix_first
+ from repowise.core.persistence.crud.analysis.queue_counts import unit_counts
repo_id = await _seed(client, app, files=3)
open_old, dismissed_old, picked_up_old = await _seed_older_model(
@@ -261,14 +269,15 @@ async def test_a_model_bump_retires_the_older_models_open_opportunities(client,
async with app.state.session_factory() as session:
rows, total = await crud.list_refactoring_opportunities(session, repo_id)
facets = await crud.refactoring_facet_counts(session, repo_id)
- queue = await load_fix_first(session, repo_id, limit=0)
+ counts = await unit_counts(session, repo_id, "plans")
assert total == 3
assert not {row.opportunity_id for row in rows} & {open_old, picked_up_old}
assert all(sum(counts.values()) == 3 for counts in facets.values())
- assert not set(queue.refactoring_reasons) & {open_old, picked_up_old}
+ # The older model's rows are no plans of the queue's inventory either.
+ assert counts.inventory == 3
async with app.state.session_factory() as session:
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
states = await _states(app, repo_id)
assert states[open_old] == "resolved"
@@ -280,7 +289,7 @@ async def test_a_model_bump_retires_the_older_models_open_opportunities(client,
# A later run with no model change resolves nothing more.
async with app.state.session_factory() as session:
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
assert await _states(app, repo_id) == states
@@ -306,7 +315,7 @@ async def _seed_mixed(client: AsyncClient, app) -> str:
repo_id,
[_plan(p, f"sym{i}", impact_delta=g) for i, (p, g) in enumerate(zip(paths, gains, strict=True))],
)
- await crud.finalize_refactoring_opportunities(session, repo_id, analyzed_commit="c" * 40)
+ await _finalize(session, repo_id, analyzed_commit="c" * 40)
await session.commit()
return repo_id
@@ -327,6 +336,75 @@ async def test_the_default_lists_what_fix_first_takes_and_counts_the_rest(client
assert "hidden" not in everything
+async def test_every_surface_reports_the_same_counts(client, app):
+ """The list, the Code Health overview and the Fix first route carry one
+ count object per unit, read from the judgements the index stored."""
+ repo_id = await _seed_mixed(client, app)
+ get_health = await _mcp(app)
+ page = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
+ assert page["counts"] == {
+ "inventory": 4,
+ "in_scope": 3,
+ "eligible": 2,
+ "due": page["counts"]["due"],
+ "shown": 2,
+ "excluded": {"below_min_worth": 1, "test": 1},
+ }
+ overview = (await client.get(f"/api/repos/{repo_id}/health/overview")).json()
+ by_unit = overview["queue_counts"]
+ assert {**by_unit["plans"], "shown": 2} == page["counts"]
+ fix_first = (await client.get(f"/api/repos/{repo_id}/health/fix-first")).json()
+ assert {**fix_first["counts"], "shown": 0} == by_unit["items"]
+ mcp = await get_health(
+ repo=repo_id, include=["refactoring"], only=["refactoring_plans"], limit=25
+ )
+ assert {**mcp["refactoring_plans_counts"], "shown": 2} == page["counts"]
+
+
+async def test_an_unjudged_store_lists_what_fix_first_takes_live(client, app):
+ """A store whose rows carry no verdict yet (an older store, or a judging
+ write that did not run) still lists Fix first's plans, counted not judged."""
+ from sqlalchemy import update
+
+ from repowise.core.persistence.models import RefactoringOpportunity
+
+ repo_id = await _seed_mixed(client, app)
+ async with app.state.session_factory() as session:
+ await session.execute(
+ update(RefactoringOpportunity).values(queue_eligible=None, queue_reason=None)
+ )
+ await session.commit()
+ clear_fix_first_cache()
+ body = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
+ assert sorted(i["file_path"] for i in body["items"]) == ["pkg/a.py", "pkg/b.py"]
+ assert body["hidden"] == {"total": 2, "by_reason": {"not_judged": 2}}
+ assert body["counts"]["excluded"] == {"not_judged": 4}
+
+
+async def test_a_stale_verdict_is_judged_again_by_the_next_write(client, app):
+ from sqlalchemy import select, update
+
+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots
+ from repowise.core.persistence.models import RefactoringOpportunity
+
+ repo_id = await _seed_mixed(client, app)
+ o = RefactoringOpportunity
+ async with app.state.session_factory() as session:
+ # An update moved a.py's plan under the worth floor.
+ await session.execute(
+ update(o).where(o.file_path == "pkg/a.py").values(recoverable_health=0.1)
+ )
+ await write_read_snapshots(session, repo_id)
+ await session.commit()
+ row = (await session.execute(select(o).where(o.file_path == "pkg/a.py"))).scalar_one()
+ assert (row.queue_eligible, row.queue_reason) == (False, "below_min_worth")
+ body = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
+ assert [i["file_path"] for i in body["items"]] == ["pkg/b.py"]
+
+
@pytest.mark.asyncio
async def test_a_file_or_a_triaged_status_reads_the_inventory(client, app):
repo_id = await _seed_mixed(client, app)
@@ -592,7 +670,7 @@ async def test_the_rest_rollup_lead_is_clear_not_absent_with_no_opportunities(
):
repo_id = await _repo(client)
async with app.state.session_factory() as session:
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
body = (await client.get(f"/api/repos/{repo_id}/refactoring/summary")).json()
assert body["directive"]["status"] == "clear"
@@ -628,7 +706,7 @@ async def test_the_default_queue_does_not_reproduce_the_ranked_head(client, app)
plans += [_plan("other/a.py", "a", impact_delta=1.0), _plan("third/b.py", "b", impact_delta=1.0)]
async with app.state.session_factory() as session:
await crud.save_refactoring_suggestions(session, repo_id, plans)
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
default = (
@@ -683,7 +761,7 @@ async def test_lead_finding_ids_follow_the_findings_not_their_insert_order(clien
[_finding(path, function_name=name) for name in ("zeta", "alpha", "mid")],
)
await crud.save_refactoring_suggestions(session, repo_id, [_plan(path, "alpha")])
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
by_function = {
row.function_name: row.public_id
@@ -784,7 +862,7 @@ async def test_each_step_keeps_its_own_validation_profile(client, app):
async with app.state.session_factory() as session:
# Inserted lowest-rank-first, so the rank order is the reverse.
await crud.save_refactoring_suggestions(session, repo_id, [local, spanning])
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
body = (await client.get(f"/api/repos/{repo_id}/refactoring/opportunities")).json()
@@ -820,7 +898,7 @@ async def test_rest_and_mcp_agree_under_every_queue_filter(client, app):
]
async with app.state.session_factory() as session:
await crud.save_refactoring_suggestions(session, repo_id, plans)
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
get_health = await _mcp(app)
@@ -1089,7 +1167,7 @@ async def test_a_dismissal_survives_the_next_index(client, app):
json={"status": "false_positive"},
)
async with app.state.session_factory() as session:
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
row = await crud.get_refactoring_opportunity(session, repo_id, oid)
assert row is not None
@@ -1109,7 +1187,7 @@ async def test_an_acknowledgement_survives_the_next_index(client, app):
json={"status": "acknowledged"},
)
async with app.state.session_factory() as session:
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
row = await crud.get_refactoring_opportunity(session, repo_id, oid)
assert row is not None
@@ -1201,7 +1279,7 @@ async def test_withheld_plans_reach_no_list_queue_or_lead(client, app):
plans.append(_plan("svc/core.py", "work", impact_delta=0.5))
async with app.state.session_factory() as session:
await crud.save_refactoring_suggestions(session, repo_id, plans)
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
# Stored all the same: the registry decides visibility, not persistence.
assert len(await crud.get_refactoring_suggestions(session, repo_id)) == 1
assert await crud.count_refactoring_suggestions(session, repo_id) == 1
@@ -1225,7 +1303,7 @@ async def test_a_test_file_is_never_the_lead_or_ahead_of_production(client, app)
]
async with app.state.session_factory() as session:
await crud.save_refactoring_suggestions(session, repo_id, plans)
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
for view in ("diversified", "canonical"):
@@ -1247,7 +1325,7 @@ async def test_the_directive_names_no_test_file_when_only_tests_have_work(client
await crud.save_refactoring_suggestions(
session, repo_id, [_plan("tests/test_core.py", "helper")]
)
- await crud.finalize_refactoring_opportunities(session, repo_id)
+ await _finalize(session, repo_id)
await session.commit()
rollup = (await client.get(f"/api/repos/{repo_id}/refactoring/summary")).json()
directive = rollup["directive"]
diff --git a/tests/unit/server/test_wire_vocabulary_parity.py b/tests/unit/server/test_wire_vocabulary_parity.py
index e95a22e0d..8e91bb857 100644
--- a/tests/unit/server/test_wire_vocabulary_parity.py
+++ b/tests/unit/server/test_wire_vocabulary_parity.py
@@ -196,3 +196,16 @@ def test_next_call_shapes_match_python() -> None:
for name in ("FixItem", "FixItemCompact"):
body = _interface_body(name, "fix-first.ts")
assert re.search(r"^ next_call: ActionCommand;", body, re.MULTILINE), name
+
+
+def test_queue_count_vocabulary_matches_python() -> None:
+ """The count object every queue unit carries, and the reasons it counts."""
+ from dataclasses import fields
+
+ from repowise.core.analysis.health.queue.counts import STORED_REASONS, QueueCounts
+ from repowise.core.analysis.health.queue.eligibility import REASONS
+ from repowise.core.persistence.crud.analysis.queue_counts import UNITS
+
+ assert _union_members("QueueReason", "queue.ts") == {*REASONS, *STORED_REASONS}
+ assert _union_members("QueueUnit", "queue.ts") == set(UNITS)
+ assert _interface_fields("QueueCounts", "queue.ts") == {f.name for f in fields(QueueCounts)}