代码提交

This commit is contained in:
董宏凯 committed 2026-08-03 19:27:54 +08:00
1 parent d93cc62806
commit ddf83f7728
25 files changed
+4086 -424

No files matched your search

@@ -18,10 +18,8 @@ from typing import TYPE_CHECKING, Any
from app.maf_roundtable.dispatch_contracts import (
DispatchConfigurationError,
MafDispatchCandidateSet,
MafDispatchCatalog,
MafDispatchPlan,
MafIntentDecision,
resolve_intent,
)
from deerflow.persistence.maf_roundtable.conversations import (
conversation_public_to_dict,
@@ -113,8 +111,24 @@ class MafConversationService:
def _enabled(self) -> bool:
"""Mutating conversation APIs obey the same feature flag as MAF runs."""
if self._enabled_override is not None:
return self._enabled_override
return bool(getattr(self._maf_config, "enabled", False))
if not self._enabled_override:
return False
elif not bool(getattr(self._maf_config, "enabled", False)):
return False
return self.conversation_dispatch_ready
@property
def conversation_dispatch_ready(self) -> bool:
"""Expose only non-sensitive route readiness to the gateway/UI."""
if self._maf_config is None:
return False
return MafDispatchCatalog(self._maf_config).conversation_dispatch_ready
@property
def conversation_dispatch_status(self) -> str:
if self._maf_config is None:
return "configuration_unavailable"
return MafDispatchCatalog(self._maf_config).readiness()[1]
def _read_scope_user(self, *, user_id: str, is_admin: bool) -> str | None:
"""Keep administrator diagnostics read-only and config-gated."""
@@ -129,83 +143,82 @@ class MafConversationService:
title: str,
content: str,
task_context: dict[str, Any] | None,
) -> tuple[dict[str, Any] | None, MafIntentDecision, MafDispatchPlan | None]:
) -> tuple[dict[str, Any] | None, MafDispatchCandidateSet]:
cfg = self._maf_config
if cfg is None:
raise ConversationDispatchError("MAF session configuration is unavailable")
intent = resolve_intent(content, task_context)
plan: MafDispatchPlan | None = None
participants: list[dict[str, Any]] = []
try:
candidate_set = MafDispatchCatalog(cfg).build_candidate_set(
task_context=task_context,
)
except DispatchConfigurationError as exc:
raise ConversationDispatchError(str(exc)) from exc
snapshot_inputs = MafDispatchCatalog.candidate_participants(
candidate_set,
include_seats=True,
)
participants = MafDispatchCatalog.candidate_participants(
candidate_set,
include_seats=False,
)
capability_snapshot: dict[str, Any] | None = None
if intent.intent_type != "clarification":
from app.maf_roundtable.capability_snapshot import (
AgentNotAccessibleError,
InvalidExcludedToolsError,
build_trusted_capability_snapshot,
seat_snapshot_for_agent,
)
if self._app_config is not None:
try:
plan = MafDispatchCatalog(cfg).build_plan(
intent=intent,
task_context=task_context,
capability_snapshot = await build_trusted_capability_snapshot(
user_id=user_id,
participants=snapshot_inputs,
app_config=self._app_config,
agent_store=self._agent_store,
session_model_name=getattr(cfg, "default_model_name", None),
session_reasoning_effort=getattr(cfg, "default_reasoning_effort", None),
)
except DispatchConfigurationError as exc:
raise ConversationDispatchError(str(exc)) from exc
except AgentNotAccessibleError as exc:
raise ConversationDispatchError(
f"configured MAF agent is not accessible: {exc.agent_id}"
) from exc
except InvalidExcludedToolsError as exc:
raise ConversationDispatchError(
f"configured MAF agent has invalid excluded tools: {exc.agent_id}"
) from exc
from app.maf_roundtable.capability_snapshot import (
AgentNotAccessibleError,
InvalidExcludedToolsError,
build_trusted_capability_snapshot,
seat_snapshot_for_agent,
)
participants = MafDispatchCatalog.participants(plan)
if self._app_config is not None:
try:
capability_snapshot = await build_trusted_capability_snapshot(
user_id=user_id,
participants=participants,
app_config=self._app_config,
agent_store=self._agent_store,
session_model_name=getattr(cfg, "default_model_name", None),
session_reasoning_effort=getattr(cfg, "default_reasoning_effort", None),
)
except AgentNotAccessibleError as exc:
raise ConversationDispatchError(
f"configured MAF agent is not accessible: {exc.agent_id}"
) from exc
except InvalidExcludedToolsError as exc:
raise ConversationDispatchError(
f"configured MAF agent has invalid excluded tools: {exc.agent_id}"
) from exc
for participant in participants:
participant["capability_snapshot"] = seat_snapshot_for_agent(
capability_snapshot, str(participant["agent_id"])
)
for participant in participants:
participant["capability_snapshot"] = seat_snapshot_for_agent(
capability_snapshot, str(participant["agent_id"])
)
payload = {
"maf_v2": {
"intent_type": intent.intent_type,
"reason_code": intent.reason_code,
"business_code": (task_context or {}).get("business_code"),
"dispatch_plan": plan.model_dump(mode="json") if plan is not None else None,
"dispatch_contract_enabled": True,
"candidate_set": candidate_set.model_dump(mode="json"),
"candidate_seat_agent_ids": list(candidate_set.candidate_seat_agent_ids),
"manager_agent_id": candidate_set.manager_agent_id,
}
}
blueprint = {
"title": title,
"task_text": content,
"mode": getattr(plan, "mode", None) or getattr(cfg, "default_mode", "structured"),
"mode": candidate_set.mode,
"model_name": getattr(cfg, "default_model_name", None),
"reasoning_effort": getattr(cfg, "default_reasoning_effort", None),
"manager_agent_id": (
plan.manager_agent_id if plan is not None
else getattr(cfg, "default_manager_agent_id", None)
),
"max_rounds": getattr(plan, "max_rounds", None) or getattr(cfg, "default_max_rounds", 8),
"manager_agent_id": candidate_set.manager_agent_id,
"max_rounds": candidate_set.max_rounds,
"write_policy": getattr(cfg, "default_write_policy", "confirm_high_risk"),
"capability_snapshot": capability_snapshot,
"task_payload": payload,
"participants": participants,
"start_run": intent.intent_type != "clarification",
"start_run": True,
}
return blueprint, intent, plan
return blueprint, candidate_set
async def create_conversation(
self, *, user_id: str, title: str = "", task_context: dict[str, Any] | None = None
@@ -275,7 +288,7 @@ class MafConversationService:
raise ConversationNotFoundError()
try:
blueprint, intent, plan = await self._build_dispatch_blueprint(
blueprint, _candidate_set = await self._build_dispatch_blueprint(
user_id=user_id,
title=conv.get("title", ""),
content=content,
@@ -290,7 +303,7 @@ class MafConversationService:
# Do not create a durable running record if the MAF SDK is missing.
# Unit tests may inject a small fake dispatcher without this optional
# readiness method.
if intent.intent_type != "clarification" and self._dispatcher is not None:
if self._dispatcher is not None:
check_sdk = getattr(self._dispatcher, "check_sdk_available", None)
if callable(check_sdk):
try:
@@ -313,48 +326,19 @@ class MafConversationService:
if result.get("no_session_config"):
raise ConversationNoSessionConfigError()
await self._publish_conversation_event(
conversation_id=conversation_id,
owner_user_id=user_id,
event_type="manager.intent",
event_data={
"intent_type": intent.intent_type,
"confidence": intent.confidence,
"reason_code": intent.reason_code,
},
)
if self._hub is not None:
try:
await self._hub.publish(conversation_id)
except Exception: # noqa: BLE001
logger.exception("hub publish failed conv=%s", conversation_id)
if intent.intent_type == "clarification":
await self._publish_conversation_event(
conversation_id=conversation_id,
owner_user_id=user_id,
event_type="clarification.required",
event_data={
"prompt": intent.clarification_prompt,
"reason_code": intent.reason_code,
},
)
else:
if plan is None or result.get("run") is None:
raise ConversationDispatchError("dispatch plan did not produce a run")
await self._publish_conversation_event(
conversation_id=conversation_id,
owner_user_id=user_id,
event_type="manager.dispatch",
event_data={
"intent_type": plan.intent_type,
"mode": plan.mode,
"business_code": plan.business_code,
"manager_agent_id": plan.manager_agent_id,
"seat_agent_ids": list(plan.seat_agent_ids),
"seat_count": len(plan.seat_agent_ids),
},
)
await self._dispatch_run(
conversation_id=conversation_id,
user_id=user_id,
run=result["run"],
)
if result.get("run") is None:
raise ConversationDispatchError("dispatch run was not created")
await self._dispatch_run(
conversation_id=conversation_id,
user_id=user_id,
run=result["run"],
)
return {
"message_id": result["message"]["id"] if result.get("message") else "",
@@ -426,16 +410,115 @@ class MafConversationService:
logger.exception("hub publish failed conv=%s", conversation_id)
return True
async def bridge_workflow_event(self, *, run_id: str, event: dict[str, Any]) -> None:
"""Project a persisted per-run event onto the V2 conversation stream.
_EVENT_TYPE_MAP = {
"agent.turn_started": "agent.message.start",
"agent.message.completed": "agent.message.complete",
"action.waiting_approval": "approval.required",
"action.approved": "approval.decided",
"action.rejected": "approval.decided",
"run.started": "run.status",
"run.failed": "run.error",
"run.cancelled": "run.cancelled",
"manager.decision": "manager.dispatch",
"manager.dispatch": "manager.dispatch",
"clarification.required": "clarification.required",
"workflow.checkpointed": "run.status",
"action.waiting_target_lease": "tool.progress",
"action.unknown": "tool.failed",
}
The per-run event ledger remains the audit source of truth. This
projection is deliberately lossy: only user-visible lifecycle fields
are copied, while tool arguments, tool-call ids, thread ids, run ids,
and raw exception bodies are discarded before the SSE event is saved.
"""
_ALLOWED_TYPES = frozenset({
"agent.message.start", "agent.message.delta", "agent.message.complete",
"seat.delivery.submitted", "manager.dispatch", "manager.review.decision",
"tool.requested", "tool.started", "tool.progress", "tool.completed", "tool.failed",
"skill.selected", "skill.started", "skill.completed", "skill.failed",
"artifact.created", "approval.required", "approval.decided", "clarification.required",
"fact.confirmed", "review.completed", "run.status", "run.error",
"run.cancelled", "run.completed",
})
_SAFE_KEYS: dict[str, dict[str, bool]] = {
"agent.message.start": {"thread_id": False},
"agent.message.delta": {"text": True},
"agent.message.complete": {"content": True, "usage": True, "metadata": False},
"tool.requested": {"tool_name": True},
"tool.started": {"tool_name": True, "node": True},
"tool.progress": {"tool_name": True, "status": True},
"tool.completed": {"tool_name": True, "status": True, "content_preview": True},
"tool.failed": {"tool_name": True, "status": True, "error_code": True},
"skill.selected": {"skill_name": True, "message": True},
"skill.started": {"skill_name": True, "message": True},
"skill.completed": {"skill_name": True, "result_preview": True},
"skill.failed": {"skill_name": True, "error": True},
"artifact.created": {"artifact_type": True, "title": True},
"approval.required": {"tool_name": True, "summary": True},
"approval.decided": {"decided_by": True},
"run.status": {"checkpoint_id": True, "reason": True, "status": True},
"run.error": {"error_code": True, "message": True},
"run.cancelled": {},
"run.completed": {"status": True},
"manager.dispatch": {
"intent_type": True, "execution_mode": True, "convergence_path": True,
"selected_seat_agent_ids": True, "assignments": True, "next_speaker": True,
"reason": True, "source": True, "clarification_prompt": True,
},
"clarification.required": {"prompt": True, "reason": True},
"manager.review.decision": {"decision": True, "reason": True, "quality": True},
"fact.confirmed": {"fact_id": True, "summary": True},
"review.completed": {"summary": True, "decision": True},
"seat.delivery.submitted": {"content": True, "schema_status": True},
}
def _build_projection_items(
self,
event: dict[str, Any],
source_audit_event_id: str | None,
) -> list[dict[str, Any]]:
source_type = str(event.get("event_type") or "")
mapped_type = self._EVENT_TYPE_MAP.get(source_type, source_type)
if mapped_type not in self._ALLOWED_TYPES:
return []
payload = event.get("payload")
if not isinstance(payload, dict):
payload = {}
safe_keys = self._SAFE_KEYS.get(mapped_type, {})
data: dict[str, Any] = {
key: payload[key]
for key in safe_keys
if key in payload and safe_keys[key]
}
if event.get("agent_id"):
data["agent_id"] = event["agent_id"]
if event.get("turn_index") is not None:
data["turn_index"] = event["turn_index"]
source_evt_id = source_audit_event_id or event.get("id") or event.get("source_event_id")
items: list[dict[str, Any]] = [{
"event_type": mapped_type,
"event_data": data,
"source_event_id": source_evt_id,
}]
if source_type == "agent.message.completed":
delivery_data: dict[str, Any] = {
key: payload[key]
for key in ("content", "schema_status")
if key in payload
}
if event.get("agent_id"):
delivery_data["agent_id"] = event["agent_id"]
items.append({
"event_type": "seat.delivery.submitted",
"event_data": delivery_data,
"source_event_id": f"{source_evt_id}:delivery" if source_evt_id else None,
})
return items
async def _resolve_conversation_for_run(self, run_id: str) -> tuple[str, str] | None:
from sqlalchemy import select
async with self._store.session_factory() as db:
row = (
await db.execute(
@@ -449,104 +532,139 @@ class MafConversationService:
)
).scalars().first()
if row is None:
return
conversation_id = row.id
owner_user_id = row.owner_user_id
return None
return row.id, row.owner_user_id
source_type = str(event.get("event_type") or "")
mapped_type = {
"agent.turn_started": "agent.message.start",
"agent.message.completed": "agent.message.complete",
"action.waiting_approval": "approval.required",
"action.approved": "approval.decided",
"action.rejected": "approval.decided",
"run.started": "run.status",
"run.failed": "run.error",
"run.cancelled": "run.cancelled",
"manager.decision": "manager.dispatch",
"workflow.checkpointed": "run.status",
"action.waiting_target_lease": "tool.progress",
"action.unknown": "tool.failed",
}.get(source_type, source_type)
async def bridge_workflow_event(
self,
*,
run_id: str,
event: dict[str, Any],
source_audit_event_id: str | None = None,
) -> bool:
"""Project a persisted per-run event onto the V2 conversation stream.
allowed_types = {
"agent.message.start", "agent.message.delta", "agent.message.complete",
"seat.delivery.submitted", "manager.dispatch", "manager.review.decision",
"tool.requested", "tool.started", "tool.progress", "tool.completed", "tool.failed",
"skill.selected", "skill.started", "skill.completed", "skill.failed",
"artifact.created", "approval.required", "approval.decided",
"fact.confirmed", "review.completed", "run.status", "run.error",
"run.cancelled", "run.completed",
}
if mapped_type not in allowed_types:
return
Returns ``True`` if at least one new conversation event was written,
``False`` if all items were deduped or unmapped.
payload = event.get("payload")
if not isinstance(payload, dict):
payload = {}
safe_keys = {
"agent.message.start": {"thread_id": False},
"agent.message.delta": {"text": True},
"agent.message.complete": {"content": True, "usage": True, "metadata": False},
"tool.requested": {"tool_name": True},
"tool.started": {"tool_name": True, "node": True},
"tool.progress": {"tool_name": True, "status": True},
"tool.completed": {"tool_name": True, "status": True, "content_preview": True},
"tool.failed": {"tool_name": True, "status": True, "error_code": True},
"skill.selected": {"skill_name": True, "message": True},
"skill.started": {"skill_name": True, "message": True},
"skill.completed": {"skill_name": True, "result_preview": True},
"skill.failed": {"skill_name": True, "error": True},
"artifact.created": {"artifact_type": True, "title": True},
"approval.required": {"tool_name": True, "summary": True},
"approval.decided": {"decided_by": True},
"run.status": {"checkpoint_id": True, "reason": True, "status": True},
"run.error": {"error_code": True, "message": True},
"run.cancelled": {},
"run.completed": {"status": True},
"manager.dispatch": {"next_speaker": True, "reason": True},
"manager.review.decision": {"decision": True, "reason": True, "quality": True},
"fact.confirmed": {"fact_id": True, "summary": True},
"review.completed": {"summary": True, "decision": True},
"seat.delivery.submitted": {"content": True, "schema_status": True},
}.get(mapped_type, {})
data: dict[str, Any] = {
key: payload[key]
for key in safe_keys
if key in payload and safe_keys[key]
}
if event.get("agent_id"):
data["agent_id"] = event["agent_id"]
if event.get("turn_index") is not None:
data["turn_index"] = event["turn_index"]
All derived events from one audit event (e.g. ``agent.message.complete``
+ ``seat.delivery.submitted``) are written in a single transaction —
if any insert fails, the entire batch rolls back.
"""
from sqlalchemy import select
# P0-4: use the source audit event's stable id for dedup.
source_evt_id = event.get("id") or event.get("source_event_id")
items = self._build_projection_items(event, source_audit_event_id)
if not items:
return False
await self._publish_conversation_event(
conversation_id=conversation_id,
owner_user_id=owner_user_id,
event_type=mapped_type,
event_data=data,
source_event_id=source_evt_id,
)
if source_type == "agent.message.completed":
# A completed structured seat turn is also a delivery milestone;
# this gives the timeline a stable gate separate from raw text.
delivery_data = {
key: payload[key]
for key in ("content", "schema_status")
if key in payload
resolved = await self._resolve_conversation_for_run(run_id)
if resolved is None:
return False
conversation_id, _owner_user_id = resolved
written = 0
async with self._store.session_factory() as db:
async with db.begin():
conv = (
await db.execute(
select(MafRoundtableConversationRow)
.where(MafRoundtableConversationRow.id == conversation_id)
.with_for_update()
)
).scalars().first()
if conv is None:
return False
written = await self._store.append_conversation_events_idempotently(
db, conversation=conv, items=items,
)
if written > 0 and self._hub is not None:
try:
await self._hub.publish(conversation_id)
except Exception: # noqa: BLE001
logger.exception("hub publish failed conv=%s", conversation_id)
return written > 0
async def reconcile_conversation_events(self, *, run_id: str) -> int:
"""Re-project audit events for a run into the conversation stream.
Scans ``maf_roundtable_events`` for the given run and re-bridges each
event. Because ``bridge_workflow_event`` is idempotent (dedup via
``source_event_id``), this is safe to call after a crash, a restart,
or a projection failure. Events already projected are silently skipped.
Returns the number of **new** conversation events written (deduped
events are NOT counted).
"""
from sqlalchemy import select
from deerflow.persistence.maf_roundtable.model import MafRoundtableEventRow
written = 0
async with self._store.session_factory() as db:
rows = (
await db.execute(
select(MafRoundtableEventRow)
.where(MafRoundtableEventRow.run_id == run_id)
.order_by(MafRoundtableEventRow.seq.asc())
)
).scalars().all()
for row in rows:
event_dict: dict[str, Any] = {
"id": row.id,
"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,
}
if event.get("agent_id"):
delivery_data["agent_id"] = event["agent_id"]
await self._publish_conversation_event(
conversation_id=conversation_id,
owner_user_id=owner_user_id,
event_type="seat.delivery.submitted",
event_data=delivery_data,
source_event_id=f"{source_evt_id}:delivery" if source_evt_id else None,
)
try:
if await self.bridge_workflow_event(
run_id=run_id,
event=event_dict,
source_audit_event_id=row.id,
):
written += 1
except Exception: # noqa: BLE001
logger.warning(
"reconcile: failed to project audit event %s for run %s",
row.id, run_id, exc_info=True,
)
return written
async def reconcile_active_conversations(self) -> int:
"""Scan active/recent MAF runs and reconcile their conversation projections.
Called by the background reconcile task and the SSE connect trigger.
Returns total new conversation events written across all runs.
"""
from sqlalchemy import select
from deerflow.persistence.maf_roundtable.model import (
MafRoundtableConversationRow,
MafRoundtableRunRow,
)
active_statuses = ("pending", "running", "waiting_approval", "waiting_input")
total = 0
async with self._store.session_factory() as db:
rows = (
await db.execute(
select(MafRoundtableRunRow.id)
.join(
MafRoundtableConversationRow,
MafRoundtableConversationRow.active_run_id == MafRoundtableRunRow.id,
)
.where(MafRoundtableRunRow.status.in_(active_statuses))
)
).scalars().all()
for run_id in rows:
try:
total += await self.reconcile_conversation_events(run_id=str(run_id))
except Exception: # noqa: BLE001
logger.warning("background reconcile: run %s failed", run_id, exc_info=True)
return total
async def _dispatch_run(self, *, conversation_id: str, user_id: str, run: dict[str, Any]) -> None:
if self._dispatcher is None:
@@ -620,6 +738,11 @@ class MafConversationService:
logger.exception("MAF conversation watcher failed run=%s", run_id)
return
try:
await self.reconcile_conversation_events(run_id=run_id)
except Exception: # noqa: BLE001
logger.warning("watcher reconcile failed run=%s", run_id, exc_info=True)
run = await self._store.runs.get_run(run_id)
if run is None:
return