代码提交
This commit is contained in:
1 parent
d93cc62806
commit
ddf83f7728
25 files changed
+4086
-424
No files matched your search
+43
@@ -99,6 +99,49 @@ class MafParticipantRepository:
|
||||
await session.commit()
|
||||
return [_to_dict(r) for r in new_rows]
|
||||
|
||||
async def ensure_participants(
|
||||
self, session_id: str, participants: list[dict[str, Any]]
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Insert missing participants without replacing existing threads.
|
||||
|
||||
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.
|
||||
"""
|
||||
async with self._sf() as session:
|
||||
existing_rows = (
|
||||
await session.execute(
|
||||
select(MafRoundtableParticipantRow).where(
|
||||
MafRoundtableParticipantRow.session_id == session_id
|
||||
)
|
||||
)
|
||||
).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]
|
||||
|
||||
async def update_participant_status(
|
||||
self, session_id: str, agent_id: str, *, status: str, last_turn_index: int | None = None
|
||||
) -> dict[str, Any] | None:
|
||||
|
||||
+71
@@ -867,6 +867,77 @@ class MafRoundtableStore:
|
||||
await db.flush()
|
||||
return {"seq": next_seq, "message_id": message_id, "event_id": event_id}
|
||||
|
||||
async def append_conversation_events_idempotently(
|
||||
self,
|
||||
db: AsyncSession,
|
||||
*,
|
||||
conversation: MafRoundtableConversationRow,
|
||||
items: list[dict[str, Any]],
|
||||
) -> int:
|
||||
"""Batch-atomic idempotent write of multiple conversation events.
|
||||
|
||||
MUST be called inside an existing transaction (``db.begin()``). The
|
||||
caller is responsible for locking the conversation row.
|
||||
|
||||
Each item in ``items`` must have:
|
||||
- ``event_type``: str
|
||||
- ``event_data``: dict
|
||||
- ``source_event_id``: str | None
|
||||
|
||||
Items with a ``source_event_id`` that already exists for this
|
||||
conversation are silently skipped (dedup). Items without a
|
||||
``source_event_id`` are always written.
|
||||
|
||||
All new items share one transaction: if any insert fails, the entire
|
||||
batch rolls back. Returns the count of **newly inserted** events.
|
||||
"""
|
||||
if not items:
|
||||
return 0
|
||||
|
||||
now = _now()
|
||||
existing_sources: set[str] = set()
|
||||
source_ids_to_check = [
|
||||
item["source_event_id"]
|
||||
for item in items
|
||||
if item.get("source_event_id") is not None
|
||||
]
|
||||
if source_ids_to_check:
|
||||
rows = (
|
||||
await db.execute(
|
||||
select(MafRoundtableConversationEventRow.source_event_id).where(
|
||||
MafRoundtableConversationEventRow.conversation_id == conversation.id,
|
||||
MafRoundtableConversationEventRow.source_event_id.in_(source_ids_to_check),
|
||||
)
|
||||
)
|
||||
).scalars().all()
|
||||
existing_sources = {str(r) for r in rows if r is not None}
|
||||
|
||||
written = 0
|
||||
for item in items:
|
||||
src_id = item.get("source_event_id")
|
||||
if src_id is not None and src_id in existing_sources:
|
||||
continue
|
||||
|
||||
next_seq = int(conversation.last_stream_seq or 0) + 1
|
||||
conversation.last_stream_seq = next_seq
|
||||
|
||||
db.add(MafRoundtableConversationEventRow(
|
||||
id=uuid4().hex,
|
||||
conversation_id=conversation.id,
|
||||
seq=next_seq,
|
||||
run_id=item.get("run_id"),
|
||||
source_event_id=src_id,
|
||||
event_type=item["event_type"],
|
||||
data_json=item.get("event_data"),
|
||||
created_at=now,
|
||||
))
|
||||
existing_sources.add(src_id) if src_id else None
|
||||
written += 1
|
||||
|
||||
if written:
|
||||
await db.flush()
|
||||
return written
|
||||
|
||||
# ── V2 conversation aggregation: atomic send-message + run launch ───────
|
||||
|
||||
async def send_message_and_start_run(
|
||||
|
||||
Reference in new issue
Block a user