代码提交

This commit is contained in:
董宏凯 committed 2026-08-03 19:27:54 +08:00
1 parent d93cc62806
commit ddf83f7728
25 files changed
+4086 -424

No files matched your search

@@ -436,3 +436,150 @@ class TestAdapterEventFlow:
events, _ = await svc.list_events(run_id=run["id"], user_id="u1")
assert len(events) == 1
@pytest.mark.asyncio
async def test_emit_critical_passes_persisted_audit_id_to_bridge(self, store):
"""_emit_critical must capture the persisted audit row id and pass it
as source_audit_event_id to _bridge_conversation_event, so projection
dedup is keyed on the real audit row — not an ephemeral input field."""
svc = _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")
client = _mock_client()
adapter = DeerFlowAgentAdapter(client, service=svc, user_id="u1")
bridge_calls: list[dict] = []
original_bridge = adapter._bridge_conversation_event
async def capture_bridge(run_id, event, *, source_audit_event_id=None):
bridge_calls.append({
"run_id": run_id,
"event_type": event.get("event_type"),
"source_audit_event_id": source_audit_event_id,
})
await original_bridge(run_id, event, source_audit_event_id=source_audit_event_id)
adapter._bridge_conversation_event = capture_bridge
critical_event = make_turn_started("expert", 1)
await adapter._emit_critical(run["id"], critical_event)
assert len(bridge_calls) == 1
assert bridge_calls[0]["source_audit_event_id"] is not None
assert isinstance(bridge_calls[0]["source_audit_event_id"], str)
assert len(bridge_calls[0]["source_audit_event_id"]) > 0
@pytest.mark.asyncio
async def test_emit_passes_persisted_audit_id_for_non_critical(self, store):
"""_emit (non-critical path) must also pass persisted audit row id."""
svc = _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")
client = _mock_client()
adapter = DeerFlowAgentAdapter(client, service=svc, user_id="u1")
bridge_calls: list[dict] = []
original_bridge = adapter._bridge_conversation_event
async def capture_bridge(run_id, event, *, source_audit_event_id=None):
bridge_calls.append({
"source_audit_event_id": source_audit_event_id,
})
await original_bridge(run_id, event, source_audit_event_id=source_audit_event_id)
adapter._bridge_conversation_event = capture_bridge
ev = {
"event_type": "agent.message.delta",
"agent_id": "a1",
"turn_index": 0,
"payload": {"text": "hello"},
"source_event_id": "stream:evt_001",
}
await adapter._emit(run["id"], ev)
assert len(bridge_calls) == 1
assert bridge_calls[0]["source_audit_event_id"] is not None
@pytest.mark.asyncio
async def test_emit_critical_dedup_across_simulated_restart(self, store):
"""Simulate: emit critical event, then 'restart' and re-emit the same
event via reconcile. The conversation projection must be deduped via
the persisted audit row id — no duplicate conversation events."""
from app.maf_roundtable.conversation_event_hub import ConversationEventHub
from app.maf_roundtable.conversation_service import MafConversationService
class FakeMafConfig:
enabled = True
default_mode = "structured"
default_model_name = "test"
default_reasoning_effort = None
default_manager_agent_id = "mgr"
default_max_rounds = 8
default_write_policy = "confirm_high_risk"
admin_diagnostic_access = True
hub = ConversationEventHub(heartbeat_interval=0.3)
conv_svc = MafConversationService(store, maf_config=FakeMafConfig(), event_hub=hub)
conv = await conv_svc.create_conversation(user_id="u1", title="RestartDedup")
from sqlalchemy import select
from deerflow.persistence.maf_roundtable.model import (
MafRoundtableConversationRow,
MafRoundtableRunRow,
MafRoundtableSessionRow,
)
svc = _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")
async with store.session_factory() as db:
async with db.begin():
conv_row = (
await db.execute(
select(MafRoundtableConversationRow).where(
MafRoundtableConversationRow.id == conv["id"]
).with_for_update()
)
).scalars().first()
assert conv_row is not None
sess_row = (
await db.execute(
select(MafRoundtableSessionRow).where(
MafRoundtableSessionRow.id == session["id"]
)
)
).scalars().first()
conv_row.internal_session_id = sess_row.id
run_row = (
await db.execute(
select(MafRoundtableRunRow).where(
MafRoundtableRunRow.id == run["id"]
)
)
).scalars().first()
assert run_row is not None
client = _mock_client()
adapter = DeerFlowAgentAdapter(client, service=svc, user_id="u1")
critical_event = {
"event_type": "action.waiting_approval",
"agent_id": "seat_a",
"turn_index": 1,
"payload": {"tool_name": "write_file", "summary": "high risk"},
}
await adapter._emit_critical(run["id"], critical_event)
await conv_svc.reconcile_conversation_events(run_id=run["id"])
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
approval_events = [e for e in events if e["type"] == "approval.required"]
assert len(approval_events) == 1
hub.shutdown()