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

@@ -437,12 +437,142 @@ class MafRoundtableActionSecretRow(Base):
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now)
# ── 10. conversations (V2: user-visible conversation aggregation) ─────────
class MafRoundtableConversationRow(Base):
"""One user-visible MAF roundtable conversation.
This is the **only** entity the frontend knows about. A conversation can
span multiple user messages, each of which may trigger an internal
``maf_roundtable_runs`` execution. The user never sees session/run/thread
ids — only ``conversation_id``.
``active_run_id`` points to the currently executing internal run (nullable
when idle). ``safe_status`` is a coarse user-facing status derived from
the active run's state (idle / running / waiting_input / waiting_approval
/ completed / failed / cancelled).
"""
__tablename__ = "maf_roundtable_conversations"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
owner_user_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(255), default="")
task_context_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
# idle | running | waiting_input | waiting_approval | completed | failed | cancelled
safe_status: Mapped[str] = mapped_column(String(32), default="idle", index=True)
active_run_id: Mapped[str | None] = mapped_column(
String(64),
ForeignKey("maf_roundtable_runs.id", ondelete="SET NULL"),
nullable=True,
)
# Internal session this conversation is linked to (lazy-created on first message).
internal_session_id: Mapped[str | None] = mapped_column(
String(64),
ForeignKey("maf_roundtable_sessions.id", ondelete="SET NULL"),
nullable=True,
)
last_stream_seq: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now)
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(), nullable=False, default=_now, onupdate=_now
)
# ── 11. conversation messages ─────────────────────────────────────────────
class MafRoundtableMessageRow(Base):
"""Stable message history for a conversation.
Every user-visible bubble (user message, manager message, seat message,
clarification card, approval card, result card) has exactly one row here.
``seq`` is a conversation-global monotonic counter (unique per conversation)
used for SSE cursor and ordering. ``message_kind`` classifies the bubble
type for the frontend reducer.
``run_id`` links the message to the internal run that produced it (nullable
for user messages that haven't started a run yet). ``agent_id`` identifies
the speaking agent (null for user messages).
"""
__tablename__ = "maf_roundtable_messages"
__table_args__ = (
UniqueConstraint("conversation_id", "seq", name="uq_maf_messages_conv_seq"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
conversation_id: Mapped[str] = mapped_column(
String(64), ForeignKey("maf_roundtable_conversations.id", ondelete="CASCADE"),
nullable=False, index=True,
)
seq: Mapped[int] = mapped_column(Integer, nullable=False)
# user | manager | seat | system | clarification | approval | result | tool_progress
message_kind: Mapped[str] = mapped_column(String(32), nullable=False, default="user")
role: Mapped[str] = mapped_column(String(16), nullable=False, default="user")
agent_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
# Stable message identity for SSE delta merging (e.g. LangGraph message id).
message_ref: Mapped[str | None] = mapped_column(String(191), nullable=True, index=True)
content: Mapped[str] = mapped_column(PortableLongText, default="")
metadata_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now, index=True)
# ── 12. conversation events (SSE stream, conversation-global seq) ─────────
class MafRoundtableConversationEventRow(Base):
"""Conversation-dimension event stream for SSE.
Unlike ``maf_roundtable_events`` (per-run seq), this table uses a
**conversation-global** monotonic ``seq`` so the frontend only needs one
cursor (``after_seq``) regardless of how many internal runs happen.
The SSE hub replays from this table on connect/reconnect, then subscribes
to real-time notifications.
``event_type`` follows the §4.3 contract (conversation.snapshot,
user.message.created, manager.intent, clarification.required, etc.).
``data_json`` is the already-sanitized event payload safe for the browser.
"""
__tablename__ = "maf_roundtable_conversation_events"
__table_args__ = (
UniqueConstraint("conversation_id", "seq", name="uq_maf_conv_events_conv_seq"),
# P0-4: dedup key for projection idempotency. The source audit event's
# stable id (e.g. LangGraph event id / tool_call id) prevents duplicate
# conversation events when the bridge retries or the adapter re-projects
# after a restart. NULL source_event_ids are not deduped (SQL treats
# NULLs as distinct in UNIQUE constraints).
UniqueConstraint("conversation_id", "source_event_id", name="uq_maf_conv_events_conv_source"),
)
id: Mapped[str] = mapped_column(String(64), primary_key=True)
conversation_id: Mapped[str] = mapped_column(
String(64), ForeignKey("maf_roundtable_conversations.id", ondelete="CASCADE"),
nullable=False, index=True,
)
seq: Mapped[int] = mapped_column(Integer, nullable=False)
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Stable upstream id from the source audit event (maf_roundtable_events.id
# or the LangGraph source_event_id). Used for projection dedup via the
# (conversation_id, source_event_id) unique constraint.
source_event_id: Mapped[str | None] = mapped_column(String(191), nullable=True, index=True)
event_type: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
data_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=_now, index=True)
__all__ = [
"MafRoundtableActionRow",
"MafRoundtableActionLeaseRow",
"MafRoundtableActionSecretRow",
"MafRoundtableApprovalRow",
"MafRoundtableConversationEventRow",
"MafRoundtableConversationRow",
"MafRoundtableEventRow",
"MafRoundtableMessageRow",
"MafRoundtableParticipantRow",
"MafRoundtableRunRow",
"MafRoundtableSeatRunRow",