fix(test): get-or-create seed agents in self-heal DB tests (unbreak CI)

The self-heal origination tests blind-inserted the fixed-uuid foundation
system agent (and a main-pm agent). In the full CI suite the app lifespan
seeds + commits those agents first, so the insert hit a duplicate-key on
pk_agents — green in isolation, red in CI. Get-or-create both (by id / by
slug) so the tests pass whether or not the agents already exist. Verified
against the real ordering (an app-lifespan integration test before this
file): 187 passed.
This commit is contained in:
Renn F
2026-06-17 22:57:33 +02:00
parent 12b8625462
commit 32b6d72933
@@ -22,6 +22,7 @@ from roboco.services.notification import NotificationService
from roboco.services.self_heal_engine import SelfHealEngine from roboco.services.self_heal_engine import SelfHealEngine
from roboco.services.task import TaskService, get_task_service from roboco.services.task import TaskService, get_task_service
from roboco.services.telemetry import TelemetrySample from roboco.services.telemetry import TelemetrySample
from sqlalchemy import select
if TYPE_CHECKING: if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
@@ -53,23 +54,30 @@ def _breach(signal: str) -> TelemetrySample:
async def _seed_project(session: AsyncSession, slug: str = SLUG) -> None: async def _seed_project(session: AsyncSession, slug: str = SLUG) -> None:
"""Seed the system agent (FK target for created_by) + RoboCo's own project.""" """Seed the system agent (FK target for created_by) + RoboCo's own project.
session.add(
AgentTable( The system agent has a FIXED foundation uuid (origination hardcodes it as
id=SYSTEM_UUID, created_by). Another test in the full suite may have already committed it
name="System", into the session-scoped DB, so get-or-create by id — a plain insert collides
slug=f"system-{slug}", on pk_agents (green in isolation, red in the full CI run).
role=AgentRole.SYSTEM, """
team=None, if await session.get(AgentTable, SYSTEM_UUID) is None:
status=AgentStatus.ACTIVE, session.add(
model_config={}, AgentTable(
system_prompt="system", id=SYSTEM_UUID,
capabilities=[], name="System",
permissions={}, slug=f"system-{slug}",
metrics={}, role=AgentRole.SYSTEM,
team=None,
status=AgentStatus.ACTIVE,
model_config={},
system_prompt="system",
capabilities=[],
permissions={},
metrics={},
)
) )
) await session.flush()
await session.flush()
session.add( session.add(
ProjectTable( ProjectTable(
name="RoboCo", name="RoboCo",
@@ -218,23 +226,28 @@ async def test_ceo_approve_and_start_flips_the_confirmation_gate(
"""The opened task is unconfirmed (held out of dispatch); the CEO's """The opened task is unconfirmed (held out of dispatch); the CEO's
approve_and_start flips confirmed_by_human=True so it can then dispatch.""" approve_and_start flips confirmed_by_human=True so it can then dispatch."""
await _seed_project(db_session) await _seed_project(db_session)
# approve_and_start reassigns to the main-pm agent — seed it. # approve_and_start reassigns to the main-pm agent — get-or-create by slug
db_session.add( # (the full suite may already have committed a "main-pm" agent).
AgentTable( existing_pm = (
id=uuid4(), await db_session.execute(select(AgentTable).where(AgentTable.slug == "main-pm"))
name="Main PM", ).scalar_one_or_none()
slug="main-pm", if existing_pm is None:
role=AgentRole.MAIN_PM, db_session.add(
team=Team.MAIN_PM, AgentTable(
status=AgentStatus.ACTIVE, id=uuid4(),
model_config={}, name="Main PM",
system_prompt="pm", slug="main-pm",
capabilities=[], role=AgentRole.MAIN_PM,
permissions={}, team=Team.MAIN_PM,
metrics={}, status=AgentStatus.ACTIVE,
model_config={},
system_prompt="pm",
capabilities=[],
permissions={},
metrics={},
)
) )
) await db_session.flush()
await db_session.flush()
_enable(monkeypatch) _enable(monkeypatch)
# approve_and_start emits a stream event; stub it out (no bus in the test). # approve_and_start emits a stream event; stub it out (no bus in the test).
monkeypatch.setattr(TaskService, "_emit_task_event", AsyncMock()) monkeypatch.setattr(TaskService, "_emit_task_event", AsyncMock())