test_intel_bounded_providers.py
python
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """Bounded cost and honest truncation for the history-mining providers — musehub#8 release gate (residual cost hotspots). |
| 2 | |
| 3 | * Coupling counted every file pair of every commit into one dict (memory O(commits x files^2)); it now uses the bounded |
| 4 | ``muse.plugins.code.analysis.coupled_files`` (shared with ``muse code coupling``), as entangle does. |
| 5 | * The 10,000-commit walk cap of coupling / entangle / velocity was silent: ``truncated`` described the pairs, not the window. |
| 6 | |
| 7 | Run ONLY this file, against a private Postgres (never the live hub database):: |
| 8 | |
| 9 | TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test python -P -m pytest tests/test_intel_bounded_providers.py |
| 10 | |
| 11 | H1 The bounded coupling algorithm returns exactly what exhaustive counting returned (pairs, order, tie-breaks, truncation), on randomised histories. |
| 12 | H2 It holds no more than a few multiples of ``max_pairs`` candidate pairs at any time, however many files and commits there are. |
| 13 | H3 A walk that hit the cap says so (``walk_truncated``) in coupling, entangle and velocity; one that did not, says it did not. |
| 14 | """ |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import random |
| 18 | from collections import defaultdict |
| 19 | |
| 20 | import pytest |
| 21 | import pytest_asyncio |
| 22 | |
| 23 | from muse.plugins.code.analysis import coupled_files |
| 24 | from musehub.services import musehub_intel_providers as P |
| 25 | from tests.factories import create_repo |
| 26 | from tests.test_intel_snapshot_providers import World |
| 27 | |
| 28 | |
| 29 | @pytest_asyncio.fixture |
| 30 | async def session_factory(db_session, session_factory): # noqa: F811 |
| 31 | return session_factory |
| 32 | |
| 33 | |
| 34 | def exhaustive(commit_files: dict[str, set[str]], *, max_files: int, min_co: int, max_pairs: int) -> list[tuple[str, str, int]]: |
| 35 | counts: dict[tuple[str, str], int] = defaultdict(int) |
| 36 | for files in commit_files.values(): |
| 37 | if len(files) > max_files: |
| 38 | continue |
| 39 | ordered = sorted(files) |
| 40 | for i in range(len(ordered)): |
| 41 | for j in range(i + 1, len(ordered)): |
| 42 | counts[(ordered[i], ordered[j])] += 1 |
| 43 | pairs = sorted(((a, b, n) for (a, b), n in counts.items() if n >= min_co), key=lambda p: (-p[2], p[0], p[1])) |
| 44 | return pairs[:max_pairs] |
| 45 | |
| 46 | |
| 47 | def random_history(rng: random.Random) -> dict[str, set[str]]: |
| 48 | files = [f"f{i:02d}.py" for i in range(rng.randint(3, 40))] |
| 49 | return {f"c{i}": set(rng.sample(files, rng.randint(1, min(len(files), 12)))) for i in range(rng.randint(1, 60))} |
| 50 | |
| 51 | |
| 52 | @pytest.mark.parametrize("seed", range(60)) |
| 53 | def test_h1_the_bounded_algorithm_equals_exhaustive_counting(seed: int) -> None: |
| 54 | rng = random.Random(seed) |
| 55 | history = random_history(rng) |
| 56 | for max_pairs in (1, 3, 200): |
| 57 | want = exhaustive(history, max_files=8, min_co=2, max_pairs=max_pairs) |
| 58 | got = coupled_files(history.values(), max_files_per_commit=8, min_co_changes=2, limit=max_pairs) |
| 59 | assert got == want, (seed, max_pairs) |
| 60 | |
| 61 | |
| 62 | def test_h2_candidate_memory_is_bounded_by_max_pairs_not_by_the_history() -> None: |
| 63 | rng = random.Random(7) |
| 64 | files = [f"m{i:03d}.py" for i in range(150)] |
| 65 | history = {f"c{i}": set(rng.sample(files, 60)) for i in range(120)} # ~1,770 pairs per commit, ~200k distinct pairs |
| 66 | stats = {"peak": 0} |
| 67 | pairs = coupled_files(history.values(), max_files_per_commit=200, min_co_changes=2, limit=20, stats=stats) |
| 68 | assert len(pairs) == 20 |
| 69 | assert stats["peak"] <= 5 * 20 + 150 # 4x the cut plus one file's worth of fresh candidates; the exhaustive dict held ~10,000+ |
| 70 | assert pairs == exhaustive(history, max_files=200, min_co=2, max_pairs=20) |
| 71 | |
| 72 | |
| 73 | @pytest.mark.asyncio |
| 74 | @pytest.mark.parametrize("cls,key", [(P.CouplingProvider, "intel.code.coupling"), (P.EntangleProvider, "intel.code.entangle"), (P.VelocityProvider, "intel.code.velocity")]) |
| 75 | async def test_h3_a_walk_that_hit_the_cap_says_so(session_factory, monkeypatch, cls, key) -> None: |
| 76 | async with session_factory() as s: |
| 77 | repo = await create_repo(s, owner="gabriel", slug=f"walk-{key.rsplit('.', 1)[1]}") |
| 78 | w = World(s, repo.repo_id) |
| 79 | head = None |
| 80 | for i in range(5): |
| 81 | head = await w.commit(f"c{i}", {"m.py": f"x = {i}\n"}, [f"c{i - 1}"] if i else None, minute=i) |
| 82 | await s.commit() |
| 83 | monkeypatch.setattr(cls, "_MAX_WALK", 3) |
| 84 | cut = (await cls().compute(s, w.repo_id, head, {}))[0] |
| 85 | assert cut[0] == key and cut[1]["walk_truncated"] is True and cut[1]["commits_analysed"] == 3 |
| 86 | monkeypatch.setattr(cls, "_MAX_WALK", 5) # exactly the history length: nothing was left out |
| 87 | whole = (await cls().compute(s, w.repo_id, head, {}))[0] |
| 88 | assert whole[1]["walk_truncated"] is False and whole[1]["commits_analysed"] == 5 |
File History
2 commits
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠
1 day ago
sha256:41ca619ffa23246b6e5d0cbb80733d8fec729dd65efe2acf92352370ee550972
fix(intel): bound coupling's pair counting; make the commit…
Sonnet 5.5
minor
⚠
4 days ago