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
@@ -0,0 +1,396 @@
|
||||
"""Phase-5 target lease, timeout and ambiguous-result safety tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from maf_test_helpers import make_maf_service
|
||||
from sqlalchemy import update
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.app import _scan_maf_approval_expiry
|
||||
from app.maf_roundtable.action_coordinator import (
|
||||
ActionCoordinator,
|
||||
derive_target_key,
|
||||
encrypt_params,
|
||||
)
|
||||
from app.maf_roundtable.service import MafConflictError
|
||||
from app.maf_roundtable.tool_executor import ToolExecutor
|
||||
from app.maf_roundtable.workflow_executors import RoundtableState
|
||||
from app.maf_roundtable.workflow_runner import WorkflowRunner
|
||||
from deerflow.config.maf_roundtable_config import MafRoundtableConfig
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
Base,
|
||||
MafRoundtableActionLeaseRow,
|
||||
MafRoundtableApprovalRow,
|
||||
)
|
||||
|
||||
TEST_TOOL = "controlled_write"
|
||||
|
||||
|
||||
def _snapshot() -> dict:
|
||||
tool = {
|
||||
"name": TEST_TOOL,
|
||||
"side_effect": "write",
|
||||
"risk_level": "R3",
|
||||
"target_key_fields": ["tenant_id", "object_id"],
|
||||
"target_key_namespace": "lease-test",
|
||||
}
|
||||
return {
|
||||
"tools": [tool],
|
||||
"agents": [{"agent_id": "seat-1", "tools": [tool]}],
|
||||
}
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def store(tmp_path):
|
||||
# A file-backed SQLite database gives concurrent sessions the same
|
||||
# transaction/constraint semantics used by deployed SQLite installations.
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'maf_action_leases.db'}", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
result = make_maf_roundtable_store(sf)
|
||||
assert result is not None
|
||||
yield result
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def svc(store):
|
||||
return make_maf_service(
|
||||
store,
|
||||
enabled=True,
|
||||
config=MafRoundtableConfig(
|
||||
enabled=True,
|
||||
approval_timeout_seconds=60,
|
||||
target_lease_seconds=30,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def _run(store, svc) -> tuple[dict, dict]:
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "test"})
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
return session, run
|
||||
|
||||
|
||||
async def _approved_action(
|
||||
store, session: dict, run: dict, *, key: str, target_key: str, agent_id: str = "seat-1"
|
||||
) -> dict:
|
||||
return await store.actions.create_action({
|
||||
"session_id": session["id"],
|
||||
"run_id": run["id"],
|
||||
"agent_id": agent_id,
|
||||
"tool_name": TEST_TOOL,
|
||||
"idempotency_key": key,
|
||||
"target_key": target_key,
|
||||
"risk_level": "R3",
|
||||
"status": "approved",
|
||||
})
|
||||
|
||||
|
||||
def test_target_key_is_server_derived_and_opaque(monkeypatch) -> None:
|
||||
snapshot = _snapshot()
|
||||
original = {
|
||||
"tenant_id": "tenant-a",
|
||||
"object_id": "task-7",
|
||||
"target_key": "browser-forged-value",
|
||||
"secret": "do-not-persist",
|
||||
}
|
||||
same_target_different_noise = {
|
||||
"tenant_id": "tenant-a",
|
||||
"object_id": "task-7",
|
||||
"target_key": "another-forged-value",
|
||||
"secret": "changed",
|
||||
}
|
||||
target = derive_target_key(TEST_TOOL, original, snapshot, "seat-1")
|
||||
assert target == derive_target_key(TEST_TOOL, same_target_different_noise, snapshot, "seat-1")
|
||||
assert target.startswith("maf-target:v1:")
|
||||
assert "tenant-a" not in target
|
||||
assert "task-7" not in target
|
||||
assert "browser-forged" not in target
|
||||
|
||||
nested = derive_target_key(
|
||||
TEST_TOOL,
|
||||
{"tenant_id": "tenant-a", "object_id": {"id": "task-7"}},
|
||||
snapshot,
|
||||
"seat-1",
|
||||
)
|
||||
nested_other = derive_target_key(
|
||||
TEST_TOOL,
|
||||
{"tenant_id": "tenant-a", "object_id": ["task-7"]},
|
||||
snapshot,
|
||||
"seat-1",
|
||||
)
|
||||
assert nested == nested_other
|
||||
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "another-test-domain-secret")
|
||||
assert target != derive_target_key(TEST_TOOL, original, snapshot, "seat-1")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_agents_same_target_are_serialized(store, svc):
|
||||
"""The DB lease, not a process lock, blocks the second approved write."""
|
||||
session, run = await _run(store, svc)
|
||||
target_key = derive_target_key(
|
||||
TEST_TOOL, {"tenant_id": "tenant-a", "object_id": "task-7"}, _snapshot(), "seat-1"
|
||||
)
|
||||
first = await _approved_action(store, session, run, key="lease-first", target_key=target_key)
|
||||
second = await _approved_action(
|
||||
store, session, run, key="lease-second", target_key=target_key, agent_id="seat-2"
|
||||
)
|
||||
|
||||
first_claim, second_claim = await asyncio.gather(
|
||||
store.claim_action_for_execution(first["id"], lease_seconds=30),
|
||||
store.claim_action_for_execution(second["id"], lease_seconds=30),
|
||||
)
|
||||
assert sorted([first_claim, second_claim]) == ["claimed", "target_busy"]
|
||||
winner = first if first_claim == "claimed" else second
|
||||
waiting = second if winner["id"] == first["id"] else first
|
||||
assert (await store.actions.get_action(waiting["id"]))["status"] == "approved"
|
||||
|
||||
assert await store.record_action_terminal_result(
|
||||
winner["id"], status="succeeded", result={"safe": True}
|
||||
)
|
||||
assert await store.claim_action_for_execution(waiting["id"], lease_seconds=30) == "claimed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_restart_recovers_expired_lease_as_unknown_without_replay(store, svc):
|
||||
session, run = await _run(store, svc)
|
||||
target_key = derive_target_key(
|
||||
TEST_TOOL, {"tenant_id": "tenant-a", "object_id": "task-7"}, _snapshot(), "seat-1"
|
||||
)
|
||||
action = await _approved_action(store, session, run, key="lease-restart", target_key=target_key)
|
||||
assert await store.claim_action_for_execution(action["id"], lease_seconds=1) == "claimed"
|
||||
|
||||
recovered = await store.recover_expired_action_leases(
|
||||
run_id=run["id"], now=datetime.now(UTC) + timedelta(seconds=5)
|
||||
)
|
||||
assert [item["id"] for item in recovered] == [action["id"]]
|
||||
stored = await store.actions.get_action(action["id"])
|
||||
assert stored["status"] == "unknown"
|
||||
assert stored["result"] == {"error_code": "target_lease_expired_during_execution"}
|
||||
assert await store.claim_action_for_execution(action["id"], lease_seconds=30) == "unknown"
|
||||
blocked = await _approved_action(
|
||||
store,
|
||||
session,
|
||||
run,
|
||||
key="lease-restart-blocked",
|
||||
target_key=target_key,
|
||||
agent_id="seat-2",
|
||||
)
|
||||
assert await store.claim_action_for_execution(blocked["id"], lease_seconds=30) == "target_reconciliation_required"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_approval_timeout_rejects_action_and_writes_audit_event(monkeypatch, store, svc):
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "test-only-lease-secret")
|
||||
session, run = await _run(store, svc)
|
||||
coordinator = ActionCoordinator(svc, user_id="u1")
|
||||
actions = await coordinator.intercept_write_tool_calls(
|
||||
run_id=run["id"],
|
||||
session_id=session["id"],
|
||||
agent_id="seat-1",
|
||||
turn_index=1,
|
||||
tool_calls=[{
|
||||
"name": TEST_TOOL,
|
||||
"id": "timeout-call",
|
||||
"args": {"tenant_id": "tenant-a", "object_id": "task-7"},
|
||||
}],
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
approval_id = actions[0]["approval_id"]
|
||||
|
||||
expired = await store.expire_due_approvals(
|
||||
run_id=run["id"], now=datetime.now(UTC) + timedelta(minutes=2)
|
||||
)
|
||||
assert expired == [{"id": approval_id, "run_id": run["id"], "action_id": actions[0]["id"]}]
|
||||
assert (await store.approvals.get_approval(approval_id))["status"] == "expired"
|
||||
assert (await store.actions.get_action(actions[0]["id"]))["status"] == "rejected"
|
||||
with pytest.raises(MafConflictError):
|
||||
await svc.decide_approval(approval_id=approval_id, decision="approved", user_id="u1")
|
||||
events = await store.events.list_events(run["id"])
|
||||
assert any(
|
||||
event["event_type"] == "action.rejected"
|
||||
and event["payload"].get("reason") == "approval_expired"
|
||||
for event in events
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_external_success_with_local_result_failure_never_replays(monkeypatch, store, svc):
|
||||
"""After external success, an unavailable DB result write becomes unknown."""
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "test-only-lease-secret")
|
||||
session, run = await _run(store, svc)
|
||||
target_key = derive_target_key(
|
||||
TEST_TOOL, {"tenant_id": "tenant-a", "object_id": "task-7"}, _snapshot(), "seat-1"
|
||||
)
|
||||
action = await _approved_action(store, session, run, key="persist-failure", target_key=target_key)
|
||||
await store.action_secrets.create(
|
||||
action_id=action["id"], arguments_encrypted=encrypt_params({"tenant_id": "tenant-a", "object_id": "task-7"})
|
||||
)
|
||||
|
||||
calls = 0
|
||||
|
||||
class Client:
|
||||
async def execute_tool_directly(self, **_kwargs):
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return {"status": "succeeded", "result": {"executed": True}}
|
||||
|
||||
adapter = SimpleNamespace(_client=Client())
|
||||
coordinator = ActionCoordinator(svc, user_id="u1")
|
||||
|
||||
async def fail_terminal_persistence(*_args, **_kwargs):
|
||||
raise RuntimeError("simulated local persistence outage")
|
||||
|
||||
monkeypatch.setattr(coordinator, "record_result", fail_terminal_persistence)
|
||||
executor = ToolExecutor(adapter=adapter, action_coordinator=coordinator, user_id="u1")
|
||||
first = await executor.execute(
|
||||
action_id=action["id"],
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
agent_id="seat-1",
|
||||
thread_id="thread-1",
|
||||
approved_tool=TEST_TOOL,
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
second = await executor.execute(
|
||||
action_id=action["id"],
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
agent_id="seat-1",
|
||||
thread_id="thread-1",
|
||||
approved_tool=TEST_TOOL,
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
|
||||
assert first["status"] == "unknown"
|
||||
assert second["status"] == "in_progress"
|
||||
assert calls == 1
|
||||
recovered = await store.recover_expired_action_leases(
|
||||
run_id=run["id"], now=datetime.now(UTC) + timedelta(minutes=20)
|
||||
)
|
||||
assert [item["id"] for item in recovered] == [action["id"]]
|
||||
assert (await store.actions.get_action(action["id"]))["status"] == "unknown"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_workflow_recovery_stops_on_expired_lease_without_tool_replay(store, svc):
|
||||
"""The restart entry point converts a stale running lease to waiting_input."""
|
||||
session, run = await _run(store, svc)
|
||||
target_key = derive_target_key(
|
||||
TEST_TOOL, {"tenant_id": "tenant-a", "object_id": "task-7"}, _snapshot(), "seat-1"
|
||||
)
|
||||
action = await _approved_action(store, session, run, key="runner-recovery", target_key=target_key)
|
||||
assert await store.claim_action_for_execution(action["id"], lease_seconds=30) == "claimed"
|
||||
async with store.session_factory() as db:
|
||||
await db.execute(
|
||||
update(MafRoundtableActionLeaseRow)
|
||||
.where(MafRoundtableActionLeaseRow.action_id == action["id"])
|
||||
.values(lease_expires_at=datetime.now(UTC) - timedelta(seconds=1))
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
state = RoundtableState(
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
task_text="test",
|
||||
mode="structured",
|
||||
model_name=None,
|
||||
reasoning_effort=None,
|
||||
max_rounds=1,
|
||||
write_policy="confirm_all_writes",
|
||||
manager_agent_id=None,
|
||||
seat_agent_ids=[],
|
||||
thread_map={},
|
||||
capability_snapshot=_snapshot(),
|
||||
)
|
||||
state.current_round = 1
|
||||
state.status = "running"
|
||||
state.pending_approvals = [{
|
||||
"action_id": action["id"],
|
||||
"approval_id": "already-approved",
|
||||
"agent_id": "seat-1",
|
||||
"tool_name": TEST_TOOL,
|
||||
}]
|
||||
await store.runs.update_run(run["id"], {"status": "running", "maf_checkpoint": state.to_dict()})
|
||||
await store.sessions.update_session(session["id"], user_id="u1", fields={"status": "running"})
|
||||
|
||||
runner = WorkflowRunner(SimpleNamespace(), service=svc, user_id="u1")
|
||||
recovered = await runner.recover(run["id"])
|
||||
assert recovered is not None and recovered["status"] == "waiting_input"
|
||||
stored = await store.actions.get_action(action["id"])
|
||||
assert stored["status"] == "unknown"
|
||||
assert (await store.sessions.get_session(session["id"], user_id="u1"))["status"] == "waiting_input"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_approval_expiry_scan_is_repeatable_and_get_is_observational(monkeypatch, store, svc):
|
||||
"""A scan expires once; ordinary approval GETs never perform that mutation."""
|
||||
|
||||
session, run = await _run(store, svc)
|
||||
coordinator = ActionCoordinator(svc, user_id="u1")
|
||||
action = (
|
||||
await coordinator.intercept_write_tool_calls(
|
||||
run_id=run["id"],
|
||||
session_id=session["id"],
|
||||
agent_id="seat-1",
|
||||
turn_index=1,
|
||||
tool_calls=[{"name": TEST_TOOL, "id": "expiry-scan", "args": {
|
||||
"tenant_id": "tenant-a", "object_id": "task-7", "value": "x"
|
||||
}}],
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
)[0]
|
||||
|
||||
async with store.session_factory() as db:
|
||||
await db.execute(
|
||||
update(MafRoundtableApprovalRow)
|
||||
.where(MafRoundtableApprovalRow.id == action["approval_id"])
|
||||
.values(expires_at=datetime.now(UTC) - timedelta(seconds=1))
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
# The normal read path must not mutate an expired pending row.
|
||||
listed = await svc.list_approvals(run_id=run["id"], user_id="u1")
|
||||
assert listed[0]["status"] == "pending"
|
||||
|
||||
class FakeDispatcher:
|
||||
def __init__(self):
|
||||
self.calls: list[tuple[str, str]] = []
|
||||
|
||||
async def resume(self, run_id: str, *, user_id: str):
|
||||
self.calls.append((run_id, user_id))
|
||||
return await store.runs.get_run(run_id)
|
||||
|
||||
fake = FakeDispatcher()
|
||||
monkeypatch.setattr(
|
||||
"app.maf_roundtable.dispatcher.get_dispatcher",
|
||||
lambda _app: fake,
|
||||
)
|
||||
app = SimpleNamespace(
|
||||
state=SimpleNamespace(maf_roundtable_service=svc),
|
||||
)
|
||||
|
||||
concurrent = await asyncio.gather(
|
||||
_scan_maf_approval_expiry(app),
|
||||
_scan_maf_approval_expiry(app),
|
||||
)
|
||||
repeat_after_restart = await _scan_maf_approval_expiry(app)
|
||||
assert sorted(concurrent, key=len) == [[], [run["id"]]]
|
||||
assert repeat_after_restart == []
|
||||
assert fake.calls == [(run["id"], "u1")]
|
||||
assert (await store.approvals.get_approval(action["approval_id"]))["status"] == "expired"
|
||||
assert (await store.actions.get_action(action["id"]))["status"] == "rejected"
|
||||
@@ -50,7 +50,11 @@ CHANNEL_VALUES: dict[str, Any] = {
|
||||
def app_config():
|
||||
from deerflow.config import get_app_config
|
||||
|
||||
return get_app_config()
|
||||
# The production config mounts WriteGuard. These low-level executor tests
|
||||
# that do not register a MAF service intentionally exercise the harness
|
||||
# path without the app-layer validator; the dedicated enabled tests below
|
||||
# provide the real bridge/service wiring.
|
||||
return get_app_config().model_copy(update={"write_guard": WriteGuardConfig(enabled=False)})
|
||||
|
||||
|
||||
def _snapshot(action_tool: str) -> dict[str, Any]:
|
||||
|
||||
@@ -86,3 +86,33 @@ async def test_inaccessible_agent_rejected():
|
||||
app_config=app_config,
|
||||
agent_store=_DenyStore(),
|
||||
)
|
||||
|
||||
|
||||
def test_maf_tool_metadata_has_highest_server_priority():
|
||||
metadata = resolve_tool_metadata(
|
||||
"weknora-retrieval_weknora_search",
|
||||
config_extra={"side_effect": "write", "risk_level": "R4"},
|
||||
maf_metadata={"weknora-retrieval_weknora_search": {"side_effect": "read", "risk_level": "R0"}},
|
||||
)
|
||||
|
||||
assert metadata["side_effect"] == "read"
|
||||
assert metadata["risk_level"] == "R0"
|
||||
|
||||
|
||||
def test_deployed_weknora_names_are_explicitly_read_only():
|
||||
metadata = get_app_config().maf_roundtable.tool_metadata
|
||||
|
||||
assert metadata["weknora-retrieval_weknora_list_knowledge_bases"].side_effect == "read"
|
||||
assert metadata["weknora-retrieval_weknora_list_knowledge_bases"].risk_level == "R0"
|
||||
assert metadata["weknora-retrieval_weknora_search"].side_effect == "read"
|
||||
assert metadata["weknora-retrieval_weknora_search"].risk_level == "R0"
|
||||
|
||||
|
||||
def test_unknown_tool_metadata_remains_conservative():
|
||||
metadata = resolve_tool_metadata(
|
||||
"unregistered_external_tool",
|
||||
config_extra={"side_effect": "invalid", "risk_level": "R99"},
|
||||
)
|
||||
|
||||
assert metadata["side_effect"] == "write"
|
||||
assert metadata["risk_level"] == "R3"
|
||||
@@ -0,0 +1,89 @@
|
||||
"""Acceptance coverage for the additive Phase-5 target-lease migration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import inspect, text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from deerflow.persistence.maf_roundtable.model import Base
|
||||
|
||||
|
||||
def _migration_module():
|
||||
script = Path(__file__).resolve().parent.parent / "scripts" / "migrate_maf_roundtable_phase5.py"
|
||||
spec = importlib.util.spec_from_file_location("migrate_maf_roundtable_phase5", script)
|
||||
assert spec and spec.loader
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_phase5_lease_migration_is_additive_and_idempotent():
|
||||
module = _migration_module()
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.execute(text("DROP TABLE maf_roundtable_action_leases"))
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(module._migrate_sync, dry_run=False)
|
||||
|
||||
def check(sync_conn):
|
||||
inspector = inspect(sync_conn)
|
||||
assert "maf_roundtable_action_leases" in inspector.get_table_names()
|
||||
columns = {column["name"] for column in inspector.get_columns("maf_roundtable_action_leases")}
|
||||
assert {"target_key", "action_id", "lease_expires_at"} <= columns
|
||||
indexes = {index["name"] for index in inspector.get_indexes("maf_roundtable_action_leases")}
|
||||
assert {"ix_maf_action_leases_action_id", "ix_maf_action_leases_lease_expires_at"} <= indexes
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(check)
|
||||
await conn.execute(text(f"DROP INDEX {module.ACTION_INDEX}"))
|
||||
await conn.execute(text(f"DROP INDEX {module.EXPIRY_INDEX}"))
|
||||
await conn.run_sync(module._migrate_sync, dry_run=False)
|
||||
await conn.run_sync(check)
|
||||
await conn.run_sync(module._migrate_sync, dry_run=True)
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_phase5_migration_dry_run_is_safe_when_table_is_absent():
|
||||
module = _migration_module()
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(module._migrate_sync, dry_run=True)
|
||||
tables = await conn.run_sync(lambda sync_conn: inspect(sync_conn).get_table_names())
|
||||
assert "maf_roundtable_action_leases" not in tables
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def test_phase5_ddl_uses_sqlite_datetime():
|
||||
module = _migration_module()
|
||||
ddl = module._create_lease_table_sql(SimpleDialect("sqlite"))
|
||||
assert "lease_expires_at DATETIME NOT NULL" in ddl
|
||||
assert "created_at DATETIME NOT NULL" in ddl
|
||||
|
||||
|
||||
def test_phase5_ddl_uses_postgresql_timestamp_with_timezone():
|
||||
module = _migration_module()
|
||||
ddl = module._create_lease_table_sql(SimpleDialect("postgresql"))
|
||||
assert "lease_expires_at TIMESTAMP WITH TIME ZONE NOT NULL" in ddl
|
||||
assert "created_at TIMESTAMP WITH TIME ZONE NOT NULL" in ddl
|
||||
assert "DATETIME" not in ddl
|
||||
|
||||
|
||||
def test_phase5_ddl_uses_mysql_microsecond_datetime():
|
||||
module = _migration_module()
|
||||
ddl = module._create_lease_table_sql(SimpleDialect("mysql"))
|
||||
assert "lease_expires_at DATETIME(6) NOT NULL" in ddl
|
||||
assert "created_at DATETIME(6) NOT NULL" in ddl
|
||||
|
||||
|
||||
class SimpleDialect:
|
||||
def __init__(self, name: str) -> None:
|
||||
self.name = name
|
||||
@@ -0,0 +1,312 @@
|
||||
"""Entry-level MAF Phase-5 approval and target-lease acceptance tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI, Request
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from langchain_core.language_models.chat_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.outputs import ChatGeneration, ChatResult
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.routers.maf_roundtable import router as maf_router
|
||||
from app.maf_roundtable.dispatcher import get_dispatcher
|
||||
from app.maf_roundtable.write_guard_bridge import register_write_guard_service
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.maf_roundtable_config import MafRoundtableConfig, MafToolMetadata
|
||||
from deerflow.config.tool_config import ToolConfig
|
||||
from deerflow.config.write_guard_config import WriteGuardConfig, WriteGuardProviderConfig
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.runtime import RunManager
|
||||
from deerflow.runtime.events.store.memory import MemoryRunEventStore
|
||||
from deerflow.runtime.stream_bridge.memory import MemoryStreamBridge
|
||||
|
||||
TEST_TOOL = "maf_test_outbox_write"
|
||||
|
||||
|
||||
class _User:
|
||||
def __init__(self, user_id: str) -> None:
|
||||
self.id = user_id
|
||||
self.system_role = "user"
|
||||
|
||||
|
||||
class _ThreadStore:
|
||||
def __init__(self) -> None:
|
||||
self.rows: dict[str, dict] = {}
|
||||
|
||||
async def create(self, thread_id, assistant_id=None, metadata=None):
|
||||
self.rows[thread_id] = {
|
||||
"thread_id": thread_id,
|
||||
"assistant_id": assistant_id,
|
||||
"metadata": metadata or {},
|
||||
"status": "idle",
|
||||
}
|
||||
|
||||
async def get(self, thread_id):
|
||||
return self.rows.get(thread_id)
|
||||
|
||||
async def update_status(self, thread_id, status):
|
||||
self.rows.setdefault(thread_id, {})["status"] = status
|
||||
|
||||
async def update_assistant_id(self, thread_id, assistant_id):
|
||||
self.rows.setdefault(thread_id, {})["assistant_id"] = assistant_id
|
||||
|
||||
async def update_metadata(self, thread_id, metadata):
|
||||
self.rows.setdefault(thread_id, {}).setdefault("metadata", {}).update(metadata)
|
||||
|
||||
async def update_display_name(self, thread_id, display_name):
|
||||
self.rows.setdefault(thread_id, {})["display_name"] = display_name
|
||||
|
||||
|
||||
class _DeterministicModel(BaseChatModel):
|
||||
"""Deterministic model boundary: propose one write, then finish."""
|
||||
|
||||
calls: int = 0
|
||||
|
||||
@property
|
||||
def _llm_type(self) -> str:
|
||||
return "maf-phase5-deterministic"
|
||||
|
||||
def bind_tools(self, tools, **kwargs): # noqa: ARG002
|
||||
return self
|
||||
|
||||
def _generate(self, messages, stop=None, run_manager=None, **kwargs): # noqa: ARG002
|
||||
if self.calls == 0:
|
||||
message = AIMessage(
|
||||
content="",
|
||||
tool_calls=[{
|
||||
"name": TEST_TOOL,
|
||||
"args": {"operation_id": "phase5-target", "value": "controlled"},
|
||||
"id": "phase5-tool-call",
|
||||
"type": "tool_call",
|
||||
}],
|
||||
)
|
||||
else:
|
||||
message = AIMessage(content="deterministic completion")
|
||||
self.calls += 1
|
||||
return ChatResult(generations=[ChatGeneration(message=message)])
|
||||
|
||||
|
||||
def _app_config():
|
||||
config = get_app_config().model_copy(deep=True)
|
||||
config.tools = [
|
||||
*config.tools,
|
||||
ToolConfig(
|
||||
name=TEST_TOOL,
|
||||
group="maf-e2e",
|
||||
use="deerflow.tools.builtins.maf_test_outbox:maf_test_outbox_write",
|
||||
),
|
||||
]
|
||||
config.write_guard = WriteGuardConfig(
|
||||
enabled=True,
|
||||
provider=WriteGuardProviderConfig(
|
||||
policy_use="app.maf_roundtable.write_guard_bridge:make_policy",
|
||||
interceptor_use="app.maf_roundtable.write_guard_bridge:make_interceptor",
|
||||
),
|
||||
)
|
||||
config.maf_roundtable = MafRoundtableConfig(
|
||||
enabled=True,
|
||||
default_write_policy="confirm_all_writes",
|
||||
approval_timeout_seconds=300,
|
||||
approval_scan_interval_seconds=1,
|
||||
tool_metadata={
|
||||
TEST_TOOL: MafToolMetadata(
|
||||
side_effect="write",
|
||||
risk_level="R2",
|
||||
target_key_fields=["operation_id"],
|
||||
target_key_namespace="phase5-e2e",
|
||||
)
|
||||
},
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
class _VisibleAgentStore:
|
||||
async def get_visible(self, agent_id: str, user_id: str): # noqa: ARG002
|
||||
return {"id": agent_id, "user_id": user_id}
|
||||
|
||||
async def ensure_builtin(self, record): # noqa: ARG002
|
||||
return None
|
||||
|
||||
|
||||
def _install_test_app(app: FastAPI, service, config) -> None:
|
||||
app.state.config = config
|
||||
app.state.maf_roundtable_service = service
|
||||
app.state.maf_auto_dispatch = True
|
||||
app.state.checkpointer = InMemorySaver()
|
||||
app.state.thread_store = _ThreadStore()
|
||||
app.state.run_manager = RunManager()
|
||||
app.state.stream_bridge = MemoryStreamBridge()
|
||||
app.state.run_event_store = MemoryRunEventStore()
|
||||
app.state.store = None
|
||||
app.state.agent_store = _VisibleAgentStore()
|
||||
register_write_guard_service(service)
|
||||
app.include_router(maf_router)
|
||||
|
||||
|
||||
async def _wait_dispatch(dispatcher, run_id: str) -> None:
|
||||
task = dispatcher._tasks.get(run_id) # noqa: SLF001 - entry test waits on the real task
|
||||
assert task is not None
|
||||
await task
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def deterministic_agent(monkeypatch, tmp_path):
|
||||
agent_config = SimpleNamespace(
|
||||
tool_groups=["maf-e2e"],
|
||||
id="seat-1",
|
||||
description="MAF E2E test seat",
|
||||
model=None,
|
||||
skills=[],
|
||||
subagent_enabled=False,
|
||||
name="MAF E2E seat",
|
||||
)
|
||||
paths = (
|
||||
"deerflow.config.agents_config.load_agent_config",
|
||||
"app.maf_roundtable.capability_snapshot.load_agent_config",
|
||||
"deerflow.agents.lead_agent.agent.load_agent_config",
|
||||
)
|
||||
for path in paths:
|
||||
monkeypatch.setattr(path, lambda _agent_id, cfg=agent_config: cfg)
|
||||
monkeypatch.setattr("deerflow.agents.lead_agent.agent.load_agent_soul", lambda _agent_id: "")
|
||||
monkeypatch.setattr(
|
||||
"deerflow.agents.lead_agent.agent.create_chat_model",
|
||||
lambda **_kwargs: _DeterministicModel(),
|
||||
)
|
||||
config = _app_config()
|
||||
config.title.enabled = False
|
||||
monkeypatch.setattr("deerflow.config.get_app_config", lambda: config)
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "phase5-entry-test-secret")
|
||||
monkeypatch.setenv("MAF_E2E_OUTBOX_DIR", str(tmp_path))
|
||||
return config
|
||||
|
||||
|
||||
async def _make_app(tmp_path, config):
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'maf_entry.db'}")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
store = make_maf_roundtable_store(sf)
|
||||
from app.maf_roundtable.service import MafRoundtableService
|
||||
|
||||
service = MafRoundtableService(
|
||||
store,
|
||||
enabled=True,
|
||||
admin_diagnostic_access=False,
|
||||
config=config.maf_roundtable,
|
||||
agent_store=_VisibleAgentStore(),
|
||||
app_config=config,
|
||||
)
|
||||
app = FastAPI()
|
||||
_install_test_app(app, service, config)
|
||||
|
||||
@app.middleware("http")
|
||||
async def test_auth(request: Request, call_next):
|
||||
request.state.user = _User("u1")
|
||||
return await call_next(request)
|
||||
|
||||
return app, service, engine
|
||||
|
||||
|
||||
async def _create_waiting_run(client: AsyncClient, dispatcher, *, task_text: str, service=None) -> tuple[str, dict]:
|
||||
session_response = await client.post(
|
||||
"/api/maf-roundtable/sessions",
|
||||
json={
|
||||
"task_text": task_text,
|
||||
"write_policy": "confirm_all_writes",
|
||||
"participants": [{"agent_id": "seat-1", "role": "seat"}],
|
||||
},
|
||||
)
|
||||
assert session_response.status_code == 200, session_response.text
|
||||
session = session_response.json()
|
||||
run_response = await client.post(
|
||||
f"/api/maf-roundtable/sessions/{session['id']}/runs",
|
||||
json={},
|
||||
)
|
||||
assert run_response.status_code == 200, run_response.text
|
||||
run = run_response.json()
|
||||
await _wait_dispatch(dispatcher, run["id"])
|
||||
run_state = (await client.get(f"/api/maf-roundtable/runs/{run['id']}")).json()
|
||||
if run_state["status"] != "waiting_approval" and service is not None:
|
||||
actions = await service.store.actions.list_actions(run_id=run["id"])
|
||||
raise AssertionError(f"unexpected run state: {run_state}; actions={actions}")
|
||||
assert run_state["status"] == "waiting_approval"
|
||||
return session["id"], run
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_entry_http_approval_reaches_real_toolnode_once(tmp_path, deterministic_agent):
|
||||
app, service, engine = await _make_app(tmp_path, deterministic_agent)
|
||||
dispatcher = get_dispatcher(app)
|
||||
try:
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
session_id, run = await _create_waiting_run(client, dispatcher, task_text="entry approval", service=service)
|
||||
approvals = (await client.get(f"/api/maf-roundtable/runs/{run['id']}/approvals")).json()["approvals"]
|
||||
assert len(approvals) == 1
|
||||
approval_response = await client.post(
|
||||
f"/api/maf-roundtable/approvals/{approvals[0]['id']}/decision",
|
||||
json={"decision": "approved"},
|
||||
)
|
||||
assert approval_response.status_code == 200, approval_response.text
|
||||
action = (await client.get(f"/api/maf-roundtable/runs/{run['id']}/actions")).json()["actions"][0]
|
||||
assert action["status"] == "succeeded"
|
||||
outbox = tmp_path / "phase5-target.jsonl"
|
||||
assert outbox.exists()
|
||||
assert outbox.read_text(encoding="utf-8").count("\n") == 1
|
||||
assert (await client.get(f"/api/maf-roundtable/sessions/{session_id}")).json()["status"] == "completed"
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_entry_http_same_target_waits_for_explicit_resume(tmp_path, deterministic_agent):
|
||||
app, service, engine = await _make_app(tmp_path, deterministic_agent)
|
||||
dispatcher = get_dispatcher(app)
|
||||
from deerflow.tools.builtins import maf_test_outbox
|
||||
|
||||
started = threading.Event()
|
||||
release = threading.Event()
|
||||
maf_test_outbox.configure_test_block(started=started, release=release)
|
||||
try:
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
_session_one, run_one = await _create_waiting_run(client, dispatcher, task_text="lease one", service=service)
|
||||
approval_one = (await client.get(f"/api/maf-roundtable/runs/{run_one['id']}/approvals")).json()["approvals"][0]
|
||||
first_decision = asyncio.create_task(client.post(
|
||||
f"/api/maf-roundtable/approvals/{approval_one['id']}/decision",
|
||||
json={"decision": "approved"},
|
||||
))
|
||||
assert await asyncio.to_thread(started.wait, 10)
|
||||
|
||||
_session_two, run_two = await _create_waiting_run(client, dispatcher, task_text="lease two", service=service)
|
||||
approval_two = (await client.get(f"/api/maf-roundtable/runs/{run_two['id']}/approvals")).json()["approvals"][0]
|
||||
second_decision = await client.post(
|
||||
f"/api/maf-roundtable/approvals/{approval_two['id']}/decision",
|
||||
json={"decision": "approved"},
|
||||
)
|
||||
assert second_decision.status_code == 200
|
||||
assert (await client.get(f"/api/maf-roundtable/runs/{run_two['id']}")).json()["status"] == "waiting_approval"
|
||||
second_action = (await client.get(f"/api/maf-roundtable/runs/{run_two['id']}/actions")).json()["actions"][0]
|
||||
assert second_action["status"] == "approved"
|
||||
|
||||
release.set()
|
||||
assert (await first_decision).status_code == 200
|
||||
assert (await client.get(f"/api/maf-roundtable/runs/{run_one['id']}")).json()["status"] == "completed"
|
||||
|
||||
resumed = await client.post(f"/api/maf-roundtable/runs/{run_two['id']}/resume")
|
||||
assert resumed.status_code == 200, resumed.text
|
||||
assert (await client.get(f"/api/maf-roundtable/runs/{run_two['id']}")).json()["status"] == "completed"
|
||||
outbox = tmp_path / "phase5-target.jsonl"
|
||||
assert outbox.read_text(encoding="utf-8").count("\n") == 2
|
||||
finally:
|
||||
release.set()
|
||||
maf_test_outbox.clear_test_block()
|
||||
await engine.dispose()
|
||||
@@ -0,0 +1,238 @@
|
||||
"""MAF approval-to-real-tool E2E coverage using the test outbox tool.
|
||||
|
||||
The tool is a real LangChain StructuredTool and is loaded through the normal
|
||||
``get_available_tools`` + LangGraph ``ToolNode`` path. It is intentionally
|
||||
only added to this test's AppConfig; production ``config.yaml`` does not
|
||||
register it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from maf_test_helpers import make_maf_service
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.maf_roundtable.action_coordinator import ActionCoordinator
|
||||
from app.maf_roundtable.tool_executor import ToolExecutor
|
||||
from app.maf_roundtable.write_guard_bridge import (
|
||||
MafWriteGuardInterceptor,
|
||||
MafWriteGuardPolicy,
|
||||
register_write_guard_service,
|
||||
)
|
||||
from deerflow.agents.middlewares.write_guard_middleware import WriteGuardMiddleware
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.tool_config import ToolConfig
|
||||
from deerflow.config.write_guard_config import WriteGuardConfig, WriteGuardProviderConfig
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import Base
|
||||
|
||||
TEST_TOOL = "maf_test_outbox_write"
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def store():
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
result = make_maf_roundtable_store(sf)
|
||||
assert result is not None
|
||||
yield result
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def svc(store):
|
||||
result = make_maf_service(store, enabled=True, admin_diagnostic_access=True)
|
||||
register_write_guard_service(result)
|
||||
return result
|
||||
|
||||
|
||||
def _app_config():
|
||||
config = get_app_config().model_copy(deep=True)
|
||||
config.tools = [
|
||||
*config.tools,
|
||||
ToolConfig(
|
||||
name=TEST_TOOL,
|
||||
group="maf-e2e",
|
||||
use="deerflow.tools.builtins.maf_test_outbox:maf_test_outbox_write",
|
||||
),
|
||||
]
|
||||
config.write_guard = WriteGuardConfig(
|
||||
enabled=True,
|
||||
provider=WriteGuardProviderConfig(
|
||||
policy_use="app.maf_roundtable.write_guard_bridge:make_policy",
|
||||
interceptor_use="app.maf_roundtable.write_guard_bridge:make_interceptor",
|
||||
),
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def _snapshot():
|
||||
tool = {"name": TEST_TOOL, "side_effect": "write", "risk_level": "R2"}
|
||||
return {
|
||||
"tools": [tool],
|
||||
"agents": [{
|
||||
"agent_id": "seat-1",
|
||||
"allowed_tool_names": [TEST_TOOL],
|
||||
"excluded_tools": [],
|
||||
"tools": [tool],
|
||||
}],
|
||||
}
|
||||
|
||||
|
||||
async def _make_pending_action(svc, store):
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "test"})
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
coordinator = ActionCoordinator(svc, user_id="u1")
|
||||
actions = await coordinator.intercept_write_tool_calls(
|
||||
run_id=run["id"],
|
||||
session_id=session["id"],
|
||||
agent_id="seat-1",
|
||||
turn_index=1,
|
||||
tool_calls=[{
|
||||
"name": TEST_TOOL,
|
||||
"id": "call-outbox-1",
|
||||
"args": {"operation_id": "op-1", "value": "controlled-value"},
|
||||
}],
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
assert len(actions) == 1
|
||||
return session, run, coordinator, actions[0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_outbox_requires_approval_and_executes_once(monkeypatch, tmp_path, svc, store):
|
||||
"""Pending action has no side effect; approved ToolNode call writes once."""
|
||||
monkeypatch.setenv("MAF_E2E_OUTBOX_DIR", str(tmp_path))
|
||||
monkeypatch.setattr(
|
||||
"deerflow.config.agents_config.load_agent_config",
|
||||
lambda _agent_id: SimpleNamespace(tool_groups=["maf-e2e"], model=None),
|
||||
)
|
||||
|
||||
session, run, coordinator, action = await _make_pending_action(svc, store)
|
||||
outbox_file = tmp_path / "op-1.jsonl"
|
||||
assert not outbox_file.exists(), "approval interception must not execute the tool"
|
||||
|
||||
assert await store.actions.mark_approved(action["id"]) is True
|
||||
|
||||
class Adapter:
|
||||
class Client:
|
||||
async def execute_tool_directly(self, **kwargs):
|
||||
from deerflow.tools.approved_tool_executor import execute_approved_tool
|
||||
|
||||
return await execute_approved_tool(
|
||||
app_config=_app_config(),
|
||||
checkpointer=None,
|
||||
store=None,
|
||||
thread_id=kwargs["thread_id"],
|
||||
user_id="u1",
|
||||
session_id=kwargs["principal"].session_id,
|
||||
run_id=kwargs["principal"].run_id,
|
||||
agent_id=kwargs["agent_id"],
|
||||
turn_index=kwargs["turn_index"],
|
||||
model_name=None,
|
||||
excluded_tools=[],
|
||||
capability_snapshot=kwargs["capability_snapshot"],
|
||||
write_policy=kwargs["write_policy"],
|
||||
tool_name=kwargs["tool_name"],
|
||||
params=kwargs["params"],
|
||||
approved_action_id=kwargs["approved_action_id"],
|
||||
action_record=kwargs["action_record"],
|
||||
)
|
||||
|
||||
def __init__(self):
|
||||
self._client = self.Client()
|
||||
|
||||
executor = ToolExecutor(
|
||||
adapter=Adapter(),
|
||||
action_coordinator=coordinator,
|
||||
user_id="u1",
|
||||
)
|
||||
first = await executor.execute(
|
||||
action_id=action["id"],
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
agent_id="seat-1",
|
||||
thread_id="thread-1",
|
||||
approved_tool=TEST_TOOL,
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
second = await executor.execute(
|
||||
action_id=action["id"],
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
agent_id="seat-1",
|
||||
thread_id="thread-1",
|
||||
approved_tool=TEST_TOOL,
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
|
||||
assert first["status"] == "succeeded"
|
||||
assert second["status"] == "succeeded"
|
||||
assert outbox_file.read_text(encoding="utf-8").count("\n") == 1
|
||||
stored = await store.actions.get_action(action["id"])
|
||||
assert stored["status"] == "succeeded"
|
||||
assert stored["result"]["tool_result"]["external_object_id"] == "op-1"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rejected_action_never_reaches_real_tool(monkeypatch, tmp_path, svc, store):
|
||||
monkeypatch.setenv("MAF_E2E_OUTBOX_DIR", str(tmp_path))
|
||||
_session, _run, _coordinator, action = await _make_pending_action(svc, store)
|
||||
|
||||
assert await store.actions.mark_rejected(action["id"]) is True
|
||||
assert not list(tmp_path.iterdir())
|
||||
assert (await store.actions.get_action(action["id"]))["status"] == "rejected"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_secret_fails_closed_before_action_creation(monkeypatch, tmp_path, svc, store):
|
||||
monkeypatch.delenv("MAF_ACTION_SECRET_KEY", raising=False)
|
||||
monkeypatch.setenv("MAF_E2E_OUTBOX_DIR", str(tmp_path))
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "test"})
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
|
||||
middleware = WriteGuardMiddleware(
|
||||
policy=MafWriteGuardPolicy(),
|
||||
interceptor=MafWriteGuardInterceptor(),
|
||||
)
|
||||
called = 0
|
||||
|
||||
context = {
|
||||
"maf_run_id": run["id"],
|
||||
"maf_session_id": session["id"],
|
||||
"maf_user_id": "u1",
|
||||
"agent_id": "seat-1",
|
||||
"turn_index": 1,
|
||||
"write_policy": "confirm_all_writes",
|
||||
"capability_snapshot": _snapshot(),
|
||||
}
|
||||
request = SimpleNamespace(
|
||||
tool_call={
|
||||
"name": TEST_TOOL,
|
||||
"id": "call-missing-key",
|
||||
"args": {"operation_id": "op-missing-key", "value": "must-not-run"},
|
||||
},
|
||||
runtime=SimpleNamespace(context=context),
|
||||
)
|
||||
|
||||
async def handler(_request):
|
||||
nonlocal called
|
||||
called += 1
|
||||
return {"executed": True}
|
||||
|
||||
result = await middleware.awrap_tool_call(request, handler)
|
||||
|
||||
envelope = json.loads(result.content)
|
||||
assert envelope["status"] == "intercept_failed"
|
||||
assert called == 0
|
||||
assert not list(tmp_path.iterdir())
|
||||
assert await store.actions.list_actions(run_id=run["id"]) == []
|
||||
@@ -0,0 +1,223 @@
|
||||
"""Real HTTP approval → dispatcher → runner → ToolNode/outbox acceptance test."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI, Request
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from maf_test_helpers import make_maf_service
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.routers.maf_roundtable import router as maf_router
|
||||
from app.maf_roundtable.action_coordinator import ActionCoordinator
|
||||
from app.maf_roundtable.dispatcher import get_dispatcher
|
||||
from app.maf_roundtable.runtime_client import DeerFlowRuntimeClient
|
||||
from app.maf_roundtable.workflow_executors import RoundtableState
|
||||
from app.maf_roundtable.write_guard_bridge import register_write_guard_service
|
||||
from deerflow.config import get_app_config
|
||||
from deerflow.config.maf_roundtable_config import MafRoundtableConfig
|
||||
from deerflow.config.tool_config import ToolConfig
|
||||
from deerflow.config.write_guard_config import WriteGuardConfig, WriteGuardProviderConfig
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.tools.approved_tool_executor import execute_approved_tool
|
||||
|
||||
TEST_TOOL = "maf_test_outbox_write"
|
||||
|
||||
|
||||
class _User:
|
||||
def __init__(self, user_id: str) -> None:
|
||||
self.id = user_id
|
||||
self.system_role = "user"
|
||||
|
||||
|
||||
def _snapshot() -> dict:
|
||||
tool = {
|
||||
"name": TEST_TOOL,
|
||||
"side_effect": "write",
|
||||
"risk_level": "R2",
|
||||
"target_key_fields": ["operation_id"],
|
||||
"target_key_namespace": "maf-e2e",
|
||||
}
|
||||
return {
|
||||
"tools": [tool],
|
||||
"agents": [{
|
||||
"agent_id": "seat-1",
|
||||
"allowed_tool_names": [TEST_TOOL],
|
||||
"excluded_tools": [],
|
||||
"tools": [tool],
|
||||
}],
|
||||
}
|
||||
|
||||
|
||||
def _app_config():
|
||||
config = get_app_config().model_copy(deep=True)
|
||||
config.tools = [
|
||||
*config.tools,
|
||||
ToolConfig(
|
||||
name=TEST_TOOL,
|
||||
group="maf-e2e",
|
||||
use="deerflow.tools.builtins.maf_test_outbox:maf_test_outbox_write",
|
||||
),
|
||||
]
|
||||
config.write_guard = WriteGuardConfig(
|
||||
enabled=True,
|
||||
provider=WriteGuardProviderConfig(
|
||||
policy_use="app.maf_roundtable.write_guard_bridge:make_policy",
|
||||
interceptor_use="app.maf_roundtable.write_guard_bridge:make_interceptor",
|
||||
),
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_approval_dispatches_real_tool_once(monkeypatch, tmp_path):
|
||||
"""A real route decision reaches the ToolNode exactly once, even on replay."""
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "test-only-http-e2e-secret")
|
||||
monkeypatch.setenv("MAF_E2E_OUTBOX_DIR", str(tmp_path))
|
||||
monkeypatch.setattr(
|
||||
"deerflow.config.agents_config.load_agent_config",
|
||||
lambda _agent_id: SimpleNamespace(tool_groups=["maf-e2e"], model=None),
|
||||
)
|
||||
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'maf_http_e2e.db'}")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
store = make_maf_roundtable_store(sf)
|
||||
assert store is not None
|
||||
svc = make_maf_service(
|
||||
store,
|
||||
enabled=True,
|
||||
config=MafRoundtableConfig(enabled=True, approval_timeout_seconds=300),
|
||||
)
|
||||
register_write_guard_service(svc)
|
||||
|
||||
app = FastAPI()
|
||||
app.state.maf_roundtable_service = svc
|
||||
app.include_router(maf_router)
|
||||
|
||||
@app.middleware("http")
|
||||
async def test_auth(request: Request, call_next):
|
||||
request.state.user = _User("u1")
|
||||
return await call_next(request)
|
||||
|
||||
async def execute_directly(_client, **kwargs):
|
||||
return await execute_approved_tool(
|
||||
app_config=_app_config(),
|
||||
checkpointer=None,
|
||||
store=None,
|
||||
thread_id=kwargs["thread_id"],
|
||||
user_id=kwargs["principal"].user_id,
|
||||
session_id=kwargs["principal"].session_id,
|
||||
run_id=kwargs["principal"].run_id,
|
||||
agent_id=kwargs["agent_id"],
|
||||
turn_index=kwargs["turn_index"],
|
||||
model_name=None,
|
||||
excluded_tools=[],
|
||||
capability_snapshot=kwargs["capability_snapshot"],
|
||||
write_policy=kwargs["write_policy"],
|
||||
tool_name=kwargs["tool_name"],
|
||||
params=kwargs["params"],
|
||||
approved_action_id=kwargs["approved_action_id"],
|
||||
action_record=kwargs["action_record"],
|
||||
)
|
||||
|
||||
async def no_model_finalizer(_adapter, state):
|
||||
state.final_result = {"content": "http e2e final report"}
|
||||
|
||||
monkeypatch.setattr(DeerFlowRuntimeClient, "execute_tool_directly", execute_directly)
|
||||
monkeypatch.setattr("app.maf_roundtable.workflow_runner.generate_final_report", no_model_finalizer)
|
||||
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "HTTP E2E"})
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
coordinator = ActionCoordinator(svc, user_id="u1")
|
||||
actions = await coordinator.intercept_write_tool_calls(
|
||||
run_id=run["id"],
|
||||
session_id=session["id"],
|
||||
agent_id="seat-1",
|
||||
turn_index=1,
|
||||
tool_calls=[{
|
||||
"name": TEST_TOOL,
|
||||
"id": "http-outbox-call",
|
||||
"args": {"operation_id": "http-op-1", "value": "controlled"},
|
||||
}],
|
||||
capability_snapshot=_snapshot(),
|
||||
write_policy="confirm_all_writes",
|
||||
)
|
||||
action = actions[0]
|
||||
state = RoundtableState(
|
||||
session_id=session["id"],
|
||||
run_id=run["id"],
|
||||
task_text="HTTP E2E",
|
||||
mode="structured",
|
||||
model_name=None,
|
||||
reasoning_effort=None,
|
||||
max_rounds=1,
|
||||
write_policy="confirm_all_writes",
|
||||
manager_agent_id=None,
|
||||
seat_agent_ids=["seat-1"],
|
||||
thread_map={"seat-1": "thread-1"},
|
||||
capability_snapshot=_snapshot(),
|
||||
)
|
||||
state.current_round = 1
|
||||
state.status = "waiting_approval"
|
||||
state.pending_approvals = [{
|
||||
"action_id": action["id"],
|
||||
"approval_id": action["approval_id"],
|
||||
"agent_id": "seat-1",
|
||||
"tool_name": TEST_TOOL,
|
||||
}]
|
||||
await store.runs.update_run(run["id"], {"status": "waiting_approval", "maf_checkpoint": state.to_dict()})
|
||||
await store.sessions.update_session(
|
||||
session["id"], user_id="u1", fields={"status": "waiting_approval"}
|
||||
)
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
listed = await client.get(f"/api/maf-roundtable/runs/{run['id']}/actions")
|
||||
assert listed.status_code == 200
|
||||
listed_action = listed.json()["actions"][0]
|
||||
assert "target_key" not in listed_action
|
||||
assert "idempotency_key" not in listed_action
|
||||
|
||||
fetched = await client.get(f"/api/maf-roundtable/actions/{action['id']}")
|
||||
assert fetched.status_code == 200
|
||||
fetched_action = fetched.json()
|
||||
assert "target_key" not in fetched_action
|
||||
assert "idempotency_key" not in fetched_action
|
||||
|
||||
# The lease arbiter still needs the private key in persistence.
|
||||
stored_before_decision = await store.actions.get_action(action["id"])
|
||||
assert stored_before_decision["target_key"]
|
||||
assert stored_before_decision["idempotency_key"]
|
||||
|
||||
response = await client.post(
|
||||
f"/api/maf-roundtable/approvals/{action['approval_id']}/decision",
|
||||
json={"decision": "approved"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "approved"
|
||||
|
||||
duplicate = await client.post(
|
||||
f"/api/maf-roundtable/approvals/{action['approval_id']}/decision",
|
||||
json={"decision": "approved"},
|
||||
)
|
||||
assert duplicate.status_code == 409
|
||||
|
||||
outbox = tmp_path / "http-op-1.jsonl"
|
||||
assert outbox.read_text(encoding="utf-8").count("\n") == 1
|
||||
stored = await store.actions.get_action(action["id"])
|
||||
assert stored["status"] == "succeeded"
|
||||
assert stored["attempt_count"] == 1
|
||||
assert (await store.runs.get_run(run["id"]))["status"] == "completed"
|
||||
|
||||
# A process/retry replay after the terminal transition returns the stored
|
||||
# run and never reaches ToolNode again.
|
||||
replay = await get_dispatcher(app).resume(run["id"], user_id="u1")
|
||||
assert replay is not None and replay["status"] == "completed"
|
||||
assert outbox.read_text(encoding="utf-8").count("\n") == 1
|
||||
|
||||
await engine.dispose()
|
||||
@@ -1,7 +1,7 @@
|
||||
"""User-isolation + table-isolation tests for the MAF roundtable experiment (MAF-01-07).
|
||||
|
||||
These tests focus on the security boundary: a non-owner cannot read or mutate
|
||||
another user's session / run / action / approval, and the six MAF tables have
|
||||
another user's session / run / action / lease / approval, and the nine MAF tables have
|
||||
no foreign-key relationship to the legacy ``roundtable_*`` tables (the
|
||||
experiment is fully isolated —see implementation plan §2.3 "不改原有的工程定义).
|
||||
"""
|
||||
@@ -17,6 +17,7 @@ from app.maf_roundtable.service import MafNotFoundError
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
MafRoundtableActionLeaseRow,
|
||||
MafRoundtableActionRow,
|
||||
MafRoundtableActionSecretRow,
|
||||
MafRoundtableApprovalRow,
|
||||
@@ -149,7 +150,7 @@ async def test_admin_diagnostic_access_off_keeps_owner_scope(store):
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_maf_tables_isolated_from_legacy_roundtable(store):
|
||||
"""The six MAF tables must exist and none of their FKs may target a legacy
|
||||
"""The nine MAF tables must exist and none of their FKs may target a legacy
|
||||
roundtable_* table (MAF-01-02 / 退出标净 MAF 表和旀roundtable 表完全无关联外键)."""
|
||||
_, engine = store
|
||||
async with engine.connect() as conn:
|
||||
@@ -162,6 +163,7 @@ async def test_maf_tables_isolated_from_legacy_roundtable(store):
|
||||
"maf_roundtable_runs",
|
||||
"maf_roundtable_events",
|
||||
"maf_roundtable_actions",
|
||||
"maf_roundtable_action_leases",
|
||||
"maf_roundtable_approvals",
|
||||
"maf_roundtable_seat_runs",
|
||||
"maf_roundtable_action_secrets",
|
||||
@@ -174,6 +176,7 @@ async def test_maf_tables_isolated_from_legacy_roundtable(store):
|
||||
MafRoundtableRunRow,
|
||||
MafRoundtableEventRow,
|
||||
MafRoundtableActionRow,
|
||||
MafRoundtableActionLeaseRow,
|
||||
MafRoundtableActionSecretRow,
|
||||
MafRoundtableApprovalRow,
|
||||
MafRoundtableSeatRunRow,
|
||||
|
||||
@@ -770,12 +770,9 @@ def test_validate_maf_prerequisites_passes_when_all_met(monkeypatch):
|
||||
reset_write_guard_config()
|
||||
|
||||
|
||||
def test_validate_maf_prerequisites_requires_secret_key(monkeypatch):
|
||||
"""validate_maf_prerequisites raises when MAF_ACTION_SECRET_KEY is missing."""
|
||||
from app.maf_roundtable.service import (
|
||||
MafConfigurationError,
|
||||
validate_maf_prerequisites,
|
||||
)
|
||||
def test_validate_maf_prerequisites_allows_read_only_without_secret(monkeypatch):
|
||||
"""A missing key leaves reads available; writes fail closed at interception."""
|
||||
from app.maf_roundtable.service import validate_maf_prerequisites
|
||||
from deerflow.config.write_guard_config import (
|
||||
load_write_guard_config_from_dict,
|
||||
reset_write_guard_config,
|
||||
@@ -790,8 +787,34 @@ def test_validate_maf_prerequisites_requires_secret_key(monkeypatch):
|
||||
"interceptor_use": "app.maf_roundtable.write_guard_bridge:make_interceptor",
|
||||
},
|
||||
})
|
||||
with pytest.raises(MafConfigurationError):
|
||||
validate_maf_prerequisites()
|
||||
validate_maf_prerequisites()
|
||||
finally:
|
||||
reset_write_guard_config()
|
||||
|
||||
|
||||
def test_write_guard_health_reports_missing_secret(monkeypatch):
|
||||
from app.maf_roundtable.service import get_maf_write_guard_readiness
|
||||
from deerflow.config.write_guard_config import (
|
||||
load_write_guard_config_from_dict,
|
||||
reset_write_guard_config,
|
||||
)
|
||||
|
||||
monkeypatch.delenv("MAF_ACTION_SECRET_KEY", raising=False)
|
||||
try:
|
||||
load_write_guard_config_from_dict({
|
||||
"enabled": True,
|
||||
"provider": {
|
||||
"policy_use": "app.maf_roundtable.write_guard_bridge:make_policy",
|
||||
"interceptor_use": "app.maf_roundtable.write_guard_bridge:make_interceptor",
|
||||
},
|
||||
})
|
||||
readiness = get_maf_write_guard_readiness()
|
||||
assert readiness == {
|
||||
"enabled": True,
|
||||
"ready": False,
|
||||
"secret_configured": False,
|
||||
"status": "missing_secret",
|
||||
}
|
||||
finally:
|
||||
reset_write_guard_config()
|
||||
|
||||
|
||||
Reference in new issue
Block a user