sync update from LAN repo (source master-nj @ 067eb808)
This commit is contained in:
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)
|
||||
Reference in new issue
Block a user