Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
"""The writer that turns stored performance findings into a servable queue.
Grouping used to happen on every read, so a page of twenty cost the whole
repository. It happens here instead, once per health persistence transaction,
and both index paths call it. These tests pin what that means: the same answer
from a full and a partial run, plans generated exactly once, a cause that stops
being observed resolved rather than dropped, and a schema that upgrades and
rolls back.
"""
from __future__ import annotations
import json
import sqlite3
from pathlib import Path
import pytest
from sqlalchemy import select
from repowise.core.analysis.health.models import HealthFindingData, Severity
from repowise.core.analysis.health.perf.opportunities import (
PERFORMANCE_MODEL_VERSION,
link_performance_findings,
)
from repowise.core.analysis.health.refactoring.performance_fix import PerformancePlanPolicy
from repowise.core.persistence import crud
from repowise.core.persistence.models import (
HealthFinding,
PerformanceOpportunity,
PerformanceSummary,
RefactoringSuggestion,
)
from tests.unit.persistence.helpers import insert_repo
_SHARED = ["{caller}::run", "src/shared.py::load", "src/db.py::fetch"]
def _finding(path: str, line: int, *, marker: str = "serial_await_in_loop", **details):
return HealthFindingData(
biomarker_type=marker,
severity=Severity.MEDIUM,
file_path=path,
function_name="run",
line_start=line,
line_end=line,
details={
"boundary_kind": "db",
"cross_function": True,
"path": [node.format(caller=path) for node in _SHARED],
"resolution_basis": "call-site",
"dataflow_verified": True,
"loop_magnitude": "grows_with_data",
"execution_role": "request",
**details,
},
health_impact=0.0,
reason="Awaited database work repeats for every loop iteration.",
dimension="performance",
)
def _corpus() -> list:
return [_finding("src/a.py", 10), _finding("src/b.py", 20), _finding("src/c.py", 30)]
async def _write(session, repo_id: str, findings: list, **kwargs) -> int:
link_performance_findings(findings)
await crud.save_health_findings(session, repo_id, findings)
return await crud.finalize_performance_opportunities(session, repo_id, **kwargs)
async def _rows(session, repo_id: str) -> list[PerformanceOpportunity]:
result = await session.execute(
select(PerformanceOpportunity)
.where(PerformanceOpportunity.repository_id == repo_id)
.order_by(PerformanceOpportunity.rank_position)
)
return list(result.scalars().all())
@pytest.mark.asyncio
async def test_one_pass_materializes_the_queue_its_plans_and_its_headline(
async_session,
) -> None:
repo = await insert_repo(async_session)
total = await _write(async_session, repo.id, _corpus(), analyzed_commit="b" * 40)
assert total == 1
(row,) = await _rows(async_session, repo.id)
assert row.status == "open"
assert row.rank_position == 0
assert row.observations_total == 3
assert row.affected_call_sites_total == 3
assert row.intervention_symbol == "src/shared.py::load"
assert row.file_path == "src/shared.py"
assert row.plan_state == "available"
assert row.performance_model_version == PERFORMANCE_MODEL_VERSION
assert row.analyzed_commit == "b" * 40
assert row.cost_proof == "proven"
# Filter and order live in columns; the rest stays in the open payload, and
# nothing appears in both.
details = json.loads(row.details_json)
assert set(details) & {"execution_context", "rank_score", "boundary_kind"} == set()
assert details["facets"]["leverage"] == "local"
plans = await crud.get_refactoring_suggestions(
async_session, repo.id, refactoring_type="performance_fix"
)
assert [plan.opportunity_id for plan in plans] == [row.opportunity_id]
summary = await crud.get_performance_summary(async_session, repo.id)
assert summary.opportunities_total == 1
# An advisory cause is queued but never the lead: only a plan-ready one is.
assert row.actionability_state == "advisory"
assert json.loads(summary.summary_json)["lead"] is None
@pytest.mark.asyncio
async def test_every_stored_observation_carries_its_cause_and_its_public_id(
async_session,
) -> None:
"""Both are columns, which is what makes drill-down an indexed seek."""
repo = await insert_repo(async_session)
await _write(async_session, repo.id, _corpus())
rows = (
(
await async_session.execute(
select(HealthFinding).where(HealthFinding.repository_id == repo.id)
)
)
.scalars()
.all()
)
(opportunity,) = await _rows(async_session, repo.id)
assert {row.opportunity_id for row in rows} == {opportunity.opportunity_id}
assert all(row.public_id and row.public_id.startswith("finding_") for row in rows)
assert len({row.public_id for row in rows}) == 3
@pytest.mark.asyncio
async def test_a_store_written_before_the_columns_existed_is_backfilled(
async_session,
) -> None:
"""The columns are nullable, so an upgraded store arrives with them empty."""
repo = await insert_repo(async_session)
await _write(async_session, repo.id, _corpus())
await async_session.execute(
HealthFinding.__table__.update()
.where(HealthFinding.repository_id == repo.id)
.values(public_id=None, opportunity_id=None)
)
await crud.finalize_performance_opportunities(async_session, repo.id)
rows = (
(
await async_session.execute(
select(HealthFinding).where(HealthFinding.repository_id == repo.id)
)
)
.scalars()
.all()
)
assert all(row.public_id and row.opportunity_id for row in rows)
@pytest.mark.asyncio
async def test_a_partial_run_and_a_full_run_reach_the_same_queue(async_session) -> None:
"""The partial path sees a subset, so it must not build the queue itself.
It writes its own findings and then rebuilds from the merged stored set,
which is the only way the two paths can agree.
"""
full_repo = await insert_repo(async_session, name="full", local_path="/tmp/full")
await _write(async_session, full_repo.id, _corpus())
expected = [
(row.opportunity_id, row.rank_position, row.observations_total, row.plan_state)
for row in await _rows(async_session, full_repo.id)
]
partial_repo = await insert_repo(async_session, name="partial", local_path="/tmp/partial")
first = _corpus()[:1]
link_performance_findings(first)
await crud.save_health_findings(async_session, partial_repo.id, first)
rest = _corpus()[1:]
link_performance_findings(rest)
await crud.upsert_health_findings(
async_session,
partial_repo.id,
rest,
file_paths=["src/b.py", "src/c.py"],
)
await crud.finalize_performance_opportunities(async_session, partial_repo.id)
assert [
(row.opportunity_id, row.rank_position, row.observations_total, row.plan_state)
for row in await _rows(async_session, partial_repo.id)
] == expected
@pytest.mark.asyncio
async def test_a_cause_nobody_observes_is_resolved_and_its_plan_withdrawn(
async_session,
) -> None:
repo = await insert_repo(async_session)
await _write(async_session, repo.id, _corpus())
(before,) = await _rows(async_session, repo.id)
await _write(async_session, repo.id, [_finding("src/z.py", 40, marker="io_in_loop")])
rows = {row.opportunity_id: row for row in await _rows(async_session, repo.id)}
assert rows[before.opportunity_id].status == "resolved"
assert len([row for row in rows.values() if row.status == "open"]) == 1
# The withdrawn cause's plan goes with it: plans are replaced wholesale
# from the current opportunities, never left behind pointing at nothing.
plans = await crud.get_refactoring_suggestions(
async_session, repo.id, refactoring_type="performance_fix"
)
assert before.opportunity_id not in {plan.opportunity_id for plan in plans}
@pytest.mark.asyncio
async def test_a_row_from_an_older_model_is_resolved_rather_than_reused(
async_session,
) -> None:
"""Two model versions disagree about membership, so neither can stand in
for the other. The old row stays addressable and stops reading as current.
"""
repo = await insert_repo(async_session)
await _write(async_session, repo.id, _corpus())
async_session.add(
PerformanceOpportunity(
id="0" * 32,
repository_id=repo.id,
opportunity_id="perf1_0123456789abcdef0123",
performance_model_version=PERFORMANCE_MODEL_VERSION - 1,
status="open",
)
)
await async_session.flush()
await crud.finalize_performance_opportunities(async_session, repo.id)
rows = {row.opportunity_id: row.status for row in await _rows(async_session, repo.id)}
assert rows["perf1_0123456789abcdef0123"] == "resolved"
@pytest.mark.asyncio
async def test_disabling_plans_leaves_the_queue_and_withdraws_the_plans(
async_session,
) -> None:
repo = await insert_repo(async_session)
await _write(
async_session,
repo.id,
_corpus(),
plan_policy=PerformancePlanPolicy(enabled=False),
)
(row,) = await _rows(async_session, repo.id)
assert row.plan_state == "not_persisted"
assert row.fix_strategy == "parallelize_independent_awaits"
assert (
await async_session.execute(
select(RefactoringSuggestion).where(
RefactoringSuggestion.repository_id == repo.id
)
)
).scalars().first() is None
@pytest.mark.asyncio
async def test_a_repository_with_no_performance_findings_still_has_a_headline(
async_session,
) -> None:
"""A missing summary row means "never analyzed"; an empty one means clear.
Collapsing the two would make an index that has not run yet look healthy.
"""
repo = await insert_repo(async_session)
assert await crud.get_performance_summary(async_session, repo.id) is None
await crud.finalize_performance_opportunities(async_session, repo.id)
summary = await crud.get_performance_summary(async_session, repo.id)
assert isinstance(summary, PerformanceSummary)
assert summary.opportunities_total == 0
assert json.loads(summary.summary_json)["lead"] is None
@pytest.mark.asyncio
async def test_a_rebuild_that_fails_halfway_leaves_the_previous_state(
async_session, monkeypatch
) -> None:
"""The queue must never describe findings that were never stored.
The incremental caller logs a failed health step and carries on to commit
the rest of the run, so the savepoint around the write is what stops a
half-finished rebuild from becoming the current answer.
"""
from repowise.core.persistence.crud.analysis import performance as writer
repo = await insert_repo(async_session)
await _write(async_session, repo.id, _corpus(), analyzed_commit="1" * 40)
(before,) = await _rows(async_session, repo.id)
async def explode(*_args, **_kwargs):
raise RuntimeError("summary write failed")
monkeypatch.setattr(writer, "_write_summary", explode)
findings = [_finding("src/z.py", 40, marker="io_in_loop")]
link_performance_findings(findings)
with pytest.raises(RuntimeError):
async with async_session.begin_nested():
await crud.save_health_findings(async_session, repo.id, findings)
await crud.finalize_performance_opportunities(async_session, repo.id)
rows = await _rows(async_session, repo.id)
assert [(row.opportunity_id, row.status) for row in rows] == [
(before.opportunity_id, "open")
]
summary = await crud.get_performance_summary(async_session, repo.id)
assert summary.analyzed_commit == "1" * 40
stored = (
(
await async_session.execute(
select(HealthFinding.file_path).where(
HealthFinding.repository_id == repo.id
)
)
)
.scalars()
.all()
)
assert sorted(stored) == ["src/a.py", "src/b.py", "src/c.py"]
def _tables(db_path: Path) -> set[str]:
conn = sqlite3.connect(db_path)
try:
return {row[0] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")}
finally:
conn.close()
def _columns(db_path: Path, table: str) -> set[str]:
conn = sqlite3.connect(db_path)
try:
return {row[1] for row in conn.execute(f'PRAGMA table_info("{table}")')}
finally:
conn.close()
def test_the_migration_upgrades_and_rolls_back(tmp_path: Path) -> None:
"""Explicit migration for managed stores, both directions.
Local stores are created by ``init_db`` and never see Alembic, which is why
the model declaration and this migration have to describe the same shape.
"""
from alembic import command
from alembic.config import Config
root = Path(__file__).resolve().parents[3] / "packages" / "core"
db_path = tmp_path / "wiki.db"
# Built without the ini file on purpose. Passing it makes ``env.py`` call
# ``fileConfig``, which reconfigures logging for the rest of the session and
# silences the warnings other tests assert on.
config = Config()
config.set_main_option("script_location", str(root / "alembic"))
config.set_main_option("sqlalchemy.url", f"sqlite+aiosqlite:///{db_path}")
command.upgrade(config, "0057")
assert {"performance_opportunities", "performance_summaries"} <= _tables(db_path)
assert {"public_id", "opportunity_id"} <= _columns(db_path, "health_findings")
assert "opportunity_id" in _columns(db_path, "refactoring_suggestions")
command.downgrade(config, "0056")
assert not {"performance_opportunities", "performance_summaries"} & _tables(db_path)
assert not {"public_id", "opportunity_id"} & _columns(db_path, "health_findings")
assert "opportunity_id" not in _columns(db_path, "refactoring_suggestions")
def test_a_non_leading_marker_never_leads_the_summary():
from types import SimpleNamespace
from repowise.core.persistence.crud.analysis.performance import _summary_payload
def _opp(oid: str, marker: str, may_lead: bool):
return SimpleNamespace(
opportunity_id=oid, actionability_state="plan_ready", biomarker_type=marker,
boundary_kind="db", execution_context="production", intervention_symbol=None,
terminal_sink=None, evidence=[{"file_path": "a.py"}], observations_total=1,
affected_call_sites_total=1, affected_files_total=1, why_ranked=[],
prerequisites=(), actionability_reason="", may_lead=may_lead,
facets={
"loop_magnitude": "grows_with_data",
"growth_source": "loop",
"execution_role": "request",
},
)
ranked = [_opp("lazy", "lazy_load_in_loop", False), _opp("io", "io_in_loop", True)]
assert _summary_payload(ranked, {})["lead"]["opportunity_id"] == "io"
assert _summary_payload(ranked[:1], {})["lead"] is None
# A queued cause whose growth only a registry loop shows never leads.
ranked[1].facets = {**ranked[1].facets, "growth_source": "registry"}
assert _summary_payload(ranked, {})["lead"] is None
@pytest.mark.asyncio
async def test_an_unmeasured_loop_is_stored_unproven_and_never_leads(async_session) -> None:
repo = await insert_repo(async_session)
await _write(async_session, repo.id, [_finding("src/a.py", 10, loop_magnitude="unknown")])
(row,) = await _rows(async_session, repo.id)
assert row.cost_proof == "unproven"
assert json.loads(row.details_json)["may_lead"] is False
summary = json.loads((await crud.get_performance_summary(async_session, repo.id)).summary_json)
assert summary["lead"] is None
assert summary["default_queue"]["total"] == 0
assert summary["default_queue"]["excluded"]["unmeasured_cost"] == 1
async def _findings(session, repo_id: str) -> list[HealthFinding]:
result = await session.execute(select(HealthFinding).where(HealthFinding.repository_id == repo_id))
return list(result.scalars().all())
@pytest.mark.asyncio
async def test_every_observation_carries_its_cause_queue_judgement(async_session) -> None:
"""A finding the queue leaves out says why on its own row, not NULL."""
repo = await insert_repo(async_session)
await _write(async_session, repo.id, [_finding("src/a.py", 10, loop_magnitude="unknown")])
(finding,) = await _findings(async_session, repo.id)
assert (finding.queue_eligible, finding.queue_reason) == (False, "unmeasured_cost")
other = await insert_repo(async_session, name="queued", local_path="/tmp/queued")
await _write(async_session, other.id, _corpus())
(cause,) = await _rows(async_session, other.id)
assert cause.queue_eligible is True
for finding in await _findings(async_session, other.id):
assert (finding.queue_eligible, finding.queue_reason, finding.queue_tier) == (
True,
None,
cause.queue_tier,
)
async def _write_with_origin(session, repo_id: str, path: str, origin: str | None) -> None:
from repowise.core.analysis.health.models import HealthFileMetricData
if origin is not None:
metric = HealthFileMetricData(
file_path=path, score=8.0, max_ccn=3, max_nesting=1, nloc=40, has_test_file=False,
code_origin=origin,
)
await crud.save_health_metrics(session, repo_id, [metric])
await _write(session, repo_id, [_finding(path, 10)])
@pytest.mark.asyncio
@pytest.mark.parametrize(
("path", "origin", "context"),
[
# ``example`` is a JVM package segment here, not an examples tree.
("app/src/main/kotlin/com/example/app/Sync.kt", "production", "production"),
("app/src/main/kotlin/com/example/app/Sync.kt", None, "unknown"),
("examples/basic/run.py", "docs_example", "unknown"),
# A root-level file carries no evidence either way, whatever its origin.
("sync.py", "production", "unknown"),
],
)
async def test_a_stored_production_origin_settles_an_unknown_context(
async_session, path: str, origin: str | None, context: str
) -> None:
repo = await insert_repo(async_session)
await _write_with_origin(async_session, repo.id, path, origin)
(cause,) = await _rows(async_session, repo.id)
assert cause.execution_context == context
if context == "unknown":
assert cause.queue_reason == "unknown"
else:
assert cause.queue_eligible is True
@pytest.mark.asyncio
async def test_settling_the_context_keeps_the_cause_id(async_session) -> None:
path = "app/src/main/kotlin/com/example/app/Sync.kt"
settled = await insert_repo(async_session, name="settled", local_path="/tmp/settled")
await _write_with_origin(async_session, settled.id, path, "production")
plain = await insert_repo(async_session, name="plain", local_path="/tmp/plain")
await _write_with_origin(async_session, plain.id, path, None)
(a,) = await _rows(async_session, settled.id)
(b,) = await _rows(async_session, plain.id)
assert a.opportunity_id == b.opportunity_id