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

@@ -25,10 +25,15 @@ from app.gateway.llmwiki_rag import (
LlmWikiRagContext,
attach_llmwiki_rag_to_graph_input,
build_llmwiki_rag_context,
build_llmwiki_smart_reasoning_context,
llmwiki_selection_declared,
llmwiki_selection_requested,
)
from app.gateway.utils import sanitize_log_param
from app.gateway.worker_pool.admission import bounded_worker_run
from deerflow.agents.middlewares.prompt_prefix_middleware import (
SMART_KNOWLEDGE_SCOPE_PROMPT_KEY,
)
from deerflow.config import get_app_config
from deerflow.config.agents_config import load_agent_config, validate_agent_id
from deerflow.config.system_settings import load_system_settings
@@ -46,6 +51,9 @@ from deerflow.runtime import (
run_agent,
)
from deerflow.runtime.user_context import get_effective_user_id
from deerflow.tools.builtins.llmwiki_reasoning_tools import (
CLASSIC_LLMWIKI_TOOL_NAMES,
)
logger = logging.getLogger(__name__)
@@ -127,7 +135,9 @@ async def _run_agent_with_concurrency_lease(
)
try:
await run_agent(**run_agent_kwargs)
from app.gateway.worker_pool.admission import run_with_worker_admission
await run_with_worker_admission(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:
@@ -200,6 +210,18 @@ def normalize_stream_modes(raw: list[str] | str | None) -> list[str]:
return raw if raw else ["values"]
def _with_knowledge_retrieval_progress_stream(
stream_modes: list[str],
knowledge_retrieval_mode: str,
) -> list[str]:
"""Ensure smart retrieval custom events reach every gateway SSE client."""
normalized = list(dict.fromkeys(stream_modes))
if knowledge_retrieval_mode == "smart_reasoning" and "custom" not in normalized:
normalized.append("custom")
return normalized
def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
"""Convert LangGraph Platform input format to LangChain state dict."""
if raw_input is None:
@@ -227,6 +249,8 @@ def normalize_input(raw_input: dict[str, Any] | None) -> dict[str, Any]:
"llmwiki_reference_query",
"llmwiki_knowledge_base_ids",
"assistant_knowledge_base_ids",
"llmwiki_knowledge_base_context",
SMART_KNOWLEDGE_SCOPE_PROMPT_KEY,
}
safe_extra = {key: value for key, value in extra.items() if key not in server_retrieval_keys}
if safe_extra:
@@ -327,6 +351,25 @@ _RAG_CITATION_PROMPT = """当前对话已开启参考文献模式。
如果没有任何可用检索结果,请直接说明未检索到可靠参考资料。"""
def _apply_classic_rag_citation_prompt(
graph_input: dict[str, Any],
*,
enabled: bool,
knowledge_retrieval_mode: str,
) -> dict[str, Any]:
"""Inject numbered-reference instructions for classic RAG only.
Smart reasoning consumes structured evidence inside its own ReAct loop and
intentionally answers without a mandatory ``[1]``/``[2]`` contract or a
generated bibliography. Keeping this policy in one helper prevents a
stale thread RAG flag from re-enabling the classic prompt.
"""
if not enabled or knowledge_retrieval_mode == "smart_reasoning":
return graph_input
return _append_invisible_prompt(graph_input, _RAG_CITATION_PROMPT)
def _prepend_prompt_prefix(graph_input: dict[str, Any], prefix: str) -> dict[str, Any]:
"""Return ``graph_input`` with the prefix *tagged* onto its first HumanMessage.
@@ -400,6 +443,48 @@ def _effective_rag_mode_after_llmwiki(
return inherited
def _retrieval_rag_metadata_flags(
*,
knowledge_retrieval_mode: str,
knowledge_scope_active: bool,
selection_declared: bool,
explicit_rag_requested: bool,
) -> dict[str, bool]:
"""Return the authoritative classic-reference flags for persisted state."""
if knowledge_retrieval_mode == "smart_reasoning":
return {"rag_mode_enabled": False, "rag_mode": False}
if knowledge_scope_active:
return {"rag_mode_enabled": True, "rag_mode": True}
if selection_declared and not explicit_rag_requested:
return {"rag_mode_enabled": False, "rag_mode": False}
return {}
def _sync_retrieval_rag_metadata(
*,
run_metadata: dict[str, Any],
record_metadata: dict[str, Any],
thread_metadata_update: dict[str, Any],
knowledge_retrieval_mode: str,
knowledge_scope_active: bool,
selection_declared: bool,
explicit_rag_requested: bool,
) -> dict[str, bool]:
"""Keep run, record and durable thread reference-mode flags identical."""
flags = _retrieval_rag_metadata_flags(
knowledge_retrieval_mode=knowledge_retrieval_mode,
knowledge_scope_active=knowledge_scope_active,
selection_declared=selection_declared,
explicit_rag_requested=explicit_rag_requested,
)
thread_metadata_update.update(flags)
run_metadata.update(flags)
record_metadata.update(flags)
return flags
def _thread_rag_mode_enabled(record: Any) -> bool:
if not isinstance(record, Mapping):
return False
@@ -441,9 +526,11 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
"thinking_force_disabled",
"reasoning_effort",
"rag_mode_enabled",
"knowledge_rag_enabled",
"llmwiki_knowledge_base_ids",
"assistant_knowledge_base_ids",
"knowledge_space_strict",
"knowledge_retrieval_mode",
"writing_mode",
"writing_artifact_path",
# 深度研究工作台「开始撰写」:走写作模式内联管线,不建 job。
@@ -471,6 +558,9 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
"writing_setup_quick",
"writing_sample_context",
"excluded_tools",
# Internal role policies may opt into a fail-closed tool allowlist.
# Ordinary clients omit it and retain the existing tool resolution.
"allowed_tools",
"skill_stop_names",
"skill_stop_message",
# 记忆开关:必须在白名单里才会从 body.context 透传到 run config,
@@ -487,6 +577,8 @@ _CONTEXT_CONFIGURABLE_KEYS: frozenset[str] = frozenset(
"roundtable_peer_deliveries",
# 岗位会商席位 / 方案总结:每轮 outputs 下 write_file 上限(行动规划不传此键)。
"position_artifact_cap",
# 会商专用文件流式兼容。只由会商 run 写入;普通对话不带此键,仍只看全局开关。
"roundtable_write_file_compatibility",
# 上下文压缩开关:由聊天框下方的按钮控制(默认关闭),透传到 run config 后
# 在 _build_middlewares 里决定是否挂载 SummarizationMiddleware。
"summarization_enabled",
@@ -506,7 +598,6 @@ _KNOWLEDGE_SPACE_EXTERNAL_RETRIEVAL_TOOLS: frozenset[str] = frozenset(
"invoke_acp_agent",
"agent_orchestration",
"task",
"deep_research_report",
}
)
@@ -621,6 +712,232 @@ def merge_run_context_overrides(config: dict[str, Any], context: Mapping[str, An
runtime_context.setdefault(key, context[key])
def enforce_knowledge_retrieval_mode_run_config(
config: dict[str, Any],
mode: str,
) -> None:
"""Write the server-validated retrieval mode into both runtime scopes."""
smart_tool_names = {
"ordinary_knowledge_search",
"ordinary_chunk_regex_search",
"ordinary_document_chunks",
"ordinary_wiki_search",
"ordinary_wiki_read_page",
}
classic_tool_names = set(CLASSIC_LLMWIKI_TOOL_NAMES)
for scope in ("configurable", "context"):
target = config.setdefault(scope, {})
if isinstance(target, dict):
target["knowledge_retrieval_mode"] = mode
if mode == "smart_reasoning":
# Smart mode keeps structured source traces but deliberately
# does not load the classic numbered-citation prompt.
target["rag_mode_enabled"] = False
target["knowledge_rag_enabled"] = False
# The mode is useful only when the five server-owned read tools
# are mounted. A caller may pre-seed ``config.context``; do not
# let that untrusted layer disable or exclude the validated
# retrieval surface.
target["disable_tools"] = False
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(
[
*(name for name in excluded if name not in smart_tool_names),
*sorted(classic_tool_names),
]
)
)
else:
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 = []
ordinary_ids = target.get("llmwiki_knowledge_base_ids")
assistant_ids = target.get("assistant_knowledge_base_ids")
has_ordinary = isinstance(ordinary_ids, list) and bool(ordinary_ids)
has_assistant = isinstance(assistant_ids, list) and bool(assistant_ids)
enabled_classic_tools: set[str] = set()
if has_ordinary or has_assistant:
enabled_classic_tools.add("llmwiki_search")
if has_ordinary:
enabled_classic_tools.update(
{"llmwiki_browse_wiki_index", "llmwiki_read_wiki_page"}
)
target["excluded_tools"] = list(
dict.fromkeys(
[
*(
name
for name in excluded
if name not in enabled_classic_tools
),
*sorted(smart_tool_names),
*sorted(classic_tool_names - enabled_classic_tools),
]
)
)
metadata = config.get("metadata")
if mode == "smart_reasoning" and isinstance(metadata, dict):
# Run-config metadata is observable by tracing/stream consumers even
# though the model reads context/configurable. Keep all three views
# consistent so smart mode cannot appear as classic reference mode.
metadata["rag_mode_enabled"] = False
metadata["rag_mode"] = False
async def resolve_knowledge_retrieval_mode(request: Request, body: Any) -> str:
"""Validate the explicit per-run mode against the database-backed switch."""
context = getattr(body, "context", None)
raw = context.get("knowledge_retrieval_mode") if isinstance(context, Mapping) else None
if raw is None or (isinstance(raw, str) and not raw.strip()):
return "classic"
if not isinstance(raw, str) or raw.strip().lower() not in {
"classic",
"smart_reasoning",
}:
raise HTTPException(
status_code=422,
detail={
"code": "INVALID_KNOWLEDGE_RETRIEVAL_MODE",
"message": "knowledge_retrieval_mode 必须为 classic 或 smart_reasoning。",
},
)
mode = raw.strip().lower()
if mode == "classic":
return mode
store = getattr(request.app.state, "knowledge_retrieval_settings_store", None)
if store is None:
raise HTTPException(
status_code=503,
detail={
"code": "KNOWLEDGE_RETRIEVAL_SETTINGS_UNAVAILABLE",
"message": "智能推理检索设置服务当前不可用。",
},
)
try:
enabled = bool(await store.get())
except Exception as exc:
logger.warning("Failed to read database-backed knowledge retrieval setting", exc_info=True)
raise HTTPException(
status_code=503,
detail={
"code": "KNOWLEDGE_RETRIEVAL_SETTINGS_UNAVAILABLE",
"message": "智能推理检索设置服务当前不可用。",
},
) from exc
if not enabled:
raise HTTPException(
status_code=409,
detail={
"code": "SMART_REASONING_DISABLED",
"message": "管理员尚未开启普通知识库智能推理检索。",
},
)
return mode
_REASONING_EFFORT_ALIASES = {
"minimal": "low",
"low": "low",
"medium": "medium",
"high": "xhigh",
"xhigh": "xhigh",
}
def _normalize_reasoning_effort(value: Any) -> str:
"""Normalize the public selector to Qwen's low/medium/xhigh contract."""
if value is None or (isinstance(value, str) and not value.strip()):
return "xhigh"
if not isinstance(value, str):
raise HTTPException(status_code=422, detail="reasoning_effort must be low, medium, or xhigh")
normalized = _REASONING_EFFORT_ALIASES.get(value.strip().lower())
if normalized is None:
raise HTTPException(status_code=422, detail="reasoning_effort must be low, medium, or xhigh")
return normalized
def enforce_reasoning_effort_run_config(
config: dict[str, Any],
*,
capability_enabled: bool,
model_supports_thinking: bool,
default_reasoning_effort: str = "xhigh",
) -> str | None:
"""Apply the database-backed reasoning policy to both runtime containers.
This intentionally runs *after* the client ``config`` and ``context`` have
been merged. A caller therefore cannot bypass the capability gate by
pre-seeding ``config.configurable.reasoning_effort``.
"""
configurable = config.setdefault("configurable", {})
runtime_context = config.setdefault("context", {})
effective: dict[str, Any] = {}
if isinstance(configurable, Mapping):
effective.update(configurable)
if isinstance(runtime_context, Mapping):
effective.update(runtime_context)
thinking_enabled = bool(effective.get("thinking_enabled", True))
if capability_enabled and model_supports_thinking and thinking_enabled:
# The selected model's persisted default is authoritative. A per-thread
# value from an older browser must not override the administrator's
# globally shared model policy.
normalized = _normalize_reasoning_effort(default_reasoning_effort)
else:
normalized = None
for target in (configurable, runtime_context):
if not isinstance(target, dict):
continue
if normalized is None:
target.pop("reasoning_effort", None)
else:
target["reasoning_effort"] = normalized
return normalized
async def _reasoning_effort_policy(request: Request, model_name: str) -> tuple[bool, str]:
"""Read one model capability and its shared default, failing closed."""
store = getattr(request.app.state, "model_capability_store", None)
if store is None:
return False, "xhigh"
try:
row = await store.get_capability(model_name)
except Exception:
logger.warning(
"Failed to read reasoning-effort capability for model %s",
sanitize_log_param(model_name),
exc_info=True,
)
return False, "xhigh"
if not row:
return False, "xhigh"
default = str(row.get("default_reasoning_effort") or "xhigh")
if default not in {"low", "medium", "xhigh"}:
default = "xhigh"
return bool(row.get("supports_reasoning_effort", False)), default
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):
@@ -762,6 +1079,12 @@ def build_run_config(
target["agent_id"] = normalized
if metadata:
config.setdefault("metadata", {}).update(metadata)
# Chat clients cannot override the administrator's deployment-wide budget.
# Other built-in graphs (writing/canvas) retain their own execution policy.
if assistant_id not in _BUILTIN_GRAPH_IDS or assistant_id == "lead_agent":
from deerflow.config.system_settings import get_conversation_context_settings
config["recursion_limit"] = get_conversation_context_settings().max_run_steps
return config
@@ -795,76 +1118,32 @@ def _collect_requested_agent_ids(body: Any) -> set[str]:
return agent_ids
def _extract_requested_model_name(body: Any) -> str | None:
"""Best-effort read of the model override requested for this run."""
context = getattr(body, "context", None)
if isinstance(context, Mapping):
for key in ("model_name", "model"):
value = str(context.get(key) or "").strip()
if value:
return value
def _runtime_config_values(config: Mapping[str, Any]) -> dict[str, Any]:
"""Mirror the lead agent's configurable/context precedence exactly."""
config = getattr(body, "config", None)
if isinstance(config, Mapping):
for container_key in ("context", "configurable"):
container = config.get(container_key)
if not isinstance(container, Mapping):
continue
for key in ("model_name", "model"):
value = str(container.get(key) or "").strip()
if value:
return value
return None
configurable = config.get("configurable")
runtime_context = config.get("context")
values = dict(configurable) if isinstance(configurable, Mapping) else {}
if isinstance(runtime_context, Mapping):
values.update(runtime_context)
return values
def _extract_effective_agent_id(body: Any) -> str | None:
"""Best-effort read of the custom agent id that the lead graph will run."""
candidates: list[str] = []
def _resolve_runtime_model_name(config: Mapping[str, Any], app_config: Any) -> str:
"""Resolve the model the lead agent will use from its final RunnableConfig.
def collect(container: Mapping[str, Any] | None) -> None:
if not container:
return
for key in ("agent_id", "agent_name"):
value = str(container.get(key) or "").strip()
if value:
candidates.append(value)
Request model overrides win over custom-agent defaults, and an unknown
explicit model falls back to the global default just like
``lead_agent._resolve_model_name``. When no model override exists, the
effective runtime agent id supplies its configured model.
"""
context = getattr(body, "context", None)
if isinstance(context, Mapping):
collect(context)
config = getattr(body, "config", None)
if isinstance(config, Mapping):
nested_context = config.get("context")
nested_configurable = config.get("configurable")
if isinstance(nested_context, Mapping):
collect(nested_context)
if isinstance(nested_configurable, Mapping):
collect(nested_configurable)
assistant_id = str(getattr(body, "assistant_id", "") or "").strip()
if assistant_id and assistant_id not in _BUILTIN_GRAPH_IDS:
candidates.append(assistant_id)
for agent_id in candidates:
try:
normalized = validate_agent_id(agent_id)
except ValueError:
logger.debug("Ignoring invalid agent id during model resolution: %s", agent_id)
continue
if normalized:
return normalized
return None
def _resolve_effective_run_model_name(body: Any) -> str:
"""Resolve the model that this run will effectively use for concurrency limiting."""
app_config = get_app_config()
default_model_name = app_config.models[0].name if app_config.models else None
if not default_model_name:
raise ValueError("No chat models are configured. Please configure at least one model in config.yaml.")
requested_model_name = _extract_requested_model_name(body)
runtime = _runtime_config_values(config)
requested_model_name = str(runtime.get("model_name") or runtime.get("model") or "").strip()
if requested_model_name:
if app_config.get_model_config(requested_model_name):
return requested_model_name
@@ -875,7 +1154,12 @@ def _resolve_effective_run_model_name(body: Any) -> str:
)
return default_model_name
agent_id = _extract_effective_agent_id(body)
raw_agent_id = runtime.get("agent_id") or runtime.get("agent_name")
try:
agent_id = validate_agent_id(raw_agent_id)
except ValueError:
logger.debug("Ignoring invalid agent id during model resolution: %s", raw_agent_id)
agent_id = None
if agent_id:
try:
agent_config = load_agent_config(agent_id)
@@ -898,30 +1182,41 @@ def _resolve_effective_run_model_name(body: Any) -> str:
return default_model_name
def _resolve_effective_model_name(body: Any, app_config: Any | None = None) -> str:
def _build_model_resolution_config(body: Any, *, thread_id: str) -> dict[str, Any]:
"""Build the same runtime containers used later by ``start_run``."""
config = build_run_config(
thread_id,
getattr(body, "config", None),
getattr(body, "metadata", None),
assistant_id=getattr(body, "assistant_id", None),
)
body_context = getattr(body, "context", None)
merge_run_context_overrides(
config,
body_context if isinstance(body_context, Mapping) else None,
)
return config
def _resolve_effective_run_model_name(body: Any) -> str:
"""Resolve the model that this run will effectively use for concurrency limiting."""
app_config = get_app_config()
config = _build_model_resolution_config(body, thread_id="__model_resolution__")
return _resolve_runtime_model_name(config, app_config)
def _resolve_effective_model_name(
body: Any,
app_config: Any | None = None,
*,
thread_id: str = "__model_resolution__",
) -> str:
"""Compatibility wrapper for resolving the effective run model."""
if app_config is None:
return _resolve_effective_run_model_name(body)
default_model_name = app_config.models[0].name if app_config.models else None
if not default_model_name:
raise ValueError("No chat models are configured. Please configure at least one model in config.yaml.")
requested_model_name = _extract_requested_model_name(body)
if requested_model_name and app_config.get_model_config(requested_model_name):
return requested_model_name
agent_id = _extract_effective_agent_id(body)
if agent_id:
try:
agent_config = load_agent_config(agent_id)
except Exception:
agent_config = None
agent_model_name = str(getattr(agent_config, "model", "") or "").strip()
if agent_model_name and app_config.get_model_config(agent_model_name):
return agent_model_name
return default_model_name
config = _build_model_resolution_config(body, thread_id=thread_id)
return _resolve_runtime_model_name(config, app_config)
def _resolve_model_concurrency_limit(model_name: str, app_config: Any) -> int | None:
@@ -1135,6 +1430,7 @@ async def _fail_unstarted_run(
logger.warning("Failed to release concurrency lease for unstarted run %s", record.run_id, exc_info=True)
@bounded_worker_run
async def start_run(
body: Any,
thread_id: str,
@@ -1167,11 +1463,21 @@ async def start_run(
"""
t_entry = time.perf_counter()
from app.gateway.analysis_projects import prepare_project_run
from app.gateway.knowledge_research import (
inject_validated_knowledge_research_context,
prepare_knowledge_research_run,
)
body, analysis_project = await prepare_project_run(body, thread_id, request)
body, knowledge_research = await prepare_knowledge_research_run(
body,
thread_id,
request,
)
bridge = get_stream_bridge(request)
run_mgr = get_run_manager(request)
run_ctx = get_run_context(request)
runtime_app_config = getattr(run_ctx, "app_config", None) or get_app_config()
concurrency_gate = get_concurrency_gate(request)
disconnect = DisconnectMode.cancel if body.on_disconnect == "cancel" else DisconnectMode.continue_
@@ -1189,9 +1495,17 @@ 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)
requested_context = getattr(body, "context", None)
requested_retrieval_mode = (
str(requested_context.get("knowledge_retrieval_mode") or "")
.strip()
.lower()
if isinstance(requested_context, Mapping)
else ""
)
fixed_answer = (
None
if analysis_project
if analysis_project or knowledge_research is not None or requested_retrieval_mode == "smart_reasoning"
else await _find_fixed_answer(
body=body,
request=request,
@@ -1204,7 +1518,11 @@ async def start_run(
model_concurrency_limit = None
user_model_concurrency_limit = None
else:
effective_model_name = _resolve_effective_run_model_name(body)
effective_model_name = _resolve_effective_model_name(
body,
runtime_app_config,
thread_id=thread_id,
)
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
run_id = str(uuid.uuid4())
@@ -1213,6 +1531,10 @@ async def start_run(
run_metadata["model_name"] = effective_model_name
run_metadata["user_id"] = effective_user_id
run_metadata["run_scope"] = effective_run_scope
if requested_retrieval_mode == "smart_reasoning":
# Avoid a transient reference-mode frame before the database-backed
# smart-mode gate and knowledge scope finish validation below.
run_metadata.update({"rag_mode_enabled": False, "rag_mode": False})
if fixed_answer is not None:
run_metadata["fixed_answer"] = True
run_metadata["fixed_question_id"] = fixed_answer["id"]
@@ -1305,7 +1627,9 @@ async def start_run(
effective_rag_mode = requested_rag_mode or _thread_rag_mode_enabled(existing)
if existing is None:
metadata = dict(body.metadata or {})
if effective_rag_mode:
if requested_retrieval_mode == "smart_reasoning":
metadata.update({"rag_mode_enabled": False, "rag_mode": False})
elif effective_rag_mode:
metadata["rag_mode_enabled"] = True
metadata["rag_mode"] = True
await run_ctx.thread_store.create(
@@ -1365,6 +1689,10 @@ async def start_run(
# thread_meta row is frequently pre-created, which previously skipped the
# prefix for genuine first turns.
try:
knowledge_retrieval_mode = await resolve_knowledge_retrieval_mode(
request,
body,
)
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():
@@ -1372,7 +1700,11 @@ async def start_run(
llmwiki_rag = (
LlmWikiRagContext([], [], 0, "", [], "")
if fixed_answer is not None
else await build_llmwiki_rag_context(
else await (
build_llmwiki_smart_reasoning_context
if knowledge_retrieval_mode == "smart_reasoning"
else build_llmwiki_rag_context
)(
request=request,
body=body,
graph_input=graph_input,
@@ -1396,14 +1728,40 @@ async def start_run(
rag=llmwiki_rag,
inherited=effective_rag_mode,
)
if knowledge_retrieval_mode == "smart_reasoning":
# Smart retrieval is a distinct evidence-gathering mode, not classic
# RAG/reference display mode. A stale thread flag must not leak into
# prompt injection or the runtime context.
effective_rag_mode = False
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)
run_metadata["knowledge_retrieval_mode"] = knowledge_retrieval_mode
record.metadata["knowledge_retrieval_mode"] = knowledge_retrieval_mode
if has_llmwiki_observability:
graph_input = attach_llmwiki_rag_to_graph_input(graph_input, llmwiki_rag)
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
if llmwiki_rag.knowledge_base_context:
# Keep the server-resolved identity beside the selected ids. A
# later turn still re-authorizes the ids and rebuilds this value;
# the durable copy exists so thread consumers do not have to infer
# what phrases such as “这个库” referred to.
run_metadata["llmwiki_knowledge_base_context"] = (
llmwiki_rag.knowledge_base_context
)
record.metadata["llmwiki_knowledge_base_context"] = (
llmwiki_rag.knowledge_base_context
)
selection_metadata_update: dict[str, Any] = {}
should_persist_retrieval_mode = (
knowledge_retrieval_mode == "smart_reasoning"
or "knowledge_retrieval_mode" in existing_thread_metadata
or has_llmwiki_observability
or llmwiki_selection_declared(body)
)
if should_persist_retrieval_mode:
selection_metadata_update["knowledge_retrieval_mode"] = knowledge_retrieval_mode
if has_llmwiki_observability or llmwiki_selection_declared(
body,
"llmwiki_knowledge_base_ids",
@@ -1414,15 +1772,45 @@ async def start_run(
"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 (
llmwiki_rag.knowledge_base_context
or "llmwiki_knowledge_base_context" in existing_thread_metadata
):
selection_metadata_update["llmwiki_knowledge_base_context"] = (
llmwiki_rag.knowledge_base_context
)
_sync_retrieval_rag_metadata(
run_metadata=run_metadata,
record_metadata=record.metadata,
thread_metadata_update=selection_metadata_update,
knowledge_retrieval_mode=knowledge_retrieval_mode,
knowledge_scope_active=knowledge_scope_active,
selection_declared=llmwiki_selection_declared(body),
explicit_rag_requested=_explicit_rag_mode_requested(body),
)
if selection_metadata_update:
try:
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)
except Exception as exc:
# Retrieval mode and scope are part of the durable run contract.
# Starting the graph after this write fails can make a later turn
# silently use a different retrieval policy, so fail the registered
# but not-yet-started run and release all of its leases instead.
logger.exception(
"Failed to persist LLMWiki retrieval metadata for %s",
sanitize_log_param(thread_id),
)
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
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
@@ -1430,8 +1818,11 @@ async def start_run(
# 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)
graph_input = _apply_classic_rag_citation_prompt(
graph_input,
enabled=effective_rag_mode,
knowledge_retrieval_mode=knowledge_retrieval_mode,
)
try:
config = build_run_config(thread_id, body.config, body.metadata, assistant_id=body.assistant_id)
except Exception as exc:
@@ -1452,8 +1843,11 @@ async def start_run(
# that carries agent configuration (model_name, thinking_enabled, etc.).
# Only agent-relevant keys are forwarded; unknown keys (e.g. thread_id) are ignored.
body_context = dict(getattr(body, "context", None) or {})
if effective_rag_mode:
body_context["knowledge_retrieval_mode"] = knowledge_retrieval_mode
if effective_rag_mode and knowledge_retrieval_mode != "smart_reasoning":
body_context["rag_mode_enabled"] = True
elif knowledge_retrieval_mode == "smart_reasoning":
body_context["rag_mode_enabled"] = False
if "llmwiki_knowledge_base_ids" not in body_context and llmwiki_rag.knowledge_base_ids:
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:
@@ -1464,13 +1858,20 @@ async def start_run(
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 knowledge_retrieval_mode == "smart_reasoning":
# Smart retrieval is an optional tool surface in the normal agent. It
# retains the validated KB ids but deliberately does not activate the
# classic fail-closed tool/skill/MCP isolation policy.
body_context["knowledge_space_strict"] = False
strict_scope_active = False
else:
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:
@@ -1481,9 +1882,81 @@ async def start_run(
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"]
unavailable_classic_tools = {
"llmwiki_browse_wiki_index",
"llmwiki_read_wiki_page",
}
if not llmwiki_rag.assistant_knowledge_base_ids:
unavailable_classic_tools.add("llmwiki_search")
missing_classic_tools = [
name for name in sorted(unavailable_classic_tools) if name not in excluded_tools
]
if missing_classic_tools:
body_context["excluded_tools"] = [*excluded_tools, *missing_classic_tools]
merge_run_context_overrides(config, body_context or None)
# This identity summary is generated only after server-side authorization.
# Put it in both LangGraph scopes so model/middleware code can resolve
# natural references to the selected library without trusting client
# metadata or inspecting a sandbox mount.
for runtime_scope in ("configurable", "context"):
runtime_target = config.setdefault(runtime_scope, {})
if isinstance(runtime_target, dict):
runtime_target["llmwiki_knowledge_base_context"] = list(
llmwiki_rag.knowledge_base_context
)
enforce_knowledge_retrieval_mode_run_config(config, knowledge_retrieval_mode)
if fixed_answer is not None:
reasoning_effort_capability = False
effective_model_supports_thinking = False
else:
# Resolve again from the actual final RunnableConfig. This is the
# security boundary: pre-seeded config.context values win exactly as
# they do in lead_agent._get_runtime_config, so capability A can never
# authorize a different runtime model B.
final_model_name = _resolve_runtime_model_name(config, runtime_app_config)
if final_model_name != effective_model_name:
logger.warning(
"Final runtime model '%s' differed from early concurrency model '%s'; applying final model policy.",
sanitize_log_param(final_model_name),
sanitize_log_param(effective_model_name),
)
effective_model_name = final_model_name
run_metadata["model_name"] = final_model_name
record.metadata["model_name"] = final_model_name
effective_model_config = runtime_app_config.get_model_config(final_model_name)
effective_model_supports_thinking = bool(
effective_model_config
and getattr(effective_model_config, "supports_thinking", False)
)
reasoning_effort_capability, default_reasoning_effort = await _reasoning_effort_policy(
request,
final_model_name,
)
try:
normalized_reasoning_effort = enforce_reasoning_effort_run_config(
config,
capability_enabled=reasoning_effort_capability,
model_supports_thinking=effective_model_supports_thinking,
default_reasoning_effort=default_reasoning_effort,
)
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
if normalized_reasoning_effort is not None:
run_metadata["reasoning_effort"] = normalized_reasoning_effort
record.metadata["reasoning_effort"] = normalized_reasoning_effort
else:
run_metadata.pop("reasoning_effort", None)
record.metadata.pop("reasoning_effort", None)
if strict_scope_active:
enforce_knowledge_space_strict_run_config(
config,
@@ -1492,12 +1965,24 @@ async def start_run(
excluded_tools=list(body_context.get("excluded_tools") or []),
app_config=getattr(request.app.state, "config", None),
)
# Strict-scope merging intentionally preserves caller exclusions. In
# smart mode the five validated retrieval tools are mandatory, so make
# the mode policy authoritative once more after that merge.
enforce_knowledge_retrieval_mode_run_config(
config,
knowledge_retrieval_mode,
)
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)
if knowledge_research:
inject_validated_knowledge_research_context(config, body.context)
stream_modes = normalize_stream_modes(body.stream_mode)
stream_modes = _with_knowledge_retrieval_progress_stream(
normalize_stream_modes(body.stream_mode),
knowledge_retrieval_mode,
)
t_pre_task = time.perf_counter()
heartbeat_interval_seconds = 20
@@ -1553,6 +2038,7 @@ async def start_run(
)
raise
record.task = task
run_mgr.watch_completion(record)
# Title sync is handled by worker.py's finally block which reads the
# title from the checkpoint and calls thread_store.update_display_name
@@ -1607,6 +2093,12 @@ async def sse_consumer(
A truly gone client fails on the next ``yield`` instead.
"""
last_event_id = request.headers.get("Last-Event-ID")
# Completed records outlive the short SSE replay buffer. Rejoining after
# buffer retirement must end cleanly, not create an empty endless stream.
has_stream = getattr(bridge, "has_stream", None)
if record.status not in (RunStatus.pending, RunStatus.running) and callable(has_stream) and not has_stream(record.run_id):
yield format_sse("end", None)
return
try:
async for entry in bridge.subscribe(record.run_id, last_event_id=last_event_id):
if record.on_disconnect == DisconnectMode.cancel and await request.is_disconnected():