M4 收口:神识用户面与计量闭环——BYOK/配额三段式/usage 落库/Anthropic+Gemini 适配器/神识阁

- 神识 Go:store 双实现(PG 自动建表+60s 就绪重试 / 内存回退)承载 usage_logs·byok_keys·quota_balances;BYOK 密钥库(AES-GCM 加密、API 脱敏、撤回即失效、主密钥未配置 fail-closed);配额三段式(预扣 max_tokens 钳位 1M→结算实际 token→全败幂等退款,HJ-2004);计量归属(IR user_id→usage_logs,/me/usage 用户维度聚合+配额总览,/admin/usage/sample 对账抽样);Anthropic/Gemini 出站适配器(SSE 解析、system 提升、用量映射);stub 端点补名修复计量空 endpoint
- IR 契约:user_id/byok_key_id 双端同步(Go types.go + Py types.py)
- 灵台:runs.owner_id(迁移 005,server_default 兼容存量)经 kernel 全链透传为 IR user_id;shenshi_client 扩展用户面方法(X-Hunjing-User-Id 代理,Echo 桩带内存实现);/me/byok-keys 登记·列表·撤回与 /me/usage 端点(4 用例)
- 前端神识阁(/models 重建):偏好(生成参数表单)/自带密钥(登记加密保存、脱敏列表、撤回二次确认)/用量(聚合表+配额进度条)三区;me API 库+mock+5 用例;侧栏恢复入口
- 配置与部署:.env.example 增 SHENSHI_DATABASE_URL/SHENSHI_BYOK_MASTER_KEY/SHENSHI_USER_BUDGET_TOKENS;compose 神识容器接自有库(init-shenshi.sql 预建 hunjing_shenshi,与灵台不共库共表)
- 证据:Go 34 用例(BYOK 独占/配额拒绝/结算退款/归属聚合)、后端 532、前端 210、BFF 4 全绿;跨进程 E2E 四场景(灵台 HttpShenshiClient→真 Go 神识:登记脱敏/BYOK 独占不可达整体失败/用量归属聚合/撤回即失效)通过;rewrite-plan M4 标记完成、能力矩阵神识行升已验证
This commit is contained in:
chenxin committed 2026-08-27 18:39:17 +08:00
1 parent 462d570751
commit 144415247e
51 files changed
+3533 -90

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
"""用户面(神识阁数据源)端点测试:BYOK 管理与用量查询,经回声桩客户端端到端。"""
from collections.abc import AsyncGenerator
import pytest
from httpx import ASGITransport, AsyncClient
from app.kernel import Kernel
from app.main import app
from app.services.event_store import DbEventStore
from app.db.session import AsyncSessionLocal
from app.kernel import KernelEventLog
from app.shenshi_client.client import EchoShenshi
@pytest.fixture
async def client(
monkeypatch: pytest.MonkeyPatch, auth_headers: dict[str, str]
) -> AsyncGenerator[AsyncClient, None]:
"""手工装配 Kernel(回声桩客户端)到 app.state(测试不经 lifespan,与既有做法一致)。"""
app.state.kernel = Kernel(
shenshi=EchoShenshi(),
session_factory=AsyncSessionLocal,
event_log=KernelEventLog(DbEventStore(AsyncSessionLocal)),
timeout_seconds=10.0,
)
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test", headers=auth_headers) as c:
yield c
app.state.kernel = None
@pytest.mark.asyncio
async def test_byok_register_list_revoke(client: AsyncClient) -> None:
payload = {
"name": "自带 DeepSeek",
"type": "openai_compatible",
"base_url": "https://api.deepseek.com",
"api_key": "sk-very-secret-value",
"model": "deepseek-v4-flash",
}
created = await client.post("/api/v1/me/byok-keys", json=payload)
assert created.status_code == 201
body = created.json()
# 脱敏:响应不得含明文密钥
assert "sk-very-secret-value" not in body
assert "****" in body.get("api_key_mask", "")
key_id = body["id"]
listed = await client.get("/api/v1/me/byok-keys")
assert listed.status_code == 200
keys = listed.json()["keys"]
assert [k["id"] for k in keys] == [key_id]
assert all("sk-very-secret-value" not in str(k) for k in keys)
revoked = await client.delete(f"/api/v1/me/byok-keys/{key_id}")
assert revoked.status_code == 204
after = await client.get("/api/v1/me/byok-keys")
assert after.json()["keys"] == []
@pytest.mark.asyncio
async def test_byok_register_validation(client: AsyncClient) -> None:
bad = await client.post(
"/api/v1/me/byok-keys",
json={"name": "x", "type": "unknown_type", "base_url": "https://x", "api_key": "k" * 8, "model": "m"},
)
# 类型枚举由神识侧裁决;回声桩直通,这里校验必填字段约束
missing = await client.post("/api/v1/me/byok-keys", json={"name": "x"})
assert missing.status_code == 422
@pytest.mark.asyncio
async def test_my_usage_shape(client: AsyncClient) -> None:
resp = await client.get("/api/v1/me/usage")
assert resp.status_code == 200
body = resp.json()
assert "rows" in body
assert isinstance(body["rows"], list)
@pytest.mark.asyncio
async def test_me_requires_auth() -> None:
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as c:
resp = await c.get("/api/v1/me/byok-keys")
assert resp.status_code in (401, 503) # 未携带凭证被拒(JWT 未配置时 fail-closed)