test_intel_experiment.py
python
sha256:64d53876b5299082b6b4bfbe92721976ec7bb5f1d9a88fdf6eafc1490148d27c
fix(intel): explicit, fail-closed deployment identity repla…
Sonnet 5.5
minor
⚠ breaking
4 days ago
| 1 | """Focused regression tests for the localhost intel.code experiment (``musehub.intel_experiment``). |
| 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 -m pytest tests/test_intel_experiment.py |
| 7 | |
| 8 | Covers: disabled by default, explicit single-repository selection, no historical queue draining, no concurrent |
| 9 | duplicate, truthful success/failure reporting, the hard-stop mechanism, and no effect on unrelated job types. |
| 10 | """ |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import hashlib |
| 14 | import msgpack |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import textwrap |
| 18 | from datetime import datetime, timedelta, timezone |
| 19 | |
| 20 | import pytest |
| 21 | import pytest_asyncio |
| 22 | from sqlalchemy import func, select, text |
| 23 | |
| 24 | from musehub.core.genesis import compute_job_id |
| 25 | from musehub.db.musehub_intel_models import MusehubIntelResult, MusehubSymbolHistoryEntry, MusehubSymbolIntel |
| 26 | from musehub.db.musehub_jobs_models import MusehubBackgroundJob |
| 27 | from musehub.db.musehub_repo_models import MusehubBranch, MusehubCommit, MusehubCommitRef, MusehubObject, MusehubSnapshot |
| 28 | from musehub import intel_experiment as exp |
| 29 | from musehub.services.musehub_jobs import _MAX_ATTEMPTS, claim_next_job, reclaim_stale_jobs |
| 30 | from tests.factories import create_repo |
| 31 | |
| 32 | ENABLED = {exp.ENABLE_ENV: "1", "MUSEHUB_DEPLOYMENT": "local"} |
| 33 | |
| 34 | |
| 35 | @pytest_asyncio.fixture |
| 36 | async def session_factory(db_session, session_factory): # noqa: F811 - deliberate override |
| 37 | """The shared factory, but only after ``db_session`` has truncated every table: each test starts empty.""" |
| 38 | return session_factory |
| 39 | |
| 40 | |
| 41 | def _cid(seed: str) -> str: |
| 42 | return "sha256:" + hashlib.sha256(seed.encode()).hexdigest() |
| 43 | |
| 44 | |
| 45 | async def _history(session, repo_id: str, *, n: int = 6, with_ops: bool = True, with_snapshots: bool = False) -> str: |
| 46 | """Give ``repo_id`` a linear history of ``n`` commits (each inserting one symbol) and a ``main`` head; return the head. |
| 47 | |
| 48 | ``with_snapshots`` also gives every commit a real snapshot (the files ``src/mod0.py`` .. ``src/mod{i}.py``, readable from ``content_cache``), which the |
| 49 | snapshot providers and ``detect_refactor`` need: a commit without one is an unanalysable gap, not an empty commit.""" |
| 50 | base = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| 51 | parent: list[str] = [] |
| 52 | head = "" |
| 53 | for i in range(n): |
| 54 | head = _cid(f"{repo_id}:{i}") |
| 55 | ops = [{"op": "insert", "address": f"src/mod{i}.py::fn_{i}", "content_id": _cid(f"body{i}"), |
| 56 | "content_summary": f"added function fn_{i} L0-5"}] |
| 57 | snapshot_id = None |
| 58 | if with_snapshots: |
| 59 | manifest: dict[str, str] = {} |
| 60 | for j in range(i + 1): |
| 61 | source = f"def fn_{j}(x: int) -> int:\n return x + {j}\n".encode() |
| 62 | oid = hashlib.sha256(source).hexdigest() |
| 63 | if await session.get(MusehubObject, oid) is None: |
| 64 | session.add(MusehubObject(object_id=oid, path=f"src/mod{j}.py", size_bytes=len(source), storage_uri="pending", content_cache=source)) |
| 65 | manifest[f"src/mod{j}.py"] = oid |
| 66 | snapshot_id = _cid(f"snapshot:{repo_id}:{i}") |
| 67 | session.add(MusehubSnapshot(snapshot_id=snapshot_id, manifest_blob=msgpack.packb(manifest), directories=["src"], entry_count=len(manifest))) |
| 68 | await session.flush() |
| 69 | session.add(MusehubCommit( |
| 70 | commit_id=head, message=f"c{i}", author="gabriel", branch="main", parent_ids=list(parent), |
| 71 | snapshot_id=snapshot_id, timestamp=base + timedelta(minutes=i), |
| 72 | structured_delta={"ops": ops} if with_ops else None, |
| 73 | )) |
| 74 | session.add(MusehubCommitRef(repo_id=repo_id, commit_id=head)) |
| 75 | parent = [head] |
| 76 | await session.flush() |
| 77 | session.add(MusehubBranch(branch_id=_cid(f"branch:{repo_id}"), repo_id=repo_id, name="main", head_commit_id=head)) |
| 78 | await session.commit() |
| 79 | return head |
| 80 | |
| 81 | |
| 82 | async def _count(session, model, repo_id: str) -> int: |
| 83 | return (await session.execute(select(func.count()).select_from(model).where(model.repo_id == repo_id))).scalar_one() |
| 84 | |
| 85 | |
| 86 | async def _jobs(session, repo_id: str) -> list[MusehubBackgroundJob]: |
| 87 | return list((await session.execute( |
| 88 | select(MusehubBackgroundJob).where(MusehubBackgroundJob.repo_id == repo_id).order_by(MusehubBackgroundJob.created_at) |
| 89 | )).scalars()) |
| 90 | |
| 91 | |
| 92 | # --------------------------------------------------------------------------- |
| 93 | # Gates (no database) |
| 94 | # --------------------------------------------------------------------------- |
| 95 | |
| 96 | DB = "postgresql+asyncpg://musehub:pw@postgres:5432/musehub" |
| 97 | |
| 98 | |
| 99 | @pytest.mark.no_db |
| 100 | def test_disabled_by_default() -> None: |
| 101 | with pytest.raises(exp.ExperimentRefused, match="disabled by default"): |
| 102 | exp.check_enabled({}, DB) |
| 103 | with pytest.raises(exp.ExperimentRefused, match="disabled by default"): |
| 104 | exp.check_enabled({exp.ENABLE_ENV: "true"}, DB) # only the literal "1" enables it |
| 105 | |
| 106 | |
| 107 | @pytest.mark.no_db |
| 108 | @pytest.mark.parametrize("url", [ |
| 109 | "postgresql+asyncpg://u:[email protected]:5432/musehub", |
| 110 | "postgresql+asyncpg://u:[email protected]:5432/musehub", |
| 111 | "postgresql+asyncpg://u:[email protected]/musehub", |
| 112 | None, |
| 113 | ]) |
| 114 | def test_non_local_or_missing_database_refused(url: str | None) -> None: |
| 115 | with pytest.raises(exp.ExperimentRefused): |
| 116 | exp.check_enabled(ENABLED, url) |
| 117 | |
| 118 | |
| 119 | @pytest.mark.no_db |
| 120 | @pytest.mark.parametrize("host", ["postgres", "localhost", "127.0.0.1"]) |
| 121 | def test_local_database_allowed_when_enabled(host: str) -> None: |
| 122 | exp.check_enabled(ENABLED, f"postgresql+asyncpg://u:p@{host}:5432/musehub") |
| 123 | |
| 124 | |
| 125 | @pytest.mark.no_db |
| 126 | def test_commit_cap_parsing() -> None: |
| 127 | assert exp.max_commits({}) == exp.DEFAULT_MAX_COMMITS |
| 128 | assert exp.max_commits({exp.MAX_COMMITS_ENV: "50"}) == 50 |
| 129 | for bad in ("abc", "0", "-3"): |
| 130 | with pytest.raises(exp.ExperimentRefused): |
| 131 | exp.max_commits({exp.MAX_COMMITS_ENV: bad}) |
| 132 | |
| 133 | |
| 134 | @pytest.mark.no_db |
| 135 | def test_hard_stop_is_a_process_signal_not_a_thread_timeout() -> None: |
| 136 | """SIGALRM's default action ends a CPU-bound process that a thread-based timeout could not interrupt.""" |
| 137 | code = textwrap.dedent(""" |
| 138 | import signal, time |
| 139 | signal.alarm(1) |
| 140 | while True: # CPU-bound, never yields: only a signal can stop this |
| 141 | sum(range(10_000)) |
| 142 | """) |
| 143 | started = datetime.now() |
| 144 | proc = subprocess.run([sys.executable, "-c", code], timeout=20) |
| 145 | assert proc.returncode == -signal_number("SIGALRM") |
| 146 | assert (datetime.now() - started).total_seconds() < 10 |
| 147 | |
| 148 | |
| 149 | def signal_number(name: str) -> int: |
| 150 | import signal |
| 151 | return int(getattr(signal, name)) |
| 152 | |
| 153 | |
| 154 | @pytest.mark.no_db |
| 155 | def test_wrapper_runs_the_child_under_an_init_and_with_real_resource_limits() -> None: |
| 156 | """PID 1 discards unhandled signals, so without --init the SIGALRM hard stop never fires (found live 2026-11-13).""" |
| 157 | from pathlib import Path |
| 158 | script = (Path(__file__).resolve().parent.parent / "scripts" / "intel_experiment.sh").read_text() |
| 159 | for required in ("docker run -d --init", '--memory "$MEM" --memory-swap "$MEM"', '--cpus "$CPUS"', "--read-only", |
| 160 | "--cap-drop ALL", 'docker kill "$NAME"', "MUSEHUB_INTEL_EXPERIMENT:-}\" != \"1\""): |
| 161 | assert required in script, required |
| 162 | |
| 163 | |
| 164 | @pytest.mark.no_db |
| 165 | @pytest.mark.asyncio |
| 166 | async def test_run_experiment_arms_the_alarm_before_any_work(monkeypatch: pytest.MonkeyPatch) -> None: |
| 167 | calls: list[str] = [] |
| 168 | monkeypatch.setattr(exp.signal, "alarm", lambda n: calls.append(f"alarm:{n}") or 0) |
| 169 | |
| 170 | async def fake_init_db(**_: object) -> None: |
| 171 | calls.append("init_db") |
| 172 | |
| 173 | async def fake_close_db() -> None: |
| 174 | calls.append("close_db") |
| 175 | |
| 176 | async def fake_execute_run(*_: object) -> dict[str, object]: |
| 177 | calls.append("execute_run") |
| 178 | return {"ok": True} |
| 179 | |
| 180 | import musehub.db.database as database |
| 181 | monkeypatch.setattr(database, "init_db", fake_init_db) |
| 182 | monkeypatch.setattr(database, "close_db", fake_close_db) |
| 183 | monkeypatch.setattr(exp, "execute_run", fake_execute_run) |
| 184 | monkeypatch.setenv("DATABASE_URL", DB) |
| 185 | await exp.run_experiment("sha256:x", 42, {**ENABLED, "DATABASE_URL": DB}) |
| 186 | assert calls == ["alarm:42", "init_db", "execute_run", "alarm:0", "close_db"] |
| 187 | |
| 188 | |
| 189 | # --------------------------------------------------------------------------- |
| 190 | # Explicit single-repository selection |
| 191 | # --------------------------------------------------------------------------- |
| 192 | |
| 193 | @pytest.mark.asyncio |
| 194 | async def test_unknown_repository_refused(session_factory) -> None: |
| 195 | async with session_factory() as s: |
| 196 | with pytest.raises(exp.ExperimentRefused, match="does not exist"): |
| 197 | await exp.resolve_target(s, "sha256:nope", 300) |
| 198 | |
| 199 | |
| 200 | @pytest.mark.asyncio |
| 201 | async def test_large_named_repositories_are_refused_even_when_small(session_factory) -> None: |
| 202 | async with session_factory() as s: |
| 203 | repo = await create_repo(s, owner="gabriel", slug="musehub") |
| 204 | await _history(s, repo.repo_id, n=5) |
| 205 | with pytest.raises(exp.ExperimentRefused, match="refused by name"): |
| 206 | await exp.resolve_target(s, repo.repo_id, 300) |
| 207 | |
| 208 | |
| 209 | @pytest.mark.asyncio |
| 210 | async def test_repository_over_commit_cap_refused(session_factory) -> None: |
| 211 | async with session_factory() as s: |
| 212 | repo = await create_repo(s, slug="intel-fixture-big") |
| 213 | await _history(s, repo.repo_id, n=8) |
| 214 | with pytest.raises(exp.ExperimentRefused, match="cap is 5"): |
| 215 | await exp.resolve_target(s, repo.repo_id, 5) |
| 216 | |
| 217 | |
| 218 | @pytest.mark.asyncio |
| 219 | async def test_repository_without_head_refused(session_factory) -> None: |
| 220 | async with session_factory() as s: |
| 221 | repo = await create_repo(s, slug="intel-fixture-headless") |
| 222 | with pytest.raises(exp.ExperimentRefused, match="no head"): |
| 223 | await exp.resolve_target(s, repo.repo_id, 300) |
| 224 | |
| 225 | |
| 226 | @pytest.mark.asyncio |
| 227 | async def test_refusal_creates_no_job(session_factory) -> None: |
| 228 | async with session_factory() as s: |
| 229 | repo = await create_repo(s, owner="gabriel", slug="muse") |
| 230 | await _history(s, repo.repo_id, n=5) |
| 231 | rid = repo.repo_id |
| 232 | with pytest.raises(exp.ExperimentRefused): |
| 233 | await exp.execute_run(session_factory, rid, 300, 60) |
| 234 | async with session_factory() as s: |
| 235 | assert await _jobs(s, rid) == [] |
| 236 | |
| 237 | |
| 238 | # --------------------------------------------------------------------------- |
| 239 | # Truthful success / failure |
| 240 | # --------------------------------------------------------------------------- |
| 241 | |
| 242 | @pytest.mark.asyncio |
| 243 | async def test_successful_run_records_execution_and_rows(session_factory) -> None: |
| 244 | async with session_factory() as s: |
| 245 | repo = await create_repo(s, slug="intel-fixture-ok") |
| 246 | head = await _history(s, repo.repo_id, n=6) |
| 247 | rid = repo.repo_id |
| 248 | |
| 249 | report = await exp.execute_run(session_factory, rid, 300, 60) |
| 250 | |
| 251 | assert report["ok"] is True and report["executed"] is True and report["outcome"] == "computed" |
| 252 | assert report["head"] == head |
| 253 | async with session_factory() as s: |
| 254 | assert await _count(s, MusehubSymbolHistoryEntry, rid) == 6 |
| 255 | assert await _count(s, MusehubSymbolIntel, rid) == 6 |
| 256 | types = set((await s.execute(select(MusehubIntelResult.intel_type).where(MusehubIntelResult.repo_id == rid))).scalars()) |
| 257 | assert types == {"code.intel_summary", "code.intel_snapshot"} |
| 258 | (job,) = await _jobs(s, rid) |
| 259 | assert job.status == "done" and job.job_type == "intel.code" |
| 260 | assert job.attempt == _MAX_ATTEMPTS # born exhausted: never retried, never claimable |
| 261 | assert job.result["executed"] is True # distinguishes real execution from a skipped 'done' |
| 262 | assert job.result["rows_written"]["symbol_intel"] == 6 |
| 263 | assert job.payload["experiment"] == exp.EXPERIMENT_NAME |
| 264 | |
| 265 | |
| 266 | @pytest.mark.asyncio |
| 267 | async def test_failure_is_recorded_failed_and_leaves_no_partial_rows(session_factory, monkeypatch: pytest.MonkeyPatch) -> None: |
| 268 | from musehub.services import musehub_intel_providers as providers |
| 269 | |
| 270 | async def boom(self, session, repo_id, ref, payload): |
| 271 | # Write something first, so the test proves a rollback — not merely an early failure. |
| 272 | from musehub.services.musehub_symbol_indexer import build_symbol_index |
| 273 | await build_symbol_index(session, repo_id, ref) |
| 274 | raise MemoryError("simulated exhaustion after rows were written") |
| 275 | |
| 276 | monkeypatch.setattr(providers.CodeProvider, "compute", boom) |
| 277 | async with session_factory() as s: |
| 278 | repo = await create_repo(s, slug="intel-fixture-fail") |
| 279 | await _history(s, repo.repo_id, n=6) |
| 280 | rid = repo.repo_id |
| 281 | |
| 282 | with pytest.raises(MemoryError): |
| 283 | await exp.execute_run(session_factory, rid, 300, 60) |
| 284 | |
| 285 | async with session_factory() as s: |
| 286 | (job,) = await _jobs(s, rid) |
| 287 | assert job.status == "failed" and job.status != "done" |
| 288 | assert job.result["executed"] is False and job.result["failed"] is True |
| 289 | assert "MemoryError" in (job.error or "") |
| 290 | assert await _count(s, MusehubSymbolHistoryEntry, rid) == 0 |
| 291 | assert await _count(s, MusehubSymbolIntel, rid) == 0 |
| 292 | assert await _count(s, MusehubIntelResult, rid) == 0 |
| 293 | |
| 294 | |
| 295 | @pytest.mark.asyncio |
| 296 | async def test_no_symbol_ops_is_a_failure_not_a_silent_success(session_factory) -> None: |
| 297 | async with session_factory() as s: |
| 298 | repo = await create_repo(s, slug="intel-fixture-empty") |
| 299 | await _history(s, repo.repo_id, n=6, with_ops=False) |
| 300 | rid = repo.repo_id |
| 301 | with pytest.raises(RuntimeError, match="did not compute"): |
| 302 | await exp.execute_run(session_factory, rid, 300, 60) |
| 303 | async with session_factory() as s: |
| 304 | (job,) = await _jobs(s, rid) |
| 305 | assert job.status == "failed" and job.result["executed"] is False |
| 306 | |
| 307 | |
| 308 | @pytest.mark.asyncio |
| 309 | async def test_second_run_reports_already_current_and_writes_nothing(session_factory) -> None: |
| 310 | async with session_factory() as s: |
| 311 | repo = await create_repo(s, slug="intel-fixture-twice") |
| 312 | await _history(s, repo.repo_id, n=6) |
| 313 | rid = repo.repo_id |
| 314 | await exp.execute_run(session_factory, rid, 300, 60) |
| 315 | second = await exp.execute_run(session_factory, rid, 300, 60) |
| 316 | assert second["outcome"] == "already_current" |
| 317 | assert all(v == 0 for v in second["rows_written"].values()) |
| 318 | |
| 319 | |
| 320 | # --------------------------------------------------------------------------- |
| 321 | # No concurrent duplicate, no queue interaction, no retries |
| 322 | # --------------------------------------------------------------------------- |
| 323 | |
| 324 | @pytest.mark.asyncio |
| 325 | async def test_concurrent_duplicate_is_refused_while_the_lock_is_held(session_factory) -> None: |
| 326 | async with session_factory() as s: |
| 327 | repo = await create_repo(s, slug="intel-fixture-lock") |
| 328 | await _history(s, repo.repo_id, n=6) |
| 329 | rid = repo.repo_id |
| 330 | |
| 331 | async with session_factory() as holder: |
| 332 | got = (await holder.execute( |
| 333 | text("SELECT pg_try_advisory_lock(:ns, :k)"), {"ns": exp._ADVISORY_NAMESPACE, "k": exp.advisory_key(rid)}, |
| 334 | )).scalar_one() |
| 335 | assert got is True |
| 336 | try: |
| 337 | with pytest.raises(exp.ExperimentRefused, match="holds the lock"): |
| 338 | await exp.execute_run(session_factory, rid, 300, 60) |
| 339 | finally: |
| 340 | await holder.execute(text("SELECT pg_advisory_unlock(:ns, :k)"), |
| 341 | {"ns": exp._ADVISORY_NAMESPACE, "k": exp.advisory_key(rid)}) |
| 342 | async with session_factory() as s: |
| 343 | assert await _jobs(s, rid) == [] |
| 344 | assert await _count(s, MusehubSymbolIntel, rid) == 0 |
| 345 | |
| 346 | |
| 347 | @pytest.mark.asyncio |
| 348 | async def test_live_experiment_row_blocks_a_second_run(session_factory) -> None: |
| 349 | async with session_factory() as s: |
| 350 | repo = await create_repo(s, slug="intel-fixture-live") |
| 351 | await _history(s, repo.repo_id, n=6) |
| 352 | rid = repo.repo_id |
| 353 | now = datetime.now(tz=timezone.utc) |
| 354 | s.add(MusehubBackgroundJob( |
| 355 | job_id=compute_job_id(rid, "x", now.isoformat()), repo_id=rid, job_type="intel.code", status="running", |
| 356 | created_at=now, claimed_at=now, attempt=_MAX_ATTEMPTS, payload={"experiment": exp.EXPERIMENT_NAME}, |
| 357 | )) |
| 358 | await s.commit() |
| 359 | with pytest.raises(exp.ExperimentRefused, match="already running"): |
| 360 | await exp.execute_run(session_factory, rid, 300, 60) |
| 361 | async with session_factory() as s: |
| 362 | assert await _count(s, MusehubSymbolIntel, rid) == 0 |
| 363 | |
| 364 | |
| 365 | @pytest.mark.asyncio |
| 366 | async def test_historical_queue_is_not_drained_and_other_work_is_untouched(session_factory) -> None: |
| 367 | """Pending jobs — same repo or another — keep their exact state, and no other job type or repository gains a row.""" |
| 368 | async with session_factory() as s: |
| 369 | target = await create_repo(s, slug="intel-fixture-queue") |
| 370 | other = await create_repo(s, slug="intel-fixture-other") |
| 371 | await _history(s, target.repo_id, n=6) |
| 372 | await _history(s, other.repo_id, n=6) |
| 373 | t, o = target.repo_id, other.repo_id |
| 374 | now = datetime.now(tz=timezone.utc) |
| 375 | seeded = [] |
| 376 | for i, (rid, jt) in enumerate([(t, "intel.code"), (o, "intel.code"), (t, "intel.code.coupling"), |
| 377 | (o, "fetch.mpack.prebuild"), (t, "mpack.index"), (o, "gc")]): |
| 378 | jid = compute_job_id(rid, jt, f"{now.isoformat()}:{i}") |
| 379 | s.add(MusehubBackgroundJob(job_id=jid, repo_id=rid, job_type=jt, status="pending", created_at=now, attempt=0, payload={})) |
| 380 | seeded.append(jid) |
| 381 | await s.commit() |
| 382 | |
| 383 | await exp.execute_run(session_factory, t, 300, 60) |
| 384 | |
| 385 | async with session_factory() as s: |
| 386 | rows = (await s.execute(select(MusehubBackgroundJob).where(MusehubBackgroundJob.job_id.in_(seeded)))).scalars().all() |
| 387 | assert len(rows) == 6 and all(r.status == "pending" and r.attempt == 0 and r.claimed_at is None for r in rows) |
| 388 | # exactly one new row in the whole table: the experiment's own |
| 389 | assert (await s.execute(select(func.count()).select_from(MusehubBackgroundJob))).scalar_one() == 7 |
| 390 | assert await _count(s, MusehubSymbolIntel, o) == 0 # the other repository was not analysed |
| 391 | assert await _count(s, MusehubIntelResult, o) == 0 |
| 392 | |
| 393 | |
| 394 | @pytest.mark.asyncio |
| 395 | async def test_experiment_row_can_never_be_claimed_or_requeued(session_factory) -> None: |
| 396 | async with session_factory() as s: |
| 397 | repo = await create_repo(s, slug="intel-fixture-claim") |
| 398 | rid = repo.repo_id |
| 399 | old = datetime.now(tz=timezone.utc) - timedelta(hours=1) |
| 400 | s.add(MusehubBackgroundJob( |
| 401 | job_id=compute_job_id(rid, "stale", old.isoformat()), repo_id=rid, job_type="intel.code", status="running", |
| 402 | created_at=old, claimed_at=old, attempt=_MAX_ATTEMPTS, payload={"experiment": exp.EXPERIMENT_NAME}, |
| 403 | )) |
| 404 | await s.commit() |
| 405 | |
| 406 | assert await claim_next_job(s) is None # running rows are never claimable |
| 407 | await reclaim_stale_jobs(s) # worker crash-recovery sweep |
| 408 | await s.commit() |
| 409 | (job,) = await _jobs(s, rid) |
| 410 | assert job.status == "failed" and job.status != "pending" # failed terminally, never re-queued or marked done |
| 411 | assert await claim_next_job(s) is None |
| 412 | |
| 413 | |
| 414 | # --------------------------------------------------------------------------- |
| 415 | # Fixture builder |
| 416 | # --------------------------------------------------------------------------- |
| 417 | |
| 418 | @pytest.mark.asyncio |
| 419 | async def test_fixture_references_source_commits_only_and_is_idempotent(session_factory) -> None: |
| 420 | async with session_factory() as s: |
| 421 | src = await create_repo(s, owner="gabriel", slug="muse") |
| 422 | await _history(s, src.repo_id, n=12) |
| 423 | src_id = src.repo_id |
| 424 | commits_before = (await s.execute(select(func.count()).select_from(MusehubCommit))).scalar_one() |
| 425 | src_refs_before = await _count(s, MusehubCommitRef, src_id) |
| 426 | |
| 427 | first = await exp.build_fixture(session_factory, "intel-fixture-small", 7) |
| 428 | again = await exp.build_fixture(session_factory, "intel-fixture-small", 7) |
| 429 | |
| 430 | assert first["created"] is True and first["commits"] == 7 and again["created"] is False |
| 431 | assert again["repo_id"] == first["repo_id"] |
| 432 | async with session_factory() as s: |
| 433 | assert (await s.execute(select(func.count()).select_from(MusehubCommit))).scalar_one() == commits_before # none copied |
| 434 | assert await _count(s, MusehubCommitRef, src_id) == src_refs_before # source untouched |
| 435 | assert await _count(s, MusehubCommitRef, first["repo_id"]) == 7 |
| 436 | # and it is analysable end to end |
| 437 | report = await exp.execute_run(session_factory, first["repo_id"], 300, 60) |
| 438 | assert report["executed"] is True and report["rows_written"]["symbol_intel"] == 7 |
| 439 | |
| 440 | |
| 441 | @pytest.mark.asyncio |
| 442 | async def test_fixture_head_reaches_every_commit_even_when_the_source_has_side_branches(session_factory) -> None: |
| 443 | """The fixture's N commits must all be reachable from its head. Picking "the oldest N by timestamp" and using the newest as head |
| 444 | made a 300-commit fixture whose head reached 20 commits: the scaling measurement silently measured the wrong size.""" |
| 445 | async with session_factory() as s: |
| 446 | src = await create_repo(s, owner="gabriel", slug="muse") |
| 447 | base = datetime(2026, 1, 1, tzinfo=timezone.utc) |
| 448 | |
| 449 | def commit(name: str, parents: list[str], minute: int) -> str: |
| 450 | c = _cid(f"dag:{name}") |
| 451 | s.add(MusehubCommit(commit_id=c, message=name, author="gabriel", branch="main", parent_ids=[_cid(f"dag:{p}") for p in parents], |
| 452 | snapshot_id=None, timestamp=base + timedelta(minutes=minute), |
| 453 | structured_delta={"ops": [{"op": "insert", "address": f"{name}.py::f", "content_id": _cid(name), |
| 454 | "content_summary": "added function f L0-1"}]})) |
| 455 | s.add(MusehubCommitRef(repo_id=src.repo_id, commit_id=c)) |
| 456 | return c |
| 457 | # main: m0..m9; a side branch forks at m1 and its commits are interleaved in time with main's |
| 458 | for i in range(10): |
| 459 | commit(f"m{i}", [f"m{i-1}"] if i else [], i * 10) |
| 460 | commit("s0", ["m1"], 25) |
| 461 | commit("s1", ["s0"], 35) |
| 462 | commit("s2", ["s1"], 45) |
| 463 | await s.commit() |
| 464 | |
| 465 | made = await exp.build_fixture(session_factory, "intel-fixture-dag", 8) |
| 466 | assert made["commits"] == 8 and made["reachable_commits"] == 8 |
| 467 | async with session_factory() as s: |
| 468 | head = (await s.execute(select(MusehubBranch.head_commit_id).where(MusehubBranch.repo_id == made["repo_id"]))).scalar_one() |
| 469 | assert head == made["head"] |
| 470 | report = await exp.execute_run(session_factory, made["repo_id"], 300, 60) |
| 471 | assert report["commits"] == 8 and report["reachable_commits"] == 8 |
| 472 | |
| 473 | |
| 474 | @pytest.mark.asyncio |
| 475 | async def test_fixture_slug_and_size_guards(session_factory) -> None: |
| 476 | with pytest.raises(exp.ExperimentRefused, match="must start with"): |
| 477 | await exp.build_fixture(session_factory, "real-looking-repo", 10) |
| 478 | with pytest.raises(exp.ExperimentRefused, match="between 5"): |
| 479 | await exp.build_fixture(session_factory, "intel-fixture-huge", 5000) |
| 480 | |
| 481 | |
| 482 | # --------------------------------------------------------------------------- |
| 483 | # run-all: the whole pipeline for one repository, one provider at a time |
| 484 | # --------------------------------------------------------------------------- |
| 485 | |
| 486 | @pytest.mark.asyncio |
| 487 | async def test_run_all_executes_every_provider_in_dependency_order_and_records_each_step(session_factory, monkeypatch) -> None: |
| 488 | async with session_factory() as s: |
| 489 | repo = await create_repo(s, owner="gabriel", slug="all-providers") |
| 490 | await _history(s, repo.repo_id, n=8, with_snapshots=True) |
| 491 | repo_id = repo.repo_id |
| 492 | # the production job path opens its own sessions from the global engine: point it at the test database for this call |
| 493 | from musehub.config import settings |
| 494 | from musehub.db import database |
| 495 | from tests.conftest import _TEST_DATABASE_URL |
| 496 | monkeypatch.setattr(settings, "database_url", _TEST_DATABASE_URL) |
| 497 | await database.init_db(pool_size=1, max_overflow=1) |
| 498 | try: |
| 499 | report = await exp.execute_all(session_factory, repo_id, 300, 60) |
| 500 | finally: |
| 501 | await database.close_db() |
| 502 | assert [st["provider"] for st in report["steps"]] == list(exp.PIPELINE_ORDER) |
| 503 | by = {st["provider"]: st for st in report["steps"]} |
| 504 | assert by["intel.code"]["status"] == "done" and by["intel.code"]["outcome"] == "computed" |
| 505 | assert all(st["status"] == "done" for st in report["steps"]), [(st["provider"], st["status"], st.get("error") or st.get("reason")) for st in report["steps"] if st["status"] != "done"] |
| 506 | assert report["ok"] is True and report["published"]["code.intel_summary"]["ref"] == report["head"] |
| 507 | assert all("seconds" in st and "peak_rss_mib" in st for st in report["steps"]) |
File History
5 commits
sha256:64d53876b5299082b6b4bfbe92721976ec7bb5f1d9a88fdf6eafc1490148d27c
fix(intel): explicit, fail-closed deployment identity repla…
Sonnet 5.5
minor
⚠
4 days ago
sha256:ad83bfa8d46d529c26bc603709ff7471ac571b967e87521eb0dc4d662a78a2a1
fix(intel): the run-all report classifies every step instea…
Sonnet 5.5
patch
4 days ago
sha256:78744d379faf9e46bfc8a45b2998d385b92f232df4fd33a1d06bfe62efa0b1d2
feat(intel): run-all measures every provider through the pr…
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
sha256:0a96f5e99fe9a92a356e7503f94752135aa45cd0fe8c657fcb403422cb30ff43
feat(intel): localhost-only bounded intel.code experiment (…
Sonnet 5.5
patch
4 days ago