HEL-877: wire the audited .12 voice base into the one CyberGril chat chain as backend-only code (no deploy, no real voice call) - new independent src/cybergril/voice module: session+router add POST /api/voice/session / DELETE end / GET honest status on the unified instance (server-side room + dedicated cybergril-voice-agent dispatch + short-TTL join token via zero-new-dependency livekit_wire HS256/Twirp matching the installed SDK's wire format verified claim-for-claim on the .12 agent venv stdin-only; keys never reach the browser; unconfigured/no-profile/TTS-down refuse honestly and capabilities.voice stays false), chat_api_client speaks the exact real.ts RealChatAdapter protocol (per-turn health epoch, bounded 409 resync, robust incremental SSE parsing across multibyte/CRLF/multi-data chunk splits, fixed turn_id idempotency, immediate HTTP-stream close on barge-in without waiting for the next ASR final), llm_chat_api is a livekit llm.LLM adapter whose only backend is POST /api/chat so persona/mood/dyad/memory/life-sharing/budget gates and the one same-turn chat_lock govern text and voice identically (phi spoken verbatim via ChatChunk, never archived, no receipt), receipt.py confirms a sharing receipt only after real SDK playback completion with every negative (interrupted/failed/mid-playout/phi/incomplete/stale-epoch) fail-closed and exactly once per turn, stt_funasr/tts_cosyvoice reuse the proven .12 adapters verbatim with provenance, worker.py assembles the dedicated worker (independent voice_state_cybergril.json so D1 stays undecided, no second persona, cg.subtitle data-channel contract for the visual lead, CG-011 proactive seam left as an explicit no-op hook, built-in health port 8082); app.py keeps a minimal registration with voice_service injection for isolated tests; deployment drafts only (config/voice.example.yaml, worker.env.example, restart_cybergril.sh.example with exact-pattern process matching, serve_dual_8360_8361.py keeping HTTP 8360 and the HTTPS voice entry on ONE process sharing state/locks, runbook-hel877-voice.md contract+startup+rollback incl. the >=6GB GPU precondition and the do-not-copy historical fuser warning) - verification: 281 pytest pass on 3.11 venv + requirements.lock after merging the chief engineer's mid-flight HEL-874 rework e978e87 in full (parallel work preserved), verify_hel877_voice_chain.py 8/8 offline full-chain incl. socket-level barge-in fail-close and text/voice mutual same-gate cancellation, real-SDK check on the .12 livekit-agents 1.8.8 venv 8/8 ALL PASS via SSH stdin with zero writes zero network, dual-port single-process smoke on loopback 18360/18361 with a throwaway self-signed cert then stopped, ruff clean on all new code, dual-target requirement checks pass vs origin/hel-870-assembly (current tip e978e87) and origin/main with handoff-HEL-877.json; zero real LLM/embed/TTS/ASR requests, no mic/camera, no downloads/installs, no service start/stop, no 4080 access, no third-account or profile writes, web/ untouched, not merged, not deployed
Co-authored-by: multica-agent <github@multica.ai> ## HEL-877 交付摘要(R5 后台施工,未部署未合并) - 新独立模块 src/cybergril/voice/:专属 livekit worker(agent_name=cybergril-voice-agent,复用 .12 FunASR/CosyVoice/silero 底座,不 fork 不另搭平台,无第二人格/记忆,不直连 LLM);llm_chat_api 适配器与文字前端 real.ts 同协议走 POST /api/chat(epoch 409 有界重同步、SSE 分片、turn_id 幂等、打断即断流);/api/voice/session 建房+专属派发+短时限 token 全服务端(密钥不进浏览器;缺配置/无档案/TTS 停机诚实拒绝;capabilities.voice 不伪造);receipt 仅在 SDK 真实播放完成后确认(打断/失败/φ 轮/旧 epoch 全 fail-closed)。 - 部署草案:/opt/voice-agent-cybergril 目录、worker 健康端口 8082、HTTP 8360 + HTTPS 8361 同进程双入口(deploy/voice/serve_dual_8360_8361.py,共用同一状态/锁)、启动顺序含 TTS GPU≥6GB 前置与回滚;本卡未执行任何启停。契约(开关/字幕/失败/打断/挂断 + CG-011 主动开口接缝)见 docs/runbook-hel877-voice.md §3。 - 自测(零真实请求):281 pytest 全过(含语音 25 例:socket 级打断断流、SSE 边界、重放零调用、文字/语音同门互打断、回执全负例、token 房间隔离);scripts/verify_hel877_voice_chain.py 8/8;.12 agent venv(livekit-agents 1.8.8)真 SDK 检查 8/8(stdin-only 零写入零网络);双端口同进程本地冒烟通过;ruff 干净;需求检查双目标通过(含 handoff-HEL-877.json)。 - 施工中总工 HEL-874(e978e87)推上同分支,本卡已完整并入,不覆盖并行成果。未验证如实列明:端到端语音(TTS 停机/GPU 3.9GB<5.6GB)、真 LiveKit server 的 dispatch/delete、移动端实机——待接线/审核卡真机验收,未经真实验收不称 R5 完成。
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
"""Voice session endpoint tests (HEL-877): honest refusal, server-side dispatch,
|
||||
token room isolation, end/invalidation cleanup — all against the real router and
|
||||
real service with a fake LiveKit Twirp transport. Zero network."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
|
||||
from test_r2 import R2LLM # noqa: E402
|
||||
|
||||
from cybergril.api.app import create_app # noqa: E402
|
||||
from cybergril.life.runtime import LifeConfig # noqa: E402
|
||||
from cybergril.think.router import build_router # noqa: E402
|
||||
from cybergril.think.scenes import Tier # noqa: E402
|
||||
from cybergril.voice import livekit_wire # noqa: E402
|
||||
from cybergril.voice.session import VoiceConfig, VoiceSessionService # noqa: E402
|
||||
|
||||
CREATE = {"name": "语音测试", "birth": "2000-10-18", "hour": "亥时", "relation": "朋友"}
|
||||
|
||||
|
||||
class FakeLiveKit:
|
||||
"""Fake Twirp LiveKit server: records dispatches, room deletes, auth tokens."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.dispatches: list[dict] = []
|
||||
self.deleted_rooms: list[str] = []
|
||||
self.auth_tokens: list[str] = []
|
||||
self.fail = False
|
||||
self.transport = httpx.MockTransport(self._handle)
|
||||
self._dispatch_seq = 0
|
||||
|
||||
def _handle(self, request: httpx.Request) -> httpx.Response:
|
||||
self.auth_tokens.append(request.headers.get("authorization", ""))
|
||||
if self.fail:
|
||||
return httpx.Response(500, text="livekit down")
|
||||
path = request.url.path
|
||||
if path == "/twirp/livekit.AgentDispatchService/CreateDispatch":
|
||||
body = request.content
|
||||
agent_name, room = _parse_dispatch(body)
|
||||
self._dispatch_seq += 1
|
||||
self.dispatches.append({"agent_name": agent_name, "room": room,
|
||||
"proto": True, "content_type":
|
||||
request.headers.get("content-type", "")})
|
||||
return httpx.Response(200, json={"id": f"AD_{self._dispatch_seq}",
|
||||
"agentName": agent_name, "room": room})
|
||||
if path == "/twirp/livekit.RoomService/DeleteRoom":
|
||||
self.deleted_rooms.append(_parse_room(request.content))
|
||||
return httpx.Response(200, content=b"")
|
||||
return httpx.Response(404, text="unknown twirp route")
|
||||
|
||||
def service(self) -> VoiceSessionService:
|
||||
config = VoiceConfig(
|
||||
livekit_url="ws://voice.test:7880",
|
||||
livekit_internal="http://livekit.test",
|
||||
api_key="devkey",
|
||||
api_secret="test-secret-not-real",
|
||||
tts_health_url="http://tts.test/health",
|
||||
)
|
||||
return VoiceSessionService(config=config, http=httpx.AsyncClient(transport=self.transport))
|
||||
|
||||
|
||||
def _read_varint(data: bytes, index: int) -> tuple[int, int]:
|
||||
shift = 0
|
||||
value = 0
|
||||
while True:
|
||||
byte = data[index]
|
||||
value |= (byte & 0x7F) << shift
|
||||
index += 1
|
||||
if not byte & 0x80:
|
||||
return value, index
|
||||
shift += 7
|
||||
|
||||
|
||||
def _parse_dispatch(body: bytes) -> tuple[str, str]:
|
||||
fields = _parse_proto_strings(body)
|
||||
return fields.get(1, ""), fields.get(2, "")
|
||||
|
||||
|
||||
def _parse_room(body: bytes) -> str:
|
||||
return _parse_proto_strings(body).get(1, "")
|
||||
|
||||
|
||||
def _parse_proto_strings(data: bytes) -> dict[int, str]:
|
||||
out = {}
|
||||
index = 0
|
||||
while index < len(data):
|
||||
tag, index = _read_varint(data, index)
|
||||
field, wire = tag >> 3, tag & 7
|
||||
if wire != 2:
|
||||
break
|
||||
length, index = _read_varint(data, index)
|
||||
out[field] = data[index: index + length].decode()
|
||||
index += length
|
||||
return out
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fake_livekit():
|
||||
return FakeLiveKit()
|
||||
|
||||
|
||||
class FakeTTS:
|
||||
def __init__(self) -> None:
|
||||
self.ok = True
|
||||
self.calls = 0
|
||||
|
||||
def handler(self, request: httpx.Request) -> httpx.Response:
|
||||
self.calls += 1
|
||||
return httpx.Response(200) if self.ok else httpx.Response(503)
|
||||
|
||||
|
||||
def make_app(fake_livekit: FakeLiveKit, tts: FakeTTS):
|
||||
provider = R2LLM()
|
||||
router = build_router(providers={t: provider for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)})
|
||||
service = fake_livekit.service()
|
||||
service.http = httpx.AsyncClient(transport=_combined_transport(fake_livekit, tts))
|
||||
app = create_app(db_path=":memory:", router=router,
|
||||
life_config=LifeConfig(enabled=True, poll_seconds=3600),
|
||||
semantic_mode="stub", voice_service=service)
|
||||
return app, service, provider
|
||||
|
||||
|
||||
def _combined_transport(fake_livekit: FakeLiveKit, tts: FakeTTS) -> httpx.MockTransport:
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
if request.url.host == "tts.test":
|
||||
return tts.handler(request)
|
||||
return fake_livekit._handle(request)
|
||||
|
||||
return httpx.MockTransport(handler)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def instance(fake_livekit):
|
||||
tts = FakeTTS()
|
||||
app, service, provider = make_app(fake_livekit, tts)
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
with TestClient(app) as client:
|
||||
client.post("/api/companion", json=CREATE)
|
||||
yield client, fake_livekit, service, provider, tts
|
||||
|
||||
|
||||
def test_status_unconfigured_honest():
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
app = create_app(db_path=":memory:", router=build_router(
|
||||
providers={t: R2LLM() for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)}),
|
||||
life_config=LifeConfig(enabled=False), semantic_mode="stub")
|
||||
with TestClient(app) as client:
|
||||
status = client.get("/api/voice/status").json()
|
||||
assert status == {"configured": False, "tts_ok": False, "agent_name": None,
|
||||
"voice": False}
|
||||
# health stays honest: capabilities.voice never faked on
|
||||
assert client.get("/api/health").json()["capabilities"]["voice"] is False
|
||||
refusal = client.post("/api/voice/session")
|
||||
assert refusal.status_code == 503
|
||||
assert "语音还没有在这台服务上配置" in refusal.json()["detail"]
|
||||
|
||||
|
||||
def test_open_session_dispatches_dedicated_worker(instance):
|
||||
client, livekit, service, _, _ = instance
|
||||
response = client.post("/api/voice/session")
|
||||
assert response.status_code == 201, response.text
|
||||
body = response.json()
|
||||
# dedicated dispatch, not the generic cascade-voice-agent
|
||||
assert livekit.dispatches[-1]["agent_name"] == "cybergril-voice-agent"
|
||||
assert livekit.dispatches[-1]["room"] == body["room"]
|
||||
assert livekit.dispatches[-1]["proto"] is True
|
||||
assert livekit.dispatches[-1]["content_type"] == "application/protobuf"
|
||||
assert body["url"] == "ws://voice.test:7880"
|
||||
assert body["agent_name"] == "cybergril-voice-agent"
|
||||
# no livekit key material leaks into the browser payload
|
||||
assert "devkey" not in json.dumps(body) and "test-secret-not-real" not in json.dumps(body)
|
||||
|
||||
|
||||
def test_token_is_real_short_ttl_and_room_scoped(instance):
|
||||
client, livekit, _, _, _ = instance
|
||||
secret = "test-secret-not-real"
|
||||
first = client.post("/api/voice/session").json()
|
||||
second = client.post("/api/voice/session").json()
|
||||
assert first["room"] != second["room"], "each session gets its own room"
|
||||
for body in (first, second):
|
||||
claims, signing_input = livekit_wire.decode_token(body["token"])
|
||||
assert livekit_wire.verify_signature(body["token"], secret)
|
||||
assert claims["iss"] == "devkey"
|
||||
assert claims["sub"].startswith("user-")
|
||||
video = claims["video"]
|
||||
assert video["roomJoin"] is True and video["room"] == body["room"]
|
||||
assert video["canPublish"] is True and video["canSubscribe"] is True
|
||||
assert claims["exp"] - claims["nbf"] <= 300, "token must be short-lived"
|
||||
# token for room A grants nothing about room B (room isolation)
|
||||
claims_a, _ = livekit_wire.decode_token(first["token"])
|
||||
claims_b, _ = livekit_wire.decode_token(second["token"])
|
||||
assert claims_a["video"]["room"] != claims_b["video"]["room"]
|
||||
|
||||
|
||||
def test_open_session_refuses_without_companion(fake_livekit):
|
||||
tts = FakeTTS()
|
||||
app, service, _ = make_app(fake_livekit, tts)
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
with TestClient(app) as client:
|
||||
refusal = client.post("/api/voice/session")
|
||||
assert refusal.status_code == 409
|
||||
assert "还没有创建真实伴侣" in refusal.json()["detail"]
|
||||
assert not fake_livekit.dispatches, "no dispatch may happen without a profile"
|
||||
|
||||
|
||||
def test_open_session_refuses_when_tts_down(instance):
|
||||
client, livekit, service, _, tts = instance
|
||||
tts.ok = False
|
||||
service._tts_health.checked = -1e9 # bust cache
|
||||
refusal = client.post("/api/voice/session")
|
||||
assert refusal.status_code == 503
|
||||
assert "她的声音服务这会儿不在" in refusal.json()["detail"]
|
||||
assert not livekit.dispatches
|
||||
tts.ok = True
|
||||
service._tts_health.checked = -1e9
|
||||
assert client.post("/api/voice/session").status_code == 201
|
||||
|
||||
|
||||
def test_open_session_refuses_when_livekit_down(instance):
|
||||
client, livekit, _, _, _ = instance
|
||||
livekit.fail = True
|
||||
refusal = client.post("/api/voice/session")
|
||||
assert refusal.status_code == 503
|
||||
assert "通话服务暂时连不上" in refusal.json()["detail"]
|
||||
|
||||
|
||||
def test_end_session_deletes_room_and_cleans_registry(instance):
|
||||
client, livekit, service, _, _ = instance
|
||||
body = client.post("/api/voice/session").json()
|
||||
assert service.get(body["session_id"]) is not None
|
||||
ended = client.delete(f"/api/voice/session/{body['session_id']}")
|
||||
assert ended.status_code == 200 and ended.json()["ended"] is True
|
||||
assert livekit.deleted_rooms == [body["room"]]
|
||||
assert service.get(body["session_id"]) is None
|
||||
again = client.delete(f"/api/voice/session/{body['session_id']}")
|
||||
assert again.status_code == 404
|
||||
|
||||
|
||||
def test_registry_expiry_prunes_old_sessions(fake_livekit):
|
||||
import asyncio
|
||||
|
||||
service = fake_livekit.service()
|
||||
now = time.time()
|
||||
session = asyncio.run(service.open_session(epoch=7))
|
||||
assert service.get(session.session_id) is not None
|
||||
service.clock = lambda: now + service.config.session_ttl_seconds + 1
|
||||
assert service.get(session.session_id) is None, "expired registry entry pruned"
|
||||
asyncio.run(service.aclose())
|
||||
|
||||
|
||||
def test_worker_auth_tokens_are_server_jwt(instance):
|
||||
client, livekit, _, _, _ = instance
|
||||
client.post("/api/voice/session")
|
||||
auth = livekit.auth_tokens[-1]
|
||||
assert auth.startswith("Bearer ")
|
||||
token = auth.removeprefix("Bearer ")
|
||||
claims, _ = livekit_wire.decode_token(token)
|
||||
assert claims["iss"] == "devkey" and claims["sub"] == ""
|
||||
assert claims["exp"] - claims["nbf"] <= 60
|
||||
Reference in New Issue
Block a user