1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富 统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」; 同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。 2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本), 新增《文档规整方案与开发前待决事项-2026-09-17》。 3) 客服agent 四份交付文档首次纳入本分支。
113 lines
6.0 KiB
Python
113 lines
6.0 KiB
Python
import asyncio
|
|
from uuid import uuid4
|
|
|
|
import httpx
|
|
import pytest
|
|
from sqlalchemy import delete, select, update
|
|
|
|
from app.api.dependencies.auth import build_request_context
|
|
from app.core.contracts import AgentRequest, AgentResult, CoreResult, RequestContext
|
|
from app.core.errors import RunLeaseLostError
|
|
from app.infrastructure.db import SessionFactory
|
|
from app.main import create_app
|
|
from app.model.audit import InteractionAudit
|
|
from app.model.conversation import ConversationMessage
|
|
from app.model.platform import AgentRun, DomainEventOutbox, OutboxDelivery, RequestIdempotency
|
|
from app.service.agent.governance import FALLBACK_DISCLAIMER
|
|
from app.service.agent_persistence_service import AgentPersistenceService
|
|
from app.service.agent_run_application_service import AgentRunApplicationService
|
|
from app.worker.runtime import WorkerRuntime
|
|
|
|
|
|
@pytest.mark.integration
|
|
@pytest.mark.parametrize("revoked", [False, True])
|
|
async def test_http_accept_worker_commit_query_and_repeat(acceptance_registry, revoked):
|
|
context = RequestContext(user_id="1", trace_id=str(uuid4()), roles=("customer",),
|
|
permissions=("agent:run",))
|
|
app = create_app()
|
|
app.dependency_overrides[build_request_context] = lambda: context
|
|
session_id = f"runtime-{uuid4()}"
|
|
run_id = None
|
|
|
|
async def identity(value):
|
|
return context if not revoked else value
|
|
|
|
runtime = WorkerRuntime(acceptance_registry, resolve_identity=identity)
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app),
|
|
base_url="http://test") as client:
|
|
try:
|
|
# 该消息故意带有偏好信号,验证客服仍不进入长期记忆抽取;客服当前仅允许
|
|
# 使用经过脱敏的会话短期上下文,不能沉淀为画像或跨会话偏好。
|
|
response = await client.post("/api/v1/agent-runs", json={
|
|
"agent_type": "customer_service",
|
|
"message": "hello runtime,我的风险偏好是稳健型",
|
|
"session_id": session_id, "idempotency_key": str(uuid4()),
|
|
})
|
|
assert response.status_code == 202
|
|
# 文档 §3.3 + §19:成功响应统一为 {data, meta} 信封。
|
|
run_id = response.json()["data"]["run_id"]
|
|
assert await runtime.dispatch_one(run_id=run_id)
|
|
assert not await runtime.dispatch_one(run_id=run_id)
|
|
await asyncio.gather(runtime.execute(run_id), runtime.execute(run_id))
|
|
result = (await client.get(f"/api/v1/agent-runs/{run_id}")).json()["data"]
|
|
assert result["status"] == ("failed" if revoked else "succeeded")
|
|
if not revoked:
|
|
# 治理层强制注入固定免责声明(门禁 F5:面向客户输出 100% 附固定话术),
|
|
# 落库正文因此是「Agent 正文 + 免责声明」,不再是纯正文——断言改为包含。
|
|
assert result["result"]["content"].startswith("test result")
|
|
assert FALLBACK_DISCLAIMER in result["result"]["content"]
|
|
assert not await runtime.execute(run_id)
|
|
async with SessionFactory() as session:
|
|
messages = list(await session.scalars(select(ConversationMessage).where(
|
|
ConversationMessage.session_id == session_id,
|
|
ConversationMessage.role == "assistant")))
|
|
assert len(messages) == (0 if revoked else 1)
|
|
events = list(await session.scalars(select(DomainEventOutbox).where(
|
|
DomainEventOutbox.aggregate_id == run_id,
|
|
DomainEventOutbox.event_type == "memory.extraction_requested")))
|
|
assert events == []
|
|
finally:
|
|
await cleanup(session_id, run_id)
|
|
|
|
|
|
async def test_expired_worker_cannot_commit(acceptance_registry):
|
|
context = RequestContext(user_id="1", trace_id=str(uuid4()), roles=("customer",),
|
|
permissions=("agent:run",))
|
|
request = AgentRequest(agent_type="customer_service", message="fence test",
|
|
session_id=f"fence-{uuid4()}", idempotency_key=str(uuid4()))
|
|
async with SessionFactory() as session:
|
|
accepted = await AgentRunApplicationService(session, acceptance_registry).accept(
|
|
request, context)
|
|
try:
|
|
async with SessionFactory() as session, session.begin():
|
|
await session.execute(update(AgentRun).where(AgentRun.run_id == accepted.run_id)
|
|
.values(status="running", worker_id="new-owner"))
|
|
async with SessionFactory() as session:
|
|
with pytest.raises(RunLeaseLostError):
|
|
await AgentPersistenceService(session).complete_run(
|
|
accepted.run_id, AgentResult(run_id=accepted.run_id,
|
|
result=CoreResult(text="stale")),
|
|
worker_id="old-owner")
|
|
async with SessionFactory() as session:
|
|
assert await session.scalar(select(ConversationMessage.id).where(
|
|
ConversationMessage.session_id == request.session_id,
|
|
ConversationMessage.role == "assistant")) is None
|
|
finally:
|
|
await cleanup(request.session_id, accepted.run_id)
|
|
|
|
|
|
async def cleanup(session_id, run_id):
|
|
async with SessionFactory() as session, session.begin():
|
|
event_ids = select(DomainEventOutbox.event_id).where(
|
|
DomainEventOutbox.aggregate_id == run_id)
|
|
await session.execute(delete(OutboxDelivery).where(OutboxDelivery.event_id.in_(event_ids)))
|
|
await session.execute(delete(DomainEventOutbox).where(
|
|
DomainEventOutbox.aggregate_id == run_id))
|
|
await session.execute(delete(InteractionAudit).where(
|
|
InteractionAudit.session_id == session_id))
|
|
await session.execute(delete(AgentRun).where(AgentRun.session_id == session_id))
|
|
await session.execute(delete(RequestIdempotency).where(
|
|
RequestIdempotency.session_id == session_id))
|
|
await session.execute(delete(ConversationMessage).where(
|
|
ConversationMessage.session_id == session_id))
|