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
+10
@@ -37,6 +37,7 @@ passing all secret-derived params and the stored action record as plain data.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import uuid
|
||||
from typing import Any, TypedDict
|
||||
@@ -477,6 +478,15 @@ def _interpret_tool_node_result(result: Any, tool_name: str) -> dict[str, Any]:
|
||||
if _is_inconclusive_text(text):
|
||||
return {"status": "unknown", "error": _ERROR_CODE_INCONCLUSIVE}
|
||||
return {"status": "failed", "error": _ERROR_CODE_FAILED}
|
||||
# LangChain may serialize a structured tool return into a JSON string
|
||||
# inside ToolMessage.content. Recover only the safe, allow-listed
|
||||
# fields so an external object id can be audited without exposing
|
||||
# arbitrary tool output.
|
||||
if isinstance(content, str):
|
||||
try:
|
||||
content = json.loads(content)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return {"status": "succeeded", "result": _safe_success_payload(content)}
|
||||
|
||||
return {"status": "succeeded", "result": {"executed": True}}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
"""Test-only real external-write tool for MAF approval E2E tests.
|
||||
|
||||
This module is deliberately not registered in ``config.yaml``. Tests add the
|
||||
tool to an isolated ``AppConfig`` and set ``MAF_E2E_OUTBOX_DIR`` to a temporary
|
||||
directory. It is a real LangChain ``StructuredTool`` and therefore exercises
|
||||
the same LangGraph ``ToolNode`` path as a configured production tool without
|
||||
touching a business system.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
from pathlib import Path
|
||||
|
||||
from langchain.tools import tool
|
||||
|
||||
_OPERATION_ID = re.compile(r"^[A-Za-z0-9][A-Za-z0-9_.-]{0,95}$")
|
||||
_TEST_BLOCK_STARTED: threading.Event | None = None
|
||||
_TEST_BLOCK_RELEASE: threading.Event | None = None
|
||||
|
||||
|
||||
def configure_test_block(
|
||||
*, started: threading.Event | None = None, release: threading.Event | None = None
|
||||
) -> None:
|
||||
"""Install a test-only barrier for lease contention tests.
|
||||
|
||||
The hook is inert unless a test explicitly supplies both events. It keeps
|
||||
the write inside the real synchronous ToolNode call while allowing the
|
||||
second MAF run to observe the first target lease as ``running``.
|
||||
"""
|
||||
|
||||
global _TEST_BLOCK_STARTED, _TEST_BLOCK_RELEASE
|
||||
_TEST_BLOCK_STARTED = started
|
||||
_TEST_BLOCK_RELEASE = release
|
||||
|
||||
|
||||
def clear_test_block() -> None:
|
||||
configure_test_block()
|
||||
|
||||
|
||||
@tool("maf_test_outbox_write", parse_docstring=True)
|
||||
def maf_test_outbox_write(operation_id: str, value: str) -> dict[str, object]:
|
||||
"""Append one controlled write record to the test outbox.
|
||||
|
||||
Args:
|
||||
operation_id: Stable idempotency identifier for the test action.
|
||||
value: Test payload written only to the temporary outbox.
|
||||
"""
|
||||
outbox_dir = (os.getenv("MAF_E2E_OUTBOX_DIR") or "").strip()
|
||||
if not outbox_dir:
|
||||
raise RuntimeError("MAF_E2E_OUTBOX_DIR is not configured")
|
||||
if not _OPERATION_ID.fullmatch(operation_id):
|
||||
raise ValueError("invalid test outbox operation id")
|
||||
|
||||
if _TEST_BLOCK_STARTED is not None and _TEST_BLOCK_RELEASE is not None:
|
||||
_TEST_BLOCK_STARTED.set()
|
||||
_TEST_BLOCK_RELEASE.wait()
|
||||
|
||||
directory = Path(outbox_dir).resolve()
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
record_path = directory / f"{operation_id}.jsonl"
|
||||
with record_path.open("a", encoding="utf-8", newline="") as record_file:
|
||||
record_file.write(
|
||||
json.dumps({"operation_id": operation_id, "value": value}, ensure_ascii=False)
|
||||
+ "\n"
|
||||
)
|
||||
return {
|
||||
"executed": True,
|
||||
"external_object_id": operation_id,
|
||||
"safe_summary": "test outbox write completed",
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["clear_test_block", "configure_test_block", "maf_test_outbox_write"]
|
||||
@@ -11,6 +11,7 @@ under ``confirm_high_risk``).
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
_DEFAULT_SIDE_EFFECT = "write"
|
||||
@@ -42,15 +43,52 @@ def resolve_tool_metadata(
|
||||
tool_name: str,
|
||||
*,
|
||||
config_extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, str]:
|
||||
"""Return ``{name, tool_name, side_effect, risk_level}`` for one tool."""
|
||||
maf_metadata: Mapping[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return trusted metadata for one tool.
|
||||
|
||||
Precedence is intentionally server-only and explicit:
|
||||
|
||||
1. MAF deployment metadata keyed by the exact runtime tool name;
|
||||
2. existing server ``ToolConfig`` extras;
|
||||
3. the curated built-in allow-list;
|
||||
4. conservative ``write`` / ``R3`` fallback.
|
||||
|
||||
``maf_metadata`` is passed by the application while building a frozen
|
||||
capability snapshot. It is never accepted from a session request or a
|
||||
model message.
|
||||
"""
|
||||
side = _DEFAULT_SIDE_EFFECT
|
||||
risk = _DEFAULT_RISK_LEVEL
|
||||
if config_extra:
|
||||
if config_extra.get("side_effect"):
|
||||
side = str(config_extra["side_effect"])
|
||||
if config_extra.get("risk_level"):
|
||||
risk = str(config_extra["risk_level"])
|
||||
target_key_fields: list[str] = []
|
||||
target_key_namespace = "default"
|
||||
registered = maf_metadata.get(tool_name) if maf_metadata else None
|
||||
if registered is not None:
|
||||
candidate_side = registered.get("side_effect") if isinstance(registered, Mapping) else getattr(registered, "side_effect", None)
|
||||
candidate_risk = registered.get("risk_level") if isinstance(registered, Mapping) else getattr(registered, "risk_level", None)
|
||||
if candidate_side in {"read", "skill", "write"}:
|
||||
side = str(candidate_side)
|
||||
if candidate_risk in {"R0", "R1", "R2", "R3", "R4"}:
|
||||
risk = str(candidate_risk)
|
||||
candidate_fields = registered.get("target_key_fields") if isinstance(registered, Mapping) else getattr(registered, "target_key_fields", None)
|
||||
candidate_namespace = registered.get("target_key_namespace") if isinstance(registered, Mapping) else getattr(registered, "target_key_namespace", None)
|
||||
if isinstance(candidate_fields, list) and all(isinstance(item, str) for item in candidate_fields):
|
||||
target_key_fields = list(candidate_fields)
|
||||
if isinstance(candidate_namespace, str) and candidate_namespace:
|
||||
target_key_namespace = candidate_namespace
|
||||
elif config_extra:
|
||||
candidate_side = config_extra.get("side_effect")
|
||||
candidate_risk = config_extra.get("risk_level")
|
||||
if candidate_side in {"read", "skill", "write"}:
|
||||
side = str(candidate_side)
|
||||
if candidate_risk in {"R0", "R1", "R2", "R3", "R4"}:
|
||||
risk = str(candidate_risk)
|
||||
candidate_fields = config_extra.get("target_key_fields")
|
||||
candidate_namespace = config_extra.get("target_key_namespace")
|
||||
if isinstance(candidate_fields, list) and all(isinstance(item, str) for item in candidate_fields):
|
||||
target_key_fields = list(candidate_fields)
|
||||
if isinstance(candidate_namespace, str) and candidate_namespace:
|
||||
target_key_namespace = candidate_namespace
|
||||
elif tool_name in _BUILTIN_METADATA:
|
||||
side, risk = _BUILTIN_METADATA[tool_name]
|
||||
return {
|
||||
@@ -58,4 +96,6 @@ def resolve_tool_metadata(
|
||||
"tool_name": tool_name,
|
||||
"side_effect": side,
|
||||
"risk_level": risk,
|
||||
"target_key_fields": target_key_fields,
|
||||
"target_key_namespace": target_key_namespace,
|
||||
}
|
||||
Reference in new issue
Block a user