fix(maf): harden phase 5 approval safety

This commit is contained in:
董宏凯 committed 2026-08-02 14:32:49 +08:00
1 parent f82363d81f
commit a55992e31c
11 files changed
+489 -146

No files matched your search

@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import threading
from datetime import UTC, datetime, timedelta
from types import SimpleNamespace
import pytest
@@ -13,8 +14,10 @@ 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 import update
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway.app import _scan_maf_approval_expiry
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
@@ -24,6 +27,7 @@ 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.persistence.maf_roundtable.model import MafRoundtableApprovalRow
from deerflow.runtime import RunManager
from deerflow.runtime.events.store.memory import MemoryRunEventStore
from deerflow.runtime.stream_bridge.memory import MemoryStreamBridge
@@ -115,7 +119,7 @@ def _app_config():
enabled=True,
default_write_policy="confirm_all_writes",
approval_timeout_seconds=300,
approval_scan_interval_seconds=1,
approval_scan_interval_seconds=5,
tool_metadata={
TEST_TOOL: MafToolMetadata(
side_effect="write",
@@ -266,6 +270,39 @@ async def test_entry_http_approval_reaches_real_toolnode_once(tmp_path, determin
await engine.dispose()
@pytest.mark.asyncio
async def test_entry_approval_scanner_expires_and_finishes_without_tool(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="scanner expiry", service=service
)
approval = (await client.get(
f"/api/maf-roundtable/runs/{run['id']}/approvals"
)).json()["approvals"][0]
async with service.store.session_factory() as db:
await db.execute(
update(MafRoundtableApprovalRow)
.where(MafRoundtableApprovalRow.id == approval["id"])
.values(expires_at=datetime.now(UTC) - timedelta(seconds=1))
)
await db.commit()
assert await _scan_maf_approval_expiry(app) == [run["id"]]
final_run = (await client.get(f"/api/maf-roundtable/runs/{run['id']}")).json()
assert final_run["status"] == "completed"
action = (await client.get(
f"/api/maf-roundtable/runs/{run['id']}/actions"
)).json()["actions"][0]
assert action["status"] == "rejected"
assert not (tmp_path / "phase5-target.jsonl").exists()
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)