feat(deep-research): 强化任务链路可靠性并接入 agentfx 报告助手种子

功能说明:\n- 重构 Deep Research 调度与执行链,补齐 dispatcher、executor、live hub 与依赖注入入口,提升任务下发、执行、回调的稳定性\n- 增强运行时持久化与恢复能力,接入业务映射、任务来源与任务事件日志模型,覆盖持久化与执行可靠性关键路径测试\n- 新增 agentfx 报告助手 seed 与任务报告导入脚本配置,支持离线环境快速初始化与任务流模板回填\n- 完善 deep-research 与 thread_runs 路由参数编排,兼容不同业务映射与运行入口的前后端联动\n- 补充前端 agentfx deep-link 解析,保证 deep-research 场景下的登录跳转与任务上下文一致
This commit is contained in:
董宏凯 committed 2026-09-04 23:18:52 +08:00
1 parent 438772f0b7
commit 5c75e1af16
36 files changed
+3163 -312

No files matched your search

@@ -1,10 +1,10 @@
"""ORM model for the global business → agent / chain mapping (业务链条管理).
A "business mapping" row binds a fixed business code (3Q / 6BF / 7BF / 8BF) to an
optional 智能体 (``agent_id``) and an optional 业务链条 (``chain_id`` → a
``roundtable_chains`` row, which must be **public** so every account can resolve
it). Used to auto-pick the business chain when a task page launches the
multi-agent roundtable.
A "business mapping" row binds a fixed business code (3Q / 6BF / 6-1…6-4 / 7BF /
8BF / AFX) to an optional 智能体 (``agent_id``) and an optional 业务链条
(``chain_id`` → a ``roundtable_chains`` row, which must be **public** so every
account can resolve it). Used to auto-pick the business chain when a task page
launches the multi-agent roundtable.
Unlike ``roundtable_chains`` (per-user), this mapping is **global / shared** —
there is no ``user_id`` column; all accounts see and use the same configuration.
@@ -1,7 +1,7 @@
"""SQLAlchemy-backed global business → agent / chain mapping store.
Global / shared (no ``user_id`` scoping): every account reads and writes the same
rows. The four business rows are seeded once on startup (``ensure_seed_defaults``)
rows. Fixed business rows are seeded once on startup (``ensure_seed_defaults``)
with empty agent/chain; users fill those in from the management page via
``update_mapping``.
"""
@@ -21,12 +21,22 @@ _UNSET = object()
# Fixed business rows (业务码 → 展示名). Seeded on first boot; agent/chain left empty.
# 8BF 页面尚未建好,先占位保留行。顺序即管理表的展示顺序。
# AFX = goPath=agentfx 深链(新系统 iframe 嵌入的智能体问答,任务详情取本系统 cop-task-detail)。
# 6-1…6-4 / 7-1…7-4 = 独立配置位(与 3Q/6BF/7BF 等无关),固定排在表尾。
DEFAULT_BUSINESS_ROWS: list[dict[str, Any]] = [
{"business_code": "3Q", "label": "3QFX"},
{"business_code": "6BF", "label": "任务FX"},
{"business_code": "7BF", "label": "XDFX"},
{"business_code": "8BF", "label": "8BF"},
{"business_code": "AFX", "label": "AGENTFX"},
# 独立配置位,与上方业务码无关;固定排在表尾。
{"business_code": "6-1", "label": "6-1"},
{"business_code": "6-2", "label": "6-2"},
{"business_code": "6-3", "label": "6-3"},
{"business_code": "6-4", "label": "6-4"},
{"business_code": "7-1", "label": "7-1"},
{"business_code": "7-2", "label": "7-2"},
{"business_code": "7-3", "label": "7-3"},
{"business_code": "7-4", "label": "7-4"},
]
@@ -94,14 +104,24 @@ class BusinessMappingRepository:
async def ensure_seed_defaults(self) -> int:
"""Idempotently insert the fixed business rows if missing. Returns inserted count.
Never overwrites an existing row (respects user-configured agent/chain).
Never overwrites an existing row's agent/chain (respects user config).
Re-syncs ``sort_order`` (and empty ``label``) so newly inserted codes keep
the intended table order next to older seeded rows.
"""
inserted = 0
dirty = False
now = datetime.now(UTC)
async with self._sf() as session:
for idx, seed in enumerate(DEFAULT_BUSINESS_ROWS):
existing = await session.get(BusinessMappingRow, seed["business_code"])
if existing is not None:
if existing.sort_order != idx:
existing.sort_order = idx
dirty = True
seed_label = str(seed.get("label") or "")
if seed_label and not (existing.label or "").strip():
existing.label = seed_label
dirty = True
continue
session.add(
BusinessMappingRow(
@@ -117,6 +137,7 @@ class BusinessMappingRepository:
)
)
inserted += 1
if inserted:
dirty = True
if dirty:
await session.commit()
return inserted
@@ -137,6 +137,38 @@ class DeepResearchJobRepository:
# Should not happen in practice.
raise RuntimeError("try_create_or_get_active: race condition not resolved after retries")
async def replace_input_snapshot(self, job_id: str, input_snapshot: dict[str, Any]) -> dict[str, Any] | None:
"""Overwrite the frozen start payload on a still-queued job.
Idempotent retries used to keep the first snapshot forever. A first
write that raced ahead of the browser evidence (or a reused empty
job) then ran without ``collector_materials`` even though the later
request carried them.
"""
async with self._sf() as session:
result = await session.execute(
update(DeepResearchJobRow)
.where(
DeepResearchJobRow.id == job_id,
DeepResearchJobRow.status.in_(("queued", "running")),
)
.values(
input_snapshot=_dumps(input_snapshot),
updated_at=datetime.now(UTC),
)
)
if result.rowcount == 0:
await session.rollback()
return None
row = (
await session.execute(
select(DeepResearchJobRow).where(DeepResearchJobRow.id == job_id)
)
).scalar_one_or_none()
snapshot = self._row_to_dict(row) if row else None
await session.commit()
return snapshot
async def _get_by_dedupe_key(self, dedupe_key: str) -> dict[str, Any] | None:
async with self._sf() as session:
row = (
@@ -198,12 +230,13 @@ class DeepResearchJobRepository:
stmt = (
select(DeepResearchJobRow.id)
.where(
DeepResearchJobRow.cancel_requested.is_(False),
or_(
DeepResearchJobRow.status == "queued",
(DeepResearchJobRow.status == "running")
& DeepResearchJobRow.lease_until.is_not(None)
& (DeepResearchJobRow.lease_until < now),
)
),
)
.order_by(DeepResearchJobRow.updated_at.asc())
.limit(limit)
@@ -227,6 +260,7 @@ class DeepResearchJobRepository:
}
where = [
DeepResearchJobRow.id == job_id,
DeepResearchJobRow.cancel_requested.is_(False),
or_(
DeepResearchJobRow.status == "queued",
(DeepResearchJobRow.status == "running")
@@ -153,6 +153,16 @@ class DeepResearchSourceRepository:
if resolved is not None:
stmt = stmt.where(DeepResearchSourceRow.user_id == resolved)
source_rows = (await session.execute(stmt)).scalars().all()
if not source_rows:
# Collector persist marks rows selected, but an older session
# may only have unselected harvest rows. Cloning nothing would
# make 「选择结构再生成」 write with no materials.
stmt = select(DeepResearchSourceRow).where(
DeepResearchSourceRow.session_id == source_session_id,
)
if resolved is not None:
stmt = stmt.where(DeepResearchSourceRow.user_id == resolved)
source_rows = (await session.execute(stmt)).scalars().all()
id_map: dict[str, str] = {}
cloned_rows: list[DeepResearchSourceRow] = []