"""TDD — intel.code job scales cleanly at 1, 5, 50, 500 commits. Each test creates a repo with N commits (each with structured_delta), runs build_symbol_index, and asserts: 1. Completes without error 2. Returns code.intel_summary and code.intel_snapshot 3. Writes rows to musehub_symbol_intel Scale ladder: 1 → 5 → 50 → 500 The first failing test tells us exactly where the ceiling is. """ from __future__ import annotations import datetime import hashlib import time import pytest from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from muse.core.types import fake_id from musehub.db.musehub_intel_models import MusehubSymbolHistoryEntry, MusehubSymbolIntel from musehub.db.musehub_repo_models import MusehubCommit, MusehubCommitRef from tests.factories import create_repo # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _structured_delta(n_symbols: int, seed: str = "") -> dict: """Produce a realistic structured_delta for N symbols.""" return { "ops": [ { "op": "insert" if i % 3 != 1 else "replace", "address": f"musehub/services/svc_{seed}_{i}.py::func_{i}", "new_content_id": fake_id(f"{seed}-{i}-content"), "symbol_kind": "function", } for i in range(n_symbols) ] } async def _populate_repo( db: AsyncSession, repo_id: str, n_commits: int, symbols_per_commit: int, ) -> str: """Create n_commits on the repo, each touching symbols_per_commit symbols.""" parent_id: str | None = None tip = "" for i in range(n_commits): commit_id = fake_id(f"{repo_id}-commit-{i}") commit = MusehubCommit( commit_id=commit_id, branch="main", parent_ids=[parent_id] if parent_id else [], message=f"commit {i}", author="gabriel", timestamp=datetime.datetime.now(tz=datetime.timezone.utc), structured_delta=_structured_delta(symbols_per_commit, seed=f"c{i}"), ) db.add(commit) db.add(MusehubCommitRef(repo_id=repo_id, commit_id=commit_id)) parent_id = commit_id tip = commit_id await db.flush() return tip def _assert_fast(elapsed: float, limit: float, label: str) -> None: assert elapsed < limit, ( f"{label} took {elapsed:.2f}s — limit is {limit:.1f}s. " f"Bottleneck is in build_symbol_index; use the [intel.code] timing logs to pinpoint." ) # --------------------------------------------------------------------------- # Scale tests # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_intel_code_1_commit(db_session: AsyncSession) -> None: """Baseline: 1 commit, 5 symbols — must complete in <2s.""" from musehub.services.musehub_symbol_indexer import build_symbol_index repo = await create_repo(db_session, name="intel-scale-1", owner="gabriel", visibility="public") tip = await _populate_repo(db_session, repo.repo_id, n_commits=1, symbols_per_commit=5) await db_session.flush() t0 = time.monotonic() result = await build_symbol_index(db_session, repo.repo_id, tip) elapsed = time.monotonic() - t0 assert any(t == "code.intel_summary" for t, _ in result), "must return code.intel_summary" rows = (await db_session.execute( select(MusehubSymbolIntel).where(MusehubSymbolIntel.repo_id == repo.repo_id) )).scalars().all() assert len(rows) == 5, f"expected 5 symbol_intel rows, got {len(rows)}" _assert_fast(elapsed, 2.0, "1 commit / 5 symbols") @pytest.mark.asyncio async def test_intel_code_5_commits(db_session: AsyncSession) -> None: """5 commits × 10 symbols each — must complete in <5s.""" from musehub.services.musehub_symbol_indexer import build_symbol_index repo = await create_repo(db_session, name="intel-scale-5", owner="gabriel", visibility="public") tip = await _populate_repo(db_session, repo.repo_id, n_commits=5, symbols_per_commit=10) await db_session.flush() t0 = time.monotonic() result = await build_symbol_index(db_session, repo.repo_id, tip) elapsed = time.monotonic() - t0 assert any(t == "code.intel_summary" for t, _ in result) _assert_fast(elapsed, 5.0, "5 commits / 10 symbols each") @pytest.mark.asyncio async def test_intel_code_50_commits(db_session: AsyncSession) -> None: """50 commits × 20 symbols each — must complete in <15s.""" from musehub.services.musehub_symbol_indexer import build_symbol_index repo = await create_repo(db_session, name="intel-scale-50", owner="gabriel", visibility="public") tip = await _populate_repo(db_session, repo.repo_id, n_commits=50, symbols_per_commit=20) await db_session.flush() t0 = time.monotonic() result = await build_symbol_index(db_session, repo.repo_id, tip) elapsed = time.monotonic() - t0 assert any(t == "code.intel_summary" for t, _ in result) _assert_fast(elapsed, 15.0, "50 commits / 20 symbols each") @pytest.mark.asyncio async def test_intel_code_500_commits(db_session: AsyncSession) -> None: """500 commits × 30 symbols each — must complete in <60s (production SLA).""" from musehub.services.musehub_symbol_indexer import build_symbol_index repo = await create_repo(db_session, name="intel-scale-500", owner="gabriel", visibility="public") tip = await _populate_repo(db_session, repo.repo_id, n_commits=500, symbols_per_commit=30) await db_session.flush() t0 = time.monotonic() result = await build_symbol_index(db_session, repo.repo_id, tip) elapsed = time.monotonic() - t0 assert any(t == "code.intel_summary" for t, _ in result) _assert_fast(elapsed, 60.0, "500 commits / 30 symbols each")