feat(maf): complete resilient roundtable workflow

This commit is contained in:
董宏凯 committed 2026-08-02 00:23:25 +08:00
1 parent bd4131b4e2
commit 21c4c898e7
23 files changed
+4810 -312

No files matched your search

@@ -13,7 +13,8 @@ Architecture:
- Cancellation propagates from MAF run → DeerFlow run via RunManager.cancel.
- All seat runs persisted to ``maf_roundtable_seat_runs`` (survives restart).
- Critical audit events (tool.*, skill.*, approval.*, artifact.*) MUST persist;
failure stops external writes and marks the run as failed.
audit failure stops external writes; seat turn failures are soft-failed
into ``seat_outcomes`` / ``partial_failure`` (structured mode).
Boundary: lives in ``app.maf_roundtable`` (may import ``deerflow.*`` and
``app.gateway.*``); never imported by ``deerflow.*``.
@@ -132,6 +133,7 @@ class DeerFlowAgentAdapter:
turn_index: int,
message: str,
thread_id: str,
execution_stage: str = "expert",
model_name: str | None = None,
reasoning_effort: str | None = None,
thinking_enabled: bool = True,
@@ -147,6 +149,7 @@ class DeerFlowAgentAdapter:
capability_snapshot: dict[str, Any] | None = None,
write_policy: str = "confirm_high_risk",
approved_tool: str | None = None,
wait_timeout_seconds: float | None = None,
) -> SeatResponse:
"""Execute one seat turn through the DeerFlow Agent Runtime."""
t0 = time.perf_counter()
@@ -166,10 +169,56 @@ class DeerFlowAgentAdapter:
principal = ExecutionPrincipal(user_id=self._user_id, session_id=session_id, run_id=run_id)
seat_run = await self._service.store.seat_runs.create_seat_run(
maf_run_id=run_id, agent_id=agent_id, turn_index=turn_index, deerflow_thread_id=thread_id
# Phase-3 P0-1: atomic "occupy or read". Only the caller that wins the
# insert (created=True) may start a DeerFlow run. An existing row is
# handled by status so we NEVER silently start a second DeerFlow
# execution for the same four-tuple (run, agent, turn, stage).
seat_run, created = await self._service.store.seat_runs.get_or_create_seat_run(
maf_run_id=run_id, agent_id=agent_id, turn_index=turn_index,
execution_stage=execution_stage, deerflow_thread_id=thread_id,
)
seat_run_id = seat_run["id"]
prior_status = seat_run.get("status")
if not created:
if prior_status == "completed":
return self._seat_response_from_snapshot(
seat_run=seat_run,
agent_id=agent_id,
turn_index=turn_index,
)
if prior_status in ("pending", "running", "cancelling"):
effective_timeout = wait_timeout_seconds if wait_timeout_seconds is not None else 600.0
seat_run = await self._service.store.seat_runs.wait_for_terminal_seat_run(
maf_run_id=run_id,
agent_id=agent_id,
turn_index=turn_index,
execution_stage=execution_stage,
timeout_seconds=effective_timeout,
)
if seat_run["status"] == "completed":
return self._seat_response_from_snapshot(
seat_run=seat_run,
agent_id=agent_id,
turn_index=turn_index,
)
return SeatResponse(
agent_id=agent_id,
turn_index=turn_index,
content="",
error=f"stage '{execution_stage}' ended as {seat_run['status']}",
metadata={"seat_run_id": seat_run["id"], "terminal": seat_run["status"]},
latency_ms=int((time.perf_counter() - t0) * 1000),
)
return SeatResponse(
agent_id=agent_id,
turn_index=turn_index,
content="",
error=f"stage '{execution_stage}' previously terminal ({prior_status}); "
f"manual recovery required",
metadata={"terminal": prior_status, "seat_run_id": seat_run_id},
latency_ms=int((time.perf_counter() - t0) * 1000),
)
try:
response = await self._execute_run(
@@ -210,7 +259,16 @@ class DeerFlowAgentAdapter:
latency_ms=latency,
)
await self._service.store.seat_runs.mark_completed(seat_run_id)
raw_snapshot = {
"content": response.content,
"tool_calls": response.tool_calls,
"usage": response.usage,
"metadata": response.metadata,
}
snapshot = sanitize(raw_snapshot)
if not isinstance(snapshot, dict):
raise RuntimeError("seat result snapshot sanitization produced invalid payload")
await self._service.store.seat_runs.mark_completed(seat_run_id, result_snapshot=snapshot)
response.latency_ms = int((time.perf_counter() - t0) * 1000)
await self._emit_critical(run_id, make_turn_completed(
agent_id, turn_index,
@@ -463,6 +521,34 @@ class DeerFlowAgentAdapter:
# ── internal helpers ────────────────────────────────────────────────────
def _seat_response_from_snapshot(
self,
*,
seat_run: dict[str, Any],
agent_id: str,
turn_index: int,
) -> SeatResponse:
snapshot = seat_run.get("result_snapshot")
if not snapshot or not isinstance(snapshot, dict):
return SeatResponse(
agent_id=agent_id,
turn_index=turn_index,
content="",
error="completed seat run has no replayable snapshot; manual recovery required",
metadata={"seat_run_id": seat_run.get("id"), "terminal": seat_run.get("status")},
latency_ms=0,
)
return SeatResponse(
agent_id=agent_id,
turn_index=turn_index,
content=str(snapshot.get("content", "")),
tool_calls=list(snapshot.get("tool_calls") or []),
usage=snapshot.get("usage"),
metadata=dict(snapshot.get("metadata") or {}),
error=None,
latency_ms=0,
)
def _latest_ai_text(self, messages: list[dict[str, Any]]) -> str:
for m in reversed(messages):
if not isinstance(m, dict):