gabriel / musehub public
test_intel_snapshot_providers.py python
341 lines 18.7 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 six snapshot-reading providers — musehub#8 campaign, Phase C (part 2).
2
3 type · api_surface · languages · codemap · breakage · detect_refactor
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_snapshot_providers.py
9
10 Rules under test:
11
12 C5 A provider analyses EXACTLY the revision it is asked about — never "the newest commit by timestamp across every branch".
13 C6 Objects are read through ``read_object_bytes`` (content cache, s3, mpack byte ranges), not ``backend.get``; an object that cannot be
14 read fails the job instead of being skipped and published as an analysis of less. Delta-only snapshots are reconstructed.
15 C7 A recompute replaces the complete result set.
16 No test below patches the storage backend: files live as ``MusehubObject`` rows whose bytes are only in ``content_cache`` (the
17 shape of an object that has not reached the blob store), which ``backend.get`` cannot find.
18 """
19 from __future__ import annotations
20
21 import hashlib
22 from datetime import datetime, timedelta, timezone
23
24 import msgpack
25 import pytest
26 import pytest_asyncio
27 from sqlalchemy import select
28
29 from musehub.db.musehub_intel_models import (
30 MusehubIntelApiSurface,
31 MusehubIntelBreakageIssue,
32 MusehubIntelCodemapMeta,
33 MusehubIntelCodemapModule,
34 MusehubIntelLanguages,
35 MusehubIntelRefactorEvent,
36 MusehubIntelType,
37 )
38 from musehub.db.musehub_repo_models import MusehubCommit, MusehubCommitRef, MusehubObject, MusehubSnapshot
39 from musehub.services import musehub_intel_providers as P
40 from tests.factories import create_repo
41
42 BASE = datetime(2026, 1, 1, tzinfo=timezone.utc)
43
44
45 def h(seed: str) -> str:
46 return "sha256:" + hashlib.sha256(seed.encode()).hexdigest()
47
48
49 @pytest_asyncio.fixture
50 async def session_factory(db_session, session_factory): # noqa: F811
51 return session_factory
52
53
54 class World:
55 """Commits whose snapshots hold real source files stored only in ``content_cache``."""
56
57 def __init__(self, session, repo_id: str) -> None:
58 self.s, self.repo_id = session, repo_id
59 self.commit_ids: dict[str, str] = {}
60
61 async def obj(self, source: str) -> str:
62 oid = hashlib.sha256(source.encode()).hexdigest()
63 if await self.s.get(MusehubObject, oid) is None:
64 self.s.add(MusehubObject(object_id=oid, path="x", size_bytes=len(source), storage_uri="pending", content_cache=source.encode()))
65 await self.s.flush()
66 return oid
67
68 async def commit(self, name: str, files: dict[str, str], parents: list[str] | None = None, minute: int = 0, *,
69 delta_of: str | None = None) -> str:
70 manifest = {path: await self.obj(src) for path, src in files.items()}
71 snap_id = h(f"snap:{name}")
72 if delta_of is None:
73 self.s.add(MusehubSnapshot(snapshot_id=snap_id, manifest_blob=msgpack.packb(manifest), directories=[], entry_count=len(manifest)))
74 else: # delta-only: no cached manifest, only the change against the parent snapshot
75 base = h(f"snap:{delta_of}")
76 base_row = await self.s.get(MusehubSnapshot, base)
77 base_manifest = msgpack.unpackb(base_row.manifest_blob, raw=False)
78 added = {p: o for p, o in manifest.items() if base_manifest.get(p) != o}
79 removed = [p for p in base_manifest if p not in manifest]
80 self.s.add(MusehubSnapshot(snapshot_id=snap_id, manifest_blob=None, parent_snapshot_id=base,
81 delta_blob=msgpack.packb({"add": added, "rm": removed}), directories=[], entry_count=len(manifest)))
82 await self.s.flush()
83 cid = h(f"commit:{name}")
84 self.s.add(MusehubCommit(commit_id=cid, message=name, author="gabriel", branch="main",
85 parent_ids=[self.commit_ids[p] for p in (parents or [])], snapshot_id=snap_id,
86 timestamp=BASE + timedelta(minutes=minute)))
87 self.s.add(MusehubCommitRef(repo_id=self.repo_id, commit_id=cid))
88 await self.s.flush()
89 self.commit_ids[name] = cid
90 return cid
91
92
93 A_V1 = "def public_fn(x: int) -> int:\n return x\n\n\nclass Thing:\n def method(self):\n return 1\n"
94 B_SRC = ("from pkg.a import public_fn\nimport os\nfrom os import path\nfrom pkg.missing import nothing\n\n\n"
95 "def use():\n return public_fn(1)\n")
96 X_SRC = "def only_on_side():\n return 0\n"
97
98
99 async def world_with_side_branch(s, slug: str) -> tuple[World, str, str]:
100 """main: c1 ─ c2 (analysed) side: c1 ─ side (NEWER than c2 by timestamp, different files)"""
101 repo = await create_repo(s, owner="gabriel", slug=slug)
102 w = World(s, repo.repo_id)
103 await w.commit("c1", {"pkg/a.py": A_V1}, minute=0)
104 c2 = await w.commit("c2", {"pkg/a.py": A_V1, "pkg/b.py": B_SRC}, ["c1"], minute=10)
105 side = await w.commit("side", {"other/x.py": X_SRC}, ["c1"], minute=99)
106 await s.commit()
107 return w, c2, side
108
109
110 # ---------------------------------------------------------------------------
111 # C5 + C6 — exact revision, supported object reads
112 # ---------------------------------------------------------------------------
113
114 @pytest.mark.asyncio
115 async def test_type_and_api_surface_analyse_the_requested_revision_not_the_newest_commit(session_factory) -> None:
116 async with session_factory() as s:
117 w, c2, side = await world_with_side_branch(s, "rev1")
118 await P.TypeProvider().compute(s, w.repo_id, c2, {})
119 await P.ApiSurfaceProvider().compute(s, w.repo_id, c2, {})
120 await s.commit()
121 types = {r.address: r for r in (await s.execute(select(MusehubIntelType).where(MusehubIntelType.repo_id == w.repo_id))).scalars()}
122 api = {r.address: r for r in (await s.execute(select(MusehubIntelApiSurface).where(MusehubIntelApiSurface.repo_id == w.repo_id))).scalars()}
123 assert "pkg/a.py::public_fn" in types and "other/x.py::only_on_side" not in types
124 assert "pkg/a.py::public_fn" in api and "other/x.py::only_on_side" not in api
125 assert {r.ref for r in types.values()} == {c2} == {r.ref for r in api.values()}
126
127
128 @pytest.mark.asyncio
129 async def test_languages_reads_objects_that_only_the_supported_abstraction_can_reach(session_factory) -> None:
130 async with session_factory() as s:
131 w, c2, side = await world_with_side_branch(s, "lang1")
132 out = await P.LanguagesProvider().compute(s, w.repo_id, c2, {})
133 await s.commit()
134 rows = {r.language: r for r in (await s.execute(select(MusehubIntelLanguages).where(MusehubIntelLanguages.repo_id == w.repo_id))).scalars()}
135 py = rows["Python"]
136 assert py.file_count == 2 and py.ref == c2
137 assert py.symbol_count > 0, "the files exist only in content_cache: backend.get() would have found nothing and counted 0 symbols"
138 assert out[0][1]["files_read"] == 2
139
140
141 @pytest.mark.asyncio
142 async def test_codemap_and_breakage_use_the_requested_revision_and_do_not_flag_external_imports(session_factory) -> None:
143 async with session_factory() as s:
144 w, c2, side = await world_with_side_branch(s, "map1")
145 await P.CodemapProvider().compute(s, w.repo_id, c2, {})
146 await P.BreakageProvider().compute(s, w.repo_id, c2, {})
147 await s.commit()
148 mods = {r.file_path: r for r in (await s.execute(select(MusehubIntelCodemapModule).where(MusehubIntelCodemapModule.repo_id == w.repo_id))).scalars()}
149 issues = (await s.execute(select(MusehubIntelBreakageIssue).where(MusehubIntelBreakageIssue.repo_id == w.repo_id))).scalars().all()
150 assert set(mods) == {"pkg/a.py", "pkg/b.py"} # not other/x.py from the newer side commit
151 assert mods["pkg/b.py"].fan_out == 1 and mods["pkg/a.py"].fan_in == 1
152 # 'pkg.missing' is this repository's own package and does not exist: stale. 'os' / 'os.path' are not the repository's: never judged.
153 assert [(i.file_path, "pkg.missing" in i.description) for i in issues] == [("pkg/b.py", True)]
154
155
156 @pytest.mark.asyncio
157 @pytest.mark.parametrize("provider", ["TypeProvider", "ApiSurfaceProvider", "LanguagesProvider", "CodemapProvider", "BreakageProvider", "DetectRefactorProvider"])
158 async def test_an_unknown_revision_is_an_error_never_a_fallback_to_some_other_commit(session_factory, provider) -> None:
159 async with session_factory() as s:
160 w, c2, side = await world_with_side_branch(s, f"unknown-{provider}")
161 with pytest.raises(LookupError):
162 await getattr(P, provider)().compute(s, w.repo_id, h("no-such-commit"), {})
163
164
165 # ---------------------------------------------------------------------------
166 # Unreadable input fails the job and leaves nothing behind
167 # ---------------------------------------------------------------------------
168
169 @pytest.mark.asyncio
170 @pytest.mark.parametrize("provider", ["TypeProvider", "ApiSurfaceProvider", "LanguagesProvider", "CodemapProvider", "BreakageProvider"])
171 async def test_an_unreadable_object_fails_the_job_and_publishes_nothing(session_factory, provider) -> None:
172 async with session_factory() as s:
173 w, c2, side = await world_with_side_branch(s, f"unread-{provider}")
174 # b.py's object disappears (e.g. gc'd, or never reached the store): the manifest still names it
175 victim = await s.get(MusehubObject, hashlib.sha256(B_SRC.encode()).hexdigest())
176 victim.content_cache = None
177 victim.storage_uri = "pending"
178 await s.commit()
179 with pytest.raises(P.UnreadableObjects, match="pkg/b.py"):
180 await getattr(P, provider)().compute(s, w.repo_id, c2, {})
181 await s.rollback()
182 for model in (MusehubIntelType, MusehubIntelApiSurface, MusehubIntelLanguages, MusehubIntelCodemapModule, MusehubIntelBreakageIssue):
183 assert (await s.execute(select(model).where(model.repo_id == w.repo_id))).first() is None
184
185
186 @pytest.mark.asyncio
187 async def test_an_empty_file_is_readable_not_missing(session_factory) -> None:
188 async with session_factory() as s:
189 repo = await create_repo(s, owner="gabriel", slug="empty-file")
190 w = World(s, repo.repo_id)
191 c1 = await w.commit("c1", {"pkg/__init__.py": "", "pkg/a.py": A_V1})
192 await s.commit()
193 out = await P.LanguagesProvider().compute(s, w.repo_id, c1, {})
194 assert out[0][1]["files_read"] == 2
195
196
197 @pytest.mark.asyncio
198 async def test_a_zero_length_object_in_mpack_storage_is_empty_not_unreadable(session_factory, monkeypatch) -> None:
199 """Found on real data: the byte-range read of a 0-byte object inside an mpack returns None, so every repository with an empty
200 ``__init__.py`` failed the type provider. An object whose recorded size is 0 is an empty file, readable by definition."""
201 from unittest.mock import AsyncMock
202 async with session_factory() as s:
203 repo = await create_repo(s, owner="gabriel", slug="zero-length")
204 w = World(s, repo.repo_id)
205 empty = hashlib.sha256(b"").hexdigest()
206 s.add(MusehubObject(object_id=empty, path="pkg/__init__.py", size_bytes=0, storage_uri="mpack://some-mpack", content_cache=None))
207 await s.flush()
208 a = await w.obj(A_V1)
209 snap = h("snap:zero")
210 s.add(MusehubSnapshot(snapshot_id=snap, manifest_blob=msgpack.packb({"pkg/__init__.py": empty, "pkg/a.py": a}), directories=[], entry_count=2))
211 await s.flush()
212 cid = h("commit:zero")
213 s.add(MusehubCommit(commit_id=cid, message="zero", author="gabriel", branch="main", parent_ids=[], snapshot_id=snap, timestamp=BASE))
214 s.add(MusehubCommitRef(repo_id=repo.repo_id, commit_id=cid))
215 await s.commit()
216 real = P_backends_read(monkeypatch)
217 out = await P.TypeProvider().compute(s, w.repo_id, cid, {})
218 assert out and out[0][1]["files_read"] == 2
219
220
221 def P_backends_read(monkeypatch):
222 """Make ``read_object_bytes`` behave like the real storage layer does for a zero-length mpack object (None), and like itself otherwise."""
223 from musehub.storage import backends
224 real = backends.read_object_bytes
225
226 async def read(obj, session=None):
227 if getattr(obj, "size_bytes", None) == 0 and (getattr(obj, "storage_uri", "") or "").startswith("mpack://"):
228 return None
229 return await real(obj, session=session)
230 monkeypatch.setattr(backends, "read_object_bytes", read)
231 return real
232
233
234 @pytest.mark.asyncio
235 async def test_a_delta_only_snapshot_is_reconstructed_not_treated_as_empty(session_factory) -> None:
236 async with session_factory() as s:
237 repo = await create_repo(s, owner="gabriel", slug="delta")
238 w = World(s, repo.repo_id)
239 await w.commit("c1", {"pkg/a.py": A_V1})
240 c2 = await w.commit("c2", {"pkg/a.py": A_V1, "pkg/b.py": B_SRC}, ["c1"], minute=5, delta_of="c1")
241 await s.commit()
242 row = await s.get(MusehubSnapshot, h("snap:c2"))
243 assert row.manifest_blob is None # the cached manifest really is absent
244 await P.CodemapProvider().compute(s, w.repo_id, c2, {})
245 await s.commit()
246 mods = {r.file_path for r in (await s.execute(select(MusehubIntelCodemapModule).where(MusehubIntelCodemapModule.repo_id == w.repo_id))).scalars()}
247 assert mods == {"pkg/a.py", "pkg/b.py"}
248
249
250 # ---------------------------------------------------------------------------
251 # C7 — recompute replaces
252 # ---------------------------------------------------------------------------
253
254 @pytest.mark.asyncio
255 async def test_a_recompute_removes_symbols_files_and_languages_that_no_longer_exist(session_factory) -> None:
256 async with session_factory() as s:
257 repo = await create_repo(s, owner="gabriel", slug="replace")
258 w = World(s, repo.repo_id)
259 big = await w.commit("big", {"pkg/a.py": A_V1, "pkg/b.py": B_SRC, "web/app.ts": "export function f() { return 1 }\n"})
260 small = await w.commit("small", {"pkg/a.py": A_V1}, ["big"], minute=5)
261 await s.commit()
262 for cls in (P.TypeProvider, P.ApiSurfaceProvider, P.LanguagesProvider, P.CodemapProvider, P.BreakageProvider):
263 await cls().compute(s, w.repo_id, big, {})
264 await s.commit()
265 for cls in (P.TypeProvider, P.ApiSurfaceProvider, P.LanguagesProvider, P.CodemapProvider, P.BreakageProvider):
266 await cls().compute(s, w.repo_id, small, {})
267 await s.commit()
268 langs = {r.language for r in (await s.execute(select(MusehubIntelLanguages).where(MusehubIntelLanguages.repo_id == w.repo_id))).scalars()}
269 mods = {r.file_path for r in (await s.execute(select(MusehubIntelCodemapModule).where(MusehubIntelCodemapModule.repo_id == w.repo_id))).scalars()}
270 types = {r.address.split("::")[0] for r in (await s.execute(select(MusehubIntelType).where(MusehubIntelType.repo_id == w.repo_id))).scalars()}
271 issues = (await s.execute(select(MusehubIntelBreakageIssue).where(MusehubIntelBreakageIssue.repo_id == w.repo_id))).scalars().all()
272 meta = (await s.execute(select(MusehubIntelCodemapMeta).where(MusehubIntelCodemapMeta.repo_id == w.repo_id))).scalar_one()
273 assert langs == {"Python"} and mods == {"pkg/a.py"} and types == {"pkg/a.py"}
274 assert issues == [] and meta.total_modules == 1 and meta.ref == small
275
276
277 # ---------------------------------------------------------------------------
278 # detect_refactor
279 # ---------------------------------------------------------------------------
280
281 F_V1 = "def alpha():\n return 1\n\n\ndef beta():\n return 2\n"
282 F_V2 = "def alpha():\n return 1\n\n\ndef beta():\n return 22\n" # beta's body rewritten
283 F_V3 = "def alpha_renamed():\n return 1\n\n\ndef beta():\n return 22\n" # alpha renamed
284
285
286 async def refactor_world(s, slug: str, extra_files: int = 0) -> tuple[World, str, str, str]:
287 repo = await create_repo(s, owner="gabriel", slug=slug)
288 w = World(s, repo.repo_id)
289 fillers = {f"bulk/f{i}.py": f"def f{i}():\n return {i}\n" for i in range(extra_files)}
290 c1 = await w.commit("c1", {"src/m.py": F_V1, **fillers}, minute=0)
291 c2 = await w.commit("c2", {"src/m.py": F_V2, **fillers}, ["c1"], minute=10)
292 c3 = await w.commit("c3", {"src/m.py": F_V3, **fillers}, ["c2"], minute=20)
293 await s.commit()
294 return w, c1, c2, c3
295
296
297 @pytest.mark.asyncio
298 async def test_a_push_of_several_commits_yields_the_events_of_every_one_of_them(session_factory) -> None:
299 async with session_factory() as s:
300 w, c1, c2, c3 = await refactor_world(s, "multi")
301 first = await P.DetectRefactorProvider().compute(s, w.repo_id, c1, {})
302 assert first == [] # root commit: nothing to diff, nothing published
303 # one push brings c2 AND c3
304 out = await P.DetectRefactorProvider().compute(s, w.repo_id, c3, {})
305 await s.commit()
306 events = {(r.commit_id, r.kind, r.address) for r in (await s.execute(select(MusehubIntelRefactorEvent).where(MusehubIntelRefactorEvent.repo_id == w.repo_id))).scalars()}
307 assert ("%s" % c2, "implementation", "src/m.py::beta") in events # c2: beta rewritten
308 assert (c3, "rename", "src/m.py::alpha") in events # c3: alpha renamed
309 assert out[0][1]["commits_analysed"] == 3 - 1 # c2 and c3 (c1 is a root)
310
311
312 @pytest.mark.asyncio
313 async def test_detect_refactor_continues_from_its_own_watermark_and_is_idempotent(session_factory) -> None:
314 from musehub.services.musehub_intel_providers import persist_intel_results
315 async with session_factory() as s:
316 w, c1, c2, c3 = await refactor_world(s, "water")
317 out = await P.DetectRefactorProvider().compute(s, w.repo_id, c2, {})
318 await persist_intel_results(s, w.repo_id, c2, out) # the worker publishes: the watermark is now c2
319 await s.commit()
320 out3 = await P.DetectRefactorProvider().compute(s, w.repo_id, c3, {})
321 assert out3[0][1]["commits_analysed"] == 1 # only c3 is new
322 await persist_intel_results(s, w.repo_id, c3, out3)
323 again = await P.DetectRefactorProvider().compute(s, w.repo_id, c3, {})
324 assert list(again) == [] and again.outcome == "already_current"
325 older = await P.DetectRefactorProvider().compute(s, w.repo_id, c2, {})
326 assert list(older) == [] and older.outcome == "stale"
327
328
329 @pytest.mark.asyncio
330 async def test_detect_refactor_reads_only_the_files_that_changed(session_factory, monkeypatch) -> None:
331 async with session_factory() as s:
332 w, c1, c2, c3 = await refactor_world(s, "cost", extra_files=40) # 41 files; only src/m.py changes per commit
333 reads: list[str] = []
334 real = P._read_source
335
336 async def spy(session, object_id):
337 reads.append(object_id)
338 return await real(session, object_id)
339 monkeypatch.setattr(P, "_read_source", spy)
340 await P.DetectRefactorProvider().compute(s, w.repo_id, c2, {})
341 assert len(reads) == 2, f"expected 1 changed file on each side, read {len(reads)} objects"
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:78744d379faf9e46bfc8a45b2998d385b92f232df4fd33a1d06bfe62efa0b1d2 feat(intel): run-all measures every provider through the pr… Sonnet 5.5 patch 4 days ago
sha256:1607b6d715d03f8bca5443548a64274bf6c306a07cfa709f0cf6db7a43f9ee93 fix(intel): snapshot providers analyse the exact revision a… Sonnet 5.5 minor ⚠ 4 days ago