test_intel_branch_policy.py
python
sha256:70cb0ad300bccf1e11f6f0fb15b383c6b5c37a7ee3881ca12c0c0511d0a10de8
fix(intel): the intel.code pre-check looks up what the job …
Sonnet 5.5
patch
4 days ago
| 1 | """Branch-head policy — musehub#8 release gate. |
| 2 | |
| 3 | Policy: **analyse the revision the push event names; never silently substitute a newer timestamp or the default-branch head; a repository-wide result follows the |
| 4 | default branch.** A result is replaced only by a revision that DESCENDS from the published one, or — between divergent revisions, which have no order — by one on the |
| 5 | default branch's history over one that is not. Otherwise the existing valid result is kept (``divergent``). Pending jobs are retargeted only to a revision that |
| 6 | descends from the one they hold, so a push on another branch is never lost behind a refresh. |
| 7 | |
| 8 | Run ONLY this file, against a private Postgres (never the live hub database):: |
| 9 | |
| 10 | TEST_DATABASE_URL=postgresql+asyncpg://musehub:musehub@<private-host>:<port>/musehub_test python -P -m pytest tests/test_intel_branch_policy.py |
| 11 | |
| 12 | c0 ─ c1 ─ c2 main (default branch) |
| 13 | ├── f1 ─ f2 ─ f3 feature |
| 14 | └── g1 other |
| 15 | P1 A divergent revision NOT on the default branch never overwrites a published default-branch result. |
| 16 | P2 A default-branch revision replaces a published divergent feature result. |
| 17 | P3 A descendant always replaces its ancestor, whatever the branch. |
| 18 | P4 Of two divergent non-default revisions, the first published is kept. |
| 19 | P5 The pre-check and the persist step agree (kind ``obsolete`` / ``divergent`` / publishable). |
| 20 | E1 A push on a divergent branch does not retarget a pending job; it gets its own pending job. |
| 21 | E2 A push that descends from a pending job's revision retargets that job (one pending job, newest head). |
| 22 | E3 A late push of an ancestor of a pending revision creates nothing (already covered). |
| 23 | E4 Divergent pending jobs are tracked independently: a descendant of the feature job retargets only it. |
| 24 | """ |
| 25 | from __future__ import annotations |
| 26 | |
| 27 | import pytest |
| 28 | from sqlalchemy import select |
| 29 | from sqlalchemy.ext.asyncio import AsyncSession |
| 30 | |
| 31 | from musehub.db.musehub_intel_models import MusehubIntelResult |
| 32 | from musehub.db.musehub_jobs_models import MusehubBackgroundJob |
| 33 | from musehub.db.musehub_repo_models import MusehubBranch |
| 34 | from musehub.services import musehub_intel_providers as P |
| 35 | from musehub.services import musehub_jobs as J |
| 36 | from tests.factories import create_repo |
| 37 | from tests.test_intel_code_correctness import Dag, cid, ins |
| 38 | from tests.test_intel_job_state_machine import worker_db as pool_db # noqa: F401 - fixture: production engine on the test database |
| 39 | |
| 40 | TYPE = "intel.code.dead" |
| 41 | |
| 42 | |
| 43 | def dag() -> Dag: |
| 44 | d = Dag() |
| 45 | d.add("c0", [], [ins("a.py", "a", "a0")], 0) |
| 46 | d.add("c1", ["c0"], [ins("b.py", "b", "b0")], 10) |
| 47 | d.add("c2", ["c1"], [ins("c.py", "c", "c0")], 20) |
| 48 | d.add("f1", ["c1"], [ins("f.py", "f1", "f10")], 30) |
| 49 | d.add("f2", ["f1"], [ins("f.py", "f2", "f20")], 40) |
| 50 | d.add("f3", ["f2"], [ins("f.py", "f3", "f30")], 50) |
| 51 | d.add("g1", ["c1"], [ins("g.py", "g", "g0")], 60) |
| 52 | return d |
| 53 | |
| 54 | |
| 55 | async def repo_with_branches(s: AsyncSession, slug: str, *, with_default: bool = True) -> tuple[str, Dag]: |
| 56 | d = dag() |
| 57 | repo = await create_repo(s, owner="gabriel", slug=slug) |
| 58 | await d.install(s, repo.repo_id, ["c0", "c1", "c2", "f1", "f2", "f3", "g1"]) |
| 59 | if with_default: |
| 60 | s.add(MusehubBranch(branch_id=cid(f"b:main:{slug}"), repo_id=repo.repo_id, name="main", head_commit_id=d.commits_id("c2"))) |
| 61 | s.add(MusehubBranch(branch_id=cid(f"b:feature:{slug}"), repo_id=repo.repo_id, name="feature", head_commit_id=d.commits_id("f3"))) |
| 62 | await s.commit() |
| 63 | return repo.repo_id, d |
| 64 | |
| 65 | |
| 66 | async def stored_ref(s: AsyncSession, repo_id: str) -> str | None: |
| 67 | await s.rollback() |
| 68 | return (await s.execute(select(MusehubIntelResult.ref).where(MusehubIntelResult.repo_id == repo_id, MusehubIntelResult.intel_type == TYPE))).scalar_one_or_none() |
| 69 | |
| 70 | |
| 71 | async def publish(s: AsyncSession, repo_id: str, ref: str) -> list[str]: |
| 72 | out = await P.persist_intel_results(s, repo_id, ref, [(TYPE, {"v": ref[-6:]})]) |
| 73 | await s.commit() |
| 74 | return out |
| 75 | |
| 76 | |
| 77 | @pytest.mark.asyncio |
| 78 | async def test_p1_a_divergent_non_default_revision_never_overwrites_the_default_branch_result(db_session) -> None: |
| 79 | repo_id, d = await repo_with_branches(db_session, "pol-p1") |
| 80 | assert await publish(db_session, repo_id, d.commits_id("c2")) == [TYPE] |
| 81 | assert await publish(db_session, repo_id, d.commits_id("f2")) == [] |
| 82 | assert await publish(db_session, repo_id, d.commits_id("g1")) == [] |
| 83 | assert await stored_ref(db_session, repo_id) == d.commits_id("c2") |
| 84 | |
| 85 | |
| 86 | @pytest.mark.asyncio |
| 87 | async def test_p2_a_default_branch_revision_replaces_a_divergent_feature_result(db_session) -> None: |
| 88 | repo_id, d = await repo_with_branches(db_session, "pol-p2") |
| 89 | assert await publish(db_session, repo_id, d.commits_id("f2")) == [TYPE] # first publication: nothing to defend yet |
| 90 | assert await publish(db_session, repo_id, d.commits_id("c2")) == [TYPE] # the default branch outranks it |
| 91 | assert await stored_ref(db_session, repo_id) == d.commits_id("c2") |
| 92 | |
| 93 | |
| 94 | @pytest.mark.asyncio |
| 95 | async def test_p3_a_descendant_always_replaces_its_ancestor(db_session) -> None: |
| 96 | repo_id, d = await repo_with_branches(db_session, "pol-p3") |
| 97 | assert await publish(db_session, repo_id, d.commits_id("c1")) == [TYPE] |
| 98 | assert await publish(db_session, repo_id, d.commits_id("f2")) == [TYPE] # extends what is published, on a feature branch |
| 99 | assert await publish(db_session, repo_id, d.commits_id("f3")) == [TYPE] |
| 100 | assert await publish(db_session, repo_id, d.commits_id("f1")) == [] # an ancestor of the stored revision: rejected |
| 101 | assert await stored_ref(db_session, repo_id) == d.commits_id("f3") |
| 102 | |
| 103 | |
| 104 | @pytest.mark.asyncio |
| 105 | async def test_p4_of_two_divergent_non_default_revisions_the_first_is_kept(db_session) -> None: |
| 106 | repo_id, d = await repo_with_branches(db_session, "pol-p4", with_default=False) |
| 107 | assert await publish(db_session, repo_id, d.commits_id("f2")) == [TYPE] |
| 108 | assert await publish(db_session, repo_id, d.commits_id("g1")) == [] |
| 109 | assert await stored_ref(db_session, repo_id) == d.commits_id("f2") |
| 110 | |
| 111 | |
| 112 | @pytest.mark.asyncio |
| 113 | async def test_p5_the_precheck_and_the_persist_step_agree(db_session) -> None: |
| 114 | repo_id, d = await repo_with_branches(db_session, "pol-p5") |
| 115 | c1, c2, f2, g1 = (d.commits_id(n) for n in ("c1", "c2", "f2", "g1")) |
| 116 | assert await P.publication_block(db_session, repo_id, TYPE, c2) is None # nothing stored |
| 117 | await publish(db_session, repo_id, f2) |
| 118 | assert await P.publication_block(db_session, repo_id, TYPE, f2) is None # same revision |
| 119 | assert await P.publication_block(db_session, repo_id, TYPE, d.commits_id("f1")) == (f2, "obsolete") |
| 120 | assert await P.publication_block(db_session, repo_id, TYPE, g1) == (f2, "divergent") |
| 121 | assert await P.publication_block(db_session, repo_id, TYPE, c2) is None # the default branch outranks the feature result |
| 122 | assert c1 |
| 123 | |
| 124 | |
| 125 | async def pending(s: AsyncSession, repo_id: str, job_type: str = "intel.code") -> list[str]: |
| 126 | await s.rollback() |
| 127 | rows = (await s.execute(select(MusehubBackgroundJob).where(MusehubBackgroundJob.repo_id == repo_id, MusehubBackgroundJob.job_type == job_type, |
| 128 | MusehubBackgroundJob.status == "pending"))).scalars().all() |
| 129 | return sorted(r.payload["head"] for r in rows) |
| 130 | |
| 131 | |
| 132 | async def push(s: AsyncSession, repo_id: str, head: str, branch: str) -> None: |
| 133 | await J.enqueue_push_intel(s, repo_id, head, branch=branch) |
| 134 | await s.commit() |
| 135 | |
| 136 | |
| 137 | @pytest.mark.asyncio |
| 138 | async def test_e1_a_push_on_a_divergent_branch_gets_its_own_pending_job(db_session) -> None: |
| 139 | repo_id, d = await repo_with_branches(db_session, "pol-e1") |
| 140 | c2, f2 = d.commits_id("c2"), d.commits_id("f2") |
| 141 | await push(db_session, repo_id, c2, "main") |
| 142 | await push(db_session, repo_id, f2, "feature") |
| 143 | assert await pending(db_session, repo_id) == sorted([c2, f2]) # the old refresh retargeted the main job to the feature head: main's push was lost |
| 144 | |
| 145 | |
| 146 | @pytest.mark.asyncio |
| 147 | async def test_e2_a_push_that_descends_from_a_pending_revision_retargets_it(db_session) -> None: |
| 148 | repo_id, d = await repo_with_branches(db_session, "pol-e2") |
| 149 | await push(db_session, repo_id, d.commits_id("f1"), "feature") |
| 150 | await push(db_session, repo_id, d.commits_id("f3"), "feature") |
| 151 | assert await pending(db_session, repo_id) == [d.commits_id("f3")] |
| 152 | |
| 153 | |
| 154 | @pytest.mark.asyncio |
| 155 | async def test_e3_a_late_push_of_an_ancestor_creates_nothing(db_session) -> None: |
| 156 | repo_id, d = await repo_with_branches(db_session, "pol-e3") |
| 157 | await push(db_session, repo_id, d.commits_id("f3"), "feature") |
| 158 | await push(db_session, repo_id, d.commits_id("f1"), "feature") # out of order: f1 is an ancestor of the pending f3 |
| 159 | assert await pending(db_session, repo_id) == [d.commits_id("f3")] |
| 160 | |
| 161 | |
| 162 | @pytest.mark.asyncio |
| 163 | async def test_e4_divergent_pending_jobs_are_tracked_independently(db_session) -> None: |
| 164 | repo_id, d = await repo_with_branches(db_session, "pol-e4") |
| 165 | c2, f1, f3 = d.commits_id("c2"), d.commits_id("f1"), d.commits_id("f3") |
| 166 | await push(db_session, repo_id, c2, "main") |
| 167 | await push(db_session, repo_id, f1, "feature") |
| 168 | await push(db_session, repo_id, f3, "feature") |
| 169 | assert await pending(db_session, repo_id) == sorted([c2, f3]) # f3 retargeted the feature job only; main's job is untouched |
| 170 | |
| 171 | |
| 172 | # ── The index itself, through the production path (atomic: a refused revision leaves nothing behind) ─────────────────────────────────── |
| 173 | |
| 174 | def divergent_dag() -> tuple[Dag, list[str]]: |
| 175 | d = Dag() |
| 176 | d.add("r", [], [ins("core.py", "core", "core0")], 0) |
| 177 | d.add("x1", ["r"], [ins("x.py", "only_on_x", "x0")], 10) |
| 178 | d.add("y1", ["r"], [ins("y.py", "only_on_y", "y0")], 20) |
| 179 | return d, ["r", "x1", "y1"] |
| 180 | |
| 181 | |
| 182 | async def run_index(head: str, repo_id: str) -> tuple[str, dict]: |
| 183 | from musehub import worker |
| 184 | return await worker._run_intel_job("intel.code", repo_id, {"head": head, "branch": "main"}) |
| 185 | |
| 186 | |
| 187 | @pytest.mark.asyncio |
| 188 | async def test_i1_a_divergent_non_default_head_is_refused_before_it_is_rebuilt_and_leaves_no_torn_state(pool_db) -> None: |
| 189 | from tests.test_intel_code_correctness import snapshot_of |
| 190 | |
| 191 | d, names = divergent_dag() |
| 192 | kept = await create_repo(pool_db, owner="gabriel", slug="idx-kept") |
| 193 | ref = await create_repo(pool_db, owner="gabriel", slug="idx-ref") |
| 194 | await d.install(pool_db, kept.repo_id, names) |
| 195 | await d.install(pool_db, ref.repo_id, names) |
| 196 | await pool_db.commit() |
| 197 | assert (await run_index(d.commits_id("x1"), kept.repo_id))[0] == "done" |
| 198 | assert (await run_index(d.commits_id("x1"), ref.repo_id))[0] == "done" |
| 199 | status, result = await run_index(d.commits_id("y1"), kept.repo_id) # no default-branch rows: neither outranks, the published x1 is kept |
| 200 | assert status == "skipped" and "divergent" in str(result["reason"]) |
| 201 | await pool_db.rollback() |
| 202 | assert await snapshot_of(pool_db, kept.repo_id) == await snapshot_of(pool_db, ref.repo_id) # tables and result rows are still exactly x1's |
| 203 | |
| 204 | |
| 205 | @pytest.mark.asyncio |
| 206 | async def test_i2_a_default_branch_head_replaces_a_divergent_index_with_exactly_a_fresh_build_of_that_head(pool_db) -> None: |
| 207 | from tests.test_intel_code_correctness import snapshot_of |
| 208 | |
| 209 | d, names = divergent_dag() |
| 210 | flip = await create_repo(pool_db, owner="gabriel", slug="idx-flip") |
| 211 | fresh = await create_repo(pool_db, owner="gabriel", slug="idx-fresh") |
| 212 | await d.install(pool_db, flip.repo_id, names) |
| 213 | await d.install(pool_db, fresh.repo_id, names) |
| 214 | for repo_id in (flip.repo_id, fresh.repo_id): # y1 is the head of the default branch ('main'), x1 of a feature branch |
| 215 | pool_db.add(MusehubBranch(branch_id=cid(f"b:main:{repo_id}"), repo_id=repo_id, name="main", head_commit_id=d.commits_id("y1"))) |
| 216 | await pool_db.commit() |
| 217 | assert (await run_index(d.commits_id("x1"), flip.repo_id))[0] == "done" # published first, from the feature branch |
| 218 | assert (await run_index(d.commits_id("y1"), flip.repo_id))[0] == "done" # the default branch outranks it |
| 219 | assert (await run_index(d.commits_id("y1"), fresh.repo_id))[0] == "done" |
| 220 | await pool_db.rollback() |
| 221 | a, b = await snapshot_of(pool_db, flip.repo_id), await snapshot_of(pool_db, fresh.repo_id) |
| 222 | assert a == b # nothing from x1's side survives |
| 223 | assert all("only_on_x" not in addr for addr, *_ in a["symbol_intel"]) |
File History
2 commits
sha256:70cb0ad300bccf1e11f6f0fb15b383c6b5c37a7ee3881ca12c0c0511d0a10de8
fix(intel): the intel.code pre-check looks up what the job …
Sonnet 5.5
patch
4 days ago
sha256:07dade914422a4077032f3ead242320995c8fdedd1a3bb7f4af27708754ce7b4
fix(intel): branch-head policy - analyse the revision the p…
Sonnet 5.5
minor
⚠
4 days ago