sync update from LAN repo (source master-nj @ 067eb808)

This commit is contained in:
gozeros committed 2026-11-10 14:04:09 +08:00
1 parent 1244fb1f2f
commit 1d5dd8fc90
182 files changed
+18973 -769

No files matched your search

@@ -0,0 +1,227 @@
"""Project ownership, immutable mode, run policy, and fixed-template writing."""
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
from langchain_core.messages import HumanMessage
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway import analysis_projects as policy
from app.gateway.routers import analysis_projects as routes
from app.gateway.routers.thread_runs import RunCreateRequest
from deerflow.agents.analysis_project import SOLUTION_OUTLINE, project_system_prompt
from deerflow.agents.deep_research.collection.thread_harvester import harvest_messages_materials
from deerflow.persistence.analysis_projects import AnalysisProjectConfigRow, AnalysisProjectRow, AnalysisProjectStore, AnalysisProjectThreadRow
from deerflow.persistence.base import Base
from deerflow.tools.builtins.deep_research_report_tool import build_writing_research_config
def run(coro):
return asyncio.run(coro)
@pytest.mark.parametrize("persistent", [False, True])
def test_store_ownership_mode_and_binding_survive_updates(tmp_path, persistent):
async def scenario():
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'projects.db'}") if persistent else None
sf = async_sessionmaker(engine, expire_on_commit=False) if engine else None
if engine:
async with engine.begin() as connection:
await connection.run_sync(lambda conn: Base.metadata.create_all(conn, tables=[AnalysisProjectRow.__table__, AnalysisProjectThreadRow.__table__, AnalysisProjectConfigRow.__table__]))
try:
store = AnalysisProjectStore(sf)
project = await store.create("alice", mode="qa", name="项目 A", knowledge_base_ids=["kb-a"], agent_id="agent-a", report_config={"custom_outline": "## 自选结构"}, strategy_config={"lenses": ["风险视角", "用户体验视角"]})
other = await store.create("bob", mode="solution", name="项目 B")
await store.bind_thread(project["id"], "alice", "thread-a")
await store.bind_thread(other["id"], "bob", "thread-b")
assert await store.get(project["id"], "bob") is None
assert await store.update(project["id"], "bob", name="伪造") is None
with pytest.raises(ValueError):
await store.update(project["id"], "alice", mode="solution")
with pytest.raises(ValueError):
await store.bind_thread(other["id"], "alice", "foreign")
await store.update(project["id"], "alice", knowledge_base_ids=["kb-b"])
reloaded = AnalysisProjectStore(sf) if sf else store
assert (await reloaded.project_for_thread("thread-a"))["mode"] == "qa"
assert (await reloaded.get(project["id"], "alice"))["knowledge_base_ids"] == ["kb-b"]
assert (await reloaded.get(project["id"], "alice"))["agent_id"] == "agent-a"
assert (await reloaded.get(project["id"], "alice"))["report_config"]["custom_outline"] == "## 自选结构"
assert (await reloaded.get(project["id"], "alice"))["strategy_config"]["lenses"] == ["风险视角", "用户体验视角"]
assert await reloaded.thread_ids(project["id"], "bob") == []
assert await reloaded.thread_ids(project["id"], "alice") == ["thread-a"]
assert [p["id"] for p in await reloaded.list("bob")] == [other["id"]]
finally:
if engine:
await engine.dispose()
run(scenario())
@pytest.fixture
def client(monkeypatch):
app = FastAPI()
app.state.analysis_project_store = AnalysisProjectStore()
app.state.llmwiki_store = SimpleNamespace(get_authorized=AsyncMock(side_effect=lambda kb, user, **kwargs: {"id": kb} if kb in {"wiki-a", "wiki-b"} else None))
app.state.assistant_knowledge_store = SimpleNamespace(get_base=AsyncMock(side_effect=lambda kb: {"id": kb} if kb == "assistant-a" else None))
async def actor(request):
return request.headers.get("x-test-user", "alice"), False
monkeypatch.setattr(policy, "actor", actor)
monkeypatch.setattr(routes, "actor", actor)
monkeypatch.setattr(policy, "get_agent_store", lambda request: SimpleNamespace(get_visible=AsyncMock(side_effect=lambda agent_id, user_id: {"id": agent_id} if agent_id in {"agent-a", "agent-b"} else None)))
app.include_router(routes.router)
with TestClient(app) as client:
yield client
def test_project_api_mode_not_editable_and_other_owners_hidden(client):
response = client.post("/api/analysis-projects", json={"name": "项目", "mode": "strategy", "knowledge_base_ids": ["wiki-a", "wiki-a"]})
assert response.status_code == 201
project = response.json()
assert project["knowledge_base_ids"] == ["wiki-a"]
assert "user_id" not in project
url = f"/api/analysis-projects/{project['id']}"
assert client.put(url, json={"name": "变更", "mode": "solution"}).status_code == 422
updated = client.put(url, json={"name": "变更", "knowledge_base_ids": ["wiki-b"], "assistant_knowledge_base_ids": ["assistant-a"]})
assert updated.status_code == 200
assert updated.json()["mode"] == "strategy"
assert client.get(url, headers={"x-test-user": "bob"}).status_code == 404
assert client.put(url, headers={"x-test-user": "bob"}, json={"name": "他人修改"}).status_code == 404
assert client.get("/api/analysis-projects", headers={"x-test-user": "bob"}).json() == {"projects": []}
assert client.post(f"{url}/threads", headers={"x-test-user": "bob"}).status_code == 404
assert client.put(url, json={"name": "变更", "knowledge_base_ids": ["private-or-deleted"]}).status_code == 422
assert client.post("/api/analysis-projects", json={"name": "", "mode": "qa"}).status_code == 422
assert client.post("/api/analysis-projects", json={"name": "自主分析", "mode": "strategy"}).status_code == 201
assert client.post("/api/analysis-projects", json={"name": "不可用", "mode": "qa", "agent_id": "private"}).status_code == 422
@pytest.mark.parametrize("mode", ["qa", "strategy", "solution"])
def test_run_policy_uses_binding_not_client_mode_or_knowledge(monkeypatch, mode):
async def scenario():
store = AnalysisProjectStore()
project = await store.create("alice", mode=mode, name="项目", knowledge_base_ids=["wiki-a"])
await store.bind_thread(project["id"], "alice", "thread", agent_id="agent-a")
request = SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(analysis_project_store=store)))
monkeypatch.setattr(policy, "actor", AsyncMock(return_value=("alice", False)))
monkeypatch.setattr(policy, "validate_knowledge", AsyncMock())
monkeypatch.setattr(policy, "validate_project_agent", AsyncMock())
monkeypatch.setattr(policy, "load_agent_config", lambda agent_id: SimpleNamespace(llmwiki_knowledge_base_ids=["wiki-a"]))
checkpointer = SimpleNamespace(aget_tuple=AsyncMock(return_value=None))
monkeypatch.setattr(policy, "get_checkpointer", lambda request: checkpointer)
body = RunCreateRequest(
assistant_id="ai_writing",
context={"writing_mode": True, "analysis_project": {"mode": "solution"}, "llmwiki_knowledge_base_ids": ["forged"]},
config={"configurable": {"analysis_project": {"mode": "solution"}, "writing_mode": True, "is_writing_setup": True}},
)
prepared, resolved = await policy.prepare_project_run(body, "thread", request)
assert resolved["id"] == project["id"]
assert prepared.assistant_id == "agent-a"
assert prepared.context["agent_id"] == "agent-a"
assert prepared.context["analysis_project"]["mode"] == mode
assert prepared.context["writing_mode"] == (mode == "solution")
assert prepared.context["llmwiki_knowledge_base_ids"] == ["wiki-a"]
assert prepared.config["configurable"]["is_writing_setup"] is False
assert body.context["llmwiki_knowledge_base_ids"] == ["forged"]
await store.update(project["id"], "alice", agent_id="agent-b", report_config={"custom_outline": "## 新结构", "structure_mode": "adaptive"})
prepared, _ = await policy.prepare_project_run(body, "thread", request)
assert prepared.context["llmwiki_knowledge_base_ids"] == ["wiki-a"]
assert prepared.context["agent_id"] == "agent-a"
assert prepared.context["analysis_project"]["report_config"]["custom_outline"] == "## 新结构"
ordinary, resolved = await policy.prepare_project_run(body, "ordinary", request)
assert resolved is None
assert "analysis_project" not in ordinary.context
assert "analysis_project" not in ordinary.config["configurable"]
monkeypatch.setattr(policy, "actor", AsyncMock(return_value=("bob", False)))
with pytest.raises(HTTPException) as error:
await policy.prepare_project_run(body, "thread", request)
assert error.value.status_code == 404
run(scenario())
def test_fixed_solution_template_and_retrieved_materials():
assert build_writing_research_config().structure_mode == "adaptive"
config = build_writing_research_config(project_mode="solution")
assert config.structure_mode == "fixed"
assert config.custom_outline == SOLUTION_OUTLINE
selected = build_writing_research_config(project_mode="solution", project_report_config={"custom_outline": "## 目标\n## 路径", "structure_mode": "adaptive", "retrieval_directions": ["市场"], "retrieval_skills": ["search"]})
assert selected.custom_outline == "## 目标\n## 路径"
assert selected.structure_mode == "adaptive"
assert selected.retrieval_directions == ["市场"]
assert selected.retrieval_skills == ["search"]
assert "不得编造" in config.report_instruction
assert project_system_prompt({}) == ""
strategy_prompt = project_system_prompt({"analysis_project": {"mode": "strategy", "strategy_config": {"lenses": ["风险视角", "用户体验视角"]}}})
assert "策略智能" in strategy_prompt
assert "策略辩论" in strategy_prompt
assert "## 完整策略方案" in strategy_prompt
assert "### 分阶段行动路线图" in strategy_prompt
assert "### 评估指标与复盘机制" in strategy_prompt
assert "### 备选策略与切换条件" in strategy_prompt
assert "用户体验视角" in strategy_prompt
source = {"title": "渠道调研", "content": "华东市场已有三个渠道伙伴", "url": "https://example.test/source"}
message = HumanMessage(content="生成区域市场方案", additional_kwargs={"llmwiki_reference_sources": [source]})
materials, _ = harvest_messages_materials([message], query="区域市场", include_rag_sources=True)
assert len(materials) == 1
assert "三个渠道伙伴" in materials[0].raw_content
assert materials[0].metadata["message_index"] == 0
def test_project_thread_creation_binding_and_failed_creation_cleanup(client, monkeypatch):
rows = {}
async def get(thread_id, **kwargs):
return rows.get(thread_id)
async def delete(thread_id):
rows.pop(thread_id, None)
async def update_metadata(thread_id, metadata, **kwargs):
rows[thread_id]["metadata"] = metadata
thread_store = SimpleNamespace(get=get, delete=delete, update_metadata=update_metadata)
checkpoint = SimpleNamespace(adelete_thread=AsyncMock())
monkeypatch.setattr(routes, "get_thread_store", lambda request: thread_store)
monkeypatch.setattr(routes, "get_checkpointer", lambda request: checkpoint)
async def create(body, request):
rows[body.thread_id] = {"thread_id": body.thread_id, "status": "idle", "metadata": body.metadata}
return rows[body.thread_id]
monkeypatch.setattr(routes, "create_thread", create)
project = client.post("/api/analysis-projects", json={"name": "方案项目", "mode": "solution", "agent_id": "agent-a"}).json()
other = client.post("/api/analysis-projects", json={"name": "问答项目", "mode": "qa"}).json()
url = f"/api/analysis-projects/{project['id']}/threads"
created = client.post(url)
assert created.status_code == 201
assert created.json()["metadata"]["agent_id"] == "agent-a"
assert created.json()["metadata"]["system"] is False
thread_id = created.json()["thread_id"]
assert client.get(url).json()["threads"] == [{**created.json(), "title": "新对话"}]
assert client.get(f"{url}/{thread_id}").status_code == 200
assert client.get(f"{url}/{thread_id}", headers={"x-test-user": "bob"}).status_code == 404
assert client.get(f"/api/analysis-projects/{other['id']}/threads/{thread_id}").status_code == 404
assert run(client.app.state.analysis_project_store.project_for_thread(thread_id))["mode"] == "solution"
second = client.post(url, json={"agent_id": "agent-b"})
assert second.status_code == 201
assert run(client.app.state.analysis_project_store.config_for(f"thread:{second.json()['thread_id']}"))["agent_id"] == "agent-b"
assert client.post(url, json={"agent_id": "private"}).status_code == 422
failed_ids = []
async def fail_after_meta(body, request):
await create(body, request)
failed_ids.append(body.thread_id)
raise HTTPException(500, "Checkpoint unavailable")
monkeypatch.setattr(routes, "create_thread", fail_after_meta)
assert client.post(url).status_code == 500
failed_id = failed_ids[0]
assert failed_id not in rows
assert run(client.app.state.analysis_project_store.project_for_thread(failed_id)) is None
checkpoint.adelete_thread.assert_awaited_once_with(failed_id)
@@ -0,0 +1,100 @@
import asyncio
from types import SimpleNamespace
import httpx
import pytest
from fastapi import FastAPI
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway import assistant_file_ingest as ingest
from app.gateway.routers import assistant_knowledge as router
from deerflow.knowledge.schemas import NoteDraft
from deerflow.persistence.assistant_knowledge import AssistantKnowledgeRepository
from deerflow.persistence.base import Base
def test_same_topic_across_document_parts_preserves_all_knowledge():
package = ingest.notes_package([
{"title": "设备维护", "content_md": "每周清理过滤网", "tags": ["运维"]},
{"title": "设备 维护", "content_md": "每月检查电机温度", "tags": ["设备"]},
], {"digest": "document", "filename": "维护指南.md"})
assert len(package["wiki_pages"]) == 1
assert "每周清理过滤网" in package["wiki_pages"][0]["content"]
assert "每月检查电机温度" in package["wiki_pages"][0]["content"]
assert package["wiki_pages"][0]["tags"] == ["运维", "设备"]
@pytest.mark.asyncio
async def test_standalone_upload_vectors_retry_and_dedup(tmp_path, monkeypatch):
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
async with engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
store = AssistantKnowledgeRepository(async_sessionmaker(engine, expire_on_commit=False))
base = await store.create_base(name="独立文档库", created_by="admin")
app = FastAPI()
app.include_router(router.router)
app.state.assistant_knowledge_store = store
app.state.llmwiki_embedding = None
monkeypatch.setattr(ingest, "runtime_home", lambda: tmp_path)
extraction_calls = []
async def actor(*args, **kwargs):
return "admin"
async def extract(text, **kwargs):
extraction_calls.append(text)
return [NoteDraft(title="冷却系统", summary="摘要占位", content_md="# 冷却系统\n风扇的额定转速为每分钟 1200 转。", tags=["设备"], entities=[{"name": "风扇", "type": "product"}])]
class Encoder:
fingerprint = "local-test-3"
dimensions = 3
config = SimpleNamespace(model="offline-test")
async def embed_texts(self, texts):
return [[1.0, 0.0, 0.0] for _ in texts]
async def embed_query(self, text):
return [1.0, 0.0, 0.0]
monkeypatch.setattr(router, "_actor", actor)
monkeypatch.setattr(ingest, "extract_document_llm_multi", extract)
async def finish():
if ingest._tasks:
await asyncio.gather(*list(ingest._tasks.values()))
try:
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client:
payload = {"assistant_base_id": base["id"]}
response = await client.post("/api/assistant-knowledge/imports/files", data=payload, files={"file": ("../../设备.md", "风扇额定转速为 1200 转。".encode())})
assert response.status_code == 202, response.text
job_id = response.json()["id"]
await finish()
assert (await store.get_job(job_id))["status"] == "failed"
assert (ingest.file_job_dir(job_id) / "wiki.json").exists()
app.state.llmwiki_embedding = Encoder()
retry = await client.post(f"/api/assistant-knowledge/import-jobs/{job_id}/retry-file")
assert retry.status_code == 202
await finish()
job = await store.get_job(job_id)
assert job["status"] == "completed", job
assert job["counts"]["vectors"] == 1
assert len(extraction_calls) == 1
results = await store.search_pages(base_ids=[base["id"]], query="额定转速", embedding_client=app.state.llmwiki_embedding)
assert results and results[0]["retrieval_mode"] == "vector"
assert "1200" in results[0]["matched_section"]["content"]
assert "摘要占位" not in results[0]["matched_section"]["content"]
global_base = await store.get_base()
assert len(await store.list_pages(base_id=global_base["id"])) == 1
again = await client.post("/api/assistant-knowledge/imports/files", data=payload, files={"file": ("设备.md", "风扇额定转速为 1200 转。".encode())})
assert again.status_code == 202
await finish()
assert len(await store.list_sources(base_id=base["id"])) == 1
assert len(await store.list_pages(base_id=global_base["id"])) == 1
bad = await client.post("/api/assistant-knowledge/imports/files", data=payload, files={"file": ("script.py", b"print('no')")})
assert bad.status_code == 415
empty = await client.post("/api/assistant-knowledge/imports/files", data=payload, files={"file": ("empty.txt", b"")})
assert empty.status_code == 422
finally:
await ingest.stop_file_jobs()
await engine.dispose()
@@ -11,9 +11,11 @@ import asyncio
from types import SimpleNamespace
import httpx
from langchain_core.messages import AIMessage
import pytest
from langchain_core.messages import AIMessage, AIMessageChunk
from deerflow.agents.middlewares.llm_error_handling_middleware import LLM_ERROR_MARKER, LLMErrorHandlingMiddleware
from deerflow.models.patched_openai import OpenAICompatibleUpstreamError, PatchedChatOpenAI
def _middleware() -> LLMErrorHandlingMiddleware:
@@ -26,6 +28,10 @@ def _middleware() -> LLMErrorHandlingMiddleware:
return middleware
def _openai_model() -> PatchedChatOpenAI:
return PatchedChatOpenAI(model="test-model", api_key="test-key")
def test_httpx_read_timeout_is_transient() -> None:
assert _middleware()._classify_error(httpx.ReadTimeout("read timed out")) == (True, "transient")
@@ -46,6 +52,98 @@ def test_value_error_stays_generic() -> None:
assert _middleware()._classify_error(ValueError("boom")) == (False, "generic")
def test_openai_compatible_embedded_error_is_retriable_upstream() -> None:
error = RuntimeError("OpenAI-compatible upstream application error (code=2056)")
assert _middleware()._classify_error(error) == (True, "upstream")
def test_openai_compatible_null_choices_error_is_retriable_upstream() -> None:
error = TypeError("Received response with null value for 'choices'.")
assert _middleware()._classify_error(error) == (True, "upstream")
def test_insufficient_balance_stays_non_retriable_quota() -> None:
"""The 200-body compatibility fix must not hide a real billing failure."""
class PaymentError(RuntimeError):
status_code = 402
error = PaymentError("Insufficient Balance")
assert _middleware()._classify_error(error) == (False, "quota")
def test_minimax_token_plan_limit_stays_non_retriable_quota() -> None:
error = OpenAICompatibleUpstreamError(
code=2056,
message="Token Plan limit reached",
)
assert _middleware()._classify_error(error) == (False, "quota")
def test_non_stream_embedded_application_error_is_raised() -> None:
response = {
"id": "failed-response",
"object": "chat.completion",
"created": 0,
"model": "test-model",
"choices": None,
"base_resp": {
"status_code": 2056,
"status_msg": "Token Plan limit reached",
},
}
with pytest.raises(OpenAICompatibleUpstreamError, match=r"code=2056") as exc_info:
_openai_model()._create_chat_result(response)
assert exc_info.value.code == 2056
assert "Token Plan limit reached" in str(exc_info.value)
def test_stream_embedded_application_error_is_raised() -> None:
chunk = {
"id": "failed-response",
"object": "chat.completion.chunk",
"created": 0,
"model": "test-model",
"choices": [],
"base_resp": {
"status_code": 2056,
"status_msg": "Token Plan limit reached",
},
}
with pytest.raises(OpenAICompatibleUpstreamError, match=r"code=2056"):
_openai_model()._convert_chunk_to_generation_chunk(chunk, AIMessageChunk, None)
def test_normal_empty_usage_chunk_is_not_treated_as_error() -> None:
chunk = {
"id": "usage-response",
"object": "chat.completion.chunk",
"created": 0,
"model": "test-model",
"choices": [],
"usage": {
"prompt_tokens": 3,
"completion_tokens": 2,
"total_tokens": 5,
},
}
result = _openai_model()._convert_chunk_to_generation_chunk(chunk, AIMessageChunk, None)
assert result is not None
assert result.message.usage_metadata == {
"input_tokens": 3,
"output_tokens": 2,
"total_tokens": 5,
"input_token_details": {},
"output_token_details": {},
}
def test_read_timeout_is_retried_and_recovers() -> None:
"""第一次 ReadTimeout、第二次成功:必须重试并返回真实结果。"""
middleware = _middleware()
@@ -0,0 +1,283 @@
from __future__ import annotations
import asyncio
from pathlib import Path
from threading import Event
from types import SimpleNamespace
import pytest
import yaml
from fastapi import FastAPI
from starlette.requests import Request
from app.gateway import llmwiki_index_runtime
from app.gateway.routers import llmwiki_index
from deerflow.config.llmwiki_config import LocalWikiEmbeddingConfig, LocalWikiIndexConfig
from deerflow.config.system_settings import LocalWikiEncoderSettings, SystemSettings
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient, WikiEmbeddingError
def _source_config(embedding: LocalWikiEmbeddingConfig):
return SimpleNamespace(
llmwiki=SimpleNamespace(
local_wiki_index=LocalWikiIndexConfig(enabled=True, embedding=embedding)
)
)
def test_runtime_mode_can_switch_remote_config_to_uploaded_local_model(tmp_path: Path) -> None:
configured = LocalWikiEmbeddingConfig(
provider="openai_compatible",
model="remote-embedding",
base_url="https://embedding.example.test/v1",
dimensions=1536,
)
source = _source_config(configured)
normal = llmwiki_index_runtime.resolve_effective_local_wiki_index_config(
source,
LocalWikiEncoderSettings(mode="configured"),
)
assert normal.embedding.provider == "openai_compatible"
assert normal.embedding.model == "remote-embedding"
assert normal.embedding.dimensions == 1536
local = llmwiki_index_runtime.resolve_effective_local_wiki_index_config(
source,
LocalWikiEncoderSettings(
mode="local",
model_path=str(tmp_path),
model_revision="revision-2",
),
)
assert local.embedding.provider == "local"
assert local.embedding.model == "bge-embedding-m3"
assert local.embedding.dimensions == 1024
assert local.embedding.local_model_path == str(tmp_path)
assert local.embedding.local_model_revision == "revision-2"
assert local.embedding.base_url == ""
assert local.embedding.api_key == ""
def test_local_model_revision_changes_embedding_fingerprint(tmp_path: Path) -> None:
common = {
"provider": "local",
"model": "bge-embedding-m3",
"dimensions": 1024,
"local_model_path": str(tmp_path),
}
first = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(**common, local_model_revision="first")
)
second = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(**common, local_model_revision="second")
)
assert first.fingerprint != second.fingerprint
def test_local_model_path_changes_embedding_fingerprint(tmp_path: Path) -> None:
first_path = tmp_path / "revision-a"
second_path = tmp_path / "revision-b"
common = {
"provider": "local",
"model": "bge-embedding-m3",
"dimensions": 1024,
"local_model_revision": "same-revision",
}
first = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(**common, local_model_path=str(first_path))
)
second = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(**common, local_model_path=str(second_path))
)
assert first.fingerprint != second.fingerprint
def test_uploaded_local_mode_always_uses_bge_m3_contract(tmp_path: Path) -> None:
configured = LocalWikiEmbeddingConfig(
provider="local",
model="another-local-model",
local_model_path=str(tmp_path / "configured"),
dimensions=384,
)
effective = llmwiki_index_runtime.resolve_effective_local_wiki_index_config(
_source_config(configured),
LocalWikiEncoderSettings(
mode="local",
model_path=str(tmp_path / "uploaded"),
model_revision="uploaded-revision",
),
)
assert effective.embedding.provider == "local"
assert effective.embedding.model == "bge-embedding-m3"
assert effective.embedding.dimensions == 1024
assert effective.embedding.local_model_path == str(tmp_path / "uploaded")
def test_local_provider_never_uses_residual_base_url_as_readiness(tmp_path: Path) -> None:
config = LocalWikiIndexConfig(
enabled=True,
embedding=LocalWikiEmbeddingConfig(
provider="local",
model="bge-embedding-m3",
local_model_path=str(tmp_path / "missing"),
base_url="https://stale.example.test/v1",
dimensions=1024,
),
)
assert llmwiki_index_runtime._embedding_provider_ready(config) is False
@pytest.mark.asyncio
async def test_retired_embedding_rejects_future_work() -> None:
client = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(
provider="openai",
model="text-embedding-3-small",
base_url="https://api.openai.com/v1",
dimensions=1536,
)
)
await client.retire()
with pytest.raises(WikiEmbeddingError) as exc_info:
await client.embed_query("retry me")
assert getattr(exc_info.value, "category", None) == "runtime_retired"
@pytest.mark.asyncio
async def test_cancelled_local_embedding_drains_worker_thread_before_releasing_lock(
monkeypatch,
) -> None:
client = StrictWikiEmbeddingClient(
LocalWikiEmbeddingConfig(
provider="local",
model="bge-embedding-m3",
dimensions=2,
)
)
started = Event()
release = Event()
def blocking_embed(_texts):
started.set()
release.wait(timeout=5)
return [[1.0, 0.0]]
monkeypatch.setattr(client, "_embed_local", blocking_embed)
embedding_task = asyncio.create_task(client.embed_texts(["text"]))
assert await asyncio.to_thread(started.wait, 1)
embedding_task.cancel()
await asyncio.sleep(0)
assert not embedding_task.done()
assert client._active_uses == 1
embedding_task.cancel()
await asyncio.sleep(0)
assert not embedding_task.done()
assert client._active_uses == 1
release.set()
with pytest.raises(asyncio.CancelledError):
await embedding_task
assert client._active_uses == 0
@pytest.mark.asyncio
async def test_status_is_available_for_remote_provider(monkeypatch) -> None:
configured = LocalWikiEmbeddingConfig(
provider="openai",
model="text-embedding-3-small",
base_url="https://api.openai.com/v1",
dimensions=1536,
)
app = FastAPI()
app.state.config = _source_config(configured)
app.state.llmwiki_effective_config = app.state.config.llmwiki.local_wiki_index
app.state.llmwiki_embedding = StrictWikiEmbeddingClient(configured)
request = Request({"type": "http", "headers": [], "app": app})
async def admin(_request):
return "admin", True
async def unchanged(_app, *, force=False):
return _app.state.llmwiki_effective_config
monkeypatch.setattr(llmwiki_index, "_actor", admin)
monkeypatch.setattr(llmwiki_index, "rebuild_local_wiki_index_runtime", unchanged)
monkeypatch.setattr(
llmwiki_index_runtime,
"load_system_settings",
lambda: SystemSettings(
local_wiki_encoder=LocalWikiEncoderSettings(mode="configured")
),
)
result = await llmwiki_index.local_encoder_status(request)
assert result["provider"] == "openai"
assert result["configured_provider"] == "openai"
assert result["mode"] == "configured"
assert result["using_local"] is False
assert result["state"] == "idle"
@pytest.mark.asyncio
async def test_local_path_save_and_restore_persist_mode_and_rebuild(
monkeypatch,
tmp_path: Path,
) -> None:
model_dir = tmp_path / "bge-m3"
(model_dir / "onnx").mkdir(parents=True)
(model_dir / "onnx" / "model.onnx").write_bytes(b"onnx")
app = FastAPI()
request = Request({"type": "http", "headers": [], "app": app})
saved: list[SystemSettings] = []
current = SystemSettings()
rebuilds: list[bool] = []
async def admin(_request):
return "admin", True
async def rebuilt(_app, *, force=False):
rebuilds.append(force)
return LocalWikiIndexConfig()
monkeypatch.setattr(llmwiki_index, "_actor", admin)
monkeypatch.setattr(llmwiki_index, "load_system_settings", lambda: saved[-1] if saved else current)
monkeypatch.setattr(llmwiki_index, "save_system_settings", lambda value: saved.append(value) or value)
monkeypatch.setattr(llmwiki_index, "rebuild_local_wiki_index_runtime", rebuilt)
monkeypatch.setattr(
llmwiki_index,
"local_encoder_runtime_status",
lambda _app: {
"provider": "local" if saved[-1].local_wiki_encoder.mode == "local" else "openai",
"mode": saved[-1].local_wiki_encoder.mode,
},
)
local = await llmwiki_index.configure_local_encoder(
request,
llmwiki_index.LocalEncoderConfigRequest(model_path=str(model_dir)),
)
assert local["mode"] == "local"
assert saved[-1].local_wiki_encoder.mode == "local"
assert saved[-1].local_wiki_encoder.model_revision
configured = await llmwiki_index.use_configured_encoder(request)
assert configured["mode"] == "configured"
assert saved[-1].local_wiki_encoder.mode == "configured"
assert rebuilds == [True, True]
def test_both_shipped_offline_configs_enable_incremental_wiki_rescan() -> None:
offline_root = Path(__file__).resolve().parents[2]
for path in (offline_root / "config.yaml", offline_root / "backend" / "config.yaml"):
payload = yaml.safe_load(path.read_text(encoding="utf-8"))
local_index = payload["llmwiki"]["local_wiki_index"]
assert local_index["enabled"] is True
assert local_index["auto_sync"] is True
@@ -497,6 +497,129 @@ async def test_weknora_client_resolves_default_embedding_model_inside_weknora()
assert created["id"] == "kb-created"
@pytest.mark.asyncio
async def test_gateway_lists_safe_embedding_models_and_uses_explicit_selection(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from app.gateway.routers import llmwiki as llmwiki_router
create_calls: list[dict[str, object]] = []
class Client:
async def list_models(self) -> list[dict[str, object]]:
return [
{
"id": "embed-local",
"type": "Embedding",
"name": "Local BGE",
"provider": "local",
"status": "active",
"is_default": True,
"api_key": "must-not-leak",
"config": {"secret": "must-not-leak"},
},
{
"id": "embed-openai",
"type": "embedding",
"display_name": "OpenAI text-embedding",
"source": "remote",
"parameters": {
"model": "text-embedding-3-large",
"provider": "openai",
"api_key": "must-not-leak",
},
"status": "active",
},
{
"id": "embed-disabled",
"type": "Embedding",
"name": "Disabled",
"status": "inactive",
},
{"id": "chat-one", "type": "LLM", "name": "chat"},
]
async def create_knowledge_base(self, **kwargs: object) -> dict[str, object]:
create_calls.append(kwargs)
return {
"id": "remote-openai",
"name": kwargs["name"],
"description": kwargs["description"],
"type": kwargs["kb_type"],
"embedding_model_id": kwargs["embedding_model_id"],
}
async def actor(_: object) -> tuple[str, bool]:
return "user-a", False
store = MemoryLlmWikiStore()
client = Client()
monkeypatch.setattr(llmwiki_router, "_actor", actor)
monkeypatch.setattr(llmwiki_router, "_client_or_503", lambda: client)
monkeypatch.setattr(llmwiki_router, "_store", lambda _: store)
monkeypatch.setattr(llmwiki_router, "_schedule_local_wiki_sync", lambda *_: None)
listed = await llmwiki_router.list_embedding_models(object())
assert listed["default_model_id"] == "embed-local"
assert [item["id"] for item in listed["models"]] == [
"embed-local",
"embed-openai",
"embed-disabled",
]
assert listed["models"][1]["provider"] == "openai"
assert listed["models"][1]["model_name"] == "text-embedding-3-large"
assert listed["models"][2]["available"] is False
assert "api_key" not in listed["models"][0]
assert "config" not in listed["models"][0]
assert "parameters" not in listed["models"][1]
created = await llmwiki_router.create_knowledge_base(
object(),
llmwiki_router.KnowledgeBaseCreate(
name="OpenAI docs",
embedding_model_id="embed-openai",
),
)
assert create_calls[0]["embedding_model_id"] == "embed-openai"
assert created["embedding_model_id"] == "embed-openai"
for rejected_model_id in ("embed-disabled", "chat-one", "missing"):
with pytest.raises(HTTPException) as exc_info:
await llmwiki_router.create_knowledge_base(
object(),
llmwiki_router.KnowledgeBaseCreate(
name="Rejected docs",
embedding_model_id=rejected_model_id,
),
)
assert exc_info.value.status_code == 422
assert len(create_calls) == 1
@pytest.mark.asyncio
async def test_gateway_embedding_model_list_allows_empty_configuration(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from app.gateway.routers import llmwiki as llmwiki_router
class Client:
async def list_models(self) -> list[dict[str, object]]:
return [{"id": "chat-only", "type": "LLM"}]
async def actor(_: object) -> tuple[str, bool]:
return "user-a", False
monkeypatch.setattr(llmwiki_router, "_actor", actor)
monkeypatch.setattr(llmwiki_router, "_client_or_503", lambda: Client())
assert await llmwiki_router.list_embedding_models(object()) == {
"models": [],
"default_model_id": None,
}
@pytest.mark.asyncio
async def test_weknora_client_creates_wiki_with_weknora_owned_models() -> None:
model_reads = 0
@@ -199,34 +199,101 @@ async def test_fingerprint_change_automatically_rebuilds_entire_mapping() -> Non
@pytest.mark.asyncio
async def test_auto_sync_scheduler_scans_immediately_then_waits(monkeypatch) -> None:
calls: list[str] = []
async def test_auto_sync_scheduler_scans_immediately_then_only_when_due(monkeypatch) -> None:
now = 0.0
refreshes: list[float] = []
syncs: list[float] = []
sleeps: list[float] = []
config = SimpleNamespace(
enabled=True,
auto_sync=True,
sync_interval_seconds=25,
)
async def fake_refresh(_app):
refreshes.append(now)
return config
async def fake_sleep(seconds: float) -> None:
calls.append(f"sleep:{seconds}")
raise asyncio.CancelledError
async def fake_sync_once(app) -> None:
calls.append("sync")
if calls.count("sync") > 1:
nonlocal now
sleeps.append(seconds)
if len(syncs) >= 2:
raise asyncio.CancelledError
now += seconds
async def fake_sync_once(_app, *, config=None) -> None:
syncs.append(now)
monkeypatch.setattr(llmwiki_index_scheduler, "monotonic", lambda: now)
monkeypatch.setattr(llmwiki_index_scheduler.asyncio, "sleep", fake_sleep)
monkeypatch.setattr(llmwiki_index_scheduler, "_sync_once", fake_sync_once)
monkeypatch.setattr(
llmwiki_index_scheduler,
"_refresh_local_wiki_config",
fake_refresh,
)
app = SimpleNamespace(
state=SimpleNamespace(
config=SimpleNamespace(
llmwiki=SimpleNamespace(
local_wiki_index=SimpleNamespace(sync_interval_seconds=120),
)
)
llmwiki_sync_service=object(),
llmwiki_store=object(),
)
)
with pytest.raises(asyncio.CancelledError):
await llmwiki_index_scheduler.local_wiki_index_scheduler_loop(app)
assert calls == ["sync", "sleep:120"]
assert refreshes == [0.0, 10.0, 20.0, 30.0]
assert syncs == [0.0, 30.0]
assert sleeps == [10.0, 10.0, 10.0, 10.0]
@pytest.mark.asyncio
async def test_auto_sync_scheduler_refreshes_provider_between_syncs(monkeypatch) -> None:
now = 0.0
seen_providers: list[str] = []
syncs: list[str] = []
providers = iter(("openai", "local", "local"))
async def fake_refresh(_app):
provider = next(providers)
seen_providers.append(provider)
_app.state.llmwiki_runtime_signature = provider
return SimpleNamespace(
enabled=True,
auto_sync=True,
sync_interval_seconds=120,
embedding=SimpleNamespace(provider=provider),
)
async def fake_sync_once(_app, *, config=None) -> None:
syncs.append(config.embedding.provider)
async def fake_sleep(seconds: float) -> None:
nonlocal now
now += seconds
if len(seen_providers) >= 3:
raise asyncio.CancelledError
monkeypatch.setattr(llmwiki_index_scheduler, "monotonic", lambda: now)
monkeypatch.setattr(llmwiki_index_scheduler.asyncio, "sleep", fake_sleep)
monkeypatch.setattr(llmwiki_index_scheduler, "_sync_once", fake_sync_once)
monkeypatch.setattr(
llmwiki_index_scheduler,
"_refresh_local_wiki_config",
fake_refresh,
)
app = SimpleNamespace(
state=SimpleNamespace(
llmwiki_sync_service=object(),
llmwiki_store=object(),
)
)
with pytest.raises(asyncio.CancelledError):
await llmwiki_index_scheduler.local_wiki_index_scheduler_loop(app)
assert seen_providers == ["openai", "local", "local"]
assert syncs == ["openai", "local"]
def test_local_wiki_index_defaults_to_automatic_sync() -> None:
@@ -33,6 +33,7 @@ def test_load_returns_defaults_when_missing(tmp_runtime_home: Path) -> None:
assert settings.prompt_prefix.ordinary_qa_markdown_format_enabled is False
assert settings.citation_display.reference_mode_enabled is False
assert settings.citation_display.cleanup_words == []
assert settings.memory.enabled is False
def test_save_then_load_roundtrips(tmp_runtime_home: Path) -> None:
@@ -139,6 +140,22 @@ async def test_citation_display_update_preserves_unspecified_fields(
assert payload["citation_display"]["cleanup_words"] == ["广告", "免责声明"]
@pytest.mark.asyncio
async def test_admin_can_enable_memory_runtime_switch(
tmp_runtime_home: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
_patch_admin(monkeypatch)
response = await ss_router.update_memory_runtime_setting(
ss_router.MemoryRuntimeUpdateRequest(enabled=True),
SimpleNamespace(),
)
assert response.enabled is True
assert ss.load_system_settings().memory.enabled is True
def test_prepend_prefix_tags_first_human_without_touching_content() -> None:
graph_input = {"messages": [HumanMessage(content="评价一下中美贸易")]}
out = _prepend_prompt_prefix(graph_input, "你需要站在中方角度")
@@ -13,14 +13,20 @@
from __future__ import annotations
import asyncio
import json
from contextlib import asynccontextmanager
from types import SimpleNamespace
import pytest
import app.gateway.roundtable_model_fallback as mf
import app.gateway.routers.parallel_agents as parallel_agents
from app.gateway.roundtable_model_fallback import (
fallback_model_chain,
is_llm_error_message,
reason_text,
)
from app.gateway.routers._strategy_selection_seed import default_strategy_agent_roles
from deerflow.agents.middlewares.llm_error_handling_middleware import (
LLM_ERROR_MARKER,
LLMErrorHandlingMiddleware,
@@ -56,6 +62,520 @@ def test_is_llm_error_message_normal_is_none() -> None:
assert is_llm_error_message({"type": "ai", "content": ""}) is None
def test_parallel_agent_run_raises_instead_of_completing_llm_fallback(monkeypatch) -> None:
fallback_message = {
"type": "ai",
"content": "大模型本次调用未能成功完成,请稍后再试或更换模型。",
}
async def fake_stream_upstream(*_args, **_kwargs):
yield None, [f"data: {json.dumps({'messages': [fallback_message]}, ensure_ascii=False)}"]
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
async def collect() -> list[object]:
return [
item
async for item in parallel_agents._run_one_agent(
None, # type: ignore[arg-type]
"http://gateway.test",
{"Authorization": "Bearer test"},
"strategy-selection-intent",
"thread-id",
"test task",
"",
)
]
try:
asyncio.run(collect())
except RuntimeError as exc:
assert "模型调用失败" in str(exc)
else:
raise AssertionError("LLM fallback message must not be emitted as agent_done")
def test_parallel_agent_run_rejects_punctuation_only_delivery(monkeypatch) -> None:
invalid_message = {"type": "ai", "content": "!" * 500}
async def fake_stream_upstream(*_args, **_kwargs):
yield None, [f"data: {json.dumps({'messages': [invalid_message]}, ensure_ascii=False)}"]
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
async def collect() -> list[object]:
return [
item
async for item in parallel_agents._run_one_agent(
None, # type: ignore[arg-type]
"http://gateway.test",
{"Authorization": "Bearer test"},
"strategy-selection-angle-security",
"thread-id",
"test task",
"",
)
]
try:
asyncio.run(collect())
except RuntimeError as exc:
assert "未生成有效正文" in str(exc)
else:
raise AssertionError("Punctuation-only output must not be emitted as agent_done")
def test_parallel_agent_run_allows_empty_content_clarification(monkeypatch) -> None:
clarification_message = {
"type": "ai",
"content": "",
"tool_calls": [
{
"id": "clarify-1",
"name": "ask_clarification",
"args": {
"question": "请确认策略时间范围。",
"clarification_type": "single_choice",
"options": ["短期", "长期"],
"allow_custom": True,
},
}
],
}
async def fake_stream_upstream(*_args, **_kwargs):
yield None, [f"data: {json.dumps({'messages': [clarification_message]}, ensure_ascii=False)}"]
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
async def collect() -> list[object]:
return [
item
async for item in parallel_agents._run_one_agent(
None, # type: ignore[arg-type]
"http://gateway.test",
{"Authorization": "Bearer test"},
"strategy-selection-intent",
"thread-id",
"test task",
"",
)
]
events = asyncio.run(collect())
assert len(events) == 1
event = events[0]
assert isinstance(event, dict)
assert event["type"] == "clarification"
assert event["content"] == ""
assert event["clarification"]["question"] == "请确认策略时间范围。"
def test_parallel_agent_final_values_keep_typed_reasoning_out_of_content(monkeypatch) -> None:
final_message = {
"type": "ai",
"content": [
{"type": "thinking", "text": "核对约束与证据"},
{"type": "text", "text": "这是最终可见策略正文,包含目标、路径和验收标准。"},
],
}
async def fake_stream_upstream(*_args, **_kwargs):
yield None, [f"data: {json.dumps({'messages': [final_message]}, ensure_ascii=False)}"]
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
async def collect() -> list[object]:
return [
item
async for item in parallel_agents._run_one_agent(
None, # type: ignore[arg-type]
"http://gateway.test",
{"Authorization": "Bearer test"},
"strategy-selection-angle-security",
"thread-id",
"test task",
"",
)
]
events = asyncio.run(collect())
assert len(events) == 1
event = events[0]
assert isinstance(event, dict)
assert event["content"] == "这是最终可见策略正文,包含目标、路径和验收标准。"
assert event["reasoning"] == "核对约束与证据"
def test_parallel_agent_final_values_recovers_template_injected_reasoning(monkeypatch) -> None:
final_message = {
"type": "ai",
"content": "模板注入的内部推理</think>这是最终可见策略正文,包含目标、路径和验收标准。",
}
async def fake_stream_upstream(*_args, **_kwargs):
yield None, [f"data: {json.dumps({'messages': [final_message]}, ensure_ascii=False)}"]
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
async def collect() -> list[object]:
return [
item
async for item in parallel_agents._run_one_agent(
None, # type: ignore[arg-type]
"http://gateway.test",
{"Authorization": "Bearer test"},
"strategy-selection-angle-security",
"thread-id",
"test task",
"",
)
]
events = asyncio.run(collect())
event = events[0]
assert isinstance(event, dict)
assert event["content"] == "这是最终可见策略正文,包含目标、路径和验收标准。"
assert event["reasoning"] == "模板注入的内部推理"
def test_parallel_agent_substantive_content_accepts_chinese_and_english() -> None:
assert parallel_agents._has_substantive_content("这是一份包含目标、前置条件、阶段路径和验收标准的完整策略。")
assert parallel_agents._has_substantive_content("A complete strategy with objectives, prerequisites, milestones, and measurable outcomes.")
assert not parallel_agents._has_substantive_content("<think>internal reasoning only</think>!!!")
def test_parallel_agent_extracts_provider_reasoning_without_mixing_answer() -> None:
answer, reasoning = parallel_agents._extract_ai_stream_parts(
{
"type": "AIMessageChunk",
"content": [
{"type": "thinking_delta", "text": "先核对边界"},
{"type": "text", "text": "可见正文"},
],
"additional_kwargs": {"reasoning_content": ",再比较路径"},
}
)
assert answer == "可见正文"
assert reasoning == "先核对边界,再比较路径"
answer, reasoning = parallel_agents._extract_ai_stream_parts(
{
"type": "AIMessageChunk",
"content": [
{"type": "thinking", "thinking": "结构化思考字段"},
{"type": "text", "text": "最终正文"},
],
}
)
assert answer == "最终正文"
assert reasoning == "结构化思考字段"
class _EnabledStrategyRoleStore:
async def ensure_defaults(self, _rows) -> int:
return 0
async def list_roles(self, *, include_disabled: bool) -> list[dict[str, object]]:
del include_disabled
return [dict(row) for row in default_strategy_agent_roles()]
async def get_role(self, agent_id: str) -> dict[str, object]:
return {"id": agent_id, "enabled": True}
class _GlobalStrategyAgentStore:
async def get_any(self, agent_id: str) -> dict[str, object]:
return {"id": agent_id, "user_id": None}
def _parallel_request() -> SimpleNamespace:
return SimpleNamespace(
app=SimpleNamespace(
state=SimpleNamespace(
strategy_agent_role_store=_EnabledStrategyRoleStore(),
agent_store=_GlobalStrategyAgentStore(),
)
)
)
def test_parallel_agent_stream_separates_inline_and_provider_reasoning(monkeypatch) -> None:
async def fake_run_one_agent(*_args, **_kwargs):
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': '<thi'}], ensure_ascii=False)}"
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': 'nk>内部分析</think>方案正文'}], ensure_ascii=False)}"
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': '', 'additional_kwargs': {'reasoning_content': '补充推理'}}], ensure_ascii=False)}"
yield {
"status": "done",
"content": "<think>内部分析</think>方案正文",
"agent_name": "strategy-selection-angle-security",
"tool_calls": [],
}
@asynccontextmanager
async def fake_client(*_args, **_kwargs):
yield object()
async def fake_create_thread(*_args, **_kwargs):
return "thread-id"
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
async def collect() -> list[dict[str, object]]:
response = await parallel_agents.run_parallel_agents_stream(
_parallel_request(),
{
"task_name": "test task",
"brief": "test brief",
"agent_ids": ["strategy-selection-angle-security"],
},
)
events: list[dict[str, object]] = []
async for chunk in response.body_iterator:
text = chunk.decode() if isinstance(chunk, bytes) else chunk
for line in text.splitlines():
if line.startswith("data:"):
events.append(json.loads(line[5:].strip()))
return events
events = asyncio.run(collect())
visible = "".join(str(event.get("text") or "") for event in events if event.get("type") == "delta")
thinking = "".join(str(event.get("text") or "") for event in events if event.get("type") == "reasoning_delta")
done = next(event for event in events if event.get("type") == "agent_done")
assert visible == "方案正文"
assert thinking == "内部分析补充推理"
assert done["content"] == "方案正文"
@pytest.mark.parametrize(
("buffered_content", "expected_error"),
[
("大模型本次调用未能成功完成,请稍后再试或更换模型。", "模型调用失败"),
("", "未生成有效正文"),
("<think>internal reasoning only</think>", "未生成有效正文"),
("!" * 500, "未生成有效正文"),
],
)
def test_parallel_agent_truncated_stream_rejects_invalid_buffer(
monkeypatch,
buffered_content: str,
expected_error: str,
) -> None:
async def fake_run_one_agent(*_args, **_kwargs):
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': buffered_content}], ensure_ascii=False)}"
@asynccontextmanager
async def fake_client(*_args, **_kwargs):
yield object()
async def fake_create_thread(*_args, **_kwargs):
return "thread-id"
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
async def collect() -> list[dict[str, object]]:
response = await parallel_agents.run_parallel_agents_stream(
_parallel_request(),
{
"task_name": "test task",
"brief": "test brief",
"agent_ids": ["strategy-selection-intent"],
},
)
events: list[dict[str, object]] = []
async for chunk in response.body_iterator:
text = chunk.decode() if isinstance(chunk, bytes) else chunk
for line in text.splitlines():
if line.startswith("data:"):
events.append(json.loads(line[5:].strip()))
return events
events = asyncio.run(collect())
assert not any(event.get("type") == "agent_done" for event in events)
errors = [event for event in events if event.get("type") == "agent_error"]
assert len(errors) == 1
assert expected_error in str(errors[0].get("message"))
def test_parallel_agent_plain_text_streams_each_chunk_without_probe_delay(
monkeypatch,
) -> None:
chunks = [
"This visible strategy ",
"includes objectives and execution paths, ",
"plus measurable acceptance criteria.",
]
async def fake_run_one_agent(*_args, **_kwargs):
for content in chunks:
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': content}], ensure_ascii=False)}"
@asynccontextmanager
async def fake_client(*_args, **_kwargs):
yield object()
async def fake_create_thread(*_args, **_kwargs):
return "thread-id"
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
monkeypatch.setattr(
parallel_agents,
"_loopback_base",
lambda _request: "http://gateway.test",
)
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
monkeypatch.setattr(
parallel_agents,
"_should_probe_implicit_reasoning",
lambda *_args: False,
)
async def collect() -> list[dict[str, object]]:
response = await parallel_agents.run_parallel_agents_stream(
_parallel_request(),
{
"task_name": "plain stream",
"brief": "plain answer chunks must stay live",
"agent_ids": ["strategy-selection-intent"],
},
)
events: list[dict[str, object]] = []
async for chunk in response.body_iterator:
text = chunk.decode() if isinstance(chunk, bytes) else chunk
for line in text.splitlines():
if line.startswith("data:"):
events.append(json.loads(line[5:].strip()))
return events
events = asyncio.run(collect())
assert [event["text"] for event in events if event.get("type") == "delta"] == chunks
done = next(event for event in events if event.get("type") == "agent_done")
assert done["content"] == "".join(chunks)
def test_parallel_agent_typed_reasoning_unlocks_live_answer_channel(monkeypatch) -> None:
chunks = [
"This visible strategy ",
"streams after typed reasoning, ",
"with objectives and acceptance criteria.",
]
async def fake_run_one_agent(*_args, **_kwargs):
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': '', 'additional_kwargs': {'reasoning_content': 'live reasoning'}}])}"
for content in chunks:
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': content}])}"
@asynccontextmanager
async def fake_client(*_args, **_kwargs):
yield object()
async def fake_create_thread(*_args, **_kwargs):
return "thread-id"
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
monkeypatch.setattr(parallel_agents, "_should_probe_implicit_reasoning", lambda *_args: True)
async def collect() -> list[dict[str, object]]:
response = await parallel_agents.run_parallel_agents_stream(
_parallel_request(),
{
"task_name": "typed reasoning stream",
"brief": "answer chunks must stay live",
"agent_ids": ["strategy-selection-intent"],
},
)
events: list[dict[str, object]] = []
async for chunk in response.body_iterator:
text = chunk.decode() if isinstance(chunk, bytes) else chunk
for line in text.splitlines():
if line.startswith("data:"):
events.append(json.loads(line[5:].strip()))
return events
events = asyncio.run(collect())
assert [event["text"] for event in events if event.get("type") == "delta"] == chunks
assert "".join(str(event.get("text") or "") for event in events if event.get("type") == "reasoning_delta") == "live reasoning"
def test_parallel_agent_truncated_close_only_stream_separates_reasoning(monkeypatch) -> None:
async def fake_run_one_agent(*_args, **_kwargs):
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': 'internal '}], ensure_ascii=False)}"
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': 'reasoning</thi'}], ensure_ascii=False)}"
yield "event: messages"
yield f"data: {json.dumps([{'type': 'ai', 'content': 'nk>This visible strategy includes objectives, execution paths, and acceptance criteria.'}], ensure_ascii=False)}"
@asynccontextmanager
async def fake_client(*_args, **_kwargs):
yield object()
async def fake_create_thread(*_args, **_kwargs):
return "thread-id"
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
monkeypatch.setattr(
parallel_agents,
"_should_probe_implicit_reasoning",
lambda *_args: True,
)
async def collect() -> list[dict[str, object]]:
response = await parallel_agents.run_parallel_agents_stream(
_parallel_request(),
{
"task_name": "test task",
"brief": "test brief",
"agent_ids": ["strategy-selection-intent"],
},
)
events: list[dict[str, object]] = []
async for chunk in response.body_iterator:
text = chunk.decode() if isinstance(chunk, bytes) else chunk
for line in text.splitlines():
if line.startswith("data:"):
events.append(json.loads(line[5:].strip()))
return events
events = asyncio.run(collect())
done = next(event for event in events if event.get("type") == "agent_done")
reasoning = "".join(str(event.get("text") or "") for event in events if event.get("type") == "reasoning_delta")
visible = "".join(str(event.get("text") or "") for event in events if event.get("type") == "delta")
assert done["content"] == ("This visible strategy includes objectives, execution paths, and acceptance criteria.")
assert visible == done["content"]
assert reasoning == "internal reasoning"
# ── 中间件给兜底 AIMessage 打标记 ────────────────────────────────────────────
@@ -0,0 +1,46 @@
from __future__ import annotations
from pathlib import Path
from deerflow.config.app_config import AppConfig
def test_relative_sandbox_mount_is_resolved_from_config_directory(tmp_path: Path) -> None:
knowledge_dir = tmp_path / "knowledge" / "knowledge_base"
knowledge_dir.mkdir(parents=True)
config_path = tmp_path / "config.yaml"
config_path.write_text(
"""
sandbox:
use: deerflow.sandbox.local:LocalSandboxProvider
mounts:
- host_path: knowledge/knowledge_base
container_path: /mnt/knowledge
read_only: true
""".strip(),
encoding="utf-8",
)
config = AppConfig.from_file(str(config_path))
assert config.sandbox.mounts[0].host_path == str(knowledge_dir.resolve())
assert Path(config.sandbox.mounts[0].host_path).is_absolute()
def test_absolute_sandbox_mount_is_preserved(tmp_path: Path) -> None:
absolute_mount = (tmp_path / "shared").resolve()
config_data = {
"sandbox": {
"use": "deerflow.sandbox.local:LocalSandboxProvider",
"mounts": [
{
"host_path": str(absolute_mount),
"container_path": "/mnt/shared",
}
],
}
}
AppConfig._resolve_relative_mount_paths(config_data, tmp_path / "elsewhere")
assert config_data["sandbox"]["mounts"][0]["host_path"] == str(absolute_mount)
@@ -0,0 +1,667 @@
"""Global strategy-agent role CRUD, permissions and visibility tests."""
from __future__ import annotations
import asyncio
from pathlib import Path
from types import SimpleNamespace
import pytest
import yaml
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway.routers import _strategy_selection_seed as seed
from app.gateway.routers import agents as agents_router
from app.gateway.routers import parallel_agents, strategy_agents
from deerflow.config import agents_config
from deerflow.config.paths import Paths
from deerflow.persistence.agents import MemoryAgentStore
from deerflow.persistence.skills.memory import MemorySkillStore
from deerflow.persistence.strategy_agent_roles import MemoryStrategyAgentRoleStore, StrategyAgentRoleRepository
from deerflow.persistence.strategy_agent_roles.model import StrategyAgentRoleRow
from deerflow.skills.types import Skill, SkillCategory
class _SkillStorage:
def __init__(self, skills: list[Skill]) -> None:
self._skills = skills
def load_skills(self, *, enabled_only: bool) -> list[Skill]:
if enabled_only:
return [skill for skill in self._skills if skill.enabled]
return list(self._skills)
def _skill(name: str, category: SkillCategory = SkillCategory.PUBLIC) -> Skill:
directory = Path("skills") / category.value / name
return Skill(
name=name,
description=name,
license=None,
skill_dir=directory,
skill_file=directory / "SKILL.md",
relative_path=Path(name),
category=category,
enabled=True,
)
def test_generic_agent_namespace_guard_is_case_insensitive_and_canonical() -> None:
assert agents_router._is_strategy_agent_id("STRATEGY-SELECTION-INTENT")
assert strategy_agents._process_lock("STRATEGY-SELECTION-INTENT") is strategy_agents._process_lock("strategy-selection-intent")
with pytest.raises(HTTPException) as exc_info:
agents_router._require_canonical_record_id(
"STRATEGY-SELECTION-INTENT",
{"id": "strategy-selection-intent"},
)
assert exc_info.value.status_code == 409
@pytest.mark.asyncio
async def test_generic_mutations_reject_strategy_namespace_before_storage(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
agents_router,
"get_agents_api_config",
lambda: SimpleNamespace(enabled=True),
)
request = SimpleNamespace()
with pytest.raises(HTTPException) as update_error:
await agents_router.update_agent(
seed.STRATEGY_INTENT_AGENT_ID.upper(),
agents_router.AgentUpdateRequest(name="bypass"),
request,
)
assert update_error.value.status_code == 409
assert "/api/strategy-agents/" in str(update_error.value.detail)
with pytest.raises(HTTPException) as delete_error:
await agents_router.delete_agent(
seed.STRATEGY_INTENT_AGENT_ID.upper(),
request,
)
assert delete_error.value.status_code == 409
assert "generic API" in str(delete_error.value.detail)
@pytest.mark.asyncio
async def test_strategy_mutation_rejects_case_insensitive_database_alias(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, role_store = await _request(tmp_path, monkeypatch)
canonical_id = seed.STRATEGY_INTENT_AGENT_ID
await role_store.ensure_defaults(seed.default_strategy_agent_roles())
role = await role_store.get_role(canonical_id)
record = await agent_store.get_any(canonical_id)
assert role is not None and record is not None
monkeypatch.setattr(strategy_agents, "_ensure_registry", lambda *_: _async_none())
monkeypatch.setattr(role_store, "get_role", lambda _: _async_value(role))
monkeypatch.setattr(agent_store, "get_any", lambda _: _async_value(record))
with pytest.raises(HTTPException) as exc_info:
await strategy_agents._get_role_and_agent(request, canonical_id.upper())
assert exc_info.value.status_code == 409
async def _async_none() -> None:
return None
async def _async_value(value):
return value
async def _request(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
*,
role: str = "admin",
) -> tuple[SimpleNamespace, MemoryAgentStore, MemoryStrategyAgentRoleStore]:
paths = Paths(tmp_path)
monkeypatch.setattr(seed, "get_paths", lambda: paths)
monkeypatch.setattr(agents_router, "get_paths", lambda: paths)
monkeypatch.setattr(strategy_agents, "get_paths", lambda: paths)
monkeypatch.setattr(agents_config, "get_paths", lambda: paths)
seed.ensure_strategy_selection_agents()
agent_store = MemoryAgentStore()
for agent_id in seed.required_agent_ids():
config = yaml.safe_load((paths.agent_dir(agent_id) / "config.yaml").read_text(encoding="utf-8"))
await agent_store.ensure_builtin(
{
"id": agent_id,
"name": config["name"],
"description": config.get("description") or "",
"published": True,
}
)
role_store = MemoryStrategyAgentRoleStore()
skill_store = MemorySkillStore()
storage = _SkillStorage(
[
_skill("public-skill"),
_skill("published-custom", SkillCategory.CUSTOM),
_skill("private-custom", SkillCategory.CUSTOM),
]
)
await skill_store.create(
{
"name": "published-custom",
"owner_user_id": "publisher",
"published": True,
}
)
await skill_store.create(
{
"name": "private-custom",
"owner_user_id": "owner",
"published": False,
}
)
monkeypatch.setattr(agents_router, "get_or_new_skill_storage", lambda **_: storage)
monkeypatch.setattr(agents_router, "get_skill_usage_entries", lambda: {})
state = SimpleNamespace(
agent_store=agent_store,
strategy_agent_role_store=role_store,
skill_store=skill_store,
config=SimpleNamespace(models=[SimpleNamespace(name="model-a")]),
)
request = SimpleNamespace(
app=SimpleNamespace(state=state),
state=SimpleNamespace(
user=SimpleNamespace(id="actor", system_role=role),
),
)
return request, agent_store, role_store
@pytest.mark.asyncio
async def test_catalog_has_fixed_categories_and_redacts_public_config(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, role_store = await _request(tmp_path, monkeypatch)
public = await strategy_agents.list_strategy_agents(request)
assert len(public.agents) == 9
assert [(item.category, item.fixed) for item in public.agents if item.fixed] == [
("intent", True),
("judge", True),
("solution", True),
]
assert all(item.model is None and item.soul is None and item.skills is None for item in public.agents)
await role_store.update_role(seed.STRATEGY_SECURITY_AGENT_ID, enabled=False)
active = await strategy_agents.list_strategy_agents(request)
assert seed.STRATEGY_SECURITY_AGENT_ID not in {item.id for item in active.agents}
admin = await strategy_agents.list_strategy_agents(
request,
include_disabled=True,
include_config=True,
)
disabled = next(item for item in admin.agents if item.id == seed.STRATEGY_SECURITY_AGENT_ID)
assert disabled.enabled is False
assert disabled.soul
assert disabled.skills == ["policy-knowledge"]
@pytest.mark.asyncio
async def test_catalog_rejects_user_owned_reserved_default(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, _ = await _request(tmp_path, monkeypatch)
await agent_store.delete_builtin(seed.STRATEGY_INTENT_AGENT_ID)
await agent_store.create(
{
"id": seed.STRATEGY_INTENT_AGENT_ID,
"name": "collision",
"user_id": "attacker",
}
)
with pytest.raises(HTTPException) as exc_info:
await strategy_agents.list_strategy_agents(request)
assert exc_info.value.status_code == 409
assert "owned by a user" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_sensitive_catalog_and_writes_are_admin_only(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, _ = await _request(tmp_path, monkeypatch, role="user")
# The active catalog is intentionally readable by the workbench.
assert len((await strategy_agents.list_strategy_agents(request)).agents) == 9
with pytest.raises(HTTPException) as list_error:
await strategy_agents.list_strategy_agents(
request,
include_disabled=True,
include_config=True,
)
assert list_error.value.status_code == 403
with pytest.raises(HTTPException) as create_error:
await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(name="非法新增"),
request,
)
assert create_error.value.status_code == 403
@pytest.mark.asyncio
async def test_angle_crud_is_global_and_delete_is_soft(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, role_store = await _request(tmp_path, monkeypatch)
created = await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="科技产业视角",
description="评估关键技术和产业链韧性",
soul="你是科技产业策略分析智能体。",
model="model-a",
skills=["public-skill", "published-custom"],
),
request,
)
assert created.category == "angle"
assert created.fixed is False
assert created.enabled is True
assert created.id.startswith(seed.STRATEGY_ANGLE_AGENT_PREFIX)
assert created.model == "model-a"
assert created.skills == ["public-skill", "published-custom"]
updated = await strategy_agents.update_strategy_agent(
created.id,
strategy_agents.StrategyAgentUpdateRequest(
name="科技与产业链视角",
enabled=False,
sort_order=123,
skills=["public-skill"],
),
request,
)
assert updated.name == "科技与产业链视角"
assert updated.enabled is False
assert updated.sort_order == 123
assert updated.skills == ["public-skill"]
deleted = await strategy_agents.disable_strategy_angle(created.id, request)
assert deleted.enabled is False
# Soft delete protects saved projects and permits an administrator to
# restore the angle without recreating its ID or files.
assert await agent_store.get_any(created.id) is not None
assert await role_store.get_role(created.id) is not None
restored = await strategy_agents.update_strategy_agent(
created.id,
strategy_agents.StrategyAgentUpdateRequest(enabled=True),
request,
)
assert restored.enabled is True
assert (tmp_path / "agents" / created.id / ".strategy-role.yaml").is_file()
@pytest.mark.asyncio
async def test_memory_mode_recovers_dynamic_angle_from_folder_sidecar(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, _ = await _request(tmp_path, monkeypatch)
created = await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="恢复测试角度",
sort_order=177,
),
request,
)
await strategy_agents.disable_strategy_angle(created.id, request)
request.app.state.agent_store = MemoryAgentStore()
request.app.state.strategy_agent_role_store = MemoryStrategyAgentRoleStore()
recovered = await strategy_agents.list_strategy_agents(
request,
include_disabled=True,
include_config=True,
)
item = next(entry for entry in recovered.agents if entry.id == created.id)
assert item.name == "恢复测试角度"
assert item.enabled is False
assert item.sort_order == 177
assert (await request.app.state.agent_store.get_any(created.id))["user_id"] is None
@pytest.mark.asyncio
async def test_memory_mode_sidecar_keeps_default_angle_disabled_after_restart(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, _ = await _request(tmp_path, monkeypatch)
await strategy_agents.update_strategy_agent(
seed.STRATEGY_SECURITY_AGENT_ID,
strategy_agents.StrategyAgentUpdateRequest(enabled=False, sort_order=188),
request,
)
request.app.state.agent_store = MemoryAgentStore()
request.app.state.strategy_agent_role_store = MemoryStrategyAgentRoleStore()
with pytest.raises(HTTPException) as run_error:
await parallel_agents.run_parallel_agents_stream(
request,
{
"task_name": "restart gate",
"brief": "disabled default must stay disabled",
"agent_ids": [seed.STRATEGY_SECURITY_AGENT_ID],
},
)
assert run_error.value.status_code == 409
recovered = await strategy_agents.list_strategy_agents(
request,
include_disabled=True,
include_config=True,
)
item = next(entry for entry in recovered.agents if entry.id == seed.STRATEGY_SECURITY_AGENT_ID)
assert item.enabled is False
assert item.sort_order == 188
@pytest.mark.asyncio
async def test_cancelled_create_compensates_every_persistence_layer(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, role_store = await _request(tmp_path, monkeypatch)
generated_id = f"{seed.STRATEGY_ANGLE_AGENT_PREFIX}cancelled"
monkeypatch.setattr(
strategy_agents,
"uuid4",
lambda: SimpleNamespace(hex="cancelled"),
)
async def cancel_set_extras(*_args, **_kwargs):
raise asyncio.CancelledError
monkeypatch.setattr(agent_store, "set_extras", cancel_set_extras)
with pytest.raises(asyncio.CancelledError):
await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(name="cancelled create"),
request,
)
assert await agent_store.get_any(generated_id) is None
assert await role_store.get_role(generated_id) is None
assert not (tmp_path / "agents" / generated_id).exists()
@pytest.mark.asyncio
async def test_cross_process_file_lock_excludes_second_handle(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
paths = Paths(tmp_path)
monkeypatch.setattr(strategy_agents, "get_paths", lambda: paths)
first = strategy_agents._open_cross_process_lock(seed.STRATEGY_INTENT_AGENT_ID)
second = strategy_agents._open_cross_process_lock(seed.STRATEGY_INTENT_AGENT_ID)
try:
assert strategy_agents._try_cross_process_lock(first) is True
assert strategy_agents._try_cross_process_lock(second) is False
strategy_agents._release_cross_process_lock(first)
first = None
assert strategy_agents._try_cross_process_lock(second) is True
finally:
if first is not None:
strategy_agents._release_cross_process_lock(first)
if not second.closed:
strategy_agents._release_cross_process_lock(second)
@pytest.mark.asyncio
async def test_update_failure_restores_files_cache_and_role(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, role_store = await _request(tmp_path, monkeypatch)
created = await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="事务前名称",
model="model-a",
skills=["public-skill"],
sort_order=166,
),
request,
)
agent_dir = tmp_path / "agents" / created.id
before_files = {name: (agent_dir / name).read_text(encoding="utf-8") for name in ("config.yaml", "SOUL.md", ".strategy-role.yaml")}
real_update = role_store.update_role
failed = False
async def fail_once(agent_id: str, **kwargs):
nonlocal failed
if kwargs.get("sort_order") == 188 and not failed:
failed = True
raise RuntimeError("injected role-store failure")
return await real_update(agent_id, **kwargs)
monkeypatch.setattr(role_store, "update_role", fail_once)
with pytest.raises(RuntimeError, match="injected role-store failure"):
await strategy_agents.update_strategy_agent(
created.id,
strategy_agents.StrategyAgentUpdateRequest(
name="不应留下的名称",
soul="不应留下的 SOUL",
skills=[],
sort_order=188,
),
request,
)
record = await agent_store.get_any(created.id)
role = await role_store.get_role(created.id)
assert record is not None and record["name"] == "事务前名称"
assert role is not None and role["sort_order"] == 166
assert (await agent_store.get_extras_for([created.id]))[created.id]["skills"] == ["public-skill"]
assert {name: (agent_dir / name).read_text(encoding="utf-8") for name in ("config.yaml", "SOUL.md", ".strategy-role.yaml")} == before_files
@pytest.mark.asyncio
async def test_fixed_roles_cannot_be_disabled_or_deleted(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, _ = await _request(tmp_path, monkeypatch)
with pytest.raises(HTTPException) as update_error:
await strategy_agents.update_strategy_agent(
seed.STRATEGY_INTENT_AGENT_ID,
strategy_agents.StrategyAgentUpdateRequest(enabled=False),
request,
)
assert update_error.value.status_code == 409
with pytest.raises(HTTPException) as delete_error:
await strategy_agents.disable_strategy_angle(
seed.STRATEGY_JUDGE_AGENT_ID,
request,
)
assert delete_error.value.status_code == 409
@pytest.mark.asyncio
async def test_model_and_global_skill_visibility_are_validated(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, _ = await _request(tmp_path, monkeypatch)
with pytest.raises(HTTPException) as model_error:
await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="未知模型",
model="not-configured",
),
request,
)
assert model_error.value.status_code == 422
with pytest.raises(HTTPException) as skill_error:
await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="私有技能",
skills=["private-custom"],
),
request,
)
assert skill_error.value.status_code == 422
assert "globally available" in str(skill_error.value.detail)
defaulted = await strategy_agents.create_strategy_angle(
strategy_agents.StrategyAngleCreateRequest(
name="默认角色模板",
description="关注跨领域风险",
),
request,
)
assert defaulted.soul
assert "默认角色模板" in defaulted.soul
assert "不替代其他角度" in defaulted.soul
@pytest.mark.asyncio
async def test_disabled_strategy_agent_cannot_be_started_via_parallel_api(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, _, role_store = await _request(tmp_path, monkeypatch)
await role_store.ensure_defaults(seed.default_strategy_agent_roles())
await role_store.update_role(seed.STRATEGY_SECURITY_AGENT_ID, enabled=False)
with pytest.raises(HTTPException) as exc_info:
await parallel_agents.run_parallel_agents_stream(
request,
{
"task_name": "测试",
"brief": "验证停用闸门",
"agent_ids": [seed.STRATEGY_SECURITY_AGENT_ID],
},
)
assert exc_info.value.status_code == 409
@pytest.mark.asyncio
async def test_parallel_api_rejects_user_owned_strategy_backing_agent(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
request, agent_store, role_store = await _request(tmp_path, monkeypatch)
await role_store.ensure_defaults(seed.default_strategy_agent_roles())
await agent_store.delete_builtin(seed.STRATEGY_SECURITY_AGENT_ID)
await agent_store.create(
{
"id": seed.STRATEGY_SECURITY_AGENT_ID,
"name": "collision",
"user_id": "attacker",
}
)
with pytest.raises(HTTPException) as exc_info:
await parallel_agents.run_parallel_agents_stream(
request,
{
"task_name": "测试",
"brief": "验证归属闸门",
"agent_ids": [seed.STRATEGY_SECURITY_AGENT_ID],
},
)
assert exc_info.value.status_code == 409
assert "owned by a user" in str(exc_info.value.detail)
@pytest.mark.asyncio
async def test_memory_registry_repairs_corrupted_fixed_role_metadata() -> None:
store = MemoryStrategyAgentRoleStore()
await store.create_role(
{
"agent_id": seed.STRATEGY_INTENT_AGENT_ID,
"category": "angle",
"fixed": False,
"enabled": False,
"sort_order": 999,
}
)
await store.ensure_defaults(seed.default_strategy_agent_roles())
repaired = await store.get_role(seed.STRATEGY_INTENT_AGENT_ID)
assert repaired is not None
assert repaired["category"] == "intent"
assert repaired["fixed"] is True
assert repaired["enabled"] is True
assert repaired["sort_order"] == 0
@pytest.mark.asyncio
async def test_sql_registry_preserves_disabled_defaults_and_soft_records(
tmp_path: Path,
) -> None:
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'strategy-agent-roles.db'}")
async with engine.begin() as connection:
await connection.run_sync(StrategyAgentRoleRow.__table__.create)
store = StrategyAgentRoleRepository(async_sessionmaker(engine, expire_on_commit=False))
try:
defaults = seed.default_strategy_agent_roles()
assert await store.ensure_defaults(defaults) == 9
assert await store.ensure_defaults(defaults) == 0
await store.update_role(seed.STRATEGY_SECURITY_AGENT_ID, enabled=False)
# Re-seeding must never undo an administrator's soft delete.
assert await store.ensure_defaults(defaults) == 0
disabled = await store.get_role(seed.STRATEGY_SECURITY_AGENT_ID)
assert disabled is not None
assert disabled["enabled"] is False
# Canonical fixed-role invariants are repaired, unlike angle lifecycle
# state which intentionally remains administrator-controlled.
async with store._sf() as session:
row = await session.get(
StrategyAgentRoleRow,
seed.STRATEGY_INTENT_AGENT_ID,
)
row.category = "angle"
row.fixed = False
row.enabled = False
row.sort_order = 999
await session.commit()
assert await store.ensure_defaults(defaults) == 0
repaired = await store.get_role(seed.STRATEGY_INTENT_AGENT_ID)
assert repaired is not None
assert repaired["category"] == "intent"
assert repaired["fixed"] is True
assert repaired["enabled"] is True
assert repaired["sort_order"] == 0
dynamic = await store.create_role(
{
"agent_id": "strategy-selection-angle-sql-test",
"category": "angle",
"fixed": False,
"enabled": True,
"sort_order": 150,
}
)
assert dynamic["category"] == "angle"
assert await store.update_role(dynamic["agent_id"], enabled=False)
assert await store.delete_role(dynamic["agent_id"]) is True
assert await store.get_role(dynamic["agent_id"]) is None
finally:
await engine.dispose()
@@ -0,0 +1,81 @@
import asyncio
from datetime import UTC, datetime
from types import SimpleNamespace
import pytest
from fastapi import HTTPException
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway.routers import parallel_agent_tasks
from deerflow.persistence.base import Base
from deerflow.persistence.parallel_agent_tasks import ParallelAgentTaskRepository
@pytest.mark.asyncio
async def test_parallel_task_repository_rejects_stale_revision(tmp_path) -> None:
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'strategy-project-cas.db'}")
async with engine.begin() as connection:
await connection.run_sync(Base.metadata.create_all)
store = ParallelAgentTaskRepository(async_sessionmaker(engine, expire_on_commit=False))
try:
original = await store.create_task(
"user-1",
{
"id": "strategy-selection-cas",
"title": "original",
"messages": [{"snapshot": "original"}],
},
)
await asyncio.sleep(0.002)
newer = await store.update_task(
"strategy-selection-cas",
"user-1",
title="newer",
messages=[{"snapshot": "newer"}],
expected_updated_at=original["updated_at"],
)
assert newer is not None
stale = await store.update_task(
"strategy-selection-cas",
"user-1",
title="stale",
messages=[{"snapshot": "stale"}],
expected_updated_at=original["updated_at"],
)
assert stale is None
final = await store.get_task("strategy-selection-cas", "user-1")
assert final is not None
assert final["title"] == "newer"
assert final["messages"] == [{"snapshot": "newer"}]
finally:
await engine.dispose()
@pytest.mark.asyncio
async def test_parallel_task_route_reports_revision_conflict() -> None:
class ConflictStore:
async def update_task(self, *_args, **_kwargs):
return None
async def get_task(self, *_args, **_kwargs):
return {"id": "strategy-selection-cas"}
request = SimpleNamespace(
state=SimpleNamespace(user=SimpleNamespace(id="user-1")),
app=SimpleNamespace(state=SimpleNamespace(parallel_agent_task_store=ConflictStore())),
)
body = parallel_agent_tasks.TaskUpdateRequest(
title="stale",
expected_updated_at=datetime.now(UTC),
)
with pytest.raises(HTTPException) as error:
await parallel_agent_tasks.update_task(
request,
"strategy-selection-cas",
body,
)
assert error.value.status_code == 409
assert "其他页面更新" in str(error.value.detail)
@@ -0,0 +1,72 @@
from types import SimpleNamespace
import pytest
from fastapi import HTTPException
from app.gateway.routers import system_settings as system_settings_router
from deerflow.config.system_settings import SystemSettings
def test_strategy_project_demo_is_hidden_by_default() -> None:
assert SystemSettings().strategy_projects.taiwan_strait_demo_enabled is False
@pytest.mark.asyncio
async def test_non_admin_cannot_update_strategy_project_settings(
monkeypatch: pytest.MonkeyPatch,
) -> None:
async def ordinary_user(_request):
return SimpleNamespace(system_role="user")
monkeypatch.setattr(
system_settings_router,
"get_optional_user_from_request",
ordinary_user,
)
with pytest.raises(HTTPException) as error:
await system_settings_router.update_strategy_project_settings(
system_settings_router.StrategyProjectSettingsUpdateRequest(
taiwan_strait_demo_enabled=True,
),
SimpleNamespace(),
)
assert error.value.status_code == 403
@pytest.mark.asyncio
async def test_admin_can_update_strategy_project_settings(
monkeypatch: pytest.MonkeyPatch,
) -> None:
current = SystemSettings()
saved: list[SystemSettings] = []
async def admin_user(_request):
return SimpleNamespace(system_role="admin")
monkeypatch.setattr(
system_settings_router,
"get_optional_user_from_request",
admin_user,
)
monkeypatch.setattr(
system_settings_router,
"load_system_settings",
lambda: current,
)
monkeypatch.setattr(
system_settings_router,
"save_system_settings",
lambda settings: saved.append(settings) or settings,
)
response = await system_settings_router.update_strategy_project_settings(
system_settings_router.StrategyProjectSettingsUpdateRequest(
taiwan_strait_demo_enabled=True,
),
SimpleNamespace(),
)
assert response.taiwan_strait_demo_enabled is True
assert saved[0].strategy_projects.taiwan_strait_demo_enabled is True
@@ -0,0 +1,276 @@
"""Regression tests for the strategy-selection built-in agent seed."""
from __future__ import annotations
from pathlib import Path
import pytest
import yaml
from fastapi import HTTPException
from app.gateway.routers import _strategy_selection_seed as seed
from app.gateway.routers import agents as agents_router
from deerflow.config.paths import Paths
from deerflow.persistence.skills.memory import MemorySkillStore
from deerflow.skills.types import Skill, SkillCategory
@pytest.fixture()
def tmp_paths(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Paths:
paths = Paths(tmp_path)
monkeypatch.setattr(seed, "get_paths", lambda: paths)
return paths
def _asset(agent_id: str, filename: str) -> Path:
return seed._ASSETS_DIR / agent_id / filename
def test_seed_assets_define_all_nine_functional_agents() -> None:
agent_ids = tuple(seed.required_agent_ids())
assert len(agent_ids) == 9
assert len(set(agent_ids)) == 9
assert agent_ids[0] == seed.STRATEGY_INTENT_AGENT_ID
assert agent_ids[-2:] == (
seed.STRATEGY_JUDGE_AGENT_ID,
seed.STRATEGY_SOLUTION_AGENT_ID,
)
assert len(seed.STRATEGY_ANGLE_AGENT_IDS) == 6
for agent_id in agent_ids:
config_path = _asset(agent_id, "config.yaml")
soul_path = _asset(agent_id, "SOUL.md")
assert config_path.is_file(), agent_id
assert soul_path.is_file(), agent_id
config = yaml.safe_load(config_path.read_text(encoding="utf-8"))
soul = soul_path.read_text(encoding="utf-8")
assert config["id"] == agent_id
assert config["name"].strip()
assert config["description"].strip()
assert "作战战术" in soul
assert "定向政治说服" in soul
assert "降级" in soul
def test_seed_assets_use_role_appropriate_existing_skills_and_tools() -> None:
intent = yaml.safe_load(
_asset(seed.STRATEGY_INTENT_AGENT_ID, "config.yaml").read_text(encoding="utf-8")
)
assert intent["skills"] == ["intent-clarification-policy"]
assert intent["tool_groups"] == []
for agent_id in seed.STRATEGY_ANGLE_AGENT_IDS:
config = yaml.safe_load(_asset(agent_id, "config.yaml").read_text(encoding="utf-8"))
assert config["skills"] == ["policy-knowledge"]
assert config["tool_groups"] == ["web", "file:read"]
judge = yaml.safe_load(
_asset(seed.STRATEGY_JUDGE_AGENT_ID, "config.yaml").read_text(encoding="utf-8")
)
solution = yaml.safe_load(
_asset(seed.STRATEGY_SOLUTION_AGENT_ID, "config.yaml").read_text(encoding="utf-8")
)
assert judge["skills"] == []
assert judge["tool_groups"] == []
assert solution["skills"] == []
assert solution["tool_groups"] == ["file:write"]
def test_default_strategy_skills_are_enabled_in_the_system_catalog() -> None:
extensions_path = Path(__file__).resolve().parents[1] / "extensions_config.json"
extensions = yaml.safe_load(extensions_path.read_text(encoding="utf-8")) or {}
configured = extensions.get("skills") or {}
# A missing entry means enabled by default. An explicit false would make
# the seeded allowlist impossible to save under the server-side validator
# and silently remove the skill from strategy runs.
for skill_name in ("intent-clarification-policy", "policy-knowledge"):
assert (configured.get(skill_name) or {}).get("enabled", True) is True
def test_seed_assets_define_machine_readable_handoff_contracts() -> None:
intent_soul = _asset(seed.STRATEGY_INTENT_AGENT_ID, "SOUL.md").read_text(encoding="utf-8")
judge_soul = _asset(seed.STRATEGY_JUDGE_AGENT_ID, "SOUL.md").read_text(encoding="utf-8")
solution_soul = _asset(seed.STRATEGY_SOLUTION_AGENT_ID, "SOUL.md").read_text(encoding="utf-8")
assert "[INTENT_READY]" in intent_soul
for field in (
"name",
"detail",
"objective",
"prerequisites",
"constraints",
"deliverables",
"acceptanceCriteria",
"horizon",
"selectedAngleIds",
):
assert f'"{field}"' in intent_soul
assert "[JUDGEMENT_READY]" in judge_soul
for field in ("agentId", "score", "conclusion", "comment", "strengths", "risks"):
assert f'"{field}"' in judge_soul
assert '"recommendedAgentIds"' in judge_soul
assert "/mnt/user-data/outputs/综合策略方案.md" in solution_soul
def test_seed_creates_once_and_never_overwrites_admin_changes(tmp_paths: Paths) -> None:
expected = set(seed.required_agent_ids())
assert set(seed.ensure_strategy_selection_agents()) == expected
for agent_id in expected:
agent_dir = tmp_paths.agent_dir(agent_id)
assert (agent_dir / "config.yaml").is_file()
assert (agent_dir / "SOUL.md").is_file()
customized_agent = seed.STRATEGY_SECURITY_AGENT_ID
customized_soul = tmp_paths.agent_dir(customized_agent) / "SOUL.md"
customized_soul.write_text("管理员现场修改", encoding="utf-8")
assert seed.ensure_strategy_selection_agents() == []
assert customized_soul.read_text(encoding="utf-8") == "管理员现场修改"
def test_seed_upgrades_only_an_exact_known_legacy_default(
tmp_paths: Paths,
monkeypatch: pytest.MonkeyPatch,
) -> None:
assert seed.ensure_strategy_selection_agents()
intent_soul = tmp_paths.agent_dir(seed.STRATEGY_INTENT_AGENT_ID) / "SOUL.md"
legacy_content = b"legacy bundled intent soul\r\n"
intent_soul.write_bytes(legacy_content)
monkeypatch.setitem(
seed._LEGACY_DEFAULT_SOUL_HASHES,
seed.STRATEGY_INTENT_AGENT_ID,
seed._normalized_text_hash(legacy_content),
)
assert seed.ensure_strategy_selection_agents() == []
assert (
intent_soul.read_bytes()
== _asset(
seed.STRATEGY_INTENT_AGENT_ID,
"SOUL.md",
).read_bytes()
)
intent_soul.write_text("管理员现场修改", encoding="utf-8")
assert seed.ensure_strategy_selection_agents() == []
assert intent_soul.read_text(encoding="utf-8") == "管理员现场修改"
class _SkillStorage:
def __init__(self, skills: list[Skill]) -> None:
self._skills = skills
def load_skills(self, *, enabled_only: bool) -> list[Skill]:
if enabled_only:
return [skill for skill in self._skills if skill.enabled]
return list(self._skills)
def _skill(name: str, *, category: SkillCategory, enabled: bool = True) -> Skill:
directory = Path("skills") / category.value / name
return Skill(
name=name,
description=name,
license=None,
skill_dir=directory,
skill_file=directory / "SKILL.md",
relative_path=Path(name),
category=category,
enabled=enabled,
)
@pytest.mark.asyncio
async def test_strategy_skill_validation_accepts_only_global_catalog_names(
monkeypatch: pytest.MonkeyPatch,
) -> None:
skill_store = MemorySkillStore()
await skill_store.create(
{
"name": "published-custom",
"owner_user_id": "publisher",
"published": True,
}
)
storage = _SkillStorage(
[
_skill("public-skill", category=SkillCategory.PUBLIC),
_skill("published-custom", category=SkillCategory.CUSTOM),
]
)
monkeypatch.setattr(agents_router, "get_or_new_skill_storage", lambda **_: storage)
monkeypatch.setattr(
agents_router,
"get_skill_usage_entries",
lambda: {},
)
result = await agents_router._validate_strategy_agent_skills(
[" public-skill ", "published-custom", "public-skill"],
app_config=object(), # type: ignore[arg-type]
skill_store=skill_store,
)
assert result == ["public-skill", "published-custom"]
@pytest.mark.asyncio
@pytest.mark.parametrize(
("requested", "detail_fragment"),
[
("missing", "Unknown strategy agent skills"),
("disabled", "Disabled or archived strategy agent skills"),
("stale", "Disabled or archived strategy agent skills"),
("private-custom", "must be globally available"),
("orphan-custom", "must be globally available"),
],
)
async def test_strategy_skill_validation_rejects_unselectable_names(
monkeypatch: pytest.MonkeyPatch,
requested: str,
detail_fragment: str,
) -> None:
skill_store = MemorySkillStore()
await skill_store.create(
{
"name": "private-custom",
"owner_user_id": "owner",
"published": False,
}
)
storage = _SkillStorage(
[
_skill("disabled", category=SkillCategory.PUBLIC, enabled=False),
_skill("stale", category=SkillCategory.PUBLIC),
_skill("private-custom", category=SkillCategory.CUSTOM),
_skill("orphan-custom", category=SkillCategory.CUSTOM),
]
)
monkeypatch.setattr(agents_router, "get_or_new_skill_storage", lambda **_: storage)
monkeypatch.setattr(
agents_router,
"get_skill_usage_entries",
lambda: {"stale": {"state": "stale"}},
)
with pytest.raises(HTTPException) as exc_info:
await agents_router._validate_strategy_agent_skills(
[requested],
app_config=object(), # type: ignore[arg-type]
skill_store=skill_store,
)
assert exc_info.value.status_code == 422
assert detail_fragment in str(exc_info.value.detail)
def test_fixed_strategy_role_agents_cannot_be_deleted() -> None:
for agent_id in seed.required_agent_ids():
with pytest.raises(HTTPException) as exc_info:
agents_router._ensure_strategy_agent_deletable(agent_id)
assert exc_info.value.status_code == 409
agents_router._ensure_strategy_agent_deletable("ordinary-custom-agent")
@@ -1,8 +1,10 @@
"""Regression coverage for provider-safe transient context compression."""
from langchain.agents.middleware.types import ModelRequest, ModelResponse
from langchain_core.language_models.fake_chat_models import FakeListChatModel
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
from deerflow.agents.middlewares.summarization_middleware import _ensure_latest_human_message
from deerflow.agents.middlewares.summarization_middleware import DeerFlowSummarizationMiddleware, _ensure_latest_human_message
def test_compression_restores_latest_human_turn_when_only_tool_tail_remains():
@@ -28,3 +30,37 @@ def test_compression_does_not_duplicate_human_turn_already_in_tail():
result = _ensure_latest_human_message([user], compressed)
assert result == compressed
def test_compression_uses_single_model_request_system_slot_for_generic_adapters():
model = FakeListChatModel(responses=["condensed history"])
middleware = DeerFlowSummarizationMiddleware(
model,
trigger=("messages", 3),
keep=("messages", 1),
)
request = ModelRequest(
model=model,
system_message=SystemMessage(content="agent prompt"),
messages=[
HumanMessage(content="first question", id="human-1"),
AIMessage(content="first answer", id="ai-1"),
HumanMessage(content="continue", id="human-2"),
],
)
captured: list[ModelRequest] = []
def handler(model_request: ModelRequest) -> ModelResponse:
captured.append(model_request)
return ModelResponse(result=[AIMessage(content="ok")])
middleware.wrap_model_call(request, handler)
assert len(captured) == 1
normalized = captured[0]
assert normalized.system_message is not None
assert "agent prompt" in normalized.system_message.text
assert "condensed history" in normalized.system_message.text
assert all(not isinstance(message, SystemMessage) for message in normalized.messages)
assert [message.type for message in normalized.messages] == ["human"]
assert normalized.messages[0].content == "continue"
@@ -8,7 +8,7 @@ import pytest
from fastapi import FastAPI, UploadFile
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway.routers import wiki_packages
from app.gateway.routers import assistant_knowledge, wiki_packages
from deerflow.assistant_knowledge.archive import PackageArchive
from deerflow.assistant_knowledge.package_io import write_package
from deerflow.config.llmwiki_config import LocalWikiEmbeddingConfig
@@ -22,6 +22,26 @@ from deerflow.persistence.base import Base
from deerflow.persistence.llmwiki_index.memory import MemoryLlmWikiIndexStore
@pytest.mark.asyncio
async def test_existing_wiki_vectors_can_be_exported_without_runtime_encoder(monkeypatch):
request = SimpleNamespace(
app=SimpleNamespace(
state=SimpleNamespace(llmwiki_sync_service=None),
),
)
async def has_vectors(_request, mapping_id):
assert mapping_id == "mapping-1"
return True
monkeypatch.setattr(assistant_knowledge, "_has_local_wiki_vectors_for_export", has_vectors)
await assistant_knowledge._ensure_local_wiki_vectors_for_export(
request,
{"id": "mapping-1", "weknora_id": "remote-1"},
)
@pytest.mark.asyncio
async def test_metadata_vectors_and_links_survive_roundtrip_without_encoding(tmp_path, monkeypatch):
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
@@ -375,8 +395,10 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
class Embedding:
def __init__(self):
self.model_path = ""
self.reset_calls = 0
async def reset_local_encoder(self, *, model_path=None):
self.reset_calls += 1
if model_path is not None:
self.model_path = model_path
@@ -395,6 +417,17 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
monkeypatch.setattr(llmwiki_index, "load_system_settings", SystemSettings)
monkeypatch.setattr(llmwiki_index, "save_system_settings", lambda value: saved.append(value) or value)
embedding = Embedding()
async def rebuild(_app, *, force=False):
assert force is True
embedding.model_path = saved[-1].local_wiki_encoder.model_path
monkeypatch.setattr(llmwiki_index, "rebuild_local_wiki_index_runtime", rebuild)
monkeypatch.setattr(
llmwiki_index,
"local_encoder_runtime_status",
lambda _app: embedding.local_encoder_status(),
)
app = FastAPI()
app.state.config = SimpleNamespace(
llmwiki=SimpleNamespace(
@@ -414,11 +447,15 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
paths=["onnx/model.onnx", "config.json"],
)
active = tmp_path / "models" / "bge-m3"
revision = saved[-1].local_wiki_encoder.model_revision
active = tmp_path / "models" / "bge-m3-revisions" / revision
assert (active / "onnx" / "model.onnx").read_bytes() == b"onnx"
assert result["uploaded_files"] == 2
assert saved[-1].local_wiki_encoder.model_path == str(active.resolve())
assert saved[-1].local_wiki_encoder.mode == "local"
assert revision
assert embedding.model_path == str(active.resolve())
assert embedding.reset_calls == 0
def test_encoder_upload_rejects_parent_traversal():