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:
董宏凯andCursor committed 2026-08-03 12:09:47 +08:00
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)