代码提交

This commit is contained in:
董宏凯 committed 2026-07-24 10:33:28 +08:00
1 parent f49def09ea
commit 45df500a9c
55 files changed
+3390 -461

No files matched your search

@@ -89,6 +89,200 @@ async def test_session_activation_freezes_chain_and_creates_nodes_once(store):
assert [node["node_key"] for node in repeated_nodes] == [node["node_key"] for node in created_nodes]
@pytest.mark.asyncio
async def test_conversation_snapshots_round_trip_and_history_delete_removes_nodes(store):
"""数据库恢复副本必须覆盖三个对话阶段、岗位节点和未发送草稿。"""
created = await store.create_session(
"user-a",
external_task_id="task-persisted",
task_snapshot={"title": "持久化测试"},
)
intent_conversation = {
"messages": [{"id": "intent-user-1", "type": "human", "content": "请分析任务"}],
"timeline": [{"kind": "message", "message_id": "intent-user-1"}],
"draft": "尚未发送的意图补充",
}
summary_conversation = {
"messages": [{"id": "summary-ai-1", "type": "ai", "content": "方案总结"}],
"draft": "总结修改意见",
}
action_conversation = {
"messages": [{"id": "action-ai-1", "type": "ai", "content": "行动规划"}],
"draft": "",
}
updated = await store.update_session(
created["id"],
"user-a",
intent_thread_id="intent-thread",
intent_conversation=intent_conversation,
summary_conversation=summary_conversation,
action_plan_conversation=action_conversation,
)
assert updated is not None
assert updated["intent_conversation"] == intent_conversation
assert updated["summary_conversation"] == summary_conversation
assert updated["action_plan_conversation"] == action_conversation
_, nodes = await store.activate_session(
created["id"],
"user-a",
chain_id="chain-a",
chain_snapshot={"id": "chain-a"},
nodes=[
{
"node_key": "stage-1-seat-1-agent-a",
"stage_index": 0,
"seat_index": 0,
"agent_id": "agent-a",
"position_id": "planning",
"status": "ready",
}
],
)
node_conversation = {
"messages": [{"id": "node-ai-1", "type": "ai", "content": "岗位答复"}],
"draft": "岗位草稿",
}
saved_node = await store.update_node(
created["id"],
nodes[0]["node_key"],
"user-a",
conversation_snapshot=node_conversation,
)
assert saved_node is not None
assert saved_node["conversation_snapshot"] == node_conversation
# Ownership checks apply to delete as well.
assert not await store.delete_session(created["id"], "user-b")
assert await store.get_session(created["id"], "user-a") is not None
assert await store.delete_session(created["id"], "user-a")
assert await store.get_session(created["id"], "user-a") is None
assert await store.list_nodes(created["id"], "user-a") is None
@pytest.mark.asyncio
async def test_demo_seed_is_complete_idempotent_and_user_scoped(store):
payload = {
"external_task_id": "position-demo-full-v1",
"task_snapshot": {"title": "暑期活动"},
"chain_id": "position-demo-chain",
"chain_snapshot": {
"id": "position-demo-chain",
"seats": [
{
"agent_id": "demo-agent",
"name": "演示分析智能体",
"position_id": "planning",
}
],
},
"intent_thread_id": "frontend-placeholder",
"intent_snapshot": {
"objective": "形成完整宣传方案",
"constraints": ["预算不少于 15 万元"],
},
"intent_conversation": {
"messages": [{"id": 1, "sender": "agent", "content": "意图已确认"}],
"inputDraft": "",
},
"summary_thread_id": "frontend-placeholder",
"summary_snapshot": {"status": "done", "content": "方案总结"},
"summary_conversation": {
"messages": [{"id": "summary-ai", "type": "ai", "content": "方案总结"}]
},
"action_plan_thread_id": "frontend-placeholder",
"action_plan_snapshot": {"status": "done", "content": "行动规划"},
"action_plan_conversation": {
"messages": [{"id": "action-ai", "type": "ai", "content": "行动规划"}]
},
"status": "completed",
}
nodes = [
{
"node_key": "stage-1-seat-1-demo-agent",
"stage_index": 0,
"seat_index": 0,
"agent_id": "demo-agent",
"position_id": "planning",
"thread_id": "frontend-placeholder",
"status": "done",
"latest_answer": "报告已完成",
"artifact_manifest": [
{
"name": "演示报告.md",
"path": "mnt/user-data/outputs/演示报告.md",
"mime_type": "text/markdown",
"content": "# 演示报告\n\n完整正文",
}
],
"conversation_snapshot": {
"messages": [{"id": "node-ai", "type": "ai", "content": "报告已完成"}],
"inputDraft": "",
},
"revision": 1,
}
]
first, first_nodes = await store.ensure_demo_session(
"user-a",
demo_key="full-v1",
session_payload=payload,
nodes=nodes,
)
repeated, repeated_nodes = await store.ensure_demo_session(
"user-a",
demo_key="full-v1",
session_payload={**payload, "task_snapshot": {"title": "不应覆盖"}},
nodes=[],
)
other_user, other_nodes = await store.ensure_demo_session(
"user-b",
demo_key="full-v1",
session_payload=payload,
nodes=nodes,
)
assert first["id"] == repeated["id"]
assert first["task_snapshot"]["title"] == "暑期活动"
assert repeated["task_snapshot"]["title"] == "暑期活动"
assert first["intent_conversation"] == payload["intent_conversation"]
assert first["summary_snapshot"]["thread_id"] == first["summary_thread_id"]
assert first["action_plan_snapshot"]["thread_id"] == first["action_plan_thread_id"]
assert first_nodes[0]["artifact_manifest"][0]["content"] == "# 演示报告\n\n完整正文"
assert repeated_nodes[0]["id"] == first_nodes[0]["id"]
assert other_user["id"] != first["id"]
assert other_user["intent_thread_id"] != first["intent_thread_id"]
assert other_nodes[0]["thread_id"] != first_nodes[0]["thread_id"]
@pytest.mark.asyncio
async def test_embedded_artifact_content_survives_missing_sandbox(monkeypatch):
"""后续岗位优先读取 SQL 正文,不能因沙箱卷丢失而只拿到文件名。"""
async def unexpected_resolve(*_args, **_kwargs):
raise AssertionError("embedded database content should avoid sandbox resolution")
monkeypatch.setattr(
position_roundtable_router,
"aresolve_thread_virtual_path",
unexpected_resolve,
)
artifacts, consumed = await position_roundtable_router._artifact_refs_with_content(
"removed-thread",
[
{
"name": "report.md",
"path": "mnt/user-data/outputs/report.md",
"mime_type": "text/markdown",
"content": "# 数据库恢复报告\n\n完整正文",
}
],
10_000,
)
assert artifacts[0]["content"] == "# 数据库恢复报告\n\n完整正文"
assert consumed == len("# 数据库恢复报告\n\n完整正文")
def test_task_external_context_reaches_agent_runtime_context():
"""岗位会商的左侧任务快照不能在网关白名单中丢失。"""
task_context = {