test_intel_alerts_reconciliation.py
python
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """The four Intel alerts tell one story: Explorer, Alerts strip, panels, detail pages and score for the same revision. |
| 2 | |
| 3 | Regression for the localhost acceptance finding: Explorer said "1 dead", the Dead Code panel "1 candidate" and the Alerts strip |
| 4 | "0 dead candidates" because the strip read the legacy cold-symbol count from the snapshot while the others read the |
| 5 | ``intel.code.dead`` rows. Every alert now counts the rows its detail page lists, qualified by its own provider's freshness. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import re |
| 10 | |
| 11 | import pytest |
| 12 | from sqlalchemy import select, update |
| 13 | from sqlalchemy.ext.asyncio import AsyncSession |
| 14 | |
| 15 | from musehub.db.musehub_intel_models import ( |
| 16 | MusehubIntelBlastRisk, |
| 17 | MusehubIntelBreakageMeta, |
| 18 | MusehubIntelDead, |
| 19 | MusehubSymbolIntel, |
| 20 | ) |
| 21 | from musehub.db.musehub_repo_models import MusehubRepo |
| 22 | from musehub.services.musehub_intel_alerts import AlertCount, HIGH_BLAST_TIERS, load_intel_alerts |
| 23 | from musehub.services.musehub_intel_status import REQUIRED_JOB_TYPES, load_intel_status |
| 24 | from tests.test_intel_health_status import H, OLD, _job, _publish, _repo_with_head |
| 25 | |
| 26 | |
| 27 | # ── seeding ─────────────────────────────────────────────────────────────────── |
| 28 | |
| 29 | async def _dead(db: AsyncSession, rid: str, rows: list[tuple[str, str, bool]], ref: str = H) -> None: |
| 30 | for addr, confidence, dismissed in rows: |
| 31 | db.add(MusehubIntelDead(repo_id=rid, address=addr, kind="function", confidence=confidence, reason="r", ref=ref, dismissed=dismissed)) |
| 32 | await db.commit() |
| 33 | |
| 34 | |
| 35 | async def _symbols(db: AsyncSession, rid: str, rows: list[tuple[str, int, str | None]]) -> None: |
| 36 | for addr, churn, op in rows: |
| 37 | db.add(MusehubSymbolIntel(repo_id=rid, address=addr, churn=churn, churn_30d=churn, op=op, symbol_kind="function")) |
| 38 | await db.commit() |
| 39 | |
| 40 | |
| 41 | async def _blast(db: AsyncSession, rid: str, rows: list[tuple[str, str]]) -> None: |
| 42 | for addr, risk in rows: |
| 43 | db.add(MusehubIntelBlastRisk(repo_id=rid, address=addr, kind="function", risk=risk, risk_score={"high": 80, "medium": 40, "low": 5}[risk], ref=H)) |
| 44 | await db.commit() |
| 45 | |
| 46 | |
| 47 | async def _breakage(db: AsyncSession, rid: str, total: int) -> None: |
| 48 | db.add(MusehubIntelBreakageMeta(repo_id=rid, total_issues=total, warning_count=total, error_count=0, file_count=1 if total else 0, ref=H)) |
| 49 | await db.commit() |
| 50 | |
| 51 | |
| 52 | async def _repo(db: AsyncSession, head: str = H) -> tuple[str, MusehubRepo]: |
| 53 | rid = await _repo_with_head(db, head) |
| 54 | return rid, (await db.execute(select(MusehubRepo).where(MusehubRepo.repo_id == rid))).scalar_one() |
| 55 | |
| 56 | |
| 57 | def _alerts_text(html: str) -> list[str]: |
| 58 | """Visible text of each Alerts row, whitespace collapsed.""" |
| 59 | rows = re.findall(r'<li class="intel-alert[^"]*">(.*?)</li>', html, re.S) |
| 60 | return [re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", r)).strip() for r in rows] |
| 61 | |
| 62 | |
| 63 | def _dead_panel(html: str) -> str: |
| 64 | start = html.index("DEAD CODE") |
| 65 | return re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", html[start:html.index("BLAST RISK", start)])).strip() |
| 66 | |
| 67 | |
| 68 | async def _status_and_alerts(db: AsyncSession, rid: str): |
| 69 | st = (await load_intel_status(db, [rid]))[rid] |
| 70 | return st, await load_intel_alerts(db, rid, st) |
| 71 | |
| 72 | |
| 73 | # ── the reported defect: exactly one dead candidate ─────────────────────────── |
| 74 | |
| 75 | @pytest.mark.asyncio |
| 76 | async def test_one_dead_candidate_is_one_everywhere_and_the_score_is_untouched(client, db_session: AsyncSession) -> None: |
| 77 | rid, repo = await _repo(db_session) |
| 78 | await _publish(db_session, rid, H, score=95) |
| 79 | await _dead(db_session, rid, [("app/shapes.py::smallest", "high", False)]) |
| 80 | |
| 81 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 82 | explore = (await client.get("/explore")).text |
| 83 | detail = (await client.get(f"/{repo.owner}/{repo.slug}/intel/dead")).text |
| 84 | |
| 85 | assert any(t.startswith("✕ 1 dead candidate ") or t.startswith("✕ 1 dead candidate view") for t in _alerts_text(page)), _alerts_text(page) |
| 86 | assert not any(t.startswith("✓ 0 dead") for t in _alerts_text(page)) |
| 87 | assert "1 candidate" in _dead_panel(page) and "1 candidates" not in _dead_panel(page) |
| 88 | assert "1 dead" in re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", explore)) |
| 89 | assert "app/shapes.py::smallest" in detail |
| 90 | # The score's dead input is the cold-symbol share carried in the snapshot, not this count: unchanged, still current, same everywhere. |
| 91 | assert 'aria-label="Health 95 of 100, Excellent, analysis current"' in page |
| 92 | assert 'aria-label="Health 95 of 100, Excellent, analysis current"' in explore |
| 93 | |
| 94 | |
| 95 | @pytest.mark.asyncio |
| 96 | async def test_the_panel_says_it_is_not_proof_of_unreachable_code(client, db_session: AsyncSession) -> None: |
| 97 | rid, repo = await _repo(db_session) |
| 98 | await _publish(db_session, rid, H) |
| 99 | await _dead(db_session, rid, [("a.py::f", "high", False)]) |
| 100 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 101 | assert "Not proof of unreachable code" in page |
| 102 | assert "not proof the code is unreachable or safe to delete" in page |
| 103 | |
| 104 | |
| 105 | @pytest.mark.asyncio |
| 106 | @pytest.mark.parametrize("rows,expected", [ |
| 107 | ([], 0), |
| 108 | ([("a.py::a", "high", False), ("a.py::b", "medium", False)], 2), # every confidence tier counts |
| 109 | ([("a.py::a", "high", False), ("a.py::b", "medium", True)], 1), # dismissed does not |
| 110 | ]) |
| 111 | async def test_dead_count_agrees_between_explorer_alerts_panel_and_detail(client, db_session: AsyncSession, rows, expected: int) -> None: |
| 112 | rid, repo = await _repo(db_session) |
| 113 | await _publish(db_session, rid, H) |
| 114 | await _dead(db_session, rid, rows) |
| 115 | |
| 116 | st, alerts = await _status_and_alerts(db_session, rid) |
| 117 | assert alerts.dead == AlertCount(expected, "current", H) |
| 118 | |
| 119 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 120 | assert f"{expected} dead candidate" in " ".join(_alerts_text(page)) |
| 121 | explore_text = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", (await client.get("/explore")).text)) |
| 122 | assert (f"{expected} dead" in explore_text) is (expected > 0) |
| 123 | detail = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", (await client.get(f"/{repo.owner}/{repo.slug}/intel/dead")).text)) |
| 124 | if expected: |
| 125 | assert f"{expected} candidate" in _dead_panel(page) |
| 126 | assert all(a in detail for a, _, dismissed in rows if not dismissed) # the detail page lists the counted rows |
| 127 | assert not any(a in detail for a, _, dismissed in rows if dismissed) |
| 128 | else: |
| 129 | assert "candidate" not in _dead_panel(page).replace("No change-isolated symbols", "") |
| 130 | |
| 131 | |
| 132 | # ── zero findings vs missing / stale / in-progress / failed ──────────────────── |
| 133 | |
| 134 | @pytest.mark.asyncio |
| 135 | async def test_completed_analysis_with_no_findings_is_a_current_zero_not_missing(client, db_session: AsyncSession) -> None: |
| 136 | rid, repo = await _repo(db_session) |
| 137 | await _publish(db_session, rid, H) |
| 138 | st, alerts = await _status_and_alerts(db_session, rid) |
| 139 | for a in (alerts.hotspots, alerts.dead, alerts.blast, alerts.breakage): |
| 140 | assert a.count == 0 and a.is_current |
| 141 | texts = _alerts_text((await client.get(f"/{repo.owner}/{repo.slug}/intel")).text) |
| 142 | assert texts == ["✓ 0 hotspot symbols", "✓ 0 dead candidates", "✓ 0 high-blast symbols", "✓ 0 breakage issues"] |
| 143 | |
| 144 | |
| 145 | @pytest.mark.asyncio |
| 146 | async def test_never_analysed_has_no_counts_at_all(client, db_session: AsyncSession) -> None: |
| 147 | rid, repo = await _repo(db_session) |
| 148 | st, alerts = await _status_and_alerts(db_session, rid) |
| 149 | assert not alerts.any_available and alerts.dead == AlertCount(None, "never", None) |
| 150 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 151 | assert "intel-alert-list" not in page and "no alert counts to show" in page |
| 152 | |
| 153 | |
| 154 | @pytest.mark.asyncio |
| 155 | async def test_stale_results_from_a_previous_revision_are_labelled_not_presented_as_current(client, db_session: AsyncSession) -> None: |
| 156 | rid, repo = await _repo(db_session) |
| 157 | await _publish(db_session, rid, OLD) # analysed OLD; the head is H and nothing ran for it |
| 158 | await _dead(db_session, rid, [("a.py::f", "high", False)], ref=OLD) |
| 159 | st, alerts = await _status_and_alerts(db_session, rid) |
| 160 | assert (alerts.dead.count, alerts.dead.state, alerts.dead.is_current, alerts.dead.short_ref) == (1, "outdated", False, "bbbbbbbb") |
| 161 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 162 | assert "as of bbbbbbbb" in " ".join(_alerts_text(page)) |
| 163 | assert "alert-list" in page and "intel-alert--stale" in page |
| 164 | |
| 165 | |
| 166 | @pytest.mark.asyncio |
| 167 | async def test_in_progress_provider_with_no_earlier_result_is_analysing_not_zero(client, db_session: AsyncSession) -> None: |
| 168 | rid, repo = await _repo(db_session) |
| 169 | await _publish(db_session, rid, H, only={"intel.code"}) |
| 170 | await _job(db_session, rid, "intel.code.dead", H, "running") |
| 171 | st, alerts = await _status_and_alerts(db_session, rid) |
| 172 | assert alerts.dead == AlertCount(None, "analyzing", None) and alerts.hotspots.is_current |
| 173 | texts = _alerts_text((await client.get(f"/{repo.owner}/{repo.slug}/intel")).text) |
| 174 | assert "– dead candidates: analysing…" in texts and not any("0 dead" in t for t in texts) |
| 175 | |
| 176 | |
| 177 | @pytest.mark.asyncio |
| 178 | async def test_failed_provider_is_failed_not_zero(client, db_session: AsyncSession) -> None: |
| 179 | rid, repo = await _repo(db_session) |
| 180 | await _publish(db_session, rid, H, only={"intel.code"}) |
| 181 | await _job(db_session, rid, "intel.code.blast_risk", H, "failed") |
| 182 | st, alerts = await _status_and_alerts(db_session, rid) |
| 183 | assert alerts.blast == AlertCount(None, "failed", None) |
| 184 | assert "– high-blast symbols: analysis failed" in _alerts_text((await client.get(f"/{repo.owner}/{repo.slug}/intel")).text) |
| 185 | |
| 186 | |
| 187 | @pytest.mark.asyncio |
| 188 | @pytest.mark.parametrize("status,state", [("done", "never"), ("skipped", "not_run"), ("blocked", "failed")]) |
| 189 | async def test_a_finished_job_without_a_published_result_is_never_a_green_zero(client, db_session: AsyncSession, status: str, state: str) -> None: |
| 190 | """Localhost real-repo run: intel.code died, its dependents then ran against an empty index and finished as ``done`` with nothing published. |
| 191 | The Alerts strip showed green "0" for them. Only a published result for the head is an analysis; a job row alone proves nothing.""" |
| 192 | rid, repo = await _repo(db_session) |
| 193 | await _publish(db_session, rid, H, only={"intel.code"}) |
| 194 | for job in ("intel.code.dead", "intel.code.blast_risk", "intel.code.breakage"): |
| 195 | await _job(db_session, rid, job, H, status) |
| 196 | st, alerts = await _status_and_alerts(db_session, rid) |
| 197 | for a in (alerts.dead, alerts.blast, alerts.breakage): |
| 198 | assert (a.count, a.state, a.is_current) == (None, state, False) |
| 199 | assert st.state != "current" |
| 200 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 201 | assert not any(t.startswith("✓") and "0 dead" in t for t in _alerts_text(page)) |
| 202 | assert not any(re.match(r"✓ 0 (dead|high-blast|breakage)", t) for t in _alerts_text(page)), _alerts_text(page) |
| 203 | |
| 204 | |
| 205 | @pytest.mark.asyncio |
| 206 | async def test_a_done_job_for_the_head_does_not_hide_an_older_result_as_current(db_session: AsyncSession) -> None: |
| 207 | rid, _ = await _repo(db_session) |
| 208 | await _publish(db_session, rid, OLD) |
| 209 | for job in REQUIRED_JOB_TYPES: |
| 210 | await _job(db_session, rid, job, H, "done") # finished for H, published nothing for H |
| 211 | st, alerts = await _status_and_alerts(db_session, rid) |
| 212 | assert st.state == "outdated" and not alerts.dead.is_current and alerts.dead.state == "outdated" and alerts.dead.ref == OLD |
| 213 | |
| 214 | |
| 215 | @pytest.mark.asyncio |
| 216 | async def test_feature_branch_jobs_do_not_change_the_default_branch_alerts(db_session: AsyncSession) -> None: |
| 217 | rid, _ = await _repo(db_session) |
| 218 | await _publish(db_session, rid, H) |
| 219 | await _dead(db_session, rid, [("a.py::f", "high", False)]) |
| 220 | for job in REQUIRED_JOB_TYPES: |
| 221 | await _job(db_session, rid, job, "sha256:" + "c" * 64, "running") # a feature-branch tip being analysed |
| 222 | st, alerts = await _status_and_alerts(db_session, rid) |
| 223 | assert st.state == "current" and alerts.dead == AlertCount(1, "current", H) |
| 224 | |
| 225 | |
| 226 | @pytest.mark.asyncio |
| 227 | async def test_new_head_without_jobs_keeps_the_old_count_visible_but_stale(db_session: AsyncSession) -> None: |
| 228 | from musehub.db.musehub_repo_models import MusehubBranch |
| 229 | rid, _ = await _repo(db_session, OLD) |
| 230 | await _publish(db_session, rid, OLD) |
| 231 | await _dead(db_session, rid, [("a.py::f", "high", False), ("a.py::g", "low", False)], ref=OLD) |
| 232 | assert (await _status_and_alerts(db_session, rid))[1].dead == AlertCount(2, "current", OLD) |
| 233 | await db_session.execute(update(MusehubBranch).where(MusehubBranch.repo_id == rid).values(head_commit_id=H)) |
| 234 | await db_session.commit() |
| 235 | alerts = (await _status_and_alerts(db_session, rid))[1] |
| 236 | assert (alerts.dead.count, alerts.dead.is_current) == (2, False) |
| 237 | |
| 238 | |
| 239 | # ── each alert matches its authoritative rows and its detail page ────────────── |
| 240 | |
| 241 | @pytest.mark.asyncio |
| 242 | async def test_hotspots_count_live_symbols_changed_at_least_ten_times(client, db_session: AsyncSession) -> None: |
| 243 | rid, repo = await _repo(db_session) |
| 244 | await _publish(db_session, rid, H) |
| 245 | await _symbols(db_session, rid, [("a.py::hot", 12, "modify"), ("a.py::edge", 10, None), ("a.py::warm", 9, "modify"), ("a.py::gone", 30, "delete")]) |
| 246 | _, alerts = await _status_and_alerts(db_session, rid) |
| 247 | assert alerts.hotspots == AlertCount(2, "current", H) |
| 248 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 249 | assert "2 hotspot symbols" in " ".join(_alerts_text(page)) |
| 250 | page_hot = page[page.index("HOTSPOTS"):page.index("DEAD CODE")] |
| 251 | assert "a.py::hot" in page_hot and "a.py::edge" in page_hot and "a.py::warm" not in page_hot and "a.py::gone" not in page_hot |
| 252 | sub = (await client.get(f"/{repo.owner}/{repo.slug}/intel/hotspots")).text |
| 253 | assert "a.py::hot" in sub and "a.py::edge" in sub and "a.py::warm" not in sub and "a.py::gone" not in sub |
| 254 | assert "12 changes" in sub # all-time change count, the same figure the threshold uses |
| 255 | |
| 256 | |
| 257 | @pytest.mark.asyncio |
| 258 | async def test_a_repo_with_changed_symbols_but_no_hotspots_lists_none(client, db_session: AsyncSession) -> None: |
| 259 | rid, repo = await _repo(db_session) |
| 260 | await _publish(db_session, rid, H) |
| 261 | await _symbols(db_session, rid, [("a.py::f", 3, "modify")]) |
| 262 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 263 | assert "✓ 0 hotspot symbols" in _alerts_text(page) |
| 264 | assert "a.py::f" not in page[page.index("HOTSPOTS"):page.index("DEAD CODE")] |
| 265 | assert "No symbol has changed in 10 or more commits." in (await client.get(f"/{repo.owner}/{repo.slug}/intel/hotspots")).text |
| 266 | |
| 267 | |
| 268 | @pytest.mark.asyncio |
| 269 | async def test_high_blast_counts_the_high_risk_tier_the_page_lists(client, db_session: AsyncSession) -> None: |
| 270 | assert HIGH_BLAST_TIERS == ("high",) |
| 271 | rid, repo = await _repo(db_session) |
| 272 | await _publish(db_session, rid, H) |
| 273 | await _blast(db_session, rid, [("a.py::hi", "high"), ("a.py::hi2", "high"), ("a.py::med", "medium"), ("a.py::lo", "low")]) |
| 274 | _, alerts = await _status_and_alerts(db_session, rid) |
| 275 | assert alerts.blast == AlertCount(2, "current", H) |
| 276 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 277 | assert "2 high-blast symbols" in " ".join(_alerts_text(page)) |
| 278 | panel = page[page.index("BLAST RISK"):page.index("STABLE SYMBOLS")] |
| 279 | assert "a.py::hi" in panel and "a.py::hi2" in panel and "a.py::med" not in panel and "a.py::lo" not in panel |
| 280 | sub = (await client.get(f"/{repo.owner}/{repo.slug}/intel/blast-risk")).text |
| 281 | assert all(a in sub for a in ("a.py::hi", "a.py::hi2", "a.py::med", "a.py::lo")) # the page lists every tier; the alert counts the top one |
| 282 | |
| 283 | |
| 284 | @pytest.mark.asyncio |
| 285 | async def test_medium_and_low_blast_risk_alone_is_zero_high_blast(client, db_session: AsyncSession) -> None: |
| 286 | rid, repo = await _repo(db_session) |
| 287 | await _publish(db_session, rid, H) |
| 288 | await _blast(db_session, rid, [("a.py::m", "medium"), ("a.py::l", "low")]) |
| 289 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 290 | assert "✓ 0 high-blast symbols" in _alerts_text(page) |
| 291 | |
| 292 | |
| 293 | @pytest.mark.asyncio |
| 294 | async def test_breakage_alert_is_the_breakage_provider_total_not_a_hardcoded_zero(client, db_session: AsyncSession) -> None: |
| 295 | rid, repo = await _repo(db_session) |
| 296 | await _publish(db_session, rid, H) |
| 297 | await _breakage(db_session, rid, 4) |
| 298 | _, alerts = await _status_and_alerts(db_session, rid) |
| 299 | assert alerts.breakage == AlertCount(4, "current", H) |
| 300 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 301 | assert "4 breakage issues" in " ".join(_alerts_text(page)) |
| 302 | assert "breaking change" not in " ".join(_alerts_text(page)) # the old label described a number that was never measured |
| 303 | assert ">4</span> issues" in re.sub(r"\s+", " ", page) # the Breakage card shows the same total |
| 304 | |
| 305 | |
| 306 | # ── the score is explained, not moved ───────────────────────────────────────── |
| 307 | |
| 308 | @pytest.mark.asyncio |
| 309 | async def test_help_text_states_which_signals_are_scored(client, db_session: AsyncSession) -> None: |
| 310 | rid, repo = await _repo(db_session) |
| 311 | await _publish(db_session, rid, H) |
| 312 | page = (await client.get(f"/{repo.owner}/{repo.slug}/intel")).text |
| 313 | assert "Dead candidates, blast risk and breakage are listed in Alerts but are not part of the score." in page |
| 314 | assert "Breakage is not yet an input." in page |
| 315 | |
| 316 | |
| 317 | def test_compute_intel_score_inputs_are_unchanged() -> None: |
| 318 | """100 − cold penalty − hotspot penalty − coupling penalty − breakage penalty; breakage receives no input from the indexer.""" |
| 319 | from musehub.services.musehub_intel import compute_intel |
| 320 | from datetime import datetime, timezone |
| 321 | now = datetime(2026, 11, 13, tzinfo=timezone.utc) |
| 322 | history = {f"a.py::s{i}": [{"commit_id": f"c{i}", "op": "insert", "committed_at": now.isoformat()}] for i in range(29)} |
| 323 | snap = compute_intel(history, [], now_utc=now) |
| 324 | assert snap.alert_dead_count == 0 and snap.health_score == 100 - snap.alert_hotspot_count - min(len(snap.coupling_pairs), 25) |
File History
3 commits
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠
1 day ago
sha256:f9cda821d29cd59f4b95c1cf9cd667e1af3a8e40de55f505bcf50fdd0dc52b1d
fix(intel): bounded failure recording, terminal resource-li…
Sonnet 5.5
minor
⚠
3 days ago
sha256:fc14662ba2c2b447114fec4364fa6d022d02f64ce5966bcecccab0458b758ab6
fix(intel): one definition per alert; Explorer, Alerts stri…
Sonnet 5.5
minor
⚠
3 days ago