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

@@ -7,7 +7,8 @@ Phase 1 ships:
- Session CRUD: ``POST /sessions``, ``GET /sessions``, ``GET /sessions/{id}``,
``PUT /sessions/{id}``, ``DELETE /sessions/{id}``.
- Run lifecycle: ``POST /sessions/{id}/runs``, ``GET /runs/{id}``,
``GET /sessions/{id}/runs``, ``POST /runs/{id}/cancel``.
``GET /sessions/{id}/runs``, ``POST /runs/{id}/cancel``,
``POST /runs/{id}/resume``, ``POST /runs/{id}/recover``.
- Event stream: ``GET /runs/{id}/events?after_seq=N`` (断线补拉) — **read-only**;
there is no public ``POST /runs/{id}/events``. Real workflow events are
written only by the backend executor via ``service.append_event``.
@@ -32,7 +33,7 @@ from __future__ import annotations
from typing import Any
from fastapi import APIRouter, HTTPException, Query, Request
from fastapi import APIRouter, HTTPException, Query, Request, status
from app.maf_roundtable import (
ActionListResponse,
@@ -291,6 +292,76 @@ async def cancel_run(request: Request, run_id: str) -> RunResponse:
return RunResponse(**final_run)
@router.post("/runs/{run_id}/resume", response_model=RunResponse)
async def resume_run(request: Request, run_id: str) -> RunResponse:
"""Resume after approvals are decided (owner-only).
If the run is not ``waiting_approval`` or still has pending approvals,
returns 409.
"""
svc = _require_service(request)
user_id, is_admin = _user_ctx(request)
try:
run = await svc.get_run(run_id=run_id, user_id=user_id, is_admin=False)
except (MafDisabledError, MafForbiddenError, MafNotFoundError) as exc:
raise _map_service_error(exc) from exc
if run.get("status") != "waiting_approval":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"run {run_id} is not waiting_approval (status={run.get('status')})",
)
from app.maf_roundtable.dispatcher import get_dispatcher
dispatcher = get_dispatcher(request.app)
try:
updated = await dispatcher.resume(run_id, user_id=user_id or "default")
except Exception as exc: # noqa: BLE001
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"resume failed: {type(exc).__name__}",
) from exc
if updated is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="run not found")
return RunResponse(**updated)
@router.post("/runs/{run_id}/recover", response_model=RunResponse)
async def recover_run(request: Request, run_id: str) -> RunResponse:
"""Graph-level recover after process restart (owner-only).
Re-enters the workflow from checkpoint; completed_nodes are skipped.
Does not silently scan all runs at startup.
"""
svc = _require_service(request)
user_id, is_admin = _user_ctx(request)
try:
run = await svc.get_run(run_id=run_id, user_id=user_id, is_admin=False)
except (MafDisabledError, MafForbiddenError, MafNotFoundError) as exc:
raise _map_service_error(exc) from exc
if run.get("status") not in ("running", "waiting_approval"):
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"run {run_id} cannot be recovered (status={run.get('status')})",
)
from app.maf_roundtable.dispatcher import get_dispatcher
dispatcher = get_dispatcher(request.app)
try:
updated = await dispatcher.recover(run_id, user_id=user_id or "default")
except Exception as exc: # noqa: BLE001
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"recover failed: {type(exc).__name__}",
) from exc
if updated is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="run not found")
return RunResponse(**updated)
# ── events ─────────────────────────────────────────────────────────────────