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
@@ -95,7 +95,31 @@ class WorkflowRunner:
|
||||
|
||||
try:
|
||||
participants = await self._service.store.participants.list_participants(session_id)
|
||||
thread_map = await self._adapter.ensure_threads(session_id, run_id, participants)
|
||||
|
||||
task_payload = session.get("task_payload") or {}
|
||||
maf_v2_payload = task_payload.get("maf_v2") if isinstance(task_payload, dict) else {}
|
||||
maf_v2_payload = maf_v2_payload if isinstance(maf_v2_payload, dict) else {}
|
||||
candidate_set = maf_v2_payload.get("candidate_set")
|
||||
candidate_set = candidate_set if isinstance(candidate_set, dict) else {}
|
||||
|
||||
# P1-3: determine dispatch_contract_enabled before deciding which
|
||||
# participants should receive threads on the first ensure_threads call.
|
||||
dispatch_contract_enabled = bool(
|
||||
maf_v2_payload.get("dispatch_contract_enabled")
|
||||
or candidate_set
|
||||
or maf_v2_payload.get("dispatch_plan") is not None
|
||||
)
|
||||
|
||||
thread_map = await self._adapter.ensure_threads(
|
||||
session_id,
|
||||
run_id,
|
||||
self._participants_requiring_threads(
|
||||
participants,
|
||||
dispatch_contract_enabled=dispatch_contract_enabled,
|
||||
dispatch_plan=maf_v2_payload.get("dispatch_plan"),
|
||||
intent_type=maf_v2_payload.get("intent_type"),
|
||||
),
|
||||
)
|
||||
|
||||
persisted_seat_agents = [
|
||||
p["agent_id"] for p in participants if p.get("role") != "manager"
|
||||
@@ -196,6 +220,62 @@ class WorkflowRunner:
|
||||
except Exception: # noqa: BLE001
|
||||
return 600.0, True
|
||||
|
||||
def _participants_requiring_threads(
|
||||
self,
|
||||
participants: list[dict[str, Any]],
|
||||
*,
|
||||
dispatch_contract_enabled: bool,
|
||||
dispatch_plan: dict[str, Any] | None,
|
||||
intent_type: str | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Determine which participants should receive threads at startup.
|
||||
|
||||
P1-3: Old V2 sessions may have persisted ALL candidate seats before
|
||||
dispatch was validated. This method restricts the initial
|
||||
``ensure_threads`` call so that unvalidated seats never receive a
|
||||
thread, enforcing the rule "only the manager gets a thread until the
|
||||
dispatch plan is server-validated".
|
||||
|
||||
Rules:
|
||||
┌─────────────────────────────────────────────────────────────────────┐
|
||||
│ State │ Allowed participants │
|
||||
├──────────────────────────────┼──────────────────────────────────────┤
|
||||
│ New V2, not yet dispatched │ manager only │
|
||||
│ Validated direct_answer │ manager only │
|
||||
│ Validated clarification │ manager only (usually no-op) │
|
||||
│ Validated normal dispatch │ manager + selected seats from plan │
|
||||
│ Legacy MAF (no V2 contract) │ all participants (original behavior) │
|
||||
└──────────────────────────────┴──────────────────────────────────────┘
|
||||
"""
|
||||
# Legacy MAF sessions without V2 contract keep original behavior.
|
||||
if not dispatch_contract_enabled:
|
||||
return participants
|
||||
|
||||
manager_participants = [
|
||||
p for p in participants if p.get("role") == "manager"
|
||||
]
|
||||
|
||||
# No validated plan yet → only manager gets a thread.
|
||||
if not dispatch_plan:
|
||||
return manager_participants
|
||||
|
||||
plan_intent = str(dispatch_plan.get("intent_type") or intent_type or "")
|
||||
execution_mode = str(dispatch_plan.get("execution_mode") or "")
|
||||
|
||||
# direct_answer or clarification → manager only.
|
||||
if plan_intent in ("direct_answer", "clarification") or execution_mode == "manager_only":
|
||||
return manager_participants
|
||||
|
||||
# Validated dispatch with selected seats → manager + those seats.
|
||||
selected_ids = set(dispatch_plan.get("seat_agent_ids") or [])
|
||||
if not selected_ids:
|
||||
return manager_participants
|
||||
|
||||
return [
|
||||
p for p in participants
|
||||
if p.get("role") == "manager" or p.get("agent_id") in selected_ids
|
||||
]
|
||||
|
||||
async def _run_manager_dispatch(self, state: RoundtableState) -> None:
|
||||
"""Freeze the V2 manager decision before constructing seat topology."""
|
||||
if state.is_completed("manager_dispatch"):
|
||||
@@ -774,9 +854,19 @@ class WorkflowRunner:
|
||||
)
|
||||
return await self._service.store.runs.get_run(run_id)
|
||||
|
||||
# Ensure threads still bound.
|
||||
# Ensure threads still bound — but only for participants that are
|
||||
# allowed to have threads at this stage (P1-3).
|
||||
participants = await self._service.store.participants.list_participants(state.session_id)
|
||||
thread_map = await self._adapter.ensure_threads(state.session_id, run_id, participants)
|
||||
thread_map = await self._adapter.ensure_threads(
|
||||
state.session_id,
|
||||
run_id,
|
||||
self._participants_requiring_threads(
|
||||
participants,
|
||||
dispatch_contract_enabled=state.dispatch_contract_enabled,
|
||||
dispatch_plan=state.dispatch_plan,
|
||||
intent_type=state.intent_type,
|
||||
),
|
||||
)
|
||||
state.thread_map.update(thread_map)
|
||||
|
||||
if run["status"] == "waiting_approval":
|
||||
|
||||
Reference in new issue
Block a user