sync update from LAN repo (source master @ ca48c4ec)

This commit is contained in:
gozeros committed 2026-11-05 10:53:54 +08:00
1 parent 8bc8b0e082
commit ea9dcff9c0
873 files changed
+107866 -14011

No files matched your search

@@ -30,6 +30,7 @@ from app.gateway.llmwiki_rag import (
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,
)
@@ -134,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:
@@ -555,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,
@@ -571,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",
@@ -871,6 +879,7 @@ def enforce_reasoning_effort_run_config(
*,
capability_enabled: bool,
model_supports_thinking: bool,
default_reasoning_effort: str = "xhigh",
) -> str | None:
"""Apply the database-backed reasoning policy to both runtime containers.
@@ -889,7 +898,10 @@ def enforce_reasoning_effort_run_config(
thinking_enabled = bool(effective.get("thinking_enabled", True))
if capability_enabled and model_supports_thinking and thinking_enabled:
normalized = _normalize_reasoning_effort(effective.get("reasoning_effort"))
# 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
@@ -903,12 +915,12 @@ def enforce_reasoning_effort_run_config(
return normalized
async def _reasoning_effort_capability(request: Request, model_name: str) -> bool:
"""Read one model capability, failing closed without breaking chat."""
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
return False, "xhigh"
try:
row = await store.get_capability(model_name)
except Exception:
@@ -917,8 +929,13 @@ async def _reasoning_effort_capability(request: Request, model_name: str) -> boo
sanitize_log_param(model_name),
exc_info=True,
)
return False
return bool(row and row.get("supports_reasoning_effort", False))
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:
@@ -1062,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
@@ -1407,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,
@@ -1439,8 +1463,17 @@ 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)
@@ -1472,7 +1505,7 @@ async def start_run(
)
fixed_answer = (
None
if analysis_project or requested_retrieval_mode == "smart_reasoning"
if analysis_project or knowledge_research is not None or requested_retrieval_mode == "smart_reasoning"
else await _find_fixed_answer(
body=body,
request=request,
@@ -1825,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:
@@ -1888,7 +1928,7 @@ async def start_run(
effective_model_config
and getattr(effective_model_config, "supports_thinking", False)
)
reasoning_effort_capability = await _reasoning_effort_capability(
reasoning_effort_capability, default_reasoning_effort = await _reasoning_effort_policy(
request,
final_model_name,
)
@@ -1897,6 +1937,7 @@ async def start_run(
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(
@@ -1935,6 +1976,8 @@ async def start_run(
# 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 = _with_knowledge_retrieval_progress_stream(
normalize_stream_modes(body.stream_mode),
@@ -1995,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
@@ -2049,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():