feat(maf-roundtable): add Phase-1 multi-agent roundtable infrastructure

Ship an independent, feature-flag-gated MAF roundtable skeleton with six
isolated tables, atomic single-transaction run launch/cancel, owner-scoped
read/write split, shared config enums, and HTTP/concurrency regression tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-01 01:07:05 +08:00
1 parent a0a75a9964
commit e690475e6e
27 files changed
+5369 -101

No files matched your search

@@ -0,0 +1,137 @@
"""Action repository for the MAF roundtable experiment (external writes).
Records every external write / tool action with an idempotency key so the
Action Coordinator (Phase 5) can guarantee at-most-once execution across
retries / restarts. Phase 1 ships the storage + CRUD; the Coordinator itself
is Phase 5. ``arguments_redacted_json`` / ``result_redacted_json`` must
already have credentials / tokens / cookies stripped by the writer — the
store does not re-redact.
"""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from uuid import uuid4
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.maf_roundtable.model import MafRoundtableActionRow
def _now() -> datetime:
return datetime.now(UTC)
def _to_dict(row: MafRoundtableActionRow) -> dict[str, Any]:
return {
"id": row.id,
"session_id": row.session_id,
"run_id": row.run_id,
"agent_id": row.agent_id,
"tool_name": row.tool_name,
"logical_action_id": row.logical_action_id,
"idempotency_key": row.idempotency_key,
"target_key": row.target_key,
"risk_level": row.risk_level,
"status": row.status,
"arguments": row.arguments_redacted_json,
"result": row.result_redacted_json,
"external_object_id": row.external_object_id,
"approval_id": row.approval_id,
"attempt_count": row.attempt_count,
"started_at": row.started_at.isoformat() if isinstance(row.started_at, datetime) else row.started_at,
"finished_at": row.finished_at.isoformat() if isinstance(row.finished_at, datetime) else row.finished_at,
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
}
class MafActionRepository:
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
async def create_action(self, data: dict[str, Any]) -> dict[str, Any]:
"""Create an action. Raises ``IntegrityError`` if the ``idempotency_key``
already exists (caller should treat that as "already submitted" and
fetch the existing row via ``get_by_idempotency_key``).
"""
now = _now()
row = MafRoundtableActionRow(
id=data.get("id") or uuid4().hex,
session_id=str(data["session_id"]),
run_id=data.get("run_id"),
agent_id=data.get("agent_id"),
tool_name=str(data["tool_name"]),
logical_action_id=data.get("logical_action_id"),
idempotency_key=str(data["idempotency_key"]),
target_key=data.get("target_key"),
risk_level=str(data.get("risk_level") or "R0"),
status=str(data.get("status") or "pending"),
arguments_redacted_json=data.get("arguments"),
result_redacted_json=data.get("result"),
external_object_id=data.get("external_object_id"),
approval_id=data.get("approval_id"),
attempt_count=int(data.get("attempt_count", 0)),
started_at=data.get("started_at"),
finished_at=data.get("finished_at"),
created_at=now,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
await session.commit()
return await self.get_action(row.id) # type: ignore[arg-type]
async def get_action(self, action_id: str) -> dict[str, Any] | None:
async with self._sf() as session:
stmt = select(MafRoundtableActionRow).where(MafRoundtableActionRow.id == action_id).limit(1)
row = (await session.execute(stmt)).scalars().first()
return _to_dict(row) if row is not None else None
async def get_by_idempotency_key(self, key: str) -> dict[str, Any] | None:
async with self._sf() as session:
stmt = select(MafRoundtableActionRow).where(MafRoundtableActionRow.idempotency_key == key).limit(1)
row = (await session.execute(stmt)).scalars().first()
return _to_dict(row) if row is not None else None
async def list_actions(
self, *, session_id: str | None = None, run_id: str | None = None, status: str | None = None
) -> list[dict[str, Any]]:
stmt = select(MafRoundtableActionRow)
if session_id is not None:
stmt = stmt.where(MafRoundtableActionRow.session_id == session_id)
if run_id is not None:
stmt = stmt.where(MafRoundtableActionRow.run_id == run_id)
if status is not None:
stmt = stmt.where(MafRoundtableActionRow.status == status)
stmt = stmt.order_by(MafRoundtableActionRow.created_at.asc())
async with self._sf() as session:
rows = (await session.execute(stmt)).scalars().all()
return [_to_dict(r) for r in rows]
async def update_action(self, action_id: str, fields: dict[str, Any]) -> dict[str, Any] | None:
async with self._sf() as session:
stmt = select(MafRoundtableActionRow).where(MafRoundtableActionRow.id == action_id).limit(1)
row = (await session.execute(stmt)).scalars().first()
if row is None:
return None
col_map = {
"status": "status",
"risk_level": "risk_level",
"arguments": "arguments_redacted_json",
"result": "result_redacted_json",
"external_object_id": "external_object_id",
"approval_id": "approval_id",
"attempt_count": "attempt_count",
"started_at": "started_at",
"finished_at": "finished_at",
}
for key, value in fields.items():
col = col_map.get(key)
if col is not None:
setattr(row, col, value)
row.updated_at = _now()
await session.commit()
return _to_dict(row)