gabriel / musehub public
test_intel_index_providers.py python
252 lines 12.3 KB
Raw
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3 feat(intel): every grammar on the hub, and every intel job … Opus 5.5 minor ⚠ breaking 1 day ago
1 """Contract for the eight providers that read the ``intel.code`` index — musehub#8 campaign, Phase C (part 1).
2
3 coupling · entangle · dead · blast_risk · stable · velocity · clones · gravity
4
5 Run ONLY this file, against a private Postgres (never the live hub database)::
6
7 TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test \
8 python -P -m pytest tests/test_intel_index_providers.py
9
10 Shared rules (the numbered finding is the campaign's cross-cutting one):
11
12 * C7 A recompute REPLACES the complete result set: a row that no longer qualifies disappears, including when the new input is empty.
13 * Deleted symbols are not live: no provider may report them as dead, risky, stable, entangled or cloned.
14 * C8 "Days" are measured back from the revision being analysed, never from the wall clock.
15 * Each assertion below is derived by hand from the fixture DAG in the comment above it.
16 """
17 from __future__ import annotations
18
19 import json
20
21 import pytest
22 import pytest_asyncio
23 from sqlalchemy import select
24
25 from musehub.db.musehub_intel_models import (
26 MusehubIntelClones,
27 MusehubIntelCoupling,
28 MusehubIntelEntangle,
29 MusehubIntelVelocity,
30 )
31 from musehub.services import musehub_intel_providers as P
32 from tests.factories import create_repo
33 from tests.test_intel_code_correctness import Dag, dele, index_and_persist, ins, rep
34 from tests.test_intel_snapshot_providers import World
35
36
37
38 @pytest_asyncio.fixture
39 async def session_factory(db_session, session_factory): # noqa: F811
40 return session_factory
41
42
43 def dag_one() -> Dag:
44 """r0 inserts a, b, c (a, b, c change together once)
45 r1 replaces a, b (a, b change together again)
46 r2 inserts d (d is alone)
47 r3 deletes c
48 r4 inserts e (alone)
49 r5 replaces e (alone again) <- head of 'before'
50 r6 replaces d and a (d now co-changes with a) <- head of 'after'"""
51 d = Dag()
52 d.add("r0", [], [ins("a.py", "a", "a0"), ins("b.py", "b", "b0"), ins("c.py", "c", "c0")], 0)
53 d.add("r1", ["r0"], [rep("a.py", "a", "a0", "a1"), rep("b.py", "b", "b0", "b1")], 10)
54 d.add("r2", ["r1"], [ins("d.py", "d", "d0")], 20)
55 d.add("r3", ["r2"], [dele("c.py", "c", "c0")], 30)
56 d.add("r4", ["r3"], [ins("e.py", "e", "e0")], 40)
57 d.add("r5", ["r4"], [rep("e.py", "e", "e0", "e1")], 50)
58 d.add("r6", ["r5"], [rep("d.py", "d", "d0", "d1"), rep("a.py", "a", "a1", "a2")], 60)
59 return d
60
61
62 ALL1 = ["r0", "r1", "r2", "r3", "r4", "r5", "r6"]
63
64
65 async def prepared(session_factory, dag: Dag, names: list[str], head: str, slug: str):
66 async with session_factory() as s:
67 repo = await create_repo(s, owner="gabriel", slug=slug)
68 await dag.install(s, repo.repo_id, names)
69 await index_and_persist(s, repo.repo_id, dag.commits_id(head))
70 await s.commit()
71 return repo.repo_id
72
73
74 async def rows(s, model, repo_id: str):
75 return list((await s.execute(select(model).where(model.repo_id == repo_id))).scalars())
76
77
78 # ---------------------------------------------------------------------------
79 # stable
80 # ---------------------------------------------------------------------------
81
82 async def _no_recorded_changes(s, repo_id: str) -> None:
83 """A history with no recorded symbol operation: no structured delta (read first, muse#251) and no history row."""
84 from sqlalchemy import delete, update
85 from musehub.db.musehub_intel_models import MusehubSymbolHistoryEntry
86 from musehub.db.musehub_repo_models import MusehubCommit, MusehubCommitRef
87
88 commits = select(MusehubCommitRef.commit_id).where(MusehubCommitRef.repo_id == repo_id)
89 await s.execute(update(MusehubCommit).where(MusehubCommit.commit_id.in_(commits)).values(structured_delta=None))
90 await s.execute(delete(MusehubSymbolHistoryEntry).where(MusehubSymbolHistoryEntry.repo_id == repo_id))
91
92
93 # ---------------------------------------------------------------------------
94 # velocity
95 # ---------------------------------------------------------------------------
96
97 @pytest.mark.asyncio
98 async def test_velocity_counts_real_insert_operations_as_additions(session_factory) -> None:
99 dag = dag_one()
100 repo_id = await prepared(session_factory, dag, ALL1, "r5", "vel1")
101 async with session_factory() as s:
102 await P.VelocityProvider().compute(s, repo_id, dag.commits_id("r5"), {})
103 got = {r.module: r for r in await rows(s, MusehubIntelVelocity, repo_id)}
104 # every file sits at the repository root: one module, "(root)" (``muse code velocity``'s name for it).
105 # r0 inserts a, b, c; r1 replaces a, b; r2 inserts d; r3 deletes c; r4 inserts e; r5 replaces e — all in the current window.
106 root = got["(root)"]
107 assert (root.added, root.removed, root.modified, root.net, root.active_commits) == (5, 1, 3, 4, 6)
108
109
110 @pytest.mark.asyncio
111 async def test_velocity_clears_stale_rows_when_nothing_is_left_to_report(session_factory) -> None:
112 dag = dag_one()
113 repo_id = await prepared(session_factory, dag, ALL1, "r5", "vel2")
114 async with session_factory() as s:
115 await P.VelocityProvider().compute(s, repo_id, dag.commits_id("r5"), {})
116 assert await rows(s, MusehubIntelVelocity, repo_id)
117 # a revision whose whole history has no recorded symbol operation: the old leaderboard must not survive
118 await _no_recorded_changes(s, repo_id)
119 out = await P.VelocityProvider().compute(s, repo_id, dag.commits_id("r5"), {})
120 await s.commit()
121 assert out == [("intel.code.velocity", {"count": 0, "commits_analysed": 6, "truncated": False, "walk_truncated": False})]
122 assert await rows(s, MusehubIntelVelocity, repo_id) == []
123
124
125 # ---------------------------------------------------------------------------
126 # coupling
127 # ---------------------------------------------------------------------------
128
129 @pytest.mark.asyncio
130 async def test_coupling_pairs_are_co_changed_files_and_stale_rows_are_cleared(session_factory) -> None:
131 dag = dag_one()
132 repo_id = await prepared(session_factory, dag, ALL1, "r5", "coup1")
133 async with session_factory() as s:
134 await P.CouplingProvider().compute(s, repo_id, dag.commits_id("r5"), {})
135 got = {(r.file_a, r.file_b): r.co_changes for r in await rows(s, MusehubIntelCoupling, repo_id)}
136 assert got == {("a.py", "b.py"): 2} # r0 and r1; every other pair co-changed once (< minimum of 2)
137 await _no_recorded_changes(s, repo_id)
138 assert await P.CouplingProvider().compute(s, repo_id, dag.commits_id("r5"), {}) == [
139 ("intel.code.coupling", {"count": 0, "commits_analysed": 6, "truncated": False, "walk_truncated": False})]
140 await s.commit()
141 assert await rows(s, MusehubIntelCoupling, repo_id) == []
142
143
144 # ---------------------------------------------------------------------------
145 # entangle
146 # ---------------------------------------------------------------------------
147
148 def test_a_symbol_is_in_a_test_by_path_segment_not_by_substring() -> None:
149 from muse.plugins.code._query import is_test_file as f # one predicate for the CLI and the hub (muse#251)
150
151 assert f("tests/test_x.py") and f("pkg/tests/helpers.py") and f("src/foo_test.py") and f("web/app.test.ts") and f("test_y.py")
152 assert not f("src/latest.py") and not f("contest/solver.py") and not f("pkg/attestation.py") and not f("src/protest.py")
153
154
155 def brute_force_pairs(commit_syms: dict[str, set[str]], max_per_commit: int, min_co: int) -> dict[tuple[str, str], tuple[int, float]]:
156 """Every pair of every commit, counted the slow way: the ``muse code entangle`` definition (rate = co / min activity)."""
157 contributing = {c: syms for c, syms in commit_syms.items() if len(syms) <= max_per_commit}
158 active: dict[str, int] = {}
159 co: dict[tuple[str, str], int] = {}
160 for syms in contributing.values():
161 for s in syms:
162 active[s] = active.get(s, 0) + 1
163 ss = sorted(syms)
164 for i in range(len(ss)):
165 for j in range(i + 1, len(ss)):
166 co[(ss[i], ss[j])] = co.get((ss[i], ss[j]), 0) + 1
167 out = {}
168 for (a, b), n in co.items():
169 if n < min_co or a.split("::")[0] == b.split("::")[0]:
170 continue
171 out[(a, b)] = (n, round(n / min(active[a], active[b]), 4))
172 return out
173
174
175 @pytest.mark.asyncio
176 async def test_entangle_equals_the_brute_force_definition_without_building_every_pair(session_factory) -> None:
177 import random
178 rnd = random.Random(7)
179 files = [f"m{i}.py" for i in range(12)]
180 d = Dag()
181 prev: list[str] = []
182 for i in range(14):
183 picked = rnd.sample(files, rnd.randint(2, 7))
184 d.add(f"k{i}", list(prev), [rep(f, "hot", f"h{i}", f"h{i + 1}") for f in picked], i * 10)
185 prev = [f"k{i}"]
186 # symbols are "<file>::hot" in every file, so the same 12 symbols recur across commits
187 names = [f"k{i}" for i in range(14)]
188 repo_id = await prepared(session_factory, d, names, "k13", "ent1")
189 async with session_factory() as s:
190 from musehub.db.musehub_intel_models import MusehubSymbolHistoryEntry
191 hist = (await s.execute(select(MusehubSymbolHistoryEntry.commit_id, MusehubSymbolHistoryEntry.address)
192 .where(MusehubSymbolHistoryEntry.repo_id == repo_id))).all()
193 cs: dict[str, set[str]] = {}
194 for c, a in hist:
195 cs.setdefault(c, set()).add(a)
196 want = brute_force_pairs(cs, 200, 2)
197 await P.EntangleProvider().compute(s, repo_id, d.commits_id("k13"), {})
198 got = {(r.symbol_a, r.symbol_b): (r.co_changes, r.co_change_rate) for r in await rows(s, MusehubIntelEntangle, repo_id)}
199 top = sorted(want.items(), key=lambda kv: (-kv[1][1], -kv[1][0], kv[0]))[:500]
200 assert got == dict(top)
201
202
203 @pytest.mark.asyncio
204 async def test_entangle_clears_stale_rows(session_factory) -> None:
205 dag = dag_one()
206 repo_id = await prepared(session_factory, dag, ALL1, "r5", "ent2")
207 async with session_factory() as s:
208 s.add(MusehubIntelEntangle(repo_id=repo_id, symbol_a="old.py::x", symbol_b="old2.py::y", co_change_rate=0.9, co_changes=9,
209 commits_both_active=10, structurally_linked=False, ref="stale"))
210 await s.flush()
211 out = await P.EntangleProvider().compute(s, repo_id, dag.commits_id("r5"), {})
212 await s.commit()
213 got = await rows(s, MusehubIntelEntangle, repo_id)
214 assert all(r.symbol_a != "old.py::x" for r in got), "a stale pair from an earlier revision must be replaced"
215 # structurally linked pairs are left out (tests/test_intel_callgraph_parity.py::test_PAR_ENT_01)
216
217
218 # ---------------------------------------------------------------------------
219 # clones
220 # ---------------------------------------------------------------------------
221
222 @pytest.mark.asyncio
223 async def test_clones_drop_a_cluster_when_the_bodies_diverge(session_factory) -> None:
224 same = "def f():\n return 1\n"
225 async with session_factory() as s:
226 repo = await create_repo(s, owner="gabriel", slug="clone1")
227 w = World(s, repo.repo_id)
228 q0 = await w.commit("q0", {"a.py": same, "b.py": same.replace("def f", "def g"), "c.py": "def h():\n return 2\n"})
229 q1 = await w.commit("q1", {"a.py": same, "b.py": "def g():\n return 3\n", "c.py": "def h():\n return 2\n"}, ["q0"], minute=1)
230 await s.commit()
231 await P.ClonesProvider().compute(s, repo.repo_id, q0, {})
232 got = await rows(s, MusehubIntelClones, repo.repo_id)
233 assert [(r.tier, r.member_count) for r in got] == [("exact", 2)]
234 out = await P.ClonesProvider().compute(s, repo.repo_id, q1, {})
235 await s.commit()
236 assert out[0][1]["count"] == 0
237 assert await rows(s, MusehubIntelClones, repo.repo_id) == [], "the clone no longer exists: its cluster must be gone"
238
239
240 @pytest.mark.asyncio
241 async def test_clones_publish_a_bounded_number_of_members_but_report_the_true_count(session_factory) -> None:
242 n = P.ClonesProvider._MAX_MEMBERS + 25
243 async with session_factory() as s:
244 repo = await create_repo(s, owner="gabriel", slug="clone2")
245 w = World(s, repo.repo_id)
246 head = await w.commit("w0", {f"f{i}.py": f"def init{i}():\n pass\n" for i in range(n)})
247 await s.commit()
248 await P.ClonesProvider().compute(s, repo.repo_id, head, {})
249 await s.commit()
250 (row,) = await rows(s, MusehubIntelClones, repo.repo_id)
251 assert row.member_count == n
252 assert len(json.loads(row.members_json)) == P.ClonesProvider._MAX_MEMBERS
File History 4 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
sha256:f0600dcd29191749f77ddd3118c65b8c2444ae7c22dd783bd818463d8533b5b1 fix(intel): a provider that recomputes to nothing leaves ta… Sonnet 5.5 minor ⚠ 4 days ago
sha256:77a57be080d5f88fedb9682754705c5b3e02fb96eb13547900ef70f6b47def04 fix(intel): make the eight index-reading providers replace-… Sonnet 5.5 minor ⚠ 4 days ago