gabriel / musehub public
test_intel_rebuild_route.py python
166 lines 9.0 KB
Raw
sha256:545266da3f09ffbfe65a21bfc233bb91fe37f83ab714dd2d07db1fee98c3988b feat(intel): always-on code intelligence for every reposito… Sonnet 5.5 minor ⚠ breaking 3 days ago
1 """``POST /repos/{id}/symbol-index/rebuild`` cannot bypass the intel execution boundary — musehub#8.
2
3 The route used to run ``build_symbol_index`` (a whole-history ORM load) inside the web process: no memory/CPU/wall ceiling, no deployment gate or allowlist, no
4 per-(repo, type) advisory lock, no attempt fencing, and a head chosen as the newest commit of ANY branch. It now only enqueues one ``intel.code`` job for the
5 default-branch head; the worker decides whether to run it (gate), runs it in the bounded child, under the lock and the fence.
6 """
7 from __future__ import annotations
8
9 import inspect
10 import re
11 import secrets
12 from datetime import datetime, timedelta, timezone
13
14 import pytest
15 from httpx import AsyncClient
16 from sqlalchemy import select
17 from sqlalchemy.ext.asyncio import AsyncSession
18
19 from musehub.db.musehub_jobs_models import MusehubBackgroundJob
20 from musehub.db.musehub_repo_models import MusehubBranch, MusehubCommit, MusehubCommitRef
21 from tests.factories import create_repo
22
23 URL = "/api/repos/{}/symbol-index/rebuild"
24
25
26 async def add_commit(db: AsyncSession, repo_id: str, commit_id: str, branch: str, ts: datetime) -> None:
27 db.add(MusehubCommit(commit_id=commit_id, branch=branch, parent_ids=[], message="m", author="gabriel", timestamp=ts))
28 db.add(MusehubCommitRef(repo_id=repo_id, commit_id=commit_id))
29
30
31 async def repo_with_branches(db: AsyncSession, slug: str):
32 """A repo whose default branch ``main`` is OLDER than a feature branch: the old route would have indexed the feature commit."""
33 repo = await create_repo(db, slug=slug)
34 now = datetime.now(timezone.utc)
35 await add_commit(db, repo.repo_id, f"{slug}-main", "main", now - timedelta(days=2))
36 await add_commit(db, repo.repo_id, f"{slug}-feature", "feature", now)
37 db.add(MusehubBranch(branch_id=secrets.token_hex(16), repo_id=repo.repo_id, name="main", head_commit_id=f"{slug}-main"))
38 db.add(MusehubBranch(branch_id=secrets.token_hex(16), repo_id=repo.repo_id, name="feature", head_commit_id=f"{slug}-feature"))
39 await db.commit()
40 return repo
41
42
43 async def jobs(db: AsyncSession, repo_id: str, job_type: str | None = "intel.code") -> list[MusehubBackgroundJob]:
44 """The repository's jobs; by default only its ``intel.code`` (index) jobs — the rebuild now also enqueues every provider (see the full-set tests)."""
45 q = select(MusehubBackgroundJob).where(MusehubBackgroundJob.repo_id == repo_id)
46 if job_type is not None:
47 q = q.where(MusehubBackgroundJob.job_type == job_type)
48 return list((await db.execute(q)).scalars())
49
50
51 def test_the_route_handler_no_longer_contains_the_indexer() -> None:
52 from musehub.api.routes.musehub import repos
53
54 src = inspect.getsource(repos.rebuild_symbol_index)
55 assert not re.search(r"\bbuild_symbol_index\b", src) and "persist_intel_results" not in src
56
57
58 @pytest.mark.asyncio
59 async def test_rebuild_never_runs_the_indexer_in_the_web_process(
60 client: AsyncClient, db_session: AsyncSession, auth_headers, monkeypatch,
61 ) -> None:
62 from musehub.services import musehub_symbol_indexer
63
64 def forbidden(*a, **k):
65 raise AssertionError("the indexer must not run inside the API process")
66
67 monkeypatch.setattr(musehub_symbol_indexer, "build_symbol_index", forbidden)
68 repo = await repo_with_branches(db_session, "rb-noinline")
69 resp = await client.post(URL.format(repo.repo_id), headers=auth_headers)
70 assert resp.status_code == 202, resp.text
71
72
73 @pytest.mark.asyncio
74 async def test_rebuild_enqueues_one_intel_code_job_for_the_default_branch_head(
75 client: AsyncClient, db_session: AsyncSession, auth_headers,
76 ) -> None:
77 repo = await repo_with_branches(db_session, "rb-enqueue")
78 resp = await client.post(URL.format(repo.repo_id), headers=auth_headers)
79 assert resp.status_code == 202, resp.text
80 body = resp.json()
81 assert body["status"] == "queued" and body["head"] == "rb-enqueue-main" # NOT the newer feature commit
82 rows = await jobs(db_session, repo.repo_id)
83 assert [(j.job_type, j.status, j.payload) for j in rows] == [("intel.code", "pending", {"head": "rb-enqueue-main", "branch": "main"})]
84
85
86 @pytest.mark.asyncio
87 async def test_rebuild_enqueues_the_index_and_every_registry_provider_once(client: AsyncClient, db_session: AsyncSession, auth_headers) -> None:
88 from musehub.services.musehub_intel_status import REQUIRED_JOB_TYPES
89
90 repo = await repo_with_branches(db_session, "rb-full")
91 for _ in range(3):
92 body = (await client.post(URL.format(repo.repo_id), headers=auth_headers)).json()
93 rows = [j for j in await jobs(db_session, repo.repo_id, None) if j.status == "pending"]
94 assert sorted(j.job_type for j in rows) == sorted(REQUIRED_JOB_TYPES) # the registry's set, each exactly once, however often it is asked
95 assert all(j.payload["head"] == "rb-full-main" for j in rows)
96 assert body["jobId"] is not None and body["providers"] == len(REQUIRED_JOB_TYPES) - 1
97
98
99 @pytest.mark.asyncio
100 async def test_rebuild_twice_does_not_stack_jobs(client: AsyncClient, db_session: AsyncSession, auth_headers) -> None:
101 repo = await repo_with_branches(db_session, "rb-dedup")
102 for _ in range(3):
103 assert (await client.post(URL.format(repo.repo_id), headers=auth_headers)).status_code == 202
104 assert len(await jobs(db_session, repo.repo_id)) == 1
105
106
107 @pytest.mark.asyncio
108 @pytest.mark.parametrize("allowlisted", [False, True])
109 async def test_rebuild_reports_whether_this_deployment_will_execute_it(
110 client: AsyncClient, db_session: AsyncSession, auth_headers, monkeypatch, allowlisted: bool,
111 ) -> None:
112 """Enqueueing is not execution: the worker's gate (deployment identity + allowlist) still decides, and the response does not pretend otherwise."""
113 repo = await repo_with_branches(db_session, f"rb-gate-{allowlisted}")
114 monkeypatch.setenv("MUSEHUB_INTEL_WORKER_REPOS", repo.repo_id if allowlisted else "")
115 monkeypatch.setenv("MUSEHUB_DEPLOYMENT", "production")
116 monkeypatch.setenv("MUSE_ENV", "production") # production executes only an exactly listed repository
117 body = (await client.post(URL.format(repo.repo_id), headers=auth_headers)).json()
118 assert body["executes"] is allowlisted
119
120
121 @pytest.mark.asyncio
122 async def test_rebuild_without_a_default_branch_head_is_404(client: AsyncClient, db_session: AsyncSession, auth_headers) -> None:
123 repo = await create_repo(db_session, slug="rb-empty")
124 await db_session.commit()
125 assert (await client.post(URL.format(repo.repo_id), headers=auth_headers)).status_code == 404
126 assert await jobs(db_session, repo.repo_id) == []
127
128
129 @pytest.mark.asyncio
130 async def test_rebuild_executes_only_for_an_allowlisted_repo_in_a_local_deployment(
131 client: AsyncClient, db_session: AsyncSession, auth_headers, monkeypatch,
132 ) -> None:
133 repo = await repo_with_branches(db_session, "rb-local-yes")
134 other = await repo_with_branches(db_session, "rb-local-no")
135 from musehub.config import settings
136
137 monkeypatch.setenv("MUSEHUB_DEPLOYMENT", "local")
138 monkeypatch.delenv("MUSE_ENV", raising=False) # what a local worker has
139 monkeypatch.setattr(settings, "database_url", "postgresql+asyncpg://u:p@localhost:5432/musehub")
140 monkeypatch.setenv("MUSEHUB_INTEL_WORKER_REPOS", repo.repo_id)
141 assert (await client.post(URL.format(repo.repo_id), headers=auth_headers)).json()["executes"] is True
142 assert (await client.post(URL.format(other.repo_id), headers=auth_headers)).json()["executes"] is False
143
144
145 @pytest.mark.asyncio
146 async def test_rebuild_is_not_swallowed_by_a_running_job_for_an_older_head(client: AsyncClient, db_session: AsyncSession, auth_headers) -> None:
147 """intel.code is not self-coalescing: a running job's head was fixed at enqueue time, so it cannot cover a different head."""
148 repo = await repo_with_branches(db_session, "rb-running")
149 db_session.add(MusehubBackgroundJob(job_id="running-old", repo_id=repo.repo_id, job_type="intel.code", payload={"head": "older", "branch": "main"},
150 status="running", created_at=datetime.now(timezone.utc), attempt=1))
151 await db_session.commit()
152 body = (await client.post(URL.format(repo.repo_id), headers=auth_headers)).json()
153 assert body["jobId"] is not None
154 pending = [j for j in await jobs(db_session, repo.repo_id) if j.status == "pending"]
155 assert [j.payload["head"] for j in pending] == ["rb-running-main"]
156
157
158 @pytest.mark.asyncio
159 async def test_rebuild_is_not_swallowed_by_a_pending_job_for_a_divergent_head(client: AsyncClient, db_session: AsyncSession, auth_headers) -> None:
160 repo = await repo_with_branches(db_session, "rb-divergent")
161 db_session.add(MusehubBackgroundJob(job_id="pending-feature", repo_id=repo.repo_id, job_type="intel.code", payload={"head": "rb-divergent-feature", "branch": "feature"},
162 status="pending", created_at=datetime.now(timezone.utc), attempt=0))
163 await db_session.commit()
164 body = (await client.post(URL.format(repo.repo_id), headers=auth_headers)).json()
165 assert body["jobId"] is not None
166 assert sorted(j.payload["head"] for j in await jobs(db_session, repo.repo_id)) == ["rb-divergent-feature", "rb-divergent-main"]
File History 4 commits
sha256:545266da3f09ffbfe65a21bfc233bb91fe37f83ab714dd2d07db1fee98c3988b feat(intel): always-on code intelligence for every reposito… Sonnet 5.5 minor ⚠ 3 days ago
sha256:cb660189f57c2e986c5f5cf076547a8e496b63e420c8fcce5e3beef78822783b fix(intel): correct stale production-gate statements; cover… Sonnet 5.5 3 days ago
sha256:c3e18b15a2c0061e5bf0cf1130ec534cfad3582e0abaf5750ae68b8093a1258d fix(intel): head-keyed rebuild dedup, named reap failure, f… Sonnet 5.5 minor ⚠ 3 days ago
sha256:b0a27572cbd45886c558887213f12b9e6051f3057ed101e713c045d84ce5db9c fix(intel): the symbol-index rebuild route enqueues, it no … Sonnet 5.5 minor ⚠ 3 days ago