Files
CyberGril/scripts/verify_hel877_voice_chain.py
T
zonggongandmultica-agent 7f66015ac8 HEL-879 unified voice candidate assembled and verified on hel-879-assembly (isolated branch; PR#11 and hel-870-assembly untouched) - the merge brings exactly the audited HEL-877 voice backend (aba2aeb: src/cybergril/voice module with zero new dependencies, byte-identical to the author's head) and the HEL-897-verified voice frontend (c61d9c9: web/src byte-identical to the original author's head - the CSS hash change to index-BpZaybfD.css is entirely the author's own components.css +21 lines, no visual rework by this card); the one integration adjustment is test-harness alignment, not a product change: the voice adapter fixtures and verify_hel877_voice_chain.py built their router without a budget config, which the mainline's HEL-882 hard gate (guard.acquire before every real send) now correctly refuses at cap 0 - they now use the same tests/conftest.engineering_budget() every mainline test uses, no gate weakened, no src change on either author's side; full battery rerun on the assembled head: pytest 324/324 (299 mainline + 25 voice), ruff clean, web typecheck 0 errors / lint 0 errors 1 pre-existing warning / vitest 88/88 / build passes, real-server browser 9/9 with the HEL-896 precise-interaction revision (reach card over real HTTP, stage plays the reach-out, unread clears, message-id dedup, restart epoch+1, zero permission API calls, zero non-loopback outbound - evidence docs/rework/hel879/browser/), verify_hel877_voice_chain 8/8 offline full-chain, the .12 livekit-agents 1.8.8 real-SDK stdin check regenerated from THIS head's sources 8/8 ALL PASS (zero writes zero network), check_release_offline R1-R7 passes, pinned-SDK regressions on hel857-venv all green (close-ownership 16/16, shutdown-queued 12/12, inflight 6/6, semantic-recovery 7/7, mem0-real 12/12 - the seal change is exercised through the same PersistentBudget paths), requirement checks 4/4 (origin/main and c121c42, each with and without handoff-HEL-879.json); release-artifacts.json records the exact release code commit (01ff919), the tarball sha256 measured equal to the lock, and the cache-reusable wheels reverified with content digests after the disk-recovery cache incident; honest gaps kept open: real-call acceptance untested and unauthorized (D1 voice, trusted HTTPS device path, budgets), LiveKit room/worker release after API restart still lacks real-environment evidence (_prune only drops the in-memory registry, end_session does delete_room), incremental subtitles remain a contract gap not a completed feature, CG-011 proactive voice stays an explicit no-op; zero real LLM/embed/ASR/TTS, zero downloads, no mic, no service started or stopped on any shared machine, not deployed, nothing merged, authoritative ledgers/freezes/HEL-835 gate untouched, real caps stay 0
Co-authored-by: multica-agent <github@multica.ai>
2026-10-27 03:36:37 +08:00

242 lines
9.9 KiB
Python

"""HEL-877 offline full-chain verification (zero model / zero embed / zero TTS /
zero ASR / zero network beyond 127.0.0.1).
Runs the REAL voice wiring end to end with fakes only:
real app (fake LLM router) + real /api/voice/session router (fake LiveKit
Twirp + fake TTS health) + real ChatApiClient over a real local socket +
real receipt gate — the same protocol the text frontend and the .12 worker
adapters speak.
Run: PYTHONPATH=src .venv311/bin/python scripts/verify_hel877_voice_chain.py
"""
from __future__ import annotations
import asyncio
import contextlib
import socket
import sys
import tempfile
import threading
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
import httpx
import uvicorn
from test_r2 import R2LLM
from cybergril.api.app import create_app
from cybergril.life.runtime import LifeConfig
from cybergril.think.router import build_router
from cybergril.think.scenes import Tier
from cybergril.voice import livekit_wire
from cybergril.voice.chat_api_client import ChatApiClient
from cybergril.voice.receipt import should_ack
from cybergril.voice.session import VoiceConfig, VoiceSessionService
CREATE = {"name": "链路检查", "birth": "2000-10-18", "hour": "亥时", "relation": "朋友"}
class FakeLiveKit:
def __init__(self) -> None:
self.dispatches: list[dict] = []
self.deleted: list[str] = []
self.fail = False
def handler(self, request: httpx.Request) -> httpx.Response:
if self.fail:
return httpx.Response(500)
if request.url.path.endswith("CreateDispatch"):
agent, room = _dispatch_fields(request.content)
self.dispatches.append({"agent_name": agent, "room": room})
return httpx.Response(200, json={"id": f"AD_{len(self.dispatches)}", "room": room})
if request.url.path.endswith("DeleteRoom"):
self.deleted.append(_dispatch_fields(request.content)[0])
return httpx.Response(200)
return httpx.Response(404)
def _read_varint(data: bytes, i: int):
shift = value = 0
while True:
b = data[i]
value |= (b & 0x7F) << shift
i += 1
if not b & 0x80:
return value, i
shift += 7
def _dispatch_fields(data: bytes) -> tuple[str, str]:
out, i = {}, 0
while i < len(data):
tag, i = _read_varint(data, i)
if tag & 7 != 2:
break
n, i = _read_varint(data, i)
out[tag >> 3] = data[i:i + n].decode()
i += n
return out.get(1, ""), out.get(2, "")
def main() -> int:
ok: list[str] = []
livekit = FakeLiveKit()
tts_ok = {"flag": True}
provider = R2LLM()
def transport(request: httpx.Request) -> httpx.Response:
if request.url.host == "tts.test":
return httpx.Response(200) if tts_ok["flag"] else httpx.Response(503)
return livekit.handler(request)
service = VoiceSessionService(
config=VoiceConfig(
livekit_url="ws://voice.test:7880",
livekit_internal="http://livekit.test",
api_key="devkey",
api_secret="verify-secret-not-real",
tts_health_url="http://tts.test/health",
),
http=httpx.AsyncClient(transport=httpx.MockTransport(transport)),
)
# HEL-879 integration: mainline chat path hard-gates every send via
# router.guard.acquire (HEL-882); use the same approved engineering caps
# as the test suite so the chain check exercises the chain, not cap 0.
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from conftest import engineering_budget
router = build_router(
providers={t: provider for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)},
budget=engineering_budget(),
)
tmp = Path(tempfile.mkdtemp(prefix="hel877-"))
app = create_app(db_path=tmp / "chain.db", router=router,
life_config=LifeConfig(enabled=True, poll_seconds=3600),
semantic_mode="stub", voice_service=service)
probe = socket.socket()
probe.bind(("127.0.0.1", 0))
port = probe.getsockname()[1]
probe.close()
server = uvicorn.Server(uvicorn.Config(app, host="127.0.0.1", port=port,
log_level="error"))
threading.Thread(target=server.run, daemon=True).start()
for _ in range(100):
try:
httpx.get(f"http://127.0.0.1:{port}/api/health", timeout=1)
break
except Exception:
time.sleep(0.1)
base = f"http://127.0.0.1:{port}"
async def scenario() -> None:
# 1. honest refusals in order: no companion -> TTS down -> ok
http = httpx.AsyncClient(base_url=base)
refusal = await http.post("/api/voice/session")
assert refusal.status_code == 409, refusal.text
await http.post("/api/companion", json=CREATE)
tts_ok["flag"] = False
service._tts_health.checked = -1e9
refusal = await http.post("/api/voice/session")
assert refusal.status_code == 503 and "声音服务" in refusal.json()["detail"]
tts_ok["flag"] = True
service._tts_health.checked = -1e9
# 2. dedicated dispatch + short-TTL token, keys never leave the server
opened = (await http.post("/api/voice/session")).json()
assert livekit.dispatches[-1]["agent_name"] == "cybergril-voice-agent"
claims, _ = livekit_wire.decode_token(opened["token"])
assert livekit_wire.verify_signature(opened["token"], "verify-secret-not-real")
assert claims["video"]["roomJoin"] and claims["video"]["room"] == opened["room"]
assert claims["exp"] - claims["nbf"] <= 300
ok.append("session open: dedicated worker dispatch + genuine short-TTL room token")
# 3. two sessions never share a room
second = (await http.post("/api/voice/session")).json()
assert opened["room"] != second["room"]
ok.append("token room isolation: each session is its own room")
# 4. end: the room is deleted server-side, mic and job release with it
ended = await http.delete(f"/api/voice/session/{opened['session_id']}")
assert ended.json()["ended"] is True and livekit.deleted == [opened["room"]]
assert (await http.delete(f"/api/voice/session/{opened['session_id']}")).status_code == 404
ok.append("session end deletes the LiveKit room (disconnect + job exit path)")
# 5. the worker's chat adapter over a real socket: multi-turn + receipt
client = ChatApiClient(base)
turn1 = await client.send_turn("第一句你好")
turn2 = await client.send_turn("第二句还在")
assert turn1.completed and turn2.completed and turn1.receipt and turn2.receipt
calls = len(provider.calls)
# 6. replay: no new model call
replay = await client.send_turn("第一句你好", turn_id=turn1.turn_id)
assert replay.completed and len(provider.calls) == calls
ok.append("voice turns reuse the chat chain; replay adds zero model calls")
# 7. epoch resync after a server restart (409 + fresh epoch)
server.should_exit = True
await asyncio.sleep(0.5)
threading.Thread(target=lambda: uvicorn.Server(
uvicorn.Config(app, host="127.0.0.1", port=port, log_level="error")).run(),
daemon=True).start()
for _ in range(100):
try:
await httpx.AsyncClient().get(f"{base}/api/health", timeout=1)
break
except Exception:
await asyncio.sleep(0.1)
turn3 = await client.send_turn("重启之后", epoch=1) # stale epoch -> resync
assert turn3.completed and turn3.epoch > 1
ok.append("epoch 409 -> one bounded resync -> turn succeeds on the fresh epoch")
# 8. barge-in cancels the in-flight stream immediately; row = cancelled
provider.mode = "slow"
got = asyncio.Event()
task = asyncio.create_task(
client.send_turn("说一半我要打断你", on_delta=lambda _: got.set())
)
await asyncio.wait_for(got.wait(), timeout=10)
task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await task
provider.mode = ""
for _ in range(50):
rows = (await http.get("/api/messages")).json()["messages"]
row = next((m for m in rows if m["text"] == "说一半我要打断你"), None)
if row and row["status"] == "cancelled":
break
await asyncio.sleep(0.2)
assert row["status"] == "cancelled"
assert not any(m["role"] == "companion" and m["turn_id"] == row["turn_id"] for m in rows)
ok.append("barge-in closes the HTTP stream at once; aborted turn fails closed")
# 9. receipt gating: only a fully played-out turn acks, exactly once
assert should_ack(played_to_end=True, interrupted=False, failed=False,
receipt=turn1.receipt, current_epoch=turn1.epoch)
assert not should_ack(played_to_end=True, interrupted=True, failed=False,
receipt=turn1.receipt, current_epoch=turn1.epoch)
accepted = await client.acknowledge(turn1.receipt)
assert accepted, "server accepts the receipt of the delivered turn"
ok.append("receipt confirmed only after SDK playback completion, once per turn")
await client.aclose()
await http.aclose()
asyncio.run(scenario())
# 10. capabilities honesty: voice stays false in health (no fake readiness)
health = httpx.get(f"{base}/api/health").json()
assert health["capabilities"]["voice"] is False
ok.append("/api/health capabilities.voice stays false (R5 not real-accepted)")
print("HEL-877 offline voice-chain check: ALL PASS")
for line in ok:
print(" ✓", line)
return 0
if __name__ == "__main__":
raise SystemExit(main())