sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -4,8 +4,10 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from deerflow.utils.time import now_iso as _now_iso
|
||||
@@ -36,6 +38,7 @@ class RunRecord:
|
||||
abort_event: asyncio.Event = field(default_factory=asyncio.Event, repr=False)
|
||||
abort_action: str = "interrupt"
|
||||
error: str | None = None
|
||||
superseded: bool = field(default=False, repr=False)
|
||||
|
||||
|
||||
class RunManager:
|
||||
@@ -50,6 +53,8 @@ class RunManager:
|
||||
self._runs: dict[str, RunRecord] = {}
|
||||
self._lock = asyncio.Lock()
|
||||
self._store = store
|
||||
self._finished_at: dict[str, float] = {}
|
||||
self._retention_task: asyncio.Task | None = None
|
||||
|
||||
async def _persist_to_store(self, record: RunRecord) -> None:
|
||||
"""Best-effort persist run record to backing store."""
|
||||
@@ -103,6 +108,9 @@ class RunManager:
|
||||
updated_at=now,
|
||||
)
|
||||
async with self._lock:
|
||||
for previous in self._runs.values():
|
||||
if previous.thread_id == thread_id:
|
||||
previous.superseded = True
|
||||
self._runs[run_id] = record
|
||||
await self._persist_to_store(record)
|
||||
logger.info("Run created: run_id=%s thread_id=%s", run_id, thread_id)
|
||||
@@ -138,7 +146,7 @@ class RunManager:
|
||||
error=data.get("error"),
|
||||
)
|
||||
|
||||
async def get_persisted(self, run_id: str) -> RunRecord | None:
|
||||
async def get_persisted(self, run_id: str, *, authorized_thread_id: str | None = None) -> RunRecord | None:
|
||||
"""Load a run from the backing store (process-restart fallback).
|
||||
|
||||
The in-memory registry is wiped on every Gateway restart, but the
|
||||
@@ -150,20 +158,36 @@ class RunManager:
|
||||
if self._store is None:
|
||||
return None
|
||||
try:
|
||||
data = await self._store.get(run_id)
|
||||
# Only an already-authorized thread route may bypass row ownership.
|
||||
# This keeps task-shared runs readable after retiring their RAM copy.
|
||||
data = await self._store.get(run_id, user_id=None) if authorized_thread_id else await self._store.get(run_id)
|
||||
except Exception:
|
||||
logger.warning("Failed to load run %s from store", run_id, exc_info=True)
|
||||
return None
|
||||
if not data:
|
||||
return None
|
||||
if authorized_thread_id is not None and data.get("thread_id") != authorized_thread_id:
|
||||
return None
|
||||
return self._record_from_store(data)
|
||||
|
||||
async def list_by_thread(self, thread_id: str) -> list[RunRecord]:
|
||||
"""Return all runs for a given thread, newest first."""
|
||||
"""Return live and stored runs in the original creation order."""
|
||||
async with self._lock:
|
||||
# Dict insertion order matches creation order, so reversing it gives
|
||||
# us deterministic newest-first results even when timestamps tie.
|
||||
return [r for r in self._runs.values() if r.thread_id == thread_id]
|
||||
# Keep live object identity when the same run also exists in SQL.
|
||||
records = {r.run_id: r for r in self._runs.values() if r.thread_id == thread_id}
|
||||
if self._store is not None:
|
||||
try:
|
||||
# Callers authorize the thread, as for the original RAM registry.
|
||||
for row in await self._store.list_by_thread(thread_id, user_id=None, limit=1000):
|
||||
records.setdefault(row["run_id"], self._record_from_store(row))
|
||||
except Exception:
|
||||
logger.warning("Failed to list persisted runs for thread %s", thread_id, exc_info=True)
|
||||
return sorted(records.values(), key=lambda record: datetime.fromisoformat(record.created_at.replace("Z", "+00:00")).timestamp())
|
||||
|
||||
def is_latest(self, record: RunRecord) -> bool:
|
||||
"""Completion fencing stays in RAM; it must not wait on history IO."""
|
||||
latest = next((item for item in reversed(self._runs.values()) if item.thread_id == record.thread_id), None)
|
||||
return not record.superseded and latest is record
|
||||
|
||||
async def set_status(self, run_id: str, status: RunStatus, *, error: str | None = None) -> None:
|
||||
"""Transition a run to a new status."""
|
||||
@@ -176,6 +200,8 @@ class RunManager:
|
||||
record.updated_at = _now_iso()
|
||||
if error is not None:
|
||||
record.error = error
|
||||
if status not in (RunStatus.pending, RunStatus.running):
|
||||
self._finished_at[run_id] = time.monotonic()
|
||||
if self._store is not None:
|
||||
try:
|
||||
await self._store.update_status(run_id, status.value, error=error)
|
||||
@@ -290,6 +316,9 @@ class RunManager:
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
for previous in self._runs.values():
|
||||
if previous.thread_id == thread_id:
|
||||
previous.superseded = True
|
||||
self._runs[run_id] = record
|
||||
|
||||
await self._persist_to_store(record)
|
||||
@@ -316,13 +345,75 @@ class RunManager:
|
||||
return [r for r in self._runs.values() if r.status in (RunStatus.pending, RunStatus.running)]
|
||||
|
||||
async def cleanup(self, run_id: str, *, delay: float = 300) -> None:
|
||||
"""Remove a run record after an optional delay."""
|
||||
"""Retire only a stopped run whose terminal snapshot is safely stored."""
|
||||
if delay > 0:
|
||||
await asyncio.sleep(delay)
|
||||
record = self._runs.get(run_id)
|
||||
if record is None:
|
||||
self._finished_at.pop(run_id, None)
|
||||
return
|
||||
if self._store is None or record.status in (RunStatus.pending, RunStatus.running):
|
||||
return
|
||||
if record.task is not None and not record.task.done():
|
||||
return
|
||||
try:
|
||||
saved = await self._store.get(run_id, user_id=None)
|
||||
except Exception:
|
||||
logger.warning("Retaining run %s: durable lookup unavailable", run_id, exc_info=True)
|
||||
return
|
||||
if not saved or saved.get("thread_id") != record.thread_id or saved.get("status") != record.status.value or saved.get("error") != record.error:
|
||||
return
|
||||
# Never evict a user input that was not successfully persisted.
|
||||
if saved.get("kwargs", {}) != record.kwargs or saved.get("metadata", {}) != record.metadata:
|
||||
try:
|
||||
if not await self._store.update_snapshot(run_id, metadata=record.metadata, kwargs=record.kwargs):
|
||||
return
|
||||
saved = await self._store.get(run_id, user_id=None)
|
||||
except Exception:
|
||||
logger.warning("Retaining run %s: final snapshot could not be stored", run_id, exc_info=True)
|
||||
return
|
||||
if not saved or saved.get("kwargs", {}) != record.kwargs or saved.get("metadata", {}) != record.metadata:
|
||||
return
|
||||
async with self._lock:
|
||||
self._runs.pop(run_id, None)
|
||||
if self._runs.get(run_id) is record and (record.task is None or record.task.done()):
|
||||
self._runs.pop(run_id, None)
|
||||
self._finished_at.pop(run_id, None)
|
||||
logger.debug("Run record %s cleaned up", run_id)
|
||||
|
||||
def watch_completion(self, record: RunRecord) -> None:
|
||||
def finished(_):
|
||||
if self._runs.get(record.run_id) is record:
|
||||
self._finished_at[record.run_id] = time.monotonic()
|
||||
|
||||
if record.task is not None:
|
||||
record.task.add_done_callback(finished)
|
||||
|
||||
async def reap_completed(self, retention_seconds: float = 300) -> None:
|
||||
cutoff = time.monotonic() - retention_seconds
|
||||
for run_id in [key for key, finished in self._finished_at.items() if finished <= cutoff][:200]:
|
||||
await self.cleanup(run_id, delay=0)
|
||||
if run_id in self._finished_at:
|
||||
self._finished_at[run_id] = time.monotonic()
|
||||
|
||||
def start_retention(self, *, retention_seconds=300, interval_seconds=10) -> None:
|
||||
if self._retention_task is not None and not self._retention_task.done():
|
||||
return
|
||||
|
||||
async def sweep():
|
||||
while True:
|
||||
await asyncio.sleep(interval_seconds)
|
||||
try:
|
||||
await self.reap_completed(retention_seconds)
|
||||
except Exception:
|
||||
logger.exception("Completed run retention failed; retaining in-memory records")
|
||||
|
||||
self._retention_task = asyncio.create_task(sweep(), name="completed-run-retention")
|
||||
|
||||
async def aclose(self) -> None:
|
||||
if self._retention_task is not None:
|
||||
self._retention_task.cancel()
|
||||
await asyncio.gather(self._retention_task, return_exceptions=True)
|
||||
|
||||
|
||||
class ConflictError(Exception):
|
||||
"""Raised when multitask_strategy=reject and thread has inflight runs."""
|
||||
|
||||
+22
-3
@@ -11,10 +11,15 @@ When user_id is None, no user filtering is applied (single-user mode).
|
||||
from __future__ import annotations
|
||||
|
||||
import abc
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
|
||||
class RunStore(abc.ABC):
|
||||
async def update_snapshot(self, run_id: str, *, metadata: dict, kwargs: dict) -> bool:
|
||||
"""Persist late metadata before retiring the live registry copy."""
|
||||
return False
|
||||
|
||||
@abc.abstractmethod
|
||||
async def put(
|
||||
self,
|
||||
@@ -33,7 +38,7 @@ class RunStore(abc.ABC):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
async def get(self, run_id: str) -> dict[str, Any] | None:
|
||||
async def get(self, run_id: str, *, user_id=None) -> dict[str, Any] | None:
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
@@ -94,11 +99,25 @@ class RunStore(abc.ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
async def count_by_user(self) -> dict[str, int]:
|
||||
"""Return ``{user_id: run_count}`` across all runs.
|
||||
async def count_by_user(self, *, since: datetime | None = None) -> dict[str, int]:
|
||||
"""Return ``{user_id: run_count}``, optionally from ``since`` onward.
|
||||
|
||||
One run corresponds to one user submission, so this backs the admin
|
||||
user center's 问答次数 (Q&A count) column — each question counts once.
|
||||
Non-overriding stores return an empty mapping (treated as 0 per user).
|
||||
"""
|
||||
return {}
|
||||
|
||||
async def purge_terminal_before(
|
||||
self,
|
||||
cutoff: datetime,
|
||||
*,
|
||||
batch_size: int = 500,
|
||||
) -> int:
|
||||
"""Delete one bounded batch of expired terminal run summaries.
|
||||
|
||||
A run summary is operational audit metadata (including the compact
|
||||
first-question / last-answer fields), not the user's conversation
|
||||
checkpoint. The default keeps memory and alternate stores compatible.
|
||||
"""
|
||||
return 0
|
||||
+7
-1
@@ -44,8 +44,14 @@ class MemoryRunStore(RunStore):
|
||||
"updated_at": now,
|
||||
}
|
||||
|
||||
async def get(self, run_id):
|
||||
async def get(self, run_id, *, user_id=None):
|
||||
return self._runs.get(run_id)
|
||||
|
||||
async def update_snapshot(self, run_id, *, metadata, kwargs):
|
||||
if run_id not in self._runs:
|
||||
return False
|
||||
self._runs[run_id].update(metadata=metadata, kwargs=kwargs)
|
||||
return True
|
||||
|
||||
async def list_by_thread(self, thread_id, *, user_id=None, limit=100):
|
||||
results = [r for r in self._runs.values() if r["thread_id"] == thread_id and (user_id is None or r.get("user_id") == user_id)]
|
||||
|
||||
+181
-52
@@ -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
|
||||
|
||||
Reference in new issue
Block a user