test_intel_pipeline_e2e.py
python
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """End to end: push → enqueue → barriers → ``intel.code`` → the other providers → persistence → the Code Intelligence pages. |
| 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_pipeline_e2e.py |
| 7 | |
| 8 | Drives the real ``worker._process_one`` (the same function production runs) with intel execution allowed for ONE repository on a local |
| 9 | database. The non-intel jobs a push also enqueues (``mpack.index``, ``fetch.mpack.prebuild``, ``gc``...) are removed first: they are |
| 10 | not under test and the prebuild runs a child process. |
| 11 | |
| 12 | What it proves (the campaign's acceptance list): correct results, revision attribution, freshness after a second push, idempotency, |
| 13 | concurrency safety (overlapping pushes, a stale retry), failure honesty, bounded retries, no stale rows, no healthy-looking page for |
| 14 | analysis that never ran. |
| 15 | """ |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | from collections.abc import AsyncGenerator |
| 19 | |
| 20 | import pytest |
| 21 | import pytest_asyncio |
| 22 | from sqlalchemy import delete, func, select |
| 23 | from sqlalchemy.ext.asyncio import AsyncSession |
| 24 | |
| 25 | from musehub.db import database |
| 26 | from musehub.db.musehub_intel_models import ( |
| 27 | MusehubIntelApiSurface, |
| 28 | MusehubIntelBlastRisk, |
| 29 | MusehubIntelBreakageMeta, |
| 30 | MusehubIntelClones, |
| 31 | MusehubIntelCodemapMeta, |
| 32 | MusehubIntelCoupling, |
| 33 | MusehubIntelDead, |
| 34 | MusehubIntelEntangle, |
| 35 | MusehubIntelLanguages, |
| 36 | MusehubIntelRefactorEvent, |
| 37 | MusehubIntelResult, |
| 38 | MusehubIntelStable, |
| 39 | MusehubIntelType, |
| 40 | MusehubIntelVelocity, |
| 41 | MusehubSymbolIntel, |
| 42 | ) |
| 43 | from musehub.db.musehub_jobs_models import MusehubBackgroundJob |
| 44 | from musehub.db.musehub_repo_models import MusehubBranch, MusehubCommit |
| 45 | from musehub.services.musehub_jobs import enqueue_push_intel |
| 46 | from tests.factories import create_repo |
| 47 | from tests.test_intel_snapshot_providers import World, h |
| 48 | |
| 49 | ALLOW_ENV = "MUSEHUB_INTEL_WORKER_REPOS" |
| 50 | |
| 51 | #: The complete enqueue contract for a code repository, spelled out (not derived from the code under test): the index, the providers that read it, the |
| 52 | #: providers that do not, and the jobs this worker skips. A missing job or an unexpected one must fail a test. |
| 53 | INDEX_JOB = "intel.code" |
| 54 | INDEX_DEPENDENT_JOBS = frozenset({ |
| 55 | "intel.code.coupling", "intel.code.entangle", "intel.code.dead", "intel.code.blast_risk", |
| 56 | "intel.code.stable", "intel.code.velocity", "intel.code.clones", "intel.code.gravity", |
| 57 | }) |
| 58 | INDEX_INDEPENDENT_JOBS = frozenset({ |
| 59 | "intel.code.type", "intel.code.api_surface", "intel.code.languages", "intel.code.detect_refactor", "intel.code.codemap", "intel.code.breakage", |
| 60 | }) |
| 61 | EXPECTED_INTEL_JOB_TYPES = frozenset({"intel.structural", INDEX_JOB}) | INDEX_DEPENDENT_JOBS | INDEX_INDEPENDENT_JOBS |
| 62 | NON_INTEL_JOB_TYPES = frozenset({"push.file_last_commits", "mpack.index", "fetch.mpack.prebuild", "gc"}) |
| 63 | |
| 64 | SRC_V0 = {"core/a.py": "def f(x: int) -> int:\n return x\n", "core/b.py": "def g():\n return 1\n"} |
| 65 | SRC_V1 = {"core/a.py": "def f(x: int) -> int:\n return x + 1\n", "core/b.py": "def g():\n return 1\n"} |
| 66 | SRC_V2 = {"core/a.py": "def f(x: int) -> int:\n return x + 1\n", "core/c.py": "from core.a import f\n\n\ndef h():\n return f(1)\n"} |
| 67 | |
| 68 | |
| 69 | def ins(path: str, name: str, body: str) -> dict: |
| 70 | return {"op": "insert", "address": f"{path}::{name}", "content_id": h(body), "content_summary": f"added function {name} L0-2"} |
| 71 | |
| 72 | |
| 73 | def rep(path: str, name: str, old: str, new: str) -> dict: |
| 74 | return {"op": "replace", "address": f"{path}::{name}", "old_content_id": h(old), "new_content_id": h(new), |
| 75 | "new_summary": f"function {name} (changed) L0-2"} |
| 76 | |
| 77 | |
| 78 | def dele(path: str, name: str, body: str) -> dict: |
| 79 | return {"op": "delete", "address": f"{path}::{name}", "content_id": h(body), "content_summary": f"removed function {name} L0-2"} |
| 80 | |
| 81 | |
| 82 | @pytest_asyncio.fixture |
| 83 | async def worker_db(db_session: AsyncSession, monkeypatch: pytest.MonkeyPatch) -> AsyncGenerator[AsyncSession, None]: |
| 84 | from musehub.config import settings |
| 85 | from tests.conftest import _TEST_DATABASE_URL |
| 86 | |
| 87 | monkeypatch.delenv(ALLOW_ENV, raising=False) |
| 88 | monkeypatch.setattr(settings, "database_url", _TEST_DATABASE_URL) |
| 89 | monkeypatch.setenv("DATABASE_URL", _TEST_DATABASE_URL) |
| 90 | await database.init_db(pool_size=1, max_overflow=1) |
| 91 | try: |
| 92 | yield db_session |
| 93 | finally: |
| 94 | await database.close_db() |
| 95 | |
| 96 | |
| 97 | async def build_repo(s: AsyncSession) -> tuple[World, dict[str, str]]: |
| 98 | repo = await create_repo(s, owner="testuser", slug="e2e") |
| 99 | w = World(s, repo.repo_id) |
| 100 | c0 = await w.commit("c0", SRC_V0, minute=0) |
| 101 | c1 = await w.commit("c1", SRC_V1, ["c0"], minute=10) |
| 102 | c2 = await w.commit("c2", SRC_V2, ["c1"], minute=20) |
| 103 | ops = { |
| 104 | c0: [ins("core/a.py", "f", "f0"), ins("core/b.py", "g", "g0")], |
| 105 | c1: [rep("core/a.py", "f", "f0", "f1")], |
| 106 | c2: [ins("core/c.py", "h", "h0"), dele("core/b.py", "g", "g0")], |
| 107 | } |
| 108 | for cid, delta in ops.items(): |
| 109 | (await s.get(MusehubCommit, cid)).structured_delta = {"ops": delta} |
| 110 | s.add(MusehubBranch(branch_id=h("branch:e2e"), repo_id=repo.repo_id, name="main", head_commit_id=c2)) |
| 111 | await s.commit() |
| 112 | return w, {"c0": c0, "c1": c1, "c2": c2} |
| 113 | |
| 114 | |
| 115 | async def push(s: AsyncSession, repo_id: str, head: str) -> None: |
| 116 | """What the wire push does after unpacking: enqueue the push's jobs. Non-intel jobs are dropped (not under test).""" |
| 117 | await enqueue_push_intel(s, repo_id, head, branch="main") |
| 118 | await s.flush() |
| 119 | await s.execute(delete(MusehubBackgroundJob).where( |
| 120 | MusehubBackgroundJob.repo_id == repo_id, ~MusehubBackgroundJob.job_type.like("intel.%"), MusehubBackgroundJob.status == "pending")) |
| 121 | await s.commit() |
| 122 | |
| 123 | |
| 124 | async def drain(worker, limit: int = 80) -> int: |
| 125 | n = 0 |
| 126 | while n < limit and await worker._process_one(): |
| 127 | n += 1 |
| 128 | return n |
| 129 | |
| 130 | |
| 131 | async def statuses(s: AsyncSession, repo_id: str) -> dict[str, list[str]]: |
| 132 | await s.rollback() |
| 133 | out: dict[str, list[str]] = {} |
| 134 | for j in (await s.execute(select(MusehubBackgroundJob).where(MusehubBackgroundJob.repo_id == repo_id).order_by(MusehubBackgroundJob.created_at))).scalars(): |
| 135 | out.setdefault(j.job_type, []).append(j.status) |
| 136 | return out |
| 137 | |
| 138 | |
| 139 | async def published_refs(s: AsyncSession, repo_id: str) -> dict[str, str]: |
| 140 | await s.rollback() |
| 141 | return {r.intel_type: r.ref for r in (await s.execute(select(MusehubIntelResult).where(MusehubIntelResult.repo_id == repo_id))).scalars()} |
| 142 | |
| 143 | |
| 144 | @pytest.mark.asyncio |
| 145 | async def test_a_push_runs_the_whole_pipeline_in_dependency_order_and_publishes_one_consistent_revision(worker_db, monkeypatch) -> None: |
| 146 | import musehub.worker as worker |
| 147 | w, c = await build_repo(worker_db) |
| 148 | monkeypatch.setenv(ALLOW_ENV, w.repo_id) |
| 149 | await push(worker_db, w.repo_id, c["c2"]) |
| 150 | |
| 151 | order: list[str] = [] |
| 152 | real = worker._run_intel_job |
| 153 | |
| 154 | async def spy(job_type, repo_id, payload, fence=None): |
| 155 | order.append(job_type) |
| 156 | return await real(job_type, repo_id, payload, fence) |
| 157 | monkeypatch.setattr(worker, "_run_intel_job", spy) |
| 158 | await drain(worker) |
| 159 | |
| 160 | st = await statuses(worker_db, w.repo_id) |
| 161 | intel_types = {t for t in st if t.startswith("intel.")} |
| 162 | assert intel_types == EXPECTED_INTEL_JOB_TYPES, (intel_types ^ EXPECTED_INTEL_JOB_TYPES) # exact: structural + index + 8 dependent + 6 independent (no midi/mist for code) |
| 163 | assert set(st) == EXPECTED_INTEL_JOB_TYPES, set(st) ^ EXPECTED_INTEL_JOB_TYPES # enqueue_push_intel creates the intel jobs only; the wire push adds the NON_INTEL ones |
| 164 | assert all(len(v) == 1 for v in st.values()), st # one job per type for one push |
| 165 | assert all(v == ["done"] for t, v in st.items() if t.startswith("intel.")), st |
| 166 | # barrier: the index is built before any of the eight providers that read it |
| 167 | assert set(order) == EXPECTED_INTEL_JOB_TYPES, set(order) ^ EXPECTED_INTEL_JOB_TYPES # every intel job ran, none twice |
| 168 | assert len(order) == len(EXPECTED_INTEL_JOB_TYPES), order |
| 169 | assert order.index(INDEX_JOB) < min(order.index(t) for t in INDEX_DEPENDENT_JOBS) |
| 170 | |
| 171 | refs = await published_refs(worker_db, w.repo_id) |
| 172 | assert refs["code.intel_summary"] == c["c2"] |
| 173 | assert all(refs[t] == c["c2"] for t in refs if t.startswith("intel.code.") and t != "intel.code.detect_refactor"), refs |
| 174 | # live symbols at c2: f, h (b::g was deleted) |
| 175 | sym = {r.address: r for r in (await worker_db.execute(select(MusehubSymbolIntel).where(MusehubSymbolIntel.repo_id == w.repo_id))).scalars()} |
| 176 | assert {a for a, r in sym.items() if r.op != "delete"} == {"core/a.py::f", "core/c.py::h"} |
| 177 | # snapshot providers saw exactly c2's files |
| 178 | codemap = (await worker_db.execute(select(MusehubIntelCodemapMeta).where(MusehubIntelCodemapMeta.repo_id == w.repo_id))).scalar_one() |
| 179 | assert codemap.total_modules == 2 and codemap.total_edges == 1 and codemap.ref == c["c2"] |
| 180 | langs = (await worker_db.execute(select(MusehubIntelLanguages).where(MusehubIntelLanguages.repo_id == w.repo_id))).scalars().all() |
| 181 | assert {(r.language, r.file_count) for r in langs} == {("Python", 2)} |
| 182 | # detect_refactor covered both commits the push brought |
| 183 | events = (await worker_db.execute(select(MusehubIntelRefactorEvent).where(MusehubIntelRefactorEvent.repo_id == w.repo_id))).scalars().all() |
| 184 | assert {e.commit_id for e in events} == {c["c1"]} # c1 rewrote f's body; c2 only adds/removes symbols |
| 185 | |
| 186 | |
| 187 | @pytest.mark.asyncio |
| 188 | async def test_a_second_push_refreshes_everything_idempotently_and_a_stale_retry_changes_nothing(worker_db, monkeypatch) -> None: |
| 189 | import musehub.worker as worker |
| 190 | w, c = await build_repo(worker_db) |
| 191 | monkeypatch.setenv(ALLOW_ENV, w.repo_id) |
| 192 | |
| 193 | await push(worker_db, w.repo_id, c["c1"]) # first push: only c1 so far |
| 194 | await drain(worker) |
| 195 | assert (await published_refs(worker_db, w.repo_id))["code.intel_summary"] == c["c1"] |
| 196 | |
| 197 | await push(worker_db, w.repo_id, c["c2"]) # second push |
| 198 | await drain(worker) |
| 199 | refs = await published_refs(worker_db, w.repo_id) |
| 200 | assert refs["code.intel_summary"] == c["c2"] and refs["intel.code.dead"] == c["c2"] |
| 201 | |
| 202 | snapshot = await _table_snapshot(worker_db, w.repo_id) |
| 203 | await push(worker_db, w.repo_id, c["c2"]) # the same revision pushed again |
| 204 | await drain(worker) |
| 205 | assert await _table_snapshot(worker_db, w.repo_id) == snapshot, "re-analysing the same revision changed something" |
| 206 | |
| 207 | # a retried job for the OLDER revision arrives late |
| 208 | await worker_db.rollback() |
| 209 | await push(worker_db, w.repo_id, c["c1"]) |
| 210 | await drain(worker) |
| 211 | assert await _table_snapshot(worker_db, w.repo_id) == snapshot, "a stale job overwrote newer results" |
| 212 | refs = await published_refs(worker_db, w.repo_id) |
| 213 | assert refs["code.intel_summary"] == c["c2"] |
| 214 | st = await statuses(worker_db, w.repo_id) |
| 215 | assert {x for t, v in st.items() if t.startswith("intel.") for x in v} <= {"done", "skipped"}, st |
| 216 | assert "skipped" in {x for v in st.values() for x in v}, "the stale retry's dependents must end as superseded, not as done" |
| 217 | |
| 218 | |
| 219 | async def _table_snapshot(s: AsyncSession, repo_id: str) -> dict: |
| 220 | await s.rollback() |
| 221 | out: dict = {} |
| 222 | for model, cols in ( |
| 223 | (MusehubIntelDead, ("address", "confidence", "ref")), (MusehubIntelBlastRisk, ("address", "risk_score", "ref")), |
| 224 | (MusehubIntelStable, ("address", "days_stable", "ref")), (MusehubIntelVelocity, ("module", "added", "modified", "ref")), |
| 225 | (MusehubIntelClones, ("cluster_hash", "member_count", "ref")), (MusehubIntelCoupling, ("file_a", "file_b", "ref")), |
| 226 | (MusehubIntelEntangle, ("symbol_a", "symbol_b", "ref")), (MusehubIntelType, ("address", "ref")), |
| 227 | (MusehubIntelApiSurface, ("address", "ref")), (MusehubIntelLanguages, ("language", "file_count", "ref")), |
| 228 | (MusehubSymbolIntel, ("address", "churn", "blast", "op", "gravity_pct")), |
| 229 | ): |
| 230 | rows = (await s.execute(select(model).where(model.repo_id == repo_id))).scalars().all() |
| 231 | out[model.__tablename__] = sorted(tuple(getattr(r, c) for c in cols) for r in rows) |
| 232 | out["refs"] = sorted((await published_refs(s, repo_id)).items()) |
| 233 | return out |
| 234 | |
| 235 | |
| 236 | @pytest.mark.asyncio |
| 237 | async def test_pushes_that_overlap_a_running_index_are_queued_and_both_revisions_end_up_analysed(worker_db, monkeypatch) -> None: |
| 238 | import musehub.worker as worker |
| 239 | w, c = await build_repo(worker_db) |
| 240 | monkeypatch.setenv(ALLOW_ENV, w.repo_id) |
| 241 | await push(worker_db, w.repo_id, c["c1"]) |
| 242 | |
| 243 | # run exactly one job: the index for c1 is claimed and finished; now c2 is pushed while c1's dependents are still pending |
| 244 | assert await worker._process_one() is True |
| 245 | await push(worker_db, w.repo_id, c["c2"]) |
| 246 | pending = (await worker_db.execute(select(MusehubBackgroundJob).where( |
| 247 | MusehubBackgroundJob.repo_id == w.repo_id, MusehubBackgroundJob.status == "pending", MusehubBackgroundJob.job_type.like("intel.%")))).scalars().all() |
| 248 | assert pending and all(j.payload["head"] == c["c2"] for j in pending), "pending jobs must be refreshed to the newest push, not left on c1" |
| 249 | await drain(worker) |
| 250 | st = await statuses(worker_db, w.repo_id) |
| 251 | assert not {x for t, v in st.items() if t.startswith("intel.") for x in v} - {"done"}, st |
| 252 | assert (await published_refs(worker_db, w.repo_id))["code.intel_summary"] == c["c2"] |
| 253 | |
| 254 | |
| 255 | @pytest.mark.asyncio |
| 256 | async def test_failure_is_honest_bounded_and_publishes_no_healthy_looking_page(worker_db, monkeypatch, client) -> None: |
| 257 | import musehub.worker as worker |
| 258 | import musehub.services.musehub_intel as intel_mod |
| 259 | w, c = await build_repo(worker_db) |
| 260 | monkeypatch.setenv(ALLOW_ENV, w.repo_id) |
| 261 | monkeypatch.setattr(intel_mod, "compute_intel", lambda *a, **k: (_ for _ in ()).throw(RuntimeError("aggregate exploded"))) |
| 262 | await push(worker_db, w.repo_id, c["c2"]) |
| 263 | await drain(worker) |
| 264 | st = await statuses(worker_db, w.repo_id) |
| 265 | assert st["intel.code"] == ["failed"], st # 3 attempts, then failed: bounded |
| 266 | assert all(st[t] == ["blocked"] for t in ("intel.code.dead", "intel.code.gravity", "intel.code.entangle")), st # never analysed stale data |
| 267 | assert st["intel.code.languages"] == ["done"], "snapshot providers do not depend on the index" |
| 268 | refs = await published_refs(worker_db, w.repo_id) |
| 269 | assert "code.intel_summary" not in refs and "intel.code.dead" not in refs |
| 270 | n = (await worker_db.execute(select(func.count()).select_from(MusehubSymbolIntel).where(MusehubSymbolIntel.repo_id == w.repo_id))).scalar_one() |
| 271 | assert n == 0, "a failed index must leave no partial symbol rows" |
| 272 | |
| 273 | |
| 274 | @pytest.mark.asyncio |
| 275 | async def test_pages_show_what_the_analysis_found_and_do_not_claim_more(worker_db, monkeypatch, client) -> None: |
| 276 | import musehub.worker as worker |
| 277 | w, c = await build_repo(worker_db) |
| 278 | monkeypatch.setenv(ALLOW_ENV, w.repo_id) |
| 279 | await push(worker_db, w.repo_id, c["c2"]) |
| 280 | await drain(worker) |
| 281 | r = await client.get("/testuser/e2e/intel/gravity") |
| 282 | assert r.status_code == 200 and "core/a.py" in r.text |
| 283 | assert "changed together" not in r.text # gravity is call-graph dependents (muse#251), not co-change |
| 284 | r = await client.get("/testuser/e2e/intel/dead") |
| 285 | assert r.status_code == 200 and "clean repo" not in r.text.lower() |
| 286 | |
| 287 | |
| 288 | def test_the_enqueue_contract_for_a_code_repository_is_exactly_the_expected_job_set() -> None: |
| 289 | from musehub.services.musehub_intel_providers import INDEX_DEPENDENT_JOB_TYPES, job_types_for_push |
| 290 | |
| 291 | for domain in (None, "code", "@gabriel/code"): |
| 292 | types = job_types_for_push(domain) |
| 293 | assert len(types) == len(set(types)), "no job type is enqueued twice" |
| 294 | assert set(types) == EXPECTED_INTEL_JOB_TYPES | NON_INTEL_JOB_TYPES, set(types) ^ (EXPECTED_INTEL_JOB_TYPES | NON_INTEL_JOB_TYPES) |
| 295 | assert INDEX_DEPENDENT_JOB_TYPES == INDEX_DEPENDENT_JOBS |
| 296 | assert len(EXPECTED_INTEL_JOB_TYPES) == 16 and len(INDEX_DEPENDENT_JOBS) == 8 and len(INDEX_INDEPENDENT_JOBS) == 6 |
| 297 | assert {t for t in job_types_for_push("midi") if t.startswith("intel.")} == {"intel.structural", "intel.midi"} |
| 298 | assert {t for t in job_types_for_push("mist") if t.startswith("intel.")} == {"intel.structural", "intel.mist"} |
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:e31a659ae921cd5892ab20f9e02f320cae182610f5495d00931b715fc31f43f5
test(intel): exact enqueue contract and provider-set assert…
Sonnet 5.5
3 days ago
sha256:cb4b05e9d6caea5f351e3cb452ee6a1db8856169328ad96bfba9ee8e04b8afa6
fix(jobs): fence every execution of a job; an expired or su…
Sonnet 5.5
minor
⚠
4 days ago
sha256:8211ff1fbc7076c725b68b3f778b5aab506725a65a5372d4b965bdc093eb7127
test(intel): end-to-end pipeline through the real worker; f…
Sonnet 5.5
patch
4 days ago