sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
No files matched your search
@@ -14,7 +14,7 @@ import os
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Awaitable, Callable, Mapping
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException, Request
|
||||
@@ -25,6 +25,7 @@ from app.gateway.llmwiki_rag import (
|
||||
LlmWikiRagContext,
|
||||
attach_llmwiki_rag_to_graph_input,
|
||||
build_llmwiki_rag_context,
|
||||
llmwiki_selection_declared,
|
||||
llmwiki_selection_requested,
|
||||
)
|
||||
from app.gateway.utils import sanitize_log_param
|
||||
@@ -54,10 +55,7 @@ RUN_SCOPE_BACKGROUND_CHILD = "background_child"
|
||||
_INTERNAL_RUN_SCOPES = {RUN_SCOPE_ORCHESTRATION_CHILD, RUN_SCOPE_BACKGROUND_CHILD}
|
||||
_ALL_RUN_SCOPES = {RUN_SCOPE_EXTERNAL_ROOT, *_INTERNAL_RUN_SCOPES}
|
||||
_INTERNAL_RUN_SCOPE_HEADER = "x-deerflow-internal-run-scope-token"
|
||||
_INTERNAL_RUN_SCOPE_TOKEN = (
|
||||
os.getenv("DEERFLOW_INTERNAL_RUN_SCOPE_TOKEN", "").strip()
|
||||
or uuid.uuid4().hex
|
||||
)
|
||||
_INTERNAL_RUN_SCOPE_TOKEN = os.getenv("DEERFLOW_INTERNAL_RUN_SCOPE_TOKEN", "").strip() or uuid.uuid4().hex
|
||||
|
||||
|
||||
def internal_run_scope_headers() -> dict[str, str]:
|
||||
@@ -94,11 +92,7 @@ def _resolve_run_scope(body: Any, request: Request, run_scope: str | None) -> st
|
||||
return scope if scope in _ALL_RUN_SCOPES else RUN_SCOPE_EXTERNAL_ROOT
|
||||
|
||||
scope = _metadata_run_scope(body)
|
||||
if (
|
||||
scope in _INTERNAL_RUN_SCOPES
|
||||
and _request_is_loopback(request)
|
||||
and _has_internal_run_scope_header(request)
|
||||
):
|
||||
if scope in _INTERNAL_RUN_SCOPES and _request_is_loopback(request) and _has_internal_run_scope_header(request):
|
||||
return scope
|
||||
if scope == RUN_SCOPE_EXTERNAL_ROOT:
|
||||
return RUN_SCOPE_EXTERNAL_ROOT
|
||||
@@ -112,6 +106,7 @@ async def _run_agent_with_concurrency_lease(
|
||||
lease_metadata: dict[str, Any],
|
||||
heartbeat_interval_seconds: int,
|
||||
run_agent_kwargs: dict[str, Any],
|
||||
after_success: Callable[[], Awaitable[None]] | None = None,
|
||||
) -> None:
|
||||
"""Run the agent while refreshing and finally releasing an optional lease."""
|
||||
|
||||
@@ -133,6 +128,15 @@ async def _run_agent_with_concurrency_lease(
|
||||
|
||||
try:
|
||||
await run_agent(**run_agent_kwargs)
|
||||
record = run_agent_kwargs.get("record")
|
||||
if after_success is not None and getattr(record, "status", None) == RunStatus.success:
|
||||
try:
|
||||
await after_success()
|
||||
except Exception: # noqa: BLE001 - memory extraction must never fail chat
|
||||
logger.exception(
|
||||
"Post-run success callback failed for run %s (non-fatal)",
|
||||
lease_metadata.get("run_id"),
|
||||
)
|
||||
finally:
|
||||
if heartbeat_task is not None:
|
||||
heartbeat_task.cancel()
|
||||
@@ -214,7 +218,19 @@ def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
|
||||
msg_kwargs: dict[str, Any] = {"content": content}
|
||||
extra = msg.get("additional_kwargs")
|
||||
if isinstance(extra, dict) and extra:
|
||||
msg_kwargs["additional_kwargs"] = extra
|
||||
# Retrieval evidence and traces are server-authored. If
|
||||
# accepted from a client, a user could forge DevTools/
|
||||
# reference-batches diagnostics or inject fake citations.
|
||||
server_retrieval_keys = {
|
||||
"knowledge_retrieval_trace",
|
||||
"llmwiki_reference_sources",
|
||||
"llmwiki_reference_query",
|
||||
"llmwiki_knowledge_base_ids",
|
||||
"assistant_knowledge_base_ids",
|
||||
}
|
||||
safe_extra = {key: value for key, value in extra.items() if key not in server_retrieval_keys}
|
||||
if safe_extra:
|
||||
msg_kwargs["additional_kwargs"] = safe_extra
|
||||
if msg.get("id"):
|
||||
msg_kwargs["id"] = msg["id"]
|
||||
if msg.get("name"):
|
||||
@@ -243,10 +259,7 @@ def _plain_fixed_question(graph_input: dict[str, Any]) -> str | None:
|
||||
if not isinstance(message, HumanMessage):
|
||||
return None
|
||||
additional_kwargs = getattr(message, "additional_kwargs", {}) or {}
|
||||
if any(
|
||||
additional_kwargs.get(key)
|
||||
for key in ("files", "uploaded_files", "attachments", "prompt_prefix")
|
||||
):
|
||||
if any(additional_kwargs.get(key) for key in ("files", "uploaded_files", "attachments", "prompt_prefix")):
|
||||
return None
|
||||
content = getattr(message, "content", None)
|
||||
if isinstance(content, str):
|
||||
@@ -351,9 +364,7 @@ def _prompt_prefix_opted_in(body: Any) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _rag_mode_requested(body: Any) -> bool:
|
||||
if llmwiki_selection_requested(body):
|
||||
return True
|
||||
def _explicit_rag_mode_requested(body: Any) -> bool:
|
||||
context = getattr(body, "context", None)
|
||||
if isinstance(context, Mapping) and context.get("rag_mode_enabled"):
|
||||
return True
|
||||
@@ -363,6 +374,32 @@ def _rag_mode_requested(body: Any) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _rag_mode_requested(body: Any) -> bool:
|
||||
return llmwiki_selection_requested(body) or _explicit_rag_mode_requested(body)
|
||||
|
||||
|
||||
def _effective_rag_mode_after_llmwiki(
|
||||
*,
|
||||
body: Any,
|
||||
rag: LlmWikiRagContext,
|
||||
inherited: bool,
|
||||
) -> bool:
|
||||
"""Resolve RAG mode without treating an observability trace as evidence.
|
||||
|
||||
A disabled trace is deliberately retained when the caller explicitly
|
||||
clears both knowledge-space selections. That trace must not reactivate a
|
||||
thread's citation prompt or persist ``rag_mode=true`` again. An explicit
|
||||
generic RAG flag still wins, while any resolved ordinary/assistant space
|
||||
enables the strict knowledge mode.
|
||||
"""
|
||||
|
||||
if rag.knowledge_base_ids or rag.assistant_knowledge_base_ids:
|
||||
return True
|
||||
if llmwiki_selection_declared(body) and not _explicit_rag_mode_requested(body):
|
||||
return False
|
||||
return inherited
|
||||
|
||||
|
||||
def _thread_rag_mode_enabled(record: Any) -> bool:
|
||||
if not isinstance(record, Mapping):
|
||||
return False
|
||||
@@ -406,8 +443,16 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
"rag_mode_enabled",
|
||||
"llmwiki_knowledge_base_ids",
|
||||
"assistant_knowledge_base_ids",
|
||||
"knowledge_space_strict",
|
||||
"writing_mode",
|
||||
"writing_artifact_path",
|
||||
# 深度研究工作台「开始撰写」:走写作模式内联管线,不建 job。
|
||||
"workbench_write_now",
|
||||
"workbench_session_id",
|
||||
"writing_report_config",
|
||||
"force_no_materials",
|
||||
# 收集员当场指定的检索技能(报告结构「检索来源」)。
|
||||
"extra_skills",
|
||||
"canvas_artifact_title",
|
||||
"canvas_artifact_type",
|
||||
"is_plan_mode",
|
||||
@@ -420,6 +465,7 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
"is_bootstrap",
|
||||
"is_writing_setup",
|
||||
"is_report_structure_setup",
|
||||
"is_gongwen_template_setup",
|
||||
"valid_article_types",
|
||||
"valid_retrieval_skills",
|
||||
"writing_setup_quick",
|
||||
@@ -447,6 +493,116 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
|
||||
}
|
||||
)
|
||||
|
||||
_KNOWLEDGE_SPACE_EXTERNAL_RETRIEVAL_TOOLS: frozenset[str] = frozenset(
|
||||
{
|
||||
"web_search",
|
||||
"web_fetch",
|
||||
"image_search",
|
||||
"browser_fetch_page",
|
||||
"browser_act",
|
||||
"knowledge_search",
|
||||
"notebook_search",
|
||||
"tool_search",
|
||||
"invoke_acp_agent",
|
||||
"agent_orchestration",
|
||||
"task",
|
||||
"deep_research_report",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def knowledge_space_strict_excluded_tools(
|
||||
existing: list[str] | None,
|
||||
*,
|
||||
app_config: Any | None = None,
|
||||
) -> list[str]:
|
||||
"""Merge fail-closed external retrieval exclusions for selected spaces."""
|
||||
|
||||
blocked = set(_KNOWLEDGE_SPACE_EXTERNAL_RETRIEVAL_TOOLS)
|
||||
resolved = app_config or get_app_config()
|
||||
for tool in getattr(resolved, "tools", []) or []:
|
||||
if str(getattr(tool, "group", "") or "").strip().lower() in {"web", "browser"}:
|
||||
blocked.add(str(getattr(tool, "name", "") or "").strip())
|
||||
return list(dict.fromkeys([*(existing or []), *(name for name in sorted(blocked) if name)]))
|
||||
|
||||
|
||||
def apply_knowledge_space_strict_scope(
|
||||
context: dict[str, Any],
|
||||
*,
|
||||
selection_requested: bool,
|
||||
knowledge_base_ids: list[str],
|
||||
assistant_knowledge_base_ids: list[str],
|
||||
app_config: Any | None = None,
|
||||
) -> bool:
|
||||
"""Fail closed whenever a run declares a selected knowledge space.
|
||||
|
||||
``selection_requested`` remains authoritative even when retrieval returns no
|
||||
authorized ids (for example, while an upstream index is unavailable). A
|
||||
failed lookup must never silently reopen Web/browser/MCP-style retrieval.
|
||||
"""
|
||||
|
||||
if not (
|
||||
selection_requested
|
||||
or knowledge_base_ids
|
||||
or assistant_knowledge_base_ids
|
||||
):
|
||||
return False
|
||||
raw_excluded = context.get("excluded_tools")
|
||||
if isinstance(raw_excluded, str):
|
||||
excluded = [raw_excluded] if raw_excluded.strip() else []
|
||||
elif isinstance(raw_excluded, (list, tuple, set)):
|
||||
excluded = [str(item) for item in raw_excluded if str(item).strip()]
|
||||
else:
|
||||
excluded = []
|
||||
context["knowledge_space_strict"] = True
|
||||
context["excluded_tools"] = knowledge_space_strict_excluded_tools(
|
||||
excluded,
|
||||
app_config=app_config,
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def enforce_knowledge_space_strict_run_config(
|
||||
config: dict[str, Any],
|
||||
*,
|
||||
knowledge_base_ids: list[str],
|
||||
assistant_knowledge_base_ids: list[str],
|
||||
excluded_tools: list[str],
|
||||
app_config: Any | None = None,
|
||||
) -> None:
|
||||
"""Overwrite both runtime scopes with the server-validated selection.
|
||||
|
||||
``merge_run_context_overrides`` intentionally uses ``setdefault`` for
|
||||
ordinary client preferences. Security policy is different: a caller must
|
||||
not pre-seed ``knowledge_space_strict=false``, alternate ids, or an empty
|
||||
exclusion list in ``config.context``/``configurable`` to reopen retrieval.
|
||||
"""
|
||||
|
||||
for scope in ("configurable", "context"):
|
||||
target = config.setdefault(scope, {})
|
||||
if not isinstance(target, dict):
|
||||
continue
|
||||
raw_target_excluded = target.get("excluded_tools")
|
||||
if isinstance(raw_target_excluded, str):
|
||||
target_excluded = (
|
||||
[raw_target_excluded] if raw_target_excluded.strip() else []
|
||||
)
|
||||
elif isinstance(raw_target_excluded, (list, tuple, set)):
|
||||
target_excluded = [
|
||||
str(item) for item in raw_target_excluded if str(item).strip()
|
||||
]
|
||||
else:
|
||||
target_excluded = []
|
||||
target["knowledge_space_strict"] = True
|
||||
target["llmwiki_knowledge_base_ids"] = list(knowledge_base_ids)
|
||||
target["assistant_knowledge_base_ids"] = list(
|
||||
assistant_knowledge_base_ids
|
||||
)
|
||||
target["excluded_tools"] = knowledge_space_strict_excluded_tools(
|
||||
[*target_excluded, *excluded_tools],
|
||||
app_config=app_config,
|
||||
)
|
||||
|
||||
|
||||
def merge_run_context_overrides(config: dict[str, Any], context: Mapping[str, Any] | None) -> None:
|
||||
"""Merge whitelisted keys from ``body.context`` into both ``config['configurable']``
|
||||
@@ -465,6 +621,56 @@ def merge_run_context_overrides(config: dict[str, Any], context: Mapping[str, An
|
||||
runtime_context.setdefault(key, context[key])
|
||||
|
||||
|
||||
def inject_validated_analysis_project_context(config: dict[str, Any], context: Mapping[str, Any] | None) -> None:
|
||||
"""Inject server-derived project policy outside the public context whitelist."""
|
||||
if not context or not isinstance(context.get("analysis_project"), dict):
|
||||
return
|
||||
project_policy = {
|
||||
"analysis_project": context["analysis_project"],
|
||||
"solution_requirements_confirmed": bool(context.get("solution_requirements_confirmed")),
|
||||
"solution_force_generate": bool(context.get("solution_force_generate")),
|
||||
"solution_intent": context.get("solution_intent") if isinstance(context.get("solution_intent"), dict) else None,
|
||||
}
|
||||
project_memory_scope = context.get("project_memory_scope")
|
||||
if isinstance(project_memory_scope, dict):
|
||||
project_policy.update(
|
||||
{
|
||||
"project_memory_scope": project_memory_scope,
|
||||
"project_memory": context.get("project_memory")
|
||||
if isinstance(context.get("project_memory"), dict)
|
||||
else None,
|
||||
# Complete isolation from the legacy/global memory path.
|
||||
"memory_recall_disabled": True,
|
||||
"memory_injection_enabled": False,
|
||||
}
|
||||
)
|
||||
for scope in ("configurable", "context"):
|
||||
target = config.setdefault(scope, {})
|
||||
if isinstance(target, dict):
|
||||
# Assignment is intentional: these values were derived from the
|
||||
# immutable project binding/checkpoint and override request config.
|
||||
target.update(project_policy)
|
||||
if isinstance(project_memory_scope, dict):
|
||||
raw_excluded = target.get("excluded_tools")
|
||||
if isinstance(raw_excluded, str):
|
||||
excluded = [raw_excluded] if raw_excluded.strip() else []
|
||||
elif isinstance(raw_excluded, (list, tuple, set)):
|
||||
excluded = [str(item) for item in raw_excluded if str(item).strip()]
|
||||
else:
|
||||
excluded = []
|
||||
target["excluded_tools"] = list(
|
||||
dict.fromkeys(
|
||||
[
|
||||
*excluded,
|
||||
"memory",
|
||||
"hindsight_recall",
|
||||
"hindsight_reflect",
|
||||
"hindsight_retain",
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def resolve_agent_factory(assistant_id: str | None):
|
||||
"""Resolve the agent factory callable from config.
|
||||
|
||||
@@ -897,6 +1103,38 @@ async def _enforce_agent_access(body: Any, request: Request) -> None:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _fail_unstarted_run(
|
||||
*,
|
||||
run_mgr: RunManager,
|
||||
run_ctx: Any,
|
||||
record: RunRecord,
|
||||
thread_id: str,
|
||||
concurrency_gate: Any | None,
|
||||
redis_lease_acquired: bool,
|
||||
lease_metadata: dict[str, Any],
|
||||
exc: BaseException,
|
||||
) -> None:
|
||||
"""Release a run that failed after registration but before task launch."""
|
||||
|
||||
error_message = f"{type(exc).__name__}: {exc}"[:1000]
|
||||
try:
|
||||
# Marking the record terminal removes it from create_or_reject's
|
||||
# in-flight set and persists the outcome even though no worker/finally
|
||||
# block will run for this pre-launch failure.
|
||||
await run_mgr.set_status(record.run_id, RunStatus.error, error=error_message)
|
||||
except Exception:
|
||||
logger.warning("Failed to close unstarted run %s", record.run_id, exc_info=True)
|
||||
try:
|
||||
await run_ctx.thread_store.update_status(thread_id, "error")
|
||||
except Exception:
|
||||
logger.debug("Failed to mark thread %s after run preparation error", sanitize_log_param(thread_id))
|
||||
if concurrency_gate is not None and redis_lease_acquired:
|
||||
try:
|
||||
await concurrency_gate.release(**lease_metadata)
|
||||
except Exception:
|
||||
logger.warning("Failed to release concurrency lease for unstarted run %s", record.run_id, exc_info=True)
|
||||
|
||||
|
||||
async def start_run(
|
||||
body: Any,
|
||||
thread_id: str,
|
||||
@@ -928,6 +1166,9 @@ async def start_run(
|
||||
router) and ``[leader-timing] / [special-timing]`` (圆桌 leader/special).
|
||||
"""
|
||||
t_entry = time.perf_counter()
|
||||
from app.gateway.analysis_projects import prepare_project_run
|
||||
|
||||
body, analysis_project = await prepare_project_run(body, thread_id, request)
|
||||
bridge = get_stream_bridge(request)
|
||||
run_mgr = get_run_manager(request)
|
||||
run_ctx = get_run_context(request)
|
||||
@@ -948,11 +1189,15 @@ async def start_run(
|
||||
effective_user_id = get_effective_user_id()
|
||||
effective_run_scope = _resolve_run_scope(body, request, run_scope)
|
||||
graph_input = normalize_input(body.input)
|
||||
fixed_answer = await _find_fixed_answer(
|
||||
body=body,
|
||||
request=request,
|
||||
graph_input=graph_input,
|
||||
run_scope=effective_run_scope,
|
||||
fixed_answer = (
|
||||
None
|
||||
if analysis_project
|
||||
else await _find_fixed_answer(
|
||||
body=body,
|
||||
request=request,
|
||||
graph_input=graph_input,
|
||||
run_scope=effective_run_scope,
|
||||
)
|
||||
)
|
||||
if fixed_answer is not None:
|
||||
effective_model_name = "fixed-answer"
|
||||
@@ -961,11 +1206,7 @@ async def start_run(
|
||||
else:
|
||||
effective_model_name = _resolve_effective_run_model_name(body)
|
||||
model_concurrency_limit = _get_model_concurrency_limit(effective_model_name)
|
||||
user_model_concurrency_limit = (
|
||||
_get_user_model_concurrency_limit(effective_model_name)
|
||||
if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT
|
||||
else None
|
||||
)
|
||||
user_model_concurrency_limit = _get_user_model_concurrency_limit(effective_model_name) if effective_run_scope == RUN_SCOPE_EXTERNAL_ROOT else None
|
||||
run_id = str(uuid.uuid4())
|
||||
|
||||
run_metadata = dict(body.metadata or {})
|
||||
@@ -1056,8 +1297,11 @@ async def start_run(
|
||||
# (e.g. stateless runs).
|
||||
requested_rag_mode = _rag_mode_requested(body)
|
||||
effective_rag_mode = requested_rag_mode
|
||||
existing_thread_metadata: dict[str, Any] = {}
|
||||
try:
|
||||
existing = await run_ctx.thread_store.get(thread_id)
|
||||
if existing is not None and isinstance(existing.get("metadata"), Mapping):
|
||||
existing_thread_metadata = dict(existing["metadata"])
|
||||
effective_rag_mode = requested_rag_mode or _thread_rag_mode_enabled(existing)
|
||||
if existing is None:
|
||||
metadata = dict(body.metadata or {})
|
||||
@@ -1099,9 +1343,17 @@ async def start_run(
|
||||
)
|
||||
else:
|
||||
agent_factory = resolve_agent_factory(body.assistant_id)
|
||||
except Exception:
|
||||
if concurrency_gate is not None and redis_lease_acquired:
|
||||
await concurrency_gate.release(**lease_metadata)
|
||||
except Exception as exc:
|
||||
await _fail_unstarted_run(
|
||||
run_mgr=run_mgr,
|
||||
run_ctx=run_ctx,
|
||||
record=record,
|
||||
thread_id=thread_id,
|
||||
concurrency_gate=concurrency_gate,
|
||||
redis_lease_acquired=redis_lease_acquired,
|
||||
lease_metadata=lease_metadata,
|
||||
exc=exc,
|
||||
)
|
||||
raise
|
||||
|
||||
# Admin-configurable prompt prefix: when the caller opted in via
|
||||
@@ -1112,40 +1364,88 @@ async def start_run(
|
||||
# applies once. We intentionally do NOT gate on thread-row existence: the
|
||||
# thread_meta row is frequently pre-created, which previously skipped the
|
||||
# prefix for genuine first turns.
|
||||
if fixed_answer is None and _prompt_prefix_opted_in(body):
|
||||
prefix_settings = load_system_settings().prompt_prefix
|
||||
if prefix_settings.enabled and prefix_settings.prompt_prefix.strip():
|
||||
graph_input = _prepend_prompt_prefix(graph_input, prefix_settings.prompt_prefix.strip())
|
||||
llmwiki_rag = (
|
||||
LlmWikiRagContext([], [], 0, "", [], "")
|
||||
if fixed_answer is not None
|
||||
else await build_llmwiki_rag_context(
|
||||
request=request,
|
||||
body=body,
|
||||
graph_input=graph_input,
|
||||
try:
|
||||
if fixed_answer is None and _prompt_prefix_opted_in(body):
|
||||
prefix_settings = load_system_settings().prompt_prefix
|
||||
if prefix_settings.enabled and prefix_settings.prompt_prefix.strip():
|
||||
graph_input = _prepend_prompt_prefix(graph_input, prefix_settings.prompt_prefix.strip())
|
||||
llmwiki_rag = (
|
||||
LlmWikiRagContext([], [], 0, "", [], "")
|
||||
if fixed_answer is not None
|
||||
else await build_llmwiki_rag_context(
|
||||
request=request,
|
||||
body=body,
|
||||
graph_input=graph_input,
|
||||
fallback_metadata=existing_thread_metadata,
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
await _fail_unstarted_run(
|
||||
run_mgr=run_mgr,
|
||||
run_ctx=run_ctx,
|
||||
record=record,
|
||||
thread_id=thread_id,
|
||||
concurrency_gate=concurrency_gate,
|
||||
redis_lease_acquired=redis_lease_acquired,
|
||||
lease_metadata=lease_metadata,
|
||||
exc=exc,
|
||||
)
|
||||
raise
|
||||
effective_rag_mode = _effective_rag_mode_after_llmwiki(
|
||||
body=body,
|
||||
rag=llmwiki_rag,
|
||||
inherited=effective_rag_mode,
|
||||
)
|
||||
if llmwiki_rag.prompt or llmwiki_rag.sources:
|
||||
has_llmwiki_observability = bool(llmwiki_rag.prompt or llmwiki_rag.sources or llmwiki_rag.trace)
|
||||
knowledge_scope_active = bool(llmwiki_rag.knowledge_base_ids or llmwiki_rag.assistant_knowledge_base_ids)
|
||||
if has_llmwiki_observability:
|
||||
graph_input = attach_llmwiki_rag_to_graph_input(graph_input, llmwiki_rag)
|
||||
effective_rag_mode = True
|
||||
run_metadata["llmwiki_knowledge_base_ids"] = llmwiki_rag.knowledge_base_ids
|
||||
run_metadata["assistant_knowledge_base_ids"] = llmwiki_rag.assistant_knowledge_base_ids
|
||||
run_metadata["llmwiki_rag_source_count"] = llmwiki_rag.source_count
|
||||
selection_metadata_update: dict[str, Any] = {}
|
||||
if has_llmwiki_observability or llmwiki_selection_declared(
|
||||
body,
|
||||
"llmwiki_knowledge_base_ids",
|
||||
):
|
||||
selection_metadata_update["llmwiki_knowledge_base_ids"] = llmwiki_rag.knowledge_base_ids
|
||||
if has_llmwiki_observability or llmwiki_selection_declared(
|
||||
body,
|
||||
"assistant_knowledge_base_ids",
|
||||
):
|
||||
selection_metadata_update["assistant_knowledge_base_ids"] = llmwiki_rag.assistant_knowledge_base_ids
|
||||
if knowledge_scope_active:
|
||||
selection_metadata_update.update({"rag_mode_enabled": True, "rag_mode": True})
|
||||
elif llmwiki_selection_declared(body) and not _explicit_rag_mode_requested(body):
|
||||
selection_metadata_update.update({"rag_mode_enabled": False, "rag_mode": False})
|
||||
if selection_metadata_update:
|
||||
try:
|
||||
await run_ctx.thread_store.update_metadata(
|
||||
thread_id,
|
||||
{
|
||||
"llmwiki_knowledge_base_ids": llmwiki_rag.knowledge_base_ids,
|
||||
"assistant_knowledge_base_ids": llmwiki_rag.assistant_knowledge_base_ids,
|
||||
"rag_mode_enabled": True,
|
||||
"rag_mode": True,
|
||||
},
|
||||
)
|
||||
await run_ctx.thread_store.update_metadata(thread_id, selection_metadata_update)
|
||||
except Exception:
|
||||
logger.debug("Failed to persist LLMWiki RAG metadata for %s", sanitize_log_param(thread_id), exc_info=True)
|
||||
if llmwiki_rag.trace:
|
||||
# Keep the exact pre-retrieval trace on the live run so the initial
|
||||
# SSE metadata frame is inspectable in browser DevTools before the
|
||||
# answer finishes. The attached HumanMessage remains the durable
|
||||
# copy used by /reference-batches after completion.
|
||||
run_metadata["knowledge_retrieval_trace"] = llmwiki_rag.trace
|
||||
record.metadata["knowledge_retrieval_trace"] = llmwiki_rag.trace
|
||||
if effective_rag_mode:
|
||||
graph_input = _append_invisible_prompt(graph_input, _RAG_CITATION_PROMPT)
|
||||
config = build_run_config(thread_id, body.config, body.metadata, assistant_id=body.assistant_id)
|
||||
try:
|
||||
config = build_run_config(thread_id, body.config, body.metadata, assistant_id=body.assistant_id)
|
||||
except Exception as exc:
|
||||
await _fail_unstarted_run(
|
||||
run_mgr=run_mgr,
|
||||
run_ctx=run_ctx,
|
||||
record=record,
|
||||
thread_id=thread_id,
|
||||
concurrency_gate=concurrency_gate,
|
||||
redis_lease_acquired=redis_lease_acquired,
|
||||
lease_metadata=lease_metadata,
|
||||
exc=exc,
|
||||
)
|
||||
raise
|
||||
|
||||
# Merge DeerFlow-specific context overrides into both ``configurable`` and ``context``.
|
||||
# The ``context`` field is a custom extension for the langgraph-compat layer
|
||||
@@ -1158,34 +1458,72 @@ async def start_run(
|
||||
body_context["llmwiki_knowledge_base_ids"] = llmwiki_rag.knowledge_base_ids
|
||||
if "assistant_knowledge_base_ids" not in body_context and llmwiki_rag.assistant_knowledge_base_ids:
|
||||
body_context["assistant_knowledge_base_ids"] = llmwiki_rag.assistant_knowledge_base_ids
|
||||
if not llmwiki_rag.knowledge_base_ids:
|
||||
existing_excluded = list(body_context.get("excluded_tools") or getattr(body, "excluded_tools", None) or [])
|
||||
if "llmwiki_search" not in existing_excluded:
|
||||
body_context["excluded_tools"] = [*existing_excluded, "llmwiki_search"]
|
||||
# Promote whitelisted top-level body fields into the context map so
|
||||
# callers can pass them either nested under ``context`` or as flat
|
||||
# top-level fields (``excluded_tools``, ``skill_stop_names``, …).
|
||||
# Promote top-level exclusions before deriving the strict scope so caller
|
||||
# policy is retained instead of being replaced by the knowledge policy.
|
||||
for top_level_key in ("excluded_tools", "skill_stop_names", "skill_stop_message"):
|
||||
value = getattr(body, top_level_key, None)
|
||||
if value is not None and top_level_key not in body_context:
|
||||
body_context[top_level_key] = value
|
||||
strict_scope_active = apply_knowledge_space_strict_scope(
|
||||
body_context,
|
||||
selection_requested=llmwiki_selection_requested(body),
|
||||
knowledge_base_ids=llmwiki_rag.knowledge_base_ids,
|
||||
assistant_knowledge_base_ids=llmwiki_rag.assistant_knowledge_base_ids,
|
||||
app_config=getattr(request.app.state, "config", None),
|
||||
)
|
||||
if strict_scope_active:
|
||||
run_metadata["knowledge_space_strict"] = True
|
||||
elif not llmwiki_rag.knowledge_base_ids:
|
||||
raw_excluded_tools = body_context.get("excluded_tools")
|
||||
if isinstance(raw_excluded_tools, str):
|
||||
excluded_tools = [raw_excluded_tools] if raw_excluded_tools.strip() else []
|
||||
elif isinstance(raw_excluded_tools, (list, tuple, set)):
|
||||
excluded_tools = [str(item) for item in raw_excluded_tools if str(item).strip()]
|
||||
else:
|
||||
excluded_tools = []
|
||||
if "llmwiki_search" not in excluded_tools:
|
||||
body_context["excluded_tools"] = [*excluded_tools, "llmwiki_search"]
|
||||
merge_run_context_overrides(config, body_context or None)
|
||||
if strict_scope_active:
|
||||
enforce_knowledge_space_strict_run_config(
|
||||
config,
|
||||
knowledge_base_ids=llmwiki_rag.knowledge_base_ids,
|
||||
assistant_knowledge_base_ids=llmwiki_rag.assistant_knowledge_base_ids,
|
||||
excluded_tools=list(body_context.get("excluded_tools") or []),
|
||||
app_config=getattr(request.app.state, "config", None),
|
||||
)
|
||||
if analysis_project:
|
||||
# These keys are deliberately absent from the public context whitelist.
|
||||
# Only the validated project binding/checkpoint can supply them.
|
||||
inject_validated_analysis_project_context(config, body.context)
|
||||
|
||||
stream_modes = normalize_stream_modes(body.stream_mode)
|
||||
|
||||
t_pre_task = time.perf_counter()
|
||||
heartbeat_interval_seconds = 20
|
||||
if concurrency_gate is not None:
|
||||
heartbeat_interval_seconds = int(
|
||||
getattr(getattr(concurrency_gate, "config", None), "heartbeat_interval_seconds", 20) or 20
|
||||
)
|
||||
heartbeat_interval_seconds = int(getattr(getattr(concurrency_gate, "config", None), "heartbeat_interval_seconds", 20) or 20)
|
||||
try:
|
||||
after_success = None
|
||||
if analysis_project and analysis_project.get("mode") == "qa":
|
||||
from app.gateway.analysis_project_memory import enqueue_completed_project_run
|
||||
|
||||
async def _enqueue_project_memory() -> None:
|
||||
await enqueue_completed_project_run(
|
||||
app=request.app,
|
||||
project=analysis_project,
|
||||
thread_id=thread_id,
|
||||
run_id=run_id,
|
||||
)
|
||||
|
||||
after_success = _enqueue_project_memory
|
||||
task = asyncio.create_task(
|
||||
_run_agent_with_concurrency_lease(
|
||||
concurrency_gate=concurrency_gate,
|
||||
lease_acquired=redis_lease_acquired,
|
||||
lease_metadata=lease_metadata,
|
||||
heartbeat_interval_seconds=heartbeat_interval_seconds,
|
||||
after_success=after_success,
|
||||
run_agent_kwargs={
|
||||
"bridge": bridge,
|
||||
"run_manager": run_mgr,
|
||||
@@ -1202,9 +1540,17 @@ async def start_run(
|
||||
},
|
||||
)
|
||||
)
|
||||
except Exception:
|
||||
if concurrency_gate is not None and redis_lease_acquired:
|
||||
await concurrency_gate.release(**lease_metadata)
|
||||
except Exception as exc:
|
||||
await _fail_unstarted_run(
|
||||
run_mgr=run_mgr,
|
||||
run_ctx=run_ctx,
|
||||
record=record,
|
||||
thread_id=thread_id,
|
||||
concurrency_gate=concurrency_gate,
|
||||
redis_lease_acquired=redis_lease_acquired,
|
||||
lease_metadata=lease_metadata,
|
||||
exc=exc,
|
||||
)
|
||||
raise
|
||||
record.task = task
|
||||
|
||||
|
||||
Reference in new issue
Block a user