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.

perf(update): cap the per-update commit-health scan at 10 seconds (#4213)

Raghav Chamadiyabf7f05444fc4
diff --git a/packages/cli/src/repowise/cli/commands/upgrade_flow.py b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
index 9aa1ba393..25339290c 100644
--- a/packages/cli/src/repowise/cli/commands/upgrade_flow.py
+++ b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
@@ -187,6 +187,18 @@ async def _backfill_git(
)
+async def _scan_commit_health(sf: Any, repo_id: str, repo_path: Path) -> None:
+ """Per-commit health at the full index's budget: an upgrade is expected to take long."""
+ from repowise.core.pipeline.commit_health import (
+ budget_seconds,
+ refresh_commit_health_after_update,
+ )
+
+ budget = budget_seconds()
+ console.print(f"Scanning commit health (up to {budget:.0f} s)…")
+ await refresh_commit_health_after_update(sf, repo_id, str(repo_path), budget=budget)
+
+
async def _persist_authoritative_pages(
*,
session: Any,
@@ -329,6 +341,7 @@ async def _run_upgrade(
await _recompute_full_tier_health(
engine, sf, repo_id, repo_path, graph_builder, git_meta_map, parsed_files
)
+ await _scan_commit_health(sf, repo_id, repo_path)
if checkpoint:
checkpoint("health")
diff --git a/packages/core/src/repowise/core/pipeline/commit_health.py b/packages/core/src/repowise/core/pipeline/commit_health.py
index 2771c908d..52a86a026 100644
--- a/packages/core/src/repowise/core/pipeline/commit_health.py
+++ b/packages/core/src/repowise/core/pipeline/commit_health.py
@@ -40,12 +40,9 @@ DEFAULT_COMMIT_LIMIT = 150
#: order of magnitude with diff size, so a count alone bounds nothing.
DEFAULT_BUDGET_SECONDS = 300.0
-#: Wall clock one update may spend. An update runs on every commit through the
-#: post-commit hook, so it gets a fifth of the full index's budget. A commit
-#: costs ~5 s on this repository under load (~2 s idle), so this covers about a
-#: dozen commits per run, a normal push in one update, and a longer gap drains
-#: over the next few updates instead of stalling one for five minutes.
-UPDATE_BUDGET_SECONDS = 60.0
+#: Wall clock one update may spend on new commits (~2-5 s each). A re-scan
+#: backlog gets :data:`DEFAULT_BUDGET_SECONDS` instead (see :func:`_rescan_backlog`).
+UPDATE_BUDGET_SECONDS = 10.0
def _env_number(name: str, default: float) -> float:
@@ -92,6 +89,23 @@ async def _pending_shas(
return [sha for sha in shas if sha not in done][:limit]
+async def _rescan_backlog(session: Any, repo_id: str, pending: list[str]) -> bool:
+ """Whether a pending sha already has a row, i.e. an analyzer or rules change re-pended it."""
+ from sqlalchemy import select
+
+ from repowise.core.persistence.models import GitCommitHealthDelta
+
+ row = await session.execute(
+ select(GitCommitHealthDelta.sha)
+ .where(
+ GitCommitHealthDelta.repository_id == repo_id,
+ GitCommitHealthDelta.sha.in_(pending),
+ )
+ .limit(1)
+ )
+ return row.first() is not None
+
+
async def _scan(repo_path: str, pending: list[str], budget: float) -> Any:
from repowise.core.analysis.change_health.commit_scan import scan_commits
@@ -199,13 +213,14 @@ async def refresh_commit_health_after_update(
if not window:
return nothing
pending = await _pending_shas(session, repo_id, repo_path, window, limit)
+ if pending and budget is None:
+ backlog = await _rescan_backlog(session, repo_id, pending)
+ budget = budget_seconds(
+ DEFAULT_BUDGET_SECONDS if backlog else UPDATE_BUDGET_SECONDS
+ )
if not pending:
return nothing
- scan = await _scan(
- repo_path,
- pending,
- budget_seconds(UPDATE_BUDGET_SECONDS) if budget is None else budget,
- )
+ scan = await _scan(repo_path, pending, budget)
if scan.delta_rows:
async with get_session(session_factory) as session:
await upsert_commit_health_bulk(
diff --git a/tests/unit/cli/test_full_upgrade_progress.py b/tests/unit/cli/test_full_upgrade_progress.py
index aeb246dd3..ad540d4cf 100644
--- a/tests/unit/cli/test_full_upgrade_progress.py
+++ b/tests/unit/cli/test_full_upgrade_progress.py
@@ -143,3 +143,21 @@ def test_full_upgrade_restores_parse_only_attrs_after_the_reparse() -> None:
reparse = src.index("_reparse(repo_path")
assert src.index("restore_parse_only_attrs(parsed_files)") > reparse
+
+
+async def test_full_upgrade_scans_commit_health_at_the_full_budget(monkeypatch) -> None:
+ from repowise.core.pipeline import commit_health
+
+ budgets: list[float] = []
+
+ async def refresh(_sf, _repo_id, _path, *, budget=None):
+ budgets.append(budget)
+
+ monkeypatch.delenv("REPOWISE_COMMIT_HEALTH_BUDGET_SECONDS", raising=False)
+ monkeypatch.setattr(commit_health, "refresh_commit_health_after_update", refresh)
+ await upgrade_flow._scan_commit_health(object(), "repo", "/r")
+ monkeypatch.setenv("REPOWISE_COMMIT_HEALTH_BUDGET_SECONDS", "7")
+ await upgrade_flow._scan_commit_health(object(), "repo", "/r")
+
+ assert budgets == [commit_health.DEFAULT_BUDGET_SECONDS, 7.0]
+ assert "_scan_commit_health" in inspect.getsource(upgrade_flow._run_upgrade)
diff --git a/tests/unit/persistence/test_commit_health_after_update.py b/tests/unit/persistence/test_commit_health_after_update.py
index b56f2ff48..5851df10d 100644
--- a/tests/unit/persistence/test_commit_health_after_update.py
+++ b/tests/unit/persistence/test_commit_health_after_update.py
@@ -27,7 +27,12 @@ from repowise.core.persistence import (
init_db,
upsert_repository,
)
-from repowise.core.persistence.crud import get_commit_health, upsert_git_commits_bulk
+from repowise.core.persistence.crud import (
+ delete_commit_health,
+ get_commit_health,
+ upsert_commit_health_bulk,
+ upsert_git_commits_bulk,
+)
from repowise.core.pipeline.commit_health import (
DEFAULT_BUDGET_SECONDS,
UPDATE_BUDGET_SECONDS,
@@ -229,3 +234,34 @@ async def test_update_budget_is_smaller_than_the_full_index(repo, monkeypatch) -
]
finally:
await engine.dispose()
+
+
+async def test_rows_from_an_older_analyzer_get_the_full_budget(repo, monkeypatch) -> None:
+ """A re-pended backlog drains at the full index's pace; new commits keep the short one."""
+ engine = create_engine(f"sqlite+aiosqlite:///{repo / '.repowise' / 'wiki.db'}")
+ await init_db(engine)
+ sf = create_session_factory(engine)
+ try:
+ async with get_session(sf) as session:
+ repo_id = (await insert_repo(session)).id
+ await upsert_git_commits_bulk(session, repo_id, _commit_rows())
+ stale = {**_delta_row(_SHAS[0], str(repo)), "analyzer_version": 0}
+ await upsert_commit_health_bulk(session, repo_id, [stale], [])
+ seen: list = []
+ monkeypatch.setattr(scan_mod, "scan_commits", _scanner(str(repo), 0, seen))
+
+ await refresh_commit_health_after_update(sf, repo_id, str(repo))
+ monkeypatch.setenv("REPOWISE_COMMIT_HEALTH_BUDGET_SECONDS", "7")
+ await refresh_commit_health_after_update(sf, repo_id, str(repo))
+ async with get_session(sf) as session:
+ await delete_commit_health(session, repo_id)
+ monkeypatch.delenv("REPOWISE_COMMIT_HEALTH_BUDGET_SECONDS")
+ await refresh_commit_health_after_update(sf, repo_id, str(repo))
+
+ assert [budget for _shas, budget in seen] == [
+ DEFAULT_BUDGET_SECONDS,
+ 7.0,
+ UPDATE_BUDGET_SECONDS,
+ ]
+ finally:
+ await engine.dispose()