feat(maf): complete resilient roundtable workflow
This commit is contained in:
1 parent
bd4131b4e2
commit
21c4c898e7
23 files changed
+4810
-312
No files matched your search
@@ -0,0 +1,305 @@
|
||||
"""Phase 3 structured workflow acceptance tests (MAF-03)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from maf_test_helpers import make_maf_service
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.maf_roundtable.fact_ledger import executed_action_ids, proposed_action_ids
|
||||
from app.maf_roundtable.workflow_executors import (
|
||||
CrossReviewExecutor,
|
||||
PlannerExecutor,
|
||||
RoundtableState,
|
||||
SeatExecutor,
|
||||
)
|
||||
from app.maf_roundtable.workflow_factory import create_workflow
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
|
||||
|
||||
class _FakeCtx:
|
||||
def __init__(self) -> None:
|
||||
self.messages: list[Any] = []
|
||||
self.outputs: list[Any] = []
|
||||
|
||||
async def send_message(self, msg: Any) -> None:
|
||||
self.messages.append(msg)
|
||||
|
||||
async def yield_output(self, msg: Any) -> None:
|
||||
self.outputs.append(msg)
|
||||
|
||||
|
||||
class _FakeSeatResponse:
|
||||
def __init__(self, content: str = "", tool_calls: list | None = None, error: str | None = None) -> None:
|
||||
self.content = content
|
||||
self.tool_calls = tool_calls or []
|
||||
self.error = error
|
||||
self.usage = {"input_tokens": 1, "output_tokens": 1}
|
||||
|
||||
|
||||
def _state(seat_ids: list[str] | None = None) -> RoundtableState:
|
||||
seats = seat_ids or ["seat-a", "seat-b", "seat-c"]
|
||||
return RoundtableState(
|
||||
session_id="s1",
|
||||
run_id="r1",
|
||||
task_text="分析市场",
|
||||
mode="structured",
|
||||
model_name=None,
|
||||
reasoning_effort=None,
|
||||
max_rounds=8,
|
||||
write_policy="confirm_high_risk",
|
||||
manager_agent_id=None,
|
||||
seat_agent_ids=seats,
|
||||
thread_map={a: f"t-{a}" for a in seats},
|
||||
capability_snapshot=None,
|
||||
seat_timeout_seconds=5,
|
||||
schema_repair_enabled=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_planner_deterministic_assignments():
|
||||
state = _state()
|
||||
adapter = SimpleNamespace(_emit_critical=AsyncMock())
|
||||
planner = PlannerExecutor(adapter, state)
|
||||
ctx = _FakeCtx()
|
||||
await planner.handle("task", ctx)
|
||||
assert "planner" in state.completed_nodes
|
||||
assert state.plan is not None
|
||||
assert set(state.plan["assignments"]) == {"seat-a", "seat-b", "seat-c"}
|
||||
assert ctx.messages[-1]["decision"] == "parallel_seats"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_planner_skips_when_completed():
|
||||
state = _state()
|
||||
state.mark_completed("planner")
|
||||
state.plan = {"source": "deterministic", "assignments": {}}
|
||||
adapter = SimpleNamespace(run_seat_turn=AsyncMock())
|
||||
planner = PlannerExecutor(adapter, state)
|
||||
ctx = _FakeCtx()
|
||||
await planner.handle("task", ctx)
|
||||
adapter.run_seat_turn.assert_not_called()
|
||||
assert ctx.messages[-1].get("replayed") is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_seat_partial_failure_does_not_raise():
|
||||
state = _state(["seat-a", "seat-b"])
|
||||
state.plan = {
|
||||
"assignments": {
|
||||
"seat-a": {"instruction": "do a", "role": "A"},
|
||||
"seat-b": {"instruction": "do b", "role": "B"},
|
||||
}
|
||||
}
|
||||
state.current_round = 1
|
||||
|
||||
async def _run(**kwargs):
|
||||
if kwargs["agent_id"] == "seat-a":
|
||||
raise RuntimeError("boom")
|
||||
return _FakeSeatResponse(content='{"summary":"ok-b","findings":["f"]}')
|
||||
|
||||
adapter = SimpleNamespace(
|
||||
run_seat_turn=AsyncMock(side_effect=_run),
|
||||
_emit_critical=AsyncMock(),
|
||||
_service=SimpleNamespace(store=SimpleNamespace(actions=SimpleNamespace(list_actions=AsyncMock(return_value=[])))),
|
||||
)
|
||||
|
||||
seat_a = SeatExecutor(adapter, state, "seat-a")
|
||||
seat_b = SeatExecutor(adapter, state, "seat-b")
|
||||
ctx_a, ctx_b = _FakeCtx(), _FakeCtx()
|
||||
await seat_a.handle({}, ctx_a)
|
||||
await seat_b.handle({}, ctx_b)
|
||||
|
||||
assert state.partial_failure is True
|
||||
assert state.seat_outcomes["seat-a"]["status"] in ("failed", "error")
|
||||
assert state.seat_outcomes["seat-b"]["status"] == "completed"
|
||||
assert any(r.get("agent_id") == "seat-b" and r.get("content") for r in state.turn_results)
|
||||
assert "seat_seat-a" in state.completed_nodes
|
||||
assert "seat_seat-b" in state.completed_nodes
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_seat_schema_one_repair_then_invalid():
|
||||
state = _state(["seat-a"])
|
||||
state.plan = {"assignments": {"seat-a": {"instruction": "x", "role": "A"}}}
|
||||
state.current_round = 1
|
||||
calls = {"n": 0}
|
||||
|
||||
async def _run(**kwargs):
|
||||
calls["n"] += 1
|
||||
if calls["n"] == 1:
|
||||
return _FakeSeatResponse(content="not-json")
|
||||
return _FakeSeatResponse(content="still-bad")
|
||||
|
||||
adapter = SimpleNamespace(
|
||||
run_seat_turn=AsyncMock(side_effect=_run),
|
||||
_emit_critical=AsyncMock(),
|
||||
_service=SimpleNamespace(store=SimpleNamespace(actions=SimpleNamespace(list_actions=AsyncMock(return_value=[])))),
|
||||
)
|
||||
seat = SeatExecutor(adapter, state, "seat-a")
|
||||
await seat.handle({}, _FakeCtx())
|
||||
# 1 expert + 1 repair
|
||||
assert calls["n"] == 2
|
||||
row = state.turn_results[-1]
|
||||
assert row["schema_status"] == "schema_invalid"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_proposed_not_executed_in_ledger():
|
||||
state = _state(["seat-a"])
|
||||
state.plan = {"assignments": {"seat-a": {"instruction": "x", "role": "A"}}}
|
||||
state.current_round = 1
|
||||
content = (
|
||||
'{"summary":"s","findings":["f"],'
|
||||
'"proposed_actions":[{"action_id":"p1","text":"write"}],'
|
||||
'"executed_actions":[{"action_id":"forge","text":"lie"}]}'
|
||||
)
|
||||
adapter = SimpleNamespace(
|
||||
run_seat_turn=AsyncMock(return_value=_FakeSeatResponse(content=content)),
|
||||
_emit_critical=AsyncMock(),
|
||||
_service=SimpleNamespace(store=SimpleNamespace(actions=SimpleNamespace(list_actions=AsyncMock(return_value=[])))),
|
||||
)
|
||||
await SeatExecutor(adapter, state, "seat-a").handle({}, _FakeCtx())
|
||||
assert "p1" in proposed_action_ids(state.fact_ledger)
|
||||
assert "forge" not in executed_action_ids(state.fact_ledger)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cross_review_barrier_marks_missing():
|
||||
state = _state(["seat-a", "seat-b"])
|
||||
state.seat_outcomes = {"seat-a": {"status": "completed"}}
|
||||
state.current_round = 1
|
||||
adapter = SimpleNamespace(run_seat_turn=AsyncMock(), _emit_critical=AsyncMock())
|
||||
review = CrossReviewExecutor(adapter, state)
|
||||
await review.handle({}, _FakeCtx())
|
||||
assert state.seat_outcomes["seat-b"]["status"] == "missing"
|
||||
assert state.partial_failure is True
|
||||
assert "cross_review" in state.completed_nodes
|
||||
|
||||
|
||||
def test_structured_factory_requires_planner():
|
||||
with pytest.raises(ValueError):
|
||||
create_workflow(
|
||||
mode="structured",
|
||||
seat_executors=[],
|
||||
approval_gate=object(),
|
||||
finalizer=object(),
|
||||
waiting_terminal=object(),
|
||||
seat_agent_ids=[],
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recover_skips_completed_seats(tmp_path, monkeypatch):
|
||||
"""completed_nodes must not re-invoke run_seat_turn on recover."""
|
||||
pytest.importorskip("agent_framework")
|
||||
|
||||
from app.maf_roundtable.workflow_runner import WorkflowRunner
|
||||
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'p3r.db'}")
|
||||
async with engine.begin() as conn:
|
||||
import deerflow.persistence.maf_roundtable.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
store = make_maf_roundtable_store(sf)
|
||||
svc = make_maf_service(store)
|
||||
session = await svc.create_session(
|
||||
user_id="u1",
|
||||
body={
|
||||
"task_text": "t",
|
||||
"mode": "structured",
|
||||
"participants": [
|
||||
{"agent_id": "seat-a", "role": "seat"},
|
||||
{"agent_id": "seat-b", "role": "seat"},
|
||||
],
|
||||
},
|
||||
)
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
|
||||
# Pretend planner + seat-a already finished; seat-b not yet.
|
||||
state = RoundtableState(
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
task_text="t",
|
||||
mode="structured",
|
||||
model_name=None,
|
||||
reasoning_effort=None,
|
||||
max_rounds=8,
|
||||
write_policy="confirm_high_risk",
|
||||
manager_agent_id=None,
|
||||
seat_agent_ids=["seat-a", "seat-b"],
|
||||
thread_map={"seat-a": "ta", "seat-b": "tb"},
|
||||
capability_snapshot=session.get("capability_snapshot"),
|
||||
seat_timeout_seconds=30,
|
||||
schema_repair_enabled=False,
|
||||
)
|
||||
state.plan = {
|
||||
"source": "deterministic",
|
||||
"assignments": {
|
||||
"seat-a": {"instruction": "a", "role": "A"},
|
||||
"seat-b": {"instruction": "b", "role": "B"},
|
||||
},
|
||||
}
|
||||
state.current_round = 1
|
||||
state.stage = "experts"
|
||||
state.mark_completed("planner")
|
||||
state.mark_completed("seat_seat-a")
|
||||
state.seat_outcomes["seat-a"] = {"status": "completed"}
|
||||
state.turn_results.append({
|
||||
"agent_id": "seat-a",
|
||||
"turn_index": 1,
|
||||
"content": '{"summary":"a"}',
|
||||
"tool_calls": [],
|
||||
"error": None,
|
||||
})
|
||||
await store.runs.update_run(run["id"], {"status": "running", "maf_checkpoint": state.to_dict()})
|
||||
|
||||
calls: list[str] = []
|
||||
|
||||
async def _run_seat(**kwargs):
|
||||
aid = kwargs["agent_id"]
|
||||
calls.append(aid)
|
||||
return _FakeSeatResponse(content=f'{{"summary":"{aid}"}}')
|
||||
|
||||
app = SimpleNamespace(state=SimpleNamespace(maf_roundtable_service=svc, checkpointer=None))
|
||||
runner = WorkflowRunner(app, service=svc, user_id="u1")
|
||||
monkeypatch.setattr(runner._adapter, "run_seat_turn", AsyncMock(side_effect=_run_seat))
|
||||
monkeypatch.setattr(runner._adapter, "ensure_threads", AsyncMock(return_value=state.thread_map))
|
||||
monkeypatch.setattr(runner._adapter, "_emit_critical", AsyncMock())
|
||||
|
||||
# Avoid needing a real SDK stream — drive recover path by patching _run_workflow
|
||||
# to invoke remaining executors with skip semantics.
|
||||
async def _fake_run_workflow(workflow, st):
|
||||
# Simulate graph: planner (skip) → seats → cross_review → approval → finalizer
|
||||
from app.maf_roundtable.workflow_executors import (
|
||||
ApprovalGateExecutor,
|
||||
CrossReviewExecutor,
|
||||
FinalizerExecutor,
|
||||
PlannerExecutor,
|
||||
SeatExecutor,
|
||||
)
|
||||
|
||||
ctx = _FakeCtx()
|
||||
await PlannerExecutor(runner._adapter, st).handle("t", ctx)
|
||||
for aid in st.seat_agent_ids:
|
||||
await SeatExecutor(runner._adapter, st, aid).handle({}, _FakeCtx())
|
||||
await CrossReviewExecutor(runner._adapter, st).handle({}, _FakeCtx())
|
||||
await ApprovalGateExecutor(runner._adapter, st, svc).handle({}, _FakeCtx())
|
||||
await FinalizerExecutor(runner._adapter, st).handle({}, _FakeCtx())
|
||||
st.status = "completed"
|
||||
|
||||
monkeypatch.setattr(runner, "_run_workflow", _fake_run_workflow)
|
||||
try:
|
||||
await runner.recover(run["id"])
|
||||
# seat-a was completed — must not be called; seat-b should run once.
|
||||
assert "seat-a" not in calls
|
||||
assert calls.count("seat-b") == 1
|
||||
finally:
|
||||
await engine.dispose()
|
||||
Reference in new issue
Block a user