fix(maf): harden phase-3 dispatch recovery and participant writes
Make ensure_participants concurrent-safe, restore clarification checkpoints without re-planning, and only create threads for validated seats. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ddf83f7728
commit
1a080311bd
4 files changed
+599
-35
No files matched your search
+107
-32
@@ -9,15 +9,19 @@ creation time.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.maf_roundtable.model import MafRoundtableParticipantRow
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
@@ -102,45 +106,116 @@ class MafParticipantRepository:
|
||||
async def ensure_participants(
|
||||
self, session_id: str, participants: list[dict[str, Any]]
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Insert missing participants without replacing existing threads.
|
||||
"""Insert missing participants using a database-level idempotent insert.
|
||||
|
||||
MAF Stage 3 initially persists only the manager participant. After
|
||||
the manager validates its dispatch plan, selected seats are added here
|
||||
so unselected seats never receive a thread or execution slot.
|
||||
Concurrent-safe: two coroutines calling this simultaneously with the
|
||||
same seat set will never raise a unique-constraint error and will
|
||||
never produce duplicate rows.
|
||||
|
||||
- SQLite / PostgreSQL: ``INSERT … ON CONFLICT (session_id, agent_id) DO NOTHING``
|
||||
- MySQL: ``INSERT IGNORE …``
|
||||
|
||||
After the insert attempt the method **re-queries** the database and
|
||||
returns the final persisted records (sorted by ``sort_order, agent_id``).
|
||||
Existing ``thread_id``, ``sandbox_id``, ``status`` and
|
||||
``capability_snapshot_json`` are never overwritten.
|
||||
"""
|
||||
# Filter: skip empty agent_ids and de-duplicate input.
|
||||
candidates: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for participant in participants:
|
||||
agent_id = str(participant.get("agent_id") or "").strip()
|
||||
if not agent_id or agent_id in seen:
|
||||
continue
|
||||
seen.add(agent_id)
|
||||
candidates.append(participant)
|
||||
|
||||
if candidates:
|
||||
async with self._sf() as session:
|
||||
await self._idempotent_insert(session, session_id, candidates)
|
||||
|
||||
# Re-query: always return the authoritative DB state.
|
||||
return await self._list_sorted(session_id)
|
||||
|
||||
async def _idempotent_insert(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
session_id: str,
|
||||
candidates: list[dict[str, Any]],
|
||||
) -> None:
|
||||
"""Execute a conflict-safe INSERT for the given candidate rows."""
|
||||
from sqlalchemy import insert as sa_insert
|
||||
|
||||
now = _now()
|
||||
rows = [
|
||||
{
|
||||
"id": uuid4().hex,
|
||||
"session_id": session_id,
|
||||
"agent_id": str(p.get("agent_id") or "").strip(),
|
||||
"display_name": str(p.get("display_name") or ""),
|
||||
"role": str(p.get("role") or "seat"),
|
||||
"sort_order": int(p.get("sort_order", 0)),
|
||||
"thread_id": None,
|
||||
"sandbox_id": None,
|
||||
"status": "idle",
|
||||
"last_turn_index": None,
|
||||
"capability_snapshot_json": p.get("capability_snapshot"),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
for p in candidates
|
||||
]
|
||||
|
||||
dialect_name = session.bind.dialect.name if session.bind else "sqlite"
|
||||
|
||||
if dialect_name in ("sqlite", "postgresql"):
|
||||
# Use ON CONFLICT DO NOTHING for SQLite and PostgreSQL.
|
||||
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
||||
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
|
||||
|
||||
dialect_insert = sqlite_insert if dialect_name == "sqlite" else pg_insert
|
||||
stmt = dialect_insert(MafRoundtableParticipantRow).values(rows)
|
||||
stmt = stmt.on_conflict_do_nothing(
|
||||
index_elements=["session_id", "agent_id"],
|
||||
)
|
||||
await session.execute(stmt)
|
||||
await session.commit()
|
||||
elif dialect_name == "mysql":
|
||||
# MySQL: INSERT IGNORE skips rows that violate a unique constraint.
|
||||
from sqlalchemy.dialects.mysql import insert as mysql_insert
|
||||
|
||||
stmt = mysql_insert(MafRoundtableParticipantRow).values(rows)
|
||||
stmt = stmt.prefix_with("IGNORE")
|
||||
await session.execute(stmt)
|
||||
await session.commit()
|
||||
else:
|
||||
# Fallback: try/except IntegrityError per row (rare dialects).
|
||||
for row_data in rows:
|
||||
try:
|
||||
session.add(MafRoundtableParticipantRow(**row_data))
|
||||
await session.commit()
|
||||
except IntegrityError:
|
||||
await session.rollback()
|
||||
logger.debug(
|
||||
"ensure_participants: skipped duplicate (%s, %s)",
|
||||
session_id,
|
||||
row_data["agent_id"],
|
||||
)
|
||||
|
||||
async def _list_sorted(self, session_id: str) -> list[dict[str, Any]]:
|
||||
"""Return all participants for a session sorted by sort_order, agent_id."""
|
||||
async with self._sf() as session:
|
||||
existing_rows = (
|
||||
rows = (
|
||||
await session.execute(
|
||||
select(MafRoundtableParticipantRow).where(
|
||||
MafRoundtableParticipantRow.session_id == session_id
|
||||
select(MafRoundtableParticipantRow)
|
||||
.where(MafRoundtableParticipantRow.session_id == session_id)
|
||||
.order_by(
|
||||
MafRoundtableParticipantRow.sort_order.asc(),
|
||||
MafRoundtableParticipantRow.agent_id.asc(),
|
||||
)
|
||||
)
|
||||
).scalars().all()
|
||||
existing = {row.agent_id: row for row in existing_rows}
|
||||
now = _now()
|
||||
for participant in participants:
|
||||
agent_id = str(participant.get("agent_id") or "").strip()
|
||||
if not agent_id or agent_id in existing:
|
||||
continue
|
||||
row = MafRoundtableParticipantRow(
|
||||
id=uuid4().hex,
|
||||
session_id=session_id,
|
||||
agent_id=agent_id,
|
||||
display_name=str(participant.get("display_name") or ""),
|
||||
role=str(participant.get("role") or "seat"),
|
||||
sort_order=int(participant.get("sort_order", 0)),
|
||||
thread_id=None,
|
||||
sandbox_id=None,
|
||||
status="idle",
|
||||
last_turn_index=None,
|
||||
capability_snapshot_json=participant.get("capability_snapshot"),
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
session.add(row)
|
||||
existing[agent_id] = row
|
||||
await session.commit()
|
||||
return [_to_dict(existing[agent_id]) for agent_id in existing]
|
||||
return [_to_dict(r) for r in rows]
|
||||
|
||||
async def update_participant_status(
|
||||
self, session_id: str, agent_id: str, *, status: str, last_turn_index: int | None = None
|
||||
|
||||
Reference in new issue
Block a user