Files
roboco/tests/integration/test_conventions_findings.py
T
16789c1ca7 Feature/architectural conventions standard (#243)
* feat(conventions): standard schema models + effective-map merge

* feat(conventions): tree-sitter Python classifier + placement checks

* feat(conventions): TS classifier, hygiene/custom checks, runner + CLI

* feat(conventions): ROBOCO_CONVENTIONS_ENABLED flag + cache table + migration

* feat(conventions): repo auto-scan + scaffold draft renderer

* feat(conventions): ConventionsService (cache/baseline/ambient/scaffold/restore)

* feat(conventions): auto-scaffold on project registration (flag-gated)

* feat(conventions): TaskDescription.constraints + auto-baseline attach

* feat(conventions): ambient architecture-map injection at spawn

* test(conventions): subprocess CLI smoke for the agent-image entrypoint

* feat(conventions): block i_am_done on block-level convention violations

* feat(conventions): block pr_pass on unresolved convention violations

* feat(conventions): surface convention findings into QA evidence

* docs(prompts): convention awareness for PO/Intake/Dev/QA/PR-reviewer

* feat(conventions): panel Conventions tab + flag toggle + parity

* test(conventions): end-to-end block, fix, and waiver through the gate

* refactor(conventions): extract pr_pass guards to keep pr_gate under the gate

* style(conventions): format the baseline-constraints attach in task.create

* test(conventions): type-annotate test helpers for the full mypy gate

* build(conventions): ignore types-PyYAML in deptry (mypy-only type stub)

* docs(conventions): document the standard in CLAUDE.md + PM prompt awareness

* fix(conventions): baseline constraints are non-suppressible (dedup-append)

* feat(conventions): scaffold on first workspace clone (threaded workspace)

* feat(conventions): multi-project ambient map for PO/Intake (per-product)

* feat(conventions): persist findings + violations-feed route (migration 044)

* feat(conventions): panel violations feed in the Conventions tab

* test(conventions): intake-spawn mock accepts the ambient layer kwarg

* fix(docker): ollama-init best-effort pull, gate startup on cached models present

A degraded/slow ollama registry made the model manifest re-check fail under
set -e, so ollama-init exited 1 and blocked the orchestrator's
service_completed_successfully gate — taking the whole stack down even though
both models were already cached. Pulls are now best-effort; success is gated on
the models being present, so a flaky registry can't down a cached deployment.

* refactor(content): drop dead TaskDescription.with_baseline_constraints

The structured baseline-merge helper had zero production callers. Project-task baseline constraints are attached by the wired string backstop (TaskService._attach_baseline_constraints), and a real task is free-form prose that cannot form a valid TaskDescription (requires a non-trivial objective + non-empty the_work), so the helper was unreachable from any live path — a leftover from the structured-merge -> string-append design pivot. Removing it leaves a single enforcement path. The constraints field itself stays: it is a member of the well-formed-spec schema (Objective / What This Builds / The Work / Notes / Constraints / Acceptance Criteria), rendered by render_markdown and unit-tested.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-06-22 12:37:46 +02:00

161 lines
5.0 KiB
Python

"""Convention-findings persistence + the violations-feed route."""
from __future__ import annotations
from http import HTTPStatus
from typing import TYPE_CHECKING, cast
from uuid import UUID, uuid4
import pytest_asyncio
from fastapi import FastAPI
from httpx import ASGITransport, AsyncClient
from roboco.api.deps import get_agent_context, get_db
from roboco.api.routes.project import router as project_router
from roboco.db.tables import AgentTable, ProjectTable
from roboco.models import AgentRole, AgentStatus, Team
from roboco.models.permissions import AgentContext
from roboco.services.conventions import get_conventions_service
if TYPE_CHECKING:
from collections.abc import AsyncIterator
from sqlalchemy.ext.asyncio import AsyncSession
_HDR = {"X-Agent-ID": str(uuid4()), "X-Agent-Role": "main_pm"}
_FINDINGS = [
{
"file": "app/routers/u.py",
"line": 2,
"kind": "model",
"rule": "no_models_in_routers",
"level": "block",
"message": "model in router",
"fix_hint": "move it",
},
{
"file": "app/routers/u.py",
"line": 9,
"kind": None,
"rule": "no_inline_comments",
"level": "warn",
"message": "inline comment",
"fix_hint": "remove",
},
]
async def _seed_project(db: AsyncSession) -> ProjectTable:
agent = AgentTable(
id=uuid4(),
name="Dev",
slug=f"be-dev-{uuid4().hex[:8]}",
role=AgentRole.DEVELOPER,
team=Team.BACKEND,
status=AgentStatus.ACTIVE,
model_config={},
system_prompt="dev",
capabilities=[],
permissions={},
metrics={},
)
db.add(agent)
await db.flush()
project = ProjectTable(
id=uuid4(),
name="C-Proj",
slug=f"c-proj-{uuid4().hex[:8]}",
git_url="https://example.com/r.git",
assigned_cell=Team.BACKEND,
created_by=agent.id,
)
db.add(project)
await db.flush()
return project
async def test_record_then_recent_findings(db_session: AsyncSession) -> None:
project = await _seed_project(db_session)
svc = get_conventions_service(db_session)
pid = UUID(str(project.id))
await svc.record_findings(pid, uuid4(), _FINDINGS)
recent = await svc.recent_findings(pid)
assert len(recent) == len(_FINDINGS)
rules = {f["rule"] for f in recent}
assert rules == {"no_models_in_routers", "no_inline_comments"}
assert all(f["detected_at"] for f in recent)
async def test_record_replaces_prior_findings_for_task(
db_session: AsyncSession,
) -> None:
project = await _seed_project(db_session)
svc = get_conventions_service(db_session)
pid = UUID(str(project.id))
task = uuid4()
await svc.record_findings(pid, task, _FINDINGS)
await svc.record_findings(pid, task, _FINDINGS[:1]) # latest wins
recent = await svc.recent_findings(pid)
assert len(recent) == len(_FINDINGS[:1])
assert recent[0]["rule"] == "no_models_in_routers"
async def test_record_skips_malformed_entries(db_session: AsyncSession) -> None:
project = await _seed_project(db_session)
svc = get_conventions_service(db_session)
pid = UUID(str(project.id))
# a could_not_run entry (no file/rule) must not be recorded
await svc.record_findings(pid, uuid4(), [{"could_not_run": True, "reason": "x"}])
assert await svc.recent_findings(pid) == []
@pytest_asyncio.fixture
async def client(db_session: AsyncSession) -> AsyncIterator[AsyncClient]:
agent = AgentTable(
id=uuid4(),
name="MainPM",
slug=f"main-pm-{uuid4().hex[:8]}",
role=AgentRole.MAIN_PM,
team=None,
status=AgentStatus.ACTIVE,
model_config={},
system_prompt="pm",
capabilities=[],
permissions={},
metrics={},
)
db_session.add(agent)
await db_session.flush()
app = FastAPI()
app.include_router(project_router, prefix="/api/projects")
async def _override_db() -> AsyncIterator[AsyncSession]:
yield db_session
async def _override_agent() -> AgentContext:
return AgentContext(
agent_id=cast("UUID", agent.id), role=AgentRole.MAIN_PM, team=None
)
app.dependency_overrides[get_db] = _override_db
app.dependency_overrides[get_agent_context] = _override_agent
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as c:
yield c
app.dependency_overrides.clear()
async def test_findings_route_returns_recorded(
db_session: AsyncSession, client: AsyncClient
) -> None:
project = await _seed_project(db_session)
pid = UUID(str(project.id))
await get_conventions_service(db_session).record_findings(pid, uuid4(), _FINDINGS)
resp = await client.get(
f"/api/projects/{project.id}/conventions/findings", headers=_HDR
)
assert resp.status_code == HTTPStatus.OK
body = resp.json()
assert len(body) == len(_FINDINGS)
assert {f["rule"] for f in body} == {"no_models_in_routers", "no_inline_comments"}