Files
roboco/tests/unit/api/routes/v1/test_flow_dev.py
T
879afc14a4 Board Program LEARN context, ruff 0.16, and verb-rejection observability (#700)
* fix(board): LEARN decisions name the item, not its per-cycle index

A cycle's reject reasons are rendered into the NEXT cycle's exploration
prompt, but the ref recorded alongside each reason was the item's stored
id (item-0/item-1) — a per-cycle index that means something different
every cycle and appears nowhere the explorer can resolve. The reason
survived the loop; what it was about did not.

Record the item's title instead, via a shared learn_ref() helper (falls
back to the id when title-less, and reads target_task_title for Scales,
whose items name the live task they mutate).

* chore(lint): satisfy ruff 0.16 — keyword-only signatures and markdown formatting

The dev toolchain resolved ruff 0.16.0, which stabilises PLR0917 (too many
positional arguments) and formats python code blocks inside markdown. Both
fired repo-wide and neither had anything to do with the code they flagged.

- 36 signatures gain a `*` so their tail arguments are keyword-only, and
  the 104 call sites that passed them positionally are converted. mypy was
  the safety net for the static ones; the full suite caught nine more that
  only bind at runtime (the MCP tool functions, whose real callers already
  pass named JSON arguments).
- 28 markdown files reformatted by 0.16's code-block formatter.
- One RUF036 (`None` mid-union) autofixed in the GitLab provider.

* fix(gateway): log the reason when a verb rejects

A rejected envelope rides an HTTP 200, its body is never logged, and there
is no trace table — so in the access log a verb an agent could not satisfy
looks identical to one that worked. On 2026-07-25 four Board Programs
(Periscope, Sentinel, Scales, Barfly) each POSTed their propose verb three
or four times, persisted nothing, and left their exploration tasks PENDING;
the reason was unrecoverable afterwards, from the logs or from the agents'
own transcripts.

Log error/message/remediate/missing plus the calling agent at
envelope_to_response — the one chokepoint every v1 flow and do route
returns through. Success envelopes stay silent.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-26 15:07:28 +02:00

227 lines
7.2 KiB
Python

"""Unit tests for /api/v1/flow/developer/* endpoints.
Uses a minimal FastAPI test client built from the new router only.
No DB required — Choreographer is mocked.
"""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from roboco.api.deps import get_choreographer
from roboco.api.routes.v1.flow_dev import router
_HTTP_200 = 200
_HTTP_422 = 422
_AGENT_ID = str(uuid4())
_TASK_ID = str(uuid4())
_HEADERS = {"X-Agent-ID": _AGENT_ID, "X-Agent-Role": "developer"}
def _make_envelope(status: str = "ok", task_id: str | None = None) -> MagicMock:
"""Return a mock Envelope whose as_dict() returns a predictable payload."""
env = MagicMock()
env.as_dict.return_value = {"status": status, "task_id": task_id, "next": "..."}
return env
def _build_app(mock_choreographer: MagicMock) -> FastAPI:
"""Build minimal FastAPI app with the flow_dev router and a mocked dep."""
app = FastAPI()
app.include_router(router)
app.dependency_overrides[get_choreographer] = lambda: mock_choreographer
return app
@pytest.mark.asyncio
async def test_give_me_work_returns_envelope() -> None:
"""POST /api/v1/flow/developer/give_me_work returns 200 with envelope shape."""
mock_chore = MagicMock()
mock_chore.give_me_work = AsyncMock(return_value=_make_envelope(status="idle"))
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/give_me_work",
json={},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
body = resp.json()
assert body["status"] == "idle"
mock_chore.give_me_work.assert_awaited_once()
@pytest.mark.asyncio
async def test_i_will_work_on_dispatches_task_id() -> None:
"""POST /api/v1/flow/developer/i_will_work_on forwards task_id and plan."""
mock_chore = MagicMock()
mock_chore.i_will_work_on = AsyncMock(
return_value=_make_envelope(status="in_progress", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/i_will_work_on",
json={"task_id": _TASK_ID, "plan": "implement the feature"},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
body = resp.json()
assert body["status"] == "in_progress"
mock_chore.i_will_work_on.assert_awaited_once()
call_args = mock_chore.i_will_work_on.call_args
# the verb's params are keyword-only past agent_id, so read the kwargs
assert str(call_args.kwargs["task_id"]) == _TASK_ID
assert call_args.kwargs["plan"] == "implement the feature"
@pytest.mark.asyncio
async def test_i_am_done_dispatches_task_and_notes() -> None:
"""POST /api/v1/flow/developer/i_am_done forwards task_id and notes."""
mock_chore = MagicMock()
mock_chore.i_am_done = AsyncMock(
return_value=_make_envelope(status="awaiting_qa", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/i_am_done",
json={"task_id": _TASK_ID, "notes": "all tests pass"},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
body = resp.json()
assert body["status"] == "awaiting_qa"
mock_chore.i_am_done.assert_awaited_once()
@pytest.mark.asyncio
async def test_i_am_blocked_dispatches_reason() -> None:
"""POST /api/v1/flow/developer/i_am_blocked forwards task_id and reason."""
mock_chore = MagicMock()
mock_chore.i_am_blocked = AsyncMock(
return_value=_make_envelope(status="blocked", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/i_am_blocked",
json={"task_id": _TASK_ID, "reason": "waiting for design spec"},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.i_am_blocked.assert_awaited_once()
assert mock_chore.i_am_blocked.call_args.args[2] == "waiting for design spec"
@pytest.mark.asyncio
async def test_i_am_idle_dispatches_agent_id() -> None:
"""POST /api/v1/flow/developer/i_am_idle delegates to Choreographer.i_am_idle."""
mock_chore = MagicMock()
mock_chore.i_am_idle = AsyncMock(return_value=_make_envelope(status="idle"))
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/i_am_idle",
json={},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
body = resp.json()
assert body["status"] == "idle"
mock_chore.i_am_idle.assert_awaited_once()
def test_i_am_blocked_rejects_empty_reason() -> None:
"""POST i_am_blocked rejects empty reason (min_length=1)."""
mock_chore = MagicMock()
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/i_am_blocked",
json={"task_id": _TASK_ID, "reason": ""},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_422
@pytest.mark.asyncio
async def test_open_pr_dispatches_task_id() -> None:
"""POST open_pr forwards task_id."""
mock_chore = MagicMock()
mock_chore.open_pr = AsyncMock(
return_value=_make_envelope(status="awaiting_qa", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/open_pr",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.open_pr.assert_awaited_once()
@pytest.mark.asyncio
async def test_unclaim_dispatches_task_id() -> None:
"""POST unclaim forwards task_id."""
mock_chore = MagicMock()
mock_chore.unclaim = AsyncMock(
return_value=_make_envelope(status="pending", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/unclaim",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.unclaim.assert_awaited_once()
@pytest.mark.asyncio
async def test_resume_dispatches_task_id() -> None:
"""POST resume forwards task_id."""
mock_chore = MagicMock()
mock_chore.resume = AsyncMock(
return_value=_make_envelope(status="in_progress", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/resume",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.resume.assert_awaited_once()
@pytest.mark.asyncio
async def test_sync_branch_dispatches_task_id() -> None:
"""POST sync_branch forwards task_id to Choreographer.sync_branch (git-only)."""
mock_chore = MagicMock()
mock_chore.sync_branch = AsyncMock(
return_value=_make_envelope(status="ok", task_id=_TASK_ID)
)
client = TestClient(_build_app(mock_chore))
resp = client.post(
"/api/v1/flow/developer/sync_branch",
json={"task_id": _TASK_ID},
headers=_HEADERS,
)
assert resp.status_code == _HTTP_200
mock_chore.sync_branch.assert_awaited_once()
# the only positional arg beyond x_agent_id is task_id
assert str(mock_chore.sync_branch.call_args.args[1]) == _TASK_ID