feat(maf): Phase 5 - action coordinator, approval flow, frontend MAF roundtable pages

- Backend: action_coordinator, capability_snapshot, event_normalizer, tool_executor updates
- Backend: approved_tool_executor with write guard, action leases, migration script
- Frontend: maf-roundtable module (API client, event timeline, approval cards, fact flow panel)
- Frontend: MafRoutes, MafSessionsPage, MafRunDetailPage
- Tests: action_leases, migration_phase5, phase5_entry_e2e, real_write_e2e/http_e2e
- Docs: update implementation plan

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-02 13:07:21 +08:00
1 parent 21c4c898e7
commit f82363d81f
55 files changed
+6304 -167

No files matched your search

@@ -6,8 +6,9 @@ import sys
import threading
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from datetime import datetime
from time import perf_counter
from datetime import datetime
from time import perf_counter
from typing import Any
from dotenv import load_dotenv
from fastapi import FastAPI
@@ -958,8 +959,63 @@ def _warn_if_multi_worker() -> None:
pass
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
async def _scan_maf_approval_expiry(app: Any) -> list[str]:
"""Expire due MAF approvals once, then resume affected workflows.
The store owns the conditional state transition and critical audit event.
Only the worker that wins that CAS receives an affected run, so repeated
scans cannot resume the same expiry twice.
"""
service = getattr(app.state, "maf_roundtable_service", None)
if service is None or not getattr(service, "enabled", False):
return []
expired = await service.store.expire_due_approvals()
run_ids = list(dict.fromkeys(
str(item["run_id"])
for item in expired
if item.get("run_id")
))
if not run_ids:
return []
from app.maf_roundtable.dispatcher import get_dispatcher
dispatcher = get_dispatcher(app)
resumed: list[str] = []
for run_id in run_ids:
run = await service.store.runs.get_run(run_id)
if run is None:
continue
session = await service.store.sessions.get_session(run["session_id"], user_id=None)
if session is None:
continue
# Dispatcher.resume is a no-op for a terminal/non-waiting run; keeping
# this call centralized preserves the restart and run-level CAS path.
await dispatcher.resume(run_id, user_id=str(session.get("user_id") or "default"))
resumed.append(run_id)
return resumed
async def _maf_approval_scanner_loop(app: Any) -> None:
"""Continuously fail closed expired MAF approvals until shutdown."""
service = getattr(app.state, "maf_roundtable_service", None)
config = getattr(service, "config", None)
interval = max(1, int(getattr(config, "approval_scan_interval_seconds", 10) or 10))
while True:
try:
await _scan_maf_approval_expiry(app)
except asyncio.CancelledError:
raise
except Exception:
logger.exception("MAF approval expiry scan failed (non-fatal)")
await asyncio.sleep(interval)
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""Application lifespan handler."""
# ?????? worker ?? ?? ?? AI ?? / ? agent ???????
@@ -1123,12 +1179,27 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
# Concurrency (system-pressure) sampler — lightweight, singleton-locked,
# runtime-toggleable via system_settings.concurrency_monitor.enabled.
try:
app.state.concurrency_sampler_task = asyncio.create_task(_concurrency_sampler_loop(app))
except Exception:
logger.exception("Concurrency sampler failed to start (non-fatal)")
# Start IM channel service if any channels are configured
try:
app.state.concurrency_sampler_task = asyncio.create_task(_concurrency_sampler_loop(app))
except Exception:
logger.exception("Concurrency sampler failed to start (non-fatal)")
# MAF-only approval expiry scanner. Legacy DeerFlow, old roundtable
# and AI-writing paths never enter this loop.
try:
maf_service = getattr(app.state, "maf_roundtable_service", None)
if maf_service is not None and getattr(maf_service, "enabled", False):
app.state.maf_approval_scanner_task = asyncio.create_task(
_maf_approval_scanner_loop(app)
)
logger.info(
"MAF approval expiry scanner started (interval=%ss)",
getattr(maf_service.config, "approval_scan_interval_seconds", 10),
)
except Exception:
logger.exception("MAF approval expiry scanner failed to start (non-fatal)")
# Start IM channel service if any channels are configured
try:
from app.channels.service import start_channel_service
@@ -1187,16 +1258,26 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
logger.exception("Leaderboard snapshot scheduler shutdown error")
concurrency_sampler_task = getattr(app.state, "concurrency_sampler_task", None)
if concurrency_sampler_task is not None:
concurrency_sampler_task.cancel()
try:
await concurrency_sampler_task
except asyncio.CancelledError:
pass
except Exception:
logger.exception("Concurrency sampler shutdown error")
# ?? 7 ????AI ?? graph ???? checkpointer / in-flight task?
if concurrency_sampler_task is not None:
concurrency_sampler_task.cancel()
try:
await concurrency_sampler_task
except asyncio.CancelledError:
pass
except Exception:
logger.exception("Concurrency sampler shutdown error")
maf_approval_scanner_task = getattr(app.state, "maf_approval_scanner_task", None)
if maf_approval_scanner_task is not None:
maf_approval_scanner_task.cancel()
try:
await maf_approval_scanner_task
except asyncio.CancelledError:
pass
except Exception:
logger.exception("MAF approval expiry scanner shutdown error")
# ?? 7 ????AI ?? graph ???? checkpointer / in-flight task?
# thread ??? LangGraph Server ? checkpointer ???lifespan ???
# ``langgraph_runtime()`` ? contextmanager ???????????????