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:
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 ???????????????
|
||||
|
||||
|
||||
@@ -112,18 +112,25 @@ async def get_health(request: Request) -> HealthResponse:
|
||||
"""Feature-flag probe. Open read — the frontend reads ``enabled`` to
|
||||
decide whether to show the MAF entry. Does not require the store.
|
||||
"""
|
||||
from app.maf_roundtable.service import get_maf_write_guard_readiness
|
||||
from deerflow.config import get_app_config
|
||||
|
||||
cfg = get_app_config().maf_roundtable
|
||||
app_config = get_app_config()
|
||||
cfg = app_config.maf_roundtable
|
||||
svc = _get_service(request)
|
||||
store_available = svc is not None and svc.store is not None
|
||||
readiness = get_maf_write_guard_readiness(app_config=app_config)
|
||||
return HealthResponse(
|
||||
enabled=cfg.enabled,
|
||||
enabled=bool(cfg.enabled and svc is not None and svc.enabled),
|
||||
store_available=store_available,
|
||||
default_mode=cfg.default_mode,
|
||||
default_write_policy=cfg.default_write_policy,
|
||||
default_max_rounds=cfg.default_max_rounds,
|
||||
admin_diagnostic_access=cfg.admin_diagnostic_access,
|
||||
write_guard_enabled=readiness["enabled"],
|
||||
write_guard_ready=readiness["ready"],
|
||||
action_secret_configured=readiness["secret_configured"],
|
||||
write_guard_status=readiness["status"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -359,6 +359,50 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
}
|
||||
)
|
||||
|
||||
# These fields activate or authorize MAF WriteGuard behavior. They may only
|
||||
# cross the Gateway boundary on the typed, server-created request from
|
||||
# ``DeerFlowRuntimeClient``. In particular, accepting them from a public
|
||||
# ``body.config`` would let a caller forge an active MAF run or a post-approval
|
||||
# passthrough.
|
||||
_MAF_PRIVATE_CONTEXT_KEYS: frozenset[str] = frozenset(
|
||||
{
|
||||
"maf_run_id",
|
||||
"maf_session_id",
|
||||
"maf_user_id",
|
||||
"turn_index",
|
||||
"write_policy",
|
||||
"capability_snapshot",
|
||||
"maf_approved_tool",
|
||||
"maf_approved_action_id",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _is_internal_maf_request(request: Any) -> bool:
|
||||
state = getattr(request, "state", None)
|
||||
return bool(getattr(state, "maf_internal", False))
|
||||
|
||||
|
||||
def _merge_internal_maf_context(config: dict[str, Any], context: Mapping[str, Any], request: Any) -> None:
|
||||
"""Forward server-generated MAF context, never public request fields."""
|
||||
if not _is_internal_maf_request(request):
|
||||
for container_name in ("context", "configurable"):
|
||||
container = config.get(container_name)
|
||||
if isinstance(container, dict):
|
||||
for key in _MAF_PRIVATE_CONTEXT_KEYS:
|
||||
container.pop(key, None)
|
||||
return
|
||||
|
||||
configurable = config.setdefault("configurable", {})
|
||||
runtime_context = config.setdefault("context", {})
|
||||
for key in _MAF_PRIVATE_CONTEXT_KEYS:
|
||||
if key not in context:
|
||||
continue
|
||||
if isinstance(configurable, dict):
|
||||
configurable.setdefault(key, context[key])
|
||||
if isinstance(runtime_context, dict):
|
||||
runtime_context.setdefault(key, context[key])
|
||||
|
||||
|
||||
def merge_run_context_overrides(config: dict[str, Any], context: Mapping[str, Any] | None) -> None:
|
||||
"""Merge whitelisted keys from ``body.context`` into both ``config['configurable']``
|
||||
@@ -1022,6 +1066,7 @@ async def start_run(
|
||||
if value is not None and top_level_key not in body_context:
|
||||
body_context[top_level_key] = value
|
||||
merge_run_context_overrides(config, body_context or None)
|
||||
_merge_internal_maf_context(config, body_context, request)
|
||||
|
||||
stream_modes = normalize_stream_modes(body.stream_mode)
|
||||
|
||||
|
||||
Reference in new issue
Block a user