test_intel_empty_publication.py
python
sha256:b4b999434d042d74edc7032f1f2cfd20fda5b3580fe85966351db5b8eef0abb3
feat(intel): every grammar on the hub, and every intel job …
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """A provider that recomputes to *nothing* must leave its table and ``musehub_intel_results`` agreeing — musehub#8, Tier A review D1. |
| 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_empty_publication.py |
| 7 | |
| 8 | The defect: a provider replaced (cleared) its own table and returned ``[]``; the worker called that ``empty`` and COMMITTED, so the |
| 9 | table was empty while ``musehub_intel_results`` still said "N rows at the old revision". The contract now: |
| 10 | |
| 11 | * the input exists and yields nothing -> an explicit **zero-count** result for the analysed revision (table empty, ``count`` 0, |
| 12 | ``ref`` = the revision); |
| 13 | * the input cannot be analysed at all (no snapshot, no index rows) -> the table is cleared AND the stored result is deleted; |
| 14 | * in neither case is the old result left behind, and a failure after the provider wrote leaves BOTH untouched (one transaction). |
| 15 | |
| 16 | Every test drives the production path, ``worker._run_intel_job``. ``detect_refactor`` is deliberately absent: it appends events and |
| 17 | never clears a table, and its result row is its watermark. |
| 18 | """ |
| 19 | from __future__ import annotations |
| 20 | |
| 21 | import json |
| 22 | from datetime import timedelta |
| 23 | |
| 24 | import pytest |
| 25 | from sqlalchemy import delete, select |
| 26 | |
| 27 | from musehub.db.musehub_intel_models import ( |
| 28 | MusehubIntelApiSurface, |
| 29 | MusehubIntelBlastRisk, |
| 30 | MusehubIntelCodemapMeta, |
| 31 | MusehubIntelCodemapModule, |
| 32 | MusehubIntelClones, |
| 33 | MusehubIntelCoupling, |
| 34 | MusehubIntelDead, |
| 35 | MusehubIntelEntangle, |
| 36 | MusehubIntelLanguages, |
| 37 | MusehubIntelResult, |
| 38 | MusehubIntelStable, |
| 39 | MusehubIntelType, |
| 40 | MusehubIntelVelocity, |
| 41 | MusehubSymbolHistoryEntry, |
| 42 | MusehubSymbolIntel, |
| 43 | ) |
| 44 | from musehub.db.musehub_repo_models import MusehubCommit, MusehubCommitRef |
| 45 | from musehub.services import musehub_intel_providers as P |
| 46 | from tests.factories import create_repo |
| 47 | from tests.test_intel_code_correctness import cid |
| 48 | from tests.test_intel_index_providers import ALL1, dag_one |
| 49 | from tests.test_intel_job_state_machine import worker_db # noqa: F401 - fixture: the production engine on the private test database |
| 50 | from tests.test_intel_snapshot_providers import A_V1, B_SRC, BASE, World |
| 51 | |
| 52 | |
| 53 | async def run(job_type: str, repo_id: str, ref: str) -> tuple[str, dict]: |
| 54 | import musehub.worker as worker |
| 55 | return await worker._run_intel_job(job_type, repo_id, {"head": ref, "branch": "main"}) |
| 56 | |
| 57 | |
| 58 | async def result_of(s, repo_id: str, intel_type: str) -> tuple[str, dict] | None: |
| 59 | await s.rollback() |
| 60 | row = (await s.execute(select(MusehubIntelResult).where( |
| 61 | MusehubIntelResult.repo_id == repo_id, MusehubIntelResult.intel_type == intel_type))).scalar_one_or_none() |
| 62 | return None if row is None else (row.ref, json.loads(row.data_json)) |
| 63 | |
| 64 | |
| 65 | async def count_of(s, model, repo_id: str) -> int: |
| 66 | await s.rollback() |
| 67 | return len((await s.execute(select(model).where(model.repo_id == repo_id))).scalars().all()) |
| 68 | |
| 69 | |
| 70 | # --------------------------------------------------------------------------- |
| 71 | # index-reading providers: the input exists and yields nothing |
| 72 | # --------------------------------------------------------------------------- |
| 73 | |
| 74 | async def _wipe_history(s, repo_id: str) -> None: |
| 75 | await s.execute(delete(MusehubSymbolHistoryEntry).where(MusehubSymbolHistoryEntry.repo_id == repo_id)) |
| 76 | |
| 77 | |
| 78 | async def _wipe_changes(s, repo_id: str) -> None: |
| 79 | """No recorded change anywhere: no structured delta (what the jobs read first, muse#251) and no history row.""" |
| 80 | from sqlalchemy import update |
| 81 | |
| 82 | commits = select(MusehubCommitRef.commit_id).where(MusehubCommitRef.repo_id == repo_id) |
| 83 | await s.execute(update(MusehubCommit).where(MusehubCommit.commit_id.in_(commits)).values(structured_delta=None)) |
| 84 | await _wipe_history(s, repo_id) |
| 85 | |
| 86 | |
| 87 | async def _wipe_symbols(s, repo_id: str) -> None: |
| 88 | await s.execute(delete(MusehubSymbolIntel).where(MusehubSymbolIntel.repo_id == repo_id)) |
| 89 | |
| 90 | |
| 91 | INDEX_CASES = [ |
| 92 | # job type, table, how the next revision's input becomes empty |
| 93 | ("intel.code.coupling", MusehubIntelCoupling, _wipe_changes), |
| 94 | ("intel.code.entangle", MusehubIntelEntangle, _wipe_changes), |
| 95 | ("intel.code.velocity", MusehubIntelVelocity, _wipe_changes), |
| 96 | ] |
| 97 | |
| 98 | |
| 99 | @pytest.mark.asyncio |
| 100 | @pytest.mark.parametrize("job_type,model,make_empty", INDEX_CASES, ids=[c[0] for c in INDEX_CASES]) |
| 101 | async def test_a_nonempty_result_followed_by_an_empty_recomputation_publishes_an_explicit_zero_for_the_new_revision( |
| 102 | worker_db, job_type, model, make_empty, |
| 103 | ) -> None: |
| 104 | s = worker_db |
| 105 | dag = dag_one() |
| 106 | repo = await create_repo(s, owner="gabriel", slug=f"d1-{job_type.replace('.', '-')}") |
| 107 | rid = repo.repo_id |
| 108 | await dag.install(s, rid, ALL1) |
| 109 | await s.commit() |
| 110 | r5, r6 = dag.commits_id("r5"), dag.commits_id("r6") |
| 111 | |
| 112 | assert (await run("intel.code", rid, r5))[0] == "done" |
| 113 | assert (await run(job_type, rid, r5))[0] == "done" |
| 114 | ref, data = await result_of(s, rid, job_type) |
| 115 | assert ref == r5 and data["count"] > 0 and await count_of(s, model, rid) > 0 # a real, non-empty result |
| 116 | |
| 117 | assert (await run("intel.code", rid, r6))[0] == "done" # the index moves to r6 ... |
| 118 | await make_empty(s, rid) # ... and the provider's input is gone |
| 119 | await s.commit() |
| 120 | status, result = await run(job_type, rid, r6) |
| 121 | |
| 122 | assert status == "done" and result["executed"] is True |
| 123 | ref, data = await result_of(s, rid, job_type) |
| 124 | assert await count_of(s, model, rid) == 0 |
| 125 | assert (ref, data["count"]) == (r6, 0), "metadata must describe the (empty) table at the NEW revision, not the old one" |
| 126 | |
| 127 | |
| 128 | @pytest.mark.asyncio |
| 129 | async def test_gravity_with_no_indexed_symbols_publishes_an_explicit_zero(worker_db) -> None: |
| 130 | """Gravity reads the snapshot's call graph and writes it to the indexed symbols; with none indexed it reports zero.""" |
| 131 | from muse.plugins.code.ast_parser import parse_symbols |
| 132 | |
| 133 | s = worker_db |
| 134 | repo = await create_repo(s, owner="gabriel", slug="d1-gravity") |
| 135 | rid = repo.repo_id |
| 136 | w = World(s, rid) |
| 137 | files = {"pkg/a.py": A_V1, "pkg/b.py": B_SRC} |
| 138 | c1 = await w.commit("c1", files, minute=0) |
| 139 | for path, src in files.items(): |
| 140 | for addr, rec in parse_symbols(src.encode(), path).items(): |
| 141 | if rec["kind"] != "import": |
| 142 | s.add(MusehubSymbolIntel(repo_id=rid, address=addr, symbol_kind=rec["kind"], op="insert")) |
| 143 | await s.commit() |
| 144 | assert (await P.GravityProvider().compute(s, rid, c1, {}))[0][1]["count"] > 0 |
| 145 | await s.commit() |
| 146 | |
| 147 | await _wipe_symbols(s, rid) |
| 148 | await s.commit() |
| 149 | assert (await P.GravityProvider().compute(s, rid, c1, {}))[0][1]["count"] == 0 |
| 150 | |
| 151 | |
| 152 | # --------------------------------------------------------------------------- |
| 153 | # snapshot-reading providers |
| 154 | # --------------------------------------------------------------------------- |
| 155 | |
| 156 | SNAPSHOT_CASES = [ |
| 157 | ("intel.code.type", [MusehubIntelType]), |
| 158 | ("intel.code.api_surface", [MusehubIntelApiSurface]), |
| 159 | ("intel.code.languages", [MusehubIntelLanguages]), |
| 160 | ("intel.code.codemap", [MusehubIntelCodemapModule]), |
| 161 | ] |
| 162 | |
| 163 | #: Snapshot readers that also wait for the index (they publish per revision of the index): dead code and blast risk (muse#251). |
| 164 | SNAPSHOT_AFTER_INDEX_CASES = [ |
| 165 | ("intel.code.dead", MusehubIntelDead, P.DeadProvider), |
| 166 | ("intel.code.blast_risk", MusehubIntelBlastRisk, P.BlastRiskProvider), |
| 167 | ("intel.code.stable", MusehubIntelStable, P.StableProvider), |
| 168 | ("intel.code.clones", MusehubIntelClones, P.ClonesProvider), |
| 169 | ] |
| 170 | |
| 171 | |
| 172 | @pytest.mark.asyncio |
| 173 | @pytest.mark.parametrize("job_type,model,provider", SNAPSHOT_AFTER_INDEX_CASES, ids=[c[0] for c in SNAPSHOT_AFTER_INDEX_CASES]) |
| 174 | async def test_an_empty_snapshot_publishes_an_explicit_zero_for_snapshot_readers(worker_db, job_type, model, provider) -> None: |
| 175 | s = worker_db |
| 176 | repo = await create_repo(s, owner="gabriel", slug=f"d1-snapidx-{job_type.replace('.', '-')}") |
| 177 | rid = repo.repo_id |
| 178 | w = World(s, rid) |
| 179 | c1 = await w.commit("c1", {"pkg/a.py": A_V1, "pkg/b.py": B_SRC, "pkg/copy.py": A_V1}, minute=0) # copy.py: a clone |
| 180 | c2 = await w.commit("c2", {}, ["c1"], minute=10) |
| 181 | await s.commit() |
| 182 | ((published, data),) = await provider().compute(s, rid, c1, {}) |
| 183 | await s.commit() |
| 184 | assert published == job_type and data["count"] > 0 and await count_of(s, model, rid) > 0 |
| 185 | ((published, data),) = await provider().compute(s, rid, c2, {}) # the worker stores it under c2 |
| 186 | await s.commit() |
| 187 | assert published == job_type and data["count"] == 0 and await count_of(s, model, rid) == 0 |
| 188 | |
| 189 | |
| 190 | @pytest.mark.asyncio |
| 191 | @pytest.mark.parametrize("job_type,models", SNAPSHOT_CASES, ids=[c[0] for c in SNAPSHOT_CASES]) |
| 192 | async def test_a_revision_with_nothing_to_report_publishes_an_explicit_zero_for_that_revision(worker_db, job_type, models) -> None: |
| 193 | s = worker_db |
| 194 | repo = await create_repo(s, owner="gabriel", slug=f"d1-snap-{job_type.replace('.', '-')}") |
| 195 | rid = repo.repo_id |
| 196 | w = World(s, rid) |
| 197 | c1 = await w.commit("c1", {"pkg/a.py": A_V1, "pkg/b.py": B_SRC}, minute=0) |
| 198 | c2 = await w.commit("c2", {}, ["c1"], minute=10) # an empty snapshot: the input exists and holds nothing |
| 199 | await s.commit() |
| 200 | |
| 201 | assert (await run(job_type, rid, c1))[0] == "done" |
| 202 | ref, data = await result_of(s, rid, job_type) |
| 203 | assert ref == c1 and (data.get("count") or data.get("modules")) > 0 and await count_of(s, models[0], rid) > 0 |
| 204 | |
| 205 | assert (await run(job_type, rid, c2))[0] == "done" |
| 206 | ref, data = await result_of(s, rid, job_type) |
| 207 | assert await count_of(s, models[0], rid) == 0 |
| 208 | assert ref == c2 and (data.get("count", data.get("modules"))) == 0 |
| 209 | |
| 210 | |
| 211 | @pytest.mark.asyncio |
| 212 | async def test_a_python_free_revision_publishes_zero_type_and_api_surface(worker_db) -> None: |
| 213 | """A manifest that exists but holds no Python (the ``py_entries`` / "no public symbol" paths) is a real zero, not "nothing".""" |
| 214 | s = worker_db |
| 215 | repo = await create_repo(s, owner="gabriel", slug="d1-nopy") |
| 216 | rid = repo.repo_id |
| 217 | w = World(s, rid) |
| 218 | c1 = await w.commit("c1", {"pkg/a.py": A_V1}, minute=0) |
| 219 | c2 = await w.commit("c2", {"README.md": "# docs only\n"}, ["c1"], minute=10) |
| 220 | await s.commit() |
| 221 | for jt in ("intel.code.type", "intel.code.api_surface"): |
| 222 | assert (await run(jt, rid, c1))[0] == "done" |
| 223 | assert (await run(jt, rid, c2))[0] == "done" |
| 224 | ref, data = await result_of(s, rid, jt) |
| 225 | assert ref == c2 and data["count"] == 0 |
| 226 | assert await count_of(s, MusehubIntelType, rid) == 0 and await count_of(s, MusehubIntelApiSurface, rid) == 0 |
| 227 | |
| 228 | |
| 229 | @pytest.mark.asyncio |
| 230 | @pytest.mark.parametrize("job_type,models", [ |
| 231 | ("intel.code.type", [MusehubIntelType]), ("intel.code.api_surface", [MusehubIntelApiSurface]), |
| 232 | ("intel.code.languages", [MusehubIntelLanguages]), ("intel.code.codemap", [MusehubIntelCodemapModule, MusehubIntelCodemapMeta]), |
| 233 | ]) |
| 234 | async def test_a_commit_with_no_snapshot_clears_the_table_and_deletes_the_stored_result(worker_db, job_type, models) -> None: |
| 235 | s = worker_db |
| 236 | repo = await create_repo(s, owner="gabriel", slug=f"d1-nosnap-{job_type.replace('.', '-')}") |
| 237 | rid = repo.repo_id |
| 238 | w = World(s, rid) |
| 239 | c1 = await w.commit("c1", {"pkg/a.py": A_V1}, minute=0) |
| 240 | bare = cid("commit:bare") |
| 241 | s.add(MusehubCommit(commit_id=bare, message="bare", author="gabriel", branch="main", parent_ids=[c1], snapshot_id=None, |
| 242 | timestamp=BASE + timedelta(minutes=5))) |
| 243 | s.add(MusehubCommitRef(repo_id=rid, commit_id=bare)) |
| 244 | await s.commit() |
| 245 | assert (await run(job_type, rid, c1))[0] == "done" |
| 246 | assert await result_of(s, rid, job_type) is not None |
| 247 | |
| 248 | assert (await run(job_type, rid, bare))[0] == "done" |
| 249 | assert await result_of(s, rid, job_type) is None |
| 250 | for m in models: |
| 251 | assert await count_of(s, m, rid) == 0 |
| 252 | |
| 253 | |
| 254 | # --------------------------------------------------------------------------- |
| 255 | # one transaction |
| 256 | # --------------------------------------------------------------------------- |
| 257 | |
| 258 | @pytest.mark.asyncio |
| 259 | async def test_a_failure_after_the_provider_cleared_its_table_leaves_table_and_metadata_untouched(worker_db, monkeypatch) -> None: |
| 260 | s = worker_db |
| 261 | dag = dag_one() |
| 262 | repo = await create_repo(s, owner="gabriel", slug="d1-rollback") |
| 263 | rid = repo.repo_id |
| 264 | await dag.install(s, rid, ALL1) |
| 265 | await s.commit() |
| 266 | r5 = dag.commits_id("r5") |
| 267 | await run("intel.code", rid, r5) |
| 268 | await run("intel.code.coupling", rid, r5) |
| 269 | before = await result_of(s, rid, "intel.code.coupling") |
| 270 | rows_before = await count_of(s, MusehubIntelCoupling, rid) |
| 271 | assert before is not None and rows_before > 0 |
| 272 | |
| 273 | await _wipe_history(s, rid) |
| 274 | await s.commit() |
| 275 | |
| 276 | import musehub.services.musehub_intel_providers as providers |
| 277 | |
| 278 | async def boom(*a, **k): |
| 279 | raise RuntimeError("persist exploded after the provider had already cleared its table") |
| 280 | monkeypatch.setattr(providers, "persist_intel_results", boom) |
| 281 | with pytest.raises(RuntimeError, match="persist exploded"): |
| 282 | await run("intel.code.coupling", rid, r5) |
| 283 | |
| 284 | assert await result_of(s, rid, "intel.code.coupling") == before |
| 285 | assert await count_of(s, MusehubIntelCoupling, rid) == rows_before |
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:f0600dcd29191749f77ddd3118c65b8c2444ae7c22dd783bd818463d8533b5b1
fix(intel): a provider that recomputes to nothing leaves ta…
Sonnet 5.5
minor
⚠
4 days ago