sync update from LAN repo (source ji @ 85078a31)

This commit is contained in:
gozeros committed 2026-10-21 15:09:16 +08:00
1 parent 5d6627af50
commit e602d88695
864 files changed
+99150 -6346

No files matched your search

@@ -13,6 +13,7 @@ don't carry those ids around.
from __future__ import annotations
import asyncio
import json
import logging
from collections.abc import Awaitable, Callable
@@ -20,7 +21,7 @@ from contextlib import suppress
from datetime import date, datetime
from enum import Enum
from time import monotonic
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, TypeVar
from deerflow.agents.deep_research.events import (
DeepResearchEvent,
@@ -33,6 +34,33 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
_EVENT_PERSIST_TIMEOUT_SECONDS = 2.0
_T = TypeVar("_T")
async def _await_or_abandon(awaitable: Awaitable[_T], timeout: float) -> _T:
"""Return the result, or raise TimeoutError without waiting out a hung DB call.
``asyncio.wait_for`` cancels the inner task and then *awaits that cancel*.
On the production MySQL this second wait is what freezes writing after the
first live frame: the query never actually cancels, so the writer never
resumes. Abandon the task instead and continue live-only.
"""
task = asyncio.ensure_future(awaitable)
done, _ = await asyncio.wait({task}, timeout=timeout)
if task in done:
return task.result()
def _log_abandoned(done_task: asyncio.Task[_T]) -> None:
if done_task.cancelled():
return
exc = done_task.exception()
if exc is not None:
logger.warning("abandoned deep-research awaitable failed later: %s", exc)
task.add_done_callback(_log_abandoned)
raise TimeoutError(f"operation exceeded {timeout:g} seconds")
def _json_default(value: Any) -> Any:
if isinstance(value, (datetime, date)):
@@ -115,12 +143,15 @@ class PersistingEventSink:
# instead of leaving it buffered until the job ends.
await self._flush_live_mirrors()
try:
row = await self._repo.append(
session_id=self._session_id,
job_id=self._job_id,
event_type=type,
phase=phase,
payload=event_payload,
row = await _await_or_abandon(
self._repo.append(
session_id=self._session_id,
job_id=self._job_id,
event_type=type,
phase=phase,
payload=event_payload,
),
_EVENT_PERSIST_TIMEOUT_SECONDS,
)
# Reconstruct the envelope from the persisted row (seq is now assigned).
event = DeepResearchEvent(
@@ -238,8 +269,8 @@ class PersistingEventSink:
type,
exc_info=True,
)
with suppress(Exception):
await self._mirror_live_frame(type, phase=phase, payload=payload or {})
# Do not persist live frames. Mid-write event-table IO is what froze
# the production writer; the completed report is stored afterwards.
# ── cross-worker mirror for live-only reasoning frames ──────────────────
@@ -330,8 +330,13 @@ class DeerFlowCompletionBackend:
durable runner either uses its safe fallback or records a terminal error.
"""
timeout = self._config.control_plane_timeout_seconds
isolated_config = _parent_isolated_run_config(f"deep_research:{operation}")
reset_parent_config = _clear_inherited_runnable_config(isolated_config)
try:
return await asyncio.wait_for(model.ainvoke(messages, **kwargs), timeout=timeout)
return await asyncio.wait_for(
model.ainvoke(messages, config=isolated_config, **kwargs),
timeout=timeout,
)
except TimeoutError as exc:
if self._cancel is not None:
self._cancel.raise_if_cancelled()
@@ -340,6 +345,8 @@ class DeerFlowCompletionBackend:
code="completion_timeout",
retryable=True,
) from exc
finally:
reset_parent_config()
async def stream_complete(
self,