gabriel / musehub public
test_intel_child_real.py python
125 lines 6.6 KB
Raw
sha256:3733f942311ecd1beecb0d6f112f3ebd87b2801a5a410bf35b2415a84a238f1b fix(intel): one-shot child, reap confirmed before the refer… Sonnet 5.5 minor ⚠ breaking 3 days ago
1 """The real bounded intel child against a private database — musehub#8 release gate R1.
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 python -P -m pytest tests/test_intel_child_real.py
6
7 R1a A job run through the real child (``python -m musehub.intel_child``) publishes exactly what the in-process path publishes.
8 R1b A child killed in the middle of its transaction (its persist step blocked on a table lock, then the wall limit) publishes NOTHING, and the
9 supervisor, i.e. the worker, survives and can run the next job.
10 """
11 from __future__ import annotations
12
13 import asyncio
14
15 import psutil
16 import pytest
17 from sqlalchemy import func, select, text
18
19 from musehub import intel_supervisor as S
20 from musehub.db import database
21 from musehub.db.musehub_intel_models import MusehubIntelLanguages
22 from tests.test_intel_job_state_machine import worker_db # noqa: F401 - fixture: production engine on the test database
23 from tests.test_intel_pipeline_e2e import EXPECTED_INTEL_JOB_TYPES
24 from tests.test_intel_snapshot_providers import world_with_side_branch
25
26
27 def real_child(**kw) -> S.IntelChild:
28 base = dict(memory_bytes=900 * 2**20, cpu_seconds=120, wall_seconds=60, poll_seconds=0.1)
29 base.update(kw)
30 return S.IntelChild(S.IntelChildLimits(**base))
31
32
33 async def languages_rows(repo_id: str) -> int:
34 async with database.AsyncSessionLocal() as s:
35 return (await s.execute(select(func.count()).select_from(MusehubIntelLanguages).where(MusehubIntelLanguages.repo_id == repo_id))).scalar_one()
36
37
38 @pytest.mark.asyncio
39 async def test_r1a_a_job_through_the_real_child_publishes_its_results(worker_db) -> None: # noqa: F811
40 w, c2, _side = await world_with_side_branch(worker_db, "child-real-a")
41 child = real_child()
42 try:
43 terminal, result = await child.run("intel.code.languages", w.repo_id, {"head": c2, "branch": "main"})
44 finally:
45 await child.close()
46 assert terminal == "done" and result["executed"] is True and result["outcome"] == "computed"
47 assert await languages_rows(w.repo_id) > 0
48 assert child.pid is None and child._proc is None, "N2: no child stays resident once its job has ended"
49
50
51 @pytest.mark.asyncio
52 async def test_r1b_a_child_killed_mid_transaction_publishes_nothing_and_the_worker_survives(worker_db) -> None: # noqa: F811
53 w, c2, _side = await world_with_side_branch(worker_db, "child-real-b")
54 table = MusehubIntelLanguages.__tablename__
55 child = real_child(wall_seconds=8)
56 async with database.AsyncSessionLocal() as locker:
57 await locker.execute(text(f"LOCK TABLE {table} IN ACCESS EXCLUSIVE MODE")) # the child's persist step now waits behind this lock
58 try:
59 with pytest.raises(S.IntelChildTimeout):
60 await child.run("intel.code.languages", w.repo_id, {"head": c2, "branch": "main"})
61 finally:
62 await locker.rollback()
63 assert child.pid is None # killed and reaped
64 await asyncio.sleep(0.5) # Postgres notices the dropped connection and rolls the child's transaction back
65 assert await languages_rows(w.repo_id) == 0
66 try:
67 terminal, _ = await child.run("intel.code.languages", w.repo_id, {"head": c2, "branch": "main"}) # a fresh child, same supervisor
68 finally:
69 await child.close()
70 assert terminal == "done" and await languages_rows(w.repo_id) > 0
71
72
73 # ── Full scenario through the real child ────────────────────────────────────────────────────────────────────────────────────────
74
75 @pytest.fixture
76 def real_child_in_worker(monkeypatch):
77 """Undo the suite's in-process stand-in: ``worker._process_one`` now hands every intel job to a genuine ``python -m musehub.intel_child``."""
78 from musehub import worker
79 child = real_child()
80 monkeypatch.setattr(worker, "_intel_child_for_worker", lambda: child)
81 return child
82
83
84 @pytest.mark.asyncio
85 async def test_r1c_the_whole_push_pipeline_runs_in_one_bounded_child_and_publishes_one_consistent_revision(
86 worker_db, real_child_in_worker, monkeypatch, client, # noqa: F811
87 ) -> None:
88 """Push → enqueue → claim → bounded child → index → the dependent providers → persistence → the Code Intelligence pages, one short-lived child per job."""
89 from musehub import worker
90 from musehub.db.musehub_intel_models import MusehubIntelResult
91 from musehub.db.musehub_jobs_models import MusehubBackgroundJob
92 from tests.test_intel_pipeline_e2e import ALLOW_ENV, build_repo, drain, push, statuses
93
94 w, c = await build_repo(worker_db)
95 monkeypatch.setenv(ALLOW_ENV, w.repo_id)
96 await push(worker_db, w.repo_id, c["c2"])
97 pids: list[int] = []
98 real_ensure = real_child_in_worker._ensure
99
100 async def ensure_and_record():
101 proc = await real_ensure()
102 pids.append(proc.pid)
103 return proc
104 monkeypatch.setattr(real_child_in_worker, "_ensure", ensure_and_record)
105
106 n = await drain(worker)
107 try:
108 st = await statuses(worker_db, w.repo_id)
109 intel = {t: v for t, v in st.items() if t.startswith("intel.")}
110 assert set(intel) == EXPECTED_INTEL_JOB_TYPES, set(intel) ^ EXPECTED_INTEL_JOB_TYPES # exactly the 16 intel jobs: none missing, none unexpected
111 assert all(v == ["done"] for v in intel.values()), st
112 assert n == len(st), (n, st) # and every job of the push was processed once
113 assert len(pids) == len(EXPECTED_INTEL_JOB_TYPES) == len(set(pids)), pids # N2: one short-lived child per job, none reused
114 assert not any(psutil.pid_exists(p) and psutil.Process(p).status() != psutil.STATUS_ZOMBIE for p in pids) # and none left running
115 async with database.AsyncSessionLocal() as s:
116 refs = {r.intel_type: r.ref for r in (await s.execute(select(MusehubIntelResult).where(MusehubIntelResult.repo_id == w.repo_id))).scalars()}
117 attempts = {j.attempt for j in (await s.execute(select(MusehubBackgroundJob).where(MusehubBackgroundJob.repo_id == w.repo_id))).scalars()}
118 assert refs["code.intel_summary"] == c["c2"]
119 assert all(refs[t] == c["c2"] for t in refs if t.startswith("intel.code.") and t != "intel.code.detect_refactor"), refs
120 assert attempts == {1} # nothing was retried: no spurious failures on the way
121 page = await client.get("/testuser/e2e/intel/gravity")
122 assert page.status_code == 200 and "core/a.py" in page.text
123 finally:
124 await real_child_in_worker.close()
125 assert real_child_in_worker.pid is None
File History 4 commits
sha256:3733f942311ecd1beecb0d6f112f3ebd87b2801a5a410bf35b2415a84a238f1b fix(intel): one-shot child, reap confirmed before the refer… Sonnet 5.5 minor ⚠ 3 days ago
sha256:e31a659ae921cd5892ab20f9e02f320cae182610f5495d00931b715fc31f43f5 test(intel): exact enqueue contract and provider-set assert… Sonnet 5.5 3 days ago
sha256:870cd46131e53e6460532e710d5e9cca3c503233c1d0a3b2b8f6c3dda00e3609 test(intel): the whole push pipeline through the real bound… Sonnet 5.5 4 days ago
sha256:f0ce9f515a530043854b2549a902d0df78837898e9e9439fe32abeee180bef85 feat(worker): run intel jobs in a bounded child process; th… Sonnet 5.5 patch 4 days ago