test_intel_code_correctness.py
python
sha256:70cb0ad300bccf1e11f6f0fb15b383c6b5c37a7ee3881ca12c0c0511d0a10de8
fix(intel): the intel.code pre-check looks up what the job …
Sonnet 5.5
patch
4 days ago
| 1 | """Correctness contract for the ``intel.code`` index (``build_symbol_index``) — musehub#8 campaign, Phase A. |
| 2 | |
| 3 | Run ONLY this file, against a private Postgres (never the live hub database):: |
| 4 | |
| 5 | TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test \ |
| 6 | python -P -m pytest tests/test_intel_code_correctness.py |
| 7 | |
| 8 | Every test states one rule of the contract. Fixtures use real Muse op formats (``insert`` / ``replace`` / ``delete`` |
| 9 | with ``content_summary`` / ``new_summary``), and a DAG with a merge, because linear history hides the walk defects. |
| 10 | |
| 11 | The index is a pure function of one revision: **the summary at ``ref`` is exactly what a fresh full build of ``ref`` would |
| 12 | produce**, whatever order the pushes arrived in. Time-windowed metrics are measured back from the revision's own commit |
| 13 | time, never from the wall clock, so re-running the same revision on another day cannot change the answer. |
| 14 | """ |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import hashlib |
| 18 | from datetime import datetime, timedelta, timezone |
| 19 | |
| 20 | import pytest |
| 21 | import pytest_asyncio |
| 22 | from sqlalchemy import select |
| 23 | |
| 24 | from musehub.db.musehub_intel_models import ( |
| 25 | MusehubHashOccurrenceEntry, |
| 26 | MusehubIntelResult, |
| 27 | MusehubSymbolHistoryEntry, |
| 28 | MusehubSymbolIntel, |
| 29 | MusehubSymbolVitals, |
| 30 | ) |
| 31 | from musehub.db.musehub_repo_models import MusehubCommit, MusehubCommitRef |
| 32 | from musehub.services import musehub_symbol_indexer as indexer |
| 33 | from musehub.services.musehub_intel_providers import persist_intel_results |
| 34 | from tests.factories import create_repo |
| 35 | |
| 36 | BASE = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| 37 | |
| 38 | |
| 39 | def cid(seed: str) -> str: |
| 40 | return "sha256:" + hashlib.sha256(seed.encode()).hexdigest() |
| 41 | |
| 42 | |
| 43 | def ins(path: str, name: str, body: str) -> dict: |
| 44 | return {"op": "insert", "address": f"{path}::{name}", "content_id": cid(body), |
| 45 | "content_summary": f"added function {name} L0-5"} |
| 46 | |
| 47 | |
| 48 | def rep(path: str, name: str, old: str, new: str) -> dict: |
| 49 | return {"op": "replace", "address": f"{path}::{name}", "old_content_id": cid(old), "new_content_id": cid(new), |
| 50 | "new_summary": f"function {name} (changed) L0-5"} |
| 51 | |
| 52 | |
| 53 | def dele(path: str, name: str, body: str) -> dict: |
| 54 | return {"op": "delete", "address": f"{path}::{name}", "content_id": cid(body), |
| 55 | "content_summary": f"removed function {name} L0-5"} |
| 56 | |
| 57 | |
| 58 | @pytest_asyncio.fixture |
| 59 | async def session_factory(db_session, session_factory): # noqa: F811 - truncate first, as in test_intel_experiment |
| 60 | return session_factory |
| 61 | |
| 62 | |
| 63 | class Dag: |
| 64 | """A commit DAG shared by several repositories (commits are global, repositories only reference them).""" |
| 65 | |
| 66 | def __init__(self) -> None: |
| 67 | self.commits: dict[str, tuple[list[str], list[dict], datetime]] = {} |
| 68 | self._nodelta: set[str] = set() |
| 69 | |
| 70 | def add(self, name: str, parents: list[str], ops: list[dict] | None, minutes: int) -> str: |
| 71 | c = cid(f"commit:{name}") |
| 72 | self.commits[name] = ([self.commits_id(p) for p in parents], ops if ops is not None else [], BASE + timedelta(minutes=minutes)) |
| 73 | if ops is None: |
| 74 | self._nodelta.add(name) |
| 75 | return c |
| 76 | |
| 77 | @staticmethod |
| 78 | def commits_id(name: str) -> str: |
| 79 | return cid(f"commit:{name}") |
| 80 | |
| 81 | async def install(self, session, repo_id: str, names: list[str]) -> None: |
| 82 | from sqlalchemy import select as _s |
| 83 | for n in names: |
| 84 | parents, ops, ts = self.commits[n] |
| 85 | c = self.commits_id(n) |
| 86 | if (await session.execute(_s(MusehubCommit.commit_id).where(MusehubCommit.commit_id == c))).first() is None: |
| 87 | session.add(MusehubCommit(commit_id=c, message=n, author="gabriel", branch="main", parent_ids=parents, |
| 88 | snapshot_id=None, timestamp=ts, |
| 89 | structured_delta=None if n in self._nodelta else {"ops": ops})) |
| 90 | await session.flush() |
| 91 | session.add(MusehubCommitRef(repo_id=repo_id, commit_id=c)) |
| 92 | await session.flush() |
| 93 | |
| 94 | |
| 95 | def standard_dag() -> Dag: |
| 96 | """c0 ─ c1 ─┐ |
| 97 | └ c2 ─┴ m ─ c3 (c3 deletes b; m changes a)""" |
| 98 | d = Dag() |
| 99 | d.add("c0", [], [ins("a.py", "a", "a0"), ins("lib.py", "shared", "s0")], 0) |
| 100 | d.add("c1", ["c0"], [ins("a.py", "a2", "a2_0"), rep("lib.py", "shared", "s0", "s1")], 10) |
| 101 | d.add("c2", ["c0"], [ins("b.py", "b", "b0")], 20) |
| 102 | d.add("m", ["c1", "c2"], [rep("a.py", "a", "a0", "a1")], 30) |
| 103 | d.add("c3", ["m"], [dele("b.py", "b", "b0")], 40) |
| 104 | return d |
| 105 | |
| 106 | |
| 107 | async def index_and_persist(session, repo_id: str, head: str): |
| 108 | results = await indexer.build_symbol_index(session, repo_id, head) |
| 109 | if results: |
| 110 | await persist_intel_results(session, repo_id, head, results) |
| 111 | await session.flush() |
| 112 | return results |
| 113 | |
| 114 | |
| 115 | async def snapshot_of(session, repo_id: str) -> dict: |
| 116 | """Everything the index wrote for a repository, in a comparable form (no repo_id, no wall-clock columns).""" |
| 117 | hist = (await session.execute(select(MusehubSymbolHistoryEntry).where(MusehubSymbolHistoryEntry.repo_id == repo_id))).scalars().all() |
| 118 | sym = (await session.execute(select(MusehubSymbolIntel).where(MusehubSymbolIntel.repo_id == repo_id))).scalars().all() |
| 119 | vit = (await session.execute(select(MusehubSymbolVitals).where(MusehubSymbolVitals.repo_id == repo_id))).scalars().all() |
| 120 | hsh = (await session.execute(select(MusehubHashOccurrenceEntry).where(MusehubHashOccurrenceEntry.repo_id == repo_id))).scalars().all() |
| 121 | res = (await session.execute(select(MusehubIntelResult).where(MusehubIntelResult.repo_id == repo_id))).scalars().all() |
| 122 | return { |
| 123 | "history": sorted((h.address, h.commit_id, h.op) for h in hist), |
| 124 | "symbol_intel": sorted( |
| 125 | (s.address, s.churn, s.churn_30d, s.churn_90d, s.blast, s.blast_direct, s.blast_cross, s.op, s.last_commit_id, |
| 126 | s.author_count, round(s.gravity, 6), tuple(s.weekly), s.symbol_kind) |
| 127 | for s in sym |
| 128 | ), |
| 129 | "vitals": sorted((v.address, v.change_count, v.version_count, v.op_add, v.op_modify, v.op_delete) for v in vit), |
| 130 | "hash": sorted((h.content_id, h.address) for h in hsh), |
| 131 | "results": sorted((r.intel_type, r.ref, r.data_json) for r in res), |
| 132 | } |
| 133 | |
| 134 | |
| 135 | # --------------------------------------------------------------------------- |
| 136 | # 1 + 2: full == incremental, and re-indexing is idempotent |
| 137 | # --------------------------------------------------------------------------- |
| 138 | |
| 139 | @pytest.mark.asyncio |
| 140 | async def test_incremental_index_equals_a_fresh_full_index_across_a_merge(session_factory) -> None: |
| 141 | dag = standard_dag() |
| 142 | async with session_factory() as s: |
| 143 | full = await create_repo(s, owner="gabriel", slug="full") |
| 144 | inc = await create_repo(s, owner="gabriel", slug="inc") |
| 145 | everything = ["c0", "c1", "c2", "m", "c3"] |
| 146 | await dag.install(s, full.repo_id, everything) |
| 147 | await dag.install(s, inc.repo_id, everything) |
| 148 | await index_and_persist(s, full.repo_id, dag.commits_id("c3")) |
| 149 | for head in ("c1", "m", "c3"): # three pushes: c1, then the merge, then c3 |
| 150 | await index_and_persist(s, inc.repo_id, dag.commits_id(head)) |
| 151 | a, b = await snapshot_of(s, full.repo_id), await snapshot_of(s, inc.repo_id) |
| 152 | assert a == b |
| 153 | |
| 154 | |
| 155 | @pytest.mark.asyncio |
| 156 | async def test_reindexing_the_same_revision_is_idempotent_and_writes_nothing(session_factory) -> None: |
| 157 | dag = standard_dag() |
| 158 | async with session_factory() as s: |
| 159 | repo = await create_repo(s, owner="gabriel", slug="idem") |
| 160 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 161 | head = dag.commits_id("c3") |
| 162 | await index_and_persist(s, repo.repo_id, head) |
| 163 | before = await snapshot_of(s, repo.repo_id) |
| 164 | again = await indexer.build_symbol_index(s, repo.repo_id, head) |
| 165 | after = await snapshot_of(s, repo.repo_id) |
| 166 | assert list(again) == [] and getattr(again, "outcome", None) == "already_current" |
| 167 | assert before == after |
| 168 | |
| 169 | |
| 170 | # --------------------------------------------------------------------------- |
| 171 | # The walk: exact set difference reachable(head) - reachable(stop_at) |
| 172 | # --------------------------------------------------------------------------- |
| 173 | |
| 174 | @pytest.mark.asyncio |
| 175 | async def test_new_commits_are_exactly_reachable_from_head_and_not_from_the_watermark(session_factory) -> None: |
| 176 | """The merge ``m`` has a second parent (c2) that is *not* an ancestor of the watermark c1, and ``c0`` is reachable |
| 177 | through both: only {c2, m, c3} are new. The old walk excluded only the watermark commit itself, so it re-walked c0.""" |
| 178 | dag = standard_dag() |
| 179 | async with session_factory() as s: |
| 180 | repo = await create_repo(s, owner="gabriel", slug="walk") |
| 181 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 182 | new = await indexer._new_commit_ids(s, repo.repo_id, dag.commits_id("c3"), dag.commits_id("c1")) |
| 183 | everything = await indexer._new_commit_ids(s, repo.repo_id, dag.commits_id("c3"), None) |
| 184 | assert set(new) == {dag.commits_id(n) for n in ("c2", "m", "c3")} |
| 185 | assert set(everything) == {dag.commits_id(n) for n in ("c0", "c1", "c2", "m", "c3")} |
| 186 | # causal order: a commit never precedes one of its parents |
| 187 | pos = {c: i for i, c in enumerate(everything)} |
| 188 | assert pos[dag.commits_id("c0")] < pos[dag.commits_id("c1")] < pos[dag.commits_id("m")] < pos[dag.commits_id("c3")] |
| 189 | assert pos[dag.commits_id("c2")] < pos[dag.commits_id("m")] |
| 190 | |
| 191 | |
| 192 | # --------------------------------------------------------------------------- |
| 193 | # Watermark: stale heads, divergent heads, no-op heads |
| 194 | # --------------------------------------------------------------------------- |
| 195 | |
| 196 | @pytest.mark.asyncio |
| 197 | async def test_an_older_head_cannot_regress_a_newer_index(session_factory) -> None: |
| 198 | dag = standard_dag() |
| 199 | async with session_factory() as s: |
| 200 | repo = await create_repo(s, owner="gabriel", slug="stale") |
| 201 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 202 | await index_and_persist(s, repo.repo_id, dag.commits_id("c3")) |
| 203 | before = await snapshot_of(s, repo.repo_id) |
| 204 | older = await indexer.build_symbol_index(s, repo.repo_id, dag.commits_id("c1")) # a retried job for an older push |
| 205 | after = await snapshot_of(s, repo.repo_id) |
| 206 | assert list(older) == [] and getattr(older, "outcome", None) == "stale" |
| 207 | assert before == after |
| 208 | assert {r[0]: r[1] for r in after["results"]}["code.intel_summary"] == dag.commits_id("c3") |
| 209 | |
| 210 | |
| 211 | @pytest.mark.asyncio |
| 212 | async def test_a_head_with_no_symbol_ops_still_advances_the_watermark(session_factory) -> None: |
| 213 | dag = Dag() |
| 214 | dag.add("c0", [], [ins("a.py", "a", "a0")], 0) |
| 215 | dag.add("docs", ["c0"], None, 10) # a docs-only commit: no structured delta at all |
| 216 | async with session_factory() as s: |
| 217 | repo = await create_repo(s, owner="gabriel", slug="noop") |
| 218 | await dag.install(s, repo.repo_id, ["c0", "docs"]) |
| 219 | await index_and_persist(s, repo.repo_id, dag.commits_id("c0")) |
| 220 | out = await index_and_persist(s, repo.repo_id, dag.commits_id("docs")) |
| 221 | snap = await snapshot_of(s, repo.repo_id) |
| 222 | assert out, "the revision changed, so the result must be re-published at the new revision" |
| 223 | assert {r[0]: r[1] for r in snap["results"]}["code.intel_summary"] == dag.commits_id("docs") |
| 224 | |
| 225 | |
| 226 | @pytest.mark.asyncio |
| 227 | async def test_a_repository_with_no_symbol_ops_at_all_publishes_no_healthy_looking_summary(session_factory) -> None: |
| 228 | dag = Dag() |
| 229 | dag.add("d0", [], None, 0) |
| 230 | async with session_factory() as s: |
| 231 | repo = await create_repo(s, owner="gabriel", slug="empty") |
| 232 | await dag.install(s, repo.repo_id, ["d0"]) |
| 233 | out = await indexer.build_symbol_index(s, repo.repo_id, dag.commits_id("d0")) |
| 234 | snap = await snapshot_of(s, repo.repo_id) |
| 235 | assert list(out) == [] and getattr(out, "outcome", None) == "empty" |
| 236 | assert snap["results"] == [] |
| 237 | |
| 238 | |
| 239 | # --------------------------------------------------------------------------- |
| 240 | # Failure honesty |
| 241 | # --------------------------------------------------------------------------- |
| 242 | |
| 243 | @pytest.mark.asyncio |
| 244 | async def test_a_failure_while_computing_the_aggregate_propagates_and_leaves_nothing(session_factory, monkeypatch) -> None: |
| 245 | """The old code logged the exception, returned an EMPTY summary and snapshot, and the caller persisted them at the new |
| 246 | head, so the watermark advanced over an analysis that never happened.""" |
| 247 | dag = standard_dag() |
| 248 | import musehub.services.musehub_intel as intel_mod |
| 249 | |
| 250 | def boom(*a, **k): |
| 251 | raise RuntimeError("aggregate exploded") |
| 252 | monkeypatch.setattr(intel_mod, "compute_intel", boom) |
| 253 | async with session_factory() as s: |
| 254 | repo = await create_repo(s, owner="gabriel", slug="boom") |
| 255 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 256 | await s.commit() |
| 257 | repo_id = repo.repo_id |
| 258 | async with session_factory() as s: |
| 259 | with pytest.raises(RuntimeError, match="aggregate exploded"): |
| 260 | await indexer.build_symbol_index(s, repo_id, dag.commits_id("c3")) |
| 261 | await s.rollback() # what the worker does when the job raises |
| 262 | async with session_factory() as s: |
| 263 | snap = await snapshot_of(s, repo_id) |
| 264 | assert snap["results"] == [] and snap["history"] == [] and snap["symbol_intel"] == [] |
| 265 | |
| 266 | |
| 267 | # --------------------------------------------------------------------------- |
| 268 | # Deleted symbols, obsolete rows, time anchoring, mass commits |
| 269 | # --------------------------------------------------------------------------- |
| 270 | |
| 271 | @pytest.mark.asyncio |
| 272 | async def test_deleted_symbols_are_not_counted_live_and_leave_no_clone_occurrence(session_factory) -> None: |
| 273 | dag = standard_dag() |
| 274 | async with session_factory() as s: |
| 275 | repo = await create_repo(s, owner="gabriel", slug="deleted") |
| 276 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 277 | await index_and_persist(s, repo.repo_id, dag.commits_id("c3")) |
| 278 | snap = await snapshot_of(s, repo.repo_id) |
| 279 | import json |
| 280 | summary = json.loads({r[0]: r[2] for r in snap["results"]}["code.intel_summary"]) |
| 281 | live = {a for a, *_ in snap["symbol_intel"] if a != "b.py::b"} |
| 282 | assert summary["symbol_count"] == len(live) == 3 # a, a2, shared — b was deleted in c3 |
| 283 | assert not any(addr == "b.py::b" for _, addr in snap["hash"]) |
| 284 | deleted_row = next(r for r in snap["symbol_intel"] if r[0] == "b.py::b") |
| 285 | assert deleted_row[7] == "delete" # history stays honest: the row says it was deleted |
| 286 | |
| 287 | |
| 288 | @pytest.mark.asyncio |
| 289 | async def test_hash_occurrence_holds_only_the_current_content_of_live_symbols(session_factory) -> None: |
| 290 | """``a`` was a0 and is now a1: a stale (a0, a) row would make a clone of anything that still has a0's body.""" |
| 291 | dag = standard_dag() |
| 292 | async with session_factory() as s: |
| 293 | repo = await create_repo(s, owner="gabriel", slug="hash") |
| 294 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 295 | await index_and_persist(s, repo.repo_id, dag.commits_id("c3")) |
| 296 | await index_and_persist(s, repo.repo_id, dag.commits_id("c3")) # and again: nothing accumulates |
| 297 | snap = await snapshot_of(s, repo.repo_id) |
| 298 | assert snap["hash"] == sorted([ |
| 299 | (cid("a1"), "a.py::a"), (cid("a2_0"), "a.py::a2"), (cid("s1"), "lib.py::shared"), |
| 300 | ]) |
| 301 | |
| 302 | |
| 303 | @pytest.mark.asyncio |
| 304 | async def test_time_windows_are_anchored_to_the_revision_not_the_wall_clock(session_factory) -> None: |
| 305 | """These commits are ~10 months older than 'now' in any test run. Measured back from the wall clock their 30-day |
| 306 | churn is 0 forever; measured back from the revision's own commit time it is what actually happened that month.""" |
| 307 | dag = standard_dag() # every commit is within 40 minutes of the head |
| 308 | async with session_factory() as s: |
| 309 | repo = await create_repo(s, owner="gabriel", slug="anchor") |
| 310 | await dag.install(s, repo.repo_id, ["c0", "c1", "c2", "m", "c3"]) |
| 311 | await index_and_persist(s, repo.repo_id, dag.commits_id("c3")) |
| 312 | snap = await snapshot_of(s, repo.repo_id) |
| 313 | row = next(r for r in snap["symbol_intel"] if r[0] == "a.py::a") |
| 314 | assert row[1] == 2 and row[2] == 2 and row[3] == 2 # churn, churn_30d, churn_90d: both ops are inside the window |
| 315 | assert row[11][0] == 2 # weekly[0] = the week ending at the head revision |
| 316 | |
| 317 | |
| 318 | def test_mass_commits_are_excluded_from_pair_counting() -> None: |
| 319 | """One import commit touching 1,500 symbols produced 1.1 million pairs (O(n²) memory) in ``compute_intel``.""" |
| 320 | from musehub.services.musehub_intel import compute_intel |
| 321 | history = {f"big.py::s{i}": [{"commit_id": cid("import"), "committed_at": BASE.isoformat(), "op": "insert"}] |
| 322 | for i in range(1500)} |
| 323 | snap = compute_intel(history, [], now_utc=BASE) |
| 324 | assert snap.coupling_pairs == [] |
| 325 | assert snap.total_symbols == 1500 |
File History
2 commits
sha256:70cb0ad300bccf1e11f6f0fb15b383c6b5c37a7ee3881ca12c0c0511d0a10de8
fix(intel): the intel.code pre-check looks up what the job …
Sonnet 5.5
patch
4 days ago
sha256:b174c9dd9807ae46a47396e24080868ed74e5e4b05a52e30538e5f706e77e9f7
fix(intel.code): make the index a pure function of one revi…
Sonnet 5.5
minor
⚠
4 days ago