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

@@ -113,6 +113,15 @@ class ToolExecutor:
)
return {"status": "failed", "error": f"action state machine error: {exc}"}
if claim == "target_busy":
logger.info("ToolExecutor: target lease is held for action %s", action_id)
return {"status": "waiting_target_lease", "error": "target lease is held by another action"}
if claim == "target_reconciliation_required":
logger.warning("ToolExecutor: expired target lease requires reconciliation for %s", action_id)
return {
"status": "unknown",
"error": "target lease expired during another action; reconciliation required",
}
if claim != "claimed":
existing = await self._coordinator._store.actions.get_action(action_id) # noqa: SLF001
if claim == "in_progress":
@@ -128,9 +137,8 @@ class ToolExecutor:
return {"status": claim, "action": existing}
if not approved_tool:
await self._coordinator.record_result(
action_id, status="failed",
result={"error": "no approved tool name — cannot execute"},
await self._record_result_or_log(
action_id, status="failed", result={"error": "no approved tool name — cannot execute"}
)
return {"status": "failed", "error": "no approved tool name"}
@@ -147,11 +155,14 @@ class ToolExecutor:
"ToolExecutor: action %s params unavailable (%s): %s",
action_id, exc.reason, safe_msg,
)
await self._coordinator.record_result(
persisted = await self._record_result_or_log(
action_id, status="failed",
result={"error": f"action secret unavailable: {exc.reason}"},
)
return {"status": "failed", "error": safe_msg}
return {
"status": "failed" if persisted else "unknown",
"error": safe_msg if persisted else "action result persistence failed",
}
# 3. Read the stored action record (authoritative turn_index + the
# run/session/agent/tool the harness will verify against).
@@ -191,34 +202,83 @@ class ToolExecutor:
"ToolExecutor: direct tool call '%s' raised: %s",
approved_tool, safe_msg,
)
await self._coordinator.record_result(
action_id, status="failed", result={"error": safe_msg},
# An exception can arrive after the remote request was accepted
# (network disconnect / timeout), so fail closed as ``unknown``.
persisted = await self._record_result_or_log(
action_id, status="unknown", result={"error": "tool_transport_exception"}
)
return {"status": "failed", "error": safe_msg}
return {
"status": "unknown",
"error": safe_msg if persisted else "action result persistence failed",
}
# 5. Record terminal result (state-machine constrained: only
# running → terminal; a lost race is a no-op).
outcome_status = outcome.get("status")
if outcome_status == "succeeded":
await self._coordinator.record_result(
action_id, status="succeeded",
result={"tool_result": outcome.get("result")},
tool_result = outcome.get("result")
external_object_id = (
str(tool_result.get("external_object_id"))
if isinstance(tool_result, dict)
and tool_result.get("external_object_id")
else None
)
return {"status": "succeeded", "tool_result": outcome.get("result")}
persisted = await self._record_result_or_log(
action_id, status="succeeded",
result={"tool_result": tool_result},
external_object_id=external_object_id,
)
if not persisted:
# The external side effect succeeded but its local terminal
# record did not. Never retry; the retained running lease is
# later reconciled to unknown by recovery.
return {"status": "unknown", "error": "action result persistence failed"}
return {"status": "succeeded", "tool_result": tool_result}
elif outcome_status == "unknown":
# Inconclusive external result (timeout / disconnect / gateway):
# record ``unknown`` — never auto-retry, a human must reconcile.
error = outcome.get("error", "inconclusive external result")
await self._coordinator.record_result(
persisted = await self._record_result_or_log(
action_id, status="unknown", result={"error": error},
)
return {"status": "unknown", "error": error}
return {"status": "unknown", "error": error if persisted else "action result persistence failed"}
else:
error = outcome.get("error", "tool execution failed")
await self._coordinator.record_result(
persisted = await self._record_result_or_log(
action_id, status="failed", result={"error": error},
)
return {"status": "failed", "error": error}
return {
"status": "failed" if persisted else "unknown",
"error": error if persisted else "action result persistence failed",
}
async def _record_result_or_log(
self,
action_id: str,
*,
status: str,
result: dict[str, Any],
external_object_id: str | None = None,
) -> bool:
"""Persist a terminal outcome without opening a retry path after I/O.
If this write fails after a real tool reports success, returning an
``unknown`` outcome lets the parent run stop for reconciliation while
the still-held lease prevents a replay of the external call.
"""
try:
await self._coordinator.record_result(
action_id,
status=status,
result=result,
external_object_id=external_object_id,
)
except Exception: # noqa: BLE001
logger.exception(
"ToolExecutor: could not persist terminal result for action %s", action_id
)
return False
return True
async def record_unknown(self, action_id: str, *, reason: str) -> None:
"""Record an ``unknown`` outcome (network timeout / inconclusive).
@@ -230,6 +290,6 @@ class ToolExecutor:
"ToolExecutor: action %s outcome unknown — %s (no auto-retry)",
action_id, reason,
)
await self._coordinator.record_result(
action_id, status="unknown", result={"reason": reason},
await self._record_result_or_log(
action_id, status="unknown", result={"reason": reason}
)