"""Phase-3 Round-2 acceptance tests (P0-3): real manager workflow coexistence. Drives the REAL workflow executors (PlannerExecutor → SeatExecutor ×N → CrossReviewExecutor → ApprovalGateExecutor → FinalizerExecutor) through a manager + 2-seat structured run, with a fake DeerFlow adapter whose ``run_seat_turn`` records each call's ``execution_stage`` into a REAL seat-run store. This proves the Phase-3 disambiguation invariants end-to-end: - manager planner / cross_review / finalizer all share the SAME business round (turn_index) but occupy distinct seat-run rows via ``execution_stage`` — they must coexist without a four-tuple collision. - an expert seat turn and its one-shot ``format_repair`` retry coexist for the same (agent, turn). - invalid expert output triggers exactly one ``format_repair`` DeerFlow call, and both rows persist. - invalid cross-review output triggers exactly one ``cross_review_repair`` DeerFlow call, and ``cross_review`` + ``cross_review_repair`` rows coexist. """ from __future__ import annotations import json from datetime import datetime from unittest.mock import AsyncMock import pytest import pytest_asyncio from maf_test_helpers import make_maf_service from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from app.maf_roundtable.deerflow_agent_adapter import SeatResponse from app.maf_roundtable.workflow_executors import ( ApprovalGateExecutor, CrossReviewExecutor, FinalizerExecutor, PlannerExecutor, RoundtableState, SeatExecutor, ) from deerflow.persistence.maf_roundtable import make_maf_roundtable_store from deerflow.persistence.maf_roundtable.model import Base, MafRoundtableRunRow, MafRoundtableSessionRow MANAGER = "manager-1" SEATS = ["seat-a", "seat-b"] class _FakeCtx: def __init__(self) -> None: self.messages: list[object] = [] self.outputs: list[object] = [] async def send_message(self, msg: object) -> None: self.messages.append(msg) async def yield_output(self, msg: object) -> None: self.outputs.append(msg) @pytest_asyncio.fixture async def svc(): engine = create_async_engine("sqlite+aiosqlite://", echo=False) async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) sf = async_sessionmaker(engine, expire_on_commit=False) store = make_maf_roundtable_store(sf) service = make_maf_service(store) now = datetime(2026, 1, 1) async with sf() as session: session.add(MafRoundtableSessionRow( id="sess-1", user_id="u1", title="t", task_text="分析市场", mode="structured", status="running", write_policy="auto", manager_agent_id=MANAGER, max_rounds=8, created_at=now, updated_at=now, )) session.add(MafRoundtableRunRow( id="run-1", session_id="sess-1", status="running", created_at=now, updated_at=now, )) await session.commit() yield service await engine.dispose() def _state() -> RoundtableState: return RoundtableState( session_id="sess-1", run_id="run-1", task_text="分析市场", mode="structured", model_name=None, reasoning_effort=None, max_rounds=8, write_policy="auto", manager_agent_id=MANAGER, seat_agent_ids=SEATS, thread_map={MANAGER: "t-mgr", "seat-a": "t-a", "seat-b": "t-b"}, capability_snapshot=None, seat_timeout_seconds=5, schema_repair_enabled=True, ) def _resp(content: str) -> SeatResponse: return SeatResponse( agent_id="x", turn_index=0, content=content, tool_calls=[], usage={"input_tokens": 1, "output_tokens": 1}, ) class _RecordingAdapter: """Fake adapter that (a) records every run_seat_turn's execution_stage and (b) writes a REAL seat-run row for each call so the four-tuple coexistence invariant is checked against the actual DB.""" def __init__(self, service) -> None: self._service = service self.calls: list[tuple[str, str, int, str]] = [] # (agent, turn, stage) self._seat_outputs: dict[tuple[str, str], str] = {} # (agent, stage) -> content self._user_id = "u1" self._emit_critical = AsyncMock() self._emit = AsyncMock() def set_output(self, agent_id: str, execution_stage: str, content: str) -> None: self._seat_outputs[(agent_id, execution_stage)] = content async def run_seat_turn(self, **kwargs): agent_id = kwargs["agent_id"] turn_index = kwargs["turn_index"] stage = kwargs.get("execution_stage", "expert") self.calls.append((agent_id, turn_index, stage)) # Persist a seat-run row so coexistence is verified against the real DB. await self._service.store.seat_runs.create_seat_run( maf_run_id=kwargs["run_id"], agent_id=agent_id, turn_index=turn_index, execution_stage=stage, ) content = self._seat_outputs.get((agent_id, stage), '{"summary":"ok"}') return _resp(content) # ── 1. Full structured run: manager stages coexist ─────────────────────────── @pytest.mark.asyncio async def test_full_structured_run_manager_stages_coexist(svc): """A complete planner→experts→cross_review→approval→finalizer run must persist manager planner/cross_review/finalizer rows that ALL share the same business round (turn_index) but differ by execution_stage — no collision.""" adapter = _RecordingAdapter(svc) # Manager emits valid JSON at each stage. adapter.set_output(MANAGER, "planner", "分工:seat-a 负责需求,seat-b 负责竞品") adapter.set_output(MANAGER, "cross_review", json.dumps({ "summary": "审查通过", "evidence_ok": True, "confirmed_candidates": [], })) adapter.set_output(MANAGER, "finalizer", "最终结论:市场看好") # Seats emit valid structured output. for s in SEATS: adapter.set_output(s, "expert", json.dumps({"summary": f"{s} 结论", "findings": ["f"]})) state = _state() ctx = _FakeCtx() await PlannerExecutor(adapter, state).handle("task", ctx) for s in SEATS: await SeatExecutor(adapter, state, s).handle({}, _FakeCtx()) await CrossReviewExecutor(adapter, state).handle({}, _FakeCtx()) await ApprovalGateExecutor(adapter, state, svc).handle({}, _FakeCtx()) await FinalizerExecutor(adapter, state).handle({}, _FakeCtx()) # Assert every manager stage produced a distinct seat-run row, all sharing # turn_index (planner=0, cross_review/finalizer advance current_round; the # invariant is that the SAME (agent) has multiple distinct stages). rows = await svc.store.seat_runs.get_by_maf_run("run-1") mgr_rows = [r for r in rows if r["agent_id"] == MANAGER] mgr_stages = {r["execution_stage"] for r in mgr_rows} assert {"planner", "cross_review", "finalizer"}.issubset(mgr_stages), \ f"manager stages missing; got {mgr_stages}" # No duplicate four-tuple (the unique constraint would have raised). seen = set() for r in mgr_rows: key = (r["maf_run_id"], r["agent_id"], r["turn_index"], r["execution_stage"]) assert key not in seen, f"duplicate four-tuple {key}" seen.add(key) # The final result was produced. assert state.final_result is not None assert state.status == "completed" # ── 2. expert + format_repair coexist ──────────────────────────────────────── @pytest.mark.asyncio async def test_expert_format_repair_coexist(svc): """A seat that emits invalid JSON triggers exactly one ``format_repair`` DeerFlow call; both the expert and the format_repair rows persist for the same (agent, turn) without colliding.""" adapter = _RecordingAdapter(svc) # First expert call: invalid JSON → triggers repair. The format_repair call # (stage='format_repair') returns valid JSON. adapter.set_output("seat-a", "expert", "not-json-at-all") adapter.set_output("seat-a", "format_repair", json.dumps({"summary": "repaired"})) state = _state() state.current_round = 1 state.plan = {"assignments": {"seat-a": {"instruction": "x", "role": "A"}}} await SeatExecutor(adapter, state, "seat-a").handle({}, _FakeCtx()) stage_calls = [(a, t, s) for (a, t, s) in adapter.calls if a == "seat-a"] stages = [s for _, _, s in stage_calls] assert "expert" in stages and "format_repair" in stages # Exactly one repair (not a retry loop). assert stages.count("format_repair") == 1 rows = await svc.store.seat_runs.get_by_maf_run("run-1") seat_a_stages = {r["execution_stage"] for r in rows if r["agent_id"] == "seat-a"} assert seat_a_stages == {"expert", "format_repair"} # ── 3. cross_review + cross_review_repair coexist ──────────────────────────── @pytest.mark.asyncio async def test_cross_review_repair_coexist(svc): """An invalid cross-review output triggers exactly one ``cross_review_repair`` DeerFlow call; cross_review and cross_review_repair rows coexist for the manager.""" adapter = _RecordingAdapter(svc) adapter.set_output(MANAGER, "cross_review", "totally not json") adapter.set_output(MANAGER, "cross_review_repair", json.dumps({ "summary": "repaired review", "confirmed_candidates": [], })) state = _state() # Pretend experts already ran so the cross-review barrier sees outcomes. state.seat_outcomes = {s: {"status": "completed"} for s in SEATS} state.current_round = 1 await CrossReviewExecutor(adapter, state).handle({}, _FakeCtx()) mgr_stages = [s for a, _, s in adapter.calls if a == MANAGER] assert "cross_review" in mgr_stages and "cross_review_repair" in mgr_stages assert mgr_stages.count("cross_review_repair") == 1 rows = await svc.store.seat_runs.get_by_maf_run("run-1") mgr_row_stages = {r["execution_stage"] for r in rows if r["agent_id"] == MANAGER} assert {"cross_review", "cross_review_repair"}.issubset(mgr_row_stages) # The repaired review was applied (deterministic degradation did NOT happen). assert state.cross_review_result is not None assert state.cross_review_result.get("schema_status") == "repaired" assert state.cross_review_result.get("summary") == "repaired review" # ── 4. manager present in participants-style config ────────────────────────── @pytest.mark.asyncio async def test_manager_role_wired_through_state(svc): """Sanity: RoundtableState carries manager_agent_id and the planner uses it for the planner seat-turn (not a seat id).""" state = _state() assert state.manager_agent_id == MANAGER assert MANAGER not in state.seat_agent_ids adapter = _RecordingAdapter(svc) adapter.set_output(MANAGER, "planner", "plan") ctx = _FakeCtx() await PlannerExecutor(adapter, state).handle("task", ctx) planner_calls = [(a, s) for a, _, s in adapter.calls if s == "planner"] assert planner_calls == [(MANAGER, "planner")], \ "planner stage must run as the MANAGER, not a seat"