feat: 人设模板系统、节点调度标签、pydantic-settings收敛、错误处理增强

新增persona_template表和CRUD API,BaseIndividualModel增加node_affinity和template_origin_id字段,
WorkerCluster支持多集群Ray资源调度,环境变量收敛到pydantic-settings统一校验,
数据库异常转换为结构化BusinessError/RetryableError,系统节点支持custom_system_prompt。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-04 06:07:46 +00:00
parent f3a92a793e
commit 8f1398c591
23 changed files with 582 additions and 48 deletions
+165
View File
@@ -0,0 +1,165 @@
"""``api/agent.py`` persona template 路由:CRUD 鉴权与 node_affinity 校验。"""
from __future__ import annotations
import types
from unittest.mock import AsyncMock
import pytest
from fastapi import FastAPI
from httpx import AsyncClient, ASGITransport
from kilostar.api.agent import agent_router, _VALID_AFFINITIES
from kilostar.utils.access import Accessor, TokenData
from kilostar.core.postgres_database.model import UserAuthority
def _fake_user(user_id: str = "alice"):
return TokenData(user_id=user_id, username=user_id)
def _tpl(owner: str = "alice", is_builtin: bool = False):
return types.SimpleNamespace(
template_id="tpl1", name="MyBot", agent_type="ordinary",
description="d", system_prompt="s", provider_title="openai",
model_id="gpt-4", tools=[], tags=[], is_builtin=is_builtin, owner_id=owner,
)
@pytest.fixture
def app(monkeypatch):
import kilostar.utils.check_user.role_check as rc
monkeypatch.setattr(rc, "get_authority", AsyncMock(return_value=UserAuthority.USER))
_app = FastAPI()
_app.include_router(agent_router)
_app.dependency_overrides[Accessor.get_current_user] = lambda: _fake_user()
return _app
def _register_pg(fake_actors, **kwargs):
pg = types.SimpleNamespace(**kwargs)
fake_actors.register("postgres_database", pg)
return pg
# ── node_affinity 校验(纯 pydantic) ─────────────────────────────────────
def test_valid_affinities_accepted():
from kilostar.api.agent import WorkerIndividualCreate
for aff in _VALID_AFFINITIES:
m = WorkerIndividualCreate(
agent_name="x", agent_type="ordinary", description="d",
provider_title="p", model_id="m", system_prompt="s",
output_template={}, bound_skill={}, workspace=[], node_affinity=aff,
)
assert m.node_affinity == aff
def test_invalid_affinity_raises():
from pydantic import ValidationError
from kilostar.api.agent import WorkerIndividualCreate
with pytest.raises(ValidationError):
WorkerIndividualCreate(
agent_name="x", agent_type="ordinary", description="d",
provider_title="p", model_id="m", system_prompt="s",
output_template={}, bound_skill={}, workspace=[], node_affinity="bad",
)
def test_update_invalid_affinity_raises():
from pydantic import ValidationError
from kilostar.api.agent import WorkerIndividualUpdate
with pytest.raises(ValidationError):
WorkerIndividualUpdate(node_affinity="bad")
def test_update_none_affinity_ok():
from kilostar.api.agent import WorkerIndividualUpdate
assert WorkerIndividualUpdate(node_affinity=None).node_affinity is None
# ── template API 路由 ──────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_list_templates(app, fake_actors):
pg = types.SimpleNamespace(
list_templates=types.SimpleNamespace(remote=AsyncMock(return_value=[]))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.get("/api/v1/agent/template")
assert r.status_code == 200
assert r.json()["templates"] == []
@pytest.mark.asyncio
async def test_create_template(app, fake_actors):
tpl = _tpl()
pg = types.SimpleNamespace(
add_template=types.SimpleNamespace(remote=AsyncMock(return_value=tpl))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.post("/api/v1/agent/template", json={"name": "test"})
assert r.status_code == 200
assert r.json()["template_id"] == "tpl1"
@pytest.mark.asyncio
async def test_delete_template_not_found(app, fake_actors):
pg = types.SimpleNamespace(
get_template=types.SimpleNamespace(remote=AsyncMock(return_value=None))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.delete("/api/v1/agent/template/missing")
assert r.status_code == 404
@pytest.mark.asyncio
async def test_delete_builtin_template_forbidden(app, fake_actors):
pg = types.SimpleNamespace(
get_template=types.SimpleNamespace(remote=AsyncMock(return_value=_tpl(is_builtin=True)))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.delete("/api/v1/agent/template/tpl1")
assert r.status_code == 403
@pytest.mark.asyncio
async def test_delete_other_users_template_forbidden(app, fake_actors):
pg = types.SimpleNamespace(
get_template=types.SimpleNamespace(remote=AsyncMock(return_value=_tpl(owner="bob")))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.delete("/api/v1/agent/template/tpl1")
assert r.status_code == 403
@pytest.mark.asyncio
async def test_create_worker_from_template(app, fake_actors):
worker = types.SimpleNamespace(agent_id="w1")
pg = types.SimpleNamespace(
get_template=types.SimpleNamespace(remote=AsyncMock(return_value=_tpl())),
add_worker_individual=types.SimpleNamespace(remote=AsyncMock(return_value=worker)),
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.post("/api/v1/agent/worker/from-template/tpl1")
assert r.status_code == 200
assert r.json()["agent_id"] == "w1"
@pytest.mark.asyncio
async def test_create_worker_from_missing_template(app, fake_actors):
pg = types.SimpleNamespace(
get_template=types.SimpleNamespace(remote=AsyncMock(return_value=None))
)
fake_actors.register("postgres_database", pg)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://t") as c:
r = await c.post("/api/v1/agent/worker/from-template/nope")
assert r.status_code == 404
+6 -5
View File
@@ -5,7 +5,7 @@ from pydantic import BaseModel, ValidationError
from sqlalchemy.exc import IntegrityError, OperationalError
from kilostar.core.postgres_database.database_exception import database_exception
from kilostar.utils.error import UserNotExistError
from kilostar.utils.error import UserNotExistError, BusinessError, RetryableError
async def test_normal_path_returns_value():
@@ -36,21 +36,22 @@ async def test_validation_error_propagates():
await boom()
async def test_integrity_error_propagates():
async def test_integrity_error_becomes_business_error():
@database_exception
async def boom() -> None:
raise IntegrityError("stmt", {}, Exception("dup"))
with pytest.raises(IntegrityError):
with pytest.raises(BusinessError) as exc_info:
await boom()
assert exc_info.value.http_status == 409
async def test_operational_error_propagates():
async def test_operational_error_becomes_retryable():
@database_exception
async def boom() -> None:
raise OperationalError("stmt", {}, Exception("conn"))
with pytest.raises(OperationalError):
with pytest.raises(RetryableError):
await boom()
+74
View File
@@ -0,0 +1,74 @@
"""``PersonaTemplateDatabase`` — list_templates 查询逻辑单元测试。"""
from __future__ import annotations
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from kilostar.core.postgres_database.module.persona_template import PersonaTemplateDatabase
def _make_db():
session = AsyncMock()
session.__aenter__ = AsyncMock(return_value=session)
session.__aexit__ = AsyncMock(return_value=False)
session_maker = MagicMock(return_value=session)
return PersonaTemplateDatabase(session_maker), session
@pytest.mark.anyio
async def test_list_owner_and_builtin_uses_or():
"""owner_id + include_builtin=True 应构造 OR 条件,不拉出其他人的模板。"""
db, session = _make_db()
execute_result = MagicMock()
execute_result.scalars.return_value.all.return_value = []
session.execute = AsyncMock(return_value=execute_result)
result = await db.list_templates(owner_id="alice", include_builtin=True)
assert result == []
# 确认 execute 被调用(OR 条件路径走通)
session.execute.assert_awaited_once()
@pytest.mark.anyio
async def test_list_owner_only():
db, session = _make_db()
execute_result = MagicMock()
execute_result.scalars.return_value.all.return_value = []
session.execute = AsyncMock(return_value=execute_result)
await db.list_templates(owner_id="alice", include_builtin=False)
session.execute.assert_awaited_once()
@pytest.mark.anyio
async def test_list_builtin_only():
db, session = _make_db()
execute_result = MagicMock()
execute_result.scalars.return_value.all.return_value = []
session.execute = AsyncMock(return_value=execute_result)
await db.list_templates(owner_id=None, include_builtin=True)
session.execute.assert_awaited_once()
@pytest.mark.anyio
async def test_delete_nonexistent_returns_false():
db, session = _make_db()
execute_result = MagicMock()
execute_result.scalar_one_or_none.return_value = None
session.execute = AsyncMock(return_value=execute_result)
result = await db.delete_template("missing")
assert result is False
@pytest.mark.anyio
async def test_update_nonexistent_returns_none():
db, session = _make_db()
execute_result = MagicMock()
execute_result.scalar_one_or_none.return_value = None
session.execute = AsyncMock(return_value=execute_result)
result = await db.update_template("missing", name="new")
assert result is None