feat(maf): add secure roundtable workflow runtime

This commit is contained in:
董宏凯 committed 2026-08-01 17:51:53 +08:00
1 parent e690475e6e
commit bd4131b4e2
52 files changed
+10315 -96

No files matched your search

@@ -46,6 +46,7 @@ def _to_dict(row: MafRoundtableEventRow) -> dict[str, Any]:
"event_type": row.event_type,
"agent_id": row.agent_id,
"turn_index": row.turn_index,
"source_event_id": row.source_event_id,
"payload": row.payload_json,
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
}
@@ -69,6 +70,7 @@ class MafEventRepository:
agent_id: str | None = None,
turn_index: int | None = None,
payload: dict[str, Any] | None = None,
source_event_id: str | None = None,
) -> dict[str, Any]:
"""Append one event with a monotonic per-run seq.
@@ -77,7 +79,19 @@ class MafEventRepository:
``last_event_seq`` cache is bumped forward (max wins) so pollers can
cheaply check ``runs.last_event_seq`` without scanning the events
table.
Durable dedup: when ``source_event_id`` is given, the
``(run_id, source_event_id)`` unique constraint guarantees at-most-once
persistence. A duplicate insert returns the already-stored row instead
of raising — this survives process restarts (unlike an in-memory seen
set), so a replayed runtime stream never double-writes an event.
"""
# Fast-path dedup: if this source_event_id already landed, return it.
if source_event_id:
existing = await self._get_by_source(run_id, source_event_id)
if existing is not None:
return existing
last_exc: Exception | None = None
for _ in range(_SEQ_INSERT_RETRIES):
async with self._sf() as session:
@@ -97,6 +111,7 @@ class MafEventRepository:
event_type=event_type,
agent_id=agent_id,
turn_index=turn_index,
source_event_id=source_event_id,
payload_json=payload,
created_at=_now(),
)
@@ -104,8 +119,14 @@ class MafEventRepository:
try:
await session.commit()
except IntegrityError as exc: # noqa: PERF203 - retry path
last_exc = exc
await session.rollback()
# A source_event_id collision means a concurrent writer
# already persisted this exact event — return its row.
if source_event_id:
dup = await self._get_by_source(run_id, source_event_id)
if dup is not None:
return dup
last_exc = exc
continue
if self._run_repo is not None:
# best-effort cache bump; never let a cache failure abort
@@ -121,6 +142,15 @@ class MafEventRepository:
orig=last_exc,
)
async def _get_by_source(self, run_id: str, source_event_id: str) -> dict[str, Any] | None:
stmt = select(MafRoundtableEventRow).where(
MafRoundtableEventRow.run_id == run_id,
MafRoundtableEventRow.source_event_id == source_event_id,
)
async with self._sf() as session:
row = (await session.execute(stmt)).scalars().first()
return _to_dict(row) if row is not None else None
async def append_events(
self, *, session_id: str, run_id: str, events: list[dict[str, Any]]
) -> list[dict[str, Any]]:
@@ -138,6 +168,7 @@ class MafEventRepository:
agent_id=ev.get("agent_id"),
turn_index=ev.get("turn_index"),
payload=ev.get("payload"),
source_event_id=ev.get("source_event_id"),
)
)
return out