Files
roboco/tests/unit/mcp_servers/test_flow_server.py
T
Renn F 4829f93a68 fix(gateway): unblock task claim; full Phase 0/1/2 remediation
Resolves the 100% claim-failure rate introduced by the gateway rewrite
  (commit 62bda0c plus 78 follow-ups). Live smoke runs hit
  `404 /api/v2/flow/developer/...` on every dev verb plus a manifest
  fallback that silently exposed off-role verbs to PMs — confirmed
  firing simultaneously in NAS agent logs (be-dev-1, be-pm, main-pm).

  Audit reports under docs/internal/audit_2026_05_04/ catalogue 49
  defects across gateway, services, prompts, MCP transport, substrate,
  and tests (8 detail reports + master synthesis). Six smoking guns;
  three proven in production logs.

  Phase 0 — unblock claim:
  - URL prefix /api/v2/flow/dev → /developer; slug-map board roles
    (product_owner, head_marketing) → /board (D-01)
  - _i_will_work_on AttributeError on None across pending /
    needs_revision / claimed re-entry branches (D-02)
  - Seed last_heartbeat_at in _qa_or_doc_claim (D-03)
  - Drop misleading i_have_committed verb; dev flow uses commit() (D-04)
  - Manifest mount via compose; flow_server + do_server fail loud
    instead of exposing all-verbs fallback (D-12)
  - MCP _post() surfaces envelope body on 4xx so agents see remediate
    hints (D-13)
    on git failure so retries aren't blocked by half-state (S-01)

  Phase 1 — lifecycle stability:
  - _resolve_skill falls back to AgentTable.capabilities (D-06)
  - main_pm_complete uses kwargs for escalate_to_ceo (D-07)
  - i_am_done auto-runs submit_verification when in_progress (D-08)
  - active_claimant_id wired in claim/unclaim paths — single-claimant
    invariant now functional (D-05)
  - qa_pass/qa_fail assert claimed_by parity with qa_agent_id (D-18)
  - Prompt-drift sweep: fail() shape, i_am_done(task_id, notes),
    subtask cap (12 hard / 8 soft), error-code symbology rewritten in
    base.md + per-role anti-patterns (D-10/11/29/30/31, D-37)

  Phase 2 — invariants + architecture:
  - Real-DB integration test exercising claim → in_progress → commit
    → submit_for_qa → i_am_done → awaiting_qa (P2-1)
  - choreographer.py → package; 3 of 6 role mixins extracted
    (board, doc, qa). _impl.py 2,526 → 2,080 lines (-18%). Continuation
    plan in docs/internal/audit_2026_05_04/p2_2_decompose_plan.md (P2-2)
  - Closure guards consolidated via _subtasks_not_terminal_envelope (P2-3)
  - TaskService.unclaim_for_reaper routed through canonical
    _validate_and_set_status; in_progress → pending added to
    VALID_TRANSITIONS (P2-4)
  - Dead code removed: i_am_done_with_catchup verb, _run_catch_up helper
    (P2-5)
  - 6 state-machine invariants asserted via property test (P2-6)
  - attempt_id (uuid4) stamped on every gateway.rejected audit row (P2-7)
  - _reconcile_orphan_claims_on_startup rolls back tasks left CLAIMED
    with branch_name=NULL from prior crashes (P2-8)
  - scripts/regenerate_verb_tables.py introspects Pydantic schemas +
    role_config; compose_prompt injects per-role tables as a layer.
    Eliminates the prompt-drift class structurally (P2-9)

  Other:
  - D-48: orchestrator mounts host's ~/.claude.json when present so
    agents don't boot from backup recovery on every spawn
  - D-49: dev dispatcher rejects role-mismatched spawns (e.g. doc task
    assigned to dev agent)

  Tests: 553 pass · ruff + mypy clean. Live NAS smoke verification
  pending — needs the stack brought back up.
2026-05-04 23:43:55 +02:00

402 lines
14 KiB
Python

"""Tests for roboco-flow MCP server."""
from __future__ import annotations
import importlib
import json
from typing import TYPE_CHECKING, Any
from unittest.mock import MagicMock, patch
import pytest
if TYPE_CHECKING:
import types
from pathlib import Path
_FULL_MANIFEST = {
"agent_id": "00000000-0000-0000-0000-000000000001",
"role": "developer",
"team": "backend",
"workspace_path": "/tmp/test",
# Test fixture provides every flow verb so per-verb URL/path tests work
# against a single fixture. Production manifests are role-scoped.
"flow_tools": [
"give_me_work",
"i_will_work_on",
"submit_for_qa",
"i_am_done",
"i_am_blocked",
"unclaim",
"resume",
"i_am_idle",
"claim_review",
"pass",
"fail",
"claim_doc_task",
"i_documented",
"triage",
"triage_all",
"unblock",
"complete",
"escalate_up",
"i_will_plan",
"delegate",
"submit_up",
"escalate_to_ceo",
],
"do_tools": ["commit", "note", "say", "dm", "evidence"],
"read_tools": ["Read", "Glob", "Grep"],
"write_tools": ["Edit", "Write"],
"bash_allowed": True,
"subagent_allowed": False,
"subagent_model": None,
"env": {},
}
@pytest.fixture()
def flow_module(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> types.ModuleType:
"""Import the flow_server module with controlled env vars + manifest."""
manifest_path = tmp_path / "tool-manifest.json"
manifest_path.write_text(json.dumps(_FULL_MANIFEST))
monkeypatch.setenv("ROBOCO_AGENT_ID", "00000000-0000-0000-0000-000000000001")
monkeypatch.setenv("ROBOCO_AGENT_ROLE", "developer")
monkeypatch.setenv("ROBOCO_ORCHESTRATOR_URL", "http://test-orchestrator:8000")
monkeypatch.setenv("ROBOCO_TOOL_MANIFEST_PATH", str(manifest_path))
import roboco.mcp.flow_server as srv
importlib.reload(srv)
return srv
def _make_fake_client(return_value: dict[str, Any]) -> MagicMock:
"""Build a fake httpx.Client context-manager that returns the given dict."""
fake_response = MagicMock()
fake_response.json.return_value = return_value
fake_client = MagicMock()
fake_client.__enter__ = MagicMock(return_value=fake_client)
fake_client.__exit__ = MagicMock(return_value=False)
fake_client.post.return_value = fake_response
return fake_client
def _reload_for_role(
monkeypatch: pytest.MonkeyPatch, role: str, agent_id: str
) -> types.ModuleType:
"""Set env + write manifest for the given role; reload flow_server.
The manifest provides the full verb superset so role-specific tests
aren't blocked by the manifest filter; the role-scoped URL routing
is what's under test in these per-role cases.
"""
import tempfile
from pathlib import Path
manifest_path = Path(tempfile.mkdtemp()) / "tool-manifest.json"
payload = {**_FULL_MANIFEST, "role": role, "agent_id": agent_id}
manifest_path.write_text(json.dumps(payload))
monkeypatch.setenv("ROBOCO_AGENT_ID", agent_id)
monkeypatch.setenv("ROBOCO_AGENT_ROLE", role)
monkeypatch.setenv("ROBOCO_ORCHESTRATOR_URL", "http://test-orchestrator:8000")
monkeypatch.setenv("ROBOCO_TOOL_MANIFEST_PATH", str(manifest_path))
import roboco.mcp.flow_server as srv
importlib.reload(srv)
return srv
def test_role_path_uses_agent_role(flow_module: types.ModuleType) -> None:
expected = "/api/v2/flow/developer/give_me_work"
assert flow_module._role_path("give_me_work") == expected
def test_role_path_includes_verb(flow_module: types.ModuleType) -> None:
assert flow_module._role_path("i_am_done") == "/api/v2/flow/developer/i_am_done"
def test_give_me_work_posts_to_orchestrator(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "idle", "task_id": None})
with patch("httpx.Client", return_value=fake_client):
result = flow_module.give_me_work()
assert result == {"status": "idle", "task_id": None}
fake_client.post.assert_called_once()
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/developer/give_me_work" in args[0]
assert kwargs["headers"]["X-Agent-ID"] == "00000000-0000-0000-0000-000000000001"
assert kwargs["headers"]["X-Agent-Role"] == "developer"
def test_i_will_work_on_passes_plan(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "in_progress"})
with patch("httpx.Client", return_value=fake_client):
flow_module.i_will_work_on("task-uuid", plan="my plan")
args, kwargs = fake_client.post.call_args
assert kwargs["json"] == {"task_id": "task-uuid", "plan": "my plan"}
assert "/api/v2/flow/developer/i_will_work_on" in args[0]
def test_i_will_work_on_plan_defaults_to_none(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "in_progress"})
with patch("httpx.Client", return_value=fake_client):
flow_module.i_will_work_on("task-uuid")
_, kwargs = fake_client.post.call_args
assert kwargs["json"] == {"task_id": "task-uuid", "plan": None}
def test_i_am_done_sends_task_id_and_notes(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "awaiting_qa"})
with patch("httpx.Client", return_value=fake_client):
result = flow_module.i_am_done("task-abc", notes="all tests green")
assert result == {"status": "awaiting_qa"}
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/developer/i_am_done" in args[0]
assert kwargs["json"] == {"task_id": "task-abc", "notes": "all tests green"}
def test_i_am_done_notes_defaults_to_empty(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "awaiting_qa"})
with patch("httpx.Client", return_value=fake_client):
flow_module.i_am_done("task-abc")
_, kwargs = fake_client.post.call_args
assert kwargs["json"]["notes"] == ""
def test_i_am_blocked_sends_reason(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "blocked"})
with patch("httpx.Client", return_value=fake_client):
result = flow_module.i_am_blocked("task-xyz", reason="waiting for env var")
assert result == {"status": "blocked"}
_, kwargs = fake_client.post.call_args
assert kwargs["json"] == {"task_id": "task-xyz", "reason": "waiting for env var"}
def test_i_am_idle_posts_empty_body(flow_module: types.ModuleType) -> None:
fake_client = _make_fake_client({"status": "idle"})
with patch("httpx.Client", return_value=fake_client):
result = flow_module.i_am_idle()
assert result == {"status": "idle"}
_, kwargs = fake_client.post.call_args
assert kwargs["json"] == {}
assert "/api/v2/flow/developer/i_am_idle" in fake_client.post.call_args[0][0]
def test_claim_review_posts_to_qa_path(monkeypatch: pytest.MonkeyPatch) -> None:
"""When AGENT_ROLE=qa, claim_review forwards to /api/v2/flow/qa/claim_review."""
srv = _reload_for_role(monkeypatch, "qa", "00000000-0000-0000-0000-000000000002")
fake_client = _make_fake_client({"status": "claimed", "evidence": {}})
with patch("httpx.Client", return_value=fake_client):
result = srv.claim_review("task-uuid")
assert result["status"] == "claimed"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/qa/claim_review" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid"}
def test_pass_review_passes_notes(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(monkeypatch, "qa", "00000000-0000-0000-0000-000000000002")
fake_client = _make_fake_client({"status": "awaiting_documentation"})
notes = "x" * 100
with patch("httpx.Client", return_value=fake_client):
srv.pass_review("task-uuid", notes)
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/qa/pass" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid", "notes": notes}
def test_fail_review_passes_issues_list(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(monkeypatch, "qa", "00000000-0000-0000-0000-000000000002")
fake_client = _make_fake_client({"status": "needs_revision"})
issues = ["Missing test for /healthz", "Lint errors"]
with patch("httpx.Client", return_value=fake_client):
srv.fail_review("task-uuid", issues)
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/qa/fail" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid", "issues": issues}
def test_claim_doc_task_posts_to_documenter_path(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""When AGENT_ROLE=documenter, claim_doc_task forwards to documenter flow."""
srv = _reload_for_role(
monkeypatch, "documenter", "00000000-0000-0000-0000-000000000003"
)
fake_client = _make_fake_client({"status": "claimed"})
with patch("httpx.Client", return_value=fake_client):
result = srv.claim_doc_task("task-uuid")
assert result["status"] == "claimed"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/documenter/claim_doc_task" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid"}
def test_i_documented_passes_notes_and_files(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "documenter", "00000000-0000-0000-0000-000000000003"
)
fake_client = _make_fake_client({"status": "awaiting_pm_review"})
with patch("httpx.Client", return_value=fake_client):
result = srv.i_documented("task-uuid", "wrote docs/foo.md", ["docs/foo.md"])
assert result["status"] == "awaiting_pm_review"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/documenter/i_documented" in args[0]
assert kwargs["json"] == {
"task_id": "task-uuid",
"notes": "wrote docs/foo.md",
"files": ["docs/foo.md"],
}
def test_triage_uses_role_path(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "cell_pm", "00000000-0000-0000-0000-000000000004"
)
fake_client = _make_fake_client({"status": "blocked"})
with patch("httpx.Client", return_value=fake_client):
result = srv.triage()
assert result["status"] == "blocked"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/cell_pm/triage" in args[0]
assert kwargs["json"] == {}
def test_triage_all_uses_role_path(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "main_pm", "00000000-0000-0000-0000-000000000005"
)
fake_client = _make_fake_client({"status": "idle"})
with patch("httpx.Client", return_value=fake_client):
result = srv.triage_all()
assert result["status"] == "idle"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/main_pm/triage_all" in args[0]
assert kwargs["json"] == {}
def test_unblock_with_restore_true(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "cell_pm", "00000000-0000-0000-0000-000000000004"
)
fake_client = _make_fake_client({"status": "in_progress"})
with patch("httpx.Client", return_value=fake_client):
result = srv.unblock("task-uuid")
assert result["status"] == "in_progress"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/cell_pm/unblock" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid", "restore": True}
def test_unblock_with_restore_false(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "cell_pm", "00000000-0000-0000-0000-000000000004"
)
fake_client = _make_fake_client({"status": "in_progress"})
with patch("httpx.Client", return_value=fake_client):
result = srv.unblock("task-uuid", restore=False)
assert result["status"] == "in_progress"
_args, kwargs = fake_client.post.call_args
assert kwargs["json"] == {"task_id": "task-uuid", "restore": False}
def test_complete_passes_notes(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "cell_pm", "00000000-0000-0000-0000-000000000004"
)
fake_client = _make_fake_client({"status": "completed"})
with patch("httpx.Client", return_value=fake_client):
result = srv.complete("task-uuid", notes="approved")
assert result["status"] == "completed"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/cell_pm/complete" in args[0]
assert kwargs["json"] == {"task_id": "task-uuid", "notes": "approved"}
def test_escalate_up_passes_reason(monkeypatch: pytest.MonkeyPatch) -> None:
srv = _reload_for_role(
monkeypatch, "cell_pm", "00000000-0000-0000-0000-000000000004"
)
fake_client = _make_fake_client({"status": "blocked"})
with patch("httpx.Client", return_value=fake_client):
result = srv.escalate_up("task-uuid", reason="cross-cell help needed")
assert result["status"] == "blocked"
args, kwargs = fake_client.post.call_args
assert "/api/v2/flow/cell_pm/escalate_up" in args[0]
assert kwargs["json"] == {
"task_id": "task-uuid",
"reason": "cross-cell help needed",
}
def test_escalate_to_ceo_passes_reason(monkeypatch: pytest.MonkeyPatch) -> None:
"""Board / Main PM verb forwards to /api/v2/flow/<role>/escalate_to_ceo."""
srv = _reload_for_role(
monkeypatch, "product_owner", "00000000-0000-0000-0000-000000000005"
)
fake_client = _make_fake_client({"status": "awaiting_ceo_approval"})
with patch("httpx.Client", return_value=fake_client):
result = srv.escalate_to_ceo("task-uuid", reason="strategic decision needed")
assert result["status"] == "awaiting_ceo_approval"
args, kwargs = fake_client.post.call_args
# Board route serves PO + Head Marketing under one prefix; the slug
# map in flow_server translates product_owner → board.
assert "/api/v2/flow/board/escalate_to_ceo" in args[0]
assert kwargs["json"] == {
"task_id": "task-uuid",
"reason": "strategic decision needed",
}