feat(roundtable): 会商诊断日志 + 后台挂起卡死兜底(idle 超时)
- 新增会商全流程诊断日志:新表 roundtable_diagnostics + 仓储 + admin API (/api/roundtable-diagnostics) + 管理员诊断页(会商页右上角设置按钮入口), 覆盖第一/二/三步及后台执行器/进程内网关各处报错与关键事件,支持按 范围/阶段/级别/事件/关键词筛选 + 分页。诊断写入全程 best-effort,绝不打断会商。 - 修复后台挂起「执行完第一个席位后永久卡死」:进程内网关每轮 agent run 改为 idle(无活动)超时(默认 20min)——只要还在持续产出 token/工具事件就一直等, 仅当连续无任何输出才判卡死并取消+报错+落 seat_turn_timeout 诊断;慢但活着的 生成绝不打断。可经 ROUNDTABLE_SEAT_IDLE_TIMEOUT_SECONDS 调整。 - 一并提交进行中的会商 seat-skill 注入 / 业务链条编辑器相关改动。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
8ca16c74bc
commit
4a403c95c5
27 files changed
+1873
-61
No files matched your search
+57
@@ -101,6 +101,11 @@ async def run_orchestration(
|
||||
intent = (intent_text or seed_message or "").strip()
|
||||
try:
|
||||
await progress.begin()
|
||||
await progress.diag(
|
||||
stage="step2_orchestration", level="info", event="begin",
|
||||
message=f"会商编排启动(mode={mode},{len(agents)} 个席位)",
|
||||
detail={"mode": mode, "agents": [a.agent_id for a in agents]},
|
||||
)
|
||||
dag_plan = parse_dag_plan(plan) if mode == "dag" else None
|
||||
if mode == "dag" and dag_plan is not None:
|
||||
outcome = await _run_dag(gateway, progress, agents, thread_ids, coordinator_name, model, seed_message, intent, dag_plan)
|
||||
@@ -112,6 +117,11 @@ async def run_orchestration(
|
||||
|
||||
if outcome.kind == "clarification":
|
||||
question = outcome.question or "总控需要补充信息,请补充后继续。"
|
||||
await progress.diag(
|
||||
stage="step2_leader", level="info", event="clarification",
|
||||
message="总控发起澄清,会商暂停等待补充信息",
|
||||
detail={"question": question[:500]},
|
||||
)
|
||||
await progress.awaiting_input(question)
|
||||
return OrchestrationResult(
|
||||
status="awaiting_input",
|
||||
@@ -120,8 +130,17 @@ async def run_orchestration(
|
||||
dialogues=progress.dialogues(),
|
||||
)
|
||||
|
||||
if outcome.kind == "max_cycles":
|
||||
await progress.diag(
|
||||
stage="step2_orchestration", level="warning", event="max_cycles",
|
||||
message=f"达到最大研讨轮次({MAX_CYCLES}),强制收口——可能未真正达成共识",
|
||||
)
|
||||
# consensus 或 max_cycles:均收口。Step3(总结报告 + 结果绘制)按 enable_report 开关执行。
|
||||
await progress.consensus(100)
|
||||
await progress.diag(
|
||||
stage="step2_orchestration", level="info", event="consensus",
|
||||
message="研讨收口,进入第三步" if enable_report else "研讨收口(未启用第三步报告)",
|
||||
)
|
||||
step3 = None
|
||||
if enable_report:
|
||||
step3 = await _run_step3(
|
||||
@@ -144,6 +163,11 @@ async def run_orchestration(
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 — 顶层兜底:标记 error 并返回
|
||||
logger.exception("roundtable orchestration failed")
|
||||
await progress.diag(
|
||||
stage="step2_orchestration", level="error", event="orchestration_crash",
|
||||
message=f"会商编排顶层异常:{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
)
|
||||
await progress.error(str(exc))
|
||||
return OrchestrationResult(status="error", error=str(exc), dialogues=progress.dialogues())
|
||||
|
||||
@@ -229,6 +253,12 @@ async def _run_recommend(
|
||||
seat_msg = _seat_message(intent_text, deliveries, name_by_id, task)
|
||||
seat = await gateway.run_seat(thread_ids=thread_ids, agent_id=agent_id, task=seat_msg, model=model)
|
||||
deliveries[agent_id] = seat.final_text
|
||||
if not (seat.final_text or "").strip():
|
||||
await progress.diag(
|
||||
stage="step2_seat", level="warning", event="seat_empty_delivery",
|
||||
message=f"席位「{name_by_id.get(agent_id, agent_id)}」本轮交付为空(可能模型未产出或被截断)",
|
||||
agent_id=agent_id, agent_name=name_by_id.get(agent_id, agent_id), cycle=cycle,
|
||||
)
|
||||
await progress.add_seat_dialogue(
|
||||
agent_id, name_by_id.get(agent_id, agent_id), seat.final_text, cycle,
|
||||
tool_calls=seat.tool_calls, thread_id=thread_ids.get(agent_id),
|
||||
@@ -298,6 +328,12 @@ async def _run_chain(
|
||||
seat_msg = _seat_message(intent_text, deliveries, name_by_id, task)
|
||||
seat_turn = await gateway.run_seat(thread_ids=thread_ids, agent_id=seat.agent_id, task=seat_msg, model=model)
|
||||
deliveries[seat.agent_id] = seat_turn.final_text
|
||||
if not (seat_turn.final_text or "").strip():
|
||||
await progress.diag(
|
||||
stage="step2_seat", level="warning", event="seat_empty_delivery",
|
||||
message=f"席位「{seat.name}」本轮交付为空(可能模型未产出或被截断)",
|
||||
agent_id=seat.agent_id, agent_name=seat.name, cycle=i + 1,
|
||||
)
|
||||
await progress.add_seat_dialogue(
|
||||
seat.agent_id, seat.name, seat_turn.final_text, i + 1,
|
||||
tool_calls=seat_turn.tool_calls, thread_id=thread_ids.get(seat.agent_id),
|
||||
@@ -398,6 +434,12 @@ async def _run_dag(
|
||||
seat_msg = _seat_message(intent_text, deliveries, name_by_id, task)
|
||||
seat_turn = await gateway.run_seat(thread_ids=thread_ids, agent_id=seat.agent_id, task=seat_msg, model=model)
|
||||
deliveries[seat.agent_id] = seat_turn.final_text
|
||||
if not (seat_turn.final_text or "").strip():
|
||||
await progress.diag(
|
||||
stage="step2_seat", level="warning", event="seat_empty_delivery",
|
||||
message=f"席位「{seat.name}」本轮交付为空(可能模型未产出或被截断)",
|
||||
agent_id=seat.agent_id, agent_name=seat.name, cycle=i + 1,
|
||||
)
|
||||
await progress.add_seat_dialogue(
|
||||
seat.agent_id, seat.name, seat_turn.final_text, i + 1,
|
||||
tool_calls=seat_turn.tool_calls, thread_id=thread_ids.get(seat.agent_id),
|
||||
@@ -477,6 +519,11 @@ async def _run_step3(
|
||||
}
|
||||
except Exception as exc: # noqa: BLE001 — 总结失败不阻塞结果绘制
|
||||
logger.exception("roundtable step3 summary failed")
|
||||
await progress.diag(
|
||||
stage="step3_summary", level="error", event="summary_failed",
|
||||
message=f"第三步「方案总结报告」生成失败:{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
)
|
||||
errors.append(exc)
|
||||
|
||||
async def _report_task() -> None:
|
||||
@@ -494,6 +541,11 @@ async def _run_step3(
|
||||
step3.update(report)
|
||||
except Exception as exc: # noqa: BLE001 — 绘制失败时保留已写出的总结报告
|
||||
logger.exception("roundtable step3 report failed")
|
||||
await progress.diag(
|
||||
stage="step3_report", level="error", event="report_failed",
|
||||
message=f"第三步「结果绘制」(方案总览.html) 生成失败:{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
)
|
||||
errors.append(exc)
|
||||
|
||||
async def _dashboard_task() -> None:
|
||||
@@ -510,6 +562,11 @@ async def _run_step3(
|
||||
step3["dashboardJson"] = data_json
|
||||
except Exception as exc: # noqa: BLE001 — 大屏失败不拖垮另两段
|
||||
logger.exception("roundtable step3 dashboard failed")
|
||||
await progress.diag(
|
||||
stage="step3_dashboard", level="error", event="dashboard_failed",
|
||||
message=f"第三步「大屏数据」(report-json) 生成失败:{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
)
|
||||
errors.append(exc)
|
||||
|
||||
await asyncio.gather(_summary_task(), _report_task(), _dashboard_task())
|
||||
|
||||
+33
@@ -53,11 +53,15 @@ class JobProgressWriter:
|
||||
agents: list[SeatRef],
|
||||
event_sink: EventSink = None,
|
||||
on_dialogue: Any | None = None,
|
||||
recorder: Any | None = None,
|
||||
) -> None:
|
||||
self._store = store
|
||||
self._job_id = job_id
|
||||
self._agents = list(agents)
|
||||
self._event_sink = event_sink
|
||||
# 诊断日志记录器(DiagnosticsRecorder,已绑定本作业上下文)。引擎经
|
||||
# ``await progress.diag(...)`` 在每个报错 / 关键转折处落一条事件。None = 不记录。
|
||||
self._recorder = recorder
|
||||
# 每轮 leader/seat 模型返回后回调(dialogues, consensus)—— 执行器据此把对话
|
||||
# 写进草稿(和手动跑同一存取路径)。
|
||||
self._on_dialogue = on_dialogue
|
||||
@@ -105,6 +109,35 @@ class JobProgressWriter:
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("roundtable job progress event sink failed", exc_info=True)
|
||||
|
||||
async def diag(
|
||||
self,
|
||||
*,
|
||||
stage: str,
|
||||
level: str,
|
||||
event: str,
|
||||
message: str | None = None,
|
||||
detail: Any = None,
|
||||
agent_id: str | None = None,
|
||||
agent_name: str | None = None,
|
||||
cycle: int | None = None,
|
||||
) -> None:
|
||||
"""落一条诊断日志(best-effort;无 recorder 时 no-op)。"""
|
||||
if self._recorder is None:
|
||||
return
|
||||
try:
|
||||
await self._recorder.record(
|
||||
stage=stage,
|
||||
level=level,
|
||||
event=event,
|
||||
message=message,
|
||||
detail=detail,
|
||||
agent_id=agent_id,
|
||||
agent_name=agent_name,
|
||||
cycle=cycle,
|
||||
)
|
||||
except Exception: # noqa: BLE001 — 诊断日志绝不打断编排
|
||||
logger.debug("roundtable diag record failed", exc_info=True)
|
||||
|
||||
# ── 语义方法(引擎调用)─────────────────────────────────────────────────
|
||||
|
||||
async def begin(self) -> None:
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
"""Create roundtable_diagnostics table.
|
||||
|
||||
会商全流程诊断日志(第一/二/三步 + 后台执行器 + 进程内网关各处报错与关键事件),
|
||||
管理员诊断页的数据源。全局/共享,不按 user 分权。
|
||||
|
||||
On a brand-new DB the table is created by ``create_all``; this revision patches
|
||||
existing DBs (alembic auto-upgrade is disabled at runtime — see
|
||||
``persistence/engine.py`` — so operators run this / the column_additions SQL).
|
||||
|
||||
Revision ID: 20260619_01
|
||||
Revises: 20260618_01
|
||||
Create Date: 2026-06-19
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.dialects.mysql import LONGTEXT
|
||||
|
||||
|
||||
revision = "20260619_01"
|
||||
down_revision = "20260618_01"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def _has_table(table_name: str) -> bool:
|
||||
inspector = inspect(op.get_bind())
|
||||
return table_name in inspector.get_table_names()
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
if _has_table("roundtable_diagnostics"):
|
||||
return
|
||||
|
||||
long_text = LONGTEXT() if op.get_bind().dialect.name == "mysql" else sa.Text()
|
||||
|
||||
op.create_table(
|
||||
"roundtable_diagnostics",
|
||||
sa.Column("id", sa.String(64), primary_key=True),
|
||||
sa.Column("scope", sa.String(16), nullable=False, server_default="background"),
|
||||
sa.Column("stage", sa.String(32), nullable=False, server_default="job"),
|
||||
sa.Column("level", sa.String(16), nullable=False, server_default="info"),
|
||||
sa.Column("event", sa.String(64), nullable=False, server_default=""),
|
||||
sa.Column("message", sa.Text(), nullable=True),
|
||||
sa.Column("detail", long_text, nullable=True),
|
||||
sa.Column("job_id", sa.String(64), nullable=True),
|
||||
sa.Column("draft_id", sa.String(64), nullable=True),
|
||||
sa.Column("task_id", sa.String(128), nullable=True),
|
||||
sa.Column("user_id", sa.String(64), nullable=True),
|
||||
sa.Column("agent_id", sa.String(128), nullable=True),
|
||||
sa.Column("agent_name", sa.String(255), nullable=True),
|
||||
sa.Column("cycle", sa.Integer(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
)
|
||||
op.create_index("ix_roundtable_diagnostics_scope", "roundtable_diagnostics", ["scope"])
|
||||
op.create_index("ix_roundtable_diagnostics_stage", "roundtable_diagnostics", ["stage"])
|
||||
op.create_index("ix_roundtable_diagnostics_level", "roundtable_diagnostics", ["level"])
|
||||
op.create_index("ix_roundtable_diagnostics_job_id", "roundtable_diagnostics", ["job_id"])
|
||||
op.create_index("ix_roundtable_diagnostics_draft_id", "roundtable_diagnostics", ["draft_id"])
|
||||
op.create_index("ix_roundtable_diagnostics_task_id", "roundtable_diagnostics", ["task_id"])
|
||||
op.create_index("ix_roundtable_diagnostics_user_id", "roundtable_diagnostics", ["user_id"])
|
||||
op.create_index("ix_roundtable_diagnostics_created_at", "roundtable_diagnostics", ["created_at"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
if _has_table("roundtable_diagnostics"):
|
||||
op.drop_table("roundtable_diagnostics")
|
||||
+2
@@ -54,6 +54,7 @@ from deerflow.persistence.roundtable_drafts.model import (
|
||||
RoundtableDraftRow,
|
||||
RoundtableRecommendHistoryRow,
|
||||
)
|
||||
from deerflow.persistence.roundtable_diagnostics.model import RoundtableDiagnosticRow
|
||||
from deerflow.persistence.roundtable_jobs.model import RoundtableJobRow
|
||||
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
|
||||
from deerflow.persistence.run.model import RunRow
|
||||
@@ -108,6 +109,7 @@ __all__ = [
|
||||
"RoundtableChainRow",
|
||||
"RoundtableDraftRow",
|
||||
"RoundtableDraftShareRow",
|
||||
"RoundtableDiagnosticRow",
|
||||
"RoundtableJobRow",
|
||||
"RoundtableRecommendHistoryRow",
|
||||
"RoundtableTaskDraftRow",
|
||||
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
"""圆桌会商诊断日志持久化。
|
||||
|
||||
对外暴露:
|
||||
- ``make_roundtable_diagnostic_store(sf)`` —— 建仓储(deps.py 启动时调)。
|
||||
- ``register_default_store`` / ``get_default_store`` —— 进程级默认仓储(跨 app/harness 边界,
|
||||
与 ``roundtable_jobs`` 同套做法:harness 侧编排引擎拿不到 FastAPI ``app.state``)。
|
||||
- ``record_diagnostic(...)`` —— **best-effort** 写一条事件(store 为 None / 出错均静默吞掉,
|
||||
诊断日志本身绝不能反过来打断会商)。
|
||||
- ``DiagnosticsRecorder`` —— 绑定一次作业上下文(scope/job_id/draft_id/task_id/user_id)的
|
||||
便捷记录器,引擎 / 网关 / 执行器逐处调用 ``await recorder.record(stage=..., level=..., ...)``。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from deerflow.persistence.roundtable_diagnostics.model import RoundtableDiagnosticRow
|
||||
from deerflow.persistence.roundtable_diagnostics.sql import RoundtableDiagnosticRepository
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def make_roundtable_diagnostic_store(session_factory) -> RoundtableDiagnosticRepository | None:
|
||||
if session_factory is None:
|
||||
return None
|
||||
return RoundtableDiagnosticRepository(session_factory)
|
||||
|
||||
|
||||
# ── 进程级默认仓储(跨 app/harness 边界)──────────────────────────────────────────
|
||||
_default_store: RoundtableDiagnosticRepository | None = None
|
||||
|
||||
|
||||
def register_default_store(store: RoundtableDiagnosticRepository | None) -> None:
|
||||
global _default_store
|
||||
_default_store = store
|
||||
|
||||
|
||||
def get_default_store() -> RoundtableDiagnosticRepository | None:
|
||||
return _default_store
|
||||
|
||||
|
||||
async def record_diagnostic(store: RoundtableDiagnosticRepository | None = None, **fields: Any) -> None:
|
||||
"""Best-effort 写一条诊断事件。``store`` 缺省用进程级默认仓储。任何异常静默吞掉。"""
|
||||
repo = store or _default_store
|
||||
if repo is None:
|
||||
return
|
||||
try:
|
||||
await repo.record(**fields)
|
||||
except Exception: # noqa: BLE001 — 诊断日志写失败绝不打断会商
|
||||
logger.debug("roundtable diagnostic record failed", exc_info=True)
|
||||
|
||||
|
||||
class DiagnosticsRecorder:
|
||||
"""绑定一次会商上下文的记录器。所有 ``record`` 均 best-effort。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
store: RoundtableDiagnosticRepository | None = None,
|
||||
*,
|
||||
scope: str = "background",
|
||||
job_id: str | None = None,
|
||||
draft_id: str | None = None,
|
||||
task_id: str | None = None,
|
||||
user_id: str | None = None,
|
||||
) -> None:
|
||||
self._store = store
|
||||
self._scope = scope
|
||||
self._job_id = job_id
|
||||
self._draft_id = draft_id
|
||||
self._task_id = task_id
|
||||
self._user_id = user_id
|
||||
|
||||
async def record(
|
||||
self,
|
||||
*,
|
||||
stage: str,
|
||||
level: str,
|
||||
event: str,
|
||||
message: str | None = None,
|
||||
detail: Any = None,
|
||||
agent_id: str | None = None,
|
||||
agent_name: str | None = None,
|
||||
cycle: int | None = None,
|
||||
) -> None:
|
||||
await record_diagnostic(
|
||||
self._store,
|
||||
scope=self._scope,
|
||||
stage=stage,
|
||||
level=level,
|
||||
event=event,
|
||||
message=message,
|
||||
detail=detail,
|
||||
job_id=self._job_id,
|
||||
draft_id=self._draft_id,
|
||||
task_id=self._task_id,
|
||||
user_id=self._user_id,
|
||||
agent_id=agent_id,
|
||||
agent_name=agent_name,
|
||||
cycle=cycle,
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"RoundtableDiagnosticRow",
|
||||
"RoundtableDiagnosticRepository",
|
||||
"DiagnosticsRecorder",
|
||||
"make_roundtable_diagnostic_store",
|
||||
"register_default_store",
|
||||
"get_default_store",
|
||||
"record_diagnostic",
|
||||
]
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
"""圆桌会商「诊断日志」ORM 模型。
|
||||
|
||||
记录会商全流程(第一步意图 / 第二步研讨 / 第三步报告 + 后台挂起执行器 / 进程内网关)
|
||||
各处可能发生的报错与关键事件,供管理员在前端诊断页筛选排查。
|
||||
|
||||
设计要点:
|
||||
- **全局/共享**:不按 user 分权(``user_id`` 仅审计),管理员读全量;写入开放(best-effort)。
|
||||
- 一行 = 一条事件。``detail`` 用 ``PortableLongText`` 存结构化补充信息(堆栈尾、上下文)。
|
||||
- 按 ``created_at`` / ``stage`` / ``level`` / ``job_id`` / ``task_id`` 建索引,支撑筛选。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableLongText
|
||||
|
||||
|
||||
class RoundtableDiagnosticRow(Base):
|
||||
__tablename__ = "roundtable_diagnostics"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
# background(后台挂起)| foreground(前台实时会商)。
|
||||
scope: Mapped[str] = mapped_column(String(16), nullable=False, default="background", index=True)
|
||||
# 阶段:step1_intent / step2_init / step2_leader / step2_seat / step2_orchestration /
|
||||
# step3_summary / step3_report / step3_dashboard / step3_structure / upstream / job。
|
||||
stage: Mapped[str] = mapped_column(String(32), nullable=False, default="job", index=True)
|
||||
# 级别:info | warning | error。
|
||||
level: Mapped[str] = mapped_column(String(16), nullable=False, default="info", index=True)
|
||||
# 短事件码(如 seat_turn_timeout / model_error / report_truncated / dispatch_invalid)。
|
||||
event: Mapped[str] = mapped_column(String(64), nullable=False, default="")
|
||||
# 人类可读的报错说明(中文)。
|
||||
message: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
# 结构化补充:堆栈尾、上下文、原始 body 片段等。
|
||||
detail: Mapped[str | None] = mapped_column(PortableLongText, nullable=True)
|
||||
|
||||
# 关联上下文(均可空)。
|
||||
job_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
draft_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
task_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
||||
user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
agent_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
agent_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
cycle: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC), index=True
|
||||
)
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
"""SQLAlchemy-backed 圆桌会商诊断日志 store。
|
||||
|
||||
写入(``record``)best-effort、全局共享;读取(``list_logs`` / ``count`` / ``facets``)
|
||||
带多维筛选,供管理员诊断页用。``detail`` 以 JSON 序列化进 ``PortableLongText``。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.roundtable_diagnostics.model import RoundtableDiagnosticRow
|
||||
|
||||
|
||||
def _dumps(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
try:
|
||||
return json.dumps(value, ensure_ascii=False, default=str)
|
||||
except Exception:
|
||||
return str(value)
|
||||
|
||||
|
||||
def _loads(value: Any) -> Any:
|
||||
if not isinstance(value, str) or not value:
|
||||
return value or None
|
||||
try:
|
||||
return json.loads(value)
|
||||
except Exception:
|
||||
return value # 非 JSON 文本(如纯字符串 detail)原样返回
|
||||
|
||||
|
||||
class RoundtableDiagnosticRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
@staticmethod
|
||||
def _to_dict(row: RoundtableDiagnosticRow) -> dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"scope": row.scope,
|
||||
"stage": row.stage,
|
||||
"level": row.level,
|
||||
"event": row.event,
|
||||
"message": row.message,
|
||||
"detail": _loads(row.detail),
|
||||
"job_id": row.job_id,
|
||||
"draft_id": row.draft_id,
|
||||
"task_id": row.task_id,
|
||||
"user_id": row.user_id,
|
||||
"agent_id": row.agent_id,
|
||||
"agent_name": row.agent_name,
|
||||
"cycle": row.cycle,
|
||||
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
|
||||
}
|
||||
|
||||
async def record(
|
||||
self,
|
||||
*,
|
||||
scope: str = "background",
|
||||
stage: str = "job",
|
||||
level: str = "info",
|
||||
event: str = "",
|
||||
message: str | None = None,
|
||||
detail: Any = None,
|
||||
job_id: str | None = None,
|
||||
draft_id: str | None = None,
|
||||
task_id: str | None = None,
|
||||
user_id: str | None = None,
|
||||
agent_id: str | None = None,
|
||||
agent_name: str | None = None,
|
||||
cycle: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""插入一条诊断事件,返回序列化 dict。"""
|
||||
row = RoundtableDiagnosticRow(
|
||||
id=uuid4().hex,
|
||||
scope=(scope or "background")[:16],
|
||||
stage=(stage or "job")[:32],
|
||||
level=(level or "info")[:16],
|
||||
event=(event or "")[:64],
|
||||
message=message,
|
||||
detail=_dumps(detail),
|
||||
job_id=job_id,
|
||||
draft_id=draft_id,
|
||||
task_id=task_id,
|
||||
user_id=user_id,
|
||||
agent_id=(agent_id or None) if agent_id is None else str(agent_id)[:128],
|
||||
agent_name=(agent_name or None) if agent_name is None else str(agent_name)[:255],
|
||||
cycle=cycle,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return self._to_dict(row)
|
||||
|
||||
def _apply_filters(
|
||||
self,
|
||||
stmt,
|
||||
*,
|
||||
scope: str | None,
|
||||
stage: str | None,
|
||||
level: str | None,
|
||||
event: str | None,
|
||||
job_id: str | None,
|
||||
draft_id: str | None,
|
||||
task_id: str | None,
|
||||
user_id: str | None,
|
||||
q: str | None,
|
||||
since: datetime | None,
|
||||
until: datetime | None,
|
||||
):
|
||||
R = RoundtableDiagnosticRow
|
||||
if scope:
|
||||
stmt = stmt.where(R.scope == scope)
|
||||
if stage:
|
||||
stmt = stmt.where(R.stage == stage)
|
||||
if level:
|
||||
stmt = stmt.where(R.level == level)
|
||||
if event:
|
||||
stmt = stmt.where(R.event == event)
|
||||
if job_id:
|
||||
stmt = stmt.where(R.job_id == job_id)
|
||||
if draft_id:
|
||||
stmt = stmt.where(R.draft_id == draft_id)
|
||||
if task_id:
|
||||
stmt = stmt.where(R.task_id == task_id)
|
||||
if user_id:
|
||||
stmt = stmt.where(R.user_id == user_id)
|
||||
if q:
|
||||
like = f"%{q}%"
|
||||
stmt = stmt.where(R.message.ilike(like))
|
||||
if since is not None:
|
||||
stmt = stmt.where(R.created_at >= since)
|
||||
if until is not None:
|
||||
stmt = stmt.where(R.created_at <= until)
|
||||
return stmt
|
||||
|
||||
async def list_logs(
|
||||
self,
|
||||
*,
|
||||
scope: str | None = None,
|
||||
stage: str | None = None,
|
||||
level: str | None = None,
|
||||
event: str | None = None,
|
||||
job_id: str | None = None,
|
||||
draft_id: str | None = None,
|
||||
task_id: str | None = None,
|
||||
user_id: str | None = None,
|
||||
q: str | None = None,
|
||||
since: datetime | None = None,
|
||||
until: datetime | None = None,
|
||||
limit: int = 100,
|
||||
offset: int = 0,
|
||||
) -> tuple[list[dict[str, Any]], int]:
|
||||
"""筛选 + 分页(newest-first)。返回 ``(items, total)``。"""
|
||||
R = RoundtableDiagnosticRow
|
||||
base = self._apply_filters(
|
||||
select(R),
|
||||
scope=scope, stage=stage, level=level, event=event,
|
||||
job_id=job_id, draft_id=draft_id, task_id=task_id, user_id=user_id,
|
||||
q=q, since=since, until=until,
|
||||
)
|
||||
count_stmt = self._apply_filters(
|
||||
select(func.count()).select_from(R),
|
||||
scope=scope, stage=stage, level=level, event=event,
|
||||
job_id=job_id, draft_id=draft_id, task_id=task_id, user_id=user_id,
|
||||
q=q, since=since, until=until,
|
||||
)
|
||||
page_stmt = base.order_by(R.created_at.desc(), R.id.desc()).limit(limit).offset(offset)
|
||||
async with self._sf() as session:
|
||||
total = int((await session.execute(count_stmt)).scalar() or 0)
|
||||
rows = (await session.execute(page_stmt)).scalars().all()
|
||||
return [self._to_dict(r) for r in rows], total
|
||||
|
||||
async def facets(self) -> dict[str, list[str]]:
|
||||
"""返回各筛选维度的 distinct 取值(供前端下拉),供筛选 UI 用。"""
|
||||
R = RoundtableDiagnosticRow
|
||||
out: dict[str, list[str]] = {}
|
||||
async with self._sf() as session:
|
||||
for key, col in (("scope", R.scope), ("stage", R.stage), ("level", R.level), ("event", R.event)):
|
||||
rows = (await session.execute(select(col).distinct())).scalars().all()
|
||||
out[key] = sorted([str(v) for v in rows if v])
|
||||
return out
|
||||
|
||||
async def list_older_than(self, cutoff: datetime, *, limit: int = 1000) -> list[str]:
|
||||
R = RoundtableDiagnosticRow
|
||||
stmt = select(R.id).where(R.created_at < cutoff).limit(limit)
|
||||
async with self._sf() as session:
|
||||
return [str(v) for v in (await session.execute(stmt)).scalars().all()]
|
||||
|
||||
async def delete_by_ids(self, ids: list[str]) -> int:
|
||||
if not ids:
|
||||
return 0
|
||||
R = RoundtableDiagnosticRow
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(delete(R).where(R.id.in_(ids)))
|
||||
await session.commit()
|
||||
return int(result.rowcount or 0)
|
||||
Reference in new issue
Block a user