feat(maf): conversation-driven MAF roundtable (R1/R2) + server-owned stage-3 dispatch
Backend:
- Conversation API at /api/maf-roundtable/conversations: create/list/detail,
paginated messages, atomic send+run start, owner-only isolation,
admin read-only diagnostic access
- SSE real-time event stream (GET /conversations/{id}/stream) backed by
ConversationEventHub + conversation_events store; multi-worker deployment doc
- Deterministic server-owned stage-3 dispatch contracts (dispatch_contracts.py):
routing resolved from trusted MAF deployment config instead of manager-chosen
agent ids; config.yaml keeps stage-3 routing deployment-owned
(default_manager_agent_id / seat ids / business_routes / max_seats_per_run)
- Conversation persistence (conversations/messages stores) + v2 migration script
- Tests: conversations, conversation stream, migration, phase3 conversation dispatch
Frontend:
- Vitest test infra for MAF (vitest.maf.config.ts + test:maf script) with
component tests for ApprovalCard/RunHeader/SessionsPage and use-maf-events
- Session form lib, flow-graph dialog / run-detail / sessions page updates,
routes and nav tweaks
- Docs: R0 legacy interaction baseline mapping, updated implementation plan
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
df408831e5
commit
99642fca6c
46 files changed
+6358
-1306
No files matched your search
@@ -0,0 +1,190 @@
|
||||
"""Tests for the MAF roundtable conversation V2 migration script.
|
||||
|
||||
Covers:
|
||||
- SQLite real migration idempotency (run twice, no error).
|
||||
- Schema validation passes after migration.
|
||||
- Deprecated last_message_seq column is not present.
|
||||
- PostgreSQL / MySQL DDL assertions (dialect-specific SQL generation).
|
||||
- Existing tables are not destroyed by re-running.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import inspect, text
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
|
||||
from scripts.migrate_maf_roundtable_conversation_v2 import (
|
||||
CONVERSATIONS_TABLE,
|
||||
EVENTS_TABLE,
|
||||
MESSAGES_TABLE,
|
||||
_conversations_ddl,
|
||||
_events_ddl,
|
||||
_indexes_ddl,
|
||||
_messages_ddl,
|
||||
run_migration,
|
||||
validate_schema,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sqlite_migration_creates_tables(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'mig.db'}"
|
||||
await run_migration(db_url)
|
||||
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
def _check(sync_conn):
|
||||
insp = inspect(sync_conn)
|
||||
tables = insp.get_table_names()
|
||||
issues = validate_schema(insp)
|
||||
return tables, issues
|
||||
|
||||
tables, issues = await conn.run_sync(_check)
|
||||
assert CONVERSATIONS_TABLE in tables
|
||||
assert MESSAGES_TABLE in tables
|
||||
assert EVENTS_TABLE in tables
|
||||
assert issues == [], f"Schema issues: {issues}"
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sqlite_migration_idempotent(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'mig2.db'}"
|
||||
await run_migration(db_url)
|
||||
await run_migration(db_url)
|
||||
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
def _check(sync_conn):
|
||||
return validate_schema(inspect(sync_conn))
|
||||
|
||||
issues = await conn.run_sync(_check)
|
||||
assert issues == [], f"Schema issues after 2nd run: {issues}"
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_deprecated_last_message_seq(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'mig3.db'}"
|
||||
await run_migration(db_url)
|
||||
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
def _check(sync_conn):
|
||||
insp = inspect(sync_conn)
|
||||
return {c["name"] for c in insp.get_columns(CONVERSATIONS_TABLE)}
|
||||
|
||||
cols = await conn.run_sync(_check)
|
||||
assert "last_stream_seq" in cols
|
||||
assert "last_message_seq" not in cols
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_existing_data_preserved(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'mig4.db'}"
|
||||
await run_migration(db_url)
|
||||
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
await conn.execute(text(
|
||||
f"INSERT INTO {CONVERSATIONS_TABLE} (id, owner_user_id, title, safe_status, last_stream_seq, created_at, updated_at) "
|
||||
"VALUES ('c1', 'u1', 'keep', 'idle', 0, '2026-01-01', '2026-01-01')"
|
||||
))
|
||||
|
||||
await run_migration(db_url)
|
||||
|
||||
async with engine.begin() as conn:
|
||||
row = (await conn.execute(text(f"SELECT id, title FROM {CONVERSATIONS_TABLE} WHERE id='c1'"))).first()
|
||||
assert row is not None
|
||||
assert row[1] == "keep"
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_partial_conversation_table_is_rebuilt_with_foreign_keys(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'partial.db'}"
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
# Represents the first uncommitted V2 draft: the table exists but its
|
||||
# private run/session links were not constrained.
|
||||
await conn.execute(text(
|
||||
f"CREATE TABLE {CONVERSATIONS_TABLE} ("
|
||||
"id VARCHAR(64) PRIMARY KEY, owner_user_id VARCHAR(64) NOT NULL, "
|
||||
"title VARCHAR(255) NOT NULL DEFAULT '', task_context_json TEXT, "
|
||||
"safe_status VARCHAR(32) NOT NULL DEFAULT 'idle', active_run_id VARCHAR(64), "
|
||||
"internal_session_id VARCHAR(64), last_stream_seq INTEGER NOT NULL DEFAULT 0, "
|
||||
"created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL)"
|
||||
))
|
||||
await engine.dispose()
|
||||
|
||||
await run_migration(db_url)
|
||||
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
def _check(sync_conn):
|
||||
return validate_schema(inspect(sync_conn))
|
||||
|
||||
assert await conn.run_sync(_check) == []
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nonempty_partial_conversation_table_fails_closed(tmp_path):
|
||||
db_url = f"sqlite+aiosqlite:///{tmp_path / 'partial-data.db'}"
|
||||
engine = create_async_engine(db_url)
|
||||
async with engine.begin() as conn:
|
||||
await conn.execute(text(
|
||||
f"CREATE TABLE {CONVERSATIONS_TABLE} ("
|
||||
"id VARCHAR(64) PRIMARY KEY, owner_user_id VARCHAR(64) NOT NULL, "
|
||||
"title VARCHAR(255) NOT NULL DEFAULT '', safe_status VARCHAR(32) NOT NULL DEFAULT 'idle', "
|
||||
"last_stream_seq INTEGER NOT NULL DEFAULT 0, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL)"
|
||||
))
|
||||
await conn.execute(text(
|
||||
f"INSERT INTO {CONVERSATIONS_TABLE} (id, owner_user_id, title, safe_status, last_stream_seq, created_at, updated_at) "
|
||||
"VALUES ('c1', 'u1', 'keep', 'idle', 0, '2026-01-01', '2026-01-01')"
|
||||
))
|
||||
await engine.dispose()
|
||||
|
||||
with pytest.raises(RuntimeError, match="refuses to delete conversation history"):
|
||||
await run_migration(db_url)
|
||||
|
||||
|
||||
def test_postgresql_ddl_generation():
|
||||
ddl = _conversations_ddl("postgresql")
|
||||
assert "TIMESTAMP WITH TIME ZONE" in ddl
|
||||
assert "JSONB" in ddl
|
||||
assert CONVERSATIONS_TABLE in ddl
|
||||
assert "FOREIGN KEY (active_run_id) REFERENCES maf_roundtable_runs" in ddl
|
||||
assert "FOREIGN KEY (internal_session_id) REFERENCES maf_roundtable_sessions" in ddl
|
||||
|
||||
msg_ddl = _messages_ddl("postgresql")
|
||||
assert "ON DELETE CASCADE" in msg_ddl
|
||||
assert "UNIQUE (conversation_id, seq)" in msg_ddl
|
||||
|
||||
evt_ddl = _events_ddl("postgresql")
|
||||
assert "UNIQUE (conversation_id, seq)" in evt_ddl
|
||||
|
||||
|
||||
def test_mysql_ddl_generation():
|
||||
ddl = _conversations_ddl("mysql")
|
||||
assert "DATETIME(6)" in ddl
|
||||
assert "JSON" in ddl
|
||||
assert "FOREIGN KEY (active_run_id) REFERENCES maf_roundtable_runs" in ddl
|
||||
|
||||
msg_ddl = _messages_ddl("mysql")
|
||||
assert "LONGTEXT" in msg_ddl
|
||||
assert "ON DELETE CASCADE" in msg_ddl
|
||||
|
||||
|
||||
def test_sqlite_ddl_generation():
|
||||
ddl = _conversations_ddl("sqlite")
|
||||
assert "DATETIME" in ddl
|
||||
assert "TEXT" in ddl
|
||||
|
||||
indexes = _indexes_ddl("sqlite")
|
||||
assert len(indexes) >= 8
|
||||
assert any("ix_maf_conv_owner_updated" in i for i in indexes)
|
||||
assert any("ix_maf_msg_conv_seq" in i for i in indexes)
|
||||
assert any("ix_maf_cevt_conv_seq" in i for i in indexes)
|
||||
@@ -0,0 +1,473 @@
|
||||
"""Tests for MAF roundtable V2 Conversation SSE streaming (Phase 2 / R2 + P0 fixes).
|
||||
|
||||
Covers:
|
||||
- ConversationEventHub: generation counter, publish/subscribe, multi-subscriber.
|
||||
- P0-1: race-free replay — events published during replay are not lost.
|
||||
- P0-2: terminal events auto-close the stream.
|
||||
- P0-4: projection dedup via source_event_id.
|
||||
- SSE replay: events persisted before connect are delivered.
|
||||
- after_seq cursor: reconnect resumes without duplicates.
|
||||
- Heartbeat: idle connection produces ``: heartbeat`` frames.
|
||||
- Concurrent subscribers: fan-out to multiple listeners.
|
||||
- Ownership: non-owner gets 404 on the stream endpoint.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.maf_roundtable.conversation_event_hub import ConversationEventHub
|
||||
from app.maf_roundtable.conversation_service import (
|
||||
ConversationNotFoundError,
|
||||
MafConversationService,
|
||||
)
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
MafRoundtableConversationRow,
|
||||
)
|
||||
from deerflow.persistence.maf_roundtable.store import MafRoundtableStore
|
||||
|
||||
|
||||
class FakeMafConfig:
|
||||
enabled = True
|
||||
default_mode = "structured"
|
||||
default_model_name = "test-model"
|
||||
default_reasoning_effort = None
|
||||
default_manager_agent_id = "mgr-agent"
|
||||
default_max_rounds = 8
|
||||
default_write_policy = "confirm_high_risk"
|
||||
admin_diagnostic_access = True
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def store(tmp_path):
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'stream.db'}")
|
||||
async with engine.begin() as conn:
|
||||
import deerflow.persistence.maf_roundtable.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
yield make_maf_roundtable_store(sf)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def hub():
|
||||
h = ConversationEventHub(heartbeat_interval=0.3)
|
||||
yield h
|
||||
h.shutdown()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def svc(store, hub):
|
||||
return MafConversationService(store, maf_config=FakeMafConfig(), event_hub=hub)
|
||||
|
||||
|
||||
# ── ConversationEventHub unit tests ───────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_publish_notifies_subscriber(hub):
|
||||
sub = hub.open_subscription("c1")
|
||||
assert not sub.has_pending
|
||||
|
||||
await hub.publish("c1")
|
||||
assert sub.has_pending
|
||||
|
||||
changed = await sub.wait_for_change(timeout=2.0)
|
||||
assert changed
|
||||
sub.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_heartbeat_on_idle(hub):
|
||||
sub = hub.open_subscription("c2")
|
||||
changed = await sub.wait_for_change(timeout=0.2)
|
||||
assert not changed
|
||||
sub.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_multi_subscriber_fanout(hub):
|
||||
sub_a = hub.open_subscription("c3")
|
||||
sub_b = hub.open_subscription("c3")
|
||||
|
||||
await hub.publish("c3")
|
||||
|
||||
assert await sub_a.wait_for_change(timeout=2.0)
|
||||
assert await sub_b.wait_for_change(timeout=2.0)
|
||||
|
||||
sub_a.close()
|
||||
sub_b.close()
|
||||
assert hub.conversation_subscriber_count("c3") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_publish_no_subscribers_is_noop(hub):
|
||||
await hub.publish("c_noone")
|
||||
assert hub.conversation_subscriber_count("c_noone") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_unsubscribe_cleans_up(hub):
|
||||
sub = hub.open_subscription("c4")
|
||||
assert hub.conversation_subscriber_count("c4") == 1
|
||||
sub.close()
|
||||
assert hub.conversation_subscriber_count("c4") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hub_shutdown_stops_subscribers():
|
||||
h = ConversationEventHub(heartbeat_interval=0.2)
|
||||
sub = h.open_subscription("c_shutdown")
|
||||
h.shutdown()
|
||||
assert h.is_closed
|
||||
# After shutdown, generation should have advanced.
|
||||
assert sub.has_pending or sub.current_generation > sub.seen_generation
|
||||
sub.close()
|
||||
|
||||
|
||||
# ── P0-1: Race-free replay ────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_1_event_during_replay_is_not_lost(svc, store, hub):
|
||||
"""Event published between list_events() and subscription must be caught
|
||||
by the stabilisation loop."""
|
||||
conv = await svc.create_conversation(user_id="u1", title="Race")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="msg1")
|
||||
await _reset_conv_to_idle(store, conv["id"])
|
||||
|
||||
# Simulate: subscribe, replay, then a publish sneaks in before stabilise.
|
||||
sub = hub.open_subscription(conv["id"])
|
||||
last_seq = 0
|
||||
|
||||
# Phase 1: initial replay
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=last_seq)
|
||||
for evt in events:
|
||||
last_seq = max(last_seq, evt["seq"])
|
||||
|
||||
# Event sneaks in here (between replay and stabilise)
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="sneaky")
|
||||
await hub.publish(conv["id"])
|
||||
|
||||
# Stabilise: should detect the pending generation
|
||||
assert sub.has_pending
|
||||
sub.mark_seen()
|
||||
late_events = await store.conversation_events.list_events(conv["id"], after_seq=last_seq)
|
||||
assert len(late_events) >= 1 # The sneaky event was caught
|
||||
for evt in late_events:
|
||||
last_seq = max(last_seq, evt["seq"])
|
||||
|
||||
sub.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_1_subscribe_before_replay_catches_concurrent_publish(svc, store, hub):
|
||||
"""Subscribe must be opened BEFORE any DB query so a publish during replay
|
||||
is detected via generation delta."""
|
||||
conv = await svc.create_conversation(user_id="u1", title="SubBefore")
|
||||
|
||||
sub = hub.open_subscription(conv["id"])
|
||||
|
||||
# No events yet
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
assert len(events) == 0
|
||||
|
||||
# Publish arrives
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="first")
|
||||
await hub.publish(conv["id"])
|
||||
|
||||
# Stabilisation detects it
|
||||
assert sub.has_pending
|
||||
sub.mark_seen()
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
assert len(events) >= 1
|
||||
|
||||
sub.close()
|
||||
|
||||
|
||||
# ── P0-2: Terminal events auto-close stream ──────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_2_replay_terminal_event_closes_stream():
|
||||
from app.gateway.routers.maf_conversations import _TERMINAL_EVENT_TYPES, _is_terminal
|
||||
|
||||
assert "run.completed" in _TERMINAL_EVENT_TYPES
|
||||
assert "run.error" in _TERMINAL_EVENT_TYPES
|
||||
assert "run.cancelled" in _TERMINAL_EVENT_TYPES
|
||||
|
||||
assert _is_terminal({"type": "run.completed"})
|
||||
assert _is_terminal({"type": "run.error"})
|
||||
assert _is_terminal({"type": "run.cancelled"})
|
||||
assert not _is_terminal({"type": "agent.message.delta"})
|
||||
assert not _is_terminal({"type": "user.message.created"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_2_terminal_event_in_replay_stops_generator(store):
|
||||
"""If the replay phase hits a terminal event, the generator should stop."""
|
||||
from app.gateway.routers.maf_conversations import _is_terminal
|
||||
|
||||
# Simulate a conversation with events ending in run.completed
|
||||
events = [
|
||||
{"seq": 1, "type": "user.message.created"},
|
||||
{"seq": 2, "type": "agent.message.delta"},
|
||||
{"seq": 3, "type": "run.completed"},
|
||||
{"seq": 4, "type": "should_not_reach"},
|
||||
]
|
||||
|
||||
found_terminal = False
|
||||
for evt in events:
|
||||
if _is_terminal(evt):
|
||||
found_terminal = True
|
||||
break
|
||||
assert found_terminal
|
||||
# Verify the event after terminal would not be reached
|
||||
terminal_idx = next(i for i, e in enumerate(events) if _is_terminal(e))
|
||||
assert events[terminal_idx]["type"] == "run.completed"
|
||||
|
||||
|
||||
# ── P0-4: Projection dedup ────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_4_dedup_same_source_event_id(svc, store):
|
||||
"""Projecting the same source_event_id twice should not create a duplicate."""
|
||||
conv = await svc.create_conversation(user_id="u1", title="Dedup")
|
||||
|
||||
result1 = await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"],
|
||||
owner_user_id="u1",
|
||||
event_type="tool.started",
|
||||
event_data={"tool_name": "search"},
|
||||
source_event_id="evt_src_001",
|
||||
)
|
||||
assert result1 is True
|
||||
|
||||
result2 = await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"],
|
||||
owner_user_id="u1",
|
||||
event_type="tool.started",
|
||||
event_data={"tool_name": "search"},
|
||||
source_event_id="evt_src_001",
|
||||
)
|
||||
assert result2 is False # Deduped
|
||||
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
tool_events = [e for e in events if e["type"] == "tool.started"]
|
||||
assert len(tool_events) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_4_different_source_event_ids_both_written(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="MultiSrc")
|
||||
|
||||
await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"], owner_user_id="u1",
|
||||
event_type="tool.started", event_data={"tool_name": "a"},
|
||||
source_event_id="src_1",
|
||||
)
|
||||
await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"], owner_user_id="u1",
|
||||
event_type="tool.started", event_data={"tool_name": "b"},
|
||||
source_event_id="src_2",
|
||||
)
|
||||
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
tool_events = [e for e in events if e["type"] == "tool.started"]
|
||||
assert len(tool_events) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_p0_4_null_source_event_id_not_deduped(svc, store):
|
||||
"""Events with no source_event_id should never be deduped."""
|
||||
conv = await svc.create_conversation(user_id="u1", title="NoSrc")
|
||||
|
||||
await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"], owner_user_id="u1",
|
||||
event_type="manager.dispatch", event_data={"reason": "x"},
|
||||
source_event_id=None,
|
||||
)
|
||||
await svc._publish_conversation_event(
|
||||
conversation_id=conv["id"], owner_user_id="u1",
|
||||
event_type="manager.dispatch", event_data={"reason": "y"},
|
||||
source_event_id=None,
|
||||
)
|
||||
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
assert len(events) == 2
|
||||
|
||||
|
||||
# ── SSE replay + cursor + real-time integration ──────────────────────────
|
||||
|
||||
|
||||
async def _reset_conv_to_idle(store: MafRoundtableStore, conv_id: str):
|
||||
async with store.session_factory() as session:
|
||||
row = await session.get(MafRoundtableConversationRow, conv_id)
|
||||
if row:
|
||||
row.safe_status = "idle"
|
||||
row.active_run_id = None
|
||||
await session.commit()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_replay_delivers_past_events(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Replay")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="msg1")
|
||||
await _reset_conv_to_idle(store, conv["id"])
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="msg2")
|
||||
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
assert len(events) >= 2
|
||||
types = [e["type"] for e in events]
|
||||
assert types.count("user.message.created") == 2
|
||||
seqs = [e["seq"] for e in events]
|
||||
assert seqs == sorted(seqs)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_after_seq_prevents_duplicates(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Cursor")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="first")
|
||||
await _reset_conv_to_idle(store, conv["id"])
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="second")
|
||||
|
||||
all_events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
last_seq = all_events[0]["seq"]
|
||||
|
||||
replay = await store.conversation_events.list_events(conv["id"], after_seq=last_seq)
|
||||
assert all(e["seq"] > last_seq for e in replay)
|
||||
assert len(replay) == len(all_events) - 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_real_time_event_pushed_after_subscribe(svc, store, hub):
|
||||
conv = await svc.create_conversation(user_id="u1", title="RT")
|
||||
|
||||
received_events: list[dict] = []
|
||||
|
||||
async def stream_consumer():
|
||||
sub = hub.open_subscription(conv["id"])
|
||||
last_seq = 0
|
||||
|
||||
# Stabilise (no events yet)
|
||||
while sub.has_pending:
|
||||
sub.mark_seen()
|
||||
|
||||
# Wait for change
|
||||
changed = await sub.wait_for_change(timeout=5.0)
|
||||
if changed:
|
||||
new_evts = await store.conversation_events.list_events(conv["id"], after_seq=last_seq)
|
||||
for evt in new_evts:
|
||||
received_events.append(evt)
|
||||
last_seq = max(last_seq, evt["seq"])
|
||||
sub.close()
|
||||
|
||||
task = asyncio.create_task(stream_consumer())
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="live msg")
|
||||
|
||||
await asyncio.wait_for(task, timeout=5.0)
|
||||
types = [e["type"] for e in received_events]
|
||||
assert "user.message.created" in types
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_concurrent_subscribers_both_receive(svc, store, hub):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Fanout")
|
||||
|
||||
buf_a: list[dict] = []
|
||||
buf_b: list[dict] = []
|
||||
|
||||
async def consume(buf):
|
||||
sub = hub.open_subscription(conv["id"])
|
||||
last_seq = 0
|
||||
changed = await sub.wait_for_change(timeout=5.0)
|
||||
if changed:
|
||||
new_evts = await store.conversation_events.list_events(conv["id"], after_seq=last_seq)
|
||||
for evt in new_evts:
|
||||
buf.append(evt)
|
||||
last_seq = max(last_seq, evt["seq"])
|
||||
sub.close()
|
||||
|
||||
ta = asyncio.create_task(consume(buf_a))
|
||||
tb = asyncio.create_task(consume(buf_b))
|
||||
await asyncio.sleep(0.15)
|
||||
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="broadcast")
|
||||
|
||||
await asyncio.wait_for(asyncio.gather(ta, tb), timeout=5.0)
|
||||
assert len(buf_a) >= 1
|
||||
assert len(buf_b) >= 1
|
||||
assert buf_a[0]["seq"] == buf_b[0]["seq"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reconnect_after_seq_resume(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Reconnect")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="batch1")
|
||||
await _reset_conv_to_idle(store, conv["id"])
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="batch2")
|
||||
|
||||
all_events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
midpoint = all_events[len(all_events) // 2]["seq"]
|
||||
|
||||
replay = await store.conversation_events.list_events(conv["id"], after_seq=midpoint)
|
||||
assert all(e["seq"] > midpoint for e in replay)
|
||||
|
||||
|
||||
# ── SSE frame format ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_frame_format():
|
||||
from app.gateway.routers.maf_conversations import _format_heartbeat, _format_sse_event
|
||||
|
||||
frame = _format_sse_event({
|
||||
"seq": 42,
|
||||
"event_id": "evt_abc",
|
||||
"conversation_id": "conv_1",
|
||||
"run_id": "run_1",
|
||||
"type": "user.message.created",
|
||||
"occurred_at": "2026-08-03T00:00:00Z",
|
||||
"data": {"content": "hi"},
|
||||
})
|
||||
assert "event: user.message.created" in frame
|
||||
assert "data: " in frame
|
||||
assert "id: evt_abc" in frame
|
||||
assert frame.endswith("\n\n")
|
||||
|
||||
parsed = None
|
||||
for line in frame.splitlines():
|
||||
if line.startswith("data: "):
|
||||
parsed = json.loads(line[len("data: "):])
|
||||
break
|
||||
assert parsed is not None
|
||||
assert parsed["seq"] == 42
|
||||
assert parsed["type"] == "user.message.created"
|
||||
|
||||
hb = _format_heartbeat()
|
||||
assert hb == ": heartbeat\n\n"
|
||||
|
||||
|
||||
# ── Ownership ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ownership_check_before_stream(svc):
|
||||
conv = await svc.create_conversation(user_id="u1", title="OwnerOnly")
|
||||
|
||||
fetched = await svc.get_conversation(conversation_id=conv["id"], user_id="u1")
|
||||
assert fetched["id"] == conv["id"]
|
||||
|
||||
with pytest.raises(ConversationNotFoundError):
|
||||
await svc.get_conversation(conversation_id=conv["id"], user_id="u2")
|
||||
@@ -0,0 +1,390 @@
|
||||
"""Tests for the MAF roundtable V2 Conversation API (Phase 1 / R1).
|
||||
|
||||
Covers:
|
||||
- Create, list, detail, message pagination, user isolation.
|
||||
- Public API responses never leak internal_session_id, active_run_id, run_id,
|
||||
owner_user_id, thread ids, or secret-class fields.
|
||||
- First message creates a real internal session; run FK is valid.
|
||||
- Transaction rollback: any flush failure rolls back message + run + event + status.
|
||||
- Concurrent send: only one succeeds, the other gets 409; no duplicate runs.
|
||||
- Mixed tool-events and messages: last_stream_seq is contiguous and usable as SSE cursor.
|
||||
- Empty / overlong / forged-internal-field content → 422.
|
||||
- Non-owner cannot read/write; admin cannot write another user's conversation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import inspect, select
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.routers.maf_conversations import _user_ctx
|
||||
from app.maf_roundtable.conversation_schemas import (
|
||||
CreateConversationRequest,
|
||||
SendMessageRequest,
|
||||
)
|
||||
from app.maf_roundtable.conversation_service import (
|
||||
ConversationConflictError,
|
||||
ConversationDisabledError,
|
||||
ConversationNotFoundError,
|
||||
MafConversationService,
|
||||
)
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
MafRoundtableConversationRow,
|
||||
MafRoundtableMessageRow,
|
||||
MafRoundtableRunRow,
|
||||
MafRoundtableSessionRow,
|
||||
)
|
||||
|
||||
|
||||
class FakeMafConfig:
|
||||
enabled = True
|
||||
default_mode = "structured"
|
||||
default_model_name = "test-model"
|
||||
default_reasoning_effort = None
|
||||
default_manager_agent_id = "mgr-agent"
|
||||
default_max_rounds = 8
|
||||
default_write_policy = "confirm_high_risk"
|
||||
admin_diagnostic_access = True
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def store(tmp_path):
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'conv.db'}")
|
||||
async with engine.begin() as conn:
|
||||
import deerflow.persistence.maf_roundtable.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
yield make_maf_roundtable_store(sf)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def svc(store):
|
||||
return MafConversationService(store, maf_config=FakeMafConfig())
|
||||
|
||||
|
||||
FORBIDDEN_FIELDS = {
|
||||
"owner_user_id", "internal_session_id", "active_run_id",
|
||||
"thread_id", "capability_snapshot", "idempotency_key",
|
||||
"target_key", "internal_canary",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_and_get_conversation(svc):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Test", task_context={"business_code": "3Q"})
|
||||
assert conv["id"]
|
||||
assert conv["title"] == "Test"
|
||||
assert conv["safe_status"] == "idle"
|
||||
assert conv["last_stream_seq"] == 0
|
||||
assert conv["task_context"] == {"business_code": "3Q"}
|
||||
for field in FORBIDDEN_FIELDS:
|
||||
assert field not in conv, f"Leaked field: {field}"
|
||||
|
||||
fetched = await svc.get_conversation(conversation_id=conv["id"], user_id="u1")
|
||||
assert fetched["id"] == conv["id"]
|
||||
for field in FORBIDDEN_FIELDS:
|
||||
assert field not in fetched
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_conversations_owner_scoped(svc):
|
||||
await svc.create_conversation(user_id="u1", title="A")
|
||||
await svc.create_conversation(user_id="u1", title="B")
|
||||
await svc.create_conversation(user_id="u2", title="C")
|
||||
|
||||
list_u1, total_u1 = await svc.list_conversations(user_id="u1")
|
||||
assert total_u1 == 2
|
||||
assert all(c["id"] for c in list_u1)
|
||||
|
||||
list_u2, total_u2 = await svc.list_conversations(user_id="u2")
|
||||
assert total_u2 == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_conversation_wrong_owner_returns_404(svc):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Private")
|
||||
with pytest.raises(ConversationNotFoundError):
|
||||
await svc.get_conversation(conversation_id=conv["id"], user_id="u2")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_can_read_but_not_write(svc):
|
||||
conv = await svc.create_conversation(user_id="u1", title="AdminRead")
|
||||
fetched = await svc.get_conversation(conversation_id=conv["id"], user_id="admin", is_admin=True)
|
||||
assert fetched["id"] == conv["id"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_creates_real_session_and_run(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="SendTest")
|
||||
result = await svc.send_message(conversation_id=conv["id"], user_id="u1", content="Hello world")
|
||||
|
||||
assert result["message_id"]
|
||||
assert result["conversation_id"] == conv["id"]
|
||||
assert result["seq"] == 1
|
||||
|
||||
async with store.session_factory() as session:
|
||||
conv_row = (await session.execute(
|
||||
select(MafRoundtableConversationRow).where(MafRoundtableConversationRow.id == conv["id"])
|
||||
)).scalars().first()
|
||||
assert conv_row is not None
|
||||
assert conv_row.internal_session_id is not None
|
||||
assert conv_row.safe_status == "running"
|
||||
assert conv_row.active_run_id is not None
|
||||
# Stage 3 persists the manager.intent and manager.dispatch events in
|
||||
# the same conversation-global cursor as the user message.
|
||||
assert conv_row.last_stream_seq == 3
|
||||
|
||||
session_row = (await session.execute(
|
||||
select(MafRoundtableSessionRow).where(MafRoundtableSessionRow.id == conv_row.internal_session_id)
|
||||
)).scalars().first()
|
||||
assert session_row is not None
|
||||
assert session_row.user_id == "u1"
|
||||
|
||||
run_row = (await session.execute(
|
||||
select(MafRoundtableRunRow).where(MafRoundtableRunRow.id == conv_row.active_run_id)
|
||||
)).scalars().first()
|
||||
assert run_row is not None
|
||||
assert run_row.session_id == conv_row.internal_session_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_message_wrong_owner_404(svc):
|
||||
conv = await svc.create_conversation(user_id="u1", title="X")
|
||||
with pytest.raises(ConversationNotFoundError):
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u2", content="hack")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_concurrent_send_only_one_succeeds(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Race")
|
||||
|
||||
async def try_send(content: str):
|
||||
try:
|
||||
r = await svc.send_message(conversation_id=conv["id"], user_id="u1", content=content)
|
||||
return ("ok", r)
|
||||
except ConversationConflictError:
|
||||
return ("conflict", None)
|
||||
|
||||
results = await asyncio.gather(try_send("msg1"), try_send("msg2"))
|
||||
statuses = [r[0] for r in results]
|
||||
assert statuses.count("ok") == 1
|
||||
assert statuses.count("conflict") == 1
|
||||
|
||||
async with store.session_factory() as session:
|
||||
runs = (await session.execute(
|
||||
select(MafRoundtableRunRow).join(
|
||||
MafRoundtableSessionRow,
|
||||
MafRoundtableRunRow.session_id == MafRoundtableSessionRow.id,
|
||||
).where(MafRoundtableSessionRow.user_id == "u1")
|
||||
)).scalars().all()
|
||||
assert len(runs) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_public_dto_no_run_id(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="PubDTO")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="test content")
|
||||
|
||||
messages, last_seq = await svc.list_messages(conversation_id=conv["id"], user_id="u1")
|
||||
assert len(messages) == 1
|
||||
msg = messages[0]
|
||||
assert "run_id" not in msg
|
||||
assert msg["content"] == "test content"
|
||||
assert msg["message_kind"] == "user"
|
||||
assert last_seq == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_last_stream_seq_contiguous(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="SeqTest")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="first")
|
||||
|
||||
async with store.session_factory() as session:
|
||||
conv_row = (await session.execute(
|
||||
select(MafRoundtableConversationRow).where(MafRoundtableConversationRow.id == conv["id"])
|
||||
)).scalars().first()
|
||||
conv_row.safe_status = "idle"
|
||||
conv_row.active_run_id = None
|
||||
await session.commit()
|
||||
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="second")
|
||||
|
||||
async with store.session_factory() as session:
|
||||
conv_row = (await session.execute(
|
||||
select(MafRoundtableConversationRow).where(MafRoundtableConversationRow.id == conv["id"])
|
||||
)).scalars().first()
|
||||
assert conv_row.last_stream_seq == 6
|
||||
|
||||
msgs = (await session.execute(
|
||||
select(MafRoundtableMessageRow).where(
|
||||
MafRoundtableMessageRow.conversation_id == conv["id"]
|
||||
).order_by(MafRoundtableMessageRow.seq)
|
||||
)).scalars().all()
|
||||
seqs = [m.seq for m in msgs]
|
||||
assert seqs == [1, 4]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_schema_rejects_forbidden_fields():
|
||||
with pytest.raises(Exception):
|
||||
CreateConversationRequest.model_validate({"title": "x", "owner_user_id": "hacker"})
|
||||
|
||||
with pytest.raises(Exception):
|
||||
CreateConversationRequest.model_validate({"title": "x", "internal_session_id": "s1"})
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SendMessageRequest.model_validate({"content": "hi", "run_id": "r1"})
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SendMessageRequest.model_validate({"content": "hi", "thread_id": "t1"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_schema_rejects_empty_and_overlong_content():
|
||||
with pytest.raises(Exception):
|
||||
SendMessageRequest.model_validate({"content": ""})
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SendMessageRequest.model_validate({"content": " "})
|
||||
|
||||
with pytest.raises(Exception):
|
||||
SendMessageRequest.model_validate({"content": "x" * 20001})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_task_context_whitelist(svc):
|
||||
conv = await svc.create_conversation(
|
||||
user_id="u1", title="Ctx",
|
||||
task_context={"business_code": "6BF", "secret_key": "LEAKED", "task_name": "T1"},
|
||||
)
|
||||
assert conv["task_context"] == {"business_code": "6BF", "task_name": "T1"}
|
||||
assert "secret_key" not in (conv["task_context"] or {})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_task_context_rejects_unknown_or_nested_values():
|
||||
with pytest.raises(Exception):
|
||||
CreateConversationRequest.model_validate({"task_context": {"secret_key": "nope"}})
|
||||
with pytest.raises(Exception):
|
||||
CreateConversationRequest.model_validate({"task_context": {"task_id": {"secret": "nope"}}})
|
||||
with pytest.raises(Exception):
|
||||
CreateConversationRequest.model_validate({"title": "x", "session_id": "forged"})
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_disabled_feature_rejects_conversation_writes(store):
|
||||
disabled = MafConversationService(store, maf_config=SimpleNamespace(enabled=False))
|
||||
with pytest.raises(ConversationDisabledError):
|
||||
await disabled.create_conversation(user_id="u1", title="disabled")
|
||||
|
||||
|
||||
def test_router_uses_authenticated_user_object_for_owner_scope():
|
||||
user = SimpleNamespace(id="real-user", system_role="admin")
|
||||
request = SimpleNamespace(state=SimpleNamespace(user=user))
|
||||
assert _user_ctx(request) == ("real-user", True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_admin_cross_user_read_respects_diagnostic_config(store):
|
||||
owner_service = MafConversationService(store, maf_config=FakeMafConfig())
|
||||
conv = await owner_service.create_conversation(user_id="u1", title="Private")
|
||||
no_diagnostics = MafConversationService(
|
||||
store,
|
||||
maf_config=SimpleNamespace(enabled=True, admin_diagnostic_access=False),
|
||||
)
|
||||
with pytest.raises(ConversationNotFoundError):
|
||||
await no_diagnostics.get_conversation(
|
||||
conversation_id=conv["id"], user_id="admin", is_admin=True
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_conversation_internal_links_have_real_foreign_keys(store):
|
||||
async with store.session_factory() as session:
|
||||
def _fks(sync_session):
|
||||
inspector = inspect(sync_session.connection())
|
||||
return {
|
||||
(tuple(fk["constrained_columns"]), fk["referred_table"])
|
||||
for fk in inspector.get_foreign_keys("maf_roundtable_conversations")
|
||||
}
|
||||
|
||||
foreign_keys = await session.run_sync(_fks)
|
||||
assert (("active_run_id",), "maf_roundtable_runs") in foreign_keys
|
||||
assert (("internal_session_id",), "maf_roundtable_sessions") in foreign_keys
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_event_only_item_advances_the_same_sse_cursor(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Unified cursor")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="first")
|
||||
|
||||
async with store.session_factory() as session:
|
||||
async with session.begin():
|
||||
row = (
|
||||
await session.execute(
|
||||
select(MafRoundtableConversationRow)
|
||||
.where(MafRoundtableConversationRow.id == conv["id"])
|
||||
.with_for_update()
|
||||
)
|
||||
).scalars().one()
|
||||
item = await store.append_conversation_item(
|
||||
session,
|
||||
conversation=row,
|
||||
event_type="manager.intent",
|
||||
event_data={"status": "understood"},
|
||||
)
|
||||
assert item["seq"] == 4
|
||||
|
||||
messages, last_stream_seq = await svc.list_messages(conversation_id=conv["id"], user_id="u1")
|
||||
assert [message["seq"] for message in messages] == [1]
|
||||
assert last_stream_seq == 4
|
||||
events = await store.conversation_events.list_events(conv["id"])
|
||||
assert [event["seq"] for event in events] == [1, 2, 3, 4]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_standalone_repositories_cannot_create_a_second_cursor(svc, store):
|
||||
conv = await svc.create_conversation(user_id="u1", title="No split cursor")
|
||||
with pytest.raises(RuntimeError, match="append_conversation_item"):
|
||||
await store.messages.append_message(
|
||||
conversation_id=conv["id"], message_kind="manager", role="assistant", content="unsafe"
|
||||
)
|
||||
with pytest.raises(RuntimeError, match="append_conversation_item"):
|
||||
await store.conversation_events.append_event(
|
||||
conversation_id=conv["id"], event_type="manager.intent"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_atomic_send_rolls_back_on_item_write_failure(svc, store, monkeypatch):
|
||||
conv = await svc.create_conversation(user_id="u1", title="Rollback")
|
||||
|
||||
async def _fail_append(*args, **kwargs):
|
||||
raise RuntimeError("simulated item write failure")
|
||||
|
||||
monkeypatch.setattr(store, "append_conversation_item", _fail_append)
|
||||
with pytest.raises(RuntimeError, match="simulated item write failure"):
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="must roll back")
|
||||
|
||||
async with store.session_factory() as session:
|
||||
refreshed = (
|
||||
await session.execute(
|
||||
select(MafRoundtableConversationRow).where(MafRoundtableConversationRow.id == conv["id"])
|
||||
)
|
||||
).scalars().one()
|
||||
assert refreshed.safe_status == "idle"
|
||||
assert refreshed.internal_session_id is None
|
||||
assert refreshed.active_run_id is None
|
||||
assert refreshed.last_stream_seq == 0
|
||||
assert (await session.execute(select(MafRoundtableMessageRow))).scalars().all() == []
|
||||
assert (await session.execute(select(MafRoundtableRunRow))).scalars().all() == []
|
||||
@@ -0,0 +1,286 @@
|
||||
"""Deterministic Stage 3 tests for V2 conversation dispatch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.maf_roundtable.conversation_service import MafConversationService
|
||||
from app.maf_roundtable.dispatch_contracts import (
|
||||
InvalidDispatchPlanError,
|
||||
MafDispatchAssignment,
|
||||
MafDispatchPlan,
|
||||
MafIntentDecision,
|
||||
resolve_intent,
|
||||
validate_dispatch_plan,
|
||||
)
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
MafRoundtableConversationRow,
|
||||
MafRoundtableParticipantRow,
|
||||
MafRoundtableRunRow,
|
||||
MafRoundtableSessionRow,
|
||||
)
|
||||
|
||||
|
||||
class FakeMafConfig:
|
||||
enabled = True
|
||||
default_mode = "structured"
|
||||
default_model_name = "test-model"
|
||||
default_reasoning_effort = None
|
||||
default_manager_agent_id = "manager-from-config"
|
||||
default_seat_agent_ids = ["seat-a", "seat-b"]
|
||||
business_routes = {}
|
||||
max_seats_per_run = 4
|
||||
default_max_rounds = 8
|
||||
default_write_policy = "confirm_high_risk"
|
||||
admin_diagnostic_access = True
|
||||
|
||||
|
||||
class RecordingDispatcher:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[tuple[dict, dict, str]] = []
|
||||
|
||||
def check_sdk_available(self) -> None:
|
||||
return
|
||||
|
||||
async def dispatch(self, session: dict, run: dict, *, user_id: str) -> None:
|
||||
self.calls.append((session, run, user_id))
|
||||
|
||||
|
||||
class CompletingDispatcher(RecordingDispatcher):
|
||||
def __init__(self, store) -> None:
|
||||
super().__init__()
|
||||
self._store = store
|
||||
self.ready = asyncio.Event()
|
||||
|
||||
async def dispatch(self, session: dict, run: dict, *, user_id: str) -> None:
|
||||
await super().dispatch(session, run, user_id=user_id)
|
||||
await self._store.runs.update_run(run["id"], {"status": "completed"})
|
||||
await self._store.sessions.update_session(
|
||||
session["id"], user_id=user_id, fields={"status": "completed"}
|
||||
)
|
||||
self.ready.set()
|
||||
|
||||
async def wait_for_run(self, run_id: str) -> None:
|
||||
await self.ready.wait()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def store(tmp_path):
|
||||
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'phase3.db'}")
|
||||
async with engine.begin() as conn:
|
||||
import deerflow.persistence.maf_roundtable.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
yield make_maf_roundtable_store(sf)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_server_route_creates_participants_and_dispatches(store):
|
||||
dispatcher = RecordingDispatcher()
|
||||
svc = MafConversationService(
|
||||
store,
|
||||
maf_config=FakeMafConfig(),
|
||||
dispatcher=dispatcher,
|
||||
)
|
||||
conv = await svc.create_conversation(
|
||||
user_id="u1",
|
||||
title="Trusted route",
|
||||
task_context={"business_code": "6BF"},
|
||||
)
|
||||
|
||||
# The unknown business code is rejected rather than falling back across
|
||||
# business boundaries.
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
await svc.send_message(
|
||||
conversation_id=conv["id"], user_id="u1", content="请多专家并行分析这个任务"
|
||||
)
|
||||
assert "route" in str(exc_info.value)
|
||||
assert dispatcher.calls == []
|
||||
|
||||
# With no business code the configured default route is selected and the
|
||||
# runtime sees only server-owned participants.
|
||||
conv2 = await svc.create_conversation(user_id="u1", title="Default route")
|
||||
await svc.send_message(
|
||||
conversation_id=conv2["id"], user_id="u1", content="请多专家并行分析这个任务"
|
||||
)
|
||||
assert len(dispatcher.calls) == 1
|
||||
session, run, owner = dispatcher.calls[0]
|
||||
assert owner == "u1"
|
||||
assert run["session_id"] == session["id"]
|
||||
|
||||
async with store.session_factory() as db:
|
||||
session_row = (
|
||||
await db.execute(
|
||||
select(MafRoundtableSessionRow).where(
|
||||
MafRoundtableSessionRow.id == session["id"]
|
||||
)
|
||||
)
|
||||
).scalars().one()
|
||||
participants = (
|
||||
await db.execute(
|
||||
select(MafRoundtableParticipantRow)
|
||||
.where(MafRoundtableParticipantRow.session_id == session["id"])
|
||||
.order_by(MafRoundtableParticipantRow.sort_order)
|
||||
)
|
||||
).scalars().all()
|
||||
assert session_row.task_payload_json["maf_v2"]["dispatch_plan"]["manager_agent_id"] == "manager-from-config"
|
||||
assert [p.agent_id for p in participants] == ["manager-from-config", "seat-a", "seat-b"]
|
||||
assert all(p.thread_id is None for p in participants)
|
||||
|
||||
events = (
|
||||
await db.execute(
|
||||
select(MafRoundtableConversationRow).where(
|
||||
MafRoundtableConversationRow.id == conv2["id"]
|
||||
)
|
||||
)
|
||||
).scalars().one()
|
||||
assert events.active_run_id == run["id"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_clarification_does_not_create_or_dispatch_run(store):
|
||||
dispatcher = RecordingDispatcher()
|
||||
svc = MafConversationService(store, maf_config=FakeMafConfig(), dispatcher=dispatcher)
|
||||
conv = await svc.create_conversation(user_id="u1", title="Need detail")
|
||||
|
||||
result = await svc.send_message(conversation_id=conv["id"], user_id="u1", content="不清楚")
|
||||
assert result["message_id"]
|
||||
assert dispatcher.calls == []
|
||||
|
||||
async with store.session_factory() as db:
|
||||
conv_row = (
|
||||
await db.execute(
|
||||
select(MafRoundtableConversationRow).where(
|
||||
MafRoundtableConversationRow.id == conv["id"]
|
||||
)
|
||||
)
|
||||
).scalars().one()
|
||||
assert conv_row.safe_status == "waiting_input"
|
||||
assert conv_row.active_run_id is None
|
||||
runs = (
|
||||
await db.execute(select(MafRoundtableRunRow))
|
||||
).scalars().all()
|
||||
assert runs == []
|
||||
|
||||
# A clarification reply is allowed to claim the idle waiting-input
|
||||
# conversation and launch exactly one run.
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="请分析这个任务")
|
||||
assert len(dispatcher.calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dispatcher_completion_bridges_back_to_conversation(store):
|
||||
dispatcher = CompletingDispatcher(store)
|
||||
svc = MafConversationService(store, maf_config=FakeMafConfig(), dispatcher=dispatcher)
|
||||
conv = await svc.create_conversation(user_id="u1", title="Completion")
|
||||
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="complete this task")
|
||||
watcher_tasks = list(svc._watchers.values()) # noqa: SLF001 - lifecycle assertion
|
||||
if watcher_tasks:
|
||||
await asyncio.gather(*watcher_tasks)
|
||||
|
||||
public = await svc.get_conversation(conversation_id=conv["id"], user_id="u1")
|
||||
assert public["safe_status"] == "completed"
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=0)
|
||||
assert [event["type"] for event in events][-1] == "run.completed"
|
||||
assert all(event["run_id"] is None for event in events)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_workflow_projection_whitelists_user_visible_event_data(store):
|
||||
svc = MafConversationService(store, maf_config=FakeMafConfig())
|
||||
conv = await svc.create_conversation(user_id="u1", title="Projection")
|
||||
await svc.send_message(conversation_id=conv["id"], user_id="u1", content="complete this task")
|
||||
internal = await store.conversations.get_conversation(conv["id"], user_id="u1")
|
||||
run = await store.runs.list_runs(internal["internal_session_id"])
|
||||
|
||||
await svc.bridge_workflow_event(
|
||||
run_id=run[0]["id"],
|
||||
event={
|
||||
"event_type": "tool.requested",
|
||||
"agent_id": "seat-a",
|
||||
"turn_index": 1,
|
||||
"payload": {
|
||||
"tool_name": "safe_tool",
|
||||
"args": {"secret": "must-not-cross"},
|
||||
"tool_call_id": "internal-call-id",
|
||||
},
|
||||
},
|
||||
)
|
||||
await svc.bridge_workflow_event(
|
||||
run_id=run[0]["id"],
|
||||
event={
|
||||
"event_type": "agent.message.completed",
|
||||
"agent_id": "seat-a",
|
||||
"turn_index": 1,
|
||||
"payload": {
|
||||
"content": "answer",
|
||||
"metadata": {"thread_id": "thread-secret", "deerflow_run_id": "df-secret"},
|
||||
},
|
||||
},
|
||||
)
|
||||
events = await store.conversation_events.list_events(conv["id"], after_seq=3)
|
||||
tool = next(event for event in events if event["type"] == "tool.requested")
|
||||
assert tool["data"] == {"tool_name": "safe_tool", "agent_id": "seat-a", "turn_index": 1}
|
||||
complete = next(event for event in events if event["type"] == "agent.message.complete")
|
||||
assert "metadata" not in complete["data"]
|
||||
assert "thread-secret" not in str(complete)
|
||||
assert any(event["type"] == "seat.delivery.submitted" for event in events)
|
||||
|
||||
|
||||
def test_intent_resolution_is_deterministic_and_does_not_select_agents():
|
||||
assert resolve_intent("请多专家并行分析") .intent_type == "parallel_consultation"
|
||||
assert resolve_intent("请发布这条内容").intent_type == "tool_write"
|
||||
assert resolve_intent("不清楚").intent_type == "clarification"
|
||||
assert resolve_intent("给我一个普通答案").intent_type == "direct_answer"
|
||||
|
||||
|
||||
def test_weak_model_cannot_expand_or_duplicate_dispatch_plan():
|
||||
with pytest.raises(ValueError):
|
||||
MafDispatchPlan(
|
||||
manager_agent_id="manager",
|
||||
seat_agent_ids=["seat-a", "seat-a"],
|
||||
assignments=[
|
||||
MafDispatchAssignment(agent_id="seat-a", sort_order=1),
|
||||
MafDispatchAssignment(agent_id="seat-a", sort_order=2),
|
||||
],
|
||||
intent_type="parallel_consultation",
|
||||
max_rounds=8,
|
||||
)
|
||||
|
||||
plan = MafDispatchPlan(
|
||||
manager_agent_id="manager",
|
||||
seat_agent_ids=["seat-a"],
|
||||
assignments=[MafDispatchAssignment(agent_id="seat-a", sort_order=1)],
|
||||
intent_type="single_seat",
|
||||
max_rounds=8,
|
||||
)
|
||||
with pytest.raises(InvalidDispatchPlanError):
|
||||
validate_dispatch_plan(plan, trusted_agent_ids={"manager", "seat-b"}, max_seats=4)
|
||||
|
||||
with pytest.raises(ValueError, match="at least one configured seat"):
|
||||
MafDispatchPlan(
|
||||
manager_agent_id="manager",
|
||||
seat_agent_ids=[],
|
||||
assignments=[],
|
||||
intent_type="parallel_consultation",
|
||||
max_rounds=8,
|
||||
)
|
||||
|
||||
|
||||
def test_clarification_plan_is_not_constructible():
|
||||
with pytest.raises(ValueError):
|
||||
MafIntentDecision(
|
||||
intent_type="clarification",
|
||||
confidence=0.9,
|
||||
reason_code="needs_detail",
|
||||
)
|
||||
@@ -21,7 +21,10 @@ from deerflow.persistence.maf_roundtable.model import (
|
||||
MafRoundtableActionRow,
|
||||
MafRoundtableActionSecretRow,
|
||||
MafRoundtableApprovalRow,
|
||||
MafRoundtableConversationEventRow,
|
||||
MafRoundtableConversationRow,
|
||||
MafRoundtableEventRow,
|
||||
MafRoundtableMessageRow,
|
||||
MafRoundtableParticipantRow,
|
||||
MafRoundtableRunRow,
|
||||
MafRoundtableSeatRunRow,
|
||||
@@ -167,6 +170,9 @@ async def test_maf_tables_isolated_from_legacy_roundtable(store):
|
||||
"maf_roundtable_approvals",
|
||||
"maf_roundtable_seat_runs",
|
||||
"maf_roundtable_action_secrets",
|
||||
"maf_roundtable_conversations",
|
||||
"maf_roundtable_messages",
|
||||
"maf_roundtable_conversation_events",
|
||||
}
|
||||
|
||||
# Every FK defined on an MAF model must reference another maf_roundtable_* table.
|
||||
@@ -180,6 +186,9 @@ async def test_maf_tables_isolated_from_legacy_roundtable(store):
|
||||
MafRoundtableActionSecretRow,
|
||||
MafRoundtableApprovalRow,
|
||||
MafRoundtableSeatRunRow,
|
||||
MafRoundtableConversationRow,
|
||||
MafRoundtableMessageRow,
|
||||
MafRoundtableConversationEventRow,
|
||||
]
|
||||
for model in maf_models:
|
||||
for fk in model.__table__.foreign_keys:
|
||||
|
||||
@@ -903,9 +903,10 @@ async def test_seat_matching_scopes_by_turn_index(svc, store):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_seat_matching_count_mismatch_fails(svc, store):
|
||||
"""P1b: a pending tool call that does not match EXACTLY one waiting action
|
||||
(by tool_call_id) fails the turn —the executor never guesses."""
|
||||
async def test_seat_matching_zero_hits_warns_and_skips(svc, store):
|
||||
"""A pending tool call that matches 0 waiting actions (e.g. unclassified
|
||||
tool, hallucinated tool name, or WriteGuard let it through as read) should
|
||||
log a warning and skip — NOT crash the entire workflow."""
|
||||
from app.maf_roundtable.workflow_executors import SeatExecutor
|
||||
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "t"})
|
||||
@@ -923,7 +924,39 @@ async def test_seat_matching_count_mismatch_fails(svc, store):
|
||||
executor = SeatExecutor(_FakeSeatAdapter(store, response), state, "seat-1",
|
||||
action_coordinator=ActionCoordinator(svc, user_id="u1"))
|
||||
|
||||
with pytest.raises(RuntimeError, match="expected exactly 1"):
|
||||
# Should NOT raise — the unmatched tool call is logged and skipped.
|
||||
await executor.handle("msg", _FakeSeatCtx())
|
||||
# The unmatched call was not added to pending_approvals.
|
||||
assert len(state.pending_approvals) == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_seat_matching_multiple_hits_raises(svc, store):
|
||||
""">1 match for a single tool call is a genuine data-integrity error —
|
||||
the executor refuses to guess and raises RuntimeError."""
|
||||
from app.maf_roundtable.workflow_executors import SeatExecutor
|
||||
|
||||
session = await svc.create_session(user_id="u1", body={"task_text": "t"})
|
||||
run = await svc.create_run(session_id=session["id"], user_id="u1")
|
||||
|
||||
# Two waiting actions with the same logical_action_id for turn 1.
|
||||
await store.actions.create_action({
|
||||
"session_id": session["id"], "run_id": run["id"], "agent_id": "seat-1",
|
||||
"tool_name": "update_task", "turn_index": 1, "logical_action_id": "call-1",
|
||||
"idempotency_key": "dup1", "risk_level": "R3", "status": "waiting_approval",
|
||||
})
|
||||
await store.actions.create_action({
|
||||
"session_id": session["id"], "run_id": run["id"], "agent_id": "seat-1",
|
||||
"tool_name": "update_task", "turn_index": 1, "logical_action_id": "call-1",
|
||||
"idempotency_key": "dup2", "risk_level": "R3", "status": "waiting_approval",
|
||||
})
|
||||
|
||||
state = _seat_state(session["id"], run["id"])
|
||||
response = _FakeSeatResponse(tool_calls=[{"name": "update_task", "id": "call-1", "args": {}}])
|
||||
executor = SeatExecutor(_FakeSeatAdapter(store, response), state, "seat-1",
|
||||
action_coordinator=ActionCoordinator(svc, user_id="u1"))
|
||||
|
||||
with pytest.raises(RuntimeError, match="refusing to guess"):
|
||||
await executor.handle("msg", _FakeSeatCtx())
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user