sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -26,6 +26,7 @@ from time import perf_counter
from typing import TYPE_CHECKING, Any, Literal, cast
from langgraph.checkpoint.base import empty_checkpoint
from langgraph.errors import GraphRecursionError
if TYPE_CHECKING:
from langchain_core.messages import HumanMessage
@@ -37,6 +38,7 @@ from deerflow.agents.middlewares.title_middleware import (
from deerflow.config.app_config import AppConfig
from deerflow.runtime.serialization import serialize
from deerflow.runtime.stream_bridge import StreamBridge
from deerflow.utils.background_init import run_background_init
from .manager import RunManager, RunRecord
from .schemas import RunStatus
@@ -50,10 +52,11 @@ _VALID_LG_MODES = {"values", "updates", "checkpoints", "tasks", "debug", "messag
# tools, research and subagents retain their own execution/cancellation policy.
_RUN_FINALIZATION_TIMEOUT_SECONDS = 10.0
_PENDING_COMPLETION_TASKS: set[asyncio.Future[Any]] = set()
_BACKGROUND_TITLE_TASKS: set[asyncio.Task[Any]] = set()
def _log_background_title_finalize_failure(thread_id: str):
def _done(task: asyncio.Task[bool]) -> None:
def _done(task: asyncio.Task[Any]) -> None:
_BACKGROUND_TITLE_TASKS.discard(task)
try:
task.result()
except Exception:
@@ -62,6 +65,83 @@ def _log_background_title_finalize_failure(thread_id: str):
return _done
async def _finalize_title_and_clear_provisional_marker(
*, checkpointer: Any, thread_store: Any, thread_id: str, source_state: dict[str, Any], app_config: AppConfig | None
) -> bool:
"""Finalize the title and always clear the cheap metadata polling marker."""
try:
return await finalize_thread_title(
checkpointer=checkpointer,
thread_store=thread_store,
thread_id=thread_id,
source_state=source_state,
app_config=app_config,
)
finally:
try:
await thread_store.update_metadata(
thread_id, {"_deerflow_title_provisional": False}, user_id=None
)
except Exception:
logger.debug("Failed to clear provisional-title marker for thread %s", thread_id, exc_info=True)
async def _sync_thread_title_after_run(
*,
checkpointer: Any,
thread_store: Any,
thread_id: str,
run_manager: RunManager,
record: RunRecord,
app_config: AppConfig | None,
) -> None:
"""Read title state and generate a final title outside run completion."""
try:
if not await _completion_is_current(run_manager, record):
return
ckpt_tuple = await checkpointer.aget_tuple(
{"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
)
if ckpt_tuple is None or not await _completion_is_current(run_manager, record):
return
checkpoint = getattr(ckpt_tuple, "checkpoint", {}) or {}
channel_values = (checkpoint.get("channel_values", {}) or {}) if isinstance(checkpoint, dict) else {}
title = channel_values.get("title")
if title:
await thread_store.update_display_name(thread_id, title)
provisional = channel_values.get("title_provisional") is True
if provisional:
await thread_store.update_metadata(
thread_id,
{"_deerflow_title_provisional": record.status == RunStatus.success},
)
if record.status == RunStatus.success and provisional:
log_title_debug(
"Run %s: scheduling background title generation for thread %s",
record.run_id,
thread_id,
)
await _finalize_title_and_clear_provisional_marker(
checkpointer=checkpointer,
thread_store=thread_store,
thread_id=thread_id,
source_state=copy.deepcopy(channel_values),
app_config=app_config,
)
elif record.status == RunStatus.success:
log_title_debug(
"Run %s: title generation not needed for thread %s (title_provisional=%r title=%r)",
record.run_id,
thread_id,
channel_values.get("title_provisional"),
title,
)
except asyncio.CancelledError:
raise
except Exception:
logger.debug("Failed to sync title for thread %s (non-fatal)", thread_id, exc_info=True)
def _build_runtime_context(
thread_id: str,
run_id: str,
@@ -139,8 +219,8 @@ def _agent_factory_supports_app_config(agent_factory: Any) -> bool:
def _build_agent_with_timing(agent_factory: Any, config: Any, app_config: AppConfig | None, run_id: str) -> Any:
# Preserve the original task/ContextVars/event loop. Moving this factory to
# a thread changes deferred-tool and MCP lifecycle behavior; measure first.
# Keep the synchronous contract for callers whose factories need their
# current event loop. The async wrapper only offloads opted-in factories.
started = perf_counter()
outcome = "error"
try:
@@ -159,6 +239,18 @@ def _build_agent_with_timing(agent_factory: Any, config: Any, app_config: AppCon
# Timing is diagnostic only; preserve the factory result/exception
# even if a deployment's custom log handler is unavailable.
pass
async def build_agent_with_timing(agent_factory: Any, config: Any, app_config: AppConfig | None, run_id: str) -> Any:
"""Build agents from async entry points with shared admission and context.
All async callers of an opted-in factory must use this path: a synchronous
build on the owner loop can block MCP discovery scheduled by another build.
"""
if getattr(agent_factory, "_deerflow_background_build", False) is not True:
return _build_agent_with_timing(agent_factory, config, app_config, run_id)
return await run_background_init(_build_agent_with_timing, agent_factory, config, app_config, run_id)
async def run_agent(
@@ -280,7 +372,9 @@ async def run_agent(
config.setdefault("callbacks", []).append(journal)
runnable_config = RunnableConfig(**config)
agent = _build_agent_with_timing(agent_factory, runnable_config, ctx.app_config, run_id)
agent = await build_agent_with_timing(agent_factory, runnable_config, ctx.app_config, run_id)
if record.abort_event.is_set():
raise asyncio.CancelledError
# 4. Attach checkpointer and store
if checkpointer is not None:
@@ -463,6 +557,23 @@ async def run_agent(
await _set_terminal_status(run_manager, record, RunStatus.interrupted)
logger.info("Run %s was cancelled", run_id)
except GraphRecursionError:
logger.info("Run %s reached its step budget", run_id)
try:
await _await_completion_io(
_finish_step_limited_run(agent, runnable_config, bridge, run_id),
run_id=run_id,
phase="step_limit_notice",
)
await _set_terminal_status(run_manager, record, RunStatus.interrupted)
except Exception as exc:
logger.exception("Could not save step-limit notice for run %s", run_id)
await _set_terminal_status(run_manager, record, RunStatus.error, error=str(exc))
await bridge.publish(run_id, "error", {
"message": "本次问答已达到步骤上限,但保存暂停提示失败,请刷新对话后重试。",
"name": "StepLimitSaveError",
})
except Exception as exc:
error_msg = f"{exc}"
logger.exception("Run %s failed: %s", run_id, error_msg)
@@ -501,6 +612,52 @@ async def run_agent(
asyncio.create_task(bridge.cleanup(run_id, delay=60))
async def _finish_step_limited_run(agent: Any, config: dict, bridge: StreamBridge, run_id: str) -> None:
"""Save a normal chat notice and close outstanding tool calls for the next turn.
Never rerun pending tools during recovery: they may have side effects.
New user input starts a fresh budget while keeping the existing transcript.
"""
from langchain_core.messages import AIMessage, ToolMessage
config = {**config, "configurable": dict(config.get("configurable", {}))}
# Recover from the run's latest checkpoint, even when its input selected
# an older checkpoint explicitly.
config["configurable"].pop("checkpoint_id", None)
snapshot = await agent.aget_state(config)
messages = snapshot.values.get("messages", [])
pending: dict[str, str] = {}
for message in messages:
if isinstance(message, AIMessage):
for call in message.tool_calls:
pending[call["id"]] = call["name"]
elif isinstance(message, ToolMessage):
pending.pop(message.tool_call_id, None)
additions: list = [
ToolMessage(
content="本次问答达到步骤上限,工具执行已暂停;此调用没有可用的完成结果。",
tool_call_id=call_id, name=name, status="error",
)
for call_id, name in pending.items()
]
notice = AIMessage(
id=f"step-limit-{run_id}",
content=(
"本次问答已达到步骤上限,已暂停处理。已有对话内容已保留。\n\n"
"如需接着完成,请发送“继续”;也可以补充要求,指定接下来优先处理的内容。"
),
additional_kwargs={"step_limit_reached": True},
)
additions.append(notice)
# Explicitly identify the model writer to avoid ambiguous last-writer
# inference when parallel tool nodes reached the budget together.
await agent.aupdate_state(config, {"messages": additions}, as_node="model" if "model" in agent.nodes else None)
await agent.aupdate_state(config, None, as_node="__end__")
final_state = await agent.aget_state(config)
await bridge.publish(run_id, "messages", serialize((notice, {"langgraph_node": "model"}), mode="messages"))
await bridge.publish(run_id, "values", serialize(final_state.values, mode="values"))
async def _await_completion_io[T](
operation: Awaitable[T],
*,
@@ -571,53 +728,8 @@ async def _finalize_run(*, journal: Any, run_manager: RunManager, record: RunRec
except Exception:
logger.warning("Failed to persist run completion for %s (non-fatal)", run_id, exc_info=True)
# Sync title from checkpoint to threads_meta.display_name
if checkpointer is not None and thread_store is not None:
try:
ckpt_config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
ckpt_tuple = await checkpointer.aget_tuple(ckpt_config)
if not await _completion_is_current(run_manager, record):
return
if ckpt_tuple is not None:
ckpt = getattr(ckpt_tuple, "checkpoint", {}) or {}
channel_values = ckpt.get("channel_values", {}) or {}
title = channel_values.get("title")
if title:
await thread_store.update_display_name(thread_id, title)
if not await _completion_is_current(run_manager, record):
return
if (
record.status == RunStatus.success
and channel_values.get("title_provisional") is True
):
log_title_debug(
"Run %s: scheduling background title generation for thread %s",
run_id,
thread_id,
)
title_task = asyncio.create_task(
finalize_thread_title(
checkpointer=checkpointer,
thread_store=thread_store,
thread_id=thread_id,
source_state=copy.deepcopy(channel_values),
app_config=ctx.app_config,
),
name=f"title-finalize:{thread_id}",
)
title_task.add_done_callback(_log_background_title_finalize_failure(thread_id))
elif record.status == RunStatus.success:
log_title_debug(
"Run %s: background title generation not scheduled for thread %s because title_provisional=%r title=%r",
run_id,
thread_id,
channel_values.get("title_provisional"),
channel_values.get("title"),
)
except Exception:
logger.debug("Failed to sync title for thread %s (non-fatal)", thread_id)
# Update threads_meta status based on run outcome
# Mark the thread idle before loading checkpoint/title data. Title refresh
# must never keep the composer locked after the answer stream has ended.
if thread_store is not None:
try:
if not await _completion_is_current(run_manager, record):
@@ -627,6 +739,21 @@ async def _finalize_run(*, journal: Any, run_manager: RunManager, record: RunRec
except Exception:
logger.debug("Failed to update thread_meta status for %s (non-fatal)", thread_id)
if checkpointer is not None and thread_store is not None:
title_task = asyncio.create_task(
_sync_thread_title_after_run(
checkpointer=checkpointer,
thread_store=thread_store,
thread_id=thread_id,
run_manager=run_manager,
record=record,
app_config=ctx.app_config,
),
name=f"title-sync:{thread_id}",
)
_BACKGROUND_TITLE_TASKS.add(title_task)
title_task.add_done_callback(_log_background_title_finalize_failure(thread_id))
def _check_completion_cancellation() -> None:
task = asyncio.current_task()
@@ -638,6 +765,8 @@ def _check_completion_cancellation() -> None:
async def _completion_is_current(run_manager: RunManager, record: RunRecord) -> bool:
_check_completion_cancellation()
if hasattr(run_manager, "is_latest"):
return run_manager.is_latest(record)
records = await run_manager.list_by_thread(record.thread_id)
_check_completion_cancellation()
return not records or records[-1].run_id == record.run_id